Skip to main content

kevy_uring/
layout.rs

1//! `#[repr(C)]` wire-layout structs the kernel reads/writes through the
2//! shared mmap regions. These are the io_uring ABI definitions translated
3//! from `<linux/io_uring.h>`.
4
5/// `struct io_sqring_offsets` — byte offsets of each SQ cursor inside the SQ
6/// ring mapping, returned by `io_uring_setup`.
7#[repr(C)]
8#[derive(Default)]
9pub struct IoSqringOffsets {
10    pub head: u32,
11    pub tail: u32,
12    pub ring_mask: u32,
13    pub ring_entries: u32,
14    pub flags: u32,
15    pub dropped: u32,
16    pub array: u32,
17    pub resv1: u32,
18    pub resv2: u64,
19}
20
21/// `struct io_cqring_offsets` — byte offsets of each CQ cursor inside the CQ
22/// ring mapping.
23#[repr(C)]
24#[derive(Default)]
25pub struct IoCqringOffsets {
26    pub head: u32,
27    pub tail: u32,
28    pub ring_mask: u32,
29    pub ring_entries: u32,
30    pub overflow: u32,
31    pub cqes: u32,
32    pub flags: u32,
33    pub resv1: u32,
34    pub resv2: u64,
35}
36
37/// `struct io_uring_params` — `io_uring_setup`'s in/out parameter.
38#[repr(C)]
39#[derive(Default)]
40pub struct IoUringParams {
41    pub sq_entries: u32,
42    pub cq_entries: u32,
43    pub flags: u32,
44    pub sq_thread_cpu: u32,
45    pub sq_thread_idle: u32,
46    pub features: u32,
47    pub wq_fd: u32,
48    pub resv: [u32; 3],
49    pub sq_off: IoSqringOffsets,
50    pub cq_off: IoCqringOffsets,
51}
52
53/// `struct io_uring_sqe` — the 64-byte submission entry.
54#[repr(C)]
55pub struct IoUringSqe {
56    pub opcode: u8,
57    pub flags: u8,
58    pub ioprio: u16,
59    pub fd: i32,
60    pub off: u64,
61    pub addr: u64,
62    pub len: u32,
63    pub rw_flags: u32,
64    pub user_data: u64,
65    pub buf_index: u16,
66    pub personality: u16,
67    pub splice_fd_in: i32,
68    pub addr3: u64,
69    pub __pad2: u64,
70}
71
72impl IoUringSqe {
73    /// A zeroed SQE with the common fields set. Op-specific fields (e.g.
74    /// `rw_flags` for accept flags) are tweaked by the caller afterward.
75    pub fn new(opcode: u8, fd: i32, addr: u64, len: u32, user_data: u64) -> IoUringSqe {
76        IoUringSqe {
77            opcode,
78            flags: 0,
79            ioprio: 0,
80            fd,
81            off: 0,
82            addr,
83            len,
84            rw_flags: 0,
85            user_data,
86            buf_index: 0,
87            personality: 0,
88            splice_fd_in: 0,
89            addr3: 0,
90            __pad2: 0,
91        }
92    }
93}
94
95/// `struct __kernel_timespec` — the timeout payload an `IORING_OP_TIMEOUT`
96/// SQE points at (always 64-bit fields, independent of the C `time_t` width).
97#[repr(C)]
98#[derive(Default)]
99pub struct KernelTimespec {
100    pub tv_sec: i64,
101    pub tv_nsec: i64,
102}
103
104impl KernelTimespec {
105    /// A relative timeout of `ms` milliseconds.
106    pub fn from_millis(ms: u64) -> KernelTimespec {
107        KernelTimespec {
108            tv_sec: (ms / 1000) as i64,
109            tv_nsec: ((ms % 1000) * 1_000_000) as i64,
110        }
111    }
112
113    /// A relative timeout of `us` microseconds — for sub-ms nap timers
114    /// (io_uring reactor's bounded nap; the previous `thread::sleep`
115    /// version was wake-deaf and broke -c1 Rust-client latency).
116    pub fn from_micros(us: u64) -> KernelTimespec {
117        KernelTimespec {
118            tv_sec: (us / 1_000_000) as i64,
119            tv_nsec: ((us % 1_000_000) * 1_000) as i64,
120        }
121    }
122}
123
124/// `struct io_uring_buf_reg` — `io_uring_register(IORING_REGISTER_PBUF_RING,
125/// …)`'s argument layout.
126#[repr(C)]
127pub struct IoUringBufReg {
128    pub ring_addr: u64,
129    pub ring_entries: u32,
130    pub bgid: u16,
131    pub pad: u16,
132    pub resv: [u64; 3],
133}
134
135/// `struct io_uring_files_update` — `io_uring_register(IORING_REGISTER_FILES_UPDATE,
136/// …)`'s argument layout. Updates a contiguous range of `len` slots starting
137/// at `offset` in the registered files table; `fds` points to `len` i32 fds
138/// (use -1 to clear a slot).
139#[repr(C)]
140pub struct IoUringFilesUpdate {
141    pub offset: u32,
142    pub resv: u32,
143    pub fds: u64,
144}
145
146/// `struct io_uring_rsrc_update` — kernel `struct io_uring_rsrc_update`,
147/// the argument layout for [`crate::ffi::IORING_REGISTER_RING_FDS`]. The
148/// caller fills `data` with the raw ring fd to register; the kernel writes
149/// the assigned index back into `offset`. `nr_args` in the syscall is the
150/// number of entries in the array (we register one at a time).
151#[repr(C)]
152#[derive(Default)]
153pub struct IoUringRsrcUpdate {
154    pub offset: u32,
155    pub resv: u32,
156    pub data: u64,
157}
158
159/// `struct io_uring_rsrc_register` — `io_uring_register(IORING_REGISTER_FILES2,
160/// …)`'s argument layout. With `flags = IORING_RSRC_REGISTER_SPARSE` and
161/// `nr = slot_count` it registers an empty registered-files table of that
162/// size. `data`/`tags` are unused for sparse registration. `nr_args` in the
163/// syscall must equal `sizeof::<Self>()` = 32.
164#[repr(C)]
165#[derive(Default)]
166pub struct IoUringRsrcRegister {
167    pub nr: u32,
168    pub flags: u32,
169    pub resv2: u64,
170    pub data: u64,
171    pub tags: u64,
172}
173
174/// `IORING_RSRC_REGISTER_SPARSE` — tells the kernel "no initial fds, just
175/// give me an empty table of size `nr`."
176pub const IORING_RSRC_REGISTER_SPARSE: u32 = 1 << 0;