diff --git a/quest/m1/README.md b/quest/m1/README.md index 70e3238991b..4e62a884c64 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -35,7 +35,6 @@ moves what remains of them once their claims merge. - [Drop set](/quest/m1/drop-set.md) - the drop set's track and cursor runs are billed to the cache pool, so fragmenting drops costs what the pool holds; eviction folds final runs or ends the cursor with `TooFarBehind` - [Media fuzz findings](/quest/m1/fuzz-findings/README.md) - the nightly media fuzz targets run clean: mp4-atom, MKV block, TS step, SPS overflow, and catalog encoder bugs they found are fixed - [A smaller, safer API than release](/quest/m1/api-audit/README.md) - every surface added since release has a consumer or is gone, overlapping operations are collapsed, names mirror across Rust, JS, and the bindings, and every break says how to migrate -- [moq-webrtc drops the ICE wire counters nothing reads](/quest/m1/ice-wire-counters.md) - the per-packet `Wire` atomics go once #8122 makes the ICE module private - [Routing guide](/quest/m1/routing-guide.md) - concepts and practical outbound federation setup - [A reader on a failed copy continues on the held re-offer](/quest/m1/copy-recover.md) - a live reader keeps a group whose copy fails by continuing on the same track's re-offer at the same frame - [Group errors are cached by kind](/quest/m1/group-error-cache.md) - permanent group errors bind every reader, retryable ones are kept only for a bounded time, and a live SUBSCRIBE_DROP or FETCH answered `Old` no longer caches it for good diff --git a/quest/m1/ice-wire-counters.md b/quest/m1/ice-wire-counters.md deleted file mode 100644 index bff4004ea39..00000000000 --- a/quest/m1/ice-wire-counters.md +++ /dev/null @@ -1,19 +0,0 @@ -# [XS] moq-webrtc drops the ICE wire counters nothing reads - -## Goal - -moq-webrtc's ICE module no longer counts every packet into `Wire` -(per-packet atomics, public as `moq_ice::Wire` before -[moq-dev/moq#8122](https://github.com/moq-dev/moq/pull/8122)), since only -tests read them. - -## Plan - -Decided 2026-11-07 (maintainer, /quest-plan from #8122): remove the counters -now, and let [Wire stats](/quest/m3/wire-stats/README.md) add an ICE source -with its adapter when it needs one. Not chosen: an ICE child in Wire stats -that wires them into Presence now; leaving them write-only. - -Milestone m1: #8122 makes the module crate-private (`mod ice;` in -`rs/moq-webrtc/src/lib.rs`), so removing `Wire` breaks no public API. Start -after #8122 lands. diff --git a/rs/moq-webrtc/src/ice/agent.rs b/rs/moq-webrtc/src/ice/agent.rs index 9f12e59db7b..a4331f8050c 100644 --- a/rs/moq-webrtc/src/ice/agent.rs +++ b/rs/moq-webrtc/src/ice/agent.rs @@ -223,7 +223,7 @@ impl Agent { while let Some((data, dst)) = self.outbox.front() { match self.socket.send_to(data, *dst).await { - Ok(len) => self.inbound.wire.sent(len), + Ok(_) => {} Err(err) => tracing::warn!(%err, dst = %dst, "send failed"), } self.outbox.pop_front(); @@ -316,7 +316,7 @@ impl Agent { fn send(&mut self, data: &[u8], dst: SocketAddr) { let dst = to_family(dst, self.socket_v6); match self.socket.try_send_to(data, dst) { - Ok(len) => self.inbound.wire.sent(len), + Ok(_) => {} // Changes can keep coming while the socket stays full, so the backlog is // bounded like the inbound queue; ICE, SCTP, and RTP recover the loss. Err(err) if err.kind() == std::io::ErrorKind::WouldBlock && self.outbox.len() < crate::ice::INBOX => { @@ -476,30 +476,6 @@ mod tests { assert_eq!(pick_local(&[v4], "[2001:db8::2]:1".parse().unwrap()), v4); } - #[tokio::test] - async fn wire_counts_only_accepted_payload() { - let socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap()); - let peer = UdpSocket::bind("127.0.0.1:0").await.unwrap(); - let local = socket.local_addr().unwrap(); - let inbound = crate::ice::reader(socket.clone()); - let wire = inbound.wire.clone(); - let socket = Socket::new(socket, vec![local], inbound).unwrap(); - let mut agent = Agent::new(Rtc::builder().build(Instant::now().into_std()), socket).unwrap(); - - // Until tokio has seen the socket writable, a send waits in the outbox. - agent.socket.writable().await.unwrap(); - agent.send(&[42; 73], peer.local_addr().unwrap()); - // UDP refuses an oversized datagram, which is never counted as sent. - agent.send(&vec![0; 65_536], peer.local_addr().unwrap()); - assert_eq!(wire.stats(), crate::ice::Stats { tx: 73, rx: 0 }); - - peer.send_to(&[7; 11], local).await.unwrap(); - let (data, _) = agent.inbound.packets.recv().await.unwrap(); - assert_eq!(data.len(), 11); - drop(agent); - assert_eq!(wire.stats(), crate::ice::Stats { tx: 73, rx: 11 }); - } - /// An agent on a fresh socket, which tokio reports unwritable until the runtime /// polls it, so every send waits in the outbox until the test yields. fn unpolled() -> Agent { diff --git a/rs/moq-webrtc/src/ice/mod.rs b/rs/moq-webrtc/src/ice/mod.rs index 7495d00fabd..d598527c9d0 100644 --- a/rs/moq-webrtc/src/ice/mod.rs +++ b/rs/moq-webrtc/src/ice/mod.rs @@ -22,6 +22,4 @@ mod agent; mod wire; pub(crate) use agent::{Agent, Error, Event, canonical}; -#[cfg(test)] -pub(crate) use wire::Stats; -pub(crate) use wire::{INBOX, Inbound, Packet, Socket, Wire, reader}; +pub(crate) use wire::{INBOX, Inbound, Packet, Socket, reader}; diff --git a/rs/moq-webrtc/src/ice/wire.rs b/rs/moq-webrtc/src/ice/wire.rs index 8e79854e0ec..dbb80bd2d6d 100644 --- a/rs/moq-webrtc/src/ice/wire.rs +++ b/rs/moq-webrtc/src/ice/wire.rs @@ -1,6 +1,5 @@ use std::net::SocketAddr; use std::sync::Arc; -use std::sync::atomic::{AtomicU64, Ordering}; use tokio::net::UdpSocket; use tokio::sync::mpsc; @@ -48,69 +47,22 @@ impl Socket { } } -/// The datagrams routed to one [`super::Agent`], and the counters they are tallied in. +/// The datagrams routed to one [`super::Agent`]. pub struct Inbound { /// Ends once every sender is dropped, which ends the agent. pub packets: mpsc::Receiver, - /// Counts `rx` where datagrams are routed, so a full queue's drops count too; - /// the agent counts `tx`. - pub wire: Wire, -} - -/// Cumulative UDP payload bytes exchanged with one peer, excluding IP and UDP -/// headers. Clones share counters, which outlive the agent. -#[derive(Clone, Debug, Default)] -pub struct Wire(Arc); - -#[derive(Debug, Default)] -struct Counters { - tx: AtomicU64, - rx: AtomicU64, -} - -/// A snapshot of a [`Wire`]'s counters. -#[cfg(test)] -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -pub struct Stats { - /// Bytes the socket accepted, including ICE, DTLS, and retransmissions. - pub tx: u64, - /// Bytes routed to the peer, including those its full queue dropped. - pub rx: u64, -} - -impl Wire { - /// Read both counters without consuming them. - #[cfg(test)] - pub fn stats(&self) -> Stats { - Stats { - tx: self.0.tx.load(Ordering::Relaxed), - rx: self.0.rx.load(Ordering::Relaxed), - } - } - - /// Count a datagram routed to this peer. - pub fn received(&self, bytes: usize) { - self.0.rx.fetch_add(bytes as u64, Ordering::Relaxed); - } - - pub(crate) fn sent(&self, bytes: usize) { - self.0.tx.fetch_add(bytes as u64, Ordering::Relaxed); - } } /// Pump every datagram from a socket the caller owns 1:1 into a queue for /// an agent, until the queue's receiver is dropped. pub fn reader(socket: Arc) -> Inbound { let (tx, rx) = mpsc::channel(INBOX); - let wire = Wire::default(); - let counted = wire.clone(); tokio::spawn(async move { let mut buf = vec![0u8; 65_535]; loop { tokio::select! { res = socket.recv_from(&mut buf) => match res { Ok((len, src)) => { - counted.received(len); // Bounded like a socket buffer: drop on full. if let Err(mpsc::error::TrySendError::Closed(_)) = tx.try_send((buf[..len].to_vec(), canonical(src))) { break; @@ -125,7 +77,7 @@ pub fn reader(socket: Arc) -> Inbound { } } }); - Inbound { packets: rx, wire } + Inbound { packets: rx } } #[cfg(test)] @@ -136,10 +88,7 @@ mod tests { async fn construction_refuses_a_socket_without_advertised_candidates() { let udp = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap()); let (_, packets) = mpsc::channel(INBOX); - let inbound = Inbound { - packets, - wire: Wire::default(), - }; + let inbound = Inbound { packets }; let Err(err) = Socket::new(udp, Vec::new(), inbound) else { panic!("an unusable socket escaped construction"); }; diff --git a/rs/moq-webrtc/src/server/mux.rs b/rs/moq-webrtc/src/server/mux.rs index c602f3a771f..673bf704999 100644 --- a/rs/moq-webrtc/src/server/mux.rs +++ b/rs/moq-webrtc/src/server/mux.rs @@ -45,7 +45,6 @@ struct Registry { struct Entry { ufrag: String, sender: mpsc::Sender, - wire: crate::ice::Wire, } /// The shared media socket plus its demux routing table. @@ -82,7 +81,7 @@ impl Drop for Registration { } impl Registry { - fn route(&mut self, len: usize, src: SocketAddr, ufrag: Option<&str>) -> Option> { + fn route(&mut self, src: SocketAddr, ufrag: Option<&str>) -> Option> { let entry = self.by_addr.get(&src).cloned().or_else(|| { let entry = self.by_ufrag.get(ufrag?).cloned()?; self.by_addr.insert(src, entry.clone()); @@ -92,9 +91,6 @@ impl Registry { self.by_addr.remove(&src); return None; } - // Count before the bounded inbox, even when it drops a received packet. - // The routing lock also prevents a late bump after unregistration. - entry.wire.received(len); Some(entry) } } @@ -126,11 +122,9 @@ impl Mux { pub(crate) fn register(&self) -> (IceCreds, Inbound, Registration) { let creds = IceCreds::new(); let (tx, rx) = mpsc::channel(crate::ice::INBOX); - let wire = crate::ice::Wire::default(); let entry = Arc::new(Entry { ufrag: creds.ufrag.clone(), sender: tx, - wire: wire.clone(), }); self.registry .lock() @@ -141,7 +135,7 @@ impl Mux { ufrag: creds.ufrag.clone(), registry: self.registry.clone(), }; - (creds, Inbound { packets: rx, wire }, registration) + (creds, Inbound { packets: rx }, registration) } /// The shared socket, handed to each session for sending. @@ -176,10 +170,10 @@ async fn demux(socket: Arc, registry: Arc>) { let data = &buf[..len]; // Cached peers need one routing lock; only a new address parses STUN. - let entry = registry.lock().unwrap().route(data.len(), src, None); + let entry = registry.lock().unwrap().route(src, None); let entry = entry.or_else(|| { let ufrag = local_ufrag(data)?; - registry.lock().unwrap().route(data.len(), src, Some(&ufrag)) + registry.lock().unwrap().route(src, Some(&ufrag)) }); let Some(entry) = entry else { continue; @@ -245,111 +239,44 @@ mod tests { } #[tokio::test] - async fn wire_sources_are_per_peer_and_count_full_inbox_drops() { + async fn routes_per_peer_until_unregistered() { let mux = Mux::bind("127.0.0.1:0".parse().unwrap(), &[]).await.unwrap(); - let (a, inbound_a, registration_a) = mux.register(); - let (b, inbound_b, _registration_b) = mux.register(); - let source_a = inbound_a.wire.clone(); - let source_b = inbound_b.wire.clone(); + let (a, mut inbound_a, registration_a) = mux.register(); + let (b, mut inbound_b, _registration_b) = mux.register(); let address_a = "127.0.0.1:10001".parse().unwrap(); let address_b = "127.0.0.1:10002".parse().unwrap(); let mut registry = mux.registry.lock().unwrap(); - assert!(registry.route(100, address_a, Some("unknown")).is_none()); - for _ in 0..crate::ice::INBOX { - let entry = registry.route(11, address_a, Some(&a.ufrag)).unwrap(); - entry.sender.try_send((vec![0; 11], address_a)).unwrap(); - } - let entry = registry.route(23, address_a, None).unwrap(); - assert!(matches!( - entry.sender.try_send((vec![0; 23], address_a)), - Err(mpsc::error::TrySendError::Full(_)) - )); - registry.route(37, address_b, Some(&b.ufrag)).unwrap(); + assert!(registry.route(address_a, Some("unknown")).is_none()); + registry + .route(address_a, Some(&a.ufrag)) + .unwrap() + .sender + .try_send((vec![1], address_a)) + .unwrap(); + // A cached address routes without a ufrag. + registry + .route(address_a, None) + .unwrap() + .sender + .try_send((vec![2], address_a)) + .unwrap(); + registry + .route(address_b, Some(&b.ufrag)) + .unwrap() + .sender + .try_send((vec![3], address_b)) + .unwrap(); drop(registry); - assert_eq!( - source_a.stats(), - crate::ice::Stats { - tx: 0, - rx: 11 * crate::ice::INBOX as u64 + 23 - } - ); - assert_eq!(source_b.stats(), crate::ice::Stats { tx: 0, rx: 37 }); + assert_eq!(inbound_a.packets.try_recv().unwrap().0, [1]); + assert_eq!(inbound_a.packets.try_recv().unwrap().0, [2]); + assert_eq!(inbound_b.packets.try_recv().unwrap().0, [3]); // Unregister even while an inbox is still held: cached addresses cannot - // keep attributing traffic after the peer ends. + // keep routing traffic after the peer ends. drop(registration_a); - let final_stats = source_a.stats(); let mut registry = mux.registry.lock().unwrap(); - assert!(registry.route(59, address_a, None).is_none()); - assert!(registry.route(59, address_a, Some(&a.ufrag)).is_none()); - drop(registry); - drop(inbound_a); - assert_eq!(source_a.stats(), final_stats); - } - - #[test] - #[expect( - clippy::disallowed_methods, - clippy::disallowed_types, - reason = "the test measures route lookup cost on the host clock" - )] - fn wire_routes_scale_with_peers_and_packets() { - use std::hint::black_box; - use std::time::Instant; - - for peers in [1, 16, 256] { - for packets in [1_000, 10_000, 100_000] { - let registry = Arc::new(Mutex::new(Registry::default())); - let mut receivers = Vec::new(); - let mut sources = Vec::new(); - let mut addresses = Vec::new(); - for peer in 0..peers { - let (sender, receiver) = mpsc::channel(1); - let wire = crate::ice::Wire::default(); - let address = SocketAddr::from(([127, 0, 0, 1], peer as u16 + 1)); - let entry = Arc::new(Entry { - ufrag: peer.to_string(), - sender, - wire: wire.clone(), - }); - registry.lock().unwrap().by_addr.insert(address, entry); - receivers.push(receiver); - sources.push(wire); - addresses.push(address); - } - let start = Instant::now(); - for packet in 0..packets { - let entry = registry - .lock() - .unwrap() - .by_addr - .get(&addresses[packet % peers]) - .cloned() - .unwrap(); - black_box(entry); - } - let lookup = start.elapsed(); - let start = Instant::now(); - for packet in 0..packets { - let entry = registry - .lock() - .unwrap() - .route(17, addresses[packet % peers], None) - .unwrap(); - black_box(entry); - } - let counted = start.elapsed(); - eprintln!( - "wire-route peers={peers} packets={packets} lookup_ns={} counted_ns={}", - lookup.as_nanos(), - counted.as_nanos() - ); - for (peer, wire) in sources.iter().enumerate() { - let expected = packets / peers + usize::from(peer < packets % peers); - assert_eq!(wire.stats().rx, 17 * expected as u64); - } - } - } + assert!(registry.route(address_a, None).is_none()); + assert!(registry.route(address_a, Some(&a.ufrag)).is_none()); } /// An IPv4 peer reaching a dual-stack (`[::]`) mux must route to its session @@ -366,7 +293,6 @@ mod tests { let mux = Mux::bind(bind, &advertise).await.expect("dual-stack bind"); let port = mux.socket.local_addr().unwrap().port(); let (creds, mut rx, registration) = mux.register(); - let wire = rx.wire.clone(); // Dial the mux over IPv4, which a dual-stack socket accepts as v4-mapped. let peer = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).await.unwrap(); @@ -383,9 +309,6 @@ mod tests { assert_eq!(src, peer_addr, "an IPv4 peer's source must arrive unmapped"); assert!(src.is_ipv4()); - assert_eq!(wire.stats().rx, request.len() as u64); drop(registration); - drop(rx); - assert_eq!(wire.stats().rx, request.len() as u64); } }