1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use crate::MIN_CLIENT_INITIAL_LEN;
#[derive(Default)]
pub struct Pmtud {
/// The current PMTU estimate.
current_mtu: usize,
/// The last PMTU probe size that was attempted.
probe_size: usize,
/// Whether or not a PMTU probe needs to be generated.
should_probe: bool,
/// Whether or not PMTUD is enabled.
enabled: bool,
}
impl Pmtud {
/// Creates new PMTUD instance.
pub fn new(initial_mtu: usize) -> Self {
// QUIC mandates packet sizes >= 1200.
assert!(initial_mtu >= MIN_CLIENT_INITIAL_LEN);
Self {
current_mtu: initial_mtu,
..Default::default()
}
}
/// Enables PMTUD for the connection.
pub fn enable(&mut self, enable: bool) {
self.enabled = enable;
}
/// Whether or not PMTUD is enabled for the connection.
pub fn is_enabled(&mut self) -> bool {
self.enabled
}
/// Specifies whether PMTUD should be performed at the next opportunity,
/// i.e., when the next packet is sent out if possible.
///
/// Once Path MTU has been discovered, this may be set to false.
pub fn set_should_probe(&mut self, should_probe: bool) {
self.should_probe = should_probe;
}
/// Returns the value of the Path MTU Discovery flag.
pub fn get_should_probe(&self) -> bool {
self.should_probe
}
/// Sets the next PMTUD probe size.
pub fn set_probe_size(&mut self, probe_size: usize) {
self.probe_size = probe_size;
}
/// Returns the next PMTUD probe size.
pub fn get_probe_size(&mut self) -> usize {
self.probe_size
}
/// Sets the current discovered PMTU after a successful probe has
/// been performed.
pub fn set_current_mtu(&mut self, pmtu: usize) {
self.current_mtu = pmtu;
}
/// Returns the discovered PMTU.
pub fn get_current_mtu(&mut self) -> usize {
self.current_mtu
}
/// Updates the PMTUD probe size based on the "Optimistic Binary" algorithm
/// defined in <https://www.hb.fh-muenster.de/opus4/frontdoor/deliver/index/docId/14965/file/dplpmtudQuicPaper.pdf>
pub fn update_probe_size(&mut self) {
self.probe_size =
self.current_mtu + ((self.probe_size - self.current_mtu) / 2);
}
/// Updates the PMTUD probe size when a previously sent probe has been lost.
pub fn pmtu_probe_lost(&mut self) {
self.update_probe_size();
self.set_should_probe(true);
}
}
impl std::fmt::Debug for Pmtud {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "current_mtu={:?} ", self.current_mtu)?;
write!(f, "probe_size={:?} ", self.probe_size)?;
write!(f, "should_probe={:?} ", self.should_probe)?;
write!(f, "enabled={:?} ", self.enabled)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pmtud_initial_state() {
let mut pmtud = Pmtud::new(1200);
assert_eq!(pmtud.get_current_mtu(), 1200);
assert_eq!(pmtud.get_probe_size(), 0);
assert!(!pmtud.get_should_probe());
assert!(!pmtud.is_enabled());
}
#[test]
fn pmtud_enable_disable() {
let mut pmtud = Pmtud::new(1200);
pmtud.enable(true);
assert!(pmtud.is_enabled());
pmtud.enable(false);
assert!(!pmtud.is_enabled());
}
#[test]
fn pmtud_probe_flag_management() {
let mut pmtud = Pmtud::new(1200);
// Initially should not probe
assert!(!pmtud.get_should_probe());
// Enable probing
pmtud.set_should_probe(true);
assert!(pmtud.get_should_probe());
// Disable probing
pmtud.set_should_probe(false);
assert!(!pmtud.get_should_probe());
}
#[test]
fn pmtud_binary_search_algorithm() {
let mut pmtud = Pmtud::new(1200);
// Set initial probe size to 1500
pmtud.set_probe_size(1500);
assert_eq!(pmtud.get_probe_size(), 1500);
// Simulate probe loss - should update to midpoint
pmtud.update_probe_size();
// Expected: 1200 + ((1500 - 1200) / 2) = 1200 + 150 = 1350
assert_eq!(pmtud.get_probe_size(), 1350);
// Another probe loss
pmtud.update_probe_size();
// Expected: 1200 + ((1350 - 1200) / 2) = 1200 + 75 = 1275
assert_eq!(pmtud.get_probe_size(), 1275);
pmtud.update_probe_size();
// Expected: 1200 + ((1275 - 1200) / 2) = 1200 + 37 = 1237
assert_eq!(pmtud.get_probe_size(), 1237);
pmtud.update_probe_size();
// Expected: 1200 + ((1237 - 1200) / 2) = 1200 + 18 = 1218
assert_eq!(pmtud.get_probe_size(), 1218);
pmtud.update_probe_size();
// Expected: 1200 + ((1218 - 1200) / 2) = 1200 + 9 = 1209
assert_eq!(pmtud.get_probe_size(), 1209);
pmtud.update_probe_size();
// Expected: 1200 + ((1209 - 1200) / 2) = 1200 + 4 = 1204
assert_eq!(pmtud.get_probe_size(), 1204);
pmtud.update_probe_size();
// Expected: 1200 + ((1204 - 1200) / 2) = 1200 + 2 = 1202
assert_eq!(pmtud.get_probe_size(), 1202);
pmtud.update_probe_size();
// Expected: 1200 + ((1202 - 1200) / 2) = 1200 + 1 = 1201
assert_eq!(pmtud.get_probe_size(), 1201);
pmtud.update_probe_size();
// Expected: 1200 + ((1201 - 1200) / 2) = 1200 + 0 = 1200
assert_eq!(pmtud.get_probe_size(), 1200);
}
#[test]
fn pmtud_probe_lost_behavior() {
let mut pmtud = Pmtud::new(1200);
pmtud.set_probe_size(1500);
pmtud.set_should_probe(false);
// Simulate probe loss
pmtud.pmtu_probe_lost();
// Should re-enable probing and adjust size
assert!(pmtud.get_should_probe());
assert_eq!(pmtud.get_probe_size(), 1350); // binary search result
assert_eq!(pmtud.get_current_mtu(), 1200); // MTU does not change
}
#[test]
fn pmtud_successful_probe() {
let mut pmtud = Pmtud::new(1200);
pmtud.set_probe_size(1400);
// Simulate successful probe
pmtud.set_current_mtu(1400);
assert_eq!(pmtud.get_current_mtu(), 1400);
}
#[test]
fn pmtud_binary_search_convergence() {
let mut pmtud = Pmtud::new(1200);
pmtud.set_probe_size(2000);
// Simulate repeated probe losses to test convergence
for _ in 0..10 {
pmtud.update_probe_size();
}
// Should converge to the minimum allowed packet size
assert_eq!(pmtud.get_probe_size(), 1200);
}
}