-
Notifications
You must be signed in to change notification settings - Fork 124
/
rtt.rs
210 lines (183 loc) · 6.08 KB
/
rtt.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Tracking of sent packets and detecting their loss.
use std::{
cmp::{max, min},
time::{Duration, Instant},
};
use neqo_common::{qlog::NeqoQlog, qtrace};
use crate::{
ackrate::{AckRate, PeerAckDelay},
packet::PacketBuilder,
qlog::{self, QlogMetric},
recovery::RecoveryToken,
stats::FrameStats,
tracking::PacketNumberSpace,
};
/// The smallest time that the system timer (via `sleep()`, `nanosleep()`,
/// `select()`, or similar) can reliably deliver; see `neqo_common::hrtime`.
pub const GRANULARITY: Duration = Duration::from_millis(1);
// Defined in -recovery 6.2 as 333ms but using lower value.
pub const INITIAL_RTT: Duration = Duration::from_millis(100);
#[derive(Debug)]
#[allow(clippy::module_name_repetitions)]
pub struct RttEstimate {
first_sample_time: Option<Instant>,
latest_rtt: Duration,
smoothed_rtt: Duration,
rttvar: Duration,
min_rtt: Duration,
ack_delay: PeerAckDelay,
}
impl RttEstimate {
fn init(&mut self, rtt: Duration) {
// Only allow this when there are no samples.
debug_assert!(self.first_sample_time.is_none());
self.latest_rtt = rtt;
self.min_rtt = rtt;
self.smoothed_rtt = rtt;
self.rttvar = rtt / 2;
}
#[cfg(test)]
pub const fn from_duration(rtt: Duration) -> Self {
Self {
first_sample_time: None,
latest_rtt: rtt,
smoothed_rtt: rtt,
rttvar: Duration::from_millis(0),
min_rtt: rtt,
ack_delay: PeerAckDelay::Fixed(Duration::from_millis(25)),
}
}
pub fn set_initial(&mut self, rtt: Duration) {
qtrace!("initial RTT={:?}", rtt);
if rtt >= GRANULARITY {
// Ignore if the value is too small.
self.init(rtt);
}
}
/// For a new path, prime the RTT based on the state of another path.
pub fn prime_rtt(&mut self, other: &Self) {
self.set_initial(other.smoothed_rtt + other.rttvar);
self.ack_delay = other.ack_delay.clone();
}
pub fn set_ack_delay(&mut self, ack_delay: PeerAckDelay) {
self.ack_delay = ack_delay;
}
pub fn update_ack_delay(&mut self, cwnd: usize, mtu: usize) {
self.ack_delay.update(cwnd, mtu, self.smoothed_rtt);
}
pub fn update(
&mut self,
qlog: &NeqoQlog,
mut rtt_sample: Duration,
ack_delay: Duration,
confirmed: bool,
now: Instant,
) {
// Limit ack delay by max_ack_delay if confirmed.
let mad = self.ack_delay.max();
let ack_delay = if confirmed && ack_delay > mad {
mad
} else {
ack_delay
};
// min_rtt ignores ack delay.
self.min_rtt = min(self.min_rtt, rtt_sample);
// Adjust for ack delay unless it goes below `min_rtt`.
if rtt_sample - self.min_rtt >= ack_delay {
rtt_sample -= ack_delay;
}
if self.first_sample_time.is_none() {
self.init(rtt_sample);
self.first_sample_time = Some(now);
} else {
// Calculate EWMA RTT (based on {{?RFC6298}}).
let rttvar_sample = if self.smoothed_rtt > rtt_sample {
self.smoothed_rtt - rtt_sample
} else {
rtt_sample - self.smoothed_rtt
};
self.latest_rtt = rtt_sample;
self.rttvar = (self.rttvar * 3 + rttvar_sample) / 4;
self.smoothed_rtt = (self.smoothed_rtt * 7 + rtt_sample) / 8;
}
qtrace!(
"RTT latest={:?} -> estimate={:?}~{:?}",
self.latest_rtt,
self.smoothed_rtt,
self.rttvar
);
qlog::metrics_updated(
qlog,
&[
QlogMetric::LatestRtt(self.latest_rtt),
QlogMetric::MinRtt(self.min_rtt),
QlogMetric::SmoothedRtt(self.smoothed_rtt),
QlogMetric::RttVariance(self.rttvar),
],
);
}
/// Get the estimated value.
pub const fn estimate(&self) -> Duration {
self.smoothed_rtt
}
pub fn pto(&self, pn_space: PacketNumberSpace) -> Duration {
let mut t = self.estimate() + max(4 * self.rttvar, GRANULARITY);
if pn_space == PacketNumberSpace::ApplicationData {
t += self.ack_delay.max();
}
t
}
/// Calculate the loss delay based on the current estimate and the last
/// RTT measurement received.
pub fn loss_delay(&self) -> Duration {
// kTimeThreshold = 9/8
// loss_delay = kTimeThreshold * max(latest_rtt, smoothed_rtt)
// loss_delay = max(loss_delay, kGranularity)
let rtt = max(self.latest_rtt, self.smoothed_rtt);
max(rtt * 9 / 8, GRANULARITY)
}
pub const fn first_sample_time(&self) -> Option<Instant> {
self.first_sample_time
}
#[cfg(test)]
pub const fn latest(&self) -> Duration {
self.latest_rtt
}
pub const fn rttvar(&self) -> Duration {
self.rttvar
}
pub const fn minimum(&self) -> Duration {
self.min_rtt
}
pub fn write_frames(
&mut self,
builder: &mut PacketBuilder,
tokens: &mut Vec<RecoveryToken>,
stats: &mut FrameStats,
) {
self.ack_delay.write_frames(builder, tokens, stats);
}
pub fn frame_lost(&mut self, lost: &AckRate) {
self.ack_delay.frame_lost(lost);
}
pub fn frame_acked(&mut self, acked: &AckRate) {
self.ack_delay.frame_acked(acked);
}
}
impl Default for RttEstimate {
fn default() -> Self {
Self {
first_sample_time: None,
latest_rtt: INITIAL_RTT,
smoothed_rtt: INITIAL_RTT,
rttvar: INITIAL_RTT / 2,
min_rtt: INITIAL_RTT,
ack_delay: PeerAckDelay::default(),
}
}
}