[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <fc7e739575767e427053e191e6cd40868378d320.1652104101.git.petrm@nvidia.com>
Date: Mon, 9 May 2022 15:59:57 +0200
From: Petr Machata <petrm@...dia.com>
To: <netdev@...r.kernel.org>
CC: David Ahern <dsahern@...il.com>, Ido Schimmel <idosch@...dia.com>,
"Petr Machata" <petrm@...dia.com>
Subject: [PATCH iproute2-next 04/10] 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 <petrm@...dia.com>
Reviewed-by: Ido Schimmel <idosch@...dia.com>
---
ip/iplink.c | 69 ++++++++++++++++++++++++++++++++++-------------------
1 file changed, 44 insertions(+), 25 deletions(-)
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)
--
2.31.1
Powered by blists - more mailing lists