From nobody Thu May 15 00:30:28 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; dmarc=fail(p=none dis=none) header.from=126.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1520493341445338.2275488772344; Wed, 7 Mar 2018 23:15:41 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A1E07C04BD5A; Thu, 8 Mar 2018 07:15:39 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 464FD5D6B7; Thu, 8 Mar 2018 07:15:39 +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 EDEC84CA99; Thu, 8 Mar 2018 07:15:38 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w287DE2M021149 for ; Thu, 8 Mar 2018 02:13:14 -0500 Received: by smtp.corp.redhat.com (Postfix) id 0B1A55D77E; Thu, 8 Mar 2018 07:13:14 +0000 (UTC) Received: from mx1.redhat.com (ext-mx02.extmail.prod.ext.phx2.redhat.com [10.5.110.26]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 041A95D6A6 for ; Thu, 8 Mar 2018 07:13:11 +0000 (UTC) Received: from m15-113.126.com (m15-113.126.com [220.181.15.113]) by mx1.redhat.com (Postfix) with ESMTP id 9B65685A02 for ; Thu, 8 Mar 2018 07:13:08 +0000 (UTC) Received: from localhost.localdomain (unknown [58.213.111.46]) by smtp3 (Coremail) with SMTP id DcmowACni0Fq4qBaI2dGBA--.64188S3; Thu, 08 Mar 2018 15:13:07 +0800 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id; bh=FriFE9T+q9mrkKeTyG USJWeE63jLHlvkGDvl6pzmfnI=; b=IBhk6xWwGdiZM5H8KtrOq1xTBnR3Tekh9I MxjjXJ6Xog3kTBTCQWdsOP44qakTWp4P37oxkRpdajEoBRJ0lCPW4U5GM19xOI3s wqtcT3curAcb/BEHTEFop2aE3Us8n/WAGnzYYkRYtWUL8Sod0bIBJy0VDYFnx78S iLj68QTlA= From: Chen Hanxiao To: libvir-list@redhat.com Date: Thu, 8 Mar 2018 15:11:55 +0800 Message-Id: <20180308071159.9861-2-chen_han_xiao@126.com> In-Reply-To: <20180308071159.9861-1-chen_han_xiao@126.com> References: <20180308071159.9861-1-chen_han_xiao@126.com> X-CM-TRANSID: DcmowACni0Fq4qBaI2dGBA--.64188S3 X-Coremail-Antispam: 1Uf129KBjvJXoWxCr45Zw1xWFyDKr4UAw4fXwb_yoWrJr45pa 4UK3Zayr4rJr1xuFZIyryUGFy3ZFnYgr13WrW2kwn7Aw4xAFyUXr1avr15uFW5Ar95tFya yFn09FW0kr45JaUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jjWlkUUUUU= X-Originating-IP: [58.213.111.46] X-CM-SenderInfo: xfkh0spkdqs5xldrqiyswou0bp/1tbiaQMDrlpD4VeODgAAso X-Greylist: Sender passed SPF test, ACL 227 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Thu, 08 Mar 2018 07:13:10 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Thu, 08 Mar 2018 07:13:10 +0000 (UTC) for IP:'220.181.15.113' DOMAIN:'m15-113.126.com' HELO:'m15-113.126.com' FROM:'chen_han_xiao@126.com' RCPT:'' X-RedHat-Spam-Score: -0.11 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, SPF_PASS, T_RP_MATCHES_RCVD) 220.181.15.113 m15-113.126.com 220.181.15.113 m15-113.126.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.26 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Cc: Chen Hanxiao Subject: [libvirt] [PATCH rebase v4 1/5] util: introduce virNetlinkGetNeighbor to get neighbor table entry 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.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Thu, 08 Mar 2018 07:15:40 +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 use RTM_GETNEIGH to query arp table entry by netlink socket Signed-off-by: Chen Hanxiao --- v4: use netlink to get arp table entry src/libvirt_private.syms | 1 + src/util/virnetlink.c | 82 ++++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virnetlink.h | 2 ++ 3 files changed, 85 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 3766e20d3..11b9f4937 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2386,6 +2386,7 @@ virNetlinkEventServiceStart; virNetlinkEventServiceStop; virNetlinkEventServiceStopAll; virNetlinkGetErrorCode; +virNetlinkGetNeighbor; virNetlinkShutdown; virNetlinkStartup; =20 diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c index e61bbb96d..f0a92db23 100644 --- a/src/util/virnetlink.c +++ b/src/util/virnetlink.c @@ -590,6 +590,88 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLin= kFallback fallback) goto cleanup; } =20 +/** + * virNetlinkGetNeighbor: + * + * @nlData: Gets a pointer to the raw data from netlink. + MUST BE FREED BY CALLER! + * @src_pid: pid used for nl_pid of the local end of the netlink message + * (0 =3D=3D "use getpid()") + * @dst_pid: pid of destination nl_pid if the kernel + * is not the target of the netlink message but it is to be + * sent to another process (0 if sending to the kernel) + * + * Get neighbor table entry from netlink. + * + * Returns 0 on success, -1 on fatal error. + */ +int +virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid) +{ + int rc =3D -1; + struct nlmsghdr *resp =3D NULL; + struct nlmsgerr *err; + struct ndmsg ndinfo =3D { + .ndm_family =3D AF_UNSPEC, + }; + unsigned int recvbuflen; + struct nl_msg *nl_msg; + + nl_msg =3D nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_DUMP | NLM_F_REQUEST= ); + if (!nl_msg) { + virReportOOMError(); + return -1; + } + + if (nlmsg_append(nl_msg, &ndinfo, sizeof(ndinfo), NLMSG_ALIGNTO) < 0) + goto buffer_too_small; + + + if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, + src_pid, dst_pid, NETLINK_ROUTE, 0) < 0) + goto cleanup; + + if (recvbuflen < NLMSG_LENGTH(0) || resp =3D=3D NULL) + goto malformed_resp; + + switch (resp->nlmsg_type) { + case NLMSG_ERROR: + err =3D (struct nlmsgerr *)NLMSG_DATA(resp); + if (resp->nlmsg_len < NLMSG_LENGTH(sizeof(*err))) + goto malformed_resp; + + if (err->error) { + virReportSystemError(-err->error, + "%s", _("error dumping")); + goto cleanup; + } + break; + + case RTM_NEWNEIGH: + break; + + default: + goto malformed_resp; + } + rc =3D recvbuflen; + + cleanup: + nlmsg_free(nl_msg); + if (rc < 0) + VIR_FREE(resp); + *nlData =3D resp; + return rc; + + malformed_resp: + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("malformed netlink response message")); + goto cleanup; + + buffer_too_small: + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("allocated netlink buffer is too small")); + goto cleanup; +} =20 int virNetlinkGetErrorCode(struct nlmsghdr *resp, unsigned int recvbuflen) diff --git a/src/util/virnetlink.h b/src/util/virnetlink.h index 088b01343..2a9de0a57 100644 --- a/src/util/virnetlink.h +++ b/src/util/virnetlink.h @@ -71,6 +71,8 @@ int virNetlinkDumpLink(const char *ifname, int ifindex, void **nlData, struct nlattr **tb, uint32_t src_pid, uint32_t dst_pid) ATTRIBUTE_RETURN_CHECK; +int +virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid); =20 typedef void (*virNetlinkEventHandleCallback)(struct nlmsghdr *, unsigned int length, --=20 2.14.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list