5 #include <sys/eventfd.h>
11 #include <sys/types.h>
13 #include <linux/vhost.h>
14 #include <linux/virtio.h>
15 #include <linux/virtio_ring.h>
16 #include "../../drivers/vhost/test.h"
24 /* copy used for control */
30 struct virtio_device vdev
;
33 struct vq_info vqs
[1];
37 struct vhost_memory
*mem
;
40 void vq_notify(struct virtqueue
*vq
)
42 struct vq_info
*info
= vq
->priv
;
43 unsigned long long v
= 1;
45 r
= write(info
->kick
, &v
, sizeof v
);
46 assert(r
== sizeof v
);
49 void vq_callback(struct virtqueue
*vq
)
54 void vhost_vq_setup(struct vdev_info
*dev
, struct vq_info
*info
)
56 struct vhost_vring_state state
= { .index
= info
->idx
};
57 struct vhost_vring_file file
= { .index
= info
->idx
};
58 unsigned long long features
= dev
->vdev
.features
[0];
59 struct vhost_vring_addr addr
= {
61 .desc_user_addr
= (uint64_t)(unsigned long)info
->vring
.desc
,
62 .avail_user_addr
= (uint64_t)(unsigned long)info
->vring
.avail
,
63 .used_user_addr
= (uint64_t)(unsigned long)info
->vring
.used
,
66 r
= ioctl(dev
->control
, VHOST_SET_FEATURES
, &features
);
68 state
.num
= info
->vring
.num
;
69 r
= ioctl(dev
->control
, VHOST_SET_VRING_NUM
, &state
);
72 r
= ioctl(dev
->control
, VHOST_SET_VRING_BASE
, &state
);
74 r
= ioctl(dev
->control
, VHOST_SET_VRING_ADDR
, &addr
);
77 r
= ioctl(dev
->control
, VHOST_SET_VRING_KICK
, &file
);
80 r
= ioctl(dev
->control
, VHOST_SET_VRING_CALL
, &file
);
84 static void vq_info_add(struct vdev_info
*dev
, int num
)
86 struct vq_info
*info
= &dev
->vqs
[dev
->nvqs
];
88 info
->idx
= dev
->nvqs
;
89 info
->kick
= eventfd(0, EFD_NONBLOCK
);
90 info
->call
= eventfd(0, EFD_NONBLOCK
);
91 r
= posix_memalign(&info
->ring
, 4096, vring_size(num
, 4096));
93 memset(info
->ring
, 0, vring_size(num
, 4096));
94 vring_init(&info
->vring
, num
, info
->ring
, 4096);
95 info
->vq
= vring_new_virtqueue(info
->vring
.num
, 4096, &dev
->vdev
, info
->ring
,
96 vq_notify
, vq_callback
, "test");
98 info
->vq
->priv
= info
;
99 vhost_vq_setup(dev
, info
);
100 dev
->fds
[info
->idx
].fd
= info
->call
;
101 dev
->fds
[info
->idx
].events
= POLLIN
;
105 static void vdev_info_init(struct vdev_info
* dev
, unsigned long long features
)
108 memset(dev
, 0, sizeof *dev
);
109 dev
->vdev
.features
[0] = features
;
110 dev
->vdev
.features
[1] = features
>> 32;
111 dev
->buf_size
= 1024;
112 dev
->buf
= malloc(dev
->buf_size
);
114 dev
->control
= open("/dev/vhost-test", O_RDWR
);
115 assert(dev
->control
>= 0);
116 r
= ioctl(dev
->control
, VHOST_SET_OWNER
, NULL
);
118 dev
->mem
= malloc(offsetof(struct vhost_memory
, regions
) +
119 sizeof dev
->mem
->regions
[0]);
121 memset(dev
->mem
, 0, offsetof(struct vhost_memory
, regions
) +
122 sizeof dev
->mem
->regions
[0]);
123 dev
->mem
->nregions
= 1;
124 dev
->mem
->regions
[0].guest_phys_addr
= (long)dev
->buf
;
125 dev
->mem
->regions
[0].userspace_addr
= (long)dev
->buf
;
126 dev
->mem
->regions
[0].memory_size
= dev
->buf_size
;
127 r
= ioctl(dev
->control
, VHOST_SET_MEM_TABLE
, dev
->mem
);
131 /* TODO: this is pretty bad: we get a cache line bounce
132 * for the wait queue on poll and another one on read,
133 * plus the read which is there just to clear the
135 static void wait_for_interrupt(struct vdev_info
*dev
)
138 unsigned long long val
;
139 poll(dev
->fds
, dev
->nvqs
, -1);
140 for (i
= 0; i
< dev
->nvqs
; ++i
)
141 if (dev
->fds
[i
].revents
& POLLIN
) {
142 read(dev
->fds
[i
].fd
, &val
, sizeof val
);
146 static void run_test(struct vdev_info
*dev
, struct vq_info
*vq
, int bufs
)
148 struct scatterlist sl
;
149 long started
= 0, completed
= 0;
150 long completed_before
;
153 long long spurious
= 0;
154 r
= ioctl(dev
->control
, VHOST_TEST_RUN
, &test
);
157 virtqueue_disable_cb(vq
->vq
);
158 completed_before
= completed
;
160 if (started
< bufs
) {
161 sg_init_one(&sl
, dev
->buf
, dev
->buf_size
);
162 r
= virtqueue_add_buf(vq
->vq
, &sl
, 1, 0,
164 if (likely(r
>= 0)) {
166 virtqueue_kick(vq
->vq
);
171 /* Flush out completed bufs if any */
172 if (virtqueue_get_buf(vq
->vq
, &len
)) {
178 if (completed
== completed_before
)
180 assert(completed
<= bufs
);
181 assert(started
<= bufs
);
182 if (completed
== bufs
)
184 if (virtqueue_enable_cb(vq
->vq
)) {
185 wait_for_interrupt(dev
);
189 r
= ioctl(dev
->control
, VHOST_TEST_RUN
, &test
);
191 fprintf(stderr
, "spurious wakeus: 0x%llx\n", spurious
);
194 const char optstring
[] = "h";
195 const struct option longopts
[] = {
205 .name
= "no-indirect",
214 fprintf(stderr
, "Usage: virtio_test [--help] [--no-indirect]\n");
217 int main(int argc
, char **argv
)
219 struct vdev_info dev
;
220 unsigned long long features
= 1ULL << VIRTIO_RING_F_INDIRECT_DESC
;
224 o
= getopt_long(argc
, argv
, optstring
, longopts
, NULL
);
235 features
&= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC
);
244 vdev_info_init(&dev
, features
);
245 vq_info_add(&dev
, 256);
246 run_test(&dev
, &dev
.vqs
[0], 0x100000);