virtio_test.c 6.8 KB

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