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