blob: 6bc3e172cc9ba4dc36b48d7884bcdec7b8da8dda [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +02002#define _GNU_SOURCE
3#include <getopt.h>
Eugenio Pérez633fae32020-04-18 12:22:11 +02004#include <limits.h>
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +02005#include <string.h>
6#include <poll.h>
7#include <sys/eventfd.h>
8#include <stdlib.h>
9#include <assert.h>
10#include <unistd.h>
11#include <sys/ioctl.h>
12#include <sys/stat.h>
13#include <sys/types.h>
14#include <fcntl.h>
Rusty Russell61d0b5a2013-03-18 13:22:19 +103015#include <stdbool.h>
Michael S. Tsirkin2d7ce0e2014-12-15 23:47:07 +020016#include <linux/virtio_types.h>
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +020017#include <linux/vhost.h>
18#include <linux/virtio.h>
19#include <linux/virtio_ring.h>
20#include "../../drivers/vhost/test.h"
21
Eugenio Pérez7add78b2020-04-18 12:22:12 +020022#define RANDOM_BATCH -1
23
Rusty Russell61d0b5a2013-03-18 13:22:19 +103024/* Unused */
25void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
26
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +020027struct vq_info {
28 int kick;
29 int call;
30 int num;
31 int idx;
32 void *ring;
33 /* copy used for control */
34 struct vring vring;
35 struct virtqueue *vq;
36};
37
38struct vdev_info {
39 struct virtio_device vdev;
40 int control;
41 struct pollfd fds[1];
42 struct vq_info vqs[1];
43 int nvqs;
44 void *buf;
45 size_t buf_size;
46 struct vhost_memory *mem;
47};
48
Eugenio Pérez264ee5a2020-04-18 12:22:13 +020049static const struct vhost_vring_file no_backend = { .fd = -1 },
50 backend = { .fd = 1 };
51
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +103052bool vq_notify(struct virtqueue *vq)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +020053{
54 struct vq_info *info = vq->priv;
55 unsigned long long v = 1;
56 int r;
57 r = write(info->kick, &v, sizeof v);
58 assert(r == sizeof v);
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +103059 return true;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +020060}
61
62void vq_callback(struct virtqueue *vq)
63{
64}
65
66
67void vhost_vq_setup(struct vdev_info *dev, struct vq_info *info)
68{
69 struct vhost_vring_state state = { .index = info->idx };
70 struct vhost_vring_file file = { .index = info->idx };
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +020071 unsigned long long features = dev->vdev.features;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +020072 struct vhost_vring_addr addr = {
73 .index = info->idx,
74 .desc_user_addr = (uint64_t)(unsigned long)info->vring.desc,
75 .avail_user_addr = (uint64_t)(unsigned long)info->vring.avail,
76 .used_user_addr = (uint64_t)(unsigned long)info->vring.used,
77 };
78 int r;
79 r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
80 assert(r >= 0);
81 state.num = info->vring.num;
82 r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
83 assert(r >= 0);
84 state.num = 0;
85 r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
86 assert(r >= 0);
87 r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
88 assert(r >= 0);
89 file.fd = info->kick;
90 r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
91 assert(r >= 0);
92 file.fd = info->call;
93 r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
94 assert(r >= 0);
95}
96
97static void vq_info_add(struct vdev_info *dev, int num)
98{
99 struct vq_info *info = &dev->vqs[dev->nvqs];
100 int r;
101 info->idx = dev->nvqs;
102 info->kick = eventfd(0, EFD_NONBLOCK);
103 info->call = eventfd(0, EFD_NONBLOCK);
104 r = posix_memalign(&info->ring, 4096, vring_size(num, 4096));
105 assert(r >= 0);
106 memset(info->ring, 0, vring_size(num, 4096));
107 vring_init(&info->vring, num, info->ring, 4096);
Michael S. Tsirkin73640c92013-03-18 13:22:18 +1030108 info->vq = vring_new_virtqueue(info->idx,
109 info->vring.num, 4096, &dev->vdev,
Sekhar Nori0a12ae42017-04-17 16:42:15 +0530110 true, false, info->ring,
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200111 vq_notify, vq_callback, "test");
112 assert(info->vq);
113 info->vq->priv = info;
114 vhost_vq_setup(dev, info);
115 dev->fds[info->idx].fd = info->call;
116 dev->fds[info->idx].events = POLLIN;
117 dev->nvqs++;
118}
119
120static void vdev_info_init(struct vdev_info* dev, unsigned long long features)
121{
122 int r;
123 memset(dev, 0, sizeof *dev);
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +0200124 dev->vdev.features = features;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200125 dev->buf_size = 1024;
126 dev->buf = malloc(dev->buf_size);
127 assert(dev->buf);
128 dev->control = open("/dev/vhost-test", O_RDWR);
129 assert(dev->control >= 0);
130 r = ioctl(dev->control, VHOST_SET_OWNER, NULL);
131 assert(r >= 0);
132 dev->mem = malloc(offsetof(struct vhost_memory, regions) +
133 sizeof dev->mem->regions[0]);
134 assert(dev->mem);
135 memset(dev->mem, 0, offsetof(struct vhost_memory, regions) +
136 sizeof dev->mem->regions[0]);
137 dev->mem->nregions = 1;
138 dev->mem->regions[0].guest_phys_addr = (long)dev->buf;
139 dev->mem->regions[0].userspace_addr = (long)dev->buf;
140 dev->mem->regions[0].memory_size = dev->buf_size;
141 r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
142 assert(r >= 0);
143}
144
145/* TODO: this is pretty bad: we get a cache line bounce
146 * for the wait queue on poll and another one on read,
147 * plus the read which is there just to clear the
148 * current state. */
149static void wait_for_interrupt(struct vdev_info *dev)
150{
151 int i;
152 unsigned long long val;
153 poll(dev->fds, dev->nvqs, -1);
154 for (i = 0; i < dev->nvqs; ++i)
155 if (dev->fds[i].revents & POLLIN) {
156 read(dev->fds[i].fd, &val, sizeof val);
157 }
158}
159
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400160static void run_test(struct vdev_info *dev, struct vq_info *vq,
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200161 bool delayed, int batch, int reset_n, int bufs)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200162{
163 struct scatterlist sl;
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200164 long started = 0, completed = 0, next_reset = reset_n;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200165 long completed_before, started_before;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200166 int r, test = 1;
167 unsigned len;
168 long long spurious = 0;
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200169 const bool random_batch = batch == RANDOM_BATCH;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200170 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
171 assert(r >= 0);
172 for (;;) {
173 virtqueue_disable_cb(vq->vq);
174 completed_before = completed;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200175 started_before = started;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200176 do {
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200177 const bool reset = reset_n && completed > next_reset;
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200178 if (random_batch)
179 batch = (random() % vq->vring.num) + 1;
180
Eugenio Pérez633fae32020-04-18 12:22:11 +0200181 while (started < bufs &&
182 (started - completed) < batch) {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200183 sg_init_one(&sl, dev->buf, dev->buf_size);
Rusty Russellcf994e02013-03-20 15:44:30 +1030184 r = virtqueue_add_outbuf(vq->vq, &sl, 1,
185 dev->buf + started,
186 GFP_ATOMIC);
Eugenio Pérez633fae32020-04-18 12:22:11 +0200187 if (unlikely(r != 0)) {
188 if (r == -ENOSPC &&
189 started > started_before)
190 r = 0;
191 else
Heinz Graalfs53c18c92013-10-29 09:40:11 +1030192 r = -1;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200193 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200194 }
Eugenio Pérez633fae32020-04-18 12:22:11 +0200195
196 ++started;
197
198 if (unlikely(!virtqueue_kick(vq->vq))) {
199 r = -1;
200 break;
201 }
202 }
203
204 if (started >= bufs)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200205 r = -1;
206
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200207 if (reset) {
208 r = ioctl(dev->control, VHOST_TEST_SET_BACKEND,
209 &no_backend);
210 assert(!r);
211 }
212
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200213 /* Flush out completed bufs if any */
Eugenio Pérez633fae32020-04-18 12:22:11 +0200214 while (virtqueue_get_buf(vq->vq, &len)) {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200215 ++completed;
216 r = 0;
217 }
218
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200219 if (reset) {
220 r = ioctl(dev->control, VHOST_TEST_SET_BACKEND,
221 &backend);
222 assert(!r);
223
224 while (completed > next_reset)
225 next_reset += completed;
226 }
Rusty Russellde929b02012-10-16 23:56:16 +1030227 } while (r == 0);
Eugenio Pérez633fae32020-04-18 12:22:11 +0200228 if (completed == completed_before && started == started_before)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200229 ++spurious;
230 assert(completed <= bufs);
231 assert(started <= bufs);
232 if (completed == bufs)
233 break;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400234 if (delayed) {
235 if (virtqueue_enable_cb_delayed(vq->vq))
236 wait_for_interrupt(dev);
237 } else {
238 if (virtqueue_enable_cb(vq->vq))
239 wait_for_interrupt(dev);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200240 }
241 }
242 test = 0;
243 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
244 assert(r >= 0);
Colin Ian King96fb20c2017-04-29 23:14:21 +0100245 fprintf(stderr, "spurious wakeups: 0x%llx\n", spurious);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200246}
247
248const char optstring[] = "h";
249const struct option longopts[] = {
250 {
251 .name = "help",
252 .val = 'h',
253 },
254 {
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300255 .name = "event-idx",
256 .val = 'E',
257 },
258 {
259 .name = "no-event-idx",
260 .val = 'e',
261 },
262 {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200263 .name = "indirect",
264 .val = 'I',
265 },
266 {
267 .name = "no-indirect",
268 .val = 'i',
269 },
270 {
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200271 .name = "virtio-1",
272 .val = '1',
273 },
274 {
275 .name = "no-virtio-1",
276 .val = '0',
277 },
278 {
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400279 .name = "delayed-interrupt",
280 .val = 'D',
281 },
282 {
283 .name = "no-delayed-interrupt",
284 .val = 'd',
285 },
286 {
Eugenio Pérez633fae32020-04-18 12:22:11 +0200287 .name = "batch",
288 .val = 'b',
289 .has_arg = required_argument,
290 },
291 {
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200292 .name = "reset",
293 .val = 'r',
294 .has_arg = optional_argument,
295 },
296 {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200297 }
298};
299
Cong Ding4a7d6452012-12-03 10:24:54 +0000300static void help(void)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200301{
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300302 fprintf(stderr, "Usage: virtio_test [--help]"
303 " [--no-indirect]"
304 " [--no-event-idx]"
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200305 " [--no-virtio-1]"
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400306 " [--delayed-interrupt]"
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200307 " [--batch=random/N]"
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200308 " [--reset=N]"
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300309 "\n");
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200310}
311
312int main(int argc, char **argv)
313{
314 struct vdev_info dev;
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300315 unsigned long long features = (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200316 (1ULL << VIRTIO_RING_F_EVENT_IDX) | (1ULL << VIRTIO_F_VERSION_1);
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200317 long batch = 1, reset = 0;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200318 int o;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400319 bool delayed = false;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200320
321 for (;;) {
322 o = getopt_long(argc, argv, optstring, longopts, NULL);
323 switch (o) {
324 case -1:
325 goto done;
326 case '?':
327 help();
328 exit(2);
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300329 case 'e':
330 features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
331 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200332 case 'h':
333 help();
334 goto done;
335 case 'i':
336 features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
337 break;
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200338 case '0':
339 features &= ~(1ULL << VIRTIO_F_VERSION_1);
340 break;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400341 case 'D':
342 delayed = true;
343 break;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200344 case 'b':
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200345 if (0 == strcmp(optarg, "random")) {
346 batch = RANDOM_BATCH;
347 } else {
348 batch = strtol(optarg, NULL, 10);
349 assert(batch > 0);
350 assert(batch < (long)INT_MAX + 1);
351 }
Eugenio Pérez633fae32020-04-18 12:22:11 +0200352 break;
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200353 case 'r':
354 if (!optarg) {
355 reset = 1;
356 } else {
357 reset = strtol(optarg, NULL, 10);
358 assert(reset > 0);
359 assert(reset < (long)INT_MAX + 1);
360 }
361 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200362 default:
363 assert(0);
364 break;
365 }
366 }
367
368done:
369 vdev_info_init(&dev, features);
370 vq_info_add(&dev, 256);
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200371 run_test(&dev, &dev.vqs[0], delayed, batch, reset, 0x100000);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200372 return 0;
373}