]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
iplink: Add JSON support to MPLS stats formatter
authorPetr Machata <petrm@nvidia.com>
Mon, 9 May 2022 13:59:57 +0000 (15:59 +0200)
committerDavid Ahern <dsahern@kernel.org>
Thu, 12 May 2022 17:08:13 +0000 (11:08 -0600)
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>
Signed-off-by: David Ahern <dsahern@kernel.org>
ip/iplink.c

index d666234335b62e56e06a9e2c7d3ed8d5925078d5..fbdf542ae92bf1b9a7cb60f6916347870709cb48 100644 (file)
@@ -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);
-
-       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);
+       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();
+
+               /* 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, "%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)