18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci====================
48c2ecf20Sopenharmony_ciInterface statistics
58c2ecf20Sopenharmony_ci====================
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ciOverview
88c2ecf20Sopenharmony_ci========
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciThis document is a guide to Linux network interface statistics.
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ciThere are three main sources of interface statistics in Linux:
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci - standard interface statistics based on
158c2ecf20Sopenharmony_ci   :c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`;
168c2ecf20Sopenharmony_ci - protocol-specific statistics; and
178c2ecf20Sopenharmony_ci - driver-defined statistics available via ethtool.
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ciStandard interface statistics
208c2ecf20Sopenharmony_ci-----------------------------
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ciThere are multiple interfaces to reach the standard statistics.
238c2ecf20Sopenharmony_ciMost commonly used is the `ip` command from `iproute2`::
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci  $ ip -s -s link show dev ens4u1u1
268c2ecf20Sopenharmony_ci  6: ens4u1u1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
278c2ecf20Sopenharmony_ci    link/ether 48:2a:e3:4c:b1:d1 brd ff:ff:ff:ff:ff:ff
288c2ecf20Sopenharmony_ci    RX: bytes  packets  errors  dropped overrun mcast
298c2ecf20Sopenharmony_ci    74327665117 69016965 0       0       0       0
308c2ecf20Sopenharmony_ci    RX errors: length   crc     frame   fifo    missed
318c2ecf20Sopenharmony_ci               0        0       0       0       0
328c2ecf20Sopenharmony_ci    TX: bytes  packets  errors  dropped carrier collsns
338c2ecf20Sopenharmony_ci    21405556176 44608960 0       0       0       0
348c2ecf20Sopenharmony_ci    TX errors: aborted  fifo   window heartbeat transns
358c2ecf20Sopenharmony_ci               0        0       0       0       128
368c2ecf20Sopenharmony_ci    altname enp58s0u1u1
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciNote that `-s` has been specified twice to see all members of
398c2ecf20Sopenharmony_ci:c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`.
408c2ecf20Sopenharmony_ciIf `-s` is specified once the detailed errors won't be shown.
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci`ip` supports JSON formatting via the `-j` option.
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ciProtocol-specific statistics
458c2ecf20Sopenharmony_ci----------------------------
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ciSome of the interfaces used for configuring devices are also able
488c2ecf20Sopenharmony_cito report related statistics. For example ethtool interface used
498c2ecf20Sopenharmony_cito configure pause frames can report corresponding hardware counters::
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci  $ ethtool --include-statistics -a eth0
528c2ecf20Sopenharmony_ci  Pause parameters for eth0:
538c2ecf20Sopenharmony_ci  Autonegotiate:	on
548c2ecf20Sopenharmony_ci  RX:			on
558c2ecf20Sopenharmony_ci  TX:			on
568c2ecf20Sopenharmony_ci  Statistics:
578c2ecf20Sopenharmony_ci    tx_pause_frames: 1
588c2ecf20Sopenharmony_ci    rx_pause_frames: 1
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ciDriver-defined statistics
618c2ecf20Sopenharmony_ci-------------------------
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ciDriver-defined ethtool statistics can be dumped using `ethtool -S $ifc`, e.g.::
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci  $ ethtool -S ens4u1u1
668c2ecf20Sopenharmony_ci  NIC statistics:
678c2ecf20Sopenharmony_ci     tx_single_collisions: 0
688c2ecf20Sopenharmony_ci     tx_multi_collisions: 0
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ciuAPIs
718c2ecf20Sopenharmony_ci=====
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ciprocfs
748c2ecf20Sopenharmony_ci------
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ciThe historical `/proc/net/dev` text interface gives access to the list
778c2ecf20Sopenharmony_ciof interfaces as well as their statistics.
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ciNote that even though this interface is using
808c2ecf20Sopenharmony_ci:c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`
818c2ecf20Sopenharmony_ciinternally it combines some of the fields.
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cisysfs
848c2ecf20Sopenharmony_ci-----
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ciEach device directory in sysfs contains a `statistics` directory (e.g.
878c2ecf20Sopenharmony_ci`/sys/class/net/lo/statistics/`) with files corresponding to
888c2ecf20Sopenharmony_cimembers of :c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`.
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ciThis simple interface is convenient especially in constrained/embedded
918c2ecf20Sopenharmony_cienvironments without access to tools. However, it's inefficient when
928c2ecf20Sopenharmony_cireading multiple stats as it internally performs a full dump of
938c2ecf20Sopenharmony_ci:c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`
948c2ecf20Sopenharmony_ciand reports only the stat corresponding to the accessed file.
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ciSysfs files are documented in
978c2ecf20Sopenharmony_ci`Documentation/ABI/testing/sysfs-class-net-statistics`.
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cinetlink
1018c2ecf20Sopenharmony_ci-------
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci`rtnetlink` (`NETLINK_ROUTE`) is the preferred method of accessing
1048c2ecf20Sopenharmony_ci:c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>` stats.
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ciStatistics are reported both in the responses to link information
1078c2ecf20Sopenharmony_cirequests (`RTM_GETLINK`) and statistic requests (`RTM_GETSTATS`,
1088c2ecf20Sopenharmony_ciwhen `IFLA_STATS_LINK_64` bit is set in the `.filter_mask` of the request).
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ciethtool
1118c2ecf20Sopenharmony_ci-------
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ciEthtool IOCTL interface allows drivers to report implementation
1148c2ecf20Sopenharmony_cispecific statistics. Historically it has also been used to report
1158c2ecf20Sopenharmony_cistatistics for which other APIs did not exist, like per-device-queue
1168c2ecf20Sopenharmony_cistatistics, or standard-based statistics (e.g. RFC 2863).
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ciStatistics and their string identifiers are retrieved separately.
1198c2ecf20Sopenharmony_ciIdentifiers via `ETHTOOL_GSTRINGS` with `string_set` set to `ETH_SS_STATS`,
1208c2ecf20Sopenharmony_ciand values via `ETHTOOL_GSTATS`. User space should use `ETHTOOL_GDRVINFO`
1218c2ecf20Sopenharmony_cito retrieve the number of statistics (`.n_stats`).
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ciethtool-netlink
1248c2ecf20Sopenharmony_ci---------------
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ciEthtool netlink is a replacement for the older IOCTL interface.
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ciProtocol-related statistics can be requested in get commands by setting
1298c2ecf20Sopenharmony_cithe `ETHTOOL_FLAG_STATS` flag in `ETHTOOL_A_HEADER_FLAGS`. Currently
1308c2ecf20Sopenharmony_cistatistics are supported in the following commands:
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci  - `ETHTOOL_MSG_PAUSE_GET`
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cidebugfs
1358c2ecf20Sopenharmony_ci-------
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ciSome drivers expose extra statistics via `debugfs`.
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistruct rtnl_link_stats64
1408c2ecf20Sopenharmony_ci========================
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci.. kernel-doc:: include/uapi/linux/if_link.h
1438c2ecf20Sopenharmony_ci    :identifiers: rtnl_link_stats64
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ciNotes for driver authors
1468c2ecf20Sopenharmony_ci========================
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ciDrivers should report all statistics which have a matching member in
1498c2ecf20Sopenharmony_ci:c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>` exclusively
1508c2ecf20Sopenharmony_civia `.ndo_get_stats64`. Reporting such standard stats via ethtool
1518c2ecf20Sopenharmony_cior debugfs will not be accepted.
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ciDrivers must ensure best possible compliance with
1548c2ecf20Sopenharmony_ci:c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`.
1558c2ecf20Sopenharmony_ciPlease note for example that detailed error statistics must be
1568c2ecf20Sopenharmony_ciadded into the general `rx_error` / `tx_error` counters.
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ciThe `.ndo_get_stats64` callback can not sleep because of accesses
1598c2ecf20Sopenharmony_civia `/proc/net/dev`. If driver may sleep when retrieving the statistics
1608c2ecf20Sopenharmony_cifrom the device it should do so periodically asynchronously and only return
1618c2ecf20Sopenharmony_cia recent copy from `.ndo_get_stats64`. Ethtool interrupt coalescing interface
1628c2ecf20Sopenharmony_ciallows setting the frequency of refreshing statistics, if needed.
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ciRetrieving ethtool statistics is a multi-syscall process, drivers are advised
1658c2ecf20Sopenharmony_cito keep the number of statistics constant to avoid race conditions with
1668c2ecf20Sopenharmony_ciuser space trying to read them.
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ciStatistics must persist across routine operations like bringing the interface
1698c2ecf20Sopenharmony_cidown and up.
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ciKernel-internal data structures
1728c2ecf20Sopenharmony_ci-------------------------------
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ciThe following structures are internal to the kernel, their members are
1758c2ecf20Sopenharmony_citranslated to netlink attributes when dumped. Drivers must not overwrite
1768c2ecf20Sopenharmony_cithe statistics they don't report with 0.
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci- ethtool_pause_stats()
179