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