connect.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Check that IORING_OP_CONNECT works, with and without other side
  5. * being open.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <fcntl.h>
  12. #include <unistd.h>
  13. #include <poll.h>
  14. #include <sys/socket.h>
  15. #include <netinet/in.h>
  16. #include <netinet/tcp.h>
  17. #include <arpa/inet.h>
  18. #include <sys/stat.h>
  19. #include "liburing.h"
  20. #include "helpers.h"
  21. static int no_connect;
  22. static unsigned short use_port;
  23. static unsigned int use_addr;
  24. static int create_socket(void)
  25. {
  26. int fd;
  27. fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  28. if (fd == -1) {
  29. perror("socket()");
  30. return -1;
  31. }
  32. return fd;
  33. }
  34. static int submit_and_wait(struct io_uring *ring, int *res)
  35. {
  36. struct io_uring_cqe *cqe;
  37. int ret;
  38. ret = io_uring_submit_and_wait(ring, 1);
  39. if (ret != 1) {
  40. fprintf(stderr, "io_using_submit: got %d\n", ret);
  41. return 1;
  42. }
  43. ret = io_uring_peek_cqe(ring, &cqe);
  44. if (ret) {
  45. fprintf(stderr, "io_uring_peek_cqe(): no cqe returned");
  46. return 1;
  47. }
  48. *res = cqe->res;
  49. io_uring_cqe_seen(ring, cqe);
  50. return 0;
  51. }
  52. static int wait_for(struct io_uring *ring, int fd, int mask)
  53. {
  54. struct io_uring_sqe *sqe;
  55. int ret, res;
  56. sqe = io_uring_get_sqe(ring);
  57. if (!sqe) {
  58. fprintf(stderr, "unable to get sqe\n");
  59. return -1;
  60. }
  61. io_uring_prep_poll_add(sqe, fd, mask);
  62. sqe->user_data = 2;
  63. ret = submit_and_wait(ring, &res);
  64. if (ret)
  65. return -1;
  66. if (res < 0) {
  67. fprintf(stderr, "poll(): failed with %d\n", res);
  68. return -1;
  69. }
  70. return res;
  71. }
  72. static int listen_on_socket(int fd)
  73. {
  74. struct sockaddr_in addr;
  75. int ret;
  76. memset(&addr, 0, sizeof(addr));
  77. addr.sin_family = AF_INET;
  78. addr.sin_port = use_port;
  79. addr.sin_addr.s_addr = use_addr;
  80. ret = bind(fd, (struct sockaddr*)&addr, sizeof(addr));
  81. if (ret == -1) {
  82. perror("bind()");
  83. return -1;
  84. }
  85. ret = listen(fd, 128);
  86. if (ret == -1) {
  87. perror("listen()");
  88. return -1;
  89. }
  90. return 0;
  91. }
  92. static int configure_connect(int fd, struct sockaddr_in* addr)
  93. {
  94. int ret, val = 1;
  95. ret = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
  96. if (ret == -1) {
  97. perror("setsockopt()");
  98. return -1;
  99. }
  100. ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
  101. if (ret == -1) {
  102. perror("setsockopt()");
  103. return -1;
  104. }
  105. memset(addr, 0, sizeof(*addr));
  106. addr->sin_family = AF_INET;
  107. addr->sin_port = use_port;
  108. ret = inet_aton("127.0.0.1", &addr->sin_addr);
  109. return ret;
  110. }
  111. static int connect_socket(struct io_uring *ring, int fd, int *code, int async)
  112. {
  113. struct sockaddr_in addr;
  114. int ret, res;
  115. socklen_t code_len = sizeof(*code);
  116. struct io_uring_sqe *sqe;
  117. if (configure_connect(fd, &addr) == -1)
  118. return -1;
  119. sqe = io_uring_get_sqe(ring);
  120. if (!sqe) {
  121. fprintf(stderr, "unable to get sqe\n");
  122. return -1;
  123. }
  124. io_uring_prep_connect(sqe, fd, (struct sockaddr*)&addr, sizeof(addr));
  125. if (async)
  126. sqe->flags |= IOSQE_ASYNC;
  127. sqe->user_data = 1;
  128. ret = submit_and_wait(ring, &res);
  129. if (ret)
  130. return -1;
  131. if (res == -EINPROGRESS) {
  132. ret = wait_for(ring, fd, POLLOUT | POLLHUP | POLLERR);
  133. if (ret == -1)
  134. return -1;
  135. int ev = (ret & POLLOUT) || (ret & POLLHUP) || (ret & POLLERR);
  136. if (!ev) {
  137. fprintf(stderr, "poll(): returned invalid value %#x\n", ret);
  138. return -1;
  139. }
  140. ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, code, &code_len);
  141. if (ret == -1) {
  142. perror("getsockopt()");
  143. return -1;
  144. }
  145. } else
  146. *code = res;
  147. return 0;
  148. }
  149. static int test_connect_with_no_peer(struct io_uring *ring)
  150. {
  151. int connect_fd;
  152. int ret, code;
  153. connect_fd = create_socket();
  154. if (connect_fd == -1)
  155. return -1;
  156. ret = connect_socket(ring, connect_fd, &code, 0);
  157. if (ret == -1)
  158. goto err;
  159. if (code != -ECONNREFUSED) {
  160. if (code == -EINVAL || code == -EBADF || code == -EOPNOTSUPP) {
  161. fprintf(stdout, "No connect support, skipping\n");
  162. no_connect = 1;
  163. goto out;
  164. }
  165. fprintf(stderr, "connect failed with %d\n", code);
  166. goto err;
  167. }
  168. out:
  169. close(connect_fd);
  170. return 0;
  171. err:
  172. close(connect_fd);
  173. return -1;
  174. }
  175. static int test_connect(struct io_uring *ring, int async)
  176. {
  177. int accept_fd;
  178. int connect_fd;
  179. int ret, code;
  180. accept_fd = create_socket();
  181. if (accept_fd == -1)
  182. return -1;
  183. ret = listen_on_socket(accept_fd);
  184. if (ret == -1)
  185. goto err1;
  186. connect_fd = create_socket();
  187. if (connect_fd == -1)
  188. goto err1;
  189. ret = connect_socket(ring, connect_fd, &code, async);
  190. if (ret == -1)
  191. goto err2;
  192. if (code != 0) {
  193. fprintf(stderr, "connect failed with %d\n", code);
  194. goto err2;
  195. }
  196. close(connect_fd);
  197. close(accept_fd);
  198. return 0;
  199. err2:
  200. close(connect_fd);
  201. err1:
  202. close(accept_fd);
  203. return -1;
  204. }
  205. static int test_connect_timeout(struct io_uring *ring)
  206. {
  207. int connect_fd[2] = {-1, -1};
  208. int accept_fd = -1;
  209. int ret, code;
  210. struct sockaddr_in addr;
  211. struct io_uring_sqe *sqe;
  212. struct __kernel_timespec ts = {.tv_sec = 0, .tv_nsec = 100000};
  213. struct stat sb;
  214. /*
  215. * Test reliably fails if syncookies isn't enabled
  216. */
  217. if (stat("/proc/sys/net/ipv4/tcp_syncookies", &sb) < 0)
  218. return T_EXIT_SKIP;
  219. connect_fd[0] = create_socket();
  220. if (connect_fd[0] == -1)
  221. return -1;
  222. connect_fd[1] = create_socket();
  223. if (connect_fd[1] == -1)
  224. goto err;
  225. accept_fd = create_socket();
  226. if (accept_fd == -1)
  227. goto err;
  228. if (configure_connect(connect_fd[0], &addr) == -1)
  229. goto err;
  230. if (configure_connect(connect_fd[1], &addr) == -1)
  231. goto err;
  232. ret = bind(accept_fd, (struct sockaddr*)&addr, sizeof(addr));
  233. if (ret == -1) {
  234. perror("bind()");
  235. goto err;
  236. }
  237. ret = listen(accept_fd, 0); // no backlog in order to block connect_fd[1]
  238. if (ret == -1) {
  239. perror("listen()");
  240. goto err;
  241. }
  242. // We first connect with one client socket in order to fill the accept queue.
  243. ret = connect_socket(ring, connect_fd[0], &code, 0);
  244. if (ret == -1 || code != 0) {
  245. fprintf(stderr, "unable to connect\n");
  246. goto err;
  247. }
  248. // We do not offload completion events from listening socket on purpose.
  249. // This way we create a state where the second connect request being stalled by OS.
  250. sqe = io_uring_get_sqe(ring);
  251. if (!sqe) {
  252. fprintf(stderr, "unable to get sqe\n");
  253. goto err;
  254. }
  255. io_uring_prep_connect(sqe, connect_fd[1], (struct sockaddr*)&addr, sizeof(addr));
  256. sqe->user_data = 1;
  257. sqe->flags |= IOSQE_IO_LINK;
  258. sqe = io_uring_get_sqe(ring);
  259. if (!sqe) {
  260. fprintf(stderr, "unable to get sqe\n");
  261. goto err;
  262. }
  263. io_uring_prep_link_timeout(sqe, &ts, 0);
  264. sqe->user_data = 2;
  265. ret = io_uring_submit(ring);
  266. if (ret != 2) {
  267. fprintf(stderr, "submitted %d\n", ret);
  268. return -1;
  269. }
  270. for (int i = 0; i < 2; i++) {
  271. int expected;
  272. struct io_uring_cqe *cqe;
  273. ret = io_uring_wait_cqe(ring, &cqe);
  274. if (ret) {
  275. fprintf(stderr, "wait_cqe=%d\n", ret);
  276. return -1;
  277. }
  278. expected = (cqe->user_data == 1) ? -ECANCELED : -ETIME;
  279. if (expected != cqe->res) {
  280. fprintf(stderr, "cqe %d, res %d, wanted %d\n",
  281. (int)cqe->user_data, cqe->res, expected);
  282. goto err;
  283. }
  284. io_uring_cqe_seen(ring, cqe);
  285. }
  286. close(connect_fd[0]);
  287. close(connect_fd[1]);
  288. close(accept_fd);
  289. return 0;
  290. err:
  291. if (connect_fd[0] != -1)
  292. close(connect_fd[0]);
  293. if (connect_fd[1] != -1)
  294. close(connect_fd[1]);
  295. if (accept_fd != -1)
  296. close(accept_fd);
  297. return -1;
  298. }
  299. static int test(int flags)
  300. {
  301. struct io_uring ring;
  302. int ret;
  303. ret = io_uring_queue_init(8, &ring, flags);
  304. if (ret) {
  305. fprintf(stderr, "io_uring_queue_setup() = %d\n", ret);
  306. return T_EXIT_FAIL;
  307. }
  308. srand(getpid());
  309. use_port = (rand() % 61440) + 4096;
  310. use_port = htons(use_port);
  311. use_addr = inet_addr("127.0.0.1");
  312. ret = test_connect_with_no_peer(&ring);
  313. if (ret == -1) {
  314. fprintf(stderr, "test_connect_with_no_peer(): failed\n");
  315. return T_EXIT_FAIL;
  316. }
  317. if (no_connect)
  318. return T_EXIT_SKIP;
  319. ret = test_connect(&ring, 0);
  320. if (ret == -1) {
  321. fprintf(stderr, "test_connect(): failed\n");
  322. return T_EXIT_FAIL;
  323. }
  324. ret = test_connect(&ring, 1);
  325. if (ret == -1) {
  326. fprintf(stderr, "test_connect(): failed\n");
  327. return T_EXIT_FAIL;
  328. }
  329. ret = test_connect_timeout(&ring);
  330. if (ret == -1) {
  331. fprintf(stderr, "test_connect_timeout(): failed\n");
  332. return T_EXIT_FAIL;
  333. }
  334. io_uring_queue_exit(&ring);
  335. return T_EXIT_PASS;
  336. }
  337. int main(int argc, char *argv[])
  338. {
  339. int ret;
  340. if (argc > 1)
  341. return T_EXIT_SKIP;
  342. ret = test(0);
  343. if (ret == -1) {
  344. fprintf(stderr, "test 0 failed\n");
  345. return T_EXIT_FAIL;
  346. }
  347. if (no_connect)
  348. return T_EXIT_SKIP;
  349. ret = test(IORING_SETUP_SQPOLL);
  350. if (ret == -1) {
  351. fprintf(stderr, "test SQPOLL failed\n");
  352. return T_EXIT_FAIL;
  353. }
  354. ret = test(IORING_SETUP_SINGLE_ISSUER|IORING_SETUP_DEFER_TASKRUN);
  355. if (ret == -1) {
  356. fprintf(stderr, "test DEFER failed\n");
  357. return T_EXIT_FAIL;
  358. }
  359. return T_EXIT_PASS;
  360. }