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 bool vq_notify(struct virtqueue *vq)
51 struct vq_info *info = vq->priv;
52 unsigned long long v = 1;
54 r = write(info->kick, &v, sizeof v);
55 assert(r == sizeof v);
59 void vq_callback(struct virtqueue *vq)
64 void vhost_vq_setup(struct vdev_info *dev, struct vq_info *info)
66 struct vhost_vring_state state = { .index = info->idx };
67 struct vhost_vring_file file = { .index = info->idx };
68 unsigned long long features = dev->vdev.features;
69 struct vhost_vring_addr addr = {
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,
76 r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
78 state.num = info->vring.num;
79 r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
82 r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
84 r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
87 r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
90 r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
94 static void vq_info_add(struct vdev_info *dev, int num)
96 struct vq_info *info = &dev->vqs[dev->nvqs];
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));
103 memset(info->ring, 0, vring_size(num, 4096));
104 vring_init(&info->vring, num, info->ring, 4096);
105 info->vq = vring_new_virtqueue(info->idx,
106 info->vring.num, 4096, &dev->vdev,
107 true, false, info->ring,
108 vq_notify, vq_callback, "test");
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;
117 static void vdev_info_init(struct vdev_info* dev, unsigned long long features)
120 memset(dev, 0, sizeof *dev);
121 dev->vdev.features = features;
122 dev->buf_size = 1024;
123 dev->buf = malloc(dev->buf_size);
125 dev->control = open("/dev/vhost-test", O_RDWR);
126 assert(dev->control >= 0);
127 r = ioctl(dev->control, VHOST_SET_OWNER, NULL);
129 dev->mem = malloc(offsetof(struct vhost_memory, regions) +
130 sizeof dev->mem->regions[0]);
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);
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
146 static void wait_for_interrupt(struct vdev_info *dev)
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);
157 static void run_test(struct vdev_info *dev, struct vq_info *vq,
158 bool delayed, int batch, int bufs)
160 struct scatterlist sl;
161 long started = 0, completed = 0;
162 long completed_before, started_before;
165 long long spurious = 0;
166 const bool random_batch = batch == RANDOM_BATCH;
167 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
170 virtqueue_disable_cb(vq->vq);
171 completed_before = completed;
172 started_before = started;
175 batch = (random() % vq->vring.num) + 1;
177 while (started < bufs &&
178 (started - completed) < batch) {
179 sg_init_one(&sl, dev->buf, dev->buf_size);
180 r = virtqueue_add_outbuf(vq->vq, &sl, 1,
183 if (unlikely(r != 0)) {
185 started > started_before)
194 if (unlikely(!virtqueue_kick(vq->vq))) {
203 /* Flush out completed bufs if any */
204 while (virtqueue_get_buf(vq->vq, &len)) {
210 if (completed == completed_before && started == started_before)
212 assert(completed <= bufs);
213 assert(started <= bufs);
214 if (completed == bufs)
217 if (virtqueue_enable_cb_delayed(vq->vq))
218 wait_for_interrupt(dev);
220 if (virtqueue_enable_cb(vq->vq))
221 wait_for_interrupt(dev);
225 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
227 fprintf(stderr, "spurious wakeups: 0x%llx\n", spurious);
230 const char optstring[] = "h";
231 const struct option longopts[] = {
241 .name = "no-event-idx",
249 .name = "no-indirect",
257 .name = "no-virtio-1",
261 .name = "delayed-interrupt",
265 .name = "no-delayed-interrupt",
271 .has_arg = required_argument,
277 static void help(void)
279 fprintf(stderr, "Usage: virtio_test [--help]"
283 " [--delayed-interrupt]"
284 " [--batch=random/N]"
288 int main(int argc, char **argv)
290 struct vdev_info dev;
291 unsigned long long features = (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
292 (1ULL << VIRTIO_RING_F_EVENT_IDX) | (1ULL << VIRTIO_F_VERSION_1);
295 bool delayed = false;
298 o = getopt_long(argc, argv, optstring, longopts, NULL);
306 features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
312 features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
315 features &= ~(1ULL << VIRTIO_F_VERSION_1);
321 if (0 == strcmp(optarg, "random")) {
322 batch = RANDOM_BATCH;
324 batch = strtol(optarg, NULL, 10);
326 assert(batch < (long)INT_MAX + 1);
336 vdev_info_init(&dev, features);
337 vq_info_add(&dev, 256);
338 run_test(&dev, &dev.vqs[0], delayed, batch, 0x100000);