From nobody Sun May 5 11:34:37 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=1606102138; cv=none; d=zohomail.com; s=zohoarc; b=ZuVZuPXlfLd3z8u/Ni8yku+jWXxzEbdMvCaMn1I/RwTlvP7bp74Lt09e3DuRYIql+I3MXdh0U2ujagKQMKzdz6m8VkUlkXqhiQwY5/ZxYPJJUu0g4jM5a10lRmupB6sB3QaFHvF6Wk9TAR2CDBBO6mWpFd4R34hUZDkp6OuEVdM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1606102138; 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=Byh5a9g6XAy5I/7W+0BbL6wmY7fZ8oRKyXljEMf85YI=; b=bFgJJhojExoflH3tTn316unxg/TR0nyASxe0FV9B2La9hv9YMU81urMARE3jBAX442Sa47f/Qmry7nuL/qXuasbR+ymSUjBjfw3N14y4YtvE4xIcseINM9bpwoCYJzPppA+ae/ANCn9FYstNuOmIqf42x0Srnchg5m8DyHrWI+8= 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 1606102137948910.7369368669248; Sun, 22 Nov 2020 19:28:57 -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-171-H32-ArSXNZiAZDKsZfRmZQ-1; Sun, 22 Nov 2020 22:28:53 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 79A4351EF; Mon, 23 Nov 2020 03:28:46 +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 2EEEE1346D; Mon, 23 Nov 2020 03:28:45 +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 29843180954D; Mon, 23 Nov 2020 03:28:42 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 0AN3SdHX019897 for ; Sun, 22 Nov 2020 22:28:40 -0500 Received: by smtp.corp.redhat.com (Postfix) id CB1022026D14; Mon, 23 Nov 2020 03:28:39 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast06.extmail.prod.ext.rdu2.redhat.com [10.11.55.22]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C5BF02026D12 for ; Mon, 23 Nov 2020 03:28:37 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-1.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 65174185A794 for ; Mon, 23 Nov 2020 03:28:37 +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-39-FpjBUyOWMoKdb24MmREpNQ-1; Sun, 22 Nov 2020 22:28:34 -0500 Received: from localhost.localdomain (unknown [113.128.197.238]) by smtp2 (Coremail) with SMTP id iOCowACnK+pVLLtfbfH8CQ--.84S3; Mon, 23 Nov 2020 11:28:29 +0800 (CST) X-MC-Unique: H32-ArSXNZiAZDKsZfRmZQ-1 X-MC-Unique: FpjBUyOWMoKdb24MmREpNQ-1 From: Shi Lei To: libvir-list@redhat.com Subject: [PATCHv2 1/2] util:netlink: Enable virNetlinkNewLink to support veth Date: Mon, 23 Nov 2020 11:28:13 +0800 Message-Id: <20201123032814.64964-2-shi_lei@massclouds.com> In-Reply-To: <20201123032814.64964-1-shi_lei@massclouds.com> References: <20201123032814.64964-1-shi_lei@massclouds.com> MIME-Version: 1.0 X-CM-TRANSID: iOCowACnK+pVLLtfbfH8CQ--.84S3 X-Coremail-Antispam: 1Uf129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73 VFW2AGmfu7bjvjm3AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjfUJ18BUUUUU X-Originating-IP: [113.128.197.238] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiRB3lT1bdHYPoowAAst 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.4 X-loop: libvir-list@redhat.com Cc: Shi Lei , laine@redhat.com 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.11 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" Signed-off-by: Shi Lei Reviewed-by: Laine Stump --- src/util/virnetlink.c | 14 ++++++++++++++ src/util/virnetlink.h | 1 + 2 files changed, 15 insertions(+) diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c index fdd3a6a4..8625b896 100644 --- a/src/util/virnetlink.c +++ b/src/util/virnetlink.c @@ -24,6 +24,7 @@ #include =20 #include +#include =20 #include "virnetlink.h" #include "virnetdev.h" @@ -535,6 +536,19 @@ virNetlinkNewLink(const char *ifname, NETLINK_MSG_NEST_END(nl_msg, infodata); } =20 + if (STREQ(type, "veth") && extra_args && extra_args->veth_peer) { + struct nlattr *infoveth =3D NULL; + + NETLINK_MSG_NEST_START(nl_msg, infodata, IFLA_INFO_DATA); + NETLINK_MSG_NEST_START(nl_msg, infoveth, VETH_INFO_PEER); + nlmsg_reserve(nl_msg, sizeof(struct ifinfomsg), 0); + NETLINK_MSG_PUT(nl_msg, IFLA_IFNAME, + (strlen(extra_args->veth_peer) + 1), + extra_args->veth_peer); + NETLINK_MSG_NEST_END(nl_msg, infoveth); + NETLINK_MSG_NEST_END(nl_msg, infodata); + } + NETLINK_MSG_NEST_END(nl_msg, linkinfo); =20 if (extra_args) { diff --git a/src/util/virnetlink.h b/src/util/virnetlink.h index 7121eac4..7c4ed202 100644 --- a/src/util/virnetlink.h +++ b/src/util/virnetlink.h @@ -84,6 +84,7 @@ 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 */ + const char *veth_peer; /* The peer name for veth */ }; =20 int virNetlinkNewLink(const char *ifname, --=20 2.25.1 From nobody Sun May 5 11:34:37 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=1606102496; cv=none; d=zohomail.com; s=zohoarc; b=YL9NogakNiejxEN4msUskxoVi/ofuNpqS1VveOC8TmzbMWI9xgO5xDUHv0z9wre3UuGizl/C+Jwf46NuxMoxCeXBJojvKtEqYP79YncyyLbO/ELOTPVgVh9q9GugFn8rHSAR02jt/SpC3sF4M4LjoZMv5e/Bh/a994CIt35YpEk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1606102496; 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=jV0r3h9AM+0oP2sncbCjzZPaG6J3qWk3ZVvT25KArgo=; b=ia7pjBWT7W0w5cCVcfgZ8C4rp1xzrCb7M8qQUigzcWx/CSeukgDXd5mQWsqCHml52/GuA2rx4i9tMhG47IADCvdtHkHUCbx/+14+4QsFLdTCBU831WWblCkCSQIjv0q9Gu7etIqQw0VJxu5F08YGeeK20b7H+sD7hLesQY1KzB4= 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 1606102496893496.672899591912; Sun, 22 Nov 2020 19:34:56 -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-292-L2F6E3UCMwOibDY6f0Oj2g-1; Sun, 22 Nov 2020 22:34:53 -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 35FD81052079; Mon, 23 Nov 2020 03:34:48 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0B5381A88B; Mon, 23 Nov 2020 03:34:48 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 969A15002D; Mon, 23 Nov 2020 03:34:47 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 0AN3YeuU020503 for ; Sun, 22 Nov 2020 22:34:40 -0500 Received: by smtp.corp.redhat.com (Postfix) id EBD742026D15; Mon, 23 Nov 2020 03:34:39 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast06.extmail.prod.ext.rdu2.redhat.com [10.11.55.22]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E69552026D12 for ; Mon, 23 Nov 2020 03:34:37 +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 A0E96186E121 for ; Mon, 23 Nov 2020 03:34:37 +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-87-O4qJ6b_iNm6HkWQe0e4IJQ-1; Sun, 22 Nov 2020 22:34:34 -0500 Received: from localhost.localdomain (unknown [113.128.197.238]) by smtp2 (Coremail) with SMTP id iOCowACnK+pVLLtfbfH8CQ--.84S4; Mon, 23 Nov 2020 11:28:32 +0800 (CST) X-MC-Unique: L2F6E3UCMwOibDY6f0Oj2g-1 X-MC-Unique: O4qJ6b_iNm6HkWQe0e4IJQ-1 From: Shi Lei To: libvir-list@redhat.com Subject: [PATCHv2 2/2] util:veth: Create veth device pair by netlink Date: Mon, 23 Nov 2020 11:28:14 +0800 Message-Id: <20201123032814.64964-3-shi_lei@massclouds.com> In-Reply-To: <20201123032814.64964-1-shi_lei@massclouds.com> References: <20201123032814.64964-1-shi_lei@massclouds.com> MIME-Version: 1.0 X-CM-TRANSID: iOCowACnK+pVLLtfbfH8CQ--.84S4 X-Coremail-Antispam: 1Uf129KBjvJXoWxXryxCr17uryrJw1Utw1DWrg_yoWrJF4Up3 y0yFZ5Ar18trsrAayjvFykWFn8Zr4vgry5t39rZ3sru3y5J345Xr1xCFyUX347trWfCFnY 9Fs3Zr98Ca1UGFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0J1U7P_UUUUU= X-Originating-IP: [113.128.197.238] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiSQrlT1dqApR9cgAAsN 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.4 X-loop: libvir-list@redhat.com Cc: Shi Lei , laine@redhat.com 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" When netlink is supported, use netlink to create veth device pair rather than 'ip link' command. Signed-off-by: Shi Lei --- src/util/virnetdevveth.c | 85 ++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 29 deletions(-) diff --git a/src/util/virnetdevveth.c b/src/util/virnetdevveth.c index b3eee1af..b4074371 100644 --- a/src/util/virnetdevveth.c +++ b/src/util/virnetdevveth.c @@ -27,6 +27,7 @@ #include "virfile.h" #include "virstring.h" #include "virnetdev.h" +#include "virnetlink.h" =20 #define VIR_FROM_THIS VIR_FROM_NONE =20 @@ -75,6 +76,55 @@ static int virNetDevVethGetFreeNum(int startDev) return -1; } =20 +#if defined(WITH_LIBNL) +static int +virNetDevVethCreateInternal(const char *veth1, const char *veth2, int *sta= tus) +{ + virNetlinkNewLinkData data =3D { .veth_peer =3D veth2 }; + + return virNetlinkNewLink(veth1, "veth", &data, status); +} + +static int +virNetDevVethDeleteInternal(const char *veth) +{ + return virNetlinkDelLink(veth, NULL); +} +#else +static int +virNetDevVethCreateInternal(const char *veth1, const char *veth2, int *sta= tus) +{ + g_autoptr(virCommand) cmd =3D virCommandNew("ip"); + virCommandAddArgList(cmd, "link", "add", veth1, "type", "veth", + "peer", "name", veth2, NULL); + + return virCommandRun(cmd, status); +} + +static int +virNetDevVethDeleteInternal(const char *veth) +{ + int status; + g_autoptr(virCommand) cmd =3D virCommandNewArgList("ip", "link", + "del", veth, NULL); + + if (virCommandRun(cmd, &status) < 0) + return -1; + + if (status !=3D 0) { + if (!virNetDevExists(veth)) { + VIR_DEBUG("Device %s already deleted (by kernel namespace clea= nup)", veth); + return 0; + } + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to delete veth device %s"), veth); + return -1; + } + + return 0; +} +#endif /* WITH_LIBNL */ + /** * virNetDevVethCreate: * @veth1: pointer to name for parent end of veth pair @@ -100,7 +150,7 @@ static int virNetDevVethGetFreeNum(int startDev) * * Returns 0 on success or -1 in case of error */ -int virNetDevVethCreate(char** veth1, char** veth2) +int virNetDevVethCreate(char **veth1, char **veth2) { int ret =3D -1; int vethNum =3D 0; @@ -116,9 +166,8 @@ int virNetDevVethCreate(char** veth1, char** veth2) for (i =3D 0; i < MAX_VETH_RETRIES; i++) { g_autofree char *veth1auto =3D NULL; g_autofree char *veth2auto =3D NULL; - g_autoptr(virCommand) cmd =3D NULL; =20 - int status; + int status =3D 0; if (!*veth1) { int veth1num; if ((veth1num =3D virNetDevVethGetFreeNum(vethNum)) < 0) @@ -136,14 +185,9 @@ int virNetDevVethCreate(char** veth1, char** veth2) vethNum =3D veth2num + 1; } =20 - cmd =3D virCommandNew("ip"); - virCommandAddArgList(cmd, "link", "add", - *veth1 ? *veth1 : veth1auto, - "type", "veth", "peer", "name", - *veth2 ? *veth2 : veth2auto, - NULL); - - if (virCommandRun(cmd, &status) < 0) + if (virNetDevVethCreateInternal(*veth1 ? *veth1 : veth1auto, + *veth2 ? *veth2 : veth2auto, + &status) < 0) goto cleanup; =20 if (status =3D=3D 0) { @@ -188,22 +232,5 @@ int virNetDevVethCreate(char** veth1, char** veth2) */ int virNetDevVethDelete(const char *veth) { - int status; - g_autoptr(virCommand) cmd =3D virCommandNewArgList("ip", "link", - "del", veth, NULL); - - if (virCommandRun(cmd, &status) < 0) - return -1; - - if (status !=3D 0) { - if (!virNetDevExists(veth)) { - VIR_DEBUG("Device %s already deleted (by kernel namespace clea= nup)", veth); - return 0; - } - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Failed to delete veth device %s"), veth); - return -1; - } - - return 0; + return virNetDevVethDeleteInternal(veth); } --=20 2.25.1