| 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::time::Duration; |
| 10 |
|
| 11 |
use test_fixture::now; |
| 12 |
|
| 13 |
use super::{IP_ADDR, MTU, RTT}; |
| 14 |
use crate::{ |
| 15 |
cc::{new_reno::NewReno, ClassicCongestionControl, CongestionControl as _}, |
| 16 |
packet::PacketType, |
| 17 |
pmtud::Pmtud, |
| 18 |
recovery::SentPacket, |
| 19 |
rtt::RttEstimate, |
| 20 |
}; |
| 21 |
|
| 22 |
const PTO: Duration = RTT; |
| 23 |
|
| 24 |
fn cwnd_is_default(cc: &ClassicCongestionControl<NewReno>) { |
| 25 |
assert_eq!(cc.cwnd(), cc.cwnd_initial()); |
| 26 |
assert_eq!(cc.ssthresh(), usize::MAX); |
| 27 |
} |
| 28 |
|
| 29 |
fn cwnd_is_halved(cc: &ClassicCongestionControl<NewReno>) { |
| 30 |
assert_eq!(cc.cwnd(), cc.cwnd_initial() / 2); |
| 31 |
assert_eq!(cc.ssthresh(), cc.cwnd_initial() / 2); |
| 32 |
} |
| 33 |
|
| 34 |
#[test] |
| 35 |
fn issue_876() { |
| 36 |
let mut cc = ClassicCongestionControl::new(NewReno::default(), Pmtud::new(IP_ADDR, MTU)); |
| 37 |
let now = now(); |
| 38 |
let before = now.checked_sub(Duration::from_millis(100)).unwrap(); |
| 39 |
let after = now + Duration::from_millis(150); |
| 40 |
|
| 41 |
let sent_packets = &[ |
| 42 |
SentPacket::new( |
| 43 |
PacketType::Short, |
| 44 |
1, |
| 45 |
before, |
| 46 |
true, |
| 47 |
Vec::new(), |
| 48 |
cc.max_datagram_size() - 1, |
| 49 |
), |
| 50 |
SentPacket::new( |
| 51 |
PacketType::Short, |
| 52 |
2, |
| 53 |
before, |
| 54 |
true, |
| 55 |
Vec::new(), |
| 56 |
cc.max_datagram_size() - 2, |
| 57 |
), |
| 58 |
SentPacket::new( |
| 59 |
PacketType::Short, |
| 60 |
3, |
| 61 |
before, |
| 62 |
true, |
| 63 |
Vec::new(), |
| 64 |
cc.max_datagram_size(), |
| 65 |
), |
| 66 |
SentPacket::new( |
| 67 |
PacketType::Short, |
| 68 |
4, |
| 69 |
before, |
| 70 |
true, |
| 71 |
Vec::new(), |
| 72 |
cc.max_datagram_size(), |
| 73 |
), |
| 74 |
SentPacket::new( |
| 75 |
PacketType::Short, |
| 76 |
5, |
| 77 |
before, |
| 78 |
true, |
| 79 |
Vec::new(), |
| 80 |
cc.max_datagram_size(), |
| 81 |
), |
| 82 |
SentPacket::new( |
| 83 |
PacketType::Short, |
| 84 |
6, |
| 85 |
before, |
| 86 |
true, |
| 87 |
Vec::new(), |
| 88 |
cc.max_datagram_size(), |
| 89 |
), |
| 90 |
SentPacket::new( |
| 91 |
PacketType::Short, |
| 92 |
7, |
| 93 |
after, |
| 94 |
true, |
| 95 |
Vec::new(), |
| 96 |
cc.max_datagram_size() - 3, |
| 97 |
), |
| 98 |
]; |
| 99 |
|
| 100 |
// Send some more packets so that the cc is not app-limited. |
| 101 |
for p in &sent_packets[..6] { |
| 102 |
cc.on_packet_sent(p, now); |
| 103 |
} |
| 104 |
assert_eq!(cc.acked_bytes(), 0); |
| 105 |
cwnd_is_default(&cc); |
| 106 |
assert_eq!(cc.bytes_in_flight(), 6 * cc.max_datagram_size() - 3); |
| 107 |
|
| 108 |
cc.on_packets_lost(Some(now), None, PTO, &sent_packets[0..1], now); |
| 109 |
|
| 110 |
// We are now in recovery |
| 111 |
assert!(cc.recovery_packet()); |
| 112 |
assert_eq!(cc.acked_bytes(), 0); |
| 113 |
cwnd_is_halved(&cc); |
| 114 |
assert_eq!(cc.bytes_in_flight(), 5 * cc.max_datagram_size() - 2); |
| 115 |
|
| 116 |
// Send a packet after recovery starts |
| 117 |
cc.on_packet_sent(&sent_packets[6], now); |
| 118 |
assert!(!cc.recovery_packet()); |
| 119 |
cwnd_is_halved(&cc); |
| 120 |
assert_eq!(cc.acked_bytes(), 0); |
| 121 |
assert_eq!(cc.bytes_in_flight(), 6 * cc.max_datagram_size() - 5); |
| 122 |
|
| 123 |
// and ack it. cwnd increases slightly |
| 124 |
cc.on_packets_acked(&sent_packets[6..], &RttEstimate::default(), now); |
| 125 |
assert_eq!(cc.acked_bytes(), sent_packets[6].len()); |
| 126 |
cwnd_is_halved(&cc); |
| 127 |
assert_eq!(cc.bytes_in_flight(), 5 * cc.max_datagram_size() - 2); |
| 128 |
|
| 129 |
// Packet from before is lost. Should not hurt cwnd. |
| 130 |
cc.on_packets_lost(Some(now), None, PTO, &sent_packets[1..2], now); |
| 131 |
assert!(!cc.recovery_packet()); |
| 132 |
assert_eq!(cc.acked_bytes(), sent_packets[6].len()); |
| 133 |
cwnd_is_halved(&cc); |
| 134 |
assert_eq!(cc.bytes_in_flight(), 4 * cc.max_datagram_size()); |
| 135 |
} |
| 136 |
|
| 137 |
#[test] |
| 138 |
// https://github.com/mozilla/neqo/pull/1465 |
| 139 |
fn issue_1465() { |
| 140 |
let mut cc = ClassicCongestionControl::new(NewReno::default(), Pmtud::new(IP_ADDR, MTU)); |
| 141 |
let mut pn = 0; |
| 142 |
let mut now = now(); |
| 143 |
let max_datagram_size = cc.max_datagram_size(); |
| 144 |
let mut next_packet = |now| { |
| 145 |
let p = SentPacket::new( |
| 146 |
PacketType::Short, |
| 147 |
pn, |
| 148 |
now, |
| 149 |
true, |
| 150 |
Vec::new(), |
| 151 |
max_datagram_size, |
| 152 |
); |
| 153 |
pn += 1; |
| 154 |
p |
| 155 |
}; |
| 156 |
let mut send_next = |cc: &mut ClassicCongestionControl<NewReno>, now| { |
| 157 |
let p = next_packet(now); |
| 158 |
cc.on_packet_sent(&p, now); |
| 159 |
p |
| 160 |
}; |
| 161 |
|
| 162 |
let p1 = send_next(&mut cc, now); |
| 163 |
let p2 = send_next(&mut cc, now); |
| 164 |
let p3 = send_next(&mut cc, now); |
| 165 |
|
| 166 |
assert_eq!(cc.acked_bytes(), 0); |
| 167 |
cwnd_is_default(&cc); |
| 168 |
assert_eq!(cc.bytes_in_flight(), 3 * cc.max_datagram_size()); |
| 169 |
|
| 170 |
// advance one rtt to detect lost packet there this simplifies the timers, because |
| 171 |
// on_packet_loss would only be called after RTO, but that is not relevant to the problem |
| 172 |
now += RTT; |
| 173 |
cc.on_packets_lost(Some(now), None, PTO, &[p1], now); |
| 174 |
|
| 175 |
// We are now in recovery |
| 176 |
assert!(cc.recovery_packet()); |
| 177 |
assert_eq!(cc.acked_bytes(), 0); |
| 178 |
cwnd_is_halved(&cc); |
| 179 |
assert_eq!(cc.bytes_in_flight(), 2 * cc.max_datagram_size()); |
| 180 |
|
| 181 |
// Don't reduce the cwnd again on second packet loss |
| 182 |
cc.on_packets_lost(Some(now), None, PTO, &[p3], now); |
| 183 |
assert_eq!(cc.acked_bytes(), 0); |
| 184 |
cwnd_is_halved(&cc); // still the same as after first packet loss |
| 185 |
assert_eq!(cc.bytes_in_flight(), cc.max_datagram_size()); |
| 186 |
|
| 187 |
// the acked packets before on_packet_sent were the cause of |
| 188 |
// https://github.com/mozilla/neqo/pull/1465 |
| 189 |
cc.on_packets_acked(&[p2], &RttEstimate::default(), now); |
| 190 |
|
| 191 |
assert_eq!(cc.bytes_in_flight(), 0); |
| 192 |
|
| 193 |
// send out recovery packet and get it acked to get out of recovery state |
| 194 |
let p4 = send_next(&mut cc, now); |
| 195 |
cc.on_packet_sent(&p4, now); |
| 196 |
now += RTT; |
| 197 |
cc.on_packets_acked(&[p4], &RttEstimate::default(), now); |
| 198 |
|
| 199 |
// do the same as in the first rtt but now the bug appears |
| 200 |
let p5 = send_next(&mut cc, now); |
| 201 |
let p6 = send_next(&mut cc, now); |
| 202 |
now += RTT; |
| 203 |
|
| 204 |
let cur_cwnd = cc.cwnd(); |
| 205 |
cc.on_packets_lost(Some(now), None, PTO, &[p5], now); |
| 206 |
|
| 207 |
// go back into recovery |
| 208 |
assert!(cc.recovery_packet()); |
| 209 |
assert_eq!(cc.cwnd(), cur_cwnd / 2); |
| 210 |
assert_eq!(cc.acked_bytes(), 0); |
| 211 |
assert_eq!(cc.bytes_in_flight(), 2 * cc.max_datagram_size()); |
| 212 |
|
| 213 |
// this shouldn't introduce further cwnd reduction, but it did before https://github.com/mozilla/neqo/pull/1465 |
| 214 |
cc.on_packets_lost(Some(now), None, PTO, &[p6], now); |
| 215 |
assert_eq!(cc.cwnd(), cur_cwnd / 2); |
| 216 |
} |