xdp_sock_drv.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Interface for implementing AF_XDP zero-copy support in drivers.
  3. * Copyright(c) 2020 Intel Corporation.
  4. */
  5. #ifndef _LINUX_XDP_SOCK_DRV_H
  6. #define _LINUX_XDP_SOCK_DRV_H
  7. #include <net/xdp_sock.h>
  8. #include <net/xsk_buff_pool.h>
  9. #define XDP_UMEM_MIN_CHUNK_SHIFT 11
  10. #define XDP_UMEM_MIN_CHUNK_SIZE (1 << XDP_UMEM_MIN_CHUNK_SHIFT)
  11. #define NETDEV_XDP_ACT_XSK (NETDEV_XDP_ACT_BASIC | \
  12. NETDEV_XDP_ACT_REDIRECT | \
  13. NETDEV_XDP_ACT_XSK_ZEROCOPY)
  14. struct xsk_cb_desc {
  15. void *src;
  16. u8 off;
  17. u8 bytes;
  18. };
  19. #ifdef CONFIG_XDP_SOCKETS
  20. void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries);
  21. bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc);
  22. u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max);
  23. void xsk_tx_release(struct xsk_buff_pool *pool);
  24. struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
  25. u16 queue_id);
  26. void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool);
  27. void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool);
  28. void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool);
  29. void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool);
  30. bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool);
  31. static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
  32. {
  33. return XDP_PACKET_HEADROOM + pool->headroom;
  34. }
  35. static inline u32 xsk_pool_get_tailroom(bool mbuf)
  36. {
  37. return mbuf ? SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) : 0;
  38. }
  39. static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
  40. {
  41. return pool->chunk_size;
  42. }
  43. static inline u32 __xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
  44. {
  45. return xsk_pool_get_chunk_size(pool) - xsk_pool_get_headroom(pool);
  46. }
  47. static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
  48. {
  49. u32 frame_size = __xsk_pool_get_rx_frame_size(pool);
  50. struct xdp_umem *umem = pool->umem;
  51. bool mbuf;
  52. /* Reserve tailroom only for zero-copy pools that opted into
  53. * multi-buffer. The reserved area is used for skb_shared_info,
  54. * matching the XDP core's xdp_data_hard_end() layout.
  55. */
  56. mbuf = pool->dev && (umem->flags & XDP_UMEM_SG_FLAG);
  57. frame_size -= xsk_pool_get_tailroom(mbuf);
  58. return ALIGN_DOWN(frame_size, 128);
  59. }
  60. static inline u32 xsk_pool_get_rx_frag_step(struct xsk_buff_pool *pool)
  61. {
  62. return pool->unaligned ? 0 : xsk_pool_get_chunk_size(pool);
  63. }
  64. static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
  65. struct xdp_rxq_info *rxq)
  66. {
  67. xp_set_rxq_info(pool, rxq);
  68. }
  69. static inline void xsk_pool_fill_cb(struct xsk_buff_pool *pool,
  70. struct xsk_cb_desc *desc)
  71. {
  72. xp_fill_cb(pool, desc);
  73. }
  74. static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
  75. unsigned long attrs)
  76. {
  77. xp_dma_unmap(pool, attrs);
  78. }
  79. static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
  80. struct device *dev, unsigned long attrs)
  81. {
  82. struct xdp_umem *umem = pool->umem;
  83. return xp_dma_map(pool, dev, attrs, umem->pgs, umem->npgs);
  84. }
  85. static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
  86. {
  87. struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
  88. return xp_get_dma(xskb);
  89. }
  90. static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
  91. {
  92. struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
  93. return xp_get_frame_dma(xskb);
  94. }
  95. static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
  96. {
  97. return xp_alloc(pool);
  98. }
  99. static inline bool xsk_is_eop_desc(const struct xdp_desc *desc)
  100. {
  101. return !xp_mb_desc(desc);
  102. }
  103. /* Returns as many entries as possible up to max. 0 <= N <= max. */
  104. static inline u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
  105. {
  106. return xp_alloc_batch(pool, xdp, max);
  107. }
  108. static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
  109. {
  110. return xp_can_alloc(pool, count);
  111. }
  112. static inline void xsk_buff_free(struct xdp_buff *xdp)
  113. {
  114. struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
  115. struct list_head *xskb_list = &xskb->pool->xskb_list;
  116. struct xdp_buff_xsk *pos, *tmp;
  117. if (likely(!xdp_buff_has_frags(xdp)))
  118. goto out;
  119. list_for_each_entry_safe(pos, tmp, xskb_list, list_node) {
  120. list_del_init(&pos->list_node);
  121. xp_free(pos);
  122. }
  123. xdp_get_shared_info_from_buff(xdp)->nr_frags = 0;
  124. out:
  125. xp_free(xskb);
  126. }
  127. static inline bool xsk_buff_add_frag(struct xdp_buff *head,
  128. struct xdp_buff *xdp)
  129. {
  130. const void *data = xdp->data;
  131. struct xdp_buff_xsk *frag;
  132. if (!__xdp_buff_add_frag(head, virt_to_netmem(data),
  133. offset_in_page(data), xdp->data_end - data,
  134. xdp->frame_sz, false))
  135. return false;
  136. frag = container_of(xdp, struct xdp_buff_xsk, xdp);
  137. list_add_tail(&frag->list_node, &frag->pool->xskb_list);
  138. return true;
  139. }
  140. static inline struct xdp_buff *xsk_buff_get_frag(const struct xdp_buff *first)
  141. {
  142. struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp);
  143. struct xdp_buff *ret = NULL;
  144. struct xdp_buff_xsk *frag;
  145. frag = list_first_entry_or_null(&xskb->pool->xskb_list,
  146. struct xdp_buff_xsk, list_node);
  147. if (frag) {
  148. list_del_init(&frag->list_node);
  149. ret = &frag->xdp;
  150. }
  151. return ret;
  152. }
  153. static inline void xsk_buff_del_frag(struct xdp_buff *xdp)
  154. {
  155. struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
  156. list_del_init(&xskb->list_node);
  157. }
  158. static inline struct xdp_buff *xsk_buff_get_head(struct xdp_buff *first)
  159. {
  160. struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp);
  161. struct xdp_buff_xsk *frag;
  162. frag = list_first_entry(&xskb->pool->xskb_list, struct xdp_buff_xsk,
  163. list_node);
  164. return &frag->xdp;
  165. }
  166. static inline struct xdp_buff *xsk_buff_get_tail(struct xdp_buff *first)
  167. {
  168. struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp);
  169. struct xdp_buff_xsk *frag;
  170. frag = list_last_entry(&xskb->pool->xskb_list, struct xdp_buff_xsk,
  171. list_node);
  172. return &frag->xdp;
  173. }
  174. static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size)
  175. {
  176. xdp->data = xdp->data_hard_start + XDP_PACKET_HEADROOM;
  177. xdp->data_meta = xdp->data;
  178. xdp->data_end = xdp->data + size;
  179. xdp->flags = 0;
  180. }
  181. static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
  182. u64 addr)
  183. {
  184. return xp_raw_get_dma(pool, addr);
  185. }
  186. static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
  187. {
  188. return xp_raw_get_data(pool, addr);
  189. }
  190. /**
  191. * xsk_buff_raw_get_ctx - get &xdp_desc context
  192. * @pool: XSk buff pool desc address belongs to
  193. * @addr: desc address (from userspace)
  194. *
  195. * Wrapper for xp_raw_get_ctx() to be used in drivers, see its kdoc for
  196. * details.
  197. *
  198. * Return: new &xdp_desc_ctx struct containing desc's DMA address and metadata
  199. * pointer, if it is present and valid (initialized to %NULL otherwise).
  200. */
  201. static inline struct xdp_desc_ctx
  202. xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr)
  203. {
  204. return xp_raw_get_ctx(pool, addr);
  205. }
  206. #define XDP_TXMD_FLAGS_VALID ( \
  207. XDP_TXMD_FLAGS_TIMESTAMP | \
  208. XDP_TXMD_FLAGS_CHECKSUM | \
  209. XDP_TXMD_FLAGS_LAUNCH_TIME | \
  210. 0)
  211. static inline bool
  212. xsk_buff_valid_tx_metadata(const struct xsk_tx_metadata *meta)
  213. {
  214. return !(meta->flags & ~XDP_TXMD_FLAGS_VALID);
  215. }
  216. static inline struct xsk_tx_metadata *
  217. __xsk_buff_get_metadata(const struct xsk_buff_pool *pool, void *data)
  218. {
  219. struct xsk_tx_metadata *meta;
  220. if (!pool->tx_metadata_len)
  221. return NULL;
  222. meta = data - pool->tx_metadata_len;
  223. if (unlikely(!xsk_buff_valid_tx_metadata(meta)))
  224. return NULL; /* no way to signal the error to the user */
  225. return meta;
  226. }
  227. static inline struct xsk_tx_metadata *
  228. xsk_buff_get_metadata(struct xsk_buff_pool *pool, u64 addr)
  229. {
  230. return __xsk_buff_get_metadata(pool, xp_raw_get_data(pool, addr));
  231. }
  232. static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp)
  233. {
  234. struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
  235. xp_dma_sync_for_cpu(xskb);
  236. }
  237. static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
  238. dma_addr_t dma,
  239. size_t size)
  240. {
  241. xp_dma_sync_for_device(pool, dma, size);
  242. }
  243. #else
  244. static inline void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
  245. {
  246. }
  247. static inline bool xsk_tx_peek_desc(struct xsk_buff_pool *pool,
  248. struct xdp_desc *desc)
  249. {
  250. return false;
  251. }
  252. static inline u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max)
  253. {
  254. return 0;
  255. }
  256. static inline void xsk_tx_release(struct xsk_buff_pool *pool)
  257. {
  258. }
  259. static inline struct xsk_buff_pool *
  260. xsk_get_pool_from_qid(struct net_device *dev, u16 queue_id)
  261. {
  262. return NULL;
  263. }
  264. static inline void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
  265. {
  266. }
  267. static inline void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool)
  268. {
  269. }
  270. static inline void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool)
  271. {
  272. }
  273. static inline void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool)
  274. {
  275. }
  276. static inline bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool)
  277. {
  278. return false;
  279. }
  280. static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
  281. {
  282. return 0;
  283. }
  284. static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
  285. {
  286. return 0;
  287. }
  288. static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
  289. {
  290. return 0;
  291. }
  292. static inline u32 xsk_pool_get_rx_frag_step(struct xsk_buff_pool *pool)
  293. {
  294. return 0;
  295. }
  296. static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
  297. struct xdp_rxq_info *rxq)
  298. {
  299. }
  300. static inline void xsk_pool_fill_cb(struct xsk_buff_pool *pool,
  301. struct xsk_cb_desc *desc)
  302. {
  303. }
  304. static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
  305. unsigned long attrs)
  306. {
  307. }
  308. static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
  309. struct device *dev, unsigned long attrs)
  310. {
  311. return 0;
  312. }
  313. static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
  314. {
  315. return 0;
  316. }
  317. static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
  318. {
  319. return 0;
  320. }
  321. static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
  322. {
  323. return NULL;
  324. }
  325. static inline bool xsk_is_eop_desc(const struct xdp_desc *desc)
  326. {
  327. return false;
  328. }
  329. static inline u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
  330. {
  331. return 0;
  332. }
  333. static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
  334. {
  335. return false;
  336. }
  337. static inline void xsk_buff_free(struct xdp_buff *xdp)
  338. {
  339. }
  340. static inline bool xsk_buff_add_frag(struct xdp_buff *head,
  341. struct xdp_buff *xdp)
  342. {
  343. return false;
  344. }
  345. static inline struct xdp_buff *xsk_buff_get_frag(const struct xdp_buff *first)
  346. {
  347. return NULL;
  348. }
  349. static inline void xsk_buff_del_frag(struct xdp_buff *xdp)
  350. {
  351. }
  352. static inline struct xdp_buff *xsk_buff_get_head(struct xdp_buff *first)
  353. {
  354. return NULL;
  355. }
  356. static inline struct xdp_buff *xsk_buff_get_tail(struct xdp_buff *first)
  357. {
  358. return NULL;
  359. }
  360. static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size)
  361. {
  362. }
  363. static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
  364. u64 addr)
  365. {
  366. return 0;
  367. }
  368. static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
  369. {
  370. return NULL;
  371. }
  372. static inline struct xdp_desc_ctx
  373. xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr)
  374. {
  375. return (struct xdp_desc_ctx){ };
  376. }
  377. static inline bool xsk_buff_valid_tx_metadata(struct xsk_tx_metadata *meta)
  378. {
  379. return false;
  380. }
  381. static inline struct xsk_tx_metadata *
  382. __xsk_buff_get_metadata(const struct xsk_buff_pool *pool, void *data)
  383. {
  384. return NULL;
  385. }
  386. static inline struct xsk_tx_metadata *
  387. xsk_buff_get_metadata(struct xsk_buff_pool *pool, u64 addr)
  388. {
  389. return NULL;
  390. }
  391. static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp)
  392. {
  393. }
  394. static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
  395. dma_addr_t dma,
  396. size_t size)
  397. {
  398. }
  399. #endif /* CONFIG_XDP_SOCKETS */
  400. #endif /* _LINUX_XDP_SOCK_DRV_H */