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