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_should_start(vdev, status); 230 Error *local_err = NULL; 231 int ret; 232 233 if (!s->connected) { 234 return; 235 } 236 237 if (vhost_dev_is_started(&s->dev) == should_start) { 238 return; 239 } 240 241 if (should_start) { 242 ret = vhost_user_blk_start(vdev, &local_err); 243 if (ret < 0) { 244 error_reportf_err(local_err, "vhost-user-blk: vhost start failed: "); 245 qemu_chr_fe_disconnect(&s->chardev); 246 } 247 } else { 248 vhost_user_blk_stop(vdev); 249 } 250 251 } 252 253 static uint64_t vhost_user_blk_get_features(VirtIODevice *vdev, 254 uint64_t features, 255 Error **errp) 256 { 257 VHostUserBlk *s = VHOST_USER_BLK(vdev); 258 259 /* Turn on pre-defined features */ 260 virtio_add_feature(&features, VIRTIO_BLK_F_SIZE_MAX); 261 virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX); 262 virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY); 263 virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY); 264 virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE); 265 virtio_add_feature(&features, VIRTIO_BLK_F_FLUSH); 266 virtio_add_feature(&features, VIRTIO_BLK_F_RO); 267 268 if (s->num_queues > 1) { 269 virtio_add_feature(&features, VIRTIO_BLK_F_MQ); 270 } 271 272 return vhost_get_features(&s->dev, user_feature_bits, features); 273 } 274 275 static void vhost_user_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq) 276 { 277 VHostUserBlk *s = VHOST_USER_BLK(vdev); 278 Error *local_err = NULL; 279 int i, ret; 280 281 if (!vdev->start_on_kick) { 282 return; 283 } 284 285 if (!s->connected) { 286 return; 287 } 288 289 if (vhost_dev_is_started(&s->dev)) { 290 return; 291 } 292 293 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start 294 * vhost here instead of waiting for .set_status(). 295 */ 296 ret = vhost_user_blk_start(vdev, &local_err); 297 if (ret < 0) { 298 error_reportf_err(local_err, "vhost-user-blk: vhost start failed: "); 299 qemu_chr_fe_disconnect(&s->chardev); 300 return; 301 } 302 303 /* Kick right away to begin processing requests already in vring */ 304 for (i = 0; i < s->dev.nvqs; i++) { 305 VirtQueue *kick_vq = virtio_get_queue(vdev, i); 306 307 if (!virtio_queue_get_desc_addr(vdev, i)) { 308 continue; 309 } 310 event_notifier_set(virtio_queue_get_host_notifier(kick_vq)); 311 } 312 } 313 314 static void vhost_user_blk_reset(VirtIODevice *vdev) 315 { 316 VHostUserBlk *s = VHOST_USER_BLK(vdev); 317 318 vhost_dev_free_inflight(s->inflight); 319 } 320 321 static int vhost_user_blk_connect(DeviceState *dev, Error **errp) 322 { 323 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 324 VHostUserBlk *s = VHOST_USER_BLK(vdev); 325 int ret = 0; 326 327 if (s->connected) { 328 return 0; 329 } 330 s->connected = true; 331 332 s->dev.num_queues = s->num_queues; 333 s->dev.nvqs = s->num_queues; 334 s->dev.vqs = s->vhost_vqs; 335 s->dev.vq_index = 0; 336 s->dev.backend_features = 0; 337 338 vhost_dev_set_config_notifier(&s->dev, &blk_ops); 339 340 s->vhost_user.supports_config = true; 341 ret = vhost_dev_init(&s->dev, &s->vhost_user, VHOST_BACKEND_TYPE_USER, 0, 342 errp); 343 if (ret < 0) { 344 return ret; 345 } 346 347 /* restore vhost state */ 348 if (virtio_device_started(vdev, vdev->status)) { 349 ret = vhost_user_blk_start(vdev, errp); 350 if (ret < 0) { 351 return ret; 352 } 353 } 354 355 return 0; 356 } 357 358 static void vhost_user_blk_disconnect(DeviceState *dev) 359 { 360 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 361 VHostUserBlk *s = VHOST_USER_BLK(vdev); 362 363 if (!s->connected) { 364 return; 365 } 366 s->connected = false; 367 368 vhost_user_blk_stop(vdev); 369 370 vhost_dev_cleanup(&s->dev); 371 } 372 373 static void vhost_user_blk_chr_closed_bh(void *opaque) 374 { 375 DeviceState *dev = opaque; 376 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 377 VHostUserBlk *s = VHOST_USER_BLK(vdev); 378 379 vhost_user_blk_disconnect(dev); 380 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, vhost_user_blk_event, 381 NULL, opaque, NULL, true); 382 } 383 384 static void vhost_user_blk_event(void *opaque, QEMUChrEvent event) 385 { 386 DeviceState *dev = opaque; 387 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 388 VHostUserBlk *s = VHOST_USER_BLK(vdev); 389 Error *local_err = NULL; 390 391 switch (event) { 392 case CHR_EVENT_OPENED: 393 if (vhost_user_blk_connect(dev, &local_err) < 0) { 394 error_report_err(local_err); 395 qemu_chr_fe_disconnect(&s->chardev); 396 return; 397 } 398 break; 399 case CHR_EVENT_CLOSED: 400 if (!runstate_check(RUN_STATE_SHUTDOWN)) { 401 /* 402 * A close event may happen during a read/write, but vhost 403 * code assumes the vhost_dev remains setup, so delay the 404 * stop & clear. 405 */ 406 AioContext *ctx = qemu_get_current_aio_context(); 407 408 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, NULL, NULL, 409 NULL, NULL, false); 410 aio_bh_schedule_oneshot(ctx, vhost_user_blk_chr_closed_bh, opaque); 411 412 /* 413 * Move vhost device to the stopped state. The vhost-user device 414 * will be clean up and disconnected in BH. This can be useful in 415 * the vhost migration code. If disconnect was caught there is an 416 * option for the general vhost code to get the dev state without 417 * knowing its type (in this case vhost-user). 418 * 419 * FIXME: this is sketchy to be reaching into vhost_dev 420 * now because we are forcing something that implies we 421 * have executed vhost_dev_stop() but that won't happen 422 * until vhost_user_blk_stop() gets called from the bh. 423 * Really this state check should be tracked locally. 424 */ 425 s->dev.started = false; 426 } 427 break; 428 case CHR_EVENT_BREAK: 429 case CHR_EVENT_MUX_IN: 430 case CHR_EVENT_MUX_OUT: 431 /* Ignore */ 432 break; 433 } 434 } 435 436 static int vhost_user_blk_realize_connect(VHostUserBlk *s, Error **errp) 437 { 438 DeviceState *dev = &s->parent_obj.parent_obj; 439 int ret; 440 441 s->connected = false; 442 443 ret = qemu_chr_fe_wait_connected(&s->chardev, errp); 444 if (ret < 0) { 445 return ret; 446 } 447 448 ret = vhost_user_blk_connect(dev, errp); 449 if (ret < 0) { 450 qemu_chr_fe_disconnect(&s->chardev); 451 return ret; 452 } 453 assert(s->connected); 454 455 ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg, 456 s->parent_obj.config_len, errp); 457 if (ret < 0) { 458 qemu_chr_fe_disconnect(&s->chardev); 459 vhost_dev_cleanup(&s->dev); 460 return ret; 461 } 462 463 return 0; 464 } 465 466 static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp) 467 { 468 ERRP_GUARD(); 469 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 470 VHostUserBlk *s = VHOST_USER_BLK(vdev); 471 size_t config_size; 472 int retries; 473 int i, ret; 474 475 if (!s->chardev.chr) { 476 error_setg(errp, "chardev is mandatory"); 477 return; 478 } 479 480 if (s->num_queues == VHOST_USER_BLK_AUTO_NUM_QUEUES) { 481 s->num_queues = 1; 482 } 483 if (!s->num_queues || s->num_queues > VIRTIO_QUEUE_MAX) { 484 error_setg(errp, "invalid number of IO queues"); 485 return; 486 } 487 488 if (!s->queue_size) { 489 error_setg(errp, "queue size must be non-zero"); 490 return; 491 } 492 if (s->queue_size > VIRTQUEUE_MAX_SIZE) { 493 error_setg(errp, "queue size must not exceed %d", 494 VIRTQUEUE_MAX_SIZE); 495 return; 496 } 497 498 if (!vhost_user_init(&s->vhost_user, &s->chardev, errp)) { 499 return; 500 } 501 502 config_size = virtio_get_config_size(&virtio_blk_cfg_size_params, 503 vdev->host_features); 504 virtio_init(vdev, VIRTIO_ID_BLOCK, config_size); 505 506 s->virtqs = g_new(VirtQueue *, s->num_queues); 507 for (i = 0; i < s->num_queues; i++) { 508 s->virtqs[i] = virtio_add_queue(vdev, s->queue_size, 509 vhost_user_blk_handle_output); 510 } 511 512 s->inflight = g_new0(struct vhost_inflight, 1); 513 s->vhost_vqs = g_new0(struct vhost_virtqueue, s->num_queues); 514 515 retries = REALIZE_CONNECTION_RETRIES; 516 assert(!*errp); 517 do { 518 if (*errp) { 519 error_prepend(errp, "Reconnecting after error: "); 520 error_report_err(*errp); 521 *errp = NULL; 522 } 523 ret = vhost_user_blk_realize_connect(s, errp); 524 } while (ret < 0 && retries--); 525 526 if (ret < 0) { 527 goto virtio_err; 528 } 529 530 /* we're fully initialized, now we can operate, so add the handler */ 531 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, 532 vhost_user_blk_event, NULL, (void *)dev, 533 NULL, true); 534 return; 535 536 virtio_err: 537 g_free(s->vhost_vqs); 538 s->vhost_vqs = NULL; 539 g_free(s->inflight); 540 s->inflight = NULL; 541 for (i = 0; i < s->num_queues; i++) { 542 virtio_delete_queue(s->virtqs[i]); 543 } 544 g_free(s->virtqs); 545 virtio_cleanup(vdev); 546 vhost_user_cleanup(&s->vhost_user); 547 } 548 549 static void vhost_user_blk_device_unrealize(DeviceState *dev) 550 { 551 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 552 VHostUserBlk *s = VHOST_USER_BLK(dev); 553 int i; 554 555 virtio_set_status(vdev, 0); 556 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, NULL, 557 NULL, NULL, NULL, false); 558 vhost_dev_cleanup(&s->dev); 559 vhost_dev_free_inflight(s->inflight); 560 g_free(s->vhost_vqs); 561 s->vhost_vqs = NULL; 562 g_free(s->inflight); 563 s->inflight = NULL; 564 565 for (i = 0; i < s->num_queues; i++) { 566 virtio_delete_queue(s->virtqs[i]); 567 } 568 g_free(s->virtqs); 569 virtio_cleanup(vdev); 570 vhost_user_cleanup(&s->vhost_user); 571 } 572 573 static void vhost_user_blk_instance_init(Object *obj) 574 { 575 VHostUserBlk *s = VHOST_USER_BLK(obj); 576 577 device_add_bootindex_property(obj, &s->bootindex, "bootindex", 578 "/disk@0,0", DEVICE(obj)); 579 } 580 581 static struct vhost_dev *vhost_user_blk_get_vhost(VirtIODevice *vdev) 582 { 583 VHostUserBlk *s = VHOST_USER_BLK(vdev); 584 return &s->dev; 585 } 586 587 static const VMStateDescription vmstate_vhost_user_blk = { 588 .name = "vhost-user-blk", 589 .minimum_version_id = 1, 590 .version_id = 1, 591 .fields = (VMStateField[]) { 592 VMSTATE_VIRTIO_DEVICE, 593 VMSTATE_END_OF_LIST() 594 }, 595 }; 596 597 static Property vhost_user_blk_properties[] = { 598 DEFINE_PROP_CHR("chardev", VHostUserBlk, chardev), 599 DEFINE_PROP_UINT16("num-queues", VHostUserBlk, num_queues, 600 VHOST_USER_BLK_AUTO_NUM_QUEUES), 601 DEFINE_PROP_UINT32("queue-size", VHostUserBlk, queue_size, 128), 602 DEFINE_PROP_BIT64("config-wce", VHostUserBlk, parent_obj.host_features, 603 VIRTIO_BLK_F_CONFIG_WCE, true), 604 DEFINE_PROP_BIT64("discard", VHostUserBlk, parent_obj.host_features, 605 VIRTIO_BLK_F_DISCARD, true), 606 DEFINE_PROP_BIT64("write-zeroes", VHostUserBlk, parent_obj.host_features, 607 VIRTIO_BLK_F_WRITE_ZEROES, true), 608 DEFINE_PROP_END_OF_LIST(), 609 }; 610 611 static void vhost_user_blk_class_init(ObjectClass *klass, void *data) 612 { 613 DeviceClass *dc = DEVICE_CLASS(klass); 614 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 615 616 device_class_set_props(dc, vhost_user_blk_properties); 617 dc->vmsd = &vmstate_vhost_user_blk; 618 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 619 vdc->realize = vhost_user_blk_device_realize; 620 vdc->unrealize = vhost_user_blk_device_unrealize; 621 vdc->get_config = vhost_user_blk_update_config; 622 vdc->set_config = vhost_user_blk_set_config; 623 vdc->get_features = vhost_user_blk_get_features; 624 vdc->set_status = vhost_user_blk_set_status; 625 vdc->reset = vhost_user_blk_reset; 626 vdc->get_vhost = vhost_user_blk_get_vhost; 627 } 628 629 static const TypeInfo vhost_user_blk_info = { 630 .name = TYPE_VHOST_USER_BLK, 631 .parent = TYPE_VIRTIO_DEVICE, 632 .instance_size = sizeof(VHostUserBlk), 633 .instance_init = vhost_user_blk_instance_init, 634 .class_init = vhost_user_blk_class_init, 635 }; 636 637 static void virtio_register_types(void) 638 { 639 type_register_static(&vhost_user_blk_info); 640 } 641 642 type_init(virtio_register_types) 643