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/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"
34 #define REALIZE_CONNECTION_RETRIES 3
36 static const int user_feature_bits
[] = {
37 VIRTIO_BLK_F_SIZE_MAX
,
39 VIRTIO_BLK_F_GEOMETRY
,
40 VIRTIO_BLK_F_BLK_SIZE
,
41 VIRTIO_BLK_F_TOPOLOGY
,
45 VIRTIO_BLK_F_CONFIG_WCE
,
47 VIRTIO_BLK_F_WRITE_ZEROES
,
49 VIRTIO_RING_F_INDIRECT_DESC
,
50 VIRTIO_RING_F_EVENT_IDX
,
51 VIRTIO_F_NOTIFY_ON_EMPTY
,
53 VIRTIO_F_IOMMU_PLATFORM
,
54 VHOST_INVALID_FEATURE_BIT
57 static void vhost_user_blk_event(void *opaque
, QEMUChrEvent event
);
59 static void vhost_user_blk_update_config(VirtIODevice
*vdev
, uint8_t *config
)
61 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
63 /* Our num_queues overrides the device backend */
64 virtio_stw_p(vdev
, &s
->blkcfg
.num_queues
, s
->num_queues
);
66 memcpy(config
, &s
->blkcfg
, sizeof(struct virtio_blk_config
));
69 static void vhost_user_blk_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
71 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
72 struct virtio_blk_config
*blkcfg
= (struct virtio_blk_config
*)config
;
75 if (blkcfg
->wce
== s
->blkcfg
.wce
) {
79 ret
= vhost_dev_set_config(&s
->dev
, &blkcfg
->wce
,
80 offsetof(struct virtio_blk_config
, wce
),
82 VHOST_SET_CONFIG_TYPE_MASTER
);
84 error_report("set device config space failed");
88 s
->blkcfg
.wce
= blkcfg
->wce
;
91 static int vhost_user_blk_handle_config_change(struct vhost_dev
*dev
)
94 struct virtio_blk_config blkcfg
;
95 VHostUserBlk
*s
= VHOST_USER_BLK(dev
->vdev
);
96 Error
*local_err
= NULL
;
98 ret
= vhost_dev_get_config(dev
, (uint8_t *)&blkcfg
,
99 sizeof(struct virtio_blk_config
),
102 error_report_err(local_err
);
106 /* valid for resize only */
107 if (blkcfg
.capacity
!= s
->blkcfg
.capacity
) {
108 s
->blkcfg
.capacity
= blkcfg
.capacity
;
109 memcpy(dev
->vdev
->config
, &s
->blkcfg
, sizeof(struct virtio_blk_config
));
110 virtio_notify_config(dev
->vdev
);
116 const VhostDevConfigOps blk_ops
= {
117 .vhost_dev_config_notifier
= vhost_user_blk_handle_config_change
,
120 static int vhost_user_blk_start(VirtIODevice
*vdev
, Error
**errp
)
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
);
127 if (!k
->set_guest_notifiers
) {
128 error_setg(errp
, "binding does not support guest notifiers");
132 ret
= vhost_dev_enable_notifiers(&s
->dev
, vdev
);
134 error_setg_errno(errp
, -ret
, "Error enabling host notifiers");
138 ret
= k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, true);
140 error_setg_errno(errp
, -ret
, "Error binding guest notifier");
141 goto err_host_notifiers
;
144 s
->dev
.acked_features
= vdev
->guest_features
;
146 ret
= vhost_dev_prepare_inflight(&s
->dev
, vdev
);
148 error_setg_errno(errp
, -ret
, "Error setting inflight format");
149 goto err_guest_notifiers
;
152 if (!s
->inflight
->addr
) {
153 ret
= vhost_dev_get_inflight(&s
->dev
, s
->queue_size
, s
->inflight
);
155 error_setg_errno(errp
, -ret
, "Error getting inflight");
156 goto err_guest_notifiers
;
160 ret
= vhost_dev_set_inflight(&s
->dev
, s
->inflight
);
162 error_setg_errno(errp
, -ret
, "Error setting inflight");
163 goto err_guest_notifiers
;
166 ret
= vhost_dev_start(&s
->dev
, vdev
);
168 error_setg_errno(errp
, -ret
, "Error starting vhost");
169 goto err_guest_notifiers
;
171 s
->started_vu
= true;
173 /* guest_notifier_mask/pending not used yet, so just unmask
174 * everything here. virtio-pci will do the right thing by
175 * enabling/disabling irqfd.
177 for (i
= 0; i
< s
->dev
.nvqs
; i
++) {
178 vhost_virtqueue_mask(&s
->dev
, vdev
, i
, false);
184 k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, false);
186 vhost_dev_disable_notifiers(&s
->dev
, vdev
);
190 static void vhost_user_blk_stop(VirtIODevice
*vdev
)
192 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
193 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
194 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
197 if (!s
->started_vu
) {
200 s
->started_vu
= false;
202 if (!k
->set_guest_notifiers
) {
206 vhost_dev_stop(&s
->dev
, vdev
);
208 ret
= k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, false);
210 error_report("vhost guest notifier cleanup failed: %d", ret
);
214 vhost_dev_disable_notifiers(&s
->dev
, vdev
);
217 static void vhost_user_blk_set_status(VirtIODevice
*vdev
, uint8_t status
)
219 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
220 bool should_start
= virtio_device_started(vdev
, status
);
221 Error
*local_err
= NULL
;
224 if (!vdev
->vm_running
) {
225 should_start
= false;
232 if (s
->dev
.started
== should_start
) {
237 ret
= vhost_user_blk_start(vdev
, &local_err
);
239 error_reportf_err(local_err
, "vhost-user-blk: vhost start failed: ");
240 qemu_chr_fe_disconnect(&s
->chardev
);
243 vhost_user_blk_stop(vdev
);
248 static uint64_t vhost_user_blk_get_features(VirtIODevice
*vdev
,
252 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
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 virtio_add_feature(&features
, VIRTIO_BLK_F_DISCARD
);
263 virtio_add_feature(&features
, VIRTIO_BLK_F_WRITE_ZEROES
);
266 virtio_add_feature(&features
, VIRTIO_BLK_F_CONFIG_WCE
);
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 (s
->dev
.started
) {
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 ret
= vhost_dev_init(&s
->dev
, &s
->vhost_user
, VHOST_BACKEND_TYPE_USER
, 0,
346 /* restore vhost state */
347 if (virtio_device_started(vdev
, vdev
->status
)) {
348 ret
= vhost_user_blk_start(vdev
, errp
);
357 static void vhost_user_blk_disconnect(DeviceState
*dev
)
359 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
360 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
365 s
->connected
= false;
367 vhost_user_blk_stop(vdev
);
369 vhost_dev_cleanup(&s
->dev
);
372 static void vhost_user_blk_chr_closed_bh(void *opaque
)
374 DeviceState
*dev
= opaque
;
375 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
376 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
378 vhost_user_blk_disconnect(dev
);
379 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, vhost_user_blk_event
,
380 NULL
, opaque
, NULL
, true);
383 static void vhost_user_blk_event(void *opaque
, QEMUChrEvent event
)
385 DeviceState
*dev
= opaque
;
386 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
387 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
388 Error
*local_err
= NULL
;
391 case CHR_EVENT_OPENED
:
392 if (vhost_user_blk_connect(dev
, &local_err
) < 0) {
393 error_report_err(local_err
);
394 qemu_chr_fe_disconnect(&s
->chardev
);
398 case CHR_EVENT_CLOSED
:
399 if (!runstate_check(RUN_STATE_SHUTDOWN
)) {
401 * A close event may happen during a read/write, but vhost
402 * code assumes the vhost_dev remains setup, so delay the
405 AioContext
*ctx
= qemu_get_current_aio_context();
407 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, NULL
, NULL
,
409 aio_bh_schedule_oneshot(ctx
, vhost_user_blk_chr_closed_bh
, opaque
);
412 * Move vhost device to the stopped state. The vhost-user device
413 * will be clean up and disconnected in BH. This can be useful in
414 * the vhost migration code. If disconnect was caught there is an
415 * option for the general vhost code to get the dev state without
416 * knowing its type (in this case vhost-user).
418 s
->dev
.started
= false;
421 case CHR_EVENT_BREAK
:
422 case CHR_EVENT_MUX_IN
:
423 case CHR_EVENT_MUX_OUT
:
429 static int vhost_user_blk_realize_connect(VHostUserBlk
*s
, Error
**errp
)
431 DeviceState
*dev
= &s
->parent_obj
.parent_obj
;
434 s
->connected
= false;
436 ret
= qemu_chr_fe_wait_connected(&s
->chardev
, errp
);
441 ret
= vhost_user_blk_connect(dev
, errp
);
443 qemu_chr_fe_disconnect(&s
->chardev
);
446 assert(s
->connected
);
448 ret
= vhost_dev_get_config(&s
->dev
, (uint8_t *)&s
->blkcfg
,
449 sizeof(struct virtio_blk_config
), errp
);
451 qemu_chr_fe_disconnect(&s
->chardev
);
452 vhost_dev_cleanup(&s
->dev
);
459 static void vhost_user_blk_device_realize(DeviceState
*dev
, Error
**errp
)
462 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
463 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
467 if (!s
->chardev
.chr
) {
468 error_setg(errp
, "chardev is mandatory");
472 if (s
->num_queues
== VHOST_USER_BLK_AUTO_NUM_QUEUES
) {
475 if (!s
->num_queues
|| s
->num_queues
> VIRTIO_QUEUE_MAX
) {
476 error_setg(errp
, "invalid number of IO queues");
480 if (!s
->queue_size
) {
481 error_setg(errp
, "queue size must be non-zero");
484 if (s
->queue_size
> VIRTQUEUE_MAX_SIZE
) {
485 error_setg(errp
, "queue size must not exceed %d",
490 if (!vhost_user_init(&s
->vhost_user
, &s
->chardev
, errp
)) {
494 virtio_init(vdev
, VIRTIO_ID_BLOCK
,
495 sizeof(struct virtio_blk_config
));
497 s
->virtqs
= g_new(VirtQueue
*, s
->num_queues
);
498 for (i
= 0; i
< s
->num_queues
; i
++) {
499 s
->virtqs
[i
] = virtio_add_queue(vdev
, s
->queue_size
,
500 vhost_user_blk_handle_output
);
503 s
->inflight
= g_new0(struct vhost_inflight
, 1);
504 s
->vhost_vqs
= g_new0(struct vhost_virtqueue
, s
->num_queues
);
506 retries
= REALIZE_CONNECTION_RETRIES
;
510 error_prepend(errp
, "Reconnecting after error: ");
511 error_report_err(*errp
);
514 ret
= vhost_user_blk_realize_connect(s
, errp
);
515 } while (ret
< 0 && retries
--);
521 /* we're fully initialized, now we can operate, so add the handler */
522 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
,
523 vhost_user_blk_event
, NULL
, (void *)dev
,
528 g_free(s
->vhost_vqs
);
532 for (i
= 0; i
< s
->num_queues
; i
++) {
533 virtio_delete_queue(s
->virtqs
[i
]);
536 virtio_cleanup(vdev
);
537 vhost_user_cleanup(&s
->vhost_user
);
540 static void vhost_user_blk_device_unrealize(DeviceState
*dev
)
542 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
543 VHostUserBlk
*s
= VHOST_USER_BLK(dev
);
546 virtio_set_status(vdev
, 0);
547 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, NULL
,
548 NULL
, NULL
, NULL
, false);
549 vhost_dev_cleanup(&s
->dev
);
550 vhost_dev_free_inflight(s
->inflight
);
551 g_free(s
->vhost_vqs
);
556 for (i
= 0; i
< s
->num_queues
; i
++) {
557 virtio_delete_queue(s
->virtqs
[i
]);
560 virtio_cleanup(vdev
);
561 vhost_user_cleanup(&s
->vhost_user
);
564 static void vhost_user_blk_instance_init(Object
*obj
)
566 VHostUserBlk
*s
= VHOST_USER_BLK(obj
);
568 device_add_bootindex_property(obj
, &s
->bootindex
, "bootindex",
569 "/disk@0,0", DEVICE(obj
));
572 static struct vhost_dev
*vhost_user_blk_get_vhost(VirtIODevice
*vdev
)
574 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
578 static const VMStateDescription vmstate_vhost_user_blk
= {
579 .name
= "vhost-user-blk",
580 .minimum_version_id
= 1,
582 .fields
= (VMStateField
[]) {
583 VMSTATE_VIRTIO_DEVICE
,
584 VMSTATE_END_OF_LIST()
588 static Property vhost_user_blk_properties
[] = {
589 DEFINE_PROP_CHR("chardev", VHostUserBlk
, chardev
),
590 DEFINE_PROP_UINT16("num-queues", VHostUserBlk
, num_queues
,
591 VHOST_USER_BLK_AUTO_NUM_QUEUES
),
592 DEFINE_PROP_UINT32("queue-size", VHostUserBlk
, queue_size
, 128),
593 DEFINE_PROP_BIT("config-wce", VHostUserBlk
, config_wce
, 0, true),
594 DEFINE_PROP_END_OF_LIST(),
597 static void vhost_user_blk_class_init(ObjectClass
*klass
, void *data
)
599 DeviceClass
*dc
= DEVICE_CLASS(klass
);
600 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
602 device_class_set_props(dc
, vhost_user_blk_properties
);
603 dc
->vmsd
= &vmstate_vhost_user_blk
;
604 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
605 vdc
->realize
= vhost_user_blk_device_realize
;
606 vdc
->unrealize
= vhost_user_blk_device_unrealize
;
607 vdc
->get_config
= vhost_user_blk_update_config
;
608 vdc
->set_config
= vhost_user_blk_set_config
;
609 vdc
->get_features
= vhost_user_blk_get_features
;
610 vdc
->set_status
= vhost_user_blk_set_status
;
611 vdc
->reset
= vhost_user_blk_reset
;
612 vdc
->get_vhost
= vhost_user_blk_get_vhost
;
615 static const TypeInfo vhost_user_blk_info
= {
616 .name
= TYPE_VHOST_USER_BLK
,
617 .parent
= TYPE_VIRTIO_DEVICE
,
618 .instance_size
= sizeof(VHostUserBlk
),
619 .instance_init
= vhost_user_blk_instance_init
,
620 .class_init
= vhost_user_blk_class_init
,
623 static void virtio_register_types(void)
625 type_register_static(&vhost_user_blk_info
);
628 type_init(virtio_register_types
)