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