10db0c34cSJakub Kicinski.. SPDX-License-Identifier: GPL-2.0 20db0c34cSJakub Kicinski 30db0c34cSJakub Kicinski==================== 40db0c34cSJakub KicinskiInterface statistics 50db0c34cSJakub Kicinski==================== 60db0c34cSJakub Kicinski 78c00bd93SJakub KicinskiOverview 88c00bd93SJakub Kicinski======== 98c00bd93SJakub Kicinski 100db0c34cSJakub KicinskiThis document is a guide to Linux network interface statistics. 110db0c34cSJakub Kicinski 128c00bd93SJakub KicinskiThere are three main sources of interface statistics in Linux: 130db0c34cSJakub Kicinski 140db0c34cSJakub Kicinski - standard interface statistics based on 158c00bd93SJakub Kicinski :c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`; 168c00bd93SJakub Kicinski - protocol-specific statistics; and 170db0c34cSJakub Kicinski - driver-defined statistics available via ethtool. 180db0c34cSJakub Kicinski 198c00bd93SJakub KicinskiStandard interface statistics 208c00bd93SJakub Kicinski----------------------------- 218c00bd93SJakub Kicinski 228c00bd93SJakub KicinskiThere are multiple interfaces to reach the standard statistics. 238c00bd93SJakub KicinskiMost commonly used is the `ip` command from `iproute2`:: 240db0c34cSJakub Kicinski 250db0c34cSJakub Kicinski $ ip -s -s link show dev ens4u1u1 260db0c34cSJakub Kicinski 6: ens4u1u1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000 270db0c34cSJakub Kicinski link/ether 48:2a:e3:4c:b1:d1 brd ff:ff:ff:ff:ff:ff 280db0c34cSJakub Kicinski RX: bytes packets errors dropped overrun mcast 290db0c34cSJakub Kicinski 74327665117 69016965 0 0 0 0 300db0c34cSJakub Kicinski RX errors: length crc frame fifo missed 310db0c34cSJakub Kicinski 0 0 0 0 0 320db0c34cSJakub Kicinski TX: bytes packets errors dropped carrier collsns 330db0c34cSJakub Kicinski 21405556176 44608960 0 0 0 0 340db0c34cSJakub Kicinski TX errors: aborted fifo window heartbeat transns 350db0c34cSJakub Kicinski 0 0 0 0 128 360db0c34cSJakub Kicinski altname enp58s0u1u1 370db0c34cSJakub Kicinski 380db0c34cSJakub KicinskiNote that `-s` has been specified twice to see all members of 390db0c34cSJakub Kicinski:c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`. 400db0c34cSJakub KicinskiIf `-s` is specified once the detailed errors won't be shown. 410db0c34cSJakub Kicinski 420db0c34cSJakub Kicinski`ip` supports JSON formatting via the `-j` option. 430db0c34cSJakub Kicinski 448c00bd93SJakub KicinskiProtocol-specific statistics 458c00bd93SJakub Kicinski---------------------------- 468c00bd93SJakub Kicinski 47f117c48cSJakub KicinskiProtocol-specific statistics are exposed via relevant interfaces, 48f117c48cSJakub Kicinskithe same interfaces as are used to configure them. 49f117c48cSJakub Kicinski 50f117c48cSJakub Kicinskiethtool 51f117c48cSJakub Kicinski~~~~~~~ 52f117c48cSJakub Kicinski 53f117c48cSJakub KicinskiEthtool exposes common low-level statistics. 54f117c48cSJakub KicinskiAll the standard statistics are expected to be maintained 55f117c48cSJakub Kicinskiby the device, not the driver (as opposed to driver-defined stats 56f117c48cSJakub Kicinskidescribed in the next section which mix software and hardware stats). 57f117c48cSJakub KicinskiFor devices which contain unmanaged 58f117c48cSJakub Kicinskiswitches (e.g. legacy SR-IOV or multi-host NICs) the events counted 59f117c48cSJakub Kicinskimay not pertain exclusively to the packets destined to 60f117c48cSJakub Kicinskithe local host interface. In other words the events may 61f117c48cSJakub Kicinskibe counted at the network port (MAC/PHY blocks) without separation 62f117c48cSJakub Kicinskifor different host side (PCIe) devices. Such ambiguity must not 63f117c48cSJakub Kicinskibe present when internal switch is managed by Linux (so called 64f117c48cSJakub Kicinskiswitchdev mode for NICs). 65f117c48cSJakub Kicinski 66f117c48cSJakub KicinskiStandard ethtool statistics can be accessed via the interfaces used 67f117c48cSJakub Kicinskifor configuration. For example ethtool interface used 688c00bd93SJakub Kicinskito configure pause frames can report corresponding hardware counters:: 698c00bd93SJakub Kicinski 708c00bd93SJakub Kicinski $ ethtool --include-statistics -a eth0 718c00bd93SJakub Kicinski Pause parameters for eth0: 728c00bd93SJakub Kicinski Autonegotiate: on 738c00bd93SJakub Kicinski RX: on 748c00bd93SJakub Kicinski TX: on 758c00bd93SJakub Kicinski Statistics: 768c00bd93SJakub Kicinski tx_pause_frames: 1 778c00bd93SJakub Kicinski rx_pause_frames: 1 788c00bd93SJakub Kicinski 79f117c48cSJakub KicinskiGeneral Ethernet statistics not associated with any particular 80f117c48cSJakub Kicinskifunctionality are exposed via ``ethtool -S $ifc`` by specifying 81f117c48cSJakub Kicinskithe ``--groups`` parameter:: 82f117c48cSJakub Kicinski 83f117c48cSJakub Kicinski $ ethtool -S eth0 --groups eth-phy eth-mac eth-ctrl rmon 84f117c48cSJakub Kicinski Stats for eth0: 85f117c48cSJakub Kicinski eth-phy-SymbolErrorDuringCarrier: 0 86f117c48cSJakub Kicinski eth-mac-FramesTransmittedOK: 1 87f117c48cSJakub Kicinski eth-mac-FrameTooLongErrors: 1 88f117c48cSJakub Kicinski eth-ctrl-MACControlFramesTransmitted: 1 89f117c48cSJakub Kicinski eth-ctrl-MACControlFramesReceived: 0 90f117c48cSJakub Kicinski eth-ctrl-UnsupportedOpcodesReceived: 1 91f117c48cSJakub Kicinski rmon-etherStatsUndersizePkts: 1 92f117c48cSJakub Kicinski rmon-etherStatsJabbers: 0 93f117c48cSJakub Kicinski rmon-rx-etherStatsPkts64Octets: 1 94f117c48cSJakub Kicinski rmon-rx-etherStatsPkts65to127Octets: 0 95f117c48cSJakub Kicinski rmon-rx-etherStatsPkts128to255Octets: 0 96f117c48cSJakub Kicinski rmon-tx-etherStatsPkts64Octets: 2 97f117c48cSJakub Kicinski rmon-tx-etherStatsPkts65to127Octets: 3 98f117c48cSJakub Kicinski rmon-tx-etherStatsPkts128to255Octets: 0 99f117c48cSJakub Kicinski 1008c00bd93SJakub KicinskiDriver-defined statistics 1018c00bd93SJakub Kicinski------------------------- 1028c00bd93SJakub Kicinski 1038c00bd93SJakub KicinskiDriver-defined ethtool statistics can be dumped using `ethtool -S $ifc`, e.g.:: 1040db0c34cSJakub Kicinski 1050db0c34cSJakub Kicinski $ ethtool -S ens4u1u1 1060db0c34cSJakub Kicinski NIC statistics: 1070db0c34cSJakub Kicinski tx_single_collisions: 0 1080db0c34cSJakub Kicinski tx_multi_collisions: 0 1090db0c34cSJakub Kicinski 1100db0c34cSJakub KicinskiuAPIs 1110db0c34cSJakub Kicinski===== 1120db0c34cSJakub Kicinski 1130db0c34cSJakub Kicinskiprocfs 1140db0c34cSJakub Kicinski------ 1150db0c34cSJakub Kicinski 1160db0c34cSJakub KicinskiThe historical `/proc/net/dev` text interface gives access to the list 1170db0c34cSJakub Kicinskiof interfaces as well as their statistics. 1180db0c34cSJakub Kicinski 1190db0c34cSJakub KicinskiNote that even though this interface is using 1200db0c34cSJakub Kicinski:c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>` 1210db0c34cSJakub Kicinskiinternally it combines some of the fields. 1220db0c34cSJakub Kicinski 1230db0c34cSJakub Kicinskisysfs 1240db0c34cSJakub Kicinski----- 1250db0c34cSJakub Kicinski 1260db0c34cSJakub KicinskiEach device directory in sysfs contains a `statistics` directory (e.g. 1270db0c34cSJakub Kicinski`/sys/class/net/lo/statistics/`) with files corresponding to 1280db0c34cSJakub Kicinskimembers of :c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`. 1290db0c34cSJakub Kicinski 1300db0c34cSJakub KicinskiThis simple interface is convenient especially in constrained/embedded 1310db0c34cSJakub Kicinskienvironments without access to tools. However, it's inefficient when 1320db0c34cSJakub Kicinskireading multiple stats as it internally performs a full dump of 1330db0c34cSJakub Kicinski:c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>` 1340db0c34cSJakub Kicinskiand reports only the stat corresponding to the accessed file. 1350db0c34cSJakub Kicinski 1360db0c34cSJakub KicinskiSysfs files are documented in 1370db0c34cSJakub Kicinski`Documentation/ABI/testing/sysfs-class-net-statistics`. 1380db0c34cSJakub Kicinski 1390db0c34cSJakub Kicinski 1400db0c34cSJakub Kicinskinetlink 1410db0c34cSJakub Kicinski------- 1420db0c34cSJakub Kicinski 1430db0c34cSJakub Kicinski`rtnetlink` (`NETLINK_ROUTE`) is the preferred method of accessing 1440db0c34cSJakub Kicinski:c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>` stats. 1450db0c34cSJakub Kicinski 1460db0c34cSJakub KicinskiStatistics are reported both in the responses to link information 1470db0c34cSJakub Kicinskirequests (`RTM_GETLINK`) and statistic requests (`RTM_GETSTATS`, 1480db0c34cSJakub Kicinskiwhen `IFLA_STATS_LINK_64` bit is set in the `.filter_mask` of the request). 1490db0c34cSJakub Kicinski 1500db0c34cSJakub Kicinskiethtool 1510db0c34cSJakub Kicinski------- 1520db0c34cSJakub Kicinski 1530db0c34cSJakub KicinskiEthtool IOCTL interface allows drivers to report implementation 1540db0c34cSJakub Kicinskispecific statistics. Historically it has also been used to report 1550db0c34cSJakub Kicinskistatistics for which other APIs did not exist, like per-device-queue 1560db0c34cSJakub Kicinskistatistics, or standard-based statistics (e.g. RFC 2863). 1570db0c34cSJakub Kicinski 1580db0c34cSJakub KicinskiStatistics and their string identifiers are retrieved separately. 1590db0c34cSJakub KicinskiIdentifiers via `ETHTOOL_GSTRINGS` with `string_set` set to `ETH_SS_STATS`, 1600db0c34cSJakub Kicinskiand values via `ETHTOOL_GSTATS`. User space should use `ETHTOOL_GDRVINFO` 1610db0c34cSJakub Kicinskito retrieve the number of statistics (`.n_stats`). 1620db0c34cSJakub Kicinski 1638c00bd93SJakub Kicinskiethtool-netlink 1648c00bd93SJakub Kicinski--------------- 1658c00bd93SJakub Kicinski 1668c00bd93SJakub KicinskiEthtool netlink is a replacement for the older IOCTL interface. 1678c00bd93SJakub Kicinski 1688c00bd93SJakub KicinskiProtocol-related statistics can be requested in get commands by setting 1698c00bd93SJakub Kicinskithe `ETHTOOL_FLAG_STATS` flag in `ETHTOOL_A_HEADER_FLAGS`. Currently 1708c00bd93SJakub Kicinskistatistics are supported in the following commands: 1718c00bd93SJakub Kicinski 1728c00bd93SJakub Kicinski - `ETHTOOL_MSG_PAUSE_GET` 173be85dbfeSJakub Kicinski - `ETHTOOL_MSG_FEC_GET` 174*37000004SVladimir Oltean - `ETHTOOL_MSG_MM_GET` 1758c00bd93SJakub Kicinski 1760db0c34cSJakub Kicinskidebugfs 1770db0c34cSJakub Kicinski------- 1780db0c34cSJakub Kicinski 1790db0c34cSJakub KicinskiSome drivers expose extra statistics via `debugfs`. 1800db0c34cSJakub Kicinski 1810db0c34cSJakub Kicinskistruct rtnl_link_stats64 1820db0c34cSJakub Kicinski======================== 1830db0c34cSJakub Kicinski 1840db0c34cSJakub Kicinski.. kernel-doc:: include/uapi/linux/if_link.h 1850db0c34cSJakub Kicinski :identifiers: rtnl_link_stats64 1860db0c34cSJakub Kicinski 1870db0c34cSJakub KicinskiNotes for driver authors 1880db0c34cSJakub Kicinski======================== 1890db0c34cSJakub Kicinski 1900db0c34cSJakub KicinskiDrivers should report all statistics which have a matching member in 1910db0c34cSJakub Kicinski:c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>` exclusively 1920db0c34cSJakub Kicinskivia `.ndo_get_stats64`. Reporting such standard stats via ethtool 1930db0c34cSJakub Kicinskior debugfs will not be accepted. 1940db0c34cSJakub Kicinski 1950db0c34cSJakub KicinskiDrivers must ensure best possible compliance with 1960db0c34cSJakub Kicinski:c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`. 1970db0c34cSJakub KicinskiPlease note for example that detailed error statistics must be 1980db0c34cSJakub Kicinskiadded into the general `rx_error` / `tx_error` counters. 1990db0c34cSJakub Kicinski 2000db0c34cSJakub KicinskiThe `.ndo_get_stats64` callback can not sleep because of accesses 2010db0c34cSJakub Kicinskivia `/proc/net/dev`. If driver may sleep when retrieving the statistics 2020db0c34cSJakub Kicinskifrom the device it should do so periodically asynchronously and only return 2030db0c34cSJakub Kicinskia recent copy from `.ndo_get_stats64`. Ethtool interrupt coalescing interface 2040db0c34cSJakub Kicinskiallows setting the frequency of refreshing statistics, if needed. 2050db0c34cSJakub Kicinski 2060db0c34cSJakub KicinskiRetrieving ethtool statistics is a multi-syscall process, drivers are advised 2070db0c34cSJakub Kicinskito keep the number of statistics constant to avoid race conditions with 2080db0c34cSJakub Kicinskiuser space trying to read them. 2090db0c34cSJakub Kicinski 2100db0c34cSJakub KicinskiStatistics must persist across routine operations like bringing the interface 2110db0c34cSJakub Kicinskidown and up. 2128c00bd93SJakub Kicinski 2138c00bd93SJakub KicinskiKernel-internal data structures 2148c00bd93SJakub Kicinski------------------------------- 2158c00bd93SJakub Kicinski 2168c00bd93SJakub KicinskiThe following structures are internal to the kernel, their members are 2178c00bd93SJakub Kicinskitranslated to netlink attributes when dumped. Drivers must not overwrite 2188c00bd93SJakub Kicinskithe statistics they don't report with 0. 2198c00bd93SJakub Kicinski 22097e44c4fSMauro Carvalho Chehab- ethtool_pause_stats() 221be85dbfeSJakub Kicinski- ethtool_fec_stats() 222