介面統計

概述

本文件是 Linux 網路介面統計資訊的指南。

Linux 中有三個主要的介面統計資訊來源

  • 基於 struct rtnl_link_stats64 的標準介面統計資訊;

  • 協議特定統計資訊;以及

  • 可透過 ethtool 獲得的驅動程式定義的統計資訊。

標準介面統計資訊

有多個介面可以訪問標準統計資訊。最常用的是來自 iproute2ip 命令

$ ip -s -s link show dev ens4u1u1
6: ens4u1u1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
  link/ether 48:2a:e3:4c:b1:d1 brd ff:ff:ff:ff:ff:ff
  RX: bytes  packets  errors  dropped overrun mcast
  74327665117 69016965 0       0       0       0
  RX errors: length   crc     frame   fifo    missed
             0        0       0       0       0
  TX: bytes  packets  errors  dropped carrier collsns
  21405556176 44608960 0       0       0       0
  TX errors: aborted  fifo   window heartbeat transns
             0        0       0       0       128
  altname enp58s0u1u1

請注意,-s 指定了兩次,以檢視 struct rtnl_link_stats64 的所有成員。如果指定 -s 一次,則不會顯示詳細錯誤。

ip 支援透過 -j 選項進行 JSON 格式化。

佇列統計資訊

佇列統計資訊可以透過 netdev netlink 系列訪問。

目前沒有廣泛分發的 CLI 可以訪問這些統計資訊。核心開發工具 (ynl) 可用於試驗它們,請參閱 使用 Netlink 協議規範

協議特定統計資訊

協議特定統計資訊透過相關介面公開,與用於配置它們的介面相同。

ethtool

Ethtool 公開常見的低階統計資訊。所有標準統計資訊都應由裝置而不是驅動程式維護(與下一節中描述的驅動程式定義的統計資訊相反,後者混合了軟體和硬體統計資訊)。對於包含非託管交換機的裝置(例如,舊式 SR-IOV 或多主機 NIC),計數的事件可能不專門與傳送到本地主機介面的資料包相關。換句話說,事件可以在網路埠(MAC/PHY 塊)計數,而無需為不同的主機端 (PCIe) 裝置進行分離。當內部交換機由 Linux 管理時(NIC 的所謂 switchdev 模式),不應存在這種歧義。

可以透過用於配置的介面訪問標準 ethtool 統計資訊。例如,用於配置暫停幀的 ethtool 介面可以報告相應的硬體計數器

$ ethtool --include-statistics -a eth0
Pause parameters for eth0:
Autonegotiate:        on
RX:                   on
TX:                   on
Statistics:
  tx_pause_frames: 1
  rx_pause_frames: 1

與任何特定功能無關的通用乙太網統計資訊透過 ethtool -S $ifc 透過指定 --groups 引數公開

$ ethtool -S eth0 --groups eth-phy eth-mac eth-ctrl rmon
Stats for eth0:
eth-phy-SymbolErrorDuringCarrier: 0
eth-mac-FramesTransmittedOK: 1
eth-mac-FrameTooLongErrors: 1
eth-ctrl-MACControlFramesTransmitted: 1
eth-ctrl-MACControlFramesReceived: 0
eth-ctrl-UnsupportedOpcodesReceived: 1
rmon-etherStatsUndersizePkts: 1
rmon-etherStatsJabbers: 0
rmon-rx-etherStatsPkts64Octets: 1
rmon-rx-etherStatsPkts65to127Octets: 0
rmon-rx-etherStatsPkts128to255Octets: 0
rmon-tx-etherStatsPkts64Octets: 2
rmon-tx-etherStatsPkts65to127Octets: 3
rmon-tx-etherStatsPkts128to255Octets: 0

驅動程式定義的統計資訊

可以使用 ethtool -S $ifc 轉儲驅動程式定義的 ethtool 統計資訊,例如

$ ethtool -S ens4u1u1
NIC statistics:
   tx_single_collisions: 0
   tx_multi_collisions: 0

uAPI

procfs

歷史的 /proc/net/dev 文字介面提供對介面列表及其統計資訊的訪問。

請注意,即使此介面在內部使用 struct rtnl_link_stats64,它也會合並某些欄位。

sysfs

sysfs 中的每個裝置目錄都包含一個 statistics 目錄(例如,/sys/class/net/lo/statistics/),其中包含與 struct rtnl_link_stats64 成員對應的檔案。

這個簡單的介面特別適用於沒有工具訪問的受限/嵌入式環境。但是,當讀取多個統計資訊時,它效率低下,因為它在內部執行 struct rtnl_link_stats64 的完整轉儲,並且僅報告與訪問的檔案對應的統計資訊。

Sysfs 檔案記錄在 ABI 檔案測試/sysfs-class-net-statistics 中。

ethtool

Ethtool IOCTL 介面允許驅動程式報告特定於實現的統計資訊。從歷史上看,它也用於報告其他 API 不存在的統計資訊,例如每個裝置佇列的統計資訊或基於標準的統計資訊(例如 RFC 2863)。

統計資訊及其字串識別符號是分開檢索的。識別符號透過 ETHTOOL_GSTRINGS,其中 string_set 設定為 ETH_SS_STATS,值透過 ETHTOOL_GSTATS。使用者空間應使用 ETHTOOL_GDRVINFO 來檢索統計資訊的數量 (.n_stats)。

debugfs

一些驅動程式透過 debugfs 公開額外的統計資訊。

驅動程式作者的說明

驅動程式應專門透過 .ndo_get_stats64 報告 struct rtnl_link_stats64 中具有匹配成員的所有統計資訊。透過 ethtool 或 debugfs 報告此類標準統計資訊將不被接受。

驅動程式必須確保儘可能符合 struct rtnl_link_stats64。請注意,例如,詳細的錯誤統計資訊必須新增到常規 rx_error / tx_error 計數器中。

.ndo_get_stats64 回撥不能休眠,因為透過 /proc/net/dev 訪問。如果驅動程式在從裝置檢索統計資訊時可能會休眠,則應定期非同步執行此操作,並且僅從 .ndo_get_stats64 返回最近的副本。 Ethtool 中斷合併介面允許設定重新整理統計資訊的頻率(如果需要)。

檢索 ethtool 統計資訊是一個多系統呼叫過程,建議驅動程式保持統計資訊數量不變,以避免與使用者空間嘗試讀取它們發生競爭條件。

統計資訊必須在例行操作(如關閉和啟動介面)之間保持不變。

核心內部資料結構

以下結構是核心內部的,其成員在轉儲時轉換為 netlink 屬性。驅動程式不得使用 0 覆蓋它們不報告的統計資訊。