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 */ 1126 #define libeth_xdp_tx_init_bulk(bq, prog, dev, xdpsqs, num) \ 1127 __libeth_xdp_tx_init_bulk(bq, prog, dev, xdpsqs, num, false, \ 1128 __UNIQUE_ID(bq_), __UNIQUE_ID(nqs_)) 1129 1130 #define __libeth_xdp_tx_init_bulk(bq, pr, d, xdpsqs, num, ub, un) do { \ 1131 typeof(bq) ub = (bq); \ 1132 u32 un = (num); \ 1133 \ 1134 rcu_read_lock(); \ 1135 \ 1136 if (un) { \ 1137 ub->prog = rcu_dereference(pr); \ 1138 ub->dev = (d); \ 1139 ub->xdpsq = (xdpsqs)[libeth_xdpsq_id(un)]; \ 1140 } else { \ 1141 ub->prog = NULL; \ 1142 } \ 1143 \ 1144 ub->act_mask = 0; \ 1145 ub->count = 0; \ 1146 } while (0) 1147 1148 void libeth_xdp_load_stash(struct libeth_xdp_buff *dst, 1149 const struct libeth_xdp_buff_stash *src); 1150 void libeth_xdp_save_stash(struct libeth_xdp_buff_stash *dst, 1151 const struct libeth_xdp_buff *src); 1152 void __libeth_xdp_return_stash(struct libeth_xdp_buff_stash *stash); 1153 1154 /** 1155 * libeth_xdp_init_buff - initialize a &libeth_xdp_buff for Rx NAPI poll 1156 * @dst: onstack buffer to initialize 1157 * @src: XDP buffer stash placed on the queue 1158 * @rxq: registered &xdp_rxq_info corresponding to this queue 1159 * 1160 * Should be called before the main NAPI polling loop. Loads the content of 1161 * the previously saved stash or initializes the buffer from scratch. 1162 */ 1163 static inline void 1164 libeth_xdp_init_buff(struct libeth_xdp_buff *dst, 1165 const struct libeth_xdp_buff_stash *src, 1166 struct xdp_rxq_info *rxq) 1167 { 1168 if (likely(!src->data)) 1169 dst->data = NULL; 1170 else 1171 libeth_xdp_load_stash(dst, src); 1172 1173 dst->base.rxq = rxq; 1174 } 1175 1176 /** 1177 * libeth_xdp_save_buff - save a partially built buffer on a queue 1178 * @dst: XDP buffer stash placed on the queue 1179 * @src: onstack buffer to save 1180 * 1181 * Should be called after the main NAPI polling loop. If the loop exited before 1182 * the buffer was finished, saves its content on the queue, so that it can be 1183 * completed during the next poll. Otherwise, clears the stash. 1184 */ 1185 static inline void libeth_xdp_save_buff(struct libeth_xdp_buff_stash *dst, 1186 const struct libeth_xdp_buff *src) 1187 { 1188 if (likely(!src->data)) 1189 dst->data = NULL; 1190 else 1191 libeth_xdp_save_stash(dst, src); 1192 } 1193 1194 /** 1195 * libeth_xdp_return_stash - free an XDP buffer stash from a queue 1196 * @stash: stash to free 1197 * 1198 * If the queue is about to be destroyed, but it still has an incompleted 1199 * buffer stash, this helper should be called to free it. 1200 */ 1201 static inline void libeth_xdp_return_stash(struct libeth_xdp_buff_stash *stash) 1202 { 1203 if (stash->data) 1204 __libeth_xdp_return_stash(stash); 1205 } 1206 1207 static inline void libeth_xdp_return_va(const void *data, bool napi) 1208 { 1209 netmem_ref netmem = virt_to_netmem(data); 1210 1211 page_pool_put_full_netmem(__netmem_get_pp(netmem), netmem, napi); 1212 } 1213 1214 static inline void libeth_xdp_return_frags(const struct skb_shared_info *sinfo, 1215 bool napi) 1216 { 1217 for (u32 i = 0; i < sinfo->nr_frags; i++) { 1218 netmem_ref netmem = skb_frag_netmem(&sinfo->frags[i]); 1219 1220 page_pool_put_full_netmem(netmem_get_pp(netmem), netmem, napi); 1221 } 1222 } 1223 1224 /** 1225 * libeth_xdp_return_buff - free/recycle &libeth_xdp_buff 1226 * @xdp: buffer to free 1227 * 1228 * Hotpath helper to free &libeth_xdp_buff. Comparing to xdp_return_buff(), 1229 * it's faster as it gets inlined and always assumes order-0 pages and safe 1230 * direct recycling. Zeroes @xdp->data to avoid UAFs. 1231 */ 1232 #define libeth_xdp_return_buff(xdp) __libeth_xdp_return_buff(xdp, true) 1233 1234 static inline void __libeth_xdp_return_buff(struct libeth_xdp_buff *xdp, 1235 bool napi) 1236 { 1237 if (!xdp_buff_has_frags(&xdp->base)) 1238 goto out; 1239 1240 libeth_xdp_return_frags(xdp_get_shared_info_from_buff(&xdp->base), 1241 napi); 1242 1243 out: 1244 libeth_xdp_return_va(xdp->data, napi); 1245 xdp->data = NULL; 1246 } 1247 1248 bool libeth_xdp_buff_add_frag(struct libeth_xdp_buff *xdp, 1249 const struct libeth_fqe *fqe, 1250 u32 len); 1251 1252 /** 1253 * libeth_xdp_prepare_buff - fill &libeth_xdp_buff with head FQE data 1254 * @xdp: XDP buffer to attach the head to 1255 * @fqe: FQE containing the head buffer 1256 * @len: buffer len passed from HW 1257 * 1258 * Internal, use libeth_xdp_process_buff() instead. Initializes XDP buffer 1259 * head with the Rx buffer data: data pointer, length, headroom, and 1260 * truesize/tailroom. Zeroes the flags. 1261 */ 1262 static inline void libeth_xdp_prepare_buff(struct libeth_xdp_buff *xdp, 1263 const struct libeth_fqe *fqe, 1264 u32 len) 1265 { 1266 const struct page *page = __netmem_to_page(fqe->netmem); 1267 1268 xdp_init_buff(&xdp->base, fqe->truesize, xdp->base.rxq); 1269 xdp_prepare_buff(&xdp->base, page_address(page) + fqe->offset, 1270 page->pp->p.offset, len, true); 1271 } 1272 1273 /** 1274 * libeth_xdp_process_buff - attach Rx buffer to &libeth_xdp_buff 1275 * @xdp: XDP buffer to attach the Rx buffer to 1276 * @fqe: Rx buffer to process 1277 * @len: received data length from the descriptor 1278 * 1279 * If the XDP buffer is empty, attaches the Rx buffer as head and initializes 1280 * the required fields. Otherwise, attaches the buffer as a frag. 1281 * Already performs DMA sync-for-CPU and frame start prefetch 1282 * (for head buffers only). 1283 * 1284 * Return: true on success, false if the descriptor must be skipped (empty or 1285 * no space for a new frag). 1286 */ 1287 static inline bool libeth_xdp_process_buff(struct libeth_xdp_buff *xdp, 1288 const struct libeth_fqe *fqe, 1289 u32 len) 1290 { 1291 if (!libeth_rx_sync_for_cpu(fqe, len)) 1292 return false; 1293 1294 if (xdp->data) 1295 return libeth_xdp_buff_add_frag(xdp, fqe, len); 1296 1297 libeth_xdp_prepare_buff(xdp, fqe, len); 1298 1299 prefetch(xdp->data); 1300 1301 return true; 1302 } 1303 1304 /** 1305 * libeth_xdp_buff_stats_frags - update onstack RQ stats with XDP frags info 1306 * @ss: onstack stats to update 1307 * @xdp: buffer to account 1308 * 1309 * Internal helper used by __libeth_xdp_run_pass(), do not call directly. 1310 * Adds buffer's frags count and total len to the onstack stats. 1311 */ 1312 static inline void 1313 libeth_xdp_buff_stats_frags(struct libeth_rq_napi_stats *ss, 1314 const struct libeth_xdp_buff *xdp) 1315 { 1316 const struct skb_shared_info *sinfo; 1317 1318 sinfo = xdp_get_shared_info_from_buff(&xdp->base); 1319 ss->bytes += sinfo->xdp_frags_size; 1320 ss->fragments += sinfo->nr_frags + 1; 1321 } 1322 1323 u32 libeth_xdp_prog_exception(const struct libeth_xdp_tx_bulk *bq, 1324 struct libeth_xdp_buff *xdp, 1325 enum xdp_action act, int ret); 1326 1327 /** 1328 * __libeth_xdp_run_prog - run XDP program on an XDP buffer 1329 * @xdp: XDP buffer to run the prog on 1330 * @bq: buffer bulk for ``XDP_TX`` queueing 1331 * 1332 * Internal inline abstraction to run XDP program. Handles ``XDP_DROP`` 1333 * and ``XDP_REDIRECT`` only, the rest is processed levels up. 1334 * Reports an XDP prog exception on errors. 1335 * 1336 * Return: libeth_xdp prog verdict depending on the prog's verdict. 1337 */ 1338 static __always_inline u32 1339 __libeth_xdp_run_prog(struct libeth_xdp_buff *xdp, 1340 const struct libeth_xdp_tx_bulk *bq) 1341 { 1342 enum xdp_action act; 1343 1344 act = bpf_prog_run_xdp(bq->prog, &xdp->base); 1345 if (unlikely(act < XDP_DROP || act > XDP_REDIRECT)) 1346 goto out; 1347 1348 switch (act) { 1349 case XDP_PASS: 1350 return LIBETH_XDP_PASS; 1351 case XDP_DROP: 1352 libeth_xdp_return_buff(xdp); 1353 1354 return LIBETH_XDP_DROP; 1355 case XDP_TX: 1356 return LIBETH_XDP_TX; 1357 case XDP_REDIRECT: 1358 if (unlikely(xdp_do_redirect(bq->dev, &xdp->base, bq->prog))) 1359 break; 1360 1361 xdp->data = NULL; 1362 1363 return LIBETH_XDP_REDIRECT; 1364 default: 1365 break; 1366 } 1367 1368 out: 1369 return libeth_xdp_prog_exception(bq, xdp, act, 0); 1370 } 1371 1372 /** 1373 * __libeth_xdp_run_flush - run XDP program and handle ``XDP_TX`` verdict 1374 * @xdp: XDP buffer to run the prog on 1375 * @bq: buffer bulk for ``XDP_TX`` queueing 1376 * @run: internal callback for running XDP program 1377 * @queue: internal callback for queuing ``XDP_TX`` frame 1378 * @flush_bulk: driver callback for flushing a bulk 1379 * 1380 * Internal inline abstraction to run XDP program and additionally handle 1381 * ``XDP_TX`` verdict. 1382 * Do not use directly. 1383 * 1384 * Return: libeth_xdp prog verdict depending on the prog's verdict. 1385 */ 1386 static __always_inline u32 1387 __libeth_xdp_run_flush(struct libeth_xdp_buff *xdp, 1388 struct libeth_xdp_tx_bulk *bq, 1389 u32 (*run)(struct libeth_xdp_buff *xdp, 1390 const struct libeth_xdp_tx_bulk *bq), 1391 bool (*queue)(struct libeth_xdp_tx_bulk *bq, 1392 struct libeth_xdp_buff *xdp, 1393 bool (*flush_bulk) 1394 (struct libeth_xdp_tx_bulk *bq, 1395 u32 flags)), 1396 bool (*flush_bulk)(struct libeth_xdp_tx_bulk *bq, 1397 u32 flags)) 1398 { 1399 u32 act; 1400 1401 act = run(xdp, bq); 1402 if (act == LIBETH_XDP_TX && unlikely(!queue(bq, xdp, flush_bulk))) 1403 act = LIBETH_XDP_DROP; 1404 1405 bq->act_mask |= act; 1406 1407 return act; 1408 } 1409 1410 /** 1411 * libeth_xdp_run_prog - run XDP program and handle all verdicts 1412 * @xdp: XDP buffer to process 1413 * @bq: XDP Tx bulk to queue ``XDP_TX`` buffers 1414 * @fl: driver ``XDP_TX`` bulk flush callback 1415 * 1416 * Run the attached XDP program and handle all possible verdicts. 1417 * Prefer using it via LIBETH_XDP_DEFINE_RUN{,_PASS,_PROG}(). 1418 * 1419 * Return: true if the buffer should be passed up the stack, false if the poll 1420 * should go to the next buffer. 1421 */ 1422 #define libeth_xdp_run_prog(xdp, bq, fl) \ 1423 (__libeth_xdp_run_flush(xdp, bq, __libeth_xdp_run_prog, \ 1424 libeth_xdp_tx_queue_bulk, \ 1425 fl) == LIBETH_XDP_PASS) 1426 1427 /** 1428 * __libeth_xdp_run_pass - helper to run XDP program and handle the result 1429 * @xdp: XDP buffer to process 1430 * @bq: XDP Tx bulk to queue ``XDP_TX`` frames 1431 * @napi: NAPI to build an skb and pass it up the stack 1432 * @rs: onstack libeth RQ stats 1433 * @md: metadata that should be filled to the XDP buffer 1434 * @prep: callback for filling the metadata 1435 * @run: driver wrapper to run XDP program 1436 * @populate: driver callback to populate an skb with the HW descriptor data 1437 * 1438 * Inline abstraction that does the following: 1439 * 1) adds frame size and frag number (if needed) to the onstack stats; 1440 * 2) fills the descriptor metadata to the onstack &libeth_xdp_buff 1441 * 3) runs XDP program if present; 1442 * 4) handles all possible verdicts; 1443 * 5) on ``XDP_PASS`, builds an skb from the buffer; 1444 * 6) populates it with the descriptor metadata; 1445 * 7) passes it up the stack. 1446 * 1447 * In most cases, number 2 means just writing the pointer to the HW descriptor 1448 * to the XDP buffer. If so, please use LIBETH_XDP_DEFINE_RUN{,_PASS}() 1449 * wrappers to build a driver function. 1450 */ 1451 static __always_inline void 1452 __libeth_xdp_run_pass(struct libeth_xdp_buff *xdp, 1453 struct libeth_xdp_tx_bulk *bq, struct napi_struct *napi, 1454 struct libeth_rq_napi_stats *rs, const void *md, 1455 void (*prep)(struct libeth_xdp_buff *xdp, 1456 const void *md), 1457 bool (*run)(struct libeth_xdp_buff *xdp, 1458 struct libeth_xdp_tx_bulk *bq), 1459 bool (*populate)(struct sk_buff *skb, 1460 const struct libeth_xdp_buff *xdp, 1461 struct libeth_rq_napi_stats *rs)) 1462 { 1463 struct sk_buff *skb; 1464 1465 rs->bytes += xdp->base.data_end - xdp->data; 1466 rs->packets++; 1467 1468 if (xdp_buff_has_frags(&xdp->base)) 1469 libeth_xdp_buff_stats_frags(rs, xdp); 1470 1471 if (prep && (!__builtin_constant_p(!!md) || md)) 1472 prep(xdp, md); 1473 1474 if (!bq || !run || !bq->prog) 1475 goto build; 1476 1477 if (!run(xdp, bq)) 1478 return; 1479 1480 build: 1481 skb = xdp_build_skb_from_buff(&xdp->base); 1482 if (unlikely(!skb)) { 1483 libeth_xdp_return_buff_slow(xdp); 1484 return; 1485 } 1486 1487 xdp->data = NULL; 1488 1489 if (unlikely(!populate(skb, xdp, rs))) { 1490 napi_consume_skb(skb, true); 1491 return; 1492 } 1493 1494 napi_gro_receive(napi, skb); 1495 } 1496 1497 static inline void libeth_xdp_prep_desc(struct libeth_xdp_buff *xdp, 1498 const void *desc) 1499 { 1500 xdp->desc = desc; 1501 } 1502 1503 /** 1504 * libeth_xdp_run_pass - helper to run XDP program and handle the result 1505 * @xdp: XDP buffer to process 1506 * @bq: XDP Tx bulk to queue ``XDP_TX`` frames 1507 * @napi: NAPI to build an skb and pass it up the stack 1508 * @ss: onstack libeth RQ stats 1509 * @desc: pointer to the HW descriptor for that frame 1510 * @run: driver wrapper to run XDP program 1511 * @populate: driver callback to populate an skb with the HW descriptor data 1512 * 1513 * Wrapper around the underscored version when "fill the descriptor metadata" 1514 * means just writing the pointer to the HW descriptor as @xdp->desc. 1515 */ 1516 #define libeth_xdp_run_pass(xdp, bq, napi, ss, desc, run, populate) \ 1517 __libeth_xdp_run_pass(xdp, bq, napi, ss, desc, libeth_xdp_prep_desc, \ 1518 run, populate) 1519 1520 /** 1521 * libeth_xdp_finalize_rx - finalize XDPSQ after a NAPI polling loop 1522 * @bq: ``XDP_TX`` frame bulk 1523 * @flush: driver callback to flush the bulk 1524 * @finalize: driver callback to start sending the frames and run the timer 1525 * 1526 * Flush the bulk if there are frames left to send, kick the queue and flush 1527 * the XDP maps. 1528 */ 1529 #define libeth_xdp_finalize_rx(bq, flush, finalize) \ 1530 __libeth_xdp_finalize_rx(bq, 0, flush, finalize) 1531 1532 static __always_inline void 1533 __libeth_xdp_finalize_rx(struct libeth_xdp_tx_bulk *bq, u32 flags, 1534 bool (*flush_bulk)(struct libeth_xdp_tx_bulk *bq, 1535 u32 flags), 1536 void (*finalize)(void *xdpsq, bool sent, bool flush)) 1537 { 1538 if (bq->act_mask & LIBETH_XDP_TX) { 1539 if (bq->count) 1540 flush_bulk(bq, flags | LIBETH_XDP_TX_DROP); 1541 finalize(bq->xdpsq, true, true); 1542 } 1543 if (bq->act_mask & LIBETH_XDP_REDIRECT) 1544 xdp_do_flush(); 1545 1546 rcu_read_unlock(); 1547 } 1548 1549 /* 1550 * Helpers to reduce boilerplate code in drivers. 1551 * 1552 * Typical driver Rx flow would be (excl. bulk and buff init, frag attach): 1553 * 1554 * LIBETH_XDP_DEFINE_START(); 1555 * LIBETH_XDP_DEFINE_FLUSH_TX(static driver_xdp_flush_tx, driver_xdp_tx_prep, 1556 * driver_xdp_xmit); 1557 * LIBETH_XDP_DEFINE_RUN(static driver_xdp_run, driver_xdp_run_prog, 1558 * driver_xdp_flush_tx, driver_populate_skb); 1559 * LIBETH_XDP_DEFINE_FINALIZE(static driver_xdp_finalize_rx, 1560 * driver_xdp_flush_tx, driver_xdp_finalize_sq); 1561 * LIBETH_XDP_DEFINE_END(); 1562 * 1563 * This will build a set of 4 static functions. The compiler is free to decide 1564 * whether to inline them. 1565 * Then, in the NAPI polling function: 1566 * 1567 * while (packets < budget) { 1568 * // ... 1569 * driver_xdp_run(xdp, &bq, napi, &rs, desc); 1570 * } 1571 * driver_xdp_finalize_rx(&bq); 1572 */ 1573 1574 #define LIBETH_XDP_DEFINE_START() \ 1575 __diag_push(); \ 1576 __diag_ignore(GCC, 8, "-Wold-style-declaration", \ 1577 "Allow specifying \'static\' after the return type") 1578 1579 /** 1580 * LIBETH_XDP_DEFINE_TIMER - define a driver XDPSQ cleanup timer callback 1581 * @name: name of the function to define 1582 * @poll: Tx polling/completion function 1583 */ 1584 #define LIBETH_XDP_DEFINE_TIMER(name, poll) \ 1585 void name(struct work_struct *work) \ 1586 { \ 1587 libeth_xdpsq_run_timer(work, poll); \ 1588 } 1589 1590 /** 1591 * LIBETH_XDP_DEFINE_FLUSH_TX - define a driver ``XDP_TX`` bulk flush function 1592 * @name: name of the function to define 1593 * @prep: driver callback to clean an XDPSQ 1594 * @xmit: driver callback to write a HW Tx descriptor 1595 */ 1596 #define LIBETH_XDP_DEFINE_FLUSH_TX(name, prep, xmit) \ 1597 __LIBETH_XDP_DEFINE_FLUSH_TX(name, prep, xmit, xdp) 1598 1599 #define __LIBETH_XDP_DEFINE_FLUSH_TX(name, prep, xmit, pfx) \ 1600 bool name(struct libeth_xdp_tx_bulk *bq, u32 flags) \ 1601 { \ 1602 return libeth_##pfx##_tx_flush_bulk(bq, flags, prep, xmit); \ 1603 } 1604 1605 /** 1606 * LIBETH_XDP_DEFINE_FLUSH_XMIT - define a driver XDP xmit bulk flush function 1607 * @name: name of the function to define 1608 * @prep: driver callback to clean an XDPSQ 1609 * @xmit: driver callback to write a HW Tx descriptor 1610 */ 1611 #define LIBETH_XDP_DEFINE_FLUSH_XMIT(name, prep, xmit) \ 1612 bool name(struct libeth_xdp_tx_bulk *bq, u32 flags) \ 1613 { \ 1614 return libeth_xdp_xmit_flush_bulk(bq, flags, prep, xmit); \ 1615 } 1616 1617 /** 1618 * LIBETH_XDP_DEFINE_RUN_PROG - define a driver XDP program run function 1619 * @name: name of the function to define 1620 * @flush: driver callback to flush an ``XDP_TX`` bulk 1621 */ 1622 #define LIBETH_XDP_DEFINE_RUN_PROG(name, flush) \ 1623 bool __LIBETH_XDP_DEFINE_RUN_PROG(name, flush, xdp) 1624 1625 #define __LIBETH_XDP_DEFINE_RUN_PROG(name, flush, pfx) \ 1626 name(struct libeth_xdp_buff *xdp, struct libeth_xdp_tx_bulk *bq) \ 1627 { \ 1628 return libeth_##pfx##_run_prog(xdp, bq, flush); \ 1629 } 1630 1631 /** 1632 * LIBETH_XDP_DEFINE_RUN_PASS - define a driver buffer process + pass function 1633 * @name: name of the function to define 1634 * @run: driver callback to run XDP program (above) 1635 * @populate: driver callback to fill an skb with HW descriptor info 1636 */ 1637 #define LIBETH_XDP_DEFINE_RUN_PASS(name, run, populate) \ 1638 void __LIBETH_XDP_DEFINE_RUN_PASS(name, run, populate, xdp) 1639 1640 #define __LIBETH_XDP_DEFINE_RUN_PASS(name, run, populate, pfx) \ 1641 name(struct libeth_xdp_buff *xdp, struct libeth_xdp_tx_bulk *bq, \ 1642 struct napi_struct *napi, struct libeth_rq_napi_stats *ss, \ 1643 const void *desc) \ 1644 { \ 1645 return libeth_##pfx##_run_pass(xdp, bq, napi, ss, desc, run, \ 1646 populate); \ 1647 } 1648 1649 /** 1650 * LIBETH_XDP_DEFINE_RUN - define a driver buffer process, run + pass function 1651 * @name: name of the function to define 1652 * @run: name of the XDP prog run function to define 1653 * @flush: driver callback to flush an ``XDP_TX`` bulk 1654 * @populate: driver callback to fill an skb with HW descriptor info 1655 */ 1656 #define LIBETH_XDP_DEFINE_RUN(name, run, flush, populate) \ 1657 __LIBETH_XDP_DEFINE_RUN(name, run, flush, populate, XDP) 1658 1659 #define __LIBETH_XDP_DEFINE_RUN(name, run, flush, populate, pfx) \ 1660 LIBETH_##pfx##_DEFINE_RUN_PROG(static run, flush); \ 1661 LIBETH_##pfx##_DEFINE_RUN_PASS(name, run, populate) 1662 1663 /** 1664 * LIBETH_XDP_DEFINE_FINALIZE - define a driver Rx NAPI poll finalize function 1665 * @name: name of the function to define 1666 * @flush: driver callback to flush an ``XDP_TX`` bulk 1667 * @finalize: driver callback to finalize an XDPSQ and run the timer 1668 */ 1669 #define LIBETH_XDP_DEFINE_FINALIZE(name, flush, finalize) \ 1670 __LIBETH_XDP_DEFINE_FINALIZE(name, flush, finalize, xdp) 1671 1672 #define __LIBETH_XDP_DEFINE_FINALIZE(name, flush, finalize, pfx) \ 1673 void name(struct libeth_xdp_tx_bulk *bq) \ 1674 { \ 1675 libeth_##pfx##_finalize_rx(bq, flush, finalize); \ 1676 } 1677 1678 #define LIBETH_XDP_DEFINE_END() __diag_pop() 1679 1680 /* XMO */ 1681 1682 /** 1683 * libeth_xdp_buff_to_rq - get RQ pointer from an XDP buffer pointer 1684 * @xdp: &libeth_xdp_buff corresponding to the queue 1685 * @type: typeof() of the driver Rx queue structure 1686 * @member: name of &xdp_rxq_info inside @type 1687 * 1688 * Often times, pointer to the RQ is needed when reading/filling metadata from 1689 * HW descriptors. The helper can be used to quickly jump from an XDP buffer 1690 * to the queue corresponding to its &xdp_rxq_info without introducing 1691 * additional fields (&libeth_xdp_buff is precisely 1 cacheline long on x64). 1692 */ 1693 #define libeth_xdp_buff_to_rq(xdp, type, member) \ 1694 container_of_const((xdp)->base.rxq, type, member) 1695 1696 /** 1697 * libeth_xdpmo_rx_hash - convert &libeth_rx_pt to an XDP RSS hash metadata 1698 * @hash: pointer to the variable to write the hash to 1699 * @rss_type: pointer to the variable to write the hash type to 1700 * @val: hash value from the HW descriptor 1701 * @pt: libeth parsed packet type 1702 * 1703 * Handle zeroed/non-available hash and convert libeth parsed packet type to 1704 * the corresponding XDP RSS hash type. To be called at the end of 1705 * xdp_metadata_ops idpf_xdpmo::xmo_rx_hash() implementation. 1706 * Note that if the driver doesn't use a constant packet type lookup table but 1707 * generates it at runtime, it must call libeth_rx_pt_gen_hash_type(pt) to 1708 * generate XDP RSS hash type for each packet type. 1709 * 1710 * Return: 0 on success, -ENODATA when the hash is not available. 1711 */ 1712 static inline int libeth_xdpmo_rx_hash(u32 *hash, 1713 enum xdp_rss_hash_type *rss_type, 1714 u32 val, struct libeth_rx_pt pt) 1715 { 1716 if (unlikely(!val)) 1717 return -ENODATA; 1718 1719 *hash = val; 1720 *rss_type = pt.hash_type; 1721 1722 return 0; 1723 } 1724 1725 /* Tx buffer completion */ 1726 1727 void libeth_xdp_return_buff_bulk(const struct skb_shared_info *sinfo, 1728 struct xdp_frame_bulk *bq, bool frags); 1729 void libeth_xsk_buff_free_slow(struct libeth_xdp_buff *xdp); 1730 1731 /** 1732 * __libeth_xdp_complete_tx - complete sent XDPSQE 1733 * @sqe: SQ element / Tx buffer to complete 1734 * @cp: Tx polling/completion params 1735 * @bulk: internal callback to bulk-free ``XDP_TX`` buffers 1736 * @xsk: internal callback to free XSk ``XDP_TX`` buffers 1737 * 1738 * Use the non-underscored version in drivers instead. This one is shared 1739 * internally with libeth_tx_complete_any(). 1740 * Complete an XDPSQE of any type of XDP frame. This includes DMA unmapping 1741 * when needed, buffer freeing, stats update, and SQE invalidation. 1742 */ 1743 static __always_inline void 1744 __libeth_xdp_complete_tx(struct libeth_sqe *sqe, struct libeth_cq_pp *cp, 1745 typeof(libeth_xdp_return_buff_bulk) bulk, 1746 typeof(libeth_xsk_buff_free_slow) xsk) 1747 { 1748 enum libeth_sqe_type type = sqe->type; 1749 1750 switch (type) { 1751 case LIBETH_SQE_EMPTY: 1752 return; 1753 case LIBETH_SQE_XDP_XMIT: 1754 case LIBETH_SQE_XDP_XMIT_FRAG: 1755 dma_unmap_page(cp->dev, dma_unmap_addr(sqe, dma), 1756 dma_unmap_len(sqe, len), DMA_TO_DEVICE); 1757 break; 1758 default: 1759 break; 1760 } 1761 1762 switch (type) { 1763 case LIBETH_SQE_XDP_TX: 1764 bulk(sqe->sinfo, cp->bq, sqe->nr_frags != 1); 1765 break; 1766 case LIBETH_SQE_XDP_XMIT: 1767 xdp_return_frame_bulk(sqe->xdpf, cp->bq); 1768 break; 1769 case LIBETH_SQE_XSK_TX: 1770 case LIBETH_SQE_XSK_TX_FRAG: 1771 xsk(sqe->xsk); 1772 break; 1773 default: 1774 break; 1775 } 1776 1777 switch (type) { 1778 case LIBETH_SQE_XDP_TX: 1779 case LIBETH_SQE_XDP_XMIT: 1780 case LIBETH_SQE_XSK_TX: 1781 cp->xdp_tx -= sqe->nr_frags; 1782 1783 cp->xss->packets++; 1784 cp->xss->bytes += sqe->bytes; 1785 break; 1786 default: 1787 break; 1788 } 1789 1790 sqe->type = LIBETH_SQE_EMPTY; 1791 } 1792 1793 static inline void libeth_xdp_complete_tx(struct libeth_sqe *sqe, 1794 struct libeth_cq_pp *cp) 1795 { 1796 __libeth_xdp_complete_tx(sqe, cp, libeth_xdp_return_buff_bulk, 1797 libeth_xsk_buff_free_slow); 1798 } 1799 1800 /* Misc */ 1801 1802 u32 libeth_xdp_queue_threshold(u32 count); 1803 1804 void __libeth_xdp_set_features(struct net_device *dev, 1805 const struct xdp_metadata_ops *xmo, 1806 u32 zc_segs, 1807 const struct xsk_tx_metadata_ops *tmo); 1808 void libeth_xdp_set_redirect(struct net_device *dev, bool enable); 1809 1810 /** 1811 * libeth_xdp_set_features - set XDP features for netdev 1812 * @dev: &net_device to configure 1813 * @...: optional params, see __libeth_xdp_set_features() 1814 * 1815 * Set all the features libeth_xdp supports, including .ndo_xdp_xmit(). That 1816 * said, it should be used only when XDPSQs are always available regardless 1817 * of whether an XDP prog is attached to @dev. 1818 */ 1819 #define libeth_xdp_set_features(dev, ...) \ 1820 CONCATENATE(__libeth_xdp_feat, \ 1821 COUNT_ARGS(__VA_ARGS__))(dev, ##__VA_ARGS__) 1822 1823 #define __libeth_xdp_feat0(dev) \ 1824 __libeth_xdp_set_features(dev, NULL, 0, NULL) 1825 #define __libeth_xdp_feat1(dev, xmo) \ 1826 __libeth_xdp_set_features(dev, xmo, 0, NULL) 1827 #define __libeth_xdp_feat2(dev, xmo, zc_segs) \ 1828 __libeth_xdp_set_features(dev, xmo, zc_segs, NULL) 1829 #define __libeth_xdp_feat3(dev, xmo, zc_segs, tmo) \ 1830 __libeth_xdp_set_features(dev, xmo, zc_segs, tmo) 1831 1832 /** 1833 * libeth_xdp_set_features_noredir - enable all libeth_xdp features w/o redir 1834 * @dev: target &net_device 1835 * @...: optional params, see __libeth_xdp_set_features() 1836 * 1837 * Enable everything except the .ndo_xdp_xmit() feature, use when XDPSQs are 1838 * not available right after netdev registration. 1839 */ 1840 #define libeth_xdp_set_features_noredir(dev, ...) \ 1841 __libeth_xdp_set_features_noredir(dev, __UNIQUE_ID(dev_), \ 1842 ##__VA_ARGS__) 1843 1844 #define __libeth_xdp_set_features_noredir(dev, ud, ...) do { \ 1845 struct net_device *ud = (dev); \ 1846 \ 1847 libeth_xdp_set_features(ud, ##__VA_ARGS__); \ 1848 libeth_xdp_set_redirect(ud, false); \ 1849 } while (0) 1850 1851 #define libeth_xsktmo ((const void *)GOLDEN_RATIO_PRIME) 1852 1853 #endif /* __LIBETH_XDP_H */ 1854