Blame SOURCES/libvirt-util-make-macvtap-macvlan-generated-name-defines-available-to-other-files.patch

3e5111
From 30400fc580740ddca6edd751d1768f189cfd112a Mon Sep 17 00:00:00 2001
3e5111
Message-Id: <30400fc580740ddca6edd751d1768f189cfd112a@dist-git>
3e5111
From: Laine Stump <laine@laine.org>
3e5111
Date: Tue, 2 May 2017 12:33:10 -0400
3e5111
Subject: [PATCH] util: make macvtap/macvlan generated name #defines available
3e5111
 to other files
3e5111
3e5111
MACVTAP_NAME_PREFIX and MACVLAN_NAME_PREFIX could be useful to other
3e5111
files if they were defined in virnetdevmacvlan.h instead of
3e5111
virnetdevmacvlan.c, so do that (while slightly renaming them and also
3e5111
adding yet another #define that chooses between macvlan/macvtap based
3e5111
on flags).
3e5111
3e5111
This is a prerequisite to fix: https://bugzilla.redhat.com/1335798
3e5111
3e5111
(cherry picked from commit a05400ef55c7c67c6b211dfa5a33400870d470ca)
3e5111
3e5111
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
3e5111
---
3e5111
 src/util/virnetdevmacvlan.c | 65 ++++++++++++++++++---------------------------
3e5111
 src/util/virnetdevmacvlan.h |  6 +++++
3e5111
 2 files changed, 32 insertions(+), 39 deletions(-)
3e5111
3e5111
diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c
3e5111
index 7222b0f2c..97c87701c 100644
3e5111
--- a/src/util/virnetdevmacvlan.c
3e5111
+++ b/src/util/virnetdevmacvlan.c
3e5111
@@ -68,11 +68,11 @@ VIR_ENUM_IMPL(virNetDevMacVLanMode, VIR_NETDEV_MACVLAN_MODE_LAST,
3e5111
 
3e5111
 VIR_LOG_INIT("util.netdevmacvlan");
3e5111
 
3e5111
-# define MACVTAP_NAME_PREFIX	"macvtap"
3e5111
-# define MACVTAP_NAME_PATTERN	"macvtap%d"
3e5111
-
3e5111
-# define MACVLAN_NAME_PREFIX	"macvlan"
3e5111
-# define MACVLAN_NAME_PATTERN	"macvlan%d"
3e5111
+# define VIR_NET_GENERATED_MACVTAP_PATTERN VIR_NET_GENERATED_MACVTAP_PREFIX "%d"
3e5111
+# define VIR_NET_GENERATED_MACVLAN_PATTERN VIR_NET_GENERATED_MACVLAN_PREFIX "%d"
3e5111
+# define VIR_NET_GENERATED_PREFIX \
3e5111
+    ((flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ? \
3e5111
+     VIR_NET_GENERATED_MACVTAP_PREFIX : VIR_NET_GENERATED_MACVLAN_PREFIX)
3e5111
 
3e5111
 # define MACVLAN_MAX_ID 8191
3e5111
 
3e5111
@@ -124,9 +124,7 @@ virNetDevMacVLanReserveID(int id, unsigned int flags,
3e5111
     if (id > MACVLAN_MAX_ID) {
3e5111
         virReportError(VIR_ERR_INTERNAL_ERROR,
3e5111
                        _("can't use name %s%d - out of range 0-%d"),
3e5111
-                       (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ?
3e5111
-                       MACVTAP_NAME_PREFIX : MACVLAN_NAME_PREFIX,
3e5111
-                       id, MACVLAN_MAX_ID);
3e5111
+                       VIR_NET_GENERATED_PREFIX, id, MACVLAN_MAX_ID);
3e5111
         return -1;
3e5111
     }
3e5111
 
3e5111
@@ -134,21 +132,18 @@ virNetDevMacVLanReserveID(int id, unsigned int flags,
3e5111
         (id = virBitmapNextClearBit(bitmap, id)) < 0) {
3e5111
         virReportError(VIR_ERR_INTERNAL_ERROR,
3e5111
                        _("no unused %s names available"),
3e5111
-                       (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ?
3e5111
-                       MACVTAP_NAME_PREFIX : MACVLAN_NAME_PREFIX);
3e5111
+                       VIR_NET_GENERATED_PREFIX);
3e5111
         return -1;
3e5111
     }
3e5111
 
3e5111
     if (virBitmapIsBitSet(bitmap, id)) {
3e5111
         if (quietFail) {
3e5111
             VIR_INFO("couldn't reserve name %s%d - already in use",
3e5111
-                     (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ?
3e5111
-                     MACVTAP_NAME_PREFIX : MACVLAN_NAME_PREFIX, id);
3e5111
+                     VIR_NET_GENERATED_PREFIX, id);
3e5111
         } else {
3e5111
             virReportError(VIR_ERR_INTERNAL_ERROR,
3e5111
                            _("couldn't reserve name %s%d - already in use"),
3e5111
-                           (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ?
3e5111
-                           MACVTAP_NAME_PREFIX : MACVLAN_NAME_PREFIX, id);
3e5111
+                           VIR_NET_GENERATED_PREFIX, id);
3e5111
         }
3e5111
         return -1;
3e5111
     }
3e5111
@@ -156,14 +151,11 @@ virNetDevMacVLanReserveID(int id, unsigned int flags,
3e5111
     if (virBitmapSetBit(bitmap, id) < 0) {
3e5111
         virReportError(VIR_ERR_INTERNAL_ERROR,
3e5111
                        _("couldn't mark %s%d as used"),
3e5111
-                       (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ?
3e5111
-                       MACVTAP_NAME_PREFIX : MACVLAN_NAME_PREFIX, id);
3e5111
+                       VIR_NET_GENERATED_PREFIX, id);
3e5111
         return -1;
3e5111
     }
3e5111
 
3e5111
-    VIR_INFO("reserving device %s%d",
3e5111
-             (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ?
3e5111
-             MACVTAP_NAME_PREFIX : MACVLAN_NAME_PREFIX, id);
3e5111
+    VIR_INFO("reserving device %s%d", VIR_NET_GENERATED_PREFIX, id);
3e5111
     return id;
3e5111
 }
3e5111
 
3e5111
@@ -188,9 +180,7 @@ virNetDevMacVLanReleaseID(int id, unsigned int flags)
3e5111
     if (id > MACVLAN_MAX_ID) {
3e5111
         virReportError(VIR_ERR_INTERNAL_ERROR,
3e5111
                        _("can't free name %s%d - out of range 0-%d"),
3e5111
-                       (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ?
3e5111
-                       MACVTAP_NAME_PREFIX : MACVLAN_NAME_PREFIX,
3e5111
-                       id, MACVLAN_MAX_ID);
3e5111
+                       VIR_NET_GENERATED_PREFIX, id, MACVLAN_MAX_ID);
3e5111
         return -1;
3e5111
     }
3e5111
 
3e5111
@@ -199,14 +189,12 @@ virNetDevMacVLanReleaseID(int id, unsigned int flags)
3e5111
 
3e5111
     VIR_INFO("releasing %sdevice %s%d",
3e5111
              virBitmapIsBitSet(bitmap, id) ? "" : "unreserved",
3e5111
-             (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ?
3e5111
-             MACVTAP_NAME_PREFIX : MACVLAN_NAME_PREFIX, id);
3e5111
+             VIR_NET_GENERATED_PREFIX, id);
3e5111
 
3e5111
     if (virBitmapClearBit(bitmap, id) < 0) {
3e5111
         virReportError(VIR_ERR_INTERNAL_ERROR,
3e5111
                        _("couldn't mark %s%d as unused"),
3e5111
-                       (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ?
3e5111
-                       MACVTAP_NAME_PREFIX : MACVLAN_NAME_PREFIX, id);
3e5111
+                       VIR_NET_GENERATED_PREFIX, id);
3e5111
         return -1;
3e5111
     }
3e5111
     return 0;
3e5111
@@ -236,11 +224,11 @@ virNetDevMacVLanReserveName(const char *name, bool quietFail)
3e5111
     if (virNetDevMacVLanInitialize() < 0)
3e5111
        return -1;
3e5111
 
3e5111
-    if (STRPREFIX(name, MACVTAP_NAME_PREFIX)) {
3e5111
-        idstr = name + strlen(MACVTAP_NAME_PREFIX);
3e5111
+    if (STRPREFIX(name, VIR_NET_GENERATED_MACVTAP_PREFIX)) {
3e5111
+        idstr = name + strlen(VIR_NET_GENERATED_MACVTAP_PREFIX);
3e5111
         flags |= VIR_NETDEV_MACVLAN_CREATE_WITH_TAP;
3e5111
-    } else if (STRPREFIX(name, MACVLAN_NAME_PREFIX)) {
3e5111
-        idstr = name + strlen(MACVLAN_NAME_PREFIX);
3e5111
+    } else if (STRPREFIX(name, VIR_NET_GENERATED_MACVLAN_PREFIX)) {
3e5111
+        idstr = name + strlen(VIR_NET_GENERATED_MACVLAN_PREFIX);
3e5111
     } else {
3e5111
         return -2;
3e5111
     }
3e5111
@@ -276,11 +264,11 @@ virNetDevMacVLanReleaseName(const char *name)
3e5111
     if (virNetDevMacVLanInitialize() < 0)
3e5111
        return -1;
3e5111
 
3e5111
-    if (STRPREFIX(name, MACVTAP_NAME_PREFIX)) {
3e5111
-        idstr = name + strlen(MACVTAP_NAME_PREFIX);
3e5111
+    if (STRPREFIX(name, VIR_NET_GENERATED_MACVTAP_PREFIX)) {
3e5111
+        idstr = name + strlen(VIR_NET_GENERATED_MACVTAP_PREFIX);
3e5111
         flags |= VIR_NETDEV_MACVLAN_CREATE_WITH_TAP;
3e5111
-    } else if (STRPREFIX(name, MACVLAN_NAME_PREFIX)) {
3e5111
-        idstr = name + strlen(MACVLAN_NAME_PREFIX);
3e5111
+    } else if (STRPREFIX(name, VIR_NET_GENERATED_MACVLAN_PREFIX)) {
3e5111
+        idstr = name + strlen(VIR_NET_GENERATED_MACVLAN_PREFIX);
3e5111
     } else {
3e5111
         return 0;
3e5111
     }
3e5111
@@ -985,10 +973,9 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifnameRequested,
3e5111
                                        size_t tapfdSize,
3e5111
                                        unsigned int flags)
3e5111
 {
3e5111
-    const char *type = (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ?
3e5111
-        MACVTAP_NAME_PREFIX : MACVLAN_NAME_PREFIX;
3e5111
+    const char *type = VIR_NET_GENERATED_PREFIX;
3e5111
     const char *pattern = (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ?
3e5111
-        MACVTAP_NAME_PATTERN : MACVLAN_NAME_PATTERN;
3e5111
+        VIR_NET_GENERATED_MACVTAP_PATTERN : VIR_NET_GENERATED_MACVLAN_PATTERN;
3e5111
     int reservedID = -1;
3e5111
     char ifname[IFNAMSIZ];
3e5111
     int retries, do_retry = 0;
3e5111
@@ -1031,8 +1018,8 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifnameRequested,
3e5111
     if (ifnameRequested) {
3e5111
         int rc;
3e5111
         bool isAutoName
3e5111
-            = (STRPREFIX(ifnameRequested, MACVTAP_NAME_PREFIX) ||
3e5111
-               STRPREFIX(ifnameRequested, MACVLAN_NAME_PREFIX));
3e5111
+            = (STRPREFIX(ifnameRequested, VIR_NET_GENERATED_MACVTAP_PREFIX) ||
3e5111
+               STRPREFIX(ifnameRequested, VIR_NET_GENERATED_MACVLAN_PREFIX));
3e5111
 
3e5111
         VIR_INFO("Requested macvtap device name: %s", ifnameRequested);
3e5111
         virMutexLock(&virNetDevMacVLanCreateMutex);
3e5111
diff --git a/src/util/virnetdevmacvlan.h b/src/util/virnetdevmacvlan.h
3e5111
index c40f23ed2..a7c4b6d9c 100644
3e5111
--- a/src/util/virnetdevmacvlan.h
3e5111
+++ b/src/util/virnetdevmacvlan.h
3e5111
@@ -51,6 +51,12 @@ typedef enum {
3e5111
    VIR_NETDEV_MACVLAN_VNET_HDR          = 1 << 2,
3e5111
 } virNetDevMacVLanCreateFlags;
3e5111
 
3e5111
+/* libvirt will start macvtap/macvlan interface names with one of
3e5111
+ * these prefixes when it auto-generates the name
3e5111
+ */
3e5111
+# define VIR_NET_GENERATED_MACVTAP_PREFIX "macvtap"
3e5111
+# define VIR_NET_GENERATED_MACVLAN_PREFIX "macvlan"
3e5111
+
3e5111
 int virNetDevMacVLanReserveName(const char *name, bool quietfail);
3e5111
 int virNetDevMacVLanReleaseName(const char *name);
3e5111
 
3e5111
-- 
3e5111
2.12.2
3e5111