skip-cqe.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9. #include <assert.h>
  10. #include "liburing.h"
  11. #include "helpers.h"
  12. #define LINK_SIZE 6
  13. #define TIMEOUT_USER_DATA (-1)
  14. static int fds[2];
  15. /* should be successfully submitted but fails during execution */
  16. static void prep_exec_fail_req(struct io_uring_sqe *sqe)
  17. {
  18. io_uring_prep_write(sqe, fds[1], NULL, 100, 0);
  19. }
  20. static int test_link_success(struct io_uring *ring, int nr, bool skip_last)
  21. {
  22. struct io_uring_cqe *cqe;
  23. struct io_uring_sqe *sqe;
  24. int ret, i;
  25. for (i = 0; i < nr; ++i) {
  26. sqe = io_uring_get_sqe(ring);
  27. io_uring_prep_nop(sqe);
  28. if (i != nr - 1 || skip_last)
  29. sqe->flags |= IOSQE_IO_LINK | IOSQE_CQE_SKIP_SUCCESS;
  30. sqe->user_data = i;
  31. }
  32. ret = io_uring_submit(ring);
  33. if (ret != nr) {
  34. fprintf(stderr, "sqe submit failed: %d\n", ret);
  35. goto err;
  36. }
  37. if (!skip_last) {
  38. ret = io_uring_wait_cqe(ring, &cqe);
  39. if (ret != 0) {
  40. fprintf(stderr, "wait completion %d\n", ret);
  41. goto err;
  42. }
  43. if (cqe->res != 0) {
  44. fprintf(stderr, "nop failed: res %d\n", cqe->res);
  45. goto err;
  46. }
  47. if (cqe->user_data != nr - 1) {
  48. fprintf(stderr, "invalid user_data %i\n", (int)cqe->user_data);
  49. goto err;
  50. }
  51. io_uring_cqe_seen(ring, cqe);
  52. }
  53. if (io_uring_peek_cqe(ring, &cqe) >= 0) {
  54. fprintf(stderr, "single CQE expected %i\n", (int)cqe->user_data);
  55. goto err;
  56. }
  57. return 0;
  58. err:
  59. return 1;
  60. }
  61. static int test_link_fail(struct io_uring *ring, int nr, int fail_idx)
  62. {
  63. struct io_uring_cqe *cqe;
  64. struct io_uring_sqe *sqe;
  65. int ret, i;
  66. for (i = 0; i < nr; ++i) {
  67. sqe = io_uring_get_sqe(ring);
  68. if (i == fail_idx)
  69. prep_exec_fail_req(sqe);
  70. else
  71. io_uring_prep_nop(sqe);
  72. if (i != nr - 1)
  73. sqe->flags |= IOSQE_IO_LINK | IOSQE_CQE_SKIP_SUCCESS;
  74. sqe->user_data = i;
  75. }
  76. ret = io_uring_submit(ring);
  77. if (ret != nr) {
  78. fprintf(stderr, "sqe submit failed: %d\n", ret);
  79. goto err;
  80. }
  81. ret = io_uring_wait_cqe(ring, &cqe);
  82. if (ret != 0) {
  83. fprintf(stderr, "wait completion %d\n", ret);
  84. goto err;
  85. }
  86. if (!cqe->res || cqe->user_data != fail_idx) {
  87. fprintf(stderr, "got: user_data %d res %d, expected data: %d\n",
  88. (int)cqe->user_data, cqe->res, fail_idx);
  89. goto err;
  90. }
  91. io_uring_cqe_seen(ring, cqe);
  92. if (io_uring_peek_cqe(ring, &cqe) >= 0) {
  93. fprintf(stderr, "single CQE expected %i\n", (int)cqe->user_data);
  94. goto err;
  95. }
  96. return 0;
  97. err:
  98. return 1;
  99. }
  100. static int test_ltimeout_cancel(struct io_uring *ring, int nr, int tout_idx,
  101. bool async, int fail_idx)
  102. {
  103. struct __kernel_timespec ts = {.tv_sec = 1, .tv_nsec = 0};
  104. struct io_uring_cqe *cqe;
  105. struct io_uring_sqe *sqe;
  106. int ret, i;
  107. int e_res = 0, e_idx = nr - 1;
  108. if (fail_idx >= 0) {
  109. e_res = -EFAULT;
  110. e_idx = fail_idx;
  111. }
  112. for (i = 0; i < nr; ++i) {
  113. sqe = io_uring_get_sqe(ring);
  114. if (i == fail_idx)
  115. prep_exec_fail_req(sqe);
  116. else
  117. io_uring_prep_nop(sqe);
  118. sqe->user_data = i;
  119. sqe->flags |= IOSQE_IO_LINK;
  120. if (async)
  121. sqe->flags |= IOSQE_ASYNC;
  122. if (i != nr - 1)
  123. sqe->flags |= IOSQE_CQE_SKIP_SUCCESS;
  124. if (i == tout_idx) {
  125. sqe = io_uring_get_sqe(ring);
  126. io_uring_prep_link_timeout(sqe, &ts, 0);
  127. sqe->flags |= IOSQE_IO_LINK | IOSQE_CQE_SKIP_SUCCESS;
  128. sqe->user_data = TIMEOUT_USER_DATA;
  129. }
  130. }
  131. ret = io_uring_submit(ring);
  132. if (ret != nr + 1) {
  133. fprintf(stderr, "sqe submit failed: %d\n", ret);
  134. goto err;
  135. }
  136. ret = io_uring_wait_cqe(ring, &cqe);
  137. if (ret != 0) {
  138. fprintf(stderr, "wait completion %d\n", ret);
  139. goto err;
  140. }
  141. if (cqe->user_data != e_idx) {
  142. fprintf(stderr, "invalid user_data %i\n", (int)cqe->user_data);
  143. goto err;
  144. }
  145. if (cqe->res != e_res) {
  146. fprintf(stderr, "unexpected res: %d\n", cqe->res);
  147. goto err;
  148. }
  149. io_uring_cqe_seen(ring, cqe);
  150. if (io_uring_peek_cqe(ring, &cqe) >= 0) {
  151. fprintf(stderr, "single CQE expected %i\n", (int)cqe->user_data);
  152. goto err;
  153. }
  154. return 0;
  155. err:
  156. return 1;
  157. }
  158. static int test_ltimeout_fire(struct io_uring *ring, bool async,
  159. bool skip_main, bool skip_tout)
  160. {
  161. char buf[1];
  162. struct __kernel_timespec ts = {.tv_sec = 0, .tv_nsec = 1000000};
  163. struct io_uring_cqe *cqe;
  164. struct io_uring_sqe *sqe;
  165. int ret, i;
  166. int nr = 1 + !skip_tout;
  167. sqe = io_uring_get_sqe(ring);
  168. io_uring_prep_read(sqe, fds[0], buf, sizeof(buf), 0);
  169. sqe->flags |= IOSQE_IO_LINK;
  170. sqe->flags |= async ? IOSQE_ASYNC : 0;
  171. sqe->flags |= skip_main ? IOSQE_CQE_SKIP_SUCCESS : 0;
  172. sqe->user_data = 0;
  173. sqe = io_uring_get_sqe(ring);
  174. io_uring_prep_link_timeout(sqe, &ts, 0);
  175. sqe->flags |= skip_tout ? IOSQE_CQE_SKIP_SUCCESS : 0;
  176. sqe->user_data = 1;
  177. ret = io_uring_submit(ring);
  178. if (ret != 2) {
  179. fprintf(stderr, "sqe submit failed: %d\n", ret);
  180. return 1;
  181. }
  182. for (i = 0; i < nr; i++) {
  183. ret = io_uring_wait_cqe(ring, &cqe);
  184. if (ret != 0) {
  185. fprintf(stderr, "wait completion %d\n", ret);
  186. return 1;
  187. }
  188. switch (cqe->user_data) {
  189. case 0:
  190. if (cqe->res != -ECANCELED && cqe->res != -EINTR) {
  191. fprintf(stderr, "unexpected read return: %d\n", cqe->res);
  192. return 1;
  193. }
  194. break;
  195. case 1:
  196. if (skip_tout) {
  197. fprintf(stderr, "extra timeout cqe, %d\n", cqe->res);
  198. return 1;
  199. }
  200. break;
  201. }
  202. io_uring_cqe_seen(ring, cqe);
  203. }
  204. if (io_uring_peek_cqe(ring, &cqe) >= 0) {
  205. fprintf(stderr, "single CQE expected: got data: %i res: %i\n",
  206. (int)cqe->user_data, cqe->res);
  207. return 1;
  208. }
  209. return 0;
  210. }
  211. static int test_hardlink(struct io_uring *ring, int nr, int fail_idx,
  212. int skip_idx, bool hardlink_last)
  213. {
  214. struct io_uring_cqe *cqe;
  215. struct io_uring_sqe *sqe;
  216. int ret, i;
  217. assert(fail_idx < nr);
  218. assert(skip_idx < nr);
  219. for (i = 0; i < nr; i++) {
  220. sqe = io_uring_get_sqe(ring);
  221. if (i == fail_idx)
  222. prep_exec_fail_req(sqe);
  223. else
  224. io_uring_prep_nop(sqe);
  225. if (i != nr - 1 || hardlink_last)
  226. sqe->flags |= IOSQE_IO_HARDLINK;
  227. if (i == skip_idx)
  228. sqe->flags |= IOSQE_CQE_SKIP_SUCCESS;
  229. sqe->user_data = i;
  230. }
  231. ret = io_uring_submit(ring);
  232. if (ret != nr) {
  233. fprintf(stderr, "sqe submit failed: %d\n", ret);
  234. goto err;
  235. }
  236. for (i = 0; i < nr; i++) {
  237. if (i == skip_idx && fail_idx != skip_idx)
  238. continue;
  239. ret = io_uring_wait_cqe(ring, &cqe);
  240. if (ret != 0) {
  241. fprintf(stderr, "wait completion %d\n", ret);
  242. goto err;
  243. }
  244. if (cqe->user_data != i) {
  245. fprintf(stderr, "invalid user_data %d (%i)\n",
  246. (int)cqe->user_data, i);
  247. goto err;
  248. }
  249. if (i == fail_idx) {
  250. if (cqe->res >= 0) {
  251. fprintf(stderr, "req should've failed %d %d\n",
  252. (int)cqe->user_data, cqe->res);
  253. goto err;
  254. }
  255. } else {
  256. if (cqe->res) {
  257. fprintf(stderr, "req error %d %d\n",
  258. (int)cqe->user_data, cqe->res);
  259. goto err;
  260. }
  261. }
  262. io_uring_cqe_seen(ring, cqe);
  263. }
  264. if (io_uring_peek_cqe(ring, &cqe) >= 0) {
  265. fprintf(stderr, "single CQE expected %i\n", (int)cqe->user_data);
  266. goto err;
  267. }
  268. return 0;
  269. err:
  270. return 1;
  271. }
  272. int main(int argc, char *argv[])
  273. {
  274. struct io_uring ring;
  275. int ret, i, j, k;
  276. int mid_idx = LINK_SIZE / 2;
  277. int last_idx = LINK_SIZE - 1;
  278. if (argc > 1)
  279. return 0;
  280. if (pipe(fds)) {
  281. fprintf(stderr, "pipe() failed\n");
  282. return 1;
  283. }
  284. ret = io_uring_queue_init(16, &ring, 0);
  285. if (ret) {
  286. fprintf(stderr, "ring setup failed: %d\n", ret);
  287. return 1;
  288. }
  289. if (!(ring.features & IORING_FEAT_CQE_SKIP))
  290. return T_EXIT_SKIP;
  291. for (i = 0; i < 4; i++) {
  292. bool skip_last = i & 1;
  293. int sz = (i & 2) ? LINK_SIZE : 1;
  294. ret = test_link_success(&ring, sz, skip_last);
  295. if (ret) {
  296. fprintf(stderr, "test_link_success sz %d, %d last\n",
  297. skip_last, sz);
  298. return ret;
  299. }
  300. }
  301. ret = test_link_fail(&ring, LINK_SIZE, mid_idx);
  302. if (ret) {
  303. fprintf(stderr, "test_link_fail mid failed\n");
  304. return ret;
  305. }
  306. ret = test_link_fail(&ring, LINK_SIZE, last_idx);
  307. if (ret) {
  308. fprintf(stderr, "test_link_fail last failed\n");
  309. return ret;
  310. }
  311. for (i = 0; i < 2; i++) {
  312. bool async = i & 1;
  313. ret = test_ltimeout_cancel(&ring, 1, 0, async, -1);
  314. if (ret) {
  315. fprintf(stderr, "test_ltimeout_cancel 1 failed, %i\n",
  316. async);
  317. return ret;
  318. }
  319. ret = test_ltimeout_cancel(&ring, LINK_SIZE, mid_idx, async, -1);
  320. if (ret) {
  321. fprintf(stderr, "test_ltimeout_cancel mid failed, %i\n",
  322. async);
  323. return ret;
  324. }
  325. ret = test_ltimeout_cancel(&ring, LINK_SIZE, last_idx, async, -1);
  326. if (ret) {
  327. fprintf(stderr, "test_ltimeout_cancel last failed, %i\n",
  328. async);
  329. return ret;
  330. }
  331. ret = test_ltimeout_cancel(&ring, LINK_SIZE, mid_idx, async, mid_idx);
  332. if (ret) {
  333. fprintf(stderr, "test_ltimeout_cancel fail mid failed, %i\n",
  334. async);
  335. return ret;
  336. }
  337. ret = test_ltimeout_cancel(&ring, LINK_SIZE, mid_idx, async, mid_idx - 1);
  338. if (ret) {
  339. fprintf(stderr, "test_ltimeout_cancel fail2 mid failed, %i\n",
  340. async);
  341. return ret;
  342. }
  343. ret = test_ltimeout_cancel(&ring, LINK_SIZE, mid_idx, async, mid_idx + 1);
  344. if (ret) {
  345. fprintf(stderr, "test_ltimeout_cancel fail3 mid failed, %i\n",
  346. async);
  347. return ret;
  348. }
  349. }
  350. for (i = 0; i < 8; i++) {
  351. bool async = i & 1;
  352. bool skip1 = i & 2;
  353. bool skip2 = i & 4;
  354. ret = test_ltimeout_fire(&ring, async, skip1, skip2);
  355. if (ret) {
  356. fprintf(stderr, "test_ltimeout_fire failed\n");
  357. return ret;
  358. }
  359. }
  360. /* test 3 positions, start/middle/end of the link, i.e. indexes 0, 3, 6 */
  361. for (i = 0; i < 3; i++) {
  362. for (j = 0; j < 3; j++) {
  363. for (k = 0; k < 2; k++) {
  364. bool mark_last = k & 1;
  365. ret = test_hardlink(&ring, 7, i * 3, j * 3, mark_last);
  366. if (ret) {
  367. fprintf(stderr, "test_hardlink failed"
  368. "fail %i skip %i mark last %i\n",
  369. i * 3, j * 3, k);
  370. return 1;
  371. }
  372. }
  373. }
  374. }
  375. close(fds[0]);
  376. close(fds[1]);
  377. io_uring_queue_exit(&ring);
  378. return 0;
  379. }