vringh_test.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Simple test of virtio code, entirely in userpsace. */
  3. #define _GNU_SOURCE
  4. #include <sched.h>
  5. #include <err.h>
  6. #include <linux/kernel.h>
  7. #include <linux/err.h>
  8. #include <linux/virtio.h>
  9. #include <linux/vringh.h>
  10. #include <linux/virtio_ring.h>
  11. #include <linux/virtio_config.h>
  12. #include <linux/uaccess.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <sys/mman.h>
  16. #include <sys/wait.h>
  17. #include <fcntl.h>
  18. #define USER_MEM (1024*1024)
  19. void *__user_addr_min, *__user_addr_max;
  20. void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
  21. static u64 user_addr_offset;
  22. #define RINGSIZE 256
  23. #define ALIGN 4096
  24. static bool never_notify_host(struct virtqueue *vq)
  25. {
  26. abort();
  27. }
  28. static void never_callback_guest(struct virtqueue *vq)
  29. {
  30. abort();
  31. }
  32. static bool getrange_iov(struct vringh *vrh, u64 addr, struct vringh_range *r)
  33. {
  34. if (addr < (u64)(unsigned long)__user_addr_min - user_addr_offset)
  35. return false;
  36. if (addr >= (u64)(unsigned long)__user_addr_max - user_addr_offset)
  37. return false;
  38. r->start = (u64)(unsigned long)__user_addr_min - user_addr_offset;
  39. r->end_incl = (u64)(unsigned long)__user_addr_max - 1 - user_addr_offset;
  40. r->offset = user_addr_offset;
  41. return true;
  42. }
  43. /* We return single byte ranges. */
  44. static bool getrange_slow(struct vringh *vrh, u64 addr, struct vringh_range *r)
  45. {
  46. if (addr < (u64)(unsigned long)__user_addr_min - user_addr_offset)
  47. return false;
  48. if (addr >= (u64)(unsigned long)__user_addr_max - user_addr_offset)
  49. return false;
  50. r->start = addr;
  51. r->end_incl = r->start;
  52. r->offset = user_addr_offset;
  53. return true;
  54. }
  55. struct guest_virtio_device {
  56. struct virtio_device vdev;
  57. int to_host_fd;
  58. unsigned long notifies;
  59. };
  60. static bool parallel_notify_host(struct virtqueue *vq)
  61. {
  62. int rc;
  63. struct guest_virtio_device *gvdev;
  64. gvdev = container_of(vq->vdev, struct guest_virtio_device, vdev);
  65. rc = write(gvdev->to_host_fd, "", 1);
  66. if (rc < 0)
  67. return false;
  68. gvdev->notifies++;
  69. return true;
  70. }
  71. static bool no_notify_host(struct virtqueue *vq)
  72. {
  73. return true;
  74. }
  75. #define NUM_XFERS (10000000)
  76. /* We aim for two "distant" cpus. */
  77. static void find_cpus(unsigned int *first, unsigned int *last)
  78. {
  79. unsigned int i;
  80. *first = -1U;
  81. *last = 0;
  82. for (i = 0; i < 4096; i++) {
  83. cpu_set_t set;
  84. CPU_ZERO(&set);
  85. CPU_SET(i, &set);
  86. if (sched_setaffinity(getpid(), sizeof(set), &set) == 0) {
  87. if (i < *first)
  88. *first = i;
  89. if (i > *last)
  90. *last = i;
  91. }
  92. }
  93. }
  94. /* Opencoded version for fast mode */
  95. static inline int vringh_get_head(struct vringh *vrh, u16 *head)
  96. {
  97. u16 avail_idx, i;
  98. int err;
  99. err = get_user(avail_idx, &vrh->vring.avail->idx);
  100. if (err)
  101. return err;
  102. if (vrh->last_avail_idx == avail_idx)
  103. return 0;
  104. /* Only get avail ring entries after they have been exposed by guest. */
  105. virtio_rmb(vrh->weak_barriers);
  106. i = vrh->last_avail_idx & (vrh->vring.num - 1);
  107. err = get_user(*head, &vrh->vring.avail->ring[i]);
  108. if (err)
  109. return err;
  110. vrh->last_avail_idx++;
  111. return 1;
  112. }
  113. static int parallel_test(u64 features,
  114. bool (*getrange)(struct vringh *vrh,
  115. u64 addr, struct vringh_range *r),
  116. bool fast_vringh)
  117. {
  118. void *host_map, *guest_map;
  119. int fd, mapsize, to_guest[2], to_host[2];
  120. unsigned long xfers = 0, notifies = 0, receives = 0;
  121. unsigned int first_cpu, last_cpu;
  122. cpu_set_t cpu_set;
  123. char buf[128];
  124. /* Create real file to mmap. */
  125. fd = open("/tmp/vringh_test-file", O_RDWR|O_CREAT|O_TRUNC, 0600);
  126. if (fd < 0)
  127. err(1, "Opening /tmp/vringh_test-file");
  128. /* Extra room at the end for some data, and indirects */
  129. mapsize = vring_size(RINGSIZE, ALIGN)
  130. + RINGSIZE * 2 * sizeof(int)
  131. + RINGSIZE * 6 * sizeof(struct vring_desc);
  132. mapsize = (mapsize + getpagesize() - 1) & ~(getpagesize() - 1);
  133. ftruncate(fd, mapsize);
  134. /* Parent and child use separate addresses, to check our mapping logic! */
  135. host_map = mmap(NULL, mapsize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  136. guest_map = mmap(NULL, mapsize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  137. pipe(to_guest);
  138. pipe(to_host);
  139. CPU_ZERO(&cpu_set);
  140. find_cpus(&first_cpu, &last_cpu);
  141. printf("Using CPUS %u and %u\n", first_cpu, last_cpu);
  142. fflush(stdout);
  143. if (fork() != 0) {
  144. struct vringh vrh;
  145. int status, err, rlen = 0;
  146. char rbuf[5];
  147. /* We are the host: never access guest addresses! */
  148. munmap(guest_map, mapsize);
  149. __user_addr_min = host_map;
  150. __user_addr_max = __user_addr_min + mapsize;
  151. user_addr_offset = host_map - guest_map;
  152. assert(user_addr_offset);
  153. close(to_guest[0]);
  154. close(to_host[1]);
  155. vring_init(&vrh.vring, RINGSIZE, host_map, ALIGN);
  156. vringh_init_user(&vrh, features, RINGSIZE, true,
  157. vrh.vring.desc, vrh.vring.avail, vrh.vring.used);
  158. CPU_SET(first_cpu, &cpu_set);
  159. if (sched_setaffinity(getpid(), sizeof(cpu_set), &cpu_set))
  160. errx(1, "Could not set affinity to cpu %u", first_cpu);
  161. while (xfers < NUM_XFERS) {
  162. struct iovec host_riov[2], host_wiov[2];
  163. struct vringh_iov riov, wiov;
  164. u16 head, written;
  165. if (fast_vringh) {
  166. for (;;) {
  167. err = vringh_get_head(&vrh, &head);
  168. if (err != 0)
  169. break;
  170. err = vringh_need_notify_user(&vrh);
  171. if (err < 0)
  172. errx(1, "vringh_need_notify_user: %i",
  173. err);
  174. if (err) {
  175. write(to_guest[1], "", 1);
  176. notifies++;
  177. }
  178. }
  179. if (err != 1)
  180. errx(1, "vringh_get_head");
  181. written = 0;
  182. goto complete;
  183. } else {
  184. vringh_iov_init(&riov,
  185. host_riov,
  186. ARRAY_SIZE(host_riov));
  187. vringh_iov_init(&wiov,
  188. host_wiov,
  189. ARRAY_SIZE(host_wiov));
  190. err = vringh_getdesc_user(&vrh, &riov, &wiov,
  191. getrange, &head);
  192. }
  193. if (err == 0) {
  194. err = vringh_need_notify_user(&vrh);
  195. if (err < 0)
  196. errx(1, "vringh_need_notify_user: %i",
  197. err);
  198. if (err) {
  199. write(to_guest[1], "", 1);
  200. notifies++;
  201. }
  202. if (!vringh_notify_enable_user(&vrh))
  203. continue;
  204. /* Swallow all notifies at once. */
  205. if (read(to_host[0], buf, sizeof(buf)) < 1)
  206. break;
  207. vringh_notify_disable_user(&vrh);
  208. receives++;
  209. continue;
  210. }
  211. if (err != 1)
  212. errx(1, "vringh_getdesc_user: %i", err);
  213. /* We simply copy bytes. */
  214. if (riov.used) {
  215. rlen = vringh_iov_pull_user(&riov, rbuf,
  216. sizeof(rbuf));
  217. if (rlen != 4)
  218. errx(1, "vringh_iov_pull_user: %i",
  219. rlen);
  220. assert(riov.i == riov.used);
  221. written = 0;
  222. } else {
  223. err = vringh_iov_push_user(&wiov, rbuf, rlen);
  224. if (err != rlen)
  225. errx(1, "vringh_iov_push_user: %i",
  226. err);
  227. assert(wiov.i == wiov.used);
  228. written = err;
  229. }
  230. complete:
  231. xfers++;
  232. err = vringh_complete_user(&vrh, head, written);
  233. if (err != 0)
  234. errx(1, "vringh_complete_user: %i", err);
  235. }
  236. err = vringh_need_notify_user(&vrh);
  237. if (err < 0)
  238. errx(1, "vringh_need_notify_user: %i", err);
  239. if (err) {
  240. write(to_guest[1], "", 1);
  241. notifies++;
  242. }
  243. wait(&status);
  244. if (!WIFEXITED(status))
  245. errx(1, "Child died with signal %i?", WTERMSIG(status));
  246. if (WEXITSTATUS(status) != 0)
  247. errx(1, "Child exited %i?", WEXITSTATUS(status));
  248. printf("Host: notified %lu, pinged %lu\n", notifies, receives);
  249. return 0;
  250. } else {
  251. struct guest_virtio_device gvdev;
  252. struct virtqueue *vq;
  253. unsigned int *data;
  254. struct vring_desc *indirects;
  255. unsigned int finished = 0;
  256. /* We pass sg[]s pointing into here, but we need RINGSIZE+1 */
  257. data = guest_map + vring_size(RINGSIZE, ALIGN);
  258. indirects = (void *)data + (RINGSIZE + 1) * 2 * sizeof(int);
  259. /* We are the guest. */
  260. munmap(host_map, mapsize);
  261. close(to_guest[1]);
  262. close(to_host[0]);
  263. gvdev.vdev.features = features;
  264. gvdev.to_host_fd = to_host[1];
  265. gvdev.notifies = 0;
  266. CPU_SET(first_cpu, &cpu_set);
  267. if (sched_setaffinity(getpid(), sizeof(cpu_set), &cpu_set))
  268. err(1, "Could not set affinity to cpu %u", first_cpu);
  269. vq = vring_new_virtqueue(0, RINGSIZE, ALIGN, &gvdev.vdev, true,
  270. false, guest_map,
  271. fast_vringh ? no_notify_host
  272. : parallel_notify_host,
  273. never_callback_guest, "guest vq");
  274. /* Don't kfree indirects. */
  275. __kfree_ignore_start = indirects;
  276. __kfree_ignore_end = indirects + RINGSIZE * 6;
  277. while (xfers < NUM_XFERS) {
  278. struct scatterlist sg[4];
  279. unsigned int num_sg, len;
  280. int *dbuf, err;
  281. bool output = !(xfers % 2);
  282. /* Consume bufs. */
  283. while ((dbuf = virtqueue_get_buf(vq, &len)) != NULL) {
  284. if (len == 4)
  285. assert(*dbuf == finished - 1);
  286. else if (!fast_vringh)
  287. assert(*dbuf == finished);
  288. finished++;
  289. }
  290. /* Produce a buffer. */
  291. dbuf = data + (xfers % (RINGSIZE + 1));
  292. if (output)
  293. *dbuf = xfers;
  294. else
  295. *dbuf = -1;
  296. switch ((xfers / sizeof(*dbuf)) % 4) {
  297. case 0:
  298. /* Nasty three-element sg list. */
  299. sg_init_table(sg, num_sg = 3);
  300. sg_set_buf(&sg[0], (void *)dbuf, 1);
  301. sg_set_buf(&sg[1], (void *)dbuf + 1, 2);
  302. sg_set_buf(&sg[2], (void *)dbuf + 3, 1);
  303. break;
  304. case 1:
  305. sg_init_table(sg, num_sg = 2);
  306. sg_set_buf(&sg[0], (void *)dbuf, 1);
  307. sg_set_buf(&sg[1], (void *)dbuf + 1, 3);
  308. break;
  309. case 2:
  310. sg_init_table(sg, num_sg = 1);
  311. sg_set_buf(&sg[0], (void *)dbuf, 4);
  312. break;
  313. case 3:
  314. sg_init_table(sg, num_sg = 4);
  315. sg_set_buf(&sg[0], (void *)dbuf, 1);
  316. sg_set_buf(&sg[1], (void *)dbuf + 1, 1);
  317. sg_set_buf(&sg[2], (void *)dbuf + 2, 1);
  318. sg_set_buf(&sg[3], (void *)dbuf + 3, 1);
  319. break;
  320. }
  321. /* May allocate an indirect, so force it to allocate
  322. * user addr */
  323. __kmalloc_fake = indirects + (xfers % RINGSIZE) * 4;
  324. if (output)
  325. err = virtqueue_add_outbuf(vq, sg, num_sg, dbuf,
  326. GFP_KERNEL);
  327. else
  328. err = virtqueue_add_inbuf(vq, sg, num_sg,
  329. dbuf, GFP_KERNEL);
  330. if (err == -ENOSPC) {
  331. if (!virtqueue_enable_cb_delayed(vq))
  332. continue;
  333. /* Swallow all notifies at once. */
  334. if (read(to_guest[0], buf, sizeof(buf)) < 1)
  335. break;
  336. receives++;
  337. virtqueue_disable_cb(vq);
  338. continue;
  339. }
  340. if (err)
  341. errx(1, "virtqueue_add_in/outbuf: %i", err);
  342. xfers++;
  343. virtqueue_kick(vq);
  344. }
  345. /* Any extra? */
  346. while (finished != xfers) {
  347. int *dbuf;
  348. unsigned int len;
  349. /* Consume bufs. */
  350. dbuf = virtqueue_get_buf(vq, &len);
  351. if (dbuf) {
  352. if (len == 4)
  353. assert(*dbuf == finished - 1);
  354. else
  355. assert(len == 0);
  356. finished++;
  357. continue;
  358. }
  359. if (!virtqueue_enable_cb_delayed(vq))
  360. continue;
  361. if (read(to_guest[0], buf, sizeof(buf)) < 1)
  362. break;
  363. receives++;
  364. virtqueue_disable_cb(vq);
  365. }
  366. printf("Guest: notified %lu, pinged %lu\n",
  367. gvdev.notifies, receives);
  368. vring_del_virtqueue(vq);
  369. return 0;
  370. }
  371. }
  372. int main(int argc, char *argv[])
  373. {
  374. struct virtio_device vdev;
  375. struct virtqueue *vq;
  376. struct vringh vrh;
  377. struct scatterlist guest_sg[RINGSIZE], *sgs[2];
  378. struct iovec host_riov[2], host_wiov[2];
  379. struct vringh_iov riov, wiov;
  380. struct vring_used_elem used[RINGSIZE];
  381. char buf[28];
  382. u16 head;
  383. int err;
  384. unsigned i;
  385. void *ret;
  386. bool (*getrange)(struct vringh *vrh, u64 addr, struct vringh_range *r);
  387. bool fast_vringh = false, parallel = false;
  388. getrange = getrange_iov;
  389. vdev.features = 0;
  390. while (argv[1]) {
  391. if (strcmp(argv[1], "--indirect") == 0)
  392. __virtio_set_bit(&vdev, VIRTIO_RING_F_INDIRECT_DESC);
  393. else if (strcmp(argv[1], "--eventidx") == 0)
  394. __virtio_set_bit(&vdev, VIRTIO_RING_F_EVENT_IDX);
  395. else if (strcmp(argv[1], "--virtio-1") == 0)
  396. __virtio_set_bit(&vdev, VIRTIO_F_VERSION_1);
  397. else if (strcmp(argv[1], "--slow-range") == 0)
  398. getrange = getrange_slow;
  399. else if (strcmp(argv[1], "--fast-vringh") == 0)
  400. fast_vringh = true;
  401. else if (strcmp(argv[1], "--parallel") == 0)
  402. parallel = true;
  403. else
  404. errx(1, "Unknown arg %s", argv[1]);
  405. argv++;
  406. }
  407. if (parallel)
  408. return parallel_test(vdev.features, getrange, fast_vringh);
  409. if (posix_memalign(&__user_addr_min, PAGE_SIZE, USER_MEM) != 0)
  410. abort();
  411. __user_addr_max = __user_addr_min + USER_MEM;
  412. memset(__user_addr_min, 0, vring_size(RINGSIZE, ALIGN));
  413. /* Set up guest side. */
  414. vq = vring_new_virtqueue(0, RINGSIZE, ALIGN, &vdev, true, false,
  415. __user_addr_min,
  416. never_notify_host, never_callback_guest,
  417. "guest vq");
  418. /* Set up host side. */
  419. vring_init(&vrh.vring, RINGSIZE, __user_addr_min, ALIGN);
  420. vringh_init_user(&vrh, vdev.features, RINGSIZE, true,
  421. vrh.vring.desc, vrh.vring.avail, vrh.vring.used);
  422. /* No descriptor to get yet... */
  423. err = vringh_getdesc_user(&vrh, &riov, &wiov, getrange, &head);
  424. if (err != 0)
  425. errx(1, "vringh_getdesc_user: %i", err);
  426. /* Guest puts in a descriptor. */
  427. memcpy(__user_addr_max - 1, "a", 1);
  428. sg_init_table(guest_sg, 1);
  429. sg_set_buf(&guest_sg[0], __user_addr_max - 1, 1);
  430. sg_init_table(guest_sg+1, 1);
  431. sg_set_buf(&guest_sg[1], __user_addr_max - 3, 2);
  432. sgs[0] = &guest_sg[0];
  433. sgs[1] = &guest_sg[1];
  434. /* May allocate an indirect, so force it to allocate user addr */
  435. __kmalloc_fake = __user_addr_min + vring_size(RINGSIZE, ALIGN);
  436. err = virtqueue_add_sgs(vq, sgs, 1, 1, &err, GFP_KERNEL);
  437. if (err)
  438. errx(1, "virtqueue_add_sgs: %i", err);
  439. __kmalloc_fake = NULL;
  440. /* Host retreives it. */
  441. vringh_iov_init(&riov, host_riov, ARRAY_SIZE(host_riov));
  442. vringh_iov_init(&wiov, host_wiov, ARRAY_SIZE(host_wiov));
  443. err = vringh_getdesc_user(&vrh, &riov, &wiov, getrange, &head);
  444. if (err != 1)
  445. errx(1, "vringh_getdesc_user: %i", err);
  446. assert(riov.used == 1);
  447. assert(riov.iov[0].iov_base == __user_addr_max - 1);
  448. assert(riov.iov[0].iov_len == 1);
  449. if (getrange != getrange_slow) {
  450. assert(wiov.used == 1);
  451. assert(wiov.iov[0].iov_base == __user_addr_max - 3);
  452. assert(wiov.iov[0].iov_len == 2);
  453. } else {
  454. assert(wiov.used == 2);
  455. assert(wiov.iov[0].iov_base == __user_addr_max - 3);
  456. assert(wiov.iov[0].iov_len == 1);
  457. assert(wiov.iov[1].iov_base == __user_addr_max - 2);
  458. assert(wiov.iov[1].iov_len == 1);
  459. }
  460. err = vringh_iov_pull_user(&riov, buf, 5);
  461. if (err != 1)
  462. errx(1, "vringh_iov_pull_user: %i", err);
  463. assert(buf[0] == 'a');
  464. assert(riov.i == 1);
  465. assert(vringh_iov_pull_user(&riov, buf, 5) == 0);
  466. memcpy(buf, "bcdef", 5);
  467. err = vringh_iov_push_user(&wiov, buf, 5);
  468. if (err != 2)
  469. errx(1, "vringh_iov_push_user: %i", err);
  470. assert(memcmp(__user_addr_max - 3, "bc", 2) == 0);
  471. assert(wiov.i == wiov.used);
  472. assert(vringh_iov_push_user(&wiov, buf, 5) == 0);
  473. /* Host is done. */
  474. err = vringh_complete_user(&vrh, head, err);
  475. if (err != 0)
  476. errx(1, "vringh_complete_user: %i", err);
  477. /* Guest should see used token now. */
  478. __kfree_ignore_start = __user_addr_min + vring_size(RINGSIZE, ALIGN);
  479. __kfree_ignore_end = __kfree_ignore_start + 1;
  480. ret = virtqueue_get_buf(vq, &i);
  481. if (ret != &err)
  482. errx(1, "virtqueue_get_buf: %p", ret);
  483. assert(i == 2);
  484. /* Guest puts in a huge descriptor. */
  485. sg_init_table(guest_sg, RINGSIZE);
  486. for (i = 0; i < RINGSIZE; i++) {
  487. sg_set_buf(&guest_sg[i],
  488. __user_addr_max - USER_MEM/4, USER_MEM/4);
  489. }
  490. /* Fill contents with recognisable garbage. */
  491. for (i = 0; i < USER_MEM/4; i++)
  492. ((char *)__user_addr_max - USER_MEM/4)[i] = i;
  493. /* This will allocate an indirect, so force it to allocate user addr */
  494. __kmalloc_fake = __user_addr_min + vring_size(RINGSIZE, ALIGN);
  495. err = virtqueue_add_outbuf(vq, guest_sg, RINGSIZE, &err, GFP_KERNEL);
  496. if (err)
  497. errx(1, "virtqueue_add_outbuf (large): %i", err);
  498. __kmalloc_fake = NULL;
  499. /* Host picks it up (allocates new iov). */
  500. vringh_iov_init(&riov, host_riov, ARRAY_SIZE(host_riov));
  501. vringh_iov_init(&wiov, host_wiov, ARRAY_SIZE(host_wiov));
  502. err = vringh_getdesc_user(&vrh, &riov, &wiov, getrange, &head);
  503. if (err != 1)
  504. errx(1, "vringh_getdesc_user: %i", err);
  505. assert(riov.max_num & VRINGH_IOV_ALLOCATED);
  506. assert(riov.iov != host_riov);
  507. if (getrange != getrange_slow)
  508. assert(riov.used == RINGSIZE);
  509. else
  510. assert(riov.used == RINGSIZE * USER_MEM/4);
  511. assert(!(wiov.max_num & VRINGH_IOV_ALLOCATED));
  512. assert(wiov.used == 0);
  513. /* Pull data back out (in odd chunks), should be as expected. */
  514. for (i = 0; i < RINGSIZE * USER_MEM/4; i += 3) {
  515. err = vringh_iov_pull_user(&riov, buf, 3);
  516. if (err != 3 && i + err != RINGSIZE * USER_MEM/4)
  517. errx(1, "vringh_iov_pull_user large: %i", err);
  518. assert(buf[0] == (char)i);
  519. assert(err < 2 || buf[1] == (char)(i + 1));
  520. assert(err < 3 || buf[2] == (char)(i + 2));
  521. }
  522. assert(riov.i == riov.used);
  523. vringh_iov_cleanup(&riov);
  524. vringh_iov_cleanup(&wiov);
  525. /* Complete using multi interface, just because we can. */
  526. used[0].id = head;
  527. used[0].len = 0;
  528. err = vringh_complete_multi_user(&vrh, used, 1);
  529. if (err)
  530. errx(1, "vringh_complete_multi_user(1): %i", err);
  531. /* Free up those descriptors. */
  532. ret = virtqueue_get_buf(vq, &i);
  533. if (ret != &err)
  534. errx(1, "virtqueue_get_buf: %p", ret);
  535. /* Add lots of descriptors. */
  536. sg_init_table(guest_sg, 1);
  537. sg_set_buf(&guest_sg[0], __user_addr_max - 1, 1);
  538. for (i = 0; i < RINGSIZE; i++) {
  539. err = virtqueue_add_outbuf(vq, guest_sg, 1, &err, GFP_KERNEL);
  540. if (err)
  541. errx(1, "virtqueue_add_outbuf (multiple): %i", err);
  542. }
  543. /* Now get many, and consume them all at once. */
  544. vringh_iov_init(&riov, host_riov, ARRAY_SIZE(host_riov));
  545. vringh_iov_init(&wiov, host_wiov, ARRAY_SIZE(host_wiov));
  546. for (i = 0; i < RINGSIZE; i++) {
  547. err = vringh_getdesc_user(&vrh, &riov, &wiov, getrange, &head);
  548. if (err != 1)
  549. errx(1, "vringh_getdesc_user: %i", err);
  550. used[i].id = head;
  551. used[i].len = 0;
  552. }
  553. /* Make sure it wraps around ring, to test! */
  554. assert(vrh.vring.used->idx % RINGSIZE != 0);
  555. err = vringh_complete_multi_user(&vrh, used, RINGSIZE);
  556. if (err)
  557. errx(1, "vringh_complete_multi_user: %i", err);
  558. /* Free those buffers. */
  559. for (i = 0; i < RINGSIZE; i++) {
  560. unsigned len;
  561. assert(virtqueue_get_buf(vq, &len) != NULL);
  562. }
  563. /* Test weird (but legal!) indirect. */
  564. if (__virtio_test_bit(&vdev, VIRTIO_RING_F_INDIRECT_DESC)) {
  565. char *data = __user_addr_max - USER_MEM/4;
  566. struct vring_desc *d = __user_addr_max - USER_MEM/2;
  567. struct vring vring;
  568. /* Force creation of direct, which we modify. */
  569. __virtio_clear_bit(&vdev, VIRTIO_RING_F_INDIRECT_DESC);
  570. vq = vring_new_virtqueue(0, RINGSIZE, ALIGN, &vdev, true,
  571. false, __user_addr_min,
  572. never_notify_host,
  573. never_callback_guest,
  574. "guest vq");
  575. sg_init_table(guest_sg, 4);
  576. sg_set_buf(&guest_sg[0], d, sizeof(*d)*2);
  577. sg_set_buf(&guest_sg[1], d + 2, sizeof(*d)*1);
  578. sg_set_buf(&guest_sg[2], data + 6, 4);
  579. sg_set_buf(&guest_sg[3], d + 3, sizeof(*d)*3);
  580. err = virtqueue_add_outbuf(vq, guest_sg, 4, &err, GFP_KERNEL);
  581. if (err)
  582. errx(1, "virtqueue_add_outbuf (indirect): %i", err);
  583. vring_init(&vring, RINGSIZE, __user_addr_min, ALIGN);
  584. /* They're used in order, but double-check... */
  585. assert(vring.desc[0].addr == (unsigned long)d);
  586. assert(vring.desc[1].addr == (unsigned long)(d+2));
  587. assert(vring.desc[2].addr == (unsigned long)data + 6);
  588. assert(vring.desc[3].addr == (unsigned long)(d+3));
  589. vring.desc[0].flags |= VRING_DESC_F_INDIRECT;
  590. vring.desc[1].flags |= VRING_DESC_F_INDIRECT;
  591. vring.desc[3].flags |= VRING_DESC_F_INDIRECT;
  592. /* First indirect */
  593. d[0].addr = (unsigned long)data;
  594. d[0].len = 1;
  595. d[0].flags = VRING_DESC_F_NEXT;
  596. d[0].next = 1;
  597. d[1].addr = (unsigned long)data + 1;
  598. d[1].len = 2;
  599. d[1].flags = 0;
  600. /* Second indirect */
  601. d[2].addr = (unsigned long)data + 3;
  602. d[2].len = 3;
  603. d[2].flags = 0;
  604. /* Third indirect */
  605. d[3].addr = (unsigned long)data + 10;
  606. d[3].len = 5;
  607. d[3].flags = VRING_DESC_F_NEXT;
  608. d[3].next = 1;
  609. d[4].addr = (unsigned long)data + 15;
  610. d[4].len = 6;
  611. d[4].flags = VRING_DESC_F_NEXT;
  612. d[4].next = 2;
  613. d[5].addr = (unsigned long)data + 21;
  614. d[5].len = 7;
  615. d[5].flags = 0;
  616. /* Host picks it up (allocates new iov). */
  617. vringh_iov_init(&riov, host_riov, ARRAY_SIZE(host_riov));
  618. vringh_iov_init(&wiov, host_wiov, ARRAY_SIZE(host_wiov));
  619. err = vringh_getdesc_user(&vrh, &riov, &wiov, getrange, &head);
  620. if (err != 1)
  621. errx(1, "vringh_getdesc_user: %i", err);
  622. if (head != 0)
  623. errx(1, "vringh_getdesc_user: head %i not 0", head);
  624. assert(riov.max_num & VRINGH_IOV_ALLOCATED);
  625. if (getrange != getrange_slow)
  626. assert(riov.used == 7);
  627. else
  628. assert(riov.used == 28);
  629. err = vringh_iov_pull_user(&riov, buf, 29);
  630. assert(err == 28);
  631. /* Data should be linear. */
  632. for (i = 0; i < err; i++)
  633. assert(buf[i] == i);
  634. vringh_iov_cleanup(&riov);
  635. }
  636. /* Don't leak memory... */
  637. vring_del_virtqueue(vq);
  638. free(__user_addr_min);
  639. return 0;
  640. }