• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Check that repeated IORING_OP_CONNECT to a socket without a listener keeps
4  * yielding -ECONNREFUSED rather than -ECONNABORTED. Based on a reproducer
5  * from:
6  *
7  * https://github.com/axboe/liburing/issues/828
8  *
9  * and adopted to our usual test cases. Other changes made like looping,
10  * using different ring types, adding a memset() for reuse, etc.
11  *
12  */
13 #include <stdio.h>
14 #include <netinet/in.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <stdlib.h>
18 #include <arpa/inet.h>
19 
20 #include "liburing.h"
21 #include "helpers.h"
22 
23 static unsigned long ud;
24 
init_test_server(struct sockaddr_in * serv_addr)25 static int init_test_server(struct sockaddr_in *serv_addr)
26 {
27 	socklen_t servaddr_len = sizeof(struct sockaddr_in);
28 	int fd;
29 
30 	/* Init server socket. Bind but don't listen */
31 	fd = socket(AF_INET, SOCK_STREAM, 0);
32 	if (fd < 0) {
33 		perror("socket");
34 		return -1;
35 	}
36 
37 	serv_addr->sin_family = AF_INET;
38 	serv_addr->sin_addr.s_addr = inet_addr("127.0.0.1");
39 
40 	if (bind(fd, (struct sockaddr *) serv_addr, servaddr_len) < 0) {
41 		perror("bind");
42 		return -1;
43 	}
44 
45 	/*
46 	 * Get the addresses the socket is bound to because the port is chosen
47 	 * by the network stack.
48 	 */
49 	if (getsockname(fd, (struct sockaddr *)serv_addr, &servaddr_len) < 0) {
50 		perror("getsockname");
51 		return -1;
52 	}
53 
54 	return fd;
55 }
56 
init_test_client(void)57 static int init_test_client(void)
58 {
59 	socklen_t addr_len = sizeof(struct sockaddr_in);
60 	struct sockaddr_in client_addr = {};
61 	int clientfd;
62 
63 	clientfd = socket(AF_INET, SOCK_STREAM, 0);
64 	if (clientfd < 0) {
65 		perror("socket");
66 		return -1;
67 	}
68 
69 	client_addr.sin_family = AF_INET;
70 	client_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
71 
72 	if (bind(clientfd, (struct sockaddr *)&client_addr, addr_len) < 0) {
73 		perror("bind");
74 		close(clientfd);
75 		return -1;
76 	}
77 
78 	/*
79 	 * Get the addresses the socket is bound to because the port is chosen
80 	 * by the network stack.
81 	 */
82 	 if (getsockname(clientfd, (struct sockaddr *)&client_addr, &addr_len) < 0) {
83 		 perror("getsockname");
84 		 close(clientfd);
85 		 return -1;
86 	 }
87 
88 	 return clientfd;
89 }
90 
get_completion_and_print(struct io_uring * ring)91 static int get_completion_and_print(struct io_uring *ring)
92 {
93 	struct io_uring_cqe *cqe;
94 	int ret, res;
95 
96 	ret = io_uring_wait_cqe(ring, &cqe);
97 	if (ret < 0) {
98 		fprintf(stderr, "wait_cqe=%d\n", ret);
99 		return -1;
100 	}
101 
102 	/* Mark this completion as seen */
103 	res = cqe->res;
104 	io_uring_cqe_seen(ring, cqe);
105 	return res;
106 }
107 
test_connect(struct io_uring * ring,int clientfd,struct sockaddr_in * serv_addr)108 static int test_connect(struct io_uring *ring,
109 			int clientfd, struct sockaddr_in *serv_addr)
110 {
111 	struct sockaddr_in local_sa;
112 	struct io_uring_sqe *sqe;
113 	int ret;
114 
115 	sqe = io_uring_get_sqe(ring);
116 	io_uring_prep_connect(sqe, clientfd, (const struct sockaddr *)serv_addr,
117 				sizeof(struct sockaddr_in));
118 	sqe->user_data = ++ud;
119 
120 	memcpy(&local_sa, serv_addr, sizeof(local_sa));
121 
122 	ret = io_uring_submit_and_wait(ring, 1);
123 	if (ret != 1) {
124 		fprintf(stderr, "submit=%d\n", ret);
125 		return T_EXIT_FAIL;
126 	}
127 
128 	/* check for reuse at the same time */
129 	memset(&local_sa, 0xff, sizeof(local_sa));
130 
131 	ret = get_completion_and_print(ring);
132 	if (ret != -ECONNREFUSED) {
133 		fprintf(stderr, "Connect got %d\n", ret);
134 		return T_EXIT_FAIL;
135 	}
136 	return T_EXIT_PASS;
137 }
138 
test(int flags)139 static int test(int flags)
140 {
141 	struct io_uring_params params = { .flags = flags, };
142 	struct sockaddr_in serv_addr = {};
143 	struct io_uring ring;
144 	int ret, clientfd, s_fd, i;
145 
146 	if (flags & IORING_SETUP_SQPOLL)
147 		params.sq_thread_idle = 50;
148 
149 	ret = io_uring_queue_init_params(8, &ring, &params);
150 	if (ret < 0) {
151 		fprintf(stderr, "Queue init: %d\n", ret);
152 		return T_EXIT_FAIL;
153 	}
154 
155 	s_fd = init_test_server(&serv_addr);
156 	if (s_fd < 0)
157 		return T_EXIT_FAIL;
158 
159 	clientfd = init_test_client();
160 	if (clientfd < 0) {
161 		close(s_fd);
162 		return T_EXIT_FAIL;
163 	}
164 
165 	/* make sure SQPOLL thread is sleeping */
166 	if (flags & IORING_SETUP_SQPOLL)
167 		usleep(100000);
168 
169 	for (i = 0; i < 32; i++) {
170 		ret = test_connect(&ring, clientfd, &serv_addr);
171 		if (ret == T_EXIT_SKIP)
172 			return T_EXIT_SKIP;
173 		else if (ret == T_EXIT_PASS)
174 			continue;
175 
176 		return T_EXIT_FAIL;
177 	}
178 
179 	close(s_fd);
180 	close(clientfd);
181 	return T_EXIT_PASS;
182 }
183 
main(int argc,char * argv[])184 int main(int argc, char *argv[])
185 {
186 	int ret;
187 
188 	if (argc > 1)
189 		return T_EXIT_SKIP;
190 
191 	ret = test(0);
192 	if (ret == T_EXIT_FAIL) {
193 		fprintf(stderr, "test(0) failed\n");
194 		return T_EXIT_FAIL;
195 	}
196 
197 	ret = test(IORING_SETUP_SQPOLL);
198 	if (ret == T_EXIT_FAIL) {
199 		fprintf(stderr, "test(SQPOLL) failed\n");
200 		return T_EXIT_FAIL;
201 	}
202 
203 	return 0;
204 }
205