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