Merge tag 'char-misc-3.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregk...
[cascardo/linux.git] / tools / virtio / virtio_test.c
1 #define _GNU_SOURCE
2 #include <getopt.h>
3 #include <string.h>
4 #include <poll.h>
5 #include <sys/eventfd.h>
6 #include <stdlib.h>
7 #include <assert.h>
8 #include <unistd.h>
9 #include <sys/ioctl.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12 #include <fcntl.h>
13 #include <stdbool.h>
14 #include <linux/vhost.h>
15 #include <linux/virtio.h>
16 #include <linux/virtio_ring.h>
17 #include "../../drivers/vhost/test.h"
18
19 /* Unused */
20 void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
21
22 struct vq_info {
23         int kick;
24         int call;
25         int num;
26         int idx;
27         void *ring;
28         /* copy used for control */
29         struct vring vring;
30         struct virtqueue *vq;
31 };
32
33 struct vdev_info {
34         struct virtio_device vdev;
35         int control;
36         struct pollfd fds[1];
37         struct vq_info vqs[1];
38         int nvqs;
39         void *buf;
40         size_t buf_size;
41         struct vhost_memory *mem;
42 };
43
44 bool vq_notify(struct virtqueue *vq)
45 {
46         struct vq_info *info = vq->priv;
47         unsigned long long v = 1;
48         int r;
49         r = write(info->kick, &v, sizeof v);
50         assert(r == sizeof v);
51         return true;
52 }
53
54 void vq_callback(struct virtqueue *vq)
55 {
56 }
57
58
59 void vhost_vq_setup(struct vdev_info *dev, struct vq_info *info)
60 {
61         struct vhost_vring_state state = { .index = info->idx };
62         struct vhost_vring_file file = { .index = info->idx };
63         unsigned long long features = dev->vdev.features;
64         struct vhost_vring_addr addr = {
65                 .index = info->idx,
66                 .desc_user_addr = (uint64_t)(unsigned long)info->vring.desc,
67                 .avail_user_addr = (uint64_t)(unsigned long)info->vring.avail,
68                 .used_user_addr = (uint64_t)(unsigned long)info->vring.used,
69         };
70         int r;
71         r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
72         assert(r >= 0);
73         state.num = info->vring.num;
74         r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
75         assert(r >= 0);
76         state.num = 0;
77         r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
78         assert(r >= 0);
79         r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
80         assert(r >= 0);
81         file.fd = info->kick;
82         r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
83         assert(r >= 0);
84         file.fd = info->call;
85         r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
86         assert(r >= 0);
87 }
88
89 static void vq_info_add(struct vdev_info *dev, int num)
90 {
91         struct vq_info *info = &dev->vqs[dev->nvqs];
92         int r;
93         info->idx = dev->nvqs;
94         info->kick = eventfd(0, EFD_NONBLOCK);
95         info->call = eventfd(0, EFD_NONBLOCK);
96         r = posix_memalign(&info->ring, 4096, vring_size(num, 4096));
97         assert(r >= 0);
98         memset(info->ring, 0, vring_size(num, 4096));
99         vring_init(&info->vring, num, info->ring, 4096);
100         info->vq = vring_new_virtqueue(info->idx,
101                                        info->vring.num, 4096, &dev->vdev,
102                                        true, info->ring,
103                                        vq_notify, vq_callback, "test");
104         assert(info->vq);
105         info->vq->priv = info;
106         vhost_vq_setup(dev, info);
107         dev->fds[info->idx].fd = info->call;
108         dev->fds[info->idx].events = POLLIN;
109         dev->nvqs++;
110 }
111
112 static void vdev_info_init(struct vdev_info* dev, unsigned long long features)
113 {
114         int r;
115         memset(dev, 0, sizeof *dev);
116         dev->vdev.features = features;
117         dev->buf_size = 1024;
118         dev->buf = malloc(dev->buf_size);
119         assert(dev->buf);
120         dev->control = open("/dev/vhost-test", O_RDWR);
121         assert(dev->control >= 0);
122         r = ioctl(dev->control, VHOST_SET_OWNER, NULL);
123         assert(r >= 0);
124         dev->mem = malloc(offsetof(struct vhost_memory, regions) +
125                           sizeof dev->mem->regions[0]);
126         assert(dev->mem);
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);
134         assert(r >= 0);
135 }
136
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
140  * current state. */
141 static void wait_for_interrupt(struct vdev_info *dev)
142 {
143         int i;
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);
149                 }
150 }
151
152 static void run_test(struct vdev_info *dev, struct vq_info *vq,
153                      bool delayed, int bufs)
154 {
155         struct scatterlist sl;
156         long started = 0, completed = 0;
157         long completed_before;
158         int r, test = 1;
159         unsigned len;
160         long long spurious = 0;
161         r = ioctl(dev->control, VHOST_TEST_RUN, &test);
162         assert(r >= 0);
163         for (;;) {
164                 virtqueue_disable_cb(vq->vq);
165                 completed_before = completed;
166                 do {
167                         if (started < bufs) {
168                                 sg_init_one(&sl, dev->buf, dev->buf_size);
169                                 r = virtqueue_add_outbuf(vq->vq, &sl, 1,
170                                                          dev->buf + started,
171                                                          GFP_ATOMIC);
172                                 if (likely(r == 0)) {
173                                         ++started;
174                                         if (unlikely(!virtqueue_kick(vq->vq)))
175                                                 r = -1;
176                                 }
177                         } else
178                                 r = -1;
179
180                         /* Flush out completed bufs if any */
181                         if (virtqueue_get_buf(vq->vq, &len)) {
182                                 ++completed;
183                                 r = 0;
184                         }
185
186                 } while (r == 0);
187                 if (completed == completed_before)
188                         ++spurious;
189                 assert(completed <= bufs);
190                 assert(started <= bufs);
191                 if (completed == bufs)
192                         break;
193                 if (delayed) {
194                         if (virtqueue_enable_cb_delayed(vq->vq))
195                                 wait_for_interrupt(dev);
196                 } else {
197                         if (virtqueue_enable_cb(vq->vq))
198                                 wait_for_interrupt(dev);
199                 }
200         }
201         test = 0;
202         r = ioctl(dev->control, VHOST_TEST_RUN, &test);
203         assert(r >= 0);
204         fprintf(stderr, "spurious wakeus: 0x%llx\n", spurious);
205 }
206
207 const char optstring[] = "h";
208 const struct option longopts[] = {
209         {
210                 .name = "help",
211                 .val = 'h',
212         },
213         {
214                 .name = "event-idx",
215                 .val = 'E',
216         },
217         {
218                 .name = "no-event-idx",
219                 .val = 'e',
220         },
221         {
222                 .name = "indirect",
223                 .val = 'I',
224         },
225         {
226                 .name = "no-indirect",
227                 .val = 'i',
228         },
229         {
230                 .name = "delayed-interrupt",
231                 .val = 'D',
232         },
233         {
234                 .name = "no-delayed-interrupt",
235                 .val = 'd',
236         },
237         {
238         }
239 };
240
241 static void help(void)
242 {
243         fprintf(stderr, "Usage: virtio_test [--help]"
244                 " [--no-indirect]"
245                 " [--no-event-idx]"
246                 " [--delayed-interrupt]"
247                 "\n");
248 }
249
250 int main(int argc, char **argv)
251 {
252         struct vdev_info dev;
253         unsigned long long features = (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
254                 (1ULL << VIRTIO_RING_F_EVENT_IDX);
255         int o;
256         bool delayed = false;
257
258         for (;;) {
259                 o = getopt_long(argc, argv, optstring, longopts, NULL);
260                 switch (o) {
261                 case -1:
262                         goto done;
263                 case '?':
264                         help();
265                         exit(2);
266                 case 'e':
267                         features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
268                         break;
269                 case 'h':
270                         help();
271                         goto done;
272                 case 'i':
273                         features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
274                         break;
275                 case 'D':
276                         delayed = true;
277                         break;
278                 default:
279                         assert(0);
280                         break;
281                 }
282         }
283
284 done:
285         vdev_info_init(&dev, features);
286         vq_info_add(&dev, 256);
287         run_test(&dev, &dev.vqs[0], delayed, 0x100000);
288         return 0;
289 }