quiche/
pmtud.rs

1#[derive(Default)]
2pub struct Pmtud {
3    /// The current path MTU estimate.
4    cur_size: usize,
5
6    /// The last MTU probe size that was attempted.
7    probe: usize,
8
9    /// Indicated if Path MTU probe needs to be generated.
10    next_size: bool,
11
12    /// Check config for PMTU variable.
13    enable: bool,
14}
15
16impl Pmtud {
17    /// Creates new PMTUD instance.
18    pub fn new(cur_size: usize) -> Self {
19        Self {
20            cur_size,
21            ..Default::default()
22        }
23    }
24
25    /// Enables Path MTU Discovery for the connection.
26    pub fn enable(&mut self, enable: bool) {
27        self.enable = enable;
28    }
29
30    /// Returns enable status for Path MTU Discovery for the connection.
31    pub fn is_enabled(&mut self) -> bool {
32        self.enable
33    }
34
35    /// Specifies whether Path MTU Discovery should be performed at the next
36    /// opportunity, i.e., when the next packet is sent out if possible.
37    ///
38    /// Once Path MTU has been discovered, this maybe set to false.
39    pub fn should_probe(&mut self, pmtu_next: bool) {
40        self.next_size = pmtu_next;
41    }
42
43    /// Returns the value of the Path MTU Discovery flag.
44    pub fn get_probe_status(&self) -> bool {
45        self.next_size
46    }
47
48    /// Sets the next Path MTU Discovery probe size.
49    pub fn set_probe_size(&mut self, pmtu_probe: usize) {
50        self.probe = pmtu_probe;
51    }
52
53    /// Returns the next Path MTU Discovery probe size.
54    pub fn get_probe_size(&mut self) -> usize {
55        self.probe
56    }
57
58    /// Sets the current Path MTU Discovery size after a successful probe has
59    /// been performed.
60    pub fn set_current(&mut self, pmtu: usize) {
61        self.cur_size = pmtu;
62    }
63
64    /// Returns the discovered PATH MTU size.
65    pub fn get_current(&mut self) -> usize {
66        self.cur_size
67    }
68
69    /// Selects path MTU probe based on the binary search algorithm.
70    ///
71    /// Based on the Optimistic Binary algorithm defined in:
72    /// Ref: <https://www.hb.fh-muenster.de/opus4/frontdoor/deliver/index/docId/14965/file/dplpmtudQuicPaper.pdf>
73    pub fn update_probe_size(&mut self) {
74        self.probe = self.cur_size + ((self.probe - self.cur_size) / 2);
75    }
76
77    /// Updates probe value when the Path MTU Discovery probe is lost.
78    pub fn pmtu_probe_lost(&mut self) {
79        self.update_probe_size();
80        self.should_probe(true);
81    }
82}
83
84impl std::fmt::Debug for Pmtud {
85    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
86        write!(f, "current={:?} ", self.cur_size)?;
87        write!(f, "probe_size={:?} ", self.probe)?;
88        write!(f, "continue_probing={:?} ", self.next_size)?;
89        write!(f, "enable={:?} ", self.enable)?;
90        Ok(())
91    }
92}