xdp.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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/bitfield.h>
  9. #include <linux/filter.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/skbuff.h> /* skb_shared_info */
  12. #include <net/page_pool/types.h>
  13. /**
  14. * DOC: XDP RX-queue information
  15. *
  16. * The XDP RX-queue info (xdp_rxq_info) is associated with the driver
  17. * level RX-ring queues. It is information that is specific to how
  18. * the driver has configured a given RX-ring queue.
  19. *
  20. * Each xdp_buff frame received in the driver carries a (pointer)
  21. * reference to this xdp_rxq_info structure. This provides the XDP
  22. * data-path read-access to RX-info for both kernel and bpf-side
  23. * (limited subset).
  24. *
  25. * For now, direct access is only safe while running in NAPI/softirq
  26. * context. Contents are read-mostly and must not be updated during
  27. * driver NAPI/softirq poll.
  28. *
  29. * The driver usage API is a register and unregister API.
  30. *
  31. * The struct is not directly tied to the XDP prog. A new XDP prog
  32. * can be attached as long as it doesn't change the underlying
  33. * RX-ring. If the RX-ring does change significantly, the NIC driver
  34. * naturally needs to stop the RX-ring before purging and reallocating
  35. * memory. In that process the driver MUST call unregister (which
  36. * also applies for driver shutdown and unload). The register API is
  37. * also mandatory during RX-ring setup.
  38. */
  39. enum xdp_mem_type {
  40. MEM_TYPE_PAGE_SHARED = 0, /* Split-page refcnt based model */
  41. MEM_TYPE_PAGE_ORDER0, /* Orig XDP full page model */
  42. MEM_TYPE_PAGE_POOL,
  43. MEM_TYPE_XSK_BUFF_POOL,
  44. MEM_TYPE_MAX,
  45. };
  46. /* XDP flags for ndo_xdp_xmit */
  47. #define XDP_XMIT_FLUSH (1U << 0) /* doorbell signal consumer */
  48. #define XDP_XMIT_FLAGS_MASK XDP_XMIT_FLUSH
  49. struct xdp_mem_info {
  50. u32 type; /* enum xdp_mem_type, but known size type */
  51. u32 id;
  52. };
  53. struct page_pool;
  54. struct xdp_rxq_info {
  55. struct net_device *dev;
  56. u32 queue_index;
  57. u32 reg_state;
  58. struct xdp_mem_info mem;
  59. u32 frag_size;
  60. } ____cacheline_aligned; /* perf critical, avoid false-sharing */
  61. struct xdp_txq_info {
  62. struct net_device *dev;
  63. };
  64. enum xdp_buff_flags {
  65. XDP_FLAGS_HAS_FRAGS = BIT(0), /* non-linear xdp buff */
  66. XDP_FLAGS_FRAGS_PF_MEMALLOC = BIT(1), /* xdp paged memory is under
  67. * pressure
  68. */
  69. /* frags have unreadable mem, this can't be true for real XDP packets,
  70. * but drivers may use XDP helpers to construct Rx pkt state even when
  71. * XDP program is not attached.
  72. */
  73. XDP_FLAGS_FRAGS_UNREADABLE = BIT(2),
  74. };
  75. struct xdp_buff {
  76. void *data;
  77. void *data_end;
  78. void *data_meta;
  79. void *data_hard_start;
  80. struct xdp_rxq_info *rxq;
  81. struct xdp_txq_info *txq;
  82. union {
  83. struct {
  84. /* frame size to deduce data_hard_end/tailroom */
  85. u32 frame_sz;
  86. /* supported values defined in xdp_buff_flags */
  87. u32 flags;
  88. };
  89. #ifdef __LITTLE_ENDIAN
  90. /* Used to micro-optimize xdp_init_buff(), don't use directly */
  91. u64 frame_sz_flags_init;
  92. #endif
  93. };
  94. };
  95. static __always_inline bool xdp_buff_has_frags(const struct xdp_buff *xdp)
  96. {
  97. return !!(xdp->flags & XDP_FLAGS_HAS_FRAGS);
  98. }
  99. static __always_inline void xdp_buff_set_frags_flag(struct xdp_buff *xdp)
  100. {
  101. xdp->flags |= XDP_FLAGS_HAS_FRAGS;
  102. }
  103. static __always_inline void xdp_buff_clear_frags_flag(struct xdp_buff *xdp)
  104. {
  105. xdp->flags &= ~XDP_FLAGS_HAS_FRAGS;
  106. }
  107. static __always_inline void xdp_buff_set_frag_pfmemalloc(struct xdp_buff *xdp)
  108. {
  109. xdp->flags |= XDP_FLAGS_FRAGS_PF_MEMALLOC;
  110. }
  111. static __always_inline void xdp_buff_set_frag_unreadable(struct xdp_buff *xdp)
  112. {
  113. xdp->flags |= XDP_FLAGS_FRAGS_UNREADABLE;
  114. }
  115. static __always_inline u32 xdp_buff_get_skb_flags(const struct xdp_buff *xdp)
  116. {
  117. return xdp->flags;
  118. }
  119. static __always_inline void xdp_buff_clear_frag_pfmemalloc(struct xdp_buff *xdp)
  120. {
  121. xdp->flags &= ~XDP_FLAGS_FRAGS_PF_MEMALLOC;
  122. }
  123. static __always_inline void
  124. xdp_init_buff(struct xdp_buff *xdp, u32 frame_sz, struct xdp_rxq_info *rxq)
  125. {
  126. xdp->rxq = rxq;
  127. #ifdef __LITTLE_ENDIAN
  128. /*
  129. * Force the compilers to initialize ::flags and assign ::frame_sz with
  130. * one write on 64-bit LE architectures as they're often unable to do
  131. * it themselves.
  132. */
  133. xdp->frame_sz_flags_init = frame_sz;
  134. #else
  135. xdp->frame_sz = frame_sz;
  136. xdp->flags = 0;
  137. #endif
  138. }
  139. static __always_inline void
  140. xdp_prepare_buff(struct xdp_buff *xdp, unsigned char *hard_start,
  141. int headroom, int data_len, const bool meta_valid)
  142. {
  143. unsigned char *data = hard_start + headroom;
  144. xdp->data_hard_start = hard_start;
  145. xdp->data = data;
  146. xdp->data_end = data + data_len;
  147. xdp->data_meta = meta_valid ? data : data + 1;
  148. }
  149. /* Reserve memory area at end-of data area.
  150. *
  151. * This macro reserves tailroom in the XDP buffer by limiting the
  152. * XDP/BPF data access to data_hard_end. Notice same area (and size)
  153. * is used for XDP_PASS, when constructing the SKB via build_skb().
  154. */
  155. #define xdp_data_hard_end(xdp) \
  156. ((xdp)->data_hard_start + (xdp)->frame_sz - \
  157. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
  158. static inline struct skb_shared_info *
  159. xdp_get_shared_info_from_buff(const struct xdp_buff *xdp)
  160. {
  161. return (struct skb_shared_info *)xdp_data_hard_end(xdp);
  162. }
  163. static __always_inline unsigned int
  164. xdp_get_buff_len(const struct xdp_buff *xdp)
  165. {
  166. unsigned int len = xdp->data_end - xdp->data;
  167. const struct skb_shared_info *sinfo;
  168. if (likely(!xdp_buff_has_frags(xdp)))
  169. goto out;
  170. sinfo = xdp_get_shared_info_from_buff(xdp);
  171. len += sinfo->xdp_frags_size;
  172. out:
  173. return len;
  174. }
  175. void xdp_return_frag(netmem_ref netmem, const struct xdp_buff *xdp);
  176. /**
  177. * __xdp_buff_add_frag - attach frag to &xdp_buff
  178. * @xdp: XDP buffer to attach the frag to
  179. * @netmem: network memory containing the frag
  180. * @offset: offset at which the frag starts
  181. * @size: size of the frag
  182. * @truesize: total memory size occupied by the frag
  183. * @try_coalesce: whether to try coalescing the frags (not valid for XSk)
  184. *
  185. * Attach frag to the XDP buffer. If it currently has no frags attached,
  186. * initialize the related fields, otherwise check that the frag number
  187. * didn't reach the limit of ``MAX_SKB_FRAGS``. If possible, try coalescing
  188. * the frag with the previous one.
  189. * The function doesn't check/update the pfmemalloc bit. Please use the
  190. * non-underscored wrapper in drivers.
  191. *
  192. * Return: true on success, false if there's no space for the frag in
  193. * the shared info struct.
  194. */
  195. static inline bool __xdp_buff_add_frag(struct xdp_buff *xdp, netmem_ref netmem,
  196. u32 offset, u32 size, u32 truesize,
  197. bool try_coalesce)
  198. {
  199. struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
  200. skb_frag_t *prev;
  201. u32 nr_frags;
  202. if (!xdp_buff_has_frags(xdp)) {
  203. xdp_buff_set_frags_flag(xdp);
  204. nr_frags = 0;
  205. sinfo->xdp_frags_size = 0;
  206. sinfo->xdp_frags_truesize = 0;
  207. goto fill;
  208. }
  209. nr_frags = sinfo->nr_frags;
  210. prev = &sinfo->frags[nr_frags - 1];
  211. if (try_coalesce && netmem == skb_frag_netmem(prev) &&
  212. offset == skb_frag_off(prev) + skb_frag_size(prev)) {
  213. skb_frag_size_add(prev, size);
  214. /* Guaranteed to only decrement the refcount */
  215. xdp_return_frag(netmem, xdp);
  216. } else if (unlikely(nr_frags == MAX_SKB_FRAGS)) {
  217. return false;
  218. } else {
  219. fill:
  220. __skb_fill_netmem_desc_noacc(sinfo, nr_frags++, netmem,
  221. offset, size);
  222. }
  223. sinfo->nr_frags = nr_frags;
  224. sinfo->xdp_frags_size += size;
  225. sinfo->xdp_frags_truesize += truesize;
  226. return true;
  227. }
  228. /**
  229. * xdp_buff_add_frag - attach frag to &xdp_buff
  230. * @xdp: XDP buffer to attach the frag to
  231. * @netmem: network memory containing the frag
  232. * @offset: offset at which the frag starts
  233. * @size: size of the frag
  234. * @truesize: total memory size occupied by the frag
  235. *
  236. * Version of __xdp_buff_add_frag() which takes care of the pfmemalloc bit.
  237. *
  238. * Return: true on success, false if there's no space for the frag in
  239. * the shared info struct.
  240. */
  241. static inline bool xdp_buff_add_frag(struct xdp_buff *xdp, netmem_ref netmem,
  242. u32 offset, u32 size, u32 truesize)
  243. {
  244. if (!__xdp_buff_add_frag(xdp, netmem, offset, size, truesize, true))
  245. return false;
  246. if (unlikely(netmem_is_pfmemalloc(netmem)))
  247. xdp_buff_set_frag_pfmemalloc(xdp);
  248. if (unlikely(netmem_is_net_iov(netmem)))
  249. xdp_buff_set_frag_unreadable(xdp);
  250. return true;
  251. }
  252. struct xdp_frame {
  253. void *data;
  254. u32 len;
  255. u32 headroom;
  256. u32 metasize; /* uses lower 8-bits */
  257. /* Lifetime of xdp_rxq_info is limited to NAPI/enqueue time,
  258. * while mem_type is valid on remote CPU.
  259. */
  260. enum xdp_mem_type mem_type:32;
  261. struct net_device *dev_rx; /* used by cpumap */
  262. u32 frame_sz;
  263. u32 flags; /* supported values defined in xdp_buff_flags */
  264. };
  265. static __always_inline bool xdp_frame_has_frags(const struct xdp_frame *frame)
  266. {
  267. return !!(frame->flags & XDP_FLAGS_HAS_FRAGS);
  268. }
  269. static __always_inline u32
  270. xdp_frame_get_skb_flags(const struct xdp_frame *frame)
  271. {
  272. return frame->flags;
  273. }
  274. #define XDP_BULK_QUEUE_SIZE 16
  275. struct xdp_frame_bulk {
  276. int count;
  277. netmem_ref q[XDP_BULK_QUEUE_SIZE];
  278. };
  279. static __always_inline void xdp_frame_bulk_init(struct xdp_frame_bulk *bq)
  280. {
  281. bq->count = 0;
  282. }
  283. static inline struct skb_shared_info *
  284. xdp_get_shared_info_from_frame(const struct xdp_frame *frame)
  285. {
  286. void *data_hard_start = frame->data - frame->headroom - sizeof(*frame);
  287. return (struct skb_shared_info *)(data_hard_start + frame->frame_sz -
  288. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
  289. }
  290. struct xdp_cpumap_stats {
  291. unsigned int redirect;
  292. unsigned int pass;
  293. unsigned int drop;
  294. };
  295. /* Clear kernel pointers in xdp_frame */
  296. static inline void xdp_scrub_frame(struct xdp_frame *frame)
  297. {
  298. frame->data = NULL;
  299. frame->dev_rx = NULL;
  300. }
  301. static inline void
  302. xdp_update_skb_frags_info(struct sk_buff *skb, u8 nr_frags,
  303. unsigned int size, unsigned int truesize,
  304. u32 xdp_flags)
  305. {
  306. struct skb_shared_info *sinfo = skb_shinfo(skb);
  307. sinfo->nr_frags = nr_frags;
  308. /*
  309. * ``destructor_arg`` is unionized with ``xdp_frags_{,true}size``,
  310. * reset it after that these fields aren't used anymore.
  311. */
  312. sinfo->destructor_arg = NULL;
  313. skb->len += size;
  314. skb->data_len += size;
  315. skb->truesize += truesize;
  316. skb->pfmemalloc |= !!(xdp_flags & XDP_FLAGS_FRAGS_PF_MEMALLOC);
  317. skb->unreadable |= !!(xdp_flags & XDP_FLAGS_FRAGS_UNREADABLE);
  318. }
  319. /* Avoids inlining WARN macro in fast-path */
  320. void xdp_warn(const char *msg, const char *func, const int line);
  321. #define XDP_WARN(msg) xdp_warn(msg, __func__, __LINE__)
  322. struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp);
  323. struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp);
  324. struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp);
  325. struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
  326. struct sk_buff *skb,
  327. struct net_device *dev);
  328. struct sk_buff *xdp_build_skb_from_frame(struct xdp_frame *xdpf,
  329. struct net_device *dev);
  330. struct xdp_frame *xdpf_clone(struct xdp_frame *xdpf);
  331. static inline
  332. void xdp_convert_frame_to_buff(const struct xdp_frame *frame,
  333. struct xdp_buff *xdp)
  334. {
  335. xdp->data_hard_start = frame->data - frame->headroom - sizeof(*frame);
  336. xdp->data = frame->data;
  337. xdp->data_end = frame->data + frame->len;
  338. xdp->data_meta = frame->data - frame->metasize;
  339. xdp->frame_sz = frame->frame_sz;
  340. xdp->flags = frame->flags;
  341. }
  342. static inline
  343. int xdp_update_frame_from_buff(const struct xdp_buff *xdp,
  344. struct xdp_frame *xdp_frame)
  345. {
  346. int metasize, headroom;
  347. /* Assure headroom is available for storing info */
  348. headroom = xdp->data - xdp->data_hard_start;
  349. metasize = xdp->data - xdp->data_meta;
  350. metasize = metasize > 0 ? metasize : 0;
  351. if (unlikely((headroom - metasize) < sizeof(*xdp_frame)))
  352. return -ENOSPC;
  353. /* Catch if driver didn't reserve tailroom for skb_shared_info */
  354. if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) {
  355. XDP_WARN("Driver BUG: missing reserved tailroom");
  356. return -ENOSPC;
  357. }
  358. xdp_frame->data = xdp->data;
  359. xdp_frame->len = xdp->data_end - xdp->data;
  360. xdp_frame->headroom = headroom - sizeof(*xdp_frame);
  361. xdp_frame->metasize = metasize;
  362. xdp_frame->frame_sz = xdp->frame_sz;
  363. xdp_frame->flags = xdp->flags;
  364. return 0;
  365. }
  366. /* Convert xdp_buff to xdp_frame */
  367. static inline
  368. struct xdp_frame *xdp_convert_buff_to_frame(struct xdp_buff *xdp)
  369. {
  370. struct xdp_frame *xdp_frame;
  371. if (xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL)
  372. return xdp_convert_zc_to_xdp_frame(xdp);
  373. /* Store info in top of packet */
  374. xdp_frame = xdp->data_hard_start;
  375. if (unlikely(xdp_update_frame_from_buff(xdp, xdp_frame) < 0))
  376. return NULL;
  377. /* rxq only valid until napi_schedule ends, convert to xdp_mem_type */
  378. xdp_frame->mem_type = xdp->rxq->mem.type;
  379. return xdp_frame;
  380. }
  381. void __xdp_return(netmem_ref netmem, enum xdp_mem_type mem_type,
  382. bool napi_direct, struct xdp_buff *xdp);
  383. void xdp_return_frame(struct xdp_frame *xdpf);
  384. void xdp_return_frame_rx_napi(struct xdp_frame *xdpf);
  385. void xdp_return_buff(struct xdp_buff *xdp);
  386. void xdp_return_frame_bulk(struct xdp_frame *xdpf,
  387. struct xdp_frame_bulk *bq);
  388. static inline void xdp_flush_frame_bulk(struct xdp_frame_bulk *bq)
  389. {
  390. if (unlikely(!bq->count))
  391. return;
  392. page_pool_put_netmem_bulk(bq->q, bq->count);
  393. bq->count = 0;
  394. }
  395. static __always_inline unsigned int
  396. xdp_get_frame_len(const struct xdp_frame *xdpf)
  397. {
  398. const struct skb_shared_info *sinfo;
  399. unsigned int len = xdpf->len;
  400. if (likely(!xdp_frame_has_frags(xdpf)))
  401. goto out;
  402. sinfo = xdp_get_shared_info_from_frame(xdpf);
  403. len += sinfo->xdp_frags_size;
  404. out:
  405. return len;
  406. }
  407. int __xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
  408. struct net_device *dev, u32 queue_index,
  409. unsigned int napi_id, u32 frag_size);
  410. static inline int
  411. xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
  412. struct net_device *dev, u32 queue_index,
  413. unsigned int napi_id)
  414. {
  415. return __xdp_rxq_info_reg(xdp_rxq, dev, queue_index, napi_id, 0);
  416. }
  417. void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq);
  418. void xdp_rxq_info_unused(struct xdp_rxq_info *xdp_rxq);
  419. bool xdp_rxq_info_is_reg(struct xdp_rxq_info *xdp_rxq);
  420. int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
  421. enum xdp_mem_type type, void *allocator);
  422. void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq);
  423. int xdp_reg_mem_model(struct xdp_mem_info *mem,
  424. enum xdp_mem_type type, void *allocator);
  425. void xdp_unreg_mem_model(struct xdp_mem_info *mem);
  426. int xdp_reg_page_pool(struct page_pool *pool);
  427. void xdp_unreg_page_pool(const struct page_pool *pool);
  428. void xdp_rxq_info_attach_page_pool(struct xdp_rxq_info *xdp_rxq,
  429. const struct page_pool *pool);
  430. /**
  431. * xdp_rxq_info_attach_mem_model - attach registered mem info to RxQ info
  432. * @xdp_rxq: XDP RxQ info to attach the memory info to
  433. * @mem: already registered memory info
  434. *
  435. * If the driver registers its memory providers manually, it must use this
  436. * function instead of xdp_rxq_info_reg_mem_model().
  437. */
  438. static inline void
  439. xdp_rxq_info_attach_mem_model(struct xdp_rxq_info *xdp_rxq,
  440. const struct xdp_mem_info *mem)
  441. {
  442. xdp_rxq->mem = *mem;
  443. }
  444. /**
  445. * xdp_rxq_info_detach_mem_model - detach registered mem info from RxQ info
  446. * @xdp_rxq: XDP RxQ info to detach the memory info from
  447. *
  448. * If the driver registers its memory providers manually and then attaches it
  449. * via xdp_rxq_info_attach_mem_model(), it must call this function before
  450. * xdp_rxq_info_unreg().
  451. */
  452. static inline void xdp_rxq_info_detach_mem_model(struct xdp_rxq_info *xdp_rxq)
  453. {
  454. xdp_rxq->mem = (struct xdp_mem_info){ };
  455. }
  456. /* Drivers not supporting XDP metadata can use this helper, which
  457. * rejects any room expansion for metadata as a result.
  458. */
  459. static __always_inline void
  460. xdp_set_data_meta_invalid(struct xdp_buff *xdp)
  461. {
  462. xdp->data_meta = xdp->data + 1;
  463. }
  464. static __always_inline bool
  465. xdp_data_meta_unsupported(const struct xdp_buff *xdp)
  466. {
  467. return unlikely(xdp->data_meta > xdp->data);
  468. }
  469. static inline bool xdp_metalen_invalid(unsigned long metalen)
  470. {
  471. unsigned long meta_max;
  472. meta_max = type_max(typeof_member(struct skb_shared_info, meta_len));
  473. BUILD_BUG_ON(!__builtin_constant_p(meta_max));
  474. return !IS_ALIGNED(metalen, sizeof(u32)) || metalen > meta_max;
  475. }
  476. struct xdp_attachment_info {
  477. struct bpf_prog *prog;
  478. u32 flags;
  479. };
  480. struct netdev_bpf;
  481. void xdp_attachment_setup(struct xdp_attachment_info *info,
  482. struct netdev_bpf *bpf);
  483. #define DEV_MAP_BULK_SIZE XDP_BULK_QUEUE_SIZE
  484. /* Define the relationship between xdp-rx-metadata kfunc and
  485. * various other entities:
  486. * - xdp_rx_metadata enum
  487. * - netdev netlink enum (Documentation/netlink/specs/netdev.yaml)
  488. * - kfunc name
  489. * - xdp_metadata_ops field
  490. */
  491. #define XDP_METADATA_KFUNC_xxx \
  492. XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_TIMESTAMP, \
  493. NETDEV_XDP_RX_METADATA_TIMESTAMP, \
  494. bpf_xdp_metadata_rx_timestamp, \
  495. xmo_rx_timestamp) \
  496. XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_HASH, \
  497. NETDEV_XDP_RX_METADATA_HASH, \
  498. bpf_xdp_metadata_rx_hash, \
  499. xmo_rx_hash) \
  500. XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_VLAN_TAG, \
  501. NETDEV_XDP_RX_METADATA_VLAN_TAG, \
  502. bpf_xdp_metadata_rx_vlan_tag, \
  503. xmo_rx_vlan_tag) \
  504. enum xdp_rx_metadata {
  505. #define XDP_METADATA_KFUNC(name, _, __, ___) name,
  506. XDP_METADATA_KFUNC_xxx
  507. #undef XDP_METADATA_KFUNC
  508. MAX_XDP_METADATA_KFUNC,
  509. };
  510. enum xdp_rss_hash_type {
  511. /* First part: Individual bits for L3/L4 types */
  512. XDP_RSS_L3_IPV4 = BIT(0),
  513. XDP_RSS_L3_IPV6 = BIT(1),
  514. /* The fixed (L3) IPv4 and IPv6 headers can both be followed by
  515. * variable/dynamic headers, IPv4 called Options and IPv6 called
  516. * Extension Headers. HW RSS type can contain this info.
  517. */
  518. XDP_RSS_L3_DYNHDR = BIT(2),
  519. /* When RSS hash covers L4 then drivers MUST set XDP_RSS_L4 bit in
  520. * addition to the protocol specific bit. This ease interaction with
  521. * SKBs and avoids reserving a fixed mask for future L4 protocol bits.
  522. */
  523. XDP_RSS_L4 = BIT(3), /* L4 based hash, proto can be unknown */
  524. XDP_RSS_L4_TCP = BIT(4),
  525. XDP_RSS_L4_UDP = BIT(5),
  526. XDP_RSS_L4_SCTP = BIT(6),
  527. XDP_RSS_L4_IPSEC = BIT(7), /* L4 based hash include IPSEC SPI */
  528. XDP_RSS_L4_ICMP = BIT(8),
  529. /* Second part: RSS hash type combinations used for driver HW mapping */
  530. XDP_RSS_TYPE_NONE = 0,
  531. XDP_RSS_TYPE_L2 = XDP_RSS_TYPE_NONE,
  532. XDP_RSS_TYPE_L3_IPV4 = XDP_RSS_L3_IPV4,
  533. XDP_RSS_TYPE_L3_IPV6 = XDP_RSS_L3_IPV6,
  534. XDP_RSS_TYPE_L3_IPV4_OPT = XDP_RSS_L3_IPV4 | XDP_RSS_L3_DYNHDR,
  535. XDP_RSS_TYPE_L3_IPV6_EX = XDP_RSS_L3_IPV6 | XDP_RSS_L3_DYNHDR,
  536. XDP_RSS_TYPE_L4_ANY = XDP_RSS_L4,
  537. XDP_RSS_TYPE_L4_IPV4_TCP = XDP_RSS_L3_IPV4 | XDP_RSS_L4 | XDP_RSS_L4_TCP,
  538. XDP_RSS_TYPE_L4_IPV4_UDP = XDP_RSS_L3_IPV4 | XDP_RSS_L4 | XDP_RSS_L4_UDP,
  539. XDP_RSS_TYPE_L4_IPV4_SCTP = XDP_RSS_L3_IPV4 | XDP_RSS_L4 | XDP_RSS_L4_SCTP,
  540. XDP_RSS_TYPE_L4_IPV4_IPSEC = XDP_RSS_L3_IPV4 | XDP_RSS_L4 | XDP_RSS_L4_IPSEC,
  541. XDP_RSS_TYPE_L4_IPV4_ICMP = XDP_RSS_L3_IPV4 | XDP_RSS_L4 | XDP_RSS_L4_ICMP,
  542. XDP_RSS_TYPE_L4_IPV6_TCP = XDP_RSS_L3_IPV6 | XDP_RSS_L4 | XDP_RSS_L4_TCP,
  543. XDP_RSS_TYPE_L4_IPV6_UDP = XDP_RSS_L3_IPV6 | XDP_RSS_L4 | XDP_RSS_L4_UDP,
  544. XDP_RSS_TYPE_L4_IPV6_SCTP = XDP_RSS_L3_IPV6 | XDP_RSS_L4 | XDP_RSS_L4_SCTP,
  545. XDP_RSS_TYPE_L4_IPV6_IPSEC = XDP_RSS_L3_IPV6 | XDP_RSS_L4 | XDP_RSS_L4_IPSEC,
  546. XDP_RSS_TYPE_L4_IPV6_ICMP = XDP_RSS_L3_IPV6 | XDP_RSS_L4 | XDP_RSS_L4_ICMP,
  547. XDP_RSS_TYPE_L4_IPV6_TCP_EX = XDP_RSS_TYPE_L4_IPV6_TCP | XDP_RSS_L3_DYNHDR,
  548. XDP_RSS_TYPE_L4_IPV6_UDP_EX = XDP_RSS_TYPE_L4_IPV6_UDP | XDP_RSS_L3_DYNHDR,
  549. XDP_RSS_TYPE_L4_IPV6_SCTP_EX = XDP_RSS_TYPE_L4_IPV6_SCTP | XDP_RSS_L3_DYNHDR,
  550. };
  551. struct xdp_metadata_ops {
  552. int (*xmo_rx_timestamp)(const struct xdp_md *ctx, u64 *timestamp);
  553. int (*xmo_rx_hash)(const struct xdp_md *ctx, u32 *hash,
  554. enum xdp_rss_hash_type *rss_type);
  555. int (*xmo_rx_vlan_tag)(const struct xdp_md *ctx, __be16 *vlan_proto,
  556. u16 *vlan_tci);
  557. };
  558. #ifdef CONFIG_NET
  559. u32 bpf_xdp_metadata_kfunc_id(int id);
  560. bool bpf_dev_bound_kfunc_id(u32 btf_id);
  561. void xdp_set_features_flag(struct net_device *dev, xdp_features_t val);
  562. void xdp_set_features_flag_locked(struct net_device *dev, xdp_features_t val);
  563. void xdp_features_set_redirect_target(struct net_device *dev, bool support_sg);
  564. void xdp_features_set_redirect_target_locked(struct net_device *dev,
  565. bool support_sg);
  566. void xdp_features_clear_redirect_target(struct net_device *dev);
  567. void xdp_features_clear_redirect_target_locked(struct net_device *dev);
  568. #else
  569. static inline u32 bpf_xdp_metadata_kfunc_id(int id) { return 0; }
  570. static inline bool bpf_dev_bound_kfunc_id(u32 btf_id) { return false; }
  571. static inline void
  572. xdp_set_features_flag(struct net_device *dev, xdp_features_t val)
  573. {
  574. }
  575. static inline void
  576. xdp_features_set_redirect_target(struct net_device *dev, bool support_sg)
  577. {
  578. }
  579. static inline void
  580. xdp_features_clear_redirect_target(struct net_device *dev)
  581. {
  582. }
  583. #endif
  584. static inline void xdp_clear_features_flag(struct net_device *dev)
  585. {
  586. xdp_set_features_flag(dev, 0);
  587. }
  588. static __always_inline u32 bpf_prog_run_xdp(const struct bpf_prog *prog,
  589. struct xdp_buff *xdp)
  590. {
  591. /* Driver XDP hooks are invoked within a single NAPI poll cycle and thus
  592. * under local_bh_disable(), which provides the needed RCU protection
  593. * for accessing map entries.
  594. */
  595. u32 act = __bpf_prog_run(prog, xdp, BPF_DISPATCHER_FUNC(xdp));
  596. if (static_branch_unlikely(&bpf_master_redirect_enabled_key)) {
  597. if (act == XDP_TX && netif_is_bond_slave(xdp->rxq->dev))
  598. act = xdp_master_redirect(xdp);
  599. }
  600. return act;
  601. }
  602. #endif /* __LINUX_NET_XDP_H__ */