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