[lxc-devel] [lxc/master] confile: support the network link string pattern matching

lifupan on Github lxc-bot at linuxcontainers.org
Wed Dec 7 10:22:10 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 700 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20161207/d87e7ee1/attachment.bin>
-------------- next part --------------
From 3c329b54199b1d910785b2e4b1cadced1ae02b55 Mon Sep 17 00:00:00 2001
From: fli <fupan.li at windriver.com>
Date: Tue, 6 Dec 2016 00:59:52 -0800
Subject: [PATCH] confile: support the network link string pattern matching

Enable lxc network config support the following type and link:

lxc.network.type = phys
lxc.network.link = eth*

Thus, when lxc find any network interfaces name prefixed
with "eth" such as "eth0", "eth1" and so on, it will
try to move them the container's namespace; If it didn't
find any matching, it would do nothing for this configure
line.

Signed-off-by: fli <fupan.li at windriver.com>
---
 src/lxc/confile.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 80 insertions(+), 1 deletion(-)

diff --git a/src/lxc/confile.c b/src/lxc/confile.c
index d7362ca..195c2c9 100644
--- a/src/lxc/confile.c
+++ b/src/lxc/confile.c
@@ -50,6 +50,12 @@
 #include "network.h"
 #include "lxcseccomp.h"
 
+#if HAVE_IFADDRS_H
+#include <ifaddrs.h>
+#else
+#include <../include/ifaddrs.h>
+#endif
+
 #if HAVE_SYS_PERSONALITY_H
 #include <sys/personality.h>
 #endif
@@ -115,6 +121,7 @@ static int config_init_uid(const char *, const char *, struct lxc_conf *);
 static int config_init_gid(const char *, const char *, struct lxc_conf *);
 static int config_ephemeral(const char *, const char *, struct lxc_conf *);
 static int config_no_new_privs(const char *, const char *, struct lxc_conf *);
+static int _config_network_link(const char *key, const char *value,struct lxc_conf *lxc_conf);
 
 static struct lxc_config_t config[] = {
 
@@ -672,16 +679,88 @@ static int config_network_flags(const char *key, const char *value,
 	return 0;
 }
 
+static int create_matched_ifnames(const char *value, 
+				struct lxc_conf *lxc_conf)
+{
+	struct ifaddrs *ifaddr, *ifa;
+	const char *type_key = "lxc.network.type";
+	const char *link_key = "lxc.network.link";
+	const char *tmpvalue = "phys";
+	int n, ret = 0;
+
+	if (getifaddrs(&ifaddr) == -1) {
+		SYSERROR("Get network interfaces failed");
+		return -1;
+	}
+
+	for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++)
+	{
+		if (!ifa->ifa_addr)
+			continue;
+		if(ifa->ifa_addr->sa_family != AF_PACKET)
+			continue;
+
+		if(!strncmp(value, ifa->ifa_name, strlen(value)-1))
+		{
+			ret = config_network_type(type_key, tmpvalue, lxc_conf);
+			if (!ret)
+			{
+				ret = _config_network_link(link_key, ifa->ifa_name, lxc_conf);
+				if (ret){
+					ERROR("failed to create mached ifnames");
+					break;
+				}
+			}else
+			{
+				ERROR("failed to create mached ifnames");
+				break;
+			}
+		}
+	}
+
+	return ret;
+}
+
+static int _config_network_link(const char *key, const char *value,
+				struct lxc_conf *lxc_conf)
+{
+	struct lxc_netdev *netdev;
+
+	netdev = network_netdev(key, value, &lxc_conf->network);
+	if (!netdev)
+		return -1;
+
+	return network_ifname(&netdev->link, value);
+}
+
 static int config_network_link(const char *key, const char *value,
 			       struct lxc_conf *lxc_conf)
 {
 	struct lxc_netdev *netdev;
+	struct lxc_list * it;
+	int ret = 0;
 
 	netdev = network_netdev(key, value, &lxc_conf->network);
 	if (!netdev)
 		return -1;
 
-	return network_ifname(&netdev->link, value);
+	if (value[strlen(value) - 1] == '*' && netdev->type == LXC_NET_PHYS)
+	{
+		//get the last network list and remove it.
+		it = lxc_conf->network.prev;
+		if (((struct lxc_netdev *)(it->elem))->type != LXC_NET_PHYS)
+		{
+			ERROR("lxc config cannot support string pattern matching for this link type");
+			return -1;
+		}
+		lxc_list_del(it);
+		free(it);
+
+		ret = create_matched_ifnames(value, lxc_conf);
+	}else
+		ret = network_ifname(&netdev->link, value);
+
+	return ret;
 }
 
 static int config_network_name(const char *key, const char *value,


More information about the lxc-devel mailing list