xref: /linux/include/net/netdev_queues.h (revision e958da0ddbe831197a0023251880a4a09d5ba268)
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  * DOC: Lockless queue stopping / waking helpers.
92  *
93  * The netif_txq_maybe_stop() and __netif_txq_completed_wake()
94  * macros are designed to safely implement stopping
95  * and waking netdev queues without full lock protection.
96  *
97  * We assume that there can be no concurrent stop attempts and no concurrent
98  * wake attempts. The try-stop should happen from the xmit handler,
99  * while wake up should be triggered from NAPI poll context.
100  * The two may run concurrently (single producer, single consumer).
101  *
102  * The try-stop side is expected to run from the xmit handler and therefore
103  * it does not reschedule Tx (netif_tx_start_queue() instead of
104  * netif_tx_wake_queue()). Uses of the ``stop`` macros outside of the xmit
105  * handler may lead to xmit queue being enabled but not run.
106  * The waking side does not have similar context restrictions.
107  *
108  * The macros guarantee that rings will not remain stopped if there's
109  * space available, but they do *not* prevent false wake ups when
110  * the ring is full! Drivers should check for ring full at the start
111  * for the xmit handler.
112  *
113  * All descriptor ring indexes (and other relevant shared state) must
114  * be updated before invoking the macros.
115  */
116 
117 #define netif_txq_try_stop(txq, get_desc, start_thrs)			\
118 	({								\
119 		int _res;						\
120 									\
121 		netif_tx_stop_queue(txq);				\
122 		/* Producer index and stop bit must be visible		\
123 		 * to consumer before we recheck.			\
124 		 * Pairs with a barrier in __netif_txq_completed_wake(). \
125 		 */							\
126 		smp_mb__after_atomic();					\
127 									\
128 		/* We need to check again in a case another		\
129 		 * CPU has just made room available.			\
130 		 */							\
131 		_res = 0;						\
132 		if (unlikely(get_desc >= start_thrs)) {			\
133 			netif_tx_start_queue(txq);			\
134 			_res = -1;					\
135 		}							\
136 		_res;							\
137 	})								\
138 
139 /**
140  * netif_txq_maybe_stop() - locklessly stop a Tx queue, if needed
141  * @txq:	struct netdev_queue to stop/start
142  * @get_desc:	get current number of free descriptors (see requirements below!)
143  * @stop_thrs:	minimal number of available descriptors for queue to be left
144  *		enabled
145  * @start_thrs:	minimal number of descriptors to re-enable the queue, can be
146  *		equal to @stop_thrs or higher to avoid frequent waking
147  *
148  * All arguments may be evaluated multiple times, beware of side effects.
149  * @get_desc must be a formula or a function call, it must always
150  * return up-to-date information when evaluated!
151  * Expected to be used from ndo_start_xmit, see the comment on top of the file.
152  *
153  * Returns:
154  *	 0 if the queue was stopped
155  *	 1 if the queue was left enabled
156  *	-1 if the queue was re-enabled (raced with waking)
157  */
158 #define netif_txq_maybe_stop(txq, get_desc, stop_thrs, start_thrs)	\
159 	({								\
160 		int _res;						\
161 									\
162 		_res = 1;						\
163 		if (unlikely(get_desc < stop_thrs))			\
164 			_res = netif_txq_try_stop(txq, get_desc, start_thrs); \
165 		_res;							\
166 	})								\
167 
168 /* Variant of netdev_tx_completed_queue() which guarantees smp_mb() if
169  * @bytes != 0, regardless of kernel config.
170  */
171 static inline void
172 netdev_txq_completed_mb(struct netdev_queue *dev_queue,
173 			unsigned int pkts, unsigned int bytes)
174 {
175 	if (IS_ENABLED(CONFIG_BQL))
176 		netdev_tx_completed_queue(dev_queue, pkts, bytes);
177 	else if (bytes)
178 		smp_mb();
179 }
180 
181 /**
182  * __netif_txq_completed_wake() - locklessly wake a Tx queue, if needed
183  * @txq:	struct netdev_queue to stop/start
184  * @pkts:	number of packets completed
185  * @bytes:	number of bytes completed
186  * @get_desc:	get current number of free descriptors (see requirements below!)
187  * @start_thrs:	minimal number of descriptors to re-enable the queue
188  * @down_cond:	down condition, predicate indicating that the queue should
189  *		not be woken up even if descriptors are available
190  *
191  * All arguments may be evaluated multiple times.
192  * @get_desc must be a formula or a function call, it must always
193  * return up-to-date information when evaluated!
194  * Reports completed pkts/bytes to BQL.
195  *
196  * Returns:
197  *	 0 if the queue was woken up
198  *	 1 if the queue was already enabled (or disabled but @down_cond is true)
199  *	-1 if the queue was left unchanged (@start_thrs not reached)
200  */
201 #define __netif_txq_completed_wake(txq, pkts, bytes,			\
202 				   get_desc, start_thrs, down_cond)	\
203 	({								\
204 		int _res;						\
205 									\
206 		/* Report to BQL and piggy back on its barrier.		\
207 		 * Barrier makes sure that anybody stopping the queue	\
208 		 * after this point sees the new consumer index.	\
209 		 * Pairs with barrier in netif_txq_try_stop().		\
210 		 */							\
211 		netdev_txq_completed_mb(txq, pkts, bytes);		\
212 									\
213 		_res = -1;						\
214 		if (pkts && likely(get_desc >= start_thrs)) {		\
215 			_res = 1;					\
216 			if (unlikely(netif_tx_queue_stopped(txq)) &&	\
217 			    !(down_cond)) {				\
218 				netif_tx_wake_queue(txq);		\
219 				_res = 0;				\
220 			}						\
221 		}							\
222 		_res;							\
223 	})
224 
225 #define netif_txq_completed_wake(txq, pkts, bytes, get_desc, start_thrs) \
226 	__netif_txq_completed_wake(txq, pkts, bytes, get_desc, start_thrs, false)
227 
228 /* subqueue variants follow */
229 
230 #define netif_subqueue_try_stop(dev, idx, get_desc, start_thrs)		\
231 	({								\
232 		struct netdev_queue *txq;				\
233 									\
234 		txq = netdev_get_tx_queue(dev, idx);			\
235 		netif_txq_try_stop(txq, get_desc, start_thrs);		\
236 	})
237 
238 #define netif_subqueue_maybe_stop(dev, idx, get_desc, stop_thrs, start_thrs) \
239 	({								\
240 		struct netdev_queue *txq;				\
241 									\
242 		txq = netdev_get_tx_queue(dev, idx);			\
243 		netif_txq_maybe_stop(txq, get_desc, stop_thrs, start_thrs); \
244 	})
245 
246 #define netif_subqueue_completed_wake(dev, idx, pkts, bytes,		\
247 				      get_desc, start_thrs)		\
248 	({								\
249 		struct netdev_queue *txq;				\
250 									\
251 		txq = netdev_get_tx_queue(dev, idx);			\
252 		netif_txq_completed_wake(txq, pkts, bytes,		\
253 					 get_desc, start_thrs);		\
254 	})
255 
256 #endif
257