]> Git Repo - qemu.git/blob - hw/block/dataplane/virtio-blk.c
Merge remote-tracking branch 'remotes/stefanha/tags/block-pull-request' 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     VirtIODevice *vdev;
35     QEMUBH *bh;                     /* bh for guest notification */
36     unsigned long *batch_notify_vqs;
37
38     /* Note that these EventNotifiers are assigned by value.  This is
39      * fine as long as you do not call event_notifier_cleanup on them
40      * (because you don't own the file descriptor or handle; you just
41      * use it).
42      */
43     IOThread *iothread;
44     AioContext *ctx;
45 };
46
47 /* Raise an interrupt to signal guest, if necessary */
48 void virtio_blk_data_plane_notify(VirtIOBlockDataPlane *s, VirtQueue *vq)
49 {
50     set_bit(virtio_get_queue_index(vq), s->batch_notify_vqs);
51     qemu_bh_schedule(s->bh);
52 }
53
54 static void notify_guest_bh(void *opaque)
55 {
56     VirtIOBlockDataPlane *s = opaque;
57     unsigned nvqs = s->conf->num_queues;
58     unsigned long bitmap[BITS_TO_LONGS(nvqs)];
59     unsigned j;
60
61     memcpy(bitmap, s->batch_notify_vqs, sizeof(bitmap));
62     memset(s->batch_notify_vqs, 0, sizeof(bitmap));
63
64     for (j = 0; j < nvqs; j += BITS_PER_LONG) {
65         unsigned long bits = bitmap[j];
66
67         while (bits != 0) {
68             unsigned i = j + ctzl(bits);
69             VirtQueue *vq = virtio_get_queue(s->vdev, i);
70
71             if (virtio_should_notify(s->vdev, vq)) {
72                 event_notifier_set(virtio_queue_get_guest_notifier(vq));
73             }
74
75             bits &= bits - 1; /* clear right-most bit */
76         }
77     }
78 }
79
80 /* Context: QEMU global mutex held */
81 void virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *conf,
82                                   VirtIOBlockDataPlane **dataplane,
83                                   Error **errp)
84 {
85     VirtIOBlockDataPlane *s;
86     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
87     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
88
89     *dataplane = NULL;
90
91     if (!conf->iothread) {
92         return;
93     }
94
95     /* Don't try if transport does not support notifiers. */
96     if (!k->set_guest_notifiers || !k->ioeventfd_started) {
97         error_setg(errp,
98                    "device is incompatible with dataplane "
99                    "(transport does not support notifiers)");
100         return;
101     }
102
103     /* If dataplane is (re-)enabled while the guest is running there could be
104      * block jobs that can conflict.
105      */
106     if (blk_op_is_blocked(conf->conf.blk, BLOCK_OP_TYPE_DATAPLANE, errp)) {
107         error_prepend(errp, "cannot start dataplane thread: ");
108         return;
109     }
110
111     s = g_new0(VirtIOBlockDataPlane, 1);
112     s->vdev = vdev;
113     s->conf = conf;
114
115     if (conf->iothread) {
116         s->iothread = conf->iothread;
117         object_ref(OBJECT(s->iothread));
118     }
119     s->ctx = iothread_get_aio_context(s->iothread);
120     s->bh = aio_bh_new(s->ctx, notify_guest_bh, s);
121     s->batch_notify_vqs = bitmap_new(conf->num_queues);
122
123     *dataplane = s;
124 }
125
126 /* Context: QEMU global mutex held */
127 void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane *s)
128 {
129     if (!s) {
130         return;
131     }
132
133     virtio_blk_data_plane_stop(s);
134     g_free(s->batch_notify_vqs);
135     qemu_bh_delete(s->bh);
136     object_unref(OBJECT(s->iothread));
137     g_free(s);
138 }
139
140 static void virtio_blk_data_plane_handle_output(VirtIODevice *vdev,
141                                                 VirtQueue *vq)
142 {
143     VirtIOBlock *s = (VirtIOBlock *)vdev;
144
145     assert(s->dataplane);
146     assert(s->dataplane_started);
147
148     virtio_blk_handle_vq(s, vq);
149 }
150
151 /* Context: QEMU global mutex held */
152 void virtio_blk_data_plane_start(VirtIOBlockDataPlane *s)
153 {
154     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s->vdev)));
155     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
156     VirtIOBlock *vblk = VIRTIO_BLK(s->vdev);
157     unsigned i;
158     unsigned nvqs = s->conf->num_queues;
159     int r;
160
161     if (vblk->dataplane_started || s->starting) {
162         return;
163     }
164
165     s->starting = true;
166
167     /* Set up guest notifier (irq) */
168     r = k->set_guest_notifiers(qbus->parent, nvqs, true);
169     if (r != 0) {
170         fprintf(stderr, "virtio-blk failed to set guest notifier (%d), "
171                 "ensure -enable-kvm is set\n", r);
172         goto fail_guest_notifiers;
173     }
174
175     /* Set up virtqueue notify */
176     for (i = 0; i < nvqs; i++) {
177         r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, true);
178         if (r != 0) {
179             fprintf(stderr, "virtio-blk failed to set host notifier (%d)\n", r);
180             while (i--) {
181                 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
182             }
183             goto fail_guest_notifiers;
184         }
185     }
186
187     s->starting = false;
188     vblk->dataplane_started = true;
189     trace_virtio_blk_data_plane_start(s);
190
191     blk_set_aio_context(s->conf->conf.blk, s->ctx);
192
193     /* Kick right away to begin processing requests already in vring */
194     for (i = 0; i < nvqs; i++) {
195         VirtQueue *vq = virtio_get_queue(s->vdev, i);
196
197         event_notifier_set(virtio_queue_get_host_notifier(vq));
198     }
199
200     /* Get this show started by hooking up our callbacks */
201     aio_context_acquire(s->ctx);
202     for (i = 0; i < nvqs; i++) {
203         VirtQueue *vq = virtio_get_queue(s->vdev, i);
204
205         virtio_queue_aio_set_host_notifier_handler(vq, s->ctx,
206                 virtio_blk_data_plane_handle_output);
207     }
208     aio_context_release(s->ctx);
209     return;
210
211   fail_guest_notifiers:
212     vblk->dataplane_disabled = true;
213     s->starting = false;
214     vblk->dataplane_started = true;
215 }
216
217 /* Context: QEMU global mutex held */
218 void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s)
219 {
220     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s->vdev)));
221     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
222     VirtIOBlock *vblk = VIRTIO_BLK(s->vdev);
223     unsigned i;
224     unsigned nvqs = s->conf->num_queues;
225
226     if (!vblk->dataplane_started || s->stopping) {
227         return;
228     }
229
230     /* Better luck next time. */
231     if (vblk->dataplane_disabled) {
232         vblk->dataplane_disabled = false;
233         vblk->dataplane_started = false;
234         return;
235     }
236     s->stopping = true;
237     trace_virtio_blk_data_plane_stop(s);
238
239     aio_context_acquire(s->ctx);
240
241     /* Stop notifications for new requests from guest */
242     for (i = 0; i < nvqs; i++) {
243         VirtQueue *vq = virtio_get_queue(s->vdev, i);
244
245         virtio_queue_aio_set_host_notifier_handler(vq, s->ctx, NULL);
246     }
247
248     /* Drain and switch bs back to the QEMU main loop */
249     blk_set_aio_context(s->conf->conf.blk, qemu_get_aio_context());
250
251     aio_context_release(s->ctx);
252
253     for (i = 0; i < nvqs; i++) {
254         virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
255     }
256
257     /* Clean up guest notifier (irq) */
258     k->set_guest_notifiers(qbus->parent, nvqs, false);
259
260     vblk->dataplane_started = false;
261     s->stopping = false;
262 }
This page took 0.038796 seconds and 4 git commands to generate.