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 static const int user_feature_bits
[] = {
35 VIRTIO_BLK_F_SIZE_MAX
,
37 VIRTIO_BLK_F_GEOMETRY
,
38 VIRTIO_BLK_F_BLK_SIZE
,
39 VIRTIO_BLK_F_TOPOLOGY
,
43 VIRTIO_BLK_F_CONFIG_WCE
,
45 VIRTIO_BLK_F_WRITE_ZEROES
,
47 VIRTIO_RING_F_INDIRECT_DESC
,
48 VIRTIO_RING_F_EVENT_IDX
,
49 VIRTIO_F_NOTIFY_ON_EMPTY
,
50 VHOST_INVALID_FEATURE_BIT
53 static void vhost_user_blk_update_config(VirtIODevice
*vdev
, uint8_t *config
)
55 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
57 /* Our num_queues overrides the device backend */
58 virtio_stw_p(vdev
, &s
->blkcfg
.num_queues
, s
->num_queues
);
60 memcpy(config
, &s
->blkcfg
, sizeof(struct virtio_blk_config
));
63 static void vhost_user_blk_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
65 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
66 struct virtio_blk_config
*blkcfg
= (struct virtio_blk_config
*)config
;
69 if (blkcfg
->wce
== s
->blkcfg
.wce
) {
73 ret
= vhost_dev_set_config(&s
->dev
, &blkcfg
->wce
,
74 offsetof(struct virtio_blk_config
, wce
),
76 VHOST_SET_CONFIG_TYPE_MASTER
);
78 error_report("set device config space failed");
82 s
->blkcfg
.wce
= blkcfg
->wce
;
85 static int vhost_user_blk_handle_config_change(struct vhost_dev
*dev
)
88 struct virtio_blk_config blkcfg
;
89 VHostUserBlk
*s
= VHOST_USER_BLK(dev
->vdev
);
91 ret
= vhost_dev_get_config(dev
, (uint8_t *)&blkcfg
,
92 sizeof(struct virtio_blk_config
));
94 error_report("get config space failed");
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
);
108 const VhostDevConfigOps blk_ops
= {
109 .vhost_dev_config_notifier
= vhost_user_blk_handle_config_change
,
112 static int vhost_user_blk_start(VirtIODevice
*vdev
)
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
);
119 if (!k
->set_guest_notifiers
) {
120 error_report("binding does not support guest notifiers");
124 ret
= vhost_dev_enable_notifiers(&s
->dev
, vdev
);
126 error_report("Error enabling host notifiers: %d", -ret
);
130 ret
= k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, true);
132 error_report("Error binding guest notifier: %d", -ret
);
133 goto err_host_notifiers
;
136 s
->dev
.acked_features
= vdev
->guest_features
;
138 ret
= vhost_dev_prepare_inflight(&s
->dev
, vdev
);
140 error_report("Error set inflight format: %d", -ret
);
141 goto err_guest_notifiers
;
144 if (!s
->inflight
->addr
) {
145 ret
= vhost_dev_get_inflight(&s
->dev
, s
->queue_size
, s
->inflight
);
147 error_report("Error get inflight: %d", -ret
);
148 goto err_guest_notifiers
;
152 ret
= vhost_dev_set_inflight(&s
->dev
, s
->inflight
);
154 error_report("Error set inflight: %d", -ret
);
155 goto err_guest_notifiers
;
158 ret
= vhost_dev_start(&s
->dev
, vdev
);
160 error_report("Error starting vhost: %d", -ret
);
161 goto err_guest_notifiers
;
163 s
->started_vu
= true;
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.
169 for (i
= 0; i
< s
->dev
.nvqs
; i
++) {
170 vhost_virtqueue_mask(&s
->dev
, vdev
, i
, false);
176 k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, false);
178 vhost_dev_disable_notifiers(&s
->dev
, vdev
);
182 static void vhost_user_blk_stop(VirtIODevice
*vdev
)
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
);
189 if (!s
->started_vu
) {
192 s
->started_vu
= false;
194 if (!k
->set_guest_notifiers
) {
198 vhost_dev_stop(&s
->dev
, vdev
);
200 ret
= k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, false);
202 error_report("vhost guest notifier cleanup failed: %d", ret
);
206 vhost_dev_disable_notifiers(&s
->dev
, vdev
);
209 static void vhost_user_blk_set_status(VirtIODevice
*vdev
, uint8_t status
)
211 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
212 bool should_start
= virtio_device_started(vdev
, status
);
215 if (!vdev
->vm_running
) {
216 should_start
= false;
223 if (s
->dev
.started
== should_start
) {
228 ret
= vhost_user_blk_start(vdev
);
230 error_report("vhost-user-blk: vhost start failed: %s",
232 qemu_chr_fe_disconnect(&s
->chardev
);
235 vhost_user_blk_stop(vdev
);
240 static uint64_t vhost_user_blk_get_features(VirtIODevice
*vdev
,
244 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
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
);
257 virtio_add_feature(&features
, VIRTIO_BLK_F_CONFIG_WCE
);
259 if (s
->num_queues
> 1) {
260 virtio_add_feature(&features
, VIRTIO_BLK_F_MQ
);
263 return vhost_get_features(&s
->dev
, user_feature_bits
, features
);
266 static void vhost_user_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
268 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
271 if (!vdev
->start_on_kick
) {
279 if (s
->dev
.started
) {
283 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
284 * vhost here instead of waiting for .set_status().
286 ret
= vhost_user_blk_start(vdev
);
288 error_report("vhost-user-blk: vhost start failed: %s",
290 qemu_chr_fe_disconnect(&s
->chardev
);
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
);
298 if (!virtio_queue_get_desc_addr(vdev
, i
)) {
301 event_notifier_set(virtio_queue_get_host_notifier(kick_vq
));
305 static void vhost_user_blk_reset(VirtIODevice
*vdev
)
307 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
309 vhost_dev_free_inflight(s
->inflight
);
312 static int vhost_user_blk_connect(DeviceState
*dev
)
314 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
315 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
323 s
->dev
.nvqs
= s
->num_queues
;
324 s
->dev
.vqs
= s
->vhost_vqs
;
326 s
->dev
.backend_features
= 0;
328 vhost_dev_set_config_notifier(&s
->dev
, &blk_ops
);
330 ret
= vhost_dev_init(&s
->dev
, &s
->vhost_user
, VHOST_BACKEND_TYPE_USER
, 0);
332 error_report("vhost-user-blk: vhost initialization failed: %s",
337 /* restore vhost state */
338 if (virtio_device_started(vdev
, vdev
->status
)) {
339 ret
= vhost_user_blk_start(vdev
);
341 error_report("vhost-user-blk: vhost start failed: %s",
350 static void vhost_user_blk_disconnect(DeviceState
*dev
)
352 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
353 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
358 s
->connected
= false;
360 vhost_user_blk_stop(vdev
);
362 vhost_dev_cleanup(&s
->dev
);
365 static void vhost_user_blk_event(void *opaque
, QEMUChrEvent event
,
368 static void vhost_user_blk_event_realize(void *opaque
, QEMUChrEvent event
)
370 vhost_user_blk_event(opaque
, event
, false);
373 static void vhost_user_blk_event_oper(void *opaque
, QEMUChrEvent event
)
375 vhost_user_blk_event(opaque
, event
, true);
378 static void vhost_user_blk_chr_closed_bh(void *opaque
)
380 DeviceState
*dev
= opaque
;
381 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
382 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
384 vhost_user_blk_disconnect(dev
);
385 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
,
386 vhost_user_blk_event_oper
, NULL
, opaque
, NULL
, true);
389 static void vhost_user_blk_event(void *opaque
, QEMUChrEvent event
,
392 DeviceState
*dev
= opaque
;
393 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
394 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
397 case CHR_EVENT_OPENED
:
398 if (vhost_user_blk_connect(dev
) < 0) {
399 qemu_chr_fe_disconnect(&s
->chardev
);
403 case CHR_EVENT_CLOSED
:
405 * Closing the connection should happen differently on device
406 * initialization and operation stages.
407 * On initalization, we want to re-start vhost_dev initialization
408 * from the very beginning right away when the connection is closed,
409 * so we clean up vhost_dev on each connection closing.
410 * On operation, we want to postpone vhost_dev cleanup to let the
411 * other code perform its own cleanup sequence using vhost_dev data
412 * (e.g. vhost_dev_set_log).
414 if (realized
&& !runstate_check(RUN_STATE_SHUTDOWN
)) {
416 * A close event may happen during a read/write, but vhost
417 * code assumes the vhost_dev remains setup, so delay the
420 AioContext
*ctx
= qemu_get_current_aio_context();
422 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, NULL
, NULL
,
424 aio_bh_schedule_oneshot(ctx
, vhost_user_blk_chr_closed_bh
, opaque
);
427 * Move vhost device to the stopped state. The vhost-user device
428 * will be clean up and disconnected in BH. This can be useful in
429 * the vhost migration code. If disconnect was caught there is an
430 * option for the general vhost code to get the dev state without
431 * knowing its type (in this case vhost-user).
433 s
->dev
.started
= false;
435 vhost_user_blk_disconnect(dev
);
438 case CHR_EVENT_BREAK
:
439 case CHR_EVENT_MUX_IN
:
440 case CHR_EVENT_MUX_OUT
:
446 static void vhost_user_blk_device_realize(DeviceState
*dev
, Error
**errp
)
448 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
449 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
453 if (!s
->chardev
.chr
) {
454 error_setg(errp
, "vhost-user-blk: chardev is mandatory");
458 if (s
->num_queues
== VHOST_USER_BLK_AUTO_NUM_QUEUES
) {
461 if (!s
->num_queues
|| s
->num_queues
> VIRTIO_QUEUE_MAX
) {
462 error_setg(errp
, "vhost-user-blk: invalid number of IO queues");
466 if (!s
->queue_size
) {
467 error_setg(errp
, "vhost-user-blk: queue size must be non-zero");
471 if (!vhost_user_init(&s
->vhost_user
, &s
->chardev
, errp
)) {
475 virtio_init(vdev
, "virtio-blk", VIRTIO_ID_BLOCK
,
476 sizeof(struct virtio_blk_config
));
478 s
->virtqs
= g_new(VirtQueue
*, s
->num_queues
);
479 for (i
= 0; i
< s
->num_queues
; i
++) {
480 s
->virtqs
[i
] = virtio_add_queue(vdev
, s
->queue_size
,
481 vhost_user_blk_handle_output
);
484 s
->inflight
= g_new0(struct vhost_inflight
, 1);
485 s
->vhost_vqs
= g_new0(struct vhost_virtqueue
, s
->num_queues
);
486 s
->connected
= false;
488 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
,
489 vhost_user_blk_event_realize
, NULL
, (void *)dev
,
493 if (qemu_chr_fe_wait_connected(&s
->chardev
, &err
) < 0) {
494 error_report_err(err
);
498 /* check whether vhost_user_blk_connect() failed or not */
503 ret
= vhost_dev_get_config(&s
->dev
, (uint8_t *)&s
->blkcfg
,
504 sizeof(struct virtio_blk_config
));
506 error_report("vhost-user-blk: get block config failed");
510 /* we're fully initialized, now we can operate, so change the handler */
511 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
,
512 vhost_user_blk_event_oper
, NULL
, (void *)dev
,
517 g_free(s
->vhost_vqs
);
521 for (i
= 0; i
< s
->num_queues
; i
++) {
522 virtio_delete_queue(s
->virtqs
[i
]);
525 virtio_cleanup(vdev
);
526 vhost_user_cleanup(&s
->vhost_user
);
529 static void vhost_user_blk_device_unrealize(DeviceState
*dev
)
531 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
532 VHostUserBlk
*s
= VHOST_USER_BLK(dev
);
535 virtio_set_status(vdev
, 0);
536 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, NULL
,
537 NULL
, NULL
, NULL
, false);
538 vhost_dev_cleanup(&s
->dev
);
539 vhost_dev_free_inflight(s
->inflight
);
540 g_free(s
->vhost_vqs
);
545 for (i
= 0; i
< s
->num_queues
; i
++) {
546 virtio_delete_queue(s
->virtqs
[i
]);
549 virtio_cleanup(vdev
);
550 vhost_user_cleanup(&s
->vhost_user
);
553 static void vhost_user_blk_instance_init(Object
*obj
)
555 VHostUserBlk
*s
= VHOST_USER_BLK(obj
);
557 device_add_bootindex_property(obj
, &s
->bootindex
, "bootindex",
558 "/disk@0,0", DEVICE(obj
));
561 static const VMStateDescription vmstate_vhost_user_blk
= {
562 .name
= "vhost-user-blk",
563 .minimum_version_id
= 1,
565 .fields
= (VMStateField
[]) {
566 VMSTATE_VIRTIO_DEVICE
,
567 VMSTATE_END_OF_LIST()
571 static Property vhost_user_blk_properties
[] = {
572 DEFINE_PROP_CHR("chardev", VHostUserBlk
, chardev
),
573 DEFINE_PROP_UINT16("num-queues", VHostUserBlk
, num_queues
,
574 VHOST_USER_BLK_AUTO_NUM_QUEUES
),
575 DEFINE_PROP_UINT32("queue-size", VHostUserBlk
, queue_size
, 128),
576 DEFINE_PROP_BIT("config-wce", VHostUserBlk
, config_wce
, 0, true),
577 DEFINE_PROP_END_OF_LIST(),
580 static void vhost_user_blk_class_init(ObjectClass
*klass
, void *data
)
582 DeviceClass
*dc
= DEVICE_CLASS(klass
);
583 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
585 device_class_set_props(dc
, vhost_user_blk_properties
);
586 dc
->vmsd
= &vmstate_vhost_user_blk
;
587 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
588 vdc
->realize
= vhost_user_blk_device_realize
;
589 vdc
->unrealize
= vhost_user_blk_device_unrealize
;
590 vdc
->get_config
= vhost_user_blk_update_config
;
591 vdc
->set_config
= vhost_user_blk_set_config
;
592 vdc
->get_features
= vhost_user_blk_get_features
;
593 vdc
->set_status
= vhost_user_blk_set_status
;
594 vdc
->reset
= vhost_user_blk_reset
;
597 static const TypeInfo vhost_user_blk_info
= {
598 .name
= TYPE_VHOST_USER_BLK
,
599 .parent
= TYPE_VIRTIO_DEVICE
,
600 .instance_size
= sizeof(VHostUserBlk
),
601 .instance_init
= vhost_user_blk_instance_init
,
602 .class_init
= vhost_user_blk_class_init
,
605 static void virtio_register_types(void)
607 type_register_static(&vhost_user_blk_info
);
610 type_init(virtio_register_types
)