aboutsummaryrefslogtreecommitdiff
path: root/src/recovery/hystart.rs
blob: 4d2ead57f11aedec46d810b4a316fa2daf554a29 (plain)
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
// Copyright (C) 2020, Cloudflare, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

//! HyStart++
//!
//! This implementation is based on the following I-D:
//!
//! <https://datatracker.ietf.org/doc/html/draft-ietf-tcpm-hystartplusplus-04>

use std::cmp;
use std::time::Duration;
use std::time::Instant;

use crate::packet;
use crate::recovery;

/// Constants from I-D.
const MIN_RTT_THRESH: Duration = Duration::from_millis(4);

const MAX_RTT_THRESH: Duration = Duration::from_millis(16);

pub const N_RTT_SAMPLE: usize = 8;

pub const CSS_GROWTH_DIVISOR: usize = 4;

pub const CSS_ROUNDS: usize = 5;

#[derive(Default)]
pub struct Hystart {
    enabled: bool,

    window_end: Option<u64>,

    last_round_min_rtt: Duration,

    current_round_min_rtt: Duration,

    css_baseline_min_rtt: Duration,

    rtt_sample_count: usize,

    css_start_time: Option<Instant>,

    css_round_count: usize,
}

impl std::fmt::Debug for Hystart {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "window_end={:?} ", self.window_end)?;
        write!(f, "last_round_min_rtt={:?} ", self.last_round_min_rtt)?;
        write!(f, "current_round_min_rtt={:?} ", self.current_round_min_rtt)?;
        write!(f, "css_baseline_min_rtt={:?} ", self.css_baseline_min_rtt)?;
        write!(f, "rtt_sample_count={:?} ", self.rtt_sample_count)?;
        write!(f, "css_start_time={:?} ", self.css_start_time)?;
        write!(f, "css_round_count={:?}", self.css_round_count)?;

        Ok(())
    }
}

impl Hystart {
    pub fn new(enabled: bool) -> Self {
        Self {
            enabled,

            last_round_min_rtt: Duration::MAX,

            current_round_min_rtt: Duration::MAX,

            css_baseline_min_rtt: Duration::MAX,

            ..Default::default()
        }
    }

    pub fn reset(&mut self) {
        *self = Self::new(self.enabled);
    }

    pub fn enabled(&self) -> bool {
        self.enabled
    }

    pub fn css_start_time(&self) -> Option<Instant> {
        self.css_start_time
    }

    pub fn in_css(&self, epoch: packet::Epoch) -> bool {
        self.enabled &&
            epoch == packet::Epoch::Application &&
            self.css_start_time().is_some()
    }

    pub fn start_round(&mut self, pkt_num: u64) {
        if self.window_end.is_none() {
            self.window_end = Some(pkt_num);

            self.last_round_min_rtt = self.current_round_min_rtt;

            self.current_round_min_rtt = Duration::MAX;

            self.rtt_sample_count = 0;
        }
    }

    // On receiving ACK. Returns true if need to enter Congestion Avoidance.
    pub fn on_packet_acked(
        &mut self, epoch: packet::Epoch, packet: &recovery::Acked, rtt: Duration,
        now: Instant,
    ) -> bool {
        if !(self.enabled && epoch == packet::Epoch::Application) {
            return false;
        }

        self.current_round_min_rtt = cmp::min(self.current_round_min_rtt, rtt);

        self.rtt_sample_count += 1;

        // Slow Start.
        if self.css_start_time().is_none() {
            if self.rtt_sample_count >= N_RTT_SAMPLE &&
                self.current_round_min_rtt != Duration::MAX &&
                self.last_round_min_rtt != Duration::MAX
            {
                // clamp(min_rtt_thresh, last_round_min_rtt/8,
                // max_rtt_thresh)
                let rtt_thresh =
                    cmp::max(self.last_round_min_rtt / 8, MIN_RTT_THRESH);
                let rtt_thresh = cmp::min(rtt_thresh, MAX_RTT_THRESH);

                // Check if we can exit to CSS.
                if self.current_round_min_rtt >=
                    self.last_round_min_rtt.saturating_add(rtt_thresh)
                {
                    self.css_baseline_min_rtt = self.current_round_min_rtt;
                    self.css_start_time = Some(now);
                }
            }
        } else {
            // Conservative Slow Start.
            if self.rtt_sample_count >= N_RTT_SAMPLE {
                self.rtt_sample_count = 0;

                if self.current_round_min_rtt < self.css_baseline_min_rtt {
                    self.css_baseline_min_rtt = Duration::MAX;

                    // Back to Slow Start.
                    self.css_start_time = None;
                    self.css_round_count = 0;
                }
            }
        }

        // Check if we reached the end of the round.
        if let Some(end_pkt_num) = self.window_end {
            if packet.pkt_num >= end_pkt_num {
                // Start of a new round.
                self.window_end = None;

                if self.css_start_time().is_some() {
                    self.css_round_count += 1;

                    // End of CSS - exit to congestion avoidance.
                    if self.css_round_count >= CSS_ROUNDS {
                        self.css_round_count = 0;
                        return true;
                    }
                }
            }
        }

        false
    }

    // Return a cwnd increment during CSS (Conservative Slow Start).
    pub fn css_cwnd_inc(&self, pkt_size: usize) -> usize {
        pkt_size / CSS_GROWTH_DIVISOR
    }

    // Exit HyStart++ when entering congestion avoidance.
    pub fn congestion_event(&mut self) {
        self.window_end = None;
        self.css_start_time = None;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn start_round() {
        let mut hspp = Hystart::default();
        let pkt_num = 100;

        hspp.start_round(pkt_num);

        assert_eq!(hspp.window_end, Some(pkt_num));
        assert_eq!(hspp.current_round_min_rtt, Duration::MAX);
    }

    #[test]
    fn css_cwnd_inc() {
        let hspp = Hystart::default();
        let datagram_size = 1200;

        let css_cwnd_inc = hspp.css_cwnd_inc(datagram_size);

        assert_eq!(datagram_size / CSS_GROWTH_DIVISOR, css_cwnd_inc);
    }

    #[test]
    fn congestion_event() {
        let mut hspp = Hystart::default();
        let pkt_num = 100;

        hspp.start_round(pkt_num);

        assert_eq!(hspp.window_end, Some(pkt_num));

        // When moving into CA mode, window_end should be cleared.
        hspp.congestion_event();

        assert_eq!(hspp.window_end, None);
    }
}