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
);
367 static void vhost_user_blk_chr_closed_bh(void *opaque
)
369 DeviceState
*dev
= opaque
;
370 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
371 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
373 vhost_user_blk_disconnect(dev
);
374 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, vhost_user_blk_event
,
375 NULL
, opaque
, NULL
, true);
378 static void vhost_user_blk_event(void *opaque
, QEMUChrEvent event
)
380 DeviceState
*dev
= opaque
;
381 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
382 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
385 case CHR_EVENT_OPENED
:
386 if (vhost_user_blk_connect(dev
) < 0) {
387 qemu_chr_fe_disconnect(&s
->chardev
);
391 case CHR_EVENT_CLOSED
:
393 * A close event may happen during a read/write, but vhost
394 * code assumes the vhost_dev remains setup, so delay the
395 * stop & clear. There are two possible paths to hit this
397 * 1. When VM is in the RUN_STATE_PRELAUNCH state. The
398 * vhost_user_blk_device_realize() is a caller.
399 * 2. In tha main loop phase after VM start.
401 * For p2 the disconnect event will be delayed. We can't
402 * do the same for p1, because we are not running the loop
403 * at this moment. So just skip this step and perform
404 * disconnect in the caller function.
406 * TODO: maybe it is a good idea to make the same fix
407 * for other vhost-user devices.
409 if (runstate_is_running()) {
410 AioContext
*ctx
= qemu_get_current_aio_context();
412 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, NULL
, NULL
,
414 aio_bh_schedule_oneshot(ctx
, vhost_user_blk_chr_closed_bh
, opaque
);
418 * Move vhost device to the stopped state. The vhost-user device
419 * will be clean up and disconnected in BH. This can be useful in
420 * the vhost migration code. If disconnect was caught there is an
421 * option for the general vhost code to get the dev state without
422 * knowing its type (in this case vhost-user).
424 s
->dev
.started
= false;
426 case CHR_EVENT_BREAK
:
427 case CHR_EVENT_MUX_IN
:
428 case CHR_EVENT_MUX_OUT
:
434 static void vhost_user_blk_device_realize(DeviceState
*dev
, Error
**errp
)
436 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
437 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
441 if (!s
->chardev
.chr
) {
442 error_setg(errp
, "vhost-user-blk: chardev is mandatory");
446 if (s
->num_queues
== VHOST_USER_BLK_AUTO_NUM_QUEUES
) {
449 if (!s
->num_queues
|| s
->num_queues
> VIRTIO_QUEUE_MAX
) {
450 error_setg(errp
, "vhost-user-blk: invalid number of IO queues");
454 if (!s
->queue_size
) {
455 error_setg(errp
, "vhost-user-blk: queue size must be non-zero");
459 if (!vhost_user_init(&s
->vhost_user
, &s
->chardev
, errp
)) {
463 virtio_init(vdev
, "virtio-blk", VIRTIO_ID_BLOCK
,
464 sizeof(struct virtio_blk_config
));
466 s
->virtqs
= g_new(VirtQueue
*, s
->num_queues
);
467 for (i
= 0; i
< s
->num_queues
; i
++) {
468 s
->virtqs
[i
] = virtio_add_queue(vdev
, s
->queue_size
,
469 vhost_user_blk_handle_output
);
472 s
->inflight
= g_new0(struct vhost_inflight
, 1);
473 s
->vhost_vqs
= g_new0(struct vhost_virtqueue
, s
->num_queues
);
474 s
->connected
= false;
476 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, vhost_user_blk_event
,
477 NULL
, (void *)dev
, NULL
, true);
480 if (qemu_chr_fe_wait_connected(&s
->chardev
, &err
) < 0) {
481 error_report_err(err
);
485 /* check whether vhost_user_blk_connect() failed or not */
490 ret
= vhost_dev_get_config(&s
->dev
, (uint8_t *)&s
->blkcfg
,
491 sizeof(struct virtio_blk_config
));
493 error_report("vhost-user-blk: get block config failed");
500 g_free(s
->vhost_vqs
);
504 for (i
= 0; i
< s
->num_queues
; i
++) {
505 virtio_delete_queue(s
->virtqs
[i
]);
508 virtio_cleanup(vdev
);
509 vhost_user_cleanup(&s
->vhost_user
);
512 static void vhost_user_blk_device_unrealize(DeviceState
*dev
)
514 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
515 VHostUserBlk
*s
= VHOST_USER_BLK(dev
);
518 virtio_set_status(vdev
, 0);
519 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, NULL
,
520 NULL
, NULL
, NULL
, false);
521 vhost_dev_cleanup(&s
->dev
);
522 vhost_dev_free_inflight(s
->inflight
);
523 g_free(s
->vhost_vqs
);
528 for (i
= 0; i
< s
->num_queues
; i
++) {
529 virtio_delete_queue(s
->virtqs
[i
]);
532 virtio_cleanup(vdev
);
533 vhost_user_cleanup(&s
->vhost_user
);
536 static void vhost_user_blk_instance_init(Object
*obj
)
538 VHostUserBlk
*s
= VHOST_USER_BLK(obj
);
540 device_add_bootindex_property(obj
, &s
->bootindex
, "bootindex",
541 "/disk@0,0", DEVICE(obj
));
544 static const VMStateDescription vmstate_vhost_user_blk
= {
545 .name
= "vhost-user-blk",
546 .minimum_version_id
= 1,
548 .fields
= (VMStateField
[]) {
549 VMSTATE_VIRTIO_DEVICE
,
550 VMSTATE_END_OF_LIST()
554 static Property vhost_user_blk_properties
[] = {
555 DEFINE_PROP_CHR("chardev", VHostUserBlk
, chardev
),
556 DEFINE_PROP_UINT16("num-queues", VHostUserBlk
, num_queues
,
557 VHOST_USER_BLK_AUTO_NUM_QUEUES
),
558 DEFINE_PROP_UINT32("queue-size", VHostUserBlk
, queue_size
, 128),
559 DEFINE_PROP_BIT("config-wce", VHostUserBlk
, config_wce
, 0, true),
560 DEFINE_PROP_END_OF_LIST(),
563 static void vhost_user_blk_class_init(ObjectClass
*klass
, void *data
)
565 DeviceClass
*dc
= DEVICE_CLASS(klass
);
566 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
568 device_class_set_props(dc
, vhost_user_blk_properties
);
569 dc
->vmsd
= &vmstate_vhost_user_blk
;
570 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
571 vdc
->realize
= vhost_user_blk_device_realize
;
572 vdc
->unrealize
= vhost_user_blk_device_unrealize
;
573 vdc
->get_config
= vhost_user_blk_update_config
;
574 vdc
->set_config
= vhost_user_blk_set_config
;
575 vdc
->get_features
= vhost_user_blk_get_features
;
576 vdc
->set_status
= vhost_user_blk_set_status
;
577 vdc
->reset
= vhost_user_blk_reset
;
580 static const TypeInfo vhost_user_blk_info
= {
581 .name
= TYPE_VHOST_USER_BLK
,
582 .parent
= TYPE_VIRTIO_DEVICE
,
583 .instance_size
= sizeof(VHostUserBlk
),
584 .instance_init
= vhost_user_blk_instance_init
,
585 .class_init
= vhost_user_blk_class_init
,
588 static void virtio_register_types(void)
590 type_register_static(&vhost_user_blk_info
);
593 type_init(virtio_register_types
)