1pub mod poll;
2
3use std::{convert, mem};
4
5use dope_sys::{
6 io::reactor::{self, Full, complete, ready::store},
7 io::socket::addr,
8 kernel, probe,
9 ring::prepare::Feature,
10};
11use o3::{alloc, buffer::pool, depot::arena::slab::Vacancy};
12
13use crate::{
14 Engine,
15 driver::{
16 life::domain,
17 receive,
18 result::poll::fault,
19 route::{indices, table::bounds},
20 settings::errors,
21 storage,
22 },
23 io::transfer,
24};
25
26#[derive(Clone, Copy, Debug, PartialEq, Eq)]
27pub enum IoOperation {
28 Entropy,
29 ReactorCreate,
30 MapRing,
31 RegisterReactor,
32 CancelRegistrations,
33 RegisterOpcodes,
34 RegisterFileTable,
35 RegisterFileRange,
36 MapBuffers,
37 UnmapRing,
38 UnmapBuffers,
39 PopulateBuffers,
40 RegisterProvidedBuffers,
41 RegisterCanaryBuffers,
42 UnregisterCanaryBuffers,
43 InstallReceiveFile,
44 RemoveReceiveFile,
45 InstallReceiveMessageFile,
46 RemoveReceiveMessageFile,
47 Probe(Probe),
48}
49
50#[derive(Clone, Copy, Debug, PartialEq, Eq)]
51pub enum Cloexec {
52 Get(crate::Errno),
53 Set(crate::Errno),
54}
55
56#[derive(Clone, Copy, Debug, PartialEq, Eq)]
57pub enum Capability {
58 Feature(Feature),
59 Opcode(Opcode),
60}
61
62#[derive(Clone, Copy, Debug, PartialEq, Eq)]
63#[repr(transparent)]
64pub struct Opcode(<crate::Backend as kernel::Source>::Code);
65
66impl<C> Opcode
67where
68 crate::Backend: kernel::Source<Code = C>,
69{
70 pub(crate) fn native<T: Into<C>>(code: T) -> Self {
71 Self(code.into())
72 }
73}
74
75#[derive(Clone, Copy, Debug, PartialEq, Eq)]
76#[repr(u8)]
77pub enum Probe {
78 Ring = 0,
79 DirectSocket = 1,
80 SocketOption = 2,
81 CloseDirectSocket = 3,
82 Receive = 4,
83 ReceiveMessage = 5,
84 DirectAccept = 6,
85 CloseDirectAccept = 7,
86 DirectAcceptMultishot = 8,
87 CloseDirectAcceptMultishot = 9,
88 RegisterAcceptListener = 10,
89 ReleaseAcceptListener = 11,
90}
91
92impl From<Probe> for u64 {
93 fn from(probe: Probe) -> Self {
94 probe as Self
95 }
96}
97
98#[derive(Clone, Copy, Debug, PartialEq, Eq)]
99pub struct ProbeError {
100 probe: Probe,
101 violation: Violation,
102}
103
104#[derive(Clone, Copy, Debug, PartialEq, Eq)]
105pub enum Violation {
106 MissingCompletion,
107 UnexpectedCompletion {
108 expected: Probe,
109 observed: fault::Observation,
110 },
111 Rejected(fault::Observation),
112 UnexpectedResult(fault::Observation),
113 Send(fault::Send),
114 InvalidFlags(fault::Observation),
115 MissingBuffer,
116 InvalidBuffer(receive::Selection),
117 BufferLength {
118 actual: transfer::Len,
119 capacity: transfer::NonZero,
120 },
121 EarlyTermination,
122 CorruptPayload,
123 FrameOverflow {
124 name: receive::Capacity,
125 control: receive::Capacity,
126 },
127 FrameShort {
128 actual: receive::Length,
129 required: receive::Length,
130 },
131 MissingFrameTruncation {
132 region: FrameRegion,
133 reported: receive::Length,
134 available: receive::Length,
135 },
136 TruncatedFrame,
137 InvalidAddress(addr::Error),
138 NonterminalCancellation(fault::Observation),
139 EscapedSlot(bounds::Slot),
140 ResidualWork {
141 submissions: fault::Pending,
142 completions: fault::Available,
143 },
144}
145
146#[derive(Clone, Copy, PartialEq, Eq, Debug)]
147pub enum FrameRegion {
148 Control,
149 Payload,
150}
151
152impl ProbeError {
153 pub(crate) fn decode(
154 failure: probe::Failure<Probe, <Engine as complete::Observe>::Observation>,
155 ) -> Self {
156 use dope_sys::probe::Check;
157 let (probe, check) = failure.into_parts();
158 let fault = match check {
159 Check::InvalidFlags(observed) => Violation::InvalidFlags((&observed).into()),
160 Check::Rejected(observed) => Violation::Rejected((&observed).into()),
161 Check::Unexpected(observed) => Violation::UnexpectedResult((&observed).into()),
162 Check::Nonterminal(observed) => Violation::NonterminalCancellation((&observed).into()),
163 };
164 Self::new(probe, fault)
165 }
166
167 pub(crate) const fn new(probe: Probe, fault: Violation) -> Self {
168 Self {
169 probe,
170 violation: fault,
171 }
172 }
173
174 pub const fn into_parts(self) -> (Probe, Violation) {
175 (self.probe, self.violation)
176 }
177}
178
179#[derive(Clone, Copy, Debug, Eq, PartialEq)]
180#[repr(transparent)]
181pub struct SubmitError(SubmitCause);
182
183#[derive(Clone, Copy, Debug, Eq, PartialEq)]
184pub enum SubmitCause {
185 Full,
187 IdentityExhausted,
189}
190
191impl SubmitError {
192 pub fn cause(self) -> SubmitCause {
193 self.0
194 }
195}
196
197impl From<Full> for SubmitError {
198 fn from(_: Full) -> Self {
199 Self(SubmitCause::Full)
200 }
201}
202
203impl From<Vacancy> for SubmitError {
204 fn from(error: Vacancy) -> Self {
205 Self(match error {
206 Vacancy::Full => SubmitCause::Full,
207 Vacancy::IdentityExhausted => SubmitCause::IdentityExhausted,
208 })
209 }
210}
211
212#[derive(Clone, Copy, Debug, PartialEq, Eq)]
213#[repr(transparent)]
214pub struct Kernel(kernel::Diagnostic<indices::SlotIndex>);
215
216impl Kernel {
217 pub fn cause(self) -> kernel::Diagnostic<indices::SlotIndex> {
219 self.0
220 }
221
222 pub(crate) fn native<T: Into<kernel::Diagnostic<indices::SlotIndex>>>(cause: T) -> Self {
223 Self(cause.into())
224 }
225}
226
227impl<O> From<kernel::Errno<O>> for Kernel
228where
229 <crate::Backend as kernel::Source>::Failure<indices::SlotIndex>: From<kernel::Errno<O>>,
230{
231 fn from(cause: kernel::Errno<O>) -> Self {
232 let cause: <crate::Backend as kernel::Source>::Failure<indices::SlotIndex> = cause.into();
233 Self::native(cause)
234 }
235}
236
237impl From<convert::Infallible> for Kernel {
238 fn from(value: convert::Infallible) -> Self {
239 match value {}
240 }
241}
242
243impl From<dope_sys::Errno> for Kernel {
244 fn from(cause: dope_sys::Errno) -> Self {
245 Self(cause.into())
246 }
247}
248
249const _: () = {
250 assert!(
251 mem::size_of::<Kernel>()
252 == mem::size_of::<<crate::Backend as kernel::Source>::Failure<indices::SlotIndex>>()
253 );
254 assert!(
255 mem::align_of::<Kernel>()
256 == mem::align_of::<<crate::Backend as kernel::Source>::Failure<indices::SlotIndex>>()
257 );
258};
259
260#[derive(Clone, Debug)]
262pub enum Refused {
263 Plan(errors::Config),
264 Allocation(alloc::Error),
265 Queue(alloc::Error),
266 Buffer(pool::CreateError),
267 ReactorCloexec(Cloexec),
268 Native(storage::Failure),
269 Removal(storage::Removal),
270 Register(domain::Register),
271}
272
273impl Refused {
274 pub(crate) fn kernel(op: IoOperation, cause: Kernel) -> Self {
275 Self::Native(storage::Failure::Kernel {
276 operation: op,
277 cause,
278 })
279 }
280}
281
282impl From<storage::Failure> for Refused {
283 fn from(cause: storage::Failure) -> Self {
284 Self::Native(cause)
285 }
286}
287
288impl From<crate::ReleasePrimary> for Refused {
289 fn from(primary: crate::ReleasePrimary) -> Self {
290 Self::Removal(primary.into())
291 }
292}
293
294impl From<store::Create> for Refused {
295 fn from(error: store::Create) -> Self {
296 use store::Create;
297
298 match error {
299 Create::Buffer(cause) => Self::Buffer(cause),
300 Create::Allocation(cause) => Self::Allocation(cause),
301 Create::Queue(cause) => Self::Queue(cause),
302 Create::Reactor(reactor::Create::Open(cause)) => {
303 Self::kernel(IoOperation::ReactorCreate, cause.into())
304 }
305 Create::Reactor(reactor::Create::Cloexec(cause)) => {
306 Self::ReactorCloexec(Cloexec::Set(cause))
307 }
308 }
309 }
310}
311
312impl From<alloc::Error> for Refused {
313 fn from(cause: alloc::Error) -> Self {
314 Self::Allocation(cause)
315 }
316}
317
318impl From<alloc::Overflow> for Refused {
319 fn from(cause: alloc::Overflow) -> Self {
320 Self::Allocation(cause.into())
321 }
322}
323
324impl From<alloc::Exhausted> for Refused {
325 fn from(cause: alloc::Exhausted) -> Self {
326 Self::Allocation(cause.into())
327 }
328}