1use std::collections::BTreeMap;
28use std::collections::VecDeque;
29
30use std::net::SocketAddr;
31
32use std::time::Duration;
33use std::time::Instant;
34
35use smallvec::SmallVec;
36
37use slab::Slab;
38
39use crate::Config;
40use crate::Error;
41use crate::Result;
42use crate::StartupExit;
43
44use crate::pmtud;
45use crate::recovery;
46use crate::recovery::Bandwidth;
47use crate::recovery::HandshakeStatus;
48use crate::recovery::OnLossDetectionTimeoutOutcome;
49use crate::recovery::RecoveryOps;
50
51#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
53pub enum PathState {
54 Failed,
56
57 Unknown,
59
60 Validating,
62
63 ValidatingMTU,
65
66 Validated,
68}
69
70impl PathState {
71 #[cfg(feature = "ffi")]
72 pub fn to_c(self) -> libc::ssize_t {
73 match self {
74 PathState::Failed => -1,
75 PathState::Unknown => 0,
76 PathState::Validating => 1,
77 PathState::ValidatingMTU => 2,
78 PathState::Validated => 3,
79 }
80 }
81}
82
83#[derive(Clone, Debug, PartialEq, Eq)]
85pub enum PathEvent {
86 New(SocketAddr, SocketAddr),
91
92 Validated(SocketAddr, SocketAddr),
95
96 FailedValidation(SocketAddr, SocketAddr),
100
101 Closed(SocketAddr, SocketAddr),
104
105 ReusedSourceConnectionId(
109 u64,
110 (SocketAddr, SocketAddr),
111 (SocketAddr, SocketAddr),
112 ),
113
114 PeerMigrated(SocketAddr, SocketAddr),
120}
121
122#[derive(Debug)]
124pub struct Path {
125 local_addr: SocketAddr,
127
128 peer_addr: SocketAddr,
130
131 pub active_scid_seq: Option<u64>,
133
134 pub active_dcid_seq: Option<u64>,
136
137 state: PathState,
139
140 active: bool,
142
143 pub recovery: recovery::Recovery,
145
146 pub pmtud: Option<pmtud::Pmtud>,
148
149 in_flight_challenges: VecDeque<([u8; 8], usize, Instant)>,
152
153 max_challenge_size: usize,
155
156 probing_lost: usize,
158
159 last_probe_lost_time: Option<Instant>,
161
162 received_challenges: VecDeque<[u8; 8]>,
164
165 received_challenges_max_len: usize,
167
168 pub sent_count: usize,
170
171 pub recv_count: usize,
173
174 pub retrans_count: usize,
176
177 pub total_pto_count: usize,
184
185 pub dgram_sent_count: usize,
187
188 pub dgram_lost_count: usize,
190
191 pub dgram_recv_count: usize,
193
194 pub sent_bytes: u64,
196
197 pub recv_bytes: u64,
199
200 pub stream_retrans_bytes: u64,
203
204 pub max_send_bytes: usize,
207
208 pub verified_peer_address: bool,
210
211 pub peer_verified_local_address: bool,
213
214 challenge_requested: bool,
216
217 failure_notified: bool,
219
220 migrating: bool,
223
224 pub needs_ack_eliciting: bool,
226}
227
228impl Path {
229 pub fn new(
232 local_addr: SocketAddr, peer_addr: SocketAddr,
233 recovery_config: &recovery::RecoveryConfig,
234 path_challenge_recv_max_queue_len: usize, is_initial: bool,
235 config: Option<&Config>,
236 ) -> Self {
237 let (state, active_scid_seq, active_dcid_seq) = if is_initial {
238 (PathState::Validated, Some(0), Some(0))
239 } else {
240 (PathState::Unknown, None, None)
241 };
242
243 let pmtud = config.and_then(|c| {
244 if c.pmtud {
245 let maximum_supported_mtu: usize = std::cmp::min(
246 c.local_transport_params
249 .max_udp_payload_size
250 .try_into()
251 .unwrap_or(c.max_send_udp_payload_size),
252 c.max_send_udp_payload_size,
253 );
254 Some(pmtud::Pmtud::new(maximum_supported_mtu, c.pmtud_max_probes))
255 } else {
256 None
257 }
258 });
259
260 Self {
261 local_addr,
262 peer_addr,
263 active_scid_seq,
264 active_dcid_seq,
265 state,
266 active: false,
267 recovery: recovery::Recovery::new_with_config(recovery_config),
268 pmtud,
269 in_flight_challenges: VecDeque::new(),
270 max_challenge_size: 0,
271 probing_lost: 0,
272 last_probe_lost_time: None,
273 received_challenges: VecDeque::with_capacity(
274 path_challenge_recv_max_queue_len,
275 ),
276 received_challenges_max_len: path_challenge_recv_max_queue_len,
277 sent_count: 0,
278 recv_count: 0,
279 retrans_count: 0,
280 total_pto_count: 0,
281 dgram_sent_count: 0,
282 dgram_lost_count: 0,
283 dgram_recv_count: 0,
284 sent_bytes: 0,
285 recv_bytes: 0,
286 stream_retrans_bytes: 0,
287 max_send_bytes: 0,
288 verified_peer_address: false,
289 peer_verified_local_address: false,
290 challenge_requested: false,
291 failure_notified: false,
292 migrating: false,
293 needs_ack_eliciting: false,
294 }
295 }
296
297 #[inline]
299 pub fn local_addr(&self) -> SocketAddr {
300 self.local_addr
301 }
302
303 #[inline]
305 pub fn peer_addr(&self) -> SocketAddr {
306 self.peer_addr
307 }
308
309 #[inline]
311 fn working(&self) -> bool {
312 self.state > PathState::Failed
313 }
314
315 #[inline]
317 pub fn active(&self) -> bool {
318 self.active && self.working() && self.active_dcid_seq.is_some()
319 }
320
321 #[inline]
323 pub fn usable(&self) -> bool {
324 self.active() ||
325 (self.state == PathState::Validated &&
326 self.active_dcid_seq.is_some())
327 }
328
329 #[inline]
331 fn unused(&self) -> bool {
332 !self.active() && self.active_dcid_seq.is_none()
334 }
335
336 #[inline]
338 pub fn probing_required(&self) -> bool {
339 !self.received_challenges.is_empty() || self.validation_requested()
340 }
341
342 fn promote_to(&mut self, state: PathState) {
345 if self.state < state {
346 self.state = state;
347 }
348 }
349
350 #[inline]
352 pub fn validated(&self) -> bool {
353 self.state == PathState::Validated
354 }
355
356 #[inline]
358 fn validation_failed(&self) -> bool {
359 self.state == PathState::Failed
360 }
361
362 #[inline]
364 pub fn under_validation(&self) -> bool {
365 matches!(self.state, PathState::Validating | PathState::ValidatingMTU)
366 }
367
368 #[inline]
370 pub fn request_validation(&mut self) {
371 self.challenge_requested = true;
372 }
373
374 #[inline]
376 pub fn validation_requested(&self) -> bool {
377 self.challenge_requested
378 }
379
380 pub fn should_send_pmtu_probe(
381 &mut self, hs_confirmed: bool, hs_done: bool, out_len: usize,
382 is_closing: bool, frames_empty: bool,
383 ) -> bool {
384 let Some(pmtud) = self.pmtud.as_mut() else {
385 return false;
386 };
387
388 (hs_confirmed && hs_done) &&
389 self.recovery.cwnd_available() > pmtud.get_probe_size() &&
390 out_len >= pmtud.get_probe_size() &&
391 pmtud.should_probe() &&
392 !is_closing &&
393 frames_empty
394 }
395
396 pub fn on_challenge_sent(&mut self) {
397 self.promote_to(PathState::Validating);
398 self.challenge_requested = false;
399 }
400
401 pub fn add_challenge_sent(
403 &mut self, data: [u8; 8], pkt_size: usize, sent_time: Instant,
404 ) {
405 self.on_challenge_sent();
406 self.in_flight_challenges
407 .push_back((data, pkt_size, sent_time));
408 }
409
410 pub fn on_challenge_received(&mut self, data: [u8; 8]) {
411 if self.received_challenges.len() == self.received_challenges_max_len {
413 return;
414 }
415
416 self.received_challenges.push_back(data);
417 self.peer_verified_local_address = true;
418 }
419
420 pub fn has_pending_challenge(&self, data: [u8; 8]) -> bool {
421 self.in_flight_challenges.iter().any(|(d, ..)| *d == data)
422 }
423
424 pub fn on_response_received(&mut self, data: [u8; 8]) -> bool {
426 self.verified_peer_address = true;
427 self.probing_lost = 0;
428
429 let mut challenge_size = 0;
430 self.in_flight_challenges.retain(|(d, s, _)| {
431 if *d == data {
432 challenge_size = *s;
433 false
434 } else {
435 true
436 }
437 });
438
439 self.promote_to(PathState::ValidatingMTU);
441
442 self.max_challenge_size =
443 std::cmp::max(self.max_challenge_size, challenge_size);
444
445 if self.state == PathState::ValidatingMTU {
446 if self.max_challenge_size >= crate::MIN_CLIENT_INITIAL_LEN {
447 self.promote_to(PathState::Validated);
449 return true;
450 }
451
452 self.request_validation();
454 }
455
456 false
457 }
458
459 fn on_failed_validation(&mut self) {
460 self.state = PathState::Failed;
461 self.active = false;
462 }
463
464 #[inline]
465 pub fn pop_received_challenge(&mut self) -> Option<[u8; 8]> {
466 self.received_challenges.pop_front()
467 }
468
469 pub fn on_loss_detection_timeout(
470 &mut self, handshake_status: HandshakeStatus, now: Instant,
471 is_server: bool, trace_id: &str,
472 ) -> OnLossDetectionTimeoutOutcome {
473 let outcome = self.recovery.on_loss_detection_timeout(
474 handshake_status,
475 now,
476 trace_id,
477 );
478
479 let mut lost_probe_time = None;
480 self.in_flight_challenges.retain(|(_, _, sent_time)| {
481 if *sent_time <= now {
482 if lost_probe_time.is_none() {
483 lost_probe_time = Some(*sent_time);
484 }
485 false
486 } else {
487 true
488 }
489 });
490
491 if let Some(lost_probe_time) = lost_probe_time {
494 self.last_probe_lost_time = match self.last_probe_lost_time {
495 Some(last) => {
496 if lost_probe_time - last >= self.recovery.rtt() {
498 self.probing_lost += 1;
499 Some(lost_probe_time)
500 } else {
501 Some(last)
502 }
503 },
504 None => {
505 self.probing_lost += 1;
506 Some(lost_probe_time)
507 },
508 };
509 if self.probing_lost >= crate::MAX_PROBING_TIMEOUTS ||
513 (is_server && self.max_send_bytes < crate::MIN_PROBING_SIZE)
514 {
515 self.on_failed_validation();
516 } else {
517 self.request_validation();
518 }
519 }
520
521 self.total_pto_count += 1;
523
524 outcome
525 }
526
527 pub fn can_reinit_recovery(&self) -> bool {
531 self.recovery.bytes_in_flight() == 0 &&
538 self.recovery.bytes_in_flight_duration() == Duration::ZERO
539 }
540
541 pub fn reinit_recovery(
542 &mut self, recovery_config: &recovery::RecoveryConfig,
543 ) {
544 self.recovery = recovery::Recovery::new_with_config(recovery_config)
545 }
546
547 pub fn stats(&self) -> PathStats {
548 let pmtu = match self.pmtud.as_ref().map(|p| p.get_current_mtu()) {
549 Some(v) => v,
550
551 None => self.recovery.max_datagram_size(),
552 };
553
554 PathStats {
555 local_addr: self.local_addr,
556 peer_addr: self.peer_addr,
557 validation_state: self.state,
558 active: self.active,
559 recv: self.recv_count,
560 sent: self.sent_count,
561 lost: self.recovery.lost_count(),
562 retrans: self.retrans_count,
563 total_pto_count: self.total_pto_count,
564 dgram_recv: self.dgram_recv_count,
565 dgram_sent: self.dgram_sent_count,
566 dgram_lost: self.dgram_lost_count,
567 rtt: self.recovery.rtt(),
568 min_rtt: self.recovery.min_rtt(),
569 max_rtt: self.recovery.max_rtt(),
570 rttvar: self.recovery.rttvar(),
571 cwnd: self.recovery.cwnd(),
572 sent_bytes: self.sent_bytes,
573 recv_bytes: self.recv_bytes,
574 lost_bytes: self.recovery.bytes_lost(),
575 stream_retrans_bytes: self.stream_retrans_bytes,
576 pmtu,
577 delivery_rate: self.recovery.delivery_rate().to_bytes_per_second(),
578 max_bandwidth: self
579 .recovery
580 .max_bandwidth()
581 .map(Bandwidth::to_bytes_per_second),
582 startup_exit: self.recovery.startup_exit(),
583 }
584 }
585
586 pub fn bytes_in_flight_duration(&self) -> Duration {
587 self.recovery.bytes_in_flight_duration()
588 }
589}
590
591#[derive(Default)]
593pub struct SocketAddrIter {
594 pub(crate) sockaddrs: SmallVec<[SocketAddr; 8]>,
595 pub(crate) index: usize,
596}
597
598impl Iterator for SocketAddrIter {
599 type Item = SocketAddr;
600
601 #[inline]
602 fn next(&mut self) -> Option<Self::Item> {
603 let v = self.sockaddrs.get(self.index)?;
604 self.index += 1;
605 Some(*v)
606 }
607}
608
609impl ExactSizeIterator for SocketAddrIter {
610 #[inline]
611 fn len(&self) -> usize {
612 self.sockaddrs.len() - self.index
613 }
614}
615
616pub struct PathMap {
618 paths: Slab<Path>,
621
622 max_concurrent_paths: usize,
624
625 addrs_to_paths: BTreeMap<(SocketAddr, SocketAddr), usize>,
628
629 events: VecDeque<PathEvent>,
631
632 is_server: bool,
634}
635
636impl PathMap {
637 pub fn new(
640 mut initial_path: Path, max_concurrent_paths: usize, is_server: bool,
641 ) -> Self {
642 let mut paths = Slab::with_capacity(1); let mut addrs_to_paths = BTreeMap::new();
644
645 let local_addr = initial_path.local_addr;
646 let peer_addr = initial_path.peer_addr;
647
648 initial_path.active = true;
650
651 let active_path_id = paths.insert(initial_path);
652 addrs_to_paths.insert((local_addr, peer_addr), active_path_id);
653
654 Self {
655 paths,
656 max_concurrent_paths,
657 addrs_to_paths,
658 events: VecDeque::new(),
659 is_server,
660 }
661 }
662
663 #[inline]
669 pub fn get(&self, path_id: usize) -> Result<&Path> {
670 self.paths.get(path_id).ok_or(Error::InvalidState)
671 }
672
673 #[inline]
679 pub fn get_mut(&mut self, path_id: usize) -> Result<&mut Path> {
680 self.paths.get_mut(path_id).ok_or(Error::InvalidState)
681 }
682
683 #[inline]
684 pub fn get_active_with_pid(&self) -> Option<(usize, &Path)> {
687 self.paths.iter().find(|(_, p)| p.active())
688 }
689
690 #[inline]
695 pub fn get_active(&self) -> Result<&Path> {
696 self.get_active_with_pid()
697 .map(|(_, p)| p)
698 .ok_or(Error::InvalidState)
699 }
700
701 #[inline]
706 pub fn get_active_path_id(&self) -> Result<usize> {
707 self.get_active_with_pid()
708 .map(|(pid, _)| pid)
709 .ok_or(Error::InvalidState)
710 }
711
712 #[inline]
717 pub fn get_active_mut(&mut self) -> Result<&mut Path> {
718 self.paths
719 .iter_mut()
720 .map(|(_, p)| p)
721 .find(|p| p.active())
722 .ok_or(Error::InvalidState)
723 }
724
725 #[inline]
727 pub fn iter(&self) -> slab::Iter<'_, Path> {
728 self.paths.iter()
729 }
730
731 #[inline]
733 pub fn iter_mut(&mut self) -> slab::IterMut<'_, Path> {
734 self.paths.iter_mut()
735 }
736
737 #[inline]
739 pub fn len(&self) -> usize {
740 self.paths.len()
741 }
742
743 #[inline]
745 pub fn path_id_from_addrs(
746 &self, addrs: &(SocketAddr, SocketAddr),
747 ) -> Option<usize> {
748 self.addrs_to_paths.get(addrs).copied()
749 }
750
751 fn make_room_for_new_path(&mut self) -> Result<()> {
757 if self.paths.len() < self.max_concurrent_paths {
758 return Ok(());
759 }
760
761 let (pid_to_remove, _) = self
762 .paths
763 .iter()
764 .find(|(_, p)| p.unused())
765 .ok_or(Error::Done)?;
766
767 let path = self.paths.remove(pid_to_remove);
768 self.addrs_to_paths
769 .remove(&(path.local_addr, path.peer_addr));
770
771 self.notify_event(PathEvent::Closed(path.local_addr, path.peer_addr));
772
773 Ok(())
774 }
775
776 pub fn insert_path(&mut self, path: Path, is_server: bool) -> Result<usize> {
787 self.make_room_for_new_path()?;
788
789 let local_addr = path.local_addr;
790 let peer_addr = path.peer_addr;
791
792 let pid = self.paths.insert(path);
793 self.addrs_to_paths.insert((local_addr, peer_addr), pid);
794
795 if is_server {
797 self.notify_event(PathEvent::New(local_addr, peer_addr));
798 }
799
800 Ok(pid)
801 }
802
803 pub fn notify_event(&mut self, ev: PathEvent) {
805 self.events.push_back(ev);
806 }
807
808 pub fn pop_event(&mut self) -> Option<PathEvent> {
810 self.events.pop_front()
811 }
812
813 pub fn notify_failed_validations(&mut self) {
815 let validation_failed = self
816 .paths
817 .iter_mut()
818 .filter(|(_, p)| p.validation_failed() && !p.failure_notified);
819
820 for (_, p) in validation_failed {
821 self.events.push_back(PathEvent::FailedValidation(
822 p.local_addr,
823 p.peer_addr,
824 ));
825
826 p.failure_notified = true;
827 }
828 }
829
830 pub fn find_candidate_path(&self) -> Option<usize> {
832 self.paths
834 .iter()
835 .find(|(_, p)| p.usable())
836 .map(|(pid, _)| pid)
837 }
838
839 pub fn on_response_received(&mut self, data: [u8; 8]) -> Result<()> {
841 let active_pid = self.get_active_path_id()?;
842
843 let challenge_pending =
844 self.iter_mut().find(|(_, p)| p.has_pending_challenge(data));
845
846 if let Some((pid, p)) = challenge_pending {
847 if p.on_response_received(data) {
848 let local_addr = p.local_addr;
849 let peer_addr = p.peer_addr;
850 let was_migrating = p.migrating;
851
852 p.migrating = false;
853
854 self.notify_event(PathEvent::Validated(local_addr, peer_addr));
856
857 if pid == active_pid && was_migrating {
860 self.notify_event(PathEvent::PeerMigrated(
861 local_addr, peer_addr,
862 ));
863 }
864 }
865 }
866 Ok(())
867 }
868
869 pub fn set_active_path(&mut self, path_id: usize) -> Result<()> {
880 let is_server = self.is_server;
881
882 if let Ok(old_active_path) = self.get_active_mut() {
883 old_active_path.active = false;
884 }
885
886 let new_active_path = self.get_mut(path_id)?;
887 new_active_path.active = true;
888
889 if is_server {
890 if new_active_path.validated() {
891 let local_addr = new_active_path.local_addr();
892 let peer_addr = new_active_path.peer_addr();
893
894 self.notify_event(PathEvent::PeerMigrated(local_addr, peer_addr));
895 } else {
896 new_active_path.migrating = true;
897
898 if !new_active_path.under_validation() {
900 new_active_path.request_validation();
901 }
902 }
903 }
904
905 Ok(())
906 }
907
908 pub fn set_discover_pmtu_on_existing_paths(
910 &mut self, discover: bool, max_send_udp_payload_size: usize,
911 pmtud_max_probes: u8,
912 ) {
913 for (_, path) in self.paths.iter_mut() {
914 path.pmtud = if discover {
915 Some(pmtud::Pmtud::new(
916 max_send_udp_payload_size,
917 pmtud_max_probes,
918 ))
919 } else {
920 None
921 };
922 }
923 }
924}
925
926#[derive(Clone)]
933#[non_exhaustive]
934pub struct PathStats {
935 pub local_addr: SocketAddr,
937
938 pub peer_addr: SocketAddr,
940
941 pub validation_state: PathState,
943
944 pub active: bool,
946
947 pub recv: usize,
949
950 pub sent: usize,
952
953 pub lost: usize,
955
956 pub retrans: usize,
958
959 pub total_pto_count: usize,
966
967 pub dgram_recv: usize,
969
970 pub dgram_sent: usize,
972
973 pub dgram_lost: usize,
975
976 pub rtt: Duration,
978
979 pub min_rtt: Option<Duration>,
981
982 pub max_rtt: Option<Duration>,
984
985 pub rttvar: Duration,
988
989 pub cwnd: usize,
991
992 pub sent_bytes: u64,
994
995 pub recv_bytes: u64,
997
998 pub lost_bytes: u64,
1000
1001 pub stream_retrans_bytes: u64,
1003
1004 pub pmtu: usize,
1006
1007 pub delivery_rate: u64,
1016
1017 pub max_bandwidth: Option<u64>,
1022
1023 pub startup_exit: Option<StartupExit>,
1025}
1026
1027impl std::fmt::Debug for PathStats {
1028 #[inline]
1029 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
1030 write!(
1031 f,
1032 "local_addr={:?} peer_addr={:?} ",
1033 self.local_addr, self.peer_addr,
1034 )?;
1035 write!(
1036 f,
1037 "validation_state={:?} active={} ",
1038 self.validation_state, self.active,
1039 )?;
1040 write!(
1041 f,
1042 "recv={} sent={} lost={} retrans={} rtt={:?} min_rtt={:?} rttvar={:?} cwnd={}",
1043 self.recv, self.sent, self.lost, self.retrans, self.rtt, self.min_rtt, self.rttvar, self.cwnd,
1044 )?;
1045
1046 write!(
1047 f,
1048 " sent_bytes={} recv_bytes={} lost_bytes={}",
1049 self.sent_bytes, self.recv_bytes, self.lost_bytes,
1050 )?;
1051
1052 write!(
1053 f,
1054 " stream_retrans_bytes={} pmtu={} delivery_rate={}",
1055 self.stream_retrans_bytes, self.pmtu, self.delivery_rate,
1056 )
1057 }
1058}
1059
1060#[cfg(test)]
1061mod tests {
1062 use crate::rand;
1063 use crate::MIN_CLIENT_INITIAL_LEN;
1064
1065 use crate::recovery::RecoveryConfig;
1066 use crate::Config;
1067
1068 use super::*;
1069
1070 #[test]
1071 fn path_validation_limited_mtu() {
1072 let client_addr = "127.0.0.1:1234".parse().unwrap();
1073 let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
1074 let server_addr = "127.0.0.1:4321".parse().unwrap();
1075
1076 let config = Config::new(crate::PROTOCOL_VERSION).unwrap();
1077 let recovery_config = RecoveryConfig::from_config(&config);
1078
1079 let path = Path::new(
1080 client_addr,
1081 server_addr,
1082 &recovery_config,
1083 config.path_challenge_recv_max_queue_len,
1084 true,
1085 None,
1086 );
1087 let mut path_mgr = PathMap::new(path, 2, false);
1088
1089 let probed_path = Path::new(
1090 client_addr_2,
1091 server_addr,
1092 &recovery_config,
1093 config.path_challenge_recv_max_queue_len,
1094 false,
1095 None,
1096 );
1097 path_mgr.insert_path(probed_path, false).unwrap();
1098
1099 let pid = path_mgr
1100 .path_id_from_addrs(&(client_addr_2, server_addr))
1101 .unwrap();
1102 path_mgr.get_mut(pid).unwrap().request_validation();
1103 assert!(path_mgr.get_mut(pid).unwrap().validation_requested());
1104 assert!(path_mgr.get_mut(pid).unwrap().probing_required());
1105
1106 let data = rand::rand_u64().to_be_bytes();
1109 path_mgr.get_mut(pid).unwrap().add_challenge_sent(
1110 data,
1111 MIN_CLIENT_INITIAL_LEN - 1,
1112 Instant::now(),
1113 );
1114
1115 assert!(!path_mgr.get_mut(pid).unwrap().validation_requested());
1116 assert!(!path_mgr.get_mut(pid).unwrap().probing_required());
1117 assert!(path_mgr.get_mut(pid).unwrap().under_validation());
1118 assert!(!path_mgr.get_mut(pid).unwrap().validated());
1119 assert_eq!(path_mgr.get_mut(pid).unwrap().state, PathState::Validating);
1120 assert_eq!(path_mgr.pop_event(), None);
1121
1122 path_mgr.on_response_received(data).unwrap();
1125
1126 assert!(path_mgr.get_mut(pid).unwrap().validation_requested());
1127 assert!(path_mgr.get_mut(pid).unwrap().probing_required());
1128 assert!(path_mgr.get_mut(pid).unwrap().under_validation());
1129 assert!(!path_mgr.get_mut(pid).unwrap().validated());
1130 assert_eq!(
1131 path_mgr.get_mut(pid).unwrap().state,
1132 PathState::ValidatingMTU
1133 );
1134 assert_eq!(path_mgr.pop_event(), None);
1135
1136 let data = rand::rand_u64().to_be_bytes();
1139 path_mgr.get_mut(pid).unwrap().add_challenge_sent(
1140 data,
1141 MIN_CLIENT_INITIAL_LEN,
1142 Instant::now(),
1143 );
1144
1145 path_mgr.on_response_received(data).unwrap();
1146
1147 assert!(!path_mgr.get_mut(pid).unwrap().validation_requested());
1148 assert!(!path_mgr.get_mut(pid).unwrap().probing_required());
1149 assert!(!path_mgr.get_mut(pid).unwrap().under_validation());
1150 assert!(path_mgr.get_mut(pid).unwrap().validated());
1151 assert_eq!(path_mgr.get_mut(pid).unwrap().state, PathState::Validated);
1152 assert_eq!(
1153 path_mgr.pop_event(),
1154 Some(PathEvent::Validated(client_addr_2, server_addr))
1155 );
1156 }
1157
1158 #[test]
1159 fn multiple_probes() {
1160 let client_addr = "127.0.0.1:1234".parse().unwrap();
1161 let server_addr = "127.0.0.1:4321".parse().unwrap();
1162
1163 let config = Config::new(crate::PROTOCOL_VERSION).unwrap();
1164 let recovery_config = RecoveryConfig::from_config(&config);
1165
1166 let path = Path::new(
1167 client_addr,
1168 server_addr,
1169 &recovery_config,
1170 config.path_challenge_recv_max_queue_len,
1171 true,
1172 None,
1173 );
1174 let mut client_path_mgr = PathMap::new(path, 2, false);
1175 let mut server_path = Path::new(
1176 server_addr,
1177 client_addr,
1178 &recovery_config,
1179 config.path_challenge_recv_max_queue_len,
1180 false,
1181 None,
1182 );
1183
1184 let client_pid = client_path_mgr
1185 .path_id_from_addrs(&(client_addr, server_addr))
1186 .unwrap();
1187
1188 let data = rand::rand_u64().to_be_bytes();
1190
1191 client_path_mgr
1192 .get_mut(client_pid)
1193 .unwrap()
1194 .add_challenge_sent(data, MIN_CLIENT_INITIAL_LEN, Instant::now());
1195
1196 let data_2 = rand::rand_u64().to_be_bytes();
1198
1199 client_path_mgr
1200 .get_mut(client_pid)
1201 .unwrap()
1202 .add_challenge_sent(data_2, MIN_CLIENT_INITIAL_LEN, Instant::now());
1203 assert_eq!(
1204 client_path_mgr
1205 .get(client_pid)
1206 .unwrap()
1207 .in_flight_challenges
1208 .len(),
1209 2
1210 );
1211
1212 server_path.on_challenge_received(data);
1214 assert_eq!(server_path.received_challenges.len(), 1);
1215 server_path.on_challenge_received(data_2);
1216 assert_eq!(server_path.received_challenges.len(), 2);
1217
1218 client_path_mgr.on_response_received(data).unwrap();
1220 assert_eq!(
1221 client_path_mgr
1222 .get(client_pid)
1223 .unwrap()
1224 .in_flight_challenges
1225 .len(),
1226 1
1227 );
1228
1229 client_path_mgr.on_response_received(data_2).unwrap();
1231 assert_eq!(
1232 client_path_mgr
1233 .get(client_pid)
1234 .unwrap()
1235 .in_flight_challenges
1236 .len(),
1237 0
1238 );
1239 }
1240
1241 #[test]
1242 fn too_many_probes() {
1243 let client_addr = "127.0.0.1:1234".parse().unwrap();
1244 let server_addr = "127.0.0.1:4321".parse().unwrap();
1245
1246 let config = Config::new(crate::PROTOCOL_VERSION).unwrap();
1248 let recovery_config = RecoveryConfig::from_config(&config);
1249
1250 let path = Path::new(
1251 client_addr,
1252 server_addr,
1253 &recovery_config,
1254 config.path_challenge_recv_max_queue_len,
1255 true,
1256 None,
1257 );
1258 let mut client_path_mgr = PathMap::new(path, 2, false);
1259 let mut server_path = Path::new(
1260 server_addr,
1261 client_addr,
1262 &recovery_config,
1263 config.path_challenge_recv_max_queue_len,
1264 false,
1265 None,
1266 );
1267
1268 let client_pid = client_path_mgr
1269 .path_id_from_addrs(&(client_addr, server_addr))
1270 .unwrap();
1271
1272 let data = rand::rand_u64().to_be_bytes();
1274
1275 client_path_mgr
1276 .get_mut(client_pid)
1277 .unwrap()
1278 .add_challenge_sent(data, MIN_CLIENT_INITIAL_LEN, Instant::now());
1279
1280 let data_2 = rand::rand_u64().to_be_bytes();
1282
1283 client_path_mgr
1284 .get_mut(client_pid)
1285 .unwrap()
1286 .add_challenge_sent(data_2, MIN_CLIENT_INITIAL_LEN, Instant::now());
1287 assert_eq!(
1288 client_path_mgr
1289 .get(client_pid)
1290 .unwrap()
1291 .in_flight_challenges
1292 .len(),
1293 2
1294 );
1295
1296 let data_3 = rand::rand_u64().to_be_bytes();
1298
1299 client_path_mgr
1300 .get_mut(client_pid)
1301 .unwrap()
1302 .add_challenge_sent(data_3, MIN_CLIENT_INITIAL_LEN, Instant::now());
1303 assert_eq!(
1304 client_path_mgr
1305 .get(client_pid)
1306 .unwrap()
1307 .in_flight_challenges
1308 .len(),
1309 3
1310 );
1311
1312 let data_4 = rand::rand_u64().to_be_bytes();
1314
1315 client_path_mgr
1316 .get_mut(client_pid)
1317 .unwrap()
1318 .add_challenge_sent(data_4, MIN_CLIENT_INITIAL_LEN, Instant::now());
1319 assert_eq!(
1320 client_path_mgr
1321 .get(client_pid)
1322 .unwrap()
1323 .in_flight_challenges
1324 .len(),
1325 4
1326 );
1327
1328 server_path.on_challenge_received(data);
1331 assert_eq!(server_path.received_challenges.len(), 1);
1332 server_path.on_challenge_received(data_2);
1333 assert_eq!(server_path.received_challenges.len(), 2);
1334 server_path.on_challenge_received(data_3);
1335 assert_eq!(server_path.received_challenges.len(), 3);
1336 server_path.on_challenge_received(data_4);
1337 assert_eq!(server_path.received_challenges.len(), 3);
1338
1339 client_path_mgr.on_response_received(data).unwrap();
1341 assert_eq!(
1342 client_path_mgr
1343 .get(client_pid)
1344 .unwrap()
1345 .in_flight_challenges
1346 .len(),
1347 3
1348 );
1349
1350 client_path_mgr.on_response_received(data_2).unwrap();
1352 assert_eq!(
1353 client_path_mgr
1354 .get(client_pid)
1355 .unwrap()
1356 .in_flight_challenges
1357 .len(),
1358 2
1359 );
1360
1361 client_path_mgr.on_response_received(data_3).unwrap();
1363 assert_eq!(
1364 client_path_mgr
1365 .get(client_pid)
1366 .unwrap()
1367 .in_flight_challenges
1368 .len(),
1369 1
1370 );
1371
1372 }
1374}