Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
ecn_tracker.cpp
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
6
8{
9
10auto ecn_result_to_string(ecn_result result) noexcept -> const char*
11{
12 switch (result)
13 {
15 return "none";
17 return "congestion_signal";
19 return "ecn_failure";
20 default:
21 return "unknown";
22 }
23}
24
26 : state_{}
27{
28 // Start in testing mode
29 state_.testing = true;
30 state_.capable = false;
31 state_.failed = false;
32}
33
35 uint64_t packets_acked,
36 std::chrono::steady_clock::time_point sent_time)
37 -> ecn_result
38{
39 // If ECN has already failed, don't process further
40 if (state_.failed)
41 {
43 }
44
45 // RFC 9000 Section 13.4.2.1: Verify ECN counts are valid
46 // ECN counts must be monotonically increasing
47 if (counts.ect0 < state_.counts.ect0 ||
48 counts.ect1 < state_.counts.ect1 ||
49 counts.ecn_ce < state_.counts.ecn_ce)
50 {
51 // ECN counts decreased - this is invalid, disable ECN
52 state_.failed = true;
53 state_.testing = false;
54 state_.capable = false;
56 }
57
58 // Validate ECN if still in testing phase
59 if (state_.testing)
60 {
61 if (!validate_ecn(counts, packets_acked))
62 {
63 state_.failed = true;
64 state_.testing = false;
65 state_.capable = false;
67 }
68
69 // Check if we have enough data to complete validation
70 uint64_t total_marks = counts.ect0 + counts.ect1 + counts.ecn_ce;
71 if (total_marks >= validation_state::kValidationThreshold)
72 {
73 state_.testing = false;
74 state_.capable = true;
75 }
76 }
77
78 // Check for ECN-CE increase (congestion signal)
79 // RFC 9002 Section 7.1: ECN-CE marks indicate congestion
81 if (counts.ecn_ce > state_.counts.ecn_ce)
82 {
83 // ECN-CE count increased - congestion experienced
85 state_.last_congestion_sent_time = sent_time;
86 }
87
88 // Update stored counts
89 state_.counts = counts;
90
91 return result;
92}
93
95 uint64_t packets_acked) -> bool
96{
97 // RFC 9000 Section 13.4.2.1:
98 // The total increase in ECT(0), ECT(1), and ECN-CE counts MUST be
99 // at least the number of newly acknowledged packets that were
100 // originally sent with an ECT codepoint.
101
102 uint64_t total_increase =
103 (counts.ect0 - state_.counts.ect0) +
104 (counts.ect1 - state_.counts.ect1) +
105 (counts.ecn_ce - state_.counts.ecn_ce);
106
107 // We sent packets with ECT(0) marking, so we expect the ECN counts
108 // to reflect at least those packets
109 if (total_increase < packets_acked && state_.packets_sent_with_ect > 0)
110 {
111 // ECN counts don't account for all packets - ECN is being stripped
112 // by the network path, or the peer doesn't support ECN properly
113 return false;
114 }
115
116 return true;
117}
118
119auto ecn_tracker::on_packets_sent(uint64_t packet_count) noexcept -> void
120{
121 if (!state_.failed)
122 {
123 state_.packets_sent_with_ect += packet_count;
124 }
125}
126
127auto ecn_tracker::reset() noexcept -> void
128{
130 state_.testing = true;
131}
132
133auto ecn_tracker::disable() noexcept -> void
134{
135 state_.failed = true;
136 state_.testing = false;
137 state_.capable = false;
138}
139
140} // namespace kcenon::network::protocols::quic
auto validate_ecn(const ecn_counts &counts, uint64_t packets_acked) -> bool
Validate ECN capability based on received counts.
validation_state state_
ECN validation state.
auto reset() noexcept -> void
Reset ECN tracker to initial state.
auto process_ecn_counts(const ecn_counts &counts, uint64_t packets_acked, std::chrono::steady_clock::time_point sent_time) -> ecn_result
Process received ECN counts from an ACK_ECN frame.
auto on_packets_sent(uint64_t packet_count) noexcept -> void
Record packets sent with ECN marking.
auto disable() noexcept -> void
Disable ECN tracking.
auto ecn_result_to_string(ecn_result result) noexcept -> const char *
Convert ecn_result to string.
ecn_result
Result of ECN counts processing (RFC 9000 Section 13.4)
Definition ecn_tracker.h:20
@ ecn_failure
ECN validation failed, should disable ECN.
@ congestion_signal
ECN-CE increased (congestion experienced)
ECN counts for ACK_ECN frames.
bool capable
True if ECN capability has been validated.