How to measure packets per second or throughput on high speed network interface

There are many traffic monitoring tools available on Linux, which can monitor/classify network traffic, and report real-time traffic statistics in fancy user interfaces. Most of these tools (e.g., ntopng, iftop) are powered by libpcap, which is a packet capture library used to monitor network traffic in user space. Despite their versatility, however, libpcap-based network monitoring tools cannot scale to handle traffic on multi Gigabit rate network interfaces, due to the overhead associated with user-space packet capture.

In this tutorial, I will present simple shell scripts that can monitor network traffic on per-interface basis, without relying on slow libpcap library. These scripts are fast enough to support multi Gigabit rates, but only suitable if you are interested in "aggregate" network statistics on per interface basis.

The secret for the scripts lies in sysfs virtual filesystem which is used by the kernel to export device- or driver-related information to user space. Network interface related statistics are exported via /sys/class/net/<ethX>/statistics.

For example, the statistics on eth0 interface are found in these files:

  • /sys/class/net/eth0/statistics/rx_packets: number of packets received
  • /sys/class/net/eth0/statistics/tx_packets: number of packets transmitted
  • /sys/class/net/eth0/statistics/rx_bytes: number of bytes received
  • /sys/class/net/eth0/statistics/tx_bytes: number of bytes transmitted
  • /sys/class/net/eth0/statistics/rx_dropped: number of packets dropped while received
  • /sys/class/net/eth0/statistics/tx_dropped: number of packets dropped while transmitted

The numbers stored in the files are automatically refreshed in real-time by the kernel. Therefore, you can write scripts that calculate traffic statistics based on these files.

The following are two such scripts (thanks to joemiller). The first script counts the number of packets per second, received (RX) or sent (TX) on an interface, while the latter scripts measures the network bandwidth of incoming (RX) and outgoing (TX) traffic on an interface. For these scripts to work, you do not need to install anything.

Measure Packets per Second on an Interface

#!/bin/bash

INTERVAL="1"  # update interval in seconds

if [ -z "$1" ]; then
        echo
        echo usage: $0 [network-interface]
        echo
        echo e.g. $0 eth0
        echo
        echo shows packets-per-second
        exit
fi

IF=$1

while true
do
        R1=`cat /sys/class/net/$1/statistics/rx_packets`
        T1=`cat /sys/class/net/$1/statistics/tx_packets`
        sleep $INTERVAL
        R2=`cat /sys/class/net/$1/statistics/rx_packets`
        T2=`cat /sys/class/net/$1/statistics/tx_packets`
        TXPPS=`expr $T2 - $T1`
        RXPPS=`expr $R2 - $R1`
        echo "TX $1: $TXPPS pkts/s RX $1: $RXPPS pkts/s"
done

Measure Network Bandwidth on an Interface

#!/bin/bash

INTERVAL="1"  # update interval in seconds

if [ -z "$1" ]; then
        echo
        echo usage: $0 [network-interface]
        echo
        echo e.g. $0 eth0
        echo
        exit
fi

IF=$1

while true
do
        R1=`cat /sys/class/net/$1/statistics/rx_bytes`
        T1=`cat /sys/class/net/$1/statistics/tx_bytes`
        sleep $INTERVAL
        R2=`cat /sys/class/net/$1/statistics/rx_bytes`
        T2=`cat /sys/class/net/$1/statistics/tx_bytes`
        TBPS=`expr $T2 - $T1`
        RBPS=`expr $R2 - $R1`
        TKBPS=`expr $TBPS / 1024`
        RKBPS=`expr $RBPS / 1024`
        echo "TX $1: $TKBPS kB/s RX $1: $RKBPS kB/s"
done

The following screenshot shows the above two scripts in action.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

11 thoughts on “How to measure packets per second or throughput on high speed network interface

  1. I believe there are typo's in this post.

    /sys/class/net/eth0/statistics/rx_packets: number of packets transmitted
    /sys/class/net/eth0/statistics/tx_bytes: number of bytes received

    S/B ?
    /sys/class/net/eth0/statistics/tx_packets: number of packets transmitted
    /sys/class/net/eth0/statistics/rx_bytes: number of bytes received

  2. nice work.
    but there's an ip tool (iproute2 package) which is quite light*
    and does the same.
    Just try:
    ip -s link

    *) light - check lib dependencies with ldd:
    ldd /sbin/ip
    libresolv.so.2
    libc.so.6
    /lib64/ld-linux-x86-64.so.2

  3. nice work and a good way to become familiar with sysfs, but I prefer ip from iproute package, that can also report network stats when called:
    /sbin/ip -s link
    It does not use libcap neither, and has far more useful features.

  4. This information can also be accessed through /proc/net/dev:

    eth=eth0; while true; do a=`cat /proc/net/dev | grep $eth | awk '{printf "%.0f\n",$2+$10}'`; sleep 1; b=`cat /proc/net/dev | grep $eth | awk '{printf "%.0f\n",$2+$10}'`; echo $eth "$(($b-$a))"; done

  5. The below solution (?????? ?????????) is not efficient and needs a lot of external commands.

    eth=eth0; while true; do a=`cat /proc/net/dev | grep $eth | awk '{printf "%.0f\n",$2+$10}'`; sleep 1; b=`cat /proc/net/dev | grep $eth | awk '{printf "%.0f\n",$2+$10}'`; echo $eth "$(($b-$a))"; done

  6. Hey, very nice script!

    One thought about it though. The output shows kb/s and in fact it displays bytes. It should read kB/s or you should multiply the result by 8.

  7. Nice script !

    Is there any tool available to measure the network performance in a multi-homing environment ?
    I want to measure performance of each interface in a gateway.

    Thanks

Leave a comment

Your email address will not be published. Required fields are marked *

Current day month ye@r *