thom311 / libnl

Netlink Library Suite
GNU Lesser General Public License v2.1
416 stars 305 forks source link

rtnl_link_get_stat(link, RTNL_LINK_RX_PACKETS) returns zero in libnl-3.5.0 #262

Open acooks opened 3 years ago

acooks commented 3 years ago

I think version 3.5.0 of libnl has a regression in received packet counts. It looks like rtnl_link_get_stat(link, RTNL_LINK_RX_PACKETS); returns zero.

System/version info:

[acooks@t460s jittertrap]$ uname -a
Linux t460s.rationali.st 5.8.9-200.fc32.x86_64 #1 SMP Mon Sep 14 18:28:45 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

[acooks@t460s jittertrap]$ dnf info libnl3
Last metadata expiration check: 0:01:24 ago on Tue 29 Sep 2020 08:49:16 PM AEST.
Installed Packages
Name         : libnl3
Version      : 3.5.0
Release      : 2.fc32
Architecture : x86_64
Size         : 1.0 M
Source       : libnl3-3.5.0-2.fc32.src.rpm
Repository   : @System
From repo    : fedora
Summary      : Convenience library for kernel netlink sockets
URL          : http://www.infradead.org/~tgr/libnl/
License      : LGPLv2
Description  : This package contains a convenience library to simplify
             : using the Linux kernel's netlink sockets interface for
             : network manipulation

Code to reproduce the problem:

#include <syslog.h>
#include <errno.h>
#include <unistd.h>
#include <linux/types.h>
#include <netlink/netlink.h>
#include <netlink/socket.h>
#include <netlink/utils.h>
#include <netlink/route/link.h>

int main() {
        struct nl_sock *nl_sock;
        struct rtnl_link *link;

        const char *iface = "wlp4s0";
        int rx_bytes, tx_bytes;
        int rx_packets, tx_packets;

        nl_sock = nl_socket_alloc();
        nl_connect(nl_sock, NETLINK_ROUTE);
        rtnl_link_get_kernel(nl_sock, 0, iface, &link);

        for (int i = 0; i < 10000; i++) {
                rtnl_link_get_kernel(nl_sock, 0, iface, &link);

                rx_bytes = rtnl_link_get_stat(link, RTNL_LINK_RX_BYTES);
                tx_bytes = rtnl_link_get_stat(link, RTNL_LINK_TX_BYTES);
                rx_packets = rtnl_link_get_stat(link, RTNL_LINK_RX_PACKETS);
                rx_packets += rtnl_link_get_stat(link, RTNL_LINK_RX_COMPRESSED);
                tx_packets = rtnl_link_get_stat(link, RTNL_LINK_TX_PACKETS);
                tx_packets += rtnl_link_get_stat(link, RTNL_LINK_TX_COMPRESSED);

                if (rx_packets == 0)
                        printf("rx bytes: %d, rx packets: %d, tx bytes: %d, tx_packets: %d \n",
                               rx_bytes, rx_packets, tx_bytes, tx_packets);

                sleep(1);
        }
}

Compiled like this:

[acooks@t460s jittertrap]$ gcc -Wall test-rx-packets.c -o test-rx-packets -I/usr/include/libnl3 -lnl-route-3 -lnl-3

Produces the following output.

[acooks@t460s jittertrap]$ ./test-rx-packets 
rx bytes: 273202455, rx packets: 0, tx bytes: 72428306, tx_packets: 221284 
rx bytes: 273207711, rx packets: 0, tx bytes: 72433656, tx_packets: 221289 
rx bytes: 273212921, rx packets: 0, tx bytes: 72439006, tx_packets: 221294 
rx bytes: 273218177, rx packets: 0, tx bytes: 72444356, tx_packets: 221299 
rx bytes: 273223387, rx packets: 0, tx bytes: 72449706, tx_packets: 221304 
rx bytes: 273228643, rx packets: 0, tx bytes: 72455056, tx_packets: 221309 
rx bytes: 273233853, rx packets: 0, tx bytes: 72460406, tx_packets: 221314 
rx bytes: 273240679, rx packets: 0, tx bytes: 72467285, tx_packets: 221328 
rx bytes: 273251803, rx packets: 0, tx bytes: 72472729, tx_packets: 221334 
^C

I can't think of a reason why received packets would not increase, and the same code works on Ubuntu with version 3.4.0:

ubuntu@demo:~$ sudo apt info libnl-3-200
Package: libnl-3-200
Version: 3.4.0-1
Priority: optional
Section: libs
Source: libnl3
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Heiko Stuebner <mmind@debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 184 kB
Depends: libc6 (>= 2.14)
Homepage: http://www.infradead.org/~tgr/libnl/
Task: ubuntu-desktop-minimal, samba-server, ubuntu-desktop, kubuntu-desktop, xubuntu-core, xubuntu-desktop, lubuntu-desktop, ubuntustudio-desktop-core, ubuntustudio-desktop, ubuntukylin-desktop, ubuntu-mate-core, ubuntu-mate-desktop, ubuntu-budgie-desktop
Download-Size: 53.9 kB
APT-Manual-Installed: yes
APT-Sources: http://archive.ubuntu.com/ubuntu focal/main amd64 Packages
Description: library for dealing with netlink sockets
 This is a library for applications dealing with netlink sockets.
 The library provides an interface for raw netlink messaging and various
 netlink family specific interfaces.

ubuntu@demo:~$ uname -a
Linux demo.jittertrap.net 5.4.0-1025-aws #25-Ubuntu SMP Fri Sep 11 09:37:24 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

therefore I think this is a bug in libnl.

MarcKe commented 3 years ago

This might help you and some others Insert this as a patch https://github.com/thom311/libnl/commit/bab9e77c87d3b596e77d669b0a827b50e725bb62 It should fix the issue.