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