virtio_test.c 6.0 KB

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