From: Petr Machata Date: Mon, 9 May 2022 13:59:54 +0000 (+0200) Subject: iplink: Fix formatting of MPLS stats X-Git-Tag: v5.19.0~24^2~1^2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=72623b73c4eaceb0267f110e55c7bc1c342267b1;p=thirdparty%2Fiproute2.git 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 Reviewed-by: Ido Schimmel Signed-off-by: David Ahern --- 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"); }