msg-ring.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. static void *wait_cqe_fn(void *data)
  68. {
  69. struct io_uring *ring = data;
  70. struct io_uring_cqe *cqe;
  71. int ret;
  72. ret = io_uring_wait_cqe(ring, &cqe);
  73. if (ret) {
  74. fprintf(stderr, "wait cqe %d\n", ret);
  75. goto err;
  76. }
  77. if (cqe->user_data != 0x5aa5) {
  78. fprintf(stderr, "user_data %llx\n", (long long) cqe->user_data);
  79. goto err;
  80. }
  81. if (cqe->res != 0x20) {
  82. fprintf(stderr, "len %x\n", cqe->res);
  83. goto err;
  84. }
  85. io_uring_cqe_seen(ring, cqe);
  86. return NULL;
  87. err:
  88. io_uring_cqe_seen(ring, cqe);
  89. return (void *) (unsigned long) 1;
  90. }
  91. static int test_remote(struct io_uring *ring, struct io_uring *target)
  92. {
  93. pthread_t thread;
  94. void *tret;
  95. struct io_uring_cqe *cqe;
  96. struct io_uring_sqe *sqe;
  97. int ret;
  98. pthread_create(&thread, NULL, wait_cqe_fn, target);
  99. sqe = io_uring_get_sqe(ring);
  100. if (!sqe) {
  101. fprintf(stderr, "get sqe failed\n");
  102. goto err;
  103. }
  104. io_uring_prep_msg_ring(sqe, target->ring_fd, 0x20, 0x5aa5, 0);
  105. sqe->user_data = 1;
  106. ret = io_uring_submit(ring);
  107. if (ret <= 0) {
  108. fprintf(stderr, "sqe submit failed: %d\n", ret);
  109. goto err;
  110. }
  111. ret = io_uring_wait_cqe(ring, &cqe);
  112. if (ret < 0) {
  113. fprintf(stderr, "wait completion %d\n", ret);
  114. goto err;
  115. }
  116. if (cqe->res != 0) {
  117. fprintf(stderr, "cqe res %d\n", cqe->res);
  118. return -1;
  119. }
  120. if (cqe->user_data != 1) {
  121. fprintf(stderr, "user_data %llx\n", (long long) cqe->user_data);
  122. return -1;
  123. }
  124. io_uring_cqe_seen(ring, cqe);
  125. pthread_join(thread, &tret);
  126. return 0;
  127. err:
  128. return 1;
  129. }
  130. static void *remote_submit_fn(void *data)
  131. {
  132. struct io_uring_sqe *sqe;
  133. struct io_uring_cqe *cqe;
  134. struct io_uring *target = data;
  135. struct io_uring ring;
  136. int ret;
  137. ret = io_uring_queue_init(8, &ring, 0);
  138. if (ret) {
  139. fprintf(stderr, "thread ring setup failed: %d\n", ret);
  140. goto err;
  141. }
  142. sqe = io_uring_get_sqe(&ring);
  143. if (!sqe) {
  144. fprintf(stderr, "get sqe failed\n");
  145. goto err;
  146. }
  147. io_uring_prep_msg_ring(sqe, target->ring_fd, 0x20, 0x5aa5, 0);
  148. sqe->user_data = 1;
  149. ret = io_uring_submit(&ring);
  150. if (ret <= 0) {
  151. fprintf(stderr, "sqe submit failed: %d\n", ret);
  152. goto err;
  153. }
  154. ret = io_uring_wait_cqe(&ring, &cqe);
  155. if (ret < 0) {
  156. fprintf(stderr, "wait completion %d\n", ret);
  157. goto err;
  158. }
  159. if (cqe->res != 0 || cqe->user_data != 1) {
  160. fprintf(stderr, "invalid cqe\n");
  161. goto err;
  162. }
  163. io_uring_cqe_seen(&ring, cqe);
  164. io_uring_queue_exit(&ring);
  165. return NULL;
  166. err:
  167. return (void *) (unsigned long) 1;
  168. }
  169. static int test_remote_submit(struct io_uring *target)
  170. {
  171. struct io_uring_cqe *cqe;
  172. pthread_t thread;
  173. void *tret;
  174. int ret;
  175. pthread_create(&thread, NULL, remote_submit_fn, target);
  176. ret = io_uring_wait_cqe(target, &cqe);
  177. if (ret < 0) {
  178. fprintf(stderr, "wait completion %d\n", ret);
  179. goto err;
  180. }
  181. if (cqe->res != 0x20) {
  182. fprintf(stderr, "cqe res %d\n", cqe->res);
  183. return -1;
  184. }
  185. if (cqe->user_data != 0x5aa5) {
  186. fprintf(stderr, "user_data %llx\n", (long long) cqe->user_data);
  187. return -1;
  188. }
  189. io_uring_cqe_seen(target, cqe);
  190. pthread_join(thread, &tret);
  191. return 0;
  192. err:
  193. return 1;
  194. }
  195. static int test_invalid(struct io_uring *ring, bool fixed)
  196. {
  197. struct io_uring_cqe *cqe;
  198. struct io_uring_sqe *sqe;
  199. int ret, fd = 1;
  200. sqe = io_uring_get_sqe(ring);
  201. if (!sqe) {
  202. fprintf(stderr, "get sqe failed\n");
  203. return 1;
  204. }
  205. if (fixed) {
  206. ret = io_uring_register_files(ring, &fd, 1);
  207. if (ret) {
  208. fprintf(stderr, "file register %d\n", ret);
  209. return 1;
  210. }
  211. io_uring_prep_msg_ring(sqe, 0, 0, 0x8989, 0);
  212. sqe->flags |= IOSQE_FIXED_FILE;
  213. } else {
  214. io_uring_prep_msg_ring(sqe, 1, 0, 0x8989, 0);
  215. }
  216. sqe->user_data = 1;
  217. ret = io_uring_submit(ring);
  218. if (ret <= 0) {
  219. fprintf(stderr, "sqe submit failed: %d\n", ret);
  220. goto err;
  221. }
  222. ret = io_uring_wait_cqe(ring, &cqe);
  223. if (ret < 0) {
  224. fprintf(stderr, "wait completion %d\n", ret);
  225. goto err;
  226. }
  227. if (cqe->res != -EBADFD) {
  228. fprintf(stderr, "cqe res %d\n", cqe->res);
  229. return -1;
  230. }
  231. io_uring_cqe_seen(ring, cqe);
  232. if (fixed)
  233. io_uring_unregister_files(ring);
  234. return 0;
  235. err:
  236. if (fixed)
  237. io_uring_unregister_files(ring);
  238. return 1;
  239. }
  240. static int test_disabled_ring(struct io_uring *ring, int flags)
  241. {
  242. struct io_uring_cqe *cqe;
  243. struct io_uring_sqe *sqe;
  244. struct io_uring disabled_ring;
  245. int ret;
  246. flags |= IORING_SETUP_R_DISABLED;
  247. ret = io_uring_queue_init(8, &disabled_ring, flags);
  248. if (ret) {
  249. fprintf(stderr, "ring setup failed: %d\n", ret);
  250. return 1;
  251. }
  252. sqe = io_uring_get_sqe(ring);
  253. io_uring_prep_msg_ring(sqe, disabled_ring.ring_fd, 0x10, 0x1234, 0);
  254. sqe->user_data = 1;
  255. ret = io_uring_submit(ring);
  256. if (ret != 1) {
  257. fprintf(stderr, "sqe submit failed: %d\n", ret);
  258. return 1;
  259. }
  260. ret = io_uring_wait_cqe(ring, &cqe);
  261. if (ret < 0) {
  262. fprintf(stderr, "wait completion %d\n", ret);
  263. return 1;
  264. }
  265. if (cqe->res != 0 && cqe->res != -EBADFD) {
  266. fprintf(stderr, "cqe res %d\n", cqe->res);
  267. return 1;
  268. }
  269. if (cqe->user_data != 1) {
  270. fprintf(stderr, "user_data %llx\n", (long long) cqe->user_data);
  271. return 1;
  272. }
  273. io_uring_cqe_seen(ring, cqe);
  274. io_uring_queue_exit(&disabled_ring);
  275. return 0;
  276. }
  277. int main(int argc, char *argv[])
  278. {
  279. struct io_uring ring, ring2, pring;
  280. int ret, i;
  281. if (argc > 1)
  282. return T_EXIT_SKIP;
  283. ret = io_uring_queue_init(8, &ring, 0);
  284. if (ret) {
  285. fprintf(stderr, "ring setup failed: %d\n", ret);
  286. return T_EXIT_FAIL;
  287. }
  288. ret = io_uring_queue_init(8, &ring2, 0);
  289. if (ret) {
  290. fprintf(stderr, "ring setup failed: %d\n", ret);
  291. return T_EXIT_FAIL;
  292. }
  293. ret = io_uring_queue_init(8, &pring, IORING_SETUP_IOPOLL);
  294. if (ret) {
  295. fprintf(stderr, "ring setup failed: %d\n", ret);
  296. return T_EXIT_FAIL;
  297. }
  298. ret = test_own(&ring);
  299. if (ret) {
  300. fprintf(stderr, "test_own failed\n");
  301. return T_EXIT_FAIL;
  302. }
  303. if (no_msg)
  304. return T_EXIT_SKIP;
  305. ret = test_own(&pring);
  306. if (ret) {
  307. fprintf(stderr, "test_own iopoll failed\n");
  308. return T_EXIT_FAIL;
  309. }
  310. ret = test_invalid(&ring, 0);
  311. if (ret) {
  312. fprintf(stderr, "test_invalid failed\n");
  313. return T_EXIT_FAIL;
  314. }
  315. for (i = 0; i < 2; i++) {
  316. ret = test_invalid(&ring, 1);
  317. if (ret) {
  318. fprintf(stderr, "test_invalid fixed failed\n");
  319. return T_EXIT_FAIL;
  320. }
  321. }
  322. ret = test_remote(&ring, &ring2);
  323. if (ret) {
  324. fprintf(stderr, "test_remote failed\n");
  325. return T_EXIT_FAIL;
  326. }
  327. io_uring_queue_exit(&ring);
  328. io_uring_queue_exit(&pring);
  329. if (t_probe_defer_taskrun()) {
  330. ret = io_uring_queue_init(8, &ring, IORING_SETUP_SINGLE_ISSUER |
  331. IORING_SETUP_DEFER_TASKRUN);
  332. if (ret) {
  333. fprintf(stderr, "deferred ring setup failed: %d\n", ret);
  334. return T_EXIT_FAIL;
  335. }
  336. ret = test_own(&ring);
  337. if (ret) {
  338. fprintf(stderr, "test_own deferred failed\n");
  339. return T_EXIT_FAIL;
  340. }
  341. for (i = 0; i < 2; i++) {
  342. ret = test_invalid(&ring, i);
  343. if (ret) {
  344. fprintf(stderr, "test_invalid(0) deferred failed\n");
  345. return T_EXIT_FAIL;
  346. }
  347. }
  348. ret = test_remote_submit(&ring);
  349. if (ret) {
  350. fprintf(stderr, "test_remote_submit failed\n");
  351. return T_EXIT_FAIL;
  352. }
  353. io_uring_queue_exit(&ring);
  354. if (test_disabled_ring(&ring2, 0)) {
  355. fprintf(stderr, "test_disabled_ring failed\n");
  356. return T_EXIT_FAIL;
  357. }
  358. if (test_disabled_ring(&ring2, IORING_SETUP_SINGLE_ISSUER |
  359. IORING_SETUP_DEFER_TASKRUN)) {
  360. fprintf(stderr, "test_disabled_ring defer failed\n");
  361. return T_EXIT_FAIL;
  362. }
  363. }
  364. io_uring_queue_exit(&ring2);
  365. return T_EXIT_PASS;
  366. }