]> git.proxmox.com Git - qemu.git/blob - hw/dataplane/vring.c
d5d4ef45d17b8a3ec756945e7baf8b8e57d0771a
[qemu.git] / hw / dataplane / vring.c
1 /* Copyright 2012 Red Hat, Inc.
2 * Copyright IBM, Corp. 2012
3 *
4 * Based on Linux 2.6.39 vhost code:
5 * Copyright (C) 2009 Red Hat, Inc.
6 * Copyright (C) 2006 Rusty Russell IBM Corporation
7 *
8 * Author: Michael S. Tsirkin <mst@redhat.com>
9 * Stefan Hajnoczi <stefanha@redhat.com>
10 *
11 * Inspiration, some code, and most witty comments come from
12 * Documentation/virtual/lguest/lguest.c, by Rusty Russell
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2.
15 */
16
17 #include "trace.h"
18 #include "hw/dataplane/vring.h"
19
20 /* Map the guest's vring to host memory */
21 bool vring_setup(Vring *vring, VirtIODevice *vdev, int n)
22 {
23 hwaddr vring_addr = virtio_queue_get_ring_addr(vdev, n);
24 hwaddr vring_size = virtio_queue_get_ring_size(vdev, n);
25 void *vring_ptr;
26
27 vring->broken = false;
28
29 hostmem_init(&vring->hostmem);
30 vring_ptr = hostmem_lookup(&vring->hostmem, vring_addr, vring_size, true);
31 if (!vring_ptr) {
32 error_report("Failed to map vring "
33 "addr %#" HWADDR_PRIx " size %" HWADDR_PRIu,
34 vring_addr, vring_size);
35 vring->broken = true;
36 return false;
37 }
38
39 vring_init(&vring->vr, virtio_queue_get_num(vdev, n), vring_ptr, 4096);
40
41 vring->last_avail_idx = 0;
42 vring->last_used_idx = 0;
43 vring->signalled_used = 0;
44 vring->signalled_used_valid = false;
45
46 trace_vring_setup(virtio_queue_get_ring_addr(vdev, n),
47 vring->vr.desc, vring->vr.avail, vring->vr.used);
48 return true;
49 }
50
51 void vring_teardown(Vring *vring)
52 {
53 hostmem_finalize(&vring->hostmem);
54 }
55
56 /* Disable guest->host notifies */
57 void vring_disable_notification(VirtIODevice *vdev, Vring *vring)
58 {
59 if (!(vdev->guest_features & (1 << VIRTIO_RING_F_EVENT_IDX))) {
60 vring->vr.used->flags |= VRING_USED_F_NO_NOTIFY;
61 }
62 }
63
64 /* Enable guest->host notifies
65 *
66 * Return true if the vring is empty, false if there are more requests.
67 */
68 bool vring_enable_notification(VirtIODevice *vdev, Vring *vring)
69 {
70 if (vdev->guest_features & (1 << VIRTIO_RING_F_EVENT_IDX)) {
71 vring_avail_event(&vring->vr) = vring->vr.avail->idx;
72 } else {
73 vring->vr.used->flags &= ~VRING_USED_F_NO_NOTIFY;
74 }
75 smp_mb(); /* ensure update is seen before reading avail_idx */
76 return !vring_more_avail(vring);
77 }
78
79 /* This is stolen from linux/drivers/vhost/vhost.c:vhost_notify() */
80 bool vring_should_notify(VirtIODevice *vdev, Vring *vring)
81 {
82 uint16_t old, new;
83 bool v;
84 /* Flush out used index updates. This is paired
85 * with the barrier that the Guest executes when enabling
86 * interrupts. */
87 smp_mb();
88
89 if ((vdev->guest_features & VIRTIO_F_NOTIFY_ON_EMPTY) &&
90 unlikely(vring->vr.avail->idx == vring->last_avail_idx)) {
91 return true;
92 }
93
94 if (!(vdev->guest_features & VIRTIO_RING_F_EVENT_IDX)) {
95 return !(vring->vr.avail->flags & VRING_AVAIL_F_NO_INTERRUPT);
96 }
97 old = vring->signalled_used;
98 v = vring->signalled_used_valid;
99 new = vring->signalled_used = vring->last_used_idx;
100 vring->signalled_used_valid = true;
101
102 if (unlikely(!v)) {
103 return true;
104 }
105
106 return vring_need_event(vring_used_event(&vring->vr), new, old);
107 }
108
109 /* This is stolen from linux/drivers/vhost/vhost.c. */
110 static int get_indirect(Vring *vring,
111 struct iovec iov[], struct iovec *iov_end,
112 unsigned int *out_num, unsigned int *in_num,
113 struct vring_desc *indirect)
114 {
115 struct vring_desc desc;
116 unsigned int i = 0, count, found = 0;
117
118 /* Sanity check */
119 if (unlikely(indirect->len % sizeof(desc))) {
120 error_report("Invalid length in indirect descriptor: "
121 "len %#x not multiple of %#zx",
122 indirect->len, sizeof(desc));
123 vring->broken = true;
124 return -EFAULT;
125 }
126
127 count = indirect->len / sizeof(desc);
128 /* Buffers are chained via a 16 bit next field, so
129 * we can have at most 2^16 of these. */
130 if (unlikely(count > USHRT_MAX + 1)) {
131 error_report("Indirect buffer length too big: %d", indirect->len);
132 vring->broken = true;
133 return -EFAULT;
134 }
135
136 do {
137 struct vring_desc *desc_ptr;
138
139 /* Translate indirect descriptor */
140 desc_ptr = hostmem_lookup(&vring->hostmem,
141 indirect->addr + found * sizeof(desc),
142 sizeof(desc), false);
143 if (!desc_ptr) {
144 error_report("Failed to map indirect descriptor "
145 "addr %#" PRIx64 " len %zu",
146 (uint64_t)indirect->addr + found * sizeof(desc),
147 sizeof(desc));
148 vring->broken = true;
149 return -EFAULT;
150 }
151 desc = *desc_ptr;
152
153 /* Ensure descriptor has been loaded before accessing fields */
154 barrier(); /* read_barrier_depends(); */
155
156 if (unlikely(++found > count)) {
157 error_report("Loop detected: last one at %u "
158 "indirect size %u", i, count);
159 vring->broken = true;
160 return -EFAULT;
161 }
162
163 if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) {
164 error_report("Nested indirect descriptor");
165 vring->broken = true;
166 return -EFAULT;
167 }
168
169 /* Stop for now if there are not enough iovecs available. */
170 if (iov >= iov_end) {
171 return -ENOBUFS;
172 }
173
174 iov->iov_base = hostmem_lookup(&vring->hostmem, desc.addr, desc.len,
175 desc.flags & VRING_DESC_F_WRITE);
176 if (!iov->iov_base) {
177 error_report("Failed to map indirect descriptor"
178 "addr %#" PRIx64 " len %u",
179 (uint64_t)desc.addr, desc.len);
180 vring->broken = true;
181 return -EFAULT;
182 }
183 iov->iov_len = desc.len;
184 iov++;
185
186 /* If this is an input descriptor, increment that count. */
187 if (desc.flags & VRING_DESC_F_WRITE) {
188 *in_num += 1;
189 } else {
190 /* If it's an output descriptor, they're all supposed
191 * to come before any input descriptors. */
192 if (unlikely(*in_num)) {
193 error_report("Indirect descriptor "
194 "has out after in: idx %u", i);
195 vring->broken = true;
196 return -EFAULT;
197 }
198 *out_num += 1;
199 }
200 i = desc.next;
201 } while (desc.flags & VRING_DESC_F_NEXT);
202 return 0;
203 }
204
205 /* This looks in the virtqueue and for the first available buffer, and converts
206 * it to an iovec for convenient access. Since descriptors consist of some
207 * number of output then some number of input descriptors, it's actually two
208 * iovecs, but we pack them into one and note how many of each there were.
209 *
210 * This function returns the descriptor number found, or vq->num (which is
211 * never a valid descriptor number) if none was found. A negative code is
212 * returned on error.
213 *
214 * Stolen from linux/drivers/vhost/vhost.c.
215 */
216 int vring_pop(VirtIODevice *vdev, Vring *vring,
217 struct iovec iov[], struct iovec *iov_end,
218 unsigned int *out_num, unsigned int *in_num)
219 {
220 struct vring_desc desc;
221 unsigned int i, head, found = 0, num = vring->vr.num;
222 uint16_t avail_idx, last_avail_idx;
223
224 /* If there was a fatal error then refuse operation */
225 if (vring->broken) {
226 return -EFAULT;
227 }
228
229 /* Check it isn't doing very strange things with descriptor numbers. */
230 last_avail_idx = vring->last_avail_idx;
231 avail_idx = vring->vr.avail->idx;
232 barrier(); /* load indices now and not again later */
233
234 if (unlikely((uint16_t)(avail_idx - last_avail_idx) > num)) {
235 error_report("Guest moved used index from %u to %u",
236 last_avail_idx, avail_idx);
237 vring->broken = true;
238 return -EFAULT;
239 }
240
241 /* If there's nothing new since last we looked. */
242 if (avail_idx == last_avail_idx) {
243 return -EAGAIN;
244 }
245
246 /* Only get avail ring entries after they have been exposed by guest. */
247 smp_rmb();
248
249 /* Grab the next descriptor number they're advertising, and increment
250 * the index we've seen. */
251 head = vring->vr.avail->ring[last_avail_idx % num];
252
253 /* If their number is silly, that's an error. */
254 if (unlikely(head >= num)) {
255 error_report("Guest says index %u > %u is available", head, num);
256 vring->broken = true;
257 return -EFAULT;
258 }
259
260 if (vdev->guest_features & (1 << VIRTIO_RING_F_EVENT_IDX)) {
261 vring_avail_event(&vring->vr) = vring->vr.avail->idx;
262 }
263
264 /* When we start there are none of either input nor output. */
265 *out_num = *in_num = 0;
266
267 i = head;
268 do {
269 if (unlikely(i >= num)) {
270 error_report("Desc index is %u > %u, head = %u", i, num, head);
271 vring->broken = true;
272 return -EFAULT;
273 }
274 if (unlikely(++found > num)) {
275 error_report("Loop detected: last one at %u vq size %u head %u",
276 i, num, head);
277 vring->broken = true;
278 return -EFAULT;
279 }
280 desc = vring->vr.desc[i];
281
282 /* Ensure descriptor is loaded before accessing fields */
283 barrier();
284
285 if (desc.flags & VRING_DESC_F_INDIRECT) {
286 int ret = get_indirect(vring, iov, iov_end, out_num, in_num, &desc);
287 if (ret < 0) {
288 return ret;
289 }
290 continue;
291 }
292
293 /* If there are not enough iovecs left, stop for now. The caller
294 * should check if there are more descs available once they have dealt
295 * with the current set.
296 */
297 if (iov >= iov_end) {
298 return -ENOBUFS;
299 }
300
301 /* TODO handle non-contiguous memory across region boundaries */
302 iov->iov_base = hostmem_lookup(&vring->hostmem, desc.addr, desc.len,
303 desc.flags & VRING_DESC_F_WRITE);
304 if (!iov->iov_base) {
305 error_report("Failed to map vring desc addr %#" PRIx64 " len %u",
306 (uint64_t)desc.addr, desc.len);
307 vring->broken = true;
308 return -EFAULT;
309 }
310 iov->iov_len = desc.len;
311 iov++;
312
313 if (desc.flags & VRING_DESC_F_WRITE) {
314 /* If this is an input descriptor,
315 * increment that count. */
316 *in_num += 1;
317 } else {
318 /* If it's an output descriptor, they're all supposed
319 * to come before any input descriptors. */
320 if (unlikely(*in_num)) {
321 error_report("Descriptor has out after in: idx %d", i);
322 vring->broken = true;
323 return -EFAULT;
324 }
325 *out_num += 1;
326 }
327 i = desc.next;
328 } while (desc.flags & VRING_DESC_F_NEXT);
329
330 /* On success, increment avail index. */
331 vring->last_avail_idx++;
332 return head;
333 }
334
335 /* After we've used one of their buffers, we tell them about it.
336 *
337 * Stolen from linux/drivers/vhost/vhost.c.
338 */
339 void vring_push(Vring *vring, unsigned int head, int len)
340 {
341 struct vring_used_elem *used;
342 uint16_t new;
343
344 /* Don't touch vring if a fatal error occurred */
345 if (vring->broken) {
346 return;
347 }
348
349 /* The virtqueue contains a ring of used buffers. Get a pointer to the
350 * next entry in that used ring. */
351 used = &vring->vr.used->ring[vring->last_used_idx % vring->vr.num];
352 used->id = head;
353 used->len = len;
354
355 /* Make sure buffer is written before we update index. */
356 smp_wmb();
357
358 new = vring->vr.used->idx = ++vring->last_used_idx;
359 if (unlikely((int16_t)(new - vring->signalled_used) < (uint16_t)1)) {
360 vring->signalled_used_valid = false;
361 }
362 }