Merge tag 'mm-hotfixes-stable-2023-05-03-16-27' of git://git.kernel.org/pub/scm/linux...
[linux-block.git] / include / net / xdp.h
CommitLineData
ddc64d0a 1/* SPDX-License-Identifier: GPL-2.0-only */
aecd67b6
JDB
2/* include/net/xdp.h
3 *
4 * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
aecd67b6
JDB
5 */
6#ifndef __LINUX_NET_XDP_H__
7#define __LINUX_NET_XDP_H__
8
f95f0f95 9#include <linux/skbuff.h> /* skb_shared_info */
d3d854fd 10#include <uapi/linux/netdev.h>
0cd917a4 11#include <linux/bitfield.h>
f95f0f95 12
aecd67b6
JDB
13/**
14 * DOC: XDP RX-queue information
15 *
16 * The XDP RX-queue info (xdp_rxq_info) is associated with the driver
17 * level RX-ring queues. It is information that is specific to how
18 * the driver have configured a given RX-ring queue.
19 *
6bbc7103 20 * Each xdp_buff frame received in the driver carries a (pointer)
aecd67b6
JDB
21 * reference to this xdp_rxq_info structure. This provides the XDP
22 * data-path read-access to RX-info for both kernel and bpf-side
23 * (limited subset).
24 *
25 * For now, direct access is only safe while running in NAPI/softirq
6bbc7103 26 * context. Contents are read-mostly and must not be updated during
aecd67b6
JDB
27 * driver NAPI/softirq poll.
28 *
29 * The driver usage API is a register and unregister API.
30 *
31 * The struct is not directly tied to the XDP prog. A new XDP prog
32 * can be attached as long as it doesn't change the underlying
33 * RX-ring. If the RX-ring does change significantly, the NIC driver
34 * naturally need to stop the RX-ring before purging and reallocating
6bbc7103
KJ
35 * memory. In that process the driver MUST call unregister (which
36 * also applies for driver shutdown and unload). The register API is
aecd67b6
JDB
37 * also mandatory during RX-ring setup.
38 */
39
5ab073ff
JDB
40enum xdp_mem_type {
41 MEM_TYPE_PAGE_SHARED = 0, /* Split-page refcnt based model */
42 MEM_TYPE_PAGE_ORDER0, /* Orig XDP full page model */
57d0a1c1 43 MEM_TYPE_PAGE_POOL,
2b43470a 44 MEM_TYPE_XSK_BUFF_POOL,
5ab073ff
JDB
45 MEM_TYPE_MAX,
46};
47
d3d854fd
JK
48typedef u32 xdp_features_t;
49
42b33468 50/* XDP flags for ndo_xdp_xmit */
42b33468
JDB
51#define XDP_XMIT_FLUSH (1U << 0) /* doorbell signal consumer */
52#define XDP_XMIT_FLAGS_MASK XDP_XMIT_FLUSH
53
5ab073ff
JDB
54struct xdp_mem_info {
55 u32 type; /* enum xdp_mem_type, but known size type */
8d5d8852 56 u32 id;
5ab073ff
JDB
57};
58
57d0a1c1
JDB
59struct page_pool;
60
aecd67b6
JDB
61struct xdp_rxq_info {
62 struct net_device *dev;
63 u32 queue_index;
64 u32 reg_state;
5ab073ff 65 struct xdp_mem_info mem;
b02e5a0e 66 unsigned int napi_id;
bf25146a 67 u32 frag_size;
aecd67b6
JDB
68} ____cacheline_aligned; /* perf critical, avoid false-sharing */
69
64b59025
DA
70struct xdp_txq_info {
71 struct net_device *dev;
72};
73
2e88d4ff 74enum xdp_buff_flags {
d65a1906
LB
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 */
2e88d4ff
LB
79};
80
106ca27f
JDB
81struct xdp_buff {
82 void *data;
83 void *data_end;
84 void *data_meta;
85 void *data_hard_start;
86 struct xdp_rxq_info *rxq;
64b59025 87 struct xdp_txq_info *txq;
f95f0f95 88 u32 frame_sz; /* frame size to deduce data_hard_end/reserved tailroom*/
2e88d4ff 89 u32 flags; /* supported values defined in xdp_buff_flags */
106ca27f 90};
5ab073ff 91
2e88d4ff
LB
92static __always_inline bool xdp_buff_has_frags(struct xdp_buff *xdp)
93{
94 return !!(xdp->flags & XDP_FLAGS_HAS_FRAGS);
95}
96
97static __always_inline void xdp_buff_set_frags_flag(struct xdp_buff *xdp)
98{
99 xdp->flags |= XDP_FLAGS_HAS_FRAGS;
100}
101
102static __always_inline void xdp_buff_clear_frags_flag(struct xdp_buff *xdp)
103{
104 xdp->flags &= ~XDP_FLAGS_HAS_FRAGS;
105}
106
d65a1906
LB
107static __always_inline bool xdp_buff_is_frag_pfmemalloc(struct xdp_buff *xdp)
108{
109 return !!(xdp->flags & XDP_FLAGS_FRAGS_PF_MEMALLOC);
110}
111
112static __always_inline void xdp_buff_set_frag_pfmemalloc(struct xdp_buff *xdp)
113{
114 xdp->flags |= XDP_FLAGS_FRAGS_PF_MEMALLOC;
115}
116
43b5169d
LB
117static __always_inline void
118xdp_init_buff(struct xdp_buff *xdp, u32 frame_sz, struct xdp_rxq_info *rxq)
119{
120 xdp->frame_sz = frame_sz;
121 xdp->rxq = rxq;
2e88d4ff 122 xdp->flags = 0;
43b5169d
LB
123}
124
be9df4af
LB
125static __always_inline void
126xdp_prepare_buff(struct xdp_buff *xdp, unsigned char *hard_start,
127 int headroom, int data_len, const bool meta_valid)
128{
129 unsigned char *data = hard_start + headroom;
130
131 xdp->data_hard_start = hard_start;
132 xdp->data = data;
133 xdp->data_end = data + data_len;
134 xdp->data_meta = meta_valid ? data : data + 1;
135}
136
f95f0f95
JDB
137/* Reserve memory area at end-of data area.
138 *
139 * This macro reserves tailroom in the XDP buffer by limiting the
140 * XDP/BPF data access to data_hard_end. Notice same area (and size)
141 * is used for XDP_PASS, when constructing the SKB via build_skb().
142 */
143#define xdp_data_hard_end(xdp) \
144 ((xdp)->data_hard_start + (xdp)->frame_sz - \
145 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
146
2f0bc54b
LB
147static inline struct skb_shared_info *
148xdp_get_shared_info_from_buff(struct xdp_buff *xdp)
149{
150 return (struct skb_shared_info *)xdp_data_hard_end(xdp);
151}
152
0165cc81
LB
153static __always_inline unsigned int xdp_get_buff_len(struct xdp_buff *xdp)
154{
155 unsigned int len = xdp->data_end - xdp->data;
156 struct skb_shared_info *sinfo;
157
158 if (likely(!xdp_buff_has_frags(xdp)))
159 goto out;
160
161 sinfo = xdp_get_shared_info_from_buff(xdp);
162 len += sinfo->xdp_frags_size;
163out:
164 return len;
165}
166
c0048cff
JDB
167struct xdp_frame {
168 void *data;
169 u16 len;
170 u16 headroom;
b860a1b9 171 u32 metasize; /* uses lower 8-bits */
c0048cff
JDB
172 /* Lifetime of xdp_rxq_info is limited to NAPI/enqueue time,
173 * while mem info is valid on remote CPU.
174 */
175 struct xdp_mem_info mem;
70280ed9 176 struct net_device *dev_rx; /* used by cpumap */
b860a1b9 177 u32 frame_sz;
2e88d4ff 178 u32 flags; /* supported values defined in xdp_buff_flags */
c0048cff
JDB
179};
180
2e88d4ff
LB
181static __always_inline bool xdp_frame_has_frags(struct xdp_frame *frame)
182{
183 return !!(frame->flags & XDP_FLAGS_HAS_FRAGS);
184}
185
d65a1906
LB
186static __always_inline bool xdp_frame_is_frag_pfmemalloc(struct xdp_frame *frame)
187{
188 return !!(frame->flags & XDP_FLAGS_FRAGS_PF_MEMALLOC);
189}
190
89653987
LB
191#define XDP_BULK_QUEUE_SIZE 16
192struct xdp_frame_bulk {
193 int count;
194 void *xa;
195 void *q[XDP_BULK_QUEUE_SIZE];
196};
197
198static __always_inline void xdp_frame_bulk_init(struct xdp_frame_bulk *bq)
199{
200 /* bq->count will be zero'ed when bq->xa gets updated */
201 bq->xa = NULL;
202}
dee72f8a 203
2f0bc54b
LB
204static inline struct skb_shared_info *
205xdp_get_shared_info_from_frame(struct xdp_frame *frame)
206{
207 void *data_hard_start = frame->data - frame->headroom - sizeof(*frame);
208
209 return (struct skb_shared_info *)(data_hard_start + frame->frame_sz -
210 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
211}
212
92164774 213struct xdp_cpumap_stats {
28b1520e 214 unsigned int redirect;
92164774
LB
215 unsigned int pass;
216 unsigned int drop;
217};
218
a8d5b4ab
TM
219/* Clear kernel pointers in xdp_frame */
220static inline void xdp_scrub_frame(struct xdp_frame *frame)
221{
222 frame->data = NULL;
223 frame->dev_rx = NULL;
224}
225
d65a1906
LB
226static inline void
227xdp_update_skb_shared_info(struct sk_buff *skb, u8 nr_frags,
228 unsigned int size, unsigned int truesize,
229 bool pfmemalloc)
230{
231 skb_shinfo(skb)->nr_frags = nr_frags;
232
233 skb->len += size;
234 skb->data_len += size;
235 skb->truesize += truesize;
236 skb->pfmemalloc |= pfmemalloc;
237}
238
34cc0b33
JDB
239/* Avoids inlining WARN macro in fast-path */
240void xdp_warn(const char *msg, const char *func, const int line);
241#define XDP_WARN(msg) xdp_warn(msg, __func__, __LINE__)
242
b0d1beef 243struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp);
97a0e1ea
LB
244struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
245 struct sk_buff *skb,
246 struct net_device *dev);
89f479f0
LB
247struct sk_buff *xdp_build_skb_from_frame(struct xdp_frame *xdpf,
248 struct net_device *dev);
65e6dcf7 249int xdp_alloc_skb_bulk(void **skbs, int n_skb, gfp_t gfp);
e624d4ed 250struct xdp_frame *xdpf_clone(struct xdp_frame *xdpf);
b0d1beef 251
fc379872
LB
252static inline
253void xdp_convert_frame_to_buff(struct xdp_frame *frame, struct xdp_buff *xdp)
254{
255 xdp->data_hard_start = frame->data - frame->headroom - sizeof(*frame);
256 xdp->data = frame->data;
257 xdp->data_end = frame->data + frame->len;
258 xdp->data_meta = frame->data - frame->metasize;
259 xdp->frame_sz = frame->frame_sz;
2e88d4ff 260 xdp->flags = frame->flags;
fc379872
LB
261}
262
c0048cff 263static inline
daa5cdc3
DA
264int xdp_update_frame_from_buff(struct xdp_buff *xdp,
265 struct xdp_frame *xdp_frame)
c0048cff 266{
daa5cdc3 267 int metasize, headroom;
02b55e56 268
c0048cff
JDB
269 /* Assure headroom is available for storing info */
270 headroom = xdp->data - xdp->data_hard_start;
271 metasize = xdp->data - xdp->data_meta;
272 metasize = metasize > 0 ? metasize : 0;
273 if (unlikely((headroom - metasize) < sizeof(*xdp_frame)))
daa5cdc3 274 return -ENOSPC;
c0048cff 275
34cc0b33
JDB
276 /* Catch if driver didn't reserve tailroom for skb_shared_info */
277 if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) {
278 XDP_WARN("Driver BUG: missing reserved tailroom");
daa5cdc3 279 return -ENOSPC;
34cc0b33
JDB
280 }
281
c0048cff
JDB
282 xdp_frame->data = xdp->data;
283 xdp_frame->len = xdp->data_end - xdp->data;
284 xdp_frame->headroom = headroom - sizeof(*xdp_frame);
285 xdp_frame->metasize = metasize;
34cc0b33 286 xdp_frame->frame_sz = xdp->frame_sz;
2e88d4ff 287 xdp_frame->flags = xdp->flags;
c0048cff 288
daa5cdc3
DA
289 return 0;
290}
291
292/* Convert xdp_buff to xdp_frame */
293static inline
294struct xdp_frame *xdp_convert_buff_to_frame(struct xdp_buff *xdp)
295{
296 struct xdp_frame *xdp_frame;
297
298 if (xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL)
299 return xdp_convert_zc_to_xdp_frame(xdp);
300
301 /* Store info in top of packet */
302 xdp_frame = xdp->data_hard_start;
303 if (unlikely(xdp_update_frame_from_buff(xdp, xdp_frame) < 0))
304 return NULL;
305
c0048cff
JDB
306 /* rxq only valid until napi_schedule ends, convert to xdp_mem_info */
307 xdp_frame->mem = xdp->rxq->mem;
308
309 return xdp_frame;
310}
311
bf25146a
EC
312void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
313 struct xdp_buff *xdp);
03993094 314void xdp_return_frame(struct xdp_frame *xdpf);
389ab7f0 315void xdp_return_frame_rx_napi(struct xdp_frame *xdpf);
c497176c 316void xdp_return_buff(struct xdp_buff *xdp);
89653987
LB
317void xdp_flush_frame_bulk(struct xdp_frame_bulk *bq);
318void xdp_return_frame_bulk(struct xdp_frame *xdpf,
319 struct xdp_frame_bulk *bq);
5ab073ff 320
5142239a
LB
321static __always_inline unsigned int xdp_get_frame_len(struct xdp_frame *xdpf)
322{
323 struct skb_shared_info *sinfo;
324 unsigned int len = xdpf->len;
325
326 if (likely(!xdp_frame_has_frags(xdpf)))
327 goto out;
328
329 sinfo = xdp_get_shared_info_from_frame(xdpf);
330 len += sinfo->xdp_frags_size;
331out:
332 return len;
333}
334
bf25146a
EC
335int __xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
336 struct net_device *dev, u32 queue_index,
337 unsigned int napi_id, u32 frag_size);
338static inline int
339xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
340 struct net_device *dev, u32 queue_index,
341 unsigned int napi_id)
342{
343 return __xdp_rxq_info_reg(xdp_rxq, dev, queue_index, napi_id, 0);
344}
345
aecd67b6
JDB
346void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq);
347void xdp_rxq_info_unused(struct xdp_rxq_info *xdp_rxq);
c0124f32 348bool xdp_rxq_info_is_reg(struct xdp_rxq_info *xdp_rxq);
5ab073ff
JDB
349int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
350 enum xdp_mem_type type, void *allocator);
dce5bd61 351void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq);
4a48ef70
THJ
352int xdp_reg_mem_model(struct xdp_mem_info *mem,
353 enum xdp_mem_type type, void *allocator);
354void xdp_unreg_mem_model(struct xdp_mem_info *mem);
aecd67b6 355
106ca27f
JDB
356/* Drivers not supporting XDP metadata can use this helper, which
357 * rejects any room expansion for metadata as a result.
358 */
359static __always_inline void
360xdp_set_data_meta_invalid(struct xdp_buff *xdp)
361{
362 xdp->data_meta = xdp->data + 1;
363}
364
365static __always_inline bool
366xdp_data_meta_unsupported(const struct xdp_buff *xdp)
367{
368 return unlikely(xdp->data_meta > xdp->data);
369}
370
7445cf31
ZE
371static inline bool xdp_metalen_invalid(unsigned long metalen)
372{
373 return (metalen & (sizeof(__u32) - 1)) || (metalen > 32);
374}
375
05296620
JK
376struct xdp_attachment_info {
377 struct bpf_prog *prog;
378 u32 flags;
379};
380
381struct netdev_bpf;
05296620
JK
382void xdp_attachment_setup(struct xdp_attachment_info *info,
383 struct netdev_bpf *bpf);
384
89653987 385#define DEV_MAP_BULK_SIZE XDP_BULK_QUEUE_SIZE
788f87ac 386
3d76a4d3
SF
387#define XDP_METADATA_KFUNC_xxx \
388 XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_TIMESTAMP, \
389 bpf_xdp_metadata_rx_timestamp) \
390 XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_HASH, \
391 bpf_xdp_metadata_rx_hash) \
392
393enum {
394#define XDP_METADATA_KFUNC(name, _) name,
395XDP_METADATA_KFUNC_xxx
396#undef XDP_METADATA_KFUNC
397MAX_XDP_METADATA_KFUNC,
398};
399
0cd917a4
JDB
400enum xdp_rss_hash_type {
401 /* First part: Individual bits for L3/L4 types */
402 XDP_RSS_L3_IPV4 = BIT(0),
403 XDP_RSS_L3_IPV6 = BIT(1),
404
405 /* The fixed (L3) IPv4 and IPv6 headers can both be followed by
406 * variable/dynamic headers, IPv4 called Options and IPv6 called
407 * Extension Headers. HW RSS type can contain this info.
408 */
409 XDP_RSS_L3_DYNHDR = BIT(2),
410
411 /* When RSS hash covers L4 then drivers MUST set XDP_RSS_L4 bit in
412 * addition to the protocol specific bit. This ease interaction with
413 * SKBs and avoids reserving a fixed mask for future L4 protocol bits.
414 */
415 XDP_RSS_L4 = BIT(3), /* L4 based hash, proto can be unknown */
416 XDP_RSS_L4_TCP = BIT(4),
417 XDP_RSS_L4_UDP = BIT(5),
418 XDP_RSS_L4_SCTP = BIT(6),
419 XDP_RSS_L4_IPSEC = BIT(7), /* L4 based hash include IPSEC SPI */
420
421 /* Second part: RSS hash type combinations used for driver HW mapping */
422 XDP_RSS_TYPE_NONE = 0,
423 XDP_RSS_TYPE_L2 = XDP_RSS_TYPE_NONE,
424
425 XDP_RSS_TYPE_L3_IPV4 = XDP_RSS_L3_IPV4,
426 XDP_RSS_TYPE_L3_IPV6 = XDP_RSS_L3_IPV6,
427 XDP_RSS_TYPE_L3_IPV4_OPT = XDP_RSS_L3_IPV4 | XDP_RSS_L3_DYNHDR,
428 XDP_RSS_TYPE_L3_IPV6_EX = XDP_RSS_L3_IPV6 | XDP_RSS_L3_DYNHDR,
429
430 XDP_RSS_TYPE_L4_ANY = XDP_RSS_L4,
431 XDP_RSS_TYPE_L4_IPV4_TCP = XDP_RSS_L3_IPV4 | XDP_RSS_L4 | XDP_RSS_L4_TCP,
432 XDP_RSS_TYPE_L4_IPV4_UDP = XDP_RSS_L3_IPV4 | XDP_RSS_L4 | XDP_RSS_L4_UDP,
433 XDP_RSS_TYPE_L4_IPV4_SCTP = XDP_RSS_L3_IPV4 | XDP_RSS_L4 | XDP_RSS_L4_SCTP,
67f245c2 434 XDP_RSS_TYPE_L4_IPV4_IPSEC = XDP_RSS_L3_IPV4 | XDP_RSS_L4 | XDP_RSS_L4_IPSEC,
0cd917a4
JDB
435
436 XDP_RSS_TYPE_L4_IPV6_TCP = XDP_RSS_L3_IPV6 | XDP_RSS_L4 | XDP_RSS_L4_TCP,
437 XDP_RSS_TYPE_L4_IPV6_UDP = XDP_RSS_L3_IPV6 | XDP_RSS_L4 | XDP_RSS_L4_UDP,
438 XDP_RSS_TYPE_L4_IPV6_SCTP = XDP_RSS_L3_IPV6 | XDP_RSS_L4 | XDP_RSS_L4_SCTP,
67f245c2 439 XDP_RSS_TYPE_L4_IPV6_IPSEC = XDP_RSS_L3_IPV6 | XDP_RSS_L4 | XDP_RSS_L4_IPSEC,
0cd917a4
JDB
440
441 XDP_RSS_TYPE_L4_IPV6_TCP_EX = XDP_RSS_TYPE_L4_IPV6_TCP | XDP_RSS_L3_DYNHDR,
442 XDP_RSS_TYPE_L4_IPV6_UDP_EX = XDP_RSS_TYPE_L4_IPV6_UDP | XDP_RSS_L3_DYNHDR,
443 XDP_RSS_TYPE_L4_IPV6_SCTP_EX = XDP_RSS_TYPE_L4_IPV6_SCTP | XDP_RSS_L3_DYNHDR,
444};
445
3d76a4d3
SF
446#ifdef CONFIG_NET
447u32 bpf_xdp_metadata_kfunc_id(int id);
448bool bpf_dev_bound_kfunc_id(u32 btf_id);
f85949f9 449void xdp_set_features_flag(struct net_device *dev, xdp_features_t val);
66c0e13a
MM
450void xdp_features_set_redirect_target(struct net_device *dev, bool support_sg);
451void xdp_features_clear_redirect_target(struct net_device *dev);
3d76a4d3
SF
452#else
453static inline u32 bpf_xdp_metadata_kfunc_id(int id) { return 0; }
454static inline bool bpf_dev_bound_kfunc_id(u32 btf_id) { return false; }
66c0e13a 455
f85949f9
LB
456static inline void
457xdp_set_features_flag(struct net_device *dev, xdp_features_t val)
458{
459}
460
66c0e13a
MM
461static inline void
462xdp_features_set_redirect_target(struct net_device *dev, bool support_sg)
463{
464}
465
466static inline void
467xdp_features_clear_redirect_target(struct net_device *dev)
468{
469}
3d76a4d3
SF
470#endif
471
f85949f9
LB
472static inline void xdp_clear_features_flag(struct net_device *dev)
473{
474 xdp_set_features_flag(dev, 0);
475}
476
aecd67b6 477#endif /* __LINUX_NET_XDP_H__ */