From nobody Fri May 3 16:58:27 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1598419447; cv=none; d=zohomail.com; s=zohoarc; b=KjmTBzgdDUfxz+Cco8ejWN9hgDAaGxNGQeC2sVeEhaPr1gZ3hK8if0y3fd8kezkPVIZXy0mG1y4zWdMMNgQZxn9U+bh/dWM3VSOfoxe5hfIFu3PLAdot0vKa67+deWdsWht0kZ8RRoa3+zwM0tLSLSpOAvp+mi0aCo6P74Xelkc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598419447; 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=uwR8PzP9D3yuVOkjO7B5XZ9lZRDLFA0B1yzmyRq4foE=; b=AFzI7nnR6Qzivmm7DYAGBH2k1yQqd5Z2BsX/ieFlwNk+mtPpwFt7bLbUtZEjaxU4PeHK+CWAD+Q18k7e+ffYPTPjlnrlXkeA1I0iYl/mjR/X+N6x1rsfd9ttBG0jdvIXc9kZjMkIYDqMk16p8g3ygd/m327kswvAFdCRfFWQxjo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1598419447434425.083085715361; Tue, 25 Aug 2020 22:24:07 -0700 (PDT) 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-290-mc5eK-UOMrC6NQEDZU71oA-1; Wed, 26 Aug 2020 01:24:03 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 64475807330; Wed, 26 Aug 2020 05:23:58 +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 EEF63100238C; Wed, 26 Aug 2020 05:23:57 +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 07CF2668F1; Wed, 26 Aug 2020 05:23:57 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 07Q5N6Yv005996 for ; Wed, 26 Aug 2020 01:23:06 -0400 Received: by smtp.corp.redhat.com (Postfix) id 2A92F709D2; Wed, 26 Aug 2020 05:23:06 +0000 (UTC) Received: from vhost2.laine.org (ovpn-112-68.phx2.redhat.com [10.3.112.68]) by smtp.corp.redhat.com (Postfix) with ESMTP id 960807C0EB; Wed, 26 Aug 2020 05:23:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598419446; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=uwR8PzP9D3yuVOkjO7B5XZ9lZRDLFA0B1yzmyRq4foE=; b=MLrxPHXBXKzkJ0nu0DFfdgiZgBozXVMGmwsJP6VCcq2idDQm5NCLBvvFXILFG/OEk5Hvrv m5GoScgAa6l60Du1Z3yH/GKsDlH9/YgikEPc+JvJulQNo7MJ9Rg/vpwQyBDuXGKd9UGT9J kcbtO0qksyfO8ONa5uId9NSdjrMc/Yo= X-MC-Unique: mc5eK-UOMrC6NQEDZU71oA-1 From: Laine Stump To: libvir-list@redhat.com Subject: [libvirt PATCH v2 1/2] util: replace macvtap name reservation bitmap with a simple counter Date: Wed, 26 Aug 2020 01:22:57 -0400 Message-Id: <20200826052258.109848-2-laine@redhat.com> In-Reply-To: <20200826052258.109848-1-laine@redhat.com> References: <20200826052258.109848-1-laine@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Cc: Bingsong Si , Wei Gong 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.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0.002 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" There have been some reports that, due to libvirt always trying to assign the lowest numbered macvtap / tap device name possible, a new guest would sometimes be started using the same tap device name as previously used by another guest that is in the process of being destroyed *as the new guest is starting. In some cases this has led to, for example, the old guest's qemuProcessStop() code deleting a port from an OVS switch that had just been re-added by the new guest (because the port name is based on only the device name using the port). Similar problems can happen (and I believe have) with nwfilter rules and bandwidth rules (which are both instantiated based on the name of the tap device). A couple patches have been previously proposed to change the ordering of startup and shutdown processing, or to put a mutex around everything related to the tap/macvtap device name usage, but in the end no matter what you do there will still be possible holes, because the device could be deleted outside libvirt's control (for example, regular tap devices are automatically deleted when the qemu process terminates, and that isn't always initiated by libvirt but could instead happen completely asynchronously - libvirt then has no control over the ordering of shutdown operations, and no opportunity to protect it with a mutex.) But this only happens if a new device is created at the same time as one is being deleted. We can effectively eliminate the chance of this happening if we end the practice of always looking for the lowest numbered available device name, and instead just keep an integer that is incremented each time we need a new device name. At some point it will need to wrap back around to 0 (in order to avoid the IFNAMSIZ 15 character limit if nothing else), and we can't guarantee that the new name really will be the *least* recently used name, but "math" suggests that it will be *much* less common that we'll try to re-use the *most* recently used name. This patch implements such a counter for macvtap/macvlan, replacing the existing, and much more complicated, "ID reservation" system. The counter is set according to whatever macvtap/macvlan devices are already in use by guests when libvirtd is started, incremented each time a new device name is needed, and wraps back to 0 when either INT_MAX is reached, or when the resulting device name would be longer than IFNAMSIZ-1 characters (which actually is what happens when the template for the device name is "maccvtap%d"). The result is that no macvtap name will be re-used until the host has created (and possibly destroyed) 99,999,999 devices. Signed-off-by: Laine Stump Reviewed-by: Michal Privoznik --- 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 | 402 +++++++++++++----------------------- src/util/virnetdevmacvlan.h | 6 +- 6 files changed, 145 insertions(+), 270 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index f950a68179..4b155691a8 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2642,7 +2642,6 @@ virNetDevMacVLanDelete; virNetDevMacVLanDeleteWithVPortProfile; virNetDevMacVLanIsMacvtap; virNetDevMacVLanModeTypeFromString; -virNetDevMacVLanReleaseName; virNetDevMacVLanReserveName; virNetDevMacVLanRestartWithVPortProfile; virNetDevMacVLanTapOpen; diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index dc602ea162..ccda4e0031 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -367,7 +367,7 @@ libxlReconnectNotifyNets(virDomainDefPtr def) * impolite. */ if (virDomainNetGetActualType(net) =3D=3D VIR_DOMAIN_NET_TYPE_DIRE= CT) - ignore_value(virNetDevMacVLanReserveName(net->ifname, false)); + virNetDevMacVLanReserveName(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 fc59c2e5af..16969dbf33 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -1613,7 +1613,7 @@ virLXCProcessReconnectNotifyNets(virDomainDefPtr def) * impolite. */ if (virDomainNetGetActualType(net) =3D=3D VIR_DOMAIN_NET_TYPE_DIRE= CT) - ignore_value(virNetDevMacVLanReserveName(net->ifname, false)); + virNetDevMacVLanReserveName(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 ad461d8f34..2a862e6d9e 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -3321,7 +3321,7 @@ qemuProcessNotifyNets(virDomainDefPtr def) * impolite. */ if (virDomainNetGetActualType(net) =3D=3D VIR_DOMAIN_NET_TYPE_DIRE= CT) - ignore_value(virNetDevMacVLanReserveName(net->ifname, false)); + virNetDevMacVLanReserveName(net->ifname); =20 if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_NETWORK) { if (!conn && !(conn =3D virGetConnectNetwork())) diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c index dcea93a5fe..dc4db2c844 100644 --- a/src/util/virnetdevmacvlan.c +++ b/src/util/virnetdevmacvlan.c @@ -45,6 +45,7 @@ VIR_ENUM_IMPL(virNetDevMacVLanMode, =20 # include # include +# include =20 /* Older kernels lacked this enum value. */ # if !HAVE_DECL_MACVLAN_MODE_PASSTHRU @@ -69,211 +70,121 @@ VIR_LOG_INIT("util.netdevmacvlan"); ((flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ? \ VIR_NET_GENERATED_MACVTAP_PREFIX : VIR_NET_GENERATED_MACVLAN_PREFIX) =20 -# define MACVLAN_MAX_ID 8191 =20 virMutex virNetDevMacVLanCreateMutex =3D VIR_MUTEX_INITIALIZER; -virBitmapPtr macvtapIDs =3D NULL; -virBitmapPtr macvlanIDs =3D NULL; - -static int -virNetDevMacVLanOnceInit(void) -{ - if (!macvtapIDs && - !(macvtapIDs =3D virBitmapNew(MACVLAN_MAX_ID + 1))) - return -1; - if (!macvlanIDs && - !(macvlanIDs =3D virBitmapNew(MACVLAN_MAX_ID + 1))) - return -1; - return 0; -} - -VIR_ONCE_GLOBAL_INIT(virNetDevMacVLan); +static int virNetDevMacVTapLastID =3D -1; +static int virNetDevMacVLanLastID =3D -1; =20 =20 -/** - * virNetDevMacVLanReserveID: - * - * @id: id 0 - MACVLAN_MAX_ID+1 to reserve (or -1 for "first free") - * @flags: set VIR_NETDEV_MACVLAN_CREATE_WITH_TAP for macvtapN else macvl= anN - * @quietFail: don't log an error if this name is already in-use - * @nextFree: reserve the next free ID *after* @id rather than @id itself - * - * Reserve the indicated ID in the appropriate bitmap, or find the - * first free ID if @id is -1. - * - * Returns newly reserved ID# on success, or -1 to indicate failure. - */ -static int -virNetDevMacVLanReserveID(int id, unsigned int flags, - bool quietFail, bool nextFree) +static void +virNetDevMacVLanReserveNameInternal(const char *name) { - virBitmapPtr bitmap; - - if (virNetDevMacVLanInitialize() < 0) - return -1; - - bitmap =3D (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ? - macvtapIDs : macvlanIDs; + unsigned int id; + const char *idstr =3D NULL; + int *lastID =3D NULL; + int len; =20 - if (id > MACVLAN_MAX_ID) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("can't use name %s%d - out of range 0-%d"), - VIR_NET_GENERATED_PREFIX, id, MACVLAN_MAX_ID); - return -1; + 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; } =20 - if ((id < 0 || nextFree) && - (id =3D virBitmapNextClearBit(bitmap, id)) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("no unused %s names available"), - VIR_NET_GENERATED_PREFIX); - return -1; - } + VIR_INFO("marking device in use: '%s'", name); =20 - if (virBitmapIsBitSet(bitmap, id)) { - if (quietFail) { - VIR_INFO("couldn't reserve name %s%d - already in use", - VIR_NET_GENERATED_PREFIX, id); - } else { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("couldn't reserve name %s%d - already in use"= ), - VIR_NET_GENERATED_PREFIX, id); - } - return -1; - } + idstr =3D name + len; =20 - if (virBitmapSetBit(bitmap, id) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("couldn't mark %s%d as used"), - VIR_NET_GENERATED_PREFIX, id); - return -1; + if (virStrToLong_ui(idstr, NULL, 10, &id) >=3D 0) { + if (*lastID < (int)id) + *lastID =3D id; } - - VIR_INFO("reserving device %s%d", VIR_NET_GENERATED_PREFIX, id); - return id; } =20 =20 /** - * virNetDevMacVLanReleaseID: - * @id: id 0 - MACVLAN_MAX_ID+1 to release + * virNetDevMacVLanReserveName: + * @name: name of an existing macvtap/macvlan device * - * Returns 0 for success or -1 for failure. + * 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. */ -static int -virNetDevMacVLanReleaseID(int id, unsigned int flags) +void +virNetDevMacVLanReserveName(const char *name) { - virBitmapPtr bitmap; - - if (virNetDevMacVLanInitialize() < 0) - return 0; - - bitmap =3D (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ? - macvtapIDs : macvlanIDs; - - if (id > MACVLAN_MAX_ID) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("can't free name %s%d - out of range 0-%d"), - VIR_NET_GENERATED_PREFIX, id, MACVLAN_MAX_ID); - return -1; - } - - if (id < 0) - return 0; - - VIR_INFO("releasing %sdevice %s%d", - virBitmapIsBitSet(bitmap, id) ? "" : "unreserved", - VIR_NET_GENERATED_PREFIX, id); - - if (virBitmapClearBit(bitmap, id) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("couldn't mark %s%d as unused"), - VIR_NET_GENERATED_PREFIX, id); - return -1; - } - return 0; + virMutexLock(&virNetDevMacVLanCreateMutex); + virNetDevMacVLanReserveNameInternal(name); + virMutexUnlock(&virNetDevMacVLanCreateMutex); } =20 =20 /** - * virNetDevMacVLanReserveName: + * virNetDevMacVLanGenerateName: + * @ifname: pointer to pointer to string containing template + * @lastID: counter to add to the template to form the name * - * @name: already-known name of device - * @quietFail: don't log an error if this name is already in-use + * 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. * - * Extract the device type and id from a macvtap/macvlan device name - * and mark the appropriate position as in-use in the appropriate - * bitmap. - * - * Returns reserved ID# on success, -1 on failure, -2 if the name - * doesn't fit the auto-pattern (so not reserveable). + * Returns 0 on success, -1 on failure. */ -int -virNetDevMacVLanReserveName(const char *name, bool quietFail) +static int +virNetDevMacVLanGenerateName(char **ifname, unsigned int flags) { - unsigned int id; - unsigned int flags =3D 0; - const char *idstr =3D NULL; - - if (virNetDevMacVLanInitialize() < 0) - return -1; + const char *prefix; + const char *iftemplate; + int *lastID; + int id; + double maxIDd; + int maxID =3D INT_MAX; + int attempts =3D 0; =20 - if (STRPREFIX(name, VIR_NET_GENERATED_MACVTAP_PREFIX)) { - idstr =3D name + strlen(VIR_NET_GENERATED_MACVTAP_PREFIX); - flags |=3D VIR_NETDEV_MACVLAN_CREATE_WITH_TAP; - } else if (STRPREFIX(name, VIR_NET_GENERATED_MACVLAN_PREFIX)) { - idstr =3D name + strlen(VIR_NET_GENERATED_MACVLAN_PREFIX); + 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 { - return -2; + prefix =3D VIR_NET_GENERATED_MACVLAN_PREFIX; + iftemplate =3D VIR_NET_GENERATED_MACVLAN_PREFIX "%d"; + lastID =3D &virNetDevMacVLanLastID; } =20 - if (virStrToLong_ui(idstr, NULL, 10, &id) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("couldn't get id value from macvtap device name %= s"), - name); - return -1; - } - return virNetDevMacVLanReserveID(id, flags, quietFail, false); -} + maxIDd =3D pow(10, IFNAMSIZ - 1 - strlen(prefix)); + if (maxIDd <=3D (double)INT_MAX) + maxID =3D (int)maxIDd; =20 + do { + g_autofree char *try =3D NULL; =20 -/** - * virNetDevMacVLanReleaseName: - * - * @name: already-known name of device - * - * Extract the device type and id from a macvtap/macvlan device name - * and mark the appropriate position as in-use in the appropriate - * bitmap. - * - * returns 0 on success, -1 on failure - */ -int -virNetDevMacVLanReleaseName(const char *name) -{ - unsigned int id; - unsigned int flags =3D 0; - const char *idstr =3D NULL; + id =3D ++(*lastID); =20 - if (virNetDevMacVLanInitialize() < 0) - return -1; + /* reset before overflow */ + if (*lastID =3D=3D maxID) + *lastID =3D -1; =20 - if (STRPREFIX(name, VIR_NET_GENERATED_MACVTAP_PREFIX)) { - idstr =3D name + strlen(VIR_NET_GENERATED_MACVTAP_PREFIX); - flags |=3D VIR_NETDEV_MACVLAN_CREATE_WITH_TAP; - } else if (STRPREFIX(name, VIR_NET_GENERATED_MACVLAN_PREFIX)) { - idstr =3D name + strlen(VIR_NET_GENERATED_MACVLAN_PREFIX); - } else { - return 0; - } + try =3D g_strdup_printf(iftemplate, id); =20 - if (virStrToLong_ui(idstr, NULL, 10, &id) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("couldn't get id value from macvtap device name %= s"), - name); - return -1; - } - return virNetDevMacVLanReleaseID(id, flags); + 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 =20 @@ -320,8 +231,7 @@ virNetDevMacVLanCreate(const char *ifname, const char *type, const virMacAddr *macaddress, const char *srcdev, - uint32_t macvlan_mode, - int *retry) + uint32_t macvlan_mode) { int error =3D 0; int ifindex =3D 0; @@ -330,7 +240,6 @@ virNetDevMacVLanCreate(const char *ifname, .mac =3D macaddress, }; =20 - *retry =3D 0; =20 if (virNetDevGetIndex(srcdev, &ifindex) < 0) return -1; @@ -338,17 +247,15 @@ virNetDevMacVLanCreate(const char *ifname, data.ifindex =3D &ifindex; if (virNetlinkNewLink(ifname, type, &data, &error) < 0) { char macstr[VIR_MAC_STRING_BUFLEN]; - if (error =3D=3D -EEXIST) - *retry =3D 1; - else if (error < 0) - virReportSystemError(-error, - _("error creating %s interface %s@%s (%s)= "), - type, ifname, srcdev, - virMacAddrFormat(macaddress, macstr)); =20 + virReportSystemError(-error, + _("error creating %s interface %s@%s (%s)"), + type, ifname, srcdev, + virMacAddrFormat(macaddress, macstr)); return -1; } =20 + VIR_INFO("created device: '%s'", ifname); return 0; } =20 @@ -363,6 +270,7 @@ virNetDevMacVLanCreate(const char *ifname, */ int virNetDevMacVLanDelete(const char *ifname) { + VIR_INFO("delete device: '%s'", ifname); return virNetlinkDelLink(ifname, NULL); } =20 @@ -903,13 +811,8 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifn= ameRequested, unsigned int flags) { const char *type =3D VIR_NET_GENERATED_PREFIX; - const char *pattern =3D (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ? - VIR_NET_GENERATED_MACVTAP_PATTERN : VIR_NET_GENERATED_MACVLAN_PATT= ERN; - int reservedID =3D -1; - char ifname[IFNAMSIZ]; - int retries, do_retry =3D 0; + g_autofree char *ifname =3D NULL; uint32_t macvtapMode; - const char *ifnameCreated =3D NULL; int vf =3D -1; bool vnet_hdr =3D flags & VIR_NETDEV_MACVLAN_VNET_HDR; =20 @@ -944,6 +847,8 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifna= meRequested, return -1; } =20 + virMutexLock(&virNetDevMacVLanCreateMutex); + if (ifnameRequested) { int rc; bool isAutoName @@ -951,97 +856,81 @@ virNetDevMacVLanCreateWithVPortProfile(const char *if= nameRequested, STRPREFIX(ifnameRequested, VIR_NET_GENERATED_MACVLAN_PREFIX= )); =20 VIR_INFO("Requested macvtap device name: %s", ifnameRequested); - virMutexLock(&virNetDevMacVLanCreateMutex); =20 if ((rc =3D virNetDevExists(ifnameRequested)) < 0) { virMutexUnlock(&virNetDevMacVLanCreateMutex); return -1; } + if (rc) { - if (isAutoName) - goto create_name; - virReportSystemError(EEXIST, - _("Unable to create %s device %s"), - type, ifnameRequested); - virMutexUnlock(&virNetDevMacVLanCreateMutex); - return -1; - } - if (isAutoName && - (reservedID =3D virNetDevMacVLanReserveName(ifnameRequested, t= rue)) < 0) { - reservedID =3D -1; - goto create_name; - } + /* ifnameRequested is already being used */ =20 - if (virNetDevMacVLanCreate(ifnameRequested, type, macaddress, - linkdev, macvtapMode, &do_retry) < 0) { - if (isAutoName) { - virNetDevMacVLanReleaseName(ifnameRequested); - reservedID =3D -1; - goto create_name; + if (!isAutoName) { + virReportSystemError(EEXIST, + _("Unable to create device '%s'"), + ifnameRequested); + virMutexUnlock(&virNetDevMacVLanCreateMutex); + return -1; + } + } else { + + /* ifnameRequested is available. try to open it */ + + virNetDevMacVLanReserveNameInternal(ifnameRequested); + + if (virNetDevMacVLanCreate(ifnameRequested, type, macaddress, + linkdev, macvtapMode) =3D=3D 0) { + + /* virNetDevMacVLanCreate() was successful - use this name= */ + ifname =3D g_strdup(ifnameRequested); + + } else if (!isAutoName) { + /* coudn't open ifnameRequested, but it wasn't an + * autogenerated named, so there is nothing else to + * try - fail and return. + */ + virMutexUnlock(&virNetDevMacVLanCreateMutex); + return -1; } - virMutexUnlock(&virNetDevMacVLanCreateMutex); - return -1; } - /* virNetDevMacVLanCreate() was successful - use this name */ - ifnameCreated =3D ifnameRequested; - create_name: - virMutexUnlock(&virNetDevMacVLanCreateMutex); } =20 - retries =3D MACVLAN_MAX_ID; - while (!ifnameCreated && retries) { - virMutexLock(&virNetDevMacVLanCreateMutex); - reservedID =3D virNetDevMacVLanReserveID(reservedID, flags, false,= true); - if (reservedID < 0) { + if (!ifname) { + /* ifnameRequested was NULL, or it was an already in use + * 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); return -1; } - g_snprintf(ifname, sizeof(ifname), pattern, reservedID); - if (virNetDevMacVLanCreate(ifname, type, macaddress, linkdev, - macvtapMode, &do_retry) < 0) { - virNetDevMacVLanReleaseID(reservedID, flags); - virMutexUnlock(&virNetDevMacVLanCreateMutex); - if (!do_retry) - return -1; - VIR_INFO("Device %s wasn't reserved but already existed, skipp= ing", - ifname); - retries--; - continue; - } - ifnameCreated =3D ifname; - virMutexUnlock(&virNetDevMacVLanCreateMutex); } =20 - if (!ifnameCreated) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Too many unreserved %s devices in use"), - type); - return -1; - } + /* all done creating the device */ + virMutexUnlock(&virNetDevMacVLanCreateMutex); =20 - if (virNetDevVPortProfileAssociate(ifnameCreated, + if (virNetDevVPortProfileAssociate(ifname, virtPortProfile, macaddress, linkdev, vf, - vmuuid, vmOp, false) < 0) + vmuuid, vmOp, false) < 0) { goto link_del_exit; + } =20 if (flags & VIR_NETDEV_MACVLAN_CREATE_IFUP) { - if (virNetDevSetOnline(ifnameCreated, true) < 0) + if (virNetDevSetOnline(ifname, true) < 0) goto disassociate_exit; } =20 if (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) { - if (virNetDevMacVLanTapOpen(ifnameCreated, tapfd, tapfdSize) < 0) + if (virNetDevMacVLanTapOpen(ifname, tapfd, tapfdSize) < 0) goto disassociate_exit; =20 if (virNetDevMacVLanTapSetup(tapfd, tapfdSize, vnet_hdr) < 0) goto disassociate_exit; - - *ifnameResult =3D g_strdup(ifnameCreated); - } else { - *ifnameResult =3D g_strdup(ifnameCreated); } =20 if (vmOp =3D=3D VIR_NETDEV_VPORT_PROFILE_OP_CREATE || @@ -1050,17 +939,18 @@ virNetDevMacVLanCreateWithVPortProfile(const char *i= fnameRequested, * a saved image) - migration and libvirtd restart are handled * elsewhere. */ - if (virNetDevMacVLanVPortProfileRegisterCallback(ifnameCreated, ma= caddress, + if (virNetDevMacVLanVPortProfileRegisterCallback(ifname, macaddres= s, linkdev, vmuuid, virtPortProfile, vmOp) < 0) goto disassociate_exit; } =20 + *ifnameResult =3D g_steal_pointer(&ifname); return 0; =20 disassociate_exit: - ignore_value(virNetDevVPortProfileDisassociate(ifnameCreated, + ignore_value(virNetDevVPortProfileDisassociate(ifname, virtPortProfile, macaddress, linkdev, @@ -1070,9 +960,7 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifn= ameRequested, VIR_FORCE_CLOSE(tapfd[tapfdSize]); =20 link_del_exit: - ignore_value(virNetDevMacVLanDelete(ifnameCreated)); - virNetDevMacVLanReleaseName(ifnameCreated); - + ignore_value(virNetDevMacVLanDelete(ifname)); return -1; } =20 @@ -1106,7 +994,6 @@ int virNetDevMacVLanDeleteWithVPortProfile(const char = *ifname, ret =3D -1; if (virNetDevMacVLanDelete(ifname) < 0) ret =3D -1; - virNetDevMacVLanReleaseName(ifname); } =20 if (mode =3D=3D VIR_NETDEV_MACVLAN_MODE_PASSTHRU) { @@ -1181,8 +1068,7 @@ 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, - int *retry G_GNUC_UNUSED) + uint32_t macvlan_mode G_GNUC_UNUSED) { virReportSystemError(ENOSYS, "%s", _("Cannot create macvlan devices on this platform= ")); @@ -1271,15 +1157,7 @@ int virNetDevMacVLanVPortProfileRegisterCallback(con= st char *ifname G_GNUC_UNUSE return -1; } =20 -int virNetDevMacVLanReleaseName(const char *name G_GNUC_UNUSED) -{ - virReportSystemError(ENOSYS, "%s", - _("Cannot create macvlan devices on this platform= ")); - return -1; -} - -int virNetDevMacVLanReserveName(const char *name G_GNUC_UNUSED, - bool quietFail G_GNUC_UNUSED) +void virNetDevMacVLanReserveName(const char *name G_GNUC_UNUSED) { virReportSystemError(ENOSYS, "%s", _("Cannot create macvlan devices on this platform= ")); diff --git a/src/util/virnetdevmacvlan.h b/src/util/virnetdevmacvlan.h index fc1bb018a2..48800a8fcf 100644 --- a/src/util/virnetdevmacvlan.h +++ b/src/util/virnetdevmacvlan.h @@ -54,8 +54,7 @@ typedef enum { #define VIR_NET_GENERATED_MACVTAP_PREFIX "macvtap" #define VIR_NET_GENERATED_MACVLAN_PREFIX "macvlan" =20 -int virNetDevMacVLanReserveName(const char *name, bool quietfail); -int virNetDevMacVLanReleaseName(const char *name); +void virNetDevMacVLanReserveName(const char *name); =20 bool virNetDevMacVLanIsMacvtap(const char *ifname) ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT G_GNUC_NO_INLINE; @@ -64,8 +63,7 @@ int virNetDevMacVLanCreate(const char *ifname, const char *type, const virMacAddr *macaddress, const char *srcdev, - uint32_t macvlan_mode, - int *retry) + uint32_t macvlan_mode) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4) G_GNUC_WARN_UNUSED_RESULT; =20 --=20 2.26.2 From nobody Fri May 3 16:58:27 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; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1598419512; cv=none; d=zohomail.com; s=zohoarc; b=e2NzckJSXvjkvhzF2SRU4SjfYJ/u2d3m9eaKC/9csFuHIsdfA34b8KEqRL5836pkXqbHYgEpL2JrTdYyYtNI2cbNNFH4+49IU8hSRabIjSCsExweesbUIpp2i31D1xDD66h6TchK9L27HRz0h5FM/te4fwVBc4r6yC2T6BWB8E8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598419512; 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=2/sBGcrnhrQelWUJA+kJGLKofRJU6CTOhvGKAKgAo3Y=; b=YI8dUWLcVLLlyG/v7mLNDa0BGQqQtnWC+orwDzmkwEPsFHbWU6+ek6Bt4JaKgkMqe6EBjAmDmujBTCkRdDh66lZnA6emO8z1M9Jzc2Fstx5C6C1WwKFj7BskuZcYdgNIlV2tx3HPR0RxmuvgclJH/H9zYslXWCfn/famTXw1E2U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= 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 1598419512469924.589788458206; Tue, 25 Aug 2020 22:25:12 -0700 (PDT) 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-246-xo6mTccqOWWRo0zEvtblWQ-1; Wed, 26 Aug 2020 01:25:08 -0400 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 6C88B1008552; Wed, 26 Aug 2020 05:25:03 +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 236465C1C2; Wed, 26 Aug 2020 05:25:03 +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 D6F89180B655; Wed, 26 Aug 2020 05:25:02 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 07Q5N6Af006001 for ; Wed, 26 Aug 2020 01:23:06 -0400 Received: by smtp.corp.redhat.com (Postfix) id C7F6C708FF; Wed, 26 Aug 2020 05:23:06 +0000 (UTC) Received: from vhost2.laine.org (ovpn-112-68.phx2.redhat.com [10.3.112.68]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5688974E14; Wed, 26 Aug 2020 05:23:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598419511; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=2/sBGcrnhrQelWUJA+kJGLKofRJU6CTOhvGKAKgAo3Y=; b=VoKvOlqbqmVlvzoNcYbHqwkY9R+PeQOZVK3tBmMedJZ0WtWgCrfk+ZzKv+8AAuCCCLTfXL Rxtke+cHQcrcWnLq5LRgn/MunJ/MgkH1kxM4qBDoiW65neaMje5HHZPamy5xdHKQu+xaxk yxvjvdY4hwydn1SESm0vw8sCwka/T+s= X-MC-Unique: xo6mTccqOWWRo0zEvtblWQ-1 From: Laine Stump To: libvir-list@redhat.com Subject: [libvirt PATCH v2 2/2] util: assign tap device names using a monotonically increasing integer Date: Wed, 26 Aug 2020 01:22:58 -0400 Message-Id: <20200826052258.109848-3-laine@redhat.com> In-Reply-To: <20200826052258.109848-1-laine@redhat.com> References: <20200826052258.109848-1-laine@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Cc: Bingsong Si , Wei Gong 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.002 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" When creating a standard tap device, if provided with an ifname that contains "%d", rather than taking that literally as the name to use for the new device, the kernel will instead use that string as a template, and search for the lowest number that could be put in place of %d and produce an otherwise unused and unique name for the new device. For example, if there is no tap device name given in the XML, libvirt will always send "vnet%d" as the device name, and the kernel will create new devices named "vnet0", "vnet1", etc. If one of those devices is deleted, creating a "hole" in the name list, the kernel will always attempt to reuse the name in the hole first before using a name with a higher number (i.e. it finds the lowest possible unused number). The problem with this, as described in the previous patch dealing with macvtap device naming, is that it makes "immediate reuse" of a newly freed tap device name *much* more common, and in the aftermath of deleting a tap device, there is some other necessary cleanup of things which are named based on the device name (nwfilter rules, bandwidth rules, OVS switch ports, to name a few) that could end up stomping over the top of the setup of a new device of the same name for a different guest. Since the kernel "create a name based on a template" functionality for tap devices doesn't exist for macvtap, this patch for standard tap devices is a bit different from the previous patch for macvtap - in particular there was no previous "bitmap ID reservation system" or overly-complex retry loop that needed to be removed. We simply find and unused name, and pass that name on to the kernel instead of "vnet%d". This counter is also wrapped when either it gets to INT_MAX or if the full name would overflow IFNAMSIZ-1 characters. In the case of "vnet%d" and a 32 bit int, we would reach INT_MAX first, but possibly someday someone will change the name from vnet to something else. (NB: It is still possible for a user to provide their own parameterized template name (e.g. "mytap%d") in the XML, and libvirt will just pass that through to the kernel as it always has.) Signed-off-by: Laine Stump Reviewed-by: Michal Privoznik --- src/libvirt_private.syms | 1 + src/qemu/qemu_process.c | 20 +++++++- src/util/virnetdevtap.c | 108 ++++++++++++++++++++++++++++++++++++++- src/util/virnetdevtap.h | 4 ++ 4 files changed, 130 insertions(+), 3 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 4b155691a8..5736a2dbd3 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2676,6 +2676,7 @@ 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 2a862e6d9e..222a1376c4 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -3320,8 +3320,26 @@ qemuProcessNotifyNets(virDomainDefPtr def) * domain to be unceremoniously killed, which would be *very* * impolite. */ - if (virDomainNetGetActualType(net) =3D=3D VIR_DOMAIN_NET_TYPE_DIRE= CT) + switch (virDomainNetGetActualType(net)) { + case VIR_DOMAIN_NET_TYPE_DIRECT: virNetDevMacVLanReserveName(net->ifname); + break; + case VIR_DOMAIN_NET_TYPE_BRIDGE: + case VIR_DOMAIN_NET_TYPE_NETWORK: + case VIR_DOMAIN_NET_TYPE_ETHERNET: + virNetDevTapReserveName(net->ifname); + break; + case VIR_DOMAIN_NET_TYPE_USER: + case VIR_DOMAIN_NET_TYPE_VHOSTUSER: + case VIR_DOMAIN_NET_TYPE_SERVER: + case VIR_DOMAIN_NET_TYPE_CLIENT: + case VIR_DOMAIN_NET_TYPE_MCAST: + case VIR_DOMAIN_NET_TYPE_INTERNAL: + case VIR_DOMAIN_NET_TYPE_HOSTDEV: + case VIR_DOMAIN_NET_TYPE_UDP: + case VIR_DOMAIN_NET_TYPE_LAST: + break; + } =20 if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_NETWORK) { if (!conn && !(conn =3D virGetConnectNetwork())) diff --git a/src/util/virnetdevtap.c b/src/util/virnetdevtap.c index c0a7c3019e..a46f836da2 100644 --- a/src/util/virnetdevtap.c +++ b/src/util/virnetdevtap.c @@ -49,11 +49,100 @@ #if defined(HAVE_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_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); + } + } +} + + +/** + * 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_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; +} + + /** * virNetDevTapGetName: * @tapfd: a tun/tap file descriptor @@ -230,10 +319,22 @@ int virNetDevTapCreate(char **ifname, size_t tapfdSize, unsigned int flags) { - size_t i; + size_t i =3D 0; struct ifreq ifr; int ret =3D -1; - int fd; + int fd =3D 0; + + 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_TAP_PREFIX "%d") && + virNetDevTapGenerateName(ifname) < 0) { + goto cleanup; + } =20 if (!tunpath) tunpath =3D "/dev/net/tun"; @@ -299,9 +400,11 @@ int virNetDevTapCreate(char **ifname, tapfd[i] =3D fd; } =20 + VIR_INFO("created device: '%s'", *ifname); ret =3D 0; =20 cleanup: + virMutexUnlock(&virNetDevTapCreateMutex); if (ret < 0) { VIR_FORCE_CLOSE(fd); while (i--) @@ -351,6 +454,7 @@ int virNetDevTapDelete(const char *ifname, goto cleanup; } =20 + VIR_INFO("delete device: '%s'", ifname); ret =3D 0; =20 cleanup: diff --git a/src/util/virnetdevtap.h b/src/util/virnetdevtap.h index c6bd9285ba..dea8aec3af 100644 --- a/src/util/virnetdevtap.h +++ b/src/util/virnetdevtap.h @@ -29,6 +29,10 @@ # 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.26.2