]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: simplify virNetDevMacVLanCreateWithVPortProfile()
authorLaine Stump <laine@redhat.com>
Tue, 15 Dec 2020 19:50:40 +0000 (14:50 -0500)
committerLaine Stump <laine@redhat.com>
Thu, 17 Dec 2020 02:32:01 +0000 (21:32 -0500)
Since commit 282d135ddbb the parser for <interface> has cleared out
any interface name from the input XML that used the macvtap/macvlan
name as a prefix. Along with that, the switch to use the new
virNetDevGenerateName() function for auto-generating macvtap/macvlan
device names (commit 9b5d741a9), has realized two facts:

1) virNetDevGenerateName() can be called with a name already filled
   in, and in that case it is an effective NOP.

2) because virNetDevGenerate() will always find an unused name, there
   is no need to retry device creation in a loop - if it fails the
   first time, it would fail any subsequent time as well.

that, combined with the aforementioned parser change allow us to
simplify virNetDevMacVLanCreateWithVPortProfile() - we no longer need
any extra code to determine if a template "AutoName" was requested,
and don't need a separate code path for creating the device in the
case that a specific name was given in the XML - all we need to do is
log any requested name, and then call exactly the same code as we
would if no name was given.

Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/util/virnetdevmacvlan.c

index a4ad69833573a74bc65797b528f78b7caa85dc1e..2deefe6589ef44b398d209915502500cf5a1ba7b 100644 (file)
@@ -673,6 +673,7 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifnameRequested,
     uint32_t macvtapMode;
     int vf = -1;
     bool vnet_hdr = flags & VIR_NETDEV_MACVLAN_VNET_HDR;
+    virNetDevGenNameType type;
 
     macvtapMode = modeMap[mode];
 
@@ -706,66 +707,21 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifnameRequested,
     }
 
     if (ifnameRequested) {
-        int rc;
-        bool isAutoName
-            = (STRPREFIX(ifnameRequested, VIR_NET_GENERATED_MACVTAP_PREFIX) ||
-               STRPREFIX(ifnameRequested, VIR_NET_GENERATED_MACVLAN_PREFIX));
-
         VIR_INFO("Requested macvtap device name: %s", ifnameRequested);
-
-        if ((rc = virNetDevExists(ifnameRequested)) < 0)
-            return -1;
-
-        if (rc) {
-            /* ifnameRequested is already being used */
-
-            if (!isAutoName) {
-                virReportSystemError(EEXIST,
-                                     _("Unable to create device '%s'"),
-                                     ifnameRequested);
-                return -1;
-            }
-        } else {
-
-            /* ifnameRequested is available. try to open it */
-
-            virNetDevReserveName(ifnameRequested);
-
-            if (virNetDevMacVLanCreate(ifnameRequested, macaddress,
-                                       linkdev, macvtapMode, flags) == 0) {
-
-                /* virNetDevMacVLanCreate() was successful - use this name */
-                ifname = g_strdup(ifnameRequested);
-
-            } else if (!isAutoName) {
-                /* couldn't open ifnameRequested, but it wasn't an
-                 * autogenerated named, so there is nothing else to
-                 * try - fail and return.
-                 */
-                return -1;
-            }
-        }
+        ifname = g_strdup(ifnameRequested);
     }
 
-    if (!ifname) {
-        /* ifnameRequested was NULL, or it was an already in use
-         * autogenerated name, so now we look for an unused
-         * autogenerated name.
-         */
-        virNetDevGenNameType type;
-        if (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP)
-            type = VIR_NET_DEV_GEN_NAME_MACVTAP;
-        else
-            type = VIR_NET_DEV_GEN_NAME_MACVLAN;
+    if (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP)
+        type = VIR_NET_DEV_GEN_NAME_MACVTAP;
+    else
+        type = VIR_NET_DEV_GEN_NAME_MACVLAN;
 
-        if (virNetDevGenerateName(&ifname, type) < 0 ||
-            virNetDevMacVLanCreate(ifname, macaddress,
-                                   linkdev, macvtapMode, flags) < 0)
-            return -1;
+    if (virNetDevGenerateName(&ifname, type) < 0 ||
+        virNetDevMacVLanCreate(ifname, macaddress,
+                               linkdev, macvtapMode, flags) < 0) {
+        return -1;
     }
 
-    /* all done creating the device */
-
     if (virNetDevVPortProfileAssociate(ifname,
                                        virtPortProfile,
                                        macaddress,