Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
11 netspeed_rx(const char *interface
)
14 static uint64_t rxbytes
;
15 extern const unsigned int interval
;
20 if (esnprintf(path
, sizeof(path
),
21 "/sys/class/net/%s/statistics/rx_bytes",
25 if (pscanf(path
, "%llu", &rxbytes
) != 1) {
28 if (oldrxbytes
== 0) {
32 return fmt_human_2((rxbytes
- oldrxbytes
) * 1000 / interval
);
36 netspeed_tx(const char *interface
)
39 static uint64_t txbytes
;
40 extern const unsigned int interval
;
45 if (esnprintf(path
, sizeof(path
),
46 "/sys/class/net/%s/statistics/tx_bytes",
50 if (pscanf(path
, "%llu", &txbytes
) != 1) {
53 if (oldtxbytes
== 0) {
57 return fmt_human_2((txbytes
- oldtxbytes
) * 1000 / interval
);
59 #elif defined(__OpenBSD__)
62 #include <sys/types.h>
63 #include <sys/socket.h>
67 netspeed_rx(const char *interface
)
69 struct ifaddrs
*ifal
, *ifa
;
72 static uint64_t rxbytes
;
73 extern const unsigned int interval
;
78 if (getifaddrs(&ifal
) == -1) {
79 warn("getifaddrs failed");
83 for (ifa
= ifal
; ifa
; ifa
= ifa
->ifa_next
) {
84 if (!strcmp(ifa
->ifa_name
, interface
) &&
85 (ifd
= (struct if_data
*)ifa
->ifa_data
)) {
86 rxbytes
+= ifd
->ifi_ibytes
, if_ok
= 1;
91 warn("reading 'if_data' failed");
94 if (oldrxbytes
== 0) {
98 return fmt_human_2((rxbytes
- oldrxbytes
) * 1000 / interval
);
102 netspeed_tx(const char *interface
)
104 struct ifaddrs
*ifal
, *ifa
;
107 static uint64_t txbytes
;
108 extern const unsigned int interval
;
111 oldtxbytes
= txbytes
;
113 if (getifaddrs(&ifal
) == -1) {
114 warn("getifaddrs failed");
118 for (ifa
= ifal
; ifa
; ifa
= ifa
->ifa_next
) {
119 if (!strcmp(ifa
->ifa_name
, interface
) &&
120 (ifd
= (struct if_data
*)ifa
->ifa_data
)) {
121 txbytes
+= ifd
->ifi_obytes
, if_ok
= 1;
126 warn("reading 'if_data' failed");
129 if (oldtxbytes
== 0) {
133 return fmt_human_2((txbytes
- oldtxbytes
) * 1000 / interval
);