Debian Codegraph @

third_party/rust/neqo-transport/src/connection/tests/recovery.rs

raw
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::time::{Duration, Instant};
8
9 use neqo_common::qdebug;
10 use neqo_crypto::AuthenticationStatus;
11 use test_fixture::{
12 assertions::{assert_handshake, assert_initial},
13 now, split_datagram,
14 };
15
16 use super::{
17 super::{Connection, ConnectionParameters, Output, State},
18 assert_full_cwnd, connect, connect_force_idle, connect_rtt_idle, connect_with_rtt, cwnd,
19 default_client, default_server, fill_cwnd, maybe_authenticate, new_client, send_and_receive,
20 send_something, AT_LEAST_PTO, DEFAULT_ADDR, DEFAULT_RTT, DEFAULT_STREAM_DATA,
21 POST_HANDSHAKE_CWND,
22 };
23 use crate::{
24 connection::{test_internal::FrameWriter, tests::cwnd_min},
25 frame::FrameType,
26 packet::PacketBuilder,
27 recovery::{
28 FAST_PTO_SCALE, MAX_OUTSTANDING_UNACK, MAX_PTO_PACKET_COUNT, MIN_OUTSTANDING_UNACK,
29 },
30 rtt::GRANULARITY,
31 stats::MAX_PTO_COUNTS,
32 tparams::{TransportParameter, TransportParameterId::*},
33 tracking::{DEFAULT_LOCAL_ACK_DELAY, DEFAULT_REMOTE_ACK_DELAY},
34 CloseReason, Error, Pmtud, StreamType,
35 };
36
37 #[test]
38 fn pto_works_basic() {
39 let mut client = default_client();
40 let mut server = default_server();
41 connect_force_idle(&mut client, &mut server);
42
43 let mut now = now();
44
45 let res = client.process_output(now);
46 let idle_timeout = ConnectionParameters::default().get_idle_timeout();
47 assert_eq!(res, Output::Callback(idle_timeout));
48
49 // Send data on two streams
50 let stream1 = client.stream_create(StreamType::UniDi).unwrap();
51 assert_eq!(client.stream_send(stream1, b"hello").unwrap(), 5);
52 assert_eq!(client.stream_send(stream1, b" world!").unwrap(), 7);
53
54 let stream2 = client.stream_create(StreamType::UniDi).unwrap();
55 assert_eq!(client.stream_send(stream2, b"there!").unwrap(), 6);
56
57 // Send a packet after some time.
58 now += Duration::from_secs(10);
59 let out = client.process_output(now);
60 assert!(out.dgram().is_some());
61
62 // Nothing to do, should return callback
63 let out = client.process_output(now);
64 assert!(matches!(out, Output::Callback(_)));
65
66 // One second later, it should want to send PTO packet
67 now += AT_LEAST_PTO;
68 let out = client.process_output(now);
69
70 let stream_before = server.stats().frame_rx.stream;
71 server.process_input(out.dgram().unwrap(), now);
72 assert_eq!(server.stats().frame_rx.stream, stream_before + 2);
73 }
74
75 #[test]
76 fn pto_works_full_cwnd() {
77 let mut client = default_client();
78 let mut server = default_server();
79 let now = connect_rtt_idle(&mut client, &mut server, DEFAULT_RTT);
80
81 // Send lots of data.
82 let stream_id = client.stream_create(StreamType::UniDi).unwrap();
83 let (dgrams, now) = fill_cwnd(&mut client, stream_id, now);
84 assert_full_cwnd(&dgrams, POST_HANDSHAKE_CWND, client.plpmtu());
85
86 // Fill the CWND after waiting for a PTO.
87 let (dgrams, now) = fill_cwnd(&mut client, stream_id, now + AT_LEAST_PTO);
88 // Two packets in the PTO.
89 // The first should be full sized; the second might be small.
90 assert_eq!(dgrams.len(), 2);
91 assert_eq!(dgrams[0].len(), client.plpmtu());
92
93 // Both datagrams contain one or more STREAM frames.
94 for d in dgrams {
95 let stream_before = server.stats().frame_rx.stream;
96 server.process_input(d, now);
97 assert!(server.stats().frame_rx.stream > stream_before);
98 }
99 }
100
101 #[test]
102 fn pto_works_ping() {
103 let mut client = default_client();
104 let mut server = default_server();
105 connect_force_idle(&mut client, &mut server);
106 let mut now = now() + Duration::from_secs(10);
107
108 // Send a few packets from the client.
109 let pkt0 = send_something(&mut client, now);
110 let pkt1 = send_something(&mut client, now);
111 let pkt2 = send_something(&mut client, now);
112 let pkt3 = send_something(&mut client, now);
113
114 // Nothing to do, should return callback
115 let cb = client.process_output(now).callback();
116 // The PTO timer is calculated with:
117 // RTT + max(rttvar * 4, GRANULARITY) + max_ack_delay
118 // With zero RTT and rttvar, max_ack_delay is minimum too (GRANULARITY)
119 assert_eq!(cb, GRANULARITY * 2);
120
121 // Process these by server, skipping pkt0
122 let srv0 = server.process(Some(pkt1), now).dgram();
123 assert!(srv0.is_some()); // ooo, ack client pkt1
124
125 now += Duration::from_millis(20);
126
127 // process pkt2 (immediate ack because last ack was more than an RTT ago; RTT=0)
128 let srv1 = server.process(Some(pkt2), now).dgram();
129 assert!(srv1.is_some()); // this is now dropped
130
131 now += Duration::from_millis(20);
132 // process pkt3 (acked for same reason)
133 let srv2 = server.process(Some(pkt3), now).dgram();
134 // ack client pkt 2 & 3
135 assert!(srv2.is_some());
136
137 // client processes ack
138 let pkt4 = client.process(srv2, now).dgram();
139 // client resends data from pkt0
140 assert!(pkt4.is_some());
141
142 // server sees ooo pkt0 and generates immediate ack
143 let srv3 = server.process(Some(pkt0), now).dgram();
144 assert!(srv3.is_some());
145
146 // Accept the acknowledgment.
147 let pkt5 = client.process(srv3, now).dgram();
148 assert!(pkt5.is_none());
149
150 now += Duration::from_millis(70);
151 // PTO expires. No unacked data. Only send PING.
152 let client_pings = client.stats().frame_tx.ping;
153 let pkt6 = client.process_output(now).dgram();
154 assert_eq!(client.stats().frame_tx.ping, client_pings + 1);
155
156 let server_pings = server.stats().frame_rx.ping;
157 server.process_input(pkt6.unwrap(), now);
158 assert_eq!(server.stats().frame_rx.ping, server_pings + 1);
159 }
160
161 #[test]
162 fn pto_initial() {
163 const INITIAL_PTO: Duration = Duration::from_millis(300);
164 let mut now = now();
165
166 // This test makes too many assumptions about single-packet PTOs for multi-packet MLKEM flights
167 qdebug!("---- client: generate CH");
168 let mut client = new_client(ConnectionParameters::default().mlkem(false));
169 let pkt1 = client.process_output(now).dgram();
170 assert!(pkt1.is_some());
171 assert_eq!(pkt1.clone().unwrap().len(), client.plpmtu());
172
173 let delay = client.process_output(now).callback();
174 assert_eq!(delay, INITIAL_PTO);
175
176 // Resend initial after PTO.
177 now += delay;
178 let pkt2 = client.process_output(now).dgram();
179 assert!(pkt2.is_some());
180 assert_eq!(pkt2.unwrap().len(), client.plpmtu());
181
182 let delay = client.process_output(now).callback();
183 // PTO has doubled.
184 assert_eq!(delay, INITIAL_PTO * 2);
185
186 // Server process the first initial pkt.
187 let mut server = default_server();
188 let out = server.process(pkt1, now).dgram();
189 assert!(out.is_some());
190
191 // Client receives ack for the first initial packet as well a Handshake packet.
192 // After the handshake packet the initial keys and the crypto stream for the initial
193 // packet number space will be discarded.
194 // Here only an ack for the Handshake packet will be sent.
195 let out = client.process(out, now).dgram();
196 assert!(out.is_some());
197
198 // We do not have PTO for the resent initial packet any more, but
199 // the Handshake PTO timer should be armed. As the RTT is apparently
200 // the same as the initial PTO value, and there is only one sample,
201 // the PTO will be 3x the INITIAL PTO.
202 let delay = client.process_output(now).callback();
203 assert_eq!(delay, INITIAL_PTO * 3);
204 }
205
206 /// A complete handshake that involves two PTOs in the Handshake space.
207 #[test]
208 fn pto_handshake_complete() {
209 const HALF_RTT: Duration = Duration::from_millis(10);
210
211 let mut now = now();
212 // start handshake
213 let mut client = default_client();
214 let mut server = default_server();
215
216 let pkt = client.process_output(now).dgram();
217 let pkt2 = client.process_output(now).dgram();
218 assert_initial(pkt.as_ref().unwrap(), false);
219 assert_initial(pkt2.as_ref().unwrap(), false);
220 let cb = client.process_output(now).callback();
221 assert_eq!(cb, Duration::from_millis(5)); // Pacing delay
222
223 now += HALF_RTT;
224 server.process_input(pkt.unwrap(), now);
225 let pkt = server.process(pkt2, now).dgram();
226 assert_initial(pkt.as_ref().unwrap(), false);
227
228 now += HALF_RTT;
229 let pkt = client.process(pkt, now).dgram();
230 assert_initial(pkt.as_ref().unwrap(), false);
231
232 now += HALF_RTT;
233 let pkt = server.process(pkt, now).dgram();
234 assert_initial(pkt.as_ref().unwrap(), false);
235
236 now += HALF_RTT;
237 let pkt = client.process(pkt, now).dgram();
238 let (initial, handshake) = split_datagram(&pkt.clone().unwrap());
239 assert_initial(&initial, false);
240 assert_handshake(handshake.as_ref().unwrap());
241
242 let cb = client.process_output(now).callback();
243 // The client now has a single RTT estimate (20ms), so
244 // the handshake PTO is set based on that.
245 let pto = HALF_RTT * 6;
246 assert_eq!(cb, pto);
247
248 now += HALF_RTT;
249 let pkt = server.process(pkt, now).dgram();
250 assert!(pkt.is_none());
251
252 now += HALF_RTT;
253 client.authenticated(AuthenticationStatus::Ok, now);
254
255 qdebug!("---- client: SH..FIN -> FIN");
256 let pkt1 = client.process_output(now).dgram();
257 assert_handshake(pkt1.as_ref().unwrap());
258 assert_eq!(*client.state(), State::Connected);
259
260 let cb = client.process_output(now).callback();
261 assert_eq!(cb, pto);
262
263 let mut pto_counts = [0; MAX_PTO_COUNTS];
264 assert_eq!(client.stats.borrow().pto_counts, pto_counts);
265
266 // Wait for PTO to expire and resend a handshake packet.
267 // Wait long enough that the 1-RTT PTO also fires.
268 qdebug!("---- client: PTO");
269 now += pto;
270 let pkt2 = client.process_output(now).dgram();
271 assert_handshake(pkt2.as_ref().unwrap());
272
273 pto_counts[0] = 1;
274 assert_eq!(client.stats.borrow().pto_counts, pto_counts);
275
276 // PTO has been doubled.
277 let pto = 2 * pto;
278 let cb = client.process_output(now).callback();
279 assert_eq!(cb, pto);
280
281 // Get a second PTO packet.
282 // Add some application data to this datagram, then split the 1-RTT off.
283 // We'll use that packet to force the server to acknowledge 1-RTT.
284 let stream_id = client.stream_create(StreamType::UniDi).unwrap();
285 client.stream_close_send(stream_id).unwrap();
286 now += pto;
287 let pkt3 = client.process_output(now).dgram();
288 assert_handshake(pkt3.as_ref().unwrap());
289 let (pkt3_hs, pkt3_1rtt) = split_datagram(&pkt3.unwrap());
290 assert_handshake(&pkt3_hs);
291 assert!(pkt3_1rtt.is_some());
292
293 // PTO has been doubled.
294 let pto = pto * 2;
295 let cb = client.process_output(now).callback();
296 assert_eq!(cb, pto);
297
298 // Not one but two PTOs in a row, thus resetting first bit and increasing
299 // second bit.
300 pto_counts[0] = 0;
301 pto_counts[1] = 1;
302 assert_eq!(client.stats.borrow().pto_counts, pto_counts);
303
304 qdebug!("---- server: receive FIN and send ACK");
305 now += HALF_RTT;
306 // Now let the server have pkt1 and expect an immediate Handshake ACK.
307 // The output will be a Handshake packet with ACK and 1-RTT packet with
308 // HANDSHAKE_DONE and (because of pkt3_1rtt) an ACK.
309 // This should remove the 1-RTT PTO from messing this test up.
310 let server_acks = server.stats().frame_tx.ack;
311 let server_done = server.stats().frame_tx.handshake_done;
312 server.process_input(pkt3_1rtt.unwrap(), now);
313 let ack = server.process(pkt1, now).dgram();
314 assert!(ack.is_some());
315 assert_eq!(server.stats().frame_tx.ack, server_acks + 2);
316 assert_eq!(server.stats().frame_tx.handshake_done, server_done + 1);
317
318 // Check that the other packets (pkt2, pkt3) are Handshake packets.
319 // The server discarded the Handshake keys already, therefore they are dropped.
320 // Note that these don't include 1-RTT packets, because 1-RTT isn't send on PTO.
321 let (pkt2_hs, pkt2_1rtt) = split_datagram(&pkt2.unwrap());
322 assert_handshake(&pkt2_hs);
323 assert!(pkt2_1rtt.is_some());
324 let dropped_before1 = server.stats().dropped_rx;
325 let server_frames = server.stats().frame_rx.all();
326 server.process_input(pkt2_hs, now);
327 assert_eq!(1, server.stats().dropped_rx - dropped_before1);
328 assert_eq!(server.stats().frame_rx.all(), server_frames);
329
330 server.process_input(pkt2_1rtt.unwrap(), now);
331 let server_frames2 = server.stats().frame_rx.all();
332 let dropped_before2 = server.stats().dropped_rx;
333 server.process_input(pkt3_hs, now);
334 assert_eq!(1, server.stats().dropped_rx - dropped_before2);
335 assert_eq!(server.stats().frame_rx.all(), server_frames2);
336
337 now += HALF_RTT;
338
339 // Let the client receive the ACK.
340 // The client now waits in order to delay its ACK of the HANDSHAKE_DONE.
341 let _retransmissions = client.process(ack, now);
342 let cb = client.process_output(now).callback();
343 // The default ack delay is the RTT divided by the default ACK ratio of 4.
344 let expected_ack_delay = HALF_RTT * 2 / 4;
345 assert_eq!(cb, expected_ack_delay);
346
347 // Let the ACK delay timer expire.
348 now += cb;
349 let out = client.process_output(now).dgram();
350 assert!(out.is_some());
351 }
352
353 /// Test that PTO in the Handshake space contains the right frames.
354 #[test]
355 fn pto_handshake_frames() {
356 let mut now = now();
357 qdebug!("---- client: generate CH");
358 let mut client = default_client();
359 let pkt = client.process_output(now);
360 let pkt2 = client.process_output(now);
361
362 now += Duration::from_millis(10);
363 qdebug!("---- server: CH -> SH, EE, CERT, CV, FIN");
364 let mut server = default_server();
365 server.process_input(pkt.dgram().unwrap(), now);
366 let pkt = server.process(pkt2.dgram(), now);
367
368 now += Duration::from_millis(10);
369 qdebug!("---- client: cert verification");
370 let pkt = client.process(pkt.dgram(), now);
371
372 now += Duration::from_millis(10);
373 let pkt = server.process(pkt.dgram(), now);
374 now += Duration::from_millis(10);
375 let pkt = client.process(pkt.dgram(), now);
376
377 now += Duration::from_millis(10);
378 drop(server.process(pkt.dgram(), now));
379
380 now += Duration::from_millis(10);
381 client.authenticated(AuthenticationStatus::Ok, now);
382
383 let stream = client.stream_create(StreamType::UniDi).unwrap();
384 assert_eq!(stream, 2);
385 assert_eq!(client.stream_send(stream, b"zero").unwrap(), 4);
386 qdebug!("---- client: SH..FIN -> FIN and 1RTT packet");
387 let pkt1 = client.process_output(now).dgram();
388 assert!(pkt1.is_some());
389
390 // Get PTO timer.
391 let out = client.process_output(now);
392 assert_eq!(out, Output::Callback(Duration::from_millis(60)));
393
394 // Wait for PTO to expire and resend a handshake packet.
395 now += Duration::from_millis(60);
396 let pkt2 = client.process_output(now).dgram();
397 assert!(pkt2.is_some());
398
399 now += Duration::from_millis(10);
400 let crypto_before = server.stats().frame_rx.crypto;
401 server.process_input(pkt2.unwrap(), now);
402 assert_eq!(server.stats().frame_rx.crypto, crypto_before + 1);
403 }
404
405 #[test]
406 fn pto_retransmits_previous_frames_across_datagrams() {
407 const NUM_PACKETS_BEFORE_PTO: usize = 10;
408
409 let mut client = default_client();
410 let mut server = default_server();
411 connect_force_idle(&mut client, &mut server);
412
413 let mut now = now();
414
415 // Send multiple tiny stream frames, each in separate UDP datagrams.
416 let mut client_stream_frame_tx = client.stats().frame_tx.stream;
417 for _ in 0..NUM_PACKETS_BEFORE_PTO {
418 let stream = client.stream_create(StreamType::UniDi).unwrap();
419 assert_eq!(client.stream_send(stream, b"42").unwrap(), 2);
420
421 let lost = client.process_output(now);
422 assert!(lost.dgram().is_some());
423
424 assert_eq!(client.stats().frame_tx.stream, client_stream_frame_tx + 1);
425 client_stream_frame_tx = client.stats().frame_tx.stream;
426 }
427
428 // Nothing to do, should return callback.
429 let out = client.process_output(now);
430 assert!(matches!(out, Output::Callback(_)));
431
432 // One second later, it should want to send PTO packet.
433 now += AT_LEAST_PTO;
434 let client_pto = client.process_output(now);
435
436 // Expect single `client_pto` datagram to retransmit all stream frames
437 // previously sent in separate datagrams, as each is tiny.
438 let server_stream_frame_rx = server.stats().frame_rx.stream;
439 server.process_input(client_pto.dgram().unwrap(), now);
440 assert_eq!(
441 server.stats().frame_rx.stream,
442 server_stream_frame_rx + NUM_PACKETS_BEFORE_PTO
443 );
444 }
445
446 /// In the case that the Handshake takes too many packets, the server might
447 /// be stalled on the anti-amplification limit. If a Handshake ACK from the
448 /// client is lost, the client has to keep the PTO timer armed or the server
449 /// might be unable to send anything, causing a deadlock.
450 #[test]
451 fn handshake_ack_pto() {
452 const RTT: Duration = Duration::from_millis(10);
453 let mut now = now();
454 // This test makes too many assumptions about single-packet PTOs for multi-packet MLKEM flights
455 // to work.
456 let mut client = new_client(ConnectionParameters::default().mlkem(false));
457 let mut server = default_server();
458 // This is a greasing transport parameter, and large enough that the
459 // server needs to send two Handshake packets.
460 let big = TransportParameter::Bytes(vec![0; Pmtud::default_plpmtu(DEFAULT_ADDR.ip())]);
461 server
462 .set_local_tparam(TestTransportParameter, big)
463 .unwrap();
464
465 let c1 = client.process_output(now).dgram();
466
467 now += RTT / 2;
468 let s1 = server.process(c1, now).dgram();
469 assert!(s1.is_some());
470 let s2 = server.process_output(now).dgram();
471 assert!(s1.is_some());
472
473 // Now let the client have the Initial, but drop the first coalesced Handshake packet.
474 now += RTT / 2;
475 let (initial, _) = split_datagram(&s1.unwrap());
476 client.process_input(initial, now);
477 let c2 = client.process(s2, now).dgram();
478 assert!(c2.is_some()); // This is an ACK. Drop it.
479 let delay = client.process_output(now).callback();
480 assert_eq!(delay, RTT * 3);
481
482 let mut pto_counts = [0; MAX_PTO_COUNTS];
483 assert_eq!(client.stats.borrow().pto_counts, pto_counts);
484
485 // Wait for the PTO and ensure that the client generates a packet.
486 now += delay;
487 let c3 = client.process_output(now).dgram();
488 assert!(c3.is_some());
489
490 now += RTT / 2;
491 let ping_before = server.stats().frame_rx.ping;
492 server.process_input(c3.unwrap(), now);
493 assert_eq!(server.stats().frame_rx.ping, ping_before + 1);
494
495 pto_counts[0] = 1;
496 assert_eq!(client.stats.borrow().pto_counts, pto_counts);
497
498 // Now complete the handshake as cheaply as possible.
499 let dgram = server.process_output(now).dgram();
500 client.process_input(dgram.unwrap(), now);
501 maybe_authenticate(&mut client);
502 let dgram = client.process_output(now).dgram();
503 assert_eq!(*client.state(), State::Connected);
504 let dgram = server.process(dgram, now).dgram();
505 assert_eq!(*server.state(), State::Confirmed);
506 client.process_input(dgram.unwrap(), now);
507 assert_eq!(*client.state(), State::Confirmed);
508
509 assert_eq!(client.stats.borrow().pto_counts, pto_counts);
510 }
511
512 #[test]
513 fn loss_recovery_crash() {
514 let mut client = default_client();
515 let mut server = default_server();
516 connect(&mut client, &mut server);
517 let now = now();
518
519 // The server sends something, but we will drop this.
520 drop(send_something(&mut server, now));
521
522 // Then send something again, but let it through.
523 let ack = send_and_receive(&mut server, &mut client, now);
524 assert!(ack.is_some());
525
526 // Have the server process the ACK.
527 let cb = server.process(ack, now).callback();
528 assert!(cb > Duration::from_secs(0));
529
530 // Now we leap into the future. The server should regard the first
531 // packet as lost based on time alone.
532 let dgram = server.process_output(now + AT_LEAST_PTO).dgram();
533 assert!(dgram.is_some());
534
535 // This crashes.
536 drop(send_something(&mut server, now + AT_LEAST_PTO));
537 }
538
539 // If we receive packets after the PTO timer has fired, we won't clear
540 // the PTO state, but we might need to acknowledge those packets.
541 // This shouldn't happen, but we found that some implementations do this.
542 #[test]
543 fn ack_after_pto() {
544 let mut client = default_client();
545 let mut server = default_server();
546 connect_force_idle(&mut client, &mut server);
547
548 let mut now = now();
549
550 // The client sends and is forced into a PTO.
551 drop(send_something(&mut client, now));
552
553 // Jump forward to the PTO and drain the PTO packets.
554 now += AT_LEAST_PTO;
555 // We can use MAX_PTO_PACKET_COUNT, because we know the handshake is over.
556 for _ in 0..MAX_PTO_PACKET_COUNT {
557 let dgram = client.process_output(now).dgram();
558 assert!(dgram.is_some());
559 }
560 assert!(client.process_output(now).dgram().is_none());
561
562 // The server now needs to send something that will cause the
563 // client to want to acknowledge it. A little out of order
564 // delivery is just the thing.
565 // Note: The server can't ACK anything here, but none of what
566 // the client has sent so far has been transferred.
567 drop(send_something(&mut server, now));
568 let dgram = send_something(&mut server, now);
569
570 // The client is now after a PTO, but if it receives something
571 // that demands acknowledgment, it will send just the ACK.
572 let ack = client.process(Some(dgram), now).dgram();
573 assert!(ack.is_some());
574
575 // Make sure that the packet only contained an ACK frame.
576 let all_frames_before = server.stats().frame_rx.all();
577 let ack_before = server.stats().frame_rx.ack;
578 server.process_input(ack.unwrap(), now);
579 assert_eq!(server.stats().frame_rx.all(), all_frames_before + 1);
580 assert_eq!(server.stats().frame_rx.ack, ack_before + 1);
581 }
582
583 /// When we declare a packet as lost, we keep it around for a while for another loss period.
584 /// Those packets should not affect how we report the loss recovery timer.
585 /// As the loss recovery timer based on RTT we use that to drive the state.
586 #[test]
587 fn lost_but_kept_and_lr_timer() {
588 const RTT: Duration = Duration::from_secs(1);
589 let mut client = default_client();
590 let mut server = default_server();
591 let mut now = connect_with_rtt(&mut client, &mut server, now(), RTT);
592
593 // Two packets (p1, p2) are sent at around t=0. The first is lost.
594 let _p1 = send_something(&mut client, now);
595 let p2 = send_something(&mut client, now);
596
597 // At t=RTT/2 the server receives the packet and ACKs it.
598 now += RTT / 2;
599 let ack = server.process(Some(p2), now).dgram();
600 assert!(ack.is_some());
601 // The client also sends another two packets (p3, p4), again losing the first.
602 let _p3 = send_something(&mut client, now);
603 let p4 = send_something(&mut client, now);
604
605 // At t=RTT the client receives the ACK and goes into timed loss recovery.
606 // The client doesn't call p1 lost at this stage, but it will soon.
607 now += RTT / 2;
608 let res = client.process(ack, now);
609 // The client should be on a loss recovery timer as p1 is missing.
610 let lr_timer = res.callback();
611 // Loss recovery timer should be RTT/8, but only check for 0 or >=RTT/2.
612 assert_ne!(lr_timer, Duration::from_secs(0));
613 assert!(lr_timer < (RTT / 2));
614 // The server also receives and acknowledges p4, again sending an ACK.
615 let ack = server.process(Some(p4), now).dgram();
616 assert!(ack.is_some());
617
618 // At t=RTT*3/2 the client should declare p1 to be lost.
619 now += RTT / 2;
620 // So the client will send the data from p1 again.
621 let res = client.process_output(now);
622 assert!(res.dgram().is_some());
623 // When the client processes the ACK, it should engage the
624 // loss recovery timer for p3, not p1 (even though it still tracks p1).
625 let res = client.process(ack, now);
626 let lr_timer2 = res.callback();
627 assert_eq!(lr_timer, lr_timer2);
628 }
629
630 /// We should not be setting the loss recovery timer based on packets
631 /// that are sent prior to the largest acknowledged.
632 /// Testing this requires that we construct a case where one packet
633 /// number space causes the loss recovery timer to be engaged. At the same time,
634 /// there is a packet in another space that hasn't been acknowledged AND
635 /// that packet number space has not received acknowledgments for later packets.
636 #[test]
637 fn loss_time_past_largest_acked() {
638 const RTT: Duration = Duration::from_secs(10);
639 const INCR: Duration = Duration::from_millis(1);
640 // This test makes too many assumptions about single-packet PTOs for multi-packet MLKEM flights
641 // to work.
642 let mut client = new_client(ConnectionParameters::default().mlkem(false));
643 let mut server = default_server();
644
645 let mut now = now();
646
647 // Start the handshake.
648 let c_in = client.process_output(now).dgram();
649 now += RTT / 2;
650 let s_hs1 = server.process(c_in, now).dgram();
651
652 // Get some spare server handshake packets for the client to ACK.
653 // This involves a time machine, so be a little cautious.
654 // This test uses an RTT of 10s, but our server starts
655 // with a much lower RTT estimate, so the PTO at this point should
656 // be much smaller than an RTT and so the server shouldn't see
657 // time go backwards.
658 let s_pto = server.process_output(now).callback();
659 assert_ne!(s_pto, Duration::from_secs(0));
660 assert!(s_pto < RTT);
661 let s_hs2 = server.process_output(now + s_pto).dgram();
662 assert!(s_hs2.is_some());
663 let s_pto = server.process_output(now).callback();
664 assert_ne!(s_pto, Duration::from_secs(0));
665 assert!(s_pto < RTT);
666 let s_hs3 = server.process_output(now + s_pto).dgram();
667 assert!(s_hs3.is_some());
668
669 // We are blocked by the amplification limit now.
670 let cb = server.process_output(now).callback();
671 assert_eq!(cb, server.conn_params.get_idle_timeout());
672
673 // Get some Handshake packets from the client.
674 // We need one to be left unacknowledged before one that is acknowledged.
675 // So that the client engages the loss recovery timer.
676 // This is complicated by the fact that it is hard to cause the client
677 // to generate an ack-eliciting packet. For that, we use the Finished message.
678 // Reordering delivery ensures that the later packet is also acknowledged.
679 now += RTT / 2;
680 let c_hs1 = client.process(s_hs1, now).dgram();
681 assert!(c_hs1.is_some()); // This comes first, so it's useless.
682 maybe_authenticate(&mut client);
683 let c_hs2 = client.process_output(now).dgram();
684 assert!(c_hs2.is_some()); // This one will elicit an ACK.
685
686 // The we need the outstanding packet to be sent after the
687 // application data packet, so space these out a tiny bit.
688 let _p1 = send_something(&mut client, now + INCR);
689 let c_hs3 = client.process(s_hs2, now + (INCR * 2)).dgram();
690 assert!(c_hs3.is_some()); // This will be left outstanding.
691 let c_hs4 = client.process(s_hs3, now + (INCR * 3)).dgram();
692 assert!(c_hs4.is_some()); // This will be acknowledged.
693
694 // Process c_hs2 and c_hs4, but skip c_hs3.
695 // Then get an ACK for the client.
696 now += RTT / 2;
697 // Deliver c_hs4 first, but don't generate a packet.
698 server.process_input(c_hs4.unwrap(), now);
699 let s_ack = server.process(c_hs2, now).dgram();
700 assert!(s_ack.is_some());
701 // This includes an ACK, but it also includes HANDSHAKE_DONE,
702 // which we need to remove because that will cause the Handshake loss
703 // recovery state to be dropped.
704 let (s_hs_ack, _s_ap_ack) = split_datagram(&s_ack.unwrap());
705
706 // Now the client should start its loss recovery timer based on the ACK.
707 now += RTT / 2;
708 let _c_ack = client.process(Some(s_hs_ack), now).dgram();
709 // This ACK triggers an immediate ACK, due to an ACK loss during handshake.
710 let c_ack = client.process_output(now).dgram();
711 assert!(c_ack.is_none());
712 // The client should now have the loss recovery timer active.
713 let lr_time = client.process_output(now).callback();
714 assert_ne!(lr_time, Duration::from_secs(0));
715 assert!(lr_time < (RTT / 2));
716 }
717
718 /// `sender` sends a little, `receiver` acknowledges it.
719 /// Repeat until `count` acknowledgements are sent.
720 /// Returns the last packet containing acknowledgements, if any.
721 fn trickle(sender: &mut Connection, receiver: &mut Connection, mut count: usize, now: Instant) {
722 let id = sender.stream_create(StreamType::UniDi).unwrap();
723 let mut maybe_ack = None;
724 while count > 0 {
725 qdebug!("trickle: remaining={count}");
726 assert_eq!(sender.stream_send(id, &[9]).unwrap(), 1);
727 let dgram = sender.process(maybe_ack, now).dgram();
728
729 maybe_ack = receiver.process(dgram, now).dgram();
730 count -= usize::from(maybe_ack.is_some());
731 }
732 sender.process_input(maybe_ack.unwrap(), now);
733 }
734
735 /// Ensure that a PING frame is sent with ACK sometimes.
736 /// `fast` allows testing of when `MAX_OUTSTANDING_UNACK` packets are
737 /// outstanding (`fast` is `true`) within 1 PTO and when only
738 /// `MIN_OUTSTANDING_UNACK` packets arrive after 2 PTOs (`fast` is `false`).
739 fn ping_with_ack(fast: bool) {
740 let mut sender = default_client();
741 let mut receiver = default_server();
742 let mut now = now();
743 connect_force_idle(&mut sender, &mut receiver);
744 let sender_acks_before = sender.stats().frame_tx.ack;
745 let receiver_acks_before = receiver.stats().frame_tx.ack;
746 let count = if fast {
747 MAX_OUTSTANDING_UNACK
748 } else {
749 MIN_OUTSTANDING_UNACK
750 };
751 trickle(&mut sender, &mut receiver, count, now);
752 assert_eq!(sender.stats().frame_tx.ack, sender_acks_before);
753 assert_eq!(receiver.stats().frame_tx.ack, receiver_acks_before + count);
754 assert_eq!(receiver.stats().frame_tx.ping, 0);
755
756 if !fast {
757 // Wait at least one PTO, from the reciever's perspective.
758 // A receiver that hasn't received MAX_OUTSTANDING_UNACK won't send PING.
759 now += receiver.pto() + Duration::from_micros(1);
760 trickle(&mut sender, &mut receiver, 1, now);
761 assert_eq!(receiver.stats().frame_tx.ping, 0);
762 }
763
764 // After a second PTO (or the first if fast), new acknowledgements come
765 // with a PING frame and cause an ACK to be sent by the sender.
766 now += receiver.pto() + Duration::from_micros(1);
767 trickle(&mut sender, &mut receiver, 1, now);
768 assert_eq!(receiver.stats().frame_tx.ping, 1);
769 if let Output::Callback(t) = sender.process_output(now) {
770 assert_eq!(t, DEFAULT_LOCAL_ACK_DELAY);
771 assert!(sender.process_output(now + t).dgram().is_some());
772 }
773 assert_eq!(sender.stats().frame_tx.ack, sender_acks_before + 1);
774 }
775
776 #[test]
777 fn ping_with_ack_fast() {
778 ping_with_ack(true);
779 }
780
781 #[test]
782 fn ping_with_ack_slow() {
783 ping_with_ack(false);
784 }
785
786 #[test]
787 fn ping_with_ack_min() {
788 const COUNT: usize = MIN_OUTSTANDING_UNACK - 2;
789 let mut sender = default_client();
790 let mut receiver = default_server();
791 let mut now = now();
792 connect_force_idle(&mut sender, &mut receiver);
793 let sender_acks_before = sender.stats().frame_tx.ack;
794 let receiver_acks_before = receiver.stats().frame_tx.ack;
795 trickle(&mut sender, &mut receiver, COUNT, now);
796 assert_eq!(sender.stats().frame_tx.ack, sender_acks_before);
797 assert_eq!(receiver.stats().frame_tx.ack, receiver_acks_before + COUNT);
798 assert_eq!(receiver.stats().frame_tx.ping, 0);
799
800 // After 3 PTO, no PING because there are too few outstanding packets.
801 now += receiver.pto() * 3 + Duration::from_micros(1);
802 trickle(&mut sender, &mut receiver, 1, now);
803 assert_eq!(receiver.stats().frame_tx.ping, 0);
804 }
805
806 /// This calculates the PTO timer immediately after connection establishment.
807 /// It depends on there only being 2 RTT samples in the handshake.
808 fn expected_pto(rtt: Duration) -> Duration {
809 // PTO calculation is rtt + 4rttvar + ack delay.
810 // rttvar should be (rtt + 4 * (rtt / 2) * (3/4)^n + 25ms)/2
811 // where n is the number of round trips
812 // This uses the default maximum ACK delay (25ms) as the ACK delay extension
813 // is negotiated and no ACK_DELAY frame has been received.
814 rtt + rtt * 9 / 8 + DEFAULT_REMOTE_ACK_DELAY
815 }
816
817 #[test]
818 fn fast_pto() {
819 // This test makes too many assumptions about single-packet PTOs for multi-packet MLKEM flights
820 // to work.
821 let mut client = new_client(
822 ConnectionParameters::default()
823 .fast_pto(FAST_PTO_SCALE / 2)
824 .mlkem(false),
825 );
826 let mut server = default_server();
827 let mut now = connect_rtt_idle(&mut client, &mut server, DEFAULT_RTT);
828
829 let res = client.process_output(now);
830 let idle_timeout = ConnectionParameters::default().get_idle_timeout() - (DEFAULT_RTT / 2);
831 assert_eq!(res, Output::Callback(idle_timeout));
832
833 // Send data on two streams
834 let stream = client.stream_create(StreamType::UniDi).unwrap();
835 assert_eq!(
836 client.stream_send(stream, DEFAULT_STREAM_DATA).unwrap(),
837 DEFAULT_STREAM_DATA.len()
838 );
839
840 // Send a packet after some time.
841 now += idle_timeout / 2;
842 let dgram = client.process_output(now).dgram();
843 assert!(dgram.is_some());
844
845 // Nothing to do, should return a callback.
846 let cb = client.process_output(now).callback();
847 assert_eq!(expected_pto(DEFAULT_RTT) / 2, cb);
848
849 // Once the PTO timer expires, a PTO packet should be sent should want to send PTO packet.
850 now += cb;
851 let dgram = client.process_output(now).dgram();
852
853 let stream_before = server.stats().frame_rx.stream;
854 server.process_input(dgram.unwrap(), now);
855 assert_eq!(server.stats().frame_rx.stream, stream_before + 1);
856 }
857
858 /// Even if the PTO timer is slowed right down, persistent congestion is declared
859 /// based on the "true" value of the timer.
860 #[test]
861 fn fast_pto_persistent_congestion() {
862 // This test makes too many assumptions about single-packet PTOs for multi-packet MLKEM flights
863 // to work.
864 let mut client = new_client(
865 ConnectionParameters::default()
866 .fast_pto(FAST_PTO_SCALE * 2)
867 .mlkem(false),
868 );
869 let mut server = default_server();
870 let mut now = connect_rtt_idle(&mut client, &mut server, DEFAULT_RTT);
871
872 let res = client.process_output(now);
873 let idle_timeout = ConnectionParameters::default().get_idle_timeout() - (DEFAULT_RTT / 2);
874 assert_eq!(res, Output::Callback(idle_timeout));
875
876 // Send packets spaced by the PTO timer. And lose them.
877 // Note: This timing is a tiny bit higher than the client will use
878 // to determine persistent congestion. The ACK below adds another RTT
879 // estimate, which will reduce rttvar by 3/4, so persistent congestion
880 // will occur at `rtt + rtt*27/32 + 25ms`.
881 // That is OK as we're still showing that this interval is less than
882 // six times the PTO, which is what would be used if the scaling
883 // applied to the PTO used to determine persistent congestion.
884 let pc_interval = expected_pto(DEFAULT_RTT) * 3;
885 println!("pc_interval {pc_interval:?}");
886 let _drop1 = send_something(&mut client, now);
887
888 // Check that the PTO matches expectations.
889 let cb = client.process_output(now).callback();
890 assert_eq!(expected_pto(DEFAULT_RTT) * 2, cb);
891
892 now += pc_interval;
893 let _drop2 = send_something(&mut client, now);
894 let _drop3 = send_something(&mut client, now);
895 let _drop4 = send_something(&mut client, now);
896 let dgram = send_something(&mut client, now);
897
898 // Now acknowledge the tail packet and enter persistent congestion.
899 now += DEFAULT_RTT / 2;
900 let ack = server.process(Some(dgram), now).dgram();
901 now += DEFAULT_RTT / 2;
902 client.process_input(ack.unwrap(), now);
903 assert_eq!(cwnd(&client), cwnd_min(&client));
904 }
905
906 /// Receiving an ACK frame for a packet number that was never sent is an error.
907 #[test]
908 fn ack_for_unsent() {
909 /// This inserts an ACK frame into packets that ACKs a packet that was never sent.
910 struct AckforUnsentWriter {}
911
912 impl FrameWriter for AckforUnsentWriter {
913 fn write_frames(&mut self, builder: &mut PacketBuilder) {
914 builder.encode_varint(FrameType::Ack);
915 builder.encode_varint(666u16); // Largest ACKed
916 builder.encode_varint(0u8); // ACK delay
917 builder.encode_varint(0u8); // ACK block count
918 builder.encode_varint(0u8); // ACK block length
919 }
920 }
921
922 let mut client = default_client();
923 let mut server = default_server();
924 connect_force_idle(&mut client, &mut server);
925
926 let spoofed = server
927 .test_write_frames(AckforUnsentWriter {}, now())
928 .dgram()
929 .unwrap();
930
931 // Now deliver the packet with the spoofed ACK frame
932 client.process_input(spoofed, now());
933 assert!(matches!(
934 client.state(),
935 State::Closing {
936 error: CloseReason::Transport(Error::AckedUnsentPacket),
937 ..
938 }
939 ));
940 }