From nobody Tue Apr 16 23:38:41 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1610331880; cv=none; d=zohomail.com; s=zohoarc; b=QQJ5Nf/zjksm6PdEIDvbGxOaXNOTe9wyVb4ohSjzw3OcA2w6KyAiN788FHn2ttms5M9lUQa2TzZ5zmAsoU4SrSG6bnUCCXDEmq5+oepiMpOAqRfdKU9khsCvlR+NU9k9wfYpUp/Ae7s9pcjATRb94g3ONYXeYXnB6vX2WrzFtgc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1610331880; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=8Xk15hfYufQ43xlz6GvswMokebsVfc33pPJuOBf0JJk=; b=mjHbWzXAqm4wOz4FjBDcXsWQGI6jcYEG/Oj1w9jGyBmXiq9Bcs85kT62dowPcuvbELVclqaGCN7vAzIOmXIrhDrxUm0cxJ5u11Vi53FQCvDDEQeGqmKCUgoR3RdBwYMTK5pnX/kfYNFB0oVloSwlRMiyn2WgeCMfwCbfq5laUpY= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1610331879639105.45291664394415; Sun, 10 Jan 2021 18:24:39 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-456-K-gs-4ioPJSXJFhHW84vFw-1; Sun, 10 Jan 2021 21:24:36 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 39DF8107ACE4; Mon, 11 Jan 2021 02:24:31 +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 773DD60C4D; Mon, 11 Jan 2021 02:24:29 +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 15A894A7C6; Mon, 11 Jan 2021 02:24:24 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 10B2OL7m007225 for ; Sun, 10 Jan 2021 21:24:23 -0500 Received: by smtp.corp.redhat.com (Postfix) id 7261E33228; Mon, 11 Jan 2021 02:24:21 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast02.extmail.prod.ext.rdu2.redhat.com [10.11.55.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6D2DD3324A for ; Mon, 11 Jan 2021 02:24:18 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BBF538007D9 for ; Mon, 11 Jan 2021 02:24:18 +0000 (UTC) Received: from m97136.mail.qiye.163.com (m97136.mail.qiye.163.com [220.181.97.136]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-525-V9Z1QJghMZOPYyY_cRU4iA-1; Sun, 10 Jan 2021 21:24:13 -0500 Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp2 (Coremail) with SMTP id iOCowAAnkeDCtvtfIO+DCw--.10533S3; Mon, 11 Jan 2021 10:24:07 +0800 (CST) X-MC-Unique: K-gs-4ioPJSXJFhHW84vFw-1 X-MC-Unique: V9Z1QJghMZOPYyY_cRU4iA-1 From: Shi Lei To: libvir-list@redhat.com Subject: [PATCHv3 1/4] netlink: Remove invalid flags(NLM_F_CREATE and NLM_F_EXCL) for RTM_DELLINK Date: Mon, 11 Jan 2021 10:23:34 +0800 Message-Id: <20210111022337.27473-2-shi_lei@massclouds.com> In-Reply-To: <20210111022337.27473-1-shi_lei@massclouds.com> References: <20210111022337.27473-1-shi_lei@massclouds.com> MIME-Version: 1.0 X-CM-TRANSID: iOCowAAnkeDCtvtfIO+DCw--.10533S3 X-Coremail-Antispam: 1Uf129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73 VFW2AGmfu7bjvjm3AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjfUJfsgUUUUU X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiDxIXT1nfQuDoGAABsQ X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-loop: libvir-list@redhat.com Cc: mprivozn@redhat.com, Shi Lei 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" NLM_F_CREATE and NLM_F_EXCL are invalid for RTM_DELLINK, so remove them. Signed-off-by: Shi Lei --- src/util/virnetlink.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c index ca735bb8..17e6eeb9 100644 --- a/src/util/virnetlink.c +++ b/src/util/virnetlink.c @@ -627,8 +627,7 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLink= Fallback fallback) g_autoptr(virNetlinkMsg) nl_msg =3D NULL; g_autofree struct nlmsghdr *resp =3D NULL; =20 - nl_msg =3D nlmsg_alloc_simple(RTM_DELLINK, - NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL); + nl_msg =3D nlmsg_alloc_simple(RTM_DELLINK, NLM_F_REQUEST); if (!nl_msg) { virReportOOMError(); return -1; --=20 2.25.1 From nobody Tue Apr 16 23:38:41 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1610331880; cv=none; d=zohomail.com; s=zohoarc; b=LuhLOAuopJpao5NFEv7ln55cZ8R1c4sSIXfJeHPQxDt/+fGcZFN25RF5SOSI0gF0VW60KOfeBOk+Ztq3MMiQ5YNjr6WxsRqnPo7UY/CZab/RWXv+xYHjNghS5W8RCDAuDLvrg7SCMlUyB1lEkOjDHDajGyJMbF7g+xtNxnYlO+s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1610331880; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=eFaiqBVe31PoZbB2F6fBi7u9XCguRd2mIz6Gp3bFLR0=; b=IrvXzaWrzDpp8+D3e972/ofKnHdR5N3ef/PdEGQl5FtBRdDbL9GfZ3vW3WtruYubj2ftV2UeR0lNla3bsPuLVQosQTY5H4IqR7a9cS2FGLeWMmpQkxg1QwtG727BAG0e/9VsxZQoy1q1grJ4g7758OLVME3VZqMwMhSvVX0QXWY= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1610331880733838.1391840765833; Sun, 10 Jan 2021 18:24:40 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-167-qtki3yXfOta-Q5NmGj8YAg-1; Sun, 10 Jan 2021 21:24:37 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0E8CC180A093; Mon, 11 Jan 2021 02:24:31 +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 D2B585D769; Mon, 11 Jan 2021 02:24:30 +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 5ED684BB7B; Mon, 11 Jan 2021 02:24:27 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 10B2ON2f007226 for ; Sun, 10 Jan 2021 21:24:23 -0500 Received: by smtp.corp.redhat.com (Postfix) id 1145D30BBA; Mon, 11 Jan 2021 02:24:23 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast03.extmail.prod.ext.rdu2.redhat.com [10.11.55.19]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0C20A2EF8E for ; Mon, 11 Jan 2021 02:24:23 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id ED9A7811E86 for ; Mon, 11 Jan 2021 02:24:22 +0000 (UTC) Received: from m97136.mail.qiye.163.com (m97136.mail.qiye.163.com [220.181.97.136]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-193-95m_8NOUNi6rFtagtkwmvw-1; Sun, 10 Jan 2021 21:24:18 -0500 Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp2 (Coremail) with SMTP id iOCowAAnkeDCtvtfIO+DCw--.10533S4; Mon, 11 Jan 2021 10:24:09 +0800 (CST) X-MC-Unique: qtki3yXfOta-Q5NmGj8YAg-1 X-MC-Unique: 95m_8NOUNi6rFtagtkwmvw-1 From: Shi Lei To: libvir-list@redhat.com Subject: [PATCHv3 2/4] netlink: Minor changes for macros NETLINK_MSG_[NEST_START|NEST_END|PUT] Date: Mon, 11 Jan 2021 10:23:35 +0800 Message-Id: <20210111022337.27473-3-shi_lei@massclouds.com> In-Reply-To: <20210111022337.27473-1-shi_lei@massclouds.com> References: <20210111022337.27473-1-shi_lei@massclouds.com> MIME-Version: 1.0 X-CM-TRANSID: iOCowAAnkeDCtvtfIO+DCw--.10533S4 X-Coremail-Antispam: 1Uf129KBjvJXoWxuF17XFWUJry7GFykZr4xWFg_yoWrWr1kpa 43KFy7Jr4UJw47Xrnaka4UWa43ZFZYgr15G34xKws2ya17JFyIqry2gas5ZryfC3yDJr4a y3ZI9r45Cw4fXaDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0J1jG-5UUUUU= X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiQgcXT1bdHkBO+QAAs- X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-loop: libvir-list@redhat.com Cc: mprivozn@redhat.com, Shi Lei 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move macros NETLINK_MSG_[NEST_START|NEST_END|PUT] from .h into .c; within these macros, replace 'goto' with reporting error and returning; simplify virNetlinkDumpLink and virNetlinkDelLink by using NETLINK_MSG_PUT. Signed-off-by: Shi Lei --- src/util/virnetlink.c | 44 +++++++++++++++++++++++++++++++++---------- src/util/virnetlink.h | 23 ---------------------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c index 17e6eeb9..0b55b124 100644 --- a/src/util/virnetlink.c +++ b/src/util/virnetlink.c @@ -44,6 +44,35 @@ VIR_LOG_INIT("util.netlink"); =20 # include =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", \ + _("allocated netlink buffer is too small")); \ + return -1; \ + } \ +} 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) { \ + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", \ + _("allocated netlink buffer is too small")); \ + return -1; \ + } \ +} while (0) + + /* State for a single netlink event handle */ struct virNetlinkEventHandle { int watch; @@ -406,10 +435,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) + NETLINK_MSG_PUT(nl_msg, IFLA_IFNAME, (strlen(ifname) + 1), ifname); =20 # ifdef RTEXT_FILTER_VF /* if this filter exists in the kernel's netlink implementation, @@ -419,10 +446,8 @@ virNetlinkDumpLink(const char *ifname, int ifindex, { uint32_t ifla_ext_mask =3D RTEXT_FILTER_VF; =20 - if (nla_put(nl_msg, IFLA_EXT_MASK, - sizeof(ifla_ext_mask), &ifla_ext_mask) < 0) { - goto buffer_too_small; - } + NETLINK_MSG_PUT(nl_msg, IFLA_EXT_MASK, + sizeof(ifla_ext_mask), &ifla_ext_mask); } # endif =20 @@ -636,8 +661,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) - goto buffer_too_small; + NETLINK_MSG_PUT(nl_msg, IFLA_IFNAME, (strlen(ifname) + 1), ifname); =20 if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, 0, 0, NETLINK_ROUTE, 0) < 0) { diff --git a/src/util/virnetlink.h b/src/util/virnetlink.h index 7c4ed202..966d6db3 100644 --- a/src/util/virnetlink.h +++ b/src/util/virnetlink.h @@ -38,29 +38,6 @@ struct nlmsghdr; =20 #endif /* WITH_LIBNL */ =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.25.1 From nobody Tue Apr 16 23:38:41 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1610331880; cv=none; d=zohomail.com; s=zohoarc; b=imMJg3z6zGa7KVKoPcMCs2za82g1h7IBIyXm+h2JI68+6Di6YFWhRjJqM4XaRXHPYwEAcXjR7sD54vU2hMCJ/WVaiEViVTckPQAWZsAJiNt1+sAUXy8PW4yJNk0yFMMfywlahAVo18kpTI8Z3AbpomQmCh0LH73egOSgUYwjvw0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1610331880; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=zjj/1QSncsW5Iuo6P3IhcwgN6iqR6Qbl9Ks9N56aco8=; b=Opxb0Xf8tiEsP6uV6/1CMsqmnaA04bQuMYF1Ph1rYkWUXsr/sXwg5SLdE/Iuqr1IoNVUsdAiILJ+3ExBbhbbkJVNEMT8fJ53erc5vP1NivWFzWe9TZuz0DRHCvD7GMqreMLEbRRNefHZCeAN8rJpxl0L0tzsAQLp/pEiJ1FLHMM= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1610331880750657.4541760683503; Sun, 10 Jan 2021 18:24:40 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-92-NH8kPxwMMmOFZwYkyiiKRg-1; Sun, 10 Jan 2021 21:24:36 -0500 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5C384BBEE0; Mon, 11 Jan 2021 02:24:31 +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 30AEB19D9D; Mon, 11 Jan 2021 02:24:31 +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 DA04D18095FF; Mon, 11 Jan 2021 02:24:30 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 10B2OSAY007257 for ; Sun, 10 Jan 2021 21:24:28 -0500 Received: by smtp.corp.redhat.com (Postfix) id B8858112C09E; Mon, 11 Jan 2021 02:24:28 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast03.extmail.prod.ext.rdu2.redhat.com [10.11.55.19]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B3FBC112C093 for ; Mon, 11 Jan 2021 02:24:24 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5C581811E78 for ; Mon, 11 Jan 2021 02:24:24 +0000 (UTC) Received: from m97136.mail.qiye.163.com (m97136.mail.qiye.163.com [220.181.97.136]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-172-6-Yw7ZMhOiicSiCB75rigA-1; Sun, 10 Jan 2021 21:24:18 -0500 Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp2 (Coremail) with SMTP id iOCowAAnkeDCtvtfIO+DCw--.10533S5; Mon, 11 Jan 2021 10:24:12 +0800 (CST) X-MC-Unique: NH8kPxwMMmOFZwYkyiiKRg-1 X-MC-Unique: 6-Yw7ZMhOiicSiCB75rigA-1 From: Shi Lei To: libvir-list@redhat.com Subject: [PATCHv3 3/4] netlink: Introduce macro NETLINK_MSG_APPEND to wrap nlmsg_append Date: Mon, 11 Jan 2021 10:23:36 +0800 Message-Id: <20210111022337.27473-4-shi_lei@massclouds.com> In-Reply-To: <20210111022337.27473-1-shi_lei@massclouds.com> References: <20210111022337.27473-1-shi_lei@massclouds.com> MIME-Version: 1.0 X-CM-TRANSID: iOCowAAnkeDCtvtfIO+DCw--.10533S5 X-Coremail-Antispam: 1Uf129KBjvJXoWxXFykJw4rXrWDKrykZr45trb_yoW5Kw4fpF y3KF9rZF18Jr17XFyq93W7WF9xZFZ0gr45K348K3Z2y3Z7ZFyjqr42vr4rZa4fAr9xJr4Y y3WDKFWfCr40vaUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0J1jManUUUUU= X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiDA0XT1f11793RgAAsi X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-loop: libvir-list@redhat.com Cc: mprivozn@redhat.com, Shi Lei 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce a macro NETLINK_MSG_APPEND to wrap nlmsg_append and simplify code. Remove those labels 'buffer_too_small', since they are now useless. Signed-off-by: Shi Lei --- src/util/virnetlink.c | 42 +++++++++++++----------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c index 0b55b124..fdcb0dc0 100644 --- a/src/util/virnetlink.c +++ b/src/util/virnetlink.c @@ -72,6 +72,15 @@ do { \ } \ } while (0) =20 +# define NETLINK_MSG_APPEND(msg, datalen, dataptr) \ +do { \ + if (nlmsg_append(msg, dataptr, datalen, NLMSG_ALIGNTO) < 0) { \ + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", \ + _("allocated netlink buffer is too small")); \ + return -1; \ + } \ +} while (0) + =20 /* State for a single netlink event handle */ struct virNetlinkEventHandle { @@ -432,8 +441,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex, return -1; } =20 - if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0) - goto buffer_too_small; + NETLINK_MSG_APPEND(nl_msg, sizeof(ifinfo), &ifinfo); =20 if (ifname) NETLINK_MSG_PUT(nl_msg, IFLA_IFNAME, (strlen(ifname) + 1), ifname); @@ -491,11 +499,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 @@ -545,8 +548,7 @@ virNetlinkNewLink(const char *ifname, return -1; } =20 - if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0) - goto buffer_too_small; + NETLINK_MSG_APPEND(nl_msg, sizeof(ifinfo), &ifinfo); =20 NETLINK_MSG_PUT(nl_msg, IFLA_IFNAME, (strlen(ifname) + 1), ifname); =20 @@ -620,11 +622,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 @@ -658,8 +655,7 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLink= Fallback fallback) return -1; } =20 - if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0) - goto buffer_too_small; + NETLINK_MSG_APPEND(nl_msg, sizeof(ifinfo), &ifinfo); =20 NETLINK_MSG_PUT(nl_msg, IFLA_IFNAME, (strlen(ifname) + 1), ifname); =20 @@ -701,11 +697,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 /** @@ -740,9 +731,7 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, = uint32_t dst_pid) return -1; } =20 - if (nlmsg_append(nl_msg, &ndinfo, sizeof(ndinfo), NLMSG_ALIGNTO) < 0) - goto buffer_too_small; - + NETLINK_MSG_APPEND(nl_msg, sizeof(ndinfo), &ndinfo); =20 if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, src_pid, dst_pid, NETLINK_ROUTE, 0) < 0) @@ -778,11 +767,6 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid,= uint32_t dst_pid) 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 int --=20 2.25.1 From nobody Tue Apr 16 23:38:41 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1610331892; cv=none; d=zohomail.com; s=zohoarc; b=GU8n3pentSI/fmIsRj5AP4oL+o4YGNHqF0yrvY/L7DAG60168M4kqS4hpUG2Ra4h8orL6HTK4DYPWqJDQ5V5Zl114QZjVnLrWlx/9G1NHIw9DqCqWcE3Z3qtcEn9ZKzZO7KpW6CiuLaU1FmquY4DxY5QGjnmpMpXtEWv3MIz5zE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1610331892; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=sbtXUSzZUgUWpIw4nGrMYAE6ToKfV/o0340Kj/Gwn7o=; b=KBS4P6hXdJr+deMPEqbYJfqP3WTXFoRSozKZsQmYL1M+WNiNt+AxfDGReKlMSbkVSVQd2u1oJYjnXJADHRRrar+TsgSaQI48Rox+mEZf3T0xA28yuGdS9bbhSSGbn4klfpTHb46FRWgmItA6hDJoFKIbnOMQ3PG+UipqVE3DtE0= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1610331892045933.9153847721748; Sun, 10 Jan 2021 18:24:52 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-34-qD2uXe8BPZGEs4NEWJ9a3A-1; Sun, 10 Jan 2021 21:24:48 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 78B9F800D62; Mon, 11 Jan 2021 02:24:43 +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 519D476E16; Mon, 11 Jan 2021 02:24:43 +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 1F4084EEEE; Mon, 11 Jan 2021 02:24:43 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 10B2ONcS007233 for ; Sun, 10 Jan 2021 21:24:24 -0500 Received: by smtp.corp.redhat.com (Postfix) id C1FE42EF8E; Mon, 11 Jan 2021 02:24:23 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast04.extmail.prod.ext.rdu2.redhat.com [10.11.55.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BCC3233228 for ; Mon, 11 Jan 2021 02:24:23 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A5ED6101A560 for ; Mon, 11 Jan 2021 02:24:23 +0000 (UTC) Received: from m97136.mail.qiye.163.com (m97136.mail.qiye.163.com [220.181.97.136]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-449-c6rNjRu1MPSP3yqeOJIhmg-1; Sun, 10 Jan 2021 21:24:19 -0500 Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp2 (Coremail) with SMTP id iOCowAAnkeDCtvtfIO+DCw--.10533S6; Mon, 11 Jan 2021 10:24:14 +0800 (CST) X-MC-Unique: qD2uXe8BPZGEs4NEWJ9a3A-1 X-MC-Unique: c6rNjRu1MPSP3yqeOJIhmg-1 From: Shi Lei To: libvir-list@redhat.com Subject: [PATCHv3 4/4] netlink: Introduce a helper function to simplify netlink functions Date: Mon, 11 Jan 2021 10:23:37 +0800 Message-Id: <20210111022337.27473-5-shi_lei@massclouds.com> In-Reply-To: <20210111022337.27473-1-shi_lei@massclouds.com> References: <20210111022337.27473-1-shi_lei@massclouds.com> MIME-Version: 1.0 X-CM-TRANSID: iOCowAAnkeDCtvtfIO+DCw--.10533S6 X-Coremail-Antispam: 1Uf129KBjvJXoW3CrWUJF1Uuw43ArW7uw45KFg_yoWDWFyxpF 1Utas7XFy8tr1rZrZ2yFyxKayavrnxWr45KrWUKwn7A3WfZry5Xw43ur1Y9ay3Cr95Jr1F yF1agFWFyr1DX3JanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0J1jManUUUUU= X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiEQ4XT1nxtPgkwwAAsE X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-loop: libvir-list@redhat.com Cc: mprivozn@redhat.com, Shi Lei 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extract common code as helper function virNetlinkTalk, then simplify the functions virNetlink[DumpLink|NewLink|DelLink|GetNeighbor]. Signed-off-by: Shi Lei --- src/util/virnetlink.c | 232 ++++++++++++++++++------------------------ src/util/virnetlink.h | 4 +- 2 files changed, 101 insertions(+), 135 deletions(-) diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c index fdcb0dc0..650acff7 100644 --- a/src/util/virnetlink.c +++ b/src/util/virnetlink.c @@ -353,6 +353,54 @@ int virNetlinkCommand(struct nl_msg *nl_msg, return 0; } =20 + +static int +virNetlinkTalk(const char *ifname, + virNetlinkMsg *nl_msg, + uint32_t src_pid, + uint32_t dst_pid, + struct nlmsghdr **resp, + unsigned int *resp_len, + int *error, + virNetlinkTalkFallback fallback) +{ + if (virNetlinkCommand(nl_msg, resp, resp_len, + src_pid, dst_pid, NETLINK_ROUTE, 0) < 0) + return -1; + + if (*resp_len < NLMSG_LENGTH(0) || *resp =3D=3D NULL) + goto malformed_resp; + + if ((*resp)->nlmsg_type =3D=3D NLMSG_ERROR) { + struct nlmsgerr *err; + + err =3D (struct nlmsgerr *) NLMSG_DATA(*resp); + + if ((*resp)->nlmsg_len < NLMSG_LENGTH(sizeof(*err))) + goto malformed_resp; + + if (-err->error =3D=3D EOPNOTSUPP && fallback) + return fallback(ifname); + + if (err->error < 0) { + if (error) + *error =3D err->error; + else + virReportSystemError(-err->error, "%s", _("netlink error")= ); + + return -1; + } + } + + return 0; + + malformed_resp: + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("malformed netlink response message")); + return -1; +} + + int virNetlinkDumpCommand(struct nl_msg *nl_msg, virNetlinkDumpCallback callback, @@ -396,6 +444,7 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg, return 0; } =20 + /** * virNetlinkDumpLink: * @@ -420,15 +469,14 @@ virNetlinkDumpLink(const char *ifname, int ifindex, void **nlData, struct nlattr **tb, uint32_t src_pid, uint32_t dst_pid) { - int rc =3D -1; - struct nlmsgerr *err; struct ifinfomsg ifinfo =3D { .ifi_family =3D AF_UNSPEC, .ifi_index =3D ifindex }; - unsigned int recvbuflen; g_autoptr(virNetlinkMsg) nl_msg =3D NULL; g_autofree struct nlmsghdr *resp =3D NULL; + unsigned int resp_len =3D 0; + int error =3D 0; =20 if (ifname && ifindex <=3D 0 && virNetDevGetIndex(ifname, &ifindex) < = 0) return -1; @@ -459,46 +507,25 @@ virNetlinkDumpLink(const char *ifname, int ifindex, } # endif =20 - if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, - src_pid, dst_pid, NETLINK_ROUTE, 0) < 0) + if (virNetlinkTalk(ifname, nl_msg, src_pid, dst_pid, + &resp, &resp_len, &error, NULL) < 0) { + virReportSystemError(error, + _("error dumping %s (%d) interface"), + ifname, ifindex); return -1; + } =20 - if (recvbuflen < NLMSG_LENGTH(0) || resp =3D=3D NULL) - goto malformed_resp; - - switch (resp->nlmsg_type) { - case NLMSG_ERROR: - err =3D (struct nlmsgerr *)NLMSG_DATA(resp); - if (resp->nlmsg_len < NLMSG_LENGTH(sizeof(*err))) - goto malformed_resp; - - if (err->error) { - virReportSystemError(-err->error, - _("error dumping %s (%d) interface"), - ifname, ifindex); - return -1; - } - break; - - case GENL_ID_CTRL: - case NLMSG_DONE: - rc =3D nlmsg_parse(resp, sizeof(struct ifinfomsg), - tb, IFLA_MAX, NULL); - if (rc < 0) - goto malformed_resp; - break; - - default: - goto malformed_resp; + if ((resp->nlmsg_type !=3D NLMSG_ERROR && + resp->nlmsg_type !=3D GENL_ID_CTRL && + resp->nlmsg_type !=3D NLMSG_DONE) || + nlmsg_parse(resp, sizeof(struct ifinfomsg), tb, IFLA_MAX, NULL) < = 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("malformed netlink response message")); + return -1; } =20 *nlData =3D g_steal_pointer(&resp); return 0; - - malformed_resp: - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("malformed netlink response message")); - return rc; } =20 =20 @@ -523,13 +550,12 @@ virNetlinkNewLink(const char *ifname, 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 }; g_autoptr(virNetlinkMsg) nl_msg =3D NULL; g_autofree struct nlmsghdr *resp =3D NULL; + unsigned int resp_len =3D 0; =20 *error =3D 0; =20 @@ -591,37 +617,18 @@ virNetlinkNewLink(const char *ifname, } } =20 - if (virNetlinkCommand(nl_msg, &resp, &buflen, 0, 0, NETLINK_ROUTE, 0) = < 0) + if (virNetlinkTalk(ifname, nl_msg, 0, 0, + &resp, &resp_len, error, NULL) < 0) return -1; =20 - 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; + if (resp->nlmsg_type !=3D NLMSG_ERROR && + resp->nlmsg_type !=3D NLMSG_DONE) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("malformed netlink response message")); + return -1; } =20 return 0; - - malformed_resp: - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("malformed netlink response message")); - return -1; } =20 =20 @@ -641,13 +648,13 @@ virNetlinkNewLink(const char *ifname, * Returns 0 on success, -1 on fatal error. */ int -virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback) +virNetlinkDelLink(const char *ifname, virNetlinkTalkFallback fallback) { - struct nlmsgerr *err; struct ifinfomsg ifinfo =3D { .ifi_family =3D AF_UNSPEC }; - unsigned int recvbuflen; g_autoptr(virNetlinkMsg) nl_msg =3D NULL; g_autofree struct nlmsghdr *resp =3D NULL; + unsigned int resp_len =3D 0; + int error =3D 0; =20 nl_msg =3D nlmsg_alloc_simple(RTM_DELLINK, NLM_F_REQUEST); if (!nl_msg) { @@ -659,44 +666,22 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLi= nkFallback fallback) =20 NETLINK_MSG_PUT(nl_msg, IFLA_IFNAME, (strlen(ifname) + 1), ifname); =20 - if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, 0, 0, - NETLINK_ROUTE, 0) < 0) { + if (virNetlinkTalk(ifname, nl_msg, 0, 0, + &resp, &resp_len, &error, fallback) < 0) { + virReportSystemError(error, + _("error destroying network device %s"), + ifname); return -1; } =20 - 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 =3D=3D EOPNOTSUPP && fallback) - return fallback(ifname); - - if (err->error) { - virReportSystemError(-err->error, - _("error destroying network device %s"), - ifname); - return -1; - } - break; - - case NLMSG_DONE: - break; - - default: - goto malformed_resp; + if (resp->nlmsg_type !=3D NLMSG_ERROR && + resp->nlmsg_type !=3D NLMSG_DONE) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("malformed netlink response message")); + return -1; } =20 return 0; - - malformed_resp: - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("malformed netlink response message")); - return -1; } =20 /** @@ -712,18 +697,18 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLi= nkFallback fallback) * * Get neighbor table entry from netlink. * - * Returns 0 on success, -1 on fatal error. + * Returns length of the raw data from netlink on success, -1 on fatal err= or. */ int virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid) { - struct nlmsgerr *err; struct ndmsg ndinfo =3D { .ndm_family =3D AF_UNSPEC, }; - unsigned int recvbuflen; g_autoptr(virNetlinkMsg) nl_msg =3D NULL; g_autofree struct nlmsghdr *resp =3D NULL; + unsigned int resp_len =3D 0; + int error =3D 0; =20 nl_msg =3D nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_DUMP | NLM_F_REQUEST= ); if (!nl_msg) { @@ -733,40 +718,21 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid= , uint32_t dst_pid) =20 NETLINK_MSG_APPEND(nl_msg, sizeof(ndinfo), &ndinfo); =20 - if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, - src_pid, dst_pid, NETLINK_ROUTE, 0) < 0) + if (virNetlinkTalk(NULL, nl_msg, src_pid, dst_pid, + &resp, &resp_len, &error, NULL) < 0) { + virReportSystemError(error, "%s", _("error dumping")); return -1; + } =20 - if (recvbuflen < NLMSG_LENGTH(0) || resp =3D=3D NULL) - goto malformed_resp; - - switch (resp->nlmsg_type) { - case NLMSG_ERROR: - err =3D (struct nlmsgerr *)NLMSG_DATA(resp); - if (resp->nlmsg_len < NLMSG_LENGTH(sizeof(*err))) - goto malformed_resp; - - if (err->error) { - virReportSystemError(-err->error, - "%s", _("error dumping")); - return -1; - } - break; - - case RTM_NEWNEIGH: - break; - - default: - goto malformed_resp; + if (resp->nlmsg_type !=3D NLMSG_ERROR && + resp->nlmsg_type !=3D RTM_NEWNEIGH) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("malformed netlink response message")); + return -1; } =20 *nlData =3D g_steal_pointer(&resp); - return recvbuflen; - - malformed_resp: - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("malformed netlink response message")); - return -1; + return resp_len; } =20 int @@ -1300,7 +1266,7 @@ virNetlinkDumpLink(const char *ifname G_GNUC_UNUSED, =20 int virNetlinkDelLink(const char *ifname G_GNUC_UNUSED, - virNetlinkDelLinkFallback fallback G_GNUC_UNUSED) + virNetlinkTalkFallback fallback G_GNUC_UNUSED) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported)); return -1; diff --git a/src/util/virnetlink.h b/src/util/virnetlink.h index 966d6db3..cab685fe 100644 --- a/src/util/virnetlink.h +++ b/src/util/virnetlink.h @@ -69,9 +69,9 @@ int virNetlinkNewLink(const char *ifname, virNetlinkNewLinkDataPtr data, int *error); =20 -typedef int (*virNetlinkDelLinkFallback)(const char *ifname); +typedef int (*virNetlinkTalkFallback)(const char *ifname); =20 -int virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallba= ck); +int virNetlinkDelLink(const char *ifname, virNetlinkTalkFallback fallback); =20 int virNetlinkGetErrorCode(struct nlmsghdr *resp, unsigned int recvbuflen); =20 --=20 2.25.1