]> xenbits.xensource.com Git - libvirt.git/commitdiff
Refactor LXC NIC creation to allow reuse by hotplug code
authorDaniel P. Berrange <berrange@redhat.com>
Tue, 27 Nov 2012 12:55:02 +0000 (12:55 +0000)
committerDaniel P. Berrange <berrange@redhat.com>
Mon, 17 Dec 2012 17:50:51 +0000 (17:50 +0000)
The code for creating veth/macvlan devices is part of the
LXC process startup code. Refactor this a little and export
the methods to the rest of the LXC driver. This allows them
to be reused for NIC hotplug code

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
src/lxc/lxc_process.c
src/lxc/lxc_process.h

index 3e7fcb85505f0bfba90e15b61aa78e01e3a442e0..82bc1fcdc65b29a175d9765b1f68de192675520e 100644 (file)
@@ -297,14 +297,12 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver,
 }
 
 
-static int virLXCProcessSetupInterfaceBridged(virConnectPtr conn,
-                                              virDomainDefPtr vm,
-                                              virDomainNetDefPtr net,
-                                              const char *brname,
-                                              unsigned int *nveths,
-                                              char ***veths)
+char *virLXCProcessSetupInterfaceBridged(virConnectPtr conn,
+                                         virDomainDefPtr vm,
+                                         virDomainNetDefPtr net,
+                                         const char *brname)
 {
-    int ret = -1;
+    char *ret = NULL;
     char *parentVeth;
     char *containerVeth = NULL;
     const virNetDevVPortProfilePtr vport = virDomainNetGetActualVirtPortProfile(net);
@@ -318,24 +316,17 @@ static int virLXCProcessSetupInterfaceBridged(virConnectPtr conn,
     if (net->ifname == NULL)
         net->ifname = parentVeth;
 
-    if (VIR_REALLOC_N(*veths, (*nveths)+1) < 0) {
-        virReportOOMError();
-        VIR_FREE(containerVeth);
-        goto cleanup;
-    }
-    (*veths)[(*nveths)] = containerVeth;
-    (*nveths)++;
-
     if (virNetDevSetMAC(containerVeth, &net->mac) < 0)
         goto cleanup;
 
-    if (vport && vport->virtPortType == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH)
-        ret = virNetDevOpenvswitchAddPort(brname, parentVeth, &net->mac,
-                                          vm->uuid, vport, virDomainNetGetActualVlan(net));
-    else
-        ret = virNetDevBridgeAddPort(brname, parentVeth);
-    if (ret < 0)
-        goto cleanup;
+    if (vport && vport->virtPortType == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH) {
+        if (virNetDevOpenvswitchAddPort(brname, parentVeth, &net->mac,
+                                        vm->uuid, vport, virDomainNetGetActualVlan(net)) < 0)
+            goto cleanup;
+    } else {
+        if (virNetDevBridgeAddPort(brname, parentVeth) < 0)
+            goto cleanup;
+    }
 
     if (virNetDevSetOnline(parentVeth, true) < 0)
         goto cleanup;
@@ -353,20 +344,18 @@ static int virLXCProcessSetupInterfaceBridged(virConnectPtr conn,
         virDomainConfNWFilterInstantiate(conn, vm->uuid, net) < 0)
         goto cleanup;
 
-    ret = 0;
+    ret = containerVeth;
 
 cleanup:
     return ret;
 }
 
 
-static int virLXCProcessSetupInterfaceDirect(virConnectPtr conn,
-                                             virDomainDefPtr def,
-                                             virDomainNetDefPtr net,
-                                             unsigned int *nveths,
-                                             char ***veths)
+char *virLXCProcessSetupInterfaceDirect(virConnectPtr conn,
+                                        virDomainDefPtr def,
+                                        virDomainNetDefPtr net)
 {
-    int ret = -1;
+    char *ret = NULL;
     char *res_ifname = NULL;
     virLXCDriverPtr driver = conn->privateData;
     virNetDevBandwidthPtr bw;
@@ -381,7 +370,7 @@ static int virLXCProcessSetupInterfaceDirect(virConnectPtr conn,
     if (bw) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                        _("Unable to set network bandwidth on direct interfaces"));
-        return -1;
+        return NULL;
     }
 
     /* XXX how todo port profiles ?
@@ -395,15 +384,9 @@ static int virLXCProcessSetupInterfaceDirect(virConnectPtr conn,
     if (prof) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                        _("Unable to set port profile on direct interfaces"));
-        return -1;
+        return NULL;
     }
 
-    if (VIR_REALLOC_N(*veths, (*nveths)+1) < 0) {
-        virReportOOMError();
-        return -1;
-    }
-    (*veths)[(*nveths)] = NULL;
-
     if (virNetDevMacVLanCreateWithVPortProfile(
             net->ifname, &net->mac,
             virDomainNetGetActualDirectDev(net),
@@ -416,10 +399,7 @@ static int virLXCProcessSetupInterfaceDirect(virConnectPtr conn,
             virDomainNetGetActualBandwidth(net)) < 0)
         goto cleanup;
 
-    (*veths)[(*nveths)] = res_ifname;
-    (*nveths)++;
-
-    ret = 0;
+    ret = res_ifname;
 
 cleanup:
     return ret;
@@ -441,13 +421,14 @@ cleanup:
  */
 static int virLXCProcessSetupInterfaces(virConnectPtr conn,
                                         virDomainDefPtr def,
-                                        unsigned int *nveths,
+                                        size_t *nveths,
                                         char ***veths)
 {
     int ret = -1;
     size_t i;
 
     for (i = 0 ; i < def->nnets ; i++) {
+        char *veth = NULL;
         /* If appropriate, grab a physical device from the configured
          * network's pool of devices, or resolve bridge device name
          * to the one defined in the network definition.
@@ -455,6 +436,11 @@ static int virLXCProcessSetupInterfaces(virConnectPtr conn,
         if (networkAllocateActualDevice(def->nets[i]) < 0)
             goto cleanup;
 
+        if (VIR_EXPAND_N(*veths, *nveths, 1) < 0) {
+            virReportOOMError();
+            goto cleanup;
+        }
+
         switch (virDomainNetGetActualType(def->nets[i])) {
         case VIR_DOMAIN_NET_TYPE_NETWORK: {
             virNetworkPtr network;
@@ -491,12 +477,10 @@ static int virLXCProcessSetupInterfaces(virConnectPtr conn,
             if (fail)
                 goto cleanup;
 
-            if (virLXCProcessSetupInterfaceBridged(conn,
-                                                   def,
-                                                   def->nets[i],
-                                                   brname,
-                                                   nveths,
-                                                   veths) < 0) {
+            if (!(veth = virLXCProcessSetupInterfaceBridged(conn,
+                                                            def,
+                                                            def->nets[i],
+                                                            brname))) {
                 VIR_FREE(brname);
                 goto cleanup;
             }
@@ -510,21 +494,17 @@ static int virLXCProcessSetupInterfaces(virConnectPtr conn,
                                _("No bridge name specified"));
                 goto cleanup;
             }
-            if (virLXCProcessSetupInterfaceBridged(conn,
-                                                   def,
-                                                   def->nets[i],
-                                                   brname,
-                                                   nveths,
-                                                   veths) < 0)
+            if (!(veth = virLXCProcessSetupInterfaceBridged(conn,
+                                                            def,
+                                                            def->nets[i],
+                                                            brname)))
                 goto cleanup;
         }   break;
 
         case VIR_DOMAIN_NET_TYPE_DIRECT:
-            if (virLXCProcessSetupInterfaceDirect(conn,
-                                                  def,
-                                                  def->nets[i],
-                                                  nveths,
-                                                  veths) < 0)
+            if (!(veth = virLXCProcessSetupInterfaceDirect(conn,
+                                                           def,
+                                                           def->nets[i])))
                 goto cleanup;
             break;
 
@@ -542,6 +522,8 @@ static int virLXCProcessSetupInterfaces(virConnectPtr conn,
                                ));
             goto cleanup;
         }
+
+        (*veths)[(*nveths)-1] = veth;
     }
 
     ret = 0;
@@ -923,7 +905,7 @@ int virLXCProcessStart(virConnectPtr conn,
     size_t i;
     char *logfile = NULL;
     int logfd = -1;
-    unsigned int nveths = 0;
+    size_t nveths = 0;
     char **veths = NULL;
     int handshakefds[2] = { -1, -1 };
     off_t pos = -1;
index bac2c6c4d67fc314bb633f7e84422162db990162..779cc5fea53897151807c72ffe7d0c0c76709893 100644 (file)
@@ -47,4 +47,12 @@ void virLXCProcessAutostartAll(virLXCDriverPtr driver);
 int virLXCProcessReconnectAll(virLXCDriverPtr driver,
                               virDomainObjListPtr doms);
 
+char *virLXCProcessSetupInterfaceBridged(virConnectPtr conn,
+                                         virDomainDefPtr vm,
+                                         virDomainNetDefPtr net,
+                                         const char *brname);
+char *virLXCProcessSetupInterfaceDirect(virConnectPtr conn,
+                                        virDomainDefPtr def,
+                                        virDomainNetDefPtr net);
+
 #endif /* __LXC_PROCESS_H__ */