summaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/read-inc-buf-more.c
blob: b035f7c602f62e22360ab8a4e553e959f4734aa2 (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
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
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
 * Description: test that IORING_CQE_F_BUF_MORE is correctly set for
 *		incremental provided buffer rings (IOU_PBUF_RING_INC).
 *
 * Two bugs existed:
 * 1) BUF_MORE was never set for non-pollable files (regular files),
 *    because the early buffer commit path discarded the information.
 * 2) BUF_MORE was not set at EOF (zero-length read), even though
 *    the buffer still had remaining space.
 */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

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

#define BUF_BGID	1
#define BUF_BID		0
#define BUF_SIZE	256
#define READ_SIZE	32

static int no_buf_ring_inc;

static int do_read(struct io_uring *ring, int fd)
{
	struct io_uring_sqe *sqe;
	struct io_uring_cqe *cqe;
	int ret;

	sqe = io_uring_get_sqe(ring);
	io_uring_prep_read(sqe, fd, NULL, READ_SIZE, -1);
	sqe->flags = IOSQE_BUFFER_SELECT;
	sqe->buf_group = BUF_BGID;

	io_uring_submit(ring);

	ret = io_uring_wait_cqe(ring, &cqe);
	if (ret) {
		fprintf(stderr, "wait: %d\n", ret);
		return -1;
	}

	ret = cqe->res;
	if (ret < 0) {
		fprintf(stderr, "read error: %d\n", ret);
		io_uring_cqe_seen(ring, cqe);
		return -1;
	}

	if (!(cqe->flags & IORING_CQE_F_BUFFER)) {
		fprintf(stderr, "no buffer flag set\n");
		io_uring_cqe_seen(ring, cqe);
		return -1;
	}

	/*
	 * If we got data and the buffer still has space, BUF_MORE must be
	 * set. If we got EOF (res=0), BUF_MORE must also be set because the
	 * buffer was not consumed. In both cases, the 256 byte buffer has
	 * room left after a <= 32 byte read.
	 */
	if (!(cqe->flags & IORING_CQE_F_BUF_MORE)) {
		fprintf(stderr, "BUF_MORE not set, res=%d flags=0x%x\n",
			cqe->res, cqe->flags);
		io_uring_cqe_seen(ring, cqe);
		return -1;
	}

	io_uring_cqe_seen(ring, cqe);
	return ret;
}

/*
 * Test BUF_MORE with a pipe (pollable fd). Exercises the normal locked
 * commit path, and tests EOF handling.
 */
static int test_pipe(void)
{
	struct io_uring_buf_ring *br;
	struct io_uring ring;
	int ret, fds[2];
	char *buf;

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

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

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

	br = io_uring_setup_buf_ring(&ring, 1, BUF_BGID, IOU_PBUF_RING_INC,
				     &ret);
	if (!br) {
		if (ret == -EINVAL) {
			no_buf_ring_inc = 1;
			free(buf);
			close(fds[0]);
			close(fds[1]);
			io_uring_queue_exit(&ring);
			return 0;
		}
		fprintf(stderr, "buffer ring register failed: %d\n", ret);
		return 1;
	}

	io_uring_buf_ring_add(br, buf, BUF_SIZE, BUF_BID, 0, 0);
	io_uring_buf_ring_advance(br, 1);

	memset(buf, 0, BUF_SIZE);

	/* Write some data and read it - BUF_MORE should be set */
	ret = write(fds[1], "hello world!", 12);
	if (ret != 12) {
		perror("write");
		return 1;
	}

	ret = do_read(&ring, fds[0]);
	if (ret < 0) {
		fprintf(stderr, "pipe data read failed\n");
		return 1;
	}
	if (ret != 12) {
		fprintf(stderr, "pipe short read: %d\n", ret);
		return 1;
	}

	/* Close write end, read should get EOF - BUF_MORE should be set
	 * because the buffer still has space */
	close(fds[1]);

	ret = do_read(&ring, fds[0]);
	if (ret < 0) {
		fprintf(stderr, "pipe EOF read failed\n");
		return 1;
	}
	if (ret != 0) {
		fprintf(stderr, "expected EOF, got %d\n", ret);
		return 1;
	}

	io_uring_free_buf_ring(&ring, br, 1, BUF_BGID);
	io_uring_queue_exit(&ring);
	free(buf);
	close(fds[0]);
	return 0;
}

/*
 * Test BUF_MORE with a regular file (non-pollable fd). Exercises the
 * early commit path where io_should_commit() returns true.
 */
static int test_file(void)
{
	struct io_uring_buf_ring *br;
	struct io_uring ring;
	char fname[64];
	int ret, fd, i;
	char *buf;

	sprintf(fname, ".read-inc-buf-more.%d", getpid());

	fd = open(fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
	if (fd < 0) {
		perror("open");
		return 1;
	}
	for (i = 0; i < 4; i++) {
		char tmp[READ_SIZE];

		memset(tmp, 'a' + i, sizeof(tmp));
		ret = write(fd, tmp, sizeof(tmp));
		if (ret != sizeof(tmp)) {
			perror("write");
			unlink(fname);
			return 1;
		}
	}
	close(fd);

	fd = open(fname, O_RDONLY);
	if (fd < 0) {
		perror("open read");
		unlink(fname);
		return 1;
	}

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

	if (posix_memalign((void **) &buf, 4096, BUF_SIZE)) {
		unlink(fname);
		return 1;
	}

	br = io_uring_setup_buf_ring(&ring, 1, BUF_BGID, IOU_PBUF_RING_INC,
				     &ret);
	if (!br) {
		if (ret == -EINVAL) {
			no_buf_ring_inc = 1;
			free(buf);
			close(fd);
			io_uring_queue_exit(&ring);
			unlink(fname);
			return 0;
		}
		fprintf(stderr, "buffer ring register failed: %d\n", ret);
		unlink(fname);
		return 1;
	}

	io_uring_buf_ring_add(br, buf, BUF_SIZE, BUF_BID, 0, 0);
	io_uring_buf_ring_advance(br, 1);

	memset(buf, 0, BUF_SIZE);

	/* Read 4 chunks - each should have BUF_MORE since buffer is 256
	 * bytes and each read is 32 bytes */
	for (i = 0; i < 4; i++) {
		ret = do_read(&ring, fd);
		if (ret < 0) {
			fprintf(stderr, "file read %d failed\n", i);
			goto err;
		}
		if (ret != READ_SIZE) {
			fprintf(stderr, "file short read %d: %d\n", i, ret);
			goto err;
		}
	}

	io_uring_free_buf_ring(&ring, br, 1, BUF_BGID);
	io_uring_queue_exit(&ring);
	free(buf);
	close(fd);
	unlink(fname);
	return 0;
err:
	unlink(fname);
	return 1;
}

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

	if (argc > 1)
		return T_EXIT_SKIP;

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

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

	return T_EXIT_PASS;
}