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