diff options
author | Anton Aksola <aakso@iki.fi> | 2016-09-20 06:01:27 +0000 |
---|---|---|
committer | Stephen Hemminger <stephen@networkplumber.org> | 2016-10-09 18:56:47 -0700 |
commit | e29a8e0537c419aa0a612db39b0feb45148a8350 (patch) | |
tree | b724362b519f57627f7a008a1968005814dd9ac3 /ip/ipnetns.c | |
parent | d99272470a0f691d592626d4d33fedf6cb647f92 (diff) |
iproute2: build nsid-name cache only for commands that need it
The calling of netns_map_init() before command parsing introduced
a performance issue with large number of namespaces.
As commands such as add, del and exec do not need to iterate through
/var/run/netns it would be good not no build the cache before executing
these commands.
Example:
unpatched:
time seq 1 1000 | xargs -n 1 ip netns add
real 0m16.832s
user 0m1.350s
sys 0m15.029s
patched:
time seq 1 1000 | xargs -n 1 ip netns add
real 0m3.859s
user 0m0.132s
sys 0m3.205s
Signed-off-by: Anton Aksola <aakso@iki.fi>
Acked-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
Diffstat (limited to 'ip/ipnetns.c')
-rw-r--r-- | ip/ipnetns.c | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/ip/ipnetns.c b/ip/ipnetns.c index ccc652c1..bd1e9013 100644 --- a/ip/ipnetns.c +++ b/ip/ipnetns.c @@ -194,6 +194,18 @@ static void netns_map_del(struct nsid_cache *c) free(c); } +void netns_nsid_socket_init(void) +{ + if (rtnsh.fd > -1 || !ipnetns_have_nsid()) + return; + + if (rtnl_open(&rtnsh, 0) < 0) { + fprintf(stderr, "Cannot open rtnetlink\n"); + exit(1); + } + +} + void netns_map_init(void) { static int initialized; @@ -204,11 +216,6 @@ void netns_map_init(void) if (initialized || !ipnetns_have_nsid()) return; - if (rtnl_open(&rtnsh, 0) < 0) { - fprintf(stderr, "Cannot open rtnetlink\n"); - exit(1); - } - dir = opendir(NETNS_RUN_DIR); if (!dir) return; @@ -775,17 +782,23 @@ static int netns_monitor(int argc, char **argv) int do_netns(int argc, char **argv) { - netns_map_init(); + netns_nsid_socket_init(); - if (argc < 1) + if (argc < 1) { + netns_map_init(); return netns_list(0, NULL); + } if ((matches(*argv, "list") == 0) || (matches(*argv, "show") == 0) || - (matches(*argv, "lst") == 0)) + (matches(*argv, "lst") == 0)) { + netns_map_init(); return netns_list(argc-1, argv+1); + } - if ((matches(*argv, "list-id") == 0)) + if ((matches(*argv, "list-id") == 0)) { + netns_map_init(); return netns_list_id(argc-1, argv+1); + } if (matches(*argv, "help") == 0) return usage(); |