xsk.h 21 KB

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