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 #include <arpa/inet.h>
21
22 #include "liburing.h"
23 #include "helpers.h"
24
main(int argc,char * argv[])25 int main(int argc, char *argv[])
26 {
27 int p_fd[2], ret;
28 int32_t recv_s0;
29 int32_t val = 1;
30 struct sockaddr_in addr;
31 struct iovec iov_r[1], iov_w[1];
32
33 if (argc > 1)
34 return 0;
35
36 srand(getpid());
37
38 recv_s0 = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
39
40 ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
41 assert(ret != -1);
42 ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
43 assert(ret != -1);
44
45 addr.sin_family = AF_INET;
46 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
47 ret = t_bind_ephemeral_port(recv_s0, &addr);
48 assert(!ret);
49 ret = listen(recv_s0, 128);
50 assert(ret != -1);
51
52
53 p_fd[1] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
54
55 val = 1;
56 ret = setsockopt(p_fd[1], IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
57 assert(ret != -1);
58
59 int32_t flags = fcntl(p_fd[1], F_GETFL, 0);
60 assert(flags != -1);
61
62 flags |= O_NONBLOCK;
63 ret = fcntl(p_fd[1], F_SETFL, flags);
64 assert(ret != -1);
65
66 ret = connect(p_fd[1], (struct sockaddr*)&addr, sizeof(addr));
67 assert(ret == -1);
68
69 flags = fcntl(p_fd[1], F_GETFL, 0);
70 assert(flags != -1);
71
72 flags &= ~O_NONBLOCK;
73 ret = fcntl(p_fd[1], F_SETFL, flags);
74 assert(ret != -1);
75
76 p_fd[0] = accept(recv_s0, NULL, NULL);
77 assert(p_fd[0] != -1);
78
79 while (1) {
80 int32_t code;
81 socklen_t code_len = sizeof(code);
82
83 ret = getsockopt(p_fd[1], SOL_SOCKET, SO_ERROR, &code, &code_len);
84 assert(ret != -1);
85
86 if (!code)
87 break;
88 }
89
90 struct io_uring m_io_uring;
91 struct io_uring_params p = { };
92
93 ret = io_uring_queue_init_params(32, &m_io_uring, &p);
94 assert(ret >= 0);
95
96 /* skip for kernels without cur position read/write */
97 if (!(p.features & IORING_FEAT_RW_CUR_POS))
98 return 0;
99
100 char recv_buff[128];
101 char send_buff[128];
102
103 {
104 iov_r[0].iov_base = recv_buff;
105 iov_r[0].iov_len = sizeof(recv_buff);
106
107 struct io_uring_sqe* sqe = io_uring_get_sqe(&m_io_uring);
108 assert(sqe != NULL);
109
110 io_uring_prep_readv(sqe, p_fd[0], iov_r, 1, -1);
111 }
112
113 {
114 iov_w[0].iov_base = send_buff;
115 iov_w[0].iov_len = sizeof(send_buff);
116
117 struct io_uring_sqe* sqe = io_uring_get_sqe(&m_io_uring);
118 assert(sqe != NULL);
119
120 io_uring_prep_writev(sqe, p_fd[1], iov_w, 1, 0);
121 }
122
123 ret = io_uring_submit_and_wait(&m_io_uring, 2);
124 assert(ret != -1);
125
126 struct io_uring_cqe* cqe;
127 uint32_t head;
128 uint32_t count = 0;
129
130 ret = 0;
131 while (count != 2) {
132 io_uring_for_each_cqe(&m_io_uring, head, cqe) {
133 if (cqe->res != 128) {
134 fprintf(stderr, "Got %d, expected 128\n", cqe->res);
135 ret = 1;
136 goto err;
137 }
138 assert(cqe->res == 128);
139 count++;
140 }
141
142 assert(count <= 2);
143 io_uring_cq_advance(&m_io_uring, count);
144 }
145
146 err:
147 io_uring_queue_exit(&m_io_uring);
148 return ret;
149 }
150