diff --git a/noq-proto/src/connection/pacing.rs b/noq-proto/src/connection/pacing.rs index d9798e7929..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}; @@ -30,6 +32,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 +46,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 +77,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 +91,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 +110,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,10 +251,25 @@ 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 (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); - rate_capacity(rate, mtu) + 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; + cmp::min( + target_capacity.clamp(MIN_BURST_SIZE * mtu, MAX_BURST_SIZE * mtu), + cmp::max(window, mtu), + ) + } } /// Clamps the window to limit the sending rate to `max_bytes_per_second`. @@ -323,7 +353,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 +389,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 +434,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 +483,7 @@ mod tests { Pacer::new(rtt, 30000, 1500, None, new_instant) .delay( Duration::from_micros(0), + true, 0, 1500, old_instant, @@ -464,6 +495,7 @@ mod tests { Pacer::new(rtt, 30000, 1500, None, new_instant) .delay( Duration::from_micros(0), + true, 1600, 1500, old_instant, @@ -475,6 +507,7 @@ mod tests { Pacer::new(rtt, 30000, 1500, None, new_instant) .delay( Duration::from_micros(0), + true, 1500, 1500, old_instant, @@ -484,6 +517,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 +597,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 +654,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 +672,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 +693,7 @@ mod tests { assert_eq!( pacer.delay( rtt, + true, mtu as u64, mtu, old_instant + pace_duration / 2, @@ -594,7 +705,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 +724,7 @@ mod tests { assert_eq!( pacer.delay( rtt, + true, mtu as u64, mtu, old_instant + pace_duration * 3 / 2, @@ -625,14 +744,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 +766,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 +782,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 +851,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 +882,7 @@ mod tests { // `ControllerMetrics` allows. The rate path leaves its own budget behind... pacer.delay( rtt, + true, u64::from(mtu), mtu, now, @@ -769,11 +892,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..6a3afbc637 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")]