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/virtio-blk-common.h"
27 #include "hw/virtio/vhost.h"
28 #include "hw/virtio/vhost-user-blk.h"
29 #include "hw/virtio/virtio.h"
30 #include "hw/virtio/virtio-bus.h"
31 #include "hw/virtio/virtio-access.h"
32 #include "sysemu/sysemu.h"
33 #include "sysemu/runstate.h"
35 static const int user_feature_bits
[] = {
36 VIRTIO_BLK_F_SIZE_MAX
,
38 VIRTIO_BLK_F_GEOMETRY
,
39 VIRTIO_BLK_F_BLK_SIZE
,
40 VIRTIO_BLK_F_TOPOLOGY
,
44 VIRTIO_BLK_F_CONFIG_WCE
,
46 VIRTIO_BLK_F_WRITE_ZEROES
,
48 VIRTIO_RING_F_INDIRECT_DESC
,
49 VIRTIO_RING_F_EVENT_IDX
,
50 VIRTIO_F_NOTIFY_ON_EMPTY
,
52 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
, vdev
->config_len
);
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_FRONTEND
);
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 VirtIODevice
*vdev
= dev
->vdev
;
95 VHostUserBlk
*s
= VHOST_USER_BLK(dev
->vdev
);
96 Error
*local_err
= NULL
;
102 ret
= vhost_dev_get_config(dev
, (uint8_t *)&s
->blkcfg
,
103 vdev
->config_len
, &local_err
);
105 error_report_err(local_err
);
109 memcpy(dev
->vdev
->config
, &s
->blkcfg
, vdev
->config_len
);
110 virtio_notify_config(dev
->vdev
);
115 const VhostDevConfigOps blk_ops
= {
116 .vhost_dev_config_notifier
= vhost_user_blk_handle_config_change
,
119 static int vhost_user_blk_start(VirtIODevice
*vdev
, Error
**errp
)
121 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
122 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
123 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
126 if (!k
->set_guest_notifiers
) {
127 error_setg(errp
, "binding does not support guest notifiers");
131 ret
= vhost_dev_enable_notifiers(&s
->dev
, vdev
);
133 error_setg_errno(errp
, -ret
, "Error enabling host notifiers");
137 ret
= k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, true);
139 error_setg_errno(errp
, -ret
, "Error binding guest notifier");
140 goto err_host_notifiers
;
143 s
->dev
.acked_features
= vdev
->guest_features
;
145 ret
= vhost_dev_prepare_inflight(&s
->dev
, vdev
);
147 error_setg_errno(errp
, -ret
, "Error setting inflight format");
148 goto err_guest_notifiers
;
151 if (!s
->inflight
->addr
) {
152 ret
= vhost_dev_get_inflight(&s
->dev
, s
->queue_size
, s
->inflight
);
154 error_setg_errno(errp
, -ret
, "Error getting inflight");
155 goto err_guest_notifiers
;
159 ret
= vhost_dev_set_inflight(&s
->dev
, s
->inflight
);
161 error_setg_errno(errp
, -ret
, "Error setting inflight");
162 goto err_guest_notifiers
;
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);
173 s
->dev
.vq_index_end
= s
->dev
.nvqs
;
174 ret
= vhost_dev_start(&s
->dev
, vdev
, true);
176 error_setg_errno(errp
, -ret
, "Error starting vhost");
177 goto err_guest_notifiers
;
179 s
->started_vu
= true;
184 for (i
= 0; i
< s
->dev
.nvqs
; i
++) {
185 vhost_virtqueue_mask(&s
->dev
, vdev
, i
, true);
187 k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, false);
189 vhost_dev_disable_notifiers(&s
->dev
, vdev
);
193 static void vhost_user_blk_stop(VirtIODevice
*vdev
)
195 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
196 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
197 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
200 if (!s
->started_vu
) {
203 s
->started_vu
= false;
205 if (!k
->set_guest_notifiers
) {
209 vhost_dev_stop(&s
->dev
, vdev
, true);
211 ret
= k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, false);
213 error_report("vhost guest notifier cleanup failed: %d", ret
);
217 vhost_dev_disable_notifiers(&s
->dev
, vdev
);
220 static void vhost_user_blk_set_status(VirtIODevice
*vdev
, uint8_t status
)
222 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
223 bool should_start
= virtio_device_should_start(vdev
, status
);
224 Error
*local_err
= NULL
;
231 if (vhost_dev_is_started(&s
->dev
) == should_start
) {
236 ret
= vhost_user_blk_start(vdev
, &local_err
);
238 error_reportf_err(local_err
, "vhost-user-blk: vhost start failed: ");
239 qemu_chr_fe_disconnect(&s
->chardev
);
242 vhost_user_blk_stop(vdev
);
247 static uint64_t vhost_user_blk_get_features(VirtIODevice
*vdev
,
251 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
253 /* Turn on pre-defined features */
254 virtio_add_feature(&features
, VIRTIO_BLK_F_SIZE_MAX
);
255 virtio_add_feature(&features
, VIRTIO_BLK_F_SEG_MAX
);
256 virtio_add_feature(&features
, VIRTIO_BLK_F_GEOMETRY
);
257 virtio_add_feature(&features
, VIRTIO_BLK_F_TOPOLOGY
);
258 virtio_add_feature(&features
, VIRTIO_BLK_F_BLK_SIZE
);
259 virtio_add_feature(&features
, VIRTIO_BLK_F_FLUSH
);
260 virtio_add_feature(&features
, VIRTIO_BLK_F_RO
);
262 if (s
->num_queues
> 1) {
263 virtio_add_feature(&features
, VIRTIO_BLK_F_MQ
);
266 return vhost_get_features(&s
->dev
, user_feature_bits
, features
);
269 static void vhost_user_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
271 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
272 Error
*local_err
= NULL
;
275 if (!vdev
->start_on_kick
) {
283 if (vhost_dev_is_started(&s
->dev
)) {
287 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
288 * vhost here instead of waiting for .set_status().
290 ret
= vhost_user_blk_start(vdev
, &local_err
);
292 error_reportf_err(local_err
, "vhost-user-blk: vhost start failed: ");
293 qemu_chr_fe_disconnect(&s
->chardev
);
297 /* Kick right away to begin processing requests already in vring */
298 for (i
= 0; i
< s
->dev
.nvqs
; i
++) {
299 VirtQueue
*kick_vq
= virtio_get_queue(vdev
, i
);
301 if (!virtio_queue_get_desc_addr(vdev
, i
)) {
304 event_notifier_set(virtio_queue_get_host_notifier(kick_vq
));
308 static void vhost_user_blk_reset(VirtIODevice
*vdev
)
310 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
312 vhost_dev_free_inflight(s
->inflight
);
315 static int vhost_user_blk_connect(DeviceState
*dev
, Error
**errp
)
317 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
318 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
325 s
->dev
.num_queues
= s
->num_queues
;
326 s
->dev
.nvqs
= s
->num_queues
;
327 s
->dev
.vqs
= s
->vhost_vqs
;
329 s
->dev
.backend_features
= 0;
331 vhost_dev_set_config_notifier(&s
->dev
, &blk_ops
);
333 s
->vhost_user
.supports_config
= true;
334 ret
= vhost_dev_init(&s
->dev
, &s
->vhost_user
, VHOST_BACKEND_TYPE_USER
, 0,
342 /* restore vhost state */
343 if (virtio_device_started(vdev
, vdev
->status
)) {
344 ret
= vhost_user_blk_start(vdev
, errp
);
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
);
364 /* Re-instate the event handler for new connections */
365 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, vhost_user_blk_event
,
366 NULL
, dev
, NULL
, true);
369 static void vhost_user_blk_event(void *opaque
, QEMUChrEvent event
)
371 DeviceState
*dev
= opaque
;
372 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
373 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
374 Error
*local_err
= NULL
;
377 case CHR_EVENT_OPENED
:
378 if (vhost_user_blk_connect(dev
, &local_err
) < 0) {
379 error_report_err(local_err
);
380 qemu_chr_fe_disconnect(&s
->chardev
);
384 case CHR_EVENT_CLOSED
:
385 /* defer close until later to avoid circular close */
386 vhost_user_async_close(dev
, &s
->chardev
, &s
->dev
,
387 vhost_user_blk_disconnect
, vhost_user_blk_event
);
389 case CHR_EVENT_BREAK
:
390 case CHR_EVENT_MUX_IN
:
391 case CHR_EVENT_MUX_OUT
:
397 static int vhost_user_blk_realize_connect(VHostUserBlk
*s
, Error
**errp
)
399 DeviceState
*dev
= DEVICE(s
);
402 s
->connected
= false;
404 ret
= qemu_chr_fe_wait_connected(&s
->chardev
, errp
);
409 ret
= vhost_user_blk_connect(dev
, errp
);
411 qemu_chr_fe_disconnect(&s
->chardev
);
414 assert(s
->connected
);
416 ret
= vhost_dev_get_config(&s
->dev
, (uint8_t *)&s
->blkcfg
,
417 VIRTIO_DEVICE(s
)->config_len
, errp
);
419 qemu_chr_fe_disconnect(&s
->chardev
);
420 vhost_dev_cleanup(&s
->dev
);
427 static void vhost_user_blk_device_realize(DeviceState
*dev
, Error
**errp
)
430 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
431 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
436 if (!s
->chardev
.chr
) {
437 error_setg(errp
, "chardev is mandatory");
441 if (s
->num_queues
== VHOST_USER_BLK_AUTO_NUM_QUEUES
) {
444 if (!s
->num_queues
|| s
->num_queues
> VIRTIO_QUEUE_MAX
) {
445 error_setg(errp
, "invalid number of IO queues");
449 if (!s
->queue_size
) {
450 error_setg(errp
, "queue size must be non-zero");
453 if (s
->queue_size
> VIRTQUEUE_MAX_SIZE
) {
454 error_setg(errp
, "queue size must not exceed %d",
459 if (!vhost_user_init(&s
->vhost_user
, &s
->chardev
, errp
)) {
463 config_size
= virtio_get_config_size(&virtio_blk_cfg_size_params
,
464 vdev
->host_features
);
465 virtio_init(vdev
, VIRTIO_ID_BLOCK
, config_size
);
467 s
->virtqs
= g_new(VirtQueue
*, s
->num_queues
);
468 for (i
= 0; i
< s
->num_queues
; i
++) {
469 s
->virtqs
[i
] = virtio_add_queue(vdev
, s
->queue_size
,
470 vhost_user_blk_handle_output
);
473 s
->inflight
= g_new0(struct vhost_inflight
, 1);
474 s
->vhost_vqs
= g_new0(struct vhost_virtqueue
, s
->num_queues
);
476 retries
= VU_REALIZE_CONN_RETRIES
;
480 error_prepend(errp
, "Reconnecting after error: ");
481 error_report_err(*errp
);
484 ret
= vhost_user_blk_realize_connect(s
, errp
);
485 } while (ret
< 0 && retries
--);
491 /* we're fully initialized, now we can operate, so add the handler */
492 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
,
493 vhost_user_blk_event
, NULL
, (void *)dev
,
498 g_free(s
->vhost_vqs
);
502 for (i
= 0; i
< s
->num_queues
; i
++) {
503 virtio_delete_queue(s
->virtqs
[i
]);
506 virtio_cleanup(vdev
);
507 vhost_user_cleanup(&s
->vhost_user
);
510 static void vhost_user_blk_device_unrealize(DeviceState
*dev
)
512 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
513 VHostUserBlk
*s
= VHOST_USER_BLK(dev
);
516 virtio_set_status(vdev
, 0);
517 qemu_chr_fe_set_handlers(&s
->chardev
, NULL
, NULL
, NULL
,
518 NULL
, NULL
, NULL
, false);
519 vhost_dev_cleanup(&s
->dev
);
520 vhost_dev_free_inflight(s
->inflight
);
521 g_free(s
->vhost_vqs
);
526 for (i
= 0; i
< s
->num_queues
; i
++) {
527 virtio_delete_queue(s
->virtqs
[i
]);
530 virtio_cleanup(vdev
);
531 vhost_user_cleanup(&s
->vhost_user
);
534 static void vhost_user_blk_instance_init(Object
*obj
)
536 VHostUserBlk
*s
= VHOST_USER_BLK(obj
);
538 device_add_bootindex_property(obj
, &s
->bootindex
, "bootindex",
539 "/disk@0,0", DEVICE(obj
));
542 static struct vhost_dev
*vhost_user_blk_get_vhost(VirtIODevice
*vdev
)
544 VHostUserBlk
*s
= VHOST_USER_BLK(vdev
);
548 static const VMStateDescription vmstate_vhost_user_blk
= {
549 .name
= "vhost-user-blk",
550 .minimum_version_id
= 1,
552 .fields
= (const VMStateField
[]) {
553 VMSTATE_VIRTIO_DEVICE
,
554 VMSTATE_END_OF_LIST()
558 static Property vhost_user_blk_properties
[] = {
559 DEFINE_PROP_CHR("chardev", VHostUserBlk
, chardev
),
560 DEFINE_PROP_UINT16("num-queues", VHostUserBlk
, num_queues
,
561 VHOST_USER_BLK_AUTO_NUM_QUEUES
),
562 DEFINE_PROP_UINT32("queue-size", VHostUserBlk
, queue_size
, 128),
563 DEFINE_PROP_BIT64("config-wce", VHostUserBlk
, parent_obj
.host_features
,
564 VIRTIO_BLK_F_CONFIG_WCE
, true),
565 DEFINE_PROP_BIT64("discard", VHostUserBlk
, parent_obj
.host_features
,
566 VIRTIO_BLK_F_DISCARD
, true),
567 DEFINE_PROP_BIT64("write-zeroes", VHostUserBlk
, parent_obj
.host_features
,
568 VIRTIO_BLK_F_WRITE_ZEROES
, true),
569 DEFINE_PROP_END_OF_LIST(),
572 static void vhost_user_blk_class_init(ObjectClass
*klass
, void *data
)
574 DeviceClass
*dc
= DEVICE_CLASS(klass
);
575 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
577 device_class_set_props(dc
, vhost_user_blk_properties
);
578 dc
->vmsd
= &vmstate_vhost_user_blk
;
579 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
580 vdc
->realize
= vhost_user_blk_device_realize
;
581 vdc
->unrealize
= vhost_user_blk_device_unrealize
;
582 vdc
->get_config
= vhost_user_blk_update_config
;
583 vdc
->set_config
= vhost_user_blk_set_config
;
584 vdc
->get_features
= vhost_user_blk_get_features
;
585 vdc
->set_status
= vhost_user_blk_set_status
;
586 vdc
->reset
= vhost_user_blk_reset
;
587 vdc
->get_vhost
= vhost_user_blk_get_vhost
;
590 static const TypeInfo vhost_user_blk_info
= {
591 .name
= TYPE_VHOST_USER_BLK
,
592 .parent
= TYPE_VIRTIO_DEVICE
,
593 .instance_size
= sizeof(VHostUserBlk
),
594 .instance_init
= vhost_user_blk_instance_init
,
595 .class_init
= vhost_user_blk_class_init
,
598 static void virtio_register_types(void)
600 type_register_static(&vhost_user_blk_info
);
603 type_init(virtio_register_types
)