| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
// Congestion control |
| 8 |
|
| 9 |
use std::{ |
| 10 |
fmt::{Debug, Display}, |
| 11 |
str::FromStr, |
| 12 |
time::{Duration, Instant}, |
| 13 |
}; |
| 14 |
|
| 15 |
use neqo_common::qlog::NeqoQlog; |
| 16 |
|
| 17 |
use crate::{recovery::SentPacket, rtt::RttEstimate, Error, Pmtud}; |
| 18 |
|
| 19 |
mod classic_cc; |
| 20 |
mod cubic; |
| 21 |
mod new_reno; |
| 22 |
|
| 23 |
pub use classic_cc::ClassicCongestionControl; |
| 24 |
#[cfg(test)] |
| 25 |
pub use classic_cc::CWND_INITIAL_PKTS; |
| 26 |
pub use cubic::Cubic; |
| 27 |
pub use new_reno::NewReno; |
| 28 |
|
| 29 |
pub trait CongestionControl: Display + Debug { |
| 30 |
fn set_qlog(&mut self, qlog: NeqoQlog); |
| 31 |
|
| 32 |
#[must_use] |
| 33 |
fn cwnd(&self) -> usize; |
| 34 |
|
| 35 |
#[must_use] |
| 36 |
fn bytes_in_flight(&self) -> usize; |
| 37 |
|
| 38 |
#[must_use] |
| 39 |
fn cwnd_avail(&self) -> usize; |
| 40 |
|
| 41 |
#[must_use] |
| 42 |
fn cwnd_min(&self) -> usize; |
| 43 |
|
| 44 |
#[cfg(test)] |
| 45 |
#[must_use] |
| 46 |
fn cwnd_initial(&self) -> usize; |
| 47 |
|
| 48 |
#[must_use] |
| 49 |
fn pmtud(&self) -> &Pmtud; |
| 50 |
|
| 51 |
#[must_use] |
| 52 |
fn pmtud_mut(&mut self) -> &mut Pmtud; |
| 53 |
|
| 54 |
fn on_packets_acked(&mut self, acked_pkts: &[SentPacket], rtt_est: &RttEstimate, now: Instant); |
| 55 |
|
| 56 |
/// Returns true if the congestion window was reduced. |
| 57 |
fn on_packets_lost( |
| 58 |
&mut self, |
| 59 |
first_rtt_sample_time: Option<Instant>, |
| 60 |
prev_largest_acked_sent: Option<Instant>, |
| 61 |
pto: Duration, |
| 62 |
lost_packets: &[SentPacket], |
| 63 |
now: Instant, |
| 64 |
) -> bool; |
| 65 |
|
| 66 |
/// Returns true if the congestion window was reduced. |
| 67 |
fn on_ecn_ce_received(&mut self, largest_acked_pkt: &SentPacket, now: Instant) -> bool; |
| 68 |
|
| 69 |
#[must_use] |
| 70 |
fn recovery_packet(&self) -> bool; |
| 71 |
|
| 72 |
fn discard(&mut self, pkt: &SentPacket, now: Instant); |
| 73 |
|
| 74 |
fn on_packet_sent(&mut self, pkt: &SentPacket, now: Instant); |
| 75 |
|
| 76 |
fn discard_in_flight(&mut self, now: Instant); |
| 77 |
} |
| 78 |
|
| 79 |
#[derive(Debug, Copy, Clone, Default)] |
| 80 |
pub enum CongestionControlAlgorithm { |
| 81 |
NewReno, |
| 82 |
#[default] |
| 83 |
Cubic, |
| 84 |
} |
| 85 |
|
| 86 |
// A `FromStr` implementation so that this can be used in command-line interfaces. |
| 87 |
impl FromStr for CongestionControlAlgorithm { |
| 88 |
type Err = Error; |
| 89 |
|
| 90 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 91 |
match s.trim().to_ascii_lowercase().as_str() { |
| 92 |
"newreno" | "reno" => Ok(Self::NewReno), |
| 93 |
"cubic" => Ok(Self::Cubic), |
| 94 |
_ => Err(Error::InvalidInput), |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
#[cfg(test)] |
| 100 |
mod tests; |