Loading...
Note: File does not exist in v3.1.
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/**
15 * DOC: XDP RX-queue information
16 *
17 * The XDP RX-queue info (xdp_rxq_info) is associated with the driver
18 * level RX-ring queues. It is information that is specific to how
19 * the driver has configured a given RX-ring queue.
20 *
21 * Each xdp_buff frame received in the driver carries a (pointer)
22 * reference to this xdp_rxq_info structure. This provides the XDP
23 * data-path read-access to RX-info for both kernel and bpf-side
24 * (limited subset).
25 *
26 * For now, direct access is only safe while running in NAPI/softirq
27 * context. Contents are read-mostly and must not be updated during
28 * driver NAPI/softirq poll.
29 *
30 * The driver usage API is a register and unregister API.
31 *
32 * The struct is not directly tied to the XDP prog. A new XDP prog
33 * can be attached as long as it doesn't change the underlying
34 * RX-ring. If the RX-ring does change significantly, the NIC driver
35 * naturally needs to stop the RX-ring before purging and reallocating
36 * memory. In that process the driver MUST call unregister (which
37 * also applies for driver shutdown and unload). The register API is
38 * also mandatory during RX-ring setup.
39 */
40
41enum xdp_mem_type {
42 MEM_TYPE_PAGE_SHARED = 0, /* Split-page refcnt based model */
43 MEM_TYPE_PAGE_ORDER0, /* Orig XDP full page model */
44 MEM_TYPE_PAGE_POOL,
45 MEM_TYPE_XSK_BUFF_POOL,
46 MEM_TYPE_MAX,
47};
48
49/* XDP flags for ndo_xdp_xmit */
50#define XDP_XMIT_FLUSH (1U << 0) /* doorbell signal consumer */
51#define XDP_XMIT_FLAGS_MASK XDP_XMIT_FLUSH
52
53struct xdp_mem_info {
54 u32 type; /* enum xdp_mem_type, but known size type */
55 u32 id;
56};
57
58struct page_pool;
59
60struct xdp_rxq_info {
61 struct net_device *dev;
62 u32 queue_index;
63 u32 reg_state;
64 struct xdp_mem_info mem;
65 u32 frag_size;
66} ____cacheline_aligned; /* perf critical, avoid false-sharing */
67
68struct xdp_txq_info {
69 struct net_device *dev;
70};
71
72enum xdp_buff_flags {
73 XDP_FLAGS_HAS_FRAGS = BIT(0), /* non-linear xdp buff */
74 XDP_FLAGS_FRAGS_PF_MEMALLOC = BIT(1), /* xdp paged memory is under
75 * pressure
76 */
77};
78
79struct xdp_buff {
80 void *data;
81 void *data_end;
82 void *data_meta;
83 void *data_hard_start;
84 struct xdp_rxq_info *rxq;
85 struct xdp_txq_info *txq;
86 u32 frame_sz; /* frame size to deduce data_hard_end/reserved tailroom*/
87 u32 flags; /* supported values defined in xdp_buff_flags */
88};
89
90static __always_inline bool xdp_buff_has_frags(struct xdp_buff *xdp)
91{
92 return !!(xdp->flags & XDP_FLAGS_HAS_FRAGS);
93}
94
95static __always_inline void xdp_buff_set_frags_flag(struct xdp_buff *xdp)
96{
97 xdp->flags |= XDP_FLAGS_HAS_FRAGS;
98}
99
100static __always_inline void xdp_buff_clear_frags_flag(struct xdp_buff *xdp)
101{
102 xdp->flags &= ~XDP_FLAGS_HAS_FRAGS;
103}
104
105static __always_inline bool xdp_buff_is_frag_pfmemalloc(struct xdp_buff *xdp)
106{
107 return !!(xdp->flags & XDP_FLAGS_FRAGS_PF_MEMALLOC);
108}
109
110static __always_inline void xdp_buff_set_frag_pfmemalloc(struct xdp_buff *xdp)
111{
112 xdp->flags |= XDP_FLAGS_FRAGS_PF_MEMALLOC;
113}
114
115static __always_inline void
116xdp_init_buff(struct xdp_buff *xdp, u32 frame_sz, struct xdp_rxq_info *rxq)
117{
118 xdp->frame_sz = frame_sz;
119 xdp->rxq = rxq;
120 xdp->flags = 0;
121}
122
123static __always_inline void
124xdp_prepare_buff(struct xdp_buff *xdp, unsigned char *hard_start,
125 int headroom, int data_len, const bool meta_valid)
126{
127 unsigned char *data = hard_start + headroom;
128
129 xdp->data_hard_start = hard_start;
130 xdp->data = data;
131 xdp->data_end = data + data_len;
132 xdp->data_meta = meta_valid ? data : data + 1;
133}
134
135/* Reserve memory area at end-of data area.
136 *
137 * This macro reserves tailroom in the XDP buffer by limiting the
138 * XDP/BPF data access to data_hard_end. Notice same area (and size)
139 * is used for XDP_PASS, when constructing the SKB via build_skb().
140 */
141#define xdp_data_hard_end(xdp) \
142 ((xdp)->data_hard_start + (xdp)->frame_sz - \
143 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
144
145static inline struct skb_shared_info *
146xdp_get_shared_info_from_buff(struct xdp_buff *xdp)
147{
148 return (struct skb_shared_info *)xdp_data_hard_end(xdp);
149}
150
151static __always_inline unsigned int xdp_get_buff_len(struct xdp_buff *xdp)
152{
153 unsigned int len = xdp->data_end - xdp->data;
154 struct skb_shared_info *sinfo;
155
156 if (likely(!xdp_buff_has_frags(xdp)))
157 goto out;
158
159 sinfo = xdp_get_shared_info_from_buff(xdp);
160 len += sinfo->xdp_frags_size;
161out:
162 return len;
163}
164
165struct xdp_frame {
166 void *data;
167 u16 len;
168 u16 headroom;
169 u32 metasize; /* uses lower 8-bits */
170 /* Lifetime of xdp_rxq_info is limited to NAPI/enqueue time,
171 * while mem info is valid on remote CPU.
172 */
173 struct xdp_mem_info mem;
174 struct net_device *dev_rx; /* used by cpumap */
175 u32 frame_sz;
176 u32 flags; /* supported values defined in xdp_buff_flags */
177};
178
179static __always_inline bool xdp_frame_has_frags(struct xdp_frame *frame)
180{
181 return !!(frame->flags & XDP_FLAGS_HAS_FRAGS);
182}
183
184static __always_inline bool xdp_frame_is_frag_pfmemalloc(struct xdp_frame *frame)
185{
186 return !!(frame->flags & XDP_FLAGS_FRAGS_PF_MEMALLOC);
187}
188
189#define XDP_BULK_QUEUE_SIZE 16
190struct xdp_frame_bulk {
191 int count;
192 void *xa;
193 void *q[XDP_BULK_QUEUE_SIZE];
194};
195
196static __always_inline void xdp_frame_bulk_init(struct xdp_frame_bulk *bq)
197{
198 /* bq->count will be zero'ed when bq->xa gets updated */
199 bq->xa = NULL;
200}
201
202static inline struct skb_shared_info *
203xdp_get_shared_info_from_frame(struct xdp_frame *frame)
204{
205 void *data_hard_start = frame->data - frame->headroom - sizeof(*frame);
206
207 return (struct skb_shared_info *)(data_hard_start + frame->frame_sz -
208 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
209}
210
211struct xdp_cpumap_stats {
212 unsigned int redirect;
213 unsigned int pass;
214 unsigned int drop;
215};
216
217/* Clear kernel pointers in xdp_frame */
218static inline void xdp_scrub_frame(struct xdp_frame *frame)
219{
220 frame->data = NULL;
221 frame->dev_rx = NULL;
222}
223
224static inline void
225xdp_update_skb_shared_info(struct sk_buff *skb, u8 nr_frags,
226 unsigned int size, unsigned int truesize,
227 bool pfmemalloc)
228{
229 skb_shinfo(skb)->nr_frags = nr_frags;
230
231 skb->len += size;
232 skb->data_len += size;
233 skb->truesize += truesize;
234 skb->pfmemalloc |= pfmemalloc;
235}
236
237/* Avoids inlining WARN macro in fast-path */
238void xdp_warn(const char *msg, const char *func, const int line);
239#define XDP_WARN(msg) xdp_warn(msg, __func__, __LINE__)
240
241struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp);
242struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
243 struct sk_buff *skb,
244 struct net_device *dev);
245struct sk_buff *xdp_build_skb_from_frame(struct xdp_frame *xdpf,
246 struct net_device *dev);
247int xdp_alloc_skb_bulk(void **skbs, int n_skb, gfp_t gfp);
248struct xdp_frame *xdpf_clone(struct xdp_frame *xdpf);
249
250static inline
251void xdp_convert_frame_to_buff(struct xdp_frame *frame, struct xdp_buff *xdp)
252{
253 xdp->data_hard_start = frame->data - frame->headroom - sizeof(*frame);
254 xdp->data = frame->data;
255 xdp->data_end = frame->data + frame->len;
256 xdp->data_meta = frame->data - frame->metasize;
257 xdp->frame_sz = frame->frame_sz;
258 xdp->flags = frame->flags;
259}
260
261static inline
262int xdp_update_frame_from_buff(struct xdp_buff *xdp,
263 struct xdp_frame *xdp_frame)
264{
265 int metasize, headroom;
266
267 /* Assure headroom is available for storing info */
268 headroom = xdp->data - xdp->data_hard_start;
269 metasize = xdp->data - xdp->data_meta;
270 metasize = metasize > 0 ? metasize : 0;
271 if (unlikely((headroom - metasize) < sizeof(*xdp_frame)))
272 return -ENOSPC;
273
274 /* Catch if driver didn't reserve tailroom for skb_shared_info */
275 if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) {
276 XDP_WARN("Driver BUG: missing reserved tailroom");
277 return -ENOSPC;
278 }
279
280 xdp_frame->data = xdp->data;
281 xdp_frame->len = xdp->data_end - xdp->data;
282 xdp_frame->headroom = headroom - sizeof(*xdp_frame);
283 xdp_frame->metasize = metasize;
284 xdp_frame->frame_sz = xdp->frame_sz;
285 xdp_frame->flags = xdp->flags;
286
287 return 0;
288}
289
290/* Convert xdp_buff to xdp_frame */
291static inline
292struct xdp_frame *xdp_convert_buff_to_frame(struct xdp_buff *xdp)
293{
294 struct xdp_frame *xdp_frame;
295
296 if (xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL)
297 return xdp_convert_zc_to_xdp_frame(xdp);
298
299 /* Store info in top of packet */
300 xdp_frame = xdp->data_hard_start;
301 if (unlikely(xdp_update_frame_from_buff(xdp, xdp_frame) < 0))
302 return NULL;
303
304 /* rxq only valid until napi_schedule ends, convert to xdp_mem_info */
305 xdp_frame->mem = xdp->rxq->mem;
306
307 return xdp_frame;
308}
309
310void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
311 struct xdp_buff *xdp);
312void xdp_return_frame(struct xdp_frame *xdpf);
313void xdp_return_frame_rx_napi(struct xdp_frame *xdpf);
314void xdp_return_buff(struct xdp_buff *xdp);
315void xdp_flush_frame_bulk(struct xdp_frame_bulk *bq);
316void xdp_return_frame_bulk(struct xdp_frame *xdpf,
317 struct xdp_frame_bulk *bq);
318
319static __always_inline unsigned int xdp_get_frame_len(struct xdp_frame *xdpf)
320{
321 struct skb_shared_info *sinfo;
322 unsigned int len = xdpf->len;
323
324 if (likely(!xdp_frame_has_frags(xdpf)))
325 goto out;
326
327 sinfo = xdp_get_shared_info_from_frame(xdpf);
328 len += sinfo->xdp_frags_size;
329out:
330 return len;
331}
332
333int __xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
334 struct net_device *dev, u32 queue_index,
335 unsigned int napi_id, u32 frag_size);
336static inline int
337xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
338 struct net_device *dev, u32 queue_index,
339 unsigned int napi_id)
340{
341 return __xdp_rxq_info_reg(xdp_rxq, dev, queue_index, napi_id, 0);
342}
343
344void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq);
345void xdp_rxq_info_unused(struct xdp_rxq_info *xdp_rxq);
346bool xdp_rxq_info_is_reg(struct xdp_rxq_info *xdp_rxq);
347int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
348 enum xdp_mem_type type, void *allocator);
349void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq);
350int xdp_reg_mem_model(struct xdp_mem_info *mem,
351 enum xdp_mem_type type, void *allocator);
352void xdp_unreg_mem_model(struct xdp_mem_info *mem);
353
354/* Drivers not supporting XDP metadata can use this helper, which
355 * rejects any room expansion for metadata as a result.
356 */
357static __always_inline void
358xdp_set_data_meta_invalid(struct xdp_buff *xdp)
359{
360 xdp->data_meta = xdp->data + 1;
361}
362
363static __always_inline bool
364xdp_data_meta_unsupported(const struct xdp_buff *xdp)
365{
366 return unlikely(xdp->data_meta > xdp->data);
367}
368
369static inline bool xdp_metalen_invalid(unsigned long metalen)
370{
371 unsigned long meta_max;
372
373 meta_max = type_max(typeof_member(struct skb_shared_info, meta_len));
374 BUILD_BUG_ON(!__builtin_constant_p(meta_max));
375
376 return !IS_ALIGNED(metalen, sizeof(u32)) || metalen > meta_max;
377}
378
379struct xdp_attachment_info {
380 struct bpf_prog *prog;
381 u32 flags;
382};
383
384struct netdev_bpf;
385void xdp_attachment_setup(struct xdp_attachment_info *info,
386 struct netdev_bpf *bpf);
387
388#define DEV_MAP_BULK_SIZE XDP_BULK_QUEUE_SIZE
389
390/* Define the relationship between xdp-rx-metadata kfunc and
391 * various other entities:
392 * - xdp_rx_metadata enum
393 * - netdev netlink enum (Documentation/netlink/specs/netdev.yaml)
394 * - kfunc name
395 * - xdp_metadata_ops field
396 */
397#define XDP_METADATA_KFUNC_xxx \
398 XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_TIMESTAMP, \
399 NETDEV_XDP_RX_METADATA_TIMESTAMP, \
400 bpf_xdp_metadata_rx_timestamp, \
401 xmo_rx_timestamp) \
402 XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_HASH, \
403 NETDEV_XDP_RX_METADATA_HASH, \
404 bpf_xdp_metadata_rx_hash, \
405 xmo_rx_hash) \
406 XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_VLAN_TAG, \
407 NETDEV_XDP_RX_METADATA_VLAN_TAG, \
408 bpf_xdp_metadata_rx_vlan_tag, \
409 xmo_rx_vlan_tag) \
410
411enum xdp_rx_metadata {
412#define XDP_METADATA_KFUNC(name, _, __, ___) name,
413XDP_METADATA_KFUNC_xxx
414#undef XDP_METADATA_KFUNC
415MAX_XDP_METADATA_KFUNC,
416};
417
418enum xdp_rss_hash_type {
419 /* First part: Individual bits for L3/L4 types */
420 XDP_RSS_L3_IPV4 = BIT(0),
421 XDP_RSS_L3_IPV6 = BIT(1),
422
423 /* The fixed (L3) IPv4 and IPv6 headers can both be followed by
424 * variable/dynamic headers, IPv4 called Options and IPv6 called
425 * Extension Headers. HW RSS type can contain this info.
426 */
427 XDP_RSS_L3_DYNHDR = BIT(2),
428
429 /* When RSS hash covers L4 then drivers MUST set XDP_RSS_L4 bit in
430 * addition to the protocol specific bit. This ease interaction with
431 * SKBs and avoids reserving a fixed mask for future L4 protocol bits.
432 */
433 XDP_RSS_L4 = BIT(3), /* L4 based hash, proto can be unknown */
434 XDP_RSS_L4_TCP = BIT(4),
435 XDP_RSS_L4_UDP = BIT(5),
436 XDP_RSS_L4_SCTP = BIT(6),
437 XDP_RSS_L4_IPSEC = BIT(7), /* L4 based hash include IPSEC SPI */
438 XDP_RSS_L4_ICMP = BIT(8),
439
440 /* Second part: RSS hash type combinations used for driver HW mapping */
441 XDP_RSS_TYPE_NONE = 0,
442 XDP_RSS_TYPE_L2 = XDP_RSS_TYPE_NONE,
443
444 XDP_RSS_TYPE_L3_IPV4 = XDP_RSS_L3_IPV4,
445 XDP_RSS_TYPE_L3_IPV6 = XDP_RSS_L3_IPV6,
446 XDP_RSS_TYPE_L3_IPV4_OPT = XDP_RSS_L3_IPV4 | XDP_RSS_L3_DYNHDR,
447 XDP_RSS_TYPE_L3_IPV6_EX = XDP_RSS_L3_IPV6 | XDP_RSS_L3_DYNHDR,
448
449 XDP_RSS_TYPE_L4_ANY = XDP_RSS_L4,
450 XDP_RSS_TYPE_L4_IPV4_TCP = XDP_RSS_L3_IPV4 | XDP_RSS_L4 | XDP_RSS_L4_TCP,
451 XDP_RSS_TYPE_L4_IPV4_UDP = XDP_RSS_L3_IPV4 | XDP_RSS_L4 | XDP_RSS_L4_UDP,
452 XDP_RSS_TYPE_L4_IPV4_SCTP = XDP_RSS_L3_IPV4 | XDP_RSS_L4 | XDP_RSS_L4_SCTP,
453 XDP_RSS_TYPE_L4_IPV4_IPSEC = XDP_RSS_L3_IPV4 | XDP_RSS_L4 | XDP_RSS_L4_IPSEC,
454 XDP_RSS_TYPE_L4_IPV4_ICMP = XDP_RSS_L3_IPV4 | XDP_RSS_L4 | XDP_RSS_L4_ICMP,
455
456 XDP_RSS_TYPE_L4_IPV6_TCP = XDP_RSS_L3_IPV6 | XDP_RSS_L4 | XDP_RSS_L4_TCP,
457 XDP_RSS_TYPE_L4_IPV6_UDP = XDP_RSS_L3_IPV6 | XDP_RSS_L4 | XDP_RSS_L4_UDP,
458 XDP_RSS_TYPE_L4_IPV6_SCTP = XDP_RSS_L3_IPV6 | XDP_RSS_L4 | XDP_RSS_L4_SCTP,
459 XDP_RSS_TYPE_L4_IPV6_IPSEC = XDP_RSS_L3_IPV6 | XDP_RSS_L4 | XDP_RSS_L4_IPSEC,
460 XDP_RSS_TYPE_L4_IPV6_ICMP = XDP_RSS_L3_IPV6 | XDP_RSS_L4 | XDP_RSS_L4_ICMP,
461
462 XDP_RSS_TYPE_L4_IPV6_TCP_EX = XDP_RSS_TYPE_L4_IPV6_TCP | XDP_RSS_L3_DYNHDR,
463 XDP_RSS_TYPE_L4_IPV6_UDP_EX = XDP_RSS_TYPE_L4_IPV6_UDP | XDP_RSS_L3_DYNHDR,
464 XDP_RSS_TYPE_L4_IPV6_SCTP_EX = XDP_RSS_TYPE_L4_IPV6_SCTP | XDP_RSS_L3_DYNHDR,
465};
466
467struct xdp_metadata_ops {
468 int (*xmo_rx_timestamp)(const struct xdp_md *ctx, u64 *timestamp);
469 int (*xmo_rx_hash)(const struct xdp_md *ctx, u32 *hash,
470 enum xdp_rss_hash_type *rss_type);
471 int (*xmo_rx_vlan_tag)(const struct xdp_md *ctx, __be16 *vlan_proto,
472 u16 *vlan_tci);
473};
474
475#ifdef CONFIG_NET
476u32 bpf_xdp_metadata_kfunc_id(int id);
477bool bpf_dev_bound_kfunc_id(u32 btf_id);
478void xdp_set_features_flag(struct net_device *dev, xdp_features_t val);
479void xdp_features_set_redirect_target(struct net_device *dev, bool support_sg);
480void xdp_features_clear_redirect_target(struct net_device *dev);
481#else
482static inline u32 bpf_xdp_metadata_kfunc_id(int id) { return 0; }
483static inline bool bpf_dev_bound_kfunc_id(u32 btf_id) { return false; }
484
485static inline void
486xdp_set_features_flag(struct net_device *dev, xdp_features_t val)
487{
488}
489
490static inline void
491xdp_features_set_redirect_target(struct net_device *dev, bool support_sg)
492{
493}
494
495static inline void
496xdp_features_clear_redirect_target(struct net_device *dev)
497{
498}
499#endif
500
501static inline void xdp_clear_features_flag(struct net_device *dev)
502{
503 xdp_set_features_flag(dev, 0);
504}
505
506static __always_inline u32 bpf_prog_run_xdp(const struct bpf_prog *prog,
507 struct xdp_buff *xdp)
508{
509 /* Driver XDP hooks are invoked within a single NAPI poll cycle and thus
510 * under local_bh_disable(), which provides the needed RCU protection
511 * for accessing map entries.
512 */
513 u32 act = __bpf_prog_run(prog, xdp, BPF_DISPATCHER_FUNC(xdp));
514
515 if (static_branch_unlikely(&bpf_master_redirect_enabled_key)) {
516 if (act == XDP_TX && netif_is_bond_slave(xdp->rxq->dev))
517 act = xdp_master_redirect(xdp);
518 }
519
520 return act;
521}
522#endif /* __LINUX_NET_XDP_H__ */