Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Linux interface traffic 64bit statistics in userspace

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?

like image 517
user9250533 Avatar asked Feb 26 '18 11:02

user9250533


People also ask

What are the sources of interface statistics in Linux?

There are three main sources of interface statistics in Linux: standard interface statistics based on struct rtnl_link_stats64; driver-defined statistics available via ethtool. There are multiple interfaces to reach the standard statistics. Most commonly used is the ip command from iproute2:

How to monitor network traffic in Linux?

Method 5: Monitor network traffic using sar 1 sar is a short term abbreviation for S ystem A ctivity R eport. 2 It can be used for realtime monitoring of Linux system performance. 3 The sar command writes to standard output based on the values in the count and interval parameters More items...

How do I check network interface port speed in Linux?

Network interface port speed can only be verified in Linux using the ‘ethtool’ command. To check the speed of a particular network interface port, use the following command: # sudo ethtool eth0 |grep "Speed:" Speed: 10000Mb/s

How to check network interface details in Linux?

Few ways to check Network Interface details in Linux such as interface name, associated IP address, MAC address and interface speed etc. The ethtool command is used to query or control network driver and hardware settings. ip command is similar to ifconfig, which is used for assigning Static IP Address, Route & Default Gateway, etc.


1 Answers

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

like image 180
user9250533 Avatar answered Nov 15 '22 09:11

user9250533