5 #include <sys/eventfd.h>
11 #include <sys/types.h>
14 #include <linux/virtio_types.h>
15 #include <linux/vhost.h>
16 #include <linux/virtio.h>
17 #include <linux/virtio_ring.h>
18 #include "../../drivers/vhost/test.h"
21 void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
29 /* copy used for control */
35 struct virtio_device vdev;
38 struct vq_info vqs[1];
42 struct vhost_memory *mem;
45 bool vq_notify(struct virtqueue *vq)
47 struct vq_info *info = vq->priv;
48 unsigned long long v = 1;
50 r = write(info->kick, &v, sizeof v);
51 assert(r == sizeof v);
55 void vq_callback(struct virtqueue *vq)
60 void vhost_vq_setup(struct vdev_info *dev, struct vq_info *info)
62 struct vhost_vring_state state = { .index = info->idx };
63 struct vhost_vring_file file = { .index = info->idx };
64 unsigned long long features = dev->vdev.features;
65 struct vhost_vring_addr addr = {
67 .desc_user_addr = (uint64_t)(unsigned long)info->vring.desc,
68 .avail_user_addr = (uint64_t)(unsigned long)info->vring.avail,
69 .used_user_addr = (uint64_t)(unsigned long)info->vring.used,
72 r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
74 state.num = info->vring.num;
75 r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
78 r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
80 r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
83 r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
86 r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
90 static void vq_info_add(struct vdev_info *dev, int num)
92 struct vq_info *info = &dev->vqs[dev->nvqs];
94 info->idx = dev->nvqs;
95 info->kick = eventfd(0, EFD_NONBLOCK);
96 info->call = eventfd(0, EFD_NONBLOCK);
97 r = posix_memalign(&info->ring, 4096, vring_size(num, 4096));
99 memset(info->ring, 0, vring_size(num, 4096));
100 vring_init(&info->vring, num, info->ring, 4096);
101 info->vq = vring_new_virtqueue(info->idx,
102 info->vring.num, 4096, &dev->vdev,
103 true, false, info->ring,
104 vq_notify, vq_callback, "test");
106 info->vq->priv = info;
107 vhost_vq_setup(dev, info);
108 dev->fds[info->idx].fd = info->call;
109 dev->fds[info->idx].events = POLLIN;
113 static void vdev_info_init(struct vdev_info* dev, unsigned long long features)
116 memset(dev, 0, sizeof *dev);
117 dev->vdev.features = features;
118 dev->buf_size = 1024;
119 dev->buf = malloc(dev->buf_size);
121 dev->control = open("/dev/vhost-test", O_RDWR);
122 assert(dev->control >= 0);
123 r = ioctl(dev->control, VHOST_SET_OWNER, NULL);
125 dev->mem = malloc(offsetof(struct vhost_memory, regions) +
126 sizeof dev->mem->regions[0]);
128 memset(dev->mem, 0, offsetof(struct vhost_memory, regions) +
129 sizeof dev->mem->regions[0]);
130 dev->mem->nregions = 1;
131 dev->mem->regions[0].guest_phys_addr = (long)dev->buf;
132 dev->mem->regions[0].userspace_addr = (long)dev->buf;
133 dev->mem->regions[0].memory_size = dev->buf_size;
134 r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
138 /* TODO: this is pretty bad: we get a cache line bounce
139 * for the wait queue on poll and another one on read,
140 * plus the read which is there just to clear the
142 static void wait_for_interrupt(struct vdev_info *dev)
145 unsigned long long val;
146 poll(dev->fds, dev->nvqs, -1);
147 for (i = 0; i < dev->nvqs; ++i)
148 if (dev->fds[i].revents & POLLIN) {
149 read(dev->fds[i].fd, &val, sizeof val);
153 static void run_test(struct vdev_info *dev, struct vq_info *vq,
154 bool delayed, int bufs)
156 struct scatterlist sl;
157 long started = 0, completed = 0;
158 long completed_before;
161 long long spurious = 0;
162 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
165 virtqueue_disable_cb(vq->vq);
166 completed_before = completed;
168 if (started < bufs) {
169 sg_init_one(&sl, dev->buf, dev->buf_size);
170 r = virtqueue_add_outbuf(vq->vq, &sl, 1,
173 if (likely(r == 0)) {
175 if (unlikely(!virtqueue_kick(vq->vq)))
181 /* Flush out completed bufs if any */
182 if (virtqueue_get_buf(vq->vq, &len)) {
188 if (completed == completed_before)
190 assert(completed <= bufs);
191 assert(started <= bufs);
192 if (completed == bufs)
195 if (virtqueue_enable_cb_delayed(vq->vq))
196 wait_for_interrupt(dev);
198 if (virtqueue_enable_cb(vq->vq))
199 wait_for_interrupt(dev);
203 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
205 fprintf(stderr, "spurious wakeups: 0x%llx\n", spurious);
208 const char optstring[] = "h";
209 const struct option longopts[] = {
219 .name = "no-event-idx",
227 .name = "no-indirect",
235 .name = "no-virtio-1",
239 .name = "delayed-interrupt",
243 .name = "no-delayed-interrupt",
250 static void help(void)
252 fprintf(stderr, "Usage: virtio_test [--help]"
256 " [--delayed-interrupt]"
260 int main(int argc, char **argv)
262 struct vdev_info dev;
263 unsigned long long features = (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
264 (1ULL << VIRTIO_RING_F_EVENT_IDX) | (1ULL << VIRTIO_F_VERSION_1);
266 bool delayed = false;
269 o = getopt_long(argc, argv, optstring, longopts, NULL);
277 features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
283 features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
286 features &= ~(1ULL << VIRTIO_F_VERSION_1);
298 vdev_info_init(&dev, features);
299 vq_info_add(&dev, 256);
300 run_test(&dev, &dev.vqs[0], delayed, 0x100000);