Skip to contentSkip to conversation

ip-link: add switch to show human readable output

PATCH Superseded Christian Hesse 2 participants 8 messages Oct 10, 2014 – Nov 3, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ip-link: add switch to show human readable output

Byte and packet count can increase to really big numbers. This adds a switch to show human readable output.

% ip -s link ls wl
4: wl: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000

    link/ether 00:de:ad:be:ee:ef brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast
    113570876  156975   0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns
    27290790   94313    0       0       0       0
% ip -s -h link ls wl
4: wl: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000
    link/ether 00:de:ad:be:ee:ef brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast
    122368888  169840   0       0       0       0
    116.7Mi    165.8Ki  0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns
    29087507   102309   0       0       0       0
    27.7Mi     99.9Ki   0       0       0       0
---
 include/utils.h       |   1 +
 ip/ip.c               |   5 ++
 ip/ipaddress.c        | 143 ++++++++++++++++++++++++++++++++++++++++++++++++--
 man/man8/ip-link.8.in |   1 +
 4 files changed, 146 insertions(+), 4 deletions(-)
include/utils.h +1 -0
index 704dc51..7bb19e9 100644
@@ -11,6 +11,7 @@
 #include "rtm_map.h"
 
 extern int preferred_family;
+extern int human_readable;
 extern int show_stats;
 extern int show_details;
 extern int show_raw;
ip/ip.c +5 -0
index 739b88d..6b352c8 100644
@@ -24,6 +24,7 @@
 #include "ip_common.h"
 
 int preferred_family = AF_UNSPEC;
+int human_readable = 0;
 int show_stats = 0;
 int show_details = 0;
 int resolve_hosts = 0;
@@ -47,6 +48,7 @@ static void usage(void)
 "                   tunnel | tuntap | maddr | mroute | mrule | monitor | xfrm |\n"
 "                   netns | l2tp | tcp_metrics | token | netconf }\n"
 "       OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
+"                    -h[uman-readable] |\n"
 "                    -f[amily] { inet | inet6 | ipx | dnet | bridge | link } |\n"
 "                    -4 | -6 | -I | -D | -B | -0 |\n"
 "                    -l[oops] { maximum-addr-flush-attempts } |\n"
@@ -212,6 +214,9 @@ int main(int argc, char **argv)
 			preferred_family = AF_DECnet;
 		} else if (strcmp(opt, "-B") == 0) {
 			preferred_family = AF_BRIDGE;
+		} else if (matches(opt, "-human") == 0 ||
+			   matches(opt, "-human-readable") == 0) {
+			++human_readable;
 		} else if (matches(opt, "-stats") == 0 ||
 			   matches(opt, "-statistics") == 0) {
 			++show_stats;
ip/ipaddress.c +139 -4

214 lines not on this page · raw message

man/man8/ip-link.8.in +1 -0
index 383917a..9c20dd0 100644
@@ -16,6 +16,7 @@ ip-link \- network device configuration
 .ti -8
 .IR OPTIONS " := { "
 \fB\-V\fR[\fIersion\fR] |
+\fB\-h\fR[\fIuman-readable\fR] |
 \fB\-s\fR[\fItatistics\fR] |
 \fB\-r\fR[\fIesolve\fR] |
 \fB\-f\fR[\fIamily\fR] {
Signature · 1 line
2.1.2
#1 lore raw
Quoted 19 lines · On Sat, 11 Oct 2014 00:27:36 +0200 Christian Hesse <[email protected]> wrote:

Byte and packet count can increase to really big numbers. This adds a switch to show human readable output.

% ip -s link ls wl
4: wl: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000

    link/ether 00:de:ad:be:ee:ef brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast
    113570876  156975   0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns
    27290790   94313    0       0       0       0
% ip -s -h link ls wl
4: wl: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000
    link/ether 00:de:ad:be:ee:ef brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast
    122368888  169840   0       0       0       0
    116.7Mi    165.8Ki  0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns
    29087507   102309   0       0       0       0
    27.7Mi     99.9Ki   0       0       0       0
Quoted 1 line

---

I like the idea as a concept but there are two issues:
1. The IEC suffix is a rarely used thing and is non-standard for communications where K = 1000 M = 1000000 etc. Please just use standard suffices

2. Don't double print the data, if the user asks for human format, only show the human format.

#2 lore raw

PATCH v3 1/1 ip-link: add switch to show human readable output

Byte and packet count can increase to really big numbers. This adds a switch to show human readable output.

% ip -s link ls en
3: en: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000

    link/ether 00:de:ad:be:ee:ef brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast
    48494310   141370   0       196     0       0
    TX: bytes  packets  errors  dropped carrier collsns
    153830639  180773   0       0       0       0
% ip -s -h link ls en
3: en: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 00:de:ad:be:ee:ef brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast
    46.3M      138.8K   0       198     0       0
    TX: bytes  packets  errors  dropped carrier collsns
    148.1M     177.7K   0       0       0       0
---
 include/utils.h       |   1 +
 ip/ip.c               |   5 +
 ip/ipaddress.c        | 279 ++++++++++++++++++++++++++++++++++++++------------
 man/man8/ip-link.8.in |   1 +
 4 files changed, 220 insertions(+), 66 deletions(-)
include/utils.h +1 -0
index 704dc51..7bb19e9 100644
@@ -11,6 +11,7 @@
 #include "rtm_map.h"
 
 extern int preferred_family;
+extern int human_readable;
 extern int show_stats;
 extern int show_details;
 extern int show_raw;
ip/ip.c +5 -0
index 739b88d..6b352c8 100644
@@ -24,6 +24,7 @@
 #include "ip_common.h"
 
 int preferred_family = AF_UNSPEC;
+int human_readable = 0;
 int show_stats = 0;
 int show_details = 0;
 int resolve_hosts = 0;
@@ -47,6 +48,7 @@ static void usage(void)
 "                   tunnel | tuntap | maddr | mroute | mrule | monitor | xfrm |\n"
 "                   netns | l2tp | tcp_metrics | token | netconf }\n"
 "       OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
+"                    -h[uman-readable] |\n"
 "                    -f[amily] { inet | inet6 | ipx | dnet | bridge | link } |\n"
 "                    -4 | -6 | -I | -D | -B | -0 |\n"
 "                    -l[oops] { maximum-addr-flush-attempts } |\n"
@@ -212,6 +214,9 @@ int main(int argc, char **argv)
 			preferred_family = AF_DECnet;
 		} else if (strcmp(opt, "-B") == 0) {
 			preferred_family = AF_BRIDGE;
+		} else if (matches(opt, "-human") == 0 ||
+			   matches(opt, "-human-readable") == 0) {
+			++human_readable;
 		} else if (matches(opt, "-stats") == 0 ||
 			   matches(opt, "-statistics") == 0) {
 			++show_stats;
ip/ipaddress.c +213 -66

320 lines not on this page · raw message

man/man8/ip-link.8.in +1 -0
index 464009d..b05c6d0 100644
@@ -16,6 +16,7 @@ ip-link \- network device configuration
 .ti -8
 .IR OPTIONS " := { "
 \fB\-V\fR[\fIersion\fR] |
+\fB\-h\fR[\fIuman-readable\fR] |
 \fB\-s\fR[\fItatistics\fR] |
 \fB\-r\fR[\fIesolve\fR] |
 \fB\-f\fR[\fIamily\fR] {
Signature · 1 line
2.1.3
#3 lore raw

Stephen Hemminger <[email protected]> on Wed, 2014/10/29 22:47:

Quoted 1 line

I like the idea as a concept

Great! ;)

Quoted 4 lines

but there are two issues:
1. The IEC suffix is a rarely used thing and is non-standard for communications where K = 1000 M = 1000000 etc. Please just use standard suffices

Removed the suffix in patch v3. Not sure if it is correct, though. I do use a base of 2, so K = 1024, M = 1048576, ...

This is what ifconfig behaves as well. Output should give the exact same numbers (except the suffix):

# ip -s -h link list en
3: en: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP
mode DEFAULT group default qlen 1000
    link/ether 00:de:ad:be:ee:ef brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast
    26.3M      59.2K    0       68      0       0
    TX: bytes  packets  errors  dropped carrier collsns
    37.0M      67.6K    0       0       0       0
# ifconfig en 
en: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet XX.XX.XX.XX  netmask 255.255.255.0  broadcast XX.XX.XX.255
        ether 00:de:ad:be:ee:ef  txqueuelen 1000  (Ethernet)
        RX packets 60677  bytes 27671339 (26.3 MiB)
        RX errors 0  dropped 68  overruns 0  frame 0
        TX packets 69350  bytes 38874609 (37.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
Quoted 2 lines

2. Don't double print the data, if the user asks for human format, only show the human format.

Fixed.

Hopefully I got the line breaks right. How to check print_link_stats32()? Even von i686 ip calls print_link_stats64() on my systems.

I did some minor changes to the error stats column alignment. Is that ok? See the difference of before and after:

# ip -s -s link list en    # before
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP
mode DEFAULT group default qlen 1000
    link/ether 00:de:ad:be:ee:ef brd ff:ff:ff:ff:ff:ff 
    RX: bytes  packets  errors  dropped overrun mcast   
    144544239  100441   0       0       0       1      
    RX errors: length  crc     frame   fifo    missed
               0        0       0       0       0      
    TX: bytes  packets  errors  dropped carrier collsns 
    808343     9630     0       0       0       0      
    TX errors: aborted fifo    window  heartbeat transns
               0        0       0       0        2      
# ip -s -s link list en    # after
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP
mode DEFAULT group default qlen 1000
    link/ether 00:de:ad:be:ee:ef brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast
    87432187   60642    0       0       0       1
    RX errors: length   crc     frame   fifo    missed
               0        0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns 
    450437     5793     0       0       0       0      
    TX errors: aborted  fifo   window heartbeat transns
               0        0       0       0       2      
Signature · 2 lines
Best regards,
Christian Hesse
#4 lore raw
Quoted 5 lines · On Fri, 31 Oct 2014 11:17:53 +0100 Christian Hesse <[email protected]> wrote:

Stephen Hemminger <[email protected]> on Wed, 2014/10/29 22:47:

Quoted 1 line

I like the idea as a concept

Great! ;)

Quoted 7 lines
Quoted 4 lines

but there are two issues:
1. The IEC suffix is a rarely used thing and is non-standard for communications where K = 1000 M = 1000000 etc. Please just use standard suffices

Removed the suffix in patch v3. Not sure if it is correct, though. I do use a base of 2, so K = 1024, M = 1048576, ...

No must be 1000 for communications (SI)

http://en.wikipedia.org/wiki/Binary_prefix

#5 lore raw

PATCH v4 1/1 ip-link: add switch to show human readable output

Byte and packet count can increase to really big numbers. This adds a switch to show human readable output.

4: wl: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000

    link/ether 00:de:ad:be:ee:ef brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast
    1523846973 3969051  0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns
    8710088361 6077735  0       0       0       0

4: wl: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000
link/ether 00:de:ad:be:ee:ef brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
1.5G 3.9M 0 0 0 0
TX: bytes packets errors dropped carrier collsns
8.7G 6.0M 0 0 0 0

---
 include/utils.h       |   1 +
 ip/ip.c               |   5 +
 ip/ipaddress.c        | 286 ++++++++++++++++++++++++++++++++++++++------------
 man/man8/ip-link.8.in |   7 ++
 4 files changed, 233 insertions(+), 66 deletions(-)
include/utils.h +1 -0
index 704dc51..7bb19e9 100644
@@ -11,6 +11,7 @@
 #include "rtm_map.h"
 
 extern int preferred_family;
+extern int human_readable;
 extern int show_stats;
 extern int show_details;
 extern int show_raw;
ip/ip.c +5 -0
index 739b88d..6b352c8 100644
@@ -24,6 +24,7 @@
 #include "ip_common.h"
 
 int preferred_family = AF_UNSPEC;
+int human_readable = 0;
 int show_stats = 0;
 int show_details = 0;
 int resolve_hosts = 0;
@@ -47,6 +48,7 @@ static void usage(void)
 "                   tunnel | tuntap | maddr | mroute | mrule | monitor | xfrm |\n"
 "                   netns | l2tp | tcp_metrics | token | netconf }\n"
 "       OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
+"                    -h[uman-readable] |\n"
 "                    -f[amily] { inet | inet6 | ipx | dnet | bridge | link } |\n"
 "                    -4 | -6 | -I | -D | -B | -0 |\n"
 "                    -l[oops] { maximum-addr-flush-attempts } |\n"
@@ -212,6 +214,9 @@ int main(int argc, char **argv)
 			preferred_family = AF_DECnet;
 		} else if (strcmp(opt, "-B") == 0) {
 			preferred_family = AF_BRIDGE;
+		} else if (matches(opt, "-human") == 0 ||
+			   matches(opt, "-human-readable") == 0) {
+			++human_readable;
 		} else if (matches(opt, "-stats") == 0 ||
 			   matches(opt, "-statistics") == 0) {
 			++show_stats;
ip/ipaddress.c +220 -66

327 lines not on this page · raw message

man/man8/ip-link.8.in +7 -0
index 464009d..9747c4c 100644
@@ -16,6 +16,7 @@ ip-link \- network device configuration
 .ti -8
 .IR OPTIONS " := { "
 \fB\-V\fR[\fIersion\fR] |
+\fB\-h\fR[\fIuman-readable\fR] |
 \fB\-s\fR[\fItatistics\fR] |
 \fB\-r\fR[\fIesolve\fR] |
 \fB\-f\fR[\fIamily\fR] {
@@ -660,6 +661,12 @@ specifies what group of devices to show.
 .B up
 only display running interfaces.
 
+.SH "NOTES"
+Human readable values are calculated with SI prefixes, so with a decimal
+base, not binary. (This is unlike
+.BR ifconfig (8)
+, with uses binary prefix.) 1,000 bytes are 1 kB, 1,000 kB are 1 MB, ...
+
 .SH "EXAMPLES"
 .PP
 ip link show
Signature · 1 line
2.1.3
#6 lore raw
Quoted 9 lines · On Fri, 31 Oct 2014 22:33:13 +0100 Christian Hesse <[email protected]> wrote:

Byte and packet count can increase to really big numbers. This adds a switch to show human readable output.

4: wl: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000

    link/ether 00:de:ad:be:ee:ef brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast
    1523846973 3969051  0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns
    8710088361 6077735  0       0       0       0
Quoted 6 lines

4: wl: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000
link/ether 00:de:ad:be:ee:ef brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
1.5G 3.9M 0 0 0 0
TX: bytes packets errors dropped carrier collsns
8.7G 6.0M 0 0 0 0

Applied, then I did a code cleanup and added -iec as a option (similar to tc).

#7 lore raw

Stephen Hemminger <[email protected]> on Sun, 2014/11/02 12:51:

Quoted 19 lines
Quoted 6 lines · On Fri, 31 Oct 2014 22:33:13 +0100 Christian Hesse <[email protected]> wrote:

Byte and packet count can increase to really big numbers. This adds a switch to show human readable output.

4: wl: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000 link/ether 00:de:ad:be:ee:ef brd ff:ff:ff:ff:ff:ff RX: bytes packets errors dropped overrun mcast

Quoted 9 lines
    1523846973 3969051  0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns
    8710088361 6077735  0       0       0       0
4: wl: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode
DORMANT group default qlen 1000 link/ether 00:de:ad:be:ee:ef brd
ff:ff:ff:ff:ff:ff RX: bytes  packets  errors  dropped overrun mcast
    1.5G       3.9M     0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns
    8.7G       6.0M     0       0       0       0
Quoted 2 lines

Applied, then I did a code cleanup and added -iec as a option (similar to tc).

I would have come up with something similar later. ;) Thanks a lot! I am perfectly happy now and can live without ifconfig in the future.

Signature · 2 lines
Best regards,
Christian Hesse