Netdev List
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: netdev@vger.kernel.org
Cc: Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH iproute2 3/3] bridge: fix vlan show formatting
Date: Thu,  6 Sep 2018 16:30:57 +0100	[thread overview]
Message-ID: <20180906153057.5379-3-stephen@networkplumber.org> (raw)
In-Reply-To: <20180906153057.5379-1-stephen@networkplumber.org>

The output of vlan show was broken previous change to use json_print.
Clean the code up and return to original format.

Note: the JSON syntax has changed to make the bridge vlan
show more like other outputs (e.g. ip -j li show).

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 bridge/br_common.h |  2 +-
 bridge/link.c      |  6 ++---
 bridge/vlan.c      | 61 +++++++++++++++++++++++++++-------------------
 3 files changed, 40 insertions(+), 29 deletions(-)

diff --git a/bridge/br_common.h b/bridge/br_common.h
index 2f1cb8fd9f3d..69665fde32b6 100644
--- a/bridge/br_common.h
+++ b/bridge/br_common.h
@@ -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);
+extern void print_vlan_info(struct rtattr *tb, int ifindex);
 extern int print_linkinfo(const struct sockaddr_nl *who,
 			  struct nlmsghdr *n,
 			  void *arg);
diff --git a/bridge/link.c b/bridge/link.c
index 8d89aca2e638..a5ee9a5c58e6 100644
--- a/bridge/link.c
+++ b/bridge/link.c
@@ -161,7 +161,7 @@ static void print_protinfo(FILE *fp, struct rtattr *attr)
  * This is reported by HW devices that have some bridging
  * capabilities.
  */
-static void print_af_spec(FILE *fp, struct rtattr *attr)
+static void print_af_spec(struct rtattr *attr, int ifindex)
 {
 	struct rtattr *aftb[IFLA_BRIDGE_MAX+1];
 
@@ -174,7 +174,7 @@ static void print_af_spec(FILE *fp, struct rtattr *attr)
 		return;
 
 	if (aftb[IFLA_BRIDGE_VLAN_INFO])
-		print_vlan_info(fp, aftb[IFLA_BRIDGE_VLAN_INFO]);
+		print_vlan_info(aftb[IFLA_BRIDGE_VLAN_INFO], ifindex);
 }
 
 int print_linkinfo(const struct sockaddr_nl *who,
@@ -229,7 +229,7 @@ int print_linkinfo(const struct sockaddr_nl *who,
 		print_protinfo(fp, tb[IFLA_PROTINFO]);
 
 	if (tb[IFLA_AF_SPEC])
-		print_af_spec(fp, tb[IFLA_AF_SPEC]);
+		print_af_spec(tb[IFLA_AF_SPEC], ifi->ifi_index);
 
 	print_string(PRINT_FP, NULL, "%s", "\n");
 	close_json_object();
diff --git a/bridge/vlan.c b/bridge/vlan.c
index 19a36b804069..bdce55ae4e14 100644
--- a/bridge/vlan.c
+++ b/bridge/vlan.c
@@ -252,10 +252,18 @@ static int filter_vlan_check(__u16 vid, __u16 flags)
 	return 1;
 }
 
-static void print_vlan_port(FILE *fp, int ifi_index)
+static void open_vlan_port(int ifi_index)
 {
-	print_string(PRINT_ANY, NULL, "%s",
+	open_json_object(NULL);
+	print_string(PRINT_ANY, "ifname", "%s",
 		     ll_index_to_name(ifi_index));
+	open_json_array(PRINT_JSON, "vlans");
+}
+
+static void close_vlan_port(void)
+{
+	close_json_array(PRINT_JSON, NULL);
+	close_json_object();
 }
 
 static void print_range(const char *name, __u16 start, __u16 id)
@@ -278,7 +286,7 @@ static void print_vlan_tunnel_info(FILE *fp, struct rtattr *tb, int ifindex)
 	__u32 last_tunid_start = 0;
 
 	if (!filter_vlan)
-		print_vlan_port(fp, ifindex);
+		open_vlan_port(ifindex);
 
 	open_json_array(PRINT_JSON, "tunnel");
 	for (i = RTA_DATA(list); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
@@ -323,18 +331,20 @@ static void print_vlan_tunnel_info(FILE *fp, struct rtattr *tb, int ifindex)
 			continue;
 
 		if (filter_vlan)
-			print_vlan_port(fp, ifindex);
+			open_vlan_port(ifindex);
 
 		open_json_object(NULL);
 		print_range("vlan", last_vid_start, tunnel_vid);
 		print_range("tunid", last_tunid_start, tunnel_id);
 		close_json_object();
 
-		if (!is_json_context())
-			fprintf(fp, "\n");
-
+		print_string(PRINT_FP, NULL, "%s", _SL_);
+		if (filter_vlan)
+			close_vlan_port();
 	}
-	close_json_array(PRINT_JSON, NULL);
+
+	if (!filter_vlan)
+		close_vlan_port();
 }
 
 static int print_vlan_tunnel(const struct sockaddr_nl *who,
@@ -421,8 +431,8 @@ static int print_vlan(const struct sockaddr_nl *who,
 		return 0;
 	}
 
-	print_vlan_port(fp, ifm->ifi_index);
-	print_vlan_info(fp, tb[IFLA_AF_SPEC]);
+	print_vlan_info(tb[IFLA_AF_SPEC], ifm->ifi_index);
+	print_string(PRINT_FP, NULL, "%s", _SL_);
 
 	fflush(fp);
 	return 0;
@@ -430,11 +440,16 @@ static int print_vlan(const struct sockaddr_nl *who,
 
 static void print_vlan_flags(__u16 flags)
 {
+	if (flags == 0)
+		return;
+
+	open_json_array(PRINT_JSON, "flags");
 	if (flags & BRIDGE_VLAN_INFO_PVID)
-		print_null(PRINT_ANY, "pvid", " %s", "PVID");
+		print_string(PRINT_ANY, NULL, " %s", "PVID");
 
 	if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
-		print_null(PRINT_ANY, "untagged", " %s", "untagged");
+		print_string(PRINT_ANY, NULL, " %s", "Egress Untagged");
+	close_json_array(PRINT_JSON, NULL);
 }
 
 static void print_one_vlan_stats(const struct bridge_vlan_xstats *vstats)
@@ -461,6 +476,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;
 
 	parse_rtattr(brtb, LINK_XSTATS_TYPE_MAX, RTA_DATA(attr),
@@ -471,13 +487,12 @@ static void print_vlan_stats_attr(struct rtattr *attr, int ifindex)
 	list = brtb[LINK_XSTATS_TYPE_BRIDGE];
 	rem = RTA_PAYLOAD(list);
 
-	open_json_object(NULL);
+	ifname = ll_index_to_name(ifindex);
+	open_json_object(ifname);
 
-	print_color_string(PRINT_ANY, COLOR_IFNAME,
-			   "dev", "%-16s",
-			   ll_index_to_name(ifindex));
+	print_color_string(PRINT_FP, COLOR_IFNAME,
+			   NULL, "%-16s", ifname);
 
-	open_json_array(PRINT_JSON, "xstats");
 	for (i = RTA_DATA(list); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
 		const struct bridge_vlan_xstats *vstats = RTA_DATA(i);
 
@@ -494,7 +509,6 @@ static void print_vlan_stats_attr(struct rtattr *attr, int ifindex)
 
 		print_one_vlan_stats(vstats);
 	}
-	close_json_array(PRINT_ANY, "\n");
 	close_json_object();
 
 }
@@ -623,16 +637,13 @@ static int vlan_show(int argc, char **argv)
 	return 0;
 }
 
-void print_vlan_info(FILE *fp, struct rtattr *tb)
+void print_vlan_info(struct rtattr *tb, int ifindex)
 {
 	struct rtattr *i, *list = tb;
 	int rem = RTA_PAYLOAD(list);
 	__u16 last_vid_start = 0;
 
-	if (!is_json_context())
-		fprintf(fp, "%s", _SL_);
-
-	open_json_array(PRINT_JSON, "vlan");
+	open_vlan_port(ifindex);
 
 	for (i = RTA_DATA(list); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
 		struct bridge_vlan_info *vinfo;
@@ -656,9 +667,9 @@ void print_vlan_info(FILE *fp, struct rtattr *tb)
 
 		print_vlan_flags(vinfo->flags);
 		close_json_object();
+		print_string(PRINT_FP, NULL, "%s", _SL_);
 	}
-
-	close_json_array(PRINT_ANY, "\n");
+	close_vlan_port();
 }
 
 int do_vlan(int argc, char **argv)
-- 
2.17.1

  parent reply	other threads:[~2018-09-06 20:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-06 15:30 [PATCH iproute2 1/3] bridge: minor change to mdb print Stephen Hemminger
2018-09-06 15:30 ` [PATCH iproute2 2/3] bridge: use print_json for some outputs Stephen Hemminger
2018-09-06 15:30 ` Stephen Hemminger [this message]
2018-09-10 11:52   ` [PATCH iproute2 3/3] bridge: fix vlan show formatting Tobias Jungel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180906153057.5379-3-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox