1 // Copyright (C) 2022, Cloudflare, Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 use super::*;
28 use crate::recovery::Recovery;
29
30 use std::time::Duration;
31 use std::time::Instant;
32
33 // BBR Functions at Initialization.
34 //
35
36 // 4.3.1. Initialization Steps
bbr_init(r: &mut Recovery)37 pub fn bbr_init(r: &mut Recovery) {
38 let rtt = r.rtt();
39 let bbr = &mut r.bbr_state;
40
41 bbr.rtprop = rtt;
42 bbr.rtprop_stamp = Instant::now();
43 bbr.next_round_delivered = r.delivery_rate.delivered();
44
45 r.send_quantum = r.max_datagram_size;
46
47 bbr_init_round_counting(r);
48 bbr_init_full_pipe(r);
49 bbr_init_pacing_rate(r);
50 bbr_enter_startup(r);
51 }
52
53 // 4.1.1.3. Tracking Time for the BBR.BtlBw Max Filter
bbr_init_round_counting(r: &mut Recovery)54 fn bbr_init_round_counting(r: &mut Recovery) {
55 let bbr = &mut r.bbr_state;
56
57 bbr.next_round_delivered = 0;
58 bbr.round_start = false;
59 bbr.round_count = 0;
60 }
61
62 // 4.2.1. Pacing Rate
bbr_init_pacing_rate(r: &mut Recovery)63 fn bbr_init_pacing_rate(r: &mut Recovery) {
64 let bbr = &mut r.bbr_state;
65
66 let srtt = r
67 .smoothed_rtt
68 .unwrap_or_else(|| Duration::from_millis(1))
69 .as_secs_f64();
70
71 // At init, cwnd is initcwnd.
72 let nominal_bandwidth = r.congestion_window as f64 / srtt;
73
74 bbr.pacing_rate = (bbr.pacing_gain * nominal_bandwidth) as u64;
75 }
76
77 // 4.3.2.1. Startup Dynamics
bbr_enter_startup(r: &mut Recovery)78 pub fn bbr_enter_startup(r: &mut Recovery) {
79 let bbr = &mut r.bbr_state;
80
81 bbr.state = BBRStateMachine::Startup;
82 bbr.pacing_gain = BBR_HIGH_GAIN;
83 bbr.cwnd_gain = BBR_HIGH_GAIN;
84 }
85
86 // 4.3.2.2. Estimating When Startup has Filled the Pipe
bbr_init_full_pipe(r: &mut Recovery)87 fn bbr_init_full_pipe(r: &mut Recovery) {
88 let bbr = &mut r.bbr_state;
89
90 bbr.filled_pipe = false;
91 bbr.full_bw = 0;
92 bbr.full_bw_count = 0;
93 }
94