tools/virtio: Add --batch=random option
[linux-2.6-microblaze.git] / tools / virtio / virtio_test.c
1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3 #include <getopt.h>
4 #include <limits.h>
5 #include <string.h>
6 #include <poll.h>
7 #include <sys/eventfd.h>
8 #include <stdlib.h>
9 #include <assert.h>
10 #include <unistd.h>
11 #include <sys/ioctl.h>
12 #include <sys/stat.h>
13 #include <sys/types.h>
14 #include <fcntl.h>
15 #include <stdbool.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"
21
22 #define RANDOM_BATCH -1
23
24 /* Unused */
25 void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
26
27 struct vq_info {
28         int kick;
29         int call;
30         int num;
31         int idx;
32         void *ring;
33         /* copy used for control */
34         struct vring vring;
35         struct virtqueue *vq;
36 };
37
38 struct vdev_info {
39         struct virtio_device vdev;
40         int control;
41         struct pollfd fds[1];
42         struct vq_info vqs[1];
43         int nvqs;
44         void *buf;
45         size_t buf_size;
46         struct vhost_memory *mem;
47 };
48
49 bool vq_notify(struct virtqueue *vq)
50 {
51         struct vq_info *info = vq->priv;
52         unsigned long long v = 1;
53         int r;
54         r = write(info->kick, &v, sizeof v);
55         assert(r == sizeof v);
56         return true;
57 }
58
59 void vq_callback(struct virtqueue *vq)
60 {
61 }
62
63
64 void vhost_vq_setup(struct vdev_info *dev, struct vq_info *info)
65 {
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 = {
70                 .index = info->idx,
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,
74         };
75         int r;
76         r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
77         assert(r >= 0);
78         state.num = info->vring.num;
79         r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
80         assert(r >= 0);
81         state.num = 0;
82         r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
83         assert(r >= 0);
84         r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
85         assert(r >= 0);
86         file.fd = info->kick;
87         r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
88         assert(r >= 0);
89         file.fd = info->call;
90         r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
91         assert(r >= 0);
92 }
93
94 static void vq_info_add(struct vdev_info *dev, int num)
95 {
96         struct vq_info *info = &dev->vqs[dev->nvqs];
97         int r;
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));
102         assert(r >= 0);
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");
109         assert(info->vq);
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;
114         dev->nvqs++;
115 }
116
117 static void vdev_info_init(struct vdev_info* dev, unsigned long long features)
118 {
119         int r;
120         memset(dev, 0, sizeof *dev);
121         dev->vdev.features = features;
122         dev->buf_size = 1024;
123         dev->buf = malloc(dev->buf_size);
124         assert(dev->buf);
125         dev->control = open("/dev/vhost-test", O_RDWR);
126         assert(dev->control >= 0);
127         r = ioctl(dev->control, VHOST_SET_OWNER, NULL);
128         assert(r >= 0);
129         dev->mem = malloc(offsetof(struct vhost_memory, regions) +
130                           sizeof dev->mem->regions[0]);
131         assert(dev->mem);
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);
139         assert(r >= 0);
140 }
141
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
145  * current state. */
146 static void wait_for_interrupt(struct vdev_info *dev)
147 {
148         int i;
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);
154                 }
155 }
156
157 static void run_test(struct vdev_info *dev, struct vq_info *vq,
158                      bool delayed, int batch, int bufs)
159 {
160         struct scatterlist sl;
161         long started = 0, completed = 0;
162         long completed_before, started_before;
163         int r, test = 1;
164         unsigned len;
165         long long spurious = 0;
166         const bool random_batch = batch == RANDOM_BATCH;
167         r = ioctl(dev->control, VHOST_TEST_RUN, &test);
168         assert(r >= 0);
169         for (;;) {
170                 virtqueue_disable_cb(vq->vq);
171                 completed_before = completed;
172                 started_before = started;
173                 do {
174                         if (random_batch)
175                                 batch = (random() % vq->vring.num) + 1;
176
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,
181                                                          dev->buf + started,
182                                                          GFP_ATOMIC);
183                                 if (unlikely(r != 0)) {
184                                         if (r == -ENOSPC &&
185                                             started > started_before)
186                                                 r = 0;
187                                         else
188                                                 r = -1;
189                                         break;
190                                 }
191
192                                 ++started;
193
194                                 if (unlikely(!virtqueue_kick(vq->vq))) {
195                                         r = -1;
196                                         break;
197                                 }
198                         }
199
200                         if (started >= bufs)
201                                 r = -1;
202
203                         /* Flush out completed bufs if any */
204                         while (virtqueue_get_buf(vq->vq, &len)) {
205                                 ++completed;
206                                 r = 0;
207                         }
208
209                 } while (r == 0);
210                 if (completed == completed_before && started == started_before)
211                         ++spurious;
212                 assert(completed <= bufs);
213                 assert(started <= bufs);
214                 if (completed == bufs)
215                         break;
216                 if (delayed) {
217                         if (virtqueue_enable_cb_delayed(vq->vq))
218                                 wait_for_interrupt(dev);
219                 } else {
220                         if (virtqueue_enable_cb(vq->vq))
221                                 wait_for_interrupt(dev);
222                 }
223         }
224         test = 0;
225         r = ioctl(dev->control, VHOST_TEST_RUN, &test);
226         assert(r >= 0);
227         fprintf(stderr, "spurious wakeups: 0x%llx\n", spurious);
228 }
229
230 const char optstring[] = "h";
231 const struct option longopts[] = {
232         {
233                 .name = "help",
234                 .val = 'h',
235         },
236         {
237                 .name = "event-idx",
238                 .val = 'E',
239         },
240         {
241                 .name = "no-event-idx",
242                 .val = 'e',
243         },
244         {
245                 .name = "indirect",
246                 .val = 'I',
247         },
248         {
249                 .name = "no-indirect",
250                 .val = 'i',
251         },
252         {
253                 .name = "virtio-1",
254                 .val = '1',
255         },
256         {
257                 .name = "no-virtio-1",
258                 .val = '0',
259         },
260         {
261                 .name = "delayed-interrupt",
262                 .val = 'D',
263         },
264         {
265                 .name = "no-delayed-interrupt",
266                 .val = 'd',
267         },
268         {
269                 .name = "batch",
270                 .val = 'b',
271                 .has_arg = required_argument,
272         },
273         {
274         }
275 };
276
277 static void help(void)
278 {
279         fprintf(stderr, "Usage: virtio_test [--help]"
280                 " [--no-indirect]"
281                 " [--no-event-idx]"
282                 " [--no-virtio-1]"
283                 " [--delayed-interrupt]"
284                 " [--batch=random/N]"
285                 "\n");
286 }
287
288 int main(int argc, char **argv)
289 {
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);
293         long batch = 1;
294         int o;
295         bool delayed = false;
296
297         for (;;) {
298                 o = getopt_long(argc, argv, optstring, longopts, NULL);
299                 switch (o) {
300                 case -1:
301                         goto done;
302                 case '?':
303                         help();
304                         exit(2);
305                 case 'e':
306                         features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
307                         break;
308                 case 'h':
309                         help();
310                         goto done;
311                 case 'i':
312                         features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
313                         break;
314                 case '0':
315                         features &= ~(1ULL << VIRTIO_F_VERSION_1);
316                         break;
317                 case 'D':
318                         delayed = true;
319                         break;
320                 case 'b':
321                         if (0 == strcmp(optarg, "random")) {
322                                 batch = RANDOM_BATCH;
323                         } else {
324                                 batch = strtol(optarg, NULL, 10);
325                                 assert(batch > 0);
326                                 assert(batch < (long)INT_MAX + 1);
327                         }
328                         break;
329                 default:
330                         assert(0);
331                         break;
332                 }
333         }
334
335 done:
336         vdev_info_init(&dev, features);
337         vq_info_add(&dev, 256);
338         run_test(&dev, &dev.vqs[0], delayed, batch, 0x100000);
339         return 0;
340 }