From nobody Tue May 14 01:08:01 2024 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=1607910701; cv=none; d=zohomail.com; s=zohoarc; b=GLxU1gy74E87b0a67zUD70EswIKyqunIPdRqLw8HKR9ANLZLCRi8BOTfGMrTQ4yJ9qHU7EGpKpQGcnOntBVJ9Tk9gSNXQ1bawSOb4HJEUN1LPP+LI/D0FTsDtBjYym6tq8txBwO3NID2CrCmVjJSoB+Cak3ERCycPoIj68s6WXg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1607910701; 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=abdka8mKyzpDu4DP7R5osMJdO2DXqgY1zb2oSetxL98=; b=bF2f3H+IUFl1ig5NoeBBYBn4jyX2wxO3K7bQbTL4w4gKCsppSPcKwxEI5WnvUEZAFk3P/qPo+r2pSHIMYjen0lQul2pQ+0DiOQ2AOGy8xrHVN2U2kSr0f44PhsVMDfkWzyU5VVfsLQD1O383Gtbks0U30HtzxylwUO33OPK7WWU= 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 1607910701002481.57884271784803; Sun, 13 Dec 2020 17:51:41 -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-416-W27N8DCQOKG3ASx0Ku1XPA-1; Sun, 13 Dec 2020 20:51:37 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D5446107ACF8; Mon, 14 Dec 2020 01:51:32 +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 B539870478; Mon, 14 Dec 2020 01:51: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 815315002D; Mon, 14 Dec 2020 01:51:32 +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 0BE1pKN2016821 for ; Sun, 13 Dec 2020 20:51:20 -0500 Received: by smtp.corp.redhat.com (Postfix) id 6B1822026D13; Mon, 14 Dec 2020 01:51:20 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast01.extmail.prod.ext.rdu2.redhat.com [10.11.55.17]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 65D252026D48 for ; Mon, 14 Dec 2020 01:51:17 +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 E00C1858280 for ; Mon, 14 Dec 2020 01:51:16 +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-558-P681aDN3PwiOhMFjcfCLNA-1; Sun, 13 Dec 2020 20:51:11 -0500 Received: from localhost.localdomain (unknown [113.128.197.238]) by smtp2 (Coremail) with SMTP id iOCowACHH+sIxdZfXRTKCg--.255S3; Mon, 14 Dec 2020 09:51:05 +0800 (CST) X-MC-Unique: W27N8DCQOKG3ASx0Ku1XPA-1 X-MC-Unique: P681aDN3PwiOhMFjcfCLNA-1 From: Shi Lei To: libvir-list@redhat.com Subject: [PATCHv3 1/5] netdev: Introduce several helper functions for generating unique netdev name Date: Mon, 14 Dec 2020 09:50:32 +0800 Message-Id: <20201214015036.51607-2-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--.255S3 X-Coremail-Antispam: 1Uf129KBjvJXoW3ur17GrWrCr4UtrWfuw4rXwb_yoWktr4DpF n5GasIgr1xJrs2kFyxZF1UuF13AryUWF93KF9093sruw48WryxXr4xKa4ftw45AFZaq34f AFWFva4kWa48GrJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0J1b9miUUUUU= X-Originating-IP: [113.128.197.238] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiART6T1ikUhtiGwAAss 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.11 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. Signed-off-by: Shi Lei Reviewed-by: Daniel Henrique Barboza Reviewed-by: Laine Stump --- src/bhyve/bhyve_command.c | 4 +- src/conf/domain_conf.c | 4 +- src/interface/interface_backend_udev.c | 2 +- src/libvirt_private.syms | 2 + src/qemu/qemu_interface.c | 8 +- src/util/virnetdev.c | 116 +++++++++++++++++++++++++ src/util/virnetdev.h | 27 +++++- src/util/virnetdevtap.c | 10 +-- 8 files changed, 158 insertions(+), 15 deletions(-) diff --git a/src/bhyve/bhyve_command.c b/src/bhyve/bhyve_command.c index acf3a5a4..4cf98c0e 100644 --- a/src/bhyve/bhyve_command.c +++ b/src/bhyve/bhyve_command.c @@ -80,10 +80,10 @@ bhyveBuildNetArgStr(const virDomainDef *def, } =20 if (!net->ifname || - STRPREFIX(net->ifname, VIR_NET_GENERATED_TAP_PREFIX) || + STRPREFIX(net->ifname, VIR_NET_GENERATED_VNET_PREFIX) || strchr(net->ifname, '%')) { VIR_FREE(net->ifname); - net->ifname =3D g_strdup(VIR_NET_GENERATED_TAP_PREFIX "%d"); + net->ifname =3D g_strdup(VIR_NET_GENERATED_VNET_PREFIX "%d"); } =20 if (!dryRun) { diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 23415b32..403ecab8 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -12037,7 +12037,7 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlop= t, =20 if (def->managed_tap !=3D VIR_TRISTATE_BOOL_NO && ifname && (flags & VIR_DOMAIN_DEF_PARSE_INACTIVE) && - (STRPREFIX(ifname, VIR_NET_GENERATED_TAP_PREFIX) || + (STRPREFIX(ifname, VIR_NET_GENERATED_VNET_PREFIX) || STRPREFIX(ifname, VIR_NET_GENERATED_MACVTAP_PREFIX) || STRPREFIX(ifname, VIR_NET_GENERATED_MACVLAN_PREFIX) || (prefix && STRPREFIX(ifname, prefix)))) { @@ -26460,7 +26460,7 @@ virDomainNetDefFormat(virBufferPtr buf, if (def->ifname && (def->managed_tap =3D=3D VIR_TRISTATE_BOOL_NO || !((flags & VIR_DOMAIN_DEF_FORMAT_INACTIVE) && - (STRPREFIX(def->ifname, VIR_NET_GENERATED_TAP_PREFIX) || + (STRPREFIX(def->ifname, VIR_NET_GENERATED_VNET_PREFIX) || STRPREFIX(def->ifname, VIR_NET_GENERATED_MACVTAP_PREFIX) || STRPREFIX(def->ifname, VIR_NET_GENERATED_MACVLAN_PREFIX) || (prefix && STRPREFIX(def->ifname, prefix)))))) { diff --git a/src/interface/interface_backend_udev.c b/src/interface/interfa= ce_backend_udev.c index 173c4fc3..6a94a450 100644 --- a/src/interface/interface_backend_udev.c +++ b/src/interface/interface_backend_udev.c @@ -544,7 +544,7 @@ udevBridgeScanDirFilter(const struct dirent *entry) * vnet%d. Improvements to this check are welcome. */ if (strlen(entry->d_name) >=3D 5) { - if (STRPREFIX(entry->d_name, VIR_NET_GENERATED_TAP_PREFIX) && + if (STRPREFIX(entry->d_name, VIR_NET_GENERATED_VNET_PREFIX) && g_ascii_isdigit(entry->d_name[4])) return 0; } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 992488f7..c0f50856 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2551,6 +2551,7 @@ virNetDevDelMulti; virNetDevExists; virNetDevFeatureTypeFromString; virNetDevFeatureTypeToString; +virNetDevGenerateName; virNetDevGetFeatures; virNetDevGetIndex; virNetDevGetLinkInfo; @@ -2574,6 +2575,7 @@ virNetDevIfStateTypeToString; virNetDevIsVirtualFunction; virNetDevPFGetVF; virNetDevReadNetConfig; +virNetDevReserveName; virNetDevRunEthernetScript; virNetDevRxFilterFree; virNetDevRxFilterModeTypeFromString; diff --git a/src/qemu/qemu_interface.c b/src/qemu/qemu_interface.c index 32b397d2..197c0aa2 100644 --- a/src/qemu/qemu_interface.c +++ b/src/qemu/qemu_interface.c @@ -456,10 +456,10 @@ qemuInterfaceEthernetConnect(virDomainDefPtr def, } } else { if (!net->ifname || - STRPREFIX(net->ifname, VIR_NET_GENERATED_TAP_PREFIX) || + STRPREFIX(net->ifname, VIR_NET_GENERATED_VNET_PREFIX) || strchr(net->ifname, '%')) { VIR_FREE(net->ifname); - net->ifname =3D g_strdup(VIR_NET_GENERATED_TAP_PREFIX "%d"); + net->ifname =3D g_strdup(VIR_NET_GENERATED_VNET_PREFIX "%d"); /* avoid exposing vnet%d in getXMLDesc or error outputs */ template_ifname =3D true; } @@ -560,10 +560,10 @@ qemuInterfaceBridgeConnect(virDomainDefPtr def, } =20 if (!net->ifname || - STRPREFIX(net->ifname, VIR_NET_GENERATED_TAP_PREFIX) || + STRPREFIX(net->ifname, VIR_NET_GENERATED_VNET_PREFIX) || strchr(net->ifname, '%')) { VIR_FREE(net->ifname); - net->ifname =3D g_strdup(VIR_NET_GENERATED_TAP_PREFIX "%d"); + net->ifname =3D g_strdup(VIR_NET_GENERATED_VNET_PREFIX "%d"); /* avoid exposing vnet%d in getXMLDesc or error outputs */ template_ifname =3D true; } diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c index 5104bbe7..bd1ca1d8 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,14 @@ VIR_LOG_INIT("util.netdev"); (FEATURE_WORD(blocks, index, field) & FEATURE_FIELD_FLAG(index)) #endif =20 + +static virNetDevGenName +virNetDevGenNames[VIR_NET_DEV_GEN_NAME_LAST] =3D { + {-1, VIR_NET_GENERATED_VNET_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 +3525,110 @@ virNetDevSetRootQDisc(const char *ifname, =20 return 0; } + + +/** + * virNetDevReserveName: + * @name: name of an existing network device + * + * 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) +{ + unsigned int id; + const char *idstr =3D NULL; + virNetDevGenNameType type; + + if (STRPREFIX(name, VIR_NET_GENERATED_VNET_PREFIX)) + type =3D VIR_NET_DEV_GEN_NAME_VNET; + else if (STRPREFIX(name, VIR_NET_GENERATED_MACVTAP_PREFIX)) + type =3D VIR_NET_DEV_GEN_NAME_MACVTAP; + else if (STRPREFIX(name, VIR_NET_GENERATED_MACVLAN_PREFIX)) + type =3D VIR_NET_DEV_GEN_NAME_MACVLAN; + else + return; + + VIR_INFO("marking device in use: '%s'", name); + + idstr =3D name + strlen(virNetDevGenNames[type].prefix); + + if (virStrToLong_ui(idstr, NULL, 10, &id) >=3D 0) { + virMutexLock(&virNetDevGenNames[type].mutex); + + if (virNetDevGenNames[type].lastID < (int)id) + virNetDevGenNames[type].lastID =3D id; + + virMutexUnlock(&virNetDevGenNames[type].mutex); + } +} + + +/** + * virNetDevGenerateName: + * @ifname: pointer to pointer to string which can be a template, + * NULL or user-provided name. + * @type: type of the network device + * + * generate a new (currently unused) name for a new network device based + * on @ifname. If string pointed by @ifname is a template, replace %d + * with the reserved id; if that string is NULL, just generate a new + * name. 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. + * + * Note: if string pointed by @ifname is NOT a template or NULL, leave + * it unchanged and return it directly. + * + * 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; + + /* The @ifname is not a template, leave it unchanged. */ + if (*ifname && strstr(*ifname, "%d") =3D=3D NULL) + return 0; + + if (maxIDd <=3D (double)INT_MAX) + maxID =3D (int)maxIDd; + + do { + g_autofree char *try =3D NULL; + + virMutexLock(&virNetDevGenNames[type].mutex); + + id =3D ++virNetDevGenNames[type].lastID; + + /* reset before overflow */ + if (virNetDevGenNames[type].lastID >=3D maxID) + virNetDevGenNames[type].lastID =3D -1; + + virMutexUnlock(&virNetDevGenNames[type].mutex); + + 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; +} diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h index 53e606c6..f0160127 100644 --- a/src/util/virnetdev.h +++ b/src/util/virnetdev.h @@ -38,7 +38,13 @@ typedef void virIfreq; /* Used for prefix of ifname of any tap device name generated * dynamically by libvirt, cannot be used for a persistent network name. */ -#define VIR_NET_GENERATED_TAP_PREFIX "vnet" +#define VIR_NET_GENERATED_VNET_PREFIX "vnet" + +/* 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" =20 typedef enum { VIR_NETDEV_RX_FILTER_MODE_NONE =3D 0, @@ -145,6 +151,21 @@ struct _virNetDevCoalesce { uint32_t rate_sample_interval; }; =20 +typedef enum { + VIR_NET_DEV_GEN_NAME_VNET, + VIR_NET_DEV_GEN_NAME_MACVTAP, + VIR_NET_DEV_GEN_NAME_MACVLAN, + VIR_NET_DEV_GEN_NAME_LAST +} 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 +342,7 @@ int virNetDevVFInterfaceStats(virPCIDeviceAddressPtr vf= Addr, ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); =20 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetDevRxFilter, virNetDevRxFilterFree); + +void virNetDevReserveName(const char *name); + +int virNetDevGenerateName(char **ifname, virNetDevGenNameType type); diff --git a/src/util/virnetdevtap.c b/src/util/virnetdevtap.c index 198607b5..9354cc10 100644 --- a/src/util/virnetdevtap.c +++ b/src/util/virnetdevtap.c @@ -76,11 +76,11 @@ virNetDevTapReserveName(const char *name) const char *idstr =3D NULL; =20 =20 - if (STRPREFIX(name, VIR_NET_GENERATED_TAP_PREFIX)) { + if (STRPREFIX(name, VIR_NET_GENERATED_VNET_PREFIX)) { =20 VIR_INFO("marking device in use: '%s'", name); =20 - idstr =3D name + strlen(VIR_NET_GENERATED_TAP_PREFIX); + idstr =3D name + strlen(VIR_NET_GENERATED_VNET_PREFIX); =20 if (virStrToLong_ui(idstr, NULL, 10, &id) >=3D 0) { virMutexLock(&virNetDevTapCreateMutex); @@ -200,7 +200,7 @@ static int virNetDevTapGenerateName(char **ifname) { int id; - double maxIDd =3D pow(10, IFNAMSIZ - 1 - strlen(VIR_NET_GENERATED_TAP_= PREFIX)); + double maxIDd =3D pow(10, IFNAMSIZ - 1 - strlen(VIR_NET_GENERATED_VNET= _PREFIX)); int maxID =3D INT_MAX; int attempts =3D 0; =20 @@ -227,7 +227,7 @@ virNetDevTapGenerateName(char **ifname) =20 virReportError(VIR_ERR_INTERNAL_ERROR, _("no unused %s names available"), - VIR_NET_GENERATED_TAP_PREFIX); + VIR_NET_GENERATED_VNET_PREFIX); return -1; } =20 @@ -270,7 +270,7 @@ int virNetDevTapCreate(char **ifname, * immediately re-using names that have just been released, which * can lead to race conditions). */ - if (STREQ(*ifname, VIR_NET_GENERATED_TAP_PREFIX "%d") && + if (STREQ(*ifname, VIR_NET_GENERATED_VNET_PREFIX "%d") && virNetDevTapGenerateName(ifname) < 0) { goto cleanup; } --=20 2.25.1 From nobody Tue May 14 01:08:01 2024 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 Reviewed-by: Daniel Henrique Barboza Reviewed-by: Laine Stump --- 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 From nobody Tue May 14 01:08:01 2024 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=1607910692; cv=none; d=zohomail.com; s=zohoarc; b=Xc1jBC/FiGBlvLgNsZhz3JZbEVXgReppa6l2Odz1nvpHoue923ICoStb9Ng/Lh6dyFPg98s03k1afgpgpCcafTWEp1N4jVCwSmy2At8Ba53H2J9sNpeqPwHb7YfNF0xfI+G3GL2e0Y+ksyQXEYC08FcN8QdWDROcBBDDpT4d+y4= 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=BhI+TmRx3/ce4fy8gCYbcdifbBS84HG6iYA+SETA1tg=; b=n1xayE4xCGEIspTIN/ptksbkADYywAue60dUSBh+kpIxM6IR/KVyzusNJemRWi1EOuS3phv21lDqzrfMez2HakuL/wua80wT5aM+5UZhmyaN8uWVhT6Q+yB33GRNkDGd0aAkagWyciGLBKPCz8nhmrkv53kwW0UxLaYBay1FvMk= 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 1607910692037321.22595885004864; Sun, 13 Dec 2020 17:51:32 -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-585-_Q8YwBUTMdWu-iu_erTyIQ-1; Sun, 13 Dec 2020 20:51:28 -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 CC4D51005D44; Mon, 14 Dec 2020 01:51:22 +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 44407709AB; Mon, 14 Dec 2020 01:51:22 +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 085DE180954D; Mon, 14 Dec 2020 01:51:19 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 0BE1pGcR016794 for ; Sun, 13 Dec 2020 20:51:17 -0500 Received: by smtp.corp.redhat.com (Postfix) id BF45944007; Mon, 14 Dec 2020 01:51:16 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast01.extmail.prod.ext.rdu2.redhat.com [10.11.55.17]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B94D244006 for ; Mon, 14 Dec 2020 01:51:14 +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 60884858280 for ; Mon, 14 Dec 2020 01:51:14 +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-117-lQ_2TzfTP4KYRK70h-_PNg-1; Sun, 13 Dec 2020 20:51:11 -0500 Received: from localhost.localdomain (unknown [113.128.197.238]) by smtp2 (Coremail) with SMTP id iOCowACHH+sIxdZfXRTKCg--.255S5; Mon, 14 Dec 2020 09:51:06 +0800 (CST) X-MC-Unique: _Q8YwBUTMdWu-iu_erTyIQ-1 X-MC-Unique: lQ_2TzfTP4KYRK70h-_PNg-1 From: Shi Lei To: libvir-list@redhat.com Subject: [PATCHv3 3/5] netdevmacvlan: Use helper function to create unique macvlan/macvtap name Date: Mon, 14 Dec 2020 09:50:34 +0800 Message-Id: <20201214015036.51607-4-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--.255S5 X-Coremail-Antispam: 1Uf129KBjvAXoWfJr45Zr1kuw48CF1xKrWUXFb_yoW8JryDWo W8Wr4av3y0kr1UKa13Ga47tFy5WFyjyryI9r45XFZ8Cwn3XrZ8CF18CanxCas8Xr15Wr47 GF17Was5KFsrXr4fn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjfUJfWrDUUUU X-Originating-IP: [113.128.197.238] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiART6T1ikUhtiIAAAsX 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.79 on 10.11.54.5 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.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" Simplify ReserveName/GenerateName for macvlan and macvtap by using common functions. Signed-off-by: Shi Lei Reviewed-by: Daniel Henrique Barboza Reviewed-by: Laine Stump --- src/libvirt_private.syms | 1 - src/libxl/libxl_driver.c | 2 +- src/lxc/lxc_process.c | 2 +- src/qemu/qemu_process.c | 2 +- src/util/virnetdevmacvlan.c | 177 +++++------------------------------- src/util/virnetdevmacvlan.h | 14 +-- 6 files changed, 27 insertions(+), 171 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 64ef01e1..4d6ae84b 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2655,7 +2655,6 @@ virNetDevMacVLanDelete; virNetDevMacVLanDeleteWithVPortProfile; virNetDevMacVLanIsMacvtap; virNetDevMacVLanModeTypeFromString; -virNetDevMacVLanReserveName; virNetDevMacVLanRestartWithVPortProfile; virNetDevMacVLanTapOpen; virNetDevMacVLanTapSetup; diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index 6af274cb..3488d7ec 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -364,7 +364,7 @@ libxlReconnectNotifyNets(virDomainDefPtr def) * impolite. */ if (virDomainNetGetActualType(net) =3D=3D VIR_DOMAIN_NET_TYPE_DIRE= CT) - virNetDevMacVLanReserveName(net->ifname); + virNetDevReserveName(net->ifname); =20 if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_NETWORK) { if (!conn && !(conn =3D virGetConnectNetwork())) diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index 0f818e2e..eb29431e 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -1641,7 +1641,7 @@ virLXCProcessReconnectNotifyNets(virDomainDefPtr def) * impolite. */ if (virDomainNetGetActualType(net) =3D=3D VIR_DOMAIN_NET_TYPE_DIRE= CT) - virNetDevMacVLanReserveName(net->ifname); + virNetDevReserveName(net->ifname); =20 if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_NETWORK) { if (!conn && !(conn =3D virGetConnectNetwork())) diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index 1d54f201..6244ade4 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -3390,7 +3390,7 @@ qemuProcessNotifyNets(virDomainDefPtr def) */ switch (virDomainNetGetActualType(net)) { case VIR_DOMAIN_NET_TYPE_DIRECT: - virNetDevMacVLanReserveName(net->ifname); + virNetDevReserveName(net->ifname); break; case VIR_DOMAIN_NET_TYPE_BRIDGE: case VIR_DOMAIN_NET_TYPE_NETWORK: diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c index 72f0d670..36b13133 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" @@ -59,129 +58,6 @@ VIR_ENUM_IMPL(virNetDevMacVLanMode, =20 VIR_LOG_INIT("util.netdevmacvlan"); =20 -# define VIR_NET_GENERATED_MACVTAP_PATTERN VIR_NET_GENERATED_MACVTAP_PREFI= X "%d" -# define VIR_NET_GENERATED_MACVLAN_PATTERN VIR_NET_GENERATED_MACVLAN_PREFI= X "%d" -# define VIR_NET_GENERATED_PREFIX \ - ((flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ? \ - VIR_NET_GENERATED_MACVTAP_PREFIX : VIR_NET_GENERATED_MACVLAN_PREFIX) - - -virMutex virNetDevMacVLanCreateMutex =3D VIR_MUTEX_INITIALIZER; -static int virNetDevMacVTapLastID =3D -1; -static int virNetDevMacVLanLastID =3D -1; - - -static void -virNetDevMacVLanReserveNameInternal(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; - } -} - - -/** - * virNetDevMacVLanReserveName: - * @name: name of an existing macvtap/macvlan device - * - * Set the value of virNetDevMacV(Lan|Tap)LastID to assure that any - * new device created with an autogenerated name will use a number - * higher than the number in the given device name. - * - * Returns nothing. - */ -void -virNetDevMacVLanReserveName(const char *name) -{ - virMutexLock(&virNetDevMacVLanCreateMutex); - virNetDevMacVLanReserveNameInternal(name); - virMutexUnlock(&virNetDevMacVLanCreateMutex); -} - - -/** - * virNetDevMacVLanGenerateName: - * @ifname: pointer to pointer to string containing template - * @lastID: counter to add to the template to form the name - * - * generate a new (currently unused) name for a new macvtap/macvlan - * device based on the template string in @ifname - replace %d with - * ++(*counter), 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 -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; -} - =20 /** * virNetDevMacVLanIsMacvtap: @@ -209,13 +85,10 @@ virNetDevMacVLanIsMacvtap(const char *ifname) * virNetDevMacVLanCreate: * * @ifname: The name the interface is supposed to have; optional parameter - * @type: The type of device, i.e., "macvtap", "macvlan" * @macaddress: The MAC address of the device * @srcdev: The name of the 'link' device * @macvlan_mode: The macvlan mode to use - * @retry: Pointer to integer that will be '1' upon return if an interface - * with the same name already exists and it is worth to try - * again with a different name + * @flags: OR of virNetDevMacVLanCreateFlags. * * Create a macvtap device with the given properties. * @@ -223,19 +96,21 @@ virNetDevMacVLanIsMacvtap(const char *ifname) */ int virNetDevMacVLanCreate(const char *ifname, - const char *type, const virMacAddr *macaddress, const char *srcdev, - uint32_t macvlan_mode) + uint32_t macvlan_mode, + unsigned int flags) { int error =3D 0; int ifindex =3D 0; + const char *type =3D (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ? + VIR_NET_GENERATED_MACVTAP_PREFIX : + VIR_NET_GENERATED_MACVLAN_PREFIX; virNetlinkNewLinkData data =3D { .macvlan_mode =3D &macvlan_mode, .mac =3D macaddress, }; =20 - if (virNetDevGetIndex(srcdev, &ifindex) < 0) return -1; =20 @@ -795,7 +670,6 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifna= meRequested, size_t tapfdSize, unsigned int flags) { - const char *type =3D VIR_NET_GENERATED_PREFIX; g_autofree char *ifname =3D NULL; uint32_t macvtapMode; int vf =3D -1; @@ -832,8 +706,6 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifna= meRequested, return -1; } =20 - virMutexLock(&virNetDevMacVLanCreateMutex); - if (ifnameRequested) { int rc; bool isAutoName @@ -842,10 +714,8 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifn= ameRequested, =20 VIR_INFO("Requested macvtap device name: %s", ifnameRequested); =20 - if ((rc =3D virNetDevExists(ifnameRequested)) < 0) { - virMutexUnlock(&virNetDevMacVLanCreateMutex); + if ((rc =3D virNetDevExists(ifnameRequested)) < 0) return -1; - } =20 if (rc) { /* ifnameRequested is already being used */ @@ -854,17 +724,16 @@ virNetDevMacVLanCreateWithVPortProfile(const char *if= nameRequested, virReportSystemError(EEXIST, _("Unable to create device '%s'"), ifnameRequested); - virMutexUnlock(&virNetDevMacVLanCreateMutex); return -1; } } else { =20 /* ifnameRequested is available. try to open it */ =20 - virNetDevMacVLanReserveNameInternal(ifnameRequested); + virNetDevReserveName(ifnameRequested); =20 - if (virNetDevMacVLanCreate(ifnameRequested, type, macaddress, - linkdev, macvtapMode) =3D=3D 0) { + if (virNetDevMacVLanCreate(ifnameRequested, macaddress, + linkdev, macvtapMode, flags) =3D=3D= 0) { =20 /* virNetDevMacVLanCreate() was successful - use this name= */ ifname =3D g_strdup(ifnameRequested); @@ -874,7 +743,6 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifna= meRequested, * autogenerated named, so there is nothing else to * try - fail and return. */ - virMutexUnlock(&virNetDevMacVLanCreateMutex); return -1; } } @@ -885,16 +753,19 @@ virNetDevMacVLanCreateWithVPortProfile(const char *if= nameRequested, * autogenerated name, so now we look for an unused * autogenerated name. */ - if (virNetDevMacVLanGenerateName(&ifname, flags) < 0 || - virNetDevMacVLanCreate(ifname, type, macaddress, - linkdev, macvtapMode) < 0) { - virMutexUnlock(&virNetDevMacVLanCreateMutex); + virNetDevGenNameType type; + if (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) + type =3D VIR_NET_DEV_GEN_NAME_MACVTAP; + else + type =3D VIR_NET_DEV_GEN_NAME_MACVLAN; + + if (virNetDevGenerateName(&ifname, type) < 0 || + virNetDevMacVLanCreate(ifname, macaddress, + linkdev, macvtapMode, flags) < 0) return -1; - } } =20 /* all done creating the device */ - virMutexUnlock(&virNetDevMacVLanCreateMutex); =20 if (virNetDevVPortProfileAssociate(ifname, virtPortProfile, @@ -1050,10 +921,10 @@ bool virNetDevMacVLanIsMacvtap(const char *ifname G_= GNUC_UNUSED) } =20 int virNetDevMacVLanCreate(const char *ifname G_GNUC_UNUSED, - const char *type G_GNUC_UNUSED, const virMacAddr *macaddress G_GNUC_UNUSED, const char *srcdev G_GNUC_UNUSED, - uint32_t macvlan_mode G_GNUC_UNUSED) + uint32_t macvlan_mode G_GNUC_UNUSED, + unsigned int fflags G_GNUC_UNUSED) { virReportSystemError(ENOSYS, "%s", _("Cannot create macvlan devices on this platform= ")); @@ -1141,10 +1012,4 @@ int virNetDevMacVLanVPortProfileRegisterCallback(con= st char *ifname G_GNUC_UNUSE _("Cannot create macvlan devices on this platform= ")); return -1; } - -void virNetDevMacVLanReserveName(const char *name G_GNUC_UNUSED) -{ - virReportSystemError(ENOSYS, "%s", - _("Cannot create macvlan devices on this platform= ")); -} #endif /* ! WITH_LIBNL */ diff --git a/src/util/virnetdevmacvlan.h b/src/util/virnetdevmacvlan.h index 48800a8f..0a013fc2 100644 --- a/src/util/virnetdevmacvlan.h +++ b/src/util/virnetdevmacvlan.h @@ -48,23 +48,15 @@ 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); - bool virNetDevMacVLanIsMacvtap(const char *ifname) ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT G_GNUC_NO_INLINE; =20 int virNetDevMacVLanCreate(const char *ifname, - const char *type, const virMacAddr *macaddress, const char *srcdev, - uint32_t macvlan_mode) - ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4) + uint32_t macvlan_mode, + unsigned int flags) + ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) G_GNUC_WARN_UNUSED_RESULT; =20 int virNetDevMacVLanDelete(const char *ifname) --=20 2.25.1 From nobody Tue May 14 01:08:01 2024 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=1607910692; cv=none; d=zohomail.com; s=zohoarc; b=XVpqggA0RViFbtxPvlbkD5ILO7LArOJXEI1J4ps3cPmPiZp38VfA/9ddYl1iSunxKPvmV6w2TxudQFm4OUco0/GKjxR1vimm5FrRD/jGW24v6Nv3YiRGv9TfRr9RYGKZR+zPIc2yTKDUS+1fcR7+Uu5lYJujVVPXmzJuhPlMraI= 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=Wakix8WspEvTZy1S/KwRzE8M6jzyy+xAjPPMyERzXs0=; b=eQ+jrHj2fO85X2uY3Dwlka2qoXtW54jaU3THcaYPHA7qNNpIJSvq4fVyyugPtNh1wNvNVlq5UPSuw7br2j6nmMzBTTNzFucDJK98XKziLb3hhFuj2fqFmkrIpL3Zak1YsYnf5vl+9uxufgINziKk50DrQ5FWXAO/8JzpffPgfFQ= 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 1607910692307732.555544254926; Sun, 13 Dec 2020 17:51:32 -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-355-noxSbs_jOtGy0tBH1wG5AA-1; Sun, 13 Dec 2020 20:51:28 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DC1C2180A093; Mon, 14 Dec 2020 01:51:22 +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 A1E357046E; Mon, 14 Dec 2020 01:51:22 +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 45A664BB7B; Mon, 14 Dec 2020 01:51:21 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 0BE1pK6P016813 for ; Sun, 13 Dec 2020 20:51:20 -0500 Received: by smtp.corp.redhat.com (Postfix) id 0A868112D175; Mon, 14 Dec 2020 01:51:20 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast01.extmail.prod.ext.rdu2.redhat.com [10.11.55.17]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 05FA2112D169 for ; Mon, 14 Dec 2020 01:51:16 +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 3D24C858EEC for ; Mon, 14 Dec 2020 01:51:16 +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-63-tvI6x8uYP7eBU-povTnQ9Q-1; Sun, 13 Dec 2020 20:51:11 -0500 Received: from localhost.localdomain (unknown [113.128.197.238]) by smtp2 (Coremail) with SMTP id iOCowACHH+sIxdZfXRTKCg--.255S6; Mon, 14 Dec 2020 09:51:06 +0800 (CST) X-MC-Unique: noxSbs_jOtGy0tBH1wG5AA-1 X-MC-Unique: tvI6x8uYP7eBU-povTnQ9Q-1 From: Shi Lei To: libvir-list@redhat.com Subject: [PATCHv3 4/5] netdevveth: Simplify virNetDevVethCreate by using virNetDevGenerateName Date: Mon, 14 Dec 2020 09:50:35 +0800 Message-Id: <20201214015036.51607-5-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--.255S6 X-Coremail-Antispam: 1Uf129KBjvJXoWxXw1fXrW7WFWfKr1DAr4Utwb_yoW7Jr1kpw 4SyF9Yyr48Xw4DAayUXFy8ZFn5Crs8Kry3KasrZ397Z3y5Zr45Wr1xCFy8trW7JFWrAFyS 9FyYvryDuay7GF7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0J1jWrXUUUUU= X-Originating-IP: [113.128.197.238] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiWhX6T1f4qC+ryAAAsx 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.3 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.11 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 Reviewed-by: Daniel Henrique Barboza Reviewed-by: Laine Stump --- src/lxc/lxc_process.c | 3 + src/util/virnetdevveth.c | 140 +++++++++------------------------------ 2 files changed, 36 insertions(+), 107 deletions(-) diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index eb29431e..2e8ae706 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -307,6 +307,9 @@ virLXCProcessSetupInterfaceTap(virDomainDefPtr vm, =20 VIR_DEBUG("calling vethCreate()"); parentVeth =3D net->ifname; + if (parentVeth) + virNetDevReserveName(parentVeth); + 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..d6932a2e 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,45 @@ 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; - } + if (!*veth1) { + if (virNetDevGenerateName(&veth1auto, VIR_NET_DEV_GEN_NAME_VNET) <= 0) + return -1; + } + if (!*veth2) { + if (virNetDevGenerateName(&veth2auto, VIR_NET_DEV_GEN_NAME_VNET) <= 0) + return -1; + } =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; - } + cmd =3D virCommandNew("ip"); + virCommandAddArgList(cmd, "link", "add", + *veth1 ? *veth1 : veth1auto, + "type", "veth", "peer", "name", + *veth2 ? *veth2 : veth2auto, + NULL); =20 - VIR_DEBUG("Failed to create veth host: %s guest: %s: %d", - *veth1 ? *veth1 : veth1auto, - *veth2 ? *veth2 : veth2auto, - status); + 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 From nobody Tue May 14 01:08:01 2024 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=1607910702; cv=none; d=zohomail.com; s=zohoarc; b=ZNOcGQesIYMgufacnef6bQAwuAUi9gPoDnlpMFBTo5hjmW/8Eyz8LeoRh31OSPuT+Ez1TYTVDmN28ODBZEnsB+1nEwDh09M0BeX9gMnX7efleHdXYObPxOo04uNCHWWEBkrtb48F7x5YFwi6UhxzEvZbw4IBIOlK36iT0uJw9gs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1607910702; 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=SorZzYbfcJO8LUzBTwODm03sDVvExKMKN61fTlv/23g=; b=mfg3x5YL0zJOhIA+bDhhwTudY7HaY0llaGOG8AdshZpI1jbaZumWF2SWTMppDgpnRlDX3l8xQRtFy2+SubfiKIo8rVAtqvyQGD/A8B9zMV+uOvWgHiKElOkHdflXuVUZmhWX+AZWTeHQhfBt4cfV0UluSAf2er57ElS5HAPfbYg= 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 1607910702966137.66242353766768; Sun, 13 Dec 2020 17:51:42 -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-478-veXNHWSNPaW4poWdZHvOrA-1; Sun, 13 Dec 2020 20:51:39 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 69642107ACE3; Mon, 14 Dec 2020 01:51:34 +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 476DD5D9DC; Mon, 14 Dec 2020 01:51:34 +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 1197F1809CA0; Mon, 14 Dec 2020 01:51:34 +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 0BE1pLxx016830 for ; Sun, 13 Dec 2020 20:51:21 -0500 Received: by smtp.corp.redhat.com (Postfix) id 1DDE72166B2C; Mon, 14 Dec 2020 01:51:21 +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 186C42166B2A for ; Mon, 14 Dec 2020 01:51:19 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-1.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 09C44811E76 for ; Mon, 14 Dec 2020 01:51:19 +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-111-02F4OmaYP-qZYpUNSqF5ag-1; Sun, 13 Dec 2020 20:51:14 -0500 Received: from localhost.localdomain (unknown [113.128.197.238]) by smtp2 (Coremail) with SMTP id iOCowACHH+sIxdZfXRTKCg--.255S7; Mon, 14 Dec 2020 09:51:06 +0800 (CST) X-MC-Unique: veXNHWSNPaW4poWdZHvOrA-1 X-MC-Unique: 02F4OmaYP-qZYpUNSqF5ag-1 From: Shi Lei To: libvir-list@redhat.com Subject: [PATCHv3 5/5] netdev: Prevent functions that call virNetDevTapCreate from adding 'vnet%d' into ifname Date: Mon, 14 Dec 2020 09:50:36 +0800 Message-Id: <20201214015036.51607-6-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--.255S7 X-Coremail-Antispam: 1Uf129KBjvJXoWxGw1DuFWxJFy7ZrWUAw45GFg_yoW5CrykpF 4rG3sFgw4ktr18CrZ7A3WUuF13J34qgF9FkFZ0vw4vv3WxWFWfZr4xKa4Sv3y5JFZ3Xw17 uayxAayUWayDGFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0JbiAw3UUUUU= X-Originating-IP: [113.128.197.238] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiGQr6T1nkhEqA5QAAsw 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 , 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.14 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" Those functions that call virNetDevTapCreate don't need to adding 'vnet%d' into ifname when it is empty, since virNetDevGenerateName which is in virNetDevTapCreate can deal with it. Signed-off-by: Shi Lei Reviewed-by: Daniel Henrique Barboza Reviewed-by: Laine Stump --- src/bhyve/bhyve_command.c | 1 - src/qemu/qemu_interface.c | 12 ------------ 2 files changed, 13 deletions(-) diff --git a/src/bhyve/bhyve_command.c b/src/bhyve/bhyve_command.c index 4cf98c0e..92b31a6e 100644 --- a/src/bhyve/bhyve_command.c +++ b/src/bhyve/bhyve_command.c @@ -83,7 +83,6 @@ bhyveBuildNetArgStr(const virDomainDef *def, STRPREFIX(net->ifname, VIR_NET_GENERATED_VNET_PREFIX) || strchr(net->ifname, '%')) { VIR_FREE(net->ifname); - net->ifname =3D g_strdup(VIR_NET_GENERATED_VNET_PREFIX "%d"); } =20 if (!dryRun) { diff --git a/src/qemu/qemu_interface.c b/src/qemu/qemu_interface.c index 197c0aa2..87cfb8fc 100644 --- a/src/qemu/qemu_interface.c +++ b/src/qemu/qemu_interface.c @@ -413,7 +413,6 @@ qemuInterfaceEthernetConnect(virDomainDefPtr def, virMacAddr tapmac; int ret =3D -1; unsigned int tap_create_flags =3D VIR_NETDEV_TAP_CREATE_IFUP; - bool template_ifname =3D false; g_autoptr(virQEMUDriverConfig) cfg =3D virQEMUDriverGetConfig(driver); const char *tunpath =3D "/dev/net/tun"; const char *auditdev =3D tunpath; @@ -459,9 +458,6 @@ qemuInterfaceEthernetConnect(virDomainDefPtr def, STRPREFIX(net->ifname, VIR_NET_GENERATED_VNET_PREFIX) || strchr(net->ifname, '%')) { VIR_FREE(net->ifname); - net->ifname =3D g_strdup(VIR_NET_GENERATED_VNET_PREFIX "%d"); - /* avoid exposing vnet%d in getXMLDesc or error outputs */ - template_ifname =3D true; } if (virNetDevTapCreate(&net->ifname, tunpath, tapfd, tapfdSize, tap_create_flags) < 0) { @@ -512,8 +508,6 @@ qemuInterfaceEthernetConnect(virDomainDefPtr def, virDomainAuditNetDevice(def, net, auditdev, false); for (i =3D 0; i < tapfdSize && tapfd[i] >=3D 0; i++) VIR_FORCE_CLOSE(tapfd[i]); - if (template_ifname) - VIR_FREE(net->ifname); } =20 return ret; @@ -541,7 +535,6 @@ qemuInterfaceBridgeConnect(virDomainDefPtr def, const char *brname; int ret =3D -1; unsigned int tap_create_flags =3D VIR_NETDEV_TAP_CREATE_IFUP; - bool template_ifname =3D false; g_autoptr(virQEMUDriverConfig) cfg =3D virQEMUDriverGetConfig(driver); const char *tunpath =3D "/dev/net/tun"; =20 @@ -563,9 +556,6 @@ qemuInterfaceBridgeConnect(virDomainDefPtr def, STRPREFIX(net->ifname, VIR_NET_GENERATED_VNET_PREFIX) || strchr(net->ifname, '%')) { VIR_FREE(net->ifname); - net->ifname =3D g_strdup(VIR_NET_GENERATED_VNET_PREFIX "%d"); - /* avoid exposing vnet%d in getXMLDesc or error outputs */ - template_ifname =3D true; } =20 if (qemuInterfaceIsVnetCompatModel(net)) @@ -630,8 +620,6 @@ qemuInterfaceBridgeConnect(virDomainDefPtr def, size_t i; for (i =3D 0; i < *tapfdSize && tapfd[i] >=3D 0; i++) VIR_FORCE_CLOSE(tapfd[i]); - if (template_ifname) - VIR_FREE(net->ifname); } =20 return ret; --=20 2.25.1