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