From: Chen Hanxiao <chenhanxiao(a)gmail.com>
Introduce VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP to get ip address
of VM from the output of `arp -an` command.
We can use:
domifaddr f26-cloud --source arp
to get the address.
Signed-off-by: Chen Hanxiao <chenhanxiao(a)gmail.com>
---
include/libvirt/libvirt-domain.h | 1 +
src/qemu/qemu_driver.c | 102 +++++++++++++++++++++++++++++++++++++++
tools/virsh-domain-monitor.c | 2 +
3 files changed, 105 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/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index a203c9297..5aaf69442 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -160,6 +160,9 @@ static int qemuGetDHCPInterfaces(virDomainPtr dom,
virDomainObjPtr vm,
virDomainInterfacePtr **ifaces);
+static int qemuARPGetInterfaces(virDomainObjPtr vm,
+ virDomainInterfacePtr **ifaces);
+
static virQEMUDriverPtr qemu_driver;
@@ -20384,6 +20387,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 +20501,101 @@ 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;
+ virCommandPtr cmd = NULL;
+ char *outbuf;
+ char **lines = NULL;
+ char **matches = NULL;
+#define ARP_CMD "/usr/sbin/arp"
+
+ if (!(cmd = virCommandNewArgList(ARP_CMD,
+ "-an",
+ NULL)))
+ goto cleanup;
+
+ virCommandSetOutputBuffer(cmd, &outbuf);
+
+ if (virCommandRun(cmd, NULL) < 0)
+ goto cleanup;
+
+ lines = virStringSplit(outbuf, "\n", 0);
+ if (lines == NULL)
+ goto cleanup;
+
+ 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);
+
+ if (strstr(outbuf, macaddr)) {
+ for (j = 0; lines[j]; j++) {
+ const char *line = lines[j];
+ if (line == NULL)
+ break;
+ if (strstr(line, macaddr) &&
+ virStringSearch(line,
"\\b([0-9]{1,3}\\.){3}[0-9]{1,3}\\b",
+ 1, &matches) == 1) {
+
+ 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];
+ iface->naddrs = 1;
+ 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;
+
+ if (VIR_STRDUP(iface->addrs->addr, matches[0]) < 0)
+ goto cleanup;
+ }
+ }
+ } else {
+ VIR_DEBUG("Got nothing");
+ continue;
+ }
+ }
+
+ *ifaces = ifaces_ret;
+ ifaces_ret = NULL;
+ ret = ifaces_count;
+
+ cleanup:
+ virCommandFree(cmd);
+ VIR_FREE(outbuf);
+ virStringListFree(lines);
+ virStringListFree(matches);
+ cmd = NULL;
+ return ret;
+
+ 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,
const char *user,
diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 32a42707e..68da11ed5 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -2190,6 +2190,8 @@ cmdDomIfAddr(vshControl *ctl, const vshCmd *cmd)
source = VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE;
} else if (STREQ(sourcestr, "agent")) {
source = VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT;
+ } else if (STREQ(sourcestr, "arp")) {
+ source = VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP;
} else {
vshError(ctl, _("Unknown data source '%s'"), sourcestr);
goto cleanup;
--
2.14.3