quiche/recovery/congestion/bbr/
per_ack.rsuse super::*;
use crate::rand;
use crate::recovery;
const PACING_RATE_1_2MBPS: u64 = 1200 * 1000 / 8;
const PACING_RATE_24MBPS: u64 = 24 * 1000 * 1000 / 8;
#[inline]
fn bbr_min_pipe_cwnd(r: &mut Congestion) -> usize {
BBR_MIN_PIPE_CWND_PKTS * r.max_datagram_size
}
pub fn bbr_update_model_and_state(
r: &mut Congestion, packet: &Acked, bytes_in_flight: usize, now: Instant,
) {
bbr_update_btlbw(r, packet, bytes_in_flight);
bbr_check_cycle_phase(r, now);
bbr_check_full_pipe(r);
bbr_check_drain(r, bytes_in_flight, now);
bbr_update_rtprop(r, now);
bbr_check_probe_rtt(r, bytes_in_flight, now);
}
pub fn bbr_update_control_parameters(
r: &mut Congestion, bytes_in_flight: usize, now: Instant,
) {
pacing::bbr_set_pacing_rate(r);
bbr_set_send_quantum(r);
r.set_pacing_rate(r.bbr_state.pacing_rate, now);
bbr_set_cwnd(r, bytes_in_flight);
}
fn bbr_update_btlbw(r: &mut Congestion, packet: &Acked, _bytes_in_flight: usize) {
bbr_update_round(r, packet);
if r.delivery_rate() >= r.bbr_state.btlbw ||
!r.delivery_rate.sample_is_app_limited()
{
r.bbr_state.btlbw = r.bbr_state.btlbwfilter.running_max(
BTLBW_FILTER_LEN,
r.bbr_state.start_time + Duration::from_secs(r.bbr_state.round_count),
r.delivery_rate(),
);
}
}
fn bbr_update_round(r: &mut Congestion, packet: &Acked) {
let bbr = &mut r.bbr_state;
if packet.delivered >= bbr.next_round_delivered {
bbr.next_round_delivered = r.delivery_rate.delivered();
bbr.round_count += 1;
bbr.round_start = true;
bbr.packet_conservation = false;
} else {
bbr.round_start = false;
}
}
fn bbr_update_rtprop(r: &mut Congestion, now: Instant) {
let bbr = &mut r.bbr_state;
let rs_rtt = r.delivery_rate.sample_rtt();
bbr.rtprop_expired = now > bbr.rtprop_stamp + RTPROP_FILTER_LEN;
if !rs_rtt.is_zero() && (rs_rtt <= bbr.rtprop || bbr.rtprop_expired) {
bbr.rtprop = rs_rtt;
bbr.rtprop_stamp = now;
}
}
fn bbr_set_send_quantum(r: &mut Congestion) {
let rate = r.bbr_state.pacing_rate;
r.send_quantum = match rate {
rate if rate < PACING_RATE_1_2MBPS => r.max_datagram_size,
rate if rate < PACING_RATE_24MBPS => 2 * r.max_datagram_size,
_ => cmp::min((rate / 1000_u64) as usize, 64 * 1024),
}
}
fn bbr_inflight(r: &mut Congestion, gain: f64) -> usize {
let bbr = &mut r.bbr_state;
if bbr.rtprop == Duration::MAX {
return r.max_datagram_size * r.initial_congestion_window_packets;
}
let quanta = 3 * r.send_quantum;
let estimated_bdp = bbr.btlbw as f64 * bbr.rtprop.as_secs_f64();
(gain * estimated_bdp) as usize + quanta
}
fn bbr_update_target_cwnd(r: &mut Congestion) {
r.bbr_state.target_cwnd = bbr_inflight(r, r.bbr_state.cwnd_gain);
}
pub fn bbr_save_cwnd(r: &mut Congestion) -> usize {
if !r.bbr_state.in_recovery && r.bbr_state.state != BBRStateMachine::ProbeRTT
{
r.congestion_window
} else {
r.congestion_window.max(r.bbr_state.prior_cwnd)
}
}
pub fn bbr_restore_cwnd(r: &mut Congestion) {
r.congestion_window = r.congestion_window.max(r.bbr_state.prior_cwnd);
}
fn bbr_modulate_cwnd_for_recovery(r: &mut Congestion, bytes_in_flight: usize) {
let acked_bytes = r.bbr_state.newly_acked_bytes;
let lost_bytes = r.bbr_state.newly_lost_bytes;
if lost_bytes > 0 {
r.congestion_window = r
.congestion_window
.saturating_sub(lost_bytes)
.max(r.max_datagram_size * recovery::MINIMUM_WINDOW_PACKETS);
}
if r.bbr_state.packet_conservation {
r.congestion_window =
r.congestion_window.max(bytes_in_flight + acked_bytes);
}
}
fn bbr_modulate_cwnd_for_probe_rtt(r: &mut Congestion) {
if r.bbr_state.state == BBRStateMachine::ProbeRTT {
r.congestion_window = r.congestion_window.min(bbr_min_pipe_cwnd(r))
}
}
fn bbr_set_cwnd(r: &mut Congestion, bytes_in_flight: usize) {
let acked_bytes = r.bbr_state.newly_acked_bytes;
bbr_update_target_cwnd(r);
bbr_modulate_cwnd_for_recovery(r, bytes_in_flight);
if !r.bbr_state.packet_conservation {
if r.bbr_state.filled_pipe {
r.congestion_window = cmp::min(
r.congestion_window + acked_bytes,
r.bbr_state.target_cwnd,
)
} else if r.congestion_window < r.bbr_state.target_cwnd ||
r.delivery_rate.delivered() <
r.max_datagram_size * r.initial_congestion_window_packets
{
r.congestion_window += acked_bytes;
}
r.congestion_window = r.congestion_window.max(bbr_min_pipe_cwnd(r))
}
bbr_modulate_cwnd_for_probe_rtt(r);
}
fn bbr_check_full_pipe(r: &mut Congestion) {
if r.bbr_state.filled_pipe ||
!r.bbr_state.round_start ||
r.delivery_rate.sample_is_app_limited()
{
return;
}
if r.bbr_state.btlbw >=
(r.bbr_state.full_bw as f64 * BTLBW_GROWTH_TARGET) as u64
{
r.bbr_state.full_bw = r.bbr_state.btlbw;
r.bbr_state.full_bw_count = 0;
return;
}
r.bbr_state.full_bw_count += 1;
if r.bbr_state.full_bw_count >= 3 {
r.bbr_state.filled_pipe = true;
}
}
fn bbr_enter_drain(r: &mut Congestion) {
let bbr = &mut r.bbr_state;
bbr.state = BBRStateMachine::Drain;
bbr.pacing_gain = 1.0 / BBR_HIGH_GAIN;
bbr.cwnd_gain = BBR_HIGH_GAIN;
}
fn bbr_check_drain(r: &mut Congestion, bytes_in_flight: usize, now: Instant) {
if r.bbr_state.state == BBRStateMachine::Startup && r.bbr_state.filled_pipe {
bbr_enter_drain(r);
}
if r.bbr_state.state == BBRStateMachine::Drain &&
bytes_in_flight <= bbr_inflight(r, 1.0)
{
bbr_enter_probe_bw(r, now);
}
}
fn bbr_enter_probe_bw(r: &mut Congestion, now: Instant) {
let bbr = &mut r.bbr_state;
bbr.state = BBRStateMachine::ProbeBW;
bbr.pacing_gain = 1.0;
bbr.cwnd_gain = 2.0;
bbr.cycle_index = BBR_GAIN_CYCLE_LEN -
1 -
(rand::rand_u64_uniform(BBR_GAIN_CYCLE_LEN as u64 - 1) as usize);
bbr_advance_cycle_phase(r, now);
}
fn bbr_check_cycle_phase(r: &mut Congestion, now: Instant) {
let bbr = &mut r.bbr_state;
if bbr.state == BBRStateMachine::ProbeBW && bbr_is_next_cycle_phase(r, now) {
bbr_advance_cycle_phase(r, now);
}
}
fn bbr_advance_cycle_phase(r: &mut Congestion, now: Instant) {
let bbr = &mut r.bbr_state;
bbr.cycle_stamp = now;
bbr.cycle_index = (bbr.cycle_index + 1) % BBR_GAIN_CYCLE_LEN;
bbr.pacing_gain = PACING_GAIN_CYCLE[bbr.cycle_index];
}
fn bbr_is_next_cycle_phase(r: &mut Congestion, now: Instant) -> bool {
let bbr = &mut r.bbr_state;
let lost_bytes = bbr.newly_lost_bytes;
let pacing_gain = bbr.pacing_gain;
let prior_in_flight = bbr.prior_bytes_in_flight;
let is_full_length = (now - bbr.cycle_stamp) > bbr.rtprop;
if (pacing_gain - 1.0).abs() < f64::EPSILON {
return is_full_length;
}
if pacing_gain > 1.0 {
return is_full_length &&
(lost_bytes > 0 ||
prior_in_flight >= bbr_inflight(r, pacing_gain));
}
is_full_length || prior_in_flight <= bbr_inflight(r, 1.0)
}
fn bbr_check_probe_rtt(r: &mut Congestion, bytes_in_flight: usize, now: Instant) {
if r.bbr_state.state != BBRStateMachine::ProbeRTT &&
r.bbr_state.rtprop_expired &&
!r.bbr_state.idle_restart
{
bbr_enter_probe_rtt(r);
r.bbr_state.prior_cwnd = bbr_save_cwnd(r);
r.bbr_state.probe_rtt_done_stamp = None;
}
if r.bbr_state.state == BBRStateMachine::ProbeRTT {
bbr_handle_probe_rtt(r, bytes_in_flight, now);
}
r.bbr_state.idle_restart = false;
}
fn bbr_enter_probe_rtt(r: &mut Congestion) {
let bbr = &mut r.bbr_state;
bbr.state = BBRStateMachine::ProbeRTT;
bbr.pacing_gain = 1.0;
bbr.cwnd_gain = 1.0;
}
fn bbr_handle_probe_rtt(
r: &mut Congestion, bytes_in_flight: usize, now: Instant,
) {
r.delivery_rate.update_app_limited(true);
if let Some(probe_rtt_done_stamp) = r.bbr_state.probe_rtt_done_stamp {
if r.bbr_state.round_start {
r.bbr_state.probe_rtt_round_done = true;
}
if r.bbr_state.probe_rtt_round_done && now > probe_rtt_done_stamp {
r.bbr_state.rtprop_stamp = now;
bbr_restore_cwnd(r);
bbr_exit_probe_rtt(r, now);
}
} else if bytes_in_flight <= bbr_min_pipe_cwnd(r) {
r.bbr_state.probe_rtt_done_stamp = Some(now + PROBE_RTT_DURATION);
r.bbr_state.probe_rtt_round_done = false;
r.bbr_state.next_round_delivered = r.delivery_rate.delivered();
}
}
fn bbr_exit_probe_rtt(r: &mut Congestion, now: Instant) {
if r.bbr_state.filled_pipe {
bbr_enter_probe_bw(r, now);
} else {
init::bbr_enter_startup(r);
}
}