blob: da7a19558281637789bd932614861cc10c15d692 [file] [log] [blame]
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +02001#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>
Rusty Russell61d0b5a2013-03-18 13:22:19 +103013#include <stdbool.h>
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +020014#include <linux/vhost.h>
15#include <linux/virtio.h>
16#include <linux/virtio_ring.h>
17#include "../../drivers/vhost/test.h"
18
Rusty Russell61d0b5a2013-03-18 13:22:19 +103019/* Unused */
20void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
21
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +020022struct vq_info {
23 int kick;
24 int call;
25 int num;
26 int idx;
27 void *ring;
28 /* copy used for control */
29 struct vring vring;
30 struct virtqueue *vq;
31};
32
33struct vdev_info {
34 struct virtio_device vdev;
35 int control;
36 struct pollfd fds[1];
37 struct vq_info vqs[1];
38 int nvqs;
39 void *buf;
40 size_t buf_size;
41 struct vhost_memory *mem;
42};
43
44void vq_notify(struct virtqueue *vq)
45{
46 struct vq_info *info = vq->priv;
47 unsigned long long v = 1;
48 int r;
49 r = write(info->kick, &v, sizeof v);
50 assert(r == sizeof v);
51}
52
53void vq_callback(struct virtqueue *vq)
54{
55}
56
57
58void vhost_vq_setup(struct vdev_info *dev, struct vq_info *info)
59{
60 struct vhost_vring_state state = { .index = info->idx };
61 struct vhost_vring_file file = { .index = info->idx };
62 unsigned long long features = dev->vdev.features[0];
63 struct vhost_vring_addr addr = {
64 .index = info->idx,
65 .desc_user_addr = (uint64_t)(unsigned long)info->vring.desc,
66 .avail_user_addr = (uint64_t)(unsigned long)info->vring.avail,
67 .used_user_addr = (uint64_t)(unsigned long)info->vring.used,
68 };
69 int r;
70 r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
71 assert(r >= 0);
72 state.num = info->vring.num;
73 r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
74 assert(r >= 0);
75 state.num = 0;
76 r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
77 assert(r >= 0);
78 r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
79 assert(r >= 0);
80 file.fd = info->kick;
81 r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
82 assert(r >= 0);
83 file.fd = info->call;
84 r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
85 assert(r >= 0);
86}
87
88static void vq_info_add(struct vdev_info *dev, int num)
89{
90 struct vq_info *info = &dev->vqs[dev->nvqs];
91 int r;
92 info->idx = dev->nvqs;
93 info->kick = eventfd(0, EFD_NONBLOCK);
94 info->call = eventfd(0, EFD_NONBLOCK);
95 r = posix_memalign(&info->ring, 4096, vring_size(num, 4096));
96 assert(r >= 0);
97 memset(info->ring, 0, vring_size(num, 4096));
98 vring_init(&info->vring, num, info->ring, 4096);
Michael S. Tsirkin73640c92013-03-18 13:22:18 +103099 info->vq = vring_new_virtqueue(info->idx,
100 info->vring.num, 4096, &dev->vdev,
Rusty Russell7b21e342012-01-12 15:44:42 +1030101 true, info->ring,
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200102 vq_notify, vq_callback, "test");
103 assert(info->vq);
104 info->vq->priv = info;
105 vhost_vq_setup(dev, info);
106 dev->fds[info->idx].fd = info->call;
107 dev->fds[info->idx].events = POLLIN;
108 dev->nvqs++;
109}
110
111static void vdev_info_init(struct vdev_info* dev, unsigned long long features)
112{
113 int r;
114 memset(dev, 0, sizeof *dev);
115 dev->vdev.features[0] = features;
116 dev->vdev.features[1] = features >> 32;
117 dev->buf_size = 1024;
118 dev->buf = malloc(dev->buf_size);
119 assert(dev->buf);
120 dev->control = open("/dev/vhost-test", O_RDWR);
121 assert(dev->control >= 0);
122 r = ioctl(dev->control, VHOST_SET_OWNER, NULL);
123 assert(r >= 0);
124 dev->mem = malloc(offsetof(struct vhost_memory, regions) +
125 sizeof dev->mem->regions[0]);
126 assert(dev->mem);
127 memset(dev->mem, 0, offsetof(struct vhost_memory, regions) +
128 sizeof dev->mem->regions[0]);
129 dev->mem->nregions = 1;
130 dev->mem->regions[0].guest_phys_addr = (long)dev->buf;
131 dev->mem->regions[0].userspace_addr = (long)dev->buf;
132 dev->mem->regions[0].memory_size = dev->buf_size;
133 r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
134 assert(r >= 0);
135}
136
137/* TODO: this is pretty bad: we get a cache line bounce
138 * for the wait queue on poll and another one on read,
139 * plus the read which is there just to clear the
140 * current state. */
141static void wait_for_interrupt(struct vdev_info *dev)
142{
143 int i;
144 unsigned long long val;
145 poll(dev->fds, dev->nvqs, -1);
146 for (i = 0; i < dev->nvqs; ++i)
147 if (dev->fds[i].revents & POLLIN) {
148 read(dev->fds[i].fd, &val, sizeof val);
149 }
150}
151
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400152static void run_test(struct vdev_info *dev, struct vq_info *vq,
153 bool delayed, int bufs)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200154{
155 struct scatterlist sl;
156 long started = 0, completed = 0;
157 long completed_before;
158 int r, test = 1;
159 unsigned len;
160 long long spurious = 0;
161 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
162 assert(r >= 0);
163 for (;;) {
164 virtqueue_disable_cb(vq->vq);
165 completed_before = completed;
166 do {
167 if (started < bufs) {
168 sg_init_one(&sl, dev->buf, dev->buf_size);
Rusty Russellcf994e02013-03-20 15:44:30 +1030169 r = virtqueue_add_outbuf(vq->vq, &sl, 1,
170 dev->buf + started,
171 GFP_ATOMIC);
Rusty Russellde929b02012-10-16 23:56:16 +1030172 if (likely(r == 0)) {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200173 ++started;
174 virtqueue_kick(vq->vq);
175 }
176 } else
177 r = -1;
178
179 /* Flush out completed bufs if any */
180 if (virtqueue_get_buf(vq->vq, &len)) {
181 ++completed;
182 r = 0;
183 }
184
Rusty Russellde929b02012-10-16 23:56:16 +1030185 } while (r == 0);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200186 if (completed == completed_before)
187 ++spurious;
188 assert(completed <= bufs);
189 assert(started <= bufs);
190 if (completed == bufs)
191 break;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400192 if (delayed) {
193 if (virtqueue_enable_cb_delayed(vq->vq))
194 wait_for_interrupt(dev);
195 } else {
196 if (virtqueue_enable_cb(vq->vq))
197 wait_for_interrupt(dev);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200198 }
199 }
200 test = 0;
201 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
202 assert(r >= 0);
203 fprintf(stderr, "spurious wakeus: 0x%llx\n", spurious);
204}
205
206const char optstring[] = "h";
207const struct option longopts[] = {
208 {
209 .name = "help",
210 .val = 'h',
211 },
212 {
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300213 .name = "event-idx",
214 .val = 'E',
215 },
216 {
217 .name = "no-event-idx",
218 .val = 'e',
219 },
220 {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200221 .name = "indirect",
222 .val = 'I',
223 },
224 {
225 .name = "no-indirect",
226 .val = 'i',
227 },
228 {
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400229 .name = "delayed-interrupt",
230 .val = 'D',
231 },
232 {
233 .name = "no-delayed-interrupt",
234 .val = 'd',
235 },
236 {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200237 }
238};
239
Cong Ding4a7d6452012-12-03 10:24:54 +0000240static void help(void)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200241{
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300242 fprintf(stderr, "Usage: virtio_test [--help]"
243 " [--no-indirect]"
244 " [--no-event-idx]"
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400245 " [--delayed-interrupt]"
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300246 "\n");
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200247}
248
249int main(int argc, char **argv)
250{
251 struct vdev_info dev;
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300252 unsigned long long features = (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
253 (1ULL << VIRTIO_RING_F_EVENT_IDX);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200254 int o;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400255 bool delayed = false;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200256
257 for (;;) {
258 o = getopt_long(argc, argv, optstring, longopts, NULL);
259 switch (o) {
260 case -1:
261 goto done;
262 case '?':
263 help();
264 exit(2);
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300265 case 'e':
266 features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
267 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200268 case 'h':
269 help();
270 goto done;
271 case 'i':
272 features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
273 break;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400274 case 'D':
275 delayed = true;
276 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200277 default:
278 assert(0);
279 break;
280 }
281 }
282
283done:
284 vdev_info_init(&dev, features);
285 vq_info_add(&dev, 256);
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400286 run_test(&dev, &dev.vqs[0], delayed, 0x100000);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200287 return 0;
288}