From nobody Mon Feb 9 08:28:26 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 153665426048121.90875031404869; Tue, 11 Sep 2018 01:24:20 -0700 (PDT) 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 A6BB485540; Tue, 11 Sep 2018 08:24:18 +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 0EFB55D9C8; Tue, 11 Sep 2018 08:24: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 2EFAE4BB7F; Tue, 11 Sep 2018 08:24:17 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w8B8OFZM004422 for ; Tue, 11 Sep 2018 04:24:15 -0400 Received: by smtp.corp.redhat.com (Postfix) id 6D9D117AEC; Tue, 11 Sep 2018 08:24:15 +0000 (UTC) Received: from mx1.redhat.com (ext-mx09.extmail.prod.ext.phx2.redhat.com [10.5.110.38]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 663945D9C8 for ; Tue, 11 Sep 2018 08:24:13 +0000 (UTC) Received: from m50212.mail.qiye.163.com (m50212.mail.qiye.163.com [123.125.50.212]) by mx1.redhat.com (Postfix) with ESMTP id 58FD9CAA64 for ; Tue, 11 Sep 2018 08:24:07 +0000 (UTC) Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp4 (Coremail) with SMTP id PtOowEA5TEege5dbWdamAQ--.3S4; Tue, 11 Sep 2018 16:24:05 +0800 (CST) From: Shi Lei To: libvir-list@redhat.com Date: Tue, 11 Sep 2018 16:23:57 +0800 Message-Id: <20180911082357.29264-3-shi_lei@massclouds.com> In-Reply-To: <20180911082357.29264-1-shi_lei@massclouds.com> References: <20180911082357.29264-1-shi_lei@massclouds.com> X-CM-TRANSID: PtOowEA5TEege5dbWdamAQ--.3S4 X-Coremail-Antispam: 1Uf129KBjvJXoWxXF4UCw1kKFWUAw1xtF4ktFb_yoW5tFy5pF yak3WfGw4rJ3y7XF1F9FWUWFy3ZFZIqr15Gw18WF4xKw48JF18Xr12gryvvryxCa9rJr90 k3ZI93yrCw1fXF7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pRqQ6JUUUUU= X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbifga+T1rpNOTHSAAAsl 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.38]); Tue, 11 Sep 2018 08:24:12 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Tue, 11 Sep 2018 08:24:12 +0000 (UTC) for IP:'123.125.50.212' DOMAIN:'m50212.mail.qiye.163.com' HELO:'m50212.mail.qiye.163.com' FROM:'shi_lei@massclouds.com' RCPT:'' X-RedHat-Spam-Score: -0.01 (RCVD_IN_DNSWL_NONE) 123.125.50.212 m50212.mail.qiye.163.com 123.125.50.212 m50212.mail.qiye.163.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.38 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCHv4 2/2] util: netlink: Add wrapper macros to make virNetlinkNewLink 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.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.28]); Tue, 11 Sep 2018 08:24:19 +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_[start|end] and nla_put which can make virNetlinkNewLink more readable. Signed-off-by: Shi Lei --- src/util/virnetlink.c | 35 +++++++++++------------------------ src/util/virnetlink.h | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c index 815bbee..b424b1b 100644 --- a/src/util/virnetlink.c +++ b/src/util/virnetlink.c @@ -537,40 +537,27 @@ virNetlinkNewLink(const char *ifname, if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0) goto buffer_too_small; =20 - 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; + NETLINK_MSG_PUT(nl_msg, IFLA_IFNAME, (strlen(ifname) + 1), ifname); =20 - if (type && nla_put(nl_msg, IFLA_INFO_KIND, strlen(type)+1, type) < 0) - goto buffer_too_small; + NETLINK_MSG_NEST_START(nl_msg, linkinfo, IFLA_LINKINFO); + NETLINK_MSG_PUT(nl_msg, IFLA_INFO_KIND, (strlen(type) + 1), type); =20 if ((STREQ(type, "macvtap") || STREQ(type, "macvlan")) && extra_args && extra_args->macvlan_mode && *extra_args->macvlan_mode > 0) { - if (!(infodata =3D nla_nest_start(nl_msg, IFLA_INFO_DATA))) - goto buffer_too_small; - - if (nla_put(nl_msg, IFLA_MACVLAN_MODE, sizeof(uint32_t), - extra_args->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(nl_msg, IFLA_MACVLAN_MODE, + sizeof(uint32_t), extra_args->macvlan_mode); + NETLINK_MSG_NEST_END(nl_msg, infodata); } =20 - nla_nest_end(nl_msg, linkinfo); + NETLINK_MSG_NEST_END(nl_msg, linkinfo); =20 if (extra_args) { - if (extra_args->ifindex && - nla_put(nl_msg, IFLA_LINK, sizeof(uint32_t), - extra_args->ifindex) < 0) - goto buffer_too_small; - - if (extra_args->mac && - nla_put(nl_msg, IFLA_ADDRESS, VIR_MAC_BUFLEN, extra_args->mac)= < 0) - goto buffer_too_small; + NETLINK_MSG_PUT(nl_msg, IFLA_LINK, sizeof(uint32_t), + extra_args->ifindex); + NETLINK_MSG_PUT(nl_msg, IFLA_ADDRESS, VIR_MAC_BUFLEN, extra_args->= mac); } =20 if (virNetlinkCommand(nl_msg, &resp, &buflen, 0, 0, NETLINK_ROUTE, 0) = < 0) diff --git a/src/util/virnetlink.h b/src/util/virnetlink.h index 09bab08..fd140f0 100644 --- a/src/util/virnetlink.h +++ b/src/util/virnetlink.h @@ -48,6 +48,29 @@ struct nlmsghdr; =20 # endif /* __linux__ */ =20 +# define NETLINK_MSG_NEST_START(msg, container, attrtype) \ +do { \ + container =3D nla_nest_start(msg, attrtype); \ + if (!container) \ + goto buffer_too_small; \ +} while(0) + +# define NETLINK_MSG_NEST_END(msg, container) \ +do { nla_nest_end(msg, container); } while(0) + +/* + * we need to use an intermediary pointer to @data as compilers may someti= mes + * complain about @data not being a pointer type: + * error: the address of 'foo' will always evaluate as 'true' [-Werror=3Da= ddress] + */ +# define NETLINK_MSG_PUT(msg, attrtype, datalen, data) \ +do { \ + const void *dataptr =3D data; \ + if (dataptr && nla_put(msg, attrtype, datalen, dataptr) < 0) \ + goto buffer_too_small; \ +} 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