Getting Linux interface traffic 64bit statistics in userspace
Asked Answered
P

2

6

I would like to get the NIC driver 64bit statistics in userspace, in the simplest way possible. The man page of API function getifaddrs(), suggests a simple example program that does it using 32bit "struct rtnl_link_stats" (possible to see it here for example: http://man7.org/linux/man-pages/man3/getifaddrs.3.html).

The problem with this struct is obvious: the 32bit variables wrap-around (exceed 2^32) quickly (e.g. "rx_bytes").

In the same header file (linux/if_link.h) that contains struct rtnl_link_stats, there is a similar struct with 64bit variables: struct rtnl_link_stats64. I was sure that I'm very close to have the same statistics on 64bit variables, but unfortunately, I failed doing so.

There are functions like dev_get_stats(), but they are all written for kernel-space.

Programs like "ethtool -S" show these statistics in 64bit values, but I think they do some manipulation and not fill up struct rtnl_link_stats64 in a simple way.

Reading directly from /sys/class/net/ethX/statistics/rx_bytes (using fopen, fscanf...) is ok, but it doesn't perform as good as reading using API.

Is there a simple way in userspace to have struct rtnl_link_stats64 filled with the relevant statistics, that I miss?

Phonics answered 26/2, 2018 at 11:30 Comment(0)
P
2

Apparently, the best (performance wise) way I found is using netlink APIs from library libnl.

Here is a link to a simple example program that I found, that implements it: https://gist.github.com/beejeebus/cc4fb07472cf5a0afd41

Phonics answered 27/2, 2018 at 11:6 Comment(0)
S
0

I checked how Linux kernel gets stats in /proc/net/snmp /proc/net/snmp6 and /proc/net/netstat. https://github.com/torvalds/linux/blob/master/net/ipv4/proc.c#L394

All of these files allready have 64 bit counters and Linux wants that you use file API in proc or directly procfs API. Good luck :)

Supplant answered 3/9, 2024 at 8:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.