From nobody Mon Feb 9 21:20:00 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=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