aboutsummaryrefslogtreecommitdiff
path: root/test/rsrc_tags.c
blob: 2b4890bae8a0ebda6fd30a077356ddc3abc81a5c (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* SPDX-License-Identifier: MIT */
/*
 * Description: run various file registration tests
 *
 */
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>

#include "../src/syscall.h"
#include "helpers.h"
#include "liburing.h"

static int pipes[2];

enum {
	TEST_IORING_RSRC_FILE		= 0,
	TEST_IORING_RSRC_BUFFER		= 1,
};

static bool check_cq_empty(struct io_uring *ring)
{
	struct io_uring_cqe *cqe = NULL;
	int ret;

	sleep(1); /* doesn't happen immediately, so wait */
	ret = io_uring_peek_cqe(ring, &cqe); /* nothing should be there */
	return ret == -EAGAIN;
}

static int register_rsrc(struct io_uring *ring, int type, int nr,
			  const void *arg, const __u64 *tags)
{
	struct io_uring_rsrc_register reg;
	int ret, reg_type;

	memset(&reg, 0, sizeof(reg));
	reg.nr = nr;
	reg.data = (__u64)arg;
	reg.tags = (__u64)tags;

	reg_type = IORING_REGISTER_FILES2;
	if (type != TEST_IORING_RSRC_FILE)
		reg_type = IORING_REGISTER_BUFFERS2;

	ret = __sys_io_uring_register(ring->ring_fd, reg_type,
					&reg, sizeof(reg));
	return ret ? -errno : 0;
}

static int update_rsrc(struct io_uring *ring, int type, int nr, int off,
			const void *arg, const __u64 *tags)
{
	struct io_uring_rsrc_update2 up;
	int ret, up_type;

	memset(&up, 0, sizeof(up));
	up.offset = off;
	up.data = (__u64)arg;
	up.tags = (__u64)tags;
	up.nr = nr;

	up_type = IORING_REGISTER_FILES_UPDATE2;
	if (type != TEST_IORING_RSRC_FILE)
		up_type = IORING_REGISTER_BUFFERS_UPDATE;
	ret = __sys_io_uring_register(ring->ring_fd, up_type,
				      &up, sizeof(up));
	return ret < 0 ? -errno : ret;
}

static bool has_rsrc_update(void)
{
	struct io_uring ring;
	char buf[1024];
	struct iovec vec = {.iov_base = buf, .iov_len = sizeof(buf), };
	int ret;

	ret = io_uring_queue_init(1, &ring, 0);
	if (ret)
		return false;

	ret = register_rsrc(&ring, TEST_IORING_RSRC_BUFFER, 1, &vec, NULL);
	io_uring_queue_exit(&ring);
	return ret != -EINVAL;
}

static int test_tags_generic(int nr, int type, void *rsrc, int ring_flags)
{
	struct io_uring_cqe *cqe = NULL;
	struct io_uring ring;
	int i, ret;
	__u64 *tags;

	tags = malloc(nr * sizeof(*tags));
	if (!tags)
		return 1;
	for (i = 0; i < nr; i++)
		tags[i] = i + 1;
	ret = io_uring_queue_init(1, &ring, 0);
	if (ret) {
		printf("ring setup failed\n");
		return 1;
	}

	ret = register_rsrc(&ring, type, nr, rsrc, tags);
	if (ret) {
		fprintf(stderr, "rsrc register failed %i\n", ret);
		return 1;
	}

	/* test that tags are set */
	tags[0] = 666;
	ret = update_rsrc(&ring, type, 1, 0, rsrc, &tags[0]);
	assert(ret == 1);
	ret = io_uring_wait_cqe(&ring, &cqe);
	assert(!ret && cqe->user_data == 1);
	io_uring_cqe_seen(&ring, cqe);

	/* test that tags are updated */
	tags[0] = 0;
	ret = update_rsrc(&ring, type, 1, 0, rsrc, &tags[0]);
	assert(ret == 1);
	ret = io_uring_wait_cqe(&ring, &cqe);
	assert(!ret && cqe->user_data == 666);
	io_uring_cqe_seen(&ring, cqe);

	/* test tag=0 doesn't emit CQE */
	tags[0] = 1;
	ret = update_rsrc(&ring, type, 1, 0, rsrc, &tags[0]);
	assert(ret == 1);
	assert(check_cq_empty(&ring));

	free(tags);
	io_uring_queue_exit(&ring);
	return 0;
}

static int test_buffers_update(void)
{
	struct io_uring_sqe *sqe;
	struct io_uring_cqe *cqe = NULL;
	struct io_uring ring;
	const int nr = 5;
	int buf_idx = 1, i, ret;
	int pipes[2];
	char tmp_buf[1024];
	char tmp_buf2[1024];
	struct iovec vecs[nr];
	__u64 tags[nr];

	for (i = 0; i < nr; i++) {
		vecs[i].iov_base = tmp_buf;
		vecs[i].iov_len = 1024;
		tags[i] = i + 1;
	}

	ret = test_tags_generic(nr, TEST_IORING_RSRC_BUFFER, vecs, 0);
	if (ret)
		return 1;

	ret = io_uring_queue_init(1, &ring, 0);
	if (ret) {
		printf("ring setup failed\n");
		return 1;
	}
	if (pipe(pipes) < 0) {
		perror("pipe");
		return 1;
	}
	ret = register_rsrc(&ring, TEST_IORING_RSRC_BUFFER, nr, vecs, tags);
	if (ret) {
		fprintf(stderr, "rsrc register failed %i\n", ret);
		return 1;
	}

	/* test that CQE is not emmited before we're done with a buffer */
	sqe = io_uring_get_sqe(&ring);
	io_uring_prep_read_fixed(sqe, pipes[0], tmp_buf, 10, 0, 0);
	sqe->user_data = 100;
	ret = io_uring_submit(&ring);
	if (ret != 1) {
		fprintf(stderr, "%s: got %d, wanted 1\n", __FUNCTION__, ret);
		return 1;
	}
	ret = io_uring_peek_cqe(&ring, &cqe);
	assert(ret == -EAGAIN);

	vecs[buf_idx].iov_base = tmp_buf2;
	ret = update_rsrc(&ring, TEST_IORING_RSRC_BUFFER, 1, buf_idx,
			  &vecs[buf_idx], &tags[buf_idx]);
	if (ret != 1) {
		fprintf(stderr, "rsrc update failed %i %i\n", ret, errno);
		return 1;
	}

	ret = io_uring_peek_cqe(&ring, &cqe); /* nothing should be there */
	assert(ret == -EAGAIN);
	close(pipes[0]);
	close(pipes[1]);

	ret = io_uring_wait_cqe(&ring, &cqe);
	assert(!ret && cqe->user_data == 100);
	io_uring_cqe_seen(&ring, cqe);
	ret = io_uring_wait_cqe(&ring, &cqe);
	assert(!ret && cqe->user_data == buf_idx + 1);
	io_uring_cqe_seen(&ring, cqe);

	io_uring_queue_exit(&ring);
	return 0;
}

static int test_buffers_empty_buffers(void)
{
	struct io_uring_sqe *sqe;
	struct io_uring_cqe *cqe = NULL;
	struct io_uring ring;
	const int nr = 5;
	int ret, i;
	char tmp_buf[1024];
	struct iovec vecs[nr];

	for (i = 0; i < nr; i++) {
		vecs[i].iov_base = 0;
		vecs[i].iov_len = 0;
	}
	vecs[0].iov_base = tmp_buf;
	vecs[0].iov_len = 10;

	ret = io_uring_queue_init(1, &ring, 0);
	if (ret) {
		printf("ring setup failed\n");
		return 1;
	}

	ret = register_rsrc(&ring, TEST_IORING_RSRC_BUFFER, nr, vecs, NULL);
	if (ret) {
		fprintf(stderr, "rsrc register failed %i\n", ret);
		return 1;
	}

	/* empty to buffer */
	vecs[1].iov_base = tmp_buf;
	vecs[1].iov_len = 10;
	ret = update_rsrc(&ring, TEST_IORING_RSRC_BUFFER, 1, 1, &vecs[1], NULL);
	if (ret != 1) {
		fprintf(stderr, "rsrc update failed %i %i\n", ret, errno);
		return 1;
	}

	/* buffer to empty */
	vecs[0].iov_base = 0;
	vecs[0].iov_len = 0;
	ret = update_rsrc(&ring, TEST_IORING_RSRC_BUFFER, 1, 0, &vecs[0], NULL);
	if (ret != 1) {
		fprintf(stderr, "rsrc update failed %i %i\n", ret, errno);
		return 1;
	}

	/* zero to zero is ok */
	ret = update_rsrc(&ring, TEST_IORING_RSRC_BUFFER, 1, 2, &vecs[2], NULL);
	if (ret != 1) {
		fprintf(stderr, "rsrc update failed %i %i\n", ret, errno);
		return 1;
	}

	/* empty buf with non-zero len fails */
	vecs[3].iov_base = 0;
	vecs[3].iov_len = 1;
	ret = update_rsrc(&ring, TEST_IORING_RSRC_BUFFER, 1, 3, &vecs[3], NULL);
	if (ret >= 0) {
		fprintf(stderr, "rsrc update failed %i %i\n", ret, errno);
		return 1;
	}

	/* test rw on empty ubuf is failed */
	sqe = io_uring_get_sqe(&ring);
	io_uring_prep_read_fixed(sqe, pipes[0], tmp_buf, 10, 0, 2);
	sqe->user_data = 100;
	ret = io_uring_submit(&ring);
	if (ret != 1) {
		fprintf(stderr, "%s: got %d, wanted 1\n", __FUNCTION__, ret);
		return 1;
	}
	ret = io_uring_wait_cqe(&ring, &cqe);
	assert(!ret && cqe->user_data == 100);
	assert(cqe->res);
	io_uring_cqe_seen(&ring, cqe);

	sqe = io_uring_get_sqe(&ring);
	io_uring_prep_read_fixed(sqe, pipes[0], tmp_buf, 0, 0, 2);
	sqe->user_data = 100;
	ret = io_uring_submit(&ring);
	if (ret != 1) {
		fprintf(stderr, "%s: got %d, wanted 1\n", __FUNCTION__, ret);
		return 1;
	}
	ret = io_uring_wait_cqe(&ring, &cqe);
	assert(!ret && cqe->user_data == 100);
	assert(cqe->res);
	io_uring_cqe_seen(&ring, cqe);

	io_uring_queue_exit(&ring);
	return 0;
}


static int test_files(int ring_flags)
{
	struct io_uring_cqe *cqe = NULL;
	struct io_uring ring;
	const int nr = 50;
	int off = 5, i, ret, fd;
	int files[nr];
	__u64 tags[nr], tag;

	for (i = 0; i < nr; ++i) {
		files[i] = pipes[0];
		tags[i] = i + 1;
	}

	ret = test_tags_generic(nr, TEST_IORING_RSRC_FILE, files, ring_flags);
	if (ret)
		return 1;

	ret = io_uring_queue_init(1, &ring, ring_flags);
	if (ret) {
		printf("ring setup failed\n");
		return 1;
	}
	ret = register_rsrc(&ring, TEST_IORING_RSRC_FILE, nr, files, tags);
	if (ret) {
		fprintf(stderr, "rsrc register failed %i\n", ret);
		return 1;
	}

	/* check update did update tag */
	fd = -1;
	ret = io_uring_register_files_update(&ring, off, &fd, 1);
	assert(ret == 1);
	ret = io_uring_wait_cqe(&ring, &cqe);
	assert(!ret && cqe->user_data == tags[off]);
	io_uring_cqe_seen(&ring, cqe);

	/* remove removed file, shouldn't emit old tag */
	ret = io_uring_register_files_update(&ring, off, &fd, 1);
	assert(ret <= 1);
	assert(check_cq_empty(&ring));

	/* non-zero tag with remove update is disallowed */
	tag = 1;
	fd = -1;
	ret = update_rsrc(&ring, TEST_IORING_RSRC_FILE, 1, off + 1, &fd, &tag);
	assert(ret);

	io_uring_queue_exit(&ring);
	return 0;
}

static int test_notag(void)
{
	struct io_uring_cqe *cqe = NULL;
	struct io_uring ring;
	int i, ret, fd;
	const int nr = 50;
	int files[nr];

	ret = io_uring_queue_init(1, &ring, 0);
	if (ret) {
		printf("ring setup failed\n");
		return 1;
	}
	for (i = 0; i < nr; ++i)
		files[i] = pipes[0];

	ret = io_uring_register_files(&ring, files, nr);
	assert(!ret);

	/* default register, update shouldn't emit CQE */
	fd = -1;
	ret = io_uring_register_files_update(&ring, 0, &fd, 1);
	assert(ret == 1);
	assert(check_cq_empty(&ring));

	ret = io_uring_unregister_files(&ring);
	assert(!ret);
	ret = io_uring_peek_cqe(&ring, &cqe); /* nothing should be there */
	assert(ret);

	io_uring_queue_exit(&ring);
	return 0;
}

int main(int argc, char *argv[])
{
	int ring_flags[] = {0, IORING_SETUP_IOPOLL, IORING_SETUP_SQPOLL};
	int i, ret;

	if (argc > 1)
		return 0;
	if (!has_rsrc_update()) {
		fprintf(stderr, "doesn't support rsrc tags, skip\n");
		return 0;
	}

	if (pipe(pipes) < 0) {
		perror("pipe");
		return 1;
	}

	ret = test_notag();
	if (ret) {
		printf("test_notag failed\n");
		return ret;
	}

	for (i = 0; i < sizeof(ring_flags) / sizeof(ring_flags[0]); i++) {
		ret = test_files(ring_flags[i]);
		if (ret) {
			printf("test_tag failed, type %i\n", i);
			return ret;
		}
	}

	ret = test_buffers_update();
	if (ret) {
		printf("test_buffers_update failed\n");
		return ret;
	}

	ret = test_buffers_empty_buffers();
	if (ret) {
		printf("test_buffers_empty_buffers failed\n");
		return ret;
	}

	return 0;
}