statistics.rst 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ====================
  3. Interface statistics
  4. ====================
  5. Overview
  6. ========
  7. This document is a guide to Linux network interface statistics.
  8. There are three main sources of interface statistics in Linux:
  9. - standard interface statistics based on
  10. :c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`;
  11. - protocol-specific statistics; and
  12. - driver-defined statistics available via ethtool.
  13. Standard interface statistics
  14. -----------------------------
  15. There are multiple interfaces to reach the standard statistics.
  16. Most commonly used is the `ip` command from `iproute2`::
  17. $ ip -s -s link show dev ens4u1u1
  18. 6: ens4u1u1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
  19. link/ether 48:2a:e3:4c:b1:d1 brd ff:ff:ff:ff:ff:ff
  20. RX: bytes packets errors dropped overrun mcast
  21. 74327665117 69016965 0 0 0 0
  22. RX errors: length crc frame fifo missed
  23. 0 0 0 0 0
  24. TX: bytes packets errors dropped carrier collsns
  25. 21405556176 44608960 0 0 0 0
  26. TX errors: aborted fifo window heartbeat transns
  27. 0 0 0 0 128
  28. altname enp58s0u1u1
  29. Note that `-s` has been specified twice to see all members of
  30. :c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`.
  31. If `-s` is specified once the detailed errors won't be shown.
  32. `ip` supports JSON formatting via the `-j` option.
  33. Queue statistics
  34. ~~~~~~~~~~~~~~~~
  35. Queue statistics are accessible via the netdev netlink family.
  36. Currently no widely distributed CLI exists to access those statistics.
  37. Kernel development tools (ynl) can be used to experiment with them,
  38. see `Documentation/userspace-api/netlink/intro-specs.rst`.
  39. Protocol-specific statistics
  40. ----------------------------
  41. Protocol-specific statistics are exposed via relevant interfaces,
  42. the same interfaces as are used to configure them.
  43. ethtool
  44. ~~~~~~~
  45. Ethtool exposes common low-level statistics.
  46. All the standard statistics are expected to be maintained
  47. by the device, not the driver (as opposed to driver-defined stats
  48. described in the next section which mix software and hardware stats).
  49. For devices which contain unmanaged
  50. switches (e.g. legacy SR-IOV or multi-host NICs) the events counted
  51. may not pertain exclusively to the packets destined to
  52. the local host interface. In other words the events may
  53. be counted at the network port (MAC/PHY blocks) without separation
  54. for different host side (PCIe) devices. Such ambiguity must not
  55. be present when internal switch is managed by Linux (so called
  56. switchdev mode for NICs).
  57. Standard ethtool statistics can be accessed via the interfaces used
  58. for configuration. For example ethtool interface used
  59. to configure pause frames can report corresponding hardware counters::
  60. $ ethtool --include-statistics -a eth0
  61. Pause parameters for eth0:
  62. Autonegotiate: on
  63. RX: on
  64. TX: on
  65. Statistics:
  66. tx_pause_frames: 1
  67. rx_pause_frames: 1
  68. General Ethernet statistics not associated with any particular
  69. functionality are exposed via ``ethtool -S $ifc`` by specifying
  70. the ``--groups`` parameter::
  71. $ ethtool -S eth0 --groups eth-phy eth-mac eth-ctrl rmon
  72. Stats for eth0:
  73. eth-phy-SymbolErrorDuringCarrier: 0
  74. eth-mac-FramesTransmittedOK: 1
  75. eth-mac-FrameTooLongErrors: 1
  76. eth-ctrl-MACControlFramesTransmitted: 1
  77. eth-ctrl-MACControlFramesReceived: 0
  78. eth-ctrl-UnsupportedOpcodesReceived: 1
  79. rmon-etherStatsUndersizePkts: 1
  80. rmon-etherStatsJabbers: 0
  81. rmon-rx-etherStatsPkts64Octets: 1
  82. rmon-rx-etherStatsPkts65to127Octets: 0
  83. rmon-rx-etherStatsPkts128to255Octets: 0
  84. rmon-tx-etherStatsPkts64Octets: 2
  85. rmon-tx-etherStatsPkts65to127Octets: 3
  86. rmon-tx-etherStatsPkts128to255Octets: 0
  87. Driver-defined statistics
  88. -------------------------
  89. Driver-defined ethtool statistics can be dumped using `ethtool -S $ifc`, e.g.::
  90. $ ethtool -S ens4u1u1
  91. NIC statistics:
  92. tx_single_collisions: 0
  93. tx_multi_collisions: 0
  94. uAPIs
  95. =====
  96. procfs
  97. ------
  98. The historical `/proc/net/dev` text interface gives access to the list
  99. of interfaces as well as their statistics.
  100. Note that even though this interface is using
  101. :c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`
  102. internally it combines some of the fields.
  103. sysfs
  104. -----
  105. Each device directory in sysfs contains a `statistics` directory (e.g.
  106. `/sys/class/net/lo/statistics/`) with files corresponding to
  107. members of :c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`.
  108. This simple interface is convenient especially in constrained/embedded
  109. environments without access to tools. However, it's inefficient when
  110. reading multiple stats as it internally performs a full dump of
  111. :c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`
  112. and reports only the stat corresponding to the accessed file.
  113. Sysfs files are documented in
  114. Documentation/ABI/testing/sysfs-class-net-statistics.
  115. netlink
  116. -------
  117. `rtnetlink` (`NETLINK_ROUTE`) is the preferred method of accessing
  118. :c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>` stats.
  119. Statistics are reported both in the responses to link information
  120. requests (`RTM_GETLINK`) and statistic requests (`RTM_GETSTATS`,
  121. when `IFLA_STATS_LINK_64` bit is set in the `.filter_mask` of the request).
  122. netdev (netlink)
  123. ~~~~~~~~~~~~~~~~
  124. `netdev` generic netlink family allows accessing page pool and per queue
  125. statistics.
  126. ethtool
  127. -------
  128. Ethtool IOCTL interface allows drivers to report implementation
  129. specific statistics. Historically it has also been used to report
  130. statistics for which other APIs did not exist, like per-device-queue
  131. statistics, or standard-based statistics (e.g. RFC 2863).
  132. Statistics and their string identifiers are retrieved separately.
  133. Identifiers via `ETHTOOL_GSTRINGS` with `string_set` set to `ETH_SS_STATS`,
  134. and values via `ETHTOOL_GSTATS`. User space should use `ETHTOOL_GDRVINFO`
  135. to retrieve the number of statistics (`.n_stats`).
  136. ethtool-netlink
  137. ---------------
  138. Ethtool netlink is a replacement for the older IOCTL interface.
  139. Protocol-related statistics can be requested in get commands by setting
  140. the `ETHTOOL_FLAG_STATS` flag in `ETHTOOL_A_HEADER_FLAGS`. Currently
  141. statistics are supported in the following commands:
  142. - `ETHTOOL_MSG_FEC_GET`
  143. - `ETHTOOL_MSG_LINKSTATE_GET`
  144. - `ETHTOOL_MSG_MM_GET`
  145. - `ETHTOOL_MSG_PAUSE_GET`
  146. - `ETHTOOL_MSG_TSINFO_GET`
  147. debugfs
  148. -------
  149. Some drivers expose extra statistics via `debugfs`.
  150. struct rtnl_link_stats64
  151. ========================
  152. .. kernel-doc:: include/uapi/linux/if_link.h
  153. :identifiers: rtnl_link_stats64
  154. Notes for driver authors
  155. ========================
  156. Drivers should report all statistics which have a matching member in
  157. :c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>` exclusively
  158. via `.ndo_get_stats64`. Reporting such standard stats via ethtool
  159. or debugfs will not be accepted.
  160. Drivers must ensure best possible compliance with
  161. :c:type:`struct rtnl_link_stats64 <rtnl_link_stats64>`.
  162. Please note for example that detailed error statistics must be
  163. added into the general `rx_error` / `tx_error` counters.
  164. The `.ndo_get_stats64` callback can not sleep because of accesses
  165. via `/proc/net/dev`. If driver may sleep when retrieving the statistics
  166. from the device it should do so periodically asynchronously and only return
  167. a recent copy from `.ndo_get_stats64`. Ethtool interrupt coalescing interface
  168. allows setting the frequency of refreshing statistics, if needed.
  169. Retrieving ethtool statistics is a multi-syscall process, drivers are advised
  170. to keep the number of statistics constant to avoid race conditions with
  171. user space trying to read them.
  172. Statistics must persist across routine operations like bringing the interface
  173. down and up.
  174. Kernel-internal data structures
  175. -------------------------------
  176. The following structures are internal to the kernel, their members are
  177. translated to netlink attributes when dumped. Drivers must not overwrite
  178. the statistics they don't report with 0.
  179. - ethtool_pause_stats()
  180. - ethtool_fec_stats()