From nobody Fri May 3 17:53:55 2024 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; 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 153613661915048.769846974963; Wed, 5 Sep 2018 01:36:59 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BA0CE811BF; Wed, 5 Sep 2018 08:36:56 +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 7DBDD16EF1; Wed, 5 Sep 2018 08:36:56 +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 2E7E3181A130; Wed, 5 Sep 2018 08:36:56 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w858aiqB028449 for ; Wed, 5 Sep 2018 04:36:44 -0400 Received: by smtp.corp.redhat.com (Postfix) id 40DD4C13B3; Wed, 5 Sep 2018 08:36:44 +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 3869D74BA1 for ; Wed, 5 Sep 2018 08:36:40 +0000 (UTC) Received: from m97134.mail.qiye.163.com (m97134.mail.qiye.163.com [220.181.97.134]) by mx1.redhat.com (Postfix) with ESMTP id 412345F732 for ; Wed, 5 Sep 2018 08:36:36 +0000 (UTC) Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp5 (Coremail) with SMTP id huCowABnV2uQlY9bWTyvAQ--.779S3; Wed, 05 Sep 2018 16:36:32 +0800 (CST) From: Shi Lei To: libvir-list@redhat.com Date: Wed, 5 Sep 2018 16:36:27 +0800 Message-Id: <20180905083630.32034-2-shi_lei@massclouds.com> In-Reply-To: <20180905083630.32034-1-shi_lei@massclouds.com> References: <20180905083630.32034-1-shi_lei@massclouds.com> X-CM-TRANSID: huCowABnV2uQlY9bWTyvAQ--.779S3 X-Coremail-Antispam: 1Uf129KBjvJXoW3Jr4kXry8JFyDZw13Ar1UJrb_yoW7ZFWrpF 1qka4kJw18Jw1fZF12yFW8WFySvFnrXr45K3y293Z2ya1xJFyUXw4agr1FvayfCrZxJryY yanIkryrCr45XaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pRrwIgUUUUU= X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbifxC4T1rpNMFsdAAAsG X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 212 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 05 Sep 2018 08:36:38 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 05 Sep 2018 08:36:38 +0000 (UTC) for IP:'220.181.97.134' DOMAIN:'m97134.mail.qiye.163.com' HELO:'m97134.mail.qiye.163.com' FROM:'shi_lei@massclouds.com' RCPT:'' X-RedHat-Spam-Score: -0.01 (RCVD_IN_DNSWL_NONE) 220.181.97.134 m97134.mail.qiye.163.com 220.181.97.134 m97134.mail.qiye.163.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.39 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.27 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCHv2 1/4] Introduce virNetlinkNewLink 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.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Wed, 05 Sep 2018 08:36:57 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This patch introduces virNetlinkNewLink which wraps newlink code using libnl. Signed-off-by: Shi Lei --- src/libvirt_private.syms | 1 + src/util/virnetlink.c | 120 +++++++++++++++++++++++++++++++++++++-- src/util/virnetlink.h | 13 +++++ 3 files changed, 129 insertions(+), 5 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index e2340d2..77c9b9e 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2447,6 +2447,7 @@ virNetlinkEventServiceStop; virNetlinkEventServiceStopAll; virNetlinkGetErrorCode; virNetlinkGetNeighbor; +virNetlinkNewLink; virNetlinkShutdown; virNetlinkStartup; =20 diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c index 8f06446..32aa62d 100644 --- a/src/util/virnetlink.c +++ b/src/util/virnetlink.c @@ -420,10 +420,8 @@ virNetlinkDumpLink(const char *ifname, int ifindex, if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0) goto buffer_too_small; =20 - if (ifname) { - if (nla_put(nl_msg, IFLA_IFNAME, strlen(ifname)+1, ifname) < 0) - goto buffer_too_small; - } + if (ifname && nla_put_string(nl_msg, IFLA_IFNAME, ifname) < 0) + goto buffer_too_small; =20 # ifdef RTEXT_FILTER_VF /* if this filter exists in the kernel's netlink implementation, @@ -488,6 +486,118 @@ virNetlinkDumpLink(const char *ifname, int ifindex, } =20 =20 +/** + * virNetlinkNewLink: + * + * @ifname: Name of the link + * @type: The type of device, i.e., "bridge", "macvtap", "macvlan" + * @ifindex: The index for the 'link' device + * @data: The extra args for creating the netlink interface + * @error: for retrieving error code + * + * Create a network "link" (aka interface aka device) with the given + * args. This works for many different types of network devices, + * including macvtap and bridges. + * + * Returns 0 on success, -1 on fatal error. + */ +int +virNetlinkNewLink(const char *ifname, + const char *type, + const int *ifindex, + virNetlinkNewLinkDataPtr data, + int *error) +{ + struct nlmsgerr *err; + struct nlattr *linkinfo =3D NULL; + struct nlattr *infodata =3D NULL; + unsigned int buflen; + struct ifinfomsg ifinfo =3D { .ifi_family =3D AF_UNSPEC }; + VIR_AUTOPTR(virNetlinkMsg) nl_msg =3D NULL; + VIR_AUTOFREE(struct nlmsghdr *) resp =3D NULL; + + *error =3D 0; + + if (!data) + return -1; + + nl_msg =3D nlmsg_alloc_simple(RTM_NEWLINK, + NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL); + if (!nl_msg) { + virReportOOMError(); + return -1; + } + + if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0) + goto buffer_too_small; + + if (ifindex && nla_put_u32(nl_msg, IFLA_LINK, *ifindex) < 0) + goto buffer_too_small; + + if (data->mac && nla_put(nl_msg, IFLA_ADDRESS, VIR_MAC_BUFLEN, data->m= ac) < 0) + goto buffer_too_small; + + if (ifname && nla_put_string(nl_msg, IFLA_IFNAME, ifname) < 0) + goto buffer_too_small; + + if (!(linkinfo =3D nla_nest_start(nl_msg, IFLA_LINKINFO))) + goto buffer_too_small; + + if (type && nla_put_string(nl_msg, IFLA_INFO_KIND, type) < 0) + goto buffer_too_small; + + if ((STREQ(type, "macvtap") || STREQ(type, "macvlan")) && + data->macvlan_mode && *data->macvlan_mode > 0) { + if (!(infodata =3D nla_nest_start(nl_msg, IFLA_INFO_DATA))) + goto buffer_too_small; + + if (nla_put_u32(nl_msg, IFLA_MACVLAN_MODE, *data->macvlan_mode) < = 0) + goto buffer_too_small; + + nla_nest_end(nl_msg, infodata); + } + + nla_nest_end(nl_msg, linkinfo); + + if (virNetlinkCommand(nl_msg, &resp, &buflen, 0, 0, NETLINK_ROUTE, 0) = < 0) + return -1; + + if (buflen < 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 < 0) { + *error =3D err->error; + return -1; + } + break; + + case NLMSG_DONE: + break; + + default: + goto malformed_resp; + } + + return 0; + + malformed_resp: + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("malformed netlink response message")); + return -1; + + buffer_too_small: + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("allocated netlink buffer is too small")); + return -1; +} + + /** * virNetlinkDelLink: * @@ -522,7 +632,7 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLink= Fallback fallback) if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0) goto buffer_too_small; =20 - if (nla_put(nl_msg, IFLA_IFNAME, strlen(ifname)+1, ifname) < 0) + if (ifname && nla_put_string(nl_msg, IFLA_IFNAME, ifname) < 0) goto buffer_too_small; =20 if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, 0, 0, diff --git a/src/util/virnetlink.h b/src/util/virnetlink.h index 1e1e616..8163a81 100644 --- a/src/util/virnetlink.h +++ b/src/util/virnetlink.h @@ -65,6 +65,19 @@ int virNetlinkDumpCommand(struct nl_msg *nl_msg, unsigned int protocol, unsigned int groups, void *opaque); =20 +typedef struct _virNetlinkNewLinkData virNetlinkNewLinkData; +typedef virNetlinkNewLinkData *virNetlinkNewLinkDataPtr; +struct _virNetlinkNewLinkData { + const virMacAddr *mac; /* The MAC address of the device */ + const uint32_t *macvlan_mode; /* The mode of macvlan */ +}; + +int virNetlinkNewLink(const char *ifname, + const char *type, + const int *ifindex, + virNetlinkNewLinkDataPtr data, + int *error); + typedef int (*virNetlinkDelLinkFallback)(const char *ifname); =20 int virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallba= ck); --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 17:53:55 2024 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; 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 1536136609645566.4983107031878; Wed, 5 Sep 2018 01:36:49 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6D6ED8830D; Wed, 5 Sep 2018 08:36:47 +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 6ECDE2015880; Wed, 5 Sep 2018 08:36:46 +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 4B35C18005DF; Wed, 5 Sep 2018 08:36:45 +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 w858ahSi028444 for ; Wed, 5 Sep 2018 04:36:43 -0400 Received: by smtp.corp.redhat.com (Postfix) id A8E48600C4; Wed, 5 Sep 2018 08:36:43 +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 A0A45600C1 for ; Wed, 5 Sep 2018 08:36:40 +0000 (UTC) Received: from m97134.mail.qiye.163.com (m97134.mail.qiye.163.com [220.181.97.134]) by mx1.redhat.com (Postfix) with ESMTP id 4322586668 for ; Wed, 5 Sep 2018 08:36:37 +0000 (UTC) Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp5 (Coremail) with SMTP id huCowABnV2uQlY9bWTyvAQ--.779S4; Wed, 05 Sep 2018 16:36:32 +0800 (CST) From: Shi Lei To: libvir-list@redhat.com Date: Wed, 5 Sep 2018 16:36:28 +0800 Message-Id: <20180905083630.32034-3-shi_lei@massclouds.com> In-Reply-To: <20180905083630.32034-1-shi_lei@massclouds.com> References: <20180905083630.32034-1-shi_lei@massclouds.com> X-CM-TRANSID: huCowABnV2uQlY9bWTyvAQ--.779S4 X-Coremail-Antispam: 1Uf129KBjvAXoWfGr18Wr45uw1UWr1kWry7trb_yoW8Wry8Go ZxZa1ayrWrKr1DAry3Ga97Ja9xZFySv34UXrs5Gr4IkanrGryUGry8Aa9F9Fy3KF1Fkr1F qFnFvayfWFWUur97n29KB7ZKAUJUUUU8529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjTRW1v3DUUUU X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbieBG4T1qrhFCrXgAAsO X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 212 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Wed, 05 Sep 2018 08:36:39 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Wed, 05 Sep 2018 08:36:39 +0000 (UTC) for IP:'220.181.97.134' DOMAIN:'m97134.mail.qiye.163.com' HELO:'m97134.mail.qiye.163.com' FROM:'shi_lei@massclouds.com' RCPT:'' X-RedHat-Spam-Score: -0.01 (RCVD_IN_DNSWL_NONE) 220.181.97.134 m97134.mail.qiye.163.com 220.181.97.134 m97134.mail.qiye.163.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.26 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCHv2 2/4] Add wrapper macros around nla_nest*/nla_put* to make code more readable 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.84 on 10.5.11.25 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 05 Sep 2018 08:36:48 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This patch adds wrapper macros around nla_nest*/nla_put* which apply to virNetlinkNewLink and virNetDevSetVfConfig and virNetDevVPortProfileOpSetLi= nk. Signed-off-by: Shi Lei --- src/util/virnetdev.c | 54 ++++++-------- src/util/virnetdevvportprofile.c | 117 ++++++++++--------------------- src/util/virnetlink.c | 74 +++++++------------ src/util/virnetlink.h | 52 ++++++++++++++ 4 files changed, 134 insertions(+), 163 deletions(-) diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c index 8eac419..892a147 100644 --- a/src/util/virnetdev.c +++ b/src/util/virnetdev.c @@ -1657,11 +1657,11 @@ virNetDevSetVfConfig(const char *ifname, int vf, { int rc =3D -1; char macstr[VIR_MAC_STRING_BUFLEN]; - struct nlmsghdr *resp =3D NULL; struct nlmsgerr *err; unsigned int recvbuflen =3D 0; - struct nl_msg *nl_msg; struct nlattr *vfinfolist, *vfinfo; + VIR_AUTOPTR(virNetlinkMsg) nl_msg =3D NULL; + VIR_AUTOFREE(struct nlmsghdr *) resp =3D NULL; struct ifinfomsg ifinfo =3D { .ifi_family =3D AF_UNSPEC, .ifi_index =3D -1, @@ -1676,47 +1676,41 @@ virNetDevSetVfConfig(const char *ifname, int vf, return rc; } =20 - if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0) - goto buffer_too_small; - - if (ifname && - nla_put(nl_msg, IFLA_IFNAME, strlen(ifname)+1, ifname) < 0) - goto buffer_too_small; - + if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0)= { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nlmsg_append error= ")); + return rc; + } =20 - if (!(vfinfolist =3D nla_nest_start(nl_msg, IFLA_VFINFO_LIST))) - goto buffer_too_small; + NETLINK_MSG_PUT_STRING(nl_msg, IFLA_IFNAME, ifname); =20 - if (!(vfinfo =3D nla_nest_start(nl_msg, IFLA_VF_INFO))) - goto buffer_too_small; + NETLINK_MSG_NEST_START(nl_msg, vfinfolist, IFLA_VFINFO_LIST); + NETLINK_MSG_NEST_START(nl_msg, vfinfo, IFLA_VF_INFO); =20 if (macaddr) { struct ifla_vf_mac ifla_vf_mac =3D { - .vf =3D vf, - .mac =3D { 0, }, + .vf =3D vf, + .mac =3D { 0, }, }; =20 virMacAddrGetRaw(macaddr, ifla_vf_mac.mac); =20 - if (nla_put(nl_msg, IFLA_VF_MAC, sizeof(ifla_vf_mac), - &ifla_vf_mac) < 0) - goto buffer_too_small; + NETLINK_MSG_PUT(nl_msg, IFLA_VF_MAC, + sizeof(ifla_vf_mac), &ifla_vf_mac); } =20 if (vlanid >=3D 0) { struct ifla_vf_vlan ifla_vf_vlan =3D { - .vf =3D vf, - .vlan =3D vlanid, - .qos =3D 0, + .vf =3D vf, + .vlan =3D vlanid, + .qos =3D 0, }; =20 - if (nla_put(nl_msg, IFLA_VF_VLAN, sizeof(ifla_vf_vlan), - &ifla_vf_vlan) < 0) - goto buffer_too_small; + NETLINK_MSG_PUT(nl_msg, IFLA_VF_VLAN, + sizeof(ifla_vf_vlan), &ifla_vf_vlan); } =20 - nla_nest_end(nl_msg, vfinfo); - nla_nest_end(nl_msg, vfinfolist); + NETLINK_MSG_NEST_END(nl_msg, vfinfo); + NETLINK_MSG_NEST_END(nl_msg, vfinfolist); =20 if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, 0, 0, NETLINK_ROUTE, 0) < 0) @@ -1767,20 +1761,12 @@ virNetDevSetVfConfig(const char *ifname, int vf, ifname, vf, macaddr ? virMacAddrFormat(macaddr, macstr) : "(unchanged)", vlanid, rc < 0 ? "Fail" : "Success"); - - nlmsg_free(nl_msg); - VIR_FREE(resp); return rc; =20 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 static int diff --git a/src/util/virnetdevvportprofile.c b/src/util/virnetdevvportprof= ile.c index 3ebf757..8574571 100644 --- a/src/util/virnetdevvportprofile.c +++ b/src/util/virnetdevvportprofile.c @@ -641,8 +641,6 @@ virNetDevVPortProfileOpSetLink(const char *ifname, int = ifindex, int32_t vf, uint8_t op) { - int rc =3D -1; - struct nlmsghdr *resp =3D NULL; struct nlmsgerr *err; struct ifinfomsg ifinfo =3D { .ifi_family =3D AF_UNSPEC, @@ -651,12 +649,14 @@ virNetDevVPortProfileOpSetLink(const char *ifname, in= t ifindex, unsigned int recvbuflen =3D 0; int src_pid =3D 0; uint32_t dst_pid =3D 0; - struct nl_msg *nl_msg; struct nlattr *vfports =3D NULL, *vfport; char macStr[VIR_MAC_STRING_BUFLEN]; char hostUUIDStr[VIR_UUID_STRING_BUFLEN]; char instanceUUIDStr[VIR_UUID_STRING_BUFLEN]; const char *opName; + int vfport_type =3D IFLA_PORT_SELF; + VIR_AUTOPTR(virNetlinkMsg) nl_msg =3D NULL; + VIR_AUTOFREE(struct nlmsghdr *) resp =3D NULL; =20 switch (op) { case PORT_REQUEST_PREASSOCIATE: @@ -691,24 +691,21 @@ virNetDevVPortProfileOpSetLink(const char *ifname, in= t ifindex, nl_msg =3D nlmsg_alloc_simple(RTM_SETLINK, NLM_F_REQUEST); if (!nl_msg) { virReportOOMError(); - return rc; + return -1; } =20 - if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0) - goto buffer_too_small; + if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0)= { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nlmsg_append error= ")); + return -1; + } =20 - if (ifname && - nla_put(nl_msg, IFLA_IFNAME, strlen(ifname)+1, ifname) < 0) - goto buffer_too_small; + NETLINK_MSG_PUT_STRING(nl_msg, IFLA_IFNAME, ifname); =20 if (macaddr || vlanid >=3D 0) { struct nlattr *vfinfolist, *vfinfo; =20 - if (!(vfinfolist =3D nla_nest_start(nl_msg, IFLA_VFINFO_LIST))) - goto buffer_too_small; - - if (!(vfinfo =3D nla_nest_start(nl_msg, IFLA_VF_INFO))) - goto buffer_too_small; + NETLINK_MSG_NEST_START(nl_msg, vfinfolist, IFLA_VFINFO_LIST); + NETLINK_MSG_NEST_START(nl_msg, vfinfo, IFLA_VF_INFO); =20 if (macaddr) { struct ifla_vf_mac ifla_vf_mac =3D { @@ -718,9 +715,8 @@ virNetDevVPortProfileOpSetLink(const char *ifname, int = ifindex, =20 virMacAddrGetRaw(macaddr, ifla_vf_mac.mac); =20 - if (nla_put(nl_msg, IFLA_VF_MAC, sizeof(ifla_vf_mac), - &ifla_vf_mac) < 0) - goto buffer_too_small; + NETLINK_MSG_PUT(nl_msg, IFLA_VF_MAC, + sizeof(ifla_vf_mac), &ifla_vf_mac); } =20 if (vlanid >=3D 0) { @@ -730,77 +726,49 @@ virNetDevVPortProfileOpSetLink(const char *ifname, in= t ifindex, .qos =3D 0, }; =20 - if (nla_put(nl_msg, IFLA_VF_VLAN, sizeof(ifla_vf_vlan), - &ifla_vf_vlan) < 0) - goto buffer_too_small; + NETLINK_MSG_PUT(nl_msg, IFLA_VF_VLAN, + sizeof(ifla_vf_vlan), &ifla_vf_vlan); } =20 - nla_nest_end(nl_msg, vfinfo); - nla_nest_end(nl_msg, vfinfolist); - } - - if (vf =3D=3D PORT_SELF_VF && nltarget_kernel) { - if (!(vfport =3D nla_nest_start(nl_msg, IFLA_PORT_SELF))) - goto buffer_too_small; - } else { - if (!(vfports =3D nla_nest_start(nl_msg, IFLA_VF_PORTS))) - goto buffer_too_small; - - /* begin nesting vfports */ - if (!(vfport =3D nla_nest_start(nl_msg, IFLA_VF_PORT))) - goto buffer_too_small; - } - - if (profileId) { - if (nla_put(nl_msg, IFLA_PORT_PROFILE, strlen(profileId) + 1, - profileId) < 0) - goto buffer_too_small; - } - - if (portVsi) { - if (nla_put(nl_msg, IFLA_PORT_VSI_TYPE, sizeof(*portVsi), - portVsi) < 0) - goto buffer_too_small; + NETLINK_MSG_NEST_END(nl_msg, vfinfo); + NETLINK_MSG_NEST_END(nl_msg, vfinfolist); } =20 - if (instanceId) { - if (nla_put(nl_msg, IFLA_PORT_INSTANCE_UUID, VIR_UUID_BUFLEN, - instanceId) < 0) - goto buffer_too_small; + if (vf !=3D PORT_SELF_VF || !nltarget_kernel) { + NETLINK_MSG_NEST_START(nl_msg, vfports, IFLA_VF_PORTS); + /* begin nesting of vfports */ + vfport_type =3D IFLA_VF_PORT; } + /* begin nesting of vfport */ + NETLINK_MSG_NEST_START(nl_msg, vfport, vfport_type); =20 - if (hostUUID) { - if (nla_put(nl_msg, IFLA_PORT_HOST_UUID, VIR_UUID_BUFLEN, - hostUUID) < 0) - goto buffer_too_small; - } + NETLINK_MSG_PUT_STRING(nl_msg, IFLA_PORT_PROFILE, profileId); + NETLINK_MSG_PUT(nl_msg, IFLA_PORT_VSI_TYPE, sizeof(*portVsi), portVsi); + NETLINK_MSG_PUT(nl_msg, IFLA_PORT_INSTANCE_UUID, VIR_UUID_BUFLEN, inst= anceId); + NETLINK_MSG_PUT(nl_msg, IFLA_PORT_HOST_UUID, VIR_UUID_BUFLEN, hostUUID= ); =20 - if (vf !=3D PORT_SELF_VF) { - if (nla_put(nl_msg, IFLA_PORT_VF, sizeof(vf), &vf) < 0) - goto buffer_too_small; - } + if (vf !=3D PORT_SELF_VF) + NETLINK_MSG_PUT(nl_msg, IFLA_PORT_VF, sizeof(vf), &vf); =20 - if (nla_put(nl_msg, IFLA_PORT_REQUEST, sizeof(op), &op) < 0) - goto buffer_too_small; + NETLINK_MSG_PUT(nl_msg, IFLA_PORT_REQUEST, sizeof(op), &op); =20 /* end nesting of vport */ - nla_nest_end(nl_msg, vfport); - + NETLINK_MSG_NEST_END(nl_msg, vfport); if (vfports) { /* end nesting of vfports */ - nla_nest_end(nl_msg, vfports); + NETLINK_MSG_NEST_END(nl_msg, vfports); } =20 if (!nltarget_kernel) { if ((src_pid =3D virNetlinkEventServiceLocalPid(NETLINK_ROUTE)) < = 0) - goto cleanup; + return -1; if ((dst_pid =3D virNetDevVPortProfileGetLldpadPid()) =3D=3D 0) - goto cleanup; + return -1; } =20 if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, src_pid, dst_pid, NETLINK_ROUTE, 0) < 0) - goto cleanup; + return -1; =20 if (recvbuflen < NLMSG_LENGTH(0) || resp =3D=3D NULL) goto malformed_resp; @@ -815,7 +783,7 @@ virNetDevVPortProfileOpSetLink(const char *ifname, int = ifindex, virReportSystemError(-err->error, _("error during virtual port configuration of ifindex %d"), ifindex); - goto cleanup; + return -1; } break; =20 @@ -826,21 +794,12 @@ virNetDevVPortProfileOpSetLink(const char *ifname, in= t ifindex, goto malformed_resp; } =20 - rc =3D 0; - cleanup: - nlmsg_free(nl_msg); - VIR_FREE(resp); - return rc; + return 0; =20 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; + return -1; } =20 =20 diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c index 32aa62d..4d19ac9 100644 --- a/src/util/virnetlink.c +++ b/src/util/virnetlink.c @@ -417,11 +417,12 @@ virNetlinkDumpLink(const char *ifname, int ifindex, return -1; } =20 - if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0) - goto buffer_too_small; + if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0)= { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nlmsg_append error= ")); + return -1; + } =20 - if (ifname && nla_put_string(nl_msg, IFLA_IFNAME, ifname) < 0) - goto buffer_too_small; + NETLINK_MSG_PUT_STRING(nl_msg, IFLA_IFNAME, ifname); =20 # ifdef RTEXT_FILTER_VF /* if this filter exists in the kernel's netlink implementation, @@ -430,11 +431,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex, */ { uint32_t ifla_ext_mask =3D RTEXT_FILTER_VF; - - if (nla_put(nl_msg, IFLA_EXT_MASK, - sizeof(ifla_ext_mask), &ifla_ext_mask) < 0) { - goto buffer_too_small; - } + NETLINK_MSG_PUT_U32PTR(nl_msg, IFLA_EXT_MASK, &ifla_ext_mask); } # endif =20 @@ -478,11 +475,6 @@ virNetlinkDumpLink(const char *ifname, int ifindex, virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("malformed netlink response message")); return rc; - - buffer_too_small: - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("allocated netlink buffer is too small")); - return rc; } =20 =20 @@ -528,36 +520,27 @@ virNetlinkNewLink(const char *ifname, return -1; } =20 - if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0) - goto buffer_too_small; - - if (ifindex && nla_put_u32(nl_msg, IFLA_LINK, *ifindex) < 0) - goto buffer_too_small; + if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0)= { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nlmsg_append error= ")); + return -1; + } =20 - if (data->mac && nla_put(nl_msg, IFLA_ADDRESS, VIR_MAC_BUFLEN, data->m= ac) < 0) - goto buffer_too_small; + NETLINK_MSG_PUT_U32PTR(nl_msg, IFLA_LINK, ifindex); + NETLINK_MSG_PUT(nl_msg, IFLA_ADDRESS, VIR_MAC_BUFLEN, data->mac); + NETLINK_MSG_PUT_STRING(nl_msg, IFLA_IFNAME, ifname); =20 - if (ifname && nla_put_string(nl_msg, IFLA_IFNAME, ifname) < 0) - goto buffer_too_small; + NETLINK_MSG_NEST_START(nl_msg, linkinfo, IFLA_LINKINFO); =20 - if (!(linkinfo =3D nla_nest_start(nl_msg, IFLA_LINKINFO))) - goto buffer_too_small; - - if (type && nla_put_string(nl_msg, IFLA_INFO_KIND, type) < 0) - goto buffer_too_small; + NETLINK_MSG_PUT_STRING(nl_msg, IFLA_INFO_KIND, type); =20 if ((STREQ(type, "macvtap") || STREQ(type, "macvlan")) && data->macvlan_mode && *data->macvlan_mode > 0) { - if (!(infodata =3D nla_nest_start(nl_msg, IFLA_INFO_DATA))) - goto buffer_too_small; - - if (nla_put_u32(nl_msg, IFLA_MACVLAN_MODE, *data->macvlan_mode) < = 0) - goto buffer_too_small; - - nla_nest_end(nl_msg, infodata); + NETLINK_MSG_NEST_START(nl_msg, infodata, IFLA_INFO_DATA); + NETLINK_MSG_PUT_U32PTR(nl_msg, IFLA_MACVLAN_MODE, data->macvlan_mo= de); + NETLINK_MSG_NEST_END(nl_msg, infodata); } =20 - nla_nest_end(nl_msg, linkinfo); + NETLINK_MSG_NEST_END(nl_msg, linkinfo); =20 if (virNetlinkCommand(nl_msg, &resp, &buflen, 0, 0, NETLINK_ROUTE, 0) = < 0) return -1; @@ -590,11 +573,6 @@ virNetlinkNewLink(const char *ifname, virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("malformed netlink response message")); return -1; - - buffer_too_small: - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("allocated netlink buffer is too small")); - return -1; } =20 =20 @@ -629,11 +607,12 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLi= nkFallback fallback) return -1; } =20 - if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0) - goto buffer_too_small; + if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0)= { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nlmsg_append error= ")); + return -1; + } =20 - if (ifname && nla_put_string(nl_msg, IFLA_IFNAME, ifname) < 0) - goto buffer_too_small; + NETLINK_MSG_PUT_STRING(nl_msg, IFLA_IFNAME, ifname); =20 if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, 0, 0, NETLINK_ROUTE, 0) < 0) { @@ -673,11 +652,6 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLin= kFallback fallback) virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("malformed netlink response message")); return -1; - - buffer_too_small: - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("allocated netlink buffer is too small")); - return -1; } =20 /** diff --git a/src/util/virnetlink.h b/src/util/virnetlink.h index 8163a81..2e64570 100644 --- a/src/util/virnetlink.h +++ b/src/util/virnetlink.h @@ -48,6 +48,58 @@ struct nlmsghdr; =20 # endif /* __linux__ */ =20 + +# define NETLINK_MSG_NEST_START(msg, container, attrtype) \ +do { \ + container =3D nla_nest_start(msg, attrtype); \ + if (!container) { \ + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", \ + _("nla_nest_start error [" #attrtype "]")); \ + return -1; \ + } \ +} while(0) + +# define NETLINK_MSG_NEST_END(msg, container) \ +do { nla_nest_end(msg, container); } while(0) + +/* + * When data is an rvalue, compiler will report error as below: + * "the address of [...] will always evaluate as 'true' [-Werror=3Daddress= ]" + * Add a dummy(as an lvalue) to solve it. + */ +# define NETLINK_MSG_PUT(msg, attrtype, datalen, data) \ +do { \ + const void *dummy =3D data; \ + if (dummy && nla_put(msg, attrtype, datalen, data) < 0) { \ + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", \ + _("nla_put error [" #attrtype "]")); \ + return -1; \ + } \ +} while(0) + +# define NETLINK_MSG_PUT_STRING(msg, attrtype, str) \ +do { \ + const void *dummy =3D str; \ + if (dummy && nla_put_string(msg, attrtype, str) < 0) { \ + virReportError(VIR_ERR_INTERNAL_ERROR, \ + _("nla_put_string error [" #attrtype "]: '%s'"), \ + str); \ + return -1; \ + } \ +} while(0) + +# define NETLINK_MSG_PUT_U32PTR(msg, attrtype, ptr) \ +do { \ + const void *dummy =3D ptr; \ + if (dummy && nla_put_u32(msg, attrtype, *ptr) < 0) { \ + virReportError(VIR_ERR_INTERNAL_ERROR, \ + _("nla_put_u32 error [" #attrtype "]: '%u'"), \ + *ptr); \ + return -1; \ + } \ +} while(0) + + int virNetlinkStartup(void); void virNetlinkShutdown(void); =20 --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 17:53:55 2024 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; 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 1536136610353579.2973200559408; Wed, 5 Sep 2018 01:36:50 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 869B3356D3; Wed, 5 Sep 2018 08:36:48 +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 11C093091375; Wed, 5 Sep 2018 08:36:48 +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 953C54BB7F; Wed, 5 Sep 2018 08:36:47 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w858aiIW028455 for ; Wed, 5 Sep 2018 04:36:44 -0400 Received: by smtp.corp.redhat.com (Postfix) id 78AE94BE; Wed, 5 Sep 2018 08:36:44 +0000 (UTC) Received: from mx1.redhat.com (ext-mx12.extmail.prod.ext.phx2.redhat.com [10.5.110.41]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 700CD5FC31 for ; Wed, 5 Sep 2018 08:36:39 +0000 (UTC) Received: from m97134.mail.qiye.163.com (m97134.mail.qiye.163.com [220.181.97.134]) by mx1.redhat.com (Postfix) with ESMTP id 31408308A95E for ; Wed, 5 Sep 2018 08:36:36 +0000 (UTC) Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp5 (Coremail) with SMTP id huCowABnV2uQlY9bWTyvAQ--.779S5; Wed, 05 Sep 2018 16:36:33 +0800 (CST) From: Shi Lei To: libvir-list@redhat.com Date: Wed, 5 Sep 2018 16:36:29 +0800 Message-Id: <20180905083630.32034-4-shi_lei@massclouds.com> In-Reply-To: <20180905083630.32034-1-shi_lei@massclouds.com> References: <20180905083630.32034-1-shi_lei@massclouds.com> X-CM-TRANSID: huCowABnV2uQlY9bWTyvAQ--.779S5 X-Coremail-Antispam: 1Uf129KBjvJXoW3Gr17tF47Aw4Uur47uFW5Awb_yoWxGr4DpF yakas7XF95Jw17uFZF9a4UGFy3ZrnFgr45G34UGa1xta1ruF1rXw129ryfZayfCF93JryY kF1a9Fy8uw4ruaUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pRrwIgUUUUU= X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiexG4T1rpNaKcIAAAsF X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 212 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Wed, 05 Sep 2018 08:36:38 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Wed, 05 Sep 2018 08:36:38 +0000 (UTC) for IP:'220.181.97.134' DOMAIN:'m97134.mail.qiye.163.com' HELO:'m97134.mail.qiye.163.com' FROM:'shi_lei@massclouds.com' RCPT:'' X-RedHat-Spam-Score: -0.01 (RCVD_IN_DNSWL_NONE) 220.181.97.134 m97134.mail.qiye.163.com 220.181.97.134 m97134.mail.qiye.163.com X-Scanned-By: MIMEDefang 2.84 on 10.5.110.41 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCHv2 3/4] Using virNetlinkNewLink to simplify virNetDev*Create 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.84 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Wed, 05 Sep 2018 08:36:49 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This patch simplifies virNetDevBridgeCreate and virNetDevMacVLanCreate by using virNetlinkNewLink. Signed-off-by: Shi Lei --- src/util/virnetdevbridge.c | 78 +++++--------------------- src/util/virnetdevmacvlan.c | 109 +++++------------------------------- 2 files changed, 27 insertions(+), 160 deletions(-) diff --git a/src/util/virnetdevbridge.c b/src/util/virnetdevbridge.c index bc377b5..beef345 100644 --- a/src/util/virnetdevbridge.c +++ b/src/util/virnetdevbridge.c @@ -416,78 +416,26 @@ int virNetDevBridgeCreate(const char *brname) { /* use a netlink RTM_NEWLINK message to create the bridge */ - const char *type =3D "bridge"; - struct nlmsgerr *err; - struct ifinfomsg ifinfo =3D { .ifi_family =3D AF_UNSPEC }; - unsigned int recvbuflen; - struct nlattr *linkinfo; - VIR_AUTOPTR(virNetlinkMsg) nl_msg =3D NULL; - VIR_AUTOFREE(struct nlmsghdr *) resp =3D NULL; + int error =3D 0; + virNetlinkNewLinkData data =3D { + .macvlan_mode =3D NULL, + .mac =3D NULL, + }; =20 - nl_msg =3D nlmsg_alloc_simple(RTM_NEWLINK, - NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL); - if (!nl_msg) { - virReportOOMError(); - return -1; - } - - if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0) - goto buffer_too_small; - if (nla_put(nl_msg, IFLA_IFNAME, strlen(brname)+1, brname) < 0) - goto buffer_too_small; - if (!(linkinfo =3D nla_nest_start(nl_msg, IFLA_LINKINFO))) - goto buffer_too_small; - if (nla_put(nl_msg, IFLA_INFO_KIND, strlen(type), type) < 0) - goto buffer_too_small; - nla_nest_end(nl_msg, linkinfo); - - if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, 0, 0, - NETLINK_ROUTE, 0) < 0) { - return -1; - } - - 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 < 0) { + if (virNetlinkNewLink(brname, "bridge", NULL, &data, &error) < 0) { # if defined(HAVE_STRUCT_IFREQ) && defined(SIOCBRADDBR) - if (err->error =3D=3D -EOPNOTSUPP) { - /* fallback to ioctl if netlink doesn't support creating - * bridges - */ - return virNetDevBridgeCreateWithIoctl(brname); - } -# endif - - virReportSystemError(-err->error, - _("error creating bridge interface %s"), - brname); - return -1; + if (error =3D=3D -EOPNOTSUPP) { + /* fallback to ioctl if netlink doesn't support creating bridg= es */ + return virNetDevBridgeCreateWithIoctl(brname); } - break; +# endif + virReportSystemError(-error, _("error creating bridge interface %s= "), + brname); =20 - case NLMSG_DONE: - break; - default: - goto malformed_resp; + return -1; } =20 return 0; - - malformed_resp: - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("malformed netlink response message")); - return -1; - buffer_too_small: - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("allocated netlink buffer is too small")); - return -1; } =20 =20 diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c index 2035b1f..1372a1a 100644 --- a/src/util/virnetdevmacvlan.c +++ b/src/util/virnetdevmacvlan.c @@ -307,113 +307,32 @@ virNetDevMacVLanCreate(const char *ifname, uint32_t macvlan_mode, int *retry) { - int rc =3D -1; - struct nlmsgerr *err; - struct ifinfomsg ifinfo =3D { .ifi_family =3D AF_UNSPEC }; - int ifindex; - unsigned int recvbuflen; - struct nl_msg *nl_msg; - struct nlattr *linkinfo, *info_data; - char macstr[VIR_MAC_STRING_BUFLEN]; - VIR_AUTOFREE(struct nlmsghdr *) resp =3D NULL; - - if (virNetDevGetIndex(srcdev, &ifindex) < 0) - return -1; + int error =3D 0; + int ifindex =3D 0; + virNetlinkNewLinkData data =3D { + .macvlan_mode =3D &macvlan_mode, + .mac =3D macaddress, + }; =20 *retry =3D 0; =20 - nl_msg =3D nlmsg_alloc_simple(RTM_NEWLINK, - NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL); - if (!nl_msg) { - virReportOOMError(); + if (virNetDevGetIndex(srcdev, &ifindex) < 0) return -1; - } - - if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0) - goto buffer_too_small; - - if (nla_put_u32(nl_msg, IFLA_LINK, ifindex) < 0) - goto buffer_too_small; - - if (nla_put(nl_msg, IFLA_ADDRESS, VIR_MAC_BUFLEN, macaddress) < 0) - goto buffer_too_small; - - if (ifname && - nla_put(nl_msg, IFLA_IFNAME, strlen(ifname)+1, ifname) < 0) - goto buffer_too_small; - - if (!(linkinfo =3D nla_nest_start(nl_msg, IFLA_LINKINFO))) - goto buffer_too_small; - - if (nla_put(nl_msg, IFLA_INFO_KIND, strlen(type), type) < 0) - goto buffer_too_small; - - if (macvlan_mode > 0) { - if (!(info_data =3D nla_nest_start(nl_msg, IFLA_INFO_DATA))) - goto buffer_too_small; - - if (nla_put(nl_msg, IFLA_MACVLAN_MODE, sizeof(macvlan_mode), - &macvlan_mode) < 0) - goto buffer_too_small; - - nla_nest_end(nl_msg, info_data); - } - - nla_nest_end(nl_msg, linkinfo); =20 - if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, 0, 0, - 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; - - switch (err->error) { - - case 0: - break; - - case -EEXIST: + if (virNetlinkNewLink(ifname, type, &ifindex, &data, &error) < 0) { + char macstr[VIR_MAC_STRING_BUFLEN]; + if (error =3D=3D -EEXIST) *retry =3D 1; - goto cleanup; - - default: - virReportSystemError(-err->error, + else + virReportSystemError(-error, _("error creating %s interface %s@%s (%s)= "), type, ifname, srcdev, virMacAddrFormat(macaddress, macstr)); - goto cleanup; - } - break; - - case NLMSG_DONE: - break; =20 - default: - goto malformed_resp; + return -1; } =20 - rc =3D 0; - cleanup: - nlmsg_free(nl_msg); - 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; + return 0; } =20 /** --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 17:53:55 2024 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; 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 1536136612122215.9098339663791; Wed, 5 Sep 2018 01:36:52 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 68B5E8666B; Wed, 5 Sep 2018 08:36:50 +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 2BF37308BDA0; Wed, 5 Sep 2018 08:36:50 +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 9D4C64A460; Wed, 5 Sep 2018 08:36:49 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w858aiRT028456 for ; Wed, 5 Sep 2018 04:36:44 -0400 Received: by smtp.corp.redhat.com (Postfix) id 7AA321719E; Wed, 5 Sep 2018 08:36:44 +0000 (UTC) Received: from mx1.redhat.com (ext-mx03.extmail.prod.ext.phx2.redhat.com [10.5.110.27]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6FEF45F726 for ; Wed, 5 Sep 2018 08:36:39 +0000 (UTC) Received: from m97134.mail.qiye.163.com (m97134.mail.qiye.163.com [220.181.97.134]) by mx1.redhat.com (Postfix) with ESMTP id 70CE780F6D for ; Wed, 5 Sep 2018 08:36:35 +0000 (UTC) Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp5 (Coremail) with SMTP id huCowABnV2uQlY9bWTyvAQ--.779S6; Wed, 05 Sep 2018 16:36:33 +0800 (CST) From: Shi Lei To: libvir-list@redhat.com Date: Wed, 5 Sep 2018 16:36:30 +0800 Message-Id: <20180905083630.32034-5-shi_lei@massclouds.com> In-Reply-To: <20180905083630.32034-1-shi_lei@massclouds.com> References: <20180905083630.32034-1-shi_lei@massclouds.com> X-CM-TRANSID: huCowABnV2uQlY9bWTyvAQ--.779S6 X-Coremail-Antispam: 1Uf129KBjvJXoWxGry5Cr4fJrW5tw47ZF1UZFb_yoW5Cr4Upa 47Ka47Wr4rJr1q9ayxZr4UCF1Fqr1fXFyfGrWUKw1xWa1UZrn2qa4xKFy8KFZ5CFZ5uFyF 9a1q9ayrJFWxCaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pRrwIgUUUUU= X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbieRG4T1qrhIG3jwAAsT X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 212 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Wed, 05 Sep 2018 08:36:38 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Wed, 05 Sep 2018 08:36:38 +0000 (UTC) for IP:'220.181.97.134' DOMAIN:'m97134.mail.qiye.163.com' HELO:'m97134.mail.qiye.163.com' FROM:'shi_lei@massclouds.com' RCPT:'' X-RedHat-Spam-Score: -0.01 (RCVD_IN_DNSWL_NONE) 220.181.97.134 m97134.mail.qiye.163.com 220.181.97.134 m97134.mail.qiye.163.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.27 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCHv2 4/4] Remove virNetDevPutExtraHeader and replace with nlmsg_append 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.84 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Wed, 05 Sep 2018 08:36:51 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This patch replaces virNetDevPutExtraHeader with nlmsg_append directly. Signed-off-by: Shi Lei --- src/util/virnetdev.c | 44 ++++++++++++-------------------------------- 1 file changed, 12 insertions(+), 32 deletions(-) diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c index 892a147..a7bc8e0 100644 --- a/src/util/virnetdev.c +++ b/src/util/virnetdev.c @@ -3155,28 +3155,6 @@ virNetDevGetEthtoolFeatures(virBitmapPtr bitmap, =20 =20 # if HAVE_DECL_DEVLINK_CMD_ESWITCH_GET -/** - * virNetDevPutExtraHeader - * reserve and prepare room for an extra header - * This function sets to zero the room that is required to put the extra - * header after the initial Netlink header. This function also increases - * the nlmsg_len field. - * - * @nlh: pointer to Netlink header - * @size: size of the extra header that we want to put - * - * Returns pointer to the start of the extended header - */ -static void * -virNetDevPutExtraHeader(struct nlmsghdr *nlh, - size_t size) -{ - char *ptr =3D (char *)nlh + nlh->nlmsg_len; - size_t len =3D NLMSG_ALIGN(size); - nlh->nlmsg_len +=3D len; - return ptr; -} - =20 /** * virNetDevGetFamilyId: @@ -3191,7 +3169,11 @@ virNetDevGetFamilyId(const char *family_name) { struct nl_msg *nl_msg =3D NULL; struct nlmsghdr *resp =3D NULL; - struct genlmsghdr* gmsgh =3D NULL; + struct genlmsghdr gmsgh =3D { + .cmd =3D CTRL_CMD_GETFAMILY, + .version =3D DEVLINK_GENL_VERSION, + .reserved =3D 0, + }; struct nlattr *tb[CTRL_ATTR_MAX + 1] =3D {NULL, }; unsigned int recvbuflen; uint32_t family_id =3D 0; @@ -3202,12 +3184,9 @@ virNetDevGetFamilyId(const char *family_name) goto cleanup; } =20 - if (!(gmsgh =3D virNetDevPutExtraHeader(nlmsg_hdr(nl_msg), sizeof(stru= ct genlmsghdr)))) + if (nlmsg_append(nl_msg, &gmsgh, sizeof(gmsgh), NLMSG_ALIGNTO) < 0) goto cleanup; =20 - gmsgh->cmd =3D CTRL_CMD_GETFAMILY; - gmsgh->version =3D DEVLINK_GENL_VERSION; - if (nla_put_string(nl_msg, CTRL_ATTR_FAMILY_NAME, family_name) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("allocated netlink buffer is too small")); @@ -3254,7 +3233,11 @@ virNetDevSwitchdevFeature(const char *ifname, unsigned int recvbuflen; struct nlattr *tb[DEVLINK_ATTR_MAX + 1] =3D {NULL, }; virPCIDevicePtr pci_device_ptr =3D NULL; - struct genlmsghdr* gmsgh =3D NULL; + struct genlmsghdr gmsgh =3D { + .cmd =3D DEVLINK_CMD_ESWITCH_GET, + .version =3D DEVLINK_GENL_VERSION, + .reserved =3D 0, + }; const char *pci_name; char *pfname =3D NULL; int is_vf =3D -1; @@ -3284,12 +3267,9 @@ virNetDevSwitchdevFeature(const char *ifname, goto cleanup; } =20 - if (!(gmsgh =3D virNetDevPutExtraHeader(nlmsg_hdr(nl_msg), sizeof(stru= ct genlmsghdr)))) + if (nlmsg_append(nl_msg, &gmsgh, sizeof(gmsgh), NLMSG_ALIGNTO) < 0) goto cleanup; =20 - gmsgh->cmd =3D DEVLINK_CMD_ESWITCH_GET; - gmsgh->version =3D DEVLINK_GENL_VERSION; - pci_name =3D virPCIDeviceGetName(pci_device_ptr); =20 if (nla_put(nl_msg, DEVLINK_ATTR_BUS_NAME, strlen("pci")+1, "pci") < 0= || --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list