get the IFLA_STATS attribute (rx/tx byte counters) for a specific interface

Jan-Philip gehrcke jgehrcke at googlemail.com
Thu Mar 3 10:13:11 EST 2011


On 02/25/2011 05:37 PM, Thomas Graf wrote:
> On Fri, Feb 25, 2011 at 05:17:50PM +0100, Jan-Philip gehrcke wrote:
>   
>> Hello,
>>
>> currently, I'm trying to create a bandwidth measurement program for my
>> Linux router in C. Obviously, for calculating a bandwidth, you need a
>> time and byte count difference. Due to performance reasons, I would like
>> to get the byte counters directly from the kernel, without reading from
>> /proc oder /sys. Therefore, I'm working myself through the 'ip' program,
>> gathering this data via rtnetlink using libnetlink. While searching the
>> web to understand more of this topic, I came across libnl which seems to
>> be a cleaner and more modern implementation. However, I'm a bit
>> overwhelmed by the documentation.
>>     
> You may want to have a look at:
>
> http://www.infradead.org/~tgr/bmon/
>
> It is a bandwidth measurement program I've created which is based on
> libnl.
>
>   
>> Assume I know the interface index of the interface I want to evaluate.
>> Then, what is the cleanest way to use libnl to receive the corresponding
>> IFLA_STATS attribute [1] containing the byte counters? I would
>> appreciate any guidance/suggestion that helps me orienting in libnl :-)
>>     
> A good reference would be:
>
> http://git.infradead.org/users/tgr/bmon.git/blob/HEAD:/src/in_netlink.c
>
> -Thomas
>   
Dear Thomas,

thanks for your immediate answer. I  ended up using the development
version of libnl to get to a working C program that reads out traffic
stats for a specific interface, just as I wanted to do. A small working
example is attached. However, I still have a performance-related question.

I definitely need a function that retrieves and returns the up-to-date
byte counter. In my attached example, this is get_rx_bytes(). My
motivation to use netlink API is to have a faster solution than reading
the byte counter from a file in sysfs. Now, in my benchmarks, this
function get_rx_bytes() is "only" ~2-fold as fast as some
read_int_from_file("/sys/class/net/eth0/statistics/rx_bytes") function.
I was hoping for let's say an order of magnitude of improvement.

Do you see an easy way to optimize my get_rx_bytes() function? Can I
outsource some initialization overhead? Do I retrieve too much data in
the first place via rtnl_link_alloc_cache()?

In other words: my goal is to minimize the time it takes to execute

    for (i=0; i<N; i++) {get_rx_bytes("eth0");}

How do I do this?

Could it be the case that libnl is already too "high-level" for such
optimizations? Do you expect libnetlink (used in iproute2/ip programs)
being "faster" by only retrieving the information I actually need?

Big thanks for any advice&suggestion.

Jan-Philip

-------------- next part --------------
A non-text attachment was scrubbed...
Name: libnl_read_link_rx_bytes_minimal.c
Type: text/x-csrc
Size: 1025 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/libnl/attachments/20110303/c73052c5/attachment.bin>


More information about the libnl mailing list