Skip to main content

dope_uring/
backend.rs

1use std::os::fd::{AsFd, BorrowedFd, OwnedFd};
2
3use dope_sys::{
4    Cloexec, Nonblock,
5    io::{
6        Close,
7        fd::Fd,
8        socket::{Setup, Socket as _, addr, option::Source as _},
9    },
10    thread::Thread,
11};
12use o3::{buffer::storage::provided::native, depot};
13
14use crate::{
15    Accepted, Borrowed, Canary, Context, Control, Encoded, Engine, Enter, Errno, Fault, Fifo,
16    Infallible, MAX, Observe, Outcome, Payload, Poll, Primary, Range, Ready, Recover, Released,
17    Resources, Ring, Selected, Session, Waiting, accept, admit, bounded, buffers, complete, cq,
18    create, engine, enter, error, fast, file, gso, host, kernel, memory, num, op, option, permit,
19    pipe, probe, provided, record, ring, socket, sq, submit, token, tuning, types,
20};
21
22#[derive(Debug)]
23pub struct Backend;
24
25impl Backend {
26    pub fn try_crc32() -> Result<bool, Errno> {
27        Ok(match Self::crc32() {
28            Some(enabled) => enabled,
29            None => crate::system::ffi::has_crc32(),
30        })
31    }
32}
33
34impl accept::Source for Backend {
35    type Failure<F: From<Errno>> = Errno;
36
37    const MAX_IN_FLIGHT: num::NonZero<usize> = num::NonZero::<usize>::MAX;
38
39    fn is_retryable(error: Errno) -> bool {
40        error.is_block()
41            || matches!(
42                error.code().get(),
43                libc::EINTR
44                    | libc::ECONNABORTED
45                    | libc::ENETDOWN
46                    | libc::EPROTO
47                    | libc::ENOPROTOOPT
48                    | libc::EHOSTDOWN
49                    | libc::ENONET
50                    | libc::EHOSTUNREACH
51                    | libc::EOPNOTSUPP
52                    | libc::ENETUNREACH
53            )
54    }
55
56    fn can_defer(error: Errno) -> bool {
57        error.code().get() == libc::ENFILE
58    }
59}
60
61impl<C: Context> engine::Source<C> for Backend {
62    type Backend = Engine<C>;
63    type Closing = <Engine<C> as Close>::State;
64}
65
66impl<C, L, P, R> engine::Storage<C, L, P, R> for Backend
67where
68    C: host::Context,
69    L: memory::raw::Canary,
70    P: Copy + From<Primary> + From<memory::Error>,
71    R: Copy,
72{
73    type Cleanup = <Self as create::Source>::Resources<Encoded<C>, L, P>;
74    type Release = <Self as probe::Source>::Release<Encoded<C>, L, R>;
75}
76
77impl engine::Removal for Backend {
78    type Slot = <Self as submit::Control>::Target;
79}
80
81impl gso::Source for Backend {
82    type Mode = Control;
83
84    const LIMITS: Option<(
85        bounded::raw::U32<1, { MAX as u32 }>,
86        bounded::raw::U32<2, { u16::MAX as u32 }>,
87    )> = Some((
88        bounded::raw::U32::exact::<65_535>(),
89        bounded::raw::U32::exact::<64>(),
90    ));
91}
92
93impl kernel::Source for Backend {
94    type Code = op::Code;
95    type Failure<S: Copy + Eq> = error::Failure<S>;
96    type Returned = error::Returned;
97}
98
99impl pipe::Pipe for Backend {
100    fn open() -> Result<(OwnedFd, OwnedFd), pipe::Error> {
101        crate::raw::pipe(libc::O_CLOEXEC)
102    }
103
104    fn nonblocking() -> Result<(OwnedFd, OwnedFd), pipe::Error> {
105        crate::raw::pipe(libc::O_CLOEXEC | libc::O_NONBLOCK)
106    }
107}
108
109impl permit::Source for Backend {
110    type Queue<K: permit::raw::Key, I: depot::raw::Compact> = Fifo<K, I>;
111}
112
113impl provided::Source for Backend {
114    const PROVIDED: u16 = 1;
115    const CANARY: u16 = 2;
116    type Index = cq::Buffer;
117    type Context<L: memory::raw::Receive> = buffers::raw::Context<L>;
118    type Canary<L: memory::raw::Canary> = Canary<L>;
119    type Producer<L: memory::raw::Receive> = buffers::raw::Producer<L>;
120    type Pending<E: memory::raw::Extent> = Selected<E, E::Capacity>;
121    type Complete<E: memory::raw::Extent> = Selected<E, E::Length>;
122}
123
124impl file::File for Backend {
125    type Options = types::OpenHow;
126    type Metadata = crate::raw::Metadata;
127    type Request<'a> = Borrowed<'a>;
128}
129
130impl socket::Socket for Backend {
131    const MAX_IOVECS: usize = libc::UIO_MAXIOV as usize;
132    type Error = Errno;
133
134    fn socket(domain: libc::c_int, kind: libc::c_int) -> Result<OwnedFd, Self::Error> {
135        Self::try_open(domain, kind | libc::SOCK_CLOEXEC, 0)
136    }
137
138    fn pair() -> Result<(OwnedFd, OwnedFd), record::Error> {
139        <Self as socket::raw::Source>::try_pair(libc::SOCK_SEQPACKET | libc::SOCK_CLOEXEC)
140    }
141}
142
143impl addr::Source for Backend {
144    const UNIX_TERMINATED: bool = false;
145    const UNIX_ABSTRACT: bool = true;
146
147    fn header(domain: socket::Domain, _: addr::Socklen) -> [u8; 2] {
148        (domain.raw() as u16).to_ne_bytes()
149    }
150}
151
152impl option::Source for Backend {
153    type Listening = Errno;
154
155    const KEEP_IDLE: libc::c_int = libc::TCP_KEEPIDLE;
156
157    fn is_listening(file: BorrowedFd<'_>) -> Result<bool, Self::Listening> {
158        let value = Self::get_option::<{ libc::SO_ACCEPTCONN }, Errno>(file)?;
159        Ok(value != 0)
160    }
161}
162
163impl socket::Prepare for Backend {
164    type Handle = OwnedFd;
165    type Fast = num::NonZero<i32>;
166
167    fn fast_open(depth: Option<num::NonZero<i32>>) -> Result<Option<Self::Fast>, fast::Error> {
168        Ok(depth)
169    }
170
171    fn open(domain: socket::Domain, kind: socket::Kind) -> Result<Self::Handle, socket::Create> {
172        Self::socket(domain.raw(), kind.raw()).map_err(socket::Create::socket)
173    }
174
175    fn listener(domain: socket::Domain) -> Result<Self::Handle, socket::Create> {
176        Self::open(domain, socket::Kind::Stream)
177    }
178
179    fn adopt(file: OwnedFd) -> Result<Self::Handle, socket::Setup> {
180        <Self as Fd>::update::<Cloexec>(file.as_fd()).map_err(Setup::cloexec)?;
181        <Self as Fd>::remove::<Nonblock>(file.as_fd()).map_err(Setup::nonblock)?;
182        Ok(file)
183    }
184
185    fn into_fd(handle: Self::Handle) -> OwnedFd {
186        handle
187    }
188
189    fn set_fast(file: BorrowedFd<'_>, fast: Self::Fast) -> Result<(), Errno> {
190        Self::set_option(file, libc::IPPROTO_TCP, libc::TCP_FASTOPEN, &fast.get())
191    }
192}
193impl<T: Copy + Eq, S: Copy + Eq, F: Copy + Eq> Observe<T, S, F> for Backend {
194    type Observation = crate::Observed<T>;
195}
196impl tuning::Source for Backend {
197    type State<E: Copy, const N: usize> = crate::driver::raw::State<E, N>;
198    type Table<E: Copy, const N: usize> = crate::driver::raw::Table<E, N>;
199}
200
201impl ring::Source for Backend {
202    type Ready<T: complete::raw::Token, L: memory::raw::Receive> = Ready<T, L>;
203    type Poll = enter::Poll<kernel::Errno<error::Enter>, kernel::Errno<error::Register<24>>>;
204    type Enter<'a, T: 'a> = Enter<'a, T>;
205    type Entry<T> = cq::Entry<T>;
206    type Message<'a, const NAME: usize, const CONTROL: usize> = cq::Output<'a, NAME, CONTROL>;
207    type Ring<T: complete::raw::Token> = Ring<T>;
208}
209
210impl submit::Control for Backend {
211    type Target = sq::Fixed;
212    type Owned<T: complete::raw::Token> = sq::Owned<T>;
213    type Poll<'a, T: complete::raw::Token> = Poll<'a, T>;
214
215    fn nop<T: complete::raw::Token>(complete: T) -> Self::Owned<T> {
216        sq::Entry::new(sq::Entry::NOP, sq::Descriptor::File(-1)).user_data(complete)
217    }
218
219    fn close<T: complete::raw::Token>(slot: Self::Target, complete: T) -> Self::Owned<T> {
220        use crate::reactor::op::files;
221
222        let mut entry = sq::Entry::new(files::CLOSE, sq::Descriptor::File(0));
223        entry.fields().index = slot.encode();
224        entry.user_data(complete)
225    }
226
227    fn shutdown<T: complete::raw::Token>(slot: Self::Target, complete: T) -> Self::Owned<T> {
228        use crate::reactor::op::sockets;
229
230        sockets::shutdown(slot, socket::How::BOTH)
231            .flags(sq::Flags::HARD_LINK | sq::Flags::SKIP_SUCCESS)
232            .user_data(complete)
233    }
234}
235
236impl probe::Source for Backend {
237    type Range = Range;
238    type Release<T: token::Codec, L: memory::raw::Canary, P: Copy> = Released<T, L, P>;
239    type Cleanup<T: token::Codec, L: memory::raw::Canary, P: Copy> = Recover<T, L, P>;
240    type Payload = Payload;
241    type Accept<T: token::Codec> = Accepted<T>;
242
243    type Session<T: token::Codec, L: memory::raw::Canary> = Session<T, L>;
244
245    type Outcome<T: complete::raw::Token, P: Copy> = Outcome<T, P>;
246    type Waiting<T: token::Codec, S> = Waiting<S, T>;
247}
248
249impl create::Source for Backend {
250    type Primary = Primary;
251    type Failure<T: token::Codec> = Fault<T>;
252    type Resources<
253        T: token::Codec,
254        L: memory::raw::Canary,
255        P: Copy + From<Self::Primary> + From<memory::Error>,
256    > = Resources<T, L, P>;
257}
258
259impl token::Source for Backend {
260    type Token<C: host::Context> = Encoded<C>;
261}
262
263impl admit::Native for Backend {
264    type Selection = native::raw::Index;
265    type Permit = ();
266    type Error<F> = Infallible;
267}