1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2017 Intel Corporation 3 */ 4 5 #include <linux/vhost.h> 6 #include <linux/virtio_net.h> 7 #include <stdint.h> 8 #include <stdlib.h> 9 #ifdef RTE_LIBRTE_VHOST_NUMA 10 #include <numa.h> 11 #include <numaif.h> 12 #endif 13 14 #include <rte_errno.h> 15 #include <rte_log.h> 16 #include <rte_memory.h> 17 #include <rte_malloc.h> 18 #include <rte_vhost.h> 19 20 #include "iotlb.h" 21 #include "vhost.h" 22 #include "vhost_user.h" 23 24 struct virtio_net *vhost_devices[RTE_MAX_VHOST_DEVICE]; 25 pthread_mutex_t vhost_dev_lock = PTHREAD_MUTEX_INITIALIZER; 26 27 struct vhost_vq_stats_name_off { 28 char name[RTE_VHOST_STATS_NAME_SIZE]; 29 unsigned int offset; 30 }; 31 32 static const struct vhost_vq_stats_name_off vhost_vq_stat_strings[] = { 33 {"good_packets", offsetof(struct vhost_virtqueue, stats.packets)}, 34 {"good_bytes", offsetof(struct vhost_virtqueue, stats.bytes)}, 35 {"multicast_packets", offsetof(struct vhost_virtqueue, stats.multicast)}, 36 {"broadcast_packets", offsetof(struct vhost_virtqueue, stats.broadcast)}, 37 {"undersize_packets", offsetof(struct vhost_virtqueue, stats.size_bins[0])}, 38 {"size_64_packets", offsetof(struct vhost_virtqueue, stats.size_bins[1])}, 39 {"size_65_127_packets", offsetof(struct vhost_virtqueue, stats.size_bins[2])}, 40 {"size_128_255_packets", offsetof(struct vhost_virtqueue, stats.size_bins[3])}, 41 {"size_256_511_packets", offsetof(struct vhost_virtqueue, stats.size_bins[4])}, 42 {"size_512_1023_packets", offsetof(struct vhost_virtqueue, stats.size_bins[5])}, 43 {"size_1024_1518_packets", offsetof(struct vhost_virtqueue, stats.size_bins[6])}, 44 {"size_1519_max_packets", offsetof(struct vhost_virtqueue, stats.size_bins[7])}, 45 {"guest_notifications", offsetof(struct vhost_virtqueue, stats.guest_notifications)}, 46 {"iotlb_hits", offsetof(struct vhost_virtqueue, stats.iotlb_hits)}, 47 {"iotlb_misses", offsetof(struct vhost_virtqueue, stats.iotlb_misses)}, 48 {"inflight_submitted", offsetof(struct vhost_virtqueue, stats.inflight_submitted)}, 49 {"inflight_completed", offsetof(struct vhost_virtqueue, stats.inflight_completed)}, 50 }; 51 52 #define VHOST_NB_VQ_STATS RTE_DIM(vhost_vq_stat_strings) 53 54 /* Called with iotlb_lock read-locked */ 55 uint64_t 56 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq, 57 uint64_t iova, uint64_t *size, uint8_t perm) 58 { 59 uint64_t vva, tmp_size; 60 61 if (unlikely(!*size)) 62 return 0; 63 64 tmp_size = *size; 65 66 vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm); 67 if (tmp_size == *size) { 68 if (dev->flags & VIRTIO_DEV_STATS_ENABLED) 69 vq->stats.iotlb_hits++; 70 return vva; 71 } 72 73 if (dev->flags & VIRTIO_DEV_STATS_ENABLED) 74 vq->stats.iotlb_misses++; 75 76 iova += tmp_size; 77 78 if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) { 79 /* 80 * iotlb_lock is read-locked for a full burst, 81 * but it only protects the iotlb cache. 82 * In case of IOTLB miss, we might block on the socket, 83 * which could cause a deadlock with QEMU if an IOTLB update 84 * is being handled. We can safely unlock here to avoid it. 85 */ 86 vhost_user_iotlb_rd_unlock(vq); 87 88 vhost_user_iotlb_pending_insert(dev, vq, iova, perm); 89 if (vhost_user_iotlb_miss(dev, iova, perm)) { 90 VHOST_LOG_DATA(dev->ifname, ERR, 91 "IOTLB miss req failed for IOVA 0x%" PRIx64 "\n", 92 iova); 93 vhost_user_iotlb_pending_remove(vq, iova, 1, perm); 94 } 95 96 vhost_user_iotlb_rd_lock(vq); 97 } 98 99 return 0; 100 } 101 102 #define VHOST_LOG_PAGE 4096 103 104 /* 105 * Atomically set a bit in memory. 106 */ 107 static __rte_always_inline void 108 vhost_set_bit(unsigned int nr, volatile uint8_t *addr) 109 { 110 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100) 111 /* 112 * __sync_ built-ins are deprecated, but __atomic_ ones 113 * are sub-optimized in older GCC versions. 114 */ 115 __sync_fetch_and_or_1(addr, (1U << nr)); 116 #else 117 __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED); 118 #endif 119 } 120 121 static __rte_always_inline void 122 vhost_log_page(uint8_t *log_base, uint64_t page) 123 { 124 vhost_set_bit(page % 8, &log_base[page / 8]); 125 } 126 127 void 128 __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len) 129 { 130 uint64_t page; 131 132 if (unlikely(!dev->log_base || !len)) 133 return; 134 135 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8))) 136 return; 137 138 /* To make sure guest memory updates are committed before logging */ 139 rte_atomic_thread_fence(__ATOMIC_RELEASE); 140 141 page = addr / VHOST_LOG_PAGE; 142 while (page * VHOST_LOG_PAGE < addr + len) { 143 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page); 144 page += 1; 145 } 146 } 147 148 void 149 __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq, 150 uint64_t iova, uint64_t len) 151 { 152 uint64_t hva, gpa, map_len; 153 map_len = len; 154 155 hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW); 156 if (map_len != len) { 157 VHOST_LOG_DATA(dev->ifname, ERR, 158 "failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n", 159 iova); 160 return; 161 } 162 163 gpa = hva_to_gpa(dev, hva, len); 164 if (gpa) 165 __vhost_log_write(dev, gpa, len); 166 } 167 168 void 169 __vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq) 170 { 171 unsigned long *log_base; 172 int i; 173 174 if (unlikely(!dev->log_base)) 175 return; 176 177 /* No cache, nothing to sync */ 178 if (unlikely(!vq->log_cache)) 179 return; 180 181 rte_atomic_thread_fence(__ATOMIC_RELEASE); 182 183 log_base = (unsigned long *)(uintptr_t)dev->log_base; 184 185 for (i = 0; i < vq->log_cache_nb_elem; i++) { 186 struct log_cache_entry *elem = vq->log_cache + i; 187 188 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100) 189 /* 190 * '__sync' builtins are deprecated, but '__atomic' ones 191 * are sub-optimized in older GCC versions. 192 */ 193 __sync_fetch_and_or(log_base + elem->offset, elem->val); 194 #else 195 __atomic_fetch_or(log_base + elem->offset, elem->val, 196 __ATOMIC_RELAXED); 197 #endif 198 } 199 200 rte_atomic_thread_fence(__ATOMIC_RELEASE); 201 202 vq->log_cache_nb_elem = 0; 203 } 204 205 static __rte_always_inline void 206 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq, 207 uint64_t page) 208 { 209 uint32_t bit_nr = page % (sizeof(unsigned long) << 3); 210 uint32_t offset = page / (sizeof(unsigned long) << 3); 211 int i; 212 213 if (unlikely(!vq->log_cache)) { 214 /* No logging cache allocated, write dirty log map directly */ 215 rte_atomic_thread_fence(__ATOMIC_RELEASE); 216 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page); 217 218 return; 219 } 220 221 for (i = 0; i < vq->log_cache_nb_elem; i++) { 222 struct log_cache_entry *elem = vq->log_cache + i; 223 224 if (elem->offset == offset) { 225 elem->val |= (1UL << bit_nr); 226 return; 227 } 228 } 229 230 if (unlikely(i >= VHOST_LOG_CACHE_NR)) { 231 /* 232 * No more room for a new log cache entry, 233 * so write the dirty log map directly. 234 */ 235 rte_atomic_thread_fence(__ATOMIC_RELEASE); 236 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page); 237 238 return; 239 } 240 241 vq->log_cache[i].offset = offset; 242 vq->log_cache[i].val = (1UL << bit_nr); 243 vq->log_cache_nb_elem++; 244 } 245 246 void 247 __vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq, 248 uint64_t addr, uint64_t len) 249 { 250 uint64_t page; 251 252 if (unlikely(!dev->log_base || !len)) 253 return; 254 255 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8))) 256 return; 257 258 page = addr / VHOST_LOG_PAGE; 259 while (page * VHOST_LOG_PAGE < addr + len) { 260 vhost_log_cache_page(dev, vq, page); 261 page += 1; 262 } 263 } 264 265 void 266 __vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq, 267 uint64_t iova, uint64_t len) 268 { 269 uint64_t hva, gpa, map_len; 270 map_len = len; 271 272 hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW); 273 if (map_len != len) { 274 VHOST_LOG_DATA(dev->ifname, ERR, 275 "failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n", 276 iova); 277 return; 278 } 279 280 gpa = hva_to_gpa(dev, hva, len); 281 if (gpa) 282 __vhost_log_cache_write(dev, vq, gpa, len); 283 } 284 285 void * 286 vhost_alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq, 287 uint64_t desc_addr, uint64_t desc_len) 288 { 289 void *idesc; 290 uint64_t src, dst; 291 uint64_t len, remain = desc_len; 292 293 idesc = rte_malloc_socket(__func__, desc_len, 0, vq->numa_node); 294 if (unlikely(!idesc)) 295 return NULL; 296 297 dst = (uint64_t)(uintptr_t)idesc; 298 299 while (remain) { 300 len = remain; 301 src = vhost_iova_to_vva(dev, vq, desc_addr, &len, 302 VHOST_ACCESS_RO); 303 if (unlikely(!src || !len)) { 304 rte_free(idesc); 305 return NULL; 306 } 307 308 rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len); 309 310 remain -= len; 311 dst += len; 312 desc_addr += len; 313 } 314 315 return idesc; 316 } 317 318 void 319 cleanup_vq(struct vhost_virtqueue *vq, int destroy) 320 { 321 if ((vq->callfd >= 0) && (destroy != 0)) 322 close(vq->callfd); 323 if (vq->kickfd >= 0) 324 close(vq->kickfd); 325 } 326 327 void 328 cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq) 329 { 330 if (!(dev->protocol_features & 331 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))) 332 return; 333 334 if (vq_is_packed(dev)) { 335 if (vq->inflight_packed) 336 vq->inflight_packed = NULL; 337 } else { 338 if (vq->inflight_split) 339 vq->inflight_split = NULL; 340 } 341 342 if (vq->resubmit_inflight) { 343 if (vq->resubmit_inflight->resubmit_list) { 344 rte_free(vq->resubmit_inflight->resubmit_list); 345 vq->resubmit_inflight->resubmit_list = NULL; 346 } 347 rte_free(vq->resubmit_inflight); 348 vq->resubmit_inflight = NULL; 349 } 350 } 351 352 /* 353 * Unmap any memory, close any file descriptors and 354 * free any memory owned by a device. 355 */ 356 void 357 cleanup_device(struct virtio_net *dev, int destroy) 358 { 359 uint32_t i; 360 361 vhost_backend_cleanup(dev); 362 363 for (i = 0; i < dev->nr_vring; i++) { 364 cleanup_vq(dev->virtqueue[i], destroy); 365 cleanup_vq_inflight(dev, dev->virtqueue[i]); 366 } 367 } 368 369 static void 370 vhost_free_async_mem(struct vhost_virtqueue *vq) 371 { 372 if (!vq->async) 373 return; 374 375 rte_free(vq->async->pkts_info); 376 rte_free(vq->async->pkts_cmpl_flag); 377 378 rte_free(vq->async->buffers_packed); 379 vq->async->buffers_packed = NULL; 380 rte_free(vq->async->descs_split); 381 vq->async->descs_split = NULL; 382 383 rte_free(vq->async); 384 vq->async = NULL; 385 } 386 387 void 388 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq) 389 { 390 if (vq_is_packed(dev)) 391 rte_free(vq->shadow_used_packed); 392 else 393 rte_free(vq->shadow_used_split); 394 395 vhost_free_async_mem(vq); 396 rte_free(vq->batch_copy_elems); 397 vhost_user_iotlb_destroy(vq); 398 rte_free(vq->log_cache); 399 rte_free(vq); 400 } 401 402 /* 403 * Release virtqueues and device memory. 404 */ 405 static void 406 free_device(struct virtio_net *dev) 407 { 408 uint32_t i; 409 410 for (i = 0; i < dev->nr_vring; i++) 411 free_vq(dev, dev->virtqueue[i]); 412 413 rte_free(dev); 414 } 415 416 static __rte_always_inline int 417 log_translate(struct virtio_net *dev, struct vhost_virtqueue *vq) 418 { 419 if (likely(!(vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG)))) 420 return 0; 421 422 vq->log_guest_addr = translate_log_addr(dev, vq, 423 vq->ring_addrs.log_guest_addr); 424 if (vq->log_guest_addr == 0) 425 return -1; 426 427 return 0; 428 } 429 430 /* 431 * Converts vring log address to GPA 432 * If IOMMU is enabled, the log address is IOVA 433 * If IOMMU not enabled, the log address is already GPA 434 * 435 * Caller should have iotlb_lock read-locked 436 */ 437 uint64_t 438 translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq, 439 uint64_t log_addr) 440 { 441 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) { 442 const uint64_t exp_size = sizeof(uint64_t); 443 uint64_t hva, gpa; 444 uint64_t size = exp_size; 445 446 hva = vhost_iova_to_vva(dev, vq, log_addr, 447 &size, VHOST_ACCESS_RW); 448 449 if (size != exp_size) 450 return 0; 451 452 gpa = hva_to_gpa(dev, hva, exp_size); 453 if (!gpa) { 454 VHOST_LOG_DATA(dev->ifname, ERR, 455 "failed to find GPA for log_addr: 0x%" 456 PRIx64 " hva: 0x%" PRIx64 "\n", 457 log_addr, hva); 458 return 0; 459 } 460 return gpa; 461 462 } else 463 return log_addr; 464 } 465 466 /* Caller should have iotlb_lock read-locked */ 467 static int 468 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq) 469 { 470 uint64_t req_size, size; 471 472 req_size = sizeof(struct vring_desc) * vq->size; 473 size = req_size; 474 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq, 475 vq->ring_addrs.desc_user_addr, 476 &size, VHOST_ACCESS_RW); 477 if (!vq->desc || size != req_size) 478 return -1; 479 480 req_size = sizeof(struct vring_avail); 481 req_size += sizeof(uint16_t) * vq->size; 482 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) 483 req_size += sizeof(uint16_t); 484 size = req_size; 485 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq, 486 vq->ring_addrs.avail_user_addr, 487 &size, VHOST_ACCESS_RW); 488 if (!vq->avail || size != req_size) 489 return -1; 490 491 req_size = sizeof(struct vring_used); 492 req_size += sizeof(struct vring_used_elem) * vq->size; 493 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) 494 req_size += sizeof(uint16_t); 495 size = req_size; 496 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq, 497 vq->ring_addrs.used_user_addr, 498 &size, VHOST_ACCESS_RW); 499 if (!vq->used || size != req_size) 500 return -1; 501 502 return 0; 503 } 504 505 /* Caller should have iotlb_lock read-locked */ 506 static int 507 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq) 508 { 509 uint64_t req_size, size; 510 511 req_size = sizeof(struct vring_packed_desc) * vq->size; 512 size = req_size; 513 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t) 514 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr, 515 &size, VHOST_ACCESS_RW); 516 if (!vq->desc_packed || size != req_size) 517 return -1; 518 519 req_size = sizeof(struct vring_packed_desc_event); 520 size = req_size; 521 vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t) 522 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr, 523 &size, VHOST_ACCESS_RW); 524 if (!vq->driver_event || size != req_size) 525 return -1; 526 527 req_size = sizeof(struct vring_packed_desc_event); 528 size = req_size; 529 vq->device_event = (struct vring_packed_desc_event *)(uintptr_t) 530 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr, 531 &size, VHOST_ACCESS_RW); 532 if (!vq->device_event || size != req_size) 533 return -1; 534 535 return 0; 536 } 537 538 int 539 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq) 540 { 541 542 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))) 543 return -1; 544 545 if (vq_is_packed(dev)) { 546 if (vring_translate_packed(dev, vq) < 0) 547 return -1; 548 } else { 549 if (vring_translate_split(dev, vq) < 0) 550 return -1; 551 } 552 553 if (log_translate(dev, vq) < 0) 554 return -1; 555 556 vq->access_ok = true; 557 558 return 0; 559 } 560 561 void 562 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq) 563 { 564 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) 565 vhost_user_iotlb_wr_lock(vq); 566 567 vq->access_ok = false; 568 vq->desc = NULL; 569 vq->avail = NULL; 570 vq->used = NULL; 571 vq->log_guest_addr = 0; 572 573 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) 574 vhost_user_iotlb_wr_unlock(vq); 575 } 576 577 static void 578 init_vring_queue(struct virtio_net *dev, struct vhost_virtqueue *vq, 579 uint32_t vring_idx) 580 { 581 int numa_node = SOCKET_ID_ANY; 582 583 memset(vq, 0, sizeof(struct vhost_virtqueue)); 584 585 vq->index = vring_idx; 586 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD; 587 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD; 588 vq->notif_enable = VIRTIO_UNINITIALIZED_NOTIF; 589 590 #ifdef RTE_LIBRTE_VHOST_NUMA 591 if (get_mempolicy(&numa_node, NULL, 0, vq, MPOL_F_NODE | MPOL_F_ADDR)) { 592 VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to query numa node: %s\n", 593 rte_strerror(errno)); 594 numa_node = SOCKET_ID_ANY; 595 } 596 #endif 597 vq->numa_node = numa_node; 598 599 vhost_user_iotlb_init(dev, vq); 600 } 601 602 static void 603 reset_vring_queue(struct virtio_net *dev, struct vhost_virtqueue *vq) 604 { 605 int callfd; 606 607 callfd = vq->callfd; 608 init_vring_queue(dev, vq, vq->index); 609 vq->callfd = callfd; 610 } 611 612 int 613 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx) 614 { 615 struct vhost_virtqueue *vq; 616 uint32_t i; 617 618 /* Also allocate holes, if any, up to requested vring index. */ 619 for (i = 0; i <= vring_idx; i++) { 620 if (dev->virtqueue[i]) 621 continue; 622 623 vq = rte_zmalloc(NULL, sizeof(struct vhost_virtqueue), 0); 624 if (vq == NULL) { 625 VHOST_LOG_CONFIG(dev->ifname, ERR, 626 "failed to allocate memory for vring %u.\n", 627 i); 628 return -1; 629 } 630 631 dev->virtqueue[i] = vq; 632 init_vring_queue(dev, vq, i); 633 rte_spinlock_init(&vq->access_lock); 634 vq->avail_wrap_counter = 1; 635 vq->used_wrap_counter = 1; 636 vq->signalled_used_valid = false; 637 } 638 639 dev->nr_vring = RTE_MAX(dev->nr_vring, vring_idx + 1); 640 641 return 0; 642 } 643 644 /* 645 * Reset some variables in device structure, while keeping few 646 * others untouched, such as vid, ifname, nr_vring: they 647 * should be same unless the device is removed. 648 */ 649 void 650 reset_device(struct virtio_net *dev) 651 { 652 uint32_t i; 653 654 dev->features = 0; 655 dev->protocol_features = 0; 656 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET; 657 658 for (i = 0; i < dev->nr_vring; i++) { 659 struct vhost_virtqueue *vq = dev->virtqueue[i]; 660 661 if (!vq) { 662 VHOST_LOG_CONFIG(dev->ifname, ERR, 663 "failed to reset vring, virtqueue not allocated (%d)\n", i); 664 continue; 665 } 666 reset_vring_queue(dev, vq); 667 } 668 } 669 670 /* 671 * Invoked when there is a new vhost-user connection established (when 672 * there is a new virtio device being attached). 673 */ 674 int 675 vhost_new_device(void) 676 { 677 struct virtio_net *dev; 678 int i; 679 680 pthread_mutex_lock(&vhost_dev_lock); 681 for (i = 0; i < RTE_MAX_VHOST_DEVICE; i++) { 682 if (vhost_devices[i] == NULL) 683 break; 684 } 685 686 if (i == RTE_MAX_VHOST_DEVICE) { 687 VHOST_LOG_CONFIG("device", ERR, "failed to find a free slot for new device.\n"); 688 pthread_mutex_unlock(&vhost_dev_lock); 689 return -1; 690 } 691 692 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0); 693 if (dev == NULL) { 694 VHOST_LOG_CONFIG("device", ERR, "failed to allocate memory for new device.\n"); 695 pthread_mutex_unlock(&vhost_dev_lock); 696 return -1; 697 } 698 699 vhost_devices[i] = dev; 700 pthread_mutex_unlock(&vhost_dev_lock); 701 702 dev->vid = i; 703 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET; 704 dev->slave_req_fd = -1; 705 dev->postcopy_ufd = -1; 706 rte_spinlock_init(&dev->slave_req_lock); 707 708 return i; 709 } 710 711 void 712 vhost_destroy_device_notify(struct virtio_net *dev) 713 { 714 struct rte_vdpa_device *vdpa_dev; 715 716 if (dev->flags & VIRTIO_DEV_RUNNING) { 717 vdpa_dev = dev->vdpa_dev; 718 if (vdpa_dev) 719 vdpa_dev->ops->dev_close(dev->vid); 720 dev->flags &= ~VIRTIO_DEV_RUNNING; 721 dev->notify_ops->destroy_device(dev->vid); 722 } 723 } 724 725 /* 726 * Invoked when there is the vhost-user connection is broken (when 727 * the virtio device is being detached). 728 */ 729 void 730 vhost_destroy_device(int vid) 731 { 732 struct virtio_net *dev = get_device(vid); 733 734 if (dev == NULL) 735 return; 736 737 vhost_destroy_device_notify(dev); 738 739 cleanup_device(dev, 1); 740 free_device(dev); 741 742 vhost_devices[vid] = NULL; 743 } 744 745 void 746 vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *vdpa_dev) 747 { 748 struct virtio_net *dev = get_device(vid); 749 750 if (dev == NULL) 751 return; 752 753 dev->vdpa_dev = vdpa_dev; 754 } 755 756 void 757 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len) 758 { 759 struct virtio_net *dev; 760 unsigned int len; 761 762 dev = get_device(vid); 763 if (dev == NULL) 764 return; 765 766 len = if_len > sizeof(dev->ifname) ? 767 sizeof(dev->ifname) : if_len; 768 769 strncpy(dev->ifname, if_name, len); 770 dev->ifname[sizeof(dev->ifname) - 1] = '\0'; 771 } 772 773 void 774 vhost_setup_virtio_net(int vid, bool enable, bool compliant_ol_flags, bool stats_enabled, 775 bool support_iommu) 776 { 777 struct virtio_net *dev = get_device(vid); 778 779 if (dev == NULL) 780 return; 781 782 if (enable) 783 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET; 784 else 785 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET; 786 if (!compliant_ol_flags) 787 dev->flags |= VIRTIO_DEV_LEGACY_OL_FLAGS; 788 else 789 dev->flags &= ~VIRTIO_DEV_LEGACY_OL_FLAGS; 790 if (stats_enabled) 791 dev->flags |= VIRTIO_DEV_STATS_ENABLED; 792 else 793 dev->flags &= ~VIRTIO_DEV_STATS_ENABLED; 794 if (support_iommu) 795 dev->flags |= VIRTIO_DEV_SUPPORT_IOMMU; 796 else 797 dev->flags &= ~VIRTIO_DEV_SUPPORT_IOMMU; 798 } 799 800 void 801 vhost_enable_extbuf(int vid) 802 { 803 struct virtio_net *dev = get_device(vid); 804 805 if (dev == NULL) 806 return; 807 808 dev->extbuf = 1; 809 } 810 811 void 812 vhost_enable_linearbuf(int vid) 813 { 814 struct virtio_net *dev = get_device(vid); 815 816 if (dev == NULL) 817 return; 818 819 dev->linearbuf = 1; 820 } 821 822 int 823 rte_vhost_get_mtu(int vid, uint16_t *mtu) 824 { 825 struct virtio_net *dev = get_device(vid); 826 827 if (dev == NULL || mtu == NULL) 828 return -ENODEV; 829 830 if (!(dev->flags & VIRTIO_DEV_READY)) 831 return -EAGAIN; 832 833 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU))) 834 return -ENOTSUP; 835 836 *mtu = dev->mtu; 837 838 return 0; 839 } 840 841 int 842 rte_vhost_get_numa_node(int vid) 843 { 844 #ifdef RTE_LIBRTE_VHOST_NUMA 845 struct virtio_net *dev = get_device(vid); 846 int numa_node; 847 int ret; 848 849 if (dev == NULL || numa_available() != 0) 850 return -1; 851 852 ret = get_mempolicy(&numa_node, NULL, 0, dev, 853 MPOL_F_NODE | MPOL_F_ADDR); 854 if (ret < 0) { 855 VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to query numa node: %s\n", 856 rte_strerror(errno)); 857 return -1; 858 } 859 860 return numa_node; 861 #else 862 RTE_SET_USED(vid); 863 return -1; 864 #endif 865 } 866 867 uint16_t 868 rte_vhost_get_vring_num(int vid) 869 { 870 struct virtio_net *dev = get_device(vid); 871 872 if (dev == NULL) 873 return 0; 874 875 return dev->nr_vring; 876 } 877 878 int 879 rte_vhost_get_ifname(int vid, char *buf, size_t len) 880 { 881 struct virtio_net *dev = get_device(vid); 882 883 if (dev == NULL || buf == NULL) 884 return -1; 885 886 len = RTE_MIN(len, sizeof(dev->ifname)); 887 888 strncpy(buf, dev->ifname, len); 889 buf[len - 1] = '\0'; 890 891 return 0; 892 } 893 894 int 895 rte_vhost_get_negotiated_features(int vid, uint64_t *features) 896 { 897 struct virtio_net *dev; 898 899 dev = get_device(vid); 900 if (dev == NULL || features == NULL) 901 return -1; 902 903 *features = dev->features; 904 return 0; 905 } 906 907 int 908 rte_vhost_get_negotiated_protocol_features(int vid, 909 uint64_t *protocol_features) 910 { 911 struct virtio_net *dev; 912 913 dev = get_device(vid); 914 if (dev == NULL || protocol_features == NULL) 915 return -1; 916 917 *protocol_features = dev->protocol_features; 918 return 0; 919 } 920 921 int 922 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem) 923 { 924 struct virtio_net *dev; 925 struct rte_vhost_memory *m; 926 size_t size; 927 928 dev = get_device(vid); 929 if (dev == NULL || mem == NULL) 930 return -1; 931 932 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region); 933 m = malloc(sizeof(struct rte_vhost_memory) + size); 934 if (!m) 935 return -1; 936 937 m->nregions = dev->mem->nregions; 938 memcpy(m->regions, dev->mem->regions, size); 939 *mem = m; 940 941 return 0; 942 } 943 944 int 945 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx, 946 struct rte_vhost_vring *vring) 947 { 948 struct virtio_net *dev; 949 struct vhost_virtqueue *vq; 950 951 dev = get_device(vid); 952 if (dev == NULL || vring == NULL) 953 return -1; 954 955 if (vring_idx >= VHOST_MAX_VRING) 956 return -1; 957 958 vq = dev->virtqueue[vring_idx]; 959 if (!vq) 960 return -1; 961 962 if (vq_is_packed(dev)) { 963 vring->desc_packed = vq->desc_packed; 964 vring->driver_event = vq->driver_event; 965 vring->device_event = vq->device_event; 966 } else { 967 vring->desc = vq->desc; 968 vring->avail = vq->avail; 969 vring->used = vq->used; 970 } 971 vring->log_guest_addr = vq->log_guest_addr; 972 973 vring->callfd = vq->callfd; 974 vring->kickfd = vq->kickfd; 975 vring->size = vq->size; 976 977 return 0; 978 } 979 980 int 981 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx, 982 struct rte_vhost_ring_inflight *vring) 983 { 984 struct virtio_net *dev; 985 struct vhost_virtqueue *vq; 986 987 dev = get_device(vid); 988 if (unlikely(!dev)) 989 return -1; 990 991 if (vring_idx >= VHOST_MAX_VRING) 992 return -1; 993 994 vq = dev->virtqueue[vring_idx]; 995 if (unlikely(!vq)) 996 return -1; 997 998 if (vq_is_packed(dev)) { 999 if (unlikely(!vq->inflight_packed)) 1000 return -1; 1001 1002 vring->inflight_packed = vq->inflight_packed; 1003 } else { 1004 if (unlikely(!vq->inflight_split)) 1005 return -1; 1006 1007 vring->inflight_split = vq->inflight_split; 1008 } 1009 1010 vring->resubmit_inflight = vq->resubmit_inflight; 1011 1012 return 0; 1013 } 1014 1015 int 1016 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx, 1017 uint16_t idx) 1018 { 1019 struct vhost_virtqueue *vq; 1020 struct virtio_net *dev; 1021 1022 dev = get_device(vid); 1023 if (unlikely(!dev)) 1024 return -1; 1025 1026 if (unlikely(!(dev->protocol_features & 1027 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))) 1028 return 0; 1029 1030 if (unlikely(vq_is_packed(dev))) 1031 return -1; 1032 1033 if (unlikely(vring_idx >= VHOST_MAX_VRING)) 1034 return -1; 1035 1036 vq = dev->virtqueue[vring_idx]; 1037 if (unlikely(!vq)) 1038 return -1; 1039 1040 if (unlikely(!vq->inflight_split)) 1041 return -1; 1042 1043 if (unlikely(idx >= vq->size)) 1044 return -1; 1045 1046 vq->inflight_split->desc[idx].counter = vq->global_counter++; 1047 vq->inflight_split->desc[idx].inflight = 1; 1048 return 0; 1049 } 1050 1051 int 1052 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx, 1053 uint16_t head, uint16_t last, 1054 uint16_t *inflight_entry) 1055 { 1056 struct rte_vhost_inflight_info_packed *inflight_info; 1057 struct virtio_net *dev; 1058 struct vhost_virtqueue *vq; 1059 struct vring_packed_desc *desc; 1060 uint16_t old_free_head, free_head; 1061 1062 dev = get_device(vid); 1063 if (unlikely(!dev)) 1064 return -1; 1065 1066 if (unlikely(!(dev->protocol_features & 1067 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))) 1068 return 0; 1069 1070 if (unlikely(!vq_is_packed(dev))) 1071 return -1; 1072 1073 if (unlikely(vring_idx >= VHOST_MAX_VRING)) 1074 return -1; 1075 1076 vq = dev->virtqueue[vring_idx]; 1077 if (unlikely(!vq)) 1078 return -1; 1079 1080 inflight_info = vq->inflight_packed; 1081 if (unlikely(!inflight_info)) 1082 return -1; 1083 1084 if (unlikely(head >= vq->size)) 1085 return -1; 1086 1087 desc = vq->desc_packed; 1088 old_free_head = inflight_info->old_free_head; 1089 if (unlikely(old_free_head >= vq->size)) 1090 return -1; 1091 1092 free_head = old_free_head; 1093 1094 /* init header descriptor */ 1095 inflight_info->desc[old_free_head].num = 0; 1096 inflight_info->desc[old_free_head].counter = vq->global_counter++; 1097 inflight_info->desc[old_free_head].inflight = 1; 1098 1099 /* save desc entry in flight entry */ 1100 while (head != ((last + 1) % vq->size)) { 1101 inflight_info->desc[old_free_head].num++; 1102 inflight_info->desc[free_head].addr = desc[head].addr; 1103 inflight_info->desc[free_head].len = desc[head].len; 1104 inflight_info->desc[free_head].flags = desc[head].flags; 1105 inflight_info->desc[free_head].id = desc[head].id; 1106 1107 inflight_info->desc[old_free_head].last = free_head; 1108 free_head = inflight_info->desc[free_head].next; 1109 inflight_info->free_head = free_head; 1110 head = (head + 1) % vq->size; 1111 } 1112 1113 inflight_info->old_free_head = free_head; 1114 *inflight_entry = old_free_head; 1115 1116 return 0; 1117 } 1118 1119 int 1120 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx, 1121 uint16_t last_used_idx, uint16_t idx) 1122 { 1123 struct virtio_net *dev; 1124 struct vhost_virtqueue *vq; 1125 1126 dev = get_device(vid); 1127 if (unlikely(!dev)) 1128 return -1; 1129 1130 if (unlikely(!(dev->protocol_features & 1131 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))) 1132 return 0; 1133 1134 if (unlikely(vq_is_packed(dev))) 1135 return -1; 1136 1137 if (unlikely(vring_idx >= VHOST_MAX_VRING)) 1138 return -1; 1139 1140 vq = dev->virtqueue[vring_idx]; 1141 if (unlikely(!vq)) 1142 return -1; 1143 1144 if (unlikely(!vq->inflight_split)) 1145 return -1; 1146 1147 if (unlikely(idx >= vq->size)) 1148 return -1; 1149 1150 rte_atomic_thread_fence(__ATOMIC_SEQ_CST); 1151 1152 vq->inflight_split->desc[idx].inflight = 0; 1153 1154 rte_atomic_thread_fence(__ATOMIC_SEQ_CST); 1155 1156 vq->inflight_split->used_idx = last_used_idx; 1157 return 0; 1158 } 1159 1160 int 1161 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx, 1162 uint16_t head) 1163 { 1164 struct rte_vhost_inflight_info_packed *inflight_info; 1165 struct virtio_net *dev; 1166 struct vhost_virtqueue *vq; 1167 1168 dev = get_device(vid); 1169 if (unlikely(!dev)) 1170 return -1; 1171 1172 if (unlikely(!(dev->protocol_features & 1173 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))) 1174 return 0; 1175 1176 if (unlikely(!vq_is_packed(dev))) 1177 return -1; 1178 1179 if (unlikely(vring_idx >= VHOST_MAX_VRING)) 1180 return -1; 1181 1182 vq = dev->virtqueue[vring_idx]; 1183 if (unlikely(!vq)) 1184 return -1; 1185 1186 inflight_info = vq->inflight_packed; 1187 if (unlikely(!inflight_info)) 1188 return -1; 1189 1190 if (unlikely(head >= vq->size)) 1191 return -1; 1192 1193 rte_atomic_thread_fence(__ATOMIC_SEQ_CST); 1194 1195 inflight_info->desc[head].inflight = 0; 1196 1197 rte_atomic_thread_fence(__ATOMIC_SEQ_CST); 1198 1199 inflight_info->old_free_head = inflight_info->free_head; 1200 inflight_info->old_used_idx = inflight_info->used_idx; 1201 inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter; 1202 1203 return 0; 1204 } 1205 1206 int 1207 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx, 1208 uint16_t idx) 1209 { 1210 struct virtio_net *dev; 1211 struct vhost_virtqueue *vq; 1212 1213 dev = get_device(vid); 1214 if (unlikely(!dev)) 1215 return -1; 1216 1217 if (unlikely(!(dev->protocol_features & 1218 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))) 1219 return 0; 1220 1221 if (unlikely(vq_is_packed(dev))) 1222 return -1; 1223 1224 if (unlikely(vring_idx >= VHOST_MAX_VRING)) 1225 return -1; 1226 1227 vq = dev->virtqueue[vring_idx]; 1228 if (unlikely(!vq)) 1229 return -1; 1230 1231 if (unlikely(!vq->inflight_split)) 1232 return -1; 1233 1234 if (unlikely(idx >= vq->size)) 1235 return -1; 1236 1237 vq->inflight_split->last_inflight_io = idx; 1238 return 0; 1239 } 1240 1241 int 1242 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx, 1243 uint16_t head) 1244 { 1245 struct rte_vhost_inflight_info_packed *inflight_info; 1246 struct virtio_net *dev; 1247 struct vhost_virtqueue *vq; 1248 uint16_t last; 1249 1250 dev = get_device(vid); 1251 if (unlikely(!dev)) 1252 return -1; 1253 1254 if (unlikely(!(dev->protocol_features & 1255 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))) 1256 return 0; 1257 1258 if (unlikely(!vq_is_packed(dev))) 1259 return -1; 1260 1261 if (unlikely(vring_idx >= VHOST_MAX_VRING)) 1262 return -1; 1263 1264 vq = dev->virtqueue[vring_idx]; 1265 if (unlikely(!vq)) 1266 return -1; 1267 1268 inflight_info = vq->inflight_packed; 1269 if (unlikely(!inflight_info)) 1270 return -1; 1271 1272 if (unlikely(head >= vq->size)) 1273 return -1; 1274 1275 last = inflight_info->desc[head].last; 1276 if (unlikely(last >= vq->size)) 1277 return -1; 1278 1279 inflight_info->desc[last].next = inflight_info->free_head; 1280 inflight_info->free_head = head; 1281 inflight_info->used_idx += inflight_info->desc[head].num; 1282 if (inflight_info->used_idx >= inflight_info->desc_num) { 1283 inflight_info->used_idx -= inflight_info->desc_num; 1284 inflight_info->used_wrap_counter = 1285 !inflight_info->used_wrap_counter; 1286 } 1287 1288 return 0; 1289 } 1290 1291 int 1292 rte_vhost_vring_call(int vid, uint16_t vring_idx) 1293 { 1294 struct virtio_net *dev; 1295 struct vhost_virtqueue *vq; 1296 1297 dev = get_device(vid); 1298 if (!dev) 1299 return -1; 1300 1301 if (vring_idx >= VHOST_MAX_VRING) 1302 return -1; 1303 1304 vq = dev->virtqueue[vring_idx]; 1305 if (!vq) 1306 return -1; 1307 1308 rte_spinlock_lock(&vq->access_lock); 1309 1310 if (vq_is_packed(dev)) 1311 vhost_vring_call_packed(dev, vq); 1312 else 1313 vhost_vring_call_split(dev, vq); 1314 1315 rte_spinlock_unlock(&vq->access_lock); 1316 1317 return 0; 1318 } 1319 1320 uint16_t 1321 rte_vhost_avail_entries(int vid, uint16_t queue_id) 1322 { 1323 struct virtio_net *dev; 1324 struct vhost_virtqueue *vq; 1325 uint16_t ret = 0; 1326 1327 dev = get_device(vid); 1328 if (!dev) 1329 return 0; 1330 1331 if (queue_id >= VHOST_MAX_VRING) 1332 return 0; 1333 1334 vq = dev->virtqueue[queue_id]; 1335 if (!vq) 1336 return 0; 1337 1338 rte_spinlock_lock(&vq->access_lock); 1339 1340 if (unlikely(!vq->enabled || vq->avail == NULL)) 1341 goto out; 1342 1343 ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx; 1344 1345 out: 1346 rte_spinlock_unlock(&vq->access_lock); 1347 return ret; 1348 } 1349 1350 static inline int 1351 vhost_enable_notify_split(struct virtio_net *dev, 1352 struct vhost_virtqueue *vq, int enable) 1353 { 1354 if (vq->used == NULL) 1355 return -1; 1356 1357 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) { 1358 if (enable) 1359 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY; 1360 else 1361 vq->used->flags |= VRING_USED_F_NO_NOTIFY; 1362 } else { 1363 if (enable) 1364 vhost_avail_event(vq) = vq->last_avail_idx; 1365 } 1366 return 0; 1367 } 1368 1369 static inline int 1370 vhost_enable_notify_packed(struct virtio_net *dev, 1371 struct vhost_virtqueue *vq, int enable) 1372 { 1373 uint16_t flags; 1374 1375 if (vq->device_event == NULL) 1376 return -1; 1377 1378 if (!enable) { 1379 vq->device_event->flags = VRING_EVENT_F_DISABLE; 1380 return 0; 1381 } 1382 1383 flags = VRING_EVENT_F_ENABLE; 1384 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) { 1385 flags = VRING_EVENT_F_DESC; 1386 vq->device_event->off_wrap = vq->last_avail_idx | 1387 vq->avail_wrap_counter << 15; 1388 } 1389 1390 rte_atomic_thread_fence(__ATOMIC_RELEASE); 1391 1392 vq->device_event->flags = flags; 1393 return 0; 1394 } 1395 1396 int 1397 vhost_enable_guest_notification(struct virtio_net *dev, 1398 struct vhost_virtqueue *vq, int enable) 1399 { 1400 /* 1401 * If the virtqueue is not ready yet, it will be applied 1402 * when it will become ready. 1403 */ 1404 if (!vq->ready) 1405 return 0; 1406 1407 if (vq_is_packed(dev)) 1408 return vhost_enable_notify_packed(dev, vq, enable); 1409 else 1410 return vhost_enable_notify_split(dev, vq, enable); 1411 } 1412 1413 int 1414 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable) 1415 { 1416 struct virtio_net *dev = get_device(vid); 1417 struct vhost_virtqueue *vq; 1418 int ret; 1419 1420 if (!dev) 1421 return -1; 1422 1423 if (queue_id >= VHOST_MAX_VRING) 1424 return -1; 1425 1426 vq = dev->virtqueue[queue_id]; 1427 if (!vq) 1428 return -1; 1429 1430 rte_spinlock_lock(&vq->access_lock); 1431 1432 vq->notif_enable = enable; 1433 ret = vhost_enable_guest_notification(dev, vq, enable); 1434 1435 rte_spinlock_unlock(&vq->access_lock); 1436 1437 return ret; 1438 } 1439 1440 void 1441 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len) 1442 { 1443 struct virtio_net *dev = get_device(vid); 1444 1445 if (dev == NULL) 1446 return; 1447 1448 vhost_log_write(dev, addr, len); 1449 } 1450 1451 void 1452 rte_vhost_log_used_vring(int vid, uint16_t vring_idx, 1453 uint64_t offset, uint64_t len) 1454 { 1455 struct virtio_net *dev; 1456 struct vhost_virtqueue *vq; 1457 1458 dev = get_device(vid); 1459 if (dev == NULL) 1460 return; 1461 1462 if (vring_idx >= VHOST_MAX_VRING) 1463 return; 1464 vq = dev->virtqueue[vring_idx]; 1465 if (!vq) 1466 return; 1467 1468 vhost_log_used_vring(dev, vq, offset, len); 1469 } 1470 1471 uint32_t 1472 rte_vhost_rx_queue_count(int vid, uint16_t qid) 1473 { 1474 struct virtio_net *dev; 1475 struct vhost_virtqueue *vq; 1476 uint32_t ret = 0; 1477 1478 dev = get_device(vid); 1479 if (dev == NULL) 1480 return 0; 1481 1482 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) { 1483 VHOST_LOG_DATA(dev->ifname, ERR, 1484 "%s: invalid virtqueue idx %d.\n", 1485 __func__, qid); 1486 return 0; 1487 } 1488 1489 vq = dev->virtqueue[qid]; 1490 if (vq == NULL) 1491 return 0; 1492 1493 rte_spinlock_lock(&vq->access_lock); 1494 1495 if (unlikely(!vq->enabled || vq->avail == NULL)) 1496 goto out; 1497 1498 ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx; 1499 1500 out: 1501 rte_spinlock_unlock(&vq->access_lock); 1502 return ret; 1503 } 1504 1505 struct rte_vdpa_device * 1506 rte_vhost_get_vdpa_device(int vid) 1507 { 1508 struct virtio_net *dev = get_device(vid); 1509 1510 if (dev == NULL) 1511 return NULL; 1512 1513 return dev->vdpa_dev; 1514 } 1515 1516 int 1517 rte_vhost_get_log_base(int vid, uint64_t *log_base, 1518 uint64_t *log_size) 1519 { 1520 struct virtio_net *dev = get_device(vid); 1521 1522 if (dev == NULL || log_base == NULL || log_size == NULL) 1523 return -1; 1524 1525 *log_base = dev->log_base; 1526 *log_size = dev->log_size; 1527 1528 return 0; 1529 } 1530 1531 int 1532 rte_vhost_get_vring_base(int vid, uint16_t queue_id, 1533 uint16_t *last_avail_idx, uint16_t *last_used_idx) 1534 { 1535 struct vhost_virtqueue *vq; 1536 struct virtio_net *dev = get_device(vid); 1537 1538 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL) 1539 return -1; 1540 1541 if (queue_id >= VHOST_MAX_VRING) 1542 return -1; 1543 1544 vq = dev->virtqueue[queue_id]; 1545 if (!vq) 1546 return -1; 1547 1548 if (vq_is_packed(dev)) { 1549 *last_avail_idx = (vq->avail_wrap_counter << 15) | 1550 vq->last_avail_idx; 1551 *last_used_idx = (vq->used_wrap_counter << 15) | 1552 vq->last_used_idx; 1553 } else { 1554 *last_avail_idx = vq->last_avail_idx; 1555 *last_used_idx = vq->last_used_idx; 1556 } 1557 1558 return 0; 1559 } 1560 1561 int 1562 rte_vhost_set_vring_base(int vid, uint16_t queue_id, 1563 uint16_t last_avail_idx, uint16_t last_used_idx) 1564 { 1565 struct vhost_virtqueue *vq; 1566 struct virtio_net *dev = get_device(vid); 1567 1568 if (!dev) 1569 return -1; 1570 1571 if (queue_id >= VHOST_MAX_VRING) 1572 return -1; 1573 1574 vq = dev->virtqueue[queue_id]; 1575 if (!vq) 1576 return -1; 1577 1578 if (vq_is_packed(dev)) { 1579 vq->last_avail_idx = last_avail_idx & 0x7fff; 1580 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15)); 1581 vq->last_used_idx = last_used_idx & 0x7fff; 1582 vq->used_wrap_counter = !!(last_used_idx & (1 << 15)); 1583 } else { 1584 vq->last_avail_idx = last_avail_idx; 1585 vq->last_used_idx = last_used_idx; 1586 } 1587 1588 return 0; 1589 } 1590 1591 int 1592 rte_vhost_get_vring_base_from_inflight(int vid, 1593 uint16_t queue_id, 1594 uint16_t *last_avail_idx, 1595 uint16_t *last_used_idx) 1596 { 1597 struct rte_vhost_inflight_info_packed *inflight_info; 1598 struct vhost_virtqueue *vq; 1599 struct virtio_net *dev = get_device(vid); 1600 1601 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL) 1602 return -1; 1603 1604 if (queue_id >= VHOST_MAX_VRING) 1605 return -1; 1606 1607 vq = dev->virtqueue[queue_id]; 1608 if (!vq) 1609 return -1; 1610 1611 if (!vq_is_packed(dev)) 1612 return -1; 1613 1614 inflight_info = vq->inflight_packed; 1615 if (!inflight_info) 1616 return -1; 1617 1618 *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) | 1619 inflight_info->old_used_idx; 1620 *last_used_idx = *last_avail_idx; 1621 1622 return 0; 1623 } 1624 1625 int 1626 rte_vhost_extern_callback_register(int vid, 1627 struct rte_vhost_user_extern_ops const * const ops, void *ctx) 1628 { 1629 struct virtio_net *dev = get_device(vid); 1630 1631 if (dev == NULL || ops == NULL) 1632 return -1; 1633 1634 dev->extern_ops = *ops; 1635 dev->extern_data = ctx; 1636 return 0; 1637 } 1638 1639 static __rte_always_inline int 1640 async_channel_register(struct virtio_net *dev, struct vhost_virtqueue *vq) 1641 { 1642 struct vhost_async *async; 1643 int node = vq->numa_node; 1644 1645 if (unlikely(vq->async)) { 1646 VHOST_LOG_CONFIG(dev->ifname, ERR, 1647 "async register failed: already registered (qid: %d)\n", 1648 vq->index); 1649 return -1; 1650 } 1651 1652 async = rte_zmalloc_socket(NULL, sizeof(struct vhost_async), 0, node); 1653 if (!async) { 1654 VHOST_LOG_CONFIG(dev->ifname, ERR, 1655 "failed to allocate async metadata (qid: %d)\n", 1656 vq->index); 1657 return -1; 1658 } 1659 1660 async->pkts_info = rte_malloc_socket(NULL, vq->size * sizeof(struct async_inflight_info), 1661 RTE_CACHE_LINE_SIZE, node); 1662 if (!async->pkts_info) { 1663 VHOST_LOG_CONFIG(dev->ifname, ERR, 1664 "failed to allocate async_pkts_info (qid: %d)\n", 1665 vq->index); 1666 goto out_free_async; 1667 } 1668 1669 async->pkts_cmpl_flag = rte_zmalloc_socket(NULL, vq->size * sizeof(bool), 1670 RTE_CACHE_LINE_SIZE, node); 1671 if (!async->pkts_cmpl_flag) { 1672 VHOST_LOG_CONFIG(dev->ifname, ERR, 1673 "failed to allocate async pkts_cmpl_flag (qid: %d)\n", 1674 vq->index); 1675 goto out_free_async; 1676 } 1677 1678 if (vq_is_packed(dev)) { 1679 async->buffers_packed = rte_malloc_socket(NULL, 1680 vq->size * sizeof(struct vring_used_elem_packed), 1681 RTE_CACHE_LINE_SIZE, node); 1682 if (!async->buffers_packed) { 1683 VHOST_LOG_CONFIG(dev->ifname, ERR, 1684 "failed to allocate async buffers (qid: %d)\n", 1685 vq->index); 1686 goto out_free_inflight; 1687 } 1688 } else { 1689 async->descs_split = rte_malloc_socket(NULL, 1690 vq->size * sizeof(struct vring_used_elem), 1691 RTE_CACHE_LINE_SIZE, node); 1692 if (!async->descs_split) { 1693 VHOST_LOG_CONFIG(dev->ifname, ERR, 1694 "failed to allocate async descs (qid: %d)\n", 1695 vq->index); 1696 goto out_free_inflight; 1697 } 1698 } 1699 1700 vq->async = async; 1701 1702 return 0; 1703 out_free_inflight: 1704 rte_free(async->pkts_info); 1705 out_free_async: 1706 rte_free(async); 1707 1708 return -1; 1709 } 1710 1711 int 1712 rte_vhost_async_channel_register(int vid, uint16_t queue_id) 1713 { 1714 struct vhost_virtqueue *vq; 1715 struct virtio_net *dev = get_device(vid); 1716 int ret; 1717 1718 if (dev == NULL) 1719 return -1; 1720 1721 if (queue_id >= VHOST_MAX_VRING) 1722 return -1; 1723 1724 vq = dev->virtqueue[queue_id]; 1725 1726 if (unlikely(vq == NULL || !dev->async_copy)) 1727 return -1; 1728 1729 rte_spinlock_lock(&vq->access_lock); 1730 ret = async_channel_register(dev, vq); 1731 rte_spinlock_unlock(&vq->access_lock); 1732 1733 return ret; 1734 } 1735 1736 int 1737 rte_vhost_async_channel_register_thread_unsafe(int vid, uint16_t queue_id) 1738 { 1739 struct vhost_virtqueue *vq; 1740 struct virtio_net *dev = get_device(vid); 1741 1742 if (dev == NULL) 1743 return -1; 1744 1745 if (queue_id >= VHOST_MAX_VRING) 1746 return -1; 1747 1748 vq = dev->virtqueue[queue_id]; 1749 1750 if (unlikely(vq == NULL || !dev->async_copy)) 1751 return -1; 1752 1753 if (unlikely(!rte_spinlock_is_locked(&vq->access_lock))) { 1754 VHOST_LOG_CONFIG(dev->ifname, ERR, "%s() called without access lock taken.\n", 1755 __func__); 1756 return -1; 1757 } 1758 1759 return async_channel_register(dev, vq); 1760 } 1761 1762 int 1763 rte_vhost_async_channel_unregister(int vid, uint16_t queue_id) 1764 { 1765 struct vhost_virtqueue *vq; 1766 struct virtio_net *dev = get_device(vid); 1767 int ret = -1; 1768 1769 if (dev == NULL) 1770 return ret; 1771 1772 if (queue_id >= VHOST_MAX_VRING) 1773 return ret; 1774 1775 vq = dev->virtqueue[queue_id]; 1776 1777 if (vq == NULL) 1778 return ret; 1779 1780 if (!rte_spinlock_trylock(&vq->access_lock)) { 1781 VHOST_LOG_CONFIG(dev->ifname, ERR, 1782 "failed to unregister async channel, virtqueue busy.\n"); 1783 return ret; 1784 } 1785 1786 if (!vq->async) { 1787 ret = 0; 1788 } else if (vq->async->pkts_inflight_n) { 1789 VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to unregister async channel.\n"); 1790 VHOST_LOG_CONFIG(dev->ifname, ERR, 1791 "inflight packets must be completed before unregistration.\n"); 1792 } else { 1793 vhost_free_async_mem(vq); 1794 ret = 0; 1795 } 1796 1797 rte_spinlock_unlock(&vq->access_lock); 1798 1799 return ret; 1800 } 1801 1802 int 1803 rte_vhost_async_channel_unregister_thread_unsafe(int vid, uint16_t queue_id) 1804 { 1805 struct vhost_virtqueue *vq; 1806 struct virtio_net *dev = get_device(vid); 1807 1808 if (dev == NULL) 1809 return -1; 1810 1811 if (queue_id >= VHOST_MAX_VRING) 1812 return -1; 1813 1814 vq = dev->virtqueue[queue_id]; 1815 1816 if (vq == NULL) 1817 return -1; 1818 1819 if (unlikely(!rte_spinlock_is_locked(&vq->access_lock))) { 1820 VHOST_LOG_CONFIG(dev->ifname, ERR, "%s() called without access lock taken.\n", 1821 __func__); 1822 return -1; 1823 } 1824 1825 if (!vq->async) 1826 return 0; 1827 1828 if (vq->async->pkts_inflight_n) { 1829 VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to unregister async channel.\n"); 1830 VHOST_LOG_CONFIG(dev->ifname, ERR, 1831 "inflight packets must be completed before unregistration.\n"); 1832 return -1; 1833 } 1834 1835 vhost_free_async_mem(vq); 1836 1837 return 0; 1838 } 1839 1840 int 1841 rte_vhost_async_dma_configure(int16_t dma_id, uint16_t vchan_id) 1842 { 1843 struct rte_dma_info info; 1844 void *pkts_cmpl_flag_addr; 1845 uint16_t max_desc; 1846 1847 if (!rte_dma_is_valid(dma_id)) { 1848 VHOST_LOG_CONFIG("dma", ERR, "DMA %d is not found.\n", dma_id); 1849 return -1; 1850 } 1851 1852 if (rte_dma_info_get(dma_id, &info) != 0) { 1853 VHOST_LOG_CONFIG("dma", ERR, "Fail to get DMA %d information.\n", dma_id); 1854 return -1; 1855 } 1856 1857 if (vchan_id >= info.max_vchans) { 1858 VHOST_LOG_CONFIG("dma", ERR, "Invalid DMA %d vChannel %u.\n", dma_id, vchan_id); 1859 return -1; 1860 } 1861 1862 if (!dma_copy_track[dma_id].vchans) { 1863 struct async_dma_vchan_info *vchans; 1864 1865 vchans = rte_zmalloc(NULL, sizeof(struct async_dma_vchan_info) * info.max_vchans, 1866 RTE_CACHE_LINE_SIZE); 1867 if (vchans == NULL) { 1868 VHOST_LOG_CONFIG("dma", ERR, 1869 "Failed to allocate vchans for DMA %d vChannel %u.\n", 1870 dma_id, vchan_id); 1871 return -1; 1872 } 1873 1874 dma_copy_track[dma_id].vchans = vchans; 1875 } 1876 1877 if (dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr) { 1878 VHOST_LOG_CONFIG("dma", INFO, "DMA %d vChannel %u already registered.\n", 1879 dma_id, vchan_id); 1880 return 0; 1881 } 1882 1883 max_desc = info.max_desc; 1884 if (!rte_is_power_of_2(max_desc)) 1885 max_desc = rte_align32pow2(max_desc); 1886 1887 pkts_cmpl_flag_addr = rte_zmalloc(NULL, sizeof(bool *) * max_desc, RTE_CACHE_LINE_SIZE); 1888 if (!pkts_cmpl_flag_addr) { 1889 VHOST_LOG_CONFIG("dma", ERR, 1890 "Failed to allocate pkts_cmpl_flag_addr for DMA %d vChannel %u.\n", 1891 dma_id, vchan_id); 1892 1893 if (dma_copy_track[dma_id].nr_vchans == 0) { 1894 rte_free(dma_copy_track[dma_id].vchans); 1895 dma_copy_track[dma_id].vchans = NULL; 1896 } 1897 return -1; 1898 } 1899 1900 dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr = pkts_cmpl_flag_addr; 1901 dma_copy_track[dma_id].vchans[vchan_id].ring_size = max_desc; 1902 dma_copy_track[dma_id].vchans[vchan_id].ring_mask = max_desc - 1; 1903 dma_copy_track[dma_id].nr_vchans++; 1904 1905 return 0; 1906 } 1907 1908 int 1909 rte_vhost_async_get_inflight(int vid, uint16_t queue_id) 1910 { 1911 struct vhost_virtqueue *vq; 1912 struct virtio_net *dev = get_device(vid); 1913 int ret = -1; 1914 1915 if (dev == NULL) 1916 return ret; 1917 1918 if (queue_id >= VHOST_MAX_VRING) 1919 return ret; 1920 1921 vq = dev->virtqueue[queue_id]; 1922 1923 if (vq == NULL) 1924 return ret; 1925 1926 if (!rte_spinlock_trylock(&vq->access_lock)) { 1927 VHOST_LOG_CONFIG(dev->ifname, DEBUG, 1928 "failed to check in-flight packets. virtqueue busy.\n"); 1929 return ret; 1930 } 1931 1932 if (vq->async) 1933 ret = vq->async->pkts_inflight_n; 1934 1935 rte_spinlock_unlock(&vq->access_lock); 1936 1937 return ret; 1938 } 1939 1940 int 1941 rte_vhost_async_get_inflight_thread_unsafe(int vid, uint16_t queue_id) 1942 { 1943 struct vhost_virtqueue *vq; 1944 struct virtio_net *dev = get_device(vid); 1945 int ret = -1; 1946 1947 if (dev == NULL) 1948 return ret; 1949 1950 if (queue_id >= VHOST_MAX_VRING) 1951 return ret; 1952 1953 vq = dev->virtqueue[queue_id]; 1954 1955 if (vq == NULL) 1956 return ret; 1957 1958 if (unlikely(!rte_spinlock_is_locked(&vq->access_lock))) { 1959 VHOST_LOG_CONFIG(dev->ifname, ERR, "%s() called without access lock taken.\n", 1960 __func__); 1961 return -1; 1962 } 1963 1964 if (!vq->async) 1965 return ret; 1966 1967 ret = vq->async->pkts_inflight_n; 1968 1969 return ret; 1970 } 1971 1972 int 1973 rte_vhost_get_monitor_addr(int vid, uint16_t queue_id, 1974 struct rte_vhost_power_monitor_cond *pmc) 1975 { 1976 struct virtio_net *dev = get_device(vid); 1977 struct vhost_virtqueue *vq; 1978 1979 if (dev == NULL) 1980 return -1; 1981 if (queue_id >= VHOST_MAX_VRING) 1982 return -1; 1983 1984 vq = dev->virtqueue[queue_id]; 1985 if (vq == NULL) 1986 return -1; 1987 1988 if (vq_is_packed(dev)) { 1989 struct vring_packed_desc *desc; 1990 desc = vq->desc_packed; 1991 pmc->addr = &desc[vq->last_avail_idx].flags; 1992 if (vq->avail_wrap_counter) 1993 pmc->val = VRING_DESC_F_AVAIL; 1994 else 1995 pmc->val = VRING_DESC_F_USED; 1996 pmc->mask = VRING_DESC_F_AVAIL | VRING_DESC_F_USED; 1997 pmc->size = sizeof(desc[vq->last_avail_idx].flags); 1998 pmc->match = 1; 1999 } else { 2000 pmc->addr = &vq->avail->idx; 2001 pmc->val = vq->last_avail_idx & (vq->size - 1); 2002 pmc->mask = vq->size - 1; 2003 pmc->size = sizeof(vq->avail->idx); 2004 pmc->match = 0; 2005 } 2006 2007 return 0; 2008 } 2009 2010 2011 int 2012 rte_vhost_vring_stats_get_names(int vid, uint16_t queue_id, 2013 struct rte_vhost_stat_name *name, unsigned int size) 2014 { 2015 struct virtio_net *dev = get_device(vid); 2016 unsigned int i; 2017 2018 if (dev == NULL) 2019 return -1; 2020 2021 if (queue_id >= dev->nr_vring) 2022 return -1; 2023 2024 if (!(dev->flags & VIRTIO_DEV_STATS_ENABLED)) 2025 return -1; 2026 2027 if (name == NULL || size < VHOST_NB_VQ_STATS) 2028 return VHOST_NB_VQ_STATS; 2029 2030 for (i = 0; i < VHOST_NB_VQ_STATS; i++) 2031 snprintf(name[i].name, sizeof(name[i].name), "%s_q%u_%s", 2032 (queue_id & 1) ? "rx" : "tx", 2033 queue_id / 2, vhost_vq_stat_strings[i].name); 2034 2035 return VHOST_NB_VQ_STATS; 2036 } 2037 2038 int 2039 rte_vhost_vring_stats_get(int vid, uint16_t queue_id, 2040 struct rte_vhost_stat *stats, unsigned int n) 2041 { 2042 struct virtio_net *dev = get_device(vid); 2043 struct vhost_virtqueue *vq; 2044 unsigned int i; 2045 2046 if (dev == NULL) 2047 return -1; 2048 2049 if (queue_id >= dev->nr_vring) 2050 return -1; 2051 2052 if (!(dev->flags & VIRTIO_DEV_STATS_ENABLED)) 2053 return -1; 2054 2055 if (stats == NULL || n < VHOST_NB_VQ_STATS) 2056 return VHOST_NB_VQ_STATS; 2057 2058 vq = dev->virtqueue[queue_id]; 2059 2060 rte_spinlock_lock(&vq->access_lock); 2061 for (i = 0; i < VHOST_NB_VQ_STATS; i++) { 2062 stats[i].value = 2063 *(uint64_t *)(((char *)vq) + vhost_vq_stat_strings[i].offset); 2064 stats[i].id = i; 2065 } 2066 rte_spinlock_unlock(&vq->access_lock); 2067 2068 return VHOST_NB_VQ_STATS; 2069 } 2070 2071 int rte_vhost_vring_stats_reset(int vid, uint16_t queue_id) 2072 { 2073 struct virtio_net *dev = get_device(vid); 2074 struct vhost_virtqueue *vq; 2075 2076 if (dev == NULL) 2077 return -1; 2078 2079 if (queue_id >= dev->nr_vring) 2080 return -1; 2081 2082 if (!(dev->flags & VIRTIO_DEV_STATS_ENABLED)) 2083 return -1; 2084 2085 vq = dev->virtqueue[queue_id]; 2086 2087 rte_spinlock_lock(&vq->access_lock); 2088 memset(&vq->stats, 0, sizeof(vq->stats)); 2089 rte_spinlock_unlock(&vq->access_lock); 2090 2091 return 0; 2092 } 2093 2094 RTE_LOG_REGISTER_SUFFIX(vhost_config_log_level, config, INFO); 2095 RTE_LOG_REGISTER_SUFFIX(vhost_data_log_level, data, WARNING); 2096