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