accept.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Check that IORING_OP_ACCEPT works, and send some data across to verify we
  5. * didn't get a junk fd.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #include <assert.h>
  11. #include <limits.h>
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15. #include <sys/socket.h>
  16. #include <sys/time.h>
  17. #include <sys/resource.h>
  18. #include <sys/un.h>
  19. #include <netinet/tcp.h>
  20. #include <netinet/in.h>
  21. #include <arpa/inet.h>
  22. #include "helpers.h"
  23. #include "liburing.h"
  24. #define MAX_FDS 32
  25. #define NOP_USER_DATA (1LLU << 50)
  26. #define INITIAL_USER_DATA 1000
  27. static int no_accept;
  28. static int no_accept_multi;
  29. struct data {
  30. char buf[128];
  31. struct iovec iov;
  32. };
  33. struct accept_test_args {
  34. int accept_should_error;
  35. bool fixed;
  36. bool nonblock;
  37. bool queue_accept_before_connect;
  38. bool multishot;
  39. int extra_loops;
  40. bool overflow;
  41. };
  42. static void close_fds(int fds[], int nr)
  43. {
  44. int i;
  45. for (i = 0; i < nr; i++)
  46. close(fds[i]);
  47. }
  48. static void close_sock_fds(int s_fd[], int c_fd[], int nr, bool fixed)
  49. {
  50. if (!fixed)
  51. close_fds(s_fd, nr);
  52. close_fds(c_fd, nr);
  53. }
  54. static void *queue_send(struct io_uring *ring, int fd)
  55. {
  56. struct io_uring_sqe *sqe;
  57. struct data *d;
  58. d = t_malloc(sizeof(*d));
  59. d->iov.iov_base = d->buf;
  60. d->iov.iov_len = sizeof(d->buf);
  61. sqe = io_uring_get_sqe(ring);
  62. io_uring_prep_writev(sqe, fd, &d->iov, 1, 0);
  63. sqe->user_data = 1;
  64. return d;
  65. }
  66. static void *queue_recv(struct io_uring *ring, int fd, bool fixed)
  67. {
  68. struct io_uring_sqe *sqe;
  69. struct data *d;
  70. d = t_malloc(sizeof(*d));
  71. d->iov.iov_base = d->buf;
  72. d->iov.iov_len = sizeof(d->buf);
  73. sqe = io_uring_get_sqe(ring);
  74. io_uring_prep_readv(sqe, fd, &d->iov, 1, 0);
  75. sqe->user_data = 2;
  76. if (fixed)
  77. sqe->flags |= IOSQE_FIXED_FILE;
  78. return d;
  79. }
  80. static void queue_accept_multishot(struct io_uring *ring, int fd,
  81. int idx, bool fixed)
  82. {
  83. struct io_uring_sqe *sqe = io_uring_get_sqe(ring);
  84. int ret;
  85. if (fixed)
  86. io_uring_prep_multishot_accept_direct(sqe, fd,
  87. NULL, NULL,
  88. 0);
  89. else
  90. io_uring_prep_multishot_accept(sqe, fd, NULL, NULL, 0);
  91. io_uring_sqe_set_data64(sqe, idx);
  92. ret = io_uring_submit(ring);
  93. assert(ret != -1);
  94. }
  95. static void queue_accept_conn(struct io_uring *ring, int fd,
  96. struct accept_test_args args)
  97. {
  98. struct io_uring_sqe *sqe;
  99. int ret;
  100. int fixed_idx = args.fixed ? 0 : -1;
  101. int count = 1 + args.extra_loops;
  102. if (args.multishot) {
  103. queue_accept_multishot(ring, fd, INITIAL_USER_DATA, args.fixed);
  104. return;
  105. }
  106. while (count--) {
  107. sqe = io_uring_get_sqe(ring);
  108. if (fixed_idx < 0) {
  109. io_uring_prep_accept(sqe, fd, NULL, NULL, 0);
  110. } else {
  111. io_uring_prep_accept_direct(sqe, fd, NULL, NULL,
  112. 0, fixed_idx);
  113. }
  114. ret = io_uring_submit(ring);
  115. assert(ret != -1);
  116. }
  117. }
  118. static int accept_conn(struct io_uring *ring, int fixed_idx, int *multishot, int fd)
  119. {
  120. struct io_uring_cqe *pcqe;
  121. struct io_uring_cqe cqe;
  122. int ret;
  123. do {
  124. ret = io_uring_wait_cqe(ring, &pcqe);
  125. assert(!ret);
  126. cqe = *pcqe;
  127. io_uring_cqe_seen(ring, pcqe);
  128. } while (cqe.user_data == NOP_USER_DATA);
  129. if (*multishot) {
  130. if (!(cqe.flags & IORING_CQE_F_MORE)) {
  131. (*multishot)++;
  132. queue_accept_multishot(ring, fd, *multishot, fixed_idx == 0);
  133. } else {
  134. if (cqe.user_data != *multishot) {
  135. fprintf(stderr, "received multishot after told done!\n");
  136. return -ECANCELED;
  137. }
  138. }
  139. }
  140. ret = cqe.res;
  141. if (fixed_idx >= 0) {
  142. if (ret > 0) {
  143. if (!multishot) {
  144. close(ret);
  145. return -EINVAL;
  146. }
  147. } else if (!ret) {
  148. ret = fixed_idx;
  149. }
  150. }
  151. return ret;
  152. }
  153. static int start_accept_listen(struct sockaddr_in *addr, int port_off,
  154. int extra_flags)
  155. {
  156. int fd, ret;
  157. fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC | extra_flags,
  158. IPPROTO_TCP);
  159. int32_t val = 1;
  160. ret = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
  161. assert(ret != -1);
  162. ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
  163. assert(ret != -1);
  164. struct sockaddr_in laddr;
  165. if (!addr)
  166. addr = &laddr;
  167. addr->sin_family = AF_INET;
  168. addr->sin_addr.s_addr = inet_addr("127.0.0.1");
  169. ret = t_bind_ephemeral_port(fd, addr);
  170. assert(!ret);
  171. ret = listen(fd, 128);
  172. assert(ret != -1);
  173. return fd;
  174. }
  175. static int set_client_fd(struct sockaddr_in *addr)
  176. {
  177. int32_t val;
  178. int fd, ret;
  179. fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
  180. val = 1;
  181. ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
  182. assert(ret != -1);
  183. int32_t flags = fcntl(fd, F_GETFL, 0);
  184. assert(flags != -1);
  185. flags |= O_NONBLOCK;
  186. ret = fcntl(fd, F_SETFL, flags);
  187. assert(ret != -1);
  188. ret = connect(fd, (struct sockaddr *)addr, sizeof(*addr));
  189. assert(ret == -1);
  190. flags = fcntl(fd, F_GETFL, 0);
  191. assert(flags != -1);
  192. flags &= ~O_NONBLOCK;
  193. ret = fcntl(fd, F_SETFL, flags);
  194. assert(ret != -1);
  195. return fd;
  196. }
  197. static void cause_overflow(struct io_uring *ring)
  198. {
  199. int i, ret;
  200. for (i = 0; i < ring->cq.ring_entries; i++) {
  201. struct io_uring_sqe *sqe = io_uring_get_sqe(ring);
  202. io_uring_prep_nop(sqe);
  203. io_uring_sqe_set_data64(sqe, NOP_USER_DATA);
  204. ret = io_uring_submit(ring);
  205. assert(ret != -1);
  206. }
  207. }
  208. static void clear_overflow(struct io_uring *ring)
  209. {
  210. struct io_uring_cqe *cqe;
  211. while (!io_uring_peek_cqe(ring, &cqe)) {
  212. if (cqe->user_data != NOP_USER_DATA)
  213. break;
  214. io_uring_cqe_seen(ring, cqe);
  215. }
  216. }
  217. static int test_loop(struct io_uring *ring,
  218. struct accept_test_args args,
  219. int recv_s0,
  220. struct sockaddr_in *addr)
  221. {
  222. struct io_uring_cqe *cqe;
  223. uint32_t head, count = 0;
  224. int i, ret, s_fd[MAX_FDS], c_fd[MAX_FDS], done = 0;
  225. bool fixed = args.fixed;
  226. bool multishot = args.multishot;
  227. uint32_t multishot_mask = 0;
  228. int nr_fds = multishot ? MAX_FDS : 1;
  229. int multishot_idx = multishot ? INITIAL_USER_DATA : 0;
  230. int err_ret = T_EXIT_FAIL;
  231. void* send_d = 0;
  232. void* recv_d = 0;
  233. if (args.overflow)
  234. cause_overflow(ring);
  235. for (i = 0; i < nr_fds; i++) {
  236. c_fd[i] = set_client_fd(addr);
  237. if (args.overflow && i == nr_fds / 2)
  238. clear_overflow(ring);
  239. }
  240. if (!args.queue_accept_before_connect)
  241. queue_accept_conn(ring, recv_s0, args);
  242. for (i = 0; i < nr_fds; i++) {
  243. s_fd[i] = accept_conn(ring, fixed ? 0 : -1, &multishot_idx, recv_s0);
  244. if (s_fd[i] == -EINVAL) {
  245. if (args.accept_should_error)
  246. goto out;
  247. fprintf(stdout,
  248. "%s %s Accept not supported, skipping\n",
  249. fixed ? "Fixed" : "",
  250. multishot ? "Multishot" : "");
  251. if (multishot)
  252. no_accept_multi = 1;
  253. else
  254. no_accept = 1;
  255. ret = T_EXIT_SKIP;
  256. goto out;
  257. } else if (s_fd[i] < 0) {
  258. if (args.accept_should_error &&
  259. (s_fd[i] == -EBADF || s_fd[i] == -EINVAL))
  260. goto out;
  261. fprintf(stderr, "%s %s Accept[%d] got %d\n",
  262. fixed ? "Fixed" : "",
  263. multishot ? "Multishot" : "",
  264. i, s_fd[i]);
  265. goto err;
  266. } else if (s_fd[i] == 195 && args.overflow) {
  267. fprintf(stderr, "Broken overflow handling\n");
  268. goto err;
  269. }
  270. if (multishot && fixed) {
  271. if (s_fd[i] >= MAX_FDS) {
  272. fprintf(stderr,
  273. "Fixed Multishot Accept[%d] got outbound index: %d\n",
  274. i, s_fd[i]);
  275. goto err;
  276. }
  277. /*
  278. * for fixed multishot accept test, the file slots
  279. * allocated are [0, 32), this means we finally end up
  280. * with each bit of a u32 being 1.
  281. */
  282. multishot_mask |= (1U << s_fd[i]);
  283. }
  284. }
  285. if (multishot) {
  286. if (fixed && (~multishot_mask != 0U)) {
  287. fprintf(stderr, "Fixed Multishot Accept misses events\n");
  288. goto err;
  289. }
  290. goto out;
  291. }
  292. send_d = queue_send(ring, c_fd[0]);
  293. recv_d = queue_recv(ring, s_fd[0], fixed);
  294. ret = io_uring_submit_and_wait(ring, 2);
  295. assert(ret != -1);
  296. while (count < 2) {
  297. io_uring_for_each_cqe(ring, head, cqe) {
  298. if (cqe->res < 0) {
  299. fprintf(stderr, "Got cqe res %d, user_data %i\n",
  300. cqe->res, (int)cqe->user_data);
  301. done = 1;
  302. break;
  303. }
  304. assert(cqe->res == 128);
  305. count++;
  306. }
  307. assert(count <= 2);
  308. io_uring_cq_advance(ring, count);
  309. if (done)
  310. goto err;
  311. }
  312. out:
  313. free(send_d);
  314. free(recv_d);
  315. close_sock_fds(s_fd, c_fd, nr_fds, fixed);
  316. return T_EXIT_PASS;
  317. err:
  318. free(send_d);
  319. free(recv_d);
  320. close_sock_fds(s_fd, c_fd, nr_fds, fixed);
  321. return err_ret;
  322. }
  323. static int test(struct io_uring *ring, struct accept_test_args args)
  324. {
  325. struct sockaddr_in addr;
  326. int ret = 0;
  327. int loop;
  328. int32_t recv_s0 = start_accept_listen(&addr, 0,
  329. args.nonblock ? SOCK_NONBLOCK : 0);
  330. if (args.queue_accept_before_connect)
  331. queue_accept_conn(ring, recv_s0, args);
  332. for (loop = 0; loop < 1 + args.extra_loops; loop++) {
  333. ret = test_loop(ring, args, recv_s0, &addr);
  334. if (ret)
  335. break;
  336. }
  337. close(recv_s0);
  338. return ret;
  339. }
  340. static void sig_alrm(int sig)
  341. {
  342. exit(0);
  343. }
  344. static int test_accept_pending_on_exit(void)
  345. {
  346. struct io_uring m_io_uring;
  347. struct io_uring_cqe *cqe;
  348. struct io_uring_sqe *sqe;
  349. int fd, ret;
  350. ret = io_uring_queue_init(32, &m_io_uring, 0);
  351. assert(ret >= 0);
  352. fd = start_accept_listen(NULL, 0, 0);
  353. sqe = io_uring_get_sqe(&m_io_uring);
  354. io_uring_prep_accept(sqe, fd, NULL, NULL, 0);
  355. ret = io_uring_submit(&m_io_uring);
  356. assert(ret != -1);
  357. signal(SIGALRM, sig_alrm);
  358. alarm(1);
  359. ret = io_uring_wait_cqe(&m_io_uring, &cqe);
  360. assert(!ret);
  361. io_uring_cqe_seen(&m_io_uring, cqe);
  362. io_uring_queue_exit(&m_io_uring);
  363. return 0;
  364. }
  365. struct test_accept_many_args {
  366. unsigned int usecs;
  367. bool nonblock;
  368. bool single_sock;
  369. bool close_fds;
  370. };
  371. /*
  372. * Test issue many accepts and see if we handle cancelation on exit
  373. */
  374. static int test_accept_many(struct test_accept_many_args args)
  375. {
  376. struct io_uring m_io_uring;
  377. struct io_uring_cqe *cqe;
  378. struct io_uring_sqe *sqe;
  379. unsigned long cur_lim;
  380. struct rlimit rlim;
  381. int *fds, i, ret;
  382. unsigned int nr = 128;
  383. int nr_socks = args.single_sock ? 1 : nr;
  384. if (getrlimit(RLIMIT_NPROC, &rlim) < 0) {
  385. perror("getrlimit");
  386. return 1;
  387. }
  388. cur_lim = rlim.rlim_cur;
  389. rlim.rlim_cur = nr / 4;
  390. if (setrlimit(RLIMIT_NPROC, &rlim) < 0) {
  391. perror("setrlimit");
  392. return 1;
  393. }
  394. ret = io_uring_queue_init(2 * nr, &m_io_uring, 0);
  395. assert(ret >= 0);
  396. fds = t_calloc(nr_socks, sizeof(int));
  397. for (i = 0; i < nr_socks; i++)
  398. fds[i] = start_accept_listen(NULL, i,
  399. args.nonblock ? SOCK_NONBLOCK : 0);
  400. for (i = 0; i < nr; i++) {
  401. int sock_idx = args.single_sock ? 0 : i;
  402. sqe = io_uring_get_sqe(&m_io_uring);
  403. io_uring_prep_accept(sqe, fds[sock_idx], NULL, NULL, 0);
  404. sqe->user_data = 1 + i;
  405. ret = io_uring_submit(&m_io_uring);
  406. assert(ret == 1);
  407. }
  408. if (args.usecs)
  409. usleep(args.usecs);
  410. if (args.close_fds)
  411. for (i = 0; i < nr_socks; i++)
  412. close(fds[i]);
  413. for (i = 0; i < nr; i++) {
  414. if (io_uring_peek_cqe(&m_io_uring, &cqe))
  415. break;
  416. if (cqe->res != -ECANCELED) {
  417. fprintf(stderr, "Expected cqe to be canceled %d\n", cqe->res);
  418. ret = 1;
  419. goto out;
  420. }
  421. io_uring_cqe_seen(&m_io_uring, cqe);
  422. }
  423. ret = 0;
  424. out:
  425. rlim.rlim_cur = cur_lim;
  426. if (setrlimit(RLIMIT_NPROC, &rlim) < 0) {
  427. perror("setrlimit");
  428. return 1;
  429. }
  430. free(fds);
  431. io_uring_queue_exit(&m_io_uring);
  432. return ret;
  433. }
  434. static int test_accept_cancel(unsigned usecs, unsigned int nr, bool multishot)
  435. {
  436. struct io_uring m_io_uring;
  437. struct io_uring_cqe *cqe;
  438. struct io_uring_sqe *sqe;
  439. int fd, i, ret;
  440. if (multishot && no_accept_multi)
  441. return T_EXIT_SKIP;
  442. ret = io_uring_queue_init(32, &m_io_uring, 0);
  443. assert(ret >= 0);
  444. fd = start_accept_listen(NULL, 0, 0);
  445. for (i = 1; i <= nr; i++) {
  446. sqe = io_uring_get_sqe(&m_io_uring);
  447. if (!multishot)
  448. io_uring_prep_accept(sqe, fd, NULL, NULL, 0);
  449. else
  450. io_uring_prep_multishot_accept(sqe, fd, NULL, NULL, 0);
  451. sqe->user_data = i;
  452. ret = io_uring_submit(&m_io_uring);
  453. assert(ret == 1);
  454. }
  455. if (usecs)
  456. usleep(usecs);
  457. for (i = 1; i <= nr; i++) {
  458. sqe = io_uring_get_sqe(&m_io_uring);
  459. io_uring_prep_cancel64(sqe, i, 0);
  460. sqe->user_data = nr + i;
  461. ret = io_uring_submit(&m_io_uring);
  462. assert(ret == 1);
  463. }
  464. for (i = 0; i < nr * 2; i++) {
  465. ret = io_uring_wait_cqe(&m_io_uring, &cqe);
  466. assert(!ret);
  467. /*
  468. * Two cases here:
  469. *
  470. * 1) We cancel the accept4() before it got started, we should
  471. * get '0' for the cancel request and '-ECANCELED' for the
  472. * accept request.
  473. * 2) We cancel the accept4() after it's already running, we
  474. * should get '-EALREADY' for the cancel request and
  475. * '-EINTR' for the accept request.
  476. */
  477. if (cqe->user_data == 0) {
  478. fprintf(stderr, "unexpected 0 user data\n");
  479. goto err;
  480. } else if (cqe->user_data <= nr) {
  481. /* no multishot */
  482. if (cqe->res == -EINVAL)
  483. return T_EXIT_SKIP;
  484. if (cqe->res != -EINTR && cqe->res != -ECANCELED) {
  485. fprintf(stderr, "Cancelled accept got %d\n", cqe->res);
  486. goto err;
  487. }
  488. } else if (cqe->user_data <= nr * 2) {
  489. if (cqe->res != -EALREADY && cqe->res != 0) {
  490. fprintf(stderr, "Cancel got %d\n", cqe->res);
  491. goto err;
  492. }
  493. }
  494. io_uring_cqe_seen(&m_io_uring, cqe);
  495. }
  496. io_uring_queue_exit(&m_io_uring);
  497. close(fd);
  498. return 0;
  499. err:
  500. io_uring_queue_exit(&m_io_uring);
  501. close(fd);
  502. return 1;
  503. }
  504. static int test_accept(int count, bool before)
  505. {
  506. struct io_uring m_io_uring;
  507. int ret;
  508. struct accept_test_args args = {
  509. .queue_accept_before_connect = before,
  510. .extra_loops = count - 1
  511. };
  512. ret = io_uring_queue_init(32, &m_io_uring, 0);
  513. assert(ret >= 0);
  514. ret = test(&m_io_uring, args);
  515. io_uring_queue_exit(&m_io_uring);
  516. return ret;
  517. }
  518. static int test_multishot_accept(int count, bool before, bool overflow)
  519. {
  520. struct io_uring m_io_uring;
  521. int ret;
  522. struct accept_test_args args = {
  523. .queue_accept_before_connect = before,
  524. .multishot = true,
  525. .extra_loops = count - 1,
  526. .overflow = overflow
  527. };
  528. if (no_accept_multi)
  529. return T_EXIT_SKIP;
  530. ret = io_uring_queue_init(MAX_FDS + 10, &m_io_uring, 0);
  531. assert(ret >= 0);
  532. ret = test(&m_io_uring, args);
  533. io_uring_queue_exit(&m_io_uring);
  534. return ret;
  535. }
  536. static int test_accept_multishot_wrong_arg(void)
  537. {
  538. struct io_uring m_io_uring;
  539. struct io_uring_cqe *cqe;
  540. struct io_uring_sqe *sqe;
  541. int fd, ret;
  542. ret = io_uring_queue_init(4, &m_io_uring, 0);
  543. assert(ret >= 0);
  544. fd = start_accept_listen(NULL, 0, 0);
  545. sqe = io_uring_get_sqe(&m_io_uring);
  546. io_uring_prep_multishot_accept_direct(sqe, fd, NULL, NULL, 0);
  547. sqe->file_index = 1;
  548. ret = io_uring_submit(&m_io_uring);
  549. assert(ret == 1);
  550. ret = io_uring_wait_cqe(&m_io_uring, &cqe);
  551. assert(!ret);
  552. if (cqe->res != -EINVAL) {
  553. fprintf(stderr, "file index should be IORING_FILE_INDEX_ALLOC \
  554. if its accept in multishot direct mode\n");
  555. goto err;
  556. }
  557. io_uring_cqe_seen(&m_io_uring, cqe);
  558. io_uring_queue_exit(&m_io_uring);
  559. close(fd);
  560. return 0;
  561. err:
  562. io_uring_queue_exit(&m_io_uring);
  563. close(fd);
  564. return 1;
  565. }
  566. static int test_accept_nonblock(bool queue_before_connect, int count)
  567. {
  568. struct io_uring m_io_uring;
  569. int ret;
  570. struct accept_test_args args = {
  571. .nonblock = true,
  572. .queue_accept_before_connect = queue_before_connect,
  573. .extra_loops = count - 1
  574. };
  575. ret = io_uring_queue_init(32, &m_io_uring, 0);
  576. assert(ret >= 0);
  577. ret = test(&m_io_uring, args);
  578. io_uring_queue_exit(&m_io_uring);
  579. return ret;
  580. }
  581. static int test_accept_fixed(void)
  582. {
  583. struct io_uring m_io_uring;
  584. int ret, fd = -1;
  585. struct accept_test_args args = {
  586. .fixed = true
  587. };
  588. ret = io_uring_queue_init(32, &m_io_uring, 0);
  589. assert(ret >= 0);
  590. ret = io_uring_register_files(&m_io_uring, &fd, 1);
  591. if (ret) {
  592. /* kernel doesn't support sparse registered files, skip */
  593. if (ret == -EBADF || ret == -EINVAL)
  594. return T_EXIT_SKIP;
  595. return T_EXIT_FAIL;
  596. }
  597. ret = test(&m_io_uring, args);
  598. io_uring_queue_exit(&m_io_uring);
  599. return ret;
  600. }
  601. static int test_multishot_fixed_accept(void)
  602. {
  603. struct io_uring m_io_uring;
  604. int ret, fd[MAX_FDS];
  605. struct accept_test_args args = {
  606. .fixed = true,
  607. .multishot = true
  608. };
  609. if (no_accept_multi)
  610. return T_EXIT_SKIP;
  611. memset(fd, -1, sizeof(fd));
  612. ret = io_uring_queue_init(MAX_FDS + 10, &m_io_uring, 0);
  613. assert(ret >= 0);
  614. ret = io_uring_register_files(&m_io_uring, fd, MAX_FDS);
  615. if (ret) {
  616. /* kernel doesn't support sparse registered files, skip */
  617. if (ret == -EBADF || ret == -EINVAL)
  618. return T_EXIT_SKIP;
  619. return T_EXIT_FAIL;
  620. }
  621. ret = test(&m_io_uring, args);
  622. io_uring_queue_exit(&m_io_uring);
  623. return ret;
  624. }
  625. static int test_accept_sqpoll(void)
  626. {
  627. struct io_uring m_io_uring;
  628. struct io_uring_params p = { };
  629. int ret;
  630. struct accept_test_args args = { };
  631. p.flags = IORING_SETUP_SQPOLL;
  632. ret = t_create_ring_params(32, &m_io_uring, &p);
  633. if (ret == T_SETUP_SKIP)
  634. return 0;
  635. else if (ret < 0)
  636. return ret;
  637. args.accept_should_error = 1;
  638. if (p.features & IORING_FEAT_SQPOLL_NONFIXED)
  639. args.accept_should_error = 0;
  640. ret = test(&m_io_uring, args);
  641. io_uring_queue_exit(&m_io_uring);
  642. return ret;
  643. }
  644. int main(int argc, char *argv[])
  645. {
  646. int ret;
  647. if (argc > 1)
  648. return T_EXIT_SKIP;
  649. ret = test_accept(1, false);
  650. if (ret == T_EXIT_FAIL) {
  651. fprintf(stderr, "test_accept failed\n");
  652. return ret;
  653. }
  654. if (no_accept)
  655. return T_EXIT_SKIP;
  656. ret = test_accept(2, false);
  657. if (ret == T_EXIT_FAIL) {
  658. fprintf(stderr, "test_accept(2) failed\n");
  659. return ret;
  660. }
  661. ret = test_accept(2, true);
  662. if (ret == T_EXIT_FAIL) {
  663. fprintf(stderr, "test_accept(2, true) failed\n");
  664. return ret;
  665. }
  666. ret = test_accept_nonblock(false, 1);
  667. if (ret == T_EXIT_FAIL) {
  668. fprintf(stderr, "test_accept_nonblock failed\n");
  669. return ret;
  670. }
  671. ret = test_accept_nonblock(true, 1);
  672. if (ret == T_EXIT_FAIL) {
  673. fprintf(stderr, "test_accept_nonblock(before, 1) failed\n");
  674. return ret;
  675. }
  676. ret = test_accept_nonblock(true, 3);
  677. if (ret == T_EXIT_FAIL) {
  678. fprintf(stderr, "test_accept_nonblock(before,3) failed\n");
  679. return ret;
  680. }
  681. ret = test_accept_fixed();
  682. if (ret == T_EXIT_FAIL) {
  683. fprintf(stderr, "test_accept_fixed failed\n");
  684. return ret;
  685. }
  686. ret = test_multishot_fixed_accept();
  687. if (ret == T_EXIT_FAIL) {
  688. fprintf(stderr, "test_multishot_fixed_accept failed\n");
  689. return ret;
  690. }
  691. ret = test_accept_multishot_wrong_arg();
  692. if (ret == T_EXIT_FAIL) {
  693. fprintf(stderr, "test_accept_multishot_wrong_arg failed\n");
  694. return ret;
  695. }
  696. ret = test_accept_sqpoll();
  697. if (ret == T_EXIT_FAIL) {
  698. fprintf(stderr, "test_accept_sqpoll failed\n");
  699. return ret;
  700. }
  701. ret = test_accept_cancel(0, 1, false);
  702. if (ret == T_EXIT_FAIL) {
  703. fprintf(stderr, "test_accept_cancel nodelay failed\n");
  704. return ret;
  705. }
  706. ret = test_accept_cancel(10000, 1, false);
  707. if (ret == T_EXIT_FAIL) {
  708. fprintf(stderr, "test_accept_cancel delay failed\n");
  709. return ret;
  710. }
  711. ret = test_accept_cancel(0, 4, false);
  712. if (ret == T_EXIT_FAIL) {
  713. fprintf(stderr, "test_accept_cancel nodelay failed\n");
  714. return ret;
  715. }
  716. ret = test_accept_cancel(10000, 4, false);
  717. if (ret == T_EXIT_FAIL) {
  718. fprintf(stderr, "test_accept_cancel delay failed\n");
  719. return ret;
  720. }
  721. ret = test_accept_cancel(0, 1, true);
  722. if (ret == T_EXIT_FAIL) {
  723. fprintf(stderr, "test_accept_cancel multishot nodelay failed\n");
  724. return ret;
  725. }
  726. ret = test_accept_cancel(10000, 1, true);
  727. if (ret == T_EXIT_FAIL) {
  728. fprintf(stderr, "test_accept_cancel multishot delay failed\n");
  729. return ret;
  730. }
  731. ret = test_accept_cancel(0, 4, true);
  732. if (ret == T_EXIT_FAIL) {
  733. fprintf(stderr, "test_accept_cancel multishot nodelay failed\n");
  734. return ret;
  735. }
  736. ret = test_accept_cancel(10000, 4, true);
  737. if (ret == T_EXIT_FAIL) {
  738. fprintf(stderr, "test_accept_cancel multishot delay failed\n");
  739. return ret;
  740. }
  741. ret = test_multishot_accept(1, true, true);
  742. if (ret == T_EXIT_FAIL) {
  743. fprintf(stderr, "test_multishot_accept(1, false, true) failed\n");
  744. return ret;
  745. }
  746. ret = test_multishot_accept(1, false, false);
  747. if (ret == T_EXIT_FAIL) {
  748. fprintf(stderr, "test_multishot_accept(1, false, false) failed\n");
  749. return ret;
  750. }
  751. ret = test_multishot_accept(1, true, false);
  752. if (ret == T_EXIT_FAIL) {
  753. fprintf(stderr, "test_multishot_accept(1, true, false) failed\n");
  754. return ret;
  755. }
  756. ret = test_accept_many((struct test_accept_many_args) {});
  757. if (ret == T_EXIT_FAIL) {
  758. fprintf(stderr, "test_accept_many failed\n");
  759. return ret;
  760. }
  761. ret = test_accept_many((struct test_accept_many_args) {
  762. .usecs = 100000 });
  763. if (ret == T_EXIT_FAIL) {
  764. fprintf(stderr, "test_accept_many(sleep) failed\n");
  765. return ret;
  766. }
  767. ret = test_accept_many((struct test_accept_many_args) {
  768. .nonblock = true });
  769. if (ret == T_EXIT_FAIL) {
  770. fprintf(stderr, "test_accept_many(nonblock) failed\n");
  771. return ret;
  772. }
  773. ret = test_accept_many((struct test_accept_many_args) {
  774. .nonblock = true,
  775. .single_sock = true,
  776. .close_fds = true });
  777. if (ret == T_EXIT_FAIL) {
  778. fprintf(stderr, "test_accept_many(nonblock,close) failed\n");
  779. return ret;
  780. }
  781. ret = test_accept_pending_on_exit();
  782. if (ret == T_EXIT_FAIL) {
  783. fprintf(stderr, "test_accept_pending_on_exit failed\n");
  784. return ret;
  785. }
  786. return T_EXIT_PASS;
  787. }