Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
loss_detector.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
7#include "ecn_tracker.h"
8#include "frame_types.h"
9#include "keys.h"
10#include "rtt_estimator.h"
11
12#include <chrono>
13#include <cstdint>
14#include <map>
15#include <optional>
16#include <vector>
17
19{
20
26{
28 uint64_t packet_number{0};
29
31 std::chrono::steady_clock::time_point sent_time;
32
34 size_t sent_bytes{0};
35
37 bool ack_eliciting{false};
38
40 bool in_flight{false};
41
44
46 std::vector<frame> frames;
47};
48
54{
55 none,
58};
59
65{
68
70 std::vector<sent_packet> acked_packets;
71
73 std::vector<sent_packet> lost_packets;
74
77
80 std::chrono::steady_clock::time_point ecn_congestion_sent_time{};
81};
82
91{
92public:
97 explicit loss_detector(rtt_estimator& rtt);
98
103 auto on_packet_sent(const sent_packet& packet) -> void;
104
112 [[nodiscard]] auto on_ack_received(const ack_frame& ack,
113 encryption_level level,
114 std::chrono::steady_clock::time_point recv_time)
116
121 [[nodiscard]] auto next_timeout() const
122 -> std::optional<std::chrono::steady_clock::time_point>;
123
128 [[nodiscard]] auto on_timeout() -> loss_detection_result;
129
135 [[nodiscard]] auto largest_acked(encryption_level level) const noexcept -> uint64_t;
136
141 [[nodiscard]] auto pto_count() const noexcept -> uint32_t
142 {
143 return pto_count_;
144 }
145
149 auto reset_pto_count() noexcept -> void
150 {
151 pto_count_ = 0;
152 }
153
159 [[nodiscard]] auto has_unacked_packets(encryption_level level) const -> bool;
160
166 [[nodiscard]] auto bytes_in_flight(encryption_level level) const -> size_t;
167
172 [[nodiscard]] auto total_bytes_in_flight() const -> size_t;
173
178 auto discard_space(encryption_level level) -> void;
179
184 auto set_handshake_confirmed(bool confirmed) noexcept -> void
185 {
186 handshake_confirmed_ = confirmed;
187 }
188
193 [[nodiscard]] auto get_ecn_tracker() noexcept -> class ecn_tracker&
194 {
195 return ecn_tracker_;
196 }
197
202 [[nodiscard]] auto get_ecn_tracker() const noexcept -> const class ecn_tracker&
203 {
204 return ecn_tracker_;
205 }
206
207private:
213 {
215 uint64_t largest_acked{0};
216
218 bool largest_acked_set{false};
219
221 std::chrono::steady_clock::time_point time_of_last_ack_eliciting;
222
224 std::map<uint64_t, sent_packet> sent_packets;
225
227 std::chrono::steady_clock::time_point loss_time;
228
231 };
232
234 [[nodiscard]] auto space_index(encryption_level level) const noexcept -> size_t;
235
237 [[nodiscard]] auto detect_lost_packets(encryption_level level,
238 std::chrono::steady_clock::time_point now)
239 -> std::vector<sent_packet>;
240
242 auto set_loss_detection_timer() -> void;
243
245 [[nodiscard]] auto get_loss_time_and_space() const
246 -> std::pair<std::chrono::steady_clock::time_point, encryption_level>;
247
249 [[nodiscard]] auto get_pto_time_and_space() const
250 -> std::pair<std::chrono::steady_clock::time_point, encryption_level>;
251
254
257
259 std::array<space_state, 3> spaces_;
260
262 uint32_t pto_count_{0};
263
266
268 std::chrono::steady_clock::time_point loss_detection_timer_;
269
271 bool timer_armed_{false};
272
274 static constexpr uint32_t kPacketThreshold = 3;
275
277 static constexpr double kTimeThreshold = 9.0 / 8.0; // 1.125
278
280 static constexpr auto kGranularity = std::chrono::milliseconds{1};
281};
282
283} // namespace kcenon::network::protocols::quic
ECN (Explicit Congestion Notification) tracker (RFC 9000 Section 13.4, RFC 9002 Section 7....
Definition ecn_tracker.h:52
QUIC loss detection (RFC 9002 Section 6)
auto has_unacked_packets(encryption_level level) const -> bool
Check if there are any unacked packets in the given space.
bool handshake_confirmed_
True if the handshake is confirmed.
auto set_handshake_confirmed(bool confirmed) noexcept -> void
Set handshake confirmed status.
auto on_timeout() -> loss_detection_result
Handle timeout expiry (RFC 9002 Section 6.2)
auto total_bytes_in_flight() const -> size_t
Get total bytes in flight across all spaces.
auto reset_pto_count() noexcept -> void
Reset PTO count to zero.
auto space_index(encryption_level level) const noexcept -> size_t
Get space index from encryption level.
loss_detector(rtt_estimator &rtt)
Constructor.
std::chrono::steady_clock::time_point loss_detection_timer_
Scheduled loss detection timeout.
auto bytes_in_flight(encryption_level level) const -> size_t
Get bytes in flight for a packet number space.
auto set_loss_detection_timer() -> void
Set loss detection timer (RFC 9002 Section 6.2)
auto pto_count() const noexcept -> uint32_t
Get the current PTO count.
auto discard_space(encryption_level level) -> void
Discard packet number space (e.g., after handshake keys discarded)
auto get_ecn_tracker() const noexcept -> const class ecn_tracker &
Get the ECN tracker (const)
auto on_packet_sent(const sent_packet &packet) -> void
Record a sent packet.
auto get_pto_time_and_space() const -> std::pair< std::chrono::steady_clock::time_point, encryption_level >
Get PTO time and space (RFC 9002 Appendix A.8)
static constexpr auto kGranularity
Timer granularity (RFC 9002 Section 6.1.2)
auto on_ack_received(const ack_frame &ack, encryption_level level, std::chrono::steady_clock::time_point recv_time) -> loss_detection_result
Process received ACK frame (RFC 9002 Section 6)
rtt_estimator & rtt_
Reference to RTT estimator.
class ecn_tracker ecn_tracker_
ECN tracker for ECN feedback processing.
bool timer_armed_
True if loss detection timer is armed.
auto detect_lost_packets(encryption_level level, std::chrono::steady_clock::time_point now) -> std::vector< sent_packet >
Detect lost packets (RFC 9002 Section 6.1)
auto next_timeout() const -> std::optional< std::chrono::steady_clock::time_point >
Get the time of the next scheduled timeout.
static constexpr uint32_t kPacketThreshold
Packet threshold (RFC 9002 Section 6.1.1)
uint32_t pto_count_
Number of times PTO has expired without receiving an ACK.
static constexpr double kTimeThreshold
Time threshold multiplier (RFC 9002 Section 6.1.2)
auto get_loss_time_and_space() const -> std::pair< std::chrono::steady_clock::time_point, encryption_level >
Get loss time and space (RFC 9002 Appendix A.8)
auto get_ecn_tracker() noexcept -> class ecn_tracker &
Get the ECN tracker.
std::array< space_state, 3 > spaces_
Per packet-number-space state (Initial, Handshake, Application)
auto largest_acked(encryption_level level) const noexcept -> uint64_t
Get the largest acknowledged packet number for a space.
QUIC packet number utilities (RFC 9000 Section 17.1)
Definition packet.h:174
RTT estimation for QUIC (RFC 9002 Section 5)
encryption_level
QUIC encryption levels (RFC 9001 Section 4)
Definition keys.h:54
@ initial
Initial encryption (derived from DCID)
ecn_result
Result of ECN counts processing (RFC 9000 Section 13.4)
Definition ecn_tracker.h:20
loss_detection_event
Events that can occur during loss detection.
ACK frame (RFC 9000 Section 19.3)
ecn_result ecn_signal
ECN signal from ACK_ECN frame processing.
std::chrono::steady_clock::time_point ecn_congestion_sent_time
Sent time of the packet that triggered ECN congestion signal (used for congestion recovery tracking)
std::vector< sent_packet > acked_packets
Packets that were acknowledged.
std::vector< sent_packet > lost_packets
Packets that were declared lost.
Per packet-number-space state (RFC 9002 Appendix A.1)
bool largest_acked_set
True if we've received any ACK in this space.
std::chrono::steady_clock::time_point loss_time
Time at which loss detection should be triggered.
std::map< uint64_t, sent_packet > sent_packets
Sent packets awaiting acknowledgment.
std::chrono::steady_clock::time_point time_of_last_ack_eliciting
Time of the most recent ack-eliciting packet.
uint64_t largest_acked
Largest acknowledged packet number.
size_t bytes_in_flight
Bytes in flight for this space.
Information about a sent packet for loss detection (RFC 9002 Section A.1.1)
encryption_level level
Encryption level of the packet.
std::chrono::steady_clock::time_point sent_time
Time the packet was sent.
size_t sent_bytes
Number of bytes in the packet.
bool in_flight
True if the packet is in flight (counted for congestion control)
bool ack_eliciting
True if this packet is ack-eliciting.
std::vector< frame > frames
Frames included in this packet (for retransmission)