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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use crate::{
random,
recovery::{
bandwidth, bbr,
bbr::{probe_rtt, round, BbrCongestionController},
congestion_controller::Publisher,
},
time::{Timer, Timestamp},
};
use core::time::Duration;
use num_rational::Ratio;
//= https://tools.ietf.org/id/draft-cardwell-iccrg-bbr-congestion-control-02#2.14.2
//# A constant specifying the minimum duration for which ProbeRTT state
//# holds inflight to BBRMinPipeCwnd or fewer packets: 200 ms.
const PROBE_RTT_DURATION: Duration = Duration::from_millis(200);
//= https://tools.ietf.org/id/draft-cardwell-iccrg-bbr-congestion-control-02#4.3.4.4
//# BBREnterProbeRTT():
//# BBR.state = ProbeRTT
//# BBR.pacing_gain = 1
pub(crate) const PACING_GAIN: Ratio<u64> = Ratio::new_raw(1, 1);
//= https://tools.ietf.org/id/draft-cardwell-iccrg-bbr-congestion-control-02#2.14.2
//# A constant specifying the gain value for calculating the cwnd during ProbeRTT: 0.5
pub(crate) const CWND_GAIN: Ratio<u64> = Ratio::new_raw(1, 2);
#[derive(Clone, Debug, Default)]
pub(crate) struct State {
timer: Timer,
round_done: bool,
}
impl State {
/// Keeps BBR in the `ProbeRTT` state for max of (PROBE_RTT_DURATION, 1 round)
fn handle_probe_rtt(
&mut self,
bw_estimator: &mut bandwidth::Estimator,
round_counter: &mut round::Counter,
probe_rtt_cwnd: u32,
bytes_in_flight: u32,
now: Timestamp,
) {
//= https://tools.ietf.org/id/draft-cardwell-iccrg-bbr-congestion-control-02#4.3.4.4
//# BBRHandleProbeRTT()
//# /* Ignore low rate samples during ProbeRTT: */
//# MarkConnectionAppLimited()
//# if (BBR.probe_rtt_done_stamp == 0 and
//# packets_in_flight <= BBRProbeRTTCwnd())
//# /* Wait for at least ProbeRTTDuration to elapse: */
//# BBR.probe_rtt_done_stamp =
//# Now() + ProbeRTTDuration
//# /* Wait for at least one round to elapse: */
//# BBR.probe_rtt_round_done = false
//# BBRStartRound()
//# else if (BBR.probe_rtt_done_stamp != 0)
//# if (BBR.round_start)
//# BBR.probe_rtt_round_done = true
//# if (BBR.probe_rtt_round_done)
//# BBRCheckProbeRTTDone()
// Ignore low rate samples during ProbeRTT
bw_estimator.on_app_limited(bytes_in_flight);
if !self.timer.is_armed() && bytes_in_flight <= probe_rtt_cwnd {
// Wait for at least ProbeRTTDuration to elapse:
self.timer.set(now + PROBE_RTT_DURATION);
// Wait for at least one round to elapse:
self.round_done = false;
round_counter.set_round_end(bw_estimator.delivered_bytes());
} else if self.timer.is_armed() && round_counter.round_start() {
self.round_done = true;
}
}
/// Returns true if the `ProbeRtt` state is done and should be exited
pub fn is_done(&self, now: Timestamp) -> bool {
self.round_done && self.timer.is_expired(now)
}
}
/// Methods related to the `ProbeRtt` state
impl BbrCongestionController {
/// Check if it is time to start probing for RTT changes, and enter the ProbeRtt state if so
#[inline]
pub(super) fn check_probe_rtt<Pub: Publisher>(
&mut self,
random_generator: &mut dyn random::Generator,
now: Timestamp,
publisher: &mut Pub,
) {
//= https://tools.ietf.org/id/draft-cardwell-iccrg-bbr-congestion-control-02#4.3.4.4
//# BBRCheckProbeRTT()
//# if (BBR.state != ProbeRTT and
//# BBR.probe_rtt_expired and
//# not BBR.idle_restart)
//# BBREnterProbeRTT()
//# BBRSaveCwnd()
//# BBR.probe_rtt_done_stamp = 0
//# BBR.ack_phase = ACKS_PROBE_STOPPING
//# BBRStartRound()
//# if (BBR.state == ProbeRTT)
//# BBRHandleProbeRTT()
//# if (rs.delivered > 0)
//# BBR.idle_restart = false
// `BBR.probe_rtt_done_stamp = 0`, which is equivalent to `probe_rtt::State.timer.cancel`, is
// not necessary, as the timer is contained with the `probe_rtt::State`, and is thus unarmed
// whenever BBR is not in the `ProbeRTT` state
// `BBR.ack_phase = ACKS_PROBE_STOPPING` is not performed here as `ack_phase` is only used by
// the `ProbeBW` state, which is initialized to `ACKS_PROBE_STOPPING` every time it is
// reentered
if !self.state.is_probing_rtt()
&& self.data_volume_model.probe_rtt_expired()
&& !self.idle_restart
{
// New BBR state requires updating the model
self.try_fast_path = false;
self.state
.transition_to(bbr::State::ProbeRtt(State::default()), publisher);
self.save_cwnd();
self.round_counter
.set_round_end(self.bw_estimator.delivered_bytes());
}
let probe_rtt_cwnd = self.probe_rtt_cwnd();
if let bbr::State::ProbeRtt(probe_rtt_state) = &mut self.state {
probe_rtt_state.handle_probe_rtt(
&mut self.bw_estimator,
&mut self.round_counter,
probe_rtt_cwnd,
*self.bytes_in_flight,
now,
);
// The RFC pseudocode exits `ProbeRTT` internal to `BBRHandleProbeRTT`, whereas this
// code checks if the `ProbeRTT` state is ready to exit here
if probe_rtt_state.is_done(now) {
self.exit_probe_rtt(random_generator, now, publisher);
}
}
if self.bw_estimator.rate_sample().delivered_bytes > 0 {
self.idle_restart = false;
}
}
/// Exits the `ProbeRtt` state
#[inline]
pub(super) fn exit_probe_rtt<Pub: Publisher>(
&mut self,
random_generator: &mut dyn random::Generator,
now: Timestamp,
publisher: &mut Pub,
) {
//= https://tools.ietf.org/id/draft-cardwell-iccrg-bbr-congestion-control-02#4.3.4.4
//# BBRCheckProbeRTTDone():
//# if (BBR.probe_rtt_done_stamp != 0 and
//# Now() > BBR.probe_rtt_done_stamp)
//# /* schedule next ProbeRTT: */
//# BBR.probe_rtt_min_stamp = Now()
//# BBRRestoreCwnd()
//# BBRExitProbeRTT()
if cfg!(debug_assertions) {
// BBR.probe_rtt_done_stamp != 0 and Now() > BBR.probe_rtt_done_stamp should be
// checked by calling `probe_rtt_state.is_done(now)` prior to calling `exit_probe_rtt`
assert!(self.state.is_probing_rtt());
if let bbr::State::ProbeRtt(probe_rtt_state) = &self.state {
assert!(probe_rtt_state.is_done(now));
}
}
// schedule next ProbeRTT:
self.data_volume_model.schedule_next_probe_rtt(now);
self.restore_cwnd();
//= https://tools.ietf.org/id/draft-cardwell-iccrg-bbr-congestion-control-02#4.3.4.5
//# BBRExitProbeRTT()
//# BBRResetLowerBounds()
//# if (BBR.filled_pipe)
//# BBRStartProbeBW_DOWN()
//# BBRStartProbeBW_CRUISE()
//# else
//# BBREnterStartup()
self.data_volume_model.reset_lower_bound();
self.data_rate_model.reset_lower_bound();
if self.full_pipe_estimator.filled_pipe() {
//= https://tools.ietf.org/id/draft-cardwell-iccrg-bbr-congestion-control-02#4.3.4.5
//# as an optimization, since the connection is exiting ProbeRTT, we know that infligh
//# is already below the estimated BDP, so the connection can proceed immediately to
//# ProbeBW_CRUISE
let cruise_immediately = true;
self.enter_probe_bw(cruise_immediately, random_generator, now, publisher);
} else {
self.enter_startup(publisher);
}
}
/// Returns the congestion window that should be used during the `ProbeRTT` state
#[inline]
pub(super) fn probe_rtt_cwnd(&self) -> u32 {
//= https://tools.ietf.org/id/draft-cardwell-iccrg-bbr-congestion-control-02#4.6.4.5
//# BBRProbeRTTCwnd():
//# probe_rtt_cwnd = BBRBDPMultiple(BBR.bw, BBRProbeRTTCwndGain)
//# probe_rtt_cwnd = max(probe_rtt_cwnd, BBRMinPipeCwnd)
//# return probe_rtt_cwnd
self.bdp_multiple(self.data_rate_model.bw(), probe_rtt::CWND_GAIN)
.try_into()
.unwrap_or(u32::MAX)
.max(Self::minimum_window(self.max_datagram_size))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
event, path,
path::MINIMUM_MAX_DATAGRAM_SIZE,
recovery::{
bandwidth::{Bandwidth, PacketInfo, RateSample},
bbr::windowed_filter::PROBE_RTT_INTERVAL,
congestion_controller::PathPublisher,
},
time::{Clock, NoopClock},
};
#[test]
fn check_probe_rtt_enter_probe_rtt() {
let mut bbr = BbrCongestionController::new(MINIMUM_MAX_DATAGRAM_SIZE, Default::default());
let now = NoopClock.get_time();
let mut rng = random::testing::Generator::default();
let mut publisher = event::testing::Publisher::snapshot();
let mut publisher = PathPublisher::new(&mut publisher, path::Id::test_id());
bbr.bw_estimator.set_delivered_bytes_for_test(1000);
bbr.data_volume_model
.update_min_rtt(Duration::from_millis(100), now);
let now = now + PROBE_RTT_INTERVAL;
bbr.data_volume_model
.update_min_rtt(Duration::from_millis(100), now);
assert!(bbr.data_volume_model.probe_rtt_expired());
assert!(!bbr.idle_restart);
bbr.check_probe_rtt(&mut rng, now, &mut publisher);
assert!(bbr.state.is_probing_rtt());
assert!(!bbr.try_fast_path);
assert_eq!(bbr.prior_cwnd, bbr.cwnd);
assert_eq!(1000, bbr.round_counter.round_end());
}
#[test]
fn check_probe_rtt_exit_probe_rtt() {
let mut bbr = bbr_in_probe_rtt_ready_to_exit();
let mut rng = random::testing::Generator::default();
let now = NoopClock.get_time();
let mut publisher = event::testing::Publisher::snapshot();
let mut publisher = PathPublisher::new(&mut publisher, path::Id::test_id());
bbr.idle_restart = true;
bbr.check_probe_rtt(&mut rng, now, &mut publisher);
assert!(!bbr.state.is_probing_rtt());
// No delivered bytes in the rate sample, so remain in idle restart
assert!(bbr.idle_restart);
// Next probe rtt is scheduled
assert_eq!(
Some(now + PROBE_RTT_INTERVAL),
bbr.data_volume_model.next_probe_rtt()
);
let mut bbr = bbr_in_probe_rtt_ready_to_exit();
bbr.idle_restart = true;
bbr.bw_estimator.set_rate_sample_for_test(RateSample {
delivered_bytes: 1000,
..Default::default()
});
bbr.check_probe_rtt(&mut rng, now, &mut publisher);
assert!(!bbr.state.is_probing_rtt());
// Positive elivered bytes in the rate sample, so set idle restart to false
assert!(!bbr.idle_restart);
// Next probe rtt is scheduled
assert_eq!(
Some(now + PROBE_RTT_INTERVAL),
bbr.data_volume_model.next_probe_rtt()
);
}
#[test]
fn exit_probe_rtt() {
let mut bbr = bbr_in_probe_rtt_ready_to_exit();
let mut rng = random::testing::Generator::default();
let mut publisher = event::testing::Publisher::snapshot();
let mut publisher = PathPublisher::new(&mut publisher, path::Id::test_id());
let now = NoopClock.get_time();
bbr.cwnd = 1000;
bbr.prior_cwnd = 2000;
bbr.data_volume_model.set_inflight_lo_for_test(100_000);
bbr.data_rate_model
.update_lower_bound(Bandwidth::new(1000, Duration::from_millis(1)));
bbr.exit_probe_rtt(&mut rng, now, &mut publisher);
// cwnd restored
assert_eq!(2000, bbr.cwnd);
// lower bounds reset
assert_eq!(u64::MAX, bbr.data_volume_model.inflight_lo());
assert_eq!(Bandwidth::INFINITY, bbr.data_rate_model.bw_lo());
assert!(bbr.state.is_startup());
// If full pipe then transition to probe bw cruise
let mut bbr = bbr_in_probe_rtt_ready_to_exit();
bbr.full_pipe_estimator.set_filled_pipe_for_test(true);
bbr.exit_probe_rtt(&mut rng, now, &mut publisher);
assert!(bbr.state.is_probing_bw_cruise());
// Next probe rtt is scheduled
assert_eq!(
Some(now + PROBE_RTT_INTERVAL),
bbr.data_volume_model.next_probe_rtt()
);
}
//= https://tools.ietf.org/id/draft-cardwell-iccrg-bbr-congestion-control-02#4.6.4.5
//= type=test
//# BBRProbeRTTCwnd():
//# probe_rtt_cwnd = BBRBDPMultiple(BBR.bw, BBRProbeRTTCwndGain)
//# probe_rtt_cwnd = max(probe_rtt_cwnd, BBRMinPipeCwnd)
//# return probe_rtt_cwnd
#[test]
fn probe_rtt_cwnd() {
let mut bbr = BbrCongestionController::new(MINIMUM_MAX_DATAGRAM_SIZE, Default::default());
let now = NoopClock.get_time();
// bdp_multiple > minimum_window
assert_eq!(
BbrCongestionController::initial_window(MINIMUM_MAX_DATAGRAM_SIZE, &Default::default()),
bbr.probe_rtt_cwnd()
);
bbr.data_volume_model
.update_min_rtt(Duration::from_millis(100), now);
// bdp_multiple < minimum_window
assert_eq!(
BbrCongestionController::minimum_window(bbr.max_datagram_size),
bbr.probe_rtt_cwnd()
);
}
/// Helper method to return a BBR congestion controller in the ProbeRtt
/// but ready to exit that state
fn bbr_in_probe_rtt_ready_to_exit() -> BbrCongestionController {
let mut bbr = BbrCongestionController::new(MINIMUM_MAX_DATAGRAM_SIZE, Default::default());
let now = NoopClock.get_time();
let mut probe_rtt_state = probe_rtt::State {
timer: Default::default(),
round_done: false,
};
probe_rtt_state.timer.set(now);
probe_rtt_state.round_done = true;
bbr.state = bbr::State::ProbeRtt(probe_rtt_state);
bbr.round_counter.set_round_end(100);
let packet_info = PacketInfo {
delivered_bytes: 100,
delivered_time: now,
lost_bytes: 0,
ecn_ce_count: 0,
first_sent_time: now,
bytes_in_flight: 0,
is_app_limited: false,
};
bbr.round_counter.on_ack(packet_info, 200);
assert!(bbr.round_counter.round_start());
bbr.bw_estimator.set_delivered_bytes_for_test(200);
bbr
}
}