[libvirt] [PATCH 2/3] util: new function virNetDevTapAttachBridge()

Laine Stump laine at laine.org
Tue Mar 21 15:03:59 UTC 2017


This patch splits out the part of virNetDevTapCreateInBridgePort()
that would need to be re-done if an existing tap device had to be
re-attached to a bridge, and puts it into a separate function. This
can be used both when an existing domain interface config is updated
to change its connection, and also to re-attach to the "same" bridge
when a network has been stopped and restarted. So far it is used for
nothing.
---
 src/libvirt_private.syms |   1 +
 src/util/virnetdevtap.c  | 111 +++++++++++++++++++++++++++++++----------------
 src/util/virnetdevtap.h  |  12 +++++
 3 files changed, 87 insertions(+), 37 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 1d783b6..905d55a 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2106,6 +2106,7 @@ virNetDevOpenvswitchSetTimeout;
 
 
 # util/virnetdevtap.h
+virNetDevTapAttachBridge;
 virNetDevTapCreate;
 virNetDevTapCreateInBridgePort;
 virNetDevTapDelete;
diff --git a/src/util/virnetdevtap.c b/src/util/virnetdevtap.c
index 41e68f4..02ef7fd 100644
--- a/src/util/virnetdevtap.c
+++ b/src/util/virnetdevtap.c
@@ -505,6 +505,77 @@ int virNetDevTapDelete(const char *ifname ATTRIBUTE_UNUSED,
 
 
 /**
+ * virNetDevTapAttachBridge:
+ * @tapname: the tap interface name (or name template)
+ * @brname: the bridge name
+ * @macaddr: desired MAC address
+ * @virtPortProfile: bridge/port specific configuration
+ * @virtVlan: vlan tag info
+ * @mtu: requested MTU for port (or 0 for "default")
+ * @actualMTU: MTU actually set for port (after accounting for bridge's MTU)
+ *
+ * This attaches an existing tap device (@tapname) to a bridge
+ * (@brname).
+ *
+ * Returns 0 in case of success or -1 on failure
+ */
+int
+virNetDevTapAttachBridge(const char *tapname,
+                         const char *brname,
+                         const virMacAddr *macaddr,
+                         const unsigned char *vmuuid,
+                         virNetDevVPortProfilePtr virtPortProfile,
+                         virNetDevVlanPtr virtVlan,
+                         unsigned int mtu,
+                         unsigned int *actualMTU)
+{
+    /* If an MTU is specified for the new device, set it before
+     * attaching the device to the bridge, as it may affect the MTU of
+     * the bridge (in particular if it is the first device attached to
+     * the bridge, or if it is smaller than the current MTU of the
+     * bridge). If MTU isn't specified for the new device (i.e. 0),
+     * we need to set the interface MTU to the current MTU of the
+     * bridge (to avoid inadvertantly changing the bridge's MTU).
+     */
+    if (mtu > 0) {
+        if (virNetDevSetMTU(tapname, mtu) < 0)
+            goto error;
+    } else {
+        if (virNetDevSetMTUFromDevice(tapname, brname) < 0)
+            goto error;
+    }
+    if (actualMTU) {
+        int retMTU = virNetDevGetMTU(tapname);
+
+        if (retMTU < 0)
+            goto error;
+
+        *actualMTU = retMTU;
+    }
+
+
+    if (virtPortProfile) {
+        if (virtPortProfile->virtPortType == VIR_NETDEV_VPORT_PROFILE_MIDONET) {
+            if (virNetDevMidonetBindPort(tapname, virtPortProfile) < 0)
+                goto error;
+        } else if (virtPortProfile->virtPortType == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH) {
+            if (virNetDevOpenvswitchAddPort(brname, tapname, macaddr, vmuuid,
+                                            virtPortProfile, virtVlan) < 0)
+                goto error;
+        }
+    } else {
+        if (virNetDevBridgeAddPort(brname, tapname) < 0)
+            goto error;
+    }
+
+    return 0;
+
+ error:
+    return -1;
+}
+
+
+/**
  * virNetDevTapCreateInBridgePort:
  * @brname: the bridge name
  * @ifname: the interface name (or name template)
@@ -582,43 +653,9 @@ int virNetDevTapCreateInBridgePort(const char *brname,
     if (virNetDevSetMAC(*ifname, &tapmac) < 0)
         goto error;
 
-    /* If an MTU is specified for the new device, set it before
-     * attaching the device to the bridge, as it may affect the MTU of
-     * the bridge (in particular if it is the first device attached to
-     * the bridge, or if it is smaller than the current MTU of the
-     * bridge). If MTU isn't specified for the new device (i.e. 0),
-     * we need to set the interface MTU to the current MTU of the
-     * bridge (to avoid inadvertantly changing the bridge's MTU).
-     */
-    if (mtu > 0) {
-        if (virNetDevSetMTU(*ifname, mtu) < 0)
-            goto error;
-    } else {
-        if (virNetDevSetMTUFromDevice(*ifname, brname) < 0)
-            goto error;
-    }
-    if (actualMTU) {
-        int retMTU = virNetDevGetMTU(*ifname);
-
-        if (retMTU < 0)
-            goto error;
-
-        *actualMTU = retMTU;
-    }
-
-
-    if (virtPortProfile) {
-        if (virtPortProfile->virtPortType == VIR_NETDEV_VPORT_PROFILE_MIDONET) {
-            if (virNetDevMidonetBindPort(*ifname, virtPortProfile) < 0)
-                goto error;
-        } else if (virtPortProfile->virtPortType == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH) {
-            if (virNetDevOpenvswitchAddPort(brname, *ifname, macaddr, vmuuid,
-                                            virtPortProfile, virtVlan) < 0)
-                goto error;
-        }
-    } else {
-        if (virNetDevBridgeAddPort(brname, *ifname) < 0)
-            goto error;
+    if (virNetDevTapAttachBridge(*ifname, brname, macaddr, vmuuid,
+                                 virtPortProfile, virtVlan, mtu, actualMTU) < 0) {
+        goto error;
     }
 
     if (virNetDevSetOnline(*ifname, !!(flags & VIR_NETDEV_TAP_CREATE_IFUP)) < 0)
diff --git a/src/util/virnetdevtap.h b/src/util/virnetdevtap.h
index 6441df4..6bb3b88 100644
--- a/src/util/virnetdevtap.h
+++ b/src/util/virnetdevtap.h
@@ -62,6 +62,18 @@ typedef enum {
    VIR_NETDEV_TAP_CREATE_PERSIST            = 1 << 3,
 } virNetDevTapCreateFlags;
 
+int
+virNetDevTapAttachBridge(const char *tapname,
+                         const char *brname,
+                         const virMacAddr *macaddr,
+                         const unsigned char *vmuuid,
+                         virNetDevVPortProfilePtr virtPortProfile,
+                         virNetDevVlanPtr virtVlan,
+                         unsigned int mtu,
+                         unsigned int *actualMTU)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
+    ATTRIBUTE_RETURN_CHECK;
+
 int virNetDevTapCreateInBridgePort(const char *brname,
                                    char **ifname,
                                    const virMacAddr *macaddr,
-- 
2.9.3




More information about the libvir-list mailing list