ip: IFLA_NEW_NETNSID/IFLA_NEW_IFINDEX support
Subsystems (2)
PATCH 1/1 ip: IFLA_NEW_NETNSID/IFLA_NEW_IFINDEX support
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
--- ip/ipaddress.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
ip/ipaddress.c +11 -0
@@ -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,
Signature · 1 line
2.15.1
Oops, I use an old email for Stephen, sorry.
Regards,
Nicolas
Le 31/05/2018 à 16:28, Nicolas Dichtel a écrit :
Quoted 37 lines
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 defaultlink/ether 66:af:3a:3f:a0:89 brd ff:ff:ff:ff:ff:ff new-nsid 0 new-ifindex 6Signed-off-by Nicolas Dichtel <[email protected]>--- ip/ipaddress.c | 11 +++++++++++ 1 file changed, 11 insertions(+)ip/ipaddress.c +11
-0@@ -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,
Quoted 26 lines · On Thu, 31 May 2018 16:28:48 +0200 Nicolas Dichtel <[email protected]>…
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 defaultlink/ether 66:af:3a:3f:a0:89 brd ff:ff:ff:ff:ff:ff new-nsid 0 new-ifindex 6Signed-off-by Nicolas Dichtel <[email protected]>--- ip/ipaddress.c | 11 +++++++++++ 1 file changed, 11 insertions(+)ip/ipaddress.c +4
-0@@ -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);
ip/ipaddress.c int print_linkinfo(const struct sockaddr_nl *who, · line 980
Quoted 10 lines
ip/ipaddress.c +7
-0+ } +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?
Le 31/05/2018 à 17:46, Stephen Hemminger a écrit :
Quoted 1 line
On Thu, 31 May 2018 16:28:48 +0200
[snip]
Quoted 3 lines
This makes sense. All of linkinfo that is present should be displayed.
Both netns and ifindex are really unsigned values. Use
__u32andprint_uint.
Ok.
Quoted 1 line
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
Le 31/05/2018 à 17:51, Nicolas Dichtel a écrit :
Quoted 7 lines
Le 31/05/2018 à 17:46, Stephen Hemminger a écrit :
Quoted 1 line
On Thu, 31 May 2018 16:28:48 +0200
[snip]
Quoted 3 lines
This makes sense. All of linkinfo that is present should be displayed.
Both netns and ifindex are really unsigned values. Use
__u32andprint_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
Quoted 6 lines · On Fri, 1 Jun 2018 17:02:18 +0200 Nicolas Dichtel <[email protected]>…
Le 31/05/2018 à 17:51, Nicolas Dichtel a écrit :
Quoted 5 lines
Le 31/05/2018 à 17:46, Stephen Hemminger a écrit :
Quoted 1 line
On Thu, 31 May 2018 16:28:48 +0200
[snip]
Quoted 2 lines
This makes sense. All of linkinfo that is present should be displayed.
Quoted 8 lines
Quoted 2 lines
Quoted 1 line
Both netns and ifindex are really unsigned values. Use
__u32andprint_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#n1621Regards,
Nicolas
Ok, Applied orignal patch
cover display netns name instead of nsid
In reply to Stephen Hemminger[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
PATCH 1/2 ip: display netns name instead of nsid
When iproute2 has a name for the nsid, let's display it. It's more user friendly than a number.
--- ip/ip_common.h | 1 + ip/ipaddress.c | 23 ++++++++++++++++++----- ip/ipnetns.c | 10 ++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-)
ip/ip_common.h +1 -0
@@ -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);
ip/ipaddress.c +18 -5
@@ -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 ]);
ip/ipnetns.c +10 -0
@@ -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;
Signature · 1 line
2.15.1
Re: [PATCH iproute2 1/2] ip: display netns name instead of nsid
ip/ipaddress.c int print_linkinfo(const struct sockaddr_nl *who, · line 824
Quoted 11 lines · On Mon, 4 Jun 2018 14:12:52 +0200 Nicolas Dichtel <[email protected]>…
ip/ipaddress.c +3
-0@@ -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?
v2 cover 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.
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
PATCH v2 1/2 ip: display netns name instead of nsid
When iproute2 has a name for the nsid, let's display it. It's more user friendly than a number.
--- ip/ip_common.h | 1 + ip/ipaddress.c | 20 +++++++++++++++----- ip/ipnetns.c | 14 ++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-)
ip/ip_common.h +1 -0
@@ -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);
ip/ipaddress.c +15 -5
@@ -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 ]);
ip/ipnetns.c +14 -0
@@ -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;
Signature · 1 line
2.15.1
Re: [PATCH iproute2 v2 1/2] ip: display netns name instead of nsid
Quoted 5 lines · On Tue, 5 Jun 2018 15:08:30 +0200 Nicolas Dichtel <[email protected]>…
ip/ipnetns.c +4
-0+char *get_name_from_nsid (int nsid) +{ +struct nsid_cache *c; +
ip/ipnetns.c static struct nsid_cache *netns_map_get_by_nsid(int nsid) · line 185
Quoted 10 lines
ip/ipnetns.c +10
-0+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?
Re: [PATCH iproute2 v2 1/2] ip: display netns name instead of nsid
Le 05/06/2018 à 18:52, Stephen Hemminger a écrit :
Quoted 13 lines
Quoted 10 lines · On Tue, 5 Jun 2018 15:08:30 +0200 Nicolas Dichtel <[email protected]>…
ip/ipnetns.c +9
-0+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)
Quoted 9 lines
ip/ipnetns.c static struct nsid_cache *netns_map_get_by_nsid(int nsid) · line 185
Quoted 5 lines
ip/ipnetns.c +5
-0+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;...
PATCH v2 2/2 iplink: enable to specify a name for the link-netns
In reply to Nicolas DichtelThe '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
--- ip/ip_common.h | 2 ++ ip/iplink.c | 18 ++++++++++++++++-- ip/ipnetns.c | 8 ++++++-- 3 files changed, 24 insertions(+), 4 deletions(-)
ip/ip_common.h +2 -0
@@ -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);
ip/iplink.c +16 -2
@@ -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);
ip/ipnetns.c +6 -2
@@ -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;
Signature · 1 line
2.15.1
Stephen Hemminger
AppliedRe: [PATCH iproute2 v2 0/2] display netns name instead of nsid
In reply to Nicolas DichtelQuoted 12 lines · On Tue, 5 Jun 2018 15:08:29 +0200 Nicolas Dichtel <[email protected]>…
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 neededip/ip_common.h | 3 +++ ip/ipaddress.c | 20 +++++++++++++++----- ip/iplink.c | 18 ++++++++++++++++-- ip/ipnetns.c | 22 ++++++++++++++++++++-- 4 files changed, 54 insertions(+), 9 deletions(-)
Quoted 3 lines
Comments are welcomed,
Regards,
Nicolas
Applied
PATCH 2/2 iplink: enable to specify a name for the link-netns
In reply to Nicolas DichtelThe '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
--- ip/ip_common.h | 2 ++ ip/iplink.c | 18 ++++++++++++++++-- ip/ipnetns.c | 8 ++++++-- 3 files changed, 24 insertions(+), 4 deletions(-)
ip/ip_common.h +2 -0
@@ -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);
ip/iplink.c +16 -2
@@ -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);
ip/ipnetns.c +6 -2
@@ -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;
Signature · 1 line
2.15.1