virtio: harsher barriers for rpmsg.
[linux-2.6.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 <linux/vhost.h>
14 #include <linux/virtio.h>
15 #include <linux/virtio_ring.h>
16 #include "../../drivers/vhost/test.h"
17
18 struct vq_info {
19         int kick;
20         int call;
21         int num;
22         int idx;
23         void *ring;
24         /* copy used for control */
25         struct vring vring;
26         struct virtqueue *vq;
27 };
28
29 struct vdev_info {
30         struct virtio_device vdev;
31         int control;
32         struct pollfd fds[1];
33         struct vq_info vqs[1];
34         int nvqs;
35         void *buf;
36         size_t buf_size;
37         struct vhost_memory *mem;
38 };
39
40 void vq_notify(struct virtqueue *vq)
41 {
42         struct vq_info *info = vq->priv;
43         unsigned long long v = 1;
44         int r;
45         r = write(info->kick, &v, sizeof v);
46         assert(r == sizeof v);
47 }
48
49 void vq_callback(struct virtqueue *vq)
50 {
51 }
52
53
54 void vhost_vq_setup(struct vdev_info *dev, struct vq_info *info)
55 {
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 = {
60                 .index = info->idx,
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,
64         };
65         int r;
66         r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
67         assert(r >= 0);
68         state.num = info->vring.num;
69         r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
70         assert(r >= 0);
71         state.num = 0;
72         r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
73         assert(r >= 0);
74         r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
75         assert(r >= 0);
76         file.fd = info->kick;
77         r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
78         assert(r >= 0);
79         file.fd = info->call;
80         r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
81         assert(r >= 0);
82 }
83
84 static void vq_info_add(struct vdev_info *dev, int num)
85 {
86         struct vq_info *info = &dev->vqs[dev->nvqs];
87         int r;
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));
92         assert(r >= 0);
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,
96                                        true, info->ring,
97                                        vq_notify, vq_callback, "test");
98         assert(info->vq);
99         info->vq->priv = info;
100         vhost_vq_setup(dev, info);
101         dev->fds[info->idx].fd = info->call;
102         dev->fds[info->idx].events = POLLIN;
103         dev->nvqs++;
104 }
105
106 static void vdev_info_init(struct vdev_info* dev, unsigned long long features)
107 {
108         int r;
109         memset(dev, 0, sizeof *dev);
110         dev->vdev.features[0] = features;
111         dev->vdev.features[1] = features >> 32;
112         dev->buf_size = 1024;
113         dev->buf = malloc(dev->buf_size);
114         assert(dev->buf);
115         dev->control = open("/dev/vhost-test", O_RDWR);
116         assert(dev->control >= 0);
117         r = ioctl(dev->control, VHOST_SET_OWNER, NULL);
118         assert(r >= 0);
119         dev->mem = malloc(offsetof(struct vhost_memory, regions) +
120                           sizeof dev->mem->regions[0]);
121         assert(dev->mem);
122         memset(dev->mem, 0, offsetof(struct vhost_memory, regions) +
123                           sizeof dev->mem->regions[0]);
124         dev->mem->nregions = 1;
125         dev->mem->regions[0].guest_phys_addr = (long)dev->buf;
126         dev->mem->regions[0].userspace_addr = (long)dev->buf;
127         dev->mem->regions[0].memory_size = dev->buf_size;
128         r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
129         assert(r >= 0);
130 }
131
132 /* TODO: this is pretty bad: we get a cache line bounce
133  * for the wait queue on poll and another one on read,
134  * plus the read which is there just to clear the
135  * current state. */
136 static void wait_for_interrupt(struct vdev_info *dev)
137 {
138         int i;
139         unsigned long long val;
140         poll(dev->fds, dev->nvqs, -1);
141         for (i = 0; i < dev->nvqs; ++i)
142                 if (dev->fds[i].revents & POLLIN) {
143                         read(dev->fds[i].fd, &val, sizeof val);
144                 }
145 }
146
147 static void run_test(struct vdev_info *dev, struct vq_info *vq, int bufs)
148 {
149         struct scatterlist sl;
150         long started = 0, completed = 0;
151         long completed_before;
152         int r, test = 1;
153         unsigned len;
154         long long spurious = 0;
155         r = ioctl(dev->control, VHOST_TEST_RUN, &test);
156         assert(r >= 0);
157         for (;;) {
158                 virtqueue_disable_cb(vq->vq);
159                 completed_before = completed;
160                 do {
161                         if (started < bufs) {
162                                 sg_init_one(&sl, dev->buf, dev->buf_size);
163                                 r = virtqueue_add_buf(vq->vq, &sl, 1, 0,
164                                                       dev->buf + started);
165                                 if (likely(r >= 0)) {
166                                         ++started;
167                                         virtqueue_kick(vq->vq);
168                                 }
169                         } else
170                                 r = -1;
171
172                         /* Flush out completed bufs if any */
173                         if (virtqueue_get_buf(vq->vq, &len)) {
174                                 ++completed;
175                                 r = 0;
176                         }
177
178                 } while (r >= 0);
179                 if (completed == completed_before)
180                         ++spurious;
181                 assert(completed <= bufs);
182                 assert(started <= bufs);
183                 if (completed == bufs)
184                         break;
185                 if (virtqueue_enable_cb(vq->vq)) {
186                         wait_for_interrupt(dev);
187                 }
188         }
189         test = 0;
190         r = ioctl(dev->control, VHOST_TEST_RUN, &test);
191         assert(r >= 0);
192         fprintf(stderr, "spurious wakeus: 0x%llx\n", spurious);
193 }
194
195 const char optstring[] = "h";
196 const struct option longopts[] = {
197         {
198                 .name = "help",
199                 .val = 'h',
200         },
201         {
202                 .name = "event-idx",
203                 .val = 'E',
204         },
205         {
206                 .name = "no-event-idx",
207                 .val = 'e',
208         },
209         {
210                 .name = "indirect",
211                 .val = 'I',
212         },
213         {
214                 .name = "no-indirect",
215                 .val = 'i',
216         },
217         {
218         }
219 };
220
221 static void help()
222 {
223         fprintf(stderr, "Usage: virtio_test [--help]"
224                 " [--no-indirect]"
225                 " [--no-event-idx]"
226                 "\n");
227 }
228
229 int main(int argc, char **argv)
230 {
231         struct vdev_info dev;
232         unsigned long long features = (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
233                 (1ULL << VIRTIO_RING_F_EVENT_IDX);
234         int o;
235
236         for (;;) {
237                 o = getopt_long(argc, argv, optstring, longopts, NULL);
238                 switch (o) {
239                 case -1:
240                         goto done;
241                 case '?':
242                         help();
243                         exit(2);
244                 case 'e':
245                         features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
246                         break;
247                 case 'h':
248                         help();
249                         goto done;
250                 case 'i':
251                         features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
252                         break;
253                 default:
254                         assert(0);
255                         break;
256                 }
257         }
258
259 done:
260         vdev_info_init(&dev, features);
261         vq_info_add(&dev, 256);
262         run_test(&dev, &dev.vqs[0], 0x100000);
263         return 0;
264 }