ip netns: Run over all netns
Subsystems (3)
cover ip netns: Run over all netns
From: Vadim Kochan <[email protected]>
Allow 'ip netns del' and 'ip netns exec' run over each network namespace names.
'ip netns exec' executes command forcely on eacn nsname.
Vadim Kochan (3):
lib: Exec func on each netns
ip netns: Allow exec on each netns
ip netns: Delete all netns
include/namespace.h | 6 ++++ include/utils.h | 5 +++ ip/ipnetns.c | 96 ++++++++++++++++++++++++++++++++--------------------- lib/namespace.c | 22 ++++++++++++ lib/utils.c | 28 ++++++++++++++++ man/man8/ip-netns.8 | 26 ++++++++++++--- 6 files changed, 141 insertions(+), 42 deletions(-)
Signature · 1 line
2.1.3
PATCH 1/3 lib: Exec func on each netns
From: Vadim Kochan <[email protected]>
Added possibility to run some func on each netns.
--- include/namespace.h | 6 ++++++ include/utils.h | 5 +++++ lib/namespace.c | 22 ++++++++++++++++++++++ lib/utils.c | 28 ++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+)
include/namespace.h +6 -0
@@ -42,5 +42,11 @@ static int setns(int fd, int nstype)#endif /* HAVE_SETNS */ extern int netns_switch (char *netns); +extern int netns_foreach (int (*func)(char *nsname,void *arg),void *arg); + +struct netns_func { +int (*func)(char *nsname,void *arg); +void *arg; +};#endif /* __NAMESPACE_H__ */
include/utils.h +5 -0
@@ -5,6 +5,7 @@#include <asm/types.h> #include <resolv.h> #include <stdlib.h> +#include <stdbool.h> #include "libnetlink.h" #include "ll_map.h"
@@ -160,4 +161,8 @@ struct iplink_req;int iplink_parse (int argc,char **argv,struct iplink_req *req,char **name,char **type,char **link,char **dev,int *group,int *index); + +extern int do_each_netns (int (*func)(char *nsname,void *arg),void *arg, +bool show_label); +#endif /* __UTILS_H__ */
lib/namespace.c +22 -0
@@ -84,3 +84,25 @@ int netns_switch(char *name)bind_etc (name);return 0 ; } + +int netns_foreach (int (*func)(char *nsname,void *arg),void *arg) +{ +DIR *dir; +struct dirent *entry; + + dir =opendir (NETNS_RUN_DIR ); +if (!dir) +return -1 ; + +while ((entry =readdir (dir)) !=NULL ) { +if (strcmp (entry->d_name,"." ) ==0 ) +continue ; +if (strcmp (entry->d_name,".." ) ==0 ) +continue ; +if (func (entry->d_name, arg)) +break ; + } + +closedir (dir); +return 0 ; +}
lib/utils.c +28 -0
@@ -31,6 +31,7 @@#include "utils.h" +#include "namespace.h" int timestamp_short =0 ;
@@ -868,3 +869,30 @@ int inet_get_addr(const char *src, __u32 *dst, struct in6_addr *dst6)else return inet_pton (AF_INET , src, dst); } + +static int on_netns (char *nsname,void *arg) +{ +struct netns_func *f = arg; + +if (netns_switch (nsname)) +return -1 ; + +return f->func (nsname, f->arg); +} + +static int on_netns_label (char *nsname,void *arg) +{ +printf ("\nnetns: %s\n" , nsname); +return on_netns (nsname, arg); +} + +int do_each_netns (int (*func)(char *nsname,void *arg),void *arg, +bool show_label) +{ +struct netns_func nsf = { .func = func, .arg = arg }; + +if (show_label) +return netns_foreach (on_netns_label, &nsf); + +return netns_foreach (on_netns, &nsf); +}
Signature · 1 line
2.1.3
PATCH 2/3 ip netns: Allow exec on each netns
In reply to Vadim KochanFrom: Vadim Kochan <[email protected]>
This change allows to exec some cmd on each netns (except default) by specifying 'all' as netns name:
# ip netns exec all ip link
Each command executes synchronously.
Exit status is not considered, so there might be a case that some CMD can fail on some netns but success on the other.
EXAMPLES:
1) Show link info on all netns:
$ ip netns exec all ip link
netns: test_net
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
4: tap0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 500
link/ether 1a:19:6f:25:eb:85 brd ff:ff:ff:ff:ff:ffnetns: home0
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
4: tap0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 500
link/ether ea:1a:59:40:d3:29 brd ff:ff:ff:ff:ff:ffnetns: lan0
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
4: tap0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 500
link/ether ce:49:d5:46:81:ea brd ff:ff:ff:ff:ff:ff2) Set UP tap0 device for the all netns:
$ ip netns exec all ip link set dev tap0 up
netns: test_net
netns: home0
netns: lan0
--- ip/ipnetns.c | 72 ++++++++++++++++++++++++++++++++--------------------- man/man8/ip-netns.8 | 14 +++++++++-- 2 files changed, 55 insertions(+), 31 deletions(-)
ip/ipnetns.c +43 -29
@@ -26,7 +26,7 @@ static int usage(void)fprintf (stderr," ip netns delete NAME\n" );fprintf (stderr," ip netns identify [PID]\n" );fprintf (stderr," ip netns pids NAME\n" ); -fprintf (stderr," ip netns exec NAME cmd ...\n" ); +fprintf (stderr," ip netns exec { NAME | all } cmd ...\n" );fprintf (stderr," ip netns monitor\n" );exit (-1 ); }
@@ -66,29 +66,10 @@ static int netns_list(int argc, char **argv)return 0 ; } -static int netns_exec (int argc,char **argv) +static int cmd_exec (const char *cmd,char **argv,bool do_fork) { -/* Setup the proper environment for apps that are not netns -* aware, and execute a program in that environment. -*/ -const char *cmd; - -if (argc <1 ) { -fprintf (stderr,"No netns name specified\n" ); -return -1 ; - } -if (argc <2 ) { -fprintf (stderr,"No command specified\n" ); -return -1 ; - } - cmd = argv[1 ]; - -if (netns_switch (argv[0 ])) -return -1 ; -fflush (stdout); - -if (batch_mode) { +if (do_fork) {int status;pid_t pid;
@@ -106,23 +87,56 @@ static int netns_exec(int argc, char **argv) }if (WIFEXITED (status)) { -/* ip must return the status of the child, -* but do_cmd() will add a minus to this, -* so let's add another one here to cancel it. -*/ -return -WEXITSTATUS (status); +return WEXITSTATUS (status); }exit (1 ); } } -if (execvp (cmd, argv +1 ) <0 ) +if (execvp (cmd, argv) <0 )fprintf (stderr,"exec of \"%s\" failed: %s\n" , - cmd,strerror (errno)); + cmd,strerror (errno));_exit (1 ); } +static int on_netns_exec (char *nsname,void *arg) +{ +char **argv = arg; +cmd_exec (argv[1 ], argv +1 ,1 ); +return 0 ; +} + +static int netns_exec (int argc,char **argv) +{ +/* Setup the proper environment for apps that are not netns +* aware, and execute a program in that environment. +*/ +const char *cmd; + +if (argc <1 ) { +fprintf (stderr,"No netns name specified\n" ); +return -1 ; + } +if (argc <2 ) { +fprintf (stderr,"No command specified\n" ); +return -1 ; + } + cmd = argv[1 ]; + +if (strcmp (argv[0 ],"all" ) ==0 ) +return do_each_netns (on_netns_exec, argv,1 ); + +if (netns_switch (argv[0 ])) +return -1 ; + +/* ip must return the status of the child, +* but do_cmd() will add a minus to this, +* so let's add another one here to cancel it. +*/ +return -cmd_exec (cmd, argv +1 , batch_mode); +} +static int is_pid (const char *str) {int ch;
man/man8/ip-netns.8 +12 -2
@@ -29,7 +29,7 @@ ip-netns \- process network namespace management .ti -8 .BR "ip netns exec " -.I NETNSNAME command ... +.RI "{ " NETNSNAME " | " all " } " command ... .ti -8 .BR "ip netns monitor"
@@ -98,7 +98,7 @@ This command walks through proc and finds all of the process who have the named network namespace as their primary network namespace. .TP -.B ip netns exec NAME cmd ... - Run cmd in the named network namespace +.B ip netns exec { NAME | all } cmd ... - Run cmd in the named network namespace .sp This command allows applications that are network namespace unaware to be run in something other than the default network namespace with
@@ -107,6 +107,16 @@ in the customary global locations. A network namespace and bind mounts are used to move files from their network namespace specific location to their default locations without affecting other processes. +If +.B all +was specified then +.B cmd +will be executed synchronously on the each named network namespace even if +.B cmd +fails on some of them. Network namespace name is printed on each +.B cmd +executing. + .TP .B ip netns monitor - Report as network namespace names are added and deleted .sp
Signature · 1 line
2.1.3
PATCH 3/3 ip netns: Delete all netns
In reply to Vadim KochanFrom: Vadim Kochan <[email protected]>
Allow delete all namespace names by:
$ ip netns del all
--- ip/ipnetns.c | 24 +++++++++++++++--------- man/man8/ip-netns.8 | 12 ++++++++++-- 2 files changed, 25 insertions(+), 11 deletions(-)
ip/ipnetns.c +15 -9
@@ -274,18 +274,11 @@ static int netns_identify(int argc, char **argv) } -static int netns_delete (int argc,char **argv) +static int on_netns_del (char *nsname,void *arg) { -const char *name;char netns_path[MAXPATHLEN ]; -if (argc <1 ) { -fprintf (stderr,"No netns name specified\n" ); -return -1 ; - } - - name = argv[0 ]; -snprintf (netns_path,sizeof (netns_path),"%s/%s" ,NETNS_RUN_DIR , name); +snprintf (netns_path,sizeof (netns_path),"%s/%s" ,NETNS_RUN_DIR , nsname);umount2 (netns_path,MNT_DETACH );if (unlink (netns_path) <0 ) { fprintf(stderr, "Cannot remove namespace file \"%s\": %s\n",
@@ -295,6 +288,19 @@ static int netns_delete(int argc, char **argv)return 0 ; } +static int netns_delete (int argc,char **argv) +{ +if (argc <1 ) { +fprintf (stderr,"No netns name specified\n" ); +return -1 ; + } + +if (strcmp (argv[0 ],"all" ) ==0 ) +return netns_foreach (on_netns_del,NULL ); + +return on_netns_del (argv[0 ],NULL ); +} +static int create_netns_dir (void ) {/* Create the base netns directory if it doesn't exist */
man/man8/ip-netns.8 +10 -2
@@ -16,10 +16,14 @@ ip-netns \- process network namespace management .BR "ip netns" " { " list " } " .ti -8 -.BR "ip netns" " { " add " | " delete " } " +.B ip netns add .I NETNSNAME .ti -8 +.B ip netns del +.RI "{ " NETNSNAME " | " all " }" + +.ti -8 .BR "ip netns identify" .RI "[ " PID " ]"
@@ -76,7 +80,7 @@ If NAME is available in /var/run/netns/ this command creates a new network namespace and assigns NAME. .TP -.B ip netns delete NAME - delete the name of a network namespace +.B ip netns delete { NAME | all } - delete the name of a network namespace(s) .sp If NAME is present in /var/run/netns it is umounted and the mount point is removed. If this is the last user of the network namespace the
@@ -84,6 +88,10 @@ network namespace will be freed, otherwise the network namespace persists until it has no more users. ip netns delete may fail if the mount point is in use in another mount namespace. +If +.B all +was specified then all the network namespace names will be removed. + .TP .B ip netns identify [PID] - Report network namespaces names for process .sp
Signature · 1 line
2.1.3
Re: [PATCH iproute2 3/3] ip netns: Delete all netns
Quoted 5 lines · Vadim Kochan
From: Vadim Kochan <[email protected]>
Allow delete all namespace names by:
$ ip netns del all
So I can still create a namespace called 'all', but can't exec in it or delete it independently with this change. Perhaps you need to block that as well? Unless there's some other patch I'm missing?
-Brian
Re: [PATCH iproute2 3/3] ip netns: Delete all netns
Quoted 5 lines · Brian Haley
Quoted 4 lines · Vadim Kochan
From: Vadim Kochan <[email protected]>
Allow delete all namespace names by:
Quoted 7 lines
Quoted 1 line
$ ip netns del allSo I can still create a namespace called 'all', but can't exec in it or delete it independently with this change. Perhaps you need to block that as well? Unless there's some other patch I'm missing?
-Brian
Hm, I did not take it into account ...
I will look if I can find another way ...
Thanks,
Re: [PATCH iproute2 3/3] ip netns: Delete all netns
Quoted 12 lines · Vadim Kochan
Quoted 11 lines · Brian Haley
Quoted 5 lines · Vadim Kochan
From: Vadim Kochan <[email protected]>
Allow delete all namespace names by:
$ ip netns del allSo I can still create a namespace called 'all', but can't exec in it or delete it independently with this change. Perhaps you need to block that as well? Unless there's some other patch I'm missing?
Quoted 5 lines
Quoted 1 line
-Brian
Hm, I did not take it into account ...
I will look if I can find another way ...Thanks,
what about this ?
$ ip netns exec / ip link
$ ip netns del /so it make a sense to be as root directory of bound ns names in /var/run/netns/ ? what do you think ?
Regards,
Re: [PATCH iproute2 3/3] ip netns: Delete all netns
Quoted 19 lines · Vadim Kochan
Quoted 12 lines · Vadim Kochan
Quoted 11 lines · Brian Haley
Quoted 5 lines · Vadim Kochan
From: Vadim Kochan <[email protected]>
Allow delete all namespace names by:
$ ip netns del allSo I can still create a namespace called 'all', but can't exec in it or delete it independently with this change. Perhaps you need to block that as well? Unless there's some other patch I'm missing?
Quoted 5 lines
Quoted 1 line
-Brian
Hm, I did not take it into account ...
I will look if I can find another way ...Thanks,
Quoted 7 lines
what about this ?
$ ip netns exec / ip link $ ip netns del /so it make a sense to be as root directory of bound ns names in
/var/run/netns/? what do you think ?
I think using / is confusing. And something like -a[ll] as an option doesn't seem right either.
Or you just trap the name "all" in the add case and don't allow it.
Just my opinion.
-Brian
Re: [PATCH iproute2 3/3] ip netns: Delete all netns
Quoted 28 lines · Brian Haley
Quoted 19 lines · Vadim Kochan
Quoted 12 lines · Vadim Kochan
Quoted 11 lines · Brian Haley
Quoted 5 lines · Vadim Kochan
From: Vadim Kochan <[email protected]>
Allow delete all namespace names by:
$ ip netns del allSo I can still create a namespace called 'all', but can't exec in it or delete it independently with this change. Perhaps you need to block that as well? Unless there's some other patch I'm missing?
Quoted 5 lines
Quoted 1 line
-Brian
Hm, I did not take it into account ...
I will look if I can find another way ...Thanks,
Quoted 7 lines
what about this ?
$ ip netns exec / ip link $ ip netns del /so it make a sense to be as root directory of bound ns names in
/var/run/netns/? what do you think ?
Quoted 8 lines
I think using / is confusing. And something like -a[ll] as an option doesn't seem right either.
Or you just trap the name "all" in the add case and don't allow it.
Just my opinion.
-Brian
So I think that do not allow to add netns "all" can be a solution, I'd like to hear from other people if it might be OK.
Thanks,
Re: [PATCH iproute2 3/3] ip netns: Delete all netns
Quoted 3 lines · Vadim Kochan
So I think that do not allow to add netns "all" can be a solution, I'd like to hear from other people if it might be OK.
Nope, that would break existing scripts using "all" as a netns.
Quoted 6 lines · Vadim Kochan
From: Vadim Kochan <[email protected]>
Allow 'ip netns del' and 'ip netns exec' run over each network namespace names.
'ip netns exec' executes command forcely on eacn nsname.
Why this has to be done in iproute command?
That is, why not just offloading this to a shell script like below?
for ns in ip netns show;
do
ip netns exec $ns ip link show.....
done
Quoted 8 lines · Cong Wang
Quoted 6 lines · Vadim Kochan
From: Vadim Kochan <[email protected]>
Allow 'ip netns del' and 'ip netns exec' run over each network namespace names.
'ip netns exec' executes command forcely on eacn nsname.
Quoted 7 lines
Why this has to be done in iproute command?
That is, why not just offloading this to a shell script like below?for ns in
ip netns show;
do
ip netns exec $ns ip link show.....
done
Hm, but would not it better to have it in iproute instead of collect scripts ? Scripts allows to do a lot of things, but in this case it seems like a feature which related to iproute.
Re: [PATCH iproute2 3/3] ip netns: Delete all netns
In reply to Vadim KochanQuoted 5 lines · Vadim Kochan
Quoted 1 line · Brian Haley
Or you just trap the name "all" in the add case and don't allow it.
So I think that do not allow to add netns "all" can be a solution, I'd like to hear from other people if it might be OK.
You can still add the name manually (there is software out there that adds symlinks to /ver/run/netns), so nope, that's not a solution.
Generally, the only special names that could be used would be those containing a letter not allowed in file name, i.e. \0 or '/'. Neither of those sounds thrilling. I'm afraid the only working solution is a new option.
Jiri
Signature · 1 line
Jiri Benc
Re: [PATCH iproute2 3/3] ip netns: Delete all netns
Ok,
If I will re-work to use new option, would it be useful ? So it will look:
$ ip -all netns del
$ ip -all netns exec ip link
$ ip -all netns exec ip route add ...Seems not so weird to me ?
Thanks,
Quoted 19 lines · Jiri Benc
Quoted 5 lines · Vadim Kochan
Quoted 1 line · Brian Haley
Or you just trap the name "all" in the add case and don't allow it.
So I think that do not allow to add netns "all" can be a solution, I'd like to hear from other people if it might be OK.
You can still add the name manually (there is software out there that adds symlinks to /ver/run/netns), so nope, that's not a solution.
Generally, the only special names that could be used would be those containing a letter not allowed in file name, i.e. \0 or '/'. Neither of those sounds thrilling. I'm afraid the only working solution is a new option.
Jiri
--
Jiri Benc
Re: [PATCH iproute2 3/3] ip netns: Delete all netns
Quoted 9 lines · Vadim Kochan
Ok,
If I will re-work to use new option, would it be useful ? So it will look:
$ ip -all netns del $ ip -all netns exec ip link $ ip -all netns exec ip route add ...Seems not so weird to me ?
What about making this new option only for the 'netns' subsystem? Something like: 'ip netns -all exec'?
Regards,
Nicolas
Quoted 12 lines · Vadim Kochan
Quoted 11 lines · Cong Wang
Quoted 6 lines · Vadim Kochan
From: Vadim Kochan <[email protected]>
Allow 'ip netns del' and 'ip netns exec' run over each network namespace names.
'ip netns exec' executes command forcely on eacn nsname.
Why this has to be done in iproute command?
That is, why not just offloading this to a shell script like below?
Quoted 8 lines
Quoted 4 lines
for ns in
ip netns show;
do
ip netns exec $ns ip link show.....
doneHm, but would not it better to have it in iproute instead of collect scripts ? Scripts allows to do a lot of things, but in this case it seems like a feature which related to iproute.
iproute2 should keep a minimum set of features especially when a one-liner shell script can do that.
Quoted 18 lines · Cong Wang
Quoted 17 lines · Vadim Kochan
Quoted 15 lines · Cong Wang
Quoted 6 lines · Vadim Kochan
From: Vadim Kochan <[email protected]>
Allow 'ip netns del' and 'ip netns exec' run over each network namespace names.
'ip netns exec' executes command forcely on eacn nsname.
Why this has to be done in iproute command?
That is, why not just offloading this to a shell script like below?for ns in
ip netns show;
do
ip netns exec $ns ip link show.....
done
Quoted 6 lines
Quoted 3 lines
Hm, but would not it better to have it in iproute instead of collect scripts ? Scripts allows to do a lot of things, but in this case it seems like a feature which related to iproute.
iproute2 should keep a minimum set of features especially when a one-liner shell script can do that.
BTW, this script should have additional output of netns name before the 'ip netns exec $ns ...' invocation.
Quoted 11 lines · On Wed, 7 Jan 2015 13:04:19 +0200 Vadim Kochan <[email protected]> wrote:
From: Vadim Kochan <[email protected]>
Allow 'ip netns del' and 'ip netns exec' run over each network namespace names.
'ip netns exec' executes command forcely on eacn nsname.
Vadim Kochan (3):
lib: Exec func on each netns
ip netns: Allow exec on each netns
ip netns: Delete all netns
Quoted 8 lines
include/namespace.h | 6 ++++ include/utils.h | 5 +++ ip/ipnetns.c | 96 ++++++++++++++++++++++++++++++++--------------------- lib/namespace.c | 22 ++++++++++++ lib/utils.c | 28 ++++++++++++++++ man/man8/ip-netns.8 | 26 ++++++++++++--- 6 files changed, 141 insertions(+), 42 deletions(-)
It is a useful concept but as others have pointed out the idea of reserving a keyword 'all' at this point is likely to cause somebody to break. So sorry.
Quoted 23 lines · Stephen Hemminger
Quoted 11 lines · On Wed, 7 Jan 2015 13:04:19 +0200 Vadim Kochan <[email protected]> wrote:
From: Vadim Kochan <[email protected]>
Allow 'ip netns del' and 'ip netns exec' run over each network namespace names.
'ip netns exec' executes command forcely on eacn nsname.
Vadim Kochan (3):
lib: Exec func on each netns
ip netns: Allow exec on each netns
ip netns: Delete all netnsQuoted 8 lines
include/namespace.h | 6 ++++ include/utils.h | 5 +++ ip/ipnetns.c | 96 ++++++++++++++++++++++++++++++++--------------------- lib/namespace.c | 22 ++++++++++++ lib/utils.c | 28 ++++++++++++++++ man/man8/ip-netns.8 | 26 ++++++++++++--- 6 files changed, 141 insertions(+), 42 deletions(-)
Quoted 3 lines
It is a useful concept but as others have pointed out the idea of reserving a keyword 'all' at this point is likely to cause somebody to break. So sorry.
OK, then what about additional option like:
$ ip -all netns exec ...
?
Thanks,