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/virtio/vhost.h"
26 #include "hw/virtio/vhost-user-blk.h"
27 #include "hw/virtio/virtio.h"
28 #include "hw/virtio/virtio-bus.h"
29 #include "hw/virtio/virtio-access.h"
30 #include "sysemu/sysemu.h"
31 #include "sysemu/runstate.h"
33 static const int user_feature_bits
[] = {
34 VIRTIO_BLK_F_SIZE_MAX
,
36 VIRTIO_BLK_F_GEOMETRY
,
37 VIRTIO_BLK_F_BLK_SIZE
,
38 VIRTIO_BLK_F_TOPOLOGY
,
42 VIRTIO_BLK_F_CONFIG_WCE
,
44 VIRTIO_BLK_F_WRITE_ZEROES
,
46 VIRTIO_RING_F_INDIRECT_DESC
,
47 VIRTIO_RING_F_EVENT_IDX
,
48 VIRTIO_F_NOTIFY_ON_EMPTY
,
49 VHOST_INVALID_FEATURE_BIT
52 static void vhost_user_blk_update_config(VirtIODevice
*vdev
, uint8_t *config
)
54 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
56 memcpy(config
, &s
->blkcfg
, sizeof(struct virtio_blk_config
));
59 static void vhost_user_blk_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
61 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
62 struct virtio_blk_config
*blkcfg
= (struct virtio_blk_config
*)config
;
65 if (blkcfg
->wce
== s
->blkcfg
.wce
) {
69 ret
= vhost_dev_set_config(&s
->dev
, &blkcfg
->wce
,
70 offsetof(struct virtio_blk_config
, wce
),
72 VHOST_SET_CONFIG_TYPE_MASTER
);
74 error_report("set device config space failed");
78 s
->blkcfg
.wce
= blkcfg
->wce
;
81 static int vhost_user_blk_handle_config_change(struct vhost_dev
*dev
)
84 struct virtio_blk_config blkcfg
;
85 VHostUserBlk
*s
= VHOST_USER_BLK(dev
->vdev
);
87 ret
= vhost_dev_get_config(dev
, (uint8_t *)&blkcfg
,
88 sizeof(struct virtio_blk_config
));
90 error_report("get config space failed");
94 /* valid for resize only */
95 if (blkcfg
.capacity
!= s
->blkcfg
.capacity
) {
96 s
->blkcfg
.capacity
= blkcfg
.capacity
;
97 memcpy(dev
->vdev
->config
, &s
->blkcfg
, sizeof(struct virtio_blk_config
));
98 virtio_notify_config(dev
->vdev
);
104 const VhostDevConfigOps blk_ops
= {
105 .vhost_dev_config_notifier
= vhost_user_blk_handle_config_change
,
108 static int vhost_user_blk_start(VirtIODevice
*vdev
)
110 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
111 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
112 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
115 if (!k
->set_guest_notifiers
) {
116 error_report("binding does not support guest notifiers");
120 ret
= vhost_dev_enable_notifiers(&s
->dev
, vdev
);
122 error_report("Error enabling host notifiers: %d", -ret
);
126 ret
= k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, true);
128 error_report("Error binding guest notifier: %d", -ret
);
129 goto err_host_notifiers
;
132 s
->dev
.acked_features
= vdev
->guest_features
;
134 ret
= vhost_dev_prepare_inflight(&s
->dev
, vdev
);
136 error_report("Error set inflight format: %d", -ret
);
137 goto err_guest_notifiers
;
140 if (!s
->inflight
->addr
) {
141 ret
= vhost_dev_get_inflight(&s
->dev
, s
->queue_size
, s
->inflight
);
143 error_report("Error get inflight: %d", -ret
);
144 goto err_guest_notifiers
;
148 ret
= vhost_dev_set_inflight(&s
->dev
, s
->inflight
);
150 error_report("Error set inflight: %d", -ret
);
151 goto err_guest_notifiers
;
154 ret
= vhost_dev_start(&s
->dev
, vdev
);
156 error_report("Error starting vhost: %d", -ret
);
157 goto err_guest_notifiers
;
159 s
->started_vu
= true;
161 /* guest_notifier_mask/pending not used yet, so just unmask
162 * everything here. virtio-pci will do the right thing by
163 * enabling/disabling irqfd.
165 for (i
= 0; i
< s
->dev
.nvqs
; i
++) {
166 vhost_virtqueue_mask(&s
->dev
, vdev
, i
, false);
172 k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, false);
174 vhost_dev_disable_notifiers(&s
->dev
, vdev
);
178 static void vhost_user_blk_stop(VirtIODevice
*vdev
)
180 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
181 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
182 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
185 if (!s
->started_vu
) {
188 s
->started_vu
= false;
190 if (!k
->set_guest_notifiers
) {
194 vhost_dev_stop(&s
->dev
, vdev
);
196 ret
= k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, false);
198 error_report("vhost guest notifier cleanup failed: %d", ret
);
202 vhost_dev_disable_notifiers(&s
->dev
, vdev
);
205 static void vhost_user_blk_set_status(VirtIODevice
*vdev
, uint8_t status
)
207 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
208 bool should_start
= virtio_device_started(vdev
, status
);
211 if (!vdev
->vm_running
) {
212 should_start
= false;
219 if (s
->dev
.started
== should_start
) {
224 ret
= vhost_user_blk_start(vdev
);
226 error_report("vhost-user-blk: vhost start failed: %s",
228 qemu_chr_fe_disconnect(&s
->chardev
);
231 vhost_user_blk_stop(vdev
);
236 static uint64_t vhost_user_blk_get_features(VirtIODevice
*vdev
,
240 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
242 /* Turn on pre-defined features */
243 virtio_add_feature(&features
, VIRTIO_BLK_F_SEG_MAX
);
244 virtio_add_feature(&features
, VIRTIO_BLK_F_GEOMETRY
);
245 virtio_add_feature(&features
, VIRTIO_BLK_F_TOPOLOGY
);
246 virtio_add_feature(&features
, VIRTIO_BLK_F_BLK_SIZE
);
247 virtio_add_feature(&features
, VIRTIO_BLK_F_FLUSH
);
248 virtio_add_feature(&features
, VIRTIO_BLK_F_RO
);
249 virtio_add_feature(&features
, VIRTIO_BLK_F_DISCARD
);
250 virtio_add_feature(&features
, VIRTIO_BLK_F_WRITE_ZEROES
);
253 virtio_add_feature(&features
, VIRTIO_BLK_F_CONFIG_WCE
);
255 if (s
->num_queues
> 1) {
256 virtio_add_feature(&features
, VIRTIO_BLK_F_MQ
);
259 return vhost_get_features(&s
->dev
, user_feature_bits
, features
);
262 static void vhost_user_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
264 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
267 if (!vdev
->start_on_kick
) {
275 if (s
->dev
.started
) {
279 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
280 * vhost here instead of waiting for .set_status().
282 ret
= vhost_user_blk_start(vdev
);
284 error_report("vhost-user-blk: vhost start failed: %s",
286 qemu_chr_fe_disconnect(&s
->chardev
);
290 /* Kick right away to begin processing requests already in vring */
291 for (i
= 0; i
< s
->dev
.nvqs
; i
++) {
292 VirtQueue
*kick_vq
= virtio_get_queue(vdev
, i
);
294 if (!virtio_queue_get_desc_addr(vdev
, i
)) {
297 event_notifier_set(virtio_queue_get_host_notifier(kick_vq
));
301 static void vhost_user_blk_reset(VirtIODevice
*vdev
)
303 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
305 vhost_dev_free_inflight(s
->inflight
);
308 static int vhost_user_blk_connect(DeviceState
*dev
)
310 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
311 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
319 s
->dev
.nvqs
= s
->num_queues
;
320 s
->dev
.vqs
= s
->vhost_vqs
;
322 s
->dev
.backend_features
= 0;
324 vhost_dev_set_config_notifier(&s
->dev
, &blk_ops
);
326 ret
= vhost_dev_init(&s
->dev
, &s
->vhost_user
, VHOST_BACKEND_TYPE_USER
, 0);
328 error_report("vhost-user-blk: vhost initialization failed: %s",
333 /* restore vhost state */
334 if (virtio_device_started(vdev
, vdev
->status
)) {
335 ret
= vhost_user_blk_start(vdev
);
337 error_report("vhost-user-blk: vhost start failed: %s",
346 static void vhost_user_blk_disconnect(DeviceState
*dev
)
348 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
349 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
354 s
->connected
= false;
356 vhost_user_blk_stop(vdev
);
358 vhost_dev_cleanup(&s
->dev
);
361 static void vhost_user_blk_event(void *opaque
, QEMUChrEvent event
);
363 static void vhost_user_blk_chr_closed_bh(void *opaque
)
365 DeviceState
*dev
= opaque
;
366 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
367 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
369 vhost_user_blk_disconnect(dev
);
370 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, vhost_user_blk_event
,
371 NULL
, opaque
, NULL
, true);
374 static void vhost_user_blk_event(void *opaque
, QEMUChrEvent event
)
376 DeviceState
*dev
= opaque
;
377 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
378 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
381 case CHR_EVENT_OPENED
:
382 if (vhost_user_blk_connect(dev
) < 0) {
383 qemu_chr_fe_disconnect(&s
->chardev
);
387 case CHR_EVENT_CLOSED
:
389 * A close event may happen during a read/write, but vhost
390 * code assumes the vhost_dev remains setup, so delay the
391 * stop & clear. There are two possible paths to hit this
393 * 1. When VM is in the RUN_STATE_PRELAUNCH state. The
394 * vhost_user_blk_device_realize() is a caller.
395 * 2. In tha main loop phase after VM start.
397 * For p2 the disconnect event will be delayed. We can't
398 * do the same for p1, because we are not running the loop
399 * at this moment. So just skip this step and perform
400 * disconnect in the caller function.
402 * TODO: maybe it is a good idea to make the same fix
403 * for other vhost-user devices.
405 if (runstate_is_running()) {
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
);
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 s
->dev
.started
= false;
422 case CHR_EVENT_BREAK
:
423 case CHR_EVENT_MUX_IN
:
424 case CHR_EVENT_MUX_OUT
:
430 static void vhost_user_blk_device_realize(DeviceState
*dev
, Error
**errp
)
432 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
433 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
437 if (!s
->chardev
.chr
) {
438 error_setg(errp
, "vhost-user-blk: chardev is mandatory");
442 if (s
->num_queues
== VHOST_USER_BLK_AUTO_NUM_QUEUES
) {
445 if (!s
->num_queues
|| s
->num_queues
> VIRTIO_QUEUE_MAX
) {
446 error_setg(errp
, "vhost-user-blk: invalid number of IO queues");
450 if (!s
->queue_size
) {
451 error_setg(errp
, "vhost-user-blk: queue size must be non-zero");
455 if (!vhost_user_init(&s
->vhost_user
, &s
->chardev
, errp
)) {
459 virtio_init(vdev
, "virtio-blk", VIRTIO_ID_BLOCK
,
460 sizeof(struct virtio_blk_config
));
462 s
->virtqs
= g_new(VirtQueue
*, s
->num_queues
);
463 for (i
= 0; i
< s
->num_queues
; i
++) {
464 s
->virtqs
[i
] = virtio_add_queue(vdev
, s
->queue_size
,
465 vhost_user_blk_handle_output
);
468 s
->inflight
= g_new0(struct vhost_inflight
, 1);
469 s
->vhost_vqs
= g_new0(struct vhost_virtqueue
, s
->num_queues
);
470 s
->connected
= false;
472 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, vhost_user_blk_event
,
473 NULL
, (void *)dev
, NULL
, true);
476 if (qemu_chr_fe_wait_connected(&s
->chardev
, &err
) < 0) {
477 error_report_err(err
);
481 /* check whether vhost_user_blk_connect() failed or not */
486 ret
= vhost_dev_get_config(&s
->dev
, (uint8_t *)&s
->blkcfg
,
487 sizeof(struct virtio_blk_config
));
489 error_report("vhost-user-blk: get block config failed");
493 if (s
->blkcfg
.num_queues
!= s
->num_queues
) {
494 s
->blkcfg
.num_queues
= s
->num_queues
;
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
)