blob: bc16c818bda3a82d93f5c241fa4b2ecca046cfa9 [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
Eugenio Pérez67412392020-04-18 12:22:15 +020097static void vq_reset(struct vq_info *info, int num, struct virtio_device *vdev)
98{
99 if (info->vq)
100 vring_del_virtqueue(info->vq);
101
102 memset(info->ring, 0, vring_size(num, 4096));
103 vring_init(&info->vring, num, info->ring, 4096);
104 info->vq = __vring_new_virtqueue(info->idx, info->vring, vdev, true,
105 false, vq_notify, vq_callback, "test");
106 assert(info->vq);
107 info->vq->priv = info;
108}
109
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200110static void vq_info_add(struct vdev_info *dev, int num)
111{
112 struct vq_info *info = &dev->vqs[dev->nvqs];
113 int r;
114 info->idx = dev->nvqs;
115 info->kick = eventfd(0, EFD_NONBLOCK);
116 info->call = eventfd(0, EFD_NONBLOCK);
117 r = posix_memalign(&info->ring, 4096, vring_size(num, 4096));
118 assert(r >= 0);
Eugenio Pérez67412392020-04-18 12:22:15 +0200119 vq_reset(info, num, &dev->vdev);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200120 vhost_vq_setup(dev, info);
121 dev->fds[info->idx].fd = info->call;
122 dev->fds[info->idx].events = POLLIN;
123 dev->nvqs++;
124}
125
126static void vdev_info_init(struct vdev_info* dev, unsigned long long features)
127{
128 int r;
129 memset(dev, 0, sizeof *dev);
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +0200130 dev->vdev.features = features;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200131 dev->buf_size = 1024;
132 dev->buf = malloc(dev->buf_size);
133 assert(dev->buf);
134 dev->control = open("/dev/vhost-test", O_RDWR);
135 assert(dev->control >= 0);
136 r = ioctl(dev->control, VHOST_SET_OWNER, NULL);
137 assert(r >= 0);
138 dev->mem = malloc(offsetof(struct vhost_memory, regions) +
139 sizeof dev->mem->regions[0]);
140 assert(dev->mem);
141 memset(dev->mem, 0, offsetof(struct vhost_memory, regions) +
142 sizeof dev->mem->regions[0]);
143 dev->mem->nregions = 1;
144 dev->mem->regions[0].guest_phys_addr = (long)dev->buf;
145 dev->mem->regions[0].userspace_addr = (long)dev->buf;
146 dev->mem->regions[0].memory_size = dev->buf_size;
147 r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
148 assert(r >= 0);
149}
150
151/* TODO: this is pretty bad: we get a cache line bounce
152 * for the wait queue on poll and another one on read,
153 * plus the read which is there just to clear the
154 * current state. */
155static void wait_for_interrupt(struct vdev_info *dev)
156{
157 int i;
158 unsigned long long val;
159 poll(dev->fds, dev->nvqs, -1);
160 for (i = 0; i < dev->nvqs; ++i)
161 if (dev->fds[i].revents & POLLIN) {
162 read(dev->fds[i].fd, &val, sizeof val);
163 }
164}
165
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400166static void run_test(struct vdev_info *dev, struct vq_info *vq,
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200167 bool delayed, int batch, int reset_n, int bufs)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200168{
169 struct scatterlist sl;
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200170 long started = 0, completed = 0, next_reset = reset_n;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200171 long completed_before, started_before;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200172 int r, test = 1;
173 unsigned len;
174 long long spurious = 0;
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200175 const bool random_batch = batch == RANDOM_BATCH;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200176 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
177 assert(r >= 0);
178 for (;;) {
179 virtqueue_disable_cb(vq->vq);
180 completed_before = completed;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200181 started_before = started;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200182 do {
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200183 const bool reset = reset_n && completed > next_reset;
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200184 if (random_batch)
185 batch = (random() % vq->vring.num) + 1;
186
Eugenio Pérez633fae32020-04-18 12:22:11 +0200187 while (started < bufs &&
188 (started - completed) < batch) {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200189 sg_init_one(&sl, dev->buf, dev->buf_size);
Rusty Russellcf994e02013-03-20 15:44:30 +1030190 r = virtqueue_add_outbuf(vq->vq, &sl, 1,
191 dev->buf + started,
192 GFP_ATOMIC);
Eugenio Pérez633fae32020-04-18 12:22:11 +0200193 if (unlikely(r != 0)) {
194 if (r == -ENOSPC &&
195 started > started_before)
196 r = 0;
197 else
Heinz Graalfs53c18c92013-10-29 09:40:11 +1030198 r = -1;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200199 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200200 }
Eugenio Pérez633fae32020-04-18 12:22:11 +0200201
202 ++started;
203
204 if (unlikely(!virtqueue_kick(vq->vq))) {
205 r = -1;
206 break;
207 }
208 }
209
210 if (started >= bufs)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200211 r = -1;
212
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200213 if (reset) {
214 r = ioctl(dev->control, VHOST_TEST_SET_BACKEND,
215 &no_backend);
216 assert(!r);
217 }
218
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200219 /* Flush out completed bufs if any */
Eugenio Pérez633fae32020-04-18 12:22:11 +0200220 while (virtqueue_get_buf(vq->vq, &len)) {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200221 ++completed;
222 r = 0;
223 }
224
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200225 if (reset) {
226 r = ioctl(dev->control, VHOST_TEST_SET_BACKEND,
227 &backend);
228 assert(!r);
229
230 while (completed > next_reset)
231 next_reset += completed;
232 }
Rusty Russellde929b02012-10-16 23:56:16 +1030233 } while (r == 0);
Eugenio Pérez633fae32020-04-18 12:22:11 +0200234 if (completed == completed_before && started == started_before)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200235 ++spurious;
236 assert(completed <= bufs);
237 assert(started <= bufs);
238 if (completed == bufs)
239 break;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400240 if (delayed) {
241 if (virtqueue_enable_cb_delayed(vq->vq))
242 wait_for_interrupt(dev);
243 } else {
244 if (virtqueue_enable_cb(vq->vq))
245 wait_for_interrupt(dev);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200246 }
247 }
248 test = 0;
249 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
250 assert(r >= 0);
Colin Ian King96fb20c2017-04-29 23:14:21 +0100251 fprintf(stderr, "spurious wakeups: 0x%llx\n", spurious);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200252}
253
254const char optstring[] = "h";
255const struct option longopts[] = {
256 {
257 .name = "help",
258 .val = 'h',
259 },
260 {
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300261 .name = "event-idx",
262 .val = 'E',
263 },
264 {
265 .name = "no-event-idx",
266 .val = 'e',
267 },
268 {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200269 .name = "indirect",
270 .val = 'I',
271 },
272 {
273 .name = "no-indirect",
274 .val = 'i',
275 },
276 {
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200277 .name = "virtio-1",
278 .val = '1',
279 },
280 {
281 .name = "no-virtio-1",
282 .val = '0',
283 },
284 {
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400285 .name = "delayed-interrupt",
286 .val = 'D',
287 },
288 {
289 .name = "no-delayed-interrupt",
290 .val = 'd',
291 },
292 {
Eugenio Pérez633fae32020-04-18 12:22:11 +0200293 .name = "batch",
294 .val = 'b',
295 .has_arg = required_argument,
296 },
297 {
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200298 .name = "reset",
299 .val = 'r',
300 .has_arg = optional_argument,
301 },
302 {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200303 }
304};
305
Cong Ding4a7d6452012-12-03 10:24:54 +0000306static void help(void)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200307{
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300308 fprintf(stderr, "Usage: virtio_test [--help]"
309 " [--no-indirect]"
310 " [--no-event-idx]"
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200311 " [--no-virtio-1]"
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400312 " [--delayed-interrupt]"
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200313 " [--batch=random/N]"
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200314 " [--reset=N]"
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300315 "\n");
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200316}
317
318int main(int argc, char **argv)
319{
320 struct vdev_info dev;
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300321 unsigned long long features = (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200322 (1ULL << VIRTIO_RING_F_EVENT_IDX) | (1ULL << VIRTIO_F_VERSION_1);
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200323 long batch = 1, reset = 0;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200324 int o;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400325 bool delayed = false;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200326
327 for (;;) {
328 o = getopt_long(argc, argv, optstring, longopts, NULL);
329 switch (o) {
330 case -1:
331 goto done;
332 case '?':
333 help();
334 exit(2);
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300335 case 'e':
336 features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
337 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200338 case 'h':
339 help();
340 goto done;
341 case 'i':
342 features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
343 break;
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200344 case '0':
345 features &= ~(1ULL << VIRTIO_F_VERSION_1);
346 break;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400347 case 'D':
348 delayed = true;
349 break;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200350 case 'b':
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200351 if (0 == strcmp(optarg, "random")) {
352 batch = RANDOM_BATCH;
353 } else {
354 batch = strtol(optarg, NULL, 10);
355 assert(batch > 0);
356 assert(batch < (long)INT_MAX + 1);
357 }
Eugenio Pérez633fae32020-04-18 12:22:11 +0200358 break;
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200359 case 'r':
360 if (!optarg) {
361 reset = 1;
362 } else {
363 reset = strtol(optarg, NULL, 10);
364 assert(reset > 0);
365 assert(reset < (long)INT_MAX + 1);
366 }
367 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200368 default:
369 assert(0);
370 break;
371 }
372 }
373
374done:
375 vdev_info_init(&dev, features);
376 vq_info_add(&dev, 256);
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200377 run_test(&dev, &dev.vqs[0], delayed, batch, reset, 0x100000);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200378 return 0;
379}