]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: new function virNetDevIPInfoAddToDev
authorLaine Stump <laine@laine.org>
Thu, 16 Jun 2016 16:22:07 +0000 (12:22 -0400)
committerLaine Stump <laine@laine.org>
Sat, 2 Jul 2016 01:13:30 +0000 (21:13 -0400)
This patch takes the code out of
lxcContainerRenameAndEnableInterfaces() that adds all IP addresses and
IP routes to the interface, and puts it into a utility function
virNetDevIPInfoAddToDev() in virnetdevip.c so that it can be used by
anyone.

One small change in functionality -
lxcContainerRenameAndEnableInterfaces() previously would add all IP
addresses to the interface while it was still offline, then set the
interface online, and then add the routes. Because I don't want the
utility function to set the interface online, I've moved this up so
the interface is first set online, then IP addresses and routes are
added. This is the same order that the network service from
initscripts (in ifup-ether) does it, so it shouldn't pose any problem
(and hasn't, in the tests that I've run).

(This patch had been pushed earlier in commit
f1e0d0da11c473905470c28a6488bf57d9d0ae6e, but was reverted in commit
05eab47559950403aa67d18b098273269ae6916e because it had been
accidentally pushed during the freeze for release 2.0.0)

src/libvirt_private.syms
src/lxc/lxc_container.c
src/util/virnetdevip.c
src/util/virnetdevip.h

index 04fcb47445eec64508580e11449d5ae2da05cdd0..aa5ae1a615773e42b11fb119733a4dd1eacc5009 100644 (file)
@@ -1932,6 +1932,7 @@ virNetDevBridgeSetVlanFiltering;
 virNetDevIPAddrAdd;
 virNetDevIPAddrDel;
 virNetDevIPAddrGet;
+virNetDevIPInfoAddToDev;
 virNetDevIPInfoClear;
 virNetDevIPRouteAdd;
 virNetDevIPRouteFree;
index 0d5f16c517661b2c79bfa952810f0f16894a76f7..916a37b68e035278e3ae4055691bb8f08a2ff759 100644 (file)
@@ -490,7 +490,7 @@ lxcContainerRenameAndEnableInterfaces(virDomainDefPtr vmDef,
                                       char **veths)
 {
     int ret = -1;
-    size_t i, j;
+    size_t i;
     const char *newname;
     virDomainNetDefPtr netDef;
     bool privNet = vmDef->features[VIR_DOMAIN_FEATURE_PRIVNET] ==
@@ -509,53 +509,28 @@ lxcContainerRenameAndEnableInterfaces(virDomainDefPtr vmDef,
 
         VIR_DEBUG("Renaming %s to %s", veths[i], newname);
         if (virNetDevSetName(veths[i], newname) < 0)
-           goto cleanup;
-
-        for (j = 0; j < netDef->guestIP.nips; j++) {
-            virNetDevIPAddrPtr ip = netDef->guestIP.ips[j];
-            int prefix;
-            char *ipStr = virSocketAddrFormat(&ip->address);
-
-            if ((prefix = virSocketAddrGetIPPrefix(&ip->address,
-                                                   NULL, ip->prefix)) < 0) {
-                ipStr = virSocketAddrFormat(&ip->address);
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("Failed to determine prefix for IP address '%s'"),
-                               ipStr);
-                VIR_FREE(ipStr);
-                goto cleanup;
-            }
-            VIR_FREE(ipStr);
-
-            if (virNetDevIPAddrAdd(newname, &ip->address, NULL, prefix) < 0)
-                goto cleanup;
-        }
+            goto cleanup;
 
+        /* Only enable this device if there is a reason to do so (either
+         * at least one IP was specified, or link state was set to up in
+         * the config)
+         */
         if (netDef->guestIP.nips ||
             netDef->linkstate == VIR_DOMAIN_NET_INTERFACE_LINK_STATE_UP) {
             VIR_DEBUG("Enabling %s", newname);
             if (virNetDevSetOnline(newname, true) < 0)
                 goto cleanup;
-
-            /* Set the routes */
-            for (j = 0; j < netDef->guestIP.nroutes; j++) {
-                virNetDevIPRoutePtr route = netDef->guestIP.routes[j];
-
-                if (virNetDevIPRouteAdd(newname,
-                                        virNetDevIPRouteGetAddress(route),
-                                        virNetDevIPRouteGetPrefix(route),
-                                        virNetDevIPRouteGetGateway(route),
-                                        virNetDevIPRouteGetMetric(route)) < 0) {
-                    goto cleanup;
-                }
-            }
         }
+
+        /* set IP addresses and routes */
+        if (virNetDevIPInfoAddToDev(newname, &netDef->guestIP) < 0)
+            goto cleanup;
     }
 
     /* enable lo device only if there were other net devices */
     if ((veths || privNet) &&
         virNetDevSetOnline("lo", true) < 0)
-       goto cleanup;
+        goto cleanup;
 
     ret = 0;
  cleanup:
index e5b88fea64a87b0e421730b7d62980c988c7a58c..ff0f2ddd3690d8cc9ad98852b7d745c1818b8bf7 100644 (file)
@@ -887,3 +887,63 @@ virNetDevIPInfoClear(virNetDevIPInfoPtr ip)
         virNetDevIPRouteFree(ip->routes[i]);
     VIR_FREE(ip->routes);
 }
+
+
+/**
+ * virNetDevIPInfoAddToDev:
+ * @ifname: name of device to operate on
+ * @ipInfo: list of routes and IP addresses to add to this device
+ *
+ * All IP routes and IP addresses in ipInfo are added to the named device.
+ *
+ * Returns: 0 on success, -1 (and error reported) on failure.
+ */
+int
+virNetDevIPInfoAddToDev(const char *ifname,
+                        virNetDevIPInfo const *ipInfo)
+{
+    int ret = -1;
+    size_t i;
+    int prefix;
+
+    /* add all IP addresses */
+    for (i = 0; i < ipInfo->nips; i++) {
+        virNetDevIPAddrPtr ip = ipInfo->ips[i];
+
+        if ((prefix = virSocketAddrGetIPPrefix(&ip->address,
+                                               NULL, ip->prefix)) < 0) {
+            char *ipStr = virSocketAddrFormat(&ip->address);
+
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Failed to determine prefix for IP address '%s'"),
+                           NULLSTR(ipStr));
+            VIR_FREE(ipStr);
+            goto cleanup;
+        }
+        if (virNetDevIPAddrAdd(ifname, &ip->address, NULL, prefix) < 0)
+            goto cleanup;
+    }
+
+    /* add all routes */
+    for (i = 0; i < ipInfo->nroutes; i++) {
+        virNetDevIPRoutePtr route = ipInfo->routes[i];
+
+        if ((prefix = virNetDevIPRouteGetPrefix(route)) < 0) {
+            char *ipStr = virSocketAddrFormat(&route->address);
+
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Failed to determine prefix for route with destination '%s'"),
+                           NULLSTR(ipStr));
+            VIR_FREE(ipStr);
+            goto cleanup;
+        }
+        if (virNetDevIPRouteAdd(ifname, &route->address, prefix,
+                                &route->gateway,
+                                virNetDevIPRouteGetMetric(route)) < 0)
+            goto cleanup;
+    }
+
+    ret = 0;
+ cleanup:
+    return ret;
+}
index be41636db548c6f487975d9f11e0828d902309c6..86fb77e28536290298ad98ef5bbe7665ffa063fe 100644 (file)
@@ -86,5 +86,7 @@ virSocketAddrPtr virNetDevIPRouteGetGateway(virNetDevIPRoutePtr def);
 
 /* virNetDevIPInfo object */
 void virNetDevIPInfoClear(virNetDevIPInfoPtr ip);
+int virNetDevIPInfoAddToDev(const char *ifname,
+                            virNetDevIPInfo const *ipInfo);
 
 #endif /* __VIR_NETDEVIP_H__ */