Merge tag 'pull-request-2024-07-02' of https://gitlab.com/thuth/qemu into staging
[qemu/armbru.git] / hw / block / vhost-user-blk.c
blob9e6bbc6950daee67f65bd68966f90c50297012a5
1 /*
2 * vhost-user-blk host device
4 * Copyright(C) 2017 Intel Corporation.
6 * Authors:
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,
37 VIRTIO_BLK_F_SEG_MAX,
38 VIRTIO_BLK_F_GEOMETRY,
39 VIRTIO_BLK_F_BLK_SIZE,
40 VIRTIO_BLK_F_TOPOLOGY,
41 VIRTIO_BLK_F_MQ,
42 VIRTIO_BLK_F_RO,
43 VIRTIO_BLK_F_FLUSH,
44 VIRTIO_BLK_F_CONFIG_WCE,
45 VIRTIO_BLK_F_DISCARD,
46 VIRTIO_BLK_F_WRITE_ZEROES,
47 VIRTIO_F_VERSION_1,
48 VIRTIO_RING_F_INDIRECT_DESC,
49 VIRTIO_RING_F_EVENT_IDX,
50 VIRTIO_F_NOTIFY_ON_EMPTY,
51 VIRTIO_F_RING_PACKED,
52 VIRTIO_F_IOMMU_PLATFORM,
53 VIRTIO_F_RING_RESET,
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;
73 int ret;
75 if (blkcfg->wce == s->blkcfg.wce) {
76 return;
79 ret = vhost_dev_set_config(&s->dev, &blkcfg->wce,
80 offsetof(struct virtio_blk_config, wce),
81 sizeof(blkcfg->wce),
82 VHOST_SET_CONFIG_TYPE_FRONTEND);
83 if (ret) {
84 error_report("set device config space failed");
85 return;
88 s->blkcfg.wce = blkcfg->wce;
91 static int vhost_user_blk_handle_config_change(struct vhost_dev *dev)
93 int ret;
94 VirtIODevice *vdev = dev->vdev;
95 VHostUserBlk *s = VHOST_USER_BLK(dev->vdev);
96 Error *local_err = NULL;
98 if (!dev->started) {
99 return 0;
102 ret = vhost_dev_get_config(dev, (uint8_t *)&s->blkcfg,
103 vdev->config_len, &local_err);
104 if (ret < 0) {
105 error_report_err(local_err);
106 return ret;
109 memcpy(dev->vdev->config, &s->blkcfg, vdev->config_len);
110 virtio_notify_config(dev->vdev);
112 return 0;
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);
124 int i, ret;
126 if (!k->set_guest_notifiers) {
127 error_setg(errp, "binding does not support guest notifiers");
128 return -ENOSYS;
131 ret = vhost_dev_enable_notifiers(&s->dev, vdev);
132 if (ret < 0) {
133 error_setg_errno(errp, -ret, "Error enabling host notifiers");
134 return ret;
137 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true);
138 if (ret < 0) {
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);
146 if (ret < 0) {
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);
153 if (ret < 0) {
154 error_setg_errno(errp, -ret, "Error getting inflight");
155 goto err_guest_notifiers;
159 ret = vhost_dev_set_inflight(&s->dev, s->inflight);
160 if (ret < 0) {
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);
175 if (ret < 0) {
176 error_setg_errno(errp, -ret, "Error starting vhost");
177 goto err_guest_notifiers;
179 s->started_vu = true;
181 return ret;
183 err_guest_notifiers:
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);
188 err_host_notifiers:
189 vhost_dev_disable_notifiers(&s->dev, vdev);
190 return ret;
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);
198 int ret;
200 if (!s->started_vu) {
201 return;
203 s->started_vu = false;
205 if (!k->set_guest_notifiers) {
206 return;
209 vhost_dev_stop(&s->dev, vdev, true);
211 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
212 if (ret < 0) {
213 error_report("vhost guest notifier cleanup failed: %d", ret);
214 return;
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;
225 int ret;
227 if (!s->connected) {
228 return;
231 if (vhost_dev_is_started(&s->dev) == should_start) {
232 return;
235 if (should_start) {
236 ret = vhost_user_blk_start(vdev, &local_err);
237 if (ret < 0) {
238 error_reportf_err(local_err, "vhost-user-blk: vhost start failed: ");
239 qemu_chr_fe_disconnect(&s->chardev);
241 } else {
242 vhost_user_blk_stop(vdev);
247 static uint64_t vhost_user_blk_get_features(VirtIODevice *vdev,
248 uint64_t features,
249 Error **errp)
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;
273 int i, ret;
275 if (!vdev->start_on_kick) {
276 return;
279 if (!s->connected) {
280 return;
283 if (vhost_dev_is_started(&s->dev)) {
284 return;
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);
291 if (ret < 0) {
292 error_reportf_err(local_err, "vhost-user-blk: vhost start failed: ");
293 qemu_chr_fe_disconnect(&s->chardev);
294 return;
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)) {
302 continue;
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);
319 int ret = 0;
321 if (s->connected) {
322 return 0;
325 s->dev.num_queues = s->num_queues;
326 s->dev.nvqs = s->num_queues;
327 s->dev.vqs = s->vhost_vqs;
328 s->dev.vq_index = 0;
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,
335 errp);
336 if (ret < 0) {
337 return ret;
340 s->connected = true;
342 /* restore vhost state */
343 if (virtio_device_started(vdev, vdev->status)) {
344 ret = vhost_user_blk_start(vdev, errp);
347 return ret;
350 static void vhost_user_blk_disconnect(DeviceState *dev)
352 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
353 VHostUserBlk *s = VHOST_USER_BLK(vdev);
355 if (!s->connected) {
356 return;
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;
376 switch (event) {
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);
381 return;
383 break;
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);
388 break;
389 case CHR_EVENT_BREAK:
390 case CHR_EVENT_MUX_IN:
391 case CHR_EVENT_MUX_OUT:
392 /* Ignore */
393 break;
397 static int vhost_user_blk_realize_connect(VHostUserBlk *s, Error **errp)
399 DeviceState *dev = DEVICE(s);
400 int ret;
402 s->connected = false;
404 ret = qemu_chr_fe_wait_connected(&s->chardev, errp);
405 if (ret < 0) {
406 return ret;
409 ret = vhost_user_blk_connect(dev, errp);
410 if (ret < 0) {
411 qemu_chr_fe_disconnect(&s->chardev);
412 return ret;
414 assert(s->connected);
416 ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg,
417 VIRTIO_DEVICE(s)->config_len, errp);
418 if (ret < 0) {
419 qemu_chr_fe_disconnect(&s->chardev);
420 vhost_dev_cleanup(&s->dev);
421 return ret;
424 return 0;
427 static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
429 ERRP_GUARD();
430 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
431 VHostUserBlk *s = VHOST_USER_BLK(vdev);
432 size_t config_size;
433 int retries;
434 int i, ret;
436 if (!s->chardev.chr) {
437 error_setg(errp, "chardev is mandatory");
438 return;
441 if (s->num_queues == VHOST_USER_BLK_AUTO_NUM_QUEUES) {
442 s->num_queues = 1;
444 if (!s->num_queues || s->num_queues > VIRTIO_QUEUE_MAX) {
445 error_setg(errp, "invalid number of IO queues");
446 return;
449 if (!s->queue_size) {
450 error_setg(errp, "queue size must be non-zero");
451 return;
453 if (s->queue_size > VIRTQUEUE_MAX_SIZE) {
454 error_setg(errp, "queue size must not exceed %d",
455 VIRTQUEUE_MAX_SIZE);
456 return;
459 if (!vhost_user_init(&s->vhost_user, &s->chardev, errp)) {
460 return;
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;
477 assert(!*errp);
478 do {
479 if (*errp) {
480 error_prepend(errp, "Reconnecting after error: ");
481 error_report_err(*errp);
482 *errp = NULL;
484 ret = vhost_user_blk_realize_connect(s, errp);
485 } while (ret < 0 && retries--);
487 if (ret < 0) {
488 goto virtio_err;
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,
494 NULL, true);
495 return;
497 virtio_err:
498 g_free(s->vhost_vqs);
499 s->vhost_vqs = NULL;
500 g_free(s->inflight);
501 s->inflight = NULL;
502 for (i = 0; i < s->num_queues; i++) {
503 virtio_delete_queue(s->virtqs[i]);
505 g_free(s->virtqs);
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);
514 int i;
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);
522 s->vhost_vqs = NULL;
523 g_free(s->inflight);
524 s->inflight = NULL;
526 for (i = 0; i < s->num_queues; i++) {
527 virtio_delete_queue(s->virtqs[i]);
529 g_free(s->virtqs);
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);
545 return &s->dev;
548 static const VMStateDescription vmstate_vhost_user_blk = {
549 .name = "vhost-user-blk",
550 .minimum_version_id = 1,
551 .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)