1 /* 2 * vhost-user-blk host device 3 * 4 * Copyright(C) 2017 Intel Corporation. 5 * 6 * Authors: 7 * Changpeng Liu <changpeng.liu@intel.com> 8 * 9 * Largely based on the "vhost-user-scsi.c" and "vhost-scsi.c" implemented by: 10 * Felipe Franciosi <felipe@nutanix.com> 11 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> 12 * Nicholas Bellinger <nab@risingtidesystems.com> 13 * 14 * This work is licensed under the terms of the GNU LGPL, version 2 or later. 15 * See the COPYING.LIB file in the top-level directory. 16 * 17 */ 18 19 #include "qemu/osdep.h" 20 #include "qapi/error.h" 21 #include "qemu/error-report.h" 22 #include "qemu/cutils.h" 23 #include "hw/qdev-core.h" 24 #include "hw/qdev-properties.h" 25 #include "hw/qdev-properties-system.h" 26 #include "hw/virtio/virtio-blk-common.h" 27 #include "hw/virtio/vhost.h" 28 #include "hw/virtio/vhost-user-blk.h" 29 #include "hw/virtio/virtio.h" 30 #include "hw/virtio/virtio-bus.h" 31 #include "hw/virtio/virtio-access.h" 32 #include "sysemu/sysemu.h" 33 #include "sysemu/runstate.h" 34 35 #define REALIZE_CONNECTION_RETRIES 3 36 37 static const int user_feature_bits[] = { 38 VIRTIO_BLK_F_SIZE_MAX, 39 VIRTIO_BLK_F_SEG_MAX, 40 VIRTIO_BLK_F_GEOMETRY, 41 VIRTIO_BLK_F_BLK_SIZE, 42 VIRTIO_BLK_F_TOPOLOGY, 43 VIRTIO_BLK_F_MQ, 44 VIRTIO_BLK_F_RO, 45 VIRTIO_BLK_F_FLUSH, 46 VIRTIO_BLK_F_CONFIG_WCE, 47 VIRTIO_BLK_F_DISCARD, 48 VIRTIO_BLK_F_WRITE_ZEROES, 49 VIRTIO_F_VERSION_1, 50 VIRTIO_RING_F_INDIRECT_DESC, 51 VIRTIO_RING_F_EVENT_IDX, 52 VIRTIO_F_NOTIFY_ON_EMPTY, 53 VIRTIO_F_RING_PACKED, 54 VIRTIO_F_IOMMU_PLATFORM, 55 VHOST_INVALID_FEATURE_BIT 56 }; 57 58 static void vhost_user_blk_event(void *opaque, QEMUChrEvent event); 59 60 static void vhost_user_blk_update_config(VirtIODevice *vdev, uint8_t *config) 61 { 62 VHostUserBlk *s = VHOST_USER_BLK(vdev); 63 64 /* Our num_queues overrides the device backend */ 65 virtio_stw_p(vdev, &s->blkcfg.num_queues, s->num_queues); 66 67 memcpy(config, &s->blkcfg, vdev->config_len); 68 } 69 70 static void vhost_user_blk_set_config(VirtIODevice *vdev, const uint8_t *config) 71 { 72 VHostUserBlk *s = VHOST_USER_BLK(vdev); 73 struct virtio_blk_config *blkcfg = (struct virtio_blk_config *)config; 74 int ret; 75 76 if (blkcfg->wce == s->blkcfg.wce) { 77 return; 78 } 79 80 ret = vhost_dev_set_config(&s->dev, &blkcfg->wce, 81 offsetof(struct virtio_blk_config, wce), 82 sizeof(blkcfg->wce), 83 VHOST_SET_CONFIG_TYPE_MASTER); 84 if (ret) { 85 error_report("set device config space failed"); 86 return; 87 } 88 89 s->blkcfg.wce = blkcfg->wce; 90 } 91 92 static int vhost_user_blk_handle_config_change(struct vhost_dev *dev) 93 { 94 int ret; 95 struct virtio_blk_config blkcfg; 96 VirtIODevice *vdev = dev->vdev; 97 VHostUserBlk *s = VHOST_USER_BLK(dev->vdev); 98 Error *local_err = NULL; 99 100 if (!dev->started) { 101 return 0; 102 } 103 104 ret = vhost_dev_get_config(dev, (uint8_t *)&blkcfg, 105 vdev->config_len, &local_err); 106 if (ret < 0) { 107 error_report_err(local_err); 108 return ret; 109 } 110 111 /* valid for resize only */ 112 if (blkcfg.capacity != s->blkcfg.capacity) { 113 s->blkcfg.capacity = blkcfg.capacity; 114 memcpy(dev->vdev->config, &s->blkcfg, vdev->config_len); 115 virtio_notify_config(dev->vdev); 116 } 117 118 return 0; 119 } 120 121 const VhostDevConfigOps blk_ops = { 122 .vhost_dev_config_notifier = vhost_user_blk_handle_config_change, 123 }; 124 125 static int vhost_user_blk_start(VirtIODevice *vdev, Error **errp) 126 { 127 VHostUserBlk *s = VHOST_USER_BLK(vdev); 128 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 129 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 130 int i, ret; 131 132 if (!k->set_guest_notifiers) { 133 error_setg(errp, "binding does not support guest notifiers"); 134 return -ENOSYS; 135 } 136 137 ret = vhost_dev_enable_notifiers(&s->dev, vdev); 138 if (ret < 0) { 139 error_setg_errno(errp, -ret, "Error enabling host notifiers"); 140 return ret; 141 } 142 143 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true); 144 if (ret < 0) { 145 error_setg_errno(errp, -ret, "Error binding guest notifier"); 146 goto err_host_notifiers; 147 } 148 149 s->dev.acked_features = vdev->guest_features; 150 151 ret = vhost_dev_prepare_inflight(&s->dev, vdev); 152 if (ret < 0) { 153 error_setg_errno(errp, -ret, "Error setting inflight format"); 154 goto err_guest_notifiers; 155 } 156 157 if (!s->inflight->addr) { 158 ret = vhost_dev_get_inflight(&s->dev, s->queue_size, s->inflight); 159 if (ret < 0) { 160 error_setg_errno(errp, -ret, "Error getting inflight"); 161 goto err_guest_notifiers; 162 } 163 } 164 165 ret = vhost_dev_set_inflight(&s->dev, s->inflight); 166 if (ret < 0) { 167 error_setg_errno(errp, -ret, "Error setting inflight"); 168 goto err_guest_notifiers; 169 } 170 171 /* guest_notifier_mask/pending not used yet, so just unmask 172 * everything here. virtio-pci will do the right thing by 173 * enabling/disabling irqfd. 174 */ 175 for (i = 0; i < s->dev.nvqs; i++) { 176 vhost_virtqueue_mask(&s->dev, vdev, i, false); 177 } 178 179 s->dev.vq_index_end = s->dev.nvqs; 180 ret = vhost_dev_start(&s->dev, vdev); 181 if (ret < 0) { 182 error_setg_errno(errp, -ret, "Error starting vhost"); 183 goto err_guest_notifiers; 184 } 185 s->started_vu = true; 186 187 return ret; 188 189 err_guest_notifiers: 190 for (i = 0; i < s->dev.nvqs; i++) { 191 vhost_virtqueue_mask(&s->dev, vdev, i, true); 192 } 193 k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false); 194 err_host_notifiers: 195 vhost_dev_disable_notifiers(&s->dev, vdev); 196 return ret; 197 } 198 199 static void vhost_user_blk_stop(VirtIODevice *vdev) 200 { 201 VHostUserBlk *s = VHOST_USER_BLK(vdev); 202 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 203 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 204 int ret; 205 206 if (!s->started_vu) { 207 return; 208 } 209 s->started_vu = false; 210 211 if (!k->set_guest_notifiers) { 212 return; 213 } 214 215 vhost_dev_stop(&s->dev, vdev); 216 217 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false); 218 if (ret < 0) { 219 error_report("vhost guest notifier cleanup failed: %d", ret); 220 return; 221 } 222 223 vhost_dev_disable_notifiers(&s->dev, vdev); 224 } 225 226 static void vhost_user_blk_set_status(VirtIODevice *vdev, uint8_t status) 227 { 228 VHostUserBlk *s = VHOST_USER_BLK(vdev); 229 bool should_start = virtio_device_started(vdev, status); 230 Error *local_err = NULL; 231 int ret; 232 233 if (!vdev->vm_running) { 234 should_start = false; 235 } 236 237 if (!s->connected) { 238 return; 239 } 240 241 if (vhost_dev_is_started(&s->dev) == should_start) { 242 return; 243 } 244 245 if (should_start) { 246 ret = vhost_user_blk_start(vdev, &local_err); 247 if (ret < 0) { 248 error_reportf_err(local_err, "vhost-user-blk: vhost start failed: "); 249 qemu_chr_fe_disconnect(&s->chardev); 250 } 251 } else { 252 vhost_user_blk_stop(vdev); 253 } 254 255 } 256 257 static uint64_t vhost_user_blk_get_features(VirtIODevice *vdev, 258 uint64_t features, 259 Error **errp) 260 { 261 VHostUserBlk *s = VHOST_USER_BLK(vdev); 262 263 /* Turn on pre-defined features */ 264 virtio_add_feature(&features, VIRTIO_BLK_F_SIZE_MAX); 265 virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX); 266 virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY); 267 virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY); 268 virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE); 269 virtio_add_feature(&features, VIRTIO_BLK_F_FLUSH); 270 virtio_add_feature(&features, VIRTIO_BLK_F_RO); 271 272 if (s->num_queues > 1) { 273 virtio_add_feature(&features, VIRTIO_BLK_F_MQ); 274 } 275 276 return vhost_get_features(&s->dev, user_feature_bits, features); 277 } 278 279 static void vhost_user_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq) 280 { 281 VHostUserBlk *s = VHOST_USER_BLK(vdev); 282 Error *local_err = NULL; 283 int i, ret; 284 285 if (!vdev->start_on_kick) { 286 return; 287 } 288 289 if (!s->connected) { 290 return; 291 } 292 293 if (vhost_dev_is_started(&s->dev)) { 294 return; 295 } 296 297 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start 298 * vhost here instead of waiting for .set_status(). 299 */ 300 ret = vhost_user_blk_start(vdev, &local_err); 301 if (ret < 0) { 302 error_reportf_err(local_err, "vhost-user-blk: vhost start failed: "); 303 qemu_chr_fe_disconnect(&s->chardev); 304 return; 305 } 306 307 /* Kick right away to begin processing requests already in vring */ 308 for (i = 0; i < s->dev.nvqs; i++) { 309 VirtQueue *kick_vq = virtio_get_queue(vdev, i); 310 311 if (!virtio_queue_get_desc_addr(vdev, i)) { 312 continue; 313 } 314 event_notifier_set(virtio_queue_get_host_notifier(kick_vq)); 315 } 316 } 317 318 static void vhost_user_blk_reset(VirtIODevice *vdev) 319 { 320 VHostUserBlk *s = VHOST_USER_BLK(vdev); 321 322 vhost_dev_free_inflight(s->inflight); 323 } 324 325 static int vhost_user_blk_connect(DeviceState *dev, Error **errp) 326 { 327 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 328 VHostUserBlk *s = VHOST_USER_BLK(vdev); 329 int ret = 0; 330 331 if (s->connected) { 332 return 0; 333 } 334 s->connected = true; 335 336 s->dev.num_queues = s->num_queues; 337 s->dev.nvqs = s->num_queues; 338 s->dev.vqs = s->vhost_vqs; 339 s->dev.vq_index = 0; 340 s->dev.backend_features = 0; 341 342 vhost_dev_set_config_notifier(&s->dev, &blk_ops); 343 344 s->vhost_user.supports_config = true; 345 ret = vhost_dev_init(&s->dev, &s->vhost_user, VHOST_BACKEND_TYPE_USER, 0, 346 errp); 347 if (ret < 0) { 348 return ret; 349 } 350 351 /* restore vhost state */ 352 if (virtio_device_started(vdev, vdev->status)) { 353 ret = vhost_user_blk_start(vdev, errp); 354 if (ret < 0) { 355 return ret; 356 } 357 } 358 359 return 0; 360 } 361 362 static void vhost_user_blk_disconnect(DeviceState *dev) 363 { 364 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 365 VHostUserBlk *s = VHOST_USER_BLK(vdev); 366 367 if (!s->connected) { 368 return; 369 } 370 s->connected = false; 371 372 vhost_user_blk_stop(vdev); 373 374 vhost_dev_cleanup(&s->dev); 375 } 376 377 static void vhost_user_blk_chr_closed_bh(void *opaque) 378 { 379 DeviceState *dev = opaque; 380 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 381 VHostUserBlk *s = VHOST_USER_BLK(vdev); 382 383 vhost_user_blk_disconnect(dev); 384 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, vhost_user_blk_event, 385 NULL, opaque, NULL, true); 386 } 387 388 static void vhost_user_blk_event(void *opaque, QEMUChrEvent event) 389 { 390 DeviceState *dev = opaque; 391 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 392 VHostUserBlk *s = VHOST_USER_BLK(vdev); 393 Error *local_err = NULL; 394 395 switch (event) { 396 case CHR_EVENT_OPENED: 397 if (vhost_user_blk_connect(dev, &local_err) < 0) { 398 error_report_err(local_err); 399 qemu_chr_fe_disconnect(&s->chardev); 400 return; 401 } 402 break; 403 case CHR_EVENT_CLOSED: 404 if (!runstate_check(RUN_STATE_SHUTDOWN)) { 405 /* 406 * A close event may happen during a read/write, but vhost 407 * code assumes the vhost_dev remains setup, so delay the 408 * stop & clear. 409 */ 410 AioContext *ctx = qemu_get_current_aio_context(); 411 412 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, NULL, NULL, 413 NULL, NULL, false); 414 aio_bh_schedule_oneshot(ctx, vhost_user_blk_chr_closed_bh, opaque); 415 416 /* 417 * Move vhost device to the stopped state. The vhost-user device 418 * will be clean up and disconnected in BH. This can be useful in 419 * the vhost migration code. If disconnect was caught there is an 420 * option for the general vhost code to get the dev state without 421 * knowing its type (in this case vhost-user). 422 * 423 * FIXME: this is sketchy to be reaching into vhost_dev 424 * now because we are forcing something that implies we 425 * have executed vhost_dev_stop() but that won't happen 426 * until vhost_user_blk_stop() gets called from the bh. 427 * Really this state check should be tracked locally. 428 */ 429 s->dev.started = false; 430 } 431 break; 432 case CHR_EVENT_BREAK: 433 case CHR_EVENT_MUX_IN: 434 case CHR_EVENT_MUX_OUT: 435 /* Ignore */ 436 break; 437 } 438 } 439 440 static int vhost_user_blk_realize_connect(VHostUserBlk *s, Error **errp) 441 { 442 DeviceState *dev = &s->parent_obj.parent_obj; 443 int ret; 444 445 s->connected = false; 446 447 ret = qemu_chr_fe_wait_connected(&s->chardev, errp); 448 if (ret < 0) { 449 return ret; 450 } 451 452 ret = vhost_user_blk_connect(dev, errp); 453 if (ret < 0) { 454 qemu_chr_fe_disconnect(&s->chardev); 455 return ret; 456 } 457 assert(s->connected); 458 459 ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg, 460 s->parent_obj.config_len, errp); 461 if (ret < 0) { 462 qemu_chr_fe_disconnect(&s->chardev); 463 vhost_dev_cleanup(&s->dev); 464 return ret; 465 } 466 467 return 0; 468 } 469 470 static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp) 471 { 472 ERRP_GUARD(); 473 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 474 VHostUserBlk *s = VHOST_USER_BLK(vdev); 475 size_t config_size; 476 int retries; 477 int i, ret; 478 479 if (!s->chardev.chr) { 480 error_setg(errp, "chardev is mandatory"); 481 return; 482 } 483 484 if (s->num_queues == VHOST_USER_BLK_AUTO_NUM_QUEUES) { 485 s->num_queues = 1; 486 } 487 if (!s->num_queues || s->num_queues > VIRTIO_QUEUE_MAX) { 488 error_setg(errp, "invalid number of IO queues"); 489 return; 490 } 491 492 if (!s->queue_size) { 493 error_setg(errp, "queue size must be non-zero"); 494 return; 495 } 496 if (s->queue_size > VIRTQUEUE_MAX_SIZE) { 497 error_setg(errp, "queue size must not exceed %d", 498 VIRTQUEUE_MAX_SIZE); 499 return; 500 } 501 502 if (!vhost_user_init(&s->vhost_user, &s->chardev, errp)) { 503 return; 504 } 505 506 config_size = virtio_get_config_size(&virtio_blk_cfg_size_params, 507 vdev->host_features); 508 virtio_init(vdev, VIRTIO_ID_BLOCK, config_size); 509 510 s->virtqs = g_new(VirtQueue *, s->num_queues); 511 for (i = 0; i < s->num_queues; i++) { 512 s->virtqs[i] = virtio_add_queue(vdev, s->queue_size, 513 vhost_user_blk_handle_output); 514 } 515 516 s->inflight = g_new0(struct vhost_inflight, 1); 517 s->vhost_vqs = g_new0(struct vhost_virtqueue, s->num_queues); 518 519 retries = REALIZE_CONNECTION_RETRIES; 520 assert(!*errp); 521 do { 522 if (*errp) { 523 error_prepend(errp, "Reconnecting after error: "); 524 error_report_err(*errp); 525 *errp = NULL; 526 } 527 ret = vhost_user_blk_realize_connect(s, errp); 528 } while (ret < 0 && retries--); 529 530 if (ret < 0) { 531 goto virtio_err; 532 } 533 534 /* we're fully initialized, now we can operate, so add the handler */ 535 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, 536 vhost_user_blk_event, NULL, (void *)dev, 537 NULL, true); 538 return; 539 540 virtio_err: 541 g_free(s->vhost_vqs); 542 s->vhost_vqs = NULL; 543 g_free(s->inflight); 544 s->inflight = NULL; 545 for (i = 0; i < s->num_queues; i++) { 546 virtio_delete_queue(s->virtqs[i]); 547 } 548 g_free(s->virtqs); 549 virtio_cleanup(vdev); 550 vhost_user_cleanup(&s->vhost_user); 551 } 552 553 static void vhost_user_blk_device_unrealize(DeviceState *dev) 554 { 555 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 556 VHostUserBlk *s = VHOST_USER_BLK(dev); 557 int i; 558 559 virtio_set_status(vdev, 0); 560 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, NULL, 561 NULL, NULL, NULL, false); 562 vhost_dev_cleanup(&s->dev); 563 vhost_dev_free_inflight(s->inflight); 564 g_free(s->vhost_vqs); 565 s->vhost_vqs = NULL; 566 g_free(s->inflight); 567 s->inflight = NULL; 568 569 for (i = 0; i < s->num_queues; i++) { 570 virtio_delete_queue(s->virtqs[i]); 571 } 572 g_free(s->virtqs); 573 virtio_cleanup(vdev); 574 vhost_user_cleanup(&s->vhost_user); 575 } 576 577 static void vhost_user_blk_instance_init(Object *obj) 578 { 579 VHostUserBlk *s = VHOST_USER_BLK(obj); 580 581 device_add_bootindex_property(obj, &s->bootindex, "bootindex", 582 "/disk@0,0", DEVICE(obj)); 583 } 584 585 static struct vhost_dev *vhost_user_blk_get_vhost(VirtIODevice *vdev) 586 { 587 VHostUserBlk *s = VHOST_USER_BLK(vdev); 588 return &s->dev; 589 } 590 591 static const VMStateDescription vmstate_vhost_user_blk = { 592 .name = "vhost-user-blk", 593 .minimum_version_id = 1, 594 .version_id = 1, 595 .fields = (VMStateField[]) { 596 VMSTATE_VIRTIO_DEVICE, 597 VMSTATE_END_OF_LIST() 598 }, 599 }; 600 601 static Property vhost_user_blk_properties[] = { 602 DEFINE_PROP_CHR("chardev", VHostUserBlk, chardev), 603 DEFINE_PROP_UINT16("num-queues", VHostUserBlk, num_queues, 604 VHOST_USER_BLK_AUTO_NUM_QUEUES), 605 DEFINE_PROP_UINT32("queue-size", VHostUserBlk, queue_size, 128), 606 DEFINE_PROP_BIT64("config-wce", VHostUserBlk, parent_obj.host_features, 607 VIRTIO_BLK_F_CONFIG_WCE, true), 608 DEFINE_PROP_BIT64("discard", VHostUserBlk, parent_obj.host_features, 609 VIRTIO_BLK_F_DISCARD, true), 610 DEFINE_PROP_BIT64("write-zeroes", VHostUserBlk, parent_obj.host_features, 611 VIRTIO_BLK_F_WRITE_ZEROES, true), 612 DEFINE_PROP_END_OF_LIST(), 613 }; 614 615 static void vhost_user_blk_class_init(ObjectClass *klass, void *data) 616 { 617 DeviceClass *dc = DEVICE_CLASS(klass); 618 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 619 620 device_class_set_props(dc, vhost_user_blk_properties); 621 dc->vmsd = &vmstate_vhost_user_blk; 622 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 623 vdc->realize = vhost_user_blk_device_realize; 624 vdc->unrealize = vhost_user_blk_device_unrealize; 625 vdc->get_config = vhost_user_blk_update_config; 626 vdc->set_config = vhost_user_blk_set_config; 627 vdc->get_features = vhost_user_blk_get_features; 628 vdc->set_status = vhost_user_blk_set_status; 629 vdc->reset = vhost_user_blk_reset; 630 vdc->get_vhost = vhost_user_blk_get_vhost; 631 } 632 633 static const TypeInfo vhost_user_blk_info = { 634 .name = TYPE_VHOST_USER_BLK, 635 .parent = TYPE_VIRTIO_DEVICE, 636 .instance_size = sizeof(VHostUserBlk), 637 .instance_init = vhost_user_blk_instance_init, 638 .class_init = vhost_user_blk_class_init, 639 }; 640 641 static void virtio_register_types(void) 642 { 643 type_register_static(&vhost_user_blk_info); 644 } 645 646 type_init(virtio_register_types) 647