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