From nobody Mon Feb 9 09:09:34 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=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