blob: 6935b994230d99afb221b9c94c4b9cf13308fc64 [file] [log] [blame]
Jens Axboef2ee0432019-10-17 17:21:25 -06001/*
2 * Check that IORING_OP_ACCEPT works, and send some data across to verify we
3 * didn't get a junk fd.
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <stdint.h>
8#include <assert.h>
9
10#include <errno.h>
11#include <fcntl.h>
12#include <unistd.h>
13#include <sys/socket.h>
14#include <sys/un.h>
15#include <netinet/tcp.h>
16#include <netinet/in.h>
17
18#include <liburing.h>
19
Jens Axboea9bb08d2019-10-18 08:20:31 -060020static int no_accept;
21
Jens Axboef2ee0432019-10-17 17:21:25 -060022static void queue_send(struct io_uring *ring, int fd)
23{
24 struct io_uring_sqe *sqe;
25 char send_buff[128];
26 struct iovec iov;
27
28 iov.iov_base = send_buff;
29 iov.iov_len = sizeof(send_buff);
30
31 sqe = io_uring_get_sqe(ring);
32 io_uring_prep_writev(sqe, fd, &iov, 1, 0);
33}
34
35static void queue_recv(struct io_uring *ring, int fd)
36{
37 struct io_uring_sqe *sqe;
38 char recv_buff[128];
39 struct iovec iov;
40
41 iov.iov_base = recv_buff;
42 iov.iov_len = sizeof(recv_buff);
43
44 sqe = io_uring_get_sqe(ring);
45 io_uring_prep_readv(sqe, fd, &iov, 1, 0);
46}
47
48static int accept_conn(struct io_uring *ring, int fd)
49{
50 struct io_uring_sqe *sqe;
51 struct io_uring_cqe *cqe;
52 int ret;
53
54 sqe = io_uring_get_sqe(ring);
55 io_uring_prep_accept(sqe, fd, NULL, NULL, 0);
56
57 assert(io_uring_submit(ring) != -1);
58
59 assert(!io_uring_wait_cqe(ring, &cqe));
60 ret = cqe->res;
61 io_uring_cqe_seen(ring, cqe);
62 return ret;
63}
64
Jens Axboea9bb08d2019-10-18 08:20:31 -060065static int test(struct io_uring *ring, int accept_should_einval)
Jens Axboef2ee0432019-10-17 17:21:25 -060066{
Jens Axboef2ee0432019-10-17 17:21:25 -060067 struct io_uring_cqe *cqe;
68 uint32_t head;
69 uint32_t count = 0;
70 int done = 0;
71 int p_fd[2];
72
73 int32_t recv_s0 = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
74
75 int32_t val = 1;
76 assert(setsockopt(recv_s0, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val)) != -1);
77 assert(setsockopt(recv_s0, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) != -1);
78
79 struct sockaddr_in addr;
80
81 addr.sin_family = AF_INET;
82 addr.sin_port = 0x1235;
83 addr.sin_addr.s_addr = 0x0100007fU;
84
85 assert(bind(recv_s0, (struct sockaddr*)&addr, sizeof(addr)) != -1);
86 assert(listen(recv_s0, 128) != -1);
87
88 p_fd[1] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
89
90 val = 1;
91 assert(setsockopt(p_fd[1], IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) != -1);
92
93 int32_t flags = fcntl(p_fd[1], F_GETFL, 0);
94 assert(flags != -1);
95
96 flags |= O_NONBLOCK;
97 assert(fcntl(p_fd[1], F_SETFL, flags) != -1);
98
99 assert(connect(p_fd[1], (struct sockaddr*)&addr, sizeof(addr)) == -1);
100
101 flags = fcntl(p_fd[1], F_GETFL, 0);
102 assert(flags != -1);
103
104 flags &= ~O_NONBLOCK;
105 assert(fcntl(p_fd[1], F_SETFL, flags) != -1);
106
Jens Axboea9bb08d2019-10-18 08:20:31 -0600107 assert(io_uring_queue_init(32, ring, 0) >= 0);
Jens Axboef2ee0432019-10-17 17:21:25 -0600108
Jens Axboea9bb08d2019-10-18 08:20:31 -0600109 p_fd[0] = accept_conn(ring, recv_s0);
Jens Axboef2ee0432019-10-17 17:21:25 -0600110 if (p_fd[0] == -EINVAL) {
Jens Axboea9bb08d2019-10-18 08:20:31 -0600111 if (accept_should_einval)
112 goto out;
Jens Axboef2ee0432019-10-17 17:21:25 -0600113 fprintf(stdout, "Accept not supported, skipping\n");
Jens Axboea9bb08d2019-10-18 08:20:31 -0600114 no_accept = 1;
Jens Axboef2ee0432019-10-17 17:21:25 -0600115 goto out;
116 }
117 assert(p_fd[0] >= 0);
118
Jens Axboea9bb08d2019-10-18 08:20:31 -0600119 queue_send(ring, p_fd[1]);
120 queue_recv(ring, p_fd[0]);
Jens Axboef2ee0432019-10-17 17:21:25 -0600121
Jens Axboea9bb08d2019-10-18 08:20:31 -0600122 assert(io_uring_submit_and_wait(ring, 2) != -1);
Jens Axboef2ee0432019-10-17 17:21:25 -0600123
124 while (count < 2) {
Jens Axboea9bb08d2019-10-18 08:20:31 -0600125 io_uring_for_each_cqe(ring, head, cqe) {
Jens Axboef2ee0432019-10-17 17:21:25 -0600126 if (cqe->res < 0) {
127 fprintf(stderr, "Got cqe res %d\n", cqe->res);
128 done = 1;
129 break;
130 }
131 assert(cqe->res == 128);
132 count++;
133 }
134
135 assert(count <= 2);
Jens Axboea9bb08d2019-10-18 08:20:31 -0600136 io_uring_cq_advance(ring, count);
Jens Axboef2ee0432019-10-17 17:21:25 -0600137 if (done)
138 goto err;
139 }
140
141out:
Jens Axboea9bb08d2019-10-18 08:20:31 -0600142 close(p_fd[0]);
143 close(p_fd[1]);
Jens Axboef2ee0432019-10-17 17:21:25 -0600144 return 0;
145err:
Jens Axboea9bb08d2019-10-18 08:20:31 -0600146 close(p_fd[0]);
147 close(p_fd[1]);
Jens Axboef2ee0432019-10-17 17:21:25 -0600148 return 1;
149}
Jens Axboea9bb08d2019-10-18 08:20:31 -0600150
151static int test_accept(void)
152{
153 struct io_uring m_io_uring;
154 int ret;
155
156 assert(io_uring_queue_init(32, &m_io_uring, 0) >= 0);
157 ret = test(&m_io_uring, 0);
158 io_uring_queue_exit(&m_io_uring);
159 return ret;
160}
161
162static int test_accept_sqpoll(void)
163{
164 struct io_uring m_io_uring;
165 int ret;
166
167 assert(io_uring_queue_init(32, &m_io_uring, IORING_SETUP_SQPOLL) >= 0);
168 ret = test(&m_io_uring, 1);
169 io_uring_queue_exit(&m_io_uring);
170 return ret;
171}
172
173int main(int argc, char *argv[])
174{
175 int ret;
176
177 ret = test_accept();
178 if (ret) {
179 fprintf(stderr, "test_accept failed\n");
180 return ret;
181 }
182 if (no_accept)
183 return 0;
184
185 ret = test_accept_sqpoll();
186 if (ret) {
187 fprintf(stderr, "test_accept_sqpoll failed\n");
188 return ret;
189 }
190
191 return 0;
192}