From 3dccd8858007de69fa7e981c95214d482d0bffcd Mon Sep 17 00:00:00 2001 From: PQCrypta Date: Mon, 16 Nov 2026 14:30:37 -0600 Subject: [PATCH 1/2] fix(proto): let the initial window leave in one burst before the first RTT sample Before a path has an RTT sample, `smoothed_rtt` is the configured initial RTT (333 ms), so `optimal_capacity` limited the pacer's burst to one MTU and refilled it at about one datagram per 27 ms. A ClientHello that takes two datagrams (any with an ML-KEM key share) had its second held until the server's ACK or the pacing timer, and a server's handshake flight was split the same way. Until the first RTT sample, the burst may now be the congestion window (the initial window then, as RFC 9002 section 7.7 allows), bounded by MIN_BURST_SIZE and MAX_BURST_SIZE times the MTU. Once the path has a sample, capacity is computed as before, so MAX_BURST_INTERVAL still bounds window-paced flows. The pacer recomputes capacity when the path gets its first sample (`RttEstimator::has_sample`), as it does when the window or the MTU change. A controller that reports a pacing rate (BBRv3) never reaches this path. `sent_packets_stats_snapshot_test` counts one more server packet: the NEW_CONNECTION_IDs now leave as 0.5-RTT data right behind the Handshake flight instead of with HANDSHAKE_DONE. New test: `initial_window_bursts_before_an_rtt_sample`. Refs #879 --- noq-proto/src/connection/pacing.rs | 180 +++++++++++++++++++++++++---- noq-proto/src/connection/paths.rs | 6 + noq-proto/src/tests/mod.rs | 8 +- 3 files changed, 169 insertions(+), 25 deletions(-) diff --git a/noq-proto/src/connection/pacing.rs b/noq-proto/src/connection/pacing.rs index d9798e7929..d20a42b637 100644 --- a/noq-proto/src/connection/pacing.rs +++ b/noq-proto/src/connection/pacing.rs @@ -30,6 +30,8 @@ struct WindowInputs { window: u64, /// MTU of the path in bytes. mtu: u16, + /// Whether the path had an RTT sample (see [`optimal_capacity`]). + rtt_sampled: bool, } impl Pacer { @@ -42,10 +44,14 @@ impl Pacer { now: Instant, ) -> Self { let window = rate_limited_window(smoothed_rtt, window, max_bytes_per_second); - let capacity = optimal_capacity(smoothed_rtt, window, mtu); + let capacity = optimal_capacity(smoothed_rtt, window, mtu, false); Self { capacity, - last_window_inputs: Some(WindowInputs { window, mtu }), + last_window_inputs: Some(WindowInputs { + window, + mtu, + rtt_sampled: false, + }), tokens: capacity, max_bytes_per_second, prev: now, @@ -69,6 +75,8 @@ impl Pacer { /// /// The 5/4 ratio used here comes from the suggestion that N = 1.25 in the draft IETF RFC for /// QUIC. + /// `rtt_sampled` is whether the path has an RTT sample yet; until it has, `smoothed_rtt` is + /// the configured initial RTT (see [`optimal_capacity`]). /// `controller_metrics` provides [`ControllerMetrics`] from the congestion controller used to /// adjust pacing. /// @@ -81,6 +89,7 @@ impl Pacer { pub(super) fn delay( &mut self, smoothed_rtt: Duration, + rtt_sampled: bool, bytes_to_send: u64, mtu: u16, now: Instant, @@ -99,9 +108,13 @@ impl Pacer { } let window = rate_limited_window(smoothed_rtt, window, self.max_bytes_per_second); - let inputs = WindowInputs { window, mtu }; + let inputs = WindowInputs { + window, + mtu, + rtt_sampled, + }; if self.last_window_inputs != Some(inputs) { - self.capacity = optimal_capacity(smoothed_rtt, window, mtu); + self.capacity = optimal_capacity(smoothed_rtt, window, mtu, rtt_sampled); // here we cap the number of bytes sent at once during a burst self.tokens = self.capacity.min(self.tokens); @@ -236,9 +249,30 @@ fn rate_capacity(pacing_rate: u64, mtu: u16) -> u64 { } /// Calculates a pacer capacity for a certain window and RTT, which imply a rate -fn optimal_capacity(smoothed_rtt: Duration, window: u64, mtu: u16) -> u64 { +/// +/// Until the path has an RTT sample, `smoothed_rtt` is the configured initial +/// RTT (333 ms by default, RFC 9002 section 6.2.2), a value for timers rather +/// than a measurement. A slow path is then not something known, and the burst +/// may be the congestion window, the initial window at that point, as RFC 9002 +/// section 7.7 allows. Limiting it by [`MAX_BURST_INTERVAL`] of the initial +/// RTT instead allowed a single datagram, and refilled at a datagram per +/// ~27 ms: a ClientHello that takes two datagrams (any with an ML-KEM key +/// share) had its second held until the server's acknowledgement or that +/// timer, and a server's first flight after its first datagram the same way. +/// (A controller that reports a pacing rate never comes here: BBRv3 starts +/// from a 1 ms nominal RTT, so its first flight leaves in one burst too.) +fn optimal_capacity(smoothed_rtt: Duration, window: u64, mtu: u16, rtt_sampled: bool) -> u64 { let rtt = smoothed_rtt.as_nanos().max(1); let rate = u64::try_from(window as u128 * 1_000_000_000 / rtt).unwrap_or(u64::MAX); + if !rtt_sampled { + let mtu = u64::from(mtu); + let target_capacity = + ((rate as u128 * TARGET_BURST_INTERVAL.as_nanos()) / 1_000_000_000) as u64; + return Ord::min( + target_capacity.clamp(MIN_BURST_SIZE * mtu, MAX_BURST_SIZE * mtu), + Ord::max(window, mtu), + ); + } rate_capacity(rate, mtu) } @@ -323,7 +357,7 @@ mod tests { ) -> Option<(Duration, u64)> { let mut sent = 0; for _ in 0..10_000 { - match pacer.delay(rtt, u64::from(mtu), mtu, now, metrics) { + match pacer.delay(rtt, true, u64::from(mtu), mtu, now, metrics) { Some(resume_after) => return Some((resume_after, sent)), None => { pacer.on_transmit(mtu); @@ -359,7 +393,7 @@ mod tests { polls < MAX_POLLS, "pacer made no progress: {sent} bytes emitted without reaching the deadline" ); - match pacer.delay(rtt, u64::from(mtu), mtu, at, metrics) { + match pacer.delay(rtt, true, u64::from(mtu), mtu, at, metrics) { None => { pacer.on_transmit(mtu); sent += u64::from(mtu); @@ -404,7 +438,7 @@ mod tests { // `poll_transmit` re-runs when the pacing timer fires. If the pacer re-derives the // same delay from the new `now` it would re-arm forever and the connection stalls. assert_eq!( - pacer.delay(rtt, u64::from(mtu), mtu, now + resume_after, &metrics), + pacer.delay(rtt, true, u64::from(mtu), mtu, now + resume_after, &metrics), None, "the delay the pacer asked for must be long enough to unblock the send" ); @@ -453,6 +487,7 @@ mod tests { Pacer::new(rtt, 30000, 1500, None, new_instant) .delay( Duration::from_micros(0), + true, 0, 1500, old_instant, @@ -464,6 +499,7 @@ mod tests { Pacer::new(rtt, 30000, 1500, None, new_instant) .delay( Duration::from_micros(0), + true, 1600, 1500, old_instant, @@ -475,6 +511,7 @@ mod tests { Pacer::new(rtt, 30000, 1500, None, new_instant) .delay( Duration::from_micros(0), + true, 1500, 1500, old_instant, @@ -484,6 +521,48 @@ mod tests { ); } + /// A client's first flight before any RTT sample: RFC 9002's initial RTT + /// and an initial window of ten 1200-byte datagrams. The two datagrams of a + /// ClientHello with an ML-KEM key share go out together; once a 333 ms RTT + /// is measured, the path is a slow one and bursts are cut to + /// `MAX_BURST_INTERVAL` of the window. + #[test] + fn initial_window_bursts_before_an_rtt_sample() { + let rtt = Duration::from_millis(333); + let mtu = 1200; + let window = 10 * u64::from(mtu); + let now = Instant::now(); + let metrics = unpaced_metrics(window); + + let mut pacer = Pacer::new(rtt, window, mtu, None, now); + assert_eq!(pacer.capacity, window); + for _ in 0..10 { + assert_eq!( + pacer.delay(rtt, false, u64::from(mtu), mtu, now, &metrics), + None + ); + pacer.on_transmit(mtu); + } + assert!( + pacer + .delay(rtt, false, u64::from(mtu), mtu, now, &metrics) + .is_some() + ); + + let mut pacer = Pacer::new(rtt, window, mtu, None, now); + assert_eq!( + pacer.delay(rtt, true, u64::from(mtu), mtu, now, &metrics), + None + ); + assert_eq!(pacer.capacity, u64::from(mtu)); + pacer.on_transmit(mtu); + assert!( + pacer + .delay(rtt, true, u64::from(mtu), mtu, now, &metrics) + .is_some() + ); + } + #[test] fn derives_initial_capacity() { let window = 2_000_000; @@ -522,27 +601,48 @@ mod tests { assert_eq!(pacer.tokens, pacer.capacity); let initial_tokens = pacer.tokens; - pacer.delay(rtt, mtu as u64, mtu, now, &unpaced_metrics(window * 2)); + pacer.delay( + rtt, + true, + mtu as u64, + mtu, + now, + &unpaced_metrics(window * 2), + ); assert_eq!( pacer.capacity, (2 * window as u128 * TARGET_BURST_INTERVAL.as_nanos() / rtt.as_nanos()) as u64 ); assert_eq!(pacer.tokens, initial_tokens); - pacer.delay(rtt, mtu as u64, mtu, now, &unpaced_metrics(window / 2)); + pacer.delay( + rtt, + true, + mtu as u64, + mtu, + now, + &unpaced_metrics(window / 2), + ); assert_eq!( pacer.capacity, (window as u128 / 2 * TARGET_BURST_INTERVAL.as_nanos() / rtt.as_nanos()) as u64 ); assert_eq!(pacer.tokens, initial_tokens / 2); - pacer.delay(rtt, mtu as u64, mtu * 2, now, &unpaced_metrics(window)); + pacer.delay( + rtt, + true, + mtu as u64, + mtu * 2, + now, + &unpaced_metrics(window), + ); assert_eq!( pacer.capacity, (window as u128 * TARGET_BURST_INTERVAL.as_nanos() / rtt.as_nanos()) as u64 ); - pacer.delay(rtt, mtu as u64, 20_000, now, &unpaced_metrics(window)); + pacer.delay(rtt, true, mtu as u64, 20_000, now, &unpaced_metrics(window)); assert_eq!(pacer.capacity, 20_000_u64 * MIN_BURST_SIZE); } @@ -558,7 +658,14 @@ mod tests { for _ in 0..packet_capacity { assert_eq!( - pacer.delay(rtt, mtu as u64, mtu, old_instant, &unpaced_metrics(window)), + pacer.delay( + rtt, + true, + mtu as u64, + mtu, + old_instant, + &unpaced_metrics(window) + ), None, "When capacity is available packets should be sent immediately" ); @@ -569,7 +676,14 @@ mod tests { let pace_duration = Duration::from_nanos((TARGET_BURST_INTERVAL.as_nanos() * 4 / 5) as u64); let actual_delay = pacer - .delay(rtt, mtu as u64, mtu, old_instant, &unpaced_metrics(window)) + .delay( + rtt, + true, + mtu as u64, + mtu, + old_instant, + &unpaced_metrics(window), + ) .expect("Send must be delayed"); let diff = actual_delay.abs_diff(pace_duration); @@ -583,6 +697,7 @@ mod tests { assert_eq!( pacer.delay( rtt, + true, mtu as u64, mtu, old_instant + pace_duration / 2, @@ -594,7 +709,14 @@ mod tests { for _ in 0..packet_capacity / 2 { assert_eq!( - pacer.delay(rtt, mtu as u64, mtu, old_instant, &unpaced_metrics(window)), + pacer.delay( + rtt, + true, + mtu as u64, + mtu, + old_instant, + &unpaced_metrics(window) + ), None, "When capacity is available packets should be sent immediately" ); @@ -606,6 +728,7 @@ mod tests { assert_eq!( pacer.delay( rtt, + true, mtu as u64, mtu, old_instant + pace_duration * 3 / 2, @@ -625,14 +748,14 @@ mod tests { let mut pacer = Pacer::new(rtt, window, mtu, Some(2_000), old_instant); assert_eq!( - pacer.delay(rtt, 1_000, mtu, old_instant, &unpaced_metrics(window)), + pacer.delay(rtt, true, 1_000, mtu, old_instant, &unpaced_metrics(window)), None, "When capacity is available packets should be sent immediately" ); pacer.on_transmit(mtu); let actual_delay = pacer - .delay(rtt, 1_000, mtu, old_instant, &unpaced_metrics(window)) + .delay(rtt, true, 1_000, mtu, old_instant, &unpaced_metrics(window)) .expect("Send must be delayed"); let expected_delay = Duration::from_millis(500); @@ -647,7 +770,7 @@ mod tests { // Should be able to send after a while let now = old_instant + expected_delay / 2; assert_eq!( - pacer.delay(rtt, 500, mtu, now, &unpaced_metrics(window)), + pacer.delay(rtt, true, 500, mtu, now, &unpaced_metrics(window)), None ); } @@ -663,7 +786,7 @@ mod tests { let metrics = paced_metrics(window * 2, TEST_PACING_RATE, 2 * u64::from(mtu)); let mut pacer = Pacer::new(rtt, window, mtu, None, now); - pacer.delay(rtt, u64::from(mtu), mtu, now, &metrics); + pacer.delay(rtt, true, u64::from(mtu), mtu, now, &metrics); assert_eq!(pacer.capacity, rate_capacity(TEST_PACING_RATE, mtu)); // 2ms of traffic at 100 Mbit/s, i.e. `TARGET_BURST_INTERVAL` worth. @@ -732,12 +855,15 @@ mod tests { let mut pacer = Pacer::new(rtt, window, mtu, None, now); // Earn credit at the high rate, as during a ProbeBW_UP phase... - assert_eq!(pacer.delay(rtt, u64::from(mtu), mtu, now, &fast), None); + assert_eq!( + pacer.delay(rtt, true, u64::from(mtu), mtu, now, &fast), + None + ); assert_eq!(pacer.capacity, rate_capacity(TEST_PACING_RATE, mtu)); // ...then a gain change lowers it. Credit earned at the old rate must not survive as a // burst the new rate cannot pay for. - pacer.delay(rtt, u64::from(mtu), mtu, now, &slow); + pacer.delay(rtt, true, u64::from(mtu), mtu, now, &slow); let budget = rate_capacity(TEST_PACING_RATE / 10, mtu); assert_eq!(pacer.capacity, budget); @@ -760,6 +886,7 @@ mod tests { // `ControllerMetrics` allows. The rate path leaves its own budget behind... pacer.delay( rtt, + true, u64::from(mtu), mtu, now, @@ -769,11 +896,18 @@ mod tests { // ...so the window path must not mistake it for a budget of its own, even though // neither the window nor the MTU it keys on has changed. - pacer.delay(rtt, u64::from(mtu), mtu, now, &unpaced_metrics(window)); + pacer.delay( + rtt, + true, + u64::from(mtu), + mtu, + now, + &unpaced_metrics(window), + ); assert_eq!( pacer.capacity, - optimal_capacity(rtt, window, mtu), + optimal_capacity(rtt, window, mtu, true), "the window path kept a burst budget the rate path derived" ); } diff --git a/noq-proto/src/connection/paths.rs b/noq-proto/src/connection/paths.rs index 4f4560b1e4..1c5b73d2fd 100644 --- a/noq-proto/src/connection/paths.rs +++ b/noq-proto/src/connection/paths.rs @@ -621,6 +621,7 @@ impl PathData { let metrics = self.congestion.metrics(); self.pacing.delay( smoothed_rtt, + self.rtt.has_sample(), bytes_to_send, self.current_mtu(), now, @@ -817,6 +818,11 @@ impl RttEstimator { self.smoothed.unwrap_or(self.latest) } + /// Whether an RTT has been measured, or [`Self::get`] is still the initial RTT + pub(crate) fn has_sample(&self) -> bool { + self.smoothed.is_some() + } + /// Conservative estimate of RTT /// /// Takes the maximum of smoothed and latest RTT, as recommended diff --git a/noq-proto/src/tests/mod.rs b/noq-proto/src/tests/mod.rs index 52910c2b1a..6ec8c7350c 100644 --- a/noq-proto/src/tests/mod.rs +++ b/noq-proto/src/tests/mod.rs @@ -5457,7 +5457,11 @@ fn sent_packets_stats_snapshot_test() { let client_stats = pair.client_conn_mut(client_ch).stats(); let server_stats = pair.server_conn_mut(server_ch).stats(); assert_eq!(client_stats.sent_packets, 13); - assert_eq!(server_stats.sent_packets, 10); + // Before the first RTT sample the pacer lets the initial window go out in + // one burst (`pacing::optimal_capacity`), so the server's + // NEW_CONNECTION_IDs leave as 0.5-RTT data right behind its Handshake + // flight, instead of waiting for the packet that carries HANDSHAKE_DONE. + assert_eq!(server_stats.sent_packets, 11); let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); const MSG: &[u8] = b"Hello, World!"; @@ -5467,7 +5471,7 @@ fn sent_packets_stats_snapshot_test() { let client_stats2 = pair.client_conn_mut(client_ch).stats(); let server_stats2 = pair.server_conn_mut(server_ch).stats(); assert_eq!(client_stats2.sent_packets, 14); - assert_eq!(server_stats2.sent_packets, 11); + assert_eq!(server_stats2.sent_packets, 12); } #[cfg(feature = "qlog")] From 16c73d7a5535633a405f0d7ff53baa04a0150454 Mon Sep 17 00:00:00 2001 From: PQCrypta Date: Fri, 4 Dec 2026 13:09:02 -0600 Subject: [PATCH 2/2] refactor(proto): review: optimal_capacity with an explicit else and cmp, shorter doc comment --- noq-proto/src/connection/pacing.rs | 28 ++++++++++++---------------- noq-proto/src/connection/paths.rs | 2 +- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/noq-proto/src/connection/pacing.rs b/noq-proto/src/connection/pacing.rs index d20a42b637..fe11f1440a 100644 --- a/noq-proto/src/connection/pacing.rs +++ b/noq-proto/src/connection/pacing.rs @@ -1,5 +1,7 @@ //! Pacing of packet transmissions. +use core::cmp; + use crate::congestion::ControllerMetrics; use crate::{Duration, Instant, math}; @@ -250,30 +252,24 @@ fn rate_capacity(pacing_rate: u64, mtu: u16) -> u64 { /// Calculates a pacer capacity for a certain window and RTT, which imply a rate /// -/// Until the path has an RTT sample, `smoothed_rtt` is the configured initial -/// RTT (333 ms by default, RFC 9002 section 6.2.2), a value for timers rather -/// than a measurement. A slow path is then not something known, and the burst -/// may be the congestion window, the initial window at that point, as RFC 9002 -/// section 7.7 allows. Limiting it by [`MAX_BURST_INTERVAL`] of the initial -/// RTT instead allowed a single datagram, and refilled at a datagram per -/// ~27 ms: a ClientHello that takes two datagrams (any with an ML-KEM key -/// share) had its second held until the server's acknowledgement or that -/// timer, and a server's first flight after its first datagram the same way. -/// (A controller that reports a pacing rate never comes here: BBRv3 starts -/// from a 1 ms nominal RTT, so its first flight leaves in one burst too.) +/// Until the path has an RTT sample, `smoothed_rtt` is the configured initial RTT (RFC 9002 +/// section 6.2.2), a value for timers rather than a measurement. The burst may then be the +/// congestion window, which is the initial window at that point, as RFC 9002 section 7.7 allows, +/// bounded by [`MIN_BURST_SIZE`] and [`MAX_BURST_SIZE`]. fn optimal_capacity(smoothed_rtt: Duration, window: u64, mtu: u16, rtt_sampled: bool) -> u64 { let rtt = smoothed_rtt.as_nanos().max(1); let rate = u64::try_from(window as u128 * 1_000_000_000 / rtt).unwrap_or(u64::MAX); - if !rtt_sampled { + if rtt_sampled { + rate_capacity(rate, mtu) + } else { let mtu = u64::from(mtu); let target_capacity = ((rate as u128 * TARGET_BURST_INTERVAL.as_nanos()) / 1_000_000_000) as u64; - return Ord::min( + cmp::min( target_capacity.clamp(MIN_BURST_SIZE * mtu, MAX_BURST_SIZE * mtu), - Ord::max(window, mtu), - ); + cmp::max(window, mtu), + ) } - rate_capacity(rate, mtu) } /// Clamps the window to limit the sending rate to `max_bytes_per_second`. diff --git a/noq-proto/src/connection/paths.rs b/noq-proto/src/connection/paths.rs index 1c5b73d2fd..6a3afbc637 100644 --- a/noq-proto/src/connection/paths.rs +++ b/noq-proto/src/connection/paths.rs @@ -818,7 +818,7 @@ impl RttEstimator { self.smoothed.unwrap_or(self.latest) } - /// Whether an RTT has been measured, or [`Self::get`] is still the initial RTT + /// Whether an RTT has been measured, or [`Self::get`] is still the initial RTT. pub(crate) fn has_sample(&self) -> bool { self.smoothed.is_some() }