1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* Copyright (C) 2025 Intel Corporation */ 3 4 #ifndef __LIBETH_XSK_H 5 #define __LIBETH_XSK_H 6 7 #include <net/libeth/xdp.h> 8 #include <net/xdp_sock_drv.h> 9 10 /* ``XDP_TXMD_FLAGS_VALID`` is defined only under ``CONFIG_XDP_SOCKETS`` */ 11 #ifdef XDP_TXMD_FLAGS_VALID 12 static_assert(XDP_TXMD_FLAGS_VALID <= LIBETH_XDP_TX_XSKMD); 13 #endif 14 15 /* ``XDP_TX`` bulking */ 16 17 /** 18 * libeth_xsk_tx_queue_head - internal helper for queueing XSk ``XDP_TX`` head 19 * @bq: XDP Tx bulk to queue the head frag to 20 * @xdp: XSk buffer with the head to queue 21 * 22 * Return: false if it's the only frag of the frame, true if it's an S/G frame. 23 */ 24 static inline bool libeth_xsk_tx_queue_head(struct libeth_xdp_tx_bulk *bq, 25 struct libeth_xdp_buff *xdp) 26 { 27 bq->bulk[bq->count++] = (typeof(*bq->bulk)){ 28 .xsk = xdp, 29 __libeth_xdp_tx_len(xdp->base.data_end - xdp->data, 30 LIBETH_XDP_TX_FIRST), 31 }; 32 33 if (likely(!xdp_buff_has_frags(&xdp->base))) 34 return false; 35 36 bq->bulk[bq->count - 1].flags |= LIBETH_XDP_TX_MULTI; 37 38 return true; 39 } 40 41 /** 42 * libeth_xsk_tx_queue_frag - internal helper for queueing XSk ``XDP_TX`` frag 43 * @bq: XDP Tx bulk to queue the frag to 44 * @frag: XSk frag to queue 45 */ 46 static inline void libeth_xsk_tx_queue_frag(struct libeth_xdp_tx_bulk *bq, 47 struct libeth_xdp_buff *frag) 48 { 49 bq->bulk[bq->count++] = (typeof(*bq->bulk)){ 50 .xsk = frag, 51 __libeth_xdp_tx_len(frag->base.data_end - frag->data), 52 }; 53 } 54 55 /** 56 * libeth_xsk_tx_queue_bulk - internal helper for queueing XSk ``XDP_TX`` frame 57 * @bq: XDP Tx bulk to queue the frame to 58 * @xdp: XSk buffer to queue 59 * @flush_bulk: driver callback to flush the bulk to the HW queue 60 * 61 * Return: true on success, false on flush error. 62 */ 63 static __always_inline bool 64 libeth_xsk_tx_queue_bulk(struct libeth_xdp_tx_bulk *bq, 65 struct libeth_xdp_buff *xdp, 66 bool (*flush_bulk)(struct libeth_xdp_tx_bulk *bq, 67 u32 flags)) 68 { 69 bool ret = true; 70 71 if (unlikely(bq->count == LIBETH_XDP_TX_BULK) && 72 unlikely(!flush_bulk(bq, LIBETH_XDP_TX_XSK))) { 73 libeth_xsk_buff_free_slow(xdp); 74 return false; 75 } 76 77 if (!libeth_xsk_tx_queue_head(bq, xdp)) 78 goto out; 79 80 for (struct libeth_xdp_buff *head = xdp; ; ) { 81 xdp = container_of(xsk_buff_get_frag(&head->base), 82 typeof(*xdp), base); 83 if (!xdp) { 84 /* Each frag is now freed via its own SQE. Freeing 85 * the head must not release what is on the pool's 86 * frag list at that time. 87 */ 88 xdp_buff_clear_frags_flag(&head->base); 89 break; 90 } 91 92 if (unlikely(bq->count == LIBETH_XDP_TX_BULK) && 93 unlikely(!flush_bulk(bq, LIBETH_XDP_TX_XSK))) { 94 ret = false; 95 break; 96 } 97 98 libeth_xsk_tx_queue_frag(bq, xdp); 99 } 100 101 out: 102 bq->bulk[bq->count - 1].flags |= LIBETH_XDP_TX_LAST; 103 104 return ret; 105 } 106 107 /** 108 * libeth_xsk_tx_fill_buf - internal helper to fill XSk ``XDP_TX`` &libeth_sqe 109 * @frm: XDP Tx frame from the bulk 110 * @i: index on the HW queue 111 * @sq: XDPSQ abstraction for the queue 112 * @priv: private data 113 * 114 * Return: XDP Tx descriptor with the synced DMA and other info to pass to 115 * the driver callback. 116 */ 117 static inline struct libeth_xdp_tx_desc 118 libeth_xsk_tx_fill_buf(struct libeth_xdp_tx_frame frm, u32 i, 119 const struct libeth_xdpsq *sq, u64 priv) 120 { 121 struct libeth_xdp_buff *xdp = frm.xsk; 122 struct libeth_xdp_tx_desc desc = { 123 .addr = xsk_buff_xdp_get_dma(&xdp->base), 124 .opts = frm.opts, 125 }; 126 struct libeth_sqe *sqe; 127 128 xsk_buff_raw_dma_sync_for_device(sq->pool, desc.addr, desc.len); 129 130 sqe = &sq->sqes[i]; 131 sqe->xsk = xdp; 132 133 if (!(desc.flags & LIBETH_XDP_TX_FIRST)) { 134 sqe->type = LIBETH_SQE_XSK_TX_FRAG; 135 return desc; 136 } 137 138 sqe->type = LIBETH_SQE_XSK_TX; 139 libeth_xdp_tx_fill_stats(sqe, &desc, 140 xdp_get_shared_info_from_buff(&xdp->base)); 141 142 return desc; 143 } 144 145 /** 146 * libeth_xsk_tx_flush_bulk - wrapper to define flush of XSk ``XDP_TX`` bulk 147 * @bq: bulk to flush 148 * @flags: Tx flags, see __libeth_xdp_tx_flush_bulk() 149 * @prep: driver callback to prepare the queue 150 * @xmit: driver callback to fill a HW descriptor 151 * 152 * Use via LIBETH_XSK_DEFINE_FLUSH_TX() to define an XSk ``XDP_TX`` driver 153 * callback. 154 */ 155 #define libeth_xsk_tx_flush_bulk(bq, flags, prep, xmit) \ 156 __libeth_xdp_tx_flush_bulk(bq, (flags) | LIBETH_XDP_TX_XSK, prep, \ 157 libeth_xsk_tx_fill_buf, xmit) 158 159 /* XSk TMO */ 160 161 /** 162 * libeth_xsktmo_req_csum - XSk Tx metadata op to request checksum offload 163 * @csum_start: unused 164 * @csum_offset: unused 165 * @priv: &libeth_xdp_tx_desc from the filling helper 166 * 167 * Generic implementation of ::tmo_request_checksum. Works only when HW doesn't 168 * require filling checksum offsets and other parameters beside the checksum 169 * request bit. 170 * Consider using within @libeth_xsktmo unless the driver requires HW-specific 171 * callbacks. 172 */ 173 static inline void libeth_xsktmo_req_csum(u16 csum_start, u16 csum_offset, 174 void *priv) 175 { 176 ((struct libeth_xdp_tx_desc *)priv)->flags |= LIBETH_XDP_TX_CSUM; 177 } 178 179 /* Only to inline the callbacks below, use @libeth_xsktmo in drivers instead */ 180 static const struct xsk_tx_metadata_ops __libeth_xsktmo = { 181 .tmo_request_checksum = libeth_xsktmo_req_csum, 182 }; 183 184 /** 185 * __libeth_xsk_xmit_fill_buf_md - internal helper to prepare XSk xmit w/meta 186 * @xdesc: &xdp_desc from the XSk buffer pool 187 * @sq: XDPSQ abstraction for the queue 188 * @priv: XSk Tx metadata ops 189 * 190 * Same as __libeth_xsk_xmit_fill_buf(), but requests metadata pointer and 191 * fills additional fields in &libeth_xdp_tx_desc to ask for metadata offload. 192 * 193 * Return: XDP Tx descriptor with the DMA, metadata request bits, and other 194 * info to pass to the driver callback. 195 */ 196 static __always_inline struct libeth_xdp_tx_desc 197 __libeth_xsk_xmit_fill_buf_md(const struct xdp_desc *xdesc, 198 const struct libeth_xdpsq *sq, 199 u64 priv) 200 { 201 const struct xsk_tx_metadata_ops *tmo = libeth_xdp_priv_to_ptr(priv); 202 struct libeth_xdp_tx_desc desc; 203 struct xdp_desc_ctx ctx; 204 205 ctx = xsk_buff_raw_get_ctx(sq->pool, xdesc->addr, xdesc->options); 206 desc = (typeof(desc)){ 207 .addr = ctx.dma, 208 __libeth_xdp_tx_len(xdesc->len), 209 }; 210 211 BUILD_BUG_ON(!__builtin_constant_p(tmo == libeth_xsktmo)); 212 tmo = tmo == libeth_xsktmo ? &__libeth_xsktmo : tmo; 213 214 xsk_tx_metadata_request(sq->pool, &ctx.meta, tmo, &desc); 215 216 return desc; 217 } 218 219 /* XSk xmit implementation */ 220 221 /** 222 * __libeth_xsk_xmit_fill_buf - internal helper to prepare XSk xmit w/o meta 223 * @xdesc: &xdp_desc from the XSk buffer pool 224 * @sq: XDPSQ abstraction for the queue 225 * 226 * Return: XDP Tx descriptor with the DMA and other info to pass to 227 * the driver callback. 228 */ 229 static inline struct libeth_xdp_tx_desc 230 __libeth_xsk_xmit_fill_buf(const struct xdp_desc *xdesc, 231 const struct libeth_xdpsq *sq) 232 { 233 return (struct libeth_xdp_tx_desc){ 234 .addr = xsk_buff_raw_get_dma(sq->pool, xdesc->addr), 235 __libeth_xdp_tx_len(xdesc->len), 236 }; 237 } 238 239 /** 240 * libeth_xsk_xmit_fill_buf - internal helper to prepare an XSk xmit 241 * @frm: &xdp_desc from the XSk buffer pool 242 * @i: index on the HW queue 243 * @sq: XDPSQ abstraction for the queue 244 * @priv: XSk Tx metadata ops 245 * 246 * Depending on the metadata ops presence (determined at compile time), calls 247 * the quickest helper to build a libeth XDP Tx descriptor. 248 * 249 * Return: XDP Tx descriptor with the synced DMA, metadata request bits, 250 * and other info to pass to the driver callback. 251 */ 252 static __always_inline struct libeth_xdp_tx_desc 253 libeth_xsk_xmit_fill_buf(struct libeth_xdp_tx_frame frm, u32 i, 254 const struct libeth_xdpsq *sq, u64 priv) 255 { 256 struct libeth_xdp_tx_desc desc; 257 258 if (priv) 259 desc = __libeth_xsk_xmit_fill_buf_md(&frm.desc, sq, priv); 260 else 261 desc = __libeth_xsk_xmit_fill_buf(&frm.desc, sq); 262 263 desc.flags |= xsk_is_eop_desc(&frm.desc) ? LIBETH_XDP_TX_LAST : 0; 264 265 xsk_buff_raw_dma_sync_for_device(sq->pool, desc.addr, desc.len); 266 267 return desc; 268 } 269 270 /** 271 * libeth_xsk_xmit_do_bulk - send XSk xmit frames 272 * @pool: XSk buffer pool containing the frames to send 273 * @xdpsq: opaque pointer to driver's XDPSQ struct 274 * @budget: maximum number of frames can be sent 275 * @tmo: optional XSk Tx metadata ops 276 * @prep: driver callback to build a &libeth_xdpsq 277 * @xmit: driver callback to put frames to a HW queue 278 * @finalize: driver callback to start a transmission 279 * 280 * Implements generic XSk xmit. Always turns on XSk Tx wakeup as it's assumed 281 * lazy cleaning is used and interrupts are disabled for the queue. 282 * HW descriptor filling is unrolled by ``LIBETH_XDP_TX_BATCH`` to optimize 283 * writes. 284 * Note that unlike other XDP Tx ops, the queue must be locked and cleaned 285 * prior to calling this function to already know available @budget. 286 * @prepare must only build a &libeth_xdpsq and return ``U32_MAX``. 287 * 288 * Return: false if @budget was exhausted, true otherwise. 289 */ 290 static __always_inline bool 291 libeth_xsk_xmit_do_bulk(struct xsk_buff_pool *pool, void *xdpsq, u32 budget, 292 const struct xsk_tx_metadata_ops *tmo, 293 u32 (*prep)(void *xdpsq, struct libeth_xdpsq *sq), 294 void (*xmit)(struct libeth_xdp_tx_desc desc, u32 i, 295 const struct libeth_xdpsq *sq, u64 priv), 296 void (*finalize)(void *xdpsq, bool sent, bool flush)) 297 { 298 const struct libeth_xdp_tx_frame *bulk; 299 bool wake; 300 u32 n; 301 302 wake = xsk_uses_need_wakeup(pool); 303 if (wake) 304 xsk_clear_tx_need_wakeup(pool); 305 306 n = xsk_tx_peek_release_desc_batch(pool, budget); 307 bulk = container_of(&pool->tx_descs[0], typeof(*bulk), desc); 308 309 libeth_xdp_tx_xmit_bulk(bulk, xdpsq, n, true, 310 libeth_xdp_ptr_to_priv(tmo), prep, 311 libeth_xsk_xmit_fill_buf, xmit); 312 finalize(xdpsq, n, true); 313 314 if (wake) 315 xsk_set_tx_need_wakeup(pool); 316 317 return n < budget; 318 } 319 320 /* Rx polling path */ 321 322 /** 323 * libeth_xsk_tx_init_bulk - initialize XDP Tx bulk for an XSk Rx NAPI poll 324 * @bq: bulk to initialize 325 * @prog: RCU pointer to the XDP program (never %NULL) 326 * @dev: target &net_device 327 * @xdpsqs: array of driver XDPSQ structs 328 * @num: number of active XDPSQs, the above array length 329 * 330 * Should be called on an onstack XDP Tx bulk before the XSk NAPI polling loop. 331 * Initializes all the needed fields to run libeth_xdp functions. 332 * Never checks if @prog is %NULL or @num == 0 as XDP must always be enabled 333 * when hitting this path. 334 */ 335 #define libeth_xsk_tx_init_bulk(bq, prog, dev, xdpsqs, num) \ 336 __libeth_xdp_tx_init_bulk(bq, prog, dev, xdpsqs, num, true, \ 337 __UNIQUE_ID(bq_), __UNIQUE_ID(nqs_)) 338 339 struct libeth_xdp_buff *libeth_xsk_buff_add_frag(struct libeth_xdp_buff *head, 340 struct libeth_xdp_buff *xdp); 341 342 /** 343 * libeth_xsk_process_buff - attach XSk Rx buffer to &libeth_xdp_buff 344 * @head: head XSk buffer to attach the XSk buffer to (or %NULL) 345 * @xdp: XSk buffer to process 346 * @len: received data length from the descriptor 347 * 348 * If @head == %NULL, treats the XSk buffer as head and initializes 349 * the required fields. Otherwise, attaches the buffer as a frag. 350 * Already performs DMA sync-for-CPU and frame start prefetch 351 * (for head buffers only). 352 * 353 * Return: head XSk buffer on success or if the descriptor must be skipped 354 * (empty), %NULL if there is no space for a new frag. 355 */ 356 static inline struct libeth_xdp_buff * 357 libeth_xsk_process_buff(struct libeth_xdp_buff *head, 358 struct libeth_xdp_buff *xdp, u32 len) 359 { 360 if (unlikely(!len)) { 361 libeth_xsk_buff_free_slow(xdp); 362 return head; 363 } 364 365 xsk_buff_set_size(&xdp->base, len); 366 xsk_buff_dma_sync_for_cpu(&xdp->base); 367 368 if (head) 369 return libeth_xsk_buff_add_frag(head, xdp); 370 371 prefetch(xdp->data); 372 373 return xdp; 374 } 375 376 void libeth_xsk_buff_stats_frags(struct libeth_rq_napi_stats *rs, 377 const struct libeth_xdp_buff *xdp); 378 379 u32 __libeth_xsk_run_prog_slow(struct libeth_xdp_buff *xdp, 380 const struct libeth_xdp_tx_bulk *bq, 381 enum xdp_action act, int ret); 382 383 /** 384 * __libeth_xsk_run_prog - run XDP program on XSk buffer 385 * @xdp: XSk buffer to run the prog on 386 * @bq: buffer bulk for ``XDP_TX`` queueing 387 * 388 * Internal inline abstraction to run XDP program on XSk Rx path. Handles 389 * only the most common ``XDP_REDIRECT`` inline, the rest is processed 390 * externally. 391 * Reports an XDP prog exception on errors. 392 * 393 * Return: libeth_xdp prog verdict depending on the prog's verdict. 394 */ 395 static __always_inline u32 396 __libeth_xsk_run_prog(struct libeth_xdp_buff *xdp, 397 const struct libeth_xdp_tx_bulk *bq) 398 { 399 enum xdp_action act; 400 int ret = 0; 401 402 act = bpf_prog_run_xdp(bq->prog, &xdp->base); 403 if (unlikely(act != XDP_REDIRECT)) 404 rest: 405 return __libeth_xsk_run_prog_slow(xdp, bq, act, ret); 406 407 ret = xdp_do_redirect(bq->dev, &xdp->base, bq->prog); 408 if (unlikely(ret)) 409 goto rest; 410 411 return LIBETH_XDP_REDIRECT; 412 } 413 414 /** 415 * libeth_xsk_run_prog - run XDP program on XSk path and handle all verdicts 416 * @xdp: XSk buffer to process 417 * @bq: XDP Tx bulk to queue ``XDP_TX`` buffers 418 * @fl: driver ``XDP_TX`` bulk flush callback 419 * 420 * Run the attached XDP program and handle all possible verdicts. 421 * Prefer using it via LIBETH_XSK_DEFINE_RUN{,_PASS,_PROG}(). 422 * 423 * Return: libeth_xdp prog verdict depending on the prog's verdict. 424 */ 425 #define libeth_xsk_run_prog(xdp, bq, fl) \ 426 __libeth_xdp_run_flush(xdp, bq, __libeth_xsk_run_prog, \ 427 libeth_xsk_tx_queue_bulk, fl) 428 429 /** 430 * __libeth_xsk_run_pass - helper to run XDP program and handle the result 431 * @xdp: XSk buffer to process 432 * @bq: XDP Tx bulk to queue ``XDP_TX`` frames 433 * @napi: NAPI to build an skb and pass it up the stack 434 * @rs: onstack libeth RQ stats 435 * @md: metadata that should be filled to the XSk buffer 436 * @prep: callback for filling the metadata 437 * @run: driver wrapper to run XDP program 438 * @populate: driver callback to populate an skb with the HW descriptor data 439 * 440 * Inline abstraction, XSk's counterpart of __libeth_xdp_run_pass(), see its 441 * doc for details. 442 * 443 * Return: false if the polling loop must be exited due to lack of free 444 * buffers, true otherwise. 445 */ 446 static __always_inline bool 447 __libeth_xsk_run_pass(struct libeth_xdp_buff *xdp, 448 struct libeth_xdp_tx_bulk *bq, struct napi_struct *napi, 449 struct libeth_rq_napi_stats *rs, const void *md, 450 void (*prep)(struct libeth_xdp_buff *xdp, 451 const void *md), 452 u32 (*run)(struct libeth_xdp_buff *xdp, 453 struct libeth_xdp_tx_bulk *bq), 454 bool (*populate)(struct sk_buff *skb, 455 const struct libeth_xdp_buff *xdp, 456 struct libeth_rq_napi_stats *rs)) 457 { 458 struct sk_buff *skb; 459 u32 act; 460 461 rs->bytes += xdp->base.data_end - xdp->data; 462 rs->packets++; 463 464 if (unlikely(xdp_buff_has_frags(&xdp->base))) 465 libeth_xsk_buff_stats_frags(rs, xdp); 466 467 if (prep && (!__builtin_constant_p(!!md) || md)) 468 prep(xdp, md); 469 470 act = run(xdp, bq); 471 if (likely(act == LIBETH_XDP_REDIRECT)) 472 return true; 473 474 if (act != LIBETH_XDP_PASS) 475 return act != LIBETH_XDP_ABORTED; 476 477 skb = xdp_build_skb_from_zc(&xdp->base); 478 if (unlikely(!skb)) { 479 libeth_xsk_buff_free_slow(xdp); 480 return true; 481 } 482 483 if (unlikely(!populate(skb, xdp, rs))) { 484 napi_consume_skb(skb, true); 485 return true; 486 } 487 488 napi_gro_receive(napi, skb); 489 490 return true; 491 } 492 493 /** 494 * libeth_xsk_run_pass - helper to run XDP program and handle the result 495 * @xdp: XSk buffer to process 496 * @bq: XDP Tx bulk to queue ``XDP_TX`` frames 497 * @napi: NAPI to build an skb and pass it up the stack 498 * @rs: onstack libeth RQ stats 499 * @desc: pointer to the HW descriptor for that frame 500 * @run: driver wrapper to run XDP program 501 * @populate: driver callback to populate an skb with the HW descriptor data 502 * 503 * Wrapper around the underscored version when "fill the descriptor metadata" 504 * means just writing the pointer to the HW descriptor as @xdp->desc. 505 */ 506 #define libeth_xsk_run_pass(xdp, bq, napi, rs, desc, run, populate) \ 507 __libeth_xsk_run_pass(xdp, bq, napi, rs, desc, libeth_xdp_prep_desc, \ 508 run, populate) 509 510 /** 511 * libeth_xsk_finalize_rx - finalize XDPSQ after an XSk NAPI polling loop 512 * @bq: ``XDP_TX`` frame bulk 513 * @flush: driver callback to flush the bulk 514 * @finalize: driver callback to start sending the frames and run the timer 515 * 516 * Flush the bulk if there are frames left to send, kick the queue and flush 517 * the XDP maps. 518 */ 519 #define libeth_xsk_finalize_rx(bq, flush, finalize) \ 520 __libeth_xdp_finalize_rx(bq, LIBETH_XDP_TX_XSK, flush, finalize) 521 522 /* 523 * Helpers to reduce boilerplate code in drivers. 524 * 525 * Typical driver XSk Rx flow would be (excl. bulk and buff init, frag attach): 526 * 527 * LIBETH_XDP_DEFINE_START(); 528 * LIBETH_XSK_DEFINE_FLUSH_TX(static driver_xsk_flush_tx, driver_xsk_tx_prep, 529 * driver_xdp_xmit); 530 * LIBETH_XSK_DEFINE_RUN(static driver_xsk_run, driver_xsk_run_prog, 531 * driver_xsk_flush_tx, driver_populate_skb); 532 * LIBETH_XSK_DEFINE_FINALIZE(static driver_xsk_finalize_rx, 533 * driver_xsk_flush_tx, driver_xdp_finalize_sq); 534 * LIBETH_XDP_DEFINE_END(); 535 * 536 * This will build a set of 4 static functions. The compiler is free to decide 537 * whether to inline them. 538 * Then, in the NAPI polling function: 539 * 540 * while (packets < budget) { 541 * // ... 542 * if (!driver_xsk_run(xdp, &bq, napi, &rs, desc)) 543 * break; 544 * } 545 * driver_xsk_finalize_rx(&bq); 546 */ 547 548 /** 549 * LIBETH_XSK_DEFINE_FLUSH_TX - define a driver XSk ``XDP_TX`` flush function 550 * @name: name of the function to define 551 * @prep: driver callback to clean an XDPSQ 552 * @xmit: driver callback to write a HW Tx descriptor 553 */ 554 #define LIBETH_XSK_DEFINE_FLUSH_TX(name, prep, xmit) \ 555 __LIBETH_XDP_DEFINE_FLUSH_TX(name, prep, xmit, xsk) 556 557 /** 558 * LIBETH_XSK_DEFINE_RUN_PROG - define a driver XDP program run function 559 * @name: name of the function to define 560 * @flush: driver callback to flush an XSk ``XDP_TX`` bulk 561 */ 562 #define LIBETH_XSK_DEFINE_RUN_PROG(name, flush) \ 563 u32 __LIBETH_XDP_DEFINE_RUN_PROG(name, flush, xsk) 564 565 /** 566 * LIBETH_XSK_DEFINE_RUN_PASS - define a driver buffer process + pass function 567 * @name: name of the function to define 568 * @run: driver callback to run XDP program (above) 569 * @populate: driver callback to fill an skb with HW descriptor info 570 */ 571 #define LIBETH_XSK_DEFINE_RUN_PASS(name, run, populate) \ 572 bool __LIBETH_XDP_DEFINE_RUN_PASS(name, run, populate, xsk) 573 574 /** 575 * LIBETH_XSK_DEFINE_RUN - define a driver buffer process, run + pass function 576 * @name: name of the function to define 577 * @run: name of the XDP prog run function to define 578 * @flush: driver callback to flush an XSk ``XDP_TX`` bulk 579 * @populate: driver callback to fill an skb with HW descriptor info 580 */ 581 #define LIBETH_XSK_DEFINE_RUN(name, run, flush, populate) \ 582 __LIBETH_XDP_DEFINE_RUN(name, run, flush, populate, XSK) 583 584 /** 585 * LIBETH_XSK_DEFINE_FINALIZE - define a driver XSk NAPI poll finalize function 586 * @name: name of the function to define 587 * @flush: driver callback to flush an XSk ``XDP_TX`` bulk 588 * @finalize: driver callback to finalize an XDPSQ and run the timer 589 */ 590 #define LIBETH_XSK_DEFINE_FINALIZE(name, flush, finalize) \ 591 __LIBETH_XDP_DEFINE_FINALIZE(name, flush, finalize, xsk) 592 593 /* Refilling */ 594 595 /** 596 * struct libeth_xskfq - structure representing an XSk buffer (fill) queue 597 * @fp: hotpath part of the structure 598 * @pool: &xsk_buff_pool for buffer management 599 * @fqes: array of XSk buffer pointers 600 * @descs: opaque pointer to the HW descriptor array 601 * @ntu: index of the next buffer to poll 602 * @count: number of descriptors/buffers the queue has 603 * @pending: current number of XSkFQEs to refill 604 * @thresh: threshold below which the queue is refilled 605 * @buf_len: HW-writeable length per each buffer 606 * @truesize: step between consecutive buffers, 0 if none exists 607 * @nid: ID of the closest NUMA node with memory 608 */ 609 struct libeth_xskfq { 610 struct_group_tagged(libeth_xskfq_fp, fp, 611 struct xsk_buff_pool *pool; 612 struct libeth_xdp_buff **fqes; 613 void *descs; 614 615 u32 ntu; 616 u32 count; 617 ); 618 619 /* Cold fields */ 620 u32 pending; 621 u32 thresh; 622 623 u32 buf_len; 624 u32 truesize; 625 626 int nid; 627 }; 628 629 int libeth_xskfq_create(struct libeth_xskfq *fq); 630 void libeth_xskfq_destroy(struct libeth_xskfq *fq); 631 632 /** 633 * libeth_xsk_buff_xdp_get_dma - get DMA address of XSk &libeth_xdp_buff 634 * @xdp: buffer to get the DMA addr for 635 */ 636 #define libeth_xsk_buff_xdp_get_dma(xdp) \ 637 xsk_buff_xdp_get_dma(&(xdp)->base) 638 639 /** 640 * libeth_xskfqe_alloc - allocate @n XSk Rx buffers 641 * @fq: hotpath part of the XSkFQ, usually onstack 642 * @n: number of buffers to allocate 643 * @fill: driver callback to write DMA addresses to HW descriptors 644 * 645 * Note that @fq->ntu gets updated, but ::pending must be recalculated 646 * by the caller. 647 * 648 * Return: number of buffers refilled. 649 */ 650 static __always_inline u32 651 libeth_xskfqe_alloc(struct libeth_xskfq_fp *fq, u32 n, 652 void (*fill)(const struct libeth_xskfq_fp *fq, u32 i)) 653 { 654 u32 this, ret, done = 0; 655 struct xdp_buff **xskb; 656 657 this = fq->count - fq->ntu; 658 if (likely(this > n)) 659 this = n; 660 661 again: 662 xskb = (typeof(xskb))&fq->fqes[fq->ntu]; 663 ret = xsk_buff_alloc_batch(fq->pool, xskb, this); 664 665 for (u32 i = 0, ntu = fq->ntu; likely(i < ret); i++) 666 fill(fq, ntu + i); 667 668 done += ret; 669 fq->ntu += ret; 670 671 if (likely(fq->ntu < fq->count) || unlikely(ret < this)) 672 goto out; 673 674 fq->ntu = 0; 675 676 if (this < n) { 677 this = n - this; 678 goto again; 679 } 680 681 out: 682 return done; 683 } 684 685 /* .ndo_xsk_wakeup */ 686 687 void libeth_xsk_init_wakeup(call_single_data_t *csd, struct napi_struct *napi); 688 void libeth_xsk_wakeup(call_single_data_t *csd, u32 qid); 689 690 /* Pool setup */ 691 692 int libeth_xsk_setup_pool(struct net_device *dev, u32 qid, bool enable); 693 694 #endif /* __LIBETH_XSK_H */ 695