5 #include <sys/eventfd.h>
11 #include <sys/types.h>
14 #include <linux/vhost.h>
15 #include <linux/virtio.h>
16 #include <linux/virtio_ring.h>
17 #include "../../drivers/vhost/test.h"
20 void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
28 /* copy used for control */
34 struct virtio_device vdev;
37 struct vq_info vqs[1];
41 struct vhost_memory *mem;
44 void vq_notify(struct virtqueue *vq)
46 struct vq_info *info = vq->priv;
47 unsigned long long v = 1;
49 r = write(info->kick, &v, sizeof v);
50 assert(r == sizeof v);
53 void vq_callback(struct virtqueue *vq)
58 void vhost_vq_setup(struct vdev_info *dev, struct vq_info *info)
60 struct vhost_vring_state state = { .index = info->idx };
61 struct vhost_vring_file file = { .index = info->idx };
62 unsigned long long features = dev->vdev.features[0];
63 struct vhost_vring_addr addr = {
65 .desc_user_addr = (uint64_t)(unsigned long)info->vring.desc,
66 .avail_user_addr = (uint64_t)(unsigned long)info->vring.avail,
67 .used_user_addr = (uint64_t)(unsigned long)info->vring.used,
70 r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
72 state.num = info->vring.num;
73 r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
76 r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
78 r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
81 r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
84 r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
88 static void vq_info_add(struct vdev_info *dev, int num)
90 struct vq_info *info = &dev->vqs[dev->nvqs];
92 info->idx = dev->nvqs;
93 info->kick = eventfd(0, EFD_NONBLOCK);
94 info->call = eventfd(0, EFD_NONBLOCK);
95 r = posix_memalign(&info->ring, 4096, vring_size(num, 4096));
97 memset(info->ring, 0, vring_size(num, 4096));
98 vring_init(&info->vring, num, info->ring, 4096);
99 info->vq = vring_new_virtqueue(info->idx,
100 info->vring.num, 4096, &dev->vdev,
102 vq_notify, vq_callback, "test");
104 info->vq->priv = info;
105 vhost_vq_setup(dev, info);
106 dev->fds[info->idx].fd = info->call;
107 dev->fds[info->idx].events = POLLIN;
111 static void vdev_info_init(struct vdev_info* dev, unsigned long long features)
114 memset(dev, 0, sizeof *dev);
115 dev->vdev.features[0] = features;
116 dev->vdev.features[1] = features >> 32;
117 dev->buf_size = 1024;
118 dev->buf = malloc(dev->buf_size);
120 dev->control = open("/dev/vhost-test", O_RDWR);
121 assert(dev->control >= 0);
122 r = ioctl(dev->control, VHOST_SET_OWNER, NULL);
124 dev->mem = malloc(offsetof(struct vhost_memory, regions) +
125 sizeof dev->mem->regions[0]);
127 memset(dev->mem, 0, offsetof(struct vhost_memory, regions) +
128 sizeof dev->mem->regions[0]);
129 dev->mem->nregions = 1;
130 dev->mem->regions[0].guest_phys_addr = (long)dev->buf;
131 dev->mem->regions[0].userspace_addr = (long)dev->buf;
132 dev->mem->regions[0].memory_size = dev->buf_size;
133 r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
137 /* TODO: this is pretty bad: we get a cache line bounce
138 * for the wait queue on poll and another one on read,
139 * plus the read which is there just to clear the
141 static void wait_for_interrupt(struct vdev_info *dev)
144 unsigned long long val;
145 poll(dev->fds, dev->nvqs, -1);
146 for (i = 0; i < dev->nvqs; ++i)
147 if (dev->fds[i].revents & POLLIN) {
148 read(dev->fds[i].fd, &val, sizeof val);
152 static void run_test(struct vdev_info *dev, struct vq_info *vq,
153 bool delayed, int bufs)
155 struct scatterlist sl;
156 long started = 0, completed = 0;
157 long completed_before;
160 long long spurious = 0;
161 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
164 virtqueue_disable_cb(vq->vq);
165 completed_before = completed;
167 if (started < bufs) {
168 sg_init_one(&sl, dev->buf, dev->buf_size);
169 r = virtqueue_add_outbuf(vq->vq, &sl, 1,
172 if (likely(r == 0)) {
174 virtqueue_kick(vq->vq);
179 /* Flush out completed bufs if any */
180 if (virtqueue_get_buf(vq->vq, &len)) {
186 if (completed == completed_before)
188 assert(completed <= bufs);
189 assert(started <= bufs);
190 if (completed == bufs)
193 if (virtqueue_enable_cb_delayed(vq->vq))
194 wait_for_interrupt(dev);
196 if (virtqueue_enable_cb(vq->vq))
197 wait_for_interrupt(dev);
201 r = ioctl(dev->control, VHOST_TEST_RUN, &test);
203 fprintf(stderr, "spurious wakeus: 0x%llx\n", spurious);
206 const char optstring[] = "h";
207 const struct option longopts[] = {
217 .name = "no-event-idx",
225 .name = "no-indirect",
229 .name = "delayed-interrupt",
233 .name = "no-delayed-interrupt",
240 static void help(void)
242 fprintf(stderr, "Usage: virtio_test [--help]"
245 " [--delayed-interrupt]"
249 int main(int argc, char **argv)
251 struct vdev_info dev;
252 unsigned long long features = (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
253 (1ULL << VIRTIO_RING_F_EVENT_IDX);
255 bool delayed = false;
258 o = getopt_long(argc, argv, optstring, longopts, NULL);
266 features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
272 features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
284 vdev_info_init(&dev, features);
285 vq_info_add(&dev, 256);
286 run_test(&dev, &dev.vqs[0], delayed, 0x100000);