// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Finding out how large a datagram the path will carry: RFC 9000 §14 and //! the DPLPMTUD of RFC 8899. //! //! §14.2: "in the absence of these mechanisms, QUIC endpoints SHOULD NOT send //! datagrams larger than the smallest allowed maximum datagram size" — 1200 //! octets, which every path is guaranteed to carry and most carry far more. //! Staying there is correct and slow: every packet header, acknowledgement //! and frame boundary is paid on 1200 octets instead of 1400, and a stream //! that would have fitted in one datagram takes two. //! //! ## Why the search is a search //! //! There is no way to *ask* a path how large a datagram it will carry. ICMP //! would tell an endpoint, and §14.2.1 spends its length explaining why that //! cannot be trusted: the messages are forgeable, often filtered, and the //! ones that arrive may describe a different path. So the only reliable //! question is the empirical one — send a datagram of the size in question //! and see whether it is acknowledged. //! //! §14.4 says what such a probe should contain: "endpoints could limit the //! content of PMTU probes to PING and PADDING frames, since packets that are //! larger than the current maximum datagram size are more likely to be //! dropped by the network." A probe carrying stream data would make every //! failed probe a retransmission. //! //! ## The failure that is not a failure //! //! §14.4: "loss of a QUIC packet that is carried in a PMTU probe is //! therefore not a reliable indication of congestion and SHOULD NOT trigger a //! congestion control reaction". A probe is *expected* to be lost — that is //! what it is for — and an endpoint that let a lost probe halve its //! congestion window would punish itself for asking. const std = @import("std"); const recovery = @import("recovery.zig"); /// §14.1: the datagram size every QUIC path is guaranteed to carry, which /// RFC 8899 calls BASE_PLPMTU and MIN_PLPMTU both. pub const base = 1200; /// The largest this will search up to. /// /// 1452 octets is 1500 — the ethernet MTU almost everything is built around — /// less 20 octets of IPv4 header and 8 of UDP. Searching past it finds /// nothing on a path that has an ethernet segment anywhere in it, and every /// path this is likely to meet does. pub const ceiling = 1452; /// §14.4 and RFC 8899 §5.1.1: how many times a probe of one size is tried /// before the size is written off. "A PL MUST NOT treat the loss of a probe /// packet as an indication of congestion" — and a probe can be lost for /// ordinary reasons as well as for being too large, so one failure is not an /// answer. pub const max_probes = 3; /// The search, as a state machine. /// /// RFC 8899 §5.2 names these states; this is the subset QUIC needs. §14.3.2: /// "QUIC is an acknowledged PL; therefore, a QUIC sender does not implement a /// DPLPMTUD CONFIRMATION_TIMER while in the SEARCH_COMPLETE state" — an /// acknowledgement is proof the path still works, and QUIC has those already. pub const State = enum { /// §14.3.1: "a QUIC sender can therefore enter the DPLPMTUD BASE state /// when the QUIC connection handshake has been completed." Before that /// there is nothing to search with. waiting, /// A probe is outstanding. searching, /// The search has finished, and `current` is the answer. complete, }; pub const Search = struct { state: State = .waiting, /// The largest size known to work, which every ordinary packet is sized /// to fit. current: usize = base, /// The size being probed, when one is. probing: usize = 0, /// The largest size known *not* to work, which bounds the search from /// above. failed: usize = ceiling + 1, /// How many times the current size has been tried. attempts: u8 = 0, /// The packet number of the probe in flight, so that an acknowledgement /// can be recognised — and so that its loss can be told from ordinary /// loss, which §14.4 says must not touch congestion control. outstanding: ?u64 = null, /// §14.3.1: the handshake is done, so the search may begin. /// /// Only the first call does anything: a search already running or /// finished is left as it is, so this can be called on every opportunity /// rather than exactly once. pub fn begin(s: *Search) void { if (s.state != .waiting) return; s.state = .searching; } test begin { var s: Search = .{}; try std.testing.expectEqual(@as(?usize, null), s.next()); s.begin(); try std.testing.expectEqual(State.searching, s.state); try std.testing.expect(s.next() != null); } /// The size to probe next, or null when there is nothing to send now. /// /// A binary search between what is known to work and what is known not /// to. RFC 8899 §5.3 offers several search algorithms and does not /// require any particular one; this is the one that asks the fewest /// questions, which matters because every question costs a datagram that /// is probably going to be dropped. /// /// Null has three meanings: the search has not begun or has finished, a /// probe is already in flight, or the gap left is under eight octets and /// not worth asking about. Only the last is the search running out of /// things to learn. pub fn next(s: *const Search) ?usize { if (s.state != .searching) return null; if (s.outstanding != null) return null; if (s.failed <= s.current + 1) return null; const step = (s.failed - s.current) / 2; // Not worth a round trip for a handful of octets. if (step < 8) return null; return s.current + step; } test next { var s: Search = .{}; s.begin(); // Halfway between the 1200 every path carries and one past the // ceiling. const size = s.next().?; try std.testing.expectEqual(@as(usize, 1326), size); // One probe at a time. s.sent(size, 0); try std.testing.expectEqual(@as(?usize, null), s.next()); } /// Notes that a probe of `size` has gone out as packet `number`, which /// `isProbe`, `acknowledged` and `lost` then recognize it by. /// /// `size` is what `next` answered. Sending the same size again after a /// loss counts as another attempt at it; `lost` writes the size off once /// there have been `max_probes`. pub fn sent(s: *Search, size: usize, number: u64) void { s.probing = size; s.outstanding = number; s.attempts += 1; } test sent { var s: Search = .{}; s.begin(); const size = s.next().?; s.sent(size, 42); try std.testing.expect(s.isProbe(42)); try std.testing.expectEqual(@as(u8, 1), s.attempts); } /// §14.4: the probe was acknowledged, so the path carries that size. /// /// `number` may be any acknowledged packet: anything but the probe in /// flight is ignored, so a caller can hand in every one. The search is /// complete when this leaves nothing worth probing. pub fn acknowledged(s: *Search, number: u64) void { const outstanding = s.outstanding orelse return; if (outstanding != number) return; s.outstanding = null; s.attempts = 0; s.current = @max(s.current, s.probing); if (s.next() == null) s.state = .complete; } test acknowledged { var s: Search = .{}; s.begin(); const size = s.next().?; s.sent(size, 7); s.acknowledged(6); // ordinary traffic: nothing learned try std.testing.expectEqual(@as(usize, base), s.current); s.acknowledged(7); // Every packet from now on may be this large, and the search goes // on above it. try std.testing.expectEqual(size, s.current); try std.testing.expect(s.next().? > size); } /// §14.4: the probe was lost — which is not news about congestion, and /// not yet news about the path either. /// /// "A PL MUST NOT treat the loss of a probe packet as an indication of /// congestion", and a probe can be lost for the ordinary reasons as well /// as for being too large. Only after `max_probes` is the size written /// off. /// /// As with `acknowledged`, a `number` other than the probe in flight is /// ignored. Losing the size for good may also finish the search, when /// what is left between the known-good and the known-bad is too small to /// be worth probing. pub fn lost(s: *Search, number: u64) void { const outstanding = s.outstanding orelse return; if (outstanding != number) return; s.outstanding = null; if (s.attempts < max_probes) return; s.failed = @min(s.failed, s.probing); s.attempts = 0; if (s.next() == null) s.state = .complete; } test lost { var s: Search = .{}; s.begin(); const size = s.next().?; for (0..max_probes) |n| { // The same size again, until the last attempt is lost. try std.testing.expectEqual(size, s.next().?); s.sent(size, n); s.lost(n); } try std.testing.expectEqual(size, s.failed); try std.testing.expect(s.next().? < size); } /// §14.2: an endpoint that learns the path cannot carry what it is using /// must come back down. /// /// "If a QUIC endpoint determines that the PMTU between any pair of local /// and remote IP addresses cannot support the smallest allowed maximum /// datagram size of 1200 bytes, it MUST immediately cease sending QUIC /// packets... on the affected path" — which is a stronger statement than /// this makes, because this never goes below the base and so never has to /// decide the path is unusable. /// /// Everything learned is forgotten: the size drops back to `base`, the /// probe in flight (if any) is no longer recognized, and the search /// starts again from the beginning -- unless it had not begun, in which /// case it still waits for `begin`. pub fn reset(s: *Search) void { s.* = .{ .state = if (s.state == .waiting) .waiting else .searching }; } test reset { var s: Search = .{}; s.begin(); const size = s.next().?; s.sent(size, 0); s.acknowledged(0); try std.testing.expectEqual(size, s.current); s.reset(); try std.testing.expectEqual(@as(usize, base), s.current); try std.testing.expectEqual(State.searching, s.state); try std.testing.expectEqual(size, s.next().?); } /// Whether `number` is the probe in flight, which §14.4 exempts from /// congestion control. False once the probe has been acknowledged or /// lost, and always false when no probe is in flight. pub fn isProbe(s: *const Search, number: u64) bool { const outstanding = s.outstanding orelse return false; return outstanding == number; } test isProbe { var s: Search = .{}; s.begin(); s.sent(s.next().?, 9); try std.testing.expect(s.isProbe(9)); try std.testing.expect(!s.isProbe(8)); s.lost(9); try std.testing.expect(!s.isProbe(9)); } }; const testing = std.testing; test "RFC 9000 14.3.1: nothing is probed before the handshake is done" { // "A QUIC sender can therefore enter the DPLPMTUD BASE state when the // QUIC connection handshake has been completed." Before that every // datagram is carrying handshake data that would have to be retransmitted // if the probe were too large. var s: Search = .{}; try testing.expectEqual(@as(?usize, null), s.next()); try testing.expectEqual(@as(usize, base), s.current); s.begin(); try testing.expect(s.next() != null); } test "RFC 9000 14.2: the search starts where the guarantee ends" { // 1200 octets is what §14.1 guarantees, so it is what an endpoint may // assume and the floor the search never goes below. var s: Search = .{}; s.begin(); // A binary search between the known-good and the known-bad, which asks // the fewest questions — and every question costs a datagram that is // probably about to be dropped. const first = s.next().?; try testing.expect(first > base); try testing.expect(first <= ceiling); try testing.expectEqual(base + (ceiling + 1 - base) / 2, first); } test "RFC 8899 5.3: the search converges from both sides" { var s: Search = .{}; s.begin(); var probes: usize = 0; while (s.next()) |size| : (probes += 1) { // Every size up to 1400 works on this imaginary path; nothing above // does. s.sent(size, probes); if (size <= 1400) { s.acknowledged(probes); } else { // §14.4: one loss is not an answer. Three are. var attempt: usize = 0; while (attempt < max_probes) : (attempt += 1) { s.sent(size, probes); s.lost(probes); } } try testing.expect(probes < 40); } try testing.expectEqual(State.complete, s.state); // Within a few octets of the truth, which is all a binary search that // stops at a granularity of eight can promise. try testing.expect(s.current <= 1400); try testing.expect(s.current > 1400 - 16); } test "RFC 9000 14.4: one lost probe is not an answer" { // "Loss of a QUIC packet that is carried in a PMTU probe is therefore not // a reliable indication of congestion" — nor of the path's size. A probe // can be lost for all the ordinary reasons. var s: Search = .{}; s.begin(); const size = s.next().?; s.sent(size, 1); s.lost(1); // Still a candidate: the search has not given up on it. try testing.expectEqual(@as(usize, ceiling + 1), s.failed); try testing.expectEqual(size, s.next().?); s.sent(size, 2); s.lost(2); try testing.expectEqual(@as(usize, ceiling + 1), s.failed); // The third failure writes the size off. s.sent(size, 3); s.lost(3); try testing.expectEqual(size, s.failed); try testing.expect(s.next().? < size); } test "RFC 9000 14.4: a probe is recognisable, so its loss can be excused" { // §14.4: a lost probe "SHOULD NOT trigger a congestion control reaction", // which requires being able to tell one packet from another after the // fact. The packet number is what does it. var s: Search = .{}; s.begin(); const size = s.next().?; try testing.expect(!s.isProbe(7)); s.sent(size, 7); try testing.expect(s.isProbe(7)); try testing.expect(!s.isProbe(8)); s.acknowledged(7); try testing.expect(!s.isProbe(7)); try testing.expectEqual(size, s.current); } test "an acknowledgement for something else does not end the search" { var s: Search = .{}; s.begin(); const size = s.next().?; s.sent(size, 10); // Ordinary traffic being acknowledged says nothing about the probe. s.acknowledged(9); try testing.expectEqual(@as(usize, base), s.current); try testing.expect(s.isProbe(10)); s.acknowledged(10); try testing.expectEqual(size, s.current); } test "the search stops rather than chasing the last few octets" { // A round trip for eight octets is not worth taking, and a search that // never terminated would probe forever on a path whose size happens to // fall between two candidates. var s: Search = .{}; s.begin(); s.current = 1400; s.failed = 1404; try testing.expectEqual(@as(?usize, null), s.next()); }