From: Chen Hanxiao <chenhanxiao@gmail.com>
introduce VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP to get ip address
of VM from the output of /proc/net/arp
Signed-off-by: Chen Hanxiao <chenhanxiao@gmail.com>
---
v3:
add docs in virDomainInterfaceAddresses
remove error label
show network interface which did not match the arp table
include/libvirt/libvirt-domain.h | 1 +
src/libvirt-domain.c | 7 +++
src/qemu/qemu_driver.c | 96 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 104 insertions(+)
diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h
index 4048acf38..38e2d9a3e 100644
--- a/include/libvirt/libvirt-domain.h
+++ b/include/libvirt/libvirt-domain.h
@@ -4665,6 +4665,7 @@ typedef virMemoryParameter *virMemoryParameterPtr;
typedef enum {
VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE = 0, /* Parse DHCP lease file */
VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT = 1, /* Query qemu guest agent */
+ VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP = 2, /* Query ARP tables */
# ifdef VIR_ENUM_SENTINELS
VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LAST
diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c
index eaec0979a..1ae83610d 100644
--- a/src/libvirt-domain.c
+++ b/src/libvirt-domain.c
@@ -11721,6 +11721,13 @@ virDomainFSInfoFree(virDomainFSInfoPtr info)
* To match such interface with the one from @dom XML use MAC address or IP
* range.
*
+ * If @source is VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP, the /proc/net/arp
+ * will be check to obtain the interface addresses.
+ * As the arp cache did not refresh in time, the returned ip address
+ * may be unreachable.
+ * As the route config of the guest, the returned mac address
+ * may be duplicated.
+ *
* @ifaces->name and @ifaces->hwaddr are never NULL.
*
* The caller *must* free @ifaces when no longer needed. Usual use case
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index a203c9297..d0cb88053 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -70,6 +70,7 @@
#include "virnetdevopenvswitch.h"
#include "capabilities.h"
#include "viralloc.h"
+#include "virarptable.h"
#include "viruuid.h"
#include "domain_conf.h"
#include "domain_audit.h"
@@ -160,6 +161,9 @@ static int qemuGetDHCPInterfaces(virDomainPtr dom,
virDomainObjPtr vm,
virDomainInterfacePtr **ifaces);
+static int qemuARPGetInterfaces(virDomainObjPtr vm,
+ virDomainInterfacePtr **ifaces);
+
static virQEMUDriverPtr qemu_driver;
@@ -20384,6 +20388,10 @@ qemuDomainInterfaceAddresses(virDomainPtr dom,
break;
+ case VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP:
+ ret = qemuARPGetInterfaces(vm, ifaces);
+ break;
+
default:
virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED,
_("Unknown IP address data source %d"),
@@ -20494,6 +20502,94 @@ qemuGetDHCPInterfaces(virDomainPtr dom,
}
+static int
+qemuARPGetInterfaces(virDomainObjPtr vm,
+ virDomainInterfacePtr **ifaces)
+{
+ size_t i, j;
+ size_t ifaces_count = 0;
+ int ret = -1;
+ char macaddr[VIR_MAC_STRING_BUFLEN];
+ virDomainInterfacePtr *ifaces_ret = NULL;
+ virDomainInterfacePtr iface = NULL;
+ virArpTablePtr table;
+ int got_one = 0;
+
+ table = virArpTableGet();
+ if (!table)
+ goto cleanup;
+
+ for (i = 0; i < vm->def->nnets; i++) {
+ got_one = 0;
+ if (vm->def->nets[i]->type != VIR_DOMAIN_NET_TYPE_NETWORK)
+ continue;
+
+ virMacAddrFormat(&(vm->def->nets[i]->mac), macaddr);
+ virArpTableEntry entry;
+ for (j = 0; j < table->n; j++) {
+ entry = table->t[j];
+ if (STREQ(entry.mac, macaddr)) {
+ got_one = 1;
+ if (VIR_EXPAND_N(ifaces_ret, ifaces_count, 1) < 0)
+ goto cleanup;
+ if (VIR_ALLOC(ifaces_ret[ifaces_count - 1]) < 0)
+ goto cleanup;
+
+ iface = ifaces_ret[ifaces_count - 1];
+ iface->naddrs = 1;
+ if (VIR_ALLOC_N(iface->addrs, iface->naddrs) < 0)
+ goto cleanup;
+
+ if (VIR_STRDUP(iface->name, vm->def->nets[i]->ifname) < 0)
+ goto cleanup;
+
+ if (VIR_STRDUP(iface->hwaddr, macaddr) < 0)
+ goto cleanup;
+
+ if (VIR_STRDUP(iface->addrs->addr, entry.ipaddr) < 0)
+ goto cleanup;
+ }
+ }
+
+ if (got_one == 0) {
+ if (VIR_EXPAND_N(ifaces_ret, ifaces_count, 1) < 0)
+ goto cleanup;
+ if (VIR_ALLOC(ifaces_ret[ifaces_count - 1]) < 0)
+ goto cleanup;
+
+ iface = ifaces_ret[ifaces_count - 1];
+ iface->naddrs = 1;
+ if (VIR_ALLOC_N(iface->addrs, iface->naddrs) < 0)
+ goto cleanup;
+
+ if (VIR_STRDUP(iface->name, "") < 0)
+ goto cleanup;
+
+ if (VIR_STRDUP(iface->hwaddr, macaddr) < 0)
+ goto cleanup;
+
+ if (VIR_STRDUP(iface->addrs->addr, "") < 0)
+ goto cleanup;
+ }
+ }
+
+ *ifaces = ifaces_ret;
+ ifaces_ret = NULL;
+ ret = ifaces_count;
+
+ cleanup:
+ virArpTableFree(table);
+
+ if (ifaces_ret) {
+ for (i = 0; i < ifaces_count; i++)
+ virDomainInterfaceFree(ifaces_ret[i]);
+ }
+ VIR_FREE(ifaces_ret);
+
+ return ret;
+}
+
+
static int
qemuDomainSetUserPassword(virDomainPtr dom,
const char *user,
--
2.14.3
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
On Mon, Jan 29, 2018 at 16:35:34 +0800, Chen Hanxiao wrote: > From: Chen Hanxiao <chenhanxiao@gmail.com> > > introduce VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP to get ip address > of VM from the output of /proc/net/arp > > Signed-off-by: Chen Hanxiao <chenhanxiao@gmail.com> > --- > v3: > add docs in virDomainInterfaceAddresses > remove error label > show network interface which did not match the arp table > > include/libvirt/libvirt-domain.h | 1 + > src/libvirt-domain.c | 7 +++ > src/qemu/qemu_driver.c | 96 ++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 104 insertions(+) > > diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h > index 4048acf38..38e2d9a3e 100644 > --- a/include/libvirt/libvirt-domain.h > +++ b/include/libvirt/libvirt-domain.h > @@ -4665,6 +4665,7 @@ typedef virMemoryParameter *virMemoryParameterPtr; > typedef enum { > VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE = 0, /* Parse DHCP lease file */ > VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT = 1, /* Query qemu guest agent */ > + VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP = 2, /* Query ARP tables */ > > # ifdef VIR_ENUM_SENTINELS > VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LAST > diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c > index eaec0979a..1ae83610d 100644 > --- a/src/libvirt-domain.c > +++ b/src/libvirt-domain.c > @@ -11721,6 +11721,13 @@ virDomainFSInfoFree(virDomainFSInfoPtr info) > * To match such interface with the one from @dom XML use MAC address or IP > * range. > * > + * If @source is VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP, the /proc/net/arp > + * will be check to obtain the interface addresses. > + * As the arp cache did not refresh in time, the returned ip address > + * may be unreachable. > + * As the route config of the guest, the returned mac address > + * may be duplicated. > + * > * @ifaces->name and @ifaces->hwaddr are never NULL. > * > * The caller *must* free @ifaces when no longer needed. Usual use case > diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c > index a203c9297..d0cb88053 100644 > --- a/src/qemu/qemu_driver.c > +++ b/src/qemu/qemu_driver.c > @@ -70,6 +70,7 @@ > #include "virnetdevopenvswitch.h" > #include "capabilities.h" > #include "viralloc.h" > +#include "virarptable.h" > #include "viruuid.h" > #include "domain_conf.h" > #include "domain_audit.h" > @@ -160,6 +161,9 @@ static int qemuGetDHCPInterfaces(virDomainPtr dom, > virDomainObjPtr vm, > virDomainInterfacePtr **ifaces); > > +static int qemuARPGetInterfaces(virDomainObjPtr vm, > + virDomainInterfacePtr **ifaces); > + > static virQEMUDriverPtr qemu_driver; > > > @@ -20384,6 +20388,10 @@ qemuDomainInterfaceAddresses(virDomainPtr dom, > > break; > > + case VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP: > + ret = qemuARPGetInterfaces(vm, ifaces); > + break; > + > default: > virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, > _("Unknown IP address data source %d"), > @@ -20494,6 +20502,94 @@ qemuGetDHCPInterfaces(virDomainPtr dom, > } > > > +static int > +qemuARPGetInterfaces(virDomainObjPtr vm, > + virDomainInterfacePtr **ifaces) > +{ > + size_t i, j; > + size_t ifaces_count = 0; > + int ret = -1; > + char macaddr[VIR_MAC_STRING_BUFLEN]; > + virDomainInterfacePtr *ifaces_ret = NULL; > + virDomainInterfacePtr iface = NULL; > + virArpTablePtr table; > + int got_one = 0; Isn't this a bool in fact? > + > + table = virArpTableGet(); > + if (!table) > + goto cleanup; > + > + for (i = 0; i < vm->def->nnets; i++) { > + got_one = 0; > + if (vm->def->nets[i]->type != VIR_DOMAIN_NET_TYPE_NETWORK) > + continue; > + > + virMacAddrFormat(&(vm->def->nets[i]->mac), macaddr); > + virArpTableEntry entry; > + for (j = 0; j < table->n; j++) { > + entry = table->t[j]; > + if (STREQ(entry.mac, macaddr)) { > + got_one = 1; > + if (VIR_EXPAND_N(ifaces_ret, ifaces_count, 1) < 0) > + goto cleanup; > + if (VIR_ALLOC(ifaces_ret[ifaces_count - 1]) < 0) > + goto cleanup; > + VIR_APPEND_ELEMENT Looks like you inspired youself in qemuAgentGetInterfaces which isn't probably the right place for nice code. > + iface = ifaces_ret[ifaces_count - 1]; > + iface->naddrs = 1; > + if (VIR_ALLOC_N(iface->addrs, iface->naddrs) < 0) > + goto cleanup; > + > + if (VIR_STRDUP(iface->name, vm->def->nets[i]->ifname) < 0) > + goto cleanup; > + > + if (VIR_STRDUP(iface->hwaddr, macaddr) < 0) > + goto cleanup; > + > + if (VIR_STRDUP(iface->addrs->addr, entry.ipaddr) < 0) > + goto cleanup; > + } > + } > + > + if (got_one == 0) { Why do you add a dummy entry if given interface does not have one? No other gathering method does this. > + if (VIR_EXPAND_N(ifaces_ret, ifaces_count, 1) < 0) > + goto cleanup; > + if (VIR_ALLOC(ifaces_ret[ifaces_count - 1]) < 0) > + goto cleanup; > + > + iface = ifaces_ret[ifaces_count - 1]; > + iface->naddrs = 1; > + if (VIR_ALLOC_N(iface->addrs, iface->naddrs) < 0) > + goto cleanup; > + > + if (VIR_STRDUP(iface->name, "") < 0) > + goto cleanup; > + > + if (VIR_STRDUP(iface->hwaddr, macaddr) < 0) > + goto cleanup; > + > + if (VIR_STRDUP(iface->addrs->addr, "") < 0) > + goto cleanup; > + } > + } > + > + *ifaces = ifaces_ret; > + ifaces_ret = NULL; VIR_STEAL_PTR > + ret = ifaces_count; > + > + cleanup: > + virArpTableFree(table); > + > + if (ifaces_ret) { > + for (i = 0; i < ifaces_count; i++) > + virDomainInterfaceFree(ifaces_ret[i]); > + } > + VIR_FREE(ifaces_ret); > + > + return ret; > +} > + > + > static int > qemuDomainSetUserPassword(virDomainPtr dom, > const char *user, > -- > 2.14.3 > > -- > libvir-list mailing list > libvir-list@redhat.com > https://www.redhat.com/mailman/listinfo/libvir-list -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
© 2016 - 2025 Red Hat, Inc.