[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 8/9] qemu: always use virDomainNetGetActualBridgeName to get interface's bridge



INFORMATIONAL ONLY - NO NEED TO REVIEW, WILL NOT BE PUSHED

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 | 35 +++++------------------------
 src/qemu/qemu_hotplug.c | 60 ++++++-------------------------------------------
 2 files changed, 13 insertions(+), 82 deletions(-)

diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index d3d129a..57815fb 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -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,
@@ -290,42 +295,14 @@ qemuNetworkIfaceConnect(virDomainDefPtr def,
     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;
-        virErrorPtr errobj;
-        virNetworkPtr network = virNetworkLookupByName(conn,
-                                                       net->data.network.name);
-        if (!network)
-            return ret;
-
-        if (!(brname = virNetworkGetBridgeName(network)))
-           fail = true;
-
-        /* Make sure any above failure is preserved */
-        errobj = virSaveLastError();
-        virNetworkFree(network);
-        virSetError(errobj);
-        virFreeError(errobj);
-
-        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));
+    if (VIR_STRDUP(brname, virDomainNetGetActualBridgeName(net)) < 0)
         return ret;
-    }
 
     if (!net->ifname ||
         STRPREFIX(net->ifname, VIR_NET_GENERATED_PREFIX) ||
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index b00fd8f..b530199 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -1746,64 +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) {
-        virErrorPtr errobj;
-        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);
-
-        /* Make sure any above failure is preserved */
-        errobj = virSaveLastError();
-        virNetworkFree(network);
-        virSetError(errobj);
-        virFreeError(errobj);
-
-    } 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);
@@ -1842,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;
 }
 
@@ -2237,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,
-- 
1.9.3


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]