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#include <linux/module.h>
60#include <net/tcp.h>
61#include <linux/inet_diag.h>
62#include <linux/inet.h>
63#include <linux/random.h>
64#include <linux/win_minmax.h>
65
66
67
68
69
70
71
72#define BW_SCALE 24
73#define BW_UNIT (1 << BW_SCALE)
74
75#define BBR_SCALE 8
76#define BBR_UNIT (1 << BBR_SCALE)
77
78
79enum bbr_mode {
80 BBR_STARTUP,
81 BBR_DRAIN,
82 BBR_PROBE_BW,
83 BBR_PROBE_RTT,
84};
85
86
87struct bbr {
88 u32 min_rtt_us;
89 u32 min_rtt_stamp;
90 u32 probe_rtt_done_stamp;
91 struct minmax bw;
92 u32 rtt_cnt;
93 u32 next_rtt_delivered;
94 u64 cycle_mstamp;
95 u32 mode:3,
96 prev_ca_state:3,
97 packet_conservation:1,
98 round_start:1,
99 idle_restart:1,
100 probe_rtt_round_done:1,
101 unused:13,
102 lt_is_sampling:1,
103 lt_rtt_cnt:7,
104 lt_use_bw:1;
105 u32 lt_bw;
106 u32 lt_last_delivered;
107 u32 lt_last_stamp;
108 u32 lt_last_lost;
109 u32 pacing_gain:10,
110 cwnd_gain:10,
111 full_bw_reached:1,
112 full_bw_cnt:2,
113 cycle_idx:3,
114 has_seen_rtt:1,
115 unused_b:5;
116 u32 prior_cwnd;
117 u32 full_bw;
118
119
120 u64 ack_epoch_mstamp;
121 u16 extra_acked[2];
122 u32 ack_epoch_acked:20,
123 extra_acked_win_rtts:5,
124 extra_acked_win_idx:1,
125 unused_c:6;
126};
127
128#define CYCLE_LEN 8
129
130
131static const int bbr_bw_rtts = CYCLE_LEN + 2;
132
133static const u32 bbr_min_rtt_win_sec = 10;
134
135static const u32 bbr_probe_rtt_mode_ms = 200;
136
137static const int bbr_min_tso_rate = 1200000;
138
139
140
141
142
143
144
145static const int bbr_pacing_margin_percent = 1;
146
147
148
149
150
151
152static const int bbr_high_gain = BBR_UNIT * 2885 / 1000 + 1;
153
154
155
156static const int bbr_drain_gain = BBR_UNIT * 1000 / 2885;
157
158static const int bbr_cwnd_gain = BBR_UNIT * 2;
159
160static const int bbr_pacing_gain[] = {
161 BBR_UNIT * 5 / 4,
162 BBR_UNIT * 3 / 4,
163 BBR_UNIT, BBR_UNIT, BBR_UNIT,
164 BBR_UNIT, BBR_UNIT, BBR_UNIT
165};
166
167static const u32 bbr_cycle_rand = 7;
168
169
170
171
172
173static const u32 bbr_cwnd_min_target = 4;
174
175
176
177static const u32 bbr_full_bw_thresh = BBR_UNIT * 5 / 4;
178
179static const u32 bbr_full_bw_cnt = 3;
180
181
182
183static const u32 bbr_lt_intvl_min_rtts = 4;
184
185static const u32 bbr_lt_loss_thresh = 50;
186
187static const u32 bbr_lt_bw_ratio = BBR_UNIT / 8;
188
189static const u32 bbr_lt_bw_diff = 4000 / 8;
190
191static const u32 bbr_lt_bw_max_rtts = 48;
192
193
194static const int bbr_extra_acked_gain = BBR_UNIT;
195
196static const u32 bbr_extra_acked_win_rtts = 5;
197
198static const u32 bbr_ack_epoch_acked_reset_thresh = 1U << 20;
199
200static const u32 bbr_extra_acked_max_us = 100 * 1000;
201
202static void bbr_check_probe_rtt_done(struct sock *sk);
203
204
205static bool bbr_full_bw_reached(const struct sock *sk)
206{
207 const struct bbr *bbr = inet_csk_ca(sk);
208
209 return bbr->full_bw_reached;
210}
211
212
213static u32 bbr_max_bw(const struct sock *sk)
214{
215 struct bbr *bbr = inet_csk_ca(sk);
216
217 return minmax_get(&bbr->bw);
218}
219
220
221static u32 bbr_bw(const struct sock *sk)
222{
223 struct bbr *bbr = inet_csk_ca(sk);
224
225 return bbr->lt_use_bw ? bbr->lt_bw : bbr_max_bw(sk);
226}
227
228
229
230
231static u16 bbr_extra_acked(const struct sock *sk)
232{
233 struct bbr *bbr = inet_csk_ca(sk);
234
235 return max(bbr->extra_acked[0], bbr->extra_acked[1]);
236}
237
238
239
240
241
242static u64 bbr_rate_bytes_per_sec(struct sock *sk, u64 rate, int gain)
243{
244 unsigned int mss = tcp_sk(sk)->mss_cache;
245
246 rate *= mss;
247 rate *= gain;
248 rate >>= BBR_SCALE;
249 rate *= USEC_PER_SEC / 100 * (100 - bbr_pacing_margin_percent);
250 return rate >> BW_SCALE;
251}
252
253
254static unsigned long bbr_bw_to_pacing_rate(struct sock *sk, u32 bw, int gain)
255{
256 u64 rate = bw;
257
258 rate = bbr_rate_bytes_per_sec(sk, rate, gain);
259 rate = min_t(u64, rate, sk->sk_max_pacing_rate);
260 return rate;
261}
262
263
264static void bbr_init_pacing_rate_from_rtt(struct sock *sk)
265{
266 struct tcp_sock *tp = tcp_sk(sk);
267 struct bbr *bbr = inet_csk_ca(sk);
268 u64 bw;
269 u32 rtt_us;
270
271 if (tp->srtt_us) {
272 rtt_us = max(tp->srtt_us >> 3, 1U);
273 bbr->has_seen_rtt = 1;
274 } else {
275 rtt_us = USEC_PER_MSEC;
276 }
277 bw = (u64)tp->snd_cwnd * BW_UNIT;
278 do_div(bw, rtt_us);
279 sk->sk_pacing_rate = bbr_bw_to_pacing_rate(sk, bw, bbr_high_gain);
280}
281
282
283static void bbr_set_pacing_rate(struct sock *sk, u32 bw, int gain)
284{
285 struct tcp_sock *tp = tcp_sk(sk);
286 struct bbr *bbr = inet_csk_ca(sk);
287 unsigned long rate = bbr_bw_to_pacing_rate(sk, bw, gain);
288
289 if (unlikely(!bbr->has_seen_rtt && tp->srtt_us))
290 bbr_init_pacing_rate_from_rtt(sk);
291 if (bbr_full_bw_reached(sk) || rate > sk->sk_pacing_rate)
292 sk->sk_pacing_rate = rate;
293}
294
295
296static u32 bbr_min_tso_segs(struct sock *sk)
297{
298 return sk->sk_pacing_rate < (bbr_min_tso_rate >> 3) ? 1 : 2;
299}
300
301static u32 bbr_tso_segs_goal(struct sock *sk)
302{
303 struct tcp_sock *tp = tcp_sk(sk);
304 u32 segs, bytes;
305
306
307
308
309 bytes = min_t(unsigned long,
310 sk->sk_pacing_rate >> READ_ONCE(sk->sk_pacing_shift),
311 GSO_MAX_SIZE - 1 - MAX_TCP_HEADER);
312 segs = max_t(u32, bytes / tp->mss_cache, bbr_min_tso_segs(sk));
313
314 return min(segs, 0x7FU);
315}
316
317
318static void bbr_save_cwnd(struct sock *sk)
319{
320 struct tcp_sock *tp = tcp_sk(sk);
321 struct bbr *bbr = inet_csk_ca(sk);
322
323 if (bbr->prev_ca_state < TCP_CA_Recovery && bbr->mode != BBR_PROBE_RTT)
324 bbr->prior_cwnd = tp->snd_cwnd;
325 else
326 bbr->prior_cwnd = max(bbr->prior_cwnd, tp->snd_cwnd);
327}
328
329static void bbr_cwnd_event(struct sock *sk, enum tcp_ca_event event)
330{
331 struct tcp_sock *tp = tcp_sk(sk);
332 struct bbr *bbr = inet_csk_ca(sk);
333
334 if (event == CA_EVENT_TX_START && tp->app_limited) {
335 bbr->idle_restart = 1;
336 bbr->ack_epoch_mstamp = tp->tcp_mstamp;
337 bbr->ack_epoch_acked = 0;
338
339
340
341 if (bbr->mode == BBR_PROBE_BW)
342 bbr_set_pacing_rate(sk, bbr_bw(sk), BBR_UNIT);
343 else if (bbr->mode == BBR_PROBE_RTT)
344 bbr_check_probe_rtt_done(sk);
345 }
346}
347
348
349
350
351
352
353
354
355
356
357static u32 bbr_bdp(struct sock *sk, u32 bw, int gain)
358{
359 struct bbr *bbr = inet_csk_ca(sk);
360 u32 bdp;
361 u64 w;
362
363
364
365
366
367
368
369 if (unlikely(bbr->min_rtt_us == ~0U))
370 return TCP_INIT_CWND;
371
372 w = (u64)bw * bbr->min_rtt_us;
373
374
375
376
377 bdp = (((w * gain) >> BBR_SCALE) + BW_UNIT - 1) / BW_UNIT;
378
379 return bdp;
380}
381
382
383
384
385
386
387
388
389
390
391
392static u32 bbr_quantization_budget(struct sock *sk, u32 cwnd)
393{
394 struct bbr *bbr = inet_csk_ca(sk);
395
396
397 cwnd += 3 * bbr_tso_segs_goal(sk);
398
399
400 cwnd = (cwnd + 1) & ~1U;
401
402
403 if (bbr->mode == BBR_PROBE_BW && bbr->cycle_idx == 0)
404 cwnd += 2;
405
406 return cwnd;
407}
408
409
410static u32 bbr_inflight(struct sock *sk, u32 bw, int gain)
411{
412 u32 inflight;
413
414 inflight = bbr_bdp(sk, bw, gain);
415 inflight = bbr_quantization_budget(sk, inflight);
416
417 return inflight;
418}
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434static u32 bbr_packets_in_net_at_edt(struct sock *sk, u32 inflight_now)
435{
436 struct tcp_sock *tp = tcp_sk(sk);
437 struct bbr *bbr = inet_csk_ca(sk);
438 u64 now_ns, edt_ns, interval_us;
439 u32 interval_delivered, inflight_at_edt;
440
441 now_ns = tp->tcp_clock_cache;
442 edt_ns = max(tp->tcp_wstamp_ns, now_ns);
443 interval_us = div_u64(edt_ns - now_ns, NSEC_PER_USEC);
444 interval_delivered = (u64)bbr_bw(sk) * interval_us >> BW_SCALE;
445 inflight_at_edt = inflight_now;
446 if (bbr->pacing_gain > BBR_UNIT)
447 inflight_at_edt += bbr_tso_segs_goal(sk);
448 if (interval_delivered >= inflight_at_edt)
449 return 0;
450 return inflight_at_edt - interval_delivered;
451}
452
453
454static u32 bbr_ack_aggregation_cwnd(struct sock *sk)
455{
456 u32 max_aggr_cwnd, aggr_cwnd = 0;
457
458 if (bbr_extra_acked_gain && bbr_full_bw_reached(sk)) {
459 max_aggr_cwnd = ((u64)bbr_bw(sk) * bbr_extra_acked_max_us)
460 / BW_UNIT;
461 aggr_cwnd = (bbr_extra_acked_gain * bbr_extra_acked(sk))
462 >> BBR_SCALE;
463 aggr_cwnd = min(aggr_cwnd, max_aggr_cwnd);
464 }
465
466 return aggr_cwnd;
467}
468
469
470
471
472
473
474
475
476
477static bool bbr_set_cwnd_to_recover_or_restore(
478 struct sock *sk, const struct rate_sample *rs, u32 acked, u32 *new_cwnd)
479{
480 struct tcp_sock *tp = tcp_sk(sk);
481 struct bbr *bbr = inet_csk_ca(sk);
482 u8 prev_state = bbr->prev_ca_state, state = inet_csk(sk)->icsk_ca_state;
483 u32 cwnd = tp->snd_cwnd;
484
485
486
487
488
489 if (rs->losses > 0)
490 cwnd = max_t(s32, cwnd - rs->losses, 1);
491
492 if (state == TCP_CA_Recovery && prev_state != TCP_CA_Recovery) {
493
494 bbr->packet_conservation = 1;
495 bbr->next_rtt_delivered = tp->delivered;
496
497 cwnd = tcp_packets_in_flight(tp) + acked;
498 } else if (prev_state >= TCP_CA_Recovery && state < TCP_CA_Recovery) {
499
500 cwnd = max(cwnd, bbr->prior_cwnd);
501 bbr->packet_conservation = 0;
502 }
503 bbr->prev_ca_state = state;
504
505 if (bbr->packet_conservation) {
506 *new_cwnd = max(cwnd, tcp_packets_in_flight(tp) + acked);
507 return true;
508 }
509 *new_cwnd = cwnd;
510 return false;
511}
512
513
514
515
516static void bbr_set_cwnd(struct sock *sk, const struct rate_sample *rs,
517 u32 acked, u32 bw, int gain)
518{
519 struct tcp_sock *tp = tcp_sk(sk);
520 struct bbr *bbr = inet_csk_ca(sk);
521 u32 cwnd = tp->snd_cwnd, target_cwnd = 0;
522
523 if (!acked)
524 goto done;
525
526 if (bbr_set_cwnd_to_recover_or_restore(sk, rs, acked, &cwnd))
527 goto done;
528
529 target_cwnd = bbr_bdp(sk, bw, gain);
530
531
532
533
534 target_cwnd += bbr_ack_aggregation_cwnd(sk);
535 target_cwnd = bbr_quantization_budget(sk, target_cwnd);
536
537
538 if (bbr_full_bw_reached(sk))
539 cwnd = min(cwnd + acked, target_cwnd);
540 else if (cwnd < target_cwnd || tp->delivered < TCP_INIT_CWND)
541 cwnd = cwnd + acked;
542 cwnd = max(cwnd, bbr_cwnd_min_target);
543
544done:
545 tp->snd_cwnd = min(cwnd, tp->snd_cwnd_clamp);
546 if (bbr->mode == BBR_PROBE_RTT)
547 tp->snd_cwnd = min(tp->snd_cwnd, bbr_cwnd_min_target);
548}
549
550
551static bool bbr_is_next_cycle_phase(struct sock *sk,
552 const struct rate_sample *rs)
553{
554 struct tcp_sock *tp = tcp_sk(sk);
555 struct bbr *bbr = inet_csk_ca(sk);
556 bool is_full_length =
557 tcp_stamp_us_delta(tp->delivered_mstamp, bbr->cycle_mstamp) >
558 bbr->min_rtt_us;
559 u32 inflight, bw;
560
561
562
563
564 if (bbr->pacing_gain == BBR_UNIT)
565 return is_full_length;
566
567 inflight = bbr_packets_in_net_at_edt(sk, rs->prior_in_flight);
568 bw = bbr_max_bw(sk);
569
570
571
572
573
574
575 if (bbr->pacing_gain > BBR_UNIT)
576 return is_full_length &&
577 (rs->losses ||
578 inflight >= bbr_inflight(sk, bw, bbr->pacing_gain));
579
580
581
582
583
584 return is_full_length ||
585 inflight <= bbr_inflight(sk, bw, BBR_UNIT);
586}
587
588static void bbr_advance_cycle_phase(struct sock *sk)
589{
590 struct tcp_sock *tp = tcp_sk(sk);
591 struct bbr *bbr = inet_csk_ca(sk);
592
593 bbr->cycle_idx = (bbr->cycle_idx + 1) & (CYCLE_LEN - 1);
594 bbr->cycle_mstamp = tp->delivered_mstamp;
595}
596
597
598static void bbr_update_cycle_phase(struct sock *sk,
599 const struct rate_sample *rs)
600{
601 struct bbr *bbr = inet_csk_ca(sk);
602
603 if (bbr->mode == BBR_PROBE_BW && bbr_is_next_cycle_phase(sk, rs))
604 bbr_advance_cycle_phase(sk);
605}
606
607static void bbr_reset_startup_mode(struct sock *sk)
608{
609 struct bbr *bbr = inet_csk_ca(sk);
610
611 bbr->mode = BBR_STARTUP;
612}
613
614static void bbr_reset_probe_bw_mode(struct sock *sk)
615{
616 struct bbr *bbr = inet_csk_ca(sk);
617
618 bbr->mode = BBR_PROBE_BW;
619 bbr->cycle_idx = CYCLE_LEN - 1 - prandom_u32_max(bbr_cycle_rand);
620 bbr_advance_cycle_phase(sk);
621}
622
623static void bbr_reset_mode(struct sock *sk)
624{
625 if (!bbr_full_bw_reached(sk))
626 bbr_reset_startup_mode(sk);
627 else
628 bbr_reset_probe_bw_mode(sk);
629}
630
631
632static void bbr_reset_lt_bw_sampling_interval(struct sock *sk)
633{
634 struct tcp_sock *tp = tcp_sk(sk);
635 struct bbr *bbr = inet_csk_ca(sk);
636
637 bbr->lt_last_stamp = div_u64(tp->delivered_mstamp, USEC_PER_MSEC);
638 bbr->lt_last_delivered = tp->delivered;
639 bbr->lt_last_lost = tp->lost;
640 bbr->lt_rtt_cnt = 0;
641}
642
643
644static void bbr_reset_lt_bw_sampling(struct sock *sk)
645{
646 struct bbr *bbr = inet_csk_ca(sk);
647
648 bbr->lt_bw = 0;
649 bbr->lt_use_bw = 0;
650 bbr->lt_is_sampling = false;
651 bbr_reset_lt_bw_sampling_interval(sk);
652}
653
654
655static void bbr_lt_bw_interval_done(struct sock *sk, u32 bw)
656{
657 struct bbr *bbr = inet_csk_ca(sk);
658 u32 diff;
659
660 if (bbr->lt_bw) {
661
662 diff = abs(bw - bbr->lt_bw);
663 if ((diff * BBR_UNIT <= bbr_lt_bw_ratio * bbr->lt_bw) ||
664 (bbr_rate_bytes_per_sec(sk, diff, BBR_UNIT) <=
665 bbr_lt_bw_diff)) {
666
667 bbr->lt_bw = (bw + bbr->lt_bw) >> 1;
668 bbr->lt_use_bw = 1;
669 bbr->pacing_gain = BBR_UNIT;
670 bbr->lt_rtt_cnt = 0;
671 return;
672 }
673 }
674 bbr->lt_bw = bw;
675 bbr_reset_lt_bw_sampling_interval(sk);
676}
677
678
679
680
681
682
683
684
685static void bbr_lt_bw_sampling(struct sock *sk, const struct rate_sample *rs)
686{
687 struct tcp_sock *tp = tcp_sk(sk);
688 struct bbr *bbr = inet_csk_ca(sk);
689 u32 lost, delivered;
690 u64 bw;
691 u32 t;
692
693 if (bbr->lt_use_bw) {
694 if (bbr->mode == BBR_PROBE_BW && bbr->round_start &&
695 ++bbr->lt_rtt_cnt >= bbr_lt_bw_max_rtts) {
696 bbr_reset_lt_bw_sampling(sk);
697 bbr_reset_probe_bw_mode(sk);
698 }
699 return;
700 }
701
702
703
704
705
706 if (!bbr->lt_is_sampling) {
707 if (!rs->losses)
708 return;
709 bbr_reset_lt_bw_sampling_interval(sk);
710 bbr->lt_is_sampling = true;
711 }
712
713
714 if (rs->is_app_limited) {
715 bbr_reset_lt_bw_sampling(sk);
716 return;
717 }
718
719 if (bbr->round_start)
720 bbr->lt_rtt_cnt++;
721 if (bbr->lt_rtt_cnt < bbr_lt_intvl_min_rtts)
722 return;
723 if (bbr->lt_rtt_cnt > 4 * bbr_lt_intvl_min_rtts) {
724 bbr_reset_lt_bw_sampling(sk);
725 return;
726 }
727
728
729
730
731
732 if (!rs->losses)
733 return;
734
735
736 lost = tp->lost - bbr->lt_last_lost;
737 delivered = tp->delivered - bbr->lt_last_delivered;
738
739 if (!delivered || (lost << BBR_SCALE) < bbr_lt_loss_thresh * delivered)
740 return;
741
742
743 t = div_u64(tp->delivered_mstamp, USEC_PER_MSEC) - bbr->lt_last_stamp;
744 if ((s32)t < 1)
745 return;
746
747 if (t >= ~0U / USEC_PER_MSEC) {
748 bbr_reset_lt_bw_sampling(sk);
749 return;
750 }
751 t *= USEC_PER_MSEC;
752 bw = (u64)delivered * BW_UNIT;
753 do_div(bw, t);
754 bbr_lt_bw_interval_done(sk, bw);
755}
756
757
758static void bbr_update_bw(struct sock *sk, const struct rate_sample *rs)
759{
760 struct tcp_sock *tp = tcp_sk(sk);
761 struct bbr *bbr = inet_csk_ca(sk);
762 u64 bw;
763
764 bbr->round_start = 0;
765 if (rs->delivered < 0 || rs->interval_us <= 0)
766 return;
767
768
769 if (!before(rs->prior_delivered, bbr->next_rtt_delivered)) {
770 bbr->next_rtt_delivered = tp->delivered;
771 bbr->rtt_cnt++;
772 bbr->round_start = 1;
773 bbr->packet_conservation = 0;
774 }
775
776 bbr_lt_bw_sampling(sk, rs);
777
778
779
780
781
782 bw = div64_long((u64)rs->delivered * BW_UNIT, rs->interval_us);
783
784
785
786
787
788
789
790
791
792
793
794
795 if (!rs->is_app_limited || bw >= bbr_max_bw(sk)) {
796
797 minmax_running_max(&bbr->bw, bbr_bw_rtts, bbr->rtt_cnt, bw);
798 }
799}
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814static void bbr_update_ack_aggregation(struct sock *sk,
815 const struct rate_sample *rs)
816{
817 u32 epoch_us, expected_acked, extra_acked;
818 struct bbr *bbr = inet_csk_ca(sk);
819 struct tcp_sock *tp = tcp_sk(sk);
820
821 if (!bbr_extra_acked_gain || rs->acked_sacked <= 0 ||
822 rs->delivered < 0 || rs->interval_us <= 0)
823 return;
824
825 if (bbr->round_start) {
826 bbr->extra_acked_win_rtts = min(0x1F,
827 bbr->extra_acked_win_rtts + 1);
828 if (bbr->extra_acked_win_rtts >= bbr_extra_acked_win_rtts) {
829 bbr->extra_acked_win_rtts = 0;
830 bbr->extra_acked_win_idx = bbr->extra_acked_win_idx ?
831 0 : 1;
832 bbr->extra_acked[bbr->extra_acked_win_idx] = 0;
833 }
834 }
835
836
837 epoch_us = tcp_stamp_us_delta(tp->delivered_mstamp,
838 bbr->ack_epoch_mstamp);
839 expected_acked = ((u64)bbr_bw(sk) * epoch_us) / BW_UNIT;
840
841
842
843
844
845 if (bbr->ack_epoch_acked <= expected_acked ||
846 (bbr->ack_epoch_acked + rs->acked_sacked >=
847 bbr_ack_epoch_acked_reset_thresh)) {
848 bbr->ack_epoch_acked = 0;
849 bbr->ack_epoch_mstamp = tp->delivered_mstamp;
850 expected_acked = 0;
851 }
852
853
854 bbr->ack_epoch_acked = min_t(u32, 0xFFFFF,
855 bbr->ack_epoch_acked + rs->acked_sacked);
856 extra_acked = bbr->ack_epoch_acked - expected_acked;
857 extra_acked = min(extra_acked, tp->snd_cwnd);
858 if (extra_acked > bbr->extra_acked[bbr->extra_acked_win_idx])
859 bbr->extra_acked[bbr->extra_acked_win_idx] = extra_acked;
860}
861
862
863
864
865
866
867
868
869
870static void bbr_check_full_bw_reached(struct sock *sk,
871 const struct rate_sample *rs)
872{
873 struct bbr *bbr = inet_csk_ca(sk);
874 u32 bw_thresh;
875
876 if (bbr_full_bw_reached(sk) || !bbr->round_start || rs->is_app_limited)
877 return;
878
879 bw_thresh = (u64)bbr->full_bw * bbr_full_bw_thresh >> BBR_SCALE;
880 if (bbr_max_bw(sk) >= bw_thresh) {
881 bbr->full_bw = bbr_max_bw(sk);
882 bbr->full_bw_cnt = 0;
883 return;
884 }
885 ++bbr->full_bw_cnt;
886 bbr->full_bw_reached = bbr->full_bw_cnt >= bbr_full_bw_cnt;
887}
888
889
890static void bbr_check_drain(struct sock *sk, const struct rate_sample *rs)
891{
892 struct bbr *bbr = inet_csk_ca(sk);
893
894 if (bbr->mode == BBR_STARTUP && bbr_full_bw_reached(sk)) {
895 bbr->mode = BBR_DRAIN;
896 tcp_sk(sk)->snd_ssthresh =
897 bbr_inflight(sk, bbr_max_bw(sk), BBR_UNIT);
898 }
899 if (bbr->mode == BBR_DRAIN &&
900 bbr_packets_in_net_at_edt(sk, tcp_packets_in_flight(tcp_sk(sk))) <=
901 bbr_inflight(sk, bbr_max_bw(sk), BBR_UNIT))
902 bbr_reset_probe_bw_mode(sk);
903}
904
905static void bbr_check_probe_rtt_done(struct sock *sk)
906{
907 struct tcp_sock *tp = tcp_sk(sk);
908 struct bbr *bbr = inet_csk_ca(sk);
909
910 if (!(bbr->probe_rtt_done_stamp &&
911 after(tcp_jiffies32, bbr->probe_rtt_done_stamp)))
912 return;
913
914 bbr->min_rtt_stamp = tcp_jiffies32;
915 tp->snd_cwnd = max(tp->snd_cwnd, bbr->prior_cwnd);
916 bbr_reset_mode(sk);
917}
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938static void bbr_update_min_rtt(struct sock *sk, const struct rate_sample *rs)
939{
940 struct tcp_sock *tp = tcp_sk(sk);
941 struct bbr *bbr = inet_csk_ca(sk);
942 bool filter_expired;
943
944
945 filter_expired = after(tcp_jiffies32,
946 bbr->min_rtt_stamp + bbr_min_rtt_win_sec * HZ);
947 if (rs->rtt_us >= 0 &&
948 (rs->rtt_us <= bbr->min_rtt_us ||
949 (filter_expired && !rs->is_ack_delayed))) {
950 bbr->min_rtt_us = rs->rtt_us;
951 bbr->min_rtt_stamp = tcp_jiffies32;
952 }
953
954 if (bbr_probe_rtt_mode_ms > 0 && filter_expired &&
955 !bbr->idle_restart && bbr->mode != BBR_PROBE_RTT) {
956 bbr->mode = BBR_PROBE_RTT;
957 bbr_save_cwnd(sk);
958 bbr->probe_rtt_done_stamp = 0;
959 }
960
961 if (bbr->mode == BBR_PROBE_RTT) {
962
963 tp->app_limited =
964 (tp->delivered + tcp_packets_in_flight(tp)) ? : 1;
965
966 if (!bbr->probe_rtt_done_stamp &&
967 tcp_packets_in_flight(tp) <= bbr_cwnd_min_target) {
968 bbr->probe_rtt_done_stamp = tcp_jiffies32 +
969 msecs_to_jiffies(bbr_probe_rtt_mode_ms);
970 bbr->probe_rtt_round_done = 0;
971 bbr->next_rtt_delivered = tp->delivered;
972 } else if (bbr->probe_rtt_done_stamp) {
973 if (bbr->round_start)
974 bbr->probe_rtt_round_done = 1;
975 if (bbr->probe_rtt_round_done)
976 bbr_check_probe_rtt_done(sk);
977 }
978 }
979
980 if (rs->delivered > 0)
981 bbr->idle_restart = 0;
982}
983
984static void bbr_update_gains(struct sock *sk)
985{
986 struct bbr *bbr = inet_csk_ca(sk);
987
988 switch (bbr->mode) {
989 case BBR_STARTUP:
990 bbr->pacing_gain = bbr_high_gain;
991 bbr->cwnd_gain = bbr_high_gain;
992 break;
993 case BBR_DRAIN:
994 bbr->pacing_gain = bbr_drain_gain;
995 bbr->cwnd_gain = bbr_high_gain;
996 break;
997 case BBR_PROBE_BW:
998 bbr->pacing_gain = (bbr->lt_use_bw ?
999 BBR_UNIT :
1000 bbr_pacing_gain[bbr->cycle_idx]);
1001 bbr->cwnd_gain = bbr_cwnd_gain;
1002 break;
1003 case BBR_PROBE_RTT:
1004 bbr->pacing_gain = BBR_UNIT;
1005 bbr->cwnd_gain = BBR_UNIT;
1006 break;
1007 default:
1008 WARN_ONCE(1, "BBR bad mode: %u\n", bbr->mode);
1009 break;
1010 }
1011}
1012
1013static void bbr_update_model(struct sock *sk, const struct rate_sample *rs)
1014{
1015 bbr_update_bw(sk, rs);
1016 bbr_update_ack_aggregation(sk, rs);
1017 bbr_update_cycle_phase(sk, rs);
1018 bbr_check_full_bw_reached(sk, rs);
1019 bbr_check_drain(sk, rs);
1020 bbr_update_min_rtt(sk, rs);
1021 bbr_update_gains(sk);
1022}
1023
1024static void bbr_main(struct sock *sk, const struct rate_sample *rs)
1025{
1026 struct bbr *bbr = inet_csk_ca(sk);
1027 u32 bw;
1028
1029 bbr_update_model(sk, rs);
1030
1031 bw = bbr_bw(sk);
1032 bbr_set_pacing_rate(sk, bw, bbr->pacing_gain);
1033 bbr_set_cwnd(sk, rs, rs->acked_sacked, bw, bbr->cwnd_gain);
1034}
1035
1036static void bbr_init(struct sock *sk)
1037{
1038 struct tcp_sock *tp = tcp_sk(sk);
1039 struct bbr *bbr = inet_csk_ca(sk);
1040
1041 bbr->prior_cwnd = 0;
1042 tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
1043 bbr->rtt_cnt = 0;
1044 bbr->next_rtt_delivered = 0;
1045 bbr->prev_ca_state = TCP_CA_Open;
1046 bbr->packet_conservation = 0;
1047
1048 bbr->probe_rtt_done_stamp = 0;
1049 bbr->probe_rtt_round_done = 0;
1050 bbr->min_rtt_us = tcp_min_rtt(tp);
1051 bbr->min_rtt_stamp = tcp_jiffies32;
1052
1053 minmax_reset(&bbr->bw, bbr->rtt_cnt, 0);
1054
1055 bbr->has_seen_rtt = 0;
1056 bbr_init_pacing_rate_from_rtt(sk);
1057
1058 bbr->round_start = 0;
1059 bbr->idle_restart = 0;
1060 bbr->full_bw_reached = 0;
1061 bbr->full_bw = 0;
1062 bbr->full_bw_cnt = 0;
1063 bbr->cycle_mstamp = 0;
1064 bbr->cycle_idx = 0;
1065 bbr_reset_lt_bw_sampling(sk);
1066 bbr_reset_startup_mode(sk);
1067
1068 bbr->ack_epoch_mstamp = tp->tcp_mstamp;
1069 bbr->ack_epoch_acked = 0;
1070 bbr->extra_acked_win_rtts = 0;
1071 bbr->extra_acked_win_idx = 0;
1072 bbr->extra_acked[0] = 0;
1073 bbr->extra_acked[1] = 0;
1074
1075 cmpxchg(&sk->sk_pacing_status, SK_PACING_NONE, SK_PACING_NEEDED);
1076}
1077
1078static u32 bbr_sndbuf_expand(struct sock *sk)
1079{
1080
1081 return 3;
1082}
1083
1084
1085
1086
1087static u32 bbr_undo_cwnd(struct sock *sk)
1088{
1089 struct bbr *bbr = inet_csk_ca(sk);
1090
1091 bbr->full_bw = 0;
1092 bbr->full_bw_cnt = 0;
1093 bbr_reset_lt_bw_sampling(sk);
1094 return tcp_sk(sk)->snd_cwnd;
1095}
1096
1097
1098static u32 bbr_ssthresh(struct sock *sk)
1099{
1100 bbr_save_cwnd(sk);
1101 return tcp_sk(sk)->snd_ssthresh;
1102}
1103
1104static size_t bbr_get_info(struct sock *sk, u32 ext, int *attr,
1105 union tcp_cc_info *info)
1106{
1107 if (ext & (1 << (INET_DIAG_BBRINFO - 1)) ||
1108 ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
1109 struct tcp_sock *tp = tcp_sk(sk);
1110 struct bbr *bbr = inet_csk_ca(sk);
1111 u64 bw = bbr_bw(sk);
1112
1113 bw = bw * tp->mss_cache * USEC_PER_SEC >> BW_SCALE;
1114 memset(&info->bbr, 0, sizeof(info->bbr));
1115 info->bbr.bbr_bw_lo = (u32)bw;
1116 info->bbr.bbr_bw_hi = (u32)(bw >> 32);
1117 info->bbr.bbr_min_rtt = bbr->min_rtt_us;
1118 info->bbr.bbr_pacing_gain = bbr->pacing_gain;
1119 info->bbr.bbr_cwnd_gain = bbr->cwnd_gain;
1120 *attr = INET_DIAG_BBRINFO;
1121 return sizeof(info->bbr);
1122 }
1123 return 0;
1124}
1125
1126static void bbr_set_state(struct sock *sk, u8 new_state)
1127{
1128 struct bbr *bbr = inet_csk_ca(sk);
1129
1130 if (new_state == TCP_CA_Loss) {
1131 struct rate_sample rs = { .losses = 1 };
1132
1133 bbr->prev_ca_state = TCP_CA_Loss;
1134 bbr->full_bw = 0;
1135 bbr->round_start = 1;
1136 bbr_lt_bw_sampling(sk, &rs);
1137 }
1138}
1139
1140static struct tcp_congestion_ops tcp_bbr_cong_ops __read_mostly = {
1141 .flags = TCP_CONG_NON_RESTRICTED,
1142 .name = "bbr",
1143 .owner = THIS_MODULE,
1144 .init = bbr_init,
1145 .cong_control = bbr_main,
1146 .sndbuf_expand = bbr_sndbuf_expand,
1147 .undo_cwnd = bbr_undo_cwnd,
1148 .cwnd_event = bbr_cwnd_event,
1149 .ssthresh = bbr_ssthresh,
1150 .min_tso_segs = bbr_min_tso_segs,
1151 .get_info = bbr_get_info,
1152 .set_state = bbr_set_state,
1153};
1154
1155static int __init bbr_register(void)
1156{
1157 BUILD_BUG_ON(sizeof(struct bbr) > ICSK_CA_PRIV_SIZE);
1158 return tcp_register_congestion_control(&tcp_bbr_cong_ops);
1159}
1160
1161static void __exit bbr_unregister(void)
1162{
1163 tcp_unregister_congestion_control(&tcp_bbr_cong_ops);
1164}
1165
1166module_init(bbr_register);
1167module_exit(bbr_unregister);
1168
1169MODULE_AUTHOR("Van Jacobson <vanj@google.com>");
1170MODULE_AUTHOR("Neal Cardwell <ncardwell@google.com>");
1171MODULE_AUTHOR("Yuchung Cheng <ycheng@google.com>");
1172MODULE_AUTHOR("Soheil Hassas Yeganeh <soheil@google.com>");
1173MODULE_LICENSE("Dual BSD/GPL");
1174MODULE_DESCRIPTION("TCP BBR (Bottleneck Bandwidth and RTT)");
1175