From nobody Sat May 18 15:38:15 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=1607065580; cv=none; d=zohomail.com; s=zohoarc; b=PQ8FVVxReqf21v0wGwU6fVVNGJLAqgPT+CQioeGF06j+WmaFeGTeu6+iyXeM9UF1SWdU/WoV8K8cPqS7h5wExh2U9FTvT7mI+jCeIoaaa/3hUahku0xy2vGwt1AyJJpZZtfsjB6Tn3H/EW5iMr2KgrhgiAOm8S8ZhSgEyzcrN3A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1607065580; 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=XQfq8L+CiWh6VGQTYWgqpe9g9Ehl1oaCB/JNS+lkaDM=; b=JkgFUyAL11i9cpf5hOgcTn0VHrRCU0JJ9bxGPk1gqc2qNtwI/CekcO8vBLv894rCPb9csTKnTyHXuOycWlArPNw7OdnYdV75LDbuJcvTx9WKhwnkta0SXUzCTXd9W5tYg/6oKQo2hJAdhY6uAwEXThynfdFSvRqz3fB0lmI7prY= 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 16070655805043.875651337747172; Thu, 3 Dec 2020 23:06:20 -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-146-rtB8cez1OcurWUsbBXrvxw-1; Fri, 04 Dec 2020 02:06:17 -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 1A69A190A7A5; Fri, 4 Dec 2020 07:06:11 +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 EB94060854; Fri, 4 Dec 2020 07:06:10 +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 87E701809CA1; Fri, 4 Dec 2020 07:06:10 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 0B4731tI003385 for ; Fri, 4 Dec 2020 02:03:02 -0500 Received: by smtp.corp.redhat.com (Postfix) id 849FA2166B2B; Fri, 4 Dec 2020 07:03:01 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast05.extmail.prod.ext.rdu2.redhat.com [10.11.55.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7FB612166B27 for ; Fri, 4 Dec 2020 07:03:01 +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 6BD4D800296 for ; Fri, 4 Dec 2020 07:03:01 +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-255-ZulHHExhPGW0SHJ4x0Q1LA-1; Fri, 04 Dec 2020 02:02:57 -0500 Received: from localhost.localdomain (unknown [113.128.197.238]) by smtp2 (Coremail) with SMTP id iOCowABHlOoY38lfucJqCg--.4173S3; Fri, 04 Dec 2020 15:02:48 +0800 (CST) X-MC-Unique: rtB8cez1OcurWUsbBXrvxw-1 X-MC-Unique: ZulHHExhPGW0SHJ4x0Q1LA-1 From: Shi Lei To: libvir-list@redhat.com Subject: [PATCH 1/5] netdev: Introduce several helper functions for generating unique netdev name Date: Fri, 4 Dec 2020 15:01:31 +0800 Message-Id: <20201204070135.33840-2-shi_lei@massclouds.com> In-Reply-To: <20201204070135.33840-1-shi_lei@massclouds.com> References: <20201204070135.33840-1-shi_lei@massclouds.com> MIME-Version: 1.0 X-CM-TRANSID: iOCowABHlOoY38lfucJqCg--.4173S3 X-Coremail-Antispam: 1Uf129KBjvJXoW3XFykAw48KryrWr1kur1Dtrb_yoWxKF17pr n5GF98Gr18Gr4xAF18Zr1xCryrAryxWFn3Gr9ru3sFq3yUJ347JrW7KF4Utw1fJrZ3Gryf ZF1Fv348Ga1UCFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0J1j3ktUUUUU= X-Originating-IP: [113.128.197.238] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiAxLwT1ilmGgglwAAsU 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.6 X-loop: libvir-list@redhat.com Cc: 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 ReserveName/GenerateName from netdevtap and netdevmacvlan as common helper functions. Along with them, export Lock/Unlock functions to protect these processes. Signed-off-by: Shi Lei --- src/libvirt_private.syms | 4 ++ src/util/virnetdev.c | 140 +++++++++++++++++++++++++++++++++++++++ src/util/virnetdev.h | 33 +++++++++ 3 files changed, 177 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 2f640ef1..be3148c9 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2551,6 +2551,7 @@ virNetDevDelMulti; virNetDevExists; virNetDevFeatureTypeFromString; virNetDevFeatureTypeToString; +virNetDevGenerateName; virNetDevGetFeatures; virNetDevGetIndex; virNetDevGetLinkInfo; @@ -2572,8 +2573,10 @@ virNetDevGetVLanID; virNetDevIfStateTypeFromString; virNetDevIfStateTypeToString; virNetDevIsVirtualFunction; +virNetDevLockGenName; virNetDevPFGetVF; virNetDevReadNetConfig; +virNetDevReserveName; virNetDevRunEthernetScript; virNetDevRxFilterFree; virNetDevRxFilterModeTypeFromString; @@ -2594,6 +2597,7 @@ virNetDevSetRcvMulti; virNetDevSetRootQDisc; virNetDevSetupControl; virNetDevSysfsFile; +virNetDevUnlockGenName; virNetDevValidateConfig; virNetDevVFInterfaceStats; =20 diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c index 5104bbe7..5ff8e35f 100644 --- a/src/util/virnetdev.c +++ b/src/util/virnetdev.c @@ -17,6 +17,7 @@ */ =20 #include +#include =20 #include "virnetdev.h" #include "viralloc.h" @@ -95,6 +96,22 @@ VIR_LOG_INIT("util.netdev"); (FEATURE_WORD(blocks, index, field) & FEATURE_FIELD_FLAG(index)) #endif =20 +VIR_ENUM_IMPL(virNetDevGenNameType, + VIR_NET_DEV_GEN_NAME_LAST, + "none", + "tap", + "macvtap", + "macvlan", +); + +static virNetDevGenName +virNetDevGenNames[VIR_NET_DEV_GEN_NAME_LAST] =3D { + {0, NULL, VIR_MUTEX_INITIALIZER}, + {-1, VIR_NET_GENERATED_TAP_PREFIX, VIR_MUTEX_INITIALIZER}, + {-1, VIR_NET_GENERATED_MACVTAP_PREFIX, VIR_MUTEX_INITIALIZER}, + {-1, VIR_NET_GENERATED_MACVLAN_PREFIX, VIR_MUTEX_INITIALIZER}, +}; + typedef enum { VIR_MCAST_TYPE_INDEX_TOKEN, VIR_MCAST_TYPE_NAME_TOKEN, @@ -3516,3 +3533,126 @@ virNetDevSetRootQDisc(const char *ifname, =20 return 0; } + + +/** + * virNetDevReserveName: + * @name: name of an existing network device + * @type: type of the network device + * @locked: whether this process is locked by the internal mutex + * + * Reserve a network device name, so that any new network device + * created with an autogenerated name will use a number higher + * than the number in the given device name. + * + * Returns nothing. + */ +void +virNetDevReserveName(const char *name, + virNetDevGenNameType type, + bool locked) +{ + unsigned int id; + const char *idstr =3D NULL; + + if (type && STRPREFIX(name, virNetDevGenNames[type].prefix)) { + + VIR_INFO("marking device in use: '%s'", name); + + idstr =3D name + strlen(virNetDevGenNames[type].prefix); + + if (virStrToLong_ui(idstr, NULL, 10, &id) >=3D 0) { + if (locked) + virMutexLock(&virNetDevGenNames[type].mutex); + + if (virNetDevGenNames[type].lastID < (int)id) + virNetDevGenNames[type].lastID =3D id; + + if (locked) + virMutexUnlock(&virNetDevGenNames[type].mutex); + } + } +} + + +/** + * virNetDevGenerateName: + * @ifname: pointer to pointer to string containing template + * + * generate a new (currently unused) name for a new network device based + * on the templace string in @ifname - replace %d with the reserved id, + * and keep trying new values until one is found + * that doesn't already exist, or we've tried 10000 different + * names. Once a usable name is found, replace the template with the + * actual name. + * + * Returns 0 on success, -1 on failure. + */ +int +virNetDevGenerateName(char **ifname, virNetDevGenNameType type) +{ + int id; + const char *prefix =3D virNetDevGenNames[type].prefix; + double maxIDd =3D pow(10, IFNAMSIZ - 1 - strlen(prefix)); + int maxID =3D INT_MAX; + int attempts =3D 0; + + if (maxIDd <=3D (double)INT_MAX) + maxID =3D (int)maxIDd; + + do { + g_autofree char *try =3D NULL; + + id =3D ++virNetDevGenNames[type].lastID; + + /* reset before overflow */ + if (virNetDevGenNames[type].lastID >=3D maxID) + virNetDevGenNames[type].lastID =3D -1; + + if (*ifname) + try =3D g_strdup_printf(*ifname, id); + else + try =3D g_strdup_printf("%s%d", prefix, id); + + if (!virNetDevExists(try)) { + g_free(*ifname); + *ifname =3D g_steal_pointer(&try); + return 0; + } + } while (++attempts < 10000); + + virReportError(VIR_ERR_INTERNAL_ERROR, + _("no unused %s names available"), + prefix); + return -1; +} + + +/** + * virNetDevLockGenName: + * @type: type of the network device + * + * Lock the internal mutex for creation for this network device type. + * + * Returns nothing. + */ +void +virNetDevLockGenName(virNetDevGenNameType type) +{ + virMutexLock(&virNetDevGenNames[type].mutex); +} + + +/** + * virNetDevUnlockGenName: + * @type: type of the network device + * + * Unlock the internal mutex for creation for this network device type. + * + * Returns nothing. + */ +void +virNetDevUnlockGenName(virNetDevGenNameType type) +{ + virMutexUnlock(&virNetDevGenNames[type].mutex); +} diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h index 53e606c6..19f37b61 100644 --- a/src/util/virnetdev.h +++ b/src/util/virnetdev.h @@ -40,6 +40,12 @@ typedef void virIfreq; */ #define VIR_NET_GENERATED_TAP_PREFIX "vnet" =20 +/* libvirt will start macvtap/macvlan interface names with one of + * these prefixes when it auto-generates the name + */ +#define VIR_NET_GENERATED_MACVTAP_PREFIX "macvtap" +#define VIR_NET_GENERATED_MACVLAN_PREFIX "macvlan" + typedef enum { VIR_NETDEV_RX_FILTER_MODE_NONE =3D 0, VIR_NETDEV_RX_FILTER_MODE_NORMAL, @@ -145,6 +151,24 @@ struct _virNetDevCoalesce { uint32_t rate_sample_interval; }; =20 +typedef enum { + VIR_NET_DEV_GEN_NAME_NONE =3D 0, + VIR_NET_DEV_GEN_NAME_TAP, + VIR_NET_DEV_GEN_NAME_MACVTAP, + VIR_NET_DEV_GEN_NAME_MACVLAN, + VIR_NET_DEV_GEN_NAME_LAST +} virNetDevGenNameType; + +VIR_ENUM_DECL(virNetDevGenNameType); + +typedef struct _virNetDevGenName virNetDevGenName; +typedef virNetDevGenName *virNetDevGenNamePtr; +struct _virNetDevGenName { + int lastID; /* not "unsigned" because callers use %d */ + const char *prefix; + virMutex mutex; +}; + =20 int virNetDevSetupControl(const char *ifname, virIfreq *ifr) @@ -321,3 +345,12 @@ int virNetDevVFInterfaceStats(virPCIDeviceAddressPtr v= fAddr, ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); =20 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetDevRxFilter, virNetDevRxFilterFree); + +void virNetDevReserveName(const char *name, + virNetDevGenNameType type, + bool locked); + +int virNetDevGenerateName(char **ifname, virNetDevGenNameType type); + +void virNetDevLockGenName(virNetDevGenNameType type); +void virNetDevUnlockGenName(virNetDevGenNameType type); --=20 2.25.1 From nobody Sat May 18 15:38:15 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=1607065659; cv=none; d=zohomail.com; s=zohoarc; b=Z62VFq3/gC1vOaQzN3uwypUzGhXHxB1Mq8LihdBHrTUeNP+1ShkiWB6ScbJYKLJh9oEJld1OKxV7i5fSKn3FJOOKwLTpOVPOur4B55OXt0z2jUBPMEWXa8gxsMZmm9AQgb701OagCn2Ncyd/ZBzNRwGFwlEmwV4yNdn271pRQHQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1607065659; 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=RbQPLw3lYrYYZImmh7YiBYhJ2lPn/qDG1+0sZ08wy/I=; b=HofdxSzJJkqs3ylAyplMmp4GH3BSbySa3989B39FEPDn5V6xOzUWtTVgoNgCbdZ37rnL3sPAM8j0L8CGbyjpSElwx4e1/189IO6T+wl05sASQ65h3gF3+kNfXRtSo86k9o7fSSUPX4WDVFMwKbMJ61WSbS7V20Zfw5RqP7LNM7Y= 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 1607065659797152.46343989980835; Thu, 3 Dec 2020 23:07: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-373--70_20PSOyqwLG6kM16JuQ-1; Fri, 04 Dec 2020 02:07:36 -0500 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id A6E41858184; Fri, 4 Dec 2020 07:07:30 +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 7D7DA5C1CF; Fri, 4 Dec 2020 07:07: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 40A41180954D; Fri, 4 Dec 2020 07:07:30 +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 0B4733PZ003406 for ; Fri, 4 Dec 2020 02:03:03 -0500 Received: by smtp.corp.redhat.com (Postfix) id 61E24200AD4D; Fri, 4 Dec 2020 07:03:03 +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 5D1E42028CD7 for ; Fri, 4 Dec 2020 07:03:01 +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 0B694811E78 for ; Fri, 4 Dec 2020 07:03:01 +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-367-h77nThDhP7G0-58jtf_zdA-1; Fri, 04 Dec 2020 02:02:56 -0500 Received: from localhost.localdomain (unknown [113.128.197.238]) by smtp2 (Coremail) with SMTP id iOCowABHlOoY38lfucJqCg--.4173S4; Fri, 04 Dec 2020 15:02:48 +0800 (CST) X-MC-Unique: -70_20PSOyqwLG6kM16JuQ-1 X-MC-Unique: h77nThDhP7G0-58jtf_zdA-1 From: Shi Lei To: libvir-list@redhat.com Subject: [PATCH 2/5] netdevtap: Use common helper function to create unique tap name Date: Fri, 4 Dec 2020 15:01:32 +0800 Message-Id: <20201204070135.33840-3-shi_lei@massclouds.com> In-Reply-To: <20201204070135.33840-1-shi_lei@massclouds.com> References: <20201204070135.33840-1-shi_lei@massclouds.com> MIME-Version: 1.0 X-CM-TRANSID: iOCowABHlOoY38lfucJqCg--.4173S4 X-Coremail-Antispam: 1Uf129KBjvJXoWxur1UuF4fAr48XFWDuw1kGrg_yoW5Wr4fpa n3GF9I9rs7Jr4qkryvv3Z8uF1rAr1jgas5G345Za4fX3yUCry3Xry2ka4rKry5JrWftw1r AF1av348Ja48AaDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0J1j3ktUUUUU= X-Originating-IP: [113.128.197.238] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiRRjwT1bdHTqTIAAAs9 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 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.16 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" Simplify GenerateName/ReserveName for netdevtap by using common functions. Signed-off-by: Shi Lei --- src/util/virnetdevtap.c | 58 +++-------------------------------------- 1 file changed, 4 insertions(+), 54 deletions(-) diff --git a/src/util/virnetdevtap.c b/src/util/virnetdevtap.c index 198607b5..38b2f171 100644 --- a/src/util/virnetdevtap.c +++ b/src/util/virnetdevtap.c @@ -55,9 +55,6 @@ =20 VIR_LOG_INIT("util.netdevtap"); =20 -virMutex virNetDevTapCreateMutex =3D VIR_MUTEX_INITIALIZER; -static int virNetDevTapLastID =3D -1; /* not "unsigned" because callers us= e %d */ - =20 /** * virNetDevTapReserveName: @@ -72,25 +69,7 @@ static int virNetDevTapLastID =3D -1; /* not "unsigned" = because callers use %d */ void virNetDevTapReserveName(const char *name) { - unsigned int id; - const char *idstr =3D NULL; - - - if (STRPREFIX(name, VIR_NET_GENERATED_TAP_PREFIX)) { - - VIR_INFO("marking device in use: '%s'", name); - - idstr =3D name + strlen(VIR_NET_GENERATED_TAP_PREFIX); - - if (virStrToLong_ui(idstr, NULL, 10, &id) >=3D 0) { - virMutexLock(&virNetDevTapCreateMutex); - - if (virNetDevTapLastID < (int)id) - virNetDevTapLastID =3D id; - - virMutexUnlock(&virNetDevTapCreateMutex); - } - } + virNetDevReserveName(name, VIR_NET_DEV_GEN_NAME_TAP, true); } =20 =20 @@ -199,36 +178,7 @@ virNetDevTapGetRealDeviceName(char *ifname G_GNUC_UNUS= ED) static int virNetDevTapGenerateName(char **ifname) { - int id; - double maxIDd =3D pow(10, IFNAMSIZ - 1 - strlen(VIR_NET_GENERATED_TAP_= PREFIX)); - int maxID =3D INT_MAX; - int attempts =3D 0; - - if (maxIDd <=3D (double)INT_MAX) - maxID =3D (int)maxIDd; - - do { - g_autofree char *try =3D NULL; - - id =3D ++virNetDevTapLastID; - - /* reset before overflow */ - if (virNetDevTapLastID >=3D maxID) - virNetDevTapLastID =3D -1; - - try =3D g_strdup_printf(*ifname, id); - - if (!virNetDevExists(try)) { - g_free(*ifname); - *ifname =3D g_steal_pointer(&try); - return 0; - } - } while (++attempts < 10000); - - virReportError(VIR_ERR_INTERNAL_ERROR, - _("no unused %s names available"), - VIR_NET_GENERATED_TAP_PREFIX); - return -1; + return virNetDevGenerateName(ifname, VIR_NET_DEV_GEN_NAME_TAP); } =20 =20 @@ -263,7 +213,7 @@ int virNetDevTapCreate(char **ifname, int ret =3D -1; int fd =3D -1; =20 - virMutexLock(&virNetDevTapCreateMutex); + virNetDevLockGenName(VIR_NET_DEV_GEN_NAME_TAP); =20 /* if ifname is "vnet%d", then auto-generate a name for the new * device (the kernel could do this for us, but has a bad habit of @@ -333,7 +283,7 @@ int virNetDevTapCreate(char **ifname, ret =3D 0; =20 cleanup: - virMutexUnlock(&virNetDevTapCreateMutex); + virNetDevUnlockGenName(VIR_NET_DEV_GEN_NAME_TAP); if (ret < 0) { VIR_FORCE_CLOSE(fd); while (i--) --=20 2.25.1 From nobody Sat May 18 15:38:15 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=1607065606; cv=none; d=zohomail.com; s=zohoarc; b=DO0nBhIW0BzuJSK2CwY4RNiuUnbo4YgHIiUGeZfJ0+2kmRol5dCKSMqdGqKkAAKnHScsP19DI8YP8QdOsQYxHzYSabM094ZPWcO6jIOw8jMI2AMInUT7IIm1ZWDlpoid13Q8T2qow2QAM/XbmfOI5PjwHk0cI/3GgP97ynx7DAc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1607065606; 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=OpvQwjfz3DAuigNanGW9H/OeKjArxPdL0T2T74ZICRw=; b=U8eFuspwGmeEnw2m5vRGsEVIZZWmkrrFavHrBsTPm0F+dfLfAiAQVz6kJGU6eBsOlpCxnwdGBqv0mRzGnJfTN8ZRixJV6RrJEzckpk0KQnKYmoivGXI9tbjRZwy6pgLlTlBu/ygYttDkK4PUH9P3Y9kbrWsCNvR/3W0MT/AID58= 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 1607065606565130.15115042111324; Thu, 3 Dec 2020 23:06:46 -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-45-4M1zZ32_NQCcBrgJ-PCJhg-1; Fri, 04 Dec 2020 02:06:43 -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 50F02AFA83; Fri, 4 Dec 2020 07:06:32 +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 2BB7D60C69; Fri, 4 Dec 2020 07:06:32 +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 E987C1809CA7; Fri, 4 Dec 2020 07:06:31 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 0B472x3R003367 for ; Fri, 4 Dec 2020 02:03:00 -0500 Received: by smtp.corp.redhat.com (Postfix) id EA3AF2166B2B; Fri, 4 Dec 2020 07:02:58 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast05.extmail.prod.ext.rdu2.redhat.com [10.11.55.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E4CA02166B27 for ; Fri, 4 Dec 2020 07:02:56 +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 A9FB1800296 for ; Fri, 4 Dec 2020 07:02:56 +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-241-KL6tYvcdPt-kJq6Yd8uDXg-1; Fri, 04 Dec 2020 02:02:53 -0500 Received: from localhost.localdomain (unknown [113.128.197.238]) by smtp2 (Coremail) with SMTP id iOCowABHlOoY38lfucJqCg--.4173S5; Fri, 04 Dec 2020 15:02:49 +0800 (CST) X-MC-Unique: 4M1zZ32_NQCcBrgJ-PCJhg-1 X-MC-Unique: KL6tYvcdPt-kJq6Yd8uDXg-1 From: Shi Lei To: libvir-list@redhat.com Subject: [PATCH 3/5] netdevmacvlan: Use helper function to create unique macvlan/macvtap name Date: Fri, 4 Dec 2020 15:01:33 +0800 Message-Id: <20201204070135.33840-4-shi_lei@massclouds.com> In-Reply-To: <20201204070135.33840-1-shi_lei@massclouds.com> References: <20201204070135.33840-1-shi_lei@massclouds.com> MIME-Version: 1.0 X-CM-TRANSID: iOCowABHlOoY38lfucJqCg--.4173S5 X-Coremail-Antispam: 1Uf129KBjvJXoW3Wr48Aw4UCr13CF4xCw18AFb_yoWxAw4DpF 4rGF9Igr4xJr40kFy0vFyDWryfZr9rXFZ3Gry5Z3sYyay5WryxAr4Ika4xZ345JrWrJF18 AF1Fv34kC3W5GrJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0J1j3ktUUUUU= X-Originating-IP: [113.128.197.238] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiQhnwT1bdHXtDwQAAsL 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.6 X-loop: libvir-list@redhat.com Cc: 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" Simplify ReserveName/GenerateName for macvlan and macvtap by using common functions. Signed-off-by: Shi Lei --- src/util/virnetdevmacvlan.c | 107 ++++++++---------------------------- src/util/virnetdevmacvlan.h | 6 -- 2 files changed, 22 insertions(+), 91 deletions(-) diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c index 72f0d670..7f58d7ca 100644 --- a/src/util/virnetdevmacvlan.c +++ b/src/util/virnetdevmacvlan.c @@ -45,7 +45,6 @@ VIR_ENUM_IMPL(virNetDevMacVLanMode, =20 # include # include -# include =20 # include "viralloc.h" # include "virlog.h" @@ -64,39 +63,20 @@ VIR_LOG_INIT("util.netdevmacvlan"); # define VIR_NET_GENERATED_PREFIX \ ((flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ? \ VIR_NET_GENERATED_MACVTAP_PREFIX : VIR_NET_GENERATED_MACVLAN_PREFIX) +# define VIR_NET_CREATE_TYPE \ + ((flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ? \ + VIR_NET_DEV_GEN_NAME_MACVTAP : VIR_NET_DEV_GEN_NAME_MACVLAN) =20 =20 -virMutex virNetDevMacVLanCreateMutex =3D VIR_MUTEX_INITIALIZER; -static int virNetDevMacVTapLastID =3D -1; -static int virNetDevMacVLanLastID =3D -1; - - -static void -virNetDevMacVLanReserveNameInternal(const char *name) +static virNetDevGenNameType +virNetDevMacVLanGetTypeByName(const char *name) { - unsigned int id; - const char *idstr =3D NULL; - int *lastID =3D NULL; - int len; - - if (STRPREFIX(name, VIR_NET_GENERATED_MACVTAP_PREFIX)) { - lastID =3D &virNetDevMacVTapLastID; - len =3D strlen(VIR_NET_GENERATED_MACVTAP_PREFIX); - } else if (STRPREFIX(name, VIR_NET_GENERATED_MACVLAN_PREFIX)) { - lastID =3D &virNetDevMacVTapLastID; - len =3D strlen(VIR_NET_GENERATED_MACVLAN_PREFIX); - } else { - return; - } - - VIR_INFO("marking device in use: '%s'", name); - - idstr =3D name + len; - - if (virStrToLong_ui(idstr, NULL, 10, &id) >=3D 0) { - if (*lastID < (int)id) - *lastID =3D id; - } + if (STRPREFIX(name, VIR_NET_GENERATED_MACVTAP_PREFIX)) + return VIR_NET_DEV_GEN_NAME_MACVTAP; + else if (STRPREFIX(name, VIR_NET_GENERATED_MACVLAN_PREFIX)) + return VIR_NET_DEV_GEN_NAME_MACVLAN; + else + return VIR_NET_DEV_GEN_NAME_NONE; } =20 =20 @@ -113,9 +93,7 @@ virNetDevMacVLanReserveNameInternal(const char *name) void virNetDevMacVLanReserveName(const char *name) { - virMutexLock(&virNetDevMacVLanCreateMutex); - virNetDevMacVLanReserveNameInternal(name); - virMutexUnlock(&virNetDevMacVLanCreateMutex); + virNetDevReserveName(name, virNetDevMacVLanGetTypeByName(name), true); } =20 =20 @@ -136,50 +114,7 @@ virNetDevMacVLanReserveName(const char *name) static int virNetDevMacVLanGenerateName(char **ifname, unsigned int flags) { - const char *prefix; - const char *iftemplate; - int *lastID; - int id; - double maxIDd; - int maxID =3D INT_MAX; - int attempts =3D 0; - - if (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) { - prefix =3D VIR_NET_GENERATED_MACVTAP_PREFIX; - iftemplate =3D VIR_NET_GENERATED_MACVTAP_PREFIX "%d"; - lastID =3D &virNetDevMacVTapLastID; - } else { - prefix =3D VIR_NET_GENERATED_MACVLAN_PREFIX; - iftemplate =3D VIR_NET_GENERATED_MACVLAN_PREFIX "%d"; - lastID =3D &virNetDevMacVLanLastID; - } - - maxIDd =3D pow(10, IFNAMSIZ - 1 - strlen(prefix)); - if (maxIDd <=3D (double)INT_MAX) - maxID =3D (int)maxIDd; - - do { - g_autofree char *try =3D NULL; - - id =3D ++(*lastID); - - /* reset before overflow */ - if (*lastID =3D=3D maxID) - *lastID =3D -1; - - try =3D g_strdup_printf(iftemplate, id); - - if (!virNetDevExists(try)) { - g_free(*ifname); - *ifname =3D g_steal_pointer(&try); - return 0; - } - } while (++attempts < 10000); - - virReportError(VIR_ERR_INTERNAL_ERROR, - _("no unused %s names available"), - *ifname); - return -1; + return virNetDevGenerateName(ifname, VIR_NET_CREATE_TYPE); } =20 =20 @@ -832,7 +767,7 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifna= meRequested, return -1; } =20 - virMutexLock(&virNetDevMacVLanCreateMutex); + virNetDevLockGenName(VIR_NET_CREATE_TYPE); =20 if (ifnameRequested) { int rc; @@ -843,7 +778,7 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifna= meRequested, VIR_INFO("Requested macvtap device name: %s", ifnameRequested); =20 if ((rc =3D virNetDevExists(ifnameRequested)) < 0) { - virMutexUnlock(&virNetDevMacVLanCreateMutex); + virNetDevUnlockGenName(VIR_NET_CREATE_TYPE); return -1; } =20 @@ -854,14 +789,16 @@ virNetDevMacVLanCreateWithVPortProfile(const char *if= nameRequested, virReportSystemError(EEXIST, _("Unable to create device '%s'"), ifnameRequested); - virMutexUnlock(&virNetDevMacVLanCreateMutex); + virNetDevUnlockGenName(VIR_NET_CREATE_TYPE); return -1; } } else { =20 /* ifnameRequested is available. try to open it */ =20 - virNetDevMacVLanReserveNameInternal(ifnameRequested); + virNetDevReserveName(ifnameRequested, + VIR_NET_CREATE_TYPE, + false); =20 if (virNetDevMacVLanCreate(ifnameRequested, type, macaddress, linkdev, macvtapMode) =3D=3D 0) { @@ -874,7 +811,7 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifna= meRequested, * autogenerated named, so there is nothing else to * try - fail and return. */ - virMutexUnlock(&virNetDevMacVLanCreateMutex); + virNetDevUnlockGenName(VIR_NET_CREATE_TYPE); return -1; } } @@ -888,13 +825,13 @@ virNetDevMacVLanCreateWithVPortProfile(const char *if= nameRequested, if (virNetDevMacVLanGenerateName(&ifname, flags) < 0 || virNetDevMacVLanCreate(ifname, type, macaddress, linkdev, macvtapMode) < 0) { - virMutexUnlock(&virNetDevMacVLanCreateMutex); + virNetDevUnlockGenName(VIR_NET_CREATE_TYPE); return -1; } } =20 /* all done creating the device */ - virMutexUnlock(&virNetDevMacVLanCreateMutex); + virNetDevUnlockGenName(VIR_NET_CREATE_TYPE); =20 if (virNetDevVPortProfileAssociate(ifname, virtPortProfile, diff --git a/src/util/virnetdevmacvlan.h b/src/util/virnetdevmacvlan.h index 48800a8f..cbdfef59 100644 --- a/src/util/virnetdevmacvlan.h +++ b/src/util/virnetdevmacvlan.h @@ -48,12 +48,6 @@ typedef enum { VIR_NETDEV_MACVLAN_VNET_HDR =3D 1 << 2, } virNetDevMacVLanCreateFlags; =20 -/* libvirt will start macvtap/macvlan interface names with one of - * these prefixes when it auto-generates the name - */ -#define VIR_NET_GENERATED_MACVTAP_PREFIX "macvtap" -#define VIR_NET_GENERATED_MACVLAN_PREFIX "macvlan" - void virNetDevMacVLanReserveName(const char *name); =20 bool virNetDevMacVLanIsMacvtap(const char *ifname) --=20 2.25.1 From nobody Sat May 18 15:38:15 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=1607065596; cv=none; d=zohomail.com; s=zohoarc; b=iWS/sslYOhJltCZhlJycaO6DJaybGwhKC9gJiwtxa8DjMOiUqJMMtmOWKPBOHIF6goCiXwaWizdDflfpoDalSE/PUPZMa/viTH+qrJ5YMYoVAwXIoLWQ9i5iDA4Q5t0lC3ym3vNTIrGLfaGPgyRnjX7u+eiBR5gZyjepVXGxoaA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1607065596; 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=5Bx+hZFXvXsSDfokeGSzavBjzFSuxhFRgir3gpHtYEY=; b=VlEg2Bg6xDOvoZLG2sRFYRTuZcXqv1t8vVbcdl0O7yDtWCl82GHmfdAGoRmVeUDyFBURDY/yowrex7F8qf4dKT9VLMoAmBtntB+EJWMcqZBpMg7pAnngx7CxFlm9aGvcTYgwsCPL0FZo4nHhvJjqWTthMuYU/BMOOCfKwDFgpw0= 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 1607065596862862.0884446338112; Thu, 3 Dec 2020 23:06:36 -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-170-nbrncXgxOcS10G9LgNWaZg-1; Fri, 04 Dec 2020 02:06:33 -0500 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id 81E4FAFA80; Fri, 4 Dec 2020 07:06:28 +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 5FA9B5D9D7; Fri, 4 Dec 2020 07:06:28 +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 29C361809CA1; Fri, 4 Dec 2020 07:06:28 +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 0B472xPq003371 for ; Fri, 4 Dec 2020 02:03:00 -0500 Received: by smtp.corp.redhat.com (Postfix) id C168CF3D13; Fri, 4 Dec 2020 07:02:59 +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 BBD77F3D17 for ; Fri, 4 Dec 2020 07:02:57 +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 35CD6103B802 for ; Fri, 4 Dec 2020 07:02:57 +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-97-ZDpWLDF5MgmTrPHtxMD2wQ-1; Fri, 04 Dec 2020 02:02:53 -0500 Received: from localhost.localdomain (unknown [113.128.197.238]) by smtp2 (Coremail) with SMTP id iOCowABHlOoY38lfucJqCg--.4173S6; Fri, 04 Dec 2020 15:02:49 +0800 (CST) X-MC-Unique: nbrncXgxOcS10G9LgNWaZg-1 X-MC-Unique: ZDpWLDF5MgmTrPHtxMD2wQ-1 From: Shi Lei To: libvir-list@redhat.com Subject: [PATCH 4/5] netdev: Enable virNetDevGenerateName to support veth Date: Fri, 4 Dec 2020 15:01:34 +0800 Message-Id: <20201204070135.33840-5-shi_lei@massclouds.com> In-Reply-To: <20201204070135.33840-1-shi_lei@massclouds.com> References: <20201204070135.33840-1-shi_lei@massclouds.com> MIME-Version: 1.0 X-CM-TRANSID: iOCowABHlOoY38lfucJqCg--.4173S6 X-Coremail-Antispam: 1Uf129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73 VFW2AGmfu7bjvjm3AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjfUJsYwUUUUU X-Originating-IP: [113.128.197.238] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiWRbwT1f4qCaKuwAAsg 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: 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.14 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 --- src/util/virnetdev.c | 2 ++ src/util/virnetdev.h | 3 +++ 2 files changed, 5 insertions(+) diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c index 5ff8e35f..ff1b1fa0 100644 --- a/src/util/virnetdev.c +++ b/src/util/virnetdev.c @@ -102,6 +102,7 @@ VIR_ENUM_IMPL(virNetDevGenNameType, "tap", "macvtap", "macvlan", + "veth", ); =20 static virNetDevGenName @@ -110,6 +111,7 @@ virNetDevGenNames[VIR_NET_DEV_GEN_NAME_LAST] =3D { {-1, VIR_NET_GENERATED_TAP_PREFIX, VIR_MUTEX_INITIALIZER}, {-1, VIR_NET_GENERATED_MACVTAP_PREFIX, VIR_MUTEX_INITIALIZER}, {-1, VIR_NET_GENERATED_MACVLAN_PREFIX, VIR_MUTEX_INITIALIZER}, + {-1, VIR_NET_GENERATED_VETH_PREFIX, VIR_MUTEX_INITIALIZER}, }; =20 typedef enum { diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h index 19f37b61..097d0f8e 100644 --- a/src/util/virnetdev.h +++ b/src/util/virnetdev.h @@ -40,6 +40,8 @@ typedef void virIfreq; */ #define VIR_NET_GENERATED_TAP_PREFIX "vnet" =20 +#define VIR_NET_GENERATED_VETH_PREFIX "veth" + /* libvirt will start macvtap/macvlan interface names with one of * these prefixes when it auto-generates the name */ @@ -156,6 +158,7 @@ typedef enum { VIR_NET_DEV_GEN_NAME_TAP, VIR_NET_DEV_GEN_NAME_MACVTAP, VIR_NET_DEV_GEN_NAME_MACVLAN, + VIR_NET_DEV_GEN_NAME_VETH, VIR_NET_DEV_GEN_NAME_LAST } virNetDevGenNameType; =20 --=20 2.25.1 From nobody Sat May 18 15:38:15 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=1607065570; cv=none; d=zohomail.com; s=zohoarc; b=eDHRKOfUwd6IKJx4JMIqrdlEULLN8KKOcPdbAzsC0mhN0KcadB/0ENCNo88IqTMyfW4p56487Uq8jSDdYfnTgNT4RY+pDGFkWlMO7JqfHrn/VPgeeDEqjU/nozlr5rCjfBeCh1jgUudYib10Fg4/GWrMm9pEVAddjZMwkJ/+/Z4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1607065570; 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=1W/jiPv7GHAOlu2ZCBWWOcA+eyMiOASjc7yt4kCRe44=; b=DtMjyhMWvQcLC634pfbvjdsemlCspi0Rabn20jGxLWiiSK6eYkdS2p9RESkcd8k/bKymzdz6T+pzREeG+GCzDlIQZXoAWxOew5m5KEYaQGK+hi8MW+jk4XFTC/p4V6YItc0LyEKl6gs3iQ/aXP37h8HRRS6KU4yUdY5XqGxsn7s= 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 1607065570093159.58166683684874; Thu, 3 Dec 2020 23:06:10 -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-389-Cat-uaOiMguoMFjFCxlGVg-1; Fri, 04 Dec 2020 02:06:07 -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 A8B14107ACE3; Fri, 4 Dec 2020 07:06:00 +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 4EA7B1A8A0; Fri, 4 Dec 2020 07:06:00 +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 3D280180954D; Fri, 4 Dec 2020 07:05:59 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 0B472xVk003370 for ; Fri, 4 Dec 2020 02:03:00 -0500 Received: by smtp.corp.redhat.com (Postfix) id 9C0372166B30; Fri, 4 Dec 2020 07:02:59 +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 96B582166B2C for ; Fri, 4 Dec 2020 07:02:59 +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 4D58E811E78 for ; Fri, 4 Dec 2020 07:02:59 +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-576-PDcP1_jVP6a2XIPqhOD8GA-1; Fri, 04 Dec 2020 02:02:56 -0500 Received: from localhost.localdomain (unknown [113.128.197.238]) by smtp2 (Coremail) with SMTP id iOCowABHlOoY38lfucJqCg--.4173S7; Fri, 04 Dec 2020 15:02:49 +0800 (CST) X-MC-Unique: Cat-uaOiMguoMFjFCxlGVg-1 X-MC-Unique: PDcP1_jVP6a2XIPqhOD8GA-1 From: Shi Lei To: libvir-list@redhat.com Subject: [PATCH 5/5] netdevveth: Simplify virNetDevVethCreate by using virNetDevGenerateName Date: Fri, 4 Dec 2020 15:01:35 +0800 Message-Id: <20201204070135.33840-6-shi_lei@massclouds.com> In-Reply-To: <20201204070135.33840-1-shi_lei@massclouds.com> References: <20201204070135.33840-1-shi_lei@massclouds.com> MIME-Version: 1.0 X-CM-TRANSID: iOCowABHlOoY38lfucJqCg--.4173S7 X-Coremail-Antispam: 1Uf129KBjvJXoW3XF1DGw47AFy7Gry5tFWfAFb_yoW7XF1fpw 4FyF9YyrW8Ww4kAayUAFy8ZFn5Cr45Kr93KasrZ397Z3y5Zr45Xr1xCFy8trW7JFWrAFyS vF1Yv34Duay7JFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0Jb_38rUUUUU= X-Originating-IP: [113.128.197.238] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbidRnwT1n5fRiBAwAAsU 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.6 X-loop: libvir-list@redhat.com Cc: 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" Simplify virNetDevVethCreate by using common GenerateName/ReserveName functions. Signed-off-by: Shi Lei --- src/lxc/lxc_process.c | 3 + src/util/virnetdevveth.c | 146 +++++++++++---------------------------- 2 files changed, 43 insertions(+), 106 deletions(-) diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index 7e07f49f..5f277a22 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -287,6 +287,9 @@ virLXCProcessSetupInterfaceTap(virDomainDefPtr vm, =20 VIR_DEBUG("calling vethCreate()"); parentVeth =3D net->ifname; + if (parentVeth) + virNetDevReserveName(parentVeth, VIR_NET_DEV_GEN_NAME_VETH, true); + if (virNetDevVethCreate(&parentVeth, &containerVeth) < 0) return NULL; VIR_DEBUG("parentVeth: %s, containerVeth: %s", parentVeth, containerVe= th); diff --git a/src/util/virnetdevveth.c b/src/util/virnetdevveth.c index b3eee1af..2e3d0c82 100644 --- a/src/util/virnetdevveth.c +++ b/src/util/virnetdevveth.c @@ -32,48 +32,6 @@ =20 VIR_LOG_INIT("util.netdevveth"); =20 -/* Functions */ - -virMutex virNetDevVethCreateMutex =3D VIR_MUTEX_INITIALIZER; - -static int virNetDevVethExists(int devNum) -{ - int ret; - g_autofree char *path =3D NULL; - - path =3D g_strdup_printf(SYSFS_NET_DIR "vnet%d/", devNum); - ret =3D virFileExists(path) ? 1 : 0; - VIR_DEBUG("Checked dev vnet%d usage: %d", devNum, ret); - return ret; -} - -/** - * virNetDevVethGetFreeNum: - * @startDev: device number to start at (x in vethx) - * - * Looks in /sys/class/net/ to find the first available veth device - * name. - * - * Returns non-negative device number on success or -1 in case of error - */ -static int virNetDevVethGetFreeNum(int startDev) -{ - int devNum; - -#define MAX_DEV_NUM 65536 - - for (devNum =3D startDev; devNum < MAX_DEV_NUM; devNum++) { - int ret =3D virNetDevVethExists(devNum); - if (ret < 0) - return -1; - if (ret =3D=3D 0) - return devNum; - } - - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("No free veth devices available")); - return -1; -} =20 /** * virNetDevVethCreate: @@ -102,77 +60,53 @@ static int virNetDevVethGetFreeNum(int startDev) */ int virNetDevVethCreate(char** veth1, char** veth2) { - int ret =3D -1; - int vethNum =3D 0; - size_t i; - - /* - * We might race with other containers, but this is reasonably - * unlikely, so don't do too many retries for device creation - */ - virMutexLock(&virNetDevVethCreateMutex); -#define MAX_VETH_RETRIES 10 - - 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; - - int status; - if (!*veth1) { - int veth1num; - if ((veth1num =3D virNetDevVethGetFreeNum(vethNum)) < 0) - goto cleanup; - - veth1auto =3D g_strdup_printf("vnet%d", veth1num); - vethNum =3D veth1num + 1; - } - if (!*veth2) { - int veth2num; - if ((veth2num =3D virNetDevVethGetFreeNum(vethNum)) < 0) - goto cleanup; + int status; + g_autofree char *veth1auto =3D NULL; + g_autofree char *veth2auto =3D NULL; + g_autoptr(virCommand) cmd =3D NULL; =20 - veth2auto =3D g_strdup_printf("vnet%d", veth2num); - vethNum =3D veth2num + 1; - } + virNetDevLockGenName(VIR_NET_DEV_GEN_NAME_VETH); =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) - goto cleanup; - - if (status =3D=3D 0) { - if (veth1auto) { - *veth1 =3D veth1auto; - veth1auto =3D NULL; - } - if (veth2auto) { - *veth2 =3D veth2auto; - veth2auto =3D NULL; - } - VIR_DEBUG("Create Host: %s guest: %s", *veth1, *veth2); - ret =3D 0; - goto cleanup; + if (!*veth1) { + if (virNetDevGenerateName(&veth1auto, VIR_NET_DEV_GEN_NAME_VETH) <= 0) { + virNetDevUnlockGenName(VIR_NET_DEV_GEN_NAME_VETH); + return -1; + } + } + if (!*veth2) { + if (virNetDevGenerateName(&veth2auto, VIR_NET_DEV_GEN_NAME_VETH) <= 0) { + virNetDevUnlockGenName(VIR_NET_DEV_GEN_NAME_VETH); + return -1; } + } + + virNetDevUnlockGenName(VIR_NET_DEV_GEN_NAME_VETH); =20 - VIR_DEBUG("Failed to create veth host: %s guest: %s: %d", - *veth1 ? *veth1 : veth1auto, - *veth2 ? *veth2 : veth2auto, - status); + cmd =3D virCommandNew("ip"); + virCommandAddArgList(cmd, "link", "add", + *veth1 ? *veth1 : veth1auto, + "type", "veth", "peer", "name", + *veth2 ? *veth2 : veth2auto, + NULL); + + if (virCommandRun(cmd, &status) < 0 || status) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "%s", _("Failed to allocate free veth pair")); + return -1; } =20 - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Failed to allocate free veth pair after %d attempts"= ), - MAX_VETH_RETRIES); + VIR_DEBUG("create veth host: %s guest: %s: %d", + *veth1 ? *veth1 : veth1auto, + *veth2 ? *veth2 : veth2auto, + status); + + if (veth1auto) + *veth1 =3D g_steal_pointer(&veth1auto); + if (veth2auto) + *veth2 =3D g_steal_pointer(&veth2auto); =20 - cleanup: - virMutexUnlock(&virNetDevVethCreateMutex); - return ret; + VIR_DEBUG("Create Host: %s guest: %s", *veth1, *veth2); + return 0; } =20 /** --=20 2.25.1