• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <zeba.hrvoje@gmail.com>
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 
21 #include "liburing.h"
22 
main(int argc,char * argv[])23 int main(int argc, char *argv[])
24 {
25 	int p_fd[2], ret;
26 	int32_t recv_s0;
27 	int32_t val = 1;
28 	struct sockaddr_in addr;
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 = 0x0100007fU;
44 
45 	do {
46 		addr.sin_port = (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 	ret = listen(recv_s0, 128);
56 	assert(ret != -1);
57 
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 	flags = fcntl(p_fd[1], F_GETFL, 0);
76 	assert(flags != -1);
77 
78 	flags &= ~O_NONBLOCK;
79 	ret = fcntl(p_fd[1], F_SETFL, flags);
80 	assert(ret != -1);
81 
82 	p_fd[0] = accept(recv_s0, NULL, NULL);
83 	assert(p_fd[0] != -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 
98 	ret = io_uring_queue_init(32, &m_io_uring, 0);
99 	assert(ret >= 0);
100 
101 	char recv_buff[128];
102 	char send_buff[128];
103 
104 	{
105 		struct iovec iov[1];
106 
107 		iov[0].iov_base = recv_buff;
108 		iov[0].iov_len = sizeof(recv_buff);
109 
110 		struct io_uring_sqe* sqe = io_uring_get_sqe(&m_io_uring);
111 		assert(sqe != NULL);
112 
113 		io_uring_prep_readv(sqe, p_fd[0], iov, 1, 0);
114 	}
115 
116 	{
117 		struct iovec iov[1];
118 
119 		iov[0].iov_base = send_buff;
120 		iov[0].iov_len = sizeof(send_buff);
121 
122 		struct io_uring_sqe* sqe = io_uring_get_sqe(&m_io_uring);
123 		assert(sqe != NULL);
124 
125 		io_uring_prep_writev(sqe, p_fd[1], iov, 1, 0);
126 	}
127 
128 	ret = io_uring_submit_and_wait(&m_io_uring, 2);
129 	assert(ret != -1);
130 
131 	struct io_uring_cqe* cqe;
132 	uint32_t head;
133 	uint32_t count = 0;
134 
135 	while (count != 2) {
136 		io_uring_for_each_cqe(&m_io_uring, head, cqe) {
137 			assert(cqe->res == 128);
138 			count++;
139 		}
140 
141 		assert(count <= 2);
142 		io_uring_cq_advance(&m_io_uring, count);
143 	}
144 
145 	io_uring_queue_exit(&m_io_uring);
146 	return 0;
147 }
148