aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/ringbuf-status.c
blob: d4a56c7473e755036488d972327e2827eaa570db (plain) (blame)
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
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
 * Description: test reading provided ring buf head
 *
 */
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

#include "liburing.h"
#include "helpers.h"

#define BUF_SIZE	32
#define NR_BUFS		8
#define FSIZE		(BUF_SIZE * NR_BUFS)

#define BR_MASK		(NR_BUFS - 1)
#define BGID		1

static int no_buf_ring;
static int no_buf_ring_status;

static int test_max(void)
{
	struct io_uring_buf_ring *br;
	struct io_uring ring;
	int nr_bufs = 32768;
	int ret, i;
	char *buf;

	ret = io_uring_queue_init(1, &ring, 0);
	if (ret) {
		fprintf(stderr, "ring setup failed: %d\n", ret);
		return 1;
	}

	if (posix_memalign((void **) &buf, 4096, FSIZE))
		return 1;

	br = io_uring_setup_buf_ring(&ring, nr_bufs, BGID, 0, &ret);
	if (!br) {
		fprintf(stderr, "Buffer ring register failed %d\n", ret);
		return 1;
	}

	ret = io_uring_buf_ring_available(&ring, br, BGID);
	if (ret) {
		fprintf(stderr, "Bad available count %d\n", ret);
		return 1;
	}

	for (i = 0; i < nr_bufs / 2; i++)
		io_uring_buf_ring_add(br, buf, BUF_SIZE, i + 1, nr_bufs - 1, i);
	io_uring_buf_ring_advance(br, nr_bufs / 2);

	ret = io_uring_buf_ring_available(&ring, br, BGID);
	if (ret != nr_bufs / 2) {
		fprintf(stderr, "Bad half full available count %d\n", ret);
		return 1;
	}

	for (i = 0; i < nr_bufs / 2; i++)
		io_uring_buf_ring_add(br, buf, BUF_SIZE, i + 1, nr_bufs - 1, i);
	io_uring_buf_ring_advance(br, nr_bufs / 2);

	ret = io_uring_buf_ring_available(&ring, br, BGID);
	if (ret != nr_bufs) {
		fprintf(stderr, "Bad half full available count %d\n", ret);
		return 1;
	}

	free(buf);
	io_uring_queue_exit(&ring);
	return T_EXIT_PASS;
}

static int test(int invalid)
{
	struct io_uring_sqe *sqe;
	struct io_uring_cqe *cqe;
	struct io_uring ring;
	struct io_uring_buf_ring *br;
	int ret, i, fds[2];
	uint16_t head;
	char *buf;
	void *ptr;
	char output[16];

	memset(output, 0x55, sizeof(output));

	ret = io_uring_queue_init(NR_BUFS, &ring, 0);
	if (ret) {
		fprintf(stderr, "ring setup failed: %d\n", ret);
		return 1;
	}

	if (pipe(fds) < 0) {
		perror("pipe");
		return T_EXIT_FAIL;
	}

	if (posix_memalign((void **) &buf, 4096, FSIZE))
		return 1;

	br = io_uring_setup_buf_ring(&ring, NR_BUFS, BGID, 0, &ret);
	if (!br) {
		if (ret == -EINVAL) {
			no_buf_ring = 1;
			return 0;
		}
		fprintf(stderr, "Buffer ring register failed %d\n", ret);
		return 1;
	}

	ptr = buf;
	for (i = 0; i < NR_BUFS; i++) {
		io_uring_buf_ring_add(br, ptr, BUF_SIZE, i + 1, BR_MASK, i);
		ptr += BUF_SIZE;
	}
	io_uring_buf_ring_advance(br, NR_BUFS);

	/* head should be zero at this point */
	head = 1;
	if (!invalid)
		ret = io_uring_buf_ring_head(&ring, BGID, &head);
	else
		ret = io_uring_buf_ring_head(&ring, BGID + 10, &head);
	if (ret) {
		if (ret == -EINVAL) {
			no_buf_ring_status = 1;
			return T_EXIT_SKIP;
		}
		if (invalid && ret == -ENOENT)
			return T_EXIT_PASS;
		fprintf(stderr, "buf_ring_head: %d\n", ret);
		return T_EXIT_FAIL;
	}
	if (invalid) {
		fprintf(stderr, "lookup of bad group id succeeded\n");
		return T_EXIT_FAIL;
	}
	if (head != 0) {
		fprintf(stderr, "bad head %d\n", head);
		return T_EXIT_FAIL;
	}

	ret = io_uring_buf_ring_available(&ring, br, BGID);
	if (ret != NR_BUFS) {
		fprintf(stderr, "ring available %d\n", ret);
		return T_EXIT_FAIL;
	}

	sqe = io_uring_get_sqe(&ring);
	io_uring_prep_read(sqe, fds[0], NULL, BUF_SIZE, i * BUF_SIZE);
	sqe->buf_group = BGID;
	sqe->flags |= IOSQE_BUFFER_SELECT;
	sqe->user_data = 1;

	ret = io_uring_submit(&ring);
	if (ret != 1) {
		fprintf(stderr, "submit: %d\n", ret);
		return T_EXIT_FAIL;
	}

	/* head should still be zero at this point, no buffers consumed */
	head = 1;
	ret = io_uring_buf_ring_head(&ring, BGID, &head);
	if (head != 0) {
		fprintf(stderr, "bad head after submit %d\n", head);
		return T_EXIT_FAIL;
	}

	ret = write(fds[1], output, sizeof(output));
	if (ret != sizeof(output)) {
		fprintf(stderr, "pipe buffer write %d\n", ret);
		return T_EXIT_FAIL;
	}

	ret = io_uring_wait_cqe(&ring, &cqe);
	if (ret) {
		fprintf(stderr, "wait cqe failed %d\n", ret);
		return T_EXIT_FAIL;
	}
	if (cqe->res != sizeof(output)) {
		fprintf(stderr, "cqe res %d\n", cqe->res);
		return T_EXIT_FAIL;
	}
	if (!(cqe->flags & IORING_CQE_F_BUFFER)) {
		fprintf(stderr, "no buffer selected\n");
		return T_EXIT_FAIL;
	}
	io_uring_cqe_seen(&ring, cqe);

	/* head should now be one, we consumed a buffer */
	ret = io_uring_buf_ring_head(&ring, BGID, &head);
	if (head != 1) {
		fprintf(stderr, "bad head after cqe %d\n", head);
		return T_EXIT_FAIL;
	}

	ret = io_uring_buf_ring_available(&ring, br, BGID);
	if (ret != NR_BUFS - 1) {
		fprintf(stderr, "ring available %d\n", ret);
		return T_EXIT_FAIL;
	}

	close(fds[0]);
	close(fds[1]);
	free(buf);
	io_uring_queue_exit(&ring);
	return T_EXIT_PASS;
}

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

	ret = test(0);
	if (ret == T_EXIT_FAIL) {
		fprintf(stderr, "test 0 failed\n");
		return T_EXIT_FAIL;
	}
	if (no_buf_ring || no_buf_ring_status)
		return T_EXIT_SKIP;

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

	ret = test_max();
	if (ret == T_EXIT_FAIL) {
		fprintf(stderr, "test_max failed\n");
		return T_EXIT_FAIL;
	}

	return T_EXIT_PASS;
}