netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH iproute2] ip: IFLA_NEW_NETNSID/IFLA_NEW_IFINDEX support
@ 2018-05-31 14:28 Nicolas Dichtel
  2018-05-31 14:31 ` Nicolas Dichtel
  2018-05-31 15:46 ` Stephen Hemminger
  0 siblings, 2 replies; 16+ messages in thread
From: Nicolas Dichtel @ 2018-05-31 14:28 UTC (permalink / raw)
  To: shemminger; +Cc: netdev, Nicolas Dichtel

Parse and display those attributes.
Example:
ip l a type dummy
ip netns add foo
ip monitor link&
ip l s dummy1 netns foo
Deleted 6: dummy1: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default
    link/ether 66:af:3a:3f:a0:89 brd ff:ff:ff:ff:ff:ff new-nsid 0 new-ifindex 6

Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
---
 ip/ipaddress.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/ip/ipaddress.c b/ip/ipaddress.c
index 00da14c6f97c..c7c7e7df4e81 100644
--- a/ip/ipaddress.c
+++ b/ip/ipaddress.c
@@ -964,6 +964,17 @@ int print_linkinfo(const struct sockaddr_nl *who,
 		}
 	}
 
+	if (tb[IFLA_NEW_NETNSID]) {
+		int id = rta_getattr_u32(tb[IFLA_NEW_NETNSID]);
+
+		print_int(PRINT_FP, NULL, " new-nsid %d", id);
+	}
+	if (tb[IFLA_NEW_IFINDEX]) {
+		int id = rta_getattr_u32(tb[IFLA_NEW_IFINDEX]);
+
+		print_int(PRINT_FP, NULL, " new-ifindex %d", id);
+	}
+
 	if (tb[IFLA_PROTO_DOWN]) {
 		if (rta_getattr_u8(tb[IFLA_PROTO_DOWN]))
 			print_bool(PRINT_ANY,
-- 
2.15.1

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH iproute2] ip: IFLA_NEW_NETNSID/IFLA_NEW_IFINDEX support
  2018-05-31 14:28 [PATCH iproute2] ip: IFLA_NEW_NETNSID/IFLA_NEW_IFINDEX support Nicolas Dichtel
@ 2018-05-31 14:31 ` Nicolas Dichtel
  2018-05-31 15:46 ` Stephen Hemminger
  1 sibling, 0 replies; 16+ messages in thread
From: Nicolas Dichtel @ 2018-05-31 14:31 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev

Oops, I use an old email for Stephen, sorry.


Regards,
Nicolas

Le 31/05/2018 à 16:28, Nicolas Dichtel a écrit :
> Parse and display those attributes.
> Example:
> ip l a type dummy
> ip netns add foo
> ip monitor link&
> ip l s dummy1 netns foo
> Deleted 6: dummy1: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default
>     link/ether 66:af:3a:3f:a0:89 brd ff:ff:ff:ff:ff:ff new-nsid 0 new-ifindex 6
> 
> Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
> ---
>  ip/ipaddress.c | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/ip/ipaddress.c b/ip/ipaddress.c
> index 00da14c6f97c..c7c7e7df4e81 100644
> --- a/ip/ipaddress.c
> +++ b/ip/ipaddress.c
> @@ -964,6 +964,17 @@ int print_linkinfo(const struct sockaddr_nl *who,
>  		}
>  	}
>  
> +	if (tb[IFLA_NEW_NETNSID]) {
> +		int id = rta_getattr_u32(tb[IFLA_NEW_NETNSID]);
> +
> +		print_int(PRINT_FP, NULL, " new-nsid %d", id);
> +	}
> +	if (tb[IFLA_NEW_IFINDEX]) {
> +		int id = rta_getattr_u32(tb[IFLA_NEW_IFINDEX]);
> +
> +		print_int(PRINT_FP, NULL, " new-ifindex %d", id);
> +	}
> +
>  	if (tb[IFLA_PROTO_DOWN]) {
>  		if (rta_getattr_u8(tb[IFLA_PROTO_DOWN]))
>  			print_bool(PRINT_ANY,
> 

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH iproute2] ip: IFLA_NEW_NETNSID/IFLA_NEW_IFINDEX support
  2018-05-31 14:28 [PATCH iproute2] ip: IFLA_NEW_NETNSID/IFLA_NEW_IFINDEX support Nicolas Dichtel
  2018-05-31 14:31 ` Nicolas Dichtel
@ 2018-05-31 15:46 ` Stephen Hemminger
  2018-05-31 15:51   ` Nicolas Dichtel
  2018-06-04 12:12   ` [PATCH iproute2 0/2] display netns name instead of nsid Nicolas Dichtel
  1 sibling, 2 replies; 16+ messages in thread
From: Stephen Hemminger @ 2018-05-31 15:46 UTC (permalink / raw)
  To: Nicolas Dichtel; +Cc: shemminger, netdev

On Thu, 31 May 2018 16:28:48 +0200
Nicolas Dichtel <nicolas.dichtel@6wind.com> wrote:

> Parse and display those attributes.
> Example:
> ip l a type dummy
> ip netns add foo
> ip monitor link&
> ip l s dummy1 netns foo
> Deleted 6: dummy1: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default
>     link/ether 66:af:3a:3f:a0:89 brd ff:ff:ff:ff:ff:ff new-nsid 0 new-ifindex 6
> 
> Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
> ---
>  ip/ipaddress.c | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/ip/ipaddress.c b/ip/ipaddress.c
> index 00da14c6f97c..c7c7e7df4e81 100644
> --- a/ip/ipaddress.c
> +++ b/ip/ipaddress.c
> @@ -964,6 +964,17 @@ int print_linkinfo(const struct sockaddr_nl *who,
>  		}
>  	}
>  
> +	if (tb[IFLA_NEW_NETNSID]) {
> +		int id = rta_getattr_u32(tb[IFLA_NEW_NETNSID]);
> +
> +		print_int(PRINT_FP, NULL, " new-nsid %d", id);
> +	}
> +	if (tb[IFLA_NEW_IFINDEX]) {
> +		int id = rta_getattr_u32(tb[IFLA_NEW_IFINDEX]);
> +
> +		print_int(PRINT_FP, NULL, " new-ifindex %d", id);
> +	}
> +
>  	if (tb[IFLA_PROTO_DOWN]) {
>  		if (rta_getattr_u8(tb[IFLA_PROTO_DOWN]))
>  			print_bool(PRINT_ANY,

This makes sense. All of linkinfo that is present should be displayed.

Both netns and ifindex are really unsigned values. Use __u32 and print_uint.
Also why not convert numeric values to names?

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH iproute2] ip: IFLA_NEW_NETNSID/IFLA_NEW_IFINDEX support
  2018-05-31 15:46 ` Stephen Hemminger
@ 2018-05-31 15:51   ` Nicolas Dichtel
  2018-06-01 15:02     ` Nicolas Dichtel
  2018-06-04 12:12   ` [PATCH iproute2 0/2] display netns name instead of nsid Nicolas Dichtel
  1 sibling, 1 reply; 16+ messages in thread
From: Nicolas Dichtel @ 2018-05-31 15:51 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev

Le 31/05/2018 à 17:46, Stephen Hemminger a écrit :
> On Thu, 31 May 2018 16:28:48 +0200
[snip]
> This makes sense. All of linkinfo that is present should be displayed.
> 
> Both netns and ifindex are really unsigned values. Use __u32 and print_uint.
Ok.

> Also why not convert numeric values to names?
The only case where the ifname can change is when a netns is deleted and the
interface is put back in init_net. But at this stage, we don't know the new name.

For the nsid, you're right, it will be better to display the netns name. If you
agree, I will do this in a following patch, thus all places using nsid can be
converted at the same time.


Regards,
Nicolas

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH iproute2] ip: IFLA_NEW_NETNSID/IFLA_NEW_IFINDEX support
  2018-05-31 15:51   ` Nicolas Dichtel
@ 2018-06-01 15:02     ` Nicolas Dichtel
  2018-06-01 20:02       ` Stephen Hemminger
  0 siblings, 1 reply; 16+ messages in thread
From: Nicolas Dichtel @ 2018-06-01 15:02 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev

Le 31/05/2018 à 17:51, Nicolas Dichtel a écrit :
> Le 31/05/2018 à 17:46, Stephen Hemminger a écrit :
>> On Thu, 31 May 2018 16:28:48 +0200
> [snip]
>> This makes sense. All of linkinfo that is present should be displayed.
>>
>> Both netns and ifindex are really unsigned values. Use __u32 and print_uint.
> Ok.
I replied a bit quickly, both are signed values:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/rtnetlink.c#n1621


Regards,
Nicolas

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH iproute2] ip: IFLA_NEW_NETNSID/IFLA_NEW_IFINDEX support
  2018-06-01 15:02     ` Nicolas Dichtel
@ 2018-06-01 20:02       ` Stephen Hemminger
  0 siblings, 0 replies; 16+ messages in thread
From: Stephen Hemminger @ 2018-06-01 20:02 UTC (permalink / raw)
  To: Nicolas Dichtel; +Cc: netdev

On Fri, 1 Jun 2018 17:02:18 +0200
Nicolas Dichtel <nicolas.dichtel@6wind.com> wrote:

> Le 31/05/2018 à 17:51, Nicolas Dichtel a écrit :
> > Le 31/05/2018 à 17:46, Stephen Hemminger a écrit :  
> >> On Thu, 31 May 2018 16:28:48 +0200  
> > [snip]  
> >> This makes sense. All of linkinfo that is present should be displayed.
> >>
> >> Both netns and ifindex are really unsigned values. Use __u32 and print_uint.  
> > Ok.  
> I replied a bit quickly, both are signed values:
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/rtnetlink.c#n1621
> 
> 
> Regards,
> Nicolas

Ok, Applied orignal patch

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH iproute2 0/2] display netns name instead of nsid
  2018-05-31 15:46 ` Stephen Hemminger
  2018-05-31 15:51   ` Nicolas Dichtel
@ 2018-06-04 12:12   ` Nicolas Dichtel
  2018-06-04 12:12     ` [PATCH iproute2 1/2] ip: " Nicolas Dichtel
  2018-06-04 12:12     ` [PATCH iproute2 2/2] iplink: enable to specify a name for the link-netns Nicolas Dichtel
  1 sibling, 2 replies; 16+ messages in thread
From: Nicolas Dichtel @ 2018-06-04 12:12 UTC (permalink / raw)
  To: stephen; +Cc: netdev


[PATCH iproute2 0/2] display netns name instead of nsid
 
After these patches, the iproute2 name of netns is displayed instead of
the nsid. It's easier to read/understand.

 ip/ip_common.h |  3 +++
 ip/ipaddress.c | 23 ++++++++++++++++++-----
 ip/iplink.c    | 18 ++++++++++++++++--
 ip/ipnetns.c   | 18 ++++++++++++++++--
 4 files changed, 53 insertions(+), 9 deletions(-)

Comments are welcomed,
Regards,
Nicolas

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH iproute2 1/2] ip: display netns name instead of nsid
  2018-06-04 12:12   ` [PATCH iproute2 0/2] display netns name instead of nsid Nicolas Dichtel
@ 2018-06-04 12:12     ` Nicolas Dichtel
  2018-06-04 21:12       ` Stephen Hemminger
  2018-06-04 12:12     ` [PATCH iproute2 2/2] iplink: enable to specify a name for the link-netns Nicolas Dichtel
  1 sibling, 1 reply; 16+ messages in thread
From: Nicolas Dichtel @ 2018-06-04 12:12 UTC (permalink / raw)
  To: stephen; +Cc: netdev, Nicolas Dichtel

When iproute2 has a name for the nsid, let's display it. It's more
user friendly than a number.

Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
---
 ip/ip_common.h |  1 +
 ip/ipaddress.c | 23 ++++++++++++++++++-----
 ip/ipnetns.c   | 10 ++++++++++
 3 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/ip/ip_common.h b/ip/ip_common.h
index 49eb7d7bed40..794478c546cd 100644
--- a/ip/ip_common.h
+++ b/ip/ip_common.h
@@ -60,6 +60,7 @@ void netns_map_init(void);
 void netns_nsid_socket_init(void);
 int print_nsid(const struct sockaddr_nl *who,
 	       struct nlmsghdr *n, void *arg);
+char *get_name_from_nsid(int nsid);
 int do_ipaddr(int argc, char **argv);
 int do_ipaddrlabel(int argc, char **argv);
 int do_iproute(int argc, char **argv);
diff --git a/ip/ipaddress.c b/ip/ipaddress.c
index c7c7e7df4e81..aee09c7ff6df 100644
--- a/ip/ipaddress.c
+++ b/ip/ipaddress.c
@@ -819,6 +819,9 @@ int print_linkinfo(const struct sockaddr_nl *who,
 	unsigned int m_flag = 0;
 	SPRINT_BUF(b1);
 
+	netns_nsid_socket_init();
+	netns_map_init();
+
 	if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
 		return 0;
 
@@ -955,10 +958,16 @@ int print_linkinfo(const struct sockaddr_nl *who,
 		if (is_json_context()) {
 			print_int(PRINT_JSON, "link_netnsid", NULL, id);
 		} else {
-			if (id >= 0)
-				print_int(PRINT_FP, NULL,
-					  " link-netnsid %d", id);
-			else
+			if (id >= 0) {
+				char *name = get_name_from_nsid(id);
+
+				if (name)
+					print_string(PRINT_FP, NULL,
+						     " link-netns %s", name);
+				else
+					print_int(PRINT_FP, NULL,
+						  " link-netnsid %d", id);
+			} else
 				print_string(PRINT_FP, NULL,
 					     " link-netnsid %s", "unknown");
 		}
@@ -966,8 +975,12 @@ int print_linkinfo(const struct sockaddr_nl *who,
 
 	if (tb[IFLA_NEW_NETNSID]) {
 		int id = rta_getattr_u32(tb[IFLA_NEW_NETNSID]);
+		char *name = get_name_from_nsid(id);
 
-		print_int(PRINT_FP, NULL, " new-nsid %d", id);
+		if (name)
+			print_string(PRINT_FP, NULL, " new-netns %s", name);
+		else
+			print_int(PRINT_FP, NULL, " new-netnsid %d", id);
 	}
 	if (tb[IFLA_NEW_IFINDEX]) {
 		int id = rta_getattr_u32(tb[IFLA_NEW_IFINDEX]);
diff --git a/ip/ipnetns.c b/ip/ipnetns.c
index e06100f4ad2d..a4f5b02427e7 100644
--- a/ip/ipnetns.c
+++ b/ip/ipnetns.c
@@ -169,6 +169,16 @@ static struct nsid_cache *netns_map_get_by_nsid(int nsid)
 	return NULL;
 }
 
+char *get_name_from_nsid(int nsid)
+{
+	struct nsid_cache *c = netns_map_get_by_nsid(nsid);
+
+	if (c)
+		return c->name;
+
+	return NULL;
+}
+
 static int netns_map_add(int nsid, const char *name)
 {
 	struct nsid_cache *c;
-- 
2.15.1

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH iproute2 2/2] iplink: enable to specify a name for the link-netns
  2018-06-04 12:12   ` [PATCH iproute2 0/2] display netns name instead of nsid Nicolas Dichtel
  2018-06-04 12:12     ` [PATCH iproute2 1/2] ip: " Nicolas Dichtel
@ 2018-06-04 12:12     ` Nicolas Dichtel
  1 sibling, 0 replies; 16+ messages in thread
From: Nicolas Dichtel @ 2018-06-04 12:12 UTC (permalink / raw)
  To: stephen; +Cc: netdev, Nicolas Dichtel

The 'link-netnsid' argument needs a number. Add 'link-netns' when the user
wants to use the iproute2 netns name instead of the nsid.

Example:
ip link add ipip1 link-netns foo type ipip remote 10.16.0.121 local 10.16.0.249

Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
---
 ip/ip_common.h |  2 ++
 ip/iplink.c    | 18 ++++++++++++++++--
 ip/ipnetns.c   |  8 ++++++--
 3 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/ip/ip_common.h b/ip/ip_common.h
index 794478c546cd..4d3227cbc389 100644
--- a/ip/ip_common.h
+++ b/ip/ip_common.h
@@ -61,6 +61,8 @@ void netns_nsid_socket_init(void);
 int print_nsid(const struct sockaddr_nl *who,
 	       struct nlmsghdr *n, void *arg);
 char *get_name_from_nsid(int nsid);
+int get_netnsid_from_name(const char *name);
+int set_netnsid_from_name(const char *name, int nsid);
 int do_ipaddr(int argc, char **argv);
 int do_ipaddrlabel(int argc, char **argv);
 int do_iproute(int argc, char **argv);
diff --git a/ip/iplink.c b/ip/iplink.c
index 9ff5f692a1d4..e4d4da96aedb 100644
--- a/ip/iplink.c
+++ b/ip/iplink.c
@@ -85,7 +85,7 @@ void iplink_usage(void)
 		"	                  [ broadcast LLADDR ]\n"
 		"	                  [ mtu MTU ]\n"
 		"	                  [ netns { PID | NAME } ]\n"
-		"	                  [ link-netnsid ID ]\n"
+		"	                  [ link-netns NAME | link-netnsid ID ]\n"
 		"			  [ alias NAME ]\n"
 		"	                  [ vf NUM [ mac LLADDR ]\n"
 		"				   [ vlan VLANID [ qos VLAN-QOS ] [ proto VLAN-PROTO ] ]\n"
@@ -865,10 +865,24 @@ int iplink_parse(int argc, char **argv, struct iplink_req *req, char **type)
 				 IFLA_INET6_ADDR_GEN_MODE, mode);
 			addattr_nest_end(&req->n, afs6);
 			addattr_nest_end(&req->n, afs);
+		} else if (matches(*argv, "link-netns") == 0) {
+			NEXT_ARG();
+			if (link_netnsid != -1)
+				duparg("link-netns/link-netnsid", *argv);
+			link_netnsid = get_netnsid_from_name(*argv);
+			/* No nsid? Try to assign one. */
+			if (link_netnsid < 0)
+				set_netnsid_from_name(*argv, -1);
+			link_netnsid = get_netnsid_from_name(*argv);
+			if (link_netnsid < 0)
+				invarg("Invalid \"link-netns\" value\n",
+				       *argv);
+			addattr32(&req->n, sizeof(*req), IFLA_LINK_NETNSID,
+				  link_netnsid);
 		} else if (matches(*argv, "link-netnsid") == 0) {
 			NEXT_ARG();
 			if (link_netnsid != -1)
-				duparg("link-netnsid", *argv);
+				duparg("link-netns/link-netnsid", *argv);
 			if (get_integer(&link_netnsid, *argv, 0))
 				invarg("Invalid \"link-netnsid\" value\n",
 				       *argv);
diff --git a/ip/ipnetns.c b/ip/ipnetns.c
index a4f5b02427e7..fb1daade366b 100644
--- a/ip/ipnetns.c
+++ b/ip/ipnetns.c
@@ -91,7 +91,7 @@ static int ipnetns_have_nsid(void)
 	return have_rtnl_getnsid;
 }
 
-static int get_netnsid_from_name(const char *name)
+int get_netnsid_from_name(const char *name)
 {
 	struct {
 		struct nlmsghdr n;
@@ -108,6 +108,8 @@ static int get_netnsid_from_name(const char *name)
 	struct rtgenmsg *rthdr;
 	int len, fd;
 
+	netns_nsid_socket_init();
+
 	fd = netns_get_fd(name);
 	if (fd < 0)
 		return fd;
@@ -701,7 +703,7 @@ out_delete:
 	return -1;
 }
 
-static int set_netnsid_from_name(const char *name, int nsid)
+int set_netnsid_from_name(const char *name, int nsid)
 {
 	struct {
 		struct nlmsghdr n;
@@ -715,6 +717,8 @@ static int set_netnsid_from_name(const char *name, int nsid)
 	};
 	int fd, err = 0;
 
+	netns_nsid_socket_init();
+
 	fd = netns_get_fd(name);
 	if (fd < 0)
 		return fd;
-- 
2.15.1

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH iproute2 1/2] ip: display netns name instead of nsid
  2018-06-04 12:12     ` [PATCH iproute2 1/2] ip: " Nicolas Dichtel
@ 2018-06-04 21:12       ` Stephen Hemminger
  2018-06-05 13:08         ` [PATCH iproute2 v2 0/2] " Nicolas Dichtel
  0 siblings, 1 reply; 16+ messages in thread
From: Stephen Hemminger @ 2018-06-04 21:12 UTC (permalink / raw)
  To: Nicolas Dichtel; +Cc: netdev

On Mon,  4 Jun 2018 14:12:52 +0200
Nicolas Dichtel <nicolas.dichtel@6wind.com> wrote:

> diff --git a/ip/ipaddress.c b/ip/ipaddress.c
> index c7c7e7df4e81..aee09c7ff6df 100644
> --- a/ip/ipaddress.c
> +++ b/ip/ipaddress.c
> @@ -819,6 +819,9 @@ int print_linkinfo(const struct sockaddr_nl *who,
>  	unsigned int m_flag = 0;
>  	SPRINT_BUF(b1);
>  
> +	netns_nsid_socket_init();
> +	netns_map_init();
> +

The idea of printing network namespace is good but I am concerned that
setting up yet another netlink socket and scanning the netns directory on
each ip link command will impact some users.

Can this setup be deferred until the first net ns lookup happens?

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH iproute2 v2 0/2] display netns name instead of nsid
  2018-06-04 21:12       ` Stephen Hemminger
@ 2018-06-05 13:08         ` Nicolas Dichtel
  2018-06-05 13:08           ` [PATCH iproute2 v2 1/2] ip: " Nicolas Dichtel
                             ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Nicolas Dichtel @ 2018-06-05 13:08 UTC (permalink / raw)
  To: stephen; +Cc: netdev


After these patches, the iproute2 name of netns is displayed instead of
the nsid. It's easier to read/understand.

v1 -> v2:
 - open netns socket and init netns map only when needed

 ip/ip_common.h |  3 +++
 ip/ipaddress.c | 20 +++++++++++++++-----
 ip/iplink.c    | 18 ++++++++++++++++--
 ip/ipnetns.c   | 22 ++++++++++++++++++++--
 4 files changed, 54 insertions(+), 9 deletions(-)
 
Comments are welcomed, 
Regards, 
Nicolas

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH iproute2 v2 1/2] ip: display netns name instead of nsid
  2018-06-05 13:08         ` [PATCH iproute2 v2 0/2] " Nicolas Dichtel
@ 2018-06-05 13:08           ` Nicolas Dichtel
  2018-06-05 16:52             ` Stephen Hemminger
  2018-06-05 13:08           ` [PATCH iproute2 v2 2/2] iplink: enable to specify a name for the link-netns Nicolas Dichtel
  2018-06-08 17:08           ` [PATCH iproute2 v2 0/2] display netns name instead of nsid Stephen Hemminger
  2 siblings, 1 reply; 16+ messages in thread
From: Nicolas Dichtel @ 2018-06-05 13:08 UTC (permalink / raw)
  To: stephen; +Cc: netdev, Nicolas Dichtel

When iproute2 has a name for the nsid, let's display it. It's more
user friendly than a number.

Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
---
 ip/ip_common.h |  1 +
 ip/ipaddress.c | 20 +++++++++++++++-----
 ip/ipnetns.c   | 14 ++++++++++++++
 3 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/ip/ip_common.h b/ip/ip_common.h
index 49eb7d7bed40..794478c546cd 100644
--- a/ip/ip_common.h
+++ b/ip/ip_common.h
@@ -60,6 +60,7 @@ void netns_map_init(void);
 void netns_nsid_socket_init(void);
 int print_nsid(const struct sockaddr_nl *who,
 	       struct nlmsghdr *n, void *arg);
+char *get_name_from_nsid(int nsid);
 int do_ipaddr(int argc, char **argv);
 int do_ipaddrlabel(int argc, char **argv);
 int do_iproute(int argc, char **argv);
diff --git a/ip/ipaddress.c b/ip/ipaddress.c
index c7c7e7df4e81..e4a1b985e4e9 100644
--- a/ip/ipaddress.c
+++ b/ip/ipaddress.c
@@ -955,10 +955,16 @@ int print_linkinfo(const struct sockaddr_nl *who,
 		if (is_json_context()) {
 			print_int(PRINT_JSON, "link_netnsid", NULL, id);
 		} else {
-			if (id >= 0)
-				print_int(PRINT_FP, NULL,
-					  " link-netnsid %d", id);
-			else
+			if (id >= 0) {
+				char *name = get_name_from_nsid(id);
+
+				if (name)
+					print_string(PRINT_FP, NULL,
+						     " link-netns %s", name);
+				else
+					print_int(PRINT_FP, NULL,
+						  " link-netnsid %d", id);
+			} else
 				print_string(PRINT_FP, NULL,
 					     " link-netnsid %s", "unknown");
 		}
@@ -966,8 +972,12 @@ int print_linkinfo(const struct sockaddr_nl *who,
 
 	if (tb[IFLA_NEW_NETNSID]) {
 		int id = rta_getattr_u32(tb[IFLA_NEW_NETNSID]);
+		char *name = get_name_from_nsid(id);
 
-		print_int(PRINT_FP, NULL, " new-nsid %d", id);
+		if (name)
+			print_string(PRINT_FP, NULL, " new-netns %s", name);
+		else
+			print_int(PRINT_FP, NULL, " new-netnsid %d", id);
 	}
 	if (tb[IFLA_NEW_IFINDEX]) {
 		int id = rta_getattr_u32(tb[IFLA_NEW_IFINDEX]);
diff --git a/ip/ipnetns.c b/ip/ipnetns.c
index e06100f4ad2d..30af9319f39e 100644
--- a/ip/ipnetns.c
+++ b/ip/ipnetns.c
@@ -169,6 +169,20 @@ static struct nsid_cache *netns_map_get_by_nsid(int nsid)
 	return NULL;
 }
 
+char *get_name_from_nsid(int nsid)
+{
+	struct nsid_cache *c;
+
+	netns_nsid_socket_init();
+	netns_map_init();
+
+	c = netns_map_get_by_nsid(nsid);
+	if (c)
+		return c->name;
+
+	return NULL;
+}
+
 static int netns_map_add(int nsid, const char *name)
 {
 	struct nsid_cache *c;
-- 
2.15.1

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH iproute2 v2 2/2] iplink: enable to specify a name for the link-netns
  2018-06-05 13:08         ` [PATCH iproute2 v2 0/2] " Nicolas Dichtel
  2018-06-05 13:08           ` [PATCH iproute2 v2 1/2] ip: " Nicolas Dichtel
@ 2018-06-05 13:08           ` Nicolas Dichtel
  2018-06-08 17:08           ` [PATCH iproute2 v2 0/2] display netns name instead of nsid Stephen Hemminger
  2 siblings, 0 replies; 16+ messages in thread
From: Nicolas Dichtel @ 2018-06-05 13:08 UTC (permalink / raw)
  To: stephen; +Cc: netdev, Nicolas Dichtel

The 'link-netnsid' argument needs a number. Add 'link-netns' when the user
wants to use the iproute2 netns name instead of the nsid.

Example:
ip link add ipip1 link-netns foo type ipip remote 10.16.0.121 local 10.16.0.249

Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
---
 ip/ip_common.h |  2 ++
 ip/iplink.c    | 18 ++++++++++++++++--
 ip/ipnetns.c   |  8 ++++++--
 3 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/ip/ip_common.h b/ip/ip_common.h
index 794478c546cd..4d3227cbc389 100644
--- a/ip/ip_common.h
+++ b/ip/ip_common.h
@@ -61,6 +61,8 @@ void netns_nsid_socket_init(void);
 int print_nsid(const struct sockaddr_nl *who,
 	       struct nlmsghdr *n, void *arg);
 char *get_name_from_nsid(int nsid);
+int get_netnsid_from_name(const char *name);
+int set_netnsid_from_name(const char *name, int nsid);
 int do_ipaddr(int argc, char **argv);
 int do_ipaddrlabel(int argc, char **argv);
 int do_iproute(int argc, char **argv);
diff --git a/ip/iplink.c b/ip/iplink.c
index 9ff5f692a1d4..e4d4da96aedb 100644
--- a/ip/iplink.c
+++ b/ip/iplink.c
@@ -85,7 +85,7 @@ void iplink_usage(void)
 		"	                  [ broadcast LLADDR ]\n"
 		"	                  [ mtu MTU ]\n"
 		"	                  [ netns { PID | NAME } ]\n"
-		"	                  [ link-netnsid ID ]\n"
+		"	                  [ link-netns NAME | link-netnsid ID ]\n"
 		"			  [ alias NAME ]\n"
 		"	                  [ vf NUM [ mac LLADDR ]\n"
 		"				   [ vlan VLANID [ qos VLAN-QOS ] [ proto VLAN-PROTO ] ]\n"
@@ -865,10 +865,24 @@ int iplink_parse(int argc, char **argv, struct iplink_req *req, char **type)
 				 IFLA_INET6_ADDR_GEN_MODE, mode);
 			addattr_nest_end(&req->n, afs6);
 			addattr_nest_end(&req->n, afs);
+		} else if (matches(*argv, "link-netns") == 0) {
+			NEXT_ARG();
+			if (link_netnsid != -1)
+				duparg("link-netns/link-netnsid", *argv);
+			link_netnsid = get_netnsid_from_name(*argv);
+			/* No nsid? Try to assign one. */
+			if (link_netnsid < 0)
+				set_netnsid_from_name(*argv, -1);
+			link_netnsid = get_netnsid_from_name(*argv);
+			if (link_netnsid < 0)
+				invarg("Invalid \"link-netns\" value\n",
+				       *argv);
+			addattr32(&req->n, sizeof(*req), IFLA_LINK_NETNSID,
+				  link_netnsid);
 		} else if (matches(*argv, "link-netnsid") == 0) {
 			NEXT_ARG();
 			if (link_netnsid != -1)
-				duparg("link-netnsid", *argv);
+				duparg("link-netns/link-netnsid", *argv);
 			if (get_integer(&link_netnsid, *argv, 0))
 				invarg("Invalid \"link-netnsid\" value\n",
 				       *argv);
diff --git a/ip/ipnetns.c b/ip/ipnetns.c
index 30af9319f39e..368be0cbc0a4 100644
--- a/ip/ipnetns.c
+++ b/ip/ipnetns.c
@@ -91,7 +91,7 @@ static int ipnetns_have_nsid(void)
 	return have_rtnl_getnsid;
 }
 
-static int get_netnsid_from_name(const char *name)
+int get_netnsid_from_name(const char *name)
 {
 	struct {
 		struct nlmsghdr n;
@@ -108,6 +108,8 @@ static int get_netnsid_from_name(const char *name)
 	struct rtgenmsg *rthdr;
 	int len, fd;
 
+	netns_nsid_socket_init();
+
 	fd = netns_get_fd(name);
 	if (fd < 0)
 		return fd;
@@ -705,7 +707,7 @@ out_delete:
 	return -1;
 }
 
-static int set_netnsid_from_name(const char *name, int nsid)
+int set_netnsid_from_name(const char *name, int nsid)
 {
 	struct {
 		struct nlmsghdr n;
@@ -719,6 +721,8 @@ static int set_netnsid_from_name(const char *name, int nsid)
 	};
 	int fd, err = 0;
 
+	netns_nsid_socket_init();
+
 	fd = netns_get_fd(name);
 	if (fd < 0)
 		return fd;
-- 
2.15.1

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH iproute2 v2 1/2] ip: display netns name instead of nsid
  2018-06-05 13:08           ` [PATCH iproute2 v2 1/2] ip: " Nicolas Dichtel
@ 2018-06-05 16:52             ` Stephen Hemminger
  2018-06-06  7:11               ` Nicolas Dichtel
  0 siblings, 1 reply; 16+ messages in thread
From: Stephen Hemminger @ 2018-06-05 16:52 UTC (permalink / raw)
  To: Nicolas Dichtel; +Cc: netdev

On Tue,  5 Jun 2018 15:08:30 +0200
Nicolas Dichtel <nicolas.dichtel@6wind.com> wrote:

>  
> +char *get_name_from_nsid(int nsid)
> +{
> +	struct nsid_cache *c;
> +
> +	netns_nsid_socket_init();
> +	netns_map_init();
> +
> +	c = netns_map_get_by_nsid(nsid);
> +	if (c)
> +		return c->name;
> +
> +	return NULL;
> +}
> +

This is better, but now there is a different problem.
When doing multiple interfaces, won't the initialization code be called twice?

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH iproute2 v2 1/2] ip: display netns name instead of nsid
  2018-06-05 16:52             ` Stephen Hemminger
@ 2018-06-06  7:11               ` Nicolas Dichtel
  0 siblings, 0 replies; 16+ messages in thread
From: Nicolas Dichtel @ 2018-06-06  7:11 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev

Le 05/06/2018 à 18:52, Stephen Hemminger a écrit :
> On Tue,  5 Jun 2018 15:08:30 +0200
> Nicolas Dichtel <nicolas.dichtel@6wind.com> wrote:
> 
>>  
>> +char *get_name_from_nsid(int nsid)
>> +{
>> +	struct nsid_cache *c;
>> +
>> +	netns_nsid_socket_init();
>> +	netns_map_init();
>> +
>> +	c = netns_map_get_by_nsid(nsid);
>> +	if (c)
>> +		return c->name;
>> +
>> +	return NULL;
>> +}
>> +
> 
> This is better, but now there is a different problem.
> When doing multiple interfaces, won't the initialization code be called twice?
> 
No, the init function is propected:

void netns_nsid_socket_init(void)
{
        if (rtnsh.fd > -1 || !ipnetns_have_nsid())
                return;
...

void netns_map_init(void)
{
...
        if (initialized || !ipnetns_have_nsid())
                return;
...

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH iproute2 v2 0/2] display netns name instead of nsid
  2018-06-05 13:08         ` [PATCH iproute2 v2 0/2] " Nicolas Dichtel
  2018-06-05 13:08           ` [PATCH iproute2 v2 1/2] ip: " Nicolas Dichtel
  2018-06-05 13:08           ` [PATCH iproute2 v2 2/2] iplink: enable to specify a name for the link-netns Nicolas Dichtel
@ 2018-06-08 17:08           ` Stephen Hemminger
  2 siblings, 0 replies; 16+ messages in thread
From: Stephen Hemminger @ 2018-06-08 17:08 UTC (permalink / raw)
  To: Nicolas Dichtel; +Cc: netdev

On Tue,  5 Jun 2018 15:08:29 +0200
Nicolas Dichtel <nicolas.dichtel@6wind.com> wrote:

> After these patches, the iproute2 name of netns is displayed instead of
> the nsid. It's easier to read/understand.
> 
> v1 -> v2:
>  - open netns socket and init netns map only when needed
> 
>  ip/ip_common.h |  3 +++
>  ip/ipaddress.c | 20 +++++++++++++++-----
>  ip/iplink.c    | 18 ++++++++++++++++--
>  ip/ipnetns.c   | 22 ++++++++++++++++++++--
>  4 files changed, 54 insertions(+), 9 deletions(-)
>  
> Comments are welcomed, 
> Regards, 
> Nicolas

Applied

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2018-06-08 17:08 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-31 14:28 [PATCH iproute2] ip: IFLA_NEW_NETNSID/IFLA_NEW_IFINDEX support Nicolas Dichtel
2018-05-31 14:31 ` Nicolas Dichtel
2018-05-31 15:46 ` Stephen Hemminger
2018-05-31 15:51   ` Nicolas Dichtel
2018-06-01 15:02     ` Nicolas Dichtel
2018-06-01 20:02       ` Stephen Hemminger
2018-06-04 12:12   ` [PATCH iproute2 0/2] display netns name instead of nsid Nicolas Dichtel
2018-06-04 12:12     ` [PATCH iproute2 1/2] ip: " Nicolas Dichtel
2018-06-04 21:12       ` Stephen Hemminger
2018-06-05 13:08         ` [PATCH iproute2 v2 0/2] " Nicolas Dichtel
2018-06-05 13:08           ` [PATCH iproute2 v2 1/2] ip: " Nicolas Dichtel
2018-06-05 16:52             ` Stephen Hemminger
2018-06-06  7:11               ` Nicolas Dichtel
2018-06-05 13:08           ` [PATCH iproute2 v2 2/2] iplink: enable to specify a name for the link-netns Nicolas Dichtel
2018-06-08 17:08           ` [PATCH iproute2 v2 0/2] display netns name instead of nsid Stephen Hemminger
2018-06-04 12:12     ` [PATCH iproute2 2/2] iplink: enable to specify a name for the link-netns Nicolas Dichtel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).