so we can use it from the LXC driver as well. --- I couldn't find a nice place to add this so I went for a separate file. I'm happy to move this elsewhere. Cheers, -- Guido po/POTFILES.in | 1 + src/Makefile.am | 1 + src/libvirt_private.syms | 4 ++ src/qemu/qemu_driver.c | 104 +---------------------------------- src/util/virdhcpinterfaces.c | 127 +++++++++++++++++++++++++++++++++++++++++++ src/util/virdhcpinterfaces.h | 34 ++++++++++++ 6 files changed, 169 insertions(+), 102 deletions(-) create mode 100644 src/util/virdhcpinterfaces.c create mode 100644 src/util/virdhcpinterfaces.h diff --git a/po/POTFILES.in b/po/POTFILES.in index 82e8d3e..64eaa9d 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -186,6 +186,7 @@ src/util/vircommand.c src/util/virconf.c src/util/vircrypto.c src/util/virdbus.c +src/util/virdhcpinterfaces.c src/util/virdnsmasq.c src/util/virerror.c src/util/virerror.h diff --git a/src/Makefile.am b/src/Makefile.am index a4aef0f..bddfd21 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -104,6 +104,7 @@ UTIL_SOURCES = \ util/virconf.c util/virconf.h \ util/vircrypto.c util/vircrypto.h \ util/virdbus.c util/virdbus.h util/virdbuspriv.h \ + util/virdhcpinterfaces.c util/virdhcpinterfaces.h \ util/virdnsmasq.c util/virdnsmasq.h \ util/virebtables.c util/virebtables.h \ util/virendian.h \ diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 3d0ec9d..ea3e83c 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1377,6 +1377,10 @@ virDBusMessageRead; virDBusSetSharedBus; +# util/virdhcpinterfaces.h +virGetDHCPInterfaces; + + # util/virdnsmasq.h dnsmasqAddDhcpHost; dnsmasqAddHost; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index abcdbe6..fe1cb6d 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -101,6 +101,7 @@ #include "virnuma.h" #include "dirname.h" #include "network/bridge_driver.h" +#include "virdhcpinterfaces.h" #define VIR_FROM_THIS VIR_FROM_QEMU @@ -173,10 +174,6 @@ static int qemuOpenFileAs(uid_t fallback_uid, gid_t fallback_gid, const char *path, int oflags, bool *needUnlink, bool *bypassSecurityDriver); -static int qemuGetDHCPInterfaces(virDomainPtr dom, - virDomainObjPtr vm, - virDomainInterfacePtr **ifaces); - virQEMUDriverPtr qemu_driver = NULL; @@ -19794,7 +19791,7 @@ qemuDomainInterfaceAddresses(virDomainPtr dom, switch (source) { case VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE: - ret = qemuGetDHCPInterfaces(dom, vm, ifaces); + ret = virGetDHCPInterfaces(dom, vm, ifaces); break; case VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT: @@ -19825,103 +19822,6 @@ qemuDomainInterfaceAddresses(virDomainPtr dom, return ret; } -static int -qemuGetDHCPInterfaces(virDomainPtr dom, - virDomainObjPtr vm, - virDomainInterfacePtr **ifaces) -{ - int rv = -1; - int n_leases = 0; - size_t i, j; - size_t ifaces_count = 0; - virNetworkPtr network = NULL; - char macaddr[VIR_MAC_STRING_BUFLEN]; - virDomainInterfacePtr iface = NULL; - virNetworkDHCPLeasePtr *leases = NULL; - virDomainInterfacePtr *ifaces_ret = NULL; - - if (!dom->conn->networkDriver || - !dom->conn->networkDriver->networkGetDHCPLeases) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Network driver does not support DHCP lease query")); - return -1; - } - - for (i = 0; i < vm->def->nnets; i++) { - if (vm->def->nets[i]->type != VIR_DOMAIN_NET_TYPE_NETWORK) - continue; - - virMacAddrFormat(&(vm->def->nets[i]->mac), macaddr); - virObjectUnref(network); - network = virNetworkLookupByName(dom->conn, - vm->def->nets[i]->data.network.name); - - if ((n_leases = virNetworkGetDHCPLeases(network, macaddr, - &leases, 0)) < 0) - goto error; - - if (n_leases) { - if (VIR_EXPAND_N(ifaces_ret, ifaces_count, 1) < 0) - goto error; - - if (VIR_ALLOC(ifaces_ret[ifaces_count - 1]) < 0) - goto error; - - iface = ifaces_ret[ifaces_count - 1]; - /* Assuming each lease corresponds to a separate IP */ - iface->naddrs = n_leases; - - if (VIR_ALLOC_N(iface->addrs, iface->naddrs) < 0) - goto error; - - if (VIR_STRDUP(iface->name, vm->def->nets[i]->ifname) < 0) - goto cleanup; - - if (VIR_STRDUP(iface->hwaddr, macaddr) < 0) - goto cleanup; - } - - for (j = 0; j < n_leases; j++) { - virNetworkDHCPLeasePtr lease = leases[j]; - virDomainIPAddressPtr ip_addr = &iface->addrs[j]; - - if (VIR_STRDUP(ip_addr->addr, lease->ipaddr) < 0) - goto cleanup; - - ip_addr->type = lease->type; - ip_addr->prefix = lease->prefix; - } - - for (j = 0; j < n_leases; j++) - virNetworkDHCPLeaseFree(leases[j]); - - VIR_FREE(leases); - } - - *ifaces = ifaces_ret; - ifaces_ret = NULL; - rv = ifaces_count; - - cleanup: - virObjectUnref(network); - if (leases) { - for (i = 0; i < n_leases; i++) - virNetworkDHCPLeaseFree(leases[i]); - } - VIR_FREE(leases); - - return rv; - - error: - if (ifaces_ret) { - for (i = 0; i < ifaces_count; i++) - virDomainInterfaceFree(ifaces_ret[i]); - } - VIR_FREE(ifaces_ret); - - goto cleanup; -} - static int qemuDomainSetUserPassword(virDomainPtr dom, diff --git a/src/util/virdhcpinterfaces.c b/src/util/virdhcpinterfaces.c new file mode 100644 index 0000000..80135f7 --- /dev/null +++ b/src/util/virdhcpinterfaces.c @@ -0,0 +1,127 @@ +/* + * virdhcpinterfaces.c: get a domains dhcp managed interfaces + * + * Copyright (C) 2016 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + * + * Author: Daniel P. Berrange <berrange@xxxxxxxxxx> + */ + +#include <config.h> + +#include "virdhcpinterfaces.h" +#include "viralloc.h" +#include "virstring.h" +#include "datatypes.h" + +#define VIR_FROM_THIS VIR_FROM_AUDIT + +int +virGetDHCPInterfaces(virDomainPtr dom, + virDomainObjPtr vm, + virDomainInterfacePtr **ifaces) +{ + int rv = -1; + int n_leases = 0; + size_t i, j; + size_t ifaces_count = 0; + virNetworkPtr network = NULL; + char macaddr[VIR_MAC_STRING_BUFLEN]; + virDomainInterfacePtr iface = NULL; + virNetworkDHCPLeasePtr *leases = NULL; + virDomainInterfacePtr *ifaces_ret = NULL; + + if (!dom->conn->networkDriver || + !dom->conn->networkDriver->networkGetDHCPLeases) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Network driver does not support DHCP lease query")); + return -1; + } + + for (i = 0; i < vm->def->nnets; i++) { + if (vm->def->nets[i]->type != VIR_DOMAIN_NET_TYPE_NETWORK) + continue; + + virMacAddrFormat(&(vm->def->nets[i]->mac), macaddr); + virObjectUnref(network); + network = virNetworkLookupByName(dom->conn, + vm->def->nets[i]->data.network.name); + + if ((n_leases = virNetworkGetDHCPLeases(network, macaddr, + &leases, 0)) < 0) + goto error; + + if (n_leases) { + if (VIR_EXPAND_N(ifaces_ret, ifaces_count, 1) < 0) + goto error; + + if (VIR_ALLOC(ifaces_ret[ifaces_count - 1]) < 0) + goto error; + + iface = ifaces_ret[ifaces_count - 1]; + /* Assuming each lease corresponds to a separate IP */ + iface->naddrs = n_leases; + + if (VIR_ALLOC_N(iface->addrs, iface->naddrs) < 0) + goto error; + + if (VIR_STRDUP(iface->name, vm->def->nets[i]->ifname) < 0) + goto cleanup; + + if (VIR_STRDUP(iface->hwaddr, macaddr) < 0) + goto cleanup; + } + + for (j = 0; j < n_leases; j++) { + virNetworkDHCPLeasePtr lease = leases[j]; + virDomainIPAddressPtr ip_addr = &iface->addrs[j]; + + if (VIR_STRDUP(ip_addr->addr, lease->ipaddr) < 0) + goto cleanup; + + ip_addr->type = lease->type; + ip_addr->prefix = lease->prefix; + } + + for (j = 0; j < n_leases; j++) + virNetworkDHCPLeaseFree(leases[j]); + + VIR_FREE(leases); + } + + *ifaces = ifaces_ret; + ifaces_ret = NULL; + rv = ifaces_count; + + cleanup: + virObjectUnref(network); + if (leases) { + for (i = 0; i < n_leases; i++) + virNetworkDHCPLeaseFree(leases[i]); + } + VIR_FREE(leases); + + return rv; + + error: + if (ifaces_ret) { + for (i = 0; i < ifaces_count; i++) + virDomainInterfaceFree(ifaces_ret[i]); + } + VIR_FREE(ifaces_ret); + + goto cleanup; +} diff --git a/src/util/virdhcpinterfaces.h b/src/util/virdhcpinterfaces.h new file mode 100644 index 0000000..c7acb8f --- /dev/null +++ b/src/util/virdhcpinterfaces.h @@ -0,0 +1,34 @@ +/* + * virdhcpinterfaces.h: get a domains dhcp managed interfaces + * + * Copyright (C) 2016 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + * + * Author: Daniel P. Berrange <berrange@xxxxxxxxxx> + */ + +#ifndef __VIR_DHCP_INTERFACES_H__ +# define __VIR_DHCP_INTERFACES_H__ + +# include "internal.h" +# include "virdomainobjlist.h" + +int +virGetDHCPInterfaces(virDomainPtr dom, + virDomainObjPtr vm, + virDomainInterfacePtr **ifaces); + +#endif /* __VIR_DHCP_INTERFACES_H__ */ -- 2.7.0.rc3 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list