[PATCH 8/9] LXC: use the new net devices gateway definition

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

 



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 | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c
index 62e9d76..4a373cb 100644
--- a/src/lxc/lxc_container.c
+++ b/src/lxc/lxc_container.c
@@ -479,6 +479,33 @@ lxcContainerGetNetDef(virDomainDefPtr vmDef, const char *devName)
     return NULL;
 }
 
+static int
+lxcContainerAddDefaultRoute(const char *ifname, const char *gateway)
+{
+    virSocketAddr address;
+    virSocketAddr network;
+
+    VIR_DEBUG("Adding default route via %s on dev %s", gateway, ifname);
+    if (virSocketAddrParse(&address, gateway, AF_UNSPEC) < 0)
+        return -1;
+
+    if (!strchr(gateway, ':')) {
+        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
@@ -539,6 +566,15 @@ static int lxcContainerRenameAndEnableInterfaces(virDomainDefPtr vmDef,
         if (rc < 0)
             goto error_out;
 
+        /* Set the gateways */
+        if (netDef->gateway_ipv4 &&
+            lxcContainerAddDefaultRoute(newname, netDef->gateway_ipv4) < 0)
+            goto error_out;
+
+        if (netDef->gateway_ipv6 &&
+            lxcContainerAddDefaultRoute(newname, netDef->gateway_ipv6) < 0)
+            goto error_out;
+
         VIR_FREE(newname);
     }
 
-- 
1.8.4.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]