msg-ring.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test ring messaging command
  5. *
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <fcntl.h>
  13. #include <pthread.h>
  14. #include "liburing.h"
  15. #include "helpers.h"
  16. static int no_msg;
  17. static int test_own(struct io_uring *ring)
  18. {
  19. struct io_uring_cqe *cqe;
  20. struct io_uring_sqe *sqe;
  21. int ret, i;
  22. sqe = io_uring_get_sqe(ring);
  23. if (!sqe) {
  24. fprintf(stderr, "get sqe failed\n");
  25. goto err;
  26. }
  27. io_uring_prep_msg_ring(sqe, ring->ring_fd, 0x10, 0x1234, 0);
  28. sqe->user_data = 1;
  29. ret = io_uring_submit(ring);
  30. if (ret <= 0) {
  31. fprintf(stderr, "sqe submit failed: %d\n", ret);
  32. goto err;
  33. }
  34. for (i = 0; i < 2; i++) {
  35. ret = io_uring_wait_cqe(ring, &cqe);
  36. if (ret < 0) {
  37. fprintf(stderr, "wait completion %d\n", ret);
  38. goto err;
  39. }
  40. switch (cqe->user_data) {
  41. case 1:
  42. if (cqe->res == -EINVAL || cqe->res == -EOPNOTSUPP) {
  43. no_msg = 1;
  44. return 0;
  45. }
  46. if (cqe->res != 0) {
  47. fprintf(stderr, "cqe res %d\n", cqe->res);
  48. return -1;
  49. }
  50. break;
  51. case 0x1234:
  52. if (cqe->res != 0x10) {
  53. fprintf(stderr, "invalid len %x\n", cqe->res);
  54. return -1;
  55. }
  56. break;
  57. default:
  58. fprintf(stderr, "Invalid user_data\n");
  59. return -1;
  60. }
  61. io_uring_cqe_seen(ring, cqe);
  62. }
  63. return 0;
  64. err:
  65. return 1;
  66. }
  67. struct data {
  68. struct io_uring *ring;
  69. unsigned int flags;
  70. pthread_barrier_t startup;
  71. pthread_barrier_t barrier;
  72. };
  73. static void *wait_cqe_fn(void *__data)
  74. {
  75. struct data *d = __data;
  76. struct io_uring_cqe *cqe;
  77. struct io_uring ring;
  78. int ret;
  79. io_uring_queue_init(4, &ring, d->flags);
  80. d->ring = &ring;
  81. pthread_barrier_wait(&d->startup);
  82. pthread_barrier_wait(&d->barrier);
  83. ret = io_uring_wait_cqe(&ring, &cqe);
  84. if (ret) {
  85. fprintf(stderr, "wait cqe %d\n", ret);
  86. goto err;
  87. }
  88. if (cqe->user_data != 0x5aa5) {
  89. fprintf(stderr, "user_data %llx\n", (long long) cqe->user_data);
  90. goto err;
  91. }
  92. if (cqe->res != 0x20) {
  93. fprintf(stderr, "len %x\n", cqe->res);
  94. goto err;
  95. }
  96. io_uring_cqe_seen(&ring, cqe);
  97. io_uring_queue_exit(&ring);
  98. return NULL;
  99. err:
  100. io_uring_cqe_seen(&ring, cqe);
  101. io_uring_queue_exit(&ring);
  102. return (void *) (unsigned long) 1;
  103. }
  104. static int test_remote(struct io_uring *ring, unsigned int ring_flags)
  105. {
  106. struct io_uring *target;
  107. pthread_t thread;
  108. void *tret;
  109. struct io_uring_cqe *cqe;
  110. struct io_uring_sqe *sqe;
  111. struct data d;
  112. int ret;
  113. d.flags = ring_flags;
  114. pthread_barrier_init(&d.barrier, NULL, 2);
  115. pthread_barrier_init(&d.startup, NULL, 2);
  116. pthread_create(&thread, NULL, wait_cqe_fn, &d);
  117. pthread_barrier_wait(&d.startup);
  118. target = d.ring;
  119. sqe = io_uring_get_sqe(ring);
  120. if (!sqe) {
  121. fprintf(stderr, "get sqe failed\n");
  122. goto err;
  123. }
  124. io_uring_prep_msg_ring(sqe, target->ring_fd, 0x20, 0x5aa5, 0);
  125. sqe->user_data = 1;
  126. ret = io_uring_submit(ring);
  127. if (ret <= 0) {
  128. fprintf(stderr, "sqe submit failed: %d\n", ret);
  129. goto err;
  130. }
  131. pthread_barrier_wait(&d.barrier);
  132. ret = io_uring_wait_cqe(ring, &cqe);
  133. if (ret < 0) {
  134. fprintf(stderr, "wait completion %d\n", ret);
  135. goto err;
  136. }
  137. if (cqe->res != 0) {
  138. fprintf(stderr, "cqe res %d\n", cqe->res);
  139. io_uring_cqe_seen(ring, cqe);
  140. return -1;
  141. }
  142. if (cqe->user_data != 1) {
  143. fprintf(stderr, "user_data %llx\n", (long long) cqe->user_data);
  144. io_uring_cqe_seen(ring, cqe);
  145. return -1;
  146. }
  147. io_uring_cqe_seen(ring, cqe);
  148. pthread_join(thread, &tret);
  149. return 0;
  150. err:
  151. return 1;
  152. }
  153. static void *remote_submit_fn(void *data)
  154. {
  155. struct io_uring_sqe *sqe;
  156. struct io_uring_cqe *cqe;
  157. struct io_uring *target = data;
  158. struct io_uring ring;
  159. int ret;
  160. ret = io_uring_queue_init(8, &ring, 0);
  161. if (ret) {
  162. fprintf(stderr, "thread ring setup failed: %d\n", ret);
  163. goto err;
  164. }
  165. sqe = io_uring_get_sqe(&ring);
  166. if (!sqe) {
  167. fprintf(stderr, "get sqe failed\n");
  168. goto err;
  169. }
  170. io_uring_prep_msg_ring(sqe, target->ring_fd, 0x20, 0x5aa5, 0);
  171. sqe->user_data = 1;
  172. ret = io_uring_submit(&ring);
  173. if (ret <= 0) {
  174. fprintf(stderr, "sqe submit failed: %d\n", ret);
  175. goto err;
  176. }
  177. ret = io_uring_wait_cqe(&ring, &cqe);
  178. if (ret < 0) {
  179. fprintf(stderr, "wait completion %d\n", ret);
  180. goto err;
  181. }
  182. if (cqe->res != 0 || cqe->user_data != 1) {
  183. fprintf(stderr, "invalid cqe\n");
  184. goto err;
  185. }
  186. io_uring_cqe_seen(&ring, cqe);
  187. io_uring_queue_exit(&ring);
  188. return NULL;
  189. err:
  190. return (void *) (unsigned long) 1;
  191. }
  192. static int test_remote_submit(struct io_uring *target)
  193. {
  194. struct io_uring_cqe *cqe;
  195. pthread_t thread;
  196. void *tret;
  197. int ret;
  198. pthread_create(&thread, NULL, remote_submit_fn, target);
  199. ret = io_uring_wait_cqe(target, &cqe);
  200. if (ret < 0) {
  201. fprintf(stderr, "wait completion %d\n", ret);
  202. goto err;
  203. }
  204. if (cqe->res != 0x20) {
  205. fprintf(stderr, "cqe res %d\n", cqe->res);
  206. return -1;
  207. }
  208. if (cqe->user_data != 0x5aa5) {
  209. fprintf(stderr, "user_data %llx\n", (long long) cqe->user_data);
  210. return -1;
  211. }
  212. io_uring_cqe_seen(target, cqe);
  213. pthread_join(thread, &tret);
  214. return 0;
  215. err:
  216. return 1;
  217. }
  218. static int test_invalid(struct io_uring *ring, bool fixed)
  219. {
  220. struct io_uring_cqe *cqe;
  221. struct io_uring_sqe *sqe;
  222. int ret, fd = 1;
  223. sqe = io_uring_get_sqe(ring);
  224. if (!sqe) {
  225. fprintf(stderr, "get sqe failed\n");
  226. return 1;
  227. }
  228. if (fixed) {
  229. ret = io_uring_register_files(ring, &fd, 1);
  230. if (ret) {
  231. fprintf(stderr, "file register %d\n", ret);
  232. return 1;
  233. }
  234. io_uring_prep_msg_ring(sqe, 0, 0, 0x8989, 0);
  235. sqe->flags |= IOSQE_FIXED_FILE;
  236. } else {
  237. io_uring_prep_msg_ring(sqe, 1, 0, 0x8989, 0);
  238. }
  239. sqe->user_data = 1;
  240. ret = io_uring_submit(ring);
  241. if (ret <= 0) {
  242. fprintf(stderr, "sqe submit failed: %d\n", ret);
  243. goto err;
  244. }
  245. ret = io_uring_wait_cqe(ring, &cqe);
  246. if (ret < 0) {
  247. fprintf(stderr, "wait completion %d\n", ret);
  248. goto err;
  249. }
  250. if (cqe->res != -EBADFD) {
  251. fprintf(stderr, "cqe res %d\n", cqe->res);
  252. return -1;
  253. }
  254. io_uring_cqe_seen(ring, cqe);
  255. if (fixed)
  256. io_uring_unregister_files(ring);
  257. return 0;
  258. err:
  259. if (fixed)
  260. io_uring_unregister_files(ring);
  261. return 1;
  262. }
  263. static int test_disabled_ring(struct io_uring *ring, int flags)
  264. {
  265. struct io_uring_cqe *cqe;
  266. struct io_uring_sqe *sqe;
  267. struct io_uring disabled_ring;
  268. int ret;
  269. flags |= IORING_SETUP_R_DISABLED;
  270. ret = io_uring_queue_init(8, &disabled_ring, flags);
  271. if (ret) {
  272. if (ret == -EINVAL)
  273. return T_EXIT_SKIP;
  274. fprintf(stderr, "ring setup failed: %d\n", ret);
  275. return 1;
  276. }
  277. sqe = io_uring_get_sqe(ring);
  278. io_uring_prep_msg_ring(sqe, disabled_ring.ring_fd, 0x10, 0x1234, 0);
  279. sqe->user_data = 1;
  280. ret = io_uring_submit(ring);
  281. if (ret != 1) {
  282. fprintf(stderr, "sqe submit failed: %d\n", ret);
  283. return 1;
  284. }
  285. ret = io_uring_wait_cqe(ring, &cqe);
  286. if (ret < 0) {
  287. fprintf(stderr, "wait completion %d\n", ret);
  288. return 1;
  289. }
  290. if (cqe->res != 0 && cqe->res != -EBADFD) {
  291. fprintf(stderr, "cqe res %d\n", cqe->res);
  292. return 1;
  293. }
  294. if (cqe->user_data != 1) {
  295. fprintf(stderr, "user_data %llx\n", (long long) cqe->user_data);
  296. return 1;
  297. }
  298. io_uring_cqe_seen(ring, cqe);
  299. io_uring_queue_exit(&disabled_ring);
  300. return 0;
  301. }
  302. static int test(int ring_flags)
  303. {
  304. struct io_uring ring, ring2, pring;
  305. int ret, i;
  306. ret = io_uring_queue_init(8, &ring, ring_flags);
  307. if (ret) {
  308. if (ret == -EINVAL)
  309. return T_EXIT_SKIP;
  310. fprintf(stderr, "ring setup failed: %d\n", ret);
  311. return T_EXIT_FAIL;
  312. }
  313. ret = io_uring_queue_init(8, &ring2, ring_flags);
  314. if (ret) {
  315. fprintf(stderr, "ring setup failed: %d\n", ret);
  316. return T_EXIT_FAIL;
  317. }
  318. ret = io_uring_queue_init(8, &pring, ring_flags | IORING_SETUP_IOPOLL);
  319. if (ret) {
  320. fprintf(stderr, "ring setup failed: %d\n", ret);
  321. return T_EXIT_FAIL;
  322. }
  323. ret = test_own(&ring);
  324. if (ret) {
  325. fprintf(stderr, "test_own failed\n");
  326. return T_EXIT_FAIL;
  327. }
  328. if (no_msg)
  329. return T_EXIT_SKIP;
  330. ret = test_own(&pring);
  331. if (ret) {
  332. fprintf(stderr, "test_own iopoll failed\n");
  333. return T_EXIT_FAIL;
  334. }
  335. ret = test_invalid(&ring, 0);
  336. if (ret) {
  337. fprintf(stderr, "test_invalid failed\n");
  338. return T_EXIT_FAIL;
  339. }
  340. for (i = 0; i < 2; i++) {
  341. ret = test_invalid(&ring, 1);
  342. if (ret) {
  343. fprintf(stderr, "test_invalid fixed failed\n");
  344. return T_EXIT_FAIL;
  345. }
  346. }
  347. ret = test_remote(&ring, ring_flags);
  348. if (ret) {
  349. fprintf(stderr, "test_remote failed\n");
  350. return T_EXIT_FAIL;
  351. }
  352. io_uring_queue_exit(&ring);
  353. io_uring_queue_exit(&pring);
  354. if (t_probe_defer_taskrun()) {
  355. ret = io_uring_queue_init(8, &ring, IORING_SETUP_SINGLE_ISSUER |
  356. IORING_SETUP_DEFER_TASKRUN);
  357. if (ret) {
  358. fprintf(stderr, "deferred ring setup failed: %d\n", ret);
  359. return T_EXIT_FAIL;
  360. }
  361. ret = test_own(&ring);
  362. if (ret) {
  363. fprintf(stderr, "test_own deferred failed\n");
  364. return T_EXIT_FAIL;
  365. }
  366. for (i = 0; i < 2; i++) {
  367. ret = test_invalid(&ring, i);
  368. if (ret) {
  369. fprintf(stderr, "test_invalid(0) deferred failed\n");
  370. return T_EXIT_FAIL;
  371. }
  372. }
  373. ret = test_remote_submit(&ring);
  374. if (ret) {
  375. fprintf(stderr, "test_remote_submit failed\n");
  376. return T_EXIT_FAIL;
  377. }
  378. io_uring_queue_exit(&ring);
  379. if (test_disabled_ring(&ring2, 0)) {
  380. fprintf(stderr, "test_disabled_ring failed\n");
  381. return T_EXIT_FAIL;
  382. }
  383. if (test_disabled_ring(&ring2, IORING_SETUP_SINGLE_ISSUER |
  384. IORING_SETUP_DEFER_TASKRUN)) {
  385. fprintf(stderr, "test_disabled_ring defer failed\n");
  386. return T_EXIT_FAIL;
  387. }
  388. }
  389. io_uring_queue_exit(&ring2);
  390. return T_EXIT_PASS;
  391. }
  392. int main(int argc, char *argv[])
  393. {
  394. int ret;
  395. if (argc > 1)
  396. return T_EXIT_SKIP;
  397. ret = test(0);
  398. if (ret == T_EXIT_FAIL) {
  399. fprintf(stderr, "ring flags 0 failed\n");
  400. return ret;
  401. } else if (ret == T_EXIT_SKIP) {
  402. return T_EXIT_SKIP;
  403. }
  404. ret = test(IORING_SETUP_SINGLE_ISSUER|IORING_SETUP_DEFER_TASKRUN);
  405. if (ret == T_EXIT_FAIL) {
  406. fprintf(stderr, "ring flags defer failed\n");
  407. return ret;
  408. }
  409. return ret;
  410. }