Netdev Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH iproute2-next v2] ipneigh: add support to print brief output of neigh cache in tabular format
@ 2021-07-27 16:46 Gokul Sivakumar
  2021-08-02 16:16 ` David Ahern
  0 siblings, 1 reply; 2+ messages in thread
From: Gokul Sivakumar @ 2021-07-27 16:46 UTC (permalink / raw)
  To: netdev, David Ahern; +Cc: Gokul Sivakumar

Make use of the already available brief flag and print the basic details of
the IPv4 or IPv6 neighbour cache in a tabular format for better readability
when the brief output is expected.

$ ip -br neigh
172.16.12.100                           bridge0          b0:fc:36:2f:07:43
172.16.12.174                           bridge0          8c:16:45:2f:bc:1c
172.16.12.250                           bridge0          04:d9:f5:c1:0c:74
fe80::267b:9f70:745e:d54d               bridge0          b0:fc:36:2f:07:43
fd16:a115:6a62:0:8744:efa1:9933:2c4c    bridge0          8c:16:45:2f:bc:1c
fe80::6d9:f5ff:fec1:c74                 bridge0          04:d9:f5:c1:0c:74

And add "ip neigh show" to the list of ip sub commands mentioned in the man
page that support the brief output in tabular format.

Signed-off-by: Gokul Sivakumar <gokulkumar792@gmail.com>
---

Notes:
    Changes in v2:
    - Reordered the columns in the brief output to be consistent with the order of the
      fields in the non-brief output.
    - changed the format specifier width of dst field from "%-40s" to "%-39s " to be
      consistent with the way width of dev field is specified.

 ip/ipneigh.c  | 50 +++++++++++++++++++++++++++++++++++++++++++++++++-
 man/man8/ip.8 |  2 +-
 2 files changed, 50 insertions(+), 2 deletions(-)

diff --git a/ip/ipneigh.c b/ip/ipneigh.c
index 2d6b7f58..95bde520 100644
--- a/ip/ipneigh.c
+++ b/ip/ipneigh.c
@@ -251,6 +251,51 @@ static void print_neigh_state(unsigned int nud)
 	close_json_array(PRINT_JSON, NULL);
 }
 
+static int print_neigh_brief(FILE *fp, struct ndmsg *r, struct rtattr *tb[])
+{
+	if (tb[NDA_DST]) {
+		const char *dst;
+		int family = r->ndm_family;
+
+		if (family == AF_BRIDGE) {
+			if (RTA_PAYLOAD(tb[NDA_DST]) == sizeof(struct in6_addr))
+				family = AF_INET6;
+			else
+				family = AF_INET;
+		}
+
+		dst = format_host_rta(family, tb[NDA_DST]);
+		print_color_string(PRINT_ANY, ifa_family_color(family),
+				   "dst", "%-39s ", dst);
+	}
+
+	if (!filter.index && r->ndm_ifindex) {
+		print_color_string(PRINT_ANY, COLOR_IFNAME,
+				   "dev", "%-16s ",
+				   ll_index_to_name(r->ndm_ifindex));
+	}
+
+	if (tb[NDA_LLADDR]) {
+		const char *lladdr;
+
+		SPRINT_BUF(b1);
+
+		lladdr = ll_addr_n2a(RTA_DATA(tb[NDA_LLADDR]),
+				     RTA_PAYLOAD(tb[NDA_LLADDR]),
+				     ll_index_to_type(r->ndm_ifindex),
+				     b1, sizeof(b1));
+
+		print_color_string(PRINT_ANY, COLOR_MAC,
+				   "lladdr", "%s", lladdr);
+	}
+
+	print_string(PRINT_FP, NULL, "%s", "\n");
+	close_json_object();
+	fflush(fp);
+
+	return 0;
+}
+
 int print_neigh(struct nlmsghdr *n, void *arg)
 {
 	FILE *fp = (FILE *)arg;
@@ -337,6 +382,9 @@ int print_neigh(struct nlmsghdr *n, void *arg)
 	else if (n->nlmsg_type == RTM_GETNEIGH)
 		print_null(PRINT_ANY, "miss", "%s ", "miss");
 
+	if (brief)
+		return print_neigh_brief(fp, r, tb);
+
 	if (tb[NDA_DST]) {
 		const char *dst;
 		int family = r->ndm_family;
@@ -412,7 +460,7 @@ int print_neigh(struct nlmsghdr *n, void *arg)
 
 	print_string(PRINT_FP, NULL, "\n", "");
 	close_json_object();
-	fflush(stdout);
+	fflush(fp);
 
 	return 0;
 }
diff --git a/man/man8/ip.8 b/man/man8/ip.8
index c9f7671e..3f572889 100644
--- a/man/man8/ip.8
+++ b/man/man8/ip.8
@@ -227,7 +227,7 @@ print human readable rates in IEC units (e.g. 1Ki = 1024).
 .BR "\-br" , " \-brief"
 Print only basic information in a tabular format for better
 readability. This option is currently only supported by
-.BR "ip addr show " and " ip link show " commands.
+.BR "ip addr show ", " ip link show " & " ip neigh show " commands.
 
 .TP
 .BR "\-j", " \-json"
-- 
2.25.1


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

* Re: [PATCH iproute2-next v2] ipneigh: add support to print brief output of neigh cache in tabular format
  2021-07-27 16:46 [PATCH iproute2-next v2] ipneigh: add support to print brief output of neigh cache in tabular format Gokul Sivakumar
@ 2021-08-02 16:16 ` David Ahern
  0 siblings, 0 replies; 2+ messages in thread
From: David Ahern @ 2021-08-02 16:16 UTC (permalink / raw)
  To: Gokul Sivakumar, netdev, David Ahern

On 7/27/21 10:46 AM, Gokul Sivakumar wrote:
> Make use of the already available brief flag and print the basic details of
> the IPv4 or IPv6 neighbour cache in a tabular format for better readability
> when the brief output is expected.
> 
> $ ip -br neigh
> 172.16.12.100                           bridge0          b0:fc:36:2f:07:43
> 172.16.12.174                           bridge0          8c:16:45:2f:bc:1c
> 172.16.12.250                           bridge0          04:d9:f5:c1:0c:74
> fe80::267b:9f70:745e:d54d               bridge0          b0:fc:36:2f:07:43
> fd16:a115:6a62:0:8744:efa1:9933:2c4c    bridge0          8c:16:45:2f:bc:1c
> fe80::6d9:f5ff:fec1:c74                 bridge0          04:d9:f5:c1:0c:74
> 
> And add "ip neigh show" to the list of ip sub commands mentioned in the man
> page that support the brief output in tabular format.
> 
> Signed-off-by: Gokul Sivakumar <gokulkumar792@gmail.com>
> ---
> 
> Notes:
>     Changes in v2:
>     - Reordered the columns in the brief output to be consistent with the order of the
>       fields in the non-brief output.
>     - changed the format specifier width of dst field from "%-40s" to "%-39s " to be
>       consistent with the way width of dev field is specified.
> 
>  ip/ipneigh.c  | 50 +++++++++++++++++++++++++++++++++++++++++++++++++-
>  man/man8/ip.8 |  2 +-
>  2 files changed, 50 insertions(+), 2 deletions(-)
> 

applied to iproute2-next


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

end of thread, other threads:[~2021-08-02 16:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-27 16:46 [PATCH iproute2-next v2] ipneigh: add support to print brief output of neigh cache in tabular format Gokul Sivakumar
2021-08-02 16:16 ` David Ahern

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).