[libvirt] [PATCH] qemu: get VM's ip address from the output of arp command

Martin Kletzander mkletzan at redhat.com
Tue Jan 23 18:01:27 UTC 2018


On Tue, Jan 23, 2018 at 03:40:02PM +0800, Chen Hanxiao wrote:
>From: Chen Hanxiao <chenhanxiao at gmail.com>
>
>Introduce VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP to get ip address
>of VM from the output of `arp -an` command.
>

I'm not sure about the implications of this as sometimes we can't use that, but
I guess that's fine.

>We can use:
>  domifaddr f26-cloud --source arp
>to get the address.
>
>Signed-off-by: Chen Hanxiao <chenhanxiao at 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(+)
>

I see no documentation changes here.

>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"
>+

I think this should be determined like other programs in
`m4/virt-external-programs.m4`.

But rather than this, is this supported under non-Linux?  I don't know how arp
works elsewhere, but for Linux we could just parse /proc/net/arp ourselves.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: Digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20180123/9e60f60d/attachment-0001.sig>


More information about the libvir-list mailing list