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 RespondWithMultipleFds,
83 }
85impl CqeOpcode {
86 pub fn try_from_raw(raw: u8) -> Option<Self> {
87 Some(match raw {
88 0 => Self::RespondRegular,
89 1 => Self::RespondWithFd,
90 2 => Self::SendFevent,
91 3 => Self::ObtainFd,
92 4 => Self::RespondWithMultipleFds,
93 _ => return None,
94 })
95 }
96}
97
98#[repr(u8)]
99#[non_exhaustive]
100#[derive(Clone, Copy, Debug)]
101pub enum Opcode {
102 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,
115 Fpath = 13, Frename = 14,
117 Fstat = 15, Fstatvfs = 16, Fsync = 17, Ftruncate = 18, Futimens = 19, MmapPrep = 20,
124 RequestMmap = 21,
125 Mremap = 22,
126 Munmap = 23,
127 Msync = 24, Cancel = 25, Getdents = 26,
132 CloseMsg = 27,
133 Call = 28,
134
135 OpenAt = 29, Flink = 30,
137
138 Recvfd = 31,
139}
140
141impl Opcode {
142 pub fn try_from_raw(raw: u8) -> Option<Self> {
143 use Opcode::*;
144
145 Some(match raw {
147 0 => Open,
148 1 => Rmdir,
149 2 => Unlink,
150 3 => Close,
151 4 => Dup,
152 5 => Read,
153 6 => Write,
154 7 => Fsize,
155 8 => Fchmod,
156 9 => Fchown,
157 10 => Fcntl,
158 11 => Fevent,
159 12 => Sendfd,
160 13 => Fpath,
161 14 => Frename,
162 15 => Fstat,
163 16 => Fstatvfs,
164 17 => Fsync,
165 18 => Ftruncate,
166 19 => Futimens,
167
168 20 => MmapPrep,
169 21 => RequestMmap,
170 22 => Mremap,
171 23 => Munmap,
172 24 => Msync,
173
174 25 => Cancel,
175 26 => Getdents,
176 27 => CloseMsg,
177 28 => Call,
178
179 29 => OpenAt,
180 30 => Flink,
181
182 31 => Recvfd,
183
184 _ => return None,
185 })
186 }
187}