virtio_test.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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[0];
  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[0] = features;
  108. dev->vdev.features[1] = features >> 32;
  109. dev->buf_size = 1024;
  110. dev->buf = malloc(dev->buf_size);
  111. assert(dev->buf);
  112. dev->control = open("/dev/vhost-test", O_RDWR);
  113. assert(dev->control >= 0);
  114. r = ioctl(dev->control, VHOST_SET_OWNER, NULL);
  115. assert(r >= 0);
  116. dev->mem = malloc(offsetof(struct vhost_memory, regions) +
  117. sizeof dev->mem->regions[0]);
  118. assert(dev->mem);
  119. memset(dev->mem, 0, offsetof(struct vhost_memory, regions) +
  120. sizeof dev->mem->regions[0]);
  121. dev->mem->nregions = 1;
  122. dev->mem->regions[0].guest_phys_addr = (long)dev->buf;
  123. dev->mem->regions[0].userspace_addr = (long)dev->buf;
  124. dev->mem->regions[0].memory_size = dev->buf_size;
  125. r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
  126. assert(r >= 0);
  127. }
  128. /* TODO: this is pretty bad: we get a cache line bounce
  129. * for the wait queue on poll and another one on read,
  130. * plus the read which is there just to clear the
  131. * current state. */
  132. static void wait_for_interrupt(struct vdev_info *dev)
  133. {
  134. int i;
  135. unsigned long long val;
  136. poll(dev->fds, dev->nvqs, -1);
  137. for (i = 0; i < dev->nvqs; ++i)
  138. if (dev->fds[i].revents & POLLIN) {
  139. read(dev->fds[i].fd, &val, sizeof val);
  140. }
  141. }
  142. static void run_test(struct vdev_info *dev, struct vq_info *vq,
  143. bool delayed, int bufs)
  144. {
  145. struct scatterlist sl;
  146. long started = 0, completed = 0;
  147. long completed_before;
  148. int r, test = 1;
  149. unsigned len;
  150. long long spurious = 0;
  151. r = ioctl(dev->control, VHOST_TEST_RUN, &test);
  152. assert(r >= 0);
  153. for (;;) {
  154. virtqueue_disable_cb(vq->vq);
  155. completed_before = completed;
  156. do {
  157. if (started < bufs) {
  158. sg_init_one(&sl, dev->buf, dev->buf_size);
  159. r = virtqueue_add_outbuf(vq->vq, &sl, 1,
  160. dev->buf + started,
  161. GFP_ATOMIC);
  162. if (likely(r == 0)) {
  163. ++started;
  164. if (unlikely(!virtqueue_kick(vq->vq))
  165. r = -1;
  166. }
  167. } else
  168. r = -1;
  169. /* Flush out completed bufs if any */
  170. if (virtqueue_get_buf(vq->vq, &len)) {
  171. ++completed;
  172. r = 0;
  173. }
  174. } while (r == 0);
  175. if (completed == completed_before)
  176. ++spurious;
  177. assert(completed <= bufs);
  178. assert(started <= bufs);
  179. if (completed == bufs)
  180. break;
  181. if (delayed) {
  182. if (virtqueue_enable_cb_delayed(vq->vq))
  183. wait_for_interrupt(dev);
  184. } else {
  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. const char optstring[] = "h";
  195. const struct option longopts[] = {
  196. {
  197. .name = "help",
  198. .val = 'h',
  199. },
  200. {
  201. .name = "event-idx",
  202. .val = 'E',
  203. },
  204. {
  205. .name = "no-event-idx",
  206. .val = 'e',
  207. },
  208. {
  209. .name = "indirect",
  210. .val = 'I',
  211. },
  212. {
  213. .name = "no-indirect",
  214. .val = 'i',
  215. },
  216. {
  217. .name = "delayed-interrupt",
  218. .val = 'D',
  219. },
  220. {
  221. .name = "no-delayed-interrupt",
  222. .val = 'd',
  223. },
  224. {
  225. }
  226. };
  227. static void help(void)
  228. {
  229. fprintf(stderr, "Usage: virtio_test [--help]"
  230. " [--no-indirect]"
  231. " [--no-event-idx]"
  232. " [--delayed-interrupt]"
  233. "\n");
  234. }
  235. int main(int argc, char **argv)
  236. {
  237. struct vdev_info dev;
  238. unsigned long long features = (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
  239. (1ULL << VIRTIO_RING_F_EVENT_IDX);
  240. int o;
  241. bool delayed = false;
  242. for (;;) {
  243. o = getopt_long(argc, argv, optstring, longopts, NULL);
  244. switch (o) {
  245. case -1:
  246. goto done;
  247. case '?':
  248. help();
  249. exit(2);
  250. case 'e':
  251. features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
  252. break;
  253. case 'h':
  254. help();
  255. goto done;
  256. case 'i':
  257. features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
  258. break;
  259. case 'D':
  260. delayed = true;
  261. break;
  262. default:
  263. assert(0);
  264. break;
  265. }
  266. }
  267. done:
  268. vdev_info_init(&dev, features);
  269. vq_info_add(&dev, 256);
  270. run_test(&dev, &dev.vqs[0], delayed, 0x100000);
  271. return 0;
  272. }