Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rs/moq-quic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Changes on top of the upstream commit, besides the renames:
We extend it to resend batches built before GSO was halted too, which upstream's version drops, and to log only the first rejection.
A `WouldBlock` partway through the resend makes the caller retry the whole batch, duplicating the datagrams already sent; QUIC drops the duplicates.
Drop it if upstream lands [quinn#2748](https://github.com/quinn-rs/quinn/pull/2748) and we cherry-pick that.
- **Unreachable sends** (`moq_sock::udp::is_unreachable_err`, the driver's `UdpSenderHelper`): `ENETUNREACH`, `EHOSTUNREACH` and `ENETDOWN` reach the connection instead of being logged as loss. A handshaking connection fails at once with `NO_VIABLE_PATH`, so a dial race moves to the next address; an established one still counts them as loss. Addresses [quinn#2766](https://github.com/quinn-rs/quinn/issues/2766), reworked from [kixelated/quinn#3](https://github.com/kixelated/quinn/pull/3).
- **Unreachable sends** (`Connection::handle_unreachable`): a driver reports a send that fails with `ENETUNREACH`, `EHOSTUNREACH` or `ENETDOWN` (`moq_sock::udp::is_unreachable_err`) instead of logging it as loss. A handshaking connection fails at once with `NO_VIABLE_PATH` and sends no CONNECTION\_CLOSE, so a dial race moves to the next address; an established one ignores it and the datagram counts as loss. Addresses [quinn#2766](https://github.com/quinn-rs/quinn/issues/2766), reworked from [kixelated/quinn#3](https://github.com/kixelated/quinn/pull/3).
- Apple fast-path fallback preserves the segmentation of batches prepared before the fast path was disabled, including when a private symbol is unavailable.
- Discarding Initial or Handshake keys resets the PTO backoff, as RFC 9002 A.11 does, so a client's Initial backoff no longer delays the retransmission of a lost Finished. Also [moq-dev/noq#32](https://github.com/moq-dev/noq/pull/32); not offered upstream yet.
- `TransportConfig::handshake_idle_timeout` (default 10s, as msquic's `HandshakeIdleTimeoutMs`) governs the idle timer until the handshake completes, and the negotiated idle timeout takes over once established. A 2s idle timeout otherwise expires 3x the initial PTO in, just before the second Initial probe, leaving a client two flights. A server also drops an Initial left waiting for `accept` past it, and a deadline too far out to represent never fires instead of panicking. Also [moq-dev/noq#34](https://github.com/moq-dev/noq/pull/34), whose third change (the handshake idle timeout picks noq's idle-based PTO interval cap) has nothing to apply to here, since quinn's PTO backoff has no such cap.
Expand Down
12 changes: 12 additions & 0 deletions rs/moq-quic/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,18 @@ impl Connection {
)
}

/// Report that a send to the peer found no route, such as `ENETUNREACH`
///
/// A handshake can never finish, so it fails at once with `NO_VIABLE_PATH`, sending no
/// CONNECTION_CLOSE the peer could not receive anyway. An established connection is left
/// alone, since a route that drops for a moment (a client switching networks) comes back, so
/// the datagram is just lost.
pub fn handle_unreachable(&mut self) {
if self.state.is_handshake() {
self.kill(TransportError::NO_VIABLE_PATH("no route to the peer").into());
}
}

fn close_inner(&mut self, now: Instant, reason: Close) {
let was_closed = self.state.is_closed();
if !was_closed {
Expand Down
43 changes: 43 additions & 0 deletions rs/moq-quic/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2189,6 +2189,49 @@ fn idle_timeout() {
);
}

#[test]
fn unreachable_handshake_fails_with_no_viable_path() {
let _guard = subscribe();
let mut pair = Pair::default();
let client_ch = pair.begin_connect(client_config());

pair.client_conn_mut(client_ch).handle_unreachable();

let conn = pair.client_conn_mut(client_ch);
assert!(conn.is_drained());
assert_matches!(
conn.poll(),
Some(Event::ConnectionLost {
reason: ConnectionError::TransportError(TransportError {
code: TransportErrorCode::NO_VIABLE_PATH,
..
}),
})
);
// Not even a CONNECTION_CLOSE goes out.
pair.drive();
pair.server.assert_no_accept();
}

#[test]
fn unreachable_is_loss_once_established() {
let _guard = subscribe();
let mut pair = Pair::default();
let (client_ch, server_ch) = pair.connect();

pair.client_conn_mut(client_ch).handle_unreachable();

assert!(!pair.client_conn_mut(client_ch).is_closed());
assert_matches!(pair.client_conn_mut(client_ch).poll(), None);
let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap();
pair.client_send(client_ch, s).write(b"hello").unwrap();
pair.drive();
assert_matches!(
pair.server_conn_mut(server_ch).poll(),
Some(Event::Stream(StreamEvent::Opened { dir: Dir::Uni }))
);
}

#[test]
fn connection_close_sends_acks() {
let _guard = subscribe();
Expand Down
32 changes: 0 additions & 32 deletions rs/moq-sock/src/udp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ use std::{
net::UdpSocket,
os::fd::{AsRawFd, FromRawFd},
};
#[cfg(not(wasm_browser))]
use std::{sync::Mutex, time::Instant};
#[cfg(windows)]
use windows_sys::Win32::Networking::WinSock;

Expand Down Expand Up @@ -267,36 +265,6 @@ pub fn is_unreachable_err(err: &io::Error) -> bool {
)
}

/// Log at most 1 IO error per minute
#[cfg(not(wasm_browser))]
const IO_ERROR_LOG_INTERVAL: Duration = Duration::from_secs(60);

/// Logs a warning message when sendmsg fails
///
/// Logging will only be performed if at least [`IO_ERROR_LOG_INTERVAL`]
/// has elapsed since the last error was logged.
#[cfg(not(wasm_browser))]
fn log_sendmsg_error(
last_send_error: &Mutex<Instant>,
err: impl core::fmt::Debug,
transmit: &Transmit<'_>,
) {
let now = Instant::now();
let last_send_error = &mut *last_send_error.lock().expect("poisend lock");
if now.saturating_duration_since(*last_send_error) > IO_ERROR_LOG_INTERVAL {
*last_send_error = now;
log::warn!(
"sendmsg error: {:?}, Transmit: {{ destination: {:?}, src_ip: {:?}, ecn: {:?}, len: {:?}, segment_size: {:?} }}",
err,
transmit.destination,
transmit.src_ip,
transmit.ecn,
transmit.contents.len(),
transmit.segment_size
);
}
}

/// A borrowed UDP socket
///
/// On Unix, constructible via `From<T: AsFd>`. On Windows, constructible via `From<T:
Expand Down
42 changes: 3 additions & 39 deletions rs/moq-sock/src/udp/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@ use std::{
mem::{self, MaybeUninit},
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
os::fd::AsRawFd,
sync::{
Mutex,
atomic::{AtomicBool, AtomicUsize, Ordering},
},
time::{Duration, Instant},
sync::atomic::{AtomicBool, AtomicUsize, Ordering},
time::Duration,
};

use socket2::SockRef;

use super::{
EcnCodepoint, IO_ERROR_LOG_INTERVAL, RecvMeta, Transmit, TransportError, UdpSockRef, cmsg,
log_sendmsg_error,
};
use super::{EcnCodepoint, RecvMeta, Transmit, TransportError, UdpSockRef, cmsg};

#[cfg(apple_fast)]
use super::apple_fast::{msghdr_x, recv_via_recvmsg_x, send};
Expand All @@ -30,7 +24,6 @@ use super::linux::{LinuxError, gso};
/// platforms.
#[derive(Debug)]
pub struct UdpSocketState {
last_send_error: Mutex<Instant>,
max_gso_segments: AtomicUsize,
gro_segments: usize,
may_fragment: bool,
Expand Down Expand Up @@ -197,9 +190,7 @@ impl UdpSocketState {
let _ = io.set_send_buffer_size(Self::MIN_SAFE_SNDBUF);
}

let now = Instant::now();
Ok(Self {
last_send_error: Mutex::new(now.checked_sub(2 * IO_ERROR_LOG_INTERVAL).unwrap_or(now)),
max_gso_segments: AtomicUsize::new(gso::max_gso_segments(&*io)),
gro_segments,
may_fragment,
Expand All @@ -211,33 +202,6 @@ impl UdpSocketState {
})
}

/// Sends a [`Transmit`] on the given socket
///
/// This function will only ever return errors of kind [`io::ErrorKind::WouldBlock`].
/// All other errors will be logged and converted to `Ok`.
///
/// UDP transmission errors are considered non-fatal because higher-level protocols must
/// employ retransmits and timeouts anyway in order to deal with UDP's unreliable nature.
/// Thus, logging is most likely the only thing you can do with these errors.
///
/// If you would like to handle these errors yourself, use [`UdpSocketState::try_send`]
/// instead.
#[deprecated(note = "silences I/O errors; use `UdpSocketState::try_send() instead")]
pub fn send(&self, socket: UdpSockRef<'_>, transmit: &Transmit<'_>) -> io::Result<()> {
match send(self, socket.0, transmit) {
Ok(()) => Ok(()),
Err(e) if e.kind() == io::ErrorKind::WouldBlock => Err(e),
// - EMSGSIZE is expected for MTU probes. Future work might be able to avoid
// these by automatically clamping the MTUD upper bound to the interface MTU.
Err(e) if e.raw_os_error() == Some(libc::EMSGSIZE) => Ok(()),
Err(e) => {
log_sendmsg_error(&self.last_send_error, e, transmit);

Ok(())
}
}
}

/// Sends a [`Transmit`] on the given socket without any additional error handling
pub fn try_send(&self, socket: UdpSockRef<'_>, transmit: &Transmit<'_>) -> io::Result<()> {
send(self, socket.0, transmit)
Expand Down
35 changes: 3 additions & 32 deletions rs/moq-sock/src/udp/wasi.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,23 @@
use std::{
io::{self, IoSliceMut},
mem::MaybeUninit,
sync::Mutex,
time::Instant,
};

use super::{IO_ERROR_LOG_INTERVAL, RecvMeta, Transmit, UdpSockRef, log_sendmsg_error};
use super::{RecvMeta, Transmit, UdpSockRef};

/// UDP socket interface for WASI
///
/// `wasi:sockets` exposes none of this crate's advanced features, so they are stubbed out.
#[derive(Debug)]
pub struct UdpSocketState {
last_send_error: Mutex<Instant>,
_private: (),
}

impl UdpSocketState {
pub fn new(socket: UdpSockRef<'_>) -> io::Result<Self> {
socket.set_nonblocking(true)?;

let now = Instant::now();
Ok(Self {
last_send_error: Mutex::new(now.checked_sub(2 * IO_ERROR_LOG_INTERVAL).unwrap_or(now)),
})
}

/// Sends a [`Transmit`] on the given socket.
///
/// This function will only ever return errors of kind [`io::ErrorKind::WouldBlock`].
/// All other errors will be logged and converted to `Ok`.
///
/// UDP transmission errors are considered non-fatal because higher-level protocols must
/// employ retransmits and timeouts anyway in order to deal with UDP's unreliable nature.
/// Thus, logging is most likely the only thing you can do with these errors.
///
/// If you would like to handle these errors yourself, use [`UdpSocketState::try_send`]
/// instead.
#[deprecated(note = "silences I/O errors; use `UdpSocketState::try_send() instead")]
pub fn send(&self, socket: UdpSockRef<'_>, transmit: &Transmit<'_>) -> io::Result<()> {
match send(socket, transmit) {
Ok(()) => Ok(()),
Err(e) if e.kind() == io::ErrorKind::WouldBlock => Err(e),
Err(e) => {
log_sendmsg_error(&self.last_send_error, e, transmit);

Ok(())
}
}
Ok(Self { _private: () })
}

/// Sends a [`Transmit`] on the given socket without any additional error handling.
Expand Down
41 changes: 2 additions & 39 deletions rs/moq-sock/src/udp/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,23 @@ use std::{
net::{IpAddr, Ipv4Addr},
os::windows::io::AsRawSocket,
ptr,
sync::{
Mutex,
atomic::{AtomicUsize, Ordering},
},
time::Instant,
sync::atomic::{AtomicUsize, Ordering},
};

use libc::{c_int, c_uint};
use windows_sys::Win32::Networking::WinSock;

use crate::udp::{
EcnCodepoint, IO_ERROR_LOG_INTERVAL, RecvMeta, Transmit, UdpSockRef,
EcnCodepoint, RecvMeta, Transmit, UdpSockRef,
cmsg::{self, CMsgHdr},
log::debug,
log_sendmsg_error,
};

/// QUIC-friendly UDP socket for Windows
///
/// Unlike a standard Windows UDP socket, this allows ECN bits to be read and written.
#[derive(Debug)]
pub struct UdpSocketState {
last_send_error: Mutex<Instant>,
max_gso_segments: AtomicUsize,

/// Whether the underlying Winsock provider supports IPv4 ECN socket options/control messages.
Expand Down Expand Up @@ -152,9 +146,7 @@ impl UdpSocketState {
}
}

let now = Instant::now();
Ok(Self {
last_send_error: Mutex::new(now.checked_sub(2 * IO_ERROR_LOG_INTERVAL).unwrap_or(now)),
max_gso_segments: AtomicUsize::new(max_gso_segments(&*socket.0)),
ecn_v4_supported,
ecn_v6_supported,
Expand Down Expand Up @@ -184,35 +176,6 @@ impl UdpSocketState {
)
}

/// Sends a [`Transmit`] on the given socket.
///
/// This function will only ever return errors of kind [`io::ErrorKind::WouldBlock`].
/// All other errors will be logged and converted to `Ok`.
///
/// UDP transmission errors are considered non-fatal because higher-level protocols must
/// employ retransmits and timeouts anyway in order to deal with UDP's unreliable nature.
/// Thus, logging is most likely the only thing you can do with these errors.
///
/// If you would like to handle these errors yourself, use [`UdpSocketState::try_send`]
/// instead.
#[deprecated(note = "silences I/O errors; use `UdpSocketState::try_send() instead")]
pub fn send(&self, socket: UdpSockRef<'_>, transmit: &Transmit<'_>) -> io::Result<()> {
match send(
socket,
transmit,
self.ecn_v4_supported,
self.ecn_v6_supported,
) {
Ok(()) => Ok(()),
Err(e) if e.kind() == io::ErrorKind::WouldBlock => Err(e),
Err(e) => {
log_sendmsg_error(&self.last_send_error, e, transmit);

Ok(())
}
}
}

/// Sends a [`Transmit`] on the given socket without any additional error handling.
pub fn try_send(&self, socket: UdpSockRef<'_>, transmit: &Transmit<'_>) -> io::Result<()> {
send(
Expand Down
Loading
Loading