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