xdp_sock_drv.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. struct xsk_cb_desc {
  12. void *src;
  13. u8 off;
  14. u8 bytes;
  15. };
  16. #ifdef CONFIG_XDP_SOCKETS
  17. void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries);
  18. bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc);
  19. u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max);
  20. void xsk_tx_release(struct xsk_buff_pool *pool);
  21. struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
  22. u16 queue_id);
  23. void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool);
  24. void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool);
  25. void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool);
  26. void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool);
  27. bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool);
  28. static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
  29. {
  30. return XDP_PACKET_HEADROOM + pool->headroom;
  31. }
  32. static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
  33. {
  34. return pool->chunk_size;
  35. }
  36. static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
  37. {
  38. return xsk_pool_get_chunk_size(pool) - xsk_pool_get_headroom(pool);
  39. }
  40. static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
  41. struct xdp_rxq_info *rxq)
  42. {
  43. xp_set_rxq_info(pool, rxq);
  44. }
  45. static inline void xsk_pool_fill_cb(struct xsk_buff_pool *pool,
  46. struct xsk_cb_desc *desc)
  47. {
  48. xp_fill_cb(pool, desc);
  49. }
  50. static inline unsigned int xsk_pool_get_napi_id(struct xsk_buff_pool *pool)
  51. {
  52. #ifdef CONFIG_NET_RX_BUSY_POLL
  53. return pool->heads[0].xdp.rxq->napi_id;
  54. #else
  55. return 0;
  56. #endif
  57. }
  58. static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
  59. unsigned long attrs)
  60. {
  61. xp_dma_unmap(pool, attrs);
  62. }
  63. static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
  64. struct device *dev, unsigned long attrs)
  65. {
  66. struct xdp_umem *umem = pool->umem;
  67. return xp_dma_map(pool, dev, attrs, umem->pgs, umem->npgs);
  68. }
  69. static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
  70. {
  71. struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
  72. return xp_get_dma(xskb);
  73. }
  74. static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
  75. {
  76. struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
  77. return xp_get_frame_dma(xskb);
  78. }
  79. static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
  80. {
  81. return xp_alloc(pool);
  82. }
  83. static inline bool xsk_is_eop_desc(struct xdp_desc *desc)
  84. {
  85. return !xp_mb_desc(desc);
  86. }
  87. /* Returns as many entries as possible up to max. 0 <= N <= max. */
  88. static inline u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
  89. {
  90. return xp_alloc_batch(pool, xdp, max);
  91. }
  92. static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
  93. {
  94. return xp_can_alloc(pool, count);
  95. }
  96. static inline void xsk_buff_free(struct xdp_buff *xdp)
  97. {
  98. struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
  99. struct list_head *xskb_list = &xskb->pool->xskb_list;
  100. struct xdp_buff_xsk *pos, *tmp;
  101. if (likely(!xdp_buff_has_frags(xdp)))
  102. goto out;
  103. list_for_each_entry_safe(pos, tmp, xskb_list, xskb_list_node) {
  104. list_del(&pos->xskb_list_node);
  105. xp_free(pos);
  106. }
  107. xdp_get_shared_info_from_buff(xdp)->nr_frags = 0;
  108. out:
  109. xp_free(xskb);
  110. }
  111. static inline void xsk_buff_add_frag(struct xdp_buff *xdp)
  112. {
  113. struct xdp_buff_xsk *frag = container_of(xdp, struct xdp_buff_xsk, xdp);
  114. list_add_tail(&frag->xskb_list_node, &frag->pool->xskb_list);
  115. }
  116. static inline struct xdp_buff *xsk_buff_get_frag(struct xdp_buff *first)
  117. {
  118. struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp);
  119. struct xdp_buff *ret = NULL;
  120. struct xdp_buff_xsk *frag;
  121. frag = list_first_entry_or_null(&xskb->pool->xskb_list,
  122. struct xdp_buff_xsk, xskb_list_node);
  123. if (frag) {
  124. list_del(&frag->xskb_list_node);
  125. ret = &frag->xdp;
  126. }
  127. return ret;
  128. }
  129. static inline void xsk_buff_del_tail(struct xdp_buff *tail)
  130. {
  131. struct xdp_buff_xsk *xskb = container_of(tail, struct xdp_buff_xsk, xdp);
  132. list_del(&xskb->xskb_list_node);
  133. }
  134. static inline struct xdp_buff *xsk_buff_get_tail(struct xdp_buff *first)
  135. {
  136. struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp);
  137. struct xdp_buff_xsk *frag;
  138. frag = list_last_entry(&xskb->pool->xskb_list, struct xdp_buff_xsk,
  139. xskb_list_node);
  140. return &frag->xdp;
  141. }
  142. static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size)
  143. {
  144. xdp->data = xdp->data_hard_start + XDP_PACKET_HEADROOM;
  145. xdp->data_meta = xdp->data;
  146. xdp->data_end = xdp->data + size;
  147. xdp->flags = 0;
  148. }
  149. static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
  150. u64 addr)
  151. {
  152. return xp_raw_get_dma(pool, addr);
  153. }
  154. static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
  155. {
  156. return xp_raw_get_data(pool, addr);
  157. }
  158. #define XDP_TXMD_FLAGS_VALID ( \
  159. XDP_TXMD_FLAGS_TIMESTAMP | \
  160. XDP_TXMD_FLAGS_CHECKSUM | \
  161. 0)
  162. static inline bool xsk_buff_valid_tx_metadata(struct xsk_tx_metadata *meta)
  163. {
  164. return !(meta->flags & ~XDP_TXMD_FLAGS_VALID);
  165. }
  166. static inline struct xsk_tx_metadata *xsk_buff_get_metadata(struct xsk_buff_pool *pool, u64 addr)
  167. {
  168. struct xsk_tx_metadata *meta;
  169. if (!pool->tx_metadata_len)
  170. return NULL;
  171. meta = xp_raw_get_data(pool, addr) - pool->tx_metadata_len;
  172. if (unlikely(!xsk_buff_valid_tx_metadata(meta)))
  173. return NULL; /* no way to signal the error to the user */
  174. return meta;
  175. }
  176. static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp)
  177. {
  178. struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
  179. xp_dma_sync_for_cpu(xskb);
  180. }
  181. static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
  182. dma_addr_t dma,
  183. size_t size)
  184. {
  185. xp_dma_sync_for_device(pool, dma, size);
  186. }
  187. #else
  188. static inline void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
  189. {
  190. }
  191. static inline bool xsk_tx_peek_desc(struct xsk_buff_pool *pool,
  192. struct xdp_desc *desc)
  193. {
  194. return false;
  195. }
  196. static inline u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max)
  197. {
  198. return 0;
  199. }
  200. static inline void xsk_tx_release(struct xsk_buff_pool *pool)
  201. {
  202. }
  203. static inline struct xsk_buff_pool *
  204. xsk_get_pool_from_qid(struct net_device *dev, u16 queue_id)
  205. {
  206. return NULL;
  207. }
  208. static inline void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
  209. {
  210. }
  211. static inline void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool)
  212. {
  213. }
  214. static inline void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool)
  215. {
  216. }
  217. static inline void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool)
  218. {
  219. }
  220. static inline bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool)
  221. {
  222. return false;
  223. }
  224. static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
  225. {
  226. return 0;
  227. }
  228. static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
  229. {
  230. return 0;
  231. }
  232. static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
  233. {
  234. return 0;
  235. }
  236. static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
  237. struct xdp_rxq_info *rxq)
  238. {
  239. }
  240. static inline void xsk_pool_fill_cb(struct xsk_buff_pool *pool,
  241. struct xsk_cb_desc *desc)
  242. {
  243. }
  244. static inline unsigned int xsk_pool_get_napi_id(struct xsk_buff_pool *pool)
  245. {
  246. return 0;
  247. }
  248. static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
  249. unsigned long attrs)
  250. {
  251. }
  252. static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
  253. struct device *dev, unsigned long attrs)
  254. {
  255. return 0;
  256. }
  257. static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
  258. {
  259. return 0;
  260. }
  261. static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
  262. {
  263. return 0;
  264. }
  265. static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
  266. {
  267. return NULL;
  268. }
  269. static inline bool xsk_is_eop_desc(struct xdp_desc *desc)
  270. {
  271. return false;
  272. }
  273. static inline u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
  274. {
  275. return 0;
  276. }
  277. static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
  278. {
  279. return false;
  280. }
  281. static inline void xsk_buff_free(struct xdp_buff *xdp)
  282. {
  283. }
  284. static inline void xsk_buff_add_frag(struct xdp_buff *xdp)
  285. {
  286. }
  287. static inline struct xdp_buff *xsk_buff_get_frag(struct xdp_buff *first)
  288. {
  289. return NULL;
  290. }
  291. static inline void xsk_buff_del_tail(struct xdp_buff *tail)
  292. {
  293. }
  294. static inline struct xdp_buff *xsk_buff_get_tail(struct xdp_buff *first)
  295. {
  296. return NULL;
  297. }
  298. static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size)
  299. {
  300. }
  301. static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
  302. u64 addr)
  303. {
  304. return 0;
  305. }
  306. static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
  307. {
  308. return NULL;
  309. }
  310. static inline bool xsk_buff_valid_tx_metadata(struct xsk_tx_metadata *meta)
  311. {
  312. return false;
  313. }
  314. static inline struct xsk_tx_metadata *xsk_buff_get_metadata(struct xsk_buff_pool *pool, u64 addr)
  315. {
  316. return NULL;
  317. }
  318. static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp)
  319. {
  320. }
  321. static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
  322. dma_addr_t dma,
  323. size_t size)
  324. {
  325. }
  326. #endif /* CONFIG_XDP_SOCKETS */
  327. #endif /* _LINUX_XDP_SOCK_DRV_H */