xdp.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /* include/net/xdp.h
  3. *
  4. * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
  5. */
  6. #ifndef __LINUX_NET_XDP_H__
  7. #define __LINUX_NET_XDP_H__
  8. #include <linux/skbuff.h> /* skb_shared_info */
  9. /**
  10. * DOC: XDP RX-queue information
  11. *
  12. * The XDP RX-queue info (xdp_rxq_info) is associated with the driver
  13. * level RX-ring queues. It is information that is specific to how
  14. * the driver have configured a given RX-ring queue.
  15. *
  16. * Each xdp_buff frame received in the driver carries a (pointer)
  17. * reference to this xdp_rxq_info structure. This provides the XDP
  18. * data-path read-access to RX-info for both kernel and bpf-side
  19. * (limited subset).
  20. *
  21. * For now, direct access is only safe while running in NAPI/softirq
  22. * context. Contents are read-mostly and must not be updated during
  23. * driver NAPI/softirq poll.
  24. *
  25. * The driver usage API is a register and unregister API.
  26. *
  27. * The struct is not directly tied to the XDP prog. A new XDP prog
  28. * can be attached as long as it doesn't change the underlying
  29. * RX-ring. If the RX-ring does change significantly, the NIC driver
  30. * naturally need to stop the RX-ring before purging and reallocating
  31. * memory. In that process the driver MUST call unregister (which
  32. * also applies for driver shutdown and unload). The register API is
  33. * also mandatory during RX-ring setup.
  34. */
  35. enum xdp_mem_type {
  36. MEM_TYPE_PAGE_SHARED = 0, /* Split-page refcnt based model */
  37. MEM_TYPE_PAGE_ORDER0, /* Orig XDP full page model */
  38. MEM_TYPE_PAGE_POOL,
  39. MEM_TYPE_XSK_BUFF_POOL,
  40. MEM_TYPE_MAX,
  41. };
  42. /* XDP flags for ndo_xdp_xmit */
  43. #define XDP_XMIT_FLUSH (1U << 0) /* doorbell signal consumer */
  44. #define XDP_XMIT_FLAGS_MASK XDP_XMIT_FLUSH
  45. struct xdp_mem_info {
  46. u32 type; /* enum xdp_mem_type, but known size type */
  47. u32 id;
  48. };
  49. struct page_pool;
  50. struct xdp_rxq_info {
  51. struct net_device *dev;
  52. u32 queue_index;
  53. u32 reg_state;
  54. struct xdp_mem_info mem;
  55. unsigned int napi_id;
  56. u32 frag_size;
  57. } ____cacheline_aligned; /* perf critical, avoid false-sharing */
  58. struct xdp_txq_info {
  59. struct net_device *dev;
  60. };
  61. enum xdp_buff_flags {
  62. XDP_FLAGS_HAS_FRAGS = BIT(0), /* non-linear xdp buff */
  63. XDP_FLAGS_FRAGS_PF_MEMALLOC = BIT(1), /* xdp paged memory is under
  64. * pressure
  65. */
  66. };
  67. struct xdp_buff {
  68. void *data;
  69. void *data_end;
  70. void *data_meta;
  71. void *data_hard_start;
  72. struct xdp_rxq_info *rxq;
  73. struct xdp_txq_info *txq;
  74. u32 frame_sz; /* frame size to deduce data_hard_end/reserved tailroom*/
  75. u32 flags; /* supported values defined in xdp_buff_flags */
  76. };
  77. static __always_inline bool xdp_buff_has_frags(struct xdp_buff *xdp)
  78. {
  79. return !!(xdp->flags & XDP_FLAGS_HAS_FRAGS);
  80. }
  81. static __always_inline void xdp_buff_set_frags_flag(struct xdp_buff *xdp)
  82. {
  83. xdp->flags |= XDP_FLAGS_HAS_FRAGS;
  84. }
  85. static __always_inline void xdp_buff_clear_frags_flag(struct xdp_buff *xdp)
  86. {
  87. xdp->flags &= ~XDP_FLAGS_HAS_FRAGS;
  88. }
  89. static __always_inline bool xdp_buff_is_frag_pfmemalloc(struct xdp_buff *xdp)
  90. {
  91. return !!(xdp->flags & XDP_FLAGS_FRAGS_PF_MEMALLOC);
  92. }
  93. static __always_inline void xdp_buff_set_frag_pfmemalloc(struct xdp_buff *xdp)
  94. {
  95. xdp->flags |= XDP_FLAGS_FRAGS_PF_MEMALLOC;
  96. }
  97. static __always_inline void
  98. xdp_init_buff(struct xdp_buff *xdp, u32 frame_sz, struct xdp_rxq_info *rxq)
  99. {
  100. xdp->frame_sz = frame_sz;
  101. xdp->rxq = rxq;
  102. xdp->flags = 0;
  103. }
  104. static __always_inline void
  105. xdp_prepare_buff(struct xdp_buff *xdp, unsigned char *hard_start,
  106. int headroom, int data_len, const bool meta_valid)
  107. {
  108. unsigned char *data = hard_start + headroom;
  109. xdp->data_hard_start = hard_start;
  110. xdp->data = data;
  111. xdp->data_end = data + data_len;
  112. xdp->data_meta = meta_valid ? data : data + 1;
  113. }
  114. /* Reserve memory area at end-of data area.
  115. *
  116. * This macro reserves tailroom in the XDP buffer by limiting the
  117. * XDP/BPF data access to data_hard_end. Notice same area (and size)
  118. * is used for XDP_PASS, when constructing the SKB via build_skb().
  119. */
  120. #define xdp_data_hard_end(xdp) \
  121. ((xdp)->data_hard_start + (xdp)->frame_sz - \
  122. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
  123. static inline struct skb_shared_info *
  124. xdp_get_shared_info_from_buff(struct xdp_buff *xdp)
  125. {
  126. return (struct skb_shared_info *)xdp_data_hard_end(xdp);
  127. }
  128. static __always_inline unsigned int xdp_get_buff_len(struct xdp_buff *xdp)
  129. {
  130. unsigned int len = xdp->data_end - xdp->data;
  131. struct skb_shared_info *sinfo;
  132. if (likely(!xdp_buff_has_frags(xdp)))
  133. goto out;
  134. sinfo = xdp_get_shared_info_from_buff(xdp);
  135. len += sinfo->xdp_frags_size;
  136. out:
  137. return len;
  138. }
  139. struct xdp_frame {
  140. void *data;
  141. u16 len;
  142. u16 headroom;
  143. u32 metasize; /* uses lower 8-bits */
  144. /* Lifetime of xdp_rxq_info is limited to NAPI/enqueue time,
  145. * while mem info is valid on remote CPU.
  146. */
  147. struct xdp_mem_info mem;
  148. struct net_device *dev_rx; /* used by cpumap */
  149. u32 frame_sz;
  150. u32 flags; /* supported values defined in xdp_buff_flags */
  151. };
  152. static __always_inline bool xdp_frame_has_frags(struct xdp_frame *frame)
  153. {
  154. return !!(frame->flags & XDP_FLAGS_HAS_FRAGS);
  155. }
  156. static __always_inline bool xdp_frame_is_frag_pfmemalloc(struct xdp_frame *frame)
  157. {
  158. return !!(frame->flags & XDP_FLAGS_FRAGS_PF_MEMALLOC);
  159. }
  160. #define XDP_BULK_QUEUE_SIZE 16
  161. struct xdp_frame_bulk {
  162. int count;
  163. void *xa;
  164. void *q[XDP_BULK_QUEUE_SIZE];
  165. };
  166. static __always_inline void xdp_frame_bulk_init(struct xdp_frame_bulk *bq)
  167. {
  168. /* bq->count will be zero'ed when bq->xa gets updated */
  169. bq->xa = NULL;
  170. }
  171. static inline struct skb_shared_info *
  172. xdp_get_shared_info_from_frame(struct xdp_frame *frame)
  173. {
  174. void *data_hard_start = frame->data - frame->headroom - sizeof(*frame);
  175. return (struct skb_shared_info *)(data_hard_start + frame->frame_sz -
  176. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
  177. }
  178. struct xdp_cpumap_stats {
  179. unsigned int redirect;
  180. unsigned int pass;
  181. unsigned int drop;
  182. };
  183. /* Clear kernel pointers in xdp_frame */
  184. static inline void xdp_scrub_frame(struct xdp_frame *frame)
  185. {
  186. frame->data = NULL;
  187. frame->dev_rx = NULL;
  188. }
  189. static inline void
  190. xdp_update_skb_shared_info(struct sk_buff *skb, u8 nr_frags,
  191. unsigned int size, unsigned int truesize,
  192. bool pfmemalloc)
  193. {
  194. skb_shinfo(skb)->nr_frags = nr_frags;
  195. skb->len += size;
  196. skb->data_len += size;
  197. skb->truesize += truesize;
  198. skb->pfmemalloc |= pfmemalloc;
  199. }
  200. /* Avoids inlining WARN macro in fast-path */
  201. void xdp_warn(const char *msg, const char *func, const int line);
  202. #define XDP_WARN(msg) xdp_warn(msg, __func__, __LINE__)
  203. struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp);
  204. struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
  205. struct sk_buff *skb,
  206. struct net_device *dev);
  207. struct sk_buff *xdp_build_skb_from_frame(struct xdp_frame *xdpf,
  208. struct net_device *dev);
  209. int xdp_alloc_skb_bulk(void **skbs, int n_skb, gfp_t gfp);
  210. struct xdp_frame *xdpf_clone(struct xdp_frame *xdpf);
  211. static inline
  212. void xdp_convert_frame_to_buff(struct xdp_frame *frame, struct xdp_buff *xdp)
  213. {
  214. xdp->data_hard_start = frame->data - frame->headroom - sizeof(*frame);
  215. xdp->data = frame->data;
  216. xdp->data_end = frame->data + frame->len;
  217. xdp->data_meta = frame->data - frame->metasize;
  218. xdp->frame_sz = frame->frame_sz;
  219. xdp->flags = frame->flags;
  220. }
  221. static inline
  222. int xdp_update_frame_from_buff(struct xdp_buff *xdp,
  223. struct xdp_frame *xdp_frame)
  224. {
  225. int metasize, headroom;
  226. /* Assure headroom is available for storing info */
  227. headroom = xdp->data - xdp->data_hard_start;
  228. metasize = xdp->data - xdp->data_meta;
  229. metasize = metasize > 0 ? metasize : 0;
  230. if (unlikely((headroom - metasize) < sizeof(*xdp_frame)))
  231. return -ENOSPC;
  232. /* Catch if driver didn't reserve tailroom for skb_shared_info */
  233. if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) {
  234. XDP_WARN("Driver BUG: missing reserved tailroom");
  235. return -ENOSPC;
  236. }
  237. xdp_frame->data = xdp->data;
  238. xdp_frame->len = xdp->data_end - xdp->data;
  239. xdp_frame->headroom = headroom - sizeof(*xdp_frame);
  240. xdp_frame->metasize = metasize;
  241. xdp_frame->frame_sz = xdp->frame_sz;
  242. xdp_frame->flags = xdp->flags;
  243. return 0;
  244. }
  245. /* Convert xdp_buff to xdp_frame */
  246. static inline
  247. struct xdp_frame *xdp_convert_buff_to_frame(struct xdp_buff *xdp)
  248. {
  249. struct xdp_frame *xdp_frame;
  250. if (xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL)
  251. return xdp_convert_zc_to_xdp_frame(xdp);
  252. /* Store info in top of packet */
  253. xdp_frame = xdp->data_hard_start;
  254. if (unlikely(xdp_update_frame_from_buff(xdp, xdp_frame) < 0))
  255. return NULL;
  256. /* rxq only valid until napi_schedule ends, convert to xdp_mem_info */
  257. xdp_frame->mem = xdp->rxq->mem;
  258. return xdp_frame;
  259. }
  260. void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
  261. struct xdp_buff *xdp);
  262. void xdp_return_frame(struct xdp_frame *xdpf);
  263. void xdp_return_frame_rx_napi(struct xdp_frame *xdpf);
  264. void xdp_return_buff(struct xdp_buff *xdp);
  265. void xdp_flush_frame_bulk(struct xdp_frame_bulk *bq);
  266. void xdp_return_frame_bulk(struct xdp_frame *xdpf,
  267. struct xdp_frame_bulk *bq);
  268. /* When sending xdp_frame into the network stack, then there is no
  269. * return point callback, which is needed to release e.g. DMA-mapping
  270. * resources with page_pool. Thus, have explicit function to release
  271. * frame resources.
  272. */
  273. void __xdp_release_frame(void *data, struct xdp_mem_info *mem);
  274. static inline void xdp_release_frame(struct xdp_frame *xdpf)
  275. {
  276. struct xdp_mem_info *mem = &xdpf->mem;
  277. struct skb_shared_info *sinfo;
  278. int i;
  279. /* Curr only page_pool needs this */
  280. if (mem->type != MEM_TYPE_PAGE_POOL)
  281. return;
  282. if (likely(!xdp_frame_has_frags(xdpf)))
  283. goto out;
  284. sinfo = xdp_get_shared_info_from_frame(xdpf);
  285. for (i = 0; i < sinfo->nr_frags; i++) {
  286. struct page *page = skb_frag_page(&sinfo->frags[i]);
  287. __xdp_release_frame(page_address(page), mem);
  288. }
  289. out:
  290. __xdp_release_frame(xdpf->data, mem);
  291. }
  292. static __always_inline unsigned int xdp_get_frame_len(struct xdp_frame *xdpf)
  293. {
  294. struct skb_shared_info *sinfo;
  295. unsigned int len = xdpf->len;
  296. if (likely(!xdp_frame_has_frags(xdpf)))
  297. goto out;
  298. sinfo = xdp_get_shared_info_from_frame(xdpf);
  299. len += sinfo->xdp_frags_size;
  300. out:
  301. return len;
  302. }
  303. int __xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
  304. struct net_device *dev, u32 queue_index,
  305. unsigned int napi_id, u32 frag_size);
  306. static inline int
  307. xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
  308. struct net_device *dev, u32 queue_index,
  309. unsigned int napi_id)
  310. {
  311. return __xdp_rxq_info_reg(xdp_rxq, dev, queue_index, napi_id, 0);
  312. }
  313. void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq);
  314. void xdp_rxq_info_unused(struct xdp_rxq_info *xdp_rxq);
  315. bool xdp_rxq_info_is_reg(struct xdp_rxq_info *xdp_rxq);
  316. int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
  317. enum xdp_mem_type type, void *allocator);
  318. void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq);
  319. int xdp_reg_mem_model(struct xdp_mem_info *mem,
  320. enum xdp_mem_type type, void *allocator);
  321. void xdp_unreg_mem_model(struct xdp_mem_info *mem);
  322. /* Drivers not supporting XDP metadata can use this helper, which
  323. * rejects any room expansion for metadata as a result.
  324. */
  325. static __always_inline void
  326. xdp_set_data_meta_invalid(struct xdp_buff *xdp)
  327. {
  328. xdp->data_meta = xdp->data + 1;
  329. }
  330. static __always_inline bool
  331. xdp_data_meta_unsupported(const struct xdp_buff *xdp)
  332. {
  333. return unlikely(xdp->data_meta > xdp->data);
  334. }
  335. static inline bool xdp_metalen_invalid(unsigned long metalen)
  336. {
  337. return (metalen & (sizeof(__u32) - 1)) || (metalen > 32);
  338. }
  339. struct xdp_attachment_info {
  340. struct bpf_prog *prog;
  341. u32 flags;
  342. };
  343. struct netdev_bpf;
  344. void xdp_attachment_setup(struct xdp_attachment_info *info,
  345. struct netdev_bpf *bpf);
  346. #define DEV_MAP_BULK_SIZE XDP_BULK_QUEUE_SIZE
  347. #endif /* __LINUX_NET_XDP_H__ */