Skip to contentSkip to conversation

ip: Simplify executing ip cmd within network ns

PATCH Superseded Vadim Kochan 2 participants 3 messages Dec 11, 2014

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ip: Simplify executing ip cmd within network ns

From: Vadim Kochan <[email protected]>

Added new '-netns' option to simplify executing following cmd:

ip netns exec NETNS ip OPTIONS COMMAND OBJECT

to

ip -n[etns] NETNS OPTIONS COMMAND OBJECT

e.g.:

ip -net vnet0 link add br0 type bridge
ip -n vnet0 link

---
Changes v1 -> v2
use -n[etns] option name: suggested by Nicolas Dichtel changed man ip.8 page

 ip/ip.c        |  6 ++++++
 ip/ip_common.h |  1 +
 ip/ipnetns.c   |  2 +-
 man/man8/ip.8  | 24 +++++++++++++++++++++++-
 4 files changed, 31 insertions(+), 2 deletions(-)
ip/ip.c +6 -0
index 5f759d5..f3c2cdb 100644
@@ -262,6 +262,12 @@ int main(int argc, char **argv)
 			rcvbuf = size;
 		} else if (matches(opt, "-help") == 0) {
 			usage();
+		} else if (matches(opt, "-netns") == 0) {
+			argc--;
+			argv++;
+			argv[0] = argv[1];
+			argv[1] = basename;
+			return netns_exec(argc, argv);
 		} else {
 			fprintf(stderr, "Option \"%s\" is unknown, try \"ip -help\".\n", opt);
 			exit(-1);
ip/ip_common.h +1 -0
index 75bfb82..d4f7e1f 100644
@@ -88,6 +88,7 @@ struct link_util
 struct link_util *get_link_kind(const char *kind);
 struct link_util *get_link_slave_kind(const char *slave_kind);
 int get_netns_fd(const char *name);
+int netns_exec(int argc, char **argv);
 
 #ifndef	INFINITY_LIFE_TIME
 #define     INFINITY_LIFE_TIME      0xFFFFFFFFU
ip/ipnetns.c +1 -1
index 1c8aa02..367841c 100644
@@ -129,7 +129,7 @@ static void bind_etc(const char *name)
 	closedir(dir);
 }
 
-static int netns_exec(int argc, char **argv)
+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.
man/man8/ip.8 +23 -1
index 2d42e98..389c808 100644
@@ -31,7 +31,8 @@ ip \- show / manipulate routing, devices, policy routing and tunnels
 \fB\-r\fR[\fIesolve\fR] |
 \fB\-f\fR[\fIamily\fR] {
 .BR inet " | " inet6 " | " ipx " | " dnet " | " link " } | "
-\fB\-o\fR[\fIneline\fR] }
+\fB\-o\fR[\fIneline\fR] |
+\fB\-n\fR[\fIetns\fR] }
 
 
 .SH OPTIONS
@@ -134,6 +135,27 @@ the output.
 use the system's name resolver to print DNS names instead of
 host addresses.
 
+.TP
+.BR "\-n" , " \-net" , " \-netns " <NETNS>
+executes the following
+.RI "[ " OPTIONS " ] " OBJECT " { " COMMAND " | "
+.BR help " }"
+in the specified network namespace
+.IR NETNS .
+Actually it just simplifies executing of:
+
+.B ip netns exec
+.IR NETNS
+.B ip
+.RI "[ " OPTIONS " ] " OBJECT " { " COMMAND " | "
+.BR help " }"
+
+to
+
+.B ip
+.RI "-n[etns] " NETNS " [ " OPTIONS " ] " OBJECT " { " COMMAND " | "
+.BR help " }"
+
 .SH IP - COMMAND SYNTAX
 
 .SS
Signature · 1 line
2.1.3
#1 lore raw
Quoted 32 lines · Thu, Dec 11, 2014 at 02:38:23PM CET, [email protected] wrote:

From: Vadim Kochan <[email protected]>

Added new '-netns' option to simplify executing following cmd:

ip netns exec NETNS ip OPTIONS COMMAND OBJECT

to

ip -n[etns] NETNS OPTIONS COMMAND OBJECT

e.g.:

ip -net vnet0 link add br0 type bridge
ip -n vnet0 link

---
Changes v1 -> v2
use -n[etns] option name: suggested by Nicolas Dichtel changed man ip.8 page

ip/ip.c        |  6 ++++++
ip/ip_common.h |  1 +
ip/ipnetns.c   |  2 +-
man/man8/ip.8  | 24 +++++++++++++++++++++++-
4 files changed, 31 insertions(+), 2 deletions(-)
ip/ip.c +0 -0
index 5f759d5..f3c2cdb 100644

ip/ip.c int main(int argc, char **argv) · line 270

Quoted 9 lines
ip/ip.c +6 -0
@@ -262,6 +262,12 @@ int main(int argc, char **argv)
 			rcvbuf = size;
 		} else if (matches(opt, "-help") == 0) {
 			usage();
+		} else if (matches(opt, "-netns") == 0) {
+			argc--;
+			argv++;
+			argv[0] = argv[1];
+			argv[1] = basename;
+			return netns_exec(argc, argv);

Can't the same functionality be done in the same ip process, meaning without execvp ip again? It would seem clearer to me.

How about other tools (tc,bridge,..) ? It would be nice to have the same option there as well.

Quoted 77 lines
ip/ip.c +0 -0
 		} else {
 			fprintf(stderr, "Option \"%s\" is unknown, try \"ip -help\".\n", opt);
 			exit(-1);
ip/ip_common.h +1 -0
index 75bfb82..d4f7e1f 100644
@@ -88,6 +88,7 @@ struct link_util
 struct link_util *get_link_kind(const char *kind);
 struct link_util *get_link_slave_kind(const char *slave_kind);
 int get_netns_fd(const char *name);
+int netns_exec(int argc, char **argv);
 
 #ifndef	INFINITY_LIFE_TIME
 #define     INFINITY_LIFE_TIME      0xFFFFFFFFU
ip/ipnetns.c +1 -1
index 1c8aa02..367841c 100644
@@ -129,7 +129,7 @@ static void bind_etc(const char *name)
 	closedir(dir);
 }
 
-static int netns_exec(int argc, char **argv)
+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.
man/man8/ip.8 +0 -0
index 2d42e98..389c808 100644
@@ -31,7 +31,8 @@ ip \- show / manipulate routing, devices, policy routing and tunnels

\fB\-r\fR[\fIesolve\fR] |
\fB\-f\fR[\fIamily\fR] {
.BR inet " | " inet6 " | " ipx " | " dnet " | " link " } | " -\fB\-o\fR[\fIneline\fR] } +\fB\-o\fR[\fIneline\fR] |
+\fB\-n\fR[\fIetns\fR] }

.SH OPTIONS

man/man8/ip.8 +0 -0
@@ -134,6 +135,27 @@ the output.

use the system's name resolver to print DNS names instead of host addresses.

+.TP
+.BR "\-n" , " \-net" , " \-netns " <NETNS>

man/man8/ip.8 +19 -0
+executes the following
+.RI "[ " OPTIONS " ] " OBJECT " { " COMMAND " | "
+.BR help " }"
+in the specified network namespace
+.IR NETNS .
+Actually it just simplifies executing of:
+
+.B ip netns exec
+.IR NETNS
+.B ip
+.RI "[ " OPTIONS " ] " OBJECT " { " COMMAND " | "
+.BR help " }"
+
+to
+
+.B ip
+.RI "-n[etns] " NETNS " [ " OPTIONS " ] " OBJECT " { " COMMAND " | "
+.BR help " }"
+

.SH IP - COMMAND SYNTAX

.SS

Signature · 6 lines
2.1.3

--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw
Quoted 28 lines · Jiri Pirko
Quoted 27 lines · Thu, Dec 11, 2014 at 02:38:23PM CET, [email protected] wrote:

From: Vadim Kochan <[email protected]>

Added new '-netns' option to simplify executing following cmd:

ip netns exec NETNS ip OPTIONS COMMAND OBJECT

to

ip -n[etns] NETNS OPTIONS COMMAND OBJECT

e.g.:

ip -net vnet0 link add br0 type bridge
ip -n vnet0 link

---
Changes v1 -> v2
use -n[etns] option name: suggested by Nicolas Dichtel changed man ip.8 page

ip/ip.c        |  6 ++++++
ip/ip_common.h |  1 +
ip/ipnetns.c   |  2 +-
man/man8/ip.8  | 24 +++++++++++++++++++++++-
4 files changed, 31 insertions(+), 2 deletions(-)
Quoted 15 lines
Quoted 5 lines
ip/ip.c +0 -0
index 5f759d5..f3c2cdb 100644

ip/ip.c int main(int argc, char **argv) · line 270

Quoted 9 lines
ip/ip.c +6 -0
@@ -262,6 +262,12 @@ int main(int argc, char **argv)
 			rcvbuf = size;
 		} else if (matches(opt, "-help") == 0) {
 			usage();
+		} else if (matches(opt, "-netns") == 0) {
+			argc--;
+			argv++;
+			argv[0] = argv[1];
+			argv[1] = basename;
+			return netns_exec(argc, argv);

Hi,

Quoted 4 lines

Can't the same functionality be done in the same ip process, meaning without execvp ip again? It would seem clearer to me.

Hm, yes, I will look on this ...

Quoted 3 lines

How about other tools (tc,bridge,..) ? It would be nice to have the same option there as well.

Sure, good idea, I will try.

Thanks,