virtio_test.c 6.5 KB

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