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
,
56 VHOST_INVALID_FEATURE_BIT
59 static void vhost_user_blk_event(void *opaque
, QEMUChrEvent event
);
61 static void vhost_user_blk_update_config(VirtIODevice
*vdev
, uint8_t *config
)
63 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
65 /* Our num_queues overrides the device backend */
66 virtio_stw_p(vdev
, &s
->blkcfg
.num_queues
, s
->num_queues
);
68 memcpy(config
, &s
->blkcfg
, vdev
->config_len
);
71 static void vhost_user_blk_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
73 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
74 struct virtio_blk_config
*blkcfg
= (struct virtio_blk_config
*)config
;
77 if (blkcfg
->wce
== s
->blkcfg
.wce
) {
81 ret
= vhost_dev_set_config(&s
->dev
, &blkcfg
->wce
,
82 offsetof(struct virtio_blk_config
, wce
),
84 VHOST_SET_CONFIG_TYPE_MASTER
);
86 error_report("set device config space failed");
90 s
->blkcfg
.wce
= blkcfg
->wce
;
93 static int vhost_user_blk_handle_config_change(struct vhost_dev
*dev
)
96 struct virtio_blk_config blkcfg
;
97 VirtIODevice
*vdev
= dev
->vdev
;
98 VHostUserBlk
*s
= VHOST_USER_BLK(dev
->vdev
);
99 Error
*local_err
= NULL
;
105 ret
= vhost_dev_get_config(dev
, (uint8_t *)&blkcfg
,
106 vdev
->config_len
, &local_err
);
108 error_report_err(local_err
);
112 /* valid for resize only */
113 if (blkcfg
.capacity
!= s
->blkcfg
.capacity
) {
114 s
->blkcfg
.capacity
= blkcfg
.capacity
;
115 memcpy(dev
->vdev
->config
, &s
->blkcfg
, vdev
->config_len
);
116 virtio_notify_config(dev
->vdev
);
122 const VhostDevConfigOps blk_ops
= {
123 .vhost_dev_config_notifier
= vhost_user_blk_handle_config_change
,
126 static int vhost_user_blk_start(VirtIODevice
*vdev
, Error
**errp
)
128 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
129 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
130 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
133 if (!k
->set_guest_notifiers
) {
134 error_setg(errp
, "binding does not support guest notifiers");
138 ret
= vhost_dev_enable_notifiers(&s
->dev
, vdev
);
140 error_setg_errno(errp
, -ret
, "Error enabling host notifiers");
144 ret
= k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, true);
146 error_setg_errno(errp
, -ret
, "Error binding guest notifier");
147 goto err_host_notifiers
;
150 s
->dev
.acked_features
= vdev
->guest_features
;
152 ret
= vhost_dev_prepare_inflight(&s
->dev
, vdev
);
154 error_setg_errno(errp
, -ret
, "Error setting inflight format");
155 goto err_guest_notifiers
;
158 if (!s
->inflight
->addr
) {
159 ret
= vhost_dev_get_inflight(&s
->dev
, s
->queue_size
, s
->inflight
);
161 error_setg_errno(errp
, -ret
, "Error getting inflight");
162 goto err_guest_notifiers
;
166 ret
= vhost_dev_set_inflight(&s
->dev
, s
->inflight
);
168 error_setg_errno(errp
, -ret
, "Error setting inflight");
169 goto err_guest_notifiers
;
172 /* guest_notifier_mask/pending not used yet, so just unmask
173 * everything here. virtio-pci will do the right thing by
174 * enabling/disabling irqfd.
176 for (i
= 0; i
< s
->dev
.nvqs
; i
++) {
177 vhost_virtqueue_mask(&s
->dev
, vdev
, i
, false);
180 s
->dev
.vq_index_end
= s
->dev
.nvqs
;
181 ret
= vhost_dev_start(&s
->dev
, vdev
);
183 error_setg_errno(errp
, -ret
, "Error starting vhost");
184 goto err_guest_notifiers
;
186 s
->started_vu
= true;
191 for (i
= 0; i
< s
->dev
.nvqs
; i
++) {
192 vhost_virtqueue_mask(&s
->dev
, vdev
, i
, true);
194 k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, false);
196 vhost_dev_disable_notifiers(&s
->dev
, vdev
);
200 static void vhost_user_blk_stop(VirtIODevice
*vdev
)
202 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
203 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
204 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
207 if (!s
->started_vu
) {
210 s
->started_vu
= false;
212 if (!k
->set_guest_notifiers
) {
216 vhost_dev_stop(&s
->dev
, vdev
);
218 ret
= k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, false);
220 error_report("vhost guest notifier cleanup failed: %d", ret
);
224 vhost_dev_disable_notifiers(&s
->dev
, vdev
);
227 static void vhost_user_blk_set_status(VirtIODevice
*vdev
, uint8_t status
)
229 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
230 bool should_start
= virtio_device_should_start(vdev
, status
);
231 Error
*local_err
= NULL
;
238 if (vhost_dev_is_started(&s
->dev
) == should_start
) {
243 ret
= vhost_user_blk_start(vdev
, &local_err
);
245 error_reportf_err(local_err
, "vhost-user-blk: vhost start failed: ");
246 qemu_chr_fe_disconnect(&s
->chardev
);
249 vhost_user_blk_stop(vdev
);
254 static uint64_t vhost_user_blk_get_features(VirtIODevice
*vdev
,
258 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
260 /* Turn on pre-defined features */
261 virtio_add_feature(&features
, VIRTIO_BLK_F_SIZE_MAX
);
262 virtio_add_feature(&features
, VIRTIO_BLK_F_SEG_MAX
);
263 virtio_add_feature(&features
, VIRTIO_BLK_F_GEOMETRY
);
264 virtio_add_feature(&features
, VIRTIO_BLK_F_TOPOLOGY
);
265 virtio_add_feature(&features
, VIRTIO_BLK_F_BLK_SIZE
);
266 virtio_add_feature(&features
, VIRTIO_BLK_F_FLUSH
);
267 virtio_add_feature(&features
, VIRTIO_BLK_F_RO
);
269 if (s
->num_queues
> 1) {
270 virtio_add_feature(&features
, VIRTIO_BLK_F_MQ
);
273 return vhost_get_features(&s
->dev
, user_feature_bits
, features
);
276 static void vhost_user_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
278 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
279 Error
*local_err
= NULL
;
282 if (!vdev
->start_on_kick
) {
290 if (vhost_dev_is_started(&s
->dev
)) {
294 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
295 * vhost here instead of waiting for .set_status().
297 ret
= vhost_user_blk_start(vdev
, &local_err
);
299 error_reportf_err(local_err
, "vhost-user-blk: vhost start failed: ");
300 qemu_chr_fe_disconnect(&s
->chardev
);
304 /* Kick right away to begin processing requests already in vring */
305 for (i
= 0; i
< s
->dev
.nvqs
; i
++) {
306 VirtQueue
*kick_vq
= virtio_get_queue(vdev
, i
);
308 if (!virtio_queue_get_desc_addr(vdev
, i
)) {
311 event_notifier_set(virtio_queue_get_host_notifier(kick_vq
));
315 static void vhost_user_blk_reset(VirtIODevice
*vdev
)
317 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
319 vhost_dev_free_inflight(s
->inflight
);
322 static int vhost_user_blk_connect(DeviceState
*dev
, Error
**errp
)
324 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
325 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
333 s
->dev
.num_queues
= s
->num_queues
;
334 s
->dev
.nvqs
= s
->num_queues
;
335 s
->dev
.vqs
= s
->vhost_vqs
;
337 s
->dev
.backend_features
= 0;
339 vhost_dev_set_config_notifier(&s
->dev
, &blk_ops
);
341 s
->vhost_user
.supports_config
= true;
342 ret
= vhost_dev_init(&s
->dev
, &s
->vhost_user
, VHOST_BACKEND_TYPE_USER
, 0,
348 /* restore vhost state */
349 if (virtio_device_started(vdev
, vdev
->status
)) {
350 ret
= vhost_user_blk_start(vdev
, errp
);
359 static void vhost_user_blk_disconnect(DeviceState
*dev
)
361 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
362 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
367 s
->connected
= false;
369 vhost_user_blk_stop(vdev
);
371 vhost_dev_cleanup(&s
->dev
);
374 static void vhost_user_blk_chr_closed_bh(void *opaque
)
376 DeviceState
*dev
= opaque
;
377 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
378 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
380 vhost_user_blk_disconnect(dev
);
381 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, vhost_user_blk_event
,
382 NULL
, opaque
, NULL
, true);
385 static void vhost_user_blk_event(void *opaque
, QEMUChrEvent event
)
387 DeviceState
*dev
= opaque
;
388 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
389 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
390 Error
*local_err
= NULL
;
393 case CHR_EVENT_OPENED
:
394 if (vhost_user_blk_connect(dev
, &local_err
) < 0) {
395 error_report_err(local_err
);
396 qemu_chr_fe_disconnect(&s
->chardev
);
400 case CHR_EVENT_CLOSED
:
401 if (!runstate_check(RUN_STATE_SHUTDOWN
)) {
403 * A close event may happen during a read/write, but vhost
404 * code assumes the vhost_dev remains setup, so delay the
407 AioContext
*ctx
= qemu_get_current_aio_context();
409 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, NULL
, NULL
,
411 aio_bh_schedule_oneshot(ctx
, vhost_user_blk_chr_closed_bh
, opaque
);
414 * Move vhost device to the stopped state. The vhost-user device
415 * will be clean up and disconnected in BH. This can be useful in
416 * the vhost migration code. If disconnect was caught there is an
417 * option for the general vhost code to get the dev state without
418 * knowing its type (in this case vhost-user).
420 * FIXME: this is sketchy to be reaching into vhost_dev
421 * now because we are forcing something that implies we
422 * have executed vhost_dev_stop() but that won't happen
423 * until vhost_user_blk_stop() gets called from the bh.
424 * Really this state check should be tracked locally.
426 s
->dev
.started
= false;
429 case CHR_EVENT_BREAK
:
430 case CHR_EVENT_MUX_IN
:
431 case CHR_EVENT_MUX_OUT
:
437 static int vhost_user_blk_realize_connect(VHostUserBlk
*s
, Error
**errp
)
439 DeviceState
*dev
= &s
->parent_obj
.parent_obj
;
442 s
->connected
= false;
444 ret
= qemu_chr_fe_wait_connected(&s
->chardev
, errp
);
449 ret
= vhost_user_blk_connect(dev
, errp
);
451 qemu_chr_fe_disconnect(&s
->chardev
);
454 assert(s
->connected
);
456 ret
= vhost_dev_get_config(&s
->dev
, (uint8_t *)&s
->blkcfg
,
457 s
->parent_obj
.config_len
, errp
);
459 qemu_chr_fe_disconnect(&s
->chardev
);
460 vhost_dev_cleanup(&s
->dev
);
467 static void vhost_user_blk_device_realize(DeviceState
*dev
, Error
**errp
)
470 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
471 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
476 if (!s
->chardev
.chr
) {
477 error_setg(errp
, "chardev is mandatory");
481 if (s
->num_queues
== VHOST_USER_BLK_AUTO_NUM_QUEUES
) {
484 if (!s
->num_queues
|| s
->num_queues
> VIRTIO_QUEUE_MAX
) {
485 error_setg(errp
, "invalid number of IO queues");
489 if (!s
->queue_size
) {
490 error_setg(errp
, "queue size must be non-zero");
493 if (s
->queue_size
> VIRTQUEUE_MAX_SIZE
) {
494 error_setg(errp
, "queue size must not exceed %d",
499 if (!vhost_user_init(&s
->vhost_user
, &s
->chardev
, errp
)) {
503 config_size
= virtio_get_config_size(&virtio_blk_cfg_size_params
,
504 vdev
->host_features
);
505 virtio_init(vdev
, VIRTIO_ID_BLOCK
, config_size
);
507 s
->virtqs
= g_new(VirtQueue
*, s
->num_queues
);
508 for (i
= 0; i
< s
->num_queues
; i
++) {
509 s
->virtqs
[i
] = virtio_add_queue(vdev
, s
->queue_size
,
510 vhost_user_blk_handle_output
);
513 s
->inflight
= g_new0(struct vhost_inflight
, 1);
514 s
->vhost_vqs
= g_new0(struct vhost_virtqueue
, s
->num_queues
);
516 retries
= REALIZE_CONNECTION_RETRIES
;
520 error_prepend(errp
, "Reconnecting after error: ");
521 error_report_err(*errp
);
524 ret
= vhost_user_blk_realize_connect(s
, errp
);
525 } while (ret
< 0 && retries
--);
531 /* we're fully initialized, now we can operate, so add the handler */
532 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
,
533 vhost_user_blk_event
, NULL
, (void *)dev
,
538 g_free(s
->vhost_vqs
);
542 for (i
= 0; i
< s
->num_queues
; i
++) {
543 virtio_delete_queue(s
->virtqs
[i
]);
546 virtio_cleanup(vdev
);
547 vhost_user_cleanup(&s
->vhost_user
);
550 static void vhost_user_blk_device_unrealize(DeviceState
*dev
)
552 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
553 VHostUserBlk
*s
= VHOST_USER_BLK(dev
);
556 virtio_set_status(vdev
, 0);
557 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, NULL
,
558 NULL
, NULL
, NULL
, false);
559 vhost_dev_cleanup(&s
->dev
);
560 vhost_dev_free_inflight(s
->inflight
);
561 g_free(s
->vhost_vqs
);
566 for (i
= 0; i
< s
->num_queues
; i
++) {
567 virtio_delete_queue(s
->virtqs
[i
]);
570 virtio_cleanup(vdev
);
571 vhost_user_cleanup(&s
->vhost_user
);
574 static void vhost_user_blk_instance_init(Object
*obj
)
576 VHostUserBlk
*s
= VHOST_USER_BLK(obj
);
578 device_add_bootindex_property(obj
, &s
->bootindex
, "bootindex",
579 "/disk@0,0", DEVICE(obj
));
582 static struct vhost_dev
*vhost_user_blk_get_vhost(VirtIODevice
*vdev
)
584 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
588 static const VMStateDescription vmstate_vhost_user_blk
= {
589 .name
= "vhost-user-blk",
590 .minimum_version_id
= 1,
592 .fields
= (VMStateField
[]) {
593 VMSTATE_VIRTIO_DEVICE
,
594 VMSTATE_END_OF_LIST()
598 static Property vhost_user_blk_properties
[] = {
599 DEFINE_PROP_CHR("chardev", VHostUserBlk
, chardev
),
600 DEFINE_PROP_UINT16("num-queues", VHostUserBlk
, num_queues
,
601 VHOST_USER_BLK_AUTO_NUM_QUEUES
),
602 DEFINE_PROP_UINT32("queue-size", VHostUserBlk
, queue_size
, 128),
603 DEFINE_PROP_BIT64("config-wce", VHostUserBlk
, parent_obj
.host_features
,
604 VIRTIO_BLK_F_CONFIG_WCE
, true),
605 DEFINE_PROP_BIT64("discard", VHostUserBlk
, parent_obj
.host_features
,
606 VIRTIO_BLK_F_DISCARD
, true),
607 DEFINE_PROP_BIT64("write-zeroes", VHostUserBlk
, parent_obj
.host_features
,
608 VIRTIO_BLK_F_WRITE_ZEROES
, true),
609 DEFINE_PROP_END_OF_LIST(),
612 static void vhost_user_blk_class_init(ObjectClass
*klass
, void *data
)
614 DeviceClass
*dc
= DEVICE_CLASS(klass
);
615 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
617 device_class_set_props(dc
, vhost_user_blk_properties
);
618 dc
->vmsd
= &vmstate_vhost_user_blk
;
619 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
620 vdc
->realize
= vhost_user_blk_device_realize
;
621 vdc
->unrealize
= vhost_user_blk_device_unrealize
;
622 vdc
->get_config
= vhost_user_blk_update_config
;
623 vdc
->set_config
= vhost_user_blk_set_config
;
624 vdc
->get_features
= vhost_user_blk_get_features
;
625 vdc
->set_status
= vhost_user_blk_set_status
;
626 vdc
->reset
= vhost_user_blk_reset
;
627 vdc
->get_vhost
= vhost_user_blk_get_vhost
;
630 static const TypeInfo vhost_user_blk_info
= {
631 .name
= TYPE_VHOST_USER_BLK
,
632 .parent
= TYPE_VIRTIO_DEVICE
,
633 .instance_size
= sizeof(VHostUserBlk
),
634 .instance_init
= vhost_user_blk_instance_init
,
635 .class_init
= vhost_user_blk_class_init
,
638 static void virtio_register_types(void)
640 type_register_static(&vhost_user_blk_info
);
643 type_init(virtio_register_types
)