1use std::{
2 cell::UnsafeCell,
3 convert::TryFrom,
4 fs::File,
5 io,
6 net::{TcpListener, TcpStream},
7 ops::Neg,
8 os::unix::io::{AsRawFd, FromRawFd},
9 sync::{
10 atomic::{
11 AtomicU32, AtomicU64,
12 Ordering::{Acquire, Relaxed, Release},
13 },
14 Arc, Condvar, Mutex,
15 },
16 mem::MaybeUninit
17};
18
19use super::{
20 pair, AsIoVec, AsIoVecMut, Completion, Filler, FromCqe,
21 Measure, M,
22};
23
24mod config;
25mod constants;
26mod cq;
27mod in_flight;
28mod kernel_types;
29mod sq;
30mod syscall;
31mod ticket_queue;
32mod uring;
33
34pub(crate) use {
35 constants::*,
36 cq::Cq,
37 in_flight::InFlight,
38 kernel_types::{
39 io_uring_cqe, io_uring_params, io_uring_sqe,
40 },
41 sq::Sq,
42 syscall::{enter, setup},
43 ticket_queue::TicketQueue,
44};
45
46pub use {
47 config::Config,
48 uring::{Rio, Uring},
49};
50
51#[derive(Clone, Debug, Copy)]
62pub enum Ordering {
63 None,
65 Link,
71 Drain,
75}
76
77fn uring_mmap(
78 size: usize,
79 ring_fd: i32,
80 offset: i64,
81) -> io::Result<*mut libc::c_void> {
82 #[allow(unsafe_code)]
83 let ptr = unsafe {
84 libc::mmap(
85 std::ptr::null_mut(),
86 size,
87 libc::PROT_READ | libc::PROT_WRITE,
88 libc::MAP_SHARED | libc::MAP_POPULATE,
89 ring_fd,
90 offset,
91 )
92 };
93
94 if ptr.is_null() || ptr == libc::MAP_FAILED {
95 let mut err = io::Error::last_os_error();
96 if let Some(12) = err.raw_os_error() {
97 err = io::Error::new(
98 io::ErrorKind::Other,
99 "Not enough lockable memory. You probably \
100 need to raise the memlock rlimit, which \
101 often defaults to a pretty low number.",
102 );
103 }
104 return Err(err);
105 }
106
107 Ok(ptr)
108}
109
110impl FromCqe for TcpStream {
111 fn from_cqe(cqe: io_uring_cqe) -> TcpStream {
112 #[allow(unsafe_code)]
113 unsafe {
114 TcpStream::from_raw_fd(cqe.res)
115 }
116 }
117}