Skip to content

Commit ab63a23

Browse files
committed
netdev: add per-queue statistics
The ethtool-nl family does a good job exposing various protocol related and IEEE/IETF statistics which used to get dumped under ethtool -S, with creative names. Queue stats don't have a netlink API, yet, and remain a lion's share of ethtool -S output for new drivers. Not only is that bad because the names differ driver to driver but it's also bug-prone. Intuitively drivers try to report only the stats for active queues, but querying ethtool stats involves multiple system calls, and the number of stats is read separately from the stats themselves. Worse still when user space asks for values of the stats, it doesn't inform the kernel how big the buffer is. If number of stats increases in the meantime kernel will overflow user buffer. Add a netlink API for dumping queue stats. Queue information is exposed via the netdev-genl family, so add the stats there. Support per-queue and sum-for-device dumps. Latter will be useful when subsequent patches add more interesting common stats than just bytes and packets. The API does not currently distinguish between HW and SW stats. The expectation is that the source of the stats will either not matter much (good packets) or be obvious (skb alloc errors). Acked-by: Stanislav Fomichev <[email protected]> Reviewed-by: Amritha Nambiar <[email protected]> Reviewed-by: Xuan Zhuo <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent e8bb2cc commit ab63a23

File tree

9 files changed

+421
-0
lines changed

9 files changed

+421
-0
lines changed

Documentation/netlink/specs/netdev.yaml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ definitions:
7474
name: queue-type
7575
type: enum
7676
entries: [ rx, tx ]
77+
-
78+
name: qstats-scope
79+
type: flags
80+
entries: [ queue ]
7781

7882
attribute-sets:
7983
-
@@ -265,6 +269,66 @@ attribute-sets:
265269
doc: ID of the NAPI instance which services this queue.
266270
type: u32
267271

272+
-
273+
name: qstats
274+
doc: |
275+
Get device statistics, scoped to a device or a queue.
276+
These statistics extend (and partially duplicate) statistics available
277+
in struct rtnl_link_stats64.
278+
Value of the `scope` attribute determines how statistics are
279+
aggregated. When aggregated for the entire device the statistics
280+
represent the total number of events since last explicit reset of
281+
the device (i.e. not a reconfiguration like changing queue count).
282+
When reported per-queue, however, the statistics may not add
283+
up to the total number of events, will only be reported for currently
284+
active objects, and will likely report the number of events since last
285+
reconfiguration.
286+
attributes:
287+
-
288+
name: ifindex
289+
doc: ifindex of the netdevice to which stats belong.
290+
type: u32
291+
checks:
292+
min: 1
293+
-
294+
name: queue-type
295+
doc: Queue type as rx, tx, for queue-id.
296+
type: u32
297+
enum: queue-type
298+
-
299+
name: queue-id
300+
doc: Queue ID, if stats are scoped to a single queue instance.
301+
type: u32
302+
-
303+
name: scope
304+
doc: |
305+
What object type should be used to iterate over the stats.
306+
type: uint
307+
enum: qstats-scope
308+
-
309+
name: rx-packets
310+
doc: |
311+
Number of wire packets successfully received and passed to the stack.
312+
For drivers supporting XDP, XDP is considered the first layer
313+
of the stack, so packets consumed by XDP are still counted here.
314+
type: uint
315+
value: 8 # reserve some attr ids in case we need more metadata later
316+
-
317+
name: rx-bytes
318+
doc: Successfully received bytes, see `rx-packets`.
319+
type: uint
320+
-
321+
name: tx-packets
322+
doc: |
323+
Number of wire packets successfully sent. Packet is considered to be
324+
successfully sent once it is in device memory (usually this means
325+
the device has issued a DMA completion for the packet).
326+
type: uint
327+
-
328+
name: tx-bytes
329+
doc: Successfully sent bytes, see `tx-packets`.
330+
type: uint
331+
268332
operations:
269333
list:
270334
-
@@ -405,6 +469,26 @@ operations:
405469
attributes:
406470
- ifindex
407471
reply: *napi-get-op
472+
-
473+
name: qstats-get
474+
doc: |
475+
Get / dump fine grained statistics. Which statistics are reported
476+
depends on the device and the driver, and whether the driver stores
477+
software counters per-queue.
478+
attribute-set: qstats
479+
dump:
480+
request:
481+
attributes:
482+
- scope
483+
reply:
484+
attributes:
485+
- ifindex
486+
- queue-type
487+
- queue-id
488+
- rx-packets
489+
- rx-bytes
490+
- tx-packets
491+
- tx-bytes
408492

409493
mcast-groups:
410494
list:

Documentation/networking/statistics.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ If `-s` is specified once the detailed errors won't be shown.
4141

4242
`ip` supports JSON formatting via the `-j` option.
4343

44+
Queue statistics
45+
~~~~~~~~~~~~~~~~
46+
47+
Queue statistics are accessible via the netdev netlink family.
48+
49+
Currently no widely distributed CLI exists to access those statistics.
50+
Kernel development tools (ynl) can be used to experiment with them,
51+
see `Documentation/userspace-api/netlink/intro-specs.rst`.
52+
4453
Protocol-specific statistics
4554
----------------------------
4655

@@ -147,6 +156,12 @@ Statistics are reported both in the responses to link information
147156
requests (`RTM_GETLINK`) and statistic requests (`RTM_GETSTATS`,
148157
when `IFLA_STATS_LINK_64` bit is set in the `.filter_mask` of the request).
149158

159+
netdev (netlink)
160+
~~~~~~~~~~~~~~~~
161+
162+
`netdev` generic netlink family allows accessing page pool and per queue
163+
statistics.
164+
150165
ethtool
151166
-------
152167

include/linux/netdevice.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,6 +1955,7 @@ enum netdev_reg_state {
19551955
*
19561956
* @sysfs_rx_queue_group: Space for optional per-rx queue attributes
19571957
* @rtnl_link_ops: Rtnl_link_ops
1958+
* @stat_ops: Optional ops for queue-aware statistics
19581959
*
19591960
* @gso_max_size: Maximum size of generic segmentation offload
19601961
* @tso_max_size: Device (as in HW) limit on the max TSO request size
@@ -2335,6 +2336,8 @@ struct net_device {
23352336

23362337
const struct rtnl_link_ops *rtnl_link_ops;
23372338

2339+
const struct netdev_stat_ops *stat_ops;
2340+
23382341
/* for setting kernel sock attribute on TCP connection setup */
23392342
#define GSO_MAX_SEGS 65535u
23402343
#define GSO_LEGACY_MAX_SIZE 65536u

include/net/netdev_queues.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,60 @@
44

55
#include <linux/netdevice.h>
66

7+
struct netdev_queue_stats_rx {
8+
u64 bytes;
9+
u64 packets;
10+
};
11+
12+
struct netdev_queue_stats_tx {
13+
u64 bytes;
14+
u64 packets;
15+
};
16+
17+
/**
18+
* struct netdev_stat_ops - netdev ops for fine grained stats
19+
* @get_queue_stats_rx: get stats for a given Rx queue
20+
* @get_queue_stats_tx: get stats for a given Tx queue
21+
* @get_base_stats: get base stats (not belonging to any live instance)
22+
*
23+
* Query stats for a given object. The values of the statistics are undefined
24+
* on entry (specifically they are *not* zero-initialized). Drivers should
25+
* assign values only to the statistics they collect. Statistics which are not
26+
* collected must be left undefined.
27+
*
28+
* Queue objects are not necessarily persistent, and only currently active
29+
* queues are queried by the per-queue callbacks. This means that per-queue
30+
* statistics will not generally add up to the total number of events for
31+
* the device. The @get_base_stats callback allows filling in the delta
32+
* between events for currently live queues and overall device history.
33+
* When the statistics for the entire device are queried, first @get_base_stats
34+
* is issued to collect the delta, and then a series of per-queue callbacks.
35+
* Only statistics which are set in @get_base_stats will be reported
36+
* at the device level, meaning that unlike in queue callbacks, setting
37+
* a statistic to zero in @get_base_stats is a legitimate thing to do.
38+
* This is because @get_base_stats has a second function of designating which
39+
* statistics are in fact correct for the entire device (e.g. when history
40+
* for some of the events is not maintained, and reliable "total" cannot
41+
* be provided).
42+
*
43+
* Device drivers can assume that when collecting total device stats,
44+
* the @get_base_stats and subsequent per-queue calls are performed
45+
* "atomically" (without releasing the rtnl_lock).
46+
*
47+
* Device drivers are encouraged to reset the per-queue statistics when
48+
* number of queues change. This is because the primary use case for
49+
* per-queue statistics is currently to detect traffic imbalance.
50+
*/
51+
struct netdev_stat_ops {
52+
void (*get_queue_stats_rx)(struct net_device *dev, int idx,
53+
struct netdev_queue_stats_rx *stats);
54+
void (*get_queue_stats_tx)(struct net_device *dev, int idx,
55+
struct netdev_queue_stats_tx *stats);
56+
void (*get_base_stats)(struct net_device *dev,
57+
struct netdev_queue_stats_rx *rx,
58+
struct netdev_queue_stats_tx *tx);
59+
};
60+
761
/**
862
* DOC: Lockless queue stopping / waking helpers.
963
*

include/uapi/linux/netdev.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ enum netdev_queue_type {
7070
NETDEV_QUEUE_TYPE_TX,
7171
};
7272

73+
enum netdev_qstats_scope {
74+
NETDEV_QSTATS_SCOPE_QUEUE = 1,
75+
};
76+
7377
enum {
7478
NETDEV_A_DEV_IFINDEX = 1,
7579
NETDEV_A_DEV_PAD,
@@ -132,6 +136,20 @@ enum {
132136
NETDEV_A_QUEUE_MAX = (__NETDEV_A_QUEUE_MAX - 1)
133137
};
134138

139+
enum {
140+
NETDEV_A_QSTATS_IFINDEX = 1,
141+
NETDEV_A_QSTATS_QUEUE_TYPE,
142+
NETDEV_A_QSTATS_QUEUE_ID,
143+
NETDEV_A_QSTATS_SCOPE,
144+
NETDEV_A_QSTATS_RX_PACKETS = 8,
145+
NETDEV_A_QSTATS_RX_BYTES,
146+
NETDEV_A_QSTATS_TX_PACKETS,
147+
NETDEV_A_QSTATS_TX_BYTES,
148+
149+
__NETDEV_A_QSTATS_MAX,
150+
NETDEV_A_QSTATS_MAX = (__NETDEV_A_QSTATS_MAX - 1)
151+
};
152+
135153
enum {
136154
NETDEV_CMD_DEV_GET = 1,
137155
NETDEV_CMD_DEV_ADD_NTF,
@@ -144,6 +162,7 @@ enum {
144162
NETDEV_CMD_PAGE_POOL_STATS_GET,
145163
NETDEV_CMD_QUEUE_GET,
146164
NETDEV_CMD_NAPI_GET,
165+
NETDEV_CMD_QSTATS_GET,
147166

148167
__NETDEV_CMD_MAX,
149168
NETDEV_CMD_MAX = (__NETDEV_CMD_MAX - 1)

net/core/netdev-genl-gen.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ static const struct nla_policy netdev_napi_get_dump_nl_policy[NETDEV_A_NAPI_IFIN
6868
[NETDEV_A_NAPI_IFINDEX] = NLA_POLICY_MIN(NLA_U32, 1),
6969
};
7070

71+
/* NETDEV_CMD_QSTATS_GET - dump */
72+
static const struct nla_policy netdev_qstats_get_nl_policy[NETDEV_A_QSTATS_SCOPE + 1] = {
73+
[NETDEV_A_QSTATS_SCOPE] = NLA_POLICY_MASK(NLA_UINT, 0x1),
74+
};
75+
7176
/* Ops table for netdev */
7277
static const struct genl_split_ops netdev_nl_ops[] = {
7378
{
@@ -138,6 +143,13 @@ static const struct genl_split_ops netdev_nl_ops[] = {
138143
.maxattr = NETDEV_A_NAPI_IFINDEX,
139144
.flags = GENL_CMD_CAP_DUMP,
140145
},
146+
{
147+
.cmd = NETDEV_CMD_QSTATS_GET,
148+
.dumpit = netdev_nl_qstats_get_dumpit,
149+
.policy = netdev_qstats_get_nl_policy,
150+
.maxattr = NETDEV_A_QSTATS_SCOPE,
151+
.flags = GENL_CMD_CAP_DUMP,
152+
},
141153
};
142154

143155
static const struct genl_multicast_group netdev_nl_mcgrps[] = {

net/core/netdev-genl-gen.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ int netdev_nl_queue_get_dumpit(struct sk_buff *skb,
2828
struct netlink_callback *cb);
2929
int netdev_nl_napi_get_doit(struct sk_buff *skb, struct genl_info *info);
3030
int netdev_nl_napi_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb);
31+
int netdev_nl_qstats_get_dumpit(struct sk_buff *skb,
32+
struct netlink_callback *cb);
3133

3234
enum {
3335
NETDEV_NLGRP_MGMT,

0 commit comments

Comments
 (0)