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

[libvirt] [PATCHv2 09/16] LXC: use the new net devices gateway definition



When a gateway is set on a network device, a new default route via this
gateway through the devoce will be added in the container.
---
 src/lxc/lxc_container.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c
index ec9d808..a2f4c5d 100644
--- a/src/lxc/lxc_container.c
+++ b/src/lxc/lxc_container.c
@@ -479,6 +479,35 @@ lxcContainerGetNetDef(virDomainDefPtr vmDef, const char *devName)
     return NULL;
 }
 
+static int
+lxcContainerAddDefaultRoute(const char *ifname,
+                            const char *gateway,
+                            int family)
+{
+    virSocketAddr address;
+    virSocketAddr network;
+
+    VIR_DEBUG("Adding default route via %s on dev %s", gateway, ifname);
+    if (virSocketAddrParse(&address, gateway, family) < 0)
+        return -1;
+
+    if (family == AF_INET) {
+        if (virSocketAddrParseIPv4(&network, "0.0.0.0") < 0)
+            return -1;
+    } else {
+        if (virSocketAddrParseIPv6(&network, "::") < 0)
+            return -1;
+    }
+
+    if (virNetDevAddRoute(ifname, &network, 0, &address, 0) < 0) {
+        virReportError(VIR_ERR_SYSTEM_ERROR,
+                       _("Failed adding default route via %s on dev %s"),
+                       gateway, ifname);
+        return -1;
+    }
+    return 0;
+}
+
 /**
  * lxcContainerRenameAndEnableInterfaces:
  * @nveths: number of interfaces
@@ -543,6 +572,17 @@ static int lxcContainerRenameAndEnableInterfaces(virDomainDefPtr vmDef,
         if (rc < 0)
             goto error_out;
 
+        /* Set the gateways */
+        if (netDef->gateway_ipv4 &&
+            lxcContainerAddDefaultRoute(newname, netDef->gateway_ipv4,
+                                        AF_INET) < 0)
+            goto error_out;
+
+        if (netDef->gateway_ipv6 &&
+            lxcContainerAddDefaultRoute(newname, netDef->gateway_ipv6,
+                                        AF_INET6) < 0)
+            goto error_out;
+
         VIR_FREE(newname);
     }
 
-- 
1.8.4.5


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