xref: /aosp_15_r20/external/ltp/include/lapi/io_uring.h (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2020 ARM. All rights reserved.
4  * Copyright (c) 2020 Petr Vorel <[email protected]>
5  *
6  * Mostly copied/adapted from <linux/io_uring.h>
7  */
8 
9 #ifndef LAPI_IO_URING_H__
10 #define LAPI_IO_URING_H__
11 
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <sys/uio.h>
15 #include <stdlib.h>
16 #include <linux/types.h>
17 
18 #include "lapi/syscalls.h"
19 
20 #ifdef HAVE_LINUX_IO_URING_H
21 #include <linux/io_uring.h>
22 #endif
23 
24 #ifndef IOSQE_FIXED_FILE
25 
26 #ifndef __kernel_rwf_t
27 typedef int __kernel_rwf_t;
28 #endif
29 
30 /*
31  * IO submission data structure (Submission Queue Entry)
32  */
33 struct io_uring_sqe {
34 	uint8_t	opcode;		/* type of operation for this sqe */
35 	uint8_t	flags;		/* IOSQE_ flags */
36 	uint16_t	ioprio;		/* ioprio for the request */
37 	int32_t	fd;		/* file descriptor to do IO on */
38 	union {
39 		uint64_t	off;	/* offset into file */
40 		uint64_t	addr2;
41 	};
42 	uint64_t	addr;		/* pointer to buffer or iovecs */
43 	uint32_t	len;		/* buffer size or number of iovecs */
44 	union {
45 		__kernel_rwf_t	rw_flags;
46 		uint32_t		fsync_flags;
47 		uint16_t		poll_events;
48 		uint32_t		sync_range_flags;
49 		uint32_t		msg_flags;
50 		uint32_t		timeout_flags;
51 		uint32_t		accept_flags;
52 		uint32_t		cancel_flags;
53 		uint32_t		open_flags;
54 		uint32_t		statx_flags;
55 		uint32_t		fadvise_advice;
56 	};
57 	uint64_t	user_data;	/* data to be passed back at completion time */
58 	union {
59 		struct {
60 			/* index into fixed buffers, if used */
61 			uint16_t	buf_index;
62 			/* personality to use, if used */
63 			uint16_t	personality;
64 		};
65 		uint64_t	__pad2[3];
66 	};
67 };
68 
69 enum {
70 	IOSQE_FIXED_FILE_BIT,
71 	IOSQE_IO_DRAIN_BIT,
72 	IOSQE_IO_LINK_BIT,
73 };
74 
75 /*
76  * sqe->flags
77  */
78 /* use fixed fileset */
79 #define IOSQE_FIXED_FILE	(1U << IOSQE_FIXED_FILE_BIT)
80 /* issue after inflight IO */
81 #define IOSQE_IO_DRAIN		(1U << IOSQE_IO_DRAIN_BIT)
82 /* links next sqe */
83 #define IOSQE_IO_LINK		(1U << IOSQE_IO_LINK_BIT)
84 
85 /*
86  * io_uring_setup() flags
87  */
88 #define IORING_SETUP_IOPOLL	(1U << 0)	/* io_context is polled */
89 #define IORING_SETUP_SQPOLL	(1U << 1)	/* SQ poll thread */
90 #define IORING_SETUP_SQ_AFF	(1U << 2)	/* sq_thread_cpu is valid */
91 #define IORING_SETUP_CQSIZE	(1U << 3)	/* app defines CQ size */
92 #define IORING_SETUP_CLAMP	(1U << 4)	/* clamp SQ/CQ ring sizes */
93 #define IORING_SETUP_ATTACH_WQ	(1U << 5)	/* attach to existing wq */
94 
95 enum {
96 	IORING_OP_NOP,
97 	IORING_OP_READV,
98 	IORING_OP_WRITEV,
99 	IORING_OP_FSYNC,
100 	IORING_OP_READ_FIXED,
101 	IORING_OP_WRITE_FIXED,
102 	IORING_OP_POLL_ADD,
103 	IORING_OP_POLL_REMOVE,
104 	IORING_OP_SYNC_FILE_RANGE,
105 	IORING_OP_SENDMSG,
106 	IORING_OP_RECVMSG,
107 	IORING_OP_TIMEOUT,
108 	IORING_OP_TIMEOUT_REMOVE,
109 	IORING_OP_ACCEPT,
110 	IORING_OP_ASYNC_CANCEL,
111 	IORING_OP_LINK_TIMEOUT,
112 	IORING_OP_CONNECT,
113 	IORING_OP_FALLOCATE,
114 	IORING_OP_OPENAT,
115 	IORING_OP_CLOSE,
116 	IORING_OP_FILES_UPDATE,
117 	IORING_OP_STATX,
118 	IORING_OP_READ,
119 	IORING_OP_WRITE,
120 	IORING_OP_FADVISE,
121 	IORING_OP_MADVISE,
122 	IORING_OP_SEND,
123 	IORING_OP_RECV,
124 	IORING_OP_OPENAT2,
125 	IORING_OP_EPOLL_CTL,
126 
127 	/* this goes last, obviously */
128 	IORING_OP_LAST,
129 };
130 
131 /*
132  * sqe->fsync_flags
133  */
134 #define IORING_FSYNC_DATASYNC	(1U << 0)
135 
136 /*
137  * sqe->timeout_flags
138  */
139 #define IORING_TIMEOUT_ABS	(1U << 0)
140 
141 /*
142  * IO completion data structure (Completion Queue Entry)
143  */
144 struct io_uring_cqe {
145 	uint64_t	user_data;	/* sqe->data submission passed back */
146 	int32_t 	res;		/* result code for this event */
147 	uint32_t	flags;
148 };
149 
150 /*
151  * Magic offsets for the application to mmap the data it needs
152  */
153 #define IORING_OFF_SQ_RING		0ULL
154 #define IORING_OFF_CQ_RING		0x8000000ULL
155 #define IORING_OFF_SQES			0x10000000ULL
156 
157 /*
158  * Filled with the offset for mmap(2)
159  */
160 struct io_sqring_offsets {
161 	uint32_t head;
162 	uint32_t tail;
163 	uint32_t ring_mask;
164 	uint32_t ring_entries;
165 	uint32_t flags;
166 	uint32_t dropped;
167 	uint32_t array;
168 	uint32_t resv1;
169 	uint64_t resv2;
170 };
171 
172 /*
173  * sq_ring->flags
174  */
175 #define IORING_SQ_NEED_WAKEUP	(1U << 0) /* needs io_uring_enter wakeup */
176 
177 struct io_cqring_offsets {
178 	uint32_t head;
179 	uint32_t tail;
180 	uint32_t ring_mask;
181 	uint32_t ring_entries;
182 	uint32_t overflow;
183 	uint32_t cqes;
184 	uint64_t resv[2];
185 };
186 
187 /*
188  * io_uring_enter(2) flags
189  */
190 #define IORING_ENTER_GETEVENTS	(1U << 0)
191 #define IORING_ENTER_SQ_WAKEUP	(1U << 1)
192 
193 /*
194  * Passed in for io_uring_setup(2). Copied back with updated info on success
195  */
196 struct io_uring_params {
197 	uint32_t sq_entries;
198 	uint32_t cq_entries;
199 	uint32_t flags;
200 	uint32_t sq_thread_cpu;
201 	uint32_t sq_thread_idle;
202 	uint32_t features;
203 	uint32_t wq_fd;
204 	uint32_t resv[3];
205 	struct io_sqring_offsets sq_off;
206 	struct io_cqring_offsets cq_off;
207 };
208 
209 /*
210  * io_uring_params->features flags
211  */
212 #define IORING_FEAT_SINGLE_MMAP		(1U << 0)
213 #define IORING_FEAT_NODROP		(1U << 1)
214 #define IORING_FEAT_SUBMIT_STABLE	(1U << 2)
215 #define IORING_FEAT_RW_CUR_POS		(1U << 3)
216 #define IORING_FEAT_CUR_PERSONALITY	(1U << 4)
217 
218 /*
219  * io_uring_register(2) opcodes and arguments
220  */
221 #define IORING_REGISTER_BUFFERS		0
222 #define IORING_UNREGISTER_BUFFERS	1
223 #define IORING_REGISTER_FILES		2
224 #define IORING_UNREGISTER_FILES		3
225 #define IORING_REGISTER_EVENTFD		4
226 #define IORING_UNREGISTER_EVENTFD	5
227 #define IORING_REGISTER_FILES_UPDATE	6
228 #define IORING_REGISTER_EVENTFD_ASYNC	7
229 #define IORING_REGISTER_PROBE		8
230 #define IORING_REGISTER_PERSONALITY	9
231 #define IORING_UNREGISTER_PERSONALITY	10
232 
233 struct io_uring_files_update {
234 	uint32_t offset;
235 	uint32_t resv;
236 	uint64_t __attribute__((aligned(8))) fds;
237 };
238 
239 #define IO_URING_OP_SUPPORTED	(1U << 0)
240 
241 struct io_uring_probe_op {
242 	uint8_t op;
243 	uint8_t resv;
244 	uint16_t flags;	/* IO_URING_OP_* flags */
245 	uint32_t resv2;
246 };
247 
248 struct io_uring_probe {
249 	uint8_t last_op;	/* last opcode supported */
250 	uint8_t ops_len;	/* length of ops[] array below */
251 	uint16_t resv;
252 	uint32_t resv2[3];
253 	struct io_uring_probe_op ops[0];
254 };
255 
256 #endif /* IOSQE_FIXED_FILE */
257 
258 #ifndef IOSQE_IO_HADRLINK
259 /* like LINK, but stronger */
260 #define IOSQE_IO_HARDLINK_BIT	3
261 #define IOSQE_IO_HARDLINK	(1U << IOSQE_IO_HARDLINK_BIT)
262 #endif /* IOSQE_IO_HADRLINK */
263 
264 #ifndef IOSQE_ASYNC
265 /* always go async */
266 #define IOSQE_ASYNC_BIT		4
267 #define IOSQE_ASYNC		(1U << IOSQE_ASYNC_BIT)
268 #endif /* IOSQE_ASYNC */
269 
270 #ifndef HAVE_IO_URING_REGISTER
io_uring_register(int fd,unsigned int opcode,void * arg,unsigned int nr_args)271 static inline int io_uring_register(int fd, unsigned int opcode, void *arg,
272 	unsigned int nr_args)
273 {
274 	return tst_syscall(__NR_io_uring_register, fd, opcode, arg, nr_args);
275 }
276 #endif /* HAVE_IO_URING_REGISTER */
277 
278 
279 #ifndef HAVE_IO_URING_SETUP
io_uring_setup(unsigned int entries,struct io_uring_params * p)280 static inline int io_uring_setup(unsigned int entries,
281 	struct io_uring_params *p)
282 {
283 	return tst_syscall(__NR_io_uring_setup, entries, p);
284 }
285 #endif /* HAVE_IO_URING_SETUP */
286 
287 #ifndef HAVE_IO_URING_ENTER
io_uring_enter(int fd,unsigned int to_submit,unsigned int min_complete,unsigned int flags,sigset_t * sig)288 static inline int io_uring_enter(int fd, unsigned int to_submit,
289 	unsigned int min_complete, unsigned int flags, sigset_t *sig)
290 {
291 	return tst_syscall(__NR_io_uring_enter, fd, to_submit, min_complete,
292 			flags, sig, _NSIG / 8);
293 }
294 #endif /* HAVE_IO_URING_ENTER */
295 
io_uring_setup_supported_by_kernel(void)296 static inline void io_uring_setup_supported_by_kernel(void)
297 {
298 	long ret;
299 	ret = syscall(__NR_io_uring_setup, NULL, 0);
300 	if (ret != -1) {
301 		SAFE_CLOSE(ret);
302 		return;
303 	}
304 
305 	if (errno == ENOSYS) {
306 		if ((tst_kvercmp(5, 1, 0)) < 0) {
307 			tst_brk(TCONF,
308 				"Test not supported on kernel version < v5.1");
309 		}
310 		tst_brk(TCONF, "CONFIG_IO_URING not set?");
311 	}
312 }
313 
314 #endif /* LAPI_IO_URING_H__ */
315