From nobody Mon Feb 9 09:09:49 2026 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