diff mbox series

[iproute2,v2] bridge: vlan: fix standard stats output

Message ID 20190416153012.32490-1-nikolay@cumulusnetworks.com
State Accepted
Delegated to: stephen hemminger
Headers show
Series [iproute2,v2] bridge: vlan: fix standard stats output | expand

Commit Message

Nikolay Aleksandrov April 16, 2019, 3:30 p.m. UTC
Each of the commits below broke the vlan stats output in a different
way:
- 45fca4ed9412 ("bridge: fix vlan show stats formatting")
 Added a second print of an interface name (e.g. eth4eth4)
- c7c1a1ef51ae ("bridge: colorize output and use JSON print library")
 Broke normal vlan stats output by not printing a new line after them
 Also printed interfaces without any vlans when printing stats

This fix is not pretty but it brings back the previous behaviour.

Before this fix:
$ bridge -s vlan show
port             vlan id
br0br0              1 PVID Egress Untagged
                   RX: 0 bytes 0 packets
                   TX: 0 bytes 0 packets 4
                   RX: 0 bytes 0 packets
                   TX: 0 bytes 0 packetseth4eth4             4
                   RX: 0 bytes 0 packets
                   TX: 0 bytes 0 packetsroot@debian:~/

After this fix:
$ bridge -s vlan show
port             vlan id
br0              1 PVID Egress Untagged
                   RX: 0 bytes 0 packets
                   TX: 0 bytes 0 packets
                 4
                   RX: 0 bytes 0 packets
                   TX: 0 bytes 0 packets
eth4             4
                   RX: 0 bytes 0 packets
                   TX: 0 bytes 0 packets

Fixes: 45fca4ed9412 ("bridge: fix vlan show stats formatting")
Fixes: c7c1a1ef51ae ("bridge: colorize output and use JSON print library")
Signed-off-by: Nikolay Aleksandrov <nikolay@cumulusnetworks.com>
---
v2: simplify print_vlan_stats_attr

 bridge/vlan.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)

Comments

Stephen Hemminger April 17, 2019, 11:34 p.m. UTC | #1
On Tue, 16 Apr 2019 18:30:12 +0300
Nikolay Aleksandrov <nikolay@cumulusnetworks.com> wrote:

> Each of the commits below broke the vlan stats output in a different
> way:
> - 45fca4ed9412 ("bridge: fix vlan show stats formatting")
>  Added a second print of an interface name (e.g. eth4eth4)
> - c7c1a1ef51ae ("bridge: colorize output and use JSON print library")
>  Broke normal vlan stats output by not printing a new line after them
>  Also printed interfaces without any vlans when printing stats
> 
> This fix is not pretty but it brings back the previous behaviour.

Ok, applied.
The output format here makes it hard to code.
diff mbox series

Patch

diff --git a/bridge/vlan.c b/bridge/vlan.c
index d075a42d139b..6d33b0a990a5 100644
--- a/bridge/vlan.c
+++ b/bridge/vlan.c
@@ -252,11 +252,11 @@  static int filter_vlan_check(__u16 vid, __u16 flags)
 	return 1;
 }
 
-static void open_vlan_port(int ifi_index)
+static void open_vlan_port(int ifi_index, const char *fmt)
 {
 	open_json_object(NULL);
-	print_string(PRINT_ANY, "ifname", "%s",
-		     ll_index_to_name(ifi_index));
+	print_color_string(PRINT_ANY, COLOR_IFNAME, "ifname", fmt,
+			   ll_index_to_name(ifi_index));
 	open_json_array(PRINT_JSON, "vlans");
 }
 
@@ -286,7 +286,7 @@  static void print_vlan_tunnel_info(FILE *fp, struct rtattr *tb, int ifindex)
 	__u32 last_tunid_start = 0;
 
 	if (!filter_vlan)
-		open_vlan_port(ifindex);
+		open_vlan_port(ifindex, "%s");
 
 	open_json_array(PRINT_JSON, "tunnel");
 	for (i = RTA_DATA(list); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
@@ -331,7 +331,7 @@  static void print_vlan_tunnel_info(FILE *fp, struct rtattr *tb, int ifindex)
 			continue;
 
 		if (filter_vlan)
-			open_vlan_port(ifindex);
+			open_vlan_port(ifindex, "%s");
 
 		open_json_object(NULL);
 		print_range("vlan", last_vid_start, tunnel_vid);
@@ -463,7 +463,7 @@  static void print_one_vlan_stats(const struct bridge_vlan_xstats *vstats)
 	print_lluint(PRINT_ANY, "tx_bytes",
 		     "                   TX: %llu bytes",
 		     vstats->tx_bytes);
-	print_lluint(PRINT_ANY, "tx_packets", " %llu packets",
+	print_lluint(PRINT_ANY, "tx_packets", " %llu packets\n",
 		vstats->tx_packets);
 	close_json_object();
 }
@@ -472,7 +472,7 @@  static void print_vlan_stats_attr(struct rtattr *attr, int ifindex)
 {
 	struct rtattr *brtb[LINK_XSTATS_TYPE_MAX+1];
 	struct rtattr *i, *list;
-	const char *ifname;
+	bool found_vlan = false;
 	int rem;
 
 	parse_rtattr(brtb, LINK_XSTATS_TYPE_MAX, RTA_DATA(attr),
@@ -483,12 +483,6 @@  static void print_vlan_stats_attr(struct rtattr *attr, int ifindex)
 	list = brtb[LINK_XSTATS_TYPE_BRIDGE];
 	rem = RTA_PAYLOAD(list);
 
-	ifname = ll_index_to_name(ifindex);
-	open_vlan_port(ifindex);
-
-	print_color_string(PRINT_FP, COLOR_IFNAME,
-			   NULL, "%-16s", ifname);
-
 	for (i = RTA_DATA(list); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
 		const struct bridge_vlan_xstats *vstats = RTA_DATA(i);
 
@@ -503,9 +497,19 @@  static void print_vlan_stats_attr(struct rtattr *attr, int ifindex)
 		    !(vstats->flags & BRIDGE_VLAN_INFO_BRENTRY))
 			continue;
 
+		/* found vlan stats, first time print the interface name */
+		if (!found_vlan) {
+			open_vlan_port(ifindex, "%-16s");
+			found_vlan = true;
+		} else {
+			print_string(PRINT_FP, NULL, "%-16s", "");
+		}
 		print_one_vlan_stats(vstats);
 	}
-	close_vlan_port();
+
+	/* vlan_port is opened only if there are any vlan stats */
+	if (found_vlan)
+		close_vlan_port();
 }
 
 static int print_vlan_stats(struct nlmsghdr *n, void *arg)
@@ -632,7 +636,7 @@  void print_vlan_info(struct rtattr *tb, int ifindex)
 	int rem = RTA_PAYLOAD(list);
 	__u16 last_vid_start = 0;
 
-	open_vlan_port(ifindex);
+	open_vlan_port(ifindex, "%s");
 
 	for (i = RTA_DATA(list); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
 		struct bridge_vlan_info *vinfo;