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        _lost_bytes: u64,
91    ) {
92        if sent <= self.recovery_start_time {
93            return;
94        }
95
96        self.recovery_start_time = now;
97        self.window = (self.window as f32 * self.config.loss_reduction_factor) as u64;
98        self.window = self.window.max(self.minimum_window());
99        self.ssthresh = self.window;
100
101        if is_persistent_congestion {
102            self.window = self.minimum_window();
103        }
104    }
105
106    fn on_mtu_update(&mut self, new_mtu: u16) {
107        self.current_mtu = new_mtu as u64;
108        self.window = self.window.max(self.minimum_window());
109    }
110
111    fn window(&self) -> u64 {
112        self.window
113    }
114
115    fn clone_box(&self) -> Box<dyn Controller> {
116        Box::new(self.clone())
117    }
118
119    fn initial_window(&self) -> u64 {
120        self.config.initial_window
121    }
122
123    fn into_any(self: Box<Self>) -> Box<dyn Any> {
124        self
125    }
126}
127
128/// Configuration for the `NewReno` congestion controller
129#[derive(Debug, Clone)]
130pub struct NewRenoConfig {
131    initial_window: u64,
132    loss_reduction_factor: f32,
133}
134
135impl NewRenoConfig {
136    /// Default limit on the amount of outstanding data in bytes.
137    ///
138    /// Recommended value: `min(10 * max_datagram_size, max(2 * max_datagram_size, 14720))`
139    pub fn initial_window(&mut self, value: u64) -> &mut Self {
140        self.initial_window = value;
141        self
142    }
143
144    /// Reduction in congestion window when a new loss event is detected.
145    pub fn loss_reduction_factor(&mut self, value: f32) -> &mut Self {
146        self.loss_reduction_factor = value;
147        self
148    }
149}
150
151impl Default for NewRenoConfig {
152    fn default() -> Self {
153        Self {
154            initial_window: 14720.clamp(2 * BASE_DATAGRAM_SIZE, 10 * BASE_DATAGRAM_SIZE),
155            loss_reduction_factor: 0.5,
156        }
157    }
158}
159
160impl ControllerFactory for NewRenoConfig {
161    fn build(self: Arc<Self>, now: Instant, current_mtu: u16) -> Box<dyn Controller> {
162        Box::new(NewReno::new(self, now, current_mtu))
163    }
164}