1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
#include <liburing.h>
#include <netdb.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include "liburing.h"
#include "helpers.h"
#include "../src/syscall.h"
struct io_uring io_uring;
int sys_io_uring_enter(const int fd,
const unsigned to_submit,
const unsigned min_complete,
const unsigned flags, sigset_t * const sig)
{
return __sys_io_uring_enter(fd, to_submit, min_complete, flags, sig);
}
int submit_sqe(void)
{
struct io_uring_sq *sq = &io_uring.sq;
const unsigned tail = *sq->ktail;
sq->array[tail & sq->ring_mask] = 0;
io_uring_smp_store_release(sq->ktail, tail + 1);
return sys_io_uring_enter(io_uring.ring_fd, 1, 0, 0, NULL);
}
int main(int argc, char **argv)
{
struct addrinfo *addr_info_list = NULL;
struct addrinfo *ai, *addr_info = NULL;
struct io_uring_params params;
struct io_uring_sqe *sqe;
struct addrinfo hints;
struct sockaddr sa;
socklen_t sa_size = sizeof(sa);
int ret, listen_fd, connect_fd, val, i;
if (argc > 1)
return T_EXIT_SKIP;
memset(¶ms, 0, sizeof(params));
ret = io_uring_queue_init_params(4, &io_uring, ¶ms);
if (ret) {
fprintf(stderr, "io_uring_init_failed: %d\n", ret);
return T_EXIT_FAIL;
}
if (!(params.features & IORING_FEAT_SUBMIT_STABLE)) {
fprintf(stdout, "FEAT_SUBMIT_STABLE not there, skipping\n");
return T_EXIT_SKIP;
}
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV;
ret = getaddrinfo(NULL, "12345", &hints, &addr_info_list);
if (ret < 0) {
perror("getaddrinfo");
return T_EXIT_FAIL;
}
for (ai = addr_info_list; ai; ai = ai->ai_next) {
if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) {
addr_info = ai;
break;
}
}
if (!addr_info) {
fprintf(stderr, "addrinfo not found\n");
return T_EXIT_FAIL;
}
sqe = &io_uring.sq.sqes[0];
listen_fd = -1;
ret = socket(addr_info->ai_family, SOCK_STREAM,
addr_info->ai_protocol);
if (ret < 0) {
perror("socket");
return T_EXIT_FAIL;
}
listen_fd = ret;
val = 1;
setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int));
setsockopt(listen_fd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(int));
ret = bind(listen_fd, addr_info->ai_addr, addr_info->ai_addrlen);
if (ret < 0) {
perror("bind");
return T_EXIT_FAIL;
}
ret = listen(listen_fd, SOMAXCONN);
if (ret < 0) {
perror("listen");
return T_EXIT_FAIL;
}
memset(&sa, 0, sizeof(sa));
io_uring_prep_accept(sqe, listen_fd, &sa, &sa_size, 0);
sqe->user_data = 1;
ret = submit_sqe();
if (ret != 1) {
fprintf(stderr, "submit failed: %d\n", ret);
return T_EXIT_FAIL;
}
connect_fd = -1;
ret = socket(addr_info->ai_family, SOCK_STREAM, addr_info->ai_protocol);
if (ret < 0) {
perror("socket");
return T_EXIT_FAIL;
}
connect_fd = ret;
io_uring_prep_connect(sqe, connect_fd, addr_info->ai_addr,
addr_info->ai_addrlen);
sqe->user_data = 2;
ret = submit_sqe();
if (ret != 1) {
fprintf(stderr, "submit failed: %d\n", ret);
return T_EXIT_FAIL;
}
for (i = 0; i < 2; i++) {
struct io_uring_cqe *cqe = NULL;
ret = io_uring_wait_cqe(&io_uring, &cqe);
if (ret) {
fprintf(stderr, "io_uring_wait_cqe: %d\n", ret);
return T_EXIT_FAIL;
}
switch (cqe->user_data) {
case 1:
if (cqe->res < 0) {
fprintf(stderr, "accept failed: %d\n", cqe->res);
return T_EXIT_FAIL;
}
break;
case 2:
if (cqe->res) {
fprintf(stderr, "connect failed: %d\n", cqe->res);
return T_EXIT_FAIL;
}
break;
}
io_uring_cq_advance(&io_uring, 1);
}
freeaddrinfo(addr_info_list);
io_uring_queue_exit(&io_uring);
return T_EXIT_PASS;
}
|