blob: 82902fc3ba2ae52274189e0607ea2d5e0fa23924 [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 };
Eugenio Pérez1d8bf5c2020-04-18 12:22:16 +020051static const struct vhost_vring_state null_state = {};
Eugenio Pérez264ee5a2020-04-18 12:22:13 +020052
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +103053bool vq_notify(struct virtqueue *vq)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +020054{
55 struct vq_info *info = vq->priv;
56 unsigned long long v = 1;
57 int r;
58 r = write(info->kick, &v, sizeof v);
59 assert(r == sizeof v);
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +103060 return true;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +020061}
62
63void vq_callback(struct virtqueue *vq)
64{
65}
66
67
68void vhost_vq_setup(struct vdev_info *dev, struct vq_info *info)
69{
70 struct vhost_vring_state state = { .index = info->idx };
71 struct vhost_vring_file file = { .index = info->idx };
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +020072 unsigned long long features = dev->vdev.features;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +020073 struct vhost_vring_addr addr = {
74 .index = info->idx,
75 .desc_user_addr = (uint64_t)(unsigned long)info->vring.desc,
76 .avail_user_addr = (uint64_t)(unsigned long)info->vring.avail,
77 .used_user_addr = (uint64_t)(unsigned long)info->vring.used,
78 };
79 int r;
80 r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
81 assert(r >= 0);
82 state.num = info->vring.num;
83 r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
84 assert(r >= 0);
85 state.num = 0;
86 r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
87 assert(r >= 0);
88 r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
89 assert(r >= 0);
90 file.fd = info->kick;
91 r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
92 assert(r >= 0);
93 file.fd = info->call;
94 r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
95 assert(r >= 0);
96}
97
Eugenio Pérez67412392020-04-18 12:22:15 +020098static void vq_reset(struct vq_info *info, int num, struct virtio_device *vdev)
99{
100 if (info->vq)
101 vring_del_virtqueue(info->vq);
102
103 memset(info->ring, 0, vring_size(num, 4096));
104 vring_init(&info->vring, num, info->ring, 4096);
105 info->vq = __vring_new_virtqueue(info->idx, info->vring, vdev, true,
106 false, vq_notify, vq_callback, "test");
107 assert(info->vq);
108 info->vq->priv = info;
109}
110
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200111static void vq_info_add(struct vdev_info *dev, int num)
112{
113 struct vq_info *info = &dev->vqs[dev->nvqs];
114 int r;
115 info->idx = dev->nvqs;
116 info->kick = eventfd(0, EFD_NONBLOCK);
117 info->call = eventfd(0, EFD_NONBLOCK);
118 r = posix_memalign(&info->ring, 4096, vring_size(num, 4096));
119 assert(r >= 0);
Eugenio Pérez67412392020-04-18 12:22:15 +0200120 vq_reset(info, num, &dev->vdev);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200121 vhost_vq_setup(dev, info);
122 dev->fds[info->idx].fd = info->call;
123 dev->fds[info->idx].events = POLLIN;
124 dev->nvqs++;
125}
126
127static void vdev_info_init(struct vdev_info* dev, unsigned long long features)
128{
129 int r;
130 memset(dev, 0, sizeof *dev);
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +0200131 dev->vdev.features = features;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200132 dev->buf_size = 1024;
133 dev->buf = malloc(dev->buf_size);
134 assert(dev->buf);
135 dev->control = open("/dev/vhost-test", O_RDWR);
136 assert(dev->control >= 0);
137 r = ioctl(dev->control, VHOST_SET_OWNER, NULL);
138 assert(r >= 0);
139 dev->mem = malloc(offsetof(struct vhost_memory, regions) +
140 sizeof dev->mem->regions[0]);
141 assert(dev->mem);
142 memset(dev->mem, 0, offsetof(struct vhost_memory, regions) +
143 sizeof dev->mem->regions[0]);
144 dev->mem->nregions = 1;
145 dev->mem->regions[0].guest_phys_addr = (long)dev->buf;
146 dev->mem->regions[0].userspace_addr = (long)dev->buf;
147 dev->mem->regions[0].memory_size = dev->buf_size;
148 r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
149 assert(r >= 0);
150}
151
152/* TODO: this is pretty bad: we get a cache line bounce
153 * for the wait queue on poll and another one on read,
154 * plus the read which is there just to clear the
155 * current state. */
156static void wait_for_interrupt(struct vdev_info *dev)
157{
158 int i;
159 unsigned long long val;
160 poll(dev->fds, dev->nvqs, -1);
161 for (i = 0; i < dev->nvqs; ++i)
162 if (dev->fds[i].revents & POLLIN) {
163 read(dev->fds[i].fd, &val, sizeof val);
164 }
165}
166
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400167static void run_test(struct vdev_info *dev, struct vq_info *vq,
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200168 bool delayed, int batch, int reset_n, int bufs)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200169{
170 struct scatterlist sl;
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200171 long started = 0, completed = 0, next_reset = reset_n;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200172 long completed_before, started_before;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200173 int r, test = 1;
174 unsigned len;
175 long long spurious = 0;
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200176 const bool random_batch = batch == RANDOM_BATCH;
Eugenio Pérez1d8bf5c2020-04-18 12:22:16 +0200177
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200178 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
179 assert(r >= 0);
Eugenio Pérez1d8bf5c2020-04-18 12:22:16 +0200180 if (!reset_n) {
181 next_reset = INT_MAX;
182 }
183
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200184 for (;;) {
185 virtqueue_disable_cb(vq->vq);
186 completed_before = completed;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200187 started_before = started;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200188 do {
Eugenio Pérez1d8bf5c2020-04-18 12:22:16 +0200189 const bool reset = completed > next_reset;
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200190 if (random_batch)
191 batch = (random() % vq->vring.num) + 1;
192
Eugenio Pérez633fae32020-04-18 12:22:11 +0200193 while (started < bufs &&
194 (started - completed) < batch) {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200195 sg_init_one(&sl, dev->buf, dev->buf_size);
Rusty Russellcf994e02013-03-20 15:44:30 +1030196 r = virtqueue_add_outbuf(vq->vq, &sl, 1,
197 dev->buf + started,
198 GFP_ATOMIC);
Eugenio Pérez633fae32020-04-18 12:22:11 +0200199 if (unlikely(r != 0)) {
200 if (r == -ENOSPC &&
201 started > started_before)
202 r = 0;
203 else
Heinz Graalfs53c18c92013-10-29 09:40:11 +1030204 r = -1;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200205 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200206 }
Eugenio Pérez633fae32020-04-18 12:22:11 +0200207
208 ++started;
209
210 if (unlikely(!virtqueue_kick(vq->vq))) {
211 r = -1;
212 break;
213 }
214 }
215
216 if (started >= bufs)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200217 r = -1;
218
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200219 if (reset) {
220 r = ioctl(dev->control, VHOST_TEST_SET_BACKEND,
221 &no_backend);
222 assert(!r);
223 }
224
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200225 /* Flush out completed bufs if any */
Eugenio Pérez633fae32020-04-18 12:22:11 +0200226 while (virtqueue_get_buf(vq->vq, &len)) {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200227 ++completed;
228 r = 0;
229 }
230
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200231 if (reset) {
Eugenio Pérez1d8bf5c2020-04-18 12:22:16 +0200232 struct vhost_vring_state s = { .index = 0 };
233
234 vq_reset(vq, vq->vring.num, &dev->vdev);
235
236 r = ioctl(dev->control, VHOST_GET_VRING_BASE,
237 &s);
238 assert(!r);
239
240 s.num = 0;
241 r = ioctl(dev->control, VHOST_SET_VRING_BASE,
242 &null_state);
243 assert(!r);
244
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200245 r = ioctl(dev->control, VHOST_TEST_SET_BACKEND,
246 &backend);
247 assert(!r);
248
Eugenio Pérez1d8bf5c2020-04-18 12:22:16 +0200249 started = completed;
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200250 while (completed > next_reset)
251 next_reset += completed;
252 }
Rusty Russellde929b02012-10-16 23:56:16 +1030253 } while (r == 0);
Eugenio Pérez633fae32020-04-18 12:22:11 +0200254 if (completed == completed_before && started == started_before)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200255 ++spurious;
256 assert(completed <= bufs);
257 assert(started <= bufs);
258 if (completed == bufs)
259 break;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400260 if (delayed) {
261 if (virtqueue_enable_cb_delayed(vq->vq))
262 wait_for_interrupt(dev);
263 } else {
264 if (virtqueue_enable_cb(vq->vq))
265 wait_for_interrupt(dev);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200266 }
267 }
268 test = 0;
269 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
270 assert(r >= 0);
Eugenio Pérez1d8bf5c2020-04-18 12:22:16 +0200271 fprintf(stderr,
272 "spurious wakeups: 0x%llx started=0x%lx completed=0x%lx\n",
273 spurious, started, completed);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200274}
275
276const char optstring[] = "h";
277const struct option longopts[] = {
278 {
279 .name = "help",
280 .val = 'h',
281 },
282 {
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300283 .name = "event-idx",
284 .val = 'E',
285 },
286 {
287 .name = "no-event-idx",
288 .val = 'e',
289 },
290 {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200291 .name = "indirect",
292 .val = 'I',
293 },
294 {
295 .name = "no-indirect",
296 .val = 'i',
297 },
298 {
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200299 .name = "virtio-1",
300 .val = '1',
301 },
302 {
303 .name = "no-virtio-1",
304 .val = '0',
305 },
306 {
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400307 .name = "delayed-interrupt",
308 .val = 'D',
309 },
310 {
311 .name = "no-delayed-interrupt",
312 .val = 'd',
313 },
314 {
Eugenio Pérez633fae32020-04-18 12:22:11 +0200315 .name = "batch",
316 .val = 'b',
317 .has_arg = required_argument,
318 },
319 {
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200320 .name = "reset",
321 .val = 'r',
322 .has_arg = optional_argument,
323 },
324 {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200325 }
326};
327
Cong Ding4a7d6452012-12-03 10:24:54 +0000328static void help(void)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200329{
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300330 fprintf(stderr, "Usage: virtio_test [--help]"
331 " [--no-indirect]"
332 " [--no-event-idx]"
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200333 " [--no-virtio-1]"
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400334 " [--delayed-interrupt]"
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200335 " [--batch=random/N]"
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200336 " [--reset=N]"
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300337 "\n");
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200338}
339
340int main(int argc, char **argv)
341{
342 struct vdev_info dev;
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300343 unsigned long long features = (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200344 (1ULL << VIRTIO_RING_F_EVENT_IDX) | (1ULL << VIRTIO_F_VERSION_1);
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200345 long batch = 1, reset = 0;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200346 int o;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400347 bool delayed = false;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200348
349 for (;;) {
350 o = getopt_long(argc, argv, optstring, longopts, NULL);
351 switch (o) {
352 case -1:
353 goto done;
354 case '?':
355 help();
356 exit(2);
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300357 case 'e':
358 features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
359 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200360 case 'h':
361 help();
362 goto done;
363 case 'i':
364 features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
365 break;
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200366 case '0':
367 features &= ~(1ULL << VIRTIO_F_VERSION_1);
368 break;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400369 case 'D':
370 delayed = true;
371 break;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200372 case 'b':
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200373 if (0 == strcmp(optarg, "random")) {
374 batch = RANDOM_BATCH;
375 } else {
376 batch = strtol(optarg, NULL, 10);
377 assert(batch > 0);
378 assert(batch < (long)INT_MAX + 1);
379 }
Eugenio Pérez633fae32020-04-18 12:22:11 +0200380 break;
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200381 case 'r':
382 if (!optarg) {
383 reset = 1;
384 } else {
385 reset = strtol(optarg, NULL, 10);
386 assert(reset > 0);
387 assert(reset < (long)INT_MAX + 1);
388 }
389 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200390 default:
391 assert(0);
392 break;
393 }
394 }
395
396done:
397 vdev_info_init(&dev, features);
398 vq_info_add(&dev, 256);
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200399 run_test(&dev, &dev.vqs[0], delayed, batch, reset, 0x100000);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200400 return 0;
401}