// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! What a receiver remembers about the packets it has received, and the ACK //! frames it builds from that. //! //! One of these per packet number space (§12.3), because the three spaces are //! separate in every way that matters: each numbers its packets from zero, //! and a packet in one can only be acknowledged in a packet of the same kind. //! Acknowledging an Initial packet in a Handshake packet is not merely wrong, //! it is a statement about a packet number that means something else. //! //! ## The shape of the data, and why it is bounded //! //! Received packet numbers are kept as a list of inclusive ranges, newest //! first, which is also the shape an ACK frame wants — §19.3 is a largest //! acknowledged and then a walk downwards. Ordinary traffic keeps this at one //! range: packets arrive in order, and each one extends the newest range by //! one. //! //! What makes it grow is gaps, and a peer can manufacture those. Sending //! every other packet number, or dropping one in ten, produces a range per //! gap — so an endpoint that kept all of them would have its memory decided //! by whoever is talking to it. The list is therefore fixed and the oldest //! range is dropped when it is full, which is what every implementation does //! and what §13.2.3 anticipates when it says an endpoint "is not obligated to //! acknowledge" what it can no longer describe. //! //! The cost of dropping is bounded and dull: a peer that is not told its //! packet arrived eventually retransmits it. The cost of not dropping is that //! a connection's memory is an attacker's parameter. const std = @import("std"); const ecn = @import("ecn.zig"); const assert = std.debug.assert; const frame = @import("frame.zig"); const varint = @import("varint.zig"); /// How many disjoint ranges of received packet numbers to remember. /// /// Thirty-two is the conventional number and is generous: reaching it needs /// thirty-two separate gaps in flight at once, which ordinary loss does not /// produce and which a peer gains nothing by manufacturing. pub const max_ranges = 32; /// An inclusive range of packet numbers, which is how §19.3 counts them too. pub const Range = struct { smallest: u64, largest: u64, /// Whether packet number `n` lies in the range, both ends included. pub fn contains(r: Range, n: u64) bool { return n >= r.smallest and n <= r.largest; } test contains { const r: Range = .{ .smallest = 3, .largest = 5 }; try testing.expect(r.contains(3)); try testing.expect(r.contains(5)); try testing.expect(!r.contains(6)); } }; pub const Error = error{ /// The ACK would not fit in the buffer offered for it. NoSpace, }; /// The two-bit ECN field of an IP header, re-exported so that a caller /// handing one to `Tracker.countEcn` need not know where it is defined. /// /// It arrives with a datagram rather than inside it -- a QUIC packet has no /// room for its own copy -- so a caller that cannot read its platform's IP /// header has nothing to pass and says `not_ect`. §13.4.1 of RFC 9000 allows /// exactly that: "The use of ECN requires the receiving endpoint to read the /// ECN field from an IP packet, which is not possible on all platforms. If an /// endpoint does not implement ECN support or does not have access to /// received ECN fields, it does not report ECN counts for packets it /// receives." pub const Codepoint = ecn.Codepoint; /// draft-ietf-quic-ack-frequency-14 §4: what the peer has asked of this /// end's acknowledgements, from the newest ACK_FREQUENCY frame it sent. pub const Frequency = struct { /// Acknowledge once more than this many ack-eliciting packets have /// arrived since the last ACK. threshold: u64 = 1, /// ...or once this long has passed since the first of them, in /// nanoseconds. max_delay: u64, /// The reordering that has an ACK sent at once; 0 for none, 1 for any. reordering: u64 = 1, }; pub const Tracker = struct { /// Newest first, disjoint, and never adjacent — two ranges that touch are /// merged, or the same set of packets would have two spellings and the /// ACK frames built from it would differ for no reason. ranges: [max_ranges]Range = undefined, len: usize = 0, /// Everything below this has been forgotten: the ranges that described /// it were dropped to make room. Whether a packet down there arrived /// before is no longer known, so `seen` assumes it did. floor: u64 = 0, /// Whether anything has arrived since the last ACK that obliges one. /// /// §13.2.1: only an ack-eliciting packet does. A packet carrying nothing /// but ACKs and PADDING is not acknowledged, because the acknowledgement /// would itself be a packet needing one. ack_eliciting_pending: bool = false, /// §13.4.1: "On receiving an IP packet with an ECT(0), ECT(1), or ECN-CE /// codepoint, an ECN-enabled endpoint accesses the ECN field and /// increases the corresponding ECT(0), ECT(1), or ECN-CE count." /// /// Per packet number space, because "each packet number space maintains /// separate acknowledgment state and separate ECN counts", and counted /// once per QUIC packet rather than once per datagram: coalesced packets /// "share the same IP header so the ECN counts are incremented once for /// each coalesced QUIC packet". ect0: u64 = 0, ect1: u64 = 0, ce: u64 = 0, /// Whether any marked packet has been seen in this space. /// /// What decides whether an ACK frame carries counts at all. Reporting /// zeros would be a claim -- §13.4.2.1 has a sender fail validation when /// "the corresponding ECN counts are not present", so counts that are /// present and zero say the path stripped the marking, which is a /// different statement from having nothing to report. marked: bool = false, // -- when to acknowledge, draft-ietf-quic-ack-frequency-14 §6 ------------- // // None of this changes anything until the peer sends an ACK_FREQUENCY // frame: without one, `due` says an ACK is owed the moment anything // ack-eliciting is, which is what this library has always done and is // RFC 9000 §13.2.1 at its most eager. /// Ack-eliciting packets since the last ACK went out, and when the first /// of them arrived. eliciting_since: u64 = 0, first_eliciting_at: ?u64 = null, /// When the largest packet number so far arrived, which an ACK's delay /// is measured from. largest_at: u64 = 0, /// The Largest Acknowledged of the last ACK sent, §6.2's Largest Acked. largest_acked_sent: ?u64 = null, /// Something has asked for an ACK now: reordering past the peer's /// threshold, a first CE mark, or an IMMEDIATE_ACK frame. immediate: bool = false, /// Whether the last packet was CE-marked, for §6.3's transition. last_ce: bool = false, /// Records a packet and decides whether it calls for an ACK sooner than /// the peer's ACK_FREQUENCY would otherwise allow. `frequency` is null /// until the peer has sent one. /// /// `ce` is whether the packet's datagram was marked Congestion /// Experienced, and `now` is in nanoseconds on whatever clock `due` and /// `delaySince` are later asked about. A duplicate neither adds to the /// count of ack-eliciting packets nor hurries an ACK, though `record` /// still notes that one is owed. ECN counts are separate: they are /// `countEcn`'s. pub fn arrived(t: *Tracker, number: u64, ack_eliciting: bool, ce: bool, now: u64, frequency: ?Frequency) void { const was_largest = t.largest(); const duplicate = t.contains(number); t.record(number, ack_eliciting); if (was_largest == null or number > was_largest.?) t.largest_at = now; const previous_ce = t.last_ce; t.last_ce = ce; if (!ack_eliciting or duplicate) return; t.eliciting_since += 1; if (t.first_eliciting_at == null) t.first_eliciting_at = now; const f = frequency orelse return; // §6.3: "If the Ack-Eliciting Threshold is larger than 1, an endpoint // SHOULD send an immediate acknowledgement when a packet marked with // the ECN Congestion Experienced (CE) codepoint in the IP header is // received and the previously received packet was not marked CE." // At 1 RFC 9000's rule applies, which is every CE mark; at 0 every // packet is acknowledged anyway. if (ce and (f.threshold == 1 or (f.threshold > 1 and !previous_ce))) t.immediate = true; if (t.outOfOrderDue(number, was_largest, f.reordering)) t.immediate = true; } test arrived { var t: Tracker = .{}; // The peer asked for an ACK every other packet, or after 25ms. const f: Frequency = .{ .threshold = 1, .max_delay = 25_000_000 }; t.arrived(0, true, false, 1_000, f); try testing.expect(!t.due(1_000, f)); t.arrived(1, true, false, 2_000, f); try testing.expect(t.due(2_000, f)); t.sent(); // Packet 3 skips 2, which RFC 9000's reordering rule answers at once. t.arrived(3, true, false, 3_000, f); try testing.expect(t.due(3_000, f)); } /// §6.2: whether packet `number`, just recorded, is reordering enough to /// be acknowledged at once. fn outOfOrderDue(t: *const Tracker, number: u64, was_largest: ?u64, reordering: u64) bool { // "A value of 0 indicates out-of-order packets do not elicit an // immediate ACK." if (reordering == 0) return false; const previous = was_largest orelse return false; // At 1, RFC 9000 §13.2.1: any packet that arrives below one already // seen, or leaves a gap below itself. if (reordering == 1) return number < previous or number > previous + 1; const largest_unacked = t.largest() orelse return false; // "The received packet number is less than or equal to Largest Acked // - Reordering Threshold": a packet the sender has probably already // declared lost, which it wants to hear about to undo that. if (t.largest_acked_sent) |acked| { if (acked >= reordering and number <= acked - reordering) return true; } // "The difference between the smallest Unreported Missing packet and // the Largest Unacked packet is greater than or equal to the // Reordering Threshold value." Unreported Missing are the gaps above // Largest Reported Missing, Largest Acked - Reordering Threshold; with // no ACK sent yet, every gap is unreported. var lo: u64 = if (t.largest_acked_sent) |acked| (if (acked >= reordering) acked - reordering + 1 else 0) else 0; if (t.len > 0) lo = @max(lo, t.ranges[t.len - 1].smallest); const missing = t.smallestMissingFrom(lo) orelse return false; if (missing >= largest_unacked) return false; return largest_unacked - missing >= reordering; } /// The smallest packet number at or above `from` that has not arrived. fn smallestMissingFrom(t: *const Tracker, from: u64) ?u64 { var candidate = from; var i = t.len; while (i > 0) { i -= 1; const r = t.ranges[i]; if (candidate < r.smallest) return candidate; if (candidate <= r.largest) candidate = r.largest + 1; } return candidate; } /// Whether an ACK is owed now. Without an ACK_FREQUENCY from the peer, /// whenever anything ack-eliciting is waiting; with one, once more than /// its threshold is, or its delay has run out, or something above has /// asked for one at once. pub fn due(t: *const Tracker, now: u64, frequency: ?Frequency) bool { if (!t.ack_eliciting_pending or t.len == 0) return false; const f = frequency orelse return true; if (t.immediate) return true; if (t.eliciting_since > f.threshold) return true; const first = t.first_eliciting_at orelse return true; return now >= first +| f.max_delay; } test due { var t: Tracker = .{}; // Nothing received, nothing owed. try testing.expect(!t.due(0, null)); // RFC 9000 on its own: an ack-eliciting packet is owed an ACK now. t.record(0, true); try testing.expect(t.due(0, null)); t.sent(); try testing.expect(!t.due(0, null)); } /// When an ACK being held back falls due, or null if none is: the /// moment the peer's `max_delay` runs out after the first ack-eliciting /// packet since the last ACK, for a timer to be armed at. /// /// Null, too, without an ACK_FREQUENCY, because then `due` holds nothing /// back. A threshold or reordering that makes an ACK due sooner is not /// reflected here; `due` is what says so. pub fn deadline(t: *const Tracker, frequency: ?Frequency) ?u64 { if (!t.ack_eliciting_pending) return null; const f = frequency orelse return null; const first = t.first_eliciting_at orelse return null; return first +| f.max_delay; } test deadline { var t: Tracker = .{}; const f: Frequency = .{ .threshold = 10, .max_delay = 25_000_000 }; try testing.expectEqual(@as(?u64, null), t.deadline(f)); t.arrived(0, true, false, 1_000_000, f); try testing.expectEqual(@as(?u64, 26_000_000), t.deadline(f)); } /// §19.3's ACK Delay before scaling: how long the largest packet has /// waited to be acknowledged, in nanoseconds. /// /// Measured from the time `arrived` was given for the largest packet /// number; a tracker fed through `record` alone has no such time, and /// measures from zero. pub fn delaySince(t: *const Tracker, now: u64) u64 { return now -| t.largest_at; } test delaySince { var t: Tracker = .{}; t.arrived(4, true, false, 10_000, null); // An older packet arriving later does not restart the clock. t.arrived(2, true, false, 15_000, null); try testing.expectEqual(@as(u64, 20_000), t.delaySince(30_000)); } /// §13.4.1: counts a processed packet against the codepoint its datagram /// carried. "ECN counts are only incremented when QUIC packets from the /// received IP packet are processed. As such, duplicate QUIC packets are /// not processed and do not increase ECN counts." pub fn countEcn(t: *Tracker, codepoint: Codepoint) void { switch (codepoint) { .not_ect => return, .ect0 => t.ect0 += 1, .ect1 => t.ect1 += 1, .ce => t.ce += 1, } t.marked = true; } test countEcn { var t: Tracker = .{}; t.record(0, true); t.countEcn(.not_ect); // Unmarked traffic leaves the ACK without counts at all. try testing.expect(!t.marked); t.countEcn(.ect0); t.countEcn(.ce); try testing.expect(t.marked); try testing.expectEqual(@as(u64, 1), t.ect0); try testing.expectEqual(@as(u64, 1), t.ce); } /// The largest packet number received, which is an ACK frame's Largest /// Acknowledged, or null before anything has arrived. pub fn largest(t: *const Tracker) ?u64 { return if (t.len == 0) null else t.ranges[0].largest; } test largest { var t: Tracker = .{}; try testing.expectEqual(@as(?u64, null), t.largest()); t.record(7, true); t.record(3, true); try testing.expectEqual(@as(?u64, 7), t.largest()); } /// Whether nothing has been received, so there is no ACK frame to /// write: `writeAck` asserts the opposite. pub fn isEmpty(t: *const Tracker) bool { return t.len == 0; } test isEmpty { var t: Tracker = .{}; try testing.expect(t.isEmpty()); // Even a packet that elicits nothing is acknowledged with the rest. t.record(0, false); try testing.expect(!t.isEmpty()); } /// Whether this packet number has already been received. /// /// §12.3 forbids a peer reusing a packet number, so a repeat is either a /// duplicate the network produced or a replay somebody produced. Either /// way §13.2 says not to process it twice, and this is the question that /// decides. pub fn contains(t: *const Tracker, number: u64) bool { for (t.ranges[0..t.len]) |r| { if (r.contains(number)) return true; // Sorted newest first, so once the ranges are below the number // there is nothing left to find. if (r.largest < number) return false; } return false; } test contains { var t: Tracker = .{}; t.record(10, true); try testing.expect(t.contains(10)); // A second copy is the one not to process. try testing.expect(!t.contains(11)); } /// Whether a packet with this number is to be treated as one already /// received: one the ranges hold, or one below what they still /// remember. /// /// §13.2.3 lets the ranges be forgotten, and §12.3 still wants a repeat /// kept from being processed twice. A packet older than everything /// remembered may be new, but it is just as likely a replay, and the /// cost of dropping a new one is a retransmission, not a wrong answer. pub fn seen(t: *const Tracker, number: u64) bool { return number < t.floor or t.contains(number); } test seen { var t: Tracker = .{}; // Every other packet number, so that each is a range of its own, // until the oldest has to be dropped. var n: u64 = 0; while (n < 2 * (max_ranges + 1)) : (n += 2) t.record(n, true); try testing.expect(t.seen(2 * max_ranges)); try testing.expect(!t.seen(2 * max_ranges + 1)); // Packet 0's range is gone, so 0 is assumed to have come before. // Packet 1 is still known never to have arrived: it would have // joined packet 0's range if it had. try testing.expect(!t.contains(0)); try testing.expect(t.seen(0)); try testing.expect(!t.seen(1)); try testing.expect(!t.seen(3)); } /// Notes that a packet arrived, adding its number to the ranges an ACK /// will describe. /// /// A duplicate leaves the ranges as they were, but an ack-eliciting one /// still marks an ACK as owed. When the ranges are full, the oldest is /// dropped (see the top of this file). This is the bare bookkeeping; /// `arrived` wraps it with draft-ietf-quic-ack-frequency-14's timing. pub fn record(t: *Tracker, number: u64, ack_eliciting: bool) void { if (ack_eliciting) t.ack_eliciting_pending = true; if (t.contains(number)) return; // Find where it belongs: the ranges are descending, so this is the // first one whose largest is below the new number. var i: usize = 0; while (i < t.len and t.ranges[i].smallest > number + 1) : (i += 1) {} // Extend downwards, upwards, or both -- and when it joins two ranges // that were only separated by it, merge them. if (i < t.len and t.ranges[i].smallest == number + 1) { t.ranges[i].smallest = number; t.mergeAt(i); return; } if (i < t.len and t.ranges[i].largest + 1 == number) { t.ranges[i].largest = number; if (i > 0) t.mergeAt(i - 1); return; } t.insertAt(i, .{ .smallest = number, .largest = number }); } test record { var t: Tracker = .{}; t.record(0, false); t.record(1, true); t.record(3, true); // Two ranges, newest first, with 2 missing between them. try testing.expectEqual(@as(usize, 2), t.len); try testing.expectEqual(Range{ .smallest = 3, .largest = 3 }, t.ranges[0]); try testing.expectEqual(Range{ .smallest = 0, .largest = 1 }, t.ranges[1]); try testing.expect(t.ack_eliciting_pending); } /// Joins range `i` with `i + 1` if they have become adjacent. fn mergeAt(t: *Tracker, i: usize) void { if (i + 1 >= t.len) return; if (t.ranges[i].smallest != t.ranges[i + 1].largest + 1) return; t.ranges[i].smallest = t.ranges[i + 1].smallest; for (i + 1..t.len - 1) |j| t.ranges[j] = t.ranges[j + 1]; t.len -= 1; } fn insertAt(t: *Tracker, i: usize, r: Range) void { if (t.len < max_ranges) { var j = t.len; while (j > i) : (j -= 1) t.ranges[j] = t.ranges[j - 1]; t.ranges[i] = r; t.len += 1; return; } // Full. The oldest range is the one to lose: it describes the packets // the peer is least likely to still be waiting to hear about, and a // range that is never acknowledged costs one retransmission. if (i >= max_ranges) { t.floor = @max(t.floor, r.largest + 1); return; } t.floor = @max(t.floor, t.ranges[max_ranges - 1].largest + 1); var j: usize = max_ranges - 1; while (j > i) : (j -= 1) t.ranges[j] = t.ranges[j - 1]; t.ranges[i] = r; } /// Writes an ACK frame describing everything received, as far as the /// bounded range list still remembers it, into the front of `out`, and /// returns how many octets it took. /// /// There has to be something to acknowledge, which is asserted (see /// `isEmpty`). Once any marked packet has been counted the frame is the /// ECN-carrying type 0x03 rather than 0x02. A frame that will not fit is /// `NoSpace`, never a shortened one. /// /// `delay` is the encoded ACK Delay — §19.3 scales it by the /// `ack_delay_exponent` transport parameter, which this layer does not /// know, so the caller has already done that division. /// /// Does not clear `ack_eliciting_pending`: whether an ACK has actually /// been *sent* is something only the thing putting packets on the wire /// knows, and a frame written into a packet that is then abandoned has /// not been sent. `sent` is how that is said. pub fn writeAck(t: *const Tracker, out: []u8, delay: u64) Error!usize { assert(t.len > 0); // The ranges beyond the first, encoded as §19.3.1's Gap and ACK Range // Length pairs. They have to be written before the frame header can // be, because the header carries the count. var ranges_buf: [max_ranges * 2 * 8]u8 = undefined; var ranges_at: usize = 0; var previous_smallest = t.ranges[0].smallest; for (t.ranges[1..t.len]) |r| { // §19.3.1: "largest = previous_smallest - gap - 2", so the gap is // two less than the distance between them. The ranges are // disjoint and never adjacent, so this cannot go negative. assert(previous_smallest > r.largest + 1); const gap = previous_smallest - r.largest - 2; ranges_at += varint.write(ranges_buf[ranges_at..], gap).len; ranges_at += varint.write(ranges_buf[ranges_at..], r.largest - r.smallest).len; previous_smallest = r.smallest; } const f: frame.Frame = .{ .ack = .{ .largest = t.ranges[0].largest, .delay = delay, .range_count = t.len - 1, .first_range = t.ranges[0].largest - t.ranges[0].smallest, .encoded_ranges = ranges_buf[0..ranges_at], // §13.4.1: "Even if an endpoint does not set an ECT field in // packets it sends, the endpoint MUST provide feedback about ECN // markings it receives, if these are accessible. Failing to // report the ECN counts will cause the sender to disable the use // of ECN for this connection." .ecn = if (t.marked) .{ .ect0 = t.ect0, .ect1 = t.ect1, .ce = t.ce } else null, }, }; return frame.write(out, f) catch error.NoSpace; } test writeAck { var t: Tracker = .{}; for ([_]u64{ 0, 1, 2, 5 }) |n| t.record(n, true); var buf: [64]u8 = undefined; const len = try t.writeAck(&buf, 0); const got = try frame.parse(buf[0..len]); // Largest 5, then one more range below the gap at 3 and 4. try testing.expectEqual(@as(u64, 5), got.frame.ack.largest); try testing.expectEqual(@as(u64, 1), got.frame.ack.range_count); } /// Notes that an ACK covering everything currently known has gone out, /// so that none is owed until something new arrives. /// /// Clears what `due` and `deadline` are decided by, and remembers the /// largest packet number acknowledged, which the draft's reordering rule /// measures from. The ranges themselves are kept: later ACKs describe /// them again until they fall off the end of the list. pub fn sent(t: *Tracker) void { t.ack_eliciting_pending = false; t.eliciting_since = 0; t.first_eliciting_at = null; t.immediate = false; t.largest_acked_sent = t.largest(); } test sent { var t: Tracker = .{}; t.record(0, true); var buf: [64]u8 = undefined; _ = try t.writeAck(&buf, 0); // Writing the frame is not sending it; only this says it went. try testing.expect(t.due(0, null)); t.sent(); try testing.expect(!t.due(0, null)); try testing.expect(t.contains(0)); } }; const testing = std.testing; test "packets arriving in order stay one range" { var t: Tracker = .{}; for (0..100) |n| t.record(n, true); try testing.expectEqual(@as(usize, 1), t.len); try testing.expectEqual(Range{ .smallest = 0, .largest = 99 }, t.ranges[0]); try testing.expectEqual(@as(?u64, 99), t.largest()); } test "a gap makes a second range, and filling it merges them back" { var t: Tracker = .{}; t.record(0, true); t.record(1, true); // 2 is missing. t.record(3, true); t.record(4, true); try testing.expectEqual(@as(usize, 2), t.len); try testing.expectEqual(Range{ .smallest = 3, .largest = 4 }, t.ranges[0]); try testing.expectEqual(Range{ .smallest = 0, .largest = 1 }, t.ranges[1]); // The retransmission arrives and the two become one, because a set of // packet numbers must have exactly one representation here. t.record(2, true); try testing.expectEqual(@as(usize, 1), t.len); try testing.expectEqual(Range{ .smallest = 0, .largest = 4 }, t.ranges[0]); } test "packets arriving backwards are still one range" { var t: Tracker = .{}; var n: u64 = 20; while (true) : (n -= 1) { t.record(n, true); if (n == 0) break; } try testing.expectEqual(@as(usize, 1), t.len); try testing.expectEqual(Range{ .smallest = 0, .largest = 20 }, t.ranges[0]); } test "a duplicate is recognised and changes nothing" { var t: Tracker = .{}; t.record(5, true); t.record(5, true); t.record(5, true); try testing.expectEqual(@as(usize, 1), t.len); try testing.expectEqual(Range{ .smallest = 5, .largest = 5 }, t.ranges[0]); try testing.expect(t.contains(5)); try testing.expect(!t.contains(4)); try testing.expect(!t.contains(6)); } test "the range list is bounded however many gaps a peer manufactures" { // Every other packet number, which is the cheapest way to make a range // per packet. Without a bound this is a peer choosing how much memory a // connection costs. var t: Tracker = .{}; var n: u64 = 0; while (n < 1000) : (n += 2) t.record(n, true); try testing.expectEqual(@as(usize, max_ranges), t.len); // What survives is the newest, which is what the peer is most likely to // still be waiting to hear about. try testing.expectEqual(@as(?u64, 998), t.largest()); try testing.expect(t.contains(998)); try testing.expect(!t.contains(0)); } test "only an ack-eliciting packet obliges an acknowledgement" { // §13.2.1. A packet of nothing but ACKs does not need one, because the // acknowledgement would be a packet needing one in its turn. var t: Tracker = .{}; t.record(0, false); try testing.expect(!t.ack_eliciting_pending); t.record(1, true); try testing.expect(t.ack_eliciting_pending); t.sent(); try testing.expect(!t.ack_eliciting_pending); } /// Reads an ACK frame back into the set of packet numbers it acknowledges. fn acknowledged(bytes: []const u8, out: *std.ArrayList(u64), gpa: std.mem.Allocator) !void { const got = try frame.parse(bytes); const a = got.frame.ack; const first = try a.firstRange(); var n = first.smallest; while (n <= first.largest) : (n += 1) try out.append(gpa, n); var it = a.ranges(); while (try it.next()) |r| { var m = r.smallest; while (m <= r.largest) : (m += 1) try out.append(gpa, m); } } test "an ACK frame describes exactly the packets that were received" { // The round trip that matters: what goes into the tracker comes out of // the frame, through an encoder and a decoder that share no code. const gpa = testing.allocator; const received = [_]u64{ 0, 1, 2, 5, 6, 9, 20, 21, 22, 100 }; var t: Tracker = .{}; for (received) |n| t.record(n, true); var buf: [256]u8 = undefined; const len = try t.writeAck(&buf, 0); var got: std.ArrayList(u64) = .empty; defer got.deinit(gpa); try acknowledged(buf[0..len], &got, gpa); std.mem.sort(u64, got.items, {}, std.sort.asc(u64)); try testing.expectEqualSlices(u64, &received, got.items); } test "an ACK of a single packet is the smallest frame there is" { var t: Tracker = .{}; t.record(0, true); var buf: [64]u8 = undefined; const len = try t.writeAck(&buf, 0); // Type, largest, delay, count, first range -- five octets and no ranges. try testing.expectEqualSlices(u8, &.{ 0x02, 0x00, 0x00, 0x00, 0x00 }, buf[0..len]); const got = try frame.parse(buf[0..len]); try testing.expectEqual(@as(u64, 0), got.frame.ack.largest); try testing.expectEqual(@as(u64, 0), got.frame.ack.range_count); } test "an ACK that will not fit says so rather than writing a short one" { var t: Tracker = .{}; var n: u64 = 0; while (n < 60) : (n += 2) t.record(n, true); var tiny: [8]u8 = undefined; try testing.expectError(error.NoSpace, t.writeAck(&tiny, 0)); } test "the ACK delay is carried through untouched" { // §19.3 scales it by a transport parameter this layer does not know, so // it is the caller's number and passes through unexamined. var t: Tracker = .{}; t.record(7, true); var buf: [64]u8 = undefined; const len = try t.writeAck(&buf, 12345); const got = try frame.parse(buf[0..len]); try testing.expectEqual(@as(u64, 12345), got.frame.ack.delay); } /// Feeds `numbers` through a tracker with only §6.2's reordering rule able to /// call for an ACK -- threshold and delay out of reach, as the draft's tables /// assume -- and says after which of them an ACK went out. fn reorderingAcks(reordering: u64, numbers: []const u64, out: []bool) void { var t: Tracker = .{}; const f: Frequency = .{ .threshold = 1000, .max_delay = std.math.maxInt(u64), .reordering = reordering }; for (numbers, 0..) |n, i| { t.arrived(n, true, false, 0, f); out[i] = t.due(0, f); if (out[i]) t.sent(); } } test "draft-ietf-quic-ack-frequency-14 6.2.1: Table 1, reordering threshold 3" { const received = [_]u64{ 0, 1, 3, 4, 5, 8, 9, 10 }; const expected = [_]bool{ false, false, false, false, true, false, true, true }; var got: [received.len]bool = undefined; reorderingAcks(3, &received, &got); try testing.expectEqualSlices(bool, &expected, &got); } test "draft-ietf-quic-ack-frequency-14 6.2.1: Table 2, reordering threshold 5" { const received = [_]u64{ 0, 1, 3, 5, 6, 7, 8, 9 }; const expected = [_]bool{ false, false, false, false, false, true, false, true }; var got: [received.len]bool = undefined; reorderingAcks(5, &received, &got); try testing.expectEqualSlices(bool, &expected, &got); } test "draft-ietf-quic-ack-frequency-14 6: more than the threshold, or the delay, and an ACK is due" { var t: Tracker = .{}; const f: Frequency = .{ .threshold = 3, .max_delay = 1000, .reordering = 0 }; // Without an ACK_FREQUENCY, every ack-eliciting packet is owed an ACK. t.arrived(0, true, false, 10, null); try testing.expect(t.due(10, null)); t.sent(); for (1..4) |n| { t.arrived(n, true, false, 20, f); try testing.expect(!t.due(20, f)); } t.arrived(4, true, false, 20, f); // the fourth: more than three try testing.expect(t.due(20, f)); t.sent(); // One packet, then the delay runs out. t.arrived(5, true, false, 100, f); try testing.expectEqual(@as(?u64, 1100), t.deadline(f)); try testing.expect(!t.due(1099, f)); try testing.expect(t.due(1100, f)); // A packet that elicits nothing starts no clock. t.sent(); t.arrived(6, false, false, 2000, f); try testing.expect(!t.due(1_000_000, f)); try testing.expect(t.deadline(f) == null); } test "draft-ietf-quic-ack-frequency-14 6.3: the first CE mark is acknowledged at once, not every one" { var t: Tracker = .{}; const f: Frequency = .{ .threshold = 10, .max_delay = std.math.maxInt(u64), .reordering = 0 }; t.arrived(0, true, false, 0, f); try testing.expect(!t.due(0, f)); t.arrived(1, true, true, 0, f); try testing.expect(t.due(0, f)); t.sent(); // Still CE: no transition, so nothing at once. t.arrived(2, true, true, 0, f); try testing.expect(!t.due(0, f)); } test "draft-ietf-quic-ack-frequency-14 4: a reordering threshold of zero never hurries an ACK" { var t: Tracker = .{}; const f: Frequency = .{ .threshold = 10, .max_delay = std.math.maxInt(u64), .reordering = 0 }; for ([_]u64{ 0, 5, 2, 9 }) |n| t.arrived(n, true, false, 0, f); try testing.expect(!t.due(0, f)); }