dope-uring 0.77.0

Owned io_uring backend for dope
Documentation
use std::os::fd::{AsFd, BorrowedFd, OwnedFd};

use dope_sys::{
    Cloexec, Nonblock,
    io::{
        Close,
        fd::Fd,
        socket::{Setup, Socket as _, addr, option::Source as _},
    },
    thread::Thread,
};
use o3::{buffer::storage::provided::native, depot};

use crate::{
    Accepted, Borrowed, Canary, Context, Control, Encoded, Engine, Enter, Errno, Fault, Fifo,
    Infallible, MAX, Observe, Outcome, Payload, Poll, Primary, Range, Ready, Recover, Released,
    Resources, Ring, Selected, Session, Waiting, accept, admit, bounded, buffers, complete, cq,
    create, engine, enter, error, fast, file, gso, host, kernel, memory, num, op, option, permit,
    pipe, probe, provided, record, ring, socket, sq, submit, token, tuning, types,
};

#[derive(Debug)]
pub struct Backend;

impl Backend {
    pub fn try_crc32() -> Result<bool, Errno> {
        Ok(match Self::crc32() {
            Some(enabled) => enabled,
            None => crate::system::ffi::has_crc32(),
        })
    }
}

impl accept::Source for Backend {
    type Failure<F: From<Errno>> = Errno;

    const MAX_IN_FLIGHT: num::NonZero<usize> = num::NonZero::<usize>::MAX;

    fn is_retryable(error: Errno) -> bool {
        error.is_block()
            || matches!(
                error.code().get(),
                libc::EINTR
                    | libc::ECONNABORTED
                    | libc::ENETDOWN
                    | libc::EPROTO
                    | libc::ENOPROTOOPT
                    | libc::EHOSTDOWN
                    | libc::ENONET
                    | libc::EHOSTUNREACH
                    | libc::EOPNOTSUPP
                    | libc::ENETUNREACH
            )
    }

    fn can_defer(error: Errno) -> bool {
        error.code().get() == libc::ENFILE
    }
}

impl<C: Context> engine::Source<C> for Backend {
    type Backend = Engine<C>;
    type Closing = <Engine<C> as Close>::State;
}

impl<C, L, P, R> engine::Storage<C, L, P, R> for Backend
where
    C: host::Context,
    L: memory::raw::Canary,
    P: Copy + From<Primary> + From<memory::Error>,
    R: Copy,
{
    type Cleanup = <Self as create::Source>::Resources<Encoded<C>, L, P>;
    type Release = <Self as probe::Source>::Release<Encoded<C>, L, R>;
}

impl engine::Removal for Backend {
    type Slot = <Self as submit::Control>::Target;
}

impl gso::Source for Backend {
    type Mode = Control;

    const LIMITS: Option<(
        bounded::raw::U32<1, { MAX as u32 }>,
        bounded::raw::U32<2, { u16::MAX as u32 }>,
    )> = Some((
        bounded::raw::U32::exact::<65_535>(),
        bounded::raw::U32::exact::<64>(),
    ));
}

impl kernel::Source for Backend {
    type Code = op::Code;
    type Failure<S: Copy + Eq> = error::Failure<S>;
    type Returned = error::Returned;
}

impl pipe::Pipe for Backend {
    fn open() -> Result<(OwnedFd, OwnedFd), pipe::Error> {
        crate::raw::pipe(libc::O_CLOEXEC)
    }

    fn nonblocking() -> Result<(OwnedFd, OwnedFd), pipe::Error> {
        crate::raw::pipe(libc::O_CLOEXEC | libc::O_NONBLOCK)
    }
}

impl permit::Source for Backend {
    type Queue<K: permit::raw::Key, I: depot::raw::Compact> = Fifo<K, I>;
}

impl provided::Source for Backend {
    const PROVIDED: u16 = 1;
    const CANARY: u16 = 2;
    type Index = cq::Buffer;
    type Context<L: memory::raw::Receive> = buffers::raw::Context<L>;
    type Canary<L: memory::raw::Canary> = Canary<L>;
    type Producer<L: memory::raw::Receive> = buffers::raw::Producer<L>;
    type Pending<E: memory::raw::Extent> = Selected<E, E::Capacity>;
    type Complete<E: memory::raw::Extent> = Selected<E, E::Length>;
}

impl file::File for Backend {
    type Options = types::OpenHow;
    type Metadata = crate::raw::Metadata;
    type Request<'a> = Borrowed<'a>;
}

impl socket::Socket for Backend {
    const MAX_IOVECS: usize = libc::UIO_MAXIOV as usize;
    type Error = Errno;

    fn socket(domain: libc::c_int, kind: libc::c_int) -> Result<OwnedFd, Self::Error> {
        Self::try_open(domain, kind | libc::SOCK_CLOEXEC, 0)
    }

    fn pair() -> Result<(OwnedFd, OwnedFd), record::Error> {
        <Self as socket::raw::Source>::try_pair(libc::SOCK_SEQPACKET | libc::SOCK_CLOEXEC)
    }
}

impl addr::Source for Backend {
    const UNIX_TERMINATED: bool = false;
    const UNIX_ABSTRACT: bool = true;

    fn header(domain: socket::Domain, _: addr::Socklen) -> [u8; 2] {
        (domain.raw() as u16).to_ne_bytes()
    }
}

impl option::Source for Backend {
    type Listening = Errno;

    const KEEP_IDLE: libc::c_int = libc::TCP_KEEPIDLE;

    fn is_listening(file: BorrowedFd<'_>) -> Result<bool, Self::Listening> {
        let value = Self::get_option::<{ libc::SO_ACCEPTCONN }, Errno>(file)?;
        Ok(value != 0)
    }
}

impl socket::Prepare for Backend {
    type Handle = OwnedFd;
    type Fast = num::NonZero<i32>;

    fn fast_open(depth: Option<num::NonZero<i32>>) -> Result<Option<Self::Fast>, fast::Error> {
        Ok(depth)
    }

    fn open(domain: socket::Domain, kind: socket::Kind) -> Result<Self::Handle, socket::Create> {
        Self::socket(domain.raw(), kind.raw()).map_err(socket::Create::socket)
    }

    fn listener(domain: socket::Domain) -> Result<Self::Handle, socket::Create> {
        Self::open(domain, socket::Kind::Stream)
    }

    fn adopt(file: OwnedFd) -> Result<Self::Handle, socket::Setup> {
        <Self as Fd>::update::<Cloexec>(file.as_fd()).map_err(Setup::cloexec)?;
        <Self as Fd>::remove::<Nonblock>(file.as_fd()).map_err(Setup::nonblock)?;
        Ok(file)
    }

    fn into_fd(handle: Self::Handle) -> OwnedFd {
        handle
    }

    fn set_fast(file: BorrowedFd<'_>, fast: Self::Fast) -> Result<(), Errno> {
        Self::set_option(file, libc::IPPROTO_TCP, libc::TCP_FASTOPEN, &fast.get())
    }
}
impl<T: Copy + Eq, S: Copy + Eq, F: Copy + Eq> Observe<T, S, F> for Backend {
    type Observation = crate::Observed<T>;
}
impl tuning::Source for Backend {
    type State<E: Copy, const N: usize> = crate::driver::raw::State<E, N>;
    type Table<E: Copy, const N: usize> = crate::driver::raw::Table<E, N>;
}

impl ring::Source for Backend {
    type Ready<T: complete::raw::Token, L: memory::raw::Receive> = Ready<T, L>;
    type Poll = enter::Poll<kernel::Errno<error::Enter>, kernel::Errno<error::Register<24>>>;
    type Enter<'a, T: 'a> = Enter<'a, T>;
    type Entry<T> = cq::Entry<T>;
    type Message<'a, const NAME: usize, const CONTROL: usize> = cq::Output<'a, NAME, CONTROL>;
    type Ring<T: complete::raw::Token> = Ring<T>;
}

impl submit::Control for Backend {
    type Target = sq::Fixed;
    type Owned<T: complete::raw::Token> = sq::Owned<T>;
    type Poll<'a, T: complete::raw::Token> = Poll<'a, T>;

    fn nop<T: complete::raw::Token>(complete: T) -> Self::Owned<T> {
        sq::Entry::new(sq::Entry::NOP, sq::Descriptor::File(-1)).user_data(complete)
    }

    fn close<T: complete::raw::Token>(slot: Self::Target, complete: T) -> Self::Owned<T> {
        use crate::reactor::op::files;

        let mut entry = sq::Entry::new(files::CLOSE, sq::Descriptor::File(0));
        entry.fields().index = slot.encode();
        entry.user_data(complete)
    }

    fn shutdown<T: complete::raw::Token>(slot: Self::Target, complete: T) -> Self::Owned<T> {
        use crate::reactor::op::sockets;

        sockets::shutdown(slot, socket::How::BOTH)
            .flags(sq::Flags::HARD_LINK | sq::Flags::SKIP_SUCCESS)
            .user_data(complete)
    }
}

impl probe::Source for Backend {
    type Range = Range;
    type Release<T: token::Codec, L: memory::raw::Canary, P: Copy> = Released<T, L, P>;
    type Cleanup<T: token::Codec, L: memory::raw::Canary, P: Copy> = Recover<T, L, P>;
    type Payload = Payload;
    type Accept<T: token::Codec> = Accepted<T>;

    type Session<T: token::Codec, L: memory::raw::Canary> = Session<T, L>;

    type Outcome<T: complete::raw::Token, P: Copy> = Outcome<T, P>;
    type Waiting<T: token::Codec, S> = Waiting<S, T>;
}

impl create::Source for Backend {
    type Primary = Primary;
    type Failure<T: token::Codec> = Fault<T>;
    type Resources<
        T: token::Codec,
        L: memory::raw::Canary,
        P: Copy + From<Self::Primary> + From<memory::Error>,
    > = Resources<T, L, P>;
}

impl token::Source for Backend {
    type Token<C: host::Context> = Encoded<C>;
}

impl admit::Native for Backend {
    type Selection = native::raw::Index;
    type Permit = ();
    type Error<F> = Infallible;
}