summaryrefslogtreecommitdiff
path: root/test/send_recvmsg.c
blob: 43121f08f00618ea1d04a5bff0798be7d1b28598 (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* SPDX-License-Identifier: MIT */
/*
 * Simple test case showing using sendmsg and recvmsg through io_uring
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <assert.h>

#include "liburing.h"

static char str[] = "This is a test of sendmsg and recvmsg over io_uring!";

#define MAX_MSG	128

#define PORT	10200
#define HOST	"127.0.0.1"

#define BUF_BGID	10
#define BUF_BID		89

#define MAX_IOV_COUNT	10

static int recv_prep(struct io_uring *ring, struct iovec iov[], int iov_count,
		     int bgid)
{
	struct sockaddr_in saddr;
	struct msghdr msg;
	struct io_uring_sqe *sqe;
	int sockfd, ret;
	int val = 1;

	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;
	}

	val = 1;
	setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
	setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));

	ret = bind(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
	if (ret < 0) {
		perror("bind");
		goto err;
	}

	sqe = io_uring_get_sqe(ring);
	if (!sqe) {
		fprintf(stderr, "io_uring_get_sqe failed\n");
		return 1;
	}

	io_uring_prep_recvmsg(sqe, sockfd, &msg, 0);
	if (bgid) {
		iov->iov_base = NULL;
		sqe->flags |= IOSQE_BUFFER_SELECT;
		sqe->buf_group = bgid;
		iov_count = 1;
	}
	memset(&msg, 0, sizeof(msg));
	msg.msg_namelen = sizeof(struct sockaddr_in);
	msg.msg_iov = iov;
	msg.msg_iovlen = iov_count;

	ret = io_uring_submit(ring);
	if (ret <= 0) {
		fprintf(stderr, "submit failed: %d\n", ret);
		goto err;
	}

	close(sockfd);
	return 0;
err:
	close(sockfd);
	return 1;
}

struct recv_data {
	pthread_mutex_t *mutex;
	int buf_select;
	int no_buf_add;
	int iov_count;
};

static int do_recvmsg(struct io_uring *ring, char buf[MAX_MSG + 1],
		      struct recv_data *rd)
{
	struct io_uring_cqe *cqe;
	int ret;

	ret = io_uring_wait_cqe(ring, &cqe);
	if (ret) {
		fprintf(stdout, "wait_cqe: %d\n", ret);
		goto err;
	}
	if (cqe->res < 0) {
		if (rd->no_buf_add && rd->buf_select)
			return 0;
		fprintf(stderr, "%s: failed cqe: %d\n", __FUNCTION__, cqe->res);
		goto err;
	}
	if (cqe->flags) {
		int bid = cqe->flags >> 16;
		if (bid != BUF_BID)
			fprintf(stderr, "Buffer ID mismatch %d\n", bid);
	}

	if (rd->no_buf_add && rd->buf_select) {
		fprintf(stderr, "Expected -ENOBUFS: %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 (strncmp(str, buf, MAX_MSG + 1)) {
		fprintf(stderr, "string mismatch\n");
		goto err;
	}

	return 0;
err:
	return 1;
}

static void init_iov(struct iovec iov[MAX_IOV_COUNT], int iov_to_use,
		     char buf[MAX_MSG + 1])
{
	int i, last_idx = iov_to_use - 1;

	assert(0 < iov_to_use && iov_to_use <= MAX_IOV_COUNT);
	for (i = 0; i < last_idx; ++i) {
		iov[i].iov_base = buf + i;
		iov[i].iov_len = 1;
	}

	iov[last_idx].iov_base = buf + last_idx;
	iov[last_idx].iov_len = MAX_MSG - last_idx;
}

static void *recv_fn(void *data)
{
	struct recv_data *rd = data;
	pthread_mutex_t *mutex = rd->mutex;
	char buf[MAX_MSG + 1];
	struct iovec iov[MAX_IOV_COUNT];
	struct io_uring_sqe *sqe;
	struct io_uring_cqe *cqe;
	struct io_uring ring;
	int ret;

	init_iov(iov, rd->iov_count, buf);

	ret = io_uring_queue_init(1, &ring, 0);
	if (ret) {
		fprintf(stderr, "queue init failed: %d\n", ret);
		goto err;
	}

	if (rd->buf_select && !rd->no_buf_add) {
		sqe = io_uring_get_sqe(&ring);
		io_uring_prep_provide_buffers(sqe, buf, sizeof(buf) -1, 1,
						BUF_BGID, BUF_BID);
		ret = io_uring_submit(&ring);
		if (ret != 1) {
			fprintf(stderr, "submit ret=%d\n", ret);
			goto err;
		}

		ret = io_uring_wait_cqe(&ring, &cqe);
		if (ret) {
			fprintf(stderr, "wait_cqe=%d\n", ret);
			goto err;
		}
		ret = cqe->res;
		io_uring_cqe_seen(&ring, cqe);
		if (ret == -EINVAL) {
			fprintf(stdout, "PROVIDE_BUFFERS not supported, skip\n");
			goto out;
			goto err;
		} else if (ret < 0) {
			fprintf(stderr, "PROVIDER_BUFFERS %d\n", ret);
			goto err;
		}
	}

	ret = recv_prep(&ring, iov, rd->iov_count, rd->buf_select ? BUF_BGID : 0);
	if (ret) {
		fprintf(stderr, "recv_prep failed: %d\n", ret);
		goto err;
	}

	pthread_mutex_unlock(mutex);
	ret = do_recvmsg(&ring, buf, rd);

	io_uring_queue_exit(&ring);

err:
	return (void *)(intptr_t)ret;
out:
	pthread_mutex_unlock(mutex);
	io_uring_queue_exit(&ring);
	return NULL;
}

static int do_sendmsg(void)
{
	struct sockaddr_in saddr;
	struct iovec iov = {
		.iov_base = str,
		.iov_len = sizeof(str),
	};
	struct msghdr msg;
	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 failed: %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);

	memset(&msg, 0, sizeof(msg));
	msg.msg_name = &saddr;
	msg.msg_namelen = sizeof(struct sockaddr_in);
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;

	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (sockfd < 0) {
		perror("socket");
		return 1;
	}

	sqe = io_uring_get_sqe(&ring);
	io_uring_prep_sendmsg(sqe, sockfd, &msg, 0);

	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 < 0) {
		fprintf(stderr, "%s: failed cqe: %d\n", __FUNCTION__, cqe->res);
		goto err;
	}

	close(sockfd);
	return 0;
err:
	close(sockfd);
	return 1;
}

static int test(int buf_select, int no_buf_add, int iov_count)
{
	struct recv_data rd;
	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);

	rd.mutex = &mutex;
	rd.buf_select = buf_select;
	rd.no_buf_add = no_buf_add;
	rd.iov_count = iov_count;
	ret = pthread_create(&recv_thread, NULL, recv_fn, &rd);
	if (ret) {
		pthread_mutex_unlock(&mutex);
		fprintf(stderr, "Thread create failed\n");
		return 1;
	}

	pthread_mutex_lock(&mutex);
	do_sendmsg();
	pthread_join(recv_thread, &retval);
	ret = (intptr_t)retval;

	return ret;
}

int main(int argc, char *argv[])
{
	int ret;

	if (argc > 1)
		return 0;

	ret = test(0, 0, 1);
	if (ret) {
		fprintf(stderr, "send_recvmsg 0 failed\n");
		return 1;
	}

	ret = test(0, 0, 10);
	if (ret) {
		fprintf(stderr, "send_recvmsg multi iov failed\n");
		return 1;
	}

	ret = test(1, 0, 1);
	if (ret) {
		fprintf(stderr, "send_recvmsg 1 0 failed\n");
		return 1;
	}

	ret = test(1, 1, 1);
	if (ret) {
		fprintf(stderr, "send_recvmsg 1 1 failed\n");
		return 1;
	}

	return 0;
}