summaryrefslogtreecommitdiff
path: root/test/io_uring_enter.c
blob: 8190178bee65c51bc783339d41278e56b30e6fcb (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
281
282
283
284
285
/*
 * io_uring_enter.c
 *
 * Description: Unit tests for the io_uring_enter system call.
 *
 * Copyright 2019, Red Hat, Inc.
 * Author: Jeff Moyer <jmoyer@redhat.com>
 */
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/sysinfo.h>
#include <poll.h>
#include <assert.h>
#include <sys/uio.h>
#include <sys/mman.h>
#include <linux/mman.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <limits.h>
#include <sys/time.h>
#include "liburing.h"
#include "liburing/barrier.h"

#define IORING_MAX_ENTRIES 4096

int
expect_failed_submit(struct io_uring *ring, int error)
{
	int ret;

	ret = io_uring_submit(ring);
	if (ret == 1) {
		printf("expected failure, but io_uring_submit succeeded.\n");
		return 1;
	}

	if (errno != error) {
		printf("expected %d, got %d\n", error, errno);
		return 1;
	}

	return 0;
}

int
expect_fail(int fd, unsigned int to_submit, unsigned int min_complete,
	    unsigned int flags, sigset_t *sig, int error)
{
	int ret;

	ret = io_uring_enter(fd, to_submit, min_complete, flags, sig);
	if (ret != -1) {
		printf("expected %s, but call succeeded\n", strerror(error));
		return 1;
	}

	if (errno != error) {
		printf("expected %d, got %d\n", error, errno);
		return 1;
	}

	return 0;
}

int
try_io_uring_enter(int fd, unsigned int to_submit, unsigned int min_complete,
		   unsigned int flags, sigset_t *sig, int expect, int error)
{
	int ret;

	printf("io_uring_enter(%d, %u, %u, %u, %p)\n", fd, to_submit,
	       min_complete, flags, sig);

	if (expect == -1)
		return expect_fail(fd, to_submit, min_complete,
				   flags, sig, error);

	ret = io_uring_enter(fd, to_submit, min_complete, flags, sig);
	if (ret != expect) {
		printf("Expected %d, got %d\n", expect, errno);
		return 1;
	}

	return 0;
}

/*
 * prep a read I/O.  index is treated like a block number.
 */
int
setup_file(off_t len)
{
	int fd, ret;
	static char template[32] = "/tmp/io_uring_enter-test.XXXXXX";
	char buf[4096];

	fd = mkstemp(template);
	if (fd < 0) {
		perror("mkstemp");
		exit(1);
	}
	ret = ftruncate(fd, len);
	if (ret < 0) {
		perror("ftruncate");
		exit(1);
	}

	ret = read(fd, buf, 4096);
	if (ret != 4096) {
		printf("read returned %d, expected 4096\n", ret);
		exit(1);
	}

	return fd;
}

void
io_prep_read(struct io_uring_sqe *sqe, int fd, off_t offset, size_t len)
{
	struct iovec *iov;

	iov = malloc(sizeof(*iov));
	assert(iov);

	iov->iov_base = malloc(len);
	assert(iov->iov_base);
	iov->iov_len = len;

	io_uring_prep_readv(sqe, fd, iov, 1, offset);
	io_uring_sqe_set_data(sqe, iov); // free on completion
}

void
reap_events(struct io_uring *ring, unsigned nr)
{
	int ret;
	unsigned left = nr;
	struct io_uring_cqe *cqe;
	struct iovec *iov;
	struct timeval start, now, elapsed;

	printf("Reaping %u I/Os\n", nr);
	gettimeofday(&start, NULL);
	while (left) {
		ret = io_uring_wait_cqe(ring, &cqe);
		if (ret < 0) {
			printf("io_uring_wait_cqe returned %d\n", ret);
			printf("expected success\n");
			exit(1);
		}
		if (cqe->res != 4096)
			printf("cqe->res: %d, expected 4096\n", cqe->res);
		iov = io_uring_cqe_get_data(cqe);
		free(iov->iov_base);
		free(iov);
		left--;
		io_uring_cqe_seen(ring, cqe);

		gettimeofday(&now, NULL);
		timersub(&now, &start, &elapsed);
		if (elapsed.tv_sec > 10) {
			printf("Timed out waiting for I/Os to complete.\n");
			printf("%u expected, %u completed\n", nr, left);
			break;
		}
	}
}

void
submit_io(struct io_uring *ring, unsigned nr)
{
	int fd, ret;
	off_t file_len;
	unsigned i;
	struct io_uring_sqe *sqe;

	printf("Allocating %u sqes\n", nr);
	file_len = nr * 4096;
	fd = setup_file(file_len);
	for (i = 0; i < nr; i++) {
		/* allocate an sqe */
		sqe = io_uring_get_sqe(ring);
		/* fill it in */
		io_prep_read(sqe, fd, i * 4096, 4096);
	}

	/* submit the I/Os */
	printf("Submitting %u I/Os\n", nr);
	ret = io_uring_submit(ring);
	if (ret < 0) {
		perror("io_uring_enter");
		exit(1);
	}
	printf("Done\n");
}

int
main(int argc, char **argv)
{
	int ret;
	unsigned int status = 0;
	struct io_uring ring;
	struct io_uring_sq *sq = &ring.sq;
	unsigned ktail, mask, index;
	unsigned sq_entries;
	unsigned completed, dropped;

	ret = io_uring_queue_init(IORING_MAX_ENTRIES, &ring, 0);
	if (ret < 0) {
		perror("io_uring_queue_init");
		exit(1);
	}
	mask = *sq->kring_mask;

	/* invalid flags */
	status |= try_io_uring_enter(ring.ring_fd, 1, 0, ~0U, NULL, -1, EINVAL);

	/* invalid fd, EBADF */
	status |= try_io_uring_enter(-1, 0, 0, 0, NULL, -1, EBADF);

	/* valid, non-ring fd, EOPNOTSUPP */
	status |= try_io_uring_enter(0, 0, 0, 0, NULL, -1, EOPNOTSUPP);

	/* to_submit: 0, flags: 0;  should get back 0. */
	status |= try_io_uring_enter(ring.ring_fd, 1, 0, 0, NULL, 0, 0);

	/* fill the sq ring */
	sq_entries = *ring.sq.kring_entries;
	submit_io(&ring, sq_entries);
	printf("Waiting for %u events\n", sq_entries);
	ret = io_uring_enter(ring.ring_fd, 0, sq_entries,
			     IORING_ENTER_GETEVENTS, NULL);
	if (ret < 0) {
		perror("io_uring_enter");
		status = 1;
	} else {
		/*
		 * This is a non-IOPOLL ring, which means that io_uring_enter
		 * should not return until min_complete events are available
		 * in the completion queue.
		 */
		completed = *ring.cq.ktail - *ring.cq.khead;
		if (completed != sq_entries) {
			printf("Submitted %u I/Os, but only got %u completions\n",
			       sq_entries, completed);
			status = 1;
		}
		reap_events(&ring, sq_entries);
	}

	/*
	 * Add an invalid index to the submission queue.  This should
	 * result in the dropped counter increasing.
	 */
	printf("Submitting invalid sqe index.\n");
	index = *sq->kring_entries + 1; // invalid index
	dropped = *sq->kdropped;
	ktail = *sq->ktail;
	sq->array[ktail & mask] = index;
	++ktail;
	/*
	 * Ensure that the kernel sees the SQE update before it sees the tail
	 * update.
	 */
	io_uring_smp_store_release(sq->ktail, ktail);

	ret = io_uring_enter(ring.ring_fd, 1, 0, 0, NULL);
	/* now check to see if our sqe was dropped */
	if (*sq->kdropped == dropped) {
		printf("dropped counter did not increase\n");
		status = 1;
	}

	if (!status) {
		printf("PASS\n");
		return 0;
	}

	printf("FAIL\n");
	return -1;
}