1use core::{
2 mem,
3 ops::{Deref, DerefMut},
4 slice,
5};
6
7use bitflags::bitflags;
8
9pub struct CallerCtx {
10 pub pid: usize,
11 pub uid: u32,
12 pub gid: u32,
13}
14
15pub enum OpenResult {
16 ThisScheme { number: usize },
17 OtherScheme { fd: usize },
18}
19
20#[repr(C)]
21#[derive(Clone, Copy, Debug, Default)]
22pub struct Sqe {
23 pub opcode: u8,
24 pub sqe_flags: SqeFlags,
25 pub _rsvd: u16, pub tag: u32,
27 pub args: [u64; 6],
28 pub caller: u64,
29}
30impl Deref for Sqe {
31 type Target = [u8];
32 fn deref(&self) -> &[u8] {
33 unsafe { slice::from_raw_parts(self as *const Sqe as *const u8, mem::size_of::<Sqe>()) }
34 }
35}
36
37impl DerefMut for Sqe {
38 fn deref_mut(&mut self) -> &mut [u8] {
39 unsafe { slice::from_raw_parts_mut(self as *mut Sqe as *mut u8, mem::size_of::<Sqe>()) }
40 }
41}
42
43bitflags! {
44 #[derive(Clone, Copy, Debug, Default)]
45 pub struct SqeFlags: u8 {
46 const ONEWAY = 1;
49 }
50}
51
52#[repr(C)]
53#[derive(Clone, Copy, Debug, Default)]
54pub struct Cqe {
55 pub flags: u8, pub extra_raw: [u8; 3],
57 pub tag: u32,
58 pub result: u64,
59}
60impl Deref for Cqe {
61 type Target = [u8];
62 fn deref(&self) -> &[u8] {
63 unsafe { slice::from_raw_parts(self as *const Cqe as *const u8, mem::size_of::<Cqe>()) }
64 }
65}
66
67impl DerefMut for Cqe {
68 fn deref_mut(&mut self) -> &mut [u8] {
69 unsafe { slice::from_raw_parts_mut(self as *mut Cqe as *mut u8, mem::size_of::<Cqe>()) }
70 }
71}
72
73bitflags! {
74 #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
75 pub struct NewFdFlags: u8 {
76 const POSITIONED = 1;
77 }
78}
79
80impl Cqe {
81 pub fn extra(&self) -> u32 {
82 u32::from_ne_bytes([self.extra_raw[0], self.extra_raw[1], self.extra_raw[2], 0])
83 }
84}
85
86#[repr(u8)]
87#[derive(Clone, Copy, Debug, Eq, PartialEq)]
88pub enum CqeOpcode {
89 RespondRegular,
90 RespondWithFd,
91 SendFevent, ObtainFd,
93 RespondWithMultipleFds,
94 }
96impl CqeOpcode {
97 pub fn try_from_raw(raw: u8) -> Option<Self> {
98 Some(match raw {
99 0 => Self::RespondRegular,
100 1 => Self::RespondWithFd,
101 2 => Self::SendFevent,
102 3 => Self::ObtainFd,
103 4 => Self::RespondWithMultipleFds,
104 _ => return None,
105 })
106 }
107}
108
109#[repr(u8)]
110#[non_exhaustive]
111#[derive(Clone, Copy, Debug)]
112pub enum Opcode {
113 Close = 3, Dup = 4, Read = 5, Write = 6, Fsize = 7, Fchmod = 8, Fchown = 9, Fcntl = 10, Fevent = 11, Sendfd = 12,
123 Fpath = 13, Frename = 14,
125 Fstat = 15, Fstatvfs = 16, Fsync = 17, Ftruncate = 18, Futimens = 19, MmapPrep = 20,
132 RequestMmap = 21,
133 Mremap = 22,
134 Munmap = 23,
135 Msync = 24, Cancel = 25, Getdents = 26,
140 CloseMsg = 27,
141 Call = 28,
142
143 OpenAt = 29, Flink = 30,
145 Recvfd = 31,
146
147 UnlinkAt = 32, }
149
150impl Opcode {
151 pub fn try_from_raw(raw: u8) -> Option<Self> {
152 use Opcode::*;
153
154 Some(match raw {
156 3 => Close,
157 4 => Dup,
158 5 => Read,
159 6 => Write,
160 7 => Fsize,
161 8 => Fchmod,
162 9 => Fchown,
163 10 => Fcntl,
164 11 => Fevent,
165 12 => Sendfd,
166 13 => Fpath,
167 14 => Frename,
168 15 => Fstat,
169 16 => Fstatvfs,
170 17 => Fsync,
171 18 => Ftruncate,
172 19 => Futimens,
173
174 20 => MmapPrep,
175 21 => RequestMmap,
176 22 => Mremap,
177 23 => Munmap,
178 24 => Msync,
179
180 25 => Cancel,
181 26 => Getdents,
182 27 => CloseMsg,
183 28 => Call,
184
185 29 => OpenAt,
186 30 => Flink,
187 31 => Recvfd,
188
189 32 => UnlinkAt,
190
191 _ => return None,
192 })
193 }
194}