From nobody Sun Feb 8 20:59:52 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=1607910692; cv=none; d=zohomail.com; s=zohoarc; b=KvGOCZme2FcL/TAWTrzuEJqU+Ys+VWpytorncLGecwjgF35CLFx0GvZmY+sRBnQFDpxOJxPc8v+8wVmM0IKH/ItIdHMucO29MIxoKNJdUaTon0DksxQe3C8ysT3sqnveUDkvUbGqvOtjpyWcWAGhoZDYhhJFuDV9Azj/a7+beDY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1607910692; 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=XmpsuDhMxh7JXJ0lFdMW0OxSgdXQQkt2Ng5ov5gFg2Q=; b=QXzNwWMOACn9T+DrnAnLYYQBERZwNsNrFZ+GVM2l7Pj9AHAop4gWJrGzH56RWIXcaRMUi71GEWwak8pXlpk+yFHUz5yfjSUWksvQcBdyqN7VTPmDAL+j6L7xnfMWTsM8Nhv7DLC+ugA7bWCAJ9IVpOZTc9Fx0gANOIAIYazyvQo= 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 1607910691859560.4231117240315; Sun, 13 Dec 2020 17:51:31 -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-242-WcMrrfE4OBCpSSh9nZka8Q-1; Sun, 13 Dec 2020 20:51:28 -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 376F6107ACE4; Mon, 14 Dec 2020 01:51:23 +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 15BA562467; Mon, 14 Dec 2020 01:51:23 +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 7C9C54A7C6; Mon, 14 Dec 2020 01:51:21 +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 0BE1pKE1016820 for ; Sun, 13 Dec 2020 20:51:20 -0500 Received: by smtp.corp.redhat.com (Postfix) id 6AC132026D46; Mon, 14 Dec 2020 01:51:20 +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 669C72026D11 for ; Mon, 14 Dec 2020 01:51:18 +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 F117F186E121 for ; Mon, 14 Dec 2020 01:51:17 +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-216-CQ1E_CsNPhqji8FWj3P7eQ-1; Sun, 13 Dec 2020 20:51:13 -0500 Received: from localhost.localdomain (unknown [113.128.197.238]) by smtp2 (Coremail) with SMTP id iOCowACHH+sIxdZfXRTKCg--.255S4; Mon, 14 Dec 2020 09:51:06 +0800 (CST) X-MC-Unique: WcMrrfE4OBCpSSh9nZka8Q-1 X-MC-Unique: CQ1E_CsNPhqji8FWj3P7eQ-1 From: Shi Lei To: libvir-list@redhat.com Subject: [PATCHv3 2/5] netdevtap: Use common helper function to create unique tap name Date: Mon, 14 Dec 2020 09:50:33 +0800 Message-Id: <20201214015036.51607-3-shi_lei@massclouds.com> In-Reply-To: <20201214015036.51607-1-shi_lei@massclouds.com> References: <20201214015036.51607-1-shi_lei@massclouds.com> MIME-Version: 1.0 X-CM-TRANSID: iOCowACHH+sIxdZfXRTKCg--.255S4 X-Coremail-Antispam: 1Uf129KBjvJXoWxtr17Jr1kuw4UuryrWr4xXrb_yoW7Ary8p3 Z3GF9akr4xtF4vkFyUZF1rCF1fAr1jqFyfG3s8uFyIv3y8ur1xXry3CFy8KrW5J3y3Jw1F yFyFvryUGa4UAaDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0J1j3kZUUUUU= X-Originating-IP: [113.128.197.238] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiART6T1ikUhtiHAAAsr 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.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" Simplify GenerateName/ReserveName for netdevtap by using common functions. Signed-off-by: Shi Lei --- src/libvirt_private.syms | 1 - src/qemu/qemu_process.c | 2 +- src/util/virnetdevtap.c | 100 ++------------------------------------- src/util/virnetdevtap.h | 4 -- 4 files changed, 5 insertions(+), 102 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index c0f50856..64ef01e1 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2689,7 +2689,6 @@ virNetDevTapGetName; virNetDevTapGetRealDeviceName; virNetDevTapInterfaceStats; virNetDevTapReattachBridge; -virNetDevTapReserveName; =20 =20 # util/virnetdevveth.h diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index 3b64caa6..1d54f201 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -3395,7 +3395,7 @@ qemuProcessNotifyNets(virDomainDefPtr def) case VIR_DOMAIN_NET_TYPE_BRIDGE: case VIR_DOMAIN_NET_TYPE_NETWORK: case VIR_DOMAIN_NET_TYPE_ETHERNET: - virNetDevTapReserveName(net->ifname); + virNetDevReserveName(net->ifname); break; case VIR_DOMAIN_NET_TYPE_USER: case VIR_DOMAIN_NET_TYPE_VHOSTUSER: diff --git a/src/util/virnetdevtap.c b/src/util/virnetdevtap.c index 9354cc10..88ad3216 100644 --- a/src/util/virnetdevtap.c +++ b/src/util/virnetdevtap.c @@ -49,51 +49,11 @@ #if defined(WITH_GETIFADDRS) && defined(AF_LINK) # include #endif -#include =20 #define VIR_FROM_THIS VIR_FROM_NONE =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 */ - - -/** - * virNetDevTapReserveName: - * @name: name of an existing tap device - * - * Set the value of virNetDevTapLastID to assure that any new tap - * device created with an autogenerated name will use a number higher - * than the number in the given tap device name. - * - * Returns nothing. - */ -void -virNetDevTapReserveName(const char *name) -{ - unsigned int id; - const char *idstr =3D NULL; - - - if (STRPREFIX(name, VIR_NET_GENERATED_VNET_PREFIX)) { - - VIR_INFO("marking device in use: '%s'", name); - - idstr =3D name + strlen(VIR_NET_GENERATED_VNET_PREFIX); - - if (virStrToLong_ui(idstr, NULL, 10, &id) >=3D 0) { - virMutexLock(&virNetDevTapCreateMutex); - - if (virNetDevTapLastID < (int)id) - virNetDevTapLastID =3D id; - - virMutexUnlock(&virNetDevTapCreateMutex); - } - } -} - - /** * virNetDevTapGetName: * @tapfd: a tun/tap file descriptor @@ -183,55 +143,6 @@ virNetDevTapGetRealDeviceName(char *ifname G_GNUC_UNUS= ED) =20 =20 #ifdef TUNSETIFF -/** - * virNetDevTapGenerateName: - * @ifname: pointer to pointer to string containing template - * - * generate a new (currently unused) name for a new tap device based - * on the templace string in @ifname - replace %d with - * ++virNetDevTapLastID, 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. - */ -static int -virNetDevTapGenerateName(char **ifname) -{ - int id; - double maxIDd =3D pow(10, IFNAMSIZ - 1 - strlen(VIR_NET_GENERATED_VNET= _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_VNET_PREFIX); - return -1; -} - - /** * virNetDevTapCreate: * @ifname: the interface name @@ -263,16 +174,14 @@ int virNetDevTapCreate(char **ifname, int ret =3D -1; int fd =3D -1; =20 - virMutexLock(&virNetDevTapCreateMutex); - /* 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 * immediately re-using names that have just been released, which * can lead to race conditions). - */ - if (STREQ(*ifname, VIR_NET_GENERATED_VNET_PREFIX "%d") && - virNetDevTapGenerateName(ifname) < 0) { - goto cleanup; + * if ifname is just a user-provided name, virNetDevGenerateName + * leaves it unchanged. */ + if (virNetDevGenerateName(ifname, VIR_NET_DEV_GEN_NAME_VNET) < 0) { + return -1; } =20 if (!tunpath) @@ -333,7 +242,6 @@ int virNetDevTapCreate(char **ifname, ret =3D 0; =20 cleanup: - virMutexUnlock(&virNetDevTapCreateMutex); if (ret < 0) { VIR_FORCE_CLOSE(fd); while (i--) diff --git a/src/util/virnetdevtap.h b/src/util/virnetdevtap.h index dea8aec3..c6bd9285 100644 --- a/src/util/virnetdevtap.h +++ b/src/util/virnetdevtap.h @@ -29,10 +29,6 @@ # define VIR_NETDEV_TAP_REQUIRE_MANUAL_CLEANUP 1 #endif =20 -void -virNetDevTapReserveName(const char *name) - ATTRIBUTE_NONNULL(1); - int virNetDevTapCreate(char **ifname, const char *tunpath, int *tapfd, --=20 2.25.1