1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun 3*4882a593Smuzhiyun==================== 4*4882a593SmuzhiyunInterface statistics 5*4882a593Smuzhiyun==================== 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunOverview 8*4882a593Smuzhiyun======== 9*4882a593Smuzhiyun 10*4882a593SmuzhiyunThis document is a guide to Linux network interface statistics. 11*4882a593Smuzhiyun 12*4882a593SmuzhiyunThere are three main sources of interface statistics in Linux: 13*4882a593Smuzhiyun 14*4882a593Smuzhiyun - standard interface statistics based on 15*4882a593Smuzhiyun :c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`; 16*4882a593Smuzhiyun - protocol-specific statistics; and 17*4882a593Smuzhiyun - driver-defined statistics available via ethtool. 18*4882a593Smuzhiyun 19*4882a593SmuzhiyunStandard interface statistics 20*4882a593Smuzhiyun----------------------------- 21*4882a593Smuzhiyun 22*4882a593SmuzhiyunThere are multiple interfaces to reach the standard statistics. 23*4882a593SmuzhiyunMost commonly used is the `ip` command from `iproute2`:: 24*4882a593Smuzhiyun 25*4882a593Smuzhiyun $ ip -s -s link show dev ens4u1u1 26*4882a593Smuzhiyun 6: ens4u1u1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000 27*4882a593Smuzhiyun link/ether 48:2a:e3:4c:b1:d1 brd ff:ff:ff:ff:ff:ff 28*4882a593Smuzhiyun RX: bytes packets errors dropped overrun mcast 29*4882a593Smuzhiyun 74327665117 69016965 0 0 0 0 30*4882a593Smuzhiyun RX errors: length crc frame fifo missed 31*4882a593Smuzhiyun 0 0 0 0 0 32*4882a593Smuzhiyun TX: bytes packets errors dropped carrier collsns 33*4882a593Smuzhiyun 21405556176 44608960 0 0 0 0 34*4882a593Smuzhiyun TX errors: aborted fifo window heartbeat transns 35*4882a593Smuzhiyun 0 0 0 0 128 36*4882a593Smuzhiyun altname enp58s0u1u1 37*4882a593Smuzhiyun 38*4882a593SmuzhiyunNote that `-s` has been specified twice to see all members of 39*4882a593Smuzhiyun:c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`. 40*4882a593SmuzhiyunIf `-s` is specified once the detailed errors won't be shown. 41*4882a593Smuzhiyun 42*4882a593Smuzhiyun`ip` supports JSON formatting via the `-j` option. 43*4882a593Smuzhiyun 44*4882a593SmuzhiyunProtocol-specific statistics 45*4882a593Smuzhiyun---------------------------- 46*4882a593Smuzhiyun 47*4882a593SmuzhiyunSome of the interfaces used for configuring devices are also able 48*4882a593Smuzhiyunto report related statistics. For example ethtool interface used 49*4882a593Smuzhiyunto configure pause frames can report corresponding hardware counters:: 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun $ ethtool --include-statistics -a eth0 52*4882a593Smuzhiyun Pause parameters for eth0: 53*4882a593Smuzhiyun Autonegotiate: on 54*4882a593Smuzhiyun RX: on 55*4882a593Smuzhiyun TX: on 56*4882a593Smuzhiyun Statistics: 57*4882a593Smuzhiyun tx_pause_frames: 1 58*4882a593Smuzhiyun rx_pause_frames: 1 59*4882a593Smuzhiyun 60*4882a593SmuzhiyunDriver-defined statistics 61*4882a593Smuzhiyun------------------------- 62*4882a593Smuzhiyun 63*4882a593SmuzhiyunDriver-defined ethtool statistics can be dumped using `ethtool -S $ifc`, e.g.:: 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun $ ethtool -S ens4u1u1 66*4882a593Smuzhiyun NIC statistics: 67*4882a593Smuzhiyun tx_single_collisions: 0 68*4882a593Smuzhiyun tx_multi_collisions: 0 69*4882a593Smuzhiyun 70*4882a593SmuzhiyunuAPIs 71*4882a593Smuzhiyun===== 72*4882a593Smuzhiyun 73*4882a593Smuzhiyunprocfs 74*4882a593Smuzhiyun------ 75*4882a593Smuzhiyun 76*4882a593SmuzhiyunThe historical `/proc/net/dev` text interface gives access to the list 77*4882a593Smuzhiyunof interfaces as well as their statistics. 78*4882a593Smuzhiyun 79*4882a593SmuzhiyunNote that even though this interface is using 80*4882a593Smuzhiyun:c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>` 81*4882a593Smuzhiyuninternally it combines some of the fields. 82*4882a593Smuzhiyun 83*4882a593Smuzhiyunsysfs 84*4882a593Smuzhiyun----- 85*4882a593Smuzhiyun 86*4882a593SmuzhiyunEach device directory in sysfs contains a `statistics` directory (e.g. 87*4882a593Smuzhiyun`/sys/class/net/lo/statistics/`) with files corresponding to 88*4882a593Smuzhiyunmembers of :c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`. 89*4882a593Smuzhiyun 90*4882a593SmuzhiyunThis simple interface is convenient especially in constrained/embedded 91*4882a593Smuzhiyunenvironments without access to tools. However, it's inefficient when 92*4882a593Smuzhiyunreading multiple stats as it internally performs a full dump of 93*4882a593Smuzhiyun:c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>` 94*4882a593Smuzhiyunand reports only the stat corresponding to the accessed file. 95*4882a593Smuzhiyun 96*4882a593SmuzhiyunSysfs files are documented in 97*4882a593Smuzhiyun`Documentation/ABI/testing/sysfs-class-net-statistics`. 98*4882a593Smuzhiyun 99*4882a593Smuzhiyun 100*4882a593Smuzhiyunnetlink 101*4882a593Smuzhiyun------- 102*4882a593Smuzhiyun 103*4882a593Smuzhiyun`rtnetlink` (`NETLINK_ROUTE`) is the preferred method of accessing 104*4882a593Smuzhiyun:c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>` stats. 105*4882a593Smuzhiyun 106*4882a593SmuzhiyunStatistics are reported both in the responses to link information 107*4882a593Smuzhiyunrequests (`RTM_GETLINK`) and statistic requests (`RTM_GETSTATS`, 108*4882a593Smuzhiyunwhen `IFLA_STATS_LINK_64` bit is set in the `.filter_mask` of the request). 109*4882a593Smuzhiyun 110*4882a593Smuzhiyunethtool 111*4882a593Smuzhiyun------- 112*4882a593Smuzhiyun 113*4882a593SmuzhiyunEthtool IOCTL interface allows drivers to report implementation 114*4882a593Smuzhiyunspecific statistics. Historically it has also been used to report 115*4882a593Smuzhiyunstatistics for which other APIs did not exist, like per-device-queue 116*4882a593Smuzhiyunstatistics, or standard-based statistics (e.g. RFC 2863). 117*4882a593Smuzhiyun 118*4882a593SmuzhiyunStatistics and their string identifiers are retrieved separately. 119*4882a593SmuzhiyunIdentifiers via `ETHTOOL_GSTRINGS` with `string_set` set to `ETH_SS_STATS`, 120*4882a593Smuzhiyunand values via `ETHTOOL_GSTATS`. User space should use `ETHTOOL_GDRVINFO` 121*4882a593Smuzhiyunto retrieve the number of statistics (`.n_stats`). 122*4882a593Smuzhiyun 123*4882a593Smuzhiyunethtool-netlink 124*4882a593Smuzhiyun--------------- 125*4882a593Smuzhiyun 126*4882a593SmuzhiyunEthtool netlink is a replacement for the older IOCTL interface. 127*4882a593Smuzhiyun 128*4882a593SmuzhiyunProtocol-related statistics can be requested in get commands by setting 129*4882a593Smuzhiyunthe `ETHTOOL_FLAG_STATS` flag in `ETHTOOL_A_HEADER_FLAGS`. Currently 130*4882a593Smuzhiyunstatistics are supported in the following commands: 131*4882a593Smuzhiyun 132*4882a593Smuzhiyun - `ETHTOOL_MSG_PAUSE_GET` 133*4882a593Smuzhiyun 134*4882a593Smuzhiyundebugfs 135*4882a593Smuzhiyun------- 136*4882a593Smuzhiyun 137*4882a593SmuzhiyunSome drivers expose extra statistics via `debugfs`. 138*4882a593Smuzhiyun 139*4882a593Smuzhiyunstruct rtnl_link_stats64 140*4882a593Smuzhiyun======================== 141*4882a593Smuzhiyun 142*4882a593Smuzhiyun.. kernel-doc:: include/uapi/linux/if_link.h 143*4882a593Smuzhiyun :identifiers: rtnl_link_stats64 144*4882a593Smuzhiyun 145*4882a593SmuzhiyunNotes for driver authors 146*4882a593Smuzhiyun======================== 147*4882a593Smuzhiyun 148*4882a593SmuzhiyunDrivers should report all statistics which have a matching member in 149*4882a593Smuzhiyun:c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>` exclusively 150*4882a593Smuzhiyunvia `.ndo_get_stats64`. Reporting such standard stats via ethtool 151*4882a593Smuzhiyunor debugfs will not be accepted. 152*4882a593Smuzhiyun 153*4882a593SmuzhiyunDrivers must ensure best possible compliance with 154*4882a593Smuzhiyun:c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`. 155*4882a593SmuzhiyunPlease note for example that detailed error statistics must be 156*4882a593Smuzhiyunadded into the general `rx_error` / `tx_error` counters. 157*4882a593Smuzhiyun 158*4882a593SmuzhiyunThe `.ndo_get_stats64` callback can not sleep because of accesses 159*4882a593Smuzhiyunvia `/proc/net/dev`. If driver may sleep when retrieving the statistics 160*4882a593Smuzhiyunfrom the device it should do so periodically asynchronously and only return 161*4882a593Smuzhiyuna recent copy from `.ndo_get_stats64`. Ethtool interrupt coalescing interface 162*4882a593Smuzhiyunallows setting the frequency of refreshing statistics, if needed. 163*4882a593Smuzhiyun 164*4882a593SmuzhiyunRetrieving ethtool statistics is a multi-syscall process, drivers are advised 165*4882a593Smuzhiyunto keep the number of statistics constant to avoid race conditions with 166*4882a593Smuzhiyunuser space trying to read them. 167*4882a593Smuzhiyun 168*4882a593SmuzhiyunStatistics must persist across routine operations like bringing the interface 169*4882a593Smuzhiyundown and up. 170*4882a593Smuzhiyun 171*4882a593SmuzhiyunKernel-internal data structures 172*4882a593Smuzhiyun------------------------------- 173*4882a593Smuzhiyun 174*4882a593SmuzhiyunThe following structures are internal to the kernel, their members are 175*4882a593Smuzhiyuntranslated to netlink attributes when dumped. Drivers must not overwrite 176*4882a593Smuzhiyunthe statistics they don't report with 0. 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun- ethtool_pause_stats() 179