xref: /linux/include/net/netdev_queues.h (revision 0e59523b440c90ed1899476f32ad4e949282f43e)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_NET_QUEUES_H
3 #define _LINUX_NET_QUEUES_H
4 
5 #include <linux/netdevice.h>
6 
7 /**
8  * struct netdev_ring_config - accepted RX ring depth configuration
9  * @rx_pending:		Size of the regular RX ring.
10  * @rx_mini_pending:	Size of the RX mini ring.
11  * @rx_jumbo_pending:	Size of the RX jumbo ring.
12  *
13  * This stores only persistent configuration values. Capability fields,
14  * such as max ring sizes, are reported by drivers but are not part of the
15  * accepted configuration.
16  *
17  * These values are only used for queue-configuration validation today.
18  * Drivers which normalize ring sizes must update the struct ethtool_ringparam
19  * passed to set_ringparam() with the applied sizes.
20  */
21 struct netdev_ring_config {
22 	u32	rx_pending;
23 	u32	rx_mini_pending;
24 	u32	rx_jumbo_pending;
25 };
26 
27 /**
28  * struct netdev_config - queue-related configuration for a netdev
29  * @hds_thresh:		HDS Threshold value.
30  * @hds_config:		HDS value from userspace.
31  * @rings:		Accepted RX ring depths.
32  *
33  * Direct values, such as @hds_thresh and @rings, hold the accepted
34  * configuration and always override callback-provided defaults, including
35  * when zero. Drivers which use @rings for queue rendering must initialize
36  * every RX ring depth before registering the netdev.
37  */
38 struct netdev_config {
39 	u32	hds_thresh;
40 	u8	hds_config;
41 
42 	struct netdev_ring_config rings;
43 };
44 
45 /**
46  * struct netdev_queue_config - rendered configuration for an RX queue
47  * @rx_page_size:	Size of one RX page-pool allocation.
48  * @rx_ring_size:	Configured size of the regular RX ring.
49  * @rx_mini_ring_size:	Configured size of the RX mini ring.
50  * @rx_jumbo_ring_size:	Configured size of the RX jumbo ring.
51  */
52 struct netdev_queue_config {
53 	u32	rx_page_size;
54 	u32	rx_ring_size;
55 	u32	rx_mini_ring_size;
56 	u32	rx_jumbo_ring_size;
57 };
58 
59 /* See the netdev.yaml spec for definition of each statistic */
60 struct netdev_queue_stats_rx {
61 	u64 bytes;
62 	u64 packets;
63 	u64 alloc_fail;
64 
65 	u64 hw_drops;
66 	u64 hw_drop_overruns;
67 
68 	u64 csum_complete;
69 	u64 csum_unnecessary;
70 	u64 csum_none;
71 	u64 csum_bad;
72 
73 	u64 hw_gro_packets;
74 	u64 hw_gro_bytes;
75 	u64 hw_gro_wire_packets;
76 	u64 hw_gro_wire_bytes;
77 
78 	u64 hw_drop_ratelimits;
79 };
80 
81 struct netdev_queue_stats_tx {
82 	u64 bytes;
83 	u64 packets;
84 
85 	u64 hw_drops;
86 	u64 hw_drop_errors;
87 
88 	u64 csum_none;
89 	u64 needs_csum;
90 
91 	u64 hw_gso_packets;
92 	u64 hw_gso_bytes;
93 	u64 hw_gso_wire_packets;
94 	u64 hw_gso_wire_bytes;
95 
96 	u64 hw_drop_ratelimits;
97 
98 	u64 stop;
99 	u64 wake;
100 };
101 
102 /**
103  * struct netdev_stat_ops - netdev ops for fine grained stats
104  * @get_queue_stats_rx:	get stats for a given Rx queue
105  * @get_queue_stats_tx:	get stats for a given Tx queue
106  * @get_base_stats:	get base stats (not belonging to any live instance)
107  *
108  * Query stats for a given object. The values of the statistics are undefined
109  * on entry (specifically they are *not* zero-initialized). Drivers should
110  * assign values only to the statistics they collect. Statistics which are not
111  * collected must be left undefined.
112  *
113  * Queue objects are not necessarily persistent, and only currently active
114  * queues are queried by the per-queue callbacks. This means that per-queue
115  * statistics will not generally add up to the total number of events for
116  * the device. The @get_base_stats callback allows filling in the delta
117  * between events for currently live queues and overall device history.
118  * @get_base_stats can also be used to report any miscellaneous packets
119  * transferred outside of the main set of queues used by the networking stack.
120  * When the statistics for the entire device are queried, first @get_base_stats
121  * is issued to collect the delta, and then a series of per-queue callbacks.
122  * Only statistics which are set in @get_base_stats will be reported
123  * at the device level, meaning that unlike in queue callbacks, setting
124  * a statistic to zero in @get_base_stats is a legitimate thing to do.
125  * This is because @get_base_stats has a second function of designating which
126  * statistics are in fact correct for the entire device (e.g. when history
127  * for some of the events is not maintained, and reliable "total" cannot
128  * be provided).
129  *
130  * Ops are called under the instance lock if netdev_need_ops_lock()
131  * returns true, otherwise under rtnl_lock.
132  * Device drivers can assume that when collecting total device stats,
133  * the @get_base_stats and subsequent per-queue calls are performed
134  * "atomically" (without releasing the relevant lock).
135  *
136  * Device drivers are encouraged to reset the per-queue statistics when
137  * number of queues change. This is because the primary use case for
138  * per-queue statistics is currently to detect traffic imbalance.
139  */
140 struct netdev_stat_ops {
141 	void (*get_queue_stats_rx)(struct net_device *dev, int idx,
142 				   struct netdev_queue_stats_rx *stats);
143 	void (*get_queue_stats_tx)(struct net_device *dev, int idx,
144 				   struct netdev_queue_stats_tx *stats);
145 	void (*get_base_stats)(struct net_device *dev,
146 			       struct netdev_queue_stats_rx *rx,
147 			       struct netdev_queue_stats_tx *tx);
148 };
149 
150 void netdev_stat_queue_sum(struct net_device *netdev,
151 			   int rx_start, int rx_end,
152 			   struct netdev_queue_stats_rx *rx_sum,
153 			   int tx_start, int tx_end,
154 			   struct netdev_queue_stats_tx *tx_sum);
155 
156 enum {
157 	/* The queue checks and honours the page size qcfg parameter */
158 	QCFG_RX_PAGE_SIZE	= 0x1,
159 };
160 
161 /**
162  * struct netdev_queue_mgmt_ops - netdev ops for queue management
163  *
164  * @ndo_queue_mem_size: Size of the struct that describes a queue's memory.
165  *
166  * @ndo_queue_mem_alloc: Allocate memory for an RX queue at the specified index.
167  *			 The new memory is written at the specified address.
168  *
169  * @ndo_queue_mem_free:	Free memory from an RX queue.
170  *
171  * @ndo_queue_start:	Start an RX queue with the specified memory and at the
172  *			specified index.
173  *
174  * @ndo_queue_stop:	Stop the RX queue at the specified index. The stopped
175  *			queue's memory is written at the specified address.
176  *
177  * @ndo_queue_get_dma_dev: Get dma device for zero-copy operations to be used
178  *			   for this queue. Return NULL on error.
179  *
180  * @ndo_default_qcfg:	(Optional) Populate queue config with defaults. Queue
181  *			config structs are passed to this helper before the
182  *			user-requested settings are applied. Ring depths from
183  *			dev->cfg override these defaults. Drivers which consume
184  *			them must initialize dev->cfg->rings before registering
185  *			the netdev.
186  *
187  * @ndo_validate_qcfg: (Optional) Check if queue config is supported.
188  *			Called when configuration affecting a queue may be
189  *			changing. When NIC-wide config is changed the
190  *			callback will be invoked for the defaults and all
191  *			queue overrides. Drivers which normalize device-wide
192  *			values when applying them must use the same
193  *			normalization during validation.
194  *
195  * @ndo_queue_create:	Create a new RX queue on a virtual device that will
196  *			be paired with a physical device's queue via leasing.
197  *			Return the new queue id on success, negative error
198  *			on failure.
199  *
200  * @supported_params:	Bitmask of supported parameters, see QCFG_*.
201  *
202  * Note that @ndo_queue_mem_alloc and @ndo_queue_mem_free may be called while
203  * the interface is closed. @ndo_queue_start and @ndo_queue_stop will only
204  * be called for an interface which is open.
205  */
206 struct netdev_queue_mgmt_ops {
207 	size_t	ndo_queue_mem_size;
208 	int	(*ndo_queue_mem_alloc)(struct net_device *dev,
209 				       struct netdev_queue_config *qcfg,
210 				       void *per_queue_mem,
211 				       int idx);
212 	void	(*ndo_queue_mem_free)(struct net_device *dev,
213 				      void *per_queue_mem);
214 	int	(*ndo_queue_start)(struct net_device *dev,
215 				   struct netdev_queue_config *qcfg,
216 				   void *per_queue_mem,
217 				   int idx);
218 	int	(*ndo_queue_stop)(struct net_device *dev,
219 				  void *per_queue_mem,
220 				  int idx);
221 	void	(*ndo_default_qcfg)(struct net_device *dev,
222 				    struct netdev_queue_config *qcfg);
223 	int	(*ndo_validate_qcfg)(struct net_device *dev,
224 				     struct netdev_queue_config *qcfg,
225 				     struct netlink_ext_ack *extack);
226 	struct device *	(*ndo_queue_get_dma_dev)(struct net_device *dev,
227 						 int idx);
228 	int	(*ndo_queue_create)(struct net_device *dev,
229 				    struct netlink_ext_ack *extack);
230 
231 	unsigned int supported_params;
232 };
233 
234 void netdev_queue_config(struct net_device *dev, int rxq,
235 			 struct netdev_queue_config *qcfg);
236 
237 bool netif_rxq_has_unreadable_mp(struct net_device *dev, unsigned int rxq_idx);
238 
239 /**
240  * DOC: Lockless queue stopping / waking helpers.
241  *
242  * The netif_txq_maybe_stop() and __netif_txq_completed_wake()
243  * macros are designed to safely implement stopping
244  * and waking netdev queues without full lock protection.
245  *
246  * We assume that there can be no concurrent stop attempts and no concurrent
247  * wake attempts. The try-stop should happen from the xmit handler,
248  * while wake up should be triggered from NAPI poll context.
249  * The two may run concurrently (single producer, single consumer).
250  *
251  * The try-stop side is expected to run from the xmit handler and therefore
252  * it does not reschedule Tx (netif_tx_start_queue() instead of
253  * netif_tx_wake_queue()). Uses of the ``stop`` macros outside of the xmit
254  * handler may lead to xmit queue being enabled but not run.
255  * The waking side does not have similar context restrictions.
256  *
257  * The macros guarantee that rings will not remain stopped if there's
258  * space available, but they do *not* prevent false wake ups when
259  * the ring is full! Drivers should check for ring full at the start
260  * for the xmit handler.
261  *
262  * All descriptor ring indexes (and other relevant shared state) must
263  * be updated before invoking the macros.
264  */
265 
266 #define netif_txq_try_stop(txq, get_desc, start_thrs)			\
267 	({								\
268 		int _res;						\
269 									\
270 		netif_tx_stop_queue(txq);				\
271 		/* Producer index and stop bit must be visible		\
272 		 * to consumer before we recheck.			\
273 		 * Pairs with a barrier in __netif_txq_completed_wake(). \
274 		 */							\
275 		smp_mb__after_atomic();					\
276 									\
277 		/* We need to check again in a case another		\
278 		 * CPU has just made room available.			\
279 		 */							\
280 		_res = 0;						\
281 		if (unlikely(get_desc >= start_thrs)) {			\
282 			netif_tx_start_queue(txq);			\
283 			_res = -1;					\
284 		}							\
285 		_res;							\
286 	})								\
287 
288 /**
289  * netif_txq_maybe_stop() - locklessly stop a Tx queue, if needed
290  * @txq:	struct netdev_queue to stop/start
291  * @get_desc:	get current number of free descriptors (see requirements below!)
292  * @stop_thrs:	minimal number of available descriptors for queue to be left
293  *		enabled
294  * @start_thrs:	minimal number of descriptors to re-enable the queue, can be
295  *		equal to @stop_thrs or higher to avoid frequent waking
296  *
297  * All arguments may be evaluated multiple times, beware of side effects.
298  * @get_desc must be a formula or a function call, it must always
299  * return up-to-date information when evaluated!
300  * Expected to be used from ndo_start_xmit, see the comment on top of the file.
301  *
302  * Returns:
303  *	 0 if the queue was stopped
304  *	 1 if the queue was left enabled
305  *	-1 if the queue was re-enabled (raced with waking)
306  */
307 #define netif_txq_maybe_stop(txq, get_desc, stop_thrs, start_thrs)	\
308 	({								\
309 		int _res;						\
310 									\
311 		_res = 1;						\
312 		if (unlikely(get_desc < stop_thrs))			\
313 			_res = netif_txq_try_stop(txq, get_desc, start_thrs); \
314 		_res;							\
315 	})								\
316 
317 /* Variant of netdev_tx_completed_queue() which guarantees smp_mb() if
318  * @bytes != 0, regardless of kernel config.
319  */
320 static inline void
321 netdev_txq_completed_mb(struct netdev_queue *dev_queue,
322 			unsigned int pkts, unsigned int bytes)
323 {
324 	if (IS_ENABLED(CONFIG_BQL))
325 		netdev_tx_completed_queue(dev_queue, pkts, bytes);
326 	else if (bytes)
327 		smp_mb();
328 }
329 
330 /**
331  * __netif_txq_completed_wake() - locklessly wake a Tx queue, if needed
332  * @txq:	struct netdev_queue to stop/start
333  * @pkts:	number of packets completed
334  * @bytes:	number of bytes completed
335  * @get_desc:	get current number of free descriptors (see requirements below!)
336  * @start_thrs:	minimal number of descriptors to re-enable the queue
337  * @down_cond:	down condition, predicate indicating that the queue should
338  *		not be woken up even if descriptors are available
339  *
340  * All arguments may be evaluated multiple times.
341  * @get_desc must be a formula or a function call, it must always
342  * return up-to-date information when evaluated!
343  * Reports completed pkts/bytes to BQL.
344  *
345  * Returns:
346  *	 0 if the queue was woken up
347  *	 1 if the queue was already enabled (or disabled but @down_cond is true)
348  *	-1 if the queue was left unchanged (@start_thrs not reached)
349  */
350 #define __netif_txq_completed_wake(txq, pkts, bytes,			\
351 				   get_desc, start_thrs, down_cond)	\
352 	({								\
353 		int _res;						\
354 									\
355 		/* Report to BQL and piggy back on its barrier.		\
356 		 * Barrier makes sure that anybody stopping the queue	\
357 		 * after this point sees the new consumer index.	\
358 		 * Pairs with barrier in netif_txq_try_stop().		\
359 		 */							\
360 		netdev_txq_completed_mb(txq, pkts, bytes);		\
361 									\
362 		_res = -1;						\
363 		if (pkts && likely(get_desc >= start_thrs)) {		\
364 			_res = 1;					\
365 			if (unlikely(netif_tx_queue_stopped(txq)) &&	\
366 			    !(down_cond)) {				\
367 				netif_tx_wake_queue(txq);		\
368 				_res = 0;				\
369 			}						\
370 		}							\
371 		_res;							\
372 	})
373 
374 #define netif_txq_completed_wake(txq, pkts, bytes, get_desc, start_thrs) \
375 	__netif_txq_completed_wake(txq, pkts, bytes, get_desc, start_thrs, false)
376 
377 /* subqueue variants follow */
378 
379 #define netif_subqueue_try_stop(dev, idx, get_desc, start_thrs)		\
380 	({								\
381 		struct netdev_queue *_txq;				\
382 									\
383 		_txq = netdev_get_tx_queue(dev, idx);			\
384 		netif_txq_try_stop(_txq, get_desc, start_thrs);		\
385 	})
386 
387 static inline void netif_subqueue_sent(const struct net_device *dev,
388 				       unsigned int idx, unsigned int bytes)
389 {
390 	struct netdev_queue *txq;
391 
392 	txq = netdev_get_tx_queue(dev, idx);
393 	netdev_tx_sent_queue(txq, bytes);
394 }
395 
396 static inline unsigned int netif_xmit_timeout_ms(struct netdev_queue *txq)
397 {
398 	unsigned long trans_start = READ_ONCE(txq->trans_start);
399 
400 	if (netif_xmit_stopped(txq) &&
401 	    time_after(jiffies, trans_start + txq->dev->watchdog_timeo))
402 		return jiffies_to_msecs(jiffies - trans_start);
403 
404 	return 0;
405 }
406 
407 #define netif_subqueue_maybe_stop(dev, idx, get_desc, stop_thrs, start_thrs) \
408 	({								\
409 		struct netdev_queue *_txq;				\
410 									\
411 		_txq = netdev_get_tx_queue(dev, idx);			\
412 		netif_txq_maybe_stop(_txq, get_desc, stop_thrs, start_thrs); \
413 	})
414 
415 #define netif_subqueue_completed_wake(dev, idx, pkts, bytes,		\
416 				      get_desc, start_thrs)		\
417 	({								\
418 		struct netdev_queue *_txq;				\
419 									\
420 		_txq = netdev_get_tx_queue(dev, idx);			\
421 		netif_txq_completed_wake(_txq, pkts, bytes,		\
422 					 get_desc, start_thrs);		\
423 	})
424 
425 struct device *netdev_queue_get_dma_dev(struct net_device *dev,
426 					unsigned int idx,
427 					enum netdev_queue_type type);
428 bool netdev_can_create_queue(const struct net_device *dev,
429 			     struct netlink_ext_ack *extack);
430 bool netdev_can_lease_queue(const struct net_device *dev,
431 			    struct netlink_ext_ack *extack);
432 bool netdev_queue_busy(struct net_device *dev, unsigned int idx,
433 		       enum netdev_queue_type type,
434 		       struct netlink_ext_ack *extack);
435 #endif /* _LINUX_NET_QUEUES_H */
436