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 int xdp_alloc_skb_bulk(void **skbs, int n_skb, gfp_t gfp);
347 struct xdp_frame *xdpf_clone(struct xdp_frame *xdpf);
348
349 static inline
xdp_convert_frame_to_buff(const struct xdp_frame * frame,struct xdp_buff * xdp)350 void xdp_convert_frame_to_buff(const struct xdp_frame *frame,
351 struct xdp_buff *xdp)
352 {
353 xdp->data_hard_start = frame->data - frame->headroom - sizeof(*frame);
354 xdp->data = frame->data;
355 xdp->data_end = frame->data + frame->len;
356 xdp->data_meta = frame->data - frame->metasize;
357 xdp->frame_sz = frame->frame_sz;
358 xdp->flags = frame->flags;
359 }
360
361 static inline
xdp_update_frame_from_buff(const struct xdp_buff * xdp,struct xdp_frame * xdp_frame)362 int xdp_update_frame_from_buff(const struct xdp_buff *xdp,
363 struct xdp_frame *xdp_frame)
364 {
365 int metasize, headroom;
366
367 /* Assure headroom is available for storing info */
368 headroom = xdp->data - xdp->data_hard_start;
369 metasize = xdp->data - xdp->data_meta;
370 metasize = metasize > 0 ? metasize : 0;
371 if (unlikely((headroom - metasize) < sizeof(*xdp_frame)))
372 return -ENOSPC;
373
374 /* Catch if driver didn't reserve tailroom for skb_shared_info */
375 if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) {
376 XDP_WARN("Driver BUG: missing reserved tailroom");
377 return -ENOSPC;
378 }
379
380 xdp_frame->data = xdp->data;
381 xdp_frame->len = xdp->data_end - xdp->data;
382 xdp_frame->headroom = headroom - sizeof(*xdp_frame);
383 xdp_frame->metasize = metasize;
384 xdp_frame->frame_sz = xdp->frame_sz;
385 xdp_frame->flags = xdp->flags;
386
387 return 0;
388 }
389
390 /* Convert xdp_buff to xdp_frame */
391 static inline
xdp_convert_buff_to_frame(struct xdp_buff * xdp)392 struct xdp_frame *xdp_convert_buff_to_frame(struct xdp_buff *xdp)
393 {
394 struct xdp_frame *xdp_frame;
395
396 if (xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL)
397 return xdp_convert_zc_to_xdp_frame(xdp);
398
399 /* Store info in top of packet */
400 xdp_frame = xdp->data_hard_start;
401 if (unlikely(xdp_update_frame_from_buff(xdp, xdp_frame) < 0))
402 return NULL;
403
404 /* rxq only valid until napi_schedule ends, convert to xdp_mem_type */
405 xdp_frame->mem_type = xdp->rxq->mem.type;
406
407 return xdp_frame;
408 }
409
410 void __xdp_return(netmem_ref netmem, enum xdp_mem_type mem_type,
411 bool napi_direct, struct xdp_buff *xdp);
412 void xdp_return_frame(struct xdp_frame *xdpf);
413 void xdp_return_frame_rx_napi(struct xdp_frame *xdpf);
414 void xdp_return_buff(struct xdp_buff *xdp);
415 void xdp_return_frame_bulk(struct xdp_frame *xdpf,
416 struct xdp_frame_bulk *bq);
417
xdp_flush_frame_bulk(struct xdp_frame_bulk * bq)418 static inline void xdp_flush_frame_bulk(struct xdp_frame_bulk *bq)
419 {
420 if (unlikely(!bq->count))
421 return;
422
423 page_pool_put_netmem_bulk(bq->q, bq->count);
424 bq->count = 0;
425 }
426
427 static __always_inline unsigned int
xdp_get_frame_len(const struct xdp_frame * xdpf)428 xdp_get_frame_len(const struct xdp_frame *xdpf)
429 {
430 const struct skb_shared_info *sinfo;
431 unsigned int len = xdpf->len;
432
433 if (likely(!xdp_frame_has_frags(xdpf)))
434 goto out;
435
436 sinfo = xdp_get_shared_info_from_frame(xdpf);
437 len += sinfo->xdp_frags_size;
438 out:
439 return len;
440 }
441
442 int __xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
443 struct net_device *dev, u32 queue_index,
444 unsigned int napi_id, u32 frag_size);
445 static inline int
xdp_rxq_info_reg(struct xdp_rxq_info * xdp_rxq,struct net_device * dev,u32 queue_index,unsigned int napi_id)446 xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
447 struct net_device *dev, u32 queue_index,
448 unsigned int napi_id)
449 {
450 return __xdp_rxq_info_reg(xdp_rxq, dev, queue_index, napi_id, 0);
451 }
452
453 void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq);
454 void xdp_rxq_info_unused(struct xdp_rxq_info *xdp_rxq);
455 bool xdp_rxq_info_is_reg(struct xdp_rxq_info *xdp_rxq);
456 int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
457 enum xdp_mem_type type, void *allocator);
458 void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq);
459 int xdp_reg_mem_model(struct xdp_mem_info *mem,
460 enum xdp_mem_type type, void *allocator);
461 void xdp_unreg_mem_model(struct xdp_mem_info *mem);
462 int xdp_reg_page_pool(struct page_pool *pool);
463 void xdp_unreg_page_pool(const struct page_pool *pool);
464 void xdp_rxq_info_attach_page_pool(struct xdp_rxq_info *xdp_rxq,
465 const struct page_pool *pool);
466
467 /**
468 * xdp_rxq_info_attach_mem_model - attach registered mem info to RxQ info
469 * @xdp_rxq: XDP RxQ info to attach the memory info to
470 * @mem: already registered memory info
471 *
472 * If the driver registers its memory providers manually, it must use this
473 * function instead of xdp_rxq_info_reg_mem_model().
474 */
475 static inline void
xdp_rxq_info_attach_mem_model(struct xdp_rxq_info * xdp_rxq,const struct xdp_mem_info * mem)476 xdp_rxq_info_attach_mem_model(struct xdp_rxq_info *xdp_rxq,
477 const struct xdp_mem_info *mem)
478 {
479 xdp_rxq->mem = *mem;
480 }
481
482 /**
483 * xdp_rxq_info_detach_mem_model - detach registered mem info from RxQ info
484 * @xdp_rxq: XDP RxQ info to detach the memory info from
485 *
486 * If the driver registers its memory providers manually and then attaches it
487 * via xdp_rxq_info_attach_mem_model(), it must call this function before
488 * xdp_rxq_info_unreg().
489 */
xdp_rxq_info_detach_mem_model(struct xdp_rxq_info * xdp_rxq)490 static inline void xdp_rxq_info_detach_mem_model(struct xdp_rxq_info *xdp_rxq)
491 {
492 xdp_rxq->mem = (struct xdp_mem_info){ };
493 }
494
495 /* Drivers not supporting XDP metadata can use this helper, which
496 * rejects any room expansion for metadata as a result.
497 */
498 static __always_inline void
xdp_set_data_meta_invalid(struct xdp_buff * xdp)499 xdp_set_data_meta_invalid(struct xdp_buff *xdp)
500 {
501 xdp->data_meta = xdp->data + 1;
502 }
503
504 static __always_inline bool
xdp_data_meta_unsupported(const struct xdp_buff * xdp)505 xdp_data_meta_unsupported(const struct xdp_buff *xdp)
506 {
507 return unlikely(xdp->data_meta > xdp->data);
508 }
509
xdp_metalen_invalid(unsigned long metalen)510 static inline bool xdp_metalen_invalid(unsigned long metalen)
511 {
512 unsigned long meta_max;
513
514 meta_max = type_max(typeof_member(struct skb_shared_info, meta_len));
515 BUILD_BUG_ON(!__builtin_constant_p(meta_max));
516
517 return !IS_ALIGNED(metalen, sizeof(u32)) || metalen > meta_max;
518 }
519
520 struct xdp_attachment_info {
521 struct bpf_prog *prog;
522 u32 flags;
523 };
524
525 struct netdev_bpf;
526 void xdp_attachment_setup(struct xdp_attachment_info *info,
527 struct netdev_bpf *bpf);
528
529 #define DEV_MAP_BULK_SIZE XDP_BULK_QUEUE_SIZE
530
531 /* Define the relationship between xdp-rx-metadata kfunc and
532 * various other entities:
533 * - xdp_rx_metadata enum
534 * - netdev netlink enum (Documentation/netlink/specs/netdev.yaml)
535 * - kfunc name
536 * - xdp_metadata_ops field
537 */
538 #define XDP_METADATA_KFUNC_xxx \
539 XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_TIMESTAMP, \
540 NETDEV_XDP_RX_METADATA_TIMESTAMP, \
541 bpf_xdp_metadata_rx_timestamp, \
542 xmo_rx_timestamp) \
543 XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_HASH, \
544 NETDEV_XDP_RX_METADATA_HASH, \
545 bpf_xdp_metadata_rx_hash, \
546 xmo_rx_hash) \
547 XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_VLAN_TAG, \
548 NETDEV_XDP_RX_METADATA_VLAN_TAG, \
549 bpf_xdp_metadata_rx_vlan_tag, \
550 xmo_rx_vlan_tag) \
551
552 enum xdp_rx_metadata {
553 #define XDP_METADATA_KFUNC(name, _, __, ___) name,
554 XDP_METADATA_KFUNC_xxx
555 #undef XDP_METADATA_KFUNC
556 MAX_XDP_METADATA_KFUNC,
557 };
558
559 enum xdp_rss_hash_type {
560 /* First part: Individual bits for L3/L4 types */
561 XDP_RSS_L3_IPV4 = BIT(0),
562 XDP_RSS_L3_IPV6 = BIT(1),
563
564 /* The fixed (L3) IPv4 and IPv6 headers can both be followed by
565 * variable/dynamic headers, IPv4 called Options and IPv6 called
566 * Extension Headers. HW RSS type can contain this info.
567 */
568 XDP_RSS_L3_DYNHDR = BIT(2),
569
570 /* When RSS hash covers L4 then drivers MUST set XDP_RSS_L4 bit in
571 * addition to the protocol specific bit. This ease interaction with
572 * SKBs and avoids reserving a fixed mask for future L4 protocol bits.
573 */
574 XDP_RSS_L4 = BIT(3), /* L4 based hash, proto can be unknown */
575 XDP_RSS_L4_TCP = BIT(4),
576 XDP_RSS_L4_UDP = BIT(5),
577 XDP_RSS_L4_SCTP = BIT(6),
578 XDP_RSS_L4_IPSEC = BIT(7), /* L4 based hash include IPSEC SPI */
579 XDP_RSS_L4_ICMP = BIT(8),
580
581 /* Second part: RSS hash type combinations used for driver HW mapping */
582 XDP_RSS_TYPE_NONE = 0,
583 XDP_RSS_TYPE_L2 = XDP_RSS_TYPE_NONE,
584
585 XDP_RSS_TYPE_L3_IPV4 = XDP_RSS_L3_IPV4,
586 XDP_RSS_TYPE_L3_IPV6 = XDP_RSS_L3_IPV6,
587 XDP_RSS_TYPE_L3_IPV4_OPT = XDP_RSS_L3_IPV4 | XDP_RSS_L3_DYNHDR,
588 XDP_RSS_TYPE_L3_IPV6_EX = XDP_RSS_L3_IPV6 | XDP_RSS_L3_DYNHDR,
589
590 XDP_RSS_TYPE_L4_ANY = XDP_RSS_L4,
591 XDP_RSS_TYPE_L4_IPV4_TCP = XDP_RSS_L3_IPV4 | XDP_RSS_L4 | XDP_RSS_L4_TCP,
592 XDP_RSS_TYPE_L4_IPV4_UDP = XDP_RSS_L3_IPV4 | XDP_RSS_L4 | XDP_RSS_L4_UDP,
593 XDP_RSS_TYPE_L4_IPV4_SCTP = XDP_RSS_L3_IPV4 | XDP_RSS_L4 | XDP_RSS_L4_SCTP,
594 XDP_RSS_TYPE_L4_IPV4_IPSEC = XDP_RSS_L3_IPV4 | XDP_RSS_L4 | XDP_RSS_L4_IPSEC,
595 XDP_RSS_TYPE_L4_IPV4_ICMP = XDP_RSS_L3_IPV4 | XDP_RSS_L4 | XDP_RSS_L4_ICMP,
596
597 XDP_RSS_TYPE_L4_IPV6_TCP = XDP_RSS_L3_IPV6 | XDP_RSS_L4 | XDP_RSS_L4_TCP,
598 XDP_RSS_TYPE_L4_IPV6_UDP = XDP_RSS_L3_IPV6 | XDP_RSS_L4 | XDP_RSS_L4_UDP,
599 XDP_RSS_TYPE_L4_IPV6_SCTP = XDP_RSS_L3_IPV6 | XDP_RSS_L4 | XDP_RSS_L4_SCTP,
600 XDP_RSS_TYPE_L4_IPV6_IPSEC = XDP_RSS_L3_IPV6 | XDP_RSS_L4 | XDP_RSS_L4_IPSEC,
601 XDP_RSS_TYPE_L4_IPV6_ICMP = XDP_RSS_L3_IPV6 | XDP_RSS_L4 | XDP_RSS_L4_ICMP,
602
603 XDP_RSS_TYPE_L4_IPV6_TCP_EX = XDP_RSS_TYPE_L4_IPV6_TCP | XDP_RSS_L3_DYNHDR,
604 XDP_RSS_TYPE_L4_IPV6_UDP_EX = XDP_RSS_TYPE_L4_IPV6_UDP | XDP_RSS_L3_DYNHDR,
605 XDP_RSS_TYPE_L4_IPV6_SCTP_EX = XDP_RSS_TYPE_L4_IPV6_SCTP | XDP_RSS_L3_DYNHDR,
606 };
607
608 struct xdp_metadata_ops {
609 int (*xmo_rx_timestamp)(const struct xdp_md *ctx, u64 *timestamp);
610 int (*xmo_rx_hash)(const struct xdp_md *ctx, u32 *hash,
611 enum xdp_rss_hash_type *rss_type);
612 int (*xmo_rx_vlan_tag)(const struct xdp_md *ctx, __be16 *vlan_proto,
613 u16 *vlan_tci);
614 };
615
616 #ifdef CONFIG_NET
617 u32 bpf_xdp_metadata_kfunc_id(int id);
618 bool bpf_dev_bound_kfunc_id(u32 btf_id);
619 void xdp_set_features_flag(struct net_device *dev, xdp_features_t val);
620 void xdp_features_set_redirect_target(struct net_device *dev, bool support_sg);
621 void xdp_features_clear_redirect_target(struct net_device *dev);
622 #else
bpf_xdp_metadata_kfunc_id(int id)623 static inline u32 bpf_xdp_metadata_kfunc_id(int id) { return 0; }
bpf_dev_bound_kfunc_id(u32 btf_id)624 static inline bool bpf_dev_bound_kfunc_id(u32 btf_id) { return false; }
625
626 static inline void
xdp_set_features_flag(struct net_device * dev,xdp_features_t val)627 xdp_set_features_flag(struct net_device *dev, xdp_features_t val)
628 {
629 }
630
631 static inline void
xdp_features_set_redirect_target(struct net_device * dev,bool support_sg)632 xdp_features_set_redirect_target(struct net_device *dev, bool support_sg)
633 {
634 }
635
636 static inline void
xdp_features_clear_redirect_target(struct net_device * dev)637 xdp_features_clear_redirect_target(struct net_device *dev)
638 {
639 }
640 #endif
641
xdp_clear_features_flag(struct net_device * dev)642 static inline void xdp_clear_features_flag(struct net_device *dev)
643 {
644 xdp_set_features_flag(dev, 0);
645 }
646
bpf_prog_run_xdp(const struct bpf_prog * prog,struct xdp_buff * xdp)647 static __always_inline u32 bpf_prog_run_xdp(const struct bpf_prog *prog,
648 struct xdp_buff *xdp)
649 {
650 /* Driver XDP hooks are invoked within a single NAPI poll cycle and thus
651 * under local_bh_disable(), which provides the needed RCU protection
652 * for accessing map entries.
653 */
654 u32 act = __bpf_prog_run(prog, xdp, BPF_DISPATCHER_FUNC(xdp));
655
656 if (static_branch_unlikely(&bpf_master_redirect_enabled_key)) {
657 if (act == XDP_TX && netif_is_bond_slave(xdp->rxq->dev))
658 act = xdp_master_redirect(xdp);
659 }
660
661 return act;
662 }
663 #endif /* __LINUX_NET_XDP_H__ */
664