xref: /linux-6.15/include/net/netdev_queues.h (revision 5ea73bf3)
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_config - queue-related configuration for a netdev
9  * @hds_thresh:		HDS Threshold value.
10  * @hds_config:		HDS value from userspace.
11  */
12 struct netdev_config {
13 	u32	hds_thresh;
14 	u8	hds_config;
15 };
16 
17 /* See the netdev.yaml spec for definition of each statistic */
18 struct netdev_queue_stats_rx {
19 	u64 bytes;
20 	u64 packets;
21 	u64 alloc_fail;
22 
23 	u64 hw_drops;
24 	u64 hw_drop_overruns;
25 
26 	u64 csum_unnecessary;
27 	u64 csum_none;
28 	u64 csum_bad;
29 
30 	u64 hw_gro_packets;
31 	u64 hw_gro_bytes;
32 	u64 hw_gro_wire_packets;
33 	u64 hw_gro_wire_bytes;
34 
35 	u64 hw_drop_ratelimits;
36 };
37 
38 struct netdev_queue_stats_tx {
39 	u64 bytes;
40 	u64 packets;
41 
42 	u64 hw_drops;
43 	u64 hw_drop_errors;
44 
45 	u64 csum_none;
46 	u64 needs_csum;
47 
48 	u64 hw_gso_packets;
49 	u64 hw_gso_bytes;
50 	u64 hw_gso_wire_packets;
51 	u64 hw_gso_wire_bytes;
52 
53 	u64 hw_drop_ratelimits;
54 
55 	u64 stop;
56 	u64 wake;
57 };
58 
59 /**
60  * struct netdev_stat_ops - netdev ops for fine grained stats
61  * @get_queue_stats_rx:	get stats for a given Rx queue
62  * @get_queue_stats_tx:	get stats for a given Tx queue
63  * @get_base_stats:	get base stats (not belonging to any live instance)
64  *
65  * Query stats for a given object. The values of the statistics are undefined
66  * on entry (specifically they are *not* zero-initialized). Drivers should
67  * assign values only to the statistics they collect. Statistics which are not
68  * collected must be left undefined.
69  *
70  * Queue objects are not necessarily persistent, and only currently active
71  * queues are queried by the per-queue callbacks. This means that per-queue
72  * statistics will not generally add up to the total number of events for
73  * the device. The @get_base_stats callback allows filling in the delta
74  * between events for currently live queues and overall device history.
75  * @get_base_stats can also be used to report any miscellaneous packets
76  * transferred outside of the main set of queues used by the networking stack.
77  * When the statistics for the entire device are queried, first @get_base_stats
78  * is issued to collect the delta, and then a series of per-queue callbacks.
79  * Only statistics which are set in @get_base_stats will be reported
80  * at the device level, meaning that unlike in queue callbacks, setting
81  * a statistic to zero in @get_base_stats is a legitimate thing to do.
82  * This is because @get_base_stats has a second function of designating which
83  * statistics are in fact correct for the entire device (e.g. when history
84  * for some of the events is not maintained, and reliable "total" cannot
85  * be provided).
86  *
87  * Device drivers can assume that when collecting total device stats,
88  * the @get_base_stats and subsequent per-queue calls are performed
89  * "atomically" (without releasing the rtnl_lock).
90  *
91  * Device drivers are encouraged to reset the per-queue statistics when
92  * number of queues change. This is because the primary use case for
93  * per-queue statistics is currently to detect traffic imbalance.
94  */
95 struct netdev_stat_ops {
96 	void (*get_queue_stats_rx)(struct net_device *dev, int idx,
97 				   struct netdev_queue_stats_rx *stats);
98 	void (*get_queue_stats_tx)(struct net_device *dev, int idx,
99 				   struct netdev_queue_stats_tx *stats);
100 	void (*get_base_stats)(struct net_device *dev,
101 			       struct netdev_queue_stats_rx *rx,
102 			       struct netdev_queue_stats_tx *tx);
103 };
104 
105 /**
106  * struct netdev_queue_mgmt_ops - netdev ops for queue management
107  *
108  * @ndo_queue_mem_size: Size of the struct that describes a queue's memory.
109  *
110  * @ndo_queue_mem_alloc: Allocate memory for an RX queue at the specified index.
111  *			 The new memory is written at the specified address.
112  *
113  * @ndo_queue_mem_free:	Free memory from an RX queue.
114  *
115  * @ndo_queue_start:	Start an RX queue with the specified memory and at the
116  *			specified index.
117  *
118  * @ndo_queue_stop:	Stop the RX queue at the specified index. The stopped
119  *			queue's memory is written at the specified address.
120  *
121  * Note that @ndo_queue_mem_alloc and @ndo_queue_mem_free may be called while
122  * the interface is closed. @ndo_queue_start and @ndo_queue_stop will only
123  * be called for an interface which is open.
124  */
125 struct netdev_queue_mgmt_ops {
126 	size_t			ndo_queue_mem_size;
127 	int			(*ndo_queue_mem_alloc)(struct net_device *dev,
128 						       void *per_queue_mem,
129 						       int idx);
130 	void			(*ndo_queue_mem_free)(struct net_device *dev,
131 						      void *per_queue_mem);
132 	int			(*ndo_queue_start)(struct net_device *dev,
133 						   void *per_queue_mem,
134 						   int idx);
135 	int			(*ndo_queue_stop)(struct net_device *dev,
136 						  void *per_queue_mem,
137 						  int idx);
138 };
139 
140 /**
141  * DOC: Lockless queue stopping / waking helpers.
142  *
143  * The netif_txq_maybe_stop() and __netif_txq_completed_wake()
144  * macros are designed to safely implement stopping
145  * and waking netdev queues without full lock protection.
146  *
147  * We assume that there can be no concurrent stop attempts and no concurrent
148  * wake attempts. The try-stop should happen from the xmit handler,
149  * while wake up should be triggered from NAPI poll context.
150  * The two may run concurrently (single producer, single consumer).
151  *
152  * The try-stop side is expected to run from the xmit handler and therefore
153  * it does not reschedule Tx (netif_tx_start_queue() instead of
154  * netif_tx_wake_queue()). Uses of the ``stop`` macros outside of the xmit
155  * handler may lead to xmit queue being enabled but not run.
156  * The waking side does not have similar context restrictions.
157  *
158  * The macros guarantee that rings will not remain stopped if there's
159  * space available, but they do *not* prevent false wake ups when
160  * the ring is full! Drivers should check for ring full at the start
161  * for the xmit handler.
162  *
163  * All descriptor ring indexes (and other relevant shared state) must
164  * be updated before invoking the macros.
165  */
166 
167 #define netif_txq_try_stop(txq, get_desc, start_thrs)			\
168 	({								\
169 		int _res;						\
170 									\
171 		netif_tx_stop_queue(txq);				\
172 		/* Producer index and stop bit must be visible		\
173 		 * to consumer before we recheck.			\
174 		 * Pairs with a barrier in __netif_txq_completed_wake(). \
175 		 */							\
176 		smp_mb__after_atomic();					\
177 									\
178 		/* We need to check again in a case another		\
179 		 * CPU has just made room available.			\
180 		 */							\
181 		_res = 0;						\
182 		if (unlikely(get_desc >= start_thrs)) {			\
183 			netif_tx_start_queue(txq);			\
184 			_res = -1;					\
185 		}							\
186 		_res;							\
187 	})								\
188 
189 /**
190  * netif_txq_maybe_stop() - locklessly stop a Tx queue, if needed
191  * @txq:	struct netdev_queue to stop/start
192  * @get_desc:	get current number of free descriptors (see requirements below!)
193  * @stop_thrs:	minimal number of available descriptors for queue to be left
194  *		enabled
195  * @start_thrs:	minimal number of descriptors to re-enable the queue, can be
196  *		equal to @stop_thrs or higher to avoid frequent waking
197  *
198  * All arguments may be evaluated multiple times, beware of side effects.
199  * @get_desc must be a formula or a function call, it must always
200  * return up-to-date information when evaluated!
201  * Expected to be used from ndo_start_xmit, see the comment on top of the file.
202  *
203  * Returns:
204  *	 0 if the queue was stopped
205  *	 1 if the queue was left enabled
206  *	-1 if the queue was re-enabled (raced with waking)
207  */
208 #define netif_txq_maybe_stop(txq, get_desc, stop_thrs, start_thrs)	\
209 	({								\
210 		int _res;						\
211 									\
212 		_res = 1;						\
213 		if (unlikely(get_desc < stop_thrs))			\
214 			_res = netif_txq_try_stop(txq, get_desc, start_thrs); \
215 		_res;							\
216 	})								\
217 
218 /* Variant of netdev_tx_completed_queue() which guarantees smp_mb() if
219  * @bytes != 0, regardless of kernel config.
220  */
221 static inline void
222 netdev_txq_completed_mb(struct netdev_queue *dev_queue,
223 			unsigned int pkts, unsigned int bytes)
224 {
225 	if (IS_ENABLED(CONFIG_BQL))
226 		netdev_tx_completed_queue(dev_queue, pkts, bytes);
227 	else if (bytes)
228 		smp_mb();
229 }
230 
231 /**
232  * __netif_txq_completed_wake() - locklessly wake a Tx queue, if needed
233  * @txq:	struct netdev_queue to stop/start
234  * @pkts:	number of packets completed
235  * @bytes:	number of bytes completed
236  * @get_desc:	get current number of free descriptors (see requirements below!)
237  * @start_thrs:	minimal number of descriptors to re-enable the queue
238  * @down_cond:	down condition, predicate indicating that the queue should
239  *		not be woken up even if descriptors are available
240  *
241  * All arguments may be evaluated multiple times.
242  * @get_desc must be a formula or a function call, it must always
243  * return up-to-date information when evaluated!
244  * Reports completed pkts/bytes to BQL.
245  *
246  * Returns:
247  *	 0 if the queue was woken up
248  *	 1 if the queue was already enabled (or disabled but @down_cond is true)
249  *	-1 if the queue was left unchanged (@start_thrs not reached)
250  */
251 #define __netif_txq_completed_wake(txq, pkts, bytes,			\
252 				   get_desc, start_thrs, down_cond)	\
253 	({								\
254 		int _res;						\
255 									\
256 		/* Report to BQL and piggy back on its barrier.		\
257 		 * Barrier makes sure that anybody stopping the queue	\
258 		 * after this point sees the new consumer index.	\
259 		 * Pairs with barrier in netif_txq_try_stop().		\
260 		 */							\
261 		netdev_txq_completed_mb(txq, pkts, bytes);		\
262 									\
263 		_res = -1;						\
264 		if (pkts && likely(get_desc >= start_thrs)) {		\
265 			_res = 1;					\
266 			if (unlikely(netif_tx_queue_stopped(txq)) &&	\
267 			    !(down_cond)) {				\
268 				netif_tx_wake_queue(txq);		\
269 				_res = 0;				\
270 			}						\
271 		}							\
272 		_res;							\
273 	})
274 
275 #define netif_txq_completed_wake(txq, pkts, bytes, get_desc, start_thrs) \
276 	__netif_txq_completed_wake(txq, pkts, bytes, get_desc, start_thrs, false)
277 
278 /* subqueue variants follow */
279 
280 #define netif_subqueue_try_stop(dev, idx, get_desc, start_thrs)		\
281 	({								\
282 		struct netdev_queue *txq;				\
283 									\
284 		txq = netdev_get_tx_queue(dev, idx);			\
285 		netif_txq_try_stop(txq, get_desc, start_thrs);		\
286 	})
287 
288 #define netif_subqueue_maybe_stop(dev, idx, get_desc, stop_thrs, start_thrs) \
289 	({								\
290 		struct netdev_queue *txq;				\
291 									\
292 		txq = netdev_get_tx_queue(dev, idx);			\
293 		netif_txq_maybe_stop(txq, get_desc, stop_thrs, start_thrs); \
294 	})
295 
296 #define netif_subqueue_completed_wake(dev, idx, pkts, bytes,		\
297 				      get_desc, start_thrs)		\
298 	({								\
299 		struct netdev_queue *txq;				\
300 									\
301 		txq = netdev_get_tx_queue(dev, idx);			\
302 		netif_txq_completed_wake(txq, pkts, bytes,		\
303 					 get_desc, start_thrs);		\
304 	})
305 
306 #endif
307