Documentation
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use core::ops::BitOr;

// Syscall numbers (x86_64)
#[repr(i64)]
pub enum Syscall {
    IoUringSetup = 425,
    IoUringEnter = 426,
    IoUringRegister = 427,
}

// io_uring opcodes
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OpCode {
    Nop = 0,
    Readv = 1,
    Writev = 2,
    Fsync = 3,
    ReadFixed = 4,
    WriteFixed = 5,
    PollAdd = 6,
    PollRemove = 7,
    SyncFileRange = 8,
    Sendmsg = 9,
    Recvmsg = 10,
    Timeout = 11,
    TimeoutRemove = 12,
    Accept = 13,
    AsyncCancel = 14,
    LinkTimeout = 15,
    Connect = 16,
    Fallocate = 17,
    Openat = 18,
    Close = 19,
    FilesUpdate = 20,
    Statx = 21,
    Read = 22,
    Write = 23,
    Fadvise = 24,
    Madvise = 25,
    Send = 26,
    Recv = 27,
    Openat2 = 28,
    EpollCtl = 29,
    Splice = 30,
    ProvideBuffers = 31,
    RemoveBuffers = 32,
    Tee = 33,
    Shutdown = 34,
    Renameat = 35,
    Unlinkat = 36,
    Mkdirat = 37,
    Symlinkat = 38,
    Linkat = 39,
    MsgRing = 40,
    Fsetxattr = 41,
    Setxattr = 42,
    Fgetxattr = 43,
    Getxattr = 44,
    Socket = 45,
    UringCmd = 46,
    SendZc = 47,
    SendMsgZc = 48,
}

// io_uring_setup flags
#[repr(u32)]
#[derive(Debug, Clone, Copy)]
pub enum Setup {
    IoPoll = 1 << 0,
    SqPoll = 1 << 1,
    SqAff = 1 << 2,
    CqSize = 1 << 3,
    Clamp = 1 << 4,
    AttachWq = 1 << 5,
    RDisabled = 1 << 6,
}

impl Setup {
    pub const fn as_u32(self) -> u32 {
        self as u32
    }

    pub const fn combine(self, other: Self) -> u32 {
        self as u32 | other as u32
    }
}

// io_uring_enter flags
#[repr(u32)]
#[derive(Debug, Clone, Copy)]
pub enum Enter {
    GetEvents = 1 << 0,
    SqWakeup = 1 << 1,
    SqWait = 1 << 2,
    ExtArg = 1 << 3,
}

impl Enter {
    pub const fn as_u32(self) -> u32 {
        self as u32
    }

    pub const fn combine(self, other: Self) -> u32 {
        self as u32 | other as u32
    }
}

// io_uring features
#[repr(u32)]
#[derive(Debug, Clone, Copy)]
pub enum Features {
    SingleMmap = 1 << 0,
    NoDrop = 1 << 1,
    SubmitStable = 1 << 2,
    RwCurPos = 1 << 3,
    CurPersonality = 1 << 4,
    FastPoll = 1 << 5,
    Poll32Bits = 1 << 6,
    SqPollNonFixed = 1 << 7,
    ExtArg = 1 << 8,
    NativeWorkers = 1 << 9,
    RsrcTags = 1 << 10,
}

impl Features {
    pub fn is_set(self, features: u32) -> bool {
        features & (self as u32) != 0
    }
}

// mmap offsets
#[repr(u64)]
#[derive(Debug, Clone, Copy)]
pub enum MmapOffset {
    SqRing = 0,
    CqRing = 0x8000000,
    Sqes = 0x10000000,
}

impl MmapOffset {
    pub const fn as_u64(self) -> u64 {
        self as u64
    }
}

// SQE flags
#[repr(u8)]
#[derive(Debug, Clone, Copy)]
pub enum Submit {
    FixedFile = 1 << 0,
    IoDrain = 1 << 1,
    IoLink = 1 << 2,
    IoHardlink = 1 << 3,
    Async = 1 << 4,
    BufferSelect = 1 << 5,
    CqeSkipSuccess = 1 << 6,
}

// Extended SQE flags (sqe->ioprio field for accept)
#[repr(u16)]
#[derive(Debug, Clone, Copy)]
pub enum SubmitExt {
    Multishot = 1 << 0,
}

impl Submit {
    pub const fn as_u8(self) -> u8 {
        self as u8
    }

    pub const fn combine(self, other: Self) -> u8 {
        self as u8 | other as u8
    }
}

// CQE flags
#[repr(u32)]
#[derive(Debug, Clone, Copy)]
pub enum Complete {
    Buffer = 1 << 0,
    More = 1 << 1,
    SockNonempty = 1 << 2,
    Notif = 1 << 3,
}

impl Complete {
    pub fn is_set(self, flags: u32) -> bool {
        flags & (self as u32) != 0
    }
}

// Accept flags
#[repr(u32)]
#[derive(Debug, Clone, Copy)]
pub enum AcceptFlag {
    NonBlock = 0x800,
    CloExec = 0x80000,
}

impl AcceptFlag {
    pub const fn as_u32(self) -> u32 {
        self as u32
    }
}

// Socket domains
#[repr(i32)]
#[derive(Debug, Clone, Copy)]
pub enum SocketDomain {
    Unix = 1,
    Inet = 2,
    Inet6 = 10,
}

// Socket types
#[repr(i32)]
#[derive(Debug, Clone, Copy)]
pub enum SocketType {
    Stream = 1,
    Dgram = 2,
    Raw = 3,
    Rdm = 4,
    SeqPacket = 5,
}

impl SocketType {
    pub const fn with_flags(self, flags: i32) -> i32 {
        self as i32 | flags
    }
}

// Socket flags
pub const SOCK_NONBLOCK: i32 = 0x800;
pub const SOCK_CLOEXEC: i32 = 0x80000;

// Register opcodes
#[repr(u32)]
#[derive(Debug, Clone, Copy)]
pub enum Register {
    Buffers = 0,
    UnregisterBuffers = 1,
    Files = 2,
    UnregisterFiles = 3,
    EventFd = 4,
    UnregisterEventFd = 5,
    FilesUpdate = 6,
    EventFdAsync = 7,
    Probe = 8,
    Personality = 9,
    UnregisterPersonality = 10,
    Restrictions = 11,
    EnableRings = 12,
}

// Memory protection flags
#[repr(i32)]
#[derive(Debug, Clone, Copy)]
pub enum Prot {
    None = 0x0,
    Read = 0x1,
    Write = 0x2,
    Exec = 0x4,
}

impl Prot {
    pub const fn as_i32(self) -> i32 {
        self as i32
    }
}

impl BitOr for Prot {
    type Output = i32;
    fn bitor(self, rhs: Self) -> Self::Output {
        self as i32 | rhs as i32
    }
}

// Memory mapping flags
#[repr(i32)]
#[derive(Debug, Clone, Copy)]
pub enum Map {
    Shared = 0x01,
    Private = 0x02,
    Fixed = 0x10,
    Anonymous = 0x20,
    Populate = 0x8000,
}

impl Map {
    pub const fn as_i32(self) -> i32 {
        self as i32
    }
}
impl BitOr for Map {
    type Output = i32;
    fn bitor(self, rhs: Self) -> Self::Output {
        self as i32 | rhs as i32
    }
}
pub const MAP_FAILED: *mut core::ffi::c_void = !0 as *mut core::ffi::c_void;

// Memory advice flags
#[repr(i32)]
#[derive(Debug, Clone, Copy)]
pub enum Madvise {
    Normal = 0,
    Random = 1,
    Sequential = 2,
    WillNeed = 3,
    DontNeed = 4,
    Remove = 9,
    DontFork = 10,
    DoFork = 11,
    Mergeable = 12,
    Unmergeable = 13,
    HugePage = 14,
    NoHugePage = 15,
    DontDump = 16,
    DoDump = 17,
    HwPoison = 100,
}

// Structures remain the same...
#[repr(C)]
pub struct Sqe {
    pub opcode: u8, // Use OpCode enum values
    pub flags: u8,  // Use Sqe enum values
    pub ioprio: u16,
    pub fd: i32,
    pub off: u64,
    pub addr: u64,
    pub len: u32,
    pub rw_flags: u32,
    pub user_data: u64,
    pub buf_index: u16,
    pub personality: u16,
    pub splice_fd_in: i32,
    pub __pad2: [u64; 2],
}

impl Sqe {
    pub fn set_opcode(&mut self, op: OpCode) {
        self.opcode = op as u8;
    }

    pub fn set_flags(&mut self, flags: u8) {
        self.flags = flags;
    }

    pub fn add_flag(&mut self, flag: Submit) {
        self.flags |= flag as u8;
    }
}

#[repr(C)]
pub struct Cqe {
    pub user_data: u64,
    pub res: i32,
    pub flags: u32, // Use Cqe enum to check
}

impl Cqe {
    pub fn has_flag(&self, flag: Complete) -> bool {
        self.flags & (flag as u32) != 0
    }
}

#[repr(C)]
pub struct SqringOffsets {
    pub head: u32,
    pub tail: u32,
    pub ring_mask: u32,
    pub ring_entries: u32,
    pub flags: u32,
    pub dropped: u32,
    pub array: u32,
    pub resv1: u32,
    pub resv2: u64,
}

#[repr(C)]
pub struct CqringOffsets {
    pub head: u32,
    pub tail: u32,
    pub ring_mask: u32,
    pub ring_entries: u32,
    pub overflow: u32,
    pub cqes: u32,
    pub flags: u32,
    pub resv1: u32,
    pub resv2: u64,
}

#[repr(C)]
pub struct Params {
    pub sq_entries: u32,
    pub cq_entries: u32,
    pub flags: u32, // Use Setup enum values
    pub sq_thread_cpu: u32,
    pub sq_thread_idle: u32,
    pub features: u32, // Use Features enum to check
    pub wq_fd: u32,
    pub resv: [u32; 3],
    pub sq_off: SqringOffsets,
    pub cq_off: CqringOffsets,
}

impl Params {
    pub fn has_feature(&self, feature: Features) -> bool {
        self.features & (feature as u32) != 0
    }
}