]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: new function virNetDevTapAttachBridge()
authorLaine Stump <laine@laine.org>
Sat, 18 Mar 2017 18:03:20 +0000 (14:03 -0400)
committerLaine Stump <laine@laine.org>
Wed, 22 Mar 2017 16:16:51 +0000 (12:16 -0400)
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
src/util/virnetdevtap.c
src/util/virnetdevtap.h

index b3fd6077e34212f67f411565b80d2cd5119ab520..a1c7624246ff03bcfc96c7b2aad4b39ff7f90ad2 100644 (file)
@@ -2107,6 +2107,7 @@ virNetDevOpenvswitchSetTimeout;
 
 
 # util/virnetdevtap.h
+virNetDevTapAttachBridge;
 virNetDevTapCreate;
 virNetDevTapCreateInBridgePort;
 virNetDevTapDelete;
index 41e68f4a69c493638ee0f970a457b6d18ff7a463..02ef7fd24047368f1d2a623f3e4449b0086948f7 100644 (file)
@@ -504,6 +504,77 @@ int virNetDevTapDelete(const char *ifname ATTRIBUTE_UNUSED,
 #endif
 
 
+/**
+ * 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
@@ -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)
index 6441df49150045c4da9ba924bb369f8f7c91ca9b..6bb3b8891c2165627e3ffa79792f7c2920bdc87c 100644 (file)
@@ -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,