vringh_test.c 20 KB

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