1 // SPDX-License-Identifier: GPL-2.0
7 #include <sys/eventfd.h>
11 #include <sys/ioctl.h>
13 #include <sys/types.h>
16 #include <linux/virtio_types.h>
17 #include <linux/vhost.h>
18 #include <linux/virtio.h>
19 #include <linux/virtio_ring.h>
20 #include "../../drivers/vhost/test.h"
22 #define RANDOM_BATCH -1
25 void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
33 /* copy used for control */
39 struct virtio_device vdev;
42 struct vq_info vqs[1];
46 struct vhost_memory *mem;
49 static const struct vhost_vring_file no_backend = { .fd = -1 },
50 backend = { .fd = 1 };
52 bool vq_notify(struct virtqueue *vq)
54 struct vq_info *info = vq->priv;
55 unsigned long long v = 1;
57 r = write(info->kick, &v, sizeof v);
58 assert(r == sizeof v);
62 void vq_callback(struct virtqueue *vq)
67 void vhost_vq_setup(struct vdev_info *dev, struct vq_info *info)
69 struct vhost_vring_state state = { .index = info->idx };
70 struct vhost_vring_file file = { .index = info->idx };
71 unsigned long long features = dev->vdev.features;
72 struct vhost_vring_addr addr = {
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,
79 r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
81 state.num = info->vring.num;
82 r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
85 r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
87 r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
90 r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
93 r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
97 static void vq_reset(struct vq_info *info, int num, struct virtio_device *vdev)
100 vring_del_virtqueue(info->vq);
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");
107 info->vq->priv = info;
110 static void vq_info_add(struct vdev_info *dev, int num)
112 struct vq_info *info = &dev->vqs[dev->nvqs];
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));
119 vq_reset(info, num, &dev->vdev);
120 vhost_vq_setup(dev, info);
121 dev->fds[info->idx].fd = info->call;
122 dev->fds[info->idx].events = POLLIN;
126 static void vdev_info_init(struct vdev_info* dev, unsigned long long features)
129 memset(dev, 0, sizeof *dev);
130 dev->vdev.features = features;
131 dev->buf_size = 1024;
132 dev->buf = malloc(dev->buf_size);
134 dev->control = open("/dev/vhost-test", O_RDWR);
135 assert(dev->control >= 0);
136 r = ioctl(dev->control, VHOST_SET_OWNER, NULL);
138 dev->mem = malloc(offsetof(struct vhost_memory, regions) +
139 sizeof dev->mem->regions[0]);
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);
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
155 static void wait_for_interrupt(struct vdev_info *dev)
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);
166 static void run_test(struct vdev_info *dev, struct vq_info *vq,
167 bool delayed, int batch, int reset_n, int bufs)
169 struct scatterlist sl;
170 long started = 0, completed = 0, next_reset = reset_n;
171 long completed_before, started_before;
174 long long spurious = 0;
175 const bool random_batch = batch == RANDOM_BATCH;
176 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
179 virtqueue_disable_cb(vq->vq);
180 completed_before = completed;
181 started_before = started;
183 const bool reset = reset_n && completed > next_reset;
185 batch = (random() % vq->vring.num) + 1;
187 while (started < bufs &&
188 (started - completed) < batch) {
189 sg_init_one(&sl, dev->buf, dev->buf_size);
190 r = virtqueue_add_outbuf(vq->vq, &sl, 1,
193 if (unlikely(r != 0)) {
195 started > started_before)
204 if (unlikely(!virtqueue_kick(vq->vq))) {
214 r = ioctl(dev->control, VHOST_TEST_SET_BACKEND,
219 /* Flush out completed bufs if any */
220 while (virtqueue_get_buf(vq->vq, &len)) {
226 r = ioctl(dev->control, VHOST_TEST_SET_BACKEND,
230 while (completed > next_reset)
231 next_reset += completed;
234 if (completed == completed_before && started == started_before)
236 assert(completed <= bufs);
237 assert(started <= bufs);
238 if (completed == bufs)
241 if (virtqueue_enable_cb_delayed(vq->vq))
242 wait_for_interrupt(dev);
244 if (virtqueue_enable_cb(vq->vq))
245 wait_for_interrupt(dev);
249 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
251 fprintf(stderr, "spurious wakeups: 0x%llx\n", spurious);
254 const char optstring[] = "h";
255 const struct option longopts[] = {
265 .name = "no-event-idx",
273 .name = "no-indirect",
281 .name = "no-virtio-1",
285 .name = "delayed-interrupt",
289 .name = "no-delayed-interrupt",
295 .has_arg = required_argument,
300 .has_arg = optional_argument,
306 static void help(void)
308 fprintf(stderr, "Usage: virtio_test [--help]"
312 " [--delayed-interrupt]"
313 " [--batch=random/N]"
318 int main(int argc, char **argv)
320 struct vdev_info dev;
321 unsigned long long features = (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
322 (1ULL << VIRTIO_RING_F_EVENT_IDX) | (1ULL << VIRTIO_F_VERSION_1);
323 long batch = 1, reset = 0;
325 bool delayed = false;
328 o = getopt_long(argc, argv, optstring, longopts, NULL);
336 features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
342 features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
345 features &= ~(1ULL << VIRTIO_F_VERSION_1);
351 if (0 == strcmp(optarg, "random")) {
352 batch = RANDOM_BATCH;
354 batch = strtol(optarg, NULL, 10);
356 assert(batch < (long)INT_MAX + 1);
363 reset = strtol(optarg, NULL, 10);
365 assert(reset < (long)INT_MAX + 1);
375 vdev_info_init(&dev, features);
376 vq_info_add(&dev, 256);
377 run_test(&dev, &dev.vqs[0], delayed, batch, reset, 0x100000);