Skip to contentSkip to conversation

ip: Simplify executing ip cmd within network ns

PATCH Superseded Vadim Kochan 2 participants 4 messages Dec 11, 2014 – Dec 12, 2014

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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 v2 -> v3
Switching to netns w/o execvp: suggested by Jiri Pirko
Jiri Benc

So I moved switching netns to lib/utils.c, looks
not very good for me, but it allows easy reuse this netns_switch func for other utils in the future (tc, bridge, ...).

May be it would be better to have separated lib/netns.c module and include/netns.h header ?

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

 include/utils.h |  27 +++++++++++++++
 ip/ip.c         |   4 +++
 ip/ipnetns.c    | 105 ++------------------------------------------------------
 lib/Makefile    |   4 +++
 lib/utils.c     |  88 +++++++++++++++++++++++++++++++++++++++++++++++
 man/man8/ip.8   |  23 ++++++++++++-
 6 files changed, 147 insertions(+), 104 deletions(-)
include/utils.h +27 -0
index eef9c42..1fd012c 100644
@@ -159,4 +159,31 @@ 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);
+
+#define NETNS_RUN_DIR "/var/run/netns"
+#define NETNS_ETC_DIR "/etc/netns"
+
+#ifndef CLONE_NEWNET
+#define CLONE_NEWNET 0x40000000	/* New network namespace (lo, device, names sockets, etc) */
+#endif
+
+#ifndef MNT_DETACH
+#define MNT_DETACH	0x00000002	/* Just detach from the tree */
+#endif /* MNT_DETACH */
+
+/* sys/mount.h may be out too old to have these */
+#ifndef MS_REC
+#define MS_REC		16384
+#endif
+
+#ifndef MS_SLAVE
+#define MS_SLAVE	(1 << 19)
+#endif
+
+#ifndef MS_SHARED
+#define MS_SHARED	(1 << 20)
+#endif
+
+extern int netns_switch(char *netns);
+
 #endif /* __UTILS_H__ */
ip/ip.c +4 -0
index 5f759d5..5da10bf 100644
@@ -262,6 +262,10 @@ int main(int argc, char **argv)
 			rcvbuf = size;
 		} else if (matches(opt, "-help") == 0) {
 			usage();
+		} else if (matches(opt, "-netns") == 0) {
+			NEXT_ARG();
+			if (netns_switch(argv[1]))
+				exit(-1);
 		} else {
 			fprintf(stderr, "Option \"%s\" is unknown, try \"ip -help\".\n", opt);
 			exit(-1);
ip/ipnetns.c +2 -103

131 lines not on this page · raw message

lib/Makefile +4 -0
index a42b885..f7cbc8d 100644
@@ -1,5 +1,9 @@
 include ../Config
 
+ifeq ($(IP_CONFIG_SETNS),y)
+	CFLAGS += -DHAVE_SETNS
+endif
+
 CFLAGS += -fPIC
 
 UTILOBJ=utils.o rt_names.o ll_types.o ll_proto.o ll_addr.o inet_proto.o
lib/utils.c +88 -0

98 lines not on this page · raw message

man/man8/ip.8 +22 -1
index 2d42e98..0fb759d 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,26 @@ the output.
 use the system's name resolver to print DNS names instead of
 host addresses.
 
+.TP
+.BR "\-n" , " \-net" , " \-netns " <NETNS>
+switches
+.B ip
+to 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 17 lines · On Fri, 12 Dec 2014 00:32:51 +0200 Vadim Kochan <[email protected]> wrote:
include/utils.h +17 -0
+
+#define NETNS_RUN_DIR "/var/run/netns"
+#define NETNS_ETC_DIR "/etc/netns"
+
+#ifndef CLONE_NEWNET
+#define CLONE_NEWNET 0x40000000	/* New network namespace (lo, device, names sockets, etc) */
+#endif
+
+#ifndef MNT_DETACH
+#define MNT_DETACH	0x00000002	/* Just detach from the tree */
+#endif /* MNT_DETACH */
+
+/* sys/mount.h may be out too old to have these */
+#ifndef MS_REC
+#define MS_REC		16384
+#endif
+

include/utils.h struct iplink_req; · line 188

Quoted 10 lines
include/utils.h +10 -0
+#ifndef MS_SLAVE
+#define MS_SLAVE	(1 << 19)
+#endif
+
+#ifndef MS_SHARED
+#define MS_SHARED	(1 << 20)
+#endif
+
+extern int netns_switch(char *netns);
+

Maybe it would be cleaner to introduce a new file netns.h with this and the setnetns syscall wrapper

#2 lore raw
Quoted 25 lines · Stephen Hemminger
Quoted 22 lines · On Fri, 12 Dec 2014 00:32:51 +0200 Vadim Kochan <[email protected]> wrote:
include/utils.h +22 -0
+
+#define NETNS_RUN_DIR "/var/run/netns"
+#define NETNS_ETC_DIR "/etc/netns"
+
+#ifndef CLONE_NEWNET
+#define CLONE_NEWNET 0x40000000	/* New network namespace (lo, device, names sockets, etc) */
+#endif
+
+#ifndef MNT_DETACH
+#define MNT_DETACH	0x00000002	/* Just detach from the tree */
+#endif /* MNT_DETACH */
+
+/* sys/mount.h may be out too old to have these */
+#ifndef MS_REC
+#define MS_REC		16384
+#endif
+
+#ifndef MS_SLAVE
+#define MS_SLAVE	(1 << 19)
+#endif
+
+#ifndef MS_SHARED
Quoted 9 lines

include/utils.h struct iplink_req; · line 188

Quoted 5 lines
include/utils.h +5 -0
+#define MS_SHARED	(1 << 20)
+#endif
+
+extern int netns_switch(char *netns);
+

Maybe it would be cleaner to introduce a new file netns.h with this and the setnetns syscall wrapper

And MS_*/MNT_* defines to new mount.h ?

Thanks,

#3 lore raw
Quoted 35 lines · On Fri, Dec 12, 2014 at 08:04:36AM +0200, [email protected] wrote:
Quoted 25 lines · Stephen Hemminger
Quoted 22 lines · On Fri, 12 Dec 2014 00:32:51 +0200 Vadim Kochan <[email protected]> wrote:
include/utils.h +22 -0
+
+#define NETNS_RUN_DIR "/var/run/netns"
+#define NETNS_ETC_DIR "/etc/netns"
+
+#ifndef CLONE_NEWNET
+#define CLONE_NEWNET 0x40000000	/* New network namespace (lo, device, names sockets, etc) */
+#endif
+
+#ifndef MNT_DETACH
+#define MNT_DETACH	0x00000002	/* Just detach from the tree */
+#endif /* MNT_DETACH */
+
+/* sys/mount.h may be out too old to have these */
+#ifndef MS_REC
+#define MS_REC		16384
+#endif
+
+#ifndef MS_SLAVE
+#define MS_SLAVE	(1 << 19)
+#endif
+
+#ifndef MS_SHARED
Quoted 9 lines

include/utils.h struct iplink_req; · line 188

Quoted 5 lines
include/utils.h +5 -0
+#define MS_SHARED	(1 << 20)
+#endif
+
+extern int netns_switch(char *netns);
+

Maybe it would be cleaner to introduce a new file netns.h with this and the setnetns syscall wrapper

Quoted 4 lines

And MS_*/MNT_* defines to new mount.h ?

Thanks,

I think it can go all together (netns + mount things) to the new include/namespace.h header, what do you think ?

And what about netns_switch func - let it be in utils.c/utils.h or add new lib/namespace.c which will be linked to the libutil ?

Thanks,