use std::cmp;
use std::time::Duration;
use std::time::Instant;
use crate::packet;
use crate::recovery;
use crate::recovery::reno;
use crate::recovery::Acked;
use crate::recovery::CongestionControlOps;
use crate::recovery::Recovery;
use crate::recovery::Sent;
pub static CUBIC: CongestionControlOps = CongestionControlOps {
on_init,
reset,
on_packet_sent,
on_packets_acked,
congestion_event,
collapse_cwnd,
checkpoint,
rollback,
has_custom_pacing,
debug_fmt,
};
const BETA_CUBIC: f64 = 0.7;
const C: f64 = 0.4;
const ROLLBACK_THRESHOLD_PERCENT: usize = 20;
const MIN_ROLLBACK_THRESHOLD: usize = 2;
const ALPHA_AIMD: f64 = 3.0 * (1.0 - BETA_CUBIC) / (1.0 + BETA_CUBIC);
#[derive(Debug, Default)]
pub struct State {
k: f64,
w_max: f64,
w_est: f64,
alpha_aimd: f64,
last_sent_time: Option<Instant>,
cwnd_inc: usize,
prior: PriorState,
}
#[derive(Debug, Default)]
struct PriorState {
congestion_window: usize,
ssthresh: usize,
w_max: f64,
k: f64,
epoch_start: Option<Instant>,
lost_count: usize,
}
impl State {
fn cubic_k(&self, cwnd: usize, max_datagram_size: usize) -> f64 {
let w_max = self.w_max / max_datagram_size as f64;
let cwnd = cwnd as f64 / max_datagram_size as f64;
libm::cbrt((w_max - cwnd) / C)
}
fn w_cubic(&self, t: Duration, max_datagram_size: usize) -> f64 {
let w_max = self.w_max / max_datagram_size as f64;
(C * (t.as_secs_f64() - self.k).powi(3) + w_max) *
max_datagram_size as f64
}
fn w_est_inc(
&self, acked: usize, cwnd: usize, max_datagram_size: usize,
) -> f64 {
self.alpha_aimd * (acked as f64 / cwnd as f64) * max_datagram_size as f64
}
}
fn on_init(_r: &mut Recovery) {}
fn reset(r: &mut Recovery) {
r.cubic_state = State::default();
}
fn collapse_cwnd(r: &mut Recovery) {
let cubic = &mut r.cubic_state;
r.congestion_recovery_start_time = None;
cubic.w_max = r.congestion_window as f64;
r.ssthresh = (r.congestion_window as f64 * BETA_CUBIC) as usize;
r.ssthresh = cmp::max(
r.ssthresh,
r.max_datagram_size * recovery::MINIMUM_WINDOW_PACKETS,
);
cubic.cwnd_inc = 0;
reno::collapse_cwnd(r);
}
fn on_packet_sent(r: &mut Recovery, sent_bytes: usize, now: Instant) {
let cubic = &mut r.cubic_state;
if let Some(last_sent_time) = cubic.last_sent_time {
if r.bytes_in_flight == 0 {
let delta = now - last_sent_time;
if let Some(recovery_start_time) = r.congestion_recovery_start_time {
if delta.as_nanos() > 0 {
r.congestion_recovery_start_time =
Some(recovery_start_time + delta);
}
}
}
}
cubic.last_sent_time = Some(now);
reno::on_packet_sent(r, sent_bytes, now);
}
fn on_packets_acked(
r: &mut Recovery, packets: &mut Vec<Acked>, epoch: packet::Epoch,
now: Instant,
) {
for pkt in packets.drain(..) {
on_packet_acked(r, &pkt, epoch, now);
}
}
fn on_packet_acked(
r: &mut Recovery, packet: &Acked, epoch: packet::Epoch, now: Instant,
) {
let in_congestion_recovery = r.in_congestion_recovery(packet.time_sent);
r.bytes_in_flight = r.bytes_in_flight.saturating_sub(packet.size);
if in_congestion_recovery {
r.prr.on_packet_acked(
packet.size,
r.bytes_in_flight,
r.ssthresh,
r.max_datagram_size,
);
return;
}
if r.app_limited {
return;
}
if r.congestion_recovery_start_time.is_some() {
let new_lost = r.lost_count - r.cubic_state.prior.lost_count;
let rollback_threshold = (r.congestion_window / r.max_datagram_size) *
ROLLBACK_THRESHOLD_PERCENT /
100;
let rollback_threshold = rollback_threshold.max(MIN_ROLLBACK_THRESHOLD);
if new_lost < rollback_threshold {
let did_rollback = rollback(r);
if did_rollback {
return;
}
}
}
if r.congestion_window < r.ssthresh {
r.bytes_acked_sl += packet.size;
if r.bytes_acked_sl >= r.max_datagram_size {
if r.hystart.in_css(epoch) {
r.congestion_window +=
r.hystart.css_cwnd_inc(r.max_datagram_size);
} else {
r.congestion_window += r.max_datagram_size;
}
r.bytes_acked_sl -= r.max_datagram_size;
}
if r.hystart.on_packet_acked(epoch, packet, r.latest_rtt, now) {
r.ssthresh = r.congestion_window;
}
} else {
let ca_start_time;
if r.hystart.in_css(epoch) {
ca_start_time = r.hystart.css_start_time().unwrap();
if r.cubic_state.w_max == 0.0 {
r.cubic_state.w_max = r.congestion_window as f64;
r.cubic_state.k = 0.0;
r.cubic_state.w_est = r.congestion_window as f64;
r.cubic_state.alpha_aimd = ALPHA_AIMD;
}
} else {
match r.congestion_recovery_start_time {
Some(t) => ca_start_time = t,
None => {
ca_start_time = now;
r.congestion_recovery_start_time = Some(now);
r.cubic_state.w_max = r.congestion_window as f64;
r.cubic_state.k = 0.0;
r.cubic_state.w_est = r.congestion_window as f64;
r.cubic_state.alpha_aimd = ALPHA_AIMD;
},
}
}
let t = now.saturating_duration_since(ca_start_time);
let target = r.cubic_state.w_cubic(t + r.min_rtt, r.max_datagram_size);
let target = f64::max(target, r.congestion_window as f64);
let target = f64::min(target, r.congestion_window as f64 * 1.5);
let w_est_inc = r.cubic_state.w_est_inc(
packet.size,
r.congestion_window,
r.max_datagram_size,
);
r.cubic_state.w_est += w_est_inc;
if r.cubic_state.w_est >= r.cubic_state.w_max {
r.cubic_state.alpha_aimd = 1.0;
}
let mut cubic_cwnd = r.congestion_window;
if r.cubic_state.w_cubic(t, r.max_datagram_size) < r.cubic_state.w_est {
cubic_cwnd = cmp::max(cubic_cwnd, r.cubic_state.w_est as usize);
} else {
let cubic_inc =
r.max_datagram_size * (target as usize - cubic_cwnd) / cubic_cwnd;
cubic_cwnd += cubic_inc;
}
r.cubic_state.cwnd_inc += cubic_cwnd - r.congestion_window;
if r.cubic_state.cwnd_inc >= r.max_datagram_size {
r.congestion_window += r.max_datagram_size;
r.cubic_state.cwnd_inc -= r.max_datagram_size;
}
}
}
fn congestion_event(
r: &mut Recovery, _lost_bytes: usize, largest_lost_pkt: &Sent,
epoch: packet::Epoch, now: Instant,
) {
let time_sent = largest_lost_pkt.time_sent;
let in_congestion_recovery = r.in_congestion_recovery(time_sent);
if !in_congestion_recovery {
r.congestion_recovery_start_time = Some(now);
if (r.congestion_window as f64) < r.cubic_state.w_max {
r.cubic_state.w_max =
r.congestion_window as f64 * (1.0 + BETA_CUBIC) / 2.0;
} else {
r.cubic_state.w_max = r.congestion_window as f64;
}
r.ssthresh = (r.congestion_window as f64 * BETA_CUBIC) as usize;
r.ssthresh = cmp::max(
r.ssthresh,
r.max_datagram_size * recovery::MINIMUM_WINDOW_PACKETS,
);
r.congestion_window = r.ssthresh;
r.cubic_state.k = if r.cubic_state.w_max < r.congestion_window as f64 {
0.0
} else {
r.cubic_state
.cubic_k(r.congestion_window, r.max_datagram_size)
};
r.cubic_state.cwnd_inc =
(r.cubic_state.cwnd_inc as f64 * BETA_CUBIC) as usize;
r.cubic_state.w_est = r.congestion_window as f64;
r.cubic_state.alpha_aimd = ALPHA_AIMD;
if r.hystart.in_css(epoch) {
r.hystart.congestion_event();
}
r.prr.congestion_event(r.bytes_in_flight);
}
}
fn checkpoint(r: &mut Recovery) {
r.cubic_state.prior.congestion_window = r.congestion_window;
r.cubic_state.prior.ssthresh = r.ssthresh;
r.cubic_state.prior.w_max = r.cubic_state.w_max;
r.cubic_state.prior.k = r.cubic_state.k;
r.cubic_state.prior.epoch_start = r.congestion_recovery_start_time;
r.cubic_state.prior.lost_count = r.lost_count;
}
fn rollback(r: &mut Recovery) -> bool {
if r.cubic_state.prior.congestion_window < r.cubic_state.prior.ssthresh {
return false;
}
if r.congestion_window >= r.cubic_state.prior.congestion_window {
return false;
}
r.congestion_window = r.cubic_state.prior.congestion_window;
r.ssthresh = r.cubic_state.prior.ssthresh;
r.cubic_state.w_max = r.cubic_state.prior.w_max;
r.cubic_state.k = r.cubic_state.prior.k;
r.congestion_recovery_start_time = r.cubic_state.prior.epoch_start;
true
}
fn has_custom_pacing() -> bool {
false
}
fn debug_fmt(r: &Recovery, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"cubic={{ k={} w_max={} }} ",
r.cubic_state.k, r.cubic_state.w_max
)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::recovery::hystart;
use smallvec::smallvec;
#[test]
fn cubic_init() {
let mut cfg = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
cfg.set_cc_algorithm(recovery::CongestionControlAlgorithm::CUBIC);
let r = Recovery::new(&cfg);
assert!(r.cwnd() > 0);
assert_eq!(r.bytes_in_flight, 0);
}
#[test]
fn cubic_send() {
let mut cfg = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
cfg.set_cc_algorithm(recovery::CongestionControlAlgorithm::CUBIC);
let mut r = Recovery::new(&cfg);
r.on_packet_sent_cc(1000, Instant::now());
assert_eq!(r.bytes_in_flight, 1000);
}
#[test]
fn cubic_slow_start() {
let mut cfg = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
cfg.set_cc_algorithm(recovery::CongestionControlAlgorithm::CUBIC);
let mut r = Recovery::new(&cfg);
let now = Instant::now();
let p = recovery::Sent {
pkt_num: 0,
frames: smallvec![],
time_sent: now,
time_acked: None,
time_lost: None,
size: r.max_datagram_size,
ack_eliciting: true,
in_flight: true,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
tx_in_flight: 0,
lost: 0,
has_data: false,
};
for _ in 0..r.initial_congestion_window_packets {
r.on_packet_sent_cc(p.size, now);
}
let cwnd_prev = r.cwnd();
let mut acked = vec![Acked {
pkt_num: p.pkt_num,
time_sent: p.time_sent,
size: p.size,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
tx_in_flight: 0,
lost: 0,
rtt: Duration::ZERO,
}];
r.on_packets_acked(&mut acked, packet::Epoch::Application, now);
assert_eq!(r.cwnd(), cwnd_prev + p.size);
}
#[test]
fn cubic_slow_start_multi_acks() {
let mut cfg = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
cfg.set_cc_algorithm(recovery::CongestionControlAlgorithm::CUBIC);
let mut r = Recovery::new(&cfg);
let now = Instant::now();
let p = recovery::Sent {
pkt_num: 0,
frames: smallvec![],
time_sent: now,
time_acked: None,
time_lost: None,
size: r.max_datagram_size,
ack_eliciting: true,
in_flight: true,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
tx_in_flight: 0,
lost: 0,
has_data: false,
};
for _ in 0..r.initial_congestion_window_packets {
r.on_packet_sent_cc(p.size, now);
}
let cwnd_prev = r.cwnd();
let mut acked = vec![
Acked {
pkt_num: p.pkt_num,
time_sent: p.time_sent,
size: p.size,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
tx_in_flight: 0,
lost: 0,
rtt: Duration::ZERO,
},
Acked {
pkt_num: p.pkt_num,
time_sent: p.time_sent,
size: p.size,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
tx_in_flight: 0,
lost: 0,
rtt: Duration::ZERO,
},
Acked {
pkt_num: p.pkt_num,
time_sent: p.time_sent,
size: p.size,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
tx_in_flight: 0,
lost: 0,
rtt: Duration::ZERO,
},
];
r.on_packets_acked(&mut acked, packet::Epoch::Application, now);
assert_eq!(r.cwnd(), cwnd_prev + p.size * 3);
}
#[test]
fn cubic_congestion_event() {
let mut cfg = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
cfg.set_cc_algorithm(recovery::CongestionControlAlgorithm::CUBIC);
let mut r = Recovery::new(&cfg);
let now = Instant::now();
let prev_cwnd = r.cwnd();
let p = recovery::Sent {
pkt_num: 0,
frames: smallvec![],
time_sent: now,
time_acked: None,
time_lost: None,
size: r.max_datagram_size,
ack_eliciting: true,
in_flight: true,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
has_data: false,
tx_in_flight: 0,
lost: 0,
};
r.congestion_event(
r.max_datagram_size,
&p,
packet::Epoch::Application,
now,
);
assert_eq!(prev_cwnd as f64 * BETA_CUBIC, r.cwnd() as f64);
}
#[test]
fn cubic_congestion_avoidance() {
let mut cfg = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
cfg.set_cc_algorithm(recovery::CongestionControlAlgorithm::CUBIC);
let mut r = Recovery::new(&cfg);
let mut now = Instant::now();
let prev_cwnd = r.cwnd();
for _ in 0..r.initial_congestion_window_packets {
r.on_packet_sent_cc(r.max_datagram_size, now);
}
let p = recovery::Sent {
pkt_num: 0,
frames: smallvec![],
time_sent: now,
time_acked: None,
time_lost: None,
size: r.max_datagram_size,
ack_eliciting: true,
in_flight: true,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
has_data: false,
tx_in_flight: 0,
lost: 0,
};
r.congestion_event(
r.max_datagram_size,
&p,
packet::Epoch::Application,
now,
);
let cur_cwnd = (prev_cwnd as f64 * BETA_CUBIC) as usize;
assert_eq!(r.cwnd(), cur_cwnd);
let rtt = Duration::from_millis(100);
r.update_rtt(rtt, Duration::from_millis(0), now);
now += rtt;
r.lost_count += MIN_ROLLBACK_THRESHOLD;
for _ in 0..5 {
let mut acked = vec![Acked {
pkt_num: 0,
time_sent: now,
size: r.max_datagram_size,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
tx_in_flight: 0,
lost: 0,
rtt: Duration::ZERO,
}];
r.on_packets_acked(&mut acked, packet::Epoch::Application, now);
now += rtt;
}
assert_eq!(r.cwnd(), cur_cwnd + r.max_datagram_size);
}
#[test]
fn cubic_collapse_cwnd_and_restart() {
let mut cfg = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
cfg.set_cc_algorithm(recovery::CongestionControlAlgorithm::CUBIC);
let mut r = Recovery::new(&cfg);
let now = Instant::now();
r.on_packet_sent_cc(30000, now);
let p = recovery::Sent {
pkt_num: 0,
frames: smallvec![],
time_sent: now,
time_acked: None,
time_lost: None,
size: r.max_datagram_size,
ack_eliciting: true,
in_flight: true,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
has_data: false,
tx_in_flight: 0,
lost: 0,
};
r.congestion_event(
r.max_datagram_size,
&p,
packet::Epoch::Application,
now,
);
r.collapse_cwnd();
assert_eq!(
r.cwnd(),
r.max_datagram_size * recovery::MINIMUM_WINDOW_PACKETS
);
let mut acked = vec![Acked {
pkt_num: 0,
time_sent: now + Duration::from_millis(1),
size: r.max_datagram_size,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
tx_in_flight: 0,
lost: 0,
rtt: Duration::ZERO,
}];
r.on_packets_acked(&mut acked, packet::Epoch::Application, now);
assert_eq!(
r.cwnd(),
r.max_datagram_size * (recovery::MINIMUM_WINDOW_PACKETS + 1)
);
}
#[test]
fn cubic_hystart_css_to_ss() {
let mut cfg = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
cfg.set_cc_algorithm(recovery::CongestionControlAlgorithm::CUBIC);
cfg.enable_hystart(true);
let mut r = Recovery::new(&cfg);
let now = Instant::now();
let epoch = packet::Epoch::Application;
let p = recovery::Sent {
pkt_num: 0,
frames: smallvec![],
time_sent: now,
time_acked: None,
time_lost: None,
size: r.max_datagram_size,
ack_eliciting: true,
in_flight: true,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
tx_in_flight: 0,
lost: 0,
has_data: false,
};
let n_rtt_sample = hystart::N_RTT_SAMPLE;
let mut send_pn = 0;
let mut ack_pn = 0;
let rtt_1st = Duration::from_millis(50);
for _ in 0..n_rtt_sample {
r.on_packet_sent_cc(p.size, now);
send_pn += 1;
}
r.hystart.start_round(send_pn - 1);
let now = now + rtt_1st;
for _ in 0..n_rtt_sample {
r.update_rtt(rtt_1st, Duration::from_millis(0), now);
let mut acked = vec![Acked {
pkt_num: ack_pn,
time_sent: p.time_sent,
size: p.size,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
tx_in_flight: 0,
lost: 0,
rtt: Duration::ZERO,
}];
r.on_packets_acked(&mut acked, epoch, now);
ack_pn += 1;
}
assert!(r.hystart.css_start_time().is_none());
let mut rtt_2nd = Duration::from_millis(100);
let now = now + rtt_2nd;
for _ in 0..n_rtt_sample {
r.on_packet_sent_cc(p.size, now);
send_pn += 1;
}
r.hystart.start_round(send_pn - 1);
let mut cwnd_prev = r.cwnd();
for _ in 0..n_rtt_sample {
cwnd_prev = r.cwnd();
r.update_rtt(rtt_2nd, Duration::from_millis(0), now);
let mut acked = vec![Acked {
pkt_num: ack_pn,
time_sent: p.time_sent,
size: p.size,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
tx_in_flight: 0,
lost: 0,
rtt: Duration::ZERO,
}];
r.on_packets_acked(&mut acked, epoch, now);
ack_pn += 1;
rtt_2nd += rtt_2nd.saturating_add(Duration::from_millis(4));
}
assert!(r.hystart.css_start_time().is_some());
assert_eq!(r.cwnd(), cwnd_prev + r.max_datagram_size);
let rtt_3rd = Duration::from_millis(80);
let now = now + rtt_3rd;
cwnd_prev = r.cwnd();
for _ in 0..n_rtt_sample {
r.on_packet_sent_cc(p.size, now);
send_pn += 1;
}
r.hystart.start_round(send_pn - 1);
for _ in 0..n_rtt_sample {
r.update_rtt(rtt_3rd, Duration::from_millis(0), now);
let mut acked = vec![Acked {
pkt_num: ack_pn,
time_sent: p.time_sent,
size: p.size,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
tx_in_flight: 0,
lost: 0,
rtt: Duration::ZERO,
}];
r.on_packets_acked(&mut acked, epoch, now);
ack_pn += 1;
}
assert!(r.hystart.css_start_time().is_none());
assert_eq!(
r.cwnd(),
cwnd_prev +
r.max_datagram_size / hystart::CSS_GROWTH_DIVISOR *
hystart::N_RTT_SAMPLE
);
}
#[test]
fn cubic_hystart_css_to_ca() {
let mut cfg = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
cfg.set_cc_algorithm(recovery::CongestionControlAlgorithm::CUBIC);
cfg.enable_hystart(true);
let mut r = Recovery::new(&cfg);
let now = Instant::now();
let epoch = packet::Epoch::Application;
let p = recovery::Sent {
pkt_num: 0,
frames: smallvec![],
time_sent: now,
time_acked: None,
time_lost: None,
size: r.max_datagram_size,
ack_eliciting: true,
in_flight: true,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
tx_in_flight: 0,
lost: 0,
has_data: false,
};
let n_rtt_sample = hystart::N_RTT_SAMPLE;
let mut send_pn = 0;
let mut ack_pn = 0;
let rtt_1st = Duration::from_millis(50);
for _ in 0..n_rtt_sample {
r.on_packet_sent_cc(p.size, now);
send_pn += 1;
}
r.hystart.start_round(send_pn - 1);
let now = now + rtt_1st;
for _ in 0..n_rtt_sample {
r.update_rtt(rtt_1st, Duration::from_millis(0), now);
let mut acked = vec![Acked {
pkt_num: ack_pn,
time_sent: p.time_sent,
size: p.size,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
tx_in_flight: 0,
lost: 0,
rtt: Duration::ZERO,
}];
r.on_packets_acked(&mut acked, epoch, now);
ack_pn += 1;
}
assert!(r.hystart.css_start_time().is_none());
let mut rtt_2nd = Duration::from_millis(100);
let now = now + rtt_2nd;
for _ in 0..n_rtt_sample {
r.on_packet_sent_cc(p.size, now);
send_pn += 1;
}
r.hystart.start_round(send_pn - 1);
let mut cwnd_prev = r.cwnd();
for _ in 0..n_rtt_sample {
cwnd_prev = r.cwnd();
r.update_rtt(rtt_2nd, Duration::from_millis(0), now);
let mut acked = vec![Acked {
pkt_num: ack_pn,
time_sent: p.time_sent,
size: p.size,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
tx_in_flight: 0,
lost: 0,
rtt: Duration::ZERO,
}];
r.on_packets_acked(&mut acked, epoch, now);
ack_pn += 1;
rtt_2nd += rtt_2nd.saturating_add(Duration::from_millis(4));
}
assert!(r.hystart.css_start_time().is_some());
assert_eq!(r.cwnd(), cwnd_prev + r.max_datagram_size);
let rtt_css = Duration::from_millis(100);
let now = now + rtt_css;
for _ in 0..hystart::CSS_ROUNDS {
for _ in 0..n_rtt_sample {
r.on_packet_sent_cc(p.size, now);
send_pn += 1;
}
r.hystart.start_round(send_pn - 1);
for _ in 0..n_rtt_sample {
r.update_rtt(rtt_css, Duration::from_millis(0), now);
let mut acked = vec![Acked {
pkt_num: ack_pn,
time_sent: p.time_sent,
size: p.size,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
tx_in_flight: 0,
lost: 0,
rtt: Duration::ZERO,
}];
r.on_packets_acked(&mut acked, epoch, now);
ack_pn += 1;
}
}
assert_eq!(r.cwnd(), r.ssthresh);
}
#[test]
fn cubic_spurious_congestion_event() {
let mut cfg = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
cfg.set_cc_algorithm(recovery::CongestionControlAlgorithm::CUBIC);
let mut r = Recovery::new(&cfg);
let now = Instant::now();
let prev_cwnd = r.cwnd();
for _ in 0..r.initial_congestion_window_packets {
r.on_packet_sent_cc(r.max_datagram_size, now);
}
let p = recovery::Sent {
pkt_num: 0,
frames: smallvec![],
time_sent: now,
time_acked: None,
time_lost: None,
size: r.max_datagram_size,
ack_eliciting: true,
in_flight: true,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
has_data: false,
tx_in_flight: 0,
lost: 0,
};
r.congestion_event(
r.max_datagram_size,
&p,
packet::Epoch::Application,
now,
);
let cur_cwnd = (prev_cwnd as f64 * BETA_CUBIC) as usize;
assert_eq!(r.cwnd(), cur_cwnd);
let rtt = Duration::from_millis(100);
let mut acked = vec![Acked {
pkt_num: 0,
time_sent: now + rtt,
size: r.max_datagram_size,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
tx_in_flight: 0,
lost: 0,
rtt: Duration::ZERO,
}];
r.update_rtt(rtt, Duration::from_millis(0), now);
r.on_packets_acked(
&mut acked,
packet::Epoch::Application,
now + rtt + Duration::from_millis(5),
);
assert_eq!(r.cwnd(), cur_cwnd);
let now = now + rtt;
let p = recovery::Sent {
pkt_num: 0,
frames: smallvec![],
time_sent: now,
time_acked: None,
time_lost: None,
size: r.max_datagram_size,
ack_eliciting: true,
in_flight: true,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
has_data: false,
tx_in_flight: 0,
lost: 0,
};
let prev_cwnd = r.cwnd();
r.congestion_event(
r.max_datagram_size,
&p,
packet::Epoch::Application,
now,
);
let cur_cwnd = (cur_cwnd as f64 * BETA_CUBIC) as usize;
assert_eq!(r.cwnd(), cur_cwnd);
let rtt = Duration::from_millis(100);
let mut acked = vec![Acked {
pkt_num: 0,
time_sent: now + rtt,
size: r.max_datagram_size,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
tx_in_flight: 0,
lost: 0,
rtt: Duration::ZERO,
}];
r.update_rtt(rtt, Duration::from_millis(0), now);
r.on_packets_acked(
&mut acked,
packet::Epoch::Application,
now + rtt + Duration::from_millis(5),
);
assert_eq!(r.cwnd(), prev_cwnd);
}
#[test]
fn cubic_fast_convergence() {
let mut cfg = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
cfg.set_cc_algorithm(recovery::CongestionControlAlgorithm::CUBIC);
let mut r = Recovery::new(&cfg);
let mut now = Instant::now();
let prev_cwnd = r.cwnd();
for _ in 0..r.initial_congestion_window_packets {
r.on_packet_sent_cc(r.max_datagram_size, now);
}
let p = recovery::Sent {
pkt_num: 0,
frames: smallvec![],
time_sent: now,
time_acked: None,
time_lost: None,
size: r.max_datagram_size,
ack_eliciting: true,
in_flight: true,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
has_data: false,
tx_in_flight: 0,
lost: 0,
};
r.congestion_event(
r.max_datagram_size,
&p,
packet::Epoch::Application,
now,
);
let cur_cwnd = (prev_cwnd as f64 * BETA_CUBIC) as usize;
assert_eq!(r.cwnd(), cur_cwnd);
let rtt = Duration::from_millis(100);
r.update_rtt(rtt, Duration::from_millis(0), now);
now += rtt;
r.lost_count += MIN_ROLLBACK_THRESHOLD;
for _ in 0..5 {
let mut acked = vec![Acked {
pkt_num: 0,
time_sent: now,
size: r.max_datagram_size,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
tx_in_flight: 0,
lost: 0,
rtt: Duration::ZERO,
}];
r.on_packets_acked(&mut acked, packet::Epoch::Application, now);
now += rtt;
}
assert_eq!(r.cwnd(), cur_cwnd + r.max_datagram_size);
let prev_cwnd = r.cwnd();
let p = recovery::Sent {
pkt_num: 0,
frames: smallvec![],
time_sent: now,
time_acked: None,
time_lost: None,
size: r.max_datagram_size,
ack_eliciting: true,
in_flight: true,
delivered: 0,
delivered_time: now,
first_sent_time: now,
is_app_limited: false,
has_data: false,
tx_in_flight: 0,
lost: 0,
};
r.congestion_event(
r.max_datagram_size,
&p,
packet::Epoch::Application,
now,
);
let cur_cwnd = (prev_cwnd as f64 * BETA_CUBIC) as usize;
assert_eq!(r.cwnd(), cur_cwnd);
assert_eq!(
r.cubic_state.w_max,
prev_cwnd as f64 * (1.0 + BETA_CUBIC) / 2.0
);
}
}