Statistical Information

The last method a driver needs is get_stats. This method returns a pointer to the statistics for the device. Its implementation is pretty easy; the one shown works even when several interfaces are managed by the same driver, because the statistics are hosted within the device data structure.

struct net_device_stats *snull_stats(struct net_device *dev)
    struct snull_priv *priv = (struct snull_priv *) dev->priv;
    return &priv->stats;

The real work needed to return meaningful statistics is distributed throughout the driver, where the various fields are updated. The following list shows the most interesting fields in struct net_device_stats.

unsigned long rx_packets; , unsigned long tx_packets;

These fields hold the total number of incoming and outgoing packets successfully transferred by the interface.

unsigned long rx_bytes; , unsigned long tx_bytes;

The number of bytes received and transmitted by the interface. These fields were added in the 2.2 kernel.

unsigned long rx_errors; , unsigned long tx_errors;

The number of erroneous receptions and transmissions. There’s no end of things that can go wrong with packet transmission, and the net_device_stats structure includes six counters for specific receive errors and five for transmit errors. See <linux/netdevice.h> for the full list. If possible, your driver should maintain detailed error statistics, because they can be most helpful to system administrators trying to track down a problem.

unsigned long rx_dropped; , unsigned long tx_dropped;

The number of packets dropped during reception and transmission. Packets are dropped when there’s no memory available for packet data. tx_dropped is rarely used.

unsigned long collisions;

The number of collisions due to congestion on the medium.

unsigned long multicast;

The number of multicast packets received.

It is worth repeating that the get_stats method can be called at any time—even when the interface is down—so the driver should not release statistic information when running the stop method.

Get Linux Device Drivers, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.