virtio_test.c 8.9 KB

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