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 if (!s
->inflight
->addr
) {
135 ret
= vhost_dev_get_inflight(&s
->dev
, s
->queue_size
, s
->inflight
);
137 error_report("Error get inflight: %d", -ret
);
138 goto err_guest_notifiers
;
142 ret
= vhost_dev_set_inflight(&s
->dev
, s
->inflight
);
144 error_report("Error set inflight: %d", -ret
);
145 goto err_guest_notifiers
;
148 ret
= vhost_dev_start(&s
->dev
, vdev
);
150 error_report("Error starting vhost: %d", -ret
);
151 goto err_guest_notifiers
;
153 s
->started_vu
= true;
155 /* guest_notifier_mask/pending not used yet, so just unmask
156 * everything here. virtio-pci will do the right thing by
157 * enabling/disabling irqfd.
159 for (i
= 0; i
< s
->dev
.nvqs
; i
++) {
160 vhost_virtqueue_mask(&s
->dev
, vdev
, i
, false);
166 k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, false);
168 vhost_dev_disable_notifiers(&s
->dev
, vdev
);
172 static void vhost_user_blk_stop(VirtIODevice
*vdev
)
174 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
175 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
176 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
179 if (!s
->started_vu
) {
182 s
->started_vu
= false;
184 if (!k
->set_guest_notifiers
) {
188 vhost_dev_stop(&s
->dev
, vdev
);
190 ret
= k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, false);
192 error_report("vhost guest notifier cleanup failed: %d", ret
);
196 vhost_dev_disable_notifiers(&s
->dev
, vdev
);
199 static void vhost_user_blk_set_status(VirtIODevice
*vdev
, uint8_t status
)
201 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
202 bool should_start
= virtio_device_started(vdev
, status
);
205 if (!vdev
->vm_running
) {
206 should_start
= false;
213 if (s
->dev
.started
== should_start
) {
218 ret
= vhost_user_blk_start(vdev
);
220 error_report("vhost-user-blk: vhost start failed: %s",
222 qemu_chr_fe_disconnect(&s
->chardev
);
225 vhost_user_blk_stop(vdev
);
230 static uint64_t vhost_user_blk_get_features(VirtIODevice
*vdev
,
234 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
236 /* Turn on pre-defined features */
237 virtio_add_feature(&features
, VIRTIO_BLK_F_SEG_MAX
);
238 virtio_add_feature(&features
, VIRTIO_BLK_F_GEOMETRY
);
239 virtio_add_feature(&features
, VIRTIO_BLK_F_TOPOLOGY
);
240 virtio_add_feature(&features
, VIRTIO_BLK_F_BLK_SIZE
);
241 virtio_add_feature(&features
, VIRTIO_BLK_F_FLUSH
);
242 virtio_add_feature(&features
, VIRTIO_BLK_F_RO
);
243 virtio_add_feature(&features
, VIRTIO_BLK_F_DISCARD
);
244 virtio_add_feature(&features
, VIRTIO_BLK_F_WRITE_ZEROES
);
247 virtio_add_feature(&features
, VIRTIO_BLK_F_CONFIG_WCE
);
249 if (s
->num_queues
> 1) {
250 virtio_add_feature(&features
, VIRTIO_BLK_F_MQ
);
253 return vhost_get_features(&s
->dev
, user_feature_bits
, features
);
256 static void vhost_user_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
258 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
261 if (!vdev
->start_on_kick
) {
269 if (s
->dev
.started
) {
273 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
274 * vhost here instead of waiting for .set_status().
276 ret
= vhost_user_blk_start(vdev
);
278 error_report("vhost-user-blk: vhost start failed: %s",
280 qemu_chr_fe_disconnect(&s
->chardev
);
284 /* Kick right away to begin processing requests already in vring */
285 for (i
= 0; i
< s
->dev
.nvqs
; i
++) {
286 VirtQueue
*kick_vq
= virtio_get_queue(vdev
, i
);
288 if (!virtio_queue_get_desc_addr(vdev
, i
)) {
291 event_notifier_set(virtio_queue_get_host_notifier(kick_vq
));
295 static void vhost_user_blk_reset(VirtIODevice
*vdev
)
297 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
299 vhost_dev_free_inflight(s
->inflight
);
302 static int vhost_user_blk_connect(DeviceState
*dev
)
304 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
305 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
313 s
->dev
.nvqs
= s
->num_queues
;
314 s
->dev
.vqs
= s
->vhost_vqs
;
316 s
->dev
.backend_features
= 0;
318 vhost_dev_set_config_notifier(&s
->dev
, &blk_ops
);
320 ret
= vhost_dev_init(&s
->dev
, &s
->vhost_user
, VHOST_BACKEND_TYPE_USER
, 0);
322 error_report("vhost-user-blk: vhost initialization failed: %s",
327 /* restore vhost state */
328 if (virtio_device_started(vdev
, vdev
->status
)) {
329 ret
= vhost_user_blk_start(vdev
);
331 error_report("vhost-user-blk: vhost start failed: %s",
340 static void vhost_user_blk_disconnect(DeviceState
*dev
)
342 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
343 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
348 s
->connected
= false;
350 vhost_user_blk_stop(vdev
);
352 vhost_dev_cleanup(&s
->dev
);
355 static void vhost_user_blk_event(void *opaque
, QEMUChrEvent event
);
357 static void vhost_user_blk_chr_closed_bh(void *opaque
)
359 DeviceState
*dev
= opaque
;
360 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
361 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
363 vhost_user_blk_disconnect(dev
);
364 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, vhost_user_blk_event
,
365 NULL
, opaque
, NULL
, true);
368 static void vhost_user_blk_event(void *opaque
, QEMUChrEvent event
)
370 DeviceState
*dev
= opaque
;
371 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
372 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
375 case CHR_EVENT_OPENED
:
376 if (vhost_user_blk_connect(dev
) < 0) {
377 qemu_chr_fe_disconnect(&s
->chardev
);
381 case CHR_EVENT_CLOSED
:
383 * A close event may happen during a read/write, but vhost
384 * code assumes the vhost_dev remains setup, so delay the
385 * stop & clear. There are two possible paths to hit this
387 * 1. When VM is in the RUN_STATE_PRELAUNCH state. The
388 * vhost_user_blk_device_realize() is a caller.
389 * 2. In tha main loop phase after VM start.
391 * For p2 the disconnect event will be delayed. We can't
392 * do the same for p1, because we are not running the loop
393 * at this moment. So just skip this step and perform
394 * disconnect in the caller function.
396 * TODO: maybe it is a good idea to make the same fix
397 * for other vhost-user devices.
399 if (runstate_is_running()) {
400 AioContext
*ctx
= qemu_get_current_aio_context();
402 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, NULL
, NULL
,
404 aio_bh_schedule_oneshot(ctx
, vhost_user_blk_chr_closed_bh
, opaque
);
408 * Move vhost device to the stopped state. The vhost-user device
409 * will be clean up and disconnected in BH. This can be useful in
410 * the vhost migration code. If disconnect was caught there is an
411 * option for the general vhost code to get the dev state without
412 * knowing its type (in this case vhost-user).
414 s
->dev
.started
= false;
416 case CHR_EVENT_BREAK
:
417 case CHR_EVENT_MUX_IN
:
418 case CHR_EVENT_MUX_OUT
:
424 static void vhost_user_blk_device_realize(DeviceState
*dev
, Error
**errp
)
426 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
427 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
431 if (!s
->chardev
.chr
) {
432 error_setg(errp
, "vhost-user-blk: chardev is mandatory");
436 if (s
->num_queues
== VHOST_USER_BLK_AUTO_NUM_QUEUES
) {
439 if (!s
->num_queues
|| s
->num_queues
> VIRTIO_QUEUE_MAX
) {
440 error_setg(errp
, "vhost-user-blk: invalid number of IO queues");
444 if (!s
->queue_size
) {
445 error_setg(errp
, "vhost-user-blk: queue size must be non-zero");
449 if (!vhost_user_init(&s
->vhost_user
, &s
->chardev
, errp
)) {
453 virtio_init(vdev
, "virtio-blk", VIRTIO_ID_BLOCK
,
454 sizeof(struct virtio_blk_config
));
456 s
->virtqs
= g_new(VirtQueue
*, s
->num_queues
);
457 for (i
= 0; i
< s
->num_queues
; i
++) {
458 s
->virtqs
[i
] = virtio_add_queue(vdev
, s
->queue_size
,
459 vhost_user_blk_handle_output
);
462 s
->inflight
= g_new0(struct vhost_inflight
, 1);
463 s
->vhost_vqs
= g_new0(struct vhost_virtqueue
, s
->num_queues
);
464 s
->connected
= false;
466 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, vhost_user_blk_event
,
467 NULL
, (void *)dev
, NULL
, true);
470 if (qemu_chr_fe_wait_connected(&s
->chardev
, &err
) < 0) {
471 error_report_err(err
);
475 /* check whether vhost_user_blk_connect() failed or not */
480 ret
= vhost_dev_get_config(&s
->dev
, (uint8_t *)&s
->blkcfg
,
481 sizeof(struct virtio_blk_config
));
483 error_report("vhost-user-blk: get block config failed");
487 if (s
->blkcfg
.num_queues
!= s
->num_queues
) {
488 s
->blkcfg
.num_queues
= s
->num_queues
;
494 g_free(s
->vhost_vqs
);
498 for (i
= 0; i
< s
->num_queues
; i
++) {
499 virtio_delete_queue(s
->virtqs
[i
]);
502 virtio_cleanup(vdev
);
503 vhost_user_cleanup(&s
->vhost_user
);
506 static void vhost_user_blk_device_unrealize(DeviceState
*dev
)
508 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
509 VHostUserBlk
*s
= VHOST_USER_BLK(dev
);
512 virtio_set_status(vdev
, 0);
513 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, NULL
,
514 NULL
, NULL
, NULL
, false);
515 vhost_dev_cleanup(&s
->dev
);
516 vhost_dev_free_inflight(s
->inflight
);
517 g_free(s
->vhost_vqs
);
522 for (i
= 0; i
< s
->num_queues
; i
++) {
523 virtio_delete_queue(s
->virtqs
[i
]);
526 virtio_cleanup(vdev
);
527 vhost_user_cleanup(&s
->vhost_user
);
530 static void vhost_user_blk_instance_init(Object
*obj
)
532 VHostUserBlk
*s
= VHOST_USER_BLK(obj
);
534 device_add_bootindex_property(obj
, &s
->bootindex
, "bootindex",
535 "/disk@0,0", DEVICE(obj
));
538 static const VMStateDescription vmstate_vhost_user_blk
= {
539 .name
= "vhost-user-blk",
540 .minimum_version_id
= 1,
542 .fields
= (VMStateField
[]) {
543 VMSTATE_VIRTIO_DEVICE
,
544 VMSTATE_END_OF_LIST()
548 static Property vhost_user_blk_properties
[] = {
549 DEFINE_PROP_CHR("chardev", VHostUserBlk
, chardev
),
550 DEFINE_PROP_UINT16("num-queues", VHostUserBlk
, num_queues
,
551 VHOST_USER_BLK_AUTO_NUM_QUEUES
),
552 DEFINE_PROP_UINT32("queue-size", VHostUserBlk
, queue_size
, 128),
553 DEFINE_PROP_BIT("config-wce", VHostUserBlk
, config_wce
, 0, true),
554 DEFINE_PROP_END_OF_LIST(),
557 static void vhost_user_blk_class_init(ObjectClass
*klass
, void *data
)
559 DeviceClass
*dc
= DEVICE_CLASS(klass
);
560 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
562 device_class_set_props(dc
, vhost_user_blk_properties
);
563 dc
->vmsd
= &vmstate_vhost_user_blk
;
564 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
565 vdc
->realize
= vhost_user_blk_device_realize
;
566 vdc
->unrealize
= vhost_user_blk_device_unrealize
;
567 vdc
->get_config
= vhost_user_blk_update_config
;
568 vdc
->set_config
= vhost_user_blk_set_config
;
569 vdc
->get_features
= vhost_user_blk_get_features
;
570 vdc
->set_status
= vhost_user_blk_set_status
;
571 vdc
->reset
= vhost_user_blk_reset
;
574 static const TypeInfo vhost_user_blk_info
= {
575 .name
= TYPE_VHOST_USER_BLK
,
576 .parent
= TYPE_VIRTIO_DEVICE
,
577 .instance_size
= sizeof(VHostUserBlk
),
578 .instance_init
= vhost_user_blk_instance_init
,
579 .class_init
= vhost_user_blk_class_init
,
582 static void virtio_register_types(void)
584 type_register_static(&vhost_user_blk_info
);
587 type_init(virtio_register_types
)