xref: /linux/include/net/libeth/xdp.h (revision 5495c58c65aa3d650cccaa19dc59115b9a0069a5)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (C) 2025 Intel Corporation */
3 
4 #ifndef __LIBETH_XDP_H
5 #define __LIBETH_XDP_H
6 
7 #include <linux/bpf_trace.h>
8 #include <linux/unroll.h>
9 
10 #include <net/libeth/rx.h>
11 #include <net/libeth/tx.h>
12 #include <net/xsk_buff_pool.h>
13 
14 /*
15  * Defined as bits to be able to use them as a mask on Rx.
16  * Also used as internal return values on Tx.
17  */
18 enum {
19 	LIBETH_XDP_PASS			= 0U,
20 	LIBETH_XDP_DROP			= BIT(0),
21 	LIBETH_XDP_ABORTED		= BIT(1),
22 	LIBETH_XDP_TX			= BIT(2),
23 	LIBETH_XDP_REDIRECT		= BIT(3),
24 };
25 
26 /*
27  * &xdp_buff_xsk is the largest structure &libeth_xdp_buff gets casted to,
28  * pick maximum pointer-compatible alignment.
29  */
30 #define __LIBETH_XDP_BUFF_ALIGN						      \
31 	(IS_ALIGNED(sizeof(struct xdp_buff_xsk), 16) ? 16 :		      \
32 	 IS_ALIGNED(sizeof(struct xdp_buff_xsk), 8) ? 8 :		      \
33 	 sizeof(long))
34 
35 /**
36  * struct libeth_xdp_buff - libeth extension over &xdp_buff
37  * @base: main &xdp_buff
38  * @data: shortcut for @base.data
39  * @desc: RQ descriptor containing metadata for this buffer
40  * @priv: driver-private scratchspace
41  *
42  * The main reason for this is to have a pointer to the descriptor to be able
43  * to quickly get frame metadata from xdpmo and driver buff-to-xdp callbacks
44  * (as well as bigger alignment).
45  * Pointer/layout-compatible with &xdp_buff and &xdp_buff_xsk.
46  */
47 struct libeth_xdp_buff {
48 	union {
49 		struct xdp_buff		base;
50 		void			*data;
51 	};
52 
53 	const void			*desc;
54 	unsigned long			priv[]
55 					__aligned(__LIBETH_XDP_BUFF_ALIGN);
56 } __aligned(__LIBETH_XDP_BUFF_ALIGN);
57 static_assert(offsetof(struct libeth_xdp_buff, data) ==
58 	      offsetof(struct xdp_buff_xsk, xdp.data));
59 static_assert(offsetof(struct libeth_xdp_buff, desc) ==
60 	      offsetof(struct xdp_buff_xsk, cb));
61 static_assert(IS_ALIGNED(sizeof(struct xdp_buff_xsk),
62 			 __alignof(struct libeth_xdp_buff)));
63 
64 /**
65  * __LIBETH_XDP_ONSTACK_BUFF - declare a &libeth_xdp_buff on the stack
66  * @name: name of the variable to declare
67  * @...: sizeof() of the driver-private data
68  */
69 #define __LIBETH_XDP_ONSTACK_BUFF(name, ...)				      \
70 	___LIBETH_XDP_ONSTACK_BUFF(name, ##__VA_ARGS__)
71 /**
72  * LIBETH_XDP_ONSTACK_BUFF - declare a &libeth_xdp_buff on the stack
73  * @name: name of the variable to declare
74  * @...: type or variable name of the driver-private data
75  */
76 #define LIBETH_XDP_ONSTACK_BUFF(name, ...)				      \
77 	__LIBETH_XDP_ONSTACK_BUFF(name, __libeth_xdp_priv_sz(__VA_ARGS__))
78 
79 #define ___LIBETH_XDP_ONSTACK_BUFF(name, ...)				      \
80 	__DEFINE_FLEX(struct libeth_xdp_buff, name, priv,		      \
81 		      LIBETH_XDP_PRIV_SZ(__VA_ARGS__ + 0),		      \
82 		      __uninitialized);					      \
83 	LIBETH_XDP_ASSERT_PRIV_SZ(__VA_ARGS__ + 0)
84 
85 #define __libeth_xdp_priv_sz(...)					      \
86 	CONCATENATE(__libeth_xdp_psz, COUNT_ARGS(__VA_ARGS__))(__VA_ARGS__)
87 
88 #define __libeth_xdp_psz0(...)
89 #define __libeth_xdp_psz1(...)		sizeof(__VA_ARGS__)
90 
91 #define LIBETH_XDP_PRIV_SZ(sz)						      \
92 	(ALIGN(sz, __alignof(struct libeth_xdp_buff)) / sizeof(long))
93 
94 /* Performs XSK_CHECK_PRIV_TYPE() */
95 #define LIBETH_XDP_ASSERT_PRIV_SZ(sz)					      \
96 	static_assert(offsetofend(struct xdp_buff_xsk, cb) >=		      \
97 		      struct_size_t(struct libeth_xdp_buff, priv,	      \
98 				    LIBETH_XDP_PRIV_SZ(sz)))
99 
100 /* XDPSQ sharing */
101 
102 DECLARE_STATIC_KEY_FALSE(libeth_xdpsq_share);
103 
104 /**
105  * libeth_xdpsq_num - calculate optimal number of XDPSQs for this device + sys
106  * @rxq: current number of active Rx queues
107  * @txq: current number of active Tx queues
108  * @max: maximum number of Tx queues
109  *
110  * Each RQ must have its own XDPSQ for XSk pairs, each CPU must have own XDPSQ
111  * for lockless sending (``XDP_TX``, .ndo_xdp_xmit()). Cap the maximum of these
112  * two with the number of SQs the device can have (minus used ones).
113  *
114  * Return: number of XDP Tx queues the device needs to use.
115  */
116 static inline u32 libeth_xdpsq_num(u32 rxq, u32 txq, u32 max)
117 {
118 	return min(max(nr_cpu_ids, rxq), max - txq);
119 }
120 
121 /**
122  * libeth_xdpsq_shared - whether XDPSQs can be shared between several CPUs
123  * @num: number of active XDPSQs
124  *
125  * Return: true if there's no 1:1 XDPSQ/CPU association, false otherwise.
126  */
127 static inline bool libeth_xdpsq_shared(u32 num)
128 {
129 	return num < nr_cpu_ids;
130 }
131 
132 /**
133  * libeth_xdpsq_id - get XDPSQ index corresponding to this CPU
134  * @num: number of active XDPSQs
135  *
136  * Helper for libeth_xdp routines, do not use in drivers directly.
137  *
138  * Return: XDPSQ index needs to be used on this CPU.
139  */
140 static inline u32 libeth_xdpsq_id(u32 num)
141 {
142 	u32 ret = raw_smp_processor_id();
143 
144 	if (static_branch_unlikely(&libeth_xdpsq_share) &&
145 	    libeth_xdpsq_shared(num))
146 		ret %= num;
147 
148 	return ret;
149 }
150 
151 void __libeth_xdpsq_get(struct libeth_xdpsq_lock *lock,
152 			const struct net_device *dev);
153 void __libeth_xdpsq_put(struct libeth_xdpsq_lock *lock,
154 			const struct net_device *dev);
155 
156 /**
157  * libeth_xdpsq_get - initialize &libeth_xdpsq_lock
158  * @lock: lock to initialize
159  * @dev: netdev which this lock belongs to
160  * @share: whether XDPSQs can be shared
161  *
162  * Tracks the current XDPSQ association and enables the static lock
163  * if needed.
164  */
165 static inline void libeth_xdpsq_get(struct libeth_xdpsq_lock *lock,
166 				    const struct net_device *dev,
167 				    bool share)
168 {
169 	if (unlikely(share))
170 		__libeth_xdpsq_get(lock, dev);
171 }
172 
173 /**
174  * libeth_xdpsq_put - deinitialize &libeth_xdpsq_lock
175  * @lock: lock to deinitialize
176  * @dev: netdev which this lock belongs to
177  *
178  * Tracks the current XDPSQ association and disables the static lock
179  * if needed.
180  */
181 static inline void libeth_xdpsq_put(struct libeth_xdpsq_lock *lock,
182 				    const struct net_device *dev)
183 {
184 	if (static_branch_unlikely(&libeth_xdpsq_share) && lock->share)
185 		__libeth_xdpsq_put(lock, dev);
186 }
187 
188 void __libeth_xdpsq_lock(struct libeth_xdpsq_lock *lock);
189 void __libeth_xdpsq_unlock(struct libeth_xdpsq_lock *lock);
190 
191 /**
192  * libeth_xdpsq_lock - grab &libeth_xdpsq_lock if needed
193  * @lock: lock to take
194  *
195  * Touches the underlying spinlock only if the static key is enabled
196  * and the queue itself is marked as shareable.
197  */
198 static inline void libeth_xdpsq_lock(struct libeth_xdpsq_lock *lock)
199 {
200 	if (static_branch_unlikely(&libeth_xdpsq_share) && lock->share)
201 		__libeth_xdpsq_lock(lock);
202 }
203 
204 /**
205  * libeth_xdpsq_unlock - free &libeth_xdpsq_lock if needed
206  * @lock: lock to free
207  *
208  * Touches the underlying spinlock only if the static key is enabled
209  * and the queue itself is marked as shareable.
210  */
211 static inline void libeth_xdpsq_unlock(struct libeth_xdpsq_lock *lock)
212 {
213 	if (static_branch_unlikely(&libeth_xdpsq_share) && lock->share)
214 		__libeth_xdpsq_unlock(lock);
215 }
216 
217 /* XDPSQ clean-up timers */
218 
219 void libeth_xdpsq_init_timer(struct libeth_xdpsq_timer *timer, void *xdpsq,
220 			     struct libeth_xdpsq_lock *lock,
221 			     void (*poll)(struct work_struct *work));
222 
223 /**
224  * libeth_xdpsq_deinit_timer - deinitialize &libeth_xdpsq_timer
225  * @timer: timer to deinitialize
226  *
227  * Flush and disable the underlying workqueue.
228  */
229 static inline void libeth_xdpsq_deinit_timer(struct libeth_xdpsq_timer *timer)
230 {
231 	cancel_delayed_work_sync(&timer->dwork);
232 }
233 
234 /**
235  * libeth_xdpsq_queue_timer - run &libeth_xdpsq_timer
236  * @timer: timer to queue
237  *
238  * Should be called after the queue was filled and the transmission was run
239  * to complete the pending buffers if no further sending will be done in a
240  * second (-> lazy cleaning won't happen).
241  * If the timer was already run, it will be requeued back to one second
242  * timeout again.
243  */
244 static inline void libeth_xdpsq_queue_timer(struct libeth_xdpsq_timer *timer)
245 {
246 	mod_delayed_work_on(raw_smp_processor_id(), system_bh_highpri_wq,
247 			    &timer->dwork, HZ);
248 }
249 
250 /**
251  * libeth_xdpsq_run_timer - wrapper to run a queue clean-up on a timer event
252  * @work: workqueue belonging to the corresponding timer
253  * @poll: driver-specific completion queue poll function
254  *
255  * Run the polling function on the locked queue and requeue the timer if
256  * there's more work to do.
257  * Designed to be used via LIBETH_XDP_DEFINE_TIMER() below.
258  */
259 static __always_inline void
260 libeth_xdpsq_run_timer(struct work_struct *work,
261 		       u32 (*poll)(void *xdpsq, u32 budget))
262 {
263 	struct libeth_xdpsq_timer *timer = container_of(work, typeof(*timer),
264 							dwork.work);
265 
266 	libeth_xdpsq_lock(timer->lock);
267 
268 	if (poll(timer->xdpsq, U32_MAX))
269 		libeth_xdpsq_queue_timer(timer);
270 
271 	libeth_xdpsq_unlock(timer->lock);
272 }
273 
274 /* Common Tx bits */
275 
276 /**
277  * enum - libeth_xdp internal Tx flags
278  * @LIBETH_XDP_TX_BULK: one bulk size at which it will be flushed to the queue
279  * @LIBETH_XDP_TX_BATCH: batch size for which the queue fill loop is unrolled
280  * @LIBETH_XDP_TX_DROP: indicates the send function must drop frames not sent
281  * @LIBETH_XDP_TX_NDO: whether the send function is called from .ndo_xdp_xmit()
282  * @LIBETH_XDP_TX_XSK: whether the function is called for ``XDP_TX`` for XSk
283  */
284 enum {
285 	LIBETH_XDP_TX_BULK		= DEV_MAP_BULK_SIZE,
286 	LIBETH_XDP_TX_BATCH		= 8,
287 
288 	LIBETH_XDP_TX_DROP		= BIT(0),
289 	LIBETH_XDP_TX_NDO		= BIT(1),
290 	LIBETH_XDP_TX_XSK		= BIT(2),
291 };
292 
293 /**
294  * enum - &libeth_xdp_tx_frame and &libeth_xdp_tx_desc flags
295  * @LIBETH_XDP_TX_LEN: only for ``XDP_TX``, [15:0] of ::len_fl is actual length
296  * @LIBETH_XDP_TX_CSUM: for XSk xmit, enable checksum offload
297  * @LIBETH_XDP_TX_XSKMD: for XSk xmit, mask of the metadata bits
298  * @LIBETH_XDP_TX_FIRST: indicates the frag is the first one of the frame
299  * @LIBETH_XDP_TX_LAST: whether the frag is the last one of the frame
300  * @LIBETH_XDP_TX_MULTI: whether the frame contains several frags
301  * @LIBETH_XDP_TX_FLAGS: only for ``XDP_TX``, [31:16] of ::len_fl is flags
302  */
303 enum {
304 	LIBETH_XDP_TX_LEN		= GENMASK(15, 0),
305 
306 	LIBETH_XDP_TX_CSUM		= XDP_TXMD_FLAGS_CHECKSUM,
307 	LIBETH_XDP_TX_XSKMD		= LIBETH_XDP_TX_LEN,
308 
309 	LIBETH_XDP_TX_FIRST		= BIT(16),
310 	LIBETH_XDP_TX_LAST		= BIT(17),
311 	LIBETH_XDP_TX_MULTI		= BIT(18),
312 
313 	LIBETH_XDP_TX_FLAGS		= GENMASK(31, 16),
314 };
315 
316 /**
317  * struct libeth_xdp_tx_frame - represents one XDP Tx element
318  * @data: frame start pointer for ``XDP_TX``
319  * @len_fl: ``XDP_TX``, combined flags [31:16] and len [15:0] field for speed
320  * @soff: ``XDP_TX``, offset from @data to the start of &skb_shared_info
321  * @frag: one (non-head) frag for ``XDP_TX``
322  * @xdpf: &xdp_frame for the head frag for .ndo_xdp_xmit()
323  * @dma: DMA address of the non-head frag for .ndo_xdp_xmit()
324  * @xsk: ``XDP_TX`` for XSk, XDP buffer for any frag
325  * @len: frag length for XSk ``XDP_TX`` and .ndo_xdp_xmit()
326  * @flags: Tx flags for the above
327  * @opts: combined @len + @flags for the above for speed
328  * @desc: XSk xmit descriptor for direct casting
329  */
330 struct libeth_xdp_tx_frame {
331 	union {
332 		/* ``XDP_TX`` */
333 		struct {
334 			void				*data;
335 			u32				len_fl;
336 			u32				soff;
337 		};
338 
339 		/* ``XDP_TX`` frag */
340 		skb_frag_t			frag;
341 
342 		/* .ndo_xdp_xmit(), XSk ``XDP_TX`` */
343 		struct {
344 			union {
345 				struct xdp_frame		*xdpf;
346 				dma_addr_t			dma;
347 
348 				struct libeth_xdp_buff		*xsk;
349 			};
350 			union {
351 				struct {
352 					u32				len;
353 					u32				flags;
354 				};
355 				aligned_u64			opts;
356 			};
357 		};
358 
359 		/* XSk xmit */
360 		struct xdp_desc			desc;
361 	};
362 } __aligned(sizeof(struct xdp_desc));
363 static_assert(offsetof(struct libeth_xdp_tx_frame, frag.len) ==
364 	      offsetof(struct libeth_xdp_tx_frame, len_fl));
365 static_assert(sizeof(struct libeth_xdp_tx_frame) == sizeof(struct xdp_desc));
366 
367 /**
368  * struct libeth_xdp_tx_bulk - XDP Tx frame bulk for bulk sending
369  * @prog: corresponding active XDP program, %NULL for .ndo_xdp_xmit()
370  * @dev: &net_device which the frames are transmitted on
371  * @xdpsq: shortcut to the corresponding driver-specific XDPSQ structure
372  * @act_mask: Rx only, mask of all the XDP prog verdicts for that NAPI session
373  * @count: current number of frames in @bulk
374  * @bulk: array of queued frames for bulk Tx
375  *
376  * All XDP Tx operations except XSk xmit queue each frame to the bulk first
377  * and flush it when @count reaches the array end. Bulk is always placed on
378  * the stack for performance. One bulk element contains all the data necessary
379  * for sending a frame and then freeing it on completion.
380  * For XSk xmit, Tx descriptor array from &xsk_buff_pool is casted directly
381  * to &libeth_xdp_tx_frame as they are compatible and the bulk structure is
382  * not used.
383  */
384 struct libeth_xdp_tx_bulk {
385 	const struct bpf_prog		*prog;
386 	struct net_device		*dev;
387 	void				*xdpsq;
388 
389 	u32				act_mask;
390 	u32				count;
391 	struct libeth_xdp_tx_frame	bulk[LIBETH_XDP_TX_BULK];
392 } __aligned(sizeof(struct libeth_xdp_tx_frame));
393 
394 /**
395  * LIBETH_XDP_ONSTACK_BULK - declare &libeth_xdp_tx_bulk on the stack
396  * @bq: name of the variable to declare
397  *
398  * Helper to declare a bulk on the stack with a compiler hint that it should
399  * not be initialized automatically (with `CONFIG_INIT_STACK_ALL_*`) for
400  * performance reasons.
401  */
402 #define LIBETH_XDP_ONSTACK_BULK(bq)					      \
403 	struct libeth_xdp_tx_bulk bq __uninitialized
404 
405 /**
406  * struct libeth_xdpsq - abstraction for an XDPSQ
407  * @pool: XSk buffer pool for XSk ``XDP_TX`` and xmit
408  * @sqes: array of Tx buffers from the actual queue struct
409  * @descs: opaque pointer to the HW descriptor array
410  * @ntu: pointer to the next free descriptor index
411  * @count: number of descriptors on that queue
412  * @pending: pointer to the number of sent-not-completed descs on that queue
413  * @xdp_tx: pointer to the above, but only for non-XSk-xmit frames
414  * @lock: corresponding XDPSQ lock
415  *
416  * Abstraction for driver-independent implementation of Tx. Placed on the stack
417  * and filled by the driver before the transmission, so that the generic
418  * functions can access and modify driver-specific resources.
419  */
420 struct libeth_xdpsq {
421 	struct xsk_buff_pool		*pool;
422 	struct libeth_sqe		*sqes;
423 	void				*descs;
424 
425 	u32				*ntu;
426 	u32				count;
427 
428 	u32				*pending;
429 	u32				*xdp_tx;
430 	struct libeth_xdpsq_lock	*lock;
431 };
432 
433 /**
434  * struct libeth_xdp_tx_desc - abstraction for an XDP Tx descriptor
435  * @addr: DMA address of the frame
436  * @len: length of the frame
437  * @flags: XDP Tx flags
438  * @opts: combined @len + @flags for speed
439  *
440  * Filled by the generic functions and then passed to driver-specific functions
441  * to fill a HW Tx descriptor, always placed on the [function] stack.
442  */
443 struct libeth_xdp_tx_desc {
444 	dma_addr_t			addr;
445 	union {
446 		struct {
447 			u32				len;
448 			u32				flags;
449 		};
450 		aligned_u64			opts;
451 	};
452 } __aligned_largest;
453 
454 /**
455  * libeth_xdp_ptr_to_priv - convert pointer to a libeth_xdp u64 priv
456  * @ptr: pointer to convert
457  *
458  * The main sending function passes private data as the largest scalar, u64.
459  * Use this helper when you want to pass a pointer there.
460  */
461 #define libeth_xdp_ptr_to_priv(ptr) ({					      \
462 	typecheck_pointer(ptr);						      \
463 	((u64)(uintptr_t)(ptr));					      \
464 })
465 /**
466  * libeth_xdp_priv_to_ptr - convert libeth_xdp u64 priv to a pointer
467  * @priv: private data to convert
468  *
469  * The main sending function passes private data as the largest scalar, u64.
470  * Use this helper when your callback takes this u64 and you want to convert
471  * it back to a pointer.
472  */
473 #define libeth_xdp_priv_to_ptr(priv) ({					      \
474 	static_assert(__same_type(priv, u64));				      \
475 	((const void *)(uintptr_t)(priv));				      \
476 })
477 
478 /**
479  * libeth_xdp_tx_xmit_bulk - main XDP Tx function
480  * @bulk: array of frames to send
481  * @xdpsq: pointer to the driver-specific XDPSQ struct
482  * @n: number of frames to send
483  * @unroll: whether to unroll the queue filling loop for speed
484  * @priv: driver-specific private data
485  * @prep: callback for cleaning the queue and filling abstract &libeth_xdpsq
486  * @fill: internal callback for filling &libeth_sqe and &libeth_xdp_tx_desc
487  * @xmit: callback for filling a HW descriptor with the frame info
488  *
489  * Internal abstraction for placing @n XDP Tx frames on the HW XDPSQ. Used for
490  * all types of frames: ``XDP_TX``, .ndo_xdp_xmit(), XSk ``XDP_TX``, and XSk
491  * xmit.
492  * @prep must lock the queue as this function releases it at the end. @unroll
493  * greatly increases the object code size, but also greatly increases XSk xmit
494  * performance; for other types of frames, it's not enabled.
495  * The compilers inline all those onstack abstractions to direct data accesses.
496  *
497  * Return: number of frames actually placed on the queue, <= @n. The function
498  * can't fail, but can send less frames if there's no enough free descriptors
499  * available. The actual free space is returned by @prep from the driver.
500  */
501 static __always_inline u32
502 libeth_xdp_tx_xmit_bulk(const struct libeth_xdp_tx_frame *bulk, void *xdpsq,
503 			u32 n, bool unroll, u64 priv,
504 			u32 (*prep)(void *xdpsq, struct libeth_xdpsq *sq),
505 			struct libeth_xdp_tx_desc
506 			(*fill)(struct libeth_xdp_tx_frame frm, u32 i,
507 				const struct libeth_xdpsq *sq, u64 priv),
508 			void (*xmit)(struct libeth_xdp_tx_desc desc, u32 i,
509 				     const struct libeth_xdpsq *sq, u64 priv))
510 {
511 	struct libeth_xdpsq sq __uninitialized;
512 	u32 this, batched, off = 0;
513 	u32 ntu, i = 0;
514 
515 	n = min(n, prep(xdpsq, &sq));
516 	if (unlikely(!n))
517 		goto unlock;
518 
519 	ntu = *sq.ntu;
520 
521 	this = sq.count - ntu;
522 	if (likely(this > n))
523 		this = n;
524 
525 again:
526 	if (!unroll)
527 		goto linear;
528 
529 	batched = ALIGN_DOWN(this, LIBETH_XDP_TX_BATCH);
530 
531 	for ( ; i < off + batched; i += LIBETH_XDP_TX_BATCH) {
532 		u32 base = ntu + i - off;
533 
534 		unrolled_count(LIBETH_XDP_TX_BATCH)
535 		for (u32 j = 0; j < LIBETH_XDP_TX_BATCH; j++)
536 			xmit(fill(bulk[i + j], base + j, &sq, priv),
537 			     base + j, &sq, priv);
538 	}
539 
540 	if (batched < this) {
541 linear:
542 		for ( ; i < off + this; i++)
543 			xmit(fill(bulk[i], ntu + i - off, &sq, priv),
544 			     ntu + i - off, &sq, priv);
545 	}
546 
547 	ntu += this;
548 	if (likely(ntu < sq.count))
549 		goto out;
550 
551 	ntu = 0;
552 
553 	if (i < n) {
554 		this = n - i;
555 		off = i;
556 
557 		goto again;
558 	}
559 
560 out:
561 	*sq.ntu = ntu;
562 	*sq.pending += n;
563 	if (sq.xdp_tx)
564 		*sq.xdp_tx += n;
565 
566 unlock:
567 	libeth_xdpsq_unlock(sq.lock);
568 
569 	return n;
570 }
571 
572 /* ``XDP_TX`` bulking */
573 
574 void libeth_xdp_return_buff_slow(struct libeth_xdp_buff *xdp);
575 
576 /**
577  * libeth_xdp_tx_queue_head - internal helper for queueing one ``XDP_TX`` head
578  * @bq: XDP Tx bulk to queue the head frag to
579  * @xdp: XDP buffer with the head to queue
580  *
581  * Return: false if it's the only frag of the frame, true if it's an S/G frame.
582  */
583 static inline bool libeth_xdp_tx_queue_head(struct libeth_xdp_tx_bulk *bq,
584 					    const struct libeth_xdp_buff *xdp)
585 {
586 	const struct xdp_buff *base = &xdp->base;
587 
588 	bq->bulk[bq->count++] = (typeof(*bq->bulk)){
589 		.data	= xdp->data,
590 		.len_fl	= (base->data_end - xdp->data) | LIBETH_XDP_TX_FIRST,
591 		.soff	= xdp_data_hard_end(base) - xdp->data,
592 	};
593 
594 	if (!xdp_buff_has_frags(base))
595 		return false;
596 
597 	bq->bulk[bq->count - 1].len_fl |= LIBETH_XDP_TX_MULTI;
598 
599 	return true;
600 }
601 
602 /**
603  * libeth_xdp_tx_queue_frag - internal helper for queueing one ``XDP_TX`` frag
604  * @bq: XDP Tx bulk to queue the frag to
605  * @frag: frag to queue
606  */
607 static inline void libeth_xdp_tx_queue_frag(struct libeth_xdp_tx_bulk *bq,
608 					    const skb_frag_t *frag)
609 {
610 	bq->bulk[bq->count++].frag = *frag;
611 }
612 
613 /**
614  * libeth_xdp_tx_queue_bulk - internal helper for queueing one ``XDP_TX`` frame
615  * @bq: XDP Tx bulk to queue the frame to
616  * @xdp: XDP buffer to queue
617  * @flush_bulk: driver callback to flush the bulk to the HW queue
618  *
619  * Return: true on success, false on flush error.
620  */
621 static __always_inline bool
622 libeth_xdp_tx_queue_bulk(struct libeth_xdp_tx_bulk *bq,
623 			 struct libeth_xdp_buff *xdp,
624 			 bool (*flush_bulk)(struct libeth_xdp_tx_bulk *bq,
625 					    u32 flags))
626 {
627 	const struct skb_shared_info *sinfo;
628 	bool ret = true;
629 	u32 nr_frags;
630 
631 	if (unlikely(bq->count == LIBETH_XDP_TX_BULK) &&
632 	    unlikely(!flush_bulk(bq, 0))) {
633 		libeth_xdp_return_buff_slow(xdp);
634 		return false;
635 	}
636 
637 	if (!libeth_xdp_tx_queue_head(bq, xdp))
638 		goto out;
639 
640 	sinfo = xdp_get_shared_info_from_buff(&xdp->base);
641 	nr_frags = sinfo->nr_frags;
642 
643 	for (u32 i = 0; i < nr_frags; i++) {
644 		if (unlikely(bq->count == LIBETH_XDP_TX_BULK) &&
645 		    unlikely(!flush_bulk(bq, 0))) {
646 			ret = false;
647 			break;
648 		}
649 
650 		libeth_xdp_tx_queue_frag(bq, &sinfo->frags[i]);
651 	}
652 
653 out:
654 	bq->bulk[bq->count - 1].len_fl |= LIBETH_XDP_TX_LAST;
655 	xdp->data = NULL;
656 
657 	return ret;
658 }
659 
660 /**
661  * libeth_xdp_tx_fill_stats - fill &libeth_sqe with ``XDP_TX`` frame stats
662  * @sqe: SQ element to fill
663  * @desc: libeth_xdp Tx descriptor
664  * @sinfo: &skb_shared_info for this frame
665  *
666  * Internal helper for filling an SQE with the frame stats, do not use in
667  * drivers. Fills the number of frags and bytes for this frame.
668  */
669 #define libeth_xdp_tx_fill_stats(sqe, desc, sinfo)			      \
670 	__libeth_xdp_tx_fill_stats(sqe, desc, sinfo, __UNIQUE_ID(sqe_),	      \
671 				   __UNIQUE_ID(desc_), __UNIQUE_ID(sinfo_))
672 
673 #define __libeth_xdp_tx_fill_stats(sqe, desc, sinfo, ue, ud, us) do {	      \
674 	const struct libeth_xdp_tx_desc *ud = (desc);			      \
675 	const struct skb_shared_info *us;				      \
676 	struct libeth_sqe *ue = (sqe);					      \
677 									      \
678 	ue->nr_frags = 1;						      \
679 	ue->bytes = ud->len;						      \
680 									      \
681 	if (ud->flags & LIBETH_XDP_TX_MULTI) {				      \
682 		us = (sinfo);						      \
683 		ue->nr_frags += us->nr_frags;				      \
684 		ue->bytes += us->xdp_frags_size;			      \
685 	}								      \
686 } while (0)
687 
688 /**
689  * libeth_xdp_tx_fill_buf - internal helper to fill one ``XDP_TX`` &libeth_sqe
690  * @frm: XDP Tx frame from the bulk
691  * @i: index on the HW queue
692  * @sq: XDPSQ abstraction for the queue
693  * @priv: private data
694  *
695  * Return: XDP Tx descriptor with the synced DMA and other info to pass to
696  * the driver callback.
697  */
698 static inline struct libeth_xdp_tx_desc
699 libeth_xdp_tx_fill_buf(struct libeth_xdp_tx_frame frm, u32 i,
700 		       const struct libeth_xdpsq *sq, u64 priv)
701 {
702 	struct libeth_xdp_tx_desc desc;
703 	struct skb_shared_info *sinfo;
704 	skb_frag_t *frag = &frm.frag;
705 	struct libeth_sqe *sqe;
706 	netmem_ref netmem;
707 
708 	if (frm.len_fl & LIBETH_XDP_TX_FIRST) {
709 		sinfo = frm.data + frm.soff;
710 		skb_frag_fill_netmem_desc(frag, virt_to_netmem(frm.data),
711 					  offset_in_page(frm.data),
712 					  frm.len_fl);
713 	} else {
714 		sinfo = NULL;
715 	}
716 
717 	netmem = skb_frag_netmem(frag);
718 	desc = (typeof(desc)){
719 		.addr	= page_pool_get_dma_addr_netmem(netmem) +
720 			  skb_frag_off(frag),
721 		.len	= skb_frag_size(frag) & LIBETH_XDP_TX_LEN,
722 		.flags	= skb_frag_size(frag) & LIBETH_XDP_TX_FLAGS,
723 	};
724 
725 	dma_sync_single_for_device(__netmem_get_pp(netmem)->p.dev, desc.addr,
726 				   desc.len, DMA_BIDIRECTIONAL);
727 
728 	if (!sinfo)
729 		return desc;
730 
731 	sqe = &sq->sqes[i];
732 	sqe->type = LIBETH_SQE_XDP_TX;
733 	sqe->sinfo = sinfo;
734 	libeth_xdp_tx_fill_stats(sqe, &desc, sinfo);
735 
736 	return desc;
737 }
738 
739 void libeth_xdp_tx_exception(struct libeth_xdp_tx_bulk *bq, u32 sent,
740 			     u32 flags);
741 
742 /**
743  * __libeth_xdp_tx_flush_bulk - internal helper to flush one XDP Tx bulk
744  * @bq: bulk to flush
745  * @flags: XDP TX flags (.ndo_xdp_xmit(), XSk etc.)
746  * @prep: driver-specific callback to prepare the queue for sending
747  * @fill: libeth_xdp callback to fill &libeth_sqe and &libeth_xdp_tx_desc
748  * @xmit: driver callback to fill a HW descriptor
749  *
750  * Internal abstraction to create bulk flush functions for drivers. Used for
751  * everything except XSk xmit.
752  *
753  * Return: true if anything was sent, false otherwise.
754  */
755 static __always_inline bool
756 __libeth_xdp_tx_flush_bulk(struct libeth_xdp_tx_bulk *bq, u32 flags,
757 			   u32 (*prep)(void *xdpsq, struct libeth_xdpsq *sq),
758 			   struct libeth_xdp_tx_desc
759 			   (*fill)(struct libeth_xdp_tx_frame frm, u32 i,
760 				   const struct libeth_xdpsq *sq, u64 priv),
761 			   void (*xmit)(struct libeth_xdp_tx_desc desc, u32 i,
762 					const struct libeth_xdpsq *sq,
763 					u64 priv))
764 {
765 	u32 sent, drops;
766 	int err = 0;
767 
768 	sent = libeth_xdp_tx_xmit_bulk(bq->bulk, bq->xdpsq,
769 				       min(bq->count, LIBETH_XDP_TX_BULK),
770 				       false, 0, prep, fill, xmit);
771 	drops = bq->count - sent;
772 
773 	if (unlikely(drops)) {
774 		libeth_xdp_tx_exception(bq, sent, flags);
775 		err = -ENXIO;
776 	} else {
777 		bq->count = 0;
778 	}
779 
780 	trace_xdp_bulk_tx(bq->dev, sent, drops, err);
781 
782 	return likely(sent);
783 }
784 
785 /**
786  * libeth_xdp_tx_flush_bulk - wrapper to define flush of one ``XDP_TX`` bulk
787  * @bq: bulk to flush
788  * @flags: Tx flags, see above
789  * @prep: driver callback to prepare the queue
790  * @xmit: driver callback to fill a HW descriptor
791  *
792  * Use via LIBETH_XDP_DEFINE_FLUSH_TX() to define an ``XDP_TX`` driver
793  * callback.
794  */
795 #define libeth_xdp_tx_flush_bulk(bq, flags, prep, xmit)			      \
796 	__libeth_xdp_tx_flush_bulk(bq, flags, prep, libeth_xdp_tx_fill_buf,   \
797 				   xmit)
798 
799 /* .ndo_xdp_xmit() implementation */
800 
801 /**
802  * libeth_xdp_xmit_init_bulk - internal helper to initialize bulk for XDP xmit
803  * @bq: bulk to initialize
804  * @dev: target &net_device
805  * @xdpsqs: array of driver-specific XDPSQ structs
806  * @num: number of active XDPSQs (the above array length)
807  */
808 #define libeth_xdp_xmit_init_bulk(bq, dev, xdpsqs, num)			      \
809 	__libeth_xdp_xmit_init_bulk(bq, dev, (xdpsqs)[libeth_xdpsq_id(num)])
810 
811 static inline void __libeth_xdp_xmit_init_bulk(struct libeth_xdp_tx_bulk *bq,
812 					       struct net_device *dev,
813 					       void *xdpsq)
814 {
815 	bq->dev = dev;
816 	bq->xdpsq = xdpsq;
817 	bq->count = 0;
818 }
819 
820 /**
821  * libeth_xdp_xmit_frame_dma - internal helper to access DMA of an &xdp_frame
822  * @xf: pointer to the XDP frame
823  *
824  * There's no place in &libeth_xdp_tx_frame to store DMA address for an
825  * &xdp_frame head. The headroom is used then, the address is placed right
826  * after the frame struct, naturally aligned.
827  *
828  * Return: pointer to the DMA address to use.
829  */
830 #define libeth_xdp_xmit_frame_dma(xf)					      \
831 	_Generic((xf),							      \
832 		 const struct xdp_frame *:				      \
833 			(const dma_addr_t *)__libeth_xdp_xmit_frame_dma(xf),  \
834 		 struct xdp_frame *:					      \
835 			(dma_addr_t *)__libeth_xdp_xmit_frame_dma(xf)	      \
836 	)
837 
838 static inline void *__libeth_xdp_xmit_frame_dma(const struct xdp_frame *xdpf)
839 {
840 	void *addr = (void *)(xdpf + 1);
841 
842 	if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
843 	    __alignof(*xdpf) < sizeof(dma_addr_t))
844 		addr = PTR_ALIGN(addr, sizeof(dma_addr_t));
845 
846 	return addr;
847 }
848 
849 /**
850  * libeth_xdp_xmit_queue_head - internal helper for queueing one XDP xmit head
851  * @bq: XDP Tx bulk to queue the head frag to
852  * @xdpf: XDP frame with the head to queue
853  * @dev: device to perform DMA mapping
854  *
855  * Return: ``LIBETH_XDP_DROP`` on DMA mapping error,
856  *	   ``LIBETH_XDP_PASS`` if it's the only frag in the frame,
857  *	   ``LIBETH_XDP_TX`` if it's an S/G frame.
858  */
859 static inline u32 libeth_xdp_xmit_queue_head(struct libeth_xdp_tx_bulk *bq,
860 					     struct xdp_frame *xdpf,
861 					     struct device *dev)
862 {
863 	dma_addr_t dma;
864 
865 	dma = dma_map_single(dev, xdpf->data, xdpf->len, DMA_TO_DEVICE);
866 	if (dma_mapping_error(dev, dma))
867 		return LIBETH_XDP_DROP;
868 
869 	*libeth_xdp_xmit_frame_dma(xdpf) = dma;
870 
871 	bq->bulk[bq->count++] = (typeof(*bq->bulk)){
872 		.xdpf	= xdpf,
873 		.len	= xdpf->len,
874 		.flags	= LIBETH_XDP_TX_FIRST,
875 	};
876 
877 	if (!xdp_frame_has_frags(xdpf))
878 		return LIBETH_XDP_PASS;
879 
880 	bq->bulk[bq->count - 1].flags |= LIBETH_XDP_TX_MULTI;
881 
882 	return LIBETH_XDP_TX;
883 }
884 
885 /**
886  * libeth_xdp_xmit_queue_frag - internal helper for queueing one XDP xmit frag
887  * @bq: XDP Tx bulk to queue the frag to
888  * @frag: frag to queue
889  * @dev: device to perform DMA mapping
890  *
891  * Return: true on success, false on DMA mapping error.
892  */
893 static inline bool libeth_xdp_xmit_queue_frag(struct libeth_xdp_tx_bulk *bq,
894 					      const skb_frag_t *frag,
895 					      struct device *dev)
896 {
897 	dma_addr_t dma;
898 
899 	dma = skb_frag_dma_map(dev, frag);
900 	if (dma_mapping_error(dev, dma))
901 		return false;
902 
903 	bq->bulk[bq->count++] = (typeof(*bq->bulk)){
904 		.dma	= dma,
905 		.len	= skb_frag_size(frag),
906 	};
907 
908 	return true;
909 }
910 
911 /**
912  * libeth_xdp_xmit_queue_bulk - internal helper for queueing one XDP xmit frame
913  * @bq: XDP Tx bulk to queue the frame to
914  * @xdpf: XDP frame to queue
915  * @flush_bulk: driver callback to flush the bulk to the HW queue
916  *
917  * Return: ``LIBETH_XDP_TX`` on success,
918  *	   ``LIBETH_XDP_DROP`` if the frame should be dropped by the stack,
919  *	   ``LIBETH_XDP_ABORTED`` if the frame will be dropped by libeth_xdp.
920  */
921 static __always_inline u32
922 libeth_xdp_xmit_queue_bulk(struct libeth_xdp_tx_bulk *bq,
923 			   struct xdp_frame *xdpf,
924 			   bool (*flush_bulk)(struct libeth_xdp_tx_bulk *bq,
925 					      u32 flags))
926 {
927 	u32 head, nr_frags, i, ret = LIBETH_XDP_TX;
928 	struct device *dev = bq->dev->dev.parent;
929 	const struct skb_shared_info *sinfo;
930 
931 	if (unlikely(bq->count == LIBETH_XDP_TX_BULK) &&
932 	    unlikely(!flush_bulk(bq, LIBETH_XDP_TX_NDO)))
933 		return LIBETH_XDP_DROP;
934 
935 	head = libeth_xdp_xmit_queue_head(bq, xdpf, dev);
936 	if (head == LIBETH_XDP_PASS)
937 		goto out;
938 	else if (head == LIBETH_XDP_DROP)
939 		return LIBETH_XDP_DROP;
940 
941 	sinfo = xdp_get_shared_info_from_frame(xdpf);
942 	nr_frags = sinfo->nr_frags;
943 
944 	for (i = 0; i < nr_frags; i++) {
945 		if (unlikely(bq->count == LIBETH_XDP_TX_BULK) &&
946 		    unlikely(!flush_bulk(bq, LIBETH_XDP_TX_NDO)))
947 			break;
948 
949 		if (!libeth_xdp_xmit_queue_frag(bq, &sinfo->frags[i], dev))
950 			break;
951 	}
952 
953 	if (unlikely(i < nr_frags))
954 		ret = LIBETH_XDP_ABORTED;
955 
956 out:
957 	bq->bulk[bq->count - 1].flags |= LIBETH_XDP_TX_LAST;
958 
959 	return ret;
960 }
961 
962 /**
963  * libeth_xdp_xmit_fill_buf - internal helper to fill one XDP xmit &libeth_sqe
964  * @frm: XDP Tx frame from the bulk
965  * @i: index on the HW queue
966  * @sq: XDPSQ abstraction for the queue
967  * @priv: private data
968  *
969  * Return: XDP Tx descriptor with the mapped DMA and other info to pass to
970  * the driver callback.
971  */
972 static inline struct libeth_xdp_tx_desc
973 libeth_xdp_xmit_fill_buf(struct libeth_xdp_tx_frame frm, u32 i,
974 			 const struct libeth_xdpsq *sq, u64 priv)
975 {
976 	struct libeth_xdp_tx_desc desc;
977 	struct libeth_sqe *sqe;
978 	struct xdp_frame *xdpf;
979 
980 	if (frm.flags & LIBETH_XDP_TX_FIRST) {
981 		xdpf = frm.xdpf;
982 		desc.addr = *libeth_xdp_xmit_frame_dma(xdpf);
983 	} else {
984 		xdpf = NULL;
985 		desc.addr = frm.dma;
986 	}
987 	desc.opts = frm.opts;
988 
989 	sqe = &sq->sqes[i];
990 	dma_unmap_addr_set(sqe, dma, desc.addr);
991 	dma_unmap_len_set(sqe, len, desc.len);
992 
993 	if (!xdpf) {
994 		sqe->type = LIBETH_SQE_XDP_XMIT_FRAG;
995 		return desc;
996 	}
997 
998 	sqe->type = LIBETH_SQE_XDP_XMIT;
999 	sqe->xdpf = xdpf;
1000 	libeth_xdp_tx_fill_stats(sqe, &desc,
1001 				 xdp_get_shared_info_from_frame(xdpf));
1002 
1003 	return desc;
1004 }
1005 
1006 /**
1007  * libeth_xdp_xmit_flush_bulk - wrapper to define flush of one XDP xmit bulk
1008  * @bq: bulk to flush
1009  * @flags: Tx flags, see __libeth_xdp_tx_flush_bulk()
1010  * @prep: driver callback to prepare the queue
1011  * @xmit: driver callback to fill a HW descriptor
1012  *
1013  * Use via LIBETH_XDP_DEFINE_FLUSH_XMIT() to define an XDP xmit driver
1014  * callback.
1015  */
1016 #define libeth_xdp_xmit_flush_bulk(bq, flags, prep, xmit)		      \
1017 	__libeth_xdp_tx_flush_bulk(bq, (flags) | LIBETH_XDP_TX_NDO, prep,     \
1018 				   libeth_xdp_xmit_fill_buf, xmit)
1019 
1020 u32 libeth_xdp_xmit_return_bulk(const struct libeth_xdp_tx_frame *bq,
1021 				u32 count, const struct net_device *dev);
1022 
1023 /**
1024  * __libeth_xdp_xmit_do_bulk - internal function to implement .ndo_xdp_xmit()
1025  * @bq: XDP Tx bulk to queue frames to
1026  * @frames: XDP frames passed by the stack
1027  * @n: number of frames
1028  * @flags: flags passed by the stack
1029  * @flush_bulk: driver callback to flush an XDP xmit bulk
1030  * @finalize: driver callback to finalize sending XDP Tx frames on the queue
1031  *
1032  * Perform common checks, map the frags and queue them to the bulk, then flush
1033  * the bulk to the XDPSQ. If requested by the stack, finalize the queue.
1034  *
1035  * Return: number of frames send or -errno on error.
1036  */
1037 static __always_inline int
1038 __libeth_xdp_xmit_do_bulk(struct libeth_xdp_tx_bulk *bq,
1039 			  struct xdp_frame **frames, u32 n, u32 flags,
1040 			  bool (*flush_bulk)(struct libeth_xdp_tx_bulk *bq,
1041 					     u32 flags),
1042 			  void (*finalize)(void *xdpsq, bool sent, bool flush))
1043 {
1044 	u32 nxmit = 0;
1045 
1046 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
1047 		return -EINVAL;
1048 
1049 	for (u32 i = 0; likely(i < n); i++) {
1050 		u32 ret;
1051 
1052 		ret = libeth_xdp_xmit_queue_bulk(bq, frames[i], flush_bulk);
1053 		if (unlikely(ret != LIBETH_XDP_TX)) {
1054 			nxmit += ret == LIBETH_XDP_ABORTED;
1055 			break;
1056 		}
1057 
1058 		nxmit++;
1059 	}
1060 
1061 	if (bq->count) {
1062 		flush_bulk(bq, LIBETH_XDP_TX_NDO);
1063 		if (unlikely(bq->count))
1064 			nxmit -= libeth_xdp_xmit_return_bulk(bq->bulk,
1065 							     bq->count,
1066 							     bq->dev);
1067 	}
1068 
1069 	finalize(bq->xdpsq, nxmit, flags & XDP_XMIT_FLUSH);
1070 
1071 	return nxmit;
1072 }
1073 
1074 /**
1075  * libeth_xdp_xmit_do_bulk - implement full .ndo_xdp_xmit() in driver
1076  * @dev: target &net_device
1077  * @n: number of frames to send
1078  * @fr: XDP frames to send
1079  * @f: flags passed by the stack
1080  * @xqs: array of XDPSQs driver structs
1081  * @nqs: number of active XDPSQs, the above array length
1082  * @fl: driver callback to flush an XDP xmit bulk
1083  * @fin: driver cabback to finalize the queue
1084  *
1085  * If the driver has active XDPSQs, perform common checks and send the frames.
1086  * Finalize the queue, if requested.
1087  *
1088  * Return: number of frames sent or -errno on error.
1089  */
1090 #define libeth_xdp_xmit_do_bulk(dev, n, fr, f, xqs, nqs, fl, fin)	      \
1091 	_libeth_xdp_xmit_do_bulk(dev, n, fr, f, xqs, nqs, fl, fin,	      \
1092 				 __UNIQUE_ID(bq_), __UNIQUE_ID(ret_),	      \
1093 				 __UNIQUE_ID(nqs_))
1094 
1095 #define _libeth_xdp_xmit_do_bulk(d, n, fr, f, xqs, nqs, fl, fin, ub, ur, un)  \
1096 ({									      \
1097 	u32 un = (nqs);							      \
1098 	int ur;								      \
1099 									      \
1100 	if (likely(un)) {						      \
1101 		LIBETH_XDP_ONSTACK_BULK(ub);				      \
1102 									      \
1103 		libeth_xdp_xmit_init_bulk(&ub, d, xqs, un);		      \
1104 		ur = __libeth_xdp_xmit_do_bulk(&ub, fr, n, f, fl, fin);	      \
1105 	} else {							      \
1106 		ur = -ENXIO;						      \
1107 	}								      \
1108 									      \
1109 	ur;								      \
1110 })
1111 
1112 /* Rx polling path */
1113 
1114 /**
1115  * libeth_xdp_tx_init_bulk - initialize an XDP Tx bulk for Rx NAPI poll
1116  * @bq: bulk to initialize
1117  * @prog: RCU pointer to the XDP program (can be %NULL)
1118  * @dev: target &net_device
1119  * @xdpsqs: array of driver XDPSQ structs
1120  * @num: number of active XDPSQs, the above array length
1121  *
1122  * Should be called on an onstack XDP Tx bulk before the NAPI polling loop.
1123  * Initializes all the needed fields to run libeth_xdp functions. If @num == 0,
1124  * assumes XDP is not enabled.
1125  * Do not use for XSk, it has its own optimized helper.
1126  */
1127 #define libeth_xdp_tx_init_bulk(bq, prog, dev, xdpsqs, num)		      \
1128 	__libeth_xdp_tx_init_bulk(bq, prog, dev, xdpsqs, num, false,	      \
1129 				  __UNIQUE_ID(bq_), __UNIQUE_ID(nqs_))
1130 
1131 #define __libeth_xdp_tx_init_bulk(bq, pr, d, xdpsqs, num, xsk, ub, un) do {   \
1132 	typeof(bq) ub = (bq);						      \
1133 	u32 un = (num);							      \
1134 									      \
1135 	rcu_read_lock();						      \
1136 									      \
1137 	if (un || (xsk)) {						      \
1138 		ub->prog = rcu_dereference(pr);				      \
1139 		ub->dev = (d);						      \
1140 		ub->xdpsq = (xdpsqs)[libeth_xdpsq_id(un)];		      \
1141 	} else {							      \
1142 		ub->prog = NULL;					      \
1143 	}								      \
1144 									      \
1145 	ub->act_mask = 0;						      \
1146 	ub->count = 0;							      \
1147 } while (0)
1148 
1149 void libeth_xdp_load_stash(struct libeth_xdp_buff *dst,
1150 			   const struct libeth_xdp_buff_stash *src);
1151 void libeth_xdp_save_stash(struct libeth_xdp_buff_stash *dst,
1152 			   const struct libeth_xdp_buff *src);
1153 void __libeth_xdp_return_stash(struct libeth_xdp_buff_stash *stash);
1154 
1155 /**
1156  * libeth_xdp_init_buff - initialize a &libeth_xdp_buff for Rx NAPI poll
1157  * @dst: onstack buffer to initialize
1158  * @src: XDP buffer stash placed on the queue
1159  * @rxq: registered &xdp_rxq_info corresponding to this queue
1160  *
1161  * Should be called before the main NAPI polling loop. Loads the content of
1162  * the previously saved stash or initializes the buffer from scratch.
1163  * Do not use for XSk.
1164  */
1165 static inline void
1166 libeth_xdp_init_buff(struct libeth_xdp_buff *dst,
1167 		     const struct libeth_xdp_buff_stash *src,
1168 		     struct xdp_rxq_info *rxq)
1169 {
1170 	if (likely(!src->data))
1171 		dst->data = NULL;
1172 	else
1173 		libeth_xdp_load_stash(dst, src);
1174 
1175 	dst->base.rxq = rxq;
1176 }
1177 
1178 /**
1179  * libeth_xdp_save_buff - save a partially built buffer on a queue
1180  * @dst: XDP buffer stash placed on the queue
1181  * @src: onstack buffer to save
1182  *
1183  * Should be called after the main NAPI polling loop. If the loop exited before
1184  * the buffer was finished, saves its content on the queue, so that it can be
1185  * completed during the next poll. Otherwise, clears the stash.
1186  */
1187 static inline void libeth_xdp_save_buff(struct libeth_xdp_buff_stash *dst,
1188 					const struct libeth_xdp_buff *src)
1189 {
1190 	if (likely(!src->data))
1191 		dst->data = NULL;
1192 	else
1193 		libeth_xdp_save_stash(dst, src);
1194 }
1195 
1196 /**
1197  * libeth_xdp_return_stash - free an XDP buffer stash from a queue
1198  * @stash: stash to free
1199  *
1200  * If the queue is about to be destroyed, but it still has an incompleted
1201  * buffer stash, this helper should be called to free it.
1202  */
1203 static inline void libeth_xdp_return_stash(struct libeth_xdp_buff_stash *stash)
1204 {
1205 	if (stash->data)
1206 		__libeth_xdp_return_stash(stash);
1207 }
1208 
1209 static inline void libeth_xdp_return_va(const void *data, bool napi)
1210 {
1211 	netmem_ref netmem = virt_to_netmem(data);
1212 
1213 	page_pool_put_full_netmem(__netmem_get_pp(netmem), netmem, napi);
1214 }
1215 
1216 static inline void libeth_xdp_return_frags(const struct skb_shared_info *sinfo,
1217 					   bool napi)
1218 {
1219 	for (u32 i = 0; i < sinfo->nr_frags; i++) {
1220 		netmem_ref netmem = skb_frag_netmem(&sinfo->frags[i]);
1221 
1222 		page_pool_put_full_netmem(netmem_get_pp(netmem), netmem, napi);
1223 	}
1224 }
1225 
1226 /**
1227  * libeth_xdp_return_buff - free/recycle &libeth_xdp_buff
1228  * @xdp: buffer to free
1229  *
1230  * Hotpath helper to free &libeth_xdp_buff. Comparing to xdp_return_buff(),
1231  * it's faster as it gets inlined and always assumes order-0 pages and safe
1232  * direct recycling. Zeroes @xdp->data to avoid UAFs.
1233  */
1234 #define libeth_xdp_return_buff(xdp)	__libeth_xdp_return_buff(xdp, true)
1235 
1236 static inline void __libeth_xdp_return_buff(struct libeth_xdp_buff *xdp,
1237 					    bool napi)
1238 {
1239 	if (!xdp_buff_has_frags(&xdp->base))
1240 		goto out;
1241 
1242 	libeth_xdp_return_frags(xdp_get_shared_info_from_buff(&xdp->base),
1243 				napi);
1244 
1245 out:
1246 	libeth_xdp_return_va(xdp->data, napi);
1247 	xdp->data = NULL;
1248 }
1249 
1250 bool libeth_xdp_buff_add_frag(struct libeth_xdp_buff *xdp,
1251 			      const struct libeth_fqe *fqe,
1252 			      u32 len);
1253 
1254 /**
1255  * libeth_xdp_prepare_buff - fill &libeth_xdp_buff with head FQE data
1256  * @xdp: XDP buffer to attach the head to
1257  * @fqe: FQE containing the head buffer
1258  * @len: buffer len passed from HW
1259  *
1260  * Internal, use libeth_xdp_process_buff() instead. Initializes XDP buffer
1261  * head with the Rx buffer data: data pointer, length, headroom, and
1262  * truesize/tailroom. Zeroes the flags.
1263  */
1264 static inline void libeth_xdp_prepare_buff(struct libeth_xdp_buff *xdp,
1265 					   const struct libeth_fqe *fqe,
1266 					   u32 len)
1267 {
1268 	const struct page *page = __netmem_to_page(fqe->netmem);
1269 
1270 	xdp_init_buff(&xdp->base, fqe->truesize, xdp->base.rxq);
1271 	xdp_prepare_buff(&xdp->base, page_address(page) + fqe->offset,
1272 			 page->pp->p.offset, len, true);
1273 }
1274 
1275 /**
1276  * libeth_xdp_process_buff - attach Rx buffer to &libeth_xdp_buff
1277  * @xdp: XDP buffer to attach the Rx buffer to
1278  * @fqe: Rx buffer to process
1279  * @len: received data length from the descriptor
1280  *
1281  * If the XDP buffer is empty, attaches the Rx buffer as head and initializes
1282  * the required fields. Otherwise, attaches the buffer as a frag.
1283  * Already performs DMA sync-for-CPU and frame start prefetch
1284  * (for head buffers only).
1285  *
1286  * Return: true on success, false if the descriptor must be skipped (empty or
1287  * no space for a new frag).
1288  */
1289 static inline bool libeth_xdp_process_buff(struct libeth_xdp_buff *xdp,
1290 					   const struct libeth_fqe *fqe,
1291 					   u32 len)
1292 {
1293 	if (!libeth_rx_sync_for_cpu(fqe, len))
1294 		return false;
1295 
1296 	if (xdp->data)
1297 		return libeth_xdp_buff_add_frag(xdp, fqe, len);
1298 
1299 	libeth_xdp_prepare_buff(xdp, fqe, len);
1300 
1301 	prefetch(xdp->data);
1302 
1303 	return true;
1304 }
1305 
1306 /**
1307  * libeth_xdp_buff_stats_frags - update onstack RQ stats with XDP frags info
1308  * @ss: onstack stats to update
1309  * @xdp: buffer to account
1310  *
1311  * Internal helper used by __libeth_xdp_run_pass(), do not call directly.
1312  * Adds buffer's frags count and total len to the onstack stats.
1313  */
1314 static inline void
1315 libeth_xdp_buff_stats_frags(struct libeth_rq_napi_stats *ss,
1316 			    const struct libeth_xdp_buff *xdp)
1317 {
1318 	const struct skb_shared_info *sinfo;
1319 
1320 	sinfo = xdp_get_shared_info_from_buff(&xdp->base);
1321 	ss->bytes += sinfo->xdp_frags_size;
1322 	ss->fragments += sinfo->nr_frags + 1;
1323 }
1324 
1325 u32 libeth_xdp_prog_exception(const struct libeth_xdp_tx_bulk *bq,
1326 			      struct libeth_xdp_buff *xdp,
1327 			      enum xdp_action act, int ret);
1328 
1329 /**
1330  * __libeth_xdp_run_prog - run XDP program on an XDP buffer
1331  * @xdp: XDP buffer to run the prog on
1332  * @bq: buffer bulk for ``XDP_TX`` queueing
1333  *
1334  * Internal inline abstraction to run XDP program. Handles ``XDP_DROP``
1335  * and ``XDP_REDIRECT`` only, the rest is processed levels up.
1336  * Reports an XDP prog exception on errors.
1337  *
1338  * Return: libeth_xdp prog verdict depending on the prog's verdict.
1339  */
1340 static __always_inline u32
1341 __libeth_xdp_run_prog(struct libeth_xdp_buff *xdp,
1342 		      const struct libeth_xdp_tx_bulk *bq)
1343 {
1344 	enum xdp_action act;
1345 
1346 	act = bpf_prog_run_xdp(bq->prog, &xdp->base);
1347 	if (unlikely(act < XDP_DROP || act > XDP_REDIRECT))
1348 		goto out;
1349 
1350 	switch (act) {
1351 	case XDP_PASS:
1352 		return LIBETH_XDP_PASS;
1353 	case XDP_DROP:
1354 		libeth_xdp_return_buff(xdp);
1355 
1356 		return LIBETH_XDP_DROP;
1357 	case XDP_TX:
1358 		return LIBETH_XDP_TX;
1359 	case XDP_REDIRECT:
1360 		if (unlikely(xdp_do_redirect(bq->dev, &xdp->base, bq->prog)))
1361 			break;
1362 
1363 		xdp->data = NULL;
1364 
1365 		return LIBETH_XDP_REDIRECT;
1366 	default:
1367 		break;
1368 	}
1369 
1370 out:
1371 	return libeth_xdp_prog_exception(bq, xdp, act, 0);
1372 }
1373 
1374 /**
1375  * __libeth_xdp_run_flush - run XDP program and handle ``XDP_TX`` verdict
1376  * @xdp: XDP buffer to run the prog on
1377  * @bq: buffer bulk for ``XDP_TX`` queueing
1378  * @run: internal callback for running XDP program
1379  * @queue: internal callback for queuing ``XDP_TX`` frame
1380  * @flush_bulk: driver callback for flushing a bulk
1381  *
1382  * Internal inline abstraction to run XDP program and additionally handle
1383  * ``XDP_TX`` verdict. Used by both XDP and XSk, hence @run and @queue.
1384  * Do not use directly.
1385  *
1386  * Return: libeth_xdp prog verdict depending on the prog's verdict.
1387  */
1388 static __always_inline u32
1389 __libeth_xdp_run_flush(struct libeth_xdp_buff *xdp,
1390 		       struct libeth_xdp_tx_bulk *bq,
1391 		       u32 (*run)(struct libeth_xdp_buff *xdp,
1392 				  const struct libeth_xdp_tx_bulk *bq),
1393 		       bool (*queue)(struct libeth_xdp_tx_bulk *bq,
1394 				     struct libeth_xdp_buff *xdp,
1395 				     bool (*flush_bulk)
1396 					  (struct libeth_xdp_tx_bulk *bq,
1397 					   u32 flags)),
1398 		       bool (*flush_bulk)(struct libeth_xdp_tx_bulk *bq,
1399 					  u32 flags))
1400 {
1401 	u32 act;
1402 
1403 	act = run(xdp, bq);
1404 	if (act == LIBETH_XDP_TX && unlikely(!queue(bq, xdp, flush_bulk)))
1405 		act = LIBETH_XDP_DROP;
1406 
1407 	bq->act_mask |= act;
1408 
1409 	return act;
1410 }
1411 
1412 /**
1413  * libeth_xdp_run_prog - run XDP program (non-XSk path) and handle all verdicts
1414  * @xdp: XDP buffer to process
1415  * @bq: XDP Tx bulk to queue ``XDP_TX`` buffers
1416  * @fl: driver ``XDP_TX`` bulk flush callback
1417  *
1418  * Run the attached XDP program and handle all possible verdicts. XSk has its
1419  * own version.
1420  * Prefer using it via LIBETH_XDP_DEFINE_RUN{,_PASS,_PROG}().
1421  *
1422  * Return: true if the buffer should be passed up the stack, false if the poll
1423  * should go to the next buffer.
1424  */
1425 #define libeth_xdp_run_prog(xdp, bq, fl)				      \
1426 	(__libeth_xdp_run_flush(xdp, bq, __libeth_xdp_run_prog,		      \
1427 				libeth_xdp_tx_queue_bulk,		      \
1428 				fl) == LIBETH_XDP_PASS)
1429 
1430 /**
1431  * __libeth_xdp_run_pass - helper to run XDP program and handle the result
1432  * @xdp: XDP buffer to process
1433  * @bq: XDP Tx bulk to queue ``XDP_TX`` frames
1434  * @napi: NAPI to build an skb and pass it up the stack
1435  * @rs: onstack libeth RQ stats
1436  * @md: metadata that should be filled to the XDP buffer
1437  * @prep: callback for filling the metadata
1438  * @run: driver wrapper to run XDP program
1439  * @populate: driver callback to populate an skb with the HW descriptor data
1440  *
1441  * Inline abstraction that does the following (non-XSk path):
1442  * 1) adds frame size and frag number (if needed) to the onstack stats;
1443  * 2) fills the descriptor metadata to the onstack &libeth_xdp_buff
1444  * 3) runs XDP program if present;
1445  * 4) handles all possible verdicts;
1446  * 5) on ``XDP_PASS`, builds an skb from the buffer;
1447  * 6) populates it with the descriptor metadata;
1448  * 7) passes it up the stack.
1449  *
1450  * In most cases, number 2 means just writing the pointer to the HW descriptor
1451  * to the XDP buffer. If so, please use LIBETH_XDP_DEFINE_RUN{,_PASS}()
1452  * wrappers to build a driver function.
1453  */
1454 static __always_inline void
1455 __libeth_xdp_run_pass(struct libeth_xdp_buff *xdp,
1456 		      struct libeth_xdp_tx_bulk *bq, struct napi_struct *napi,
1457 		      struct libeth_rq_napi_stats *rs, const void *md,
1458 		      void (*prep)(struct libeth_xdp_buff *xdp,
1459 				   const void *md),
1460 		      bool (*run)(struct libeth_xdp_buff *xdp,
1461 				  struct libeth_xdp_tx_bulk *bq),
1462 		      bool (*populate)(struct sk_buff *skb,
1463 				       const struct libeth_xdp_buff *xdp,
1464 				       struct libeth_rq_napi_stats *rs))
1465 {
1466 	struct sk_buff *skb;
1467 
1468 	rs->bytes += xdp->base.data_end - xdp->data;
1469 	rs->packets++;
1470 
1471 	if (xdp_buff_has_frags(&xdp->base))
1472 		libeth_xdp_buff_stats_frags(rs, xdp);
1473 
1474 	if (prep && (!__builtin_constant_p(!!md) || md))
1475 		prep(xdp, md);
1476 
1477 	if (!bq || !run || !bq->prog)
1478 		goto build;
1479 
1480 	if (!run(xdp, bq))
1481 		return;
1482 
1483 build:
1484 	skb = xdp_build_skb_from_buff(&xdp->base);
1485 	if (unlikely(!skb)) {
1486 		libeth_xdp_return_buff_slow(xdp);
1487 		return;
1488 	}
1489 
1490 	xdp->data = NULL;
1491 
1492 	if (unlikely(!populate(skb, xdp, rs))) {
1493 		napi_consume_skb(skb, true);
1494 		return;
1495 	}
1496 
1497 	napi_gro_receive(napi, skb);
1498 }
1499 
1500 static inline void libeth_xdp_prep_desc(struct libeth_xdp_buff *xdp,
1501 					const void *desc)
1502 {
1503 	xdp->desc = desc;
1504 }
1505 
1506 /**
1507  * libeth_xdp_run_pass - helper to run XDP program and handle the result
1508  * @xdp: XDP buffer to process
1509  * @bq: XDP Tx bulk to queue ``XDP_TX`` frames
1510  * @napi: NAPI to build an skb and pass it up the stack
1511  * @ss: onstack libeth RQ stats
1512  * @desc: pointer to the HW descriptor for that frame
1513  * @run: driver wrapper to run XDP program
1514  * @populate: driver callback to populate an skb with the HW descriptor data
1515  *
1516  * Wrapper around the underscored version when "fill the descriptor metadata"
1517  * means just writing the pointer to the HW descriptor as @xdp->desc.
1518  */
1519 #define libeth_xdp_run_pass(xdp, bq, napi, ss, desc, run, populate)	      \
1520 	__libeth_xdp_run_pass(xdp, bq, napi, ss, desc, libeth_xdp_prep_desc,  \
1521 			      run, populate)
1522 
1523 /**
1524  * libeth_xdp_finalize_rx - finalize XDPSQ after a NAPI polling loop (non-XSk)
1525  * @bq: ``XDP_TX`` frame bulk
1526  * @flush: driver callback to flush the bulk
1527  * @finalize: driver callback to start sending the frames and run the timer
1528  *
1529  * Flush the bulk if there are frames left to send, kick the queue and flush
1530  * the XDP maps.
1531  */
1532 #define libeth_xdp_finalize_rx(bq, flush, finalize)			      \
1533 	__libeth_xdp_finalize_rx(bq, 0, flush, finalize)
1534 
1535 static __always_inline void
1536 __libeth_xdp_finalize_rx(struct libeth_xdp_tx_bulk *bq, u32 flags,
1537 			 bool (*flush_bulk)(struct libeth_xdp_tx_bulk *bq,
1538 					    u32 flags),
1539 			 void (*finalize)(void *xdpsq, bool sent, bool flush))
1540 {
1541 	if (bq->act_mask & LIBETH_XDP_TX) {
1542 		if (bq->count)
1543 			flush_bulk(bq, flags | LIBETH_XDP_TX_DROP);
1544 		finalize(bq->xdpsq, true, true);
1545 	}
1546 	if (bq->act_mask & LIBETH_XDP_REDIRECT)
1547 		xdp_do_flush();
1548 
1549 	rcu_read_unlock();
1550 }
1551 
1552 /*
1553  * Helpers to reduce boilerplate code in drivers.
1554  *
1555  * Typical driver Rx flow would be (excl. bulk and buff init, frag attach):
1556  *
1557  * LIBETH_XDP_DEFINE_START();
1558  * LIBETH_XDP_DEFINE_FLUSH_TX(static driver_xdp_flush_tx, driver_xdp_tx_prep,
1559  *			      driver_xdp_xmit);
1560  * LIBETH_XDP_DEFINE_RUN(static driver_xdp_run, driver_xdp_run_prog,
1561  *			 driver_xdp_flush_tx, driver_populate_skb);
1562  * LIBETH_XDP_DEFINE_FINALIZE(static driver_xdp_finalize_rx,
1563  *			      driver_xdp_flush_tx, driver_xdp_finalize_sq);
1564  * LIBETH_XDP_DEFINE_END();
1565  *
1566  * This will build a set of 4 static functions. The compiler is free to decide
1567  * whether to inline them.
1568  * Then, in the NAPI polling function:
1569  *
1570  *	while (packets < budget) {
1571  *		// ...
1572  *		driver_xdp_run(xdp, &bq, napi, &rs, desc);
1573  *	}
1574  *	driver_xdp_finalize_rx(&bq);
1575  */
1576 
1577 #define LIBETH_XDP_DEFINE_START()					      \
1578 	__diag_push();							      \
1579 	__diag_ignore(GCC, 8, "-Wold-style-declaration",		      \
1580 		      "Allow specifying \'static\' after the return type")
1581 
1582 /**
1583  * LIBETH_XDP_DEFINE_TIMER - define a driver XDPSQ cleanup timer callback
1584  * @name: name of the function to define
1585  * @poll: Tx polling/completion function
1586  */
1587 #define LIBETH_XDP_DEFINE_TIMER(name, poll)				      \
1588 void name(struct work_struct *work)					      \
1589 {									      \
1590 	libeth_xdpsq_run_timer(work, poll);				      \
1591 }
1592 
1593 /**
1594  * LIBETH_XDP_DEFINE_FLUSH_TX - define a driver ``XDP_TX`` bulk flush function
1595  * @name: name of the function to define
1596  * @prep: driver callback to clean an XDPSQ
1597  * @xmit: driver callback to write a HW Tx descriptor
1598  */
1599 #define LIBETH_XDP_DEFINE_FLUSH_TX(name, prep, xmit)			      \
1600 	__LIBETH_XDP_DEFINE_FLUSH_TX(name, prep, xmit, xdp)
1601 
1602 #define __LIBETH_XDP_DEFINE_FLUSH_TX(name, prep, xmit, pfx)		      \
1603 bool name(struct libeth_xdp_tx_bulk *bq, u32 flags)			      \
1604 {									      \
1605 	return libeth_##pfx##_tx_flush_bulk(bq, flags, prep, xmit);	      \
1606 }
1607 
1608 /**
1609  * LIBETH_XDP_DEFINE_FLUSH_XMIT - define a driver XDP xmit bulk flush function
1610  * @name: name of the function to define
1611  * @prep: driver callback to clean an XDPSQ
1612  * @xmit: driver callback to write a HW Tx descriptor
1613  */
1614 #define LIBETH_XDP_DEFINE_FLUSH_XMIT(name, prep, xmit)			      \
1615 bool name(struct libeth_xdp_tx_bulk *bq, u32 flags)			      \
1616 {									      \
1617 	return libeth_xdp_xmit_flush_bulk(bq, flags, prep, xmit);	      \
1618 }
1619 
1620 /**
1621  * LIBETH_XDP_DEFINE_RUN_PROG - define a driver XDP program run function
1622  * @name: name of the function to define
1623  * @flush: driver callback to flush an ``XDP_TX`` bulk
1624  */
1625 #define LIBETH_XDP_DEFINE_RUN_PROG(name, flush)				      \
1626 	bool __LIBETH_XDP_DEFINE_RUN_PROG(name, flush, xdp)
1627 
1628 #define __LIBETH_XDP_DEFINE_RUN_PROG(name, flush, pfx)			      \
1629 name(struct libeth_xdp_buff *xdp, struct libeth_xdp_tx_bulk *bq)	      \
1630 {									      \
1631 	return libeth_##pfx##_run_prog(xdp, bq, flush);			      \
1632 }
1633 
1634 /**
1635  * LIBETH_XDP_DEFINE_RUN_PASS - define a driver buffer process + pass function
1636  * @name: name of the function to define
1637  * @run: driver callback to run XDP program (above)
1638  * @populate: driver callback to fill an skb with HW descriptor info
1639  */
1640 #define LIBETH_XDP_DEFINE_RUN_PASS(name, run, populate)			      \
1641 	void __LIBETH_XDP_DEFINE_RUN_PASS(name, run, populate, xdp)
1642 
1643 #define __LIBETH_XDP_DEFINE_RUN_PASS(name, run, populate, pfx)		      \
1644 name(struct libeth_xdp_buff *xdp, struct libeth_xdp_tx_bulk *bq,	      \
1645      struct napi_struct *napi, struct libeth_rq_napi_stats *ss,		      \
1646      const void *desc)							      \
1647 {									      \
1648 	return libeth_##pfx##_run_pass(xdp, bq, napi, ss, desc, run,	      \
1649 				       populate);			      \
1650 }
1651 
1652 /**
1653  * LIBETH_XDP_DEFINE_RUN - define a driver buffer process, run + pass function
1654  * @name: name of the function to define
1655  * @run: name of the XDP prog run function to define
1656  * @flush: driver callback to flush an ``XDP_TX`` bulk
1657  * @populate: driver callback to fill an skb with HW descriptor info
1658  */
1659 #define LIBETH_XDP_DEFINE_RUN(name, run, flush, populate)		      \
1660 	__LIBETH_XDP_DEFINE_RUN(name, run, flush, populate, XDP)
1661 
1662 #define __LIBETH_XDP_DEFINE_RUN(name, run, flush, populate, pfx)	      \
1663 	LIBETH_##pfx##_DEFINE_RUN_PROG(static run, flush);		      \
1664 	LIBETH_##pfx##_DEFINE_RUN_PASS(name, run, populate)
1665 
1666 /**
1667  * LIBETH_XDP_DEFINE_FINALIZE - define a driver Rx NAPI poll finalize function
1668  * @name: name of the function to define
1669  * @flush: driver callback to flush an ``XDP_TX`` bulk
1670  * @finalize: driver callback to finalize an XDPSQ and run the timer
1671  */
1672 #define LIBETH_XDP_DEFINE_FINALIZE(name, flush, finalize)		      \
1673 	__LIBETH_XDP_DEFINE_FINALIZE(name, flush, finalize, xdp)
1674 
1675 #define __LIBETH_XDP_DEFINE_FINALIZE(name, flush, finalize, pfx)	      \
1676 void name(struct libeth_xdp_tx_bulk *bq)				      \
1677 {									      \
1678 	libeth_##pfx##_finalize_rx(bq, flush, finalize);		      \
1679 }
1680 
1681 #define LIBETH_XDP_DEFINE_END()		__diag_pop()
1682 
1683 /* XMO */
1684 
1685 /**
1686  * libeth_xdp_buff_to_rq - get RQ pointer from an XDP buffer pointer
1687  * @xdp: &libeth_xdp_buff corresponding to the queue
1688  * @type: typeof() of the driver Rx queue structure
1689  * @member: name of &xdp_rxq_info inside @type
1690  *
1691  * Often times, pointer to the RQ is needed when reading/filling metadata from
1692  * HW descriptors. The helper can be used to quickly jump from an XDP buffer
1693  * to the queue corresponding to its &xdp_rxq_info without introducing
1694  * additional fields (&libeth_xdp_buff is precisely 1 cacheline long on x64).
1695  */
1696 #define libeth_xdp_buff_to_rq(xdp, type, member)			      \
1697 	container_of_const((xdp)->base.rxq, type, member)
1698 
1699 /**
1700  * libeth_xdpmo_rx_hash - convert &libeth_rx_pt to an XDP RSS hash metadata
1701  * @hash: pointer to the variable to write the hash to
1702  * @rss_type: pointer to the variable to write the hash type to
1703  * @val: hash value from the HW descriptor
1704  * @pt: libeth parsed packet type
1705  *
1706  * Handle zeroed/non-available hash and convert libeth parsed packet type to
1707  * the corresponding XDP RSS hash type. To be called at the end of
1708  * xdp_metadata_ops idpf_xdpmo::xmo_rx_hash() implementation.
1709  * Note that if the driver doesn't use a constant packet type lookup table but
1710  * generates it at runtime, it must call libeth_rx_pt_gen_hash_type(pt) to
1711  * generate XDP RSS hash type for each packet type.
1712  *
1713  * Return: 0 on success, -ENODATA when the hash is not available.
1714  */
1715 static inline int libeth_xdpmo_rx_hash(u32 *hash,
1716 				       enum xdp_rss_hash_type *rss_type,
1717 				       u32 val, struct libeth_rx_pt pt)
1718 {
1719 	if (unlikely(!val))
1720 		return -ENODATA;
1721 
1722 	*hash = val;
1723 	*rss_type = pt.hash_type;
1724 
1725 	return 0;
1726 }
1727 
1728 /* Tx buffer completion */
1729 
1730 void libeth_xdp_return_buff_bulk(const struct skb_shared_info *sinfo,
1731 				 struct xdp_frame_bulk *bq, bool frags);
1732 void libeth_xsk_buff_free_slow(struct libeth_xdp_buff *xdp);
1733 
1734 /**
1735  * __libeth_xdp_complete_tx - complete sent XDPSQE
1736  * @sqe: SQ element / Tx buffer to complete
1737  * @cp: Tx polling/completion params
1738  * @bulk: internal callback to bulk-free ``XDP_TX`` buffers
1739  * @xsk: internal callback to free XSk ``XDP_TX`` buffers
1740  *
1741  * Use the non-underscored version in drivers instead. This one is shared
1742  * internally with libeth_tx_complete_any().
1743  * Complete an XDPSQE of any type of XDP frame. This includes DMA unmapping
1744  * when needed, buffer freeing, stats update, and SQE invalidation.
1745  */
1746 static __always_inline void
1747 __libeth_xdp_complete_tx(struct libeth_sqe *sqe, struct libeth_cq_pp *cp,
1748 			 typeof(libeth_xdp_return_buff_bulk) bulk,
1749 			 typeof(libeth_xsk_buff_free_slow) xsk)
1750 {
1751 	enum libeth_sqe_type type = sqe->type;
1752 
1753 	switch (type) {
1754 	case LIBETH_SQE_EMPTY:
1755 		return;
1756 	case LIBETH_SQE_XDP_XMIT:
1757 	case LIBETH_SQE_XDP_XMIT_FRAG:
1758 		dma_unmap_page(cp->dev, dma_unmap_addr(sqe, dma),
1759 			       dma_unmap_len(sqe, len), DMA_TO_DEVICE);
1760 		break;
1761 	default:
1762 		break;
1763 	}
1764 
1765 	switch (type) {
1766 	case LIBETH_SQE_XDP_TX:
1767 		bulk(sqe->sinfo, cp->bq, sqe->nr_frags != 1);
1768 		break;
1769 	case LIBETH_SQE_XDP_XMIT:
1770 		xdp_return_frame_bulk(sqe->xdpf, cp->bq);
1771 		break;
1772 	case LIBETH_SQE_XSK_TX:
1773 	case LIBETH_SQE_XSK_TX_FRAG:
1774 		xsk(sqe->xsk);
1775 		break;
1776 	default:
1777 		break;
1778 	}
1779 
1780 	switch (type) {
1781 	case LIBETH_SQE_XDP_TX:
1782 	case LIBETH_SQE_XDP_XMIT:
1783 	case LIBETH_SQE_XSK_TX:
1784 		cp->xdp_tx -= sqe->nr_frags;
1785 
1786 		cp->xss->packets++;
1787 		cp->xss->bytes += sqe->bytes;
1788 		break;
1789 	default:
1790 		break;
1791 	}
1792 
1793 	sqe->type = LIBETH_SQE_EMPTY;
1794 }
1795 
1796 static inline void libeth_xdp_complete_tx(struct libeth_sqe *sqe,
1797 					  struct libeth_cq_pp *cp)
1798 {
1799 	__libeth_xdp_complete_tx(sqe, cp, libeth_xdp_return_buff_bulk,
1800 				 libeth_xsk_buff_free_slow);
1801 }
1802 
1803 /* Misc */
1804 
1805 u32 libeth_xdp_queue_threshold(u32 count);
1806 
1807 void __libeth_xdp_set_features(struct net_device *dev,
1808 			       const struct xdp_metadata_ops *xmo,
1809 			       u32 zc_segs,
1810 			       const struct xsk_tx_metadata_ops *tmo);
1811 void libeth_xdp_set_redirect(struct net_device *dev, bool enable);
1812 
1813 /**
1814  * libeth_xdp_set_features - set XDP features for netdev
1815  * @dev: &net_device to configure
1816  * @...: optional params, see __libeth_xdp_set_features()
1817  *
1818  * Set all the features libeth_xdp supports, including .ndo_xdp_xmit(). That
1819  * said, it should be used only when XDPSQs are always available regardless
1820  * of whether an XDP prog is attached to @dev.
1821  */
1822 #define libeth_xdp_set_features(dev, ...)				      \
1823 	CONCATENATE(__libeth_xdp_feat,					      \
1824 		    COUNT_ARGS(__VA_ARGS__))(dev, ##__VA_ARGS__)
1825 
1826 #define __libeth_xdp_feat0(dev)						      \
1827 	__libeth_xdp_set_features(dev, NULL, 0, NULL)
1828 #define __libeth_xdp_feat1(dev, xmo)					      \
1829 	__libeth_xdp_set_features(dev, xmo, 0, NULL)
1830 #define __libeth_xdp_feat2(dev, xmo, zc_segs)				      \
1831 	__libeth_xdp_set_features(dev, xmo, zc_segs, NULL)
1832 #define __libeth_xdp_feat3(dev, xmo, zc_segs, tmo)			      \
1833 	__libeth_xdp_set_features(dev, xmo, zc_segs, tmo)
1834 
1835 /**
1836  * libeth_xdp_set_features_noredir - enable all libeth_xdp features w/o redir
1837  * @dev: target &net_device
1838  * @...: optional params, see __libeth_xdp_set_features()
1839  *
1840  * Enable everything except the .ndo_xdp_xmit() feature, use when XDPSQs are
1841  * not available right after netdev registration.
1842  */
1843 #define libeth_xdp_set_features_noredir(dev, ...)			      \
1844 	__libeth_xdp_set_features_noredir(dev, __UNIQUE_ID(dev_),	      \
1845 					  ##__VA_ARGS__)
1846 
1847 #define __libeth_xdp_set_features_noredir(dev, ud, ...) do {		      \
1848 	struct net_device *ud = (dev);					      \
1849 									      \
1850 	libeth_xdp_set_features(ud, ##__VA_ARGS__);			      \
1851 	libeth_xdp_set_redirect(ud, false);				      \
1852 } while (0)
1853 
1854 #define libeth_xsktmo			((const void *)GOLDEN_RATIO_PRIME)
1855 
1856 #endif /* __LIBETH_XDP_H */
1857