Debian Codegraph @

third_party/rust/neqo-transport/src/connection/tests/ecn.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;
8
9 use neqo_common::{Datagram, IpTos, IpTosEcn};
10 use strum::IntoEnumIterator as _;
11 use test_fixture::{
12 assertions::{assert_v4_path, assert_v6_path},
13 fixture_init, now, DEFAULT_ADDR_V4,
14 };
15
16 use crate::{
17 connection::tests::{
18 assert_path_challenge_min_len, connect_force_idle, connect_force_idle_with_modifier,
19 default_client, default_server, handshake_with_modifier, migration::get_cid, new_client,
20 new_server, send_and_receive, send_something, send_something_with_modifier,
21 send_with_modifier_and_receive, DEFAULT_RTT,
22 },
23 ecn,
24 packet::PacketType,
25 path::MAX_PATH_PROBES,
26 ConnectionId, ConnectionParameters, StreamType,
27 };
28
29 fn assert_ecn_enabled(tos: IpTos) {
30 assert!(tos.is_ecn_marked());
31 }
32
33 fn assert_ecn_disabled(tos: IpTos) {
34 assert!(!tos.is_ecn_marked());
35 }
36
37 fn set_tos(mut d: Datagram, ecn: IpTosEcn) -> Datagram {
38 d.set_tos(ecn.into());
39 d
40 }
41
42 fn noop() -> fn(Datagram) -> Option<Datagram> {
43 Some
44 }
45
46 fn bleach() -> fn(Datagram) -> Option<Datagram> {
47 |d| Some(set_tos(d, IpTosEcn::NotEct))
48 }
49
50 fn remark() -> fn(Datagram) -> Option<Datagram> {
51 |d| {
52 if d.tos().is_ecn_marked() {
53 Some(set_tos(d, IpTosEcn::Ect1))
54 } else {
55 Some(d)
56 }
57 }
58 }
59
60 fn ce() -> fn(Datagram) -> Option<Datagram> {
61 |d| {
62 if d.tos().is_ecn_marked() {
63 Some(set_tos(d, IpTosEcn::Ce))
64 } else {
65 Some(d)
66 }
67 }
68 }
69
70 fn drop() -> fn(Datagram) -> Option<Datagram> {
71 |_| None
72 }
73
74 fn drop_ecn_marked_datagrams() -> fn(Datagram) -> Option<Datagram> {
75 |d| (!d.tos().is_ecn_marked()).then_some(d)
76 }
77
78 #[test]
79 fn handshake_delay_with_ecn_blackhole() {
80 let start = now();
81 // `handshake_with_modifier` with-multi packet Intial flights will throw off the RTT calculation
82 // below.
83 let mut client = new_client(ConnectionParameters::default().mlkem(false));
84 let mut server = default_server();
85 let finish = handshake_with_modifier(
86 &mut client,
87 &mut server,
88 start,
89 DEFAULT_RTT,
90 drop_ecn_marked_datagrams(),
91 );
92
93 assert!(client.state().connected());
94 assert!(server.state().connected());
95
96 assert_eq!(
97 (finish - start).as_millis() / DEFAULT_RTT.as_millis(),
98 45,
99 "expected 3 RTT for first client PTO + 6 RTT for second PTO + 12 RTT for third PTO + another 21 RTT for the same on the server side + 3 RTT for handshake to be confirmed",
100 );
101 }
102
103 #[test]
104 fn migration_delay_to_ecn_blackhole() {
105 let mut now = now();
106 let mut client = default_client();
107 let mut server = default_server();
108
109 // Do a handshake.
110 connect_force_idle(&mut client, &mut server);
111
112 // Migrate the client.
113 client
114 .migrate(Some(DEFAULT_ADDR_V4), Some(DEFAULT_ADDR_V4), false, now)
115 .unwrap();
116
117 // The client should send MAX_PATH_PROBES path challenges with ECN enabled, and then another
118 // MAX_PATH_PROBES without ECN.
119 let mut probes = 0;
120 while probes < MAX_PATH_PROBES * 2 {
121 match client.process_output(now) {
122 crate::Output::Callback(t) => {
123 now += t;
124 }
125 crate::Output::Datagram(d) => {
126 // The new path is IPv4.
127 if d.source().is_ipv4() {
128 // This should be a PATH_CHALLENGE.
129 probes += 1;
130 assert_eq!(client.stats().frame_tx.path_challenge, probes);
131 assert_path_challenge_min_len(&client, &d, now);
132 if probes <= MAX_PATH_PROBES {
133 // The first probes should be sent with ECN.
134 assert_ecn_enabled(d.tos());
135 } else {
136 // The next probes should be sent without ECN.
137 assert_ecn_disabled(d.tos());
138 }
139 }
140 }
141 crate::Output::None => panic!("unexpected output"),
142 }
143 }
144 }
145
146 #[test]
147 fn debug() {
148 let stats = crate::Stats::default();
149 assert_eq!(
150 format!("{stats:?}"),
151 "stats for\u{0020}
152 rx: 0 drop 0 dup 0 saved 0
153 tx: 0 lost 0 lateack 0 ptoack 0
154 pmtud: 0 sent 0 acked 0 lost 0 change 0 iface_mtu 0 pmtu
155 resumed: false
156 frames rx:
157 crypto 0 done 0 token 0 close 0
158 ack 0 (max 0) ping 0 padding 0
159 stream 0 reset 0 stop 0
160 max: stream 0 data 0 stream_data 0
161 blocked: stream 0 data 0 stream_data 0
162 datagram 0
163 ncid 0 rcid 0 pchallenge 0 presponse 0
164 ack_frequency 0
165 frames tx:
166 crypto 0 done 0 token 0 close 0
167 ack 0 (max 0) ping 0 padding 0
168 stream 0 reset 0 stop 0
169 max: stream 0 data 0 stream_data 0
170 blocked: stream 0 data 0 stream_data 0
171 datagram 0
172 ncid 0 rcid 0 pchallenge 0 presponse 0
173 ack_frequency 0
174 ecn:
175 tx:
176 acked:
177 rx:
178 path validation outcomes: ValidationCount({Capable: 0, NotCapable(BlackHole): 0, NotCapable(Bleaching): 0, NotCapable(ReceivedUnsentECT1): 0})
179 mark transitions:
180 dscp: \n"
181 );
182 }
183
184 #[test]
185 fn stats() {
186 let now = now();
187 let mut client = default_client();
188 let mut server = default_server();
189 connect_force_idle(&mut client, &mut server);
190
191 for _ in 0..ecn::TEST_COUNT {
192 let ack = send_and_receive(&mut client, &mut server, now);
193 client.process_input(ack.unwrap(), now);
194 }
195
196 for _ in 0..ecn::TEST_COUNT {
197 let ack = send_and_receive(&mut server, &mut client, now);
198 server.process_input(ack.unwrap(), now);
199 }
200
201 for stats in [client.stats(), server.stats()] {
202 for (outcome, count) in stats.ecn_path_validation.iter() {
203 match outcome {
204 ecn::ValidationOutcome::Capable => assert_eq!(*count, 1),
205 ecn::ValidationOutcome::NotCapable(_) => assert_eq!(*count, 0),
206 }
207 }
208
209 for packet_type in PacketType::iter() {
210 for codepoint in [IpTosEcn::Ect1, IpTosEcn::Ce] {
211 assert_eq!(stats.ecn_tx[packet_type][codepoint], 0);
212 assert_eq!(stats.ecn_tx_acked[packet_type][codepoint], 0);
213 assert_eq!(stats.ecn_rx[packet_type][codepoint], 0);
214 }
215 }
216 }
217
218 for packet_type in PacketType::iter() {
219 assert!(
220 client.stats().ecn_tx_acked[packet_type][IpTosEcn::Ect0]
221 <= server.stats().ecn_rx[packet_type][IpTosEcn::Ect0]
222 );
223 assert!(
224 server.stats().ecn_tx_acked[packet_type][IpTosEcn::Ect0]
225 <= client.stats().ecn_rx[packet_type][IpTosEcn::Ect0]
226 );
227 assert_eq!(
228 client.stats().ecn_tx[packet_type][IpTosEcn::Ect0],
229 server.stats().ecn_rx[packet_type][IpTosEcn::Ect0]
230 );
231 assert_eq!(
232 server.stats().ecn_tx[packet_type][IpTosEcn::Ect0],
233 client.stats().ecn_rx[packet_type][IpTosEcn::Ect0]
234 );
235 }
236 }
237
238 #[test]
239 fn disables_on_loss() {
240 let now = now();
241 let mut client = default_client();
242 let mut server = default_server();
243 connect_force_idle(&mut client, &mut server);
244
245 // Right after the handshake, the ECN validation should still be in progress.
246 let client_pkt = send_something(&mut client, now);
247 assert_ecn_enabled(client_pkt.tos());
248
249 for _ in 0..ecn::TEST_COUNT {
250 send_something(&mut client, now);
251 }
252
253 // ECN should now be disabled.
254 let client_pkt = send_something(&mut client, now);
255 assert_ecn_disabled(client_pkt.tos());
256 }
257
258 #[test]
259 fn disables_on_remark() {
260 let now = now();
261 let mut client = default_client();
262 let mut server = default_server();
263 connect_force_idle(&mut client, &mut server);
264
265 for _ in 0..ecn::TEST_COUNT {
266 if let Some(ack) = send_with_modifier_and_receive(&mut client, &mut server, now, remark()) {
267 client.process_input(ack, now);
268 }
269 }
270
271 // ECN should now be disabled.
272 let client_pkt = send_something(&mut client, now);
273 assert_ecn_disabled(client_pkt.tos());
274 }
275
276 /// This function performs a handshake over a path that modifies packets via `orig_path_modifier`.
277 /// It then sends `burst` packets on that path, and then migrates to a new path that
278 /// modifies packets via `new_path_modifier`. It sends `burst` packets on the new path.
279 /// The function returns the TOS value of the last packet sent on the old path and the TOS value
280 /// of the last packet sent on the new path to allow for verification of correct behavior.
281 pub fn migration_with_modifiers(
282 orig_path_modifier: fn(Datagram) -> Option<Datagram>,
283 new_path_modifier: fn(Datagram) -> Option<Datagram>,
284 burst: usize,
285 ) -> (IpTos, IpTos, bool) {
286 fixture_init();
287 let mut client = new_client(ConnectionParameters::default().max_streams(StreamType::UniDi, 64));
288 let mut server = new_server(ConnectionParameters::default().max_streams(StreamType::UniDi, 64));
289
290 connect_force_idle_with_modifier(&mut client, &mut server, orig_path_modifier);
291 let mut now = now();
292
293 // Right after the handshake, the ECN validation should still be in progress.
294 let client_pkt = send_something(&mut client, now);
295 assert_ecn_enabled(client_pkt.tos());
296 server.process_input(orig_path_modifier(client_pkt).unwrap(), now);
297
298 // Send some data on the current path.
299 for _ in 0..burst {
300 let client_pkt = send_something_with_modifier(&mut client, now, orig_path_modifier);
301 server.process_input(client_pkt, now);
302 }
303
304 if let Some(ack) = server.process_output(now).dgram() {
305 client.process_input(ack, now);
306 }
307
308 let client_pkt = send_something(&mut client, now);
309 let tos_before_migration = client_pkt.tos();
310 server.process_input(orig_path_modifier(client_pkt).unwrap(), now);
311
312 client
313 .migrate(Some(DEFAULT_ADDR_V4), Some(DEFAULT_ADDR_V4), false, now)
314 .unwrap();
315
316 let mut migrated = false;
317 let probe = new_path_modifier(client.process_output(now).dgram().unwrap());
318 if let Some(probe) = probe {
319 assert_v4_path(&probe, true); // Contains PATH_CHALLENGE.
320 assert_path_challenge_min_len(&client, &probe, now);
321 assert_eq!(client.stats().frame_tx.path_challenge, 1);
322 let probe_cid = ConnectionId::from(get_cid(&probe));
323
324 let resp = new_path_modifier(server.process(Some(probe), now).dgram().unwrap()).unwrap();
325 assert_v4_path(&resp, true);
326 assert_eq!(server.stats().frame_tx.path_response, 1);
327 assert_eq!(server.stats().frame_tx.path_challenge, 1);
328 assert_path_challenge_min_len(&server, &resp, now);
329
330 // Data continues to be exchanged on the old path.
331 let client_data = send_something_with_modifier(&mut client, now, orig_path_modifier);
332 assert_ne!(get_cid(&client_data), probe_cid);
333 assert_v6_path(&client_data, false);
334 server.process_input(client_data, now);
335 let server_data = send_something_with_modifier(&mut server, now, orig_path_modifier);
336 assert_v6_path(&server_data, false);
337 client.process_input(server_data, now);
338
339 // Once the client receives the probe response, it migrates to the new path.
340 client.process_input(resp, now);
341 assert_eq!(client.stats().frame_rx.path_challenge, 1);
342 migrated = true;
343
344 let migrate_client = send_something_with_modifier(&mut client, now, new_path_modifier);
345 assert_v4_path(&migrate_client, true); // Responds to server probe.
346
347 // The server now sees the migration and will switch over.
348 // However, it will probe the old path again, even though it has just
349 // received a response to its last probe, because it needs to verify
350 // that the migration is genuine.
351 server.process_input(migrate_client, now);
352 }
353
354 let stream_before = server.stats().frame_tx.stream;
355 let probe_old_server = send_something_with_modifier(&mut server, now, orig_path_modifier);
356 // This is just the double-check probe; no STREAM frames.
357 assert_v6_path(&probe_old_server, migrated);
358 assert_eq!(
359 server.stats().frame_tx.path_challenge,
360 if migrated { 2 } else { 0 }
361 );
362 if migrated {
363 assert_path_challenge_min_len(&server, &probe_old_server, now);
364 }
365
366 assert_eq!(
367 server.stats().frame_tx.stream,
368 if migrated { stream_before } else { 1 }
369 );
370
371 if migrated {
372 // The server then sends data on the new path.
373 let migrate_server =
374 new_path_modifier(server.process_output(now).dgram().unwrap()).unwrap();
375 assert_v4_path(&migrate_server, false);
376 assert_eq!(server.stats().frame_tx.path_challenge, 2);
377 assert_eq!(server.stats().frame_tx.stream, stream_before + 1);
378
379 // The client receives these checks and responds to the probe, but uses the new path.
380 client.process_input(migrate_server, now);
381 client.process_input(probe_old_server, now);
382 let old_probe_resp = send_something_with_modifier(&mut client, now, new_path_modifier);
383 assert_v6_path(&old_probe_resp, true);
384 let client_confirmation = client.process_output(now).dgram().unwrap();
385 assert_v4_path(&client_confirmation, false);
386
387 // The server has now sent 2 packets, so it is blocked on the pacer. Wait.
388 let server_pacing = server.process_output(now).callback();
389 assert_ne!(server_pacing, Duration::new(0, 0));
390 // ... then confirm that the server sends on the new path still.
391 let server_confirmation =
392 send_something_with_modifier(&mut server, now + server_pacing, new_path_modifier);
393 assert_v4_path(&server_confirmation, false);
394 client.process_input(server_confirmation, now);
395
396 // Send some data on the new path.
397 for _ in 0..burst {
398 now += client.process_output(now).callback();
399 let client_pkt = send_something_with_modifier(&mut client, now, new_path_modifier);
400 server.process_input(client_pkt, now);
401 }
402
403 if let Some(ack) = server.process_output(now).dgram() {
404 client.process_input(ack, now);
405 }
406 }
407
408 now += client.process_output(now).callback();
409 let mut client_pkt = send_something(&mut client, now);
410 while !migrated && client_pkt.source() == DEFAULT_ADDR_V4 {
411 client_pkt = send_something(&mut client, now);
412 }
413 let tos_after_migration = client_pkt.tos();
414 (tos_before_migration, tos_after_migration, migrated)
415 }
416
417 #[test]
418 fn ecn_migration_zero_burst_all_cases() {
419 for orig_path_mod in [noop(), bleach(), remark(), ce()] {
420 for (new_path_mod_name, new_path_mod) in [
421 ("noop", noop()),
422 ("bleach", bleach()),
423 ("remark", remark()),
424 ("ce", ce()),
425 ("drop", drop()),
426 ] {
427 let (before, after, migrated) =
428 migration_with_modifiers(orig_path_mod, new_path_mod, 0);
429 // Too few packets sent before and after migration to conclude ECN validation.
430 assert_ecn_enabled(before);
431 assert_ecn_enabled(after);
432 // Migration succeeds except if the new path drops ECN.
433 assert!(new_path_mod_name == "drop" || migrated);
434 }
435 }
436 }
437
438 #[test]
439 fn ecn_migration_noop_bleach_data() {
440 let (before, after, migrated) = migration_with_modifiers(noop(), bleach(), ecn::TEST_COUNT);
441 assert_ecn_enabled(before); // ECN validation concludes before migration.
442 assert_ecn_disabled(after); // ECN validation fails after migration due to bleaching.
443 assert!(migrated);
444 }
445
446 #[test]
447 fn ecn_migration_noop_remark_data() {
448 let (before, after, migrated) = migration_with_modifiers(noop(), remark(), ecn::TEST_COUNT);
449 assert_ecn_enabled(before); // ECN validation concludes before migration.
450 assert_ecn_disabled(after); // ECN validation fails after migration due to remarking.
451 assert!(migrated);
452 }
453
454 #[test]
455 fn ecn_migration_noop_ce_data() {
456 let (before, after, migrated) = migration_with_modifiers(noop(), ce(), ecn::TEST_COUNT);
457 assert_ecn_enabled(before); // ECN validation concludes before migration.
458 assert_ecn_enabled(after); // ECN validation concludes after migration, despite all CE marks.
459 assert!(migrated);
460 }
461
462 #[test]
463 fn ecn_migration_noop_drop_data() {
464 let (before, after, migrated) = migration_with_modifiers(noop(), drop(), ecn::TEST_COUNT);
465 assert_ecn_enabled(before); // ECN validation concludes before migration.
466 assert_ecn_enabled(after); // Migration failed, ECN on original path is still validated.
467 assert!(!migrated);
468 }
469
470 #[test]
471 fn ecn_migration_bleach_noop_data() {
472 let (before, after, migrated) = migration_with_modifiers(bleach(), noop(), ecn::TEST_COUNT);
473 assert_ecn_disabled(before); // ECN validation fails before migration due to bleaching.
474 assert_ecn_enabled(after); // ECN validation concludes after migration.
475 assert!(migrated);
476 }
477
478 #[test]
479 fn ecn_migration_bleach_bleach_data() {
480 let (before, after, migrated) = migration_with_modifiers(bleach(), bleach(), ecn::TEST_COUNT);
481 assert_ecn_disabled(before); // ECN validation fails before migration due to bleaching.
482 assert_ecn_disabled(after); // ECN validation fails after migration due to bleaching.
483 assert!(migrated);
484 }
485
486 #[test]
487 fn ecn_migration_bleach_remark_data() {
488 let (before, after, migrated) = migration_with_modifiers(bleach(), remark(), ecn::TEST_COUNT);
489 assert_ecn_disabled(before); // ECN validation fails before migration due to bleaching.
490 assert_ecn_disabled(after); // ECN validation fails after migration due to remarking.
491 assert!(migrated);
492 }
493
494 #[test]
495 fn ecn_migration_bleach_ce_data() {
496 let (before, after, migrated) = migration_with_modifiers(bleach(), ce(), ecn::TEST_COUNT);
497 assert_ecn_disabled(before); // ECN validation fails before migration due to bleaching.
498 assert_ecn_enabled(after); // ECN validation concludes after migration, despite all CE marks.
499 assert!(migrated);
500 }
501
502 #[test]
503 fn ecn_migration_bleach_drop_data() {
504 let (before, after, migrated) = migration_with_modifiers(bleach(), drop(), ecn::TEST_COUNT);
505 assert_ecn_disabled(before); // ECN validation fails before migration due to bleaching.
506 // Migration failed, ECN on original path is still disabled.
507 assert_ecn_disabled(after);
508 assert!(!migrated);
509 }
510
511 #[test]
512 fn ecn_migration_remark_noop_data() {
513 let (before, after, migrated) = migration_with_modifiers(remark(), noop(), ecn::TEST_COUNT);
514 assert_ecn_disabled(before); // ECN validation fails before migration due to remarking.
515 assert_ecn_enabled(after); // ECN validation succeeds after migration.
516 assert!(migrated);
517 }
518
519 #[test]
520 fn ecn_migration_remark_bleach_data() {
521 let (before, after, migrated) = migration_with_modifiers(remark(), bleach(), ecn::TEST_COUNT);
522 assert_ecn_disabled(before); // ECN validation fails before migration due to remarking.
523 assert_ecn_disabled(after); // ECN validation fails after migration due to bleaching.
524 assert!(migrated);
525 }
526
527 #[test]
528 fn ecn_migration_remark_remark_data() {
529 let (before, after, migrated) = migration_with_modifiers(remark(), remark(), ecn::TEST_COUNT);
530 assert_ecn_disabled(before); // ECN validation fails before migration due to remarking.
531 assert_ecn_disabled(after); // ECN validation fails after migration due to remarking.
532 assert!(migrated);
533 }
534
535 #[test]
536 fn ecn_migration_remark_ce_data() {
537 let (before, after, migrated) = migration_with_modifiers(remark(), ce(), ecn::TEST_COUNT);
538 assert_ecn_disabled(before); // ECN validation fails before migration due to remarking.
539 assert_ecn_enabled(after); // ECN validation concludes after migration, despite all CE marks.
540 assert!(migrated);
541 }
542
543 #[test]
544 fn ecn_migration_remark_drop_data() {
545 let (before, after, migrated) = migration_with_modifiers(remark(), drop(), ecn::TEST_COUNT);
546 assert_ecn_disabled(before); // ECN validation fails before migration due to remarking.
547 assert_ecn_disabled(after); // Migration failed, ECN on original path is still disabled.
548 assert!(!migrated);
549 }
550
551 #[test]
552 fn ecn_migration_ce_noop_data() {
553 let (before, after, migrated) = migration_with_modifiers(ce(), noop(), ecn::TEST_COUNT);
554 assert_ecn_enabled(before); // ECN validation concludes before migration, despite all CE marks.
555 assert_ecn_enabled(after); // ECN validation concludes after migration.
556 assert!(migrated);
557 }
558
559 #[test]
560 fn ecn_migration_ce_bleach_data() {
561 let (before, after, migrated) = migration_with_modifiers(ce(), bleach(), ecn::TEST_COUNT);
562 assert_ecn_enabled(before); // ECN validation concludes before migration, despite all CE marks.
563 assert_ecn_disabled(after); // ECN validation fails after migration due to bleaching
564 assert!(migrated);
565 }
566
567 #[test]
568 fn ecn_migration_ce_remark_data() {
569 let (before, after, migrated) = migration_with_modifiers(ce(), remark(), ecn::TEST_COUNT);
570 assert_ecn_enabled(before); // ECN validation concludes before migration, despite all CE marks.
571 assert_ecn_disabled(after); // ECN validation fails after migration due to remarking.
572 assert!(migrated);
573 }
574
575 #[test]
576 fn ecn_migration_ce_ce_data() {
577 let (before, after, migrated) = migration_with_modifiers(ce(), ce(), ecn::TEST_COUNT);
578 assert_ecn_enabled(before); // ECN validation concludes before migration, despite all CE marks.
579 assert_ecn_enabled(after); // ECN validation concludes after migration, despite all CE marks.
580 assert!(migrated);
581 }
582
583 #[test]
584 fn ecn_migration_ce_drop_data() {
585 let (before, after, migrated) = migration_with_modifiers(ce(), drop(), ecn::TEST_COUNT);
586 assert_ecn_enabled(before); // ECN validation concludes before migration, despite all CE marks.
587 // Migration failed, ECN on original path is still enabled.
588 assert_ecn_enabled(after);
589 assert!(!migrated);
590 }