iroh_quinn_proto/congestion/
new_reno.rs1use std::any::Any;
2use std::sync::Arc;
3
4use super::{BASE_DATAGRAM_SIZE, Controller, ControllerFactory};
5use crate::Instant;
6use crate::connection::RttEstimator;
7
8#[derive(Debug, Clone)]
10pub struct NewReno {
11 config: Arc<NewRenoConfig>,
12 current_mtu: u64,
13 window: u64,
15 ssthresh: u64,
18 recovery_start_time: Instant,
21 bytes_acked: u64,
23}
24
25impl NewReno {
26 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 self.window += bytes;
59
60 if self.window >= self.ssthresh {
61 self.bytes_acked = self.window - self.ssthresh;
68 }
69 } else {
70 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#[derive(Debug, Clone)]
139pub struct NewRenoConfig {
140 initial_window: u64,
141 loss_reduction_factor: f32,
142}
143
144impl NewRenoConfig {
145 pub fn initial_window(&mut self, value: u64) -> &mut Self {
149 self.initial_window = value;
150 self
151 }
152
153 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}