| 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 |
use std::{ |
| 8 |
cell::RefCell, |
| 9 |
cmp::min, |
| 10 |
mem, |
| 11 |
net::SocketAddr, |
| 12 |
rc::Rc, |
| 13 |
time::{Duration, Instant}, |
| 14 |
}; |
| 15 |
|
| 16 |
use enum_map::EnumMap; |
| 17 |
use neqo_common::{event::Provider as _, qdebug, qtrace, Datagram, Decoder, Role}; |
| 18 |
use neqo_crypto::{random, AllowZeroRtt, AuthenticationStatus, ResumptionToken}; |
| 19 |
use test_fixture::{fixture_init, new_neqo_qlog, now, DEFAULT_ADDR}; |
| 20 |
|
| 21 |
use super::{test_internal, CloseReason, Connection, ConnectionId, Output, State}; |
| 22 |
use crate::{ |
| 23 |
addr_valid::{AddressValidation, ValidateAddress}, |
| 24 |
cc::CWND_INITIAL_PKTS, |
| 25 |
cid::ConnectionIdRef, |
| 26 |
events::ConnectionEvent, |
| 27 |
frame::FrameType, |
| 28 |
packet::PacketBuilder, |
| 29 |
pmtud::Pmtud, |
| 30 |
recovery::ACK_ONLY_SIZE_LIMIT, |
| 31 |
stats::{FrameStats, Stats, MAX_PTO_COUNTS}, |
| 32 |
tparams::TransportParameterId::*, |
| 33 |
ConnectionIdDecoder, ConnectionIdGenerator, ConnectionParameters, EmptyConnectionIdGenerator, |
| 34 |
Error, StreamId, StreamType, Version, MIN_INITIAL_PACKET_SIZE, |
| 35 |
}; |
| 36 |
|
| 37 |
// All the tests. |
| 38 |
mod ackrate; |
| 39 |
mod cc; |
| 40 |
mod close; |
| 41 |
mod datagram; |
| 42 |
mod ecn; |
| 43 |
mod handshake; |
| 44 |
mod idle; |
| 45 |
mod keys; |
| 46 |
mod migration; |
| 47 |
mod null; |
| 48 |
mod priority; |
| 49 |
mod recovery; |
| 50 |
mod resumption; |
| 51 |
mod stream; |
| 52 |
mod vn; |
| 53 |
mod zerortt; |
| 54 |
|
| 55 |
const DEFAULT_RTT: Duration = Duration::from_millis(100); |
| 56 |
const AT_LEAST_PTO: Duration = Duration::from_secs(1); |
| 57 |
const DEFAULT_STREAM_DATA: &[u8] = b"message"; |
| 58 |
/// The number of 1-RTT packets sent in `force_idle` by a client. |
| 59 |
const CLIENT_HANDSHAKE_1RTT_PACKETS: usize = 1; |
| 60 |
|
| 61 |
/// WARNING! In this module, this version of the generator needs to be used. |
| 62 |
/// This copies the implementation from |
| 63 |
/// `test_fixture::CountingConnectionIdGenerator`, but it uses the different |
| 64 |
/// types that are exposed to this module. See also `default_client`. |
| 65 |
/// |
| 66 |
/// This version doesn't randomize the length; as the congestion control tests |
| 67 |
/// count the amount of data sent precisely. |
| 68 |
#[derive(Debug, Default)] |
| 69 |
pub struct CountingConnectionIdGenerator { |
| 70 |
counter: u32, |
| 71 |
} |
| 72 |
|
| 73 |
impl ConnectionIdDecoder for CountingConnectionIdGenerator { |
| 74 |
fn decode_cid<'a>(&self, dec: &mut Decoder<'a>) -> Option<ConnectionIdRef<'a>> { |
| 75 |
let len = usize::from(dec.peek_byte()?); |
| 76 |
dec.decode(len).map(ConnectionIdRef::from) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
impl ConnectionIdGenerator for CountingConnectionIdGenerator { |
| 81 |
fn generate_cid(&mut self) -> Option<ConnectionId> { |
| 82 |
let mut r = random::<20>(); |
| 83 |
r[0] = 8; |
| 84 |
r[1] = u8::try_from(self.counter >> 24).ok()?; |
| 85 |
r[2] = u8::try_from((self.counter >> 16) & 0xff).ok()?; |
| 86 |
r[3] = u8::try_from((self.counter >> 8) & 0xff).ok()?; |
| 87 |
r[4] = u8::try_from(self.counter & 0xff).ok()?; |
| 88 |
self.counter += 1; |
| 89 |
Some(ConnectionId::from(&r[..8])) |
| 90 |
} |
| 91 |
|
| 92 |
fn as_decoder(&self) -> &dyn ConnectionIdDecoder { |
| 93 |
self |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
// This is fabulous: because test_fixture uses the public API for Connection, |
| 98 |
// it gets a different type to the ones that are referenced via super::super::*. |
| 99 |
// Thus, this code can't use default_client() and default_server() from |
| 100 |
// test_fixture because they produce different - and incompatible - types. |
| 101 |
// |
| 102 |
// These are a direct copy of those functions. |
| 103 |
pub fn new_client(params: ConnectionParameters) -> Connection { |
| 104 |
fixture_init(); |
| 105 |
let (log, _contents) = new_neqo_qlog(); |
| 106 |
let mut client = Connection::new_client( |
| 107 |
test_fixture::DEFAULT_SERVER_NAME, |
| 108 |
test_fixture::DEFAULT_ALPN, |
| 109 |
Rc::new(RefCell::new(CountingConnectionIdGenerator::default())), |
| 110 |
DEFAULT_ADDR, |
| 111 |
DEFAULT_ADDR, |
| 112 |
params, |
| 113 |
now(), |
| 114 |
) |
| 115 |
.expect("create a default client"); |
| 116 |
client.set_qlog(log); |
| 117 |
client |
| 118 |
} |
| 119 |
|
| 120 |
pub fn default_client() -> Connection { |
| 121 |
new_client(ConnectionParameters::default()) |
| 122 |
} |
| 123 |
|
| 124 |
fn zero_len_cid_client(local_addr: SocketAddr, remote_addr: SocketAddr) -> Connection { |
| 125 |
Connection::new_client( |
| 126 |
test_fixture::DEFAULT_SERVER_NAME, |
| 127 |
test_fixture::DEFAULT_ALPN, |
| 128 |
Rc::new(RefCell::new(EmptyConnectionIdGenerator::default())), |
| 129 |
local_addr, |
| 130 |
remote_addr, |
| 131 |
ConnectionParameters::default(), |
| 132 |
now(), |
| 133 |
) |
| 134 |
.unwrap() |
| 135 |
} |
| 136 |
|
| 137 |
pub fn new_server(params: ConnectionParameters) -> Connection { |
| 138 |
fixture_init(); |
| 139 |
let (log, _contents) = new_neqo_qlog(); |
| 140 |
let mut c = Connection::new_server( |
| 141 |
test_fixture::DEFAULT_KEYS, |
| 142 |
test_fixture::DEFAULT_ALPN, |
| 143 |
Rc::new(RefCell::new(CountingConnectionIdGenerator::default())), |
| 144 |
params, |
| 145 |
) |
| 146 |
.expect("create a default server"); |
| 147 |
c.set_qlog(log); |
| 148 |
c.server_enable_0rtt(&test_fixture::anti_replay(), AllowZeroRtt {}) |
| 149 |
.expect("enable 0-RTT"); |
| 150 |
c |
| 151 |
} |
| 152 |
pub fn default_server() -> Connection { |
| 153 |
new_server(ConnectionParameters::default()) |
| 154 |
} |
| 155 |
pub fn resumed_server(client: &Connection) -> Connection { |
| 156 |
new_server(ConnectionParameters::default().versions(client.version(), Version::all())) |
| 157 |
} |
| 158 |
|
| 159 |
/// If state is `AuthenticationNeeded` call `authenticated()`. This function will |
| 160 |
/// consume all outstanding events on the connection. |
| 161 |
pub fn maybe_authenticate(conn: &mut Connection) -> bool { |
| 162 |
let authentication_needed = |e| matches!(e, ConnectionEvent::AuthenticationNeeded); |
| 163 |
if conn.events().any(authentication_needed) { |
| 164 |
conn.authenticated(AuthenticationStatus::Ok, now()); |
| 165 |
return true; |
| 166 |
} |
| 167 |
false |
| 168 |
} |
| 169 |
|
| 170 |
/// Compute the RTT variance after `n` ACKs or other RTT updates. |
| 171 |
pub fn rttvar_after_n_updates(n: usize, rtt: Duration) -> Duration { |
| 172 |
assert!(n > 0); |
| 173 |
let mut rttvar = rtt / 2; |
| 174 |
for _ in 1..n { |
| 175 |
rttvar = rttvar * 3 / 4; |
| 176 |
} |
| 177 |
rttvar |
| 178 |
} |
| 179 |
|
| 180 |
/// This inserts a PING frame into packets. |
| 181 |
struct PingWriter {} |
| 182 |
|
| 183 |
impl test_internal::FrameWriter for PingWriter { |
| 184 |
fn write_frames(&mut self, builder: &mut PacketBuilder) { |
| 185 |
builder.encode_varint(FrameType::Ping); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/// Drive the handshake between the client and server. |
| 190 |
fn handshake_with_modifier( |
| 191 |
client: &mut Connection, |
| 192 |
server: &mut Connection, |
| 193 |
now: Instant, |
| 194 |
rtt: Duration, |
| 195 |
modifier: fn(Datagram) -> Option<Datagram>, |
| 196 |
) -> Instant { |
| 197 |
let mut a = client; |
| 198 |
let mut b = server; |
| 199 |
let mut now = now; |
| 200 |
|
| 201 |
let mut input = None; |
| 202 |
let is_done = |c: &mut Connection| { |
| 203 |
matches!( |
| 204 |
c.state(), |
| 205 |
State::Confirmed | State::Closing { .. } | State::Closed(..) |
| 206 |
) |
| 207 |
}; |
| 208 |
|
| 209 |
let mut did_ping = EnumMap::from_array([false, false]); |
| 210 |
while !is_done(a) { |
| 211 |
_ = maybe_authenticate(a); |
| 212 |
// Insert a PING frame into the first application data packet an endpoint sends, |
| 213 |
// in order to force the peer to ACK it. For the server, this is depending on the |
| 214 |
// client's connection state, which is accessible during the tests. |
| 215 |
// |
| 216 |
// We're doing this to prevent packet loss from delaying ACKs, which would cause |
| 217 |
// cwnd to shrink, and also to prevent the delayed ACK timer from being armed after |
| 218 |
// the handshake, which is not something the tests are written to account for. |
| 219 |
let should_ping = !did_ping[a.role()] |
| 220 |
&& (a.role() == Role::Client && *a.state() == State::Connected |
| 221 |
|| (a.role() == Role::Server && *b.state() == State::Connected)); |
| 222 |
if should_ping { |
| 223 |
a.test_frame_writer = Some(Box::new(PingWriter {})); |
| 224 |
} |
| 225 |
let output = a.process(input, now).dgram(); |
| 226 |
if should_ping { |
| 227 |
a.test_frame_writer = None; |
| 228 |
did_ping[a.role()] = true; |
| 229 |
} |
| 230 |
input = output.and_then(modifier); |
| 231 |
qtrace!("handshake: t += {:?}", rtt / 2); |
| 232 |
now += rtt / 2; |
| 233 |
mem::swap(&mut a, &mut b); |
| 234 |
} |
| 235 |
if let Some(d) = input { |
| 236 |
a.process_input(d, now); |
| 237 |
} |
| 238 |
now |
| 239 |
} |
| 240 |
|
| 241 |
fn handshake( |
| 242 |
client: &mut Connection, |
| 243 |
server: &mut Connection, |
| 244 |
now: Instant, |
| 245 |
rtt: Duration, |
| 246 |
) -> Instant { |
| 247 |
handshake_with_modifier(client, server, now, rtt, Some) |
| 248 |
} |
| 249 |
|
| 250 |
fn connect_fail( |
| 251 |
client: &mut Connection, |
| 252 |
server: &mut Connection, |
| 253 |
client_error: Error, |
| 254 |
server_error: Error, |
| 255 |
) { |
| 256 |
handshake(client, server, now(), Duration::new(0, 0)); |
| 257 |
assert_error(client, &CloseReason::Transport(client_error)); |
| 258 |
assert_error(server, &CloseReason::Transport(server_error)); |
| 259 |
} |
| 260 |
|
| 261 |
fn connect_with_rtt_and_modifier( |
| 262 |
client: &mut Connection, |
| 263 |
server: &mut Connection, |
| 264 |
now: Instant, |
| 265 |
rtt: Duration, |
| 266 |
modifier: fn(Datagram) -> Option<Datagram>, |
| 267 |
) -> Instant { |
| 268 |
fn check_rtt(stats: &Stats, rtt: Duration) { |
| 269 |
assert_eq!(stats.rtt, rtt); |
| 270 |
// Validate that rttvar has been computed correctly based on the number of RTT updates. |
| 271 |
let n = stats.frame_rx.ack + usize::from(stats.rtt_init_guess); |
| 272 |
assert_eq!(stats.rttvar, rttvar_after_n_updates(n, rtt)); |
| 273 |
} |
| 274 |
let now = handshake_with_modifier(client, server, now, rtt, modifier); |
| 275 |
assert_eq!(*client.state(), State::Confirmed); |
| 276 |
assert_eq!(*server.state(), State::Confirmed); |
| 277 |
|
| 278 |
check_rtt(&client.stats(), rtt); |
| 279 |
check_rtt(&server.stats(), rtt); |
| 280 |
now |
| 281 |
} |
| 282 |
|
| 283 |
fn connect_with_rtt( |
| 284 |
client: &mut Connection, |
| 285 |
server: &mut Connection, |
| 286 |
now: Instant, |
| 287 |
rtt: Duration, |
| 288 |
) -> Instant { |
| 289 |
connect_with_rtt_and_modifier(client, server, now, rtt, Some) |
| 290 |
} |
| 291 |
|
| 292 |
fn connect(client: &mut Connection, server: &mut Connection) { |
| 293 |
connect_with_rtt(client, server, now(), Duration::new(0, 0)); |
| 294 |
} |
| 295 |
|
| 296 |
fn assert_error(c: &Connection, expected: &CloseReason) { |
| 297 |
match c.state() { |
| 298 |
State::Closing { error, .. } | State::Draining { error, .. } | State::Closed(error) => { |
| 299 |
assert_eq!(*error, *expected, "{c} error mismatch"); |
| 300 |
} |
| 301 |
_ => panic!("bad state {:?}", c.state()), |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
fn exchange_ticket( |
| 306 |
client: &mut Connection, |
| 307 |
server: &mut Connection, |
| 308 |
now: Instant, |
| 309 |
) -> ResumptionToken { |
| 310 |
let validation = AddressValidation::new(now, ValidateAddress::NoToken).unwrap(); |
| 311 |
let validation = Rc::new(RefCell::new(validation)); |
| 312 |
server.set_validation(&validation); |
| 313 |
server.send_ticket(now, &[]).expect("can send ticket"); |
| 314 |
let ticket = server.process_output(now).dgram(); |
| 315 |
assert!(ticket.is_some()); |
| 316 |
client.process_input(ticket.unwrap(), now); |
| 317 |
assert_eq!(*client.state(), State::Confirmed); |
| 318 |
get_tokens(client).pop().expect("should have token") |
| 319 |
} |
| 320 |
|
| 321 |
/// The `handshake` method inserts PING frames into the first application data packets, |
| 322 |
/// which forces each peer to ACK them. As a side effect, that causes both sides of the |
| 323 |
/// connection to be idle aftwerwards. This method simply verifies that this is the case. |
| 324 |
fn assert_idle(client: &mut Connection, server: &mut Connection, rtt: Duration, now: Instant) { |
| 325 |
let idle_timeout = min( |
| 326 |
client.conn_params.get_idle_timeout(), |
| 327 |
server.conn_params.get_idle_timeout(), |
| 328 |
); |
| 329 |
// Client started its idle period half an RTT before now. |
| 330 |
assert_eq!( |
| 331 |
client.process_output(now), |
| 332 |
Output::Callback(idle_timeout - rtt / 2) |
| 333 |
); |
| 334 |
assert_eq!(server.process_output(now), Output::Callback(idle_timeout)); |
| 335 |
} |
| 336 |
|
| 337 |
/// Connect with an RTT and then force both peers to be idle. |
| 338 |
fn connect_rtt_idle_with_modifier( |
| 339 |
client: &mut Connection, |
| 340 |
server: &mut Connection, |
| 341 |
rtt: Duration, |
| 342 |
modifier: fn(Datagram) -> Option<Datagram>, |
| 343 |
) -> Instant { |
| 344 |
let now = connect_with_rtt_and_modifier(client, server, now(), rtt, modifier); |
| 345 |
assert_idle(client, server, rtt, now); |
| 346 |
// Drain events from both as well. |
| 347 |
_ = client.events().count(); |
| 348 |
_ = server.events().count(); |
| 349 |
qtrace!("----- connected and idle with RTT {rtt:?}"); |
| 350 |
now |
| 351 |
} |
| 352 |
|
| 353 |
fn connect_rtt_idle(client: &mut Connection, server: &mut Connection, rtt: Duration) -> Instant { |
| 354 |
connect_rtt_idle_with_modifier(client, server, rtt, Some) |
| 355 |
} |
| 356 |
|
| 357 |
fn connect_force_idle_with_modifier( |
| 358 |
client: &mut Connection, |
| 359 |
server: &mut Connection, |
| 360 |
modifier: fn(Datagram) -> Option<Datagram>, |
| 361 |
) { |
| 362 |
connect_rtt_idle_with_modifier(client, server, Duration::new(0, 0), modifier); |
| 363 |
} |
| 364 |
|
| 365 |
fn connect_force_idle(client: &mut Connection, server: &mut Connection) { |
| 366 |
connect_force_idle_with_modifier(client, server, Some); |
| 367 |
} |
| 368 |
|
| 369 |
fn fill_stream(c: &mut Connection, stream: StreamId) { |
| 370 |
const BLOCK_SIZE: usize = 4_096; |
| 371 |
loop { |
| 372 |
let bytes_sent = c.stream_send(stream, &[0x42; BLOCK_SIZE]).unwrap(); |
| 373 |
qtrace!("fill_cwnd wrote {bytes_sent} bytes"); |
| 374 |
if bytes_sent < BLOCK_SIZE { |
| 375 |
break; |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
/// This fills the congestion window from a single source. |
| 381 |
/// As the pacer will interfere with this, this moves time forward |
| 382 |
/// as `Output::Callback` is received. Because it is hard to tell |
| 383 |
/// from the return value whether a timeout is an ACK delay, PTO, or |
| 384 |
/// pacing, this looks at the congestion window to tell when to stop. |
| 385 |
/// Returns a list of datagrams and the new time. |
| 386 |
fn fill_cwnd(c: &mut Connection, stream: StreamId, mut now: Instant) -> (Vec<Datagram>, Instant) { |
| 387 |
qtrace!("fill_cwnd starting cwnd: {}", cwnd_avail(c)); |
| 388 |
fill_stream(c, stream); |
| 389 |
|
| 390 |
let mut total_dgrams = Vec::new(); |
| 391 |
loop { |
| 392 |
let pkt = c.process_output(now); |
| 393 |
qtrace!( |
| 394 |
"fill_cwnd cwnd remaining={}, output: {pkt:?}", |
| 395 |
cwnd_avail(c) |
| 396 |
); |
| 397 |
match pkt { |
| 398 |
Output::Datagram(dgram) => { |
| 399 |
total_dgrams.push(dgram); |
| 400 |
} |
| 401 |
Output::Callback(t) => { |
| 402 |
if cwnd_avail(c) < ACK_ONLY_SIZE_LIMIT { |
| 403 |
break; |
| 404 |
} |
| 405 |
now += t; |
| 406 |
} |
| 407 |
Output::None => panic!(), |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
qtrace!( |
| 412 |
"fill_cwnd sent {} bytes", |
| 413 |
total_dgrams.iter().map(Datagram::len).sum::<usize>() |
| 414 |
); |
| 415 |
(total_dgrams, now) |
| 416 |
} |
| 417 |
|
| 418 |
/// This function is like the combination of `fill_cwnd` and `ack_bytes`. |
| 419 |
/// However, it acknowledges everything inline and preserves an RTT of `DEFAULT_RTT`. |
| 420 |
fn increase_cwnd( |
| 421 |
sender: &mut Connection, |
| 422 |
receiver: &mut Connection, |
| 423 |
stream: StreamId, |
| 424 |
mut now: Instant, |
| 425 |
) -> Instant { |
| 426 |
fill_stream(sender, stream); |
| 427 |
loop { |
| 428 |
let pkt = sender.process_output(now); |
| 429 |
match pkt { |
| 430 |
Output::Datagram(dgram) => { |
| 431 |
receiver.process_input(dgram, now + DEFAULT_RTT / 2); |
| 432 |
} |
| 433 |
Output::Callback(t) => { |
| 434 |
if t < DEFAULT_RTT { |
| 435 |
now += t; |
| 436 |
} else { |
| 437 |
break; // We're on PTO now. |
| 438 |
} |
| 439 |
} |
| 440 |
Output::None => panic!(), |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
// Now acknowledge all those packets at once. |
| 445 |
now += DEFAULT_RTT / 2; |
| 446 |
let ack = receiver.process_output(now).dgram(); |
| 447 |
now += DEFAULT_RTT / 2; |
| 448 |
sender.process_input(ack.unwrap(), now); |
| 449 |
now |
| 450 |
} |
| 451 |
|
| 452 |
/// Receive multiple packets and generate an ack-only packet. |
| 453 |
/// |
| 454 |
/// # Panics |
| 455 |
/// |
| 456 |
/// The caller is responsible for ensuring that `dest` has received |
| 457 |
/// enough data that it wants to generate an ACK. This panics if |
| 458 |
/// no ACK frame is generated. |
| 459 |
fn ack_bytes<D>(dest: &mut Connection, stream: StreamId, in_dgrams: D, now: Instant) -> Datagram |
| 460 |
where |
| 461 |
D: IntoIterator<Item = Datagram>, |
| 462 |
D::IntoIter: ExactSizeIterator, |
| 463 |
{ |
| 464 |
let mut srv_buf = [0; 4_096]; |
| 465 |
|
| 466 |
let in_dgrams = in_dgrams.into_iter(); |
| 467 |
qdebug!("[{dest}] ack_bytes {} datagrams", in_dgrams.len()); |
| 468 |
for dgram in in_dgrams { |
| 469 |
dest.process_input(dgram, now); |
| 470 |
} |
| 471 |
|
| 472 |
loop { |
| 473 |
let (bytes_read, _fin) = dest.stream_recv(stream, &mut srv_buf).unwrap(); |
| 474 |
qtrace!("[{dest}] ack_bytes read {bytes_read} bytes"); |
| 475 |
if bytes_read == 0 { |
| 476 |
break; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
dest.process_output(now).dgram().unwrap() |
| 481 |
} |
| 482 |
|
| 483 |
// Get the current congestion window for the connection. |
| 484 |
fn cwnd(c: &Connection) -> usize { |
| 485 |
c.paths.primary().unwrap().borrow().sender().cwnd() |
| 486 |
} |
| 487 |
|
| 488 |
fn cwnd_avail(c: &Connection) -> usize { |
| 489 |
c.paths.primary().unwrap().borrow().sender().cwnd_avail() |
| 490 |
} |
| 491 |
|
| 492 |
fn cwnd_min(c: &Connection) -> usize { |
| 493 |
c.paths.primary().unwrap().borrow().sender().cwnd_min() |
| 494 |
} |
| 495 |
|
| 496 |
fn induce_persistent_congestion( |
| 497 |
client: &mut Connection, |
| 498 |
server: &mut Connection, |
| 499 |
stream: StreamId, |
| 500 |
mut now: Instant, |
| 501 |
) -> Instant { |
| 502 |
// Note: wait some arbitrary time that should be longer than pto |
| 503 |
// timer. This is rather brittle. |
| 504 |
qtrace!("[{client}] induce_persistent_congestion"); |
| 505 |
now += AT_LEAST_PTO; |
| 506 |
|
| 507 |
let mut pto_counts = [0; MAX_PTO_COUNTS]; |
| 508 |
assert_eq!(client.stats.borrow().pto_counts, pto_counts); |
| 509 |
|
| 510 |
qtrace!("[{client}] first PTO"); |
| 511 |
let (c_tx_dgrams, next_now) = fill_cwnd(client, stream, now); |
| 512 |
now = next_now; |
| 513 |
assert_eq!(c_tx_dgrams.len(), 2); // Two PTO packets |
| 514 |
|
| 515 |
pto_counts[0] = 1; |
| 516 |
assert_eq!(client.stats.borrow().pto_counts, pto_counts); |
| 517 |
|
| 518 |
qtrace!("[{client}] second PTO"); |
| 519 |
now += AT_LEAST_PTO * 2; |
| 520 |
let (c_tx_dgrams, next_now) = fill_cwnd(client, stream, now); |
| 521 |
now = next_now; |
| 522 |
assert_eq!(c_tx_dgrams.len(), 2); // Two PTO packets |
| 523 |
|
| 524 |
pto_counts[0] = 0; |
| 525 |
pto_counts[1] = 1; |
| 526 |
assert_eq!(client.stats.borrow().pto_counts, pto_counts); |
| 527 |
|
| 528 |
qtrace!("[{client}] third PTO"); |
| 529 |
now += AT_LEAST_PTO * 4; |
| 530 |
let (c_tx_dgrams, next_now) = fill_cwnd(client, stream, now); |
| 531 |
now = next_now; |
| 532 |
assert_eq!(c_tx_dgrams.len(), 2); // Two PTO packets |
| 533 |
|
| 534 |
pto_counts[1] = 0; |
| 535 |
pto_counts[2] = 1; |
| 536 |
assert_eq!(client.stats.borrow().pto_counts, pto_counts); |
| 537 |
|
| 538 |
// An ACK for the third PTO causes persistent congestion. |
| 539 |
let s_ack = ack_bytes(server, stream, c_tx_dgrams, now); |
| 540 |
client.process_input(s_ack, now); |
| 541 |
assert_eq!(cwnd(client), cwnd_min(client)); |
| 542 |
now |
| 543 |
} |
| 544 |
|
| 545 |
/// This magic number is the size of the client's CWND after the handshake completes. |
| 546 |
/// This is the same as the initial congestion window, because during the handshake |
| 547 |
/// the cc is app limited and cwnd is not increased. |
| 548 |
/// |
| 549 |
/// As we change how we build packets, or even as NSS changes, |
| 550 |
/// this number might be different. The tests that depend on this |
| 551 |
/// value could fail as a result of variations, so it's OK to just |
| 552 |
/// change this value, but it is good to first understand where the |
| 553 |
/// change came from. |
| 554 |
const POST_HANDSHAKE_CWND: usize = Pmtud::default_plpmtu(DEFAULT_ADDR.ip()) * CWND_INITIAL_PKTS; |
| 555 |
|
| 556 |
/// Determine the number of packets required to fill the CWND. |
| 557 |
const fn cwnd_packets(data: usize, mtu: usize) -> usize { |
| 558 |
// Add one if the last chunk is >= ACK_ONLY_SIZE_LIMIT. |
| 559 |
(data + mtu - ACK_ONLY_SIZE_LIMIT) / mtu |
| 560 |
} |
| 561 |
|
| 562 |
/// Determine the size of the last packet. |
| 563 |
/// The minimal size of a packet is `ACK_ONLY_SIZE_LIMIT`. |
| 564 |
const fn last_packet(cwnd: usize, mtu: usize) -> usize { |
| 565 |
if (cwnd % mtu) > ACK_ONLY_SIZE_LIMIT { |
| 566 |
cwnd % mtu |
| 567 |
} else { |
| 568 |
mtu |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
/// Assert that the set of packets fill the CWND. |
| 573 |
fn assert_full_cwnd(packets: &[Datagram], cwnd: usize, mtu: usize) { |
| 574 |
assert_eq!(packets.len(), cwnd_packets(cwnd, mtu)); |
| 575 |
let (last, rest) = packets.split_last().unwrap(); |
| 576 |
assert!(rest.iter().all(|d| d.len() == mtu)); |
| 577 |
assert_eq!(last.len(), last_packet(cwnd, mtu)); |
| 578 |
} |
| 579 |
|
| 580 |
/// Send something on a stream from `sender` to `receiver`, maybe allowing for pacing. |
| 581 |
/// Takes a modifier function that can be used to modify the datagram before it is sent. |
| 582 |
/// Return the resulting datagram and the new time. |
| 583 |
#[must_use] |
| 584 |
fn send_something_paced_with_modifier( |
| 585 |
sender: &mut Connection, |
| 586 |
mut now: Instant, |
| 587 |
allow_pacing: bool, |
| 588 |
modifier: fn(Datagram) -> Option<Datagram>, |
| 589 |
) -> (Datagram, Instant) { |
| 590 |
let stream_id = sender.stream_create(StreamType::UniDi).unwrap(); |
| 591 |
assert!(sender.stream_send(stream_id, DEFAULT_STREAM_DATA).is_ok()); |
| 592 |
assert!(sender.stream_close_send(stream_id).is_ok()); |
| 593 |
qdebug!("[{sender}] send_something on {stream_id}"); |
| 594 |
let dgram = match sender.process_output(now) { |
| 595 |
Output::Callback(t) => { |
| 596 |
assert!(allow_pacing, "send_something: unexpected delay"); |
| 597 |
now += t; |
| 598 |
sender |
| 599 |
.process_output(now) |
| 600 |
.dgram() |
| 601 |
.expect("send_something: should have something to send") |
| 602 |
} |
| 603 |
Output::Datagram(d) => d, |
| 604 |
Output::None => panic!("send_something: got Output::None"), |
| 605 |
}; |
| 606 |
(modifier(dgram).unwrap(), now) |
| 607 |
} |
| 608 |
|
| 609 |
fn send_something_paced( |
| 610 |
sender: &mut Connection, |
| 611 |
now: Instant, |
| 612 |
allow_pacing: bool, |
| 613 |
) -> (Datagram, Instant) { |
| 614 |
send_something_paced_with_modifier(sender, now, allow_pacing, Some) |
| 615 |
} |
| 616 |
|
| 617 |
fn send_something_with_modifier( |
| 618 |
sender: &mut Connection, |
| 619 |
now: Instant, |
| 620 |
modifier: fn(Datagram) -> Option<Datagram>, |
| 621 |
) -> Datagram { |
| 622 |
send_something_paced_with_modifier(sender, now, false, modifier).0 |
| 623 |
} |
| 624 |
|
| 625 |
/// Send something on a stream from `sender` to `receiver`. |
| 626 |
/// Return the resulting datagram. |
| 627 |
fn send_something(sender: &mut Connection, now: Instant) -> Datagram { |
| 628 |
send_something_with_modifier(sender, now, Some) |
| 629 |
} |
| 630 |
|
| 631 |
/// Send something, but add a little something extra into the output. |
| 632 |
fn send_with_extra<W>(sender: &mut Connection, writer: W, now: Instant) -> Datagram |
| 633 |
where |
| 634 |
W: test_internal::FrameWriter + 'static, |
| 635 |
{ |
| 636 |
sender.test_frame_writer = Some(Box::new(writer)); |
| 637 |
let res = send_something_with_modifier(sender, now, Some); |
| 638 |
sender.test_frame_writer = None; |
| 639 |
res |
| 640 |
} |
| 641 |
|
| 642 |
/// Send something on a stream from `sender` through a modifier to `receiver`. |
| 643 |
/// Return any ACK that might result. |
| 644 |
fn send_with_modifier_and_receive( |
| 645 |
sender: &mut Connection, |
| 646 |
receiver: &mut Connection, |
| 647 |
now: Instant, |
| 648 |
modifier: fn(Datagram) -> Option<Datagram>, |
| 649 |
) -> Option<Datagram> { |
| 650 |
let dgram = send_something_with_modifier(sender, now, modifier); |
| 651 |
receiver.process(Some(dgram), now).dgram() |
| 652 |
} |
| 653 |
|
| 654 |
/// Send something on a stream from `sender` to `receiver`. |
| 655 |
/// Return any ACK that might result. |
| 656 |
fn send_and_receive( |
| 657 |
sender: &mut Connection, |
| 658 |
receiver: &mut Connection, |
| 659 |
now: Instant, |
| 660 |
) -> Option<Datagram> { |
| 661 |
send_with_modifier_and_receive(sender, receiver, now, Some) |
| 662 |
} |
| 663 |
|
| 664 |
fn get_tokens(client: &mut Connection) -> Vec<ResumptionToken> { |
| 665 |
client |
| 666 |
.events() |
| 667 |
.filter_map(|e| { |
| 668 |
if let ConnectionEvent::ResumptionToken(token) = e { |
| 669 |
Some(token) |
| 670 |
} else { |
| 671 |
None |
| 672 |
} |
| 673 |
}) |
| 674 |
.collect() |
| 675 |
} |
| 676 |
|
| 677 |
fn assert_default_stats(stats: &Stats) { |
| 678 |
assert_eq!(stats.packets_rx, 0); |
| 679 |
assert_eq!(stats.packets_tx, 0); |
| 680 |
let dflt_frames = FrameStats::default(); |
| 681 |
assert_eq!(stats.frame_rx, dflt_frames); |
| 682 |
assert_eq!(stats.frame_tx, dflt_frames); |
| 683 |
} |
| 684 |
|
| 685 |
fn assert_path_challenge_min_len(c: &Connection, d: &Datagram, now: Instant) { |
| 686 |
let path = c.paths.find_path( |
| 687 |
d.source(), |
| 688 |
d.destination(), |
| 689 |
&c.conn_params, |
| 690 |
now, |
| 691 |
&mut c.stats.borrow_mut(), |
| 692 |
); |
| 693 |
if path.borrow().amplification_limit() < path.borrow().plpmtu() { |
| 694 |
// If the amplification limit is less than the PLPMTU, then the path |
| 695 |
// challenge will not have been padded. |
| 696 |
return; |
| 697 |
} |
| 698 |
assert!( |
| 699 |
d.len() >= MIN_INITIAL_PACKET_SIZE, |
| 700 |
"{} < {MIN_INITIAL_PACKET_SIZE}", |
| 701 |
d.len() |
| 702 |
); |
| 703 |
} |
| 704 |
|
| 705 |
#[test] |
| 706 |
fn create_client() { |
| 707 |
let client = default_client(); |
| 708 |
assert_eq!(client.role(), Role::Client); |
| 709 |
assert!(matches!(client.state(), State::Init)); |
| 710 |
let stats = client.stats(); |
| 711 |
assert_default_stats(&stats); |
| 712 |
assert_eq!(stats.rtt, crate::rtt::INITIAL_RTT); |
| 713 |
assert_eq!(stats.rttvar, crate::rtt::INITIAL_RTT / 2); |
| 714 |
} |
| 715 |
|
| 716 |
#[test] |
| 717 |
fn create_server() { |
| 718 |
let server = default_server(); |
| 719 |
assert_eq!(server.role(), Role::Server); |
| 720 |
assert!(matches!(server.state(), State::Init)); |
| 721 |
let stats = server.stats(); |
| 722 |
assert_default_stats(&stats); |
| 723 |
// Server won't have a default path, so no RTT. |
| 724 |
assert_eq!(stats.rtt, Duration::from_secs(0)); |
| 725 |
} |
| 726 |
|
| 727 |
#[test] |
| 728 |
fn tp_grease() { |
| 729 |
for enable in [true, false] { |
| 730 |
let client = new_client(ConnectionParameters::default().grease(enable)); |
| 731 |
let grease = client.tps.borrow_mut().local().get_empty(GreaseQuicBit); |
| 732 |
assert_eq!(enable, grease); |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
#[test] |
| 737 |
fn tp_disable_migration() { |
| 738 |
for disable in [true, false] { |
| 739 |
let client = new_client(ConnectionParameters::default().disable_migration(disable)); |
| 740 |
let disable_migration = client.tps.borrow_mut().local().get_empty(DisableMigration); |
| 741 |
assert_eq!(disable, disable_migration); |
| 742 |
} |
| 743 |
} |