6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Author: Randall Stewart <rrs@netflix.com>
30 * This work is based on the ACM Queue paper
31 * BBR - Congestion Based Congestion Control
32 * and also numerous discussions with Neal, Yuchung and Van.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 #include "opt_inet6.h"
40 #include "opt_ipsec.h"
41 #include "opt_tcpdebug.h"
42 #include "opt_ratelimit.h"
43 #include "opt_kern_tls.h"
44 #include <sys/param.h>
46 #include <sys/module.h>
47 #include <sys/kernel.h>
49 #include <sys/hhook.h>
51 #include <sys/malloc.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
59 #include <sys/sysctl.h>
60 #include <sys/systm.h>
62 #include <sys/qmath.h>
64 #include <sys/stats.h> /* Must come after qmath.h and tree.h */
66 #include <sys/refcount.h>
67 #include <sys/queue.h>
68 #include <sys/eventhandler.h>
70 #include <sys/kthread.h>
72 #include <sys/mutex.h>
73 #include <sys/tim_filter.h>
76 #include <sys/kern_prefetch.h>
78 #include <net/route.h>
81 #define TCPSTATES /* for logging */
83 #include <netinet/in.h>
84 #include <netinet/in_kdtrace.h>
85 #include <netinet/in_pcb.h>
86 #include <netinet/ip.h>
87 #include <netinet/ip_icmp.h> /* required for icmp_var.h */
88 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
89 #include <netinet/ip_var.h>
90 #include <netinet/ip6.h>
91 #include <netinet6/in6_pcb.h>
92 #include <netinet6/ip6_var.h>
94 #include <netinet/tcp.h>
95 #include <netinet/tcp_fsm.h>
96 #include <netinet/tcp_seq.h>
97 #include <netinet/tcp_timer.h>
98 #include <netinet/tcp_var.h>
99 #include <netinet/tcpip.h>
100 #include <netinet/tcp_hpts.h>
101 #include <netinet/cc/cc.h>
102 #include <netinet/tcp_log_buf.h>
103 #include <netinet/tcp_ratelimit.h>
104 #include <netinet/tcp_lro.h>
106 #include <netinet/tcp_debug.h>
107 #endif /* TCPDEBUG */
109 #include <netinet/tcp_offload.h>
112 #include <netinet6/tcp6_var.h>
114 #include <netinet/tcp_fastopen.h>
116 #include <netipsec/ipsec_support.h>
118 #include <net/if_var.h>
119 #include <net/ethernet.h>
121 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
122 #include <netipsec/ipsec.h>
123 #include <netipsec/ipsec6.h>
126 #include <netinet/udp.h>
127 #include <netinet/udp_var.h>
128 #include <machine/in_cksum.h>
131 #include <security/mac/mac_framework.h>
134 #include "sack_filter.h"
136 #include "rack_bbr_common.h"
138 uma_zone_t bbr_pcb_zone;
140 struct sysctl_ctx_list bbr_sysctl_ctx;
141 struct sysctl_oid *bbr_sysctl_root;
143 #define TCPT_RANGESET_NOSLOP(tv, value, tvmin, tvmax) do { \
145 if ((u_long)(tv) < (u_long)(tvmin)) \
147 if ((u_long)(tv) > (u_long)(tvmax)) \
151 /*#define BBR_INVARIANT 1*/
156 static uint32_t bbr_def_init_win = 10;
157 static int32_t bbr_persist_min = 250000; /* 250ms */
158 static int32_t bbr_persist_max = 1000000; /* 1 Second */
159 static int32_t bbr_cwnd_may_shrink = 0;
160 static int32_t bbr_cwndtarget_rtt_touse = BBR_RTT_PROP;
161 static int32_t bbr_num_pktepo_for_del_limit = BBR_NUM_RTTS_FOR_DEL_LIMIT;
162 static int32_t bbr_hardware_pacing_limit = 8000;
163 static int32_t bbr_quanta = 3; /* How much extra quanta do we get? */
164 static int32_t bbr_no_retran = 0;
167 static int32_t bbr_error_base_paceout = 10000; /* usec to pace */
168 static int32_t bbr_max_net_error_cnt = 10;
169 /* Should the following be dynamic too -- loss wise */
170 static int32_t bbr_rtt_gain_thresh = 0;
171 /* Measurement controls */
172 static int32_t bbr_use_google_algo = 1;
173 static int32_t bbr_ts_limiting = 1;
174 static int32_t bbr_ts_can_raise = 0;
175 static int32_t bbr_do_red = 600;
176 static int32_t bbr_red_scale = 20000;
177 static int32_t bbr_red_mul = 1;
178 static int32_t bbr_red_div = 2;
179 static int32_t bbr_red_growth_restrict = 1;
180 static int32_t bbr_target_is_bbunit = 0;
181 static int32_t bbr_drop_limit = 0;
183 * How much gain do we need to see to
186 static int32_t bbr_marks_rxt_sack_passed = 0;
187 static int32_t bbr_start_exit = 25;
188 static int32_t bbr_low_start_exit = 25; /* When we are in reduced gain */
189 static int32_t bbr_startup_loss_thresh = 2000; /* 20.00% loss */
190 static int32_t bbr_hptsi_max_mul = 1; /* These two mul/div assure a min pacing */
191 static int32_t bbr_hptsi_max_div = 2; /* time, 0 means turned off. We need this
192 * if we go back ever to where the pacer
193 * has priority over timers.
195 static int32_t bbr_policer_call_from_rack_to = 0;
196 static int32_t bbr_policer_detection_enabled = 1;
197 static int32_t bbr_min_measurements_req = 1; /* We need at least 2
198 * measurments before we are
199 * "good" note that 2 == 1.
200 * This is because we use a >
201 * comparison. This means if
202 * min_measure was 0, it takes
203 * num-measures > min(0) and
204 * you get 1 measurement and
205 * you are good. Set to 1, you
207 * measurements (this is done
208 * to prevent it from being ok
209 * to have no measurements). */
210 static int32_t bbr_no_pacing_until = 4;
212 static int32_t bbr_min_usec_delta = 20000; /* 20,000 usecs */
213 static int32_t bbr_min_peer_delta = 20; /* 20 units */
214 static int32_t bbr_delta_percent = 150; /* 15.0 % */
216 static int32_t bbr_target_cwnd_mult_limit = 8;
218 * bbr_cwnd_min_val is the number of
219 * segments we hold to in the RTT probe
222 static int32_t bbr_cwnd_min_val = BBR_PROBERTT_NUM_MSS;
225 static int32_t bbr_cwnd_min_val_hs = BBR_HIGHSPEED_NUM_MSS;
227 static int32_t bbr_gain_to_target = 1;
228 static int32_t bbr_gain_gets_extra_too = 1;
230 * bbr_high_gain is the 2/ln(2) value we need
231 * to double the sending rate in startup. This
232 * is used for both cwnd and hptsi gain's.
234 static int32_t bbr_high_gain = BBR_UNIT * 2885 / 1000 + 1;
235 static int32_t bbr_startup_lower = BBR_UNIT * 1500 / 1000 + 1;
236 static int32_t bbr_use_lower_gain_in_startup = 1;
238 /* thresholds for reduction on drain in sub-states/drain */
239 static int32_t bbr_drain_rtt = BBR_SRTT;
240 static int32_t bbr_drain_floor = 88;
241 static int32_t google_allow_early_out = 1;
242 static int32_t google_consider_lost = 1;
243 static int32_t bbr_drain_drop_mul = 4;
244 static int32_t bbr_drain_drop_div = 5;
245 static int32_t bbr_rand_ot = 50;
246 static int32_t bbr_can_force_probertt = 0;
247 static int32_t bbr_can_adjust_probertt = 1;
248 static int32_t bbr_probertt_sets_rtt = 0;
249 static int32_t bbr_can_use_ts_for_rtt = 1;
250 static int32_t bbr_is_ratio = 0;
251 static int32_t bbr_sub_drain_app_limit = 1;
252 static int32_t bbr_prtt_slam_cwnd = 1;
253 static int32_t bbr_sub_drain_slam_cwnd = 1;
254 static int32_t bbr_slam_cwnd_in_main_drain = 1;
255 static int32_t bbr_filter_len_sec = 6; /* How long does the rttProp filter
257 static uint32_t bbr_rtt_probe_limit = (USECS_IN_SECOND * 4);
259 * bbr_drain_gain is the reverse of the high_gain
260 * designed to drain back out the standing queue
261 * that is formed in startup by causing a larger
262 * hptsi gain and thus drainging the packets
265 static int32_t bbr_drain_gain = BBR_UNIT * 1000 / 2885;
266 static int32_t bbr_rttprobe_gain = 192;
269 * The cwnd_gain is the default cwnd gain applied when
270 * calculating a target cwnd. Note that the cwnd is
271 * a secondary factor in the way BBR works (see the
272 * paper and think about it, it will take some time).
273 * Basically the hptsi_gain spreads the packets out
274 * so you never get more than BDP to the peer even
275 * if the cwnd is high. In our implemenation that
276 * means in non-recovery/retransmission scenarios
277 * cwnd will never be reached by the flight-size.
279 static int32_t bbr_cwnd_gain = BBR_UNIT * 2;
280 static int32_t bbr_tlp_type_to_use = BBR_SRTT;
281 static int32_t bbr_delack_time = 100000; /* 100ms in useconds */
282 static int32_t bbr_sack_not_required = 0; /* set to one to allow non-sack to use bbr */
283 static int32_t bbr_initial_bw_bps = 62500; /* 500kbps in bytes ps */
284 static int32_t bbr_ignore_data_after_close = 1;
285 static int16_t bbr_hptsi_gain[] = {
295 int32_t bbr_use_rack_resend_cheat = 1;
296 int32_t bbr_sends_full_iwnd = 1;
298 #define BBR_HPTSI_GAIN_MAX 8
300 * The BBR module incorporates a number of
301 * TCP ideas that have been put out into the IETF
302 * over the last few years:
303 * - Yuchung Cheng's RACK TCP (for which its named) that
304 * will stop us using the number of dup acks and instead
305 * use time as the gage of when we retransmit.
306 * - Reorder Detection of RFC4737 and the Tail-Loss probe draft
307 * of Dukkipati et.al.
308 * - Van Jacobson's et.al BBR.
310 * RACK depends on SACK, so if an endpoint arrives that
311 * cannot do SACK the state machine below will shuttle the
312 * connection back to using the "default" TCP stack that is
315 * To implement BBR and RACK the original TCP stack was first decomposed
316 * into a functional state machine with individual states
317 * for each of the possible TCP connection states. The do_segement
318 * functions role in life is to mandate the connection supports SACK
319 * initially and then assure that the RACK state matches the conenction
320 * state before calling the states do_segment function. Data processing
321 * of inbound segments also now happens in the hpts_do_segment in general
322 * with only one exception. This is so we can keep the connection on
325 * Each state is simplified due to the fact that the original do_segment
326 * has been decomposed and we *know* what state we are in (no
327 * switches on the state) and all tests for SACK are gone. This
328 * greatly simplifies what each state does.
330 * TCP output is also over-written with a new version since it
331 * must maintain the new rack scoreboard and has had hptsi
332 * integrated as a requirment. Still todo is to eliminate the
333 * use of the callout_() system and use the hpts for all
336 static uint32_t bbr_rtt_probe_time = 200000; /* 200ms in micro seconds */
337 static uint32_t bbr_rtt_probe_cwndtarg = 4; /* How many mss's outstanding */
338 static const int32_t bbr_min_req_free = 2; /* The min we must have on the
340 static int32_t bbr_tlp_thresh = 1;
341 static int32_t bbr_reorder_thresh = 2;
342 static int32_t bbr_reorder_fade = 60000000; /* 0 - never fade, def
343 * 60,000,000 - 60 seconds */
344 static int32_t bbr_pkt_delay = 1000;
345 static int32_t bbr_min_to = 1000; /* Number of usec's minimum timeout */
346 static int32_t bbr_incr_timers = 1;
348 static int32_t bbr_tlp_min = 10000; /* 10ms in usecs */
349 static int32_t bbr_delayed_ack_time = 200000; /* 200ms in usecs */
350 static int32_t bbr_exit_startup_at_loss = 1;
353 * bbr_lt_bw_ratio is 1/8th
354 * bbr_lt_bw_diff is < 4 Kbit/sec
356 static uint64_t bbr_lt_bw_diff = 4000 / 8; /* In bytes per second */
357 static uint64_t bbr_lt_bw_ratio = 8; /* For 1/8th */
358 static uint32_t bbr_lt_bw_max_rtts = 48; /* How many rtt's do we use
360 static uint32_t bbr_lt_intvl_min_rtts = 4; /* Min num of RTT's to measure
362 static int32_t bbr_lt_intvl_fp = 0; /* False positive epoch diff */
363 static int32_t bbr_lt_loss_thresh = 196; /* Lost vs delivered % */
364 static int32_t bbr_lt_fd_thresh = 100; /* false detection % */
366 static int32_t bbr_verbose_logging = 0;
368 * Currently regular tcp has a rto_min of 30ms
369 * the backoff goes 12 times so that ends up
370 * being a total of 122.850 seconds before a
371 * connection is killed.
373 static int32_t bbr_rto_min_ms = 30; /* 30ms same as main freebsd */
374 static int32_t bbr_rto_max_sec = 4; /* 4 seconds */
376 /****************************************************/
377 /* DEFAULT TSO SIZING (cpu performance impacting) */
378 /****************************************************/
379 /* What amount is our formula using to get TSO size */
380 static int32_t bbr_hptsi_per_second = 1000;
383 * For hptsi under bbr_cross_over connections what is delay
384 * target 7ms (in usec) combined with a seg_max of 2
385 * gets us close to identical google behavior in
386 * TSO size selection (possibly more 1MSS sends).
388 static int32_t bbr_hptsi_segments_delay_tar = 7000;
390 /* Does pacing delay include overhead's in its time calculations? */
391 static int32_t bbr_include_enet_oh = 0;
392 static int32_t bbr_include_ip_oh = 1;
393 static int32_t bbr_include_tcp_oh = 1;
394 static int32_t bbr_google_discount = 10;
396 /* Do we use (nf mode) pkt-epoch to drive us or rttProp? */
397 static int32_t bbr_state_is_pkt_epoch = 0;
398 static int32_t bbr_state_drain_2_tar = 1;
399 /* What is the max the 0 - bbr_cross_over MBPS TSO target
400 * can reach using our delay target. Note that this
401 * value becomes the floor for the cross over
404 static int32_t bbr_hptsi_segments_max = 2;
405 static int32_t bbr_hptsi_segments_floor = 1;
406 static int32_t bbr_hptsi_utter_max = 0;
408 /* What is the min the 0 - bbr_cross-over MBPS TSO target can be */
409 static int32_t bbr_hptsi_bytes_min = 1460;
410 static int32_t bbr_all_get_min = 0;
412 /* Cross over point from algo-a to algo-b */
413 static uint32_t bbr_cross_over = TWENTY_THREE_MBPS;
415 /* Do we deal with our restart state? */
416 static int32_t bbr_uses_idle_restart = 0;
417 static int32_t bbr_idle_restart_threshold = 100000; /* 100ms in useconds */
419 /* Do we allow hardware pacing? */
420 static int32_t bbr_allow_hdwr_pacing = 0;
421 static int32_t bbr_hdwr_pace_adjust = 2; /* multipler when we calc the tso size */
422 static int32_t bbr_hdwr_pace_floor = 1;
423 static int32_t bbr_hdwr_pacing_delay_cnt = 10;
425 /****************************************************/
426 static int32_t bbr_resends_use_tso = 0;
427 static int32_t bbr_tlp_max_resend = 2;
428 static int32_t bbr_sack_block_limit = 128;
430 #define BBR_MAX_STAT 19
431 counter_u64_t bbr_state_time[BBR_MAX_STAT];
432 counter_u64_t bbr_state_lost[BBR_MAX_STAT];
433 counter_u64_t bbr_state_resend[BBR_MAX_STAT];
434 counter_u64_t bbr_stat_arry[BBR_STAT_SIZE];
435 counter_u64_t bbr_opts_arry[BBR_OPTS_SIZE];
436 counter_u64_t bbr_out_size[TCP_MSS_ACCT_SIZE];
437 counter_u64_t bbr_flows_whdwr_pacing;
438 counter_u64_t bbr_flows_nohdwr_pacing;
440 counter_u64_t bbr_nohdwr_pacing_enobuf;
441 counter_u64_t bbr_hdwr_pacing_enobuf;
443 static inline uint64_t bbr_get_bw(struct tcp_bbr *bbr);
446 * Static defintions we need for forward declarations.
449 bbr_get_pacing_length(struct tcp_bbr *bbr, uint16_t gain,
450 uint32_t useconds_time, uint64_t bw);
452 bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain);
454 bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win);
456 bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses);
458 bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int line,
461 bbr_get_target_cwnd(struct tcp_bbr *bbr, uint64_t bw, uint32_t gain);
463 bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch,
464 int32_t pkt_epoch, uint32_t losses);
466 bbr_calc_thresh_rack(struct tcp_bbr *bbr, uint32_t srtt, uint32_t cts, struct bbr_sendmap *rsm);
467 static uint32_t bbr_initial_cwnd(struct tcp_bbr *bbr, struct tcpcb *tp);
469 bbr_calc_thresh_tlp(struct tcpcb *tp, struct tcp_bbr *bbr,
470 struct bbr_sendmap *rsm, uint32_t srtt,
473 bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts,
476 bbr_set_state_target(struct tcp_bbr *bbr, int line);
478 bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line);
481 bbr_log_progress_event(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t tick, int event, int line);
484 tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts);
487 bbr_setup_red_bw(struct tcp_bbr *bbr, uint32_t cts);
490 bbr_log_rtt_shrinks(struct tcp_bbr *bbr, uint32_t cts, uint32_t applied, uint32_t rtt,
491 uint32_t line, uint8_t is_start, uint16_t set);
493 static struct bbr_sendmap *
494 bbr_find_lowest_rsm(struct tcp_bbr *bbr);
495 static __inline uint32_t
496 bbr_get_rtt(struct tcp_bbr *bbr, int32_t rtt_type);
498 bbr_log_to_start(struct tcp_bbr *bbr, uint32_t cts, uint32_t to, int32_t slot, uint8_t which);
501 bbr_log_timer_var(struct tcp_bbr *bbr, int mode, uint32_t cts, uint32_t time_since_sent, uint32_t srtt,
502 uint32_t thresh, uint32_t to);
504 bbr_log_hpts_diag(struct tcp_bbr *bbr, uint32_t cts, struct hpts_diag *diag);
507 bbr_log_type_bbrsnd(struct tcp_bbr *bbr, uint32_t len, uint32_t slot,
508 uint32_t del_by, uint32_t cts, uint32_t sloton, uint32_t prev_delay);
511 bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr,
512 uint32_t cts, int32_t line);
514 bbr_stop_all_timers(struct tcpcb *tp);
516 bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts);
518 bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts);
520 bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts);
524 bbr_log_pacing_delay_calc(struct tcp_bbr *bbr, uint16_t gain, uint32_t len,
525 uint32_t cts, uint32_t usecs, uint64_t bw, uint32_t override, int mod);
527 static inline uint8_t
528 bbr_state_val(struct tcp_bbr *bbr)
530 return(bbr->rc_bbr_substate);
533 static inline uint32_t
534 get_min_cwnd(struct tcp_bbr *bbr)
538 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs);
539 if (bbr_get_rtt(bbr, BBR_RTT_PROP) < BBR_HIGH_SPEED)
540 return (bbr_cwnd_min_val_hs * mss);
542 return (bbr_cwnd_min_val * mss);
546 bbr_get_persists_timer_val(struct tcpcb *tp, struct tcp_bbr *bbr)
551 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT;
552 if (tp->t_srtt == 0) {
553 srtt = (uint64_t)BBR_INITIAL_RTO;
556 srtt = ((uint64_t)TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT);
557 var = ((uint64_t)TICKS_2_USEC(tp->t_rttvar) >> TCP_RTT_SHIFT);
559 TCPT_RANGESET_NOSLOP(ret_val, ((srtt + var) * tcp_backoff[tp->t_rxtshift]),
560 bbr_persist_min, bbr_persist_max);
561 return ((uint32_t)ret_val);
565 bbr_timer_start(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
568 * Start the FR timer, we do this based on getting the first one in
569 * the rc_tmap. Note that if its NULL we must stop the timer. in all
570 * events we need to stop the running timer (if its running) before
571 * starting the new one.
573 uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse;
575 int32_t is_tlp_timer = 0;
576 struct bbr_sendmap *rsm;
578 if (bbr->rc_all_timers_stopped) {
579 /* All timers have been stopped none are to run */
582 if (bbr->rc_in_persist) {
583 /* We can't start any timer in persists */
584 return (bbr_get_persists_timer_val(tp, bbr));
586 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
588 ((tp->t_flags & TF_SACK_PERMIT) == 0) ||
589 (tp->t_state < TCPS_ESTABLISHED)) {
590 /* Nothing on the send map */
592 if (SEQ_LT(tp->snd_una, tp->snd_max) || sbavail(&(tp->t_inpcb->inp_socket->so_snd))) {
596 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
598 idx = rsm->r_rtr_cnt - 1;
599 if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], bbr->r_ctl.rc_tlp_rxt_last_time))
600 tstmp_touse = rsm->r_tim_lastsent[idx];
602 tstmp_touse = bbr->r_ctl.rc_tlp_rxt_last_time;
603 if (TSTMP_GT(tstmp_touse, cts))
604 time_since_sent = cts - tstmp_touse;
606 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_RXT;
608 tov = BBR_INITIAL_RTO;
610 tov = ((uint64_t)(TICKS_2_USEC(tp->t_srtt) +
611 ((uint64_t)TICKS_2_USEC(tp->t_rttvar) * (uint64_t)4)) >> TCP_RTT_SHIFT);
613 tov *= tcp_backoff[tp->t_rxtshift];
614 if (tov > time_since_sent)
615 tov -= time_since_sent;
617 tov = bbr->r_ctl.rc_min_to;
618 TCPT_RANGESET_NOSLOP(to, tov,
619 (bbr->r_ctl.rc_min_rto_ms * MS_IN_USEC),
620 (bbr->rc_max_rto_sec * USECS_IN_SECOND));
621 bbr_log_timer_var(bbr, 2, cts, 0, srtt, 0, to);
626 if (rsm->r_flags & BBR_ACKED) {
627 rsm = bbr_find_lowest_rsm(bbr);
633 /* Convert from ms to usecs */
634 if (rsm->r_flags & BBR_SACK_PASSED) {
635 if ((tp->t_flags & TF_SENTFIN) &&
636 ((tp->snd_max - tp->snd_una) == 1) &&
637 (rsm->r_flags & BBR_HAS_FIN)) {
639 * We don't start a bbr rack timer if all we have is
644 srtt = bbr_get_rtt(bbr, BBR_RTT_RACK);
645 thresh = bbr_calc_thresh_rack(bbr, srtt, cts, rsm);
646 idx = rsm->r_rtr_cnt - 1;
647 exp = rsm->r_tim_lastsent[idx] + thresh;
648 if (SEQ_GEQ(exp, cts)) {
650 if (to < bbr->r_ctl.rc_min_to) {
651 to = bbr->r_ctl.rc_min_to;
654 to = bbr->r_ctl.rc_min_to;
657 /* Ok we need to do a TLP not RACK */
658 if (bbr->rc_tlp_in_progress != 0) {
660 * The previous send was a TLP.
664 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_tmap, bbr_sendmap, r_tnext);
666 /* We found no rsm to TLP with. */
669 if (rsm->r_flags & BBR_HAS_FIN) {
670 /* If its a FIN we don't do TLP */
675 idx = rsm->r_rtr_cnt - 1;
676 if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], bbr->r_ctl.rc_tlp_rxt_last_time))
677 tstmp_touse = rsm->r_tim_lastsent[idx];
679 tstmp_touse = bbr->r_ctl.rc_tlp_rxt_last_time;
680 if (TSTMP_GT(tstmp_touse, cts))
681 time_since_sent = cts - tstmp_touse;
683 srtt = bbr_get_rtt(bbr, bbr_tlp_type_to_use);
684 thresh = bbr_calc_thresh_tlp(tp, bbr, rsm, srtt, cts);
685 if (thresh > time_since_sent)
686 to = thresh - time_since_sent;
688 to = bbr->r_ctl.rc_min_to;
689 if (to > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) {
691 * If the TLP time works out to larger than the max
692 * RTO lets not do TLP.. just RTO.
696 if ((bbr->rc_tlp_rtx_out == 1) &&
697 (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq)) {
699 * Second retransmit of the same TLP
702 bbr->rc_tlp_rtx_out = 0;
705 if (rsm->r_start != bbr->r_ctl.rc_last_tlp_seq) {
707 * The tail is no longer the last one I did a probe
710 bbr->r_ctl.rc_tlp_seg_send_cnt = 0;
711 bbr->r_ctl.rc_last_tlp_seq = rsm->r_start;
714 if (is_tlp_timer == 0) {
715 BBR_STAT_INC(bbr_to_arm_rack);
716 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_RACK;
718 bbr_log_timer_var(bbr, 1, cts, time_since_sent, srtt, thresh, to);
719 if (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend) {
721 * We have exceeded how many times we can retran the
722 * current TLP timer, switch to the RTO timer.
726 BBR_STAT_INC(bbr_to_arm_tlp);
727 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_TLP;
733 static inline int32_t
734 bbr_minseg(struct tcp_bbr *bbr)
736 return (bbr->r_ctl.rc_pace_min_segs - bbr->rc_last_options);
740 bbr_start_hpts_timer(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t cts, int32_t frm, int32_t slot, uint32_t tot_len)
743 struct hpts_diag diag;
744 uint32_t delayed_ack = 0;
746 uint32_t hpts_timeout;
748 int32_t delay_calc = 0;
749 uint32_t prev_delay = 0;
752 if (inp->inp_in_hpts) {
753 /* A previous call is already set up */
756 if ((tp->t_state == TCPS_CLOSED) ||
757 (tp->t_state == TCPS_LISTEN)) {
760 stopped = bbr->rc_tmr_stopped;
761 if (stopped && TSTMP_GT(bbr->r_ctl.rc_timer_exp, cts)) {
762 left = bbr->r_ctl.rc_timer_exp - cts;
764 bbr->r_ctl.rc_hpts_flags = 0;
765 bbr->r_ctl.rc_timer_exp = 0;
766 prev_delay = bbr->r_ctl.rc_last_delay_val;
767 if (bbr->r_ctl.rc_last_delay_val &&
770 * If a previous pacer delay was in place we
771 * are not coming from the output side (where
772 * we calculate a delay, more likely a timer).
774 slot = bbr->r_ctl.rc_last_delay_val;
775 if (TSTMP_GT(cts, bbr->rc_pacer_started)) {
776 /* Compensate for time passed */
777 delay_calc = cts - bbr->rc_pacer_started;
778 if (delay_calc <= slot)
782 /* Do we have early to make up for by pushing out the pacing time? */
783 if (bbr->r_agg_early_set) {
784 bbr_log_pacing_delay_calc(bbr, 0, bbr->r_ctl.rc_agg_early, cts, slot, 0, bbr->r_agg_early_set, 2);
785 slot += bbr->r_ctl.rc_agg_early;
786 bbr->r_ctl.rc_agg_early = 0;
787 bbr->r_agg_early_set = 0;
789 /* Are we running a total debt that needs to be compensated for? */
790 if (bbr->r_ctl.rc_hptsi_agg_delay) {
791 if (slot > bbr->r_ctl.rc_hptsi_agg_delay) {
792 /* We nuke the delay */
793 slot -= bbr->r_ctl.rc_hptsi_agg_delay;
794 bbr->r_ctl.rc_hptsi_agg_delay = 0;
796 /* We nuke some of the delay, put in a minimal 100usecs */
797 bbr->r_ctl.rc_hptsi_agg_delay -= slot;
798 bbr->r_ctl.rc_last_delay_val = slot = 100;
801 bbr->r_ctl.rc_last_delay_val = slot;
802 hpts_timeout = bbr_timer_start(tp, bbr, cts);
803 if (tp->t_flags & TF_DELACK) {
804 if (bbr->rc_in_persist == 0) {
805 delayed_ack = bbr_delack_time;
808 * We are in persists and have
809 * gotten a new data element.
811 if (hpts_timeout > bbr_delack_time) {
813 * Lets make the persists timer (which acks)
814 * be the smaller of hpts_timeout and bbr_delack_time.
816 hpts_timeout = bbr_delack_time;
821 ((hpts_timeout == 0) ||
822 (delayed_ack < hpts_timeout))) {
823 /* We need a Delayed ack timer */
824 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK;
825 hpts_timeout = delayed_ack;
828 /* Mark that we have a pacing timer up */
829 BBR_STAT_INC(bbr_paced_segments);
830 bbr->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT;
833 * If no timers are going to run and we will fall off thfe hptsi
834 * wheel, we resort to a keep-alive timer if its configured.
836 if ((hpts_timeout == 0) &&
838 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
839 (tp->t_state <= TCPS_CLOSING)) {
841 * Ok we have no timer (persists, rack, tlp, rxt or
842 * del-ack), we don't have segments being paced. So
843 * all that is left is the keepalive timer.
845 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
846 hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp));
848 hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp));
850 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP;
853 if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) ==
854 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) {
856 * RACK, TLP, persists and RXT timers all are restartable
857 * based on actions input .. i.e we received a packet (ack
858 * or sack) and that changes things (rw, or snd_una etc).
859 * Thus we can restart them with a new value. For
860 * keep-alive, delayed_ack we keep track of what was left
861 * and restart the timer with a smaller value.
863 if (left < hpts_timeout)
866 if (bbr->r_ctl.rc_incr_tmrs && slot &&
867 (bbr->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) {
869 * If configured to do so, and the timer is either
870 * the TLP or RXT timer, we need to increase the timeout
871 * by the pacing time. Consider the bottleneck at my
872 * machine as an example, we are sending something
873 * to start a TLP on. The last packet won't be emitted
874 * fully until the pacing time (the bottleneck will hold
875 * the data in place). Once the packet is emitted that
876 * is when we want to start waiting for the TLP. This
877 * is most evident with hardware pacing (where the nic
878 * is holding the packet(s) before emitting). But it
879 * can also show up in the network so we do it for all
880 * cases. Technically we would take off one packet from
881 * this extra delay but this is easier and being more
882 * conservative is probably better.
884 hpts_timeout += slot;
888 * Hack alert for now we can't time-out over 2147 seconds (a
889 * bit more than 35min)
891 if (hpts_timeout > 0x7ffffffe)
892 hpts_timeout = 0x7ffffffe;
893 bbr->r_ctl.rc_timer_exp = cts + hpts_timeout;
895 bbr->r_ctl.rc_timer_exp = 0;
897 (bbr->rc_use_google ||
898 bbr->output_error_seen ||
899 (slot <= hpts_timeout)) ) {
901 * Tell LRO that it can queue packets while
904 bbr->rc_inp->inp_flags2 |= INP_MBUF_QUEUE_READY;
905 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) &&
906 (bbr->rc_cwnd_limited == 0)) {
908 * If we are not cwnd limited and we
909 * are running a rack timer we put on
910 * the do not disturbe even for sack.
912 inp->inp_flags2 |= INP_DONT_SACK_QUEUE;
914 inp->inp_flags2 &= ~INP_DONT_SACK_QUEUE;
915 bbr->rc_pacer_started = cts;
917 (void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(slot),
919 bbr->rc_timer_first = 0;
920 bbr->bbr_timer_src = frm;
921 bbr_log_to_start(bbr, cts, hpts_timeout, slot, 1);
922 bbr_log_hpts_diag(bbr, cts, &diag);
923 } else if (hpts_timeout) {
924 (void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(hpts_timeout),
927 * We add the flag here as well if the slot is set,
928 * since hpts will call in to clear the queue first before
929 * calling the output routine (which does our timers).
930 * We don't want to set the flag if its just a timer
931 * else the arrival of data might (that causes us
932 * to send more) might get delayed. Imagine being
933 * on a keep-alive timer and a request comes in for
937 bbr->rc_pacer_started = cts;
938 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) &&
939 (bbr->rc_cwnd_limited == 0)) {
941 * For a rack timer, don't wake us even
942 * if a sack arrives as long as we are
945 bbr->rc_inp->inp_flags2 |= INP_MBUF_QUEUE_READY;
946 inp->inp_flags2 |= INP_DONT_SACK_QUEUE;
948 /* All other timers wake us up */
949 bbr->rc_inp->inp_flags2 &= ~INP_MBUF_QUEUE_READY;
950 inp->inp_flags2 &= ~INP_DONT_SACK_QUEUE;
952 bbr->bbr_timer_src = frm;
953 bbr_log_to_start(bbr, cts, hpts_timeout, slot, 0);
954 bbr_log_hpts_diag(bbr, cts, &diag);
955 bbr->rc_timer_first = 1;
957 bbr->rc_tmr_stopped = 0;
958 bbr_log_type_bbrsnd(bbr, tot_len, slot, delay_calc, cts, frm, prev_delay);
962 bbr_timer_audit(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, struct sockbuf *sb)
965 * We received an ack, and then did not call send or were bounced
966 * out due to the hpts was running. Now a timer is up as well, is it
970 struct bbr_sendmap *rsm;
971 uint32_t hpts_timeout;
974 tmr_up = bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
975 if (bbr->rc_in_persist && (tmr_up == PACE_TMR_PERSIT))
977 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
978 if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) &&
979 (tmr_up == PACE_TMR_RXT)) {
980 /* Should be an RXT */
985 /* Nothing outstanding? */
986 if (tp->t_flags & TF_DELACK) {
987 if (tmr_up == PACE_TMR_DELACK)
989 * We are supposed to have delayed ack up
993 } else if (sbavail(&inp->inp_socket->so_snd) &&
994 (tmr_up == PACE_TMR_RXT)) {
996 * if we hit enobufs then we would expect the
997 * possiblity of nothing outstanding and the RXT up
998 * (and the hptsi timer).
1001 } else if (((V_tcp_always_keepalive ||
1002 inp->inp_socket->so_options & SO_KEEPALIVE) &&
1003 (tp->t_state <= TCPS_CLOSING)) &&
1004 (tmr_up == PACE_TMR_KEEP) &&
1005 (tp->snd_max == tp->snd_una)) {
1006 /* We should have keep alive up and we do */
1010 if (rsm && (rsm->r_flags & BBR_SACK_PASSED)) {
1011 if ((tp->t_flags & TF_SENTFIN) &&
1012 ((tp->snd_max - tp->snd_una) == 1) &&
1013 (rsm->r_flags & BBR_HAS_FIN)) {
1014 /* needs to be a RXT */
1015 if (tmr_up == PACE_TMR_RXT)
1019 } else if (tmr_up == PACE_TMR_RACK)
1023 } else if (rsm && (tmr_up == PACE_TMR_RACK)) {
1024 /* Rack timer has priority if we have data out */
1026 } else if (SEQ_GT(tp->snd_max, tp->snd_una) &&
1027 ((tmr_up == PACE_TMR_TLP) ||
1028 (tmr_up == PACE_TMR_RXT))) {
1030 * Either a TLP or RXT is fine if no sack-passed is in place
1031 * and data is outstanding.
1034 } else if (tmr_up == PACE_TMR_DELACK) {
1036 * If the delayed ack was going to go off before the
1037 * rtx/tlp/rack timer were going to expire, then that would
1038 * be the timer in control. Note we don't check the time
1039 * here trusting the code is correct.
1043 if (SEQ_GT(tp->snd_max, tp->snd_una) &&
1044 ((tmr_up == PACE_TMR_RXT) ||
1045 (tmr_up == PACE_TMR_TLP) ||
1046 (tmr_up == PACE_TMR_RACK))) {
1048 * We have outstanding data and
1049 * we *do* have a RACK, TLP or RXT
1050 * timer running. We won't restart
1051 * anything here since thats probably ok we
1052 * will get called with some timer here shortly.
1057 * Ok the timer originally started is not what we want now. We will
1058 * force the hpts to be stopped if any, and restart with the slot
1059 * set to what was in the saved slot.
1062 if ((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) {
1063 if (inp->inp_in_hpts)
1064 tcp_hpts_remove(inp, HPTS_REMOVE_OUTPUT);
1065 bbr_timer_cancel(bbr, __LINE__, cts);
1066 bbr_start_hpts_timer(bbr, tp, cts, 1, bbr->r_ctl.rc_last_delay_val,
1070 * Output is hptsi so we just need to switch the type of
1071 * timer. We don't bother with keep-alive, since when we
1072 * jump through the output, it will start the keep-alive if
1075 * We only need a delayed-ack added and or the hpts_timeout.
1077 hpts_timeout = bbr_timer_start(tp, bbr, cts);
1078 if (tp->t_flags & TF_DELACK) {
1079 if (hpts_timeout == 0) {
1080 hpts_timeout = bbr_delack_time;
1081 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK;
1083 else if (hpts_timeout > bbr_delack_time) {
1084 hpts_timeout = bbr_delack_time;
1085 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK;
1089 if (hpts_timeout > 0x7ffffffe)
1090 hpts_timeout = 0x7ffffffe;
1091 bbr->r_ctl.rc_timer_exp = cts + hpts_timeout;
1096 int32_t bbr_clear_lost = 0;
1099 * Considers the two time values now (cts) and earlier.
1100 * If cts is smaller than earlier, we could have
1101 * had a sequence wrap (our counter wraps every
1102 * 70 min or so) or it could be just clock skew
1103 * getting us two differnt time values. Clock skew
1104 * will show up within 10ms or so. So in such
1105 * a case (where cts is behind earlier time by
1106 * less than 10ms) we return 0. Otherwise we
1107 * return the true difference between them.
1109 static inline uint32_t
1110 bbr_calc_time(uint32_t cts, uint32_t earlier_time) {
1112 * Given two timestamps, the current time stamp cts, and some other
1113 * time-stamp taken in theory earlier return the difference. The
1114 * trick is here sometimes locking will get the other timestamp
1115 * after the cts. If this occurs we need to return 0.
1117 if (TSTMP_GEQ(cts, earlier_time))
1118 return (cts - earlier_time);
1120 * cts is behind earlier_time if its less than 10ms consider it 0.
1121 * If its more than 10ms difference then we had a time wrap. Else
1122 * its just the normal locking foo. I wonder if we should not go to
1123 * 64bit TS and get rid of this issue.
1125 if (TSTMP_GEQ((cts + 10000), earlier_time))
1128 * Ok the time must have wrapped. So we need to answer a large
1129 * amount of time, which the normal subtraction should do.
1131 return (cts - earlier_time);
1137 sysctl_bbr_clear_lost(SYSCTL_HANDLER_ARGS)
1142 error = SYSCTL_OUT(req, &bbr_clear_lost, sizeof(uint32_t));
1143 if (error || req->newptr == NULL)
1146 error = SYSCTL_IN(req, &stat, sizeof(uint32_t));
1150 #ifdef BBR_INVARIANTS
1151 printf("Clearing BBR lost counters\n");
1153 COUNTER_ARRAY_ZERO(bbr_state_lost, BBR_MAX_STAT);
1154 COUNTER_ARRAY_ZERO(bbr_state_time, BBR_MAX_STAT);
1155 COUNTER_ARRAY_ZERO(bbr_state_resend, BBR_MAX_STAT);
1156 } else if (stat == 2) {
1157 #ifdef BBR_INVARIANTS
1158 printf("Clearing BBR option counters\n");
1160 COUNTER_ARRAY_ZERO(bbr_opts_arry, BBR_OPTS_SIZE);
1161 } else if (stat == 3) {
1162 #ifdef BBR_INVARIANTS
1163 printf("Clearing BBR stats counters\n");
1165 COUNTER_ARRAY_ZERO(bbr_stat_arry, BBR_STAT_SIZE);
1166 } else if (stat == 4) {
1167 #ifdef BBR_INVARIANTS
1168 printf("Clearing BBR out-size counters\n");
1170 COUNTER_ARRAY_ZERO(bbr_out_size, TCP_MSS_ACCT_SIZE);
1177 bbr_init_sysctls(void)
1179 struct sysctl_oid *bbr_probertt;
1180 struct sysctl_oid *bbr_hptsi;
1181 struct sysctl_oid *bbr_measure;
1182 struct sysctl_oid *bbr_cwnd;
1183 struct sysctl_oid *bbr_timeout;
1184 struct sysctl_oid *bbr_states;
1185 struct sysctl_oid *bbr_startup;
1186 struct sysctl_oid *bbr_policer;
1188 /* Probe rtt controls */
1189 bbr_probertt = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1190 SYSCTL_CHILDREN(bbr_sysctl_root),
1193 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1195 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1196 SYSCTL_CHILDREN(bbr_probertt),
1197 OID_AUTO, "gain", CTLFLAG_RW,
1198 &bbr_rttprobe_gain, 192,
1199 "What is the filter gain drop in probe_rtt (0=disable)?");
1200 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1201 SYSCTL_CHILDREN(bbr_probertt),
1202 OID_AUTO, "cwnd", CTLFLAG_RW,
1203 &bbr_rtt_probe_cwndtarg, 4,
1204 "How many mss's are outstanding during probe-rtt");
1205 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1206 SYSCTL_CHILDREN(bbr_probertt),
1207 OID_AUTO, "int", CTLFLAG_RW,
1208 &bbr_rtt_probe_limit, 4000000,
1209 "If RTT has not shrank in this many micro-seconds enter probe-rtt");
1210 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1211 SYSCTL_CHILDREN(bbr_probertt),
1212 OID_AUTO, "mintime", CTLFLAG_RW,
1213 &bbr_rtt_probe_time, 200000,
1214 "How many microseconds in probe-rtt");
1215 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1216 SYSCTL_CHILDREN(bbr_probertt),
1217 OID_AUTO, "filter_len_sec", CTLFLAG_RW,
1218 &bbr_filter_len_sec, 6,
1219 "How long in seconds does the rttProp filter run?");
1220 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1221 SYSCTL_CHILDREN(bbr_probertt),
1222 OID_AUTO, "drain_rtt", CTLFLAG_RW,
1223 &bbr_drain_rtt, BBR_SRTT,
1224 "What is the drain rtt to use in probeRTT (rtt_prop=0, rtt_rack=1, rtt_pkt=2, rtt_srtt=3?");
1225 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1226 SYSCTL_CHILDREN(bbr_probertt),
1227 OID_AUTO, "can_force", CTLFLAG_RW,
1228 &bbr_can_force_probertt, 0,
1229 "If we keep setting new low rtt's but delay going in probe-rtt can we force in??");
1230 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1231 SYSCTL_CHILDREN(bbr_probertt),
1232 OID_AUTO, "enter_sets_force", CTLFLAG_RW,
1233 &bbr_probertt_sets_rtt, 0,
1234 "In NF mode, do we imitate google_mode and set the rttProp on entry to probe-rtt?");
1235 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1236 SYSCTL_CHILDREN(bbr_probertt),
1237 OID_AUTO, "can_adjust", CTLFLAG_RW,
1238 &bbr_can_adjust_probertt, 1,
1239 "Can we dynamically adjust the probe-rtt limits and times?");
1240 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1241 SYSCTL_CHILDREN(bbr_probertt),
1242 OID_AUTO, "is_ratio", CTLFLAG_RW,
1244 "is the limit to filter a ratio?");
1245 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1246 SYSCTL_CHILDREN(bbr_probertt),
1247 OID_AUTO, "use_cwnd", CTLFLAG_RW,
1248 &bbr_prtt_slam_cwnd, 0,
1249 "Should we set/recover cwnd?");
1250 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1251 SYSCTL_CHILDREN(bbr_probertt),
1252 OID_AUTO, "can_use_ts", CTLFLAG_RW,
1253 &bbr_can_use_ts_for_rtt, 1,
1254 "Can we use the ms timestamp if available for retransmistted rtt calculations?");
1256 /* Pacing controls */
1257 bbr_hptsi = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1258 SYSCTL_CHILDREN(bbr_sysctl_root),
1261 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1263 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1264 SYSCTL_CHILDREN(bbr_hptsi),
1265 OID_AUTO, "hw_pacing", CTLFLAG_RW,
1266 &bbr_allow_hdwr_pacing, 1,
1267 "Do we allow hardware pacing?");
1268 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1269 SYSCTL_CHILDREN(bbr_hptsi),
1270 OID_AUTO, "hw_pacing_limit", CTLFLAG_RW,
1271 &bbr_hardware_pacing_limit, 4000,
1272 "Do we have a limited number of connections for pacing chelsio (0=no limit)?");
1273 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1274 SYSCTL_CHILDREN(bbr_hptsi),
1275 OID_AUTO, "hw_pacing_adj", CTLFLAG_RW,
1276 &bbr_hdwr_pace_adjust, 2,
1277 "Multiplier to calculated tso size?");
1278 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1279 SYSCTL_CHILDREN(bbr_hptsi),
1280 OID_AUTO, "hw_pacing_floor", CTLFLAG_RW,
1281 &bbr_hdwr_pace_floor, 1,
1282 "Do we invoke the hardware pacing floor?");
1283 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1284 SYSCTL_CHILDREN(bbr_hptsi),
1285 OID_AUTO, "hw_pacing_delay_cnt", CTLFLAG_RW,
1286 &bbr_hdwr_pacing_delay_cnt, 10,
1287 "How many packets must be sent after hdwr pacing is enabled");
1288 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1289 SYSCTL_CHILDREN(bbr_hptsi),
1290 OID_AUTO, "bw_cross", CTLFLAG_RW,
1291 &bbr_cross_over, 3000000,
1292 "What is the point where we cross over to linux like TSO size set");
1293 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1294 SYSCTL_CHILDREN(bbr_hptsi),
1295 OID_AUTO, "seg_deltarg", CTLFLAG_RW,
1296 &bbr_hptsi_segments_delay_tar, 7000,
1297 "What is the worse case delay target for hptsi < 48Mbp connections");
1298 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1299 SYSCTL_CHILDREN(bbr_hptsi),
1300 OID_AUTO, "enet_oh", CTLFLAG_RW,
1301 &bbr_include_enet_oh, 0,
1302 "Do we include the ethernet overhead in calculating pacing delay?");
1303 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1304 SYSCTL_CHILDREN(bbr_hptsi),
1305 OID_AUTO, "ip_oh", CTLFLAG_RW,
1306 &bbr_include_ip_oh, 1,
1307 "Do we include the IP overhead in calculating pacing delay?");
1308 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1309 SYSCTL_CHILDREN(bbr_hptsi),
1310 OID_AUTO, "tcp_oh", CTLFLAG_RW,
1311 &bbr_include_tcp_oh, 0,
1312 "Do we include the TCP overhead in calculating pacing delay?");
1313 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1314 SYSCTL_CHILDREN(bbr_hptsi),
1315 OID_AUTO, "google_discount", CTLFLAG_RW,
1316 &bbr_google_discount, 10,
1317 "What is the default google discount percentage wise for pacing (11 = 1.1%%)?");
1318 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1319 SYSCTL_CHILDREN(bbr_hptsi),
1320 OID_AUTO, "all_get_min", CTLFLAG_RW,
1321 &bbr_all_get_min, 0,
1322 "If you are less than a MSS do you just get the min?");
1323 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1324 SYSCTL_CHILDREN(bbr_hptsi),
1325 OID_AUTO, "tso_min", CTLFLAG_RW,
1326 &bbr_hptsi_bytes_min, 1460,
1327 "For 0 -> 24Mbps what is floor number of segments for TSO");
1328 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1329 SYSCTL_CHILDREN(bbr_hptsi),
1330 OID_AUTO, "seg_tso_max", CTLFLAG_RW,
1331 &bbr_hptsi_segments_max, 6,
1332 "For 0 -> 24Mbps what is top number of segments for TSO");
1333 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1334 SYSCTL_CHILDREN(bbr_hptsi),
1335 OID_AUTO, "seg_floor", CTLFLAG_RW,
1336 &bbr_hptsi_segments_floor, 1,
1337 "Minimum TSO size we will fall too in segments");
1338 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1339 SYSCTL_CHILDREN(bbr_hptsi),
1340 OID_AUTO, "utter_max", CTLFLAG_RW,
1341 &bbr_hptsi_utter_max, 0,
1342 "The absolute maximum that any pacing (outside of hardware) can be");
1343 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1344 SYSCTL_CHILDREN(bbr_hptsi),
1345 OID_AUTO, "seg_divisor", CTLFLAG_RW,
1346 &bbr_hptsi_per_second, 100,
1347 "What is the divisor in our hptsi TSO calculation 512Mbps < X > 24Mbps ");
1348 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1349 SYSCTL_CHILDREN(bbr_hptsi),
1350 OID_AUTO, "srtt_mul", CTLFLAG_RW,
1351 &bbr_hptsi_max_mul, 1,
1352 "The multiplier for pace len max");
1353 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1354 SYSCTL_CHILDREN(bbr_hptsi),
1355 OID_AUTO, "srtt_div", CTLFLAG_RW,
1356 &bbr_hptsi_max_div, 2,
1357 "The divisor for pace len max");
1358 /* Measurement controls */
1359 bbr_measure = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1360 SYSCTL_CHILDREN(bbr_sysctl_root),
1363 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1364 "Measurement controls");
1365 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1366 SYSCTL_CHILDREN(bbr_measure),
1367 OID_AUTO, "min_i_bw", CTLFLAG_RW,
1368 &bbr_initial_bw_bps, 62500,
1369 "Minimum initial b/w in bytes per second");
1370 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1371 SYSCTL_CHILDREN(bbr_measure),
1372 OID_AUTO, "no_sack_needed", CTLFLAG_RW,
1373 &bbr_sack_not_required, 0,
1374 "Do we allow bbr to run on connections not supporting SACK?");
1375 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1376 SYSCTL_CHILDREN(bbr_measure),
1377 OID_AUTO, "use_google", CTLFLAG_RW,
1378 &bbr_use_google_algo, 0,
1379 "Use has close to google V1.0 has possible?");
1380 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1381 SYSCTL_CHILDREN(bbr_measure),
1382 OID_AUTO, "ts_limiting", CTLFLAG_RW,
1383 &bbr_ts_limiting, 1,
1384 "Do we attempt to use the peers timestamp to limit b/w caculations?");
1385 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1386 SYSCTL_CHILDREN(bbr_measure),
1387 OID_AUTO, "ts_can_raise", CTLFLAG_RW,
1388 &bbr_ts_can_raise, 0,
1389 "Can we raise the b/w via timestamp b/w calculation?");
1390 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1391 SYSCTL_CHILDREN(bbr_measure),
1392 OID_AUTO, "ts_delta", CTLFLAG_RW,
1393 &bbr_min_usec_delta, 20000,
1394 "How long in usec between ts of our sends in ts validation code?");
1395 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1396 SYSCTL_CHILDREN(bbr_measure),
1397 OID_AUTO, "ts_peer_delta", CTLFLAG_RW,
1398 &bbr_min_peer_delta, 20,
1399 "What min numerical value should be between the peer deltas?");
1400 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1401 SYSCTL_CHILDREN(bbr_measure),
1402 OID_AUTO, "ts_delta_percent", CTLFLAG_RW,
1403 &bbr_delta_percent, 150,
1404 "What percentage (150 = 15.0) do we allow variance for?");
1405 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1406 SYSCTL_CHILDREN(bbr_measure),
1407 OID_AUTO, "min_measure_good_bw", CTLFLAG_RW,
1408 &bbr_min_measurements_req, 1,
1409 "What is the minimum measurment count we need before we switch to our b/w estimate");
1410 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1411 SYSCTL_CHILDREN(bbr_measure),
1412 OID_AUTO, "min_measure_before_pace", CTLFLAG_RW,
1413 &bbr_no_pacing_until, 4,
1414 "How many pkt-epoch's (0 is off) do we need before pacing is on?");
1415 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1416 SYSCTL_CHILDREN(bbr_measure),
1417 OID_AUTO, "quanta", CTLFLAG_RW,
1419 "Extra quanta to add when calculating the target (ID section 4.2.3.2).");
1420 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1421 SYSCTL_CHILDREN(bbr_measure),
1422 OID_AUTO, "noretran", CTLFLAG_RW,
1424 "Should google mode not use retransmission measurements for the b/w estimation?");
1425 /* State controls */
1426 bbr_states = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1427 SYSCTL_CHILDREN(bbr_sysctl_root),
1430 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1432 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1433 SYSCTL_CHILDREN(bbr_states),
1434 OID_AUTO, "idle_restart", CTLFLAG_RW,
1435 &bbr_uses_idle_restart, 0,
1436 "Do we use a new special idle_restart state to ramp back up quickly?");
1437 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1438 SYSCTL_CHILDREN(bbr_states),
1439 OID_AUTO, "idle_restart_threshold", CTLFLAG_RW,
1440 &bbr_idle_restart_threshold, 100000,
1441 "How long must we be idle before we restart??");
1442 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1443 SYSCTL_CHILDREN(bbr_states),
1444 OID_AUTO, "use_pkt_epoch", CTLFLAG_RW,
1445 &bbr_state_is_pkt_epoch, 0,
1446 "Do we use a pkt-epoch for substate if 0 rttProp?");
1447 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1448 SYSCTL_CHILDREN(bbr_states),
1449 OID_AUTO, "startup_rtt_gain", CTLFLAG_RW,
1450 &bbr_rtt_gain_thresh, 0,
1451 "What increase in RTT triggers us to stop ignoring no-loss and possibly exit startup?");
1452 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1453 SYSCTL_CHILDREN(bbr_states),
1454 OID_AUTO, "drain_floor", CTLFLAG_RW,
1455 &bbr_drain_floor, 88,
1456 "What is the lowest we can drain (pg) too?");
1457 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1458 SYSCTL_CHILDREN(bbr_states),
1459 OID_AUTO, "drain_2_target", CTLFLAG_RW,
1460 &bbr_state_drain_2_tar, 1,
1461 "Do we drain to target in drain substate?");
1462 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1463 SYSCTL_CHILDREN(bbr_states),
1464 OID_AUTO, "gain_2_target", CTLFLAG_RW,
1465 &bbr_gain_to_target, 1,
1466 "Does probe bw gain to target??");
1467 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1468 SYSCTL_CHILDREN(bbr_states),
1469 OID_AUTO, "gain_extra_time", CTLFLAG_RW,
1470 &bbr_gain_gets_extra_too, 1,
1471 "Does probe bw gain get the extra time too?");
1472 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1473 SYSCTL_CHILDREN(bbr_states),
1474 OID_AUTO, "ld_div", CTLFLAG_RW,
1475 &bbr_drain_drop_div, 5,
1476 "Long drain drop divider?");
1477 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1478 SYSCTL_CHILDREN(bbr_states),
1479 OID_AUTO, "ld_mul", CTLFLAG_RW,
1480 &bbr_drain_drop_mul, 4,
1481 "Long drain drop multiplier?");
1482 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1483 SYSCTL_CHILDREN(bbr_states),
1484 OID_AUTO, "rand_ot_disc", CTLFLAG_RW,
1486 "Random discount of the ot?");
1487 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1488 SYSCTL_CHILDREN(bbr_states),
1489 OID_AUTO, "dr_filter_life", CTLFLAG_RW,
1490 &bbr_num_pktepo_for_del_limit, BBR_NUM_RTTS_FOR_DEL_LIMIT,
1491 "How many packet-epochs does the b/w delivery rate last?");
1492 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1493 SYSCTL_CHILDREN(bbr_states),
1494 OID_AUTO, "subdrain_applimited", CTLFLAG_RW,
1495 &bbr_sub_drain_app_limit, 0,
1496 "Does our sub-state drain invoke app limited if its long?");
1497 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1498 SYSCTL_CHILDREN(bbr_states),
1499 OID_AUTO, "use_cwnd_subdrain", CTLFLAG_RW,
1500 &bbr_sub_drain_slam_cwnd, 0,
1501 "Should we set/recover cwnd for sub-state drain?");
1502 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1503 SYSCTL_CHILDREN(bbr_states),
1504 OID_AUTO, "use_cwnd_maindrain", CTLFLAG_RW,
1505 &bbr_slam_cwnd_in_main_drain, 0,
1506 "Should we set/recover cwnd for main-state drain?");
1507 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1508 SYSCTL_CHILDREN(bbr_states),
1509 OID_AUTO, "google_gets_earlyout", CTLFLAG_RW,
1510 &google_allow_early_out, 1,
1511 "Should we allow google probe-bw/drain to exit early at flight target?");
1512 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1513 SYSCTL_CHILDREN(bbr_states),
1514 OID_AUTO, "google_exit_loss", CTLFLAG_RW,
1515 &google_consider_lost, 1,
1516 "Should we have losses exit gain of probebw in google mode??");
1517 /* Startup controls */
1518 bbr_startup = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1519 SYSCTL_CHILDREN(bbr_sysctl_root),
1522 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1523 "Startup controls");
1524 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1525 SYSCTL_CHILDREN(bbr_startup),
1526 OID_AUTO, "cheat_iwnd", CTLFLAG_RW,
1527 &bbr_sends_full_iwnd, 1,
1528 "Do we not pace but burst out initial windows has our TSO size?");
1529 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1530 SYSCTL_CHILDREN(bbr_startup),
1531 OID_AUTO, "loss_threshold", CTLFLAG_RW,
1532 &bbr_startup_loss_thresh, 2000,
1533 "In startup what is the loss threshold in a pe that will exit us from startup?");
1534 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1535 SYSCTL_CHILDREN(bbr_startup),
1536 OID_AUTO, "use_lowerpg", CTLFLAG_RW,
1537 &bbr_use_lower_gain_in_startup, 1,
1538 "Should we use a lower hptsi gain if we see loss in startup?");
1539 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1540 SYSCTL_CHILDREN(bbr_startup),
1541 OID_AUTO, "gain", CTLFLAG_RW,
1542 &bbr_start_exit, 25,
1543 "What gain percent do we need to see to stay in startup??");
1544 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1545 SYSCTL_CHILDREN(bbr_startup),
1546 OID_AUTO, "low_gain", CTLFLAG_RW,
1547 &bbr_low_start_exit, 15,
1548 "What gain percent do we need to see to stay in the lower gain startup??");
1549 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1550 SYSCTL_CHILDREN(bbr_startup),
1551 OID_AUTO, "loss_exit", CTLFLAG_RW,
1552 &bbr_exit_startup_at_loss, 1,
1553 "Should we exit startup at loss in an epoch if we are not gaining?");
1555 bbr_cwnd = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1556 SYSCTL_CHILDREN(bbr_sysctl_root),
1559 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1561 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1562 SYSCTL_CHILDREN(bbr_cwnd),
1563 OID_AUTO, "tar_rtt", CTLFLAG_RW,
1564 &bbr_cwndtarget_rtt_touse, 0,
1565 "Target cwnd rtt measurment to use (0=rtt_prop, 1=rtt_rack, 2=pkt_rtt, 3=srtt)?");
1566 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1567 SYSCTL_CHILDREN(bbr_cwnd),
1568 OID_AUTO, "may_shrink", CTLFLAG_RW,
1569 &bbr_cwnd_may_shrink, 0,
1570 "Can the cwnd shrink if it would grow to more than the target?");
1571 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1572 SYSCTL_CHILDREN(bbr_cwnd),
1573 OID_AUTO, "max_target_limit", CTLFLAG_RW,
1574 &bbr_target_cwnd_mult_limit, 8,
1575 "Do we limit the cwnd to some multiple of the cwnd target if cwnd can't shrink 0=no?");
1576 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1577 SYSCTL_CHILDREN(bbr_cwnd),
1578 OID_AUTO, "highspeed_min", CTLFLAG_RW,
1579 &bbr_cwnd_min_val_hs, BBR_HIGHSPEED_NUM_MSS,
1580 "What is the high-speed min cwnd (rttProp under 1ms)");
1581 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1582 SYSCTL_CHILDREN(bbr_cwnd),
1583 OID_AUTO, "lowspeed_min", CTLFLAG_RW,
1584 &bbr_cwnd_min_val, BBR_PROBERTT_NUM_MSS,
1585 "What is the min cwnd (rttProp > 1ms)");
1586 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1587 SYSCTL_CHILDREN(bbr_cwnd),
1588 OID_AUTO, "initwin", CTLFLAG_RW,
1589 &bbr_def_init_win, 10,
1590 "What is the BBR initial window, if 0 use tcp version");
1591 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1592 SYSCTL_CHILDREN(bbr_cwnd),
1593 OID_AUTO, "do_loss_red", CTLFLAG_RW,
1595 "Do we reduce the b/w at exit from recovery based on ratio of prop/srtt (800=80.0, 0=off)?");
1596 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1597 SYSCTL_CHILDREN(bbr_cwnd),
1598 OID_AUTO, "red_scale", CTLFLAG_RW,
1599 &bbr_red_scale, 20000,
1600 "What RTT do we scale with?");
1601 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1602 SYSCTL_CHILDREN(bbr_cwnd),
1603 OID_AUTO, "red_growslow", CTLFLAG_RW,
1604 &bbr_red_growth_restrict, 1,
1605 "Do we restrict cwnd growth for whats in flight?");
1606 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1607 SYSCTL_CHILDREN(bbr_cwnd),
1608 OID_AUTO, "red_div", CTLFLAG_RW,
1610 "If we reduce whats the divisor?");
1611 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1612 SYSCTL_CHILDREN(bbr_cwnd),
1613 OID_AUTO, "red_mul", CTLFLAG_RW,
1615 "If we reduce whats the mulitiplier?");
1616 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1617 SYSCTL_CHILDREN(bbr_cwnd),
1618 OID_AUTO, "target_is_unit", CTLFLAG_RW,
1619 &bbr_target_is_bbunit, 0,
1620 "Is the state target the pacing_gain or BBR_UNIT?");
1621 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1622 SYSCTL_CHILDREN(bbr_cwnd),
1623 OID_AUTO, "drop_limit", CTLFLAG_RW,
1625 "Number of segments limit for drop (0=use min_cwnd w/flight)?");
1627 /* Timeout controls */
1628 bbr_timeout = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1629 SYSCTL_CHILDREN(bbr_sysctl_root),
1632 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1633 "Time out controls");
1634 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1635 SYSCTL_CHILDREN(bbr_timeout),
1636 OID_AUTO, "delack", CTLFLAG_RW,
1637 &bbr_delack_time, 100000,
1638 "BBR's delayed ack time");
1639 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1640 SYSCTL_CHILDREN(bbr_timeout),
1641 OID_AUTO, "tlp_uses", CTLFLAG_RW,
1642 &bbr_tlp_type_to_use, 3,
1643 "RTT that TLP uses in its calculations, 0=rttProp, 1=Rack_rtt, 2=pkt_rtt and 3=srtt");
1644 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1645 SYSCTL_CHILDREN(bbr_timeout),
1646 OID_AUTO, "persmin", CTLFLAG_RW,
1647 &bbr_persist_min, 250000,
1648 "What is the minimum time in microseconds between persists");
1649 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1650 SYSCTL_CHILDREN(bbr_timeout),
1651 OID_AUTO, "persmax", CTLFLAG_RW,
1652 &bbr_persist_max, 1000000,
1653 "What is the largest delay in microseconds between persists");
1654 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1655 SYSCTL_CHILDREN(bbr_timeout),
1656 OID_AUTO, "tlp_minto", CTLFLAG_RW,
1657 &bbr_tlp_min, 10000,
1658 "TLP Min timeout in usecs");
1659 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1660 SYSCTL_CHILDREN(bbr_timeout),
1661 OID_AUTO, "tlp_dack_time", CTLFLAG_RW,
1662 &bbr_delayed_ack_time, 200000,
1663 "TLP delayed ack compensation value");
1664 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1665 SYSCTL_CHILDREN(bbr_sysctl_root),
1666 OID_AUTO, "minrto", CTLFLAG_RW,
1667 &bbr_rto_min_ms, 30,
1668 "Minimum RTO in ms");
1669 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1670 SYSCTL_CHILDREN(bbr_timeout),
1671 OID_AUTO, "maxrto", CTLFLAG_RW,
1672 &bbr_rto_max_sec, 4,
1673 "Maxiumum RTO in seconds -- should be at least as large as min_rto");
1674 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1675 SYSCTL_CHILDREN(bbr_timeout),
1676 OID_AUTO, "tlp_retry", CTLFLAG_RW,
1677 &bbr_tlp_max_resend, 2,
1678 "How many times does TLP retry a single segment or multiple with no ACK");
1679 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1680 SYSCTL_CHILDREN(bbr_timeout),
1681 OID_AUTO, "minto", CTLFLAG_RW,
1683 "Minimum rack timeout in useconds");
1684 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1685 SYSCTL_CHILDREN(bbr_timeout),
1686 OID_AUTO, "pktdelay", CTLFLAG_RW,
1687 &bbr_pkt_delay, 1000,
1688 "Extra RACK time (in useconds) besides reordering thresh");
1689 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1690 SYSCTL_CHILDREN(bbr_timeout),
1691 OID_AUTO, "incr_tmrs", CTLFLAG_RW,
1692 &bbr_incr_timers, 1,
1693 "Increase the RXT/TLP timer by the pacing time used?");
1694 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1695 SYSCTL_CHILDREN(bbr_timeout),
1696 OID_AUTO, "rxtmark_sackpassed", CTLFLAG_RW,
1697 &bbr_marks_rxt_sack_passed, 0,
1698 "Mark sack passed on all those not ack'd when a RXT hits?");
1699 /* Policer controls */
1700 bbr_policer = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1701 SYSCTL_CHILDREN(bbr_sysctl_root),
1704 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1705 "Policer controls");
1706 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1707 SYSCTL_CHILDREN(bbr_policer),
1708 OID_AUTO, "detect_enable", CTLFLAG_RW,
1709 &bbr_policer_detection_enabled, 1,
1710 "Is policer detection enabled??");
1711 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1712 SYSCTL_CHILDREN(bbr_policer),
1713 OID_AUTO, "min_pes", CTLFLAG_RW,
1714 &bbr_lt_intvl_min_rtts, 4,
1715 "Minimum number of PE's?");
1716 SYSCTL_ADD_U64(&bbr_sysctl_ctx,
1717 SYSCTL_CHILDREN(bbr_policer),
1718 OID_AUTO, "bwdiff", CTLFLAG_RW,
1719 &bbr_lt_bw_diff, (4000/8),
1720 "Minimal bw diff?");
1721 SYSCTL_ADD_U64(&bbr_sysctl_ctx,
1722 SYSCTL_CHILDREN(bbr_policer),
1723 OID_AUTO, "bwratio", CTLFLAG_RW,
1724 &bbr_lt_bw_ratio, 8,
1725 "Minimal bw diff?");
1726 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1727 SYSCTL_CHILDREN(bbr_policer),
1728 OID_AUTO, "from_rack_rxt", CTLFLAG_RW,
1729 &bbr_policer_call_from_rack_to, 0,
1730 "Do we call the policer detection code from a rack-timeout?");
1731 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1732 SYSCTL_CHILDREN(bbr_policer),
1733 OID_AUTO, "false_postive", CTLFLAG_RW,
1734 &bbr_lt_intvl_fp, 0,
1735 "What packet epoch do we do false-postive detection at (0=no)?");
1736 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1737 SYSCTL_CHILDREN(bbr_policer),
1738 OID_AUTO, "loss_thresh", CTLFLAG_RW,
1739 &bbr_lt_loss_thresh, 196,
1740 "Loss threshold 196 = 19.6%?");
1741 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1742 SYSCTL_CHILDREN(bbr_policer),
1743 OID_AUTO, "false_postive_thresh", CTLFLAG_RW,
1744 &bbr_lt_fd_thresh, 100,
1745 "What percentage is the false detection threshold (150=15.0)?");
1747 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1748 SYSCTL_CHILDREN(bbr_sysctl_root),
1749 OID_AUTO, "cheat_rxt", CTLFLAG_RW,
1750 &bbr_use_rack_resend_cheat, 0,
1751 "Do we burst 1ms between sends on retransmissions (like rack)?");
1752 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1753 SYSCTL_CHILDREN(bbr_sysctl_root),
1754 OID_AUTO, "error_paceout", CTLFLAG_RW,
1755 &bbr_error_base_paceout, 10000,
1756 "When we hit an error what is the min to pace out in usec's?");
1757 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1758 SYSCTL_CHILDREN(bbr_sysctl_root),
1759 OID_AUTO, "kill_paceout", CTLFLAG_RW,
1760 &bbr_max_net_error_cnt, 10,
1761 "When we hit this many errors in a row, kill the session?");
1762 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1763 SYSCTL_CHILDREN(bbr_sysctl_root),
1764 OID_AUTO, "data_after_close", CTLFLAG_RW,
1765 &bbr_ignore_data_after_close, 1,
1766 "Do we hold off sending a RST until all pending data is ack'd");
1767 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1768 SYSCTL_CHILDREN(bbr_sysctl_root),
1769 OID_AUTO, "resend_use_tso", CTLFLAG_RW,
1770 &bbr_resends_use_tso, 0,
1771 "Can resends use TSO?");
1772 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1773 SYSCTL_CHILDREN(bbr_sysctl_root),
1774 OID_AUTO, "sblklimit", CTLFLAG_RW,
1775 &bbr_sack_block_limit, 128,
1776 "When do we start ignoring small sack blocks");
1777 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1778 SYSCTL_CHILDREN(bbr_sysctl_root),
1779 OID_AUTO, "bb_verbose", CTLFLAG_RW,
1780 &bbr_verbose_logging, 0,
1781 "Should BBR black box logging be verbose");
1782 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1783 SYSCTL_CHILDREN(bbr_sysctl_root),
1784 OID_AUTO, "reorder_thresh", CTLFLAG_RW,
1785 &bbr_reorder_thresh, 2,
1786 "What factor for rack will be added when seeing reordering (shift right)");
1787 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1788 SYSCTL_CHILDREN(bbr_sysctl_root),
1789 OID_AUTO, "reorder_fade", CTLFLAG_RW,
1790 &bbr_reorder_fade, 0,
1791 "Does reorder detection fade, if so how many ms (0 means never)");
1792 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1793 SYSCTL_CHILDREN(bbr_sysctl_root),
1794 OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW,
1796 "what divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)");
1797 /* Stats and counters */
1798 /* The pacing counters for hdwr/software can't be in the array */
1799 bbr_nohdwr_pacing_enobuf = counter_u64_alloc(M_WAITOK);
1800 bbr_hdwr_pacing_enobuf = counter_u64_alloc(M_WAITOK);
1801 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx,
1802 SYSCTL_CHILDREN(bbr_sysctl_root),
1803 OID_AUTO, "enob_hdwr_pacing", CTLFLAG_RD,
1804 &bbr_hdwr_pacing_enobuf,
1805 "Total number of enobufs for hardware paced flows");
1806 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx,
1807 SYSCTL_CHILDREN(bbr_sysctl_root),
1808 OID_AUTO, "enob_no_hdwr_pacing", CTLFLAG_RD,
1809 &bbr_nohdwr_pacing_enobuf,
1810 "Total number of enobufs for non-hardware paced flows");
1813 bbr_flows_whdwr_pacing = counter_u64_alloc(M_WAITOK);
1814 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx,
1815 SYSCTL_CHILDREN(bbr_sysctl_root),
1816 OID_AUTO, "hdwr_pacing", CTLFLAG_RD,
1817 &bbr_flows_whdwr_pacing,
1818 "Total number of hardware paced flows");
1819 bbr_flows_nohdwr_pacing = counter_u64_alloc(M_WAITOK);
1820 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx,
1821 SYSCTL_CHILDREN(bbr_sysctl_root),
1822 OID_AUTO, "software_pacing", CTLFLAG_RD,
1823 &bbr_flows_nohdwr_pacing,
1824 "Total number of software paced flows");
1825 COUNTER_ARRAY_ALLOC(bbr_stat_arry, BBR_STAT_SIZE, M_WAITOK);
1826 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1827 OID_AUTO, "stats", CTLFLAG_RD,
1828 bbr_stat_arry, BBR_STAT_SIZE, "BBR Stats");
1829 COUNTER_ARRAY_ALLOC(bbr_opts_arry, BBR_OPTS_SIZE, M_WAITOK);
1830 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1831 OID_AUTO, "opts", CTLFLAG_RD,
1832 bbr_opts_arry, BBR_OPTS_SIZE, "BBR Option Stats");
1833 COUNTER_ARRAY_ALLOC(bbr_state_lost, BBR_MAX_STAT, M_WAITOK);
1834 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1835 OID_AUTO, "lost", CTLFLAG_RD,
1836 bbr_state_lost, BBR_MAX_STAT, "Stats of when losses occur");
1837 COUNTER_ARRAY_ALLOC(bbr_state_resend, BBR_MAX_STAT, M_WAITOK);
1838 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1839 OID_AUTO, "stateresend", CTLFLAG_RD,
1840 bbr_state_resend, BBR_MAX_STAT, "Stats of what states resend");
1841 COUNTER_ARRAY_ALLOC(bbr_state_time, BBR_MAX_STAT, M_WAITOK);
1842 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1843 OID_AUTO, "statetime", CTLFLAG_RD,
1844 bbr_state_time, BBR_MAX_STAT, "Stats of time spent in the states");
1845 COUNTER_ARRAY_ALLOC(bbr_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK);
1846 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1847 OID_AUTO, "outsize", CTLFLAG_RD,
1848 bbr_out_size, TCP_MSS_ACCT_SIZE, "Size of output calls");
1849 SYSCTL_ADD_PROC(&bbr_sysctl_ctx,
1850 SYSCTL_CHILDREN(bbr_sysctl_root),
1851 OID_AUTO, "clrlost", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1852 &bbr_clear_lost, 0, sysctl_bbr_clear_lost, "IU", "Clear lost counters");
1855 static inline int32_t
1856 bbr_progress_timeout_check(struct tcp_bbr *bbr)
1858 if (bbr->rc_tp->t_maxunacktime && bbr->rc_tp->t_acktime &&
1859 TSTMP_GT(ticks, bbr->rc_tp->t_acktime)) {
1860 if ((((uint32_t)ticks - bbr->rc_tp->t_acktime)) >= bbr->rc_tp->t_maxunacktime) {
1862 * There is an assumption here that the caller will
1863 * drop the connection, so we increment the
1866 bbr_log_progress_event(bbr, bbr->rc_tp, ticks, PROGRESS_DROP, __LINE__);
1867 BBR_STAT_INC(bbr_progress_drops);
1868 #ifdef NETFLIX_STATS
1869 KMOD_TCPSTAT_INC(tcps_progdrops);
1878 bbr_counter_destroy(void)
1880 COUNTER_ARRAY_FREE(bbr_stat_arry, BBR_STAT_SIZE);
1881 COUNTER_ARRAY_FREE(bbr_opts_arry, BBR_OPTS_SIZE);
1882 COUNTER_ARRAY_FREE(bbr_out_size, TCP_MSS_ACCT_SIZE);
1883 COUNTER_ARRAY_FREE(bbr_state_lost, BBR_MAX_STAT);
1884 COUNTER_ARRAY_FREE(bbr_state_time, BBR_MAX_STAT);
1885 COUNTER_ARRAY_FREE(bbr_state_resend, BBR_MAX_STAT);
1886 counter_u64_free(bbr_flows_whdwr_pacing);
1887 counter_u64_free(bbr_flows_nohdwr_pacing);
1891 static __inline void
1892 bbr_fill_in_logging_data(struct tcp_bbr *bbr, struct tcp_log_bbr *l, uint32_t cts)
1894 memset(l, 0, sizeof(union tcp_log_stackspecific));
1895 l->cur_del_rate = bbr->r_ctl.rc_bbr_cur_del_rate;
1896 l->delRate = get_filter_value(&bbr->r_ctl.rc_delrate);
1897 l->rttProp = get_filter_value_small(&bbr->r_ctl.rc_rttprop);
1898 l->bw_inuse = bbr_get_bw(bbr);
1899 l->inflight = ctf_flight_size(bbr->rc_tp,
1900 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
1901 l->applimited = bbr->r_ctl.r_app_limited_until;
1902 l->delivered = bbr->r_ctl.rc_delivered;
1904 l->lost = bbr->r_ctl.rc_lost;
1905 l->bbr_state = bbr->rc_bbr_state;
1906 l->bbr_substate = bbr_state_val(bbr);
1907 l->epoch = bbr->r_ctl.rc_rtt_epoch;
1908 l->lt_epoch = bbr->r_ctl.rc_lt_epoch;
1909 l->pacing_gain = bbr->r_ctl.rc_bbr_hptsi_gain;
1910 l->cwnd_gain = bbr->r_ctl.rc_bbr_cwnd_gain;
1911 l->inhpts = bbr->rc_inp->inp_in_hpts;
1912 l->ininput = bbr->rc_inp->inp_in_input;
1913 l->use_lt_bw = bbr->rc_lt_use_bw;
1914 l->pkts_out = bbr->r_ctl.rc_flight_at_input;
1915 l->pkt_epoch = bbr->r_ctl.rc_pkt_epoch;
1919 bbr_log_type_bw_reduce(struct tcp_bbr *bbr, int reason)
1921 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1922 union tcp_log_stackspecific log;
1924 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
1925 log.u_bbr.flex1 = 0;
1926 log.u_bbr.flex2 = 0;
1927 log.u_bbr.flex5 = 0;
1928 log.u_bbr.flex3 = 0;
1929 log.u_bbr.flex4 = bbr->r_ctl.rc_pkt_epoch_loss_rate;
1930 log.u_bbr.flex7 = reason;
1931 log.u_bbr.flex6 = bbr->r_ctl.rc_bbr_enters_probertt;
1932 log.u_bbr.flex8 = 0;
1933 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
1934 &bbr->rc_inp->inp_socket->so_rcv,
1935 &bbr->rc_inp->inp_socket->so_snd,
1936 BBR_LOG_BW_RED_EV, 0,
1937 0, &log, false, &bbr->rc_tv);
1942 bbr_log_type_rwnd_collapse(struct tcp_bbr *bbr, int seq, int mode, uint32_t count)
1944 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1945 union tcp_log_stackspecific log;
1947 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
1948 log.u_bbr.flex1 = seq;
1949 log.u_bbr.flex2 = count;
1950 log.u_bbr.flex8 = mode;
1951 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
1952 &bbr->rc_inp->inp_socket->so_rcv,
1953 &bbr->rc_inp->inp_socket->so_snd,
1955 0, &log, false, &bbr->rc_tv);
1962 bbr_log_type_just_return(struct tcp_bbr *bbr, uint32_t cts, uint32_t tlen, uint8_t hpts_calling,
1963 uint8_t reason, uint32_t p_maxseg, int len)
1965 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1966 union tcp_log_stackspecific log;
1968 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
1969 log.u_bbr.flex1 = p_maxseg;
1970 log.u_bbr.flex2 = bbr->r_ctl.rc_hpts_flags;
1971 log.u_bbr.flex3 = bbr->r_ctl.rc_timer_exp;
1972 log.u_bbr.flex4 = reason;
1973 log.u_bbr.flex5 = bbr->rc_in_persist;
1974 log.u_bbr.flex6 = bbr->r_ctl.rc_last_delay_val;
1975 log.u_bbr.flex7 = p_maxseg;
1976 log.u_bbr.flex8 = bbr->rc_in_persist;
1977 log.u_bbr.pkts_out = 0;
1978 log.u_bbr.applimited = len;
1979 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
1980 &bbr->rc_inp->inp_socket->so_rcv,
1981 &bbr->rc_inp->inp_socket->so_snd,
1983 tlen, &log, false, &bbr->rc_tv);
1989 bbr_log_type_enter_rec(struct tcp_bbr *bbr, uint32_t seq)
1991 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1992 union tcp_log_stackspecific log;
1994 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
1995 log.u_bbr.flex1 = seq;
1996 log.u_bbr.flex2 = bbr->r_ctl.rc_cwnd_on_ent;
1997 log.u_bbr.flex3 = bbr->r_ctl.rc_recovery_start;
1998 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
1999 &bbr->rc_inp->inp_socket->so_rcv,
2000 &bbr->rc_inp->inp_socket->so_snd,
2002 0, &log, false, &bbr->rc_tv);
2007 bbr_log_msgsize_fail(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t len, uint32_t maxseg, uint32_t mtu, int32_t csum_flags, int32_t tso, uint32_t cts)
2009 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
2010 union tcp_log_stackspecific log;
2012 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2013 log.u_bbr.flex1 = tso;
2014 log.u_bbr.flex2 = maxseg;
2015 log.u_bbr.flex3 = mtu;
2016 log.u_bbr.flex4 = csum_flags;
2017 TCP_LOG_EVENTP(tp, NULL,
2018 &bbr->rc_inp->inp_socket->so_rcv,
2019 &bbr->rc_inp->inp_socket->so_snd,
2021 0, &log, false, &bbr->rc_tv);
2026 bbr_log_flowend(struct tcp_bbr *bbr)
2028 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2029 union tcp_log_stackspecific log;
2030 struct sockbuf *r, *s;
2033 if (bbr->rc_inp->inp_socket) {
2034 r = &bbr->rc_inp->inp_socket->so_rcv;
2035 s = &bbr->rc_inp->inp_socket->so_snd;
2039 bbr_fill_in_logging_data(bbr, &log.u_bbr, tcp_get_usecs(&tv));
2040 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2043 0, &log, false, &tv);
2048 bbr_log_pkt_epoch(struct tcp_bbr *bbr, uint32_t cts, uint32_t line,
2049 uint32_t lost, uint32_t del)
2051 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2052 union tcp_log_stackspecific log;
2054 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2055 log.u_bbr.flex1 = lost;
2056 log.u_bbr.flex2 = del;
2057 log.u_bbr.flex3 = bbr->r_ctl.rc_bbr_lastbtlbw;
2058 log.u_bbr.flex4 = bbr->r_ctl.rc_pkt_epoch_rtt;
2059 log.u_bbr.flex5 = bbr->r_ctl.rc_bbr_last_startup_epoch;
2060 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup;
2061 log.u_bbr.flex7 = line;
2062 log.u_bbr.flex8 = 0;
2063 log.u_bbr.inflight = bbr->r_ctl.r_measurement_count;
2064 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2065 &bbr->rc_inp->inp_socket->so_rcv,
2066 &bbr->rc_inp->inp_socket->so_snd,
2067 BBR_LOG_PKT_EPOCH, 0,
2068 0, &log, false, &bbr->rc_tv);
2073 bbr_log_time_epoch(struct tcp_bbr *bbr, uint32_t cts, uint32_t line, uint32_t epoch_time)
2075 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2076 union tcp_log_stackspecific log;
2078 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2079 log.u_bbr.flex1 = bbr->r_ctl.rc_lost;
2080 log.u_bbr.flex2 = bbr->rc_inp->inp_socket->so_snd.sb_lowat;
2081 log.u_bbr.flex3 = bbr->rc_inp->inp_socket->so_snd.sb_hiwat;
2082 log.u_bbr.flex7 = line;
2083 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2084 &bbr->rc_inp->inp_socket->so_rcv,
2085 &bbr->rc_inp->inp_socket->so_snd,
2086 BBR_LOG_TIME_EPOCH, 0,
2087 0, &log, false, &bbr->rc_tv);
2092 bbr_log_set_of_state_target(struct tcp_bbr *bbr, uint32_t new_tar, int line, int meth)
2094 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2095 union tcp_log_stackspecific log;
2097 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2098 log.u_bbr.flex1 = bbr->r_ctl.rc_target_at_state;
2099 log.u_bbr.flex2 = new_tar;
2100 log.u_bbr.flex3 = line;
2101 log.u_bbr.flex4 = bbr->r_ctl.rc_pace_max_segs;
2102 log.u_bbr.flex5 = bbr_quanta;
2103 log.u_bbr.flex6 = bbr->r_ctl.rc_pace_min_segs;
2104 log.u_bbr.flex7 = bbr->rc_last_options;
2105 log.u_bbr.flex8 = meth;
2106 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2107 &bbr->rc_inp->inp_socket->so_rcv,
2108 &bbr->rc_inp->inp_socket->so_snd,
2109 BBR_LOG_STATE_TARGET, 0,
2110 0, &log, false, &bbr->rc_tv);
2116 bbr_log_type_statechange(struct tcp_bbr *bbr, uint32_t cts, int32_t line)
2118 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2119 union tcp_log_stackspecific log;
2121 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2122 log.u_bbr.flex1 = line;
2123 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks;
2124 log.u_bbr.flex3 = bbr->r_ctl.rc_probertt_int;
2125 if (bbr_state_is_pkt_epoch)
2126 log.u_bbr.flex4 = bbr_get_rtt(bbr, BBR_RTT_PKTRTT);
2128 log.u_bbr.flex4 = bbr_get_rtt(bbr, BBR_RTT_PROP);
2129 log.u_bbr.flex5 = bbr->r_ctl.rc_bbr_last_startup_epoch;
2130 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup;
2131 log.u_bbr.flex7 = (bbr->r_ctl.rc_target_at_state/1000);
2132 log.u_bbr.lt_epoch = bbr->r_ctl.rc_level_state_extra;
2133 log.u_bbr.pkts_out = bbr->r_ctl.rc_target_at_state;
2134 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2135 &bbr->rc_inp->inp_socket->so_rcv,
2136 &bbr->rc_inp->inp_socket->so_snd,
2138 0, &log, false, &bbr->rc_tv);
2143 bbr_log_rtt_shrinks(struct tcp_bbr *bbr, uint32_t cts, uint32_t applied,
2144 uint32_t rtt, uint32_t line, uint8_t reas, uint16_t cond)
2146 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2147 union tcp_log_stackspecific log;
2149 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2150 log.u_bbr.flex1 = line;
2151 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks;
2152 log.u_bbr.flex3 = bbr->r_ctl.last_in_probertt;
2153 log.u_bbr.flex4 = applied;
2154 log.u_bbr.flex5 = rtt;
2155 log.u_bbr.flex6 = bbr->r_ctl.rc_target_at_state;
2156 log.u_bbr.flex7 = cond;
2157 log.u_bbr.flex8 = reas;
2158 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2159 &bbr->rc_inp->inp_socket->so_rcv,
2160 &bbr->rc_inp->inp_socket->so_snd,
2161 BBR_LOG_RTT_SHRINKS, 0,
2162 0, &log, false, &bbr->rc_tv);
2167 bbr_log_type_exit_rec(struct tcp_bbr *bbr)
2169 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2170 union tcp_log_stackspecific log;
2172 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2173 log.u_bbr.flex1 = bbr->r_ctl.rc_recovery_start;
2174 log.u_bbr.flex2 = bbr->r_ctl.rc_cwnd_on_ent;
2175 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2176 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2177 &bbr->rc_inp->inp_socket->so_rcv,
2178 &bbr->rc_inp->inp_socket->so_snd,
2180 0, &log, false, &bbr->rc_tv);
2185 bbr_log_type_cwndupd(struct tcp_bbr *bbr, uint32_t bytes_this_ack, uint32_t chg,
2186 uint32_t prev_acked, int32_t meth, uint32_t target, uint32_t th_ack, int32_t line)
2188 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2189 union tcp_log_stackspecific log;
2191 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2192 log.u_bbr.flex1 = line;
2193 log.u_bbr.flex2 = prev_acked;
2194 log.u_bbr.flex3 = bytes_this_ack;
2195 log.u_bbr.flex4 = chg;
2196 log.u_bbr.flex5 = th_ack;
2197 log.u_bbr.flex6 = target;
2198 log.u_bbr.flex8 = meth;
2199 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2200 &bbr->rc_inp->inp_socket->so_rcv,
2201 &bbr->rc_inp->inp_socket->so_snd,
2203 0, &log, false, &bbr->rc_tv);
2208 bbr_log_rtt_sample(struct tcp_bbr *bbr, uint32_t rtt, uint32_t tsin)
2211 * Log the rtt sample we are applying to the srtt algorithm in
2214 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2215 union tcp_log_stackspecific log;
2217 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2218 log.u_bbr.flex1 = rtt;
2219 log.u_bbr.flex2 = bbr->r_ctl.rc_bbr_state_time;
2220 log.u_bbr.flex3 = bbr->r_ctl.rc_ack_hdwr_delay;
2221 log.u_bbr.flex4 = bbr->rc_tp->ts_offset;
2222 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2223 log.u_bbr.pkts_out = tcp_tv_to_mssectick(&bbr->rc_tv);
2224 log.u_bbr.flex6 = tsin;
2225 log.u_bbr.flex7 = 0;
2226 log.u_bbr.flex8 = bbr->rc_ack_was_delayed;
2227 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2228 &bbr->rc_inp->inp_socket->so_rcv,
2229 &bbr->rc_inp->inp_socket->so_snd,
2231 0, &log, false, &bbr->rc_tv);
2236 bbr_log_type_pesist(struct tcp_bbr *bbr, uint32_t cts, uint32_t time_in, int32_t line, uint8_t enter_exit)
2238 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2239 union tcp_log_stackspecific log;
2241 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2242 log.u_bbr.flex1 = time_in;
2243 log.u_bbr.flex2 = line;
2244 log.u_bbr.flex8 = enter_exit;
2245 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2246 &bbr->rc_inp->inp_socket->so_rcv,
2247 &bbr->rc_inp->inp_socket->so_snd,
2249 0, &log, false, &bbr->rc_tv);
2253 bbr_log_ack_clear(struct tcp_bbr *bbr, uint32_t cts)
2255 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2256 union tcp_log_stackspecific log;
2258 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2259 log.u_bbr.flex1 = bbr->rc_tp->ts_recent_age;
2260 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks;
2261 log.u_bbr.flex3 = bbr->r_ctl.rc_probertt_int;
2262 log.u_bbr.flex4 = bbr->r_ctl.rc_went_idle_time;
2263 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2264 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2265 &bbr->rc_inp->inp_socket->so_rcv,
2266 &bbr->rc_inp->inp_socket->so_snd,
2267 BBR_LOG_ACKCLEAR, 0,
2268 0, &log, false, &bbr->rc_tv);
2273 bbr_log_ack_event(struct tcp_bbr *bbr, struct tcphdr *th, struct tcpopt *to, uint32_t tlen,
2274 uint16_t nsegs, uint32_t cts, int32_t nxt_pkt, struct mbuf *m)
2276 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2277 union tcp_log_stackspecific log;
2280 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2281 log.u_bbr.flex1 = nsegs;
2282 log.u_bbr.flex2 = bbr->r_ctl.rc_lost_bytes;
2286 log.u_bbr.flex3 = m->m_flags;
2287 if (m->m_flags & M_TSTMP) {
2288 mbuf_tstmp2timespec(m, &ts);
2289 tv.tv_sec = ts.tv_sec;
2290 tv.tv_usec = ts.tv_nsec / 1000;
2291 log.u_bbr.lt_epoch = tcp_tv_to_usectick(&tv);
2293 log.u_bbr.lt_epoch = 0;
2295 if (m->m_flags & M_TSTMP_LRO) {
2296 tv.tv_sec = m->m_pkthdr.rcv_tstmp / 1000000000;
2297 tv.tv_usec = (m->m_pkthdr.rcv_tstmp % 1000000000) / 1000;
2298 log.u_bbr.flex5 = tcp_tv_to_usectick(&tv);
2300 /* No arrival timestamp */
2301 log.u_bbr.flex5 = 0;
2304 log.u_bbr.pkts_out = tcp_get_usecs(&tv);
2306 log.u_bbr.flex3 = 0;
2307 log.u_bbr.flex5 = 0;
2308 log.u_bbr.flex6 = 0;
2309 log.u_bbr.pkts_out = 0;
2311 log.u_bbr.flex4 = bbr->r_ctl.rc_target_at_state;
2312 log.u_bbr.flex7 = bbr->r_wanted_output;
2313 log.u_bbr.flex8 = bbr->rc_in_persist;
2314 TCP_LOG_EVENTP(bbr->rc_tp, th,
2315 &bbr->rc_inp->inp_socket->so_rcv,
2316 &bbr->rc_inp->inp_socket->so_snd,
2318 tlen, &log, true, &bbr->rc_tv);
2323 bbr_log_doseg_done(struct tcp_bbr *bbr, uint32_t cts, int32_t nxt_pkt, int32_t did_out)
2325 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2326 union tcp_log_stackspecific log;
2328 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2329 log.u_bbr.flex1 = did_out;
2330 log.u_bbr.flex2 = nxt_pkt;
2331 log.u_bbr.flex3 = bbr->r_ctl.rc_last_delay_val;
2332 log.u_bbr.flex4 = bbr->r_ctl.rc_hpts_flags;
2333 log.u_bbr.flex5 = bbr->r_ctl.rc_timer_exp;
2334 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_bytes;
2335 log.u_bbr.flex7 = bbr->r_wanted_output;
2336 log.u_bbr.flex8 = bbr->rc_in_persist;
2337 log.u_bbr.pkts_out = bbr->r_ctl.highest_hdwr_delay;
2338 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2339 &bbr->rc_inp->inp_socket->so_rcv,
2340 &bbr->rc_inp->inp_socket->so_snd,
2341 BBR_LOG_DOSEG_DONE, 0,
2342 0, &log, true, &bbr->rc_tv);
2347 bbr_log_enobuf_jmp(struct tcp_bbr *bbr, uint32_t len, uint32_t cts,
2348 int32_t line, uint32_t o_len, uint32_t segcnt, uint32_t segsiz)
2350 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2351 union tcp_log_stackspecific log;
2353 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2354 log.u_bbr.flex1 = line;
2355 log.u_bbr.flex2 = o_len;
2356 log.u_bbr.flex3 = segcnt;
2357 log.u_bbr.flex4 = segsiz;
2358 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2359 &bbr->rc_inp->inp_socket->so_rcv,
2360 &bbr->rc_inp->inp_socket->so_snd,
2361 BBR_LOG_ENOBUF_JMP, ENOBUFS,
2362 len, &log, true, &bbr->rc_tv);
2367 bbr_log_to_processing(struct tcp_bbr *bbr, uint32_t cts, int32_t ret, int32_t timers, uint8_t hpts_calling)
2369 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2370 union tcp_log_stackspecific log;
2372 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2373 log.u_bbr.flex1 = timers;
2374 log.u_bbr.flex2 = ret;
2375 log.u_bbr.flex3 = bbr->r_ctl.rc_timer_exp;
2376 log.u_bbr.flex4 = bbr->r_ctl.rc_hpts_flags;
2377 log.u_bbr.flex5 = cts;
2378 log.u_bbr.flex6 = bbr->r_ctl.rc_target_at_state;
2379 log.u_bbr.flex8 = hpts_calling;
2380 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2381 &bbr->rc_inp->inp_socket->so_rcv,
2382 &bbr->rc_inp->inp_socket->so_snd,
2383 BBR_LOG_TO_PROCESS, 0,
2384 0, &log, false, &bbr->rc_tv);
2389 bbr_log_to_event(struct tcp_bbr *bbr, uint32_t cts, int32_t to_num)
2391 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2392 union tcp_log_stackspecific log;
2395 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2396 log.u_bbr.flex1 = bbr->bbr_timer_src;
2397 log.u_bbr.flex2 = 0;
2398 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags;
2399 ar = (uint64_t)(bbr->r_ctl.rc_resend);
2401 ar &= 0x00000000ffffffff;
2402 log.u_bbr.flex4 = (uint32_t)ar;
2403 ar = (uint64_t)bbr->r_ctl.rc_resend;
2404 ar &= 0x00000000ffffffff;
2405 log.u_bbr.flex5 = (uint32_t)ar;
2406 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
2407 log.u_bbr.flex8 = to_num;
2408 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2409 &bbr->rc_inp->inp_socket->so_rcv,
2410 &bbr->rc_inp->inp_socket->so_snd,
2412 0, &log, false, &bbr->rc_tv);
2417 bbr_log_startup_event(struct tcp_bbr *bbr, uint32_t cts, uint32_t flex1, uint32_t flex2, uint32_t flex3, uint8_t reason)
2419 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2420 union tcp_log_stackspecific log;
2422 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2423 log.u_bbr.flex1 = flex1;
2424 log.u_bbr.flex2 = flex2;
2425 log.u_bbr.flex3 = flex3;
2426 log.u_bbr.flex4 = 0;
2427 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2428 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup;
2429 log.u_bbr.flex8 = reason;
2430 log.u_bbr.cur_del_rate = bbr->r_ctl.rc_bbr_lastbtlbw;
2431 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2432 &bbr->rc_inp->inp_socket->so_rcv,
2433 &bbr->rc_inp->inp_socket->so_snd,
2435 0, &log, false, &bbr->rc_tv);
2440 bbr_log_hpts_diag(struct tcp_bbr *bbr, uint32_t cts, struct hpts_diag *diag)
2442 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2443 union tcp_log_stackspecific log;
2445 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2446 log.u_bbr.flex1 = diag->p_nxt_slot;
2447 log.u_bbr.flex2 = diag->p_cur_slot;
2448 log.u_bbr.flex3 = diag->slot_req;
2449 log.u_bbr.flex4 = diag->inp_hptsslot;
2450 log.u_bbr.flex5 = diag->slot_remaining;
2451 log.u_bbr.flex6 = diag->need_new_to;
2452 log.u_bbr.flex7 = diag->p_hpts_active;
2453 log.u_bbr.flex8 = diag->p_on_min_sleep;
2454 /* Hijack other fields as needed */
2455 log.u_bbr.epoch = diag->have_slept;
2456 log.u_bbr.lt_epoch = diag->yet_to_sleep;
2457 log.u_bbr.pkts_out = diag->co_ret;
2458 log.u_bbr.applimited = diag->hpts_sleep_time;
2459 log.u_bbr.delivered = diag->p_prev_slot;
2460 log.u_bbr.inflight = diag->p_runningtick;
2461 log.u_bbr.bw_inuse = diag->wheel_tick;
2462 log.u_bbr.rttProp = diag->wheel_cts;
2463 log.u_bbr.delRate = diag->maxticks;
2464 log.u_bbr.cur_del_rate = diag->p_curtick;
2465 log.u_bbr.cur_del_rate <<= 32;
2466 log.u_bbr.cur_del_rate |= diag->p_lasttick;
2467 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2468 &bbr->rc_inp->inp_socket->so_rcv,
2469 &bbr->rc_inp->inp_socket->so_snd,
2470 BBR_LOG_HPTSDIAG, 0,
2471 0, &log, false, &bbr->rc_tv);
2476 bbr_log_timer_var(struct tcp_bbr *bbr, int mode, uint32_t cts, uint32_t time_since_sent, uint32_t srtt,
2477 uint32_t thresh, uint32_t to)
2479 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2480 union tcp_log_stackspecific log;
2482 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2483 log.u_bbr.flex1 = bbr->rc_tp->t_rttvar;
2484 log.u_bbr.flex2 = time_since_sent;
2485 log.u_bbr.flex3 = srtt;
2486 log.u_bbr.flex4 = thresh;
2487 log.u_bbr.flex5 = to;
2488 log.u_bbr.flex6 = bbr->rc_tp->t_srtt;
2489 log.u_bbr.flex8 = mode;
2490 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2491 &bbr->rc_inp->inp_socket->so_rcv,
2492 &bbr->rc_inp->inp_socket->so_snd,
2493 BBR_LOG_TIMERPREP, 0,
2494 0, &log, false, &bbr->rc_tv);
2499 bbr_log_pacing_delay_calc(struct tcp_bbr *bbr, uint16_t gain, uint32_t len,
2500 uint32_t cts, uint32_t usecs, uint64_t bw, uint32_t override, int mod)
2502 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2503 union tcp_log_stackspecific log;
2505 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2506 log.u_bbr.flex1 = usecs;
2507 log.u_bbr.flex2 = len;
2508 log.u_bbr.flex3 = (uint32_t)((bw >> 32) & 0x00000000ffffffff);
2509 log.u_bbr.flex4 = (uint32_t)(bw & 0x00000000ffffffff);
2511 log.u_bbr.flex5 = (1 << 2);
2513 log.u_bbr.flex5 = 0;
2514 log.u_bbr.flex6 = override;
2515 log.u_bbr.flex7 = gain;
2516 log.u_bbr.flex8 = mod;
2517 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2518 &bbr->rc_inp->inp_socket->so_rcv,
2519 &bbr->rc_inp->inp_socket->so_snd,
2520 BBR_LOG_HPTSI_CALC, 0,
2521 len, &log, false, &bbr->rc_tv);
2526 bbr_log_to_start(struct tcp_bbr *bbr, uint32_t cts, uint32_t to, int32_t slot, uint8_t which)
2528 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2529 union tcp_log_stackspecific log;
2531 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2533 log.u_bbr.flex1 = bbr->bbr_timer_src;
2534 log.u_bbr.flex2 = to;
2535 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags;
2536 log.u_bbr.flex4 = slot;
2537 log.u_bbr.flex5 = bbr->rc_inp->inp_hptsslot;
2538 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
2539 log.u_bbr.pkts_out = bbr->rc_inp->inp_flags2;
2540 log.u_bbr.flex8 = which;
2541 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2542 &bbr->rc_inp->inp_socket->so_rcv,
2543 &bbr->rc_inp->inp_socket->so_snd,
2544 BBR_LOG_TIMERSTAR, 0,
2545 0, &log, false, &bbr->rc_tv);
2550 bbr_log_thresh_choice(struct tcp_bbr *bbr, uint32_t cts, uint32_t thresh, uint32_t lro, uint32_t srtt, struct bbr_sendmap *rsm, uint8_t frm)
2552 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2553 union tcp_log_stackspecific log;
2555 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2556 log.u_bbr.flex1 = thresh;
2557 log.u_bbr.flex2 = lro;
2558 log.u_bbr.flex3 = bbr->r_ctl.rc_reorder_ts;
2559 log.u_bbr.flex4 = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
2560 log.u_bbr.flex5 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
2561 log.u_bbr.flex6 = srtt;
2562 log.u_bbr.flex7 = bbr->r_ctl.rc_reorder_shift;
2563 log.u_bbr.flex8 = frm;
2564 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2565 &bbr->rc_inp->inp_socket->so_rcv,
2566 &bbr->rc_inp->inp_socket->so_snd,
2567 BBR_LOG_THRESH_CALC, 0,
2568 0, &log, false, &bbr->rc_tv);
2573 bbr_log_to_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts, uint8_t hpts_removed)
2575 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2576 union tcp_log_stackspecific log;
2578 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2579 log.u_bbr.flex1 = line;
2580 log.u_bbr.flex2 = bbr->bbr_timer_src;
2581 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags;
2582 log.u_bbr.flex4 = bbr->rc_in_persist;
2583 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2584 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
2585 log.u_bbr.flex8 = hpts_removed;
2586 log.u_bbr.pkts_out = bbr->rc_pacer_started;
2587 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2588 &bbr->rc_inp->inp_socket->so_rcv,
2589 &bbr->rc_inp->inp_socket->so_snd,
2590 BBR_LOG_TIMERCANC, 0,
2591 0, &log, false, &bbr->rc_tv);
2597 bbr_log_tstmp_validation(struct tcp_bbr *bbr, uint64_t peer_delta, uint64_t delta)
2599 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2600 union tcp_log_stackspecific log;
2602 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2603 log.u_bbr.flex1 = bbr->r_ctl.bbr_peer_tsratio;
2604 log.u_bbr.flex2 = (peer_delta >> 32);
2605 log.u_bbr.flex3 = (peer_delta & 0x00000000ffffffff);
2606 log.u_bbr.flex4 = (delta >> 32);
2607 log.u_bbr.flex5 = (delta & 0x00000000ffffffff);
2608 log.u_bbr.flex7 = bbr->rc_ts_clock_set;
2609 log.u_bbr.flex8 = bbr->rc_ts_cant_be_used;
2610 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2611 &bbr->rc_inp->inp_socket->so_rcv,
2612 &bbr->rc_inp->inp_socket->so_snd,
2613 BBR_LOG_TSTMP_VAL, 0,
2614 0, &log, false, &bbr->rc_tv);
2620 bbr_log_type_tsosize(struct tcp_bbr *bbr, uint32_t cts, uint32_t tsosz, uint32_t tls, uint32_t old_val, uint32_t maxseg, int hdwr)
2622 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2623 union tcp_log_stackspecific log;
2625 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2626 log.u_bbr.flex1 = tsosz;
2627 log.u_bbr.flex2 = tls;
2628 log.u_bbr.flex3 = tcp_min_hptsi_time;
2629 log.u_bbr.flex4 = bbr->r_ctl.bbr_hptsi_bytes_min;
2630 log.u_bbr.flex5 = old_val;
2631 log.u_bbr.flex6 = maxseg;
2632 log.u_bbr.flex7 = bbr->rc_no_pacing;
2633 log.u_bbr.flex7 <<= 1;
2634 log.u_bbr.flex7 |= bbr->rc_past_init_win;
2636 log.u_bbr.flex8 = 0x80 | bbr->rc_use_google;
2638 log.u_bbr.flex8 = bbr->rc_use_google;
2639 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2640 &bbr->rc_inp->inp_socket->so_rcv,
2641 &bbr->rc_inp->inp_socket->so_snd,
2643 0, &log, false, &bbr->rc_tv);
2648 bbr_log_type_rsmclear(struct tcp_bbr *bbr, uint32_t cts, struct bbr_sendmap *rsm,
2649 uint32_t flags, uint32_t line)
2651 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2652 union tcp_log_stackspecific log;
2654 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2655 log.u_bbr.flex1 = line;
2656 log.u_bbr.flex2 = rsm->r_start;
2657 log.u_bbr.flex3 = rsm->r_end;
2658 log.u_bbr.flex4 = rsm->r_delivered;
2659 log.u_bbr.flex5 = rsm->r_rtr_cnt;
2660 log.u_bbr.flex6 = rsm->r_dupack;
2661 log.u_bbr.flex7 = rsm->r_tim_lastsent[0];
2662 log.u_bbr.flex8 = rsm->r_flags;
2663 /* Hijack the pkts_out fids */
2664 log.u_bbr.applimited = flags;
2665 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2666 &bbr->rc_inp->inp_socket->so_rcv,
2667 &bbr->rc_inp->inp_socket->so_snd,
2669 0, &log, false, &bbr->rc_tv);
2674 bbr_log_type_bbrupd(struct tcp_bbr *bbr, uint8_t flex8, uint32_t cts,
2675 uint32_t flex3, uint32_t flex2, uint32_t flex5,
2676 uint32_t flex6, uint32_t pkts_out, int flex7,
2677 uint32_t flex4, uint32_t flex1)
2680 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2681 union tcp_log_stackspecific log;
2683 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2684 log.u_bbr.flex1 = flex1;
2685 log.u_bbr.flex2 = flex2;
2686 log.u_bbr.flex3 = flex3;
2687 log.u_bbr.flex4 = flex4;
2688 log.u_bbr.flex5 = flex5;
2689 log.u_bbr.flex6 = flex6;
2690 log.u_bbr.flex7 = flex7;
2691 /* Hijack the pkts_out fids */
2692 log.u_bbr.pkts_out = pkts_out;
2693 log.u_bbr.flex8 = flex8;
2694 if (bbr->rc_ack_was_delayed)
2695 log.u_bbr.epoch = bbr->r_ctl.rc_ack_hdwr_delay;
2697 log.u_bbr.epoch = 0;
2698 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2699 &bbr->rc_inp->inp_socket->so_rcv,
2700 &bbr->rc_inp->inp_socket->so_snd,
2702 flex2, &log, false, &bbr->rc_tv);
2708 bbr_log_type_ltbw(struct tcp_bbr *bbr, uint32_t cts, int32_t reason,
2709 uint32_t newbw, uint32_t obw, uint32_t diff,
2712 if (/*bbr_verbose_logging && */(bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2713 union tcp_log_stackspecific log;
2715 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2716 log.u_bbr.flex1 = reason;
2717 log.u_bbr.flex2 = newbw;
2718 log.u_bbr.flex3 = obw;
2719 log.u_bbr.flex4 = diff;
2720 log.u_bbr.flex5 = bbr->r_ctl.rc_lt_lost;
2721 log.u_bbr.flex6 = bbr->r_ctl.rc_lt_del;
2722 log.u_bbr.flex7 = bbr->rc_lt_is_sampling;
2723 log.u_bbr.pkts_out = tim;
2724 log.u_bbr.bw_inuse = bbr->r_ctl.rc_lt_bw;
2725 if (bbr->rc_lt_use_bw == 0)
2726 log.u_bbr.epoch = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch;
2728 log.u_bbr.epoch = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch_use;
2729 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2730 &bbr->rc_inp->inp_socket->so_rcv,
2731 &bbr->rc_inp->inp_socket->so_snd,
2733 0, &log, false, &bbr->rc_tv);
2738 bbr_log_progress_event(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t tick, int event, int line)
2740 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2741 union tcp_log_stackspecific log;
2743 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2744 log.u_bbr.flex1 = line;
2745 log.u_bbr.flex2 = tick;
2746 log.u_bbr.flex3 = tp->t_maxunacktime;
2747 log.u_bbr.flex4 = tp->t_acktime;
2748 log.u_bbr.flex8 = event;
2749 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2750 &bbr->rc_inp->inp_socket->so_rcv,
2751 &bbr->rc_inp->inp_socket->so_snd,
2752 BBR_LOG_PROGRESS, 0,
2753 0, &log, false, &bbr->rc_tv);
2758 bbr_type_log_hdwr_pacing(struct tcp_bbr *bbr, const struct ifnet *ifp,
2759 uint64_t rate, uint64_t hw_rate, int line, uint32_t cts,
2762 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2763 union tcp_log_stackspecific log;
2765 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2766 log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff);
2767 log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff);
2768 log.u_bbr.flex3 = (((uint64_t)ifp >> 32) & 0x00000000ffffffff);
2769 log.u_bbr.flex4 = ((uint64_t)ifp & 0x00000000ffffffff);
2770 log.u_bbr.bw_inuse = rate;
2771 log.u_bbr.flex5 = line;
2772 log.u_bbr.flex6 = error;
2773 log.u_bbr.flex8 = bbr->skip_gain;
2774 log.u_bbr.flex8 <<= 1;
2775 log.u_bbr.flex8 |= bbr->gain_is_limited;
2776 log.u_bbr.flex8 <<= 1;
2777 log.u_bbr.flex8 |= bbr->bbr_hdrw_pacing;
2778 log.u_bbr.pkts_out = bbr->rc_tp->t_maxseg;
2779 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2780 &bbr->rc_inp->inp_socket->so_rcv,
2781 &bbr->rc_inp->inp_socket->so_snd,
2782 BBR_LOG_HDWR_PACE, 0,
2783 0, &log, false, &bbr->rc_tv);
2788 bbr_log_type_bbrsnd(struct tcp_bbr *bbr, uint32_t len, uint32_t slot, uint32_t del_by, uint32_t cts, uint32_t line, uint32_t prev_delay)
2790 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2791 union tcp_log_stackspecific log;
2793 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2794 log.u_bbr.flex1 = slot;
2795 log.u_bbr.flex2 = del_by;
2796 log.u_bbr.flex3 = prev_delay;
2797 log.u_bbr.flex4 = line;
2798 log.u_bbr.flex5 = bbr->r_ctl.rc_last_delay_val;
2799 log.u_bbr.flex6 = bbr->r_ctl.rc_hptsi_agg_delay;
2800 log.u_bbr.flex7 = (0x0000ffff & bbr->r_ctl.rc_hpts_flags);
2801 log.u_bbr.flex8 = bbr->rc_in_persist;
2802 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2803 &bbr->rc_inp->inp_socket->so_rcv,
2804 &bbr->rc_inp->inp_socket->so_snd,
2806 len, &log, false, &bbr->rc_tv);
2811 bbr_log_type_bbrrttprop(struct tcp_bbr *bbr, uint32_t t, uint32_t end, uint32_t tsconv, uint32_t cts, int32_t match, uint32_t seq, uint8_t flags)
2813 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2814 union tcp_log_stackspecific log;
2816 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2817 log.u_bbr.flex1 = bbr->r_ctl.rc_delivered;
2818 log.u_bbr.flex2 = 0;
2819 log.u_bbr.flex3 = bbr->r_ctl.rc_lowest_rtt;
2820 log.u_bbr.flex4 = end;
2821 log.u_bbr.flex5 = seq;
2822 log.u_bbr.flex6 = t;
2823 log.u_bbr.flex7 = match;
2824 log.u_bbr.flex8 = flags;
2825 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2826 &bbr->rc_inp->inp_socket->so_rcv,
2827 &bbr->rc_inp->inp_socket->so_snd,
2829 0, &log, false, &bbr->rc_tv);
2834 bbr_log_exit_gain(struct tcp_bbr *bbr, uint32_t cts, int32_t entry_method)
2836 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2837 union tcp_log_stackspecific log;
2839 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2840 log.u_bbr.flex1 = bbr->r_ctl.rc_target_at_state;
2841 log.u_bbr.flex2 = (bbr->rc_tp->t_maxseg - bbr->rc_last_options);
2842 log.u_bbr.flex3 = bbr->r_ctl.gain_epoch;
2843 log.u_bbr.flex4 = bbr->r_ctl.rc_pace_max_segs;
2844 log.u_bbr.flex5 = bbr->r_ctl.rc_pace_min_segs;
2845 log.u_bbr.flex6 = bbr->r_ctl.rc_bbr_state_atflight;
2846 log.u_bbr.flex7 = 0;
2847 log.u_bbr.flex8 = entry_method;
2848 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2849 &bbr->rc_inp->inp_socket->so_rcv,
2850 &bbr->rc_inp->inp_socket->so_snd,
2851 BBR_LOG_EXIT_GAIN, 0,
2852 0, &log, false, &bbr->rc_tv);
2857 bbr_log_settings_change(struct tcp_bbr *bbr, int settings_desired)
2859 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2860 union tcp_log_stackspecific log;
2862 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2864 log.u_bbr.flex1 = 0;
2865 log.u_bbr.flex2 = 0;
2866 log.u_bbr.flex3 = 0;
2867 log.u_bbr.flex4 = 0;
2868 log.u_bbr.flex7 = 0;
2869 log.u_bbr.flex8 = settings_desired;
2871 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2872 &bbr->rc_inp->inp_socket->so_rcv,
2873 &bbr->rc_inp->inp_socket->so_snd,
2874 BBR_LOG_SETTINGS_CHG, 0,
2875 0, &log, false, &bbr->rc_tv);
2880 * Returns the bw from the our filter.
2882 static inline uint64_t
2883 bbr_get_full_bw(struct tcp_bbr *bbr)
2887 bw = get_filter_value(&bbr->r_ctl.rc_delrate);
2893 bbr_set_pktepoch(struct tcp_bbr *bbr, uint32_t cts, int32_t line)
2898 if (bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_pktepoch)
2899 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lost_at_pktepoch;
2902 del = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_pkt_epoch_del;
2907 calclr *= (uint64_t)1000;
2908 calclr /= (uint64_t)del;
2910 /* Nothing delivered? 100.0% loss */
2913 bbr->r_ctl.rc_pkt_epoch_loss_rate = (uint32_t)calclr;
2914 if (IN_RECOVERY(bbr->rc_tp->t_flags))
2915 bbr->r_ctl.recovery_lr += (uint32_t)calclr;
2916 bbr->r_ctl.rc_pkt_epoch++;
2917 if (bbr->rc_no_pacing &&
2918 (bbr->r_ctl.rc_pkt_epoch >= bbr->no_pacing_until)) {
2919 bbr->rc_no_pacing = 0;
2920 tcp_bbr_tso_size_check(bbr, cts);
2922 bbr->r_ctl.rc_pkt_epoch_rtt = bbr_calc_time(cts, bbr->r_ctl.rc_pkt_epoch_time);
2923 bbr->r_ctl.rc_pkt_epoch_time = cts;
2924 /* What was our loss rate */
2925 bbr_log_pkt_epoch(bbr, cts, line, lost, del);
2926 bbr->r_ctl.rc_pkt_epoch_del = bbr->r_ctl.rc_delivered;
2927 bbr->r_ctl.rc_lost_at_pktepoch = bbr->r_ctl.rc_lost;
2931 bbr_set_epoch(struct tcp_bbr *bbr, uint32_t cts, int32_t line)
2933 uint32_t epoch_time;
2935 /* Tick the RTT clock */
2936 bbr->r_ctl.rc_rtt_epoch++;
2937 epoch_time = cts - bbr->r_ctl.rc_rcv_epoch_start;
2938 bbr_log_time_epoch(bbr, cts, line, epoch_time);
2939 bbr->r_ctl.rc_rcv_epoch_start = cts;
2944 bbr_isit_a_pkt_epoch(struct tcp_bbr *bbr, uint32_t cts, struct bbr_sendmap *rsm, int32_t line, int32_t cum_acked)
2946 if (SEQ_GEQ(rsm->r_delivered, bbr->r_ctl.rc_pkt_epoch_del)) {
2947 bbr->rc_is_pkt_epoch_now = 1;
2952 * Returns the bw from either the b/w filter
2953 * or from the lt_bw (if the connection is being
2956 static inline uint64_t
2957 __bbr_get_bw(struct tcp_bbr *bbr)
2959 uint64_t bw, min_bw;
2961 int gm_measure_cnt = 1;
2964 * For startup we make, like google, a
2965 * minimum b/w. This is generated from the
2966 * IW and the rttProp. We do fall back to srtt
2967 * if for some reason (initial handshake) we don't
2968 * have a rttProp. We, in the worst case, fall back
2969 * to the configured min_bw (rc_initial_hptsi_bw).
2971 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) {
2972 /* Attempt first to use rttProp */
2973 rtt = (uint64_t)get_filter_value_small(&bbr->r_ctl.rc_rttprop);
2974 if (rtt && (rtt < 0xffffffff)) {
2976 min_bw = (uint64_t)(bbr_initial_cwnd(bbr, bbr->rc_tp)) *
2977 ((uint64_t)1000000);
2979 if (min_bw < bbr->r_ctl.rc_initial_hptsi_bw) {
2980 min_bw = bbr->r_ctl.rc_initial_hptsi_bw;
2983 } else if (bbr->rc_tp->t_srtt != 0) {
2984 /* No rttProp, use srtt? */
2985 rtt = bbr_get_rtt(bbr, BBR_SRTT);
2988 min_bw = bbr->r_ctl.rc_initial_hptsi_bw;
2993 if ((bbr->rc_past_init_win == 0) &&
2994 (bbr->r_ctl.rc_delivered > bbr_initial_cwnd(bbr, bbr->rc_tp)))
2995 bbr->rc_past_init_win = 1;
2996 if ((bbr->rc_use_google) && (bbr->r_ctl.r_measurement_count >= 1))
2998 if (gm_measure_cnt &&
2999 ((bbr->r_ctl.r_measurement_count < bbr_min_measurements_req) ||
3000 (bbr->rc_past_init_win == 0))) {
3001 /* For google we use our guess rate until we get 1 measurement */
3004 rtt = (uint64_t)get_filter_value_small(&bbr->r_ctl.rc_rttprop);
3005 if (rtt && (rtt < 0xffffffff)) {
3007 * We have an RTT measurment. Use that in
3008 * combination with our initial window to calculate
3011 bw = (uint64_t)(bbr_initial_cwnd(bbr, bbr->rc_tp)) *
3012 ((uint64_t)1000000);
3014 if (bw < bbr->r_ctl.rc_initial_hptsi_bw) {
3015 bw = bbr->r_ctl.rc_initial_hptsi_bw;
3018 /* Drop back to the 40 and punt to a default */
3019 bw = bbr->r_ctl.rc_initial_hptsi_bw;
3022 /* Probably should panic */
3029 if (bbr->rc_lt_use_bw)
3030 bw = bbr->r_ctl.rc_lt_bw;
3031 else if (bbr->r_recovery_bw && (bbr->rc_use_google == 0))
3032 bw = bbr->r_ctl.red_bw;
3034 bw = get_filter_value(&bbr->r_ctl.rc_delrate);
3035 if (bbr->rc_tp->t_peakrate_thr && (bbr->rc_use_google == 0)) {
3037 * Enforce user set rate limit, keep in mind that
3038 * t_peakrate_thr is in B/s already
3040 bw = uqmin((uint64_t)bbr->rc_tp->t_peakrate_thr, bw);
3043 /* We should not be at 0, go to the initial window then */
3044 goto use_initial_window;
3047 /* Probably should panic */
3054 static inline uint64_t
3055 bbr_get_bw(struct tcp_bbr *bbr)
3059 bw = __bbr_get_bw(bbr);
3064 bbr_reset_lt_bw_interval(struct tcp_bbr *bbr, uint32_t cts)
3066 bbr->r_ctl.rc_lt_epoch = bbr->r_ctl.rc_pkt_epoch;
3067 bbr->r_ctl.rc_lt_time = bbr->r_ctl.rc_del_time;
3068 bbr->r_ctl.rc_lt_del = bbr->r_ctl.rc_delivered;
3069 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
3073 bbr_reset_lt_bw_sampling(struct tcp_bbr *bbr, uint32_t cts)
3075 bbr->rc_lt_is_sampling = 0;
3076 bbr->rc_lt_use_bw = 0;
3077 bbr->r_ctl.rc_lt_bw = 0;
3078 bbr_reset_lt_bw_interval(bbr, cts);
3082 bbr_lt_bw_samp_done(struct tcp_bbr *bbr, uint64_t bw, uint32_t cts, uint32_t timin)
3086 /* Do we have a previous sample? */
3087 if (bbr->r_ctl.rc_lt_bw) {
3088 /* Get the diff in bytes per second */
3089 if (bbr->r_ctl.rc_lt_bw > bw)
3090 diff = bbr->r_ctl.rc_lt_bw - bw;
3092 diff = bw - bbr->r_ctl.rc_lt_bw;
3093 if ((diff <= bbr_lt_bw_diff) ||
3094 (diff <= (bbr->r_ctl.rc_lt_bw / bbr_lt_bw_ratio))) {
3095 /* Consider us policed */
3098 saved_bw = (uint32_t)bbr->r_ctl.rc_lt_bw;
3099 bbr->r_ctl.rc_lt_bw = (bw + bbr->r_ctl.rc_lt_bw) / 2; /* average of two */
3100 bbr->rc_lt_use_bw = 1;
3101 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
3103 * Use pkt based epoch for measuring length of
3106 bbr->r_ctl.rc_lt_epoch_use = bbr->r_ctl.rc_pkt_epoch;
3108 * reason 4 is we need to start consider being
3111 bbr_log_type_ltbw(bbr, cts, 4, (uint32_t)bw, saved_bw, (uint32_t)diff, timin);
3115 bbr->r_ctl.rc_lt_bw = bw;
3116 bbr_reset_lt_bw_interval(bbr, cts);
3117 bbr_log_type_ltbw(bbr, cts, 5, 0, (uint32_t)bw, 0, timin);
3121 * RRS: Copied from user space!
3122 * Calculate a uniformly distributed random number less than upper_bound
3123 * avoiding "modulo bias".
3125 * Uniformity is achieved by generating new random numbers until the one
3126 * returned is outside the range [0, 2**32 % upper_bound). This
3127 * guarantees the selected random number will be inside
3128 * [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound)
3129 * after reduction modulo upper_bound.
3132 arc4random_uniform(uint32_t upper_bound)
3136 if (upper_bound < 2)
3139 /* 2**32 % x == (2**32 - x) % x */
3140 min = -upper_bound % upper_bound;
3143 * This could theoretically loop forever but each retry has
3144 * p > 0.5 (worst case, usually far better) of selecting a
3145 * number inside the range we need, so it should rarely need
3154 return r % upper_bound;
3158 bbr_randomize_extra_state_time(struct tcp_bbr *bbr)
3160 uint32_t ran, deduct;
3162 ran = arc4random_uniform(bbr_rand_ot);
3164 deduct = bbr->r_ctl.rc_level_state_extra / ran;
3165 bbr->r_ctl.rc_level_state_extra -= deduct;
3169 * Return randomly the starting state
3170 * to use in probebw.
3173 bbr_pick_probebw_substate(struct tcp_bbr *bbr, uint32_t cts)
3178 /* Initialize the offset to 0 */
3179 bbr->r_ctl.rc_exta_time_gd = 0;
3180 bbr->rc_hit_state_1 = 0;
3181 bbr->r_ctl.rc_level_state_extra = 0;
3182 ran = arc4random_uniform((BBR_SUBSTATE_COUNT-1));
3184 * The math works funny here :) the return value is used to set the
3185 * substate and then the state change is called which increments by
3186 * one. So if we return 1 (DRAIN) we will increment to 2 (LEVEL1) when
3187 * we fully enter the state. Note that the (8 - 1 - ran) assures that
3188 * we return 1 - 7, so we dont return 0 and end up starting in
3191 ret_val = BBR_SUBSTATE_COUNT - 1 - ran;
3193 if ((cts - bbr->r_ctl.rc_rcv_epoch_start) >= bbr_get_rtt(bbr, BBR_RTT_PROP))
3194 bbr_set_epoch(bbr, cts, __LINE__);
3196 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
3201 bbr_lt_bw_sampling(struct tcp_bbr *bbr, uint32_t cts, int32_t loss_detected)
3203 uint32_t diff, d_time;
3204 uint64_t del_time, bw, lost, delivered;
3206 if (bbr->r_use_policer == 0)
3208 if (bbr->rc_lt_use_bw) {
3209 /* We are using lt bw do we stop yet? */
3210 diff = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch_use;
3211 if (diff > bbr_lt_bw_max_rtts) {
3214 bbr_reset_lt_bw_sampling(bbr, cts);
3215 if (bbr->rc_filled_pipe) {
3216 bbr_set_epoch(bbr, cts, __LINE__);
3217 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts);
3218 bbr_substate_change(bbr, cts, __LINE__, 0);
3219 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
3220 bbr_log_type_statechange(bbr, cts, __LINE__);
3223 * This should not happen really
3224 * unless we remove the startup/drain
3225 * restrictions above.
3227 bbr->rc_bbr_state = BBR_STATE_STARTUP;
3228 bbr_set_epoch(bbr, cts, __LINE__);
3229 bbr->r_ctl.rc_bbr_state_time = cts;
3230 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
3231 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg;
3232 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg;
3233 bbr_set_state_target(bbr, __LINE__);
3234 bbr_log_type_statechange(bbr, cts, __LINE__);
3236 /* reason 0 is to stop using lt-bw */
3237 bbr_log_type_ltbw(bbr, cts, 0, 0, 0, 0, 0);
3240 if (bbr_lt_intvl_fp == 0) {
3241 /* Not doing false-postive detection */
3244 /* False positive detection */
3245 if (diff == bbr_lt_intvl_fp) {
3246 /* At bbr_lt_intvl_fp we record the lost */
3247 bbr->r_ctl.rc_lt_del = bbr->r_ctl.rc_delivered;
3248 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
3249 } else if (diff > (bbr_lt_intvl_min_rtts + bbr_lt_intvl_fp)) {
3250 /* Now is our loss rate still high? */
3251 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lt_lost;
3252 delivered = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_lt_del;
3253 if ((delivered == 0) ||
3254 (((lost * 1000)/delivered) < bbr_lt_fd_thresh)) {
3255 /* No still below our threshold */
3256 bbr_log_type_ltbw(bbr, cts, 7, lost, delivered, 0, 0);
3258 /* Yikes its still high, it must be a false positive */
3259 bbr_log_type_ltbw(bbr, cts, 8, lost, delivered, 0, 0);
3266 * Wait for the first loss before sampling, to let the policer
3267 * exhaust its tokens and estimate the steady-state rate allowed by
3268 * the policer. Starting samples earlier includes bursts that
3269 * over-estimate the bw.
3271 if (bbr->rc_lt_is_sampling == 0) {
3272 /* reason 1 is to begin doing the sampling */
3273 if (loss_detected == 0)
3275 bbr_reset_lt_bw_interval(bbr, cts);
3276 bbr->rc_lt_is_sampling = 1;
3277 bbr_log_type_ltbw(bbr, cts, 1, 0, 0, 0, 0);
3280 /* Now how long were we delivering long term last> */
3281 if (TSTMP_GEQ(bbr->r_ctl.rc_del_time, bbr->r_ctl.rc_lt_time))
3282 d_time = bbr->r_ctl.rc_del_time - bbr->r_ctl.rc_lt_time;
3286 /* To avoid underestimates, reset sampling if we run out of data. */
3287 if (bbr->r_ctl.r_app_limited_until) {
3288 /* Can not measure in app-limited state */
3289 bbr_reset_lt_bw_sampling(bbr, cts);
3290 /* reason 2 is to reset sampling due to app limits */
3291 bbr_log_type_ltbw(bbr, cts, 2, 0, 0, 0, d_time);
3294 diff = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch;
3295 if (diff < bbr_lt_intvl_min_rtts) {
3297 * need more samples (we don't
3298 * start on a round like linux so
3301 /* 6 is not_enough time or no-loss */
3302 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time);
3305 if (diff > (4 * bbr_lt_intvl_min_rtts)) {
3307 * For now if we wait too long, reset all sampling. We need
3308 * to do some research here, its possible that we should
3309 * base this on how much loss as occurred.. something like
3310 * if its under 10% (or some thresh) reset all otherwise
3311 * don't. Thats for phase II I guess.
3313 bbr_reset_lt_bw_sampling(bbr, cts);
3314 /* reason 3 is to reset sampling due too long of sampling */
3315 bbr_log_type_ltbw(bbr, cts, 3, 0, 0, 0, d_time);
3319 * End sampling interval when a packet is lost, so we estimate the
3320 * policer tokens were exhausted. Stopping the sampling before the
3321 * tokens are exhausted under-estimates the policed rate.
3323 if (loss_detected == 0) {
3324 /* 6 is not_enough time or no-loss */
3325 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time);
3328 /* Calculate packets lost and delivered in sampling interval. */
3329 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lt_lost;
3330 delivered = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_lt_del;
3331 if ((delivered == 0) ||
3332 (((lost * 1000)/delivered) < bbr_lt_loss_thresh)) {
3333 bbr_log_type_ltbw(bbr, cts, 6, lost, delivered, 0, d_time);
3336 if (d_time < 1000) {
3337 /* Not enough time. wait */
3338 /* 6 is not_enough time or no-loss */
3339 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time);
3342 if (d_time >= (0xffffffff / USECS_IN_MSEC)) {
3344 bbr_reset_lt_bw_sampling(bbr, cts);
3345 /* reason 3 is to reset sampling due too long of sampling */
3346 bbr_log_type_ltbw(bbr, cts, 3, 0, 0, 0, d_time);
3351 bw *= (uint64_t)USECS_IN_SECOND;
3353 bbr_lt_bw_samp_done(bbr, bw, cts, d_time);
3357 * Allocate a sendmap from our zone.
3359 static struct bbr_sendmap *
3360 bbr_alloc(struct tcp_bbr *bbr)
3362 struct bbr_sendmap *rsm;
3364 BBR_STAT_INC(bbr_to_alloc);
3365 rsm = uma_zalloc(bbr_zone, (M_NOWAIT | M_ZERO));
3367 bbr->r_ctl.rc_num_maps_alloced++;
3370 if (bbr->r_ctl.rc_free_cnt) {
3371 BBR_STAT_INC(bbr_to_alloc_emerg);
3372 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free);
3373 TAILQ_REMOVE(&bbr->r_ctl.rc_free, rsm, r_next);
3374 bbr->r_ctl.rc_free_cnt--;
3377 BBR_STAT_INC(bbr_to_alloc_failed);
3381 static struct bbr_sendmap *
3382 bbr_alloc_full_limit(struct tcp_bbr *bbr)
3384 if ((V_tcp_map_entries_limit > 0) &&
3385 (bbr->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
3386 BBR_STAT_INC(bbr_alloc_limited);
3387 if (!bbr->alloc_limit_reported) {
3388 bbr->alloc_limit_reported = 1;
3389 BBR_STAT_INC(bbr_alloc_limited_conns);
3393 return (bbr_alloc(bbr));
3397 /* wrapper to allocate a sendmap entry, subject to a specific limit */
3398 static struct bbr_sendmap *
3399 bbr_alloc_limit(struct tcp_bbr *bbr, uint8_t limit_type)
3401 struct bbr_sendmap *rsm;
3404 /* currently there is only one limit type */
3405 if (V_tcp_map_split_limit > 0 &&
3406 bbr->r_ctl.rc_num_split_allocs >= V_tcp_map_split_limit) {
3407 BBR_STAT_INC(bbr_split_limited);
3408 if (!bbr->alloc_limit_reported) {
3409 bbr->alloc_limit_reported = 1;
3410 BBR_STAT_INC(bbr_alloc_limited_conns);
3416 /* allocate and mark in the limit type, if set */
3417 rsm = bbr_alloc(bbr);
3418 if (rsm != NULL && limit_type) {
3419 rsm->r_limit_type = limit_type;
3420 bbr->r_ctl.rc_num_split_allocs++;
3426 bbr_free(struct tcp_bbr *bbr, struct bbr_sendmap *rsm)
3428 if (rsm->r_limit_type) {
3429 /* currently there is only one limit type */
3430 bbr->r_ctl.rc_num_split_allocs--;
3432 if (rsm->r_is_smallmap)
3433 bbr->r_ctl.rc_num_small_maps_alloced--;
3434 if (bbr->r_ctl.rc_tlp_send == rsm)
3435 bbr->r_ctl.rc_tlp_send = NULL;
3436 if (bbr->r_ctl.rc_resend == rsm) {
3437 bbr->r_ctl.rc_resend = NULL;
3439 if (bbr->r_ctl.rc_next == rsm)
3440 bbr->r_ctl.rc_next = NULL;
3441 if (bbr->r_ctl.rc_sacklast == rsm)
3442 bbr->r_ctl.rc_sacklast = NULL;
3443 if (bbr->r_ctl.rc_free_cnt < bbr_min_req_free) {
3444 memset(rsm, 0, sizeof(struct bbr_sendmap));
3445 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_free, rsm, r_next);
3446 rsm->r_limit_type = 0;
3447 bbr->r_ctl.rc_free_cnt++;
3450 bbr->r_ctl.rc_num_maps_alloced--;
3451 uma_zfree(bbr_zone, rsm);
3458 bbr_get_bw_delay_prod(uint64_t rtt, uint64_t bw) {
3460 * Calculate the bytes in flight needed given the bw (in bytes per
3461 * second) and the specifyed rtt in useconds. We need to put out the
3462 * returned value per RTT to match that rate. Gain will normaly
3463 * raise it up from there.
3465 * This should not overflow as long as the bandwidth is below 1
3466 * TByte per second (bw < 10**12 = 2**40) and the rtt is smaller
3467 * than 1000 seconds (rtt < 10**3 * 10**6 = 10**9 = 2**30).
3469 uint64_t usec_per_sec;
3471 usec_per_sec = USECS_IN_SECOND;
3472 return ((rtt * bw) / usec_per_sec);
3476 * Return the initial cwnd.
3479 bbr_initial_cwnd(struct tcp_bbr *bbr, struct tcpcb *tp)
3483 if (bbr->rc_init_win) {
3484 i_cwnd = bbr->rc_init_win * tp->t_maxseg;
3485 } else if (V_tcp_initcwnd_segments)
3486 i_cwnd = min((V_tcp_initcwnd_segments * tp->t_maxseg),
3487 max(2 * tp->t_maxseg, 14600));
3488 else if (V_tcp_do_rfc3390)
3489 i_cwnd = min(4 * tp->t_maxseg,
3490 max(2 * tp->t_maxseg, 4380));
3492 /* Per RFC5681 Section 3.1 */
3493 if (tp->t_maxseg > 2190)
3494 i_cwnd = 2 * tp->t_maxseg;
3495 else if (tp->t_maxseg > 1095)
3496 i_cwnd = 3 * tp->t_maxseg;
3498 i_cwnd = 4 * tp->t_maxseg;
3504 * Given a specified gain, return the target
3505 * cwnd based on that gain.
3508 bbr_get_raw_target_cwnd(struct tcp_bbr *bbr, uint32_t gain, uint64_t bw)
3513 if ((get_filter_value_small(&bbr->r_ctl.rc_rttprop) == 0xffffffff) ||
3514 (bbr_get_full_bw(bbr) == 0)) {
3515 /* No measurements yet */
3516 return (bbr_initial_cwnd(bbr, bbr->rc_tp));
3519 * Get bytes per RTT needed (rttProp is normally in
3520 * bbr_cwndtarget_rtt_touse)
3522 rtt = bbr_get_rtt(bbr, bbr_cwndtarget_rtt_touse);
3523 /* Get the bdp from the two values */
3524 bdp = bbr_get_bw_delay_prod(rtt, bw);
3525 /* Now apply the gain */
3526 cwnd = (uint32_t)(((bdp * ((uint64_t)gain)) + (uint64_t)(BBR_UNIT - 1)) / ((uint64_t)BBR_UNIT));
3532 bbr_get_target_cwnd(struct tcp_bbr *bbr, uint64_t bw, uint32_t gain)
3536 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs);
3537 /* Get the base cwnd with gain rounded to a mss */
3538 cwnd = roundup(bbr_get_raw_target_cwnd(bbr, bw, gain), mss);
3540 * Add in N (2 default since we do not have a
3541 * fq layer to trap packets in) quanta's per the I-D
3542 * section 4.2.3.2 quanta adjust.
3544 cwnd += (bbr_quanta * bbr->r_ctl.rc_pace_max_segs);
3545 if (bbr->rc_use_google) {
3546 if((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) &&
3547 (bbr_state_val(bbr) == BBR_SUB_GAIN)) {
3549 * The linux implementation adds
3550 * an extra 2 x mss in gain cycle which
3551 * is documented no-where except in the code.
3552 * so we add more for Neal undocumented feature
3556 if ((cwnd / mss) & 0x1) {
3557 /* Round up for odd num mss */
3561 /* Are we below the min cwnd? */
3562 if (cwnd < get_min_cwnd(bbr))
3563 return (get_min_cwnd(bbr));
3568 bbr_gain_adjust(struct tcp_bbr *bbr, uint16_t gain)
3576 bbr_get_header_oh(struct tcp_bbr *bbr)
3581 if (bbr->r_ctl.rc_inc_tcp_oh) {
3582 /* Do we include TCP overhead? */
3583 seg_oh = (bbr->rc_last_options + sizeof(struct tcphdr));
3585 if (bbr->r_ctl.rc_inc_ip_oh) {
3586 /* Do we include IP overhead? */
3589 seg_oh += sizeof(struct ip6_hdr);
3593 seg_oh += sizeof(struct ip);
3596 if (bbr->r_ctl.rc_inc_enet_oh) {
3597 /* Do we include the ethernet overhead? */
3598 seg_oh += sizeof(struct ether_header);
3605 bbr_get_pacing_length(struct tcp_bbr *bbr, uint16_t gain, uint32_t useconds_time, uint64_t bw)
3607 uint64_t divor, res, tim;
3609 if (useconds_time == 0)
3611 gain = bbr_gain_adjust(bbr, gain);
3612 divor = (uint64_t)USECS_IN_SECOND * (uint64_t)BBR_UNIT;
3613 tim = useconds_time;
3614 res = (tim * bw * gain) / divor;
3617 return ((uint32_t)res);
3621 * Given a gain and a length return the delay in useconds that
3622 * should be used to evenly space out packets
3623 * on the connection (based on the gain factor).
3626 bbr_get_pacing_delay(struct tcp_bbr *bbr, uint16_t gain, int32_t len, uint32_t cts, int nolog)
3628 uint64_t bw, lentim, res;
3629 uint32_t usecs, srtt, over = 0;
3630 uint32_t seg_oh, num_segs, maxseg;
3635 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
3636 num_segs = (len + maxseg - 1) / maxseg;
3637 if (bbr->rc_use_google == 0) {
3638 seg_oh = bbr_get_header_oh(bbr);
3639 len += (num_segs * seg_oh);
3641 gain = bbr_gain_adjust(bbr, gain);
3642 bw = bbr_get_bw(bbr);
3643 if (bbr->rc_use_google) {
3647 * Reduce the b/w by the google discount
3650 cbw = bw * (uint64_t)(1000 - bbr->r_ctl.bbr_google_discount);
3651 cbw /= (uint64_t)1000;
3652 /* We don't apply a discount if it results in 0 */
3656 lentim = ((uint64_t)len *
3657 (uint64_t)USECS_IN_SECOND *
3658 (uint64_t)BBR_UNIT);
3659 res = lentim / ((uint64_t)gain * bw);
3662 usecs = (uint32_t)res;
3663 srtt = bbr_get_rtt(bbr, BBR_SRTT);
3664 if (bbr_hptsi_max_mul && bbr_hptsi_max_div &&
3665 (bbr->rc_use_google == 0) &&
3666 (usecs > ((srtt * bbr_hptsi_max_mul) / bbr_hptsi_max_div))) {
3668 * We cannot let the delay be more than 1/2 the srtt time.
3669 * Otherwise we cannot pace out or send properly.
3671 over = usecs = (srtt * bbr_hptsi_max_mul) / bbr_hptsi_max_div;
3672 BBR_STAT_INC(bbr_hpts_min_time);
3675 bbr_log_pacing_delay_calc(bbr, gain, len, cts, usecs, bw, over, 1);
3680 bbr_ack_received(struct tcpcb *tp, struct tcp_bbr *bbr, struct tcphdr *th, uint32_t bytes_this_ack,
3681 uint32_t sack_changed, uint32_t prev_acked, int32_t line, uint32_t losses)
3683 INP_WLOCK_ASSERT(tp->t_inpcb);
3685 uint32_t cwnd, target_cwnd, saved_bytes, maxseg;
3689 if ((tp->t_flags & TF_GPUTINPROG) &&
3690 SEQ_GEQ(th->th_ack, tp->gput_ack)) {
3692 * Strech acks and compressed acks will cause this to
3693 * oscillate but we are doing it the same way as the main
3694 * stack so it will be compariable (though possibly not
3698 int64_t gput, time_stamp;
3700 gput = (int64_t) (th->th_ack - tp->gput_seq) * 8;
3701 time_stamp = max(1, ((bbr->r_ctl.rc_rcvtime - tp->gput_ts) / 1000));
3702 cgput = gput / time_stamp;
3703 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
3705 if (tp->t_stats_gput_prev > 0)
3706 stats_voi_update_abs_s32(tp->t_stats,
3708 ((gput - tp->t_stats_gput_prev) * 100) /
3709 tp->t_stats_gput_prev);
3710 tp->t_flags &= ~TF_GPUTINPROG;
3711 tp->t_stats_gput_prev = cgput;
3714 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) &&
3715 ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google)) {
3716 /* We don't change anything in probe-rtt */
3719 maxseg = tp->t_maxseg - bbr->rc_last_options;
3720 saved_bytes = bytes_this_ack;
3721 bytes_this_ack += sack_changed;
3722 if (bytes_this_ack > prev_acked) {
3723 bytes_this_ack -= prev_acked;
3725 * A byte ack'd gives us a full mss
3726 * to be like linux i.e. they count packets.
3728 if ((bytes_this_ack < maxseg) && bbr->rc_use_google)
3729 bytes_this_ack = maxseg;
3734 cwnd = tp->snd_cwnd;
3735 bw = get_filter_value(&bbr->r_ctl.rc_delrate);
3737 target_cwnd = bbr_get_target_cwnd(bbr,
3739 (uint32_t)bbr->r_ctl.rc_bbr_cwnd_gain);
3741 target_cwnd = bbr_initial_cwnd(bbr, bbr->rc_tp);
3742 if (IN_RECOVERY(tp->t_flags) &&
3743 (bbr->bbr_prev_in_rec == 0)) {
3745 * We are entering recovery and
3746 * thus packet conservation.
3748 bbr->pkt_conservation = 1;
3749 bbr->r_ctl.rc_recovery_start = bbr->r_ctl.rc_rcvtime;
3750 cwnd = ctf_flight_size(tp,
3751 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) +
3754 if (IN_RECOVERY(tp->t_flags)) {
3757 bbr->bbr_prev_in_rec = 1;
3758 if (cwnd > losses) {
3764 flight = ctf_flight_size(tp,
3765 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
3766 bbr_log_type_cwndupd(bbr, flight, 0,
3767 losses, 10, 0, 0, line);
3768 if (bbr->pkt_conservation) {
3771 if (TSTMP_GEQ(bbr->r_ctl.rc_rcvtime, bbr->r_ctl.rc_recovery_start))
3772 time_in = bbr->r_ctl.rc_rcvtime - bbr->r_ctl.rc_recovery_start;
3776 if (time_in >= bbr_get_rtt(bbr, BBR_RTT_PROP)) {
3777 /* Clear packet conservation after an rttProp */
3778 bbr->pkt_conservation = 0;
3780 if ((flight + bytes_this_ack) > cwnd)
3781 cwnd = flight + bytes_this_ack;
3782 if (cwnd < get_min_cwnd(bbr))
3783 cwnd = get_min_cwnd(bbr);
3784 tp->snd_cwnd = cwnd;
3785 bbr_log_type_cwndupd(bbr, saved_bytes, sack_changed,
3786 prev_acked, 1, target_cwnd, th->th_ack, line);
3791 bbr->bbr_prev_in_rec = 0;
3792 if ((bbr->rc_use_google == 0) && bbr->r_ctl.restrict_growth) {
3793 bbr->r_ctl.restrict_growth--;
3794 if (bytes_this_ack > maxseg)
3795 bytes_this_ack = maxseg;
3797 if (bbr->rc_filled_pipe) {
3799 * Here we have exited startup and filled the pipe. We will
3800 * thus allow the cwnd to shrink to the target. We hit here
3806 s_cwnd = min((cwnd + bytes_this_ack), target_cwnd);
3809 else if (bbr_cwnd_may_shrink || bbr->rc_use_google || bbr->rc_no_pacing)
3813 * Here we are still in startup, we increase cwnd by what
3816 if ((cwnd < target_cwnd) ||
3817 (bbr->rc_past_init_win == 0)) {
3819 cwnd += bytes_this_ack;
3822 * Method 4 means we are at target so no gain in
3823 * startup and past the initial window.
3828 tp->snd_cwnd = max(cwnd, get_min_cwnd(bbr));
3829 bbr_log_type_cwndupd(bbr, saved_bytes, sack_changed, prev_acked, meth, target_cwnd, th->th_ack, line);
3833 tcp_bbr_partialack(struct tcpcb *tp)
3835 struct tcp_bbr *bbr;
3837 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
3838 INP_WLOCK_ASSERT(tp->t_inpcb);
3839 if (ctf_flight_size(tp,
3840 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <=
3842 bbr->r_wanted_output = 1;
3847 bbr_post_recovery(struct tcpcb *tp)
3849 struct tcp_bbr *bbr;
3852 INP_WLOCK_ASSERT(tp->t_inpcb);
3853 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
3855 * Here we just exit recovery.
3857 EXIT_RECOVERY(tp->t_flags);
3858 /* Lock in our b/w reduction for the specified number of pkt-epochs */
3859 bbr->r_recovery_bw = 0;
3860 tp->snd_recover = tp->snd_una;
3861 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime);
3862 bbr->pkt_conservation = 0;
3863 if (bbr->rc_use_google == 0) {
3865 * For non-google mode lets
3866 * go ahead and make sure we clear
3867 * the recovery state so if we
3868 * bounce back in to recovery we
3871 bbr->bbr_prev_in_rec = 0;
3873 bbr_log_type_exit_rec(bbr);
3874 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) {
3875 tp->snd_cwnd = max(tp->snd_cwnd, bbr->r_ctl.rc_cwnd_on_ent);
3876 bbr_log_type_cwndupd(bbr, 0, 0, 0, 15, 0, 0, __LINE__);
3878 /* For probe-rtt case lets fix up its saved_cwnd */
3879 if (bbr->r_ctl.rc_saved_cwnd < bbr->r_ctl.rc_cwnd_on_ent) {
3880 bbr->r_ctl.rc_saved_cwnd = bbr->r_ctl.rc_cwnd_on_ent;
3881 bbr_log_type_cwndupd(bbr, 0, 0, 0, 16, 0, 0, __LINE__);
3884 flight = ctf_flight_size(tp,
3885 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
3886 if ((bbr->rc_use_google == 0) &&
3888 uint64_t val, lr2use;
3889 uint32_t maxseg, newcwnd, acks_inflight, ratio, cwnd;
3892 if (bbr_get_rtt(bbr, BBR_SRTT)) {
3893 val = ((uint64_t)bbr_get_rtt(bbr, BBR_RTT_PROP) * (uint64_t)1000);
3894 val /= bbr_get_rtt(bbr, BBR_SRTT);
3895 ratio = (uint32_t)val;
3899 bbr_log_type_cwndupd(bbr, bbr_red_mul, bbr_red_div,
3900 bbr->r_ctl.recovery_lr, 21,
3902 bbr->r_ctl.rc_red_cwnd_pe,
3904 if ((ratio < bbr_do_red) || (bbr_do_red == 0))
3906 if (((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) &&
3907 bbr_prtt_slam_cwnd) ||
3908 (bbr_sub_drain_slam_cwnd &&
3909 (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) &&
3910 bbr->rc_hit_state_1 &&
3911 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) ||
3912 ((bbr->rc_bbr_state == BBR_STATE_DRAIN) &&
3913 bbr_slam_cwnd_in_main_drain)) {
3915 * Here we must poke at the saved cwnd
3916 * as well as the cwnd.
3918 cwnd = bbr->r_ctl.rc_saved_cwnd;
3919 cwnd_p = &bbr->r_ctl.rc_saved_cwnd;
3921 cwnd = tp->snd_cwnd;
3922 cwnd_p = &tp->snd_cwnd;
3924 maxseg = tp->t_maxseg - bbr->rc_last_options;
3925 /* Add the overall lr with the recovery lr */
3926 if (bbr->r_ctl.rc_lost == 0)
3928 else if (bbr->r_ctl.rc_delivered == 0)
3931 lr2use = bbr->r_ctl.rc_lost * 1000;
3932 lr2use /= bbr->r_ctl.rc_delivered;
3934 lr2use += bbr->r_ctl.recovery_lr;
3935 acks_inflight = (flight / (maxseg * 2));
3936 if (bbr_red_scale) {
3937 lr2use *= bbr_get_rtt(bbr, BBR_SRTT);
3938 lr2use /= bbr_red_scale;
3939 if ((bbr_red_growth_restrict) &&
3940 ((bbr_get_rtt(bbr, BBR_SRTT)/bbr_red_scale) > 1))
3941 bbr->r_ctl.restrict_growth += acks_inflight;
3944 val = (uint64_t)cwnd * lr2use;
3947 newcwnd = roundup((cwnd - val), maxseg);
3951 val = (uint64_t)cwnd * (uint64_t)bbr_red_mul;
3952 val /= (uint64_t)bbr_red_div;
3953 newcwnd = roundup((uint32_t)val, maxseg);
3955 /* with standard delayed acks how many acks can I expect? */
3956 if (bbr_drop_limit == 0) {
3958 * Anticpate how much we will
3959 * raise the cwnd based on the acks.
3961 if ((newcwnd + (acks_inflight * maxseg)) < get_min_cwnd(bbr)) {
3962 /* We do enforce the min (with the acks) */
3963 newcwnd = (get_min_cwnd(bbr) - acks_inflight);
3967 * A strict drop limit of N is is inplace
3969 if (newcwnd < (bbr_drop_limit * maxseg)) {
3970 newcwnd = bbr_drop_limit * maxseg;
3973 /* For the next N acks do we restrict the growth */
3975 if (tp->snd_cwnd > newcwnd)
3976 tp->snd_cwnd = newcwnd;
3977 bbr_log_type_cwndupd(bbr, bbr_red_mul, bbr_red_div, val, 22,
3979 bbr_get_rtt(bbr, BBR_SRTT), __LINE__);
3980 bbr->r_ctl.rc_red_cwnd_pe = bbr->r_ctl.rc_pkt_epoch;
3983 bbr->r_ctl.recovery_lr = 0;
3984 if (flight <= tp->snd_cwnd) {
3985 bbr->r_wanted_output = 1;
3987 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime);
3991 bbr_setup_red_bw(struct tcp_bbr *bbr, uint32_t cts)
3993 bbr->r_ctl.red_bw = get_filter_value(&bbr->r_ctl.rc_delrate);
3994 /* Limit the drop in b/w to 1/2 our current filter. */
3995 if (bbr->r_ctl.red_bw > bbr->r_ctl.rc_bbr_cur_del_rate)
3996 bbr->r_ctl.red_bw = bbr->r_ctl.rc_bbr_cur_del_rate;
3997 if (bbr->r_ctl.red_bw < (get_filter_value(&bbr->r_ctl.rc_delrate) / 2))
3998 bbr->r_ctl.red_bw = get_filter_value(&bbr->r_ctl.rc_delrate) / 2;
3999 tcp_bbr_tso_size_check(bbr, cts);
4003 bbr_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type, struct bbr_sendmap *rsm)
4005 struct tcp_bbr *bbr;
4007 INP_WLOCK_ASSERT(tp->t_inpcb);
4008 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
4011 if (!IN_RECOVERY(tp->t_flags)) {
4012 tp->snd_recover = tp->snd_max;
4013 /* Start a new epoch */
4014 bbr_set_pktepoch(bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
4015 if (bbr->rc_lt_is_sampling || bbr->rc_lt_use_bw) {
4017 * Move forward the lt epoch
4018 * so it won't count the truncated
4021 bbr->r_ctl.rc_lt_epoch++;
4023 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) {
4025 * Just like the policer detection code
4026 * if we are in startup we must push
4027 * forward the last startup epoch
4028 * to hide the truncated PE.
4030 bbr->r_ctl.rc_bbr_last_startup_epoch++;
4032 bbr->r_ctl.rc_cwnd_on_ent = tp->snd_cwnd;
4033 ENTER_RECOVERY(tp->t_flags);
4034 bbr->rc_tlp_rtx_out = 0;
4035 bbr->r_ctl.recovery_lr = bbr->r_ctl.rc_pkt_epoch_loss_rate;
4036 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime);
4037 if (bbr->rc_inp->inp_in_hpts &&
4038 ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) == 0)) {
4040 * When we enter recovery, we need to restart
4041 * any timers. This may mean we gain an agg
4042 * early, which will be made up for at the last
4045 bbr->rc_timer_first = 1;
4046 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
4049 * Calculate a new cwnd based on to the current
4050 * delivery rate with no gain. We get the bdp
4051 * without gaining it up like we normally would and
4052 * we use the last cur_del_rate.
4054 if ((bbr->rc_use_google == 0) &&
4055 (bbr->r_ctl.bbr_rttprobe_gain_val ||
4056 (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT))) {
4057 tp->snd_cwnd = ctf_flight_size(tp,
4058 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) +
4059 (tp->t_maxseg - bbr->rc_last_options);
4060 if (tp->snd_cwnd < get_min_cwnd(bbr)) {
4061 /* We always gate to min cwnd */
4062 tp->snd_cwnd = get_min_cwnd(bbr);
4064 bbr_log_type_cwndupd(bbr, 0, 0, 0, 14, 0, 0, __LINE__);
4066 bbr_log_type_enter_rec(bbr, rsm->r_start);
4070 KMOD_TCPSTAT_INC(tcps_sndrexmitbad);
4071 /* RTO was unnecessary, so reset everything. */
4072 bbr_reset_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime);
4073 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) {
4074 tp->snd_cwnd = tp->snd_cwnd_prev;
4075 tp->snd_ssthresh = tp->snd_ssthresh_prev;
4076 tp->snd_recover = tp->snd_recover_prev;
4077 tp->snd_cwnd = max(tp->snd_cwnd, bbr->r_ctl.rc_cwnd_on_ent);
4078 bbr_log_type_cwndupd(bbr, 0, 0, 0, 13, 0, 0, __LINE__);
4080 tp->t_badrxtwin = 0;
4086 * Indicate whether this ack should be delayed. We can delay the ack if
4087 * following conditions are met:
4088 * - There is no delayed ack timer in progress.
4089 * - Our last ack wasn't a 0-sized window. We never want to delay
4090 * the ack that opens up a 0-sized window.
4091 * - LRO wasn't used for this segment. We make sure by checking that the
4092 * segment size is not larger than the MSS.
4093 * - Delayed acks are enabled or this is a half-synchronized T/TCP
4095 * - The data being acked is less than a full segment (a stretch ack
4096 * of more than a segment we should ack.
4097 * - nsegs is 1 (if its more than that we received more than 1 ack).
4099 #define DELAY_ACK(tp, bbr, nsegs) \
4100 (((tp->t_flags & TF_RXWIN0SENT) == 0) && \
4101 ((bbr->bbr_segs_rcvd + nsegs) < tp->t_delayed_ack) && \
4102 (tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN)))
4105 * Return the lowest RSM in the map of
4106 * packets still in flight that is not acked.
4107 * This should normally find on the first one
4108 * since we remove packets from the send
4109 * map after they are marked ACKED.
4111 static struct bbr_sendmap *
4112 bbr_find_lowest_rsm(struct tcp_bbr *bbr)
4114 struct bbr_sendmap *rsm;
4117 * Walk the time-order transmitted list looking for an rsm that is
4118 * not acked. This will be the one that was sent the longest time
4119 * ago that is still outstanding.
4121 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_tmap, r_tnext) {
4122 if (rsm->r_flags & BBR_ACKED) {
4131 static struct bbr_sendmap *
4132 bbr_find_high_nonack(struct tcp_bbr *bbr, struct bbr_sendmap *rsm)
4134 struct bbr_sendmap *prsm;
4137 * Walk the sequence order list backward until we hit and arrive at
4138 * the highest seq not acked. In theory when this is called it
4139 * should be the last segment (which it was not).
4142 TAILQ_FOREACH_REVERSE_FROM(prsm, &bbr->r_ctl.rc_map, bbr_head, r_next) {
4143 if (prsm->r_flags & (BBR_ACKED | BBR_HAS_FIN)) {
4152 * Returns to the caller the number of microseconds that
4153 * the packet can be outstanding before we think we
4154 * should have had an ack returned.
4157 bbr_calc_thresh_rack(struct tcp_bbr *bbr, uint32_t srtt, uint32_t cts, struct bbr_sendmap *rsm)
4160 * lro is the flag we use to determine if we have seen reordering.
4161 * If it gets set we have seen reordering. The reorder logic either
4162 * works in one of two ways:
4164 * If reorder-fade is configured, then we track the last time we saw
4165 * re-ordering occur. If we reach the point where enough time as
4166 * passed we no longer consider reordering has occuring.
4168 * Or if reorder-face is 0, then once we see reordering we consider
4169 * the connection to alway be subject to reordering and just set lro
4172 * In the end if lro is non-zero we add the extra time for
4176 uint32_t thresh, t_rxtcur;
4180 if (bbr->r_ctl.rc_reorder_ts) {
4181 if (bbr->r_ctl.rc_reorder_fade) {
4182 if (SEQ_GEQ(cts, bbr->r_ctl.rc_reorder_ts)) {
4183 lro = cts - bbr->r_ctl.rc_reorder_ts;
4186 * No time as passed since the last
4187 * reorder, mark it as reordering.
4192 /* Negative time? */
4195 if (lro > bbr->r_ctl.rc_reorder_fade) {
4196 /* Turn off reordering seen too */
4197 bbr->r_ctl.rc_reorder_ts = 0;
4201 /* Reodering does not fade */
4207 thresh = srtt + bbr->r_ctl.rc_pkt_delay;
4209 /* It must be set, if not you get 1/4 rtt */
4210 if (bbr->r_ctl.rc_reorder_shift)
4211 thresh += (srtt >> bbr->r_ctl.rc_reorder_shift);
4213 thresh += (srtt >> 2);
4217 /* We don't let the rack timeout be above a RTO */
4218 if ((bbr->rc_tp)->t_srtt == 0)
4219 t_rxtcur = BBR_INITIAL_RTO;
4221 t_rxtcur = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
4222 if (thresh > t_rxtcur) {
4225 /* And we don't want it above the RTO max either */
4226 if (thresh > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) {
4227 thresh = (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND);
4229 bbr_log_thresh_choice(bbr, cts, thresh, lro, srtt, rsm, BBR_TO_FRM_RACK);
4234 * Return to the caller the amount of time in mico-seconds
4235 * that should be used for the TLP timer from the last
4236 * send time of this packet.
4239 bbr_calc_thresh_tlp(struct tcpcb *tp, struct tcp_bbr *bbr,
4240 struct bbr_sendmap *rsm, uint32_t srtt,
4243 uint32_t thresh, len, maxseg, t_rxtcur;
4244 struct bbr_sendmap *prsm;
4248 if (bbr->rc_tlp_threshold)
4249 thresh = srtt + (srtt / bbr->rc_tlp_threshold);
4251 thresh = (srtt * 2);
4252 maxseg = tp->t_maxseg - bbr->rc_last_options;
4253 /* Get the previous sent packet, if any */
4254 len = rsm->r_end - rsm->r_start;
4257 prsm = TAILQ_PREV(rsm, bbr_head, r_tnext);
4258 if (prsm && (len <= maxseg)) {
4260 * Two packets outstanding, thresh should be (2*srtt) +
4261 * possible inter-packet delay (if any).
4263 uint32_t inter_gap = 0;
4266 idx = rsm->r_rtr_cnt - 1;
4267 nidx = prsm->r_rtr_cnt - 1;
4268 if (TSTMP_GEQ(rsm->r_tim_lastsent[nidx], prsm->r_tim_lastsent[idx])) {
4269 /* Yes it was sent later (or at the same time) */
4270 inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx];
4272 thresh += inter_gap;
4273 } else if (len <= maxseg) {
4275 * Possibly compensate for delayed-ack.
4277 uint32_t alt_thresh;
4279 alt_thresh = srtt + (srtt / 2) + bbr_delayed_ack_time;
4280 if (alt_thresh > thresh)
4281 thresh = alt_thresh;
4283 /* Not above the current RTO */
4284 if (tp->t_srtt == 0)
4285 t_rxtcur = BBR_INITIAL_RTO;
4287 t_rxtcur = TICKS_2_USEC(tp->t_rxtcur);
4289 bbr_log_thresh_choice(bbr, cts, thresh, t_rxtcur, srtt, rsm, BBR_TO_FRM_TLP);
4290 /* Not above an RTO */
4291 if (thresh > t_rxtcur) {
4294 /* Not above a RTO max */
4295 if (thresh > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) {
4296 thresh = (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND);
4298 /* And now apply the user TLP min */
4299 if (thresh < bbr_tlp_min) {
4300 thresh = bbr_tlp_min;
4306 * Return one of three RTTs to use (in microseconds).
4308 static __inline uint32_t
4309 bbr_get_rtt(struct tcp_bbr *bbr, int32_t rtt_type)
4314 f_rtt = get_filter_value_small(&bbr->r_ctl.rc_rttprop);
4315 if (get_filter_value_small(&bbr->r_ctl.rc_rttprop) == 0xffffffff) {
4316 /* We have no rtt at all */
4317 if (bbr->rc_tp->t_srtt == 0)
4318 f_rtt = BBR_INITIAL_RTO;
4320 f_rtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT);
4322 * Since we don't know how good the rtt is apply a
4325 if (f_rtt < bbr_delayed_ack_time) {
4326 f_rtt = bbr_delayed_ack_time;
4329 /* Take the filter version or last measured pkt-rtt */
4330 if (rtt_type == BBR_RTT_PROP) {
4332 } else if (rtt_type == BBR_RTT_PKTRTT) {
4333 if (bbr->r_ctl.rc_pkt_epoch_rtt) {
4334 srtt = bbr->r_ctl.rc_pkt_epoch_rtt;
4336 /* No pkt rtt yet */
4339 } else if (rtt_type == BBR_RTT_RACK) {
4340 srtt = bbr->r_ctl.rc_last_rtt;
4341 /* We need to add in any internal delay for our timer */
4342 if (bbr->rc_ack_was_delayed)
4343 srtt += bbr->r_ctl.rc_ack_hdwr_delay;
4344 } else if (rtt_type == BBR_SRTT) {
4345 srtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT);
4349 #ifdef BBR_INVARIANTS
4350 panic("Unknown rtt request type %d", rtt_type);
4357 bbr_is_lost(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t cts)
4362 thresh = bbr_calc_thresh_rack(bbr, bbr_get_rtt(bbr, BBR_RTT_RACK),
4364 if ((cts - rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]) >= thresh) {
4365 /* It is lost (past time) */
4372 * Return a sendmap if we need to retransmit something.
4374 static struct bbr_sendmap *
4375 bbr_check_recovery_mode(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4378 * Check to see that we don't need to fall into recovery. We will
4379 * need to do so if our oldest transmit is past the time we should
4383 struct bbr_sendmap *rsm;
4386 if (TAILQ_EMPTY(&bbr->r_ctl.rc_map)) {
4387 /* Nothing outstanding that we know of */
4390 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
4392 /* Nothing in the transmit map */
4395 if (tp->t_flags & TF_SENTFIN) {
4396 /* Fin restricted, don't find anything once a fin is sent */
4399 if (rsm->r_flags & BBR_ACKED) {
4401 * Ok the first one is acked (this really should not happen
4402 * since we remove the from the tmap once they are acked)
4404 rsm = bbr_find_lowest_rsm(bbr);
4408 idx = rsm->r_rtr_cnt - 1;
4409 if (SEQ_LEQ(cts, rsm->r_tim_lastsent[idx])) {
4410 /* Send timestamp is the same or less? can't be ready */
4413 /* Get our RTT time */
4414 if (bbr_is_lost(bbr, rsm, cts) &&
4415 ((rsm->r_dupack >= DUP_ACK_THRESHOLD) ||
4416 (rsm->r_flags & BBR_SACK_PASSED))) {
4417 if ((rsm->r_flags & BBR_MARKED_LOST) == 0) {
4418 rsm->r_flags |= BBR_MARKED_LOST;
4419 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start;
4420 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start;
4422 bbr_cong_signal(tp, NULL, CC_NDUPACK, rsm);
4423 #ifdef BBR_INVARIANTS
4424 if ((rsm->r_end - rsm->r_start) == 0)
4425 panic("tp:%p bbr:%p rsm:%p length is 0?", tp, bbr, rsm);
4433 * RACK Timer, here we simply do logging and house keeping.
4434 * the normal bbr_output_wtime() function will call the
4435 * appropriate thing to check if we need to do a RACK retransmit.
4436 * We return 1, saying don't proceed with bbr_output_wtime only
4437 * when all timers have been stopped (destroyed PCB?).
4440 bbr_timeout_rack(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4443 * This timer simply provides an internal trigger to send out data.
4444 * The check_recovery_mode call will see if there are needed
4445 * retransmissions, if so we will enter fast-recovery. The output
4446 * call may or may not do the same thing depending on sysctl
4451 if (bbr->rc_all_timers_stopped) {
4454 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) {
4455 /* Its not time yet */
4458 BBR_STAT_INC(bbr_to_tot);
4459 lost = bbr->r_ctl.rc_lost;
4460 if (bbr->r_state && (bbr->r_state != tp->t_state))
4461 bbr_set_state(tp, bbr, 0);
4462 bbr_log_to_event(bbr, cts, BBR_TO_FRM_RACK);
4463 if (bbr->r_ctl.rc_resend == NULL) {
4464 /* Lets do the check here */
4465 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts);
4467 if (bbr_policer_call_from_rack_to)
4468 bbr_lt_bw_sampling(bbr, cts, (bbr->r_ctl.rc_lost > lost));
4469 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK;
4473 static __inline void
4474 bbr_clone_rsm(struct tcp_bbr *bbr, struct bbr_sendmap *nrsm, struct bbr_sendmap *rsm, uint32_t start)
4478 nrsm->r_start = start;
4479 nrsm->r_end = rsm->r_end;
4480 nrsm->r_rtr_cnt = rsm->r_rtr_cnt;
4481 nrsm->r_flags = rsm->r_flags;
4482 /* We don't transfer forward the SYN flag */
4483 nrsm->r_flags &= ~BBR_HAS_SYN;
4484 /* We move forward the FIN flag, not that this should happen */
4485 rsm->r_flags &= ~BBR_HAS_FIN;
4486 nrsm->r_dupack = rsm->r_dupack;
4487 nrsm->r_rtr_bytes = 0;
4488 nrsm->r_is_gain = rsm->r_is_gain;
4489 nrsm->r_is_drain = rsm->r_is_drain;
4490 nrsm->r_delivered = rsm->r_delivered;
4491 nrsm->r_ts_valid = rsm->r_ts_valid;
4492 nrsm->r_del_ack_ts = rsm->r_del_ack_ts;
4493 nrsm->r_del_time = rsm->r_del_time;
4494 nrsm->r_app_limited = rsm->r_app_limited;
4495 nrsm->r_first_sent_time = rsm->r_first_sent_time;
4496 nrsm->r_flight_at_send = rsm->r_flight_at_send;
4497 /* We split a piece the lower section looses any just_ret flag. */
4498 nrsm->r_bbr_state = rsm->r_bbr_state;
4499 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) {
4500 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx];
4502 rsm->r_end = nrsm->r_start;
4503 idx = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs);
4505 /* Check if we got too small */
4506 if ((rsm->r_is_smallmap == 0) &&
4507 ((rsm->r_end - rsm->r_start) <= idx)) {
4508 bbr->r_ctl.rc_num_small_maps_alloced++;
4509 rsm->r_is_smallmap = 1;
4511 /* Check the new one as well */
4512 if ((nrsm->r_end - nrsm->r_start) <= idx) {
4513 bbr->r_ctl.rc_num_small_maps_alloced++;
4514 nrsm->r_is_smallmap = 1;
4519 bbr_sack_mergable(struct bbr_sendmap *at,
4520 uint32_t start, uint32_t end)
4523 * Given a sack block defined by
4524 * start and end, and a current postion
4525 * at. Return 1 if either side of at
4526 * would show that the block is mergable
4527 * to that side. A block to be mergable
4528 * must have overlap with the start/end
4529 * and be in the SACK'd state.
4531 struct bbr_sendmap *l_rsm;
4532 struct bbr_sendmap *r_rsm;
4534 /* first get the either side blocks */
4535 l_rsm = TAILQ_PREV(at, bbr_head, r_next);
4536 r_rsm = TAILQ_NEXT(at, r_next);
4537 if (l_rsm && (l_rsm->r_flags & BBR_ACKED)) {
4538 /* Potentially mergeable */
4539 if ((l_rsm->r_end == start) ||
4540 (SEQ_LT(start, l_rsm->r_end) &&
4541 SEQ_GT(end, l_rsm->r_end))) {
4552 if (r_rsm && (r_rsm->r_flags & BBR_ACKED)) {
4553 /* Potentially mergeable */
4554 if ((r_rsm->r_start == end) ||
4555 (SEQ_LT(start, r_rsm->r_start) &&
4556 SEQ_GT(end, r_rsm->r_start))) {
4558 * map blk |---------|
4561 * map blk |---------|
4562 * sack blk |-------|
4570 static struct bbr_sendmap *
4571 bbr_merge_rsm(struct tcp_bbr *bbr,
4572 struct bbr_sendmap *l_rsm,
4573 struct bbr_sendmap *r_rsm)
4576 * We are merging two ack'd RSM's,
4577 * the l_rsm is on the left (lower seq
4578 * values) and the r_rsm is on the right
4579 * (higher seq value). The simplest way
4580 * to merge these is to move the right
4581 * one into the left. I don't think there
4582 * is any reason we need to try to find
4583 * the oldest (or last oldest retransmitted).
4585 l_rsm->r_end = r_rsm->r_end;
4586 if (l_rsm->r_dupack < r_rsm->r_dupack)
4587 l_rsm->r_dupack = r_rsm->r_dupack;
4588 if (r_rsm->r_rtr_bytes)
4589 l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes;
4590 if (r_rsm->r_in_tmap) {
4591 /* This really should not happen */
4592 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, r_rsm, r_tnext);
4594 if (r_rsm->r_app_limited)
4595 l_rsm->r_app_limited = r_rsm->r_app_limited;
4597 if (r_rsm->r_flags & BBR_HAS_FIN)
4598 l_rsm->r_flags |= BBR_HAS_FIN;
4599 if (r_rsm->r_flags & BBR_TLP)
4600 l_rsm->r_flags |= BBR_TLP;
4601 if (r_rsm->r_flags & BBR_RWND_COLLAPSED)
4602 l_rsm->r_flags |= BBR_RWND_COLLAPSED;
4603 if (r_rsm->r_flags & BBR_MARKED_LOST) {
4604 /* This really should not happen */
4605 bbr->r_ctl.rc_lost_bytes -= r_rsm->r_end - r_rsm->r_start;
4607 TAILQ_REMOVE(&bbr->r_ctl.rc_map, r_rsm, r_next);
4608 if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) {
4609 /* Transfer the split limit to the map we free */
4610 r_rsm->r_limit_type = l_rsm->r_limit_type;
4611 l_rsm->r_limit_type = 0;
4613 bbr_free(bbr, r_rsm);
4618 * TLP Timer, here we simply setup what segment we want to
4619 * have the TLP expire on, the normal bbr_output_wtime() will then
4622 * We return 1, saying don't proceed with bbr_output_wtime only
4623 * when all timers have been stopped (destroyed PCB?).
4626 bbr_timeout_tlp(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4631 struct bbr_sendmap *rsm = NULL;
4634 uint32_t out, avail;
4636 int collapsed_win = 0;
4638 if (bbr->rc_all_timers_stopped) {
4641 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) {
4642 /* Its not time yet */
4645 if (bbr_progress_timeout_check(bbr)) {
4646 tcp_set_inp_to_drop(bbr->rc_inp, ETIMEDOUT);
4649 /* Did we somehow get into persists? */
4650 if (bbr->rc_in_persist) {
4653 if (bbr->r_state && (bbr->r_state != tp->t_state))
4654 bbr_set_state(tp, bbr, 0);
4655 BBR_STAT_INC(bbr_tlp_tot);
4656 maxseg = tp->t_maxseg - bbr->rc_last_options;
4658 if (bbr->rc_inp->inp_socket->so_snd.sb_flags & SB_TLS_IFNET) {
4660 * For hardware TLS we do *not* want to send
4667 * A TLP timer has expired. We have been idle for 2 rtts. So we now
4668 * need to figure out how to force a full MSS segment out.
4670 so = tp->t_inpcb->inp_socket;
4671 avail = sbavail(&so->so_snd);
4672 out = ctf_outstanding(tp);
4673 if (out > tp->snd_wnd) {
4674 /* special case, we need a retransmission */
4679 /* New data is available */
4683 } else if ((amm < maxseg) && ((tp->t_flags & TF_NODELAY) == 0)) {
4684 /* not enough to fill a MTU and no-delay is off */
4687 /* Set the send-new override */
4688 if ((out + amm) <= tp->snd_wnd) {
4689 bbr->rc_tlp_new_data = 1;
4693 bbr->r_ctl.rc_tlp_seg_send_cnt = 0;
4694 bbr->r_ctl.rc_last_tlp_seq = tp->snd_max;
4695 bbr->r_ctl.rc_tlp_send = NULL;
4697 BBR_STAT_INC(bbr_tlp_newdata);
4702 * Ok we need to arrange the last un-acked segment to be re-sent, or
4703 * optionally the first un-acked segment.
4705 if (collapsed_win == 0) {
4706 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next);
4707 if (rsm && (BBR_ACKED | BBR_HAS_FIN)) {
4708 rsm = bbr_find_high_nonack(bbr, rsm);
4715 * We must find the last segment
4716 * that was acceptable by the client.
4718 TAILQ_FOREACH_REVERSE(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) {
4719 if ((rsm->r_flags & BBR_RWND_COLLAPSED) == 0) {
4725 /* None? if so send the first */
4726 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
4731 if ((rsm->r_end - rsm->r_start) > maxseg) {
4733 * We need to split this the last segment in two.
4735 struct bbr_sendmap *nrsm;
4737 nrsm = bbr_alloc_full_limit(bbr);
4740 * We can't get memory to split, we can either just
4741 * not split it. Or retransmit the whole piece, lets
4742 * do the large send (BTLP :-) ).
4746 bbr_clone_rsm(bbr, nrsm, rsm, (rsm->r_end - maxseg));
4747 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
4748 if (rsm->r_in_tmap) {
4749 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
4750 nrsm->r_in_tmap = 1;
4752 rsm->r_flags &= (~BBR_HAS_FIN);
4756 bbr->r_ctl.rc_tlp_send = rsm;
4757 bbr->rc_tlp_rtx_out = 1;
4758 if (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq) {
4759 bbr->r_ctl.rc_tlp_seg_send_cnt++;
4762 bbr->r_ctl.rc_last_tlp_seq = rsm->r_start;
4763 bbr->r_ctl.rc_tlp_seg_send_cnt = 1;
4766 if (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend) {
4768 * Can't [re]/transmit a segment we have retranmitted the
4769 * max times. We need the retransmit timer to take over.
4772 bbr->rc_tlp_new_data = 0;
4773 bbr->r_ctl.rc_tlp_send = NULL;
4775 rsm->r_flags &= ~BBR_TLP;
4776 BBR_STAT_INC(bbr_tlp_retran_fail);
4779 rsm->r_flags |= BBR_TLP;
4781 if (rsm && (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq) &&
4782 (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend)) {
4784 * We have retransmitted to many times for TLP. Switch to
4785 * the regular RTO timer
4789 bbr_log_to_event(bbr, cts, BBR_TO_FRM_TLP);
4790 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
4795 * Delayed ack Timer, here we simply need to setup the
4796 * ACK_NOW flag and remove the DELACK flag. From there
4797 * the output routine will send the ack out.
4799 * We only return 1, saying don't proceed, if all timers
4800 * are stopped (destroyed PCB?).
4803 bbr_timeout_delack(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4805 if (bbr->rc_all_timers_stopped) {
4808 bbr_log_to_event(bbr, cts, BBR_TO_FRM_DELACK);
4809 tp->t_flags &= ~TF_DELACK;
4810 tp->t_flags |= TF_ACKNOW;
4811 KMOD_TCPSTAT_INC(tcps_delack);
4812 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
4817 * Persists timer, here we simply need to setup the
4818 * FORCE-DATA flag the output routine will send
4819 * the one byte send.
4821 * We only return 1, saying don't proceed, if all timers
4822 * are stopped (destroyed PCB?).
4825 bbr_timeout_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4827 struct tcptemp *t_template;
4830 if (bbr->rc_all_timers_stopped) {
4833 if (bbr->rc_in_persist == 0)
4835 KASSERT(tp->t_inpcb != NULL,
4836 ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
4838 * Persistence timer into zero window. Force a byte to be output, if
4841 bbr_log_to_event(bbr, cts, BBR_TO_FRM_PERSIST);
4842 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT;
4843 KMOD_TCPSTAT_INC(tcps_persisttimeo);
4845 * Have we exceeded the user specified progress time?
4847 if (bbr_progress_timeout_check(bbr)) {
4848 tcp_set_inp_to_drop(bbr->rc_inp, ETIMEDOUT);
4852 * Hack: if the peer is dead/unreachable, we do not time out if the
4853 * window is closed. After a full backoff, drop the connection if
4854 * the idle time (no responses to probes) reaches the maximum
4855 * backoff that we would use if retransmitting.
4857 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
4858 (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
4859 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
4860 KMOD_TCPSTAT_INC(tcps_persistdrop);
4861 tcp_set_inp_to_drop(bbr->rc_inp, ETIMEDOUT);
4864 if ((sbavail(&bbr->rc_inp->inp_socket->so_snd) == 0) &&
4865 tp->snd_una == tp->snd_max) {
4866 bbr_exit_persist(tp, bbr, cts, __LINE__);
4871 * If the user has closed the socket then drop a persisting
4872 * connection after a much reduced timeout.
4874 if (tp->t_state > TCPS_CLOSE_WAIT &&
4875 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
4876 KMOD_TCPSTAT_INC(tcps_persistdrop);
4877 tcp_set_inp_to_drop(bbr->rc_inp, ETIMEDOUT);
4880 t_template = tcpip_maketemplate(bbr->rc_inp);
4882 tcp_respond(tp, t_template->tt_ipgen,
4883 &t_template->tt_t, (struct mbuf *)NULL,
4884 tp->rcv_nxt, tp->snd_una - 1, 0);
4885 /* This sends an ack */
4886 if (tp->t_flags & TF_DELACK)
4887 tp->t_flags &= ~TF_DELACK;
4888 free(t_template, M_TEMP);
4890 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
4892 bbr_start_hpts_timer(bbr, tp, cts, 3, 0, 0);
4898 * If a keepalive goes off, we had no other timers
4899 * happening. We always return 1 here since this
4900 * routine either drops the connection or sends
4901 * out a segment with respond.
4904 bbr_timeout_keepalive(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4906 struct tcptemp *t_template;
4909 if (bbr->rc_all_timers_stopped) {
4912 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP;
4914 bbr_log_to_event(bbr, cts, BBR_TO_FRM_KEEP);
4916 * Keep-alive timer went off; send something or drop connection if
4917 * idle for too long.
4919 KMOD_TCPSTAT_INC(tcps_keeptimeo);
4920 if (tp->t_state < TCPS_ESTABLISHED)
4922 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
4923 tp->t_state <= TCPS_CLOSING) {
4924 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
4927 * Send a packet designed to force a response if the peer is
4928 * up and reachable: either an ACK if the connection is
4929 * still alive, or an RST if the peer has closed the
4930 * connection due to timeout or reboot. Using sequence
4931 * number tp->snd_una-1 causes the transmitted zero-length
4932 * segment to lie outside the receive window; by the
4933 * protocol spec, this requires the correspondent TCP to
4936 KMOD_TCPSTAT_INC(tcps_keepprobe);
4937 t_template = tcpip_maketemplate(inp);
4939 tcp_respond(tp, t_template->tt_ipgen,
4940 &t_template->tt_t, (struct mbuf *)NULL,
4941 tp->rcv_nxt, tp->snd_una - 1, 0);
4942 free(t_template, M_TEMP);
4945 bbr_start_hpts_timer(bbr, tp, cts, 4, 0, 0);
4948 KMOD_TCPSTAT_INC(tcps_keepdrops);
4949 tcp_set_inp_to_drop(bbr->rc_inp, ETIMEDOUT);
4954 * Retransmit helper function, clear up all the ack
4955 * flags and take care of important book keeping.
4958 bbr_remxt_tmr(struct tcpcb *tp)
4961 * The retransmit timer went off, all sack'd blocks must be
4964 struct bbr_sendmap *rsm, *trsm = NULL;
4965 struct tcp_bbr *bbr;
4968 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
4969 cts = tcp_get_usecs(&bbr->rc_tv);
4970 lost = bbr->r_ctl.rc_lost;
4971 if (bbr->r_state && (bbr->r_state != tp->t_state))
4972 bbr_set_state(tp, bbr, 0);
4974 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
4975 if (rsm->r_flags & BBR_ACKED) {
4979 if (rsm->r_in_tmap == 0) {
4980 /* We must re-add it back to the tlist */
4982 TAILQ_INSERT_HEAD(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
4984 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, trsm, rsm, r_tnext);
4988 old_flags = rsm->r_flags;
4989 rsm->r_flags |= BBR_RXT_CLEARED;
4990 rsm->r_flags &= ~(BBR_ACKED | BBR_SACK_PASSED | BBR_WAS_SACKPASS);
4991 bbr_log_type_rsmclear(bbr, cts, rsm, old_flags, __LINE__);
4993 if ((rsm->r_flags & BBR_MARKED_LOST) == 0) {
4994 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start;
4995 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start;
4997 if (bbr_marks_rxt_sack_passed) {
4999 * With this option, we will rack out
5000 * in 1ms increments the rest of the packets.
5002 rsm->r_flags |= BBR_SACK_PASSED | BBR_MARKED_LOST;
5003 rsm->r_flags &= ~BBR_WAS_SACKPASS;
5006 * With this option we only mark them lost
5007 * and remove all sack'd markings. We will run
5008 * another RXT or a TLP. This will cause
5009 * us to eventually send more based on what
5012 rsm->r_flags |= BBR_MARKED_LOST;
5013 rsm->r_flags &= ~BBR_WAS_SACKPASS;
5014 rsm->r_flags &= ~BBR_SACK_PASSED;
5019 bbr->r_ctl.rc_resend = TAILQ_FIRST(&bbr->r_ctl.rc_map);
5020 /* Clear the count (we just un-acked them) */
5021 bbr_log_to_event(bbr, cts, BBR_TO_FRM_TMR);
5022 bbr->rc_tlp_new_data = 0;
5023 bbr->r_ctl.rc_tlp_seg_send_cnt = 0;
5024 /* zap the behindness on a rxt */
5025 bbr->r_ctl.rc_hptsi_agg_delay = 0;
5026 bbr->r_agg_early_set = 0;
5027 bbr->r_ctl.rc_agg_early = 0;
5028 bbr->rc_tlp_rtx_out = 0;
5029 bbr->r_ctl.rc_sacked = 0;
5030 bbr->r_ctl.rc_sacklast = NULL;
5031 bbr->r_timer_override = 1;
5032 bbr_lt_bw_sampling(bbr, cts, (bbr->r_ctl.rc_lost > lost));
5036 * Re-transmit timeout! If we drop the PCB we will return 1, otherwise
5037 * we will setup to retransmit the lowest seq number outstanding.
5040 bbr_timeout_rxt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
5046 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT;
5047 if (bbr->rc_all_timers_stopped) {
5050 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
5051 (tp->snd_una == tp->snd_max)) {
5052 /* Nothing outstanding .. nothing to do */
5056 * Retransmission timer went off. Message has not been acked within
5057 * retransmit interval. Back off to a longer retransmit interval
5058 * and retransmit one segment.
5060 if (bbr_progress_timeout_check(bbr)) {
5062 tcp_set_inp_to_drop(bbr->rc_inp, ETIMEDOUT);
5066 if ((bbr->r_ctl.rc_resend == NULL) ||
5067 ((bbr->r_ctl.rc_resend->r_flags & BBR_RWND_COLLAPSED) == 0)) {
5069 * If the rwnd collapsed on
5070 * the one we are retransmitting
5071 * it does not count against the
5076 if (tp->t_rxtshift > TCP_MAXRXTSHIFT) {
5077 tp->t_rxtshift = TCP_MAXRXTSHIFT;
5078 KMOD_TCPSTAT_INC(tcps_timeoutdrop);
5080 tcp_set_inp_to_drop(bbr->rc_inp,
5081 (tp->t_softerror ? (uint16_t) tp->t_softerror : ETIMEDOUT));
5084 if (tp->t_state == TCPS_SYN_SENT) {
5086 * If the SYN was retransmitted, indicate CWND to be limited
5087 * to 1 segment in cc_conn_init().
5090 } else if (tp->t_rxtshift == 1) {
5092 * first retransmit; record ssthresh and cwnd so they can be
5093 * recovered if this turns out to be a "bad" retransmit. A
5094 * retransmit is considered "bad" if an ACK for this segment
5095 * is received within RTT/2 interval; the assumption here is
5096 * that the ACK was already in flight. See "On Estimating
5097 * End-to-End Network Path Properties" by Allman and Paxson
5100 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options;
5101 if (!IN_RECOVERY(tp->t_flags)) {
5102 tp->snd_cwnd_prev = tp->snd_cwnd;
5103 tp->snd_ssthresh_prev = tp->snd_ssthresh;
5104 tp->snd_recover_prev = tp->snd_recover;
5105 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
5106 tp->t_flags |= TF_PREVVALID;
5108 tp->t_flags &= ~TF_PREVVALID;
5110 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options;
5112 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options;
5113 tp->t_flags &= ~TF_PREVVALID;
5115 KMOD_TCPSTAT_INC(tcps_rexmttimeo);
5116 if ((tp->t_state == TCPS_SYN_SENT) ||
5117 (tp->t_state == TCPS_SYN_RECEIVED))
5118 rexmt = USEC_2_TICKS(BBR_INITIAL_RTO) * tcp_backoff[tp->t_rxtshift];
5120 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
5121 TCPT_RANGESET(tp->t_rxtcur, rexmt,
5122 MSEC_2_TICKS(bbr->r_ctl.rc_min_rto_ms),
5123 MSEC_2_TICKS(((uint32_t)bbr->rc_max_rto_sec) * 1000));
5125 * We enter the path for PLMTUD if connection is established or, if
5126 * connection is FIN_WAIT_1 status, reason for the last is that if
5127 * amount of data we send is very small, we could send it in couple
5128 * of packets and process straight to FIN. In that case we won't
5129 * catch ESTABLISHED state.
5132 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? true : false;
5136 if (((V_tcp_pmtud_blackhole_detect == 1) ||
5137 (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) ||
5138 (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) &&
5139 ((tp->t_state == TCPS_ESTABLISHED) ||
5140 (tp->t_state == TCPS_FIN_WAIT_1))) {
5143 * Idea here is that at each stage of mtu probe (usually,
5144 * 1448 -> 1188 -> 524) should be given 2 chances to recover
5145 * before further clamping down. 'tp->t_rxtshift % 2 == 0'
5146 * should take care of that.
5148 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) ==
5149 (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) &&
5150 (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 &&
5151 tp->t_rxtshift % 2 == 0)) {
5153 * Enter Path MTU Black-hole Detection mechanism: -
5154 * Disable Path MTU Discovery (IP "DF" bit). -
5155 * Reduce MTU to lower value than what we negotiated
5158 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
5160 * Record that we may have found a black
5163 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
5164 /* Keep track of previous MSS. */
5165 tp->t_pmtud_saved_maxseg = tp->t_maxseg;
5168 * Reduce the MSS to blackhole value or to the
5169 * default in an attempt to retransmit.
5172 isipv6 = bbr->r_is_v6;
5174 tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) {
5175 /* Use the sysctl tuneable blackhole MSS. */
5176 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
5177 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
5178 } else if (isipv6) {
5179 /* Use the default MSS. */
5180 tp->t_maxseg = V_tcp_v6mssdflt;
5182 * Disable Path MTU Discovery when we switch
5185 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
5186 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
5189 #if defined(INET6) && defined(INET)
5193 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) {
5194 /* Use the sysctl tuneable blackhole MSS. */
5195 tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
5196 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
5198 /* Use the default MSS. */
5199 tp->t_maxseg = V_tcp_mssdflt;
5201 * Disable Path MTU Discovery when we switch
5204 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
5205 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
5210 * If further retransmissions are still unsuccessful
5211 * with a lowered MTU, maybe this isn't a blackhole
5212 * and we restore the previous MSS and blackhole
5213 * detection flags. The limit '6' is determined by
5214 * giving each probe stage (1448, 1188, 524) 2
5215 * chances to recover.
5217 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
5218 (tp->t_rxtshift >= 6)) {
5219 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
5220 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
5221 tp->t_maxseg = tp->t_pmtud_saved_maxseg;
5222 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed);
5227 * Disable RFC1323 and SACK if we haven't got any response to our
5228 * third SYN to work-around some broken terminal servers (most of
5229 * which have hopefully been retired) that have bad VJ header
5230 * compression code which trashes TCP segments containing
5231 * unknown-to-them TCP options.
5233 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
5234 (tp->t_rxtshift == 3))
5235 tp->t_flags &= ~(TF_REQ_SCALE | TF_REQ_TSTMP | TF_SACK_PERMIT);
5237 * If we backed off this far, our srtt estimate is probably bogus.
5238 * Clobber it so we'll take the next rtt measurement as our srtt;
5239 * move the current srtt into rttvar to keep the current retransmit
5242 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
5245 in6_losing(tp->t_inpcb);
5248 in_losing(tp->t_inpcb);
5249 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
5252 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
5253 tp->snd_recover = tp->snd_max;
5254 tp->t_flags |= TF_ACKNOW;
5261 bbr_process_timers(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, uint8_t hpts_calling)
5264 int32_t timers = (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK);
5269 if (tp->t_state == TCPS_LISTEN) {
5270 /* no timers on listen sockets */
5271 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)
5275 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) {
5278 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
5280 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling);
5283 if (hpts_calling == 0) {
5285 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling);
5289 * Ok our timer went off early and we are not paced false
5290 * alarm, go back to sleep.
5292 left = bbr->r_ctl.rc_timer_exp - cts;
5294 bbr_log_to_processing(bbr, cts, ret, left, hpts_calling);
5295 tcp_hpts_insert(tp->t_inpcb, HPTS_USEC_TO_SLOTS(left));
5298 bbr->rc_tmr_stopped = 0;
5299 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK;
5300 if (timers & PACE_TMR_DELACK) {
5301 ret = bbr_timeout_delack(tp, bbr, cts);
5302 } else if (timers & PACE_TMR_PERSIT) {
5303 ret = bbr_timeout_persist(tp, bbr, cts);
5304 } else if (timers & PACE_TMR_RACK) {
5305 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
5306 ret = bbr_timeout_rack(tp, bbr, cts);
5307 } else if (timers & PACE_TMR_TLP) {
5308 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
5309 ret = bbr_timeout_tlp(tp, bbr, cts);
5310 } else if (timers & PACE_TMR_RXT) {
5311 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
5312 ret = bbr_timeout_rxt(tp, bbr, cts);
5313 } else if (timers & PACE_TMR_KEEP) {
5314 ret = bbr_timeout_keepalive(tp, bbr, cts);
5316 bbr_log_to_processing(bbr, cts, ret, timers, hpts_calling);
5321 bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts)
5323 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
5324 uint8_t hpts_removed = 0;
5326 if (bbr->rc_inp->inp_in_hpts &&
5327 (bbr->rc_timer_first == 1)) {
5329 * If we are canceling timer's when we have the
5330 * timer ahead of the output being paced. We also
5331 * must remove ourselves from the hpts.
5334 tcp_hpts_remove(bbr->rc_inp, HPTS_REMOVE_OUTPUT);
5335 if (bbr->r_ctl.rc_last_delay_val) {
5336 /* Update the last hptsi delay too */
5337 uint32_t time_since_send;
5339 if (TSTMP_GT(cts, bbr->rc_pacer_started))
5340 time_since_send = cts - bbr->rc_pacer_started;
5342 time_since_send = 0;
5343 if (bbr->r_ctl.rc_last_delay_val > time_since_send) {
5344 /* Cut down our slot time */
5345 bbr->r_ctl.rc_last_delay_val -= time_since_send;
5347 bbr->r_ctl.rc_last_delay_val = 0;
5349 bbr->rc_pacer_started = cts;
5352 bbr->rc_timer_first = 0;
5353 bbr_log_to_cancel(bbr, line, cts, hpts_removed);
5354 bbr->rc_tmr_stopped = bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
5355 bbr->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK);
5360 bbr_timer_stop(struct tcpcb *tp, uint32_t timer_type)
5362 struct tcp_bbr *bbr;
5364 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
5365 bbr->rc_all_timers_stopped = 1;
5370 * stop all timers always returning 0.
5373 bbr_stopall(struct tcpcb *tp)
5379 bbr_timer_activate(struct tcpcb *tp, uint32_t timer_type, uint32_t delta)
5385 * return true if a bbr timer (rack or tlp) is active.
5388 bbr_timer_active(struct tcpcb *tp, uint32_t timer_type)
5394 bbr_get_earliest_send_outstanding(struct tcp_bbr *bbr, struct bbr_sendmap *u_rsm, uint32_t cts)
5396 struct bbr_sendmap *rsm;
5398 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
5399 if ((rsm == NULL) || (u_rsm == rsm))
5401 return(rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]);
5405 bbr_update_rsm(struct tcpcb *tp, struct tcp_bbr *bbr,
5406 struct bbr_sendmap *rsm, uint32_t cts, uint32_t pacing_time)
5412 if (rsm->r_rtr_cnt > BBR_NUM_OF_RETRANS) {
5413 rsm->r_rtr_cnt = BBR_NUM_OF_RETRANS;
5414 rsm->r_flags |= BBR_OVERMAX;
5416 if (rsm->r_flags & BBR_RWND_COLLAPSED) {
5417 /* Take off the collapsed flag at rxt */
5418 rsm->r_flags &= ~BBR_RWND_COLLAPSED;
5420 if (rsm->r_flags & BBR_MARKED_LOST) {
5421 /* We have retransmitted, its no longer lost */
5422 rsm->r_flags &= ~BBR_MARKED_LOST;
5423 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
5425 if (rsm->r_flags & BBR_RXT_CLEARED) {
5427 * We hit a RXT timer on it and
5428 * we cleared the "acked" flag.
5429 * We now have it going back into
5430 * flight, we can remove the cleared
5431 * flag and possibly do accounting on
5434 rsm->r_flags &= ~BBR_RXT_CLEARED;
5436 if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & BBR_TLP) == 0)) {
5437 bbr->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start);
5438 rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start);
5440 idx = rsm->r_rtr_cnt - 1;
5441 rsm->r_tim_lastsent[idx] = cts;
5442 rsm->r_pacing_delay = pacing_time;
5443 rsm->r_delivered = bbr->r_ctl.rc_delivered;
5444 rsm->r_ts_valid = bbr->rc_ts_valid;
5445 if (bbr->rc_ts_valid)
5446 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts;
5447 if (bbr->r_ctl.r_app_limited_until)
5448 rsm->r_app_limited = 1;
5450 rsm->r_app_limited = 0;
5451 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW)
5452 rsm->r_bbr_state = bbr_state_val(bbr);
5454 rsm->r_bbr_state = 8;
5455 if (rsm->r_flags & BBR_ACKED) {
5456 /* Problably MTU discovery messing with us */
5459 old_flags = rsm->r_flags;
5460 rsm->r_flags &= ~BBR_ACKED;
5461 bbr_log_type_rsmclear(bbr, cts, rsm, old_flags, __LINE__);
5462 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
5463 if (bbr->r_ctl.rc_sacked == 0)
5464 bbr->r_ctl.rc_sacklast = NULL;
5466 if (rsm->r_in_tmap) {
5467 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
5469 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
5471 if (rsm->r_flags & BBR_SACK_PASSED) {
5472 /* We have retransmitted due to the SACK pass */
5473 rsm->r_flags &= ~BBR_SACK_PASSED;
5474 rsm->r_flags |= BBR_WAS_SACKPASS;
5476 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts);
5477 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp,
5478 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
5479 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next);
5480 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) {
5482 rsm->r_is_drain = 0;
5483 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) {
5484 rsm->r_is_drain = 1;
5487 rsm->r_is_drain = 0;
5490 rsm->r_del_time = bbr->r_ctl.rc_del_time; /* TEMP GOOGLE CODE */
5494 * Returns 0, or the sequence where we stopped
5495 * updating. We also update the lenp to be the amount
5500 bbr_update_entry(struct tcpcb *tp, struct tcp_bbr *bbr,
5501 struct bbr_sendmap *rsm, uint32_t cts, int32_t *lenp, uint32_t pacing_time)
5504 * We (re-)transmitted starting at rsm->r_start for some length
5505 * (possibly less than r_end.
5507 struct bbr_sendmap *nrsm;
5512 c_end = rsm->r_start + len;
5513 if (SEQ_GEQ(c_end, rsm->r_end)) {
5515 * We retransmitted the whole piece or more than the whole
5516 * slopping into the next rsm.
5518 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time);
5519 if (c_end == rsm->r_end) {
5525 /* Hangs over the end return whats left */
5526 act_len = rsm->r_end - rsm->r_start;
5527 *lenp = (len - act_len);
5528 return (rsm->r_end);
5530 /* We don't get out of this block. */
5533 * Here we retransmitted less than the whole thing which means we
5534 * have to split this into what was transmitted and what was not.
5536 nrsm = bbr_alloc_full_limit(bbr);
5542 * So here we are going to take the original rsm and make it what we
5543 * retransmitted. nrsm will be the tail portion we did not
5544 * retransmit. For example say the chunk was 1, 11 (10 bytes). And
5545 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to
5546 * 1, 6 and the new piece will be 6, 11.
5548 bbr_clone_rsm(bbr, nrsm, rsm, c_end);
5549 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
5551 if (rsm->r_in_tmap) {
5552 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
5553 nrsm->r_in_tmap = 1;
5555 rsm->r_flags &= (~BBR_HAS_FIN);
5556 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time);
5562 bbr_get_hardware_rate(struct tcp_bbr *bbr)
5566 bw = bbr_get_bw(bbr);
5567 bw *= (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN];
5568 bw /= (uint64_t)BBR_UNIT;
5573 bbr_setup_less_of_rate(struct tcp_bbr *bbr, uint32_t cts,
5574 uint64_t act_rate, uint64_t rate_wanted)
5577 * We could not get a full gains worth
5580 if (get_filter_value(&bbr->r_ctl.rc_delrate) >= act_rate) {
5581 /* we can't even get the real rate */
5585 bbr->gain_is_limited = 0;
5586 red = get_filter_value(&bbr->r_ctl.rc_delrate) - act_rate;
5588 filter_reduce_by(&bbr->r_ctl.rc_delrate, red, cts);
5590 /* We can use a lower gain */
5592 bbr->gain_is_limited = 1;
5597 bbr_update_hardware_pacing_rate(struct tcp_bbr *bbr, uint32_t cts)
5599 const struct tcp_hwrate_limit_table *nrte;
5600 int error, rate = -1;
5602 if (bbr->r_ctl.crte == NULL)
5604 if ((bbr->rc_inp->inp_route.ro_rt == NULL) ||
5605 (bbr->rc_inp->inp_route.ro_rt->rt_ifp == NULL)) {
5606 /* Lost our routes? */
5607 /* Clear the way for a re-attempt */
5608 bbr->bbr_attempt_hdwr_pace = 0;
5610 bbr->gain_is_limited = 0;
5612 bbr->bbr_hdrw_pacing = 0;
5613 counter_u64_add(bbr_flows_whdwr_pacing, -1);
5614 counter_u64_add(bbr_flows_nohdwr_pacing, 1);
5615 tcp_bbr_tso_size_check(bbr, cts);
5618 rate = bbr_get_hardware_rate(bbr);
5619 nrte = tcp_chg_pacing_rate(bbr->r_ctl.crte,
5621 bbr->rc_inp->inp_route.ro_rt->rt_ifp,
5623 (RS_PACING_GEQ|RS_PACING_SUB_OK),
5628 if (nrte != bbr->r_ctl.crte) {
5629 bbr->r_ctl.crte = nrte;
5631 BBR_STAT_INC(bbr_hdwr_rl_mod_ok);
5632 if (bbr->r_ctl.crte->rate < rate) {
5633 /* We have a problem */
5634 bbr_setup_less_of_rate(bbr, cts,
5635 bbr->r_ctl.crte->rate, rate);
5638 bbr->gain_is_limited = 0;
5642 /* A failure should release the tag */
5643 BBR_STAT_INC(bbr_hdwr_rl_mod_fail);
5644 bbr->gain_is_limited = 0;
5646 bbr->bbr_hdrw_pacing = 0;
5648 bbr_type_log_hdwr_pacing(bbr,
5649 bbr->r_ctl.crte->ptbl->rs_ifp,
5651 ((bbr->r_ctl.crte == NULL) ? 0 : bbr->r_ctl.crte->rate),
5659 bbr_adjust_for_hw_pacing(struct tcp_bbr *bbr, uint32_t cts)
5662 * If we have hardware pacing support
5663 * we need to factor that in for our
5666 const struct tcp_hwrate_limit_table *rlp;
5667 uint32_t cur_delay, seg_sz, maxseg, new_tso, delta, hdwr_delay;
5669 if ((bbr->bbr_hdrw_pacing == 0) ||
5670 (IN_RECOVERY(bbr->rc_tp->t_flags)) ||
5671 (bbr->r_ctl.crte == NULL))
5673 if (bbr->hw_pacing_set == 0) {
5674 /* Not yet by the hdwr pacing count delay */
5677 if (bbr_hdwr_pace_adjust == 0) {
5681 rlp = bbr->r_ctl.crte;
5682 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options)
5683 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
5685 maxseg = BBR_MIN_SEG - bbr->rc_last_options;
5687 * So lets first get the
5688 * time we will take between
5689 * TSO sized sends currently without
5692 cur_delay = bbr_get_pacing_delay(bbr, BBR_UNIT,
5693 bbr->r_ctl.rc_pace_max_segs, cts, 1);
5694 hdwr_delay = bbr->r_ctl.rc_pace_max_segs / maxseg;
5695 hdwr_delay *= rlp->time_between;
5696 if (cur_delay > hdwr_delay)
5697 delta = cur_delay - hdwr_delay;
5700 bbr_log_type_tsosize(bbr, cts, delta, cur_delay, hdwr_delay,
5701 (bbr->r_ctl.rc_pace_max_segs / maxseg),
5704 (delta < (max(rlp->time_between,
5705 bbr->r_ctl.bbr_hptsi_segments_delay_tar)))) {
5707 * Now lets divide by the pacing
5708 * time between each segment the
5709 * hardware sends rounding up and
5710 * derive a bytes from that. We multiply
5711 * that by bbr_hdwr_pace_adjust to get
5712 * more bang for our buck.
5714 * The goal is to have the software pacer
5715 * waiting no more than an additional
5716 * pacing delay if we can (without the
5717 * compensation i.e. x bbr_hdwr_pace_adjust).
5719 seg_sz = max(((cur_delay + rlp->time_between)/rlp->time_between),
5720 (bbr->r_ctl.rc_pace_max_segs/maxseg));
5721 seg_sz *= bbr_hdwr_pace_adjust;
5722 if (bbr_hdwr_pace_floor &&
5723 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) {
5724 /* Currently hardware paces
5725 * out rs_min_seg segments at a time.
5726 * We need to make sure we always send at least
5727 * a full burst of bbr_hdwr_pace_floor down.
5729 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg;
5732 } else if (delta == 0) {
5734 * The highest pacing rate is
5735 * above our b/w gained. This means
5736 * we probably are going quite fast at
5737 * the hardware highest rate. Lets just multiply
5738 * the calculated TSO size by the
5739 * multiplier factor (its probably
5740 * 4 segments in the default config for
5743 seg_sz = bbr->r_ctl.rc_pace_max_segs * bbr_hdwr_pace_adjust;
5744 if (bbr_hdwr_pace_floor &&
5745 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) {
5746 /* Currently hardware paces
5747 * out rs_min_seg segments at a time.
5748 * We need to make sure we always send at least
5749 * a full burst of bbr_hdwr_pace_floor down.
5751 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg;
5755 * The pacing time difference is so
5756 * big that the hardware will
5757 * pace out more rapidly then we
5758 * really want and then we
5759 * will have a long delay. Lets just keep
5760 * the same TSO size so its as if
5761 * we were not using hdwr pacing (we
5762 * just gain a bit of spacing from the
5763 * hardware if seg_sz > 1).
5765 seg_sz = bbr->r_ctl.rc_pace_max_segs;
5767 if (seg_sz > bbr->r_ctl.rc_pace_max_segs)
5770 new_tso = bbr->r_ctl.rc_pace_max_segs;
5771 if (new_tso >= (PACE_MAX_IP_BYTES-maxseg))
5772 new_tso = PACE_MAX_IP_BYTES - maxseg;
5774 if (new_tso != bbr->r_ctl.rc_pace_max_segs) {
5775 bbr_log_type_tsosize(bbr, cts, new_tso, 0, bbr->r_ctl.rc_pace_max_segs, maxseg, 0);
5776 bbr->r_ctl.rc_pace_max_segs = new_tso;
5781 tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts)
5784 uint32_t old_tso = 0, new_tso;
5785 uint32_t maxseg, bytes;
5788 * Google/linux uses the following algorithm to determine
5789 * the TSO size based on the b/w of the link (from Neal Cardwell email 9/27/18):
5791 * bytes = bw_in_bytes_per_second / 1000
5792 * bytes = min(bytes, 64k)
5793 * tso_segs = bytes / MSS
5798 * tso_segs = max(tso_segs, min_tso_segs)
5800 * * Note apply a device specific limit (we apply this in the
5802 * Note that before the initial measurement is made google bursts out
5803 * a full iwnd just like new-reno/cubic.
5805 * We do not use this algorithm. Instead we
5806 * use a two phased approach:
5808 * if ( bw <= per-tcb-cross-over)
5809 * goal_tso = calculate how much with this bw we
5810 * can send in goal-time seconds.
5811 * if (goal_tso > mss)
5812 * seg = goal_tso / mss
5816 * if (tso > per-tcb-max)
5818 * else if ( bw > 512Mbps)
5819 * tso = max-tso (64k/mss)
5821 * goal_tso = bw / per-tcb-divsor
5822 * seg = (goal_tso + mss-1)/mss
5825 * if (tso < per-tcb-floor)
5826 * tso = per-tcb-floor
5827 * if (tso > per-tcb-utter_max)
5828 * tso = per-tcb-utter_max
5830 * Note the default per-tcb-divisor is 1000 (same as google).
5831 * the goal cross over is 30Mbps however. To recreate googles
5832 * algorithm you need to set:
5834 * cross-over = 23,168,000 bps
5837 * per-tcb-divisor = 1000
5840 * This will get you "google bbr" behavior with respect to tso size.
5842 * Note we do set anything TSO size until we are past the initial
5843 * window. Before that we gnerally use either a single MSS
5844 * or we use the full IW size (so we burst a IW at a time)
5845 * Also note that Hardware-TLS is special and does alternate
5846 * things to minimize PCI Bus Bandwidth use.
5849 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options) {
5850 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
5852 maxseg = BBR_MIN_SEG - bbr->rc_last_options;
5855 if (bbr->rc_inp->inp_socket->so_snd.sb_flags & SB_TLS_IFNET) {
5856 tls_seg = ctf_get_opt_tls_size(bbr->rc_inp->inp_socket, bbr->rc_tp->snd_wnd);
5857 bbr->r_ctl.rc_pace_min_segs = (tls_seg + bbr->rc_last_options);
5860 old_tso = bbr->r_ctl.rc_pace_max_segs;
5861 if (bbr->rc_past_init_win == 0) {
5863 * Not enough data has been acknowledged to make a
5864 * judgement unless we are hardware TLS. Set up
5865 * the initial TSO based on if we are sending a
5866 * full IW at once or not.
5868 if (bbr->rc_use_google)
5869 bbr->r_ctl.rc_pace_max_segs = ((bbr->rc_tp->t_maxseg - bbr->rc_last_options) * 2);
5870 else if (bbr->bbr_init_win_cheat)
5871 bbr->r_ctl.rc_pace_max_segs = bbr_initial_cwnd(bbr, bbr->rc_tp);
5873 bbr->r_ctl.rc_pace_max_segs = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
5874 if (bbr->r_ctl.rc_pace_min_segs != bbr->rc_tp->t_maxseg)
5875 bbr->r_ctl.rc_pace_min_segs = bbr->rc_tp->t_maxseg;
5877 if ((bbr->rc_inp->inp_socket->so_snd.sb_flags & SB_TLS_IFNET) && tls_seg) {
5879 * For hardware TLS we set our min to the tls_seg size.
5881 bbr->r_ctl.rc_pace_max_segs = tls_seg;
5882 bbr->r_ctl.rc_pace_min_segs = tls_seg + bbr->rc_last_options;
5885 if (bbr->r_ctl.rc_pace_max_segs == 0) {
5886 bbr->r_ctl.rc_pace_max_segs = maxseg;
5888 bbr_log_type_tsosize(bbr, cts, bbr->r_ctl.rc_pace_max_segs, tls_seg, old_tso, maxseg, 0);
5890 if ((bbr->rc_inp->inp_socket->so_snd.sb_flags & SB_TLS_IFNET) == 0)
5892 bbr_adjust_for_hw_pacing(bbr, cts);
5896 * Now lets set the TSO goal based on our delivery rate in
5897 * bytes per second. Note we only do this if
5898 * we have acked at least the initial cwnd worth of data.
5900 bw = bbr_get_bw(bbr);
5901 if (IN_RECOVERY(bbr->rc_tp->t_flags) &&
5902 (bbr->rc_use_google == 0)) {
5903 /* We clamp to one MSS in recovery */
5905 } else if (bbr->rc_use_google) {
5908 /* Google considers the gain too */
5909 if (bbr->r_ctl.rc_bbr_hptsi_gain != BBR_UNIT) {
5910 bw *= bbr->r_ctl.rc_bbr_hptsi_gain;
5914 if (bytes > (64 * 1024))
5916 new_tso = bytes / maxseg;
5917 if (bw < ONE_POINT_TWO_MEG)
5921 if (new_tso < min_tso_segs)
5922 new_tso = min_tso_segs;
5924 } else if (bbr->rc_no_pacing) {
5925 new_tso = (PACE_MAX_IP_BYTES / maxseg) * maxseg;
5926 } else if (bw <= bbr->r_ctl.bbr_cross_over) {
5928 * Calculate the worse case b/w TSO if we are inserting no
5929 * more than a delay_target number of TSO's.
5931 uint32_t tso_len, min_tso;
5933 tso_len = bbr_get_pacing_length(bbr, BBR_UNIT, bbr->r_ctl.bbr_hptsi_segments_delay_tar, bw);
5934 if (tso_len > maxseg) {
5935 new_tso = tso_len / maxseg;
5936 if (new_tso > bbr->r_ctl.bbr_hptsi_segments_max)
5937 new_tso = bbr->r_ctl.bbr_hptsi_segments_max;
5941 * less than a full sized frame yikes.. long rtt or
5944 min_tso = bbr_minseg(bbr);
5945 if ((tso_len > min_tso) && (bbr_all_get_min == 0))
5946 new_tso = rounddown(tso_len, min_tso);
5950 } else if (bw > FIVETWELVE_MBPS) {
5952 * This guy is so fast b/w wise that we can TSO as large as
5953 * possible of segments that the NIC will allow.
5955 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg);
5958 * This formula is based on attempting to send a segment or
5959 * more every bbr_hptsi_per_second. The default is 1000
5960 * which means you are targeting what you can send every 1ms
5961 * based on the peers bw.
5963 * If the number drops to say 500, then you are looking more
5964 * at 2ms and you will raise how much we send in a single
5965 * TSO thus saving CPU (less bbr_output_wtime() calls). The
5966 * trade off of course is you will send more at once and
5967 * thus tend to clump up the sends into larger "bursts"
5970 bw /= bbr->r_ctl.bbr_hptsi_per_second;
5971 new_tso = roundup(bw, (uint64_t)maxseg);
5973 * Gate the floor to match what our lower than 48Mbps
5974 * algorithm does. The ceiling (bbr_hptsi_segments_max) thus
5975 * becomes the floor for this calculation.
5977 if (new_tso < (bbr->r_ctl.bbr_hptsi_segments_max * maxseg))
5978 new_tso = (bbr->r_ctl.bbr_hptsi_segments_max * maxseg);
5980 if (bbr->r_ctl.bbr_hptsi_segments_floor && (new_tso < (maxseg * bbr->r_ctl.bbr_hptsi_segments_floor)))
5981 new_tso = maxseg * bbr->r_ctl.bbr_hptsi_segments_floor;
5982 if (new_tso > PACE_MAX_IP_BYTES)
5983 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg);
5984 /* Enforce an utter maximum if we are not HW-TLS */
5986 if ((bbr->rc_inp->inp_socket->so_snd.sb_flags & SB_TLS_IFNET) == 0)
5988 if (bbr->r_ctl.bbr_utter_max && (new_tso > (bbr->r_ctl.bbr_utter_max * maxseg))) {
5989 new_tso = bbr->r_ctl.bbr_utter_max * maxseg;
5994 * Lets move the output size
5995 * up to 1 or more TLS record sizes.
5999 temp = roundup(new_tso, tls_seg);
6001 /* Back down if needed to under a full frame */
6002 while (new_tso > PACE_MAX_IP_BYTES)
6006 if (old_tso != new_tso) {
6007 /* Only log changes */
6008 bbr_log_type_tsosize(bbr, cts, new_tso, tls_seg, old_tso, maxseg, 0);
6009 bbr->r_ctl.rc_pace_max_segs = new_tso;
6012 if ((bbr->rc_inp->inp_socket->so_snd.sb_flags & SB_TLS_IFNET) &&
6014 bbr->r_ctl.rc_pace_min_segs = tls_seg + bbr->rc_last_options;
6017 /* We have hardware pacing and not hardware TLS! */
6018 bbr_adjust_for_hw_pacing(bbr, cts);
6022 bbr_log_output(struct tcp_bbr *bbr, struct tcpcb *tp, struct tcpopt *to, int32_t len,
6023 uint32_t seq_out, uint8_t th_flags, int32_t err, uint32_t cts,
6024 struct mbuf *mb, int32_t * abandon, struct bbr_sendmap *hintrsm, uint32_t delay_calc,
6028 struct bbr_sendmap *rsm, *nrsm;
6029 register uint32_t snd_max, snd_una;
6030 uint32_t pacing_time;
6032 * Add to the RACK log of packets in flight or retransmitted. If
6033 * there is a TS option we will use the TS echoed, if not we will
6036 * Retransmissions will increment the count and move the ts to its
6037 * proper place. Note that if options do not include TS's then we
6038 * won't be able to effectively use the ACK for an RTT on a retran.
6040 * Notes about r_start and r_end. Lets consider a send starting at
6041 * sequence 1 for 10 bytes. In such an example the r_start would be
6042 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11.
6043 * This means that r_end is actually the first sequence for the next
6047 INP_WLOCK_ASSERT(tp->t_inpcb);
6050 * We don't log errors -- we could but snd_max does not
6051 * advance in this case either.
6055 if (th_flags & TH_RST) {
6057 * We don't log resets and we return immediately from
6063 snd_una = tp->snd_una;
6064 if (th_flags & (TH_SYN | TH_FIN) && (hintrsm == NULL)) {
6066 * The call to bbr_log_output is made before bumping
6067 * snd_max. This means we can record one extra byte on a SYN
6068 * or FIN if seq_out is adding more on and a FIN is present
6069 * (and we are not resending).
6071 if (th_flags & TH_SYN)
6073 if (th_flags & TH_FIN)
6076 if (SEQ_LEQ((seq_out + len), snd_una)) {
6077 /* Are sending an old segment to induce an ack (keep-alive)? */
6080 if (SEQ_LT(seq_out, snd_una)) {
6081 /* huh? should we panic? */
6084 end = seq_out + len;
6086 len = end - seq_out;
6088 snd_max = tp->snd_max;
6090 /* We don't log zero window probes */
6093 pacing_time = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, len, cts, 1);
6094 /* First question is it a retransmission? */
6095 if (seq_out == snd_max) {
6097 rsm = bbr_alloc(bbr);
6102 if (th_flags & TH_SYN)
6103 rsm->r_flags |= BBR_HAS_SYN;
6104 if (th_flags & TH_FIN)
6105 rsm->r_flags |= BBR_HAS_FIN;
6106 rsm->r_tim_lastsent[0] = cts;
6108 rsm->r_rtr_bytes = 0;
6109 rsm->r_start = seq_out;
6110 rsm->r_end = rsm->r_start + len;
6112 rsm->r_delivered = bbr->r_ctl.rc_delivered;
6113 rsm->r_pacing_delay = pacing_time;
6114 rsm->r_ts_valid = bbr->rc_ts_valid;
6115 if (bbr->rc_ts_valid)
6116 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts;
6117 rsm->r_del_time = bbr->r_ctl.rc_del_time;
6118 if (bbr->r_ctl.r_app_limited_until)
6119 rsm->r_app_limited = 1;
6121 rsm->r_app_limited = 0;
6122 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts);
6123 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp,
6124 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
6126 * Here we must also add in this rsm since snd_max
6127 * is updated after we return from a new send.
6129 rsm->r_flight_at_send += len;
6130 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next);
6131 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
6133 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW)
6134 rsm->r_bbr_state = bbr_state_val(bbr);
6136 rsm->r_bbr_state = 8;
6137 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) {
6139 rsm->r_is_drain = 0;
6140 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) {
6141 rsm->r_is_drain = 1;
6144 rsm->r_is_drain = 0;
6150 * If we reach here its a retransmission and we need to find it.
6153 if (hintrsm && (hintrsm->r_start == seq_out)) {
6156 } else if (bbr->r_ctl.rc_next) {
6157 /* We have a hint from a previous run */
6158 rsm = bbr->r_ctl.rc_next;
6160 /* No hints sorry */
6163 if ((rsm) && (rsm->r_start == seq_out)) {
6165 * We used rc_next or hintrsm to retransmit, hopefully the
6168 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time);
6175 /* Ok it was not the last pointer go through it the hard way. */
6176 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
6177 if (rsm->r_start == seq_out) {
6178 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time);
6179 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next);
6186 if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) {
6187 /* Transmitted within this piece */
6189 * Ok we must split off the front and then let the
6190 * update do the rest
6192 nrsm = bbr_alloc_full_limit(bbr);
6194 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time);
6198 * copy rsm to nrsm and then trim the front of rsm
6199 * to not include this part.
6201 bbr_clone_rsm(bbr, nrsm, rsm, seq_out);
6202 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
6203 if (rsm->r_in_tmap) {
6204 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
6205 nrsm->r_in_tmap = 1;
6207 rsm->r_flags &= (~BBR_HAS_FIN);
6208 seq_out = bbr_update_entry(tp, bbr, nrsm, cts, &len, pacing_time);
6215 * Hmm not found in map did they retransmit both old and on into the
6218 if (seq_out == tp->snd_max) {
6220 } else if (SEQ_LT(seq_out, tp->snd_max)) {
6221 #ifdef BBR_INVARIANTS
6222 printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n",
6223 seq_out, len, tp->snd_una, tp->snd_max);
6224 printf("Starting Dump of all rack entries\n");
6225 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
6226 printf("rsm:%p start:%u end:%u\n",
6227 rsm, rsm->r_start, rsm->r_end);
6229 printf("Dump complete\n");
6230 panic("seq_out not found rack:%p tp:%p",
6234 #ifdef BBR_INVARIANTS
6236 * Hmm beyond sndmax? (only if we are using the new rtt-pack
6239 panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p",
6240 seq_out, len, tp->snd_max, tp);
6246 bbr_collapse_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, int32_t rtt)
6249 * Collapse timeout back the cum-ack moved.
6252 tp->t_softerror = 0;
6257 tcp_bbr_xmit_timer(struct tcp_bbr *bbr, uint32_t rtt_usecs, uint32_t rsm_send_time, uint32_t r_start, uint32_t tsin)
6260 bbr->r_ctl.cur_rtt = rtt_usecs;
6261 bbr->r_ctl.ts_in = tsin;
6263 bbr->r_ctl.cur_rtt_send_time = rsm_send_time;
6267 bbr_make_timestamp_determination(struct tcp_bbr *bbr)
6270 * We have in our bbr control:
6271 * 1) The timestamp we started observing cum-acks (bbr->r_ctl.bbr_ts_check_tstmp).
6272 * 2) Our timestamp indicating when we sent that packet (bbr->r_ctl.rsm->bbr_ts_check_our_cts).
6273 * 3) The current timestamp that just came in (bbr->r_ctl.last_inbound_ts)
6274 * 4) The time that the packet that generated that ack was sent (bbr->r_ctl.cur_rtt_send_time)
6276 * Now we can calculate the time between the sends by doing:
6278 * delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts
6280 * And the peer's time between receiving them by doing:
6282 * peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp
6284 * We want to figure out if the timestamp values are in msec, 10msec or usec.
6285 * We also may find that we can't use the timestamps if say we see
6286 * that the peer_delta indicates that though we may have taken 10ms to
6287 * pace out the data, it only saw 1ms between the two packets. This would
6288 * indicate that somewhere on the path is a batching entity that is giving
6289 * out time-slices of the actual b/w. This would mean we could not use
6290 * reliably the peers timestamps.
6292 * We expect delta > peer_delta initially. Until we figure out the
6293 * timestamp difference which we will store in bbr->r_ctl.bbr_peer_tsratio.
6294 * If we place 1000 there then its a ms vs our usec. If we place 10000 there
6295 * then its 10ms vs our usec. If the peer is running a usec clock we would
6296 * put a 1 there. If the value is faster then ours, we will disable the
6297 * use of timestamps (though we could revist this later if we find it to be not
6298 * just an isolated one or two flows)).
6300 * To detect the batching middle boxes we will come up with our compensation and
6301 * if with it in place, we find the peer is drastically off (by some margin) in
6302 * the smaller direction, then we will assume the worst case and disable use of timestamps.
6305 uint64_t delta, peer_delta, delta_up;
6307 delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts;
6308 if (delta < bbr_min_usec_delta) {
6310 * Have not seen a min amount of time
6311 * between our send times so we can
6312 * make a determination of the timestamp
6317 peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp;
6318 if (peer_delta < bbr_min_peer_delta) {
6320 * We may have enough in the form of
6321 * our delta but the peers number
6322 * has not changed that much. It could
6323 * be its clock ratio is such that
6324 * we need more data (10ms tick) or
6325 * there may be other compression scenarios
6326 * going on. In any event we need the
6327 * spread to be larger.
6331 /* Ok lets first see which way our delta is going */
6332 if (peer_delta > delta) {
6333 /* Very unlikely, the peer without
6334 * compensation shows that it saw
6335 * the two sends arrive further apart
6336 * then we saw then in micro-seconds.
6338 if (peer_delta < (delta + ((delta * (uint64_t)1000)/ (uint64_t)bbr_delta_percent))) {
6339 /* well it looks like the peer is a micro-second clock. */
6340 bbr->rc_ts_clock_set = 1;
6341 bbr->r_ctl.bbr_peer_tsratio = 1;
6343 bbr->rc_ts_cant_be_used = 1;
6344 bbr->rc_ts_clock_set = 1;
6348 /* Ok we know that the peer_delta is smaller than our send distance */
6349 bbr->rc_ts_clock_set = 1;
6350 /* First question is it within the percentage that they are using usec time? */
6351 delta_up = (peer_delta * 1000) / (uint64_t)bbr_delta_percent;
6352 if ((peer_delta + delta_up) >= delta) {
6353 /* Its a usec clock */
6354 bbr->r_ctl.bbr_peer_tsratio = 1;
6355 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6358 /* Ok if not usec, what about 10usec (though unlikely)? */
6359 delta_up = (peer_delta * 1000 * 10) / (uint64_t)bbr_delta_percent;
6360 if (((peer_delta * 10) + delta_up) >= delta) {
6361 bbr->r_ctl.bbr_peer_tsratio = 10;
6362 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6365 /* And what about 100usec (though again unlikely)? */
6366 delta_up = (peer_delta * 1000 * 100) / (uint64_t)bbr_delta_percent;
6367 if (((peer_delta * 100) + delta_up) >= delta) {
6368 bbr->r_ctl.bbr_peer_tsratio = 100;
6369 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6372 /* And how about 1 msec (the most likely one)? */
6373 delta_up = (peer_delta * 1000 * 1000) / (uint64_t)bbr_delta_percent;
6374 if (((peer_delta * 1000) + delta_up) >= delta) {
6375 bbr->r_ctl.bbr_peer_tsratio = 1000;
6376 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6379 /* Ok if not msec could it be 10 msec? */
6380 delta_up = (peer_delta * 1000 * 10000) / (uint64_t)bbr_delta_percent;
6381 if (((peer_delta * 10000) + delta_up) >= delta) {
6382 bbr->r_ctl.bbr_peer_tsratio = 10000;
6385 /* If we fall down here the clock tick so slowly we can't use it */
6386 bbr->rc_ts_cant_be_used = 1;
6387 bbr->r_ctl.bbr_peer_tsratio = 0;
6388 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6392 * Collect new round-trip time estimate
6393 * and update averages and current timeout.
6396 tcp_bbr_xmit_timer_commit(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t cts)
6403 if (bbr->rtt_valid == 0)
6404 /* No valid sample */
6407 rtt = bbr->r_ctl.cur_rtt;
6408 tsin = bbr->r_ctl.ts_in;
6409 if (bbr->rc_prtt_set_ts) {
6411 * We are to force feed the rttProp filter due
6412 * to an entry into PROBE_RTT. This assures
6413 * that the times are sync'd between when we
6414 * go into PROBE_RTT and the filter expiration.
6416 * Google does not use a true filter, so they do
6417 * this implicitly since they only keep one value
6418 * and when they enter probe-rtt they update the
6419 * value to the newest rtt.
6423 bbr->rc_prtt_set_ts = 0;
6424 rtt_prop = get_filter_value_small(&bbr->r_ctl.rc_rttprop);
6426 filter_increase_by_small(&bbr->r_ctl.rc_rttprop, (rtt - rtt_prop), cts);
6428 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
6430 if (bbr->rc_ack_was_delayed)
6431 rtt += bbr->r_ctl.rc_ack_hdwr_delay;
6433 if (rtt < bbr->r_ctl.rc_lowest_rtt)
6434 bbr->r_ctl.rc_lowest_rtt = rtt;
6435 bbr_log_rtt_sample(bbr, rtt, tsin);
6436 if (bbr->r_init_rtt) {
6438 * The initial rtt is not-trusted, nuke it and lets get
6439 * our first valid measurement in.
6441 bbr->r_init_rtt = 0;
6444 if ((bbr->rc_ts_clock_set == 0) && bbr->rc_ts_valid) {
6446 * So we have not yet figured out
6447 * what the peers TSTMP value is
6448 * in (most likely ms). We need a
6449 * series of cum-ack's to determine
6452 if (bbr->rc_ack_is_cumack) {
6453 if (bbr->rc_ts_data_set) {
6454 /* Lets attempt to determine the timestamp granularity. */
6455 bbr_make_timestamp_determination(bbr);
6457 bbr->rc_ts_data_set = 1;
6458 bbr->r_ctl.bbr_ts_check_tstmp = bbr->r_ctl.last_inbound_ts;
6459 bbr->r_ctl.bbr_ts_check_our_cts = bbr->r_ctl.cur_rtt_send_time;
6463 * We have to have consecutive acks
6464 * reset any "filled" state to none.
6466 bbr->rc_ts_data_set = 0;
6470 rtt_ticks = USEC_2_TICKS((rtt + (USECS_IN_MSEC - 1)));
6473 if (tp->t_srtt != 0) {
6475 * srtt is stored as fixed point with 5 bits after the
6476 * binary point (i.e., scaled by 8). The following magic is
6477 * equivalent to the smoothing algorithm in rfc793 with an
6478 * alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed point).
6479 * Adjust rtt to origin 0.
6482 delta = ((rtt_ticks - 1) << TCP_DELTA_SHIFT)
6483 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
6485 tp->t_srtt += delta;
6486 if (tp->t_srtt <= 0)
6490 * We accumulate a smoothed rtt variance (actually, a
6491 * smoothed mean difference), then set the retransmit timer
6492 * to smoothed rtt + 4 times the smoothed variance. rttvar
6493 * is stored as fixed point with 4 bits after the binary
6494 * point (scaled by 16). The following is equivalent to
6495 * rfc793 smoothing with an alpha of .75 (rttvar =
6496 * rttvar*3/4 + |delta| / 4). This replaces rfc793's
6501 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
6502 tp->t_rttvar += delta;
6503 if (tp->t_rttvar <= 0)
6505 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
6506 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
6509 * No rtt measurement yet - use the unsmoothed rtt. Set the
6510 * variance to half the rtt (so our first retransmit happens
6513 tp->t_srtt = rtt_ticks << TCP_RTT_SHIFT;
6514 tp->t_rttvar = rtt_ticks << (TCP_RTTVAR_SHIFT - 1);
6515 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
6517 KMOD_TCPSTAT_INC(tcps_rttupdated);
6520 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt_ticks));
6523 * the retransmit should happen at rtt + 4 * rttvar. Because of the
6524 * way we do the smoothing, srtt and rttvar will each average +1/2
6525 * tick of bias. When we compute the retransmit timer, we want 1/2
6526 * tick of rounding and 1 extra tick because of +-1/2 tick
6527 * uncertainty in the firing of the timer. The bias will give us
6528 * exactly the 1.5 tick we need. But, because the bias is
6529 * statistical, we have to test that we don't drop below the minimum
6530 * feasible timer (which is 2 ticks).
6532 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
6533 max(MSEC_2_TICKS(bbr->r_ctl.rc_min_rto_ms), rtt_ticks + 2),
6534 MSEC_2_TICKS(((uint32_t)bbr->rc_max_rto_sec) * 1000));
6537 * We received an ack for a packet that wasn't retransmitted; it is
6538 * probably safe to discard any error indications we've received
6539 * recently. This isn't quite right, but close enough for now (a
6540 * route might have failed after we sent a segment, and the return
6541 * path might not be symmetrical).
6543 tp->t_softerror = 0;
6544 rtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT);
6545 if (bbr->r_ctl.bbr_smallest_srtt_this_state > rtt)
6546 bbr->r_ctl.bbr_smallest_srtt_this_state = rtt;
6550 bbr_earlier_retran(struct tcpcb *tp, struct tcp_bbr *bbr, struct bbr_sendmap *rsm,
6551 uint32_t t, uint32_t cts, int ack_type)
6554 * For this RSM, we acknowledged the data from a previous
6555 * transmission, not the last one we made. This means we did a false
6558 if (rsm->r_flags & BBR_HAS_FIN) {
6560 * The sending of the FIN often is multiple sent when we
6561 * have everything outstanding ack'd. We ignore this case
6562 * since its over now.
6566 if (rsm->r_flags & BBR_TLP) {
6568 * We expect TLP's to have this occur often
6570 bbr->rc_tlp_rtx_out = 0;
6573 if (ack_type != BBR_CUM_ACKED) {
6575 * If it was not a cum-ack we
6576 * don't really know for sure since
6577 * the timestamp could be from some
6578 * other transmission.
6583 if (rsm->r_flags & BBR_WAS_SACKPASS) {
6585 * We retransmitted based on a sack and the earlier
6586 * retransmission ack'd it - re-ordering is occuring.
6588 BBR_STAT_INC(bbr_reorder_seen);
6589 bbr->r_ctl.rc_reorder_ts = cts;
6591 /* Back down the loss count */
6592 if (rsm->r_flags & BBR_MARKED_LOST) {
6593 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
6594 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
6595 rsm->r_flags &= ~BBR_MARKED_LOST;
6596 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
6597 /* LT sampling also needs adjustment */
6598 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
6600 /***** RRS HERE ************************/
6601 /* Do we need to do this??? */
6602 /* bbr_reset_lt_bw_sampling(bbr, cts); */
6603 /***** RRS HERE ************************/
6604 BBR_STAT_INC(bbr_badfr);
6605 BBR_STAT_ADD(bbr_badfr_bytes, (rsm->r_end - rsm->r_start));
6610 bbr_set_reduced_rtt(struct tcp_bbr *bbr, uint32_t cts, uint32_t line)
6612 bbr->r_ctl.rc_rtt_shrinks = cts;
6613 if (bbr_can_force_probertt &&
6614 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) &&
6615 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) {
6617 * We should enter probe-rtt its been too long
6618 * since we have been there.
6620 bbr_enter_probe_rtt(bbr, cts, __LINE__);
6622 bbr_check_probe_rtt_limits(bbr, cts);
6626 tcp_bbr_commit_bw(struct tcp_bbr *bbr, uint32_t cts)
6630 if (bbr->r_ctl.rc_bbr_cur_del_rate == 0) {
6631 /* We never apply a zero measurment */
6632 bbr_log_type_bbrupd(bbr, 20, cts, 0, 0,
6636 if (bbr->r_ctl.r_measurement_count < 0xffffffff)
6637 bbr->r_ctl.r_measurement_count++;
6638 orig_bw = get_filter_value(&bbr->r_ctl.rc_delrate);
6639 apply_filter_max(&bbr->r_ctl.rc_delrate, bbr->r_ctl.rc_bbr_cur_del_rate, bbr->r_ctl.rc_pkt_epoch);
6640 bbr_log_type_bbrupd(bbr, 21, cts, (uint32_t)orig_bw,
6641 (uint32_t)get_filter_value(&bbr->r_ctl.rc_delrate),
6644 (orig_bw != get_filter_value(&bbr->r_ctl.rc_delrate))) {
6645 if (bbr->bbr_hdrw_pacing) {
6647 * Apply a new rate to the hardware
6650 bbr_update_hardware_pacing_rate(bbr, cts);
6652 bbr_set_state_target(bbr, __LINE__);
6653 tcp_bbr_tso_size_check(bbr, cts);
6654 if (bbr->r_recovery_bw) {
6655 bbr_setup_red_bw(bbr, cts);
6656 bbr_log_type_bw_reduce(bbr, BBR_RED_BW_USELRBW);
6658 } else if ((orig_bw == 0) && get_filter_value(&bbr->r_ctl.rc_delrate))
6659 tcp_bbr_tso_size_check(bbr, cts);
6663 bbr_nf_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts)
6665 if (bbr->rc_in_persist == 0) {
6666 /* We log only when not in persist */
6667 /* Translate to a Bytes Per Second */
6668 uint64_t tim, bw, ts_diff, ts_bw;
6669 uint32_t upper, lower, delivered;
6671 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time))
6672 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time);
6676 * Now that we have processed the tim (skipping the sample
6677 * or possibly updating the time, go ahead and
6678 * calculate the cdr.
6680 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered);
6681 bw = (uint64_t)delivered;
6682 bw *= (uint64_t)USECS_IN_SECOND;
6685 /* We must have a calculatable amount */
6688 upper = (bw >> 32) & 0x00000000ffffffff;
6689 lower = bw & 0x00000000ffffffff;
6691 * If we are using this b/w shove it in now so we
6692 * can see in the trace viewer if it gets over-ridden.
6694 if (rsm->r_ts_valid &&
6696 bbr->rc_ts_clock_set &&
6697 (bbr->rc_ts_cant_be_used == 0) &&
6698 bbr->rc_use_ts_limit) {
6699 ts_diff = max((bbr->r_ctl.last_inbound_ts - rsm->r_del_ack_ts), 1);
6700 ts_diff *= bbr->r_ctl.bbr_peer_tsratio;
6701 if ((delivered == 0) ||
6703 /* Can't use the ts */
6704 bbr_log_type_bbrupd(bbr, 61, cts,
6706 bbr->r_ctl.last_inbound_ts,
6707 rsm->r_del_ack_ts, 0,
6708 0, 0, 0, delivered);
6710 ts_bw = (uint64_t)delivered;
6711 ts_bw *= (uint64_t)USECS_IN_SECOND;
6713 bbr_log_type_bbrupd(bbr, 62, cts,
6715 (ts_bw & 0xffffffff), 0, 0,
6716 0, 0, ts_diff, delivered);
6717 if ((bbr->ts_can_raise) &&
6719 bbr_log_type_bbrupd(bbr, 8, cts,
6723 (bw & 0x00000000ffffffff),
6726 } else if (ts_bw && (ts_bw < bw)) {
6727 bbr_log_type_bbrupd(bbr, 7, cts,
6731 (bw & 0x00000000ffffffff),
6737 if (rsm->r_first_sent_time &&
6738 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) {
6741 * We use what was in flight at the time of our
6742 * send and the size of this send to figure
6743 * out what we have been sending at (amount).
6744 * For the time we take from the time of
6745 * the send of the first send outstanding
6746 * until this send plus this sends pacing
6747 * time. This gives us a good calculation
6748 * as to the rate we have been sending at.
6751 sbw = (uint64_t)(rsm->r_flight_at_send);
6752 sbw *= (uint64_t)USECS_IN_SECOND;
6753 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time;
6754 sti += rsm->r_pacing_delay;
6757 bbr_log_type_bbrupd(bbr, 6, cts,
6762 rsm->r_first_sent_time, 0, (sbw >> 32),
6767 /* Use the google algorithm for b/w measurements */
6768 bbr->r_ctl.rc_bbr_cur_del_rate = bw;
6769 if ((rsm->r_app_limited == 0) ||
6770 (bw > get_filter_value(&bbr->r_ctl.rc_delrate))) {
6771 tcp_bbr_commit_bw(bbr, cts);
6772 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered,
6773 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time);
6779 bbr_google_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts)
6781 if (bbr->rc_in_persist == 0) {
6782 /* We log only when not in persist */
6783 /* Translate to a Bytes Per Second */
6785 uint32_t upper, lower, delivered;
6788 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time))
6789 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time);
6793 * Now that we have processed the tim (skipping the sample
6794 * or possibly updating the time, go ahead and
6795 * calculate the cdr.
6797 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered);
6798 bw = (uint64_t)delivered;
6799 bw *= (uint64_t)USECS_IN_SECOND;
6801 if (tim < bbr->r_ctl.rc_lowest_rtt) {
6802 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered,
6803 tim, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0);
6807 upper = (bw >> 32) & 0x00000000ffffffff;
6808 lower = bw & 0x00000000ffffffff;
6810 * If we are using this b/w shove it in now so we
6811 * can see in the trace viewer if it gets over-ridden.
6813 bbr->r_ctl.rc_bbr_cur_del_rate = bw;
6814 /* Gate by the sending rate */
6815 if (rsm->r_first_sent_time &&
6816 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) {
6819 * We use what was in flight at the time of our
6820 * send and the size of this send to figure
6821 * out what we have been sending at (amount).
6822 * For the time we take from the time of
6823 * the send of the first send outstanding
6824 * until this send plus this sends pacing
6825 * time. This gives us a good calculation
6826 * as to the rate we have been sending at.
6829 sbw = (uint64_t)(rsm->r_flight_at_send);
6830 sbw *= (uint64_t)USECS_IN_SECOND;
6831 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time;
6832 sti += rsm->r_pacing_delay;
6835 bbr_log_type_bbrupd(bbr, 6, cts,
6840 rsm->r_first_sent_time, 0, (sbw >> 32),
6845 (sti < bbr->r_ctl.rc_lowest_rtt)) {
6846 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered,
6847 (uint32_t)sti, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0);
6852 bbr->r_ctl.rc_bbr_cur_del_rate = bw;
6853 if ((no_apply == 0) &&
6854 ((rsm->r_app_limited == 0) ||
6855 (bw > get_filter_value(&bbr->r_ctl.rc_delrate)))) {
6856 tcp_bbr_commit_bw(bbr, cts);
6857 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered,
6858 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time);
6865 bbr_update_bbr_info(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts, uint32_t tsin,
6866 uint32_t uts, int32_t match, uint32_t rsm_send_time, int32_t ack_type, struct tcpopt *to)
6868 uint64_t old_rttprop;
6870 /* Update our delivery time and amount */
6871 bbr->r_ctl.rc_delivered += (rsm->r_end - rsm->r_start);
6872 bbr->r_ctl.rc_del_time = cts;
6875 * 0 means its a retransmit, for now we don't use these for
6880 if ((bbr->rc_use_google == 0) &&
6881 (match != BBR_RTT_BY_EXACTMATCH) &&
6882 (match != BBR_RTT_BY_TIMESTAMP)){
6884 * We get a lot of rtt updates, lets not pay attention to
6885 * any that are not an exact match. That way we don't have
6886 * to worry about timestamps and the whole nonsense of
6887 * unsure if its a retransmission etc (if we ever had the
6888 * timestamp fixed to always have the last thing sent this
6889 * would not be a issue).
6893 if ((bbr_no_retran && bbr->rc_use_google) &&
6894 (match != BBR_RTT_BY_EXACTMATCH) &&
6895 (match != BBR_RTT_BY_TIMESTAMP)){
6897 * We only do measurements in google mode
6898 * with bbr_no_retran on for sure things.
6902 /* Only update srtt if we know by exact match */
6903 tcp_bbr_xmit_timer(bbr, rtt, rsm_send_time, rsm->r_start, tsin);
6904 if (ack_type == BBR_CUM_ACKED)
6905 bbr->rc_ack_is_cumack = 1;
6907 bbr->rc_ack_is_cumack = 0;
6908 old_rttprop = bbr_get_rtt(bbr, BBR_RTT_PROP);
6910 * Note the following code differs to the original
6911 * BBR spec. It calls for <= not <. However after a
6912 * long discussion in email with Neal, he acknowledged
6913 * that it should be < than so that we will have flows
6914 * going into probe-rtt (we were seeing cases where that
6915 * did not happen and caused ugly things to occur). We
6916 * have added this agreed upon fix to our code base.
6918 if (rtt < old_rttprop) {
6919 /* Update when we last saw a rtt drop */
6920 bbr_log_rtt_shrinks(bbr, cts, 0, rtt, __LINE__, BBR_RTTS_NEWRTT, 0);
6921 bbr_set_reduced_rtt(bbr, cts, __LINE__);
6923 bbr_log_type_bbrrttprop(bbr, rtt, (rsm ? rsm->r_end : 0), uts, cts,
6924 match, rsm->r_start, rsm->r_flags);
6925 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
6926 if (old_rttprop != bbr_get_rtt(bbr, BBR_RTT_PROP)) {
6928 * The RTT-prop moved, reset the target (may be a
6929 * nop for some states).
6931 bbr_set_state_target(bbr, __LINE__);
6932 if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT)
6933 bbr_log_rtt_shrinks(bbr, cts, 0, 0,
6934 __LINE__, BBR_RTTS_NEW_TARGET, 0);
6935 else if (old_rttprop < bbr_get_rtt(bbr, BBR_RTT_PROP))
6937 bbr_check_probe_rtt_limits(bbr, cts);
6939 if ((bbr->rc_use_google == 0) &&
6940 (match == BBR_RTT_BY_TIMESTAMP)) {
6942 * We don't do b/w update with
6943 * these since they are not really
6948 if (bbr->r_ctl.r_app_limited_until &&
6949 (bbr->r_ctl.rc_delivered >= bbr->r_ctl.r_app_limited_until)) {
6950 /* We are no longer app-limited */
6951 bbr->r_ctl.r_app_limited_until = 0;
6953 if (bbr->rc_use_google) {
6954 bbr_google_measurement(bbr, rsm, rtt, cts);
6956 bbr_nf_measurement(bbr, rsm, rtt, cts);
6961 * Convert a timestamp that the main stack
6962 * uses (milliseconds) into one that bbr uses
6963 * (microseconds). Return that converted timestamp.
6966 bbr_ts_convert(uint32_t cts) {
6969 sec = cts / MS_IN_USEC;
6970 msec = cts - (MS_IN_USEC * sec);
6971 return ((sec * USECS_IN_SECOND) + (msec * MS_IN_USEC));
6975 * Return 0 if we did not update the RTT time, return
6979 bbr_update_rtt(struct tcpcb *tp, struct tcp_bbr *bbr,
6980 struct bbr_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, uint32_t th_ack)
6983 uint32_t t, uts = 0;
6985 if ((rsm->r_flags & BBR_ACKED) ||
6986 (rsm->r_flags & BBR_WAS_RENEGED) ||
6987 (rsm->r_flags & BBR_RXT_CLEARED)) {
6991 if (rsm->r_rtr_cnt == 1) {
6993 * Only one transmit. Hopefully the normal case.
6995 if (TSTMP_GT(cts, rsm->r_tim_lastsent[0]))
6996 t = cts - rsm->r_tim_lastsent[0];
7001 bbr->r_ctl.rc_last_rtt = t;
7002 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0,
7003 BBR_RTT_BY_EXACTMATCH, rsm->r_tim_lastsent[0], ack_type, to);
7006 /* Convert to usecs */
7007 if ((bbr_can_use_ts_for_rtt == 1) &&
7008 (bbr->rc_use_google == 1) &&
7009 (ack_type == BBR_CUM_ACKED) &&
7010 (to->to_flags & TOF_TS) &&
7011 (to->to_tsecr != 0)) {
7013 t = tcp_tv_to_mssectick(&bbr->rc_tv) - to->to_tsecr;
7017 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0,
7018 BBR_RTT_BY_TIMESTAMP,
7019 rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)],
7023 uts = bbr_ts_convert(to->to_tsecr);
7024 if ((to->to_flags & TOF_TS) &&
7025 (to->to_tsecr != 0) &&
7026 (ack_type == BBR_CUM_ACKED) &&
7027 ((rsm->r_flags & BBR_OVERMAX) == 0)) {
7029 * Now which timestamp does it match? In this block the ACK
7030 * may be coming from a previous transmission.
7034 fudge = BBR_TIMER_FUDGE;
7035 for (i = 0; i < rsm->r_rtr_cnt; i++) {
7036 if ((SEQ_GEQ(uts, (rsm->r_tim_lastsent[i] - fudge))) &&
7037 (SEQ_LEQ(uts, (rsm->r_tim_lastsent[i] + fudge)))) {
7038 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i]))
7039 t = cts - rsm->r_tim_lastsent[i];
7044 bbr->r_ctl.rc_last_rtt = t;
7045 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_TSMATCHING,
7046 rsm->r_tim_lastsent[i], ack_type, to);
7047 if ((i + 1) < rsm->r_rtr_cnt) {
7049 bbr_earlier_retran(tp, bbr, rsm, t, cts, ack_type);
7050 } else if (rsm->r_flags & BBR_TLP) {
7051 bbr->rc_tlp_rtx_out = 0;
7056 /* Fall through if we can't find a matching timestamp */
7059 * Ok its a SACK block that we retransmitted. or a windows
7060 * machine without timestamps. We can tell nothing from the
7061 * time-stamp since its not there or the time the peer last
7062 * recieved a segment that moved forward its cum-ack point.
7064 * Lets look at the last retransmit and see what we can tell
7065 * (with BBR for space we only keep 2 note we have to keep
7066 * at least 2 so the map can not be condensed more).
7068 i = rsm->r_rtr_cnt - 1;
7069 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i]))
7070 t = cts - rsm->r_tim_lastsent[i];
7073 if (t < bbr->r_ctl.rc_lowest_rtt) {
7075 * We retransmitted and the ack came back in less
7076 * than the smallest rtt we have observed in the
7077 * windowed rtt. We most likey did an improper
7078 * retransmit as outlined in 4.2 Step 3 point 2 in
7081 * Use the prior transmission to update all the
7082 * information as long as there is only one prior
7085 if ((rsm->r_flags & BBR_OVERMAX) == 0) {
7086 #ifdef BBR_INVARIANTS
7087 if (rsm->r_rtr_cnt == 1)
7088 panic("rsm:%p bbr:%p rsm has overmax and only 1 retranmit flags:%x?", rsm, bbr, rsm->r_flags);
7090 i = rsm->r_rtr_cnt - 2;
7091 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i]))
7092 t = cts - rsm->r_tim_lastsent[i];
7095 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_EARLIER_RET,
7096 rsm->r_tim_lastsent[i], ack_type, to);
7097 bbr_earlier_retran(tp, bbr, rsm, t, cts, ack_type);
7100 * Too many prior transmissions, just
7101 * updated BBR delivered
7104 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts,
7105 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to);
7109 * We retransmitted it and the retransmit did the
7112 if (rsm->r_flags & BBR_TLP)
7113 bbr->rc_tlp_rtx_out = 0;
7114 if ((rsm->r_flags & BBR_OVERMAX) == 0)
7115 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts,
7116 BBR_RTT_BY_THIS_RETRAN, 0, ack_type, to);
7118 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts,
7119 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to);
7126 * Mark the SACK_PASSED flag on all entries prior to rsm send wise.
7129 bbr_log_sack_passed(struct tcpcb *tp,
7130 struct tcp_bbr *bbr, struct bbr_sendmap *rsm)
7132 struct bbr_sendmap *nrsm;
7135 TAILQ_FOREACH_REVERSE_FROM(nrsm, &bbr->r_ctl.rc_tmap,
7136 bbr_head, r_tnext) {
7138 /* Skip orginal segment he is acked */
7141 if (nrsm->r_flags & BBR_ACKED) {
7142 /* Skip ack'd segments */
7145 if (nrsm->r_flags & BBR_SACK_PASSED) {
7147 * We found one that is already marked
7148 * passed, we have been here before and
7149 * so all others below this are marked.
7153 BBR_STAT_INC(bbr_sack_passed);
7154 nrsm->r_flags |= BBR_SACK_PASSED;
7155 if (((nrsm->r_flags & BBR_MARKED_LOST) == 0) &&
7156 bbr_is_lost(bbr, nrsm, bbr->r_ctl.rc_rcvtime)) {
7157 bbr->r_ctl.rc_lost += nrsm->r_end - nrsm->r_start;
7158 bbr->r_ctl.rc_lost_bytes += nrsm->r_end - nrsm->r_start;
7159 nrsm->r_flags |= BBR_MARKED_LOST;
7161 nrsm->r_flags &= ~BBR_WAS_SACKPASS;
7166 * Returns the number of bytes that were
7167 * newly ack'd by sack blocks.
7170 bbr_proc_sack_blk(struct tcpcb *tp, struct tcp_bbr *bbr, struct sackblk *sack,
7171 struct tcpopt *to, struct bbr_sendmap **prsm, uint32_t cts)
7174 uint32_t start, end, maxseg, changed = 0;
7175 struct bbr_sendmap *rsm, *nrsm;
7176 int32_t used_ref = 1;
7177 uint8_t went_back = 0, went_fwd = 0;
7179 maxseg = tp->t_maxseg - bbr->rc_last_options;
7180 start = sack->start;
7186 /* Do we locate the block behind where we last were? */
7187 if (rsm && SEQ_LT(start, rsm->r_start)) {
7189 TAILQ_FOREACH_REVERSE_FROM(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) {
7190 if (SEQ_GEQ(start, rsm->r_start) &&
7191 SEQ_LT(start, rsm->r_end)) {
7199 * Ok lets locate the block where this guy is fwd from rsm (if its
7202 TAILQ_FOREACH_FROM(rsm, &bbr->r_ctl.rc_map, r_next) {
7203 if (SEQ_GEQ(start, rsm->r_start) &&
7204 SEQ_LT(start, rsm->r_end)) {
7211 * This happens when we get duplicate sack blocks with the
7212 * same end. For example SACK 4: 100 SACK 3: 100 The sort
7213 * will not change there location so we would just start at
7214 * the end of the first one and get lost.
7216 if (tp->t_flags & TF_SENTFIN) {
7218 * Check to see if we have not logged the FIN that
7221 nrsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next);
7222 if (nrsm && (nrsm->r_end + 1) == tp->snd_max) {
7224 * Ok we did not get the FIN logged.
7232 #ifdef BBR_INVARIANTS
7233 panic("tp:%p bbr:%p sack:%p to:%p prsm:%p",
7234 tp, bbr, sack, to, prsm);
7240 BBR_STAT_INC(bbr_sack_proc_restart);
7242 goto start_at_beginning;
7244 /* Ok we have an ACK for some piece of rsm */
7245 if (rsm->r_start != start) {
7247 * Need to split this in two pieces the before and after.
7249 if (bbr_sack_mergable(rsm, start, end))
7250 nrsm = bbr_alloc_full_limit(bbr);
7252 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT);
7254 /* We could not allocate ignore the sack */
7259 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk);
7262 bbr_clone_rsm(bbr, nrsm, rsm, start);
7263 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
7264 if (rsm->r_in_tmap) {
7265 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7266 nrsm->r_in_tmap = 1;
7268 rsm->r_flags &= (~BBR_HAS_FIN);
7271 if (SEQ_GEQ(end, rsm->r_end)) {
7273 * The end of this block is either beyond this guy or right
7276 if ((rsm->r_flags & BBR_ACKED) == 0) {
7277 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0);
7278 changed += (rsm->r_end - rsm->r_start);
7279 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
7280 bbr_log_sack_passed(tp, bbr, rsm);
7281 if (rsm->r_flags & BBR_MARKED_LOST) {
7282 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7284 /* Is Reordering occuring? */
7285 if (rsm->r_flags & BBR_SACK_PASSED) {
7286 BBR_STAT_INC(bbr_reorder_seen);
7287 bbr->r_ctl.rc_reorder_ts = cts;
7288 if (rsm->r_flags & BBR_MARKED_LOST) {
7289 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7290 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7291 /* LT sampling also needs adjustment */
7292 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7295 rsm->r_flags |= BBR_ACKED;
7296 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST);
7297 if (rsm->r_in_tmap) {
7298 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7302 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED);
7303 if (end == rsm->r_end) {
7304 /* This block only - done */
7307 /* There is more not coverend by this rsm move on */
7309 nrsm = TAILQ_NEXT(rsm, r_next);
7314 if (rsm->r_flags & BBR_ACKED) {
7315 /* Been here done that */
7318 /* Ok we need to split off this one at the tail */
7319 if (bbr_sack_mergable(rsm, start, end))
7320 nrsm = bbr_alloc_full_limit(bbr);
7322 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT);
7324 /* failed XXXrrs what can we do but loose the sack info? */
7329 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk);
7333 bbr_clone_rsm(bbr, nrsm, rsm, end);
7334 /* The sack block does not cover this guy fully */
7335 rsm->r_flags &= (~BBR_HAS_FIN);
7336 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
7337 if (rsm->r_in_tmap) {
7338 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7339 nrsm->r_in_tmap = 1;
7342 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0);
7343 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED);
7344 changed += (rsm->r_end - rsm->r_start);
7345 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
7346 bbr_log_sack_passed(tp, bbr, rsm);
7347 /* Is Reordering occuring? */
7348 if (rsm->r_flags & BBR_MARKED_LOST) {
7349 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7351 if (rsm->r_flags & BBR_SACK_PASSED) {
7352 BBR_STAT_INC(bbr_reorder_seen);
7353 bbr->r_ctl.rc_reorder_ts = cts;
7354 if (rsm->r_flags & BBR_MARKED_LOST) {
7355 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7356 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7357 /* LT sampling also needs adjustment */
7358 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7361 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST);
7362 rsm->r_flags |= BBR_ACKED;
7363 if (rsm->r_in_tmap) {
7364 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7368 if (rsm && (rsm->r_flags & BBR_ACKED)) {
7370 * Now can we merge this newly acked
7371 * block with either the previous or
7374 nrsm = TAILQ_NEXT(rsm, r_next);
7376 (nrsm->r_flags & BBR_ACKED)) {
7377 /* yep this and next can be merged */
7378 rsm = bbr_merge_rsm(bbr, rsm, nrsm);
7380 /* Now what about the previous? */
7381 nrsm = TAILQ_PREV(rsm, bbr_head, r_next);
7383 (nrsm->r_flags & BBR_ACKED)) {
7384 /* yep the previous and this can be merged */
7385 rsm = bbr_merge_rsm(bbr, nrsm, rsm);
7388 if (used_ref == 0) {
7389 BBR_STAT_INC(bbr_sack_proc_all);
7391 BBR_STAT_INC(bbr_sack_proc_short);
7393 if (went_fwd && went_back) {
7394 BBR_STAT_INC(bbr_sack_search_both);
7395 } else if (went_fwd) {
7396 BBR_STAT_INC(bbr_sack_search_fwd);
7397 } else if (went_back) {
7398 BBR_STAT_INC(bbr_sack_search_back);
7400 /* Save off where the next seq is */
7402 bbr->r_ctl.rc_sacklast = TAILQ_NEXT(rsm, r_next);
7404 bbr->r_ctl.rc_sacklast = NULL;
7411 bbr_peer_reneges(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, tcp_seq th_ack)
7413 struct bbr_sendmap *tmap;
7415 BBR_STAT_INC(bbr_reneges_seen);
7417 while (rsm && (rsm->r_flags & BBR_ACKED)) {
7418 /* Its no longer sacked, mark it so */
7420 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
7421 #ifdef BBR_INVARIANTS
7422 if (rsm->r_in_tmap) {
7423 panic("bbr:%p rsm:%p flags:0x%x in tmap?",
7424 bbr, rsm, rsm->r_flags);
7427 oflags = rsm->r_flags;
7428 if (rsm->r_flags & BBR_MARKED_LOST) {
7429 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7430 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7431 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7432 /* LT sampling also needs adjustment */
7433 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7435 rsm->r_flags &= ~(BBR_ACKED | BBR_SACK_PASSED | BBR_WAS_SACKPASS | BBR_MARKED_LOST);
7436 rsm->r_flags |= BBR_WAS_RENEGED;
7437 rsm->r_flags |= BBR_RXT_CLEARED;
7438 bbr_log_type_rsmclear(bbr, bbr->r_ctl.rc_rcvtime, rsm, oflags, __LINE__);
7439 /* Rebuild it into our tmap */
7441 TAILQ_INSERT_HEAD(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7444 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, tmap, rsm, r_tnext);
7447 tmap->r_in_tmap = 1;
7449 * XXXrrs Delivered? Should we do anything here?
7451 * Of course we don't on a rxt timeout so maybe its ok that
7456 rsm = TAILQ_NEXT(rsm, r_next);
7459 * Now lets possibly clear the sack filter so we start recognizing
7460 * sacks that cover this area.
7462 sack_filter_clear(&bbr->r_ctl.bbr_sf, th_ack);
7466 bbr_log_syn(struct tcpcb *tp, struct tcpopt *to)
7468 struct tcp_bbr *bbr;
7469 struct bbr_sendmap *rsm;
7472 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
7473 cts = bbr->r_ctl.rc_rcvtime;
7474 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7475 if (rsm && (rsm->r_flags & BBR_HAS_SYN)) {
7476 if ((rsm->r_end - rsm->r_start) <= 1) {
7477 /* Log out the SYN completely */
7478 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
7479 rsm->r_rtr_bytes = 0;
7480 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next);
7481 if (rsm->r_in_tmap) {
7482 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7485 if (bbr->r_ctl.rc_next == rsm) {
7486 /* scoot along the marker */
7487 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7490 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, 0);
7493 /* There is more (Fast open)? strip out SYN. */
7494 rsm->r_flags &= ~BBR_HAS_SYN;
7501 * Returns the number of bytes that were
7502 * acknowledged by SACK blocks.
7506 bbr_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th,
7507 uint32_t *prev_acked)
7509 uint32_t changed, last_seq, entered_recovery = 0;
7510 struct tcp_bbr *bbr;
7511 struct bbr_sendmap *rsm;
7512 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1];
7513 register uint32_t th_ack;
7514 int32_t i, j, k, new_sb, num_sack_blks = 0;
7515 uint32_t cts, acked, ack_point, sack_changed = 0;
7516 uint32_t p_maxseg, maxseg, p_acked = 0;
7518 INP_WLOCK_ASSERT(tp->t_inpcb);
7519 if (th->th_flags & TH_RST) {
7520 /* We don't log resets */
7523 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
7524 cts = bbr->r_ctl.rc_rcvtime;
7526 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7528 maxseg = tp->t_maxseg - bbr->rc_last_options;
7529 p_maxseg = min(bbr->r_ctl.rc_pace_max_segs, maxseg);
7530 th_ack = th->th_ack;
7531 if (SEQ_GT(th_ack, tp->snd_una)) {
7532 acked = th_ack - tp->snd_una;
7533 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_UPDATE, __LINE__);
7534 bbr->rc_tp->t_acktime = ticks;
7537 if (SEQ_LEQ(th_ack, tp->snd_una)) {
7538 /* Only sent here for sack processing */
7541 if (rsm && SEQ_GT(th_ack, rsm->r_start)) {
7542 changed = th_ack - rsm->r_start;
7543 } else if ((rsm == NULL) && ((th_ack - 1) == tp->iss)) {
7545 * For the SYN incoming case we will not have called
7546 * tcp_output for the sending of the SYN, so there will be
7547 * no map. All other cases should probably be a panic.
7549 if ((to->to_flags & TOF_TS) && (to->to_tsecr != 0)) {
7551 * We have a timestamp that can be used to generate
7554 uint32_t ts, now, rtt;
7556 ts = bbr_ts_convert(to->to_tsecr);
7557 now = bbr_ts_convert(tcp_tv_to_mssectick(&bbr->rc_tv));
7561 bbr_log_type_bbrrttprop(bbr, rtt,
7563 BBR_RTT_BY_TIMESTAMP, tp->iss, 0);
7564 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
7566 bbr->r_wanted_output = 1;
7570 } else if (rsm == NULL) {
7575 * The ACK point is advancing to th_ack, we must drop off
7576 * the packets in the rack log and calculate any eligble
7579 bbr->r_wanted_output = 1;
7583 if (tp->t_flags & TF_SENTFIN) {
7584 /* if we send a FIN we will not hav a map */
7587 #ifdef BBR_INVARIANTS
7588 panic("No rack map tp:%p for th:%p state:%d bbr:%p snd_una:%u snd_max:%u chg:%d\n",
7590 th, tp->t_state, bbr,
7591 tp->snd_una, tp->snd_max, changed);
7596 if (SEQ_LT(th_ack, rsm->r_start)) {
7597 /* Huh map is missing this */
7598 #ifdef BBR_INVARIANTS
7599 printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d bbr:%p\n",
7601 th_ack, tp->t_state,
7603 panic("th-ack is bad bbr:%p tp:%p", bbr, tp);
7606 } else if (th_ack == rsm->r_start) {
7607 /* None here to ack */
7611 * Clear the dup ack counter, it will
7612 * either be freed or if there is some
7613 * remaining we need to start it at zero.
7616 /* Now do we consume the whole thing? */
7617 if (SEQ_GEQ(th_ack, rsm->r_end)) {
7618 /* Its all consumed. */
7621 if (rsm->r_flags & BBR_ACKED) {
7623 * It was acked on the scoreboard -- remove it from
7626 p_acked += (rsm->r_end - rsm->r_start);
7627 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
7628 if (bbr->r_ctl.rc_sacked == 0)
7629 bbr->r_ctl.rc_sacklast = NULL;
7631 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, th_ack);
7632 if (rsm->r_flags & BBR_MARKED_LOST) {
7633 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7635 if (rsm->r_flags & BBR_SACK_PASSED) {
7637 * There are acked segments ACKED on the
7638 * scoreboard further up. We are seeing
7641 BBR_STAT_INC(bbr_reorder_seen);
7642 bbr->r_ctl.rc_reorder_ts = cts;
7643 if (rsm->r_flags & BBR_MARKED_LOST) {
7644 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7645 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7646 /* LT sampling also needs adjustment */
7647 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7650 rsm->r_flags &= ~BBR_MARKED_LOST;
7652 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
7653 rsm->r_rtr_bytes = 0;
7654 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next);
7655 if (rsm->r_in_tmap) {
7656 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7659 if (bbr->r_ctl.rc_next == rsm) {
7660 /* scoot along the marker */
7661 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7663 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED);
7664 /* Adjust the packet counts */
7665 left = th_ack - rsm->r_end;
7666 /* Free back to zone */
7669 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7674 if (rsm->r_flags & BBR_ACKED) {
7676 * It was acked on the scoreboard -- remove it from total
7677 * for the part being cum-acked.
7679 p_acked += (rsm->r_end - rsm->r_start);
7680 bbr->r_ctl.rc_sacked -= (th_ack - rsm->r_start);
7681 if (bbr->r_ctl.rc_sacked == 0)
7682 bbr->r_ctl.rc_sacklast = NULL;
7685 * It was acked up to th_ack point for the first time
7687 struct bbr_sendmap lrsm;
7689 memcpy(&lrsm, rsm, sizeof(struct bbr_sendmap));
7690 lrsm.r_end = th_ack;
7691 bbr_update_rtt(tp, bbr, &lrsm, to, cts, BBR_CUM_ACKED, th_ack);
7693 if ((rsm->r_flags & BBR_MARKED_LOST) &&
7694 ((rsm->r_flags & BBR_ACKED) == 0)) {
7696 * It was marked lost and partly ack'd now
7697 * for the first time. We lower the rc_lost_bytes
7698 * and still leave it MARKED.
7700 bbr->r_ctl.rc_lost_bytes -= th_ack - rsm->r_start;
7702 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED);
7703 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
7704 rsm->r_rtr_bytes = 0;
7705 /* adjust packet count */
7706 rsm->r_start = th_ack;
7708 /* Check for reneging */
7709 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7710 if (rsm && (rsm->r_flags & BBR_ACKED) && (th_ack == rsm->r_start)) {
7712 * The peer has moved snd_una up to the edge of this send,
7713 * i.e. one that it had previously acked. The only way that
7714 * can be true if the peer threw away data (space issues)
7715 * that it had previously sacked (else it would have given
7716 * us snd_una up to (rsm->r_end). We need to undo the acked
7719 * Note we have to look to make sure th_ack is our
7720 * rsm->r_start in case we get an old ack where th_ack is
7723 bbr_peer_reneges(bbr, rsm, th->th_ack);
7725 if ((to->to_flags & TOF_SACK) == 0) {
7726 /* We are done nothing left to log */
7729 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next);
7731 last_seq = rsm->r_end;
7733 last_seq = tp->snd_max;
7735 /* Sack block processing */
7736 if (SEQ_GT(th_ack, tp->snd_una))
7739 ack_point = tp->snd_una;
7740 for (i = 0; i < to->to_nsacks; i++) {
7741 bcopy((to->to_sacks + i * TCPOLEN_SACK),
7742 &sack, sizeof(sack));
7743 sack.start = ntohl(sack.start);
7744 sack.end = ntohl(sack.end);
7745 if (SEQ_GT(sack.end, sack.start) &&
7746 SEQ_GT(sack.start, ack_point) &&
7747 SEQ_LT(sack.start, tp->snd_max) &&
7748 SEQ_GT(sack.end, ack_point) &&
7749 SEQ_LEQ(sack.end, tp->snd_max)) {
7750 if ((bbr->r_ctl.rc_num_small_maps_alloced > bbr_sack_block_limit) &&
7751 (SEQ_LT(sack.end, last_seq)) &&
7752 ((sack.end - sack.start) < (p_maxseg / 8))) {
7754 * Not the last piece and its smaller than
7755 * 1/8th of a p_maxseg. We ignore this.
7757 BBR_STAT_INC(bbr_runt_sacks);
7760 sack_blocks[num_sack_blks] = sack;
7762 #ifdef NETFLIX_STATS
7763 } else if (SEQ_LEQ(sack.start, th_ack) &&
7764 SEQ_LEQ(sack.end, th_ack)) {
7766 * Its a D-SACK block.
7768 tcp_record_dsack(sack.start, sack.end);
7772 if (num_sack_blks == 0)
7775 * Sort the SACK blocks so we can update the rack scoreboard with
7778 new_sb = sack_filter_blks(&bbr->r_ctl.bbr_sf, sack_blocks,
7779 num_sack_blks, th->th_ack);
7780 ctf_log_sack_filter(bbr->rc_tp, new_sb, sack_blocks);
7781 BBR_STAT_ADD(bbr_sack_blocks, num_sack_blks);
7782 BBR_STAT_ADD(bbr_sack_blocks_skip, (num_sack_blks - new_sb));
7783 num_sack_blks = new_sb;
7784 if (num_sack_blks < 2) {
7787 /* Sort the sacks */
7788 for (i = 0; i < num_sack_blks; i++) {
7789 for (j = i + 1; j < num_sack_blks; j++) {
7790 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
7791 sack = sack_blocks[i];
7792 sack_blocks[i] = sack_blocks[j];
7793 sack_blocks[j] = sack;
7798 * Now are any of the sack block ends the same (yes some
7799 * implememtations send these)?
7802 if (num_sack_blks > 1) {
7803 for (i = 0; i < num_sack_blks; i++) {
7804 for (j = i + 1; j < num_sack_blks; j++) {
7805 if (sack_blocks[i].end == sack_blocks[j].end) {
7807 * Ok these two have the same end we
7808 * want the smallest end and then
7809 * throw away the larger and start
7812 if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) {
7814 * The second block covers
7815 * more area use that
7817 sack_blocks[i].start = sack_blocks[j].start;
7820 * Now collapse out the dup-sack and
7823 for (k = (j + 1); k < num_sack_blks; k++) {
7824 sack_blocks[j].start = sack_blocks[k].start;
7825 sack_blocks[j].end = sack_blocks[k].end;
7835 rsm = bbr->r_ctl.rc_sacklast;
7836 for (i = 0; i < num_sack_blks; i++) {
7837 acked = bbr_proc_sack_blk(tp, bbr, &sack_blocks[i], to, &rsm, cts);
7839 bbr->r_wanted_output = 1;
7841 sack_changed += acked;
7845 *prev_acked = p_acked;
7846 if ((sack_changed) && (!IN_RECOVERY(tp->t_flags))) {
7848 * Ok we have a high probability that we need to go in to
7849 * recovery since we have data sack'd
7851 struct bbr_sendmap *rsm;
7853 rsm = bbr_check_recovery_mode(tp, bbr, cts);
7855 /* Enter recovery */
7856 entered_recovery = 1;
7857 bbr->r_wanted_output = 1;
7859 * When we enter recovery we need to assure we send
7862 if (bbr->r_ctl.rc_resend == NULL) {
7863 bbr->r_ctl.rc_resend = rsm;
7867 if (IN_RECOVERY(tp->t_flags) && (entered_recovery == 0)) {
7869 * See if we need to rack-retransmit anything if so set it
7870 * up as the thing to resend assuming something else is not
7871 * already in that position.
7873 if (bbr->r_ctl.rc_resend == NULL) {
7874 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts);
7878 * We return the amount that changed via sack, this is used by the
7879 * ack-received code to augment what was changed between th_ack <->
7882 return (sack_changed);
7886 bbr_strike_dupack(struct tcp_bbr *bbr)
7888 struct bbr_sendmap *rsm;
7890 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
7891 if (rsm && (rsm->r_dupack < 0xff)) {
7893 if (rsm->r_dupack >= DUP_ACK_THRESHOLD)
7894 bbr->r_wanted_output = 1;
7899 * Return value of 1, we do not need to call bbr_process_data().
7900 * return value of 0, bbr_process_data can be called.
7901 * For ret_val if its 0 the TCB is locked and valid, if its non-zero
7902 * its unlocked and probably unsafe to touch the TCB.
7905 bbr_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so,
7906 struct tcpcb *tp, struct tcpopt *to,
7907 uint32_t tiwin, int32_t tlen,
7908 int32_t * ofia, int32_t thflags, int32_t * ret_val)
7910 int32_t ourfinisacked = 0;
7911 int32_t acked_amount;
7914 uint32_t lost, sack_changed = 0;
7916 struct tcp_bbr *bbr;
7917 uint32_t prev_acked = 0;
7919 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
7920 lost = bbr->r_ctl.rc_lost;
7921 nsegs = max(1, m->m_pkthdr.lro_nsegs);
7922 if (SEQ_GT(th->th_ack, tp->snd_max)) {
7923 ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val);
7924 bbr->r_wanted_output = 1;
7927 if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) {
7928 /* Process the ack */
7929 if (bbr->rc_in_persist)
7931 if ((th->th_ack == tp->snd_una) && (tiwin == tp->snd_wnd))
7932 bbr_strike_dupack(bbr);
7933 sack_changed = bbr_log_ack(tp, to, th, &prev_acked);
7935 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, (bbr->r_ctl.rc_lost > lost));
7936 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
7938 * Old ack, behind the last one rcv'd or a duplicate ack
7941 if (th->th_ack == tp->snd_una) {
7942 bbr_ack_received(tp, bbr, th, 0, sack_changed, prev_acked, __LINE__, 0);
7943 if (bbr->r_state == TCPS_SYN_SENT) {
7945 * Special case on where we sent SYN. When
7946 * the SYN-ACK is processed in syn_sent
7947 * state it bumps the snd_una. This causes
7948 * us to hit here even though we did ack 1
7951 * Go through the nothing left case so we
7960 * If we reach this point, ACK is not a duplicate, i.e., it ACKs
7961 * something we sent.
7963 if (tp->t_flags & TF_NEEDSYN) {
7965 * T/TCP: Connection was half-synchronized, and our SYN has
7966 * been ACK'd (so connection is now fully synchronized). Go
7967 * to non-starred state, increment snd_una for ACK of SYN,
7968 * and check if we can do window scaling.
7970 tp->t_flags &= ~TF_NEEDSYN;
7972 /* Do window scaling? */
7973 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
7974 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
7975 tp->rcv_scale = tp->request_r_scale;
7976 /* Send window already scaled. */
7979 INP_WLOCK_ASSERT(tp->t_inpcb);
7981 acked = BYTES_THIS_ACK(tp, th);
7982 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs);
7983 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
7986 * If we just performed our first retransmit, and the ACK arrives
7987 * within our recovery window, then it was a mistake to do the
7988 * retransmit in the first place. Recover our original cwnd and
7989 * ssthresh, and proceed to transmit where we left off.
7991 if (tp->t_flags & TF_PREVVALID) {
7992 tp->t_flags &= ~TF_PREVVALID;
7993 if (tp->t_rxtshift == 1 &&
7994 (int)(ticks - tp->t_badrxtwin) < 0)
7995 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL);
7997 SOCKBUF_LOCK(&so->so_snd);
7998 acked_amount = min(acked, (int)sbavail(&so->so_snd));
7999 tp->snd_wnd -= acked_amount;
8000 mfree = sbcut_locked(&so->so_snd, acked_amount);
8001 /* NB: sowwakeup_locked() does an implicit unlock. */
8002 sowwakeup_locked(so);
8004 if (SEQ_GT(th->th_ack, tp->snd_una)) {
8005 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp));
8007 tp->snd_una = th->th_ack;
8008 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, (bbr->r_ctl.rc_lost - lost));
8009 if (IN_RECOVERY(tp->t_flags)) {
8010 if (SEQ_LT(th->th_ack, tp->snd_recover) &&
8011 (SEQ_LT(th->th_ack, tp->snd_max))) {
8012 tcp_bbr_partialack(tp);
8014 bbr_post_recovery(tp);
8017 if (SEQ_GT(tp->snd_una, tp->snd_recover)) {
8018 tp->snd_recover = tp->snd_una;
8020 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
8021 tp->snd_nxt = tp->snd_max;
8023 if (tp->snd_una == tp->snd_max) {
8024 /* Nothing left outstanding */
8026 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__);
8027 if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0)
8028 bbr->rc_tp->t_acktime = 0;
8029 if ((sbused(&so->so_snd) == 0) &&
8030 (tp->t_flags & TF_SENTFIN)) {
8033 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8034 if (bbr->rc_in_persist == 0) {
8035 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime;
8037 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
8038 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime);
8040 * We invalidate the last ack here since we
8041 * don't want to transfer forward the time
8042 * for our sum's calculations.
8044 if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
8045 (sbavail(&so->so_snd) == 0) &&
8046 (tp->t_flags2 & TF2_DROP_AF_DATA)) {
8048 * The socket was gone and the peer sent data, time
8053 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen);
8054 BBR_STAT_INC(bbr_dropped_af_data);
8057 /* Set need output so persist might get set */
8058 bbr->r_wanted_output = 1;
8061 *ofia = ourfinisacked;
8066 bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line)
8068 if (bbr->rc_in_persist == 0) {
8069 bbr_timer_cancel(bbr, __LINE__, cts);
8070 bbr->r_ctl.rc_last_delay_val = 0;
8072 bbr->rc_in_persist = 1;
8073 bbr->r_ctl.rc_went_idle_time = cts;
8074 /* We should be capped when rw went to 0 but just in case */
8075 bbr_log_type_pesist(bbr, cts, 0, line, 1);
8076 /* Time freezes for the state, so do the accounting now */
8077 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
8080 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
8081 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) {
8084 idx = bbr_state_val(bbr);
8085 counter_u64_add(bbr_state_time[(idx + 5)], time_in);
8087 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
8090 bbr->r_ctl.rc_bbr_state_time = cts;
8095 bbr_restart_after_idle(struct tcp_bbr *bbr, uint32_t cts, uint32_t idle_time)
8098 * Note that if idle time does not exceed our
8099 * threshold, we do nothing continuing the state
8100 * transitions we were last walking through.
8102 if (idle_time >= bbr_idle_restart_threshold) {
8103 if (bbr->rc_use_idle_restart) {
8104 bbr->rc_bbr_state = BBR_STATE_IDLE_EXIT;
8106 * Set our target using BBR_UNIT, so
8107 * we increase at a dramatic rate but
8108 * we stop when we get the pipe
8109 * full again for our current b/w estimate.
8111 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
8112 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
8113 bbr_set_state_target(bbr, __LINE__);
8114 /* Now setup our gains to ramp up */
8115 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg;
8116 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg;
8117 bbr_log_type_statechange(bbr, cts, __LINE__);
8119 bbr_substate_change(bbr, cts, __LINE__, 1);
8125 bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line)
8129 if (bbr->rc_in_persist == 0)
8131 idle_time = bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time);
8132 bbr->rc_in_persist = 0;
8133 bbr->rc_hit_state_1 = 0;
8134 tp->t_flags &= ~TF_FORCEDATA;
8135 bbr->r_ctl.rc_del_time = cts;
8137 * We invalidate the last ack here since we
8138 * don't want to transfer forward the time
8139 * for our sum's calculations.
8141 if (bbr->rc_inp->inp_in_hpts) {
8142 tcp_hpts_remove(bbr->rc_inp, HPTS_REMOVE_OUTPUT);
8143 bbr->rc_timer_first = 0;
8144 bbr->r_ctl.rc_hpts_flags = 0;
8145 bbr->r_ctl.rc_last_delay_val = 0;
8146 bbr->r_ctl.rc_hptsi_agg_delay = 0;
8147 bbr->r_agg_early_set = 0;
8148 bbr->r_ctl.rc_agg_early = 0;
8150 bbr_log_type_pesist(bbr, cts, idle_time, line, 0);
8151 if (idle_time >= bbr_rtt_probe_time) {
8153 * This qualifies as a RTT_PROBE session since we drop the
8154 * data outstanding to nothing and waited more than
8155 * bbr_rtt_probe_time.
8157 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_PERSIST, 0);
8158 bbr->r_ctl.last_in_probertt = bbr->r_ctl.rc_rtt_shrinks = cts;
8162 * If in probeBW and we have persisted more than an RTT lets do
8165 /* Force a time based epoch */
8166 bbr_set_epoch(bbr, cts, __LINE__);
8168 * Setup the lost so we don't count anything against the guy
8169 * we have been stuck with during persists.
8171 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
8172 /* Time un-freezes for the state */
8173 bbr->r_ctl.rc_bbr_state_time = cts;
8174 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) ||
8175 (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT)) {
8177 * If we are going back to probe-bw
8178 * or probe_rtt, we may need to possibly
8179 * do a fast restart.
8181 bbr_restart_after_idle(bbr, cts, idle_time);
8186 bbr_collapsed_window(struct tcp_bbr *bbr)
8189 * Now we must walk the
8190 * send map and divide the
8191 * ones left stranded. These
8192 * guys can't cause us to abort
8193 * the connection and are really
8194 * "unsent". However if a buggy
8195 * client actually did keep some
8196 * of the data i.e. collapsed the win
8197 * and refused to ack and then opened
8198 * the win and acked that data. We would
8199 * get into an ack war, the simplier
8200 * method then of just pretending we
8201 * did not send those segments something
8204 struct bbr_sendmap *rsm, *nrsm;
8210 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
8211 max_seq = bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd;
8212 bbr_log_type_rwnd_collapse(bbr, max_seq, 1, 0);
8213 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
8214 /* Find the first seq past or at maxseq */
8215 if (rsm->r_flags & BBR_RWND_COLLAPSED)
8216 rsm->r_flags &= ~BBR_RWND_COLLAPSED;
8217 if (SEQ_GEQ(max_seq, rsm->r_start) &&
8218 SEQ_GEQ(rsm->r_end, max_seq)) {
8223 bbr->rc_has_collapsed = 0;
8225 /* Nothing to do strange */
8231 * We don't want to split if splitting
8232 * would generate too many small segments
8233 * less we let an attacker fragment our
8234 * send_map and leave us out of memory.
8236 if ((max_seq != rsm->r_start) &&
8237 (max_seq != rsm->r_end)){
8241 res1 = max_seq - rsm->r_start;
8242 res2 = rsm->r_end - max_seq;
8243 if ((res1 >= (maxseg/8)) &&
8244 (res2 >= (maxseg/8))) {
8245 /* No small pieces here */
8247 } else if (bbr->r_ctl.rc_num_small_maps_alloced < bbr_sack_block_limit) {
8248 /* We are under the limit */
8252 /* Ok do we need to split this rsm? */
8253 if (max_seq == rsm->r_start) {
8254 /* It's this guy no split required */
8256 } else if (max_seq == rsm->r_end) {
8257 /* It's the next one no split required. */
8258 nrsm = TAILQ_NEXT(rsm, r_next);
8263 } else if (can_split && SEQ_LT(max_seq, rsm->r_end)) {
8264 /* yep we need to split it */
8265 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT);
8267 /* failed XXXrrs what can we do mark the whole? */
8272 bbr_log_type_rwnd_collapse(bbr, max_seq, 3, 0);
8273 bbr_clone_rsm(bbr, nrsm, rsm, max_seq);
8274 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
8275 if (rsm->r_in_tmap) {
8276 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8277 nrsm->r_in_tmap = 1;
8281 * Split not allowed just start here just
8287 BBR_STAT_INC(bbr_collapsed_win);
8288 /* reuse fnd as a count */
8290 TAILQ_FOREACH_FROM(nrsm, &bbr->r_ctl.rc_map, r_next) {
8291 nrsm->r_flags |= BBR_RWND_COLLAPSED;
8293 bbr->rc_has_collapsed = 1;
8295 bbr_log_type_rwnd_collapse(bbr, max_seq, 4, fnd);
8299 bbr_un_collapse_window(struct tcp_bbr *bbr)
8301 struct bbr_sendmap *rsm;
8304 TAILQ_FOREACH_REVERSE(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) {
8305 if (rsm->r_flags & BBR_RWND_COLLAPSED) {
8306 /* Clear the flag */
8307 rsm->r_flags &= ~BBR_RWND_COLLAPSED;
8312 bbr_log_type_rwnd_collapse(bbr,
8313 (bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd), 0, cleared);
8314 bbr->rc_has_collapsed = 0;
8318 * Return value of 1, the TCB is unlocked and most
8319 * likely gone, return value of 0, the TCB is still
8323 bbr_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so,
8324 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
8325 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
8328 * Update window information. Don't look at window if no ACK: TAC's
8329 * send garbage on first SYN.
8333 struct tcp_bbr *bbr;
8335 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8336 INP_WLOCK_ASSERT(tp->t_inpcb);
8337 nsegs = max(1, m->m_pkthdr.lro_nsegs);
8338 if ((thflags & TH_ACK) &&
8339 (SEQ_LT(tp->snd_wl1, th->th_seq) ||
8340 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
8341 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
8342 /* keep track of pure window updates */
8344 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
8345 KMOD_TCPSTAT_INC(tcps_rcvwinupd);
8346 tp->snd_wnd = tiwin;
8347 tp->snd_wl1 = th->th_seq;
8348 tp->snd_wl2 = th->th_ack;
8349 if (tp->snd_wnd > tp->max_sndwnd)
8350 tp->max_sndwnd = tp->snd_wnd;
8351 bbr->r_wanted_output = 1;
8352 } else if (thflags & TH_ACK) {
8353 if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) {
8354 tp->snd_wnd = tiwin;
8355 tp->snd_wl1 = th->th_seq;
8356 tp->snd_wl2 = th->th_ack;
8359 if (tp->snd_wnd < ctf_outstanding(tp))
8360 /* The peer collapsed its window on us */
8361 bbr_collapsed_window(bbr);
8362 else if (bbr->rc_has_collapsed)
8363 bbr_un_collapse_window(bbr);
8364 /* Was persist timer active and now we have window space? */
8365 if ((bbr->rc_in_persist != 0) &&
8366 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2),
8367 bbr_minseg(bbr)))) {
8369 * Make the rate persist at end of persist mode if idle long
8372 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8374 /* Make sure we output to start the timer */
8375 bbr->r_wanted_output = 1;
8377 /* Do we need to enter persist? */
8378 if ((bbr->rc_in_persist == 0) &&
8379 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
8380 TCPS_HAVEESTABLISHED(tp->t_state) &&
8381 (tp->snd_max == tp->snd_una) &&
8382 sbavail(&tp->t_inpcb->inp_socket->so_snd) &&
8383 (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) {
8384 /* No send window.. we must enter persist */
8385 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8387 if (tp->t_flags2 & TF2_DROP_AF_DATA) {
8392 * Process segments with URG.
8394 if ((thflags & TH_URG) && th->th_urp &&
8395 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
8397 * This is a kludge, but if we receive and accept random
8398 * urgent pointers, we'll crash in soreceive. It's hard to
8399 * imagine someone actually wanting to send this much urgent
8402 SOCKBUF_LOCK(&so->so_rcv);
8403 if (th->th_urp + sbavail(&so->so_rcv) > sb_max) {
8404 th->th_urp = 0; /* XXX */
8405 thflags &= ~TH_URG; /* XXX */
8406 SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */
8407 goto dodata; /* XXX */
8410 * If this segment advances the known urgent pointer, then
8411 * mark the data stream. This should not happen in
8412 * CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since a
8413 * FIN has been received from the remote side. In these
8414 * states we ignore the URG.
8416 * According to RFC961 (Assigned Protocols), the urgent
8417 * pointer points to the last octet of urgent data. We
8418 * continue, however, to consider it to indicate the first
8419 * octet of data past the urgent section as the original
8420 * spec states (in one of two places).
8422 if (SEQ_GT(th->th_seq + th->th_urp, tp->rcv_up)) {
8423 tp->rcv_up = th->th_seq + th->th_urp;
8424 so->so_oobmark = sbavail(&so->so_rcv) +
8425 (tp->rcv_up - tp->rcv_nxt) - 1;
8426 if (so->so_oobmark == 0)
8427 so->so_rcv.sb_state |= SBS_RCVATMARK;
8429 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
8431 SOCKBUF_UNLOCK(&so->so_rcv);
8433 * Remove out of band data so doesn't get presented to user.
8434 * This can happen independent of advancing the URG pointer,
8435 * but if two URG's are pending at once, some out-of-band
8436 * data may creep in... ick.
8438 if (th->th_urp <= (uint32_t)tlen &&
8439 !(so->so_options & SO_OOBINLINE)) {
8440 /* hdr drop is delayed */
8441 tcp_pulloutofband(so, th, m, drop_hdrlen);
8445 * If no out of band data is expected, pull receive urgent
8446 * pointer along with the receive window.
8448 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
8449 tp->rcv_up = tp->rcv_nxt;
8452 INP_WLOCK_ASSERT(tp->t_inpcb);
8455 * Process the segment text, merging it into the TCP sequencing
8456 * queue, and arranging for acknowledgment of receipt if necessary.
8457 * This process logically involves adjusting tp->rcv_wnd as data is
8458 * presented to the user (this happens in tcp_usrreq.c, case
8459 * PRU_RCVD). If a FIN has already been received on this connection
8460 * then we just ignore the text.
8462 tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
8463 IS_FASTOPEN(tp->t_flags));
8464 if ((tlen || (thflags & TH_FIN) || tfo_syn) &&
8465 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
8466 tcp_seq save_start = th->th_seq;
8467 tcp_seq save_rnxt = tp->rcv_nxt;
8468 int save_tlen = tlen;
8470 m_adj(m, drop_hdrlen); /* delayed header drop */
8472 * Insert segment which includes th into TCP reassembly
8473 * queue with control block tp. Set thflags to whether
8474 * reassembly now includes a segment with FIN. This handles
8475 * the common case inline (segment is the next to be
8476 * received on an established connection, and the queue is
8477 * empty), avoiding linkage into and removal from the queue
8478 * and repetition of various conversions. Set DELACK for
8479 * segments received in order, but ack immediately when
8480 * segments are out of order (so fast retransmit can work).
8482 if (th->th_seq == tp->rcv_nxt &&
8484 (TCPS_HAVEESTABLISHED(tp->t_state) ||
8486 #ifdef NETFLIX_SB_LIMITS
8487 u_int mcnt, appended;
8489 if (so->so_rcv.sb_shlim) {
8492 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
8493 CFO_NOSLEEP, NULL) == false) {
8494 counter_u64_add(tcp_sb_shlim_fails, 1);
8501 if (DELAY_ACK(tp, bbr, nsegs) || tfo_syn) {
8502 bbr->bbr_segs_rcvd += max(1, nsegs);
8503 tp->t_flags |= TF_DELACK;
8504 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8506 bbr->r_wanted_output = 1;
8507 tp->t_flags |= TF_ACKNOW;
8509 tp->rcv_nxt += tlen;
8510 thflags = th->th_flags & TH_FIN;
8511 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs);
8512 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
8513 SOCKBUF_LOCK(&so->so_rcv);
8514 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
8517 #ifdef NETFLIX_SB_LIMITS
8520 sbappendstream_locked(&so->so_rcv, m, 0);
8521 /* NB: sorwakeup_locked() does an implicit unlock. */
8522 sorwakeup_locked(so);
8523 #ifdef NETFLIX_SB_LIMITS
8524 if (so->so_rcv.sb_shlim && appended != mcnt)
8525 counter_fo_release(so->so_rcv.sb_shlim,
8530 * XXX: Due to the header drop above "th" is
8531 * theoretically invalid by now. Fortunately
8532 * m_adj() doesn't actually frees any mbufs when
8533 * trimming from the head.
8535 tcp_seq temp = save_start;
8536 thflags = tcp_reass(tp, th, &temp, &tlen, m);
8537 tp->t_flags |= TF_ACKNOW;
8539 if ((tp->t_flags & TF_SACK_PERMIT) && (save_tlen > 0)) {
8540 if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
8542 * DSACK actually handled in the fastpath
8545 tcp_update_sack_list(tp, save_start,
8546 save_start + save_tlen);
8547 } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
8548 if ((tp->rcv_numsacks >= 1) &&
8549 (tp->sackblks[0].end == save_start)) {
8551 * Partial overlap, recorded at todrop
8554 tcp_update_sack_list(tp,
8555 tp->sackblks[0].start,
8556 tp->sackblks[0].end);
8558 tcp_update_dsack_list(tp, save_start,
8559 save_start + save_tlen);
8561 } else if (tlen >= save_tlen) {
8562 /* Update of sackblks. */
8563 tcp_update_dsack_list(tp, save_start,
8564 save_start + save_tlen);
8565 } else if (tlen > 0) {
8566 tcp_update_dsack_list(tp, save_start,
8576 * If FIN is received ACK the FIN and let the user know that the
8577 * connection is closing.
8579 if (thflags & TH_FIN) {
8580 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
8583 * If connection is half-synchronized (ie NEEDSYN
8584 * flag on) then delay ACK, so it may be piggybacked
8585 * when SYN is sent. Otherwise, since we received a
8586 * FIN then no more input can be expected, send ACK
8589 if (tp->t_flags & TF_NEEDSYN) {
8590 tp->t_flags |= TF_DELACK;
8591 bbr_timer_cancel(bbr,
8592 __LINE__, bbr->r_ctl.rc_rcvtime);
8594 tp->t_flags |= TF_ACKNOW;
8598 switch (tp->t_state) {
8601 * In SYN_RECEIVED and ESTABLISHED STATES enter the
8604 case TCPS_SYN_RECEIVED:
8605 tp->t_starttime = ticks;
8607 case TCPS_ESTABLISHED:
8608 tcp_state_change(tp, TCPS_CLOSE_WAIT);
8612 * If still in FIN_WAIT_1 STATE FIN has not been
8613 * acked so enter the CLOSING state.
8615 case TCPS_FIN_WAIT_1:
8616 tcp_state_change(tp, TCPS_CLOSING);
8620 * In FIN_WAIT_2 state enter the TIME_WAIT state,
8621 * starting the time-wait timer, turning off the
8622 * other standard timers.
8624 case TCPS_FIN_WAIT_2:
8625 bbr->rc_timer_first = 1;
8626 bbr_timer_cancel(bbr,
8627 __LINE__, bbr->r_ctl.rc_rcvtime);
8628 INP_WLOCK_ASSERT(tp->t_inpcb);
8634 * Return any desired output.
8636 if ((tp->t_flags & TF_ACKNOW) ||
8637 (sbavail(&so->so_snd) > ctf_outstanding(tp))) {
8638 bbr->r_wanted_output = 1;
8640 INP_WLOCK_ASSERT(tp->t_inpcb);
8645 * Here nothing is really faster, its just that we
8646 * have broken out the fast-data path also just like
8647 * the fast-ack. Return 1 if we processed the packet
8648 * return 0 if you need to take the "slow-path".
8651 bbr_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so,
8652 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
8653 uint32_t tiwin, int32_t nxt_pkt)
8656 int32_t newsize = 0; /* automatic sockbuf scaling */
8657 struct tcp_bbr *bbr;
8658 #ifdef NETFLIX_SB_LIMITS
8659 u_int mcnt, appended;
8663 * The size of tcp_saveipgen must be the size of the max ip header,
8666 u_char tcp_saveipgen[IP6_HDR_LEN];
8667 struct tcphdr tcp_savetcp;
8671 /* On the hpts and we would have called output */
8672 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8675 * If last ACK falls within this segment's sequence numbers, record
8676 * the timestamp. NOTE that the test is modified according to the
8677 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
8679 if (bbr->r_ctl.rc_resend != NULL) {
8682 if (tiwin && tiwin != tp->snd_wnd) {
8685 if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) {
8688 if (__predict_false((to->to_flags & TOF_TS) &&
8689 (TSTMP_LT(to->to_tsval, tp->ts_recent)))) {
8692 if (__predict_false((th->th_ack != tp->snd_una))) {
8695 if (__predict_false(tlen > sbspace(&so->so_rcv))) {
8698 if ((to->to_flags & TOF_TS) != 0 &&
8699 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
8700 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
8701 tp->ts_recent = to->to_tsval;
8704 * This is a pure, in-sequence data packet with nothing on the
8705 * reassembly queue and we have enough buffer space to take it.
8707 nsegs = max(1, m->m_pkthdr.lro_nsegs);
8709 #ifdef NETFLIX_SB_LIMITS
8710 if (so->so_rcv.sb_shlim) {
8713 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
8714 CFO_NOSLEEP, NULL) == false) {
8715 counter_u64_add(tcp_sb_shlim_fails, 1);
8721 /* Clean receiver SACK report if present */
8722 if (tp->rcv_numsacks)
8723 tcp_clean_sackreport(tp);
8724 KMOD_TCPSTAT_INC(tcps_preddat);
8725 tp->rcv_nxt += tlen;
8727 * Pull snd_wl1 up to prevent seq wrap relative to th_seq.
8729 tp->snd_wl1 = th->th_seq;
8731 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt.
8733 tp->rcv_up = tp->rcv_nxt;
8734 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs);
8735 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
8737 if (so->so_options & SO_DEBUG)
8738 tcp_trace(TA_INPUT, ostate, tp,
8739 (void *)tcp_saveipgen, &tcp_savetcp, 0);
8741 newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
8743 /* Add data to socket buffer. */
8744 SOCKBUF_LOCK(&so->so_rcv);
8745 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
8749 * Set new socket buffer size. Give up when limit is
8753 if (!sbreserve_locked(&so->so_rcv,
8755 so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
8756 m_adj(m, drop_hdrlen); /* delayed header drop */
8758 #ifdef NETFLIX_SB_LIMITS
8761 sbappendstream_locked(&so->so_rcv, m, 0);
8762 ctf_calc_rwin(so, tp);
8764 /* NB: sorwakeup_locked() does an implicit unlock. */
8765 sorwakeup_locked(so);
8766 #ifdef NETFLIX_SB_LIMITS
8767 if (so->so_rcv.sb_shlim && mcnt != appended)
8768 counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended);
8770 if (DELAY_ACK(tp, bbr, nsegs)) {
8771 bbr->bbr_segs_rcvd += max(1, nsegs);
8772 tp->t_flags |= TF_DELACK;
8773 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8775 bbr->r_wanted_output = 1;
8776 tp->t_flags |= TF_ACKNOW;
8782 * This subfunction is used to try to highly optimize the
8783 * fast path. We again allow window updates that are
8784 * in sequence to remain in the fast-path. We also add
8785 * in the __predict's to attempt to help the compiler.
8786 * Note that if we return a 0, then we can *not* process
8787 * it and the caller should push the packet into the
8788 * slow-path. If we return 1, then all is well and
8789 * the packet is fully processed.
8792 bbr_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
8793 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
8794 uint32_t tiwin, int32_t nxt_pkt)
8798 uint32_t sack_changed;
8801 * The size of tcp_saveipgen must be the size of the max ip header,
8804 u_char tcp_saveipgen[IP6_HDR_LEN];
8805 struct tcphdr tcp_savetcp;
8809 uint32_t prev_acked = 0;
8810 struct tcp_bbr *bbr;
8812 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
8813 /* Old ack, behind (or duplicate to) the last one rcv'd */
8816 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) {
8817 /* Above what we have sent? */
8820 if (__predict_false(tiwin == 0)) {
8824 if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) {
8825 /* We need a SYN or a FIN, unlikely.. */
8828 if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) {
8829 /* Timestamp is behind .. old ack with seq wrap? */
8832 if (__predict_false(IN_RECOVERY(tp->t_flags))) {
8833 /* Still recovering */
8836 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8837 if (__predict_false(bbr->r_ctl.rc_resend != NULL)) {
8838 /* We are retransmitting */
8841 if (__predict_false(bbr->rc_in_persist != 0)) {
8842 /* In persist mode */
8845 if (bbr->r_ctl.rc_sacked) {
8846 /* We have sack holes on our scoreboard */
8849 /* Ok if we reach here, we can process a fast-ack */
8850 nsegs = max(1, m->m_pkthdr.lro_nsegs);
8851 sack_changed = bbr_log_ack(tp, to, th, &prev_acked);
8853 * We never detect loss in fast ack [we can't
8854 * have a sack and can't be in recovery so
8855 * we always pass 0 (nothing detected)].
8857 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, 0);
8858 /* Did the window get updated? */
8859 if (tiwin != tp->snd_wnd) {
8860 tp->snd_wnd = tiwin;
8861 tp->snd_wl1 = th->th_seq;
8862 if (tp->snd_wnd > tp->max_sndwnd)
8863 tp->max_sndwnd = tp->snd_wnd;
8865 /* Do we need to exit persists? */
8866 if ((bbr->rc_in_persist != 0) &&
8867 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2),
8868 bbr_minseg(bbr)))) {
8869 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8870 bbr->r_wanted_output = 1;
8872 /* Do we need to enter persists? */
8873 if ((bbr->rc_in_persist == 0) &&
8874 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
8875 TCPS_HAVEESTABLISHED(tp->t_state) &&
8876 (tp->snd_max == tp->snd_una) &&
8877 sbavail(&tp->t_inpcb->inp_socket->so_snd) &&
8878 (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) {
8879 /* No send window.. we must enter persist */
8880 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8883 * If last ACK falls within this segment's sequence numbers, record
8884 * the timestamp. NOTE that the test is modified according to the
8885 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
8887 if ((to->to_flags & TOF_TS) != 0 &&
8888 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
8889 tp->ts_recent_age = bbr->r_ctl.rc_rcvtime;
8890 tp->ts_recent = to->to_tsval;
8893 * This is a pure ack for outstanding data.
8895 KMOD_TCPSTAT_INC(tcps_predack);
8898 * "bad retransmit" recovery.
8900 if (tp->t_flags & TF_PREVVALID) {
8901 tp->t_flags &= ~TF_PREVVALID;
8902 if (tp->t_rxtshift == 1 &&
8903 (int)(ticks - tp->t_badrxtwin) < 0)
8904 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL);
8907 * Recalculate the transmit timer / rtt.
8909 * Some boxes send broken timestamp replies during the SYN+ACK
8910 * phase, ignore timestamps of 0 or we could calculate a huge RTT
8911 * and blow up the retransmit timer.
8913 acked = BYTES_THIS_ACK(tp, th);
8916 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
8917 hhook_run_tcp_est_in(tp, th, to);
8920 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs);
8921 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
8922 sbdrop(&so->so_snd, acked);
8924 if (SEQ_GT(th->th_ack, tp->snd_una))
8925 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp));
8926 tp->snd_una = th->th_ack;
8927 if (tp->snd_wnd < ctf_outstanding(tp))
8928 /* The peer collapsed its window on us */
8929 bbr_collapsed_window(bbr);
8930 else if (bbr->rc_has_collapsed)
8931 bbr_un_collapse_window(bbr);
8933 if (SEQ_GT(tp->snd_una, tp->snd_recover)) {
8934 tp->snd_recover = tp->snd_una;
8936 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, 0);
8938 * Pull snd_wl2 up to prevent seq wrap relative to th_ack.
8940 tp->snd_wl2 = th->th_ack;
8943 * If all outstanding data are acked, stop retransmit timer,
8944 * otherwise restart timer using current (possibly backed-off)
8945 * value. If process is waiting for space, wakeup/selwakeup/signal.
8946 * If data are ready to send, let tcp_output decide between more
8947 * output or persist.
8950 if (so->so_options & SO_DEBUG)
8951 tcp_trace(TA_INPUT, ostate, tp,
8952 (void *)tcp_saveipgen,
8955 /* Wake up the socket if we have room to write more */
8957 if (tp->snd_una == tp->snd_max) {
8958 /* Nothing left outstanding */
8959 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__);
8960 if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0)
8961 bbr->rc_tp->t_acktime = 0;
8962 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8963 if (bbr->rc_in_persist == 0) {
8964 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime;
8966 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
8967 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime);
8969 * We invalidate the last ack here since we
8970 * don't want to transfer forward the time
8971 * for our sum's calculations.
8973 bbr->r_wanted_output = 1;
8975 if (sbavail(&so->so_snd)) {
8976 bbr->r_wanted_output = 1;
8982 * Return value of 1, the TCB is unlocked and most
8983 * likely gone, return value of 0, the TCB is still
8987 bbr_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so,
8988 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
8989 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
8992 int32_t ourfinisacked = 0;
8993 struct tcp_bbr *bbr;
8994 int32_t ret_val = 0;
8996 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8997 ctf_calc_rwin(so, tp);
8999 * If the state is SYN_SENT: if seg contains an ACK, but not for our
9000 * SYN, drop the input. if seg contains a RST, then drop the
9001 * connection. if seg does not contain SYN, then drop it. Otherwise
9002 * this is an acceptable SYN segment initialize tp->rcv_nxt and
9003 * tp->irs if seg contains ack then advance tp->snd_una. BRR does
9004 * not support ECN so we will not say we are capable. if SYN has
9005 * been acked change to ESTABLISHED else SYN_RCVD state arrange for
9006 * segment to be acked (eventually) continue processing rest of
9007 * data/controls, beginning with URG
9009 if ((thflags & TH_ACK) &&
9010 (SEQ_LEQ(th->th_ack, tp->iss) ||
9011 SEQ_GT(th->th_ack, tp->snd_max))) {
9012 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9015 if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) {
9016 TCP_PROBE5(connect__refused, NULL, tp,
9017 mtod(m, const char *), tp, th);
9018 tp = tcp_drop(tp, ECONNREFUSED);
9022 if (thflags & TH_RST) {
9026 if (!(thflags & TH_SYN)) {
9030 tp->irs = th->th_seq;
9032 if (thflags & TH_ACK) {
9033 int tfo_partial = 0;
9035 KMOD_TCPSTAT_INC(tcps_connects);
9038 mac_socketpeer_set_from_mbuf(m, so);
9040 /* Do window scaling on this connection? */
9041 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
9042 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
9043 tp->rcv_scale = tp->request_r_scale;
9045 tp->rcv_adv += min(tp->rcv_wnd,
9046 TCP_MAXWIN << tp->rcv_scale);
9048 * If not all the data that was sent in the TFO SYN
9049 * has been acked, resend the remainder right away.
9051 if (IS_FASTOPEN(tp->t_flags) &&
9052 (tp->snd_una != tp->snd_max)) {
9053 tp->snd_nxt = th->th_ack;
9057 * If there's data, delay ACK; if there's also a FIN ACKNOW
9058 * will be turned on later.
9060 if (DELAY_ACK(tp, bbr, 1) && tlen != 0 && (tfo_partial == 0)) {
9061 bbr->bbr_segs_rcvd += 1;
9062 tp->t_flags |= TF_DELACK;
9063 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
9065 bbr->r_wanted_output = 1;
9066 tp->t_flags |= TF_ACKNOW;
9068 if (SEQ_GT(th->th_ack, tp->iss)) {
9071 * handle it specially.
9073 bbr_log_syn(tp, to);
9075 if (SEQ_GT(th->th_ack, tp->snd_una)) {
9077 * We advance snd_una for the
9078 * fast open case. If th_ack is
9079 * acknowledging data beyond
9080 * snd_una we can't just call
9081 * ack-processing since the
9082 * data stream in our send-map
9083 * will start at snd_una + 1 (one
9084 * beyond the SYN). If its just
9085 * equal we don't need to do that
9086 * and there is no send_map.
9091 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions:
9092 * SYN_SENT --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1
9094 tp->t_starttime = ticks;
9095 if (tp->t_flags & TF_NEEDFIN) {
9096 tcp_state_change(tp, TCPS_FIN_WAIT_1);
9097 tp->t_flags &= ~TF_NEEDFIN;
9100 tcp_state_change(tp, TCPS_ESTABLISHED);
9101 TCP_PROBE5(connect__established, NULL, tp,
9102 mtod(m, const char *), tp, th);
9107 * Received initial SYN in SYN-SENT[*] state => simultaneous
9108 * open. If segment contains CC option and there is a
9109 * cached CC, apply TAO test. If it succeeds, connection is *
9110 * half-synchronized. Otherwise, do 3-way handshake:
9111 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If
9112 * there was no CC option, clear cached CC value.
9114 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
9115 tcp_state_change(tp, TCPS_SYN_RECEIVED);
9117 INP_WLOCK_ASSERT(tp->t_inpcb);
9119 * Advance th->th_seq to correspond to first data byte. If data,
9120 * trim to stay within window, dropping FIN if necessary.
9123 if (tlen > tp->rcv_wnd) {
9124 todrop = tlen - tp->rcv_wnd;
9128 KMOD_TCPSTAT_INC(tcps_rcvpackafterwin);
9129 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
9131 tp->snd_wl1 = th->th_seq - 1;
9132 tp->rcv_up = th->th_seq;
9134 * Client side of transaction: already sent SYN and data. If the
9135 * remote host used T/TCP to validate the SYN, our data will be
9136 * ACK'd; if so, enter normal data segment processing in the middle
9137 * of step 5, ack processing. Otherwise, goto step 6.
9139 if (thflags & TH_ACK) {
9140 if ((to->to_flags & TOF_TS) != 0) {
9143 t = tcp_tv_to_mssectick(&bbr->rc_tv);
9144 if (TSTMP_GEQ(t, to->to_tsecr)) {
9145 rtt = t - to->to_tsecr;
9150 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0);
9151 apply_filter_min_small(&bbr->r_ctl.rc_rttprop,
9152 rtt, bbr->r_ctl.rc_rcvtime);
9155 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val))
9157 /* We may have changed to FIN_WAIT_1 above */
9158 if (tp->t_state == TCPS_FIN_WAIT_1) {
9160 * In FIN_WAIT_1 STATE in addition to the processing
9161 * for the ESTABLISHED state if our FIN is now
9162 * acknowledged then enter FIN_WAIT_2.
9164 if (ourfinisacked) {
9166 * If we can't receive any more data, then
9167 * closing user can proceed. Starting the
9168 * timer is contrary to the specification,
9169 * but if we don't get a FIN we'll hang
9172 * XXXjl: we should release the tp also, and
9173 * use a compressed state.
9175 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
9176 soisdisconnected(so);
9177 tcp_timer_activate(tp, TT_2MSL,
9178 (tcp_fast_finwait2_recycle ?
9179 tcp_finwait2_timeout :
9182 tcp_state_change(tp, TCPS_FIN_WAIT_2);
9186 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9187 tiwin, thflags, nxt_pkt));
9191 * Return value of 1, the TCB is unlocked and most
9192 * likely gone, return value of 0, the TCB is still
9196 bbr_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so,
9197 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9198 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
9200 int32_t ourfinisacked = 0;
9202 struct tcp_bbr *bbr;
9204 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9205 ctf_calc_rwin(so, tp);
9206 if ((thflags & TH_ACK) &&
9207 (SEQ_LEQ(th->th_ack, tp->snd_una) ||
9208 SEQ_GT(th->th_ack, tp->snd_max))) {
9209 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9212 if (IS_FASTOPEN(tp->t_flags)) {
9214 * When a TFO connection is in SYN_RECEIVED, the only valid
9215 * packets are the initial SYN, a retransmit/copy of the
9216 * initial SYN (possibly with a subset of the original
9217 * data), a valid ACK, a FIN, or a RST.
9219 if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) {
9220 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9222 } else if (thflags & TH_SYN) {
9223 /* non-initial SYN is ignored */
9224 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RXT) ||
9225 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_TLP) ||
9226 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) {
9227 ctf_do_drop(m, NULL);
9230 } else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) {
9231 ctf_do_drop(m, NULL);
9235 if ((thflags & TH_RST) ||
9236 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9237 return (ctf_process_rst(m, th, so, tp));
9239 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9240 * it's less than ts_recent, drop it.
9242 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9243 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9244 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9248 * In the SYN-RECEIVED state, validate that the packet belongs to
9249 * this connection before trimming the data to fit the receive
9250 * window. Check the sequence number versus IRS since we know the
9251 * sequence numbers haven't wrapped. This is a partial fix for the
9252 * "LAND" DoS attack.
9254 if (SEQ_LT(th->th_seq, tp->irs)) {
9255 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9258 INP_WLOCK_ASSERT(tp->t_inpcb);
9259 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9263 * If last ACK falls within this segment's sequence numbers, record
9264 * its timestamp. NOTE: 1) That the test incorporates suggestions
9265 * from the latest proposal of the tcplw@cray.com list (Braden
9266 * 1993/04/26). 2) That updating only on newer timestamps interferes
9267 * with our earlier PAWS tests, so this check should be solely
9268 * predicated on the sequence space of this segment. 3) That we
9269 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9270 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9271 * SEG.Len, This modified check allows us to overcome RFC1323's
9272 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9273 * p.869. In such cases, we can still calculate the RTT correctly
9274 * when RCV.NXT == Last.ACK.Sent.
9276 if ((to->to_flags & TOF_TS) != 0 &&
9277 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9278 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9279 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9280 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9281 tp->ts_recent = to->to_tsval;
9283 tp->snd_wnd = tiwin;
9285 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9286 * is on (half-synchronized state), then queue data for later
9287 * processing; else drop segment and return.
9289 if ((thflags & TH_ACK) == 0) {
9290 if (IS_FASTOPEN(tp->t_flags)) {
9293 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9294 tiwin, thflags, nxt_pkt));
9296 KMOD_TCPSTAT_INC(tcps_connects);
9298 /* Do window scaling? */
9299 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
9300 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
9301 tp->rcv_scale = tp->request_r_scale;
9304 * ok for the first time in lets see if we can use the ts to figure
9305 * out what the initial RTT was.
9307 if ((to->to_flags & TOF_TS) != 0) {
9310 t = tcp_tv_to_mssectick(&bbr->rc_tv);
9311 if (TSTMP_GEQ(t, to->to_tsecr)) {
9312 rtt = t - to->to_tsecr;
9317 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0);
9318 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, bbr->r_ctl.rc_rcvtime);
9321 /* Drop off any SYN in the send map (probably not there) */
9322 if (thflags & TH_ACK)
9323 bbr_log_syn(tp, to);
9324 if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
9326 tcp_fastopen_decrement_counter(tp->t_tfo_pending);
9327 tp->t_tfo_pending = NULL;
9329 * Account for the ACK of our SYN prior to regular
9330 * ACK processing below.
9335 * Make transitions: SYN-RECEIVED -> ESTABLISHED SYN-RECEIVED* ->
9338 tp->t_starttime = ticks;
9339 if (tp->t_flags & TF_NEEDFIN) {
9340 tcp_state_change(tp, TCPS_FIN_WAIT_1);
9341 tp->t_flags &= ~TF_NEEDFIN;
9343 tcp_state_change(tp, TCPS_ESTABLISHED);
9344 TCP_PROBE5(accept__established, NULL, tp,
9345 mtod(m, const char *), tp, th);
9347 * TFO connections call cc_conn_init() during SYN
9348 * processing. Calling it again here for such connections
9349 * is not harmless as it would undo the snd_cwnd reduction
9350 * that occurs when a TFO SYN|ACK is retransmitted.
9352 if (!IS_FASTOPEN(tp->t_flags))
9356 * If segment contains data or ACK, will call tcp_reass() later; if
9357 * not, do so now to pass queued data to user.
9359 if (tlen == 0 && (thflags & TH_FIN) == 0)
9360 (void)tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
9362 tp->snd_wl1 = th->th_seq - 1;
9363 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9366 if (tp->t_state == TCPS_FIN_WAIT_1) {
9367 /* We could have went to FIN_WAIT_1 (or EST) above */
9369 * In FIN_WAIT_1 STATE in addition to the processing for the
9370 * ESTABLISHED state if our FIN is now acknowledged then
9373 if (ourfinisacked) {
9375 * If we can't receive any more data, then closing
9376 * user can proceed. Starting the timer is contrary
9377 * to the specification, but if we don't get a FIN
9378 * we'll hang forever.
9380 * XXXjl: we should release the tp also, and use a
9383 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
9384 soisdisconnected(so);
9385 tcp_timer_activate(tp, TT_2MSL,
9386 (tcp_fast_finwait2_recycle ?
9387 tcp_finwait2_timeout :
9390 tcp_state_change(tp, TCPS_FIN_WAIT_2);
9393 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9394 tiwin, thflags, nxt_pkt));
9398 * Return value of 1, the TCB is unlocked and most
9399 * likely gone, return value of 0, the TCB is still
9403 bbr_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so,
9404 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9405 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
9407 struct tcp_bbr *bbr;
9411 * Header prediction: check for the two common cases of a
9412 * uni-directional data xfer. If the packet has no control flags,
9413 * is in-sequence, the window didn't change and we're not
9414 * retransmitting, it's a candidate. If the length is zero and the
9415 * ack moved forward, we're the sender side of the xfer. Just free
9416 * the data acked & wake any higher level process that was blocked
9417 * waiting for space. If the length is non-zero and the ack didn't
9418 * move, we're the receiver side. If we're getting packets in-order
9419 * (the reassembly queue is empty), add the data toc The socket
9420 * buffer and note that we need a delayed ack. Make sure that the
9421 * hidden state-flags are also off. Since we check for
9422 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN.
9424 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9425 if (bbr->r_ctl.rc_delivered < (4 * tp->t_maxseg)) {
9427 * If we have delived under 4 segments increase the initial
9428 * window if raised by the peer. We use this to determine
9429 * dynamic and static rwnd's at the end of a connection.
9431 bbr->r_ctl.rc_init_rwnd = max(tiwin, tp->snd_wnd);
9433 if (__predict_true(((to->to_flags & TOF_SACK) == 0)) &&
9434 __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_URG | TH_ACK)) == TH_ACK) &&
9435 __predict_true(SEGQ_EMPTY(tp)) &&
9436 __predict_true(th->th_seq == tp->rcv_nxt)) {
9438 if (bbr_fastack(m, th, so, tp, to, drop_hdrlen, tlen,
9443 if (bbr_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen,
9449 ctf_calc_rwin(so, tp);
9451 if ((thflags & TH_RST) ||
9452 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9453 return (ctf_process_rst(m, th, so, tp));
9455 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9456 * synchronized state.
9458 if (thflags & TH_SYN) {
9459 ctf_challenge_ack(m, th, tp, &ret_val);
9463 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9464 * it's less than ts_recent, drop it.
9466 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9467 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9468 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9471 INP_WLOCK_ASSERT(tp->t_inpcb);
9472 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9476 * If last ACK falls within this segment's sequence numbers, record
9477 * its timestamp. NOTE: 1) That the test incorporates suggestions
9478 * from the latest proposal of the tcplw@cray.com list (Braden
9479 * 1993/04/26). 2) That updating only on newer timestamps interferes
9480 * with our earlier PAWS tests, so this check should be solely
9481 * predicated on the sequence space of this segment. 3) That we
9482 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9483 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9484 * SEG.Len, This modified check allows us to overcome RFC1323's
9485 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9486 * p.869. In such cases, we can still calculate the RTT correctly
9487 * when RCV.NXT == Last.ACK.Sent.
9489 if ((to->to_flags & TOF_TS) != 0 &&
9490 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9491 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9492 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9493 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9494 tp->ts_recent = to->to_tsval;
9497 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9498 * is on (half-synchronized state), then queue data for later
9499 * processing; else drop segment and return.
9501 if ((thflags & TH_ACK) == 0) {
9502 if (tp->t_flags & TF_NEEDSYN) {
9503 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9504 tiwin, thflags, nxt_pkt));
9505 } else if (tp->t_flags & TF_ACKNOW) {
9506 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9507 bbr->r_wanted_output = 1;
9510 ctf_do_drop(m, NULL);
9517 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
9520 if (sbavail(&so->so_snd)) {
9521 if (bbr_progress_timeout_check(bbr)) {
9522 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9526 /* State changes only happen in bbr_process_data() */
9527 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9528 tiwin, thflags, nxt_pkt));
9532 * Return value of 1, the TCB is unlocked and most
9533 * likely gone, return value of 0, the TCB is still
9537 bbr_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so,
9538 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9539 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
9541 struct tcp_bbr *bbr;
9544 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9545 ctf_calc_rwin(so, tp);
9546 if ((thflags & TH_RST) ||
9547 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9548 return (ctf_process_rst(m, th, so, tp));
9550 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9551 * synchronized state.
9553 if (thflags & TH_SYN) {
9554 ctf_challenge_ack(m, th, tp, &ret_val);
9558 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9559 * it's less than ts_recent, drop it.
9561 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9562 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9563 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9566 INP_WLOCK_ASSERT(tp->t_inpcb);
9567 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9571 * If last ACK falls within this segment's sequence numbers, record
9572 * its timestamp. NOTE: 1) That the test incorporates suggestions
9573 * from the latest proposal of the tcplw@cray.com list (Braden
9574 * 1993/04/26). 2) That updating only on newer timestamps interferes
9575 * with our earlier PAWS tests, so this check should be solely
9576 * predicated on the sequence space of this segment. 3) That we
9577 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9578 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9579 * SEG.Len, This modified check allows us to overcome RFC1323's
9580 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9581 * p.869. In such cases, we can still calculate the RTT correctly
9582 * when RCV.NXT == Last.ACK.Sent.
9584 if ((to->to_flags & TOF_TS) != 0 &&
9585 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9586 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9587 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9588 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9589 tp->ts_recent = to->to_tsval;
9592 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9593 * is on (half-synchronized state), then queue data for later
9594 * processing; else drop segment and return.
9596 if ((thflags & TH_ACK) == 0) {
9597 if (tp->t_flags & TF_NEEDSYN) {
9598 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9599 tiwin, thflags, nxt_pkt));
9600 } else if (tp->t_flags & TF_ACKNOW) {
9601 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9602 bbr->r_wanted_output = 1;
9605 ctf_do_drop(m, NULL);
9612 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
9615 if (sbavail(&so->so_snd)) {
9616 if (bbr_progress_timeout_check(bbr)) {
9617 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9621 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9622 tiwin, thflags, nxt_pkt));
9626 bbr_check_data_after_close(struct mbuf *m, struct tcp_bbr *bbr,
9627 struct tcpcb *tp, int32_t * tlen, struct tcphdr *th, struct socket *so)
9630 if (bbr->rc_allow_data_af_clo == 0) {
9633 KMOD_TCPSTAT_INC(tcps_rcvafterclose);
9634 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen));
9637 if (sbavail(&so->so_snd) == 0)
9639 /* Ok we allow data that is ignored and a followup reset */
9640 tp->rcv_nxt = th->th_seq + *tlen;
9641 tp->t_flags2 |= TF2_DROP_AF_DATA;
9642 bbr->r_wanted_output = 1;
9648 * Return value of 1, the TCB is unlocked and most
9649 * likely gone, return value of 0, the TCB is still
9653 bbr_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so,
9654 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9655 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
9657 int32_t ourfinisacked = 0;
9659 struct tcp_bbr *bbr;
9661 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9662 ctf_calc_rwin(so, tp);
9663 if ((thflags & TH_RST) ||
9664 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9665 return (ctf_process_rst(m, th, so, tp));
9667 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9668 * synchronized state.
9670 if (thflags & TH_SYN) {
9671 ctf_challenge_ack(m, th, tp, &ret_val);
9675 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9676 * it's less than ts_recent, drop it.
9678 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9679 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9680 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9683 INP_WLOCK_ASSERT(tp->t_inpcb);
9684 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9688 * If new data are received on a connection after the user processes
9689 * are gone, then RST the other end.
9691 if ((so->so_state & SS_NOFDREF) && tlen) {
9693 * We call a new function now so we might continue and setup
9694 * to reset at all data being ack'd.
9696 if (bbr_check_data_after_close(m, bbr, tp, &tlen, th, so))
9700 * If last ACK falls within this segment's sequence numbers, record
9701 * its timestamp. NOTE: 1) That the test incorporates suggestions
9702 * from the latest proposal of the tcplw@cray.com list (Braden
9703 * 1993/04/26). 2) That updating only on newer timestamps interferes
9704 * with our earlier PAWS tests, so this check should be solely
9705 * predicated on the sequence space of this segment. 3) That we
9706 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9707 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9708 * SEG.Len, This modified check allows us to overcome RFC1323's
9709 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9710 * p.869. In such cases, we can still calculate the RTT correctly
9711 * when RCV.NXT == Last.ACK.Sent.
9713 if ((to->to_flags & TOF_TS) != 0 &&
9714 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9715 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9716 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9717 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9718 tp->ts_recent = to->to_tsval;
9721 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9722 * is on (half-synchronized state), then queue data for later
9723 * processing; else drop segment and return.
9725 if ((thflags & TH_ACK) == 0) {
9726 if (tp->t_flags & TF_NEEDSYN) {
9727 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9728 tiwin, thflags, nxt_pkt));
9729 } else if (tp->t_flags & TF_ACKNOW) {
9730 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9731 bbr->r_wanted_output = 1;
9734 ctf_do_drop(m, NULL);
9741 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9744 if (ourfinisacked) {
9746 * If we can't receive any more data, then closing user can
9747 * proceed. Starting the timer is contrary to the
9748 * specification, but if we don't get a FIN we'll hang
9751 * XXXjl: we should release the tp also, and use a
9754 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
9755 soisdisconnected(so);
9756 tcp_timer_activate(tp, TT_2MSL,
9757 (tcp_fast_finwait2_recycle ?
9758 tcp_finwait2_timeout :
9761 tcp_state_change(tp, TCPS_FIN_WAIT_2);
9763 if (sbavail(&so->so_snd)) {
9764 if (bbr_progress_timeout_check(bbr)) {
9765 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9769 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9770 tiwin, thflags, nxt_pkt));
9774 * Return value of 1, the TCB is unlocked and most
9775 * likely gone, return value of 0, the TCB is still
9779 bbr_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so,
9780 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9781 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
9783 int32_t ourfinisacked = 0;
9785 struct tcp_bbr *bbr;
9787 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9788 ctf_calc_rwin(so, tp);
9789 if ((thflags & TH_RST) ||
9790 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9791 return (ctf_process_rst(m, th, so, tp));
9793 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9794 * synchronized state.
9796 if (thflags & TH_SYN) {
9797 ctf_challenge_ack(m, th, tp, &ret_val);
9801 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9802 * it's less than ts_recent, drop it.
9804 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9805 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9806 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9809 INP_WLOCK_ASSERT(tp->t_inpcb);
9810 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9814 * If new data are received on a connection after the user processes
9815 * are gone, then RST the other end.
9817 if ((so->so_state & SS_NOFDREF) && tlen) {
9819 * We call a new function now so we might continue and setup
9820 * to reset at all data being ack'd.
9822 if (bbr_check_data_after_close(m, bbr, tp, &tlen, th, so))
9826 * If last ACK falls within this segment's sequence numbers, record
9827 * its timestamp. NOTE: 1) That the test incorporates suggestions
9828 * from the latest proposal of the tcplw@cray.com list (Braden
9829 * 1993/04/26). 2) That updating only on newer timestamps interferes
9830 * with our earlier PAWS tests, so this check should be solely
9831 * predicated on the sequence space of this segment. 3) That we
9832 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9833 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9834 * SEG.Len, This modified check allows us to overcome RFC1323's
9835 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9836 * p.869. In such cases, we can still calculate the RTT correctly
9837 * when RCV.NXT == Last.ACK.Sent.
9839 if ((to->to_flags & TOF_TS) != 0 &&
9840 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9841 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9842 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9843 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9844 tp->ts_recent = to->to_tsval;
9847 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9848 * is on (half-synchronized state), then queue data for later
9849 * processing; else drop segment and return.
9851 if ((thflags & TH_ACK) == 0) {
9852 if (tp->t_flags & TF_NEEDSYN) {
9853 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9854 tiwin, thflags, nxt_pkt));
9855 } else if (tp->t_flags & TF_ACKNOW) {
9856 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9857 bbr->r_wanted_output = 1;
9860 ctf_do_drop(m, NULL);
9867 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9870 if (ourfinisacked) {
9875 if (sbavail(&so->so_snd)) {
9876 if (bbr_progress_timeout_check(bbr)) {
9877 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9881 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9882 tiwin, thflags, nxt_pkt));
9886 * Return value of 1, the TCB is unlocked and most
9887 * likely gone, return value of 0, the TCB is still
9891 bbr_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
9892 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9893 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
9895 int32_t ourfinisacked = 0;
9897 struct tcp_bbr *bbr;
9899 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9900 ctf_calc_rwin(so, tp);
9901 if ((thflags & TH_RST) ||
9902 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9903 return (ctf_process_rst(m, th, so, tp));
9905 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9906 * synchronized state.
9908 if (thflags & TH_SYN) {
9909 ctf_challenge_ack(m, th, tp, &ret_val);
9913 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9914 * it's less than ts_recent, drop it.
9916 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9917 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9918 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9921 INP_WLOCK_ASSERT(tp->t_inpcb);
9922 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9926 * If new data are received on a connection after the user processes
9927 * are gone, then RST the other end.
9929 if ((so->so_state & SS_NOFDREF) && tlen) {
9931 * We call a new function now so we might continue and setup
9932 * to reset at all data being ack'd.
9934 if (bbr_check_data_after_close(m, bbr, tp, &tlen, th, so))
9938 * If last ACK falls within this segment's sequence numbers, record
9939 * its timestamp. NOTE: 1) That the test incorporates suggestions
9940 * from the latest proposal of the tcplw@cray.com list (Braden
9941 * 1993/04/26). 2) That updating only on newer timestamps interferes
9942 * with our earlier PAWS tests, so this check should be solely
9943 * predicated on the sequence space of this segment. 3) That we
9944 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9945 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9946 * SEG.Len, This modified check allows us to overcome RFC1323's
9947 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9948 * p.869. In such cases, we can still calculate the RTT correctly
9949 * when RCV.NXT == Last.ACK.Sent.
9951 if ((to->to_flags & TOF_TS) != 0 &&
9952 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9953 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9954 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9955 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9956 tp->ts_recent = to->to_tsval;
9959 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9960 * is on (half-synchronized state), then queue data for later
9961 * processing; else drop segment and return.
9963 if ((thflags & TH_ACK) == 0) {
9964 if (tp->t_flags & TF_NEEDSYN) {
9965 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9966 tiwin, thflags, nxt_pkt));
9967 } else if (tp->t_flags & TF_ACKNOW) {
9968 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9969 bbr->r_wanted_output = 1;
9972 ctf_do_drop(m, NULL);
9977 * case TCPS_LAST_ACK: Ack processing.
9979 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9982 if (ourfinisacked) {
9987 if (sbavail(&so->so_snd)) {
9988 if (bbr_progress_timeout_check(bbr)) {
9989 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9993 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9994 tiwin, thflags, nxt_pkt));
9999 * Return value of 1, the TCB is unlocked and most
10000 * likely gone, return value of 0, the TCB is still
10004 bbr_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so,
10005 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
10006 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
10008 int32_t ourfinisacked = 0;
10010 struct tcp_bbr *bbr;
10012 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
10013 ctf_calc_rwin(so, tp);
10014 /* Reset receive buffer auto scaling when not in bulk receive mode. */
10015 if ((thflags & TH_RST) ||
10016 (tp->t_fin_is_rst && (thflags & TH_FIN)))
10017 return (ctf_process_rst(m, th, so, tp));
10020 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
10021 * synchronized state.
10023 if (thflags & TH_SYN) {
10024 ctf_challenge_ack(m, th, tp, &ret_val);
10027 INP_WLOCK_ASSERT(tp->t_inpcb);
10029 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
10030 * it's less than ts_recent, drop it.
10032 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
10033 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
10034 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
10037 INP_WLOCK_ASSERT(tp->t_inpcb);
10038 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
10042 * If new data are received on a connection after the user processes
10043 * are gone, then we may RST the other end depending on the outcome
10044 * of bbr_check_data_after_close.
10046 if ((so->so_state & SS_NOFDREF) &&
10049 * We call a new function now so we might continue and setup
10050 * to reset at all data being ack'd.
10052 if (bbr_check_data_after_close(m, bbr, tp, &tlen, th, so))
10055 INP_WLOCK_ASSERT(tp->t_inpcb);
10057 * If last ACK falls within this segment's sequence numbers, record
10058 * its timestamp. NOTE: 1) That the test incorporates suggestions
10059 * from the latest proposal of the tcplw@cray.com list (Braden
10060 * 1993/04/26). 2) That updating only on newer timestamps interferes
10061 * with our earlier PAWS tests, so this check should be solely
10062 * predicated on the sequence space of this segment. 3) That we
10063 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
10064 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
10065 * SEG.Len, This modified check allows us to overcome RFC1323's
10066 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
10067 * p.869. In such cases, we can still calculate the RTT correctly
10068 * when RCV.NXT == Last.ACK.Sent.
10070 INP_WLOCK_ASSERT(tp->t_inpcb);
10071 if ((to->to_flags & TOF_TS) != 0 &&
10072 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
10073 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
10074 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
10075 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
10076 tp->ts_recent = to->to_tsval;
10079 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
10080 * is on (half-synchronized state), then queue data for later
10081 * processing; else drop segment and return.
10083 if ((thflags & TH_ACK) == 0) {
10084 if (tp->t_flags & TF_NEEDSYN) {
10085 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
10086 tiwin, thflags, nxt_pkt));
10087 } else if (tp->t_flags & TF_ACKNOW) {
10088 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
10089 bbr->r_wanted_output = 1;
10092 ctf_do_drop(m, NULL);
10099 INP_WLOCK_ASSERT(tp->t_inpcb);
10100 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
10103 if (sbavail(&so->so_snd)) {
10104 if (bbr_progress_timeout_check(bbr)) {
10105 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
10109 INP_WLOCK_ASSERT(tp->t_inpcb);
10110 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
10111 tiwin, thflags, nxt_pkt));
10115 bbr_stop_all_timers(struct tcpcb *tp)
10117 struct tcp_bbr *bbr;
10120 * Assure no timers are running.
10122 if (tcp_timer_active(tp, TT_PERSIST)) {
10123 /* We enter in persists, set the flag appropriately */
10124 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
10125 bbr->rc_in_persist = 1;
10127 tcp_timer_suspend(tp, TT_PERSIST);
10128 tcp_timer_suspend(tp, TT_REXMT);
10129 tcp_timer_suspend(tp, TT_KEEP);
10130 tcp_timer_suspend(tp, TT_DELACK);
10134 bbr_google_mode_on(struct tcp_bbr *bbr)
10136 bbr->rc_use_google = 1;
10137 bbr->rc_no_pacing = 0;
10138 bbr->r_ctl.bbr_google_discount = bbr_google_discount;
10139 bbr->r_use_policer = bbr_policer_detection_enabled;
10140 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10);
10141 bbr->bbr_use_rack_cheat = 0;
10142 bbr->r_ctl.rc_incr_tmrs = 0;
10143 bbr->r_ctl.rc_inc_tcp_oh = 0;
10144 bbr->r_ctl.rc_inc_ip_oh = 0;
10145 bbr->r_ctl.rc_inc_enet_oh = 0;
10146 reset_time(&bbr->r_ctl.rc_delrate,
10147 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT);
10148 reset_time_small(&bbr->r_ctl.rc_rttprop,
10149 (11 * USECS_IN_SECOND));
10150 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv));
10154 bbr_google_mode_off(struct tcp_bbr *bbr)
10156 bbr->rc_use_google = 0;
10157 bbr->r_ctl.bbr_google_discount = 0;
10158 bbr->no_pacing_until = bbr_no_pacing_until;
10159 bbr->r_use_policer = 0;
10160 if (bbr->no_pacing_until)
10161 bbr->rc_no_pacing = 1;
10163 bbr->rc_no_pacing = 0;
10164 if (bbr_use_rack_resend_cheat)
10165 bbr->bbr_use_rack_cheat = 1;
10167 bbr->bbr_use_rack_cheat = 0;
10168 if (bbr_incr_timers)
10169 bbr->r_ctl.rc_incr_tmrs = 1;
10171 bbr->r_ctl.rc_incr_tmrs = 0;
10172 if (bbr_include_tcp_oh)
10173 bbr->r_ctl.rc_inc_tcp_oh = 1;
10175 bbr->r_ctl.rc_inc_tcp_oh = 0;
10176 if (bbr_include_ip_oh)
10177 bbr->r_ctl.rc_inc_ip_oh = 1;
10179 bbr->r_ctl.rc_inc_ip_oh = 0;
10180 if (bbr_include_enet_oh)
10181 bbr->r_ctl.rc_inc_enet_oh = 1;
10183 bbr->r_ctl.rc_inc_enet_oh = 0;
10184 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit;
10185 reset_time(&bbr->r_ctl.rc_delrate,
10186 bbr_num_pktepo_for_del_limit);
10187 reset_time_small(&bbr->r_ctl.rc_rttprop,
10188 (bbr_filter_len_sec * USECS_IN_SECOND));
10189 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv));
10192 * Return 0 on success, non-zero on failure
10193 * which indicates the error (usually no memory).
10196 bbr_init(struct tcpcb *tp)
10198 struct tcp_bbr *bbr = NULL;
10202 tp->t_fb_ptr = uma_zalloc(bbr_pcb_zone, (M_NOWAIT | M_ZERO));
10203 if (tp->t_fb_ptr == NULL) {
10205 * We need to allocate memory but cant. The INP and INP_INFO
10206 * locks and they are recusive (happens during setup. So a
10207 * scheme to drop the locks fails :(
10212 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
10213 bbr->rtt_valid = 0;
10215 inp->inp_flags2 |= INP_CANNOT_DO_ECN;
10216 inp->inp_flags2 |= INP_SUPPORTS_MBUFQ;
10217 TAILQ_INIT(&bbr->r_ctl.rc_map);
10218 TAILQ_INIT(&bbr->r_ctl.rc_free);
10219 TAILQ_INIT(&bbr->r_ctl.rc_tmap);
10222 bbr->rc_inp = tp->t_inpcb;
10224 cts = tcp_get_usecs(&bbr->rc_tv);
10226 bbr->rc_allow_data_af_clo = bbr_ignore_data_after_close;
10227 bbr->r_ctl.rc_reorder_fade = bbr_reorder_fade;
10228 bbr->rc_tlp_threshold = bbr_tlp_thresh;
10229 bbr->r_ctl.rc_reorder_shift = bbr_reorder_thresh;
10230 bbr->r_ctl.rc_pkt_delay = bbr_pkt_delay;
10231 bbr->r_ctl.rc_min_to = bbr_min_to;
10232 bbr->rc_bbr_state = BBR_STATE_STARTUP;
10233 bbr->r_ctl.bbr_lost_at_state = 0;
10234 bbr->r_ctl.rc_lost_at_startup = 0;
10235 bbr->rc_all_timers_stopped = 0;
10236 bbr->r_ctl.rc_bbr_lastbtlbw = 0;
10237 bbr->r_ctl.rc_pkt_epoch_del = 0;
10238 bbr->r_ctl.rc_pkt_epoch = 0;
10239 bbr->r_ctl.rc_lowest_rtt = 0xffffffff;
10240 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_high_gain;
10241 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain;
10242 bbr->r_ctl.rc_went_idle_time = cts;
10243 bbr->rc_pacer_started = cts;
10244 bbr->r_ctl.rc_pkt_epoch_time = cts;
10245 bbr->r_ctl.rc_rcvtime = cts;
10246 bbr->r_ctl.rc_bbr_state_time = cts;
10247 bbr->r_ctl.rc_del_time = cts;
10248 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
10249 bbr->r_ctl.last_in_probertt = cts;
10250 bbr->skip_gain = 0;
10251 bbr->gain_is_limited = 0;
10252 bbr->no_pacing_until = bbr_no_pacing_until;
10253 if (bbr->no_pacing_until)
10254 bbr->rc_no_pacing = 1;
10255 if (bbr_use_google_algo) {
10256 bbr->rc_no_pacing = 0;
10257 bbr->rc_use_google = 1;
10258 bbr->r_ctl.bbr_google_discount = bbr_google_discount;
10259 bbr->r_use_policer = bbr_policer_detection_enabled;
10261 bbr->rc_use_google = 0;
10262 bbr->r_ctl.bbr_google_discount = 0;
10263 bbr->r_use_policer = 0;
10265 if (bbr_ts_limiting)
10266 bbr->rc_use_ts_limit = 1;
10268 bbr->rc_use_ts_limit = 0;
10269 if (bbr_ts_can_raise)
10270 bbr->ts_can_raise = 1;
10272 bbr->ts_can_raise = 0;
10273 if (V_tcp_delack_enabled == 1)
10274 tp->t_delayed_ack = 2;
10275 else if (V_tcp_delack_enabled == 0)
10276 tp->t_delayed_ack = 0;
10277 else if (V_tcp_delack_enabled < 100)
10278 tp->t_delayed_ack = V_tcp_delack_enabled;
10280 tp->t_delayed_ack = 2;
10281 if (bbr->rc_use_google == 0)
10282 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit;
10284 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10);
10285 bbr->r_ctl.rc_min_rto_ms = bbr_rto_min_ms;
10286 bbr->rc_max_rto_sec = bbr_rto_max_sec;
10287 bbr->rc_init_win = bbr_def_init_win;
10288 if (tp->t_flags & TF_REQ_TSTMP)
10289 bbr->rc_last_options = TCP_TS_OVERHEAD;
10290 bbr->r_ctl.rc_pace_max_segs = tp->t_maxseg - bbr->rc_last_options;
10291 bbr->r_ctl.rc_high_rwnd = tp->snd_wnd;
10292 bbr->r_init_rtt = 1;
10294 counter_u64_add(bbr_flows_nohdwr_pacing, 1);
10295 if (bbr_allow_hdwr_pacing)
10296 bbr->bbr_hdw_pace_ena = 1;
10298 bbr->bbr_hdw_pace_ena = 0;
10299 if (bbr_sends_full_iwnd)
10300 bbr->bbr_init_win_cheat = 1;
10302 bbr->bbr_init_win_cheat = 0;
10303 bbr->r_ctl.bbr_utter_max = bbr_hptsi_utter_max;
10304 bbr->r_ctl.rc_drain_pg = bbr_drain_gain;
10305 bbr->r_ctl.rc_startup_pg = bbr_high_gain;
10306 bbr->rc_loss_exit = bbr_exit_startup_at_loss;
10307 bbr->r_ctl.bbr_rttprobe_gain_val = bbr_rttprobe_gain;
10308 bbr->r_ctl.bbr_hptsi_per_second = bbr_hptsi_per_second;
10309 bbr->r_ctl.bbr_hptsi_segments_delay_tar = bbr_hptsi_segments_delay_tar;
10310 bbr->r_ctl.bbr_hptsi_segments_max = bbr_hptsi_segments_max;
10311 bbr->r_ctl.bbr_hptsi_segments_floor = bbr_hptsi_segments_floor;
10312 bbr->r_ctl.bbr_hptsi_bytes_min = bbr_hptsi_bytes_min;
10313 bbr->r_ctl.bbr_cross_over = bbr_cross_over;
10314 bbr->r_ctl.rc_rtt_shrinks = cts;
10315 if (bbr->rc_use_google) {
10316 setup_time_filter(&bbr->r_ctl.rc_delrate,
10318 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT);
10319 setup_time_filter_small(&bbr->r_ctl.rc_rttprop,
10320 FILTER_TYPE_MIN, (11 * USECS_IN_SECOND));
10322 setup_time_filter(&bbr->r_ctl.rc_delrate,
10324 bbr_num_pktepo_for_del_limit);
10325 setup_time_filter_small(&bbr->r_ctl.rc_rttprop,
10326 FILTER_TYPE_MIN, (bbr_filter_len_sec * USECS_IN_SECOND));
10328 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_INIT, 0);
10329 if (bbr_uses_idle_restart)
10330 bbr->rc_use_idle_restart = 1;
10332 bbr->rc_use_idle_restart = 0;
10333 bbr->r_ctl.rc_bbr_cur_del_rate = 0;
10334 bbr->r_ctl.rc_initial_hptsi_bw = bbr_initial_bw_bps;
10335 if (bbr_resends_use_tso)
10336 bbr->rc_resends_use_tso = 1;
10337 #ifdef NETFLIX_PEAKRATE
10338 tp->t_peakrate_thr = tp->t_maxpeakrate;
10340 if (tp->snd_una != tp->snd_max) {
10341 /* Create a send map for the current outstanding data */
10342 struct bbr_sendmap *rsm;
10344 rsm = bbr_alloc(bbr);
10346 uma_zfree(bbr_pcb_zone, tp->t_fb_ptr);
10347 tp->t_fb_ptr = NULL;
10350 rsm->r_flags = BBR_OVERMAX;
10351 rsm->r_tim_lastsent[0] = cts;
10352 rsm->r_rtr_cnt = 1;
10353 rsm->r_rtr_bytes = 0;
10354 rsm->r_start = tp->snd_una;
10355 rsm->r_end = tp->snd_max;
10357 rsm->r_delivered = bbr->r_ctl.rc_delivered;
10358 rsm->r_ts_valid = 0;
10359 rsm->r_del_ack_ts = tp->ts_recent;
10360 rsm->r_del_time = cts;
10361 if (bbr->r_ctl.r_app_limited_until)
10362 rsm->r_app_limited = 1;
10364 rsm->r_app_limited = 0;
10365 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next);
10366 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
10367 rsm->r_in_tmap = 1;
10368 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW)
10369 rsm->r_bbr_state = bbr_state_val(bbr);
10371 rsm->r_bbr_state = 8;
10373 if (bbr_use_rack_resend_cheat && (bbr->rc_use_google == 0))
10374 bbr->bbr_use_rack_cheat = 1;
10375 if (bbr_incr_timers && (bbr->rc_use_google == 0))
10376 bbr->r_ctl.rc_incr_tmrs = 1;
10377 if (bbr_include_tcp_oh && (bbr->rc_use_google == 0))
10378 bbr->r_ctl.rc_inc_tcp_oh = 1;
10379 if (bbr_include_ip_oh && (bbr->rc_use_google == 0))
10380 bbr->r_ctl.rc_inc_ip_oh = 1;
10381 if (bbr_include_enet_oh && (bbr->rc_use_google == 0))
10382 bbr->r_ctl.rc_inc_enet_oh = 1;
10384 bbr_log_type_statechange(bbr, cts, __LINE__);
10385 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
10389 rtt = (TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT);
10390 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
10392 /* announce the settings and state */
10393 bbr_log_settings_change(bbr, BBR_RECOVERY_LOWRTT);
10394 tcp_bbr_tso_size_check(bbr, cts);
10396 * Now call the generic function to start a timer. This will place
10397 * the TCB on the hptsi wheel if a timer is needed with appropriate
10400 bbr_stop_all_timers(tp);
10401 bbr_start_hpts_timer(bbr, tp, cts, 5, 0, 0);
10406 * Return 0 if we can accept the connection. Return
10407 * non-zero if we can't handle the connection. A EAGAIN
10408 * means you need to wait until the connection is up.
10409 * a EADDRNOTAVAIL means we can never handle the connection
10413 bbr_handoff_ok(struct tcpcb *tp)
10415 if ((tp->t_state == TCPS_CLOSED) ||
10416 (tp->t_state == TCPS_LISTEN)) {
10417 /* Sure no problem though it may not stick */
10420 if ((tp->t_state == TCPS_SYN_SENT) ||
10421 (tp->t_state == TCPS_SYN_RECEIVED)) {
10423 * We really don't know you have to get to ESTAB or beyond
10428 if ((tp->t_flags & TF_SACK_PERMIT) || bbr_sack_not_required) {
10432 * If we reach here we don't do SACK on this connection so we can
10439 bbr_fini(struct tcpcb *tp, int32_t tcb_is_purged)
10441 if (tp->t_fb_ptr) {
10443 struct tcp_bbr *bbr;
10444 struct bbr_sendmap *rsm;
10446 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
10447 if (bbr->r_ctl.crte)
10448 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp);
10449 bbr_log_flowend(bbr);
10452 /* Backout any flags2 we applied */
10453 tp->t_inpcb->inp_flags2 &= ~INP_CANNOT_DO_ECN;
10454 tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
10455 tp->t_inpcb->inp_flags2 &= ~INP_MBUF_QUEUE_READY;
10457 if (bbr->bbr_hdrw_pacing)
10458 counter_u64_add(bbr_flows_whdwr_pacing, -1);
10460 counter_u64_add(bbr_flows_nohdwr_pacing, -1);
10461 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
10463 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next);
10464 uma_zfree(bbr_zone, rsm);
10465 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
10467 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free);
10469 TAILQ_REMOVE(&bbr->r_ctl.rc_free, rsm, r_next);
10470 uma_zfree(bbr_zone, rsm);
10471 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free);
10473 calc = bbr->r_ctl.rc_high_rwnd - bbr->r_ctl.rc_init_rwnd;
10474 if (calc > (bbr->r_ctl.rc_init_rwnd / 10))
10475 BBR_STAT_INC(bbr_dynamic_rwnd);
10477 BBR_STAT_INC(bbr_static_rwnd);
10478 bbr->r_ctl.rc_free_cnt = 0;
10479 uma_zfree(bbr_pcb_zone, tp->t_fb_ptr);
10480 tp->t_fb_ptr = NULL;
10482 /* Make sure snd_nxt is correctly set */
10483 tp->snd_nxt = tp->snd_max;
10487 bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win)
10489 switch (tp->t_state) {
10490 case TCPS_SYN_SENT:
10491 bbr->r_state = TCPS_SYN_SENT;
10492 bbr->r_substate = bbr_do_syn_sent;
10494 case TCPS_SYN_RECEIVED:
10495 bbr->r_state = TCPS_SYN_RECEIVED;
10496 bbr->r_substate = bbr_do_syn_recv;
10498 case TCPS_ESTABLISHED:
10499 bbr->r_ctl.rc_init_rwnd = max(win, bbr->rc_tp->snd_wnd);
10500 bbr->r_state = TCPS_ESTABLISHED;
10501 bbr->r_substate = bbr_do_established;
10503 case TCPS_CLOSE_WAIT:
10504 bbr->r_state = TCPS_CLOSE_WAIT;
10505 bbr->r_substate = bbr_do_close_wait;
10507 case TCPS_FIN_WAIT_1:
10508 bbr->r_state = TCPS_FIN_WAIT_1;
10509 bbr->r_substate = bbr_do_fin_wait_1;
10512 bbr->r_state = TCPS_CLOSING;
10513 bbr->r_substate = bbr_do_closing;
10515 case TCPS_LAST_ACK:
10516 bbr->r_state = TCPS_LAST_ACK;
10517 bbr->r_substate = bbr_do_lastack;
10519 case TCPS_FIN_WAIT_2:
10520 bbr->r_state = TCPS_FIN_WAIT_2;
10521 bbr->r_substate = bbr_do_fin_wait_2;
10525 case TCPS_TIME_WAIT:
10532 bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int32_t line, int dolog)
10535 * Now what state are we going into now? Is there adjustments
10538 int32_t old_state, old_gain;
10541 old_state = bbr_state_val(bbr);
10542 old_gain = bbr->r_ctl.rc_bbr_hptsi_gain;
10543 if (bbr_state_val(bbr) == BBR_SUB_LEVEL1) {
10544 /* Save the lowest srtt we saw in our end of the sub-state */
10545 bbr->rc_hit_state_1 = 0;
10546 if (bbr->r_ctl.bbr_smallest_srtt_this_state != 0xffffffff)
10547 bbr->r_ctl.bbr_smallest_srtt_state2 = bbr->r_ctl.bbr_smallest_srtt_this_state;
10549 bbr->rc_bbr_substate++;
10550 if (bbr->rc_bbr_substate >= BBR_SUBSTATE_COUNT) {
10551 /* Cycle back to first state-> gain */
10552 bbr->rc_bbr_substate = 0;
10554 if (bbr_state_val(bbr) == BBR_SUB_GAIN) {
10556 * We enter the gain(5/4) cycle (possibly less if
10557 * shallow buffer detection is enabled)
10559 if (bbr->skip_gain) {
10561 * Hardware pacing has set our rate to
10562 * the max and limited our b/w just
10563 * do level i.e. no gain.
10565 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_LEVEL1];
10566 } else if (bbr->gain_is_limited &&
10567 bbr->bbr_hdrw_pacing &&
10570 * We can't gain above the hardware pacing
10571 * rate which is less than our rate + the gain
10572 * calculate the gain needed to reach the hardware
10575 uint64_t bw, rate, gain_calc;
10577 bw = bbr_get_bw(bbr);
10578 rate = bbr->r_ctl.crte->rate;
10580 (((bw * (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN]) / (uint64_t)BBR_UNIT) > rate)) {
10581 gain_calc = (rate * BBR_UNIT) / bw;
10582 if (gain_calc < BBR_UNIT)
10583 gain_calc = BBR_UNIT;
10584 bbr->r_ctl.rc_bbr_hptsi_gain = (uint16_t)gain_calc;
10586 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN];
10589 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN];
10590 if ((bbr->rc_use_google == 0) && (bbr_gain_to_target == 0)) {
10591 bbr->r_ctl.rc_bbr_state_atflight = cts;
10593 bbr->r_ctl.rc_bbr_state_atflight = 0;
10594 } else if (bbr_state_val(bbr) == BBR_SUB_DRAIN) {
10595 bbr->rc_hit_state_1 = 1;
10596 bbr->r_ctl.rc_exta_time_gd = 0;
10597 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp,
10598 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
10599 if (bbr_state_drain_2_tar) {
10600 bbr->r_ctl.rc_bbr_state_atflight = 0;
10602 bbr->r_ctl.rc_bbr_state_atflight = cts;
10603 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_DRAIN];
10605 /* All other cycles hit here 2-7 */
10606 if ((old_state == BBR_SUB_DRAIN) && bbr->rc_hit_state_1) {
10607 if (bbr_sub_drain_slam_cwnd &&
10608 (bbr->rc_use_google == 0) &&
10609 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) {
10610 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
10611 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10613 if ((cts - bbr->r_ctl.rc_bbr_state_time) > bbr_get_rtt(bbr, BBR_RTT_PROP))
10614 bbr->r_ctl.rc_exta_time_gd += ((cts - bbr->r_ctl.rc_bbr_state_time) -
10615 bbr_get_rtt(bbr, BBR_RTT_PROP));
10617 bbr->r_ctl.rc_exta_time_gd = 0;
10618 if (bbr->r_ctl.rc_exta_time_gd) {
10619 bbr->r_ctl.rc_level_state_extra = bbr->r_ctl.rc_exta_time_gd;
10620 /* Now chop up the time for each state (div by 7) */
10621 bbr->r_ctl.rc_level_state_extra /= 7;
10622 if (bbr_rand_ot && bbr->r_ctl.rc_level_state_extra) {
10623 /* Add a randomization */
10624 bbr_randomize_extra_state_time(bbr);
10628 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts);
10629 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[bbr_state_val(bbr)];
10631 if (bbr->rc_use_google) {
10632 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts);
10634 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
10635 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain;
10637 bbr_log_type_statechange(bbr, cts, line);
10639 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
10642 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
10643 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) {
10644 counter_u64_add(bbr_state_time[(old_state + 5)], time_in);
10646 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
10649 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff;
10650 bbr_set_state_target(bbr, __LINE__);
10651 if (bbr_sub_drain_slam_cwnd &&
10652 (bbr->rc_use_google == 0) &&
10653 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) {
10654 /* Slam down the cwnd */
10655 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
10656 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
10657 if (bbr_sub_drain_app_limit) {
10658 /* Go app limited if we are on a long drain */
10659 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered +
10660 ctf_flight_size(bbr->rc_tp,
10661 (bbr->r_ctl.rc_sacked +
10662 bbr->r_ctl.rc_lost_bytes)));
10664 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10666 if (bbr->rc_lt_use_bw) {
10667 /* In policed mode we clamp pacing_gain to BBR_UNIT */
10668 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
10670 /* Google changes TSO size every cycle */
10671 if (bbr->rc_use_google)
10672 tcp_bbr_tso_size_check(bbr, cts);
10673 bbr->r_ctl.gain_epoch = cts;
10674 bbr->r_ctl.rc_bbr_state_time = cts;
10675 bbr->r_ctl.substate_pe = bbr->r_ctl.rc_pkt_epoch;
10679 bbr_set_probebw_google_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses)
10681 if ((bbr_state_val(bbr) == BBR_SUB_DRAIN) &&
10682 (google_allow_early_out == 1) &&
10683 (bbr->r_ctl.rc_flight_at_input <= bbr->r_ctl.rc_target_at_state)) {
10684 /* We have reached out target flight size possibly early */
10687 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time)) {
10690 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_get_rtt(bbr, BBR_RTT_PROP)) {
10692 * Must be a rttProp movement forward before
10693 * we can change states.
10697 if (bbr_state_val(bbr) == BBR_SUB_GAIN) {
10699 * The needed time has passed but for
10700 * the gain cycle extra rules apply:
10701 * 1) If we have seen loss, we exit
10702 * 2) If we have not reached the target
10703 * we stay in GAIN (gain-to-target).
10705 if (google_consider_lost && losses)
10707 if (bbr->r_ctl.rc_target_at_state > bbr->r_ctl.rc_flight_at_input) {
10712 /* For gain we must reach our target, all others last 1 rttProp */
10713 bbr_substate_change(bbr, cts, __LINE__, 1);
10717 bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses)
10719 uint32_t flight, bbr_cur_cycle_time;
10721 if (bbr->rc_use_google) {
10722 bbr_set_probebw_google_gains(bbr, cts, losses);
10727 * Never alow cts to be 0 we
10728 * do this so we can judge if
10729 * we have set a timestamp.
10733 if (bbr_state_is_pkt_epoch)
10734 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PKTRTT);
10736 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PROP);
10738 if (bbr->r_ctl.rc_bbr_state_atflight == 0) {
10739 if (bbr_state_val(bbr) == BBR_SUB_DRAIN) {
10740 flight = ctf_flight_size(bbr->rc_tp,
10741 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
10742 if (bbr_sub_drain_slam_cwnd && bbr->rc_hit_state_1) {
10743 /* Keep it slam down */
10744 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state) {
10745 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
10746 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10748 if (bbr_sub_drain_app_limit) {
10749 /* Go app limited if we are on a long drain */
10750 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered + flight);
10753 if (TSTMP_GT(cts, bbr->r_ctl.gain_epoch) &&
10754 (((cts - bbr->r_ctl.gain_epoch) > bbr_get_rtt(bbr, BBR_RTT_PROP)) ||
10755 (flight >= bbr->r_ctl.flightsize_at_drain))) {
10757 * Still here after the same time as
10758 * the gain. We need to drain harder
10759 * for the next srtt. Reduce by a set amount
10760 * the gain drop is capped at DRAIN states
10763 bbr->r_ctl.flightsize_at_drain = flight;
10764 if (bbr_drain_drop_mul &&
10765 bbr_drain_drop_div &&
10766 (bbr_drain_drop_mul < bbr_drain_drop_div)) {
10767 /* Use your specific drop value (def 4/5 = 20%) */
10768 bbr->r_ctl.rc_bbr_hptsi_gain *= bbr_drain_drop_mul;
10769 bbr->r_ctl.rc_bbr_hptsi_gain /= bbr_drain_drop_div;
10771 /* You get drop of 20% */
10772 bbr->r_ctl.rc_bbr_hptsi_gain *= 4;
10773 bbr->r_ctl.rc_bbr_hptsi_gain /= 5;
10775 if (bbr->r_ctl.rc_bbr_hptsi_gain <= bbr_drain_floor) {
10776 /* Reduce our gain again to the bottom */
10777 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1);
10779 bbr_log_exit_gain(bbr, cts, 4);
10781 * Extend out so we wait another
10782 * epoch before dropping again.
10784 bbr->r_ctl.gain_epoch = cts;
10786 if (flight <= bbr->r_ctl.rc_target_at_state) {
10787 if (bbr_sub_drain_slam_cwnd &&
10788 (bbr->rc_use_google == 0) &&
10789 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) {
10790 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
10791 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10793 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1);
10794 bbr_log_exit_gain(bbr, cts, 3);
10798 if (bbr->r_ctl.rc_lost > bbr->r_ctl.bbr_lost_at_state) {
10799 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1);
10802 if ((ctf_outstanding(bbr->rc_tp) >= bbr->r_ctl.rc_target_at_state) ||
10803 ((ctf_outstanding(bbr->rc_tp) + bbr->rc_tp->t_maxseg - 1) >=
10804 bbr->rc_tp->snd_wnd)) {
10805 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1);
10806 bbr_log_exit_gain(bbr, cts, 2);
10810 * We fall through and return always one of two things has
10812 * 1) We are still not at target
10814 * 2) We reached the target and set rc_bbr_state_atflight
10815 * which means we no longer hit this block
10816 * next time we are called.
10821 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time))
10823 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_cur_cycle_time) {
10824 /* Less than a full time-period has passed */
10827 if (bbr->r_ctl.rc_level_state_extra &&
10828 (bbr_state_val(bbr) > BBR_SUB_DRAIN) &&
10829 ((cts - bbr->r_ctl.rc_bbr_state_time) <
10830 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) {
10831 /* Less than a full time-period + extra has passed */
10834 if (bbr_gain_gets_extra_too &&
10835 bbr->r_ctl.rc_level_state_extra &&
10836 (bbr_state_val(bbr) == BBR_SUB_GAIN) &&
10837 ((cts - bbr->r_ctl.rc_bbr_state_time) <
10838 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) {
10839 /* Less than a full time-period + extra has passed */
10842 bbr_substate_change(bbr, cts, __LINE__, 1);
10846 bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain)
10850 if (bbr->rc_use_google) {
10851 /* Google just uses the cwnd target */
10852 tar = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), gain);
10854 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options),
10855 bbr->r_ctl.rc_pace_max_segs);
10856 /* Get the base cwnd with gain rounded to a mss */
10857 tar = roundup(bbr_get_raw_target_cwnd(bbr, bbr_get_bw(bbr),
10859 /* Make sure it is within our min */
10860 if (tar < get_min_cwnd(bbr))
10861 return (get_min_cwnd(bbr));
10867 bbr_set_state_target(struct tcp_bbr *bbr, int line)
10869 uint32_t tar, meth;
10871 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) &&
10872 ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google)) {
10873 /* Special case using old probe-rtt method */
10874 tar = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options);
10877 /* Non-probe-rtt case and reduced probe-rtt */
10878 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) &&
10879 (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT)) {
10880 /* For gain cycle we use the hptsi gain */
10881 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain);
10883 } else if ((bbr_target_is_bbunit) || bbr->rc_use_google) {
10885 * If configured, or for google all other states
10888 tar = bbr_get_a_state_target(bbr, BBR_UNIT);
10892 * Or we set a target based on the pacing gain
10893 * for non-google mode and default (non-configured).
10894 * Note we don't set a target goal below drain (192).
10896 if (bbr->r_ctl.rc_bbr_hptsi_gain < bbr_hptsi_gain[BBR_SUB_DRAIN]) {
10897 tar = bbr_get_a_state_target(bbr, bbr_hptsi_gain[BBR_SUB_DRAIN]);
10900 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain);
10905 bbr_log_set_of_state_target(bbr, tar, line, meth);
10906 bbr->r_ctl.rc_target_at_state = tar;
10910 bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line)
10912 /* Change to probe_rtt */
10915 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
10916 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp,
10917 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
10918 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.flightsize_at_drain
10919 + bbr->r_ctl.rc_delivered);
10920 /* Setup so we force feed the filter */
10921 if (bbr->rc_use_google || bbr_probertt_sets_rtt)
10922 bbr->rc_prtt_set_ts = 1;
10923 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
10924 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
10925 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
10927 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_ENTERPROBE, 0);
10928 bbr->r_ctl.rc_rtt_shrinks = cts;
10929 bbr->r_ctl.last_in_probertt = cts;
10930 bbr->r_ctl.rc_probertt_srttchktim = cts;
10931 bbr->r_ctl.rc_bbr_state_time = cts;
10932 bbr->rc_bbr_state = BBR_STATE_PROBE_RTT;
10933 /* We need to force the filter to update */
10935 if ((bbr_sub_drain_slam_cwnd) &&
10936 bbr->rc_hit_state_1 &&
10937 (bbr->rc_use_google == 0) &&
10938 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) {
10939 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_saved_cwnd)
10940 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
10942 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
10943 /* Update the lost */
10944 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
10945 if ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google){
10946 /* Set to the non-configurable default of 4 (PROBE_RTT_MIN) */
10947 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options);
10948 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10949 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
10950 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
10951 bbr_log_set_of_state_target(bbr, bbr->rc_tp->snd_cwnd, __LINE__, 6);
10952 bbr->r_ctl.rc_target_at_state = bbr->rc_tp->snd_cwnd;
10955 * We bring it down slowly by using a hptsi gain that is
10956 * probably 75%. This will slowly float down our outstanding
10957 * without tampering with the cwnd.
10959 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val;
10960 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
10961 bbr_set_state_target(bbr, __LINE__);
10962 if (bbr_prtt_slam_cwnd &&
10963 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
10964 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
10965 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10968 if (ctf_flight_size(bbr->rc_tp,
10969 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <=
10970 bbr->r_ctl.rc_target_at_state) {
10971 /* We are at target */
10972 bbr->r_ctl.rc_bbr_enters_probertt = cts;
10974 /* We need to come down to reach target before our time begins */
10975 bbr->r_ctl.rc_bbr_enters_probertt = 0;
10977 bbr->r_ctl.rc_pe_of_prtt = bbr->r_ctl.rc_pkt_epoch;
10978 BBR_STAT_INC(bbr_enter_probertt);
10979 bbr_log_exit_gain(bbr, cts, 0);
10980 bbr_log_type_statechange(bbr, cts, line);
10984 bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts)
10987 * Sanity check on probe-rtt intervals.
10988 * In crazy situations where we are competing
10989 * against new-reno flows with huge buffers
10990 * our rtt-prop interval could come to dominate
10991 * things if we can't get through a full set
10992 * of cycles, we need to adjust it.
10994 if (bbr_can_adjust_probertt &&
10995 (bbr->rc_use_google == 0)) {
10997 uint32_t cur_rttp, fval, newval, baseval;
10999 /* Are we to small and go into probe-rtt to often? */
11000 baseval = (bbr_get_rtt(bbr, BBR_RTT_PROP) * (BBR_SUBSTATE_COUNT + 1));
11001 cur_rttp = roundup(baseval, USECS_IN_SECOND);
11002 fval = bbr_filter_len_sec * USECS_IN_SECOND;
11003 if (bbr_is_ratio == 0) {
11004 if (fval > bbr_rtt_probe_limit)
11005 newval = cur_rttp + (fval - bbr_rtt_probe_limit);
11011 mul = fval / bbr_rtt_probe_limit;
11012 newval = cur_rttp * mul;
11014 if (cur_rttp > bbr->r_ctl.rc_probertt_int) {
11015 bbr->r_ctl.rc_probertt_int = cur_rttp;
11016 reset_time_small(&bbr->r_ctl.rc_rttprop, newval);
11020 * No adjustments were made
11021 * do we need to shrink it?
11023 if (bbr->r_ctl.rc_probertt_int > bbr_rtt_probe_limit) {
11024 if (cur_rttp <= bbr_rtt_probe_limit) {
11026 * Things have calmed down lets
11027 * shrink all the way to default
11029 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit;
11030 reset_time_small(&bbr->r_ctl.rc_rttprop,
11031 (bbr_filter_len_sec * USECS_IN_SECOND));
11032 cur_rttp = bbr_rtt_probe_limit;
11033 newval = (bbr_filter_len_sec * USECS_IN_SECOND);
11037 * Well does some adjustment make sense?
11039 if (cur_rttp < bbr->r_ctl.rc_probertt_int) {
11040 /* We can reduce interval time some */
11041 bbr->r_ctl.rc_probertt_int = cur_rttp;
11042 reset_time_small(&bbr->r_ctl.rc_rttprop, newval);
11049 bbr_log_rtt_shrinks(bbr, cts, cur_rttp, newval, __LINE__, BBR_RTTS_RESETS_VALUES, val);
11054 bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
11056 /* Exit probe-rtt */
11058 if (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd) {
11059 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
11060 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11062 bbr_log_exit_gain(bbr, cts, 1);
11063 bbr->rc_hit_state_1 = 0;
11064 bbr->r_ctl.rc_rtt_shrinks = cts;
11065 bbr->r_ctl.last_in_probertt = cts;
11066 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_RTTPROBE, 0);
11067 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
11068 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp,
11069 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) +
11070 bbr->r_ctl.rc_delivered);
11071 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
11074 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
11075 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
11077 if (bbr->rc_filled_pipe) {
11078 /* Switch to probe_bw */
11079 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
11080 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts);
11081 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain;
11082 bbr_substate_change(bbr, cts, __LINE__, 0);
11083 bbr_log_type_statechange(bbr, cts, __LINE__);
11085 /* Back to startup */
11086 bbr->rc_bbr_state = BBR_STATE_STARTUP;
11087 bbr->r_ctl.rc_bbr_state_time = cts;
11089 * We don't want to give a complete free 3
11090 * measurements until we exit, so we use
11091 * the number of pe's we were in probe-rtt
11092 * to add to the startup_epoch. That way
11093 * we will still retain the old state.
11095 bbr->r_ctl.rc_bbr_last_startup_epoch += (bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_pe_of_prtt);
11096 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11097 /* Make sure to use the lower pg when shifting back in */
11098 if (bbr->r_ctl.rc_lost &&
11099 bbr_use_lower_gain_in_startup &&
11100 (bbr->rc_use_google == 0))
11101 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower;
11103 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg;
11104 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg;
11105 /* Probably not needed but set it anyway */
11106 bbr_set_state_target(bbr, __LINE__);
11107 bbr_log_type_statechange(bbr, cts, __LINE__);
11108 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11109 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 0);
11111 bbr_check_probe_rtt_limits(bbr, cts);
11114 static int32_t inline
11115 bbr_should_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts)
11117 if ((bbr->rc_past_init_win == 1) &&
11118 (bbr->rc_in_persist == 0) &&
11119 (bbr_calc_time(cts, bbr->r_ctl.rc_rtt_shrinks) >= bbr->r_ctl.rc_probertt_int)) {
11122 if (bbr_can_force_probertt &&
11123 (bbr->rc_in_persist == 0) &&
11124 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) &&
11125 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) {
11133 bbr_google_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t pkt_epoch)
11135 uint64_t btlbw, gain;
11136 if (pkt_epoch == 0) {
11138 * Need to be on a pkt-epoch to continue.
11142 btlbw = bbr_get_full_bw(bbr);
11143 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw *
11144 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw;
11145 if (btlbw >= gain) {
11146 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch;
11147 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11148 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 3);
11149 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw;
11151 if ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS)
11153 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11154 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8);
11158 static int32_t inline
11159 bbr_state_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch)
11161 /* Have we gained 25% in the last 3 packet based epoch's? */
11162 uint64_t btlbw, gain;
11164 int delta, rtt_gain;
11166 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) &&
11167 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) {
11169 * This qualifies as a RTT_PROBE session since we drop the
11170 * data outstanding to nothing and waited more than
11171 * bbr_rtt_probe_time.
11173 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0);
11174 bbr_set_reduced_rtt(bbr, cts, __LINE__);
11176 if (bbr_should_enter_probe_rtt(bbr, cts)) {
11177 bbr_enter_probe_rtt(bbr, cts, __LINE__);
11180 if (bbr->rc_use_google)
11181 return (bbr_google_startup(bbr, cts, pkt_epoch));
11183 if ((bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) &&
11184 (bbr_use_lower_gain_in_startup)) {
11185 /* Drop to a lower gain 1.5 x since we saw loss */
11186 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower;
11188 if (pkt_epoch == 0) {
11190 * Need to be on a pkt-epoch to continue.
11194 if (bbr_rtt_gain_thresh) {
11196 * Do we allow a flow to stay
11197 * in startup with no loss and no
11198 * gain in rtt over a set threshold?
11200 if (bbr->r_ctl.rc_pkt_epoch_rtt &&
11201 bbr->r_ctl.startup_last_srtt &&
11202 (bbr->r_ctl.rc_pkt_epoch_rtt > bbr->r_ctl.startup_last_srtt)) {
11203 delta = bbr->r_ctl.rc_pkt_epoch_rtt - bbr->r_ctl.startup_last_srtt;
11204 rtt_gain = (delta * 100) / bbr->r_ctl.startup_last_srtt;
11207 if ((bbr->r_ctl.startup_last_srtt == 0) ||
11208 (bbr->r_ctl.rc_pkt_epoch_rtt < bbr->r_ctl.startup_last_srtt))
11209 /* First time or new lower value */
11210 bbr->r_ctl.startup_last_srtt = bbr->r_ctl.rc_pkt_epoch_rtt;
11212 if ((bbr->r_ctl.rc_lost == 0) &&
11213 (rtt_gain < bbr_rtt_gain_thresh)) {
11215 * No loss, and we are under
11216 * our gain threhold for
11219 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch)
11220 bbr->r_ctl.rc_bbr_last_startup_epoch++;
11221 bbr_log_startup_event(bbr, cts, rtt_gain,
11222 delta, bbr->r_ctl.startup_last_srtt, 10);
11226 if ((bbr->r_ctl.r_measurement_count == bbr->r_ctl.last_startup_measure) &&
11227 (bbr->r_ctl.rc_lost_at_startup == bbr->r_ctl.rc_lost) &&
11228 (!IN_RECOVERY(bbr->rc_tp->t_flags))) {
11230 * We only assess if we have a new measurment when
11231 * we have no loss and are not in recovery.
11232 * Drag up by one our last_startup epoch so we will hold
11233 * the number of non-gain we have already accumulated.
11235 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch)
11236 bbr->r_ctl.rc_bbr_last_startup_epoch++;
11237 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11238 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 9);
11241 /* Case where we reduced the lost (bad retransmit) */
11242 if (bbr->r_ctl.rc_lost_at_startup > bbr->r_ctl.rc_lost)
11243 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11244 bbr->r_ctl.last_startup_measure = bbr->r_ctl.r_measurement_count;
11245 btlbw = bbr_get_full_bw(bbr);
11246 if (bbr->r_ctl.rc_bbr_hptsi_gain == bbr_startup_lower)
11247 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw *
11248 (uint64_t)bbr_low_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw;
11250 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw *
11251 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw;
11253 if (btlbw > bbr->r_ctl.rc_bbr_lastbtlbw)
11254 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw;
11255 if (btlbw >= gain) {
11256 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch;
11257 /* Update the lost so we won't exit in next set of tests */
11258 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11259 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11260 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 3);
11262 if ((bbr->rc_loss_exit &&
11263 (bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) &&
11264 (bbr->r_ctl.rc_pkt_epoch_loss_rate > bbr_startup_loss_thresh)) &&
11265 ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS)) {
11267 * If we had no gain, we had loss and that loss was above
11268 * our threshould, the rwnd is not constrained, and we have
11269 * had at least 3 packet epochs exit. Note that this is
11270 * switched off by sysctl. Google does not do this by the
11273 if ((ctf_flight_size(bbr->rc_tp,
11274 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) +
11275 (2 * max(bbr->r_ctl.rc_pace_max_segs, bbr->rc_tp->t_maxseg))) <= bbr->rc_tp->snd_wnd) {
11277 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11278 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 4);
11280 /* Just record an updated loss value */
11281 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11282 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11283 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 5);
11286 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11287 if (((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS) ||
11289 /* Return 1 to exit the startup state. */
11292 /* Stay in startup */
11293 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11294 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8);
11299 bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch, uint32_t losses)
11302 * A tick occured in the rtt epoch do we need to do anything?
11304 #ifdef BBR_INVARIANTS
11305 if ((bbr->rc_bbr_state != BBR_STATE_STARTUP) &&
11306 (bbr->rc_bbr_state != BBR_STATE_DRAIN) &&
11307 (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) &&
11308 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) &&
11309 (bbr->rc_bbr_state != BBR_STATE_PROBE_BW)) {
11311 panic("Unknown BBR state %d?\n", bbr->rc_bbr_state);
11314 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) {
11315 /* Do we exit the startup state? */
11316 if (bbr_state_startup(bbr, cts, epoch, pkt_epoch)) {
11319 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11320 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 6);
11321 bbr->rc_filled_pipe = 1;
11322 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
11323 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
11325 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
11326 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
11329 if (bbr->rc_no_pacing)
11330 bbr->rc_no_pacing = 0;
11331 bbr->r_ctl.rc_bbr_state_time = cts;
11332 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_drain_pg;
11333 bbr->rc_bbr_state = BBR_STATE_DRAIN;
11334 bbr_set_state_target(bbr, __LINE__);
11335 if ((bbr->rc_use_google == 0) &&
11336 bbr_slam_cwnd_in_main_drain) {
11337 /* Here we don't have to worry about probe-rtt */
11338 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
11339 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
11340 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11342 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain;
11343 bbr_log_type_statechange(bbr, cts, __LINE__);
11344 if (ctf_flight_size(bbr->rc_tp,
11345 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <=
11346 bbr->r_ctl.rc_target_at_state) {
11348 * Switch to probe_bw if we are already
11351 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts);
11352 bbr_substate_change(bbr, cts, __LINE__, 0);
11353 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
11354 bbr_log_type_statechange(bbr, cts, __LINE__);
11357 } else if (bbr->rc_bbr_state == BBR_STATE_IDLE_EXIT) {
11362 inflight = ctf_flight_size(tp,
11363 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11364 if (inflight >= bbr->r_ctl.rc_target_at_state) {
11365 /* We have reached a flight of the cwnd target */
11366 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
11367 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
11368 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
11369 bbr_set_state_target(bbr, __LINE__);
11371 * Rig it so we don't do anything crazy and
11372 * start fresh with a new randomization.
11374 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff;
11375 bbr->rc_bbr_substate = BBR_SUB_LEVEL6;
11376 bbr_substate_change(bbr, cts, __LINE__, 1);
11378 } else if (bbr->rc_bbr_state == BBR_STATE_DRAIN) {
11379 /* Has in-flight reached the bdp (or less)? */
11384 inflight = ctf_flight_size(tp,
11385 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11386 if ((bbr->rc_use_google == 0) &&
11387 bbr_slam_cwnd_in_main_drain &&
11388 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
11390 * Here we don't have to worry about probe-rtt
11391 * re-slam it, but keep it slammed down.
11393 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
11394 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11396 if (inflight <= bbr->r_ctl.rc_target_at_state) {
11397 /* We have drained */
11398 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
11399 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
11400 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
11403 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
11404 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
11406 if ((bbr->rc_use_google == 0) &&
11407 bbr_slam_cwnd_in_main_drain &&
11408 (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) {
11409 /* Restore the cwnd */
11410 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
11411 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11413 /* Setup probe-rtt has being done now RRS-HERE */
11414 bbr->r_ctl.rc_rtt_shrinks = cts;
11415 bbr->r_ctl.last_in_probertt = cts;
11416 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_LEAVE_DRAIN, 0);
11417 /* Randomly pick a sub-state */
11418 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts);
11419 bbr_substate_change(bbr, cts, __LINE__, 0);
11420 bbr_log_type_statechange(bbr, cts, __LINE__);
11422 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) {
11425 flight = ctf_flight_size(bbr->rc_tp,
11426 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11427 bbr->r_ctl.r_app_limited_until = (flight + bbr->r_ctl.rc_delivered);
11428 if (((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google) &&
11429 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
11431 * We must keep cwnd at the desired MSS.
11433 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options);
11434 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11435 } else if ((bbr_prtt_slam_cwnd) &&
11436 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
11438 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
11439 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11441 if (bbr->r_ctl.rc_bbr_enters_probertt == 0) {
11442 /* Has outstanding reached our target? */
11443 if (flight <= bbr->r_ctl.rc_target_at_state) {
11444 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_REACHTAR, 0);
11445 bbr->r_ctl.rc_bbr_enters_probertt = cts;
11446 /* If time is exactly 0, be 1usec off */
11447 if (bbr->r_ctl.rc_bbr_enters_probertt == 0)
11448 bbr->r_ctl.rc_bbr_enters_probertt = 1;
11449 if (bbr->rc_use_google == 0) {
11451 * Restore any lowering that as occured to
11454 if (bbr->r_ctl.bbr_rttprobe_gain_val)
11455 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val;
11457 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
11460 if ((bbr->r_ctl.rc_bbr_enters_probertt == 0) &&
11461 (bbr->rc_use_google == 0) &&
11462 bbr->r_ctl.bbr_rttprobe_gain_val &&
11463 (((cts - bbr->r_ctl.rc_probertt_srttchktim) > bbr_get_rtt(bbr, bbr_drain_rtt)) ||
11464 (flight >= bbr->r_ctl.flightsize_at_drain))) {
11466 * We have doddled with our current hptsi
11467 * gain an srtt and have still not made it
11468 * to target, or we have increased our flight.
11469 * Lets reduce the gain by xx%
11470 * flooring the reduce at DRAIN (based on
11475 bbr->r_ctl.flightsize_at_drain = flight;
11476 bbr->r_ctl.rc_probertt_srttchktim = cts;
11477 red = max((bbr->r_ctl.bbr_rttprobe_gain_val / 10), 1);
11478 if ((bbr->r_ctl.rc_bbr_hptsi_gain - red) > max(bbr_drain_floor, 1)) {
11479 /* Reduce our gain again */
11480 bbr->r_ctl.rc_bbr_hptsi_gain -= red;
11481 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG, 0);
11482 } else if (bbr->r_ctl.rc_bbr_hptsi_gain > max(bbr_drain_floor, 1)) {
11483 /* one more chance before we give up */
11484 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1);
11485 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG_FINAL, 0);
11487 /* At the very bottom */
11488 bbr->r_ctl.rc_bbr_hptsi_gain = max((bbr_drain_floor-1), 1);
11492 if (bbr->r_ctl.rc_bbr_enters_probertt &&
11493 (TSTMP_GT(cts, bbr->r_ctl.rc_bbr_enters_probertt)) &&
11494 ((cts - bbr->r_ctl.rc_bbr_enters_probertt) >= bbr_rtt_probe_time)) {
11495 /* Time to exit probe RTT normally */
11496 bbr_exit_probe_rtt(bbr->rc_tp, bbr, cts);
11498 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) {
11499 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) &&
11500 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) {
11502 * This qualifies as a RTT_PROBE session since we
11503 * drop the data outstanding to nothing and waited
11504 * more than bbr_rtt_probe_time.
11506 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0);
11507 bbr_set_reduced_rtt(bbr, cts, __LINE__);
11509 if (bbr_should_enter_probe_rtt(bbr, cts)) {
11510 bbr_enter_probe_rtt(bbr, cts, __LINE__);
11512 bbr_set_probebw_gains(bbr, cts, losses);
11518 bbr_check_bbr_for_state(struct tcp_bbr *bbr, uint32_t cts, int32_t line, uint32_t losses)
11522 if ((cts - bbr->r_ctl.rc_rcv_epoch_start) >= bbr_get_rtt(bbr, BBR_RTT_PROP)) {
11523 bbr_set_epoch(bbr, cts, line);
11524 /* At each epoch doe lt bw sampling */
11527 bbr_state_change(bbr, cts, epoch, bbr->rc_is_pkt_epoch_now, losses);
11531 bbr_do_segment_nounlock(struct mbuf *m, struct tcphdr *th, struct socket *so,
11532 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos,
11533 int32_t nxt_pkt, struct timeval *tv)
11535 int32_t thflags, retval;
11536 uint32_t cts, lcts;
11539 struct tcp_bbr *bbr;
11540 struct bbr_sendmap *rsm;
11541 struct timeval ltv;
11542 int32_t did_out = 0;
11543 int32_t in_recovery;
11545 int32_t prev_state;
11548 nsegs = max(1, m->m_pkthdr.lro_nsegs);
11549 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
11550 /* add in our stats */
11551 kern_prefetch(bbr, &prev_state);
11553 thflags = th->th_flags;
11555 * If this is either a state-changing packet or current state isn't
11556 * established, we require a write lock on tcbinfo. Otherwise, we
11557 * allow the tcbinfo to be in either alocked or unlocked, as the
11558 * caller may have unnecessarily acquired a write lock due to a
11561 INP_WLOCK_ASSERT(tp->t_inpcb);
11562 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
11564 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
11567 tp->t_rcvtime = ticks;
11569 * Unscale the window into a 32-bit value. For the SYN_SENT state
11570 * the scale is zero.
11572 tiwin = th->th_win << tp->snd_scale;
11574 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
11577 * Parse options on any incoming segment.
11579 tcp_dooptions(&to, (u_char *)(th + 1),
11580 (th->th_off << 2) - sizeof(struct tcphdr),
11581 (thflags & TH_SYN) ? TO_SYN : 0);
11583 if (m->m_flags & M_TSTMP) {
11584 /* Prefer the hardware timestamp if present */
11585 struct timespec ts;
11587 mbuf_tstmp2timespec(m, &ts);
11588 bbr->rc_tv.tv_sec = ts.tv_sec;
11589 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000;
11590 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usectick(&bbr->rc_tv);
11591 } else if (m->m_flags & M_TSTMP_LRO) {
11592 /* Next the arrival timestamp */
11593 struct timespec ts;
11595 mbuf_tstmp2timespec(m, &ts);
11596 bbr->rc_tv.tv_sec = ts.tv_sec;
11597 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000;
11598 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usectick(&bbr->rc_tv);
11601 * Ok just get the current time.
11603 bbr->r_ctl.rc_rcvtime = lcts = cts = tcp_get_usecs(&bbr->rc_tv);
11606 * If echoed timestamp is later than the current time, fall back to
11607 * non RFC1323 RTT calculation. Normalize timestamp if syncookies
11608 * were used when this connection was established.
11610 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
11611 to.to_tsecr -= tp->ts_offset;
11612 if (TSTMP_GT(to.to_tsecr, tcp_tv_to_mssectick(&bbr->rc_tv)))
11616 * If its the first time in we need to take care of options and
11617 * verify we can do SACK for rack!
11619 if (bbr->r_state == 0) {
11621 * Process options only when we get SYN/ACK back. The SYN
11622 * case for incoming connections is handled in tcp_syncache.
11623 * According to RFC1323 the window field in a SYN (i.e., a
11624 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX
11625 * this is traditional behavior, may need to be cleaned up.
11627 if (bbr->rc_inp == NULL) {
11628 bbr->rc_inp = tp->t_inpcb;
11631 * We need to init rc_inp here since its not init'd when
11632 * bbr_init is called
11634 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
11635 if ((to.to_flags & TOF_SCALE) &&
11636 (tp->t_flags & TF_REQ_SCALE)) {
11637 tp->t_flags |= TF_RCVD_SCALE;
11638 tp->snd_scale = to.to_wscale;
11641 * Initial send window. It will be updated with the
11642 * next incoming segment to the scaled value.
11644 tp->snd_wnd = th->th_win;
11645 if (to.to_flags & TOF_TS) {
11646 tp->t_flags |= TF_RCVD_TSTMP;
11647 tp->ts_recent = to.to_tsval;
11648 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
11650 if (to.to_flags & TOF_MSS)
11651 tcp_mss(tp, to.to_mss);
11652 if ((tp->t_flags & TF_SACK_PERMIT) &&
11653 (to.to_flags & TOF_SACKPERM) == 0)
11654 tp->t_flags &= ~TF_SACK_PERMIT;
11655 if (IS_FASTOPEN(tp->t_flags)) {
11656 if (to.to_flags & TOF_FASTOPEN) {
11659 if (to.to_flags & TOF_MSS)
11662 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
11666 tcp_fastopen_update_cache(tp, mss,
11667 to.to_tfo_len, to.to_tfo_cookie);
11669 tcp_fastopen_disable_path(tp);
11673 * At this point we are at the initial call. Here we decide
11674 * if we are doing RACK or not. We do this by seeing if
11675 * TF_SACK_PERMIT is set, if not rack is *not* possible and
11676 * we switch to the default code.
11678 if ((tp->t_flags & TF_SACK_PERMIT) == 0) {
11680 tcp_switch_back_to_default(tp);
11681 (*tp->t_fb->tfb_tcp_do_segment) (m, th, so, tp, drop_hdrlen,
11686 bbr->r_is_v6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
11687 tcp_set_hpts(tp->t_inpcb);
11688 sack_filter_clear(&bbr->r_ctl.bbr_sf, th->th_ack);
11690 if (thflags & TH_ACK) {
11691 /* Track ack types */
11692 if (to.to_flags & TOF_SACK)
11693 BBR_STAT_INC(bbr_acks_with_sacks);
11695 BBR_STAT_INC(bbr_plain_acks);
11698 * This is the one exception case where we set the rack state
11699 * always. All other times (timers etc) we must have a rack-state
11700 * set (so we assure we have done the checks above for SACK).
11702 if (bbr->r_state != tp->t_state)
11703 bbr_set_state(tp, bbr, tiwin);
11705 if (SEQ_GT(th->th_ack, tp->snd_una) && (rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map)) != NULL)
11706 kern_prefetch(rsm, &prev_state);
11707 prev_state = bbr->r_state;
11708 bbr->rc_ack_was_delayed = 0;
11709 lost = bbr->r_ctl.rc_lost;
11710 bbr->rc_is_pkt_epoch_now = 0;
11711 if (m->m_flags & (M_TSTMP|M_TSTMP_LRO)) {
11712 /* Get the real time into lcts and figure the real delay */
11713 lcts = tcp_get_usecs(<v);
11714 if (TSTMP_GT(lcts, cts)) {
11715 bbr->r_ctl.rc_ack_hdwr_delay = lcts - cts;
11716 bbr->rc_ack_was_delayed = 1;
11717 if (TSTMP_GT(bbr->r_ctl.rc_ack_hdwr_delay,
11718 bbr->r_ctl.highest_hdwr_delay))
11719 bbr->r_ctl.highest_hdwr_delay = bbr->r_ctl.rc_ack_hdwr_delay;
11721 bbr->r_ctl.rc_ack_hdwr_delay = 0;
11722 bbr->rc_ack_was_delayed = 0;
11725 bbr->r_ctl.rc_ack_hdwr_delay = 0;
11726 bbr->rc_ack_was_delayed = 0;
11728 bbr_log_ack_event(bbr, th, &to, tlen, nsegs, cts, nxt_pkt, m);
11729 if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
11732 goto done_with_input;
11735 * If a segment with the ACK-bit set arrives in the SYN-SENT state
11736 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9.
11738 if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
11739 (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
11740 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11743 in_recovery = IN_RECOVERY(tp->t_flags);
11744 if (tiwin > bbr->r_ctl.rc_high_rwnd)
11745 bbr->r_ctl.rc_high_rwnd = tiwin;
11746 #ifdef BBR_INVARIANTS
11747 if ((tp->t_inpcb->inp_flags & INP_DROPPED) ||
11748 (tp->t_inpcb->inp_flags2 & INP_FREED)) {
11749 panic("tp:%p bbr:%p given a dropped inp:%p",
11750 tp, bbr, tp->t_inpcb);
11753 bbr->r_ctl.rc_flight_at_input = ctf_flight_size(tp,
11754 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11755 bbr->rtt_valid = 0;
11756 if (to.to_flags & TOF_TS) {
11757 bbr->rc_ts_valid = 1;
11758 bbr->r_ctl.last_inbound_ts = to.to_tsval;
11760 bbr->rc_ts_valid = 0;
11761 bbr->r_ctl.last_inbound_ts = 0;
11763 retval = (*bbr->r_substate) (m, th, so,
11764 tp, &to, drop_hdrlen,
11765 tlen, tiwin, thflags, nxt_pkt);
11766 #ifdef BBR_INVARIANTS
11767 if ((retval == 0) &&
11768 (tp->t_inpcb == NULL)) {
11769 panic("retval:%d tp:%p t_inpcb:NULL state:%d",
11770 retval, tp, prev_state);
11774 BBR_STAT_INC(bbr_rlock_left_ret0);
11776 BBR_STAT_INC(bbr_rlock_left_ret1);
11779 * If retval is 1 the tcb is unlocked and most likely the tp
11782 INP_WLOCK_ASSERT(tp->t_inpcb);
11783 tcp_bbr_xmit_timer_commit(bbr, tp, cts);
11784 if (bbr->rc_is_pkt_epoch_now)
11785 bbr_set_pktepoch(bbr, cts, __LINE__);
11786 bbr_check_bbr_for_state(bbr, cts, __LINE__, (bbr->r_ctl.rc_lost - lost));
11787 if (nxt_pkt == 0) {
11788 if (bbr->r_wanted_output != 0) {
11789 bbr->rc_output_starts_timer = 0;
11791 (void)tp->t_fb->tfb_tcp_output(tp);
11793 bbr_start_hpts_timer(bbr, tp, cts, 6, 0, 0);
11795 if ((nxt_pkt == 0) &&
11796 ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) &&
11797 (SEQ_GT(tp->snd_max, tp->snd_una) ||
11798 (tp->t_flags & TF_DELACK) ||
11799 ((V_tcp_always_keepalive || bbr->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
11800 (tp->t_state <= TCPS_CLOSING)))) {
11802 * We could not send (probably in the hpts but
11803 * stopped the timer)?
11805 if ((tp->snd_max == tp->snd_una) &&
11806 ((tp->t_flags & TF_DELACK) == 0) &&
11807 (bbr->rc_inp->inp_in_hpts) &&
11808 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
11810 * keep alive not needed if we are hptsi
11815 if (bbr->rc_inp->inp_in_hpts) {
11816 tcp_hpts_remove(bbr->rc_inp, HPTS_REMOVE_OUTPUT);
11817 if ((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
11818 (TSTMP_GT(lcts, bbr->rc_pacer_started))) {
11821 del = lcts - bbr->rc_pacer_started;
11822 if (bbr->r_ctl.rc_last_delay_val > del) {
11823 BBR_STAT_INC(bbr_force_timer_start);
11824 bbr->r_ctl.rc_last_delay_val -= del;
11825 bbr->rc_pacer_started = lcts;
11828 bbr->r_ctl.rc_last_delay_val = 0;
11829 BBR_STAT_INC(bbr_force_output);
11830 (void)tp->t_fb->tfb_tcp_output(tp);
11834 bbr_start_hpts_timer(bbr, tp, cts, 8, bbr->r_ctl.rc_last_delay_val,
11837 } else if ((bbr->rc_output_starts_timer == 0) && (nxt_pkt == 0)) {
11838 /* Do we have the correct timer running? */
11839 bbr_timer_audit(tp, bbr, lcts, &so->so_snd);
11841 /* Do we have a new state */
11842 if (bbr->r_state != tp->t_state)
11843 bbr_set_state(tp, bbr, tiwin);
11845 bbr_log_doseg_done(bbr, cts, nxt_pkt, did_out);
11847 bbr->r_wanted_output = 0;
11848 #ifdef BBR_INVARIANTS
11849 if (tp->t_inpcb == NULL) {
11850 panic("OP:%d retval:%d tp:%p t_inpcb:NULL state:%d",
11852 retval, tp, prev_state);
11860 bbr_log_type_hrdwtso(struct tcpcb *tp, struct tcp_bbr *bbr, int len, int mod, int what_we_can_send)
11862 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
11863 union tcp_log_stackspecific log;
11867 cts = tcp_get_usecs(&tv);
11868 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
11869 log.u_bbr.flex1 = bbr->r_ctl.rc_pace_min_segs;
11870 log.u_bbr.flex2 = what_we_can_send;
11871 log.u_bbr.flex3 = bbr->r_ctl.rc_pace_max_segs;
11872 log.u_bbr.flex4 = len;
11873 log.u_bbr.flex5 = 0;
11874 log.u_bbr.flex7 = mod;
11875 log.u_bbr.flex8 = 1;
11876 TCP_LOG_EVENTP(tp, NULL,
11877 &tp->t_inpcb->inp_socket->so_rcv,
11878 &tp->t_inpcb->inp_socket->so_snd,
11880 0, &log, false, &tv);
11885 bbr_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
11886 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos)
11891 /* First lets see if we have old packets */
11892 if (tp->t_in_pkt) {
11893 if (ctf_do_queued_segments(so, tp, 1)) {
11898 if (m->m_flags & M_TSTMP_LRO) {
11899 tv.tv_sec = m->m_pkthdr.rcv_tstmp /1000000000;
11900 tv.tv_usec = (m->m_pkthdr.rcv_tstmp % 1000000000)/1000;
11902 /* Should not be should we kassert instead? */
11903 tcp_get_usecs(&tv);
11905 retval = bbr_do_segment_nounlock(m, th, so, tp,
11906 drop_hdrlen, tlen, iptos, 0, &tv);
11908 INP_WUNLOCK(tp->t_inpcb);
11912 * Return how much data can be sent without violating the
11916 static inline uint32_t
11917 bbr_what_can_we_send(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t sendwin,
11918 uint32_t avail, int32_t sb_offset, uint32_t cts)
11922 if (ctf_outstanding(tp) >= tp->snd_wnd) {
11923 /* We never want to go over our peers rcv-window */
11928 flight = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11929 if (flight >= sendwin) {
11931 * We have in flight what we are allowed by cwnd (if
11932 * it was rwnd blocking it would have hit above out
11937 len = sendwin - flight;
11938 if ((len + ctf_outstanding(tp)) > tp->snd_wnd) {
11939 /* We would send too much (beyond the rwnd) */
11940 len = tp->snd_wnd - ctf_outstanding(tp);
11942 if ((len + sb_offset) > avail) {
11944 * We don't have that much in the SB, how much is
11947 len = avail - sb_offset;
11954 bbr_do_error_accounting(struct tcpcb *tp, struct tcp_bbr *bbr, struct bbr_sendmap *rsm, int32_t len, int32_t error)
11956 #ifdef NETFLIX_STATS
11957 KMOD_TCPSTAT_INC(tcps_sndpack_error);
11958 KMOD_TCPSTAT_ADD(tcps_sndbyte_error, len);
11963 bbr_do_send_accounting(struct tcpcb *tp, struct tcp_bbr *bbr, struct bbr_sendmap *rsm, int32_t len, int32_t error)
11966 bbr_do_error_accounting(tp, bbr, rsm, len, error);
11969 if ((tp->t_flags & TF_FORCEDATA) && len == 1) {
11971 KMOD_TCPSTAT_INC(tcps_sndprobe);
11973 stats_voi_update_abs_u32(tp->t_stats,
11974 VOI_TCP_RETXPB, len);
11977 if (rsm->r_flags & BBR_TLP) {
11979 * TLP should not count in retran count, but in its
11982 #ifdef NETFLIX_STATS
11983 tp->t_sndtlppack++;
11984 tp->t_sndtlpbyte += len;
11985 KMOD_TCPSTAT_INC(tcps_tlpresends);
11986 KMOD_TCPSTAT_ADD(tcps_tlpresend_bytes, len);
11990 tp->t_sndrexmitpack++;
11991 KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
11992 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
11994 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
11999 * Logs in 0 - 8, 8 is all non probe_bw states 0-7 is
12002 counter_u64_add(bbr_state_lost[rsm->r_bbr_state], len);
12003 if (bbr->rc_bbr_state != BBR_STATE_PROBE_BW) {
12004 /* Non probe_bw log in 1, 2, or 4. */
12005 counter_u64_add(bbr_state_resend[bbr->rc_bbr_state], len);
12008 * Log our probe state 3, and log also 5-13 to show
12009 * us the recovery sub-state for the send. This
12010 * means that 3 == (5+6+7+8+9+10+11+12+13)
12012 counter_u64_add(bbr_state_resend[BBR_STATE_PROBE_BW], len);
12013 counter_u64_add(bbr_state_resend[(bbr_state_val(bbr) + 5)], len);
12015 /* Place in both 16's the totals of retransmitted */
12016 counter_u64_add(bbr_state_lost[16], len);
12017 counter_u64_add(bbr_state_resend[16], len);
12018 /* Place in 17's the total sent */
12019 counter_u64_add(bbr_state_resend[17], len);
12020 counter_u64_add(bbr_state_lost[17], len);
12024 KMOD_TCPSTAT_INC(tcps_sndpack);
12025 KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
12026 /* Place in 17's the total sent */
12027 counter_u64_add(bbr_state_resend[17], len);
12028 counter_u64_add(bbr_state_lost[17], len);
12030 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
12037 bbr_cwnd_limiting(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t in_level)
12039 if (bbr->rc_filled_pipe && bbr_target_cwnd_mult_limit && (bbr->rc_use_google == 0)) {
12041 * Limit the cwnd to not be above N x the target plus whats
12042 * is outstanding. The target is based on the current b/w
12047 target = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), BBR_UNIT);
12048 target += ctf_outstanding(tp);
12049 target *= bbr_target_cwnd_mult_limit;
12050 if (tp->snd_cwnd > target)
12051 tp->snd_cwnd = target;
12052 bbr_log_type_cwndupd(bbr, 0, 0, 0, 10, 0, 0, __LINE__);
12057 bbr_window_update_needed(struct tcpcb *tp, struct socket *so, uint32_t recwin, int32_t maxseg)
12060 * "adv" is the amount we could increase the window, taking into
12061 * account that we are limited by TCP_MAXWIN << tp->rcv_scale.
12066 adv = min(recwin, TCP_MAXWIN << tp->rcv_scale);
12067 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
12068 oldwin = (tp->rcv_adv - tp->rcv_nxt);
12074 * If the new window size ends up being the same as the old size
12075 * when it is scaled, then don't force a window update.
12077 if (oldwin >> tp->rcv_scale == (adv + oldwin) >> tp->rcv_scale)
12080 if (adv >= (2 * maxseg) &&
12081 (adv >= (so->so_rcv.sb_hiwat / 4) ||
12082 recwin <= (so->so_rcv.sb_hiwat / 8) ||
12083 so->so_rcv.sb_hiwat <= 8 * maxseg)) {
12086 if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat)
12092 * Return 0 on success and a errno on failure to send.
12093 * Note that a 0 return may not mean we sent anything
12094 * if the TCB was on the hpts. A non-zero return
12095 * does indicate the error we got from ip[6]_output.
12098 bbr_output_wtime(struct tcpcb *tp, const struct timeval *tv)
12103 uint32_t recwin, sendwin;
12105 int32_t flags, abandon, error = 0;
12106 struct tcp_log_buffer *lgb = NULL;
12109 uint32_t if_hw_tsomaxsegcount = 0;
12110 uint32_t if_hw_tsomaxsegsize = 0;
12111 uint32_t if_hw_tsomax = 0;
12112 struct ip *ip = NULL;
12114 struct ipovly *ipov = NULL;
12116 struct tcp_bbr *bbr;
12118 #ifdef NETFLIX_TCPOUDP
12119 struct udphdr *udp = NULL;
12121 u_char opt[TCP_MAXOLEN];
12122 unsigned ipoptlen, optlen, hdrlen;
12123 #ifdef NETFLIX_TCPOUDP
12127 uint32_t delay_calc=0;
12128 uint8_t doing_tlp = 0;
12129 uint8_t local_options;
12130 #ifdef BBR_INVARIANTS
12131 uint8_t doing_retran_from = 0;
12132 uint8_t picked_up_retran = 0;
12134 uint8_t wanted_cookie = 0;
12135 uint8_t more_to_rxt=0;
12136 int32_t prefetch_so_done = 0;
12137 int32_t prefetch_rsm = 0;
12138 uint32_t what_we_can = 0;
12139 uint32_t tot_len = 0;
12140 uint32_t rtr_cnt = 0;
12141 uint32_t maxseg, pace_max_segs, p_maxseg;
12142 int32_t csum_flags;
12144 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
12145 unsigned ipsec_optlen = 0;
12148 volatile int32_t sack_rxmit;
12149 struct bbr_sendmap *rsm = NULL;
12155 struct sockbuf *sb;
12156 uint32_t hpts_calling;
12158 struct ip6_hdr *ip6 = NULL;
12161 uint8_t app_limited = BBR_JR_SENT_DATA;
12162 uint8_t filled_all = 0;
12163 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
12164 /* We take a cache hit here */
12165 memcpy(&bbr->rc_tv, tv, sizeof(struct timeval));
12166 cts = tcp_tv_to_usectick(&bbr->rc_tv);
12168 so = inp->inp_socket;
12171 if (sb->sb_flags & SB_TLS_IFNET)
12176 kern_prefetch(sb, &maxseg);
12177 maxseg = tp->t_maxseg - bbr->rc_last_options;
12178 if (bbr_minseg(bbr) < maxseg) {
12179 tcp_bbr_tso_size_check(bbr, cts);
12181 /* Remove any flags that indicate we are pacing on the inp */
12182 pace_max_segs = bbr->r_ctl.rc_pace_max_segs;
12183 p_maxseg = min(maxseg, pace_max_segs);
12184 INP_WLOCK_ASSERT(inp);
12186 if (tp->t_flags & TF_TOE)
12187 return (tcp_offload_output(tp));
12191 if (bbr->r_state) {
12192 /* Use the cache line loaded if possible */
12193 isipv6 = bbr->r_is_v6;
12195 isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
12198 if (((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) &&
12199 inp->inp_in_hpts) {
12201 * We are on the hpts for some timer but not hptsi output.
12202 * Possibly remove from the hpts so we can send/recv etc.
12204 if ((tp->t_flags & TF_ACKNOW) == 0) {
12206 * No immediate demand right now to send an ack, but
12207 * the user may have read, making room for new data
12208 * (a window update). If so we may want to cancel
12209 * whatever timer is running (KEEP/DEL-ACK?) and
12210 * continue to send out a window update. Or we may
12211 * have gotten more data into the socket buffer to
12214 recwin = min(max(sbspace(&so->so_rcv), 0),
12215 TCP_MAXWIN << tp->rcv_scale);
12216 if ((bbr_window_update_needed(tp, so, recwin, maxseg) == 0) &&
12217 ((sbavail(sb) + ((tcp_outflags[tp->t_state] & TH_FIN) ? 1 : 0)) <=
12218 (tp->snd_max - tp->snd_una))) {
12220 * Nothing new to send and no window update
12221 * is needed to send. Lets just return and
12222 * let the timer-run off.
12227 tcp_hpts_remove(inp, HPTS_REMOVE_OUTPUT);
12228 bbr_timer_cancel(bbr, __LINE__, cts);
12230 if (bbr->r_ctl.rc_last_delay_val) {
12231 /* Calculate a rough delay for early escape to sending */
12232 if (SEQ_GT(cts, bbr->rc_pacer_started))
12233 delay_calc = cts - bbr->rc_pacer_started;
12234 if (delay_calc >= bbr->r_ctl.rc_last_delay_val)
12235 delay_calc -= bbr->r_ctl.rc_last_delay_val;
12239 /* Mark that we have called bbr_output(). */
12240 if ((bbr->r_timer_override) ||
12241 (tp->t_flags & TF_FORCEDATA) ||
12242 (tp->t_state < TCPS_ESTABLISHED)) {
12243 /* Timeouts or early states are exempt */
12244 if (inp->inp_in_hpts)
12245 tcp_hpts_remove(inp, HPTS_REMOVE_OUTPUT);
12246 } else if (inp->inp_in_hpts) {
12247 if ((bbr->r_ctl.rc_last_delay_val) &&
12248 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
12251 * We were being paced for output and the delay has
12252 * already exceeded when we were supposed to be
12253 * called, lets go ahead and pull out of the hpts
12256 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_LATE], 1);
12257 bbr->r_ctl.rc_last_delay_val = 0;
12258 tcp_hpts_remove(inp, HPTS_REMOVE_OUTPUT);
12259 } else if (tp->t_state == TCPS_CLOSED) {
12260 bbr->r_ctl.rc_last_delay_val = 0;
12261 tcp_hpts_remove(inp, HPTS_REMOVE_OUTPUT);
12264 * On the hpts, you shall not pass! even if ACKNOW
12265 * is on, we will when the hpts fires, unless of
12266 * course we are overdue.
12268 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_INPACE], 1);
12272 bbr->rc_cwnd_limited = 0;
12273 if (bbr->r_ctl.rc_last_delay_val) {
12274 /* recalculate the real delay and deal with over/under */
12275 if (SEQ_GT(cts, bbr->rc_pacer_started))
12276 delay_calc = cts - bbr->rc_pacer_started;
12279 if (delay_calc >= bbr->r_ctl.rc_last_delay_val)
12280 /* Setup the delay which will be added in */
12281 delay_calc -= bbr->r_ctl.rc_last_delay_val;
12284 * We are early setup to adjust
12287 uint64_t merged_val;
12289 bbr->r_ctl.rc_agg_early += (bbr->r_ctl.rc_last_delay_val - delay_calc);
12290 bbr->r_agg_early_set = 1;
12291 if (bbr->r_ctl.rc_hptsi_agg_delay) {
12292 if (bbr->r_ctl.rc_hptsi_agg_delay >= bbr->r_ctl.rc_agg_early) {
12293 /* Nope our previous late cancels out the early */
12294 bbr->r_ctl.rc_hptsi_agg_delay -= bbr->r_ctl.rc_agg_early;
12295 bbr->r_agg_early_set = 0;
12296 bbr->r_ctl.rc_agg_early = 0;
12298 bbr->r_ctl.rc_agg_early -= bbr->r_ctl.rc_hptsi_agg_delay;
12299 bbr->r_ctl.rc_hptsi_agg_delay = 0;
12302 merged_val = bbr->rc_pacer_started;
12304 merged_val |= bbr->r_ctl.rc_last_delay_val;
12305 bbr_log_pacing_delay_calc(bbr, inp->inp_hpts_calls,
12306 bbr->r_ctl.rc_agg_early, cts, delay_calc, merged_val,
12307 bbr->r_agg_early_set, 3);
12308 bbr->r_ctl.rc_last_delay_val = 0;
12309 BBR_STAT_INC(bbr_early);
12313 /* We were not delayed due to hptsi */
12314 if (bbr->r_agg_early_set)
12315 bbr->r_ctl.rc_agg_early = 0;
12316 bbr->r_agg_early_set = 0;
12321 * We had a hptsi delay which means we are falling behind on
12322 * sending at the expected rate. Calculate an extra amount
12323 * of data we can send, if any, to put us back on track.
12325 if ((bbr->r_ctl.rc_hptsi_agg_delay + delay_calc) < bbr->r_ctl.rc_hptsi_agg_delay)
12326 bbr->r_ctl.rc_hptsi_agg_delay = 0xffffffff;
12328 bbr->r_ctl.rc_hptsi_agg_delay += delay_calc;
12330 sendwin = min(tp->snd_wnd, tp->snd_cwnd);
12331 if ((tp->snd_una == tp->snd_max) &&
12332 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) &&
12335 * Ok we have been idle with nothing outstanding
12336 * we possibly need to start fresh with either a new
12337 * suite of states or a fast-ramp up.
12339 bbr_restart_after_idle(bbr,
12340 cts, bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time));
12343 * Now was there a hptsi delay where we are behind? We only count
12344 * being behind if: a) We are not in recovery. b) There was a delay.
12345 * <and> c) We had room to send something.
12348 hpts_calling = inp->inp_hpts_calls;
12349 inp->inp_hpts_calls = 0;
12350 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
12351 if (bbr_process_timers(tp, bbr, cts, hpts_calling)) {
12352 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_ATIMER], 1);
12356 bbr->rc_inp->inp_flags2 &= ~INP_MBUF_QUEUE_READY;
12357 if (hpts_calling &&
12358 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
12359 bbr->r_ctl.rc_last_delay_val = 0;
12361 bbr->r_timer_override = 0;
12362 bbr->r_wanted_output = 0;
12364 * For TFO connections in SYN_RECEIVED, only allow the initial
12365 * SYN|ACK and those sent by the retransmit timer.
12367 if (IS_FASTOPEN(tp->t_flags) &&
12368 ((tp->t_state == TCPS_SYN_RECEIVED) ||
12369 (tp->t_state == TCPS_SYN_SENT)) &&
12370 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */
12371 (tp->t_rxtshift == 0)) { /* not a retransmit */
12375 * Before sending anything check for a state update. For hpts
12376 * calling without input this is important. If its input calling
12377 * then this was already done.
12379 if (bbr->rc_use_google == 0)
12380 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0);
12383 * If we've recently taken a timeout, snd_max will be greater than
12384 * snd_max. BBR in general does not pay much attention to snd_nxt
12385 * for historic reasons the persist timer still uses it. This means
12386 * we have to look at it. All retransmissions that are not persits
12387 * use the rsm that needs to be sent so snd_nxt is ignored. At the
12388 * end of this routine we pull snd_nxt always up to snd_max.
12391 #ifdef BBR_INVARIANTS
12392 doing_retran_from = picked_up_retran = 0;
12398 sendwin = min(tp->snd_wnd, tp->snd_cwnd);
12399 sb_offset = tp->snd_max - tp->snd_una;
12400 flags = tcp_outflags[tp->t_state];
12404 if (flags & TH_RST) {
12409 while (bbr->r_ctl.rc_free_cnt < bbr_min_req_free) {
12410 /* We need to always have one in reserve */
12411 rsm = bbr_alloc(bbr);
12414 /* Lie to get on the hpts */
12415 tot_len = tp->t_maxseg;
12417 /* Retry in a ms */
12419 goto just_return_nolock;
12421 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_free, rsm, r_next);
12422 bbr->r_ctl.rc_free_cnt++;
12425 /* What do we send, a resend? */
12426 if (bbr->r_ctl.rc_resend == NULL) {
12427 /* Check for rack timeout */
12428 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts);
12429 if (bbr->r_ctl.rc_resend) {
12430 #ifdef BBR_INVARIANTS
12431 picked_up_retran = 1;
12433 bbr_cong_signal(tp, NULL, CC_NDUPACK, bbr->r_ctl.rc_resend);
12436 if (bbr->r_ctl.rc_resend) {
12437 rsm = bbr->r_ctl.rc_resend;
12438 #ifdef BBR_INVARIANTS
12439 doing_retran_from = 1;
12441 /* Remove any TLP flags its a RACK or T-O */
12442 rsm->r_flags &= ~BBR_TLP;
12443 bbr->r_ctl.rc_resend = NULL;
12444 if (SEQ_LT(rsm->r_start, tp->snd_una)) {
12445 #ifdef BBR_INVARIANTS
12446 panic("Huh, tp:%p bbr:%p rsm:%p start:%u < snd_una:%u\n",
12447 tp, bbr, rsm, rsm->r_start, tp->snd_una);
12448 goto recheck_resend;
12452 goto recheck_resend;
12456 if (rsm->r_flags & BBR_HAS_SYN) {
12457 /* Only retransmit a SYN by itself */
12459 if ((flags & TH_SYN) == 0) {
12460 /* Huh something is wrong */
12462 if (rsm->r_start == rsm->r_end) {
12463 /* Clean it up, somehow we missed the ack? */
12464 bbr_log_syn(tp, NULL);
12466 /* TFO with data? */
12467 rsm->r_flags &= ~BBR_HAS_SYN;
12468 len = rsm->r_end - rsm->r_start;
12471 /* Retransmitting SYN */
12477 len = rsm->r_end - rsm->r_start;
12478 if ((bbr->rc_resends_use_tso == 0) &&
12480 ((sb->sb_flags & SB_TLS_IFNET) == 0) &&
12486 sb_offset = rsm->r_start - tp->snd_una;
12489 KMOD_TCPSTAT_INC(tcps_sack_rexmits);
12490 KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes,
12493 /* I dont think this can happen */
12495 goto recheck_resend;
12497 BBR_STAT_INC(bbr_resends_set);
12498 } else if (bbr->r_ctl.rc_tlp_send) {
12503 rsm = bbr->r_ctl.rc_tlp_send;
12504 bbr->r_ctl.rc_tlp_send = NULL;
12506 len = rsm->r_end - rsm->r_start;
12508 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg))
12511 if (SEQ_GT(tp->snd_una, rsm->r_start)) {
12512 #ifdef BBR_INVARIANTS
12513 panic("tp:%p bbc:%p snd_una:%u rsm:%p r_start:%u",
12514 tp, bbr, tp->snd_una, rsm, rsm->r_start);
12518 goto recheck_resend;
12521 sb_offset = rsm->r_start - tp->snd_una;
12522 BBR_STAT_INC(bbr_tlp_set);
12525 * Enforce a connection sendmap count limit if set
12526 * as long as we are not retransmiting.
12528 if ((rsm == NULL) &&
12529 (V_tcp_map_entries_limit > 0) &&
12530 (bbr->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
12531 BBR_STAT_INC(bbr_alloc_limited);
12532 if (!bbr->alloc_limit_reported) {
12533 bbr->alloc_limit_reported = 1;
12534 BBR_STAT_INC(bbr_alloc_limited_conns);
12536 goto just_return_nolock;
12538 #ifdef BBR_INVARIANTS
12539 if (rsm && SEQ_LT(rsm->r_start, tp->snd_una)) {
12540 panic("tp:%p bbr:%p rsm:%p sb_offset:%u len:%u",
12541 tp, bbr, rsm, sb_offset, len);
12545 * Get standard flags, and add SYN or FIN if requested by 'hidden'
12548 if (tp->t_flags & TF_NEEDFIN && (rsm == NULL))
12550 if (tp->t_flags & TF_NEEDSYN)
12553 if (rsm && (rsm->r_flags & BBR_HAS_FIN)) {
12554 /* we are retransmitting the fin */
12558 * When retransmitting data do *not* include the
12559 * FIN. This could happen from a TLP probe if we
12560 * allowed data with a FIN.
12565 if (flags & TH_FIN)
12568 if ((sack_rxmit == 0) && (prefetch_rsm == 0)) {
12571 end_rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_tmap, bbr_sendmap, r_tnext);
12573 kern_prefetch(end_rsm, &prefetch_rsm);
12578 * If in persist timeout with window of 0, send 1 byte. Otherwise,
12579 * if window is small but nonzero and time TF_SENTFIN expired, we
12580 * will send what we can and go to transmit state.
12582 if (tp->t_flags & TF_FORCEDATA) {
12583 if ((sendwin == 0) || (sendwin <= (tp->snd_max - tp->snd_una))) {
12585 * If we still have some data to send, then clear
12586 * the FIN bit. Usually this would happen below
12587 * when it realizes that we aren't sending all the
12588 * data. However, if we have exactly 1 byte of
12589 * unsent data, then it won't clear the FIN bit
12590 * below, and if we are in persist state, we wind up
12591 * sending the packet without recording that we sent
12594 * We can't just blindly clear the FIN bit, because
12595 * if we don't have any more data to send then the
12596 * probe will be the FIN itself.
12598 if (sb_offset < sbused(sb))
12602 if ((bbr->rc_in_persist != 0) &&
12603 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2),
12604 bbr_minseg(bbr)))) {
12605 /* Exit persists if there is space */
12606 bbr_exit_persist(tp, bbr, cts, __LINE__);
12610 * If we are dropping persist mode then we
12611 * need to correct sb_offset if not a
12614 sb_offset = tp->snd_max - tp->snd_una;
12619 * If snd_nxt == snd_max and we have transmitted a FIN, the
12620 * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a
12621 * negative length. This can also occur when TCP opens up its
12622 * congestion window while receiving additional duplicate acks after
12623 * fast-retransmit because TCP will reset snd_nxt to snd_max after
12624 * the fast-retransmit.
12626 * In the normal retransmit-FIN-only case, however, snd_nxt will be
12627 * set to snd_una, the sb_offset will be 0, and the length may wind
12630 * If sack_rxmit is true we are retransmitting from the scoreboard
12631 * in which case len is already set.
12633 if (sack_rxmit == 0) {
12636 avail = sbavail(sb);
12637 if (SEQ_GT(tp->snd_max, tp->snd_una))
12638 sb_offset = tp->snd_max - tp->snd_una;
12641 if (bbr->rc_tlp_new_data) {
12642 /* TLP is forcing out new data */
12648 if (tlplen > (uint32_t)(avail - sb_offset)) {
12649 tlplen = (uint32_t)(avail - sb_offset);
12651 if (tlplen > tp->snd_wnd) {
12656 bbr->rc_tlp_new_data = 0;
12658 what_we_can = len = bbr_what_can_we_send(tp, bbr, sendwin, avail, sb_offset, cts);
12659 if ((len < p_maxseg) &&
12660 (bbr->rc_in_persist == 0) &&
12661 (ctf_outstanding(tp) >= (2 * p_maxseg)) &&
12662 ((avail - sb_offset) >= p_maxseg)) {
12664 * We are not completing whats in the socket
12665 * buffer (i.e. there is at least a segment
12666 * waiting to send) and we have 2 or more
12667 * segments outstanding. There is no sense
12668 * of sending a little piece. Lets defer and
12669 * and wait until we can send a whole
12674 if ((tp->t_flags & TF_FORCEDATA) && (bbr->rc_in_persist)) {
12676 * We are in persists, figure out if
12677 * a retransmit is available (maybe the previous
12678 * persists we sent) or if we have to send new
12681 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
12683 len = rsm->r_end - rsm->r_start;
12684 if (rsm->r_flags & BBR_HAS_FIN)
12686 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg))
12689 BBR_STAT_INC(bbr_persist_reneg);
12691 * XXXrrs we could force the len to
12692 * 1 byte here to cause the chunk to
12693 * split apart.. but that would then
12694 * mean we always retransmit it as
12695 * one byte even after the window
12699 sb_offset = rsm->r_start - tp->snd_una;
12702 * First time through in persists or peer
12703 * acked our one byte. Though we do have
12704 * to have something in the sb.
12714 if (prefetch_so_done == 0) {
12715 kern_prefetch(so, &prefetch_so_done);
12716 prefetch_so_done = 1;
12719 * Lop off SYN bit if it has already been sent. However, if this is
12720 * SYN-SENT state and if segment contains data and if we don't know
12721 * that foreign host supports TAO, suppress sending segment.
12723 if ((flags & TH_SYN) && (rsm == NULL) &&
12724 SEQ_GT(tp->snd_max, tp->snd_una)) {
12725 if (tp->t_state != TCPS_SYN_RECEIVED)
12728 * When sending additional segments following a TFO SYN|ACK,
12729 * do not include the SYN bit.
12731 if (IS_FASTOPEN(tp->t_flags) &&
12732 (tp->t_state == TCPS_SYN_RECEIVED))
12734 sb_offset--, len++;
12735 if (sbavail(sb) == 0)
12737 } else if ((flags & TH_SYN) && rsm) {
12739 * Subtract one from the len for the SYN being
12745 * Be careful not to send data and/or FIN on SYN segments. This
12746 * measure is needed to prevent interoperability problems with not
12747 * fully conformant TCP implementations.
12749 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
12754 * On TFO sockets, ensure no data is sent in the following cases:
12756 * - When retransmitting SYN|ACK on a passively-created socket
12757 * - When retransmitting SYN on an actively created socket
12758 * - When sending a zero-length cookie (cookie request) on an
12759 * actively created socket
12760 * - When the socket is in the CLOSED state (RST is being sent)
12762 if (IS_FASTOPEN(tp->t_flags) &&
12763 (((flags & TH_SYN) && (tp->t_rxtshift > 0)) ||
12764 ((tp->t_state == TCPS_SYN_SENT) &&
12765 (tp->t_tfo_client_cookie_len == 0)) ||
12766 (flags & TH_RST))) {
12771 /* Without fast-open there should never be data sent on a SYN */
12772 if ((flags & TH_SYN) && (!IS_FASTOPEN(tp->t_flags)))
12776 * If FIN has been sent but not acked, but we haven't been
12777 * called to retransmit, len will be < 0. Otherwise, window
12778 * shrank after we sent into it. If window shrank to 0,
12779 * cancel pending retransmit, pull snd_nxt back to (closed)
12780 * window, and set the persist timer if it isn't already
12781 * going. If the window didn't close completely, just wait
12784 * We also do a general check here to ensure that we will
12785 * set the persist timer when we have data to send, but a
12786 * 0-byte window. This makes sure the persist timer is set
12787 * even if the packet hits one of the "goto send" lines
12791 if ((tp->snd_wnd == 0) &&
12792 (TCPS_HAVEESTABLISHED(tp->t_state)) &&
12793 (tp->snd_una == tp->snd_max) &&
12794 (sb_offset < (int)sbavail(sb))) {
12796 * Not enough room in the rwnd to send
12797 * a paced segment out.
12799 bbr_enter_persist(tp, bbr, cts, __LINE__);
12801 } else if ((rsm == NULL) &&
12802 (doing_tlp == 0) &&
12803 (len < bbr->r_ctl.rc_pace_max_segs)) {
12805 * We are not sending a full segment for
12806 * some reason. Should we not send anything (think
12807 * sws or persists)?
12809 if ((tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
12810 (TCPS_HAVEESTABLISHED(tp->t_state)) &&
12811 (len < (int)(sbavail(sb) - sb_offset))) {
12813 * Here the rwnd is less than
12814 * the pacing size, this is not a retransmit,
12815 * we are established and
12816 * the send is not the last in the socket buffer
12817 * lets not send, and possibly enter persists.
12820 if (tp->snd_max == tp->snd_una)
12821 bbr_enter_persist(tp, bbr, cts, __LINE__);
12822 } else if ((tp->snd_cwnd >= bbr->r_ctl.rc_pace_max_segs) &&
12823 (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12824 bbr->r_ctl.rc_lost_bytes)) > (2 * maxseg)) &&
12825 (len < (int)(sbavail(sb) - sb_offset)) &&
12826 (len < bbr_minseg(bbr))) {
12828 * Here we are not retransmitting, and
12829 * the cwnd is not so small that we could
12830 * not send at least a min size (rxt timer
12831 * not having gone off), We have 2 segments or
12832 * more already in flight, its not the tail end
12833 * of the socket buffer and the cwnd is blocking
12834 * us from sending out minimum pacing segment size.
12835 * Lets not send anything.
12837 bbr->rc_cwnd_limited = 1;
12839 } else if (((tp->snd_wnd - ctf_outstanding(tp)) <
12840 min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
12841 (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12842 bbr->r_ctl.rc_lost_bytes)) > (2 * maxseg)) &&
12843 (len < (int)(sbavail(sb) - sb_offset)) &&
12844 (TCPS_HAVEESTABLISHED(tp->t_state))) {
12846 * Here we have a send window but we have
12847 * filled it up and we can't send another pacing segment.
12848 * We also have in flight more than 2 segments
12849 * and we are not completing the sb i.e. we allow
12850 * the last bytes of the sb to go out even if
12851 * its not a full pacing segment.
12856 /* len will be >= 0 after this point. */
12857 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
12858 tcp_sndbuf_autoscale(tp, so, sendwin);
12862 if (bbr->rc_in_persist &&
12865 (len < min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs))) {
12867 * We are in persist, not doing a retransmit and don't have enough space
12868 * yet to send a full TSO. So is it at the end of the sb
12869 * if so we need to send else nuke to 0 and don't send.
12872 if (sbavail(sb) > sb_offset)
12873 sbleft = sbavail(sb) - sb_offset;
12876 if (sbleft >= min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs)) {
12877 /* not at end of sb lets not send */
12882 * Decide if we can use TCP Segmentation Offloading (if supported by
12885 * TSO may only be used if we are in a pure bulk sending state. The
12886 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP
12887 * options prevent using TSO. With TSO the TCP header is the same
12888 * (except for the sequence number) for all generated packets. This
12889 * makes it impossible to transmit any options which vary per
12890 * generated segment or packet.
12892 * IPv4 handling has a clear separation of ip options and ip header
12893 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen()
12894 * does the right thing below to provide length of just ip options
12895 * and thus checking for ipoptlen is enough to decide if ip options
12900 ipoptlen = ip6_optlen(inp);
12903 if (inp->inp_options)
12904 ipoptlen = inp->inp_options->m_len -
12905 offsetof(struct ipoption, ipopt_list);
12908 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
12910 * Pre-calculate here as we save another lookup into the darknesses
12911 * of IPsec that way and can actually decide if TSO is ok.
12914 if (isipv6 && IPSEC_ENABLED(ipv6))
12915 ipsec_optlen = IPSEC_HDRSIZE(ipv6, inp);
12921 if (IPSEC_ENABLED(ipv4))
12922 ipsec_optlen = IPSEC_HDRSIZE(ipv4, inp);
12925 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
12926 ipoptlen += ipsec_optlen;
12928 if ((tp->t_flags & TF_TSO) && V_tcp_do_tso &&
12930 (tp->t_port == 0) &&
12931 ((tp->t_flags & TF_SIGNATURE) == 0) &&
12932 tp->rcv_numsacks == 0 &&
12936 recwin = min(max(sbspace(&so->so_rcv), 0),
12937 TCP_MAXWIN << tp->rcv_scale);
12939 * Sender silly window avoidance. We transmit under the following
12940 * conditions when len is non-zero:
12942 * - We have a full segment (or more with TSO) - This is the last
12943 * buffer in a write()/send() and we are either idle or running
12944 * NODELAY - we've timed out (e.g. persist timer) - we have more
12945 * then 1/2 the maximum send window's worth of data (receiver may be
12946 * limited the window size) - we need to retransmit
12953 if (len >= p_maxseg)
12956 * NOTE! on localhost connections an 'ack' from the remote
12957 * end may occur synchronously with the output and cause us
12958 * to flush a buffer queued with moretocome. XXX
12961 if (((tp->t_flags & TF_MORETOCOME) == 0) && /* normal case */
12962 ((tp->t_flags & TF_NODELAY) ||
12963 ((uint32_t)len + (uint32_t)sb_offset) >= sbavail(&so->so_snd)) &&
12964 (tp->t_flags & TF_NOPUSH) == 0) {
12967 if ((tp->snd_una == tp->snd_max) && len) { /* Nothing outstanding */
12970 if (tp->t_flags & TF_FORCEDATA) { /* typ. timeout case */
12973 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) {
12978 * Sending of standalone window updates.
12980 * Window updates are important when we close our window due to a
12981 * full socket buffer and are opening it again after the application
12982 * reads data from it. Once the window has opened again and the
12983 * remote end starts to send again the ACK clock takes over and
12984 * provides the most current window information.
12986 * We must avoid the silly window syndrome whereas every read from
12987 * the receive buffer, no matter how small, causes a window update
12988 * to be sent. We also should avoid sending a flurry of window
12989 * updates when the socket buffer had queued a lot of data and the
12990 * application is doing small reads.
12992 * Prevent a flurry of pointless window updates by only sending an
12993 * update when we can increase the advertized window by more than
12994 * 1/4th of the socket buffer capacity. When the buffer is getting
12995 * full or is very small be more aggressive and send an update
12996 * whenever we can increase by two mss sized segments. In all other
12997 * situations the ACK's to new incoming data will carry further
12998 * window increases.
13000 * Don't send an independent window update if a delayed ACK is
13001 * pending (it will get piggy-backed on it) or the remote side
13002 * already has done a half-close and won't send more data. Skip
13003 * this if the connection is in T/TCP half-open state.
13005 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
13006 !(tp->t_flags & TF_DELACK) &&
13007 !TCPS_HAVERCVDFIN(tp->t_state)) {
13008 /* Check to see if we should do a window update */
13009 if (bbr_window_update_needed(tp, so, recwin, maxseg))
13013 * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW
13014 * is also a catch-all for the retransmit timer timeout case.
13016 if (tp->t_flags & TF_ACKNOW) {
13019 if (((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) {
13022 if (SEQ_GT(tp->snd_up, tp->snd_una)) {
13026 * If our state indicates that FIN should be sent and we have not
13027 * yet done so, then we need to send.
13029 if (flags & TH_FIN &&
13030 ((tp->t_flags & TF_SENTFIN) == 0)) {
13034 * No reason to send a segment, just return.
13037 SOCKBUF_UNLOCK(sb);
13038 just_return_nolock:
13040 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0);
13041 if (bbr->rc_no_pacing)
13043 if (tot_len == 0) {
13044 if ((ctf_outstanding(tp) + min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) >=
13046 BBR_STAT_INC(bbr_rwnd_limited);
13047 app_limited = BBR_JR_RWND_LIMITED;
13048 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp));
13049 if ((bbr->rc_in_persist == 0) &&
13050 TCPS_HAVEESTABLISHED(tp->t_state) &&
13051 (tp->snd_max == tp->snd_una) &&
13052 sbavail(&tp->t_inpcb->inp_socket->so_snd)) {
13053 /* No send window.. we must enter persist */
13054 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
13056 } else if (ctf_outstanding(tp) >= sbavail(sb)) {
13057 BBR_STAT_INC(bbr_app_limited);
13058 app_limited = BBR_JR_APP_LIMITED;
13059 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp));
13060 } else if ((ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
13061 bbr->r_ctl.rc_lost_bytes)) + p_maxseg) >= tp->snd_cwnd) {
13062 BBR_STAT_INC(bbr_cwnd_limited);
13063 app_limited = BBR_JR_CWND_LIMITED;
13064 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
13065 bbr->r_ctl.rc_lost_bytes)));
13066 bbr->rc_cwnd_limited = 1;
13068 BBR_STAT_INC(bbr_app_limited);
13069 app_limited = BBR_JR_APP_LIMITED;
13070 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp));
13072 bbr->r_ctl.rc_hptsi_agg_delay = 0;
13073 bbr->r_agg_early_set = 0;
13074 bbr->r_ctl.rc_agg_early = 0;
13075 bbr->r_ctl.rc_last_delay_val = 0;
13076 } else if (bbr->rc_use_google == 0)
13077 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0);
13078 /* Are we app limited? */
13079 if ((app_limited == BBR_JR_APP_LIMITED) ||
13080 (app_limited == BBR_JR_RWND_LIMITED)) {
13082 * We are application limited.
13084 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
13085 bbr->r_ctl.rc_lost_bytes)) + bbr->r_ctl.rc_delivered);
13088 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_JUSTRET], 1);
13089 tp->t_flags &= ~TF_FORCEDATA;
13090 /* Dont update the time if we did not send */
13091 bbr->r_ctl.rc_last_delay_val = 0;
13092 bbr->rc_output_starts_timer = 1;
13093 bbr_start_hpts_timer(bbr, tp, cts, 9, slot, tot_len);
13094 bbr_log_type_just_return(bbr, cts, tot_len, hpts_calling, app_limited, p_maxseg, len);
13095 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
13096 /* Make sure snd_nxt is drug up */
13097 tp->snd_nxt = tp->snd_max;
13102 if (doing_tlp == 0) {
13104 * Data not a TLP, and its not the rxt firing. If it is the
13105 * rxt firing, we want to leave the tlp_in_progress flag on
13106 * so we don't send another TLP. It has to be a rack timer
13107 * or normal send (response to acked data) to clear the tlp
13108 * in progress flag.
13110 bbr->rc_tlp_in_progress = 0;
13111 bbr->rc_tlp_rtx_out = 0;
13116 bbr->rc_tlp_in_progress = 1;
13118 bbr_timer_cancel(bbr, __LINE__, cts);
13120 if (sbused(sb) > 0) {
13122 * This is sub-optimal. We only send a stand alone
13123 * FIN on its own segment.
13125 if (flags & TH_FIN) {
13127 if ((len == 0) && ((tp->t_flags & TF_ACKNOW) == 0)) {
13128 /* Lets not send this */
13136 * We do *not* send a FIN on a retransmit if it has data.
13137 * The if clause here where len > 1 should never come true.
13140 (((rsm->r_flags & BBR_HAS_FIN) == 0) &&
13141 (flags & TH_FIN))) {
13146 SOCKBUF_LOCK_ASSERT(sb);
13148 if ((tp->snd_una == tp->snd_max) &&
13149 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) {
13151 * This qualifies as a RTT_PROBE session since we
13152 * drop the data outstanding to nothing and waited
13153 * more than bbr_rtt_probe_time.
13155 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0);
13156 bbr_set_reduced_rtt(bbr, cts, __LINE__);
13159 tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT;
13161 tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT;
13164 * Before ESTABLISHED, force sending of initial options unless TCP
13165 * set not to do any options. NOTE: we assume that the IP/TCP header
13166 * plus TCP options always fit in a single mbuf, leaving room for a
13167 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr)
13168 * + optlen <= MCLBYTES
13173 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
13176 hdrlen = sizeof(struct tcpiphdr);
13179 * Compute options for segment. We only have to care about SYN and
13180 * established connection segments. Options for SYN-ACK segments
13181 * are handled in TCP syncache.
13185 if ((tp->t_flags & TF_NOOPT) == 0) {
13186 /* Maximum segment size. */
13187 if (flags & TH_SYN) {
13188 to.to_mss = tcp_mssopt(&inp->inp_inc);
13189 #ifdef NETFLIX_TCPOUDP
13191 to.to_mss -= V_tcp_udp_tunneling_overhead;
13193 to.to_flags |= TOF_MSS;
13195 * On SYN or SYN|ACK transmits on TFO connections,
13196 * only include the TFO option if it is not a
13197 * retransmit, as the presence of the TFO option may
13198 * have caused the original SYN or SYN|ACK to have
13199 * been dropped by a middlebox.
13201 if (IS_FASTOPEN(tp->t_flags) &&
13202 (tp->t_rxtshift == 0)) {
13203 if (tp->t_state == TCPS_SYN_RECEIVED) {
13204 to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN;
13206 (u_int8_t *)&tp->t_tfo_cookie.server;
13207 to.to_flags |= TOF_FASTOPEN;
13209 } else if (tp->t_state == TCPS_SYN_SENT) {
13211 tp->t_tfo_client_cookie_len;
13213 tp->t_tfo_cookie.client;
13214 to.to_flags |= TOF_FASTOPEN;
13219 /* Window scaling. */
13220 if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
13221 to.to_wscale = tp->request_r_scale;
13222 to.to_flags |= TOF_SCALE;
13225 if ((tp->t_flags & TF_RCVD_TSTMP) ||
13226 ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
13227 to.to_tsval = tcp_tv_to_mssectick(&bbr->rc_tv) + tp->ts_offset;
13228 to.to_tsecr = tp->ts_recent;
13229 to.to_flags |= TOF_TS;
13230 local_options += TCPOLEN_TIMESTAMP + 2;
13232 /* Set receive buffer autosizing timestamp. */
13233 if (tp->rfbuf_ts == 0 &&
13234 (so->so_rcv.sb_flags & SB_AUTOSIZE))
13235 tp->rfbuf_ts = tcp_tv_to_mssectick(&bbr->rc_tv);
13236 /* Selective ACK's. */
13237 if (flags & TH_SYN)
13238 to.to_flags |= TOF_SACKPERM;
13239 else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
13240 tp->rcv_numsacks > 0) {
13241 to.to_flags |= TOF_SACK;
13242 to.to_nsacks = tp->rcv_numsacks;
13243 to.to_sacks = (u_char *)tp->sackblks;
13245 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
13246 /* TCP-MD5 (RFC2385). */
13247 if (tp->t_flags & TF_SIGNATURE)
13248 to.to_flags |= TOF_SIGNATURE;
13249 #endif /* TCP_SIGNATURE */
13251 /* Processing the options. */
13252 hdrlen += (optlen = tcp_addoptions(&to, opt));
13254 * If we wanted a TFO option to be added, but it was unable
13255 * to fit, ensure no data is sent.
13257 if (IS_FASTOPEN(tp->t_flags) && wanted_cookie &&
13258 !(to.to_flags & TOF_FASTOPEN))
13261 #ifdef NETFLIX_TCPOUDP
13263 if (V_tcp_udp_tunneling_port == 0) {
13264 /* The port was removed?? */
13265 SOCKBUF_UNLOCK(&so->so_snd);
13266 return (EHOSTUNREACH);
13268 hdrlen += sizeof(struct udphdr);
13273 ipoptlen = ip6_optlen(tp->t_inpcb);
13276 if (tp->t_inpcb->inp_options)
13277 ipoptlen = tp->t_inpcb->inp_options->m_len -
13278 offsetof(struct ipoption, ipopt_list);
13282 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
13283 ipoptlen += ipsec_optlen;
13285 if (bbr->rc_last_options != local_options) {
13287 * Cache the options length this generally does not change
13288 * on a connection. We use this to calculate TSO.
13290 bbr->rc_last_options = local_options;
13292 maxseg = tp->t_maxseg - (ipoptlen + optlen);
13293 p_maxseg = min(maxseg, pace_max_segs);
13295 * Adjust data length if insertion of options will bump the packet
13296 * length beyond the t_maxseg length. Clear the FIN bit because we
13297 * cut off the tail of the segment.
13300 /* force TSO for so TLS offload can get mss */
13301 if (sb->sb_flags & SB_TLS_IFNET) {
13306 if (len > maxseg) {
13307 if (len != 0 && (flags & TH_FIN)) {
13314 /* extract TSO information */
13315 if_hw_tsomax = tp->t_tsomax;
13316 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
13317 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
13318 KASSERT(ipoptlen == 0,
13319 ("%s: TSO can't do IP options", __func__));
13322 * Check if we should limit by maximum payload
13325 if (if_hw_tsomax != 0) {
13326 /* compute maximum TSO length */
13327 max_len = (if_hw_tsomax - hdrlen -
13329 if (max_len <= 0) {
13331 } else if (len > max_len) {
13336 * Prevent the last segment from being fractional
13337 * unless the send sockbuf can be emptied:
13339 if (((sb_offset + len) < sbavail(sb)) &&
13341 moff = len % (uint32_t)maxseg;
13347 * In case there are too many small fragments don't
13350 if (len <= maxseg) {
13355 /* Not doing TSO */
13356 if (optlen + ipoptlen >= tp->t_maxseg) {
13358 * Since we don't have enough space to put
13359 * the IP header chain and the TCP header in
13360 * one packet as required by RFC 7112, don't
13361 * send it. Also ensure that at least one
13362 * byte of the payload can be put into the
13365 SOCKBUF_UNLOCK(&so->so_snd);
13373 /* Not doing TSO */
13374 if_hw_tsomaxsegcount = 0;
13377 KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET,
13378 ("%s: len > IP_MAXPACKET", __func__));
13381 if (max_linkhdr + hdrlen > MCLBYTES)
13383 if (max_linkhdr + hdrlen > MHLEN)
13385 panic("tcphdr too big");
13388 * This KASSERT is here to catch edge cases at a well defined place.
13389 * Before, those had triggered (random) panic conditions further
13392 #ifdef BBR_INVARIANTS
13394 if (SEQ_LT(rsm->r_start, tp->snd_una)) {
13395 panic("RSM:%p TP:%p bbr:%p start:%u is < snd_una:%u",
13396 rsm, tp, bbr, rsm->r_start, tp->snd_una);
13400 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
13402 (flags & TH_FIN) &&
13405 * We have outstanding data, don't send a fin by itself!.
13411 * Grab a header mbuf, attaching a copy of data to be transmitted,
13412 * and initialize the header from the template for sends on this
13420 * We place a limit on sending with hptsi.
13422 if ((rsm == NULL) && len > pace_max_segs)
13423 len = pace_max_segs;
13427 if (MHLEN < hdrlen + max_linkhdr)
13428 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
13431 m = m_gethdr(M_NOWAIT, MT_DATA);
13434 BBR_STAT_INC(bbr_failed_mbuf_aloc);
13435 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0);
13436 SOCKBUF_UNLOCK(sb);
13441 m->m_data += max_linkhdr;
13444 * Start the m_copy functions from the closest mbuf to the
13445 * sb_offset in the socket buffer chain.
13447 if ((sb_offset > sbavail(sb)) || ((len + sb_offset) > sbavail(sb))) {
13448 #ifdef BBR_INVARIANTS
13449 if ((len + sb_offset) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0)))
13450 panic("tp:%p bbr:%p len:%u sb_offset:%u sbavail:%u rsm:%p %u:%u:%u",
13451 tp, bbr, len, sb_offset, sbavail(sb), rsm,
13458 * In this messed up situation we have two choices,
13459 * a) pretend the send worked, and just start timers
13460 * and what not (not good since that may lead us
13461 * back here a lot). <or> b) Send the lowest segment
13462 * in the map. <or> c) Drop the connection. Lets do
13463 * <b> which if it continues to happen will lead to
13464 * <c> via timeouts.
13466 BBR_STAT_INC(bbr_offset_recovery);
13467 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
13474 if (rsm->r_start != tp->snd_una) {
13476 * Things are really messed up, <c>
13477 * is the only thing to do.
13479 BBR_STAT_INC(bbr_offset_drop);
13480 tcp_set_inp_to_drop(inp, EFAULT);
13483 len = rsm->r_end - rsm->r_start;
13485 if (len > sbavail(sb))
13490 mb = sbsndptr_noadv(sb, sb_offset, &moff);
13491 if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) {
13492 m_copydata(mb, moff, (int)len,
13493 mtod(m, caddr_t)+hdrlen);
13495 sbsndptr_adv(sb, mb, len);
13498 struct sockbuf *msb;
13504 #ifdef BBR_INVARIANTS
13505 if ((len + moff) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0))) {
13507 panic("tp:%p bbr:%p len:%u moff:%u sbavail:%u rsm:%p snd_una:%u rsm_start:%u flg:%x %u:%u:%u sr:%d ",
13508 tp, bbr, len, moff,
13510 tp->snd_una, rsm->r_flags, rsm->r_start,
13513 doing_tlp, sack_rxmit);
13515 panic("tp:%p bbr:%p len:%u moff:%u sbavail:%u sb_offset:%u snd_una:%u",
13516 tp, bbr, len, moff, sbavail(sb), sb_offset, tp->snd_una);
13521 m->m_next = tcp_m_copym(
13522 #ifdef NETFLIX_COPY_ARGS
13526 if_hw_tsomaxsegcount,
13527 if_hw_tsomaxsegsize, msb,
13528 ((rsm == NULL) ? hw_tls : 0)
13529 #ifdef NETFLIX_COPY_ARGS
13533 if (len <= maxseg && !force_tso) {
13535 * Must have ran out of mbufs for the copy
13536 * shorten it to no longer need tso. Lets
13537 * not put on sendalot since we are low on
13542 if (m->m_next == NULL) {
13543 SOCKBUF_UNLOCK(sb);
13550 #ifdef BBR_INVARIANTS
13551 if (tso && len < maxseg) {
13552 panic("tp:%p tso on, but len:%d < maxseg:%d",
13555 if (tso && if_hw_tsomaxsegcount) {
13556 int32_t seg_cnt = 0;
13564 if (seg_cnt > if_hw_tsomaxsegcount) {
13565 panic("seg_cnt:%d > max:%d", seg_cnt, if_hw_tsomaxsegcount);
13570 * If we're sending everything we've got, set PUSH. (This
13571 * will keep happy those implementations which only give
13572 * data to the user when a buffer fills or a PUSH comes in.)
13574 if (sb_offset + len == sbused(sb) &&
13576 !(flags & TH_SYN)) {
13579 SOCKBUF_UNLOCK(sb);
13581 SOCKBUF_UNLOCK(sb);
13582 if (tp->t_flags & TF_ACKNOW)
13583 KMOD_TCPSTAT_INC(tcps_sndacks);
13584 else if (flags & (TH_SYN | TH_FIN | TH_RST))
13585 KMOD_TCPSTAT_INC(tcps_sndctrl);
13586 else if (SEQ_GT(tp->snd_up, tp->snd_una))
13587 KMOD_TCPSTAT_INC(tcps_sndurg);
13589 KMOD_TCPSTAT_INC(tcps_sndwinup);
13591 m = m_gethdr(M_NOWAIT, MT_DATA);
13593 BBR_STAT_INC(bbr_failed_mbuf_aloc);
13594 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0);
13596 /* Fudge the send time since we could not send */
13601 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
13603 M_ALIGN(m, hdrlen);
13606 m->m_data += max_linkhdr;
13609 SOCKBUF_UNLOCK_ASSERT(sb);
13610 m->m_pkthdr.rcvif = (struct ifnet *)0;
13612 mac_inpcb_create_mbuf(inp, m);
13616 ip6 = mtod(m, struct ip6_hdr *);
13617 #ifdef NETFLIX_TCPOUDP
13619 udp = (struct udphdr *)((caddr_t)ip6 + ipoptlen + sizeof(struct ip6_hdr));
13620 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
13621 udp->uh_dport = tp->t_port;
13622 ulen = hdrlen + len - sizeof(struct ip6_hdr);
13623 udp->uh_ulen = htons(ulen);
13624 th = (struct tcphdr *)(udp + 1);
13627 th = (struct tcphdr *)(ip6 + 1);
13629 #ifdef NETFLIX_TCPOUDP
13632 tcpip_fillheaders(inp,
13633 #ifdef NETFLIX_TCPOUDP
13640 ip = mtod(m, struct ip *);
13642 ipov = (struct ipovly *)ip;
13644 #ifdef NETFLIX_TCPOUDP
13646 udp = (struct udphdr *)((caddr_t)ip + ipoptlen + sizeof(struct ip));
13647 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
13648 udp->uh_dport = tp->t_port;
13649 ulen = hdrlen + len - sizeof(struct ip);
13650 udp->uh_ulen = htons(ulen);
13651 th = (struct tcphdr *)(udp + 1);
13654 th = (struct tcphdr *)(ip + 1);
13655 tcpip_fillheaders(inp,
13656 #ifdef NETFLIX_TCPOUDP
13662 * If we are doing retransmissions, then snd_nxt will not reflect
13663 * the first unsent octet. For ACK only packets, we do not want the
13664 * sequence number of the retransmitted packet, we want the sequence
13665 * number of the next unsent octet. So, if there is no data (and no
13666 * SYN or FIN), use snd_max instead of snd_nxt when filling in
13667 * ti_seq. But if we are in persist state, snd_max might reflect
13668 * one byte beyond the right edge of the window, so use snd_nxt in
13669 * that case, since we know we aren't doing a retransmission.
13670 * (retransmit and persist are mutually exclusive...)
13672 if (sack_rxmit == 0) {
13673 if (len && ((flags & (TH_FIN | TH_SYN | TH_RST)) == 0)) {
13674 /* New data (including new persists) */
13675 th->th_seq = htonl(tp->snd_max);
13676 bbr_seq = tp->snd_max;
13677 } else if (flags & TH_SYN) {
13678 /* Syn's always send from iss */
13679 th->th_seq = htonl(tp->iss);
13681 } else if (flags & TH_FIN) {
13682 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN) {
13684 * If we sent the fin already its 1 minus
13687 th->th_seq = (htonl(tp->snd_max - 1));
13688 bbr_seq = (tp->snd_max - 1);
13690 /* First time FIN use snd_max */
13691 th->th_seq = htonl(tp->snd_max);
13692 bbr_seq = tp->snd_max;
13694 } else if (flags & TH_RST) {
13696 * For a Reset send the last cum ack in sequence
13697 * (this like any other choice may still generate a
13698 * challenge ack, if a ack-update packet is in
13701 th->th_seq = htonl(tp->snd_una);
13702 bbr_seq = tp->snd_una;
13705 * len == 0 and not persist we use snd_max, sending
13706 * an ack unless we have sent the fin then its 1
13710 * XXXRRS Question if we are in persists and we have
13711 * nothing outstanding to send and we have not sent
13712 * a FIN, we will send an ACK. In such a case it
13713 * might be better to send (tp->snd_una - 1) which
13714 * would force the peer to ack.
13716 if (tp->t_flags & TF_SENTFIN) {
13717 th->th_seq = htonl(tp->snd_max - 1);
13718 bbr_seq = (tp->snd_max - 1);
13720 th->th_seq = htonl(tp->snd_max);
13721 bbr_seq = tp->snd_max;
13725 /* All retransmits use the rsm to guide the send */
13726 th->th_seq = htonl(rsm->r_start);
13727 bbr_seq = rsm->r_start;
13729 th->th_ack = htonl(tp->rcv_nxt);
13731 bcopy(opt, th + 1, optlen);
13732 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
13734 th->th_flags = flags;
13736 * Calculate receive window. Don't shrink window, but avoid silly
13739 if ((flags & TH_RST) || ((recwin < (so->so_rcv.sb_hiwat / 4) &&
13742 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) &&
13743 recwin < (tp->rcv_adv - tp->rcv_nxt))
13744 recwin = (tp->rcv_adv - tp->rcv_nxt);
13745 if (recwin > TCP_MAXWIN << tp->rcv_scale)
13746 recwin = TCP_MAXWIN << tp->rcv_scale;
13749 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or
13750 * <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> case is
13751 * handled in syncache.
13753 if (flags & TH_SYN)
13754 th->th_win = htons((u_short)
13755 (min(sbspace(&so->so_rcv), TCP_MAXWIN)));
13757 th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
13759 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0
13760 * window. This may cause the remote transmitter to stall. This
13761 * flag tells soreceive() to disable delayed acknowledgements when
13762 * draining the buffer. This can occur if the receiver is
13763 * attempting to read more data than can be buffered prior to
13764 * transmitting on the connection.
13766 if (th->th_win == 0) {
13767 tp->t_sndzerowin++;
13768 tp->t_flags |= TF_RXWIN0SENT;
13770 tp->t_flags &= ~TF_RXWIN0SENT;
13771 if (SEQ_GT(tp->snd_up, tp->snd_max)) {
13772 th->th_urp = htons((u_short)(tp->snd_up - tp->snd_max));
13773 th->th_flags |= TH_URG;
13776 * If no urgent pointer to send, then we pull the urgent
13777 * pointer to the left edge of the send window so that it
13778 * doesn't drift into the send window on sequence number
13781 tp->snd_up = tp->snd_una; /* drag it along */
13783 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
13784 if (to.to_flags & TOF_SIGNATURE) {
13786 * Calculate MD5 signature and put it into the place
13787 * determined before. NOTE: since TCP options buffer doesn't
13788 * point into mbuf's data, calculate offset and use it.
13790 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
13791 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
13793 * Do not send segment if the calculation of MD5
13794 * digest has failed.
13802 * Put TCP length in extended header, and then checksum extended
13805 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
13809 * ip6_plen is not need to be filled now, and will be filled
13812 #ifdef NETFLIX_TCPOUDP
13814 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
13815 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
13816 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
13817 th->th_sum = htons(0);
13818 UDPSTAT_INC(udps_opackets);
13821 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
13822 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
13823 th->th_sum = in6_cksum_pseudo(ip6, sizeof(struct tcphdr) +
13824 optlen + len, IPPROTO_TCP, 0);
13825 #ifdef NETFLIX_TCPOUDP
13830 #if defined(INET6) && defined(INET)
13835 #ifdef NETFLIX_TCPOUDP
13837 m->m_pkthdr.csum_flags = CSUM_UDP;
13838 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
13839 udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
13840 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
13841 th->th_sum = htons(0);
13842 UDPSTAT_INC(udps_opackets);
13845 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP;
13846 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
13847 th->th_sum = in_pseudo(ip->ip_src.s_addr,
13848 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
13849 IPPROTO_TCP + len + optlen));
13850 #ifdef NETFLIX_TCPOUDP
13853 /* IP version must be set here for ipv4/ipv6 checking later */
13854 KASSERT(ip->ip_v == IPVERSION,
13855 ("%s: IP version incorrect: %d", __func__, ip->ip_v));
13860 * Enable TSO and specify the size of the segments. The TCP pseudo
13861 * header checksum is always provided. XXX: Fixme: This is currently
13862 * not the case for IPv6.
13864 if (tso || force_tso) {
13865 KASSERT(force_tso || len > maxseg,
13866 ("%s: len:%d <= tso_segsz:%d", __func__, len, maxseg));
13867 m->m_pkthdr.csum_flags |= CSUM_TSO;
13868 csum_flags |= CSUM_TSO;
13869 m->m_pkthdr.tso_segsz = maxseg;
13871 KASSERT(len + hdrlen == m_length(m, NULL),
13872 ("%s: mbuf chain different than expected: %d + %u != %u",
13873 __func__, len, hdrlen, m_length(m, NULL)));
13876 /* Run HHOOK_TC_ESTABLISHED_OUT helper hooks. */
13877 hhook_run_tcp_est_out(tp, th, &to, len, tso);
13883 if (so->so_options & SO_DEBUG) {
13890 save = ipov->ih_len;
13891 ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen +
13892 * (th->th_off << 2) */ );
13894 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
13898 ipov->ih_len = save;
13900 #endif /* TCPDEBUG */
13902 /* Log to the black box */
13903 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
13904 union tcp_log_stackspecific log;
13906 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
13907 /* Record info on type of transmission */
13908 log.u_bbr.flex1 = bbr->r_ctl.rc_hptsi_agg_delay;
13909 log.u_bbr.flex2 = (bbr->r_recovery_bw << 3);
13910 log.u_bbr.flex3 = maxseg;
13911 log.u_bbr.flex4 = delay_calc;
13912 /* Encode filled_all into the upper flex5 bit */
13913 log.u_bbr.flex5 = bbr->rc_past_init_win;
13914 log.u_bbr.flex5 <<= 1;
13915 log.u_bbr.flex5 |= bbr->rc_no_pacing;
13916 log.u_bbr.flex5 <<= 29;
13918 log.u_bbr.flex5 |= 0x80000000;
13919 log.u_bbr.flex5 |= tp->t_maxseg;
13920 log.u_bbr.flex6 = bbr->r_ctl.rc_pace_max_segs;
13921 log.u_bbr.flex7 = (bbr->rc_bbr_state << 8) | bbr_state_val(bbr);
13922 /* lets poke in the low and the high here for debugging */
13923 log.u_bbr.pkts_out = bbr->rc_tp->t_maxseg;
13924 if (rsm || sack_rxmit) {
13926 log.u_bbr.flex8 = 2;
13928 log.u_bbr.flex8 = 1;
13930 log.u_bbr.flex8 = 0;
13932 lgb = tcp_log_event_(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK,
13933 len, &log, false, NULL, NULL, 0, tv);
13938 * Fill in IP length and desired time to live and send to IP level.
13939 * There should be a better way to handle ttl and tos; we could keep
13940 * them in the template, but need a way to checksum without them.
13943 * m->m_pkthdr.len should have been set before cksum calcuration,
13944 * because in6_cksum() need it.
13949 * we separately set hoplimit for every segment, since the
13950 * user might want to change the value via setsockopt. Also,
13951 * desired default hop limit might be changed via Neighbor
13954 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
13957 * Set the packet size here for the benefit of DTrace
13958 * probes. ip6_output() will set it properly; it's supposed
13959 * to include the option header lengths as well.
13961 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
13963 if (V_path_mtu_discovery && maxseg > V_tcp_minmss)
13964 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
13966 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
13968 if (tp->t_state == TCPS_SYN_SENT)
13969 TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th);
13971 TCP_PROBE5(send, NULL, tp, ip6, tp, th);
13972 /* TODO: IPv6 IP6TOS_ECT bit on */
13973 error = ip6_output(m, inp->in6p_outputopts,
13975 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0),
13978 if (error == EMSGSIZE && inp->inp_route6.ro_rt != NULL)
13979 mtu = inp->inp_route6.ro_rt->rt_mtu;
13982 #if defined(INET) && defined(INET6)
13987 ip->ip_len = htons(m->m_pkthdr.len);
13990 ip->ip_ttl = in6_selecthlim(inp, NULL);
13993 * If we do path MTU discovery, then we set DF on every
13994 * packet. This might not be the best thing to do according
13995 * to RFC3390 Section 2. However the tcp hostcache migitates
13996 * the problem so it affects only the first tcp connection
13999 * NB: Don't set DF on small MTU/MSS to have a safe
14002 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
14003 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
14004 if (tp->t_port == 0 || len < V_tcp_minmss) {
14005 ip->ip_off |= htons(IP_DF);
14008 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
14011 if (tp->t_state == TCPS_SYN_SENT)
14012 TCP_PROBE5(connect__request, NULL, tp, ip, tp, th);
14014 TCP_PROBE5(send, NULL, tp, ip, tp, th);
14016 error = ip_output(m, inp->inp_options, &inp->inp_route,
14017 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0,
14019 if (error == EMSGSIZE && inp->inp_route.ro_rt != NULL)
14020 mtu = inp->inp_route.ro_rt->rt_mtu;
14026 lgb->tlb_errno = error;
14030 * In transmit state, time the transmission and arrange for the
14031 * retransmit. In persist state, just set snd_max.
14034 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
14035 (tp->t_flags & TF_SACK_PERMIT) &&
14036 tp->rcv_numsacks > 0)
14037 tcp_clean_dsack_blocks(tp);
14038 /* We sent an ack clear the bbr_segs_rcvd count */
14039 bbr->output_error_seen = 0;
14040 bbr->oerror_cnt = 0;
14041 bbr->bbr_segs_rcvd = 0;
14043 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_SNDACK], 1);
14046 (len >= bbr->r_ctl.rc_pace_max_segs))
14047 BBR_STAT_INC(bbr_meets_tso_thresh);
14050 BBR_STAT_INC(bbr_miss_tlp);
14051 bbr_log_type_hrdwtso(tp, bbr, len, 1, what_we_can);
14055 BBR_STAT_INC(bbr_miss_retran);
14056 bbr_log_type_hrdwtso(tp, bbr, len, 2, what_we_can);
14057 } else if ((ctf_outstanding(tp) + bbr->r_ctl.rc_pace_max_segs) > sbavail(sb)) {
14058 BBR_STAT_INC(bbr_miss_tso_app);
14059 bbr_log_type_hrdwtso(tp, bbr, len, 3, what_we_can);
14060 } else if ((ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
14061 bbr->r_ctl.rc_lost_bytes)) + bbr->r_ctl.rc_pace_max_segs) > tp->snd_cwnd) {
14062 BBR_STAT_INC(bbr_miss_tso_cwnd);
14063 bbr_log_type_hrdwtso(tp, bbr, len, 4, what_we_can);
14064 } else if ((ctf_outstanding(tp) + bbr->r_ctl.rc_pace_max_segs) > tp->snd_wnd) {
14065 BBR_STAT_INC(bbr_miss_tso_rwnd);
14066 bbr_log_type_hrdwtso(tp, bbr, len, 5, what_we_can);
14068 BBR_STAT_INC(bbr_miss_unknown);
14069 bbr_log_type_hrdwtso(tp, bbr, len, 6, what_we_can);
14073 /* Do accounting for new sends */
14074 if ((len > 0) && (rsm == NULL)) {
14076 if (tp->snd_una == tp->snd_max) {
14078 * Special case to match google, when
14079 * nothing is in flight the delivered
14080 * time does get updated to the current
14081 * time (see tcp_rate_bsd.c).
14083 bbr->r_ctl.rc_del_time = cts;
14085 if (len >= maxseg) {
14086 idx = (len / maxseg) + 3;
14087 if (idx >= TCP_MSS_ACCT_ATIMER)
14088 counter_u64_add(bbr_out_size[(TCP_MSS_ACCT_ATIMER - 1)], 1);
14090 counter_u64_add(bbr_out_size[idx], 1);
14092 /* smaller than a MSS */
14093 idx = len / (bbr_hptsi_bytes_min - bbr->rc_last_options);
14094 if (idx >= TCP_MSS_SMALL_MAX_SIZE_DIV)
14095 idx = (TCP_MSS_SMALL_MAX_SIZE_DIV - 1);
14096 counter_u64_add(bbr_out_size[(idx + TCP_MSS_SMALL_SIZE_OFF)], 1);
14102 * We must do the send accounting before we log the output,
14103 * otherwise the state of the rsm could change and we account to the
14107 bbr_do_send_accounting(tp, bbr, rsm, len, error);
14109 if (tp->snd_una == tp->snd_max)
14110 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
14113 bbr_log_output(bbr, tp, &to, len, bbr_seq, (uint8_t) flags, error,
14114 cts, mb, &abandon, rsm, 0, sb);
14117 * If bbr_log_output destroys the TCB or sees a TH_RST being
14118 * sent we should hit this condition.
14122 if (((tp->t_flags & TF_FORCEDATA) == 0) ||
14123 (bbr->rc_in_persist == 0)) {
14125 * Advance snd_nxt over sequence space of this segment.
14128 /* We don't log or do anything with errors */
14131 if (tp->snd_una == tp->snd_max &&
14132 (len || (flags & (TH_SYN | TH_FIN)))) {
14134 * Update the time we just added data since none was
14137 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__);
14138 bbr->rc_tp->t_acktime = ticks;
14140 if (flags & (TH_SYN | TH_FIN) && (rsm == NULL)) {
14141 if (flags & TH_SYN) {
14144 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) {
14146 tp->t_flags |= TF_SENTFIN;
14149 if (sack_rxmit == 0)
14150 tp->snd_max += len;
14152 if ((error == 0) && len)
14155 /* Persists case */
14156 int32_t xlen = len;
14161 if (flags & TH_SYN)
14163 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) {
14165 tp->t_flags |= TF_SENTFIN;
14167 if (xlen && (tp->snd_una == tp->snd_max)) {
14169 * Update the time we just added data since none was
14172 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__);
14173 bbr->rc_tp->t_acktime = ticks;
14175 if (sack_rxmit == 0)
14176 tp->snd_max += xlen;
14177 tot_len += (len + optlen + ipoptlen);
14182 * Failures do not advance the seq counter above. For the
14183 * case of ENOBUFS we will fall out and become ack-clocked.
14184 * capping the cwnd at the current flight.
14185 * Everything else will just have to retransmit with the timer
14188 SOCKBUF_UNLOCK_ASSERT(sb);
14189 BBR_STAT_INC(bbr_saw_oerr);
14190 /* Clear all delay/early tracks */
14191 bbr->r_ctl.rc_hptsi_agg_delay = 0;
14192 bbr->r_ctl.rc_agg_early = 0;
14193 bbr->r_agg_early_set = 0;
14194 bbr->output_error_seen = 1;
14195 if (bbr->oerror_cnt < 0xf)
14197 if (bbr_max_net_error_cnt && (bbr->oerror_cnt >= bbr_max_net_error_cnt)) {
14198 /* drop the session */
14199 tcp_set_inp_to_drop(inp, ENETDOWN);
14204 * Make this guy have to get ack's to send
14205 * more but lets make sure we don't
14206 * slam him below a T-O (1MSS).
14208 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) {
14209 tp->snd_cwnd = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
14210 bbr->r_ctl.rc_lost_bytes)) - maxseg;
14211 if (tp->snd_cwnd < maxseg)
14212 tp->snd_cwnd = maxseg;
14214 slot = (bbr_error_base_paceout + 1) << bbr->oerror_cnt;
14215 BBR_STAT_INC(bbr_saw_enobuf);
14216 if (bbr->bbr_hdrw_pacing)
14217 counter_u64_add(bbr_hdwr_pacing_enobuf, 1);
14219 counter_u64_add(bbr_nohdwr_pacing_enobuf, 1);
14221 * Here even in the enobuf's case we want to do our
14222 * state update. The reason being we may have been
14223 * called by the input function. If so we have had
14230 * For some reason the interface we used initially
14231 * to send segments changed to another or lowered
14232 * its MTU. If TSO was active we either got an
14233 * interface without TSO capabilits or TSO was
14234 * turned off. If we obtained mtu from ip_output()
14235 * then update it and try again.
14237 /* Turn on tracing (or try to) */
14241 old_maxseg = tp->t_maxseg;
14242 BBR_STAT_INC(bbr_saw_emsgsiz);
14243 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, csum_flags, tso, cts);
14245 tcp_mss_update(tp, -1, mtu, NULL, NULL);
14246 if (old_maxseg <= tp->t_maxseg) {
14247 /* Huh it did not shrink? */
14248 tp->t_maxseg = old_maxseg - 40;
14249 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, 0, tso, cts);
14251 tp->t_flags &= ~TF_FORCEDATA;
14253 * Nuke all other things that can interfere
14256 if ((tot_len + len) && (len >= tp->t_maxseg)) {
14257 slot = bbr_get_pacing_delay(bbr,
14258 bbr->r_ctl.rc_bbr_hptsi_gain,
14259 (tot_len + len), cts, 0);
14260 if (slot < bbr_error_base_paceout)
14261 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt;
14263 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt;
14264 bbr->rc_output_starts_timer = 1;
14265 bbr_start_hpts_timer(bbr, tp, cts, 10, slot,
14270 tp->t_softerror = error;
14276 if (TCPS_HAVERCVDSYN(tp->t_state)) {
14277 tp->t_softerror = error;
14281 tp->t_flags &= ~TF_FORCEDATA;
14282 slot = (bbr_error_base_paceout + 3) << bbr->oerror_cnt;
14283 bbr->rc_output_starts_timer = 1;
14284 bbr_start_hpts_timer(bbr, tp, cts, 11, slot, 0);
14288 } else if (((tp->t_flags & TF_GPUTINPROG) == 0) &&
14291 (bbr->rc_in_persist == 0)) {
14292 tp->gput_seq = bbr_seq;
14293 tp->gput_ack = bbr_seq +
14294 min(sbavail(&so->so_snd) - sb_offset, sendwin);
14296 tp->t_flags |= TF_GPUTINPROG;
14299 KMOD_TCPSTAT_INC(tcps_sndtotal);
14300 if ((bbr->bbr_hdw_pace_ena) &&
14301 (bbr->bbr_attempt_hdwr_pace == 0) &&
14302 (bbr->rc_past_init_win) &&
14303 (bbr->rc_bbr_state != BBR_STATE_STARTUP) &&
14304 (get_filter_value(&bbr->r_ctl.rc_delrate)) &&
14305 (inp->inp_route.ro_rt &&
14306 inp->inp_route.ro_rt->rt_ifp)) {
14308 * We are past the initial window and
14309 * have at least one measurement so we
14310 * could use hardware pacing if its available.
14311 * We have an interface and we have not attempted
14312 * to setup hardware pacing, lets try to now.
14314 uint64_t rate_wanted;
14317 rate_wanted = bbr_get_hardware_rate(bbr);
14318 bbr->bbr_attempt_hdwr_pace = 1;
14319 bbr->r_ctl.crte = tcp_set_pacing_rate(bbr->rc_tp,
14320 inp->inp_route.ro_rt->rt_ifp,
14322 (RS_PACING_GEQ|RS_PACING_SUB_OK),
14324 if (bbr->r_ctl.crte) {
14325 bbr_type_log_hdwr_pacing(bbr,
14326 bbr->r_ctl.crte->ptbl->rs_ifp,
14328 bbr->r_ctl.crte->rate,
14329 __LINE__, cts, err);
14330 BBR_STAT_INC(bbr_hdwr_rl_add_ok);
14331 counter_u64_add(bbr_flows_nohdwr_pacing, -1);
14332 counter_u64_add(bbr_flows_whdwr_pacing, 1);
14333 bbr->bbr_hdrw_pacing = 1;
14334 /* Now what is our gain status? */
14335 if (bbr->r_ctl.crte->rate < rate_wanted) {
14336 /* We have a problem */
14337 bbr_setup_less_of_rate(bbr, cts,
14338 bbr->r_ctl.crte->rate, rate_wanted);
14341 bbr->gain_is_limited = 0;
14342 bbr->skip_gain = 0;
14344 tcp_bbr_tso_size_check(bbr, cts);
14346 bbr_type_log_hdwr_pacing(bbr,
14347 inp->inp_route.ro_rt->rt_ifp,
14350 __LINE__, cts, err);
14351 BBR_STAT_INC(bbr_hdwr_rl_add_fail);
14354 if (bbr->bbr_hdrw_pacing) {
14356 * Worry about cases where the route
14357 * changes or something happened that we
14358 * lost our hardware pacing possibly during
14359 * the last ip_output call.
14361 if (inp->inp_snd_tag == NULL) {
14362 /* A change during ip output disabled hw pacing? */
14363 bbr->bbr_hdrw_pacing = 0;
14364 } else if ((inp->inp_route.ro_rt == NULL) ||
14365 (inp->inp_route.ro_rt->rt_ifp != inp->inp_snd_tag->ifp)) {
14367 * We had an interface or route change,
14368 * detach from the current hdwr pacing
14369 * and setup to re-attempt next go
14372 bbr->bbr_hdrw_pacing = 0;
14373 bbr->bbr_attempt_hdwr_pace = 0;
14374 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp);
14375 tcp_bbr_tso_size_check(bbr, cts);
14379 * Data sent (as far as we can tell). If this advertises a larger
14380 * window than any other segment, then remember the size of the
14381 * advertised window. Any pending ACK has now been sent.
14383 if (SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
14384 tp->rcv_adv = tp->rcv_nxt + recwin;
14386 tp->last_ack_sent = tp->rcv_nxt;
14387 if ((error == 0) &&
14388 (bbr->r_ctl.rc_pace_max_segs > tp->t_maxseg) &&
14389 (doing_tlp == 0) &&
14393 ((flags & TH_RST) == 0) &&
14394 (IN_RECOVERY(tp->t_flags) == 0) &&
14395 (bbr->rc_in_persist == 0) &&
14396 ((tp->t_flags & TF_FORCEDATA) == 0) &&
14397 (tot_len < bbr->r_ctl.rc_pace_max_segs)) {
14399 * For non-tso we need to goto again until we have sent out
14400 * enough data to match what we are hptsi out every hptsi
14403 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
14404 /* Make sure snd_nxt is drug up */
14405 tp->snd_nxt = tp->snd_max;
14413 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK | TF_FORCEDATA);
14417 if (((flags & (TH_RST | TH_SYN | TH_FIN)) == 0) && tot_len) {
14419 * Calculate/Re-Calculate the hptsi slot in usecs based on
14420 * what we have sent so far
14422 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0);
14423 if (bbr->rc_no_pacing)
14426 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK | TF_FORCEDATA);
14428 if (bbr->rc_use_google == 0)
14429 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0);
14430 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
14431 bbr->r_ctl.rc_lost_bytes)));
14432 bbr->rc_output_starts_timer = 1;
14433 if (bbr->bbr_use_rack_cheat &&
14435 ((bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts)) != NULL))) {
14436 /* Rack cheats and shotguns out all rxt's 1ms apart */
14440 if (bbr->bbr_hdrw_pacing && (bbr->hw_pacing_set == 0)) {
14442 * We don't change the tso size until some number of sends
14443 * to give the hardware commands time to get down
14444 * to the interface.
14446 bbr->r_ctl.bbr_hdwr_cnt_noset_snt++;
14447 if (bbr->r_ctl.bbr_hdwr_cnt_noset_snt >= bbr_hdwr_pacing_delay_cnt) {
14448 bbr->hw_pacing_set = 1;
14449 tcp_bbr_tso_size_check(bbr, cts);
14452 bbr_start_hpts_timer(bbr, tp, cts, 12, slot, tot_len);
14453 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
14454 /* Make sure snd_nxt is drug up */
14455 tp->snd_nxt = tp->snd_max;
14462 * See bbr_output_wtime() for return values.
14465 bbr_output(struct tcpcb *tp)
14469 struct tcp_bbr *bbr;
14471 NET_EPOCH_ASSERT();
14473 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
14474 INP_WLOCK_ASSERT(tp->t_inpcb);
14475 (void)tcp_get_usecs(&tv);
14476 ret = bbr_output_wtime(tp, &tv);
14481 bbr_mtu_chg(struct tcpcb *tp)
14483 struct tcp_bbr *bbr;
14484 struct bbr_sendmap *rsm, *frsm = NULL;
14488 * The MTU has changed. a) Clear the sack filter. b) Mark everything
14489 * over the current size as SACK_PASS so a retransmit will occur.
14492 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
14493 maxseg = tp->t_maxseg - bbr->rc_last_options;
14494 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
14495 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
14496 /* Don't mess with ones acked (by sack?) */
14497 if (rsm->r_flags & BBR_ACKED)
14499 if ((rsm->r_end - rsm->r_start) > maxseg) {
14501 * We mark sack-passed on all the previous large
14502 * sends we did. This will force them to retransmit.
14504 rsm->r_flags |= BBR_SACK_PASSED;
14505 if (((rsm->r_flags & BBR_MARKED_LOST) == 0) &&
14506 bbr_is_lost(bbr, rsm, bbr->r_ctl.rc_rcvtime)) {
14507 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start;
14508 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start;
14509 rsm->r_flags |= BBR_MARKED_LOST;
14516 bbr->r_ctl.rc_resend = frsm;
14521 * bbr_ctloutput() must drop the inpcb lock before performing copyin on
14522 * socket option arguments. When it re-acquires the lock after the copy, it
14523 * has to revalidate that the connection is still valid for the socket
14527 bbr_set_sockopt(struct socket *so, struct sockopt *sopt,
14528 struct inpcb *inp, struct tcpcb *tp, struct tcp_bbr *bbr)
14530 int32_t error = 0, optval;
14532 switch (sopt->sopt_name) {
14533 case TCP_RACK_PACE_MAX_SEG:
14534 case TCP_RACK_MIN_TO:
14535 case TCP_RACK_REORD_THRESH:
14536 case TCP_RACK_REORD_FADE:
14537 case TCP_RACK_TLP_THRESH:
14538 case TCP_RACK_PKT_DELAY:
14539 case TCP_BBR_ALGORITHM:
14540 case TCP_BBR_TSLIMITS:
14541 case TCP_BBR_IWINTSO:
14542 case TCP_BBR_RECFORCE:
14543 case TCP_BBR_STARTUP_PG:
14544 case TCP_BBR_DRAIN_PG:
14545 case TCP_BBR_RWND_IS_APP:
14546 case TCP_BBR_PROBE_RTT_INT:
14547 case TCP_BBR_PROBE_RTT_GAIN:
14548 case TCP_BBR_PROBE_RTT_LEN:
14549 case TCP_BBR_STARTUP_LOSS_EXIT:
14550 case TCP_BBR_USEDEL_RATE:
14551 case TCP_BBR_MIN_RTO:
14552 case TCP_BBR_MAX_RTO:
14553 case TCP_BBR_PACE_PER_SEC:
14555 case TCP_BBR_PACE_DEL_TAR:
14556 case TCP_BBR_SEND_IWND_IN_TSO:
14557 case TCP_BBR_EXTRA_STATE:
14558 case TCP_BBR_UTTER_MAX_TSO:
14559 case TCP_BBR_MIN_TOPACEOUT:
14560 case TCP_BBR_FLOOR_MIN_TSO:
14561 case TCP_BBR_TSTMP_RAISES:
14562 case TCP_BBR_POLICER_DETECT:
14563 case TCP_BBR_USE_RACK_CHEAT:
14564 case TCP_DATA_AFTER_CLOSE:
14565 case TCP_BBR_HDWR_PACE:
14566 case TCP_BBR_PACE_SEG_MAX:
14567 case TCP_BBR_PACE_SEG_MIN:
14568 case TCP_BBR_PACE_CROSS:
14569 case TCP_BBR_PACE_OH:
14570 #ifdef NETFLIX_PEAKRATE
14571 case TCP_MAXPEAKRATE:
14573 case TCP_BBR_TMR_PACE_OH:
14574 case TCP_BBR_RACK_RTT_USE:
14575 case TCP_BBR_RETRAN_WTSO:
14578 return (tcp_default_ctloutput(so, sopt, inp, tp));
14582 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
14586 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
14588 return (ECONNRESET);
14590 tp = intotcpcb(inp);
14591 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
14592 switch (sopt->sopt_name) {
14593 case TCP_BBR_PACE_PER_SEC:
14594 BBR_OPTS_INC(tcp_bbr_pace_per_sec);
14595 bbr->r_ctl.bbr_hptsi_per_second = optval;
14597 case TCP_BBR_PACE_DEL_TAR:
14598 BBR_OPTS_INC(tcp_bbr_pace_del_tar);
14599 bbr->r_ctl.bbr_hptsi_segments_delay_tar = optval;
14601 case TCP_BBR_PACE_SEG_MAX:
14602 BBR_OPTS_INC(tcp_bbr_pace_seg_max);
14603 bbr->r_ctl.bbr_hptsi_segments_max = optval;
14605 case TCP_BBR_PACE_SEG_MIN:
14606 BBR_OPTS_INC(tcp_bbr_pace_seg_min);
14607 bbr->r_ctl.bbr_hptsi_bytes_min = optval;
14609 case TCP_BBR_PACE_CROSS:
14610 BBR_OPTS_INC(tcp_bbr_pace_cross);
14611 bbr->r_ctl.bbr_cross_over = optval;
14613 case TCP_BBR_ALGORITHM:
14614 BBR_OPTS_INC(tcp_bbr_algorithm);
14615 if (optval && (bbr->rc_use_google == 0)) {
14616 /* Turn on the google mode */
14617 bbr_google_mode_on(bbr);
14618 if ((optval > 3) && (optval < 500)) {
14620 * Must be at least greater than .3%
14621 * and must be less than 50.0%.
14623 bbr->r_ctl.bbr_google_discount = optval;
14625 } else if ((optval == 0) && (bbr->rc_use_google == 1)) {
14626 /* Turn off the google mode */
14627 bbr_google_mode_off(bbr);
14630 case TCP_BBR_TSLIMITS:
14631 BBR_OPTS_INC(tcp_bbr_tslimits);
14633 bbr->rc_use_ts_limit = 1;
14634 else if (optval == 0)
14635 bbr->rc_use_ts_limit = 0;
14640 case TCP_BBR_IWINTSO:
14641 BBR_OPTS_INC(tcp_bbr_iwintso);
14642 if ((optval >= 0) && (optval < 128)) {
14645 bbr->rc_init_win = optval;
14646 twin = bbr_initial_cwnd(bbr, tp);
14647 if ((bbr->rc_past_init_win == 0) && (twin > tp->snd_cwnd))
14648 tp->snd_cwnd = twin;
14654 case TCP_BBR_STARTUP_PG:
14655 BBR_OPTS_INC(tcp_bbr_startup_pg);
14656 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE)) {
14657 bbr->r_ctl.rc_startup_pg = optval;
14658 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) {
14659 bbr->r_ctl.rc_bbr_hptsi_gain = optval;
14664 case TCP_BBR_DRAIN_PG:
14665 BBR_OPTS_INC(tcp_bbr_drain_pg);
14666 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE))
14667 bbr->r_ctl.rc_drain_pg = optval;
14671 case TCP_BBR_PROBE_RTT_LEN:
14672 BBR_OPTS_INC(tcp_bbr_probertt_len);
14674 reset_time_small(&bbr->r_ctl.rc_rttprop, (optval * USECS_IN_SECOND));
14678 case TCP_BBR_PROBE_RTT_GAIN:
14679 BBR_OPTS_INC(tcp_bbr_probertt_gain);
14680 if (optval <= BBR_UNIT)
14681 bbr->r_ctl.bbr_rttprobe_gain_val = optval;
14685 case TCP_BBR_PROBE_RTT_INT:
14686 BBR_OPTS_INC(tcp_bbr_probe_rtt_int);
14688 bbr->r_ctl.rc_probertt_int = optval;
14692 case TCP_BBR_MIN_TOPACEOUT:
14693 BBR_OPTS_INC(tcp_bbr_topaceout);
14695 bbr->no_pacing_until = 0;
14696 bbr->rc_no_pacing = 0;
14697 } else if (optval <= 0x00ff) {
14698 bbr->no_pacing_until = optval;
14699 if ((bbr->r_ctl.rc_pkt_epoch < bbr->no_pacing_until) &&
14700 (bbr->rc_bbr_state == BBR_STATE_STARTUP)){
14701 /* Turn on no pacing */
14702 bbr->rc_no_pacing = 1;
14707 case TCP_BBR_STARTUP_LOSS_EXIT:
14708 BBR_OPTS_INC(tcp_bbr_startup_loss_exit);
14709 bbr->rc_loss_exit = optval;
14711 case TCP_BBR_USEDEL_RATE:
14714 case TCP_BBR_MIN_RTO:
14715 BBR_OPTS_INC(tcp_bbr_min_rto);
14716 bbr->r_ctl.rc_min_rto_ms = optval;
14718 case TCP_BBR_MAX_RTO:
14719 BBR_OPTS_INC(tcp_bbr_max_rto);
14720 bbr->rc_max_rto_sec = optval;
14722 case TCP_RACK_MIN_TO:
14723 /* Minimum time between rack t-o's in ms */
14724 BBR_OPTS_INC(tcp_rack_min_to);
14725 bbr->r_ctl.rc_min_to = optval;
14727 case TCP_RACK_REORD_THRESH:
14728 /* RACK reorder threshold (shift amount) */
14729 BBR_OPTS_INC(tcp_rack_reord_thresh);
14730 if ((optval > 0) && (optval < 31))
14731 bbr->r_ctl.rc_reorder_shift = optval;
14735 case TCP_RACK_REORD_FADE:
14736 /* Does reordering fade after ms time */
14737 BBR_OPTS_INC(tcp_rack_reord_fade);
14738 bbr->r_ctl.rc_reorder_fade = optval;
14740 case TCP_RACK_TLP_THRESH:
14741 /* RACK TLP theshold i.e. srtt+(srtt/N) */
14742 BBR_OPTS_INC(tcp_rack_tlp_thresh);
14744 bbr->rc_tlp_threshold = optval;
14748 case TCP_BBR_USE_RACK_CHEAT:
14749 BBR_OPTS_INC(tcp_use_rackcheat);
14750 if (bbr->rc_use_google) {
14754 BBR_OPTS_INC(tcp_rack_cheat);
14756 bbr->bbr_use_rack_cheat = 1;
14758 bbr->bbr_use_rack_cheat = 0;
14760 case TCP_BBR_FLOOR_MIN_TSO:
14761 BBR_OPTS_INC(tcp_utter_max_tso);
14762 if ((optval >= 0) && (optval < 40))
14763 bbr->r_ctl.bbr_hptsi_segments_floor = optval;
14767 case TCP_BBR_UTTER_MAX_TSO:
14768 BBR_OPTS_INC(tcp_utter_max_tso);
14769 if ((optval >= 0) && (optval < 0xffff))
14770 bbr->r_ctl.bbr_utter_max = optval;
14775 case TCP_BBR_EXTRA_STATE:
14776 BBR_OPTS_INC(tcp_extra_state);
14778 bbr->rc_use_idle_restart = 1;
14780 bbr->rc_use_idle_restart = 0;
14782 case TCP_BBR_SEND_IWND_IN_TSO:
14783 BBR_OPTS_INC(tcp_iwnd_tso);
14785 bbr->bbr_init_win_cheat = 1;
14786 if (bbr->rc_past_init_win == 0) {
14788 cts = tcp_get_usecs(&bbr->rc_tv);
14789 tcp_bbr_tso_size_check(bbr, cts);
14792 bbr->bbr_init_win_cheat = 0;
14794 case TCP_BBR_HDWR_PACE:
14795 BBR_OPTS_INC(tcp_hdwr_pacing);
14797 bbr->bbr_hdw_pace_ena = 1;
14798 bbr->bbr_attempt_hdwr_pace = 0;
14800 bbr->bbr_hdw_pace_ena = 0;
14802 if (bbr->bbr_hdrw_pacing) {
14803 bbr->bbr_hdrw_pacing = 0;
14804 in_pcbdetach_txrtlmt(bbr->rc_inp);
14811 BBR_OPTS_INC(tcp_delack);
14812 if (optval < 100) {
14813 if (optval == 0) /* off */
14814 tp->t_delayed_ack = 0;
14815 else if (optval == 1) /* on which is 2 */
14816 tp->t_delayed_ack = 2;
14817 else /* higher than 2 and less than 100 */
14818 tp->t_delayed_ack = optval;
14819 if (tp->t_flags & TF_DELACK) {
14820 tp->t_flags &= ~TF_DELACK;
14821 tp->t_flags |= TF_ACKNOW;
14827 case TCP_RACK_PKT_DELAY:
14828 /* RACK added ms i.e. rack-rtt + reord + N */
14829 BBR_OPTS_INC(tcp_rack_pkt_delay);
14830 bbr->r_ctl.rc_pkt_delay = optval;
14832 #ifdef NETFLIX_PEAKRATE
14833 case TCP_MAXPEAKRATE:
14834 BBR_OPTS_INC(tcp_maxpeak);
14835 error = tcp_set_maxpeakrate(tp, optval);
14837 tp->t_peakrate_thr = tp->t_maxpeakrate;
14840 case TCP_BBR_RETRAN_WTSO:
14841 BBR_OPTS_INC(tcp_retran_wtso);
14843 bbr->rc_resends_use_tso = 1;
14845 bbr->rc_resends_use_tso = 0;
14847 case TCP_DATA_AFTER_CLOSE:
14848 BBR_OPTS_INC(tcp_data_ac);
14850 bbr->rc_allow_data_af_clo = 1;
14852 bbr->rc_allow_data_af_clo = 0;
14854 case TCP_BBR_POLICER_DETECT:
14855 BBR_OPTS_INC(tcp_policer_det);
14856 if (bbr->rc_use_google == 0)
14859 bbr->r_use_policer = 1;
14861 bbr->r_use_policer = 0;
14864 case TCP_BBR_TSTMP_RAISES:
14865 BBR_OPTS_INC(tcp_ts_raises);
14867 bbr->ts_can_raise = 1;
14869 bbr->ts_can_raise = 0;
14871 case TCP_BBR_TMR_PACE_OH:
14872 BBR_OPTS_INC(tcp_pacing_oh_tmr);
14873 if (bbr->rc_use_google) {
14877 bbr->r_ctl.rc_incr_tmrs = 1;
14879 bbr->r_ctl.rc_incr_tmrs = 0;
14882 case TCP_BBR_PACE_OH:
14883 BBR_OPTS_INC(tcp_pacing_oh);
14884 if (bbr->rc_use_google) {
14887 if (optval > (BBR_INCL_TCP_OH|
14889 BBR_INCL_ENET_OH)) {
14893 if (optval & BBR_INCL_TCP_OH)
14894 bbr->r_ctl.rc_inc_tcp_oh = 1;
14896 bbr->r_ctl.rc_inc_tcp_oh = 0;
14897 if (optval & BBR_INCL_IP_OH)
14898 bbr->r_ctl.rc_inc_ip_oh = 1;
14900 bbr->r_ctl.rc_inc_ip_oh = 0;
14901 if (optval & BBR_INCL_ENET_OH)
14902 bbr->r_ctl.rc_inc_enet_oh = 1;
14904 bbr->r_ctl.rc_inc_enet_oh = 0;
14908 return (tcp_default_ctloutput(so, sopt, inp, tp));
14911 #ifdef NETFLIX_STATS
14912 tcp_log_socket_option(tp, sopt->sopt_name, optval, error);
14919 * return 0 on success, error-num on failure
14922 bbr_get_sockopt(struct socket *so, struct sockopt *sopt,
14923 struct inpcb *inp, struct tcpcb *tp, struct tcp_bbr *bbr)
14925 int32_t error, optval;
14928 * Because all our options are either boolean or an int, we can just
14929 * pull everything into optval and then unlock and copy. If we ever
14930 * add a option that is not a int, then this will have quite an
14931 * impact to this routine.
14933 switch (sopt->sopt_name) {
14934 case TCP_BBR_PACE_PER_SEC:
14935 optval = bbr->r_ctl.bbr_hptsi_per_second;
14937 case TCP_BBR_PACE_DEL_TAR:
14938 optval = bbr->r_ctl.bbr_hptsi_segments_delay_tar;
14940 case TCP_BBR_PACE_SEG_MAX:
14941 optval = bbr->r_ctl.bbr_hptsi_segments_max;
14943 case TCP_BBR_MIN_TOPACEOUT:
14944 optval = bbr->no_pacing_until;
14946 case TCP_BBR_PACE_SEG_MIN:
14947 optval = bbr->r_ctl.bbr_hptsi_bytes_min;
14949 case TCP_BBR_PACE_CROSS:
14950 optval = bbr->r_ctl.bbr_cross_over;
14952 case TCP_BBR_ALGORITHM:
14953 optval = bbr->rc_use_google;
14955 case TCP_BBR_TSLIMITS:
14956 optval = bbr->rc_use_ts_limit;
14958 case TCP_BBR_IWINTSO:
14959 optval = bbr->rc_init_win;
14961 case TCP_BBR_STARTUP_PG:
14962 optval = bbr->r_ctl.rc_startup_pg;
14964 case TCP_BBR_DRAIN_PG:
14965 optval = bbr->r_ctl.rc_drain_pg;
14967 case TCP_BBR_PROBE_RTT_INT:
14968 optval = bbr->r_ctl.rc_probertt_int;
14970 case TCP_BBR_PROBE_RTT_LEN:
14971 optval = (bbr->r_ctl.rc_rttprop.cur_time_limit / USECS_IN_SECOND);
14973 case TCP_BBR_PROBE_RTT_GAIN:
14974 optval = bbr->r_ctl.bbr_rttprobe_gain_val;
14976 case TCP_BBR_STARTUP_LOSS_EXIT:
14977 optval = bbr->rc_loss_exit;
14979 case TCP_BBR_USEDEL_RATE:
14982 case TCP_BBR_MIN_RTO:
14983 optval = bbr->r_ctl.rc_min_rto_ms;
14985 case TCP_BBR_MAX_RTO:
14986 optval = bbr->rc_max_rto_sec;
14988 case TCP_RACK_PACE_MAX_SEG:
14989 /* Max segments in a pace */
14990 optval = bbr->r_ctl.rc_pace_max_segs;
14992 case TCP_RACK_MIN_TO:
14993 /* Minimum time between rack t-o's in ms */
14994 optval = bbr->r_ctl.rc_min_to;
14996 case TCP_RACK_REORD_THRESH:
14997 /* RACK reorder threshold (shift amount) */
14998 optval = bbr->r_ctl.rc_reorder_shift;
15000 case TCP_RACK_REORD_FADE:
15001 /* Does reordering fade after ms time */
15002 optval = bbr->r_ctl.rc_reorder_fade;
15004 case TCP_BBR_USE_RACK_CHEAT:
15005 /* Do we use the rack cheat for rxt */
15006 optval = bbr->bbr_use_rack_cheat;
15008 case TCP_BBR_FLOOR_MIN_TSO:
15009 optval = bbr->r_ctl.bbr_hptsi_segments_floor;
15011 case TCP_BBR_UTTER_MAX_TSO:
15012 optval = bbr->r_ctl.bbr_utter_max;
15014 case TCP_BBR_SEND_IWND_IN_TSO:
15015 /* Do we send TSO size segments initially */
15016 optval = bbr->bbr_init_win_cheat;
15018 case TCP_BBR_EXTRA_STATE:
15019 optval = bbr->rc_use_idle_restart;
15021 case TCP_RACK_TLP_THRESH:
15022 /* RACK TLP theshold i.e. srtt+(srtt/N) */
15023 optval = bbr->rc_tlp_threshold;
15025 case TCP_RACK_PKT_DELAY:
15026 /* RACK added ms i.e. rack-rtt + reord + N */
15027 optval = bbr->r_ctl.rc_pkt_delay;
15029 case TCP_BBR_RETRAN_WTSO:
15030 optval = bbr->rc_resends_use_tso;
15032 case TCP_DATA_AFTER_CLOSE:
15033 optval = bbr->rc_allow_data_af_clo;
15036 optval = tp->t_delayed_ack;
15038 case TCP_BBR_HDWR_PACE:
15039 optval = bbr->bbr_hdw_pace_ena;
15041 case TCP_BBR_POLICER_DETECT:
15042 optval = bbr->r_use_policer;
15044 case TCP_BBR_TSTMP_RAISES:
15045 optval = bbr->ts_can_raise;
15047 case TCP_BBR_TMR_PACE_OH:
15048 optval = bbr->r_ctl.rc_incr_tmrs;
15050 case TCP_BBR_PACE_OH:
15052 if (bbr->r_ctl.rc_inc_tcp_oh)
15053 optval |= BBR_INCL_TCP_OH;
15054 if (bbr->r_ctl.rc_inc_ip_oh)
15055 optval |= BBR_INCL_IP_OH;
15056 if (bbr->r_ctl.rc_inc_enet_oh)
15057 optval |= BBR_INCL_ENET_OH;
15060 return (tcp_default_ctloutput(so, sopt, inp, tp));
15064 error = sooptcopyout(sopt, &optval, sizeof optval);
15069 * return 0 on success, error-num on failure
15072 bbr_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
15074 int32_t error = EINVAL;
15075 struct tcp_bbr *bbr;
15077 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
15082 if (sopt->sopt_dir == SOPT_SET) {
15083 return (bbr_set_sockopt(so, sopt, inp, tp, bbr));
15084 } else if (sopt->sopt_dir == SOPT_GET) {
15085 return (bbr_get_sockopt(so, sopt, inp, tp, bbr));
15093 struct tcp_function_block __tcp_bbr = {
15094 .tfb_tcp_block_name = __XSTRING(STACKNAME),
15095 .tfb_tcp_output = bbr_output,
15096 .tfb_do_queued_segments = ctf_do_queued_segments,
15097 .tfb_do_segment_nounlock = bbr_do_segment_nounlock,
15098 .tfb_tcp_do_segment = bbr_do_segment,
15099 .tfb_tcp_ctloutput = bbr_ctloutput,
15100 .tfb_tcp_fb_init = bbr_init,
15101 .tfb_tcp_fb_fini = bbr_fini,
15102 .tfb_tcp_timer_stop_all = bbr_stopall,
15103 .tfb_tcp_timer_activate = bbr_timer_activate,
15104 .tfb_tcp_timer_active = bbr_timer_active,
15105 .tfb_tcp_timer_stop = bbr_timer_stop,
15106 .tfb_tcp_rexmit_tmr = bbr_remxt_tmr,
15107 .tfb_tcp_handoff_ok = bbr_handoff_ok,
15108 .tfb_tcp_mtu_chg = bbr_mtu_chg
15111 static const char *bbr_stack_names[] = {
15112 __XSTRING(STACKNAME),
15114 __XSTRING(STACKALIAS),
15118 static bool bbr_mod_inited = false;
15121 tcp_addbbr(module_t mod, int32_t type, void *data)
15128 printf("Attempting to load " __XSTRING(MODNAME) "\n");
15129 bbr_zone = uma_zcreate(__XSTRING(MODNAME) "_map",
15130 sizeof(struct bbr_sendmap),
15131 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
15132 bbr_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb",
15133 sizeof(struct tcp_bbr),
15134 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
15135 sysctl_ctx_init(&bbr_sysctl_ctx);
15136 bbr_sysctl_root = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
15137 SYSCTL_STATIC_CHILDREN(_net_inet_tcp),
15140 __XSTRING(STACKALIAS),
15142 __XSTRING(STACKNAME),
15144 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
15146 if (bbr_sysctl_root == NULL) {
15147 printf("Failed to add sysctl node\n");
15151 bbr_init_sysctls();
15152 num_stacks = nitems(bbr_stack_names);
15153 err = register_tcp_functions_as_names(&__tcp_bbr, M_WAITOK,
15154 bbr_stack_names, &num_stacks);
15156 printf("Failed to register %s stack name for "
15157 "%s module\n", bbr_stack_names[num_stacks],
15158 __XSTRING(MODNAME));
15159 sysctl_ctx_free(&bbr_sysctl_ctx);
15161 uma_zdestroy(bbr_zone);
15162 uma_zdestroy(bbr_pcb_zone);
15163 bbr_counter_destroy();
15164 printf("Failed to register " __XSTRING(MODNAME)
15165 " module err:%d\n", err);
15168 tcp_lro_reg_mbufq();
15169 bbr_mod_inited = true;
15170 printf(__XSTRING(MODNAME) " is now available\n");
15173 err = deregister_tcp_functions(&__tcp_bbr, true, false);
15176 err = deregister_tcp_functions(&__tcp_bbr, false, true);
15179 if (bbr_mod_inited) {
15180 uma_zdestroy(bbr_zone);
15181 uma_zdestroy(bbr_pcb_zone);
15182 sysctl_ctx_free(&bbr_sysctl_ctx);
15183 bbr_counter_destroy();
15184 printf(__XSTRING(MODNAME)
15185 " is now no longer available\n");
15186 bbr_mod_inited = false;
15188 tcp_lro_dereg_mbufq();
15192 return (EOPNOTSUPP);
15197 static moduledata_t tcp_bbr = {
15198 .name = __XSTRING(MODNAME),
15199 .evhand = tcp_addbbr,
15203 MODULE_VERSION(MODNAME, 1);
15204 DECLARE_MODULE(MODNAME, tcp_bbr, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
15205 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1);