-
Notifications
You must be signed in to change notification settings - Fork 950
Expand file tree
/
Copy pathbbr.c
More file actions
14977 lines (14412 loc) · 449 KB
/
bbr.c
File metadata and controls
14977 lines (14412 loc) · 449 KB
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright (c) 2016-2020 Netflix, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/**
* Author: Randall Stewart <rrs@netflix.com>
* This work is based on the ACM Queue paper
* BBR - Congestion Based Congestion Control
* and also numerous discussions with Neal, Yuchung and Van.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "opt_inet.h"
#include "opt_inet6.h"
#include "opt_ipsec.h"
#include "opt_tcpdebug.h"
#include "opt_ratelimit.h"
#include <sys/param.h>
#include <sys/arb.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/libkern.h>
#ifdef TCP_HHOOK
#include <sys/hhook.h>
#endif
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/proc.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#ifdef STATS
#include <sys/qmath.h>
#include <sys/tree.h>
#include <sys/stats.h> /* Must come after qmath.h and tree.h */
#endif
#include <sys/refcount.h>
#include <sys/queue.h>
#include <sys/eventhandler.h>
#include <sys/smp.h>
#include <sys/kthread.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/tim_filter.h>
#include <sys/time.h>
#include <sys/protosw.h>
#include <vm/uma.h>
#include <sys/kern_prefetch.h>
#include <net/route.h>
#include <net/route/nhop.h>
#include <net/vnet.h>
#define TCPSTATES /* for logging */
#include <netinet/in.h>
#include <netinet/in_kdtrace.h>
#include <netinet/in_pcb.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h> /* required for icmp_var.h */
#include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
#include <netinet/ip_var.h>
#include <netinet/ip6.h>
#include <netinet6/in6_pcb.h>
#include <netinet6/ip6_var.h>
#define TCPOUTFLAGS
#include <netinet/tcp.h>
#include <netinet/tcp_fsm.h>
#include <netinet/tcp_seq.h>
#include <netinet/tcp_timer.h>
#include <netinet/tcp_var.h>
#include <netinet/tcpip.h>
#include <netinet/tcp_hpts.h>
#include <netinet/cc/cc.h>
#include <netinet/tcp_log_buf.h>
#include <netinet/tcp_ratelimit.h>
#include <netinet/tcp_lro.h>
#ifdef TCPDEBUG
#include <netinet/tcp_debug.h>
#endif /* TCPDEBUG */
#ifdef TCP_OFFLOAD
#include <netinet/tcp_offload.h>
#endif
#ifdef INET6
#include <netinet6/tcp6_var.h>
#endif
#include <netinet/tcp_fastopen.h>
#include <netipsec/ipsec_support.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/ethernet.h>
#if defined(IPSEC) || defined(IPSEC_SUPPORT)
#include <netipsec/ipsec.h>
#include <netipsec/ipsec6.h>
#endif /* IPSEC */
#include <netinet/udp.h>
#include <netinet/udp_var.h>
#include <machine/in_cksum.h>
#ifdef MAC
#include <security/mac/mac_framework.h>
#endif
#include "sack_filter.h"
#include "tcp_bbr.h"
#include "rack_bbr_common.h"
uma_zone_t bbr_zone;
uma_zone_t bbr_pcb_zone;
struct sysctl_ctx_list bbr_sysctl_ctx;
struct sysctl_oid *bbr_sysctl_root;
#define TCPT_RANGESET_NOSLOP(tv, value, tvmin, tvmax) do { \
(tv) = (value); \
if ((u_long)(tv) < (u_long)(tvmin)) \
(tv) = (tvmin); \
if ((u_long)(tv) > (u_long)(tvmax)) \
(tv) = (tvmax); \
} while(0)
/*#define BBR_INVARIANT 1*/
#ifdef FSTACK
#define MODNAME tcp_bbr
#define STACKNAME bbr
#endif
/*
* initial window
*/
static uint32_t bbr_def_init_win = 10;
static int32_t bbr_persist_min = 250000; /* 250ms */
static int32_t bbr_persist_max = 1000000; /* 1 Second */
static int32_t bbr_cwnd_may_shrink = 0;
static int32_t bbr_cwndtarget_rtt_touse = BBR_RTT_PROP;
static int32_t bbr_num_pktepo_for_del_limit = BBR_NUM_RTTS_FOR_DEL_LIMIT;
static int32_t bbr_hardware_pacing_limit = 8000;
static int32_t bbr_quanta = 3; /* How much extra quanta do we get? */
static int32_t bbr_no_retran = 0;
static int32_t bbr_error_base_paceout = 10000; /* usec to pace */
static int32_t bbr_max_net_error_cnt = 10;
/* Should the following be dynamic too -- loss wise */
static int32_t bbr_rtt_gain_thresh = 0;
/* Measurement controls */
static int32_t bbr_use_google_algo = 1;
static int32_t bbr_ts_limiting = 1;
static int32_t bbr_ts_can_raise = 0;
static int32_t bbr_do_red = 600;
static int32_t bbr_red_scale = 20000;
static int32_t bbr_red_mul = 1;
static int32_t bbr_red_div = 2;
static int32_t bbr_red_growth_restrict = 1;
static int32_t bbr_target_is_bbunit = 0;
static int32_t bbr_drop_limit = 0;
/*
* How much gain do we need to see to
* stay in startup?
*/
static int32_t bbr_marks_rxt_sack_passed = 0;
static int32_t bbr_start_exit = 25;
static int32_t bbr_low_start_exit = 25; /* When we are in reduced gain */
static int32_t bbr_startup_loss_thresh = 2000; /* 20.00% loss */
static int32_t bbr_hptsi_max_mul = 1; /* These two mul/div assure a min pacing */
static int32_t bbr_hptsi_max_div = 2; /* time, 0 means turned off. We need this
* if we go back ever to where the pacer
* has priority over timers.
*/
static int32_t bbr_policer_call_from_rack_to = 0;
static int32_t bbr_policer_detection_enabled = 1;
static int32_t bbr_min_measurements_req = 1; /* We need at least 2
* measurments before we are
* "good" note that 2 == 1.
* This is because we use a >
* comparison. This means if
* min_measure was 0, it takes
* num-measures > min(0) and
* you get 1 measurement and
* you are good. Set to 1, you
* have to have two
* measurements (this is done
* to prevent it from being ok
* to have no measurements). */
static int32_t bbr_no_pacing_until = 4;
static int32_t bbr_min_usec_delta = 20000; /* 20,000 usecs */
static int32_t bbr_min_peer_delta = 20; /* 20 units */
static int32_t bbr_delta_percent = 150; /* 15.0 % */
static int32_t bbr_target_cwnd_mult_limit = 8;
/*
* bbr_cwnd_min_val is the number of
* segments we hold to in the RTT probe
* state typically 4.
*/
static int32_t bbr_cwnd_min_val = BBR_PROBERTT_NUM_MSS;
static int32_t bbr_cwnd_min_val_hs = BBR_HIGHSPEED_NUM_MSS;
static int32_t bbr_gain_to_target = 1;
static int32_t bbr_gain_gets_extra_too = 1;
/*
* bbr_high_gain is the 2/ln(2) value we need
* to double the sending rate in startup. This
* is used for both cwnd and hptsi gain's.
*/
static int32_t bbr_high_gain = BBR_UNIT * 2885 / 1000 + 1;
static int32_t bbr_startup_lower = BBR_UNIT * 1500 / 1000 + 1;
static int32_t bbr_use_lower_gain_in_startup = 1;
/* thresholds for reduction on drain in sub-states/drain */
static int32_t bbr_drain_rtt = BBR_SRTT;
static int32_t bbr_drain_floor = 88;
static int32_t google_allow_early_out = 1;
static int32_t google_consider_lost = 1;
static int32_t bbr_drain_drop_mul = 4;
static int32_t bbr_drain_drop_div = 5;
static int32_t bbr_rand_ot = 50;
static int32_t bbr_can_force_probertt = 0;
static int32_t bbr_can_adjust_probertt = 1;
static int32_t bbr_probertt_sets_rtt = 0;
static int32_t bbr_can_use_ts_for_rtt = 1;
static int32_t bbr_is_ratio = 0;
static int32_t bbr_sub_drain_app_limit = 1;
static int32_t bbr_prtt_slam_cwnd = 1;
static int32_t bbr_sub_drain_slam_cwnd = 1;
static int32_t bbr_slam_cwnd_in_main_drain = 1;
static int32_t bbr_filter_len_sec = 6; /* How long does the rttProp filter
* hold */
static uint32_t bbr_rtt_probe_limit = (USECS_IN_SECOND * 4);
/*
* bbr_drain_gain is the reverse of the high_gain
* designed to drain back out the standing queue
* that is formed in startup by causing a larger
* hptsi gain and thus drainging the packets
* in flight.
*/
static int32_t bbr_drain_gain = BBR_UNIT * 1000 / 2885;
static int32_t bbr_rttprobe_gain = 192;
/*
* The cwnd_gain is the default cwnd gain applied when
* calculating a target cwnd. Note that the cwnd is
* a secondary factor in the way BBR works (see the
* paper and think about it, it will take some time).
* Basically the hptsi_gain spreads the packets out
* so you never get more than BDP to the peer even
* if the cwnd is high. In our implemenation that
* means in non-recovery/retransmission scenarios
* cwnd will never be reached by the flight-size.
*/
static int32_t bbr_cwnd_gain = BBR_UNIT * 2;
static int32_t bbr_tlp_type_to_use = BBR_SRTT;
static int32_t bbr_delack_time = 100000; /* 100ms in useconds */
static int32_t bbr_sack_not_required = 0; /* set to one to allow non-sack to use bbr */
static int32_t bbr_initial_bw_bps = 62500; /* 500kbps in bytes ps */
static int32_t bbr_ignore_data_after_close = 1;
static int16_t bbr_hptsi_gain[] = {
(BBR_UNIT *5 / 4),
(BBR_UNIT * 3 / 4),
BBR_UNIT,
BBR_UNIT,
BBR_UNIT,
BBR_UNIT,
BBR_UNIT,
BBR_UNIT
};
int32_t bbr_use_rack_resend_cheat = 1;
int32_t bbr_sends_full_iwnd = 1;
#define BBR_HPTSI_GAIN_MAX 8
/*
* The BBR module incorporates a number of
* TCP ideas that have been put out into the IETF
* over the last few years:
* - Yuchung Cheng's RACK TCP (for which its named) that
* will stop us using the number of dup acks and instead
* use time as the gage of when we retransmit.
* - Reorder Detection of RFC4737 and the Tail-Loss probe draft
* of Dukkipati et.al.
* - Van Jacobson's et.al BBR.
*
* RACK depends on SACK, so if an endpoint arrives that
* cannot do SACK the state machine below will shuttle the
* connection back to using the "default" TCP stack that is
* in FreeBSD.
*
* To implement BBR and RACK the original TCP stack was first decomposed
* into a functional state machine with individual states
* for each of the possible TCP connection states. The do_segement
* functions role in life is to mandate the connection supports SACK
* initially and then assure that the RACK state matches the conenction
* state before calling the states do_segment function. Data processing
* of inbound segments also now happens in the hpts_do_segment in general
* with only one exception. This is so we can keep the connection on
* a single CPU.
*
* Each state is simplified due to the fact that the original do_segment
* has been decomposed and we *know* what state we are in (no
* switches on the state) and all tests for SACK are gone. This
* greatly simplifies what each state does.
*
* TCP output is also over-written with a new version since it
* must maintain the new rack scoreboard and has had hptsi
* integrated as a requirment. Still todo is to eliminate the
* use of the callout_() system and use the hpts for all
* timers as well.
*/
static uint32_t bbr_rtt_probe_time = 200000; /* 200ms in micro seconds */
static uint32_t bbr_rtt_probe_cwndtarg = 4; /* How many mss's outstanding */
static const int32_t bbr_min_req_free = 2; /* The min we must have on the
* free list */
static int32_t bbr_tlp_thresh = 1;
static int32_t bbr_reorder_thresh = 2;
static int32_t bbr_reorder_fade = 60000000; /* 0 - never fade, def
* 60,000,000 - 60 seconds */
static int32_t bbr_pkt_delay = 1000;
static int32_t bbr_min_to = 1000; /* Number of usec's minimum timeout */
static int32_t bbr_incr_timers = 1;
static int32_t bbr_tlp_min = 10000; /* 10ms in usecs */
static int32_t bbr_delayed_ack_time = 200000; /* 200ms in usecs */
static int32_t bbr_exit_startup_at_loss = 1;
/*
* bbr_lt_bw_ratio is 1/8th
* bbr_lt_bw_diff is < 4 Kbit/sec
*/
static uint64_t bbr_lt_bw_diff = 4000 / 8; /* In bytes per second */
static uint64_t bbr_lt_bw_ratio = 8; /* For 1/8th */
static uint32_t bbr_lt_bw_max_rtts = 48; /* How many rtt's do we use
* the lt_bw for */
static uint32_t bbr_lt_intvl_min_rtts = 4; /* Min num of RTT's to measure
* lt_bw */
static int32_t bbr_lt_intvl_fp = 0; /* False positive epoch diff */
static int32_t bbr_lt_loss_thresh = 196; /* Lost vs delivered % */
static int32_t bbr_lt_fd_thresh = 100; /* false detection % */
static int32_t bbr_verbose_logging = 0;
/*
* Currently regular tcp has a rto_min of 30ms
* the backoff goes 12 times so that ends up
* being a total of 122.850 seconds before a
* connection is killed.
*/
static int32_t bbr_rto_min_ms = 30; /* 30ms same as main freebsd */
static int32_t bbr_rto_max_sec = 4; /* 4 seconds */
/****************************************************/
/* DEFAULT TSO SIZING (cpu performance impacting) */
/****************************************************/
/* What amount is our formula using to get TSO size */
static int32_t bbr_hptsi_per_second = 1000;
/*
* For hptsi under bbr_cross_over connections what is delay
* target 7ms (in usec) combined with a seg_max of 2
* gets us close to identical google behavior in
* TSO size selection (possibly more 1MSS sends).
*/
static int32_t bbr_hptsi_segments_delay_tar = 7000;
/* Does pacing delay include overhead's in its time calculations? */
static int32_t bbr_include_enet_oh = 0;
static int32_t bbr_include_ip_oh = 1;
static int32_t bbr_include_tcp_oh = 1;
static int32_t bbr_google_discount = 10;
/* Do we use (nf mode) pkt-epoch to drive us or rttProp? */
static int32_t bbr_state_is_pkt_epoch = 0;
static int32_t bbr_state_drain_2_tar = 1;
/* What is the max the 0 - bbr_cross_over MBPS TSO target
* can reach using our delay target. Note that this
* value becomes the floor for the cross over
* algorithm.
*/
static int32_t bbr_hptsi_segments_max = 2;
static int32_t bbr_hptsi_segments_floor = 1;
static int32_t bbr_hptsi_utter_max = 0;
/* What is the min the 0 - bbr_cross-over MBPS TSO target can be */
static int32_t bbr_hptsi_bytes_min = 1460;
static int32_t bbr_all_get_min = 0;
/* Cross over point from algo-a to algo-b */
static uint32_t bbr_cross_over = TWENTY_THREE_MBPS;
/* Do we deal with our restart state? */
static int32_t bbr_uses_idle_restart = 0;
static int32_t bbr_idle_restart_threshold = 100000; /* 100ms in useconds */
/* Do we allow hardware pacing? */
static int32_t bbr_allow_hdwr_pacing = 0;
static int32_t bbr_hdwr_pace_adjust = 2; /* multipler when we calc the tso size */
static int32_t bbr_hdwr_pace_floor = 1;
static int32_t bbr_hdwr_pacing_delay_cnt = 10;
/****************************************************/
static int32_t bbr_resends_use_tso = 0;
static int32_t bbr_tlp_max_resend = 2;
static int32_t bbr_sack_block_limit = 128;
#define BBR_MAX_STAT 19
counter_u64_t bbr_state_time[BBR_MAX_STAT];
counter_u64_t bbr_state_lost[BBR_MAX_STAT];
counter_u64_t bbr_state_resend[BBR_MAX_STAT];
counter_u64_t bbr_stat_arry[BBR_STAT_SIZE];
counter_u64_t bbr_opts_arry[BBR_OPTS_SIZE];
counter_u64_t bbr_out_size[TCP_MSS_ACCT_SIZE];
counter_u64_t bbr_flows_whdwr_pacing;
counter_u64_t bbr_flows_nohdwr_pacing;
counter_u64_t bbr_nohdwr_pacing_enobuf;
counter_u64_t bbr_hdwr_pacing_enobuf;
static inline uint64_t bbr_get_bw(struct tcp_bbr *bbr);
/*
* Static defintions we need for forward declarations.
*/
static uint32_t
bbr_get_pacing_length(struct tcp_bbr *bbr, uint16_t gain,
uint32_t useconds_time, uint64_t bw);
static uint32_t
bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain);
static void
bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win);
static void
bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses);
static void
bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int line,
int dolog);
static uint32_t
bbr_get_target_cwnd(struct tcp_bbr *bbr, uint64_t bw, uint32_t gain);
static void
bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch,
int32_t pkt_epoch, uint32_t losses);
static uint32_t
bbr_calc_thresh_rack(struct tcp_bbr *bbr, uint32_t srtt, uint32_t cts, struct bbr_sendmap *rsm);
static uint32_t bbr_initial_cwnd(struct tcp_bbr *bbr, struct tcpcb *tp);
static uint32_t
bbr_calc_thresh_tlp(struct tcpcb *tp, struct tcp_bbr *bbr,
struct bbr_sendmap *rsm, uint32_t srtt,
uint32_t cts);
static void
bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts,
int32_t line);
static void
bbr_set_state_target(struct tcp_bbr *bbr, int line);
static void
bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line);
static void
bbr_log_progress_event(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t tick, int event, int line);
static void
tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts);
static void
bbr_setup_red_bw(struct tcp_bbr *bbr, uint32_t cts);
static void
bbr_log_rtt_shrinks(struct tcp_bbr *bbr, uint32_t cts, uint32_t applied, uint32_t rtt,
uint32_t line, uint8_t is_start, uint16_t set);
static struct bbr_sendmap *
bbr_find_lowest_rsm(struct tcp_bbr *bbr);
static __inline uint32_t
bbr_get_rtt(struct tcp_bbr *bbr, int32_t rtt_type);
static void
bbr_log_to_start(struct tcp_bbr *bbr, uint32_t cts, uint32_t to, int32_t slot, uint8_t which);
static void
bbr_log_timer_var(struct tcp_bbr *bbr, int mode, uint32_t cts, uint32_t time_since_sent, uint32_t srtt,
uint32_t thresh, uint32_t to);
static void
bbr_log_hpts_diag(struct tcp_bbr *bbr, uint32_t cts, struct hpts_diag *diag);
static void
bbr_log_type_bbrsnd(struct tcp_bbr *bbr, uint32_t len, uint32_t slot,
uint32_t del_by, uint32_t cts, uint32_t sloton, uint32_t prev_delay);
static void
bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr,
uint32_t cts, int32_t line);
static void
bbr_stop_all_timers(struct tcpcb *tp);
static void
bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts);
static void
bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts);
static void
bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts);
static void
bbr_log_pacing_delay_calc(struct tcp_bbr *bbr, uint16_t gain, uint32_t len,
uint32_t cts, uint32_t usecs, uint64_t bw, uint32_t override, int mod);
static inline uint8_t
bbr_state_val(struct tcp_bbr *bbr)
{
return(bbr->rc_bbr_substate);
}
static inline uint32_t
get_min_cwnd(struct tcp_bbr *bbr)
{
int mss;
mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs);
if (bbr_get_rtt(bbr, BBR_RTT_PROP) < BBR_HIGH_SPEED)
return (bbr_cwnd_min_val_hs * mss);
else
return (bbr_cwnd_min_val * mss);
}
static uint32_t
bbr_get_persists_timer_val(struct tcpcb *tp, struct tcp_bbr *bbr)
{
uint64_t srtt, var;
uint64_t ret_val;
bbr->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT;
if (tp->t_srtt == 0) {
srtt = (uint64_t)BBR_INITIAL_RTO;
var = 0;
} else {
srtt = ((uint64_t)TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT);
var = ((uint64_t)TICKS_2_USEC(tp->t_rttvar) >> TCP_RTT_SHIFT);
}
TCPT_RANGESET_NOSLOP(ret_val, ((srtt + var) * tcp_backoff[tp->t_rxtshift]),
bbr_persist_min, bbr_persist_max);
return ((uint32_t)ret_val);
}
static uint32_t
bbr_timer_start(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
{
/*
* Start the FR timer, we do this based on getting the first one in
* the rc_tmap. Note that if its NULL we must stop the timer. in all
* events we need to stop the running timer (if its running) before
* starting the new one.
*/
uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse;
int32_t idx;
int32_t is_tlp_timer = 0;
struct bbr_sendmap *rsm;
if (bbr->rc_all_timers_stopped) {
/* All timers have been stopped none are to run */
return (0);
}
if (bbr->rc_in_persist) {
/* We can't start any timer in persists */
return (bbr_get_persists_timer_val(tp, bbr));
}
rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
if ((rsm == NULL) ||
((tp->t_flags & TF_SACK_PERMIT) == 0) ||
(tp->t_state < TCPS_ESTABLISHED)) {
/* Nothing on the send map */
activate_rxt:
if (SEQ_LT(tp->snd_una, tp->snd_max) || sbavail(&(tp->t_inpcb->inp_socket->so_snd))) {
uint64_t tov;
time_since_sent = 0;
rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
if (rsm) {
idx = rsm->r_rtr_cnt - 1;
if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], bbr->r_ctl.rc_tlp_rxt_last_time))
tstmp_touse = rsm->r_tim_lastsent[idx];
else
tstmp_touse = bbr->r_ctl.rc_tlp_rxt_last_time;
if (TSTMP_GT(tstmp_touse, cts))
time_since_sent = cts - tstmp_touse;
}
bbr->r_ctl.rc_hpts_flags |= PACE_TMR_RXT;
if (tp->t_srtt == 0)
tov = BBR_INITIAL_RTO;
else
tov = ((uint64_t)(TICKS_2_USEC(tp->t_srtt) +
((uint64_t)TICKS_2_USEC(tp->t_rttvar) * (uint64_t)4)) >> TCP_RTT_SHIFT);
if (tp->t_rxtshift)
tov *= tcp_backoff[tp->t_rxtshift];
if (tov > time_since_sent)
tov -= time_since_sent;
else
tov = bbr->r_ctl.rc_min_to;
TCPT_RANGESET_NOSLOP(to, tov,
(bbr->r_ctl.rc_min_rto_ms * MS_IN_USEC),
(bbr->rc_max_rto_sec * USECS_IN_SECOND));
bbr_log_timer_var(bbr, 2, cts, 0, srtt, 0, to);
return (to);
}
return (0);
}
if (rsm->r_flags & BBR_ACKED) {
rsm = bbr_find_lowest_rsm(bbr);
if (rsm == NULL) {
/* No lowest? */
goto activate_rxt;
}
}
/* Convert from ms to usecs */
if (rsm->r_flags & BBR_SACK_PASSED) {
if ((tp->t_flags & TF_SENTFIN) &&
((tp->snd_max - tp->snd_una) == 1) &&
(rsm->r_flags & BBR_HAS_FIN)) {
/*
* We don't start a bbr rack timer if all we have is
* a FIN outstanding.
*/
goto activate_rxt;
}
srtt = bbr_get_rtt(bbr, BBR_RTT_RACK);
thresh = bbr_calc_thresh_rack(bbr, srtt, cts, rsm);
idx = rsm->r_rtr_cnt - 1;
exp = rsm->r_tim_lastsent[idx] + thresh;
if (SEQ_GEQ(exp, cts)) {
to = exp - cts;
if (to < bbr->r_ctl.rc_min_to) {
to = bbr->r_ctl.rc_min_to;
}
} else {
to = bbr->r_ctl.rc_min_to;
}
} else {
/* Ok we need to do a TLP not RACK */
if (bbr->rc_tlp_in_progress != 0) {
/*
* The previous send was a TLP.
*/
goto activate_rxt;
}
rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_tmap, bbr_sendmap, r_tnext);
if (rsm == NULL) {
/* We found no rsm to TLP with. */
goto activate_rxt;
}
if (rsm->r_flags & BBR_HAS_FIN) {
/* If its a FIN we don't do TLP */
rsm = NULL;
goto activate_rxt;
}
time_since_sent = 0;
idx = rsm->r_rtr_cnt - 1;
if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], bbr->r_ctl.rc_tlp_rxt_last_time))
tstmp_touse = rsm->r_tim_lastsent[idx];
else
tstmp_touse = bbr->r_ctl.rc_tlp_rxt_last_time;
if (TSTMP_GT(tstmp_touse, cts))
time_since_sent = cts - tstmp_touse;
is_tlp_timer = 1;
srtt = bbr_get_rtt(bbr, bbr_tlp_type_to_use);
thresh = bbr_calc_thresh_tlp(tp, bbr, rsm, srtt, cts);
if (thresh > time_since_sent)
to = thresh - time_since_sent;
else
to = bbr->r_ctl.rc_min_to;
if (to > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) {
/*
* If the TLP time works out to larger than the max
* RTO lets not do TLP.. just RTO.
*/
goto activate_rxt;
}
if ((bbr->rc_tlp_rtx_out == 1) &&
(rsm->r_start == bbr->r_ctl.rc_last_tlp_seq)) {
/*
* Second retransmit of the same TLP
* lets not.
*/
bbr->rc_tlp_rtx_out = 0;
goto activate_rxt;
}
if (rsm->r_start != bbr->r_ctl.rc_last_tlp_seq) {
/*
* The tail is no longer the last one I did a probe
* on
*/
bbr->r_ctl.rc_tlp_seg_send_cnt = 0;
bbr->r_ctl.rc_last_tlp_seq = rsm->r_start;
}
}
if (is_tlp_timer == 0) {
BBR_STAT_INC(bbr_to_arm_rack);
bbr->r_ctl.rc_hpts_flags |= PACE_TMR_RACK;
} else {
bbr_log_timer_var(bbr, 1, cts, time_since_sent, srtt, thresh, to);
if (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend) {
/*
* We have exceeded how many times we can retran the
* current TLP timer, switch to the RTO timer.
*/
goto activate_rxt;
} else {
BBR_STAT_INC(bbr_to_arm_tlp);
bbr->r_ctl.rc_hpts_flags |= PACE_TMR_TLP;
}
}
return (to);
}
static inline int32_t
bbr_minseg(struct tcp_bbr *bbr)
{
return (bbr->r_ctl.rc_pace_min_segs - bbr->rc_last_options);
}
static void
bbr_start_hpts_timer(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t cts, int32_t frm, int32_t slot, uint32_t tot_len)
{
struct inpcb *inp;
struct hpts_diag diag;
uint32_t delayed_ack = 0;
uint32_t left = 0;
uint32_t hpts_timeout;
uint8_t stopped;
int32_t delay_calc = 0;
uint32_t prev_delay = 0;
inp = tp->t_inpcb;
if (inp->inp_in_hpts) {
/* A previous call is already set up */
return;
}
if ((tp->t_state == TCPS_CLOSED) ||
(tp->t_state == TCPS_LISTEN)) {
return;
}
stopped = bbr->rc_tmr_stopped;
if (stopped && TSTMP_GT(bbr->r_ctl.rc_timer_exp, cts)) {
left = bbr->r_ctl.rc_timer_exp - cts;
}
bbr->r_ctl.rc_hpts_flags = 0;
bbr->r_ctl.rc_timer_exp = 0;
prev_delay = bbr->r_ctl.rc_last_delay_val;
if (bbr->r_ctl.rc_last_delay_val &&
(slot == 0)) {
/*
* If a previous pacer delay was in place we
* are not coming from the output side (where
* we calculate a delay, more likely a timer).
*/
slot = bbr->r_ctl.rc_last_delay_val;
if (TSTMP_GT(cts, bbr->rc_pacer_started)) {
/* Compensate for time passed */
delay_calc = cts - bbr->rc_pacer_started;
if (delay_calc <= slot)
slot -= delay_calc;
}
}
/* Do we have early to make up for by pushing out the pacing time? */
if (bbr->r_agg_early_set) {
bbr_log_pacing_delay_calc(bbr, 0, bbr->r_ctl.rc_agg_early, cts, slot, 0, bbr->r_agg_early_set, 2);
slot += bbr->r_ctl.rc_agg_early;
bbr->r_ctl.rc_agg_early = 0;
bbr->r_agg_early_set = 0;
}
/* Are we running a total debt that needs to be compensated for? */
if (bbr->r_ctl.rc_hptsi_agg_delay) {
if (slot > bbr->r_ctl.rc_hptsi_agg_delay) {
/* We nuke the delay */
slot -= bbr->r_ctl.rc_hptsi_agg_delay;
bbr->r_ctl.rc_hptsi_agg_delay = 0;
} else {
/* We nuke some of the delay, put in a minimal 100usecs */
bbr->r_ctl.rc_hptsi_agg_delay -= slot;
bbr->r_ctl.rc_last_delay_val = slot = 100;
}
}
bbr->r_ctl.rc_last_delay_val = slot;
hpts_timeout = bbr_timer_start(tp, bbr, cts);
if (tp->t_flags & TF_DELACK) {
if (bbr->rc_in_persist == 0) {
delayed_ack = bbr_delack_time;
} else {
/*
* We are in persists and have
* gotten a new data element.
*/
if (hpts_timeout > bbr_delack_time) {
/*
* Lets make the persists timer (which acks)
* be the smaller of hpts_timeout and bbr_delack_time.
*/
hpts_timeout = bbr_delack_time;
}
}
}
if (delayed_ack &&
((hpts_timeout == 0) ||
(delayed_ack < hpts_timeout))) {
/* We need a Delayed ack timer */
bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK;
hpts_timeout = delayed_ack;
}
if (slot) {
/* Mark that we have a pacing timer up */
BBR_STAT_INC(bbr_paced_segments);
bbr->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT;
}
/*
* If no timers are going to run and we will fall off thfe hptsi
* wheel, we resort to a keep-alive timer if its configured.
*/
if ((hpts_timeout == 0) &&
(slot == 0)) {
if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
(tp->t_state <= TCPS_CLOSING)) {
/*
* Ok we have no timer (persists, rack, tlp, rxt or
* del-ack), we don't have segments being paced. So
* all that is left is the keepalive timer.
*/
if (TCPS_HAVEESTABLISHED(tp->t_state)) {
hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp));
} else {
hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp));
}
bbr->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP;
}
}
if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) ==
(bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) {
/*
* RACK, TLP, persists and RXT timers all are restartable
* based on actions input .. i.e we received a packet (ack
* or sack) and that changes things (rw, or snd_una etc).
* Thus we can restart them with a new value. For
* keep-alive, delayed_ack we keep track of what was left
* and restart the timer with a smaller value.
*/
if (left < hpts_timeout)
hpts_timeout = left;
}
if (bbr->r_ctl.rc_incr_tmrs && slot &&
(bbr->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) {
/*
* If configured to do so, and the timer is either
* the TLP or RXT timer, we need to increase the timeout
* by the pacing time. Consider the bottleneck at my
* machine as an example, we are sending something
* to start a TLP on. The last packet won't be emitted
* fully until the pacing time (the bottleneck will hold
* the data in place). Once the packet is emitted that
* is when we want to start waiting for the TLP. This
* is most evident with hardware pacing (where the nic
* is holding the packet(s) before emitting). But it
* can also show up in the network so we do it for all
* cases. Technically we would take off one packet from
* this extra delay but this is easier and being more
* conservative is probably better.
*/
hpts_timeout += slot;
}
if (hpts_timeout) {
/*
* Hack alert for now we can't time-out over 2147 seconds (a
* bit more than 35min)
*/
if (hpts_timeout > 0x7ffffffe)
hpts_timeout = 0x7ffffffe;
bbr->r_ctl.rc_timer_exp = cts + hpts_timeout;
} else
bbr->r_ctl.rc_timer_exp = 0;
if ((slot) &&
(bbr->rc_use_google ||
bbr->output_error_seen ||
(slot <= hpts_timeout)) ) {
/*
* Tell LRO that it can queue packets while
* we pace.
*/
bbr->rc_inp->inp_flags2 |= INP_MBUF_QUEUE_READY;
if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) &&
(bbr->rc_cwnd_limited == 0)) {
/*
* If we are not cwnd limited and we
* are running a rack timer we put on
* the do not disturbe even for sack.
*/
inp->inp_flags2 |= INP_DONT_SACK_QUEUE;
} else
inp->inp_flags2 &= ~INP_DONT_SACK_QUEUE;
bbr->rc_pacer_started = cts;
(void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(slot),
__LINE__, &diag);
bbr->rc_timer_first = 0;
bbr->bbr_timer_src = frm;
bbr_log_to_start(bbr, cts, hpts_timeout, slot, 1);
bbr_log_hpts_diag(bbr, cts, &diag);
} else if (hpts_timeout) {
(void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(hpts_timeout),
__LINE__, &diag);
/*
* We add the flag here as well if the slot is set,
* since hpts will call in to clear the queue first before
* calling the output routine (which does our timers).
* We don't want to set the flag if its just a timer
* else the arrival of data might (that causes us
* to send more) might get delayed. Imagine being
* on a keep-alive timer and a request comes in for
* more data.
*/
if (slot)
bbr->rc_pacer_started = cts;
if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) &&
(bbr->rc_cwnd_limited == 0)) {
/*
* For a rack timer, don't wake us even
* if a sack arrives as long as we are
* not cwnd limited.
*/
bbr->rc_inp->inp_flags2 |= INP_MBUF_QUEUE_READY;
inp->inp_flags2 |= INP_DONT_SACK_QUEUE;
} else {
/* All other timers wake us up */
bbr->rc_inp->inp_flags2 &= ~INP_MBUF_QUEUE_READY;
inp->inp_flags2 &= ~INP_DONT_SACK_QUEUE;
}
bbr->bbr_timer_src = frm;
bbr_log_to_start(bbr, cts, hpts_timeout, slot, 0);
bbr_log_hpts_diag(bbr, cts, &diag);
bbr->rc_timer_first = 1;
}
bbr->rc_tmr_stopped = 0;
bbr_log_type_bbrsnd(bbr, tot_len, slot, delay_calc, cts, frm, prev_delay);
}
static void
bbr_timer_audit(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, struct sockbuf *sb)
{
/*
* We received an ack, and then did not call send or were bounced
* out due to the hpts was running. Now a timer is up as well, is it
* the right timer?
*/
struct inpcb *inp;
struct bbr_sendmap *rsm;
uint32_t hpts_timeout;
int tmr_up;
tmr_up = bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
if (bbr->rc_in_persist && (tmr_up == PACE_TMR_PERSIT))
return;
rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) &&
(tmr_up == PACE_TMR_RXT)) {
/* Should be an RXT */
return;
}
inp = bbr->rc_inp;
if (rsm == NULL) {
/* Nothing outstanding? */
if (tp->t_flags & TF_DELACK) {
if (tmr_up == PACE_TMR_DELACK)
/*
* We are supposed to have delayed ack up
* and we do
*/
return;
} else if (sbavail(&inp->inp_socket->so_snd) &&
(tmr_up == PACE_TMR_RXT)) {
/*
* if we hit enobufs then we would expect the
* possiblity of nothing outstanding and the RXT up
* (and the hptsi timer).
*/
return;
} else if (((V_tcp_always_keepalive ||