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 _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#[derive(Debug, Clone)]
130pub struct NewRenoConfig {
131 initial_window: u64,
132 loss_reduction_factor: f32,
133}
134
135impl NewRenoConfig {
136 pub fn initial_window(&mut self, value: u64) -> &mut Self {
140 self.initial_window = value;
141 self
142 }
143
144 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}