virtio_test.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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,
  89. true, info->ring,
  90. vq_notify, vq_callback, "test");
  91. assert(info->vq);
  92. info->vq->priv = info;
  93. vhost_vq_setup(dev, info);
  94. dev->fds[info->idx].fd = info->call;
  95. dev->fds[info->idx].events = POLLIN;
  96. dev->nvqs++;
  97. }
  98. static void vdev_info_init(struct vdev_info* dev, unsigned long long features)
  99. {
  100. int r;
  101. memset(dev, 0, sizeof *dev);
  102. dev->vdev.features[0] = features;
  103. dev->vdev.features[1] = features >> 32;
  104. dev->buf_size = 1024;
  105. dev->buf = malloc(dev->buf_size);
  106. assert(dev->buf);
  107. dev->control = open("/dev/vhost-test", O_RDWR);
  108. assert(dev->control >= 0);
  109. r = ioctl(dev->control, VHOST_SET_OWNER, NULL);
  110. assert(r >= 0);
  111. dev->mem = malloc(offsetof(struct vhost_memory, regions) +
  112. sizeof dev->mem->regions[0]);
  113. assert(dev->mem);
  114. memset(dev->mem, 0, offsetof(struct vhost_memory, regions) +
  115. sizeof dev->mem->regions[0]);
  116. dev->mem->nregions = 1;
  117. dev->mem->regions[0].guest_phys_addr = (long)dev->buf;
  118. dev->mem->regions[0].userspace_addr = (long)dev->buf;
  119. dev->mem->regions[0].memory_size = dev->buf_size;
  120. r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
  121. assert(r >= 0);
  122. }
  123. /* TODO: this is pretty bad: we get a cache line bounce
  124. * for the wait queue on poll and another one on read,
  125. * plus the read which is there just to clear the
  126. * current state. */
  127. static void wait_for_interrupt(struct vdev_info *dev)
  128. {
  129. int i;
  130. unsigned long long val;
  131. poll(dev->fds, dev->nvqs, -1);
  132. for (i = 0; i < dev->nvqs; ++i)
  133. if (dev->fds[i].revents & POLLIN) {
  134. read(dev->fds[i].fd, &val, sizeof val);
  135. }
  136. }
  137. static void run_test(struct vdev_info *dev, struct vq_info *vq, int bufs)
  138. {
  139. struct scatterlist sl;
  140. long started = 0, completed = 0;
  141. long completed_before;
  142. int r, test = 1;
  143. unsigned len;
  144. long long spurious = 0;
  145. r = ioctl(dev->control, VHOST_TEST_RUN, &test);
  146. assert(r >= 0);
  147. for (;;) {
  148. virtqueue_disable_cb(vq->vq);
  149. completed_before = completed;
  150. do {
  151. if (started < bufs) {
  152. sg_init_one(&sl, dev->buf, dev->buf_size);
  153. r = virtqueue_add_buf(vq->vq, &sl, 1, 0,
  154. dev->buf + started,
  155. GFP_ATOMIC);
  156. if (likely(r >= 0)) {
  157. ++started;
  158. virtqueue_kick(vq->vq);
  159. }
  160. } else
  161. r = -1;
  162. /* Flush out completed bufs if any */
  163. if (virtqueue_get_buf(vq->vq, &len)) {
  164. ++completed;
  165. r = 0;
  166. }
  167. } while (r >= 0);
  168. if (completed == completed_before)
  169. ++spurious;
  170. assert(completed <= bufs);
  171. assert(started <= bufs);
  172. if (completed == bufs)
  173. break;
  174. if (virtqueue_enable_cb(vq->vq)) {
  175. wait_for_interrupt(dev);
  176. }
  177. }
  178. test = 0;
  179. r = ioctl(dev->control, VHOST_TEST_RUN, &test);
  180. assert(r >= 0);
  181. fprintf(stderr, "spurious wakeus: 0x%llx\n", spurious);
  182. }
  183. const char optstring[] = "h";
  184. const struct option longopts[] = {
  185. {
  186. .name = "help",
  187. .val = 'h',
  188. },
  189. {
  190. .name = "event-idx",
  191. .val = 'E',
  192. },
  193. {
  194. .name = "no-event-idx",
  195. .val = 'e',
  196. },
  197. {
  198. .name = "indirect",
  199. .val = 'I',
  200. },
  201. {
  202. .name = "no-indirect",
  203. .val = 'i',
  204. },
  205. {
  206. }
  207. };
  208. static void help()
  209. {
  210. fprintf(stderr, "Usage: virtio_test [--help]"
  211. " [--no-indirect]"
  212. " [--no-event-idx]"
  213. "\n");
  214. }
  215. int main(int argc, char **argv)
  216. {
  217. struct vdev_info dev;
  218. unsigned long long features = (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
  219. (1ULL << VIRTIO_RING_F_EVENT_IDX);
  220. int o;
  221. for (;;) {
  222. o = getopt_long(argc, argv, optstring, longopts, NULL);
  223. switch (o) {
  224. case -1:
  225. goto done;
  226. case '?':
  227. help();
  228. exit(2);
  229. case 'e':
  230. features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
  231. break;
  232. case 'h':
  233. help();
  234. goto done;
  235. case 'i':
  236. features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
  237. break;
  238. default:
  239. assert(0);
  240. break;
  241. }
  242. }
  243. done:
  244. vdev_info_init(&dev, features);
  245. vq_info_add(&dev, 256);
  246. run_test(&dev, &dev.vqs[0], 0x100000);
  247. return 0;
  248. }