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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
/*
* Simple test case showing using send and recv through io_uring
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include "liburing.h"
static char str[] = "This is a test of send and recv over io_uring!";
#define MAX_MSG 128
#define PORT 10200
#define HOST "127.0.0.1"
#if 0
# define io_uring_prep_send io_uring_prep_write
# define io_uring_prep_recv io_uring_prep_read
#endif
static int recv_prep(struct io_uring *ring, struct iovec *iov)
{
struct sockaddr_in saddr;
struct io_uring_sqe *sqe;
int sockfd, ret;
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
saddr.sin_port = htons(PORT);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("socket");
return 1;
}
ret = bind(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
if (ret < 0) {
perror("bind");
goto err;
}
sqe = io_uring_get_sqe(ring);
io_uring_prep_recv(sqe, sockfd, iov->iov_base, iov->iov_len, 0);
sqe->user_data = 2;
ret = io_uring_submit(ring);
if (ret <= 0) {
fprintf(stderr, "submit failed: %d\n", ret);
goto err;
}
return 0;
err:
close(sockfd);
return 1;
}
static int do_recv(struct io_uring *ring, struct iovec *iov)
{
struct io_uring_cqe *cqe;
io_uring_wait_cqe(ring, &cqe);
if (cqe->res < 0) {
fprintf(stderr, "failed cqe: %d\n", cqe->res);
goto err;
}
if (cqe->res -1 != strlen(str)) {
fprintf(stderr, "got wrong length: %d/%d\n", cqe->res,
(int) strlen(str) + 1);
goto err;
}
if (strcmp(str, iov->iov_base)) {
fprintf(stderr, "string mismatch\n");
goto err;
}
return 0;
err:
return 1;
}
static void *recv_fn(void *data)
{
pthread_mutex_t *mutex = data;
char buf[MAX_MSG + 1];
struct iovec iov = {
.iov_base = buf,
.iov_len = sizeof(buf) - 1,
};
struct io_uring ring;
int ret;
io_uring_queue_init(1, &ring, 0);
recv_prep(&ring, &iov);
pthread_mutex_unlock(mutex);
ret = do_recv(&ring, &iov);
io_uring_queue_exit(&ring);
return (void *)(intptr_t)ret;
}
static int do_send(void)
{
struct sockaddr_in saddr;
struct iovec iov = {
.iov_base = str,
.iov_len = sizeof(str),
};
struct io_uring ring;
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
int sockfd, ret;
ret = io_uring_queue_init(1, &ring, 0);
if (ret) {
fprintf(stderr, "queue init fail: %d\n", ret);
return 1;
}
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(PORT);
inet_pton(AF_INET, HOST, &saddr.sin_addr);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("socket");
return 1;
}
ret = connect(sockfd, &saddr, sizeof(saddr));
if (ret < 0) {
perror("connect");
return 1;
}
sqe = io_uring_get_sqe(&ring);
io_uring_prep_send(sqe, sockfd, iov.iov_base, iov.iov_len, 0);
sqe->user_data = 1;
ret = io_uring_submit(&ring);
if (ret <= 0) {
fprintf(stderr, "submit failed: %d\n", ret);
goto err;
}
ret = io_uring_wait_cqe(&ring, &cqe);
if (cqe->res != iov.iov_len) {
fprintf(stderr, "failed cqe: %d\n", cqe->res);
goto err;
}
close(sockfd);
return 0;
err:
close(sockfd);
return 1;
}
int main(int argc, char *argv[])
{
pthread_mutexattr_t attr;
pthread_t recv_thread;
pthread_mutex_t mutex;
int ret;
void *retval;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, 1);
pthread_mutex_init(&mutex, &attr);
pthread_mutex_lock(&mutex);
ret = pthread_create(&recv_thread, NULL, recv_fn, &mutex);
if (ret) {
fprintf(stderr, "Thread create failed: %d\n", ret);
return 1;
}
pthread_mutex_lock(&mutex);
do_send();
pthread_join(recv_thread, &retval);
ret = (int)(intptr_t)retval;
return ret;
}
|