diff options
author | Petr Machata <petrm@nvidia.com> | 2022-05-09 15:59:54 +0200 |
---|---|---|
committer | David Ahern <dsahern@kernel.org> | 2022-05-12 11:07:46 -0600 |
commit | 72623b73c4eaceb0267f110e55c7bc1c342267b1 (patch) | |
tree | 736facd6fc335f0db0438167d9ec304995df0dfb | |
parent | ce41750fcc924c377ac3dda1df265e3b27d90a68 (diff) | |
download | iproute2-72623b73c4eaceb0267f110e55c7bc1c342267b1.tar.gz |
iplink: Fix formatting of MPLS stats
Currently, MPLS stats are formatted this way:
# ip -n ns0-DBZpxj8I link afstats dev veth01
3: veth01
mpls:
RX: bytes packets errors dropped noroute
0 0 0 0 0
TX: bytes packets errors dropped
216 2 0 0
Note how most numbers are not aligned properly under their column headers.
Fix by converting the code to use size_columns() to dynamically determine
the necessary width of individual columns, which also takes care of
formatting the table properly in case the counter values are high.
After the fix, the formatting looks as follows:
# ip -n ns0-Y1PyEc55 link afstats dev veth01
3: veth01
mpls:
RX: bytes packets errors dropped noroute
0 0 0 0 0
TX: bytes packets errors dropped
108 1 0 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 | 44 |
1 files changed, 32 insertions, 12 deletions
diff --git a/ip/iplink.c b/ip/iplink.c index 23eb6c6e2..b87d9bcd3 100644 --- a/ip/iplink.c +++ b/ip/iplink.c @@ -1521,6 +1521,13 @@ static void print_mpls_stats(FILE *fp, struct rtattr *attr) { struct rtattr *mrtb[MPLS_STATS_MAX+1]; struct mpls_link_stats *stats; + unsigned int cols[] = { + strlen("*X: bytes"), + strlen("packets"), + strlen("errors"), + strlen("dropped"), + strlen("noroute"), + }; parse_rtattr(mrtb, MPLS_STATS_MAX, RTA_DATA(attr), RTA_PAYLOAD(attr)); @@ -1528,22 +1535,35 @@ static void print_mpls_stats(FILE *fp, struct rtattr *attr) return; stats = RTA_DATA(mrtb[MPLS_STATS_LINK]); - fprintf(fp, " mpls:\n"); - fprintf(fp, " RX: bytes packets errors dropped noroute\n"); + + 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, " RX: %*s %*s %*s %*s %*s%s", + cols[0] - 4, "bytes", cols[1], "packets", + cols[2], "errors", cols[3], "dropped", + cols[4], "noroute", _SL_); fprintf(fp, " "); - print_num(fp, 10, stats->rx_bytes); - print_num(fp, 8, stats->rx_packets); - print_num(fp, 7, stats->rx_errors); - print_num(fp, 8, stats->rx_dropped); - print_num(fp, 7, stats->rx_noroute); + 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, " TX: bytes packets errors dropped\n"); + + fprintf(fp, " TX: %*s %*s %*s %*s%s", + cols[0] - 4, "bytes", cols[1], "packets", + cols[2], "errors", cols[3], "dropped", _SL_); fprintf(fp, " "); - print_num(fp, 10, stats->tx_bytes); - print_num(fp, 8, stats->tx_packets); - print_num(fp, 7, stats->tx_errors); - print_num(fp, 7, stats->tx_dropped); + 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, "\n"); } |