1use super::{
8 CongestionControlAlgorithm, CongestionSignals, CongestionWindow, State,
9 params::{Algorithm, VegasParams},
10 rtt::{ClockStall, RoundtripTimeEstimator},
11};
12use crate::Result;
13
14use tor_error::{error_report, internal};
15
16#[derive(Clone, Debug, Default)]
20pub(crate) struct BdpEstimator {
21 bdp: u32,
23}
24
25impl BdpEstimator {
26 fn get(&self) -> u32 {
28 self.bdp
29 }
30
31 fn update(
36 &mut self,
37 cwnd: &CongestionWindow,
38 rtt: &RoundtripTimeEstimator,
39 signals: &CongestionSignals,
40 ) {
41 if rtt.clock_stalled() {
43 self.bdp = if signals.channel_blocked {
44 cwnd.get()
47 .saturating_sub(signals.channel_outbound_size)
48 .max(cwnd.min())
49 } else {
50 cwnd.get()
51 };
52 } else {
53 let min_rtt_usec = rtt.min_rtt_usec().unwrap_or(u32::MAX);
59 let ewma_rtt_usec = rtt.ewma_rtt_usec().unwrap_or(u32::MAX);
60 self.bdp = cwnd
61 .get()
62 .saturating_mul(min_rtt_usec)
63 .saturating_div(ewma_rtt_usec);
64 }
65 }
66}
67
68#[derive(Clone, Debug)]
78pub(crate) struct Vegas {
79 params: VegasParams,
81 bdp: BdpEstimator,
84 cwnd: CongestionWindow,
87 num_cell_until_sendme: u32,
89 num_sendme_until_cwnd_update: u32,
92 num_sendme_per_cwnd: u32,
95 num_inflight: u32,
98 is_blocked_on_chan: bool,
102}
103
104impl Vegas {
105 pub(crate) fn new(params: VegasParams, state: &State, cwnd: CongestionWindow) -> Self {
107 Self {
108 params,
109 bdp: BdpEstimator::default(),
110 num_cell_until_sendme: cwnd.sendme_inc(),
111 num_inflight: 0,
112 num_sendme_per_cwnd: 0,
113 num_sendme_until_cwnd_update: cwnd.update_rate(state),
114 cwnd,
115 is_blocked_on_chan: false,
116 }
117 }
118}
119
120impl CongestionControlAlgorithm for Vegas {
121 fn uses_stream_sendme(&self) -> bool {
122 false
124 }
125
126 fn uses_xon_xoff(&self) -> bool {
127 true
128 }
129
130 fn is_next_cell_sendme(&self) -> bool {
131 self.num_inflight.is_multiple_of(self.cwnd.sendme_inc())
134 }
135
136 fn can_send(&self) -> bool {
137 self.num_inflight < self.cwnd.get()
138 }
139
140 fn cwnd(&self) -> Option<CongestionWindow> {
141 Some(self.cwnd)
142 }
143
144 fn sendme_received(
155 &mut self,
156 state: &mut State,
157 rtt: &mut RoundtripTimeEstimator,
158 signals: CongestionSignals,
159 clock_stall: ClockStall,
160 ) -> Result<()> {
161 if clock_stall == ClockStall::Detected {
163 self.num_inflight = self.num_inflight.saturating_sub(self.cwnd.sendme_inc());
165 return Ok(());
166 }
167
168 self.num_sendme_until_cwnd_update = self.num_sendme_until_cwnd_update.saturating_sub(1);
170 self.num_sendme_per_cwnd = self.num_sendme_per_cwnd.saturating_sub(1);
172
173 self.bdp.update(&self.cwnd, rtt, &signals);
180
181 if rtt.is_ready() {
184 if signals.channel_blocked {
185 if !self.is_blocked_on_chan {
188 self.num_sendme_until_cwnd_update = 0;
189 }
190 } else {
191 if self.is_blocked_on_chan {
194 self.num_sendme_until_cwnd_update = 0;
195 }
196 }
197 }
198 self.is_blocked_on_chan = signals.channel_blocked;
199
200 if !rtt.is_ready() && !self.is_blocked_on_chan {
202 debug_assert!(self.num_inflight >= self.cwnd.sendme_inc());
206 self.num_inflight = self.num_inflight.saturating_sub(self.cwnd.sendme_inc());
207 return Ok(());
208 }
209
210 let queue_use = self.cwnd.get().saturating_sub(self.bdp.get());
213
214 self.cwnd.eval_fullness(
216 self.num_inflight,
217 self.params.cwnd_full_gap(),
218 self.params.cwnd_full_min_pct().as_percent(),
219 );
220
221 if state.in_slow_start() {
223 if queue_use < self.params.cell_in_queue_params().gamma() && !self.is_blocked_on_chan {
224 if self.cwnd.is_full() {
226 let inc = self
228 .cwnd
229 .rfc3742_ss_inc(self.params.cell_in_queue_params().ss_cwnd_cap());
230
231 if (inc * self.cwnd.sendme_per_cwnd())
234 <= (self.cwnd.increment() * self.cwnd.increment_rate())
235 {
236 *state = State::Steady;
237 }
238 }
239 } else {
240 self.cwnd
242 .set(self.bdp.get() + self.params.cell_in_queue_params().gamma());
243 *state = State::Steady;
245 }
246
247 if self.cwnd.get() >= self.params.ss_cwnd_max() {
249 self.cwnd.set(self.params.ss_cwnd_max());
250 *state = State::Steady;
251 }
252 } else if self.num_sendme_until_cwnd_update == 0 {
253 if queue_use > self.params.cell_in_queue_params().delta() {
255 self.cwnd.set(
257 self.bdp.get() + self.params.cell_in_queue_params().delta()
258 - self.cwnd.increment(),
259 );
260 } else if queue_use > self.params.cell_in_queue_params().beta()
261 || self.is_blocked_on_chan
262 {
263 self.cwnd.dec();
265 } else if self.cwnd.is_full() && queue_use < self.params.cell_in_queue_params().alpha()
266 {
267 self.cwnd.inc();
269 }
270 }
271
272 if self.num_sendme_until_cwnd_update == 0 {
274 self.num_sendme_until_cwnd_update = self.cwnd.update_rate(state);
275 }
276 if self.num_sendme_per_cwnd == 0 {
277 self.num_sendme_per_cwnd = self.cwnd.sendme_per_cwnd();
278 }
279
280 if self.params.cwnd_full_per_cwnd() != 0 {
282 if self.num_sendme_per_cwnd == self.cwnd.sendme_per_cwnd() {
283 self.cwnd.reset_full();
284 }
285 } else if self.num_sendme_until_cwnd_update == self.cwnd.update_rate(state) {
286 self.cwnd.reset_full();
287 }
288
289 self.num_inflight = self.num_inflight.saturating_sub(self.cwnd.sendme_inc());
291 Ok(())
292 }
293
294 fn sendme_sent(&mut self) -> Result<()> {
295 self.num_cell_until_sendme = self.cwnd.sendme_inc();
297 Ok(())
298 }
299
300 fn data_received(&mut self) -> Result<bool> {
301 if self.num_cell_until_sendme == 0 {
302 error_report!(internal!("Congestion control unexptected data cell"), "");
306 return Ok(false);
307 }
308
309 self.num_cell_until_sendme = self.num_cell_until_sendme.saturating_sub(1);
311
312 Ok(self.num_cell_until_sendme == 0)
315 }
316
317 fn data_sent(&mut self) -> Result<()> {
318 self.num_inflight = self.num_inflight.saturating_add(1);
320 Ok(())
321 }
322
323 #[cfg(feature = "conflux")]
324 fn inflight(&self) -> Option<u32> {
325 Some(self.num_inflight)
326 }
327
328 #[cfg(test)]
329 fn send_window(&self) -> u32 {
330 self.cwnd.get()
331 }
332
333 fn algorithm(&self) -> Algorithm {
334 Algorithm::Vegas(self.params)
335 }
336}
337
338#[cfg(test)]
339pub(crate) mod test {
340 #![allow(clippy::bool_assert_comparison)]
342 #![allow(clippy::clone_on_copy)]
343 #![allow(clippy::dbg_macro)]
344 #![allow(clippy::mixed_attributes_style)]
345 #![allow(clippy::print_stderr)]
346 #![allow(clippy::print_stdout)]
347 #![allow(clippy::single_char_pattern)]
348 #![allow(clippy::unwrap_used)]
349 #![allow(clippy::unchecked_time_subtraction)]
350 #![allow(clippy::useless_vec)]
351 #![allow(clippy::needless_pass_by_value)]
352 #![allow(clippy::string_slice)] use std::collections::VecDeque;
356 use tor_units::Percentage;
357 use web_time_compat::{Duration, Instant, InstantExt};
358
359 use super::*;
360 use crate::congestion::{
361 params::VegasParamsBuilder,
362 test_utils::{new_cwnd, new_rtt_estimator},
363 };
364
365 impl Vegas {
366 pub(crate) fn set_inflight(&mut self, v: u32) {
368 self.num_inflight = v;
369 }
370 fn is_blocked_on_chan(&self) -> bool {
372 self.is_blocked_on_chan
373 }
374 fn set_is_blocked_on_chan(&mut self, v: bool) {
376 self.is_blocked_on_chan = v;
377 }
378 }
379
380 #[derive(Debug)]
383 struct TestVectorParams {
384 sent_usec_in: u64,
386 got_sendme_usec_in: u64,
387 or_conn_blocked_in: bool,
388 inflight_in: u32,
389 ewma_rtt_usec_out: u32,
391 min_rtt_usec_out: u32,
392 cwnd_out: u32,
393 in_slow_start_out: bool,
394 cwnd_full_out: bool,
395 blocked_chan_out: bool,
396 }
397
398 impl From<[u32; 10]> for TestVectorParams {
399 fn from(arr: [u32; 10]) -> Self {
400 Self {
401 sent_usec_in: u64::from(arr[0]),
402 got_sendme_usec_in: u64::from(arr[1]),
403 or_conn_blocked_in: arr[2] == 1,
404 inflight_in: arr[3],
405 ewma_rtt_usec_out: arr[4],
406 min_rtt_usec_out: arr[5],
407 cwnd_out: arr[6],
408 in_slow_start_out: arr[7] == 1,
409 cwnd_full_out: arr[8] == 1,
410 blocked_chan_out: arr[9] == 1,
411 }
412 }
413 }
414
415 struct VegasTest {
416 params: VecDeque<TestVectorParams>,
417 rtt: RoundtripTimeEstimator,
418 state: State,
419 vegas: Vegas,
420 }
421
422 impl VegasTest {
423 fn new(vec: Vec<[u32; 10]>) -> Self {
424 let mut params = VecDeque::new();
425 for values in vec {
426 params.push_back(values.into());
427 }
428 let state = State::default();
429 Self {
430 params,
431 rtt: new_rtt_estimator(),
432 vegas: Vegas::new(build_vegas_params(), &state, new_cwnd()),
433 state,
434 }
435 }
436
437 fn run_once(&mut self, p: &TestVectorParams) {
438 eprintln!("Testing vector: {:?}", p);
439 self.vegas.set_inflight(p.inflight_in);
441 self.vegas.set_is_blocked_on_chan(p.or_conn_blocked_in);
442
443 let now = Instant::get();
444 self.rtt
445 .expect_sendme(now + Duration::from_micros(p.sent_usec_in));
446 let ret = self.rtt.update(
447 now + Duration::from_micros(p.got_sendme_usec_in),
448 &self.state,
449 &self.vegas.cwnd().expect("No CWND"),
450 );
451 assert!(ret.is_ok());
452
453 let signals = CongestionSignals::new(p.or_conn_blocked_in, 0);
454 let clock_stall = ClockStall::NotDetected;
455 let ret =
456 self.vegas
457 .sendme_received(&mut self.state, &mut self.rtt, signals, clock_stall);
458 assert!(ret.is_ok());
459
460 assert_eq!(self.rtt.ewma_rtt_usec().unwrap(), p.ewma_rtt_usec_out);
461 assert_eq!(self.rtt.min_rtt_usec().unwrap(), p.min_rtt_usec_out);
462 assert_eq!(self.vegas.cwnd().expect("No CWND").get(), p.cwnd_out);
463 assert_eq!(
464 self.vegas.cwnd().expect("No CWND").is_full(),
465 p.cwnd_full_out
466 );
467 assert_eq!(self.state.in_slow_start(), p.in_slow_start_out);
468 assert_eq!(self.vegas.is_blocked_on_chan(), p.blocked_chan_out);
469 }
470
471 fn run(&mut self) {
472 while let Some(param) = self.params.pop_front() {
473 self.run_once(¶m);
474 }
475 }
476 }
477
478 pub(crate) fn build_vegas_params() -> VegasParams {
479 const OUTBUF_CELLS: u32 = 62;
480 VegasParamsBuilder::default()
481 .cell_in_queue_params(
482 (
483 3 * OUTBUF_CELLS, 4 * OUTBUF_CELLS, 5 * OUTBUF_CELLS, 3 * OUTBUF_CELLS, 600, )
489 .into(),
490 )
491 .ss_cwnd_max(5_000)
492 .cwnd_full_gap(4)
493 .cwnd_full_min_pct(Percentage::new(25))
494 .cwnd_full_per_cwnd(1)
495 .build()
496 .expect("Unable to build Vegas parameters")
497 }
498
499 #[test]
500 fn test_vectors() {
501 let vec1 = vec![
502 [100000, 200000, 0, 124, 100000, 100000, 155, 1, 0, 0],
503 [200000, 300000, 0, 155, 100000, 100000, 186, 1, 1, 0],
504 [350000, 500000, 0, 186, 133333, 100000, 217, 1, 1, 0],
505 [500000, 550000, 0, 217, 77777, 77777, 248, 1, 1, 0],
506 [600000, 700000, 0, 248, 92592, 77777, 279, 1, 1, 0],
507 [700000, 750000, 0, 279, 64197, 64197, 310, 1, 0, 0], [750000, 875000, 0, 310, 104732, 64197, 341, 1, 1, 0],
509 [875000, 900000, 0, 341, 51577, 51577, 372, 1, 1, 0],
510 [900000, 950000, 0, 279, 50525, 50525, 403, 1, 1, 0],
511 [950000, 1000000, 0, 279, 50175, 50175, 434, 1, 1, 0],
512 [1000000, 1050000, 0, 279, 50058, 50058, 465, 1, 1, 0],
513 [1050000, 1100000, 0, 279, 50019, 50019, 496, 1, 1, 0],
514 [1100000, 1150000, 0, 279, 50006, 50006, 527, 1, 1, 0],
515 [1150000, 1200000, 0, 279, 50002, 50002, 558, 1, 1, 0],
516 [1200000, 1250000, 0, 550, 50000, 50000, 589, 1, 1, 0],
517 [1250000, 1300000, 0, 550, 50000, 50000, 620, 1, 0, 0], [1300000, 1350000, 0, 550, 50000, 50000, 635, 1, 1, 0],
519 [1350000, 1400000, 0, 550, 50000, 50000, 650, 1, 1, 0],
520 [1400000, 1450000, 0, 150, 50000, 50000, 650, 1, 0, 0], [1450000, 1500000, 0, 150, 50000, 50000, 650, 1, 0, 0], [1500000, 1550000, 0, 550, 50000, 50000, 664, 1, 1, 0], [1500000, 1600000, 0, 550, 83333, 50000, 584, 0, 1, 0], [1600000, 1650000, 0, 550, 61111, 50000, 585, 0, 1, 0], [1650000, 1700000, 0, 550, 53703, 50000, 586, 0, 1, 0],
526 [1700000, 1750000, 0, 100, 51234, 50000, 586, 0, 0, 0], [1750000, 1900000, 0, 100, 117078, 50000, 559, 0, 0, 0], [1900000, 2000000, 0, 100, 105692, 50000, 558, 0, 0, 0], [2000000, 2075000, 0, 500, 85230, 50000, 558, 0, 1, 0], [2075000, 2125000, 1, 500, 61743, 50000, 557, 0, 1, 1], [2125000, 2150000, 0, 500, 37247, 37247, 558, 0, 1, 0], [2150000, 2350000, 0, 500, 145749, 37247, 451, 0, 1, 0], ];
534 VegasTest::new(vec1).run();
535
536 let vec2 = vec![
537 [100000, 200000, 0, 124, 100000, 100000, 155, 1, 0, 0],
538 [200000, 300000, 0, 155, 100000, 100000, 186, 1, 1, 0],
539 [350000, 500000, 0, 186, 133333, 100000, 217, 1, 1, 0],
540 [500000, 550000, 1, 217, 77777, 77777, 403, 0, 1, 1], [600000, 700000, 0, 248, 92592, 77777, 404, 0, 1, 0], [700000, 750000, 1, 404, 64197, 64197, 403, 0, 0, 1], [750000, 875000, 0, 403, 104732, 64197, 404, 0, 1, 0],
544 ];
545 VegasTest::new(vec2).run();
546
547 let vec3 = vec![
548 [18258527, 19002938, 0, 83, 744411, 744411, 155, 1, 0, 0],
549 [18258580, 19254257, 0, 52, 911921, 744411, 186, 1, 1, 0],
550 [20003224, 20645298, 0, 164, 732023, 732023, 217, 1, 1, 0],
551 [20003367, 21021444, 0, 133, 922725, 732023, 248, 1, 1, 0],
552 [20003845, 21265508, 0, 102, 1148683, 732023, 279, 1, 1, 0],
553 [20003975, 21429157, 0, 71, 1333015, 732023, 310, 1, 0, 0],
554 [20004309, 21707677, 0, 40, 1579917, 732023, 310, 1, 0, 0],
555 ];
556 VegasTest::new(vec3).run();
557
558 let vec4 = vec![
559 [358297091, 358854163, 0, 83, 557072, 557072, 155, 1, 0, 0],
560 [358297649, 359123845, 0, 52, 736488, 557072, 186, 1, 1, 0],
561 [359492879, 359995330, 0, 186, 580463, 557072, 217, 1, 1, 0],
562 [359493043, 360489243, 0, 217, 857621, 557072, 248, 1, 1, 0],
563 [359493232, 360489673, 0, 248, 950167, 557072, 279, 1, 1, 0],
564 [359493795, 360489971, 0, 279, 980839, 557072, 310, 1, 0, 0],
565 [359493918, 360490248, 0, 310, 991166, 557072, 341, 1, 1, 0],
566 [359494029, 360716465, 0, 341, 1145346, 557072, 372, 1, 1, 0],
567 [359996888, 360948867, 0, 372, 1016434, 557072, 403, 1, 1, 0],
568 [359996979, 360949330, 0, 403, 973712, 557072, 434, 1, 1, 0],
569 [360489528, 361113615, 0, 434, 740628, 557072, 465, 1, 1, 0],
570 [360489656, 361281604, 0, 465, 774841, 557072, 496, 1, 1, 0],
571 [360489837, 361500461, 0, 496, 932029, 557072, 482, 0, 1, 0],
572 [360489963, 361500631, 0, 482, 984455, 557072, 482, 0, 1, 0],
573 [360490117, 361842481, 0, 482, 1229727, 557072, 481, 0, 1, 0],
574 ];
575 VegasTest::new(vec4).run();
576 }
577}