blob: 4a2b9d11f2870cc86ff7365687b29e518d9142c4 [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
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +103049bool vq_notify(struct virtqueue *vq)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +020050{
51 struct vq_info *info = vq->priv;
52 unsigned long long v = 1;
53 int r;
54 r = write(info->kick, &v, sizeof v);
55 assert(r == sizeof v);
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +103056 return true;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +020057}
58
59void vq_callback(struct virtqueue *vq)
60{
61}
62
63
64void vhost_vq_setup(struct vdev_info *dev, struct vq_info *info)
65{
66 struct vhost_vring_state state = { .index = info->idx };
67 struct vhost_vring_file file = { .index = info->idx };
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +020068 unsigned long long features = dev->vdev.features;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +020069 struct vhost_vring_addr addr = {
70 .index = info->idx,
71 .desc_user_addr = (uint64_t)(unsigned long)info->vring.desc,
72 .avail_user_addr = (uint64_t)(unsigned long)info->vring.avail,
73 .used_user_addr = (uint64_t)(unsigned long)info->vring.used,
74 };
75 int r;
76 r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
77 assert(r >= 0);
78 state.num = info->vring.num;
79 r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
80 assert(r >= 0);
81 state.num = 0;
82 r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
83 assert(r >= 0);
84 r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
85 assert(r >= 0);
86 file.fd = info->kick;
87 r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
88 assert(r >= 0);
89 file.fd = info->call;
90 r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
91 assert(r >= 0);
92}
93
94static void vq_info_add(struct vdev_info *dev, int num)
95{
96 struct vq_info *info = &dev->vqs[dev->nvqs];
97 int r;
98 info->idx = dev->nvqs;
99 info->kick = eventfd(0, EFD_NONBLOCK);
100 info->call = eventfd(0, EFD_NONBLOCK);
101 r = posix_memalign(&info->ring, 4096, vring_size(num, 4096));
102 assert(r >= 0);
103 memset(info->ring, 0, vring_size(num, 4096));
104 vring_init(&info->vring, num, info->ring, 4096);
Michael S. Tsirkin73640c92013-03-18 13:22:18 +1030105 info->vq = vring_new_virtqueue(info->idx,
106 info->vring.num, 4096, &dev->vdev,
Sekhar Nori0a12ae42017-04-17 16:42:15 +0530107 true, false, info->ring,
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200108 vq_notify, vq_callback, "test");
109 assert(info->vq);
110 info->vq->priv = info;
111 vhost_vq_setup(dev, info);
112 dev->fds[info->idx].fd = info->call;
113 dev->fds[info->idx].events = POLLIN;
114 dev->nvqs++;
115}
116
117static void vdev_info_init(struct vdev_info* dev, unsigned long long features)
118{
119 int r;
120 memset(dev, 0, sizeof *dev);
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +0200121 dev->vdev.features = features;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200122 dev->buf_size = 1024;
123 dev->buf = malloc(dev->buf_size);
124 assert(dev->buf);
125 dev->control = open("/dev/vhost-test", O_RDWR);
126 assert(dev->control >= 0);
127 r = ioctl(dev->control, VHOST_SET_OWNER, NULL);
128 assert(r >= 0);
129 dev->mem = malloc(offsetof(struct vhost_memory, regions) +
130 sizeof dev->mem->regions[0]);
131 assert(dev->mem);
132 memset(dev->mem, 0, offsetof(struct vhost_memory, regions) +
133 sizeof dev->mem->regions[0]);
134 dev->mem->nregions = 1;
135 dev->mem->regions[0].guest_phys_addr = (long)dev->buf;
136 dev->mem->regions[0].userspace_addr = (long)dev->buf;
137 dev->mem->regions[0].memory_size = dev->buf_size;
138 r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
139 assert(r >= 0);
140}
141
142/* TODO: this is pretty bad: we get a cache line bounce
143 * for the wait queue on poll and another one on read,
144 * plus the read which is there just to clear the
145 * current state. */
146static void wait_for_interrupt(struct vdev_info *dev)
147{
148 int i;
149 unsigned long long val;
150 poll(dev->fds, dev->nvqs, -1);
151 for (i = 0; i < dev->nvqs; ++i)
152 if (dev->fds[i].revents & POLLIN) {
153 read(dev->fds[i].fd, &val, sizeof val);
154 }
155}
156
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400157static void run_test(struct vdev_info *dev, struct vq_info *vq,
Eugenio Pérez633fae32020-04-18 12:22:11 +0200158 bool delayed, int batch, int bufs)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200159{
160 struct scatterlist sl;
161 long started = 0, completed = 0;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200162 long completed_before, started_before;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200163 int r, test = 1;
164 unsigned len;
165 long long spurious = 0;
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200166 const bool random_batch = batch == RANDOM_BATCH;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200167 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
168 assert(r >= 0);
169 for (;;) {
170 virtqueue_disable_cb(vq->vq);
171 completed_before = completed;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200172 started_before = started;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200173 do {
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200174 if (random_batch)
175 batch = (random() % vq->vring.num) + 1;
176
Eugenio Pérez633fae32020-04-18 12:22:11 +0200177 while (started < bufs &&
178 (started - completed) < batch) {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200179 sg_init_one(&sl, dev->buf, dev->buf_size);
Rusty Russellcf994e02013-03-20 15:44:30 +1030180 r = virtqueue_add_outbuf(vq->vq, &sl, 1,
181 dev->buf + started,
182 GFP_ATOMIC);
Eugenio Pérez633fae32020-04-18 12:22:11 +0200183 if (unlikely(r != 0)) {
184 if (r == -ENOSPC &&
185 started > started_before)
186 r = 0;
187 else
Heinz Graalfs53c18c92013-10-29 09:40:11 +1030188 r = -1;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200189 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200190 }
Eugenio Pérez633fae32020-04-18 12:22:11 +0200191
192 ++started;
193
194 if (unlikely(!virtqueue_kick(vq->vq))) {
195 r = -1;
196 break;
197 }
198 }
199
200 if (started >= bufs)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200201 r = -1;
202
203 /* Flush out completed bufs if any */
Eugenio Pérez633fae32020-04-18 12:22:11 +0200204 while (virtqueue_get_buf(vq->vq, &len)) {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200205 ++completed;
206 r = 0;
207 }
208
Rusty Russellde929b02012-10-16 23:56:16 +1030209 } while (r == 0);
Eugenio Pérez633fae32020-04-18 12:22:11 +0200210 if (completed == completed_before && started == started_before)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200211 ++spurious;
212 assert(completed <= bufs);
213 assert(started <= bufs);
214 if (completed == bufs)
215 break;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400216 if (delayed) {
217 if (virtqueue_enable_cb_delayed(vq->vq))
218 wait_for_interrupt(dev);
219 } else {
220 if (virtqueue_enable_cb(vq->vq))
221 wait_for_interrupt(dev);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200222 }
223 }
224 test = 0;
225 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
226 assert(r >= 0);
Colin Ian King96fb20c2017-04-29 23:14:21 +0100227 fprintf(stderr, "spurious wakeups: 0x%llx\n", spurious);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200228}
229
230const char optstring[] = "h";
231const struct option longopts[] = {
232 {
233 .name = "help",
234 .val = 'h',
235 },
236 {
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300237 .name = "event-idx",
238 .val = 'E',
239 },
240 {
241 .name = "no-event-idx",
242 .val = 'e',
243 },
244 {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200245 .name = "indirect",
246 .val = 'I',
247 },
248 {
249 .name = "no-indirect",
250 .val = 'i',
251 },
252 {
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200253 .name = "virtio-1",
254 .val = '1',
255 },
256 {
257 .name = "no-virtio-1",
258 .val = '0',
259 },
260 {
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400261 .name = "delayed-interrupt",
262 .val = 'D',
263 },
264 {
265 .name = "no-delayed-interrupt",
266 .val = 'd',
267 },
268 {
Eugenio Pérez633fae32020-04-18 12:22:11 +0200269 .name = "batch",
270 .val = 'b',
271 .has_arg = required_argument,
272 },
273 {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200274 }
275};
276
Cong Ding4a7d6452012-12-03 10:24:54 +0000277static void help(void)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200278{
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300279 fprintf(stderr, "Usage: virtio_test [--help]"
280 " [--no-indirect]"
281 " [--no-event-idx]"
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200282 " [--no-virtio-1]"
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400283 " [--delayed-interrupt]"
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200284 " [--batch=random/N]"
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300285 "\n");
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200286}
287
288int main(int argc, char **argv)
289{
290 struct vdev_info dev;
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300291 unsigned long long features = (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200292 (1ULL << VIRTIO_RING_F_EVENT_IDX) | (1ULL << VIRTIO_F_VERSION_1);
Eugenio Pérez633fae32020-04-18 12:22:11 +0200293 long batch = 1;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200294 int o;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400295 bool delayed = false;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200296
297 for (;;) {
298 o = getopt_long(argc, argv, optstring, longopts, NULL);
299 switch (o) {
300 case -1:
301 goto done;
302 case '?':
303 help();
304 exit(2);
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300305 case 'e':
306 features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
307 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200308 case 'h':
309 help();
310 goto done;
311 case 'i':
312 features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
313 break;
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200314 case '0':
315 features &= ~(1ULL << VIRTIO_F_VERSION_1);
316 break;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400317 case 'D':
318 delayed = true;
319 break;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200320 case 'b':
Eugenio Pérez7add78b2020-04-18 12:22:12 +0200321 if (0 == strcmp(optarg, "random")) {
322 batch = RANDOM_BATCH;
323 } else {
324 batch = strtol(optarg, NULL, 10);
325 assert(batch > 0);
326 assert(batch < (long)INT_MAX + 1);
327 }
Eugenio Pérez633fae32020-04-18 12:22:11 +0200328 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200329 default:
330 assert(0);
331 break;
332 }
333 }
334
335done:
336 vdev_info_init(&dev, features);
337 vq_info_add(&dev, 256);
Eugenio Pérez633fae32020-04-18 12:22:11 +0200338 run_test(&dev, &dev.vqs[0], delayed, batch, 0x100000);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200339 return 0;
340}