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