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