From nobody Mon Feb 9 03:13:48 2026 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