How to reset RX/TX counters of a network interface on Linux

Last updated on July 7, 2020 by Dan Nanni

Question: I am testing a network interface card (NIC), and currently ifconfig command reports huge numbers on RX/TX and error/dropped counters on some interface. Is there a way to reset ifconfig packet counters on a network interface without rebooting the server?

On Linux, each network interface comes with several packet counters such as RX (number of received packets), TX (number of transmitted packets), errors (number of packets with errors), dropped (number of dropped packets) and overruns (number of lost packets due to queue overrun). You can inspect these packet counters either by running ifconfig command or by inspecting /proc/net/dev. These counters keep incrementing over time until they wrap around. If, for some reason, you want to reset those counters at any time, here is what you can do.

First, you must understand that you cannot reset the packet counters while the NIC is being used. Since these packet counters are maintained by a NIC driver, you can reset the counters by disabling and reloading the NIC driver.

In this example, let's reset the packet counters for a network interface named p1p2.

First, you need to identify which NIC driver is associated with p1p2 interface.

The following command will help you find that out. It will show detailed information of existing Ethernet interfaces, including assigned interface name and an associated NIC driver.

$ sudo lshw -class network

In this example, the interface p1p2 is managed by a driver named ixgbe.

Check if the driver is currently loaded in the kernel.

$ lsmod | grep ixgbe
ixgbe                  236327  0
dca                     15232  1 ixgbe
ptp                     18627  1 ixgbe
mdio                    13807  1 ixgbe

Now go ahead and disable the driver by using modprobe command. Be aware that if you are connected remotely to this machine through the interface managed by the driver, you will lose the connection. So be careful!

The following command will remove the driver from the kernel.

$ sudo modprobe -r ixgbe

Verify that the driver is successfully removed by issuing the command below. If the driver is unloaded safely, the command should not display any output.

$ lsmod | grep ixgbe

Go ahead and re-enable the driver by using modprobe command.

$ sudo modprobe ixgbe

At this point, the RX/TX and all other counters of the interface p1p2 should be successfully reset.

NOTE: In this example, we assume that the NIC driver exists as a loadable kernel module. However, some NIC drivers are built into the kernel, in which case you cannot disable the module as described in this tutorial. To check if the NIC driver is kernel built-in, use modprobe command.

$ modprobe -D lzo_compress
builtin lzo_compress

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean