1 /* SPDX-License-Identifier: MIT */
2 #include <errno.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <assert.h>
9 #include <pthread.h>
10 #include <sys/socket.h>
11 #include <netinet/tcp.h>
12 #include <netinet/in.h>
13 #include <poll.h>
14 #include <arpa/inet.h>
15
16 #include "liburing.h"
17
18 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
19 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
20
21 static int recv_thread_ready = 0;
22 static int recv_thread_done = 0;
23
signal_var(int * var)24 static void signal_var(int *var)
25 {
26 pthread_mutex_lock(&mutex);
27 *var = 1;
28 pthread_cond_signal(&cond);
29 pthread_mutex_unlock(&mutex);
30 }
31
wait_for_var(int * var)32 static void wait_for_var(int *var)
33 {
34 pthread_mutex_lock(&mutex);
35
36 while (!*var)
37 pthread_cond_wait(&cond, &mutex);
38
39 pthread_mutex_unlock(&mutex);
40 }
41
42 struct data {
43 unsigned expected[2];
44 unsigned is_mask[2];
45 unsigned long timeout;
46 unsigned short port;
47 unsigned int addr;
48 int stop;
49 };
50
send_thread(void * arg)51 static void *send_thread(void *arg)
52 {
53 struct data *data = arg;
54
55 wait_for_var(&recv_thread_ready);
56
57 int s0 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
58 assert(s0 != -1);
59
60 struct sockaddr_in addr;
61
62 addr.sin_family = AF_INET;
63 addr.sin_port = data->port;
64 addr.sin_addr.s_addr = data->addr;
65
66 if (connect(s0, (struct sockaddr*)&addr, sizeof(addr)) != -1)
67 wait_for_var(&recv_thread_done);
68
69 close(s0);
70 return 0;
71 }
72
recv_thread(void * arg)73 void *recv_thread(void *arg)
74 {
75 struct data *data = arg;
76 struct io_uring_sqe *sqe;
77 struct io_uring ring;
78 int i, ret;
79
80 ret = io_uring_queue_init(8, &ring, 0);
81 assert(ret == 0);
82
83 int s0 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
84 assert(s0 != -1);
85
86 int32_t val = 1;
87 ret = setsockopt(s0, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
88 assert(ret != -1);
89 ret = setsockopt(s0, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
90 assert(ret != -1);
91
92 struct sockaddr_in addr;
93
94 addr.sin_family = AF_INET;
95 data->addr = inet_addr("127.0.0.1");
96 addr.sin_addr.s_addr = data->addr;
97
98 i = 0;
99 do {
100 data->port = htons(1025 + (rand() % 64510));
101 addr.sin_port = data->port;
102
103 if (bind(s0, (struct sockaddr*)&addr, sizeof(addr)) != -1)
104 break;
105 } while (++i < 100);
106
107 if (i >= 100) {
108 fprintf(stderr, "Can't find good port, skipped\n");
109 data->stop = 1;
110 signal_var(&recv_thread_ready);
111 goto out;
112 }
113
114 ret = listen(s0, 128);
115 assert(ret != -1);
116
117 signal_var(&recv_thread_ready);
118
119 sqe = io_uring_get_sqe(&ring);
120 assert(sqe != NULL);
121
122 io_uring_prep_poll_add(sqe, s0, POLLIN | POLLHUP | POLLERR);
123 sqe->flags |= IOSQE_IO_LINK;
124 sqe->user_data = 1;
125
126 sqe = io_uring_get_sqe(&ring);
127 assert(sqe != NULL);
128
129 struct __kernel_timespec ts;
130 ts.tv_sec = data->timeout / 1000000000;
131 ts.tv_nsec = data->timeout % 1000000000;
132 io_uring_prep_link_timeout(sqe, &ts, 0);
133 sqe->user_data = 2;
134
135 ret = io_uring_submit(&ring);
136 assert(ret == 2);
137
138 for (i = 0; i < 2; i++) {
139 struct io_uring_cqe *cqe;
140 int idx;
141
142 if (io_uring_wait_cqe(&ring, &cqe)) {
143 fprintf(stderr, "wait cqe failed\n");
144 goto err;
145 }
146 idx = cqe->user_data - 1;
147 if (data->is_mask[idx] && !(data->expected[idx] & cqe->res)) {
148 fprintf(stderr, "cqe %" PRIu64 " got %x, wanted mask %x\n",
149 (uint64_t) cqe->user_data, cqe->res,
150 data->expected[idx]);
151 goto err;
152 } else if (!data->is_mask[idx] && cqe->res != data->expected[idx]) {
153 fprintf(stderr, "cqe %" PRIu64 " got %d, wanted %d\n",
154 (uint64_t) cqe->user_data, cqe->res,
155 data->expected[idx]);
156 goto err;
157 }
158 io_uring_cqe_seen(&ring, cqe);
159 }
160
161 out:
162 signal_var(&recv_thread_done);
163 close(s0);
164 io_uring_queue_exit(&ring);
165 return NULL;
166 err:
167 signal_var(&recv_thread_done);
168 close(s0);
169 io_uring_queue_exit(&ring);
170 return (void *) 1;
171 }
172
test_poll_timeout(int do_connect,unsigned long timeout)173 static int test_poll_timeout(int do_connect, unsigned long timeout)
174 {
175 pthread_t t1, t2;
176 struct data d;
177 void *tret;
178 int ret = 0;
179
180 recv_thread_ready = 0;
181 recv_thread_done = 0;
182
183 memset(&d, 0, sizeof(d));
184 d.timeout = timeout;
185 if (!do_connect) {
186 d.expected[0] = -ECANCELED;
187 d.expected[1] = -ETIME;
188 } else {
189 d.expected[0] = POLLIN;
190 d.is_mask[0] = 1;
191 d.expected[1] = -ECANCELED;
192 }
193
194 pthread_create(&t1, NULL, recv_thread, &d);
195
196 if (do_connect)
197 pthread_create(&t2, NULL, send_thread, &d);
198
199 pthread_join(t1, &tret);
200 if (tret)
201 ret++;
202
203 if (do_connect) {
204 pthread_join(t2, &tret);
205 if (tret)
206 ret++;
207 }
208
209 return ret;
210 }
211
main(int argc,char * argv[])212 int main(int argc, char *argv[])
213 {
214 if (argc > 1)
215 return 0;
216
217 srand(getpid());
218
219 if (test_poll_timeout(0, 200000000)) {
220 fprintf(stderr, "poll timeout 0 failed\n");
221 return 1;
222 }
223
224 if (test_poll_timeout(1, 1000000000)) {
225 fprintf(stderr, "poll timeout 1 failed\n");
226 return 1;
227 }
228
229 return 0;
230 }
231