xref: /aosp_15_r20/external/liburing/test/socket-rw-eagain.c (revision 25da2bea747f3a93b4c30fd9708b0618ef55a0e6)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Check that a readv on a nonblocking socket queued before a writev doesn't
4  * wait for data to arrive.
5  */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <assert.h>
10 
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include <sys/socket.h>
15 #include <sys/un.h>
16 #include <netinet/tcp.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 
20 #include "liburing.h"
21 
main(int argc,char * argv[])22 int main(int argc, char *argv[])
23 {
24 	int p_fd[2], ret;
25 	int32_t recv_s0;
26 	int32_t val = 1;
27 	struct sockaddr_in addr;
28 	struct iovec iov_r[1], iov_w[1];
29 
30 	if (argc > 1)
31 		return 0;
32 
33 	srand(getpid());
34 
35 	recv_s0 = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
36 
37 	ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
38 	assert(ret != -1);
39 	ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
40 	assert(ret != -1);
41 
42 	addr.sin_family = AF_INET;
43 	addr.sin_addr.s_addr = inet_addr("127.0.0.1");
44 
45 	do {
46 		addr.sin_port = htons((rand() % 61440) + 4096);
47 		ret = bind(recv_s0, (struct sockaddr*)&addr, sizeof(addr));
48 		if (!ret)
49 			break;
50 		if (errno != EADDRINUSE) {
51 			perror("bind");
52 			exit(1);
53 		}
54 	} while (1);
55 
56 	ret = listen(recv_s0, 128);
57 	assert(ret != -1);
58 
59 	p_fd[1] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
60 
61 	val = 1;
62 	ret = setsockopt(p_fd[1], IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
63 	assert(ret != -1);
64 
65 	int32_t flags = fcntl(p_fd[1], F_GETFL, 0);
66 	assert(flags != -1);
67 
68 	flags |= O_NONBLOCK;
69 	ret = fcntl(p_fd[1], F_SETFL, flags);
70 	assert(ret != -1);
71 
72 	ret = connect(p_fd[1], (struct sockaddr*)&addr, sizeof(addr));
73 	assert(ret == -1);
74 
75 	p_fd[0] = accept(recv_s0, NULL, NULL);
76 	assert(p_fd[0] != -1);
77 
78 	flags = fcntl(p_fd[0], F_GETFL, 0);
79 	assert(flags != -1);
80 
81 	flags |= O_NONBLOCK;
82         ret = fcntl(p_fd[0], F_SETFL, flags);
83 	assert(ret != -1);
84 
85 	while (1) {
86 		int32_t code;
87 		socklen_t code_len = sizeof(code);
88 
89 		ret = getsockopt(p_fd[1], SOL_SOCKET, SO_ERROR, &code, &code_len);
90 		assert(ret != -1);
91 
92 		if (!code)
93 			break;
94 	}
95 
96 	struct io_uring m_io_uring;
97 	struct io_uring_params p = { };
98 
99 	ret = io_uring_queue_init_params(32, &m_io_uring, &p);
100 	assert(ret >= 0);
101 
102 	if (p.features & IORING_FEAT_FAST_POLL)
103 		return 0;
104 
105 	char recv_buff[128];
106 	char send_buff[128];
107 
108 	{
109 		iov_r[0].iov_base = recv_buff;
110 		iov_r[0].iov_len = sizeof(recv_buff);
111 
112 		struct io_uring_sqe* sqe = io_uring_get_sqe(&m_io_uring);
113 		assert(sqe != NULL);
114 
115 		io_uring_prep_readv(sqe, p_fd[0], iov_r, 1, 0);
116 		sqe->user_data = 1;
117 	}
118 
119 	{
120 		iov_w[0].iov_base = send_buff;
121 		iov_w[0].iov_len = sizeof(send_buff);
122 
123 		struct io_uring_sqe* sqe = io_uring_get_sqe(&m_io_uring);
124 		assert(sqe != NULL);
125 
126 		io_uring_prep_writev(sqe, p_fd[1], iov_w, 1, 0);
127 		sqe->user_data = 2;
128 	}
129 
130 	ret = io_uring_submit_and_wait(&m_io_uring, 2);
131 	assert(ret != -1);
132 
133 	struct io_uring_cqe* cqe;
134 	uint32_t head;
135 	uint32_t count = 0;
136 
137 	while (count != 2) {
138 		io_uring_for_each_cqe(&m_io_uring, head, cqe) {
139 			if (cqe->user_data == 2 && cqe->res != 128) {
140 				fprintf(stderr, "write=%d\n", cqe->res);
141 				goto err;
142 			} else if (cqe->user_data == 1 && cqe->res != -EAGAIN) {
143 				fprintf(stderr, "read=%d\n", cqe->res);
144 				goto err;
145 			}
146 			count++;
147 		}
148 
149 		assert(count <= 2);
150 		io_uring_cq_advance(&m_io_uring, count);
151 	}
152 
153 	io_uring_queue_exit(&m_io_uring);
154 	return 0;
155 err:
156 	io_uring_queue_exit(&m_io_uring);
157 	return 1;
158 }
159