1use core::{
2 mem,
3 ops::{Deref, DerefMut},
4 slice,
5};
6
7use bitflags::bitflags;
8
9#[repr(C)]
10#[derive(Clone, Copy, Debug, Default)]
11pub struct Sqe {
12 pub opcode: u8,
13 pub sqe_flags: SqeFlags,
14 pub _rsvd: u16, pub tag: u32,
16 pub args: [u64; 6],
17 pub caller: u64,
18}
19impl Deref for Sqe {
20 type Target = [u8];
21 fn deref(&self) -> &[u8] {
22 unsafe { slice::from_raw_parts(self as *const Sqe as *const u8, mem::size_of::<Sqe>()) }
23 }
24}
25
26impl DerefMut for Sqe {
27 fn deref_mut(&mut self) -> &mut [u8] {
28 unsafe { slice::from_raw_parts_mut(self as *mut Sqe as *mut u8, mem::size_of::<Sqe>()) }
29 }
30}
31
32bitflags! {
33 #[derive(Clone, Copy, Debug, Default)]
34 pub struct SqeFlags: u8 {
35 const ONEWAY = 1;
38 }
39}
40
41#[repr(C)]
42#[derive(Clone, Copy, Debug, Default)]
43pub struct Cqe {
44 pub flags: u8, pub extra_raw: [u8; 3],
46 pub tag: u32,
47 pub result: u64,
48}
49impl Deref for Cqe {
50 type Target = [u8];
51 fn deref(&self) -> &[u8] {
52 unsafe { slice::from_raw_parts(self as *const Cqe as *const u8, mem::size_of::<Cqe>()) }
53 }
54}
55
56impl DerefMut for Cqe {
57 fn deref_mut(&mut self) -> &mut [u8] {
58 unsafe { slice::from_raw_parts_mut(self as *mut Cqe as *mut u8, mem::size_of::<Cqe>()) }
59 }
60}
61
62bitflags! {
63 #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
64 pub struct NewFdFlags: u8 {
65 const POSITIONED = 1;
66 }
67}
68
69impl Cqe {
70 pub fn extra(&self) -> u32 {
71 u32::from_ne_bytes([self.extra_raw[0], self.extra_raw[1], self.extra_raw[2], 0])
72 }
73}
74
75#[repr(u8)]
76#[derive(Clone, Copy, Debug, Eq, PartialEq)]
77pub enum CqeOpcode {
78 RespondRegular,
79 RespondWithFd,
80 SendFevent, ObtainFd,
82 }
84impl CqeOpcode {
85 pub fn try_from_raw(raw: u8) -> Option<Self> {
86 Some(match raw {
87 0 => Self::RespondRegular,
88 1 => Self::RespondWithFd,
89 2 => Self::SendFevent,
90 3 => Self::ObtainFd,
91 _ => return None,
92 })
93 }
94}
95
96#[repr(u8)]
97#[non_exhaustive]
98#[derive(Clone, Copy, Debug)]
99pub enum Opcode {
100 Open = 0, Rmdir = 1, Unlink = 2, Close = 3, Dup = 4, Read = 5, Write = 6, Fsize = 7, Fchmod = 8, Fchown = 9, Fcntl = 10, Fevent = 11, Sendfd = 12,
113 Fpath = 13, Frename = 14,
115 Fstat = 15, Fstatvfs = 16, Fsync = 17, Ftruncate = 18, Futimens = 19, MmapPrep = 20,
122 RequestMmap = 21,
123 Mremap = 22,
124 Munmap = 23,
125 Msync = 24, Cancel = 25, Getdents = 26,
130 CloseMsg = 27,
131}
132
133impl Opcode {
134 pub fn try_from_raw(raw: u8) -> Option<Self> {
135 use Opcode::*;
136
137 Some(match raw {
139 0 => Open,
140 1 => Rmdir,
141 2 => Unlink,
142 3 => Close,
143 4 => Dup,
144 5 => Read,
145 6 => Write,
146 7 => Fsize,
147 8 => Fchmod,
148 9 => Fchown,
149 10 => Fcntl,
150 11 => Fevent,
151 12 => Sendfd,
152 13 => Fpath,
153 14 => Frename,
154 15 => Fstat,
155 16 => Fstatvfs,
156 17 => Fsync,
157 18 => Ftruncate,
158 19 => Futimens,
159
160 20 => MmapPrep,
161 21 => RequestMmap,
162 22 => Mremap,
163 23 => Munmap,
164 24 => Msync,
165
166 25 => Cancel,
167 26 => Getdents,
168 27 => CloseMsg,
169
170 _ => return None,
171 })
172 }
173}