rat_congestion 0.1.0

High-performance congestion control algorithms for network protocols
Documentation
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! Performance constants and configuration values for congestion control.
//!
//! This module centralizes all the performance-critical constants used
//! throughout the congestion control algorithms.

use std::time::Duration;

/// Performance optimization constants.
pub mod perf {
    use std::time::Duration;
    
    /// Minimum RTT threshold for calculations (microseconds)
    pub const MIN_RTT_THRESHOLD_US: u64 = 1000;
    
    /// Maximum RTT threshold for calculations (microseconds)
    pub const MAX_RTT_THRESHOLD_US: u64 = 10_000_000; // 10 seconds
    
    /// Default initial congestion window (packets)
    pub const INITIAL_CWND: u64 = 10;
    
    /// Minimum congestion window (packets)
    pub const MIN_CWND: u64 = 2;
    
    /// Maximum congestion window (packets)
    pub const MAX_CWND: u64 = 0x7fff_ffff;
    
    /// Default packet size (bytes)
    pub const DEFAULT_MSS: u64 = 1460;
    
    /// Pacing gain precision multiplier
    pub const PACING_GAIN_PRECISION: u64 = 1000;
    
    /// Bandwidth estimation window size
    pub const BW_ESTIMATION_WINDOW: usize = 10;
    
    /// RTT estimation alpha (for EWMA)
    pub const RTT_ALPHA: f64 = 0.125;
    
    /// RTT variance beta (for EWMA)
    pub const RTT_BETA: f64 = 0.25;
    
    /// Minimum pacing rate (bytes per second)
    pub const MIN_PACING_RATE: u64 = 1000;
    
    /// Maximum pacing rate (bytes per second)
    pub const MAX_PACING_RATE: u64 = 100_000_000_000; // 100 Gbps
    
    /// Algorithm switching cooldown period
    pub const SWITCH_COOLDOWN: Duration = Duration::from_secs(5);
    
    /// High loss rate threshold for algorithm switching
    pub const HIGH_LOSS_THRESHOLD: f64 = 0.05; // 5%
    
    /// High RTT variation coefficient threshold
    pub const HIGH_RTT_CV_THRESHOLD: f64 = 0.3; // 30%
    
    /// Low bandwidth utilization threshold
    pub const LOW_BW_UTIL_THRESHOLD: f64 = 0.7; // 70%
    
    /// Batch processing size for ACKs
    pub const ACK_BATCH_SIZE: usize = 32;
    
    /// Memory alignment for performance-critical structures
    pub const CACHE_LINE_SIZE: usize = 64;
    
    /// Minimum update interval for algorithms
    pub const MIN_UPDATE_INTERVAL: Duration = Duration::from_millis(1);
    
    /// BBR bandwidth probe cycle duration
    pub const BW_PROBE_CYCLE: Duration = Duration::from_secs(10);
    
    /// BBR RTT probe duration
    pub const RTT_PROBE_DURATION: Duration = Duration::from_millis(200);
}

/// BBR algorithm constants.
pub mod bbr {
    use std::time::Duration;
    
    /// BBR gain cycle values (scaled by 1000 for integer arithmetic)
    pub const PACING_GAIN_CYCLE: [u64; 8] = [1250, 750, 1000, 1000, 1000, 1000, 1000, 1000];
    
    /// BBR startup gain
    pub const STARTUP_GAIN: u64 = 2885; // 2/ln(2) * 1000
    
    /// BBR drain gain
    pub const DRAIN_GAIN: u64 = 1000; // 1.0 * 1000
    
    /// BBR probe bandwidth gain
    pub const PROBE_BW_GAIN: u64 = 1000; // 1.0 * 1000
    
    /// BBR probe RTT gain
    pub const PROBE_RTT_GAIN: u64 = 1000; // 1.0 * 1000
    
    /// BBR cwnd gain
    pub const CWND_GAIN: u64 = 2000; // 2.0 * 1000
    
    /// Minimum time in ProbeRTT state
    pub const PROBE_RTT_DURATION: Duration = Duration::from_millis(200);
    
    /// Minimum interval between ProbeRTT states
    pub const PROBE_RTT_INTERVAL: Duration = Duration::from_secs(10);
    
    /// RTT window length for min RTT estimation
    pub const RTT_WINDOW_LENGTH: Duration = Duration::from_secs(10);
    
    /// Bandwidth window length for max bandwidth estimation
    pub const BW_WINDOW_LENGTH: Duration = Duration::from_secs(10);
    
    /// Number of rounds to stay in startup
    pub const STARTUP_ROUNDS: u32 = 3;
    
    /// Startup growth threshold (scaled by 1000)
    pub const STARTUP_GROWTH_THRESHOLD: u64 = 1250; // 1.25 * 1000
    
    /// Full bandwidth threshold (scaled by 1000)
    pub const FULL_BW_THRESHOLD: u64 = 1250; // 1.25 * 1000
    
    /// Full bandwidth count threshold
    pub const FULL_BW_COUNT: u32 = 3;
    
    /// Probe bandwidth cycle length
    pub const PROBE_BW_CYCLE_LEN: usize = 8;
    
    /// Extra ACKed bytes threshold for bandwidth estimation
    pub const EXTRA_ACKED_FILTER_LEN: usize = 10;
    
    /// Bandwidth estimation filter length
    pub const BW_FILTER_LEN: usize = 10;
    
    /// RTT estimation filter length
    pub const RTT_FILTER_LEN: usize = 10;
    
    /// Minimum congestion window in ProbeRTT
    pub const PROBE_RTT_CWND_PACKETS: u64 = 4;
    
    /// Beta for multiplicative decrease
    pub const BETA: u64 = 717; // 0.717 * 1000 (30% decrease)
    
    /// Pacing margin for burst tolerance
    pub const PACING_MARGIN_PERCENT: u64 = 1; // 1%
}

/// CUBIC algorithm constants.
pub mod cubic {
    /// CUBIC scaling factor C
    pub const C: f64 = 0.4;
    
    /// CUBIC beta (multiplicative decrease factor)
    pub const BETA: f64 = 0.7;
    
    /// Fast convergence factor
    pub const FAST_CONVERGENCE_FACTOR: f64 = 0.85;
    
    /// TCP friendliness factor
    pub const TCP_FRIENDLINESS: bool = true;
    
    /// Hystart enabled
    pub const HYSTART_ENABLED: bool = true;
    
    /// Hystart ACK train threshold
    pub const HYSTART_ACK_TRAIN_THRESHOLD: u32 = 2;
    
    /// Hystart delay threshold (microseconds)
    pub const HYSTART_DELAY_THRESHOLD_US: u64 = 2000;
    
    /// Hystart minimum samples
    pub const HYSTART_MIN_SAMPLES: u32 = 8;
    
    /// CUBIC epoch length (for time calculations)
    pub const EPOCH_LENGTH_US: u64 = 100_000; // 100ms
    
    /// Maximum increment per ACK in congestion avoidance
    pub const MAX_INCREMENT: f64 = 1.0;
    
    /// Minimum increment per ACK in congestion avoidance
    pub const MIN_INCREMENT: f64 = 0.01;
    
    /// Initial slow start threshold
    pub const INITIAL_SSTHRESH: u64 = 0x7fff_ffff;
    
    /// Minimum slow start threshold
    pub const MIN_SSTHRESH: u64 = 2;
    
    /// CUBIC root calculation precision
    pub const CUBIC_ROOT_PRECISION: f64 = 1e-6;
    
    /// Maximum CUBIC time value (seconds)
    pub const MAX_CUBIC_TIME: f64 = 1000.0;
    
    /// CUBIC time scale factor
    pub const TIME_SCALE: f64 = 1024.0;
    
    /// ACK ratio for TCP friendliness
    pub const ACK_RATIO: f64 = 1.0;
    
    /// Loss event threshold
    pub const LOSS_EVENT_THRESHOLD: u32 = 1;
}

/// Network condition thresholds for algorithm switching.
pub mod thresholds {
    /// High loss rate threshold (5%)
    pub const HIGH_LOSS_RATE: f64 = 0.05;
    
    /// Very high loss rate threshold (10%)
    pub const VERY_HIGH_LOSS_RATE: f64 = 0.10;
    
    /// Low loss rate threshold (0.1%)
    pub const LOW_LOSS_RATE: f64 = 0.001;
    
    /// High RTT variation coefficient (30%)
    pub const HIGH_RTT_VARIATION: f64 = 0.30;
    
    /// Low RTT variation coefficient (10%)
    pub const LOW_RTT_VARIATION: f64 = 0.10;
    
    /// High bandwidth utilization (90%)
    pub const HIGH_BW_UTILIZATION: f64 = 0.90;
    
    /// Low bandwidth utilization (70%)
    pub const LOW_BW_UTILIZATION: f64 = 0.70;
    
    /// Very low bandwidth utilization (50%)
    pub const VERY_LOW_BW_UTILIZATION: f64 = 0.50;
    
    /// High RTT threshold (100ms)
    pub const HIGH_RTT_THRESHOLD_US: u64 = 100_000;
    
    /// Low RTT threshold (10ms)
    pub const LOW_RTT_THRESHOLD_US: u64 = 10_000;
    
    /// Very high RTT threshold (500ms)
    pub const VERY_HIGH_RTT_THRESHOLD_US: u64 = 500_000;
    
    /// Stable network threshold (consecutive measurements)
    pub const STABLE_NETWORK_SAMPLES: usize = 10;
    
    /// Unstable network threshold (consecutive measurements)
    pub const UNSTABLE_NETWORK_SAMPLES: usize = 3;
}

/// Buffer and memory constants.
pub mod memory {
    /// Default socket buffer size
    pub const DEFAULT_SOCKET_BUFFER_SIZE: usize = 4 * 1024 * 1024; // 4MB
    
    /// Maximum socket buffer size
    pub const MAX_SOCKET_BUFFER_SIZE: usize = 128 * 1024 * 1024; // 128MB
    
    /// Minimum socket buffer size
    pub const MIN_SOCKET_BUFFER_SIZE: usize = 64 * 1024; // 64KB
    
    /// Packet buffer pool size
    pub const PACKET_BUFFER_POOL_SIZE: usize = 1024;
    
    /// Maximum packet size
    pub const MAX_PACKET_SIZE: usize = 9000; // Jumbo frame
    
    /// Standard MTU size
    pub const STANDARD_MTU: usize = 1500;
    
    /// Memory alignment for SIMD operations
    pub const SIMD_ALIGNMENT: usize = 32;
    
    /// Ring buffer size for metrics
    pub const METRICS_RING_BUFFER_SIZE: usize = 256;
}

/// Timing constants.
pub mod timing {
    use std::time::Duration;
    
    /// Minimum timer resolution
    pub const MIN_TIMER_RESOLUTION: Duration = Duration::from_micros(100);
    
    /// Maximum timer resolution
    pub const MAX_TIMER_RESOLUTION: Duration = Duration::from_millis(100);
    
    /// Default pacing timer resolution
    pub const PACING_TIMER_RESOLUTION: Duration = Duration::from_micros(500);
    
    /// ACK timeout base
    pub const ACK_TIMEOUT_BASE: Duration = Duration::from_millis(200);
    
    /// ACK timeout maximum
    pub const ACK_TIMEOUT_MAX: Duration = Duration::from_secs(60);
    
    /// Retransmission timeout minimum
    pub const RTO_MIN: Duration = Duration::from_millis(200);
    
    /// Retransmission timeout maximum
    pub const RTO_MAX: Duration = Duration::from_secs(60);
    
    /// Delayed ACK timeout
    pub const DELAYED_ACK_TIMEOUT: Duration = Duration::from_millis(40);
    
    /// Keep-alive interval
    pub const KEEP_ALIVE_INTERVAL: Duration = Duration::from_secs(7200); // 2 hours
    
    /// Connection idle timeout
    pub const IDLE_TIMEOUT: Duration = Duration::from_secs(300); // 5 minutes
}

/// Mathematical constants for calculations.
pub mod math {
    /// Natural logarithm of 2
    pub const LN_2: f64 = 0.6931471805599453;
    
    /// Square root of 2
    pub const SQRT_2: f64 = 1.4142135623730951;
    
    /// Euler's number
    pub const E: f64 = 2.718281828459045;
    
    /// Golden ratio
    pub const PHI: f64 = 1.618033988749895;
    
    /// Precision for floating point comparisons
    pub const EPSILON: f64 = 1e-9;
    
    /// Maximum safe integer for calculations
    pub const MAX_SAFE_INTEGER: u64 = 0x1f_ffff_ffff_ffff;
    
    /// Scaling factor for fixed-point arithmetic
    pub const FIXED_POINT_SCALE: u64 = 1_000_000;
    
    /// Maximum value for scaled integers
    pub const MAX_SCALED_VALUE: u64 = u64::MAX / FIXED_POINT_SCALE;
}

/// Feature flags and compile-time configuration.
pub mod features {
    /// Enable SIMD optimizations
    #[cfg(feature = "simd")]
    pub const SIMD_ENABLED: bool = true;
    #[cfg(not(feature = "simd"))]
    pub const SIMD_ENABLED: bool = false;
    
    /// Enable platform-specific optimizations
    #[cfg(feature = "platform-optimized")]
    pub const PLATFORM_OPTIMIZED: bool = true;
    #[cfg(not(feature = "platform-optimized"))]
    pub const PLATFORM_OPTIMIZED: bool = false;
    
    /// Enable BBR algorithm
    #[cfg(feature = "bbr")]
    pub const BBR_ENABLED: bool = true;
    #[cfg(not(feature = "bbr"))]
    pub const BBR_ENABLED: bool = false;
    
    /// Enable CUBIC algorithm
    #[cfg(feature = "cubic")]
    pub const CUBIC_ENABLED: bool = true;
    #[cfg(not(feature = "cubic"))]
    pub const CUBIC_ENABLED: bool = false;
    
    /// Enable debug logging
    #[cfg(debug_assertions)]
    pub const DEBUG_ENABLED: bool = true;
    #[cfg(not(debug_assertions))]
    pub const DEBUG_ENABLED: bool = false;
    
    /// Enable performance profiling
    #[cfg(feature = "profiling")]
    pub const PROFILING_ENABLED: bool = true;
    #[cfg(not(feature = "profiling"))]
    pub const PROFILING_ENABLED: bool = false;
}

/// Utility functions for working with constants.
pub mod utils {
    use super::*;
    
    /// Convert microseconds to Duration.
    #[inline(always)]
    pub const fn us_to_duration(us: u64) -> std::time::Duration {
        std::time::Duration::from_micros(us)
    }
    
    /// Convert Duration to microseconds.
    #[inline(always)]
    pub fn duration_to_us(duration: std::time::Duration) -> u64 {
        duration.as_micros() as u64
    }
    
    /// Scale a value by the pacing gain precision.
    #[inline(always)]
    pub const fn scale_pacing_gain(gain: u64) -> u64 {
        gain * perf::PACING_GAIN_PRECISION
    }
    
    /// Unscale a pacing gain value.
    #[inline(always)]
    pub const fn unscale_pacing_gain(scaled_gain: u64) -> u64 {
        scaled_gain / perf::PACING_GAIN_PRECISION
    }
    
    /// Clamp a value between min and max.
    #[inline(always)]
    pub fn clamp<T: PartialOrd>(value: T, min: T, max: T) -> T {
        if value < min {
            min
        } else if value > max {
            max
        } else {
            value
        }
    }
    
    /// Check if a loss rate is considered high.
    #[inline(always)]
    pub fn is_high_loss_rate(loss_rate: f64) -> bool {
        loss_rate >= thresholds::HIGH_LOSS_RATE
    }
    
    /// Check if RTT variation is considered high.
    #[inline(always)]
    pub fn is_high_rtt_variation(cv: f64) -> bool {
        cv >= thresholds::HIGH_RTT_VARIATION
    }
    
    /// Check if bandwidth utilization is considered low.
    #[inline(always)]
    pub fn is_low_bandwidth_utilization(utilization: f64) -> bool {
        utilization <= thresholds::LOW_BW_UTILIZATION
    }
    
    /// Calculate the next power of 2.
    #[inline(always)]
    pub const fn next_power_of_2(n: usize) -> usize {
        if n == 0 {
            1
        } else {
            1 << (64 - (n - 1).leading_zeros())
        }
    }
    
    /// Align a size to the cache line boundary.
    #[inline(always)]
    pub const fn align_to_cache_line(size: usize) -> usize {
        (size + perf::CACHE_LINE_SIZE - 1) & !(perf::CACHE_LINE_SIZE - 1)
    }
}