diff mbox series

[iproute2-next,04/10] iplink: Add JSON support to MPLS stats formatter

Message ID fc7e739575767e427053e191e6cd40868378d320.1652104101.git.petrm@nvidia.com (mailing list archive)
State Accepted
Delegated to: David Ahern
Headers show
Series ip stats: Support for xstats and afstats | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Petr Machata May 9, 2022, 1:59 p.m. UTC
MPLS stats currently do not support dumping in JSON format. Recognize when
JSON is requested and dump in an obvious manner:

 # ip -n ns0-2G8Ozd9z -j stats show dev veth01 group afstats | jq
 [
   {
     "ifindex": 3,
     "ifname": "veth01",
     "group": "afstats",
     "subgroup": "mpls",
     "mpls_stats": {
       "rx": {
         "bytes": 0,
         "packets": 0,
         "errors": 0,
         "dropped": 0,
         "noroute": 0
       },
       "tx": {
         "bytes": 216,
         "packets": 2,
         "errors": 0,
         "dropped": 0
       }
     }
   }
 ]

Signed-off-by: Petr Machata <petrm@nvidia.com>
Reviewed-by: Ido Schimmel <idosch@nvidia.com>
---
 ip/iplink.c | 69 ++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 44 insertions(+), 25 deletions(-)
diff mbox series

Patch

diff --git a/ip/iplink.c b/ip/iplink.c
index d6662343..fbdf542a 100644
--- a/ip/iplink.c
+++ b/ip/iplink.c
@@ -1528,33 +1528,52 @@  void print_mpls_link_stats(FILE *fp, const struct mpls_link_stats *stats,
 		strlen("noroute"),
 	};
 
-	size_columns(cols, ARRAY_SIZE(cols),
-		     stats->rx_bytes, stats->rx_packets, stats->rx_errors,
-		     stats->rx_dropped, stats->rx_noroute);
-	size_columns(cols, ARRAY_SIZE(cols),
-		     stats->tx_bytes, stats->tx_packets, stats->tx_errors,
-		     stats->tx_dropped, 0);
+	if (is_json_context()) {
+		/* RX stats */
+		open_json_object("rx");
+		print_u64(PRINT_JSON, "bytes", NULL, stats->rx_bytes);
+		print_u64(PRINT_JSON, "packets", NULL, stats->rx_packets);
+		print_u64(PRINT_JSON, "errors", NULL, stats->rx_errors);
+		print_u64(PRINT_JSON, "dropped", NULL, stats->rx_dropped);
+		print_u64(PRINT_JSON, "noroute", NULL, stats->rx_noroute);
+		close_json_object();
 
-	fprintf(fp, "%sRX: %*s %*s %*s %*s %*s%s", indent,
-		cols[0] - 4, "bytes", cols[1], "packets",
-		cols[2], "errors", cols[3], "dropped",
-		cols[4], "noroute", _SL_);
-	fprintf(fp, "%s", indent);
-	print_num(fp, cols[0], stats->rx_bytes);
-	print_num(fp, cols[1], stats->rx_packets);
-	print_num(fp, cols[2], stats->rx_errors);
-	print_num(fp, cols[3], stats->rx_dropped);
-	print_num(fp, cols[4], stats->rx_noroute);
-	fprintf(fp, "\n");
+		/* TX stats */
+		open_json_object("tx");
+		print_u64(PRINT_JSON, "bytes", NULL, stats->tx_bytes);
+		print_u64(PRINT_JSON, "packets", NULL, stats->tx_packets);
+		print_u64(PRINT_JSON, "errors", NULL, stats->tx_errors);
+		print_u64(PRINT_JSON, "dropped", NULL, stats->tx_dropped);
+		close_json_object();
+	} else {
+		size_columns(cols, ARRAY_SIZE(cols), stats->rx_bytes,
+			     stats->rx_packets, stats->rx_errors,
+			     stats->rx_dropped, stats->rx_noroute);
+		size_columns(cols, ARRAY_SIZE(cols), stats->tx_bytes,
+			     stats->tx_packets, stats->tx_errors,
+			     stats->tx_dropped, 0);
 
-	fprintf(fp, "%sTX: %*s %*s %*s %*s%s", indent,
-		cols[0] - 4, "bytes", cols[1], "packets",
-		cols[2], "errors", cols[3], "dropped", _SL_);
-	fprintf(fp, "%s", indent);
-	print_num(fp, cols[0], stats->tx_bytes);
-	print_num(fp, cols[1], stats->tx_packets);
-	print_num(fp, cols[2], stats->tx_errors);
-	print_num(fp, cols[3], stats->tx_dropped);
+		fprintf(fp, "%sRX: %*s %*s %*s %*s %*s%s", indent,
+			cols[0] - 4, "bytes", cols[1], "packets",
+			cols[2], "errors", cols[3], "dropped",
+			cols[4], "noroute", _SL_);
+		fprintf(fp, "%s", indent);
+		print_num(fp, cols[0], stats->rx_bytes);
+		print_num(fp, cols[1], stats->rx_packets);
+		print_num(fp, cols[2], stats->rx_errors);
+		print_num(fp, cols[3], stats->rx_dropped);
+		print_num(fp, cols[4], stats->rx_noroute);
+		fprintf(fp, "\n");
+
+		fprintf(fp, "%sTX: %*s %*s %*s %*s%s", indent,
+			cols[0] - 4, "bytes", cols[1], "packets",
+			cols[2], "errors", cols[3], "dropped", _SL_);
+		fprintf(fp, "%s", indent);
+		print_num(fp, cols[0], stats->tx_bytes);
+		print_num(fp, cols[1], stats->tx_packets);
+		print_num(fp, cols[2], stats->tx_errors);
+		print_num(fp, cols[3], stats->tx_dropped);
+	}
 }
 
 static void print_mpls_stats(FILE *fp, struct rtattr *attr)