1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* Interface for implementing AF_XDP zero-copy support in drivers. 3 * Copyright(c) 2020 Intel Corporation. 4 */ 5 6 #ifndef _LINUX_XDP_SOCK_DRV_H 7 #define _LINUX_XDP_SOCK_DRV_H 8 9 #include <net/xdp_sock.h> 10 #include <net/xsk_buff_pool.h> 11 12 #define XDP_UMEM_MIN_CHUNK_SHIFT 11 13 #define XDP_UMEM_MIN_CHUNK_SIZE (1 << XDP_UMEM_MIN_CHUNK_SHIFT) 14 15 #define NETDEV_XDP_ACT_XSK (NETDEV_XDP_ACT_BASIC | \ 16 NETDEV_XDP_ACT_REDIRECT | \ 17 NETDEV_XDP_ACT_XSK_ZEROCOPY) 18 19 struct xsk_cb_desc { 20 void *src; 21 u8 off; 22 u8 bytes; 23 }; 24 25 #ifdef CONFIG_XDP_SOCKETS 26 27 void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries); 28 bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc); 29 u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max); 30 void xsk_tx_release(struct xsk_buff_pool *pool); 31 struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev, 32 u16 queue_id); 33 void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool); 34 void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool); 35 void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool); 36 void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool); 37 bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool); 38 39 static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool) 40 { 41 return XDP_PACKET_HEADROOM + pool->headroom; 42 } 43 44 static inline u32 xsk_pool_get_tailroom(bool mbuf) 45 { 46 return mbuf ? SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) : 0; 47 } 48 49 static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool) 50 { 51 return pool->chunk_size; 52 } 53 54 static inline u32 __xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool) 55 { 56 return xsk_pool_get_chunk_size(pool) - xsk_pool_get_headroom(pool); 57 } 58 59 static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool) 60 { 61 u32 frame_size = __xsk_pool_get_rx_frame_size(pool); 62 struct xdp_umem *umem = pool->umem; 63 bool mbuf; 64 65 /* Reserve tailroom only for zero-copy pools that opted into 66 * multi-buffer. The reserved area is used for skb_shared_info, 67 * matching the XDP core's xdp_data_hard_end() layout. 68 */ 69 mbuf = pool->dev && (umem->flags & XDP_UMEM_SG_FLAG); 70 frame_size -= xsk_pool_get_tailroom(mbuf); 71 72 return ALIGN_DOWN(frame_size, 128); 73 } 74 75 static inline u32 xsk_pool_get_rx_frag_step(struct xsk_buff_pool *pool) 76 { 77 return pool->unaligned ? 0 : xsk_pool_get_chunk_size(pool); 78 } 79 80 static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool, 81 struct xdp_rxq_info *rxq) 82 { 83 xp_set_rxq_info(pool, rxq); 84 } 85 86 static inline void xsk_pool_fill_cb(struct xsk_buff_pool *pool, 87 struct xsk_cb_desc *desc) 88 { 89 xp_fill_cb(pool, desc); 90 } 91 92 static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool, 93 unsigned long attrs) 94 { 95 xp_dma_unmap(pool, attrs); 96 } 97 98 static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool, 99 struct device *dev, unsigned long attrs) 100 { 101 struct xdp_umem *umem = pool->umem; 102 103 return xp_dma_map(pool, dev, attrs, umem->pgs, umem->npgs); 104 } 105 106 static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp) 107 { 108 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp); 109 110 return xp_get_dma(xskb); 111 } 112 113 static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp) 114 { 115 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp); 116 117 return xp_get_frame_dma(xskb); 118 } 119 120 static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool) 121 { 122 return xp_alloc(pool); 123 } 124 125 static inline bool xsk_is_eop_desc(const struct xdp_desc *desc) 126 { 127 return !xp_mb_desc(desc); 128 } 129 130 /* Returns as many entries as possible up to max. 0 <= N <= max. */ 131 static inline u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max) 132 { 133 return xp_alloc_batch(pool, xdp, max); 134 } 135 136 static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count) 137 { 138 return xp_can_alloc(pool, count); 139 } 140 141 static inline void xsk_buff_free(struct xdp_buff *xdp) 142 { 143 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp); 144 struct list_head *xskb_list = &xskb->pool->xskb_list; 145 struct xdp_buff_xsk *pos, *tmp; 146 147 if (likely(!xdp_buff_has_frags(xdp))) 148 goto out; 149 150 list_for_each_entry_safe(pos, tmp, xskb_list, list_node) { 151 list_del_init(&pos->list_node); 152 xp_free(pos); 153 } 154 155 xdp_get_shared_info_from_buff(xdp)->nr_frags = 0; 156 out: 157 xp_free(xskb); 158 } 159 160 static inline bool xsk_buff_add_frag(struct xdp_buff *head, 161 struct xdp_buff *xdp) 162 { 163 const void *data = xdp->data; 164 struct xdp_buff_xsk *frag; 165 166 if (!__xdp_buff_add_frag(head, virt_to_netmem(data), 167 offset_in_page(data), xdp->data_end - data, 168 xdp->frame_sz, false)) 169 return false; 170 171 frag = container_of(xdp, struct xdp_buff_xsk, xdp); 172 list_add_tail(&frag->list_node, &frag->pool->xskb_list); 173 174 return true; 175 } 176 177 static inline struct xdp_buff *xsk_buff_get_frag(const struct xdp_buff *first) 178 { 179 struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp); 180 struct xdp_buff *ret = NULL; 181 struct xdp_buff_xsk *frag; 182 183 frag = list_first_entry_or_null(&xskb->pool->xskb_list, 184 struct xdp_buff_xsk, list_node); 185 if (frag) { 186 list_del_init(&frag->list_node); 187 ret = &frag->xdp; 188 } 189 190 return ret; 191 } 192 193 static inline void xsk_buff_del_frag(struct xdp_buff *xdp) 194 { 195 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp); 196 197 list_del_init(&xskb->list_node); 198 } 199 200 static inline struct xdp_buff *xsk_buff_get_head(struct xdp_buff *first) 201 { 202 struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp); 203 struct xdp_buff_xsk *frag; 204 205 frag = list_first_entry(&xskb->pool->xskb_list, struct xdp_buff_xsk, 206 list_node); 207 return &frag->xdp; 208 } 209 210 static inline struct xdp_buff *xsk_buff_get_tail(struct xdp_buff *first) 211 { 212 struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp); 213 struct xdp_buff_xsk *frag; 214 215 frag = list_last_entry(&xskb->pool->xskb_list, struct xdp_buff_xsk, 216 list_node); 217 return &frag->xdp; 218 } 219 220 static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size) 221 { 222 xdp->data = xdp->data_hard_start + XDP_PACKET_HEADROOM; 223 xdp->data_meta = xdp->data; 224 xdp->data_end = xdp->data + size; 225 xdp->flags = 0; 226 } 227 228 static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool, 229 u64 addr) 230 { 231 return xp_raw_get_dma(pool, addr); 232 } 233 234 static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr) 235 { 236 return xp_raw_get_data(pool, addr); 237 } 238 239 /** 240 * xsk_buff_raw_get_ctx - get &xdp_desc context 241 * @pool: XSk buff pool desc address belongs to 242 * @addr: desc address (from userspace) 243 * 244 * Wrapper for xp_raw_get_ctx() to be used in drivers, see its kdoc for 245 * details. 246 * 247 * Return: new &xdp_desc_ctx struct containing desc's DMA address and metadata 248 * pointer, if it is present (initialized to %NULL otherwise). 249 */ 250 static inline struct xdp_desc_ctx 251 xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) 252 { 253 return xp_raw_get_ctx(pool, addr); 254 } 255 256 #define XDP_TXMD_FLAGS_VALID ( \ 257 XDP_TXMD_FLAGS_TIMESTAMP | \ 258 XDP_TXMD_FLAGS_CHECKSUM | \ 259 XDP_TXMD_FLAGS_LAUNCH_TIME | \ 260 0) 261 262 static inline bool 263 xsk_buff_valid_tx_metadata(const struct xsk_buff_pool *pool, 264 const struct xsk_tx_metadata *meta, u64 *flags) 265 { 266 *flags = READ_ONCE(meta->flags); 267 if (*flags & XDP_TXMD_FLAGS_LAUNCH_TIME) 268 if (pool->tx_metadata_len < 269 offsetofend(struct xsk_tx_metadata, request.launch_time)) 270 return false; 271 return !(*flags & ~XDP_TXMD_FLAGS_VALID); 272 } 273 274 /** 275 * xsk_tx_metadata_request - Evaluate AF_XDP TX metadata at submission 276 * and call appropriate xsk_tx_metadata_ops operation. 277 * @pool: pointer to AF_XDP buffer pool, used to validate the metadata 278 * @pmeta: pointer to pointer to AF_XDP metadata area 279 * @ops: pointer to struct xsk_tx_metadata_ops 280 * @priv: pointer to driver-private area 281 * 282 * This function should be called by the networking device when 283 * it prepares AF_XDP egress packet. 284 */ 285 static inline void 286 xsk_tx_metadata_request(const struct xsk_buff_pool *pool, 287 struct xsk_tx_metadata **pmeta, 288 const struct xsk_tx_metadata_ops *ops, void *priv) 289 { 290 const struct xsk_tx_metadata *meta = *pmeta; 291 u64 flags; 292 293 if (!meta) 294 return; 295 296 if (unlikely(!xsk_buff_valid_tx_metadata(pool, meta, &flags))) { 297 *pmeta = NULL; 298 return; /* no way to signal the error to the user */ 299 } 300 301 if (ops->tmo_request_launch_time) 302 if (flags & XDP_TXMD_FLAGS_LAUNCH_TIME) 303 ops->tmo_request_launch_time( 304 READ_ONCE(meta->request.launch_time), priv); 305 306 if (ops->tmo_request_timestamp) 307 if (flags & XDP_TXMD_FLAGS_TIMESTAMP) 308 ops->tmo_request_timestamp(priv); 309 310 if (ops->tmo_request_checksum) 311 if (flags & XDP_TXMD_FLAGS_CHECKSUM) 312 ops->tmo_request_checksum( 313 READ_ONCE(meta->request.csum_start), 314 READ_ONCE(meta->request.csum_offset), priv); 315 316 if (!(flags & XDP_TXMD_FLAGS_TIMESTAMP)) 317 *pmeta = NULL; 318 } 319 320 static inline struct xsk_tx_metadata * 321 __xsk_buff_get_metadata(const struct xsk_buff_pool *pool, void *data) 322 { 323 if (!pool->tx_metadata_len) 324 return NULL; 325 326 return data - pool->tx_metadata_len; 327 } 328 329 static inline struct xsk_tx_metadata * 330 xsk_buff_get_metadata(struct xsk_buff_pool *pool, u64 addr) 331 { 332 return __xsk_buff_get_metadata(pool, xp_raw_get_data(pool, addr)); 333 } 334 335 static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp) 336 { 337 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp); 338 339 xp_dma_sync_for_cpu(xskb); 340 } 341 342 static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool, 343 dma_addr_t dma, 344 size_t size) 345 { 346 xp_dma_sync_for_device(pool, dma, size); 347 } 348 349 #else 350 351 static inline void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries) 352 { 353 } 354 355 static inline bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, 356 struct xdp_desc *desc) 357 { 358 return false; 359 } 360 361 static inline u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max) 362 { 363 return 0; 364 } 365 366 static inline void xsk_tx_release(struct xsk_buff_pool *pool) 367 { 368 } 369 370 static inline struct xsk_buff_pool * 371 xsk_get_pool_from_qid(struct net_device *dev, u16 queue_id) 372 { 373 return NULL; 374 } 375 376 static inline void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool) 377 { 378 } 379 380 static inline void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool) 381 { 382 } 383 384 static inline void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool) 385 { 386 } 387 388 static inline void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool) 389 { 390 } 391 392 static inline bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool) 393 { 394 return false; 395 } 396 397 static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool) 398 { 399 return 0; 400 } 401 402 static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool) 403 { 404 return 0; 405 } 406 407 static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool) 408 { 409 return 0; 410 } 411 412 static inline u32 xsk_pool_get_rx_frag_step(struct xsk_buff_pool *pool) 413 { 414 return 0; 415 } 416 417 static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool, 418 struct xdp_rxq_info *rxq) 419 { 420 } 421 422 static inline void xsk_pool_fill_cb(struct xsk_buff_pool *pool, 423 struct xsk_cb_desc *desc) 424 { 425 } 426 427 static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool, 428 unsigned long attrs) 429 { 430 } 431 432 static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool, 433 struct device *dev, unsigned long attrs) 434 { 435 return 0; 436 } 437 438 static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp) 439 { 440 return 0; 441 } 442 443 static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp) 444 { 445 return 0; 446 } 447 448 static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool) 449 { 450 return NULL; 451 } 452 453 static inline bool xsk_is_eop_desc(const struct xdp_desc *desc) 454 { 455 return false; 456 } 457 458 static inline u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max) 459 { 460 return 0; 461 } 462 463 static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count) 464 { 465 return false; 466 } 467 468 static inline void xsk_buff_free(struct xdp_buff *xdp) 469 { 470 } 471 472 static inline bool xsk_buff_add_frag(struct xdp_buff *head, 473 struct xdp_buff *xdp) 474 { 475 return false; 476 } 477 478 static inline struct xdp_buff *xsk_buff_get_frag(const struct xdp_buff *first) 479 { 480 return NULL; 481 } 482 483 static inline void xsk_buff_del_frag(struct xdp_buff *xdp) 484 { 485 } 486 487 static inline struct xdp_buff *xsk_buff_get_head(struct xdp_buff *first) 488 { 489 return NULL; 490 } 491 492 static inline struct xdp_buff *xsk_buff_get_tail(struct xdp_buff *first) 493 { 494 return NULL; 495 } 496 497 static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size) 498 { 499 } 500 501 static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool, 502 u64 addr) 503 { 504 return 0; 505 } 506 507 static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr) 508 { 509 return NULL; 510 } 511 512 static inline struct xdp_desc_ctx 513 xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) 514 { 515 return (struct xdp_desc_ctx){ }; 516 } 517 518 static inline bool 519 xsk_buff_valid_tx_metadata(const struct xsk_buff_pool *pool, 520 const struct xsk_tx_metadata *meta, u64 *flags) 521 { 522 return false; 523 } 524 525 static inline void 526 xsk_tx_metadata_request(const struct xsk_buff_pool *pool, 527 struct xsk_tx_metadata **pmeta, 528 const struct xsk_tx_metadata_ops *ops, void *priv) 529 { 530 } 531 532 static inline struct xsk_tx_metadata * 533 __xsk_buff_get_metadata(const struct xsk_buff_pool *pool, void *data) 534 { 535 return NULL; 536 } 537 538 static inline struct xsk_tx_metadata * 539 xsk_buff_get_metadata(struct xsk_buff_pool *pool, u64 addr) 540 { 541 return NULL; 542 } 543 544 static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp) 545 { 546 } 547 548 static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool, 549 dma_addr_t dma, 550 size_t size) 551 { 552 } 553 554 #endif /* CONFIG_XDP_SOCKETS */ 555 556 #endif /* _LINUX_XDP_SOCK_DRV_H */ 557