1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (c) Intel Corporation. 3 * All rights reserved. 4 * Copyright (c) 2021 Mellanox Technologies LTD. All rights reserved. 5 */ 6 7 #include "spdk/stdinc.h" 8 9 #include "spdk/env.h" 10 #include "spdk/likely.h" 11 #include "spdk/string.h" 12 #include "spdk/util.h" 13 #include "spdk/memory.h" 14 #include "spdk/barrier.h" 15 #include "spdk/vhost.h" 16 #include "vhost_internal.h" 17 #include <rte_version.h> 18 19 #include "spdk_internal/vhost_user.h" 20 21 /* Path to folder where character device will be created. Can be set by user. */ 22 static char g_vhost_user_dev_dirname[PATH_MAX] = ""; 23 24 static struct spdk_thread *g_vhost_user_init_thread; 25 26 /** 27 * DPDK calls our callbacks synchronously but the work those callbacks 28 * perform needs to be async. Luckily, all DPDK callbacks are called on 29 * a DPDK-internal pthread, so we'll just wait on a semaphore in there. 30 */ 31 static sem_t g_dpdk_sem; 32 33 /** Return code for the current DPDK callback */ 34 static int g_dpdk_response; 35 36 struct vhost_session_fn_ctx { 37 /** Device pointer obtained before enqueueing the event */ 38 struct spdk_vhost_dev *vdev; 39 40 /** ID of the session to send event to. */ 41 uint32_t vsession_id; 42 43 /** User provided function to be executed on session's thread. */ 44 spdk_vhost_session_fn cb_fn; 45 46 /** 47 * User provided function to be called on the init thread 48 * after iterating through all sessions. 49 */ 50 spdk_vhost_dev_fn cpl_fn; 51 52 /** Custom user context */ 53 void *user_ctx; 54 }; 55 56 static struct spdk_vhost_user_dev * 57 to_user_dev(struct spdk_vhost_dev *vdev) 58 { 59 assert(vdev != NULL); 60 return vdev->ctxt; 61 } 62 63 static void __attribute__((constructor)) 64 _vhost_user_sem_init(void) 65 { 66 if (sem_init(&g_dpdk_sem, 0, 0) != 0) { 67 SPDK_ERRLOG("Failed to initialize semaphore for rte_vhost pthread.\n"); 68 abort(); 69 } 70 } 71 72 static void __attribute__((destructor)) 73 _vhost_user_sem_destroy(void) 74 { 75 sem_destroy(&g_dpdk_sem); 76 } 77 78 void *vhost_gpa_to_vva(struct spdk_vhost_session *vsession, uint64_t addr, uint64_t len) 79 { 80 void *vva; 81 uint64_t newlen; 82 83 newlen = len; 84 vva = (void *)rte_vhost_va_from_guest_pa(vsession->mem, addr, &newlen); 85 if (newlen != len) { 86 return NULL; 87 } 88 89 return vva; 90 91 } 92 93 static void 94 vhost_log_req_desc(struct spdk_vhost_session *vsession, struct spdk_vhost_virtqueue *virtqueue, 95 uint16_t req_id) 96 { 97 struct vring_desc *desc, *desc_table; 98 uint32_t desc_table_size; 99 int rc; 100 101 if (spdk_likely(!vhost_dev_has_feature(vsession, VHOST_F_LOG_ALL))) { 102 return; 103 } 104 105 rc = vhost_vq_get_desc(vsession, virtqueue, req_id, &desc, &desc_table, &desc_table_size); 106 if (spdk_unlikely(rc != 0)) { 107 SPDK_ERRLOG("Can't log used ring descriptors!\n"); 108 return; 109 } 110 111 do { 112 if (vhost_vring_desc_is_wr(desc)) { 113 /* To be honest, only pages realy touched should be logged, but 114 * doing so would require tracking those changes in each backed. 115 * Also backend most likely will touch all/most of those pages so 116 * for lets assume we touched all pages passed to as writeable buffers. */ 117 rte_vhost_log_write(vsession->vid, desc->addr, desc->len); 118 } 119 vhost_vring_desc_get_next(&desc, desc_table, desc_table_size); 120 } while (desc); 121 } 122 123 static void 124 vhost_log_used_vring_elem(struct spdk_vhost_session *vsession, 125 struct spdk_vhost_virtqueue *virtqueue, 126 uint16_t idx) 127 { 128 uint64_t offset, len; 129 130 if (spdk_likely(!vhost_dev_has_feature(vsession, VHOST_F_LOG_ALL))) { 131 return; 132 } 133 134 if (spdk_unlikely(virtqueue->packed.packed_ring)) { 135 offset = idx * sizeof(struct vring_packed_desc); 136 len = sizeof(struct vring_packed_desc); 137 } else { 138 offset = offsetof(struct vring_used, ring[idx]); 139 len = sizeof(virtqueue->vring.used->ring[idx]); 140 } 141 142 rte_vhost_log_used_vring(vsession->vid, virtqueue->vring_idx, offset, len); 143 } 144 145 static void 146 vhost_log_used_vring_idx(struct spdk_vhost_session *vsession, 147 struct spdk_vhost_virtqueue *virtqueue) 148 { 149 uint64_t offset, len; 150 uint16_t vq_idx; 151 152 if (spdk_likely(!vhost_dev_has_feature(vsession, VHOST_F_LOG_ALL))) { 153 return; 154 } 155 156 offset = offsetof(struct vring_used, idx); 157 len = sizeof(virtqueue->vring.used->idx); 158 vq_idx = virtqueue - vsession->virtqueue; 159 160 rte_vhost_log_used_vring(vsession->vid, vq_idx, offset, len); 161 } 162 163 /* 164 * Get available requests from avail ring. 165 */ 166 uint16_t 167 vhost_vq_avail_ring_get(struct spdk_vhost_virtqueue *virtqueue, uint16_t *reqs, 168 uint16_t reqs_len) 169 { 170 struct rte_vhost_vring *vring = &virtqueue->vring; 171 struct vring_avail *avail = vring->avail; 172 uint16_t size_mask = vring->size - 1; 173 uint16_t last_idx = virtqueue->last_avail_idx, avail_idx = avail->idx; 174 uint16_t count, i; 175 int rc; 176 uint64_t u64_value; 177 178 spdk_smp_rmb(); 179 180 if (virtqueue->vsession && spdk_unlikely(virtqueue->vsession->interrupt_mode)) { 181 /* Read to clear vring's kickfd */ 182 rc = read(vring->kickfd, &u64_value, sizeof(u64_value)); 183 if (rc < 0) { 184 SPDK_ERRLOG("failed to acknowledge kickfd: %s.\n", spdk_strerror(errno)); 185 return -errno; 186 } 187 } 188 189 count = avail_idx - last_idx; 190 if (spdk_likely(count == 0)) { 191 return 0; 192 } 193 194 if (spdk_unlikely(count > vring->size)) { 195 /* TODO: the queue is unrecoverably broken and should be marked so. 196 * For now we will fail silently and report there are no new avail entries. 197 */ 198 return 0; 199 } 200 201 count = spdk_min(count, reqs_len); 202 203 virtqueue->last_avail_idx += count; 204 /* Check whether there are unprocessed reqs in vq, then kick vq manually */ 205 if (virtqueue->vsession && spdk_unlikely(virtqueue->vsession->interrupt_mode)) { 206 /* If avail_idx is larger than virtqueue's last_avail_idx, then there is unprocessed reqs. 207 * avail_idx should get updated here from memory, in case of race condition with guest. 208 */ 209 avail_idx = * (volatile uint16_t *) &avail->idx; 210 if (avail_idx > virtqueue->last_avail_idx) { 211 /* Write to notify vring's kickfd */ 212 rc = write(vring->kickfd, &u64_value, sizeof(u64_value)); 213 if (rc < 0) { 214 SPDK_ERRLOG("failed to kick vring: %s.\n", spdk_strerror(errno)); 215 return -errno; 216 } 217 } 218 } 219 220 for (i = 0; i < count; i++) { 221 reqs[i] = vring->avail->ring[(last_idx + i) & size_mask]; 222 } 223 224 SPDK_DEBUGLOG(vhost_ring, 225 "AVAIL: last_idx=%"PRIu16" avail_idx=%"PRIu16" count=%"PRIu16"\n", 226 last_idx, avail_idx, count); 227 228 return count; 229 } 230 231 static bool 232 vhost_vring_desc_is_indirect(struct vring_desc *cur_desc) 233 { 234 return !!(cur_desc->flags & VRING_DESC_F_INDIRECT); 235 } 236 237 static bool 238 vhost_vring_packed_desc_is_indirect(struct vring_packed_desc *cur_desc) 239 { 240 return (cur_desc->flags & VRING_DESC_F_INDIRECT) != 0; 241 } 242 243 static bool 244 vhost_inflight_packed_desc_is_indirect(spdk_vhost_inflight_desc *cur_desc) 245 { 246 return (cur_desc->flags & VRING_DESC_F_INDIRECT) != 0; 247 } 248 249 int 250 vhost_vq_get_desc(struct spdk_vhost_session *vsession, struct spdk_vhost_virtqueue *virtqueue, 251 uint16_t req_idx, struct vring_desc **desc, struct vring_desc **desc_table, 252 uint32_t *desc_table_size) 253 { 254 if (spdk_unlikely(req_idx >= virtqueue->vring.size)) { 255 return -1; 256 } 257 258 *desc = &virtqueue->vring.desc[req_idx]; 259 260 if (vhost_vring_desc_is_indirect(*desc)) { 261 *desc_table_size = (*desc)->len / sizeof(**desc); 262 *desc_table = vhost_gpa_to_vva(vsession, (*desc)->addr, 263 sizeof(**desc) * *desc_table_size); 264 *desc = *desc_table; 265 if (*desc == NULL) { 266 return -1; 267 } 268 269 return 0; 270 } 271 272 *desc_table = virtqueue->vring.desc; 273 *desc_table_size = virtqueue->vring.size; 274 275 return 0; 276 } 277 278 static bool 279 vhost_packed_desc_indirect_to_desc_table(struct spdk_vhost_session *vsession, 280 uint64_t addr, uint32_t len, 281 struct vring_packed_desc **desc_table, 282 uint32_t *desc_table_size) 283 { 284 *desc_table_size = len / sizeof(struct vring_packed_desc); 285 286 *desc_table = vhost_gpa_to_vva(vsession, addr, len); 287 if (spdk_unlikely(*desc_table == NULL)) { 288 return false; 289 } 290 291 return true; 292 } 293 294 int 295 vhost_vq_get_desc_packed(struct spdk_vhost_session *vsession, 296 struct spdk_vhost_virtqueue *virtqueue, 297 uint16_t req_idx, struct vring_packed_desc **desc, 298 struct vring_packed_desc **desc_table, uint32_t *desc_table_size) 299 { 300 *desc = &virtqueue->vring.desc_packed[req_idx]; 301 302 /* In packed ring when the desc is non-indirect we get next desc 303 * by judging (desc->flag & VRING_DESC_F_NEXT) != 0. When the desc 304 * is indirect we get next desc by idx and desc_table_size. It's 305 * different from split ring. 306 */ 307 if (vhost_vring_packed_desc_is_indirect(*desc)) { 308 if (!vhost_packed_desc_indirect_to_desc_table(vsession, (*desc)->addr, (*desc)->len, 309 desc_table, desc_table_size)) { 310 return -1; 311 } 312 313 *desc = *desc_table; 314 } else { 315 *desc_table = NULL; 316 *desc_table_size = 0; 317 } 318 319 return 0; 320 } 321 322 int 323 vhost_inflight_queue_get_desc(struct spdk_vhost_session *vsession, 324 spdk_vhost_inflight_desc *desc_array, 325 uint16_t req_idx, spdk_vhost_inflight_desc **desc, 326 struct vring_packed_desc **desc_table, uint32_t *desc_table_size) 327 { 328 *desc = &desc_array[req_idx]; 329 330 if (vhost_inflight_packed_desc_is_indirect(*desc)) { 331 if (!vhost_packed_desc_indirect_to_desc_table(vsession, (*desc)->addr, (*desc)->len, 332 desc_table, desc_table_size)) { 333 return -1; 334 } 335 336 /* This desc is the inflight desc not the packed desc. 337 * When set the F_INDIRECT the table entry should be the packed desc 338 * so set the inflight desc NULL. 339 */ 340 *desc = NULL; 341 } else { 342 /* When not set the F_INDIRECT means there is no packed desc table */ 343 *desc_table = NULL; 344 *desc_table_size = 0; 345 } 346 347 return 0; 348 } 349 350 int 351 vhost_vq_used_signal(struct spdk_vhost_session *vsession, 352 struct spdk_vhost_virtqueue *virtqueue) 353 { 354 if (virtqueue->used_req_cnt == 0) { 355 return 0; 356 } 357 358 virtqueue->req_cnt += virtqueue->used_req_cnt; 359 virtqueue->used_req_cnt = 0; 360 361 SPDK_DEBUGLOG(vhost_ring, 362 "Queue %td - USED RING: sending IRQ: last used %"PRIu16"\n", 363 virtqueue - vsession->virtqueue, virtqueue->last_used_idx); 364 365 if (rte_vhost_vring_call(vsession->vid, virtqueue->vring_idx) == 0) { 366 /* interrupt signalled */ 367 return 1; 368 } else { 369 /* interrupt not signalled */ 370 return 0; 371 } 372 } 373 374 static void 375 session_vq_io_stats_update(struct spdk_vhost_session *vsession, 376 struct spdk_vhost_virtqueue *virtqueue, uint64_t now) 377 { 378 uint32_t irq_delay_base = vsession->coalescing_delay_time_base; 379 uint32_t io_threshold = vsession->coalescing_io_rate_threshold; 380 int32_t irq_delay; 381 uint32_t req_cnt; 382 383 req_cnt = virtqueue->req_cnt + virtqueue->used_req_cnt; 384 if (req_cnt <= io_threshold) { 385 return; 386 } 387 388 irq_delay = (irq_delay_base * (req_cnt - io_threshold)) / io_threshold; 389 virtqueue->irq_delay_time = (uint32_t) spdk_max(0, irq_delay); 390 391 virtqueue->req_cnt = 0; 392 virtqueue->next_event_time = now; 393 } 394 395 static void 396 check_session_vq_io_stats(struct spdk_vhost_session *vsession, 397 struct spdk_vhost_virtqueue *virtqueue, uint64_t now) 398 { 399 if (now < vsession->next_stats_check_time) { 400 return; 401 } 402 403 vsession->next_stats_check_time = now + vsession->stats_check_interval; 404 session_vq_io_stats_update(vsession, virtqueue, now); 405 } 406 407 static inline bool 408 vhost_vq_event_is_suppressed(struct spdk_vhost_virtqueue *vq) 409 { 410 if (spdk_unlikely(vq->packed.packed_ring)) { 411 if (vq->vring.driver_event->flags & VRING_PACKED_EVENT_FLAG_DISABLE) { 412 return true; 413 } 414 } else { 415 if (vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT) { 416 return true; 417 } 418 } 419 420 return false; 421 } 422 423 void 424 vhost_session_vq_used_signal(struct spdk_vhost_virtqueue *virtqueue) 425 { 426 struct spdk_vhost_session *vsession = virtqueue->vsession; 427 uint64_t now; 428 429 if (vsession->coalescing_delay_time_base == 0) { 430 if (virtqueue->vring.desc == NULL) { 431 return; 432 } 433 434 if (vhost_vq_event_is_suppressed(virtqueue)) { 435 return; 436 } 437 438 vhost_vq_used_signal(vsession, virtqueue); 439 } else { 440 now = spdk_get_ticks(); 441 check_session_vq_io_stats(vsession, virtqueue, now); 442 443 /* No need for event right now */ 444 if (now < virtqueue->next_event_time) { 445 return; 446 } 447 448 if (vhost_vq_event_is_suppressed(virtqueue)) { 449 return; 450 } 451 452 if (!vhost_vq_used_signal(vsession, virtqueue)) { 453 return; 454 } 455 456 /* Syscall is quite long so update time */ 457 now = spdk_get_ticks(); 458 virtqueue->next_event_time = now + virtqueue->irq_delay_time; 459 } 460 } 461 462 void 463 vhost_session_used_signal(struct spdk_vhost_session *vsession) 464 { 465 struct spdk_vhost_virtqueue *virtqueue; 466 uint16_t q_idx; 467 468 for (q_idx = 0; q_idx < vsession->max_queues; q_idx++) { 469 virtqueue = &vsession->virtqueue[q_idx]; 470 vhost_session_vq_used_signal(virtqueue); 471 } 472 } 473 474 /* 475 * Enqueue id and len to used ring. 476 */ 477 void 478 vhost_vq_used_ring_enqueue(struct spdk_vhost_session *vsession, 479 struct spdk_vhost_virtqueue *virtqueue, 480 uint16_t id, uint32_t len) 481 { 482 struct rte_vhost_vring *vring = &virtqueue->vring; 483 struct vring_used *used = vring->used; 484 uint16_t last_idx = virtqueue->last_used_idx & (vring->size - 1); 485 uint16_t vq_idx = virtqueue->vring_idx; 486 487 SPDK_DEBUGLOG(vhost_ring, 488 "Queue %td - USED RING: last_idx=%"PRIu16" req id=%"PRIu16" len=%"PRIu32"\n", 489 virtqueue - vsession->virtqueue, virtqueue->last_used_idx, id, len); 490 491 vhost_log_req_desc(vsession, virtqueue, id); 492 493 virtqueue->last_used_idx++; 494 used->ring[last_idx].id = id; 495 used->ring[last_idx].len = len; 496 497 /* Ensure the used ring is updated before we log it or increment used->idx. */ 498 spdk_smp_wmb(); 499 500 rte_vhost_set_last_inflight_io_split(vsession->vid, vq_idx, id); 501 502 vhost_log_used_vring_elem(vsession, virtqueue, last_idx); 503 * (volatile uint16_t *) &used->idx = virtqueue->last_used_idx; 504 vhost_log_used_vring_idx(vsession, virtqueue); 505 506 rte_vhost_clr_inflight_desc_split(vsession->vid, vq_idx, virtqueue->last_used_idx, id); 507 508 virtqueue->used_req_cnt++; 509 510 if (vsession->interrupt_mode) { 511 if (virtqueue->vring.desc == NULL || vhost_vq_event_is_suppressed(virtqueue)) { 512 return; 513 } 514 515 vhost_vq_used_signal(vsession, virtqueue); 516 } 517 } 518 519 void 520 vhost_vq_packed_ring_enqueue(struct spdk_vhost_session *vsession, 521 struct spdk_vhost_virtqueue *virtqueue, 522 uint16_t num_descs, uint16_t buffer_id, 523 uint32_t length, uint16_t inflight_head) 524 { 525 struct vring_packed_desc *desc = &virtqueue->vring.desc_packed[virtqueue->last_used_idx]; 526 bool used, avail; 527 528 SPDK_DEBUGLOG(vhost_ring, 529 "Queue %td - RING: buffer_id=%"PRIu16"\n", 530 virtqueue - vsession->virtqueue, buffer_id); 531 532 /* When the descriptor is used, two flags in descriptor 533 * avail flag and used flag are set to equal 534 * and used flag value == used_wrap_counter. 535 */ 536 used = !!(desc->flags & VRING_DESC_F_USED); 537 avail = !!(desc->flags & VRING_DESC_F_AVAIL); 538 if (spdk_unlikely(used == virtqueue->packed.used_phase && used == avail)) { 539 SPDK_ERRLOG("descriptor has been used before\n"); 540 return; 541 } 542 543 /* In used desc addr is unused and len specifies the buffer length 544 * that has been written to by the device. 545 */ 546 desc->addr = 0; 547 desc->len = length; 548 549 /* This bit specifies whether any data has been written by the device */ 550 if (length != 0) { 551 desc->flags |= VRING_DESC_F_WRITE; 552 } 553 554 /* Buffer ID is included in the last descriptor in the list. 555 * The driver needs to keep track of the size of the list corresponding 556 * to each buffer ID. 557 */ 558 desc->id = buffer_id; 559 560 /* A device MUST NOT make the descriptor used before buffer_id is 561 * written to the descriptor. 562 */ 563 spdk_smp_wmb(); 564 565 rte_vhost_set_last_inflight_io_packed(vsession->vid, virtqueue->vring_idx, inflight_head); 566 /* To mark a desc as used, the device sets the F_USED bit in flags to match 567 * the internal Device ring wrap counter. It also sets the F_AVAIL bit to 568 * match the same value. 569 */ 570 if (virtqueue->packed.used_phase) { 571 desc->flags |= VRING_DESC_F_AVAIL_USED; 572 } else { 573 desc->flags &= ~VRING_DESC_F_AVAIL_USED; 574 } 575 rte_vhost_clr_inflight_desc_packed(vsession->vid, virtqueue->vring_idx, inflight_head); 576 577 vhost_log_used_vring_elem(vsession, virtqueue, virtqueue->last_used_idx); 578 virtqueue->last_used_idx += num_descs; 579 if (virtqueue->last_used_idx >= virtqueue->vring.size) { 580 virtqueue->last_used_idx -= virtqueue->vring.size; 581 virtqueue->packed.used_phase = !virtqueue->packed.used_phase; 582 } 583 584 virtqueue->used_req_cnt++; 585 } 586 587 bool 588 vhost_vq_packed_ring_is_avail(struct spdk_vhost_virtqueue *virtqueue) 589 { 590 uint16_t flags = virtqueue->vring.desc_packed[virtqueue->last_avail_idx].flags; 591 592 /* To mark a desc as available, the driver sets the F_AVAIL bit in flags 593 * to match the internal avail wrap counter. It also sets the F_USED bit to 594 * match the inverse value but it's not mandatory. 595 */ 596 return (!!(flags & VRING_DESC_F_AVAIL) == virtqueue->packed.avail_phase); 597 } 598 599 bool 600 vhost_vring_packed_desc_is_wr(struct vring_packed_desc *cur_desc) 601 { 602 return (cur_desc->flags & VRING_DESC_F_WRITE) != 0; 603 } 604 605 bool 606 vhost_vring_inflight_desc_is_wr(spdk_vhost_inflight_desc *cur_desc) 607 { 608 return (cur_desc->flags & VRING_DESC_F_WRITE) != 0; 609 } 610 611 int 612 vhost_vring_packed_desc_get_next(struct vring_packed_desc **desc, uint16_t *req_idx, 613 struct spdk_vhost_virtqueue *vq, 614 struct vring_packed_desc *desc_table, 615 uint32_t desc_table_size) 616 { 617 if (desc_table != NULL) { 618 /* When the desc_table isn't NULL means it's indirect and we get the next 619 * desc by req_idx and desc_table_size. The return value is NULL means 620 * we reach the last desc of this request. 621 */ 622 (*req_idx)++; 623 if (*req_idx < desc_table_size) { 624 *desc = &desc_table[*req_idx]; 625 } else { 626 *desc = NULL; 627 } 628 } else { 629 /* When the desc_table is NULL means it's non-indirect and we get the next 630 * desc by req_idx and F_NEXT in flags. The return value is NULL means 631 * we reach the last desc of this request. When return new desc 632 * we update the req_idx too. 633 */ 634 if (((*desc)->flags & VRING_DESC_F_NEXT) == 0) { 635 *desc = NULL; 636 return 0; 637 } 638 639 *req_idx = (*req_idx + 1) % vq->vring.size; 640 *desc = &vq->vring.desc_packed[*req_idx]; 641 } 642 643 return 0; 644 } 645 646 static int 647 vhost_vring_desc_payload_to_iov(struct spdk_vhost_session *vsession, struct iovec *iov, 648 uint16_t *iov_index, uintptr_t payload, uint64_t remaining) 649 { 650 uintptr_t vva; 651 uint64_t len; 652 653 do { 654 if (*iov_index >= SPDK_VHOST_IOVS_MAX) { 655 SPDK_ERRLOG("SPDK_VHOST_IOVS_MAX(%d) reached\n", SPDK_VHOST_IOVS_MAX); 656 return -1; 657 } 658 len = remaining; 659 vva = (uintptr_t)rte_vhost_va_from_guest_pa(vsession->mem, payload, &len); 660 if (vva == 0 || len == 0) { 661 SPDK_ERRLOG("gpa_to_vva(%p) == NULL\n", (void *)payload); 662 return -1; 663 } 664 iov[*iov_index].iov_base = (void *)vva; 665 iov[*iov_index].iov_len = len; 666 remaining -= len; 667 payload += len; 668 (*iov_index)++; 669 } while (remaining); 670 671 return 0; 672 } 673 674 int 675 vhost_vring_packed_desc_to_iov(struct spdk_vhost_session *vsession, struct iovec *iov, 676 uint16_t *iov_index, const struct vring_packed_desc *desc) 677 { 678 return vhost_vring_desc_payload_to_iov(vsession, iov, iov_index, 679 desc->addr, desc->len); 680 } 681 682 int 683 vhost_vring_inflight_desc_to_iov(struct spdk_vhost_session *vsession, struct iovec *iov, 684 uint16_t *iov_index, const spdk_vhost_inflight_desc *desc) 685 { 686 return vhost_vring_desc_payload_to_iov(vsession, iov, iov_index, 687 desc->addr, desc->len); 688 } 689 690 /* 1, Traverse the desc chain to get the buffer_id and return buffer_id as task_idx. 691 * 2, Update the vq->last_avail_idx to point next available desc chain. 692 * 3, Update the avail_wrap_counter if last_avail_idx overturn. 693 */ 694 uint16_t 695 vhost_vring_packed_desc_get_buffer_id(struct spdk_vhost_virtqueue *vq, uint16_t req_idx, 696 uint16_t *num_descs) 697 { 698 struct vring_packed_desc *desc; 699 uint16_t desc_head = req_idx; 700 701 *num_descs = 1; 702 703 desc = &vq->vring.desc_packed[req_idx]; 704 if (!vhost_vring_packed_desc_is_indirect(desc)) { 705 while ((desc->flags & VRING_DESC_F_NEXT) != 0) { 706 req_idx = (req_idx + 1) % vq->vring.size; 707 desc = &vq->vring.desc_packed[req_idx]; 708 (*num_descs)++; 709 } 710 } 711 712 /* Queue Size doesn't have to be a power of 2 713 * Device maintains last_avail_idx so we can make sure 714 * the value is valid(0 ~ vring.size - 1) 715 */ 716 vq->last_avail_idx = (req_idx + 1) % vq->vring.size; 717 if (vq->last_avail_idx < desc_head) { 718 vq->packed.avail_phase = !vq->packed.avail_phase; 719 } 720 721 return desc->id; 722 } 723 724 int 725 vhost_vring_desc_get_next(struct vring_desc **desc, 726 struct vring_desc *desc_table, uint32_t desc_table_size) 727 { 728 struct vring_desc *old_desc = *desc; 729 uint16_t next_idx; 730 731 if ((old_desc->flags & VRING_DESC_F_NEXT) == 0) { 732 *desc = NULL; 733 return 0; 734 } 735 736 next_idx = old_desc->next; 737 if (spdk_unlikely(next_idx >= desc_table_size)) { 738 *desc = NULL; 739 return -1; 740 } 741 742 *desc = &desc_table[next_idx]; 743 return 0; 744 } 745 746 int 747 vhost_vring_desc_to_iov(struct spdk_vhost_session *vsession, struct iovec *iov, 748 uint16_t *iov_index, const struct vring_desc *desc) 749 { 750 return vhost_vring_desc_payload_to_iov(vsession, iov, iov_index, 751 desc->addr, desc->len); 752 } 753 754 static inline void 755 vhost_session_mem_region_calc(uint64_t *previous_start, uint64_t *start, uint64_t *end, 756 uint64_t *len, struct rte_vhost_mem_region *region) 757 { 758 *start = FLOOR_2MB(region->mmap_addr); 759 *end = CEIL_2MB(region->mmap_addr + region->mmap_size); 760 if (*start == *previous_start) { 761 *start += (size_t) VALUE_2MB; 762 } 763 *previous_start = *start; 764 *len = *end - *start; 765 } 766 767 void 768 vhost_session_mem_register(struct rte_vhost_memory *mem) 769 { 770 uint64_t start, end, len; 771 uint32_t i; 772 uint64_t previous_start = UINT64_MAX; 773 774 775 for (i = 0; i < mem->nregions; i++) { 776 vhost_session_mem_region_calc(&previous_start, &start, &end, &len, &mem->regions[i]); 777 SPDK_INFOLOG(vhost, "Registering VM memory for vtophys translation - 0x%jx len:0x%jx\n", 778 start, len); 779 780 if (spdk_mem_register((void *)start, len) != 0) { 781 SPDK_WARNLOG("Failed to register memory region %"PRIu32". Future vtophys translation might fail.\n", 782 i); 783 continue; 784 } 785 } 786 } 787 788 void 789 vhost_session_mem_unregister(struct rte_vhost_memory *mem) 790 { 791 uint64_t start, end, len; 792 uint32_t i; 793 uint64_t previous_start = UINT64_MAX; 794 795 for (i = 0; i < mem->nregions; i++) { 796 vhost_session_mem_region_calc(&previous_start, &start, &end, &len, &mem->regions[i]); 797 if (spdk_vtophys((void *) start, NULL) == SPDK_VTOPHYS_ERROR) { 798 continue; /* region has not been registered */ 799 } 800 801 if (spdk_mem_unregister((void *)start, len) != 0) { 802 assert(false); 803 } 804 } 805 } 806 807 static int 808 _stop_session(struct spdk_vhost_session *vsession) 809 { 810 struct spdk_vhost_dev *vdev = vsession->vdev; 811 struct spdk_vhost_user_dev *user_vdev = to_user_dev(vdev); 812 struct spdk_vhost_virtqueue *q; 813 int rc; 814 uint16_t i; 815 816 rc = user_vdev->user_backend->stop_session(vsession); 817 if (rc != 0) { 818 SPDK_ERRLOG("Couldn't stop device with vid %d.\n", vsession->vid); 819 return rc; 820 } 821 822 for (i = 0; i < vsession->max_queues; i++) { 823 q = &vsession->virtqueue[i]; 824 825 /* vring.desc and vring.desc_packed are in a union struct 826 * so q->vring.desc can replace q->vring.desc_packed. 827 */ 828 if (q->vring.desc == NULL) { 829 continue; 830 } 831 832 /* Packed virtqueues support up to 2^15 entries each 833 * so left one bit can be used as wrap counter. 834 */ 835 if (q->packed.packed_ring) { 836 q->last_avail_idx = q->last_avail_idx | 837 ((uint16_t)q->packed.avail_phase << 15); 838 q->last_used_idx = q->last_used_idx | 839 ((uint16_t)q->packed.used_phase << 15); 840 } 841 842 rte_vhost_set_vring_base(vsession->vid, i, q->last_avail_idx, q->last_used_idx); 843 } 844 845 vhost_session_mem_unregister(vsession->mem); 846 free(vsession->mem); 847 848 return 0; 849 } 850 851 static int 852 new_connection(int vid) 853 { 854 struct spdk_vhost_dev *vdev; 855 struct spdk_vhost_user_dev *user_dev; 856 struct spdk_vhost_session *vsession; 857 size_t dev_dirname_len; 858 char ifname[PATH_MAX]; 859 char *ctrlr_name; 860 861 if (rte_vhost_get_ifname(vid, ifname, PATH_MAX) < 0) { 862 SPDK_ERRLOG("Couldn't get a valid ifname for device with vid %d\n", vid); 863 return -1; 864 } 865 866 spdk_vhost_lock(); 867 868 ctrlr_name = &ifname[0]; 869 dev_dirname_len = strlen(g_vhost_user_dev_dirname); 870 if (strncmp(ctrlr_name, g_vhost_user_dev_dirname, dev_dirname_len) == 0) { 871 ctrlr_name += dev_dirname_len; 872 } 873 874 vdev = spdk_vhost_dev_find(ctrlr_name); 875 if (vdev == NULL) { 876 SPDK_ERRLOG("Couldn't find device with vid %d to create connection for.\n", vid); 877 spdk_vhost_unlock(); 878 return -1; 879 } 880 user_dev = to_user_dev(vdev); 881 882 /* We expect sessions inside user_dev->vsessions to be sorted in ascending 883 * order in regard of vsession->id. For now we always set id = vsessions_cnt++ 884 * and append each session to the very end of the vsessions list. 885 * This is required for vhost_user_dev_foreach_session() to work. 886 */ 887 if (user_dev->vsessions_num == UINT_MAX) { 888 assert(false); 889 return -EINVAL; 890 } 891 892 if (posix_memalign((void **)&vsession, SPDK_CACHE_LINE_SIZE, sizeof(*vsession) + 893 user_dev->user_backend->session_ctx_size)) { 894 SPDK_ERRLOG("vsession alloc failed\n"); 895 spdk_vhost_unlock(); 896 return -1; 897 } 898 memset(vsession, 0, sizeof(*vsession) + user_dev->user_backend->session_ctx_size); 899 900 vsession->vdev = vdev; 901 vsession->vid = vid; 902 vsession->id = user_dev->vsessions_num++; 903 vsession->name = spdk_sprintf_alloc("%ss%u", vdev->name, vsession->vid); 904 if (vsession->name == NULL) { 905 SPDK_ERRLOG("vsession alloc failed\n"); 906 spdk_vhost_unlock(); 907 free(vsession); 908 return -1; 909 } 910 vsession->started = false; 911 vsession->initialized = false; 912 vsession->next_stats_check_time = 0; 913 vsession->stats_check_interval = SPDK_VHOST_STATS_CHECK_INTERVAL_MS * 914 spdk_get_ticks_hz() / 1000UL; 915 TAILQ_INSERT_TAIL(&user_dev->vsessions, vsession, tailq); 916 917 vhost_session_install_rte_compat_hooks(vsession); 918 spdk_vhost_unlock(); 919 return 0; 920 } 921 922 static int 923 vhost_user_session_start(struct spdk_vhost_dev *vdev, struct spdk_vhost_session *vsession) 924 { 925 const struct spdk_vhost_user_dev_backend *backend; 926 927 backend = to_user_dev(vdev)->user_backend; 928 929 return vhost_user_session_send_event(vsession, backend->start_session, 3, "start session"); 930 } 931 932 static int 933 start_device(int vid) 934 { 935 struct spdk_vhost_dev *vdev; 936 struct spdk_vhost_session *vsession; 937 int rc = -1; 938 uint16_t i; 939 bool packed_ring; 940 941 spdk_vhost_lock(); 942 943 vsession = vhost_session_find_by_vid(vid); 944 if (vsession == NULL) { 945 SPDK_ERRLOG("Couldn't find session with vid %d.\n", vid); 946 goto out; 947 } 948 949 vdev = vsession->vdev; 950 if (vsession->started) { 951 /* already started, nothing to do */ 952 rc = 0; 953 goto out; 954 } 955 956 if (vhost_get_negotiated_features(vid, &vsession->negotiated_features) != 0) { 957 SPDK_ERRLOG("vhost device %d: Failed to get negotiated driver features\n", vid); 958 goto out; 959 } 960 961 packed_ring = ((vsession->negotiated_features & (1ULL << VIRTIO_F_RING_PACKED)) != 0); 962 963 vsession->max_queues = 0; 964 memset(vsession->virtqueue, 0, sizeof(vsession->virtqueue)); 965 for (i = 0; i < SPDK_VHOST_MAX_VQUEUES; i++) { 966 struct spdk_vhost_virtqueue *q = &vsession->virtqueue[i]; 967 968 q->vsession = vsession; 969 q->vring_idx = -1; 970 if (rte_vhost_get_vhost_vring(vid, i, &q->vring)) { 971 continue; 972 } 973 q->vring_idx = i; 974 rte_vhost_get_vhost_ring_inflight(vid, i, &q->vring_inflight); 975 976 /* vring.desc and vring.desc_packed are in a union struct 977 * so q->vring.desc can replace q->vring.desc_packed. 978 */ 979 if (q->vring.desc == NULL || q->vring.size == 0) { 980 continue; 981 } 982 983 if (rte_vhost_get_vring_base(vsession->vid, i, &q->last_avail_idx, &q->last_used_idx)) { 984 q->vring.desc = NULL; 985 continue; 986 } 987 988 if (packed_ring) { 989 /* Use the inflight mem to restore the last_avail_idx and last_used_idx. 990 * When the vring format is packed, there is no used_idx in the 991 * used ring, so VM can't resend the used_idx to VHOST when reconnect. 992 * QEMU version 5.2.0 supports the packed inflight before that it only 993 * supports split ring inflight because it doesn't send negotiated features 994 * before get inflight fd. Users can use RPC to enable this function. 995 */ 996 if (spdk_unlikely(vdev->packed_ring_recovery)) { 997 rte_vhost_get_vring_base_from_inflight(vsession->vid, i, 998 &q->last_avail_idx, 999 &q->last_used_idx); 1000 } 1001 1002 /* Packed virtqueues support up to 2^15 entries each 1003 * so left one bit can be used as wrap counter. 1004 */ 1005 q->packed.avail_phase = q->last_avail_idx >> 15; 1006 q->last_avail_idx = q->last_avail_idx & 0x7FFF; 1007 q->packed.used_phase = q->last_used_idx >> 15; 1008 q->last_used_idx = q->last_used_idx & 0x7FFF; 1009 1010 if (!vsession->interrupt_mode) { 1011 /* Disable I/O submission notifications, we'll be polling. */ 1012 q->vring.device_event->flags = VRING_PACKED_EVENT_FLAG_DISABLE; 1013 } 1014 } else { 1015 if (!vsession->interrupt_mode) { 1016 /* Disable I/O submission notifications, we'll be polling. */ 1017 q->vring.used->flags = VRING_USED_F_NO_NOTIFY; 1018 } 1019 } 1020 1021 q->packed.packed_ring = packed_ring; 1022 vsession->max_queues = i + 1; 1023 } 1024 1025 if (vhost_get_mem_table(vid, &vsession->mem) != 0) { 1026 SPDK_ERRLOG("vhost device %d: Failed to get guest memory table\n", vid); 1027 goto out; 1028 } 1029 1030 /* 1031 * Not sure right now but this look like some kind of QEMU bug and guest IO 1032 * might be frozed without kicking all queues after live-migration. This look like 1033 * the previous vhost instance failed to effectively deliver all interrupts before 1034 * the GET_VRING_BASE message. This shouldn't harm guest since spurious interrupts 1035 * should be ignored by guest virtio driver. 1036 * 1037 * Tested on QEMU 2.10.91 and 2.11.50. 1038 */ 1039 for (i = 0; i < vsession->max_queues; i++) { 1040 struct spdk_vhost_virtqueue *q = &vsession->virtqueue[i]; 1041 1042 /* vring.desc and vring.desc_packed are in a union struct 1043 * so q->vring.desc can replace q->vring.desc_packed. 1044 */ 1045 if (q->vring.desc != NULL && q->vring.size > 0) { 1046 rte_vhost_vring_call(vsession->vid, q->vring_idx); 1047 } 1048 } 1049 1050 vhost_user_session_set_coalescing(vdev, vsession, NULL); 1051 vhost_session_mem_register(vsession->mem); 1052 vsession->initialized = true; 1053 rc = vhost_user_session_start(vdev, vsession); 1054 if (rc != 0) { 1055 vhost_session_mem_unregister(vsession->mem); 1056 free(vsession->mem); 1057 goto out; 1058 } 1059 1060 out: 1061 spdk_vhost_unlock(); 1062 return rc; 1063 } 1064 1065 static void 1066 stop_device(int vid) 1067 { 1068 struct spdk_vhost_session *vsession; 1069 1070 spdk_vhost_lock(); 1071 vsession = vhost_session_find_by_vid(vid); 1072 if (vsession == NULL) { 1073 SPDK_ERRLOG("Couldn't find session with vid %d.\n", vid); 1074 spdk_vhost_unlock(); 1075 return; 1076 } 1077 1078 if (!vsession->started) { 1079 /* already stopped, nothing to do */ 1080 spdk_vhost_unlock(); 1081 return; 1082 } 1083 1084 _stop_session(vsession); 1085 spdk_vhost_unlock(); 1086 1087 return; 1088 } 1089 1090 static void 1091 destroy_connection(int vid) 1092 { 1093 struct spdk_vhost_session *vsession; 1094 1095 spdk_vhost_lock(); 1096 vsession = vhost_session_find_by_vid(vid); 1097 if (vsession == NULL) { 1098 SPDK_ERRLOG("Couldn't find session with vid %d.\n", vid); 1099 spdk_vhost_unlock(); 1100 return; 1101 } 1102 1103 if (vsession->started) { 1104 if (_stop_session(vsession) != 0) { 1105 spdk_vhost_unlock(); 1106 return; 1107 } 1108 } 1109 1110 TAILQ_REMOVE(&to_user_dev(vsession->vdev)->vsessions, vsession, tailq); 1111 free(vsession->name); 1112 free(vsession); 1113 spdk_vhost_unlock(); 1114 } 1115 1116 #if RTE_VERSION >= RTE_VERSION_NUM(21, 11, 0, 0) 1117 static const struct rte_vhost_device_ops g_spdk_vhost_ops = { 1118 #else 1119 static const struct vhost_device_ops g_spdk_vhost_ops = { 1120 #endif 1121 .new_device = start_device, 1122 .destroy_device = stop_device, 1123 .new_connection = new_connection, 1124 .destroy_connection = destroy_connection, 1125 }; 1126 1127 static struct spdk_vhost_session * 1128 vhost_session_find_by_id(struct spdk_vhost_dev *vdev, unsigned id) 1129 { 1130 struct spdk_vhost_session *vsession; 1131 1132 TAILQ_FOREACH(vsession, &to_user_dev(vdev)->vsessions, tailq) { 1133 if (vsession->id == id) { 1134 return vsession; 1135 } 1136 } 1137 1138 return NULL; 1139 } 1140 1141 struct spdk_vhost_session * 1142 vhost_session_find_by_vid(int vid) 1143 { 1144 struct spdk_vhost_dev *vdev; 1145 struct spdk_vhost_session *vsession; 1146 1147 for (vdev = spdk_vhost_dev_next(NULL); vdev != NULL; 1148 vdev = spdk_vhost_dev_next(vdev)) { 1149 TAILQ_FOREACH(vsession, &to_user_dev(vdev)->vsessions, tailq) { 1150 if (vsession->vid == vid) { 1151 return vsession; 1152 } 1153 } 1154 } 1155 1156 return NULL; 1157 } 1158 1159 static void 1160 wait_for_semaphore(int timeout_sec, const char *errmsg) 1161 { 1162 struct timespec timeout; 1163 int rc; 1164 1165 clock_gettime(CLOCK_REALTIME, &timeout); 1166 timeout.tv_sec += timeout_sec; 1167 rc = sem_timedwait(&g_dpdk_sem, &timeout); 1168 if (rc != 0) { 1169 SPDK_ERRLOG("Timeout waiting for event: %s.\n", errmsg); 1170 sem_wait(&g_dpdk_sem); 1171 } 1172 } 1173 1174 static void 1175 vhost_session_cb_done(int rc) 1176 { 1177 g_dpdk_response = rc; 1178 sem_post(&g_dpdk_sem); 1179 } 1180 1181 void 1182 vhost_user_session_start_done(struct spdk_vhost_session *vsession, int response) 1183 { 1184 struct spdk_vhost_user_dev *user_dev = to_user_dev(vsession->vdev); 1185 if (response == 0) { 1186 vsession->started = true; 1187 1188 assert(user_dev->active_session_num < UINT32_MAX); 1189 user_dev->active_session_num++; 1190 } 1191 1192 vhost_session_cb_done(response); 1193 } 1194 1195 void 1196 vhost_user_session_stop_done(struct spdk_vhost_session *vsession, int response) 1197 { 1198 struct spdk_vhost_user_dev *user_dev = to_user_dev(vsession->vdev); 1199 1200 if (response == 0) { 1201 vsession->started = false; 1202 1203 assert(user_dev->active_session_num > 0); 1204 user_dev->active_session_num--; 1205 } 1206 1207 vhost_session_cb_done(response); 1208 } 1209 1210 static void 1211 vhost_event_cb(void *arg1) 1212 { 1213 struct vhost_session_fn_ctx *ctx = arg1; 1214 struct spdk_vhost_session *vsession; 1215 1216 if (spdk_vhost_trylock() != 0) { 1217 spdk_thread_send_msg(spdk_get_thread(), vhost_event_cb, arg1); 1218 return; 1219 } 1220 1221 vsession = vhost_session_find_by_id(ctx->vdev, ctx->vsession_id); 1222 ctx->cb_fn(ctx->vdev, vsession, NULL); 1223 spdk_vhost_unlock(); 1224 } 1225 1226 int 1227 vhost_user_session_send_event(struct spdk_vhost_session *vsession, 1228 spdk_vhost_session_fn cb_fn, unsigned timeout_sec, 1229 const char *errmsg) 1230 { 1231 struct vhost_session_fn_ctx ev_ctx = {0}; 1232 struct spdk_vhost_dev *vdev = vsession->vdev; 1233 1234 ev_ctx.vdev = vdev; 1235 ev_ctx.vsession_id = vsession->id; 1236 ev_ctx.cb_fn = cb_fn; 1237 1238 spdk_thread_send_msg(vdev->thread, vhost_event_cb, &ev_ctx); 1239 1240 spdk_vhost_unlock(); 1241 wait_for_semaphore(timeout_sec, errmsg); 1242 spdk_vhost_lock(); 1243 1244 return g_dpdk_response; 1245 } 1246 1247 static void 1248 foreach_session_finish_cb(void *arg1) 1249 { 1250 struct vhost_session_fn_ctx *ev_ctx = arg1; 1251 struct spdk_vhost_dev *vdev = ev_ctx->vdev; 1252 struct spdk_vhost_user_dev *user_dev = to_user_dev(vdev); 1253 1254 if (spdk_vhost_trylock() != 0) { 1255 spdk_thread_send_msg(spdk_get_thread(), 1256 foreach_session_finish_cb, arg1); 1257 return; 1258 } 1259 1260 assert(user_dev->pending_async_op_num > 0); 1261 user_dev->pending_async_op_num--; 1262 if (ev_ctx->cpl_fn != NULL) { 1263 ev_ctx->cpl_fn(vdev, ev_ctx->user_ctx); 1264 } 1265 1266 spdk_vhost_unlock(); 1267 free(ev_ctx); 1268 } 1269 1270 static void 1271 foreach_session(void *arg1) 1272 { 1273 struct vhost_session_fn_ctx *ev_ctx = arg1; 1274 struct spdk_vhost_session *vsession; 1275 struct spdk_vhost_dev *vdev = ev_ctx->vdev; 1276 int rc; 1277 1278 if (spdk_vhost_trylock() != 0) { 1279 spdk_thread_send_msg(spdk_get_thread(), foreach_session, arg1); 1280 return; 1281 } 1282 1283 TAILQ_FOREACH(vsession, &to_user_dev(vdev)->vsessions, tailq) { 1284 if (vsession->initialized) { 1285 rc = ev_ctx->cb_fn(vdev, vsession, ev_ctx->user_ctx); 1286 if (rc < 0) { 1287 goto out; 1288 } 1289 } 1290 } 1291 1292 out: 1293 spdk_vhost_unlock(); 1294 1295 spdk_thread_send_msg(g_vhost_user_init_thread, foreach_session_finish_cb, arg1); 1296 } 1297 1298 void 1299 vhost_user_dev_foreach_session(struct spdk_vhost_dev *vdev, 1300 spdk_vhost_session_fn fn, 1301 spdk_vhost_dev_fn cpl_fn, 1302 void *arg) 1303 { 1304 struct vhost_session_fn_ctx *ev_ctx; 1305 struct spdk_vhost_user_dev *user_dev = to_user_dev(vdev); 1306 1307 ev_ctx = calloc(1, sizeof(*ev_ctx)); 1308 if (ev_ctx == NULL) { 1309 SPDK_ERRLOG("Failed to alloc vhost event.\n"); 1310 assert(false); 1311 return; 1312 } 1313 1314 ev_ctx->vdev = vdev; 1315 ev_ctx->cb_fn = fn; 1316 ev_ctx->cpl_fn = cpl_fn; 1317 ev_ctx->user_ctx = arg; 1318 1319 assert(user_dev->pending_async_op_num < UINT32_MAX); 1320 user_dev->pending_async_op_num++; 1321 1322 spdk_thread_send_msg(vdev->thread, foreach_session, ev_ctx); 1323 } 1324 1325 void 1326 vhost_user_session_set_interrupt_mode(struct spdk_vhost_session *vsession, bool interrupt_mode) 1327 { 1328 uint16_t i; 1329 bool packed_ring; 1330 int rc = 0; 1331 1332 packed_ring = ((vsession->negotiated_features & (1ULL << VIRTIO_F_RING_PACKED)) != 0); 1333 1334 for (i = 0; i < vsession->max_queues; i++) { 1335 struct spdk_vhost_virtqueue *q = &vsession->virtqueue[i]; 1336 uint64_t num_events = 1; 1337 1338 /* vring.desc and vring.desc_packed are in a union struct 1339 * so q->vring.desc can replace q->vring.desc_packed. 1340 */ 1341 if (q->vring.desc == NULL || q->vring.size == 0) { 1342 continue; 1343 } 1344 1345 if (interrupt_mode) { 1346 /* Enable I/O submission notifications, we'll be interrupting. */ 1347 if (packed_ring) { 1348 * (volatile uint16_t *) &q->vring.device_event->flags = VRING_PACKED_EVENT_FLAG_ENABLE; 1349 } else { 1350 * (volatile uint16_t *) &q->vring.used->flags = 0; 1351 } 1352 1353 /* In case of race condition, always kick vring when switch to intr */ 1354 rc = write(q->vring.kickfd, &num_events, sizeof(num_events)); 1355 if (rc < 0) { 1356 SPDK_ERRLOG("failed to kick vring: %s.\n", spdk_strerror(errno)); 1357 } 1358 1359 vsession->interrupt_mode = true; 1360 } else { 1361 /* Disable I/O submission notifications, we'll be polling. */ 1362 if (packed_ring) { 1363 * (volatile uint16_t *) &q->vring.device_event->flags = VRING_PACKED_EVENT_FLAG_DISABLE; 1364 } else { 1365 * (volatile uint16_t *) &q->vring.used->flags = VRING_USED_F_NO_NOTIFY; 1366 } 1367 1368 vsession->interrupt_mode = false; 1369 } 1370 } 1371 } 1372 1373 1374 static enum rte_vhost_msg_result 1375 extern_vhost_pre_msg_handler(int vid, void *_msg) 1376 { 1377 struct vhost_user_msg *msg = _msg; 1378 struct spdk_vhost_session *vsession; 1379 1380 vsession = vhost_session_find_by_vid(vid); 1381 if (vsession == NULL) { 1382 SPDK_ERRLOG("Received a message to unitialized session (vid %d).\n", vid); 1383 assert(false); 1384 return RTE_VHOST_MSG_RESULT_ERR; 1385 } 1386 1387 switch (msg->request) { 1388 case VHOST_USER_GET_VRING_BASE: 1389 if (vsession->forced_polling && vsession->started) { 1390 /* Our queue is stopped for whatever reason, but we may still 1391 * need to poll it after it's initialized again. 1392 */ 1393 g_spdk_vhost_ops.destroy_device(vid); 1394 } 1395 break; 1396 case VHOST_USER_SET_VRING_BASE: 1397 case VHOST_USER_SET_VRING_ADDR: 1398 case VHOST_USER_SET_VRING_NUM: 1399 if (vsession->forced_polling && vsession->started) { 1400 /* Additional queues are being initialized, so we either processed 1401 * enough I/Os and are switching from SeaBIOS to the OS now, or 1402 * we were never in SeaBIOS in the first place. Either way, we 1403 * don't need our workaround anymore. 1404 */ 1405 g_spdk_vhost_ops.destroy_device(vid); 1406 vsession->forced_polling = false; 1407 } 1408 break; 1409 case VHOST_USER_SET_VRING_KICK: 1410 /* rte_vhost(after 20.08) will call new_device after one active vring is 1411 * configured, we will start the session before all vrings are available, 1412 * so for each new vring, if the session is started, we need to restart it 1413 * again. 1414 */ 1415 case VHOST_USER_SET_VRING_CALL: 1416 /* rte_vhost will close the previous callfd and won't notify 1417 * us about any change. This will effectively make SPDK fail 1418 * to deliver any subsequent interrupts until a session is 1419 * restarted. We stop the session here before closing the previous 1420 * fd (so that all interrupts must have been delivered by the 1421 * time the descriptor is closed) and start right after (which 1422 * will make SPDK retrieve the latest, up-to-date callfd from 1423 * rte_vhost. 1424 */ 1425 case VHOST_USER_SET_MEM_TABLE: 1426 /* rte_vhost will unmap previous memory that SPDK may still 1427 * have pending DMA operations on. We can't let that happen, 1428 * so stop the device before letting rte_vhost unmap anything. 1429 * This will block until all pending I/Os are finished. 1430 * We will start the device again from the post-processing 1431 * message handler. 1432 */ 1433 if (vsession->started) { 1434 g_spdk_vhost_ops.destroy_device(vid); 1435 vsession->needs_restart = true; 1436 } 1437 break; 1438 case VHOST_USER_GET_CONFIG: { 1439 int rc = 0; 1440 1441 spdk_vhost_lock(); 1442 if (vsession->vdev->backend->vhost_get_config) { 1443 rc = vsession->vdev->backend->vhost_get_config(vsession->vdev, 1444 msg->payload.cfg.region, msg->payload.cfg.size); 1445 if (rc != 0) { 1446 msg->size = 0; 1447 } 1448 } 1449 spdk_vhost_unlock(); 1450 1451 return RTE_VHOST_MSG_RESULT_REPLY; 1452 } 1453 case VHOST_USER_SET_CONFIG: { 1454 int rc = 0; 1455 1456 spdk_vhost_lock(); 1457 if (vsession->vdev->backend->vhost_set_config) { 1458 rc = vsession->vdev->backend->vhost_set_config(vsession->vdev, 1459 msg->payload.cfg.region, msg->payload.cfg.offset, 1460 msg->payload.cfg.size, msg->payload.cfg.flags); 1461 } 1462 spdk_vhost_unlock(); 1463 1464 return rc == 0 ? RTE_VHOST_MSG_RESULT_OK : RTE_VHOST_MSG_RESULT_ERR; 1465 } 1466 default: 1467 break; 1468 } 1469 1470 return RTE_VHOST_MSG_RESULT_NOT_HANDLED; 1471 } 1472 1473 static enum rte_vhost_msg_result 1474 extern_vhost_post_msg_handler(int vid, void *_msg) 1475 { 1476 struct vhost_user_msg *msg = _msg; 1477 struct spdk_vhost_session *vsession; 1478 1479 vsession = vhost_session_find_by_vid(vid); 1480 if (vsession == NULL) { 1481 SPDK_ERRLOG("Received a message to unitialized session (vid %d).\n", vid); 1482 assert(false); 1483 return RTE_VHOST_MSG_RESULT_ERR; 1484 } 1485 1486 if (vsession->needs_restart) { 1487 g_spdk_vhost_ops.new_device(vid); 1488 vsession->needs_restart = false; 1489 return RTE_VHOST_MSG_RESULT_NOT_HANDLED; 1490 } 1491 1492 switch (msg->request) { 1493 case VHOST_USER_SET_FEATURES: 1494 /* rte_vhost requires all queues to be fully initialized in order 1495 * to start I/O processing. This behavior is not compliant with the 1496 * vhost-user specification and doesn't work with QEMU 2.12+, which 1497 * will only initialize 1 I/O queue for the SeaBIOS boot. 1498 * Theoretically, we should start polling each virtqueue individually 1499 * after receiving its SET_VRING_KICK message, but rte_vhost is not 1500 * designed to poll individual queues. So here we use a workaround 1501 * to detect when the vhost session could be potentially at that SeaBIOS 1502 * stage and we mark it to start polling as soon as its first virtqueue 1503 * gets initialized. This doesn't hurt any non-QEMU vhost slaves 1504 * and allows QEMU 2.12+ to boot correctly. SET_FEATURES could be sent 1505 * at any time, but QEMU will send it at least once on SeaBIOS 1506 * initialization - whenever powered-up or rebooted. 1507 */ 1508 vsession->forced_polling = true; 1509 break; 1510 case VHOST_USER_SET_VRING_KICK: 1511 /* vhost-user spec tells us to start polling a queue after receiving 1512 * its SET_VRING_KICK message. Let's do it! 1513 */ 1514 if (vsession->forced_polling && !vsession->started) { 1515 g_spdk_vhost_ops.new_device(vid); 1516 } 1517 break; 1518 default: 1519 break; 1520 } 1521 1522 return RTE_VHOST_MSG_RESULT_NOT_HANDLED; 1523 } 1524 1525 struct rte_vhost_user_extern_ops g_spdk_extern_vhost_ops = { 1526 .pre_msg_handle = extern_vhost_pre_msg_handler, 1527 .post_msg_handle = extern_vhost_post_msg_handler, 1528 }; 1529 1530 void 1531 vhost_session_install_rte_compat_hooks(struct spdk_vhost_session *vsession) 1532 { 1533 int rc; 1534 1535 rc = rte_vhost_extern_callback_register(vsession->vid, &g_spdk_extern_vhost_ops, NULL); 1536 if (rc != 0) { 1537 SPDK_ERRLOG("rte_vhost_extern_callback_register() failed for vid = %d\n", 1538 vsession->vid); 1539 return; 1540 } 1541 } 1542 1543 int 1544 vhost_register_unix_socket(const char *path, const char *ctrl_name, 1545 uint64_t virtio_features, uint64_t disabled_features, uint64_t protocol_features) 1546 { 1547 struct stat file_stat; 1548 uint64_t features = 0; 1549 1550 /* Register vhost driver to handle vhost messages. */ 1551 if (stat(path, &file_stat) != -1) { 1552 if (!S_ISSOCK(file_stat.st_mode)) { 1553 SPDK_ERRLOG("Cannot create a domain socket at path \"%s\": " 1554 "The file already exists and is not a socket.\n", 1555 path); 1556 return -EIO; 1557 } else if (unlink(path) != 0) { 1558 SPDK_ERRLOG("Cannot create a domain socket at path \"%s\": " 1559 "The socket already exists and failed to unlink.\n", 1560 path); 1561 return -EIO; 1562 } 1563 } 1564 1565 #if RTE_VERSION < RTE_VERSION_NUM(20, 8, 0, 0) 1566 if (rte_vhost_driver_register(path, 0) != 0) { 1567 #else 1568 if (rte_vhost_driver_register(path, RTE_VHOST_USER_ASYNC_COPY) != 0) { 1569 #endif 1570 SPDK_ERRLOG("Could not register controller %s with vhost library\n", ctrl_name); 1571 SPDK_ERRLOG("Check if domain socket %s already exists\n", path); 1572 return -EIO; 1573 } 1574 if (rte_vhost_driver_set_features(path, virtio_features) || 1575 rte_vhost_driver_disable_features(path, disabled_features)) { 1576 SPDK_ERRLOG("Couldn't set vhost features for controller %s\n", ctrl_name); 1577 1578 rte_vhost_driver_unregister(path); 1579 return -EIO; 1580 } 1581 1582 if (rte_vhost_driver_callback_register(path, &g_spdk_vhost_ops) != 0) { 1583 rte_vhost_driver_unregister(path); 1584 SPDK_ERRLOG("Couldn't register callbacks for controller %s\n", ctrl_name); 1585 return -EIO; 1586 } 1587 1588 rte_vhost_driver_get_protocol_features(path, &features); 1589 features |= protocol_features; 1590 rte_vhost_driver_set_protocol_features(path, features); 1591 1592 if (rte_vhost_driver_start(path) != 0) { 1593 SPDK_ERRLOG("Failed to start vhost driver for controller %s (%d): %s\n", 1594 ctrl_name, errno, spdk_strerror(errno)); 1595 rte_vhost_driver_unregister(path); 1596 return -EIO; 1597 } 1598 1599 return 0; 1600 } 1601 1602 int 1603 vhost_get_mem_table(int vid, struct rte_vhost_memory **mem) 1604 { 1605 return rte_vhost_get_mem_table(vid, mem); 1606 } 1607 1608 int 1609 vhost_driver_unregister(const char *path) 1610 { 1611 return rte_vhost_driver_unregister(path); 1612 } 1613 1614 int 1615 vhost_get_negotiated_features(int vid, uint64_t *negotiated_features) 1616 { 1617 return rte_vhost_get_negotiated_features(vid, negotiated_features); 1618 } 1619 1620 int 1621 vhost_user_dev_set_coalescing(struct spdk_vhost_user_dev *user_dev, uint32_t delay_base_us, 1622 uint32_t iops_threshold) 1623 { 1624 uint64_t delay_time_base = delay_base_us * spdk_get_ticks_hz() / 1000000ULL; 1625 uint32_t io_rate = iops_threshold * SPDK_VHOST_STATS_CHECK_INTERVAL_MS / 1000U; 1626 1627 if (delay_time_base >= UINT32_MAX) { 1628 SPDK_ERRLOG("Delay time of %"PRIu32" is to big\n", delay_base_us); 1629 return -EINVAL; 1630 } else if (io_rate == 0) { 1631 SPDK_ERRLOG("IOPS rate of %"PRIu32" is too low. Min is %u\n", io_rate, 1632 1000U / SPDK_VHOST_STATS_CHECK_INTERVAL_MS); 1633 return -EINVAL; 1634 } 1635 1636 user_dev->coalescing_delay_us = delay_base_us; 1637 user_dev->coalescing_iops_threshold = iops_threshold; 1638 return 0; 1639 } 1640 1641 int 1642 vhost_user_session_set_coalescing(struct spdk_vhost_dev *vdev, 1643 struct spdk_vhost_session *vsession, void *ctx) 1644 { 1645 vsession->coalescing_delay_time_base = 1646 to_user_dev(vdev)->coalescing_delay_us * spdk_get_ticks_hz() / 1000000ULL; 1647 vsession->coalescing_io_rate_threshold = 1648 to_user_dev(vdev)->coalescing_iops_threshold * SPDK_VHOST_STATS_CHECK_INTERVAL_MS / 1000U; 1649 return 0; 1650 } 1651 1652 int 1653 spdk_vhost_set_coalescing(struct spdk_vhost_dev *vdev, uint32_t delay_base_us, 1654 uint32_t iops_threshold) 1655 { 1656 int rc; 1657 1658 rc = vhost_user_dev_set_coalescing(to_user_dev(vdev), delay_base_us, iops_threshold); 1659 if (rc != 0) { 1660 return rc; 1661 } 1662 1663 vhost_user_dev_foreach_session(vdev, vhost_user_session_set_coalescing, NULL, NULL); 1664 return 0; 1665 } 1666 1667 void 1668 spdk_vhost_get_coalescing(struct spdk_vhost_dev *vdev, uint32_t *delay_base_us, 1669 uint32_t *iops_threshold) 1670 { 1671 struct spdk_vhost_user_dev *user_dev = to_user_dev(vdev); 1672 1673 if (delay_base_us) { 1674 *delay_base_us = user_dev->coalescing_delay_us; 1675 } 1676 1677 if (iops_threshold) { 1678 *iops_threshold = user_dev->coalescing_iops_threshold; 1679 } 1680 } 1681 1682 int 1683 spdk_vhost_set_socket_path(const char *basename) 1684 { 1685 int ret; 1686 1687 if (basename && strlen(basename) > 0) { 1688 ret = snprintf(g_vhost_user_dev_dirname, sizeof(g_vhost_user_dev_dirname) - 2, "%s", basename); 1689 if (ret <= 0) { 1690 return -EINVAL; 1691 } 1692 if ((size_t)ret >= sizeof(g_vhost_user_dev_dirname) - 2) { 1693 SPDK_ERRLOG("Char dev dir path length %d is too long\n", ret); 1694 return -EINVAL; 1695 } 1696 1697 if (g_vhost_user_dev_dirname[ret - 1] != '/') { 1698 g_vhost_user_dev_dirname[ret] = '/'; 1699 g_vhost_user_dev_dirname[ret + 1] = '\0'; 1700 } 1701 } 1702 1703 return 0; 1704 } 1705 1706 static void 1707 vhost_dev_thread_exit(void *arg1) 1708 { 1709 spdk_thread_exit(spdk_get_thread()); 1710 } 1711 1712 int 1713 vhost_user_dev_register(struct spdk_vhost_dev *vdev, const char *name, struct spdk_cpuset *cpumask, 1714 const struct spdk_vhost_user_dev_backend *user_backend) 1715 { 1716 char path[PATH_MAX]; 1717 struct spdk_vhost_user_dev *user_dev; 1718 1719 if (snprintf(path, sizeof(path), "%s%s", g_vhost_user_dev_dirname, name) >= (int)sizeof(path)) { 1720 SPDK_ERRLOG("Resulting socket path for controller %s is too long: %s%s\n", 1721 name,g_vhost_user_dev_dirname, name); 1722 return -EINVAL; 1723 } 1724 1725 vdev->path = strdup(path); 1726 if (vdev->path == NULL) { 1727 return -EIO; 1728 } 1729 1730 user_dev = calloc(1, sizeof(*user_dev)); 1731 if (user_dev == NULL) { 1732 free(vdev->path); 1733 return -ENOMEM; 1734 } 1735 vdev->ctxt = user_dev; 1736 1737 vdev->thread = spdk_thread_create(vdev->name, cpumask); 1738 if (vdev->thread == NULL) { 1739 free(user_dev); 1740 free(vdev->path); 1741 SPDK_ERRLOG("Failed to create thread for vhost controller %s.\n", name); 1742 return -EIO; 1743 } 1744 1745 vdev->registered = true; 1746 user_dev->user_backend = user_backend; 1747 user_dev->vdev = vdev; 1748 TAILQ_INIT(&user_dev->vsessions); 1749 1750 vhost_user_dev_set_coalescing(user_dev, SPDK_VHOST_COALESCING_DELAY_BASE_US, 1751 SPDK_VHOST_VQ_IOPS_COALESCING_THRESHOLD); 1752 1753 if (vhost_register_unix_socket(path, name, vdev->virtio_features, vdev->disabled_features, 1754 vdev->protocol_features)) { 1755 spdk_thread_send_msg(vdev->thread, vhost_dev_thread_exit, NULL); 1756 free(user_dev); 1757 free(vdev->path); 1758 return -EIO; 1759 } 1760 1761 return 0; 1762 } 1763 1764 int 1765 vhost_user_dev_unregister(struct spdk_vhost_dev *vdev) 1766 { 1767 struct spdk_vhost_user_dev *user_dev = to_user_dev(vdev); 1768 1769 if (user_dev->pending_async_op_num) { 1770 return -EBUSY; 1771 } 1772 1773 if (!TAILQ_EMPTY(&user_dev->vsessions)) { 1774 SPDK_ERRLOG("Controller %s has still valid connection.\n", vdev->name); 1775 return -EBUSY; 1776 } 1777 1778 if (vdev->registered && vhost_driver_unregister(vdev->path) != 0) { 1779 SPDK_ERRLOG("Could not unregister controller %s with vhost library\n" 1780 "Check if domain socket %s still exists\n", 1781 vdev->name, vdev->path); 1782 return -EIO; 1783 } 1784 1785 spdk_thread_send_msg(vdev->thread, vhost_dev_thread_exit, NULL); 1786 free(user_dev); 1787 free(vdev->path); 1788 1789 return 0; 1790 } 1791 1792 static bool g_vhost_user_started = false; 1793 1794 int 1795 vhost_user_init(void) 1796 { 1797 size_t len; 1798 1799 if (g_vhost_user_started) { 1800 return 0; 1801 } 1802 1803 if (g_vhost_user_dev_dirname[0] == '\0') { 1804 if (getcwd(g_vhost_user_dev_dirname, sizeof(g_vhost_user_dev_dirname) - 1) == NULL) { 1805 SPDK_ERRLOG("getcwd failed (%d): %s\n", errno, spdk_strerror(errno)); 1806 return -1; 1807 } 1808 1809 len = strlen(g_vhost_user_dev_dirname); 1810 if (g_vhost_user_dev_dirname[len - 1] != '/') { 1811 g_vhost_user_dev_dirname[len] = '/'; 1812 g_vhost_user_dev_dirname[len + 1] = '\0'; 1813 } 1814 } 1815 1816 g_vhost_user_started = true; 1817 1818 g_vhost_user_init_thread = spdk_get_thread(); 1819 assert(g_vhost_user_init_thread != NULL); 1820 1821 return 0; 1822 } 1823 1824 static void 1825 vhost_user_session_shutdown_on_init(void *vhost_cb) 1826 { 1827 spdk_vhost_fini_cb fn = vhost_cb; 1828 1829 fn(); 1830 } 1831 1832 static void * 1833 vhost_user_session_shutdown(void *vhost_cb) 1834 { 1835 struct spdk_vhost_dev *vdev = NULL; 1836 struct spdk_vhost_session *vsession; 1837 1838 for (vdev = spdk_vhost_dev_next(NULL); vdev != NULL; 1839 vdev = spdk_vhost_dev_next(vdev)) { 1840 spdk_vhost_lock(); 1841 TAILQ_FOREACH(vsession, &to_user_dev(vdev)->vsessions, tailq) { 1842 if (vsession->started) { 1843 _stop_session(vsession); 1844 } 1845 } 1846 spdk_vhost_unlock(); 1847 vhost_driver_unregister(vdev->path); 1848 vdev->registered = false; 1849 } 1850 1851 SPDK_INFOLOG(vhost, "Exiting\n"); 1852 spdk_thread_send_msg(g_vhost_user_init_thread, vhost_user_session_shutdown_on_init, vhost_cb); 1853 return NULL; 1854 } 1855 1856 void 1857 vhost_user_fini(spdk_vhost_fini_cb vhost_cb) 1858 { 1859 pthread_t tid; 1860 int rc; 1861 1862 if (!g_vhost_user_started) { 1863 vhost_cb(); 1864 return; 1865 } 1866 1867 g_vhost_user_started = false; 1868 1869 /* rte_vhost API for removing sockets is not asynchronous. Since it may call SPDK 1870 * ops for stopping a device or removing a connection, we need to call it from 1871 * a separate thread to avoid deadlock. 1872 */ 1873 rc = pthread_create(&tid, NULL, &vhost_user_session_shutdown, vhost_cb); 1874 if (rc < 0) { 1875 SPDK_ERRLOG("Failed to start session shutdown thread (%d): %s\n", rc, spdk_strerror(rc)); 1876 abort(); 1877 } 1878 pthread_detach(tid); 1879 } 1880