1use std::{
2 alloc::Layout,
3 os::fd::{AsFd, BorrowedFd, OwnedFd},
4 ptr,
5};
6
7use dope_sys::{
8 Cloexec, Nonblock,
9 engine::{life, life::Error, ring::Backend as _},
10 io::{
11 fd::{self, Fd},
12 socket::{Setup, Socket as _, addr, option::tuning::Quiesce as _},
13 },
14 probe::Stopped,
15 ring::ready::Ready as _,
16 thread::Thread,
17};
18use o3::depot;
19
20use crate::{
21 Accepted, Borrowed, Canary, Cleanup, Closing, Context, Control, Cpus, Encoded, Engine, Enter,
22 Errno, Fault, Fifo, Infallible, MAX, Observe, Operation, Outcome, Payload, Poll, Primary,
23 Producer, Range, Ready, Recover, Released, Resources, Ring, Selected, Session, Source, Table,
24 Timer, Turn, Waiting, accept, admit, affinity, bounded, buffer, buffers, complete, cq, create,
25 delivery,
26 driver::{Quiesced, tuning::Progress},
27 engine, enter, errno, error, fast, file, gso, host, kernel, mem, memory, num, op, option,
28 packet, permit, pipe, probe, provided, record, ring, socket, sq, submit,
29 system::signal,
30 token, tuning, types,
31};
32
33#[derive(Debug)]
34pub struct Uring;
35
36impl Uring {
37 const ALIGNMENT: num::NonZero<usize> = num::NonZero::<usize>::MIN.saturating_add(4095);
38
39 fn open_with(flags: libc::c_int) -> Result<(OwnedFd, OwnedFd), pipe::Error> {
40 let mut fds = [0; 2];
41 let result = unsafe { libc::pipe2(fds.as_mut_ptr(), flags) };
43 unsafe { fd::raw::Pair::new(fds).try_adopt::<Self, Errno>(result) }
45 .map_err(pipe::Error::Open)
46 }
47
48 pub fn layout<T>(count: usize) -> Result<num::NonZero<usize>, memory::Error> {
49 let invalid = || memory::Error::Length {
50 element: mem::size_of::<T>(),
51 count,
52 };
53 let layout = Layout::array::<T>(count).map_err(|_| invalid())?;
54 let bytes = num::NonZero::new(layout.size()).ok_or_else(invalid)?;
55 let element = layout.align();
56 let limit = Self::ALIGNMENT.get();
57 if element > limit {
58 return Err(memory::Error::Alignment {
59 element,
60 supported: limit,
61 });
62 }
63 Ok(bytes)
64 }
65
66 pub(crate) unsafe fn status(raw: libc::c_long) -> kernel::raw::Status<-1, { i32::MAX }> {
69 const { assert!(mem::size_of::<libc::c_long>() == mem::size_of::<libc::ssize_t>()) };
70 unsafe { kernel::raw::Status::from_count(raw as libc::ssize_t) }
73 }
74
75 pub(crate) fn try_map<const FLAGS: libc::c_int>(
77 bytes: num::NonZero<usize>,
78 ) -> Result<*mut libc::c_void, Errno> {
79 const { assert!(FLAGS & !libc::MAP_NORESERVE == 0) };
80 let address = unsafe {
82 libc::mmap(
83 ptr::null_mut(),
84 bytes.get(),
85 libc::PROT_READ | libc::PROT_WRITE,
86 libc::MAP_PRIVATE | libc::MAP_ANONYMOUS | FLAGS,
87 -1,
88 0,
89 )
90 };
91 if address == libc::MAP_FAILED {
92 Err(unsafe { kernel::raw::Code::last::<Self>().into() })
94 } else {
95 Ok(address)
96 }
97 }
98
99 pub(crate) unsafe fn try_unmap<E: From<Errno>>(
103 address: *mut libc::c_void,
104 bytes: num::NonZero<usize>,
105 ) -> Result<(), E> {
106 let result = unsafe { libc::munmap(address, bytes.get()) };
108 unsafe { kernel::raw::Status::new(result).try_zero::<Self, E>() }
110 }
111}
112
113impl accept::Source for Uring {
114 type Failure<F: From<Errno>> = Errno;
115
116 const MAX_IN_FLIGHT: num::NonZero<usize> = num::NonZero::<usize>::MAX;
117
118 fn is_retryable(error: Errno) -> bool {
119 error.is_block()
120 || matches!(
121 error.code().get(),
122 libc::EINTR
123 | libc::ECONNABORTED
124 | libc::ENETDOWN
125 | libc::EPROTO
126 | libc::ENOPROTOOPT
127 | libc::EHOSTDOWN
128 | libc::ENONET
129 | libc::EHOSTUNREACH
130 | libc::EOPNOTSUPP
131 | libc::ENETUNREACH
132 )
133 }
134
135 fn can_defer(error: Errno) -> bool {
136 error.code().get() == libc::ENFILE
137 }
138}
139
140impl<L: Copy + Eq> buffer::Selection<L> for Uring {
141 type Error = (<Self as provided::Source>::Index, L);
142}
143
144impl<C: Context> engine::Source<C> for Uring {
145 type Map = C::Map;
146 type Backend = Engine<C>;
147 type Closing = Closing<C>;
148}
149
150impl<C, P, R> engine::Storage<C, P, R> for Uring
151where
152 C: Context<Layout: memory::raw::Canary>,
153 P: Copy + From<Primary> + From<memory::Error>,
154 R: Copy,
155{
156 type Cleanup = <Self as create::Source>::Resources<C::Token, C::Layout, P>;
157 type Release = <Self as probe::Source>::Release<C::Token, C::Layout, R>;
158}
159
160impl engine::Removal for Uring {
161 type Slot = <Self as submit::Control>::Target;
162}
163
164impl<'a, C, H> engine::Dispatch<'a, C, H> for Uring
165where
166 C: Context + delivery::Context<H, Backend = Engine<C>>,
167 C::Tuning: Progress<C::Token>,
168 H: 'a,
169{
170 type Turn<Q: delivery::Quota<H>> = Turn<'a, H, Q>;
171}
172
173impl gso::Source for Uring {
174 type Mode = Control;
175
176 const LIMITS: Option<(
177 bounded::U32<1, { MAX as u32 }>,
178 bounded::U32<2, { u16::MAX as u32 }>,
179 )> = Some((bounded::U32::exact::<65_535>(), bounded::U32::exact::<64>()));
180}
181
182impl kernel::Source for Uring {
183 type Code = op::Code;
184 type Failure<S: Copy + Eq> = error::Failure<S>;
185 type Returned = error::Returned;
186}
187
188impl Thread for Uring {
189 type Timer = Timer;
190 type Receiver = signal::raw::Receiver;
191 type Cpus = Cpus;
192
193 fn is_main() -> bool {
194 unsafe { libc::syscall(libc::SYS_gettid) == libc::getpid() as libc::c_long }
197 }
198
199 fn current() -> Result<Self::Cpus, affinity::Error> {
200 let mut mask: libc::cpu_set_t = unsafe { mem::zeroed() };
203 let result =
205 unsafe { libc::sched_getaffinity(0, mem::size_of::<libc::cpu_set_t>(), &mut mask) };
206 unsafe { kernel::raw::Status::new(result).try_zero::<Uring, Errno>() }
208 .map_err(affinity::Error::Current)?;
209 Ok(Cpus::new(&mask))
210 }
211
212 fn bind(cpu: affinity::raw::Cpu) -> Result<(), affinity::Error> {
213 const MAX: u32 = Cpus::LIMIT as u32 - 1;
214 let index = bounded::U32::<0, MAX>::new(u32::from(cpu.raw())).ok_or(
215 affinity::Error::BindingRange {
216 cpu,
217 maximum: MAX as u16,
218 },
219 )?;
220 let mut mask: libc::cpu_set_t = unsafe { mem::zeroed() };
222 unsafe { libc::CPU_SET(index.into_usize(), &mut mask) };
224 let result =
226 unsafe { libc::sched_setaffinity(0, mem::size_of::<libc::cpu_set_t>(), &mask) };
227 unsafe { kernel::raw::Status::new(result).try_zero::<Uring, Errno>() }
229 .map_err(|fault| affinity::Error::Bind { cpu, fault })?;
230 Ok(())
231 }
232}
233
234impl errno::Source for Uring {
235 fn clear() {
236 unsafe { libc::__errno_location().write(0) };
238 }
239}
240
241impl pipe::Pipe for Uring {
242 fn open() -> Result<(OwnedFd, OwnedFd), pipe::Error> {
243 Self::open_with(libc::O_CLOEXEC)
244 }
245
246 fn nonblocking() -> Result<(OwnedFd, OwnedFd), pipe::Error> {
247 Self::open_with(libc::O_CLOEXEC | libc::O_NONBLOCK)
248 }
249}
250
251impl permit::Source for Uring {
252 type Queue<K: permit::raw::Key, I: depot::raw::Compact> = Fifo<K, I>;
253}
254
255impl provided::Source for Uring {
256 const PROVIDED: u16 = 1;
257 const CANARY: u16 = 2;
258 type Index = cq::Buffer;
259 type Context<L: memory::raw::Receive> = buffers::raw::Context<L>;
260 type Canary<L: memory::raw::Canary> = Canary<L>;
261 type Cleanup<L: memory::raw::Receive> = Cleanup<L>;
262 type Producer<L: memory::raw::Receive> = Producer<L>;
263 type Pending<E: memory::raw::Extent> = Selected<E, E::Capacity>;
264 type Complete<E: memory::raw::Extent> = Selected<E, E::Length>;
265}
266
267impl file::File for Uring {
268 type Options = types::OpenHow;
269 type Metadata = crate::raw::Metadata;
270 type Request<'a> = Borrowed<'a>;
271}
272
273impl socket::Socket for Uring {
274 const MAX_IOVECS: usize = libc::UIO_MAXIOV as usize;
275 type Error = Errno;
276
277 fn socket(domain: libc::c_int, kind: libc::c_int) -> Result<OwnedFd, Self::Error> {
278 Self::try_open(domain, kind | libc::SOCK_CLOEXEC, 0)
279 }
280
281 fn pair() -> Result<(OwnedFd, OwnedFd), record::Error> {
282 Self::try_pair::<Errno>(libc::SOCK_SEQPACKET | libc::SOCK_CLOEXEC)
283 .map_err(record::Error::Open)
284 }
285}
286
287impl addr::Source for Uring {
288 const UNIX_TERMINATED: bool = false;
289 const UNIX_ABSTRACT: bool = true;
290
291 fn header(domain: socket::Domain, _: addr::Socklen) -> [u8; 2] {
292 (domain.raw() as u16).to_ne_bytes()
293 }
294}
295
296impl option::Source for Uring {
297 const KEEP_IDLE: libc::c_int = libc::TCP_KEEPIDLE;
298}
299
300impl socket::Prepare for Uring {
301 type Handle = OwnedFd;
302 type Fast = num::NonZero<i32>;
303
304 fn fast_open(depth: Option<num::NonZero<i32>>) -> Result<Option<Self::Fast>, fast::Error> {
305 Ok(depth)
306 }
307
308 fn open(domain: socket::Domain, kind: socket::Kind) -> Result<Self::Handle, socket::Create> {
309 Self::socket(domain.raw(), kind.raw()).map_err(socket::Create::socket)
310 }
311
312 fn listener(domain: socket::Domain) -> Result<Self::Handle, socket::Create> {
313 Self::open(domain, socket::Kind::Stream)
314 }
315
316 fn adopt(file: OwnedFd) -> Result<Self::Handle, socket::Setup> {
317 <Self as Fd>::update::<Cloexec>(file.as_fd()).map_err(Setup::cloexec)?;
318 <Self as Fd>::remove::<Nonblock>(file.as_fd()).map_err(Setup::nonblock)?;
319 Ok(file)
320 }
321
322 fn into_fd(handle: Self::Handle) -> OwnedFd {
323 handle
324 }
325
326 fn set_fast(file: BorrowedFd<'_>, fast: Self::Fast) -> Result<(), Errno> {
327 Self::set_option(file, libc::IPPROTO_TCP, libc::TCP_FASTOPEN, &fast.get())
328 }
329}
330impl<T: Copy + Eq, S: Copy + Eq, F: Copy + Eq> Observe<T, S, F> for Uring {
331 type Observation = crate::Observed<T>;
332}
333impl tuning::Source for Uring {
334 type State<E: Copy, const N: usize> = crate::State<E, N>;
335 type Table<E: Copy, const N: usize> = Table<E, N>;
336}
337
338impl ring::Source for Uring {
339 type Decoded<T: Copy, E> = packet::Decoded<T, E, ()>;
340 type Ready<T: complete::raw::Token, L: memory::raw::Receive> = Ready<T, L>;
341 type Packet<T: Copy, B> = packet::Packet<T, B>;
342 type Poll = enter::Poll<kernel::Errno<error::Enter>, kernel::Errno<error::Register<24>>>;
343 type Enter<'a, T: 'a> = Enter<'a, T>;
344 type Entry<T> = cq::Entry<T>;
345 type Message<'a, const NAME: usize, const CONTROL: usize> = cq::Output<'a, NAME, CONTROL>;
346 type Ring<T: complete::raw::Token> = Ring<T>;
347}
348
349impl submit::Control for Uring {
350 type Target = sq::Fixed;
351 type Owned<T: complete::raw::Token> = sq::Owned<T>;
352 type Poll<'a, T: complete::raw::Token> = Poll<'a, T>;
353
354 fn nop<T: complete::raw::Token>(complete: T) -> Self::Owned<T> {
355 sq::Entry::new(sq::Entry::NOP, sq::Descriptor::File(-1)).user_data(complete)
356 }
357
358 fn cancel<T: complete::raw::Token>(target: T, complete: T) -> Self::Owned<T> {
359 use crate::reactor::op::controls;
360
361 controls::cancel(target, false)
362 .flags(sq::Flags::SKIP_SUCCESS)
363 .user_data(complete)
364 }
365
366 fn close<T: complete::raw::Token>(slot: Self::Target, complete: T) -> Self::Owned<T> {
367 use crate::reactor::op::files;
368
369 let mut entry = sq::Entry::new(files::CLOSE, sq::Descriptor::File(0));
370 entry.fields().index = slot.encode();
371 entry.user_data(complete)
372 }
373
374 fn shutdown<T: complete::raw::Token>(slot: Self::Target, complete: T) -> Self::Owned<T> {
375 use crate::reactor::op::sockets;
376
377 sockets::shutdown(slot, socket::How::BOTH)
378 .flags(sq::Flags::HARD_LINK | sq::Flags::SKIP_SUCCESS)
379 .user_data(complete)
380 }
381}
382
383impl probe::Source for Uring {
384 type Range = Range;
385 type Release<T: token::Codec, L: memory::raw::Canary, P: Copy> = Released<T, L, P>;
386 type Cleanup<T: token::Codec, L: memory::raw::Canary, P: Copy> = Recover<T, L, P>;
387 type Payload = Payload;
388 type Accept<T: token::Codec> = Accepted<T>;
389
390 type Session<T: token::Codec, L: memory::raw::Canary> = Session<T, L>;
391
392 type Outcome<T: complete::raw::Token, P: Copy> = Outcome<T, P>;
393 type Waiting<T: token::Codec, S> = Waiting<S, T>;
394}
395
396impl create::Source for Uring {
397 type Primary = Primary;
398 type Failure<T: token::Codec> = Fault<T>;
399 type Resources<
400 T: token::Codec,
401 L: memory::raw::Canary,
402 P: Copy + From<Self::Primary> + From<memory::Error>,
403 > = Resources<T, L, P>;
404}
405
406impl token::Source for Uring {
407 type Token<C: host::Context> = Encoded<C>;
408}
409
410impl Source for Uring {
411 type Socket<H> = Operation<H>;
412}
413
414impl admit::Native for Uring {
415 type Error<F> = Infallible;
416}
417
418impl<'a, C, R> life::Finish<'a, R> for Uring
419where
420 C: Context + 'a,
421 C::Tuning: Progress<C::Token>,
422 R: life::Reclaim<Backend = Engine<C>> + 'a,
423{
424 type Quiesced = Quiesced<R>;
425
426 fn try_finish(
427 resolver: R,
428 ) -> Result<Self::Quiesced, Stopped<R, Error<<Self as ring::Source>::Poll, R::Fault>>> {
429 Engine::try_finish(resolver)
430 }
431}
432
433impl<'a, C, R> life::Quiesce<'a, R> for Uring
434where
435 C: Context + 'a,
436 C::Tuning: Progress<C::Token>,
437 R: life::Cancel<Backend = Engine<C>> + 'a,
438 R::Cancelling: 'a,
439 R::Closing: 'a,
440{
441 type Quiesced = Quiesced<R::Closing>;
442
443 fn try_quiesce(
444 resolver: R,
445 ) -> Result<Self::Quiesced, Stopped<R::Closing, Error<<Self as ring::Source>::Poll, R::Fault>>>
446 {
447 use life::{Close as _, Finish as _, Resolver as _};
448
449 let mut resolver = resolver.cancel();
450 resolver.backend().tuning().cancel_all();
451 let result = resolver.backend().ring().submit().map_err(Error::File);
452 (resolver, ()) = Stopped::attach(resolver, result)
453 .map_err(|stopped| stopped.map(life::Close::into_closing))?;
454 let result = resolver.backend().ring().cancel_all().map_err(Error::File);
455 (resolver, ()) = Stopped::attach(resolver, result)
456 .map_err(|stopped| stopped.map(life::Close::into_closing))?;
457 resolver =
458 Engine::drain(resolver).map_err(|stopped| stopped.map(life::Close::into_closing))?;
459 if !resolver.backend().tuning().is_quiescent() {
460 return Err(Stopped {
461 state: resolver.into_closing(),
462 cause: Error::Tuning,
463 });
464 }
465 Self::try_finish(resolver.into_closing())
466 }
467}