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 /* vhost_user_lock_all_queue_pairs locked all qps */ 990 VHOST_USER_ASSERT_LOCK(dev, vq, VHOST_USER_SET_VRING_ADDR); 991 992 access_ok = vq->access_ok; 993 994 /* 995 * Rings addresses should not be interpreted as long as the ring is not 996 * started and enabled 997 */ 998 memcpy(&vq->ring_addrs, addr, sizeof(*addr)); 999 1000 vring_invalidate(dev, vq); 1001 1002 if ((vq->enabled && (dev->features & 1003 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) || 1004 access_ok) { 1005 translate_ring_addresses(&dev, &vq); 1006 *pdev = dev; 1007 } 1008 1009 return RTE_VHOST_MSG_RESULT_OK; 1010 } 1011 1012 /* 1013 * The virtio device sends us the available ring last used index. 1014 */ 1015 static int 1016 vhost_user_set_vring_base(struct virtio_net **pdev, 1017 struct vhu_msg_context *ctx, 1018 int main_fd __rte_unused) 1019 { 1020 struct virtio_net *dev = *pdev; 1021 struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index]; 1022 uint64_t val = ctx->msg.payload.state.num; 1023 1024 if (vq_is_packed(dev)) { 1025 /* 1026 * Bit[0:14]: avail index 1027 * Bit[15]: avail wrap counter 1028 */ 1029 vq->last_avail_idx = val & 0x7fff; 1030 vq->avail_wrap_counter = !!(val & (0x1 << 15)); 1031 /* 1032 * Set used index to same value as available one, as 1033 * their values should be the same since ring processing 1034 * was stopped at get time. 1035 */ 1036 vq->last_used_idx = vq->last_avail_idx; 1037 vq->used_wrap_counter = vq->avail_wrap_counter; 1038 } else { 1039 vq->last_used_idx = ctx->msg.payload.state.num; 1040 vq->last_avail_idx = ctx->msg.payload.state.num; 1041 } 1042 1043 VHOST_CONFIG_LOG(dev->ifname, INFO, 1044 "vring base idx:%u last_used_idx:%u last_avail_idx:%u.", 1045 ctx->msg.payload.state.index, vq->last_used_idx, vq->last_avail_idx); 1046 1047 return RTE_VHOST_MSG_RESULT_OK; 1048 } 1049 1050 static int 1051 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr, 1052 uint64_t host_iova, uint64_t host_user_addr, uint64_t size) 1053 { 1054 struct guest_page *page, *last_page; 1055 struct guest_page *old_pages; 1056 1057 if (dev->nr_guest_pages == dev->max_guest_pages) { 1058 dev->max_guest_pages *= 2; 1059 old_pages = dev->guest_pages; 1060 dev->guest_pages = rte_realloc(dev->guest_pages, 1061 dev->max_guest_pages * sizeof(*page), 1062 RTE_CACHE_LINE_SIZE); 1063 if (dev->guest_pages == NULL) { 1064 VHOST_CONFIG_LOG(dev->ifname, ERR, "cannot realloc guest_pages"); 1065 rte_free(old_pages); 1066 return -1; 1067 } 1068 } 1069 1070 if (dev->nr_guest_pages > 0) { 1071 last_page = &dev->guest_pages[dev->nr_guest_pages - 1]; 1072 /* merge if the two pages are continuous */ 1073 if (host_iova == last_page->host_iova + last_page->size && 1074 guest_phys_addr == last_page->guest_phys_addr + last_page->size && 1075 host_user_addr == last_page->host_user_addr + last_page->size) { 1076 last_page->size += size; 1077 return 0; 1078 } 1079 } 1080 1081 page = &dev->guest_pages[dev->nr_guest_pages++]; 1082 page->guest_phys_addr = guest_phys_addr; 1083 page->host_iova = host_iova; 1084 page->host_user_addr = host_user_addr; 1085 page->size = size; 1086 1087 return 0; 1088 } 1089 1090 static int 1091 add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg, 1092 uint64_t page_size) 1093 { 1094 uint64_t reg_size = reg->size; 1095 uint64_t host_user_addr = reg->host_user_addr; 1096 uint64_t guest_phys_addr = reg->guest_phys_addr; 1097 uint64_t host_iova; 1098 uint64_t size; 1099 1100 host_iova = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr); 1101 size = page_size - (guest_phys_addr & (page_size - 1)); 1102 size = RTE_MIN(size, reg_size); 1103 1104 if (add_one_guest_page(dev, guest_phys_addr, host_iova, 1105 host_user_addr, size) < 0) 1106 return -1; 1107 1108 host_user_addr += size; 1109 guest_phys_addr += size; 1110 reg_size -= size; 1111 1112 while (reg_size > 0) { 1113 size = RTE_MIN(reg_size, page_size); 1114 host_iova = rte_mem_virt2iova((void *)(uintptr_t) 1115 host_user_addr); 1116 if (add_one_guest_page(dev, guest_phys_addr, host_iova, 1117 host_user_addr, size) < 0) 1118 return -1; 1119 1120 host_user_addr += size; 1121 guest_phys_addr += size; 1122 reg_size -= size; 1123 } 1124 1125 /* sort guest page array if over binary search threshold */ 1126 if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) { 1127 qsort((void *)dev->guest_pages, dev->nr_guest_pages, 1128 sizeof(struct guest_page), guest_page_addrcmp); 1129 } 1130 1131 return 0; 1132 } 1133 1134 #ifdef RTE_LIBRTE_VHOST_DEBUG 1135 /* TODO: enable it only in debug mode? */ 1136 static void 1137 dump_guest_pages(struct virtio_net *dev) 1138 { 1139 uint32_t i; 1140 struct guest_page *page; 1141 1142 for (i = 0; i < dev->nr_guest_pages; i++) { 1143 page = &dev->guest_pages[i]; 1144 1145 VHOST_CONFIG_LOG(dev->ifname, INFO, "guest physical page region %u", i); 1146 VHOST_CONFIG_LOG(dev->ifname, INFO, "\tguest_phys_addr: %" PRIx64, 1147 page->guest_phys_addr); 1148 VHOST_CONFIG_LOG(dev->ifname, INFO, "\thost_iova : %" PRIx64, 1149 page->host_iova); 1150 VHOST_CONFIG_LOG(dev->ifname, INFO, "\tsize : %" PRIx64, 1151 page->size); 1152 } 1153 } 1154 #else 1155 #define dump_guest_pages(dev) 1156 #endif 1157 1158 static bool 1159 vhost_memory_changed(struct VhostUserMemory *new, 1160 struct rte_vhost_memory *old) 1161 { 1162 uint32_t i; 1163 1164 if (new->nregions != old->nregions) 1165 return true; 1166 1167 for (i = 0; i < new->nregions; ++i) { 1168 VhostUserMemoryRegion *new_r = &new->regions[i]; 1169 struct rte_vhost_mem_region *old_r = &old->regions[i]; 1170 1171 if (new_r->guest_phys_addr != old_r->guest_phys_addr) 1172 return true; 1173 if (new_r->memory_size != old_r->size) 1174 return true; 1175 if (new_r->userspace_addr != old_r->guest_user_addr) 1176 return true; 1177 } 1178 1179 return false; 1180 } 1181 1182 #ifdef RTE_LIBRTE_VHOST_POSTCOPY 1183 static int 1184 vhost_user_postcopy_region_register(struct virtio_net *dev, 1185 struct rte_vhost_mem_region *reg) 1186 { 1187 struct uffdio_register reg_struct; 1188 1189 /* 1190 * Let's register all the mmapped area to ensure 1191 * alignment on page boundary. 1192 */ 1193 reg_struct.range.start = (uint64_t)(uintptr_t)reg->mmap_addr; 1194 reg_struct.range.len = reg->mmap_size; 1195 reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING; 1196 1197 if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER, 1198 ®_struct)) { 1199 VHOST_CONFIG_LOG(dev->ifname, ERR, 1200 "failed to register ufd for region " 1201 "%" PRIx64 " - %" PRIx64 " (ufd = %d) %s", 1202 (uint64_t)reg_struct.range.start, 1203 (uint64_t)reg_struct.range.start + 1204 (uint64_t)reg_struct.range.len - 1, 1205 dev->postcopy_ufd, 1206 strerror(errno)); 1207 return -1; 1208 } 1209 1210 VHOST_CONFIG_LOG(dev->ifname, INFO, 1211 "\t userfaultfd registered for range : %" PRIx64 " - %" PRIx64, 1212 (uint64_t)reg_struct.range.start, 1213 (uint64_t)reg_struct.range.start + 1214 (uint64_t)reg_struct.range.len - 1); 1215 1216 return 0; 1217 } 1218 #else 1219 static int 1220 vhost_user_postcopy_region_register(struct virtio_net *dev __rte_unused, 1221 struct rte_vhost_mem_region *reg __rte_unused) 1222 { 1223 return -1; 1224 } 1225 #endif 1226 1227 static int 1228 vhost_user_postcopy_register(struct virtio_net *dev, int main_fd, 1229 struct vhu_msg_context *ctx) 1230 { 1231 struct VhostUserMemory *memory; 1232 struct rte_vhost_mem_region *reg; 1233 struct vhu_msg_context ack_ctx; 1234 uint32_t i; 1235 1236 if (!dev->postcopy_listening) 1237 return 0; 1238 1239 /* 1240 * We haven't a better way right now than sharing 1241 * DPDK's virtual address with Qemu, so that Qemu can 1242 * retrieve the region offset when handling userfaults. 1243 */ 1244 memory = &ctx->msg.payload.memory; 1245 for (i = 0; i < memory->nregions; i++) { 1246 reg = &dev->mem->regions[i]; 1247 memory->regions[i].userspace_addr = reg->host_user_addr; 1248 } 1249 1250 /* Send the addresses back to qemu */ 1251 ctx->fd_num = 0; 1252 send_vhost_reply(dev, main_fd, ctx); 1253 1254 /* Wait for qemu to acknowledge it got the addresses 1255 * we've got to wait before we're allowed to generate faults. 1256 */ 1257 if (read_vhost_message(dev, main_fd, &ack_ctx) <= 0) { 1258 VHOST_CONFIG_LOG(dev->ifname, ERR, 1259 "failed to read qemu ack on postcopy set-mem-table"); 1260 return -1; 1261 } 1262 1263 if (validate_msg_fds(dev, &ack_ctx, 0) != 0) 1264 return -1; 1265 1266 if (ack_ctx.msg.request.frontend != VHOST_USER_SET_MEM_TABLE) { 1267 VHOST_CONFIG_LOG(dev->ifname, ERR, 1268 "bad qemu ack on postcopy set-mem-table (%d)", 1269 ack_ctx.msg.request.frontend); 1270 return -1; 1271 } 1272 1273 /* Now userfault register and we can use the memory */ 1274 for (i = 0; i < memory->nregions; i++) { 1275 reg = &dev->mem->regions[i]; 1276 if (vhost_user_postcopy_region_register(dev, reg) < 0) 1277 return -1; 1278 } 1279 1280 return 0; 1281 } 1282 1283 static int 1284 vhost_user_mmap_region(struct virtio_net *dev, 1285 struct rte_vhost_mem_region *region, 1286 uint64_t mmap_offset) 1287 { 1288 void *mmap_addr; 1289 uint64_t mmap_size; 1290 uint64_t alignment; 1291 int populate; 1292 1293 /* Check for memory_size + mmap_offset overflow */ 1294 if (mmap_offset >= -region->size) { 1295 VHOST_CONFIG_LOG(dev->ifname, ERR, 1296 "mmap_offset (%#"PRIx64") and memory_size (%#"PRIx64") overflow", 1297 mmap_offset, region->size); 1298 return -1; 1299 } 1300 1301 mmap_size = region->size + mmap_offset; 1302 1303 /* mmap() without flag of MAP_ANONYMOUS, should be called with length 1304 * argument aligned with hugepagesz at older longterm version Linux, 1305 * like 2.6.32 and 3.2.72, or mmap() will fail with EINVAL. 1306 * 1307 * To avoid failure, make sure in caller to keep length aligned. 1308 */ 1309 alignment = get_blk_size(region->fd); 1310 if (alignment == (uint64_t)-1) { 1311 VHOST_CONFIG_LOG(dev->ifname, ERR, "couldn't get hugepage size through fstat"); 1312 return -1; 1313 } 1314 mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment); 1315 if (mmap_size == 0) { 1316 /* 1317 * It could happen if initial mmap_size + alignment overflows 1318 * the sizeof uint64, which could happen if either mmap_size or 1319 * alignment value is wrong. 1320 * 1321 * mmap() kernel implementation would return an error, but 1322 * better catch it before and provide useful info in the logs. 1323 */ 1324 VHOST_CONFIG_LOG(dev->ifname, ERR, 1325 "mmap size (0x%" PRIx64 ") or alignment (0x%" PRIx64 ") is invalid", 1326 region->size + mmap_offset, alignment); 1327 return -1; 1328 } 1329 1330 populate = dev->async_copy ? MAP_POPULATE : 0; 1331 mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, 1332 MAP_SHARED | populate, region->fd, 0); 1333 1334 if (mmap_addr == MAP_FAILED) { 1335 VHOST_CONFIG_LOG(dev->ifname, ERR, "mmap failed (%s).", strerror(errno)); 1336 return -1; 1337 } 1338 1339 region->mmap_addr = mmap_addr; 1340 region->mmap_size = mmap_size; 1341 region->host_user_addr = (uint64_t)(uintptr_t)mmap_addr + mmap_offset; 1342 mem_set_dump(dev, mmap_addr, mmap_size, false, alignment); 1343 1344 if (dev->async_copy) { 1345 if (add_guest_pages(dev, region, alignment) < 0) { 1346 VHOST_CONFIG_LOG(dev->ifname, ERR, 1347 "adding guest pages to region failed."); 1348 return -1; 1349 } 1350 } 1351 1352 VHOST_CONFIG_LOG(dev->ifname, INFO, 1353 "guest memory region size: 0x%" PRIx64, 1354 region->size); 1355 VHOST_CONFIG_LOG(dev->ifname, INFO, 1356 "\t guest physical addr: 0x%" PRIx64, 1357 region->guest_phys_addr); 1358 VHOST_CONFIG_LOG(dev->ifname, INFO, 1359 "\t guest virtual addr: 0x%" PRIx64, 1360 region->guest_user_addr); 1361 VHOST_CONFIG_LOG(dev->ifname, INFO, 1362 "\t host virtual addr: 0x%" PRIx64, 1363 region->host_user_addr); 1364 VHOST_CONFIG_LOG(dev->ifname, INFO, 1365 "\t mmap addr : 0x%" PRIx64, 1366 (uint64_t)(uintptr_t)mmap_addr); 1367 VHOST_CONFIG_LOG(dev->ifname, INFO, 1368 "\t mmap size : 0x%" PRIx64, 1369 mmap_size); 1370 VHOST_CONFIG_LOG(dev->ifname, INFO, 1371 "\t mmap align: 0x%" PRIx64, 1372 alignment); 1373 VHOST_CONFIG_LOG(dev->ifname, INFO, 1374 "\t mmap off : 0x%" PRIx64, 1375 mmap_offset); 1376 1377 return 0; 1378 } 1379 1380 static int 1381 vhost_user_set_mem_table(struct virtio_net **pdev, 1382 struct vhu_msg_context *ctx, 1383 int main_fd) 1384 { 1385 struct virtio_net *dev = *pdev; 1386 struct VhostUserMemory *memory = &ctx->msg.payload.memory; 1387 struct rte_vhost_mem_region *reg; 1388 int numa_node = SOCKET_ID_ANY; 1389 uint64_t mmap_offset; 1390 uint32_t i; 1391 bool async_notify = false; 1392 1393 if (validate_msg_fds(dev, ctx, memory->nregions) != 0) 1394 return RTE_VHOST_MSG_RESULT_ERR; 1395 1396 if (memory->nregions > VHOST_MEMORY_MAX_NREGIONS) { 1397 VHOST_CONFIG_LOG(dev->ifname, ERR, 1398 "too many memory regions (%u)", 1399 memory->nregions); 1400 goto close_msg_fds; 1401 } 1402 1403 if (dev->mem && !vhost_memory_changed(memory, dev->mem)) { 1404 VHOST_CONFIG_LOG(dev->ifname, INFO, "memory regions not changed"); 1405 1406 close_msg_fds(ctx); 1407 1408 return RTE_VHOST_MSG_RESULT_OK; 1409 } 1410 1411 if (dev->mem) { 1412 if (dev->flags & VIRTIO_DEV_VDPA_CONFIGURED) { 1413 struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev; 1414 1415 if (vdpa_dev && vdpa_dev->ops->dev_close) 1416 vdpa_dev->ops->dev_close(dev->vid); 1417 dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED; 1418 } 1419 1420 /* notify the vhost application to stop DMA transfers */ 1421 if (dev->async_copy && dev->notify_ops->vring_state_changed) { 1422 for (i = 0; i < dev->nr_vring; i++) { 1423 dev->notify_ops->vring_state_changed(dev->vid, 1424 i, 0); 1425 } 1426 async_notify = true; 1427 } 1428 1429 /* Flush IOTLB cache as previous HVAs are now invalid */ 1430 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) 1431 vhost_user_iotlb_flush_all(dev); 1432 1433 free_mem_region(dev); 1434 rte_free(dev->mem); 1435 dev->mem = NULL; 1436 } 1437 1438 /* 1439 * If VQ 0 has already been allocated, try to allocate on the same 1440 * NUMA node. It can be reallocated later in numa_realloc(). 1441 */ 1442 if (dev->nr_vring > 0) 1443 numa_node = dev->virtqueue[0]->numa_node; 1444 1445 dev->nr_guest_pages = 0; 1446 if (dev->guest_pages == NULL) { 1447 dev->max_guest_pages = 8; 1448 dev->guest_pages = rte_zmalloc_socket(NULL, 1449 dev->max_guest_pages * 1450 sizeof(struct guest_page), 1451 RTE_CACHE_LINE_SIZE, 1452 numa_node); 1453 if (dev->guest_pages == NULL) { 1454 VHOST_CONFIG_LOG(dev->ifname, ERR, 1455 "failed to allocate memory for dev->guest_pages"); 1456 goto close_msg_fds; 1457 } 1458 } 1459 1460 dev->mem = rte_zmalloc_socket("vhost-mem-table", sizeof(struct rte_vhost_memory) + 1461 sizeof(struct rte_vhost_mem_region) * memory->nregions, 0, numa_node); 1462 if (dev->mem == NULL) { 1463 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to allocate memory for dev->mem"); 1464 goto free_guest_pages; 1465 } 1466 1467 for (i = 0; i < memory->nregions; i++) { 1468 reg = &dev->mem->regions[i]; 1469 1470 reg->guest_phys_addr = memory->regions[i].guest_phys_addr; 1471 reg->guest_user_addr = memory->regions[i].userspace_addr; 1472 reg->size = memory->regions[i].memory_size; 1473 reg->fd = ctx->fds[i]; 1474 1475 /* 1476 * Assign invalid file descriptor value to avoid double 1477 * closing on error path. 1478 */ 1479 ctx->fds[i] = -1; 1480 1481 mmap_offset = memory->regions[i].mmap_offset; 1482 1483 if (vhost_user_mmap_region(dev, reg, mmap_offset) < 0) { 1484 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to mmap region %u", i); 1485 goto free_mem_table; 1486 } 1487 1488 dev->mem->nregions++; 1489 } 1490 1491 if (dev->async_copy && rte_vfio_is_enabled("vfio")) 1492 async_dma_map(dev, true); 1493 1494 if (vhost_user_postcopy_register(dev, main_fd, ctx) < 0) 1495 goto free_mem_table; 1496 1497 for (i = 0; i < dev->nr_vring; i++) { 1498 struct vhost_virtqueue *vq = dev->virtqueue[i]; 1499 1500 if (!vq) 1501 continue; 1502 1503 if (vq->desc || vq->avail || vq->used) { 1504 /* vhost_user_lock_all_queue_pairs locked all qps */ 1505 VHOST_USER_ASSERT_LOCK(dev, vq, VHOST_USER_SET_MEM_TABLE); 1506 1507 /* 1508 * If the memory table got updated, the ring addresses 1509 * need to be translated again as virtual addresses have 1510 * changed. 1511 */ 1512 vring_invalidate(dev, vq); 1513 1514 translate_ring_addresses(&dev, &vq); 1515 *pdev = dev; 1516 } 1517 } 1518 1519 dump_guest_pages(dev); 1520 1521 if (async_notify) { 1522 for (i = 0; i < dev->nr_vring; i++) 1523 dev->notify_ops->vring_state_changed(dev->vid, i, 1); 1524 } 1525 1526 return RTE_VHOST_MSG_RESULT_OK; 1527 1528 free_mem_table: 1529 free_mem_region(dev); 1530 rte_free(dev->mem); 1531 dev->mem = NULL; 1532 1533 free_guest_pages: 1534 rte_free(dev->guest_pages); 1535 dev->guest_pages = NULL; 1536 close_msg_fds: 1537 close_msg_fds(ctx); 1538 return RTE_VHOST_MSG_RESULT_ERR; 1539 } 1540 1541 static bool 1542 vq_is_ready(struct virtio_net *dev, struct vhost_virtqueue *vq) 1543 { 1544 bool rings_ok; 1545 1546 if (!vq) 1547 return false; 1548 1549 if (vq_is_packed(dev)) 1550 rings_ok = vq->desc_packed && vq->driver_event && 1551 vq->device_event; 1552 else 1553 rings_ok = vq->desc && vq->avail && vq->used; 1554 1555 return rings_ok && 1556 vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD && 1557 vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD && 1558 vq->enabled; 1559 } 1560 1561 #define VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY 2u 1562 #define VIRTIO_BLK_NUM_VQS_TO_BE_READY 1u 1563 1564 static int 1565 virtio_is_ready(struct virtio_net *dev) 1566 { 1567 struct rte_vdpa_device *vdpa_dev; 1568 struct vhost_virtqueue *vq; 1569 uint32_t vdpa_type; 1570 uint32_t i, nr_vring = dev->nr_vring; 1571 1572 if (dev->flags & VIRTIO_DEV_READY) 1573 return 1; 1574 1575 if (!dev->nr_vring) 1576 return 0; 1577 1578 vdpa_dev = dev->vdpa_dev; 1579 if (vdpa_dev) 1580 vdpa_type = vdpa_dev->type; 1581 else 1582 vdpa_type = -1; 1583 1584 if (vdpa_type == RTE_VHOST_VDPA_DEVICE_TYPE_BLK) { 1585 nr_vring = VIRTIO_BLK_NUM_VQS_TO_BE_READY; 1586 } else { 1587 if (dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) 1588 nr_vring = VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY; 1589 } 1590 1591 if (dev->nr_vring < nr_vring) 1592 return 0; 1593 1594 for (i = 0; i < nr_vring; i++) { 1595 vq = dev->virtqueue[i]; 1596 1597 if (!vq_is_ready(dev, vq)) 1598 return 0; 1599 } 1600 1601 /* If supported, ensure the frontend is really done with config */ 1602 if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS)) 1603 if (!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK)) 1604 return 0; 1605 1606 dev->flags |= VIRTIO_DEV_READY; 1607 1608 if (!(dev->flags & VIRTIO_DEV_RUNNING)) 1609 VHOST_CONFIG_LOG(dev->ifname, INFO, "virtio is now ready for processing."); 1610 return 1; 1611 } 1612 1613 static void * 1614 inflight_mem_alloc(struct virtio_net *dev, const char *name, size_t size, int *fd) 1615 { 1616 void *ptr; 1617 int mfd = -1; 1618 uint64_t alignment; 1619 char fname[20] = "/tmp/memfd-XXXXXX"; 1620 1621 *fd = -1; 1622 #ifdef MEMFD_SUPPORTED 1623 mfd = memfd_create(name, MFD_CLOEXEC); 1624 #else 1625 RTE_SET_USED(name); 1626 #endif 1627 if (mfd == -1) { 1628 mfd = mkstemp(fname); 1629 if (mfd == -1) { 1630 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to get inflight buffer fd"); 1631 return NULL; 1632 } 1633 1634 unlink(fname); 1635 } 1636 1637 if (ftruncate(mfd, size) == -1) { 1638 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc inflight buffer"); 1639 close(mfd); 1640 return NULL; 1641 } 1642 1643 ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0); 1644 if (ptr == MAP_FAILED) { 1645 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to mmap inflight buffer"); 1646 close(mfd); 1647 return NULL; 1648 } 1649 1650 alignment = get_blk_size(mfd); 1651 mem_set_dump(dev, ptr, size, false, alignment); 1652 *fd = mfd; 1653 return ptr; 1654 } 1655 1656 static uint32_t 1657 get_pervq_shm_size_split(uint16_t queue_size) 1658 { 1659 return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_split) * 1660 queue_size + sizeof(uint64_t) + 1661 sizeof(uint16_t) * 4, INFLIGHT_ALIGNMENT); 1662 } 1663 1664 static uint32_t 1665 get_pervq_shm_size_packed(uint16_t queue_size) 1666 { 1667 return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_packed) 1668 * queue_size + sizeof(uint64_t) + 1669 sizeof(uint16_t) * 6 + sizeof(uint8_t) * 9, 1670 INFLIGHT_ALIGNMENT); 1671 } 1672 1673 static int 1674 vhost_user_get_inflight_fd(struct virtio_net **pdev, 1675 struct vhu_msg_context *ctx, 1676 int main_fd __rte_unused) 1677 { 1678 struct rte_vhost_inflight_info_packed *inflight_packed; 1679 uint64_t pervq_inflight_size, mmap_size; 1680 uint16_t num_queues, queue_size; 1681 struct virtio_net *dev = *pdev; 1682 int fd, i, j; 1683 int numa_node = SOCKET_ID_ANY; 1684 void *addr; 1685 1686 if (ctx->msg.size != sizeof(ctx->msg.payload.inflight)) { 1687 VHOST_CONFIG_LOG(dev->ifname, ERR, 1688 "invalid get_inflight_fd message size is %d", 1689 ctx->msg.size); 1690 return RTE_VHOST_MSG_RESULT_ERR; 1691 } 1692 1693 /* 1694 * If VQ 0 has already been allocated, try to allocate on the same 1695 * NUMA node. It can be reallocated later in numa_realloc(). 1696 */ 1697 if (dev->nr_vring > 0) 1698 numa_node = dev->virtqueue[0]->numa_node; 1699 1700 if (dev->inflight_info == NULL) { 1701 dev->inflight_info = rte_zmalloc_socket("inflight_info", 1702 sizeof(struct inflight_mem_info), 0, numa_node); 1703 if (!dev->inflight_info) { 1704 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc dev inflight area"); 1705 return RTE_VHOST_MSG_RESULT_ERR; 1706 } 1707 dev->inflight_info->fd = -1; 1708 } 1709 1710 num_queues = ctx->msg.payload.inflight.num_queues; 1711 queue_size = ctx->msg.payload.inflight.queue_size; 1712 1713 VHOST_CONFIG_LOG(dev->ifname, INFO, 1714 "get_inflight_fd num_queues: %u", 1715 ctx->msg.payload.inflight.num_queues); 1716 VHOST_CONFIG_LOG(dev->ifname, INFO, 1717 "get_inflight_fd queue_size: %u", 1718 ctx->msg.payload.inflight.queue_size); 1719 1720 if (vq_is_packed(dev)) 1721 pervq_inflight_size = get_pervq_shm_size_packed(queue_size); 1722 else 1723 pervq_inflight_size = get_pervq_shm_size_split(queue_size); 1724 1725 mmap_size = num_queues * pervq_inflight_size; 1726 addr = inflight_mem_alloc(dev, "vhost-inflight", mmap_size, &fd); 1727 if (!addr) { 1728 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc vhost inflight area"); 1729 ctx->msg.payload.inflight.mmap_size = 0; 1730 return RTE_VHOST_MSG_RESULT_ERR; 1731 } 1732 memset(addr, 0, mmap_size); 1733 1734 if (dev->inflight_info->addr) { 1735 munmap(dev->inflight_info->addr, dev->inflight_info->size); 1736 dev->inflight_info->addr = NULL; 1737 } 1738 1739 if (dev->inflight_info->fd >= 0) { 1740 close(dev->inflight_info->fd); 1741 dev->inflight_info->fd = -1; 1742 } 1743 1744 dev->inflight_info->addr = addr; 1745 dev->inflight_info->size = ctx->msg.payload.inflight.mmap_size = mmap_size; 1746 dev->inflight_info->fd = ctx->fds[0] = fd; 1747 ctx->msg.payload.inflight.mmap_offset = 0; 1748 ctx->fd_num = 1; 1749 1750 if (vq_is_packed(dev)) { 1751 for (i = 0; i < num_queues; i++) { 1752 inflight_packed = 1753 (struct rte_vhost_inflight_info_packed *)addr; 1754 inflight_packed->used_wrap_counter = 1; 1755 inflight_packed->old_used_wrap_counter = 1; 1756 for (j = 0; j < queue_size; j++) 1757 inflight_packed->desc[j].next = j + 1; 1758 addr = (void *)((char *)addr + pervq_inflight_size); 1759 } 1760 } 1761 1762 VHOST_CONFIG_LOG(dev->ifname, INFO, 1763 "send inflight mmap_size: %"PRIu64, 1764 ctx->msg.payload.inflight.mmap_size); 1765 VHOST_CONFIG_LOG(dev->ifname, INFO, 1766 "send inflight mmap_offset: %"PRIu64, 1767 ctx->msg.payload.inflight.mmap_offset); 1768 VHOST_CONFIG_LOG(dev->ifname, INFO, 1769 "send inflight fd: %d", ctx->fds[0]); 1770 1771 return RTE_VHOST_MSG_RESULT_REPLY; 1772 } 1773 1774 static int 1775 vhost_user_set_inflight_fd(struct virtio_net **pdev, 1776 struct vhu_msg_context *ctx, 1777 int main_fd __rte_unused) 1778 { 1779 uint64_t mmap_size, mmap_offset; 1780 uint16_t num_queues, queue_size; 1781 struct virtio_net *dev = *pdev; 1782 uint32_t pervq_inflight_size; 1783 struct vhost_virtqueue *vq; 1784 void *addr; 1785 int fd, i; 1786 int numa_node = SOCKET_ID_ANY; 1787 1788 if (validate_msg_fds(dev, ctx, 1) != 0) 1789 return RTE_VHOST_MSG_RESULT_ERR; 1790 1791 fd = ctx->fds[0]; 1792 if (ctx->msg.size != sizeof(ctx->msg.payload.inflight) || fd < 0) { 1793 VHOST_CONFIG_LOG(dev->ifname, ERR, 1794 "invalid set_inflight_fd message size is %d,fd is %d", 1795 ctx->msg.size, fd); 1796 return RTE_VHOST_MSG_RESULT_ERR; 1797 } 1798 1799 mmap_size = ctx->msg.payload.inflight.mmap_size; 1800 mmap_offset = ctx->msg.payload.inflight.mmap_offset; 1801 num_queues = ctx->msg.payload.inflight.num_queues; 1802 queue_size = ctx->msg.payload.inflight.queue_size; 1803 1804 if (vq_is_packed(dev)) 1805 pervq_inflight_size = get_pervq_shm_size_packed(queue_size); 1806 else 1807 pervq_inflight_size = get_pervq_shm_size_split(queue_size); 1808 1809 VHOST_CONFIG_LOG(dev->ifname, INFO, "set_inflight_fd mmap_size: %"PRIu64, mmap_size); 1810 VHOST_CONFIG_LOG(dev->ifname, INFO, 1811 "set_inflight_fd mmap_offset: %"PRIu64, 1812 mmap_offset); 1813 VHOST_CONFIG_LOG(dev->ifname, INFO, 1814 "set_inflight_fd num_queues: %u", 1815 num_queues); 1816 VHOST_CONFIG_LOG(dev->ifname, INFO, 1817 "set_inflight_fd queue_size: %u", 1818 queue_size); 1819 VHOST_CONFIG_LOG(dev->ifname, INFO, 1820 "set_inflight_fd fd: %d", 1821 fd); 1822 VHOST_CONFIG_LOG(dev->ifname, INFO, 1823 "set_inflight_fd pervq_inflight_size: %d", 1824 pervq_inflight_size); 1825 1826 /* 1827 * If VQ 0 has already been allocated, try to allocate on the same 1828 * NUMA node. It can be reallocated later in numa_realloc(). 1829 */ 1830 if (dev->nr_vring > 0) 1831 numa_node = dev->virtqueue[0]->numa_node; 1832 1833 if (!dev->inflight_info) { 1834 dev->inflight_info = rte_zmalloc_socket("inflight_info", 1835 sizeof(struct inflight_mem_info), 0, numa_node); 1836 if (dev->inflight_info == NULL) { 1837 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc dev inflight area"); 1838 return RTE_VHOST_MSG_RESULT_ERR; 1839 } 1840 dev->inflight_info->fd = -1; 1841 } 1842 1843 if (dev->inflight_info->addr) { 1844 munmap(dev->inflight_info->addr, dev->inflight_info->size); 1845 dev->inflight_info->addr = NULL; 1846 } 1847 1848 addr = mmap(0, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, 1849 fd, mmap_offset); 1850 if (addr == MAP_FAILED) { 1851 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to mmap share memory."); 1852 return RTE_VHOST_MSG_RESULT_ERR; 1853 } 1854 1855 if (dev->inflight_info->fd >= 0) { 1856 close(dev->inflight_info->fd); 1857 dev->inflight_info->fd = -1; 1858 } 1859 1860 mem_set_dump(dev, addr, mmap_size, false, get_blk_size(fd)); 1861 dev->inflight_info->fd = fd; 1862 dev->inflight_info->addr = addr; 1863 dev->inflight_info->size = mmap_size; 1864 1865 for (i = 0; i < num_queues; i++) { 1866 vq = dev->virtqueue[i]; 1867 if (!vq) 1868 continue; 1869 1870 if (vq_is_packed(dev)) { 1871 vq->inflight_packed = addr; 1872 vq->inflight_packed->desc_num = queue_size; 1873 } else { 1874 vq->inflight_split = addr; 1875 vq->inflight_split->desc_num = queue_size; 1876 } 1877 addr = (void *)((char *)addr + pervq_inflight_size); 1878 } 1879 1880 return RTE_VHOST_MSG_RESULT_OK; 1881 } 1882 1883 static int 1884 vhost_user_set_vring_call(struct virtio_net **pdev, 1885 struct vhu_msg_context *ctx, 1886 int main_fd __rte_unused) 1887 { 1888 struct virtio_net *dev = *pdev; 1889 struct vhost_vring_file file; 1890 struct vhost_virtqueue *vq; 1891 int expected_fds; 1892 1893 expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1; 1894 if (validate_msg_fds(dev, ctx, expected_fds) != 0) 1895 return RTE_VHOST_MSG_RESULT_ERR; 1896 1897 file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK; 1898 if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) 1899 file.fd = VIRTIO_INVALID_EVENTFD; 1900 else 1901 file.fd = ctx->fds[0]; 1902 VHOST_CONFIG_LOG(dev->ifname, INFO, 1903 "vring call idx:%d file:%d", 1904 file.index, file.fd); 1905 1906 vq = dev->virtqueue[file.index]; 1907 1908 if (vq->ready) { 1909 vq->ready = false; 1910 vhost_user_notify_queue_state(dev, vq, 0); 1911 } 1912 1913 if (vq->callfd >= 0) 1914 close(vq->callfd); 1915 1916 vq->callfd = file.fd; 1917 1918 return RTE_VHOST_MSG_RESULT_OK; 1919 } 1920 1921 static int vhost_user_set_vring_err(struct virtio_net **pdev, 1922 struct vhu_msg_context *ctx, 1923 int main_fd __rte_unused) 1924 { 1925 struct virtio_net *dev = *pdev; 1926 int expected_fds; 1927 1928 expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1; 1929 if (validate_msg_fds(dev, ctx, expected_fds) != 0) 1930 return RTE_VHOST_MSG_RESULT_ERR; 1931 1932 if (!(ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)) 1933 close(ctx->fds[0]); 1934 VHOST_CONFIG_LOG(dev->ifname, DEBUG, "not implemented"); 1935 1936 return RTE_VHOST_MSG_RESULT_OK; 1937 } 1938 1939 static int 1940 resubmit_desc_compare(const void *a, const void *b) 1941 { 1942 const struct rte_vhost_resubmit_desc *desc0 = a; 1943 const struct rte_vhost_resubmit_desc *desc1 = b; 1944 1945 if (desc1->counter > desc0->counter) 1946 return 1; 1947 1948 return -1; 1949 } 1950 1951 static int 1952 vhost_check_queue_inflights_split(struct virtio_net *dev, 1953 struct vhost_virtqueue *vq) 1954 { 1955 uint16_t i; 1956 uint16_t resubmit_num = 0, last_io, num; 1957 struct vring_used *used = vq->used; 1958 struct rte_vhost_resubmit_info *resubmit; 1959 struct rte_vhost_inflight_info_split *inflight_split; 1960 1961 if (!(dev->protocol_features & 1962 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))) 1963 return RTE_VHOST_MSG_RESULT_OK; 1964 1965 /* The frontend may still not support the inflight feature 1966 * although we negotiate the protocol feature. 1967 */ 1968 if ((!vq->inflight_split)) 1969 return RTE_VHOST_MSG_RESULT_OK; 1970 1971 if (!vq->inflight_split->version) { 1972 vq->inflight_split->version = INFLIGHT_VERSION; 1973 return RTE_VHOST_MSG_RESULT_OK; 1974 } 1975 1976 if (vq->resubmit_inflight) 1977 return RTE_VHOST_MSG_RESULT_OK; 1978 1979 inflight_split = vq->inflight_split; 1980 vq->global_counter = 0; 1981 last_io = inflight_split->last_inflight_io; 1982 1983 if (inflight_split->used_idx != used->idx) { 1984 inflight_split->desc[last_io].inflight = 0; 1985 rte_atomic_thread_fence(rte_memory_order_seq_cst); 1986 inflight_split->used_idx = used->idx; 1987 } 1988 1989 for (i = 0; i < inflight_split->desc_num; i++) { 1990 if (inflight_split->desc[i].inflight == 1) 1991 resubmit_num++; 1992 } 1993 1994 vq->last_avail_idx += resubmit_num; 1995 1996 if (resubmit_num) { 1997 resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info), 1998 0, vq->numa_node); 1999 if (!resubmit) { 2000 VHOST_CONFIG_LOG(dev->ifname, ERR, 2001 "failed to allocate memory for resubmit info."); 2002 return RTE_VHOST_MSG_RESULT_ERR; 2003 } 2004 2005 resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list", 2006 resubmit_num * sizeof(struct rte_vhost_resubmit_desc), 2007 0, vq->numa_node); 2008 if (!resubmit->resubmit_list) { 2009 VHOST_CONFIG_LOG(dev->ifname, ERR, 2010 "failed to allocate memory for inflight desc."); 2011 rte_free(resubmit); 2012 return RTE_VHOST_MSG_RESULT_ERR; 2013 } 2014 2015 num = 0; 2016 for (i = 0; i < vq->inflight_split->desc_num; i++) { 2017 if (vq->inflight_split->desc[i].inflight == 1) { 2018 resubmit->resubmit_list[num].index = i; 2019 resubmit->resubmit_list[num].counter = 2020 inflight_split->desc[i].counter; 2021 num++; 2022 } 2023 } 2024 resubmit->resubmit_num = num; 2025 2026 if (resubmit->resubmit_num > 1) 2027 qsort(resubmit->resubmit_list, resubmit->resubmit_num, 2028 sizeof(struct rte_vhost_resubmit_desc), 2029 resubmit_desc_compare); 2030 2031 vq->global_counter = resubmit->resubmit_list[0].counter + 1; 2032 vq->resubmit_inflight = resubmit; 2033 } 2034 2035 return RTE_VHOST_MSG_RESULT_OK; 2036 } 2037 2038 static int 2039 vhost_check_queue_inflights_packed(struct virtio_net *dev, 2040 struct vhost_virtqueue *vq) 2041 { 2042 uint16_t i; 2043 uint16_t resubmit_num = 0, old_used_idx, num; 2044 struct rte_vhost_resubmit_info *resubmit; 2045 struct rte_vhost_inflight_info_packed *inflight_packed; 2046 2047 if (!(dev->protocol_features & 2048 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))) 2049 return RTE_VHOST_MSG_RESULT_OK; 2050 2051 /* The frontend may still not support the inflight feature 2052 * although we negotiate the protocol feature. 2053 */ 2054 if ((!vq->inflight_packed)) 2055 return RTE_VHOST_MSG_RESULT_OK; 2056 2057 if (!vq->inflight_packed->version) { 2058 vq->inflight_packed->version = INFLIGHT_VERSION; 2059 return RTE_VHOST_MSG_RESULT_OK; 2060 } 2061 2062 if (vq->resubmit_inflight) 2063 return RTE_VHOST_MSG_RESULT_OK; 2064 2065 inflight_packed = vq->inflight_packed; 2066 vq->global_counter = 0; 2067 old_used_idx = inflight_packed->old_used_idx; 2068 2069 if (inflight_packed->used_idx != old_used_idx) { 2070 if (inflight_packed->desc[old_used_idx].inflight == 0) { 2071 inflight_packed->old_used_idx = 2072 inflight_packed->used_idx; 2073 inflight_packed->old_used_wrap_counter = 2074 inflight_packed->used_wrap_counter; 2075 inflight_packed->old_free_head = 2076 inflight_packed->free_head; 2077 } else { 2078 inflight_packed->used_idx = 2079 inflight_packed->old_used_idx; 2080 inflight_packed->used_wrap_counter = 2081 inflight_packed->old_used_wrap_counter; 2082 inflight_packed->free_head = 2083 inflight_packed->old_free_head; 2084 } 2085 } 2086 2087 for (i = 0; i < inflight_packed->desc_num; i++) { 2088 if (inflight_packed->desc[i].inflight == 1) 2089 resubmit_num++; 2090 } 2091 2092 if (resubmit_num) { 2093 resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info), 2094 0, vq->numa_node); 2095 if (resubmit == NULL) { 2096 VHOST_CONFIG_LOG(dev->ifname, ERR, 2097 "failed to allocate memory for resubmit info."); 2098 return RTE_VHOST_MSG_RESULT_ERR; 2099 } 2100 2101 resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list", 2102 resubmit_num * sizeof(struct rte_vhost_resubmit_desc), 2103 0, vq->numa_node); 2104 if (resubmit->resubmit_list == NULL) { 2105 VHOST_CONFIG_LOG(dev->ifname, ERR, 2106 "failed to allocate memory for resubmit desc."); 2107 rte_free(resubmit); 2108 return RTE_VHOST_MSG_RESULT_ERR; 2109 } 2110 2111 num = 0; 2112 for (i = 0; i < inflight_packed->desc_num; i++) { 2113 if (vq->inflight_packed->desc[i].inflight == 1) { 2114 resubmit->resubmit_list[num].index = i; 2115 resubmit->resubmit_list[num].counter = 2116 inflight_packed->desc[i].counter; 2117 num++; 2118 } 2119 } 2120 resubmit->resubmit_num = num; 2121 2122 if (resubmit->resubmit_num > 1) 2123 qsort(resubmit->resubmit_list, resubmit->resubmit_num, 2124 sizeof(struct rte_vhost_resubmit_desc), 2125 resubmit_desc_compare); 2126 2127 vq->global_counter = resubmit->resubmit_list[0].counter + 1; 2128 vq->resubmit_inflight = resubmit; 2129 } 2130 2131 return RTE_VHOST_MSG_RESULT_OK; 2132 } 2133 2134 static int 2135 vhost_user_set_vring_kick(struct virtio_net **pdev, 2136 struct vhu_msg_context *ctx, 2137 int main_fd __rte_unused) 2138 { 2139 struct virtio_net *dev = *pdev; 2140 struct vhost_vring_file file; 2141 struct vhost_virtqueue *vq; 2142 int expected_fds; 2143 2144 expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1; 2145 if (validate_msg_fds(dev, ctx, expected_fds) != 0) 2146 return RTE_VHOST_MSG_RESULT_ERR; 2147 2148 file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK; 2149 if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) 2150 file.fd = VIRTIO_INVALID_EVENTFD; 2151 else 2152 file.fd = ctx->fds[0]; 2153 VHOST_CONFIG_LOG(dev->ifname, INFO, 2154 "vring kick idx:%d file:%d", 2155 file.index, file.fd); 2156 2157 /* Interpret ring addresses only when ring is started. */ 2158 vq = dev->virtqueue[file.index]; 2159 translate_ring_addresses(&dev, &vq); 2160 *pdev = dev; 2161 2162 /* 2163 * When VHOST_USER_F_PROTOCOL_FEATURES is not negotiated, 2164 * the ring starts already enabled. Otherwise, it is enabled via 2165 * the SET_VRING_ENABLE message. 2166 */ 2167 if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) { 2168 vq->enabled = true; 2169 } 2170 2171 if (vq->ready) { 2172 vq->ready = false; 2173 vhost_user_notify_queue_state(dev, vq, 0); 2174 } 2175 2176 if (vq->kickfd >= 0) 2177 close(vq->kickfd); 2178 vq->kickfd = file.fd; 2179 2180 if (vq_is_packed(dev)) { 2181 if (vhost_check_queue_inflights_packed(dev, vq)) { 2182 VHOST_CONFIG_LOG(dev->ifname, ERR, 2183 "failed to inflights for vq: %d", 2184 file.index); 2185 return RTE_VHOST_MSG_RESULT_ERR; 2186 } 2187 } else { 2188 if (vhost_check_queue_inflights_split(dev, vq)) { 2189 VHOST_CONFIG_LOG(dev->ifname, ERR, 2190 "failed to inflights for vq: %d", 2191 file.index); 2192 return RTE_VHOST_MSG_RESULT_ERR; 2193 } 2194 } 2195 2196 return RTE_VHOST_MSG_RESULT_OK; 2197 } 2198 2199 /* 2200 * when virtio is stopped, qemu will send us the GET_VRING_BASE message. 2201 */ 2202 static int 2203 vhost_user_get_vring_base(struct virtio_net **pdev, 2204 struct vhu_msg_context *ctx, 2205 int main_fd __rte_unused) 2206 { 2207 struct virtio_net *dev = *pdev; 2208 struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index]; 2209 uint64_t val; 2210 2211 /* We have to stop the queue (virtio) if it is running. */ 2212 vhost_destroy_device_notify(dev); 2213 2214 dev->flags &= ~VIRTIO_DEV_READY; 2215 dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED; 2216 2217 /* Here we are safe to get the indexes */ 2218 if (vq_is_packed(dev)) { 2219 /* 2220 * Bit[0:14]: avail index 2221 * Bit[15]: avail wrap counter 2222 */ 2223 val = vq->last_avail_idx & 0x7fff; 2224 val |= vq->avail_wrap_counter << 15; 2225 ctx->msg.payload.state.num = val; 2226 } else { 2227 ctx->msg.payload.state.num = vq->last_avail_idx; 2228 } 2229 2230 VHOST_CONFIG_LOG(dev->ifname, INFO, 2231 "vring base idx:%d file:%d", 2232 ctx->msg.payload.state.index, ctx->msg.payload.state.num); 2233 /* 2234 * Based on current qemu vhost-user implementation, this message is 2235 * sent and only sent in vhost_vring_stop. 2236 * TODO: cleanup the vring, it isn't usable since here. 2237 */ 2238 if (vq->kickfd >= 0) 2239 close(vq->kickfd); 2240 2241 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD; 2242 2243 if (vq->callfd >= 0) 2244 close(vq->callfd); 2245 2246 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD; 2247 2248 vq->signalled_used_valid = false; 2249 2250 if (vq_is_packed(dev)) { 2251 rte_free(vq->shadow_used_packed); 2252 vq->shadow_used_packed = NULL; 2253 } else { 2254 rte_free(vq->shadow_used_split); 2255 vq->shadow_used_split = NULL; 2256 } 2257 2258 rte_free(vq->batch_copy_elems); 2259 vq->batch_copy_elems = NULL; 2260 2261 rte_free(vq->log_cache); 2262 vq->log_cache = NULL; 2263 2264 ctx->msg.size = sizeof(ctx->msg.payload.state); 2265 ctx->fd_num = 0; 2266 2267 vhost_user_iotlb_flush_all(dev); 2268 2269 rte_rwlock_write_lock(&vq->access_lock); 2270 vring_invalidate(dev, vq); 2271 rte_rwlock_write_unlock(&vq->access_lock); 2272 2273 return RTE_VHOST_MSG_RESULT_REPLY; 2274 } 2275 2276 /* 2277 * when virtio queues are ready to work, qemu will send us to 2278 * enable the virtio queue pair. 2279 */ 2280 static int 2281 vhost_user_set_vring_enable(struct virtio_net **pdev, 2282 struct vhu_msg_context *ctx, 2283 int main_fd __rte_unused) 2284 { 2285 struct virtio_net *dev = *pdev; 2286 struct vhost_virtqueue *vq; 2287 bool enable = !!ctx->msg.payload.state.num; 2288 int index = (int)ctx->msg.payload.state.index; 2289 2290 VHOST_CONFIG_LOG(dev->ifname, INFO, 2291 "set queue enable: %d to qp idx: %d", 2292 enable, index); 2293 2294 vq = dev->virtqueue[index]; 2295 if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) { 2296 /* vhost_user_lock_all_queue_pairs locked all qps */ 2297 VHOST_USER_ASSERT_LOCK(dev, vq, VHOST_USER_SET_VRING_ENABLE); 2298 if (enable && vq->async && vq->async->pkts_inflight_n) { 2299 VHOST_CONFIG_LOG(dev->ifname, ERR, 2300 "failed to enable vring. Inflight packets must be completed first"); 2301 return RTE_VHOST_MSG_RESULT_ERR; 2302 } 2303 } 2304 2305 vq->enabled = enable; 2306 2307 return RTE_VHOST_MSG_RESULT_OK; 2308 } 2309 2310 static int 2311 vhost_user_get_protocol_features(struct virtio_net **pdev, 2312 struct vhu_msg_context *ctx, 2313 int main_fd __rte_unused) 2314 { 2315 struct virtio_net *dev = *pdev; 2316 uint64_t features, protocol_features; 2317 2318 rte_vhost_driver_get_features(dev->ifname, &features); 2319 rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features); 2320 2321 ctx->msg.payload.u64 = protocol_features; 2322 ctx->msg.size = sizeof(ctx->msg.payload.u64); 2323 ctx->fd_num = 0; 2324 2325 return RTE_VHOST_MSG_RESULT_REPLY; 2326 } 2327 2328 static int 2329 vhost_user_set_protocol_features(struct virtio_net **pdev, 2330 struct vhu_msg_context *ctx, 2331 int main_fd __rte_unused) 2332 { 2333 struct virtio_net *dev = *pdev; 2334 uint64_t protocol_features = ctx->msg.payload.u64; 2335 uint64_t backend_protocol_features = 0; 2336 2337 rte_vhost_driver_get_protocol_features(dev->ifname, 2338 &backend_protocol_features); 2339 if (protocol_features & ~backend_protocol_features) { 2340 VHOST_CONFIG_LOG(dev->ifname, ERR, "received invalid protocol features."); 2341 return RTE_VHOST_MSG_RESULT_ERR; 2342 } 2343 2344 dev->protocol_features = protocol_features; 2345 VHOST_CONFIG_LOG(dev->ifname, INFO, 2346 "negotiated Vhost-user protocol features: 0x%" PRIx64, 2347 dev->protocol_features); 2348 2349 return RTE_VHOST_MSG_RESULT_OK; 2350 } 2351 2352 static int 2353 vhost_user_set_log_base(struct virtio_net **pdev, 2354 struct vhu_msg_context *ctx, 2355 int main_fd __rte_unused) 2356 { 2357 struct virtio_net *dev = *pdev; 2358 int fd = ctx->fds[0]; 2359 uint64_t size, off; 2360 uint64_t alignment; 2361 void *addr; 2362 uint32_t i; 2363 2364 if (validate_msg_fds(dev, ctx, 1) != 0) 2365 return RTE_VHOST_MSG_RESULT_ERR; 2366 2367 if (fd < 0) { 2368 VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid log fd: %d", fd); 2369 return RTE_VHOST_MSG_RESULT_ERR; 2370 } 2371 2372 if (ctx->msg.size != sizeof(VhostUserLog)) { 2373 VHOST_CONFIG_LOG(dev->ifname, ERR, 2374 "invalid log base msg size: %"PRId32" != %d", 2375 ctx->msg.size, (int)sizeof(VhostUserLog)); 2376 goto close_msg_fds; 2377 } 2378 2379 size = ctx->msg.payload.log.mmap_size; 2380 off = ctx->msg.payload.log.mmap_offset; 2381 2382 /* Check for mmap size and offset overflow. */ 2383 if (off >= -size) { 2384 VHOST_CONFIG_LOG(dev->ifname, ERR, 2385 "log offset %#"PRIx64" and log size %#"PRIx64" overflow", 2386 off, size); 2387 goto close_msg_fds; 2388 } 2389 2390 VHOST_CONFIG_LOG(dev->ifname, INFO, 2391 "log mmap size: %"PRId64", offset: %"PRId64, 2392 size, off); 2393 2394 /* 2395 * mmap from 0 to workaround a hugepage mmap bug: mmap will 2396 * fail when offset is not page size aligned. 2397 */ 2398 addr = mmap(0, size + off, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 2399 alignment = get_blk_size(fd); 2400 close(fd); 2401 if (addr == MAP_FAILED) { 2402 VHOST_CONFIG_LOG(dev->ifname, ERR, "mmap log base failed!"); 2403 return RTE_VHOST_MSG_RESULT_ERR; 2404 } 2405 2406 /* 2407 * Free previously mapped log memory on occasionally 2408 * multiple VHOST_USER_SET_LOG_BASE. 2409 */ 2410 if (dev->log_addr) { 2411 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size); 2412 } 2413 dev->log_addr = (uint64_t)(uintptr_t)addr; 2414 dev->log_base = dev->log_addr + off; 2415 dev->log_size = size; 2416 mem_set_dump(dev, addr, size + off, false, alignment); 2417 2418 for (i = 0; i < dev->nr_vring; i++) { 2419 struct vhost_virtqueue *vq = dev->virtqueue[i]; 2420 2421 rte_free(vq->log_cache); 2422 vq->log_cache = NULL; 2423 vq->log_cache_nb_elem = 0; 2424 vq->log_cache = rte_malloc_socket("vq log cache", 2425 sizeof(struct log_cache_entry) * VHOST_LOG_CACHE_NR, 2426 0, vq->numa_node); 2427 /* 2428 * If log cache alloc fail, don't fail migration, but no 2429 * caching will be done, which will impact performance 2430 */ 2431 if (!vq->log_cache) 2432 VHOST_CONFIG_LOG(dev->ifname, ERR, 2433 "failed to allocate VQ logging cache"); 2434 } 2435 2436 /* 2437 * The spec is not clear about it (yet), but QEMU doesn't expect 2438 * any payload in the reply. 2439 */ 2440 ctx->msg.size = 0; 2441 ctx->fd_num = 0; 2442 2443 return RTE_VHOST_MSG_RESULT_REPLY; 2444 2445 close_msg_fds: 2446 close_msg_fds(ctx); 2447 return RTE_VHOST_MSG_RESULT_ERR; 2448 } 2449 2450 static int vhost_user_set_log_fd(struct virtio_net **pdev, 2451 struct vhu_msg_context *ctx, 2452 int main_fd __rte_unused) 2453 { 2454 struct virtio_net *dev = *pdev; 2455 2456 if (validate_msg_fds(dev, ctx, 1) != 0) 2457 return RTE_VHOST_MSG_RESULT_ERR; 2458 2459 close(ctx->fds[0]); 2460 VHOST_CONFIG_LOG(dev->ifname, DEBUG, "not implemented."); 2461 2462 return RTE_VHOST_MSG_RESULT_OK; 2463 } 2464 2465 /* 2466 * An rarp packet is constructed and broadcasted to notify switches about 2467 * the new location of the migrated VM, so that packets from outside will 2468 * not be lost after migration. 2469 * 2470 * However, we don't actually "send" a rarp packet here, instead, we set 2471 * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it. 2472 */ 2473 static int 2474 vhost_user_send_rarp(struct virtio_net **pdev, 2475 struct vhu_msg_context *ctx, 2476 int main_fd __rte_unused) 2477 { 2478 struct virtio_net *dev = *pdev; 2479 uint8_t *mac = (uint8_t *)&ctx->msg.payload.u64; 2480 struct rte_vdpa_device *vdpa_dev; 2481 2482 VHOST_CONFIG_LOG(dev->ifname, DEBUG, 2483 "MAC: " RTE_ETHER_ADDR_PRT_FMT, 2484 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 2485 memcpy(dev->mac.addr_bytes, mac, 6); 2486 2487 /* 2488 * Set the flag to inject a RARP broadcast packet at 2489 * rte_vhost_dequeue_burst(). 2490 * 2491 * rte_memory_order_release ordering is for making sure the mac is 2492 * copied before the flag is set. 2493 */ 2494 rte_atomic_store_explicit(&dev->broadcast_rarp, 1, rte_memory_order_release); 2495 vdpa_dev = dev->vdpa_dev; 2496 if (vdpa_dev && vdpa_dev->ops->migration_done) 2497 vdpa_dev->ops->migration_done(dev->vid); 2498 2499 return RTE_VHOST_MSG_RESULT_OK; 2500 } 2501 2502 static int 2503 vhost_user_net_set_mtu(struct virtio_net **pdev, 2504 struct vhu_msg_context *ctx, 2505 int main_fd __rte_unused) 2506 { 2507 struct virtio_net *dev = *pdev; 2508 2509 if (ctx->msg.payload.u64 < VIRTIO_MIN_MTU || 2510 ctx->msg.payload.u64 > VIRTIO_MAX_MTU) { 2511 VHOST_CONFIG_LOG(dev->ifname, ERR, 2512 "invalid MTU size (%"PRIu64")", 2513 ctx->msg.payload.u64); 2514 2515 return RTE_VHOST_MSG_RESULT_ERR; 2516 } 2517 2518 dev->mtu = ctx->msg.payload.u64; 2519 2520 return RTE_VHOST_MSG_RESULT_OK; 2521 } 2522 2523 static int 2524 vhost_user_set_req_fd(struct virtio_net **pdev, 2525 struct vhu_msg_context *ctx, 2526 int main_fd __rte_unused) 2527 { 2528 struct virtio_net *dev = *pdev; 2529 int fd = ctx->fds[0]; 2530 2531 if (validate_msg_fds(dev, ctx, 1) != 0) 2532 return RTE_VHOST_MSG_RESULT_ERR; 2533 2534 if (fd < 0) { 2535 VHOST_CONFIG_LOG(dev->ifname, ERR, 2536 "invalid file descriptor for backend channel (%d)", fd); 2537 return RTE_VHOST_MSG_RESULT_ERR; 2538 } 2539 2540 if (dev->backend_req_fd >= 0) 2541 close(dev->backend_req_fd); 2542 2543 dev->backend_req_fd = fd; 2544 2545 return RTE_VHOST_MSG_RESULT_OK; 2546 } 2547 2548 static int 2549 is_vring_iotlb_split(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg) 2550 { 2551 struct vhost_vring_addr *ra; 2552 uint64_t start, end, len; 2553 2554 start = imsg->iova; 2555 end = start + imsg->size; 2556 2557 ra = &vq->ring_addrs; 2558 len = sizeof(struct vring_desc) * vq->size; 2559 if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start) 2560 return 1; 2561 2562 len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size; 2563 if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start) 2564 return 1; 2565 2566 len = sizeof(struct vring_used) + 2567 sizeof(struct vring_used_elem) * vq->size; 2568 if (ra->used_user_addr < end && (ra->used_user_addr + len) > start) 2569 return 1; 2570 2571 if (ra->flags & (1 << VHOST_VRING_F_LOG)) { 2572 len = sizeof(uint64_t); 2573 if (ra->log_guest_addr < end && 2574 (ra->log_guest_addr + len) > start) 2575 return 1; 2576 } 2577 2578 return 0; 2579 } 2580 2581 static int 2582 is_vring_iotlb_packed(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg) 2583 { 2584 struct vhost_vring_addr *ra; 2585 uint64_t start, end, len; 2586 2587 start = imsg->iova; 2588 end = start + imsg->size; 2589 2590 ra = &vq->ring_addrs; 2591 len = sizeof(struct vring_packed_desc) * vq->size; 2592 if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start) 2593 return 1; 2594 2595 len = sizeof(struct vring_packed_desc_event); 2596 if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start) 2597 return 1; 2598 2599 len = sizeof(struct vring_packed_desc_event); 2600 if (ra->used_user_addr < end && (ra->used_user_addr + len) > start) 2601 return 1; 2602 2603 if (ra->flags & (1 << VHOST_VRING_F_LOG)) { 2604 len = sizeof(uint64_t); 2605 if (ra->log_guest_addr < end && 2606 (ra->log_guest_addr + len) > start) 2607 return 1; 2608 } 2609 2610 return 0; 2611 } 2612 2613 static int is_vring_iotlb(struct virtio_net *dev, 2614 struct vhost_virtqueue *vq, 2615 struct vhost_iotlb_msg *imsg) 2616 { 2617 if (vq_is_packed(dev)) 2618 return is_vring_iotlb_packed(vq, imsg); 2619 else 2620 return is_vring_iotlb_split(vq, imsg); 2621 } 2622 2623 static int 2624 vhost_user_get_config(struct virtio_net **pdev, 2625 struct vhu_msg_context *ctx, 2626 int main_fd __rte_unused) 2627 { 2628 struct virtio_net *dev = *pdev; 2629 struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev; 2630 int ret = 0; 2631 2632 if (validate_msg_fds(dev, ctx, 0) != 0) 2633 return RTE_VHOST_MSG_RESULT_ERR; 2634 2635 if (!vdpa_dev) { 2636 VHOST_CONFIG_LOG(dev->ifname, ERR, "is not vDPA device!"); 2637 return RTE_VHOST_MSG_RESULT_ERR; 2638 } 2639 2640 if (vdpa_dev->ops->get_config) { 2641 ret = vdpa_dev->ops->get_config(dev->vid, 2642 ctx->msg.payload.cfg.region, 2643 ctx->msg.payload.cfg.size); 2644 if (ret != 0) { 2645 ctx->msg.size = 0; 2646 VHOST_CONFIG_LOG(dev->ifname, ERR, "get_config() return error!"); 2647 } 2648 } else { 2649 VHOST_CONFIG_LOG(dev->ifname, ERR, "get_config() not supported!"); 2650 } 2651 2652 return RTE_VHOST_MSG_RESULT_REPLY; 2653 } 2654 2655 static int 2656 vhost_user_set_config(struct virtio_net **pdev, 2657 struct vhu_msg_context *ctx, 2658 int main_fd __rte_unused) 2659 { 2660 struct virtio_net *dev = *pdev; 2661 struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev; 2662 int ret = 0; 2663 2664 if (validate_msg_fds(dev, ctx, 0) != 0) 2665 return RTE_VHOST_MSG_RESULT_ERR; 2666 2667 if (ctx->msg.payload.cfg.size > VHOST_USER_MAX_CONFIG_SIZE) { 2668 VHOST_CONFIG_LOG(dev->ifname, ERR, 2669 "vhost_user_config size: %"PRIu32", should not be larger than %d", 2670 ctx->msg.payload.cfg.size, VHOST_USER_MAX_CONFIG_SIZE); 2671 goto out; 2672 } 2673 2674 if (!vdpa_dev) { 2675 VHOST_CONFIG_LOG(dev->ifname, ERR, "is not vDPA device!"); 2676 goto out; 2677 } 2678 2679 if (vdpa_dev->ops->set_config) { 2680 ret = vdpa_dev->ops->set_config(dev->vid, 2681 ctx->msg.payload.cfg.region, 2682 ctx->msg.payload.cfg.offset, 2683 ctx->msg.payload.cfg.size, 2684 ctx->msg.payload.cfg.flags); 2685 if (ret) 2686 VHOST_CONFIG_LOG(dev->ifname, ERR, "set_config() return error!"); 2687 } else { 2688 VHOST_CONFIG_LOG(dev->ifname, ERR, "set_config() not supported!"); 2689 } 2690 2691 return RTE_VHOST_MSG_RESULT_OK; 2692 2693 out: 2694 return RTE_VHOST_MSG_RESULT_ERR; 2695 } 2696 2697 static int 2698 vhost_user_iotlb_msg(struct virtio_net **pdev, 2699 struct vhu_msg_context *ctx, 2700 int main_fd __rte_unused) 2701 { 2702 struct virtio_net *dev = *pdev; 2703 struct vhost_iotlb_msg *imsg = &ctx->msg.payload.iotlb; 2704 uint16_t i; 2705 uint64_t vva, len, pg_sz; 2706 2707 switch (imsg->type) { 2708 case VHOST_IOTLB_UPDATE: 2709 len = imsg->size; 2710 vva = qva_to_vva(dev, imsg->uaddr, &len); 2711 if (!vva) 2712 return RTE_VHOST_MSG_RESULT_ERR; 2713 2714 pg_sz = hua_to_alignment(dev->mem, (void *)(uintptr_t)vva); 2715 2716 vhost_user_iotlb_cache_insert(dev, imsg->iova, vva, 0, len, pg_sz, imsg->perm); 2717 2718 for (i = 0; i < dev->nr_vring; i++) { 2719 struct vhost_virtqueue *vq = dev->virtqueue[i]; 2720 2721 if (!vq) 2722 continue; 2723 2724 if (is_vring_iotlb(dev, vq, imsg)) { 2725 rte_rwlock_write_lock(&vq->access_lock); 2726 translate_ring_addresses(&dev, &vq); 2727 *pdev = dev; 2728 rte_rwlock_write_unlock(&vq->access_lock); 2729 } 2730 } 2731 break; 2732 case VHOST_IOTLB_INVALIDATE: 2733 vhost_user_iotlb_cache_remove(dev, imsg->iova, imsg->size); 2734 2735 for (i = 0; i < dev->nr_vring; i++) { 2736 struct vhost_virtqueue *vq = dev->virtqueue[i]; 2737 2738 if (!vq) 2739 continue; 2740 2741 if (is_vring_iotlb(dev, vq, imsg)) { 2742 rte_rwlock_write_lock(&vq->access_lock); 2743 vring_invalidate(dev, vq); 2744 rte_rwlock_write_unlock(&vq->access_lock); 2745 } 2746 } 2747 break; 2748 default: 2749 VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid IOTLB message type (%d)", 2750 imsg->type); 2751 return RTE_VHOST_MSG_RESULT_ERR; 2752 } 2753 2754 return RTE_VHOST_MSG_RESULT_OK; 2755 } 2756 2757 static int 2758 vhost_user_set_postcopy_advise(struct virtio_net **pdev, 2759 struct vhu_msg_context *ctx, 2760 int main_fd __rte_unused) 2761 { 2762 struct virtio_net *dev = *pdev; 2763 #ifdef RTE_LIBRTE_VHOST_POSTCOPY 2764 struct uffdio_api api_struct; 2765 2766 dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); 2767 2768 if (dev->postcopy_ufd == -1) { 2769 VHOST_CONFIG_LOG(dev->ifname, ERR, 2770 "userfaultfd not available: %s", 2771 strerror(errno)); 2772 return RTE_VHOST_MSG_RESULT_ERR; 2773 } 2774 api_struct.api = UFFD_API; 2775 api_struct.features = 0; 2776 if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) { 2777 VHOST_CONFIG_LOG(dev->ifname, ERR, 2778 "UFFDIO_API ioctl failure: %s", 2779 strerror(errno)); 2780 close(dev->postcopy_ufd); 2781 dev->postcopy_ufd = -1; 2782 return RTE_VHOST_MSG_RESULT_ERR; 2783 } 2784 ctx->fds[0] = dev->postcopy_ufd; 2785 ctx->fd_num = 1; 2786 2787 return RTE_VHOST_MSG_RESULT_REPLY; 2788 #else 2789 dev->postcopy_ufd = -1; 2790 ctx->fd_num = 0; 2791 2792 return RTE_VHOST_MSG_RESULT_ERR; 2793 #endif 2794 } 2795 2796 static int 2797 vhost_user_set_postcopy_listen(struct virtio_net **pdev, 2798 struct vhu_msg_context *ctx __rte_unused, 2799 int main_fd __rte_unused) 2800 { 2801 struct virtio_net *dev = *pdev; 2802 2803 if (dev->mem && dev->mem->nregions) { 2804 VHOST_CONFIG_LOG(dev->ifname, ERR, 2805 "regions already registered at postcopy-listen"); 2806 return RTE_VHOST_MSG_RESULT_ERR; 2807 } 2808 dev->postcopy_listening = 1; 2809 2810 return RTE_VHOST_MSG_RESULT_OK; 2811 } 2812 2813 static int 2814 vhost_user_postcopy_end(struct virtio_net **pdev, 2815 struct vhu_msg_context *ctx, 2816 int main_fd __rte_unused) 2817 { 2818 struct virtio_net *dev = *pdev; 2819 2820 dev->postcopy_listening = 0; 2821 if (dev->postcopy_ufd >= 0) { 2822 close(dev->postcopy_ufd); 2823 dev->postcopy_ufd = -1; 2824 } 2825 2826 ctx->msg.payload.u64 = 0; 2827 ctx->msg.size = sizeof(ctx->msg.payload.u64); 2828 ctx->fd_num = 0; 2829 2830 return RTE_VHOST_MSG_RESULT_REPLY; 2831 } 2832 2833 static int 2834 vhost_user_get_status(struct virtio_net **pdev, 2835 struct vhu_msg_context *ctx, 2836 int main_fd __rte_unused) 2837 { 2838 struct virtio_net *dev = *pdev; 2839 2840 ctx->msg.payload.u64 = dev->status; 2841 ctx->msg.size = sizeof(ctx->msg.payload.u64); 2842 ctx->fd_num = 0; 2843 2844 return RTE_VHOST_MSG_RESULT_REPLY; 2845 } 2846 2847 static int 2848 vhost_user_set_status(struct virtio_net **pdev, 2849 struct vhu_msg_context *ctx, 2850 int main_fd __rte_unused) 2851 { 2852 struct virtio_net *dev = *pdev; 2853 2854 /* As per Virtio specification, the device status is 8bits long */ 2855 if (ctx->msg.payload.u64 > UINT8_MAX) { 2856 VHOST_CONFIG_LOG(dev->ifname, ERR, 2857 "invalid VHOST_USER_SET_STATUS payload 0x%" PRIx64, 2858 ctx->msg.payload.u64); 2859 return RTE_VHOST_MSG_RESULT_ERR; 2860 } 2861 2862 dev->status = ctx->msg.payload.u64; 2863 2864 if ((dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK) && 2865 (dev->flags & VIRTIO_DEV_FEATURES_FAILED)) { 2866 VHOST_CONFIG_LOG(dev->ifname, ERR, 2867 "FEATURES_OK bit is set but feature negotiation failed"); 2868 /* 2869 * Clear the bit to let the driver know about the feature 2870 * negotiation failure 2871 */ 2872 dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK; 2873 } 2874 2875 VHOST_CONFIG_LOG(dev->ifname, INFO, "new device status(0x%08x):", dev->status); 2876 VHOST_CONFIG_LOG(dev->ifname, INFO, 2877 "\t-RESET: %u", 2878 (dev->status == VIRTIO_DEVICE_STATUS_RESET)); 2879 VHOST_CONFIG_LOG(dev->ifname, INFO, 2880 "\t-ACKNOWLEDGE: %u", 2881 !!(dev->status & VIRTIO_DEVICE_STATUS_ACK)); 2882 VHOST_CONFIG_LOG(dev->ifname, INFO, 2883 "\t-DRIVER: %u", 2884 !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER)); 2885 VHOST_CONFIG_LOG(dev->ifname, INFO, 2886 "\t-FEATURES_OK: %u", 2887 !!(dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK)); 2888 VHOST_CONFIG_LOG(dev->ifname, INFO, 2889 "\t-DRIVER_OK: %u", 2890 !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK)); 2891 VHOST_CONFIG_LOG(dev->ifname, INFO, 2892 "\t-DEVICE_NEED_RESET: %u", 2893 !!(dev->status & VIRTIO_DEVICE_STATUS_DEV_NEED_RESET)); 2894 VHOST_CONFIG_LOG(dev->ifname, INFO, 2895 "\t-FAILED: %u", 2896 !!(dev->status & VIRTIO_DEVICE_STATUS_FAILED)); 2897 2898 return RTE_VHOST_MSG_RESULT_OK; 2899 } 2900 2901 #define VHOST_MESSAGE_HANDLER(id, handler, accepts_fd, lock_all_qps) \ 2902 [id] = { #id, handler, accepts_fd, id ## _LOCK_ALL_QPS }, 2903 static vhost_message_handler_t vhost_message_handlers[] = { 2904 VHOST_MESSAGE_HANDLERS 2905 }; 2906 #undef VHOST_MESSAGE_HANDLER 2907 2908 /* return bytes# of read on success or negative val on failure. */ 2909 static int 2910 read_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx) 2911 { 2912 int ret; 2913 2914 ret = read_fd_message(dev->ifname, sockfd, (char *)&ctx->msg, VHOST_USER_HDR_SIZE, 2915 ctx->fds, VHOST_MEMORY_MAX_NREGIONS, &ctx->fd_num); 2916 if (ret <= 0) 2917 goto out; 2918 2919 if (ret != VHOST_USER_HDR_SIZE) { 2920 VHOST_CONFIG_LOG(dev->ifname, ERR, "Unexpected header size read"); 2921 ret = -1; 2922 goto out; 2923 } 2924 2925 if (ctx->msg.size) { 2926 if (ctx->msg.size > sizeof(ctx->msg.payload)) { 2927 VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid msg size: %d", 2928 ctx->msg.size); 2929 ret = -1; 2930 goto out; 2931 } 2932 ret = read(sockfd, &ctx->msg.payload, ctx->msg.size); 2933 if (ret <= 0) 2934 goto out; 2935 if (ret != (int)ctx->msg.size) { 2936 VHOST_CONFIG_LOG(dev->ifname, ERR, "read control message failed"); 2937 ret = -1; 2938 goto out; 2939 } 2940 } 2941 2942 out: 2943 if (ret <= 0) 2944 close_msg_fds(ctx); 2945 2946 return ret; 2947 } 2948 2949 static int 2950 send_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx) 2951 { 2952 if (!ctx) 2953 return 0; 2954 2955 return send_fd_message(dev->ifname, sockfd, (char *)&ctx->msg, 2956 VHOST_USER_HDR_SIZE + ctx->msg.size, ctx->fds, ctx->fd_num); 2957 } 2958 2959 static int 2960 send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx) 2961 { 2962 if (!ctx) 2963 return 0; 2964 2965 ctx->msg.flags &= ~VHOST_USER_VERSION_MASK; 2966 ctx->msg.flags &= ~VHOST_USER_NEED_REPLY; 2967 ctx->msg.flags |= VHOST_USER_VERSION; 2968 ctx->msg.flags |= VHOST_USER_REPLY_MASK; 2969 2970 return send_vhost_message(dev, sockfd, ctx); 2971 } 2972 2973 static int 2974 send_vhost_backend_message(struct virtio_net *dev, struct vhu_msg_context *ctx) 2975 { 2976 return send_vhost_message(dev, dev->backend_req_fd, ctx); 2977 } 2978 2979 static int 2980 send_vhost_backend_message_process_reply(struct virtio_net *dev, struct vhu_msg_context *ctx) 2981 { 2982 struct vhu_msg_context msg_reply; 2983 int ret; 2984 2985 rte_spinlock_lock(&dev->backend_req_lock); 2986 ret = send_vhost_backend_message(dev, ctx); 2987 if (ret < 0) { 2988 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to send config change (%d)", ret); 2989 goto out; 2990 } 2991 2992 ret = read_vhost_message(dev, dev->backend_req_fd, &msg_reply); 2993 if (ret <= 0) { 2994 if (ret < 0) 2995 VHOST_CONFIG_LOG(dev->ifname, ERR, 2996 "vhost read backend message reply failed"); 2997 else 2998 VHOST_CONFIG_LOG(dev->ifname, INFO, "vhost peer closed"); 2999 ret = -1; 3000 goto out; 3001 } 3002 3003 if (msg_reply.msg.request.backend != ctx->msg.request.backend) { 3004 VHOST_CONFIG_LOG(dev->ifname, ERR, 3005 "received unexpected msg type (%u), expected %u", 3006 msg_reply.msg.request.backend, ctx->msg.request.backend); 3007 ret = -1; 3008 goto out; 3009 } 3010 3011 ret = msg_reply.msg.payload.u64 ? -1 : 0; 3012 out: 3013 rte_spinlock_unlock(&dev->backend_req_lock); 3014 return ret; 3015 } 3016 3017 /* 3018 * Allocate a queue pair if it hasn't been allocated yet 3019 */ 3020 static int 3021 vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev, 3022 struct vhu_msg_context *ctx) 3023 { 3024 uint32_t vring_idx; 3025 3026 switch (ctx->msg.request.frontend) { 3027 case VHOST_USER_SET_VRING_KICK: 3028 case VHOST_USER_SET_VRING_CALL: 3029 case VHOST_USER_SET_VRING_ERR: 3030 vring_idx = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK; 3031 break; 3032 case VHOST_USER_SET_VRING_NUM: 3033 case VHOST_USER_SET_VRING_BASE: 3034 case VHOST_USER_GET_VRING_BASE: 3035 case VHOST_USER_SET_VRING_ENABLE: 3036 vring_idx = ctx->msg.payload.state.index; 3037 break; 3038 case VHOST_USER_SET_VRING_ADDR: 3039 vring_idx = ctx->msg.payload.addr.index; 3040 break; 3041 case VHOST_USER_SET_INFLIGHT_FD: 3042 vring_idx = ctx->msg.payload.inflight.num_queues - 1; 3043 break; 3044 default: 3045 return 0; 3046 } 3047 3048 if (vring_idx >= VHOST_MAX_VRING) { 3049 VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid vring index: %u", vring_idx); 3050 return -1; 3051 } 3052 3053 if (dev->virtqueue[vring_idx]) 3054 return 0; 3055 3056 return alloc_vring_queue(dev, vring_idx); 3057 } 3058 3059 static void 3060 vhost_user_lock_all_queue_pairs(struct virtio_net *dev) 3061 __rte_no_thread_safety_analysis 3062 { 3063 unsigned int i = 0; 3064 unsigned int vq_num = 0; 3065 3066 while (vq_num < dev->nr_vring) { 3067 struct vhost_virtqueue *vq = dev->virtqueue[i]; 3068 3069 if (vq) { 3070 rte_rwlock_write_lock(&vq->access_lock); 3071 vq_num++; 3072 } 3073 i++; 3074 } 3075 } 3076 3077 static void 3078 vhost_user_unlock_all_queue_pairs(struct virtio_net *dev) 3079 __rte_no_thread_safety_analysis 3080 { 3081 unsigned int i = 0; 3082 unsigned int vq_num = 0; 3083 3084 while (vq_num < dev->nr_vring) { 3085 struct vhost_virtqueue *vq = dev->virtqueue[i]; 3086 3087 if (vq) { 3088 rte_rwlock_write_unlock(&vq->access_lock); 3089 vq_num++; 3090 } 3091 i++; 3092 } 3093 } 3094 3095 int 3096 vhost_user_msg_handler(int vid, int fd) 3097 { 3098 struct virtio_net *dev; 3099 struct vhu_msg_context ctx; 3100 vhost_message_handler_t *msg_handler; 3101 struct rte_vdpa_device *vdpa_dev; 3102 int msg_result = RTE_VHOST_MSG_RESULT_OK; 3103 int ret; 3104 int unlock_required = 0; 3105 bool handled; 3106 uint32_t request; 3107 uint32_t i; 3108 uint16_t blk_call_fd; 3109 3110 dev = get_device(vid); 3111 if (dev == NULL) 3112 return -1; 3113 3114 if (!dev->notify_ops) { 3115 dev->notify_ops = vhost_driver_callback_get(dev->ifname); 3116 if (!dev->notify_ops) { 3117 VHOST_CONFIG_LOG(dev->ifname, ERR, 3118 "failed to get callback ops for driver"); 3119 return -1; 3120 } 3121 } 3122 3123 ctx.msg.request.frontend = VHOST_USER_NONE; 3124 ret = read_vhost_message(dev, fd, &ctx); 3125 if (ret == 0) { 3126 VHOST_CONFIG_LOG(dev->ifname, INFO, "vhost peer closed"); 3127 return -1; 3128 } 3129 3130 request = ctx.msg.request.frontend; 3131 if (request > VHOST_USER_NONE && request < RTE_DIM(vhost_message_handlers)) 3132 msg_handler = &vhost_message_handlers[request]; 3133 else 3134 msg_handler = NULL; 3135 3136 if (ret < 0) { 3137 VHOST_CONFIG_LOG(dev->ifname, ERR, "vhost read message %s%s%sfailed", 3138 msg_handler != NULL ? "for " : "", 3139 msg_handler != NULL ? msg_handler->description : "", 3140 msg_handler != NULL ? " " : ""); 3141 return -1; 3142 } 3143 3144 if (msg_handler != NULL && msg_handler->description != NULL) { 3145 if (request != VHOST_USER_IOTLB_MSG) 3146 VHOST_CONFIG_LOG(dev->ifname, INFO, 3147 "read message %s", 3148 msg_handler->description); 3149 else 3150 VHOST_CONFIG_LOG(dev->ifname, DEBUG, 3151 "read message %s", 3152 msg_handler->description); 3153 } else { 3154 VHOST_CONFIG_LOG(dev->ifname, DEBUG, "external request %d", request); 3155 } 3156 3157 ret = vhost_user_check_and_alloc_queue_pair(dev, &ctx); 3158 if (ret < 0) { 3159 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc queue"); 3160 return -1; 3161 } 3162 3163 /* 3164 * Note: we don't lock all queues on VHOST_USER_GET_VRING_BASE 3165 * and VHOST_USER_RESET_OWNER, since it is sent when virtio stops 3166 * and device is destroyed. destroy_device waits for queues to be 3167 * inactive, so it is safe. Otherwise taking the access_lock 3168 * would cause a dead lock. 3169 */ 3170 if (msg_handler->lock_all_qps) { 3171 if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) { 3172 vhost_user_lock_all_queue_pairs(dev); 3173 unlock_required = 1; 3174 } 3175 } 3176 3177 handled = false; 3178 if (dev->extern_ops.pre_msg_handle) { 3179 RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0); 3180 msg_result = (*dev->extern_ops.pre_msg_handle)(dev->vid, &ctx); 3181 switch (msg_result) { 3182 case RTE_VHOST_MSG_RESULT_REPLY: 3183 send_vhost_reply(dev, fd, &ctx); 3184 /* Fall-through */ 3185 case RTE_VHOST_MSG_RESULT_ERR: 3186 case RTE_VHOST_MSG_RESULT_OK: 3187 handled = true; 3188 goto skip_to_post_handle; 3189 case RTE_VHOST_MSG_RESULT_NOT_HANDLED: 3190 default: 3191 break; 3192 } 3193 } 3194 3195 if (msg_handler == NULL || msg_handler->callback == NULL) 3196 goto skip_to_post_handle; 3197 3198 if (!msg_handler->accepts_fd && validate_msg_fds(dev, &ctx, 0) != 0) { 3199 msg_result = RTE_VHOST_MSG_RESULT_ERR; 3200 } else { 3201 msg_result = msg_handler->callback(&dev, &ctx, fd); 3202 } 3203 3204 switch (msg_result) { 3205 case RTE_VHOST_MSG_RESULT_ERR: 3206 VHOST_CONFIG_LOG(dev->ifname, ERR, 3207 "processing %s failed.", 3208 msg_handler->description); 3209 handled = true; 3210 break; 3211 case RTE_VHOST_MSG_RESULT_OK: 3212 VHOST_CONFIG_LOG(dev->ifname, DEBUG, 3213 "processing %s succeeded.", 3214 msg_handler->description); 3215 handled = true; 3216 break; 3217 case RTE_VHOST_MSG_RESULT_REPLY: 3218 VHOST_CONFIG_LOG(dev->ifname, DEBUG, 3219 "processing %s succeeded and needs reply.", 3220 msg_handler->description); 3221 send_vhost_reply(dev, fd, &ctx); 3222 handled = true; 3223 break; 3224 default: 3225 break; 3226 } 3227 3228 skip_to_post_handle: 3229 if (msg_result != RTE_VHOST_MSG_RESULT_ERR && 3230 dev->extern_ops.post_msg_handle) { 3231 RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0); 3232 msg_result = (*dev->extern_ops.post_msg_handle)(dev->vid, &ctx); 3233 switch (msg_result) { 3234 case RTE_VHOST_MSG_RESULT_REPLY: 3235 send_vhost_reply(dev, fd, &ctx); 3236 /* Fall-through */ 3237 case RTE_VHOST_MSG_RESULT_ERR: 3238 case RTE_VHOST_MSG_RESULT_OK: 3239 handled = true; 3240 case RTE_VHOST_MSG_RESULT_NOT_HANDLED: 3241 default: 3242 break; 3243 } 3244 } 3245 3246 /* If message was not handled at this stage, treat it as an error */ 3247 if (!handled) { 3248 VHOST_CONFIG_LOG(dev->ifname, ERR, 3249 "vhost message (req: %d) was not handled.", 3250 request); 3251 close_msg_fds(&ctx); 3252 msg_result = RTE_VHOST_MSG_RESULT_ERR; 3253 } 3254 3255 /* 3256 * If the request required a reply that was already sent, 3257 * this optional reply-ack won't be sent as the 3258 * VHOST_USER_NEED_REPLY was cleared in send_vhost_reply(). 3259 */ 3260 if (ctx.msg.flags & VHOST_USER_NEED_REPLY) { 3261 ctx.msg.payload.u64 = msg_result == RTE_VHOST_MSG_RESULT_ERR; 3262 ctx.msg.size = sizeof(ctx.msg.payload.u64); 3263 ctx.fd_num = 0; 3264 send_vhost_reply(dev, fd, &ctx); 3265 } else if (msg_result == RTE_VHOST_MSG_RESULT_ERR) { 3266 VHOST_CONFIG_LOG(dev->ifname, ERR, "vhost message handling failed."); 3267 ret = -1; 3268 goto unlock; 3269 } 3270 3271 for (i = 0; i < dev->nr_vring; i++) { 3272 struct vhost_virtqueue *vq = dev->virtqueue[i]; 3273 bool cur_ready = vq_is_ready(dev, vq); 3274 3275 if (cur_ready != (vq && vq->ready)) { 3276 vq->ready = cur_ready; 3277 vhost_user_notify_queue_state(dev, vq, cur_ready); 3278 } 3279 } 3280 3281 unlock: 3282 if (unlock_required) 3283 vhost_user_unlock_all_queue_pairs(dev); 3284 3285 if (ret != 0 || !virtio_is_ready(dev)) 3286 goto out; 3287 3288 /* 3289 * Virtio is now ready. If not done already, it is time 3290 * to notify the application it can process the rings and 3291 * configure the vDPA device if present. 3292 */ 3293 3294 if (!(dev->flags & VIRTIO_DEV_RUNNING)) { 3295 if (dev->notify_ops->new_device(dev->vid) == 0) 3296 dev->flags |= VIRTIO_DEV_RUNNING; 3297 } 3298 3299 vdpa_dev = dev->vdpa_dev; 3300 if (!vdpa_dev) 3301 goto out; 3302 3303 if (vdpa_dev->type == RTE_VHOST_VDPA_DEVICE_TYPE_BLK) { 3304 if (request == VHOST_USER_SET_VRING_CALL) { 3305 blk_call_fd = ctx.msg.payload.u64 & VHOST_USER_VRING_IDX_MASK; 3306 if (blk_call_fd != dev->nr_vring - 1) 3307 goto out; 3308 } else { 3309 goto out; 3310 } 3311 } 3312 3313 if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) { 3314 if (vdpa_dev->ops->dev_conf(dev->vid)) 3315 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to configure vDPA device"); 3316 else 3317 dev->flags |= VIRTIO_DEV_VDPA_CONFIGURED; 3318 } 3319 3320 out: 3321 return ret; 3322 } 3323 3324 static int 3325 vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm) 3326 { 3327 int ret; 3328 struct vhu_msg_context ctx = { 3329 .msg = { 3330 .request.backend = VHOST_USER_BACKEND_IOTLB_MSG, 3331 .flags = VHOST_USER_VERSION, 3332 .size = sizeof(ctx.msg.payload.iotlb), 3333 .payload.iotlb = { 3334 .iova = iova, 3335 .perm = perm, 3336 .type = VHOST_IOTLB_MISS, 3337 }, 3338 }, 3339 }; 3340 3341 ret = send_vhost_message(dev, dev->backend_req_fd, &ctx); 3342 if (ret < 0) { 3343 VHOST_CONFIG_LOG(dev->ifname, ERR, 3344 "failed to send IOTLB miss message (%d)", 3345 ret); 3346 return ret; 3347 } 3348 3349 return 0; 3350 } 3351 3352 int 3353 rte_vhost_backend_config_change(int vid, bool need_reply) 3354 { 3355 struct vhu_msg_context ctx = { 3356 .msg = { 3357 .request.backend = VHOST_USER_BACKEND_CONFIG_CHANGE_MSG, 3358 .flags = VHOST_USER_VERSION, 3359 .size = 0, 3360 } 3361 }; 3362 struct virtio_net *dev; 3363 int ret; 3364 3365 dev = get_device(vid); 3366 if (!dev) 3367 return -ENODEV; 3368 3369 if (!need_reply) { 3370 ret = send_vhost_backend_message(dev, &ctx); 3371 } else { 3372 ctx.msg.flags |= VHOST_USER_NEED_REPLY; 3373 ret = send_vhost_backend_message_process_reply(dev, &ctx); 3374 } 3375 3376 if (ret < 0) 3377 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to send config change (%d)", ret); 3378 return ret; 3379 } 3380 3381 static int vhost_user_backend_set_vring_host_notifier(struct virtio_net *dev, 3382 int index, int fd, 3383 uint64_t offset, 3384 uint64_t size) 3385 { 3386 int ret; 3387 struct vhu_msg_context ctx = { 3388 .msg = { 3389 .request.backend = VHOST_USER_BACKEND_VRING_HOST_NOTIFIER_MSG, 3390 .flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY, 3391 .size = sizeof(ctx.msg.payload.area), 3392 .payload.area = { 3393 .u64 = index & VHOST_USER_VRING_IDX_MASK, 3394 .size = size, 3395 .offset = offset, 3396 }, 3397 }, 3398 }; 3399 3400 if (fd < 0) 3401 ctx.msg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK; 3402 else { 3403 ctx.fds[0] = fd; 3404 ctx.fd_num = 1; 3405 } 3406 3407 ret = send_vhost_backend_message_process_reply(dev, &ctx); 3408 if (ret < 0) 3409 VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to set host notifier (%d)", ret); 3410 3411 return ret; 3412 } 3413 3414 int rte_vhost_host_notifier_ctrl(int vid, uint16_t qid, bool enable) 3415 { 3416 struct virtio_net *dev; 3417 struct rte_vdpa_device *vdpa_dev; 3418 int vfio_device_fd, ret = 0; 3419 uint64_t offset, size; 3420 unsigned int i, q_start, q_last; 3421 3422 dev = get_device(vid); 3423 if (!dev) 3424 return -ENODEV; 3425 3426 vdpa_dev = dev->vdpa_dev; 3427 if (vdpa_dev == NULL) 3428 return -ENODEV; 3429 3430 if (!(dev->features & (1ULL << VIRTIO_F_VERSION_1)) || 3431 !(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) || 3432 !(dev->protocol_features & 3433 (1ULL << VHOST_USER_PROTOCOL_F_BACKEND_REQ)) || 3434 !(dev->protocol_features & 3435 (1ULL << VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD)) || 3436 !(dev->protocol_features & 3437 (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER))) 3438 return -ENOTSUP; 3439 3440 if (qid == RTE_VHOST_QUEUE_ALL) { 3441 q_start = 0; 3442 q_last = dev->nr_vring - 1; 3443 } else { 3444 if (qid >= dev->nr_vring) 3445 return -EINVAL; 3446 q_start = qid; 3447 q_last = qid; 3448 } 3449 3450 if (vdpa_dev->ops->get_vfio_device_fd == NULL) 3451 return -ENOTSUP; 3452 if (vdpa_dev->ops->get_notify_area == NULL) 3453 return -ENOTSUP; 3454 3455 vfio_device_fd = vdpa_dev->ops->get_vfio_device_fd(vid); 3456 if (vfio_device_fd < 0) 3457 return -ENOTSUP; 3458 3459 if (enable) { 3460 for (i = q_start; i <= q_last; i++) { 3461 if (vdpa_dev->ops->get_notify_area(vid, i, &offset, 3462 &size) < 0) { 3463 ret = -ENOTSUP; 3464 goto disable; 3465 } 3466 3467 if (vhost_user_backend_set_vring_host_notifier(dev, i, 3468 vfio_device_fd, offset, size) < 0) { 3469 ret = -EFAULT; 3470 goto disable; 3471 } 3472 } 3473 } else { 3474 disable: 3475 for (i = q_start; i <= q_last; i++) { 3476 vhost_user_backend_set_vring_host_notifier(dev, i, -1, 3477 0, 0); 3478 } 3479 } 3480 3481 return ret; 3482 } 3483 3484 static int 3485 vhost_user_inject_irq(struct virtio_net *dev __rte_unused, struct vhost_virtqueue *vq) 3486 { 3487 if (vq->callfd < 0) 3488 return -1; 3489 3490 return eventfd_write(vq->callfd, (eventfd_t)1); 3491 } 3492 3493 static struct vhost_backend_ops vhost_user_backend_ops = { 3494 .iotlb_miss = vhost_user_iotlb_miss, 3495 .inject_irq = vhost_user_inject_irq, 3496 }; 3497 3498 int 3499 vhost_user_new_device(void) 3500 { 3501 return vhost_new_device(&vhost_user_backend_ops); 3502 } 3503