From nobody Wed Nov 27 20:43:31 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 153630466442891.47017734775011; Fri, 7 Sep 2018 00:17:44 -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 95B0EC05D3E1; Fri, 7 Sep 2018 07:17:41 +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 DB1C2307CD90; Fri, 7 Sep 2018 07:17:40 +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 7FF0D4BB75; Fri, 7 Sep 2018 07:17: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 w877HbJ1012662 for ; Fri, 7 Sep 2018 03:17:37 -0400 Received: by smtp.corp.redhat.com (Postfix) id 692E75B684; Fri, 7 Sep 2018 07:17:37 +0000 (UTC) Received: from mx1.redhat.com (ext-mx04.extmail.prod.ext.phx2.redhat.com [10.5.110.28]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5F0295B683 for ; Fri, 7 Sep 2018 07:17:35 +0000 (UTC) Received: from m97138.mail.qiye.163.com (m97138.mail.qiye.163.com [220.181.97.138]) by mx1.redhat.com (Postfix) with ESMTP id 279238830B for ; Fri, 7 Sep 2018 07:17:32 +0000 (UTC) Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp9 (Coremail) with SMTP id u+CowACnjYYJJpJbS7CRAQ--.3853S3; Fri, 07 Sep 2018 15:17:30 +0800 (CST) From: Shi Lei To: libvir-list@redhat.com Date: Fri, 7 Sep 2018 15:17:24 +0800 Message-Id: <20180907071726.23380-2-shi_lei@massclouds.com> In-Reply-To: <20180907071726.23380-1-shi_lei@massclouds.com> References: <20180907071726.23380-1-shi_lei@massclouds.com> X-CM-TRANSID: u+CowACnjYYJJpJbS7CRAQ--.3853S3 X-Coremail-Antispam: 1Uf129KBjvJXoWxXw1fCr13Ar4Uuw43ZrWDurg_yoW7Jr43pF nFkas5Jw48Jw1xZF1IyFWUWFySvrn5WF45K3yS93s2ya1xJFy8Xw13Zr1FvFW3CrZ8Jrya k3Z09ryrCr45XaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pRhiSLUUUUU= X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbieQq6T1qrhJl3MQAAss 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.28]); Fri, 07 Sep 2018 07:17:34 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Fri, 07 Sep 2018 07:17:34 +0000 (UTC) for IP:'220.181.97.138' DOMAIN:'m97138.mail.qiye.163.com' HELO:'m97138.mail.qiye.163.com' FROM:'shi_lei@massclouds.com' RCPT:'' X-RedHat-Spam-Score: -0.01 (RCVD_IN_DNSWL_NONE) 220.181.97.138 m97138.mail.qiye.163.com 220.181.97.138 m97138.mail.qiye.163.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.28 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCHv3 1/3] util: netlink: Introduce virNetlinkNewLink helper 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.32]); Fri, 07 Sep 2018 07:17:42 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This patch introduces virNetlinkNewLink helper which wraps the common libnl/netlink code to create a new link. Signed-off-by: Shi Lei --- src/libvirt_private.syms | 1 + src/util/virnetlink.c | 117 +++++++++++++++++++++++++++++++++++++++ src/util/virnetlink.h | 13 +++++ 3 files changed, 131 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 0fc5314..a0d229a 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2446,6 +2446,7 @@ virNetlinkEventServiceStop; virNetlinkEventServiceStopAll; virNetlinkGetErrorCode; virNetlinkGetNeighbor; +virNetlinkNewLink; virNetlinkShutdown; virNetlinkStartup; =20 diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c index 8f06446..d53cc73 100644 --- a/src/util/virnetlink.c +++ b/src/util/virnetlink.c @@ -488,6 +488,123 @@ virNetlinkDumpLink(const char *ifname, int ifindex, } =20 =20 +/** + * virNetlinkNewLink: + * + * @ifname: name of the link + * @type: the type of the device, i.e. "bridge", "macvtap", "macvlan" + * @extra_args: the extra args for creating the netlink interface + * @error: netlink error code + * + * A generic wrapper to create a network link. + * + * Returns 0 on success, < 0 on fatal error. + */ +int +virNetlinkNewLink(const char *ifname, + const char *type, + virNetlinkNewLinkDataPtr extra_args, + 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 (!ifname || !type) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("neither of name and type can be NULL")); + return -EINVAL; + } + + nl_msg =3D nlmsg_alloc_simple(RTM_NEWLINK, + NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL); + if (!nl_msg) { + virReportOOMError(); + return -ENOMEM; + } + + if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 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")) && + 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_u32(nl_msg, IFLA_MACVLAN_MODE, *extra_args->macvlan_mo= de) < 0) + goto buffer_too_small; + + nla_nest_end(nl_msg, infodata); + } + + nla_nest_end(nl_msg, linkinfo); + + if (extra_args) { + if (extra_args->ifindex && + nla_put_u32(nl_msg, IFLA_LINK, *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; + } + + 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 -EBADMSG; + + buffer_too_small: + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("allocated netlink buffer is too small")); + return -ENOMEM; +} + + /** * virNetlinkDelLink: * diff --git a/src/util/virnetlink.h b/src/util/virnetlink.h index 1e1e616..09bab08 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 int *ifindex; /* The index for the 'link' device */ + 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, + 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 Wed Nov 27 20:43:31 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 1536304681323323.71352457038086; Fri, 7 Sep 2018 00:18:01 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CF236308FB9D; Fri, 7 Sep 2018 07:17:59 +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 9767D5C6DC; Fri, 7 Sep 2018 07:17:59 +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 4BFB6181A12E; Fri, 7 Sep 2018 07:17:59 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w877HbhH012667 for ; Fri, 7 Sep 2018 03:17:37 -0400 Received: by smtp.corp.redhat.com (Postfix) id D47F81C95D; Fri, 7 Sep 2018 07:17:37 +0000 (UTC) Received: from mx1.redhat.com (ext-mx19.extmail.prod.ext.phx2.redhat.com [10.5.110.48]) by smtp.corp.redhat.com (Postfix) with ESMTPS id CCD9C17F7A for ; Fri, 7 Sep 2018 07:17:34 +0000 (UTC) Received: from m97138.mail.qiye.163.com (m97138.mail.qiye.163.com [220.181.97.138]) by mx1.redhat.com (Postfix) with ESMTP id 26AA2307D85E for ; Fri, 7 Sep 2018 07:17:32 +0000 (UTC) Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp9 (Coremail) with SMTP id u+CowACnjYYJJpJbS7CRAQ--.3853S4; Fri, 07 Sep 2018 15:17:30 +0800 (CST) From: Shi Lei To: libvir-list@redhat.com Date: Fri, 7 Sep 2018 15:17:25 +0800 Message-Id: <20180907071726.23380-3-shi_lei@massclouds.com> In-Reply-To: <20180907071726.23380-1-shi_lei@massclouds.com> References: <20180907071726.23380-1-shi_lei@massclouds.com> X-CM-TRANSID: u+CowACnjYYJJpJbS7CRAQ--.3853S4 X-Coremail-Antispam: 1Uf129KBjvJXoWxXF4UCw47WF48AryfZr17Awb_yoWrAF43pF yaka4S9F48J3y7XFyF9a1UWFyfZFWFqr15G34xCF4kCa18Aw10qr129r1vqryxCa9rJrWY y3Wa9rWruw1xXaUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pRhiSLUUUUU= X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiEQq6T1nxsFRspwAAsp 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.48]); Fri, 07 Sep 2018 07:17:33 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.48]); Fri, 07 Sep 2018 07:17:33 +0000 (UTC) for IP:'220.181.97.138' DOMAIN:'m97138.mail.qiye.163.com' HELO:'m97138.mail.qiye.163.com' FROM:'shi_lei@massclouds.com' RCPT:'' X-RedHat-Spam-Score: -0.01 (RCVD_IN_DNSWL_NONE) 220.181.97.138 m97138.mail.qiye.163.com 220.181.97.138 m97138.mail.qiye.163.com X-Scanned-By: MIMEDefang 2.84 on 10.5.110.48 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCHv3 2/3] 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.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.43]); Fri, 07 Sep 2018 07:18:00 +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 | 43 +++++++++++++++---------------------------- src/util/virnetlink.h | 30 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c index d53cc73..99ad003 100644 --- a/src/util/virnetlink.c +++ b/src/util/virnetlink.c @@ -529,41 +529,33 @@ virNetlinkNewLink(const char *ifname, return -ENOMEM; } =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", + _("out of memory: nlmsg_append")); + return -ENOMEM; + } =20 - if (ifname && nla_put_string(nl_msg, IFLA_IFNAME, ifname) < 0) - goto buffer_too_small; + NETLINK_MSG_PUT(nl_msg, IFLA_IFNAME, (strlen(ifname)+1), ifname); =20 - if (!(linkinfo =3D nla_nest_start(nl_msg, IFLA_LINKINFO))) - goto buffer_too_small; + NETLINK_MSG_NEST_START(nl_msg, linkinfo, IFLA_LINKINFO); =20 - if (type && nla_put_string(nl_msg, IFLA_INFO_KIND, type) < 0) - goto buffer_too_small; + 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_u32(nl_msg, IFLA_MACVLAN_MODE, *extra_args->macvlan_mo= de) < 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_u32(nl_msg, IFLA_LINK, *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->i= findex); + 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) @@ -597,11 +589,6 @@ virNetlinkNewLink(const char *ifname, virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("malformed netlink response message")); return -EBADMSG; - - buffer_too_small: - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("allocated netlink buffer is too small")); - return -ENOMEM; } =20 =20 diff --git a/src/util/virnetlink.h b/src/util/virnetlink.h index 09bab08..6f59d6e 100644 --- a/src/util/virnetlink.h +++ b/src/util/virnetlink.h @@ -48,6 +48,36 @@ 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", \ + _("out of memory: nla_nest_start [" #attrtype "]"))= ; \ + return -ENOMEM; \ + } \ +} while(0) + +# define NETLINK_MSG_NEST_END(msg, container) \ +do { nla_nest_end(msg, container); } while(0) + +/* + * @data may be like &foo sometimes, therefore making compilers complain + * about the check below, so let's use an intermediary pointer type + * "the address of [...] will always evaluate as 'true' [-Werror=3Daddress= ]" + */ +# define NETLINK_MSG_PUT(msg, attrtype, datalen, data) \ +do { \ + const void *dataptr =3D data; \ + if (dataptr && nla_put(msg, attrtype, datalen, dataptr) < 0) { \ + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", \ + _("out of memory: nla_put [" #attrtype "]")); \ + return -ENOMEM; \ + } \ +} 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 Wed Nov 27 20:43:31 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 1536304686705536.9363954314338; Fri, 7 Sep 2018 00:18:06 -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 B77FCA8F2; Fri, 7 Sep 2018 07:18:04 +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 7D9E417F7A; Fri, 7 Sep 2018 07:18:04 +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 2C4A54BB75; Fri, 7 Sep 2018 07:18:04 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w877HcBs012675 for ; Fri, 7 Sep 2018 03:17:38 -0400 Received: by smtp.corp.redhat.com (Postfix) id 050DA608F6; Fri, 7 Sep 2018 07:17:38 +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 F02E567641 for ; Fri, 7 Sep 2018 07:17:34 +0000 (UTC) Received: from m97138.mail.qiye.163.com (m97138.mail.qiye.163.com [220.181.97.138]) by mx1.redhat.com (Postfix) with ESMTP id 2930232B69C for ; Fri, 7 Sep 2018 07:17:32 +0000 (UTC) Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp9 (Coremail) with SMTP id u+CowACnjYYJJpJbS7CRAQ--.3853S5; Fri, 07 Sep 2018 15:17:30 +0800 (CST) From: Shi Lei To: libvir-list@redhat.com Date: Fri, 7 Sep 2018 15:17:26 +0800 Message-Id: <20180907071726.23380-4-shi_lei@massclouds.com> In-Reply-To: <20180907071726.23380-1-shi_lei@massclouds.com> References: <20180907071726.23380-1-shi_lei@massclouds.com> X-CM-TRANSID: u+CowACnjYYJJpJbS7CRAQ--.3853S5 X-Coremail-Antispam: 1Uf129KBjvJXoWxKFyfAr13Ww4DJryUtr15CFg_yoWxJFWUpF yakasrXF95Jw17uF4v9a4UGFy3ZrnFgr45G3yUGw4xt3WrCF1rXw129r1SvayfAF93JryY kF1Y9Fy8uw4ruaUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pRhiSLUUUUU= X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbieAq6T1qrhGhmhQAAs5 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]); Fri, 07 Sep 2018 07:17:33 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Fri, 07 Sep 2018 07:17:33 +0000 (UTC) for IP:'220.181.97.138' DOMAIN:'m97138.mail.qiye.163.com' HELO:'m97138.mail.qiye.163.com' FROM:'shi_lei@massclouds.com' RCPT:'' X-RedHat-Spam-Score: -0.01 (RCVD_IN_DNSWL_NONE) 220.181.97.138 m97138.mail.qiye.163.com 220.181.97.138 m97138.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.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCHv3 3/3] util: netlink: 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.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Fri, 07 Sep 2018 07:18:05 +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 Reviewed-by: Erik Skultety --- src/util/virnetdevbridge.c | 73 +++--------------------- src/util/virnetdevmacvlan.c | 110 +++++------------------------------- 2 files changed, 24 insertions(+), 159 deletions(-) diff --git a/src/util/virnetdevbridge.c b/src/util/virnetdevbridge.c index bc377b5..ed2db27 100644 --- a/src/util/virnetdevbridge.c +++ b/src/util/virnetdevbridge.c @@ -416,78 +416,23 @@ 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; - - 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; + int error =3D 0; =20 - if (err->error < 0) { + if (virNetlinkNewLink(brname, "bridge", NULL, &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); - } + if (error =3D=3D -EOPNOTSUPP) { + /* fallback to ioctl if netlink doesn't support creating bridg= es */ + return virNetDevBridgeCreateWithIoctl(brname); + } # endif - - virReportSystemError(-err->error, - _("error creating bridge interface %s"), + if (error < 0) + virReportSystemError(-error, _("error creating bridge interfac= e %s"), brname); - return -1; - } - break; =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..a66ab59 100644 --- a/src/util/virnetdevmacvlan.c +++ b/src/util/virnetdevmacvlan.c @@ -307,113 +307,33 @@ 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); - } =20 - nla_nest_end(nl_msg, linkinfo); - - 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: + data.ifindex =3D &ifindex; + if (virNetlinkNewLink(ifname, type, &data, &error) < 0) { + char macstr[VIR_MAC_STRING_BUFLEN]; + if (error =3D=3D -EEXIST) *retry =3D 1; - goto cleanup; - - default: - virReportSystemError(-err->error, + else if (error < 0) + 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