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