From nobody Thu May 15 07:28:18 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1517215040466769.6875258282591; Mon, 29 Jan 2018 00:37:20 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2DF7B7C83B; Mon, 29 Jan 2018 08:37:19 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EDD447D51A; Mon, 29 Jan 2018 08:37:18 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 9AEBE18033DC; Mon, 29 Jan 2018 08:37:18 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w0T8ZriA012957 for ; Mon, 29 Jan 2018 03:35:53 -0500 Received: by smtp.corp.redhat.com (Postfix) id 952BF6EE75; Mon, 29 Jan 2018 08:35:53 +0000 (UTC) Received: from mx1.redhat.com (ext-mx10.extmail.prod.ext.phx2.redhat.com [10.5.110.39]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8D9006EE7A for ; Mon, 29 Jan 2018 08:35:50 +0000 (UTC) Received: from m15-114.126.com (m15-114.126.com [220.181.15.114]) by mx1.redhat.com (Postfix) with ESMTP id 288155F7AF for ; Mon, 29 Jan 2018 08:35:48 +0000 (UTC) Received: from localhost.localdomain (unknown [58.213.111.46]) by smtp7 (Coremail) with SMTP id DsmowAB3v8rY3G5aWHXEBQ--.13603S4; Mon, 29 Jan 2018 16:35:46 +0800 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id; bh=a7p5VR0nY72WoBi6Vh YrO078VbBmhaJNAe+1I8WHN0Q=; b=lNYUhW0c2SPybBKywqHJQmBVrI92d9V0+S 94PumwueWo+Fmp3HZz+OUUhrxFWX1iEPRh9a/o+yg40ldP2Kidtg3iEzpGyg++Kl E0ifezTtjOVFFw8Sj1/jsj8TyP3nwdyjaxGT0ovroVjdEArlqWtKzNSBG/eqsuRA uH6FaoOo8= From: Chen Hanxiao To: libvir-list@redhat.com Date: Mon, 29 Jan 2018 16:35:34 +0800 Message-Id: <20180129083536.24554-3-chen_han_xiao@126.com> In-Reply-To: <20180129083536.24554-1-chen_han_xiao@126.com> References: <20180129083536.24554-1-chen_han_xiao@126.com> X-CM-TRANSID: DsmowAB3v8rY3G5aWHXEBQ--.13603S4 X-Coremail-Antispam: 1Uf129KBjvJXoW3JFykCw4rKF4UJF17KryDJrb_yoW7GrWrpF WUXFs5trW8Gw1fGrZxGa17XFn8W3y8KF9xGFsa9a1vvr18Gr9rXw1xWw47ZFyjyr4fXFy7 CrZFgayUWF4jgFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07j1tC7UUUUU= X-Originating-IP: [58.213.111.46] X-CM-SenderInfo: xfkh0spkdqs5xldrqiyswou0bp/1tbiFgLcrlpD4slLTAAAsT X-Greylist: Sender passed SPF test, ACL 227 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Mon, 29 Jan 2018 08:35:49 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Mon, 29 Jan 2018 08:35:49 +0000 (UTC) for IP:'220.181.15.114' DOMAIN:'m15-114.126.com' HELO:'m15-114.126.com' FROM:'chen_han_xiao@126.com' RCPT:'' X-RedHat-Spam-Score: 1.99 * (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_PSBL, SPF_PASS, T_RP_MATCHES_RCVD) 220.181.15.114 m15-114.126.com 220.181.15.114 m15-114.126.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.39 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Cc: Chen Hanxiao Subject: [libvirt] [PATCH v3 2/4] qemu: introduce qemuARPGetInterfaces to get IP from host's arp table X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Mon, 29 Jan 2018 08:37:19 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" From: Chen Hanxiao 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 --- 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-dom= ain.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 =3D 0, /* Parse DHCP lease fi= le */ VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT =3D 1, /* Query qemu guest ag= ent */ + VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP =3D 2, /* Query ARP tables */ =20 # 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); =20 +static int qemuARPGetInterfaces(virDomainObjPtr vm, + virDomainInterfacePtr **ifaces); + static virQEMUDriverPtr qemu_driver; =20 =20 @@ -20384,6 +20388,10 @@ qemuDomainInterfaceAddresses(virDomainPtr dom, =20 break; =20 + case VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP: + ret =3D qemuARPGetInterfaces(vm, ifaces); + break; + default: virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, _("Unknown IP address data source %d"), @@ -20494,6 +20502,94 @@ qemuGetDHCPInterfaces(virDomainPtr dom, } =20 =20 +static int +qemuARPGetInterfaces(virDomainObjPtr vm, + virDomainInterfacePtr **ifaces) +{ + size_t i, j; + size_t ifaces_count =3D 0; + int ret =3D -1; + char macaddr[VIR_MAC_STRING_BUFLEN]; + virDomainInterfacePtr *ifaces_ret =3D NULL; + virDomainInterfacePtr iface =3D NULL; + virArpTablePtr table; + int got_one =3D 0; + + table =3D virArpTableGet(); + if (!table) + goto cleanup; + + for (i =3D 0; i < vm->def->nnets; i++) { + got_one =3D 0; + if (vm->def->nets[i]->type !=3D VIR_DOMAIN_NET_TYPE_NETWORK) + continue; + + virMacAddrFormat(&(vm->def->nets[i]->mac), macaddr); + virArpTableEntry entry; + for (j =3D 0; j < table->n; j++) { + entry =3D table->t[j]; + if (STREQ(entry.mac, macaddr)) { + got_one =3D 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 =3D ifaces_ret[ifaces_count - 1]; + iface->naddrs =3D 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 =3D=3D 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 =3D ifaces_ret[ifaces_count - 1]; + iface->naddrs =3D 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 =3D ifaces_ret; + ifaces_ret =3D NULL; + ret =3D ifaces_count; + + cleanup: + virArpTableFree(table); + + if (ifaces_ret) { + for (i =3D 0; i < ifaces_count; i++) + virDomainInterfaceFree(ifaces_ret[i]); + } + VIR_FREE(ifaces_ret); + + return ret; +} + + static int qemuDomainSetUserPassword(virDomainPtr dom, const char *user, --=20 2.14.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list