1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2018 Intel Corporation 3 */ 4 5 /* Security model 6 * -------------- 7 * The vhost-user protocol connection is an external interface, so it must be 8 * robust against invalid inputs. 9 * 10 * This is important because the vhost-user master is only one step removed 11 * from the guest. Malicious guests that have escaped will then launch further 12 * attacks from the vhost-user master. 13 * 14 * Even in deployments where guests are trusted, a bug in the vhost-user master 15 * can still cause invalid messages to be sent. Such messages must not 16 * compromise the stability of the DPDK application by causing crashes, memory 17 * corruption, or other problematic behavior. 18 * 19 * Do not assume received VhostUserMsg fields contain sensible values! 20 */ 21 22 #include <stdint.h> 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include <unistd.h> 27 #include <fcntl.h> 28 #include <sys/ioctl.h> 29 #include <sys/mman.h> 30 #include <sys/stat.h> 31 #include <sys/syscall.h> 32 #ifdef RTE_LIBRTE_VHOST_NUMA 33 #include <numaif.h> 34 #endif 35 #ifdef RTE_LIBRTE_VHOST_POSTCOPY 36 #include <linux/userfaultfd.h> 37 #endif 38 #ifdef F_ADD_SEALS /* if file sealing is supported, so is memfd */ 39 #include <linux/memfd.h> 40 #define MEMFD_SUPPORTED 41 #endif 42 43 #include <rte_common.h> 44 #include <rte_malloc.h> 45 #include <rte_log.h> 46 #include <rte_vfio.h> 47 #include <rte_errno.h> 48 49 #include "iotlb.h" 50 #include "vhost.h" 51 #include "vhost_user.h" 52 53 #define VIRTIO_MIN_MTU 68 54 #define VIRTIO_MAX_MTU 65535 55 56 #define INFLIGHT_ALIGNMENT 64 57 #define INFLIGHT_VERSION 0x1 58 59 typedef struct vhost_message_handler { 60 const char *description; 61 int (*callback)(struct virtio_net **pdev, struct vhu_msg_context *ctx, 62 int main_fd); 63 bool accepts_fd; 64 } vhost_message_handler_t; 65 static vhost_message_handler_t vhost_message_handlers[]; 66 67 static int send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx); 68 static int read_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx); 69 70 static void 71 close_msg_fds(struct vhu_msg_context *ctx) 72 { 73 int i; 74 75 for (i = 0; i < ctx->fd_num; i++) { 76 int fd = ctx->fds[i]; 77 78 if (fd == -1) 79 continue; 80 81 ctx->fds[i] = -1; 82 close(fd); 83 } 84 } 85 86 /* 87 * Ensure the expected number of FDs is received, 88 * close all FDs and return an error if this is not the case. 89 */ 90 static int 91 validate_msg_fds(struct virtio_net *dev, struct vhu_msg_context *ctx, int expected_fds) 92 { 93 if (ctx->fd_num == expected_fds) 94 return 0; 95 96 VHOST_LOG_CONFIG(dev->ifname, ERR, 97 "expect %d FDs for request %s, received %d\n", 98 expected_fds, vhost_message_handlers[ctx->msg.request.master].description, 99 ctx->fd_num); 100 101 close_msg_fds(ctx); 102 103 return -1; 104 } 105 106 static uint64_t 107 get_blk_size(int fd) 108 { 109 struct stat stat; 110 int ret; 111 112 ret = fstat(fd, &stat); 113 return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize; 114 } 115 116 static void 117 async_dma_map(struct virtio_net *dev, bool do_map) 118 { 119 int ret = 0; 120 uint32_t i; 121 struct guest_page *page; 122 123 if (do_map) { 124 for (i = 0; i < dev->nr_guest_pages; i++) { 125 page = &dev->guest_pages[i]; 126 ret = rte_vfio_container_dma_map(RTE_VFIO_DEFAULT_CONTAINER_FD, 127 page->host_user_addr, 128 page->host_iova, 129 page->size); 130 if (ret) { 131 /* 132 * DMA device may bind with kernel driver, in this case, 133 * we don't need to program IOMMU manually. However, if no 134 * device is bound with vfio/uio in DPDK, and vfio kernel 135 * module is loaded, the API will still be called and return 136 * with ENODEV. 137 * 138 * DPDK vfio only returns ENODEV in very similar situations 139 * (vfio either unsupported, or supported but no devices found). 140 * Either way, no mappings could be performed. We treat it as 141 * normal case in async path. This is a workaround. 142 */ 143 if (rte_errno == ENODEV) 144 return; 145 146 /* DMA mapping errors won't stop VHOST_USER_SET_MEM_TABLE. */ 147 VHOST_LOG_CONFIG(dev->ifname, ERR, "DMA engine map failed\n"); 148 } 149 } 150 151 } else { 152 for (i = 0; i < dev->nr_guest_pages; i++) { 153 page = &dev->guest_pages[i]; 154 ret = rte_vfio_container_dma_unmap(RTE_VFIO_DEFAULT_CONTAINER_FD, 155 page->host_user_addr, 156 page->host_iova, 157 page->size); 158 if (ret) { 159 /* like DMA map, ignore the kernel driver case when unmap. */ 160 if (rte_errno == EINVAL) 161 return; 162 163 VHOST_LOG_CONFIG(dev->ifname, ERR, "DMA engine unmap failed\n"); 164 } 165 } 166 } 167 } 168 169 static void 170 free_mem_region(struct virtio_net *dev) 171 { 172 uint32_t i; 173 struct rte_vhost_mem_region *reg; 174 175 if (!dev || !dev->mem) 176 return; 177 178 if (dev->async_copy && rte_vfio_is_enabled("vfio")) 179 async_dma_map(dev, false); 180 181 for (i = 0; i < dev->mem->nregions; i++) { 182 reg = &dev->mem->regions[i]; 183 if (reg->host_user_addr) { 184 munmap(reg->mmap_addr, reg->mmap_size); 185 close(reg->fd); 186 } 187 } 188 } 189 190 void 191 vhost_backend_cleanup(struct virtio_net *dev) 192 { 193 struct rte_vdpa_device *vdpa_dev; 194 195 vdpa_dev = dev->vdpa_dev; 196 if (vdpa_dev && vdpa_dev->ops->dev_cleanup != NULL) 197 vdpa_dev->ops->dev_cleanup(dev->vid); 198 199 if (dev->mem) { 200 free_mem_region(dev); 201 rte_free(dev->mem); 202 dev->mem = NULL; 203 } 204 205 rte_free(dev->guest_pages); 206 dev->guest_pages = NULL; 207 208 if (dev->log_addr) { 209 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size); 210 dev->log_addr = 0; 211 } 212 213 if (dev->inflight_info) { 214 if (dev->inflight_info->addr) { 215 munmap(dev->inflight_info->addr, 216 dev->inflight_info->size); 217 dev->inflight_info->addr = NULL; 218 } 219 220 if (dev->inflight_info->fd >= 0) { 221 close(dev->inflight_info->fd); 222 dev->inflight_info->fd = -1; 223 } 224 225 rte_free(dev->inflight_info); 226 dev->inflight_info = NULL; 227 } 228 229 if (dev->slave_req_fd >= 0) { 230 close(dev->slave_req_fd); 231 dev->slave_req_fd = -1; 232 } 233 234 if (dev->postcopy_ufd >= 0) { 235 close(dev->postcopy_ufd); 236 dev->postcopy_ufd = -1; 237 } 238 239 dev->postcopy_listening = 0; 240 } 241 242 static void 243 vhost_user_notify_queue_state(struct virtio_net *dev, struct vhost_virtqueue *vq, 244 int enable) 245 { 246 struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev; 247 248 /* Configure guest notifications on enable */ 249 if (enable && vq->notif_enable != VIRTIO_UNINITIALIZED_NOTIF) 250 vhost_enable_guest_notification(dev, vq, vq->notif_enable); 251 252 if (vdpa_dev && vdpa_dev->ops->set_vring_state) 253 vdpa_dev->ops->set_vring_state(dev->vid, vq->index, enable); 254 255 if (dev->notify_ops->vring_state_changed) 256 dev->notify_ops->vring_state_changed(dev->vid, vq->index, enable); 257 } 258 259 /* 260 * This function just returns success at the moment unless 261 * the device hasn't been initialised. 262 */ 263 static int 264 vhost_user_set_owner(struct virtio_net **pdev __rte_unused, 265 struct vhu_msg_context *ctx __rte_unused, 266 int main_fd __rte_unused) 267 { 268 return RTE_VHOST_MSG_RESULT_OK; 269 } 270 271 static int 272 vhost_user_reset_owner(struct virtio_net **pdev, 273 struct vhu_msg_context *ctx __rte_unused, 274 int main_fd __rte_unused) 275 { 276 struct virtio_net *dev = *pdev; 277 278 vhost_destroy_device_notify(dev); 279 280 cleanup_device(dev, 0); 281 reset_device(dev); 282 return RTE_VHOST_MSG_RESULT_OK; 283 } 284 285 /* 286 * The features that we support are requested. 287 */ 288 static int 289 vhost_user_get_features(struct virtio_net **pdev, 290 struct vhu_msg_context *ctx, 291 int main_fd __rte_unused) 292 { 293 struct virtio_net *dev = *pdev; 294 uint64_t features = 0; 295 296 rte_vhost_driver_get_features(dev->ifname, &features); 297 298 ctx->msg.payload.u64 = features; 299 ctx->msg.size = sizeof(ctx->msg.payload.u64); 300 ctx->fd_num = 0; 301 302 return RTE_VHOST_MSG_RESULT_REPLY; 303 } 304 305 /* 306 * The queue number that we support are requested. 307 */ 308 static int 309 vhost_user_get_queue_num(struct virtio_net **pdev, 310 struct vhu_msg_context *ctx, 311 int main_fd __rte_unused) 312 { 313 struct virtio_net *dev = *pdev; 314 uint32_t queue_num = 0; 315 316 rte_vhost_driver_get_queue_num(dev->ifname, &queue_num); 317 318 ctx->msg.payload.u64 = (uint64_t)queue_num; 319 ctx->msg.size = sizeof(ctx->msg.payload.u64); 320 ctx->fd_num = 0; 321 322 return RTE_VHOST_MSG_RESULT_REPLY; 323 } 324 325 /* 326 * We receive the negotiated features supported by us and the virtio device. 327 */ 328 static int 329 vhost_user_set_features(struct virtio_net **pdev, 330 struct vhu_msg_context *ctx, 331 int main_fd __rte_unused) 332 { 333 struct virtio_net *dev = *pdev; 334 uint64_t features = ctx->msg.payload.u64; 335 uint64_t vhost_features = 0; 336 struct rte_vdpa_device *vdpa_dev; 337 338 rte_vhost_driver_get_features(dev->ifname, &vhost_features); 339 if (features & ~vhost_features) { 340 VHOST_LOG_CONFIG(dev->ifname, ERR, "received invalid negotiated features.\n"); 341 dev->flags |= VIRTIO_DEV_FEATURES_FAILED; 342 dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK; 343 344 return RTE_VHOST_MSG_RESULT_ERR; 345 } 346 347 if (dev->flags & VIRTIO_DEV_RUNNING) { 348 if (dev->features == features) 349 return RTE_VHOST_MSG_RESULT_OK; 350 351 /* 352 * Error out if master tries to change features while device is 353 * in running state. The exception being VHOST_F_LOG_ALL, which 354 * is enabled when the live-migration starts. 355 */ 356 if ((dev->features ^ features) & ~(1ULL << VHOST_F_LOG_ALL)) { 357 VHOST_LOG_CONFIG(dev->ifname, ERR, 358 "features changed while device is running.\n"); 359 return RTE_VHOST_MSG_RESULT_ERR; 360 } 361 362 if (dev->notify_ops->features_changed) 363 dev->notify_ops->features_changed(dev->vid, features); 364 } 365 366 dev->features = features; 367 if (dev->features & 368 ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | 369 (1ULL << VIRTIO_F_VERSION_1) | 370 (1ULL << VIRTIO_F_RING_PACKED))) { 371 dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf); 372 } else { 373 dev->vhost_hlen = sizeof(struct virtio_net_hdr); 374 } 375 VHOST_LOG_CONFIG(dev->ifname, INFO, 376 "negotiated Virtio features: 0x%" PRIx64 "\n", 377 dev->features); 378 VHOST_LOG_CONFIG(dev->ifname, DEBUG, 379 "mergeable RX buffers %s, virtio 1 %s\n", 380 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off", 381 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off"); 382 383 if ((dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) && 384 !(dev->features & (1ULL << VIRTIO_NET_F_MQ))) { 385 /* 386 * Remove all but first queue pair if MQ hasn't been 387 * negotiated. This is safe because the device is not 388 * running at this stage. 389 */ 390 while (dev->nr_vring > 2) { 391 struct vhost_virtqueue *vq; 392 393 vq = dev->virtqueue[--dev->nr_vring]; 394 if (!vq) 395 continue; 396 397 dev->virtqueue[dev->nr_vring] = NULL; 398 cleanup_vq(vq, 1); 399 cleanup_vq_inflight(dev, vq); 400 free_vq(dev, vq); 401 } 402 } 403 404 vdpa_dev = dev->vdpa_dev; 405 if (vdpa_dev) 406 vdpa_dev->ops->set_features(dev->vid); 407 408 dev->flags &= ~VIRTIO_DEV_FEATURES_FAILED; 409 return RTE_VHOST_MSG_RESULT_OK; 410 } 411 412 /* 413 * The virtio device sends us the size of the descriptor ring. 414 */ 415 static int 416 vhost_user_set_vring_num(struct virtio_net **pdev, 417 struct vhu_msg_context *ctx, 418 int main_fd __rte_unused) 419 { 420 struct virtio_net *dev = *pdev; 421 struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index]; 422 423 if (ctx->msg.payload.state.num > 32768) { 424 VHOST_LOG_CONFIG(dev->ifname, ERR, 425 "invalid virtqueue size %u\n", 426 ctx->msg.payload.state.num); 427 return RTE_VHOST_MSG_RESULT_ERR; 428 } 429 430 vq->size = ctx->msg.payload.state.num; 431 432 /* VIRTIO 1.0, 2.4 Virtqueues says: 433 * 434 * Queue Size value is always a power of 2. The maximum Queue Size 435 * value is 32768. 436 * 437 * VIRTIO 1.1 2.7 Virtqueues says: 438 * 439 * Packed virtqueues support up to 2^15 entries each. 440 */ 441 if (!vq_is_packed(dev)) { 442 if (vq->size & (vq->size - 1)) { 443 VHOST_LOG_CONFIG(dev->ifname, ERR, 444 "invalid virtqueue size %u\n", 445 vq->size); 446 return RTE_VHOST_MSG_RESULT_ERR; 447 } 448 } 449 450 if (vq_is_packed(dev)) { 451 rte_free(vq->shadow_used_packed); 452 vq->shadow_used_packed = rte_malloc_socket(NULL, 453 vq->size * 454 sizeof(struct vring_used_elem_packed), 455 RTE_CACHE_LINE_SIZE, vq->numa_node); 456 if (!vq->shadow_used_packed) { 457 VHOST_LOG_CONFIG(dev->ifname, ERR, 458 "failed to allocate memory for shadow used ring.\n"); 459 return RTE_VHOST_MSG_RESULT_ERR; 460 } 461 462 } else { 463 rte_free(vq->shadow_used_split); 464 465 vq->shadow_used_split = rte_malloc_socket(NULL, 466 vq->size * sizeof(struct vring_used_elem), 467 RTE_CACHE_LINE_SIZE, vq->numa_node); 468 469 if (!vq->shadow_used_split) { 470 VHOST_LOG_CONFIG(dev->ifname, ERR, 471 "failed to allocate memory for vq internal data.\n"); 472 return RTE_VHOST_MSG_RESULT_ERR; 473 } 474 } 475 476 rte_free(vq->batch_copy_elems); 477 vq->batch_copy_elems = rte_malloc_socket(NULL, 478 vq->size * sizeof(struct batch_copy_elem), 479 RTE_CACHE_LINE_SIZE, vq->numa_node); 480 if (!vq->batch_copy_elems) { 481 VHOST_LOG_CONFIG(dev->ifname, ERR, 482 "failed to allocate memory for batching copy.\n"); 483 return RTE_VHOST_MSG_RESULT_ERR; 484 } 485 486 return RTE_VHOST_MSG_RESULT_OK; 487 } 488 489 /* 490 * Reallocate virtio_dev, vhost_virtqueue and related data structures to 491 * make them on the same numa node as the memory of vring descriptor. 492 */ 493 #ifdef RTE_LIBRTE_VHOST_NUMA 494 static void 495 numa_realloc(struct virtio_net **pdev, struct vhost_virtqueue **pvq) 496 { 497 int node, dev_node; 498 struct virtio_net *dev; 499 struct vhost_virtqueue *vq; 500 struct batch_copy_elem *bce; 501 struct guest_page *gp; 502 struct rte_vhost_memory *mem; 503 size_t mem_size; 504 int ret; 505 506 dev = *pdev; 507 vq = *pvq; 508 509 /* 510 * If VQ is ready, it is too late to reallocate, it certainly already 511 * happened anyway on VHOST_USER_SET_VRING_ADRR. 512 */ 513 if (vq->ready) 514 return; 515 516 ret = get_mempolicy(&node, NULL, 0, vq->desc, MPOL_F_NODE | MPOL_F_ADDR); 517 if (ret) { 518 VHOST_LOG_CONFIG(dev->ifname, ERR, 519 "unable to get virtqueue %d numa information.\n", 520 vq->index); 521 return; 522 } 523 524 if (node == vq->numa_node) 525 goto out_dev_realloc; 526 527 vq = rte_realloc_socket(*pvq, sizeof(**pvq), 0, node); 528 if (!vq) { 529 VHOST_LOG_CONFIG(dev->ifname, ERR, 530 "failed to realloc virtqueue %d on node %d\n", 531 (*pvq)->index, node); 532 return; 533 } 534 *pvq = vq; 535 536 if (vq != dev->virtqueue[vq->index]) { 537 VHOST_LOG_CONFIG(dev->ifname, INFO, "reallocated virtqueue on node %d\n", node); 538 dev->virtqueue[vq->index] = vq; 539 vhost_user_iotlb_init(dev, vq); 540 } 541 542 if (vq_is_packed(dev)) { 543 struct vring_used_elem_packed *sup; 544 545 sup = rte_realloc_socket(vq->shadow_used_packed, vq->size * sizeof(*sup), 546 RTE_CACHE_LINE_SIZE, node); 547 if (!sup) { 548 VHOST_LOG_CONFIG(dev->ifname, ERR, 549 "failed to realloc shadow packed on node %d\n", 550 node); 551 return; 552 } 553 vq->shadow_used_packed = sup; 554 } else { 555 struct vring_used_elem *sus; 556 557 sus = rte_realloc_socket(vq->shadow_used_split, vq->size * sizeof(*sus), 558 RTE_CACHE_LINE_SIZE, node); 559 if (!sus) { 560 VHOST_LOG_CONFIG(dev->ifname, ERR, 561 "failed to realloc shadow split on node %d\n", 562 node); 563 return; 564 } 565 vq->shadow_used_split = sus; 566 } 567 568 bce = rte_realloc_socket(vq->batch_copy_elems, vq->size * sizeof(*bce), 569 RTE_CACHE_LINE_SIZE, node); 570 if (!bce) { 571 VHOST_LOG_CONFIG(dev->ifname, ERR, 572 "failed to realloc batch copy elem on node %d\n", 573 node); 574 return; 575 } 576 vq->batch_copy_elems = bce; 577 578 if (vq->log_cache) { 579 struct log_cache_entry *lc; 580 581 lc = rte_realloc_socket(vq->log_cache, sizeof(*lc) * VHOST_LOG_CACHE_NR, 0, node); 582 if (!lc) { 583 VHOST_LOG_CONFIG(dev->ifname, ERR, 584 "failed to realloc log cache on node %d\n", 585 node); 586 return; 587 } 588 vq->log_cache = lc; 589 } 590 591 if (vq->resubmit_inflight) { 592 struct rte_vhost_resubmit_info *ri; 593 594 ri = rte_realloc_socket(vq->resubmit_inflight, sizeof(*ri), 0, node); 595 if (!ri) { 596 VHOST_LOG_CONFIG(dev->ifname, ERR, 597 "failed to realloc resubmit inflight on node %d\n", 598 node); 599 return; 600 } 601 vq->resubmit_inflight = ri; 602 603 if (ri->resubmit_list) { 604 struct rte_vhost_resubmit_desc *rd; 605 606 rd = rte_realloc_socket(ri->resubmit_list, sizeof(*rd) * ri->resubmit_num, 607 0, node); 608 if (!rd) { 609 VHOST_LOG_CONFIG(dev->ifname, ERR, 610 "failed to realloc resubmit list on node %d\n", 611 node); 612 return; 613 } 614 ri->resubmit_list = rd; 615 } 616 } 617 618 vq->numa_node = node; 619 620 out_dev_realloc: 621 622 if (dev->flags & VIRTIO_DEV_RUNNING) 623 return; 624 625 ret = get_mempolicy(&dev_node, NULL, 0, dev, MPOL_F_NODE | MPOL_F_ADDR); 626 if (ret) { 627 VHOST_LOG_CONFIG(dev->ifname, ERR, "unable to get numa information.\n"); 628 return; 629 } 630 631 if (dev_node == node) 632 return; 633 634 dev = rte_realloc_socket(*pdev, sizeof(**pdev), 0, node); 635 if (!dev) { 636 VHOST_LOG_CONFIG((*pdev)->ifname, ERR, "failed to realloc dev on node %d\n", node); 637 return; 638 } 639 *pdev = dev; 640 641 VHOST_LOG_CONFIG(dev->ifname, INFO, "reallocated device on node %d\n", node); 642 vhost_devices[dev->vid] = dev; 643 644 mem_size = sizeof(struct rte_vhost_memory) + 645 sizeof(struct rte_vhost_mem_region) * dev->mem->nregions; 646 mem = rte_realloc_socket(dev->mem, mem_size, 0, node); 647 if (!mem) { 648 VHOST_LOG_CONFIG(dev->ifname, ERR, 649 "failed to realloc mem table on node %d\n", 650 node); 651 return; 652 } 653 dev->mem = mem; 654 655 gp = rte_realloc_socket(dev->guest_pages, dev->max_guest_pages * sizeof(*gp), 656 RTE_CACHE_LINE_SIZE, node); 657 if (!gp) { 658 VHOST_LOG_CONFIG(dev->ifname, ERR, 659 "failed to realloc guest pages on node %d\n", 660 node); 661 return; 662 } 663 dev->guest_pages = gp; 664 } 665 #else 666 static void 667 numa_realloc(struct virtio_net **pdev, struct vhost_virtqueue **pvq) 668 { 669 RTE_SET_USED(pdev); 670 RTE_SET_USED(pvq); 671 } 672 #endif 673 674 /* Converts QEMU virtual address to Vhost virtual address. */ 675 static uint64_t 676 qva_to_vva(struct virtio_net *dev, uint64_t qva, uint64_t *len) 677 { 678 struct rte_vhost_mem_region *r; 679 uint32_t i; 680 681 if (unlikely(!dev || !dev->mem)) 682 goto out_error; 683 684 /* Find the region where the address lives. */ 685 for (i = 0; i < dev->mem->nregions; i++) { 686 r = &dev->mem->regions[i]; 687 688 if (qva >= r->guest_user_addr && 689 qva < r->guest_user_addr + r->size) { 690 691 if (unlikely(*len > r->guest_user_addr + r->size - qva)) 692 *len = r->guest_user_addr + r->size - qva; 693 694 return qva - r->guest_user_addr + 695 r->host_user_addr; 696 } 697 } 698 out_error: 699 *len = 0; 700 701 return 0; 702 } 703 704 705 /* 706 * Converts ring address to Vhost virtual address. 707 * If IOMMU is enabled, the ring address is a guest IO virtual address, 708 * else it is a QEMU virtual address. 709 */ 710 static uint64_t 711 ring_addr_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq, 712 uint64_t ra, uint64_t *size) 713 { 714 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) { 715 uint64_t vva; 716 717 vhost_user_iotlb_rd_lock(vq); 718 vva = vhost_iova_to_vva(dev, vq, ra, 719 size, VHOST_ACCESS_RW); 720 vhost_user_iotlb_rd_unlock(vq); 721 722 return vva; 723 } 724 725 return qva_to_vva(dev, ra, size); 726 } 727 728 static uint64_t 729 log_addr_to_gpa(struct virtio_net *dev, struct vhost_virtqueue *vq) 730 { 731 uint64_t log_gpa; 732 733 vhost_user_iotlb_rd_lock(vq); 734 log_gpa = translate_log_addr(dev, vq, vq->ring_addrs.log_guest_addr); 735 vhost_user_iotlb_rd_unlock(vq); 736 737 return log_gpa; 738 } 739 740 uint64_t 741 hua_to_alignment(struct rte_vhost_memory *mem, void *ptr) 742 { 743 struct rte_vhost_mem_region *r; 744 uint32_t i; 745 uintptr_t hua = (uintptr_t)ptr; 746 747 for (i = 0; i < mem->nregions; i++) { 748 r = &mem->regions[i]; 749 if (hua >= r->host_user_addr && 750 hua < r->host_user_addr + r->size) { 751 return get_blk_size(r->fd); 752 } 753 } 754 755 /* If region isn't found, don't align at all */ 756 return 1; 757 } 758 759 void 760 mem_set_dump(void *ptr, size_t size, bool enable, uint64_t pagesz) 761 { 762 #ifdef MADV_DONTDUMP 763 void *start = RTE_PTR_ALIGN_FLOOR(ptr, pagesz); 764 uintptr_t end = RTE_ALIGN_CEIL((uintptr_t)ptr + size, pagesz); 765 size_t len = end - (uintptr_t)start; 766 767 if (madvise(start, len, enable ? MADV_DODUMP : MADV_DONTDUMP) == -1) { 768 rte_log(RTE_LOG_INFO, vhost_config_log_level, 769 "VHOST_CONFIG: could not set coredump preference (%s).\n", strerror(errno)); 770 } 771 #endif 772 } 773 774 static void 775 translate_ring_addresses(struct virtio_net **pdev, struct vhost_virtqueue **pvq) 776 { 777 struct vhost_virtqueue *vq; 778 struct virtio_net *dev; 779 uint64_t len, expected_len; 780 781 dev = *pdev; 782 vq = *pvq; 783 784 if (vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG)) { 785 vq->log_guest_addr = 786 log_addr_to_gpa(dev, vq); 787 if (vq->log_guest_addr == 0) { 788 VHOST_LOG_CONFIG(dev->ifname, DEBUG, "failed to map log_guest_addr.\n"); 789 return; 790 } 791 } 792 793 if (vq_is_packed(dev)) { 794 len = sizeof(struct vring_packed_desc) * vq->size; 795 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t) 796 ring_addr_to_vva(dev, vq, vq->ring_addrs.desc_user_addr, &len); 797 if (vq->desc_packed == NULL || 798 len != sizeof(struct vring_packed_desc) * 799 vq->size) { 800 VHOST_LOG_CONFIG(dev->ifname, DEBUG, "failed to map desc_packed ring.\n"); 801 return; 802 } 803 804 mem_set_dump(vq->desc_packed, len, true, 805 hua_to_alignment(dev->mem, vq->desc_packed)); 806 numa_realloc(&dev, &vq); 807 *pdev = dev; 808 *pvq = vq; 809 810 len = sizeof(struct vring_packed_desc_event); 811 vq->driver_event = (struct vring_packed_desc_event *) 812 (uintptr_t)ring_addr_to_vva(dev, 813 vq, vq->ring_addrs.avail_user_addr, &len); 814 if (vq->driver_event == NULL || 815 len != sizeof(struct vring_packed_desc_event)) { 816 VHOST_LOG_CONFIG(dev->ifname, DEBUG, 817 "failed to find driver area address.\n"); 818 return; 819 } 820 821 mem_set_dump(vq->driver_event, len, true, 822 hua_to_alignment(dev->mem, vq->driver_event)); 823 len = sizeof(struct vring_packed_desc_event); 824 vq->device_event = (struct vring_packed_desc_event *) 825 (uintptr_t)ring_addr_to_vva(dev, 826 vq, vq->ring_addrs.used_user_addr, &len); 827 if (vq->device_event == NULL || 828 len != sizeof(struct vring_packed_desc_event)) { 829 VHOST_LOG_CONFIG(dev->ifname, DEBUG, 830 "failed to find device area address.\n"); 831 return; 832 } 833 834 mem_set_dump(vq->device_event, len, true, 835 hua_to_alignment(dev->mem, vq->device_event)); 836 vq->access_ok = true; 837 return; 838 } 839 840 /* The addresses are converted from QEMU virtual to Vhost virtual. */ 841 if (vq->desc && vq->avail && vq->used) 842 return; 843 844 len = sizeof(struct vring_desc) * vq->size; 845 vq->desc = (struct vring_desc *)(uintptr_t)ring_addr_to_vva(dev, 846 vq, vq->ring_addrs.desc_user_addr, &len); 847 if (vq->desc == 0 || len != sizeof(struct vring_desc) * vq->size) { 848 VHOST_LOG_CONFIG(dev->ifname, DEBUG, "failed to map desc ring.\n"); 849 return; 850 } 851 852 mem_set_dump(vq->desc, len, true, hua_to_alignment(dev->mem, vq->desc)); 853 numa_realloc(&dev, &vq); 854 *pdev = dev; 855 *pvq = vq; 856 857 len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size; 858 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) 859 len += sizeof(uint16_t); 860 expected_len = len; 861 vq->avail = (struct vring_avail *)(uintptr_t)ring_addr_to_vva(dev, 862 vq, vq->ring_addrs.avail_user_addr, &len); 863 if (vq->avail == 0 || len != expected_len) { 864 VHOST_LOG_CONFIG(dev->ifname, DEBUG, "failed to map avail ring.\n"); 865 return; 866 } 867 868 mem_set_dump(vq->avail, len, true, hua_to_alignment(dev->mem, vq->avail)); 869 len = sizeof(struct vring_used) + 870 sizeof(struct vring_used_elem) * vq->size; 871 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) 872 len += sizeof(uint16_t); 873 expected_len = len; 874 vq->used = (struct vring_used *)(uintptr_t)ring_addr_to_vva(dev, 875 vq, vq->ring_addrs.used_user_addr, &len); 876 if (vq->used == 0 || len != expected_len) { 877 VHOST_LOG_CONFIG(dev->ifname, DEBUG, "failed to map used ring.\n"); 878 return; 879 } 880 881 mem_set_dump(vq->used, len, true, hua_to_alignment(dev->mem, vq->used)); 882 883 if (vq->last_used_idx != vq->used->idx) { 884 VHOST_LOG_CONFIG(dev->ifname, WARNING, 885 "last_used_idx (%u) and vq->used->idx (%u) mismatches;\n", 886 vq->last_used_idx, vq->used->idx); 887 vq->last_used_idx = vq->used->idx; 888 vq->last_avail_idx = vq->used->idx; 889 VHOST_LOG_CONFIG(dev->ifname, WARNING, 890 "some packets maybe resent for Tx and dropped for Rx\n"); 891 } 892 893 vq->access_ok = true; 894 895 VHOST_LOG_CONFIG(dev->ifname, DEBUG, "mapped address desc: %p\n", vq->desc); 896 VHOST_LOG_CONFIG(dev->ifname, DEBUG, "mapped address avail: %p\n", vq->avail); 897 VHOST_LOG_CONFIG(dev->ifname, DEBUG, "mapped address used: %p\n", vq->used); 898 VHOST_LOG_CONFIG(dev->ifname, DEBUG, "log_guest_addr: %" PRIx64 "\n", vq->log_guest_addr); 899 } 900 901 /* 902 * The virtio device sends us the desc, used and avail ring addresses. 903 * This function then converts these to our address space. 904 */ 905 static int 906 vhost_user_set_vring_addr(struct virtio_net **pdev, 907 struct vhu_msg_context *ctx, 908 int main_fd __rte_unused) 909 { 910 struct virtio_net *dev = *pdev; 911 struct vhost_virtqueue *vq; 912 struct vhost_vring_addr *addr = &ctx->msg.payload.addr; 913 bool access_ok; 914 915 if (dev->mem == NULL) 916 return RTE_VHOST_MSG_RESULT_ERR; 917 918 /* addr->index refers to the queue index. The txq 1, rxq is 0. */ 919 vq = dev->virtqueue[ctx->msg.payload.addr.index]; 920 921 access_ok = vq->access_ok; 922 923 /* 924 * Rings addresses should not be interpreted as long as the ring is not 925 * started and enabled 926 */ 927 memcpy(&vq->ring_addrs, addr, sizeof(*addr)); 928 929 vring_invalidate(dev, vq); 930 931 if ((vq->enabled && (dev->features & 932 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) || 933 access_ok) { 934 translate_ring_addresses(&dev, &vq); 935 *pdev = dev; 936 } 937 938 return RTE_VHOST_MSG_RESULT_OK; 939 } 940 941 /* 942 * The virtio device sends us the available ring last used index. 943 */ 944 static int 945 vhost_user_set_vring_base(struct virtio_net **pdev, 946 struct vhu_msg_context *ctx, 947 int main_fd __rte_unused) 948 { 949 struct virtio_net *dev = *pdev; 950 struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index]; 951 uint64_t val = ctx->msg.payload.state.num; 952 953 if (vq_is_packed(dev)) { 954 /* 955 * Bit[0:14]: avail index 956 * Bit[15]: avail wrap counter 957 */ 958 vq->last_avail_idx = val & 0x7fff; 959 vq->avail_wrap_counter = !!(val & (0x1 << 15)); 960 /* 961 * Set used index to same value as available one, as 962 * their values should be the same since ring processing 963 * was stopped at get time. 964 */ 965 vq->last_used_idx = vq->last_avail_idx; 966 vq->used_wrap_counter = vq->avail_wrap_counter; 967 } else { 968 vq->last_used_idx = ctx->msg.payload.state.num; 969 vq->last_avail_idx = ctx->msg.payload.state.num; 970 } 971 972 VHOST_LOG_CONFIG(dev->ifname, INFO, 973 "vring base idx:%u last_used_idx:%u last_avail_idx:%u.\n", 974 ctx->msg.payload.state.index, vq->last_used_idx, vq->last_avail_idx); 975 976 return RTE_VHOST_MSG_RESULT_OK; 977 } 978 979 static int 980 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr, 981 uint64_t host_iova, uint64_t host_user_addr, uint64_t size) 982 { 983 struct guest_page *page, *last_page; 984 struct guest_page *old_pages; 985 986 if (dev->nr_guest_pages == dev->max_guest_pages) { 987 dev->max_guest_pages *= 2; 988 old_pages = dev->guest_pages; 989 dev->guest_pages = rte_realloc(dev->guest_pages, 990 dev->max_guest_pages * sizeof(*page), 991 RTE_CACHE_LINE_SIZE); 992 if (dev->guest_pages == NULL) { 993 VHOST_LOG_CONFIG(dev->ifname, ERR, "cannot realloc guest_pages\n"); 994 rte_free(old_pages); 995 return -1; 996 } 997 } 998 999 if (dev->nr_guest_pages > 0) { 1000 last_page = &dev->guest_pages[dev->nr_guest_pages - 1]; 1001 /* merge if the two pages are continuous */ 1002 if (host_iova == last_page->host_iova + last_page->size && 1003 guest_phys_addr == last_page->guest_phys_addr + last_page->size && 1004 host_user_addr == last_page->host_user_addr + last_page->size) { 1005 last_page->size += size; 1006 return 0; 1007 } 1008 } 1009 1010 page = &dev->guest_pages[dev->nr_guest_pages++]; 1011 page->guest_phys_addr = guest_phys_addr; 1012 page->host_iova = host_iova; 1013 page->host_user_addr = host_user_addr; 1014 page->size = size; 1015 1016 return 0; 1017 } 1018 1019 static int 1020 add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg, 1021 uint64_t page_size) 1022 { 1023 uint64_t reg_size = reg->size; 1024 uint64_t host_user_addr = reg->host_user_addr; 1025 uint64_t guest_phys_addr = reg->guest_phys_addr; 1026 uint64_t host_iova; 1027 uint64_t size; 1028 1029 host_iova = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr); 1030 size = page_size - (guest_phys_addr & (page_size - 1)); 1031 size = RTE_MIN(size, reg_size); 1032 1033 if (add_one_guest_page(dev, guest_phys_addr, host_iova, 1034 host_user_addr, size) < 0) 1035 return -1; 1036 1037 host_user_addr += size; 1038 guest_phys_addr += size; 1039 reg_size -= size; 1040 1041 while (reg_size > 0) { 1042 size = RTE_MIN(reg_size, page_size); 1043 host_iova = rte_mem_virt2iova((void *)(uintptr_t) 1044 host_user_addr); 1045 if (add_one_guest_page(dev, guest_phys_addr, host_iova, 1046 host_user_addr, size) < 0) 1047 return -1; 1048 1049 host_user_addr += size; 1050 guest_phys_addr += size; 1051 reg_size -= size; 1052 } 1053 1054 /* sort guest page array if over binary search threshold */ 1055 if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) { 1056 qsort((void *)dev->guest_pages, dev->nr_guest_pages, 1057 sizeof(struct guest_page), guest_page_addrcmp); 1058 } 1059 1060 return 0; 1061 } 1062 1063 #ifdef RTE_LIBRTE_VHOST_DEBUG 1064 /* TODO: enable it only in debug mode? */ 1065 static void 1066 dump_guest_pages(struct virtio_net *dev) 1067 { 1068 uint32_t i; 1069 struct guest_page *page; 1070 1071 for (i = 0; i < dev->nr_guest_pages; i++) { 1072 page = &dev->guest_pages[i]; 1073 1074 VHOST_LOG_CONFIG(dev->ifname, INFO, "guest physical page region %u\n", i); 1075 VHOST_LOG_CONFIG(dev->ifname, INFO, "\tguest_phys_addr: %" PRIx64 "\n", 1076 page->guest_phys_addr); 1077 VHOST_LOG_CONFIG(dev->ifname, INFO, "\thost_iova : %" PRIx64 "\n", 1078 page->host_iova); 1079 VHOST_LOG_CONFIG(dev->ifname, INFO, "\tsize : %" PRIx64 "\n", 1080 page->size); 1081 } 1082 } 1083 #else 1084 #define dump_guest_pages(dev) 1085 #endif 1086 1087 static bool 1088 vhost_memory_changed(struct VhostUserMemory *new, 1089 struct rte_vhost_memory *old) 1090 { 1091 uint32_t i; 1092 1093 if (new->nregions != old->nregions) 1094 return true; 1095 1096 for (i = 0; i < new->nregions; ++i) { 1097 VhostUserMemoryRegion *new_r = &new->regions[i]; 1098 struct rte_vhost_mem_region *old_r = &old->regions[i]; 1099 1100 if (new_r->guest_phys_addr != old_r->guest_phys_addr) 1101 return true; 1102 if (new_r->memory_size != old_r->size) 1103 return true; 1104 if (new_r->userspace_addr != old_r->guest_user_addr) 1105 return true; 1106 } 1107 1108 return false; 1109 } 1110 1111 #ifdef RTE_LIBRTE_VHOST_POSTCOPY 1112 static int 1113 vhost_user_postcopy_region_register(struct virtio_net *dev, 1114 struct rte_vhost_mem_region *reg) 1115 { 1116 struct uffdio_register reg_struct; 1117 1118 /* 1119 * Let's register all the mmapped area to ensure 1120 * alignment on page boundary. 1121 */ 1122 reg_struct.range.start = (uint64_t)(uintptr_t)reg->mmap_addr; 1123 reg_struct.range.len = reg->mmap_size; 1124 reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING; 1125 1126 if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER, 1127 ®_struct)) { 1128 VHOST_LOG_CONFIG(dev->ifname, ERR, 1129 "failed to register ufd for region " 1130 "%" PRIx64 " - %" PRIx64 " (ufd = %d) %s\n", 1131 (uint64_t)reg_struct.range.start, 1132 (uint64_t)reg_struct.range.start + 1133 (uint64_t)reg_struct.range.len - 1, 1134 dev->postcopy_ufd, 1135 strerror(errno)); 1136 return -1; 1137 } 1138 1139 VHOST_LOG_CONFIG(dev->ifname, INFO, 1140 "\t userfaultfd registered for range : %" PRIx64 " - %" PRIx64 "\n", 1141 (uint64_t)reg_struct.range.start, 1142 (uint64_t)reg_struct.range.start + 1143 (uint64_t)reg_struct.range.len - 1); 1144 1145 return 0; 1146 } 1147 #else 1148 static int 1149 vhost_user_postcopy_region_register(struct virtio_net *dev __rte_unused, 1150 struct rte_vhost_mem_region *reg __rte_unused) 1151 { 1152 return -1; 1153 } 1154 #endif 1155 1156 static int 1157 vhost_user_postcopy_register(struct virtio_net *dev, int main_fd, 1158 struct vhu_msg_context *ctx) 1159 { 1160 struct VhostUserMemory *memory; 1161 struct rte_vhost_mem_region *reg; 1162 struct vhu_msg_context ack_ctx; 1163 uint32_t i; 1164 1165 if (!dev->postcopy_listening) 1166 return 0; 1167 1168 /* 1169 * We haven't a better way right now than sharing 1170 * DPDK's virtual address with Qemu, so that Qemu can 1171 * retrieve the region offset when handling userfaults. 1172 */ 1173 memory = &ctx->msg.payload.memory; 1174 for (i = 0; i < memory->nregions; i++) { 1175 reg = &dev->mem->regions[i]; 1176 memory->regions[i].userspace_addr = reg->host_user_addr; 1177 } 1178 1179 /* Send the addresses back to qemu */ 1180 ctx->fd_num = 0; 1181 send_vhost_reply(dev, main_fd, ctx); 1182 1183 /* Wait for qemu to acknowledge it got the addresses 1184 * we've got to wait before we're allowed to generate faults. 1185 */ 1186 if (read_vhost_message(dev, main_fd, &ack_ctx) <= 0) { 1187 VHOST_LOG_CONFIG(dev->ifname, ERR, 1188 "failed to read qemu ack on postcopy set-mem-table\n"); 1189 return -1; 1190 } 1191 1192 if (validate_msg_fds(dev, &ack_ctx, 0) != 0) 1193 return -1; 1194 1195 if (ack_ctx.msg.request.master != VHOST_USER_SET_MEM_TABLE) { 1196 VHOST_LOG_CONFIG(dev->ifname, ERR, 1197 "bad qemu ack on postcopy set-mem-table (%d)\n", 1198 ack_ctx.msg.request.master); 1199 return -1; 1200 } 1201 1202 /* Now userfault register and we can use the memory */ 1203 for (i = 0; i < memory->nregions; i++) { 1204 reg = &dev->mem->regions[i]; 1205 if (vhost_user_postcopy_region_register(dev, reg) < 0) 1206 return -1; 1207 } 1208 1209 return 0; 1210 } 1211 1212 static int 1213 vhost_user_mmap_region(struct virtio_net *dev, 1214 struct rte_vhost_mem_region *region, 1215 uint64_t mmap_offset) 1216 { 1217 void *mmap_addr; 1218 uint64_t mmap_size; 1219 uint64_t alignment; 1220 int populate; 1221 1222 /* Check for memory_size + mmap_offset overflow */ 1223 if (mmap_offset >= -region->size) { 1224 VHOST_LOG_CONFIG(dev->ifname, ERR, 1225 "mmap_offset (%#"PRIx64") and memory_size (%#"PRIx64") overflow\n", 1226 mmap_offset, region->size); 1227 return -1; 1228 } 1229 1230 mmap_size = region->size + mmap_offset; 1231 1232 /* mmap() without flag of MAP_ANONYMOUS, should be called with length 1233 * argument aligned with hugepagesz at older longterm version Linux, 1234 * like 2.6.32 and 3.2.72, or mmap() will fail with EINVAL. 1235 * 1236 * To avoid failure, make sure in caller to keep length aligned. 1237 */ 1238 alignment = get_blk_size(region->fd); 1239 if (alignment == (uint64_t)-1) { 1240 VHOST_LOG_CONFIG(dev->ifname, ERR, "couldn't get hugepage size through fstat\n"); 1241 return -1; 1242 } 1243 mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment); 1244 if (mmap_size == 0) { 1245 /* 1246 * It could happen if initial mmap_size + alignment overflows 1247 * the sizeof uint64, which could happen if either mmap_size or 1248 * alignment value is wrong. 1249 * 1250 * mmap() kernel implementation would return an error, but 1251 * better catch it before and provide useful info in the logs. 1252 */ 1253 VHOST_LOG_CONFIG(dev->ifname, ERR, 1254 "mmap size (0x%" PRIx64 ") or alignment (0x%" PRIx64 ") is invalid\n", 1255 region->size + mmap_offset, alignment); 1256 return -1; 1257 } 1258 1259 populate = dev->async_copy ? MAP_POPULATE : 0; 1260 mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, 1261 MAP_SHARED | populate, region->fd, 0); 1262 1263 if (mmap_addr == MAP_FAILED) { 1264 VHOST_LOG_CONFIG(dev->ifname, ERR, "mmap failed (%s).\n", strerror(errno)); 1265 return -1; 1266 } 1267 1268 region->mmap_addr = mmap_addr; 1269 region->mmap_size = mmap_size; 1270 region->host_user_addr = (uint64_t)(uintptr_t)mmap_addr + mmap_offset; 1271 mem_set_dump(mmap_addr, mmap_size, false, alignment); 1272 1273 if (dev->async_copy) { 1274 if (add_guest_pages(dev, region, alignment) < 0) { 1275 VHOST_LOG_CONFIG(dev->ifname, ERR, 1276 "adding guest pages to region failed.\n"); 1277 return -1; 1278 } 1279 } 1280 1281 VHOST_LOG_CONFIG(dev->ifname, INFO, 1282 "guest memory region size: 0x%" PRIx64 "\n", 1283 region->size); 1284 VHOST_LOG_CONFIG(dev->ifname, INFO, 1285 "\t guest physical addr: 0x%" PRIx64 "\n", 1286 region->guest_phys_addr); 1287 VHOST_LOG_CONFIG(dev->ifname, INFO, 1288 "\t guest virtual addr: 0x%" PRIx64 "\n", 1289 region->guest_user_addr); 1290 VHOST_LOG_CONFIG(dev->ifname, INFO, 1291 "\t host virtual addr: 0x%" PRIx64 "\n", 1292 region->host_user_addr); 1293 VHOST_LOG_CONFIG(dev->ifname, INFO, 1294 "\t mmap addr : 0x%" PRIx64 "\n", 1295 (uint64_t)(uintptr_t)mmap_addr); 1296 VHOST_LOG_CONFIG(dev->ifname, INFO, 1297 "\t mmap size : 0x%" PRIx64 "\n", 1298 mmap_size); 1299 VHOST_LOG_CONFIG(dev->ifname, INFO, 1300 "\t mmap align: 0x%" PRIx64 "\n", 1301 alignment); 1302 VHOST_LOG_CONFIG(dev->ifname, INFO, 1303 "\t mmap off : 0x%" PRIx64 "\n", 1304 mmap_offset); 1305 1306 return 0; 1307 } 1308 1309 static int 1310 vhost_user_set_mem_table(struct virtio_net **pdev, 1311 struct vhu_msg_context *ctx, 1312 int main_fd) 1313 { 1314 struct virtio_net *dev = *pdev; 1315 struct VhostUserMemory *memory = &ctx->msg.payload.memory; 1316 struct rte_vhost_mem_region *reg; 1317 int numa_node = SOCKET_ID_ANY; 1318 uint64_t mmap_offset; 1319 uint32_t i; 1320 bool async_notify = false; 1321 1322 if (validate_msg_fds(dev, ctx, memory->nregions) != 0) 1323 return RTE_VHOST_MSG_RESULT_ERR; 1324 1325 if (memory->nregions > VHOST_MEMORY_MAX_NREGIONS) { 1326 VHOST_LOG_CONFIG(dev->ifname, ERR, 1327 "too many memory regions (%u)\n", 1328 memory->nregions); 1329 goto close_msg_fds; 1330 } 1331 1332 if (dev->mem && !vhost_memory_changed(memory, dev->mem)) { 1333 VHOST_LOG_CONFIG(dev->ifname, INFO, "memory regions not changed\n"); 1334 1335 close_msg_fds(ctx); 1336 1337 return RTE_VHOST_MSG_RESULT_OK; 1338 } 1339 1340 if (dev->mem) { 1341 if (dev->flags & VIRTIO_DEV_VDPA_CONFIGURED) { 1342 struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev; 1343 1344 if (vdpa_dev && vdpa_dev->ops->dev_close) 1345 vdpa_dev->ops->dev_close(dev->vid); 1346 dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED; 1347 } 1348 1349 /* notify the vhost application to stop DMA transfers */ 1350 if (dev->async_copy && dev->notify_ops->vring_state_changed) { 1351 for (i = 0; i < dev->nr_vring; i++) { 1352 dev->notify_ops->vring_state_changed(dev->vid, 1353 i, 0); 1354 } 1355 async_notify = true; 1356 } 1357 1358 free_mem_region(dev); 1359 rte_free(dev->mem); 1360 dev->mem = NULL; 1361 } 1362 1363 /* Flush IOTLB cache as previous HVAs are now invalid */ 1364 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) 1365 for (i = 0; i < dev->nr_vring; i++) 1366 vhost_user_iotlb_flush_all(dev, dev->virtqueue[i]); 1367 1368 /* 1369 * If VQ 0 has already been allocated, try to allocate on the same 1370 * NUMA node. It can be reallocated later in numa_realloc(). 1371 */ 1372 if (dev->nr_vring > 0) 1373 numa_node = dev->virtqueue[0]->numa_node; 1374 1375 dev->nr_guest_pages = 0; 1376 if (dev->guest_pages == NULL) { 1377 dev->max_guest_pages = 8; 1378 dev->guest_pages = rte_zmalloc_socket(NULL, 1379 dev->max_guest_pages * 1380 sizeof(struct guest_page), 1381 RTE_CACHE_LINE_SIZE, 1382 numa_node); 1383 if (dev->guest_pages == NULL) { 1384 VHOST_LOG_CONFIG(dev->ifname, ERR, 1385 "failed to allocate memory for dev->guest_pages\n"); 1386 goto close_msg_fds; 1387 } 1388 } 1389 1390 dev->mem = rte_zmalloc_socket("vhost-mem-table", sizeof(struct rte_vhost_memory) + 1391 sizeof(struct rte_vhost_mem_region) * memory->nregions, 0, numa_node); 1392 if (dev->mem == NULL) { 1393 VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to allocate memory for dev->mem\n"); 1394 goto free_guest_pages; 1395 } 1396 1397 for (i = 0; i < memory->nregions; i++) { 1398 reg = &dev->mem->regions[i]; 1399 1400 reg->guest_phys_addr = memory->regions[i].guest_phys_addr; 1401 reg->guest_user_addr = memory->regions[i].userspace_addr; 1402 reg->size = memory->regions[i].memory_size; 1403 reg->fd = ctx->fds[i]; 1404 1405 /* 1406 * Assign invalid file descriptor value to avoid double 1407 * closing on error path. 1408 */ 1409 ctx->fds[i] = -1; 1410 1411 mmap_offset = memory->regions[i].mmap_offset; 1412 1413 if (vhost_user_mmap_region(dev, reg, mmap_offset) < 0) { 1414 VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to mmap region %u\n", i); 1415 goto free_mem_table; 1416 } 1417 1418 dev->mem->nregions++; 1419 } 1420 1421 if (dev->async_copy && rte_vfio_is_enabled("vfio")) 1422 async_dma_map(dev, true); 1423 1424 if (vhost_user_postcopy_register(dev, main_fd, ctx) < 0) 1425 goto free_mem_table; 1426 1427 for (i = 0; i < dev->nr_vring; i++) { 1428 struct vhost_virtqueue *vq = dev->virtqueue[i]; 1429 1430 if (!vq) 1431 continue; 1432 1433 if (vq->desc || vq->avail || vq->used) { 1434 /* 1435 * If the memory table got updated, the ring addresses 1436 * need to be translated again as virtual addresses have 1437 * changed. 1438 */ 1439 vring_invalidate(dev, vq); 1440 1441 translate_ring_addresses(&dev, &vq); 1442 *pdev = dev; 1443 } 1444 } 1445 1446 dump_guest_pages(dev); 1447 1448 if (async_notify) { 1449 for (i = 0; i < dev->nr_vring; i++) 1450 dev->notify_ops->vring_state_changed(dev->vid, i, 1); 1451 } 1452 1453 return RTE_VHOST_MSG_RESULT_OK; 1454 1455 free_mem_table: 1456 free_mem_region(dev); 1457 rte_free(dev->mem); 1458 dev->mem = NULL; 1459 1460 free_guest_pages: 1461 rte_free(dev->guest_pages); 1462 dev->guest_pages = NULL; 1463 close_msg_fds: 1464 close_msg_fds(ctx); 1465 return RTE_VHOST_MSG_RESULT_ERR; 1466 } 1467 1468 static bool 1469 vq_is_ready(struct virtio_net *dev, struct vhost_virtqueue *vq) 1470 { 1471 bool rings_ok; 1472 1473 if (!vq) 1474 return false; 1475 1476 if (vq_is_packed(dev)) 1477 rings_ok = vq->desc_packed && vq->driver_event && 1478 vq->device_event; 1479 else 1480 rings_ok = vq->desc && vq->avail && vq->used; 1481 1482 return rings_ok && 1483 vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD && 1484 vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD && 1485 vq->enabled; 1486 } 1487 1488 #define VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY 2u 1489 #define VIRTIO_BLK_NUM_VQS_TO_BE_READY 1u 1490 1491 static int 1492 virtio_is_ready(struct virtio_net *dev) 1493 { 1494 struct rte_vdpa_device *vdpa_dev; 1495 struct vhost_virtqueue *vq; 1496 uint32_t vdpa_type; 1497 uint32_t i, nr_vring = dev->nr_vring; 1498 1499 if (dev->flags & VIRTIO_DEV_READY) 1500 return 1; 1501 1502 if (!dev->nr_vring) 1503 return 0; 1504 1505 vdpa_dev = dev->vdpa_dev; 1506 if (vdpa_dev) 1507 vdpa_type = vdpa_dev->type; 1508 else 1509 vdpa_type = -1; 1510 1511 if (vdpa_type == RTE_VHOST_VDPA_DEVICE_TYPE_BLK) { 1512 nr_vring = VIRTIO_BLK_NUM_VQS_TO_BE_READY; 1513 } else { 1514 if (dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) 1515 nr_vring = VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY; 1516 } 1517 1518 if (dev->nr_vring < nr_vring) 1519 return 0; 1520 1521 for (i = 0; i < nr_vring; i++) { 1522 vq = dev->virtqueue[i]; 1523 1524 if (!vq_is_ready(dev, vq)) 1525 return 0; 1526 } 1527 1528 /* If supported, ensure the frontend is really done with config */ 1529 if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS)) 1530 if (!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK)) 1531 return 0; 1532 1533 dev->flags |= VIRTIO_DEV_READY; 1534 1535 if (!(dev->flags & VIRTIO_DEV_RUNNING)) 1536 VHOST_LOG_CONFIG(dev->ifname, INFO, "virtio is now ready for processing.\n"); 1537 return 1; 1538 } 1539 1540 static void * 1541 inflight_mem_alloc(struct virtio_net *dev, const char *name, size_t size, int *fd) 1542 { 1543 void *ptr; 1544 int mfd = -1; 1545 uint64_t alignment; 1546 char fname[20] = "/tmp/memfd-XXXXXX"; 1547 1548 *fd = -1; 1549 #ifdef MEMFD_SUPPORTED 1550 mfd = memfd_create(name, MFD_CLOEXEC); 1551 #else 1552 RTE_SET_USED(name); 1553 #endif 1554 if (mfd == -1) { 1555 mfd = mkstemp(fname); 1556 if (mfd == -1) { 1557 VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to get inflight buffer fd\n"); 1558 return NULL; 1559 } 1560 1561 unlink(fname); 1562 } 1563 1564 if (ftruncate(mfd, size) == -1) { 1565 VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to alloc inflight buffer\n"); 1566 close(mfd); 1567 return NULL; 1568 } 1569 1570 ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0); 1571 if (ptr == MAP_FAILED) { 1572 VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to mmap inflight buffer\n"); 1573 close(mfd); 1574 return NULL; 1575 } 1576 1577 alignment = get_blk_size(mfd); 1578 mem_set_dump(ptr, size, false, alignment); 1579 *fd = mfd; 1580 return ptr; 1581 } 1582 1583 static uint32_t 1584 get_pervq_shm_size_split(uint16_t queue_size) 1585 { 1586 return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_split) * 1587 queue_size + sizeof(uint64_t) + 1588 sizeof(uint16_t) * 4, INFLIGHT_ALIGNMENT); 1589 } 1590 1591 static uint32_t 1592 get_pervq_shm_size_packed(uint16_t queue_size) 1593 { 1594 return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_packed) 1595 * queue_size + sizeof(uint64_t) + 1596 sizeof(uint16_t) * 6 + sizeof(uint8_t) * 9, 1597 INFLIGHT_ALIGNMENT); 1598 } 1599 1600 static int 1601 vhost_user_get_inflight_fd(struct virtio_net **pdev, 1602 struct vhu_msg_context *ctx, 1603 int main_fd __rte_unused) 1604 { 1605 struct rte_vhost_inflight_info_packed *inflight_packed; 1606 uint64_t pervq_inflight_size, mmap_size; 1607 uint16_t num_queues, queue_size; 1608 struct virtio_net *dev = *pdev; 1609 int fd, i, j; 1610 int numa_node = SOCKET_ID_ANY; 1611 void *addr; 1612 1613 if (ctx->msg.size != sizeof(ctx->msg.payload.inflight)) { 1614 VHOST_LOG_CONFIG(dev->ifname, ERR, 1615 "invalid get_inflight_fd message size is %d\n", 1616 ctx->msg.size); 1617 return RTE_VHOST_MSG_RESULT_ERR; 1618 } 1619 1620 /* 1621 * If VQ 0 has already been allocated, try to allocate on the same 1622 * NUMA node. It can be reallocated later in numa_realloc(). 1623 */ 1624 if (dev->nr_vring > 0) 1625 numa_node = dev->virtqueue[0]->numa_node; 1626 1627 if (dev->inflight_info == NULL) { 1628 dev->inflight_info = rte_zmalloc_socket("inflight_info", 1629 sizeof(struct inflight_mem_info), 0, numa_node); 1630 if (!dev->inflight_info) { 1631 VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to alloc dev inflight area\n"); 1632 return RTE_VHOST_MSG_RESULT_ERR; 1633 } 1634 dev->inflight_info->fd = -1; 1635 } 1636 1637 num_queues = ctx->msg.payload.inflight.num_queues; 1638 queue_size = ctx->msg.payload.inflight.queue_size; 1639 1640 VHOST_LOG_CONFIG(dev->ifname, INFO, 1641 "get_inflight_fd num_queues: %u\n", 1642 ctx->msg.payload.inflight.num_queues); 1643 VHOST_LOG_CONFIG(dev->ifname, INFO, 1644 "get_inflight_fd queue_size: %u\n", 1645 ctx->msg.payload.inflight.queue_size); 1646 1647 if (vq_is_packed(dev)) 1648 pervq_inflight_size = get_pervq_shm_size_packed(queue_size); 1649 else 1650 pervq_inflight_size = get_pervq_shm_size_split(queue_size); 1651 1652 mmap_size = num_queues * pervq_inflight_size; 1653 addr = inflight_mem_alloc(dev, "vhost-inflight", mmap_size, &fd); 1654 if (!addr) { 1655 VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to alloc vhost inflight area\n"); 1656 ctx->msg.payload.inflight.mmap_size = 0; 1657 return RTE_VHOST_MSG_RESULT_ERR; 1658 } 1659 memset(addr, 0, mmap_size); 1660 1661 if (dev->inflight_info->addr) { 1662 munmap(dev->inflight_info->addr, dev->inflight_info->size); 1663 dev->inflight_info->addr = NULL; 1664 } 1665 1666 if (dev->inflight_info->fd >= 0) { 1667 close(dev->inflight_info->fd); 1668 dev->inflight_info->fd = -1; 1669 } 1670 1671 dev->inflight_info->addr = addr; 1672 dev->inflight_info->size = ctx->msg.payload.inflight.mmap_size = mmap_size; 1673 dev->inflight_info->fd = ctx->fds[0] = fd; 1674 ctx->msg.payload.inflight.mmap_offset = 0; 1675 ctx->fd_num = 1; 1676 1677 if (vq_is_packed(dev)) { 1678 for (i = 0; i < num_queues; i++) { 1679 inflight_packed = 1680 (struct rte_vhost_inflight_info_packed *)addr; 1681 inflight_packed->used_wrap_counter = 1; 1682 inflight_packed->old_used_wrap_counter = 1; 1683 for (j = 0; j < queue_size; j++) 1684 inflight_packed->desc[j].next = j + 1; 1685 addr = (void *)((char *)addr + pervq_inflight_size); 1686 } 1687 } 1688 1689 VHOST_LOG_CONFIG(dev->ifname, INFO, 1690 "send inflight mmap_size: %"PRIu64"\n", 1691 ctx->msg.payload.inflight.mmap_size); 1692 VHOST_LOG_CONFIG(dev->ifname, INFO, 1693 "send inflight mmap_offset: %"PRIu64"\n", 1694 ctx->msg.payload.inflight.mmap_offset); 1695 VHOST_LOG_CONFIG(dev->ifname, INFO, 1696 "send inflight fd: %d\n", ctx->fds[0]); 1697 1698 return RTE_VHOST_MSG_RESULT_REPLY; 1699 } 1700 1701 static int 1702 vhost_user_set_inflight_fd(struct virtio_net **pdev, 1703 struct vhu_msg_context *ctx, 1704 int main_fd __rte_unused) 1705 { 1706 uint64_t mmap_size, mmap_offset; 1707 uint16_t num_queues, queue_size; 1708 struct virtio_net *dev = *pdev; 1709 uint32_t pervq_inflight_size; 1710 struct vhost_virtqueue *vq; 1711 void *addr; 1712 int fd, i; 1713 int numa_node = SOCKET_ID_ANY; 1714 1715 if (validate_msg_fds(dev, ctx, 1) != 0) 1716 return RTE_VHOST_MSG_RESULT_ERR; 1717 1718 fd = ctx->fds[0]; 1719 if (ctx->msg.size != sizeof(ctx->msg.payload.inflight) || fd < 0) { 1720 VHOST_LOG_CONFIG(dev->ifname, ERR, 1721 "invalid set_inflight_fd message size is %d,fd is %d\n", 1722 ctx->msg.size, fd); 1723 return RTE_VHOST_MSG_RESULT_ERR; 1724 } 1725 1726 mmap_size = ctx->msg.payload.inflight.mmap_size; 1727 mmap_offset = ctx->msg.payload.inflight.mmap_offset; 1728 num_queues = ctx->msg.payload.inflight.num_queues; 1729 queue_size = ctx->msg.payload.inflight.queue_size; 1730 1731 if (vq_is_packed(dev)) 1732 pervq_inflight_size = get_pervq_shm_size_packed(queue_size); 1733 else 1734 pervq_inflight_size = get_pervq_shm_size_split(queue_size); 1735 1736 VHOST_LOG_CONFIG(dev->ifname, INFO, "set_inflight_fd mmap_size: %"PRIu64"\n", mmap_size); 1737 VHOST_LOG_CONFIG(dev->ifname, INFO, 1738 "set_inflight_fd mmap_offset: %"PRIu64"\n", 1739 mmap_offset); 1740 VHOST_LOG_CONFIG(dev->ifname, INFO, 1741 "set_inflight_fd num_queues: %u\n", 1742 num_queues); 1743 VHOST_LOG_CONFIG(dev->ifname, INFO, 1744 "set_inflight_fd queue_size: %u\n", 1745 queue_size); 1746 VHOST_LOG_CONFIG(dev->ifname, INFO, 1747 "set_inflight_fd fd: %d\n", 1748 fd); 1749 VHOST_LOG_CONFIG(dev->ifname, INFO, 1750 "set_inflight_fd pervq_inflight_size: %d\n", 1751 pervq_inflight_size); 1752 1753 /* 1754 * If VQ 0 has already been allocated, try to allocate on the same 1755 * NUMA node. It can be reallocated later in numa_realloc(). 1756 */ 1757 if (dev->nr_vring > 0) 1758 numa_node = dev->virtqueue[0]->numa_node; 1759 1760 if (!dev->inflight_info) { 1761 dev->inflight_info = rte_zmalloc_socket("inflight_info", 1762 sizeof(struct inflight_mem_info), 0, numa_node); 1763 if (dev->inflight_info == NULL) { 1764 VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to alloc dev inflight area\n"); 1765 return RTE_VHOST_MSG_RESULT_ERR; 1766 } 1767 dev->inflight_info->fd = -1; 1768 } 1769 1770 if (dev->inflight_info->addr) { 1771 munmap(dev->inflight_info->addr, dev->inflight_info->size); 1772 dev->inflight_info->addr = NULL; 1773 } 1774 1775 addr = mmap(0, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, 1776 fd, mmap_offset); 1777 if (addr == MAP_FAILED) { 1778 VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to mmap share memory.\n"); 1779 return RTE_VHOST_MSG_RESULT_ERR; 1780 } 1781 1782 if (dev->inflight_info->fd >= 0) { 1783 close(dev->inflight_info->fd); 1784 dev->inflight_info->fd = -1; 1785 } 1786 1787 mem_set_dump(addr, mmap_size, false, get_blk_size(fd)); 1788 dev->inflight_info->fd = fd; 1789 dev->inflight_info->addr = addr; 1790 dev->inflight_info->size = mmap_size; 1791 1792 for (i = 0; i < num_queues; i++) { 1793 vq = dev->virtqueue[i]; 1794 if (!vq) 1795 continue; 1796 1797 if (vq_is_packed(dev)) { 1798 vq->inflight_packed = addr; 1799 vq->inflight_packed->desc_num = queue_size; 1800 } else { 1801 vq->inflight_split = addr; 1802 vq->inflight_split->desc_num = queue_size; 1803 } 1804 addr = (void *)((char *)addr + pervq_inflight_size); 1805 } 1806 1807 return RTE_VHOST_MSG_RESULT_OK; 1808 } 1809 1810 static int 1811 vhost_user_set_vring_call(struct virtio_net **pdev, 1812 struct vhu_msg_context *ctx, 1813 int main_fd __rte_unused) 1814 { 1815 struct virtio_net *dev = *pdev; 1816 struct vhost_vring_file file; 1817 struct vhost_virtqueue *vq; 1818 int expected_fds; 1819 1820 expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1; 1821 if (validate_msg_fds(dev, ctx, expected_fds) != 0) 1822 return RTE_VHOST_MSG_RESULT_ERR; 1823 1824 file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK; 1825 if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) 1826 file.fd = VIRTIO_INVALID_EVENTFD; 1827 else 1828 file.fd = ctx->fds[0]; 1829 VHOST_LOG_CONFIG(dev->ifname, INFO, 1830 "vring call idx:%d file:%d\n", 1831 file.index, file.fd); 1832 1833 vq = dev->virtqueue[file.index]; 1834 1835 if (vq->ready) { 1836 vq->ready = false; 1837 vhost_user_notify_queue_state(dev, vq, 0); 1838 } 1839 1840 if (vq->callfd >= 0) 1841 close(vq->callfd); 1842 1843 vq->callfd = file.fd; 1844 1845 return RTE_VHOST_MSG_RESULT_OK; 1846 } 1847 1848 static int vhost_user_set_vring_err(struct virtio_net **pdev, 1849 struct vhu_msg_context *ctx, 1850 int main_fd __rte_unused) 1851 { 1852 struct virtio_net *dev = *pdev; 1853 int expected_fds; 1854 1855 expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1; 1856 if (validate_msg_fds(dev, ctx, expected_fds) != 0) 1857 return RTE_VHOST_MSG_RESULT_ERR; 1858 1859 if (!(ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)) 1860 close(ctx->fds[0]); 1861 VHOST_LOG_CONFIG(dev->ifname, DEBUG, "not implemented\n"); 1862 1863 return RTE_VHOST_MSG_RESULT_OK; 1864 } 1865 1866 static int 1867 resubmit_desc_compare(const void *a, const void *b) 1868 { 1869 const struct rte_vhost_resubmit_desc *desc0 = a; 1870 const struct rte_vhost_resubmit_desc *desc1 = b; 1871 1872 if (desc1->counter > desc0->counter) 1873 return 1; 1874 1875 return -1; 1876 } 1877 1878 static int 1879 vhost_check_queue_inflights_split(struct virtio_net *dev, 1880 struct vhost_virtqueue *vq) 1881 { 1882 uint16_t i; 1883 uint16_t resubmit_num = 0, last_io, num; 1884 struct vring_used *used = vq->used; 1885 struct rte_vhost_resubmit_info *resubmit; 1886 struct rte_vhost_inflight_info_split *inflight_split; 1887 1888 if (!(dev->protocol_features & 1889 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))) 1890 return RTE_VHOST_MSG_RESULT_OK; 1891 1892 /* The frontend may still not support the inflight feature 1893 * although we negotiate the protocol feature. 1894 */ 1895 if ((!vq->inflight_split)) 1896 return RTE_VHOST_MSG_RESULT_OK; 1897 1898 if (!vq->inflight_split->version) { 1899 vq->inflight_split->version = INFLIGHT_VERSION; 1900 return RTE_VHOST_MSG_RESULT_OK; 1901 } 1902 1903 if (vq->resubmit_inflight) 1904 return RTE_VHOST_MSG_RESULT_OK; 1905 1906 inflight_split = vq->inflight_split; 1907 vq->global_counter = 0; 1908 last_io = inflight_split->last_inflight_io; 1909 1910 if (inflight_split->used_idx != used->idx) { 1911 inflight_split->desc[last_io].inflight = 0; 1912 rte_atomic_thread_fence(__ATOMIC_SEQ_CST); 1913 inflight_split->used_idx = used->idx; 1914 } 1915 1916 for (i = 0; i < inflight_split->desc_num; i++) { 1917 if (inflight_split->desc[i].inflight == 1) 1918 resubmit_num++; 1919 } 1920 1921 vq->last_avail_idx += resubmit_num; 1922 1923 if (resubmit_num) { 1924 resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info), 1925 0, vq->numa_node); 1926 if (!resubmit) { 1927 VHOST_LOG_CONFIG(dev->ifname, ERR, 1928 "failed to allocate memory for resubmit info.\n"); 1929 return RTE_VHOST_MSG_RESULT_ERR; 1930 } 1931 1932 resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list", 1933 resubmit_num * sizeof(struct rte_vhost_resubmit_desc), 1934 0, vq->numa_node); 1935 if (!resubmit->resubmit_list) { 1936 VHOST_LOG_CONFIG(dev->ifname, ERR, 1937 "failed to allocate memory for inflight desc.\n"); 1938 rte_free(resubmit); 1939 return RTE_VHOST_MSG_RESULT_ERR; 1940 } 1941 1942 num = 0; 1943 for (i = 0; i < vq->inflight_split->desc_num; i++) { 1944 if (vq->inflight_split->desc[i].inflight == 1) { 1945 resubmit->resubmit_list[num].index = i; 1946 resubmit->resubmit_list[num].counter = 1947 inflight_split->desc[i].counter; 1948 num++; 1949 } 1950 } 1951 resubmit->resubmit_num = num; 1952 1953 if (resubmit->resubmit_num > 1) 1954 qsort(resubmit->resubmit_list, resubmit->resubmit_num, 1955 sizeof(struct rte_vhost_resubmit_desc), 1956 resubmit_desc_compare); 1957 1958 vq->global_counter = resubmit->resubmit_list[0].counter + 1; 1959 vq->resubmit_inflight = resubmit; 1960 } 1961 1962 return RTE_VHOST_MSG_RESULT_OK; 1963 } 1964 1965 static int 1966 vhost_check_queue_inflights_packed(struct virtio_net *dev, 1967 struct vhost_virtqueue *vq) 1968 { 1969 uint16_t i; 1970 uint16_t resubmit_num = 0, old_used_idx, num; 1971 struct rte_vhost_resubmit_info *resubmit; 1972 struct rte_vhost_inflight_info_packed *inflight_packed; 1973 1974 if (!(dev->protocol_features & 1975 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))) 1976 return RTE_VHOST_MSG_RESULT_OK; 1977 1978 /* The frontend may still not support the inflight feature 1979 * although we negotiate the protocol feature. 1980 */ 1981 if ((!vq->inflight_packed)) 1982 return RTE_VHOST_MSG_RESULT_OK; 1983 1984 if (!vq->inflight_packed->version) { 1985 vq->inflight_packed->version = INFLIGHT_VERSION; 1986 return RTE_VHOST_MSG_RESULT_OK; 1987 } 1988 1989 if (vq->resubmit_inflight) 1990 return RTE_VHOST_MSG_RESULT_OK; 1991 1992 inflight_packed = vq->inflight_packed; 1993 vq->global_counter = 0; 1994 old_used_idx = inflight_packed->old_used_idx; 1995 1996 if (inflight_packed->used_idx != old_used_idx) { 1997 if (inflight_packed->desc[old_used_idx].inflight == 0) { 1998 inflight_packed->old_used_idx = 1999 inflight_packed->used_idx; 2000 inflight_packed->old_used_wrap_counter = 2001 inflight_packed->used_wrap_counter; 2002 inflight_packed->old_free_head = 2003 inflight_packed->free_head; 2004 } else { 2005 inflight_packed->used_idx = 2006 inflight_packed->old_used_idx; 2007 inflight_packed->used_wrap_counter = 2008 inflight_packed->old_used_wrap_counter; 2009 inflight_packed->free_head = 2010 inflight_packed->old_free_head; 2011 } 2012 } 2013 2014 for (i = 0; i < inflight_packed->desc_num; i++) { 2015 if (inflight_packed->desc[i].inflight == 1) 2016 resubmit_num++; 2017 } 2018 2019 if (resubmit_num) { 2020 resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info), 2021 0, vq->numa_node); 2022 if (resubmit == NULL) { 2023 VHOST_LOG_CONFIG(dev->ifname, ERR, 2024 "failed to allocate memory for resubmit info.\n"); 2025 return RTE_VHOST_MSG_RESULT_ERR; 2026 } 2027 2028 resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list", 2029 resubmit_num * sizeof(struct rte_vhost_resubmit_desc), 2030 0, vq->numa_node); 2031 if (resubmit->resubmit_list == NULL) { 2032 VHOST_LOG_CONFIG(dev->ifname, ERR, 2033 "failed to allocate memory for resubmit desc.\n"); 2034 rte_free(resubmit); 2035 return RTE_VHOST_MSG_RESULT_ERR; 2036 } 2037 2038 num = 0; 2039 for (i = 0; i < inflight_packed->desc_num; i++) { 2040 if (vq->inflight_packed->desc[i].inflight == 1) { 2041 resubmit->resubmit_list[num].index = i; 2042 resubmit->resubmit_list[num].counter = 2043 inflight_packed->desc[i].counter; 2044 num++; 2045 } 2046 } 2047 resubmit->resubmit_num = num; 2048 2049 if (resubmit->resubmit_num > 1) 2050 qsort(resubmit->resubmit_list, resubmit->resubmit_num, 2051 sizeof(struct rte_vhost_resubmit_desc), 2052 resubmit_desc_compare); 2053 2054 vq->global_counter = resubmit->resubmit_list[0].counter + 1; 2055 vq->resubmit_inflight = resubmit; 2056 } 2057 2058 return RTE_VHOST_MSG_RESULT_OK; 2059 } 2060 2061 static int 2062 vhost_user_set_vring_kick(struct virtio_net **pdev, 2063 struct vhu_msg_context *ctx, 2064 int main_fd __rte_unused) 2065 { 2066 struct virtio_net *dev = *pdev; 2067 struct vhost_vring_file file; 2068 struct vhost_virtqueue *vq; 2069 int expected_fds; 2070 2071 expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1; 2072 if (validate_msg_fds(dev, ctx, expected_fds) != 0) 2073 return RTE_VHOST_MSG_RESULT_ERR; 2074 2075 file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK; 2076 if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) 2077 file.fd = VIRTIO_INVALID_EVENTFD; 2078 else 2079 file.fd = ctx->fds[0]; 2080 VHOST_LOG_CONFIG(dev->ifname, INFO, 2081 "vring kick idx:%d file:%d\n", 2082 file.index, file.fd); 2083 2084 /* Interpret ring addresses only when ring is started. */ 2085 vq = dev->virtqueue[file.index]; 2086 translate_ring_addresses(&dev, &vq); 2087 *pdev = dev; 2088 2089 /* 2090 * When VHOST_USER_F_PROTOCOL_FEATURES is not negotiated, 2091 * the ring starts already enabled. Otherwise, it is enabled via 2092 * the SET_VRING_ENABLE message. 2093 */ 2094 if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) { 2095 vq->enabled = true; 2096 } 2097 2098 if (vq->ready) { 2099 vq->ready = false; 2100 vhost_user_notify_queue_state(dev, vq, 0); 2101 } 2102 2103 if (vq->kickfd >= 0) 2104 close(vq->kickfd); 2105 vq->kickfd = file.fd; 2106 2107 if (vq_is_packed(dev)) { 2108 if (vhost_check_queue_inflights_packed(dev, vq)) { 2109 VHOST_LOG_CONFIG(dev->ifname, ERR, 2110 "failed to inflights for vq: %d\n", 2111 file.index); 2112 return RTE_VHOST_MSG_RESULT_ERR; 2113 } 2114 } else { 2115 if (vhost_check_queue_inflights_split(dev, vq)) { 2116 VHOST_LOG_CONFIG(dev->ifname, ERR, 2117 "failed to inflights for vq: %d\n", 2118 file.index); 2119 return RTE_VHOST_MSG_RESULT_ERR; 2120 } 2121 } 2122 2123 return RTE_VHOST_MSG_RESULT_OK; 2124 } 2125 2126 /* 2127 * when virtio is stopped, qemu will send us the GET_VRING_BASE message. 2128 */ 2129 static int 2130 vhost_user_get_vring_base(struct virtio_net **pdev, 2131 struct vhu_msg_context *ctx, 2132 int main_fd __rte_unused) 2133 { 2134 struct virtio_net *dev = *pdev; 2135 struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index]; 2136 uint64_t val; 2137 2138 /* We have to stop the queue (virtio) if it is running. */ 2139 vhost_destroy_device_notify(dev); 2140 2141 dev->flags &= ~VIRTIO_DEV_READY; 2142 dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED; 2143 2144 /* Here we are safe to get the indexes */ 2145 if (vq_is_packed(dev)) { 2146 /* 2147 * Bit[0:14]: avail index 2148 * Bit[15]: avail wrap counter 2149 */ 2150 val = vq->last_avail_idx & 0x7fff; 2151 val |= vq->avail_wrap_counter << 15; 2152 ctx->msg.payload.state.num = val; 2153 } else { 2154 ctx->msg.payload.state.num = vq->last_avail_idx; 2155 } 2156 2157 VHOST_LOG_CONFIG(dev->ifname, INFO, 2158 "vring base idx:%d file:%d\n", 2159 ctx->msg.payload.state.index, ctx->msg.payload.state.num); 2160 /* 2161 * Based on current qemu vhost-user implementation, this message is 2162 * sent and only sent in vhost_vring_stop. 2163 * TODO: cleanup the vring, it isn't usable since here. 2164 */ 2165 if (vq->kickfd >= 0) 2166 close(vq->kickfd); 2167 2168 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD; 2169 2170 if (vq->callfd >= 0) 2171 close(vq->callfd); 2172 2173 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD; 2174 2175 vq->signalled_used_valid = false; 2176 2177 if (vq_is_packed(dev)) { 2178 rte_free(vq->shadow_used_packed); 2179 vq->shadow_used_packed = NULL; 2180 } else { 2181 rte_free(vq->shadow_used_split); 2182 vq->shadow_used_split = NULL; 2183 } 2184 2185 rte_free(vq->batch_copy_elems); 2186 vq->batch_copy_elems = NULL; 2187 2188 rte_free(vq->log_cache); 2189 vq->log_cache = NULL; 2190 2191 ctx->msg.size = sizeof(ctx->msg.payload.state); 2192 ctx->fd_num = 0; 2193 2194 vhost_user_iotlb_flush_all(dev, vq); 2195 2196 vring_invalidate(dev, vq); 2197 2198 return RTE_VHOST_MSG_RESULT_REPLY; 2199 } 2200 2201 /* 2202 * when virtio queues are ready to work, qemu will send us to 2203 * enable the virtio queue pair. 2204 */ 2205 static int 2206 vhost_user_set_vring_enable(struct virtio_net **pdev, 2207 struct vhu_msg_context *ctx, 2208 int main_fd __rte_unused) 2209 { 2210 struct virtio_net *dev = *pdev; 2211 struct vhost_virtqueue *vq; 2212 bool enable = !!ctx->msg.payload.state.num; 2213 int index = (int)ctx->msg.payload.state.index; 2214 2215 VHOST_LOG_CONFIG(dev->ifname, INFO, 2216 "set queue enable: %d to qp idx: %d\n", 2217 enable, index); 2218 2219 vq = dev->virtqueue[index]; 2220 if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) { 2221 /* vhost_user_lock_all_queue_pairs locked all qps */ 2222 vq_assert_lock(dev, vq); 2223 if (enable && vq->async && vq->async->pkts_inflight_n) { 2224 VHOST_LOG_CONFIG(dev->ifname, ERR, 2225 "failed to enable vring. Inflight packets must be completed first\n"); 2226 return RTE_VHOST_MSG_RESULT_ERR; 2227 } 2228 } 2229 2230 vq->enabled = enable; 2231 2232 return RTE_VHOST_MSG_RESULT_OK; 2233 } 2234 2235 static int 2236 vhost_user_get_protocol_features(struct virtio_net **pdev, 2237 struct vhu_msg_context *ctx, 2238 int main_fd __rte_unused) 2239 { 2240 struct virtio_net *dev = *pdev; 2241 uint64_t features, protocol_features; 2242 2243 rte_vhost_driver_get_features(dev->ifname, &features); 2244 rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features); 2245 2246 ctx->msg.payload.u64 = protocol_features; 2247 ctx->msg.size = sizeof(ctx->msg.payload.u64); 2248 ctx->fd_num = 0; 2249 2250 return RTE_VHOST_MSG_RESULT_REPLY; 2251 } 2252 2253 static int 2254 vhost_user_set_protocol_features(struct virtio_net **pdev, 2255 struct vhu_msg_context *ctx, 2256 int main_fd __rte_unused) 2257 { 2258 struct virtio_net *dev = *pdev; 2259 uint64_t protocol_features = ctx->msg.payload.u64; 2260 uint64_t slave_protocol_features = 0; 2261 2262 rte_vhost_driver_get_protocol_features(dev->ifname, 2263 &slave_protocol_features); 2264 if (protocol_features & ~slave_protocol_features) { 2265 VHOST_LOG_CONFIG(dev->ifname, ERR, "received invalid protocol features.\n"); 2266 return RTE_VHOST_MSG_RESULT_ERR; 2267 } 2268 2269 dev->protocol_features = protocol_features; 2270 VHOST_LOG_CONFIG(dev->ifname, INFO, 2271 "negotiated Vhost-user protocol features: 0x%" PRIx64 "\n", 2272 dev->protocol_features); 2273 2274 return RTE_VHOST_MSG_RESULT_OK; 2275 } 2276 2277 static int 2278 vhost_user_set_log_base(struct virtio_net **pdev, 2279 struct vhu_msg_context *ctx, 2280 int main_fd __rte_unused) 2281 { 2282 struct virtio_net *dev = *pdev; 2283 int fd = ctx->fds[0]; 2284 uint64_t size, off; 2285 uint64_t alignment; 2286 void *addr; 2287 uint32_t i; 2288 2289 if (validate_msg_fds(dev, ctx, 1) != 0) 2290 return RTE_VHOST_MSG_RESULT_ERR; 2291 2292 if (fd < 0) { 2293 VHOST_LOG_CONFIG(dev->ifname, ERR, "invalid log fd: %d\n", fd); 2294 return RTE_VHOST_MSG_RESULT_ERR; 2295 } 2296 2297 if (ctx->msg.size != sizeof(VhostUserLog)) { 2298 VHOST_LOG_CONFIG(dev->ifname, ERR, 2299 "invalid log base msg size: %"PRId32" != %d\n", 2300 ctx->msg.size, (int)sizeof(VhostUserLog)); 2301 goto close_msg_fds; 2302 } 2303 2304 size = ctx->msg.payload.log.mmap_size; 2305 off = ctx->msg.payload.log.mmap_offset; 2306 2307 /* Check for mmap size and offset overflow. */ 2308 if (off >= -size) { 2309 VHOST_LOG_CONFIG(dev->ifname, ERR, 2310 "log offset %#"PRIx64" and log size %#"PRIx64" overflow\n", 2311 off, size); 2312 goto close_msg_fds; 2313 } 2314 2315 VHOST_LOG_CONFIG(dev->ifname, INFO, 2316 "log mmap size: %"PRId64", offset: %"PRId64"\n", 2317 size, off); 2318 2319 /* 2320 * mmap from 0 to workaround a hugepage mmap bug: mmap will 2321 * fail when offset is not page size aligned. 2322 */ 2323 addr = mmap(0, size + off, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 2324 alignment = get_blk_size(fd); 2325 close(fd); 2326 if (addr == MAP_FAILED) { 2327 VHOST_LOG_CONFIG(dev->ifname, ERR, "mmap log base failed!\n"); 2328 return RTE_VHOST_MSG_RESULT_ERR; 2329 } 2330 2331 /* 2332 * Free previously mapped log memory on occasionally 2333 * multiple VHOST_USER_SET_LOG_BASE. 2334 */ 2335 if (dev->log_addr) { 2336 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size); 2337 } 2338 dev->log_addr = (uint64_t)(uintptr_t)addr; 2339 dev->log_base = dev->log_addr + off; 2340 dev->log_size = size; 2341 mem_set_dump(addr, size + off, false, alignment); 2342 2343 for (i = 0; i < dev->nr_vring; i++) { 2344 struct vhost_virtqueue *vq = dev->virtqueue[i]; 2345 2346 rte_free(vq->log_cache); 2347 vq->log_cache = NULL; 2348 vq->log_cache_nb_elem = 0; 2349 vq->log_cache = rte_malloc_socket("vq log cache", 2350 sizeof(struct log_cache_entry) * VHOST_LOG_CACHE_NR, 2351 0, vq->numa_node); 2352 /* 2353 * If log cache alloc fail, don't fail migration, but no 2354 * caching will be done, which will impact performance 2355 */ 2356 if (!vq->log_cache) 2357 VHOST_LOG_CONFIG(dev->ifname, ERR, 2358 "failed to allocate VQ logging cache\n"); 2359 } 2360 2361 /* 2362 * The spec is not clear about it (yet), but QEMU doesn't expect 2363 * any payload in the reply. 2364 */ 2365 ctx->msg.size = 0; 2366 ctx->fd_num = 0; 2367 2368 return RTE_VHOST_MSG_RESULT_REPLY; 2369 2370 close_msg_fds: 2371 close_msg_fds(ctx); 2372 return RTE_VHOST_MSG_RESULT_ERR; 2373 } 2374 2375 static int vhost_user_set_log_fd(struct virtio_net **pdev, 2376 struct vhu_msg_context *ctx, 2377 int main_fd __rte_unused) 2378 { 2379 struct virtio_net *dev = *pdev; 2380 2381 if (validate_msg_fds(dev, ctx, 1) != 0) 2382 return RTE_VHOST_MSG_RESULT_ERR; 2383 2384 close(ctx->fds[0]); 2385 VHOST_LOG_CONFIG(dev->ifname, DEBUG, "not implemented.\n"); 2386 2387 return RTE_VHOST_MSG_RESULT_OK; 2388 } 2389 2390 /* 2391 * An rarp packet is constructed and broadcasted to notify switches about 2392 * the new location of the migrated VM, so that packets from outside will 2393 * not be lost after migration. 2394 * 2395 * However, we don't actually "send" a rarp packet here, instead, we set 2396 * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it. 2397 */ 2398 static int 2399 vhost_user_send_rarp(struct virtio_net **pdev, 2400 struct vhu_msg_context *ctx, 2401 int main_fd __rte_unused) 2402 { 2403 struct virtio_net *dev = *pdev; 2404 uint8_t *mac = (uint8_t *)&ctx->msg.payload.u64; 2405 struct rte_vdpa_device *vdpa_dev; 2406 2407 VHOST_LOG_CONFIG(dev->ifname, DEBUG, 2408 "MAC: " RTE_ETHER_ADDR_PRT_FMT "\n", 2409 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 2410 memcpy(dev->mac.addr_bytes, mac, 6); 2411 2412 /* 2413 * Set the flag to inject a RARP broadcast packet at 2414 * rte_vhost_dequeue_burst(). 2415 * 2416 * __ATOMIC_RELEASE ordering is for making sure the mac is 2417 * copied before the flag is set. 2418 */ 2419 __atomic_store_n(&dev->broadcast_rarp, 1, __ATOMIC_RELEASE); 2420 vdpa_dev = dev->vdpa_dev; 2421 if (vdpa_dev && vdpa_dev->ops->migration_done) 2422 vdpa_dev->ops->migration_done(dev->vid); 2423 2424 return RTE_VHOST_MSG_RESULT_OK; 2425 } 2426 2427 static int 2428 vhost_user_net_set_mtu(struct virtio_net **pdev, 2429 struct vhu_msg_context *ctx, 2430 int main_fd __rte_unused) 2431 { 2432 struct virtio_net *dev = *pdev; 2433 2434 if (ctx->msg.payload.u64 < VIRTIO_MIN_MTU || 2435 ctx->msg.payload.u64 > VIRTIO_MAX_MTU) { 2436 VHOST_LOG_CONFIG(dev->ifname, ERR, 2437 "invalid MTU size (%"PRIu64")\n", 2438 ctx->msg.payload.u64); 2439 2440 return RTE_VHOST_MSG_RESULT_ERR; 2441 } 2442 2443 dev->mtu = ctx->msg.payload.u64; 2444 2445 return RTE_VHOST_MSG_RESULT_OK; 2446 } 2447 2448 static int 2449 vhost_user_set_req_fd(struct virtio_net **pdev, 2450 struct vhu_msg_context *ctx, 2451 int main_fd __rte_unused) 2452 { 2453 struct virtio_net *dev = *pdev; 2454 int fd = ctx->fds[0]; 2455 2456 if (validate_msg_fds(dev, ctx, 1) != 0) 2457 return RTE_VHOST_MSG_RESULT_ERR; 2458 2459 if (fd < 0) { 2460 VHOST_LOG_CONFIG(dev->ifname, ERR, 2461 "invalid file descriptor for slave channel (%d)\n", fd); 2462 return RTE_VHOST_MSG_RESULT_ERR; 2463 } 2464 2465 if (dev->slave_req_fd >= 0) 2466 close(dev->slave_req_fd); 2467 2468 dev->slave_req_fd = fd; 2469 2470 return RTE_VHOST_MSG_RESULT_OK; 2471 } 2472 2473 static int 2474 is_vring_iotlb_split(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg) 2475 { 2476 struct vhost_vring_addr *ra; 2477 uint64_t start, end, len; 2478 2479 start = imsg->iova; 2480 end = start + imsg->size; 2481 2482 ra = &vq->ring_addrs; 2483 len = sizeof(struct vring_desc) * vq->size; 2484 if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start) 2485 return 1; 2486 2487 len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size; 2488 if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start) 2489 return 1; 2490 2491 len = sizeof(struct vring_used) + 2492 sizeof(struct vring_used_elem) * vq->size; 2493 if (ra->used_user_addr < end && (ra->used_user_addr + len) > start) 2494 return 1; 2495 2496 if (ra->flags & (1 << VHOST_VRING_F_LOG)) { 2497 len = sizeof(uint64_t); 2498 if (ra->log_guest_addr < end && 2499 (ra->log_guest_addr + len) > start) 2500 return 1; 2501 } 2502 2503 return 0; 2504 } 2505 2506 static int 2507 is_vring_iotlb_packed(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg) 2508 { 2509 struct vhost_vring_addr *ra; 2510 uint64_t start, end, len; 2511 2512 start = imsg->iova; 2513 end = start + imsg->size; 2514 2515 ra = &vq->ring_addrs; 2516 len = sizeof(struct vring_packed_desc) * vq->size; 2517 if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start) 2518 return 1; 2519 2520 len = sizeof(struct vring_packed_desc_event); 2521 if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start) 2522 return 1; 2523 2524 len = sizeof(struct vring_packed_desc_event); 2525 if (ra->used_user_addr < end && (ra->used_user_addr + len) > start) 2526 return 1; 2527 2528 if (ra->flags & (1 << VHOST_VRING_F_LOG)) { 2529 len = sizeof(uint64_t); 2530 if (ra->log_guest_addr < end && 2531 (ra->log_guest_addr + len) > start) 2532 return 1; 2533 } 2534 2535 return 0; 2536 } 2537 2538 static int is_vring_iotlb(struct virtio_net *dev, 2539 struct vhost_virtqueue *vq, 2540 struct vhost_iotlb_msg *imsg) 2541 { 2542 if (vq_is_packed(dev)) 2543 return is_vring_iotlb_packed(vq, imsg); 2544 else 2545 return is_vring_iotlb_split(vq, imsg); 2546 } 2547 2548 static int 2549 vhost_user_get_config(struct virtio_net **pdev, 2550 struct vhu_msg_context *ctx, 2551 int main_fd __rte_unused) 2552 { 2553 struct virtio_net *dev = *pdev; 2554 struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev; 2555 int ret = 0; 2556 2557 if (validate_msg_fds(dev, ctx, 0) != 0) 2558 return RTE_VHOST_MSG_RESULT_ERR; 2559 2560 if (!vdpa_dev) { 2561 VHOST_LOG_CONFIG(dev->ifname, ERR, "is not vDPA device!\n"); 2562 return RTE_VHOST_MSG_RESULT_ERR; 2563 } 2564 2565 if (vdpa_dev->ops->get_config) { 2566 ret = vdpa_dev->ops->get_config(dev->vid, 2567 ctx->msg.payload.cfg.region, 2568 ctx->msg.payload.cfg.size); 2569 if (ret != 0) { 2570 ctx->msg.size = 0; 2571 VHOST_LOG_CONFIG(dev->ifname, ERR, "get_config() return error!\n"); 2572 } 2573 } else { 2574 VHOST_LOG_CONFIG(dev->ifname, ERR, "get_config() not supported!\n"); 2575 } 2576 2577 return RTE_VHOST_MSG_RESULT_REPLY; 2578 } 2579 2580 static int 2581 vhost_user_set_config(struct virtio_net **pdev, 2582 struct vhu_msg_context *ctx, 2583 int main_fd __rte_unused) 2584 { 2585 struct virtio_net *dev = *pdev; 2586 struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev; 2587 int ret = 0; 2588 2589 if (validate_msg_fds(dev, ctx, 0) != 0) 2590 return RTE_VHOST_MSG_RESULT_ERR; 2591 2592 if (ctx->msg.payload.cfg.size > VHOST_USER_MAX_CONFIG_SIZE) { 2593 VHOST_LOG_CONFIG(dev->ifname, ERR, 2594 "vhost_user_config size: %"PRIu32", should not be larger than %d\n", 2595 ctx->msg.payload.cfg.size, VHOST_USER_MAX_CONFIG_SIZE); 2596 goto out; 2597 } 2598 2599 if (!vdpa_dev) { 2600 VHOST_LOG_CONFIG(dev->ifname, ERR, "is not vDPA device!\n"); 2601 goto out; 2602 } 2603 2604 if (vdpa_dev->ops->set_config) { 2605 ret = vdpa_dev->ops->set_config(dev->vid, 2606 ctx->msg.payload.cfg.region, 2607 ctx->msg.payload.cfg.offset, 2608 ctx->msg.payload.cfg.size, 2609 ctx->msg.payload.cfg.flags); 2610 if (ret) 2611 VHOST_LOG_CONFIG(dev->ifname, ERR, "set_config() return error!\n"); 2612 } else { 2613 VHOST_LOG_CONFIG(dev->ifname, ERR, "set_config() not supported!\n"); 2614 } 2615 2616 return RTE_VHOST_MSG_RESULT_OK; 2617 2618 out: 2619 return RTE_VHOST_MSG_RESULT_ERR; 2620 } 2621 2622 static int 2623 vhost_user_iotlb_msg(struct virtio_net **pdev, 2624 struct vhu_msg_context *ctx, 2625 int main_fd __rte_unused) 2626 { 2627 struct virtio_net *dev = *pdev; 2628 struct vhost_iotlb_msg *imsg = &ctx->msg.payload.iotlb; 2629 uint16_t i; 2630 uint64_t vva, len; 2631 2632 switch (imsg->type) { 2633 case VHOST_IOTLB_UPDATE: 2634 len = imsg->size; 2635 vva = qva_to_vva(dev, imsg->uaddr, &len); 2636 if (!vva) 2637 return RTE_VHOST_MSG_RESULT_ERR; 2638 2639 for (i = 0; i < dev->nr_vring; i++) { 2640 struct vhost_virtqueue *vq = dev->virtqueue[i]; 2641 2642 if (!vq) 2643 continue; 2644 2645 vhost_user_iotlb_cache_insert(dev, vq, imsg->iova, vva, 2646 len, imsg->perm); 2647 2648 if (is_vring_iotlb(dev, vq, imsg)) { 2649 rte_spinlock_lock(&vq->access_lock); 2650 translate_ring_addresses(&dev, &vq); 2651 *pdev = dev; 2652 rte_spinlock_unlock(&vq->access_lock); 2653 } 2654 } 2655 break; 2656 case VHOST_IOTLB_INVALIDATE: 2657 for (i = 0; i < dev->nr_vring; i++) { 2658 struct vhost_virtqueue *vq = dev->virtqueue[i]; 2659 2660 if (!vq) 2661 continue; 2662 2663 vhost_user_iotlb_cache_remove(dev, vq, imsg->iova, 2664 imsg->size); 2665 2666 if (is_vring_iotlb(dev, vq, imsg)) { 2667 rte_spinlock_lock(&vq->access_lock); 2668 vring_invalidate(dev, vq); 2669 rte_spinlock_unlock(&vq->access_lock); 2670 } 2671 } 2672 break; 2673 default: 2674 VHOST_LOG_CONFIG(dev->ifname, ERR, 2675 "invalid IOTLB message type (%d)\n", 2676 imsg->type); 2677 return RTE_VHOST_MSG_RESULT_ERR; 2678 } 2679 2680 return RTE_VHOST_MSG_RESULT_OK; 2681 } 2682 2683 static int 2684 vhost_user_set_postcopy_advise(struct virtio_net **pdev, 2685 struct vhu_msg_context *ctx, 2686 int main_fd __rte_unused) 2687 { 2688 struct virtio_net *dev = *pdev; 2689 #ifdef RTE_LIBRTE_VHOST_POSTCOPY 2690 struct uffdio_api api_struct; 2691 2692 dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); 2693 2694 if (dev->postcopy_ufd == -1) { 2695 VHOST_LOG_CONFIG(dev->ifname, ERR, 2696 "userfaultfd not available: %s\n", 2697 strerror(errno)); 2698 return RTE_VHOST_MSG_RESULT_ERR; 2699 } 2700 api_struct.api = UFFD_API; 2701 api_struct.features = 0; 2702 if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) { 2703 VHOST_LOG_CONFIG(dev->ifname, ERR, 2704 "UFFDIO_API ioctl failure: %s\n", 2705 strerror(errno)); 2706 close(dev->postcopy_ufd); 2707 dev->postcopy_ufd = -1; 2708 return RTE_VHOST_MSG_RESULT_ERR; 2709 } 2710 ctx->fds[0] = dev->postcopy_ufd; 2711 ctx->fd_num = 1; 2712 2713 return RTE_VHOST_MSG_RESULT_REPLY; 2714 #else 2715 dev->postcopy_ufd = -1; 2716 ctx->fd_num = 0; 2717 2718 return RTE_VHOST_MSG_RESULT_ERR; 2719 #endif 2720 } 2721 2722 static int 2723 vhost_user_set_postcopy_listen(struct virtio_net **pdev, 2724 struct vhu_msg_context *ctx __rte_unused, 2725 int main_fd __rte_unused) 2726 { 2727 struct virtio_net *dev = *pdev; 2728 2729 if (dev->mem && dev->mem->nregions) { 2730 VHOST_LOG_CONFIG(dev->ifname, ERR, 2731 "regions already registered at postcopy-listen\n"); 2732 return RTE_VHOST_MSG_RESULT_ERR; 2733 } 2734 dev->postcopy_listening = 1; 2735 2736 return RTE_VHOST_MSG_RESULT_OK; 2737 } 2738 2739 static int 2740 vhost_user_postcopy_end(struct virtio_net **pdev, 2741 struct vhu_msg_context *ctx, 2742 int main_fd __rte_unused) 2743 { 2744 struct virtio_net *dev = *pdev; 2745 2746 dev->postcopy_listening = 0; 2747 if (dev->postcopy_ufd >= 0) { 2748 close(dev->postcopy_ufd); 2749 dev->postcopy_ufd = -1; 2750 } 2751 2752 ctx->msg.payload.u64 = 0; 2753 ctx->msg.size = sizeof(ctx->msg.payload.u64); 2754 ctx->fd_num = 0; 2755 2756 return RTE_VHOST_MSG_RESULT_REPLY; 2757 } 2758 2759 static int 2760 vhost_user_get_status(struct virtio_net **pdev, 2761 struct vhu_msg_context *ctx, 2762 int main_fd __rte_unused) 2763 { 2764 struct virtio_net *dev = *pdev; 2765 2766 ctx->msg.payload.u64 = dev->status; 2767 ctx->msg.size = sizeof(ctx->msg.payload.u64); 2768 ctx->fd_num = 0; 2769 2770 return RTE_VHOST_MSG_RESULT_REPLY; 2771 } 2772 2773 static int 2774 vhost_user_set_status(struct virtio_net **pdev, 2775 struct vhu_msg_context *ctx, 2776 int main_fd __rte_unused) 2777 { 2778 struct virtio_net *dev = *pdev; 2779 2780 /* As per Virtio specification, the device status is 8bits long */ 2781 if (ctx->msg.payload.u64 > UINT8_MAX) { 2782 VHOST_LOG_CONFIG(dev->ifname, ERR, 2783 "invalid VHOST_USER_SET_STATUS payload 0x%" PRIx64 "\n", 2784 ctx->msg.payload.u64); 2785 return RTE_VHOST_MSG_RESULT_ERR; 2786 } 2787 2788 dev->status = ctx->msg.payload.u64; 2789 2790 if ((dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK) && 2791 (dev->flags & VIRTIO_DEV_FEATURES_FAILED)) { 2792 VHOST_LOG_CONFIG(dev->ifname, ERR, 2793 "FEATURES_OK bit is set but feature negotiation failed\n"); 2794 /* 2795 * Clear the bit to let the driver know about the feature 2796 * negotiation failure 2797 */ 2798 dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK; 2799 } 2800 2801 VHOST_LOG_CONFIG(dev->ifname, INFO, "new device status(0x%08x):\n", dev->status); 2802 VHOST_LOG_CONFIG(dev->ifname, INFO, 2803 "\t-RESET: %u\n", 2804 (dev->status == VIRTIO_DEVICE_STATUS_RESET)); 2805 VHOST_LOG_CONFIG(dev->ifname, INFO, 2806 "\t-ACKNOWLEDGE: %u\n", 2807 !!(dev->status & VIRTIO_DEVICE_STATUS_ACK)); 2808 VHOST_LOG_CONFIG(dev->ifname, INFO, 2809 "\t-DRIVER: %u\n", 2810 !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER)); 2811 VHOST_LOG_CONFIG(dev->ifname, INFO, 2812 "\t-FEATURES_OK: %u\n", 2813 !!(dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK)); 2814 VHOST_LOG_CONFIG(dev->ifname, INFO, 2815 "\t-DRIVER_OK: %u\n", 2816 !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK)); 2817 VHOST_LOG_CONFIG(dev->ifname, INFO, 2818 "\t-DEVICE_NEED_RESET: %u\n", 2819 !!(dev->status & VIRTIO_DEVICE_STATUS_DEV_NEED_RESET)); 2820 VHOST_LOG_CONFIG(dev->ifname, INFO, 2821 "\t-FAILED: %u\n", 2822 !!(dev->status & VIRTIO_DEVICE_STATUS_FAILED)); 2823 2824 return RTE_VHOST_MSG_RESULT_OK; 2825 } 2826 2827 #define VHOST_MESSAGE_HANDLERS \ 2828 VHOST_MESSAGE_HANDLER(VHOST_USER_NONE, NULL, false) \ 2829 VHOST_MESSAGE_HANDLER(VHOST_USER_GET_FEATURES, vhost_user_get_features, false) \ 2830 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_FEATURES, vhost_user_set_features, false) \ 2831 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_OWNER, vhost_user_set_owner, false) \ 2832 VHOST_MESSAGE_HANDLER(VHOST_USER_RESET_OWNER, vhost_user_reset_owner, false) \ 2833 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_MEM_TABLE, vhost_user_set_mem_table, true) \ 2834 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_BASE, vhost_user_set_log_base, true) \ 2835 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_FD, vhost_user_set_log_fd, true) \ 2836 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_NUM, vhost_user_set_vring_num, false) \ 2837 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ADDR, vhost_user_set_vring_addr, false) \ 2838 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_BASE, vhost_user_set_vring_base, false) \ 2839 VHOST_MESSAGE_HANDLER(VHOST_USER_GET_VRING_BASE, vhost_user_get_vring_base, false) \ 2840 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_KICK, vhost_user_set_vring_kick, true) \ 2841 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_CALL, vhost_user_set_vring_call, true) \ 2842 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ERR, vhost_user_set_vring_err, true) \ 2843 VHOST_MESSAGE_HANDLER(VHOST_USER_GET_PROTOCOL_FEATURES, vhost_user_get_protocol_features, false) \ 2844 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_PROTOCOL_FEATURES, vhost_user_set_protocol_features, false) \ 2845 VHOST_MESSAGE_HANDLER(VHOST_USER_GET_QUEUE_NUM, vhost_user_get_queue_num, false) \ 2846 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ENABLE, vhost_user_set_vring_enable, false) \ 2847 VHOST_MESSAGE_HANDLER(VHOST_USER_SEND_RARP, vhost_user_send_rarp, false) \ 2848 VHOST_MESSAGE_HANDLER(VHOST_USER_NET_SET_MTU, vhost_user_net_set_mtu, false) \ 2849 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_SLAVE_REQ_FD, vhost_user_set_req_fd, true) \ 2850 VHOST_MESSAGE_HANDLER(VHOST_USER_IOTLB_MSG, vhost_user_iotlb_msg, false) \ 2851 VHOST_MESSAGE_HANDLER(VHOST_USER_GET_CONFIG, vhost_user_get_config, false) \ 2852 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_CONFIG, vhost_user_set_config, false) \ 2853 VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_ADVISE, vhost_user_set_postcopy_advise, false) \ 2854 VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_LISTEN, vhost_user_set_postcopy_listen, false) \ 2855 VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_END, vhost_user_postcopy_end, false) \ 2856 VHOST_MESSAGE_HANDLER(VHOST_USER_GET_INFLIGHT_FD, vhost_user_get_inflight_fd, false) \ 2857 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_INFLIGHT_FD, vhost_user_set_inflight_fd, true) \ 2858 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_STATUS, vhost_user_set_status, false) \ 2859 VHOST_MESSAGE_HANDLER(VHOST_USER_GET_STATUS, vhost_user_get_status, false) 2860 2861 #define VHOST_MESSAGE_HANDLER(id, handler, accepts_fd) \ 2862 [id] = { #id, handler, accepts_fd }, 2863 static vhost_message_handler_t vhost_message_handlers[] = { 2864 VHOST_MESSAGE_HANDLERS 2865 }; 2866 #undef VHOST_MESSAGE_HANDLER 2867 2868 /* return bytes# of read on success or negative val on failure. */ 2869 static int 2870 read_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx) 2871 { 2872 int ret; 2873 2874 ret = read_fd_message(dev->ifname, sockfd, (char *)&ctx->msg, VHOST_USER_HDR_SIZE, 2875 ctx->fds, VHOST_MEMORY_MAX_NREGIONS, &ctx->fd_num); 2876 if (ret <= 0) 2877 goto out; 2878 2879 if (ret != VHOST_USER_HDR_SIZE) { 2880 VHOST_LOG_CONFIG(dev->ifname, ERR, "Unexpected header size read\n"); 2881 ret = -1; 2882 goto out; 2883 } 2884 2885 if (ctx->msg.size) { 2886 if (ctx->msg.size > sizeof(ctx->msg.payload)) { 2887 VHOST_LOG_CONFIG(dev->ifname, ERR, "invalid msg size: %d\n", 2888 ctx->msg.size); 2889 ret = -1; 2890 goto out; 2891 } 2892 ret = read(sockfd, &ctx->msg.payload, ctx->msg.size); 2893 if (ret <= 0) 2894 goto out; 2895 if (ret != (int)ctx->msg.size) { 2896 VHOST_LOG_CONFIG(dev->ifname, ERR, "read control message failed\n"); 2897 ret = -1; 2898 goto out; 2899 } 2900 } 2901 2902 out: 2903 if (ret <= 0) 2904 close_msg_fds(ctx); 2905 2906 return ret; 2907 } 2908 2909 static int 2910 send_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx) 2911 { 2912 if (!ctx) 2913 return 0; 2914 2915 return send_fd_message(dev->ifname, sockfd, (char *)&ctx->msg, 2916 VHOST_USER_HDR_SIZE + ctx->msg.size, ctx->fds, ctx->fd_num); 2917 } 2918 2919 static int 2920 send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx) 2921 { 2922 if (!ctx) 2923 return 0; 2924 2925 ctx->msg.flags &= ~VHOST_USER_VERSION_MASK; 2926 ctx->msg.flags &= ~VHOST_USER_NEED_REPLY; 2927 ctx->msg.flags |= VHOST_USER_VERSION; 2928 ctx->msg.flags |= VHOST_USER_REPLY_MASK; 2929 2930 return send_vhost_message(dev, sockfd, ctx); 2931 } 2932 2933 static int 2934 send_vhost_slave_message(struct virtio_net *dev, struct vhu_msg_context *ctx) 2935 { 2936 return send_vhost_message(dev, dev->slave_req_fd, ctx); 2937 } 2938 2939 static int 2940 send_vhost_slave_message_process_reply(struct virtio_net *dev, struct vhu_msg_context *ctx) 2941 { 2942 struct vhu_msg_context msg_reply; 2943 int ret; 2944 2945 rte_spinlock_lock(&dev->slave_req_lock); 2946 ret = send_vhost_slave_message(dev, ctx); 2947 if (ret < 0) { 2948 VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to send config change (%d)\n", ret); 2949 goto out; 2950 } 2951 2952 ret = read_vhost_message(dev, dev->slave_req_fd, &msg_reply); 2953 if (ret <= 0) { 2954 if (ret < 0) 2955 VHOST_LOG_CONFIG(dev->ifname, ERR, 2956 "vhost read slave message reply failed\n"); 2957 else 2958 VHOST_LOG_CONFIG(dev->ifname, INFO, "vhost peer closed\n"); 2959 ret = -1; 2960 goto out; 2961 } 2962 2963 if (msg_reply.msg.request.slave != ctx->msg.request.slave) { 2964 VHOST_LOG_CONFIG(dev->ifname, ERR, 2965 "received unexpected msg type (%u), expected %u\n", 2966 msg_reply.msg.request.slave, ctx->msg.request.slave); 2967 ret = -1; 2968 goto out; 2969 } 2970 2971 ret = msg_reply.msg.payload.u64 ? -1 : 0; 2972 out: 2973 rte_spinlock_unlock(&dev->slave_req_lock); 2974 return ret; 2975 } 2976 2977 /* 2978 * Allocate a queue pair if it hasn't been allocated yet 2979 */ 2980 static int 2981 vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev, 2982 struct vhu_msg_context *ctx) 2983 { 2984 uint32_t vring_idx; 2985 2986 switch (ctx->msg.request.master) { 2987 case VHOST_USER_SET_VRING_KICK: 2988 case VHOST_USER_SET_VRING_CALL: 2989 case VHOST_USER_SET_VRING_ERR: 2990 vring_idx = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK; 2991 break; 2992 case VHOST_USER_SET_VRING_NUM: 2993 case VHOST_USER_SET_VRING_BASE: 2994 case VHOST_USER_GET_VRING_BASE: 2995 case VHOST_USER_SET_VRING_ENABLE: 2996 vring_idx = ctx->msg.payload.state.index; 2997 break; 2998 case VHOST_USER_SET_VRING_ADDR: 2999 vring_idx = ctx->msg.payload.addr.index; 3000 break; 3001 case VHOST_USER_SET_INFLIGHT_FD: 3002 vring_idx = ctx->msg.payload.inflight.num_queues - 1; 3003 break; 3004 default: 3005 return 0; 3006 } 3007 3008 if (vring_idx >= VHOST_MAX_VRING) { 3009 VHOST_LOG_CONFIG(dev->ifname, ERR, "invalid vring index: %u\n", vring_idx); 3010 return -1; 3011 } 3012 3013 if (dev->virtqueue[vring_idx]) 3014 return 0; 3015 3016 return alloc_vring_queue(dev, vring_idx); 3017 } 3018 3019 static void 3020 vhost_user_lock_all_queue_pairs(struct virtio_net *dev) 3021 __rte_no_thread_safety_analysis 3022 { 3023 unsigned int i = 0; 3024 unsigned int vq_num = 0; 3025 3026 while (vq_num < dev->nr_vring) { 3027 struct vhost_virtqueue *vq = dev->virtqueue[i]; 3028 3029 if (vq) { 3030 rte_spinlock_lock(&vq->access_lock); 3031 vq_num++; 3032 } 3033 i++; 3034 } 3035 } 3036 3037 static void 3038 vhost_user_unlock_all_queue_pairs(struct virtio_net *dev) 3039 __rte_no_thread_safety_analysis 3040 { 3041 unsigned int i = 0; 3042 unsigned int vq_num = 0; 3043 3044 while (vq_num < dev->nr_vring) { 3045 struct vhost_virtqueue *vq = dev->virtqueue[i]; 3046 3047 if (vq) { 3048 rte_spinlock_unlock(&vq->access_lock); 3049 vq_num++; 3050 } 3051 i++; 3052 } 3053 } 3054 3055 int 3056 vhost_user_msg_handler(int vid, int fd) 3057 { 3058 struct virtio_net *dev; 3059 struct vhu_msg_context ctx; 3060 vhost_message_handler_t *msg_handler; 3061 struct rte_vdpa_device *vdpa_dev; 3062 int msg_result = RTE_VHOST_MSG_RESULT_OK; 3063 int ret; 3064 int unlock_required = 0; 3065 bool handled; 3066 uint32_t request; 3067 uint32_t i; 3068 uint16_t blk_call_fd; 3069 3070 dev = get_device(vid); 3071 if (dev == NULL) 3072 return -1; 3073 3074 if (!dev->notify_ops) { 3075 dev->notify_ops = vhost_driver_callback_get(dev->ifname); 3076 if (!dev->notify_ops) { 3077 VHOST_LOG_CONFIG(dev->ifname, ERR, 3078 "failed to get callback ops for driver\n"); 3079 return -1; 3080 } 3081 } 3082 3083 ctx.msg.request.master = VHOST_USER_NONE; 3084 ret = read_vhost_message(dev, fd, &ctx); 3085 if (ret == 0) { 3086 VHOST_LOG_CONFIG(dev->ifname, INFO, "vhost peer closed\n"); 3087 return -1; 3088 } 3089 3090 request = ctx.msg.request.master; 3091 if (request > VHOST_USER_NONE && request < RTE_DIM(vhost_message_handlers)) 3092 msg_handler = &vhost_message_handlers[request]; 3093 else 3094 msg_handler = NULL; 3095 3096 if (ret < 0) { 3097 VHOST_LOG_CONFIG(dev->ifname, ERR, "vhost read message %s%s%sfailed\n", 3098 msg_handler != NULL ? "for " : "", 3099 msg_handler != NULL ? msg_handler->description : "", 3100 msg_handler != NULL ? " " : ""); 3101 return -1; 3102 } 3103 3104 if (msg_handler != NULL && msg_handler->description != NULL) { 3105 if (request != VHOST_USER_IOTLB_MSG) 3106 VHOST_LOG_CONFIG(dev->ifname, INFO, 3107 "read message %s\n", 3108 msg_handler->description); 3109 else 3110 VHOST_LOG_CONFIG(dev->ifname, DEBUG, 3111 "read message %s\n", 3112 msg_handler->description); 3113 } else { 3114 VHOST_LOG_CONFIG(dev->ifname, DEBUG, "external request %d\n", request); 3115 } 3116 3117 ret = vhost_user_check_and_alloc_queue_pair(dev, &ctx); 3118 if (ret < 0) { 3119 VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to alloc queue\n"); 3120 return -1; 3121 } 3122 3123 /* 3124 * Note: we don't lock all queues on VHOST_USER_GET_VRING_BASE 3125 * and VHOST_USER_RESET_OWNER, since it is sent when virtio stops 3126 * and device is destroyed. destroy_device waits for queues to be 3127 * inactive, so it is safe. Otherwise taking the access_lock 3128 * would cause a dead lock. 3129 */ 3130 switch (request) { 3131 case VHOST_USER_SET_FEATURES: 3132 case VHOST_USER_SET_PROTOCOL_FEATURES: 3133 case VHOST_USER_SET_OWNER: 3134 case VHOST_USER_SET_MEM_TABLE: 3135 case VHOST_USER_SET_LOG_BASE: 3136 case VHOST_USER_SET_LOG_FD: 3137 case VHOST_USER_SET_VRING_NUM: 3138 case VHOST_USER_SET_VRING_ADDR: 3139 case VHOST_USER_SET_VRING_BASE: 3140 case VHOST_USER_SET_VRING_KICK: 3141 case VHOST_USER_SET_VRING_CALL: 3142 case VHOST_USER_SET_VRING_ERR: 3143 case VHOST_USER_SET_VRING_ENABLE: 3144 case VHOST_USER_SEND_RARP: 3145 case VHOST_USER_NET_SET_MTU: 3146 case VHOST_USER_SET_SLAVE_REQ_FD: 3147 if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) { 3148 vhost_user_lock_all_queue_pairs(dev); 3149 unlock_required = 1; 3150 } 3151 break; 3152 default: 3153 break; 3154 3155 } 3156 3157 handled = false; 3158 if (dev->extern_ops.pre_msg_handle) { 3159 RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0); 3160 msg_result = (*dev->extern_ops.pre_msg_handle)(dev->vid, &ctx); 3161 switch (msg_result) { 3162 case RTE_VHOST_MSG_RESULT_REPLY: 3163 send_vhost_reply(dev, fd, &ctx); 3164 /* Fall-through */ 3165 case RTE_VHOST_MSG_RESULT_ERR: 3166 case RTE_VHOST_MSG_RESULT_OK: 3167 handled = true; 3168 goto skip_to_post_handle; 3169 case RTE_VHOST_MSG_RESULT_NOT_HANDLED: 3170 default: 3171 break; 3172 } 3173 } 3174 3175 if (msg_handler == NULL || msg_handler->callback == NULL) 3176 goto skip_to_post_handle; 3177 3178 if (!msg_handler->accepts_fd && validate_msg_fds(dev, &ctx, 0) != 0) { 3179 msg_result = RTE_VHOST_MSG_RESULT_ERR; 3180 } else { 3181 msg_result = msg_handler->callback(&dev, &ctx, fd); 3182 } 3183 3184 switch (msg_result) { 3185 case RTE_VHOST_MSG_RESULT_ERR: 3186 VHOST_LOG_CONFIG(dev->ifname, ERR, 3187 "processing %s failed.\n", 3188 msg_handler->description); 3189 handled = true; 3190 break; 3191 case RTE_VHOST_MSG_RESULT_OK: 3192 VHOST_LOG_CONFIG(dev->ifname, DEBUG, 3193 "processing %s succeeded.\n", 3194 msg_handler->description); 3195 handled = true; 3196 break; 3197 case RTE_VHOST_MSG_RESULT_REPLY: 3198 VHOST_LOG_CONFIG(dev->ifname, DEBUG, 3199 "processing %s succeeded and needs reply.\n", 3200 msg_handler->description); 3201 send_vhost_reply(dev, fd, &ctx); 3202 handled = true; 3203 break; 3204 default: 3205 break; 3206 } 3207 3208 skip_to_post_handle: 3209 if (msg_result != RTE_VHOST_MSG_RESULT_ERR && 3210 dev->extern_ops.post_msg_handle) { 3211 RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0); 3212 msg_result = (*dev->extern_ops.post_msg_handle)(dev->vid, &ctx); 3213 switch (msg_result) { 3214 case RTE_VHOST_MSG_RESULT_REPLY: 3215 send_vhost_reply(dev, fd, &ctx); 3216 /* Fall-through */ 3217 case RTE_VHOST_MSG_RESULT_ERR: 3218 case RTE_VHOST_MSG_RESULT_OK: 3219 handled = true; 3220 case RTE_VHOST_MSG_RESULT_NOT_HANDLED: 3221 default: 3222 break; 3223 } 3224 } 3225 3226 /* If message was not handled at this stage, treat it as an error */ 3227 if (!handled) { 3228 VHOST_LOG_CONFIG(dev->ifname, ERR, 3229 "vhost message (req: %d) was not handled.\n", 3230 request); 3231 close_msg_fds(&ctx); 3232 msg_result = RTE_VHOST_MSG_RESULT_ERR; 3233 } 3234 3235 /* 3236 * If the request required a reply that was already sent, 3237 * this optional reply-ack won't be sent as the 3238 * VHOST_USER_NEED_REPLY was cleared in send_vhost_reply(). 3239 */ 3240 if (ctx.msg.flags & VHOST_USER_NEED_REPLY) { 3241 ctx.msg.payload.u64 = msg_result == RTE_VHOST_MSG_RESULT_ERR; 3242 ctx.msg.size = sizeof(ctx.msg.payload.u64); 3243 ctx.fd_num = 0; 3244 send_vhost_reply(dev, fd, &ctx); 3245 } else if (msg_result == RTE_VHOST_MSG_RESULT_ERR) { 3246 VHOST_LOG_CONFIG(dev->ifname, ERR, "vhost message handling failed.\n"); 3247 ret = -1; 3248 goto unlock; 3249 } 3250 3251 for (i = 0; i < dev->nr_vring; i++) { 3252 struct vhost_virtqueue *vq = dev->virtqueue[i]; 3253 bool cur_ready = vq_is_ready(dev, vq); 3254 3255 if (cur_ready != (vq && vq->ready)) { 3256 vq->ready = cur_ready; 3257 vhost_user_notify_queue_state(dev, vq, cur_ready); 3258 } 3259 } 3260 3261 unlock: 3262 if (unlock_required) 3263 vhost_user_unlock_all_queue_pairs(dev); 3264 3265 if (ret != 0 || !virtio_is_ready(dev)) 3266 goto out; 3267 3268 /* 3269 * Virtio is now ready. If not done already, it is time 3270 * to notify the application it can process the rings and 3271 * configure the vDPA device if present. 3272 */ 3273 3274 if (!(dev->flags & VIRTIO_DEV_RUNNING)) { 3275 if (dev->notify_ops->new_device(dev->vid) == 0) 3276 dev->flags |= VIRTIO_DEV_RUNNING; 3277 } 3278 3279 vdpa_dev = dev->vdpa_dev; 3280 if (!vdpa_dev) 3281 goto out; 3282 3283 if (vdpa_dev->type == RTE_VHOST_VDPA_DEVICE_TYPE_BLK) { 3284 if (request == VHOST_USER_SET_VRING_CALL) { 3285 blk_call_fd = ctx.msg.payload.u64 & VHOST_USER_VRING_IDX_MASK; 3286 if (blk_call_fd != dev->nr_vring - 1) 3287 goto out; 3288 } else { 3289 goto out; 3290 } 3291 } 3292 3293 if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) { 3294 if (vdpa_dev->ops->dev_conf(dev->vid)) 3295 VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to configure vDPA device\n"); 3296 else 3297 dev->flags |= VIRTIO_DEV_VDPA_CONFIGURED; 3298 } 3299 3300 out: 3301 return ret; 3302 } 3303 3304 int 3305 vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm) 3306 { 3307 int ret; 3308 struct vhu_msg_context ctx = { 3309 .msg = { 3310 .request.slave = VHOST_USER_SLAVE_IOTLB_MSG, 3311 .flags = VHOST_USER_VERSION, 3312 .size = sizeof(ctx.msg.payload.iotlb), 3313 .payload.iotlb = { 3314 .iova = iova, 3315 .perm = perm, 3316 .type = VHOST_IOTLB_MISS, 3317 }, 3318 }, 3319 }; 3320 3321 ret = send_vhost_message(dev, dev->slave_req_fd, &ctx); 3322 if (ret < 0) { 3323 VHOST_LOG_CONFIG(dev->ifname, ERR, 3324 "failed to send IOTLB miss message (%d)\n", 3325 ret); 3326 return ret; 3327 } 3328 3329 return 0; 3330 } 3331 3332 int 3333 rte_vhost_slave_config_change(int vid, bool need_reply) 3334 { 3335 struct vhu_msg_context ctx = { 3336 .msg = { 3337 .request.slave = VHOST_USER_SLAVE_CONFIG_CHANGE_MSG, 3338 .flags = VHOST_USER_VERSION, 3339 .size = 0, 3340 } 3341 }; 3342 struct virtio_net *dev; 3343 int ret; 3344 3345 dev = get_device(vid); 3346 if (!dev) 3347 return -ENODEV; 3348 3349 if (!need_reply) { 3350 ret = send_vhost_slave_message(dev, &ctx); 3351 } else { 3352 ctx.msg.flags |= VHOST_USER_NEED_REPLY; 3353 ret = send_vhost_slave_message_process_reply(dev, &ctx); 3354 } 3355 3356 if (ret < 0) 3357 VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to send config change (%d)\n", ret); 3358 return ret; 3359 } 3360 3361 static int vhost_user_slave_set_vring_host_notifier(struct virtio_net *dev, 3362 int index, int fd, 3363 uint64_t offset, 3364 uint64_t size) 3365 { 3366 int ret; 3367 struct vhu_msg_context ctx = { 3368 .msg = { 3369 .request.slave = VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG, 3370 .flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY, 3371 .size = sizeof(ctx.msg.payload.area), 3372 .payload.area = { 3373 .u64 = index & VHOST_USER_VRING_IDX_MASK, 3374 .size = size, 3375 .offset = offset, 3376 }, 3377 }, 3378 }; 3379 3380 if (fd < 0) 3381 ctx.msg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK; 3382 else { 3383 ctx.fds[0] = fd; 3384 ctx.fd_num = 1; 3385 } 3386 3387 ret = send_vhost_slave_message_process_reply(dev, &ctx); 3388 if (ret < 0) 3389 VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to set host notifier (%d)\n", ret); 3390 3391 return ret; 3392 } 3393 3394 int rte_vhost_host_notifier_ctrl(int vid, uint16_t qid, bool enable) 3395 { 3396 struct virtio_net *dev; 3397 struct rte_vdpa_device *vdpa_dev; 3398 int vfio_device_fd, ret = 0; 3399 uint64_t offset, size; 3400 unsigned int i, q_start, q_last; 3401 3402 dev = get_device(vid); 3403 if (!dev) 3404 return -ENODEV; 3405 3406 vdpa_dev = dev->vdpa_dev; 3407 if (vdpa_dev == NULL) 3408 return -ENODEV; 3409 3410 if (!(dev->features & (1ULL << VIRTIO_F_VERSION_1)) || 3411 !(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) || 3412 !(dev->protocol_features & 3413 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ)) || 3414 !(dev->protocol_features & 3415 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD)) || 3416 !(dev->protocol_features & 3417 (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER))) 3418 return -ENOTSUP; 3419 3420 if (qid == RTE_VHOST_QUEUE_ALL) { 3421 q_start = 0; 3422 q_last = dev->nr_vring - 1; 3423 } else { 3424 if (qid >= dev->nr_vring) 3425 return -EINVAL; 3426 q_start = qid; 3427 q_last = qid; 3428 } 3429 3430 if (vdpa_dev->ops->get_vfio_device_fd == NULL) 3431 return -ENOTSUP; 3432 if (vdpa_dev->ops->get_notify_area == NULL) 3433 return -ENOTSUP; 3434 3435 vfio_device_fd = vdpa_dev->ops->get_vfio_device_fd(vid); 3436 if (vfio_device_fd < 0) 3437 return -ENOTSUP; 3438 3439 if (enable) { 3440 for (i = q_start; i <= q_last; i++) { 3441 if (vdpa_dev->ops->get_notify_area(vid, i, &offset, 3442 &size) < 0) { 3443 ret = -ENOTSUP; 3444 goto disable; 3445 } 3446 3447 if (vhost_user_slave_set_vring_host_notifier(dev, i, 3448 vfio_device_fd, offset, size) < 0) { 3449 ret = -EFAULT; 3450 goto disable; 3451 } 3452 } 3453 } else { 3454 disable: 3455 for (i = q_start; i <= q_last; i++) { 3456 vhost_user_slave_set_vring_host_notifier(dev, i, -1, 3457 0, 0); 3458 } 3459 } 3460 3461 return ret; 3462 } 3463