]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
iplink: Fix formatting of MPLS stats
authorPetr Machata <petrm@nvidia.com>
Mon, 9 May 2022 13:59:54 +0000 (15:59 +0200)
committerDavid Ahern <dsahern@kernel.org>
Thu, 12 May 2022 17:07:46 +0000 (11:07 -0600)
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>
ip/iplink.c

index 23eb6c6e2bfe755e1d0a9700039e523536f94928..b87d9bcd387ea82073587d9a7deccb2339f34dec 100644 (file)
@@ -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");
 }