render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
79b470
From 4238e5f0783c63802de79bc5ed2a1f49673ef2a3 Mon Sep 17 00:00:00 2001
79b470
Message-Id: <4238e5f0783c63802de79bc5ed2a1f49673ef2a3@dist-git>
79b470
From: Laine Stump <laine@redhat.com>
79b470
Date: Sat, 12 Dec 2020 22:04:51 -0500
79b470
Subject: [PATCH] util: replace macvtap name reservation bitmap with a simple
79b470
 counter
79b470
79b470
There have been some reports that, due to libvirt always trying to
79b470
assign the lowest numbered macvtap / tap device name possible, a new
79b470
guest would sometimes be started using the same tap device name as
79b470
previously used by another guest that is in the process of being
79b470
destroyed *as the new guest is starting.
79b470
79b470
In some cases this has led to, for example, the old guest's
79b470
qemuProcessStop() code deleting a port from an OVS switch that had
79b470
just been re-added by the new guest (because the port name is based on
79b470
only the device name using the port). Similar problems can happen (and
79b470
I believe have) with nwfilter rules and bandwidth rules (which are
79b470
both instantiated based on the name of the tap device).
79b470
79b470
A couple patches have been previously proposed to change the ordering
79b470
of startup and shutdown processing, or to put a mutex around
79b470
everything related to the tap/macvtap device name usage, but in the
79b470
end no matter what you do there will still be possible holes, because
79b470
the device could be deleted outside libvirt's control (for example,
79b470
regular tap devices are automatically deleted when the qemu process
79b470
terminates, and that isn't always initiated by libvirt but could
79b470
instead happen completely asynchronously - libvirt then has no control
79b470
over the ordering of shutdown operations, and no opportunity to
79b470
protect it with a mutex.)
79b470
79b470
But this only happens if a new device is created at the same time as
79b470
one is being deleted. We can effectively eliminate the chance of this
79b470
happening if we end the practice of always looking for the lowest
79b470
numbered available device name, and instead just keep an integer that
79b470
is incremented each time we need a new device name. At some point it
79b470
will need to wrap back around to 0 (in order to avoid the IFNAMSIZ 15
79b470
character limit if nothing else), and we can't guarantee that the new
79b470
name really will be the *least* recently used name, but "math"
79b470
suggests that it will be *much* less common that we'll try to re-use
79b470
the *most* recently used name.
79b470
79b470
This patch implements such a counter for macvtap/macvlan, replacing
79b470
the existing, and much more complicated, "ID reservation" system. The
79b470
counter is set according to whatever macvtap/macvlan devices are
79b470
already in use by guests when libvirtd is started, incremented each
79b470
time a new device name is needed, and wraps back to 0 when either
79b470
INT_MAX is reached, or when the resulting device name would be longer
79b470
than IFNAMSIZ-1 characters (which actually is what happens when the
79b470
template for the device name is "maccvtap%d"). The result is that no
79b470
macvtap name will be re-used until the host has created (and possibly
79b470
destroyed) 99,999,999 devices.
79b470
79b470
Signed-off-by: Laine Stump <laine@redhat.com>
79b470
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
79b470
(cherry picked from commit d7f38beb2ee072f1f19bb91fbafc9182ce9b069e)
79b470
79b470
https://bugzilla.redhat.com/1874304
79b470
Signed-off-by: Laine Stump <laine@redhat.com>
79b470
Message-Id: <20201213030453.48851-2-laine@redhat.com>
79b470
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
79b470
---
79b470
 src/libvirt_private.syms    |   1 -
79b470
 src/libxl/libxl_driver.c    |   2 +-
79b470
 src/lxc/lxc_process.c       |   2 +-
79b470
 src/qemu/qemu_process.c     |   2 +-
79b470
 src/util/virnetdevmacvlan.c | 403 +++++++++++++-----------------------
79b470
 src/util/virnetdevmacvlan.h |   6 +-
79b470
 6 files changed, 145 insertions(+), 271 deletions(-)
79b470
79b470
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
79b470
index fdd104cd25..1c66c40f86 100644
79b470
--- a/src/libvirt_private.syms
79b470
+++ b/src/libvirt_private.syms
79b470
@@ -2604,7 +2604,6 @@ virNetDevMacVLanDelete;
79b470
 virNetDevMacVLanDeleteWithVPortProfile;
79b470
 virNetDevMacVLanIsMacvtap;
79b470
 virNetDevMacVLanModeTypeFromString;
79b470
-virNetDevMacVLanReleaseName;
79b470
 virNetDevMacVLanReserveName;
79b470
 virNetDevMacVLanRestartWithVPortProfile;
79b470
 virNetDevMacVLanTapOpen;
79b470
diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c
79b470
index 1449795494..9269e9b475 100644
79b470
--- a/src/libxl/libxl_driver.c
79b470
+++ b/src/libxl/libxl_driver.c
79b470
@@ -367,7 +367,7 @@ libxlReconnectNotifyNets(virDomainDefPtr def)
79b470
          * impolite.
79b470
          */
79b470
         if (virDomainNetGetActualType(net) == VIR_DOMAIN_NET_TYPE_DIRECT)
79b470
-           ignore_value(virNetDevMacVLanReserveName(net->ifname, false));
79b470
+            virNetDevMacVLanReserveName(net->ifname);
79b470
 
79b470
         if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
79b470
             if (!conn && !(conn = virGetConnectNetwork()))
79b470
diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c
79b470
index 0a9ccdf9ec..114c40b0ea 100644
79b470
--- a/src/lxc/lxc_process.c
79b470
+++ b/src/lxc/lxc_process.c
79b470
@@ -1638,7 +1638,7 @@ virLXCProcessReconnectNotifyNets(virDomainDefPtr def)
79b470
          * impolite.
79b470
          */
79b470
         if (virDomainNetGetActualType(net) == VIR_DOMAIN_NET_TYPE_DIRECT)
79b470
-           ignore_value(virNetDevMacVLanReserveName(net->ifname, false));
79b470
+            virNetDevMacVLanReserveName(net->ifname);
79b470
 
79b470
         if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
79b470
             if (!conn && !(conn = virGetConnectNetwork()))
79b470
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
79b470
index 95c0315e53..b49a463c02 100644
79b470
--- a/src/qemu/qemu_process.c
79b470
+++ b/src/qemu/qemu_process.c
79b470
@@ -3288,7 +3288,7 @@ qemuProcessNotifyNets(virDomainDefPtr def)
79b470
          * impolite.
79b470
          */
79b470
         if (virDomainNetGetActualType(net) == VIR_DOMAIN_NET_TYPE_DIRECT)
79b470
-           ignore_value(virNetDevMacVLanReserveName(net->ifname, false));
79b470
+            virNetDevMacVLanReserveName(net->ifname);
79b470
 
79b470
         if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
79b470
             if (!conn && !(conn = virGetConnectNetwork()))
79b470
diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c
79b470
index 3ca568fb44..7046cbb04e 100644
79b470
--- a/src/util/virnetdevmacvlan.c
79b470
+++ b/src/util/virnetdevmacvlan.c
79b470
@@ -47,6 +47,7 @@ VIR_ENUM_IMPL(virNetDevMacVLanMode,
79b470
 
79b470
 # include <net/if.h>
79b470
 # include <linux/if_tun.h>
79b470
+# include <math.h>
79b470
 
79b470
 /* Older kernels lacked this enum value.  */
79b470
 # if !HAVE_DECL_MACVLAN_MODE_PASSTHRU
79b470
@@ -70,211 +71,121 @@ VIR_LOG_INIT("util.netdevmacvlan");
79b470
     ((flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ? \
79b470
      VIR_NET_GENERATED_MACVTAP_PREFIX : VIR_NET_GENERATED_MACVLAN_PREFIX)
79b470
 
79b470
-# define MACVLAN_MAX_ID 8191
79b470
 
79b470
 virMutex virNetDevMacVLanCreateMutex = VIR_MUTEX_INITIALIZER;
79b470
-virBitmapPtr macvtapIDs = NULL;
79b470
-virBitmapPtr macvlanIDs = NULL;
79b470
-
79b470
-static int
79b470
-virNetDevMacVLanOnceInit(void)
79b470
-{
79b470
-    if (!macvtapIDs &&
79b470
-        !(macvtapIDs = virBitmapNew(MACVLAN_MAX_ID + 1)))
79b470
-        return -1;
79b470
-    if (!macvlanIDs &&
79b470
-        !(macvlanIDs = virBitmapNew(MACVLAN_MAX_ID + 1)))
79b470
-        return -1;
79b470
-    return 0;
79b470
-}
79b470
-
79b470
-VIR_ONCE_GLOBAL_INIT(virNetDevMacVLan);
79b470
+static int virNetDevMacVTapLastID = -1;
79b470
+static int virNetDevMacVLanLastID = -1;
79b470
 
79b470
 
79b470
-/**
79b470
- * virNetDevMacVLanReserveID:
79b470
- *
79b470
- *  @id: id 0 - MACVLAN_MAX_ID+1 to reserve (or -1 for "first free")
79b470
- *  @flags: set VIR_NETDEV_MACVLAN_CREATE_WITH_TAP for macvtapN else macvlanN
79b470
- *  @quietFail: don't log an error if this name is already in-use
79b470
- *  @nextFree: reserve the next free ID *after* @id rather than @id itself
79b470
- *
79b470
- *  Reserve the indicated ID in the appropriate bitmap, or find the
79b470
- *  first free ID if @id is -1.
79b470
- *
79b470
- *  Returns newly reserved ID# on success, or -1 to indicate failure.
79b470
- */
79b470
-static int
79b470
-virNetDevMacVLanReserveID(int id, unsigned int flags,
79b470
-                          bool quietFail, bool nextFree)
79b470
+static void
79b470
+virNetDevMacVLanReserveNameInternal(const char *name)
79b470
 {
79b470
-    virBitmapPtr bitmap;
79b470
-
79b470
-    if (virNetDevMacVLanInitialize() < 0)
79b470
-       return -1;
79b470
-
79b470
-    bitmap = (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ?
79b470
-        macvtapIDs :  macvlanIDs;
79b470
+    unsigned int id;
79b470
+    const char *idstr = NULL;
79b470
+    int *lastID = NULL;
79b470
+    int len;
79b470
 
79b470
-    if (id > MACVLAN_MAX_ID) {
79b470
-        virReportError(VIR_ERR_INTERNAL_ERROR,
79b470
-                       _("can't use name %s%d - out of range 0-%d"),
79b470
-                       VIR_NET_GENERATED_PREFIX, id, MACVLAN_MAX_ID);
79b470
-        return -1;
79b470
+    if (STRPREFIX(name, VIR_NET_GENERATED_MACVTAP_PREFIX)) {
79b470
+        lastID = &virNetDevMacVTapLastID;
79b470
+        len = strlen(VIR_NET_GENERATED_MACVTAP_PREFIX);
79b470
+    } else if (STRPREFIX(name, VIR_NET_GENERATED_MACVLAN_PREFIX)) {
79b470
+        lastID = &virNetDevMacVTapLastID;
79b470
+        len = strlen(VIR_NET_GENERATED_MACVLAN_PREFIX);
79b470
+    } else {
79b470
+        return;
79b470
     }
79b470
 
79b470
-    if ((id < 0 || nextFree) &&
79b470
-        (id = virBitmapNextClearBit(bitmap, id)) < 0) {
79b470
-        virReportError(VIR_ERR_INTERNAL_ERROR,
79b470
-                       _("no unused %s names available"),
79b470
-                       VIR_NET_GENERATED_PREFIX);
79b470
-        return -1;
79b470
-    }
79b470
+    VIR_INFO("marking device in use: '%s'", name);
79b470
 
79b470
-    if (virBitmapIsBitSet(bitmap, id)) {
79b470
-        if (quietFail) {
79b470
-            VIR_INFO("couldn't reserve name %s%d - already in use",
79b470
-                     VIR_NET_GENERATED_PREFIX, id);
79b470
-        } else {
79b470
-            virReportError(VIR_ERR_INTERNAL_ERROR,
79b470
-                           _("couldn't reserve name %s%d - already in use"),
79b470
-                           VIR_NET_GENERATED_PREFIX, id);
79b470
-        }
79b470
-        return -1;
79b470
-    }
79b470
+    idstr = name + len;
79b470
 
79b470
-    if (virBitmapSetBit(bitmap, id) < 0) {
79b470
-        virReportError(VIR_ERR_INTERNAL_ERROR,
79b470
-                       _("couldn't mark %s%d as used"),
79b470
-                       VIR_NET_GENERATED_PREFIX, id);
79b470
-        return -1;
79b470
+    if (virStrToLong_ui(idstr, NULL, 10, &id) >= 0) {
79b470
+        if (*lastID < (int)id)
79b470
+            *lastID = id;
79b470
     }
79b470
-
79b470
-    VIR_INFO("reserving device %s%d", VIR_NET_GENERATED_PREFIX, id);
79b470
-    return id;
79b470
 }
79b470
 
79b470
 
79b470
 /**
79b470
- * virNetDevMacVLanReleaseID:
79b470
- *  @id: id 0 - MACVLAN_MAX_ID+1 to release
79b470
+ * virNetDevMacVLanReserveName:
79b470
+ * @name: name of an existing macvtap/macvlan device
79b470
  *
79b470
- *  Returns 0 for success or -1 for failure.
79b470
+ * Set the value of virNetDevMacV(Lan|Tap)LastID to assure that any
79b470
+ * new device created with an autogenerated name will use a number
79b470
+ * higher than the number in the given device name.
79b470
+ *
79b470
+ * Returns nothing.
79b470
  */
79b470
-static int
79b470
-virNetDevMacVLanReleaseID(int id, unsigned int flags)
79b470
+void
79b470
+virNetDevMacVLanReserveName(const char *name)
79b470
 {
79b470
-    virBitmapPtr bitmap;
79b470
-
79b470
-    if (virNetDevMacVLanInitialize() < 0)
79b470
-        return 0;
79b470
-
79b470
-    bitmap = (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ?
79b470
-        macvtapIDs :  macvlanIDs;
79b470
-
79b470
-    if (id > MACVLAN_MAX_ID) {
79b470
-        virReportError(VIR_ERR_INTERNAL_ERROR,
79b470
-                       _("can't free name %s%d - out of range 0-%d"),
79b470
-                       VIR_NET_GENERATED_PREFIX, id, MACVLAN_MAX_ID);
79b470
-        return -1;
79b470
-    }
79b470
-
79b470
-    if (id < 0)
79b470
-        return 0;
79b470
-
79b470
-    VIR_INFO("releasing %sdevice %s%d",
79b470
-             virBitmapIsBitSet(bitmap, id) ? "" : "unreserved",
79b470
-             VIR_NET_GENERATED_PREFIX, id);
79b470
-
79b470
-    if (virBitmapClearBit(bitmap, id) < 0) {
79b470
-        virReportError(VIR_ERR_INTERNAL_ERROR,
79b470
-                       _("couldn't mark %s%d as unused"),
79b470
-                       VIR_NET_GENERATED_PREFIX, id);
79b470
-        return -1;
79b470
-    }
79b470
-    return 0;
79b470
+    virMutexLock(&virNetDevMacVLanCreateMutex);
79b470
+    virNetDevMacVLanReserveNameInternal(name);
79b470
+    virMutexUnlock(&virNetDevMacVLanCreateMutex);
79b470
 }
79b470
 
79b470
 
79b470
 /**
79b470
- * virNetDevMacVLanReserveName:
79b470
- *
79b470
- *  @name: already-known name of device
79b470
- *  @quietFail: don't log an error if this name is already in-use
79b470
+ * virNetDevMacVLanGenerateName:
79b470
+ * @ifname: pointer to pointer to string containing template
79b470
+ * @lastID: counter to add to the template to form the name
79b470
  *
79b470
- *  Extract the device type and id from a macvtap/macvlan device name
79b470
- *  and mark the appropriate position as in-use in the appropriate
79b470
- *  bitmap.
79b470
+ * generate a new (currently unused) name for a new macvtap/macvlan
79b470
+ * device based on the template string in @ifname - replace %d with
79b470
+ * ++(*counter), and keep trying new values until one is found
79b470
+ * that doesn't already exist, or we've tried 10000 different
79b470
+ * names. Once a usable name is found, replace the template with the
79b470
+ * actual name.
79b470
  *
79b470
- *  Returns reserved ID# on success, -1 on failure, -2 if the name
79b470
- *  doesn't fit the auto-pattern (so not reserveable).
79b470
+ * Returns 0 on success, -1 on failure.
79b470
  */
79b470
-int
79b470
-virNetDevMacVLanReserveName(const char *name, bool quietFail)
79b470
+static int
79b470
+virNetDevMacVLanGenerateName(char **ifname, unsigned int flags)
79b470
 {
79b470
-    unsigned int id;
79b470
-    unsigned int flags = 0;
79b470
-    const char *idstr = NULL;
79b470
+    const char *prefix;
79b470
+    const char *iftemplate;
79b470
+    int *lastID;
79b470
+    int id;
79b470
+    double maxIDd;
79b470
+    int maxID = INT_MAX;
79b470
+    int attempts = 0;
79b470
 
79b470
-    if (virNetDevMacVLanInitialize() < 0)
79b470
-       return -1;
79b470
-
79b470
-    if (STRPREFIX(name, VIR_NET_GENERATED_MACVTAP_PREFIX)) {
79b470
-        idstr = name + strlen(VIR_NET_GENERATED_MACVTAP_PREFIX);
79b470
-        flags |= VIR_NETDEV_MACVLAN_CREATE_WITH_TAP;
79b470
-    } else if (STRPREFIX(name, VIR_NET_GENERATED_MACVLAN_PREFIX)) {
79b470
-        idstr = name + strlen(VIR_NET_GENERATED_MACVLAN_PREFIX);
79b470
+    if (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) {
79b470
+        prefix = VIR_NET_GENERATED_MACVTAP_PREFIX;
79b470
+        iftemplate = VIR_NET_GENERATED_MACVTAP_PREFIX "%d";
79b470
+        lastID = &virNetDevMacVTapLastID;
79b470
     } else {
79b470
-        return -2;
79b470
+        prefix = VIR_NET_GENERATED_MACVLAN_PREFIX;
79b470
+        iftemplate = VIR_NET_GENERATED_MACVLAN_PREFIX "%d";
79b470
+        lastID = &virNetDevMacVLanLastID;
79b470
     }
79b470
 
79b470
-    if (virStrToLong_ui(idstr, NULL, 10, &id) < 0) {
79b470
-        virReportError(VIR_ERR_INTERNAL_ERROR,
79b470
-                       _("couldn't get id value from macvtap device name %s"),
79b470
-                       name);
79b470
-        return -1;
79b470
-    }
79b470
-    return virNetDevMacVLanReserveID(id, flags, quietFail, false);
79b470
-}
79b470
+    maxIDd = pow(10, IFNAMSIZ - 1 - strlen(prefix));
79b470
+    if (maxIDd <= (double)INT_MAX)
79b470
+        maxID = (int)maxIDd;
79b470
 
79b470
+    do {
79b470
+        g_autofree char *try = NULL;
79b470
 
79b470
-/**
79b470
- * virNetDevMacVLanReleaseName:
79b470
- *
79b470
- *  @name: already-known name of device
79b470
- *
79b470
- *  Extract the device type and id from a macvtap/macvlan device name
79b470
- *  and mark the appropriate position as in-use in the appropriate
79b470
- *  bitmap.
79b470
- *
79b470
- *  returns 0 on success, -1 on failure
79b470
- */
79b470
-int
79b470
-virNetDevMacVLanReleaseName(const char *name)
79b470
-{
79b470
-    unsigned int id;
79b470
-    unsigned int flags = 0;
79b470
-    const char *idstr = NULL;
79b470
+        id = ++(*lastID);
79b470
 
79b470
-    if (virNetDevMacVLanInitialize() < 0)
79b470
-       return -1;
79b470
+        /* reset before overflow */
79b470
+        if (*lastID == maxID)
79b470
+            *lastID = -1;
79b470
 
79b470
-    if (STRPREFIX(name, VIR_NET_GENERATED_MACVTAP_PREFIX)) {
79b470
-        idstr = name + strlen(VIR_NET_GENERATED_MACVTAP_PREFIX);
79b470
-        flags |= VIR_NETDEV_MACVLAN_CREATE_WITH_TAP;
79b470
-    } else if (STRPREFIX(name, VIR_NET_GENERATED_MACVLAN_PREFIX)) {
79b470
-        idstr = name + strlen(VIR_NET_GENERATED_MACVLAN_PREFIX);
79b470
-    } else {
79b470
-        return 0;
79b470
-    }
79b470
+        try = g_strdup_printf(iftemplate, id);
79b470
 
79b470
-    if (virStrToLong_ui(idstr, NULL, 10, &id) < 0) {
79b470
-        virReportError(VIR_ERR_INTERNAL_ERROR,
79b470
-                       _("couldn't get id value from macvtap device name %s"),
79b470
-                       name);
79b470
-        return -1;
79b470
-    }
79b470
-    return virNetDevMacVLanReleaseID(id, flags);
79b470
+        if (!virNetDevExists(try)) {
79b470
+            g_free(*ifname);
79b470
+            *ifname = g_steal_pointer(&try;;
79b470
+            return 0;
79b470
+        }
79b470
+    } while (++attempts < 10000);
79b470
+
79b470
+    virReportError(VIR_ERR_INTERNAL_ERROR,
79b470
+                   _("no unused %s names available"),
79b470
+                   *ifname);
79b470
+    return -1;
79b470
 }
79b470
 
79b470
 
79b470
@@ -321,8 +232,7 @@ virNetDevMacVLanCreate(const char *ifname,
79b470
                        const char *type,
79b470
                        const virMacAddr *macaddress,
79b470
                        const char *srcdev,
79b470
-                       uint32_t macvlan_mode,
79b470
-                       int *retry)
79b470
+                       uint32_t macvlan_mode)
79b470
 {
79b470
     int error = 0;
79b470
     int ifindex = 0;
79b470
@@ -331,7 +241,6 @@ virNetDevMacVLanCreate(const char *ifname,
79b470
         .mac = macaddress,
79b470
     };
79b470
 
79b470
-    *retry = 0;
79b470
 
79b470
     if (virNetDevGetIndex(srcdev, &ifindex) < 0)
79b470
         return -1;
79b470
@@ -339,17 +248,15 @@ virNetDevMacVLanCreate(const char *ifname,
79b470
     data.ifindex = &ifindex;
79b470
     if (virNetlinkNewLink(ifname, type, &data, &error) < 0) {
79b470
         char macstr[VIR_MAC_STRING_BUFLEN];
79b470
-        if (error == -EEXIST)
79b470
-            *retry = 1;
79b470
-        else if (error < 0)
79b470
-            virReportSystemError(-error,
79b470
-                                 _("error creating %s interface %s@%s (%s)"),
79b470
-                                 type, ifname, srcdev,
79b470
-                                 virMacAddrFormat(macaddress, macstr));
79b470
 
79b470
+        virReportSystemError(-error,
79b470
+                             _("error creating %s interface %s@%s (%s)"),
79b470
+                             type, ifname, srcdev,
79b470
+                             virMacAddrFormat(macaddress, macstr));
79b470
         return -1;
79b470
     }
79b470
 
79b470
+    VIR_INFO("created device: '%s'", ifname);
79b470
     return 0;
79b470
 }
79b470
 
79b470
@@ -364,6 +271,7 @@ virNetDevMacVLanCreate(const char *ifname,
79b470
  */
79b470
 int virNetDevMacVLanDelete(const char *ifname)
79b470
 {
79b470
+    VIR_INFO("delete device: '%s'", ifname);
79b470
     return virNetlinkDelLink(ifname, NULL);
79b470
 }
79b470
 
79b470
@@ -904,13 +812,8 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifnameRequested,
79b470
                                        unsigned int flags)
79b470
 {
79b470
     const char *type = VIR_NET_GENERATED_PREFIX;
79b470
-    const char *pattern = (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ?
79b470
-        VIR_NET_GENERATED_MACVTAP_PATTERN : VIR_NET_GENERATED_MACVLAN_PATTERN;
79b470
-    int reservedID = -1;
79b470
-    char ifname[IFNAMSIZ];
79b470
-    int retries, do_retry = 0;
79b470
+    g_autofree char *ifname = NULL;
79b470
     uint32_t macvtapMode;
79b470
-    const char *ifnameCreated = NULL;
79b470
     int vf = -1;
79b470
     bool vnet_hdr = flags & VIR_NETDEV_MACVLAN_VNET_HDR;
79b470
 
79b470
@@ -945,6 +848,8 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifnameRequested,
79b470
            return -1;
79b470
     }
79b470
 
79b470
+    virMutexLock(&virNetDevMacVLanCreateMutex);
79b470
+
79b470
     if (ifnameRequested) {
79b470
         int rc;
79b470
         bool isAutoName
79b470
@@ -952,97 +857,81 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifnameRequested,
79b470
                STRPREFIX(ifnameRequested, VIR_NET_GENERATED_MACVLAN_PREFIX));
79b470
 
79b470
         VIR_INFO("Requested macvtap device name: %s", ifnameRequested);
79b470
-        virMutexLock(&virNetDevMacVLanCreateMutex);
79b470
 
79b470
         if ((rc = virNetDevExists(ifnameRequested)) < 0) {
79b470
             virMutexUnlock(&virNetDevMacVLanCreateMutex);
79b470
             return -1;
79b470
         }
79b470
+
79b470
         if (rc) {
79b470
-            if (isAutoName)
79b470
-                goto create_name;
79b470
-            virReportSystemError(EEXIST,
79b470
-                                 _("Unable to create %s device %s"),
79b470
-                                 type, ifnameRequested);
79b470
-            virMutexUnlock(&virNetDevMacVLanCreateMutex);
79b470
-            return -1;
79b470
-        }
79b470
-        if (isAutoName &&
79b470
-            (reservedID = virNetDevMacVLanReserveName(ifnameRequested, true)) < 0) {
79b470
-            reservedID = -1;
79b470
-            goto create_name;
79b470
-        }
79b470
+            /* ifnameRequested is already being used */
79b470
 
79b470
-        if (virNetDevMacVLanCreate(ifnameRequested, type, macaddress,
79b470
-                                   linkdev, macvtapMode, &do_retry) < 0) {
79b470
-            if (isAutoName) {
79b470
-                virNetDevMacVLanReleaseName(ifnameRequested);
79b470
-                reservedID = -1;
79b470
-                goto create_name;
79b470
+            if (!isAutoName) {
79b470
+                virReportSystemError(EEXIST,
79b470
+                                     _("Unable to create device '%s'"),
79b470
+                                     ifnameRequested);
79b470
+                virMutexUnlock(&virNetDevMacVLanCreateMutex);
79b470
+                return -1;
79b470
+            }
79b470
+        } else {
79b470
+
79b470
+            /* ifnameRequested is available. try to open it */
79b470
+
79b470
+            virNetDevMacVLanReserveNameInternal(ifnameRequested);
79b470
+
79b470
+            if (virNetDevMacVLanCreate(ifnameRequested, type, macaddress,
79b470
+                                       linkdev, macvtapMode) == 0) {
79b470
+
79b470
+                /* virNetDevMacVLanCreate() was successful - use this name */
79b470
+                ifname = g_strdup(ifnameRequested);
79b470
+
79b470
+            } else if (!isAutoName) {
79b470
+                /* coudn't open ifnameRequested, but it wasn't an
79b470
+                 * autogenerated named, so there is nothing else to
79b470
+                 * try - fail and return.
79b470
+                 */
79b470
+                virMutexUnlock(&virNetDevMacVLanCreateMutex);
79b470
+                return -1;
79b470
             }
79b470
-            virMutexUnlock(&virNetDevMacVLanCreateMutex);
79b470
-            return -1;
79b470
         }
79b470
-        /* virNetDevMacVLanCreate() was successful - use this name */
79b470
-        ifnameCreated = ifnameRequested;
79b470
- create_name:
79b470
-        virMutexUnlock(&virNetDevMacVLanCreateMutex);
79b470
     }
79b470
 
79b470
-    retries = MACVLAN_MAX_ID;
79b470
-    while (!ifnameCreated && retries) {
79b470
-        virMutexLock(&virNetDevMacVLanCreateMutex);
79b470
-        reservedID = virNetDevMacVLanReserveID(reservedID, flags, false, true);
79b470
-        if (reservedID < 0) {
79b470
+    if (!ifname) {
79b470
+        /* ifnameRequested was NULL, or it was an already in use
79b470
+         * autogenerated name, so now we look for an unused
79b470
+         * autogenerated name.
79b470
+         */
79b470
+        if (virNetDevMacVLanGenerateName(&ifname, flags) < 0 ||
79b470
+            virNetDevMacVLanCreate(ifname, type, macaddress,
79b470
+                                   linkdev, macvtapMode) < 0) {
79b470
             virMutexUnlock(&virNetDevMacVLanCreateMutex);
79b470
             return -1;
79b470
         }
79b470
-        g_snprintf(ifname, sizeof(ifname), pattern, reservedID);
79b470
-        if (virNetDevMacVLanCreate(ifname, type, macaddress, linkdev,
79b470
-                                   macvtapMode, &do_retry) < 0) {
79b470
-            virNetDevMacVLanReleaseID(reservedID, flags);
79b470
-            virMutexUnlock(&virNetDevMacVLanCreateMutex);
79b470
-            if (!do_retry)
79b470
-                return -1;
79b470
-            VIR_INFO("Device %s wasn't reserved but already existed, skipping",
79b470
-                     ifname);
79b470
-            retries--;
79b470
-            continue;
79b470
-        }
79b470
-        ifnameCreated = ifname;
79b470
-        virMutexUnlock(&virNetDevMacVLanCreateMutex);
79b470
     }
79b470
 
79b470
-    if (!ifnameCreated) {
79b470
-        virReportError(VIR_ERR_INTERNAL_ERROR,
79b470
-                       _("Too many unreserved %s devices in use"),
79b470
-                       type);
79b470
-        return -1;
79b470
-    }
79b470
+    /* all done creating the device */
79b470
+    virMutexUnlock(&virNetDevMacVLanCreateMutex);
79b470
 
79b470
-    if (virNetDevVPortProfileAssociate(ifnameCreated,
79b470
+    if (virNetDevVPortProfileAssociate(ifname,
79b470
                                        virtPortProfile,
79b470
                                        macaddress,
79b470
                                        linkdev,
79b470
                                        vf,
79b470
-                                       vmuuid, vmOp, false) < 0)
79b470
+                                       vmuuid, vmOp, false) < 0) {
79b470
         goto link_del_exit;
79b470
+    }
79b470
 
79b470
     if (flags & VIR_NETDEV_MACVLAN_CREATE_IFUP) {
79b470
-        if (virNetDevSetOnline(ifnameCreated, true) < 0)
79b470
+        if (virNetDevSetOnline(ifname, true) < 0)
79b470
             goto disassociate_exit;
79b470
     }
79b470
 
79b470
     if (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) {
79b470
-        if (virNetDevMacVLanTapOpen(ifnameCreated, tapfd, tapfdSize) < 0)
79b470
+        if (virNetDevMacVLanTapOpen(ifname, tapfd, tapfdSize) < 0)
79b470
             goto disassociate_exit;
79b470
 
79b470
         if (virNetDevMacVLanTapSetup(tapfd, tapfdSize, vnet_hdr) < 0)
79b470
             goto disassociate_exit;
79b470
-
79b470
-        *ifnameResult = g_strdup(ifnameCreated);
79b470
-    } else {
79b470
-        *ifnameResult = g_strdup(ifnameCreated);
79b470
     }
79b470
 
79b470
     if (vmOp == VIR_NETDEV_VPORT_PROFILE_OP_CREATE ||
79b470
@@ -1051,17 +940,18 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifnameRequested,
79b470
          * a saved image) - migration and libvirtd restart are handled
79b470
          * elsewhere.
79b470
          */
79b470
-        if (virNetDevMacVLanVPortProfileRegisterCallback(ifnameCreated, macaddress,
79b470
+        if (virNetDevMacVLanVPortProfileRegisterCallback(ifname, macaddress,
79b470
                                                          linkdev, vmuuid,
79b470
                                                          virtPortProfile,
79b470
                                                          vmOp) < 0)
79b470
             goto disassociate_exit;
79b470
     }
79b470
 
79b470
+    *ifnameResult = g_steal_pointer(&ifname);
79b470
     return 0;
79b470
 
79b470
  disassociate_exit:
79b470
-    ignore_value(virNetDevVPortProfileDisassociate(ifnameCreated,
79b470
+    ignore_value(virNetDevVPortProfileDisassociate(ifname,
79b470
                                                    virtPortProfile,
79b470
                                                    macaddress,
79b470
                                                    linkdev,
79b470
@@ -1071,9 +961,7 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifnameRequested,
79b470
         VIR_FORCE_CLOSE(tapfd[tapfdSize]);
79b470
 
79b470
  link_del_exit:
79b470
-    ignore_value(virNetDevMacVLanDelete(ifnameCreated));
79b470
-    virNetDevMacVLanReleaseName(ifnameCreated);
79b470
-
79b470
+    ignore_value(virNetDevMacVLanDelete(ifname));
79b470
     return -1;
79b470
 }
79b470
 
79b470
@@ -1107,7 +995,6 @@ int virNetDevMacVLanDeleteWithVPortProfile(const char *ifname,
79b470
             ret = -1;
79b470
         if (virNetDevMacVLanDelete(ifname) < 0)
79b470
             ret = -1;
79b470
-        virNetDevMacVLanReleaseName(ifname);
79b470
     }
79b470
 
79b470
     if (mode == VIR_NETDEV_MACVLAN_MODE_PASSTHRU) {
79b470
@@ -1182,8 +1069,7 @@ int virNetDevMacVLanCreate(const char *ifname G_GNUC_UNUSED,
79b470
                            const char *type G_GNUC_UNUSED,
79b470
                            const virMacAddr *macaddress G_GNUC_UNUSED,
79b470
                            const char *srcdev G_GNUC_UNUSED,
79b470
-                           uint32_t macvlan_mode G_GNUC_UNUSED,
79b470
-                           int *retry G_GNUC_UNUSED)
79b470
+                           uint32_t macvlan_mode G_GNUC_UNUSED)
79b470
 {
79b470
     virReportSystemError(ENOSYS, "%s",
79b470
                          _("Cannot create macvlan devices on this platform"));
79b470
@@ -1272,18 +1158,9 @@ int virNetDevMacVLanVPortProfileRegisterCallback(const char *ifname G_GNUC_UNUSE
79b470
     return -1;
79b470
 }
79b470
 
79b470
-int virNetDevMacVLanReleaseName(const char *name G_GNUC_UNUSED)
79b470
+void virNetDevMacVLanReserveName(const char *name G_GNUC_UNUSED)
79b470
 {
79b470
     virReportSystemError(ENOSYS, "%s",
79b470
                          _("Cannot create macvlan devices on this platform"));
79b470
-    return -1;
79b470
-}
79b470
-
79b470
-int virNetDevMacVLanReserveName(const char *name G_GNUC_UNUSED,
79b470
-                                bool quietFail G_GNUC_UNUSED)
79b470
-{
79b470
-    virReportSystemError(ENOSYS, "%s",
79b470
-                         _("Cannot create macvlan devices on this platform"));
79b470
-    return -1;
79b470
 }
79b470
 #endif /* ! WITH_MACVTAP */
79b470
diff --git a/src/util/virnetdevmacvlan.h b/src/util/virnetdevmacvlan.h
79b470
index fc1bb018a2..48800a8fcf 100644
79b470
--- a/src/util/virnetdevmacvlan.h
79b470
+++ b/src/util/virnetdevmacvlan.h
79b470
@@ -54,8 +54,7 @@ typedef enum {
79b470
 #define VIR_NET_GENERATED_MACVTAP_PREFIX "macvtap"
79b470
 #define VIR_NET_GENERATED_MACVLAN_PREFIX "macvlan"
79b470
 
79b470
-int virNetDevMacVLanReserveName(const char *name, bool quietfail);
79b470
-int virNetDevMacVLanReleaseName(const char *name);
79b470
+void virNetDevMacVLanReserveName(const char *name);
79b470
 
79b470
 bool virNetDevMacVLanIsMacvtap(const char *ifname)
79b470
    ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT G_GNUC_NO_INLINE;
79b470
@@ -64,8 +63,7 @@ int virNetDevMacVLanCreate(const char *ifname,
79b470
                            const char *type,
79b470
                            const virMacAddr *macaddress,
79b470
                            const char *srcdev,
79b470
-                           uint32_t macvlan_mode,
79b470
-                           int *retry)
79b470
+                           uint32_t macvlan_mode)
79b470
     ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4)
79b470
     G_GNUC_WARN_UNUSED_RESULT;
79b470
 
79b470
-- 
79b470
2.29.2
79b470