[PATCH v1 08/12] qemu: Rework qemuNetworkIfaceConnect

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

 



For future work it's better, if tapfd is passed as pointer.
Moreover, we need to be able to return multiple values now.
---
 src/qemu/qemu_command.c | 70 +++++++++++++++++++++++++------------------------
 src/qemu/qemu_command.h |  4 ++-
 src/qemu/qemu_hotplug.c |  4 +--
 3 files changed, 41 insertions(+), 37 deletions(-)

diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 7337069..58c81d7 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -199,11 +199,12 @@ qemuNetworkIfaceConnect(virDomainDefPtr def,
                         virConnectPtr conn,
                         virQEMUDriverPtr driver,
                         virDomainNetDefPtr net,
-                        virQEMUCapsPtr qemuCaps)
+                        virQEMUCapsPtr qemuCaps,
+                        int *tapfd,
+                        int tapfdSize)
 {
     char *brname = NULL;
-    int err;
-    int tapfd = -1;
+    int ret = -1;
     unsigned int tap_create_flags = VIR_NETDEV_TAP_CREATE_IFUP;
     bool template_ifname = false;
     int actualType = virDomainNetGetActualType(net);
@@ -215,7 +216,7 @@ qemuNetworkIfaceConnect(virDomainDefPtr def,
         virNetworkPtr network = virNetworkLookupByName(conn,
                                                        net->data.network.name);
         if (!network)
-            return -1;
+            return ret;
 
         active = virNetworkIsActive(network);
         if (active != 1) {
@@ -240,18 +241,18 @@ qemuNetworkIfaceConnect(virDomainDefPtr def,
         virFreeError(errobj);
 
         if (fail)
-            return -1;
+            return ret;
 
     } else if (actualType == VIR_DOMAIN_NET_TYPE_BRIDGE) {
         if (!(brname = strdup(virDomainNetGetActualBridgeName(net)))) {
             virReportOOMError();
-            return -1;
+            return ret;
         }
     } else {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Network type %d is not supported"),
                        virDomainNetGetActualType(net));
-        return -1;
+        return ret;
     }
 
     if (!net->ifname ||
@@ -271,49 +272,51 @@ qemuNetworkIfaceConnect(virDomainDefPtr def,
         tap_create_flags |= VIR_NETDEV_TAP_CREATE_VNET_HDR;
     }
 
-    err = virNetDevTapCreateInBridgePort(brname, &net->ifname, &net->mac,
-                                         def->uuid, &tapfd, 1,
-                                         virDomainNetGetActualVirtPortProfile(net),
-                                         virDomainNetGetActualVlan(net),
-                                         tap_create_flags);
-    virDomainAuditNetDevice(def, net, "/dev/net/tun", tapfd >= 0);
-    if (err < 0) {
+    if (virNetDevTapCreateInBridgePort(brname, &net->ifname, &net->mac,
+                                       def->uuid, tapfd, tapfdSize,
+                                       virDomainNetGetActualVirtPortProfile(net),
+                                       virDomainNetGetActualVlan(net),
+                                       tap_create_flags) < 0) {
+        virDomainAuditNetDevice(def, net, "/dev/net/tun", false);
         if (template_ifname)
             VIR_FREE(net->ifname);
-        tapfd = -1;
+        goto cleanup;
     }
+    virDomainAuditNetDevice(def, net, "/dev/net/tun", true);
 
-    if (cfg->macFilter) {
-        if ((err = networkAllowMacOnPort(driver, net->ifname, &net->mac))) {
-            virReportSystemError(err,
-                 _("failed to add ebtables rule to allow MAC address on '%s'"),
-                                 net->ifname);
-        }
+    if (cfg->macFilter &&
+        (ret = networkAllowMacOnPort(driver, net->ifname, &net->mac)) < 0) {
+        virReportSystemError(ret,
+                             _("failed to add ebtables rule "
+                               "to allow MAC address on '%s'"),
+                             net->ifname);
     }
 
-    if (tapfd >= 0 &&
-        virNetDevBandwidthSet(net->ifname,
+    if (virNetDevBandwidthSet(net->ifname,
                               virDomainNetGetActualBandwidth(net),
                               false) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("cannot set bandwidth limits on %s"),
                        net->ifname);
-        VIR_FORCE_CLOSE(tapfd);
         goto cleanup;
     }
 
-    if (tapfd >= 0) {
-        if ((net->filter) && (net->ifname)) {
-            if (virDomainConfNWFilterInstantiate(conn, def->uuid, net) < 0)
-                VIR_FORCE_CLOSE(tapfd);
-        }
-    }
+    if (net->filter && net->ifname &&
+        virDomainConfNWFilterInstantiate(conn, def->uuid, net) < 0)
+        goto cleanup;
+
+
+    ret = 0;
 
 cleanup:
+    if (ret < 0) {
+        while (tapfdSize--)
+            VIR_FORCE_CLOSE(tapfd[tapfdSize]);
+    }
     VIR_FREE(brname);
     virObjectUnref(cfg);
 
-    return tapfd;
+    return ret;
 }
 
 
@@ -5868,9 +5871,8 @@ qemuBuildInterfaceCommandLine(virCommandPtr cmd,
         if (actualType == VIR_DOMAIN_NET_TYPE_NETWORK ||
             cfg->privileged ||
             (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_NETDEV_BRIDGE))) {
-            tapfd = qemuNetworkIfaceConnect(def, conn, driver, net,
-                                            qemuCaps);
-            if (tapfd < 0)
+            if (qemuNetworkIfaceConnect(def, conn, driver, net,
+                                        qemuCaps, &tapfd, 1) < 0)
                 goto cleanup;
         }
     } else if (actualType == VIR_DOMAIN_NET_TYPE_DIRECT) {
diff --git a/src/qemu/qemu_command.h b/src/qemu/qemu_command.h
index db14d8f..4a225b3 100644
--- a/src/qemu/qemu_command.h
+++ b/src/qemu/qemu_command.h
@@ -150,7 +150,9 @@ int qemuNetworkIfaceConnect(virDomainDefPtr def,
                             virConnectPtr conn,
                             virQEMUDriverPtr driver,
                             virDomainNetDefPtr net,
-                            virQEMUCapsPtr qemuCaps)
+                            virQEMUCapsPtr qemuCaps,
+                            int *tapfd,
+                            int tapfdSize)
     ATTRIBUTE_NONNULL(2);
 
 int qemuPhysIfaceConnect(virDomainDefPtr def,
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index b34160a..3505c52 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -740,8 +740,8 @@ int qemuDomainAttachNetDevice(virConnectPtr conn,
         if (actualType == VIR_DOMAIN_NET_TYPE_NETWORK ||
             cfg->privileged ||
             (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_NETDEV_BRIDGE))) {
-            if ((tapfd = qemuNetworkIfaceConnect(vm->def, conn, driver, net,
-                                                 priv->qemuCaps)) < 0)
+            if (qemuNetworkIfaceConnect(vm->def, conn, driver, net,
+                                        priv->qemuCaps, &tapfd, 1) < 0)
                 goto cleanup;
             iface_connected = true;
             if (qemuOpenVhostNet(vm->def, net, priv->qemuCaps, &vhostfd, 1) < 0)
-- 
1.8.1.5

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]