1 /* Simple test of virtio code, entirely in userpsace. */
5 #include <linux/kernel.h>
7 #include <linux/virtio.h>
8 #include <linux/vringh.h>
9 #include <linux/virtio_ring.h>
10 #include <linux/uaccess.h>
11 #include <sys/types.h>
17 #define USER_MEM (1024*1024)
18 void *__user_addr_min, *__user_addr_max;
19 void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
20 static u64 user_addr_offset;
25 static bool never_notify_host(struct virtqueue *vq)
30 static void never_callback_guest(struct virtqueue *vq)
35 static bool getrange_iov(struct vringh *vrh, u64 addr, struct vringh_range *r)
37 if (addr < (u64)(unsigned long)__user_addr_min - user_addr_offset)
39 if (addr >= (u64)(unsigned long)__user_addr_max - user_addr_offset)
42 r->start = (u64)(unsigned long)__user_addr_min - user_addr_offset;
43 r->end_incl = (u64)(unsigned long)__user_addr_max - 1 - user_addr_offset;
44 r->offset = user_addr_offset;
48 /* We return single byte ranges. */
49 static bool getrange_slow(struct vringh *vrh, u64 addr, struct vringh_range *r)
51 if (addr < (u64)(unsigned long)__user_addr_min - user_addr_offset)
53 if (addr >= (u64)(unsigned long)__user_addr_max - user_addr_offset)
57 r->end_incl = r->start;
58 r->offset = user_addr_offset;
62 struct guest_virtio_device {
63 struct virtio_device vdev;
65 unsigned long notifies;
68 static bool parallel_notify_host(struct virtqueue *vq)
71 struct guest_virtio_device *gvdev;
73 gvdev = container_of(vq->vdev, struct guest_virtio_device, vdev);
74 rc = write(gvdev->to_host_fd, "", 1);
81 static bool no_notify_host(struct virtqueue *vq)
86 #define NUM_XFERS (10000000)
88 /* We aim for two "distant" cpus. */
89 static void find_cpus(unsigned int *first, unsigned int *last)
95 for (i = 0; i < 4096; i++) {
99 if (sched_setaffinity(getpid(), sizeof(set), &set) == 0) {
108 /* Opencoded version for fast mode */
109 static inline int vringh_get_head(struct vringh *vrh, u16 *head)
114 err = get_user(avail_idx, &vrh->vring.avail->idx);
118 if (vrh->last_avail_idx == avail_idx)
121 /* Only get avail ring entries after they have been exposed by guest. */
122 virtio_rmb(vrh->weak_barriers);
124 i = vrh->last_avail_idx & (vrh->vring.num - 1);
126 err = get_user(*head, &vrh->vring.avail->ring[i]);
130 vrh->last_avail_idx++;
134 static int parallel_test(unsigned long features,
135 bool (*getrange)(struct vringh *vrh,
136 u64 addr, struct vringh_range *r),
139 void *host_map, *guest_map;
140 int fd, mapsize, to_guest[2], to_host[2];
141 unsigned long xfers = 0, notifies = 0, receives = 0;
142 unsigned int first_cpu, last_cpu;
146 /* Create real file to mmap. */
147 fd = open("/tmp/vringh_test-file", O_RDWR|O_CREAT|O_TRUNC, 0600);
149 err(1, "Opening /tmp/vringh_test-file");
151 /* Extra room at the end for some data, and indirects */
152 mapsize = vring_size(RINGSIZE, ALIGN)
153 + RINGSIZE * 2 * sizeof(int)
154 + RINGSIZE * 6 * sizeof(struct vring_desc);
155 mapsize = (mapsize + getpagesize() - 1) & ~(getpagesize() - 1);
156 ftruncate(fd, mapsize);
158 /* Parent and child use separate addresses, to check our mapping logic! */
159 host_map = mmap(NULL, mapsize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
160 guest_map = mmap(NULL, mapsize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
166 find_cpus(&first_cpu, &last_cpu);
167 printf("Using CPUS %u and %u\n", first_cpu, last_cpu);
172 int status, err, rlen = 0;
175 /* We are the host: never access guest addresses! */
176 munmap(guest_map, mapsize);
178 __user_addr_min = host_map;
179 __user_addr_max = __user_addr_min + mapsize;
180 user_addr_offset = host_map - guest_map;
181 assert(user_addr_offset);
186 vring_init(&vrh.vring, RINGSIZE, host_map, ALIGN);
187 vringh_init_user(&vrh, features, RINGSIZE, true,
188 vrh.vring.desc, vrh.vring.avail, vrh.vring.used);
189 CPU_SET(first_cpu, &cpu_set);
190 if (sched_setaffinity(getpid(), sizeof(cpu_set), &cpu_set))
191 errx(1, "Could not set affinity to cpu %u", first_cpu);
193 while (xfers < NUM_XFERS) {
194 struct iovec host_riov[2], host_wiov[2];
195 struct vringh_iov riov, wiov;
200 err = vringh_get_head(&vrh, &head);
203 err = vringh_need_notify_user(&vrh);
205 errx(1, "vringh_need_notify_user: %i",
208 write(to_guest[1], "", 1);
213 errx(1, "vringh_get_head");
217 vringh_iov_init(&riov,
219 ARRAY_SIZE(host_riov));
220 vringh_iov_init(&wiov,
222 ARRAY_SIZE(host_wiov));
224 err = vringh_getdesc_user(&vrh, &riov, &wiov,
228 err = vringh_need_notify_user(&vrh);
230 errx(1, "vringh_need_notify_user: %i",
233 write(to_guest[1], "", 1);
237 if (!vringh_notify_enable_user(&vrh))
240 /* Swallow all notifies at once. */
241 if (read(to_host[0], buf, sizeof(buf)) < 1)
244 vringh_notify_disable_user(&vrh);
249 errx(1, "vringh_getdesc_user: %i", err);
251 /* We simply copy bytes. */
253 rlen = vringh_iov_pull_user(&riov, rbuf,
256 errx(1, "vringh_iov_pull_user: %i",
258 assert(riov.i == riov.used);
261 err = vringh_iov_push_user(&wiov, rbuf, rlen);
263 errx(1, "vringh_iov_push_user: %i",
265 assert(wiov.i == wiov.used);
271 err = vringh_complete_user(&vrh, head, written);
273 errx(1, "vringh_complete_user: %i", err);
276 err = vringh_need_notify_user(&vrh);
278 errx(1, "vringh_need_notify_user: %i", err);
280 write(to_guest[1], "", 1);
284 if (!WIFEXITED(status))
285 errx(1, "Child died with signal %i?", WTERMSIG(status));
286 if (WEXITSTATUS(status) != 0)
287 errx(1, "Child exited %i?", WEXITSTATUS(status));
288 printf("Host: notified %lu, pinged %lu\n", notifies, receives);
291 struct guest_virtio_device gvdev;
292 struct virtqueue *vq;
294 struct vring_desc *indirects;
295 unsigned int finished = 0;
297 /* We pass sg[]s pointing into here, but we need RINGSIZE+1 */
298 data = guest_map + vring_size(RINGSIZE, ALIGN);
299 indirects = (void *)data + (RINGSIZE + 1) * 2 * sizeof(int);
301 /* We are the guest. */
302 munmap(host_map, mapsize);
307 gvdev.vdev.features[0] = features;
308 gvdev.to_host_fd = to_host[1];
311 CPU_SET(first_cpu, &cpu_set);
312 if (sched_setaffinity(getpid(), sizeof(cpu_set), &cpu_set))
313 err(1, "Could not set affinity to cpu %u", first_cpu);
315 vq = vring_new_virtqueue(0, RINGSIZE, ALIGN, &gvdev.vdev, true,
316 guest_map, fast_vringh ? no_notify_host
317 : parallel_notify_host,
318 never_callback_guest, "guest vq");
320 /* Don't kfree indirects. */
321 __kfree_ignore_start = indirects;
322 __kfree_ignore_end = indirects + RINGSIZE * 6;
324 while (xfers < NUM_XFERS) {
325 struct scatterlist sg[4];
326 unsigned int num_sg, len;
328 bool output = !(xfers % 2);
331 while ((dbuf = virtqueue_get_buf(vq, &len)) != NULL) {
333 assert(*dbuf == finished - 1);
334 else if (!fast_vringh)
335 assert(*dbuf == finished);
339 /* Produce a buffer. */
340 dbuf = data + (xfers % (RINGSIZE + 1));
347 switch ((xfers / sizeof(*dbuf)) % 4) {
349 /* Nasty three-element sg list. */
350 sg_init_table(sg, num_sg = 3);
351 sg_set_buf(&sg[0], (void *)dbuf, 1);
352 sg_set_buf(&sg[1], (void *)dbuf + 1, 2);
353 sg_set_buf(&sg[2], (void *)dbuf + 3, 1);
356 sg_init_table(sg, num_sg = 2);
357 sg_set_buf(&sg[0], (void *)dbuf, 1);
358 sg_set_buf(&sg[1], (void *)dbuf + 1, 3);
361 sg_init_table(sg, num_sg = 1);
362 sg_set_buf(&sg[0], (void *)dbuf, 4);
365 sg_init_table(sg, num_sg = 4);
366 sg_set_buf(&sg[0], (void *)dbuf, 1);
367 sg_set_buf(&sg[1], (void *)dbuf + 1, 1);
368 sg_set_buf(&sg[2], (void *)dbuf + 2, 1);
369 sg_set_buf(&sg[3], (void *)dbuf + 3, 1);
373 /* May allocate an indirect, so force it to allocate
375 __kmalloc_fake = indirects + (xfers % RINGSIZE) * 4;
377 err = virtqueue_add_outbuf(vq, sg, num_sg, dbuf,
380 err = virtqueue_add_inbuf(vq, sg, num_sg,
383 if (err == -ENOSPC) {
384 if (!virtqueue_enable_cb_delayed(vq))
386 /* Swallow all notifies at once. */
387 if (read(to_guest[0], buf, sizeof(buf)) < 1)
391 virtqueue_disable_cb(vq);
396 errx(1, "virtqueue_add_in/outbuf: %i", err);
403 while (finished != xfers) {
408 dbuf = virtqueue_get_buf(vq, &len);
411 assert(*dbuf == finished - 1);
418 if (!virtqueue_enable_cb_delayed(vq))
420 if (read(to_guest[0], buf, sizeof(buf)) < 1)
424 virtqueue_disable_cb(vq);
427 printf("Guest: notified %lu, pinged %lu\n",
428 gvdev.notifies, receives);
429 vring_del_virtqueue(vq);
434 int main(int argc, char *argv[])
436 struct virtio_device vdev;
437 struct virtqueue *vq;
439 struct scatterlist guest_sg[RINGSIZE], *sgs[2];
440 struct iovec host_riov[2], host_wiov[2];
441 struct vringh_iov riov, wiov;
442 struct vring_used_elem used[RINGSIZE];
448 bool (*getrange)(struct vringh *vrh, u64 addr, struct vringh_range *r);
449 bool fast_vringh = false, parallel = false;
451 getrange = getrange_iov;
452 vdev.features[0] = 0;
455 if (strcmp(argv[1], "--indirect") == 0)
456 vdev.features[0] |= (1 << VIRTIO_RING_F_INDIRECT_DESC);
457 else if (strcmp(argv[1], "--eventidx") == 0)
458 vdev.features[0] |= (1 << VIRTIO_RING_F_EVENT_IDX);
459 else if (strcmp(argv[1], "--slow-range") == 0)
460 getrange = getrange_slow;
461 else if (strcmp(argv[1], "--fast-vringh") == 0)
463 else if (strcmp(argv[1], "--parallel") == 0)
466 errx(1, "Unknown arg %s", argv[1]);
471 return parallel_test(vdev.features[0], getrange, fast_vringh);
473 if (posix_memalign(&__user_addr_min, PAGE_SIZE, USER_MEM) != 0)
475 __user_addr_max = __user_addr_min + USER_MEM;
476 memset(__user_addr_min, 0, vring_size(RINGSIZE, ALIGN));
478 /* Set up guest side. */
479 vq = vring_new_virtqueue(0, RINGSIZE, ALIGN, &vdev, true,
481 never_notify_host, never_callback_guest,
484 /* Set up host side. */
485 vring_init(&vrh.vring, RINGSIZE, __user_addr_min, ALIGN);
486 vringh_init_user(&vrh, vdev.features[0], RINGSIZE, true,
487 vrh.vring.desc, vrh.vring.avail, vrh.vring.used);
489 /* No descriptor to get yet... */
490 err = vringh_getdesc_user(&vrh, &riov, &wiov, getrange, &head);
492 errx(1, "vringh_getdesc_user: %i", err);
494 /* Guest puts in a descriptor. */
495 memcpy(__user_addr_max - 1, "a", 1);
496 sg_init_table(guest_sg, 1);
497 sg_set_buf(&guest_sg[0], __user_addr_max - 1, 1);
498 sg_init_table(guest_sg+1, 1);
499 sg_set_buf(&guest_sg[1], __user_addr_max - 3, 2);
500 sgs[0] = &guest_sg[0];
501 sgs[1] = &guest_sg[1];
503 /* May allocate an indirect, so force it to allocate user addr */
504 __kmalloc_fake = __user_addr_min + vring_size(RINGSIZE, ALIGN);
505 err = virtqueue_add_sgs(vq, sgs, 1, 1, &err, GFP_KERNEL);
507 errx(1, "virtqueue_add_sgs: %i", err);
508 __kmalloc_fake = NULL;
510 /* Host retreives it. */
511 vringh_iov_init(&riov, host_riov, ARRAY_SIZE(host_riov));
512 vringh_iov_init(&wiov, host_wiov, ARRAY_SIZE(host_wiov));
514 err = vringh_getdesc_user(&vrh, &riov, &wiov, getrange, &head);
516 errx(1, "vringh_getdesc_user: %i", err);
518 assert(riov.used == 1);
519 assert(riov.iov[0].iov_base == __user_addr_max - 1);
520 assert(riov.iov[0].iov_len == 1);
521 if (getrange != getrange_slow) {
522 assert(wiov.used == 1);
523 assert(wiov.iov[0].iov_base == __user_addr_max - 3);
524 assert(wiov.iov[0].iov_len == 2);
526 assert(wiov.used == 2);
527 assert(wiov.iov[0].iov_base == __user_addr_max - 3);
528 assert(wiov.iov[0].iov_len == 1);
529 assert(wiov.iov[1].iov_base == __user_addr_max - 2);
530 assert(wiov.iov[1].iov_len == 1);
533 err = vringh_iov_pull_user(&riov, buf, 5);
535 errx(1, "vringh_iov_pull_user: %i", err);
536 assert(buf[0] == 'a');
538 assert(vringh_iov_pull_user(&riov, buf, 5) == 0);
540 memcpy(buf, "bcdef", 5);
541 err = vringh_iov_push_user(&wiov, buf, 5);
543 errx(1, "vringh_iov_push_user: %i", err);
544 assert(memcmp(__user_addr_max - 3, "bc", 2) == 0);
545 assert(wiov.i == wiov.used);
546 assert(vringh_iov_push_user(&wiov, buf, 5) == 0);
549 err = vringh_complete_user(&vrh, head, err);
551 errx(1, "vringh_complete_user: %i", err);
553 /* Guest should see used token now. */
554 __kfree_ignore_start = __user_addr_min + vring_size(RINGSIZE, ALIGN);
555 __kfree_ignore_end = __kfree_ignore_start + 1;
556 ret = virtqueue_get_buf(vq, &i);
558 errx(1, "virtqueue_get_buf: %p", ret);
561 /* Guest puts in a huge descriptor. */
562 sg_init_table(guest_sg, RINGSIZE);
563 for (i = 0; i < RINGSIZE; i++) {
564 sg_set_buf(&guest_sg[i],
565 __user_addr_max - USER_MEM/4, USER_MEM/4);
568 /* Fill contents with recognisable garbage. */
569 for (i = 0; i < USER_MEM/4; i++)
570 ((char *)__user_addr_max - USER_MEM/4)[i] = i;
572 /* This will allocate an indirect, so force it to allocate user addr */
573 __kmalloc_fake = __user_addr_min + vring_size(RINGSIZE, ALIGN);
574 err = virtqueue_add_outbuf(vq, guest_sg, RINGSIZE, &err, GFP_KERNEL);
576 errx(1, "virtqueue_add_outbuf (large): %i", err);
577 __kmalloc_fake = NULL;
579 /* Host picks it up (allocates new iov). */
580 vringh_iov_init(&riov, host_riov, ARRAY_SIZE(host_riov));
581 vringh_iov_init(&wiov, host_wiov, ARRAY_SIZE(host_wiov));
583 err = vringh_getdesc_user(&vrh, &riov, &wiov, getrange, &head);
585 errx(1, "vringh_getdesc_user: %i", err);
587 assert(riov.max_num & VRINGH_IOV_ALLOCATED);
588 assert(riov.iov != host_riov);
589 if (getrange != getrange_slow)
590 assert(riov.used == RINGSIZE);
592 assert(riov.used == RINGSIZE * USER_MEM/4);
594 assert(!(wiov.max_num & VRINGH_IOV_ALLOCATED));
595 assert(wiov.used == 0);
597 /* Pull data back out (in odd chunks), should be as expected. */
598 for (i = 0; i < RINGSIZE * USER_MEM/4; i += 3) {
599 err = vringh_iov_pull_user(&riov, buf, 3);
600 if (err != 3 && i + err != RINGSIZE * USER_MEM/4)
601 errx(1, "vringh_iov_pull_user large: %i", err);
602 assert(buf[0] == (char)i);
603 assert(err < 2 || buf[1] == (char)(i + 1));
604 assert(err < 3 || buf[2] == (char)(i + 2));
606 assert(riov.i == riov.used);
607 vringh_iov_cleanup(&riov);
608 vringh_iov_cleanup(&wiov);
610 /* Complete using multi interface, just because we can. */
613 err = vringh_complete_multi_user(&vrh, used, 1);
615 errx(1, "vringh_complete_multi_user(1): %i", err);
617 /* Free up those descriptors. */
618 ret = virtqueue_get_buf(vq, &i);
620 errx(1, "virtqueue_get_buf: %p", ret);
622 /* Add lots of descriptors. */
623 sg_init_table(guest_sg, 1);
624 sg_set_buf(&guest_sg[0], __user_addr_max - 1, 1);
625 for (i = 0; i < RINGSIZE; i++) {
626 err = virtqueue_add_outbuf(vq, guest_sg, 1, &err, GFP_KERNEL);
628 errx(1, "virtqueue_add_outbuf (multiple): %i", err);
631 /* Now get many, and consume them all at once. */
632 vringh_iov_init(&riov, host_riov, ARRAY_SIZE(host_riov));
633 vringh_iov_init(&wiov, host_wiov, ARRAY_SIZE(host_wiov));
635 for (i = 0; i < RINGSIZE; i++) {
636 err = vringh_getdesc_user(&vrh, &riov, &wiov, getrange, &head);
638 errx(1, "vringh_getdesc_user: %i", err);
642 /* Make sure it wraps around ring, to test! */
643 assert(vrh.vring.used->idx % RINGSIZE != 0);
644 err = vringh_complete_multi_user(&vrh, used, RINGSIZE);
646 errx(1, "vringh_complete_multi_user: %i", err);
648 /* Free those buffers. */
649 for (i = 0; i < RINGSIZE; i++) {
651 assert(virtqueue_get_buf(vq, &len) != NULL);
654 /* Test weird (but legal!) indirect. */
655 if (vdev.features[0] & (1 << VIRTIO_RING_F_INDIRECT_DESC)) {
656 char *data = __user_addr_max - USER_MEM/4;
657 struct vring_desc *d = __user_addr_max - USER_MEM/2;
660 /* Force creation of direct, which we modify. */
661 vdev.features[0] &= ~(1 << VIRTIO_RING_F_INDIRECT_DESC);
662 vq = vring_new_virtqueue(0, RINGSIZE, ALIGN, &vdev, true,
665 never_callback_guest,
668 sg_init_table(guest_sg, 4);
669 sg_set_buf(&guest_sg[0], d, sizeof(*d)*2);
670 sg_set_buf(&guest_sg[1], d + 2, sizeof(*d)*1);
671 sg_set_buf(&guest_sg[2], data + 6, 4);
672 sg_set_buf(&guest_sg[3], d + 3, sizeof(*d)*3);
674 err = virtqueue_add_outbuf(vq, guest_sg, 4, &err, GFP_KERNEL);
676 errx(1, "virtqueue_add_outbuf (indirect): %i", err);
678 vring_init(&vring, RINGSIZE, __user_addr_min, ALIGN);
680 /* They're used in order, but double-check... */
681 assert(vring.desc[0].addr == (unsigned long)d);
682 assert(vring.desc[1].addr == (unsigned long)(d+2));
683 assert(vring.desc[2].addr == (unsigned long)data + 6);
684 assert(vring.desc[3].addr == (unsigned long)(d+3));
685 vring.desc[0].flags |= VRING_DESC_F_INDIRECT;
686 vring.desc[1].flags |= VRING_DESC_F_INDIRECT;
687 vring.desc[3].flags |= VRING_DESC_F_INDIRECT;
690 d[0].addr = (unsigned long)data;
692 d[0].flags = VRING_DESC_F_NEXT;
694 d[1].addr = (unsigned long)data + 1;
698 /* Second indirect */
699 d[2].addr = (unsigned long)data + 3;
704 d[3].addr = (unsigned long)data + 10;
706 d[3].flags = VRING_DESC_F_NEXT;
708 d[4].addr = (unsigned long)data + 15;
710 d[4].flags = VRING_DESC_F_NEXT;
712 d[5].addr = (unsigned long)data + 21;
716 /* Host picks it up (allocates new iov). */
717 vringh_iov_init(&riov, host_riov, ARRAY_SIZE(host_riov));
718 vringh_iov_init(&wiov, host_wiov, ARRAY_SIZE(host_wiov));
720 err = vringh_getdesc_user(&vrh, &riov, &wiov, getrange, &head);
722 errx(1, "vringh_getdesc_user: %i", err);
725 errx(1, "vringh_getdesc_user: head %i not 0", head);
727 assert(riov.max_num & VRINGH_IOV_ALLOCATED);
728 if (getrange != getrange_slow)
729 assert(riov.used == 7);
731 assert(riov.used == 28);
732 err = vringh_iov_pull_user(&riov, buf, 29);
735 /* Data should be linear. */
736 for (i = 0; i < err; i++)
738 vringh_iov_cleanup(&riov);
741 /* Don't leak memory... */
742 vring_del_virtqueue(vq);
743 free(__user_addr_min);