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