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