From 2ed73b9a80d8291359d477caafab5defca113c58 Mon Sep 17 00:00:00 2001 From: Petr Machata Date: Mon, 9 May 2022 15:59:57 +0200 Subject: [PATCH] iplink: Add JSON support to MPLS stats formatter 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 Reviewed-by: Ido Schimmel Signed-off-by: David Ahern --- ip/iplink.c | 73 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/ip/iplink.c b/ip/iplink.c index d66623433..fbdf542ae 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); - - 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) -- 2.47.2