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