dope-core 0.77.0

Owned native I/O and completion driver for dope
Documentation
pub mod poll;

use std::{convert, mem};

use dope_sys::{
    io::reactor::{self, Full, complete, ready::store},
    io::socket::addr,
    kernel, probe,
    ring::prepare::Feature,
};
use o3::{alloc, buffer::pool, depot::arena::slab::Vacancy};

use crate::{
    Engine,
    driver::{
        life::domain,
        receive,
        result::poll::fault,
        route::{indices, table::bounds},
        settings::errors,
        storage,
    },
    io::transfer,
};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum IoOperation {
    Entropy,
    ReactorCreate,
    MapRing,
    RegisterReactor,
    CancelRegistrations,
    RegisterOpcodes,
    RegisterFileTable,
    RegisterFileRange,
    MapBuffers,
    UnmapRing,
    UnmapBuffers,
    PopulateBuffers,
    RegisterProvidedBuffers,
    RegisterCanaryBuffers,
    UnregisterCanaryBuffers,
    InstallReceiveFile,
    RemoveReceiveFile,
    InstallReceiveMessageFile,
    RemoveReceiveMessageFile,
    Probe(Probe),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Cloexec {
    Get(crate::Errno),
    Set(crate::Errno),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Capability {
    Feature(Feature),
    Opcode(Opcode),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct Opcode(<crate::Backend as kernel::Source>::Code);

impl<C> Opcode
where
    crate::Backend: kernel::Source<Code = C>,
{
    pub(crate) fn native<T: Into<C>>(code: T) -> Self {
        Self(code.into())
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum Probe {
    Ring = 0,
    DirectSocket = 1,
    SocketOption = 2,
    CloseDirectSocket = 3,
    Receive = 4,
    ReceiveMessage = 5,
    DirectAccept = 6,
    CloseDirectAccept = 7,
    DirectAcceptMultishot = 8,
    CloseDirectAcceptMultishot = 9,
    RegisterAcceptListener = 10,
    ReleaseAcceptListener = 11,
}

impl From<Probe> for u64 {
    fn from(probe: Probe) -> Self {
        probe as Self
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ProbeError {
    probe: Probe,
    violation: Violation,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Violation {
    MissingCompletion,
    UnexpectedCompletion {
        expected: Probe,
        observed: fault::Observation,
    },
    Rejected(fault::Observation),
    UnexpectedResult(fault::Observation),
    Send(fault::Send),
    InvalidFlags(fault::Observation),
    MissingBuffer,
    InvalidBuffer(receive::Selection),
    BufferLength {
        actual: transfer::Len,
        capacity: transfer::NonZero,
    },
    EarlyTermination,
    CorruptPayload,
    FrameOverflow {
        name: receive::Capacity,
        control: receive::Capacity,
    },
    FrameShort {
        actual: receive::Length,
        required: receive::Length,
    },
    MissingFrameTruncation {
        region: FrameRegion,
        reported: receive::Length,
        available: receive::Length,
    },
    TruncatedFrame,
    InvalidAddress(addr::Error),
    NonterminalCancellation(fault::Observation),
    EscapedSlot(bounds::Slot),
    ResidualWork {
        submissions: fault::Pending,
        completions: fault::Available,
    },
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum FrameRegion {
    Control,
    Payload,
}

impl ProbeError {
    pub(crate) fn decode(
        failure: probe::Failure<Probe, <Engine as complete::Observe>::Observation>,
    ) -> Self {
        use dope_sys::probe::Check;
        let (probe, check) = failure.into_parts();
        let fault = match check {
            Check::InvalidFlags(observed) => Violation::InvalidFlags((&observed).into()),
            Check::Rejected(observed) => Violation::Rejected((&observed).into()),
            Check::Unexpected(observed) => Violation::UnexpectedResult((&observed).into()),
            Check::Nonterminal(observed) => Violation::NonterminalCancellation((&observed).into()),
        };
        Self::new(probe, fault)
    }

    pub(crate) const fn new(probe: Probe, fault: Violation) -> Self {
        Self {
            probe,
            violation: fault,
        }
    }

    pub const fn into_parts(self) -> (Probe, Violation) {
        (self.probe, self.violation)
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(transparent)]
pub struct SubmitError(SubmitCause);

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SubmitCause {
    /// Queue or flight capacity can become available after existing work retires.
    Full,
    /// Every identity in the retained flight group has been permanently consumed.
    IdentityExhausted,
}

impl SubmitError {
    pub fn cause(self) -> SubmitCause {
        self.0
    }
}

impl From<Full> for SubmitError {
    fn from(_: Full) -> Self {
        Self(SubmitCause::Full)
    }
}

impl From<Vacancy> for SubmitError {
    fn from(error: Vacancy) -> Self {
        Self(match error {
            Vacancy::Full => SubmitCause::Full,
            Vacancy::IdentityExhausted => SubmitCause::IdentityExhausted,
        })
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct Kernel(kernel::Diagnostic<indices::SlotIndex>);

impl Kernel {
    /// Observes the exact operation and payload without exposing the native backend.
    pub fn cause(self) -> kernel::Diagnostic<indices::SlotIndex> {
        self.0
    }

    pub(crate) fn native<T: Into<kernel::Diagnostic<indices::SlotIndex>>>(cause: T) -> Self {
        Self(cause.into())
    }
}

impl<O> From<kernel::Errno<O>> for Kernel
where
    <crate::Backend as kernel::Source>::Failure<indices::SlotIndex>: From<kernel::Errno<O>>,
{
    fn from(cause: kernel::Errno<O>) -> Self {
        let cause: <crate::Backend as kernel::Source>::Failure<indices::SlotIndex> = cause.into();
        Self::native(cause)
    }
}

impl From<convert::Infallible> for Kernel {
    fn from(value: convert::Infallible) -> Self {
        match value {}
    }
}

impl From<dope_sys::Errno> for Kernel {
    fn from(cause: dope_sys::Errno) -> Self {
        Self(cause.into())
    }
}

const _: () = {
    assert!(
        mem::size_of::<Kernel>()
            == mem::size_of::<<crate::Backend as kernel::Source>::Failure<indices::SlotIndex>>()
    );
    assert!(
        mem::align_of::<Kernel>()
            == mem::align_of::<<crate::Backend as kernel::Source>::Failure<indices::SlotIndex>>()
    );
};

/// Construction failures without an outstanding native retirement.
#[derive(Clone, Debug)]
pub enum Refused {
    Plan(errors::Config),
    Allocation(alloc::Error),
    Queue(alloc::Error),
    Buffer(pool::CreateError),
    ReactorCloexec(Cloexec),
    Native(storage::Failure),
    Removal(storage::Removal),
    Register(domain::Register),
}

impl Refused {
    pub(crate) fn kernel(op: IoOperation, cause: Kernel) -> Self {
        Self::Native(storage::Failure::Kernel {
            operation: op,
            cause,
        })
    }
}

impl From<storage::Failure> for Refused {
    fn from(cause: storage::Failure) -> Self {
        Self::Native(cause)
    }
}

impl From<crate::ReleasePrimary> for Refused {
    fn from(primary: crate::ReleasePrimary) -> Self {
        Self::Removal(primary.into())
    }
}

impl From<store::Create> for Refused {
    fn from(error: store::Create) -> Self {
        use store::Create;

        match error {
            Create::Buffer(cause) => Self::Buffer(cause),
            Create::Allocation(cause) => Self::Allocation(cause),
            Create::Queue(cause) => Self::Queue(cause),
            Create::Reactor(reactor::Create::Open(cause)) => {
                Self::kernel(IoOperation::ReactorCreate, cause.into())
            }
            Create::Reactor(reactor::Create::Cloexec(cause)) => {
                Self::ReactorCloexec(Cloexec::Set(cause))
            }
        }
    }
}

impl From<alloc::Error> for Refused {
    fn from(cause: alloc::Error) -> Self {
        Self::Allocation(cause)
    }
}

impl From<alloc::Overflow> for Refused {
    fn from(cause: alloc::Overflow) -> Self {
        Self::Allocation(cause.into())
    }
}

impl From<alloc::Exhausted> for Refused {
    fn from(cause: alloc::Exhausted) -> Self {
        Self::Allocation(cause.into())
    }
}