|
1 | 1 | pub(crate) mod inside;
|
2 | 2 | pub(crate) mod outside;
|
| 3 | + |
| 4 | +mod ffi; |
| 5 | +mod tx; |
| 6 | + |
| 7 | +use std::{ |
| 8 | + os::fd::{AsRawFd, OwnedFd, RawFd}, |
| 9 | + sync::{Arc, Mutex}, |
| 10 | +}; |
| 11 | + |
| 12 | +use anyhow::{anyhow, Result}; |
| 13 | +use io_uring::{ |
| 14 | + cqueue::Entry as CEntry, |
| 15 | + opcode, |
| 16 | + squeue::Entry as SEntry, |
| 17 | + types::{Fd, Fixed}, |
| 18 | + IoUring, SubmissionQueue, Submitter, |
| 19 | +}; |
| 20 | + |
| 21 | +use ffi::{iovec, msghdr}; |
| 22 | +pub use tx::TxQueue; |
| 23 | + |
| 24 | +const IOURING_SQPOLL_IDLE_TIME_MS: u32 = 100; |
| 25 | + |
| 26 | +/// Convenience function to handle errors in a uring result codes |
| 27 | +/// (which are negative errno codes). |
| 28 | +fn io_uring_res(res: i32) -> std::io::Result<i32> { |
| 29 | + if res < 0 { |
| 30 | + Err(std::io::Error::from_raw_os_error(-res)) |
| 31 | + } else { |
| 32 | + Ok(res) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/// An I/O source pushing requests to a uring instance |
| 37 | +pub(crate) trait UringIoSource: Send { |
| 38 | + /// Return the raw file descriptor. This will be registered as an |
| 39 | + /// fd with the ring, allowing the use of io_uring::types::Fixed. |
| 40 | + fn as_raw_fd(&self) -> RawFd; |
| 41 | + |
| 42 | + /// Push the initial set of requests to `sq`. |
| 43 | + fn push_initial_ops(&mut self, sq: &mut io_uring::SubmissionQueue) -> Result<()>; |
| 44 | + |
| 45 | + /// Complete an rx request |
| 46 | + fn complete_rx( |
| 47 | + &mut self, |
| 48 | + sq: &mut io_uring::SubmissionQueue, |
| 49 | + cqe: io_uring::cqueue::Entry, |
| 50 | + idx: u32, |
| 51 | + ) -> Result<()>; |
| 52 | + |
| 53 | + /// Complete a tx request |
| 54 | + fn complete_tx( |
| 55 | + &mut self, |
| 56 | + sq: &mut io_uring::SubmissionQueue, |
| 57 | + cqe: io_uring::cqueue::Entry, |
| 58 | + idx: u32, |
| 59 | + ) -> Result<()>; |
| 60 | +} |
| 61 | + |
| 62 | +pub(crate) enum OutsideIoSource { |
| 63 | + Udp(outside::udp::UdpServer), |
| 64 | + Tcp(outside::tcp::TcpServer), |
| 65 | +} |
| 66 | + |
| 67 | +// Avoiding `dyn`amic dispatch is a small performance win. |
| 68 | +impl UringIoSource for OutsideIoSource { |
| 69 | + fn as_raw_fd(&self) -> RawFd { |
| 70 | + match self { |
| 71 | + OutsideIoSource::Udp(udp) => udp.as_raw_fd(), |
| 72 | + OutsideIoSource::Tcp(tcp) => tcp.as_raw_fd(), |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + fn push_initial_ops(&mut self, sq: &mut io_uring::SubmissionQueue) -> Result<()> { |
| 77 | + match self { |
| 78 | + OutsideIoSource::Udp(udp) => udp.push_initial_ops(sq), |
| 79 | + OutsideIoSource::Tcp(tcp) => tcp.push_initial_ops(sq), |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + fn complete_rx( |
| 84 | + &mut self, |
| 85 | + sq: &mut io_uring::SubmissionQueue, |
| 86 | + cqe: io_uring::cqueue::Entry, |
| 87 | + idx: u32, |
| 88 | + ) -> Result<()> { |
| 89 | + match self { |
| 90 | + OutsideIoSource::Udp(udp) => udp.complete_rx(sq, cqe, idx), |
| 91 | + OutsideIoSource::Tcp(tcp) => tcp.complete_rx(sq, cqe, idx), |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + fn complete_tx( |
| 96 | + &mut self, |
| 97 | + sq: &mut io_uring::SubmissionQueue, |
| 98 | + cqe: io_uring::cqueue::Entry, |
| 99 | + idx: u32, |
| 100 | + ) -> Result<()> { |
| 101 | + match self { |
| 102 | + OutsideIoSource::Udp(udp) => udp.complete_tx(sq, cqe, idx), |
| 103 | + OutsideIoSource::Tcp(tcp) => tcp.complete_tx(sq, cqe, idx), |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +pub(crate) struct Loop { |
| 109 | + ring: IoUring, |
| 110 | + |
| 111 | + tx: Arc<Mutex<TxQueue>>, |
| 112 | + |
| 113 | + cancel_buf: u8, |
| 114 | + |
| 115 | + outside: OutsideIoSource, |
| 116 | + inside: inside::tun::Tun, |
| 117 | +} |
| 118 | + |
| 119 | +impl Loop { |
| 120 | + /// Use for outside IO requests, `self.outside.as_raw_fd` will be registered in this slot. |
| 121 | + const FIXED_OUTSIDE_FD: Fixed = Fixed(0); |
| 122 | + /// Use for inside IO requests, `self.inside.as_raw_fd` will be registered in this slot. |
| 123 | + const FIXED_INSIDE_FD: Fixed = Fixed(1); |
| 124 | + |
| 125 | + /// Masks the bits used by `*_USER_DATA_BASE` |
| 126 | + const USER_DATA_TYPE_MASK: u64 = 0xe000_0000_0000_0000; |
| 127 | + |
| 128 | + /// Indexes in this range will result in a call to `self.outside.complete_rx` |
| 129 | + const OUTSIDE_RX_USER_DATA_BASE: u64 = 0xc000_0000_0000_0000; |
| 130 | + /// Indexes in this range will result in a call to `self.outside.complete_tx` |
| 131 | + const OUTSIDE_TX_USER_DATA_BASE: u64 = 0x8000_0000_0000_0000; |
| 132 | + |
| 133 | + /// Indexes in this range will result in a call to `self.inside.complete_rx` |
| 134 | + const INSIDE_RX_USER_DATA_BASE: u64 = 0x4000_0000_0000_0000; |
| 135 | + /// Indexes in this range will result in a call to `self.inside.complete_tx` |
| 136 | + const INSIDE_TX_USER_DATA_BASE: u64 = 0x2000_0000_0000_0000; |
| 137 | + |
| 138 | + /// Indexes in this range are used by `Loop` itself. |
| 139 | + const CONTROL_USER_DATA_BASE: u64 = 0x0000_0000_0000_0000; |
| 140 | + |
| 141 | + /// A read request on the cancellation fd (used to exit the io loop) |
| 142 | + const CANCEL_USER_DATA: u64 = Self::CONTROL_USER_DATA_BASE + 1; |
| 143 | + |
| 144 | + /// Return user data for a particular outside rx index. |
| 145 | + fn outside_rx_user_data(idx: u32) -> u64 { |
| 146 | + Self::OUTSIDE_RX_USER_DATA_BASE + (idx as u64) |
| 147 | + } |
| 148 | + |
| 149 | + /// Return user data for a particular inside rx index. |
| 150 | + fn inside_rx_user_data(idx: u32) -> u64 { |
| 151 | + Self::INSIDE_RX_USER_DATA_BASE + (idx as u64) |
| 152 | + } |
| 153 | + |
| 154 | + /// Return user data for a particular outside tx index. |
| 155 | + fn inside_tx_user_data(idx: u32) -> u64 { |
| 156 | + Self::INSIDE_TX_USER_DATA_BASE + (idx as u64) |
| 157 | + } |
| 158 | + |
| 159 | + /// Return user data for a particular inside tx index. |
| 160 | + fn outside_tx_user_data(idx: u32) -> u64 { |
| 161 | + Self::OUTSIDE_TX_USER_DATA_BASE + (idx as u64) |
| 162 | + } |
| 163 | + |
| 164 | + pub(crate) fn new( |
| 165 | + ring_size: usize, |
| 166 | + tx: Arc<Mutex<TxQueue>>, |
| 167 | + outside: OutsideIoSource, |
| 168 | + inside: inside::tun::Tun, |
| 169 | + ) -> Result<Self> { |
| 170 | + tracing::info!(ring_size, "creating IoUring"); |
| 171 | + let ring: IoUring<SEntry, CEntry> = IoUring::builder() |
| 172 | + .dontfork() |
| 173 | + .setup_sqpoll(IOURING_SQPOLL_IDLE_TIME_MS) // Needs 5.13 |
| 174 | + .build(ring_size as u32)?; |
| 175 | + |
| 176 | + Ok(Self { |
| 177 | + ring, |
| 178 | + tx, |
| 179 | + cancel_buf: 0, |
| 180 | + outside, |
| 181 | + inside, |
| 182 | + }) |
| 183 | + } |
| 184 | + |
| 185 | + pub(crate) fn run(mut self, cancel: OwnedFd) -> Result<()> { |
| 186 | + let (submitter, mut sq, mut cq) = self.ring.split(); |
| 187 | + |
| 188 | + submitter.register_files(&[self.outside.as_raw_fd(), self.inside.as_raw_fd()])?; |
| 189 | + |
| 190 | + let sqe = opcode::Read::new( |
| 191 | + Fd(cancel.as_raw_fd()), |
| 192 | + &mut self.cancel_buf as *mut _, |
| 193 | + std::mem::size_of_val(&self.cancel_buf) as _, |
| 194 | + ) |
| 195 | + .build() |
| 196 | + .user_data(Self::CANCEL_USER_DATA); |
| 197 | + |
| 198 | + #[allow(unsafe_code)] |
| 199 | + // SAFETY: The buffer is owned by `self.cancel_buf` and `self` is owned |
| 200 | + unsafe { |
| 201 | + sq.push(&sqe)? |
| 202 | + }; |
| 203 | + |
| 204 | + self.outside.push_initial_ops(&mut sq)?; |
| 205 | + self.inside.push_initial_ops(&mut sq)?; |
| 206 | + sq.sync(); |
| 207 | + |
| 208 | + loop { |
| 209 | + let _ = submitter.submit_and_wait(1)?; |
| 210 | + |
| 211 | + cq.sync(); |
| 212 | + |
| 213 | + for cqe in &mut cq { |
| 214 | + let user_data = cqe.user_data(); |
| 215 | + |
| 216 | + match user_data & Self::USER_DATA_TYPE_MASK { |
| 217 | + Self::CONTROL_USER_DATA_BASE => { |
| 218 | + match user_data - Self::CONTROL_USER_DATA_BASE { |
| 219 | + Self::CANCEL_USER_DATA => { |
| 220 | + let res = cqe.result(); |
| 221 | + tracing::debug!(?res, "Uring cancelled"); |
| 222 | + return Ok(()); |
| 223 | + } |
| 224 | + idx => { |
| 225 | + return Err(anyhow!( |
| 226 | + "Unknown control data {user_data:016x} => {idx:016x}" |
| 227 | + )) |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | + Self::OUTSIDE_RX_USER_DATA_BASE => { |
| 232 | + self.outside.complete_rx( |
| 233 | + &mut sq, |
| 234 | + cqe, |
| 235 | + (user_data - Self::OUTSIDE_RX_USER_DATA_BASE) as u32, |
| 236 | + )?; |
| 237 | + } |
| 238 | + Self::OUTSIDE_TX_USER_DATA_BASE => { |
| 239 | + self.outside.complete_tx( |
| 240 | + &mut sq, |
| 241 | + cqe, |
| 242 | + (user_data - Self::OUTSIDE_TX_USER_DATA_BASE) as u32, |
| 243 | + )?; |
| 244 | + } |
| 245 | + |
| 246 | + Self::INSIDE_RX_USER_DATA_BASE => { |
| 247 | + self.inside.complete_rx( |
| 248 | + &mut sq, |
| 249 | + cqe, |
| 250 | + (user_data - Self::INSIDE_RX_USER_DATA_BASE) as u32, |
| 251 | + )?; |
| 252 | + } |
| 253 | + Self::INSIDE_TX_USER_DATA_BASE => { |
| 254 | + self.inside.complete_tx( |
| 255 | + &mut sq, |
| 256 | + cqe, |
| 257 | + (user_data - Self::INSIDE_TX_USER_DATA_BASE) as u32, |
| 258 | + )?; |
| 259 | + } |
| 260 | + |
| 261 | + _ => unreachable!(), |
| 262 | + } |
| 263 | + |
| 264 | + self.tx.lock().unwrap().drain(&submitter, &mut sq)?; |
| 265 | + } |
| 266 | + } |
| 267 | + } |
| 268 | +} |
0 commit comments