virtio_ring: expose virtio barriers for use in vringh.
[deliverable/linux.git] / tools / virtio / virtio_test.c
CommitLineData
4e53f78e
MT
1#define _GNU_SOURCE
2#include <getopt.h>
3#include <string.h>
4#include <poll.h>
5#include <sys/eventfd.h>
6#include <stdlib.h>
7#include <assert.h>
8#include <unistd.h>
9#include <sys/ioctl.h>
10#include <sys/stat.h>
11#include <sys/types.h>
12#include <fcntl.h>
13#include <linux/vhost.h>
14#include <linux/virtio.h>
15#include <linux/virtio_ring.h>
16#include "../../drivers/vhost/test.h"
17
18struct vq_info {
19 int kick;
20 int call;
21 int num;
22 int idx;
23 void *ring;
24 /* copy used for control */
25 struct vring vring;
26 struct virtqueue *vq;
27};
28
29struct vdev_info {
30 struct virtio_device vdev;
31 int control;
32 struct pollfd fds[1];
33 struct vq_info vqs[1];
34 int nvqs;
35 void *buf;
36 size_t buf_size;
37 struct vhost_memory *mem;
38};
39
40void vq_notify(struct virtqueue *vq)
41{
42 struct vq_info *info = vq->priv;
43 unsigned long long v = 1;
44 int r;
45 r = write(info->kick, &v, sizeof v);
46 assert(r == sizeof v);
47}
48
49void vq_callback(struct virtqueue *vq)
50{
51}
52
53
54void vhost_vq_setup(struct vdev_info *dev, struct vq_info *info)
55{
56 struct vhost_vring_state state = { .index = info->idx };
57 struct vhost_vring_file file = { .index = info->idx };
58 unsigned long long features = dev->vdev.features[0];
59 struct vhost_vring_addr addr = {
60 .index = info->idx,
61 .desc_user_addr = (uint64_t)(unsigned long)info->vring.desc,
62 .avail_user_addr = (uint64_t)(unsigned long)info->vring.avail,
63 .used_user_addr = (uint64_t)(unsigned long)info->vring.used,
64 };
65 int r;
66 r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
67 assert(r >= 0);
68 state.num = info->vring.num;
69 r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
70 assert(r >= 0);
71 state.num = 0;
72 r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
73 assert(r >= 0);
74 r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
75 assert(r >= 0);
76 file.fd = info->kick;
77 r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
78 assert(r >= 0);
79 file.fd = info->call;
80 r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
81 assert(r >= 0);
82}
83
84static void vq_info_add(struct vdev_info *dev, int num)
85{
86 struct vq_info *info = &dev->vqs[dev->nvqs];
87 int r;
88 info->idx = dev->nvqs;
89 info->kick = eventfd(0, EFD_NONBLOCK);
90 info->call = eventfd(0, EFD_NONBLOCK);
91 r = posix_memalign(&info->ring, 4096, vring_size(num, 4096));
92 assert(r >= 0);
93 memset(info->ring, 0, vring_size(num, 4096));
94 vring_init(&info->vring, num, info->ring, 4096);
73640c99
MT
95 info->vq = vring_new_virtqueue(info->idx,
96 info->vring.num, 4096, &dev->vdev,
7b21e34f 97 true, info->ring,
4e53f78e
MT
98 vq_notify, vq_callback, "test");
99 assert(info->vq);
100 info->vq->priv = info;
101 vhost_vq_setup(dev, info);
102 dev->fds[info->idx].fd = info->call;
103 dev->fds[info->idx].events = POLLIN;
104 dev->nvqs++;
105}
106
107static void vdev_info_init(struct vdev_info* dev, unsigned long long features)
108{
109 int r;
110 memset(dev, 0, sizeof *dev);
111 dev->vdev.features[0] = features;
112 dev->vdev.features[1] = features >> 32;
113 dev->buf_size = 1024;
114 dev->buf = malloc(dev->buf_size);
115 assert(dev->buf);
116 dev->control = open("/dev/vhost-test", O_RDWR);
117 assert(dev->control >= 0);
118 r = ioctl(dev->control, VHOST_SET_OWNER, NULL);
119 assert(r >= 0);
120 dev->mem = malloc(offsetof(struct vhost_memory, regions) +
121 sizeof dev->mem->regions[0]);
122 assert(dev->mem);
123 memset(dev->mem, 0, offsetof(struct vhost_memory, regions) +
124 sizeof dev->mem->regions[0]);
125 dev->mem->nregions = 1;
126 dev->mem->regions[0].guest_phys_addr = (long)dev->buf;
127 dev->mem->regions[0].userspace_addr = (long)dev->buf;
128 dev->mem->regions[0].memory_size = dev->buf_size;
129 r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
130 assert(r >= 0);
131}
132
133/* TODO: this is pretty bad: we get a cache line bounce
134 * for the wait queue on poll and another one on read,
135 * plus the read which is there just to clear the
136 * current state. */
137static void wait_for_interrupt(struct vdev_info *dev)
138{
139 int i;
140 unsigned long long val;
141 poll(dev->fds, dev->nvqs, -1);
142 for (i = 0; i < dev->nvqs; ++i)
143 if (dev->fds[i].revents & POLLIN) {
144 read(dev->fds[i].fd, &val, sizeof val);
145 }
146}
147
64d09888
MT
148static void run_test(struct vdev_info *dev, struct vq_info *vq,
149 bool delayed, int bufs)
4e53f78e
MT
150{
151 struct scatterlist sl;
152 long started = 0, completed = 0;
153 long completed_before;
154 int r, test = 1;
155 unsigned len;
156 long long spurious = 0;
157 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
158 assert(r >= 0);
159 for (;;) {
160 virtqueue_disable_cb(vq->vq);
161 completed_before = completed;
162 do {
163 if (started < bufs) {
164 sg_init_one(&sl, dev->buf, dev->buf_size);
165 r = virtqueue_add_buf(vq->vq, &sl, 1, 0,
f96fde41
RR
166 dev->buf + started,
167 GFP_ATOMIC);
de929b04 168 if (likely(r == 0)) {
4e53f78e
MT
169 ++started;
170 virtqueue_kick(vq->vq);
171 }
172 } else
173 r = -1;
174
175 /* Flush out completed bufs if any */
176 if (virtqueue_get_buf(vq->vq, &len)) {
177 ++completed;
178 r = 0;
179 }
180
de929b04 181 } while (r == 0);
4e53f78e
MT
182 if (completed == completed_before)
183 ++spurious;
184 assert(completed <= bufs);
185 assert(started <= bufs);
186 if (completed == bufs)
187 break;
64d09888
MT
188 if (delayed) {
189 if (virtqueue_enable_cb_delayed(vq->vq))
190 wait_for_interrupt(dev);
191 } else {
192 if (virtqueue_enable_cb(vq->vq))
193 wait_for_interrupt(dev);
4e53f78e
MT
194 }
195 }
196 test = 0;
197 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
198 assert(r >= 0);
199 fprintf(stderr, "spurious wakeus: 0x%llx\n", spurious);
200}
201
202const char optstring[] = "h";
203const struct option longopts[] = {
204 {
205 .name = "help",
206 .val = 'h',
207 },
4423fe40
MT
208 {
209 .name = "event-idx",
210 .val = 'E',
211 },
212 {
213 .name = "no-event-idx",
214 .val = 'e',
215 },
4e53f78e
MT
216 {
217 .name = "indirect",
218 .val = 'I',
219 },
220 {
221 .name = "no-indirect",
222 .val = 'i',
223 },
64d09888
MT
224 {
225 .name = "delayed-interrupt",
226 .val = 'D',
227 },
228 {
229 .name = "no-delayed-interrupt",
230 .val = 'd',
231 },
4e53f78e
MT
232 {
233 }
234};
235
4a7d6455 236static void help(void)
4e53f78e 237{
4423fe40
MT
238 fprintf(stderr, "Usage: virtio_test [--help]"
239 " [--no-indirect]"
240 " [--no-event-idx]"
64d09888 241 " [--delayed-interrupt]"
4423fe40 242 "\n");
4e53f78e
MT
243}
244
245int main(int argc, char **argv)
246{
247 struct vdev_info dev;
4423fe40
MT
248 unsigned long long features = (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
249 (1ULL << VIRTIO_RING_F_EVENT_IDX);
4e53f78e 250 int o;
64d09888 251 bool delayed = false;
4e53f78e
MT
252
253 for (;;) {
254 o = getopt_long(argc, argv, optstring, longopts, NULL);
255 switch (o) {
256 case -1:
257 goto done;
258 case '?':
259 help();
260 exit(2);
4423fe40
MT
261 case 'e':
262 features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
263 break;
4e53f78e
MT
264 case 'h':
265 help();
266 goto done;
267 case 'i':
268 features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
269 break;
64d09888
MT
270 case 'D':
271 delayed = true;
272 break;
4e53f78e
MT
273 default:
274 assert(0);
275 break;
276 }
277 }
278
279done:
280 vdev_info_init(&dev, features);
281 vq_info_add(&dev, 256);
64d09888 282 run_test(&dev, &dev.vqs[0], delayed, 0x100000);
4e53f78e
MT
283 return 0;
284}
This page took 0.1905 seconds and 5 git commands to generate.