Skip to contentSkip to conversation

bridge: json and color output

PATCH Superseded Stephen Hemminger 2 participants 7 messages Feb 19, 2018 – Feb 20, 2018

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover bridge: json and color output

This set of patches adds color and full JSON support to bridge command. It also aligns ths output format and command line options to match ip command; this is a change that may impact those using "compress vlan" option or doing screen scraping.

The JSON output also changes slightly. But this should not impact any real user, because in several cases the current format was invalid JSON!

Stephen Hemminger (4):
bridge: implement json pretty print flag
bridge: colorize output and use JSON print library
bridge: add json support for link command
bridge: update man page for new color and json changes

 bridge/br_common.h |   5 +-
 bridge/bridge.c    |  16 ++-
 bridge/fdb.c       | 279 ++++++++++++++++-------------------------
 bridge/link.c      | 304 +++++++++++++++++++++++++++------------------
 bridge/mdb.c       | 354 ++++++++++++++++++++++-------------------------------
 bridge/vlan.c      | 280 ++++++++++++++++--------------------------
 man/man8/bridge.8  |  15 ++-
 7 files changed, 574 insertions(+), 679 deletions(-)
Signature · 1 line
2.16.1
#1 lore raw

PATCH 1/4 bridge: implement json pretty print flag

Make bridge work like other iproute2 commands with standard -j and -p flags.

---
 bridge/br_common.h |  3 ++-
 bridge/bridge.c    |  9 ++++++---
 bridge/fdb.c       | 10 ++++++----
 bridge/mdb.c       | 12 ++++++++++--
 bridge/vlan.c      | 11 +++++++----
 man/man8/bridge.8  |  6 ++++++
 6 files changed, 37 insertions(+), 14 deletions(-)
bridge/br_common.h +2 -1
index f07c7d1c9090..32dc46a2415d 100644
@@ -24,7 +24,8 @@ extern int do_link(int argc, char **argv);
 extern int preferred_family;
 extern int show_stats;
 extern int show_details;
+extern int show_pretty;
 extern int timestamp;
 extern int compress_vlans;
-extern int json_output;
+extern int json;
 extern struct rtnl_handle rth;
bridge/bridge.c +6 -3
index 48fe1c8b2ef1..3837c3d79290 100644
@@ -22,8 +22,9 @@ int preferred_family = AF_UNSPEC;
 int oneline;
 int show_stats;
 int show_details;
+int show_pretty;
 int compress_vlans;
-int json_output;
+int json;
 int timestamp;
 char *batch_file;
 int force;
@@ -39,7 +40,7 @@ static void usage(void)
 "where	OBJECT := { link | fdb | mdb | vlan | monitor }\n"
 "	OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] |\n"
 "		     -o[neline] | -t[imestamp] | -n[etns] name |\n"
-"		     -c[ompressvlans] -j{son} }\n");
+"		     -c[ompressvlans] -p[retty] -j{son} }\n");
 	exit(-1);
 }
 
@@ -175,7 +176,9 @@ main(int argc, char **argv)
 		} else if (matches(opt, "-force") == 0) {
 			++force;
 		} else if (matches(opt, "-json") == 0) {
-			++json_output;
+			++json;
+		} else if (matches(opt, "-pretty") == 0) {
+			++show_pretty;
 		} else if (matches(opt, "-batch") == 0) {
 			argc--;
 			argv++;
bridge/fdb.c +6 -4
index 8b133f9c1c8d..82dff5167e86 100644
@@ -132,10 +132,8 @@ int print_fdb(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 	if (filter_vlan && filter_vlan != vid)
 		return 0;
 
-	if (jw_global) {
-		jsonw_pretty(jw_global, 1);
+	if (jw_global)
 		jsonw_start_object(jw_global);
-	}
 
 	if (n->nlmsg_type == RTM_DELNEIGH) {
 		if (jw_global)
@@ -388,14 +386,18 @@ static int fdb_show(int argc, char **argv)
 		exit(1);
 	}
 
-	if (json_output) {
+	if (json) {
 		jw_global = jsonw_new(stdout);
 		if (!jw_global) {
 			fprintf(stderr, "Error allocation json object\n");
 			exit(1);
 		}
+		if (show_pretty)
+			jsonw_pretty(jw_global, 1);
+
 		jsonw_start_array(jw_global);
 	}
+
 	if (rtnl_dump_filter(&rth, print_fdb, stdout) < 0) {
 		fprintf(stderr, "Dump terminated\n");
 		exit(1);
bridge/mdb.c +10 -2
index 62dc8a0c58c3..0873502e8a24 100644
@@ -325,7 +325,7 @@ static int mdb_show(int argc, char **argv)
 		return -1;
 	}
 
-	if (!json_output) {
+	if (!json) {
 		/* Normal output */
 		if (rtnl_dump_filter(&rth, print_mdb, stdout) < 0) {
 			fprintf(stderr, "Dump terminated\n");
@@ -333,9 +333,17 @@ static int mdb_show(int argc, char **argv)
 		}
 		return 0;
 	}
+
 	/* Json output */
 	jw_global = jsonw_new(stdout);
-	jsonw_pretty(jw_global, 1);
+	if (!jw_global) {
+		fprintf(stderr, "Error allocation json object\n");
+		exit(1);
+	}
+
+	if (show_pretty)
+		jsonw_pretty(jw_global, 1);
+
 	jsonw_start_object(jw_global);
 	jsonw_name(jw_global, "mdb");
 	jsonw_start_array(jw_global);
bridge/vlan.c +7 -4
index f42d7e6ba244..e4eab4c2384f 100644
@@ -258,7 +258,6 @@ static int filter_vlan_check(__u16 vid, __u16 flags)
 static void print_vlan_port(FILE *fp, int ifi_index)
 {
 	if (jw_global) {
-		jsonw_pretty(jw_global, 1);
 		jsonw_name(jw_global,
 			   ll_index_to_name(ifi_index));
 		jsonw_start_array(jw_global);
@@ -578,17 +577,21 @@ static int vlan_show(int argc, char **argv)
 	if (!show_stats) {
 		if (rtnl_wilddump_req_filter(&rth, PF_BRIDGE, RTM_GETLINK,
 					     (compress_vlans ?
-						RTEXT_FILTER_BRVLAN_COMPRESSED :
-						RTEXT_FILTER_BRVLAN)) < 0) {
+					      RTEXT_FILTER_BRVLAN_COMPRESSED :
+					      RTEXT_FILTER_BRVLAN)) < 0) {
 			perror("Cannont send dump request");
 			exit(1);
 		}
-		if (json_output) {
+
+		if (json) {
 			jw_global = jsonw_new(stdout);
 			if (!jw_global) {
 				fprintf(stderr, "Error allocation json object\n");
 				exit(1);
 			}
+			if (show_pretty)
+				jsonw_pretty(jw_global, 1);
+
 			jsonw_start_object(jw_global);
 		} else {
 			if (show_vlan_tunnel_info)
man/man8/bridge.8 +6 -0
index d6baa819020a..490ef58da16c 100644
@@ -22,6 +22,7 @@ bridge \- show / manipulate bridge addresses and devices
 \fB\-s\fR[\fItatistics\fR] |
 \fB\-n\fR[\fIetns\fR] name |
 \fB\-b\fR[\fIatch\fR] filename |
+\fB\-p\fR[\fIretty\fR] |
 \fB\-j\fR[\fIson\fR] }
 
 .ti -8
@@ -171,6 +172,11 @@ return code will be non zero.
 .BR "\-json"
 Display results in JSON format. Currently available for vlan and fdb.
 
+.TP
+.BR "\-pretty"
+When combined with -j generate a pretty JSON output.
+
+
 .SH BRIDGE - COMMAND SYNTAX
 
 .SS
Signature · 1 line
2.16.1
#4 lore raw

PATCH 2/4 bridge: colorize output and use JSON print library

In reply to Stephen Hemminger

Use new functions from json_print to simplify code. Provide standard flag for colorizing output.

Note: the simple -c flag is ambiguous it could mean color or compressvlan; it is now changed to mean color for consistency with other iproute2 commands.

---
 bridge/br_common.h |   2 +-
 bridge/bridge.c    |   9 +-
 bridge/fdb.c       | 281 +++++++++++++++--------------------------
 bridge/mdb.c       | 362 ++++++++++++++++++++++-------------------------------
 bridge/vlan.c      | 275 +++++++++++++++-------------------------
 5 files changed, 362 insertions(+), 567 deletions(-)
bridge/br_common.h +1 -1
index 32dc46a2415d..1dcb14ae8062 100644
@@ -6,7 +6,7 @@
 #define MDB_RTR_RTA(r) \
 		((struct rtattr *)(((char *)(r)) + RTA_ALIGN(sizeof(__u32))))
 
-extern void print_vlan_info(FILE *fp, struct rtattr *tb, int ifindex);
+extern void print_vlan_info(FILE *fp, struct rtattr *tb);
 extern int print_linkinfo(const struct sockaddr_nl *who,
 			  struct nlmsghdr *n,
 			  void *arg);
bridge/bridge.c +8 -1
index 3837c3d79290..fe4050407377 100644
@@ -16,6 +16,7 @@
 #include "utils.h"
 #include "br_common.h"
 #include "namespace.h"
+#include "color.h"
 
 struct rtnl_handle rth = { .fd = -1 };
 int preferred_family = AF_UNSPEC;
@@ -23,6 +24,7 @@ int oneline;
 int show_stats;
 int show_details;
 int show_pretty;
+int color;
 int compress_vlans;
 int json;
 int timestamp;
@@ -40,7 +42,7 @@ static void usage(void)
 "where	OBJECT := { link | fdb | mdb | vlan | monitor }\n"
 "	OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] |\n"
 "		     -o[neline] | -t[imestamp] | -n[etns] name |\n"
-"		     -c[ompressvlans] -p[retty] -j{son} }\n");
+"		     -c[ompressvlans] -color -p[retty] -j{son} }\n");
 	exit(-1);
 }
 
@@ -171,6 +173,8 @@ main(int argc, char **argv)
 			NEXT_ARG();
 			if (netns_switch(argv[1]))
 				exit(-1);
+		} else if (matches(opt, "-color") == 0) {
+			enable_color();
 		} else if (matches(opt, "-compressvlans") == 0) {
 			++compress_vlans;
 		} else if (matches(opt, "-force") == 0) {
@@ -196,6 +200,9 @@ main(int argc, char **argv)
 
 	_SL_ = oneline ? "\\" : "\n";
 
+	if (json)
+		check_if_color_enabled();
+
 	if (batch_file)
 		return batch(batch_file);
 
bridge/fdb.c +103 -178

356 lines not on this page · raw message

bridge/mdb.c +148 -214

458 lines not on this page · raw message

bridge/vlan.c +102 -173

420 lines not on this page · raw message

Signature · 1 line
2.16.1
#5 lore raw

PATCH 3/4 bridge: add json support for link command

In reply to Stephen Hemminger

Add json output for bridge link show command.
This also changes the output format slightly for the non JSON case so that it has same format as the ip link show command.

---
 bridge/link.c | 304 ++++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 188 insertions(+), 116 deletions(-)
bridge/link.c +188 -116
index 870ebe050477..12cc995e05e3 100644
@@ -12,6 +12,7 @@
 #include <string.h>
 #include <stdbool.h>
 
+#include "json_print.h"
 #include "libnetlink.h"
 #include "utils.h"
 #include "br_common.h"
@@ -26,15 +27,27 @@ static const char *port_states[] = {
 	[BR_STATE_BLOCKING] = "blocking",
 };
 
-static void print_link_flags(FILE *fp, unsigned int flags)
+
+static const char *oper_states[] = {
+	"UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
+	"TESTING", "DORMANT",	 "UP"
+};
+
+static const char *hw_mode[] = {
+	"VEB", "VEPA"
+};
+
+static void print_link_flags(FILE *fp, unsigned int flags, unsigned int mdown)
 {
-	fprintf(fp, "<");
+	open_json_array(PRINT_ANY, is_json_context() ? "flags" : "<");
 	if (flags & IFF_UP && !(flags & IFF_RUNNING))
-		fprintf(fp, "NO-CARRIER%s", flags ? "," : "");
+		print_string(PRINT_ANY, NULL,
+			     flags ? "%s," : "%s", "NO-CARRIER");
 	flags &= ~IFF_RUNNING;
-#define _PF(f) if (flags&IFF_##f) { \
-		  flags &= ~IFF_##f ; \
-		  fprintf(fp, #f "%s", flags ? "," : ""); }
+
+#define _PF(f) if (flags&IFF_##f) {					\
+		flags &= ~IFF_##f ;					\
+		print_string(PRINT_ANY, NULL, flags ? "%s," : "%s", #f); }
 	_PF(LOOPBACK);
 	_PF(BROADCAST);
 	_PF(POINTOPOINT);
@@ -55,44 +68,136 @@ static void print_link_flags(FILE *fp, unsigned int flags)
 	_PF(ECHO);
 #undef _PF
 	if (flags)
-		fprintf(fp, "%x", flags);
-	fprintf(fp, "> ");
+		print_hex(PRINT_ANY, NULL, "%x", flags);
+	if (mdown)
+		print_string(PRINT_ANY, NULL, ",%s", "M-DOWN");
+	close_json_array(PRINT_ANY, "> ");
 }
 
-static const char *oper_states[] = {
-	"UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
-	"TESTING", "DORMANT",	 "UP"
-};
-
-static const char *hw_mode[] = {"VEB", "VEPA"};
-
 static void print_operstate(FILE *f, __u8 state)
 {
-	if (state >= ARRAY_SIZE(oper_states))
-		fprintf(f, "state %#x ", state);
-	else
-		fprintf(f, "state %s ", oper_states[state]);
+	if (state >= ARRAY_SIZE(oper_states)) {
+		print_0xhex(PRINT_ANY, "operstate", "operstat %#x", state);
+	} else {
+		if (is_json_context())
+			print_string(PRINT_JSON,
+				     "operstate",
+				     NULL, oper_states[state]);
+		else {
+			fprintf(f, "state ");
+			color_fprintf(f, oper_state_color(state),
+				      "%s ", oper_states[state]);
+		}
+	}
 }
 
-static void print_portstate(FILE *f, __u8 state)
+static void print_portstate(__u8 state)
 {
 	if (state <= BR_STATE_BLOCKING)
-		fprintf(f, "state %s ", port_states[state]);
+		print_string(PRINT_ANY, "state",
+			     "state %s ", port_states[state]);
 	else
-		fprintf(f, "state (%d) ", state);
+		print_uint(PRINT_ANY, "state",
+			     "state (%d) ", state);
 }
 
-static void print_onoff(FILE *f, char *flag, __u8 val)
+static void print_onoff(FILE *fp, const char *flag, __u8 val)
 {
-	fprintf(f, "%s %s ", flag, val ? "on" : "off");
+	if (is_json_context())
+		print_bool(PRINT_JSON, flag, NULL, val);
+	else
+		fprintf(fp, "%s %s ", flag, val ? "on" : "off");
 }
 
-static void print_hwmode(FILE *f, __u16 mode)
+static void print_hwmode(__u16 mode)
 {
 	if (mode >= ARRAY_SIZE(hw_mode))
-		fprintf(f, "hwmode %#hx ", mode);
+		print_0xhex(PRINT_ANY, "hwmode",
+			    "hwmode %#hx ", mode);
 	else
-		fprintf(f, "hwmode %s ", hw_mode[mode]);
+		print_string(PRINT_ANY, "hwmode",
+			     "hwmode %s ", hw_mode[mode]);
+}
+
+static void print_protinfo(FILE *fp, struct rtattr *attr)
+{
+	if (attr->rta_type & NLA_F_NESTED) {
+		struct rtattr *prtb[IFLA_BRPORT_MAX + 1];
+
+		parse_rtattr_nested(prtb, IFLA_BRPORT_MAX, attr);
+
+		if (prtb[IFLA_BRPORT_STATE])
+			print_portstate(rta_getattr_u8(prtb[IFLA_BRPORT_STATE]));
+
+		if (prtb[IFLA_BRPORT_PRIORITY])
+			print_uint(PRINT_ANY, "priority",
+				   "priority %u ",
+				   rta_getattr_u16(prtb[IFLA_BRPORT_PRIORITY]));
+
+		if (prtb[IFLA_BRPORT_COST])
+			print_uint(PRINT_ANY, "cost",
+				   "cost %u ",
+				   rta_getattr_u32(prtb[IFLA_BRPORT_COST]));
+
+		if (!show_details)
+			return;
+
+		if (!is_json_context())
+			fprintf(fp, "%s    ", _SL_);
+
+		if (prtb[IFLA_BRPORT_MODE])
+			print_onoff(fp, "hairpin",
+				    rta_getattr_u8(prtb[IFLA_BRPORT_MODE]));
+		if (prtb[IFLA_BRPORT_GUARD])
+			print_onoff(fp, "guard",
+				    rta_getattr_u8(prtb[IFLA_BRPORT_GUARD]));
+		if (prtb[IFLA_BRPORT_PROTECT])
+			print_onoff(fp, "root_block",
+				    rta_getattr_u8(prtb[IFLA_BRPORT_PROTECT]));
+		if (prtb[IFLA_BRPORT_FAST_LEAVE])
+			print_onoff(fp, "fastleave",
+				    rta_getattr_u8(prtb[IFLA_BRPORT_FAST_LEAVE]));
+		if (prtb[IFLA_BRPORT_LEARNING])
+			print_onoff(fp, "learning",
+				    rta_getattr_u8(prtb[IFLA_BRPORT_LEARNING]));
+		if (prtb[IFLA_BRPORT_LEARNING_SYNC])
+			print_onoff(fp, "learning_sync",
+				    rta_getattr_u8(prtb[IFLA_BRPORT_LEARNING_SYNC]));
+		if (prtb[IFLA_BRPORT_UNICAST_FLOOD])
+			print_onoff(fp, "flood",
+				    rta_getattr_u8(prtb[IFLA_BRPORT_UNICAST_FLOOD]));
+		if (prtb[IFLA_BRPORT_MCAST_FLOOD])
+			print_onoff(fp, "mcast_flood",
+				    rta_getattr_u8(prtb[IFLA_BRPORT_MCAST_FLOOD]));
+		if (prtb[IFLA_BRPORT_NEIGH_SUPPRESS])
+			print_onoff(fp, "neigh_suppress",
+				    rta_getattr_u8(prtb[IFLA_BRPORT_NEIGH_SUPPRESS]));
+		if (prtb[IFLA_BRPORT_VLAN_TUNNEL])
+			print_onoff(fp, "vlan_tunnel",
+				    rta_getattr_u8(prtb[IFLA_BRPORT_VLAN_TUNNEL]));
+	} else
+		print_portstate(rta_getattr_u8(attr));
+}
+
+
+/*
+ * This is reported by HW devices that have some bridging
+ * capabilities.
+ */
+static void print_af_spec(FILE *fp, struct rtattr *attr)
+{
+	struct rtattr *aftb[IFLA_BRIDGE_MAX+1];
+
+	parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, attr);
+
+	if (aftb[IFLA_BRIDGE_MODE])
+		print_hwmode(rta_getattr_u16(aftb[IFLA_BRIDGE_MODE]));
+
+	if (!show_details)
+		return;
+
+	if (aftb[IFLA_BRIDGE_VLAN_INFO])
+		print_vlan_info(fp, aftb[IFLA_BRIDGE_VLAN_INFO]);
 }
 
 int print_linkinfo(const struct sockaddr_nl *who,
@@ -102,6 +207,7 @@ int print_linkinfo(const struct sockaddr_nl *who,
 	int len = n->nlmsg_len;
 	struct ifinfomsg *ifi = NLMSG_DATA(n);
 	struct rtattr *tb[IFLA_MAX+1];
+	unsigned int m_flag = 0;
 
 	len -= NLMSG_LENGTH(sizeof(*ifi));
 	if (len < 0) {
@@ -118,113 +224,75 @@ int print_linkinfo(const struct sockaddr_nl *who,
 	parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len, NLA_F_NESTED);
 
 	if (tb[IFLA_IFNAME] == NULL) {
-		fprintf(stderr, "BUG: nil ifname\n");
+		fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n",
+			ifi->ifi_index);
 		return -1;
 	}
 
+	open_json_object(NULL);
 	if (n->nlmsg_type == RTM_DELLINK)
-		fprintf(fp, "Deleted ");
-
-	fprintf(fp, "%d: %s ", ifi->ifi_index,
-		tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : "<nil>");
-
-	if (tb[IFLA_OPERSTATE])
-		print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE]));
+		print_bool(PRINT_ANY, "deleted", "Deleted ", true);
+
+	print_int(PRINT_ANY, "ifindex", "%d: ", ifi->ifi_index);
+	if (tb[IFLA_IFNAME]) {
+		print_color_string(PRINT_ANY,
+				   COLOR_IFNAME,
+				   "ifname", "%s",
+				   rta_getattr_str(tb[IFLA_IFNAME]));
+	} else {
+		print_null(PRINT_JSON, "ifname", NULL, NULL);
+		print_color_null(PRINT_FP, COLOR_IFNAME,
+				 "ifname", "%s", "<nil>");
+	}
 
 	if (tb[IFLA_LINK]) {
 		int iflink = rta_getattr_u32(tb[IFLA_LINK]);
 
-		fprintf(fp, "@%s: ",
-			iflink ? ll_index_to_name(iflink) : "NONE");
-	} else
-		fprintf(fp, ": ");
+		if (iflink == 0)
+			print_null(PRINT_ANY, "link", "@%s: ", "NONE");
+		else {
+			if (tb[IFLA_LINK_NETNSID])
+				print_int(PRINT_ANY,
+					  "link_index", "@if%d: ", iflink);
+			else {
+				SPRINT_BUF(b1);
+
+				print_string(PRINT_ANY,
+					     "link",
+					     "@%s: ",
+					     ll_idx_n2a(iflink, b1));
+				m_flag = ll_index_to_flags(iflink);
+				m_flag = !(m_flag & IFF_UP);
+			}
+		}
+	} else {
+		print_string(PRINT_FP, NULL, ": ", NULL);
+	}
+	print_link_flags(fp, ifi->ifi_flags, m_flag);
 
-	print_link_flags(fp, ifi->ifi_flags);
+	if (tb[IFLA_OPERSTATE])
+		print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE]));
 
 	if (tb[IFLA_MTU])
-		fprintf(fp, "mtu %u ", rta_getattr_u32(tb[IFLA_MTU]));
+		print_int(PRINT_ANY,
+			  "mtu", "mtu %u ",
+			  rta_getattr_u32(tb[IFLA_MTU]));
 
 	if (tb[IFLA_MASTER]) {
-		int master = rta_getattr_u32(tb[IFLA_MASTER]);
+		SPRINT_BUF(b1);
 
-		fprintf(fp, "master %s ", ll_index_to_name(master));
+		print_string(PRINT_ANY, "master", "master %s ",
+			     ll_idx_n2a(rta_getattr_u32(tb[IFLA_MASTER]), b1));
 	}
 
-	if (tb[IFLA_PROTINFO]) {
-		if (tb[IFLA_PROTINFO]->rta_type & NLA_F_NESTED) {
-			struct rtattr *prtb[IFLA_BRPORT_MAX+1];
-
-			parse_rtattr_nested(prtb, IFLA_BRPORT_MAX,
-					    tb[IFLA_PROTINFO]);
-
-			if (prtb[IFLA_BRPORT_STATE])
-				print_portstate(fp,
-						rta_getattr_u8(prtb[IFLA_BRPORT_STATE]));
-			if (prtb[IFLA_BRPORT_PRIORITY])
-				fprintf(fp, "priority %hu ",
-					rta_getattr_u16(prtb[IFLA_BRPORT_PRIORITY]));
-			if (prtb[IFLA_BRPORT_COST])
-				fprintf(fp, "cost %u ",
-					rta_getattr_u32(prtb[IFLA_BRPORT_COST]));
-
-			if (show_details) {
-				fprintf(fp, "%s    ", _SL_);
-
-				if (prtb[IFLA_BRPORT_MODE])
-					print_onoff(fp, "hairpin",
-						    rta_getattr_u8(prtb[IFLA_BRPORT_MODE]));
-				if (prtb[IFLA_BRPORT_GUARD])
-					print_onoff(fp, "guard",
-						    rta_getattr_u8(prtb[IFLA_BRPORT_GUARD]));
-				if (prtb[IFLA_BRPORT_PROTECT])
-					print_onoff(fp, "root_block",
-						    rta_getattr_u8(prtb[IFLA_BRPORT_PROTECT]));
-				if (prtb[IFLA_BRPORT_FAST_LEAVE])
-					print_onoff(fp, "fastleave",
-						    rta_getattr_u8(prtb[IFLA_BRPORT_FAST_LEAVE]));
-				if (prtb[IFLA_BRPORT_LEARNING])
-					print_onoff(fp, "learning",
-						    rta_getattr_u8(prtb[IFLA_BRPORT_LEARNING]));
-				if (prtb[IFLA_BRPORT_LEARNING_SYNC])
-					print_onoff(fp, "learning_sync",
-						    rta_getattr_u8(prtb[IFLA_BRPORT_LEARNING_SYNC]));
-				if (prtb[IFLA_BRPORT_UNICAST_FLOOD])
-					print_onoff(fp, "flood",
-						    rta_getattr_u8(prtb[IFLA_BRPORT_UNICAST_FLOOD]));
-				if (prtb[IFLA_BRPORT_MCAST_FLOOD])
-					print_onoff(fp, "mcast_flood",
-						    rta_getattr_u8(prtb[IFLA_BRPORT_MCAST_FLOOD]));
-				if (prtb[IFLA_BRPORT_NEIGH_SUPPRESS])
-					print_onoff(fp, "neigh_suppress",
-						    rta_getattr_u8(prtb[IFLA_BRPORT_NEIGH_SUPPRESS]));
-				if (prtb[IFLA_BRPORT_VLAN_TUNNEL])
-					print_onoff(fp, "vlan_tunnel",
-						    rta_getattr_u8(prtb[IFLA_BRPORT_VLAN_TUNNEL]));
-			}
-		} else
-			print_portstate(fp, rta_getattr_u8(tb[IFLA_PROTINFO]));
-	}
+	if (tb[IFLA_PROTINFO])
+		print_protinfo(fp, tb[IFLA_PROTINFO]);
 
-	if (tb[IFLA_AF_SPEC]) {
-		/* This is reported by HW devices that have some bridging
-		 * capabilities.
-		 */
-		struct rtattr *aftb[IFLA_BRIDGE_MAX+1];
-
-		parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, tb[IFLA_AF_SPEC]);
-
-		if (aftb[IFLA_BRIDGE_MODE])
-			print_hwmode(fp, rta_getattr_u16(aftb[IFLA_BRIDGE_MODE]));
-		if (show_details) {
-			if (aftb[IFLA_BRIDGE_VLAN_INFO]) {
-				fprintf(fp, "\n");
-				print_vlan_info(fp, tb[IFLA_AF_SPEC],
-						ifi->ifi_index);
-			}
-		}
-	}
+	if (tb[IFLA_AF_SPEC])
+		print_af_spec(fp, tb[IFLA_AF_SPEC]);
 
-	fprintf(fp, "\n");
+	print_string(PRINT_FP, NULL, "%s", "\n");
+	close_json_object();
 	fflush(fp);
 	return 0;
 }
@@ -499,10 +567,14 @@ static int brlink_show(int argc, char **argv)
 		}
 	}
 
+	new_json_obj(json);
 	if (rtnl_dump_filter(&rth, print_linkinfo, stdout) < 0) {
 		fprintf(stderr, "Dump terminated\n");
 		exit(1);
 	}
+
+	delete_json_obj();
+	fflush(stdout);
 	return 0;
 }
 
Signature · 1 line
2.16.1
#6 lore raw

PATCH 4/4 bridge: update man page for new color and json changes

In reply to Stephen Hemminger

Document color option, and no longer have restriction on json

---
 man/man8/bridge.8 | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
man/man8/bridge.8 +8 -3
index 490ef58da16c..05512b029fb9 100644
@@ -22,6 +22,7 @@ bridge \- show / manipulate bridge addresses and devices
 \fB\-s\fR[\fItatistics\fR] |
 \fB\-n\fR[\fIetns\fR] name |
 \fB\-b\fR[\fIatch\fR] filename |
+\fB\-c\fR[\folor\fR] |
 \fB\-p\fR[\fIretty\fR] |
 \fB\-j\fR[\fIson\fR] }
 
@@ -169,11 +170,15 @@ If there were any errors during execution of the commands, the application
 return code will be non zero.
 
 .TP
-.BR "\-json"
-Display results in JSON format. Currently available for vlan and fdb.
+.BR "\-c" , " -color"
+Use color output.
 
 .TP
-.BR "\-pretty"
+.BR "\-j", " \-json"
+Output results in JavaScript Object Notation (JSON).
+
+.TP
+.BR "\-p", " \-pretty"
 When combined with -j generate a pretty JSON output.
 
 
Signature · 1 line
2.16.1
#2 lore raw

Re: [PATCH iproute2-next 1/4] bridge: implement json pretty print flag

In reply to Stephen Hemminger
Quoted 18 lines · Stephen Hemminger

Make bridge work like other iproute2 commands with standard -j and -p flags.

---
 bridge/br_common.h |  3 ++-
 bridge/bridge.c    |  9 ++++++---
 bridge/fdb.c       | 10 ++++++----
 bridge/mdb.c       | 12 ++++++++++--
 bridge/vlan.c      | 11 +++++++----
 man/man8/bridge.8  |  6 ++++++
 6 files changed, 37 insertions(+), 14 deletions(-)
bridge/br_common.h +0 -0
index f07c7d1c9090..32dc46a2415d 100644

bridge/br_common.h extern int do_link(int argc, char **argv); · line 27

Quoted 4 lines
bridge/br_common.h +1 -0
@@ -24,7 +24,8 @@ extern int do_link(int argc, char **argv);
 extern int preferred_family;
 extern int show_stats;
 extern int show_details;
+extern int show_pretty;

Why not to use @pretty from utils.c/utils.h recently added as common point for json_print.c and rest of the code with commit 543365670534 ("ip: Use single variable to represent -pretty")?

While new_json_obj() from json_print.c isn't used in bridge/ it is still seems reasonable in my opinion to use common @pretty variable.

Quoted 170 lines
bridge/br_common.h +1 -1
 extern int timestamp;
 extern int compress_vlans;
-extern int json_output;
+extern int json;
 extern struct rtnl_handle rth;
bridge/bridge.c +6 -3
index 48fe1c8b2ef1..3837c3d79290 100644
@@ -22,8 +22,9 @@ int preferred_family = AF_UNSPEC;
 int oneline;
 int show_stats;
 int show_details;
+int show_pretty;
 int compress_vlans;
-int json_output;
+int json;
 int timestamp;
 char *batch_file;
 int force;
@@ -39,7 +40,7 @@ static void usage(void)
 "where	OBJECT := { link | fdb | mdb | vlan | monitor }\n"
 "	OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] |\n"
 "		     -o[neline] | -t[imestamp] | -n[etns] name |\n"
-"		     -c[ompressvlans] -j{son} }\n");
+"		     -c[ompressvlans] -p[retty] -j{son} }\n");
 	exit(-1);
 }
 
@@ -175,7 +176,9 @@ main(int argc, char **argv)
 		} else if (matches(opt, "-force") == 0) {
 			++force;
 		} else if (matches(opt, "-json") == 0) {
-			++json_output;
+			++json;
+		} else if (matches(opt, "-pretty") == 0) {
+			++show_pretty;
 		} else if (matches(opt, "-batch") == 0) {
 			argc--;
 			argv++;
bridge/fdb.c +6 -4
index 8b133f9c1c8d..82dff5167e86 100644
@@ -132,10 +132,8 @@ int print_fdb(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 	if (filter_vlan && filter_vlan != vid)
 		return 0;
 
-	if (jw_global) {
-		jsonw_pretty(jw_global, 1);
+	if (jw_global)
 		jsonw_start_object(jw_global);
-	}
 
 	if (n->nlmsg_type == RTM_DELNEIGH) {
 		if (jw_global)
@@ -388,14 +386,18 @@ static int fdb_show(int argc, char **argv)
 		exit(1);
 	}
 
-	if (json_output) {
+	if (json) {
 		jw_global = jsonw_new(stdout);
 		if (!jw_global) {
 			fprintf(stderr, "Error allocation json object\n");
 			exit(1);
 		}
+		if (show_pretty)
+			jsonw_pretty(jw_global, 1);
+
 		jsonw_start_array(jw_global);
 	}
+
 	if (rtnl_dump_filter(&rth, print_fdb, stdout) < 0) {
 		fprintf(stderr, "Dump terminated\n");
 		exit(1);
bridge/mdb.c +10 -2
index 62dc8a0c58c3..0873502e8a24 100644
@@ -325,7 +325,7 @@ static int mdb_show(int argc, char **argv)
 		return -1;
 	}
 
-	if (!json_output) {
+	if (!json) {
 		/* Normal output */
 		if (rtnl_dump_filter(&rth, print_mdb, stdout) < 0) {
 			fprintf(stderr, "Dump terminated\n");
@@ -333,9 +333,17 @@ static int mdb_show(int argc, char **argv)
 		}
 		return 0;
 	}
+
 	/* Json output */
 	jw_global = jsonw_new(stdout);
-	jsonw_pretty(jw_global, 1);
+	if (!jw_global) {
+		fprintf(stderr, "Error allocation json object\n");
+		exit(1);
+	}
+
+	if (show_pretty)
+		jsonw_pretty(jw_global, 1);
+
 	jsonw_start_object(jw_global);
 	jsonw_name(jw_global, "mdb");
 	jsonw_start_array(jw_global);
bridge/vlan.c +7 -4
index f42d7e6ba244..e4eab4c2384f 100644
@@ -258,7 +258,6 @@ static int filter_vlan_check(__u16 vid, __u16 flags)
 static void print_vlan_port(FILE *fp, int ifi_index)
 {
 	if (jw_global) {
-		jsonw_pretty(jw_global, 1);
 		jsonw_name(jw_global,
 			   ll_index_to_name(ifi_index));
 		jsonw_start_array(jw_global);
@@ -578,17 +577,21 @@ static int vlan_show(int argc, char **argv)
 	if (!show_stats) {
 		if (rtnl_wilddump_req_filter(&rth, PF_BRIDGE, RTM_GETLINK,
 					     (compress_vlans ?
-						RTEXT_FILTER_BRVLAN_COMPRESSED :
-						RTEXT_FILTER_BRVLAN)) < 0) {
+					      RTEXT_FILTER_BRVLAN_COMPRESSED :
+					      RTEXT_FILTER_BRVLAN)) < 0) {
 			perror("Cannont send dump request");
 			exit(1);
 		}
-		if (json_output) {
+
+		if (json) {
 			jw_global = jsonw_new(stdout);
 			if (!jw_global) {
 				fprintf(stderr, "Error allocation json object\n");
 				exit(1);
 			}
+			if (show_pretty)
+				jsonw_pretty(jw_global, 1);
+
 			jsonw_start_object(jw_global);
 		} else {
 			if (show_vlan_tunnel_info)
man/man8/bridge.8 +6 -0
index d6baa819020a..490ef58da16c 100644
@@ -22,6 +22,7 @@ bridge \- show / manipulate bridge addresses and devices
 \fB\-s\fR[\fItatistics\fR] |
 \fB\-n\fR[\fIetns\fR] name |
 \fB\-b\fR[\fIatch\fR] filename |
+\fB\-p\fR[\fIretty\fR] |
 \fB\-j\fR[\fIson\fR] }
 
 .ti -8
@@ -171,6 +172,11 @@ return code will be non zero.
 .BR "\-json"
 Display results in JSON format. Currently available for vlan and fdb.
 
+.TP
+.BR "\-pretty"
+When combined with -j generate a pretty JSON output.
+
+
 .SH BRIDGE - COMMAND SYNTAX
 
 .SS
#3 lore raw

Re: [PATCH iproute2-next 1/4] bridge: implement json pretty print flag

Quoted 2 lines · On Tue, 20 Feb 2018 00:24:46 +0200 Serhey Popovych <[email protected]>…

While new_json_obj() from json_print.c isn't used in bridge/ it is still seems reasonable in my opinion to use common @pretty variable.

Actually new_json_obj is used in later patches 2 and 3.