connect.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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)
  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. sqe->user_data = 1;
  126. ret = submit_and_wait(ring, &res);
  127. if (ret)
  128. return -1;
  129. if (res == -EINPROGRESS) {
  130. ret = wait_for(ring, fd, POLLOUT | POLLHUP | POLLERR);
  131. if (ret == -1)
  132. return -1;
  133. int ev = (ret & POLLOUT) || (ret & POLLHUP) || (ret & POLLERR);
  134. if (!ev) {
  135. fprintf(stderr, "poll(): returned invalid value %#x\n", ret);
  136. return -1;
  137. }
  138. ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, code, &code_len);
  139. if (ret == -1) {
  140. perror("getsockopt()");
  141. return -1;
  142. }
  143. } else
  144. *code = res;
  145. return 0;
  146. }
  147. static int test_connect_with_no_peer(struct io_uring *ring)
  148. {
  149. int connect_fd;
  150. int ret, code;
  151. connect_fd = create_socket();
  152. if (connect_fd == -1)
  153. return -1;
  154. ret = connect_socket(ring, connect_fd, &code);
  155. if (ret == -1)
  156. goto err;
  157. if (code != -ECONNREFUSED) {
  158. if (code == -EINVAL || code == -EBADF || code == -EOPNOTSUPP) {
  159. fprintf(stdout, "No connect support, skipping\n");
  160. no_connect = 1;
  161. goto out;
  162. }
  163. fprintf(stderr, "connect failed with %d\n", code);
  164. goto err;
  165. }
  166. out:
  167. close(connect_fd);
  168. return 0;
  169. err:
  170. close(connect_fd);
  171. return -1;
  172. }
  173. static int test_connect(struct io_uring *ring)
  174. {
  175. int accept_fd;
  176. int connect_fd;
  177. int ret, code;
  178. accept_fd = create_socket();
  179. if (accept_fd == -1)
  180. return -1;
  181. ret = listen_on_socket(accept_fd);
  182. if (ret == -1)
  183. goto err1;
  184. connect_fd = create_socket();
  185. if (connect_fd == -1)
  186. goto err1;
  187. ret = connect_socket(ring, connect_fd, &code);
  188. if (ret == -1)
  189. goto err2;
  190. if (code != 0) {
  191. fprintf(stderr, "connect failed with %d\n", code);
  192. goto err2;
  193. }
  194. close(connect_fd);
  195. close(accept_fd);
  196. return 0;
  197. err2:
  198. close(connect_fd);
  199. err1:
  200. close(accept_fd);
  201. return -1;
  202. }
  203. static int test_connect_timeout(struct io_uring *ring)
  204. {
  205. int connect_fd[2] = {-1, -1};
  206. int accept_fd = -1;
  207. int ret, code;
  208. struct sockaddr_in addr;
  209. struct io_uring_sqe *sqe;
  210. struct __kernel_timespec ts = {.tv_sec = 0, .tv_nsec = 100000};
  211. struct stat sb;
  212. /*
  213. * Test reliably fails if syncookies isn't enabled
  214. */
  215. if (stat("/proc/sys/net/ipv4/tcp_syncookies", &sb) < 0)
  216. return T_EXIT_SKIP;
  217. connect_fd[0] = create_socket();
  218. if (connect_fd[0] == -1)
  219. return -1;
  220. connect_fd[1] = create_socket();
  221. if (connect_fd[1] == -1)
  222. goto err;
  223. accept_fd = create_socket();
  224. if (accept_fd == -1)
  225. goto err;
  226. if (configure_connect(connect_fd[0], &addr) == -1)
  227. goto err;
  228. if (configure_connect(connect_fd[1], &addr) == -1)
  229. goto err;
  230. ret = bind(accept_fd, (struct sockaddr*)&addr, sizeof(addr));
  231. if (ret == -1) {
  232. perror("bind()");
  233. goto err;
  234. }
  235. ret = listen(accept_fd, 0); // no backlog in order to block connect_fd[1]
  236. if (ret == -1) {
  237. perror("listen()");
  238. goto err;
  239. }
  240. // We first connect with one client socket in order to fill the accept queue.
  241. ret = connect_socket(ring, connect_fd[0], &code);
  242. if (ret == -1 || code != 0) {
  243. fprintf(stderr, "unable to connect\n");
  244. goto err;
  245. }
  246. // We do not offload completion events from listening socket on purpose.
  247. // This way we create a state where the second connect request being stalled by OS.
  248. sqe = io_uring_get_sqe(ring);
  249. if (!sqe) {
  250. fprintf(stderr, "unable to get sqe\n");
  251. goto err;
  252. }
  253. io_uring_prep_connect(sqe, connect_fd[1], (struct sockaddr*)&addr, sizeof(addr));
  254. sqe->user_data = 1;
  255. sqe->flags |= IOSQE_IO_LINK;
  256. sqe = io_uring_get_sqe(ring);
  257. if (!sqe) {
  258. fprintf(stderr, "unable to get sqe\n");
  259. goto err;
  260. }
  261. io_uring_prep_link_timeout(sqe, &ts, 0);
  262. sqe->user_data = 2;
  263. ret = io_uring_submit(ring);
  264. if (ret != 2) {
  265. fprintf(stderr, "submitted %d\n", ret);
  266. return -1;
  267. }
  268. for (int i = 0; i < 2; i++) {
  269. int expected;
  270. struct io_uring_cqe *cqe;
  271. ret = io_uring_wait_cqe(ring, &cqe);
  272. if (ret) {
  273. fprintf(stderr, "wait_cqe=%d\n", ret);
  274. return -1;
  275. }
  276. expected = (cqe->user_data == 1) ? -ECANCELED : -ETIME;
  277. if (expected != cqe->res) {
  278. fprintf(stderr, "cqe %d, res %d, wanted %d\n",
  279. (int)cqe->user_data, cqe->res, expected);
  280. goto err;
  281. }
  282. io_uring_cqe_seen(ring, cqe);
  283. }
  284. close(connect_fd[0]);
  285. close(connect_fd[1]);
  286. close(accept_fd);
  287. return 0;
  288. err:
  289. if (connect_fd[0] != -1)
  290. close(connect_fd[0]);
  291. if (connect_fd[1] != -1)
  292. close(connect_fd[1]);
  293. if (accept_fd != -1)
  294. close(accept_fd);
  295. return -1;
  296. }
  297. int main(int argc, char *argv[])
  298. {
  299. struct io_uring ring;
  300. int ret;
  301. if (argc > 1)
  302. return T_EXIT_SKIP;
  303. ret = io_uring_queue_init(8, &ring, 0);
  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);
  320. if (ret == -1) {
  321. fprintf(stderr, "test_connect(): failed\n");
  322. return T_EXIT_FAIL;
  323. }
  324. ret = test_connect_timeout(&ring);
  325. if (ret == -1) {
  326. fprintf(stderr, "test_connect_timeout(): failed\n");
  327. return T_EXIT_FAIL;
  328. }
  329. io_uring_queue_exit(&ring);
  330. return T_EXIT_PASS;
  331. }