xdp.h 56 KB

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