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