use rtcp::transport_feedbacks::cc_feedback_report::Ecn;
use rtcp::transport_feedbacks::ecn_feedback::EcnCounters;
use std::time::{Duration, Instant};
pub const PROBE_SPACING: u32 = 10;
pub const PROBE_INTERVAL: Duration = Duration::from_millis(500);
pub const CONFIRMATIONS: u32 = 2;
pub const PROBES_LOST_TO_FAIL: u32 = 4;
pub const WINDOW: u32 = 32;
pub const DEFAULT_RETRY_AFTER: Duration = Duration::from_secs(60);
pub const FALLBACKS_TO_DISABLE: u32 = 2;
pub const PROVISIONAL_MONITOR: Duration = Duration::from_secs(15);
const HISTORY: usize = 2048;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EcnState {
Initiating,
Active,
Fallback,
Failed {
retry_at: Instant,
},
Disabled,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EctPreference {
Ect0,
Ect1,
Random,
}
impl EctPreference {
pub fn from_sdp(value: &str) -> Option<Self> {
match value.trim().to_ascii_lowercase().as_str() {
"0" => Some(Self::Ect0),
"1" => Some(Self::Ect1),
"random" => Some(Self::Random),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum EcnMarking {
#[default]
Rfc6679,
L4s,
}
impl EcnMarking {
pub fn codepoint(self, preference: Option<EctPreference>, coin: bool) -> shared::EcnCodepoint {
match (self, preference) {
(Self::L4s, _) | (Self::Rfc6679, Some(EctPreference::Ect1)) => {
shared::EcnCodepoint::Ect1
}
(Self::Rfc6679, Some(EctPreference::Random)) if coin => shared::EcnCodepoint::Ect1,
_ => shared::EcnCodepoint::Ect0,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum EcnInitiation {
#[default]
RtpAndRtcp,
LeapOfFaith,
}
impl EcnInitiation {
pub fn one_per_transport<K: PartialEq + Copy>(
sections: &[(K, Option<EcnInitiation>)],
) -> Vec<Option<EcnInitiation>> {
sections
.iter()
.map(|(transport, method)| {
method.and_then(|_| {
sections
.iter()
.find(|(other, m)| other == transport && m.is_some())
.and_then(|(_, m)| *m)
})
})
.collect()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum EcnFailure {
ProbeArrivedNotEct,
NoProbeArrived,
FieldCleared,
ProbesCountedNotEct,
NoEcnReport,
NoReception,
EctLost {
disabled: bool,
},
}
impl EcnFailure {
pub fn is_initiation_failure(&self) -> bool {
!matches!(self, EcnFailure::EctLost { .. })
}
}
impl std::fmt::Display for EcnFailure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
EcnFailure::ProbeArrivedNotEct => "an ECT probe arrived Not-ECT",
EcnFailure::NoProbeArrived => "no ECT probe arrived",
EcnFailure::FieldCleared => "the path clears the ECN field",
EcnFailure::ProbesCountedNotEct => "ECT probes were counted Not-ECT",
EcnFailure::NoEcnReport => {
"a receiver reported more than three probes without an ECN report"
}
EcnFailure::NoReception => "two receiver reports in a row showed no reception",
EcnFailure::EctLost { disabled: true } => {
"ECT packets were lost again; ECN off for the stream"
}
EcnFailure::EctLost { disabled: false } => "ECT packets were lost; initiating again",
})
}
}
const MAX_KEPT_FAILURES: usize = 16;
#[derive(Debug, Clone, Copy, Default)]
struct Sent {
sequence_number: u16,
known: bool,
marked: bool,
reported: Option<bool>,
}
#[derive(Debug, Clone, Copy, Default)]
struct Window {
reported: u32,
lost: u32,
bleached: u32,
}
#[derive(Debug, Clone)]
pub struct EcnValidation {
state: EcnState,
retry_after: Duration,
since_probe: u32,
last_probe: Option<Instant>,
history: Box<[Sent]>,
probes_confirmed: u32,
probes_lost: u32,
window: Window,
fallback_from: u32,
fallbacks: u32,
last_counters: Option<(u32, EcnCounters)>,
initiated_at: Option<Instant>,
provisional_until: Option<Instant>,
receiver_cname: Option<String>,
failures: Vec<EcnFailure>,
leap_of_faith: bool,
reports_without_reception: u32,
}
impl Default for EcnValidation {
fn default() -> Self {
Self::new(EcnInitiation::default())
}
}
impl EcnValidation {
pub fn new(initiation: EcnInitiation) -> Self {
Self {
state: match initiation {
EcnInitiation::RtpAndRtcp => EcnState::Initiating,
EcnInitiation::LeapOfFaith => EcnState::Active,
},
retry_after: DEFAULT_RETRY_AFTER,
since_probe: PROBE_SPACING,
last_probe: None,
history: vec![Sent::default(); HISTORY].into_boxed_slice(),
probes_confirmed: 0,
probes_lost: 0,
window: Window::default(),
fallback_from: 0,
fallbacks: 0,
last_counters: None,
initiated_at: None,
provisional_until: None,
receiver_cname: None,
failures: Vec::new(),
leap_of_faith: initiation == EcnInitiation::LeapOfFaith,
reports_without_reception: 0,
}
}
pub fn with_retry_after(mut self, retry_after: Duration) -> Self {
self.retry_after = retry_after;
self
}
pub fn state(&self) -> EcnState {
self.state
}
pub fn marks_all(&self) -> bool {
self.state == EcnState::Active
}
pub fn was_marked(&self, sequence_number: u16) -> bool {
let slot = &self.history[usize::from(sequence_number) % HISTORY];
slot.known && slot.sequence_number == sequence_number && slot.marked
}
pub fn on_send(&mut self, now: Instant, sequence_number: u16) -> bool {
if let EcnState::Failed { retry_at } = self.state
&& now >= retry_at
{
self.initiate();
}
let marked = match self.state {
EcnState::Initiating => {
let due = self.since_probe + 1 >= PROBE_SPACING
|| (self.since_probe >= 1
&& self
.last_probe
.is_none_or(|at| now.saturating_duration_since(at) >= PROBE_INTERVAL));
if due {
self.since_probe = 0;
self.last_probe = Some(now);
self.initiated_at.get_or_insert(now);
} else {
self.since_probe += 1;
}
due
}
EcnState::Active => true,
EcnState::Fallback | EcnState::Failed { .. } | EcnState::Disabled => false,
};
self.history[usize::from(sequence_number) % HISTORY] = Sent {
sequence_number,
known: true,
marked,
reported: None,
};
marked
}
pub fn on_report(&mut self, now: Instant, sequence_number: u16, received: bool, ecn: Ecn) {
let slot = &mut self.history[usize::from(sequence_number) % HISTORY];
if !slot.known || slot.sequence_number != sequence_number {
return;
}
let undo_loss = match slot.reported {
Some(true) => return,
Some(false) if !received => return,
Some(false) => true,
None => false,
};
slot.reported = Some(received);
let marked = slot.marked;
let ecn_capable = matches!(ecn, Ecn::Ect0 | Ecn::Ect1 | Ecn::Ce);
match self.state {
EcnState::Initiating if marked => {
if undo_loss {
self.probes_lost = self.probes_lost.saturating_sub(1);
}
if !received {
self.probes_lost += 1;
} else if ecn_capable {
self.probes_confirmed += 1;
} else {
self.fail(now, EcnFailure::ProbeArrivedNotEct);
return;
}
if self.probes_confirmed == 0 && self.probes_lost >= PROBES_LOST_TO_FAIL {
self.fail(now, EcnFailure::NoProbeArrived);
} else if self.probes_confirmed >= CONFIRMATIONS {
self.provisional_success(now);
}
}
EcnState::Active if marked => {
self.count(undo_loss, received, !ecn_capable);
if self.window.bleached * 4 > WINDOW {
self.fail(now, EcnFailure::FieldCleared);
} else if self.window.lost * 2 >= WINDOW {
log::debug!("ECN: high loss of ECT packets; sending Not-ECT to compare");
self.state = EcnState::Fallback;
self.fallback_from = self.window.lost;
self.window = Window::default();
} else if self.window.reported >= WINDOW {
self.window = Window::default();
}
}
EcnState::Fallback if !marked => {
self.count(undo_loss, received, false);
if self.window.reported >= WINDOW {
if self.window.lost * 4 < WINDOW && self.window.lost * 2 < self.fallback_from {
self.fallbacks += 1;
if self.fallbacks >= FALLBACKS_TO_DISABLE {
self.record(EcnFailure::EctLost { disabled: true });
self.state = EcnState::Disabled;
} else {
self.record(EcnFailure::EctLost { disabled: false });
self.initiate();
}
} else {
self.state = EcnState::Active;
self.window = Window::default();
}
}
}
_ => {}
}
}
pub fn on_counters(&mut self, now: Instant, extended_highest: u32, counters: EcnCounters) {
let (previous_highest, previous) = match self.last_counters {
Some(last) => last,
None => (
extended_highest.wrapping_sub(self.known_since(extended_highest)),
EcnCounters::default(),
),
};
let span = extended_highest.wrapping_sub(previous_highest);
if span > 0x8000_0000 {
return;
}
self.last_counters = Some((extended_highest, counters));
if span == 0 && counters == previous {
return;
}
let ect = u64::from(counters.ect0.wrapping_sub(previous.ect0))
+ u64::from(counters.ect1.wrapping_sub(previous.ect1))
+ u64::from(counters.ce.wrapping_sub(previous.ce));
let not_ect = u64::from(counters.not_ect.wrapping_sub(previous.not_ect));
let duplicates = u64::from(counters.duplicates.wrapping_sub(previous.duplicates));
let lost = i64::from(counters.lost.wrapping_sub(previous.lost) as i16).max(0) as u64;
let (mut marked, mut unmarked) = (0u64, 0u64);
let first = previous_highest.wrapping_add(1);
for offset in 0..span.min(HISTORY as u32) {
let sequence_number = first.wrapping_add(offset) as u16;
let slot = &self.history[usize::from(sequence_number) % HISTORY];
if slot.known && slot.sequence_number == sequence_number {
if slot.marked {
marked += 1;
} else {
unmarked += 1;
}
}
}
let bleached = not_ect.saturating_sub(unmarked + duplicates);
match self.state {
EcnState::Initiating => {
if bleached > 0 && marked > 0 {
self.fail(now, EcnFailure::ProbesCountedNotEct);
return;
}
self.probes_confirmed += ect.min(u64::from(u32::MAX)) as u32;
if ect == 0 {
self.probes_lost += marked as u32;
}
if self.probes_confirmed == 0 && self.probes_lost >= PROBES_LOST_TO_FAIL {
self.fail(now, EcnFailure::NoProbeArrived);
} else if self.probes_confirmed >= CONFIRMATIONS && bleached == 0 {
self.provisional_success(now);
}
}
EcnState::Active => {
self.window.reported += marked as u32;
self.window.lost += lost.min(marked) as u32;
self.window.bleached += bleached.min(marked) as u32;
if self.window.bleached * 4 > WINDOW {
self.fail(now, EcnFailure::FieldCleared);
} else if self.window.lost * 2 >= WINDOW {
log::debug!("ECN: high loss of ECT packets; sending Not-ECT to compare");
self.state = EcnState::Fallback;
self.fallback_from = self.window.lost;
self.window = Window::default();
} else if self.window.reported >= WINDOW {
self.window = Window::default();
}
}
EcnState::Fallback => {
self.window.reported += unmarked as u32;
self.window.lost += lost.min(unmarked) as u32;
if self.window.reported >= WINDOW {
if self.window.lost * 4 < WINDOW && self.window.lost * 2 < self.fallback_from {
self.fallbacks += 1;
if self.fallbacks >= FALLBACKS_TO_DISABLE {
self.record(EcnFailure::EctLost { disabled: true });
self.state = EcnState::Disabled;
} else {
self.record(EcnFailure::EctLost { disabled: false });
self.initiate();
}
} else {
self.state = EcnState::Active;
self.window = Window::default();
}
}
}
EcnState::Failed { .. } | EcnState::Disabled => {}
}
}
fn known_since(&self, extended_highest: u32) -> u32 {
let mut span = 0u32;
while span < HISTORY as u32 {
let sequence_number = extended_highest.wrapping_sub(span) as u16;
let slot = &self.history[usize::from(sequence_number) % HISTORY];
if !(slot.known && slot.sequence_number == sequence_number) {
break;
}
span += 1;
}
span
}
fn count(&mut self, undo_loss: bool, received: bool, bleached: bool) {
if undo_loss {
self.window.lost = self.window.lost.saturating_sub(1);
} else {
self.window.reported += 1;
}
if !received {
self.window.lost += 1;
} else if bleached {
self.window.bleached += 1;
}
}
fn provisional_success(&mut self, now: Instant) {
self.state = EcnState::Active;
self.window = Window::default();
let started = self.initiated_at.unwrap_or(now);
self.provisional_until = Some(started + PROVISIONAL_MONITOR);
}
pub fn on_remote_cname(&mut self, now: Instant, cname: &str) {
match &self.receiver_cname {
None => self.receiver_cname = Some(cname.to_owned()),
Some(known) if known == cname => {}
Some(_) => {
let provisional = self.state == EcnState::Active
&& self.provisional_until.is_some_and(|until| now < until);
if provisional {
log::info!(
"ECN: another RTCP CNAME during a provisional success, probing again \
(RFC 6679 section 7.2.1)"
);
self.initiate();
}
self.receiver_cname = Some(cname.to_owned());
}
}
}
pub fn on_reception_report_without_ecn(&mut self, now: Instant, extended_highest: u32) {
if self.state != EcnState::Initiating {
return;
}
let highest = extended_highest as u16;
let due = self
.history
.iter()
.filter(|sent| {
sent.known && sent.marked && {
let behind = highest.wrapping_sub(sent.sequence_number);
usize::from(behind) < HISTORY
}
})
.count();
if due > 3 {
self.fail(now, EcnFailure::NoEcnReport);
}
}
pub fn on_report_without_reception(&mut self, now: Instant) {
if !self.leap_of_faith
|| self.state != EcnState::Active
|| !self.history.iter().any(|sent| sent.known && sent.marked)
{
return;
}
self.reports_without_reception += 1;
if self.reports_without_reception >= 2 {
self.leap_of_faith = false;
self.fail(now, EcnFailure::NoReception);
}
}
pub fn on_reception_reported(&mut self) {
self.reports_without_reception = 0;
self.leap_of_faith = false;
}
pub fn with_receiver_cname(mut self, cname: &str) -> Self {
self.receiver_cname = Some(cname.to_owned());
self
}
pub fn is_provisional(&self, now: Instant) -> bool {
self.state == EcnState::Active && self.provisional_until.is_some_and(|until| now < until)
}
fn initiate(&mut self) {
self.provisional_until = None;
self.initiated_at = None;
self.state = EcnState::Initiating;
self.since_probe = PROBE_SPACING;
self.last_probe = None;
self.probes_confirmed = 0;
self.probes_lost = 0;
self.window = Window::default();
}
fn record(&mut self, failure: EcnFailure) {
if failure.is_initiation_failure() {
log::warn!("ECN: {failure}; packets go out Not-ECT");
} else {
log::warn!("ECN: {failure}");
}
if self.failures.len() == MAX_KEPT_FAILURES {
self.failures.remove(0);
}
self.failures.push(failure);
}
pub fn take_failures(&mut self) -> Vec<EcnFailure> {
std::mem::take(&mut self.failures)
}
fn fail(&mut self, now: Instant, failure: EcnFailure) {
self.record(failure);
self.state = EcnState::Failed {
retry_at: now + self.retry_after,
};
self.window = Window::default();
}
}
#[cfg(test)]
mod tests {
use super::*;
fn counters_for(
sent: &[u16],
marked: &[u16],
ect_arrives: bool,
lost: u16,
base: EcnCounters,
) -> EcnCounters {
let marked_count = marked.len() as u32;
let unmarked = (sent.len() - marked.len()) as u16;
let mut c = base;
if ect_arrives {
c.ect1 += marked_count - u32::from(lost.min(marked_count as u16));
c.not_ect += unmarked;
} else {
c.not_ect += unmarked + marked_count as u16 - lost;
}
c.lost += lost;
c
}
#[test]
fn bundled_sections_use_one_initiation_method() {
use EcnInitiation::{LeapOfFaith, RtpAndRtcp};
let methods = EcnInitiation::one_per_transport(&[
(0, Some(RtpAndRtcp)),
(0, Some(LeapOfFaith)),
(0, None),
(1, Some(LeapOfFaith)),
]);
assert_eq!(
methods,
vec![Some(RtpAndRtcp), Some(RtpAndRtcp), None, Some(LeapOfFaith)]
);
}
#[test]
fn the_ect_parameter_names_ect0_ect1_or_random_and_nothing_else() {
assert_eq!(EctPreference::from_sdp("0"), Some(EctPreference::Ect0));
assert_eq!(EctPreference::from_sdp(" 1"), Some(EctPreference::Ect1));
assert_eq!(
EctPreference::from_sdp("Random"),
Some(EctPreference::Random)
);
assert_eq!(EctPreference::from_sdp("2"), None);
assert_eq!(
EcnMarking::Rfc6679.codepoint(None, true),
shared::EcnCodepoint::Ect0,
"RFC 6679 section 7.3.1: ECT(0) unless asked"
);
assert_eq!(
EcnMarking::L4s.codepoint(Some(EctPreference::Ect0), false),
shared::EcnCodepoint::Ect1
);
}
#[test]
fn counted_ect_probes_complete_the_initiation() {
let mut v = EcnValidation::default();
let mut at = Instant::now();
let sent: Vec<u16> = (0..40).collect();
let marked = send(&mut v, &mut at, 0, 40);
assert!(
marked.len() >= 2 && marked.len() < 40,
"probes only: {marked:?}"
);
let c = counters_for(&sent, &marked, true, 0, EcnCounters::default());
v.on_counters(at, 39, c);
assert_eq!(v.state(), EcnState::Active);
assert_eq!(
send(&mut v, &mut at, 40, 10).len(),
10,
"all marked once active"
);
}
#[test]
fn probes_counted_not_ect_fail_the_initiation() {
let mut v = EcnValidation::default().with_retry_after(Duration::from_secs(10));
let mut at = Instant::now();
let sent: Vec<u16> = (0..40).collect();
let marked = send(&mut v, &mut at, 0, 40);
let c = counters_for(&sent, &marked, false, 0, EcnCounters::default());
v.on_counters(at, 39, c);
assert!(
matches!(v.state(), EcnState::Failed { .. }),
"{:?}",
v.state()
);
assert!(
send(&mut v, &mut at, 40, 20).is_empty(),
"nothing marked after the failure"
);
at += Duration::from_secs(11);
assert!(
!send(&mut v, &mut at, 60, 20).is_empty(),
"probing again after the retry time"
);
}
#[test]
fn bleaching_counted_in_ongoing_use_stops_the_marking() {
let mut v = EcnValidation::new(EcnInitiation::LeapOfFaith);
let mut at = Instant::now();
let sent: Vec<u16> = (0..40).collect();
let marked = send(&mut v, &mut at, 0, 40);
assert_eq!(marked.len(), 40);
let c = counters_for(&sent, &marked, false, 0, EcnCounters::default());
v.on_counters(at, 39, c);
assert!(
matches!(v.state(), EcnState::Failed { .. }),
"{:?}",
v.state()
);
}
#[test]
fn counted_loss_of_ect_packets_falls_back_to_not_ect() {
let mut v = EcnValidation::new(EcnInitiation::LeapOfFaith);
let mut at = Instant::now();
let sent: Vec<u16> = (0..40).collect();
let marked = send(&mut v, &mut at, 0, 40);
let c = counters_for(&sent, &marked, true, 20, EcnCounters::default());
v.on_counters(at, 39, c);
assert_eq!(v.state(), EcnState::Fallback);
assert!(send(&mut v, &mut at, 40, 10).is_empty());
}
fn send(v: &mut EcnValidation, at: &mut Instant, first: u16, count: u16) -> Vec<u16> {
let mut marked = vec![];
for n in 0..count {
let seq = first.wrapping_add(n);
if v.on_send(*at, seq) {
marked.push(seq);
}
*at += Duration::from_millis(20);
}
marked
}
fn report(
v: &mut EcnValidation,
at: Instant,
first: u16,
count: u16,
path: impl Fn(u16, bool) -> Option<Ecn>,
) {
for n in 0..count {
let seq = first.wrapping_add(n);
let marked = v.was_marked(seq);
match path(seq, marked) {
Some(ecn) => v.on_report(at, seq, true, ecn),
None => v.on_report(at, seq, false, Ecn::NotEct),
}
}
}
fn carries_ecn(_: u16, marked: bool) -> Option<Ecn> {
Some(if marked { Ecn::Ect1 } else { Ecn::NotEct })
}
#[test]
fn a_second_cname_during_a_provisional_success_sends_the_stream_back_to_probing() {
let mut v = EcnValidation::default();
let mut at = Instant::now();
v.on_remote_cname(at, "peer");
send(&mut v, &mut at, 0, 40);
report(&mut v, at, 0, 40, carries_ecn);
assert!(v.is_provisional(at));
v.on_remote_cname(at, "peer");
assert_eq!(v.state(), EcnState::Active, "the same receiver");
v.on_remote_cname(at, "another participant");
assert_eq!(v.state(), EcnState::Initiating);
let probes = send(&mut v, &mut at, 40, 40).len();
assert!((1..40).contains(&probes), "a small fraction only: {probes}");
report(&mut v, at, 40, 40, carries_ecn);
assert_eq!(v.state(), EcnState::Active);
at += PROVISIONAL_MONITOR;
assert!(!v.is_provisional(at));
v.on_remote_cname(at, "a third");
assert_eq!(v.state(), EcnState::Active);
}
#[test]
fn initiation_marks_a_small_fraction_never_all() {
let mut v = EcnValidation::default();
let mut at = Instant::now();
let marked = send(&mut v, &mut at, 0, 100);
assert!(marked.len() >= 4, "probes: {marked:?}");
assert!(marked.len() <= 100 / 4, "a small fraction: {marked:?}");
assert!(
marked.windows(2).all(|w| w[1] - w[0] >= 2),
"never two in a row: {marked:?}"
);
assert_eq!(v.state(), EcnState::Initiating);
}
#[test]
fn a_slow_stream_still_probes_twice_a_second() {
let mut v = EcnValidation::default();
let mut at = Instant::now();
let mut marked = 0;
for seq in 0..10u16 {
marked += u32::from(v.on_send(at, seq));
at += Duration::from_millis(200);
}
assert!(marked >= 4, "{marked} probes in two seconds");
assert!(marked < 10);
}
#[test]
fn confirmed_probes_make_every_packet_ect() {
let mut v = EcnValidation::default();
let mut at = Instant::now();
send(&mut v, &mut at, 0, 40);
report(&mut v, at, 0, 40, carries_ecn);
assert_eq!(v.state(), EcnState::Active);
assert_eq!(send(&mut v, &mut at, 40, 20).len(), 20);
assert!(v.marks_all());
}
#[test]
fn a_ce_marked_probe_confirms_ecn_too() {
let mut v = EcnValidation::default();
let mut at = Instant::now();
send(&mut v, &mut at, 0, 40);
report(&mut v, at, 0, 40, |_, m| {
Some(if m { Ecn::Ce } else { Ecn::NotEct })
});
assert_eq!(v.state(), EcnState::Active);
}
#[test]
fn a_bleached_probe_fails_the_initiation_and_a_retry_follows() {
let mut v = EcnValidation::default().with_retry_after(Duration::from_secs(10));
let mut at = Instant::now();
send(&mut v, &mut at, 0, 40);
report(&mut v, at, 0, 40, |_, _| Some(Ecn::NotEct));
assert!(
matches!(v.state(), EcnState::Failed { .. }),
"{:?}",
v.state()
);
assert!(send(&mut v, &mut at, 40, 100).is_empty(), "nothing marked");
at += Duration::from_secs(10);
assert!(
!send(&mut v, &mut at, 140, 20).is_empty(),
"initiating again"
);
assert_eq!(v.state(), EcnState::Initiating);
}
#[test]
fn a_path_that_drops_ect_packets_fails_the_initiation() {
let mut v = EcnValidation::default();
let mut at = Instant::now();
let probes = send(&mut v, &mut at, 0, 60);
assert!(probes.len() >= 4);
report(&mut v, at, 0, 60, |_, m| (!m).then_some(Ecn::NotEct));
assert!(
matches!(v.state(), EcnState::Failed { .. }),
"{:?}",
v.state()
);
}
#[test]
fn three_lost_probes_are_not_yet_a_failure() {
let mut v = EcnValidation::default();
let mut at = Instant::now();
let probes = send(&mut v, &mut at, 0, 60);
let lost: Vec<u16> = probes[..3].to_vec();
report(&mut v, at, 0, 60, |s, m| {
if lost.contains(&s) {
None
} else {
carries_ecn(s, m)
}
});
assert_eq!(v.state(), EcnState::Active, "the fourth probe arrived ECT");
}
#[test]
fn a_late_packet_reported_lost_then_received_is_not_a_loss() {
let mut v = EcnValidation::default();
let mut at = Instant::now();
let probes = send(&mut v, &mut at, 0, 60);
for &p in &probes {
v.on_report(at, p, false, Ecn::NotEct);
v.on_report(at, p, true, Ecn::Ect1);
v.on_report(at, p, true, Ecn::Ect1);
v.on_report(at, p, false, Ecn::NotEct);
}
assert_eq!(v.state(), EcnState::Active);
}
#[test]
fn bleaching_during_use_stops_the_marking() {
let mut v = EcnValidation::new(EcnInitiation::LeapOfFaith);
let mut at = Instant::now();
assert_eq!(
send(&mut v, &mut at, 0, 32).len(),
32,
"leap of faith marks all"
);
report(&mut v, at, 0, 32, |s, _| {
Some(if s % 16 == 0 { Ecn::NotEct } else { Ecn::Ect1 })
});
assert_eq!(v.state(), EcnState::Active);
send(&mut v, &mut at, 32, 32);
report(&mut v, at, 32, 32, |_, _| Some(Ecn::NotEct));
assert!(
matches!(v.state(), EcnState::Failed { .. }),
"{:?}",
v.state()
);
assert!(send(&mut v, &mut at, 64, 50).is_empty());
}
#[test]
fn leap_of_faith_waits_for_a_second_report_without_reception() {
let mut v = EcnValidation::new(EcnInitiation::LeapOfFaith);
let mut at = Instant::now();
v.on_report_without_reception(at);
v.on_report_without_reception(at);
assert_eq!(v.state(), EcnState::Active);
assert_eq!(send(&mut v, &mut at, 0, 10).len(), 10);
v.on_report_without_reception(at);
assert_eq!(v.state(), EcnState::Active, "one report is not enough");
v.on_report_without_reception(at);
assert!(
matches!(v.state(), EcnState::Failed { .. }),
"{:?}",
v.state()
);
assert_eq!(v.take_failures(), vec![EcnFailure::NoReception]);
assert!(
send(&mut v, &mut at, 10, 10).is_empty(),
"Not-ECT after the failure"
);
let mut v = EcnValidation::new(EcnInitiation::LeapOfFaith);
send(&mut v, &mut at, 0, 10);
v.on_report_without_reception(at);
v.on_reception_reported();
v.on_report_without_reception(at);
v.on_report_without_reception(at);
assert_eq!(v.state(), EcnState::Active, "reception was reported");
let mut v = EcnValidation::new(EcnInitiation::RtpAndRtcp);
send(&mut v, &mut at, 0, 40);
v.on_report_without_reception(at);
v.on_report_without_reception(at);
assert!(v.take_failures().is_empty());
}
#[test]
fn loss_that_goes_with_the_marking_initiates_again_then_disables() {
let mut v = EcnValidation::new(EcnInitiation::LeapOfFaith);
let mut at = Instant::now();
let mut seq = 0u16;
for round in 0..FALLBACKS_TO_DISABLE {
if round > 0 {
let n = 40;
send(&mut v, &mut at, seq, n);
report(&mut v, at, seq, n, carries_ecn);
seq += n;
}
assert_eq!(v.state(), EcnState::Active, "round {round}");
send(&mut v, &mut at, seq, 32);
report(&mut v, at, seq, 32, |_, m| (!m).then_some(Ecn::NotEct));
seq += 32;
assert_eq!(v.state(), EcnState::Fallback, "round {round}");
assert!(
send(&mut v, &mut at, seq, 32).is_empty(),
"Not-ECT while comparing"
);
report(&mut v, at, seq, 32, |_, m| (!m).then_some(Ecn::NotEct));
seq += 32;
}
assert_eq!(v.state(), EcnState::Disabled);
at += DEFAULT_RETRY_AFTER * 2;
assert!(
send(&mut v, &mut at, seq, 40).is_empty(),
"disabled for good"
);
let mut expected =
vec![EcnFailure::EctLost { disabled: false }; FALLBACKS_TO_DISABLE as usize - 1];
expected.push(EcnFailure::EctLost { disabled: true });
assert_eq!(v.take_failures(), expected);
assert!(v.take_failures().is_empty(), "taken once");
}
#[test]
fn loss_that_stays_without_the_marking_resumes_it() {
let mut v = EcnValidation::new(EcnInitiation::LeapOfFaith);
let mut at = Instant::now();
send(&mut v, &mut at, 0, 32);
report(&mut v, at, 0, 32, |s, _| (s % 2 == 0).then_some(Ecn::Ect1));
assert_eq!(v.state(), EcnState::Fallback);
send(&mut v, &mut at, 32, 32);
report(&mut v, at, 32, 32, |s, _| {
(s % 2 == 0).then_some(Ecn::NotEct)
});
assert_eq!(v.state(), EcnState::Active, "congestion, not ECN");
}
#[test]
fn arbitrary_send_and_report_sequences_keep_the_invariants() {
let mut state = 0x0EC0_0001u32;
let mut next = move || {
state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
state >> 8
};
for _ in 0..400 {
let mut v = if next() % 2 == 0 {
EcnValidation::default()
} else {
EcnValidation::new(EcnInitiation::LeapOfFaith)
}
.with_retry_after(Duration::from_millis(u64::from(next() % 3000)));
let mut at = Instant::now();
let mut seq = next() as u16;
let mut sent: Vec<u16> = vec![];
let mut run_marked = 0u32;
let mut initiating = (0u32, 0u32);
for _ in 0..600 {
at += Duration::from_millis(u64::from(next() % 60));
match next() % 10 {
0..=5 => {
seq = match next() % 20 {
0 => seq.wrapping_add(next() as u16),
1 => seq,
_ => seq.wrapping_add(1),
};
let was = v.state();
let marked = v.on_send(at, seq);
sent.push(seq);
if was == EcnState::Disabled {
assert!(!marked, "a disabled stream marks nothing");
}
if v.state() == EcnState::Initiating && was == EcnState::Initiating {
initiating.0 += 1;
initiating.1 += u32::from(marked);
run_marked = if marked { run_marked + 1 } else { 0 };
assert!(run_marked <= 1, "two probes in a row");
} else {
run_marked = 0;
}
}
_ => {
let target = if sent.is_empty() || next() % 8 == 0 {
next() as u16
} else {
sent[(next() as usize) % sent.len()]
};
let ecn = match next() % 4 {
0 => Ecn::NotEct,
1 => Ecn::Ect1,
2 => Ecn::Ect0,
_ => Ecn::Ce,
};
v.on_report(at, target, next() % 5 != 0, ecn);
}
}
}
if initiating.0 >= 40 {
assert!(
initiating.1 * 4 <= initiating.0 + 4,
"a small fraction while initiating: {initiating:?}"
);
}
}
}
}