diff mbox series

[iproute2] bridge: vlan: fix standard stats output

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

Commit Message

Nikolay Aleksandrov April 16, 2019, 3:14 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>
---
 bridge/vlan.c | 46 +++++++++++++++++++++++++++-------------------
 1 file changed, 27 insertions(+), 19 deletions(-)
diff mbox series

Patch

diff --git a/bridge/vlan.c b/bridge/vlan.c
index d075a42d139b..dbed7fbd8c02 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);
@@ -448,9 +448,15 @@  static void print_vlan_flags(__u16 flags)
 	close_json_array(PRINT_JSON, NULL);
 }
 
-static void print_one_vlan_stats(const struct bridge_vlan_xstats *vstats)
+static void print_one_vlan_stats(const struct bridge_vlan_xstats *vstats,
+				 int vlan_idx)
 {
 	open_json_object(NULL);
+	/* we need to know if this is the initial vlan to align vlan ids
+	 * properly under it
+	 */
+	if (vlan_idx > 0)
+		print_string(PRINT_FP, NULL, "%-16s", "");
 	print_hu(PRINT_ANY, "vid", " %hu", vstats->vid);
 
 	print_vlan_flags(vstats->flags);
@@ -463,7 +469,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,8 +478,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;
-	int rem;
+	int rem, vlan_idx;
 
 	parse_rtattr(brtb, LINK_XSTATS_TYPE_MAX, RTA_DATA(attr),
 		     RTA_PAYLOAD(attr));
@@ -483,13 +488,8 @@  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)) {
+	for (i = RTA_DATA(list), vlan_idx = 0; RTA_OK(i, rem);
+	     i = RTA_NEXT(i, rem)) {
 		const struct bridge_vlan_xstats *vstats = RTA_DATA(i);
 
 		if (i->rta_type != BRIDGE_XSTATS_VLAN)
@@ -503,9 +503,17 @@  static void print_vlan_stats_attr(struct rtattr *attr, int ifindex)
 		    !(vstats->flags & BRIDGE_VLAN_INFO_BRENTRY))
 			continue;
 
-		print_one_vlan_stats(vstats);
+		/* found vlan stats, print the interface name */
+		if (vlan_idx == 0)
+			open_vlan_port(ifindex, "%-16s");
+
+		print_one_vlan_stats(vstats, vlan_idx);
+		vlan_idx++;
 	}
-	close_vlan_port();
+
+	/* vlan port is opened only if there are any vlan stats */
+	if (vlan_idx > 0)
+		close_vlan_port();
 }
 
 static int print_vlan_stats(struct nlmsghdr *n, void *arg)
@@ -632,7 +640,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;