2 * vhost-user-blk host device
4 * Copyright(C) 2017 Intel Corporation.
7 * Changpeng Liu <changpeng.liu@intel.com>
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>
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.
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"
35 #define REALIZE_CONNECTION_RETRIES 3
37 static const int user_feature_bits
[] = {
38 VIRTIO_BLK_F_SIZE_MAX
,
40 VIRTIO_BLK_F_GEOMETRY
,
41 VIRTIO_BLK_F_BLK_SIZE
,
42 VIRTIO_BLK_F_TOPOLOGY
,
46 VIRTIO_BLK_F_CONFIG_WCE
,
48 VIRTIO_BLK_F_WRITE_ZEROES
,
50 VIRTIO_RING_F_INDIRECT_DESC
,
51 VIRTIO_RING_F_EVENT_IDX
,
52 VIRTIO_F_NOTIFY_ON_EMPTY
,
54 VIRTIO_F_IOMMU_PLATFORM
,
55 VHOST_INVALID_FEATURE_BIT
58 static void vhost_user_blk_event(void *opaque
, QEMUChrEvent event
);
60 static void vhost_user_blk_update_config(VirtIODevice
*vdev
, uint8_t *config
)
62 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
64 /* Our num_queues overrides the device backend */
65 virtio_stw_p(vdev
, &s
->blkcfg
.num_queues
, s
->num_queues
);
67 memcpy(config
, &s
->blkcfg
, vdev
->config_len
);
70 static void vhost_user_blk_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
72 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
73 struct virtio_blk_config
*blkcfg
= (struct virtio_blk_config
*)config
;
76 if (blkcfg
->wce
== s
->blkcfg
.wce
) {
80 ret
= vhost_dev_set_config(&s
->dev
, &blkcfg
->wce
,
81 offsetof(struct virtio_blk_config
, wce
),
83 VHOST_SET_CONFIG_TYPE_MASTER
);
85 error_report("set device config space failed");
89 s
->blkcfg
.wce
= blkcfg
->wce
;
92 static int vhost_user_blk_handle_config_change(struct vhost_dev
*dev
)
95 struct virtio_blk_config blkcfg
;
96 VirtIODevice
*vdev
= dev
->vdev
;
97 VHostUserBlk
*s
= VHOST_USER_BLK(dev
->vdev
);
98 Error
*local_err
= NULL
;
104 ret
= vhost_dev_get_config(dev
, (uint8_t *)&blkcfg
,
105 vdev
->config_len
, &local_err
);
107 error_report_err(local_err
);
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
);
121 const VhostDevConfigOps blk_ops
= {
122 .vhost_dev_config_notifier
= vhost_user_blk_handle_config_change
,
125 static int vhost_user_blk_start(VirtIODevice
*vdev
, Error
**errp
)
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
);
132 if (!k
->set_guest_notifiers
) {
133 error_setg(errp
, "binding does not support guest notifiers");
137 ret
= vhost_dev_enable_notifiers(&s
->dev
, vdev
);
139 error_setg_errno(errp
, -ret
, "Error enabling host notifiers");
143 ret
= k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, true);
145 error_setg_errno(errp
, -ret
, "Error binding guest notifier");
146 goto err_host_notifiers
;
149 s
->dev
.acked_features
= vdev
->guest_features
;
151 ret
= vhost_dev_prepare_inflight(&s
->dev
, vdev
);
153 error_setg_errno(errp
, -ret
, "Error setting inflight format");
154 goto err_guest_notifiers
;
157 if (!s
->inflight
->addr
) {
158 ret
= vhost_dev_get_inflight(&s
->dev
, s
->queue_size
, s
->inflight
);
160 error_setg_errno(errp
, -ret
, "Error getting inflight");
161 goto err_guest_notifiers
;
165 ret
= vhost_dev_set_inflight(&s
->dev
, s
->inflight
);
167 error_setg_errno(errp
, -ret
, "Error setting inflight");
168 goto err_guest_notifiers
;
171 ret
= vhost_dev_start(&s
->dev
, vdev
);
173 error_setg_errno(errp
, -ret
, "Error starting vhost");
174 goto err_guest_notifiers
;
176 s
->started_vu
= true;
178 /* guest_notifier_mask/pending not used yet, so just unmask
179 * everything here. virtio-pci will do the right thing by
180 * enabling/disabling irqfd.
182 for (i
= 0; i
< s
->dev
.nvqs
; i
++) {
183 vhost_virtqueue_mask(&s
->dev
, vdev
, i
, false);
189 k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, false);
191 vhost_dev_disable_notifiers(&s
->dev
, vdev
);
195 static void vhost_user_blk_stop(VirtIODevice
*vdev
)
197 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
198 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
199 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
202 if (!s
->started_vu
) {
205 s
->started_vu
= false;
207 if (!k
->set_guest_notifiers
) {
211 vhost_dev_stop(&s
->dev
, vdev
);
213 ret
= k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, false);
215 error_report("vhost guest notifier cleanup failed: %d", ret
);
219 vhost_dev_disable_notifiers(&s
->dev
, vdev
);
222 static void vhost_user_blk_set_status(VirtIODevice
*vdev
, uint8_t status
)
224 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
225 bool should_start
= virtio_device_started(vdev
, status
);
226 Error
*local_err
= NULL
;
229 if (!vdev
->vm_running
) {
230 should_start
= false;
237 if (vhost_dev_is_started(&s
->dev
) == should_start
) {
242 ret
= vhost_user_blk_start(vdev
, &local_err
);
244 error_reportf_err(local_err
, "vhost-user-blk: vhost start failed: ");
245 qemu_chr_fe_disconnect(&s
->chardev
);
248 vhost_user_blk_stop(vdev
);
253 static uint64_t vhost_user_blk_get_features(VirtIODevice
*vdev
,
257 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
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
);
268 if (s
->num_queues
> 1) {
269 virtio_add_feature(&features
, VIRTIO_BLK_F_MQ
);
272 return vhost_get_features(&s
->dev
, user_feature_bits
, features
);
275 static void vhost_user_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
277 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
278 Error
*local_err
= NULL
;
281 if (!vdev
->start_on_kick
) {
289 if (vhost_dev_is_started(&s
->dev
)) {
293 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
294 * vhost here instead of waiting for .set_status().
296 ret
= vhost_user_blk_start(vdev
, &local_err
);
298 error_reportf_err(local_err
, "vhost-user-blk: vhost start failed: ");
299 qemu_chr_fe_disconnect(&s
->chardev
);
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
);
307 if (!virtio_queue_get_desc_addr(vdev
, i
)) {
310 event_notifier_set(virtio_queue_get_host_notifier(kick_vq
));
314 static void vhost_user_blk_reset(VirtIODevice
*vdev
)
316 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
318 vhost_dev_free_inflight(s
->inflight
);
321 static int vhost_user_blk_connect(DeviceState
*dev
, Error
**errp
)
323 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
324 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
332 s
->dev
.num_queues
= s
->num_queues
;
333 s
->dev
.nvqs
= s
->num_queues
;
334 s
->dev
.vqs
= s
->vhost_vqs
;
336 s
->dev
.backend_features
= 0;
338 vhost_dev_set_config_notifier(&s
->dev
, &blk_ops
);
340 s
->vhost_user
.supports_config
= true;
341 ret
= vhost_dev_init(&s
->dev
, &s
->vhost_user
, VHOST_BACKEND_TYPE_USER
, 0,
347 /* restore vhost state */
348 if (virtio_device_started(vdev
, vdev
->status
)) {
349 ret
= vhost_user_blk_start(vdev
, errp
);
358 static void vhost_user_blk_disconnect(DeviceState
*dev
)
360 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
361 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
366 s
->connected
= false;
368 vhost_user_blk_stop(vdev
);
370 vhost_dev_cleanup(&s
->dev
);
373 static void vhost_user_blk_chr_closed_bh(void *opaque
)
375 DeviceState
*dev
= opaque
;
376 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
377 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
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);
384 static void vhost_user_blk_event(void *opaque
, QEMUChrEvent event
)
386 DeviceState
*dev
= opaque
;
387 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
388 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
389 Error
*local_err
= NULL
;
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
);
399 case CHR_EVENT_CLOSED
:
400 if (!runstate_check(RUN_STATE_SHUTDOWN
)) {
402 * A close event may happen during a read/write, but vhost
403 * code assumes the vhost_dev remains setup, so delay the
406 AioContext
*ctx
= qemu_get_current_aio_context();
408 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, NULL
, NULL
,
410 aio_bh_schedule_oneshot(ctx
, vhost_user_blk_chr_closed_bh
, opaque
);
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).
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.
425 s
->dev
.started
= false;
428 case CHR_EVENT_BREAK
:
429 case CHR_EVENT_MUX_IN
:
430 case CHR_EVENT_MUX_OUT
:
436 static int vhost_user_blk_realize_connect(VHostUserBlk
*s
, Error
**errp
)
438 DeviceState
*dev
= &s
->parent_obj
.parent_obj
;
441 s
->connected
= false;
443 ret
= qemu_chr_fe_wait_connected(&s
->chardev
, errp
);
448 ret
= vhost_user_blk_connect(dev
, errp
);
450 qemu_chr_fe_disconnect(&s
->chardev
);
453 assert(s
->connected
);
455 ret
= vhost_dev_get_config(&s
->dev
, (uint8_t *)&s
->blkcfg
,
456 s
->parent_obj
.config_len
, errp
);
458 qemu_chr_fe_disconnect(&s
->chardev
);
459 vhost_dev_cleanup(&s
->dev
);
466 static void vhost_user_blk_device_realize(DeviceState
*dev
, Error
**errp
)
469 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
470 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
475 if (!s
->chardev
.chr
) {
476 error_setg(errp
, "chardev is mandatory");
480 if (s
->num_queues
== VHOST_USER_BLK_AUTO_NUM_QUEUES
) {
483 if (!s
->num_queues
|| s
->num_queues
> VIRTIO_QUEUE_MAX
) {
484 error_setg(errp
, "invalid number of IO queues");
488 if (!s
->queue_size
) {
489 error_setg(errp
, "queue size must be non-zero");
492 if (s
->queue_size
> VIRTQUEUE_MAX_SIZE
) {
493 error_setg(errp
, "queue size must not exceed %d",
498 if (!vhost_user_init(&s
->vhost_user
, &s
->chardev
, errp
)) {
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
);
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
);
512 s
->inflight
= g_new0(struct vhost_inflight
, 1);
513 s
->vhost_vqs
= g_new0(struct vhost_virtqueue
, s
->num_queues
);
515 retries
= REALIZE_CONNECTION_RETRIES
;
519 error_prepend(errp
, "Reconnecting after error: ");
520 error_report_err(*errp
);
523 ret
= vhost_user_blk_realize_connect(s
, errp
);
524 } while (ret
< 0 && retries
--);
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
,
537 g_free(s
->vhost_vqs
);
541 for (i
= 0; i
< s
->num_queues
; i
++) {
542 virtio_delete_queue(s
->virtqs
[i
]);
545 virtio_cleanup(vdev
);
546 vhost_user_cleanup(&s
->vhost_user
);
549 static void vhost_user_blk_device_unrealize(DeviceState
*dev
)
551 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
552 VHostUserBlk
*s
= VHOST_USER_BLK(dev
);
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
);
565 for (i
= 0; i
< s
->num_queues
; i
++) {
566 virtio_delete_queue(s
->virtqs
[i
]);
569 virtio_cleanup(vdev
);
570 vhost_user_cleanup(&s
->vhost_user
);
573 static void vhost_user_blk_instance_init(Object
*obj
)
575 VHostUserBlk
*s
= VHOST_USER_BLK(obj
);
577 device_add_bootindex_property(obj
, &s
->bootindex
, "bootindex",
578 "/disk@0,0", DEVICE(obj
));
581 static struct vhost_dev
*vhost_user_blk_get_vhost(VirtIODevice
*vdev
)
583 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
587 static const VMStateDescription vmstate_vhost_user_blk
= {
588 .name
= "vhost-user-blk",
589 .minimum_version_id
= 1,
591 .fields
= (VMStateField
[]) {
592 VMSTATE_VIRTIO_DEVICE
,
593 VMSTATE_END_OF_LIST()
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(),
611 static void vhost_user_blk_class_init(ObjectClass
*klass
, void *data
)
613 DeviceClass
*dc
= DEVICE_CLASS(klass
);
614 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
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
;
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
,
637 static void virtio_register_types(void)
639 type_register_static(&vhost_user_blk_info
);
642 type_init(virtio_register_types
)