]> Git Repo - qemu.git/blob - hw/block/dataplane/virtio-blk.c
Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[qemu.git] / hw / block / dataplane / virtio-blk.c
1 /*
2  * Dedicated thread for virtio-blk I/O processing
3  *
4  * Copyright 2012 IBM, Corp.
5  * Copyright 2012 Red Hat, Inc. and/or its affiliates
6  *
7  * Authors:
8  *   Stefan Hajnoczi <[email protected]>
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2 or later.
11  * See the COPYING file in the top-level directory.
12  *
13  */
14
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "trace.h"
18 #include "qemu/iov.h"
19 #include "qemu/thread.h"
20 #include "qemu/error-report.h"
21 #include "hw/virtio/virtio-access.h"
22 #include "sysemu/block-backend.h"
23 #include "hw/virtio/virtio-blk.h"
24 #include "virtio-blk.h"
25 #include "block/aio.h"
26 #include "hw/virtio/virtio-bus.h"
27 #include "qom/object_interfaces.h"
28
29 struct VirtIOBlockDataPlane {
30     bool starting;
31     bool stopping;
32
33     VirtIOBlkConf *conf;
34
35     VirtIODevice *vdev;
36     VirtQueue *vq;                  /* virtqueue vring */
37     EventNotifier *guest_notifier;  /* irq */
38     QEMUBH *bh;                     /* bh for guest notification */
39
40     /* Note that these EventNotifiers are assigned by value.  This is
41      * fine as long as you do not call event_notifier_cleanup on them
42      * (because you don't own the file descriptor or handle; you just
43      * use it).
44      */
45     IOThread *iothread;
46     AioContext *ctx;
47 };
48
49 /* Raise an interrupt to signal guest, if necessary */
50 void virtio_blk_data_plane_notify(VirtIOBlockDataPlane *s)
51 {
52     qemu_bh_schedule(s->bh);
53 }
54
55 static void notify_guest_bh(void *opaque)
56 {
57     VirtIOBlockDataPlane *s = opaque;
58
59     if (!virtio_should_notify(s->vdev, s->vq)) {
60         return;
61     }
62
63     event_notifier_set(s->guest_notifier);
64 }
65
66 /* Context: QEMU global mutex held */
67 void virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *conf,
68                                   VirtIOBlockDataPlane **dataplane,
69                                   Error **errp)
70 {
71     VirtIOBlockDataPlane *s;
72     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
73     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
74
75     *dataplane = NULL;
76
77     if (!conf->iothread) {
78         return;
79     }
80
81     /* Don't try if transport does not support notifiers. */
82     if (!k->set_guest_notifiers || !k->ioeventfd_started) {
83         error_setg(errp,
84                    "device is incompatible with dataplane "
85                    "(transport does not support notifiers)");
86         return;
87     }
88
89     /* If dataplane is (re-)enabled while the guest is running there could be
90      * block jobs that can conflict.
91      */
92     if (blk_op_is_blocked(conf->conf.blk, BLOCK_OP_TYPE_DATAPLANE, errp)) {
93         error_prepend(errp, "cannot start dataplane thread: ");
94         return;
95     }
96
97     s = g_new0(VirtIOBlockDataPlane, 1);
98     s->vdev = vdev;
99     s->conf = conf;
100
101     if (conf->iothread) {
102         s->iothread = conf->iothread;
103         object_ref(OBJECT(s->iothread));
104     }
105     s->ctx = iothread_get_aio_context(s->iothread);
106     s->bh = aio_bh_new(s->ctx, notify_guest_bh, s);
107
108     *dataplane = s;
109 }
110
111 /* Context: QEMU global mutex held */
112 void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane *s)
113 {
114     if (!s) {
115         return;
116     }
117
118     virtio_blk_data_plane_stop(s);
119     qemu_bh_delete(s->bh);
120     object_unref(OBJECT(s->iothread));
121     g_free(s);
122 }
123
124 static void virtio_blk_data_plane_handle_output(VirtIODevice *vdev,
125                                                 VirtQueue *vq)
126 {
127     VirtIOBlock *s = (VirtIOBlock *)vdev;
128
129     assert(s->dataplane);
130     assert(s->dataplane_started);
131
132     virtio_blk_handle_vq(s, vq);
133 }
134
135 /* Context: QEMU global mutex held */
136 void virtio_blk_data_plane_start(VirtIOBlockDataPlane *s)
137 {
138     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s->vdev)));
139     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
140     VirtIOBlock *vblk = VIRTIO_BLK(s->vdev);
141     int r;
142
143     if (vblk->dataplane_started || s->starting) {
144         return;
145     }
146
147     s->starting = true;
148     s->vq = virtio_get_queue(s->vdev, 0);
149
150     /* Set up guest notifier (irq) */
151     r = k->set_guest_notifiers(qbus->parent, 1, true);
152     if (r != 0) {
153         fprintf(stderr, "virtio-blk failed to set guest notifier (%d), "
154                 "ensure -enable-kvm is set\n", r);
155         goto fail_guest_notifiers;
156     }
157     s->guest_notifier = virtio_queue_get_guest_notifier(s->vq);
158
159     /* Set up virtqueue notify */
160     r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), 0, true);
161     if (r != 0) {
162         fprintf(stderr, "virtio-blk failed to set host notifier (%d)\n", r);
163         goto fail_host_notifier;
164     }
165
166     s->starting = false;
167     vblk->dataplane_started = true;
168     trace_virtio_blk_data_plane_start(s);
169
170     blk_set_aio_context(s->conf->conf.blk, s->ctx);
171
172     /* Kick right away to begin processing requests already in vring */
173     event_notifier_set(virtio_queue_get_host_notifier(s->vq));
174
175     /* Get this show started by hooking up our callbacks */
176     aio_context_acquire(s->ctx);
177     virtio_queue_aio_set_host_notifier_handler(s->vq, s->ctx,
178                                                virtio_blk_data_plane_handle_output);
179     aio_context_release(s->ctx);
180     return;
181
182   fail_host_notifier:
183     k->set_guest_notifiers(qbus->parent, 1, false);
184   fail_guest_notifiers:
185     vblk->dataplane_disabled = true;
186     s->starting = false;
187     vblk->dataplane_started = true;
188 }
189
190 /* Context: QEMU global mutex held */
191 void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s)
192 {
193     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s->vdev)));
194     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
195     VirtIOBlock *vblk = VIRTIO_BLK(s->vdev);
196
197     if (!vblk->dataplane_started || s->stopping) {
198         return;
199     }
200
201     /* Better luck next time. */
202     if (vblk->dataplane_disabled) {
203         vblk->dataplane_disabled = false;
204         vblk->dataplane_started = false;
205         return;
206     }
207     s->stopping = true;
208     trace_virtio_blk_data_plane_stop(s);
209
210     aio_context_acquire(s->ctx);
211
212     /* Stop notifications for new requests from guest */
213     virtio_queue_aio_set_host_notifier_handler(s->vq, s->ctx, NULL);
214
215     /* Drain and switch bs back to the QEMU main loop */
216     blk_set_aio_context(s->conf->conf.blk, qemu_get_aio_context());
217
218     aio_context_release(s->ctx);
219
220     virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), 0, false);
221
222     /* Clean up guest notifier (irq) */
223     k->set_guest_notifiers(qbus->parent, 1, false);
224
225     vblk->dataplane_started = false;
226     s->stopping = false;
227 }
This page took 0.036621 seconds and 4 git commands to generate.