virtio_test.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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/virtio_types.h>
  15. #include <linux/vhost.h>
  16. #include <linux/virtio.h>
  17. #include <linux/virtio_ring.h>
  18. #include "../../drivers/vhost/test.h"
  19. /* Unused */
  20. void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
  21. struct vq_info {
  22. int kick;
  23. int call;
  24. int num;
  25. int idx;
  26. void *ring;
  27. /* copy used for control */
  28. struct vring vring;
  29. struct virtqueue *vq;
  30. };
  31. struct vdev_info {
  32. struct virtio_device vdev;
  33. int control;
  34. struct pollfd fds[1];
  35. struct vq_info vqs[1];
  36. int nvqs;
  37. void *buf;
  38. size_t buf_size;
  39. struct vhost_memory *mem;
  40. };
  41. bool vq_notify(struct virtqueue *vq)
  42. {
  43. struct vq_info *info = vq->priv;
  44. unsigned long long v = 1;
  45. int r;
  46. r = write(info->kick, &v, sizeof v);
  47. assert(r == sizeof v);
  48. return true;
  49. }
  50. void vq_callback(struct virtqueue *vq)
  51. {
  52. }
  53. void vhost_vq_setup(struct vdev_info *dev, struct vq_info *info)
  54. {
  55. struct vhost_vring_state state = { .index = info->idx };
  56. struct vhost_vring_file file = { .index = info->idx };
  57. unsigned long long features = dev->vdev.features;
  58. struct vhost_vring_addr addr = {
  59. .index = info->idx,
  60. .desc_user_addr = (uint64_t)(unsigned long)info->vring.desc,
  61. .avail_user_addr = (uint64_t)(unsigned long)info->vring.avail,
  62. .used_user_addr = (uint64_t)(unsigned long)info->vring.used,
  63. };
  64. int r;
  65. r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
  66. assert(r >= 0);
  67. state.num = info->vring.num;
  68. r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
  69. assert(r >= 0);
  70. state.num = 0;
  71. r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
  72. assert(r >= 0);
  73. r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
  74. assert(r >= 0);
  75. file.fd = info->kick;
  76. r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
  77. assert(r >= 0);
  78. file.fd = info->call;
  79. r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
  80. assert(r >= 0);
  81. }
  82. static void vq_info_add(struct vdev_info *dev, int num)
  83. {
  84. struct vq_info *info = &dev->vqs[dev->nvqs];
  85. int r;
  86. info->idx = dev->nvqs;
  87. info->kick = eventfd(0, EFD_NONBLOCK);
  88. info->call = eventfd(0, EFD_NONBLOCK);
  89. r = posix_memalign(&info->ring, 4096, vring_size(num, 4096));
  90. assert(r >= 0);
  91. memset(info->ring, 0, vring_size(num, 4096));
  92. vring_init(&info->vring, num, info->ring, 4096);
  93. info->vq = vring_new_virtqueue(info->idx,
  94. info->vring.num, 4096, &dev->vdev,
  95. true, info->ring,
  96. vq_notify, vq_callback, "test");
  97. assert(info->vq);
  98. info->vq->priv = info;
  99. vhost_vq_setup(dev, info);
  100. dev->fds[info->idx].fd = info->call;
  101. dev->fds[info->idx].events = POLLIN;
  102. dev->nvqs++;
  103. }
  104. static void vdev_info_init(struct vdev_info* dev, unsigned long long features)
  105. {
  106. int r;
  107. memset(dev, 0, sizeof *dev);
  108. dev->vdev.features = features;
  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 = "virtio-1",
  218. .val = '1',
  219. },
  220. {
  221. .name = "no-virtio-1",
  222. .val = '0',
  223. },
  224. {
  225. .name = "delayed-interrupt",
  226. .val = 'D',
  227. },
  228. {
  229. .name = "no-delayed-interrupt",
  230. .val = 'd',
  231. },
  232. {
  233. }
  234. };
  235. static void help(void)
  236. {
  237. fprintf(stderr, "Usage: virtio_test [--help]"
  238. " [--no-indirect]"
  239. " [--no-event-idx]"
  240. " [--no-virtio-1]"
  241. " [--delayed-interrupt]"
  242. "\n");
  243. }
  244. int main(int argc, char **argv)
  245. {
  246. struct vdev_info dev;
  247. unsigned long long features = (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
  248. (1ULL << VIRTIO_RING_F_EVENT_IDX) | (1ULL << VIRTIO_F_VERSION_1);
  249. int o;
  250. bool delayed = false;
  251. for (;;) {
  252. o = getopt_long(argc, argv, optstring, longopts, NULL);
  253. switch (o) {
  254. case -1:
  255. goto done;
  256. case '?':
  257. help();
  258. exit(2);
  259. case 'e':
  260. features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
  261. break;
  262. case 'h':
  263. help();
  264. goto done;
  265. case 'i':
  266. features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
  267. break;
  268. case '0':
  269. features &= ~(1ULL << VIRTIO_F_VERSION_1);
  270. break;
  271. case 'D':
  272. delayed = true;
  273. break;
  274. default:
  275. assert(0);
  276. break;
  277. }
  278. }
  279. done:
  280. vdev_info_init(&dev, features);
  281. vq_info_add(&dev, 256);
  282. run_test(&dev, &dev.vqs[0], delayed, 0x100000);
  283. return 0;
  284. }