From nobody Mon Feb 9 10:28:32 2026 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