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