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