blob: 0a247ba3b899e16a172040e4d6c522249d49839c [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);
Eugenio Pérez4cfb9392020-04-18 12:22:14 +0200108 info->vq =
109 __vring_new_virtqueue(info->idx, info->vring, &dev->vdev, true,
110 false, vq_notify, vq_callback, "test");
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200111 assert(info->vq);
112 info->vq->priv = info;
113 vhost_vq_setup(dev, info);
114 dev->fds[info->idx].fd = info->call;
115 dev->fds[info->idx].events = POLLIN;
116 dev->nvqs++;
117}
118
119static void vdev_info_init(struct vdev_info* dev, unsigned long long features)
120{
121 int r;
122 memset(dev, 0, sizeof *dev);
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +0200123 dev->vdev.features = features;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200124 dev->buf_size = 1024;
125 dev->buf = malloc(dev->buf_size);
126 assert(dev->buf);
127 dev->control = open("/dev/vhost-test", O_RDWR);
128 assert(dev->control >= 0);
129 r = ioctl(dev->control, VHOST_SET_OWNER, NULL);
130 assert(r >= 0);
131 dev->mem = malloc(offsetof(struct vhost_memory, regions) +
132 sizeof dev->mem->regions[0]);
133 assert(dev->mem);
134 memset(dev->mem, 0, offsetof(struct vhost_memory, regions) +
135 sizeof dev->mem->regions[0]);
136 dev->mem->nregions = 1;
137 dev->mem->regions[0].guest_phys_addr = (long)dev->buf;
138 dev->mem->regions[0].userspace_addr = (long)dev->buf;
139 dev->mem->regions[0].memory_size = dev->buf_size;
140 r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
141 assert(r >= 0);
142}
143
144/* TODO: this is pretty bad: we get a cache line bounce
145 * for the wait queue on poll and another one on read,
146 * plus the read which is there just to clear the
147 * current state. */
148static void wait_for_interrupt(struct vdev_info *dev)
149{
150 int i;
151 unsigned long long val;
152 poll(dev->fds, dev->nvqs, -1);
153 for (i = 0; i < dev->nvqs; ++i)
154 if (dev->fds[i].revents & POLLIN) {
155 read(dev->fds[i].fd, &val, sizeof val);
156 }
157}
158
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400159static void run_test(struct vdev_info *dev, struct vq_info *vq,
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200160 bool delayed, int batch, int reset_n, int bufs)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200161{
162 struct scatterlist sl;
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200163 long started = 0, completed = 0, next_reset = reset_n;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200164 long completed_before, started_before;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200165 int r, test = 1;
166 unsigned len;
167 long long spurious = 0;
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200168 const bool random_batch = batch == RANDOM_BATCH;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200169 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
170 assert(r >= 0);
171 for (;;) {
172 virtqueue_disable_cb(vq->vq);
173 completed_before = completed;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200174 started_before = started;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200175 do {
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200176 const bool reset = reset_n && completed > next_reset;
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200177 if (random_batch)
178 batch = (random() % vq->vring.num) + 1;
179
Eugenio Pérez633fae32020-04-18 12:22:11 +0200180 while (started < bufs &&
181 (started - completed) < batch) {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200182 sg_init_one(&sl, dev->buf, dev->buf_size);
Rusty Russellcf994e02013-03-20 15:44:30 +1030183 r = virtqueue_add_outbuf(vq->vq, &sl, 1,
184 dev->buf + started,
185 GFP_ATOMIC);
Eugenio Pérez633fae32020-04-18 12:22:11 +0200186 if (unlikely(r != 0)) {
187 if (r == -ENOSPC &&
188 started > started_before)
189 r = 0;
190 else
Heinz Graalfs53c18c92013-10-29 09:40:11 +1030191 r = -1;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200192 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200193 }
Eugenio Pérez633fae32020-04-18 12:22:11 +0200194
195 ++started;
196
197 if (unlikely(!virtqueue_kick(vq->vq))) {
198 r = -1;
199 break;
200 }
201 }
202
203 if (started >= bufs)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200204 r = -1;
205
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200206 if (reset) {
207 r = ioctl(dev->control, VHOST_TEST_SET_BACKEND,
208 &no_backend);
209 assert(!r);
210 }
211
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200212 /* Flush out completed bufs if any */
Eugenio Pérez633fae32020-04-18 12:22:11 +0200213 while (virtqueue_get_buf(vq->vq, &len)) {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200214 ++completed;
215 r = 0;
216 }
217
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200218 if (reset) {
219 r = ioctl(dev->control, VHOST_TEST_SET_BACKEND,
220 &backend);
221 assert(!r);
222
223 while (completed > next_reset)
224 next_reset += completed;
225 }
Rusty Russellde929b02012-10-16 23:56:16 +1030226 } while (r == 0);
Eugenio Pérez633fae32020-04-18 12:22:11 +0200227 if (completed == completed_before && started == started_before)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200228 ++spurious;
229 assert(completed <= bufs);
230 assert(started <= bufs);
231 if (completed == bufs)
232 break;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400233 if (delayed) {
234 if (virtqueue_enable_cb_delayed(vq->vq))
235 wait_for_interrupt(dev);
236 } else {
237 if (virtqueue_enable_cb(vq->vq))
238 wait_for_interrupt(dev);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200239 }
240 }
241 test = 0;
242 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
243 assert(r >= 0);
Colin Ian King96fb20c2017-04-29 23:14:21 +0100244 fprintf(stderr, "spurious wakeups: 0x%llx\n", spurious);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200245}
246
247const char optstring[] = "h";
248const struct option longopts[] = {
249 {
250 .name = "help",
251 .val = 'h',
252 },
253 {
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300254 .name = "event-idx",
255 .val = 'E',
256 },
257 {
258 .name = "no-event-idx",
259 .val = 'e',
260 },
261 {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200262 .name = "indirect",
263 .val = 'I',
264 },
265 {
266 .name = "no-indirect",
267 .val = 'i',
268 },
269 {
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200270 .name = "virtio-1",
271 .val = '1',
272 },
273 {
274 .name = "no-virtio-1",
275 .val = '0',
276 },
277 {
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400278 .name = "delayed-interrupt",
279 .val = 'D',
280 },
281 {
282 .name = "no-delayed-interrupt",
283 .val = 'd',
284 },
285 {
Eugenio Pérez633fae32020-04-18 12:22:11 +0200286 .name = "batch",
287 .val = 'b',
288 .has_arg = required_argument,
289 },
290 {
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200291 .name = "reset",
292 .val = 'r',
293 .has_arg = optional_argument,
294 },
295 {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200296 }
297};
298
Cong Ding4a7d6452012-12-03 10:24:54 +0000299static void help(void)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200300{
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300301 fprintf(stderr, "Usage: virtio_test [--help]"
302 " [--no-indirect]"
303 " [--no-event-idx]"
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200304 " [--no-virtio-1]"
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400305 " [--delayed-interrupt]"
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200306 " [--batch=random/N]"
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200307 " [--reset=N]"
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300308 "\n");
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200309}
310
311int main(int argc, char **argv)
312{
313 struct vdev_info dev;
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300314 unsigned long long features = (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200315 (1ULL << VIRTIO_RING_F_EVENT_IDX) | (1ULL << VIRTIO_F_VERSION_1);
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200316 long batch = 1, reset = 0;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200317 int o;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400318 bool delayed = false;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200319
320 for (;;) {
321 o = getopt_long(argc, argv, optstring, longopts, NULL);
322 switch (o) {
323 case -1:
324 goto done;
325 case '?':
326 help();
327 exit(2);
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300328 case 'e':
329 features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
330 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200331 case 'h':
332 help();
333 goto done;
334 case 'i':
335 features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
336 break;
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200337 case '0':
338 features &= ~(1ULL << VIRTIO_F_VERSION_1);
339 break;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400340 case 'D':
341 delayed = true;
342 break;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200343 case 'b':
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200344 if (0 == strcmp(optarg, "random")) {
345 batch = RANDOM_BATCH;
346 } else {
347 batch = strtol(optarg, NULL, 10);
348 assert(batch > 0);
349 assert(batch < (long)INT_MAX + 1);
350 }
Eugenio Pérez633fae32020-04-18 12:22:11 +0200351 break;
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200352 case 'r':
353 if (!optarg) {
354 reset = 1;
355 } else {
356 reset = strtol(optarg, NULL, 10);
357 assert(reset > 0);
358 assert(reset < (long)INT_MAX + 1);
359 }
360 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200361 default:
362 assert(0);
363 break;
364 }
365 }
366
367done:
368 vdev_info_init(&dev, features);
369 vq_info_add(&dev, 256);
Eugenio Pérez264ee5a2020-04-18 12:22:13 +0200370 run_test(&dev, &dev.vqs[0], delayed, batch, reset, 0x100000);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200371 return 0;
372}