xref: /openbmc/qemu/hw/block/vhost-user-blk.c (revision ef7e76a2)
1 /*
2  * vhost-user-blk host device
3  *
4  * Copyright(C) 2017 Intel Corporation.
5  *
6  * Authors:
7  *  Changpeng Liu <changpeng.liu@intel.com>
8  *
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>
13  *
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.
16  *
17  */
18 
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"
34 
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     VIRTIO_F_IN_ORDER,
55     VIRTIO_F_NOTIFICATION_DATA,
56     VHOST_INVALID_FEATURE_BIT
57 };
58 
59 static void vhost_user_blk_event(void *opaque, QEMUChrEvent event);
60 
61 static void vhost_user_blk_update_config(VirtIODevice *vdev, uint8_t *config)
62 {
63     VHostUserBlk *s = VHOST_USER_BLK(vdev);
64 
65     /* Our num_queues overrides the device backend */
66     virtio_stw_p(vdev, &s->blkcfg.num_queues, s->num_queues);
67 
68     memcpy(config, &s->blkcfg, vdev->config_len);
69 }
70 
71 static void vhost_user_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
72 {
73     VHostUserBlk *s = VHOST_USER_BLK(vdev);
74     struct virtio_blk_config *blkcfg = (struct virtio_blk_config *)config;
75     int ret;
76 
77     if (blkcfg->wce == s->blkcfg.wce) {
78         return;
79     }
80 
81     ret = vhost_dev_set_config(&s->dev, &blkcfg->wce,
82                                offsetof(struct virtio_blk_config, wce),
83                                sizeof(blkcfg->wce),
84                                VHOST_SET_CONFIG_TYPE_FRONTEND);
85     if (ret) {
86         error_report("set device config space failed");
87         return;
88     }
89 
90     s->blkcfg.wce = blkcfg->wce;
91 }
92 
93 static int vhost_user_blk_sync_config(DeviceState *dev, Error **errp)
94 {
95     int ret;
96     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
97     VHostUserBlk *s = VHOST_USER_BLK(vdev);
98 
99     ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg,
100                                vdev->config_len, errp);
101     if (ret < 0) {
102         return ret;
103     }
104 
105     memcpy(vdev->config, &s->blkcfg, vdev->config_len);
106     virtio_notify_config(vdev);
107 
108     return 0;
109 }
110 
111 static int vhost_user_blk_handle_config_change(struct vhost_dev *dev)
112 {
113     int ret;
114     Error *local_err = NULL;
115 
116     if (!dev->started) {
117         return 0;
118     }
119 
120     ret = vhost_user_blk_sync_config(DEVICE(dev->vdev), &local_err);
121     if (ret < 0) {
122         error_report_err(local_err);
123         return ret;
124     }
125 
126     return 0;
127 }
128 
129 const VhostDevConfigOps blk_ops = {
130     .vhost_dev_config_notifier = vhost_user_blk_handle_config_change,
131 };
132 
133 static int vhost_user_blk_start(VirtIODevice *vdev, Error **errp)
134 {
135     VHostUserBlk *s = VHOST_USER_BLK(vdev);
136     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
137     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
138     int i, ret;
139 
140     if (!k->set_guest_notifiers) {
141         error_setg(errp, "binding does not support guest notifiers");
142         return -ENOSYS;
143     }
144 
145     ret = vhost_dev_enable_notifiers(&s->dev, vdev);
146     if (ret < 0) {
147         error_setg_errno(errp, -ret, "Error enabling host notifiers");
148         return ret;
149     }
150 
151     ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true);
152     if (ret < 0) {
153         error_setg_errno(errp, -ret, "Error binding guest notifier");
154         goto err_host_notifiers;
155     }
156 
157     s->dev.acked_features = vdev->guest_features;
158 
159     ret = vhost_dev_prepare_inflight(&s->dev, vdev);
160     if (ret < 0) {
161         error_setg_errno(errp, -ret, "Error setting inflight format");
162         goto err_guest_notifiers;
163     }
164 
165     if (!s->inflight->addr) {
166         ret = vhost_dev_get_inflight(&s->dev, s->queue_size, s->inflight);
167         if (ret < 0) {
168             error_setg_errno(errp, -ret, "Error getting inflight");
169             goto err_guest_notifiers;
170         }
171     }
172 
173     ret = vhost_dev_set_inflight(&s->dev, s->inflight);
174     if (ret < 0) {
175         error_setg_errno(errp, -ret, "Error setting inflight");
176         goto err_guest_notifiers;
177     }
178 
179     /* guest_notifier_mask/pending not used yet, so just unmask
180      * everything here. virtio-pci will do the right thing by
181      * enabling/disabling irqfd.
182      */
183     for (i = 0; i < s->dev.nvqs; i++) {
184         vhost_virtqueue_mask(&s->dev, vdev, i, false);
185     }
186 
187     s->dev.vq_index_end = s->dev.nvqs;
188     ret = vhost_dev_start(&s->dev, vdev, true);
189     if (ret < 0) {
190         error_setg_errno(errp, -ret, "Error starting vhost");
191         goto err_guest_notifiers;
192     }
193     s->started_vu = true;
194 
195     return ret;
196 
197 err_guest_notifiers:
198     for (i = 0; i < s->dev.nvqs; i++) {
199         vhost_virtqueue_mask(&s->dev, vdev, i, true);
200     }
201     k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
202 err_host_notifiers:
203     vhost_dev_disable_notifiers(&s->dev, vdev);
204     return ret;
205 }
206 
207 static void vhost_user_blk_stop(VirtIODevice *vdev)
208 {
209     VHostUserBlk *s = VHOST_USER_BLK(vdev);
210     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
211     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
212     int ret;
213 
214     if (!s->started_vu) {
215         return;
216     }
217     s->started_vu = false;
218 
219     if (!k->set_guest_notifiers) {
220         return;
221     }
222 
223     vhost_dev_stop(&s->dev, vdev, true);
224 
225     ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
226     if (ret < 0) {
227         error_report("vhost guest notifier cleanup failed: %d", ret);
228         return;
229     }
230 
231     vhost_dev_disable_notifiers(&s->dev, vdev);
232 }
233 
234 static void vhost_user_blk_set_status(VirtIODevice *vdev, uint8_t status)
235 {
236     VHostUserBlk *s = VHOST_USER_BLK(vdev);
237     bool should_start = virtio_device_should_start(vdev, status);
238     Error *local_err = NULL;
239     int ret;
240 
241     if (!s->connected) {
242         return;
243     }
244 
245     if (vhost_dev_is_started(&s->dev) == should_start) {
246         return;
247     }
248 
249     if (should_start) {
250         ret = vhost_user_blk_start(vdev, &local_err);
251         if (ret < 0) {
252             error_reportf_err(local_err, "vhost-user-blk: vhost start failed: ");
253             qemu_chr_fe_disconnect(&s->chardev);
254         }
255     } else {
256         vhost_user_blk_stop(vdev);
257     }
258 
259 }
260 
261 static uint64_t vhost_user_blk_get_features(VirtIODevice *vdev,
262                                             uint64_t features,
263                                             Error **errp)
264 {
265     VHostUserBlk *s = VHOST_USER_BLK(vdev);
266 
267     /* Turn on pre-defined features */
268     virtio_add_feature(&features, VIRTIO_BLK_F_SIZE_MAX);
269     virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
270     virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY);
271     virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY);
272     virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
273     virtio_add_feature(&features, VIRTIO_BLK_F_FLUSH);
274     virtio_add_feature(&features, VIRTIO_BLK_F_RO);
275 
276     if (s->num_queues > 1) {
277         virtio_add_feature(&features, VIRTIO_BLK_F_MQ);
278     }
279 
280     return vhost_get_features(&s->dev, user_feature_bits, features);
281 }
282 
283 static void vhost_user_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
284 {
285     VHostUserBlk *s = VHOST_USER_BLK(vdev);
286     Error *local_err = NULL;
287     int i, ret;
288 
289     if (!vdev->start_on_kick) {
290         return;
291     }
292 
293     if (!s->connected) {
294         return;
295     }
296 
297     if (vhost_dev_is_started(&s->dev)) {
298         return;
299     }
300 
301     /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
302      * vhost here instead of waiting for .set_status().
303      */
304     ret = vhost_user_blk_start(vdev, &local_err);
305     if (ret < 0) {
306         error_reportf_err(local_err, "vhost-user-blk: vhost start failed: ");
307         qemu_chr_fe_disconnect(&s->chardev);
308         return;
309     }
310 
311     /* Kick right away to begin processing requests already in vring */
312     for (i = 0; i < s->dev.nvqs; i++) {
313         VirtQueue *kick_vq = virtio_get_queue(vdev, i);
314 
315         if (!virtio_queue_get_desc_addr(vdev, i)) {
316             continue;
317         }
318         event_notifier_set(virtio_queue_get_host_notifier(kick_vq));
319     }
320 }
321 
322 static void vhost_user_blk_reset(VirtIODevice *vdev)
323 {
324     VHostUserBlk *s = VHOST_USER_BLK(vdev);
325 
326     vhost_dev_free_inflight(s->inflight);
327 }
328 
329 static int vhost_user_blk_connect(DeviceState *dev, Error **errp)
330 {
331     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
332     VHostUserBlk *s = VHOST_USER_BLK(vdev);
333     int ret = 0;
334 
335     if (s->connected) {
336         return 0;
337     }
338 
339     s->dev.num_queues = s->num_queues;
340     s->dev.nvqs = s->num_queues;
341     s->dev.vqs = s->vhost_vqs;
342     s->dev.vq_index = 0;
343     s->dev.backend_features = 0;
344 
345     vhost_dev_set_config_notifier(&s->dev, &blk_ops);
346 
347     s->vhost_user.supports_config = true;
348     ret = vhost_dev_init(&s->dev, &s->vhost_user, VHOST_BACKEND_TYPE_USER, 0,
349                          errp);
350     if (ret < 0) {
351         return ret;
352     }
353 
354     s->connected = true;
355 
356     /* restore vhost state */
357     if (virtio_device_started(vdev, vdev->status)) {
358         ret = vhost_user_blk_start(vdev, errp);
359     }
360 
361     return ret;
362 }
363 
364 static void vhost_user_blk_disconnect(DeviceState *dev)
365 {
366     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
367     VHostUserBlk *s = VHOST_USER_BLK(vdev);
368 
369     if (!s->connected) {
370         goto done;
371     }
372     s->connected = false;
373 
374     vhost_user_blk_stop(vdev);
375 
376     vhost_dev_cleanup(&s->dev);
377 
378 done:
379     /* Re-instate the event handler for new connections */
380     qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, vhost_user_blk_event,
381                              NULL, dev, NULL, true);
382 }
383 
384 static void vhost_user_blk_event(void *opaque, QEMUChrEvent event)
385 {
386     DeviceState *dev = opaque;
387     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
388     VHostUserBlk *s = VHOST_USER_BLK(vdev);
389     Error *local_err = NULL;
390 
391     switch (event) {
392     case CHR_EVENT_OPENED:
393         if (vhost_user_blk_connect(dev, &local_err) < 0) {
394             error_report_err(local_err);
395             qemu_chr_fe_disconnect(&s->chardev);
396             return;
397         }
398         break;
399     case CHR_EVENT_CLOSED:
400         /* defer close until later to avoid circular close */
401         vhost_user_async_close(dev, &s->chardev, &s->dev,
402                                vhost_user_blk_disconnect);
403         break;
404     case CHR_EVENT_BREAK:
405     case CHR_EVENT_MUX_IN:
406     case CHR_EVENT_MUX_OUT:
407         /* Ignore */
408         break;
409     }
410 }
411 
412 static int vhost_user_blk_realize_connect(VHostUserBlk *s, Error **errp)
413 {
414     DeviceState *dev = DEVICE(s);
415     int ret;
416 
417     s->connected = false;
418 
419     ret = qemu_chr_fe_wait_connected(&s->chardev, errp);
420     if (ret < 0) {
421         return ret;
422     }
423 
424     ret = vhost_user_blk_connect(dev, errp);
425     if (ret < 0) {
426         qemu_chr_fe_disconnect(&s->chardev);
427         return ret;
428     }
429     assert(s->connected);
430 
431     ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg,
432                                VIRTIO_DEVICE(s)->config_len, errp);
433     if (ret < 0) {
434         qemu_chr_fe_disconnect(&s->chardev);
435         vhost_dev_cleanup(&s->dev);
436         return ret;
437     }
438 
439     return 0;
440 }
441 
442 static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
443 {
444     ERRP_GUARD();
445     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
446     VHostUserBlk *s = VHOST_USER_BLK(vdev);
447     size_t config_size;
448     int retries;
449     int i, ret;
450 
451     if (!s->chardev.chr) {
452         error_setg(errp, "chardev is mandatory");
453         return;
454     }
455 
456     if (s->num_queues == VHOST_USER_BLK_AUTO_NUM_QUEUES) {
457         s->num_queues = 1;
458     }
459     if (!s->num_queues || s->num_queues > VIRTIO_QUEUE_MAX) {
460         error_setg(errp, "invalid number of IO queues");
461         return;
462     }
463 
464     if (!s->queue_size) {
465         error_setg(errp, "queue size must be non-zero");
466         return;
467     }
468     if (s->queue_size > VIRTQUEUE_MAX_SIZE) {
469         error_setg(errp, "queue size must not exceed %d",
470                    VIRTQUEUE_MAX_SIZE);
471         return;
472     }
473 
474     if (!vhost_user_init(&s->vhost_user, &s->chardev, errp)) {
475         return;
476     }
477 
478     config_size = virtio_get_config_size(&virtio_blk_cfg_size_params,
479                                          vdev->host_features);
480     virtio_init(vdev, VIRTIO_ID_BLOCK, config_size);
481 
482     s->virtqs = g_new(VirtQueue *, s->num_queues);
483     for (i = 0; i < s->num_queues; i++) {
484         s->virtqs[i] = virtio_add_queue(vdev, s->queue_size,
485                                         vhost_user_blk_handle_output);
486     }
487 
488     s->inflight = g_new0(struct vhost_inflight, 1);
489     s->vhost_vqs = g_new0(struct vhost_virtqueue, s->num_queues);
490 
491     retries = VU_REALIZE_CONN_RETRIES;
492     assert(!*errp);
493     do {
494         if (*errp) {
495             error_prepend(errp, "Reconnecting after error: ");
496             error_report_err(*errp);
497             *errp = NULL;
498         }
499         ret = vhost_user_blk_realize_connect(s, errp);
500     } while (ret < 0 && retries--);
501 
502     if (ret < 0) {
503         goto virtio_err;
504     }
505 
506     /* we're fully initialized, now we can operate, so add the handler */
507     qemu_chr_fe_set_handlers(&s->chardev,  NULL, NULL,
508                              vhost_user_blk_event, NULL, (void *)dev,
509                              NULL, true);
510     return;
511 
512 virtio_err:
513     g_free(s->vhost_vqs);
514     s->vhost_vqs = NULL;
515     g_free(s->inflight);
516     s->inflight = NULL;
517     for (i = 0; i < s->num_queues; i++) {
518         virtio_delete_queue(s->virtqs[i]);
519     }
520     g_free(s->virtqs);
521     virtio_cleanup(vdev);
522     vhost_user_cleanup(&s->vhost_user);
523 }
524 
525 static void vhost_user_blk_device_unrealize(DeviceState *dev)
526 {
527     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
528     VHostUserBlk *s = VHOST_USER_BLK(dev);
529     int i;
530 
531     virtio_set_status(vdev, 0);
532     qemu_chr_fe_set_handlers(&s->chardev,  NULL, NULL, NULL,
533                              NULL, NULL, NULL, false);
534     vhost_dev_cleanup(&s->dev);
535     vhost_dev_free_inflight(s->inflight);
536     g_free(s->vhost_vqs);
537     s->vhost_vqs = NULL;
538     g_free(s->inflight);
539     s->inflight = NULL;
540 
541     for (i = 0; i < s->num_queues; i++) {
542         virtio_delete_queue(s->virtqs[i]);
543     }
544     g_free(s->virtqs);
545     virtio_cleanup(vdev);
546     vhost_user_cleanup(&s->vhost_user);
547 }
548 
549 static void vhost_user_blk_instance_init(Object *obj)
550 {
551     VHostUserBlk *s = VHOST_USER_BLK(obj);
552 
553     device_add_bootindex_property(obj, &s->bootindex, "bootindex",
554                                   "/disk@0,0", DEVICE(obj));
555 }
556 
557 static struct vhost_dev *vhost_user_blk_get_vhost(VirtIODevice *vdev)
558 {
559     VHostUserBlk *s = VHOST_USER_BLK(vdev);
560     return &s->dev;
561 }
562 
563 static const VMStateDescription vmstate_vhost_user_blk = {
564     .name = "vhost-user-blk",
565     .minimum_version_id = 1,
566     .version_id = 1,
567     .fields = (const VMStateField[]) {
568         VMSTATE_VIRTIO_DEVICE,
569         VMSTATE_END_OF_LIST()
570     },
571 };
572 
573 static Property vhost_user_blk_properties[] = {
574     DEFINE_PROP_CHR("chardev", VHostUserBlk, chardev),
575     DEFINE_PROP_UINT16("num-queues", VHostUserBlk, num_queues,
576                        VHOST_USER_BLK_AUTO_NUM_QUEUES),
577     DEFINE_PROP_UINT32("queue-size", VHostUserBlk, queue_size, 128),
578     DEFINE_PROP_BIT64("config-wce", VHostUserBlk, parent_obj.host_features,
579                       VIRTIO_BLK_F_CONFIG_WCE, true),
580     DEFINE_PROP_BIT64("discard", VHostUserBlk, parent_obj.host_features,
581                       VIRTIO_BLK_F_DISCARD, true),
582     DEFINE_PROP_BIT64("write-zeroes", VHostUserBlk, parent_obj.host_features,
583                       VIRTIO_BLK_F_WRITE_ZEROES, true),
584     DEFINE_PROP_END_OF_LIST(),
585 };
586 
587 static void vhost_user_blk_class_init(ObjectClass *klass, void *data)
588 {
589     DeviceClass *dc = DEVICE_CLASS(klass);
590     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
591 
592     device_class_set_props(dc, vhost_user_blk_properties);
593     dc->vmsd = &vmstate_vhost_user_blk;
594     dc->sync_config = vhost_user_blk_sync_config;
595     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
596     vdc->realize = vhost_user_blk_device_realize;
597     vdc->unrealize = vhost_user_blk_device_unrealize;
598     vdc->get_config = vhost_user_blk_update_config;
599     vdc->set_config = vhost_user_blk_set_config;
600     vdc->get_features = vhost_user_blk_get_features;
601     vdc->set_status = vhost_user_blk_set_status;
602     vdc->reset = vhost_user_blk_reset;
603     vdc->get_vhost = vhost_user_blk_get_vhost;
604 }
605 
606 static const TypeInfo vhost_user_blk_info = {
607     .name = TYPE_VHOST_USER_BLK,
608     .parent = TYPE_VIRTIO_DEVICE,
609     .instance_size = sizeof(VHostUserBlk),
610     .instance_init = vhost_user_blk_instance_init,
611     .class_init = vhost_user_blk_class_init,
612 };
613 
614 static void virtio_register_types(void)
615 {
616     type_register_static(&vhost_user_blk_info);
617 }
618 
619 type_init(virtio_register_types)
620