diff --git a/quest/m1/quic/README.md b/quest/m1/quic/README.md index 06bd6a53d82..1d8fe78858f 100644 --- a/quest/m1/quic/README.md +++ b/quest/m1/quic/README.md @@ -75,7 +75,6 @@ cache limits remain separate; bitrate pacing composes with this memory cap. and MAX_DATA are raised after SETUP identifies a cluster peer - [Shard the endpoint](/quest/m1/quic/shard.md) - the library shards a server endpoint across cores by default, replacing the `--workers` group -- [Surface UDP send errors](/quest/m1/quic/send-errors.md) - a client drops a QUIC attempt the moment its address family is unreachable ## Related diff --git a/quest/m1/quic/send-errors.md b/quest/m1/quic/send-errors.md deleted file mode 100644 index db6bb1e1e8c..00000000000 --- a/quest/m1/quic/send-errors.md +++ /dev/null @@ -1,25 +0,0 @@ -# [S] Surface UDP send errors so the client can fall back - -## Goal - -When a UDP send fails because the address family is unreachable (for -example an IPv6 target on a host without IPv6 routing), moq-tokio's QUIC -endpoint reports the error instead of swallowing it, and moq-tokio's dial race drops -that QUIC attempt at once rather than waiting out the handshake timeout. - -## Plan - -Rebase [kixelated/quinn#3](https://github.com/kixelated/quinn/pull/3) onto -`moq_sock::udp` and moq-tokio's imported quinn layer. It is 154 commits behind quinn main and -never went upstream; it touches only the udp and tokio crates, not the -sans-IO core. Decided 2026-09-30: an m1 quest after the switch, since the -client connect path is user-facing. - -Only fatal, destination-specific errors end the attempt; transient ones -(`ENOBUFS`, `EAGAIN`) stay handled as packet loss, as today. Test the fallback with a dial -against an unroutable family, and check that moq-uring maps the same errors -through its ring. - -## Related - -- [quinn#2766](https://github.com/quinn-rs/quinn/issues/2766) - the upstream issue diff --git a/rs/moq-quic/README.md b/rs/moq-quic/README.md index 7b7971bbcd4..26c9b544068 100644 --- a/rs/moq-quic/README.md +++ b/rs/moq-quic/README.md @@ -60,6 +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). - 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. diff --git a/rs/moq-sock/src/udp/mod.rs b/rs/moq-sock/src/udp/mod.rs index 5ff3398cbf9..ccdaff88fc3 100644 --- a/rs/moq-sock/src/udp/mod.rs +++ b/rs/moq-sock/src/udp/mod.rs @@ -247,6 +247,26 @@ pub fn is_msg_size_err(err: &io::Error) -> bool { } } +/// Returns true if the given send error means the local stack has no way to reach the destination +/// +/// Most send errors are transient, so higher layers treat them as packet loss. These are not: +/// there is no route to the address (ENETUNREACH, EHOSTUNREACH) or the interface is down +/// (ENETDOWN), and the Windows `WSAE*` equivalents. Retransmitting cannot fix them, so a +/// connection attempt can give up on that address at once, for example to dial the other address +/// family. +/// +/// They are only local for an unconnected socket with transport errors left disabled, as +/// `UdpSocketState` sockets are by default. A `connect`ed socket, or one with `IP_RECVERR`, can +/// report a received ICMP error here instead, which an off-path attacker can forge. +pub fn is_unreachable_err(err: &io::Error) -> bool { + matches!( + err.kind(), + io::ErrorKind::NetworkUnreachable + | io::ErrorKind::HostUnreachable + | io::ErrorKind::NetworkDown + ) +} + /// Log at most 1 IO error per minute #[cfg(not(wasm_browser))] const IO_ERROR_LOG_INTERVAL: Duration = Duration::from_secs(60); @@ -357,6 +377,27 @@ mod tests { use super::*; + #[cfg(unix)] + #[test] + fn unreachable_errors_are_only_the_routing_failures() { + for code in [libc::ENETUNREACH, libc::EHOSTUNREACH, libc::ENETDOWN] { + let err = io::Error::from_raw_os_error(code); + assert!(is_unreachable_err(&err), "{err}"); + } + // Loss, readiness, MTU probes, the GSO fallback, and ICMP noise all stay retryable. + for code in [ + libc::ENOBUFS, + libc::EAGAIN, + libc::EMSGSIZE, + libc::EINVAL, + libc::EIO, + libc::ECONNREFUSED, + ] { + let err = io::Error::from_raw_os_error(code); + assert!(!is_unreachable_err(&err), "{err}"); + } + } + #[test] fn effective_segment_size() { assert_eq!( diff --git a/rs/moq-tokio/src/quic/driver/connection.rs b/rs/moq-tokio/src/quic/driver/connection.rs index e86cecef417..056ea5719c9 100644 --- a/rs/moq-tokio/src/quic/driver/connection.rs +++ b/rs/moq-tokio/src/quic/driver/connection.rs @@ -266,12 +266,30 @@ impl Future for ConnectionDriver { conn.terminate(e, &self.conn.shared); return Poll::Ready(Ok(())); } - let mut keep_going = conn.drive_transmit(cx).inspect_err(|_| { - // Transmit failed, so close the connection and clean state before returning the error. - if !conn.inner.is_closed() { - conn.implicit_close(&self.conn.shared); + let mut keep_going = match conn.drive_transmit(cx) { + Ok(keep_going) => keep_going, + // No route to the peer, so the handshake can never finish: fail it now rather than at + // the handshake timeout, which lets a dial race move on to the next address. The + // driver keeps running until the closed connection drains, so the endpoint forgets it + // even while the application still holds a handle. + Err(e) if moq_sock::udp::is_unreachable_err(&e) => { + if !conn.inner.is_closed() { + // Closed, so dropping the last handle cannot overwrite the cause. + conn.inner.close(Instant::now(), 0u32.into(), Bytes::new()); + let cause = + TransportError::new(TransportErrorCode::NO_VIABLE_PATH, e.to_string()); + conn.terminate(cause.into(), &self.conn.shared); + } + true + } + Err(e) => { + // Transmit failed, so close the connection and clean state before returning the error. + if !conn.inner.is_closed() { + conn.implicit_close(&self.conn.shared); + } + return Poll::Ready(Err(e)); } - })?; + }; // If a timer expires, there might be more to transmit. When we transmit something, we // might need to reset a timer. Hence, we must loop until neither happens. keep_going |= conn.drive_timer(cx); @@ -1176,6 +1194,13 @@ impl State { self.buffered_transmit = Some(t); return Ok(false); } + // An established connection outlives a route that drops for a moment, such as a + // client switching networks, so the datagram is just lost. + Poll::Ready(Err(e)) + if moq_sock::udp::is_unreachable_err(&e) && !self.inner.is_handshaking() => + { + tracing::debug!(%e, "transmit failed; treating it as loss"); + } Poll::Ready(Err(e)) => return Err(e), Poll::Ready(Ok(())) => {} } diff --git a/rs/moq-tokio/src/quic/driver/runtime/mod.rs b/rs/moq-tokio/src/quic/driver/runtime/mod.rs index cc89f2f90e8..3f806a98367 100644 --- a/rs/moq-tokio/src/quic/driver/runtime/mod.rs +++ b/rs/moq-tokio/src/quic/driver/runtime/mod.rs @@ -66,6 +66,10 @@ pub trait UdpSender: Send + Sync + Debug + 'static { /// /// A single [`UdpSender`] will be re-used, even if `poll_send` returns `Poll::Ready` once, /// unlike [`Future::poll`], so calling it again after readiness should not panic. + /// + /// An error matching [`moq_sock::udp::is_unreachable_err`] fails a handshaking connection + /// with `NO_VIABLE_PATH` and counts as loss once it is established; any other error closes + /// the connection. fn poll_send( self: Pin<&mut Self>, transmit: &Transmit<'_>, @@ -162,6 +166,9 @@ where // registers us for a wakeup, or the send succeeds if this really was just a // transient failure. Err(e) if e.kind() == io::ErrorKind::WouldBlock => continue, + // No route to the destination: retransmitting cannot help, so the connection + // decides whether to give up on the peer. + Err(e) if moq_sock::udp::is_unreachable_err(&e) => return Poll::Ready(Err(e)), Err(e) => { log_sendmsg_error(this.last_send_error, &e, transmit); return Poll::Ready(Ok(())); @@ -235,28 +242,50 @@ mod tests { #[test] fn non_would_block_send_errors_are_logged_and_ignored() { - let mut sender = Box::pin(UdpSenderHelper::new(TestSocket, writable)); - let transmit = Transmit { - destination: SocketAddr::from(([127, 0, 0, 1], 4433)), - ecn: None, - contents: &[], - segment_size: None, - src_ip: None, - }; + let mut sender = Box::pin(UdpSenderHelper::new( + TestSocket(io::ErrorKind::PermissionDenied), + writable, + )); let mut cx = Context::from_waker(Waker::noop()); - let result = sender.as_mut().poll_send(&transmit, &mut cx); + let result = sender.as_mut().poll_send(&transmit(), &mut cx); assert!(matches!(result, Poll::Ready(Ok(())))); assert!(sender.last_send_error.is_some()); } + #[test] + fn unreachable_send_errors_are_returned() { + let mut sender = Box::pin(UdpSenderHelper::new( + TestSocket(io::ErrorKind::NetworkUnreachable), + writable, + )); + let mut cx = Context::from_waker(Waker::noop()); + + let result = sender.as_mut().poll_send(&transmit(), &mut cx); + + assert!( + matches!(result, Poll::Ready(Err(ref e)) if e.kind() == io::ErrorKind::NetworkUnreachable), + "{result:?}" + ); + } + + fn transmit() -> Transmit<'static> { + Transmit { + destination: SocketAddr::from(([127, 0, 0, 1], 4433)), + ecn: None, + contents: &[], + segment_size: None, + src_ip: None, + } + } + #[derive(Debug)] - struct TestSocket; + struct TestSocket(io::ErrorKind); impl UdpSenderHelperSocket for TestSocket { fn try_send(&self, _transmit: &Transmit<'_>) -> io::Result<()> { - Err(io::ErrorKind::PermissionDenied.into()) + Err(self.0.into()) } fn max_transmit_segments(&self) -> usize { diff --git a/rs/moq-tokio/src/quic/driver/tests.rs b/rs/moq-tokio/src/quic/driver/tests.rs index aae7054d83a..4de4f319d8c 100755 --- a/rs/moq-tokio/src/quic/driver/tests.rs +++ b/rs/moq-tokio/src/quic/driver/tests.rs @@ -1906,3 +1906,202 @@ unsafe fn drop_waker(data: *const ()) { // SAFETY: pointer always comes from `Arc::into_raw()` (see `raw_waker()`) drop(unsafe { Arc::::from_raw(data as *const WakeCounter) }); } + +/// A dial to an address the host cannot route fails at once with `NO_VIABLE_PATH`, rather than at +/// the handshake timeout, so a dial race can move on to the next address. +/// +/// Linux refuses a v4-mapped destination on an IPv6-only socket with `ENETUNREACH`, which makes an +/// unroutable family without touching the host's routes. +#[cfg(target_os = "linux")] +#[tokio::test] +async fn unroutable_dial_fails_with_no_viable_path() { + let _guard = subscribe(); + + let socket = socket2::Socket::new( + socket2::Domain::IPV6, + socket2::Type::DGRAM, + Some(socket2::Protocol::UDP), + ) + .unwrap(); + socket.set_only_v6(true).unwrap(); + socket + .bind(&SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 0).into()) + .unwrap(); + let client = Endpoint::new(EndpointConfig::default(), None, socket.into()).unwrap(); + + let cert = rcgen::generate_simple_self_signed(vec!["localhost".into()]).unwrap(); + let mut roots = RootCertStore::empty(); + roots.add(cert.cert.into()).unwrap(); + let client_config = ClientConfig::with_root_certificates(Arc::new(roots)).unwrap(); + + let unroutable = SocketAddr::new(IpAddr::V6(Ipv4Addr::LOCALHOST.to_ipv6_mapped()), 1); + let err = client + .connect_with(client_config, unroutable, "localhost") + .unwrap() + .await + .expect_err("nothing can be sent to an unroutable address"); + + // A swallowed send error waits out the handshake instead and reports `TimedOut`. + let crate::quic::driver::ConnectionError::TransportError(err) = err else { + panic!("expected a transport error, got {err:?}"); + }; + assert_eq!(err.code, moq_quic::TransportErrorCode::NO_VIABLE_PATH); +} + +/// Once established, a connection treats an unreachable destination as loss: a route that drops +/// for a moment, as when a client switches networks, must not end the connection. +#[tokio::test] +async fn unreachable_sends_are_loss_once_established() { + let _guard = subscribe(); + + let factory = EndpointFactory::new(); + let server = factory.endpoint(); + let outage = Arc::new(Outage::default()); + let client = Endpoint::new_with_abstract_socket( + EndpointConfig::default(), + None, + Box::new(UnreachableSocket { + inner: localhost_socket(), + outage: outage.clone(), + }), + ) + .unwrap(); + let mut roots = RootCertStore::empty(); + roots.add(factory.cert.cert.der().clone()).unwrap(); + client + .set_default_client_config(ClientConfig::with_root_certificates(Arc::new(roots)).unwrap()); + + let (client_conn, server_conn) = join!( + async { + client + .connect(server.local_addr().unwrap(), "localhost") + .unwrap() + .await + }, + async { server.accept().await.unwrap().await } + ); + let client_conn = client_conn.unwrap(); + let server_conn = server_conn.unwrap(); + + outage.active.store(true, Ordering::SeqCst); + let mut send = client_conn.open_uni().await.unwrap(); + send.write_all(b"hello").await.unwrap(); + send.finish().unwrap(); + outage.dropped.notified().await; + outage.active.store(false, Ordering::SeqCst); + + // The stream arrives on a retransmission once the route is back. + let received = async { + let mut recv = server_conn.accept_uni().await.unwrap(); + recv.read_to_end(usize::MAX).await.unwrap() + }; + tokio::select! { + received = received => assert_eq!(received, b"hello"), + reason = client_conn.closed() => panic!("the outage closed the connection: {reason}"), + } +} + +/// A handshake that fails on an unreachable peer still drains out of its endpoint while the +/// application holds the connection, here a server's 0.5-RTT handle. +#[tokio::test(start_paused = true)] +async fn unreachable_handshake_drains_while_held() { + let _guard = subscribe(); + + let factory = EndpointFactory::new(); + let key = PrivateKeyDer::Pkcs8(factory.cert.signing_key.serialize_der().into()); + let server_config = crate::quic::driver::ServerConfig::with_single_cert( + vec![factory.cert.cert.der().clone()], + key, + ) + .unwrap(); + let outage = Arc::new(Outage::default()); + outage.active.store(true, Ordering::SeqCst); + let server = Endpoint::new_with_abstract_socket( + EndpointConfig::default(), + Some(server_config), + Box::new(UnreachableSocket { + inner: localhost_socket(), + outage: outage.clone(), + }), + ) + .unwrap(); + let client = factory.endpoint(); + + let connecting = client + .connect(server.local_addr().unwrap(), "localhost") + .unwrap(); + let incoming = server.accept().await.unwrap(); + let conn = incoming.accept().unwrap().into_0rtt().unwrap(); + // Stop the client retrying, which would only start new handshakes. + client.close(0u32.into(), b"done"); + drop(connecting); + + let crate::quic::driver::ConnectionError::TransportError(err) = conn.closed().await else { + panic!("expected a transport error"); + }; + assert_eq!(err.code, moq_quic::TransportErrorCode::NO_VIABLE_PATH); + timeout(Duration::from_secs(60), server.wait_idle()) + .await + .expect("the failed connection never drained out of the endpoint"); + drop(conn); +} + +/// Whether [`UnreachableSocket`] is refusing sends, and a signal once it has refused one. +#[derive(Debug, Default)] +struct Outage { + active: AtomicBool, + dropped: tokio::sync::Notify, +} + +/// Wraps a socket so that a test can make every send fail as though the peer had no route. +#[derive(Debug)] +struct UnreachableSocket { + inner: Box, + outage: Arc, +} + +impl AsyncUdpSocket for UnreachableSocket { + fn create_sender(&self) -> Pin> { + Box::pin(UnreachableSender { + inner: self.inner.create_sender(), + outage: self.outage.clone(), + }) + } + + fn poll_recv( + &mut self, + cx: &mut Context<'_>, + bufs: &mut [IoSliceMut<'_>], + meta: &mut [RecvMeta], + ) -> Poll> { + self.inner.poll_recv(cx, bufs, meta) + } + + fn local_addr(&self) -> io::Result { + self.inner.local_addr() + } +} + +#[derive(Debug)] +struct UnreachableSender { + inner: Pin>, + outage: Arc, +} + +impl UdpSender for UnreachableSender { + fn poll_send( + mut self: Pin<&mut Self>, + transmit: &moq_sock::udp::Transmit<'_>, + cx: &mut Context<'_>, + ) -> Poll> { + if self.outage.active.load(Ordering::SeqCst) { + self.outage.dropped.notify_one(); + return Poll::Ready(Err(io::ErrorKind::NetworkUnreachable.into())); + } + self.inner.as_mut().poll_send(transmit, cx) + } + + fn max_transmit_segments(&self) -> usize { + self.inner.max_transmit_segments() + } +} diff --git a/rs/moq-uring/Cargo.toml b/rs/moq-uring/Cargo.toml index a0e1f2513e1..d727a202229 100644 --- a/rs/moq-uring/Cargo.toml +++ b/rs/moq-uring/Cargo.toml @@ -87,3 +87,8 @@ harness = false [[bench]] name = "worker_turns" harness = false + +[[bench]] +name = "unreachable_dials" +harness = false +required-features = ["quic"] diff --git a/rs/moq-uring/benches/unreachable_dials.rs b/rs/moq-uring/benches/unreachable_dials.rs new file mode 100644 index 00000000000..921aec26675 --- /dev/null +++ b/rs/moq-uring/benches/unreachable_dials.rs @@ -0,0 +1,139 @@ +//! Failing unroutable dials while other handshakes are in flight on the same socket. +//! +//! A send that finds no route fails the handshakes dialing that destination. +//! Its cost should follow the failed destinations alone, not how many other +//! handshakes the endpoint is carrying, so the per-dial time stays flat across +//! the `handshakes` axis. + +use criterion::{criterion_group, criterion_main}; + +#[cfg(target_os = "linux")] +mod linux { + use std::future::Future; + use std::net::{Ipv4Addr, SocketAddr, UdpSocket}; + use std::os::fd::AsRawFd; + use std::pin::Pin; + use std::task::Poll; + use std::time::Duration; + + use criterion::{BenchmarkId, Criterion, Throughput}; + use moq_uring::{Config, Error, Worker, quic, udp}; + use nix::sys::socket::{AddressFamily, SockFlag, SockType, SockaddrIn6, bind, setsockopt, socket, sockopt}; + + type Dial<'a> = Pin> + 'a>>; + + fn dial_config(peer: SocketAddr) -> quic::client::Config { + let mut config = quic::client::Config::new(peer, "localhost"); + config.alpn = vec!["moq-uring-bench".to_string()]; + config.verify = false; + config.system_roots = false; + config + } + + /// An IPv6-only socket, which Linux refuses to send to a v4-mapped address + /// with `ENETUNREACH`: an unroutable family without touching the routes. + fn v6_only() -> UdpSocket { + let fd = socket(AddressFamily::Inet6, SockType::Datagram, SockFlag::empty(), None).expect("socket"); + setsockopt(&fd, sockopt::Ipv6V6Only, &true).expect("IPV6_V6ONLY"); + let local: std::net::SocketAddrV6 = "[::1]:0".parse().expect("addr"); + bind(fd.as_raw_fd(), &SockaddrIn6::from(local)).expect("bind"); + UdpSocket::from(fd) + } + + /// Poll every dial until it resolves, returning how many failed. + async fn settle(mut dials: Vec>) -> usize { + let mut failed = 0; + std::future::poll_fn(|cx| { + dials.retain_mut(|dial| match dial.as_mut().poll(cx) { + Poll::Ready(result) => { + failed += usize::from(result.is_err()); + false + } + Poll::Pending => true, + }); + if dials.is_empty() { + Poll::Ready(()) + } else { + Poll::Pending + } + }) + .await; + failed + } + + pub fn benchmark(c: &mut Criterion) { + let mut group = c.benchmark_group("unreachable_dials"); + // The background dials end at the 10s handshake timeout, so each point + // has to finish well inside it. + group + .sample_size(10) + .warm_up_time(Duration::from_millis(500)) + .measurement_time(Duration::from_secs(2)); + + for handshakes in [1, 256, 4096] { + for destinations in [1u16, 8, 64] { + let mut worker = match Worker::new(Config::default()) { + Ok(worker) => worker, + Err(Error::Unsupported(reason)) => { + eprintln!("skipping unreachable dial benchmark: {reason}"); + return; + } + Err(err) => panic!("worker setup failed: {err}"), + }; + let handle = worker.handle(); + let endpoint = quic::Endpoint::new( + handle.udp(v6_only(), udp::Config::default()).expect("socket"), + quic::endpoint::Config::default(), + ) + .expect("endpoint"); + + // Handshakes into a socket nobody reads stay in flight for the run. + let hole = UdpSocket::bind("[::1]:0").expect("bind"); + let into_hole = dial_config(hole.local_addr().expect("addr")); + let mut background: Vec> = (0..handshakes) + .map(|_| Box::pin(endpoint.connect(&into_hole)) as Dial<'_>) + .collect(); + worker + .block_on(std::future::poll_fn(|cx| { + for dial in &mut background { + assert!(dial.as_mut().poll(cx).is_pending(), "a dial into a hole resolved"); + } + Poll::Ready(()) + })) + .expect("worker"); + + let unroutable: Vec = (1..=destinations) + .map(|port| dial_config(SocketAddr::new(Ipv4Addr::LOCALHOST.to_ipv6_mapped().into(), port))) + .collect(); + + group.throughput(Throughput::Elements(destinations.into())); + group.bench_function( + BenchmarkId::new(format!("handshakes-{handshakes}"), destinations), + |b| { + b.iter(|| { + let dials = unroutable + .iter() + .map(|config| Box::pin(endpoint.connect(config)) as Dial<'_>) + .collect(); + let failed = worker.block_on(settle(dials)).expect("worker"); + assert_eq!(failed, unroutable.len(), "every unroutable dial fails"); + }); + }, + ); + drop(background); + } + } + group.finish(); + } +} + +#[cfg(target_os = "linux")] +fn benchmark(c: &mut criterion::Criterion) { + linux::benchmark(c); +} + +#[cfg(not(target_os = "linux"))] +fn benchmark(_: &mut criterion::Criterion) {} + +criterion_group!(benches, benchmark); +criterion_main!(benches); diff --git a/rs/moq-uring/src/quic/driver/connection.rs b/rs/moq-uring/src/quic/driver/connection.rs index fb70a5a1126..15b42aaa2f5 100644 --- a/rs/moq-uring/src/quic/driver/connection.rs +++ b/rs/moq-uring/src/quic/driver/connection.rs @@ -171,6 +171,18 @@ impl Inner { state.fail(err); } + /// Fail the connection because its peer has no route, closing it so the + /// driver still drains it out of the endpoint. + pub(crate) fn unreachable(&self, err: &std::io::Error) { + self.conn + .borrow_mut() + .close(moq_clock::Instant::now(), 0u32.into(), Bytes::new()); + self.state.borrow_mut().fail(Error::Transport { + code: moq_quic::TransportErrorCode::NO_VIABLE_PATH.into(), + reason: err.to_string(), + }); + } + /// Park `waiter` until stream `id` is writable again. pub(crate) fn park_writable(&self, id: StreamId, waiter: &kio::Waiter) { let mut state = self.state.borrow_mut(); diff --git a/rs/moq-uring/src/quic/driver/endpoint.rs b/rs/moq-uring/src/quic/driver/endpoint.rs index 2246efed3ed..e91ad1950be 100644 --- a/rs/moq-uring/src/quic/driver/endpoint.rs +++ b/rs/moq-uring/src/quic/driver/endpoint.rs @@ -9,7 +9,7 @@ //! shared with the rest of the endpoint implementation. use std::cell::{Cell, RefCell}; -use std::collections::VecDeque; +use std::collections::{BTreeSet, VecDeque}; use std::net::SocketAddr; use std::rc::Rc; use std::sync::Arc; @@ -50,6 +50,9 @@ pub(crate) struct Inner { /// owns connection-id routing (and its hasher choices) itself, so nothing /// a peer picks reaches this map. conns: RefCell>, + /// Every live connection by the peer it was launched toward, so a send + /// that finds no route only visits the connections dialing that peer. + peers: RefCell>, /// Incoming handshakes in flight. Together with the accept queue this is /// bounded by [`Config::backlog`]. pending: Cell, @@ -88,6 +91,7 @@ impl Endpoint { return Err(Error::Io(Shared::gone_error().to_string())); }; let local = socket.local_addr().map_err(|err| Error::Io(err.to_string()))?; + socket.route_unreachable(); let server = match &config.server { Some(server) => { let mut server = super::server_config(server)?; @@ -111,6 +115,7 @@ impl Endpoint { endpoint: RefCell::new(endpoint), accepting: RefCell::new(accepting), conns: RefCell::new(FxHashMap::default()), + peers: RefCell::new(BTreeSet::new()), pending: Cell::new(0), backlog: config.backlog, handshake: config.handshake, @@ -228,6 +233,10 @@ impl Inner { // Handle drops and connection teardowns re-check the exit condition. waiter.register(&mut self.task_waiters.borrow_mut()); + while let Some((addr, err)) = self.socket.take_unreachable() { + self.unreachable(addr, &err); + } + loop { if self.handles.get() == 0 && self.conns.borrow().is_empty() { return Poll::Ready(()); @@ -417,13 +426,15 @@ impl Inner { /// Register `conn`, spawn its driver, and arrange its teardown. fn launch(self: &Rc, key: ConnectionHandle, conn: moq_quic::Connection) -> connection::Shared { + let peer = conn.remote_address(); let (shared, driver) = connection::launch(&self.owner, self.socket.clone(), Rc::downgrade(self), key, conn); self.conns.borrow_mut().insert(key, shared.clone()); + self.peers.borrow_mut().insert((peer, key)); let inner = self.clone(); self.owner.spawn(async move { driver.await; - inner.release(key); + inner.release(key, peer); }); shared } @@ -439,12 +450,33 @@ impl Inner { } /// A connection's driver ended; forget it. - fn release(&self, key: ConnectionHandle) { + fn release(&self, key: ConnectionHandle, peer: SocketAddr) { self.conns.borrow_mut().remove(&key); + self.peers.borrow_mut().remove(&(peer, key)); // The demux task may be waiting on us to exit. self.task_waiters.borrow_mut().wake(); } + /// A send to `addr` found no route, so a handshake dialing it can never + /// finish: fail it now rather than at the handshake timeout. An established + /// connection rides it out as loss, since a route that drops for a moment + /// (a client switching networks) comes back. + fn unreachable(&self, addr: SocketAddr, err: &std::io::Error) { + let conns: Vec = { + let all = self.conns.borrow(); + self.peers + .borrow() + .range((addr, ConnectionHandle(0))..=(addr, ConnectionHandle(usize::MAX))) + .filter_map(|(_, key)| all.get(key)) + .filter(|shared| shared.conn.borrow().is_handshaking()) + .cloned() + .collect() + }; + for conn in conns { + conn.unreachable(err); + } + } + /// The socket died: everything on it fails with `err`. fn fail(&self, err: Error) { *self.closed.borrow_mut() = Some(err.clone()); diff --git a/rs/moq-uring/src/udp.rs b/rs/moq-uring/src/udp.rs index 90ed65bd66f..a090ca00db3 100644 --- a/rs/moq-uring/src/udp.rs +++ b/rs/moq-uring/src/udp.rs @@ -342,6 +342,11 @@ struct Rx { starved: bool, /// Terminal failure, surfaced by `poll_recv` once the queue drains. error: Option, + /// Destinations a send found no route to, each with its errno, for the + /// demux task to hand to the connections dialing them. Here rather than + /// on the send side because the demux task is what parks on `waiters`. + /// `None` until a consumer asks for them, so a raw socket queues nothing. + unreachable: Option>, } /// Send-side state. @@ -475,6 +480,23 @@ impl SockShared { rx.waiters.wake(); } + /// A send to `to` found no route. Unlike other send failures this says + /// nothing about the socket, so with a consumer only the connections + /// dialing `to` hear it. Without one there is no destination to route it + /// to, so it fails sends like any other error. + fn unreachable(&self, to: SocketAddr, code: i32) { + let mut rx = self.rx.borrow_mut(); + let Some(reports) = rx.unreachable.as_mut() else { + drop(rx); + self.fail_tx(code); + return; + }; + if !reports.iter().any(|(addr, _)| *addr == to) { + reports.push((to, code)); + } + rx.waiters.wake(); + } + fn fail_tx(&self, code: i32) { let mut tx = self.tx.borrow_mut(); tx.error.get_or_insert(code); @@ -625,6 +647,7 @@ impl Socket { armed: None, starved: false, error: None, + unreachable: None, }), tx: RefCell::new(tx), }); @@ -681,6 +704,24 @@ impl Socket { Poll::Pending } + /// Report sends that find no route through + /// [`take_unreachable`](Self::take_unreachable) instead of failing every + /// later send, for a consumer that routes them to the right connection. + #[cfg_attr(not(feature = "quic"), allow(dead_code))] + pub(crate) fn route_unreachable(&self) { + self.shared.rx.borrow_mut().unreachable.get_or_insert_default(); + } + + /// A destination a send found no route to, and why, once per report. + /// + /// Reported through the receive waiters, so whoever parks in + /// [`poll_recv`](Self::poll_recv) is woken to collect it. + #[cfg_attr(not(feature = "quic"), allow(dead_code))] + pub(crate) fn take_unreachable(&self) -> Option<(SocketAddr, io::Error)> { + let (to, code) = self.shared.rx.borrow_mut().unreachable.as_mut()?.pop()?; + Some((to, io::Error::from_raw_os_error(code))) + } + /// Await [`poll_recv`](Self::poll_recv). pub async fn recv(&self) -> io::Result { kio::wait(|waiter| self.poll_recv(waiter)).await @@ -978,6 +1019,7 @@ pub(crate) struct SendOp { sock: Rc, id: u16, expect: usize, + to: SocketAddr, /// A GSO send's payload and destination, to resend it unsegmented if the /// kernel rejects the train. train: Option<(NonNull, SendOne)>, @@ -1071,6 +1113,7 @@ fn send_one( sock: staging.sock.clone(), id: staging.id, expect: len, + to, train: segment.and(NonNull::new(base)).map(|base| (base, *one)), })); let entry = opcode::SendMsg::new(types::Fd(staging.sock.io.as_raw_fd()), hdr_ptr) @@ -1354,6 +1397,10 @@ pub(crate) fn on_send(op: SendOp, cqe: Cqe) { } return; } + if moq_sock::udp::is_unreachable_err(&io::Error::from_raw_os_error(code)) { + op.sock.unreachable(op.to, code); + return; + } if code != libc::ECANCELED { op.sock.fail_tx(code); } @@ -1582,6 +1629,7 @@ mod tests { armed: None, starved: false, error: None, + unreachable: None, } } @@ -1619,4 +1667,55 @@ mod tests { } assert!(!grow_rx(&mut rx, &config), "grew past the ceiling"); } + + /// With nothing routing unreachable reports, a send that finds no route + /// fails later sends like any other error instead of queueing a report + /// nobody will collect. Linux refuses a v4-mapped destination on an + /// IPv6-only socket with `ENETUNREACH`. + #[test] + fn a_raw_socket_sees_an_unreachable_send_fail() { + use nix::sys::socket::{AddressFamily, SockFlag, SockType, SockaddrIn6, bind, setsockopt, socket, sockopt}; + + let mut worker = match crate::Worker::new(crate::Config::default()) { + Ok(worker) => worker, + Err(Error::Unsupported(reason)) if std::env::var_os("MOQ_STRICT").is_none() => { + eprintln!("skipping io_uring socket test: {reason}"); + return; + } + Err(err) => panic!("worker setup failed: {err}"), + }; + let handle = worker.handle(); + let fd = socket(AddressFamily::Inet6, SockType::Datagram, SockFlag::empty(), None).expect("socket"); + setsockopt(&fd, sockopt::Ipv6V6Only, &true).expect("IPV6_V6ONLY"); + let local: SocketAddrV6 = "[::1]:0".parse().expect("addr"); + bind(fd.as_raw_fd(), &SockaddrIn6::from(local)).expect("bind"); + let sock = handle.udp(UdpSocket::from(fd), Config::default()).expect("socket"); + + let err = worker + .block_on(async { + let mut tx = sock.acquire().await.expect("send buffer"); + tx[..4].copy_from_slice(b"lost"); + tx.send(Transmit { + to: SocketAddr::new(Ipv4Addr::LOCALHOST.to_ipv6_mapped().into(), 1), + len: 4, + segment: 4, + ecn: None, + }) + .expect("staged"); + // The failure lands with the send's completion, a worker turn later. + for _ in 0..1000 { + match sock.acquire().await { + Ok(tx) => drop(tx), + Err(err) => return err, + } + crate::Timer::after(&handle, std::time::Duration::from_millis(1)) + .wait() + .await; + } + panic!("the unreachable send never failed the socket"); + }) + .expect("worker"); + assert_eq!(err.kind(), io::ErrorKind::NetworkUnreachable, "{err}"); + assert!(sock.shared.rx.borrow().unreachable.is_none(), "nothing queued"); + } } diff --git a/rs/moq-uring/tests/endpoint.rs b/rs/moq-uring/tests/endpoint.rs index a6e41b28e61..78337b03ce6 100644 --- a/rs/moq-uring/tests/endpoint.rs +++ b/rs/moq-uring/tests/endpoint.rs @@ -391,6 +391,66 @@ fn an_unanswered_dial_times_out() { assert!(result.is_err(), "a dial into a black hole must time out"); } +/// A dial to an address the host cannot route fails at once with +/// `NO_VIABLE_PATH` rather than at the handshake timeout, and fails alone: the +/// socket it shares goes on dialing. Linux refuses a v4-mapped destination on +/// an IPv6-only socket with `ENETUNREACH`, an unroutable family without +/// touching the host's routes. +#[test] +fn an_unroutable_dial_fails_alone() { + let Some(mut worker) = worker() else { return }; + let handle = worker.handle(); + let certs = support::certs().expect("certificates"); + + let server = quic::Endpoint::new( + handle + .udp(UdpSocket::bind("[::1]:0").expect("bind"), udp::Config::default()) + .expect("server socket"), + quic::endpoint::Config::default().with_server(server_config(&certs)), + ) + .expect("server endpoint"); + let v6_only = { + use nix::sys::socket::{AddressFamily, SockFlag, SockType, SockaddrIn6, bind, setsockopt, socket, sockopt}; + use std::os::fd::AsRawFd; + let fd = socket(AddressFamily::Inet6, SockType::Datagram, SockFlag::empty(), None).expect("socket"); + setsockopt(&fd, sockopt::Ipv6V6Only, &true).expect("IPV6_V6ONLY"); + let local: std::net::SocketAddrV6 = "[::1]:0".parse().expect("addr"); + bind(fd.as_raw_fd(), &SockaddrIn6::from(local)).expect("bind"); + UdpSocket::from(fd) + }; + let client = quic::Endpoint::new( + handle.udp(v6_only, udp::Config::default()).expect("client socket"), + quic::endpoint::Config::default(), + ) + .expect("dial-only endpoint"); + + let port = server.local_addr().port(); + let unroutable = std::net::SocketAddr::new(std::net::Ipv4Addr::LOCALHOST.to_ipv6_mapped().into(), port); + worker + .block_on(async move { + // Well inside the 10s handshake timeout a swallowed error waits for. + match within( + &handle, + "the unroutable dial to fail", + client.connect(&dial_config(unroutable)), + ) + .await + { + Err(quic::Error::Transport { code: 0x10, .. }) => {} + other => panic!("expected NO_VIABLE_PATH, got {other:?}"), + } + + let dialed = within( + &handle, + "a dial on the same socket", + client.connect(&dial_config(server.local_addr())), + ) + .await; + dialed.expect("the socket outlives one unroutable destination"); + }) + .expect("worker"); +} + /// The handshake backlog bounds what an unauthenticated Initial can allocate: /// past it, Initials are dropped before any per-connection state exists. Zero /// is the forcing value, refusing every handshake outright.