1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2018 Intel Corporation 3 */ 4 5 #ifndef _VHOST_NET_CDEV_H_ 6 #define _VHOST_NET_CDEV_H_ 7 #include <stdint.h> 8 #include <stdio.h> 9 #include <stdbool.h> 10 #include <sys/queue.h> 11 #include <unistd.h> 12 #include <linux/virtio_net.h> 13 #include <sys/socket.h> 14 #include <linux/if.h> 15 16 #include <rte_log.h> 17 #include <rte_ether.h> 18 #include <rte_malloc.h> 19 #include <rte_dmadev.h> 20 21 #include "rte_vhost.h" 22 #include "vdpa_driver.h" 23 24 #include "rte_vhost_async.h" 25 26 /* Used to indicate that the device is running on a data core */ 27 #define VIRTIO_DEV_RUNNING ((uint32_t)1 << 0) 28 /* Used to indicate that the device is ready to operate */ 29 #define VIRTIO_DEV_READY ((uint32_t)1 << 1) 30 /* Used to indicate that the built-in vhost net device backend is enabled */ 31 #define VIRTIO_DEV_BUILTIN_VIRTIO_NET ((uint32_t)1 << 2) 32 /* Used to indicate that the device has its own data path and configured */ 33 #define VIRTIO_DEV_VDPA_CONFIGURED ((uint32_t)1 << 3) 34 /* Used to indicate that the feature negotiation failed */ 35 #define VIRTIO_DEV_FEATURES_FAILED ((uint32_t)1 << 4) 36 /* Used to indicate that the virtio_net tx code should fill TX ol_flags */ 37 #define VIRTIO_DEV_LEGACY_OL_FLAGS ((uint32_t)1 << 5) 38 /* Used to indicate the application has requested statistics collection */ 39 #define VIRTIO_DEV_STATS_ENABLED ((uint32_t)1 << 6) 40 41 /* Backend value set by guest. */ 42 #define VIRTIO_DEV_STOPPED -1 43 44 #define BUF_VECTOR_MAX 256 45 46 #define VHOST_LOG_CACHE_NR 32 47 48 #define MAX_PKT_BURST 32 49 50 #define VHOST_MAX_ASYNC_IT (MAX_PKT_BURST) 51 #define VHOST_MAX_ASYNC_VEC 2048 52 #define VIRTIO_MAX_RX_PKTLEN 9728U 53 #define VHOST_DMA_MAX_COPY_COMPLETE ((VIRTIO_MAX_RX_PKTLEN / RTE_MBUF_DEFAULT_DATAROOM) \ 54 * MAX_PKT_BURST) 55 56 #define PACKED_DESC_ENQUEUE_USED_FLAG(w) \ 57 ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED | VRING_DESC_F_WRITE) : \ 58 VRING_DESC_F_WRITE) 59 #define PACKED_DESC_DEQUEUE_USED_FLAG(w) \ 60 ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED) : 0x0) 61 #define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \ 62 VRING_DESC_F_INDIRECT) 63 64 #define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ 65 sizeof(struct vring_packed_desc)) 66 #define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1) 67 68 #ifdef VHOST_GCC_UNROLL_PRAGMA 69 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("GCC unroll 4") \ 70 for (iter = val; iter < size; iter++) 71 #endif 72 73 #ifdef VHOST_CLANG_UNROLL_PRAGMA 74 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll 4") \ 75 for (iter = val; iter < size; iter++) 76 #endif 77 78 #ifdef VHOST_ICC_UNROLL_PRAGMA 79 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll (4)") \ 80 for (iter = val; iter < size; iter++) 81 #endif 82 83 #ifndef vhost_for_each_try_unroll 84 #define vhost_for_each_try_unroll(iter, val, num) \ 85 for (iter = val; iter < num; iter++) 86 #endif 87 88 /** 89 * Structure contains buffer address, length and descriptor index 90 * from vring to do scatter RX. 91 */ 92 struct buf_vector { 93 uint64_t buf_iova; 94 uint64_t buf_addr; 95 uint32_t buf_len; 96 uint32_t desc_idx; 97 }; 98 99 /* 100 * Structure contains the info for each batched memory copy. 101 */ 102 struct batch_copy_elem { 103 void *dst; 104 void *src; 105 uint32_t len; 106 uint64_t log_addr; 107 }; 108 109 /* 110 * Structure that contains the info for batched dirty logging. 111 */ 112 struct log_cache_entry { 113 uint32_t offset; 114 unsigned long val; 115 }; 116 117 struct vring_used_elem_packed { 118 uint16_t id; 119 uint16_t flags; 120 uint32_t len; 121 uint32_t count; 122 }; 123 124 /** 125 * Virtqueue statistics 126 */ 127 struct virtqueue_stats { 128 uint64_t packets; 129 uint64_t bytes; 130 uint64_t multicast; 131 uint64_t broadcast; 132 /* Size bins in array as RFC 2819, undersized [0], 64 [1], etc */ 133 uint64_t size_bins[8]; 134 uint64_t guest_notifications; 135 uint64_t iotlb_hits; 136 uint64_t iotlb_misses; 137 uint64_t inflight_submitted; 138 uint64_t inflight_completed; 139 }; 140 141 /** 142 * iovec 143 */ 144 struct vhost_iovec { 145 void *src_addr; 146 void *dst_addr; 147 size_t len; 148 }; 149 150 /** 151 * iovec iterator 152 */ 153 struct vhost_iov_iter { 154 /** pointer to the iovec array */ 155 struct vhost_iovec *iov; 156 /** number of iovec in this iterator */ 157 unsigned long nr_segs; 158 }; 159 160 struct async_dma_vchan_info { 161 /* circular array to track if packet copy completes */ 162 bool **pkts_cmpl_flag_addr; 163 164 /* max elements in 'pkts_cmpl_flag_addr' */ 165 uint16_t ring_size; 166 /* ring index mask for 'pkts_cmpl_flag_addr' */ 167 uint16_t ring_mask; 168 169 /** 170 * DMA virtual channel lock. Although it is able to bind DMA 171 * virtual channels to data plane threads, vhost control plane 172 * thread could call data plane functions too, thus causing 173 * DMA device contention. 174 * 175 * For example, in VM exit case, vhost control plane thread needs 176 * to clear in-flight packets before disable vring, but there could 177 * be anotther data plane thread is enqueuing packets to the same 178 * vring with the same DMA virtual channel. As dmadev PMD functions 179 * are lock-free, the control plane and data plane threads could 180 * operate the same DMA virtual channel at the same time. 181 */ 182 rte_spinlock_t dma_lock; 183 }; 184 185 struct async_dma_info { 186 struct async_dma_vchan_info *vchans; 187 /* number of registered virtual channels */ 188 uint16_t nr_vchans; 189 }; 190 191 extern struct async_dma_info dma_copy_track[RTE_DMADEV_DEFAULT_MAX]; 192 193 /** 194 * inflight async packet information 195 */ 196 struct async_inflight_info { 197 struct rte_mbuf *mbuf; 198 uint16_t descs; /* num of descs inflight */ 199 uint16_t nr_buffers; /* num of buffers inflight for packed ring */ 200 struct virtio_net_hdr nethdr; 201 }; 202 203 struct vhost_async { 204 struct vhost_iov_iter iov_iter[VHOST_MAX_ASYNC_IT]; 205 struct vhost_iovec iovec[VHOST_MAX_ASYNC_VEC]; 206 uint16_t iter_idx; 207 uint16_t iovec_idx; 208 209 /* data transfer status */ 210 struct async_inflight_info *pkts_info; 211 /** 212 * Packet reorder array. "true" indicates that DMA device 213 * completes all copies for the packet. 214 * 215 * Note that this array could be written by multiple threads 216 * simultaneously. For example, in the case of thread0 and 217 * thread1 RX packets from NIC and then enqueue packets to 218 * vring0 and vring1 with own DMA device DMA0 and DMA1, it's 219 * possible for thread0 to get completed copies belonging to 220 * vring1 from DMA0, while thread0 is calling rte_vhost_poll 221 * _enqueue_completed() for vring0 and thread1 is calling 222 * rte_vhost_submit_enqueue_burst() for vring1. In this case, 223 * vq->access_lock cannot protect pkts_cmpl_flag of vring1. 224 * 225 * However, since offloading is per-packet basis, each packet 226 * flag will only be written by one thread. And single byte 227 * write is atomic, so no lock for pkts_cmpl_flag is needed. 228 */ 229 bool *pkts_cmpl_flag; 230 uint16_t pkts_idx; 231 uint16_t pkts_inflight_n; 232 union { 233 struct vring_used_elem *descs_split; 234 struct vring_used_elem_packed *buffers_packed; 235 }; 236 union { 237 uint16_t desc_idx_split; 238 uint16_t buffer_idx_packed; 239 }; 240 union { 241 uint16_t last_desc_idx_split; 242 uint16_t last_buffer_idx_packed; 243 }; 244 }; 245 246 /** 247 * Structure contains variables relevant to RX/TX virtqueues. 248 */ 249 struct vhost_virtqueue { 250 union { 251 struct vring_desc *desc; 252 struct vring_packed_desc *desc_packed; 253 }; 254 union { 255 struct vring_avail *avail; 256 struct vring_packed_desc_event *driver_event; 257 }; 258 union { 259 struct vring_used *used; 260 struct vring_packed_desc_event *device_event; 261 }; 262 uint16_t size; 263 264 uint16_t last_avail_idx; 265 uint16_t last_used_idx; 266 /* Last used index we notify to front end. */ 267 uint16_t signalled_used; 268 bool signalled_used_valid; 269 #define VIRTIO_INVALID_EVENTFD (-1) 270 #define VIRTIO_UNINITIALIZED_EVENTFD (-2) 271 272 bool enabled; 273 bool access_ok; 274 bool ready; 275 276 rte_spinlock_t access_lock; 277 278 279 union { 280 struct vring_used_elem *shadow_used_split; 281 struct vring_used_elem_packed *shadow_used_packed; 282 }; 283 uint16_t shadow_used_idx; 284 /* Record packed ring enqueue latest desc cache aligned index */ 285 uint16_t shadow_aligned_idx; 286 /* Record packed ring first dequeue desc index */ 287 uint16_t shadow_last_used_idx; 288 289 uint16_t batch_copy_nb_elems; 290 struct batch_copy_elem *batch_copy_elems; 291 int numa_node; 292 bool used_wrap_counter; 293 bool avail_wrap_counter; 294 295 /* Physical address of used ring, for logging */ 296 uint16_t log_cache_nb_elem; 297 uint64_t log_guest_addr; 298 struct log_cache_entry *log_cache; 299 300 rte_rwlock_t iotlb_lock; 301 rte_rwlock_t iotlb_pending_lock; 302 struct rte_mempool *iotlb_pool; 303 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list; 304 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list; 305 int iotlb_cache_nr; 306 307 /* Used to notify the guest (trigger interrupt) */ 308 int callfd; 309 /* Currently unused as polling mode is enabled */ 310 int kickfd; 311 312 /* inflight share memory info */ 313 union { 314 struct rte_vhost_inflight_info_split *inflight_split; 315 struct rte_vhost_inflight_info_packed *inflight_packed; 316 }; 317 struct rte_vhost_resubmit_info *resubmit_inflight; 318 uint64_t global_counter; 319 320 struct vhost_async *async; 321 322 int notif_enable; 323 #define VIRTIO_UNINITIALIZED_NOTIF (-1) 324 325 struct vhost_vring_addr ring_addrs; 326 struct virtqueue_stats stats; 327 } __rte_cache_aligned; 328 329 /* Virtio device status as per Virtio specification */ 330 #define VIRTIO_DEVICE_STATUS_RESET 0x00 331 #define VIRTIO_DEVICE_STATUS_ACK 0x01 332 #define VIRTIO_DEVICE_STATUS_DRIVER 0x02 333 #define VIRTIO_DEVICE_STATUS_DRIVER_OK 0x04 334 #define VIRTIO_DEVICE_STATUS_FEATURES_OK 0x08 335 #define VIRTIO_DEVICE_STATUS_DEV_NEED_RESET 0x40 336 #define VIRTIO_DEVICE_STATUS_FAILED 0x80 337 338 #define VHOST_MAX_VRING 0x100 339 #define VHOST_MAX_QUEUE_PAIRS 0x80 340 341 /* Declare IOMMU related bits for older kernels */ 342 #ifndef VIRTIO_F_IOMMU_PLATFORM 343 344 #define VIRTIO_F_IOMMU_PLATFORM 33 345 346 struct vhost_iotlb_msg { 347 __u64 iova; 348 __u64 size; 349 __u64 uaddr; 350 #define VHOST_ACCESS_RO 0x1 351 #define VHOST_ACCESS_WO 0x2 352 #define VHOST_ACCESS_RW 0x3 353 __u8 perm; 354 #define VHOST_IOTLB_MISS 1 355 #define VHOST_IOTLB_UPDATE 2 356 #define VHOST_IOTLB_INVALIDATE 3 357 #define VHOST_IOTLB_ACCESS_FAIL 4 358 __u8 type; 359 }; 360 361 #define VHOST_IOTLB_MSG 0x1 362 363 struct vhost_msg { 364 int type; 365 union { 366 struct vhost_iotlb_msg iotlb; 367 __u8 padding[64]; 368 }; 369 }; 370 #endif 371 372 /* 373 * Define virtio 1.0 for older kernels 374 */ 375 #ifndef VIRTIO_F_VERSION_1 376 #define VIRTIO_F_VERSION_1 32 377 #endif 378 379 /* Declare packed ring related bits for older kernels */ 380 #ifndef VIRTIO_F_RING_PACKED 381 382 #define VIRTIO_F_RING_PACKED 34 383 384 struct vring_packed_desc { 385 uint64_t addr; 386 uint32_t len; 387 uint16_t id; 388 uint16_t flags; 389 }; 390 391 struct vring_packed_desc_event { 392 uint16_t off_wrap; 393 uint16_t flags; 394 }; 395 #endif 396 397 /* 398 * Declare below packed ring defines unconditionally 399 * as Kernel header might use different names. 400 */ 401 #define VRING_DESC_F_AVAIL (1ULL << 7) 402 #define VRING_DESC_F_USED (1ULL << 15) 403 404 #define VRING_EVENT_F_ENABLE 0x0 405 #define VRING_EVENT_F_DISABLE 0x1 406 #define VRING_EVENT_F_DESC 0x2 407 408 /* 409 * Available and used descs are in same order 410 */ 411 #ifndef VIRTIO_F_IN_ORDER 412 #define VIRTIO_F_IN_ORDER 35 413 #endif 414 415 /* Features supported by this builtin vhost-user net driver. */ 416 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \ 417 (1ULL << VIRTIO_F_ANY_LAYOUT) | \ 418 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \ 419 (1ULL << VIRTIO_NET_F_CTRL_RX) | \ 420 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \ 421 (1ULL << VIRTIO_NET_F_MQ) | \ 422 (1ULL << VIRTIO_F_VERSION_1) | \ 423 (1ULL << VHOST_F_LOG_ALL) | \ 424 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \ 425 (1ULL << VIRTIO_NET_F_GSO) | \ 426 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \ 427 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \ 428 (1ULL << VIRTIO_NET_F_HOST_UFO) | \ 429 (1ULL << VIRTIO_NET_F_HOST_ECN) | \ 430 (1ULL << VIRTIO_NET_F_CSUM) | \ 431 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \ 432 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \ 433 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \ 434 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \ 435 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \ 436 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \ 437 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \ 438 (1ULL << VIRTIO_NET_F_MTU) | \ 439 (1ULL << VIRTIO_F_IN_ORDER) | \ 440 (1ULL << VIRTIO_F_IOMMU_PLATFORM) | \ 441 (1ULL << VIRTIO_F_RING_PACKED)) 442 443 444 struct guest_page { 445 uint64_t guest_phys_addr; 446 uint64_t host_iova; 447 uint64_t host_user_addr; 448 uint64_t size; 449 }; 450 451 struct inflight_mem_info { 452 int fd; 453 void *addr; 454 uint64_t size; 455 }; 456 457 /** 458 * Device structure contains all configuration information relating 459 * to the device. 460 */ 461 struct virtio_net { 462 /* Frontend (QEMU) memory and memory region information */ 463 struct rte_vhost_memory *mem; 464 uint64_t features; 465 uint64_t protocol_features; 466 int vid; 467 uint32_t flags; 468 uint16_t vhost_hlen; 469 /* to tell if we need broadcast rarp packet */ 470 int16_t broadcast_rarp; 471 uint32_t nr_vring; 472 int async_copy; 473 474 int extbuf; 475 int linearbuf; 476 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2]; 477 struct inflight_mem_info *inflight_info; 478 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ) 479 char ifname[IF_NAME_SZ]; 480 uint64_t log_size; 481 uint64_t log_base; 482 uint64_t log_addr; 483 struct rte_ether_addr mac; 484 uint16_t mtu; 485 uint8_t status; 486 487 struct rte_vhost_device_ops const *notify_ops; 488 489 uint32_t nr_guest_pages; 490 uint32_t max_guest_pages; 491 struct guest_page *guest_pages; 492 493 int slave_req_fd; 494 rte_spinlock_t slave_req_lock; 495 496 int postcopy_ufd; 497 int postcopy_listening; 498 499 struct rte_vdpa_device *vdpa_dev; 500 501 /* context data for the external message handlers */ 502 void *extern_data; 503 /* pre and post vhost user message handlers for the device */ 504 struct rte_vhost_user_extern_ops extern_ops; 505 } __rte_cache_aligned; 506 507 static __rte_always_inline bool 508 vq_is_packed(struct virtio_net *dev) 509 { 510 return dev->features & (1ull << VIRTIO_F_RING_PACKED); 511 } 512 513 static inline bool 514 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter) 515 { 516 uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE); 517 518 return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) && 519 wrap_counter != !!(flags & VRING_DESC_F_USED); 520 } 521 522 static inline void 523 vq_inc_last_used_packed(struct vhost_virtqueue *vq, uint16_t num) 524 { 525 vq->last_used_idx += num; 526 if (vq->last_used_idx >= vq->size) { 527 vq->used_wrap_counter ^= 1; 528 vq->last_used_idx -= vq->size; 529 } 530 } 531 532 static inline void 533 vq_inc_last_avail_packed(struct vhost_virtqueue *vq, uint16_t num) 534 { 535 vq->last_avail_idx += num; 536 if (vq->last_avail_idx >= vq->size) { 537 vq->avail_wrap_counter ^= 1; 538 vq->last_avail_idx -= vq->size; 539 } 540 } 541 542 void __vhost_log_cache_write(struct virtio_net *dev, 543 struct vhost_virtqueue *vq, 544 uint64_t addr, uint64_t len); 545 void __vhost_log_cache_write_iova(struct virtio_net *dev, 546 struct vhost_virtqueue *vq, 547 uint64_t iova, uint64_t len); 548 void __vhost_log_cache_sync(struct virtio_net *dev, 549 struct vhost_virtqueue *vq); 550 void __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len); 551 void __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq, 552 uint64_t iova, uint64_t len); 553 554 static __rte_always_inline void 555 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len) 556 { 557 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) 558 __vhost_log_write(dev, addr, len); 559 } 560 561 static __rte_always_inline void 562 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq) 563 { 564 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) 565 __vhost_log_cache_sync(dev, vq); 566 } 567 568 static __rte_always_inline void 569 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq, 570 uint64_t addr, uint64_t len) 571 { 572 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) 573 __vhost_log_cache_write(dev, vq, addr, len); 574 } 575 576 static __rte_always_inline void 577 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq, 578 uint64_t offset, uint64_t len) 579 { 580 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) { 581 if (unlikely(vq->log_guest_addr == 0)) 582 return; 583 __vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, 584 len); 585 } 586 } 587 588 static __rte_always_inline void 589 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq, 590 uint64_t offset, uint64_t len) 591 { 592 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) { 593 if (unlikely(vq->log_guest_addr == 0)) 594 return; 595 __vhost_log_write(dev, vq->log_guest_addr + offset, len); 596 } 597 } 598 599 static __rte_always_inline void 600 vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq, 601 uint64_t iova, uint64_t len) 602 { 603 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL)))) 604 return; 605 606 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) 607 __vhost_log_cache_write_iova(dev, vq, iova, len); 608 else 609 __vhost_log_cache_write(dev, vq, iova, len); 610 } 611 612 static __rte_always_inline void 613 vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq, 614 uint64_t iova, uint64_t len) 615 { 616 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL)))) 617 return; 618 619 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) 620 __vhost_log_write_iova(dev, vq, iova, len); 621 else 622 __vhost_log_write(dev, iova, len); 623 } 624 625 extern int vhost_config_log_level; 626 extern int vhost_data_log_level; 627 628 #define VHOST_LOG_CONFIG(level, fmt, args...) \ 629 rte_log(RTE_LOG_ ## level, vhost_config_log_level, \ 630 "VHOST_CONFIG: " fmt, ##args) 631 632 #define VHOST_LOG_DATA(level, fmt, args...) \ 633 (void)((RTE_LOG_ ## level <= RTE_LOG_DP_LEVEL) ? \ 634 rte_log(RTE_LOG_ ## level, vhost_data_log_level, \ 635 "VHOST_DATA : " fmt, ##args) : \ 636 0) 637 638 #ifdef RTE_LIBRTE_VHOST_DEBUG 639 #define VHOST_MAX_PRINT_BUFF 6072 640 #define PRINT_PACKET(device, addr, size, header) do { \ 641 char *pkt_addr = (char *)(addr); \ 642 unsigned int index; \ 643 char packet[VHOST_MAX_PRINT_BUFF]; \ 644 \ 645 if ((header)) \ 646 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \ 647 else \ 648 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \ 649 for (index = 0; index < (size); index++) { \ 650 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \ 651 "%02hhx ", pkt_addr[index]); \ 652 } \ 653 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \ 654 \ 655 VHOST_LOG_DATA(DEBUG, "%s", packet); \ 656 } while (0) 657 #else 658 #define PRINT_PACKET(device, addr, size, header) do {} while (0) 659 #endif 660 661 extern struct virtio_net *vhost_devices[RTE_MAX_VHOST_DEVICE]; 662 663 #define VHOST_BINARY_SEARCH_THRESH 256 664 665 static __rte_always_inline int guest_page_addrcmp(const void *p1, 666 const void *p2) 667 { 668 const struct guest_page *page1 = (const struct guest_page *)p1; 669 const struct guest_page *page2 = (const struct guest_page *)p2; 670 671 if (page1->guest_phys_addr > page2->guest_phys_addr) 672 return 1; 673 if (page1->guest_phys_addr < page2->guest_phys_addr) 674 return -1; 675 676 return 0; 677 } 678 679 static __rte_always_inline int guest_page_rangecmp(const void *p1, const void *p2) 680 { 681 const struct guest_page *page1 = (const struct guest_page *)p1; 682 const struct guest_page *page2 = (const struct guest_page *)p2; 683 684 if (page1->guest_phys_addr >= page2->guest_phys_addr) { 685 if (page1->guest_phys_addr < page2->guest_phys_addr + page2->size) 686 return 0; 687 else 688 return 1; 689 } else 690 return -1; 691 } 692 693 static __rte_always_inline rte_iova_t 694 gpa_to_first_hpa(struct virtio_net *dev, uint64_t gpa, 695 uint64_t gpa_size, uint64_t *hpa_size) 696 { 697 uint32_t i; 698 struct guest_page *page; 699 struct guest_page key; 700 701 *hpa_size = gpa_size; 702 if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) { 703 key.guest_phys_addr = gpa; 704 page = bsearch(&key, dev->guest_pages, dev->nr_guest_pages, 705 sizeof(struct guest_page), guest_page_rangecmp); 706 if (page) { 707 if (gpa + gpa_size <= 708 page->guest_phys_addr + page->size) { 709 return gpa - page->guest_phys_addr + 710 page->host_iova; 711 } else if (gpa < page->guest_phys_addr + 712 page->size) { 713 *hpa_size = page->guest_phys_addr + 714 page->size - gpa; 715 return gpa - page->guest_phys_addr + 716 page->host_iova; 717 } 718 } 719 } else { 720 for (i = 0; i < dev->nr_guest_pages; i++) { 721 page = &dev->guest_pages[i]; 722 723 if (gpa >= page->guest_phys_addr) { 724 if (gpa + gpa_size <= 725 page->guest_phys_addr + page->size) { 726 return gpa - page->guest_phys_addr + 727 page->host_iova; 728 } else if (gpa < page->guest_phys_addr + 729 page->size) { 730 *hpa_size = page->guest_phys_addr + 731 page->size - gpa; 732 return gpa - page->guest_phys_addr + 733 page->host_iova; 734 } 735 } 736 } 737 } 738 739 *hpa_size = 0; 740 return 0; 741 } 742 743 /* Convert guest physical address to host physical address */ 744 static __rte_always_inline rte_iova_t 745 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size) 746 { 747 rte_iova_t hpa; 748 uint64_t hpa_size; 749 750 hpa = gpa_to_first_hpa(dev, gpa, size, &hpa_size); 751 return hpa_size == size ? hpa : 0; 752 } 753 754 static __rte_always_inline uint64_t 755 hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len) 756 { 757 struct rte_vhost_mem_region *r; 758 uint32_t i; 759 760 if (unlikely(!dev || !dev->mem)) 761 return 0; 762 763 for (i = 0; i < dev->mem->nregions; i++) { 764 r = &dev->mem->regions[i]; 765 766 if (vva >= r->host_user_addr && 767 vva + len < r->host_user_addr + r->size) { 768 return r->guest_phys_addr + vva - r->host_user_addr; 769 } 770 } 771 return 0; 772 } 773 774 static __rte_always_inline struct virtio_net * 775 get_device(int vid) 776 { 777 struct virtio_net *dev = vhost_devices[vid]; 778 779 if (unlikely(!dev)) { 780 VHOST_LOG_CONFIG(ERR, 781 "(%d) device not found.\n", vid); 782 } 783 784 return dev; 785 } 786 787 int vhost_new_device(void); 788 void cleanup_device(struct virtio_net *dev, int destroy); 789 void reset_device(struct virtio_net *dev); 790 void vhost_destroy_device(int); 791 void vhost_destroy_device_notify(struct virtio_net *dev); 792 793 void cleanup_vq(struct vhost_virtqueue *vq, int destroy); 794 void cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq); 795 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq); 796 797 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx); 798 799 void vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *dev); 800 801 void vhost_set_ifname(int, const char *if_name, unsigned int if_len); 802 void vhost_setup_virtio_net(int vid, bool enable, bool legacy_ol_flags, bool stats_enabled); 803 void vhost_enable_extbuf(int vid); 804 void vhost_enable_linearbuf(int vid); 805 int vhost_enable_guest_notification(struct virtio_net *dev, 806 struct vhost_virtqueue *vq, int enable); 807 808 struct rte_vhost_device_ops const *vhost_driver_callback_get(const char *path); 809 810 /* 811 * Backend-specific cleanup. 812 * 813 * TODO: fix it; we have one backend now 814 */ 815 void vhost_backend_cleanup(struct virtio_net *dev); 816 817 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq, 818 uint64_t iova, uint64_t *len, uint8_t perm); 819 void *vhost_alloc_copy_ind_table(struct virtio_net *dev, 820 struct vhost_virtqueue *vq, 821 uint64_t desc_addr, uint64_t desc_len); 822 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq); 823 uint64_t translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq, 824 uint64_t log_addr); 825 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq); 826 827 static __rte_always_inline uint64_t 828 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq, 829 uint64_t iova, uint64_t *len, uint8_t perm) 830 { 831 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))) 832 return rte_vhost_va_from_guest_pa(dev->mem, iova, len); 833 834 return __vhost_iova_to_vva(dev, vq, iova, len, perm); 835 } 836 837 #define vhost_avail_event(vr) \ 838 (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size]) 839 #define vhost_used_event(vr) \ 840 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size]) 841 842 /* 843 * The following is used with VIRTIO_RING_F_EVENT_IDX. 844 * Assuming a given event_idx value from the other size, if we have 845 * just incremented index from old to new_idx, should we trigger an 846 * event? 847 */ 848 static __rte_always_inline int 849 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old) 850 { 851 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old); 852 } 853 854 static __rte_always_inline void 855 vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq) 856 { 857 /* Flush used->idx update before we read avail->flags. */ 858 rte_atomic_thread_fence(__ATOMIC_SEQ_CST); 859 860 /* Don't kick guest if we don't reach index specified by guest. */ 861 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) { 862 uint16_t old = vq->signalled_used; 863 uint16_t new = vq->last_used_idx; 864 bool signalled_used_valid = vq->signalled_used_valid; 865 866 vq->signalled_used = new; 867 vq->signalled_used_valid = true; 868 869 VHOST_LOG_DATA(DEBUG, "%s: used_event_idx=%d, old=%d, new=%d\n", 870 __func__, 871 vhost_used_event(vq), 872 old, new); 873 874 if ((vhost_need_event(vhost_used_event(vq), new, old) && 875 (vq->callfd >= 0)) || 876 unlikely(!signalled_used_valid)) { 877 eventfd_write(vq->callfd, (eventfd_t) 1); 878 if (dev->flags & VIRTIO_DEV_STATS_ENABLED) 879 vq->stats.guest_notifications++; 880 if (dev->notify_ops->guest_notified) 881 dev->notify_ops->guest_notified(dev->vid); 882 } 883 } else { 884 /* Kick the guest if necessary. */ 885 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT) 886 && (vq->callfd >= 0)) { 887 eventfd_write(vq->callfd, (eventfd_t)1); 888 if (dev->flags & VIRTIO_DEV_STATS_ENABLED) 889 vq->stats.guest_notifications++; 890 if (dev->notify_ops->guest_notified) 891 dev->notify_ops->guest_notified(dev->vid); 892 } 893 } 894 } 895 896 static __rte_always_inline void 897 vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq) 898 { 899 uint16_t old, new, off, off_wrap; 900 bool signalled_used_valid, kick = false; 901 902 /* Flush used desc update. */ 903 rte_atomic_thread_fence(__ATOMIC_SEQ_CST); 904 905 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) { 906 if (vq->driver_event->flags != 907 VRING_EVENT_F_DISABLE) 908 kick = true; 909 goto kick; 910 } 911 912 old = vq->signalled_used; 913 new = vq->last_used_idx; 914 vq->signalled_used = new; 915 signalled_used_valid = vq->signalled_used_valid; 916 vq->signalled_used_valid = true; 917 918 if (vq->driver_event->flags != VRING_EVENT_F_DESC) { 919 if (vq->driver_event->flags != VRING_EVENT_F_DISABLE) 920 kick = true; 921 goto kick; 922 } 923 924 if (unlikely(!signalled_used_valid)) { 925 kick = true; 926 goto kick; 927 } 928 929 rte_atomic_thread_fence(__ATOMIC_ACQUIRE); 930 931 off_wrap = vq->driver_event->off_wrap; 932 off = off_wrap & ~(1 << 15); 933 934 if (new <= old) 935 old -= vq->size; 936 937 if (vq->used_wrap_counter != off_wrap >> 15) 938 off -= vq->size; 939 940 if (vhost_need_event(off, new, old)) 941 kick = true; 942 kick: 943 if (kick) { 944 eventfd_write(vq->callfd, (eventfd_t)1); 945 if (dev->notify_ops->guest_notified) 946 dev->notify_ops->guest_notified(dev->vid); 947 } 948 } 949 950 static __rte_always_inline void 951 free_ind_table(void *idesc) 952 { 953 rte_free(idesc); 954 } 955 956 static __rte_always_inline void 957 restore_mbuf(struct rte_mbuf *m) 958 { 959 uint32_t mbuf_size, priv_size; 960 961 while (m) { 962 priv_size = rte_pktmbuf_priv_size(m->pool); 963 mbuf_size = sizeof(struct rte_mbuf) + priv_size; 964 /* start of buffer is after mbuf structure and priv data */ 965 966 m->buf_addr = (char *)m + mbuf_size; 967 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size; 968 m = m->next; 969 } 970 } 971 972 static __rte_always_inline bool 973 mbuf_is_consumed(struct rte_mbuf *m) 974 { 975 while (m) { 976 if (rte_mbuf_refcnt_read(m) > 1) 977 return false; 978 m = m->next; 979 } 980 981 return true; 982 } 983 #endif /* _VHOST_NET_CDEV_H_ */ 984