netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH iproute2-next 0/2] more JSON support
@ 2018-03-28  1:07 Stephen Hemminger
  2018-03-28  1:07 ` [PATCH iproute2-next 1/2] ip/ila: support json and color Stephen Hemminger
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Stephen Hemminger @ 2018-03-28  1:07 UTC (permalink / raw)
  To: netdev; +Cc: Stephen Hemminger

From: Stephen Hemminger <sthemmin@microsoft.com>

Add JSON to ILA and L2TP display

Stephen Hemminger (2):
  ip/ila: support json and color
  ip/l2tp: add JSON support

 ip/ipila.c  |  76 +++++++++++++++---------------
 ip/ipl2tp.c | 152 ++++++++++++++++++++++++++++++++++++++++--------------------
 2 files changed, 140 insertions(+), 88 deletions(-)

-- 
2.16.2

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

* [PATCH iproute2-next 1/2] ip/ila: support json and color
  2018-03-28  1:07 [PATCH iproute2-next 0/2] more JSON support Stephen Hemminger
@ 2018-03-28  1:07 ` Stephen Hemminger
  2018-03-28  1:07 ` [PATCH iproute2-next 2/2] ip/l2tp: add JSON support Stephen Hemminger
  2018-03-29  3:37 ` [PATCH iproute2-next 0/2] more " David Ahern
  2 siblings, 0 replies; 4+ messages in thread
From: Stephen Hemminger @ 2018-03-28  1:07 UTC (permalink / raw)
  To: netdev; +Cc: Stephen Hemminger, Stephen Hemminger

From: Stephen Hemminger <sthemmin@microsoft.com>

Use json print to enhance ila output.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 ip/ipila.c | 76 ++++++++++++++++++++++++++++++--------------------------------
 1 file changed, 37 insertions(+), 39 deletions(-)

diff --git a/ip/ipila.c b/ip/ipila.c
index 9a324296ffd6..370385c0c375 100644
--- a/ip/ipila.c
+++ b/ip/ipila.c
@@ -23,6 +23,7 @@
 #include "utils.h"
 #include "ip_common.h"
 #include "ila_common.h"
+#include "json_print.h"
 
 static void usage(void)
 {
@@ -47,9 +48,7 @@ static int genl_family = -1;
 #define ILA_RTA(g) ((struct rtattr *)(((char *)(g)) +	\
 	NLMSG_ALIGN(sizeof(struct genlmsghdr))))
 
-#define ADDR_BUF_SIZE sizeof("xxxx:xxxx:xxxx:xxxx")
-
-static int print_addr64(__u64 addr, char *buff, size_t len)
+static void print_addr64(__u64 addr, char *buff, size_t len)
 {
 	__u16 *words = (__u16 *)&addr;
 	__u16 v;
@@ -64,38 +63,27 @@ static int print_addr64(__u64 addr, char *buff, size_t len)
 			sep = "";
 
 		ret = snprintf(&buff[written], len - written, "%x%s", v, sep);
-		if (ret < 0)
-			return ret;
-
 		written += ret;
 	}
-
-	return written;
 }
 
-static void print_ila_locid(FILE *fp, int attr, struct rtattr *tb[], int space)
+static void print_ila_locid(const char *tag, int attr, struct rtattr *tb[])
 {
 	char abuf[256];
-	size_t blen;
-	int i;
 
-	if (tb[attr]) {
-		blen = print_addr64(rta_getattr_u64(tb[attr]),
-				    abuf, sizeof(abuf));
-		fprintf(fp, "%s", abuf);
-	} else {
-		fprintf(fp, "-");
-		blen = 1;
-	}
+	if (tb[attr])
+		print_addr64(rta_getattr_u64(tb[attr]),
+			     abuf, sizeof(abuf));
+	else
+		snprintf(abuf, sizeof(abuf), "-");
 
-	for (i = 0; i < space - blen; i++)
-		fprintf(fp, " ");
+	/* 20 = sizeof("xxxx:xxxx:xxxx:xxxx") */
+	print_string(PRINT_ANY, tag, "%-20s", abuf);
 }
 
 static int print_ila_mapping(const struct sockaddr_nl *who,
 			     struct nlmsghdr *n, void *arg)
 {
-	FILE *fp = (FILE *)arg;
 	struct genlmsghdr *ghdr;
 	struct rtattr *tb[ILA_ATTR_MAX + 1];
 	int len = n->nlmsg_len;
@@ -110,31 +98,38 @@ static int print_ila_mapping(const struct sockaddr_nl *who,
 	ghdr = NLMSG_DATA(n);
 	parse_rtattr(tb, ILA_ATTR_MAX, (void *) ghdr + GENL_HDRLEN, len);
 
-	print_ila_locid(fp, ILA_ATTR_LOCATOR_MATCH, tb, ADDR_BUF_SIZE);
-	print_ila_locid(fp, ILA_ATTR_LOCATOR, tb, ADDR_BUF_SIZE);
+	open_json_object(NULL);
+	print_ila_locid("locator_match", ILA_ATTR_LOCATOR_MATCH, tb);
+	print_ila_locid("locator", ILA_ATTR_LOCATOR, tb);
 
-	if (tb[ILA_ATTR_IFINDEX])
-		fprintf(fp, "%-16s",
-			ll_index_to_name(rta_getattr_u32(
-						tb[ILA_ATTR_IFINDEX])));
-	else
-		fprintf(fp, "%-10s ", "-");
+	if (tb[ILA_ATTR_IFINDEX]) {
+		__u32 ifindex
+			= rta_getattr_u32(tb[ILA_ATTR_IFINDEX]);
 
-	if (tb[ILA_ATTR_CSUM_MODE])
-		fprintf(fp, "%s",
-			ila_csum_mode2name(rta_getattr_u8(
-						tb[ILA_ATTR_CSUM_MODE])));
-	else
-		fprintf(fp, "%-10s ", "-");
+		print_color_string(PRINT_ANY, COLOR_IFNAME,
+				   "interface", "%-16s",
+				   ll_index_to_name(ifindex));
+	} else {
+		print_string(PRINT_FP, NULL, "%-10s ", "-");
+	}
+
+	if (tb[ILA_ATTR_CSUM_MODE]) {
+		__u8 csum = rta_getattr_u8(tb[ILA_ATTR_CSUM_MODE]);
+
+		print_string(PRINT_ANY, "csum_mode", "%s",
+			     ila_csum_mode2name(csum));
+	} else
+		print_string(PRINT_FP, NULL, "%-10s ", "-");
 
 	if (tb[ILA_ATTR_IDENT_TYPE])
-		fprintf(fp, "%s",
+		print_string(PRINT_ANY, "ident_type", "%s",
 			ila_ident_type2name(rta_getattr_u8(
 						tb[ILA_ATTR_IDENT_TYPE])));
 	else
-		fprintf(fp, "-");
+		print_string(PRINT_FP, NULL, "%s", "-");
 
-	fprintf(fp, "\n");
+	print_string(PRINT_FP, NULL, "%s", _SL_);
+	close_json_object();
 
 	return 0;
 }
@@ -156,10 +151,13 @@ static int do_list(int argc, char **argv)
 		exit(1);
 	}
 
+	new_json_obj(json);
 	if (rtnl_dump_filter(&genl_rth, print_ila_mapping, stdout) < 0) {
 		fprintf(stderr, "Dump terminated\n");
 		return 1;
 	}
+	delete_json_obj();
+	fflush(stdout);
 
 	return 0;
 }
-- 
2.16.2

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

* [PATCH iproute2-next 2/2] ip/l2tp: add JSON support
  2018-03-28  1:07 [PATCH iproute2-next 0/2] more JSON support Stephen Hemminger
  2018-03-28  1:07 ` [PATCH iproute2-next 1/2] ip/ila: support json and color Stephen Hemminger
@ 2018-03-28  1:07 ` Stephen Hemminger
  2018-03-29  3:37 ` [PATCH iproute2-next 0/2] more " David Ahern
  2 siblings, 0 replies; 4+ messages in thread
From: Stephen Hemminger @ 2018-03-28  1:07 UTC (permalink / raw)
  To: netdev; +Cc: Stephen Hemminger, Stephen Hemminger

From: Stephen Hemminger <sthemmin@microsoft.com>

Convert ip l2tp to use JSON output routines.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 ip/ipl2tp.c | 152 ++++++++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 103 insertions(+), 49 deletions(-)

diff --git a/ip/ipl2tp.c b/ip/ipl2tp.c
index 8aaee747e294..750f912aa96a 100644
--- a/ip/ipl2tp.c
+++ b/ip/ipl2tp.c
@@ -204,15 +204,22 @@ static int delete_session(struct l2tp_parm *p)
 	return 0;
 }
 
-static void print_cookie(char *name, const uint8_t *cookie, int len)
+static void print_cookie(const char *name, const char *fmt,
+			 const uint8_t *cookie, int len)
 {
-	printf("  %s %02x%02x%02x%02x", name,
-	       cookie[0], cookie[1],
-	       cookie[2], cookie[3]);
+	char abuf[32];
+	size_t n;
+
+	n = snprintf(abuf, sizeof(abuf),
+		     "%02x%02x%02x%02x",
+		     cookie[0], cookie[1], cookie[2], cookie[3]);
 	if (len == 8)
-		printf("%02x%02x%02x%02x",
-		       cookie[4], cookie[5],
-		       cookie[6], cookie[7]);
+		snprintf(abuf + n, sizeof(abuf) - n,
+			 "%02x%02x%02x%02x",
+			 cookie[4], cookie[5],
+			 cookie[6], cookie[7]);
+
+	print_string(PRINT_ANY, name, fmt, abuf);
 }
 
 static void print_tunnel(const struct l2tp_data *data)
@@ -220,74 +227,115 @@ static void print_tunnel(const struct l2tp_data *data)
 	const struct l2tp_parm *p = &data->config;
 	char buf[INET6_ADDRSTRLEN];
 
-	printf("Tunnel %u, encap %s\n",
-	       p->tunnel_id,
-	       p->encap == L2TP_ENCAPTYPE_UDP ? "UDP" :
-	       p->encap == L2TP_ENCAPTYPE_IP ? "IP" : "??");
-	printf("  From %s ",
-	       inet_ntop(p->local_ip.family, p->local_ip.data,
-			 buf, sizeof(buf)));
-	printf("to %s\n",
-	       inet_ntop(p->peer_ip.family, p->peer_ip.data,
-			 buf, sizeof(buf)));
-	printf("  Peer tunnel %u\n",
-	       p->peer_tunnel_id);
+	open_json_object(NULL);
+	print_uint(PRINT_ANY, "tunnel_id", "Tunnel %u,", p->tunnel_id);
+	print_string(PRINT_ANY, "encap", " encap %s",
+		     p->encap == L2TP_ENCAPTYPE_UDP ? "UDP" :
+		     p->encap == L2TP_ENCAPTYPE_IP ? "IP" : "??");
+	print_string(PRINT_FP, NULL, "%s", _SL_);
+
+	print_string(PRINT_ANY, "local", "  From %s ",
+		     inet_ntop(p->local_ip.family, p->local_ip.data,
+			       buf, sizeof(buf)));
+	print_string(PRINT_ANY, "peer", "to %s",
+		     inet_ntop(p->peer_ip.family, p->peer_ip.data,
+			       buf, sizeof(buf)));
+	print_string(PRINT_FP, NULL, "%s", _SL_);
+
+	print_uint(PRINT_ANY, "peer_tunnel", "  Peer tunnel %u",
+		   p->peer_tunnel_id);
+	print_string(PRINT_FP, NULL, "%s", _SL_);
 
 	if (p->encap == L2TP_ENCAPTYPE_UDP) {
-		printf("  UDP source / dest ports: %hu/%hu\n",
-		       p->local_udp_port, p->peer_udp_port);
+		print_string(PRINT_FP, NULL,
+			     "  UDP source / dest ports:", NULL);
+
+		print_uint(PRINT_ANY, "local_port", " %hu",
+			   p->local_udp_port);
+		print_uint(PRINT_ANY, "peer_port", "/%hu",
+			   p->peer_udp_port);
+		print_string(PRINT_FP, NULL, "%s", _SL_);
 
 		switch (p->local_ip.family) {
 		case AF_INET:
-			printf("  UDP checksum: %s\n",
-			       p->udp_csum ? "enabled" : "disabled");
+			print_bool(PRINT_JSON, "checksum",
+				   NULL, p->udp_csum);
+			print_string(PRINT_FP, NULL,
+				     "  UDP checksum: %s\n",
+				     p->udp_csum ? "enabled" : "disabled");
 			break;
 		case AF_INET6:
-			printf("  UDP checksum: %s%s%s%s\n",
-			       p->udp6_csum_tx && p->udp6_csum_rx
-			       ? "enabled" : "",
-			       p->udp6_csum_tx && !p->udp6_csum_rx
-			       ? "tx" : "",
-			       !p->udp6_csum_tx && p->udp6_csum_rx
-			       ? "rx" : "",
-			       !p->udp6_csum_tx && !p->udp6_csum_rx
-			       ? "disabled" : "");
+			if (is_json_context()) {
+				print_bool(PRINT_JSON, "checksum_tx",
+					   NULL, p->udp6_csum_tx);
+
+				print_bool(PRINT_JSON, "checksum_rx",
+					   NULL, p->udp6_csum_tx);
+			} else {
+				printf("  UDP checksum: %s%s%s%s\n",
+				       p->udp6_csum_tx && p->udp6_csum_rx
+				       ? "enabled" : "",
+				       p->udp6_csum_tx && !p->udp6_csum_rx
+				       ? "tx" : "",
+				       !p->udp6_csum_tx && p->udp6_csum_rx
+				       ? "rx" : "",
+				       !p->udp6_csum_tx && !p->udp6_csum_rx
+				       ? "disabled" : "");
+			}
 			break;
 		}
 	}
+	close_json_object();
 }
 
 static void print_session(struct l2tp_data *data)
 {
 	struct l2tp_parm *p = &data->config;
 
-	printf("Session %u in tunnel %u\n",
-	       p->session_id, p->tunnel_id);
-	printf("  Peer session %u, tunnel %u\n",
-	       p->peer_session_id, p->peer_tunnel_id);
+	open_json_object(NULL);
 
-	if (p->ifname != NULL)
-		printf("  interface name: %s\n", p->ifname);
+	print_uint(PRINT_ANY, "session_id", "Session %u", p->session_id);
+	print_uint(PRINT_ANY, "tunnel_id",  " in tunnel %u", p->tunnel_id);
+	print_string(PRINT_FP, NULL, "%s", _SL_);
+
+	print_uint(PRINT_ANY, "peer_session_id",
+		     "  Peer session %u,", p->peer_session_id);
+	print_uint(PRINT_ANY, "peer_tunnel_id",
+		     " tunnel %u",  p->peer_tunnel_id);
+	print_string(PRINT_FP, NULL, "%s", _SL_);
+
+	if (p->ifname != NULL) {
+		print_color_string(PRINT_ANY, COLOR_IFNAME,
+				   "interface", "  interface name: %s" , p->ifname);
+		print_string(PRINT_FP, NULL, "%s", _SL_);
+	}
+
+	print_uint(PRINT_ANY, "offset", "  offset %u,", p->offset);
+	print_uint(PRINT_ANY, "peer_offset", " peer offset %u\n", p->peer_offset);
 
-	printf("  offset %u, peer offset %u\n",
-	       p->offset, p->peer_offset);
 	if (p->cookie_len > 0)
-		print_cookie("cookie", p->cookie, p->cookie_len);
+		print_cookie("cookie", "cookie",
+			     p->cookie, p->cookie_len);
 	if (p->peer_cookie_len > 0)
-		print_cookie("peer cookie", p->peer_cookie, p->peer_cookie_len);
+		print_cookie("peer_cookie", "peer cookie",
+			     p->peer_cookie, p->peer_cookie_len);
 
 	if (p->reorder_timeout != 0)
-		printf("  reorder timeout: %u\n", p->reorder_timeout);
-	else
-		printf("\n");
+		print_uint(PRINT_ANY, "reorder_timeout",
+			   "  reorder timeout: %u", p->reorder_timeout);
+
+
 	if (p->send_seq || p->recv_seq) {
-		printf("  sequence numbering:");
+		print_string(PRINT_FP, NULL, "%s  sequence numbering:", _SL_);
+
 		if (p->send_seq)
-			printf(" send");
+			print_null(PRINT_ANY, "send_seq", " send", NULL);
 		if (p->recv_seq)
-			printf(" recv");
-		printf("\n");
+			print_null(PRINT_ANY, "recv_seq", " recv", NULL);
+
 	}
+	print_string(PRINT_FP, NULL, "\n", NULL);
+	close_json_object();
 }
 
 static int get_response(struct nlmsghdr *n, void *arg)
@@ -435,10 +483,13 @@ static int get_session(struct l2tp_data *p)
 	if (rtnl_send(&genl_rth, &req, req.n.nlmsg_len) < 0)
 		return -2;
 
+	new_json_obj(json);
 	if (rtnl_dump_filter(&genl_rth, session_nlmsg, p) < 0) {
 		fprintf(stderr, "Dump terminated\n");
 		exit(1);
 	}
+	delete_json_obj();
+	fflush(stdout);
 
 	return 0;
 }
@@ -468,10 +519,13 @@ static int get_tunnel(struct l2tp_data *p)
 	if (rtnl_send(&genl_rth, &req, req.n.nlmsg_len) < 0)
 		return -2;
 
+	new_json_obj(json);
 	if (rtnl_dump_filter(&genl_rth, tunnel_nlmsg, p) < 0) {
 		fprintf(stderr, "Dump terminated\n");
 		exit(1);
 	}
+	delete_json_obj();
+	fflush(stdout);
 
 	return 0;
 }
-- 
2.16.2

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

* Re: [PATCH iproute2-next 0/2] more JSON support
  2018-03-28  1:07 [PATCH iproute2-next 0/2] more JSON support Stephen Hemminger
  2018-03-28  1:07 ` [PATCH iproute2-next 1/2] ip/ila: support json and color Stephen Hemminger
  2018-03-28  1:07 ` [PATCH iproute2-next 2/2] ip/l2tp: add JSON support Stephen Hemminger
@ 2018-03-29  3:37 ` David Ahern
  2 siblings, 0 replies; 4+ messages in thread
From: David Ahern @ 2018-03-29  3:37 UTC (permalink / raw)
  To: Stephen Hemminger, netdev; +Cc: Stephen Hemminger

On 3/27/18 7:07 PM, Stephen Hemminger wrote:
> From: Stephen Hemminger <sthemmin@microsoft.com>
> 
> Add JSON to ILA and L2TP display
> 
> Stephen Hemminger (2):
>   ip/ila: support json and color
>   ip/l2tp: add JSON support
> 
>  ip/ipila.c  |  76 +++++++++++++++---------------
>  ip/ipl2tp.c | 152 ++++++++++++++++++++++++++++++++++++++++--------------------
>  2 files changed, 140 insertions(+), 88 deletions(-)
> 

applied to iproute2-next. Thanks,

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

end of thread, other threads:[~2018-03-29  3:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-03-28  1:07 [PATCH iproute2-next 0/2] more JSON support Stephen Hemminger
2018-03-28  1:07 ` [PATCH iproute2-next 1/2] ip/ila: support json and color Stephen Hemminger
2018-03-28  1:07 ` [PATCH iproute2-next 2/2] ip/l2tp: add JSON support Stephen Hemminger
2018-03-29  3:37 ` [PATCH iproute2-next 0/2] more " 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).