Skip to main content

dope_core/driver/result/
mod.rs

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