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