Debian Codegraph @

contrib/libngtcp2/ngtcp2/lib/ngtcp2_bbr.c

raw symbols SCIP
1 /*
2 * ngtcp2
3 *
4 * Copyright (c) 2021 ngtcp2 contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #include "ngtcp2_bbr.h"
26
27 #include <assert.h>
28 #include <string.h>
29
30 #include "ngtcp2_log.h"
31 #include "ngtcp2_macro.h"
32 #include "ngtcp2_mem.h"
33 #include "ngtcp2_rcvry.h"
34 #include "ngtcp2_rst.h"
35 #include "ngtcp2_conn_stat.h"
36 #include "ngtcp2_pcg.h"
37
38 #define NGTCP2_BBR_MAX_BW_FILTERLEN 2
39
40 #define NGTCP2_BBR_EXTRA_ACKED_FILTERLEN 10
41
42 #define NGTCP2_BBR_STARTUP_PACING_GAIN_H 277
43 #define NGTCP2_BBR_DRAIN_PACING_GAIN_H 50
44
45 #define NGTCP2_BBR_DEFAULT_CWND_GAIN_H 200
46
47 #define NGTCP2_BBR_PROBE_RTT_CWND_GAIN_H 50
48
49 #define NGTCP2_BBR_BETA_NUMER 7
50 #define NGTCP2_BBR_BETA_DENOM 10
51
52 #define NGTCP2_BBR_LOSS_THRESH_NUMER 2
53 #define NGTCP2_BBR_LOSS_THRESH_DENOM 100
54
55 #define NGTCP2_BBR_HEADROOM_NUMER 15
56 #define NGTCP2_BBR_HEADROOM_DENOM 100
57
58 #define NGTCP2_BBR_PROBE_RTT_INTERVAL (5 * NGTCP2_SECONDS)
59 #define NGTCP2_BBR_MIN_RTT_FILTERLEN (10 * NGTCP2_SECONDS)
60
61 #define NGTCP2_BBR_PROBE_RTT_DURATION (200 * NGTCP2_MILLISECONDS)
62
63 #define NGTCP2_BBR_PACING_MARGIN_PERCENT 1
64
65 static void bbr_on_init(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
66 ngtcp2_tstamp initial_ts);
67
68 static void bbr_on_transmit(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
69 ngtcp2_tstamp ts);
70
71 static void bbr_reset_congestion_signals(ngtcp2_cc_bbr *bbr);
72
73 static void bbr_reset_shortterm_model(ngtcp2_cc_bbr *bbr);
74
75 static void bbr_init_round_counting(ngtcp2_cc_bbr *bbr);
76
77 static void bbr_reset_full_bw(ngtcp2_cc_bbr *bbr);
78
79 static void bbr_init_pacing_rate(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat);
80
81 static void bbr_set_pacing_rate_with_gain(ngtcp2_cc_bbr *bbr,
82 ngtcp2_conn_stat *cstat,
83 uint64_t pacing_gain_h);
84
85 static void bbr_set_pacing_rate(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat);
86
87 static void bbr_enter_startup(ngtcp2_cc_bbr *bbr);
88
89 static void bbr_check_startup_done(ngtcp2_cc_bbr *bbr);
90
91 static void bbr_update_on_ack(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
92 const ngtcp2_cc_ack *ack, ngtcp2_tstamp ts);
93
94 static void bbr_update_model_and_state(ngtcp2_cc_bbr *cc,
95 ngtcp2_conn_stat *cstat,
96 const ngtcp2_cc_ack *ack,
97 ngtcp2_tstamp ts);
98
99 static void bbr_update_control_parameters(ngtcp2_cc_bbr *cc,
100 ngtcp2_conn_stat *cstat,
101 const ngtcp2_cc_ack *ack);
102
103 static void bbr_update_on_loss(ngtcp2_cc_bbr *cc, ngtcp2_conn_stat *cstat,
104 const ngtcp2_cc_pkt *pkt, ngtcp2_tstamp ts);
105
106 static void bbr_update_latest_delivery_signals(ngtcp2_cc_bbr *bbr,
107 ngtcp2_conn_stat *cstat);
108
109 static void bbr_advance_latest_delivery_signals(ngtcp2_cc_bbr *bbr,
110 ngtcp2_conn_stat *cstat);
111
112 static void bbr_update_congestion_signals(ngtcp2_cc_bbr *bbr,
113 ngtcp2_conn_stat *cstat,
114 const ngtcp2_cc_ack *ack);
115
116 static void bbr_adapt_lower_bounds_from_congestion(ngtcp2_cc_bbr *bbr,
117 ngtcp2_conn_stat *cstat);
118
119 static void bbr_init_lower_bounds(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat);
120
121 static void bbr_loss_lower_bounds(ngtcp2_cc_bbr *bbr);
122
123 static void bbr_bound_bw_for_model(ngtcp2_cc_bbr *bbr);
124
125 static void bbr_update_max_bw(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
126 const ngtcp2_cc_ack *ack);
127
128 static void bbr_update_round(ngtcp2_cc_bbr *bbr, const ngtcp2_cc_ack *ack);
129
130 static void bbr_start_round(ngtcp2_cc_bbr *bbr);
131
132 static int bbr_is_in_probe_bw_state(ngtcp2_cc_bbr *bbr);
133
134 static int bbr_is_probing_bw(ngtcp2_cc_bbr *bbr);
135
136 static void bbr_update_ack_aggregation(ngtcp2_cc_bbr *bbr,
137 ngtcp2_conn_stat *cstat,
138 const ngtcp2_cc_ack *ack,
139 ngtcp2_tstamp ts);
140
141 static void bbr_enter_drain(ngtcp2_cc_bbr *bbr);
142
143 static void bbr_check_drain_done(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
144 ngtcp2_tstamp ts);
145
146 static void bbr_enter_probe_bw(ngtcp2_cc_bbr *bbr, ngtcp2_tstamp ts);
147
148 static void bbr_start_probe_bw_down(ngtcp2_cc_bbr *bbr, ngtcp2_tstamp ts);
149
150 static void bbr_start_probe_bw_cruise(ngtcp2_cc_bbr *bbr);
151
152 static void bbr_start_probe_bw_refill(ngtcp2_cc_bbr *bbr);
153
154 static void bbr_start_probe_bw_up(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat);
155
156 static void bbr_update_probe_bw_cycle_phase(ngtcp2_cc_bbr *bbr,
157 ngtcp2_conn_stat *cstat,
158 const ngtcp2_cc_ack *ack,
159 ngtcp2_tstamp ts);
160
161 static int bbr_is_time_to_cruise(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat);
162
163 static int bbr_is_time_to_go_down(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat);
164
165 static int bbr_has_elapsed_in_phase(ngtcp2_cc_bbr *bbr,
166 ngtcp2_duration interval, ngtcp2_tstamp ts);
167
168 static uint64_t bbr_inflight_with_headroom(ngtcp2_cc_bbr *bbr,
169 ngtcp2_conn_stat *cstat);
170
171 static void bbr_raise_inflight_longterm_slope(ngtcp2_cc_bbr *bbr,
172 ngtcp2_conn_stat *cstat);
173
174 static void bbr_probe_inflight_longterm_upward(ngtcp2_cc_bbr *bbr,
175 ngtcp2_conn_stat *cstat,
176 const ngtcp2_cc_ack *ack);
177
178 static void bbr_adapt_longterm_model(ngtcp2_cc_bbr *bbr,
179 ngtcp2_conn_stat *cstat,
180 const ngtcp2_cc_ack *ack);
181
182 static int bbr_is_time_to_probe_bw(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
183 ngtcp2_tstamp ts);
184
185 static void bbr_pick_probe_wait(ngtcp2_cc_bbr *bbr);
186
187 static int bbr_is_reno_coexistence_probe_time(ngtcp2_cc_bbr *bbr,
188 ngtcp2_conn_stat *cstat);
189
190 static uint64_t bbr_target_inflight(ngtcp2_cc_bbr *bbr,
191 ngtcp2_conn_stat *cstat);
192
193 static int bbr_is_inflight_too_high(ngtcp2_cc_bbr *bbr, const ngtcp2_rs *rs);
194
195 static void bbr_handle_inflight_too_high(ngtcp2_cc_bbr *bbr,
196 ngtcp2_conn_stat *cstat,
197 const ngtcp2_rs *rs, ngtcp2_tstamp ts);
198
199 static void bbr_note_loss(ngtcp2_cc_bbr *bbr);
200
201 static void bbr_handle_lost_packet(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
202 const ngtcp2_cc_pkt *pkt, ngtcp2_tstamp ts);
203
204 static uint64_t bbr_inflight_at_loss(ngtcp2_cc_bbr *bbr,
205 const ngtcp2_cc_pkt *pkt,
206 const ngtcp2_rs *rs);
207
208 static void bbr_update_min_rtt(ngtcp2_cc_bbr *bbr, const ngtcp2_cc_ack *ack,
209 ngtcp2_tstamp ts);
210
211 static void bbr_check_probe_rtt(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
212 ngtcp2_tstamp ts);
213
214 static void bbr_enter_probe_rtt(ngtcp2_cc_bbr *bbr);
215
216 static void bbr_handle_probe_rtt(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
217 ngtcp2_tstamp ts);
218
219 static void bbr_check_probe_rtt_done(ngtcp2_cc_bbr *bbr,
220 ngtcp2_conn_stat *cstat, ngtcp2_tstamp ts);
221
222 static void bbr_mark_connection_app_limited(ngtcp2_cc_bbr *bbr,
223 ngtcp2_conn_stat *cstat);
224
225 static void bbr_exit_probe_rtt(ngtcp2_cc_bbr *bbr, ngtcp2_tstamp ts);
226
227 static void bbr_handle_restart_from_idle(ngtcp2_cc_bbr *bbr,
228 ngtcp2_conn_stat *cstat,
229 ngtcp2_tstamp ts);
230
231 static uint64_t bbr_bdp_multiple(ngtcp2_cc_bbr *bbr, uint64_t gain_h);
232
233 static uint64_t bbr_quantization_budget(ngtcp2_cc_bbr *bbr,
234 ngtcp2_conn_stat *cstat,
235 uint64_t inflight);
236
237 static uint64_t bbr_inflight(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
238 uint64_t gain_h);
239
240 static void bbr_update_max_inflight(ngtcp2_cc_bbr *bbr,
241 ngtcp2_conn_stat *cstat);
242
243 static void bbr_update_offload_budget(ngtcp2_cc_bbr *bbr,
244 ngtcp2_conn_stat *cstat);
245
246 static uint64_t min_pipe_cwnd(size_t max_udp_payload_size);
247
248 static void bbr_advance_max_bw_filter(ngtcp2_cc_bbr *bbr);
249
250 static void bbr_save_cwnd(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat);
251
252 static void bbr_restore_cwnd(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat);
253
254 static uint64_t bbr_probe_rtt_cwnd(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat);
255
256 static void bbr_bound_cwnd_for_probe_rtt(ngtcp2_cc_bbr *bbr,
257 ngtcp2_conn_stat *cstat);
258
259 static void bbr_set_cwnd(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
260 const ngtcp2_cc_ack *ack);
261
262 static void bbr_bound_cwnd_for_model(ngtcp2_cc_bbr *bbr,
263 ngtcp2_conn_stat *cstat);
264
265 static void bbr_set_send_quantum(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat);
266
267 static int in_congestion_recovery(const ngtcp2_conn_stat *cstat,
268 ngtcp2_tstamp sent_time);
269
270 static void bbr_handle_recovery(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
271 const ngtcp2_cc_ack *ack);
272
273 static void bbr_on_init(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
274 ngtcp2_tstamp initial_ts) {
275 ngtcp2_window_filter_init(&bbr->max_bw_filter, NGTCP2_BBR_MAX_BW_FILTERLEN);
276 ngtcp2_window_filter_init(&bbr->extra_acked_filter,
277 NGTCP2_BBR_EXTRA_ACKED_FILTERLEN);
278
279 bbr->min_rtt =
280 cstat->first_rtt_sample_ts == UINT64_MAX ? UINT64_MAX : cstat->smoothed_rtt;
281 bbr->min_rtt_stamp = initial_ts;
282 /* remark: Use UINT64_MAX instead of 0 for consistency. */
283 bbr->probe_rtt_done_stamp = UINT64_MAX;
284 bbr->probe_rtt_round_done = 0;
285 bbr->prior_cwnd = 0;
286 bbr->idle_restart = 0;
287 bbr->extra_acked_interval_start = initial_ts;
288 bbr->extra_acked_delivered = 0;
289 bbr->full_bw_reached = 0;
290
291 bbr_reset_congestion_signals(bbr);
292 bbr_reset_shortterm_model(bbr);
293 bbr_init_round_counting(bbr);
294 bbr_reset_full_bw(bbr);
295 bbr_init_pacing_rate(bbr, cstat);
296 bbr_enter_startup(bbr);
297
298 cstat->send_quantum = cstat->max_tx_udp_payload_size * 10;
299
300 /* Missing in documentation */
301 bbr->loss_round_start = 0;
302 bbr->loss_round_delivered = UINT64_MAX;
303
304 bbr->rounds_since_bw_probe = 0;
305
306 bbr->max_bw = 0;
307 bbr->bw = 0;
308
309 bbr->cycle_count = 0;
310
311 bbr->extra_acked = 0;
312
313 bbr->bytes_lost_in_round = 0;
314 bbr->loss_events_in_round = 0;
315
316 bbr->offload_budget = 0;
317
318 bbr->probe_up_cnt = UINT64_MAX;
319 bbr->cycle_stamp = UINT64_MAX;
320 bbr->ack_phase = 0;
321 bbr->bw_probe_wait = 0;
322 bbr->bw_probe_samples = 0;
323 bbr->bw_probe_up_rounds = 0;
324 bbr->bw_probe_up_acks = 0;
325
326 bbr->inflight_longterm = UINT64_MAX;
327
328 bbr->probe_rtt_expired = 0;
329 bbr->probe_rtt_min_delay = UINT64_MAX;
330 bbr->probe_rtt_min_stamp = initial_ts;
331
332 bbr->in_loss_recovery = 0;
333 bbr->round_count_at_recovery = UINT64_MAX;
334
335 bbr->max_inflight = 0;
336
337 bbr->bdp = 0;
338
339 bbr->undo_bw_shortterm = 0;
340 bbr->undo_inflight_shortterm = 0;
341 bbr->undo_inflight_longterm = 0;
342 }
343
344 static void bbr_reset_congestion_signals(ngtcp2_cc_bbr *bbr) {
345 bbr->loss_in_round = 0;
346 bbr->bw_latest = 0;
347 bbr->inflight_latest = 0;
348 }
349
350 static void bbr_reset_shortterm_model(ngtcp2_cc_bbr *bbr) {
351 bbr->bw_shortterm = UINT64_MAX;
352 bbr->inflight_shortterm = UINT64_MAX;
353 }
354
355 static void bbr_init_round_counting(ngtcp2_cc_bbr *bbr) {
356 bbr->next_round_delivered = 0;
357 bbr->round_start = 0;
358 bbr->round_count = 0;
359 }
360
361 static void bbr_reset_full_bw(ngtcp2_cc_bbr *bbr) {
362 bbr->full_bw = 0;
363 bbr->full_bw_count = 0;
364 bbr->full_bw_now = 0;
365 }
366
367 static void bbr_check_full_bw_reached(ngtcp2_cc_bbr *bbr,
368 ngtcp2_conn_stat *cstat) {
369 if (bbr->full_bw_now || !bbr->round_start || bbr->rst->rs.is_app_limited) {
370 return;
371 }
372
373 if (cstat->delivery_rate_sec * 100 >= bbr->full_bw * 125) {
374 bbr_reset_full_bw(bbr);
375 bbr->full_bw = cstat->delivery_rate_sec;
376
377 return;
378 }
379
380 ++bbr->full_bw_count;
381
382 bbr->full_bw_now = bbr->full_bw_count >= 3;
383 if (!bbr->full_bw_now) {
384 return;
385 }
386
387 bbr->full_bw_reached = 1;
388
389 ngtcp2_log_infof(bbr->cc.log, NGTCP2_LOG_EVENT_CCA,
390 "bbr reached full bandwidth, full_bw=%" PRIu64,
391 bbr->full_bw);
392 }
393
394 static void bbr_check_startup_high_loss(ngtcp2_cc_bbr *bbr) {
395 if (bbr->full_bw_reached || bbr->loss_events_in_round <= 6 ||
396 (bbr->in_loss_recovery &&
397 bbr->round_count <= bbr->round_count_at_recovery) ||
398 !bbr_is_inflight_too_high(bbr, &bbr->rst->rs)) {
399 return;
400 }
401
402 bbr->full_bw_reached = 1;
403 bbr->inflight_longterm = ngtcp2_max_uint64(
404 bbr_bdp_multiple(bbr, bbr->cwnd_gain_h), bbr->inflight_latest);
405 }
406
407 static void bbr_init_pacing_rate(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat) {
408 cstat->pacing_interval_m = ngtcp2_max_uint64(
409 ((cstat->first_rtt_sample_ts == UINT64_MAX ? NGTCP2_MILLISECONDS
410 : cstat->smoothed_rtt)
411 << 10) *
412 100 / NGTCP2_BBR_STARTUP_PACING_GAIN_H / bbr->initial_cwnd,
413 1);
414 }
415
416 static void bbr_set_pacing_rate_with_gain(ngtcp2_cc_bbr *bbr,
417 ngtcp2_conn_stat *cstat,
418 uint64_t pacing_gain_h) {
419 uint64_t interval_m;
420
421 if (bbr->bw == 0) {
422 return;
423 }
424
425 interval_m = (NGTCP2_SECONDS << 10) * 100 * 100 / pacing_gain_h / bbr->bw /
426 (100 - NGTCP2_BBR_PACING_MARGIN_PERCENT);
427 interval_m = ngtcp2_max_uint64(interval_m, 1);
428
429 if (bbr->full_bw_reached || interval_m < cstat->pacing_interval_m) {
430 cstat->pacing_interval_m = interval_m;
431 }
432 }
433
434 static void bbr_set_pacing_rate(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat) {
435 bbr_set_pacing_rate_with_gain(bbr, cstat, bbr->pacing_gain_h);
436 }
437
438 static void bbr_enter_startup(ngtcp2_cc_bbr *bbr) {
439 ngtcp2_log_info(bbr->cc.log, NGTCP2_LOG_EVENT_CCA, "bbr enter Startup");
440
441 bbr->state = NGTCP2_BBR_STATE_STARTUP;
442 bbr->pacing_gain_h = NGTCP2_BBR_STARTUP_PACING_GAIN_H;
443 bbr->cwnd_gain_h = NGTCP2_BBR_DEFAULT_CWND_GAIN_H;
444 }
445
446 static void bbr_check_startup_done(ngtcp2_cc_bbr *bbr) {
447 bbr_check_startup_high_loss(bbr);
448
449 if (bbr->state == NGTCP2_BBR_STATE_STARTUP && bbr->full_bw_reached) {
450 bbr_enter_drain(bbr);
451 }
452 }
453
454 static void bbr_on_transmit(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
455 ngtcp2_tstamp ts) {
456 bbr_handle_restart_from_idle(bbr, cstat, ts);
457 }
458
459 static void bbr_update_on_ack(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
460 const ngtcp2_cc_ack *ack, ngtcp2_tstamp ts) {
461 bbr_update_model_and_state(bbr, cstat, ack, ts);
462 bbr_update_control_parameters(bbr, cstat, ack);
463 }
464
465 static void bbr_update_model_and_state(ngtcp2_cc_bbr *bbr,
466 ngtcp2_conn_stat *cstat,
467 const ngtcp2_cc_ack *ack,
468 ngtcp2_tstamp ts) {
469 bbr_update_latest_delivery_signals(bbr, cstat);
470 bbr_update_congestion_signals(bbr, cstat, ack);
471 bbr_update_ack_aggregation(bbr, cstat, ack, ts);
472 bbr_check_full_bw_reached(bbr, cstat);
473 bbr_check_startup_done(bbr);
474 bbr_check_drain_done(bbr, cstat, ts);
475 bbr_update_probe_bw_cycle_phase(bbr, cstat, ack, ts);
476 bbr_update_min_rtt(bbr, ack, ts);
477 bbr_check_probe_rtt(bbr, cstat, ts);
478 bbr_advance_latest_delivery_signals(bbr, cstat);
479 bbr_bound_bw_for_model(bbr);
480 }
481
482 static void bbr_update_control_parameters(ngtcp2_cc_bbr *bbr,
483 ngtcp2_conn_stat *cstat,
484 const ngtcp2_cc_ack *ack) {
485 bbr_set_pacing_rate(bbr, cstat);
486 bbr_set_send_quantum(bbr, cstat);
487 bbr_set_cwnd(bbr, cstat, ack);
488 }
489
490 static void bbr_update_on_loss(ngtcp2_cc_bbr *cc, ngtcp2_conn_stat *cstat,
491 const ngtcp2_cc_pkt *pkt, ngtcp2_tstamp ts) {
492 bbr_handle_lost_packet(cc, cstat, pkt, ts);
493 }
494
495 static void bbr_update_latest_delivery_signals(ngtcp2_cc_bbr *bbr,
496 ngtcp2_conn_stat *cstat) {
497 bbr->loss_round_start = 0;
498 bbr->bw_latest = ngtcp2_max_uint64(bbr->bw_latest, cstat->delivery_rate_sec);
499 bbr->inflight_latest =
500 ngtcp2_max_uint64(bbr->inflight_latest, bbr->rst->rs.delivered);
501
502 if (bbr->rst->rs.prior_delivered >= bbr->loss_round_delivered) {
503 bbr->loss_round_delivered = bbr->rst->delivered;
504 bbr->loss_round_start = 1;
505 }
506 }
507
508 static void bbr_advance_latest_delivery_signals(ngtcp2_cc_bbr *bbr,
509 ngtcp2_conn_stat *cstat) {
510 if (bbr->loss_round_start) {
511 bbr->bw_latest = cstat->delivery_rate_sec;
512 bbr->inflight_latest = bbr->rst->rs.delivered;
513 }
514 }
515
516 static void bbr_update_congestion_signals(ngtcp2_cc_bbr *bbr,
517 ngtcp2_conn_stat *cstat,
518 const ngtcp2_cc_ack *ack) {
519 bbr_update_max_bw(bbr, cstat, ack);
520
521 if (ack->bytes_lost) {
522 bbr->bytes_lost_in_round += ack->bytes_lost;
523 ++bbr->loss_events_in_round;
524 }
525
526 if (!bbr->loss_round_start) {
527 return;
528 }
529
530 bbr_adapt_lower_bounds_from_congestion(bbr, cstat);
531
532 bbr->loss_in_round = 0;
533 }
534
535 static void bbr_adapt_lower_bounds_from_congestion(ngtcp2_cc_bbr *bbr,
536 ngtcp2_conn_stat *cstat) {
537 if (bbr_is_probing_bw(bbr)) {
538 return;
539 }
540
541 if (bbr->loss_in_round) {
542 bbr_init_lower_bounds(bbr, cstat);
543 bbr_loss_lower_bounds(bbr);
544 }
545 }
546
547 static void bbr_init_lower_bounds(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat) {
548 if (bbr->bw_shortterm == UINT64_MAX) {
549 bbr->bw_shortterm = bbr->max_bw;
550 }
551
552 if (bbr->inflight_shortterm == UINT64_MAX) {
553 bbr->inflight_shortterm = cstat->cwnd;
554 }
555 }
556
557 static void bbr_loss_lower_bounds(ngtcp2_cc_bbr *bbr) {
558 bbr->bw_shortterm = ngtcp2_max_uint64(
559 bbr->bw_latest,
560 bbr->bw_shortterm * NGTCP2_BBR_BETA_NUMER / NGTCP2_BBR_BETA_DENOM);
561 bbr->inflight_shortterm = ngtcp2_max_uint64(
562 bbr->inflight_latest,
563 bbr->inflight_shortterm * NGTCP2_BBR_BETA_NUMER / NGTCP2_BBR_BETA_DENOM);
564 }
565
566 static void bbr_bound_bw_for_model(ngtcp2_cc_bbr *bbr) {
567 bbr->bw = ngtcp2_min_uint64(bbr->max_bw, bbr->bw_shortterm);
568 }
569
570 static void bbr_update_max_bw(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
571 const ngtcp2_cc_ack *ack) {
572 bbr_update_round(bbr, ack);
573
574 if (cstat->delivery_rate_sec && (cstat->delivery_rate_sec >= bbr->max_bw ||
575 !bbr->rst->rs.is_app_limited)) {
576 ngtcp2_window_filter_update(&bbr->max_bw_filter, cstat->delivery_rate_sec,
577 bbr->cycle_count);
578
579 bbr->max_bw = ngtcp2_window_filter_get_best(&bbr->max_bw_filter);
580 }
581 }
582
583 static void bbr_update_round(ngtcp2_cc_bbr *bbr, const ngtcp2_cc_ack *ack) {
584 if (ack->pkt_delivered >= bbr->next_round_delivered) {
585 bbr_start_round(bbr);
586
587 ++bbr->round_count;
588 ++bbr->rounds_since_bw_probe;
589 bbr->round_start = 1;
590
591 bbr->bytes_lost_in_round = 0;
592 bbr->loss_events_in_round = 0;
593
594 bbr->rst->is_cwnd_limited = 0;
595
596 return;
597 }
598
599 bbr->round_start = 0;
600 }
601
602 static void bbr_start_round(ngtcp2_cc_bbr *bbr) {
603 bbr->next_round_delivered = bbr->rst->delivered;
604 }
605
606 static int bbr_is_in_probe_bw_state(ngtcp2_cc_bbr *bbr) {
607 switch (bbr->state) {
608 case NGTCP2_BBR_STATE_PROBE_BW_DOWN:
609 case NGTCP2_BBR_STATE_PROBE_BW_CRUISE:
610 case NGTCP2_BBR_STATE_PROBE_BW_REFILL:
611 case NGTCP2_BBR_STATE_PROBE_BW_UP:
612 return 1;
613 default:
614 return 0;
615 }
616 }
617
618 static int bbr_is_probing_bw(ngtcp2_cc_bbr *bbr) {
619 switch (bbr->state) {
620 case NGTCP2_BBR_STATE_STARTUP:
621 case NGTCP2_BBR_STATE_PROBE_BW_REFILL:
622 case NGTCP2_BBR_STATE_PROBE_BW_UP:
623 return 1;
624 default:
625 return 0;
626 }
627 }
628
629 static void bbr_update_ack_aggregation(ngtcp2_cc_bbr *bbr,
630 ngtcp2_conn_stat *cstat,
631 const ngtcp2_cc_ack *ack,
632 ngtcp2_tstamp ts) {
633 ngtcp2_duration interval = ts - bbr->extra_acked_interval_start;
634 uint64_t expected_delivered = bbr->bw * interval / NGTCP2_SECONDS;
635 uint64_t extra;
636
637 if (bbr->extra_acked_delivered <= expected_delivered) {
638 bbr->extra_acked_delivered = 0;
639 bbr->extra_acked_interval_start = ts;
640 expected_delivered = 0;
641 }
642
643 bbr->extra_acked_delivered += ack->bytes_delivered;
644
645 if (bbr->extra_acked_delivered <= expected_delivered) {
646 extra = 0;
647 } else {
648 extra = bbr->extra_acked_delivered - expected_delivered;
649 extra = ngtcp2_min_uint64(extra, cstat->cwnd);
650 }
651
652 if (bbr->full_bw_reached) {
653 bbr->extra_acked_filter.window_length = NGTCP2_BBR_EXTRA_ACKED_FILTERLEN;
654 } else {
655 bbr->extra_acked_filter.window_length = 1;
656 }
657
658 ngtcp2_window_filter_update(&bbr->extra_acked_filter, extra,
659 bbr->round_count);
660
661 bbr->extra_acked = ngtcp2_window_filter_get_best(&bbr->extra_acked_filter);
662 }
663
664 static void bbr_enter_drain(ngtcp2_cc_bbr *bbr) {
665 ngtcp2_log_info(bbr->cc.log, NGTCP2_LOG_EVENT_CCA, "bbr enter Drain");
666
667 bbr->state = NGTCP2_BBR_STATE_DRAIN;
668 bbr->pacing_gain_h = NGTCP2_BBR_DRAIN_PACING_GAIN_H;
669 bbr->cwnd_gain_h = NGTCP2_BBR_DEFAULT_CWND_GAIN_H;
670 }
671
672 static void bbr_check_drain_done(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
673 ngtcp2_tstamp ts) {
674 if (bbr->state == NGTCP2_BBR_STATE_DRAIN &&
675 cstat->bytes_in_flight <= bbr_inflight(bbr, cstat, 100)) {
676 bbr_enter_probe_bw(bbr, ts);
677 }
678 }
679
680 static void bbr_enter_probe_bw(ngtcp2_cc_bbr *bbr, ngtcp2_tstamp ts) {
681 bbr_start_probe_bw_down(bbr, ts);
682 }
683
684 static void bbr_start_probe_bw_down(ngtcp2_cc_bbr *bbr, ngtcp2_tstamp ts) {
685 ngtcp2_log_info(bbr->cc.log, NGTCP2_LOG_EVENT_CCA, "bbr start ProbeBW_DOWN");
686
687 bbr_reset_congestion_signals(bbr);
688
689 bbr->probe_up_cnt = UINT64_MAX;
690
691 bbr_pick_probe_wait(bbr);
692
693 bbr->cycle_stamp = ts;
694 bbr->ack_phase = NGTCP2_BBR_ACK_PHASE_ACKS_PROBE_STOPPING;
695
696 bbr_start_round(bbr);
697
698 bbr->state = NGTCP2_BBR_STATE_PROBE_BW_DOWN;
699 bbr->pacing_gain_h = 90;
700 bbr->cwnd_gain_h = NGTCP2_BBR_DEFAULT_CWND_GAIN_H;
701 }
702
703 static void bbr_start_probe_bw_cruise(ngtcp2_cc_bbr *bbr) {
704 ngtcp2_log_info(bbr->cc.log, NGTCP2_LOG_EVENT_CCA,
705 "bbr start ProbeBW_CRUISE");
706
707 bbr->state = NGTCP2_BBR_STATE_PROBE_BW_CRUISE;
708 bbr->pacing_gain_h = 100;
709 bbr->cwnd_gain_h = NGTCP2_BBR_DEFAULT_CWND_GAIN_H;
710 }
711
712 static void bbr_start_probe_bw_refill(ngtcp2_cc_bbr *bbr) {
713 ngtcp2_log_info(bbr->cc.log, NGTCP2_LOG_EVENT_CCA,
714 "bbr start ProbeBW_REFILL");
715
716 bbr_reset_shortterm_model(bbr);
717
718 bbr->bw_probe_up_rounds = 0;
719 bbr->bw_probe_up_acks = 0;
720 bbr->ack_phase = NGTCP2_BBR_ACK_PHASE_ACKS_REFILLING;
721
722 bbr_start_round(bbr);
723
724 bbr->state = NGTCP2_BBR_STATE_PROBE_BW_REFILL;
725 bbr->pacing_gain_h = 100;
726 bbr->cwnd_gain_h = NGTCP2_BBR_DEFAULT_CWND_GAIN_H;
727 }
728
729 static void bbr_start_probe_bw_up(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat) {
730 ngtcp2_log_info(bbr->cc.log, NGTCP2_LOG_EVENT_CCA, "bbr start ProbeBW_UP");
731
732 bbr->ack_phase = NGTCP2_BBR_ACK_PHASE_ACKS_PROBE_STARTING;
733
734 bbr_start_round(bbr);
735 bbr_reset_full_bw(bbr);
736
737 bbr->full_bw = cstat->delivery_rate_sec;
738 bbr->state = NGTCP2_BBR_STATE_PROBE_BW_UP;
739 bbr->pacing_gain_h = 125;
740 bbr->cwnd_gain_h = 225;
741
742 bbr_raise_inflight_longterm_slope(bbr, cstat);
743 }
744
745 static void bbr_update_probe_bw_cycle_phase(ngtcp2_cc_bbr *bbr,
746 ngtcp2_conn_stat *cstat,
747 const ngtcp2_cc_ack *ack,
748 ngtcp2_tstamp ts) {
749 if (!bbr->full_bw_reached) {
750 return;
751 }
752
753 bbr_adapt_longterm_model(bbr, cstat, ack);
754
755 if (!bbr_is_in_probe_bw_state(bbr)) {
756 return;
757 }
758
759 switch (bbr->state) {
760 case NGTCP2_BBR_STATE_PROBE_BW_DOWN:
761 if (bbr_is_time_to_probe_bw(bbr, cstat, ts)) {
762 return;
763 }
764
765 if (bbr_is_time_to_cruise(bbr, cstat)) {
766 bbr_start_probe_bw_cruise(bbr);
767 }
768
769 break;
770 case NGTCP2_BBR_STATE_PROBE_BW_CRUISE:
771 if (bbr_is_time_to_probe_bw(bbr, cstat, ts)) {
772 return;
773 }
774
775 break;
776 case NGTCP2_BBR_STATE_PROBE_BW_REFILL:
777 if (bbr->round_start) {
778 bbr->bw_probe_samples = 1;
779 bbr_start_probe_bw_up(bbr, cstat);
780 }
781
782 break;
783 case NGTCP2_BBR_STATE_PROBE_BW_UP:
784 if (bbr_is_time_to_go_down(bbr, cstat)) {
785 bbr_start_probe_bw_down(bbr, ts);
786 }
787
788 break;
789 default:
790 break;
791 }
792 }
793
794 static int bbr_is_time_to_cruise(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat) {
795 uint64_t inflight = ngtcp2_min_uint64(bbr_inflight_with_headroom(bbr, cstat),
796 bbr_inflight(bbr, cstat, 100));
797
798 return cstat->bytes_in_flight <= inflight;
799 }
800
801 static int bbr_is_time_to_go_down(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat) {
802 if (bbr->rst->is_cwnd_limited && cstat->cwnd >= bbr->inflight_longterm) {
803 bbr_reset_full_bw(bbr);
804 bbr->full_bw = cstat->delivery_rate_sec;
805
806 return 0;
807 }
808
809 return bbr->full_bw_now;
810 }
811
812 static int bbr_has_elapsed_in_phase(ngtcp2_cc_bbr *bbr,
813 ngtcp2_duration interval,
814 ngtcp2_tstamp ts) {
815 return ts > bbr->cycle_stamp + interval;
816 }
817
818 static uint64_t bbr_inflight_with_headroom(ngtcp2_cc_bbr *bbr,
819 ngtcp2_conn_stat *cstat) {
820 uint64_t headroom;
821 uint64_t mpcwnd;
822 if (bbr->inflight_longterm == UINT64_MAX) {
823 return UINT64_MAX;
824 }
825
826 headroom =
827 ngtcp2_max_uint64(cstat->max_tx_udp_payload_size,
828 bbr->inflight_longterm * NGTCP2_BBR_HEADROOM_NUMER /
829 NGTCP2_BBR_HEADROOM_DENOM);
830 mpcwnd = min_pipe_cwnd(cstat->max_tx_udp_payload_size);
831
832 if (bbr->inflight_longterm > headroom) {
833 return ngtcp2_max_uint64(bbr->inflight_longterm - headroom, mpcwnd);
834 }
835
836 return mpcwnd;
837 }
838
839 static void bbr_raise_inflight_longterm_slope(ngtcp2_cc_bbr *bbr,
840 ngtcp2_conn_stat *cstat) {
841 uint64_t growth_this_round = cstat->max_tx_udp_payload_size
842 << bbr->bw_probe_up_rounds;
843
844 bbr->bw_probe_up_rounds = ngtcp2_min_size(bbr->bw_probe_up_rounds + 1, 30);
845 bbr->probe_up_cnt = ngtcp2_max_uint64(cstat->cwnd / growth_this_round, 1);
846 }
847
848 static void bbr_probe_inflight_longterm_upward(ngtcp2_cc_bbr *bbr,
849 ngtcp2_conn_stat *cstat,
850 const ngtcp2_cc_ack *ack) {
851 uint64_t delta;
852
853 if (!bbr->rst->is_cwnd_limited || cstat->cwnd < bbr->inflight_longterm) {
854 return;
855 }
856
857 bbr->bw_probe_up_acks += ack->bytes_delivered;
858
859 if (bbr->probe_up_cnt != UINT64_MAX &&
860 bbr->bw_probe_up_acks >=
861 bbr->probe_up_cnt * cstat->max_tx_udp_payload_size) {
862 delta = bbr->bw_probe_up_acks / bbr->probe_up_cnt;
863 bbr->bw_probe_up_acks -= delta * bbr->probe_up_cnt;
864 bbr->inflight_longterm += delta;
865 }
866
867 if (bbr->round_start) {
868 bbr_raise_inflight_longterm_slope(bbr, cstat);
869 }
870 }
871
872 static void bbr_adapt_longterm_model(ngtcp2_cc_bbr *bbr,
873 ngtcp2_conn_stat *cstat,
874 const ngtcp2_cc_ack *ack) {
875 if (bbr->ack_phase == NGTCP2_BBR_ACK_PHASE_ACKS_PROBE_STARTING &&
876 bbr->round_start) {
877 bbr->ack_phase = NGTCP2_BBR_ACK_PHASE_ACKS_PROBE_FEEDBACK;
878 }
879
880 if (bbr->ack_phase == NGTCP2_BBR_ACK_PHASE_ACKS_PROBE_STOPPING &&
881 bbr->round_start) {
882 if (bbr_is_in_probe_bw_state(bbr) && !bbr->rst->rs.is_app_limited) {
883 bbr_advance_max_bw_filter(bbr);
884 }
885 }
886
887 if (!bbr_is_inflight_too_high(bbr, &bbr->rst->rs)) {
888 if (bbr->inflight_longterm == UINT64_MAX) {
889 return;
890 }
891
892 if (bbr->rst->rs.tx_in_flight > bbr->inflight_longterm) {
893 bbr->inflight_longterm = bbr->rst->rs.tx_in_flight;
894 }
895
896 if (bbr->state == NGTCP2_BBR_STATE_PROBE_BW_UP) {
897 bbr_probe_inflight_longterm_upward(bbr, cstat, ack);
898 }
899 }
900 }
901
902 static int bbr_is_time_to_probe_bw(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
903 ngtcp2_tstamp ts) {
904 if (bbr_has_elapsed_in_phase(bbr, bbr->bw_probe_wait, ts) ||
905 bbr_is_reno_coexistence_probe_time(bbr, cstat)) {
906 bbr_start_probe_bw_refill(bbr);
907
908 return 1;
909 }
910
911 return 0;
912 }
913
914 static void bbr_pick_probe_wait(ngtcp2_cc_bbr *bbr) {
915 bbr->rounds_since_bw_probe = ngtcp2_pcg32_rand_n(bbr->pcg, 2);
916 bbr->bw_probe_wait =
917 2 * NGTCP2_SECONDS + ngtcp2_pcg32_rand_n(bbr->pcg, NGTCP2_SECONDS + 1);
918 }
919
920 static int bbr_is_reno_coexistence_probe_time(ngtcp2_cc_bbr *bbr,
921 ngtcp2_conn_stat *cstat) {
922 uint64_t reno_rounds =
923 bbr_target_inflight(bbr, cstat) / cstat->max_tx_udp_payload_size;
924
925 return bbr->rounds_since_bw_probe >= ngtcp2_min_uint64(reno_rounds, 63);
926 }
927
928 static uint64_t bbr_target_inflight(ngtcp2_cc_bbr *bbr,
929 ngtcp2_conn_stat *cstat) {
930 return ngtcp2_min_uint64(bbr->bdp, cstat->cwnd);
931 }
932
933 static int bbr_is_inflight_too_high(ngtcp2_cc_bbr *bbr, const ngtcp2_rs *rs) {
934 (void)bbr;
935 return rs->lost * NGTCP2_BBR_LOSS_THRESH_DENOM >
936 rs->tx_in_flight * NGTCP2_BBR_LOSS_THRESH_NUMER;
937 }
938
939 static void bbr_handle_inflight_too_high(ngtcp2_cc_bbr *bbr,
940 ngtcp2_conn_stat *cstat,
941 const ngtcp2_rs *rs,
942 ngtcp2_tstamp ts) {
943 bbr->bw_probe_samples = 0;
944
945 if (!rs->is_app_limited) {
946 bbr->inflight_longterm = ngtcp2_max_uint64(
947 rs->tx_in_flight, bbr_target_inflight(bbr, cstat) *
948 NGTCP2_BBR_BETA_NUMER / NGTCP2_BBR_BETA_DENOM);
949 }
950
951 if (bbr->state == NGTCP2_BBR_STATE_PROBE_BW_UP) {
952 bbr_start_probe_bw_down(bbr, ts);
953 }
954 }
955
956 static void bbr_note_loss(ngtcp2_cc_bbr *bbr) {
957 if (!bbr->loss_in_round) {
958 bbr->loss_round_delivered = bbr->rst->delivered;
959 }
960
961 bbr->loss_in_round = 1;
962 }
963
964 static void bbr_handle_lost_packet(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
965 const ngtcp2_cc_pkt *pkt, ngtcp2_tstamp ts) {
966 ngtcp2_rs rs = {0};
967
968 bbr_note_loss(bbr);
969
970 if (!bbr->bw_probe_samples) {
971 return;
972 }
973
974 rs.tx_in_flight = pkt->tx_in_flight;
975 assert(bbr->rst->lost >= pkt->lost);
976 rs.lost = bbr->rst->lost - pkt->lost;
977 rs.is_app_limited = pkt->is_app_limited;
978
979 if (bbr_is_inflight_too_high(bbr, &rs)) {
980 rs.tx_in_flight = bbr_inflight_at_loss(bbr, pkt, &rs);
981
982 bbr_handle_inflight_too_high(bbr, cstat, &rs, ts);
983 }
984 }
985
986 static uint64_t bbr_inflight_at_loss(ngtcp2_cc_bbr *bbr,
987 const ngtcp2_cc_pkt *pkt,
988 const ngtcp2_rs *rs) {
989 uint64_t inflight_prev, lost_prev, lost_prefix;
990 (void)bbr;
991
992 assert(rs->tx_in_flight >= pkt->pktlen);
993
994 inflight_prev = rs->tx_in_flight - pkt->pktlen;
995
996 assert(rs->lost >= pkt->pktlen);
997
998 lost_prev = rs->lost - pkt->pktlen;
999
1000 if (inflight_prev * NGTCP2_BBR_LOSS_THRESH_NUMER <
1001 lost_prev * NGTCP2_BBR_LOSS_THRESH_DENOM) {
1002 return inflight_prev;
1003 }
1004
1005 lost_prefix = (inflight_prev * NGTCP2_BBR_LOSS_THRESH_NUMER -
1006 lost_prev * NGTCP2_BBR_LOSS_THRESH_DENOM) /
1007 (NGTCP2_BBR_LOSS_THRESH_DENOM - NGTCP2_BBR_LOSS_THRESH_NUMER);
1008
1009 return inflight_prev + lost_prefix;
1010 }
1011
1012 static void bbr_update_min_rtt(ngtcp2_cc_bbr *bbr, const ngtcp2_cc_ack *ack,
1013 ngtcp2_tstamp ts) {
1014 int min_rtt_expired;
1015
1016 bbr->probe_rtt_expired =
1017 ts > bbr->probe_rtt_min_stamp + NGTCP2_BBR_PROBE_RTT_INTERVAL;
1018
1019 if (ack->rtt != UINT64_MAX &&
1020 (ack->rtt < bbr->probe_rtt_min_delay || bbr->probe_rtt_expired)) {
1021 bbr->probe_rtt_min_delay = ack->rtt;
1022 bbr->probe_rtt_min_stamp = ts;
1023 }
1024
1025 min_rtt_expired = ts > bbr->min_rtt_stamp + NGTCP2_BBR_MIN_RTT_FILTERLEN;
1026
1027 if (bbr->probe_rtt_min_delay < bbr->min_rtt || min_rtt_expired) {
1028 bbr->min_rtt = bbr->probe_rtt_min_delay;
1029 bbr->min_rtt_stamp = bbr->probe_rtt_min_stamp;
1030
1031 ngtcp2_log_infof(bbr->cc.log, NGTCP2_LOG_EVENT_CCA,
1032 "bbr update min_rtt=%" PRIu64, bbr->min_rtt);
1033 }
1034 }
1035
1036 static void bbr_check_probe_rtt(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
1037 ngtcp2_tstamp ts) {
1038 if (bbr->state != NGTCP2_BBR_STATE_PROBE_RTT && bbr->probe_rtt_expired &&
1039 !bbr->idle_restart) {
1040 bbr_enter_probe_rtt(bbr);
1041 bbr_save_cwnd(bbr, cstat);
1042
1043 bbr->probe_rtt_done_stamp = UINT64_MAX;
1044 bbr->ack_phase = NGTCP2_BBR_ACK_PHASE_ACKS_PROBE_STOPPING;
1045
1046 bbr_start_round(bbr);
1047 }
1048
1049 if (bbr->state == NGTCP2_BBR_STATE_PROBE_RTT) {
1050 bbr_handle_probe_rtt(bbr, cstat, ts);
1051 }
1052
1053 if (bbr->rst->rs.delivered) {
1054 bbr->idle_restart = 0;
1055 }
1056 }
1057
1058 static void bbr_enter_probe_rtt(ngtcp2_cc_bbr *bbr) {
1059 ngtcp2_log_info(bbr->cc.log, NGTCP2_LOG_EVENT_CCA, "bbr enter ProbeRTT");
1060
1061 bbr->state = NGTCP2_BBR_STATE_PROBE_RTT;
1062 bbr->pacing_gain_h = 100;
1063 bbr->cwnd_gain_h = NGTCP2_BBR_PROBE_RTT_CWND_GAIN_H;
1064 }
1065
1066 static void bbr_handle_probe_rtt(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
1067 ngtcp2_tstamp ts) {
1068 bbr_mark_connection_app_limited(bbr, cstat);
1069
1070 if (bbr->probe_rtt_done_stamp == UINT64_MAX &&
1071 cstat->bytes_in_flight <= bbr_probe_rtt_cwnd(bbr, cstat)) {
1072 bbr->probe_rtt_done_stamp = ts + NGTCP2_BBR_PROBE_RTT_DURATION;
1073 bbr->probe_rtt_round_done = 0;
1074
1075 bbr_start_round(bbr);
1076
1077 return;
1078 }
1079
1080 if (bbr->probe_rtt_done_stamp != UINT64_MAX) {
1081 if (bbr->round_start) {
1082 bbr->probe_rtt_round_done = 1;
1083 }
1084
1085 if (bbr->probe_rtt_round_done) {
1086 bbr_check_probe_rtt_done(bbr, cstat, ts);
1087 }
1088 }
1089 }
1090
1091 static void bbr_check_probe_rtt_done(ngtcp2_cc_bbr *bbr,
1092 ngtcp2_conn_stat *cstat,
1093 ngtcp2_tstamp ts) {
1094 if (bbr->probe_rtt_done_stamp != UINT64_MAX &&
1095 ts > bbr->probe_rtt_done_stamp) {
1096 bbr->probe_rtt_min_stamp = ts;
1097 bbr_restore_cwnd(bbr, cstat);
1098 bbr_exit_probe_rtt(bbr, ts);
1099 }
1100 }
1101
1102 static void bbr_mark_connection_app_limited(ngtcp2_cc_bbr *bbr,
1103 ngtcp2_conn_stat *cstat) {
1104 bbr->rst->app_limited =
1105 ngtcp2_max_uint64(bbr->rst->delivered + cstat->bytes_in_flight, 1);
1106 }
1107
1108 static void bbr_exit_probe_rtt(ngtcp2_cc_bbr *bbr, ngtcp2_tstamp ts) {
1109 bbr_reset_shortterm_model(bbr);
1110
1111 if (bbr->full_bw_reached) {
1112 bbr_start_probe_bw_down(bbr, ts);
1113 bbr_start_probe_bw_cruise(bbr);
1114 } else {
1115 bbr_enter_startup(bbr);
1116 }
1117 }
1118
1119 static void bbr_handle_restart_from_idle(ngtcp2_cc_bbr *bbr,
1120 ngtcp2_conn_stat *cstat,
1121 ngtcp2_tstamp ts) {
1122 if (cstat->bytes_in_flight == 0 && bbr->rst->app_limited) {
1123 ngtcp2_log_info(bbr->cc.log, NGTCP2_LOG_EVENT_CCA, "bbr restart from idle");
1124
1125 bbr->idle_restart = 1;
1126 bbr->extra_acked_interval_start = ts;
1127
1128 if (bbr_is_in_probe_bw_state(bbr)) {
1129 bbr_set_pacing_rate_with_gain(bbr, cstat, 100);
1130 } else if (bbr->state == NGTCP2_BBR_STATE_PROBE_RTT) {
1131 bbr_check_probe_rtt_done(bbr, cstat, ts);
1132 }
1133 }
1134 }
1135
1136 static uint64_t bbr_bdp_multiple(ngtcp2_cc_bbr *bbr, uint64_t gain_h) {
1137 if (bbr->min_rtt == UINT64_MAX) {
1138 return bbr->initial_cwnd;
1139 }
1140
1141 bbr->bdp = ngtcp2_max_uint64(bbr->bw * bbr->min_rtt / NGTCP2_SECONDS, 1);
1142
1143 return (uint64_t)(bbr->bdp * gain_h / 100);
1144 }
1145
1146 static uint64_t min_pipe_cwnd(size_t max_udp_payload_size) {
1147 return max_udp_payload_size * 4;
1148 }
1149
1150 static uint64_t bbr_quantization_budget(ngtcp2_cc_bbr *bbr,
1151 ngtcp2_conn_stat *cstat,
1152 uint64_t inflight) {
1153 bbr_update_offload_budget(bbr, cstat);
1154
1155 inflight = ngtcp2_max_uint64(inflight, bbr->offload_budget);
1156 inflight =
1157 ngtcp2_max_uint64(inflight, min_pipe_cwnd(cstat->max_tx_udp_payload_size));
1158
1159 if (bbr->state == NGTCP2_BBR_STATE_PROBE_BW_UP) {
1160 inflight += 2 * cstat->max_tx_udp_payload_size;
1161 }
1162
1163 return inflight;
1164 }
1165
1166 static uint64_t bbr_inflight(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
1167 uint64_t gain_h) {
1168 uint64_t inflight = bbr_bdp_multiple(bbr, gain_h);
1169
1170 return bbr_quantization_budget(bbr, cstat, inflight);
1171 }
1172
1173 static void bbr_update_max_inflight(ngtcp2_cc_bbr *bbr,
1174 ngtcp2_conn_stat *cstat) {
1175 uint64_t inflight;
1176
1177 inflight = bbr_bdp_multiple(bbr, bbr->cwnd_gain_h) + bbr->extra_acked;
1178 bbr->max_inflight = bbr_quantization_budget(bbr, cstat, inflight);
1179 }
1180
1181 static void bbr_update_offload_budget(ngtcp2_cc_bbr *bbr,
1182 ngtcp2_conn_stat *cstat) {
1183 bbr->offload_budget = 3 * cstat->send_quantum;
1184 }
1185
1186 static void bbr_advance_max_bw_filter(ngtcp2_cc_bbr *bbr) {
1187 ++bbr->cycle_count;
1188 }
1189
1190 static void bbr_save_cwnd(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat) {
1191 if (!bbr->in_loss_recovery && bbr->state != NGTCP2_BBR_STATE_PROBE_RTT) {
1192 bbr->prior_cwnd = cstat->cwnd;
1193 return;
1194 }
1195
1196 bbr->prior_cwnd = ngtcp2_max_uint64(bbr->prior_cwnd, cstat->cwnd);
1197 }
1198
1199 static void bbr_restore_cwnd(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat) {
1200 cstat->cwnd = ngtcp2_max_uint64(cstat->cwnd, bbr->prior_cwnd);
1201 }
1202
1203 static uint64_t bbr_probe_rtt_cwnd(ngtcp2_cc_bbr *bbr,
1204 ngtcp2_conn_stat *cstat) {
1205 uint64_t probe_rtt_cwnd =
1206 bbr_bdp_multiple(bbr, NGTCP2_BBR_PROBE_RTT_CWND_GAIN_H);
1207 uint64_t mpcwnd = min_pipe_cwnd(cstat->max_tx_udp_payload_size);
1208
1209 return ngtcp2_max_uint64(probe_rtt_cwnd, mpcwnd);
1210 }
1211
1212 static void bbr_bound_cwnd_for_probe_rtt(ngtcp2_cc_bbr *bbr,
1213 ngtcp2_conn_stat *cstat) {
1214 if (bbr->state == NGTCP2_BBR_STATE_PROBE_RTT) {
1215 cstat->cwnd =
1216 ngtcp2_min_uint64(cstat->cwnd, bbr_probe_rtt_cwnd(bbr, cstat));
1217 }
1218 }
1219
1220 static void bbr_set_cwnd(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
1221 const ngtcp2_cc_ack *ack) {
1222 uint64_t mpcwnd;
1223
1224 bbr_update_max_inflight(bbr, cstat);
1225
1226 if (bbr->full_bw_reached) {
1227 cstat->cwnd += ack->bytes_delivered;
1228 cstat->cwnd = ngtcp2_min_uint64(cstat->cwnd, bbr->max_inflight);
1229 } else if (cstat->cwnd < bbr->max_inflight ||
1230 bbr->rst->delivered < bbr->initial_cwnd) {
1231 cstat->cwnd += ack->bytes_delivered;
1232 }
1233
1234 mpcwnd = min_pipe_cwnd(cstat->max_tx_udp_payload_size);
1235 cstat->cwnd = ngtcp2_max_uint64(cstat->cwnd, mpcwnd);
1236
1237 bbr_bound_cwnd_for_probe_rtt(bbr, cstat);
1238 bbr_bound_cwnd_for_model(bbr, cstat);
1239 }
1240
1241 static void bbr_bound_cwnd_for_model(ngtcp2_cc_bbr *bbr,
1242 ngtcp2_conn_stat *cstat) {
1243 uint64_t cap = UINT64_MAX;
1244 uint64_t mpcwnd = min_pipe_cwnd(cstat->max_tx_udp_payload_size);
1245
1246 if (bbr_is_in_probe_bw_state(bbr) &&
1247 bbr->state != NGTCP2_BBR_STATE_PROBE_BW_CRUISE) {
1248 cap = bbr->inflight_longterm;
1249 } else if (bbr->state == NGTCP2_BBR_STATE_PROBE_RTT ||
1250 bbr->state == NGTCP2_BBR_STATE_PROBE_BW_CRUISE) {
1251 cap = bbr_inflight_with_headroom(bbr, cstat);
1252 }
1253
1254 cap = ngtcp2_min_uint64(cap, bbr->inflight_shortterm);
1255 cap = ngtcp2_max_uint64(cap, mpcwnd);
1256
1257 cstat->cwnd = ngtcp2_min_uint64(cstat->cwnd, cap);
1258 }
1259
1260 static void bbr_set_send_quantum(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat) {
1261 size_t send_quantum = 64 * 1024;
1262 (void)bbr;
1263
1264 send_quantum =
1265 ngtcp2_min_size(send_quantum, (size_t)((NGTCP2_MILLISECONDS << 10) /
1266 cstat->pacing_interval_m));
1267
1268 cstat->send_quantum =
1269 ngtcp2_max_size(send_quantum, 2 * cstat->max_tx_udp_payload_size);
1270 }
1271
1272 static int in_congestion_recovery(const ngtcp2_conn_stat *cstat,
1273 ngtcp2_tstamp sent_time) {
1274 return cstat->congestion_recovery_start_ts != UINT64_MAX &&
1275 sent_time <= cstat->congestion_recovery_start_ts;
1276 }
1277
1278 static void bbr_handle_recovery(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
1279 const ngtcp2_cc_ack *ack) {
1280 if (!bbr->in_loss_recovery) {
1281 return;
1282 }
1283
1284 if (ack->largest_pkt_sent_ts != UINT64_MAX &&
1285 !in_congestion_recovery(cstat, ack->largest_pkt_sent_ts)) {
1286 bbr->in_loss_recovery = 0;
1287 bbr->round_count_at_recovery = UINT64_MAX;
1288 bbr_restore_cwnd(bbr, cstat);
1289 }
1290 }
1291
1292 static void bbr_cc_on_pkt_lost(ngtcp2_cc *cc, ngtcp2_conn_stat *cstat,
1293 const ngtcp2_cc_pkt *pkt, ngtcp2_tstamp ts) {
1294 ngtcp2_cc_bbr *bbr = ngtcp2_struct_of(cc, ngtcp2_cc_bbr, cc);
1295
1296 bbr_update_on_loss(bbr, cstat, pkt, ts);
1297 }
1298
1299 static void bbr_cc_congestion_event(ngtcp2_cc *cc, ngtcp2_conn_stat *cstat,
1300 ngtcp2_tstamp sent_ts,
1301 const ngtcp2_cc_ack *ack,
1302 ngtcp2_tstamp ts) {
1303 ngtcp2_cc_bbr *bbr = ngtcp2_struct_of(cc, ngtcp2_cc_bbr, cc);
1304 (void)ack;
1305
1306 if (bbr->in_loss_recovery || in_congestion_recovery(cstat, sent_ts)) {
1307 return;
1308 }
1309
1310 bbr->in_loss_recovery = 1;
1311 bbr->round_count_at_recovery =
1312 bbr->round_start ? bbr->round_count : bbr->round_count + 1;
1313 bbr_save_cwnd(bbr, cstat);
1314 bbr->undo_bw_shortterm = bbr->bw_shortterm;
1315 bbr->undo_inflight_shortterm = bbr->inflight_shortterm;
1316 bbr->undo_inflight_longterm = bbr->inflight_longterm;
1317
1318 cstat->congestion_recovery_start_ts = ts;
1319 }
1320
1321 static void bbr_cc_on_spurious_congestion(ngtcp2_cc *cc,
1322 ngtcp2_conn_stat *cstat,
1323 ngtcp2_tstamp ts) {
1324 ngtcp2_cc_bbr *bbr = ngtcp2_struct_of(cc, ngtcp2_cc_bbr, cc);
1325 (void)ts;
1326
1327 cstat->congestion_recovery_start_ts = UINT64_MAX;
1328
1329 bbr->in_loss_recovery = 0;
1330 bbr->round_count_at_recovery = UINT64_MAX;
1331 bbr_reset_full_bw(bbr);
1332 bbr->loss_in_round = 0;
1333 bbr_restore_cwnd(bbr, cstat);
1334 bbr->bw_shortterm =
1335 ngtcp2_max_uint64(bbr->bw_shortterm, bbr->undo_bw_shortterm);
1336 bbr->inflight_shortterm =
1337 ngtcp2_max_uint64(bbr->inflight_shortterm, bbr->undo_inflight_shortterm);
1338 bbr->inflight_longterm =
1339 ngtcp2_max_uint64(bbr->inflight_longterm, bbr->undo_inflight_longterm);
1340 }
1341
1342 static void bbr_cc_on_persistent_congestion(ngtcp2_cc *cc,
1343 ngtcp2_conn_stat *cstat,
1344 ngtcp2_tstamp ts) {
1345 ngtcp2_cc_bbr *bbr = ngtcp2_struct_of(cc, ngtcp2_cc_bbr, cc);
1346 (void)ts;
1347
1348 cstat->congestion_recovery_start_ts = UINT64_MAX;
1349 bbr->in_loss_recovery = 0;
1350 bbr->round_count_at_recovery = UINT64_MAX;
1351
1352 bbr_save_cwnd(bbr, cstat);
1353 cstat->cwnd = cstat->bytes_in_flight + cstat->max_tx_udp_payload_size;
1354 cstat->cwnd = ngtcp2_max_uint64(
1355 cstat->cwnd, min_pipe_cwnd(cstat->max_tx_udp_payload_size));
1356 }
1357
1358 static void bbr_cc_on_ack_recv(ngtcp2_cc *cc, ngtcp2_conn_stat *cstat,
1359 const ngtcp2_cc_ack *ack, ngtcp2_tstamp ts) {
1360 ngtcp2_cc_bbr *bbr = ngtcp2_struct_of(cc, ngtcp2_cc_bbr, cc);
1361
1362 bbr_handle_recovery(bbr, cstat, ack);
1363
1364 if (ack->bytes_delivered == 0) {
1365 return;
1366 }
1367
1368 bbr_update_on_ack(bbr, cstat, ack, ts);
1369 }
1370
1371 static void bbr_cc_on_pkt_sent(ngtcp2_cc *cc, ngtcp2_conn_stat *cstat,
1372 const ngtcp2_cc_pkt *pkt) {
1373 ngtcp2_cc_bbr *bbr = ngtcp2_struct_of(cc, ngtcp2_cc_bbr, cc);
1374
1375 bbr_on_transmit(bbr, cstat, pkt->sent_ts);
1376 }
1377
1378 static void bbr_cc_reset(ngtcp2_cc *cc, ngtcp2_conn_stat *cstat,
1379 ngtcp2_tstamp ts) {
1380 ngtcp2_cc_bbr *bbr = ngtcp2_struct_of(cc, ngtcp2_cc_bbr, cc);
1381
1382 bbr_on_init(bbr, cstat, ts);
1383 }
1384
1385 void ngtcp2_cc_bbr_init(ngtcp2_cc_bbr *bbr, ngtcp2_log *log,
1386 ngtcp2_conn_stat *cstat, ngtcp2_rst *rst,
1387 ngtcp2_tstamp initial_ts, ngtcp2_pcg32 *pcg) {
1388 *bbr = (ngtcp2_cc_bbr){
1389 .cc =
1390 {
1391 .log = log,
1392 .on_pkt_lost = bbr_cc_on_pkt_lost,
1393 .congestion_event = bbr_cc_congestion_event,
1394 .on_spurious_congestion = bbr_cc_on_spurious_congestion,
1395 .on_persistent_congestion = bbr_cc_on_persistent_congestion,
1396 .on_ack_recv = bbr_cc_on_ack_recv,
1397 .on_pkt_sent = bbr_cc_on_pkt_sent,
1398 .reset = bbr_cc_reset,
1399 },
1400 .rst = rst,
1401 .pcg = pcg,
1402 .initial_cwnd = cstat->cwnd,
1403 };
1404
1405 bbr_on_init(bbr, cstat, initial_ts);
1406 }