diff options
author | Petr Machata <petrm@nvidia.com> | 2022-05-09 15:59:57 +0200 |
---|---|---|
committer | David Ahern <dsahern@kernel.org> | 2022-05-12 11:08:13 -0600 |
commit | 2ed73b9a80d8291359d477caafab5defca113c58 (patch) | |
tree | 34e759d3300a7baa7ff0a9b7004c99cca9c9e035 | |
parent | 5ed8fd9d514422771d283607fac523d8baef1179 (diff) | |
download | iproute2-2ed73b9a80d8291359d477caafab5defca113c58.tar.gz |
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@nvidia.com>
Reviewed-by: Ido Schimmel <idosch@nvidia.com>
Signed-off-by: David Ahern <dsahern@kernel.org>
-rw-r--r-- | ip/iplink.c | 73 |
1 files changed, 46 insertions, 27 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); - - 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) |