blob: c30de9088f3cc4ef230972af0eca3dc5233879d6 [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
Rusty Russell61d0b5a2013-03-18 13:22:19 +103022/* Unused */
23void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
24
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +020025struct vq_info {
26 int kick;
27 int call;
28 int num;
29 int idx;
30 void *ring;
31 /* copy used for control */
32 struct vring vring;
33 struct virtqueue *vq;
34};
35
36struct vdev_info {
37 struct virtio_device vdev;
38 int control;
39 struct pollfd fds[1];
40 struct vq_info vqs[1];
41 int nvqs;
42 void *buf;
43 size_t buf_size;
44 struct vhost_memory *mem;
45};
46
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +103047bool vq_notify(struct virtqueue *vq)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +020048{
49 struct vq_info *info = vq->priv;
50 unsigned long long v = 1;
51 int r;
52 r = write(info->kick, &v, sizeof v);
53 assert(r == sizeof v);
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +103054 return true;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +020055}
56
57void vq_callback(struct virtqueue *vq)
58{
59}
60
61
62void vhost_vq_setup(struct vdev_info *dev, struct vq_info *info)
63{
64 struct vhost_vring_state state = { .index = info->idx };
65 struct vhost_vring_file file = { .index = info->idx };
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +020066 unsigned long long features = dev->vdev.features;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +020067 struct vhost_vring_addr addr = {
68 .index = info->idx,
69 .desc_user_addr = (uint64_t)(unsigned long)info->vring.desc,
70 .avail_user_addr = (uint64_t)(unsigned long)info->vring.avail,
71 .used_user_addr = (uint64_t)(unsigned long)info->vring.used,
72 };
73 int r;
74 r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
75 assert(r >= 0);
76 state.num = info->vring.num;
77 r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
78 assert(r >= 0);
79 state.num = 0;
80 r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
81 assert(r >= 0);
82 r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
83 assert(r >= 0);
84 file.fd = info->kick;
85 r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
86 assert(r >= 0);
87 file.fd = info->call;
88 r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
89 assert(r >= 0);
90}
91
92static void vq_info_add(struct vdev_info *dev, int num)
93{
94 struct vq_info *info = &dev->vqs[dev->nvqs];
95 int r;
96 info->idx = dev->nvqs;
97 info->kick = eventfd(0, EFD_NONBLOCK);
98 info->call = eventfd(0, EFD_NONBLOCK);
99 r = posix_memalign(&info->ring, 4096, vring_size(num, 4096));
100 assert(r >= 0);
101 memset(info->ring, 0, vring_size(num, 4096));
102 vring_init(&info->vring, num, info->ring, 4096);
Michael S. Tsirkin73640c92013-03-18 13:22:18 +1030103 info->vq = vring_new_virtqueue(info->idx,
104 info->vring.num, 4096, &dev->vdev,
Sekhar Nori0a12ae42017-04-17 16:42:15 +0530105 true, false, info->ring,
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200106 vq_notify, vq_callback, "test");
107 assert(info->vq);
108 info->vq->priv = info;
109 vhost_vq_setup(dev, info);
110 dev->fds[info->idx].fd = info->call;
111 dev->fds[info->idx].events = POLLIN;
112 dev->nvqs++;
113}
114
115static void vdev_info_init(struct vdev_info* dev, unsigned long long features)
116{
117 int r;
118 memset(dev, 0, sizeof *dev);
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +0200119 dev->vdev.features = features;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200120 dev->buf_size = 1024;
121 dev->buf = malloc(dev->buf_size);
122 assert(dev->buf);
123 dev->control = open("/dev/vhost-test", O_RDWR);
124 assert(dev->control >= 0);
125 r = ioctl(dev->control, VHOST_SET_OWNER, NULL);
126 assert(r >= 0);
127 dev->mem = malloc(offsetof(struct vhost_memory, regions) +
128 sizeof dev->mem->regions[0]);
129 assert(dev->mem);
130 memset(dev->mem, 0, offsetof(struct vhost_memory, regions) +
131 sizeof dev->mem->regions[0]);
132 dev->mem->nregions = 1;
133 dev->mem->regions[0].guest_phys_addr = (long)dev->buf;
134 dev->mem->regions[0].userspace_addr = (long)dev->buf;
135 dev->mem->regions[0].memory_size = dev->buf_size;
136 r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
137 assert(r >= 0);
138}
139
140/* TODO: this is pretty bad: we get a cache line bounce
141 * for the wait queue on poll and another one on read,
142 * plus the read which is there just to clear the
143 * current state. */
144static void wait_for_interrupt(struct vdev_info *dev)
145{
146 int i;
147 unsigned long long val;
148 poll(dev->fds, dev->nvqs, -1);
149 for (i = 0; i < dev->nvqs; ++i)
150 if (dev->fds[i].revents & POLLIN) {
151 read(dev->fds[i].fd, &val, sizeof val);
152 }
153}
154
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400155static void run_test(struct vdev_info *dev, struct vq_info *vq,
Eugenio Pérez633fae32020-04-18 12:22:11 +0200156 bool delayed, int batch, int bufs)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200157{
158 struct scatterlist sl;
159 long started = 0, completed = 0;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200160 long completed_before, started_before;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200161 int r, test = 1;
162 unsigned len;
163 long long spurious = 0;
164 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
165 assert(r >= 0);
166 for (;;) {
167 virtqueue_disable_cb(vq->vq);
168 completed_before = completed;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200169 started_before = started;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200170 do {
Eugenio Pérez633fae32020-04-18 12:22:11 +0200171 while (started < bufs &&
172 (started - completed) < batch) {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200173 sg_init_one(&sl, dev->buf, dev->buf_size);
Rusty Russellcf994e02013-03-20 15:44:30 +1030174 r = virtqueue_add_outbuf(vq->vq, &sl, 1,
175 dev->buf + started,
176 GFP_ATOMIC);
Eugenio Pérez633fae32020-04-18 12:22:11 +0200177 if (unlikely(r != 0)) {
178 if (r == -ENOSPC &&
179 started > started_before)
180 r = 0;
181 else
Heinz Graalfs53c18c92013-10-29 09:40:11 +1030182 r = -1;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200183 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200184 }
Eugenio Pérez633fae32020-04-18 12:22:11 +0200185
186 ++started;
187
188 if (unlikely(!virtqueue_kick(vq->vq))) {
189 r = -1;
190 break;
191 }
192 }
193
194 if (started >= bufs)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200195 r = -1;
196
197 /* Flush out completed bufs if any */
Eugenio Pérez633fae32020-04-18 12:22:11 +0200198 while (virtqueue_get_buf(vq->vq, &len)) {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200199 ++completed;
200 r = 0;
201 }
202
Rusty Russellde929b02012-10-16 23:56:16 +1030203 } while (r == 0);
Eugenio Pérez633fae32020-04-18 12:22:11 +0200204 if (completed == completed_before && started == started_before)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200205 ++spurious;
206 assert(completed <= bufs);
207 assert(started <= bufs);
208 if (completed == bufs)
209 break;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400210 if (delayed) {
211 if (virtqueue_enable_cb_delayed(vq->vq))
212 wait_for_interrupt(dev);
213 } else {
214 if (virtqueue_enable_cb(vq->vq))
215 wait_for_interrupt(dev);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200216 }
217 }
218 test = 0;
219 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
220 assert(r >= 0);
Colin Ian King96fb20c2017-04-29 23:14:21 +0100221 fprintf(stderr, "spurious wakeups: 0x%llx\n", spurious);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200222}
223
224const char optstring[] = "h";
225const struct option longopts[] = {
226 {
227 .name = "help",
228 .val = 'h',
229 },
230 {
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300231 .name = "event-idx",
232 .val = 'E',
233 },
234 {
235 .name = "no-event-idx",
236 .val = 'e',
237 },
238 {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200239 .name = "indirect",
240 .val = 'I',
241 },
242 {
243 .name = "no-indirect",
244 .val = 'i',
245 },
246 {
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200247 .name = "virtio-1",
248 .val = '1',
249 },
250 {
251 .name = "no-virtio-1",
252 .val = '0',
253 },
254 {
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400255 .name = "delayed-interrupt",
256 .val = 'D',
257 },
258 {
259 .name = "no-delayed-interrupt",
260 .val = 'd',
261 },
262 {
Eugenio Pérez633fae32020-04-18 12:22:11 +0200263 .name = "batch",
264 .val = 'b',
265 .has_arg = required_argument,
266 },
267 {
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200268 }
269};
270
Cong Ding4a7d6452012-12-03 10:24:54 +0000271static void help(void)
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200272{
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300273 fprintf(stderr, "Usage: virtio_test [--help]"
274 " [--no-indirect]"
275 " [--no-event-idx]"
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200276 " [--no-virtio-1]"
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400277 " [--delayed-interrupt]"
Eugenio Pérez633fae32020-04-18 12:22:11 +0200278 " [--batch=N]"
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300279 "\n");
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200280}
281
282int main(int argc, char **argv)
283{
284 struct vdev_info dev;
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300285 unsigned long long features = (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200286 (1ULL << VIRTIO_RING_F_EVENT_IDX) | (1ULL << VIRTIO_F_VERSION_1);
Eugenio Pérez633fae32020-04-18 12:22:11 +0200287 long batch = 1;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200288 int o;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400289 bool delayed = false;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200290
291 for (;;) {
292 o = getopt_long(argc, argv, optstring, longopts, NULL);
293 switch (o) {
294 case -1:
295 goto done;
296 case '?':
297 help();
298 exit(2);
Michael S. Tsirkin4423fe42011-05-20 02:11:05 +0300299 case 'e':
300 features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
301 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200302 case 'h':
303 help();
304 goto done;
305 case 'i':
306 features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
307 break;
Michael S. Tsirkin43b09122014-12-14 22:49:16 +0200308 case '0':
309 features &= ~(1ULL << VIRTIO_F_VERSION_1);
310 break;
Michael S. Tsirkin64d09882012-04-16 10:11:12 -0400311 case 'D':
312 delayed = true;
313 break;
Eugenio Pérez633fae32020-04-18 12:22:11 +0200314 case 'b':
315 batch = strtol(optarg, NULL, 10);
316 assert(batch > 0);
317 assert(batch < (long)INT_MAX + 1);
318 break;
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200319 default:
320 assert(0);
321 break;
322 }
323 }
324
325done:
326 vdev_info_init(&dev, features);
327 vq_info_add(&dev, 256);
Eugenio Pérez633fae32020-04-18 12:22:11 +0200328 run_test(&dev, &dev.vqs[0], delayed, batch, 0x100000);
Michael S. Tsirkin4e53f782010-11-29 19:16:37 +0200329 return 0;
330}