From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicolas Dichtel Subject: [PATCH iproute2 v2 1/2] ip: display netns name instead of nsid Date: Tue, 5 Jun 2018 15:08:30 +0200 Message-ID: <20180605130831.8175-2-nicolas.dichtel@6wind.com> References: <20180604141215.0669f05f@xeon-e3> <20180605130831.8175-1-nicolas.dichtel@6wind.com> Cc: netdev@vger.kernel.org, Nicolas Dichtel To: stephen@networkplumber.org Return-path: Received: from host.76.145.23.62.rev.coltfrance.com ([62.23.145.76]:51607 "EHLO proxy.6wind.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751598AbeFENIv (ORCPT ); Tue, 5 Jun 2018 09:08:51 -0400 In-Reply-To: <20180605130831.8175-1-nicolas.dichtel@6wind.com> Sender: netdev-owner@vger.kernel.org List-ID: 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 --- 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