aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/io_uring_enter.c
blob: 7c73d0a403fb0742e378a09117169a33d24c9892 (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
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
 * 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 "helpers.h"
#include "liburing.h"
#include "liburing/barrier.h"
#include "../src/syscall.h"

#define IORING_MAX_ENTRIES 4096
#define IORING_MAX_ENTRIES_FALLBACK 128

static 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 >= 0) {
		fprintf(stderr, "expected %s, but call succeeded\n", strerror(-error));
		return 1;
	}

	if (ret != error) {
		fprintf(stderr, "expected %d, got %d\n", error, ret);
		return 1;
	}

	return 0;
}

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

	if (expect < 0)
		return expect_fail(fd, to_submit, min_complete, flags, sig,
				   expect);

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

	return 0;
}

/*
 * prep a read I/O.  index is treated like a block number.
 */
static int setup_file(char *template, off_t len)
{
	int fd, ret;
	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) {
		fprintf(stderr, "read returned %d, expected 4096\n", ret);
		exit(1);
	}

	return fd;
}

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

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

	iov->iov_base = t_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
}

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

	gettimeofday(&start, NULL);
	while (left) {
		ret = io_uring_wait_cqe(ring, &cqe);
		if (ret < 0) {
			fprintf(stderr, "io_uring_wait_cqe returned %d\n", ret);
			exit(1);
		}
		if (cqe->res != 4096)
			fprintf(stderr, "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) {
			fprintf(stderr, "Timed out waiting for I/Os to complete.\n");
			fprintf(stderr, "%u expected, %u completed\n", nr, left);
			break;
		}
	}
}

static void submit_io(struct io_uring *ring, unsigned nr)
{
	int fd, ret;
	off_t file_len;
	unsigned i;
	static char template[32] = "/tmp/io_uring_enter-test.XXXXXX";
	struct io_uring_sqe *sqe;

	file_len = nr * 4096;
	fd = setup_file(template, 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 */
	ret = io_uring_submit(ring);
	unlink(template);
	if (ret < 0) {
		perror("io_uring_enter");
		exit(1);
	}
}

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;
	struct io_uring_params p;

	if (argc > 1)
		return T_EXIT_SKIP;

	memset(&p, 0, sizeof(p));
	ret = t_io_uring_init_sqarray(IORING_MAX_ENTRIES, &ring, &p);
	if (ret == -ENOMEM)
		ret = t_io_uring_init_sqarray(IORING_MAX_ENTRIES_FALLBACK,
					      &ring, &p);
	if (ret < 0) {
		perror("io_uring_queue_init");
		exit(T_EXIT_FAIL);
	}
	mask = sq->ring_mask;

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

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

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

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

	/* fill the sq ring */
	sq_entries = ring.sq.ring_entries;
	submit_io(&ring, sq_entries);
	ret = io_uring_enter(ring.ring_fd, 0, sq_entries,
			     IORING_ENTER_GETEVENTS, NULL);
	if (ret < 0) {
		fprintf(stderr, "io_uring_enter: %s\n", strerror(-ret));
		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) {
			fprintf(stderr, "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.
	 */
	index = sq->ring_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) {
		fprintf(stderr, "dropped counter did not increase\n");
		status = 1;
	}

	if (!status)
		return T_EXIT_PASS;

	fprintf(stderr, "FAIL\n");
	return T_EXIT_FAIL;
}