Blame SOURCES/iproute2-4.0.0-netns.patch

a4b897
commit a721ebaafd6852e86b636e2595e36f635c2b3cae
a4b897
Author: Nicolas Dichtel <nicolas.dichtel@6wind.com>
a4b897
Date:   Wed Apr 15 14:23:22 2015 +0200
a4b897
a4b897
    netns: allow to dump and monitor nsid
a4b897
    
a4b897
    Two commands are added:
a4b897
     - ip netns list-id
a4b897
     - ip monitor nsid
a4b897
    
a4b897
    A cache is also added to remember the association between the iproute2 netns
a4b897
    name (from /var/run/netns/) and the nsid.
a4b897
    To avoid interfering with the rth socket, a new rtnl socket (rtnsh) is used to
a4b897
    get nsid (we may send rtnl request during listing on rth).
a4b897
    
a4b897
    Example:
a4b897
    $ ip netns list-id
a4b897
    nsid 0 (iproute2 netns name: foo)
a4b897
    $ ip monitor nsid
a4b897
    Deleted nsid 0 (iproute2 netns name: foo)
a4b897
    nsid 16 (iproute2 netns name: bar)
a4b897
    
a4b897
    Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
a4b897
a4b897
diff --git a/include/ll_map.h b/include/ll_map.h
a4b897
index f1dda39..a9d9cb3 100644
a4b897
--- a/include/ll_map.h
a4b897
+++ b/include/ll_map.h
a4b897
@@ -10,5 +10,6 @@ extern const char *ll_index_to_name(unsigned idx);
a4b897
 extern const char *ll_idx_n2a(unsigned idx, char *buf);
a4b897
 extern int ll_index_to_type(unsigned idx);
a4b897
 extern unsigned ll_index_to_flags(unsigned idx);
a4b897
+extern unsigned namehash(const char *str);
a4b897
 
a4b897
 #endif /* __LL_MAP_H__ */
a4b897
diff --git a/ip/ip_common.h b/ip/ip_common.h
a4b897
index f9b4734..85529f0 100644
a4b897
--- a/ip/ip_common.h
a4b897
+++ b/ip/ip_common.h
a4b897
@@ -30,6 +30,9 @@ extern int print_rule(const struct sockaddr_nl *who,
a4b897
 		      struct nlmsghdr *n, void *arg);
a4b897
 extern int print_netconf(const struct sockaddr_nl *who,
a4b897
 			 struct nlmsghdr *n, void *arg);
a4b897
+extern void netns_map_init(void);
a4b897
+extern int print_nsid(const struct sockaddr_nl *who,
a4b897
+		      struct nlmsghdr *n, void *arg);
a4b897
 extern int do_ipaddr(int argc, char **argv);
a4b897
 extern int do_ipaddrlabel(int argc, char **argv);
a4b897
 extern int do_iproute(int argc, char **argv);
a4b897
diff --git a/ip/ipmonitor.c b/ip/ipmonitor.c
a4b897
index 86c473e..148cf1e 100644
a4b897
--- a/ip/ipmonitor.c
a4b897
+++ b/ip/ipmonitor.c
a4b897
@@ -31,7 +31,7 @@ static void usage(void)
a4b897
 {
a4b897
 	fprintf(stderr, "Usage: ip monitor [ all | LISTofOBJECTS ] [ FILE ]\n");
a4b897
 	fprintf(stderr, "LISTofOBJECTS := link | address | route | mroute | prefix |\n");
a4b897
-	fprintf(stderr, "                 neigh | netconf\n");
a4b897
+	fprintf(stderr, "                 neigh | netconf | nsid\n");
a4b897
 	fprintf(stderr, "FILE := file FILENAME\n");
a4b897
 	exit(-1);
a4b897
 }
a4b897
@@ -127,6 +127,12 @@ static int accept_msg(const struct sockaddr_nl *who,
a4b897
 	    n->nlmsg_type == RTM_NEWTFILTER ||
a4b897
 	    n->nlmsg_type == RTM_DELTFILTER)
a4b897
 		return 0;
a4b897
+	if (n->nlmsg_type == RTM_NEWNSID || n->nlmsg_type == RTM_DELNSID) {
a4b897
+		if (prefix_banner)
a4b897
+			fprintf(fp, "[NSID]");
a4b897
+		print_nsid(who, n, arg);
a4b897
+		return 0;
a4b897
+	}
a4b897
 	if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
a4b897
 	    n->nlmsg_type != NLMSG_DONE) {
a4b897
 		fprintf(fp, "Unknown message: %08x %08x %08x\n",
a4b897
@@ -146,6 +152,9 @@ int do_ipmonitor(int argc, char **argv)
a4b897
 	int lprefix=0;
a4b897
 	int lneigh=0;
a4b897
 	int lnetconf=0;
a4b897
+	int lnsid=0;
a4b897
+
a4b897
+	groups |= nl_mgrp(RTNLGRP_NSID);
a4b897
 
a4b897
 	rtnl_close(&rth);
a4b897
 	ipaddr_reset_filter(1);
a4b897
@@ -178,6 +187,9 @@ int do_ipmonitor(int argc, char **argv)
a4b897
 		} else if (matches(*argv, "netconf") == 0) {
a4b897
 			lnetconf = 1;
a4b897
 			groups = 0;
a4b897
+		} else if (matches(*argv, "nsid") == 0) {
a4b897
+			lnsid = 1;
a4b897
+			groups = 0;
a4b897
 		} else if (strcmp(*argv, "all") == 0) {
a4b897
 			groups = ~RTMGRP_TC;
a4b897
 			prefix_banner=1;
a4b897
@@ -223,6 +235,9 @@ int do_ipmonitor(int argc, char **argv)
a4b897
 		if (!preferred_family || preferred_family == AF_INET6)
a4b897
 			groups |= nl_mgrp(RTNLGRP_IPV6_NETCONF);
a4b897
 	}
a4b897
+	if (lnsid) {
a4b897
+		groups |= nl_mgrp(RTNLGRP_NSID);
a4b897
+	}
a4b897
 	if (file) {
a4b897
 		FILE *fp;
a4b897
 		fp = fopen(file, "r");
a4b897
@@ -236,6 +251,7 @@ int do_ipmonitor(int argc, char **argv)
a4b897
 	if (rtnl_open(&rth, groups) < 0)
a4b897
 		exit(1);
a4b897
 	ll_init_map(&rth);
a4b897
+	netns_map_init();
a4b897
 
a4b897
 	if (rtnl_listen(&rth, accept_msg, stdout) < 0)
a4b897
 		exit(2);
a4b897
diff --git a/ip/ipnetns.c b/ip/ipnetns.c
a4b897
index 24df167..438d59b 100644
a4b897
--- a/ip/ipnetns.c
a4b897
+++ b/ip/ipnetns.c
a4b897
@@ -14,10 +14,12 @@
a4b897
 #include <errno.h>
a4b897
 #include <unistd.h>
a4b897
 #include <ctype.h>
a4b897
+#include <linux/limits.h>
a4b897
 
a4b897
 #include <linux/net_namespace.h>
a4b897
 
a4b897
 #include "utils.h"
a4b897
+#include "hlist.h"
a4b897
 #include "ip_common.h"
a4b897
 #include "namespace.h"
a4b897
 
a4b897
@@ -31,9 +33,13 @@ static int usage(void)
a4b897
 	fprintf(stderr, "       ip netns pids NAME\n");
a4b897
 	fprintf(stderr, "       ip [-all] netns exec [NAME] cmd ...\n");
a4b897
 	fprintf(stderr, "       ip netns monitor\n");
a4b897
+	fprintf(stderr, "       ip netns list-id\n");
a4b897
 	exit(-1);
a4b897
 }
a4b897
 
a4b897
+/* This socket is used to get nsid */
a4b897
+static struct rtnl_handle rtnsh = { .fd = -1 };
a4b897
+
a4b897
 static int have_rtnl_getnsid = -1;
a4b897
 
a4b897
 static int ipnetns_accept_msg(const struct sockaddr_nl *who,
a4b897
@@ -106,7 +112,7 @@ static int get_netnsid_from_name(const char *name)
a4b897
 		return fd;
a4b897
 
a4b897
 	addattr32(&req.n, 1024, NETNSA_FD, fd);
a4b897
-	if (rtnl_talk(&rth, &req.n, 0, 0, &answer.n) < 0) {
a4b897
+	if (rtnl_talk(&rtnsh, &req.n, 0, 0, &answer.n) < 0) {
a4b897
 		close(fd);
a4b897
 		return -2;
a4b897
 	}
a4b897
@@ -129,6 +135,196 @@ static int get_netnsid_from_name(const char *name)
a4b897
 	return -1;
a4b897
 }
a4b897
 
a4b897
+struct nsid_cache {
a4b897
+	struct hlist_node	nsid_hash;
a4b897
+	struct hlist_node	name_hash;
a4b897
+	int			nsid;
a4b897
+	char			name[NAME_MAX];
a4b897
+};
a4b897
+
a4b897
+#define NSIDMAP_SIZE		128
a4b897
+#define NSID_HASH_NSID(nsid)	(nsid & (NSIDMAP_SIZE - 1))
a4b897
+#define NSID_HASH_NAME(name)	(namehash(name) & (NSIDMAP_SIZE - 1))
a4b897
+
a4b897
+static struct hlist_head	nsid_head[NSIDMAP_SIZE];
a4b897
+static struct hlist_head	name_head[NSIDMAP_SIZE];
a4b897
+
a4b897
+static struct nsid_cache *netns_map_get_by_nsid(int nsid)
a4b897
+{
a4b897
+	uint32_t h = NSID_HASH_NSID(nsid);
a4b897
+	struct hlist_node *n;
a4b897
+
a4b897
+	hlist_for_each(n, &nsid_head[h]) {
a4b897
+		struct nsid_cache *c = container_of(n, struct nsid_cache,
a4b897
+						    nsid_hash);
a4b897
+		if (c->nsid == nsid)
a4b897
+			return c;
a4b897
+	}
a4b897
+
a4b897
+	return NULL;
a4b897
+}
a4b897
+
a4b897
+static int netns_map_add(int nsid, char *name)
a4b897
+{
a4b897
+	struct nsid_cache *c;
a4b897
+	uint32_t h;
a4b897
+
a4b897
+	if (netns_map_get_by_nsid(nsid) != NULL)
a4b897
+		return -EEXIST;
a4b897
+
a4b897
+	c = malloc(sizeof(*c));
a4b897
+	if (c == NULL) {
a4b897
+		perror("malloc");
a4b897
+		return -ENOMEM;
a4b897
+	}
a4b897
+	c->nsid = nsid;
a4b897
+	strcpy(c->name, name);
a4b897
+
a4b897
+	h = NSID_HASH_NSID(nsid);
a4b897
+	hlist_add_head(&c->nsid_hash, &nsid_head[h]);
a4b897
+
a4b897
+	h = NSID_HASH_NAME(name);
a4b897
+	hlist_add_head(&c->name_hash, &name_head[h]);
a4b897
+
a4b897
+	return 0;
a4b897
+}
a4b897
+
a4b897
+static void netns_map_del(struct nsid_cache *c)
a4b897
+{
a4b897
+	hlist_del(&c->name_hash);
a4b897
+	hlist_del(&c->nsid_hash);
a4b897
+	free(c);
a4b897
+}
a4b897
+
a4b897
+void netns_map_init(void)
a4b897
+{
a4b897
+	static int initialized;
a4b897
+	struct dirent *entry;
a4b897
+	DIR *dir;
a4b897
+	int nsid;
a4b897
+
a4b897
+	if (initialized || !ipnetns_have_nsid())
a4b897
+		return;
a4b897
+
a4b897
+	if (rtnl_open(&rtnsh, 0) < 0) {
a4b897
+		fprintf(stderr, "Cannot open rtnetlink\n");
a4b897
+		exit(1);
a4b897
+	}
a4b897
+
a4b897
+	dir = opendir(NETNS_RUN_DIR);
a4b897
+	if (!dir)
a4b897
+		return;
a4b897
+
a4b897
+	while ((entry = readdir(dir)) != NULL) {
a4b897
+		if (strcmp(entry->d_name, ".") == 0)
a4b897
+			continue;
a4b897
+		if (strcmp(entry->d_name, "..") == 0)
a4b897
+			continue;
a4b897
+		nsid = get_netnsid_from_name(entry->d_name);
a4b897
+
a4b897
+		if (nsid >= 0)
a4b897
+			netns_map_add(nsid, entry->d_name);
a4b897
+	}
a4b897
+	closedir(dir);
a4b897
+	initialized = 1;
a4b897
+}
a4b897
+
a4b897
+static int netns_get_name(int nsid, char *name)
a4b897
+{
a4b897
+	struct dirent *entry;
a4b897
+	DIR *dir;
a4b897
+	int id;
a4b897
+
a4b897
+	dir = opendir(NETNS_RUN_DIR);
a4b897
+	if (!dir)
a4b897
+		return -ENOENT;
a4b897
+
a4b897
+	while ((entry = readdir(dir)) != NULL) {
a4b897
+		if (strcmp(entry->d_name, ".") == 0)
a4b897
+			continue;
a4b897
+		if (strcmp(entry->d_name, "..") == 0)
a4b897
+			continue;
a4b897
+		id = get_netnsid_from_name(entry->d_name);
a4b897
+
a4b897
+		if (nsid == id) {
a4b897
+			strcpy(name, entry->d_name);
a4b897
+			closedir(dir);
a4b897
+			return 0;
a4b897
+		}
a4b897
+	}
a4b897
+	closedir(dir);
a4b897
+	return -ENOENT;
a4b897
+}
a4b897
+
a4b897
+int print_nsid(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
a4b897
+{
a4b897
+	struct rtgenmsg *rthdr = NLMSG_DATA(n);
a4b897
+	struct rtattr *tb[NETNSA_MAX+1];
a4b897
+	int len = n->nlmsg_len;
a4b897
+	FILE *fp = (FILE *)arg;
a4b897
+	struct nsid_cache *c;
a4b897
+	char name[NAME_MAX];
a4b897
+	int nsid;
a4b897
+
a4b897
+	if (n->nlmsg_type != RTM_NEWNSID && n->nlmsg_type != RTM_DELNSID)
a4b897
+		return 0;
a4b897
+
a4b897
+	len -= NLMSG_SPACE(sizeof(*rthdr));
a4b897
+	if (len < 0) {
a4b897
+		fprintf(stderr, "BUG: wrong nlmsg len %d in %s\n", len,
a4b897
+			__func__);
a4b897
+		return -1;
a4b897
+	}
a4b897
+
a4b897
+	parse_rtattr(tb, NETNSA_MAX, NETNS_RTA(rthdr), len);
a4b897
+	if (tb[NETNSA_NSID] == NULL) {
a4b897
+		fprintf(stderr, "BUG: NETNSA_NSID is missing %s\n", __func__);
a4b897
+		return -1;
a4b897
+	}
a4b897
+
a4b897
+	if (n->nlmsg_type == RTM_DELNSID)
a4b897
+		fprintf(fp, "Deleted ");
a4b897
+
a4b897
+	nsid = rta_getattr_u32(tb[NETNSA_NSID]);
a4b897
+	fprintf(fp, "nsid %u ", nsid);
a4b897
+
a4b897
+	c = netns_map_get_by_nsid(nsid);
a4b897
+	if (c != NULL) {
a4b897
+		fprintf(fp, "(iproute2 netns name: %s)", c->name);
a4b897
+		netns_map_del(c);
a4b897
+	}
a4b897
+
a4b897
+	/* During 'ip monitor nsid', no chance to have new nsid in cache. */
a4b897
+	if (c == NULL && n->nlmsg_type == RTM_NEWNSID)
a4b897
+		if (netns_get_name(nsid, name) == 0) {
a4b897
+			fprintf(fp, "(iproute2 netns name: %s)", name);
a4b897
+			netns_map_add(nsid, name);
a4b897
+		}
a4b897
+
a4b897
+	fprintf(fp, "\n");
a4b897
+	fflush(fp);
a4b897
+	return 0;
a4b897
+}
a4b897
+
a4b897
+static int netns_list_id(int argc, char **argv)
a4b897
+{
a4b897
+	if (!ipnetns_have_nsid()) {
a4b897
+		fprintf(stderr,
a4b897
+			"RTM_GETNSID is not supported by the kernel.\n");
a4b897
+		return -ENOTSUP;
a4b897
+	}
a4b897
+
a4b897
+	if (rtnl_wilddump_request(&rth, AF_UNSPEC, RTM_GETNSID) < 0) {
a4b897
+		perror("Cannot send dump request");
a4b897
+		exit(1);
a4b897
+	}
a4b897
+	if (rtnl_dump_filter(&rth, print_nsid, stdout) < 0) {
a4b897
+		fprintf(stderr, "Dump terminated\n");
a4b897
+		exit(1);
a4b897
+	}
a4b897
+	return 0;
a4b897
+}
a4b897
+
a4b897
 static int netns_list(int argc, char **argv)
a4b897
 {
a4b897
 	struct dirent *entry;
a4b897
@@ -577,6 +773,8 @@ static int netns_monitor(int argc, char **argv)
a4b897
 
a4b897
 int do_netns(int argc, char **argv)
a4b897
 {
a4b897
+	netns_map_init();
a4b897
+
a4b897
 	if (argc < 1)
a4b897
 		return netns_list(0, NULL);
a4b897
 
a4b897
@@ -584,6 +782,9 @@ int do_netns(int argc, char **argv)
a4b897
 	    (matches(*argv, "lst") == 0))
a4b897
 		return netns_list(argc-1, argv+1);
a4b897
 
a4b897
+	if ((matches(*argv, "list-id") == 0))
a4b897
+		return netns_list_id(argc-1, argv+1);
a4b897
+
a4b897
 	if (matches(*argv, "help") == 0)
a4b897
 		return usage();
a4b897
 
a4b897
diff --git a/lib/ll_map.c b/lib/ll_map.c
a4b897
index fd7db55..a57a150 100644
a4b897
--- a/lib/ll_map.c
a4b897
+++ b/lib/ll_map.c
a4b897
@@ -52,7 +52,7 @@ static struct ll_cache *ll_get_by_index(unsigned index)
a4b897
 	return NULL;
a4b897
 }
a4b897
 
a4b897
-static unsigned namehash(const char *str)
a4b897
+unsigned namehash(const char *str)
a4b897
 {
a4b897
 	unsigned hash = 5381;
a4b897
 
a4b897
diff --git a/man/man8/ip-monitor.8 b/man/man8/ip-monitor.8
a4b897
index b6e8d1d..a710b34 100644
a4b897
--- a/man/man8/ip-monitor.8
a4b897
+++ b/man/man8/ip-monitor.8
a4b897
@@ -32,7 +32,7 @@ command is the first in the command line and then the object list follows:
a4b897
 is the list of object types that we want to monitor.
a4b897
 It may contain
a4b897
 .BR link ", " address ", " route ", " mroute ", " prefix ", "
a4b897
-.BR neigh " and " netconf "."
a4b897
+.BR neigh ", " netconf " and " nsid "."
a4b897
 If no
a4b897
 .B file
a4b897
 argument is given,
a4b897
diff --git a/man/man8/ip-netns.8 b/man/man8/ip-netns.8
a4b897
index 80a4ad1..c9b0fbc 100644
a4b897
--- a/man/man8/ip-netns.8
a4b897
+++ b/man/man8/ip-netns.8
a4b897
@@ -42,6 +42,9 @@ ip-netns \- process network namespace management
a4b897
 .ti -8
a4b897
 .BR "ip netns monitor"
a4b897
 
a4b897
+.ti -8
a4b897
+.BR "ip netns list-id"
a4b897
+
a4b897
 .SH DESCRIPTION
a4b897
 A network namespace is logically another copy of the network stack,
a4b897
 with its own routes, firewall rules, and network devices.
a4b897
@@ -178,6 +181,13 @@ executing.
a4b897
 This command watches network namespace name addition and deletion events
a4b897
 and prints a line for each event it sees.
a4b897
 
a4b897
+.TP
a4b897
+.B ip netns list-id - list network namespace ids (nsid)
a4b897
+.sp
a4b897
+Network namespace ids are used to identify a peer network namespace. This
a4b897
+command displays nsid of the current network namespace and provides the
a4b897
+corresponding iproute2 netns name (from /var/run/netns) if any.
a4b897
+
a4b897
 .SH EXAMPLES
a4b897
 .PP
a4b897
 ip netns list