iroh_quinn_proto/congestion/
new_reno.rs

1use std::any::Any;
2use std::sync::Arc;
3
4use super::{BASE_DATAGRAM_SIZE, Controller, ControllerFactory};
5use crate::Instant;
6use crate::connection::RttEstimator;
7
8/// A simple, standard congestion controller
9#[derive(Debug, Clone)]
10pub struct NewReno {
11    config: Arc<NewRenoConfig>,
12    current_mtu: u64,
13    /// Maximum number of bytes in flight that may be sent.
14    window: u64,
15    /// Slow start threshold in bytes. When the congestion window is below ssthresh, the mode is
16    /// slow start and the window grows by the number of bytes acknowledged.
17    ssthresh: u64,
18    /// The time when QUIC first detects a loss, causing it to enter recovery. When a packet sent
19    /// after this time is acknowledged, QUIC exits recovery.
20    recovery_start_time: Instant,
21    /// Bytes which had been acked by the peer since leaving slow start
22    bytes_acked: u64,
23}
24
25impl NewReno {
26    /// Construct a state using the given `config` and current time `now`
27    pub fn new(config: Arc<NewRenoConfig>, now: Instant, current_mtu: u16) -> Self {
28        Self {
29            window: config.initial_window,
30            ssthresh: u64::MAX,
31            recovery_start_time: now,
32            current_mtu: current_mtu as u64,
33            config,
34            bytes_acked: 0,
35        }
36    }
37
38    fn minimum_window(&self) -> u64 {
39        2 * self.current_mtu
40    }
41}
42
43impl Controller for NewReno {
44    fn on_ack(
45        &mut self,
46        _now: Instant,
47        sent: Instant,
48        bytes: u64,
49        app_limited: bool,
50        _rtt: &RttEstimator,
51    ) {
52        if app_limited || sent <= self.recovery_start_time {
53            return;
54        }
55
56        if self.window < self.ssthresh {
57            // Slow start
58            self.window += bytes;
59
60            if self.window >= self.ssthresh {
61                // Exiting slow start
62                // Initialize `bytes_acked` for congestion avoidance. The idea
63                // here is that any bytes over `sshthresh` will already be counted
64                // towards the congestion avoidance phase - independent of when
65                // how close to `sshthresh` the `window` was when switching states,
66                // and independent of datagram sizes.
67                self.bytes_acked = self.window - self.ssthresh;
68            }
69        } else {
70            // Congestion avoidance
71            // This implementation uses the method which does not require
72            // floating point math, which also increases the window by 1 datagram
73            // for every round trip.
74            // This mechanism is called Appropriate Byte Counting in
75            // https://tools.ietf.org/html/rfc3465
76            self.bytes_acked += bytes;
77
78            if self.bytes_acked >= self.window {
79                self.bytes_acked -= self.window;
80                self.window += self.current_mtu;
81            }
82        }
83    }
84
85    fn on_congestion_event(
86        &mut self,
87        now: Instant,
88        sent: Instant,
89        is_persistent_congestion: bool,
90        _is_ecn: bool,
91        _lost_bytes: u64,
92    ) {
93        if sent <= self.recovery_start_time {
94            return;
95        }
96
97        self.recovery_start_time = now;
98        self.window = (self.window as f32 * self.config.loss_reduction_factor) as u64;
99        self.window = self.window.max(self.minimum_window());
100        self.ssthresh = self.window;
101
102        if is_persistent_congestion {
103            self.window = self.minimum_window();
104        }
105    }
106
107    fn on_mtu_update(&mut self, new_mtu: u16) {
108        self.current_mtu = new_mtu as u64;
109        self.window = self.window.max(self.minimum_window());
110    }
111
112    fn window(&self) -> u64 {
113        self.window
114    }
115
116    fn metrics(&self) -> super::ControllerMetrics {
117        super::ControllerMetrics {
118            congestion_window: self.window(),
119            ssthresh: Some(self.ssthresh),
120            pacing_rate: None,
121        }
122    }
123
124    fn clone_box(&self) -> Box<dyn Controller> {
125        Box::new(self.clone())
126    }
127
128    fn initial_window(&self) -> u64 {
129        self.config.initial_window
130    }
131
132    fn into_any(self: Box<Self>) -> Box<dyn Any> {
133        self
134    }
135}
136
137/// Configuration for the `NewReno` congestion controller
138#[derive(Debug, Clone)]
139pub struct NewRenoConfig {
140    initial_window: u64,
141    loss_reduction_factor: f32,
142}
143
144impl NewRenoConfig {
145    /// Default limit on the amount of outstanding data in bytes.
146    ///
147    /// Recommended value: `min(10 * max_datagram_size, max(2 * max_datagram_size, 14720))`
148    pub fn initial_window(&mut self, value: u64) -> &mut Self {
149        self.initial_window = value;
150        self
151    }
152
153    /// Reduction in congestion window when a new loss event is detected.
154    pub fn loss_reduction_factor(&mut self, value: f32) -> &mut Self {
155        self.loss_reduction_factor = value;
156        self
157    }
158}
159
160impl Default for NewRenoConfig {
161    fn default() -> Self {
162        Self {
163            initial_window: 14720.clamp(2 * BASE_DATAGRAM_SIZE, 10 * BASE_DATAGRAM_SIZE),
164            loss_reduction_factor: 0.5,
165        }
166    }
167}
168
169impl ControllerFactory for NewRenoConfig {
170    fn build(self: Arc<Self>, now: Instant, current_mtu: u16) -> Box<dyn Controller> {
171        Box::new(NewReno::new(self, now, current_mtu))
172    }
173}