]> xenbits.xensource.com Git - libvirt.git/commitdiff
qemu: always use virDomainNetGetActualBridgeName to get interface's bridge
authorLaine Stump <laine@laine.org>
Fri, 21 Nov 2014 17:51:13 +0000 (12:51 -0500)
committerLaine Stump <laine@laine.org>
Mon, 8 Dec 2014 19:50:50 +0000 (14:50 -0500)
qemuNetworkIfaceConnect() used to have a special case for
actualType='network' (a network with forward mode of route, nat, or
isolated) to call the libvirt public API to retrieve the bridge being
used by a network. That is no longer necessary - since all network
types that use a bridge and tap device now get the bridge name stored
in the ActualNetDef, we can just always use
virDomainNetGetActualBridgeName() instead.

(an audit of the two callers to qemuNetworkIfaceConnect() confirms
that it is never called for any other type of network, so the dead
code in the else statement (logging an internal error if it is called
for any other type of network) is eliminated in the process.)

src/qemu/qemu_command.c
src/qemu/qemu_hotplug.c

index 11ed58b8471153b9b63d37410a6b9d46277e470f..48bdf4ebe4465abafcd2d5bd78de8e4650d0e48c 100644 (file)
@@ -277,6 +277,11 @@ static int qemuCreateInBridgePortWithHelper(virQEMUDriverConfigPtr cfg,
     return *tapfd < 0 ? -1 : 0;
 }
 
+/* qemuNetworkIfaceConnect - *only* called if actualType is
+ * VIR_DOMAIN_NET_TYPE_NETWORK or VIR_DOMAIN_NET_TYPE_BRIDGE (i.e. if
+ * the connection is made with a tap device connecting to a bridge
+ * device)
+ */
 int
 qemuNetworkIfaceConnect(virDomainDefPtr def,
                         virConnectPtr conn,
@@ -286,39 +291,19 @@ qemuNetworkIfaceConnect(virDomainDefPtr def,
                         int *tapfd,
                         int *tapfdSize)
 {
-    char *brname = NULL;
+    const char *brname;
     int ret = -1;
     unsigned int tap_create_flags = VIR_NETDEV_TAP_CREATE_IFUP;
     bool template_ifname = false;
-    int actualType = virDomainNetGetActualType(net);
     virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
     const char *tunpath = "/dev/net/tun";
 
     if (net->backend.tap)
         tunpath = net->backend.tap;
 
-    if (actualType == VIR_DOMAIN_NET_TYPE_NETWORK) {
-        bool fail = false;
-        virNetworkPtr network = virNetworkLookupByName(conn,
-                                                       net->data.network.name);
-        if (!network)
-            return ret;
-
-        if (!(brname = virNetworkGetBridgeName(network)))
-           fail = true;
-
-        virObjectUnref(network);
-        if (fail)
-            return ret;
-
-    } else if (actualType == VIR_DOMAIN_NET_TYPE_BRIDGE) {
-        if (VIR_STRDUP(brname, virDomainNetGetActualBridgeName(net)) < 0)
-            return ret;
-    } else {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Network type %d is not supported"),
-                       virDomainNetGetActualType(net));
-        return ret;
+    if (!(brname = virDomainNetGetActualBridgeName(net))) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing bridge name"));
+        goto cleanup;
     }
 
     if (!net->ifname ||
@@ -400,7 +385,6 @@ qemuNetworkIfaceConnect(virDomainDefPtr def,
         if (template_ifname)
             VIR_FREE(net->ifname);
     }
-    VIR_FREE(brname);
     virObjectUnref(cfg);
 
     return ret;
index 9467d7d937e7001e1c298bbae49dd96feae17f07..0d97b579969c6890327b23b158818d401fd0f06d 100644 (file)
@@ -1746,58 +1746,20 @@ static virDomainNetDefPtr *qemuDomainFindNet(virDomainObjPtr vm,
     return NULL;
 }
 
-static char *
-qemuDomainNetGetBridgeName(virConnectPtr conn, virDomainNetDefPtr net)
-{
-    char *brname = NULL;
-    int actualType = virDomainNetGetActualType(net);
-
-    if (actualType == VIR_DOMAIN_NET_TYPE_BRIDGE) {
-        const char *tmpbr = virDomainNetGetActualBridgeName(net);
-        if (!tmpbr) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("interface is missing bridge name"));
-            goto cleanup;
-        }
-        /* we need a copy, not just a pointer to the original */
-        if (VIR_STRDUP(brname, tmpbr) < 0)
-            goto cleanup;
-    } else if (actualType == VIR_DOMAIN_NET_TYPE_NETWORK) {
-        virNetworkPtr network;
-
-        if (!(network = virNetworkLookupByName(conn, net->data.network.name))) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("Couldn't find network '%s'"),
-                           net->data.network.name);
-            goto cleanup;
-        }
-        brname = virNetworkGetBridgeName(network);
-
-        virObjectUnref(network);
-    } else {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Interface type %d has no bridge name"),
-                       virDomainNetGetActualType(net));
-    }
-
- cleanup:
-    return brname;
-}
 
 static int
-qemuDomainChangeNetBridge(virConnectPtr conn,
-                          virDomainObjPtr vm,
+qemuDomainChangeNetBridge(virDomainObjPtr vm,
                           virDomainNetDefPtr olddev,
                           virDomainNetDefPtr newdev)
 {
     int ret = -1;
-    char *oldbridge = NULL, *newbridge = NULL;
+    const char *oldbridge = virDomainNetGetActualBridgeName(olddev);
+    const char *newbridge = virDomainNetGetActualBridgeName(newdev);
 
-    if (!(oldbridge = qemuDomainNetGetBridgeName(conn, olddev)))
-        goto cleanup;
-
-    if (!(newbridge = qemuDomainNetGetBridgeName(conn, newdev)))
+    if (!oldbridge || !newbridge) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing bridge name"));
         goto cleanup;
+    }
 
     VIR_DEBUG("Change bridge for interface %s: %s -> %s",
               olddev->ifname, oldbridge, newbridge);
@@ -1836,8 +1798,6 @@ qemuDomainChangeNetBridge(virConnectPtr conn,
     /* caller will replace entire olddev with newdev in domain nets list */
     ret = 0;
  cleanup:
-    VIR_FREE(oldbridge);
-    VIR_FREE(newbridge);
     return ret;
 }
 
@@ -2231,7 +2191,7 @@ qemuDomainChangeNet(virQEMUDriverPtr driver,
     }
 
     if (needBridgeChange) {
-        if (qemuDomainChangeNetBridge(dom->conn, vm, olddev, newdev) < 0)
+        if (qemuDomainChangeNetBridge(vm, olddev, newdev) < 0)
             goto cleanup;
         /* we successfully switched to the new bridge, and we've
          * determined that the rest of newdev is equivalent to olddev,