2 * Copyright (c) 2016-2020 Netflix, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Author: Randall Stewart <rrs@netflix.com>
28 * This work is based on the ACM Queue paper
29 * BBR - Congestion Based Congestion Control
30 * and also numerous discussions with Neal, Yuchung and Van.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 #include "opt_inet6.h"
38 #include "opt_ipsec.h"
39 #include "opt_tcpdebug.h"
40 #include "opt_ratelimit.h"
41 #include "opt_kern_tls.h"
42 #include <sys/param.h>
44 #include <sys/module.h>
45 #include <sys/kernel.h>
46 #include <sys/libkern.h>
48 #include <sys/hhook.h>
50 #include <sys/malloc.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/systm.h>
61 #include <sys/qmath.h>
63 #include <sys/stats.h> /* Must come after qmath.h and tree.h */
65 #include <sys/refcount.h>
66 #include <sys/queue.h>
67 #include <sys/eventhandler.h>
69 #include <sys/kthread.h>
71 #include <sys/mutex.h>
72 #include <sys/tim_filter.h>
74 #include <sys/protosw.h>
76 #include <sys/kern_prefetch.h>
78 #include <net/route.h>
79 #include <net/route/nhop.h>
82 #define TCPSTATES /* for logging */
84 #include <netinet/in.h>
85 #include <netinet/in_kdtrace.h>
86 #include <netinet/in_pcb.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_icmp.h> /* required for icmp_var.h */
89 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
90 #include <netinet/ip_var.h>
91 #include <netinet/ip6.h>
92 #include <netinet6/in6_pcb.h>
93 #include <netinet6/ip6_var.h>
95 #include <netinet/tcp.h>
96 #include <netinet/tcp_fsm.h>
97 #include <netinet/tcp_seq.h>
98 #include <netinet/tcp_timer.h>
99 #include <netinet/tcp_var.h>
100 #include <netinet/tcpip.h>
101 #include <netinet/tcp_hpts.h>
102 #include <netinet/cc/cc.h>
103 #include <netinet/tcp_log_buf.h>
104 #include <netinet/tcp_ratelimit.h>
105 #include <netinet/tcp_lro.h>
107 #include <netinet/tcp_debug.h>
108 #endif /* TCPDEBUG */
110 #include <netinet/tcp_offload.h>
113 #include <netinet6/tcp6_var.h>
115 #include <netinet/tcp_fastopen.h>
117 #include <netipsec/ipsec_support.h>
119 #include <net/if_var.h>
120 #include <net/ethernet.h>
122 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
123 #include <netipsec/ipsec.h>
124 #include <netipsec/ipsec6.h>
127 #include <netinet/udp.h>
128 #include <netinet/udp_var.h>
129 #include <machine/in_cksum.h>
132 #include <security/mac/mac_framework.h>
135 #include "sack_filter.h"
137 #include "rack_bbr_common.h"
139 uma_zone_t bbr_pcb_zone;
141 struct sysctl_ctx_list bbr_sysctl_ctx;
142 struct sysctl_oid *bbr_sysctl_root;
144 #define TCPT_RANGESET_NOSLOP(tv, value, tvmin, tvmax) do { \
146 if ((u_long)(tv) < (u_long)(tvmin)) \
148 if ((u_long)(tv) > (u_long)(tvmax)) \
152 /*#define BBR_INVARIANT 1*/
157 static uint32_t bbr_def_init_win = 10;
158 static int32_t bbr_persist_min = 250000; /* 250ms */
159 static int32_t bbr_persist_max = 1000000; /* 1 Second */
160 static int32_t bbr_cwnd_may_shrink = 0;
161 static int32_t bbr_cwndtarget_rtt_touse = BBR_RTT_PROP;
162 static int32_t bbr_num_pktepo_for_del_limit = BBR_NUM_RTTS_FOR_DEL_LIMIT;
163 static int32_t bbr_hardware_pacing_limit = 8000;
164 static int32_t bbr_quanta = 3; /* How much extra quanta do we get? */
165 static int32_t bbr_no_retran = 0;
168 static int32_t bbr_error_base_paceout = 10000; /* usec to pace */
169 static int32_t bbr_max_net_error_cnt = 10;
170 /* Should the following be dynamic too -- loss wise */
171 static int32_t bbr_rtt_gain_thresh = 0;
172 /* Measurement controls */
173 static int32_t bbr_use_google_algo = 1;
174 static int32_t bbr_ts_limiting = 1;
175 static int32_t bbr_ts_can_raise = 0;
176 static int32_t bbr_do_red = 600;
177 static int32_t bbr_red_scale = 20000;
178 static int32_t bbr_red_mul = 1;
179 static int32_t bbr_red_div = 2;
180 static int32_t bbr_red_growth_restrict = 1;
181 static int32_t bbr_target_is_bbunit = 0;
182 static int32_t bbr_drop_limit = 0;
184 * How much gain do we need to see to
187 static int32_t bbr_marks_rxt_sack_passed = 0;
188 static int32_t bbr_start_exit = 25;
189 static int32_t bbr_low_start_exit = 25; /* When we are in reduced gain */
190 static int32_t bbr_startup_loss_thresh = 2000; /* 20.00% loss */
191 static int32_t bbr_hptsi_max_mul = 1; /* These two mul/div assure a min pacing */
192 static int32_t bbr_hptsi_max_div = 2; /* time, 0 means turned off. We need this
193 * if we go back ever to where the pacer
194 * has priority over timers.
196 static int32_t bbr_policer_call_from_rack_to = 0;
197 static int32_t bbr_policer_detection_enabled = 1;
198 static int32_t bbr_min_measurements_req = 1; /* We need at least 2
199 * measurments before we are
200 * "good" note that 2 == 1.
201 * This is because we use a >
202 * comparison. This means if
203 * min_measure was 0, it takes
204 * num-measures > min(0) and
205 * you get 1 measurement and
206 * you are good. Set to 1, you
208 * measurements (this is done
209 * to prevent it from being ok
210 * to have no measurements). */
211 static int32_t bbr_no_pacing_until = 4;
213 static int32_t bbr_min_usec_delta = 20000; /* 20,000 usecs */
214 static int32_t bbr_min_peer_delta = 20; /* 20 units */
215 static int32_t bbr_delta_percent = 150; /* 15.0 % */
217 static int32_t bbr_target_cwnd_mult_limit = 8;
219 * bbr_cwnd_min_val is the number of
220 * segments we hold to in the RTT probe
223 static int32_t bbr_cwnd_min_val = BBR_PROBERTT_NUM_MSS;
226 static int32_t bbr_cwnd_min_val_hs = BBR_HIGHSPEED_NUM_MSS;
228 static int32_t bbr_gain_to_target = 1;
229 static int32_t bbr_gain_gets_extra_too = 1;
231 * bbr_high_gain is the 2/ln(2) value we need
232 * to double the sending rate in startup. This
233 * is used for both cwnd and hptsi gain's.
235 static int32_t bbr_high_gain = BBR_UNIT * 2885 / 1000 + 1;
236 static int32_t bbr_startup_lower = BBR_UNIT * 1500 / 1000 + 1;
237 static int32_t bbr_use_lower_gain_in_startup = 1;
239 /* thresholds for reduction on drain in sub-states/drain */
240 static int32_t bbr_drain_rtt = BBR_SRTT;
241 static int32_t bbr_drain_floor = 88;
242 static int32_t google_allow_early_out = 1;
243 static int32_t google_consider_lost = 1;
244 static int32_t bbr_drain_drop_mul = 4;
245 static int32_t bbr_drain_drop_div = 5;
246 static int32_t bbr_rand_ot = 50;
247 static int32_t bbr_can_force_probertt = 0;
248 static int32_t bbr_can_adjust_probertt = 1;
249 static int32_t bbr_probertt_sets_rtt = 0;
250 static int32_t bbr_can_use_ts_for_rtt = 1;
251 static int32_t bbr_is_ratio = 0;
252 static int32_t bbr_sub_drain_app_limit = 1;
253 static int32_t bbr_prtt_slam_cwnd = 1;
254 static int32_t bbr_sub_drain_slam_cwnd = 1;
255 static int32_t bbr_slam_cwnd_in_main_drain = 1;
256 static int32_t bbr_filter_len_sec = 6; /* How long does the rttProp filter
258 static uint32_t bbr_rtt_probe_limit = (USECS_IN_SECOND * 4);
260 * bbr_drain_gain is the reverse of the high_gain
261 * designed to drain back out the standing queue
262 * that is formed in startup by causing a larger
263 * hptsi gain and thus drainging the packets
266 static int32_t bbr_drain_gain = BBR_UNIT * 1000 / 2885;
267 static int32_t bbr_rttprobe_gain = 192;
270 * The cwnd_gain is the default cwnd gain applied when
271 * calculating a target cwnd. Note that the cwnd is
272 * a secondary factor in the way BBR works (see the
273 * paper and think about it, it will take some time).
274 * Basically the hptsi_gain spreads the packets out
275 * so you never get more than BDP to the peer even
276 * if the cwnd is high. In our implemenation that
277 * means in non-recovery/retransmission scenarios
278 * cwnd will never be reached by the flight-size.
280 static int32_t bbr_cwnd_gain = BBR_UNIT * 2;
281 static int32_t bbr_tlp_type_to_use = BBR_SRTT;
282 static int32_t bbr_delack_time = 100000; /* 100ms in useconds */
283 static int32_t bbr_sack_not_required = 0; /* set to one to allow non-sack to use bbr */
284 static int32_t bbr_initial_bw_bps = 62500; /* 500kbps in bytes ps */
285 static int32_t bbr_ignore_data_after_close = 1;
286 static int16_t bbr_hptsi_gain[] = {
296 int32_t bbr_use_rack_resend_cheat = 1;
297 int32_t bbr_sends_full_iwnd = 1;
299 #define BBR_HPTSI_GAIN_MAX 8
301 * The BBR module incorporates a number of
302 * TCP ideas that have been put out into the IETF
303 * over the last few years:
304 * - Yuchung Cheng's RACK TCP (for which its named) that
305 * will stop us using the number of dup acks and instead
306 * use time as the gage of when we retransmit.
307 * - Reorder Detection of RFC4737 and the Tail-Loss probe draft
308 * of Dukkipati et.al.
309 * - Van Jacobson's et.al BBR.
311 * RACK depends on SACK, so if an endpoint arrives that
312 * cannot do SACK the state machine below will shuttle the
313 * connection back to using the "default" TCP stack that is
316 * To implement BBR and RACK the original TCP stack was first decomposed
317 * into a functional state machine with individual states
318 * for each of the possible TCP connection states. The do_segement
319 * functions role in life is to mandate the connection supports SACK
320 * initially and then assure that the RACK state matches the conenction
321 * state before calling the states do_segment function. Data processing
322 * of inbound segments also now happens in the hpts_do_segment in general
323 * with only one exception. This is so we can keep the connection on
326 * Each state is simplified due to the fact that the original do_segment
327 * has been decomposed and we *know* what state we are in (no
328 * switches on the state) and all tests for SACK are gone. This
329 * greatly simplifies what each state does.
331 * TCP output is also over-written with a new version since it
332 * must maintain the new rack scoreboard and has had hptsi
333 * integrated as a requirment. Still todo is to eliminate the
334 * use of the callout_() system and use the hpts for all
337 static uint32_t bbr_rtt_probe_time = 200000; /* 200ms in micro seconds */
338 static uint32_t bbr_rtt_probe_cwndtarg = 4; /* How many mss's outstanding */
339 static const int32_t bbr_min_req_free = 2; /* The min we must have on the
341 static int32_t bbr_tlp_thresh = 1;
342 static int32_t bbr_reorder_thresh = 2;
343 static int32_t bbr_reorder_fade = 60000000; /* 0 - never fade, def
344 * 60,000,000 - 60 seconds */
345 static int32_t bbr_pkt_delay = 1000;
346 static int32_t bbr_min_to = 1000; /* Number of usec's minimum timeout */
347 static int32_t bbr_incr_timers = 1;
349 static int32_t bbr_tlp_min = 10000; /* 10ms in usecs */
350 static int32_t bbr_delayed_ack_time = 200000; /* 200ms in usecs */
351 static int32_t bbr_exit_startup_at_loss = 1;
354 * bbr_lt_bw_ratio is 1/8th
355 * bbr_lt_bw_diff is < 4 Kbit/sec
357 static uint64_t bbr_lt_bw_diff = 4000 / 8; /* In bytes per second */
358 static uint64_t bbr_lt_bw_ratio = 8; /* For 1/8th */
359 static uint32_t bbr_lt_bw_max_rtts = 48; /* How many rtt's do we use
361 static uint32_t bbr_lt_intvl_min_rtts = 4; /* Min num of RTT's to measure
363 static int32_t bbr_lt_intvl_fp = 0; /* False positive epoch diff */
364 static int32_t bbr_lt_loss_thresh = 196; /* Lost vs delivered % */
365 static int32_t bbr_lt_fd_thresh = 100; /* false detection % */
367 static int32_t bbr_verbose_logging = 0;
369 * Currently regular tcp has a rto_min of 30ms
370 * the backoff goes 12 times so that ends up
371 * being a total of 122.850 seconds before a
372 * connection is killed.
374 static int32_t bbr_rto_min_ms = 30; /* 30ms same as main freebsd */
375 static int32_t bbr_rto_max_sec = 4; /* 4 seconds */
377 /****************************************************/
378 /* DEFAULT TSO SIZING (cpu performance impacting) */
379 /****************************************************/
380 /* What amount is our formula using to get TSO size */
381 static int32_t bbr_hptsi_per_second = 1000;
384 * For hptsi under bbr_cross_over connections what is delay
385 * target 7ms (in usec) combined with a seg_max of 2
386 * gets us close to identical google behavior in
387 * TSO size selection (possibly more 1MSS sends).
389 static int32_t bbr_hptsi_segments_delay_tar = 7000;
391 /* Does pacing delay include overhead's in its time calculations? */
392 static int32_t bbr_include_enet_oh = 0;
393 static int32_t bbr_include_ip_oh = 1;
394 static int32_t bbr_include_tcp_oh = 1;
395 static int32_t bbr_google_discount = 10;
397 /* Do we use (nf mode) pkt-epoch to drive us or rttProp? */
398 static int32_t bbr_state_is_pkt_epoch = 0;
399 static int32_t bbr_state_drain_2_tar = 1;
400 /* What is the max the 0 - bbr_cross_over MBPS TSO target
401 * can reach using our delay target. Note that this
402 * value becomes the floor for the cross over
405 static int32_t bbr_hptsi_segments_max = 2;
406 static int32_t bbr_hptsi_segments_floor = 1;
407 static int32_t bbr_hptsi_utter_max = 0;
409 /* What is the min the 0 - bbr_cross-over MBPS TSO target can be */
410 static int32_t bbr_hptsi_bytes_min = 1460;
411 static int32_t bbr_all_get_min = 0;
413 /* Cross over point from algo-a to algo-b */
414 static uint32_t bbr_cross_over = TWENTY_THREE_MBPS;
416 /* Do we deal with our restart state? */
417 static int32_t bbr_uses_idle_restart = 0;
418 static int32_t bbr_idle_restart_threshold = 100000; /* 100ms in useconds */
420 /* Do we allow hardware pacing? */
421 static int32_t bbr_allow_hdwr_pacing = 0;
422 static int32_t bbr_hdwr_pace_adjust = 2; /* multipler when we calc the tso size */
423 static int32_t bbr_hdwr_pace_floor = 1;
424 static int32_t bbr_hdwr_pacing_delay_cnt = 10;
426 /****************************************************/
427 static int32_t bbr_resends_use_tso = 0;
428 static int32_t bbr_tlp_max_resend = 2;
429 static int32_t bbr_sack_block_limit = 128;
431 #define BBR_MAX_STAT 19
432 counter_u64_t bbr_state_time[BBR_MAX_STAT];
433 counter_u64_t bbr_state_lost[BBR_MAX_STAT];
434 counter_u64_t bbr_state_resend[BBR_MAX_STAT];
435 counter_u64_t bbr_stat_arry[BBR_STAT_SIZE];
436 counter_u64_t bbr_opts_arry[BBR_OPTS_SIZE];
437 counter_u64_t bbr_out_size[TCP_MSS_ACCT_SIZE];
438 counter_u64_t bbr_flows_whdwr_pacing;
439 counter_u64_t bbr_flows_nohdwr_pacing;
441 counter_u64_t bbr_nohdwr_pacing_enobuf;
442 counter_u64_t bbr_hdwr_pacing_enobuf;
444 static inline uint64_t bbr_get_bw(struct tcp_bbr *bbr);
447 * Static defintions we need for forward declarations.
450 bbr_get_pacing_length(struct tcp_bbr *bbr, uint16_t gain,
451 uint32_t useconds_time, uint64_t bw);
453 bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain);
455 bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win);
457 bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses);
459 bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int line,
462 bbr_get_target_cwnd(struct tcp_bbr *bbr, uint64_t bw, uint32_t gain);
464 bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch,
465 int32_t pkt_epoch, uint32_t losses);
467 bbr_calc_thresh_rack(struct tcp_bbr *bbr, uint32_t srtt, uint32_t cts, struct bbr_sendmap *rsm);
468 static uint32_t bbr_initial_cwnd(struct tcp_bbr *bbr, struct tcpcb *tp);
470 bbr_calc_thresh_tlp(struct tcpcb *tp, struct tcp_bbr *bbr,
471 struct bbr_sendmap *rsm, uint32_t srtt,
474 bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts,
477 bbr_set_state_target(struct tcp_bbr *bbr, int line);
479 bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line);
482 bbr_log_progress_event(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t tick, int event, int line);
485 tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts);
488 bbr_setup_red_bw(struct tcp_bbr *bbr, uint32_t cts);
491 bbr_log_rtt_shrinks(struct tcp_bbr *bbr, uint32_t cts, uint32_t applied, uint32_t rtt,
492 uint32_t line, uint8_t is_start, uint16_t set);
494 static struct bbr_sendmap *
495 bbr_find_lowest_rsm(struct tcp_bbr *bbr);
496 static __inline uint32_t
497 bbr_get_rtt(struct tcp_bbr *bbr, int32_t rtt_type);
499 bbr_log_to_start(struct tcp_bbr *bbr, uint32_t cts, uint32_t to, int32_t slot, uint8_t which);
502 bbr_log_timer_var(struct tcp_bbr *bbr, int mode, uint32_t cts, uint32_t time_since_sent, uint32_t srtt,
503 uint32_t thresh, uint32_t to);
505 bbr_log_hpts_diag(struct tcp_bbr *bbr, uint32_t cts, struct hpts_diag *diag);
508 bbr_log_type_bbrsnd(struct tcp_bbr *bbr, uint32_t len, uint32_t slot,
509 uint32_t del_by, uint32_t cts, uint32_t sloton, uint32_t prev_delay);
512 bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr,
513 uint32_t cts, int32_t line);
515 bbr_stop_all_timers(struct tcpcb *tp);
517 bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts);
519 bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts);
521 bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts);
525 bbr_log_pacing_delay_calc(struct tcp_bbr *bbr, uint16_t gain, uint32_t len,
526 uint32_t cts, uint32_t usecs, uint64_t bw, uint32_t override, int mod);
528 static inline uint8_t
529 bbr_state_val(struct tcp_bbr *bbr)
531 return(bbr->rc_bbr_substate);
534 static inline uint32_t
535 get_min_cwnd(struct tcp_bbr *bbr)
539 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs);
540 if (bbr_get_rtt(bbr, BBR_RTT_PROP) < BBR_HIGH_SPEED)
541 return (bbr_cwnd_min_val_hs * mss);
543 return (bbr_cwnd_min_val * mss);
547 bbr_get_persists_timer_val(struct tcpcb *tp, struct tcp_bbr *bbr)
552 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT;
553 if (tp->t_srtt == 0) {
554 srtt = (uint64_t)BBR_INITIAL_RTO;
557 srtt = ((uint64_t)TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT);
558 var = ((uint64_t)TICKS_2_USEC(tp->t_rttvar) >> TCP_RTT_SHIFT);
560 TCPT_RANGESET_NOSLOP(ret_val, ((srtt + var) * tcp_backoff[tp->t_rxtshift]),
561 bbr_persist_min, bbr_persist_max);
562 return ((uint32_t)ret_val);
566 bbr_timer_start(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
569 * Start the FR timer, we do this based on getting the first one in
570 * the rc_tmap. Note that if its NULL we must stop the timer. in all
571 * events we need to stop the running timer (if its running) before
572 * starting the new one.
574 uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse;
576 int32_t is_tlp_timer = 0;
577 struct bbr_sendmap *rsm;
579 if (bbr->rc_all_timers_stopped) {
580 /* All timers have been stopped none are to run */
583 if (bbr->rc_in_persist) {
584 /* We can't start any timer in persists */
585 return (bbr_get_persists_timer_val(tp, bbr));
587 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
589 ((tp->t_flags & TF_SACK_PERMIT) == 0) ||
590 (tp->t_state < TCPS_ESTABLISHED)) {
591 /* Nothing on the send map */
593 if (SEQ_LT(tp->snd_una, tp->snd_max) || sbavail(&(tp->t_inpcb->inp_socket->so_snd))) {
597 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
599 idx = rsm->r_rtr_cnt - 1;
600 if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], bbr->r_ctl.rc_tlp_rxt_last_time))
601 tstmp_touse = rsm->r_tim_lastsent[idx];
603 tstmp_touse = bbr->r_ctl.rc_tlp_rxt_last_time;
604 if (TSTMP_GT(tstmp_touse, cts))
605 time_since_sent = cts - tstmp_touse;
607 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_RXT;
609 tov = BBR_INITIAL_RTO;
611 tov = ((uint64_t)(TICKS_2_USEC(tp->t_srtt) +
612 ((uint64_t)TICKS_2_USEC(tp->t_rttvar) * (uint64_t)4)) >> TCP_RTT_SHIFT);
614 tov *= tcp_backoff[tp->t_rxtshift];
615 if (tov > time_since_sent)
616 tov -= time_since_sent;
618 tov = bbr->r_ctl.rc_min_to;
619 TCPT_RANGESET_NOSLOP(to, tov,
620 (bbr->r_ctl.rc_min_rto_ms * MS_IN_USEC),
621 (bbr->rc_max_rto_sec * USECS_IN_SECOND));
622 bbr_log_timer_var(bbr, 2, cts, 0, srtt, 0, to);
627 if (rsm->r_flags & BBR_ACKED) {
628 rsm = bbr_find_lowest_rsm(bbr);
634 /* Convert from ms to usecs */
635 if (rsm->r_flags & BBR_SACK_PASSED) {
636 if ((tp->t_flags & TF_SENTFIN) &&
637 ((tp->snd_max - tp->snd_una) == 1) &&
638 (rsm->r_flags & BBR_HAS_FIN)) {
640 * We don't start a bbr rack timer if all we have is
645 srtt = bbr_get_rtt(bbr, BBR_RTT_RACK);
646 thresh = bbr_calc_thresh_rack(bbr, srtt, cts, rsm);
647 idx = rsm->r_rtr_cnt - 1;
648 exp = rsm->r_tim_lastsent[idx] + thresh;
649 if (SEQ_GEQ(exp, cts)) {
651 if (to < bbr->r_ctl.rc_min_to) {
652 to = bbr->r_ctl.rc_min_to;
655 to = bbr->r_ctl.rc_min_to;
658 /* Ok we need to do a TLP not RACK */
659 if (bbr->rc_tlp_in_progress != 0) {
661 * The previous send was a TLP.
665 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_tmap, bbr_sendmap, r_tnext);
667 /* We found no rsm to TLP with. */
670 if (rsm->r_flags & BBR_HAS_FIN) {
671 /* If its a FIN we don't do TLP */
676 idx = rsm->r_rtr_cnt - 1;
677 if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], bbr->r_ctl.rc_tlp_rxt_last_time))
678 tstmp_touse = rsm->r_tim_lastsent[idx];
680 tstmp_touse = bbr->r_ctl.rc_tlp_rxt_last_time;
681 if (TSTMP_GT(tstmp_touse, cts))
682 time_since_sent = cts - tstmp_touse;
684 srtt = bbr_get_rtt(bbr, bbr_tlp_type_to_use);
685 thresh = bbr_calc_thresh_tlp(tp, bbr, rsm, srtt, cts);
686 if (thresh > time_since_sent)
687 to = thresh - time_since_sent;
689 to = bbr->r_ctl.rc_min_to;
690 if (to > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) {
692 * If the TLP time works out to larger than the max
693 * RTO lets not do TLP.. just RTO.
697 if ((bbr->rc_tlp_rtx_out == 1) &&
698 (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq)) {
700 * Second retransmit of the same TLP
703 bbr->rc_tlp_rtx_out = 0;
706 if (rsm->r_start != bbr->r_ctl.rc_last_tlp_seq) {
708 * The tail is no longer the last one I did a probe
711 bbr->r_ctl.rc_tlp_seg_send_cnt = 0;
712 bbr->r_ctl.rc_last_tlp_seq = rsm->r_start;
715 if (is_tlp_timer == 0) {
716 BBR_STAT_INC(bbr_to_arm_rack);
717 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_RACK;
719 bbr_log_timer_var(bbr, 1, cts, time_since_sent, srtt, thresh, to);
720 if (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend) {
722 * We have exceeded how many times we can retran the
723 * current TLP timer, switch to the RTO timer.
727 BBR_STAT_INC(bbr_to_arm_tlp);
728 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_TLP;
734 static inline int32_t
735 bbr_minseg(struct tcp_bbr *bbr)
737 return (bbr->r_ctl.rc_pace_min_segs - bbr->rc_last_options);
741 bbr_start_hpts_timer(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t cts, int32_t frm, int32_t slot, uint32_t tot_len)
744 struct hpts_diag diag;
745 uint32_t delayed_ack = 0;
747 uint32_t hpts_timeout;
749 int32_t delay_calc = 0;
750 uint32_t prev_delay = 0;
753 if (inp->inp_in_hpts) {
754 /* A previous call is already set up */
757 if ((tp->t_state == TCPS_CLOSED) ||
758 (tp->t_state == TCPS_LISTEN)) {
761 stopped = bbr->rc_tmr_stopped;
762 if (stopped && TSTMP_GT(bbr->r_ctl.rc_timer_exp, cts)) {
763 left = bbr->r_ctl.rc_timer_exp - cts;
765 bbr->r_ctl.rc_hpts_flags = 0;
766 bbr->r_ctl.rc_timer_exp = 0;
767 prev_delay = bbr->r_ctl.rc_last_delay_val;
768 if (bbr->r_ctl.rc_last_delay_val &&
771 * If a previous pacer delay was in place we
772 * are not coming from the output side (where
773 * we calculate a delay, more likely a timer).
775 slot = bbr->r_ctl.rc_last_delay_val;
776 if (TSTMP_GT(cts, bbr->rc_pacer_started)) {
777 /* Compensate for time passed */
778 delay_calc = cts - bbr->rc_pacer_started;
779 if (delay_calc <= slot)
783 /* Do we have early to make up for by pushing out the pacing time? */
784 if (bbr->r_agg_early_set) {
785 bbr_log_pacing_delay_calc(bbr, 0, bbr->r_ctl.rc_agg_early, cts, slot, 0, bbr->r_agg_early_set, 2);
786 slot += bbr->r_ctl.rc_agg_early;
787 bbr->r_ctl.rc_agg_early = 0;
788 bbr->r_agg_early_set = 0;
790 /* Are we running a total debt that needs to be compensated for? */
791 if (bbr->r_ctl.rc_hptsi_agg_delay) {
792 if (slot > bbr->r_ctl.rc_hptsi_agg_delay) {
793 /* We nuke the delay */
794 slot -= bbr->r_ctl.rc_hptsi_agg_delay;
795 bbr->r_ctl.rc_hptsi_agg_delay = 0;
797 /* We nuke some of the delay, put in a minimal 100usecs */
798 bbr->r_ctl.rc_hptsi_agg_delay -= slot;
799 bbr->r_ctl.rc_last_delay_val = slot = 100;
802 bbr->r_ctl.rc_last_delay_val = slot;
803 hpts_timeout = bbr_timer_start(tp, bbr, cts);
804 if (tp->t_flags & TF_DELACK) {
805 if (bbr->rc_in_persist == 0) {
806 delayed_ack = bbr_delack_time;
809 * We are in persists and have
810 * gotten a new data element.
812 if (hpts_timeout > bbr_delack_time) {
814 * Lets make the persists timer (which acks)
815 * be the smaller of hpts_timeout and bbr_delack_time.
817 hpts_timeout = bbr_delack_time;
822 ((hpts_timeout == 0) ||
823 (delayed_ack < hpts_timeout))) {
824 /* We need a Delayed ack timer */
825 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK;
826 hpts_timeout = delayed_ack;
829 /* Mark that we have a pacing timer up */
830 BBR_STAT_INC(bbr_paced_segments);
831 bbr->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT;
834 * If no timers are going to run and we will fall off thfe hptsi
835 * wheel, we resort to a keep-alive timer if its configured.
837 if ((hpts_timeout == 0) &&
839 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
840 (tp->t_state <= TCPS_CLOSING)) {
842 * Ok we have no timer (persists, rack, tlp, rxt or
843 * del-ack), we don't have segments being paced. So
844 * all that is left is the keepalive timer.
846 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
847 hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp));
849 hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp));
851 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP;
854 if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) ==
855 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) {
857 * RACK, TLP, persists and RXT timers all are restartable
858 * based on actions input .. i.e we received a packet (ack
859 * or sack) and that changes things (rw, or snd_una etc).
860 * Thus we can restart them with a new value. For
861 * keep-alive, delayed_ack we keep track of what was left
862 * and restart the timer with a smaller value.
864 if (left < hpts_timeout)
867 if (bbr->r_ctl.rc_incr_tmrs && slot &&
868 (bbr->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) {
870 * If configured to do so, and the timer is either
871 * the TLP or RXT timer, we need to increase the timeout
872 * by the pacing time. Consider the bottleneck at my
873 * machine as an example, we are sending something
874 * to start a TLP on. The last packet won't be emitted
875 * fully until the pacing time (the bottleneck will hold
876 * the data in place). Once the packet is emitted that
877 * is when we want to start waiting for the TLP. This
878 * is most evident with hardware pacing (where the nic
879 * is holding the packet(s) before emitting). But it
880 * can also show up in the network so we do it for all
881 * cases. Technically we would take off one packet from
882 * this extra delay but this is easier and being more
883 * conservative is probably better.
885 hpts_timeout += slot;
889 * Hack alert for now we can't time-out over 2147 seconds (a
890 * bit more than 35min)
892 if (hpts_timeout > 0x7ffffffe)
893 hpts_timeout = 0x7ffffffe;
894 bbr->r_ctl.rc_timer_exp = cts + hpts_timeout;
896 bbr->r_ctl.rc_timer_exp = 0;
898 (bbr->rc_use_google ||
899 bbr->output_error_seen ||
900 (slot <= hpts_timeout)) ) {
902 * Tell LRO that it can queue packets while
905 bbr->rc_inp->inp_flags2 |= INP_MBUF_QUEUE_READY;
906 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) &&
907 (bbr->rc_cwnd_limited == 0)) {
909 * If we are not cwnd limited and we
910 * are running a rack timer we put on
911 * the do not disturbe even for sack.
913 inp->inp_flags2 |= INP_DONT_SACK_QUEUE;
915 inp->inp_flags2 &= ~INP_DONT_SACK_QUEUE;
916 bbr->rc_pacer_started = cts;
918 (void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(slot),
920 bbr->rc_timer_first = 0;
921 bbr->bbr_timer_src = frm;
922 bbr_log_to_start(bbr, cts, hpts_timeout, slot, 1);
923 bbr_log_hpts_diag(bbr, cts, &diag);
924 } else if (hpts_timeout) {
925 (void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(hpts_timeout),
928 * We add the flag here as well if the slot is set,
929 * since hpts will call in to clear the queue first before
930 * calling the output routine (which does our timers).
931 * We don't want to set the flag if its just a timer
932 * else the arrival of data might (that causes us
933 * to send more) might get delayed. Imagine being
934 * on a keep-alive timer and a request comes in for
938 bbr->rc_pacer_started = cts;
939 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) &&
940 (bbr->rc_cwnd_limited == 0)) {
942 * For a rack timer, don't wake us even
943 * if a sack arrives as long as we are
946 bbr->rc_inp->inp_flags2 |= INP_MBUF_QUEUE_READY;
947 inp->inp_flags2 |= INP_DONT_SACK_QUEUE;
949 /* All other timers wake us up */
950 bbr->rc_inp->inp_flags2 &= ~INP_MBUF_QUEUE_READY;
951 inp->inp_flags2 &= ~INP_DONT_SACK_QUEUE;
953 bbr->bbr_timer_src = frm;
954 bbr_log_to_start(bbr, cts, hpts_timeout, slot, 0);
955 bbr_log_hpts_diag(bbr, cts, &diag);
956 bbr->rc_timer_first = 1;
958 bbr->rc_tmr_stopped = 0;
959 bbr_log_type_bbrsnd(bbr, tot_len, slot, delay_calc, cts, frm, prev_delay);
963 bbr_timer_audit(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, struct sockbuf *sb)
966 * We received an ack, and then did not call send or were bounced
967 * out due to the hpts was running. Now a timer is up as well, is it
971 struct bbr_sendmap *rsm;
972 uint32_t hpts_timeout;
975 tmr_up = bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
976 if (bbr->rc_in_persist && (tmr_up == PACE_TMR_PERSIT))
978 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
979 if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) &&
980 (tmr_up == PACE_TMR_RXT)) {
981 /* Should be an RXT */
986 /* Nothing outstanding? */
987 if (tp->t_flags & TF_DELACK) {
988 if (tmr_up == PACE_TMR_DELACK)
990 * We are supposed to have delayed ack up
994 } else if (sbavail(&inp->inp_socket->so_snd) &&
995 (tmr_up == PACE_TMR_RXT)) {
997 * if we hit enobufs then we would expect the
998 * possiblity of nothing outstanding and the RXT up
999 * (and the hptsi timer).
1002 } else if (((V_tcp_always_keepalive ||
1003 inp->inp_socket->so_options & SO_KEEPALIVE) &&
1004 (tp->t_state <= TCPS_CLOSING)) &&
1005 (tmr_up == PACE_TMR_KEEP) &&
1006 (tp->snd_max == tp->snd_una)) {
1007 /* We should have keep alive up and we do */
1011 if (rsm && (rsm->r_flags & BBR_SACK_PASSED)) {
1012 if ((tp->t_flags & TF_SENTFIN) &&
1013 ((tp->snd_max - tp->snd_una) == 1) &&
1014 (rsm->r_flags & BBR_HAS_FIN)) {
1015 /* needs to be a RXT */
1016 if (tmr_up == PACE_TMR_RXT)
1020 } else if (tmr_up == PACE_TMR_RACK)
1024 } else if (rsm && (tmr_up == PACE_TMR_RACK)) {
1025 /* Rack timer has priority if we have data out */
1027 } else if (SEQ_GT(tp->snd_max, tp->snd_una) &&
1028 ((tmr_up == PACE_TMR_TLP) ||
1029 (tmr_up == PACE_TMR_RXT))) {
1031 * Either a TLP or RXT is fine if no sack-passed is in place
1032 * and data is outstanding.
1035 } else if (tmr_up == PACE_TMR_DELACK) {
1037 * If the delayed ack was going to go off before the
1038 * rtx/tlp/rack timer were going to expire, then that would
1039 * be the timer in control. Note we don't check the time
1040 * here trusting the code is correct.
1044 if (SEQ_GT(tp->snd_max, tp->snd_una) &&
1045 ((tmr_up == PACE_TMR_RXT) ||
1046 (tmr_up == PACE_TMR_TLP) ||
1047 (tmr_up == PACE_TMR_RACK))) {
1049 * We have outstanding data and
1050 * we *do* have a RACK, TLP or RXT
1051 * timer running. We won't restart
1052 * anything here since thats probably ok we
1053 * will get called with some timer here shortly.
1058 * Ok the timer originally started is not what we want now. We will
1059 * force the hpts to be stopped if any, and restart with the slot
1060 * set to what was in the saved slot.
1063 if ((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) {
1064 if (inp->inp_in_hpts)
1065 tcp_hpts_remove(inp, HPTS_REMOVE_OUTPUT);
1066 bbr_timer_cancel(bbr, __LINE__, cts);
1067 bbr_start_hpts_timer(bbr, tp, cts, 1, bbr->r_ctl.rc_last_delay_val,
1071 * Output is hptsi so we just need to switch the type of
1072 * timer. We don't bother with keep-alive, since when we
1073 * jump through the output, it will start the keep-alive if
1076 * We only need a delayed-ack added and or the hpts_timeout.
1078 hpts_timeout = bbr_timer_start(tp, bbr, cts);
1079 if (tp->t_flags & TF_DELACK) {
1080 if (hpts_timeout == 0) {
1081 hpts_timeout = bbr_delack_time;
1082 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK;
1084 else if (hpts_timeout > bbr_delack_time) {
1085 hpts_timeout = bbr_delack_time;
1086 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK;
1090 if (hpts_timeout > 0x7ffffffe)
1091 hpts_timeout = 0x7ffffffe;
1092 bbr->r_ctl.rc_timer_exp = cts + hpts_timeout;
1097 int32_t bbr_clear_lost = 0;
1100 * Considers the two time values now (cts) and earlier.
1101 * If cts is smaller than earlier, we could have
1102 * had a sequence wrap (our counter wraps every
1103 * 70 min or so) or it could be just clock skew
1104 * getting us two differnt time values. Clock skew
1105 * will show up within 10ms or so. So in such
1106 * a case (where cts is behind earlier time by
1107 * less than 10ms) we return 0. Otherwise we
1108 * return the true difference between them.
1110 static inline uint32_t
1111 bbr_calc_time(uint32_t cts, uint32_t earlier_time) {
1113 * Given two timestamps, the current time stamp cts, and some other
1114 * time-stamp taken in theory earlier return the difference. The
1115 * trick is here sometimes locking will get the other timestamp
1116 * after the cts. If this occurs we need to return 0.
1118 if (TSTMP_GEQ(cts, earlier_time))
1119 return (cts - earlier_time);
1121 * cts is behind earlier_time if its less than 10ms consider it 0.
1122 * If its more than 10ms difference then we had a time wrap. Else
1123 * its just the normal locking foo. I wonder if we should not go to
1124 * 64bit TS and get rid of this issue.
1126 if (TSTMP_GEQ((cts + 10000), earlier_time))
1129 * Ok the time must have wrapped. So we need to answer a large
1130 * amount of time, which the normal subtraction should do.
1132 return (cts - earlier_time);
1138 sysctl_bbr_clear_lost(SYSCTL_HANDLER_ARGS)
1143 error = SYSCTL_OUT(req, &bbr_clear_lost, sizeof(uint32_t));
1144 if (error || req->newptr == NULL)
1147 error = SYSCTL_IN(req, &stat, sizeof(uint32_t));
1151 #ifdef BBR_INVARIANTS
1152 printf("Clearing BBR lost counters\n");
1154 COUNTER_ARRAY_ZERO(bbr_state_lost, BBR_MAX_STAT);
1155 COUNTER_ARRAY_ZERO(bbr_state_time, BBR_MAX_STAT);
1156 COUNTER_ARRAY_ZERO(bbr_state_resend, BBR_MAX_STAT);
1157 } else if (stat == 2) {
1158 #ifdef BBR_INVARIANTS
1159 printf("Clearing BBR option counters\n");
1161 COUNTER_ARRAY_ZERO(bbr_opts_arry, BBR_OPTS_SIZE);
1162 } else if (stat == 3) {
1163 #ifdef BBR_INVARIANTS
1164 printf("Clearing BBR stats counters\n");
1166 COUNTER_ARRAY_ZERO(bbr_stat_arry, BBR_STAT_SIZE);
1167 } else if (stat == 4) {
1168 #ifdef BBR_INVARIANTS
1169 printf("Clearing BBR out-size counters\n");
1171 COUNTER_ARRAY_ZERO(bbr_out_size, TCP_MSS_ACCT_SIZE);
1178 bbr_init_sysctls(void)
1180 struct sysctl_oid *bbr_probertt;
1181 struct sysctl_oid *bbr_hptsi;
1182 struct sysctl_oid *bbr_measure;
1183 struct sysctl_oid *bbr_cwnd;
1184 struct sysctl_oid *bbr_timeout;
1185 struct sysctl_oid *bbr_states;
1186 struct sysctl_oid *bbr_startup;
1187 struct sysctl_oid *bbr_policer;
1189 /* Probe rtt controls */
1190 bbr_probertt = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1191 SYSCTL_CHILDREN(bbr_sysctl_root),
1194 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1196 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1197 SYSCTL_CHILDREN(bbr_probertt),
1198 OID_AUTO, "gain", CTLFLAG_RW,
1199 &bbr_rttprobe_gain, 192,
1200 "What is the filter gain drop in probe_rtt (0=disable)?");
1201 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1202 SYSCTL_CHILDREN(bbr_probertt),
1203 OID_AUTO, "cwnd", CTLFLAG_RW,
1204 &bbr_rtt_probe_cwndtarg, 4,
1205 "How many mss's are outstanding during probe-rtt");
1206 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1207 SYSCTL_CHILDREN(bbr_probertt),
1208 OID_AUTO, "int", CTLFLAG_RW,
1209 &bbr_rtt_probe_limit, 4000000,
1210 "If RTT has not shrank in this many micro-seconds enter probe-rtt");
1211 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1212 SYSCTL_CHILDREN(bbr_probertt),
1213 OID_AUTO, "mintime", CTLFLAG_RW,
1214 &bbr_rtt_probe_time, 200000,
1215 "How many microseconds in probe-rtt");
1216 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1217 SYSCTL_CHILDREN(bbr_probertt),
1218 OID_AUTO, "filter_len_sec", CTLFLAG_RW,
1219 &bbr_filter_len_sec, 6,
1220 "How long in seconds does the rttProp filter run?");
1221 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1222 SYSCTL_CHILDREN(bbr_probertt),
1223 OID_AUTO, "drain_rtt", CTLFLAG_RW,
1224 &bbr_drain_rtt, BBR_SRTT,
1225 "What is the drain rtt to use in probeRTT (rtt_prop=0, rtt_rack=1, rtt_pkt=2, rtt_srtt=3?");
1226 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1227 SYSCTL_CHILDREN(bbr_probertt),
1228 OID_AUTO, "can_force", CTLFLAG_RW,
1229 &bbr_can_force_probertt, 0,
1230 "If we keep setting new low rtt's but delay going in probe-rtt can we force in??");
1231 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1232 SYSCTL_CHILDREN(bbr_probertt),
1233 OID_AUTO, "enter_sets_force", CTLFLAG_RW,
1234 &bbr_probertt_sets_rtt, 0,
1235 "In NF mode, do we imitate google_mode and set the rttProp on entry to probe-rtt?");
1236 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1237 SYSCTL_CHILDREN(bbr_probertt),
1238 OID_AUTO, "can_adjust", CTLFLAG_RW,
1239 &bbr_can_adjust_probertt, 1,
1240 "Can we dynamically adjust the probe-rtt limits and times?");
1241 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1242 SYSCTL_CHILDREN(bbr_probertt),
1243 OID_AUTO, "is_ratio", CTLFLAG_RW,
1245 "is the limit to filter a ratio?");
1246 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1247 SYSCTL_CHILDREN(bbr_probertt),
1248 OID_AUTO, "use_cwnd", CTLFLAG_RW,
1249 &bbr_prtt_slam_cwnd, 0,
1250 "Should we set/recover cwnd?");
1251 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1252 SYSCTL_CHILDREN(bbr_probertt),
1253 OID_AUTO, "can_use_ts", CTLFLAG_RW,
1254 &bbr_can_use_ts_for_rtt, 1,
1255 "Can we use the ms timestamp if available for retransmistted rtt calculations?");
1257 /* Pacing controls */
1258 bbr_hptsi = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1259 SYSCTL_CHILDREN(bbr_sysctl_root),
1262 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1264 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1265 SYSCTL_CHILDREN(bbr_hptsi),
1266 OID_AUTO, "hw_pacing", CTLFLAG_RW,
1267 &bbr_allow_hdwr_pacing, 1,
1268 "Do we allow hardware pacing?");
1269 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1270 SYSCTL_CHILDREN(bbr_hptsi),
1271 OID_AUTO, "hw_pacing_limit", CTLFLAG_RW,
1272 &bbr_hardware_pacing_limit, 4000,
1273 "Do we have a limited number of connections for pacing chelsio (0=no limit)?");
1274 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1275 SYSCTL_CHILDREN(bbr_hptsi),
1276 OID_AUTO, "hw_pacing_adj", CTLFLAG_RW,
1277 &bbr_hdwr_pace_adjust, 2,
1278 "Multiplier to calculated tso size?");
1279 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1280 SYSCTL_CHILDREN(bbr_hptsi),
1281 OID_AUTO, "hw_pacing_floor", CTLFLAG_RW,
1282 &bbr_hdwr_pace_floor, 1,
1283 "Do we invoke the hardware pacing floor?");
1284 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1285 SYSCTL_CHILDREN(bbr_hptsi),
1286 OID_AUTO, "hw_pacing_delay_cnt", CTLFLAG_RW,
1287 &bbr_hdwr_pacing_delay_cnt, 10,
1288 "How many packets must be sent after hdwr pacing is enabled");
1289 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1290 SYSCTL_CHILDREN(bbr_hptsi),
1291 OID_AUTO, "bw_cross", CTLFLAG_RW,
1292 &bbr_cross_over, 3000000,
1293 "What is the point where we cross over to linux like TSO size set");
1294 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1295 SYSCTL_CHILDREN(bbr_hptsi),
1296 OID_AUTO, "seg_deltarg", CTLFLAG_RW,
1297 &bbr_hptsi_segments_delay_tar, 7000,
1298 "What is the worse case delay target for hptsi < 48Mbp connections");
1299 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1300 SYSCTL_CHILDREN(bbr_hptsi),
1301 OID_AUTO, "enet_oh", CTLFLAG_RW,
1302 &bbr_include_enet_oh, 0,
1303 "Do we include the ethernet overhead in calculating pacing delay?");
1304 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1305 SYSCTL_CHILDREN(bbr_hptsi),
1306 OID_AUTO, "ip_oh", CTLFLAG_RW,
1307 &bbr_include_ip_oh, 1,
1308 "Do we include the IP overhead in calculating pacing delay?");
1309 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1310 SYSCTL_CHILDREN(bbr_hptsi),
1311 OID_AUTO, "tcp_oh", CTLFLAG_RW,
1312 &bbr_include_tcp_oh, 0,
1313 "Do we include the TCP overhead in calculating pacing delay?");
1314 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1315 SYSCTL_CHILDREN(bbr_hptsi),
1316 OID_AUTO, "google_discount", CTLFLAG_RW,
1317 &bbr_google_discount, 10,
1318 "What is the default google discount percentage wise for pacing (11 = 1.1%%)?");
1319 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1320 SYSCTL_CHILDREN(bbr_hptsi),
1321 OID_AUTO, "all_get_min", CTLFLAG_RW,
1322 &bbr_all_get_min, 0,
1323 "If you are less than a MSS do you just get the min?");
1324 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1325 SYSCTL_CHILDREN(bbr_hptsi),
1326 OID_AUTO, "tso_min", CTLFLAG_RW,
1327 &bbr_hptsi_bytes_min, 1460,
1328 "For 0 -> 24Mbps what is floor number of segments for TSO");
1329 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1330 SYSCTL_CHILDREN(bbr_hptsi),
1331 OID_AUTO, "seg_tso_max", CTLFLAG_RW,
1332 &bbr_hptsi_segments_max, 6,
1333 "For 0 -> 24Mbps what is top number of segments for TSO");
1334 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1335 SYSCTL_CHILDREN(bbr_hptsi),
1336 OID_AUTO, "seg_floor", CTLFLAG_RW,
1337 &bbr_hptsi_segments_floor, 1,
1338 "Minimum TSO size we will fall too in segments");
1339 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1340 SYSCTL_CHILDREN(bbr_hptsi),
1341 OID_AUTO, "utter_max", CTLFLAG_RW,
1342 &bbr_hptsi_utter_max, 0,
1343 "The absolute maximum that any pacing (outside of hardware) can be");
1344 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1345 SYSCTL_CHILDREN(bbr_hptsi),
1346 OID_AUTO, "seg_divisor", CTLFLAG_RW,
1347 &bbr_hptsi_per_second, 100,
1348 "What is the divisor in our hptsi TSO calculation 512Mbps < X > 24Mbps ");
1349 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1350 SYSCTL_CHILDREN(bbr_hptsi),
1351 OID_AUTO, "srtt_mul", CTLFLAG_RW,
1352 &bbr_hptsi_max_mul, 1,
1353 "The multiplier for pace len max");
1354 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1355 SYSCTL_CHILDREN(bbr_hptsi),
1356 OID_AUTO, "srtt_div", CTLFLAG_RW,
1357 &bbr_hptsi_max_div, 2,
1358 "The divisor for pace len max");
1359 /* Measurement controls */
1360 bbr_measure = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1361 SYSCTL_CHILDREN(bbr_sysctl_root),
1364 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1365 "Measurement controls");
1366 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1367 SYSCTL_CHILDREN(bbr_measure),
1368 OID_AUTO, "min_i_bw", CTLFLAG_RW,
1369 &bbr_initial_bw_bps, 62500,
1370 "Minimum initial b/w in bytes per second");
1371 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1372 SYSCTL_CHILDREN(bbr_measure),
1373 OID_AUTO, "no_sack_needed", CTLFLAG_RW,
1374 &bbr_sack_not_required, 0,
1375 "Do we allow bbr to run on connections not supporting SACK?");
1376 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1377 SYSCTL_CHILDREN(bbr_measure),
1378 OID_AUTO, "use_google", CTLFLAG_RW,
1379 &bbr_use_google_algo, 0,
1380 "Use has close to google V1.0 has possible?");
1381 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1382 SYSCTL_CHILDREN(bbr_measure),
1383 OID_AUTO, "ts_limiting", CTLFLAG_RW,
1384 &bbr_ts_limiting, 1,
1385 "Do we attempt to use the peers timestamp to limit b/w caculations?");
1386 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1387 SYSCTL_CHILDREN(bbr_measure),
1388 OID_AUTO, "ts_can_raise", CTLFLAG_RW,
1389 &bbr_ts_can_raise, 0,
1390 "Can we raise the b/w via timestamp b/w calculation?");
1391 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1392 SYSCTL_CHILDREN(bbr_measure),
1393 OID_AUTO, "ts_delta", CTLFLAG_RW,
1394 &bbr_min_usec_delta, 20000,
1395 "How long in usec between ts of our sends in ts validation code?");
1396 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1397 SYSCTL_CHILDREN(bbr_measure),
1398 OID_AUTO, "ts_peer_delta", CTLFLAG_RW,
1399 &bbr_min_peer_delta, 20,
1400 "What min numerical value should be between the peer deltas?");
1401 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1402 SYSCTL_CHILDREN(bbr_measure),
1403 OID_AUTO, "ts_delta_percent", CTLFLAG_RW,
1404 &bbr_delta_percent, 150,
1405 "What percentage (150 = 15.0) do we allow variance for?");
1406 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1407 SYSCTL_CHILDREN(bbr_measure),
1408 OID_AUTO, "min_measure_good_bw", CTLFLAG_RW,
1409 &bbr_min_measurements_req, 1,
1410 "What is the minimum measurment count we need before we switch to our b/w estimate");
1411 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1412 SYSCTL_CHILDREN(bbr_measure),
1413 OID_AUTO, "min_measure_before_pace", CTLFLAG_RW,
1414 &bbr_no_pacing_until, 4,
1415 "How many pkt-epoch's (0 is off) do we need before pacing is on?");
1416 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1417 SYSCTL_CHILDREN(bbr_measure),
1418 OID_AUTO, "quanta", CTLFLAG_RW,
1420 "Extra quanta to add when calculating the target (ID section 4.2.3.2).");
1421 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1422 SYSCTL_CHILDREN(bbr_measure),
1423 OID_AUTO, "noretran", CTLFLAG_RW,
1425 "Should google mode not use retransmission measurements for the b/w estimation?");
1426 /* State controls */
1427 bbr_states = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1428 SYSCTL_CHILDREN(bbr_sysctl_root),
1431 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1433 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1434 SYSCTL_CHILDREN(bbr_states),
1435 OID_AUTO, "idle_restart", CTLFLAG_RW,
1436 &bbr_uses_idle_restart, 0,
1437 "Do we use a new special idle_restart state to ramp back up quickly?");
1438 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1439 SYSCTL_CHILDREN(bbr_states),
1440 OID_AUTO, "idle_restart_threshold", CTLFLAG_RW,
1441 &bbr_idle_restart_threshold, 100000,
1442 "How long must we be idle before we restart??");
1443 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1444 SYSCTL_CHILDREN(bbr_states),
1445 OID_AUTO, "use_pkt_epoch", CTLFLAG_RW,
1446 &bbr_state_is_pkt_epoch, 0,
1447 "Do we use a pkt-epoch for substate if 0 rttProp?");
1448 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1449 SYSCTL_CHILDREN(bbr_states),
1450 OID_AUTO, "startup_rtt_gain", CTLFLAG_RW,
1451 &bbr_rtt_gain_thresh, 0,
1452 "What increase in RTT triggers us to stop ignoring no-loss and possibly exit startup?");
1453 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1454 SYSCTL_CHILDREN(bbr_states),
1455 OID_AUTO, "drain_floor", CTLFLAG_RW,
1456 &bbr_drain_floor, 88,
1457 "What is the lowest we can drain (pg) too?");
1458 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1459 SYSCTL_CHILDREN(bbr_states),
1460 OID_AUTO, "drain_2_target", CTLFLAG_RW,
1461 &bbr_state_drain_2_tar, 1,
1462 "Do we drain to target in drain substate?");
1463 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1464 SYSCTL_CHILDREN(bbr_states),
1465 OID_AUTO, "gain_2_target", CTLFLAG_RW,
1466 &bbr_gain_to_target, 1,
1467 "Does probe bw gain to target??");
1468 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1469 SYSCTL_CHILDREN(bbr_states),
1470 OID_AUTO, "gain_extra_time", CTLFLAG_RW,
1471 &bbr_gain_gets_extra_too, 1,
1472 "Does probe bw gain get the extra time too?");
1473 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1474 SYSCTL_CHILDREN(bbr_states),
1475 OID_AUTO, "ld_div", CTLFLAG_RW,
1476 &bbr_drain_drop_div, 5,
1477 "Long drain drop divider?");
1478 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1479 SYSCTL_CHILDREN(bbr_states),
1480 OID_AUTO, "ld_mul", CTLFLAG_RW,
1481 &bbr_drain_drop_mul, 4,
1482 "Long drain drop multiplier?");
1483 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1484 SYSCTL_CHILDREN(bbr_states),
1485 OID_AUTO, "rand_ot_disc", CTLFLAG_RW,
1487 "Random discount of the ot?");
1488 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1489 SYSCTL_CHILDREN(bbr_states),
1490 OID_AUTO, "dr_filter_life", CTLFLAG_RW,
1491 &bbr_num_pktepo_for_del_limit, BBR_NUM_RTTS_FOR_DEL_LIMIT,
1492 "How many packet-epochs does the b/w delivery rate last?");
1493 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1494 SYSCTL_CHILDREN(bbr_states),
1495 OID_AUTO, "subdrain_applimited", CTLFLAG_RW,
1496 &bbr_sub_drain_app_limit, 0,
1497 "Does our sub-state drain invoke app limited if its long?");
1498 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1499 SYSCTL_CHILDREN(bbr_states),
1500 OID_AUTO, "use_cwnd_subdrain", CTLFLAG_RW,
1501 &bbr_sub_drain_slam_cwnd, 0,
1502 "Should we set/recover cwnd for sub-state drain?");
1503 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1504 SYSCTL_CHILDREN(bbr_states),
1505 OID_AUTO, "use_cwnd_maindrain", CTLFLAG_RW,
1506 &bbr_slam_cwnd_in_main_drain, 0,
1507 "Should we set/recover cwnd for main-state drain?");
1508 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1509 SYSCTL_CHILDREN(bbr_states),
1510 OID_AUTO, "google_gets_earlyout", CTLFLAG_RW,
1511 &google_allow_early_out, 1,
1512 "Should we allow google probe-bw/drain to exit early at flight target?");
1513 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1514 SYSCTL_CHILDREN(bbr_states),
1515 OID_AUTO, "google_exit_loss", CTLFLAG_RW,
1516 &google_consider_lost, 1,
1517 "Should we have losses exit gain of probebw in google mode??");
1518 /* Startup controls */
1519 bbr_startup = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1520 SYSCTL_CHILDREN(bbr_sysctl_root),
1523 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1524 "Startup controls");
1525 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1526 SYSCTL_CHILDREN(bbr_startup),
1527 OID_AUTO, "cheat_iwnd", CTLFLAG_RW,
1528 &bbr_sends_full_iwnd, 1,
1529 "Do we not pace but burst out initial windows has our TSO size?");
1530 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1531 SYSCTL_CHILDREN(bbr_startup),
1532 OID_AUTO, "loss_threshold", CTLFLAG_RW,
1533 &bbr_startup_loss_thresh, 2000,
1534 "In startup what is the loss threshold in a pe that will exit us from startup?");
1535 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1536 SYSCTL_CHILDREN(bbr_startup),
1537 OID_AUTO, "use_lowerpg", CTLFLAG_RW,
1538 &bbr_use_lower_gain_in_startup, 1,
1539 "Should we use a lower hptsi gain if we see loss in startup?");
1540 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1541 SYSCTL_CHILDREN(bbr_startup),
1542 OID_AUTO, "gain", CTLFLAG_RW,
1543 &bbr_start_exit, 25,
1544 "What gain percent do we need to see to stay in startup??");
1545 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1546 SYSCTL_CHILDREN(bbr_startup),
1547 OID_AUTO, "low_gain", CTLFLAG_RW,
1548 &bbr_low_start_exit, 15,
1549 "What gain percent do we need to see to stay in the lower gain startup??");
1550 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1551 SYSCTL_CHILDREN(bbr_startup),
1552 OID_AUTO, "loss_exit", CTLFLAG_RW,
1553 &bbr_exit_startup_at_loss, 1,
1554 "Should we exit startup at loss in an epoch if we are not gaining?");
1556 bbr_cwnd = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1557 SYSCTL_CHILDREN(bbr_sysctl_root),
1560 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1562 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1563 SYSCTL_CHILDREN(bbr_cwnd),
1564 OID_AUTO, "tar_rtt", CTLFLAG_RW,
1565 &bbr_cwndtarget_rtt_touse, 0,
1566 "Target cwnd rtt measurment to use (0=rtt_prop, 1=rtt_rack, 2=pkt_rtt, 3=srtt)?");
1567 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1568 SYSCTL_CHILDREN(bbr_cwnd),
1569 OID_AUTO, "may_shrink", CTLFLAG_RW,
1570 &bbr_cwnd_may_shrink, 0,
1571 "Can the cwnd shrink if it would grow to more than the target?");
1572 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1573 SYSCTL_CHILDREN(bbr_cwnd),
1574 OID_AUTO, "max_target_limit", CTLFLAG_RW,
1575 &bbr_target_cwnd_mult_limit, 8,
1576 "Do we limit the cwnd to some multiple of the cwnd target if cwnd can't shrink 0=no?");
1577 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1578 SYSCTL_CHILDREN(bbr_cwnd),
1579 OID_AUTO, "highspeed_min", CTLFLAG_RW,
1580 &bbr_cwnd_min_val_hs, BBR_HIGHSPEED_NUM_MSS,
1581 "What is the high-speed min cwnd (rttProp under 1ms)");
1582 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1583 SYSCTL_CHILDREN(bbr_cwnd),
1584 OID_AUTO, "lowspeed_min", CTLFLAG_RW,
1585 &bbr_cwnd_min_val, BBR_PROBERTT_NUM_MSS,
1586 "What is the min cwnd (rttProp > 1ms)");
1587 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1588 SYSCTL_CHILDREN(bbr_cwnd),
1589 OID_AUTO, "initwin", CTLFLAG_RW,
1590 &bbr_def_init_win, 10,
1591 "What is the BBR initial window, if 0 use tcp version");
1592 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1593 SYSCTL_CHILDREN(bbr_cwnd),
1594 OID_AUTO, "do_loss_red", CTLFLAG_RW,
1596 "Do we reduce the b/w at exit from recovery based on ratio of prop/srtt (800=80.0, 0=off)?");
1597 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1598 SYSCTL_CHILDREN(bbr_cwnd),
1599 OID_AUTO, "red_scale", CTLFLAG_RW,
1600 &bbr_red_scale, 20000,
1601 "What RTT do we scale with?");
1602 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1603 SYSCTL_CHILDREN(bbr_cwnd),
1604 OID_AUTO, "red_growslow", CTLFLAG_RW,
1605 &bbr_red_growth_restrict, 1,
1606 "Do we restrict cwnd growth for whats in flight?");
1607 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1608 SYSCTL_CHILDREN(bbr_cwnd),
1609 OID_AUTO, "red_div", CTLFLAG_RW,
1611 "If we reduce whats the divisor?");
1612 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1613 SYSCTL_CHILDREN(bbr_cwnd),
1614 OID_AUTO, "red_mul", CTLFLAG_RW,
1616 "If we reduce whats the mulitiplier?");
1617 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1618 SYSCTL_CHILDREN(bbr_cwnd),
1619 OID_AUTO, "target_is_unit", CTLFLAG_RW,
1620 &bbr_target_is_bbunit, 0,
1621 "Is the state target the pacing_gain or BBR_UNIT?");
1622 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1623 SYSCTL_CHILDREN(bbr_cwnd),
1624 OID_AUTO, "drop_limit", CTLFLAG_RW,
1626 "Number of segments limit for drop (0=use min_cwnd w/flight)?");
1628 /* Timeout controls */
1629 bbr_timeout = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1630 SYSCTL_CHILDREN(bbr_sysctl_root),
1633 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1634 "Time out controls");
1635 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1636 SYSCTL_CHILDREN(bbr_timeout),
1637 OID_AUTO, "delack", CTLFLAG_RW,
1638 &bbr_delack_time, 100000,
1639 "BBR's delayed ack time");
1640 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1641 SYSCTL_CHILDREN(bbr_timeout),
1642 OID_AUTO, "tlp_uses", CTLFLAG_RW,
1643 &bbr_tlp_type_to_use, 3,
1644 "RTT that TLP uses in its calculations, 0=rttProp, 1=Rack_rtt, 2=pkt_rtt and 3=srtt");
1645 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1646 SYSCTL_CHILDREN(bbr_timeout),
1647 OID_AUTO, "persmin", CTLFLAG_RW,
1648 &bbr_persist_min, 250000,
1649 "What is the minimum time in microseconds between persists");
1650 SYSCTL_ADD_U32(&bbr_sysctl_ctx,
1651 SYSCTL_CHILDREN(bbr_timeout),
1652 OID_AUTO, "persmax", CTLFLAG_RW,
1653 &bbr_persist_max, 1000000,
1654 "What is the largest delay in microseconds between persists");
1655 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1656 SYSCTL_CHILDREN(bbr_timeout),
1657 OID_AUTO, "tlp_minto", CTLFLAG_RW,
1658 &bbr_tlp_min, 10000,
1659 "TLP Min timeout in usecs");
1660 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1661 SYSCTL_CHILDREN(bbr_timeout),
1662 OID_AUTO, "tlp_dack_time", CTLFLAG_RW,
1663 &bbr_delayed_ack_time, 200000,
1664 "TLP delayed ack compensation value");
1665 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1666 SYSCTL_CHILDREN(bbr_sysctl_root),
1667 OID_AUTO, "minrto", CTLFLAG_RW,
1668 &bbr_rto_min_ms, 30,
1669 "Minimum RTO in ms");
1670 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1671 SYSCTL_CHILDREN(bbr_timeout),
1672 OID_AUTO, "maxrto", CTLFLAG_RW,
1673 &bbr_rto_max_sec, 4,
1674 "Maxiumum RTO in seconds -- should be at least as large as min_rto");
1675 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1676 SYSCTL_CHILDREN(bbr_timeout),
1677 OID_AUTO, "tlp_retry", CTLFLAG_RW,
1678 &bbr_tlp_max_resend, 2,
1679 "How many times does TLP retry a single segment or multiple with no ACK");
1680 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1681 SYSCTL_CHILDREN(bbr_timeout),
1682 OID_AUTO, "minto", CTLFLAG_RW,
1684 "Minimum rack timeout in useconds");
1685 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1686 SYSCTL_CHILDREN(bbr_timeout),
1687 OID_AUTO, "pktdelay", CTLFLAG_RW,
1688 &bbr_pkt_delay, 1000,
1689 "Extra RACK time (in useconds) besides reordering thresh");
1690 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1691 SYSCTL_CHILDREN(bbr_timeout),
1692 OID_AUTO, "incr_tmrs", CTLFLAG_RW,
1693 &bbr_incr_timers, 1,
1694 "Increase the RXT/TLP timer by the pacing time used?");
1695 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1696 SYSCTL_CHILDREN(bbr_timeout),
1697 OID_AUTO, "rxtmark_sackpassed", CTLFLAG_RW,
1698 &bbr_marks_rxt_sack_passed, 0,
1699 "Mark sack passed on all those not ack'd when a RXT hits?");
1700 /* Policer controls */
1701 bbr_policer = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
1702 SYSCTL_CHILDREN(bbr_sysctl_root),
1705 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1706 "Policer controls");
1707 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1708 SYSCTL_CHILDREN(bbr_policer),
1709 OID_AUTO, "detect_enable", CTLFLAG_RW,
1710 &bbr_policer_detection_enabled, 1,
1711 "Is policer detection enabled??");
1712 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1713 SYSCTL_CHILDREN(bbr_policer),
1714 OID_AUTO, "min_pes", CTLFLAG_RW,
1715 &bbr_lt_intvl_min_rtts, 4,
1716 "Minimum number of PE's?");
1717 SYSCTL_ADD_U64(&bbr_sysctl_ctx,
1718 SYSCTL_CHILDREN(bbr_policer),
1719 OID_AUTO, "bwdiff", CTLFLAG_RW,
1720 &bbr_lt_bw_diff, (4000/8),
1721 "Minimal bw diff?");
1722 SYSCTL_ADD_U64(&bbr_sysctl_ctx,
1723 SYSCTL_CHILDREN(bbr_policer),
1724 OID_AUTO, "bwratio", CTLFLAG_RW,
1725 &bbr_lt_bw_ratio, 8,
1726 "Minimal bw diff?");
1727 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1728 SYSCTL_CHILDREN(bbr_policer),
1729 OID_AUTO, "from_rack_rxt", CTLFLAG_RW,
1730 &bbr_policer_call_from_rack_to, 0,
1731 "Do we call the policer detection code from a rack-timeout?");
1732 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1733 SYSCTL_CHILDREN(bbr_policer),
1734 OID_AUTO, "false_postive", CTLFLAG_RW,
1735 &bbr_lt_intvl_fp, 0,
1736 "What packet epoch do we do false-postive detection at (0=no)?");
1737 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1738 SYSCTL_CHILDREN(bbr_policer),
1739 OID_AUTO, "loss_thresh", CTLFLAG_RW,
1740 &bbr_lt_loss_thresh, 196,
1741 "Loss threshold 196 = 19.6%?");
1742 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1743 SYSCTL_CHILDREN(bbr_policer),
1744 OID_AUTO, "false_postive_thresh", CTLFLAG_RW,
1745 &bbr_lt_fd_thresh, 100,
1746 "What percentage is the false detection threshold (150=15.0)?");
1748 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1749 SYSCTL_CHILDREN(bbr_sysctl_root),
1750 OID_AUTO, "cheat_rxt", CTLFLAG_RW,
1751 &bbr_use_rack_resend_cheat, 0,
1752 "Do we burst 1ms between sends on retransmissions (like rack)?");
1753 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1754 SYSCTL_CHILDREN(bbr_sysctl_root),
1755 OID_AUTO, "error_paceout", CTLFLAG_RW,
1756 &bbr_error_base_paceout, 10000,
1757 "When we hit an error what is the min to pace out in usec's?");
1758 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1759 SYSCTL_CHILDREN(bbr_sysctl_root),
1760 OID_AUTO, "kill_paceout", CTLFLAG_RW,
1761 &bbr_max_net_error_cnt, 10,
1762 "When we hit this many errors in a row, kill the session?");
1763 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1764 SYSCTL_CHILDREN(bbr_sysctl_root),
1765 OID_AUTO, "data_after_close", CTLFLAG_RW,
1766 &bbr_ignore_data_after_close, 1,
1767 "Do we hold off sending a RST until all pending data is ack'd");
1768 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1769 SYSCTL_CHILDREN(bbr_sysctl_root),
1770 OID_AUTO, "resend_use_tso", CTLFLAG_RW,
1771 &bbr_resends_use_tso, 0,
1772 "Can resends use TSO?");
1773 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1774 SYSCTL_CHILDREN(bbr_sysctl_root),
1775 OID_AUTO, "sblklimit", CTLFLAG_RW,
1776 &bbr_sack_block_limit, 128,
1777 "When do we start ignoring small sack blocks");
1778 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1779 SYSCTL_CHILDREN(bbr_sysctl_root),
1780 OID_AUTO, "bb_verbose", CTLFLAG_RW,
1781 &bbr_verbose_logging, 0,
1782 "Should BBR black box logging be verbose");
1783 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1784 SYSCTL_CHILDREN(bbr_sysctl_root),
1785 OID_AUTO, "reorder_thresh", CTLFLAG_RW,
1786 &bbr_reorder_thresh, 2,
1787 "What factor for rack will be added when seeing reordering (shift right)");
1788 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1789 SYSCTL_CHILDREN(bbr_sysctl_root),
1790 OID_AUTO, "reorder_fade", CTLFLAG_RW,
1791 &bbr_reorder_fade, 0,
1792 "Does reorder detection fade, if so how many ms (0 means never)");
1793 SYSCTL_ADD_S32(&bbr_sysctl_ctx,
1794 SYSCTL_CHILDREN(bbr_sysctl_root),
1795 OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW,
1797 "what divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)");
1798 /* Stats and counters */
1799 /* The pacing counters for hdwr/software can't be in the array */
1800 bbr_nohdwr_pacing_enobuf = counter_u64_alloc(M_WAITOK);
1801 bbr_hdwr_pacing_enobuf = counter_u64_alloc(M_WAITOK);
1802 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx,
1803 SYSCTL_CHILDREN(bbr_sysctl_root),
1804 OID_AUTO, "enob_hdwr_pacing", CTLFLAG_RD,
1805 &bbr_hdwr_pacing_enobuf,
1806 "Total number of enobufs for hardware paced flows");
1807 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx,
1808 SYSCTL_CHILDREN(bbr_sysctl_root),
1809 OID_AUTO, "enob_no_hdwr_pacing", CTLFLAG_RD,
1810 &bbr_nohdwr_pacing_enobuf,
1811 "Total number of enobufs for non-hardware paced flows");
1814 bbr_flows_whdwr_pacing = counter_u64_alloc(M_WAITOK);
1815 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx,
1816 SYSCTL_CHILDREN(bbr_sysctl_root),
1817 OID_AUTO, "hdwr_pacing", CTLFLAG_RD,
1818 &bbr_flows_whdwr_pacing,
1819 "Total number of hardware paced flows");
1820 bbr_flows_nohdwr_pacing = counter_u64_alloc(M_WAITOK);
1821 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx,
1822 SYSCTL_CHILDREN(bbr_sysctl_root),
1823 OID_AUTO, "software_pacing", CTLFLAG_RD,
1824 &bbr_flows_nohdwr_pacing,
1825 "Total number of software paced flows");
1826 COUNTER_ARRAY_ALLOC(bbr_stat_arry, BBR_STAT_SIZE, M_WAITOK);
1827 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1828 OID_AUTO, "stats", CTLFLAG_RD,
1829 bbr_stat_arry, BBR_STAT_SIZE, "BBR Stats");
1830 COUNTER_ARRAY_ALLOC(bbr_opts_arry, BBR_OPTS_SIZE, M_WAITOK);
1831 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1832 OID_AUTO, "opts", CTLFLAG_RD,
1833 bbr_opts_arry, BBR_OPTS_SIZE, "BBR Option Stats");
1834 COUNTER_ARRAY_ALLOC(bbr_state_lost, BBR_MAX_STAT, M_WAITOK);
1835 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1836 OID_AUTO, "lost", CTLFLAG_RD,
1837 bbr_state_lost, BBR_MAX_STAT, "Stats of when losses occur");
1838 COUNTER_ARRAY_ALLOC(bbr_state_resend, BBR_MAX_STAT, M_WAITOK);
1839 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1840 OID_AUTO, "stateresend", CTLFLAG_RD,
1841 bbr_state_resend, BBR_MAX_STAT, "Stats of what states resend");
1842 COUNTER_ARRAY_ALLOC(bbr_state_time, BBR_MAX_STAT, M_WAITOK);
1843 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1844 OID_AUTO, "statetime", CTLFLAG_RD,
1845 bbr_state_time, BBR_MAX_STAT, "Stats of time spent in the states");
1846 COUNTER_ARRAY_ALLOC(bbr_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK);
1847 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root),
1848 OID_AUTO, "outsize", CTLFLAG_RD,
1849 bbr_out_size, TCP_MSS_ACCT_SIZE, "Size of output calls");
1850 SYSCTL_ADD_PROC(&bbr_sysctl_ctx,
1851 SYSCTL_CHILDREN(bbr_sysctl_root),
1852 OID_AUTO, "clrlost", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1853 &bbr_clear_lost, 0, sysctl_bbr_clear_lost, "IU", "Clear lost counters");
1857 bbr_counter_destroy(void)
1859 COUNTER_ARRAY_FREE(bbr_stat_arry, BBR_STAT_SIZE);
1860 COUNTER_ARRAY_FREE(bbr_opts_arry, BBR_OPTS_SIZE);
1861 COUNTER_ARRAY_FREE(bbr_out_size, TCP_MSS_ACCT_SIZE);
1862 COUNTER_ARRAY_FREE(bbr_state_lost, BBR_MAX_STAT);
1863 COUNTER_ARRAY_FREE(bbr_state_time, BBR_MAX_STAT);
1864 COUNTER_ARRAY_FREE(bbr_state_resend, BBR_MAX_STAT);
1865 counter_u64_free(bbr_nohdwr_pacing_enobuf);
1866 counter_u64_free(bbr_hdwr_pacing_enobuf);
1867 counter_u64_free(bbr_flows_whdwr_pacing);
1868 counter_u64_free(bbr_flows_nohdwr_pacing);
1872 static __inline void
1873 bbr_fill_in_logging_data(struct tcp_bbr *bbr, struct tcp_log_bbr *l, uint32_t cts)
1875 memset(l, 0, sizeof(union tcp_log_stackspecific));
1876 l->cur_del_rate = bbr->r_ctl.rc_bbr_cur_del_rate;
1877 l->delRate = get_filter_value(&bbr->r_ctl.rc_delrate);
1878 l->rttProp = get_filter_value_small(&bbr->r_ctl.rc_rttprop);
1879 l->bw_inuse = bbr_get_bw(bbr);
1880 l->inflight = ctf_flight_size(bbr->rc_tp,
1881 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
1882 l->applimited = bbr->r_ctl.r_app_limited_until;
1883 l->delivered = bbr->r_ctl.rc_delivered;
1885 l->lost = bbr->r_ctl.rc_lost;
1886 l->bbr_state = bbr->rc_bbr_state;
1887 l->bbr_substate = bbr_state_val(bbr);
1888 l->epoch = bbr->r_ctl.rc_rtt_epoch;
1889 l->lt_epoch = bbr->r_ctl.rc_lt_epoch;
1890 l->pacing_gain = bbr->r_ctl.rc_bbr_hptsi_gain;
1891 l->cwnd_gain = bbr->r_ctl.rc_bbr_cwnd_gain;
1892 l->inhpts = bbr->rc_inp->inp_in_hpts;
1893 l->ininput = bbr->rc_inp->inp_in_input;
1894 l->use_lt_bw = bbr->rc_lt_use_bw;
1895 l->pkts_out = bbr->r_ctl.rc_flight_at_input;
1896 l->pkt_epoch = bbr->r_ctl.rc_pkt_epoch;
1900 bbr_log_type_bw_reduce(struct tcp_bbr *bbr, int reason)
1902 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1903 union tcp_log_stackspecific log;
1905 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
1906 log.u_bbr.flex1 = 0;
1907 log.u_bbr.flex2 = 0;
1908 log.u_bbr.flex5 = 0;
1909 log.u_bbr.flex3 = 0;
1910 log.u_bbr.flex4 = bbr->r_ctl.rc_pkt_epoch_loss_rate;
1911 log.u_bbr.flex7 = reason;
1912 log.u_bbr.flex6 = bbr->r_ctl.rc_bbr_enters_probertt;
1913 log.u_bbr.flex8 = 0;
1914 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
1915 &bbr->rc_inp->inp_socket->so_rcv,
1916 &bbr->rc_inp->inp_socket->so_snd,
1917 BBR_LOG_BW_RED_EV, 0,
1918 0, &log, false, &bbr->rc_tv);
1923 bbr_log_type_rwnd_collapse(struct tcp_bbr *bbr, int seq, int mode, uint32_t count)
1925 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1926 union tcp_log_stackspecific log;
1928 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
1929 log.u_bbr.flex1 = seq;
1930 log.u_bbr.flex2 = count;
1931 log.u_bbr.flex8 = mode;
1932 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
1933 &bbr->rc_inp->inp_socket->so_rcv,
1934 &bbr->rc_inp->inp_socket->so_snd,
1936 0, &log, false, &bbr->rc_tv);
1943 bbr_log_type_just_return(struct tcp_bbr *bbr, uint32_t cts, uint32_t tlen, uint8_t hpts_calling,
1944 uint8_t reason, uint32_t p_maxseg, int len)
1946 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1947 union tcp_log_stackspecific log;
1949 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
1950 log.u_bbr.flex1 = p_maxseg;
1951 log.u_bbr.flex2 = bbr->r_ctl.rc_hpts_flags;
1952 log.u_bbr.flex3 = bbr->r_ctl.rc_timer_exp;
1953 log.u_bbr.flex4 = reason;
1954 log.u_bbr.flex5 = bbr->rc_in_persist;
1955 log.u_bbr.flex6 = bbr->r_ctl.rc_last_delay_val;
1956 log.u_bbr.flex7 = p_maxseg;
1957 log.u_bbr.flex8 = bbr->rc_in_persist;
1958 log.u_bbr.pkts_out = 0;
1959 log.u_bbr.applimited = len;
1960 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
1961 &bbr->rc_inp->inp_socket->so_rcv,
1962 &bbr->rc_inp->inp_socket->so_snd,
1964 tlen, &log, false, &bbr->rc_tv);
1970 bbr_log_type_enter_rec(struct tcp_bbr *bbr, uint32_t seq)
1972 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1973 union tcp_log_stackspecific log;
1975 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
1976 log.u_bbr.flex1 = seq;
1977 log.u_bbr.flex2 = bbr->r_ctl.rc_cwnd_on_ent;
1978 log.u_bbr.flex3 = bbr->r_ctl.rc_recovery_start;
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 0, &log, false, &bbr->rc_tv);
1988 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)
1990 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
1991 union tcp_log_stackspecific log;
1993 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
1994 log.u_bbr.flex1 = tso;
1995 log.u_bbr.flex2 = maxseg;
1996 log.u_bbr.flex3 = mtu;
1997 log.u_bbr.flex4 = csum_flags;
1998 TCP_LOG_EVENTP(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_flowend(struct tcp_bbr *bbr)
2009 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2010 union tcp_log_stackspecific log;
2011 struct sockbuf *r, *s;
2014 if (bbr->rc_inp->inp_socket) {
2015 r = &bbr->rc_inp->inp_socket->so_rcv;
2016 s = &bbr->rc_inp->inp_socket->so_snd;
2020 bbr_fill_in_logging_data(bbr, &log.u_bbr, tcp_get_usecs(&tv));
2021 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2024 0, &log, false, &tv);
2029 bbr_log_pkt_epoch(struct tcp_bbr *bbr, uint32_t cts, uint32_t line,
2030 uint32_t lost, uint32_t del)
2032 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2033 union tcp_log_stackspecific log;
2035 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2036 log.u_bbr.flex1 = lost;
2037 log.u_bbr.flex2 = del;
2038 log.u_bbr.flex3 = bbr->r_ctl.rc_bbr_lastbtlbw;
2039 log.u_bbr.flex4 = bbr->r_ctl.rc_pkt_epoch_rtt;
2040 log.u_bbr.flex5 = bbr->r_ctl.rc_bbr_last_startup_epoch;
2041 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup;
2042 log.u_bbr.flex7 = line;
2043 log.u_bbr.flex8 = 0;
2044 log.u_bbr.inflight = bbr->r_ctl.r_measurement_count;
2045 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2046 &bbr->rc_inp->inp_socket->so_rcv,
2047 &bbr->rc_inp->inp_socket->so_snd,
2048 BBR_LOG_PKT_EPOCH, 0,
2049 0, &log, false, &bbr->rc_tv);
2054 bbr_log_time_epoch(struct tcp_bbr *bbr, uint32_t cts, uint32_t line, uint32_t epoch_time)
2056 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2057 union tcp_log_stackspecific log;
2059 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2060 log.u_bbr.flex1 = bbr->r_ctl.rc_lost;
2061 log.u_bbr.flex2 = bbr->rc_inp->inp_socket->so_snd.sb_lowat;
2062 log.u_bbr.flex3 = bbr->rc_inp->inp_socket->so_snd.sb_hiwat;
2063 log.u_bbr.flex7 = line;
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_TIME_EPOCH, 0,
2068 0, &log, false, &bbr->rc_tv);
2073 bbr_log_set_of_state_target(struct tcp_bbr *bbr, uint32_t new_tar, int line, int meth)
2075 if (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, bbr->r_ctl.rc_rcvtime);
2079 log.u_bbr.flex1 = bbr->r_ctl.rc_target_at_state;
2080 log.u_bbr.flex2 = new_tar;
2081 log.u_bbr.flex3 = line;
2082 log.u_bbr.flex4 = bbr->r_ctl.rc_pace_max_segs;
2083 log.u_bbr.flex5 = bbr_quanta;
2084 log.u_bbr.flex6 = bbr->r_ctl.rc_pace_min_segs;
2085 log.u_bbr.flex7 = bbr->rc_last_options;
2086 log.u_bbr.flex8 = meth;
2087 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2088 &bbr->rc_inp->inp_socket->so_rcv,
2089 &bbr->rc_inp->inp_socket->so_snd,
2090 BBR_LOG_STATE_TARGET, 0,
2091 0, &log, false, &bbr->rc_tv);
2097 bbr_log_type_statechange(struct tcp_bbr *bbr, uint32_t cts, int32_t line)
2099 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2100 union tcp_log_stackspecific log;
2102 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2103 log.u_bbr.flex1 = line;
2104 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks;
2105 log.u_bbr.flex3 = bbr->r_ctl.rc_probertt_int;
2106 if (bbr_state_is_pkt_epoch)
2107 log.u_bbr.flex4 = bbr_get_rtt(bbr, BBR_RTT_PKTRTT);
2109 log.u_bbr.flex4 = bbr_get_rtt(bbr, BBR_RTT_PROP);
2110 log.u_bbr.flex5 = bbr->r_ctl.rc_bbr_last_startup_epoch;
2111 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup;
2112 log.u_bbr.flex7 = (bbr->r_ctl.rc_target_at_state/1000);
2113 log.u_bbr.lt_epoch = bbr->r_ctl.rc_level_state_extra;
2114 log.u_bbr.pkts_out = bbr->r_ctl.rc_target_at_state;
2115 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2116 &bbr->rc_inp->inp_socket->so_rcv,
2117 &bbr->rc_inp->inp_socket->so_snd,
2119 0, &log, false, &bbr->rc_tv);
2124 bbr_log_rtt_shrinks(struct tcp_bbr *bbr, uint32_t cts, uint32_t applied,
2125 uint32_t rtt, uint32_t line, uint8_t reas, uint16_t cond)
2127 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2128 union tcp_log_stackspecific log;
2130 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2131 log.u_bbr.flex1 = line;
2132 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks;
2133 log.u_bbr.flex3 = bbr->r_ctl.last_in_probertt;
2134 log.u_bbr.flex4 = applied;
2135 log.u_bbr.flex5 = rtt;
2136 log.u_bbr.flex6 = bbr->r_ctl.rc_target_at_state;
2137 log.u_bbr.flex7 = cond;
2138 log.u_bbr.flex8 = reas;
2139 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2140 &bbr->rc_inp->inp_socket->so_rcv,
2141 &bbr->rc_inp->inp_socket->so_snd,
2142 BBR_LOG_RTT_SHRINKS, 0,
2143 0, &log, false, &bbr->rc_tv);
2148 bbr_log_type_exit_rec(struct tcp_bbr *bbr)
2150 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2151 union tcp_log_stackspecific log;
2153 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2154 log.u_bbr.flex1 = bbr->r_ctl.rc_recovery_start;
2155 log.u_bbr.flex2 = bbr->r_ctl.rc_cwnd_on_ent;
2156 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2157 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2158 &bbr->rc_inp->inp_socket->so_rcv,
2159 &bbr->rc_inp->inp_socket->so_snd,
2161 0, &log, false, &bbr->rc_tv);
2166 bbr_log_type_cwndupd(struct tcp_bbr *bbr, uint32_t bytes_this_ack, uint32_t chg,
2167 uint32_t prev_acked, int32_t meth, uint32_t target, uint32_t th_ack, int32_t line)
2169 if (bbr_verbose_logging && (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 = line;
2174 log.u_bbr.flex2 = prev_acked;
2175 log.u_bbr.flex3 = bytes_this_ack;
2176 log.u_bbr.flex4 = chg;
2177 log.u_bbr.flex5 = th_ack;
2178 log.u_bbr.flex6 = target;
2179 log.u_bbr.flex8 = meth;
2180 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2181 &bbr->rc_inp->inp_socket->so_rcv,
2182 &bbr->rc_inp->inp_socket->so_snd,
2184 0, &log, false, &bbr->rc_tv);
2189 bbr_log_rtt_sample(struct tcp_bbr *bbr, uint32_t rtt, uint32_t tsin)
2192 * Log the rtt sample we are applying to the srtt algorithm in
2195 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2196 union tcp_log_stackspecific log;
2198 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2199 log.u_bbr.flex1 = rtt;
2200 log.u_bbr.flex2 = bbr->r_ctl.rc_bbr_state_time;
2201 log.u_bbr.flex3 = bbr->r_ctl.rc_ack_hdwr_delay;
2202 log.u_bbr.flex4 = bbr->rc_tp->ts_offset;
2203 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2204 log.u_bbr.pkts_out = tcp_tv_to_mssectick(&bbr->rc_tv);
2205 log.u_bbr.flex6 = tsin;
2206 log.u_bbr.flex7 = 0;
2207 log.u_bbr.flex8 = bbr->rc_ack_was_delayed;
2208 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2209 &bbr->rc_inp->inp_socket->so_rcv,
2210 &bbr->rc_inp->inp_socket->so_snd,
2212 0, &log, false, &bbr->rc_tv);
2217 bbr_log_type_pesist(struct tcp_bbr *bbr, uint32_t cts, uint32_t time_in, int32_t line, uint8_t enter_exit)
2219 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2220 union tcp_log_stackspecific log;
2222 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2223 log.u_bbr.flex1 = time_in;
2224 log.u_bbr.flex2 = line;
2225 log.u_bbr.flex8 = enter_exit;
2226 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2227 &bbr->rc_inp->inp_socket->so_rcv,
2228 &bbr->rc_inp->inp_socket->so_snd,
2230 0, &log, false, &bbr->rc_tv);
2234 bbr_log_ack_clear(struct tcp_bbr *bbr, uint32_t cts)
2236 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2237 union tcp_log_stackspecific log;
2239 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2240 log.u_bbr.flex1 = bbr->rc_tp->ts_recent_age;
2241 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks;
2242 log.u_bbr.flex3 = bbr->r_ctl.rc_probertt_int;
2243 log.u_bbr.flex4 = bbr->r_ctl.rc_went_idle_time;
2244 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2245 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2246 &bbr->rc_inp->inp_socket->so_rcv,
2247 &bbr->rc_inp->inp_socket->so_snd,
2248 BBR_LOG_ACKCLEAR, 0,
2249 0, &log, false, &bbr->rc_tv);
2254 bbr_log_ack_event(struct tcp_bbr *bbr, struct tcphdr *th, struct tcpopt *to, uint32_t tlen,
2255 uint16_t nsegs, uint32_t cts, int32_t nxt_pkt, struct mbuf *m)
2257 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2258 union tcp_log_stackspecific log;
2261 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2262 log.u_bbr.flex1 = nsegs;
2263 log.u_bbr.flex2 = bbr->r_ctl.rc_lost_bytes;
2267 log.u_bbr.flex3 = m->m_flags;
2268 if (m->m_flags & M_TSTMP) {
2269 mbuf_tstmp2timespec(m, &ts);
2270 tv.tv_sec = ts.tv_sec;
2271 tv.tv_usec = ts.tv_nsec / 1000;
2272 log.u_bbr.lt_epoch = tcp_tv_to_usectick(&tv);
2274 log.u_bbr.lt_epoch = 0;
2276 if (m->m_flags & M_TSTMP_LRO) {
2277 tv.tv_sec = m->m_pkthdr.rcv_tstmp / 1000000000;
2278 tv.tv_usec = (m->m_pkthdr.rcv_tstmp % 1000000000) / 1000;
2279 log.u_bbr.flex5 = tcp_tv_to_usectick(&tv);
2281 /* No arrival timestamp */
2282 log.u_bbr.flex5 = 0;
2285 log.u_bbr.pkts_out = tcp_get_usecs(&tv);
2287 log.u_bbr.flex3 = 0;
2288 log.u_bbr.flex5 = 0;
2289 log.u_bbr.flex6 = 0;
2290 log.u_bbr.pkts_out = 0;
2292 log.u_bbr.flex4 = bbr->r_ctl.rc_target_at_state;
2293 log.u_bbr.flex7 = bbr->r_wanted_output;
2294 log.u_bbr.flex8 = bbr->rc_in_persist;
2295 TCP_LOG_EVENTP(bbr->rc_tp, th,
2296 &bbr->rc_inp->inp_socket->so_rcv,
2297 &bbr->rc_inp->inp_socket->so_snd,
2299 tlen, &log, true, &bbr->rc_tv);
2304 bbr_log_doseg_done(struct tcp_bbr *bbr, uint32_t cts, int32_t nxt_pkt, int32_t did_out)
2306 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2307 union tcp_log_stackspecific log;
2309 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2310 log.u_bbr.flex1 = did_out;
2311 log.u_bbr.flex2 = nxt_pkt;
2312 log.u_bbr.flex3 = bbr->r_ctl.rc_last_delay_val;
2313 log.u_bbr.flex4 = bbr->r_ctl.rc_hpts_flags;
2314 log.u_bbr.flex5 = bbr->r_ctl.rc_timer_exp;
2315 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_bytes;
2316 log.u_bbr.flex7 = bbr->r_wanted_output;
2317 log.u_bbr.flex8 = bbr->rc_in_persist;
2318 log.u_bbr.pkts_out = bbr->r_ctl.highest_hdwr_delay;
2319 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2320 &bbr->rc_inp->inp_socket->so_rcv,
2321 &bbr->rc_inp->inp_socket->so_snd,
2322 BBR_LOG_DOSEG_DONE, 0,
2323 0, &log, true, &bbr->rc_tv);
2328 bbr_log_enobuf_jmp(struct tcp_bbr *bbr, uint32_t len, uint32_t cts,
2329 int32_t line, uint32_t o_len, uint32_t segcnt, uint32_t segsiz)
2331 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2332 union tcp_log_stackspecific log;
2334 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2335 log.u_bbr.flex1 = line;
2336 log.u_bbr.flex2 = o_len;
2337 log.u_bbr.flex3 = segcnt;
2338 log.u_bbr.flex4 = segsiz;
2339 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2340 &bbr->rc_inp->inp_socket->so_rcv,
2341 &bbr->rc_inp->inp_socket->so_snd,
2342 BBR_LOG_ENOBUF_JMP, ENOBUFS,
2343 len, &log, true, &bbr->rc_tv);
2348 bbr_log_to_processing(struct tcp_bbr *bbr, uint32_t cts, int32_t ret, int32_t timers, uint8_t hpts_calling)
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 = timers;
2355 log.u_bbr.flex2 = ret;
2356 log.u_bbr.flex3 = bbr->r_ctl.rc_timer_exp;
2357 log.u_bbr.flex4 = bbr->r_ctl.rc_hpts_flags;
2358 log.u_bbr.flex5 = cts;
2359 log.u_bbr.flex6 = bbr->r_ctl.rc_target_at_state;
2360 log.u_bbr.flex8 = hpts_calling;
2361 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2362 &bbr->rc_inp->inp_socket->so_rcv,
2363 &bbr->rc_inp->inp_socket->so_snd,
2364 BBR_LOG_TO_PROCESS, 0,
2365 0, &log, false, &bbr->rc_tv);
2370 bbr_log_to_event(struct tcp_bbr *bbr, uint32_t cts, int32_t to_num)
2372 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2373 union tcp_log_stackspecific log;
2376 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2377 log.u_bbr.flex1 = bbr->bbr_timer_src;
2378 log.u_bbr.flex2 = 0;
2379 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags;
2380 ar = (uint64_t)(bbr->r_ctl.rc_resend);
2382 ar &= 0x00000000ffffffff;
2383 log.u_bbr.flex4 = (uint32_t)ar;
2384 ar = (uint64_t)bbr->r_ctl.rc_resend;
2385 ar &= 0x00000000ffffffff;
2386 log.u_bbr.flex5 = (uint32_t)ar;
2387 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
2388 log.u_bbr.flex8 = to_num;
2389 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2390 &bbr->rc_inp->inp_socket->so_rcv,
2391 &bbr->rc_inp->inp_socket->so_snd,
2393 0, &log, false, &bbr->rc_tv);
2398 bbr_log_startup_event(struct tcp_bbr *bbr, uint32_t cts, uint32_t flex1, uint32_t flex2, uint32_t flex3, uint8_t reason)
2400 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2401 union tcp_log_stackspecific log;
2403 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2404 log.u_bbr.flex1 = flex1;
2405 log.u_bbr.flex2 = flex2;
2406 log.u_bbr.flex3 = flex3;
2407 log.u_bbr.flex4 = 0;
2408 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2409 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup;
2410 log.u_bbr.flex8 = reason;
2411 log.u_bbr.cur_del_rate = bbr->r_ctl.rc_bbr_lastbtlbw;
2412 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2413 &bbr->rc_inp->inp_socket->so_rcv,
2414 &bbr->rc_inp->inp_socket->so_snd,
2416 0, &log, false, &bbr->rc_tv);
2421 bbr_log_hpts_diag(struct tcp_bbr *bbr, uint32_t cts, struct hpts_diag *diag)
2423 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2424 union tcp_log_stackspecific log;
2426 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2427 log.u_bbr.flex1 = diag->p_nxt_slot;
2428 log.u_bbr.flex2 = diag->p_cur_slot;
2429 log.u_bbr.flex3 = diag->slot_req;
2430 log.u_bbr.flex4 = diag->inp_hptsslot;
2431 log.u_bbr.flex5 = diag->slot_remaining;
2432 log.u_bbr.flex6 = diag->need_new_to;
2433 log.u_bbr.flex7 = diag->p_hpts_active;
2434 log.u_bbr.flex8 = diag->p_on_min_sleep;
2435 /* Hijack other fields as needed */
2436 log.u_bbr.epoch = diag->have_slept;
2437 log.u_bbr.lt_epoch = diag->yet_to_sleep;
2438 log.u_bbr.pkts_out = diag->co_ret;
2439 log.u_bbr.applimited = diag->hpts_sleep_time;
2440 log.u_bbr.delivered = diag->p_prev_slot;
2441 log.u_bbr.inflight = diag->p_runningtick;
2442 log.u_bbr.bw_inuse = diag->wheel_tick;
2443 log.u_bbr.rttProp = diag->wheel_cts;
2444 log.u_bbr.delRate = diag->maxticks;
2445 log.u_bbr.cur_del_rate = diag->p_curtick;
2446 log.u_bbr.cur_del_rate <<= 32;
2447 log.u_bbr.cur_del_rate |= diag->p_lasttick;
2448 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2449 &bbr->rc_inp->inp_socket->so_rcv,
2450 &bbr->rc_inp->inp_socket->so_snd,
2451 BBR_LOG_HPTSDIAG, 0,
2452 0, &log, false, &bbr->rc_tv);
2457 bbr_log_timer_var(struct tcp_bbr *bbr, int mode, uint32_t cts, uint32_t time_since_sent, uint32_t srtt,
2458 uint32_t thresh, uint32_t to)
2460 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2461 union tcp_log_stackspecific log;
2463 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2464 log.u_bbr.flex1 = bbr->rc_tp->t_rttvar;
2465 log.u_bbr.flex2 = time_since_sent;
2466 log.u_bbr.flex3 = srtt;
2467 log.u_bbr.flex4 = thresh;
2468 log.u_bbr.flex5 = to;
2469 log.u_bbr.flex6 = bbr->rc_tp->t_srtt;
2470 log.u_bbr.flex8 = mode;
2471 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2472 &bbr->rc_inp->inp_socket->so_rcv,
2473 &bbr->rc_inp->inp_socket->so_snd,
2474 BBR_LOG_TIMERPREP, 0,
2475 0, &log, false, &bbr->rc_tv);
2480 bbr_log_pacing_delay_calc(struct tcp_bbr *bbr, uint16_t gain, uint32_t len,
2481 uint32_t cts, uint32_t usecs, uint64_t bw, uint32_t override, int mod)
2483 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2484 union tcp_log_stackspecific log;
2486 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2487 log.u_bbr.flex1 = usecs;
2488 log.u_bbr.flex2 = len;
2489 log.u_bbr.flex3 = (uint32_t)((bw >> 32) & 0x00000000ffffffff);
2490 log.u_bbr.flex4 = (uint32_t)(bw & 0x00000000ffffffff);
2492 log.u_bbr.flex5 = (1 << 2);
2494 log.u_bbr.flex5 = 0;
2495 log.u_bbr.flex6 = override;
2496 log.u_bbr.flex7 = gain;
2497 log.u_bbr.flex8 = mod;
2498 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2499 &bbr->rc_inp->inp_socket->so_rcv,
2500 &bbr->rc_inp->inp_socket->so_snd,
2501 BBR_LOG_HPTSI_CALC, 0,
2502 len, &log, false, &bbr->rc_tv);
2507 bbr_log_to_start(struct tcp_bbr *bbr, uint32_t cts, uint32_t to, int32_t slot, uint8_t which)
2509 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2510 union tcp_log_stackspecific log;
2512 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2514 log.u_bbr.flex1 = bbr->bbr_timer_src;
2515 log.u_bbr.flex2 = to;
2516 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags;
2517 log.u_bbr.flex4 = slot;
2518 log.u_bbr.flex5 = bbr->rc_inp->inp_hptsslot;
2519 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
2520 log.u_bbr.pkts_out = bbr->rc_inp->inp_flags2;
2521 log.u_bbr.flex8 = which;
2522 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2523 &bbr->rc_inp->inp_socket->so_rcv,
2524 &bbr->rc_inp->inp_socket->so_snd,
2525 BBR_LOG_TIMERSTAR, 0,
2526 0, &log, false, &bbr->rc_tv);
2531 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)
2533 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2534 union tcp_log_stackspecific log;
2536 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2537 log.u_bbr.flex1 = thresh;
2538 log.u_bbr.flex2 = lro;
2539 log.u_bbr.flex3 = bbr->r_ctl.rc_reorder_ts;
2540 log.u_bbr.flex4 = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
2541 log.u_bbr.flex5 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
2542 log.u_bbr.flex6 = srtt;
2543 log.u_bbr.flex7 = bbr->r_ctl.rc_reorder_shift;
2544 log.u_bbr.flex8 = frm;
2545 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2546 &bbr->rc_inp->inp_socket->so_rcv,
2547 &bbr->rc_inp->inp_socket->so_snd,
2548 BBR_LOG_THRESH_CALC, 0,
2549 0, &log, false, &bbr->rc_tv);
2554 bbr_log_to_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts, uint8_t hpts_removed)
2556 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2557 union tcp_log_stackspecific log;
2559 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2560 log.u_bbr.flex1 = line;
2561 log.u_bbr.flex2 = bbr->bbr_timer_src;
2562 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags;
2563 log.u_bbr.flex4 = bbr->rc_in_persist;
2564 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state;
2565 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
2566 log.u_bbr.flex8 = hpts_removed;
2567 log.u_bbr.pkts_out = bbr->rc_pacer_started;
2568 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2569 &bbr->rc_inp->inp_socket->so_rcv,
2570 &bbr->rc_inp->inp_socket->so_snd,
2571 BBR_LOG_TIMERCANC, 0,
2572 0, &log, false, &bbr->rc_tv);
2578 bbr_log_tstmp_validation(struct tcp_bbr *bbr, uint64_t peer_delta, uint64_t delta)
2580 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2581 union tcp_log_stackspecific log;
2583 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2584 log.u_bbr.flex1 = bbr->r_ctl.bbr_peer_tsratio;
2585 log.u_bbr.flex2 = (peer_delta >> 32);
2586 log.u_bbr.flex3 = (peer_delta & 0x00000000ffffffff);
2587 log.u_bbr.flex4 = (delta >> 32);
2588 log.u_bbr.flex5 = (delta & 0x00000000ffffffff);
2589 log.u_bbr.flex7 = bbr->rc_ts_clock_set;
2590 log.u_bbr.flex8 = bbr->rc_ts_cant_be_used;
2591 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2592 &bbr->rc_inp->inp_socket->so_rcv,
2593 &bbr->rc_inp->inp_socket->so_snd,
2594 BBR_LOG_TSTMP_VAL, 0,
2595 0, &log, false, &bbr->rc_tv);
2601 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)
2603 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2604 union tcp_log_stackspecific log;
2606 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2607 log.u_bbr.flex1 = tsosz;
2608 log.u_bbr.flex2 = tls;
2609 log.u_bbr.flex3 = tcp_min_hptsi_time;
2610 log.u_bbr.flex4 = bbr->r_ctl.bbr_hptsi_bytes_min;
2611 log.u_bbr.flex5 = old_val;
2612 log.u_bbr.flex6 = maxseg;
2613 log.u_bbr.flex7 = bbr->rc_no_pacing;
2614 log.u_bbr.flex7 <<= 1;
2615 log.u_bbr.flex7 |= bbr->rc_past_init_win;
2617 log.u_bbr.flex8 = 0x80 | bbr->rc_use_google;
2619 log.u_bbr.flex8 = bbr->rc_use_google;
2620 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2621 &bbr->rc_inp->inp_socket->so_rcv,
2622 &bbr->rc_inp->inp_socket->so_snd,
2624 0, &log, false, &bbr->rc_tv);
2629 bbr_log_type_rsmclear(struct tcp_bbr *bbr, uint32_t cts, struct bbr_sendmap *rsm,
2630 uint32_t flags, uint32_t line)
2632 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2633 union tcp_log_stackspecific log;
2635 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2636 log.u_bbr.flex1 = line;
2637 log.u_bbr.flex2 = rsm->r_start;
2638 log.u_bbr.flex3 = rsm->r_end;
2639 log.u_bbr.flex4 = rsm->r_delivered;
2640 log.u_bbr.flex5 = rsm->r_rtr_cnt;
2641 log.u_bbr.flex6 = rsm->r_dupack;
2642 log.u_bbr.flex7 = rsm->r_tim_lastsent[0];
2643 log.u_bbr.flex8 = rsm->r_flags;
2644 /* Hijack the pkts_out fids */
2645 log.u_bbr.applimited = flags;
2646 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2647 &bbr->rc_inp->inp_socket->so_rcv,
2648 &bbr->rc_inp->inp_socket->so_snd,
2650 0, &log, false, &bbr->rc_tv);
2655 bbr_log_type_bbrupd(struct tcp_bbr *bbr, uint8_t flex8, uint32_t cts,
2656 uint32_t flex3, uint32_t flex2, uint32_t flex5,
2657 uint32_t flex6, uint32_t pkts_out, int flex7,
2658 uint32_t flex4, uint32_t flex1)
2661 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2662 union tcp_log_stackspecific log;
2664 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2665 log.u_bbr.flex1 = flex1;
2666 log.u_bbr.flex2 = flex2;
2667 log.u_bbr.flex3 = flex3;
2668 log.u_bbr.flex4 = flex4;
2669 log.u_bbr.flex5 = flex5;
2670 log.u_bbr.flex6 = flex6;
2671 log.u_bbr.flex7 = flex7;
2672 /* Hijack the pkts_out fids */
2673 log.u_bbr.pkts_out = pkts_out;
2674 log.u_bbr.flex8 = flex8;
2675 if (bbr->rc_ack_was_delayed)
2676 log.u_bbr.epoch = bbr->r_ctl.rc_ack_hdwr_delay;
2678 log.u_bbr.epoch = 0;
2679 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2680 &bbr->rc_inp->inp_socket->so_rcv,
2681 &bbr->rc_inp->inp_socket->so_snd,
2683 flex2, &log, false, &bbr->rc_tv);
2689 bbr_log_type_ltbw(struct tcp_bbr *bbr, uint32_t cts, int32_t reason,
2690 uint32_t newbw, uint32_t obw, uint32_t diff,
2693 if (/*bbr_verbose_logging && */(bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2694 union tcp_log_stackspecific log;
2696 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2697 log.u_bbr.flex1 = reason;
2698 log.u_bbr.flex2 = newbw;
2699 log.u_bbr.flex3 = obw;
2700 log.u_bbr.flex4 = diff;
2701 log.u_bbr.flex5 = bbr->r_ctl.rc_lt_lost;
2702 log.u_bbr.flex6 = bbr->r_ctl.rc_lt_del;
2703 log.u_bbr.flex7 = bbr->rc_lt_is_sampling;
2704 log.u_bbr.pkts_out = tim;
2705 log.u_bbr.bw_inuse = bbr->r_ctl.rc_lt_bw;
2706 if (bbr->rc_lt_use_bw == 0)
2707 log.u_bbr.epoch = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch;
2709 log.u_bbr.epoch = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch_use;
2710 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2711 &bbr->rc_inp->inp_socket->so_rcv,
2712 &bbr->rc_inp->inp_socket->so_snd,
2714 0, &log, false, &bbr->rc_tv);
2719 bbr_log_progress_event(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t tick, int event, int line)
2721 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2722 union tcp_log_stackspecific log;
2724 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2725 log.u_bbr.flex1 = line;
2726 log.u_bbr.flex2 = tick;
2727 log.u_bbr.flex3 = tp->t_maxunacktime;
2728 log.u_bbr.flex4 = tp->t_acktime;
2729 log.u_bbr.flex8 = event;
2730 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2731 &bbr->rc_inp->inp_socket->so_rcv,
2732 &bbr->rc_inp->inp_socket->so_snd,
2733 BBR_LOG_PROGRESS, 0,
2734 0, &log, false, &bbr->rc_tv);
2739 bbr_type_log_hdwr_pacing(struct tcp_bbr *bbr, const struct ifnet *ifp,
2740 uint64_t rate, uint64_t hw_rate, int line, uint32_t cts,
2743 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2744 union tcp_log_stackspecific log;
2746 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2747 log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff);
2748 log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff);
2749 log.u_bbr.flex3 = (((uint64_t)ifp >> 32) & 0x00000000ffffffff);
2750 log.u_bbr.flex4 = ((uint64_t)ifp & 0x00000000ffffffff);
2751 log.u_bbr.bw_inuse = rate;
2752 log.u_bbr.flex5 = line;
2753 log.u_bbr.flex6 = error;
2754 log.u_bbr.flex8 = bbr->skip_gain;
2755 log.u_bbr.flex8 <<= 1;
2756 log.u_bbr.flex8 |= bbr->gain_is_limited;
2757 log.u_bbr.flex8 <<= 1;
2758 log.u_bbr.flex8 |= bbr->bbr_hdrw_pacing;
2759 log.u_bbr.pkts_out = bbr->rc_tp->t_maxseg;
2760 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2761 &bbr->rc_inp->inp_socket->so_rcv,
2762 &bbr->rc_inp->inp_socket->so_snd,
2763 BBR_LOG_HDWR_PACE, 0,
2764 0, &log, false, &bbr->rc_tv);
2769 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)
2771 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2772 union tcp_log_stackspecific log;
2774 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2775 log.u_bbr.flex1 = slot;
2776 log.u_bbr.flex2 = del_by;
2777 log.u_bbr.flex3 = prev_delay;
2778 log.u_bbr.flex4 = line;
2779 log.u_bbr.flex5 = bbr->r_ctl.rc_last_delay_val;
2780 log.u_bbr.flex6 = bbr->r_ctl.rc_hptsi_agg_delay;
2781 log.u_bbr.flex7 = (0x0000ffff & bbr->r_ctl.rc_hpts_flags);
2782 log.u_bbr.flex8 = bbr->rc_in_persist;
2783 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2784 &bbr->rc_inp->inp_socket->so_rcv,
2785 &bbr->rc_inp->inp_socket->so_snd,
2787 len, &log, false, &bbr->rc_tv);
2792 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)
2794 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2795 union tcp_log_stackspecific log;
2797 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2798 log.u_bbr.flex1 = bbr->r_ctl.rc_delivered;
2799 log.u_bbr.flex2 = 0;
2800 log.u_bbr.flex3 = bbr->r_ctl.rc_lowest_rtt;
2801 log.u_bbr.flex4 = end;
2802 log.u_bbr.flex5 = seq;
2803 log.u_bbr.flex6 = t;
2804 log.u_bbr.flex7 = match;
2805 log.u_bbr.flex8 = flags;
2806 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2807 &bbr->rc_inp->inp_socket->so_rcv,
2808 &bbr->rc_inp->inp_socket->so_snd,
2810 0, &log, false, &bbr->rc_tv);
2815 bbr_log_exit_gain(struct tcp_bbr *bbr, uint32_t cts, int32_t entry_method)
2817 if (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2818 union tcp_log_stackspecific log;
2820 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
2821 log.u_bbr.flex1 = bbr->r_ctl.rc_target_at_state;
2822 log.u_bbr.flex2 = (bbr->rc_tp->t_maxseg - bbr->rc_last_options);
2823 log.u_bbr.flex3 = bbr->r_ctl.gain_epoch;
2824 log.u_bbr.flex4 = bbr->r_ctl.rc_pace_max_segs;
2825 log.u_bbr.flex5 = bbr->r_ctl.rc_pace_min_segs;
2826 log.u_bbr.flex6 = bbr->r_ctl.rc_bbr_state_atflight;
2827 log.u_bbr.flex7 = 0;
2828 log.u_bbr.flex8 = entry_method;
2829 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2830 &bbr->rc_inp->inp_socket->so_rcv,
2831 &bbr->rc_inp->inp_socket->so_snd,
2832 BBR_LOG_EXIT_GAIN, 0,
2833 0, &log, false, &bbr->rc_tv);
2838 bbr_log_settings_change(struct tcp_bbr *bbr, int settings_desired)
2840 if (bbr_verbose_logging && (bbr->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2841 union tcp_log_stackspecific log;
2843 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime);
2845 log.u_bbr.flex1 = 0;
2846 log.u_bbr.flex2 = 0;
2847 log.u_bbr.flex3 = 0;
2848 log.u_bbr.flex4 = 0;
2849 log.u_bbr.flex7 = 0;
2850 log.u_bbr.flex8 = settings_desired;
2852 TCP_LOG_EVENTP(bbr->rc_tp, NULL,
2853 &bbr->rc_inp->inp_socket->so_rcv,
2854 &bbr->rc_inp->inp_socket->so_snd,
2855 BBR_LOG_SETTINGS_CHG, 0,
2856 0, &log, false, &bbr->rc_tv);
2861 * Returns the bw from the our filter.
2863 static inline uint64_t
2864 bbr_get_full_bw(struct tcp_bbr *bbr)
2868 bw = get_filter_value(&bbr->r_ctl.rc_delrate);
2874 bbr_set_pktepoch(struct tcp_bbr *bbr, uint32_t cts, int32_t line)
2879 if (bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_pktepoch)
2880 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lost_at_pktepoch;
2883 del = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_pkt_epoch_del;
2888 calclr *= (uint64_t)1000;
2889 calclr /= (uint64_t)del;
2891 /* Nothing delivered? 100.0% loss */
2894 bbr->r_ctl.rc_pkt_epoch_loss_rate = (uint32_t)calclr;
2895 if (IN_RECOVERY(bbr->rc_tp->t_flags))
2896 bbr->r_ctl.recovery_lr += (uint32_t)calclr;
2897 bbr->r_ctl.rc_pkt_epoch++;
2898 if (bbr->rc_no_pacing &&
2899 (bbr->r_ctl.rc_pkt_epoch >= bbr->no_pacing_until)) {
2900 bbr->rc_no_pacing = 0;
2901 tcp_bbr_tso_size_check(bbr, cts);
2903 bbr->r_ctl.rc_pkt_epoch_rtt = bbr_calc_time(cts, bbr->r_ctl.rc_pkt_epoch_time);
2904 bbr->r_ctl.rc_pkt_epoch_time = cts;
2905 /* What was our loss rate */
2906 bbr_log_pkt_epoch(bbr, cts, line, lost, del);
2907 bbr->r_ctl.rc_pkt_epoch_del = bbr->r_ctl.rc_delivered;
2908 bbr->r_ctl.rc_lost_at_pktepoch = bbr->r_ctl.rc_lost;
2912 bbr_set_epoch(struct tcp_bbr *bbr, uint32_t cts, int32_t line)
2914 uint32_t epoch_time;
2916 /* Tick the RTT clock */
2917 bbr->r_ctl.rc_rtt_epoch++;
2918 epoch_time = cts - bbr->r_ctl.rc_rcv_epoch_start;
2919 bbr_log_time_epoch(bbr, cts, line, epoch_time);
2920 bbr->r_ctl.rc_rcv_epoch_start = cts;
2925 bbr_isit_a_pkt_epoch(struct tcp_bbr *bbr, uint32_t cts, struct bbr_sendmap *rsm, int32_t line, int32_t cum_acked)
2927 if (SEQ_GEQ(rsm->r_delivered, bbr->r_ctl.rc_pkt_epoch_del)) {
2928 bbr->rc_is_pkt_epoch_now = 1;
2933 * Returns the bw from either the b/w filter
2934 * or from the lt_bw (if the connection is being
2937 static inline uint64_t
2938 __bbr_get_bw(struct tcp_bbr *bbr)
2940 uint64_t bw, min_bw;
2942 int gm_measure_cnt = 1;
2945 * For startup we make, like google, a
2946 * minimum b/w. This is generated from the
2947 * IW and the rttProp. We do fall back to srtt
2948 * if for some reason (initial handshake) we don't
2949 * have a rttProp. We, in the worst case, fall back
2950 * to the configured min_bw (rc_initial_hptsi_bw).
2952 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) {
2953 /* Attempt first to use rttProp */
2954 rtt = (uint64_t)get_filter_value_small(&bbr->r_ctl.rc_rttprop);
2955 if (rtt && (rtt < 0xffffffff)) {
2957 min_bw = (uint64_t)(bbr_initial_cwnd(bbr, bbr->rc_tp)) *
2958 ((uint64_t)1000000);
2960 if (min_bw < bbr->r_ctl.rc_initial_hptsi_bw) {
2961 min_bw = bbr->r_ctl.rc_initial_hptsi_bw;
2964 } else if (bbr->rc_tp->t_srtt != 0) {
2965 /* No rttProp, use srtt? */
2966 rtt = bbr_get_rtt(bbr, BBR_SRTT);
2969 min_bw = bbr->r_ctl.rc_initial_hptsi_bw;
2974 if ((bbr->rc_past_init_win == 0) &&
2975 (bbr->r_ctl.rc_delivered > bbr_initial_cwnd(bbr, bbr->rc_tp)))
2976 bbr->rc_past_init_win = 1;
2977 if ((bbr->rc_use_google) && (bbr->r_ctl.r_measurement_count >= 1))
2979 if (gm_measure_cnt &&
2980 ((bbr->r_ctl.r_measurement_count < bbr_min_measurements_req) ||
2981 (bbr->rc_past_init_win == 0))) {
2982 /* For google we use our guess rate until we get 1 measurement */
2985 rtt = (uint64_t)get_filter_value_small(&bbr->r_ctl.rc_rttprop);
2986 if (rtt && (rtt < 0xffffffff)) {
2988 * We have an RTT measurment. Use that in
2989 * combination with our initial window to calculate
2992 bw = (uint64_t)(bbr_initial_cwnd(bbr, bbr->rc_tp)) *
2993 ((uint64_t)1000000);
2995 if (bw < bbr->r_ctl.rc_initial_hptsi_bw) {
2996 bw = bbr->r_ctl.rc_initial_hptsi_bw;
2999 /* Drop back to the 40 and punt to a default */
3000 bw = bbr->r_ctl.rc_initial_hptsi_bw;
3003 /* Probably should panic */
3010 if (bbr->rc_lt_use_bw)
3011 bw = bbr->r_ctl.rc_lt_bw;
3012 else if (bbr->r_recovery_bw && (bbr->rc_use_google == 0))
3013 bw = bbr->r_ctl.red_bw;
3015 bw = get_filter_value(&bbr->r_ctl.rc_delrate);
3016 if (bbr->rc_tp->t_peakrate_thr && (bbr->rc_use_google == 0)) {
3018 * Enforce user set rate limit, keep in mind that
3019 * t_peakrate_thr is in B/s already
3021 bw = uqmin((uint64_t)bbr->rc_tp->t_peakrate_thr, bw);
3024 /* We should not be at 0, go to the initial window then */
3025 goto use_initial_window;
3028 /* Probably should panic */
3035 static inline uint64_t
3036 bbr_get_bw(struct tcp_bbr *bbr)
3040 bw = __bbr_get_bw(bbr);
3045 bbr_reset_lt_bw_interval(struct tcp_bbr *bbr, uint32_t cts)
3047 bbr->r_ctl.rc_lt_epoch = bbr->r_ctl.rc_pkt_epoch;
3048 bbr->r_ctl.rc_lt_time = bbr->r_ctl.rc_del_time;
3049 bbr->r_ctl.rc_lt_del = bbr->r_ctl.rc_delivered;
3050 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
3054 bbr_reset_lt_bw_sampling(struct tcp_bbr *bbr, uint32_t cts)
3056 bbr->rc_lt_is_sampling = 0;
3057 bbr->rc_lt_use_bw = 0;
3058 bbr->r_ctl.rc_lt_bw = 0;
3059 bbr_reset_lt_bw_interval(bbr, cts);
3063 bbr_lt_bw_samp_done(struct tcp_bbr *bbr, uint64_t bw, uint32_t cts, uint32_t timin)
3067 /* Do we have a previous sample? */
3068 if (bbr->r_ctl.rc_lt_bw) {
3069 /* Get the diff in bytes per second */
3070 if (bbr->r_ctl.rc_lt_bw > bw)
3071 diff = bbr->r_ctl.rc_lt_bw - bw;
3073 diff = bw - bbr->r_ctl.rc_lt_bw;
3074 if ((diff <= bbr_lt_bw_diff) ||
3075 (diff <= (bbr->r_ctl.rc_lt_bw / bbr_lt_bw_ratio))) {
3076 /* Consider us policed */
3079 saved_bw = (uint32_t)bbr->r_ctl.rc_lt_bw;
3080 bbr->r_ctl.rc_lt_bw = (bw + bbr->r_ctl.rc_lt_bw) / 2; /* average of two */
3081 bbr->rc_lt_use_bw = 1;
3082 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
3084 * Use pkt based epoch for measuring length of
3087 bbr->r_ctl.rc_lt_epoch_use = bbr->r_ctl.rc_pkt_epoch;
3089 * reason 4 is we need to start consider being
3092 bbr_log_type_ltbw(bbr, cts, 4, (uint32_t)bw, saved_bw, (uint32_t)diff, timin);
3096 bbr->r_ctl.rc_lt_bw = bw;
3097 bbr_reset_lt_bw_interval(bbr, cts);
3098 bbr_log_type_ltbw(bbr, cts, 5, 0, (uint32_t)bw, 0, timin);
3102 bbr_randomize_extra_state_time(struct tcp_bbr *bbr)
3104 uint32_t ran, deduct;
3106 ran = arc4random_uniform(bbr_rand_ot);
3108 deduct = bbr->r_ctl.rc_level_state_extra / ran;
3109 bbr->r_ctl.rc_level_state_extra -= deduct;
3113 * Return randomly the starting state
3114 * to use in probebw.
3117 bbr_pick_probebw_substate(struct tcp_bbr *bbr, uint32_t cts)
3122 /* Initialize the offset to 0 */
3123 bbr->r_ctl.rc_exta_time_gd = 0;
3124 bbr->rc_hit_state_1 = 0;
3125 bbr->r_ctl.rc_level_state_extra = 0;
3126 ran = arc4random_uniform((BBR_SUBSTATE_COUNT-1));
3128 * The math works funny here :) the return value is used to set the
3129 * substate and then the state change is called which increments by
3130 * one. So if we return 1 (DRAIN) we will increment to 2 (LEVEL1) when
3131 * we fully enter the state. Note that the (8 - 1 - ran) assures that
3132 * we return 1 - 7, so we dont return 0 and end up starting in
3135 ret_val = BBR_SUBSTATE_COUNT - 1 - ran;
3137 if ((cts - bbr->r_ctl.rc_rcv_epoch_start) >= bbr_get_rtt(bbr, BBR_RTT_PROP))
3138 bbr_set_epoch(bbr, cts, __LINE__);
3140 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
3145 bbr_lt_bw_sampling(struct tcp_bbr *bbr, uint32_t cts, int32_t loss_detected)
3147 uint32_t diff, d_time;
3148 uint64_t del_time, bw, lost, delivered;
3150 if (bbr->r_use_policer == 0)
3152 if (bbr->rc_lt_use_bw) {
3153 /* We are using lt bw do we stop yet? */
3154 diff = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch_use;
3155 if (diff > bbr_lt_bw_max_rtts) {
3158 bbr_reset_lt_bw_sampling(bbr, cts);
3159 if (bbr->rc_filled_pipe) {
3160 bbr_set_epoch(bbr, cts, __LINE__);
3161 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts);
3162 bbr_substate_change(bbr, cts, __LINE__, 0);
3163 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
3164 bbr_log_type_statechange(bbr, cts, __LINE__);
3167 * This should not happen really
3168 * unless we remove the startup/drain
3169 * restrictions above.
3171 bbr->rc_bbr_state = BBR_STATE_STARTUP;
3172 bbr_set_epoch(bbr, cts, __LINE__);
3173 bbr->r_ctl.rc_bbr_state_time = cts;
3174 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
3175 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg;
3176 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg;
3177 bbr_set_state_target(bbr, __LINE__);
3178 bbr_log_type_statechange(bbr, cts, __LINE__);
3180 /* reason 0 is to stop using lt-bw */
3181 bbr_log_type_ltbw(bbr, cts, 0, 0, 0, 0, 0);
3184 if (bbr_lt_intvl_fp == 0) {
3185 /* Not doing false-postive detection */
3188 /* False positive detection */
3189 if (diff == bbr_lt_intvl_fp) {
3190 /* At bbr_lt_intvl_fp we record the lost */
3191 bbr->r_ctl.rc_lt_del = bbr->r_ctl.rc_delivered;
3192 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
3193 } else if (diff > (bbr_lt_intvl_min_rtts + bbr_lt_intvl_fp)) {
3194 /* Now is our loss rate still high? */
3195 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lt_lost;
3196 delivered = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_lt_del;
3197 if ((delivered == 0) ||
3198 (((lost * 1000)/delivered) < bbr_lt_fd_thresh)) {
3199 /* No still below our threshold */
3200 bbr_log_type_ltbw(bbr, cts, 7, lost, delivered, 0, 0);
3202 /* Yikes its still high, it must be a false positive */
3203 bbr_log_type_ltbw(bbr, cts, 8, lost, delivered, 0, 0);
3210 * Wait for the first loss before sampling, to let the policer
3211 * exhaust its tokens and estimate the steady-state rate allowed by
3212 * the policer. Starting samples earlier includes bursts that
3213 * over-estimate the bw.
3215 if (bbr->rc_lt_is_sampling == 0) {
3216 /* reason 1 is to begin doing the sampling */
3217 if (loss_detected == 0)
3219 bbr_reset_lt_bw_interval(bbr, cts);
3220 bbr->rc_lt_is_sampling = 1;
3221 bbr_log_type_ltbw(bbr, cts, 1, 0, 0, 0, 0);
3224 /* Now how long were we delivering long term last> */
3225 if (TSTMP_GEQ(bbr->r_ctl.rc_del_time, bbr->r_ctl.rc_lt_time))
3226 d_time = bbr->r_ctl.rc_del_time - bbr->r_ctl.rc_lt_time;
3230 /* To avoid underestimates, reset sampling if we run out of data. */
3231 if (bbr->r_ctl.r_app_limited_until) {
3232 /* Can not measure in app-limited state */
3233 bbr_reset_lt_bw_sampling(bbr, cts);
3234 /* reason 2 is to reset sampling due to app limits */
3235 bbr_log_type_ltbw(bbr, cts, 2, 0, 0, 0, d_time);
3238 diff = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch;
3239 if (diff < bbr_lt_intvl_min_rtts) {
3241 * need more samples (we don't
3242 * start on a round like linux so
3245 /* 6 is not_enough time or no-loss */
3246 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time);
3249 if (diff > (4 * bbr_lt_intvl_min_rtts)) {
3251 * For now if we wait too long, reset all sampling. We need
3252 * to do some research here, its possible that we should
3253 * base this on how much loss as occurred.. something like
3254 * if its under 10% (or some thresh) reset all otherwise
3255 * don't. Thats for phase II I guess.
3257 bbr_reset_lt_bw_sampling(bbr, cts);
3258 /* reason 3 is to reset sampling due too long of sampling */
3259 bbr_log_type_ltbw(bbr, cts, 3, 0, 0, 0, d_time);
3263 * End sampling interval when a packet is lost, so we estimate the
3264 * policer tokens were exhausted. Stopping the sampling before the
3265 * tokens are exhausted under-estimates the policed rate.
3267 if (loss_detected == 0) {
3268 /* 6 is not_enough time or no-loss */
3269 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time);
3272 /* Calculate packets lost and delivered in sampling interval. */
3273 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lt_lost;
3274 delivered = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_lt_del;
3275 if ((delivered == 0) ||
3276 (((lost * 1000)/delivered) < bbr_lt_loss_thresh)) {
3277 bbr_log_type_ltbw(bbr, cts, 6, lost, delivered, 0, d_time);
3280 if (d_time < 1000) {
3281 /* Not enough time. wait */
3282 /* 6 is not_enough time or no-loss */
3283 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time);
3286 if (d_time >= (0xffffffff / USECS_IN_MSEC)) {
3288 bbr_reset_lt_bw_sampling(bbr, cts);
3289 /* reason 3 is to reset sampling due too long of sampling */
3290 bbr_log_type_ltbw(bbr, cts, 3, 0, 0, 0, d_time);
3295 bw *= (uint64_t)USECS_IN_SECOND;
3297 bbr_lt_bw_samp_done(bbr, bw, cts, d_time);
3301 * Allocate a sendmap from our zone.
3303 static struct bbr_sendmap *
3304 bbr_alloc(struct tcp_bbr *bbr)
3306 struct bbr_sendmap *rsm;
3308 BBR_STAT_INC(bbr_to_alloc);
3309 rsm = uma_zalloc(bbr_zone, (M_NOWAIT | M_ZERO));
3311 bbr->r_ctl.rc_num_maps_alloced++;
3314 if (bbr->r_ctl.rc_free_cnt) {
3315 BBR_STAT_INC(bbr_to_alloc_emerg);
3316 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free);
3317 TAILQ_REMOVE(&bbr->r_ctl.rc_free, rsm, r_next);
3318 bbr->r_ctl.rc_free_cnt--;
3321 BBR_STAT_INC(bbr_to_alloc_failed);
3325 static struct bbr_sendmap *
3326 bbr_alloc_full_limit(struct tcp_bbr *bbr)
3328 if ((V_tcp_map_entries_limit > 0) &&
3329 (bbr->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
3330 BBR_STAT_INC(bbr_alloc_limited);
3331 if (!bbr->alloc_limit_reported) {
3332 bbr->alloc_limit_reported = 1;
3333 BBR_STAT_INC(bbr_alloc_limited_conns);
3337 return (bbr_alloc(bbr));
3341 /* wrapper to allocate a sendmap entry, subject to a specific limit */
3342 static struct bbr_sendmap *
3343 bbr_alloc_limit(struct tcp_bbr *bbr, uint8_t limit_type)
3345 struct bbr_sendmap *rsm;
3348 /* currently there is only one limit type */
3349 if (V_tcp_map_split_limit > 0 &&
3350 bbr->r_ctl.rc_num_split_allocs >= V_tcp_map_split_limit) {
3351 BBR_STAT_INC(bbr_split_limited);
3352 if (!bbr->alloc_limit_reported) {
3353 bbr->alloc_limit_reported = 1;
3354 BBR_STAT_INC(bbr_alloc_limited_conns);
3360 /* allocate and mark in the limit type, if set */
3361 rsm = bbr_alloc(bbr);
3362 if (rsm != NULL && limit_type) {
3363 rsm->r_limit_type = limit_type;
3364 bbr->r_ctl.rc_num_split_allocs++;
3370 bbr_free(struct tcp_bbr *bbr, struct bbr_sendmap *rsm)
3372 if (rsm->r_limit_type) {
3373 /* currently there is only one limit type */
3374 bbr->r_ctl.rc_num_split_allocs--;
3376 if (rsm->r_is_smallmap)
3377 bbr->r_ctl.rc_num_small_maps_alloced--;
3378 if (bbr->r_ctl.rc_tlp_send == rsm)
3379 bbr->r_ctl.rc_tlp_send = NULL;
3380 if (bbr->r_ctl.rc_resend == rsm) {
3381 bbr->r_ctl.rc_resend = NULL;
3383 if (bbr->r_ctl.rc_next == rsm)
3384 bbr->r_ctl.rc_next = NULL;
3385 if (bbr->r_ctl.rc_sacklast == rsm)
3386 bbr->r_ctl.rc_sacklast = NULL;
3387 if (bbr->r_ctl.rc_free_cnt < bbr_min_req_free) {
3388 memset(rsm, 0, sizeof(struct bbr_sendmap));
3389 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_free, rsm, r_next);
3390 rsm->r_limit_type = 0;
3391 bbr->r_ctl.rc_free_cnt++;
3394 bbr->r_ctl.rc_num_maps_alloced--;
3395 uma_zfree(bbr_zone, rsm);
3402 bbr_get_bw_delay_prod(uint64_t rtt, uint64_t bw) {
3404 * Calculate the bytes in flight needed given the bw (in bytes per
3405 * second) and the specifyed rtt in useconds. We need to put out the
3406 * returned value per RTT to match that rate. Gain will normaly
3407 * raise it up from there.
3409 * This should not overflow as long as the bandwidth is below 1
3410 * TByte per second (bw < 10**12 = 2**40) and the rtt is smaller
3411 * than 1000 seconds (rtt < 10**3 * 10**6 = 10**9 = 2**30).
3413 uint64_t usec_per_sec;
3415 usec_per_sec = USECS_IN_SECOND;
3416 return ((rtt * bw) / usec_per_sec);
3420 * Return the initial cwnd.
3423 bbr_initial_cwnd(struct tcp_bbr *bbr, struct tcpcb *tp)
3427 if (bbr->rc_init_win) {
3428 i_cwnd = bbr->rc_init_win * tp->t_maxseg;
3429 } else if (V_tcp_initcwnd_segments)
3430 i_cwnd = min((V_tcp_initcwnd_segments * tp->t_maxseg),
3431 max(2 * tp->t_maxseg, 14600));
3432 else if (V_tcp_do_rfc3390)
3433 i_cwnd = min(4 * tp->t_maxseg,
3434 max(2 * tp->t_maxseg, 4380));
3436 /* Per RFC5681 Section 3.1 */
3437 if (tp->t_maxseg > 2190)
3438 i_cwnd = 2 * tp->t_maxseg;
3439 else if (tp->t_maxseg > 1095)
3440 i_cwnd = 3 * tp->t_maxseg;
3442 i_cwnd = 4 * tp->t_maxseg;
3448 * Given a specified gain, return the target
3449 * cwnd based on that gain.
3452 bbr_get_raw_target_cwnd(struct tcp_bbr *bbr, uint32_t gain, uint64_t bw)
3457 if ((get_filter_value_small(&bbr->r_ctl.rc_rttprop) == 0xffffffff) ||
3458 (bbr_get_full_bw(bbr) == 0)) {
3459 /* No measurements yet */
3460 return (bbr_initial_cwnd(bbr, bbr->rc_tp));
3463 * Get bytes per RTT needed (rttProp is normally in
3464 * bbr_cwndtarget_rtt_touse)
3466 rtt = bbr_get_rtt(bbr, bbr_cwndtarget_rtt_touse);
3467 /* Get the bdp from the two values */
3468 bdp = bbr_get_bw_delay_prod(rtt, bw);
3469 /* Now apply the gain */
3470 cwnd = (uint32_t)(((bdp * ((uint64_t)gain)) + (uint64_t)(BBR_UNIT - 1)) / ((uint64_t)BBR_UNIT));
3476 bbr_get_target_cwnd(struct tcp_bbr *bbr, uint64_t bw, uint32_t gain)
3480 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs);
3481 /* Get the base cwnd with gain rounded to a mss */
3482 cwnd = roundup(bbr_get_raw_target_cwnd(bbr, bw, gain), mss);
3484 * Add in N (2 default since we do not have a
3485 * fq layer to trap packets in) quanta's per the I-D
3486 * section 4.2.3.2 quanta adjust.
3488 cwnd += (bbr_quanta * bbr->r_ctl.rc_pace_max_segs);
3489 if (bbr->rc_use_google) {
3490 if((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) &&
3491 (bbr_state_val(bbr) == BBR_SUB_GAIN)) {
3493 * The linux implementation adds
3494 * an extra 2 x mss in gain cycle which
3495 * is documented no-where except in the code.
3496 * so we add more for Neal undocumented feature
3500 if ((cwnd / mss) & 0x1) {
3501 /* Round up for odd num mss */
3505 /* Are we below the min cwnd? */
3506 if (cwnd < get_min_cwnd(bbr))
3507 return (get_min_cwnd(bbr));
3512 bbr_gain_adjust(struct tcp_bbr *bbr, uint16_t gain)
3520 bbr_get_header_oh(struct tcp_bbr *bbr)
3525 if (bbr->r_ctl.rc_inc_tcp_oh) {
3526 /* Do we include TCP overhead? */
3527 seg_oh = (bbr->rc_last_options + sizeof(struct tcphdr));
3529 if (bbr->r_ctl.rc_inc_ip_oh) {
3530 /* Do we include IP overhead? */
3533 seg_oh += sizeof(struct ip6_hdr);
3537 seg_oh += sizeof(struct ip);
3540 if (bbr->r_ctl.rc_inc_enet_oh) {
3541 /* Do we include the ethernet overhead? */
3542 seg_oh += sizeof(struct ether_header);
3549 bbr_get_pacing_length(struct tcp_bbr *bbr, uint16_t gain, uint32_t useconds_time, uint64_t bw)
3551 uint64_t divor, res, tim;
3553 if (useconds_time == 0)
3555 gain = bbr_gain_adjust(bbr, gain);
3556 divor = (uint64_t)USECS_IN_SECOND * (uint64_t)BBR_UNIT;
3557 tim = useconds_time;
3558 res = (tim * bw * gain) / divor;
3561 return ((uint32_t)res);
3565 * Given a gain and a length return the delay in useconds that
3566 * should be used to evenly space out packets
3567 * on the connection (based on the gain factor).
3570 bbr_get_pacing_delay(struct tcp_bbr *bbr, uint16_t gain, int32_t len, uint32_t cts, int nolog)
3572 uint64_t bw, lentim, res;
3573 uint32_t usecs, srtt, over = 0;
3574 uint32_t seg_oh, num_segs, maxseg;
3579 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
3580 num_segs = (len + maxseg - 1) / maxseg;
3581 if (bbr->rc_use_google == 0) {
3582 seg_oh = bbr_get_header_oh(bbr);
3583 len += (num_segs * seg_oh);
3585 gain = bbr_gain_adjust(bbr, gain);
3586 bw = bbr_get_bw(bbr);
3587 if (bbr->rc_use_google) {
3591 * Reduce the b/w by the google discount
3594 cbw = bw * (uint64_t)(1000 - bbr->r_ctl.bbr_google_discount);
3595 cbw /= (uint64_t)1000;
3596 /* We don't apply a discount if it results in 0 */
3600 lentim = ((uint64_t)len *
3601 (uint64_t)USECS_IN_SECOND *
3602 (uint64_t)BBR_UNIT);
3603 res = lentim / ((uint64_t)gain * bw);
3606 usecs = (uint32_t)res;
3607 srtt = bbr_get_rtt(bbr, BBR_SRTT);
3608 if (bbr_hptsi_max_mul && bbr_hptsi_max_div &&
3609 (bbr->rc_use_google == 0) &&
3610 (usecs > ((srtt * bbr_hptsi_max_mul) / bbr_hptsi_max_div))) {
3612 * We cannot let the delay be more than 1/2 the srtt time.
3613 * Otherwise we cannot pace out or send properly.
3615 over = usecs = (srtt * bbr_hptsi_max_mul) / bbr_hptsi_max_div;
3616 BBR_STAT_INC(bbr_hpts_min_time);
3619 bbr_log_pacing_delay_calc(bbr, gain, len, cts, usecs, bw, over, 1);
3624 bbr_ack_received(struct tcpcb *tp, struct tcp_bbr *bbr, struct tcphdr *th, uint32_t bytes_this_ack,
3625 uint32_t sack_changed, uint32_t prev_acked, int32_t line, uint32_t losses)
3627 INP_WLOCK_ASSERT(tp->t_inpcb);
3629 uint32_t cwnd, target_cwnd, saved_bytes, maxseg;
3633 if ((tp->t_flags & TF_GPUTINPROG) &&
3634 SEQ_GEQ(th->th_ack, tp->gput_ack)) {
3636 * Strech acks and compressed acks will cause this to
3637 * oscillate but we are doing it the same way as the main
3638 * stack so it will be compariable (though possibly not
3642 int64_t gput, time_stamp;
3644 gput = (int64_t) (th->th_ack - tp->gput_seq) * 8;
3645 time_stamp = max(1, ((bbr->r_ctl.rc_rcvtime - tp->gput_ts) / 1000));
3646 cgput = gput / time_stamp;
3647 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
3649 if (tp->t_stats_gput_prev > 0)
3650 stats_voi_update_abs_s32(tp->t_stats,
3652 ((gput - tp->t_stats_gput_prev) * 100) /
3653 tp->t_stats_gput_prev);
3654 tp->t_flags &= ~TF_GPUTINPROG;
3655 tp->t_stats_gput_prev = cgput;
3658 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) &&
3659 ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google)) {
3660 /* We don't change anything in probe-rtt */
3663 maxseg = tp->t_maxseg - bbr->rc_last_options;
3664 saved_bytes = bytes_this_ack;
3665 bytes_this_ack += sack_changed;
3666 if (bytes_this_ack > prev_acked) {
3667 bytes_this_ack -= prev_acked;
3669 * A byte ack'd gives us a full mss
3670 * to be like linux i.e. they count packets.
3672 if ((bytes_this_ack < maxseg) && bbr->rc_use_google)
3673 bytes_this_ack = maxseg;
3678 cwnd = tp->snd_cwnd;
3679 bw = get_filter_value(&bbr->r_ctl.rc_delrate);
3681 target_cwnd = bbr_get_target_cwnd(bbr,
3683 (uint32_t)bbr->r_ctl.rc_bbr_cwnd_gain);
3685 target_cwnd = bbr_initial_cwnd(bbr, bbr->rc_tp);
3686 if (IN_RECOVERY(tp->t_flags) &&
3687 (bbr->bbr_prev_in_rec == 0)) {
3689 * We are entering recovery and
3690 * thus packet conservation.
3692 bbr->pkt_conservation = 1;
3693 bbr->r_ctl.rc_recovery_start = bbr->r_ctl.rc_rcvtime;
3694 cwnd = ctf_flight_size(tp,
3695 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) +
3698 if (IN_RECOVERY(tp->t_flags)) {
3701 bbr->bbr_prev_in_rec = 1;
3702 if (cwnd > losses) {
3708 flight = ctf_flight_size(tp,
3709 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
3710 bbr_log_type_cwndupd(bbr, flight, 0,
3711 losses, 10, 0, 0, line);
3712 if (bbr->pkt_conservation) {
3715 if (TSTMP_GEQ(bbr->r_ctl.rc_rcvtime, bbr->r_ctl.rc_recovery_start))
3716 time_in = bbr->r_ctl.rc_rcvtime - bbr->r_ctl.rc_recovery_start;
3720 if (time_in >= bbr_get_rtt(bbr, BBR_RTT_PROP)) {
3721 /* Clear packet conservation after an rttProp */
3722 bbr->pkt_conservation = 0;
3724 if ((flight + bytes_this_ack) > cwnd)
3725 cwnd = flight + bytes_this_ack;
3726 if (cwnd < get_min_cwnd(bbr))
3727 cwnd = get_min_cwnd(bbr);
3728 tp->snd_cwnd = cwnd;
3729 bbr_log_type_cwndupd(bbr, saved_bytes, sack_changed,
3730 prev_acked, 1, target_cwnd, th->th_ack, line);
3735 bbr->bbr_prev_in_rec = 0;
3736 if ((bbr->rc_use_google == 0) && bbr->r_ctl.restrict_growth) {
3737 bbr->r_ctl.restrict_growth--;
3738 if (bytes_this_ack > maxseg)
3739 bytes_this_ack = maxseg;
3741 if (bbr->rc_filled_pipe) {
3743 * Here we have exited startup and filled the pipe. We will
3744 * thus allow the cwnd to shrink to the target. We hit here
3750 s_cwnd = min((cwnd + bytes_this_ack), target_cwnd);
3753 else if (bbr_cwnd_may_shrink || bbr->rc_use_google || bbr->rc_no_pacing)
3757 * Here we are still in startup, we increase cwnd by what
3760 if ((cwnd < target_cwnd) ||
3761 (bbr->rc_past_init_win == 0)) {
3763 cwnd += bytes_this_ack;
3766 * Method 4 means we are at target so no gain in
3767 * startup and past the initial window.
3772 tp->snd_cwnd = max(cwnd, get_min_cwnd(bbr));
3773 bbr_log_type_cwndupd(bbr, saved_bytes, sack_changed, prev_acked, meth, target_cwnd, th->th_ack, line);
3777 tcp_bbr_partialack(struct tcpcb *tp)
3779 struct tcp_bbr *bbr;
3781 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
3782 INP_WLOCK_ASSERT(tp->t_inpcb);
3783 if (ctf_flight_size(tp,
3784 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <=
3786 bbr->r_wanted_output = 1;
3791 bbr_post_recovery(struct tcpcb *tp)
3793 struct tcp_bbr *bbr;
3796 INP_WLOCK_ASSERT(tp->t_inpcb);
3797 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
3799 * Here we just exit recovery.
3801 EXIT_RECOVERY(tp->t_flags);
3802 /* Lock in our b/w reduction for the specified number of pkt-epochs */
3803 bbr->r_recovery_bw = 0;
3804 tp->snd_recover = tp->snd_una;
3805 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime);
3806 bbr->pkt_conservation = 0;
3807 if (bbr->rc_use_google == 0) {
3809 * For non-google mode lets
3810 * go ahead and make sure we clear
3811 * the recovery state so if we
3812 * bounce back in to recovery we
3815 bbr->bbr_prev_in_rec = 0;
3817 bbr_log_type_exit_rec(bbr);
3818 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) {
3819 tp->snd_cwnd = max(tp->snd_cwnd, bbr->r_ctl.rc_cwnd_on_ent);
3820 bbr_log_type_cwndupd(bbr, 0, 0, 0, 15, 0, 0, __LINE__);
3822 /* For probe-rtt case lets fix up its saved_cwnd */
3823 if (bbr->r_ctl.rc_saved_cwnd < bbr->r_ctl.rc_cwnd_on_ent) {
3824 bbr->r_ctl.rc_saved_cwnd = bbr->r_ctl.rc_cwnd_on_ent;
3825 bbr_log_type_cwndupd(bbr, 0, 0, 0, 16, 0, 0, __LINE__);
3828 flight = ctf_flight_size(tp,
3829 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
3830 if ((bbr->rc_use_google == 0) &&
3832 uint64_t val, lr2use;
3833 uint32_t maxseg, newcwnd, acks_inflight, ratio, cwnd;
3836 if (bbr_get_rtt(bbr, BBR_SRTT)) {
3837 val = ((uint64_t)bbr_get_rtt(bbr, BBR_RTT_PROP) * (uint64_t)1000);
3838 val /= bbr_get_rtt(bbr, BBR_SRTT);
3839 ratio = (uint32_t)val;
3843 bbr_log_type_cwndupd(bbr, bbr_red_mul, bbr_red_div,
3844 bbr->r_ctl.recovery_lr, 21,
3846 bbr->r_ctl.rc_red_cwnd_pe,
3848 if ((ratio < bbr_do_red) || (bbr_do_red == 0))
3850 if (((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) &&
3851 bbr_prtt_slam_cwnd) ||
3852 (bbr_sub_drain_slam_cwnd &&
3853 (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) &&
3854 bbr->rc_hit_state_1 &&
3855 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) ||
3856 ((bbr->rc_bbr_state == BBR_STATE_DRAIN) &&
3857 bbr_slam_cwnd_in_main_drain)) {
3859 * Here we must poke at the saved cwnd
3860 * as well as the cwnd.
3862 cwnd = bbr->r_ctl.rc_saved_cwnd;
3863 cwnd_p = &bbr->r_ctl.rc_saved_cwnd;
3865 cwnd = tp->snd_cwnd;
3866 cwnd_p = &tp->snd_cwnd;
3868 maxseg = tp->t_maxseg - bbr->rc_last_options;
3869 /* Add the overall lr with the recovery lr */
3870 if (bbr->r_ctl.rc_lost == 0)
3872 else if (bbr->r_ctl.rc_delivered == 0)
3875 lr2use = bbr->r_ctl.rc_lost * 1000;
3876 lr2use /= bbr->r_ctl.rc_delivered;
3878 lr2use += bbr->r_ctl.recovery_lr;
3879 acks_inflight = (flight / (maxseg * 2));
3880 if (bbr_red_scale) {
3881 lr2use *= bbr_get_rtt(bbr, BBR_SRTT);
3882 lr2use /= bbr_red_scale;
3883 if ((bbr_red_growth_restrict) &&
3884 ((bbr_get_rtt(bbr, BBR_SRTT)/bbr_red_scale) > 1))
3885 bbr->r_ctl.restrict_growth += acks_inflight;
3888 val = (uint64_t)cwnd * lr2use;
3891 newcwnd = roundup((cwnd - val), maxseg);
3895 val = (uint64_t)cwnd * (uint64_t)bbr_red_mul;
3896 val /= (uint64_t)bbr_red_div;
3897 newcwnd = roundup((uint32_t)val, maxseg);
3899 /* with standard delayed acks how many acks can I expect? */
3900 if (bbr_drop_limit == 0) {
3902 * Anticpate how much we will
3903 * raise the cwnd based on the acks.
3905 if ((newcwnd + (acks_inflight * maxseg)) < get_min_cwnd(bbr)) {
3906 /* We do enforce the min (with the acks) */
3907 newcwnd = (get_min_cwnd(bbr) - acks_inflight);
3911 * A strict drop limit of N is is inplace
3913 if (newcwnd < (bbr_drop_limit * maxseg)) {
3914 newcwnd = bbr_drop_limit * maxseg;
3917 /* For the next N acks do we restrict the growth */
3919 if (tp->snd_cwnd > newcwnd)
3920 tp->snd_cwnd = newcwnd;
3921 bbr_log_type_cwndupd(bbr, bbr_red_mul, bbr_red_div, val, 22,
3923 bbr_get_rtt(bbr, BBR_SRTT), __LINE__);
3924 bbr->r_ctl.rc_red_cwnd_pe = bbr->r_ctl.rc_pkt_epoch;
3927 bbr->r_ctl.recovery_lr = 0;
3928 if (flight <= tp->snd_cwnd) {
3929 bbr->r_wanted_output = 1;
3931 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime);
3935 bbr_setup_red_bw(struct tcp_bbr *bbr, uint32_t cts)
3937 bbr->r_ctl.red_bw = get_filter_value(&bbr->r_ctl.rc_delrate);
3938 /* Limit the drop in b/w to 1/2 our current filter. */
3939 if (bbr->r_ctl.red_bw > bbr->r_ctl.rc_bbr_cur_del_rate)
3940 bbr->r_ctl.red_bw = bbr->r_ctl.rc_bbr_cur_del_rate;
3941 if (bbr->r_ctl.red_bw < (get_filter_value(&bbr->r_ctl.rc_delrate) / 2))
3942 bbr->r_ctl.red_bw = get_filter_value(&bbr->r_ctl.rc_delrate) / 2;
3943 tcp_bbr_tso_size_check(bbr, cts);
3947 bbr_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type, struct bbr_sendmap *rsm)
3949 struct tcp_bbr *bbr;
3951 INP_WLOCK_ASSERT(tp->t_inpcb);
3952 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
3955 if (!IN_RECOVERY(tp->t_flags)) {
3956 tp->snd_recover = tp->snd_max;
3957 /* Start a new epoch */
3958 bbr_set_pktepoch(bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
3959 if (bbr->rc_lt_is_sampling || bbr->rc_lt_use_bw) {
3961 * Move forward the lt epoch
3962 * so it won't count the truncated
3965 bbr->r_ctl.rc_lt_epoch++;
3967 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) {
3969 * Just like the policer detection code
3970 * if we are in startup we must push
3971 * forward the last startup epoch
3972 * to hide the truncated PE.
3974 bbr->r_ctl.rc_bbr_last_startup_epoch++;
3976 bbr->r_ctl.rc_cwnd_on_ent = tp->snd_cwnd;
3977 ENTER_RECOVERY(tp->t_flags);
3978 bbr->rc_tlp_rtx_out = 0;
3979 bbr->r_ctl.recovery_lr = bbr->r_ctl.rc_pkt_epoch_loss_rate;
3980 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime);
3981 if (bbr->rc_inp->inp_in_hpts &&
3982 ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) == 0)) {
3984 * When we enter recovery, we need to restart
3985 * any timers. This may mean we gain an agg
3986 * early, which will be made up for at the last
3989 bbr->rc_timer_first = 1;
3990 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
3993 * Calculate a new cwnd based on to the current
3994 * delivery rate with no gain. We get the bdp
3995 * without gaining it up like we normally would and
3996 * we use the last cur_del_rate.
3998 if ((bbr->rc_use_google == 0) &&
3999 (bbr->r_ctl.bbr_rttprobe_gain_val ||
4000 (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT))) {
4001 tp->snd_cwnd = ctf_flight_size(tp,
4002 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) +
4003 (tp->t_maxseg - bbr->rc_last_options);
4004 if (tp->snd_cwnd < get_min_cwnd(bbr)) {
4005 /* We always gate to min cwnd */
4006 tp->snd_cwnd = get_min_cwnd(bbr);
4008 bbr_log_type_cwndupd(bbr, 0, 0, 0, 14, 0, 0, __LINE__);
4010 bbr_log_type_enter_rec(bbr, rsm->r_start);
4014 KMOD_TCPSTAT_INC(tcps_sndrexmitbad);
4015 /* RTO was unnecessary, so reset everything. */
4016 bbr_reset_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime);
4017 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) {
4018 tp->snd_cwnd = tp->snd_cwnd_prev;
4019 tp->snd_ssthresh = tp->snd_ssthresh_prev;
4020 tp->snd_recover = tp->snd_recover_prev;
4021 tp->snd_cwnd = max(tp->snd_cwnd, bbr->r_ctl.rc_cwnd_on_ent);
4022 bbr_log_type_cwndupd(bbr, 0, 0, 0, 13, 0, 0, __LINE__);
4024 tp->t_badrxtwin = 0;
4030 * Indicate whether this ack should be delayed. We can delay the ack if
4031 * following conditions are met:
4032 * - There is no delayed ack timer in progress.
4033 * - Our last ack wasn't a 0-sized window. We never want to delay
4034 * the ack that opens up a 0-sized window.
4035 * - LRO wasn't used for this segment. We make sure by checking that the
4036 * segment size is not larger than the MSS.
4037 * - Delayed acks are enabled or this is a half-synchronized T/TCP
4039 * - The data being acked is less than a full segment (a stretch ack
4040 * of more than a segment we should ack.
4041 * - nsegs is 1 (if its more than that we received more than 1 ack).
4043 #define DELAY_ACK(tp, bbr, nsegs) \
4044 (((tp->t_flags & TF_RXWIN0SENT) == 0) && \
4045 ((tp->t_flags & TF_DELACK) == 0) && \
4046 ((bbr->bbr_segs_rcvd + nsegs) < tp->t_delayed_ack) && \
4047 (tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN)))
4050 * Return the lowest RSM in the map of
4051 * packets still in flight that is not acked.
4052 * This should normally find on the first one
4053 * since we remove packets from the send
4054 * map after they are marked ACKED.
4056 static struct bbr_sendmap *
4057 bbr_find_lowest_rsm(struct tcp_bbr *bbr)
4059 struct bbr_sendmap *rsm;
4062 * Walk the time-order transmitted list looking for an rsm that is
4063 * not acked. This will be the one that was sent the longest time
4064 * ago that is still outstanding.
4066 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_tmap, r_tnext) {
4067 if (rsm->r_flags & BBR_ACKED) {
4076 static struct bbr_sendmap *
4077 bbr_find_high_nonack(struct tcp_bbr *bbr, struct bbr_sendmap *rsm)
4079 struct bbr_sendmap *prsm;
4082 * Walk the sequence order list backward until we hit and arrive at
4083 * the highest seq not acked. In theory when this is called it
4084 * should be the last segment (which it was not).
4087 TAILQ_FOREACH_REVERSE_FROM(prsm, &bbr->r_ctl.rc_map, bbr_head, r_next) {
4088 if (prsm->r_flags & (BBR_ACKED | BBR_HAS_FIN)) {
4097 * Returns to the caller the number of microseconds that
4098 * the packet can be outstanding before we think we
4099 * should have had an ack returned.
4102 bbr_calc_thresh_rack(struct tcp_bbr *bbr, uint32_t srtt, uint32_t cts, struct bbr_sendmap *rsm)
4105 * lro is the flag we use to determine if we have seen reordering.
4106 * If it gets set we have seen reordering. The reorder logic either
4107 * works in one of two ways:
4109 * If reorder-fade is configured, then we track the last time we saw
4110 * re-ordering occur. If we reach the point where enough time as
4111 * passed we no longer consider reordering has occuring.
4113 * Or if reorder-face is 0, then once we see reordering we consider
4114 * the connection to alway be subject to reordering and just set lro
4117 * In the end if lro is non-zero we add the extra time for
4121 uint32_t thresh, t_rxtcur;
4125 if (bbr->r_ctl.rc_reorder_ts) {
4126 if (bbr->r_ctl.rc_reorder_fade) {
4127 if (SEQ_GEQ(cts, bbr->r_ctl.rc_reorder_ts)) {
4128 lro = cts - bbr->r_ctl.rc_reorder_ts;
4131 * No time as passed since the last
4132 * reorder, mark it as reordering.
4137 /* Negative time? */
4140 if (lro > bbr->r_ctl.rc_reorder_fade) {
4141 /* Turn off reordering seen too */
4142 bbr->r_ctl.rc_reorder_ts = 0;
4146 /* Reodering does not fade */
4152 thresh = srtt + bbr->r_ctl.rc_pkt_delay;
4154 /* It must be set, if not you get 1/4 rtt */
4155 if (bbr->r_ctl.rc_reorder_shift)
4156 thresh += (srtt >> bbr->r_ctl.rc_reorder_shift);
4158 thresh += (srtt >> 2);
4162 /* We don't let the rack timeout be above a RTO */
4163 if ((bbr->rc_tp)->t_srtt == 0)
4164 t_rxtcur = BBR_INITIAL_RTO;
4166 t_rxtcur = TICKS_2_USEC(bbr->rc_tp->t_rxtcur);
4167 if (thresh > t_rxtcur) {
4170 /* And we don't want it above the RTO max either */
4171 if (thresh > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) {
4172 thresh = (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND);
4174 bbr_log_thresh_choice(bbr, cts, thresh, lro, srtt, rsm, BBR_TO_FRM_RACK);
4179 * Return to the caller the amount of time in mico-seconds
4180 * that should be used for the TLP timer from the last
4181 * send time of this packet.
4184 bbr_calc_thresh_tlp(struct tcpcb *tp, struct tcp_bbr *bbr,
4185 struct bbr_sendmap *rsm, uint32_t srtt,
4188 uint32_t thresh, len, maxseg, t_rxtcur;
4189 struct bbr_sendmap *prsm;
4193 if (bbr->rc_tlp_threshold)
4194 thresh = srtt + (srtt / bbr->rc_tlp_threshold);
4196 thresh = (srtt * 2);
4197 maxseg = tp->t_maxseg - bbr->rc_last_options;
4198 /* Get the previous sent packet, if any */
4199 len = rsm->r_end - rsm->r_start;
4202 prsm = TAILQ_PREV(rsm, bbr_head, r_tnext);
4203 if (prsm && (len <= maxseg)) {
4205 * Two packets outstanding, thresh should be (2*srtt) +
4206 * possible inter-packet delay (if any).
4208 uint32_t inter_gap = 0;
4211 idx = rsm->r_rtr_cnt - 1;
4212 nidx = prsm->r_rtr_cnt - 1;
4213 if (TSTMP_GEQ(rsm->r_tim_lastsent[nidx], prsm->r_tim_lastsent[idx])) {
4214 /* Yes it was sent later (or at the same time) */
4215 inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx];
4217 thresh += inter_gap;
4218 } else if (len <= maxseg) {
4220 * Possibly compensate for delayed-ack.
4222 uint32_t alt_thresh;
4224 alt_thresh = srtt + (srtt / 2) + bbr_delayed_ack_time;
4225 if (alt_thresh > thresh)
4226 thresh = alt_thresh;
4228 /* Not above the current RTO */
4229 if (tp->t_srtt == 0)
4230 t_rxtcur = BBR_INITIAL_RTO;
4232 t_rxtcur = TICKS_2_USEC(tp->t_rxtcur);
4234 bbr_log_thresh_choice(bbr, cts, thresh, t_rxtcur, srtt, rsm, BBR_TO_FRM_TLP);
4235 /* Not above an RTO */
4236 if (thresh > t_rxtcur) {
4239 /* Not above a RTO max */
4240 if (thresh > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) {
4241 thresh = (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND);
4243 /* And now apply the user TLP min */
4244 if (thresh < bbr_tlp_min) {
4245 thresh = bbr_tlp_min;
4251 * Return one of three RTTs to use (in microseconds).
4253 static __inline uint32_t
4254 bbr_get_rtt(struct tcp_bbr *bbr, int32_t rtt_type)
4259 f_rtt = get_filter_value_small(&bbr->r_ctl.rc_rttprop);
4260 if (get_filter_value_small(&bbr->r_ctl.rc_rttprop) == 0xffffffff) {
4261 /* We have no rtt at all */
4262 if (bbr->rc_tp->t_srtt == 0)
4263 f_rtt = BBR_INITIAL_RTO;
4265 f_rtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT);
4267 * Since we don't know how good the rtt is apply a
4270 if (f_rtt < bbr_delayed_ack_time) {
4271 f_rtt = bbr_delayed_ack_time;
4274 /* Take the filter version or last measured pkt-rtt */
4275 if (rtt_type == BBR_RTT_PROP) {
4277 } else if (rtt_type == BBR_RTT_PKTRTT) {
4278 if (bbr->r_ctl.rc_pkt_epoch_rtt) {
4279 srtt = bbr->r_ctl.rc_pkt_epoch_rtt;
4281 /* No pkt rtt yet */
4284 } else if (rtt_type == BBR_RTT_RACK) {
4285 srtt = bbr->r_ctl.rc_last_rtt;
4286 /* We need to add in any internal delay for our timer */
4287 if (bbr->rc_ack_was_delayed)
4288 srtt += bbr->r_ctl.rc_ack_hdwr_delay;
4289 } else if (rtt_type == BBR_SRTT) {
4290 srtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT);
4294 #ifdef BBR_INVARIANTS
4295 panic("Unknown rtt request type %d", rtt_type);
4302 bbr_is_lost(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t cts)
4307 thresh = bbr_calc_thresh_rack(bbr, bbr_get_rtt(bbr, BBR_RTT_RACK),
4309 if ((cts - rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]) >= thresh) {
4310 /* It is lost (past time) */
4317 * Return a sendmap if we need to retransmit something.
4319 static struct bbr_sendmap *
4320 bbr_check_recovery_mode(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4323 * Check to see that we don't need to fall into recovery. We will
4324 * need to do so if our oldest transmit is past the time we should
4328 struct bbr_sendmap *rsm;
4331 if (TAILQ_EMPTY(&bbr->r_ctl.rc_map)) {
4332 /* Nothing outstanding that we know of */
4335 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
4337 /* Nothing in the transmit map */
4340 if (tp->t_flags & TF_SENTFIN) {
4341 /* Fin restricted, don't find anything once a fin is sent */
4344 if (rsm->r_flags & BBR_ACKED) {
4346 * Ok the first one is acked (this really should not happen
4347 * since we remove the from the tmap once they are acked)
4349 rsm = bbr_find_lowest_rsm(bbr);
4353 idx = rsm->r_rtr_cnt - 1;
4354 if (SEQ_LEQ(cts, rsm->r_tim_lastsent[idx])) {
4355 /* Send timestamp is the same or less? can't be ready */
4358 /* Get our RTT time */
4359 if (bbr_is_lost(bbr, rsm, cts) &&
4360 ((rsm->r_dupack >= DUP_ACK_THRESHOLD) ||
4361 (rsm->r_flags & BBR_SACK_PASSED))) {
4362 if ((rsm->r_flags & BBR_MARKED_LOST) == 0) {
4363 rsm->r_flags |= BBR_MARKED_LOST;
4364 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start;
4365 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start;
4367 bbr_cong_signal(tp, NULL, CC_NDUPACK, rsm);
4368 #ifdef BBR_INVARIANTS
4369 if ((rsm->r_end - rsm->r_start) == 0)
4370 panic("tp:%p bbr:%p rsm:%p length is 0?", tp, bbr, rsm);
4378 * RACK Timer, here we simply do logging and house keeping.
4379 * the normal bbr_output_wtime() function will call the
4380 * appropriate thing to check if we need to do a RACK retransmit.
4381 * We return 1, saying don't proceed with bbr_output_wtime only
4382 * when all timers have been stopped (destroyed PCB?).
4385 bbr_timeout_rack(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4388 * This timer simply provides an internal trigger to send out data.
4389 * The check_recovery_mode call will see if there are needed
4390 * retransmissions, if so we will enter fast-recovery. The output
4391 * call may or may not do the same thing depending on sysctl
4396 if (bbr->rc_all_timers_stopped) {
4399 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) {
4400 /* Its not time yet */
4403 BBR_STAT_INC(bbr_to_tot);
4404 lost = bbr->r_ctl.rc_lost;
4405 if (bbr->r_state && (bbr->r_state != tp->t_state))
4406 bbr_set_state(tp, bbr, 0);
4407 bbr_log_to_event(bbr, cts, BBR_TO_FRM_RACK);
4408 if (bbr->r_ctl.rc_resend == NULL) {
4409 /* Lets do the check here */
4410 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts);
4412 if (bbr_policer_call_from_rack_to)
4413 bbr_lt_bw_sampling(bbr, cts, (bbr->r_ctl.rc_lost > lost));
4414 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK;
4418 static __inline void
4419 bbr_clone_rsm(struct tcp_bbr *bbr, struct bbr_sendmap *nrsm, struct bbr_sendmap *rsm, uint32_t start)
4423 nrsm->r_start = start;
4424 nrsm->r_end = rsm->r_end;
4425 nrsm->r_rtr_cnt = rsm->r_rtr_cnt;
4426 nrsm->r_flags = rsm->r_flags;
4427 /* We don't transfer forward the SYN flag */
4428 nrsm->r_flags &= ~BBR_HAS_SYN;
4429 /* We move forward the FIN flag, not that this should happen */
4430 rsm->r_flags &= ~BBR_HAS_FIN;
4431 nrsm->r_dupack = rsm->r_dupack;
4432 nrsm->r_rtr_bytes = 0;
4433 nrsm->r_is_gain = rsm->r_is_gain;
4434 nrsm->r_is_drain = rsm->r_is_drain;
4435 nrsm->r_delivered = rsm->r_delivered;
4436 nrsm->r_ts_valid = rsm->r_ts_valid;
4437 nrsm->r_del_ack_ts = rsm->r_del_ack_ts;
4438 nrsm->r_del_time = rsm->r_del_time;
4439 nrsm->r_app_limited = rsm->r_app_limited;
4440 nrsm->r_first_sent_time = rsm->r_first_sent_time;
4441 nrsm->r_flight_at_send = rsm->r_flight_at_send;
4442 /* We split a piece the lower section looses any just_ret flag. */
4443 nrsm->r_bbr_state = rsm->r_bbr_state;
4444 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) {
4445 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx];
4447 rsm->r_end = nrsm->r_start;
4448 idx = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs);
4450 /* Check if we got too small */
4451 if ((rsm->r_is_smallmap == 0) &&
4452 ((rsm->r_end - rsm->r_start) <= idx)) {
4453 bbr->r_ctl.rc_num_small_maps_alloced++;
4454 rsm->r_is_smallmap = 1;
4456 /* Check the new one as well */
4457 if ((nrsm->r_end - nrsm->r_start) <= idx) {
4458 bbr->r_ctl.rc_num_small_maps_alloced++;
4459 nrsm->r_is_smallmap = 1;
4464 bbr_sack_mergable(struct bbr_sendmap *at,
4465 uint32_t start, uint32_t end)
4468 * Given a sack block defined by
4469 * start and end, and a current postion
4470 * at. Return 1 if either side of at
4471 * would show that the block is mergable
4472 * to that side. A block to be mergable
4473 * must have overlap with the start/end
4474 * and be in the SACK'd state.
4476 struct bbr_sendmap *l_rsm;
4477 struct bbr_sendmap *r_rsm;
4479 /* first get the either side blocks */
4480 l_rsm = TAILQ_PREV(at, bbr_head, r_next);
4481 r_rsm = TAILQ_NEXT(at, r_next);
4482 if (l_rsm && (l_rsm->r_flags & BBR_ACKED)) {
4483 /* Potentially mergeable */
4484 if ((l_rsm->r_end == start) ||
4485 (SEQ_LT(start, l_rsm->r_end) &&
4486 SEQ_GT(end, l_rsm->r_end))) {
4497 if (r_rsm && (r_rsm->r_flags & BBR_ACKED)) {
4498 /* Potentially mergeable */
4499 if ((r_rsm->r_start == end) ||
4500 (SEQ_LT(start, r_rsm->r_start) &&
4501 SEQ_GT(end, r_rsm->r_start))) {
4503 * map blk |---------|
4506 * map blk |---------|
4507 * sack blk |-------|
4515 static struct bbr_sendmap *
4516 bbr_merge_rsm(struct tcp_bbr *bbr,
4517 struct bbr_sendmap *l_rsm,
4518 struct bbr_sendmap *r_rsm)
4521 * We are merging two ack'd RSM's,
4522 * the l_rsm is on the left (lower seq
4523 * values) and the r_rsm is on the right
4524 * (higher seq value). The simplest way
4525 * to merge these is to move the right
4526 * one into the left. I don't think there
4527 * is any reason we need to try to find
4528 * the oldest (or last oldest retransmitted).
4530 l_rsm->r_end = r_rsm->r_end;
4531 if (l_rsm->r_dupack < r_rsm->r_dupack)
4532 l_rsm->r_dupack = r_rsm->r_dupack;
4533 if (r_rsm->r_rtr_bytes)
4534 l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes;
4535 if (r_rsm->r_in_tmap) {
4536 /* This really should not happen */
4537 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, r_rsm, r_tnext);
4539 if (r_rsm->r_app_limited)
4540 l_rsm->r_app_limited = r_rsm->r_app_limited;
4542 if (r_rsm->r_flags & BBR_HAS_FIN)
4543 l_rsm->r_flags |= BBR_HAS_FIN;
4544 if (r_rsm->r_flags & BBR_TLP)
4545 l_rsm->r_flags |= BBR_TLP;
4546 if (r_rsm->r_flags & BBR_RWND_COLLAPSED)
4547 l_rsm->r_flags |= BBR_RWND_COLLAPSED;
4548 if (r_rsm->r_flags & BBR_MARKED_LOST) {
4549 /* This really should not happen */
4550 bbr->r_ctl.rc_lost_bytes -= r_rsm->r_end - r_rsm->r_start;
4552 TAILQ_REMOVE(&bbr->r_ctl.rc_map, r_rsm, r_next);
4553 if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) {
4554 /* Transfer the split limit to the map we free */
4555 r_rsm->r_limit_type = l_rsm->r_limit_type;
4556 l_rsm->r_limit_type = 0;
4558 bbr_free(bbr, r_rsm);
4563 * TLP Timer, here we simply setup what segment we want to
4564 * have the TLP expire on, the normal bbr_output_wtime() will then
4567 * We return 1, saying don't proceed with bbr_output_wtime only
4568 * when all timers have been stopped (destroyed PCB?).
4571 bbr_timeout_tlp(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4576 struct bbr_sendmap *rsm = NULL;
4579 uint32_t out, avail;
4581 int collapsed_win = 0;
4583 if (bbr->rc_all_timers_stopped) {
4586 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) {
4587 /* Its not time yet */
4590 if (ctf_progress_timeout_check(tp, true)) {
4591 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
4592 tcp_set_inp_to_drop(bbr->rc_inp, ETIMEDOUT);
4595 /* Did we somehow get into persists? */
4596 if (bbr->rc_in_persist) {
4599 if (bbr->r_state && (bbr->r_state != tp->t_state))
4600 bbr_set_state(tp, bbr, 0);
4601 BBR_STAT_INC(bbr_tlp_tot);
4602 maxseg = tp->t_maxseg - bbr->rc_last_options;
4604 if (bbr->rc_inp->inp_socket->so_snd.sb_flags & SB_TLS_IFNET) {
4606 * For hardware TLS we do *not* want to send
4613 * A TLP timer has expired. We have been idle for 2 rtts. So we now
4614 * need to figure out how to force a full MSS segment out.
4616 so = tp->t_inpcb->inp_socket;
4617 avail = sbavail(&so->so_snd);
4618 out = ctf_outstanding(tp);
4619 if (out > tp->snd_wnd) {
4620 /* special case, we need a retransmission */
4625 /* New data is available */
4629 } else if ((amm < maxseg) && ((tp->t_flags & TF_NODELAY) == 0)) {
4630 /* not enough to fill a MTU and no-delay is off */
4633 /* Set the send-new override */
4634 if ((out + amm) <= tp->snd_wnd) {
4635 bbr->rc_tlp_new_data = 1;
4639 bbr->r_ctl.rc_tlp_seg_send_cnt = 0;
4640 bbr->r_ctl.rc_last_tlp_seq = tp->snd_max;
4641 bbr->r_ctl.rc_tlp_send = NULL;
4643 BBR_STAT_INC(bbr_tlp_newdata);
4648 * Ok we need to arrange the last un-acked segment to be re-sent, or
4649 * optionally the first un-acked segment.
4651 if (collapsed_win == 0) {
4652 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next);
4653 if (rsm && (BBR_ACKED | BBR_HAS_FIN)) {
4654 rsm = bbr_find_high_nonack(bbr, rsm);
4661 * We must find the last segment
4662 * that was acceptable by the client.
4664 TAILQ_FOREACH_REVERSE(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) {
4665 if ((rsm->r_flags & BBR_RWND_COLLAPSED) == 0) {
4671 /* None? if so send the first */
4672 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
4677 if ((rsm->r_end - rsm->r_start) > maxseg) {
4679 * We need to split this the last segment in two.
4681 struct bbr_sendmap *nrsm;
4683 nrsm = bbr_alloc_full_limit(bbr);
4686 * We can't get memory to split, we can either just
4687 * not split it. Or retransmit the whole piece, lets
4688 * do the large send (BTLP :-) ).
4692 bbr_clone_rsm(bbr, nrsm, rsm, (rsm->r_end - maxseg));
4693 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
4694 if (rsm->r_in_tmap) {
4695 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
4696 nrsm->r_in_tmap = 1;
4698 rsm->r_flags &= (~BBR_HAS_FIN);
4702 bbr->r_ctl.rc_tlp_send = rsm;
4703 bbr->rc_tlp_rtx_out = 1;
4704 if (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq) {
4705 bbr->r_ctl.rc_tlp_seg_send_cnt++;
4708 bbr->r_ctl.rc_last_tlp_seq = rsm->r_start;
4709 bbr->r_ctl.rc_tlp_seg_send_cnt = 1;
4712 if (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend) {
4714 * Can't [re]/transmit a segment we have retranmitted the
4715 * max times. We need the retransmit timer to take over.
4718 bbr->rc_tlp_new_data = 0;
4719 bbr->r_ctl.rc_tlp_send = NULL;
4721 rsm->r_flags &= ~BBR_TLP;
4722 BBR_STAT_INC(bbr_tlp_retran_fail);
4725 rsm->r_flags |= BBR_TLP;
4727 if (rsm && (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq) &&
4728 (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend)) {
4730 * We have retransmitted to many times for TLP. Switch to
4731 * the regular RTO timer
4735 bbr_log_to_event(bbr, cts, BBR_TO_FRM_TLP);
4736 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
4741 * Delayed ack Timer, here we simply need to setup the
4742 * ACK_NOW flag and remove the DELACK flag. From there
4743 * the output routine will send the ack out.
4745 * We only return 1, saying don't proceed, if all timers
4746 * are stopped (destroyed PCB?).
4749 bbr_timeout_delack(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4751 if (bbr->rc_all_timers_stopped) {
4754 bbr_log_to_event(bbr, cts, BBR_TO_FRM_DELACK);
4755 tp->t_flags &= ~TF_DELACK;
4756 tp->t_flags |= TF_ACKNOW;
4757 KMOD_TCPSTAT_INC(tcps_delack);
4758 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
4763 * Here we send a KEEP-ALIVE like probe to the
4764 * peer, we do not send data.
4766 * We only return 1, saying don't proceed, if all timers
4767 * are stopped (destroyed PCB?).
4770 bbr_timeout_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4772 struct tcptemp *t_template;
4775 if (bbr->rc_all_timers_stopped) {
4778 if (bbr->rc_in_persist == 0)
4780 KASSERT(tp->t_inpcb != NULL,
4781 ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
4783 * Persistence timer into zero window. Force a byte to be output, if
4786 bbr_log_to_event(bbr, cts, BBR_TO_FRM_PERSIST);
4787 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT;
4788 KMOD_TCPSTAT_INC(tcps_persisttimeo);
4790 * Have we exceeded the user specified progress time?
4792 if (ctf_progress_timeout_check(tp, true)) {
4793 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
4794 tcp_set_inp_to_drop(bbr->rc_inp, ETIMEDOUT);
4798 * Hack: if the peer is dead/unreachable, we do not time out if the
4799 * window is closed. After a full backoff, drop the connection if
4800 * the idle time (no responses to probes) reaches the maximum
4801 * backoff that we would use if retransmitting.
4803 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
4804 (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
4805 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
4806 KMOD_TCPSTAT_INC(tcps_persistdrop);
4807 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
4808 tcp_set_inp_to_drop(bbr->rc_inp, ETIMEDOUT);
4811 if ((sbavail(&bbr->rc_inp->inp_socket->so_snd) == 0) &&
4812 tp->snd_una == tp->snd_max) {
4813 bbr_exit_persist(tp, bbr, cts, __LINE__);
4818 * If the user has closed the socket then drop a persisting
4819 * connection after a much reduced timeout.
4821 if (tp->t_state > TCPS_CLOSE_WAIT &&
4822 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
4823 KMOD_TCPSTAT_INC(tcps_persistdrop);
4824 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
4825 tcp_set_inp_to_drop(bbr->rc_inp, ETIMEDOUT);
4828 t_template = tcpip_maketemplate(bbr->rc_inp);
4830 tcp_respond(tp, t_template->tt_ipgen,
4831 &t_template->tt_t, (struct mbuf *)NULL,
4832 tp->rcv_nxt, tp->snd_una - 1, 0);
4833 /* This sends an ack */
4834 if (tp->t_flags & TF_DELACK)
4835 tp->t_flags &= ~TF_DELACK;
4836 free(t_template, M_TEMP);
4838 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
4840 bbr_start_hpts_timer(bbr, tp, cts, 3, 0, 0);
4846 * If a keepalive goes off, we had no other timers
4847 * happening. We always return 1 here since this
4848 * routine either drops the connection or sends
4849 * out a segment with respond.
4852 bbr_timeout_keepalive(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
4854 struct tcptemp *t_template;
4857 if (bbr->rc_all_timers_stopped) {
4860 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP;
4862 bbr_log_to_event(bbr, cts, BBR_TO_FRM_KEEP);
4864 * Keep-alive timer went off; send something or drop connection if
4865 * idle for too long.
4867 KMOD_TCPSTAT_INC(tcps_keeptimeo);
4868 if (tp->t_state < TCPS_ESTABLISHED)
4870 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
4871 tp->t_state <= TCPS_CLOSING) {
4872 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
4875 * Send a packet designed to force a response if the peer is
4876 * up and reachable: either an ACK if the connection is
4877 * still alive, or an RST if the peer has closed the
4878 * connection due to timeout or reboot. Using sequence
4879 * number tp->snd_una-1 causes the transmitted zero-length
4880 * segment to lie outside the receive window; by the
4881 * protocol spec, this requires the correspondent TCP to
4884 KMOD_TCPSTAT_INC(tcps_keepprobe);
4885 t_template = tcpip_maketemplate(inp);
4887 tcp_respond(tp, t_template->tt_ipgen,
4888 &t_template->tt_t, (struct mbuf *)NULL,
4889 tp->rcv_nxt, tp->snd_una - 1, 0);
4890 free(t_template, M_TEMP);
4893 bbr_start_hpts_timer(bbr, tp, cts, 4, 0, 0);
4896 KMOD_TCPSTAT_INC(tcps_keepdrops);
4897 tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
4898 tcp_set_inp_to_drop(bbr->rc_inp, ETIMEDOUT);
4903 * Retransmit helper function, clear up all the ack
4904 * flags and take care of important book keeping.
4907 bbr_remxt_tmr(struct tcpcb *tp)
4910 * The retransmit timer went off, all sack'd blocks must be
4913 struct bbr_sendmap *rsm, *trsm = NULL;
4914 struct tcp_bbr *bbr;
4917 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
4918 cts = tcp_get_usecs(&bbr->rc_tv);
4919 lost = bbr->r_ctl.rc_lost;
4920 if (bbr->r_state && (bbr->r_state != tp->t_state))
4921 bbr_set_state(tp, bbr, 0);
4923 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
4924 if (rsm->r_flags & BBR_ACKED) {
4928 if (rsm->r_in_tmap == 0) {
4929 /* We must re-add it back to the tlist */
4931 TAILQ_INSERT_HEAD(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
4933 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, trsm, rsm, r_tnext);
4937 old_flags = rsm->r_flags;
4938 rsm->r_flags |= BBR_RXT_CLEARED;
4939 rsm->r_flags &= ~(BBR_ACKED | BBR_SACK_PASSED | BBR_WAS_SACKPASS);
4940 bbr_log_type_rsmclear(bbr, cts, rsm, old_flags, __LINE__);
4942 if ((tp->t_state < TCPS_ESTABLISHED) &&
4943 (rsm->r_start == tp->snd_una)) {
4945 * Special case for TCP FO. Where
4946 * we sent more data beyond the snd_max.
4947 * We don't mark that as lost and stop here.
4951 if ((rsm->r_flags & BBR_MARKED_LOST) == 0) {
4952 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start;
4953 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start;
4955 if (bbr_marks_rxt_sack_passed) {
4957 * With this option, we will rack out
4958 * in 1ms increments the rest of the packets.
4960 rsm->r_flags |= BBR_SACK_PASSED | BBR_MARKED_LOST;
4961 rsm->r_flags &= ~BBR_WAS_SACKPASS;
4964 * With this option we only mark them lost
4965 * and remove all sack'd markings. We will run
4966 * another RXT or a TLP. This will cause
4967 * us to eventually send more based on what
4970 rsm->r_flags |= BBR_MARKED_LOST;
4971 rsm->r_flags &= ~BBR_WAS_SACKPASS;
4972 rsm->r_flags &= ~BBR_SACK_PASSED;
4977 bbr->r_ctl.rc_resend = TAILQ_FIRST(&bbr->r_ctl.rc_map);
4978 /* Clear the count (we just un-acked them) */
4979 bbr_log_to_event(bbr, cts, BBR_TO_FRM_TMR);
4980 bbr->rc_tlp_new_data = 0;
4981 bbr->r_ctl.rc_tlp_seg_send_cnt = 0;
4982 /* zap the behindness on a rxt */
4983 bbr->r_ctl.rc_hptsi_agg_delay = 0;
4984 bbr->r_agg_early_set = 0;
4985 bbr->r_ctl.rc_agg_early = 0;
4986 bbr->rc_tlp_rtx_out = 0;
4987 bbr->r_ctl.rc_sacked = 0;
4988 bbr->r_ctl.rc_sacklast = NULL;
4989 bbr->r_timer_override = 1;
4990 bbr_lt_bw_sampling(bbr, cts, (bbr->r_ctl.rc_lost > lost));
4994 * Re-transmit timeout! If we drop the PCB we will return 1, otherwise
4995 * we will setup to retransmit the lowest seq number outstanding.
4998 bbr_timeout_rxt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
5004 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT;
5005 if (bbr->rc_all_timers_stopped) {
5008 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
5009 (tp->snd_una == tp->snd_max)) {
5010 /* Nothing outstanding .. nothing to do */
5014 * Retransmission timer went off. Message has not been acked within
5015 * retransmit interval. Back off to a longer retransmit interval
5016 * and retransmit one segment.
5018 if (ctf_progress_timeout_check(tp, true)) {
5020 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
5021 tcp_set_inp_to_drop(bbr->rc_inp, ETIMEDOUT);
5025 if ((bbr->r_ctl.rc_resend == NULL) ||
5026 ((bbr->r_ctl.rc_resend->r_flags & BBR_RWND_COLLAPSED) == 0)) {
5028 * If the rwnd collapsed on
5029 * the one we are retransmitting
5030 * it does not count against the
5035 if (tp->t_rxtshift > TCP_MAXRXTSHIFT) {
5036 tp->t_rxtshift = TCP_MAXRXTSHIFT;
5037 KMOD_TCPSTAT_INC(tcps_timeoutdrop);
5039 tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
5040 tcp_set_inp_to_drop(bbr->rc_inp,
5041 (tp->t_softerror ? (uint16_t) tp->t_softerror : ETIMEDOUT));
5044 if (tp->t_state == TCPS_SYN_SENT) {
5046 * If the SYN was retransmitted, indicate CWND to be limited
5047 * to 1 segment in cc_conn_init().
5050 } else if (tp->t_rxtshift == 1) {
5052 * first retransmit; record ssthresh and cwnd so they can be
5053 * recovered if this turns out to be a "bad" retransmit. A
5054 * retransmit is considered "bad" if an ACK for this segment
5055 * is received within RTT/2 interval; the assumption here is
5056 * that the ACK was already in flight. See "On Estimating
5057 * End-to-End Network Path Properties" by Allman and Paxson
5060 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options;
5061 if (!IN_RECOVERY(tp->t_flags)) {
5062 tp->snd_cwnd_prev = tp->snd_cwnd;
5063 tp->snd_ssthresh_prev = tp->snd_ssthresh;
5064 tp->snd_recover_prev = tp->snd_recover;
5065 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
5066 tp->t_flags |= TF_PREVVALID;
5068 tp->t_flags &= ~TF_PREVVALID;
5070 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options;
5072 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options;
5073 tp->t_flags &= ~TF_PREVVALID;
5075 KMOD_TCPSTAT_INC(tcps_rexmttimeo);
5076 if ((tp->t_state == TCPS_SYN_SENT) ||
5077 (tp->t_state == TCPS_SYN_RECEIVED))
5078 rexmt = USEC_2_TICKS(BBR_INITIAL_RTO) * tcp_backoff[tp->t_rxtshift];
5080 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
5081 TCPT_RANGESET(tp->t_rxtcur, rexmt,
5082 MSEC_2_TICKS(bbr->r_ctl.rc_min_rto_ms),
5083 MSEC_2_TICKS(((uint32_t)bbr->rc_max_rto_sec) * 1000));
5085 * We enter the path for PLMTUD if connection is established or, if
5086 * connection is FIN_WAIT_1 status, reason for the last is that if
5087 * amount of data we send is very small, we could send it in couple
5088 * of packets and process straight to FIN. In that case we won't
5089 * catch ESTABLISHED state.
5092 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? true : false;
5096 if (((V_tcp_pmtud_blackhole_detect == 1) ||
5097 (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) ||
5098 (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) &&
5099 ((tp->t_state == TCPS_ESTABLISHED) ||
5100 (tp->t_state == TCPS_FIN_WAIT_1))) {
5103 * Idea here is that at each stage of mtu probe (usually,
5104 * 1448 -> 1188 -> 524) should be given 2 chances to recover
5105 * before further clamping down. 'tp->t_rxtshift % 2 == 0'
5106 * should take care of that.
5108 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) ==
5109 (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) &&
5110 (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 &&
5111 tp->t_rxtshift % 2 == 0)) {
5113 * Enter Path MTU Black-hole Detection mechanism: -
5114 * Disable Path MTU Discovery (IP "DF" bit). -
5115 * Reduce MTU to lower value than what we negotiated
5118 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
5120 * Record that we may have found a black
5123 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
5124 /* Keep track of previous MSS. */
5125 tp->t_pmtud_saved_maxseg = tp->t_maxseg;
5128 * Reduce the MSS to blackhole value or to the
5129 * default in an attempt to retransmit.
5132 isipv6 = bbr->r_is_v6;
5134 tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) {
5135 /* Use the sysctl tuneable blackhole MSS. */
5136 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
5137 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
5138 } else if (isipv6) {
5139 /* Use the default MSS. */
5140 tp->t_maxseg = V_tcp_v6mssdflt;
5142 * Disable Path MTU Discovery when we switch
5145 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
5146 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
5149 #if defined(INET6) && defined(INET)
5153 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) {
5154 /* Use the sysctl tuneable blackhole MSS. */
5155 tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
5156 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
5158 /* Use the default MSS. */
5159 tp->t_maxseg = V_tcp_mssdflt;
5161 * Disable Path MTU Discovery when we switch
5164 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
5165 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
5170 * If further retransmissions are still unsuccessful
5171 * with a lowered MTU, maybe this isn't a blackhole
5172 * and we restore the previous MSS and blackhole
5173 * detection flags. The limit '6' is determined by
5174 * giving each probe stage (1448, 1188, 524) 2
5175 * chances to recover.
5177 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
5178 (tp->t_rxtshift >= 6)) {
5179 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
5180 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
5181 tp->t_maxseg = tp->t_pmtud_saved_maxseg;
5182 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed);
5187 * Disable RFC1323 and SACK if we haven't got any response to our
5188 * third SYN to work-around some broken terminal servers (most of
5189 * which have hopefully been retired) that have bad VJ header
5190 * compression code which trashes TCP segments containing
5191 * unknown-to-them TCP options.
5193 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
5194 (tp->t_rxtshift == 3))
5195 tp->t_flags &= ~(TF_REQ_SCALE | TF_REQ_TSTMP | TF_SACK_PERMIT);
5197 * If we backed off this far, our srtt estimate is probably bogus.
5198 * Clobber it so we'll take the next rtt measurement as our srtt;
5199 * move the current srtt into rttvar to keep the current retransmit
5202 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
5205 in6_losing(tp->t_inpcb);
5208 in_losing(tp->t_inpcb);
5209 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
5212 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
5213 tp->snd_recover = tp->snd_max;
5214 tp->t_flags |= TF_ACKNOW;
5221 bbr_process_timers(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, uint8_t hpts_calling)
5224 int32_t timers = (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK);
5229 if (tp->t_state == TCPS_LISTEN) {
5230 /* no timers on listen sockets */
5231 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)
5235 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) {
5238 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
5240 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling);
5243 if (hpts_calling == 0) {
5245 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling);
5249 * Ok our timer went off early and we are not paced false
5250 * alarm, go back to sleep.
5252 left = bbr->r_ctl.rc_timer_exp - cts;
5254 bbr_log_to_processing(bbr, cts, ret, left, hpts_calling);
5255 tcp_hpts_insert(tp->t_inpcb, HPTS_USEC_TO_SLOTS(left));
5258 bbr->rc_tmr_stopped = 0;
5259 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK;
5260 if (timers & PACE_TMR_DELACK) {
5261 ret = bbr_timeout_delack(tp, bbr, cts);
5262 } else if (timers & PACE_TMR_PERSIT) {
5263 ret = bbr_timeout_persist(tp, bbr, cts);
5264 } else if (timers & PACE_TMR_RACK) {
5265 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
5266 ret = bbr_timeout_rack(tp, bbr, cts);
5267 } else if (timers & PACE_TMR_TLP) {
5268 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
5269 ret = bbr_timeout_tlp(tp, bbr, cts);
5270 } else if (timers & PACE_TMR_RXT) {
5271 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
5272 ret = bbr_timeout_rxt(tp, bbr, cts);
5273 } else if (timers & PACE_TMR_KEEP) {
5274 ret = bbr_timeout_keepalive(tp, bbr, cts);
5276 bbr_log_to_processing(bbr, cts, ret, timers, hpts_calling);
5281 bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts)
5283 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
5284 uint8_t hpts_removed = 0;
5286 if (bbr->rc_inp->inp_in_hpts &&
5287 (bbr->rc_timer_first == 1)) {
5289 * If we are canceling timer's when we have the
5290 * timer ahead of the output being paced. We also
5291 * must remove ourselves from the hpts.
5294 tcp_hpts_remove(bbr->rc_inp, HPTS_REMOVE_OUTPUT);
5295 if (bbr->r_ctl.rc_last_delay_val) {
5296 /* Update the last hptsi delay too */
5297 uint32_t time_since_send;
5299 if (TSTMP_GT(cts, bbr->rc_pacer_started))
5300 time_since_send = cts - bbr->rc_pacer_started;
5302 time_since_send = 0;
5303 if (bbr->r_ctl.rc_last_delay_val > time_since_send) {
5304 /* Cut down our slot time */
5305 bbr->r_ctl.rc_last_delay_val -= time_since_send;
5307 bbr->r_ctl.rc_last_delay_val = 0;
5309 bbr->rc_pacer_started = cts;
5312 bbr->rc_timer_first = 0;
5313 bbr_log_to_cancel(bbr, line, cts, hpts_removed);
5314 bbr->rc_tmr_stopped = bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
5315 bbr->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK);
5320 bbr_timer_stop(struct tcpcb *tp, uint32_t timer_type)
5322 struct tcp_bbr *bbr;
5324 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
5325 bbr->rc_all_timers_stopped = 1;
5330 * stop all timers always returning 0.
5333 bbr_stopall(struct tcpcb *tp)
5339 bbr_timer_activate(struct tcpcb *tp, uint32_t timer_type, uint32_t delta)
5345 * return true if a bbr timer (rack or tlp) is active.
5348 bbr_timer_active(struct tcpcb *tp, uint32_t timer_type)
5354 bbr_get_earliest_send_outstanding(struct tcp_bbr *bbr, struct bbr_sendmap *u_rsm, uint32_t cts)
5356 struct bbr_sendmap *rsm;
5358 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
5359 if ((rsm == NULL) || (u_rsm == rsm))
5361 return(rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]);
5365 bbr_update_rsm(struct tcpcb *tp, struct tcp_bbr *bbr,
5366 struct bbr_sendmap *rsm, uint32_t cts, uint32_t pacing_time)
5372 if (rsm->r_rtr_cnt > BBR_NUM_OF_RETRANS) {
5373 rsm->r_rtr_cnt = BBR_NUM_OF_RETRANS;
5374 rsm->r_flags |= BBR_OVERMAX;
5376 if (rsm->r_flags & BBR_RWND_COLLAPSED) {
5377 /* Take off the collapsed flag at rxt */
5378 rsm->r_flags &= ~BBR_RWND_COLLAPSED;
5380 if (rsm->r_flags & BBR_MARKED_LOST) {
5381 /* We have retransmitted, its no longer lost */
5382 rsm->r_flags &= ~BBR_MARKED_LOST;
5383 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
5385 if (rsm->r_flags & BBR_RXT_CLEARED) {
5387 * We hit a RXT timer on it and
5388 * we cleared the "acked" flag.
5389 * We now have it going back into
5390 * flight, we can remove the cleared
5391 * flag and possibly do accounting on
5394 rsm->r_flags &= ~BBR_RXT_CLEARED;
5396 if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & BBR_TLP) == 0)) {
5397 bbr->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start);
5398 rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start);
5400 idx = rsm->r_rtr_cnt - 1;
5401 rsm->r_tim_lastsent[idx] = cts;
5402 rsm->r_pacing_delay = pacing_time;
5403 rsm->r_delivered = bbr->r_ctl.rc_delivered;
5404 rsm->r_ts_valid = bbr->rc_ts_valid;
5405 if (bbr->rc_ts_valid)
5406 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts;
5407 if (bbr->r_ctl.r_app_limited_until)
5408 rsm->r_app_limited = 1;
5410 rsm->r_app_limited = 0;
5411 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW)
5412 rsm->r_bbr_state = bbr_state_val(bbr);
5414 rsm->r_bbr_state = 8;
5415 if (rsm->r_flags & BBR_ACKED) {
5416 /* Problably MTU discovery messing with us */
5419 old_flags = rsm->r_flags;
5420 rsm->r_flags &= ~BBR_ACKED;
5421 bbr_log_type_rsmclear(bbr, cts, rsm, old_flags, __LINE__);
5422 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
5423 if (bbr->r_ctl.rc_sacked == 0)
5424 bbr->r_ctl.rc_sacklast = NULL;
5426 if (rsm->r_in_tmap) {
5427 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
5429 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
5431 if (rsm->r_flags & BBR_SACK_PASSED) {
5432 /* We have retransmitted due to the SACK pass */
5433 rsm->r_flags &= ~BBR_SACK_PASSED;
5434 rsm->r_flags |= BBR_WAS_SACKPASS;
5436 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts);
5437 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp,
5438 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
5439 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next);
5440 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) {
5442 rsm->r_is_drain = 0;
5443 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) {
5444 rsm->r_is_drain = 1;
5447 rsm->r_is_drain = 0;
5450 rsm->r_del_time = bbr->r_ctl.rc_del_time; /* TEMP GOOGLE CODE */
5454 * Returns 0, or the sequence where we stopped
5455 * updating. We also update the lenp to be the amount
5460 bbr_update_entry(struct tcpcb *tp, struct tcp_bbr *bbr,
5461 struct bbr_sendmap *rsm, uint32_t cts, int32_t *lenp, uint32_t pacing_time)
5464 * We (re-)transmitted starting at rsm->r_start for some length
5465 * (possibly less than r_end.
5467 struct bbr_sendmap *nrsm;
5472 c_end = rsm->r_start + len;
5473 if (SEQ_GEQ(c_end, rsm->r_end)) {
5475 * We retransmitted the whole piece or more than the whole
5476 * slopping into the next rsm.
5478 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time);
5479 if (c_end == rsm->r_end) {
5485 /* Hangs over the end return whats left */
5486 act_len = rsm->r_end - rsm->r_start;
5487 *lenp = (len - act_len);
5488 return (rsm->r_end);
5490 /* We don't get out of this block. */
5493 * Here we retransmitted less than the whole thing which means we
5494 * have to split this into what was transmitted and what was not.
5496 nrsm = bbr_alloc_full_limit(bbr);
5502 * So here we are going to take the original rsm and make it what we
5503 * retransmitted. nrsm will be the tail portion we did not
5504 * retransmit. For example say the chunk was 1, 11 (10 bytes). And
5505 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to
5506 * 1, 6 and the new piece will be 6, 11.
5508 bbr_clone_rsm(bbr, nrsm, rsm, c_end);
5509 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
5511 if (rsm->r_in_tmap) {
5512 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
5513 nrsm->r_in_tmap = 1;
5515 rsm->r_flags &= (~BBR_HAS_FIN);
5516 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time);
5522 bbr_get_hardware_rate(struct tcp_bbr *bbr)
5526 bw = bbr_get_bw(bbr);
5527 bw *= (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN];
5528 bw /= (uint64_t)BBR_UNIT;
5533 bbr_setup_less_of_rate(struct tcp_bbr *bbr, uint32_t cts,
5534 uint64_t act_rate, uint64_t rate_wanted)
5537 * We could not get a full gains worth
5540 if (get_filter_value(&bbr->r_ctl.rc_delrate) >= act_rate) {
5541 /* we can't even get the real rate */
5545 bbr->gain_is_limited = 0;
5546 red = get_filter_value(&bbr->r_ctl.rc_delrate) - act_rate;
5548 filter_reduce_by(&bbr->r_ctl.rc_delrate, red, cts);
5550 /* We can use a lower gain */
5552 bbr->gain_is_limited = 1;
5557 bbr_update_hardware_pacing_rate(struct tcp_bbr *bbr, uint32_t cts)
5559 const struct tcp_hwrate_limit_table *nrte;
5560 int error, rate = -1;
5562 if (bbr->r_ctl.crte == NULL)
5564 if ((bbr->rc_inp->inp_route.ro_nh == NULL) ||
5565 (bbr->rc_inp->inp_route.ro_nh->nh_ifp == NULL)) {
5566 /* Lost our routes? */
5567 /* Clear the way for a re-attempt */
5568 bbr->bbr_attempt_hdwr_pace = 0;
5570 bbr->gain_is_limited = 0;
5572 bbr->bbr_hdrw_pacing = 0;
5573 counter_u64_add(bbr_flows_whdwr_pacing, -1);
5574 counter_u64_add(bbr_flows_nohdwr_pacing, 1);
5575 tcp_bbr_tso_size_check(bbr, cts);
5578 rate = bbr_get_hardware_rate(bbr);
5579 nrte = tcp_chg_pacing_rate(bbr->r_ctl.crte,
5581 bbr->rc_inp->inp_route.ro_nh->nh_ifp,
5583 (RS_PACING_GEQ|RS_PACING_SUB_OK),
5588 if (nrte != bbr->r_ctl.crte) {
5589 bbr->r_ctl.crte = nrte;
5591 BBR_STAT_INC(bbr_hdwr_rl_mod_ok);
5592 if (bbr->r_ctl.crte->rate < rate) {
5593 /* We have a problem */
5594 bbr_setup_less_of_rate(bbr, cts,
5595 bbr->r_ctl.crte->rate, rate);
5598 bbr->gain_is_limited = 0;
5602 /* A failure should release the tag */
5603 BBR_STAT_INC(bbr_hdwr_rl_mod_fail);
5604 bbr->gain_is_limited = 0;
5606 bbr->bbr_hdrw_pacing = 0;
5608 bbr_type_log_hdwr_pacing(bbr,
5609 bbr->r_ctl.crte->ptbl->rs_ifp,
5611 ((bbr->r_ctl.crte == NULL) ? 0 : bbr->r_ctl.crte->rate),
5619 bbr_adjust_for_hw_pacing(struct tcp_bbr *bbr, uint32_t cts)
5622 * If we have hardware pacing support
5623 * we need to factor that in for our
5626 const struct tcp_hwrate_limit_table *rlp;
5627 uint32_t cur_delay, seg_sz, maxseg, new_tso, delta, hdwr_delay;
5629 if ((bbr->bbr_hdrw_pacing == 0) ||
5630 (IN_RECOVERY(bbr->rc_tp->t_flags)) ||
5631 (bbr->r_ctl.crte == NULL))
5633 if (bbr->hw_pacing_set == 0) {
5634 /* Not yet by the hdwr pacing count delay */
5637 if (bbr_hdwr_pace_adjust == 0) {
5641 rlp = bbr->r_ctl.crte;
5642 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options)
5643 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
5645 maxseg = BBR_MIN_SEG - bbr->rc_last_options;
5647 * So lets first get the
5648 * time we will take between
5649 * TSO sized sends currently without
5652 cur_delay = bbr_get_pacing_delay(bbr, BBR_UNIT,
5653 bbr->r_ctl.rc_pace_max_segs, cts, 1);
5654 hdwr_delay = bbr->r_ctl.rc_pace_max_segs / maxseg;
5655 hdwr_delay *= rlp->time_between;
5656 if (cur_delay > hdwr_delay)
5657 delta = cur_delay - hdwr_delay;
5660 bbr_log_type_tsosize(bbr, cts, delta, cur_delay, hdwr_delay,
5661 (bbr->r_ctl.rc_pace_max_segs / maxseg),
5664 (delta < (max(rlp->time_between,
5665 bbr->r_ctl.bbr_hptsi_segments_delay_tar)))) {
5667 * Now lets divide by the pacing
5668 * time between each segment the
5669 * hardware sends rounding up and
5670 * derive a bytes from that. We multiply
5671 * that by bbr_hdwr_pace_adjust to get
5672 * more bang for our buck.
5674 * The goal is to have the software pacer
5675 * waiting no more than an additional
5676 * pacing delay if we can (without the
5677 * compensation i.e. x bbr_hdwr_pace_adjust).
5679 seg_sz = max(((cur_delay + rlp->time_between)/rlp->time_between),
5680 (bbr->r_ctl.rc_pace_max_segs/maxseg));
5681 seg_sz *= bbr_hdwr_pace_adjust;
5682 if (bbr_hdwr_pace_floor &&
5683 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) {
5684 /* Currently hardware paces
5685 * out rs_min_seg segments at a time.
5686 * We need to make sure we always send at least
5687 * a full burst of bbr_hdwr_pace_floor down.
5689 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg;
5692 } else if (delta == 0) {
5694 * The highest pacing rate is
5695 * above our b/w gained. This means
5696 * we probably are going quite fast at
5697 * the hardware highest rate. Lets just multiply
5698 * the calculated TSO size by the
5699 * multiplier factor (its probably
5700 * 4 segments in the default config for
5703 seg_sz = bbr->r_ctl.rc_pace_max_segs * bbr_hdwr_pace_adjust;
5704 if (bbr_hdwr_pace_floor &&
5705 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) {
5706 /* Currently hardware paces
5707 * out rs_min_seg segments at a time.
5708 * We need to make sure we always send at least
5709 * a full burst of bbr_hdwr_pace_floor down.
5711 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg;
5715 * The pacing time difference is so
5716 * big that the hardware will
5717 * pace out more rapidly then we
5718 * really want and then we
5719 * will have a long delay. Lets just keep
5720 * the same TSO size so its as if
5721 * we were not using hdwr pacing (we
5722 * just gain a bit of spacing from the
5723 * hardware if seg_sz > 1).
5725 seg_sz = bbr->r_ctl.rc_pace_max_segs;
5727 if (seg_sz > bbr->r_ctl.rc_pace_max_segs)
5730 new_tso = bbr->r_ctl.rc_pace_max_segs;
5731 if (new_tso >= (PACE_MAX_IP_BYTES-maxseg))
5732 new_tso = PACE_MAX_IP_BYTES - maxseg;
5734 if (new_tso != bbr->r_ctl.rc_pace_max_segs) {
5735 bbr_log_type_tsosize(bbr, cts, new_tso, 0, bbr->r_ctl.rc_pace_max_segs, maxseg, 0);
5736 bbr->r_ctl.rc_pace_max_segs = new_tso;
5741 tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts)
5744 uint32_t old_tso = 0, new_tso;
5745 uint32_t maxseg, bytes;
5748 * Google/linux uses the following algorithm to determine
5749 * the TSO size based on the b/w of the link (from Neal Cardwell email 9/27/18):
5751 * bytes = bw_in_bytes_per_second / 1000
5752 * bytes = min(bytes, 64k)
5753 * tso_segs = bytes / MSS
5758 * tso_segs = max(tso_segs, min_tso_segs)
5760 * * Note apply a device specific limit (we apply this in the
5762 * Note that before the initial measurement is made google bursts out
5763 * a full iwnd just like new-reno/cubic.
5765 * We do not use this algorithm. Instead we
5766 * use a two phased approach:
5768 * if ( bw <= per-tcb-cross-over)
5769 * goal_tso = calculate how much with this bw we
5770 * can send in goal-time seconds.
5771 * if (goal_tso > mss)
5772 * seg = goal_tso / mss
5776 * if (tso > per-tcb-max)
5778 * else if ( bw > 512Mbps)
5779 * tso = max-tso (64k/mss)
5781 * goal_tso = bw / per-tcb-divsor
5782 * seg = (goal_tso + mss-1)/mss
5785 * if (tso < per-tcb-floor)
5786 * tso = per-tcb-floor
5787 * if (tso > per-tcb-utter_max)
5788 * tso = per-tcb-utter_max
5790 * Note the default per-tcb-divisor is 1000 (same as google).
5791 * the goal cross over is 30Mbps however. To recreate googles
5792 * algorithm you need to set:
5794 * cross-over = 23,168,000 bps
5797 * per-tcb-divisor = 1000
5800 * This will get you "google bbr" behavior with respect to tso size.
5802 * Note we do set anything TSO size until we are past the initial
5803 * window. Before that we gnerally use either a single MSS
5804 * or we use the full IW size (so we burst a IW at a time)
5805 * Also note that Hardware-TLS is special and does alternate
5806 * things to minimize PCI Bus Bandwidth use.
5809 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options) {
5810 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
5812 maxseg = BBR_MIN_SEG - bbr->rc_last_options;
5815 if (bbr->rc_inp->inp_socket->so_snd.sb_flags & SB_TLS_IFNET) {
5816 tls_seg = ctf_get_opt_tls_size(bbr->rc_inp->inp_socket, bbr->rc_tp->snd_wnd);
5817 bbr->r_ctl.rc_pace_min_segs = (tls_seg + bbr->rc_last_options);
5820 old_tso = bbr->r_ctl.rc_pace_max_segs;
5821 if (bbr->rc_past_init_win == 0) {
5823 * Not enough data has been acknowledged to make a
5824 * judgement unless we are hardware TLS. Set up
5825 * the initial TSO based on if we are sending a
5826 * full IW at once or not.
5828 if (bbr->rc_use_google)
5829 bbr->r_ctl.rc_pace_max_segs = ((bbr->rc_tp->t_maxseg - bbr->rc_last_options) * 2);
5830 else if (bbr->bbr_init_win_cheat)
5831 bbr->r_ctl.rc_pace_max_segs = bbr_initial_cwnd(bbr, bbr->rc_tp);
5833 bbr->r_ctl.rc_pace_max_segs = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
5834 if (bbr->r_ctl.rc_pace_min_segs != bbr->rc_tp->t_maxseg)
5835 bbr->r_ctl.rc_pace_min_segs = bbr->rc_tp->t_maxseg;
5837 if ((bbr->rc_inp->inp_socket->so_snd.sb_flags & SB_TLS_IFNET) && tls_seg) {
5839 * For hardware TLS we set our min to the tls_seg size.
5841 bbr->r_ctl.rc_pace_max_segs = tls_seg;
5842 bbr->r_ctl.rc_pace_min_segs = tls_seg + bbr->rc_last_options;
5845 if (bbr->r_ctl.rc_pace_max_segs == 0) {
5846 bbr->r_ctl.rc_pace_max_segs = maxseg;
5848 bbr_log_type_tsosize(bbr, cts, bbr->r_ctl.rc_pace_max_segs, tls_seg, old_tso, maxseg, 0);
5850 if ((bbr->rc_inp->inp_socket->so_snd.sb_flags & SB_TLS_IFNET) == 0)
5852 bbr_adjust_for_hw_pacing(bbr, cts);
5856 * Now lets set the TSO goal based on our delivery rate in
5857 * bytes per second. Note we only do this if
5858 * we have acked at least the initial cwnd worth of data.
5860 bw = bbr_get_bw(bbr);
5861 if (IN_RECOVERY(bbr->rc_tp->t_flags) &&
5862 (bbr->rc_use_google == 0)) {
5863 /* We clamp to one MSS in recovery */
5865 } else if (bbr->rc_use_google) {
5868 /* Google considers the gain too */
5869 if (bbr->r_ctl.rc_bbr_hptsi_gain != BBR_UNIT) {
5870 bw *= bbr->r_ctl.rc_bbr_hptsi_gain;
5874 if (bytes > (64 * 1024))
5876 new_tso = bytes / maxseg;
5877 if (bw < ONE_POINT_TWO_MEG)
5881 if (new_tso < min_tso_segs)
5882 new_tso = min_tso_segs;
5884 } else if (bbr->rc_no_pacing) {
5885 new_tso = (PACE_MAX_IP_BYTES / maxseg) * maxseg;
5886 } else if (bw <= bbr->r_ctl.bbr_cross_over) {
5888 * Calculate the worse case b/w TSO if we are inserting no
5889 * more than a delay_target number of TSO's.
5891 uint32_t tso_len, min_tso;
5893 tso_len = bbr_get_pacing_length(bbr, BBR_UNIT, bbr->r_ctl.bbr_hptsi_segments_delay_tar, bw);
5894 if (tso_len > maxseg) {
5895 new_tso = tso_len / maxseg;
5896 if (new_tso > bbr->r_ctl.bbr_hptsi_segments_max)
5897 new_tso = bbr->r_ctl.bbr_hptsi_segments_max;
5901 * less than a full sized frame yikes.. long rtt or
5904 min_tso = bbr_minseg(bbr);
5905 if ((tso_len > min_tso) && (bbr_all_get_min == 0))
5906 new_tso = rounddown(tso_len, min_tso);
5910 } else if (bw > FIVETWELVE_MBPS) {
5912 * This guy is so fast b/w wise that we can TSO as large as
5913 * possible of segments that the NIC will allow.
5915 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg);
5918 * This formula is based on attempting to send a segment or
5919 * more every bbr_hptsi_per_second. The default is 1000
5920 * which means you are targeting what you can send every 1ms
5921 * based on the peers bw.
5923 * If the number drops to say 500, then you are looking more
5924 * at 2ms and you will raise how much we send in a single
5925 * TSO thus saving CPU (less bbr_output_wtime() calls). The
5926 * trade off of course is you will send more at once and
5927 * thus tend to clump up the sends into larger "bursts"
5930 bw /= bbr->r_ctl.bbr_hptsi_per_second;
5931 new_tso = roundup(bw, (uint64_t)maxseg);
5933 * Gate the floor to match what our lower than 48Mbps
5934 * algorithm does. The ceiling (bbr_hptsi_segments_max) thus
5935 * becomes the floor for this calculation.
5937 if (new_tso < (bbr->r_ctl.bbr_hptsi_segments_max * maxseg))
5938 new_tso = (bbr->r_ctl.bbr_hptsi_segments_max * maxseg);
5940 if (bbr->r_ctl.bbr_hptsi_segments_floor && (new_tso < (maxseg * bbr->r_ctl.bbr_hptsi_segments_floor)))
5941 new_tso = maxseg * bbr->r_ctl.bbr_hptsi_segments_floor;
5942 if (new_tso > PACE_MAX_IP_BYTES)
5943 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg);
5944 /* Enforce an utter maximum if we are not HW-TLS */
5946 if ((bbr->rc_inp->inp_socket->so_snd.sb_flags & SB_TLS_IFNET) == 0)
5948 if (bbr->r_ctl.bbr_utter_max && (new_tso > (bbr->r_ctl.bbr_utter_max * maxseg))) {
5949 new_tso = bbr->r_ctl.bbr_utter_max * maxseg;
5954 * Lets move the output size
5955 * up to 1 or more TLS record sizes.
5959 temp = roundup(new_tso, tls_seg);
5961 /* Back down if needed to under a full frame */
5962 while (new_tso > PACE_MAX_IP_BYTES)
5966 if (old_tso != new_tso) {
5967 /* Only log changes */
5968 bbr_log_type_tsosize(bbr, cts, new_tso, tls_seg, old_tso, maxseg, 0);
5969 bbr->r_ctl.rc_pace_max_segs = new_tso;
5972 if ((bbr->rc_inp->inp_socket->so_snd.sb_flags & SB_TLS_IFNET) &&
5974 bbr->r_ctl.rc_pace_min_segs = tls_seg + bbr->rc_last_options;
5977 /* We have hardware pacing and not hardware TLS! */
5978 bbr_adjust_for_hw_pacing(bbr, cts);
5982 bbr_log_output(struct tcp_bbr *bbr, struct tcpcb *tp, struct tcpopt *to, int32_t len,
5983 uint32_t seq_out, uint8_t th_flags, int32_t err, uint32_t cts,
5984 struct mbuf *mb, int32_t * abandon, struct bbr_sendmap *hintrsm, uint32_t delay_calc,
5988 struct bbr_sendmap *rsm, *nrsm;
5989 register uint32_t snd_max, snd_una;
5990 uint32_t pacing_time;
5992 * Add to the RACK log of packets in flight or retransmitted. If
5993 * there is a TS option we will use the TS echoed, if not we will
5996 * Retransmissions will increment the count and move the ts to its
5997 * proper place. Note that if options do not include TS's then we
5998 * won't be able to effectively use the ACK for an RTT on a retran.
6000 * Notes about r_start and r_end. Lets consider a send starting at
6001 * sequence 1 for 10 bytes. In such an example the r_start would be
6002 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11.
6003 * This means that r_end is actually the first sequence for the next
6007 INP_WLOCK_ASSERT(tp->t_inpcb);
6010 * We don't log errors -- we could but snd_max does not
6011 * advance in this case either.
6015 if (th_flags & TH_RST) {
6017 * We don't log resets and we return immediately from
6023 snd_una = tp->snd_una;
6024 if (th_flags & (TH_SYN | TH_FIN) && (hintrsm == NULL)) {
6026 * The call to bbr_log_output is made before bumping
6027 * snd_max. This means we can record one extra byte on a SYN
6028 * or FIN if seq_out is adding more on and a FIN is present
6029 * (and we are not resending).
6031 if ((th_flags & TH_SYN) && (tp->iss == seq_out))
6033 if (th_flags & TH_FIN)
6036 if (SEQ_LEQ((seq_out + len), snd_una)) {
6037 /* Are sending an old segment to induce an ack (keep-alive)? */
6040 if (SEQ_LT(seq_out, snd_una)) {
6041 /* huh? should we panic? */
6044 end = seq_out + len;
6046 len = end - seq_out;
6048 snd_max = tp->snd_max;
6050 /* We don't log zero window probes */
6053 pacing_time = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, len, cts, 1);
6054 /* First question is it a retransmission? */
6055 if (seq_out == snd_max) {
6057 rsm = bbr_alloc(bbr);
6062 if (th_flags & TH_SYN)
6063 rsm->r_flags |= BBR_HAS_SYN;
6064 if (th_flags & TH_FIN)
6065 rsm->r_flags |= BBR_HAS_FIN;
6066 rsm->r_tim_lastsent[0] = cts;
6068 rsm->r_rtr_bytes = 0;
6069 rsm->r_start = seq_out;
6070 rsm->r_end = rsm->r_start + len;
6072 rsm->r_delivered = bbr->r_ctl.rc_delivered;
6073 rsm->r_pacing_delay = pacing_time;
6074 rsm->r_ts_valid = bbr->rc_ts_valid;
6075 if (bbr->rc_ts_valid)
6076 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts;
6077 rsm->r_del_time = bbr->r_ctl.rc_del_time;
6078 if (bbr->r_ctl.r_app_limited_until)
6079 rsm->r_app_limited = 1;
6081 rsm->r_app_limited = 0;
6082 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts);
6083 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp,
6084 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
6086 * Here we must also add in this rsm since snd_max
6087 * is updated after we return from a new send.
6089 rsm->r_flight_at_send += len;
6090 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next);
6091 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
6093 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW)
6094 rsm->r_bbr_state = bbr_state_val(bbr);
6096 rsm->r_bbr_state = 8;
6097 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) {
6099 rsm->r_is_drain = 0;
6100 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) {
6101 rsm->r_is_drain = 1;
6104 rsm->r_is_drain = 0;
6110 * If we reach here its a retransmission and we need to find it.
6113 if (hintrsm && (hintrsm->r_start == seq_out)) {
6116 } else if (bbr->r_ctl.rc_next) {
6117 /* We have a hint from a previous run */
6118 rsm = bbr->r_ctl.rc_next;
6120 /* No hints sorry */
6123 if ((rsm) && (rsm->r_start == seq_out)) {
6125 * We used rc_next or hintrsm to retransmit, hopefully the
6128 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time);
6135 /* Ok it was not the last pointer go through it the hard way. */
6136 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
6137 if (rsm->r_start == seq_out) {
6138 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time);
6139 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next);
6146 if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) {
6147 /* Transmitted within this piece */
6149 * Ok we must split off the front and then let the
6150 * update do the rest
6152 nrsm = bbr_alloc_full_limit(bbr);
6154 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time);
6158 * copy rsm to nrsm and then trim the front of rsm
6159 * to not include this part.
6161 bbr_clone_rsm(bbr, nrsm, rsm, seq_out);
6162 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
6163 if (rsm->r_in_tmap) {
6164 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
6165 nrsm->r_in_tmap = 1;
6167 rsm->r_flags &= (~BBR_HAS_FIN);
6168 seq_out = bbr_update_entry(tp, bbr, nrsm, cts, &len, pacing_time);
6175 * Hmm not found in map did they retransmit both old and on into the
6178 if (seq_out == tp->snd_max) {
6180 } else if (SEQ_LT(seq_out, tp->snd_max)) {
6181 #ifdef BBR_INVARIANTS
6182 printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n",
6183 seq_out, len, tp->snd_una, tp->snd_max);
6184 printf("Starting Dump of all rack entries\n");
6185 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
6186 printf("rsm:%p start:%u end:%u\n",
6187 rsm, rsm->r_start, rsm->r_end);
6189 printf("Dump complete\n");
6190 panic("seq_out not found rack:%p tp:%p",
6194 #ifdef BBR_INVARIANTS
6196 * Hmm beyond sndmax? (only if we are using the new rtt-pack
6199 panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p",
6200 seq_out, len, tp->snd_max, tp);
6206 bbr_collapse_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, int32_t rtt)
6209 * Collapse timeout back the cum-ack moved.
6212 tp->t_softerror = 0;
6217 tcp_bbr_xmit_timer(struct tcp_bbr *bbr, uint32_t rtt_usecs, uint32_t rsm_send_time, uint32_t r_start, uint32_t tsin)
6220 bbr->r_ctl.cur_rtt = rtt_usecs;
6221 bbr->r_ctl.ts_in = tsin;
6223 bbr->r_ctl.cur_rtt_send_time = rsm_send_time;
6227 bbr_make_timestamp_determination(struct tcp_bbr *bbr)
6230 * We have in our bbr control:
6231 * 1) The timestamp we started observing cum-acks (bbr->r_ctl.bbr_ts_check_tstmp).
6232 * 2) Our timestamp indicating when we sent that packet (bbr->r_ctl.rsm->bbr_ts_check_our_cts).
6233 * 3) The current timestamp that just came in (bbr->r_ctl.last_inbound_ts)
6234 * 4) The time that the packet that generated that ack was sent (bbr->r_ctl.cur_rtt_send_time)
6236 * Now we can calculate the time between the sends by doing:
6238 * delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts
6240 * And the peer's time between receiving them by doing:
6242 * peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp
6244 * We want to figure out if the timestamp values are in msec, 10msec or usec.
6245 * We also may find that we can't use the timestamps if say we see
6246 * that the peer_delta indicates that though we may have taken 10ms to
6247 * pace out the data, it only saw 1ms between the two packets. This would
6248 * indicate that somewhere on the path is a batching entity that is giving
6249 * out time-slices of the actual b/w. This would mean we could not use
6250 * reliably the peers timestamps.
6252 * We expect delta > peer_delta initially. Until we figure out the
6253 * timestamp difference which we will store in bbr->r_ctl.bbr_peer_tsratio.
6254 * If we place 1000 there then its a ms vs our usec. If we place 10000 there
6255 * then its 10ms vs our usec. If the peer is running a usec clock we would
6256 * put a 1 there. If the value is faster then ours, we will disable the
6257 * use of timestamps (though we could revist this later if we find it to be not
6258 * just an isolated one or two flows)).
6260 * To detect the batching middle boxes we will come up with our compensation and
6261 * if with it in place, we find the peer is drastically off (by some margin) in
6262 * the smaller direction, then we will assume the worst case and disable use of timestamps.
6265 uint64_t delta, peer_delta, delta_up;
6267 delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts;
6268 if (delta < bbr_min_usec_delta) {
6270 * Have not seen a min amount of time
6271 * between our send times so we can
6272 * make a determination of the timestamp
6277 peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp;
6278 if (peer_delta < bbr_min_peer_delta) {
6280 * We may have enough in the form of
6281 * our delta but the peers number
6282 * has not changed that much. It could
6283 * be its clock ratio is such that
6284 * we need more data (10ms tick) or
6285 * there may be other compression scenarios
6286 * going on. In any event we need the
6287 * spread to be larger.
6291 /* Ok lets first see which way our delta is going */
6292 if (peer_delta > delta) {
6293 /* Very unlikely, the peer without
6294 * compensation shows that it saw
6295 * the two sends arrive further apart
6296 * then we saw then in micro-seconds.
6298 if (peer_delta < (delta + ((delta * (uint64_t)1000)/ (uint64_t)bbr_delta_percent))) {
6299 /* well it looks like the peer is a micro-second clock. */
6300 bbr->rc_ts_clock_set = 1;
6301 bbr->r_ctl.bbr_peer_tsratio = 1;
6303 bbr->rc_ts_cant_be_used = 1;
6304 bbr->rc_ts_clock_set = 1;
6308 /* Ok we know that the peer_delta is smaller than our send distance */
6309 bbr->rc_ts_clock_set = 1;
6310 /* First question is it within the percentage that they are using usec time? */
6311 delta_up = (peer_delta * 1000) / (uint64_t)bbr_delta_percent;
6312 if ((peer_delta + delta_up) >= delta) {
6313 /* Its a usec clock */
6314 bbr->r_ctl.bbr_peer_tsratio = 1;
6315 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6318 /* Ok if not usec, what about 10usec (though unlikely)? */
6319 delta_up = (peer_delta * 1000 * 10) / (uint64_t)bbr_delta_percent;
6320 if (((peer_delta * 10) + delta_up) >= delta) {
6321 bbr->r_ctl.bbr_peer_tsratio = 10;
6322 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6325 /* And what about 100usec (though again unlikely)? */
6326 delta_up = (peer_delta * 1000 * 100) / (uint64_t)bbr_delta_percent;
6327 if (((peer_delta * 100) + delta_up) >= delta) {
6328 bbr->r_ctl.bbr_peer_tsratio = 100;
6329 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6332 /* And how about 1 msec (the most likely one)? */
6333 delta_up = (peer_delta * 1000 * 1000) / (uint64_t)bbr_delta_percent;
6334 if (((peer_delta * 1000) + delta_up) >= delta) {
6335 bbr->r_ctl.bbr_peer_tsratio = 1000;
6336 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6339 /* Ok if not msec could it be 10 msec? */
6340 delta_up = (peer_delta * 1000 * 10000) / (uint64_t)bbr_delta_percent;
6341 if (((peer_delta * 10000) + delta_up) >= delta) {
6342 bbr->r_ctl.bbr_peer_tsratio = 10000;
6345 /* If we fall down here the clock tick so slowly we can't use it */
6346 bbr->rc_ts_cant_be_used = 1;
6347 bbr->r_ctl.bbr_peer_tsratio = 0;
6348 bbr_log_tstmp_validation(bbr, peer_delta, delta);
6352 * Collect new round-trip time estimate
6353 * and update averages and current timeout.
6356 tcp_bbr_xmit_timer_commit(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t cts)
6363 if (bbr->rtt_valid == 0)
6364 /* No valid sample */
6367 rtt = bbr->r_ctl.cur_rtt;
6368 tsin = bbr->r_ctl.ts_in;
6369 if (bbr->rc_prtt_set_ts) {
6371 * We are to force feed the rttProp filter due
6372 * to an entry into PROBE_RTT. This assures
6373 * that the times are sync'd between when we
6374 * go into PROBE_RTT and the filter expiration.
6376 * Google does not use a true filter, so they do
6377 * this implicitly since they only keep one value
6378 * and when they enter probe-rtt they update the
6379 * value to the newest rtt.
6383 bbr->rc_prtt_set_ts = 0;
6384 rtt_prop = get_filter_value_small(&bbr->r_ctl.rc_rttprop);
6386 filter_increase_by_small(&bbr->r_ctl.rc_rttprop, (rtt - rtt_prop), cts);
6388 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
6390 if (bbr->rc_ack_was_delayed)
6391 rtt += bbr->r_ctl.rc_ack_hdwr_delay;
6393 if (rtt < bbr->r_ctl.rc_lowest_rtt)
6394 bbr->r_ctl.rc_lowest_rtt = rtt;
6395 bbr_log_rtt_sample(bbr, rtt, tsin);
6396 if (bbr->r_init_rtt) {
6398 * The initial rtt is not-trusted, nuke it and lets get
6399 * our first valid measurement in.
6401 bbr->r_init_rtt = 0;
6404 if ((bbr->rc_ts_clock_set == 0) && bbr->rc_ts_valid) {
6406 * So we have not yet figured out
6407 * what the peers TSTMP value is
6408 * in (most likely ms). We need a
6409 * series of cum-ack's to determine
6412 if (bbr->rc_ack_is_cumack) {
6413 if (bbr->rc_ts_data_set) {
6414 /* Lets attempt to determine the timestamp granularity. */
6415 bbr_make_timestamp_determination(bbr);
6417 bbr->rc_ts_data_set = 1;
6418 bbr->r_ctl.bbr_ts_check_tstmp = bbr->r_ctl.last_inbound_ts;
6419 bbr->r_ctl.bbr_ts_check_our_cts = bbr->r_ctl.cur_rtt_send_time;
6423 * We have to have consecutive acks
6424 * reset any "filled" state to none.
6426 bbr->rc_ts_data_set = 0;
6430 rtt_ticks = USEC_2_TICKS((rtt + (USECS_IN_MSEC - 1)));
6433 if (tp->t_srtt != 0) {
6435 * srtt is stored as fixed point with 5 bits after the
6436 * binary point (i.e., scaled by 8). The following magic is
6437 * equivalent to the smoothing algorithm in rfc793 with an
6438 * alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed point).
6439 * Adjust rtt to origin 0.
6442 delta = ((rtt_ticks - 1) << TCP_DELTA_SHIFT)
6443 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
6445 tp->t_srtt += delta;
6446 if (tp->t_srtt <= 0)
6450 * We accumulate a smoothed rtt variance (actually, a
6451 * smoothed mean difference), then set the retransmit timer
6452 * to smoothed rtt + 4 times the smoothed variance. rttvar
6453 * is stored as fixed point with 4 bits after the binary
6454 * point (scaled by 16). The following is equivalent to
6455 * rfc793 smoothing with an alpha of .75 (rttvar =
6456 * rttvar*3/4 + |delta| / 4). This replaces rfc793's
6461 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
6462 tp->t_rttvar += delta;
6463 if (tp->t_rttvar <= 0)
6465 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
6466 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
6469 * No rtt measurement yet - use the unsmoothed rtt. Set the
6470 * variance to half the rtt (so our first retransmit happens
6473 tp->t_srtt = rtt_ticks << TCP_RTT_SHIFT;
6474 tp->t_rttvar = rtt_ticks << (TCP_RTTVAR_SHIFT - 1);
6475 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
6477 KMOD_TCPSTAT_INC(tcps_rttupdated);
6480 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt_ticks));
6483 * the retransmit should happen at rtt + 4 * rttvar. Because of the
6484 * way we do the smoothing, srtt and rttvar will each average +1/2
6485 * tick of bias. When we compute the retransmit timer, we want 1/2
6486 * tick of rounding and 1 extra tick because of +-1/2 tick
6487 * uncertainty in the firing of the timer. The bias will give us
6488 * exactly the 1.5 tick we need. But, because the bias is
6489 * statistical, we have to test that we don't drop below the minimum
6490 * feasible timer (which is 2 ticks).
6492 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
6493 max(MSEC_2_TICKS(bbr->r_ctl.rc_min_rto_ms), rtt_ticks + 2),
6494 MSEC_2_TICKS(((uint32_t)bbr->rc_max_rto_sec) * 1000));
6497 * We received an ack for a packet that wasn't retransmitted; it is
6498 * probably safe to discard any error indications we've received
6499 * recently. This isn't quite right, but close enough for now (a
6500 * route might have failed after we sent a segment, and the return
6501 * path might not be symmetrical).
6503 tp->t_softerror = 0;
6504 rtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT);
6505 if (bbr->r_ctl.bbr_smallest_srtt_this_state > rtt)
6506 bbr->r_ctl.bbr_smallest_srtt_this_state = rtt;
6510 bbr_earlier_retran(struct tcpcb *tp, struct tcp_bbr *bbr, struct bbr_sendmap *rsm,
6511 uint32_t t, uint32_t cts, int ack_type)
6514 * For this RSM, we acknowledged the data from a previous
6515 * transmission, not the last one we made. This means we did a false
6518 if (rsm->r_flags & BBR_HAS_FIN) {
6520 * The sending of the FIN often is multiple sent when we
6521 * have everything outstanding ack'd. We ignore this case
6522 * since its over now.
6526 if (rsm->r_flags & BBR_TLP) {
6528 * We expect TLP's to have this occur often
6530 bbr->rc_tlp_rtx_out = 0;
6533 if (ack_type != BBR_CUM_ACKED) {
6535 * If it was not a cum-ack we
6536 * don't really know for sure since
6537 * the timestamp could be from some
6538 * other transmission.
6543 if (rsm->r_flags & BBR_WAS_SACKPASS) {
6545 * We retransmitted based on a sack and the earlier
6546 * retransmission ack'd it - re-ordering is occuring.
6548 BBR_STAT_INC(bbr_reorder_seen);
6549 bbr->r_ctl.rc_reorder_ts = cts;
6551 /* Back down the loss count */
6552 if (rsm->r_flags & BBR_MARKED_LOST) {
6553 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
6554 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
6555 rsm->r_flags &= ~BBR_MARKED_LOST;
6556 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
6557 /* LT sampling also needs adjustment */
6558 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
6560 /***** RRS HERE ************************/
6561 /* Do we need to do this??? */
6562 /* bbr_reset_lt_bw_sampling(bbr, cts); */
6563 /***** RRS HERE ************************/
6564 BBR_STAT_INC(bbr_badfr);
6565 BBR_STAT_ADD(bbr_badfr_bytes, (rsm->r_end - rsm->r_start));
6570 bbr_set_reduced_rtt(struct tcp_bbr *bbr, uint32_t cts, uint32_t line)
6572 bbr->r_ctl.rc_rtt_shrinks = cts;
6573 if (bbr_can_force_probertt &&
6574 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) &&
6575 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) {
6577 * We should enter probe-rtt its been too long
6578 * since we have been there.
6580 bbr_enter_probe_rtt(bbr, cts, __LINE__);
6582 bbr_check_probe_rtt_limits(bbr, cts);
6586 tcp_bbr_commit_bw(struct tcp_bbr *bbr, uint32_t cts)
6590 if (bbr->r_ctl.rc_bbr_cur_del_rate == 0) {
6591 /* We never apply a zero measurment */
6592 bbr_log_type_bbrupd(bbr, 20, cts, 0, 0,
6596 if (bbr->r_ctl.r_measurement_count < 0xffffffff)
6597 bbr->r_ctl.r_measurement_count++;
6598 orig_bw = get_filter_value(&bbr->r_ctl.rc_delrate);
6599 apply_filter_max(&bbr->r_ctl.rc_delrate, bbr->r_ctl.rc_bbr_cur_del_rate, bbr->r_ctl.rc_pkt_epoch);
6600 bbr_log_type_bbrupd(bbr, 21, cts, (uint32_t)orig_bw,
6601 (uint32_t)get_filter_value(&bbr->r_ctl.rc_delrate),
6604 (orig_bw != get_filter_value(&bbr->r_ctl.rc_delrate))) {
6605 if (bbr->bbr_hdrw_pacing) {
6607 * Apply a new rate to the hardware
6610 bbr_update_hardware_pacing_rate(bbr, cts);
6612 bbr_set_state_target(bbr, __LINE__);
6613 tcp_bbr_tso_size_check(bbr, cts);
6614 if (bbr->r_recovery_bw) {
6615 bbr_setup_red_bw(bbr, cts);
6616 bbr_log_type_bw_reduce(bbr, BBR_RED_BW_USELRBW);
6618 } else if ((orig_bw == 0) && get_filter_value(&bbr->r_ctl.rc_delrate))
6619 tcp_bbr_tso_size_check(bbr, cts);
6623 bbr_nf_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts)
6625 if (bbr->rc_in_persist == 0) {
6626 /* We log only when not in persist */
6627 /* Translate to a Bytes Per Second */
6628 uint64_t tim, bw, ts_diff, ts_bw;
6629 uint32_t upper, lower, delivered;
6631 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time))
6632 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time);
6636 * Now that we have processed the tim (skipping the sample
6637 * or possibly updating the time, go ahead and
6638 * calculate the cdr.
6640 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered);
6641 bw = (uint64_t)delivered;
6642 bw *= (uint64_t)USECS_IN_SECOND;
6645 /* We must have a calculatable amount */
6648 upper = (bw >> 32) & 0x00000000ffffffff;
6649 lower = bw & 0x00000000ffffffff;
6651 * If we are using this b/w shove it in now so we
6652 * can see in the trace viewer if it gets over-ridden.
6654 if (rsm->r_ts_valid &&
6656 bbr->rc_ts_clock_set &&
6657 (bbr->rc_ts_cant_be_used == 0) &&
6658 bbr->rc_use_ts_limit) {
6659 ts_diff = max((bbr->r_ctl.last_inbound_ts - rsm->r_del_ack_ts), 1);
6660 ts_diff *= bbr->r_ctl.bbr_peer_tsratio;
6661 if ((delivered == 0) ||
6663 /* Can't use the ts */
6664 bbr_log_type_bbrupd(bbr, 61, cts,
6666 bbr->r_ctl.last_inbound_ts,
6667 rsm->r_del_ack_ts, 0,
6668 0, 0, 0, delivered);
6670 ts_bw = (uint64_t)delivered;
6671 ts_bw *= (uint64_t)USECS_IN_SECOND;
6673 bbr_log_type_bbrupd(bbr, 62, cts,
6675 (ts_bw & 0xffffffff), 0, 0,
6676 0, 0, ts_diff, delivered);
6677 if ((bbr->ts_can_raise) &&
6679 bbr_log_type_bbrupd(bbr, 8, cts,
6683 (bw & 0x00000000ffffffff),
6686 } else if (ts_bw && (ts_bw < bw)) {
6687 bbr_log_type_bbrupd(bbr, 7, cts,
6691 (bw & 0x00000000ffffffff),
6697 if (rsm->r_first_sent_time &&
6698 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) {
6701 * We use what was in flight at the time of our
6702 * send and the size of this send to figure
6703 * out what we have been sending at (amount).
6704 * For the time we take from the time of
6705 * the send of the first send outstanding
6706 * until this send plus this sends pacing
6707 * time. This gives us a good calculation
6708 * as to the rate we have been sending at.
6711 sbw = (uint64_t)(rsm->r_flight_at_send);
6712 sbw *= (uint64_t)USECS_IN_SECOND;
6713 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time;
6714 sti += rsm->r_pacing_delay;
6717 bbr_log_type_bbrupd(bbr, 6, cts,
6722 rsm->r_first_sent_time, 0, (sbw >> 32),
6727 /* Use the google algorithm for b/w measurements */
6728 bbr->r_ctl.rc_bbr_cur_del_rate = bw;
6729 if ((rsm->r_app_limited == 0) ||
6730 (bw > get_filter_value(&bbr->r_ctl.rc_delrate))) {
6731 tcp_bbr_commit_bw(bbr, cts);
6732 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered,
6733 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time);
6739 bbr_google_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts)
6741 if (bbr->rc_in_persist == 0) {
6742 /* We log only when not in persist */
6743 /* Translate to a Bytes Per Second */
6745 uint32_t upper, lower, delivered;
6748 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time))
6749 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time);
6753 * Now that we have processed the tim (skipping the sample
6754 * or possibly updating the time, go ahead and
6755 * calculate the cdr.
6757 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered);
6758 bw = (uint64_t)delivered;
6759 bw *= (uint64_t)USECS_IN_SECOND;
6761 if (tim < bbr->r_ctl.rc_lowest_rtt) {
6762 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered,
6763 tim, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0);
6767 upper = (bw >> 32) & 0x00000000ffffffff;
6768 lower = bw & 0x00000000ffffffff;
6770 * If we are using this b/w shove it in now so we
6771 * can see in the trace viewer if it gets over-ridden.
6773 bbr->r_ctl.rc_bbr_cur_del_rate = bw;
6774 /* Gate by the sending rate */
6775 if (rsm->r_first_sent_time &&
6776 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) {
6779 * We use what was in flight at the time of our
6780 * send and the size of this send to figure
6781 * out what we have been sending at (amount).
6782 * For the time we take from the time of
6783 * the send of the first send outstanding
6784 * until this send plus this sends pacing
6785 * time. This gives us a good calculation
6786 * as to the rate we have been sending at.
6789 sbw = (uint64_t)(rsm->r_flight_at_send);
6790 sbw *= (uint64_t)USECS_IN_SECOND;
6791 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time;
6792 sti += rsm->r_pacing_delay;
6795 bbr_log_type_bbrupd(bbr, 6, cts,
6800 rsm->r_first_sent_time, 0, (sbw >> 32),
6805 (sti < bbr->r_ctl.rc_lowest_rtt)) {
6806 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered,
6807 (uint32_t)sti, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0);
6812 bbr->r_ctl.rc_bbr_cur_del_rate = bw;
6813 if ((no_apply == 0) &&
6814 ((rsm->r_app_limited == 0) ||
6815 (bw > get_filter_value(&bbr->r_ctl.rc_delrate)))) {
6816 tcp_bbr_commit_bw(bbr, cts);
6817 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered,
6818 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time);
6825 bbr_update_bbr_info(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts, uint32_t tsin,
6826 uint32_t uts, int32_t match, uint32_t rsm_send_time, int32_t ack_type, struct tcpopt *to)
6828 uint64_t old_rttprop;
6830 /* Update our delivery time and amount */
6831 bbr->r_ctl.rc_delivered += (rsm->r_end - rsm->r_start);
6832 bbr->r_ctl.rc_del_time = cts;
6835 * 0 means its a retransmit, for now we don't use these for
6840 if ((bbr->rc_use_google == 0) &&
6841 (match != BBR_RTT_BY_EXACTMATCH) &&
6842 (match != BBR_RTT_BY_TIMESTAMP)){
6844 * We get a lot of rtt updates, lets not pay attention to
6845 * any that are not an exact match. That way we don't have
6846 * to worry about timestamps and the whole nonsense of
6847 * unsure if its a retransmission etc (if we ever had the
6848 * timestamp fixed to always have the last thing sent this
6849 * would not be a issue).
6853 if ((bbr_no_retran && bbr->rc_use_google) &&
6854 (match != BBR_RTT_BY_EXACTMATCH) &&
6855 (match != BBR_RTT_BY_TIMESTAMP)){
6857 * We only do measurements in google mode
6858 * with bbr_no_retran on for sure things.
6862 /* Only update srtt if we know by exact match */
6863 tcp_bbr_xmit_timer(bbr, rtt, rsm_send_time, rsm->r_start, tsin);
6864 if (ack_type == BBR_CUM_ACKED)
6865 bbr->rc_ack_is_cumack = 1;
6867 bbr->rc_ack_is_cumack = 0;
6868 old_rttprop = bbr_get_rtt(bbr, BBR_RTT_PROP);
6870 * Note the following code differs to the original
6871 * BBR spec. It calls for <= not <. However after a
6872 * long discussion in email with Neal, he acknowledged
6873 * that it should be < than so that we will have flows
6874 * going into probe-rtt (we were seeing cases where that
6875 * did not happen and caused ugly things to occur). We
6876 * have added this agreed upon fix to our code base.
6878 if (rtt < old_rttprop) {
6879 /* Update when we last saw a rtt drop */
6880 bbr_log_rtt_shrinks(bbr, cts, 0, rtt, __LINE__, BBR_RTTS_NEWRTT, 0);
6881 bbr_set_reduced_rtt(bbr, cts, __LINE__);
6883 bbr_log_type_bbrrttprop(bbr, rtt, (rsm ? rsm->r_end : 0), uts, cts,
6884 match, rsm->r_start, rsm->r_flags);
6885 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
6886 if (old_rttprop != bbr_get_rtt(bbr, BBR_RTT_PROP)) {
6888 * The RTT-prop moved, reset the target (may be a
6889 * nop for some states).
6891 bbr_set_state_target(bbr, __LINE__);
6892 if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT)
6893 bbr_log_rtt_shrinks(bbr, cts, 0, 0,
6894 __LINE__, BBR_RTTS_NEW_TARGET, 0);
6895 else if (old_rttprop < bbr_get_rtt(bbr, BBR_RTT_PROP))
6897 bbr_check_probe_rtt_limits(bbr, cts);
6899 if ((bbr->rc_use_google == 0) &&
6900 (match == BBR_RTT_BY_TIMESTAMP)) {
6902 * We don't do b/w update with
6903 * these since they are not really
6908 if (bbr->r_ctl.r_app_limited_until &&
6909 (bbr->r_ctl.rc_delivered >= bbr->r_ctl.r_app_limited_until)) {
6910 /* We are no longer app-limited */
6911 bbr->r_ctl.r_app_limited_until = 0;
6913 if (bbr->rc_use_google) {
6914 bbr_google_measurement(bbr, rsm, rtt, cts);
6916 bbr_nf_measurement(bbr, rsm, rtt, cts);
6921 * Convert a timestamp that the main stack
6922 * uses (milliseconds) into one that bbr uses
6923 * (microseconds). Return that converted timestamp.
6926 bbr_ts_convert(uint32_t cts) {
6929 sec = cts / MS_IN_USEC;
6930 msec = cts - (MS_IN_USEC * sec);
6931 return ((sec * USECS_IN_SECOND) + (msec * MS_IN_USEC));
6935 * Return 0 if we did not update the RTT time, return
6939 bbr_update_rtt(struct tcpcb *tp, struct tcp_bbr *bbr,
6940 struct bbr_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, uint32_t th_ack)
6943 uint32_t t, uts = 0;
6945 if ((rsm->r_flags & BBR_ACKED) ||
6946 (rsm->r_flags & BBR_WAS_RENEGED) ||
6947 (rsm->r_flags & BBR_RXT_CLEARED)) {
6951 if (rsm->r_rtr_cnt == 1) {
6953 * Only one transmit. Hopefully the normal case.
6955 if (TSTMP_GT(cts, rsm->r_tim_lastsent[0]))
6956 t = cts - rsm->r_tim_lastsent[0];
6961 bbr->r_ctl.rc_last_rtt = t;
6962 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0,
6963 BBR_RTT_BY_EXACTMATCH, rsm->r_tim_lastsent[0], ack_type, to);
6966 /* Convert to usecs */
6967 if ((bbr_can_use_ts_for_rtt == 1) &&
6968 (bbr->rc_use_google == 1) &&
6969 (ack_type == BBR_CUM_ACKED) &&
6970 (to->to_flags & TOF_TS) &&
6971 (to->to_tsecr != 0)) {
6973 t = tcp_tv_to_mssectick(&bbr->rc_tv) - to->to_tsecr;
6977 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0,
6978 BBR_RTT_BY_TIMESTAMP,
6979 rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)],
6983 uts = bbr_ts_convert(to->to_tsecr);
6984 if ((to->to_flags & TOF_TS) &&
6985 (to->to_tsecr != 0) &&
6986 (ack_type == BBR_CUM_ACKED) &&
6987 ((rsm->r_flags & BBR_OVERMAX) == 0)) {
6989 * Now which timestamp does it match? In this block the ACK
6990 * may be coming from a previous transmission.
6994 fudge = BBR_TIMER_FUDGE;
6995 for (i = 0; i < rsm->r_rtr_cnt; i++) {
6996 if ((SEQ_GEQ(uts, (rsm->r_tim_lastsent[i] - fudge))) &&
6997 (SEQ_LEQ(uts, (rsm->r_tim_lastsent[i] + fudge)))) {
6998 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i]))
6999 t = cts - rsm->r_tim_lastsent[i];
7004 bbr->r_ctl.rc_last_rtt = t;
7005 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_TSMATCHING,
7006 rsm->r_tim_lastsent[i], ack_type, to);
7007 if ((i + 1) < rsm->r_rtr_cnt) {
7009 bbr_earlier_retran(tp, bbr, rsm, t, cts, ack_type);
7010 } else if (rsm->r_flags & BBR_TLP) {
7011 bbr->rc_tlp_rtx_out = 0;
7016 /* Fall through if we can't find a matching timestamp */
7019 * Ok its a SACK block that we retransmitted. or a windows
7020 * machine without timestamps. We can tell nothing from the
7021 * time-stamp since its not there or the time the peer last
7022 * recieved a segment that moved forward its cum-ack point.
7024 * Lets look at the last retransmit and see what we can tell
7025 * (with BBR for space we only keep 2 note we have to keep
7026 * at least 2 so the map can not be condensed more).
7028 i = rsm->r_rtr_cnt - 1;
7029 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i]))
7030 t = cts - rsm->r_tim_lastsent[i];
7033 if (t < bbr->r_ctl.rc_lowest_rtt) {
7035 * We retransmitted and the ack came back in less
7036 * than the smallest rtt we have observed in the
7037 * windowed rtt. We most likey did an improper
7038 * retransmit as outlined in 4.2 Step 3 point 2 in
7041 * Use the prior transmission to update all the
7042 * information as long as there is only one prior
7045 if ((rsm->r_flags & BBR_OVERMAX) == 0) {
7046 #ifdef BBR_INVARIANTS
7047 if (rsm->r_rtr_cnt == 1)
7048 panic("rsm:%p bbr:%p rsm has overmax and only 1 retranmit flags:%x?", rsm, bbr, rsm->r_flags);
7050 i = rsm->r_rtr_cnt - 2;
7051 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i]))
7052 t = cts - rsm->r_tim_lastsent[i];
7055 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_EARLIER_RET,
7056 rsm->r_tim_lastsent[i], ack_type, to);
7057 bbr_earlier_retran(tp, bbr, rsm, t, cts, ack_type);
7060 * Too many prior transmissions, just
7061 * updated BBR delivered
7064 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts,
7065 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to);
7069 * We retransmitted it and the retransmit did the
7072 if (rsm->r_flags & BBR_TLP)
7073 bbr->rc_tlp_rtx_out = 0;
7074 if ((rsm->r_flags & BBR_OVERMAX) == 0)
7075 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts,
7076 BBR_RTT_BY_THIS_RETRAN, 0, ack_type, to);
7078 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts,
7079 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to);
7086 * Mark the SACK_PASSED flag on all entries prior to rsm send wise.
7089 bbr_log_sack_passed(struct tcpcb *tp,
7090 struct tcp_bbr *bbr, struct bbr_sendmap *rsm)
7092 struct bbr_sendmap *nrsm;
7095 TAILQ_FOREACH_REVERSE_FROM(nrsm, &bbr->r_ctl.rc_tmap,
7096 bbr_head, r_tnext) {
7098 /* Skip orginal segment he is acked */
7101 if (nrsm->r_flags & BBR_ACKED) {
7102 /* Skip ack'd segments */
7105 if (nrsm->r_flags & BBR_SACK_PASSED) {
7107 * We found one that is already marked
7108 * passed, we have been here before and
7109 * so all others below this are marked.
7113 BBR_STAT_INC(bbr_sack_passed);
7114 nrsm->r_flags |= BBR_SACK_PASSED;
7115 if (((nrsm->r_flags & BBR_MARKED_LOST) == 0) &&
7116 bbr_is_lost(bbr, nrsm, bbr->r_ctl.rc_rcvtime)) {
7117 bbr->r_ctl.rc_lost += nrsm->r_end - nrsm->r_start;
7118 bbr->r_ctl.rc_lost_bytes += nrsm->r_end - nrsm->r_start;
7119 nrsm->r_flags |= BBR_MARKED_LOST;
7121 nrsm->r_flags &= ~BBR_WAS_SACKPASS;
7126 * Returns the number of bytes that were
7127 * newly ack'd by sack blocks.
7130 bbr_proc_sack_blk(struct tcpcb *tp, struct tcp_bbr *bbr, struct sackblk *sack,
7131 struct tcpopt *to, struct bbr_sendmap **prsm, uint32_t cts)
7134 uint32_t start, end, maxseg, changed = 0;
7135 struct bbr_sendmap *rsm, *nrsm;
7136 int32_t used_ref = 1;
7137 uint8_t went_back = 0, went_fwd = 0;
7139 maxseg = tp->t_maxseg - bbr->rc_last_options;
7140 start = sack->start;
7146 /* Do we locate the block behind where we last were? */
7147 if (rsm && SEQ_LT(start, rsm->r_start)) {
7149 TAILQ_FOREACH_REVERSE_FROM(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) {
7150 if (SEQ_GEQ(start, rsm->r_start) &&
7151 SEQ_LT(start, rsm->r_end)) {
7159 * Ok lets locate the block where this guy is fwd from rsm (if its
7162 TAILQ_FOREACH_FROM(rsm, &bbr->r_ctl.rc_map, r_next) {
7163 if (SEQ_GEQ(start, rsm->r_start) &&
7164 SEQ_LT(start, rsm->r_end)) {
7171 * This happens when we get duplicate sack blocks with the
7172 * same end. For example SACK 4: 100 SACK 3: 100 The sort
7173 * will not change there location so we would just start at
7174 * the end of the first one and get lost.
7176 if (tp->t_flags & TF_SENTFIN) {
7178 * Check to see if we have not logged the FIN that
7181 nrsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next);
7182 if (nrsm && (nrsm->r_end + 1) == tp->snd_max) {
7184 * Ok we did not get the FIN logged.
7192 #ifdef BBR_INVARIANTS
7193 panic("tp:%p bbr:%p sack:%p to:%p prsm:%p",
7194 tp, bbr, sack, to, prsm);
7200 BBR_STAT_INC(bbr_sack_proc_restart);
7202 goto start_at_beginning;
7204 /* Ok we have an ACK for some piece of rsm */
7205 if (rsm->r_start != start) {
7207 * Need to split this in two pieces the before and after.
7209 if (bbr_sack_mergable(rsm, start, end))
7210 nrsm = bbr_alloc_full_limit(bbr);
7212 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT);
7214 /* We could not allocate ignore the sack */
7219 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk);
7222 bbr_clone_rsm(bbr, nrsm, rsm, start);
7223 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
7224 if (rsm->r_in_tmap) {
7225 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7226 nrsm->r_in_tmap = 1;
7228 rsm->r_flags &= (~BBR_HAS_FIN);
7231 if (SEQ_GEQ(end, rsm->r_end)) {
7233 * The end of this block is either beyond this guy or right
7236 if ((rsm->r_flags & BBR_ACKED) == 0) {
7237 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0);
7238 changed += (rsm->r_end - rsm->r_start);
7239 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
7240 bbr_log_sack_passed(tp, bbr, rsm);
7241 if (rsm->r_flags & BBR_MARKED_LOST) {
7242 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7244 /* Is Reordering occuring? */
7245 if (rsm->r_flags & BBR_SACK_PASSED) {
7246 BBR_STAT_INC(bbr_reorder_seen);
7247 bbr->r_ctl.rc_reorder_ts = cts;
7248 if (rsm->r_flags & BBR_MARKED_LOST) {
7249 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7250 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7251 /* LT sampling also needs adjustment */
7252 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7255 rsm->r_flags |= BBR_ACKED;
7256 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST);
7257 if (rsm->r_in_tmap) {
7258 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7262 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED);
7263 if (end == rsm->r_end) {
7264 /* This block only - done */
7267 /* There is more not coverend by this rsm move on */
7269 nrsm = TAILQ_NEXT(rsm, r_next);
7274 if (rsm->r_flags & BBR_ACKED) {
7275 /* Been here done that */
7278 /* Ok we need to split off this one at the tail */
7279 if (bbr_sack_mergable(rsm, start, end))
7280 nrsm = bbr_alloc_full_limit(bbr);
7282 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT);
7284 /* failed XXXrrs what can we do but loose the sack info? */
7289 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk);
7293 bbr_clone_rsm(bbr, nrsm, rsm, end);
7294 /* The sack block does not cover this guy fully */
7295 rsm->r_flags &= (~BBR_HAS_FIN);
7296 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
7297 if (rsm->r_in_tmap) {
7298 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7299 nrsm->r_in_tmap = 1;
7302 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0);
7303 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED);
7304 changed += (rsm->r_end - rsm->r_start);
7305 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
7306 bbr_log_sack_passed(tp, bbr, rsm);
7307 /* Is Reordering occuring? */
7308 if (rsm->r_flags & BBR_MARKED_LOST) {
7309 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7311 if (rsm->r_flags & BBR_SACK_PASSED) {
7312 BBR_STAT_INC(bbr_reorder_seen);
7313 bbr->r_ctl.rc_reorder_ts = cts;
7314 if (rsm->r_flags & BBR_MARKED_LOST) {
7315 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7316 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7317 /* LT sampling also needs adjustment */
7318 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7321 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST);
7322 rsm->r_flags |= BBR_ACKED;
7323 if (rsm->r_in_tmap) {
7324 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7328 if (rsm && (rsm->r_flags & BBR_ACKED)) {
7330 * Now can we merge this newly acked
7331 * block with either the previous or
7334 nrsm = TAILQ_NEXT(rsm, r_next);
7336 (nrsm->r_flags & BBR_ACKED)) {
7337 /* yep this and next can be merged */
7338 rsm = bbr_merge_rsm(bbr, rsm, nrsm);
7340 /* Now what about the previous? */
7341 nrsm = TAILQ_PREV(rsm, bbr_head, r_next);
7343 (nrsm->r_flags & BBR_ACKED)) {
7344 /* yep the previous and this can be merged */
7345 rsm = bbr_merge_rsm(bbr, nrsm, rsm);
7348 if (used_ref == 0) {
7349 BBR_STAT_INC(bbr_sack_proc_all);
7351 BBR_STAT_INC(bbr_sack_proc_short);
7353 if (went_fwd && went_back) {
7354 BBR_STAT_INC(bbr_sack_search_both);
7355 } else if (went_fwd) {
7356 BBR_STAT_INC(bbr_sack_search_fwd);
7357 } else if (went_back) {
7358 BBR_STAT_INC(bbr_sack_search_back);
7360 /* Save off where the next seq is */
7362 bbr->r_ctl.rc_sacklast = TAILQ_NEXT(rsm, r_next);
7364 bbr->r_ctl.rc_sacklast = NULL;
7371 bbr_peer_reneges(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, tcp_seq th_ack)
7373 struct bbr_sendmap *tmap;
7375 BBR_STAT_INC(bbr_reneges_seen);
7377 while (rsm && (rsm->r_flags & BBR_ACKED)) {
7378 /* Its no longer sacked, mark it so */
7380 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
7381 #ifdef BBR_INVARIANTS
7382 if (rsm->r_in_tmap) {
7383 panic("bbr:%p rsm:%p flags:0x%x in tmap?",
7384 bbr, rsm, rsm->r_flags);
7387 oflags = rsm->r_flags;
7388 if (rsm->r_flags & BBR_MARKED_LOST) {
7389 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7390 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7391 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7392 /* LT sampling also needs adjustment */
7393 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7395 rsm->r_flags &= ~(BBR_ACKED | BBR_SACK_PASSED | BBR_WAS_SACKPASS | BBR_MARKED_LOST);
7396 rsm->r_flags |= BBR_WAS_RENEGED;
7397 rsm->r_flags |= BBR_RXT_CLEARED;
7398 bbr_log_type_rsmclear(bbr, bbr->r_ctl.rc_rcvtime, rsm, oflags, __LINE__);
7399 /* Rebuild it into our tmap */
7401 TAILQ_INSERT_HEAD(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7404 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, tmap, rsm, r_tnext);
7407 tmap->r_in_tmap = 1;
7409 * XXXrrs Delivered? Should we do anything here?
7411 * Of course we don't on a rxt timeout so maybe its ok that
7416 rsm = TAILQ_NEXT(rsm, r_next);
7419 * Now lets possibly clear the sack filter so we start recognizing
7420 * sacks that cover this area.
7422 sack_filter_clear(&bbr->r_ctl.bbr_sf, th_ack);
7426 bbr_log_syn(struct tcpcb *tp, struct tcpopt *to)
7428 struct tcp_bbr *bbr;
7429 struct bbr_sendmap *rsm;
7432 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
7433 cts = bbr->r_ctl.rc_rcvtime;
7434 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7435 if (rsm && (rsm->r_flags & BBR_HAS_SYN)) {
7436 if ((rsm->r_end - rsm->r_start) <= 1) {
7437 /* Log out the SYN completely */
7438 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
7439 rsm->r_rtr_bytes = 0;
7440 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next);
7441 if (rsm->r_in_tmap) {
7442 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7445 if (bbr->r_ctl.rc_next == rsm) {
7446 /* scoot along the marker */
7447 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7450 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, 0);
7453 /* There is more (Fast open)? strip out SYN. */
7454 rsm->r_flags &= ~BBR_HAS_SYN;
7461 * Returns the number of bytes that were
7462 * acknowledged by SACK blocks.
7466 bbr_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th,
7467 uint32_t *prev_acked)
7469 uint32_t changed, last_seq, entered_recovery = 0;
7470 struct tcp_bbr *bbr;
7471 struct bbr_sendmap *rsm;
7472 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1];
7473 register uint32_t th_ack;
7474 int32_t i, j, k, new_sb, num_sack_blks = 0;
7475 uint32_t cts, acked, ack_point, sack_changed = 0;
7476 uint32_t p_maxseg, maxseg, p_acked = 0;
7478 INP_WLOCK_ASSERT(tp->t_inpcb);
7479 if (th->th_flags & TH_RST) {
7480 /* We don't log resets */
7483 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
7484 cts = bbr->r_ctl.rc_rcvtime;
7486 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7488 maxseg = tp->t_maxseg - bbr->rc_last_options;
7489 p_maxseg = min(bbr->r_ctl.rc_pace_max_segs, maxseg);
7490 th_ack = th->th_ack;
7491 if (SEQ_GT(th_ack, tp->snd_una)) {
7492 acked = th_ack - tp->snd_una;
7493 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_UPDATE, __LINE__);
7494 bbr->rc_tp->t_acktime = ticks;
7497 if (SEQ_LEQ(th_ack, tp->snd_una)) {
7498 /* Only sent here for sack processing */
7501 if (rsm && SEQ_GT(th_ack, rsm->r_start)) {
7502 changed = th_ack - rsm->r_start;
7503 } else if ((rsm == NULL) && ((th_ack - 1) == tp->iss)) {
7505 * For the SYN incoming case we will not have called
7506 * tcp_output for the sending of the SYN, so there will be
7507 * no map. All other cases should probably be a panic.
7509 if ((to->to_flags & TOF_TS) && (to->to_tsecr != 0)) {
7511 * We have a timestamp that can be used to generate
7514 uint32_t ts, now, rtt;
7516 ts = bbr_ts_convert(to->to_tsecr);
7517 now = bbr_ts_convert(tcp_tv_to_mssectick(&bbr->rc_tv));
7521 bbr_log_type_bbrrttprop(bbr, rtt,
7523 BBR_RTT_BY_TIMESTAMP, tp->iss, 0);
7524 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
7526 bbr->r_wanted_output = 1;
7530 } else if (rsm == NULL) {
7535 * The ACK point is advancing to th_ack, we must drop off
7536 * the packets in the rack log and calculate any eligble
7539 bbr->r_wanted_output = 1;
7543 if (tp->t_flags & TF_SENTFIN) {
7544 /* if we send a FIN we will not hav a map */
7547 #ifdef BBR_INVARIANTS
7548 panic("No rack map tp:%p for th:%p state:%d bbr:%p snd_una:%u snd_max:%u chg:%d\n",
7550 th, tp->t_state, bbr,
7551 tp->snd_una, tp->snd_max, changed);
7556 if (SEQ_LT(th_ack, rsm->r_start)) {
7557 /* Huh map is missing this */
7558 #ifdef BBR_INVARIANTS
7559 printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d bbr:%p\n",
7561 th_ack, tp->t_state,
7563 panic("th-ack is bad bbr:%p tp:%p", bbr, tp);
7566 } else if (th_ack == rsm->r_start) {
7567 /* None here to ack */
7571 * Clear the dup ack counter, it will
7572 * either be freed or if there is some
7573 * remaining we need to start it at zero.
7576 /* Now do we consume the whole thing? */
7577 if (SEQ_GEQ(th_ack, rsm->r_end)) {
7578 /* Its all consumed. */
7581 if (rsm->r_flags & BBR_ACKED) {
7583 * It was acked on the scoreboard -- remove it from
7586 p_acked += (rsm->r_end - rsm->r_start);
7587 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
7588 if (bbr->r_ctl.rc_sacked == 0)
7589 bbr->r_ctl.rc_sacklast = NULL;
7591 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, th_ack);
7592 if (rsm->r_flags & BBR_MARKED_LOST) {
7593 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start;
7595 if (rsm->r_flags & BBR_SACK_PASSED) {
7597 * There are acked segments ACKED on the
7598 * scoreboard further up. We are seeing
7601 BBR_STAT_INC(bbr_reorder_seen);
7602 bbr->r_ctl.rc_reorder_ts = cts;
7603 if (rsm->r_flags & BBR_MARKED_LOST) {
7604 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start;
7605 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost))
7606 /* LT sampling also needs adjustment */
7607 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost;
7610 rsm->r_flags &= ~BBR_MARKED_LOST;
7612 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
7613 rsm->r_rtr_bytes = 0;
7614 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next);
7615 if (rsm->r_in_tmap) {
7616 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
7619 if (bbr->r_ctl.rc_next == rsm) {
7620 /* scoot along the marker */
7621 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7623 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED);
7624 /* Adjust the packet counts */
7625 left = th_ack - rsm->r_end;
7626 /* Free back to zone */
7629 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7634 if (rsm->r_flags & BBR_ACKED) {
7636 * It was acked on the scoreboard -- remove it from total
7637 * for the part being cum-acked.
7639 p_acked += (rsm->r_end - rsm->r_start);
7640 bbr->r_ctl.rc_sacked -= (th_ack - rsm->r_start);
7641 if (bbr->r_ctl.rc_sacked == 0)
7642 bbr->r_ctl.rc_sacklast = NULL;
7645 * It was acked up to th_ack point for the first time
7647 struct bbr_sendmap lrsm;
7649 memcpy(&lrsm, rsm, sizeof(struct bbr_sendmap));
7650 lrsm.r_end = th_ack;
7651 bbr_update_rtt(tp, bbr, &lrsm, to, cts, BBR_CUM_ACKED, th_ack);
7653 if ((rsm->r_flags & BBR_MARKED_LOST) &&
7654 ((rsm->r_flags & BBR_ACKED) == 0)) {
7656 * It was marked lost and partly ack'd now
7657 * for the first time. We lower the rc_lost_bytes
7658 * and still leave it MARKED.
7660 bbr->r_ctl.rc_lost_bytes -= th_ack - rsm->r_start;
7662 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED);
7663 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
7664 rsm->r_rtr_bytes = 0;
7665 /* adjust packet count */
7666 rsm->r_start = th_ack;
7668 /* Check for reneging */
7669 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
7670 if (rsm && (rsm->r_flags & BBR_ACKED) && (th_ack == rsm->r_start)) {
7672 * The peer has moved snd_una up to the edge of this send,
7673 * i.e. one that it had previously acked. The only way that
7674 * can be true if the peer threw away data (space issues)
7675 * that it had previously sacked (else it would have given
7676 * us snd_una up to (rsm->r_end). We need to undo the acked
7679 * Note we have to look to make sure th_ack is our
7680 * rsm->r_start in case we get an old ack where th_ack is
7683 bbr_peer_reneges(bbr, rsm, th->th_ack);
7685 if ((to->to_flags & TOF_SACK) == 0) {
7686 /* We are done nothing left to log */
7689 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next);
7691 last_seq = rsm->r_end;
7693 last_seq = tp->snd_max;
7695 /* Sack block processing */
7696 if (SEQ_GT(th_ack, tp->snd_una))
7699 ack_point = tp->snd_una;
7700 for (i = 0; i < to->to_nsacks; i++) {
7701 bcopy((to->to_sacks + i * TCPOLEN_SACK),
7702 &sack, sizeof(sack));
7703 sack.start = ntohl(sack.start);
7704 sack.end = ntohl(sack.end);
7705 if (SEQ_GT(sack.end, sack.start) &&
7706 SEQ_GT(sack.start, ack_point) &&
7707 SEQ_LT(sack.start, tp->snd_max) &&
7708 SEQ_GT(sack.end, ack_point) &&
7709 SEQ_LEQ(sack.end, tp->snd_max)) {
7710 if ((bbr->r_ctl.rc_num_small_maps_alloced > bbr_sack_block_limit) &&
7711 (SEQ_LT(sack.end, last_seq)) &&
7712 ((sack.end - sack.start) < (p_maxseg / 8))) {
7714 * Not the last piece and its smaller than
7715 * 1/8th of a p_maxseg. We ignore this.
7717 BBR_STAT_INC(bbr_runt_sacks);
7720 sack_blocks[num_sack_blks] = sack;
7722 #ifdef NETFLIX_STATS
7723 } else if (SEQ_LEQ(sack.start, th_ack) &&
7724 SEQ_LEQ(sack.end, th_ack)) {
7726 * Its a D-SACK block.
7728 tcp_record_dsack(sack.start, sack.end);
7732 if (num_sack_blks == 0)
7735 * Sort the SACK blocks so we can update the rack scoreboard with
7738 new_sb = sack_filter_blks(&bbr->r_ctl.bbr_sf, sack_blocks,
7739 num_sack_blks, th->th_ack);
7740 ctf_log_sack_filter(bbr->rc_tp, new_sb, sack_blocks);
7741 BBR_STAT_ADD(bbr_sack_blocks, num_sack_blks);
7742 BBR_STAT_ADD(bbr_sack_blocks_skip, (num_sack_blks - new_sb));
7743 num_sack_blks = new_sb;
7744 if (num_sack_blks < 2) {
7747 /* Sort the sacks */
7748 for (i = 0; i < num_sack_blks; i++) {
7749 for (j = i + 1; j < num_sack_blks; j++) {
7750 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
7751 sack = sack_blocks[i];
7752 sack_blocks[i] = sack_blocks[j];
7753 sack_blocks[j] = sack;
7758 * Now are any of the sack block ends the same (yes some
7759 * implememtations send these)?
7762 if (num_sack_blks > 1) {
7763 for (i = 0; i < num_sack_blks; i++) {
7764 for (j = i + 1; j < num_sack_blks; j++) {
7765 if (sack_blocks[i].end == sack_blocks[j].end) {
7767 * Ok these two have the same end we
7768 * want the smallest end and then
7769 * throw away the larger and start
7772 if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) {
7774 * The second block covers
7775 * more area use that
7777 sack_blocks[i].start = sack_blocks[j].start;
7780 * Now collapse out the dup-sack and
7783 for (k = (j + 1); k < num_sack_blks; k++) {
7784 sack_blocks[j].start = sack_blocks[k].start;
7785 sack_blocks[j].end = sack_blocks[k].end;
7795 rsm = bbr->r_ctl.rc_sacklast;
7796 for (i = 0; i < num_sack_blks; i++) {
7797 acked = bbr_proc_sack_blk(tp, bbr, &sack_blocks[i], to, &rsm, cts);
7799 bbr->r_wanted_output = 1;
7801 sack_changed += acked;
7805 *prev_acked = p_acked;
7806 if ((sack_changed) && (!IN_RECOVERY(tp->t_flags))) {
7808 * Ok we have a high probability that we need to go in to
7809 * recovery since we have data sack'd
7811 struct bbr_sendmap *rsm;
7813 rsm = bbr_check_recovery_mode(tp, bbr, cts);
7815 /* Enter recovery */
7816 entered_recovery = 1;
7817 bbr->r_wanted_output = 1;
7819 * When we enter recovery we need to assure we send
7822 if (bbr->r_ctl.rc_resend == NULL) {
7823 bbr->r_ctl.rc_resend = rsm;
7827 if (IN_RECOVERY(tp->t_flags) && (entered_recovery == 0)) {
7829 * See if we need to rack-retransmit anything if so set it
7830 * up as the thing to resend assuming something else is not
7831 * already in that position.
7833 if (bbr->r_ctl.rc_resend == NULL) {
7834 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts);
7838 * We return the amount that changed via sack, this is used by the
7839 * ack-received code to augment what was changed between th_ack <->
7842 return (sack_changed);
7846 bbr_strike_dupack(struct tcp_bbr *bbr)
7848 struct bbr_sendmap *rsm;
7850 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap);
7851 if (rsm && (rsm->r_dupack < 0xff)) {
7853 if (rsm->r_dupack >= DUP_ACK_THRESHOLD)
7854 bbr->r_wanted_output = 1;
7859 * Return value of 1, we do not need to call bbr_process_data().
7860 * return value of 0, bbr_process_data can be called.
7861 * For ret_val if its 0 the TCB is locked and valid, if its non-zero
7862 * its unlocked and probably unsafe to touch the TCB.
7865 bbr_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so,
7866 struct tcpcb *tp, struct tcpopt *to,
7867 uint32_t tiwin, int32_t tlen,
7868 int32_t * ofia, int32_t thflags, int32_t * ret_val)
7870 int32_t ourfinisacked = 0;
7871 int32_t acked_amount;
7874 uint32_t lost, sack_changed = 0;
7876 struct tcp_bbr *bbr;
7877 uint32_t prev_acked = 0;
7879 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
7880 lost = bbr->r_ctl.rc_lost;
7881 nsegs = max(1, m->m_pkthdr.lro_nsegs);
7882 if (SEQ_GT(th->th_ack, tp->snd_max)) {
7883 ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val);
7884 bbr->r_wanted_output = 1;
7887 if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) {
7888 /* Process the ack */
7889 if (bbr->rc_in_persist)
7891 if ((th->th_ack == tp->snd_una) && (tiwin == tp->snd_wnd))
7892 bbr_strike_dupack(bbr);
7893 sack_changed = bbr_log_ack(tp, to, th, &prev_acked);
7895 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, (bbr->r_ctl.rc_lost > lost));
7896 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
7898 * Old ack, behind the last one rcv'd or a duplicate ack
7901 if (th->th_ack == tp->snd_una) {
7902 bbr_ack_received(tp, bbr, th, 0, sack_changed, prev_acked, __LINE__, 0);
7903 if (bbr->r_state == TCPS_SYN_SENT) {
7905 * Special case on where we sent SYN. When
7906 * the SYN-ACK is processed in syn_sent
7907 * state it bumps the snd_una. This causes
7908 * us to hit here even though we did ack 1
7911 * Go through the nothing left case so we
7920 * If we reach this point, ACK is not a duplicate, i.e., it ACKs
7921 * something we sent.
7923 if (tp->t_flags & TF_NEEDSYN) {
7925 * T/TCP: Connection was half-synchronized, and our SYN has
7926 * been ACK'd (so connection is now fully synchronized). Go
7927 * to non-starred state, increment snd_una for ACK of SYN,
7928 * and check if we can do window scaling.
7930 tp->t_flags &= ~TF_NEEDSYN;
7932 /* Do window scaling? */
7933 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
7934 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
7935 tp->rcv_scale = tp->request_r_scale;
7936 /* Send window already scaled. */
7939 INP_WLOCK_ASSERT(tp->t_inpcb);
7941 acked = BYTES_THIS_ACK(tp, th);
7942 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs);
7943 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
7946 * If we just performed our first retransmit, and the ACK arrives
7947 * within our recovery window, then it was a mistake to do the
7948 * retransmit in the first place. Recover our original cwnd and
7949 * ssthresh, and proceed to transmit where we left off.
7951 if (tp->t_flags & TF_PREVVALID) {
7952 tp->t_flags &= ~TF_PREVVALID;
7953 if (tp->t_rxtshift == 1 &&
7954 (int)(ticks - tp->t_badrxtwin) < 0)
7955 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL);
7957 SOCKBUF_LOCK(&so->so_snd);
7958 acked_amount = min(acked, (int)sbavail(&so->so_snd));
7959 tp->snd_wnd -= acked_amount;
7960 mfree = sbcut_locked(&so->so_snd, acked_amount);
7961 /* NB: sowwakeup_locked() does an implicit unlock. */
7962 sowwakeup_locked(so);
7964 if (SEQ_GT(th->th_ack, tp->snd_una)) {
7965 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp));
7967 tp->snd_una = th->th_ack;
7968 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, (bbr->r_ctl.rc_lost - lost));
7969 if (IN_RECOVERY(tp->t_flags)) {
7970 if (SEQ_LT(th->th_ack, tp->snd_recover) &&
7971 (SEQ_LT(th->th_ack, tp->snd_max))) {
7972 tcp_bbr_partialack(tp);
7974 bbr_post_recovery(tp);
7977 if (SEQ_GT(tp->snd_una, tp->snd_recover)) {
7978 tp->snd_recover = tp->snd_una;
7980 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
7981 tp->snd_nxt = tp->snd_max;
7983 if (tp->snd_una == tp->snd_max) {
7984 /* Nothing left outstanding */
7986 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__);
7987 if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0)
7988 bbr->rc_tp->t_acktime = 0;
7989 if ((sbused(&so->so_snd) == 0) &&
7990 (tp->t_flags & TF_SENTFIN)) {
7993 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
7994 if (bbr->rc_in_persist == 0) {
7995 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime;
7997 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
7998 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime);
8000 * We invalidate the last ack here since we
8001 * don't want to transfer forward the time
8002 * for our sum's calculations.
8004 if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
8005 (sbavail(&so->so_snd) == 0) &&
8006 (tp->t_flags2 & TF2_DROP_AF_DATA)) {
8008 * The socket was gone and the peer sent data, time
8012 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
8013 /* tcp_close will kill the inp pre-log the Reset */
8014 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
8016 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen);
8017 BBR_STAT_INC(bbr_dropped_af_data);
8020 /* Set need output so persist might get set */
8021 bbr->r_wanted_output = 1;
8024 *ofia = ourfinisacked;
8029 bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line)
8031 if (bbr->rc_in_persist == 0) {
8032 bbr_timer_cancel(bbr, __LINE__, cts);
8033 bbr->r_ctl.rc_last_delay_val = 0;
8035 bbr->rc_in_persist = 1;
8036 bbr->r_ctl.rc_went_idle_time = cts;
8037 /* We should be capped when rw went to 0 but just in case */
8038 bbr_log_type_pesist(bbr, cts, 0, line, 1);
8039 /* Time freezes for the state, so do the accounting now */
8040 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
8043 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
8044 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) {
8047 idx = bbr_state_val(bbr);
8048 counter_u64_add(bbr_state_time[(idx + 5)], time_in);
8050 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
8053 bbr->r_ctl.rc_bbr_state_time = cts;
8058 bbr_restart_after_idle(struct tcp_bbr *bbr, uint32_t cts, uint32_t idle_time)
8061 * Note that if idle time does not exceed our
8062 * threshold, we do nothing continuing the state
8063 * transitions we were last walking through.
8065 if (idle_time >= bbr_idle_restart_threshold) {
8066 if (bbr->rc_use_idle_restart) {
8067 bbr->rc_bbr_state = BBR_STATE_IDLE_EXIT;
8069 * Set our target using BBR_UNIT, so
8070 * we increase at a dramatic rate but
8071 * we stop when we get the pipe
8072 * full again for our current b/w estimate.
8074 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
8075 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
8076 bbr_set_state_target(bbr, __LINE__);
8077 /* Now setup our gains to ramp up */
8078 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg;
8079 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg;
8080 bbr_log_type_statechange(bbr, cts, __LINE__);
8081 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) {
8082 bbr_substate_change(bbr, cts, __LINE__, 1);
8088 bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line)
8092 if (bbr->rc_in_persist == 0)
8094 idle_time = bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time);
8095 bbr->rc_in_persist = 0;
8096 bbr->rc_hit_state_1 = 0;
8097 bbr->r_ctl.rc_del_time = cts;
8099 * We invalidate the last ack here since we
8100 * don't want to transfer forward the time
8101 * for our sum's calculations.
8103 if (bbr->rc_inp->inp_in_hpts) {
8104 tcp_hpts_remove(bbr->rc_inp, HPTS_REMOVE_OUTPUT);
8105 bbr->rc_timer_first = 0;
8106 bbr->r_ctl.rc_hpts_flags = 0;
8107 bbr->r_ctl.rc_last_delay_val = 0;
8108 bbr->r_ctl.rc_hptsi_agg_delay = 0;
8109 bbr->r_agg_early_set = 0;
8110 bbr->r_ctl.rc_agg_early = 0;
8112 bbr_log_type_pesist(bbr, cts, idle_time, line, 0);
8113 if (idle_time >= bbr_rtt_probe_time) {
8115 * This qualifies as a RTT_PROBE session since we drop the
8116 * data outstanding to nothing and waited more than
8117 * bbr_rtt_probe_time.
8119 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_PERSIST, 0);
8120 bbr->r_ctl.last_in_probertt = bbr->r_ctl.rc_rtt_shrinks = cts;
8124 * If in probeBW and we have persisted more than an RTT lets do
8127 /* Force a time based epoch */
8128 bbr_set_epoch(bbr, cts, __LINE__);
8130 * Setup the lost so we don't count anything against the guy
8131 * we have been stuck with during persists.
8133 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
8134 /* Time un-freezes for the state */
8135 bbr->r_ctl.rc_bbr_state_time = cts;
8136 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) ||
8137 (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT)) {
8139 * If we are going back to probe-bw
8140 * or probe_rtt, we may need to possibly
8141 * do a fast restart.
8143 bbr_restart_after_idle(bbr, cts, idle_time);
8148 bbr_collapsed_window(struct tcp_bbr *bbr)
8151 * Now we must walk the
8152 * send map and divide the
8153 * ones left stranded. These
8154 * guys can't cause us to abort
8155 * the connection and are really
8156 * "unsent". However if a buggy
8157 * client actually did keep some
8158 * of the data i.e. collapsed the win
8159 * and refused to ack and then opened
8160 * the win and acked that data. We would
8161 * get into an ack war, the simplier
8162 * method then of just pretending we
8163 * did not send those segments something
8166 struct bbr_sendmap *rsm, *nrsm;
8172 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options;
8173 max_seq = bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd;
8174 bbr_log_type_rwnd_collapse(bbr, max_seq, 1, 0);
8175 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
8176 /* Find the first seq past or at maxseq */
8177 if (rsm->r_flags & BBR_RWND_COLLAPSED)
8178 rsm->r_flags &= ~BBR_RWND_COLLAPSED;
8179 if (SEQ_GEQ(max_seq, rsm->r_start) &&
8180 SEQ_GEQ(rsm->r_end, max_seq)) {
8185 bbr->rc_has_collapsed = 0;
8187 /* Nothing to do strange */
8193 * We don't want to split if splitting
8194 * would generate too many small segments
8195 * less we let an attacker fragment our
8196 * send_map and leave us out of memory.
8198 if ((max_seq != rsm->r_start) &&
8199 (max_seq != rsm->r_end)){
8203 res1 = max_seq - rsm->r_start;
8204 res2 = rsm->r_end - max_seq;
8205 if ((res1 >= (maxseg/8)) &&
8206 (res2 >= (maxseg/8))) {
8207 /* No small pieces here */
8209 } else if (bbr->r_ctl.rc_num_small_maps_alloced < bbr_sack_block_limit) {
8210 /* We are under the limit */
8214 /* Ok do we need to split this rsm? */
8215 if (max_seq == rsm->r_start) {
8216 /* It's this guy no split required */
8218 } else if (max_seq == rsm->r_end) {
8219 /* It's the next one no split required. */
8220 nrsm = TAILQ_NEXT(rsm, r_next);
8225 } else if (can_split && SEQ_LT(max_seq, rsm->r_end)) {
8226 /* yep we need to split it */
8227 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT);
8229 /* failed XXXrrs what can we do mark the whole? */
8234 bbr_log_type_rwnd_collapse(bbr, max_seq, 3, 0);
8235 bbr_clone_rsm(bbr, nrsm, rsm, max_seq);
8236 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next);
8237 if (rsm->r_in_tmap) {
8238 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8239 nrsm->r_in_tmap = 1;
8243 * Split not allowed just start here just
8249 BBR_STAT_INC(bbr_collapsed_win);
8250 /* reuse fnd as a count */
8252 TAILQ_FOREACH_FROM(nrsm, &bbr->r_ctl.rc_map, r_next) {
8253 nrsm->r_flags |= BBR_RWND_COLLAPSED;
8255 bbr->rc_has_collapsed = 1;
8257 bbr_log_type_rwnd_collapse(bbr, max_seq, 4, fnd);
8261 bbr_un_collapse_window(struct tcp_bbr *bbr)
8263 struct bbr_sendmap *rsm;
8266 TAILQ_FOREACH_REVERSE(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) {
8267 if (rsm->r_flags & BBR_RWND_COLLAPSED) {
8268 /* Clear the flag */
8269 rsm->r_flags &= ~BBR_RWND_COLLAPSED;
8274 bbr_log_type_rwnd_collapse(bbr,
8275 (bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd), 0, cleared);
8276 bbr->rc_has_collapsed = 0;
8280 * Return value of 1, the TCB is unlocked and most
8281 * likely gone, return value of 0, the TCB is still
8285 bbr_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so,
8286 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
8287 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
8290 * Update window information. Don't look at window if no ACK: TAC's
8291 * send garbage on first SYN.
8295 struct tcp_bbr *bbr;
8297 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8298 INP_WLOCK_ASSERT(tp->t_inpcb);
8299 nsegs = max(1, m->m_pkthdr.lro_nsegs);
8300 if ((thflags & TH_ACK) &&
8301 (SEQ_LT(tp->snd_wl1, th->th_seq) ||
8302 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
8303 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
8304 /* keep track of pure window updates */
8306 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
8307 KMOD_TCPSTAT_INC(tcps_rcvwinupd);
8308 tp->snd_wnd = tiwin;
8309 tp->snd_wl1 = th->th_seq;
8310 tp->snd_wl2 = th->th_ack;
8311 if (tp->snd_wnd > tp->max_sndwnd)
8312 tp->max_sndwnd = tp->snd_wnd;
8313 bbr->r_wanted_output = 1;
8314 } else if (thflags & TH_ACK) {
8315 if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) {
8316 tp->snd_wnd = tiwin;
8317 tp->snd_wl1 = th->th_seq;
8318 tp->snd_wl2 = th->th_ack;
8321 if (tp->snd_wnd < ctf_outstanding(tp))
8322 /* The peer collapsed its window on us */
8323 bbr_collapsed_window(bbr);
8324 else if (bbr->rc_has_collapsed)
8325 bbr_un_collapse_window(bbr);
8326 /* Was persist timer active and now we have window space? */
8327 if ((bbr->rc_in_persist != 0) &&
8328 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2),
8329 bbr_minseg(bbr)))) {
8331 * Make the rate persist at end of persist mode if idle long
8334 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8336 /* Make sure we output to start the timer */
8337 bbr->r_wanted_output = 1;
8339 /* Do we need to enter persist? */
8340 if ((bbr->rc_in_persist == 0) &&
8341 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
8342 TCPS_HAVEESTABLISHED(tp->t_state) &&
8343 (tp->snd_max == tp->snd_una) &&
8344 sbavail(&tp->t_inpcb->inp_socket->so_snd) &&
8345 (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) {
8346 /* No send window.. we must enter persist */
8347 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8349 if (tp->t_flags2 & TF2_DROP_AF_DATA) {
8354 * We don't support urgent data but
8355 * drag along the up just to make sure
8356 * if there is a stack switch no one
8359 tp->rcv_up = tp->rcv_nxt;
8360 INP_WLOCK_ASSERT(tp->t_inpcb);
8363 * Process the segment text, merging it into the TCP sequencing
8364 * queue, and arranging for acknowledgment of receipt if necessary.
8365 * This process logically involves adjusting tp->rcv_wnd as data is
8366 * presented to the user (this happens in tcp_usrreq.c, case
8367 * PRU_RCVD). If a FIN has already been received on this connection
8368 * then we just ignore the text.
8370 tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
8371 IS_FASTOPEN(tp->t_flags));
8372 if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
8373 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
8374 tcp_seq save_start = th->th_seq;
8375 tcp_seq save_rnxt = tp->rcv_nxt;
8376 int save_tlen = tlen;
8378 m_adj(m, drop_hdrlen); /* delayed header drop */
8380 * Insert segment which includes th into TCP reassembly
8381 * queue with control block tp. Set thflags to whether
8382 * reassembly now includes a segment with FIN. This handles
8383 * the common case inline (segment is the next to be
8384 * received on an established connection, and the queue is
8385 * empty), avoiding linkage into and removal from the queue
8386 * and repetition of various conversions. Set DELACK for
8387 * segments received in order, but ack immediately when
8388 * segments are out of order (so fast retransmit can work).
8390 if (th->th_seq == tp->rcv_nxt &&
8392 (TCPS_HAVEESTABLISHED(tp->t_state) ||
8394 #ifdef NETFLIX_SB_LIMITS
8395 u_int mcnt, appended;
8397 if (so->so_rcv.sb_shlim) {
8400 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
8401 CFO_NOSLEEP, NULL) == false) {
8402 counter_u64_add(tcp_sb_shlim_fails, 1);
8409 if (DELAY_ACK(tp, bbr, nsegs) || tfo_syn) {
8410 bbr->bbr_segs_rcvd += max(1, nsegs);
8411 tp->t_flags |= TF_DELACK;
8412 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8414 bbr->r_wanted_output = 1;
8415 tp->t_flags |= TF_ACKNOW;
8417 tp->rcv_nxt += tlen;
8419 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
8420 (tp->t_fbyte_in == 0)) {
8421 tp->t_fbyte_in = ticks;
8422 if (tp->t_fbyte_in == 0)
8424 if (tp->t_fbyte_out && tp->t_fbyte_in)
8425 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
8427 thflags = th->th_flags & TH_FIN;
8428 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs);
8429 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
8430 SOCKBUF_LOCK(&so->so_rcv);
8431 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
8434 #ifdef NETFLIX_SB_LIMITS
8437 sbappendstream_locked(&so->so_rcv, m, 0);
8438 /* NB: sorwakeup_locked() does an implicit unlock. */
8439 sorwakeup_locked(so);
8440 #ifdef NETFLIX_SB_LIMITS
8441 if (so->so_rcv.sb_shlim && appended != mcnt)
8442 counter_fo_release(so->so_rcv.sb_shlim,
8447 * XXX: Due to the header drop above "th" is
8448 * theoretically invalid by now. Fortunately
8449 * m_adj() doesn't actually frees any mbufs when
8450 * trimming from the head.
8452 tcp_seq temp = save_start;
8453 thflags = tcp_reass(tp, th, &temp, &tlen, m);
8454 tp->t_flags |= TF_ACKNOW;
8456 if ((tp->t_flags & TF_SACK_PERMIT) && (save_tlen > 0)) {
8457 if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
8459 * DSACK actually handled in the fastpath
8462 tcp_update_sack_list(tp, save_start,
8463 save_start + save_tlen);
8464 } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
8465 if ((tp->rcv_numsacks >= 1) &&
8466 (tp->sackblks[0].end == save_start)) {
8468 * Partial overlap, recorded at todrop
8471 tcp_update_sack_list(tp,
8472 tp->sackblks[0].start,
8473 tp->sackblks[0].end);
8475 tcp_update_dsack_list(tp, save_start,
8476 save_start + save_tlen);
8478 } else if (tlen >= save_tlen) {
8479 /* Update of sackblks. */
8480 tcp_update_dsack_list(tp, save_start,
8481 save_start + save_tlen);
8482 } else if (tlen > 0) {
8483 tcp_update_dsack_list(tp, save_start,
8493 * If FIN is received ACK the FIN and let the user know that the
8494 * connection is closing.
8496 if (thflags & TH_FIN) {
8497 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
8500 * If connection is half-synchronized (ie NEEDSYN
8501 * flag on) then delay ACK, so it may be piggybacked
8502 * when SYN is sent. Otherwise, since we received a
8503 * FIN then no more input can be expected, send ACK
8506 if (tp->t_flags & TF_NEEDSYN) {
8507 tp->t_flags |= TF_DELACK;
8508 bbr_timer_cancel(bbr,
8509 __LINE__, bbr->r_ctl.rc_rcvtime);
8511 tp->t_flags |= TF_ACKNOW;
8515 switch (tp->t_state) {
8518 * In SYN_RECEIVED and ESTABLISHED STATES enter the
8521 case TCPS_SYN_RECEIVED:
8522 tp->t_starttime = ticks;
8524 case TCPS_ESTABLISHED:
8525 tcp_state_change(tp, TCPS_CLOSE_WAIT);
8529 * If still in FIN_WAIT_1 STATE FIN has not been
8530 * acked so enter the CLOSING state.
8532 case TCPS_FIN_WAIT_1:
8533 tcp_state_change(tp, TCPS_CLOSING);
8537 * In FIN_WAIT_2 state enter the TIME_WAIT state,
8538 * starting the time-wait timer, turning off the
8539 * other standard timers.
8541 case TCPS_FIN_WAIT_2:
8542 bbr->rc_timer_first = 1;
8543 bbr_timer_cancel(bbr,
8544 __LINE__, bbr->r_ctl.rc_rcvtime);
8545 INP_WLOCK_ASSERT(tp->t_inpcb);
8551 * Return any desired output.
8553 if ((tp->t_flags & TF_ACKNOW) ||
8554 (sbavail(&so->so_snd) > ctf_outstanding(tp))) {
8555 bbr->r_wanted_output = 1;
8557 INP_WLOCK_ASSERT(tp->t_inpcb);
8562 * Here nothing is really faster, its just that we
8563 * have broken out the fast-data path also just like
8564 * the fast-ack. Return 1 if we processed the packet
8565 * return 0 if you need to take the "slow-path".
8568 bbr_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so,
8569 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
8570 uint32_t tiwin, int32_t nxt_pkt)
8573 int32_t newsize = 0; /* automatic sockbuf scaling */
8574 struct tcp_bbr *bbr;
8575 #ifdef NETFLIX_SB_LIMITS
8576 u_int mcnt, appended;
8580 * The size of tcp_saveipgen must be the size of the max ip header,
8583 u_char tcp_saveipgen[IP6_HDR_LEN];
8584 struct tcphdr tcp_savetcp;
8588 /* On the hpts and we would have called output */
8589 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8592 * If last ACK falls within this segment's sequence numbers, record
8593 * the timestamp. NOTE that the test is modified according to the
8594 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
8596 if (bbr->r_ctl.rc_resend != NULL) {
8599 if (tiwin && tiwin != tp->snd_wnd) {
8602 if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) {
8605 if (__predict_false((to->to_flags & TOF_TS) &&
8606 (TSTMP_LT(to->to_tsval, tp->ts_recent)))) {
8609 if (__predict_false((th->th_ack != tp->snd_una))) {
8612 if (__predict_false(tlen > sbspace(&so->so_rcv))) {
8615 if ((to->to_flags & TOF_TS) != 0 &&
8616 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
8617 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
8618 tp->ts_recent = to->to_tsval;
8621 * This is a pure, in-sequence data packet with nothing on the
8622 * reassembly queue and we have enough buffer space to take it.
8624 nsegs = max(1, m->m_pkthdr.lro_nsegs);
8626 #ifdef NETFLIX_SB_LIMITS
8627 if (so->so_rcv.sb_shlim) {
8630 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
8631 CFO_NOSLEEP, NULL) == false) {
8632 counter_u64_add(tcp_sb_shlim_fails, 1);
8638 /* Clean receiver SACK report if present */
8639 if (tp->rcv_numsacks)
8640 tcp_clean_sackreport(tp);
8641 KMOD_TCPSTAT_INC(tcps_preddat);
8642 tp->rcv_nxt += tlen;
8644 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
8645 (tp->t_fbyte_in == 0)) {
8646 tp->t_fbyte_in = ticks;
8647 if (tp->t_fbyte_in == 0)
8649 if (tp->t_fbyte_out && tp->t_fbyte_in)
8650 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
8653 * Pull snd_wl1 up to prevent seq wrap relative to th_seq.
8655 tp->snd_wl1 = th->th_seq;
8657 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt.
8659 tp->rcv_up = tp->rcv_nxt;
8660 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs);
8661 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
8663 if (so->so_options & SO_DEBUG)
8664 tcp_trace(TA_INPUT, ostate, tp,
8665 (void *)tcp_saveipgen, &tcp_savetcp, 0);
8667 newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
8669 /* Add data to socket buffer. */
8670 SOCKBUF_LOCK(&so->so_rcv);
8671 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
8675 * Set new socket buffer size. Give up when limit is
8679 if (!sbreserve_locked(&so->so_rcv,
8681 so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
8682 m_adj(m, drop_hdrlen); /* delayed header drop */
8684 #ifdef NETFLIX_SB_LIMITS
8687 sbappendstream_locked(&so->so_rcv, m, 0);
8688 ctf_calc_rwin(so, tp);
8690 /* NB: sorwakeup_locked() does an implicit unlock. */
8691 sorwakeup_locked(so);
8692 #ifdef NETFLIX_SB_LIMITS
8693 if (so->so_rcv.sb_shlim && mcnt != appended)
8694 counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended);
8696 if (DELAY_ACK(tp, bbr, nsegs)) {
8697 bbr->bbr_segs_rcvd += max(1, nsegs);
8698 tp->t_flags |= TF_DELACK;
8699 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8701 bbr->r_wanted_output = 1;
8702 tp->t_flags |= TF_ACKNOW;
8708 * This subfunction is used to try to highly optimize the
8709 * fast path. We again allow window updates that are
8710 * in sequence to remain in the fast-path. We also add
8711 * in the __predict's to attempt to help the compiler.
8712 * Note that if we return a 0, then we can *not* process
8713 * it and the caller should push the packet into the
8714 * slow-path. If we return 1, then all is well and
8715 * the packet is fully processed.
8718 bbr_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
8719 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
8720 uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos)
8724 uint32_t sack_changed;
8727 * The size of tcp_saveipgen must be the size of the max ip header,
8730 u_char tcp_saveipgen[IP6_HDR_LEN];
8731 struct tcphdr tcp_savetcp;
8735 uint32_t prev_acked = 0;
8736 struct tcp_bbr *bbr;
8738 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
8739 /* Old ack, behind (or duplicate to) the last one rcv'd */
8742 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) {
8743 /* Above what we have sent? */
8746 if (__predict_false(tiwin == 0)) {
8750 if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) {
8751 /* We need a SYN or a FIN, unlikely.. */
8754 if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) {
8755 /* Timestamp is behind .. old ack with seq wrap? */
8758 if (__predict_false(IN_RECOVERY(tp->t_flags))) {
8759 /* Still recovering */
8762 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8763 if (__predict_false(bbr->r_ctl.rc_resend != NULL)) {
8764 /* We are retransmitting */
8767 if (__predict_false(bbr->rc_in_persist != 0)) {
8768 /* In persist mode */
8771 if (bbr->r_ctl.rc_sacked) {
8772 /* We have sack holes on our scoreboard */
8775 /* Ok if we reach here, we can process a fast-ack */
8776 nsegs = max(1, m->m_pkthdr.lro_nsegs);
8777 sack_changed = bbr_log_ack(tp, to, th, &prev_acked);
8779 * We never detect loss in fast ack [we can't
8780 * have a sack and can't be in recovery so
8781 * we always pass 0 (nothing detected)].
8783 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, 0);
8784 /* Did the window get updated? */
8785 if (tiwin != tp->snd_wnd) {
8786 tp->snd_wnd = tiwin;
8787 tp->snd_wl1 = th->th_seq;
8788 if (tp->snd_wnd > tp->max_sndwnd)
8789 tp->max_sndwnd = tp->snd_wnd;
8791 /* Do we need to exit persists? */
8792 if ((bbr->rc_in_persist != 0) &&
8793 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2),
8794 bbr_minseg(bbr)))) {
8795 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8796 bbr->r_wanted_output = 1;
8798 /* Do we need to enter persists? */
8799 if ((bbr->rc_in_persist == 0) &&
8800 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
8801 TCPS_HAVEESTABLISHED(tp->t_state) &&
8802 (tp->snd_max == tp->snd_una) &&
8803 sbavail(&tp->t_inpcb->inp_socket->so_snd) &&
8804 (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) {
8805 /* No send window.. we must enter persist */
8806 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
8809 * If last ACK falls within this segment's sequence numbers, record
8810 * the timestamp. NOTE that the test is modified according to the
8811 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
8813 if ((to->to_flags & TOF_TS) != 0 &&
8814 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
8815 tp->ts_recent_age = bbr->r_ctl.rc_rcvtime;
8816 tp->ts_recent = to->to_tsval;
8819 * This is a pure ack for outstanding data.
8821 KMOD_TCPSTAT_INC(tcps_predack);
8824 * "bad retransmit" recovery.
8826 if (tp->t_flags & TF_PREVVALID) {
8827 tp->t_flags &= ~TF_PREVVALID;
8828 if (tp->t_rxtshift == 1 &&
8829 (int)(ticks - tp->t_badrxtwin) < 0)
8830 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL);
8833 * Recalculate the transmit timer / rtt.
8835 * Some boxes send broken timestamp replies during the SYN+ACK
8836 * phase, ignore timestamps of 0 or we could calculate a huge RTT
8837 * and blow up the retransmit timer.
8839 acked = BYTES_THIS_ACK(tp, th);
8842 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
8843 hhook_run_tcp_est_in(tp, th, to);
8846 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs);
8847 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
8848 sbdrop(&so->so_snd, acked);
8850 if (SEQ_GT(th->th_ack, tp->snd_una))
8851 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp));
8852 tp->snd_una = th->th_ack;
8853 if (tp->snd_wnd < ctf_outstanding(tp))
8854 /* The peer collapsed its window on us */
8855 bbr_collapsed_window(bbr);
8856 else if (bbr->rc_has_collapsed)
8857 bbr_un_collapse_window(bbr);
8859 if (SEQ_GT(tp->snd_una, tp->snd_recover)) {
8860 tp->snd_recover = tp->snd_una;
8862 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, 0);
8864 * Pull snd_wl2 up to prevent seq wrap relative to th_ack.
8866 tp->snd_wl2 = th->th_ack;
8869 * If all outstanding data are acked, stop retransmit timer,
8870 * otherwise restart timer using current (possibly backed-off)
8871 * value. If process is waiting for space, wakeup/selwakeup/signal.
8872 * If data are ready to send, let tcp_output decide between more
8873 * output or persist.
8876 if (so->so_options & SO_DEBUG)
8877 tcp_trace(TA_INPUT, ostate, tp,
8878 (void *)tcp_saveipgen,
8881 /* Wake up the socket if we have room to write more */
8883 if (tp->snd_una == tp->snd_max) {
8884 /* Nothing left outstanding */
8885 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__);
8886 if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0)
8887 bbr->rc_tp->t_acktime = 0;
8888 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8889 if (bbr->rc_in_persist == 0) {
8890 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime;
8892 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
8893 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime);
8895 * We invalidate the last ack here since we
8896 * don't want to transfer forward the time
8897 * for our sum's calculations.
8899 bbr->r_wanted_output = 1;
8901 if (sbavail(&so->so_snd)) {
8902 bbr->r_wanted_output = 1;
8908 * Return value of 1, the TCB is unlocked and most
8909 * likely gone, return value of 0, the TCB is still
8913 bbr_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so,
8914 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
8915 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
8918 int32_t ourfinisacked = 0;
8919 struct tcp_bbr *bbr;
8920 int32_t ret_val = 0;
8922 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
8923 ctf_calc_rwin(so, tp);
8925 * If the state is SYN_SENT: if seg contains an ACK, but not for our
8926 * SYN, drop the input. if seg contains a RST, then drop the
8927 * connection. if seg does not contain SYN, then drop it. Otherwise
8928 * this is an acceptable SYN segment initialize tp->rcv_nxt and
8929 * tp->irs if seg contains ack then advance tp->snd_una. BRR does
8930 * not support ECN so we will not say we are capable. if SYN has
8931 * been acked change to ESTABLISHED else SYN_RCVD state arrange for
8932 * segment to be acked (eventually) continue processing rest of
8933 * data/controls, beginning with URG
8935 if ((thflags & TH_ACK) &&
8936 (SEQ_LEQ(th->th_ack, tp->iss) ||
8937 SEQ_GT(th->th_ack, tp->snd_max))) {
8938 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
8939 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
8942 if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) {
8943 TCP_PROBE5(connect__refused, NULL, tp,
8944 mtod(m, const char *), tp, th);
8945 tp = tcp_drop(tp, ECONNREFUSED);
8949 if (thflags & TH_RST) {
8953 if (!(thflags & TH_SYN)) {
8957 tp->irs = th->th_seq;
8959 if (thflags & TH_ACK) {
8960 int tfo_partial = 0;
8962 KMOD_TCPSTAT_INC(tcps_connects);
8965 mac_socketpeer_set_from_mbuf(m, so);
8967 /* Do window scaling on this connection? */
8968 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
8969 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
8970 tp->rcv_scale = tp->request_r_scale;
8972 tp->rcv_adv += min(tp->rcv_wnd,
8973 TCP_MAXWIN << tp->rcv_scale);
8975 * If not all the data that was sent in the TFO SYN
8976 * has been acked, resend the remainder right away.
8978 if (IS_FASTOPEN(tp->t_flags) &&
8979 (tp->snd_una != tp->snd_max)) {
8980 tp->snd_nxt = th->th_ack;
8984 * If there's data, delay ACK; if there's also a FIN ACKNOW
8985 * will be turned on later.
8987 if (DELAY_ACK(tp, bbr, 1) && tlen != 0 && !tfo_partial) {
8988 bbr->bbr_segs_rcvd += 1;
8989 tp->t_flags |= TF_DELACK;
8990 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime);
8992 bbr->r_wanted_output = 1;
8993 tp->t_flags |= TF_ACKNOW;
8995 if (SEQ_GT(th->th_ack, tp->iss)) {
8998 * handle it specially.
9000 bbr_log_syn(tp, to);
9002 if (SEQ_GT(th->th_ack, tp->snd_una)) {
9004 * We advance snd_una for the
9005 * fast open case. If th_ack is
9006 * acknowledging data beyond
9007 * snd_una we can't just call
9008 * ack-processing since the
9009 * data stream in our send-map
9010 * will start at snd_una + 1 (one
9011 * beyond the SYN). If its just
9012 * equal we don't need to do that
9013 * and there is no send_map.
9018 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions:
9019 * SYN_SENT --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1
9021 tp->t_starttime = ticks;
9022 if (tp->t_flags & TF_NEEDFIN) {
9023 tcp_state_change(tp, TCPS_FIN_WAIT_1);
9024 tp->t_flags &= ~TF_NEEDFIN;
9027 tcp_state_change(tp, TCPS_ESTABLISHED);
9028 TCP_PROBE5(connect__established, NULL, tp,
9029 mtod(m, const char *), tp, th);
9034 * Received initial SYN in SYN-SENT[*] state => simultaneous
9035 * open. If segment contains CC option and there is a
9036 * cached CC, apply TAO test. If it succeeds, connection is *
9037 * half-synchronized. Otherwise, do 3-way handshake:
9038 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If
9039 * there was no CC option, clear cached CC value.
9041 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
9042 tcp_state_change(tp, TCPS_SYN_RECEIVED);
9044 INP_WLOCK_ASSERT(tp->t_inpcb);
9046 * Advance th->th_seq to correspond to first data byte. If data,
9047 * trim to stay within window, dropping FIN if necessary.
9050 if (tlen > tp->rcv_wnd) {
9051 todrop = tlen - tp->rcv_wnd;
9055 KMOD_TCPSTAT_INC(tcps_rcvpackafterwin);
9056 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
9058 tp->snd_wl1 = th->th_seq - 1;
9059 tp->rcv_up = th->th_seq;
9061 * Client side of transaction: already sent SYN and data. If the
9062 * remote host used T/TCP to validate the SYN, our data will be
9063 * ACK'd; if so, enter normal data segment processing in the middle
9064 * of step 5, ack processing. Otherwise, goto step 6.
9066 if (thflags & TH_ACK) {
9067 if ((to->to_flags & TOF_TS) != 0) {
9070 t = tcp_tv_to_mssectick(&bbr->rc_tv);
9071 if (TSTMP_GEQ(t, to->to_tsecr)) {
9072 rtt = t - to->to_tsecr;
9077 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0);
9078 apply_filter_min_small(&bbr->r_ctl.rc_rttprop,
9079 rtt, bbr->r_ctl.rc_rcvtime);
9082 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val))
9084 /* We may have changed to FIN_WAIT_1 above */
9085 if (tp->t_state == TCPS_FIN_WAIT_1) {
9087 * In FIN_WAIT_1 STATE in addition to the processing
9088 * for the ESTABLISHED state if our FIN is now
9089 * acknowledged then enter FIN_WAIT_2.
9091 if (ourfinisacked) {
9093 * If we can't receive any more data, then
9094 * closing user can proceed. Starting the
9095 * timer is contrary to the specification,
9096 * but if we don't get a FIN we'll hang
9099 * XXXjl: we should release the tp also, and
9100 * use a compressed state.
9102 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
9103 soisdisconnected(so);
9104 tcp_timer_activate(tp, TT_2MSL,
9105 (tcp_fast_finwait2_recycle ?
9106 tcp_finwait2_timeout :
9109 tcp_state_change(tp, TCPS_FIN_WAIT_2);
9113 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9114 tiwin, thflags, nxt_pkt));
9118 * Return value of 1, the TCB is unlocked and most
9119 * likely gone, return value of 0, the TCB is still
9123 bbr_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so,
9124 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9125 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9127 int32_t ourfinisacked = 0;
9129 struct tcp_bbr *bbr;
9131 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9132 ctf_calc_rwin(so, tp);
9133 if ((thflags & TH_ACK) &&
9134 (SEQ_LEQ(th->th_ack, tp->snd_una) ||
9135 SEQ_GT(th->th_ack, tp->snd_max))) {
9136 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
9137 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9140 if (IS_FASTOPEN(tp->t_flags)) {
9142 * When a TFO connection is in SYN_RECEIVED, the only valid
9143 * packets are the initial SYN, a retransmit/copy of the
9144 * initial SYN (possibly with a subset of the original
9145 * data), a valid ACK, a FIN, or a RST.
9147 if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) {
9148 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
9149 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9151 } else if (thflags & TH_SYN) {
9152 /* non-initial SYN is ignored */
9153 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RXT) ||
9154 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_TLP) ||
9155 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) {
9156 ctf_do_drop(m, NULL);
9159 } else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) {
9160 ctf_do_drop(m, NULL);
9164 if ((thflags & TH_RST) ||
9165 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9166 return (ctf_process_rst(m, th, so, tp));
9168 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9169 * it's less than ts_recent, drop it.
9171 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9172 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9173 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9177 * In the SYN-RECEIVED state, validate that the packet belongs to
9178 * this connection before trimming the data to fit the receive
9179 * window. Check the sequence number versus IRS since we know the
9180 * sequence numbers haven't wrapped. This is a partial fix for the
9181 * "LAND" DoS attack.
9183 if (SEQ_LT(th->th_seq, tp->irs)) {
9184 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
9185 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9188 INP_WLOCK_ASSERT(tp->t_inpcb);
9189 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9193 * If last ACK falls within this segment's sequence numbers, record
9194 * its timestamp. NOTE: 1) That the test incorporates suggestions
9195 * from the latest proposal of the tcplw@cray.com list (Braden
9196 * 1993/04/26). 2) That updating only on newer timestamps interferes
9197 * with our earlier PAWS tests, so this check should be solely
9198 * predicated on the sequence space of this segment. 3) That we
9199 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9200 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9201 * SEG.Len, This modified check allows us to overcome RFC1323's
9202 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9203 * p.869. In such cases, we can still calculate the RTT correctly
9204 * when RCV.NXT == Last.ACK.Sent.
9206 if ((to->to_flags & TOF_TS) != 0 &&
9207 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9208 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9209 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9210 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9211 tp->ts_recent = to->to_tsval;
9213 tp->snd_wnd = tiwin;
9215 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9216 * is on (half-synchronized state), then queue data for later
9217 * processing; else drop segment and return.
9219 if ((thflags & TH_ACK) == 0) {
9220 if (IS_FASTOPEN(tp->t_flags)) {
9223 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9224 tiwin, thflags, nxt_pkt));
9226 KMOD_TCPSTAT_INC(tcps_connects);
9228 /* Do window scaling? */
9229 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
9230 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
9231 tp->rcv_scale = tp->request_r_scale;
9234 * ok for the first time in lets see if we can use the ts to figure
9235 * out what the initial RTT was.
9237 if ((to->to_flags & TOF_TS) != 0) {
9240 t = tcp_tv_to_mssectick(&bbr->rc_tv);
9241 if (TSTMP_GEQ(t, to->to_tsecr)) {
9242 rtt = t - to->to_tsecr;
9247 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0);
9248 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, bbr->r_ctl.rc_rcvtime);
9251 /* Drop off any SYN in the send map (probably not there) */
9252 if (thflags & TH_ACK)
9253 bbr_log_syn(tp, to);
9254 if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
9256 tcp_fastopen_decrement_counter(tp->t_tfo_pending);
9257 tp->t_tfo_pending = NULL;
9260 * Make transitions: SYN-RECEIVED -> ESTABLISHED SYN-RECEIVED* ->
9263 tp->t_starttime = ticks;
9264 if (tp->t_flags & TF_NEEDFIN) {
9265 tcp_state_change(tp, TCPS_FIN_WAIT_1);
9266 tp->t_flags &= ~TF_NEEDFIN;
9268 tcp_state_change(tp, TCPS_ESTABLISHED);
9269 TCP_PROBE5(accept__established, NULL, tp,
9270 mtod(m, const char *), tp, th);
9272 * TFO connections call cc_conn_init() during SYN
9273 * processing. Calling it again here for such connections
9274 * is not harmless as it would undo the snd_cwnd reduction
9275 * that occurs when a TFO SYN|ACK is retransmitted.
9277 if (!IS_FASTOPEN(tp->t_flags))
9281 * Account for the ACK of our SYN prior to
9282 * regular ACK processing below, except for
9283 * simultaneous SYN, which is handled later.
9285 if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
9288 * If segment contains data or ACK, will call tcp_reass() later; if
9289 * not, do so now to pass queued data to user.
9291 if (tlen == 0 && (thflags & TH_FIN) == 0)
9292 (void)tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
9294 tp->snd_wl1 = th->th_seq - 1;
9295 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9298 if (tp->t_state == TCPS_FIN_WAIT_1) {
9299 /* We could have went to FIN_WAIT_1 (or EST) above */
9301 * In FIN_WAIT_1 STATE in addition to the processing for the
9302 * ESTABLISHED state if our FIN is now acknowledged then
9305 if (ourfinisacked) {
9307 * If we can't receive any more data, then closing
9308 * user can proceed. Starting the timer is contrary
9309 * to the specification, but if we don't get a FIN
9310 * we'll hang forever.
9312 * XXXjl: we should release the tp also, and use a
9315 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
9316 soisdisconnected(so);
9317 tcp_timer_activate(tp, TT_2MSL,
9318 (tcp_fast_finwait2_recycle ?
9319 tcp_finwait2_timeout :
9322 tcp_state_change(tp, TCPS_FIN_WAIT_2);
9325 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9326 tiwin, thflags, nxt_pkt));
9330 * Return value of 1, the TCB is unlocked and most
9331 * likely gone, return value of 0, the TCB is still
9335 bbr_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so,
9336 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9337 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9339 struct tcp_bbr *bbr;
9343 * Header prediction: check for the two common cases of a
9344 * uni-directional data xfer. If the packet has no control flags,
9345 * is in-sequence, the window didn't change and we're not
9346 * retransmitting, it's a candidate. If the length is zero and the
9347 * ack moved forward, we're the sender side of the xfer. Just free
9348 * the data acked & wake any higher level process that was blocked
9349 * waiting for space. If the length is non-zero and the ack didn't
9350 * move, we're the receiver side. If we're getting packets in-order
9351 * (the reassembly queue is empty), add the data toc The socket
9352 * buffer and note that we need a delayed ack. Make sure that the
9353 * hidden state-flags are also off. Since we check for
9354 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN.
9356 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9357 if (bbr->r_ctl.rc_delivered < (4 * tp->t_maxseg)) {
9359 * If we have delived under 4 segments increase the initial
9360 * window if raised by the peer. We use this to determine
9361 * dynamic and static rwnd's at the end of a connection.
9363 bbr->r_ctl.rc_init_rwnd = max(tiwin, tp->snd_wnd);
9365 if (__predict_true(((to->to_flags & TOF_SACK) == 0)) &&
9366 __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_URG | TH_ACK)) == TH_ACK) &&
9367 __predict_true(SEGQ_EMPTY(tp)) &&
9368 __predict_true(th->th_seq == tp->rcv_nxt)) {
9370 if (bbr_fastack(m, th, so, tp, to, drop_hdrlen, tlen,
9371 tiwin, nxt_pkt, iptos)) {
9375 if (bbr_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen,
9381 ctf_calc_rwin(so, tp);
9383 if ((thflags & TH_RST) ||
9384 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9385 return (ctf_process_rst(m, th, so, tp));
9387 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9388 * synchronized state.
9390 if (thflags & TH_SYN) {
9391 ctf_challenge_ack(m, th, tp, &ret_val);
9395 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9396 * it's less than ts_recent, drop it.
9398 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9399 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9400 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9403 INP_WLOCK_ASSERT(tp->t_inpcb);
9404 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9408 * If last ACK falls within this segment's sequence numbers, record
9409 * its timestamp. NOTE: 1) That the test incorporates suggestions
9410 * from the latest proposal of the tcplw@cray.com list (Braden
9411 * 1993/04/26). 2) That updating only on newer timestamps interferes
9412 * with our earlier PAWS tests, so this check should be solely
9413 * predicated on the sequence space of this segment. 3) That we
9414 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9415 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9416 * SEG.Len, This modified check allows us to overcome RFC1323's
9417 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9418 * p.869. In such cases, we can still calculate the RTT correctly
9419 * when RCV.NXT == Last.ACK.Sent.
9421 if ((to->to_flags & TOF_TS) != 0 &&
9422 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9423 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9424 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9425 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9426 tp->ts_recent = to->to_tsval;
9429 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9430 * is on (half-synchronized state), then queue data for later
9431 * processing; else drop segment and return.
9433 if ((thflags & TH_ACK) == 0) {
9434 if (tp->t_flags & TF_NEEDSYN) {
9435 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9436 tiwin, thflags, nxt_pkt));
9437 } else if (tp->t_flags & TF_ACKNOW) {
9438 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9439 bbr->r_wanted_output = 1;
9442 ctf_do_drop(m, NULL);
9449 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
9452 if (sbavail(&so->so_snd)) {
9453 if (ctf_progress_timeout_check(tp, true)) {
9454 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9455 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9459 /* State changes only happen in bbr_process_data() */
9460 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9461 tiwin, thflags, nxt_pkt));
9465 * Return value of 1, the TCB is unlocked and most
9466 * likely gone, return value of 0, the TCB is still
9470 bbr_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so,
9471 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9472 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9474 struct tcp_bbr *bbr;
9477 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9478 ctf_calc_rwin(so, tp);
9479 if ((thflags & TH_RST) ||
9480 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9481 return (ctf_process_rst(m, th, so, tp));
9483 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9484 * synchronized state.
9486 if (thflags & TH_SYN) {
9487 ctf_challenge_ack(m, th, tp, &ret_val);
9491 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9492 * it's less than ts_recent, drop it.
9494 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9495 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9496 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9499 INP_WLOCK_ASSERT(tp->t_inpcb);
9500 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9504 * If last ACK falls within this segment's sequence numbers, record
9505 * its timestamp. NOTE: 1) That the test incorporates suggestions
9506 * from the latest proposal of the tcplw@cray.com list (Braden
9507 * 1993/04/26). 2) That updating only on newer timestamps interferes
9508 * with our earlier PAWS tests, so this check should be solely
9509 * predicated on the sequence space of this segment. 3) That we
9510 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9511 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9512 * SEG.Len, This modified check allows us to overcome RFC1323's
9513 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9514 * p.869. In such cases, we can still calculate the RTT correctly
9515 * when RCV.NXT == Last.ACK.Sent.
9517 if ((to->to_flags & TOF_TS) != 0 &&
9518 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9519 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9520 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9521 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9522 tp->ts_recent = to->to_tsval;
9525 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9526 * is on (half-synchronized state), then queue data for later
9527 * processing; else drop segment and return.
9529 if ((thflags & TH_ACK) == 0) {
9530 if (tp->t_flags & TF_NEEDSYN) {
9531 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9532 tiwin, thflags, nxt_pkt));
9533 } else if (tp->t_flags & TF_ACKNOW) {
9534 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9535 bbr->r_wanted_output = 1;
9538 ctf_do_drop(m, NULL);
9545 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
9548 if (sbavail(&so->so_snd)) {
9549 if (ctf_progress_timeout_check(tp, true)) {
9550 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9551 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9555 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9556 tiwin, thflags, nxt_pkt));
9560 bbr_check_data_after_close(struct mbuf *m, struct tcp_bbr *bbr,
9561 struct tcpcb *tp, int32_t * tlen, struct tcphdr *th, struct socket *so)
9564 if (bbr->rc_allow_data_af_clo == 0) {
9566 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
9567 /* tcp_close will kill the inp pre-log the Reset */
9568 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
9570 KMOD_TCPSTAT_INC(tcps_rcvafterclose);
9571 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen));
9574 if (sbavail(&so->so_snd) == 0)
9576 /* Ok we allow data that is ignored and a followup reset */
9577 tp->rcv_nxt = th->th_seq + *tlen;
9578 tp->t_flags2 |= TF2_DROP_AF_DATA;
9579 bbr->r_wanted_output = 1;
9585 * Return value of 1, the TCB is unlocked and most
9586 * likely gone, return value of 0, the TCB is still
9590 bbr_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so,
9591 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9592 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9594 int32_t ourfinisacked = 0;
9596 struct tcp_bbr *bbr;
9598 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9599 ctf_calc_rwin(so, tp);
9600 if ((thflags & TH_RST) ||
9601 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9602 return (ctf_process_rst(m, th, so, tp));
9604 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9605 * synchronized state.
9607 if (thflags & TH_SYN) {
9608 ctf_challenge_ack(m, th, tp, &ret_val);
9612 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9613 * it's less than ts_recent, drop it.
9615 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9616 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9617 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9620 INP_WLOCK_ASSERT(tp->t_inpcb);
9621 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9625 * If new data are received on a connection after the user processes
9626 * are gone, then RST the other end.
9628 if ((so->so_state & SS_NOFDREF) && tlen) {
9630 * We call a new function now so we might continue and setup
9631 * to reset at all data being ack'd.
9633 if (bbr_check_data_after_close(m, bbr, tp, &tlen, th, so))
9637 * If last ACK falls within this segment's sequence numbers, record
9638 * its timestamp. NOTE: 1) That the test incorporates suggestions
9639 * from the latest proposal of the tcplw@cray.com list (Braden
9640 * 1993/04/26). 2) That updating only on newer timestamps interferes
9641 * with our earlier PAWS tests, so this check should be solely
9642 * predicated on the sequence space of this segment. 3) That we
9643 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9644 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9645 * SEG.Len, This modified check allows us to overcome RFC1323's
9646 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9647 * p.869. In such cases, we can still calculate the RTT correctly
9648 * when RCV.NXT == Last.ACK.Sent.
9650 if ((to->to_flags & TOF_TS) != 0 &&
9651 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9652 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9653 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9654 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9655 tp->ts_recent = to->to_tsval;
9658 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9659 * is on (half-synchronized state), then queue data for later
9660 * processing; else drop segment and return.
9662 if ((thflags & TH_ACK) == 0) {
9663 if (tp->t_flags & TF_NEEDSYN) {
9664 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9665 tiwin, thflags, nxt_pkt));
9666 } else if (tp->t_flags & TF_ACKNOW) {
9667 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9668 bbr->r_wanted_output = 1;
9671 ctf_do_drop(m, NULL);
9678 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9681 if (ourfinisacked) {
9683 * If we can't receive any more data, then closing user can
9684 * proceed. Starting the timer is contrary to the
9685 * specification, but if we don't get a FIN we'll hang
9688 * XXXjl: we should release the tp also, and use a
9691 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
9692 soisdisconnected(so);
9693 tcp_timer_activate(tp, TT_2MSL,
9694 (tcp_fast_finwait2_recycle ?
9695 tcp_finwait2_timeout :
9698 tcp_state_change(tp, TCPS_FIN_WAIT_2);
9700 if (sbavail(&so->so_snd)) {
9701 if (ctf_progress_timeout_check(tp, true)) {
9702 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9703 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9707 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9708 tiwin, thflags, nxt_pkt));
9712 * Return value of 1, the TCB is unlocked and most
9713 * likely gone, return value of 0, the TCB is still
9717 bbr_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so,
9718 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9719 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9721 int32_t ourfinisacked = 0;
9723 struct tcp_bbr *bbr;
9725 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9726 ctf_calc_rwin(so, tp);
9727 if ((thflags & TH_RST) ||
9728 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9729 return (ctf_process_rst(m, th, so, tp));
9731 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9732 * synchronized state.
9734 if (thflags & TH_SYN) {
9735 ctf_challenge_ack(m, th, tp, &ret_val);
9739 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9740 * it's less than ts_recent, drop it.
9742 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9743 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9744 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9747 INP_WLOCK_ASSERT(tp->t_inpcb);
9748 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9752 * If new data are received on a connection after the user processes
9753 * are gone, then RST the other end.
9755 if ((so->so_state & SS_NOFDREF) && tlen) {
9757 * We call a new function now so we might continue and setup
9758 * to reset at all data being ack'd.
9760 if (bbr_check_data_after_close(m, bbr, tp, &tlen, th, so))
9764 * If last ACK falls within this segment's sequence numbers, record
9765 * its timestamp. NOTE: 1) That the test incorporates suggestions
9766 * from the latest proposal of the tcplw@cray.com list (Braden
9767 * 1993/04/26). 2) That updating only on newer timestamps interferes
9768 * with our earlier PAWS tests, so this check should be solely
9769 * predicated on the sequence space of this segment. 3) That we
9770 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9771 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9772 * SEG.Len, This modified check allows us to overcome RFC1323's
9773 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9774 * p.869. In such cases, we can still calculate the RTT correctly
9775 * when RCV.NXT == Last.ACK.Sent.
9777 if ((to->to_flags & TOF_TS) != 0 &&
9778 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9779 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9780 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9781 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9782 tp->ts_recent = to->to_tsval;
9785 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9786 * is on (half-synchronized state), then queue data for later
9787 * processing; else drop segment and return.
9789 if ((thflags & TH_ACK) == 0) {
9790 if (tp->t_flags & TF_NEEDSYN) {
9791 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9792 tiwin, thflags, nxt_pkt));
9793 } else if (tp->t_flags & TF_ACKNOW) {
9794 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9795 bbr->r_wanted_output = 1;
9798 ctf_do_drop(m, NULL);
9805 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9808 if (ourfinisacked) {
9813 if (sbavail(&so->so_snd)) {
9814 if (ctf_progress_timeout_check(tp, true)) {
9815 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9816 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9820 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9821 tiwin, thflags, nxt_pkt));
9825 * Return value of 1, the TCB is unlocked and most
9826 * likely gone, return value of 0, the TCB is still
9830 bbr_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
9831 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9832 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9834 int32_t ourfinisacked = 0;
9836 struct tcp_bbr *bbr;
9838 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9839 ctf_calc_rwin(so, tp);
9840 if ((thflags & TH_RST) ||
9841 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9842 return (ctf_process_rst(m, th, so, tp));
9844 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9845 * synchronized state.
9847 if (thflags & TH_SYN) {
9848 ctf_challenge_ack(m, th, tp, &ret_val);
9852 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9853 * it's less than ts_recent, drop it.
9855 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9856 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9857 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9860 INP_WLOCK_ASSERT(tp->t_inpcb);
9861 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9865 * If new data are received on a connection after the user processes
9866 * are gone, then RST the other end.
9868 if ((so->so_state & SS_NOFDREF) && tlen) {
9870 * We call a new function now so we might continue and setup
9871 * to reset at all data being ack'd.
9873 if (bbr_check_data_after_close(m, bbr, tp, &tlen, th, so))
9877 * If last ACK falls within this segment's sequence numbers, record
9878 * its timestamp. NOTE: 1) That the test incorporates suggestions
9879 * from the latest proposal of the tcplw@cray.com list (Braden
9880 * 1993/04/26). 2) That updating only on newer timestamps interferes
9881 * with our earlier PAWS tests, so this check should be solely
9882 * predicated on the sequence space of this segment. 3) That we
9883 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9884 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9885 * SEG.Len, This modified check allows us to overcome RFC1323's
9886 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9887 * p.869. In such cases, we can still calculate the RTT correctly
9888 * when RCV.NXT == Last.ACK.Sent.
9890 if ((to->to_flags & TOF_TS) != 0 &&
9891 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9892 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9893 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9894 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
9895 tp->ts_recent = to->to_tsval;
9898 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9899 * is on (half-synchronized state), then queue data for later
9900 * processing; else drop segment and return.
9902 if ((thflags & TH_ACK) == 0) {
9903 if (tp->t_flags & TF_NEEDSYN) {
9904 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9905 tiwin, thflags, nxt_pkt));
9906 } else if (tp->t_flags & TF_ACKNOW) {
9907 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9908 bbr->r_wanted_output = 1;
9911 ctf_do_drop(m, NULL);
9916 * case TCPS_LAST_ACK: Ack processing.
9918 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9921 if (ourfinisacked) {
9926 if (sbavail(&so->so_snd)) {
9927 if (ctf_progress_timeout_check(tp, true)) {
9928 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
9929 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9933 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
9934 tiwin, thflags, nxt_pkt));
9939 * Return value of 1, the TCB is unlocked and most
9940 * likely gone, return value of 0, the TCB is still
9944 bbr_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so,
9945 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9946 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9948 int32_t ourfinisacked = 0;
9950 struct tcp_bbr *bbr;
9952 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
9953 ctf_calc_rwin(so, tp);
9954 /* Reset receive buffer auto scaling when not in bulk receive mode. */
9955 if ((thflags & TH_RST) ||
9956 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9957 return (ctf_process_rst(m, th, so, tp));
9960 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9961 * synchronized state.
9963 if (thflags & TH_SYN) {
9964 ctf_challenge_ack(m, th, tp, &ret_val);
9967 INP_WLOCK_ASSERT(tp->t_inpcb);
9969 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9970 * it's less than ts_recent, drop it.
9972 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9973 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9974 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9977 INP_WLOCK_ASSERT(tp->t_inpcb);
9978 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9982 * If new data are received on a connection after the user processes
9983 * are gone, then we may RST the other end depending on the outcome
9984 * of bbr_check_data_after_close.
9986 if ((so->so_state & SS_NOFDREF) &&
9989 * We call a new function now so we might continue and setup
9990 * to reset at all data being ack'd.
9992 if (bbr_check_data_after_close(m, bbr, tp, &tlen, th, so))
9995 INP_WLOCK_ASSERT(tp->t_inpcb);
9997 * If last ACK falls within this segment's sequence numbers, record
9998 * its timestamp. NOTE: 1) That the test incorporates suggestions
9999 * from the latest proposal of the tcplw@cray.com list (Braden
10000 * 1993/04/26). 2) That updating only on newer timestamps interferes
10001 * with our earlier PAWS tests, so this check should be solely
10002 * predicated on the sequence space of this segment. 3) That we
10003 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
10004 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
10005 * SEG.Len, This modified check allows us to overcome RFC1323's
10006 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
10007 * p.869. In such cases, we can still calculate the RTT correctly
10008 * when RCV.NXT == Last.ACK.Sent.
10010 INP_WLOCK_ASSERT(tp->t_inpcb);
10011 if ((to->to_flags & TOF_TS) != 0 &&
10012 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
10013 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
10014 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
10015 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
10016 tp->ts_recent = to->to_tsval;
10019 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
10020 * is on (half-synchronized state), then queue data for later
10021 * processing; else drop segment and return.
10023 if ((thflags & TH_ACK) == 0) {
10024 if (tp->t_flags & TF_NEEDSYN) {
10025 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
10026 tiwin, thflags, nxt_pkt));
10027 } else if (tp->t_flags & TF_ACKNOW) {
10028 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
10029 bbr->r_wanted_output = 1;
10032 ctf_do_drop(m, NULL);
10039 INP_WLOCK_ASSERT(tp->t_inpcb);
10040 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
10043 if (sbavail(&so->so_snd)) {
10044 if (ctf_progress_timeout_check(tp, true)) {
10045 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__);
10046 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
10050 INP_WLOCK_ASSERT(tp->t_inpcb);
10051 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen,
10052 tiwin, thflags, nxt_pkt));
10056 bbr_stop_all_timers(struct tcpcb *tp)
10058 struct tcp_bbr *bbr;
10061 * Assure no timers are running.
10063 if (tcp_timer_active(tp, TT_PERSIST)) {
10064 /* We enter in persists, set the flag appropriately */
10065 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
10066 bbr->rc_in_persist = 1;
10068 tcp_timer_suspend(tp, TT_PERSIST);
10069 tcp_timer_suspend(tp, TT_REXMT);
10070 tcp_timer_suspend(tp, TT_KEEP);
10071 tcp_timer_suspend(tp, TT_DELACK);
10075 bbr_google_mode_on(struct tcp_bbr *bbr)
10077 bbr->rc_use_google = 1;
10078 bbr->rc_no_pacing = 0;
10079 bbr->r_ctl.bbr_google_discount = bbr_google_discount;
10080 bbr->r_use_policer = bbr_policer_detection_enabled;
10081 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10);
10082 bbr->bbr_use_rack_cheat = 0;
10083 bbr->r_ctl.rc_incr_tmrs = 0;
10084 bbr->r_ctl.rc_inc_tcp_oh = 0;
10085 bbr->r_ctl.rc_inc_ip_oh = 0;
10086 bbr->r_ctl.rc_inc_enet_oh = 0;
10087 reset_time(&bbr->r_ctl.rc_delrate,
10088 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT);
10089 reset_time_small(&bbr->r_ctl.rc_rttprop,
10090 (11 * USECS_IN_SECOND));
10091 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv));
10095 bbr_google_mode_off(struct tcp_bbr *bbr)
10097 bbr->rc_use_google = 0;
10098 bbr->r_ctl.bbr_google_discount = 0;
10099 bbr->no_pacing_until = bbr_no_pacing_until;
10100 bbr->r_use_policer = 0;
10101 if (bbr->no_pacing_until)
10102 bbr->rc_no_pacing = 1;
10104 bbr->rc_no_pacing = 0;
10105 if (bbr_use_rack_resend_cheat)
10106 bbr->bbr_use_rack_cheat = 1;
10108 bbr->bbr_use_rack_cheat = 0;
10109 if (bbr_incr_timers)
10110 bbr->r_ctl.rc_incr_tmrs = 1;
10112 bbr->r_ctl.rc_incr_tmrs = 0;
10113 if (bbr_include_tcp_oh)
10114 bbr->r_ctl.rc_inc_tcp_oh = 1;
10116 bbr->r_ctl.rc_inc_tcp_oh = 0;
10117 if (bbr_include_ip_oh)
10118 bbr->r_ctl.rc_inc_ip_oh = 1;
10120 bbr->r_ctl.rc_inc_ip_oh = 0;
10121 if (bbr_include_enet_oh)
10122 bbr->r_ctl.rc_inc_enet_oh = 1;
10124 bbr->r_ctl.rc_inc_enet_oh = 0;
10125 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit;
10126 reset_time(&bbr->r_ctl.rc_delrate,
10127 bbr_num_pktepo_for_del_limit);
10128 reset_time_small(&bbr->r_ctl.rc_rttprop,
10129 (bbr_filter_len_sec * USECS_IN_SECOND));
10130 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv));
10133 * Return 0 on success, non-zero on failure
10134 * which indicates the error (usually no memory).
10137 bbr_init(struct tcpcb *tp)
10139 struct tcp_bbr *bbr = NULL;
10143 tp->t_fb_ptr = uma_zalloc(bbr_pcb_zone, (M_NOWAIT | M_ZERO));
10144 if (tp->t_fb_ptr == NULL) {
10146 * We need to allocate memory but cant. The INP and INP_INFO
10147 * locks and they are recusive (happens during setup. So a
10148 * scheme to drop the locks fails :(
10153 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
10154 bbr->rtt_valid = 0;
10156 inp->inp_flags2 |= INP_CANNOT_DO_ECN;
10157 inp->inp_flags2 |= INP_SUPPORTS_MBUFQ;
10158 TAILQ_INIT(&bbr->r_ctl.rc_map);
10159 TAILQ_INIT(&bbr->r_ctl.rc_free);
10160 TAILQ_INIT(&bbr->r_ctl.rc_tmap);
10163 bbr->rc_inp = tp->t_inpcb;
10165 cts = tcp_get_usecs(&bbr->rc_tv);
10167 bbr->rc_allow_data_af_clo = bbr_ignore_data_after_close;
10168 bbr->r_ctl.rc_reorder_fade = bbr_reorder_fade;
10169 bbr->rc_tlp_threshold = bbr_tlp_thresh;
10170 bbr->r_ctl.rc_reorder_shift = bbr_reorder_thresh;
10171 bbr->r_ctl.rc_pkt_delay = bbr_pkt_delay;
10172 bbr->r_ctl.rc_min_to = bbr_min_to;
10173 bbr->rc_bbr_state = BBR_STATE_STARTUP;
10174 bbr->r_ctl.bbr_lost_at_state = 0;
10175 bbr->r_ctl.rc_lost_at_startup = 0;
10176 bbr->rc_all_timers_stopped = 0;
10177 bbr->r_ctl.rc_bbr_lastbtlbw = 0;
10178 bbr->r_ctl.rc_pkt_epoch_del = 0;
10179 bbr->r_ctl.rc_pkt_epoch = 0;
10180 bbr->r_ctl.rc_lowest_rtt = 0xffffffff;
10181 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_high_gain;
10182 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain;
10183 bbr->r_ctl.rc_went_idle_time = cts;
10184 bbr->rc_pacer_started = cts;
10185 bbr->r_ctl.rc_pkt_epoch_time = cts;
10186 bbr->r_ctl.rc_rcvtime = cts;
10187 bbr->r_ctl.rc_bbr_state_time = cts;
10188 bbr->r_ctl.rc_del_time = cts;
10189 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
10190 bbr->r_ctl.last_in_probertt = cts;
10191 bbr->skip_gain = 0;
10192 bbr->gain_is_limited = 0;
10193 bbr->no_pacing_until = bbr_no_pacing_until;
10194 if (bbr->no_pacing_until)
10195 bbr->rc_no_pacing = 1;
10196 if (bbr_use_google_algo) {
10197 bbr->rc_no_pacing = 0;
10198 bbr->rc_use_google = 1;
10199 bbr->r_ctl.bbr_google_discount = bbr_google_discount;
10200 bbr->r_use_policer = bbr_policer_detection_enabled;
10202 bbr->rc_use_google = 0;
10203 bbr->r_ctl.bbr_google_discount = 0;
10204 bbr->r_use_policer = 0;
10206 if (bbr_ts_limiting)
10207 bbr->rc_use_ts_limit = 1;
10209 bbr->rc_use_ts_limit = 0;
10210 if (bbr_ts_can_raise)
10211 bbr->ts_can_raise = 1;
10213 bbr->ts_can_raise = 0;
10214 if (V_tcp_delack_enabled == 1)
10215 tp->t_delayed_ack = 2;
10216 else if (V_tcp_delack_enabled == 0)
10217 tp->t_delayed_ack = 0;
10218 else if (V_tcp_delack_enabled < 100)
10219 tp->t_delayed_ack = V_tcp_delack_enabled;
10221 tp->t_delayed_ack = 2;
10222 if (bbr->rc_use_google == 0)
10223 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit;
10225 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10);
10226 bbr->r_ctl.rc_min_rto_ms = bbr_rto_min_ms;
10227 bbr->rc_max_rto_sec = bbr_rto_max_sec;
10228 bbr->rc_init_win = bbr_def_init_win;
10229 if (tp->t_flags & TF_REQ_TSTMP)
10230 bbr->rc_last_options = TCP_TS_OVERHEAD;
10231 bbr->r_ctl.rc_pace_max_segs = tp->t_maxseg - bbr->rc_last_options;
10232 bbr->r_ctl.rc_high_rwnd = tp->snd_wnd;
10233 bbr->r_init_rtt = 1;
10235 counter_u64_add(bbr_flows_nohdwr_pacing, 1);
10236 if (bbr_allow_hdwr_pacing)
10237 bbr->bbr_hdw_pace_ena = 1;
10239 bbr->bbr_hdw_pace_ena = 0;
10240 if (bbr_sends_full_iwnd)
10241 bbr->bbr_init_win_cheat = 1;
10243 bbr->bbr_init_win_cheat = 0;
10244 bbr->r_ctl.bbr_utter_max = bbr_hptsi_utter_max;
10245 bbr->r_ctl.rc_drain_pg = bbr_drain_gain;
10246 bbr->r_ctl.rc_startup_pg = bbr_high_gain;
10247 bbr->rc_loss_exit = bbr_exit_startup_at_loss;
10248 bbr->r_ctl.bbr_rttprobe_gain_val = bbr_rttprobe_gain;
10249 bbr->r_ctl.bbr_hptsi_per_second = bbr_hptsi_per_second;
10250 bbr->r_ctl.bbr_hptsi_segments_delay_tar = bbr_hptsi_segments_delay_tar;
10251 bbr->r_ctl.bbr_hptsi_segments_max = bbr_hptsi_segments_max;
10252 bbr->r_ctl.bbr_hptsi_segments_floor = bbr_hptsi_segments_floor;
10253 bbr->r_ctl.bbr_hptsi_bytes_min = bbr_hptsi_bytes_min;
10254 bbr->r_ctl.bbr_cross_over = bbr_cross_over;
10255 bbr->r_ctl.rc_rtt_shrinks = cts;
10256 if (bbr->rc_use_google) {
10257 setup_time_filter(&bbr->r_ctl.rc_delrate,
10259 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT);
10260 setup_time_filter_small(&bbr->r_ctl.rc_rttprop,
10261 FILTER_TYPE_MIN, (11 * USECS_IN_SECOND));
10263 setup_time_filter(&bbr->r_ctl.rc_delrate,
10265 bbr_num_pktepo_for_del_limit);
10266 setup_time_filter_small(&bbr->r_ctl.rc_rttprop,
10267 FILTER_TYPE_MIN, (bbr_filter_len_sec * USECS_IN_SECOND));
10269 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_INIT, 0);
10270 if (bbr_uses_idle_restart)
10271 bbr->rc_use_idle_restart = 1;
10273 bbr->rc_use_idle_restart = 0;
10274 bbr->r_ctl.rc_bbr_cur_del_rate = 0;
10275 bbr->r_ctl.rc_initial_hptsi_bw = bbr_initial_bw_bps;
10276 if (bbr_resends_use_tso)
10277 bbr->rc_resends_use_tso = 1;
10278 #ifdef NETFLIX_PEAKRATE
10279 tp->t_peakrate_thr = tp->t_maxpeakrate;
10281 if (tp->snd_una != tp->snd_max) {
10282 /* Create a send map for the current outstanding data */
10283 struct bbr_sendmap *rsm;
10285 rsm = bbr_alloc(bbr);
10287 uma_zfree(bbr_pcb_zone, tp->t_fb_ptr);
10288 tp->t_fb_ptr = NULL;
10291 rsm->r_flags = BBR_OVERMAX;
10292 rsm->r_tim_lastsent[0] = cts;
10293 rsm->r_rtr_cnt = 1;
10294 rsm->r_rtr_bytes = 0;
10295 rsm->r_start = tp->snd_una;
10296 rsm->r_end = tp->snd_max;
10298 rsm->r_delivered = bbr->r_ctl.rc_delivered;
10299 rsm->r_ts_valid = 0;
10300 rsm->r_del_ack_ts = tp->ts_recent;
10301 rsm->r_del_time = cts;
10302 if (bbr->r_ctl.r_app_limited_until)
10303 rsm->r_app_limited = 1;
10305 rsm->r_app_limited = 0;
10306 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next);
10307 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext);
10308 rsm->r_in_tmap = 1;
10309 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW)
10310 rsm->r_bbr_state = bbr_state_val(bbr);
10312 rsm->r_bbr_state = 8;
10314 if (bbr_use_rack_resend_cheat && (bbr->rc_use_google == 0))
10315 bbr->bbr_use_rack_cheat = 1;
10316 if (bbr_incr_timers && (bbr->rc_use_google == 0))
10317 bbr->r_ctl.rc_incr_tmrs = 1;
10318 if (bbr_include_tcp_oh && (bbr->rc_use_google == 0))
10319 bbr->r_ctl.rc_inc_tcp_oh = 1;
10320 if (bbr_include_ip_oh && (bbr->rc_use_google == 0))
10321 bbr->r_ctl.rc_inc_ip_oh = 1;
10322 if (bbr_include_enet_oh && (bbr->rc_use_google == 0))
10323 bbr->r_ctl.rc_inc_enet_oh = 1;
10325 bbr_log_type_statechange(bbr, cts, __LINE__);
10326 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
10330 rtt = (TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT);
10331 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts);
10333 /* announce the settings and state */
10334 bbr_log_settings_change(bbr, BBR_RECOVERY_LOWRTT);
10335 tcp_bbr_tso_size_check(bbr, cts);
10337 * Now call the generic function to start a timer. This will place
10338 * the TCB on the hptsi wheel if a timer is needed with appropriate
10341 bbr_stop_all_timers(tp);
10342 bbr_start_hpts_timer(bbr, tp, cts, 5, 0, 0);
10347 * Return 0 if we can accept the connection. Return
10348 * non-zero if we can't handle the connection. A EAGAIN
10349 * means you need to wait until the connection is up.
10350 * a EADDRNOTAVAIL means we can never handle the connection
10354 bbr_handoff_ok(struct tcpcb *tp)
10356 if ((tp->t_state == TCPS_CLOSED) ||
10357 (tp->t_state == TCPS_LISTEN)) {
10358 /* Sure no problem though it may not stick */
10361 if ((tp->t_state == TCPS_SYN_SENT) ||
10362 (tp->t_state == TCPS_SYN_RECEIVED)) {
10364 * We really don't know you have to get to ESTAB or beyond
10369 if ((tp->t_flags & TF_SACK_PERMIT) || bbr_sack_not_required) {
10373 * If we reach here we don't do SACK on this connection so we can
10380 bbr_fini(struct tcpcb *tp, int32_t tcb_is_purged)
10382 if (tp->t_fb_ptr) {
10384 struct tcp_bbr *bbr;
10385 struct bbr_sendmap *rsm;
10387 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
10388 if (bbr->r_ctl.crte)
10389 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp);
10390 bbr_log_flowend(bbr);
10393 /* Backout any flags2 we applied */
10394 tp->t_inpcb->inp_flags2 &= ~INP_CANNOT_DO_ECN;
10395 tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
10396 tp->t_inpcb->inp_flags2 &= ~INP_MBUF_QUEUE_READY;
10398 if (bbr->bbr_hdrw_pacing)
10399 counter_u64_add(bbr_flows_whdwr_pacing, -1);
10401 counter_u64_add(bbr_flows_nohdwr_pacing, -1);
10402 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
10404 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next);
10405 uma_zfree(bbr_zone, rsm);
10406 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
10408 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free);
10410 TAILQ_REMOVE(&bbr->r_ctl.rc_free, rsm, r_next);
10411 uma_zfree(bbr_zone, rsm);
10412 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free);
10414 calc = bbr->r_ctl.rc_high_rwnd - bbr->r_ctl.rc_init_rwnd;
10415 if (calc > (bbr->r_ctl.rc_init_rwnd / 10))
10416 BBR_STAT_INC(bbr_dynamic_rwnd);
10418 BBR_STAT_INC(bbr_static_rwnd);
10419 bbr->r_ctl.rc_free_cnt = 0;
10420 uma_zfree(bbr_pcb_zone, tp->t_fb_ptr);
10421 tp->t_fb_ptr = NULL;
10423 /* Make sure snd_nxt is correctly set */
10424 tp->snd_nxt = tp->snd_max;
10428 bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win)
10430 switch (tp->t_state) {
10431 case TCPS_SYN_SENT:
10432 bbr->r_state = TCPS_SYN_SENT;
10433 bbr->r_substate = bbr_do_syn_sent;
10435 case TCPS_SYN_RECEIVED:
10436 bbr->r_state = TCPS_SYN_RECEIVED;
10437 bbr->r_substate = bbr_do_syn_recv;
10439 case TCPS_ESTABLISHED:
10440 bbr->r_ctl.rc_init_rwnd = max(win, bbr->rc_tp->snd_wnd);
10441 bbr->r_state = TCPS_ESTABLISHED;
10442 bbr->r_substate = bbr_do_established;
10444 case TCPS_CLOSE_WAIT:
10445 bbr->r_state = TCPS_CLOSE_WAIT;
10446 bbr->r_substate = bbr_do_close_wait;
10448 case TCPS_FIN_WAIT_1:
10449 bbr->r_state = TCPS_FIN_WAIT_1;
10450 bbr->r_substate = bbr_do_fin_wait_1;
10453 bbr->r_state = TCPS_CLOSING;
10454 bbr->r_substate = bbr_do_closing;
10456 case TCPS_LAST_ACK:
10457 bbr->r_state = TCPS_LAST_ACK;
10458 bbr->r_substate = bbr_do_lastack;
10460 case TCPS_FIN_WAIT_2:
10461 bbr->r_state = TCPS_FIN_WAIT_2;
10462 bbr->r_substate = bbr_do_fin_wait_2;
10466 case TCPS_TIME_WAIT:
10473 bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int32_t line, int dolog)
10476 * Now what state are we going into now? Is there adjustments
10479 int32_t old_state, old_gain;
10482 old_state = bbr_state_val(bbr);
10483 old_gain = bbr->r_ctl.rc_bbr_hptsi_gain;
10484 if (bbr_state_val(bbr) == BBR_SUB_LEVEL1) {
10485 /* Save the lowest srtt we saw in our end of the sub-state */
10486 bbr->rc_hit_state_1 = 0;
10487 if (bbr->r_ctl.bbr_smallest_srtt_this_state != 0xffffffff)
10488 bbr->r_ctl.bbr_smallest_srtt_state2 = bbr->r_ctl.bbr_smallest_srtt_this_state;
10490 bbr->rc_bbr_substate++;
10491 if (bbr->rc_bbr_substate >= BBR_SUBSTATE_COUNT) {
10492 /* Cycle back to first state-> gain */
10493 bbr->rc_bbr_substate = 0;
10495 if (bbr_state_val(bbr) == BBR_SUB_GAIN) {
10497 * We enter the gain(5/4) cycle (possibly less if
10498 * shallow buffer detection is enabled)
10500 if (bbr->skip_gain) {
10502 * Hardware pacing has set our rate to
10503 * the max and limited our b/w just
10504 * do level i.e. no gain.
10506 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_LEVEL1];
10507 } else if (bbr->gain_is_limited &&
10508 bbr->bbr_hdrw_pacing &&
10511 * We can't gain above the hardware pacing
10512 * rate which is less than our rate + the gain
10513 * calculate the gain needed to reach the hardware
10516 uint64_t bw, rate, gain_calc;
10518 bw = bbr_get_bw(bbr);
10519 rate = bbr->r_ctl.crte->rate;
10521 (((bw * (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN]) / (uint64_t)BBR_UNIT) > rate)) {
10522 gain_calc = (rate * BBR_UNIT) / bw;
10523 if (gain_calc < BBR_UNIT)
10524 gain_calc = BBR_UNIT;
10525 bbr->r_ctl.rc_bbr_hptsi_gain = (uint16_t)gain_calc;
10527 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN];
10530 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN];
10531 if ((bbr->rc_use_google == 0) && (bbr_gain_to_target == 0)) {
10532 bbr->r_ctl.rc_bbr_state_atflight = cts;
10534 bbr->r_ctl.rc_bbr_state_atflight = 0;
10535 } else if (bbr_state_val(bbr) == BBR_SUB_DRAIN) {
10536 bbr->rc_hit_state_1 = 1;
10537 bbr->r_ctl.rc_exta_time_gd = 0;
10538 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp,
10539 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
10540 if (bbr_state_drain_2_tar) {
10541 bbr->r_ctl.rc_bbr_state_atflight = 0;
10543 bbr->r_ctl.rc_bbr_state_atflight = cts;
10544 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_DRAIN];
10546 /* All other cycles hit here 2-7 */
10547 if ((old_state == BBR_SUB_DRAIN) && bbr->rc_hit_state_1) {
10548 if (bbr_sub_drain_slam_cwnd &&
10549 (bbr->rc_use_google == 0) &&
10550 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) {
10551 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
10552 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10554 if ((cts - bbr->r_ctl.rc_bbr_state_time) > bbr_get_rtt(bbr, BBR_RTT_PROP))
10555 bbr->r_ctl.rc_exta_time_gd += ((cts - bbr->r_ctl.rc_bbr_state_time) -
10556 bbr_get_rtt(bbr, BBR_RTT_PROP));
10558 bbr->r_ctl.rc_exta_time_gd = 0;
10559 if (bbr->r_ctl.rc_exta_time_gd) {
10560 bbr->r_ctl.rc_level_state_extra = bbr->r_ctl.rc_exta_time_gd;
10561 /* Now chop up the time for each state (div by 7) */
10562 bbr->r_ctl.rc_level_state_extra /= 7;
10563 if (bbr_rand_ot && bbr->r_ctl.rc_level_state_extra) {
10564 /* Add a randomization */
10565 bbr_randomize_extra_state_time(bbr);
10569 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts);
10570 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[bbr_state_val(bbr)];
10572 if (bbr->rc_use_google) {
10573 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts);
10575 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
10576 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain;
10578 bbr_log_type_statechange(bbr, cts, line);
10580 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
10583 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
10584 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) {
10585 counter_u64_add(bbr_state_time[(old_state + 5)], time_in);
10587 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
10590 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff;
10591 bbr_set_state_target(bbr, __LINE__);
10592 if (bbr_sub_drain_slam_cwnd &&
10593 (bbr->rc_use_google == 0) &&
10594 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) {
10595 /* Slam down the cwnd */
10596 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
10597 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
10598 if (bbr_sub_drain_app_limit) {
10599 /* Go app limited if we are on a long drain */
10600 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered +
10601 ctf_flight_size(bbr->rc_tp,
10602 (bbr->r_ctl.rc_sacked +
10603 bbr->r_ctl.rc_lost_bytes)));
10605 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10607 if (bbr->rc_lt_use_bw) {
10608 /* In policed mode we clamp pacing_gain to BBR_UNIT */
10609 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
10611 /* Google changes TSO size every cycle */
10612 if (bbr->rc_use_google)
10613 tcp_bbr_tso_size_check(bbr, cts);
10614 bbr->r_ctl.gain_epoch = cts;
10615 bbr->r_ctl.rc_bbr_state_time = cts;
10616 bbr->r_ctl.substate_pe = bbr->r_ctl.rc_pkt_epoch;
10620 bbr_set_probebw_google_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses)
10622 if ((bbr_state_val(bbr) == BBR_SUB_DRAIN) &&
10623 (google_allow_early_out == 1) &&
10624 (bbr->r_ctl.rc_flight_at_input <= bbr->r_ctl.rc_target_at_state)) {
10625 /* We have reached out target flight size possibly early */
10628 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time)) {
10631 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_get_rtt(bbr, BBR_RTT_PROP)) {
10633 * Must be a rttProp movement forward before
10634 * we can change states.
10638 if (bbr_state_val(bbr) == BBR_SUB_GAIN) {
10640 * The needed time has passed but for
10641 * the gain cycle extra rules apply:
10642 * 1) If we have seen loss, we exit
10643 * 2) If we have not reached the target
10644 * we stay in GAIN (gain-to-target).
10646 if (google_consider_lost && losses)
10648 if (bbr->r_ctl.rc_target_at_state > bbr->r_ctl.rc_flight_at_input) {
10653 /* For gain we must reach our target, all others last 1 rttProp */
10654 bbr_substate_change(bbr, cts, __LINE__, 1);
10658 bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses)
10660 uint32_t flight, bbr_cur_cycle_time;
10662 if (bbr->rc_use_google) {
10663 bbr_set_probebw_google_gains(bbr, cts, losses);
10668 * Never alow cts to be 0 we
10669 * do this so we can judge if
10670 * we have set a timestamp.
10674 if (bbr_state_is_pkt_epoch)
10675 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PKTRTT);
10677 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PROP);
10679 if (bbr->r_ctl.rc_bbr_state_atflight == 0) {
10680 if (bbr_state_val(bbr) == BBR_SUB_DRAIN) {
10681 flight = ctf_flight_size(bbr->rc_tp,
10682 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
10683 if (bbr_sub_drain_slam_cwnd && bbr->rc_hit_state_1) {
10684 /* Keep it slam down */
10685 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state) {
10686 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
10687 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10689 if (bbr_sub_drain_app_limit) {
10690 /* Go app limited if we are on a long drain */
10691 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered + flight);
10694 if (TSTMP_GT(cts, bbr->r_ctl.gain_epoch) &&
10695 (((cts - bbr->r_ctl.gain_epoch) > bbr_get_rtt(bbr, BBR_RTT_PROP)) ||
10696 (flight >= bbr->r_ctl.flightsize_at_drain))) {
10698 * Still here after the same time as
10699 * the gain. We need to drain harder
10700 * for the next srtt. Reduce by a set amount
10701 * the gain drop is capped at DRAIN states
10704 bbr->r_ctl.flightsize_at_drain = flight;
10705 if (bbr_drain_drop_mul &&
10706 bbr_drain_drop_div &&
10707 (bbr_drain_drop_mul < bbr_drain_drop_div)) {
10708 /* Use your specific drop value (def 4/5 = 20%) */
10709 bbr->r_ctl.rc_bbr_hptsi_gain *= bbr_drain_drop_mul;
10710 bbr->r_ctl.rc_bbr_hptsi_gain /= bbr_drain_drop_div;
10712 /* You get drop of 20% */
10713 bbr->r_ctl.rc_bbr_hptsi_gain *= 4;
10714 bbr->r_ctl.rc_bbr_hptsi_gain /= 5;
10716 if (bbr->r_ctl.rc_bbr_hptsi_gain <= bbr_drain_floor) {
10717 /* Reduce our gain again to the bottom */
10718 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1);
10720 bbr_log_exit_gain(bbr, cts, 4);
10722 * Extend out so we wait another
10723 * epoch before dropping again.
10725 bbr->r_ctl.gain_epoch = cts;
10727 if (flight <= bbr->r_ctl.rc_target_at_state) {
10728 if (bbr_sub_drain_slam_cwnd &&
10729 (bbr->rc_use_google == 0) &&
10730 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) {
10731 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
10732 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10734 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1);
10735 bbr_log_exit_gain(bbr, cts, 3);
10739 if (bbr->r_ctl.rc_lost > bbr->r_ctl.bbr_lost_at_state) {
10740 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1);
10743 if ((ctf_outstanding(bbr->rc_tp) >= bbr->r_ctl.rc_target_at_state) ||
10744 ((ctf_outstanding(bbr->rc_tp) + bbr->rc_tp->t_maxseg - 1) >=
10745 bbr->rc_tp->snd_wnd)) {
10746 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1);
10747 bbr_log_exit_gain(bbr, cts, 2);
10751 * We fall through and return always one of two things has
10753 * 1) We are still not at target
10755 * 2) We reached the target and set rc_bbr_state_atflight
10756 * which means we no longer hit this block
10757 * next time we are called.
10762 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time))
10764 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_cur_cycle_time) {
10765 /* Less than a full time-period has passed */
10768 if (bbr->r_ctl.rc_level_state_extra &&
10769 (bbr_state_val(bbr) > BBR_SUB_DRAIN) &&
10770 ((cts - bbr->r_ctl.rc_bbr_state_time) <
10771 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) {
10772 /* Less than a full time-period + extra has passed */
10775 if (bbr_gain_gets_extra_too &&
10776 bbr->r_ctl.rc_level_state_extra &&
10777 (bbr_state_val(bbr) == BBR_SUB_GAIN) &&
10778 ((cts - bbr->r_ctl.rc_bbr_state_time) <
10779 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) {
10780 /* Less than a full time-period + extra has passed */
10783 bbr_substate_change(bbr, cts, __LINE__, 1);
10787 bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain)
10791 if (bbr->rc_use_google) {
10792 /* Google just uses the cwnd target */
10793 tar = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), gain);
10795 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options),
10796 bbr->r_ctl.rc_pace_max_segs);
10797 /* Get the base cwnd with gain rounded to a mss */
10798 tar = roundup(bbr_get_raw_target_cwnd(bbr, bbr_get_bw(bbr),
10800 /* Make sure it is within our min */
10801 if (tar < get_min_cwnd(bbr))
10802 return (get_min_cwnd(bbr));
10808 bbr_set_state_target(struct tcp_bbr *bbr, int line)
10810 uint32_t tar, meth;
10812 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) &&
10813 ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google)) {
10814 /* Special case using old probe-rtt method */
10815 tar = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options);
10818 /* Non-probe-rtt case and reduced probe-rtt */
10819 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) &&
10820 (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT)) {
10821 /* For gain cycle we use the hptsi gain */
10822 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain);
10824 } else if ((bbr_target_is_bbunit) || bbr->rc_use_google) {
10826 * If configured, or for google all other states
10829 tar = bbr_get_a_state_target(bbr, BBR_UNIT);
10833 * Or we set a target based on the pacing gain
10834 * for non-google mode and default (non-configured).
10835 * Note we don't set a target goal below drain (192).
10837 if (bbr->r_ctl.rc_bbr_hptsi_gain < bbr_hptsi_gain[BBR_SUB_DRAIN]) {
10838 tar = bbr_get_a_state_target(bbr, bbr_hptsi_gain[BBR_SUB_DRAIN]);
10841 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain);
10846 bbr_log_set_of_state_target(bbr, tar, line, meth);
10847 bbr->r_ctl.rc_target_at_state = tar;
10851 bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line)
10853 /* Change to probe_rtt */
10856 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
10857 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp,
10858 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
10859 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.flightsize_at_drain
10860 + bbr->r_ctl.rc_delivered);
10861 /* Setup so we force feed the filter */
10862 if (bbr->rc_use_google || bbr_probertt_sets_rtt)
10863 bbr->rc_prtt_set_ts = 1;
10864 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
10865 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
10866 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
10868 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_ENTERPROBE, 0);
10869 bbr->r_ctl.rc_rtt_shrinks = cts;
10870 bbr->r_ctl.last_in_probertt = cts;
10871 bbr->r_ctl.rc_probertt_srttchktim = cts;
10872 bbr->r_ctl.rc_bbr_state_time = cts;
10873 bbr->rc_bbr_state = BBR_STATE_PROBE_RTT;
10874 /* We need to force the filter to update */
10876 if ((bbr_sub_drain_slam_cwnd) &&
10877 bbr->rc_hit_state_1 &&
10878 (bbr->rc_use_google == 0) &&
10879 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) {
10880 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_saved_cwnd)
10881 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
10883 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
10884 /* Update the lost */
10885 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
10886 if ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google){
10887 /* Set to the non-configurable default of 4 (PROBE_RTT_MIN) */
10888 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options);
10889 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10890 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
10891 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
10892 bbr_log_set_of_state_target(bbr, bbr->rc_tp->snd_cwnd, __LINE__, 6);
10893 bbr->r_ctl.rc_target_at_state = bbr->rc_tp->snd_cwnd;
10896 * We bring it down slowly by using a hptsi gain that is
10897 * probably 75%. This will slowly float down our outstanding
10898 * without tampering with the cwnd.
10900 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val;
10901 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
10902 bbr_set_state_target(bbr, __LINE__);
10903 if (bbr_prtt_slam_cwnd &&
10904 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
10905 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
10906 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
10909 if (ctf_flight_size(bbr->rc_tp,
10910 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <=
10911 bbr->r_ctl.rc_target_at_state) {
10912 /* We are at target */
10913 bbr->r_ctl.rc_bbr_enters_probertt = cts;
10915 /* We need to come down to reach target before our time begins */
10916 bbr->r_ctl.rc_bbr_enters_probertt = 0;
10918 bbr->r_ctl.rc_pe_of_prtt = bbr->r_ctl.rc_pkt_epoch;
10919 BBR_STAT_INC(bbr_enter_probertt);
10920 bbr_log_exit_gain(bbr, cts, 0);
10921 bbr_log_type_statechange(bbr, cts, line);
10925 bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts)
10928 * Sanity check on probe-rtt intervals.
10929 * In crazy situations where we are competing
10930 * against new-reno flows with huge buffers
10931 * our rtt-prop interval could come to dominate
10932 * things if we can't get through a full set
10933 * of cycles, we need to adjust it.
10935 if (bbr_can_adjust_probertt &&
10936 (bbr->rc_use_google == 0)) {
10938 uint32_t cur_rttp, fval, newval, baseval;
10940 /* Are we to small and go into probe-rtt to often? */
10941 baseval = (bbr_get_rtt(bbr, BBR_RTT_PROP) * (BBR_SUBSTATE_COUNT + 1));
10942 cur_rttp = roundup(baseval, USECS_IN_SECOND);
10943 fval = bbr_filter_len_sec * USECS_IN_SECOND;
10944 if (bbr_is_ratio == 0) {
10945 if (fval > bbr_rtt_probe_limit)
10946 newval = cur_rttp + (fval - bbr_rtt_probe_limit);
10952 mul = fval / bbr_rtt_probe_limit;
10953 newval = cur_rttp * mul;
10955 if (cur_rttp > bbr->r_ctl.rc_probertt_int) {
10956 bbr->r_ctl.rc_probertt_int = cur_rttp;
10957 reset_time_small(&bbr->r_ctl.rc_rttprop, newval);
10961 * No adjustments were made
10962 * do we need to shrink it?
10964 if (bbr->r_ctl.rc_probertt_int > bbr_rtt_probe_limit) {
10965 if (cur_rttp <= bbr_rtt_probe_limit) {
10967 * Things have calmed down lets
10968 * shrink all the way to default
10970 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit;
10971 reset_time_small(&bbr->r_ctl.rc_rttprop,
10972 (bbr_filter_len_sec * USECS_IN_SECOND));
10973 cur_rttp = bbr_rtt_probe_limit;
10974 newval = (bbr_filter_len_sec * USECS_IN_SECOND);
10978 * Well does some adjustment make sense?
10980 if (cur_rttp < bbr->r_ctl.rc_probertt_int) {
10981 /* We can reduce interval time some */
10982 bbr->r_ctl.rc_probertt_int = cur_rttp;
10983 reset_time_small(&bbr->r_ctl.rc_rttprop, newval);
10990 bbr_log_rtt_shrinks(bbr, cts, cur_rttp, newval, __LINE__, BBR_RTTS_RESETS_VALUES, val);
10995 bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts)
10997 /* Exit probe-rtt */
10999 if (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd) {
11000 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
11001 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11003 bbr_log_exit_gain(bbr, cts, 1);
11004 bbr->rc_hit_state_1 = 0;
11005 bbr->r_ctl.rc_rtt_shrinks = cts;
11006 bbr->r_ctl.last_in_probertt = cts;
11007 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_RTTPROBE, 0);
11008 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
11009 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp,
11010 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) +
11011 bbr->r_ctl.rc_delivered);
11012 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
11015 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
11016 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
11018 if (bbr->rc_filled_pipe) {
11019 /* Switch to probe_bw */
11020 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
11021 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts);
11022 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain;
11023 bbr_substate_change(bbr, cts, __LINE__, 0);
11024 bbr_log_type_statechange(bbr, cts, __LINE__);
11026 /* Back to startup */
11027 bbr->rc_bbr_state = BBR_STATE_STARTUP;
11028 bbr->r_ctl.rc_bbr_state_time = cts;
11030 * We don't want to give a complete free 3
11031 * measurements until we exit, so we use
11032 * the number of pe's we were in probe-rtt
11033 * to add to the startup_epoch. That way
11034 * we will still retain the old state.
11036 bbr->r_ctl.rc_bbr_last_startup_epoch += (bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_pe_of_prtt);
11037 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11038 /* Make sure to use the lower pg when shifting back in */
11039 if (bbr->r_ctl.rc_lost &&
11040 bbr_use_lower_gain_in_startup &&
11041 (bbr->rc_use_google == 0))
11042 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower;
11044 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg;
11045 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg;
11046 /* Probably not needed but set it anyway */
11047 bbr_set_state_target(bbr, __LINE__);
11048 bbr_log_type_statechange(bbr, cts, __LINE__);
11049 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11050 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 0);
11052 bbr_check_probe_rtt_limits(bbr, cts);
11055 static int32_t inline
11056 bbr_should_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts)
11058 if ((bbr->rc_past_init_win == 1) &&
11059 (bbr->rc_in_persist == 0) &&
11060 (bbr_calc_time(cts, bbr->r_ctl.rc_rtt_shrinks) >= bbr->r_ctl.rc_probertt_int)) {
11063 if (bbr_can_force_probertt &&
11064 (bbr->rc_in_persist == 0) &&
11065 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) &&
11066 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) {
11074 bbr_google_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t pkt_epoch)
11076 uint64_t btlbw, gain;
11077 if (pkt_epoch == 0) {
11079 * Need to be on a pkt-epoch to continue.
11083 btlbw = bbr_get_full_bw(bbr);
11084 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw *
11085 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw;
11086 if (btlbw >= gain) {
11087 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch;
11088 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11089 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 3);
11090 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw;
11092 if ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS)
11094 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11095 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8);
11099 static int32_t inline
11100 bbr_state_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch)
11102 /* Have we gained 25% in the last 3 packet based epoch's? */
11103 uint64_t btlbw, gain;
11105 int delta, rtt_gain;
11107 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) &&
11108 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) {
11110 * This qualifies as a RTT_PROBE session since we drop the
11111 * data outstanding to nothing and waited more than
11112 * bbr_rtt_probe_time.
11114 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0);
11115 bbr_set_reduced_rtt(bbr, cts, __LINE__);
11117 if (bbr_should_enter_probe_rtt(bbr, cts)) {
11118 bbr_enter_probe_rtt(bbr, cts, __LINE__);
11121 if (bbr->rc_use_google)
11122 return (bbr_google_startup(bbr, cts, pkt_epoch));
11124 if ((bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) &&
11125 (bbr_use_lower_gain_in_startup)) {
11126 /* Drop to a lower gain 1.5 x since we saw loss */
11127 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower;
11129 if (pkt_epoch == 0) {
11131 * Need to be on a pkt-epoch to continue.
11135 if (bbr_rtt_gain_thresh) {
11137 * Do we allow a flow to stay
11138 * in startup with no loss and no
11139 * gain in rtt over a set threshold?
11141 if (bbr->r_ctl.rc_pkt_epoch_rtt &&
11142 bbr->r_ctl.startup_last_srtt &&
11143 (bbr->r_ctl.rc_pkt_epoch_rtt > bbr->r_ctl.startup_last_srtt)) {
11144 delta = bbr->r_ctl.rc_pkt_epoch_rtt - bbr->r_ctl.startup_last_srtt;
11145 rtt_gain = (delta * 100) / bbr->r_ctl.startup_last_srtt;
11148 if ((bbr->r_ctl.startup_last_srtt == 0) ||
11149 (bbr->r_ctl.rc_pkt_epoch_rtt < bbr->r_ctl.startup_last_srtt))
11150 /* First time or new lower value */
11151 bbr->r_ctl.startup_last_srtt = bbr->r_ctl.rc_pkt_epoch_rtt;
11153 if ((bbr->r_ctl.rc_lost == 0) &&
11154 (rtt_gain < bbr_rtt_gain_thresh)) {
11156 * No loss, and we are under
11157 * our gain threhold for
11160 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch)
11161 bbr->r_ctl.rc_bbr_last_startup_epoch++;
11162 bbr_log_startup_event(bbr, cts, rtt_gain,
11163 delta, bbr->r_ctl.startup_last_srtt, 10);
11167 if ((bbr->r_ctl.r_measurement_count == bbr->r_ctl.last_startup_measure) &&
11168 (bbr->r_ctl.rc_lost_at_startup == bbr->r_ctl.rc_lost) &&
11169 (!IN_RECOVERY(bbr->rc_tp->t_flags))) {
11171 * We only assess if we have a new measurment when
11172 * we have no loss and are not in recovery.
11173 * Drag up by one our last_startup epoch so we will hold
11174 * the number of non-gain we have already accumulated.
11176 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch)
11177 bbr->r_ctl.rc_bbr_last_startup_epoch++;
11178 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11179 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 9);
11182 /* Case where we reduced the lost (bad retransmit) */
11183 if (bbr->r_ctl.rc_lost_at_startup > bbr->r_ctl.rc_lost)
11184 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11185 bbr->r_ctl.last_startup_measure = bbr->r_ctl.r_measurement_count;
11186 btlbw = bbr_get_full_bw(bbr);
11187 if (bbr->r_ctl.rc_bbr_hptsi_gain == bbr_startup_lower)
11188 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw *
11189 (uint64_t)bbr_low_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw;
11191 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw *
11192 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw;
11194 if (btlbw > bbr->r_ctl.rc_bbr_lastbtlbw)
11195 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw;
11196 if (btlbw >= gain) {
11197 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch;
11198 /* Update the lost so we won't exit in next set of tests */
11199 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11200 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11201 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 3);
11203 if ((bbr->rc_loss_exit &&
11204 (bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) &&
11205 (bbr->r_ctl.rc_pkt_epoch_loss_rate > bbr_startup_loss_thresh)) &&
11206 ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS)) {
11208 * If we had no gain, we had loss and that loss was above
11209 * our threshould, the rwnd is not constrained, and we have
11210 * had at least 3 packet epochs exit. Note that this is
11211 * switched off by sysctl. Google does not do this by the
11214 if ((ctf_flight_size(bbr->rc_tp,
11215 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) +
11216 (2 * max(bbr->r_ctl.rc_pace_max_segs, bbr->rc_tp->t_maxseg))) <= bbr->rc_tp->snd_wnd) {
11218 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11219 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 4);
11221 /* Just record an updated loss value */
11222 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11223 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11224 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 5);
11227 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost;
11228 if (((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS) ||
11230 /* Return 1 to exit the startup state. */
11233 /* Stay in startup */
11234 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11235 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8);
11240 bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch, uint32_t losses)
11243 * A tick occured in the rtt epoch do we need to do anything?
11245 #ifdef BBR_INVARIANTS
11246 if ((bbr->rc_bbr_state != BBR_STATE_STARTUP) &&
11247 (bbr->rc_bbr_state != BBR_STATE_DRAIN) &&
11248 (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) &&
11249 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) &&
11250 (bbr->rc_bbr_state != BBR_STATE_PROBE_BW)) {
11252 panic("Unknown BBR state %d?\n", bbr->rc_bbr_state);
11255 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) {
11256 /* Do we exit the startup state? */
11257 if (bbr_state_startup(bbr, cts, epoch, pkt_epoch)) {
11260 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch,
11261 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 6);
11262 bbr->rc_filled_pipe = 1;
11263 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
11264 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
11266 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
11267 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
11270 if (bbr->rc_no_pacing)
11271 bbr->rc_no_pacing = 0;
11272 bbr->r_ctl.rc_bbr_state_time = cts;
11273 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_drain_pg;
11274 bbr->rc_bbr_state = BBR_STATE_DRAIN;
11275 bbr_set_state_target(bbr, __LINE__);
11276 if ((bbr->rc_use_google == 0) &&
11277 bbr_slam_cwnd_in_main_drain) {
11278 /* Here we don't have to worry about probe-rtt */
11279 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd;
11280 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
11281 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11283 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain;
11284 bbr_log_type_statechange(bbr, cts, __LINE__);
11285 if (ctf_flight_size(bbr->rc_tp,
11286 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <=
11287 bbr->r_ctl.rc_target_at_state) {
11289 * Switch to probe_bw if we are already
11292 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts);
11293 bbr_substate_change(bbr, cts, __LINE__, 0);
11294 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
11295 bbr_log_type_statechange(bbr, cts, __LINE__);
11298 } else if (bbr->rc_bbr_state == BBR_STATE_IDLE_EXIT) {
11303 inflight = ctf_flight_size(tp,
11304 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11305 if (inflight >= bbr->r_ctl.rc_target_at_state) {
11306 /* We have reached a flight of the cwnd target */
11307 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
11308 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
11309 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT;
11310 bbr_set_state_target(bbr, __LINE__);
11312 * Rig it so we don't do anything crazy and
11313 * start fresh with a new randomization.
11315 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff;
11316 bbr->rc_bbr_substate = BBR_SUB_LEVEL6;
11317 bbr_substate_change(bbr, cts, __LINE__, 1);
11319 } else if (bbr->rc_bbr_state == BBR_STATE_DRAIN) {
11320 /* Has in-flight reached the bdp (or less)? */
11325 inflight = ctf_flight_size(tp,
11326 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11327 if ((bbr->rc_use_google == 0) &&
11328 bbr_slam_cwnd_in_main_drain &&
11329 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
11331 * Here we don't have to worry about probe-rtt
11332 * re-slam it, but keep it slammed down.
11334 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
11335 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11337 if (inflight <= bbr->r_ctl.rc_target_at_state) {
11338 /* We have drained */
11339 bbr->rc_bbr_state = BBR_STATE_PROBE_BW;
11340 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost;
11341 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) {
11344 time_in = cts - bbr->r_ctl.rc_bbr_state_time;
11345 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in);
11347 if ((bbr->rc_use_google == 0) &&
11348 bbr_slam_cwnd_in_main_drain &&
11349 (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) {
11350 /* Restore the cwnd */
11351 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd;
11352 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11354 /* Setup probe-rtt has being done now RRS-HERE */
11355 bbr->r_ctl.rc_rtt_shrinks = cts;
11356 bbr->r_ctl.last_in_probertt = cts;
11357 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_LEAVE_DRAIN, 0);
11358 /* Randomly pick a sub-state */
11359 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts);
11360 bbr_substate_change(bbr, cts, __LINE__, 0);
11361 bbr_log_type_statechange(bbr, cts, __LINE__);
11363 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) {
11366 flight = ctf_flight_size(bbr->rc_tp,
11367 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11368 bbr->r_ctl.r_app_limited_until = (flight + bbr->r_ctl.rc_delivered);
11369 if (((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google) &&
11370 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
11372 * We must keep cwnd at the desired MSS.
11374 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options);
11375 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11376 } else if ((bbr_prtt_slam_cwnd) &&
11377 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) {
11379 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state;
11380 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__);
11382 if (bbr->r_ctl.rc_bbr_enters_probertt == 0) {
11383 /* Has outstanding reached our target? */
11384 if (flight <= bbr->r_ctl.rc_target_at_state) {
11385 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_REACHTAR, 0);
11386 bbr->r_ctl.rc_bbr_enters_probertt = cts;
11387 /* If time is exactly 0, be 1usec off */
11388 if (bbr->r_ctl.rc_bbr_enters_probertt == 0)
11389 bbr->r_ctl.rc_bbr_enters_probertt = 1;
11390 if (bbr->rc_use_google == 0) {
11392 * Restore any lowering that as occured to
11395 if (bbr->r_ctl.bbr_rttprobe_gain_val)
11396 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val;
11398 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT;
11401 if ((bbr->r_ctl.rc_bbr_enters_probertt == 0) &&
11402 (bbr->rc_use_google == 0) &&
11403 bbr->r_ctl.bbr_rttprobe_gain_val &&
11404 (((cts - bbr->r_ctl.rc_probertt_srttchktim) > bbr_get_rtt(bbr, bbr_drain_rtt)) ||
11405 (flight >= bbr->r_ctl.flightsize_at_drain))) {
11407 * We have doddled with our current hptsi
11408 * gain an srtt and have still not made it
11409 * to target, or we have increased our flight.
11410 * Lets reduce the gain by xx%
11411 * flooring the reduce at DRAIN (based on
11416 bbr->r_ctl.flightsize_at_drain = flight;
11417 bbr->r_ctl.rc_probertt_srttchktim = cts;
11418 red = max((bbr->r_ctl.bbr_rttprobe_gain_val / 10), 1);
11419 if ((bbr->r_ctl.rc_bbr_hptsi_gain - red) > max(bbr_drain_floor, 1)) {
11420 /* Reduce our gain again */
11421 bbr->r_ctl.rc_bbr_hptsi_gain -= red;
11422 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG, 0);
11423 } else if (bbr->r_ctl.rc_bbr_hptsi_gain > max(bbr_drain_floor, 1)) {
11424 /* one more chance before we give up */
11425 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1);
11426 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG_FINAL, 0);
11428 /* At the very bottom */
11429 bbr->r_ctl.rc_bbr_hptsi_gain = max((bbr_drain_floor-1), 1);
11433 if (bbr->r_ctl.rc_bbr_enters_probertt &&
11434 (TSTMP_GT(cts, bbr->r_ctl.rc_bbr_enters_probertt)) &&
11435 ((cts - bbr->r_ctl.rc_bbr_enters_probertt) >= bbr_rtt_probe_time)) {
11436 /* Time to exit probe RTT normally */
11437 bbr_exit_probe_rtt(bbr->rc_tp, bbr, cts);
11439 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) {
11440 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) &&
11441 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) {
11443 * This qualifies as a RTT_PROBE session since we
11444 * drop the data outstanding to nothing and waited
11445 * more than bbr_rtt_probe_time.
11447 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0);
11448 bbr_set_reduced_rtt(bbr, cts, __LINE__);
11450 if (bbr_should_enter_probe_rtt(bbr, cts)) {
11451 bbr_enter_probe_rtt(bbr, cts, __LINE__);
11453 bbr_set_probebw_gains(bbr, cts, losses);
11459 bbr_check_bbr_for_state(struct tcp_bbr *bbr, uint32_t cts, int32_t line, uint32_t losses)
11463 if ((cts - bbr->r_ctl.rc_rcv_epoch_start) >= bbr_get_rtt(bbr, BBR_RTT_PROP)) {
11464 bbr_set_epoch(bbr, cts, line);
11465 /* At each epoch doe lt bw sampling */
11468 bbr_state_change(bbr, cts, epoch, bbr->rc_is_pkt_epoch_now, losses);
11472 bbr_do_segment_nounlock(struct mbuf *m, struct tcphdr *th, struct socket *so,
11473 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos,
11474 int32_t nxt_pkt, struct timeval *tv)
11476 int32_t thflags, retval;
11477 uint32_t cts, lcts;
11480 struct tcp_bbr *bbr;
11481 struct bbr_sendmap *rsm;
11482 struct timeval ltv;
11483 int32_t did_out = 0;
11484 int32_t in_recovery;
11486 int32_t prev_state;
11489 nsegs = max(1, m->m_pkthdr.lro_nsegs);
11490 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
11491 /* add in our stats */
11492 kern_prefetch(bbr, &prev_state);
11494 thflags = th->th_flags;
11496 * If this is either a state-changing packet or current state isn't
11497 * established, we require a write lock on tcbinfo. Otherwise, we
11498 * allow the tcbinfo to be in either alocked or unlocked, as the
11499 * caller may have unnecessarily acquired a write lock due to a
11502 INP_WLOCK_ASSERT(tp->t_inpcb);
11503 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
11505 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
11508 tp->t_rcvtime = ticks;
11510 * Unscale the window into a 32-bit value. For the SYN_SENT state
11511 * the scale is zero.
11513 tiwin = th->th_win << tp->snd_scale;
11515 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
11518 * Parse options on any incoming segment.
11520 tcp_dooptions(&to, (u_char *)(th + 1),
11521 (th->th_off << 2) - sizeof(struct tcphdr),
11522 (thflags & TH_SYN) ? TO_SYN : 0);
11524 if (m->m_flags & M_TSTMP) {
11525 /* Prefer the hardware timestamp if present */
11526 struct timespec ts;
11528 mbuf_tstmp2timespec(m, &ts);
11529 bbr->rc_tv.tv_sec = ts.tv_sec;
11530 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000;
11531 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usectick(&bbr->rc_tv);
11532 } else if (m->m_flags & M_TSTMP_LRO) {
11533 /* Next the arrival timestamp */
11534 struct timespec ts;
11536 mbuf_tstmp2timespec(m, &ts);
11537 bbr->rc_tv.tv_sec = ts.tv_sec;
11538 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000;
11539 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usectick(&bbr->rc_tv);
11542 * Ok just get the current time.
11544 bbr->r_ctl.rc_rcvtime = lcts = cts = tcp_get_usecs(&bbr->rc_tv);
11547 * If echoed timestamp is later than the current time, fall back to
11548 * non RFC1323 RTT calculation. Normalize timestamp if syncookies
11549 * were used when this connection was established.
11551 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
11552 to.to_tsecr -= tp->ts_offset;
11553 if (TSTMP_GT(to.to_tsecr, tcp_tv_to_mssectick(&bbr->rc_tv)))
11557 * If its the first time in we need to take care of options and
11558 * verify we can do SACK for rack!
11560 if (bbr->r_state == 0) {
11562 * Process options only when we get SYN/ACK back. The SYN
11563 * case for incoming connections is handled in tcp_syncache.
11564 * According to RFC1323 the window field in a SYN (i.e., a
11565 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX
11566 * this is traditional behavior, may need to be cleaned up.
11568 if (bbr->rc_inp == NULL) {
11569 bbr->rc_inp = tp->t_inpcb;
11572 * We need to init rc_inp here since its not init'd when
11573 * bbr_init is called
11575 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
11576 if ((to.to_flags & TOF_SCALE) &&
11577 (tp->t_flags & TF_REQ_SCALE)) {
11578 tp->t_flags |= TF_RCVD_SCALE;
11579 tp->snd_scale = to.to_wscale;
11581 tp->t_flags &= ~TF_REQ_SCALE;
11583 * Initial send window. It will be updated with the
11584 * next incoming segment to the scaled value.
11586 tp->snd_wnd = th->th_win;
11587 if ((to.to_flags & TOF_TS) &&
11588 (tp->t_flags & TF_REQ_TSTMP)) {
11589 tp->t_flags |= TF_RCVD_TSTMP;
11590 tp->ts_recent = to.to_tsval;
11591 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv);
11593 tp->t_flags &= ~TF_REQ_TSTMP;
11594 if (to.to_flags & TOF_MSS)
11595 tcp_mss(tp, to.to_mss);
11596 if ((tp->t_flags & TF_SACK_PERMIT) &&
11597 (to.to_flags & TOF_SACKPERM) == 0)
11598 tp->t_flags &= ~TF_SACK_PERMIT;
11599 if (IS_FASTOPEN(tp->t_flags)) {
11600 if (to.to_flags & TOF_FASTOPEN) {
11603 if (to.to_flags & TOF_MSS)
11606 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
11610 tcp_fastopen_update_cache(tp, mss,
11611 to.to_tfo_len, to.to_tfo_cookie);
11613 tcp_fastopen_disable_path(tp);
11617 * At this point we are at the initial call. Here we decide
11618 * if we are doing RACK or not. We do this by seeing if
11619 * TF_SACK_PERMIT is set, if not rack is *not* possible and
11620 * we switch to the default code.
11622 if ((tp->t_flags & TF_SACK_PERMIT) == 0) {
11624 tcp_switch_back_to_default(tp);
11625 (*tp->t_fb->tfb_tcp_do_segment) (m, th, so, tp, drop_hdrlen,
11630 bbr->r_is_v6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
11631 tcp_set_hpts(tp->t_inpcb);
11632 sack_filter_clear(&bbr->r_ctl.bbr_sf, th->th_ack);
11634 if (thflags & TH_ACK) {
11635 /* Track ack types */
11636 if (to.to_flags & TOF_SACK)
11637 BBR_STAT_INC(bbr_acks_with_sacks);
11639 BBR_STAT_INC(bbr_plain_acks);
11642 * This is the one exception case where we set the rack state
11643 * always. All other times (timers etc) we must have a rack-state
11644 * set (so we assure we have done the checks above for SACK).
11646 if (thflags & TH_FIN)
11647 tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
11648 if (bbr->r_state != tp->t_state)
11649 bbr_set_state(tp, bbr, tiwin);
11651 if (SEQ_GT(th->th_ack, tp->snd_una) && (rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map)) != NULL)
11652 kern_prefetch(rsm, &prev_state);
11653 prev_state = bbr->r_state;
11654 bbr->rc_ack_was_delayed = 0;
11655 lost = bbr->r_ctl.rc_lost;
11656 bbr->rc_is_pkt_epoch_now = 0;
11657 if (m->m_flags & (M_TSTMP|M_TSTMP_LRO)) {
11658 /* Get the real time into lcts and figure the real delay */
11659 lcts = tcp_get_usecs(<v);
11660 if (TSTMP_GT(lcts, cts)) {
11661 bbr->r_ctl.rc_ack_hdwr_delay = lcts - cts;
11662 bbr->rc_ack_was_delayed = 1;
11663 if (TSTMP_GT(bbr->r_ctl.rc_ack_hdwr_delay,
11664 bbr->r_ctl.highest_hdwr_delay))
11665 bbr->r_ctl.highest_hdwr_delay = bbr->r_ctl.rc_ack_hdwr_delay;
11667 bbr->r_ctl.rc_ack_hdwr_delay = 0;
11668 bbr->rc_ack_was_delayed = 0;
11671 bbr->r_ctl.rc_ack_hdwr_delay = 0;
11672 bbr->rc_ack_was_delayed = 0;
11674 bbr_log_ack_event(bbr, th, &to, tlen, nsegs, cts, nxt_pkt, m);
11675 if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
11678 goto done_with_input;
11681 * If a segment with the ACK-bit set arrives in the SYN-SENT state
11682 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9.
11684 if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
11685 (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
11686 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
11687 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11690 in_recovery = IN_RECOVERY(tp->t_flags);
11691 if (tiwin > bbr->r_ctl.rc_high_rwnd)
11692 bbr->r_ctl.rc_high_rwnd = tiwin;
11693 #ifdef BBR_INVARIANTS
11694 if ((tp->t_inpcb->inp_flags & INP_DROPPED) ||
11695 (tp->t_inpcb->inp_flags2 & INP_FREED)) {
11696 panic("tp:%p bbr:%p given a dropped inp:%p",
11697 tp, bbr, tp->t_inpcb);
11700 bbr->r_ctl.rc_flight_at_input = ctf_flight_size(tp,
11701 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11702 bbr->rtt_valid = 0;
11703 if (to.to_flags & TOF_TS) {
11704 bbr->rc_ts_valid = 1;
11705 bbr->r_ctl.last_inbound_ts = to.to_tsval;
11707 bbr->rc_ts_valid = 0;
11708 bbr->r_ctl.last_inbound_ts = 0;
11710 retval = (*bbr->r_substate) (m, th, so,
11711 tp, &to, drop_hdrlen,
11712 tlen, tiwin, thflags, nxt_pkt, iptos);
11713 #ifdef BBR_INVARIANTS
11714 if ((retval == 0) &&
11715 (tp->t_inpcb == NULL)) {
11716 panic("retval:%d tp:%p t_inpcb:NULL state:%d",
11717 retval, tp, prev_state);
11721 BBR_STAT_INC(bbr_rlock_left_ret0);
11723 BBR_STAT_INC(bbr_rlock_left_ret1);
11726 * If retval is 1 the tcb is unlocked and most likely the tp
11729 INP_WLOCK_ASSERT(tp->t_inpcb);
11730 tcp_bbr_xmit_timer_commit(bbr, tp, cts);
11731 if (bbr->rc_is_pkt_epoch_now)
11732 bbr_set_pktepoch(bbr, cts, __LINE__);
11733 bbr_check_bbr_for_state(bbr, cts, __LINE__, (bbr->r_ctl.rc_lost - lost));
11734 if (nxt_pkt == 0) {
11735 if (bbr->r_wanted_output != 0) {
11736 bbr->rc_output_starts_timer = 0;
11738 (void)tp->t_fb->tfb_tcp_output(tp);
11740 bbr_start_hpts_timer(bbr, tp, cts, 6, 0, 0);
11742 if ((nxt_pkt == 0) &&
11743 ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) &&
11744 (SEQ_GT(tp->snd_max, tp->snd_una) ||
11745 (tp->t_flags & TF_DELACK) ||
11746 ((V_tcp_always_keepalive || bbr->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
11747 (tp->t_state <= TCPS_CLOSING)))) {
11749 * We could not send (probably in the hpts but
11750 * stopped the timer)?
11752 if ((tp->snd_max == tp->snd_una) &&
11753 ((tp->t_flags & TF_DELACK) == 0) &&
11754 (bbr->rc_inp->inp_in_hpts) &&
11755 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
11757 * keep alive not needed if we are hptsi
11762 if (bbr->rc_inp->inp_in_hpts) {
11763 tcp_hpts_remove(bbr->rc_inp, HPTS_REMOVE_OUTPUT);
11764 if ((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
11765 (TSTMP_GT(lcts, bbr->rc_pacer_started))) {
11768 del = lcts - bbr->rc_pacer_started;
11769 if (bbr->r_ctl.rc_last_delay_val > del) {
11770 BBR_STAT_INC(bbr_force_timer_start);
11771 bbr->r_ctl.rc_last_delay_val -= del;
11772 bbr->rc_pacer_started = lcts;
11775 bbr->r_ctl.rc_last_delay_val = 0;
11776 BBR_STAT_INC(bbr_force_output);
11777 (void)tp->t_fb->tfb_tcp_output(tp);
11781 bbr_start_hpts_timer(bbr, tp, cts, 8, bbr->r_ctl.rc_last_delay_val,
11784 } else if ((bbr->rc_output_starts_timer == 0) && (nxt_pkt == 0)) {
11785 /* Do we have the correct timer running? */
11786 bbr_timer_audit(tp, bbr, lcts, &so->so_snd);
11788 /* Do we have a new state */
11789 if (bbr->r_state != tp->t_state)
11790 bbr_set_state(tp, bbr, tiwin);
11792 bbr_log_doseg_done(bbr, cts, nxt_pkt, did_out);
11794 bbr->r_wanted_output = 0;
11795 #ifdef BBR_INVARIANTS
11796 if (tp->t_inpcb == NULL) {
11797 panic("OP:%d retval:%d tp:%p t_inpcb:NULL state:%d",
11799 retval, tp, prev_state);
11807 bbr_log_type_hrdwtso(struct tcpcb *tp, struct tcp_bbr *bbr, int len, int mod, int what_we_can_send)
11809 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
11810 union tcp_log_stackspecific log;
11814 cts = tcp_get_usecs(&tv);
11815 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
11816 log.u_bbr.flex1 = bbr->r_ctl.rc_pace_min_segs;
11817 log.u_bbr.flex2 = what_we_can_send;
11818 log.u_bbr.flex3 = bbr->r_ctl.rc_pace_max_segs;
11819 log.u_bbr.flex4 = len;
11820 log.u_bbr.flex5 = 0;
11821 log.u_bbr.flex7 = mod;
11822 log.u_bbr.flex8 = 1;
11823 TCP_LOG_EVENTP(tp, NULL,
11824 &tp->t_inpcb->inp_socket->so_rcv,
11825 &tp->t_inpcb->inp_socket->so_snd,
11827 0, &log, false, &tv);
11832 bbr_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
11833 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos)
11838 /* First lets see if we have old packets */
11839 if (tp->t_in_pkt) {
11840 if (ctf_do_queued_segments(so, tp, 1)) {
11845 if (m->m_flags & M_TSTMP_LRO) {
11846 tv.tv_sec = m->m_pkthdr.rcv_tstmp /1000000000;
11847 tv.tv_usec = (m->m_pkthdr.rcv_tstmp % 1000000000)/1000;
11849 /* Should not be should we kassert instead? */
11850 tcp_get_usecs(&tv);
11852 retval = bbr_do_segment_nounlock(m, th, so, tp,
11853 drop_hdrlen, tlen, iptos, 0, &tv);
11855 INP_WUNLOCK(tp->t_inpcb);
11859 * Return how much data can be sent without violating the
11863 static inline uint32_t
11864 bbr_what_can_we_send(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t sendwin,
11865 uint32_t avail, int32_t sb_offset, uint32_t cts)
11869 if (ctf_outstanding(tp) >= tp->snd_wnd) {
11870 /* We never want to go over our peers rcv-window */
11875 flight = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes));
11876 if (flight >= sendwin) {
11878 * We have in flight what we are allowed by cwnd (if
11879 * it was rwnd blocking it would have hit above out
11884 len = sendwin - flight;
11885 if ((len + ctf_outstanding(tp)) > tp->snd_wnd) {
11886 /* We would send too much (beyond the rwnd) */
11887 len = tp->snd_wnd - ctf_outstanding(tp);
11889 if ((len + sb_offset) > avail) {
11891 * We don't have that much in the SB, how much is
11894 len = avail - sb_offset;
11901 bbr_do_error_accounting(struct tcpcb *tp, struct tcp_bbr *bbr, struct bbr_sendmap *rsm, int32_t len, int32_t error)
11903 #ifdef NETFLIX_STATS
11904 KMOD_TCPSTAT_INC(tcps_sndpack_error);
11905 KMOD_TCPSTAT_ADD(tcps_sndbyte_error, len);
11910 bbr_do_send_accounting(struct tcpcb *tp, struct tcp_bbr *bbr, struct bbr_sendmap *rsm, int32_t len, int32_t error)
11913 bbr_do_error_accounting(tp, bbr, rsm, len, error);
11917 if (rsm->r_flags & BBR_TLP) {
11919 * TLP should not count in retran count, but in its
11922 #ifdef NETFLIX_STATS
11923 tp->t_sndtlppack++;
11924 tp->t_sndtlpbyte += len;
11925 KMOD_TCPSTAT_INC(tcps_tlpresends);
11926 KMOD_TCPSTAT_ADD(tcps_tlpresend_bytes, len);
11930 tp->t_sndrexmitpack++;
11931 KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
11932 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
11934 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
11939 * Logs in 0 - 8, 8 is all non probe_bw states 0-7 is
11942 counter_u64_add(bbr_state_lost[rsm->r_bbr_state], len);
11943 if (bbr->rc_bbr_state != BBR_STATE_PROBE_BW) {
11944 /* Non probe_bw log in 1, 2, or 4. */
11945 counter_u64_add(bbr_state_resend[bbr->rc_bbr_state], len);
11948 * Log our probe state 3, and log also 5-13 to show
11949 * us the recovery sub-state for the send. This
11950 * means that 3 == (5+6+7+8+9+10+11+12+13)
11952 counter_u64_add(bbr_state_resend[BBR_STATE_PROBE_BW], len);
11953 counter_u64_add(bbr_state_resend[(bbr_state_val(bbr) + 5)], len);
11955 /* Place in both 16's the totals of retransmitted */
11956 counter_u64_add(bbr_state_lost[16], len);
11957 counter_u64_add(bbr_state_resend[16], len);
11958 /* Place in 17's the total sent */
11959 counter_u64_add(bbr_state_resend[17], len);
11960 counter_u64_add(bbr_state_lost[17], len);
11964 KMOD_TCPSTAT_INC(tcps_sndpack);
11965 KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
11966 /* Place in 17's the total sent */
11967 counter_u64_add(bbr_state_resend[17], len);
11968 counter_u64_add(bbr_state_lost[17], len);
11970 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
11977 bbr_cwnd_limiting(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t in_level)
11979 if (bbr->rc_filled_pipe && bbr_target_cwnd_mult_limit && (bbr->rc_use_google == 0)) {
11981 * Limit the cwnd to not be above N x the target plus whats
11982 * is outstanding. The target is based on the current b/w
11987 target = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), BBR_UNIT);
11988 target += ctf_outstanding(tp);
11989 target *= bbr_target_cwnd_mult_limit;
11990 if (tp->snd_cwnd > target)
11991 tp->snd_cwnd = target;
11992 bbr_log_type_cwndupd(bbr, 0, 0, 0, 10, 0, 0, __LINE__);
11997 bbr_window_update_needed(struct tcpcb *tp, struct socket *so, uint32_t recwin, int32_t maxseg)
12000 * "adv" is the amount we could increase the window, taking into
12001 * account that we are limited by TCP_MAXWIN << tp->rcv_scale.
12007 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
12008 oldwin = (tp->rcv_adv - tp->rcv_nxt);
12012 /* We can't increase the window */
12019 * If the new window size ends up being the same as or less
12020 * than the old size when it is scaled, then don't force
12023 if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale)
12026 if (adv >= (2 * maxseg) &&
12027 (adv >= (so->so_rcv.sb_hiwat / 4) ||
12028 recwin <= (so->so_rcv.sb_hiwat / 8) ||
12029 so->so_rcv.sb_hiwat <= 8 * maxseg)) {
12032 if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat)
12038 * Return 0 on success and a errno on failure to send.
12039 * Note that a 0 return may not mean we sent anything
12040 * if the TCB was on the hpts. A non-zero return
12041 * does indicate the error we got from ip[6]_output.
12044 bbr_output_wtime(struct tcpcb *tp, const struct timeval *tv)
12049 uint32_t recwin, sendwin;
12051 int32_t flags, abandon, error = 0;
12052 struct tcp_log_buffer *lgb = NULL;
12055 uint32_t if_hw_tsomaxsegcount = 0;
12056 uint32_t if_hw_tsomaxsegsize = 0;
12057 uint32_t if_hw_tsomax = 0;
12058 struct ip *ip = NULL;
12060 struct ipovly *ipov = NULL;
12062 struct tcp_bbr *bbr;
12064 #ifdef NETFLIX_TCPOUDP
12065 struct udphdr *udp = NULL;
12067 u_char opt[TCP_MAXOLEN];
12068 unsigned ipoptlen, optlen, hdrlen;
12069 #ifdef NETFLIX_TCPOUDP
12073 uint32_t delay_calc=0;
12074 uint8_t doing_tlp = 0;
12075 uint8_t local_options;
12076 #ifdef BBR_INVARIANTS
12077 uint8_t doing_retran_from = 0;
12078 uint8_t picked_up_retran = 0;
12080 uint8_t wanted_cookie = 0;
12081 uint8_t more_to_rxt=0;
12082 int32_t prefetch_so_done = 0;
12083 int32_t prefetch_rsm = 0;
12084 uint32_t what_we_can = 0;
12085 uint32_t tot_len = 0;
12086 uint32_t rtr_cnt = 0;
12087 uint32_t maxseg, pace_max_segs, p_maxseg;
12088 int32_t csum_flags;
12090 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
12091 unsigned ipsec_optlen = 0;
12094 volatile int32_t sack_rxmit;
12095 struct bbr_sendmap *rsm = NULL;
12101 struct sockbuf *sb;
12102 uint32_t hpts_calling;
12104 struct ip6_hdr *ip6 = NULL;
12107 uint8_t app_limited = BBR_JR_SENT_DATA;
12108 uint8_t filled_all = 0;
12109 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
12110 /* We take a cache hit here */
12111 memcpy(&bbr->rc_tv, tv, sizeof(struct timeval));
12112 cts = tcp_tv_to_usectick(&bbr->rc_tv);
12114 so = inp->inp_socket;
12117 if (sb->sb_flags & SB_TLS_IFNET)
12122 kern_prefetch(sb, &maxseg);
12123 maxseg = tp->t_maxseg - bbr->rc_last_options;
12124 if (bbr_minseg(bbr) < maxseg) {
12125 tcp_bbr_tso_size_check(bbr, cts);
12127 /* Remove any flags that indicate we are pacing on the inp */
12128 pace_max_segs = bbr->r_ctl.rc_pace_max_segs;
12129 p_maxseg = min(maxseg, pace_max_segs);
12130 INP_WLOCK_ASSERT(inp);
12132 if (tp->t_flags & TF_TOE)
12133 return (tcp_offload_output(tp));
12137 if (bbr->r_state) {
12138 /* Use the cache line loaded if possible */
12139 isipv6 = bbr->r_is_v6;
12141 isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
12144 if (((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) &&
12145 inp->inp_in_hpts) {
12147 * We are on the hpts for some timer but not hptsi output.
12148 * Possibly remove from the hpts so we can send/recv etc.
12150 if ((tp->t_flags & TF_ACKNOW) == 0) {
12152 * No immediate demand right now to send an ack, but
12153 * the user may have read, making room for new data
12154 * (a window update). If so we may want to cancel
12155 * whatever timer is running (KEEP/DEL-ACK?) and
12156 * continue to send out a window update. Or we may
12157 * have gotten more data into the socket buffer to
12160 recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
12161 (long)TCP_MAXWIN << tp->rcv_scale);
12162 if ((bbr_window_update_needed(tp, so, recwin, maxseg) == 0) &&
12163 ((tcp_outflags[tp->t_state] & TH_RST) == 0) &&
12164 ((sbavail(sb) + ((tcp_outflags[tp->t_state] & TH_FIN) ? 1 : 0)) <=
12165 (tp->snd_max - tp->snd_una))) {
12167 * Nothing new to send and no window update
12168 * is needed to send. Lets just return and
12169 * let the timer-run off.
12174 tcp_hpts_remove(inp, HPTS_REMOVE_OUTPUT);
12175 bbr_timer_cancel(bbr, __LINE__, cts);
12177 if (bbr->r_ctl.rc_last_delay_val) {
12178 /* Calculate a rough delay for early escape to sending */
12179 if (SEQ_GT(cts, bbr->rc_pacer_started))
12180 delay_calc = cts - bbr->rc_pacer_started;
12181 if (delay_calc >= bbr->r_ctl.rc_last_delay_val)
12182 delay_calc -= bbr->r_ctl.rc_last_delay_val;
12186 /* Mark that we have called bbr_output(). */
12187 if ((bbr->r_timer_override) ||
12188 (tp->t_state < TCPS_ESTABLISHED)) {
12189 /* Timeouts or early states are exempt */
12190 if (inp->inp_in_hpts)
12191 tcp_hpts_remove(inp, HPTS_REMOVE_OUTPUT);
12192 } else if (inp->inp_in_hpts) {
12193 if ((bbr->r_ctl.rc_last_delay_val) &&
12194 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
12197 * We were being paced for output and the delay has
12198 * already exceeded when we were supposed to be
12199 * called, lets go ahead and pull out of the hpts
12202 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_LATE], 1);
12203 bbr->r_ctl.rc_last_delay_val = 0;
12204 tcp_hpts_remove(inp, HPTS_REMOVE_OUTPUT);
12205 } else if (tp->t_state == TCPS_CLOSED) {
12206 bbr->r_ctl.rc_last_delay_val = 0;
12207 tcp_hpts_remove(inp, HPTS_REMOVE_OUTPUT);
12210 * On the hpts, you shall not pass! even if ACKNOW
12211 * is on, we will when the hpts fires, unless of
12212 * course we are overdue.
12214 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_INPACE], 1);
12218 bbr->rc_cwnd_limited = 0;
12219 if (bbr->r_ctl.rc_last_delay_val) {
12220 /* recalculate the real delay and deal with over/under */
12221 if (SEQ_GT(cts, bbr->rc_pacer_started))
12222 delay_calc = cts - bbr->rc_pacer_started;
12225 if (delay_calc >= bbr->r_ctl.rc_last_delay_val)
12226 /* Setup the delay which will be added in */
12227 delay_calc -= bbr->r_ctl.rc_last_delay_val;
12230 * We are early setup to adjust
12233 uint64_t merged_val;
12235 bbr->r_ctl.rc_agg_early += (bbr->r_ctl.rc_last_delay_val - delay_calc);
12236 bbr->r_agg_early_set = 1;
12237 if (bbr->r_ctl.rc_hptsi_agg_delay) {
12238 if (bbr->r_ctl.rc_hptsi_agg_delay >= bbr->r_ctl.rc_agg_early) {
12239 /* Nope our previous late cancels out the early */
12240 bbr->r_ctl.rc_hptsi_agg_delay -= bbr->r_ctl.rc_agg_early;
12241 bbr->r_agg_early_set = 0;
12242 bbr->r_ctl.rc_agg_early = 0;
12244 bbr->r_ctl.rc_agg_early -= bbr->r_ctl.rc_hptsi_agg_delay;
12245 bbr->r_ctl.rc_hptsi_agg_delay = 0;
12248 merged_val = bbr->rc_pacer_started;
12250 merged_val |= bbr->r_ctl.rc_last_delay_val;
12251 bbr_log_pacing_delay_calc(bbr, inp->inp_hpts_calls,
12252 bbr->r_ctl.rc_agg_early, cts, delay_calc, merged_val,
12253 bbr->r_agg_early_set, 3);
12254 bbr->r_ctl.rc_last_delay_val = 0;
12255 BBR_STAT_INC(bbr_early);
12259 /* We were not delayed due to hptsi */
12260 if (bbr->r_agg_early_set)
12261 bbr->r_ctl.rc_agg_early = 0;
12262 bbr->r_agg_early_set = 0;
12267 * We had a hptsi delay which means we are falling behind on
12268 * sending at the expected rate. Calculate an extra amount
12269 * of data we can send, if any, to put us back on track.
12271 if ((bbr->r_ctl.rc_hptsi_agg_delay + delay_calc) < bbr->r_ctl.rc_hptsi_agg_delay)
12272 bbr->r_ctl.rc_hptsi_agg_delay = 0xffffffff;
12274 bbr->r_ctl.rc_hptsi_agg_delay += delay_calc;
12276 sendwin = min(tp->snd_wnd, tp->snd_cwnd);
12277 if ((tp->snd_una == tp->snd_max) &&
12278 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) &&
12281 * Ok we have been idle with nothing outstanding
12282 * we possibly need to start fresh with either a new
12283 * suite of states or a fast-ramp up.
12285 bbr_restart_after_idle(bbr,
12286 cts, bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time));
12289 * Now was there a hptsi delay where we are behind? We only count
12290 * being behind if: a) We are not in recovery. b) There was a delay.
12291 * <and> c) We had room to send something.
12294 hpts_calling = inp->inp_hpts_calls;
12295 inp->inp_hpts_calls = 0;
12296 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
12297 if (bbr_process_timers(tp, bbr, cts, hpts_calling)) {
12298 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_ATIMER], 1);
12302 bbr->rc_inp->inp_flags2 &= ~INP_MBUF_QUEUE_READY;
12303 if (hpts_calling &&
12304 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
12305 bbr->r_ctl.rc_last_delay_val = 0;
12307 bbr->r_timer_override = 0;
12308 bbr->r_wanted_output = 0;
12310 * For TFO connections in SYN_RECEIVED, only allow the initial
12311 * SYN|ACK and those sent by the retransmit timer.
12313 if (IS_FASTOPEN(tp->t_flags) &&
12314 ((tp->t_state == TCPS_SYN_RECEIVED) ||
12315 (tp->t_state == TCPS_SYN_SENT)) &&
12316 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */
12317 (tp->t_rxtshift == 0)) { /* not a retransmit */
12319 goto just_return_nolock;
12322 * Before sending anything check for a state update. For hpts
12323 * calling without input this is important. If its input calling
12324 * then this was already done.
12326 if (bbr->rc_use_google == 0)
12327 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0);
12330 * If we've recently taken a timeout, snd_max will be greater than
12331 * snd_max. BBR in general does not pay much attention to snd_nxt
12332 * for historic reasons the persist timer still uses it. This means
12333 * we have to look at it. All retransmissions that are not persits
12334 * use the rsm that needs to be sent so snd_nxt is ignored. At the
12335 * end of this routine we pull snd_nxt always up to snd_max.
12338 #ifdef BBR_INVARIANTS
12339 doing_retran_from = picked_up_retran = 0;
12345 sendwin = min(tp->snd_wnd, tp->snd_cwnd);
12346 sb_offset = tp->snd_max - tp->snd_una;
12347 flags = tcp_outflags[tp->t_state];
12351 if (flags & TH_RST) {
12356 while (bbr->r_ctl.rc_free_cnt < bbr_min_req_free) {
12357 /* We need to always have one in reserve */
12358 rsm = bbr_alloc(bbr);
12361 /* Lie to get on the hpts */
12362 tot_len = tp->t_maxseg;
12364 /* Retry in a ms */
12366 goto just_return_nolock;
12368 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_free, rsm, r_next);
12369 bbr->r_ctl.rc_free_cnt++;
12372 /* What do we send, a resend? */
12373 if (bbr->r_ctl.rc_resend == NULL) {
12374 /* Check for rack timeout */
12375 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts);
12376 if (bbr->r_ctl.rc_resend) {
12377 #ifdef BBR_INVARIANTS
12378 picked_up_retran = 1;
12380 bbr_cong_signal(tp, NULL, CC_NDUPACK, bbr->r_ctl.rc_resend);
12383 if (bbr->r_ctl.rc_resend) {
12384 rsm = bbr->r_ctl.rc_resend;
12385 #ifdef BBR_INVARIANTS
12386 doing_retran_from = 1;
12388 /* Remove any TLP flags its a RACK or T-O */
12389 rsm->r_flags &= ~BBR_TLP;
12390 bbr->r_ctl.rc_resend = NULL;
12391 if (SEQ_LT(rsm->r_start, tp->snd_una)) {
12392 #ifdef BBR_INVARIANTS
12393 panic("Huh, tp:%p bbr:%p rsm:%p start:%u < snd_una:%u\n",
12394 tp, bbr, rsm, rsm->r_start, tp->snd_una);
12395 goto recheck_resend;
12399 goto recheck_resend;
12403 if (rsm->r_flags & BBR_HAS_SYN) {
12404 /* Only retransmit a SYN by itself */
12406 if ((flags & TH_SYN) == 0) {
12407 /* Huh something is wrong */
12409 if (rsm->r_start == rsm->r_end) {
12410 /* Clean it up, somehow we missed the ack? */
12411 bbr_log_syn(tp, NULL);
12413 /* TFO with data? */
12414 rsm->r_flags &= ~BBR_HAS_SYN;
12415 len = rsm->r_end - rsm->r_start;
12418 /* Retransmitting SYN */
12424 len = rsm->r_end - rsm->r_start;
12425 if ((bbr->rc_resends_use_tso == 0) &&
12427 ((sb->sb_flags & SB_TLS_IFNET) == 0) &&
12433 sb_offset = rsm->r_start - tp->snd_una;
12436 KMOD_TCPSTAT_INC(tcps_sack_rexmits);
12437 KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes,
12440 /* I dont think this can happen */
12442 goto recheck_resend;
12444 BBR_STAT_INC(bbr_resends_set);
12445 } else if (bbr->r_ctl.rc_tlp_send) {
12450 rsm = bbr->r_ctl.rc_tlp_send;
12451 bbr->r_ctl.rc_tlp_send = NULL;
12453 len = rsm->r_end - rsm->r_start;
12455 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg))
12458 if (SEQ_GT(tp->snd_una, rsm->r_start)) {
12459 #ifdef BBR_INVARIANTS
12460 panic("tp:%p bbc:%p snd_una:%u rsm:%p r_start:%u",
12461 tp, bbr, tp->snd_una, rsm, rsm->r_start);
12465 goto recheck_resend;
12468 sb_offset = rsm->r_start - tp->snd_una;
12469 BBR_STAT_INC(bbr_tlp_set);
12472 * Enforce a connection sendmap count limit if set
12473 * as long as we are not retransmiting.
12475 if ((rsm == NULL) &&
12476 (V_tcp_map_entries_limit > 0) &&
12477 (bbr->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
12478 BBR_STAT_INC(bbr_alloc_limited);
12479 if (!bbr->alloc_limit_reported) {
12480 bbr->alloc_limit_reported = 1;
12481 BBR_STAT_INC(bbr_alloc_limited_conns);
12483 goto just_return_nolock;
12485 #ifdef BBR_INVARIANTS
12486 if (rsm && SEQ_LT(rsm->r_start, tp->snd_una)) {
12487 panic("tp:%p bbr:%p rsm:%p sb_offset:%u len:%u",
12488 tp, bbr, rsm, sb_offset, len);
12492 * Get standard flags, and add SYN or FIN if requested by 'hidden'
12495 if (tp->t_flags & TF_NEEDFIN && (rsm == NULL))
12497 if (tp->t_flags & TF_NEEDSYN)
12500 if (rsm && (rsm->r_flags & BBR_HAS_FIN)) {
12501 /* we are retransmitting the fin */
12505 * When retransmitting data do *not* include the
12506 * FIN. This could happen from a TLP probe if we
12507 * allowed data with a FIN.
12512 if (flags & TH_FIN)
12515 if ((sack_rxmit == 0) && (prefetch_rsm == 0)) {
12518 end_rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_tmap, bbr_sendmap, r_tnext);
12520 kern_prefetch(end_rsm, &prefetch_rsm);
12525 * If snd_nxt == snd_max and we have transmitted a FIN, the
12526 * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a
12527 * negative length. This can also occur when TCP opens up its
12528 * congestion window while receiving additional duplicate acks after
12529 * fast-retransmit because TCP will reset snd_nxt to snd_max after
12530 * the fast-retransmit.
12532 * In the normal retransmit-FIN-only case, however, snd_nxt will be
12533 * set to snd_una, the sb_offset will be 0, and the length may wind
12536 * If sack_rxmit is true we are retransmitting from the scoreboard
12537 * in which case len is already set.
12539 if (sack_rxmit == 0) {
12542 avail = sbavail(sb);
12543 if (SEQ_GT(tp->snd_max, tp->snd_una))
12544 sb_offset = tp->snd_max - tp->snd_una;
12547 if (bbr->rc_tlp_new_data) {
12548 /* TLP is forcing out new data */
12554 if (tlplen > (uint32_t)(avail - sb_offset)) {
12555 tlplen = (uint32_t)(avail - sb_offset);
12557 if (tlplen > tp->snd_wnd) {
12562 bbr->rc_tlp_new_data = 0;
12564 what_we_can = len = bbr_what_can_we_send(tp, bbr, sendwin, avail, sb_offset, cts);
12565 if ((len < p_maxseg) &&
12566 (bbr->rc_in_persist == 0) &&
12567 (ctf_outstanding(tp) >= (2 * p_maxseg)) &&
12568 ((avail - sb_offset) >= p_maxseg)) {
12570 * We are not completing whats in the socket
12571 * buffer (i.e. there is at least a segment
12572 * waiting to send) and we have 2 or more
12573 * segments outstanding. There is no sense
12574 * of sending a little piece. Lets defer and
12575 * and wait until we can send a whole
12580 if (bbr->rc_in_persist) {
12582 * We are in persists, figure out if
12583 * a retransmit is available (maybe the previous
12584 * persists we sent) or if we have to send new
12587 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
12589 len = rsm->r_end - rsm->r_start;
12590 if (rsm->r_flags & BBR_HAS_FIN)
12592 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg))
12595 BBR_STAT_INC(bbr_persist_reneg);
12597 * XXXrrs we could force the len to
12598 * 1 byte here to cause the chunk to
12599 * split apart.. but that would then
12600 * mean we always retransmit it as
12601 * one byte even after the window
12605 sb_offset = rsm->r_start - tp->snd_una;
12608 * First time through in persists or peer
12609 * acked our one byte. Though we do have
12610 * to have something in the sb.
12620 if (prefetch_so_done == 0) {
12621 kern_prefetch(so, &prefetch_so_done);
12622 prefetch_so_done = 1;
12625 * Lop off SYN bit if it has already been sent. However, if this is
12626 * SYN-SENT state and if segment contains data and if we don't know
12627 * that foreign host supports TAO, suppress sending segment.
12629 if ((flags & TH_SYN) && (rsm == NULL) &&
12630 SEQ_GT(tp->snd_max, tp->snd_una)) {
12631 if (tp->t_state != TCPS_SYN_RECEIVED)
12634 * When sending additional segments following a TFO SYN|ACK,
12635 * do not include the SYN bit.
12637 if (IS_FASTOPEN(tp->t_flags) &&
12638 (tp->t_state == TCPS_SYN_RECEIVED))
12640 sb_offset--, len++;
12641 if (sbavail(sb) == 0)
12643 } else if ((flags & TH_SYN) && rsm) {
12645 * Subtract one from the len for the SYN being
12651 * Be careful not to send data and/or FIN on SYN segments. This
12652 * measure is needed to prevent interoperability problems with not
12653 * fully conformant TCP implementations.
12655 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
12660 * On TFO sockets, ensure no data is sent in the following cases:
12662 * - When retransmitting SYN|ACK on a passively-created socket
12663 * - When retransmitting SYN on an actively created socket
12664 * - When sending a zero-length cookie (cookie request) on an
12665 * actively created socket
12666 * - When the socket is in the CLOSED state (RST is being sent)
12668 if (IS_FASTOPEN(tp->t_flags) &&
12669 (((flags & TH_SYN) && (tp->t_rxtshift > 0)) ||
12670 ((tp->t_state == TCPS_SYN_SENT) &&
12671 (tp->t_tfo_client_cookie_len == 0)) ||
12672 (flags & TH_RST))) {
12677 /* Without fast-open there should never be data sent on a SYN */
12678 if ((flags & TH_SYN) && (!IS_FASTOPEN(tp->t_flags)))
12682 * If FIN has been sent but not acked, but we haven't been
12683 * called to retransmit, len will be < 0. Otherwise, window
12684 * shrank after we sent into it. If window shrank to 0,
12685 * cancel pending retransmit, pull snd_nxt back to (closed)
12686 * window, and set the persist timer if it isn't already
12687 * going. If the window didn't close completely, just wait
12690 * We also do a general check here to ensure that we will
12691 * set the persist timer when we have data to send, but a
12692 * 0-byte window. This makes sure the persist timer is set
12693 * even if the packet hits one of the "goto send" lines
12697 if ((tp->snd_wnd == 0) &&
12698 (TCPS_HAVEESTABLISHED(tp->t_state)) &&
12699 (tp->snd_una == tp->snd_max) &&
12700 (sb_offset < (int)sbavail(sb))) {
12702 * Not enough room in the rwnd to send
12703 * a paced segment out.
12705 bbr_enter_persist(tp, bbr, cts, __LINE__);
12707 } else if ((rsm == NULL) &&
12708 (doing_tlp == 0) &&
12709 (len < bbr->r_ctl.rc_pace_max_segs)) {
12711 * We are not sending a full segment for
12712 * some reason. Should we not send anything (think
12713 * sws or persists)?
12715 if ((tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
12716 (TCPS_HAVEESTABLISHED(tp->t_state)) &&
12717 (len < (int)(sbavail(sb) - sb_offset))) {
12719 * Here the rwnd is less than
12720 * the pacing size, this is not a retransmit,
12721 * we are established and
12722 * the send is not the last in the socket buffer
12723 * lets not send, and possibly enter persists.
12726 if (tp->snd_max == tp->snd_una)
12727 bbr_enter_persist(tp, bbr, cts, __LINE__);
12728 } else if ((tp->snd_cwnd >= bbr->r_ctl.rc_pace_max_segs) &&
12729 (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12730 bbr->r_ctl.rc_lost_bytes)) > (2 * maxseg)) &&
12731 (len < (int)(sbavail(sb) - sb_offset)) &&
12732 (len < bbr_minseg(bbr))) {
12734 * Here we are not retransmitting, and
12735 * the cwnd is not so small that we could
12736 * not send at least a min size (rxt timer
12737 * not having gone off), We have 2 segments or
12738 * more already in flight, its not the tail end
12739 * of the socket buffer and the cwnd is blocking
12740 * us from sending out minimum pacing segment size.
12741 * Lets not send anything.
12743 bbr->rc_cwnd_limited = 1;
12745 } else if (((tp->snd_wnd - ctf_outstanding(tp)) <
12746 min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) &&
12747 (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12748 bbr->r_ctl.rc_lost_bytes)) > (2 * maxseg)) &&
12749 (len < (int)(sbavail(sb) - sb_offset)) &&
12750 (TCPS_HAVEESTABLISHED(tp->t_state))) {
12752 * Here we have a send window but we have
12753 * filled it up and we can't send another pacing segment.
12754 * We also have in flight more than 2 segments
12755 * and we are not completing the sb i.e. we allow
12756 * the last bytes of the sb to go out even if
12757 * its not a full pacing segment.
12762 /* len will be >= 0 after this point. */
12763 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
12764 tcp_sndbuf_autoscale(tp, so, sendwin);
12768 if (bbr->rc_in_persist &&
12771 (len < min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs))) {
12773 * We are in persist, not doing a retransmit and don't have enough space
12774 * yet to send a full TSO. So is it at the end of the sb
12775 * if so we need to send else nuke to 0 and don't send.
12778 if (sbavail(sb) > sb_offset)
12779 sbleft = sbavail(sb) - sb_offset;
12782 if (sbleft >= min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs)) {
12783 /* not at end of sb lets not send */
12788 * Decide if we can use TCP Segmentation Offloading (if supported by
12791 * TSO may only be used if we are in a pure bulk sending state. The
12792 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP
12793 * options prevent using TSO. With TSO the TCP header is the same
12794 * (except for the sequence number) for all generated packets. This
12795 * makes it impossible to transmit any options which vary per
12796 * generated segment or packet.
12798 * IPv4 handling has a clear separation of ip options and ip header
12799 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen()
12800 * does the right thing below to provide length of just ip options
12801 * and thus checking for ipoptlen is enough to decide if ip options
12806 ipoptlen = ip6_optlen(inp);
12809 if (inp->inp_options)
12810 ipoptlen = inp->inp_options->m_len -
12811 offsetof(struct ipoption, ipopt_list);
12814 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
12816 * Pre-calculate here as we save another lookup into the darknesses
12817 * of IPsec that way and can actually decide if TSO is ok.
12820 if (isipv6 && IPSEC_ENABLED(ipv6))
12821 ipsec_optlen = IPSEC_HDRSIZE(ipv6, inp);
12827 if (IPSEC_ENABLED(ipv4))
12828 ipsec_optlen = IPSEC_HDRSIZE(ipv4, inp);
12831 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
12832 ipoptlen += ipsec_optlen;
12834 if ((tp->t_flags & TF_TSO) && V_tcp_do_tso &&
12836 (tp->t_port == 0) &&
12837 ((tp->t_flags & TF_SIGNATURE) == 0) &&
12838 tp->rcv_numsacks == 0 &&
12842 recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
12843 (long)TCP_MAXWIN << tp->rcv_scale);
12845 * Sender silly window avoidance. We transmit under the following
12846 * conditions when len is non-zero:
12848 * - We have a full segment (or more with TSO) - This is the last
12849 * buffer in a write()/send() and we are either idle or running
12850 * NODELAY - we've timed out (e.g. persist timer) - we have more
12851 * then 1/2 the maximum send window's worth of data (receiver may be
12852 * limited the window size) - we need to retransmit
12859 if (len >= p_maxseg)
12862 * NOTE! on localhost connections an 'ack' from the remote
12863 * end may occur synchronously with the output and cause us
12864 * to flush a buffer queued with moretocome. XXX
12867 if (((tp->t_flags & TF_MORETOCOME) == 0) && /* normal case */
12868 ((tp->t_flags & TF_NODELAY) ||
12869 ((uint32_t)len + (uint32_t)sb_offset) >= sbavail(&so->so_snd)) &&
12870 (tp->t_flags & TF_NOPUSH) == 0) {
12873 if ((tp->snd_una == tp->snd_max) && len) { /* Nothing outstanding */
12876 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) {
12881 * Sending of standalone window updates.
12883 * Window updates are important when we close our window due to a
12884 * full socket buffer and are opening it again after the application
12885 * reads data from it. Once the window has opened again and the
12886 * remote end starts to send again the ACK clock takes over and
12887 * provides the most current window information.
12889 * We must avoid the silly window syndrome whereas every read from
12890 * the receive buffer, no matter how small, causes a window update
12891 * to be sent. We also should avoid sending a flurry of window
12892 * updates when the socket buffer had queued a lot of data and the
12893 * application is doing small reads.
12895 * Prevent a flurry of pointless window updates by only sending an
12896 * update when we can increase the advertized window by more than
12897 * 1/4th of the socket buffer capacity. When the buffer is getting
12898 * full or is very small be more aggressive and send an update
12899 * whenever we can increase by two mss sized segments. In all other
12900 * situations the ACK's to new incoming data will carry further
12901 * window increases.
12903 * Don't send an independent window update if a delayed ACK is
12904 * pending (it will get piggy-backed on it) or the remote side
12905 * already has done a half-close and won't send more data. Skip
12906 * this if the connection is in T/TCP half-open state.
12908 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
12909 !(tp->t_flags & TF_DELACK) &&
12910 !TCPS_HAVERCVDFIN(tp->t_state)) {
12911 /* Check to see if we should do a window update */
12912 if (bbr_window_update_needed(tp, so, recwin, maxseg))
12916 * Send if we owe the peer an ACK, RST, SYN. ACKNOW
12917 * is also a catch-all for the retransmit timer timeout case.
12919 if (tp->t_flags & TF_ACKNOW) {
12922 if (flags & TH_RST) {
12923 /* Always send a RST if one is due */
12926 if ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0) {
12930 * If our state indicates that FIN should be sent and we have not
12931 * yet done so, then we need to send.
12933 if (flags & TH_FIN &&
12934 ((tp->t_flags & TF_SENTFIN) == 0)) {
12938 * No reason to send a segment, just return.
12941 SOCKBUF_UNLOCK(sb);
12942 just_return_nolock:
12944 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0);
12945 if (bbr->rc_no_pacing)
12947 if (tot_len == 0) {
12948 if ((ctf_outstanding(tp) + min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) >=
12950 BBR_STAT_INC(bbr_rwnd_limited);
12951 app_limited = BBR_JR_RWND_LIMITED;
12952 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp));
12953 if ((bbr->rc_in_persist == 0) &&
12954 TCPS_HAVEESTABLISHED(tp->t_state) &&
12955 (tp->snd_max == tp->snd_una) &&
12956 sbavail(&tp->t_inpcb->inp_socket->so_snd)) {
12957 /* No send window.. we must enter persist */
12958 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__);
12960 } else if (ctf_outstanding(tp) >= sbavail(sb)) {
12961 BBR_STAT_INC(bbr_app_limited);
12962 app_limited = BBR_JR_APP_LIMITED;
12963 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp));
12964 } else if ((ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12965 bbr->r_ctl.rc_lost_bytes)) + p_maxseg) >= tp->snd_cwnd) {
12966 BBR_STAT_INC(bbr_cwnd_limited);
12967 app_limited = BBR_JR_CWND_LIMITED;
12968 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12969 bbr->r_ctl.rc_lost_bytes)));
12970 bbr->rc_cwnd_limited = 1;
12972 BBR_STAT_INC(bbr_app_limited);
12973 app_limited = BBR_JR_APP_LIMITED;
12974 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp));
12976 bbr->r_ctl.rc_hptsi_agg_delay = 0;
12977 bbr->r_agg_early_set = 0;
12978 bbr->r_ctl.rc_agg_early = 0;
12979 bbr->r_ctl.rc_last_delay_val = 0;
12980 } else if (bbr->rc_use_google == 0)
12981 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0);
12982 /* Are we app limited? */
12983 if ((app_limited == BBR_JR_APP_LIMITED) ||
12984 (app_limited == BBR_JR_RWND_LIMITED)) {
12986 * We are application limited.
12988 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
12989 bbr->r_ctl.rc_lost_bytes)) + bbr->r_ctl.rc_delivered);
12992 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_JUSTRET], 1);
12993 /* Dont update the time if we did not send */
12994 bbr->r_ctl.rc_last_delay_val = 0;
12995 bbr->rc_output_starts_timer = 1;
12996 bbr_start_hpts_timer(bbr, tp, cts, 9, slot, tot_len);
12997 bbr_log_type_just_return(bbr, cts, tot_len, hpts_calling, app_limited, p_maxseg, len);
12998 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
12999 /* Make sure snd_nxt is drug up */
13000 tp->snd_nxt = tp->snd_max;
13005 if (doing_tlp == 0) {
13007 * Data not a TLP, and its not the rxt firing. If it is the
13008 * rxt firing, we want to leave the tlp_in_progress flag on
13009 * so we don't send another TLP. It has to be a rack timer
13010 * or normal send (response to acked data) to clear the tlp
13011 * in progress flag.
13013 bbr->rc_tlp_in_progress = 0;
13014 bbr->rc_tlp_rtx_out = 0;
13019 bbr->rc_tlp_in_progress = 1;
13021 bbr_timer_cancel(bbr, __LINE__, cts);
13023 if (sbused(sb) > 0) {
13025 * This is sub-optimal. We only send a stand alone
13026 * FIN on its own segment.
13028 if (flags & TH_FIN) {
13030 if ((len == 0) && ((tp->t_flags & TF_ACKNOW) == 0)) {
13031 /* Lets not send this */
13039 * We do *not* send a FIN on a retransmit if it has data.
13040 * The if clause here where len > 1 should never come true.
13043 (((rsm->r_flags & BBR_HAS_FIN) == 0) &&
13044 (flags & TH_FIN))) {
13049 SOCKBUF_LOCK_ASSERT(sb);
13051 if ((tp->snd_una == tp->snd_max) &&
13052 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) {
13054 * This qualifies as a RTT_PROBE session since we
13055 * drop the data outstanding to nothing and waited
13056 * more than bbr_rtt_probe_time.
13058 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0);
13059 bbr_set_reduced_rtt(bbr, cts, __LINE__);
13062 tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT;
13064 tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT;
13067 * Before ESTABLISHED, force sending of initial options unless TCP
13068 * set not to do any options. NOTE: we assume that the IP/TCP header
13069 * plus TCP options always fit in a single mbuf, leaving room for a
13070 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr)
13071 * + optlen <= MCLBYTES
13076 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
13079 hdrlen = sizeof(struct tcpiphdr);
13082 * Compute options for segment. We only have to care about SYN and
13083 * established connection segments. Options for SYN-ACK segments
13084 * are handled in TCP syncache.
13088 if ((tp->t_flags & TF_NOOPT) == 0) {
13089 /* Maximum segment size. */
13090 if (flags & TH_SYN) {
13091 to.to_mss = tcp_mssopt(&inp->inp_inc);
13092 #ifdef NETFLIX_TCPOUDP
13094 to.to_mss -= V_tcp_udp_tunneling_overhead;
13096 to.to_flags |= TOF_MSS;
13098 * On SYN or SYN|ACK transmits on TFO connections,
13099 * only include the TFO option if it is not a
13100 * retransmit, as the presence of the TFO option may
13101 * have caused the original SYN or SYN|ACK to have
13102 * been dropped by a middlebox.
13104 if (IS_FASTOPEN(tp->t_flags) &&
13105 (tp->t_rxtshift == 0)) {
13106 if (tp->t_state == TCPS_SYN_RECEIVED) {
13107 to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN;
13109 (u_int8_t *)&tp->t_tfo_cookie.server;
13110 to.to_flags |= TOF_FASTOPEN;
13112 } else if (tp->t_state == TCPS_SYN_SENT) {
13114 tp->t_tfo_client_cookie_len;
13116 tp->t_tfo_cookie.client;
13117 to.to_flags |= TOF_FASTOPEN;
13122 /* Window scaling. */
13123 if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
13124 to.to_wscale = tp->request_r_scale;
13125 to.to_flags |= TOF_SCALE;
13128 if ((tp->t_flags & TF_RCVD_TSTMP) ||
13129 ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
13130 to.to_tsval = tcp_tv_to_mssectick(&bbr->rc_tv) + tp->ts_offset;
13131 to.to_tsecr = tp->ts_recent;
13132 to.to_flags |= TOF_TS;
13133 local_options += TCPOLEN_TIMESTAMP + 2;
13135 /* Set receive buffer autosizing timestamp. */
13136 if (tp->rfbuf_ts == 0 &&
13137 (so->so_rcv.sb_flags & SB_AUTOSIZE))
13138 tp->rfbuf_ts = tcp_tv_to_mssectick(&bbr->rc_tv);
13139 /* Selective ACK's. */
13140 if (flags & TH_SYN)
13141 to.to_flags |= TOF_SACKPERM;
13142 else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
13143 tp->rcv_numsacks > 0) {
13144 to.to_flags |= TOF_SACK;
13145 to.to_nsacks = tp->rcv_numsacks;
13146 to.to_sacks = (u_char *)tp->sackblks;
13148 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
13149 /* TCP-MD5 (RFC2385). */
13150 if (tp->t_flags & TF_SIGNATURE)
13151 to.to_flags |= TOF_SIGNATURE;
13152 #endif /* TCP_SIGNATURE */
13154 /* Processing the options. */
13155 hdrlen += (optlen = tcp_addoptions(&to, opt));
13157 * If we wanted a TFO option to be added, but it was unable
13158 * to fit, ensure no data is sent.
13160 if (IS_FASTOPEN(tp->t_flags) && wanted_cookie &&
13161 !(to.to_flags & TOF_FASTOPEN))
13164 #ifdef NETFLIX_TCPOUDP
13166 if (V_tcp_udp_tunneling_port == 0) {
13167 /* The port was removed?? */
13168 SOCKBUF_UNLOCK(&so->so_snd);
13169 return (EHOSTUNREACH);
13171 hdrlen += sizeof(struct udphdr);
13176 ipoptlen = ip6_optlen(tp->t_inpcb);
13179 if (tp->t_inpcb->inp_options)
13180 ipoptlen = tp->t_inpcb->inp_options->m_len -
13181 offsetof(struct ipoption, ipopt_list);
13185 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
13186 ipoptlen += ipsec_optlen;
13188 if (bbr->rc_last_options != local_options) {
13190 * Cache the options length this generally does not change
13191 * on a connection. We use this to calculate TSO.
13193 bbr->rc_last_options = local_options;
13195 maxseg = tp->t_maxseg - (ipoptlen + optlen);
13196 p_maxseg = min(maxseg, pace_max_segs);
13198 * Adjust data length if insertion of options will bump the packet
13199 * length beyond the t_maxseg length. Clear the FIN bit because we
13200 * cut off the tail of the segment.
13203 /* force TSO for so TLS offload can get mss */
13204 if (sb->sb_flags & SB_TLS_IFNET) {
13209 if (len > maxseg) {
13210 if (len != 0 && (flags & TH_FIN)) {
13217 /* extract TSO information */
13218 if_hw_tsomax = tp->t_tsomax;
13219 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
13220 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
13221 KASSERT(ipoptlen == 0,
13222 ("%s: TSO can't do IP options", __func__));
13225 * Check if we should limit by maximum payload
13228 if (if_hw_tsomax != 0) {
13229 /* compute maximum TSO length */
13230 max_len = (if_hw_tsomax - hdrlen -
13232 if (max_len <= 0) {
13234 } else if (len > max_len) {
13239 * Prevent the last segment from being fractional
13240 * unless the send sockbuf can be emptied:
13242 if (((sb_offset + len) < sbavail(sb)) &&
13244 moff = len % (uint32_t)maxseg;
13250 * In case there are too many small fragments don't
13253 if (len <= maxseg) {
13258 /* Not doing TSO */
13259 if (optlen + ipoptlen >= tp->t_maxseg) {
13261 * Since we don't have enough space to put
13262 * the IP header chain and the TCP header in
13263 * one packet as required by RFC 7112, don't
13264 * send it. Also ensure that at least one
13265 * byte of the payload can be put into the
13268 SOCKBUF_UNLOCK(&so->so_snd);
13276 /* Not doing TSO */
13277 if_hw_tsomaxsegcount = 0;
13280 KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET,
13281 ("%s: len > IP_MAXPACKET", __func__));
13284 if (max_linkhdr + hdrlen > MCLBYTES)
13286 if (max_linkhdr + hdrlen > MHLEN)
13288 panic("tcphdr too big");
13291 * This KASSERT is here to catch edge cases at a well defined place.
13292 * Before, those had triggered (random) panic conditions further
13295 #ifdef BBR_INVARIANTS
13297 if (SEQ_LT(rsm->r_start, tp->snd_una)) {
13298 panic("RSM:%p TP:%p bbr:%p start:%u is < snd_una:%u",
13299 rsm, tp, bbr, rsm->r_start, tp->snd_una);
13303 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
13305 (flags & TH_FIN) &&
13308 * We have outstanding data, don't send a fin by itself!.
13314 * Grab a header mbuf, attaching a copy of data to be transmitted,
13315 * and initialize the header from the template for sends on this
13323 * We place a limit on sending with hptsi.
13325 if ((rsm == NULL) && len > pace_max_segs)
13326 len = pace_max_segs;
13330 if (MHLEN < hdrlen + max_linkhdr)
13331 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
13334 m = m_gethdr(M_NOWAIT, MT_DATA);
13337 BBR_STAT_INC(bbr_failed_mbuf_aloc);
13338 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0);
13339 SOCKBUF_UNLOCK(sb);
13344 m->m_data += max_linkhdr;
13347 * Start the m_copy functions from the closest mbuf to the
13348 * sb_offset in the socket buffer chain.
13350 if ((sb_offset > sbavail(sb)) || ((len + sb_offset) > sbavail(sb))) {
13351 #ifdef BBR_INVARIANTS
13352 if ((len + sb_offset) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0)))
13353 panic("tp:%p bbr:%p len:%u sb_offset:%u sbavail:%u rsm:%p %u:%u:%u",
13354 tp, bbr, len, sb_offset, sbavail(sb), rsm,
13361 * In this messed up situation we have two choices,
13362 * a) pretend the send worked, and just start timers
13363 * and what not (not good since that may lead us
13364 * back here a lot). <or> b) Send the lowest segment
13365 * in the map. <or> c) Drop the connection. Lets do
13366 * <b> which if it continues to happen will lead to
13367 * <c> via timeouts.
13369 BBR_STAT_INC(bbr_offset_recovery);
13370 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map);
13377 if (rsm->r_start != tp->snd_una) {
13379 * Things are really messed up, <c>
13380 * is the only thing to do.
13382 BBR_STAT_INC(bbr_offset_drop);
13383 tcp_set_inp_to_drop(inp, EFAULT);
13384 SOCKBUF_UNLOCK(sb);
13388 len = rsm->r_end - rsm->r_start;
13390 if (len > sbavail(sb))
13395 mb = sbsndptr_noadv(sb, sb_offset, &moff);
13396 if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) {
13397 m_copydata(mb, moff, (int)len,
13398 mtod(m, caddr_t)+hdrlen);
13400 sbsndptr_adv(sb, mb, len);
13403 struct sockbuf *msb;
13409 #ifdef BBR_INVARIANTS
13410 if ((len + moff) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0))) {
13412 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 ",
13413 tp, bbr, len, moff,
13415 tp->snd_una, rsm->r_flags, rsm->r_start,
13418 doing_tlp, sack_rxmit);
13420 panic("tp:%p bbr:%p len:%u moff:%u sbavail:%u sb_offset:%u snd_una:%u",
13421 tp, bbr, len, moff, sbavail(sb), sb_offset, tp->snd_una);
13426 m->m_next = tcp_m_copym(
13428 if_hw_tsomaxsegcount,
13429 if_hw_tsomaxsegsize, msb,
13430 ((rsm == NULL) ? hw_tls : 0)
13431 #ifdef NETFLIX_COPY_ARGS
13435 if (len <= maxseg && !force_tso) {
13437 * Must have ran out of mbufs for the copy
13438 * shorten it to no longer need tso. Lets
13439 * not put on sendalot since we are low on
13444 if (m->m_next == NULL) {
13445 SOCKBUF_UNLOCK(sb);
13452 #ifdef BBR_INVARIANTS
13453 if (tso && len < maxseg) {
13454 panic("tp:%p tso on, but len:%d < maxseg:%d",
13457 if (tso && if_hw_tsomaxsegcount) {
13458 int32_t seg_cnt = 0;
13466 if (seg_cnt > if_hw_tsomaxsegcount) {
13467 panic("seg_cnt:%d > max:%d", seg_cnt, if_hw_tsomaxsegcount);
13472 * If we're sending everything we've got, set PUSH. (This
13473 * will keep happy those implementations which only give
13474 * data to the user when a buffer fills or a PUSH comes in.)
13476 if (sb_offset + len == sbused(sb) &&
13478 !(flags & TH_SYN)) {
13481 SOCKBUF_UNLOCK(sb);
13483 SOCKBUF_UNLOCK(sb);
13484 if (tp->t_flags & TF_ACKNOW)
13485 KMOD_TCPSTAT_INC(tcps_sndacks);
13486 else if (flags & (TH_SYN | TH_FIN | TH_RST))
13487 KMOD_TCPSTAT_INC(tcps_sndctrl);
13489 KMOD_TCPSTAT_INC(tcps_sndwinup);
13491 m = m_gethdr(M_NOWAIT, MT_DATA);
13493 BBR_STAT_INC(bbr_failed_mbuf_aloc);
13494 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0);
13496 /* Fudge the send time since we could not send */
13501 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
13503 M_ALIGN(m, hdrlen);
13506 m->m_data += max_linkhdr;
13509 SOCKBUF_UNLOCK_ASSERT(sb);
13510 m->m_pkthdr.rcvif = (struct ifnet *)0;
13512 mac_inpcb_create_mbuf(inp, m);
13516 ip6 = mtod(m, struct ip6_hdr *);
13517 #ifdef NETFLIX_TCPOUDP
13519 udp = (struct udphdr *)((caddr_t)ip6 + ipoptlen + sizeof(struct ip6_hdr));
13520 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
13521 udp->uh_dport = tp->t_port;
13522 ulen = hdrlen + len - sizeof(struct ip6_hdr);
13523 udp->uh_ulen = htons(ulen);
13524 th = (struct tcphdr *)(udp + 1);
13527 th = (struct tcphdr *)(ip6 + 1);
13529 #ifdef NETFLIX_TCPOUDP
13532 tcpip_fillheaders(inp,
13533 #ifdef NETFLIX_TCPOUDP
13540 ip = mtod(m, struct ip *);
13542 ipov = (struct ipovly *)ip;
13544 #ifdef NETFLIX_TCPOUDP
13546 udp = (struct udphdr *)((caddr_t)ip + ipoptlen + sizeof(struct ip));
13547 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
13548 udp->uh_dport = tp->t_port;
13549 ulen = hdrlen + len - sizeof(struct ip);
13550 udp->uh_ulen = htons(ulen);
13551 th = (struct tcphdr *)(udp + 1);
13554 th = (struct tcphdr *)(ip + 1);
13555 tcpip_fillheaders(inp,
13556 #ifdef NETFLIX_TCPOUDP
13562 * If we are doing retransmissions, then snd_nxt will not reflect
13563 * the first unsent octet. For ACK only packets, we do not want the
13564 * sequence number of the retransmitted packet, we want the sequence
13565 * number of the next unsent octet. So, if there is no data (and no
13566 * SYN or FIN), use snd_max instead of snd_nxt when filling in
13567 * ti_seq. But if we are in persist state, snd_max might reflect
13568 * one byte beyond the right edge of the window, so use snd_nxt in
13569 * that case, since we know we aren't doing a retransmission.
13570 * (retransmit and persist are mutually exclusive...)
13572 if (sack_rxmit == 0) {
13573 if (len && ((flags & (TH_FIN | TH_SYN | TH_RST)) == 0)) {
13574 /* New data (including new persists) */
13575 th->th_seq = htonl(tp->snd_max);
13576 bbr_seq = tp->snd_max;
13577 } else if (flags & TH_SYN) {
13578 /* Syn's always send from iss */
13579 th->th_seq = htonl(tp->iss);
13581 } else if (flags & TH_FIN) {
13582 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN) {
13584 * If we sent the fin already its 1 minus
13587 th->th_seq = (htonl(tp->snd_max - 1));
13588 bbr_seq = (tp->snd_max - 1);
13590 /* First time FIN use snd_max */
13591 th->th_seq = htonl(tp->snd_max);
13592 bbr_seq = tp->snd_max;
13594 } else if (flags & TH_RST) {
13596 * For a Reset send the last cum ack in sequence
13597 * (this like any other choice may still generate a
13598 * challenge ack, if a ack-update packet is in
13601 th->th_seq = htonl(tp->snd_una);
13602 bbr_seq = tp->snd_una;
13605 * len == 0 and not persist we use snd_max, sending
13606 * an ack unless we have sent the fin then its 1
13610 * XXXRRS Question if we are in persists and we have
13611 * nothing outstanding to send and we have not sent
13612 * a FIN, we will send an ACK. In such a case it
13613 * might be better to send (tp->snd_una - 1) which
13614 * would force the peer to ack.
13616 if (tp->t_flags & TF_SENTFIN) {
13617 th->th_seq = htonl(tp->snd_max - 1);
13618 bbr_seq = (tp->snd_max - 1);
13620 th->th_seq = htonl(tp->snd_max);
13621 bbr_seq = tp->snd_max;
13625 /* All retransmits use the rsm to guide the send */
13626 th->th_seq = htonl(rsm->r_start);
13627 bbr_seq = rsm->r_start;
13629 th->th_ack = htonl(tp->rcv_nxt);
13631 bcopy(opt, th + 1, optlen);
13632 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
13634 th->th_flags = flags;
13636 * Calculate receive window. Don't shrink window, but avoid silly
13639 if ((flags & TH_RST) || ((recwin < (so->so_rcv.sb_hiwat / 4) &&
13642 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) &&
13643 recwin < (tp->rcv_adv - tp->rcv_nxt))
13644 recwin = (tp->rcv_adv - tp->rcv_nxt);
13645 if (recwin > TCP_MAXWIN << tp->rcv_scale)
13646 recwin = TCP_MAXWIN << tp->rcv_scale;
13649 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or
13650 * <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> case is
13651 * handled in syncache.
13653 if (flags & TH_SYN)
13654 th->th_win = htons((u_short)
13655 (min(sbspace(&so->so_rcv), TCP_MAXWIN)));
13657 /* Avoid shrinking window with window scaling. */
13658 recwin = roundup2(recwin, 1 << tp->rcv_scale);
13659 th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
13662 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0
13663 * window. This may cause the remote transmitter to stall. This
13664 * flag tells soreceive() to disable delayed acknowledgements when
13665 * draining the buffer. This can occur if the receiver is
13666 * attempting to read more data than can be buffered prior to
13667 * transmitting on the connection.
13669 if (th->th_win == 0) {
13670 tp->t_sndzerowin++;
13671 tp->t_flags |= TF_RXWIN0SENT;
13673 tp->t_flags &= ~TF_RXWIN0SENT;
13675 * We don't support urgent data, but drag along
13676 * the pointer in case of a stack switch.
13678 tp->snd_up = tp->snd_una;
13680 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
13681 if (to.to_flags & TOF_SIGNATURE) {
13683 * Calculate MD5 signature and put it into the place
13684 * determined before. NOTE: since TCP options buffer doesn't
13685 * point into mbuf's data, calculate offset and use it.
13687 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
13688 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
13690 * Do not send segment if the calculation of MD5
13691 * digest has failed.
13699 * Put TCP length in extended header, and then checksum extended
13702 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
13706 * ip6_plen is not need to be filled now, and will be filled
13709 #ifdef NETFLIX_TCPOUDP
13711 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
13712 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
13713 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
13714 th->th_sum = htons(0);
13715 UDPSTAT_INC(udps_opackets);
13718 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
13719 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
13720 th->th_sum = in6_cksum_pseudo(ip6, sizeof(struct tcphdr) +
13721 optlen + len, IPPROTO_TCP, 0);
13722 #ifdef NETFLIX_TCPOUDP
13727 #if defined(INET6) && defined(INET)
13732 #ifdef NETFLIX_TCPOUDP
13734 m->m_pkthdr.csum_flags = CSUM_UDP;
13735 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
13736 udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
13737 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
13738 th->th_sum = htons(0);
13739 UDPSTAT_INC(udps_opackets);
13742 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP;
13743 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
13744 th->th_sum = in_pseudo(ip->ip_src.s_addr,
13745 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
13746 IPPROTO_TCP + len + optlen));
13747 #ifdef NETFLIX_TCPOUDP
13750 /* IP version must be set here for ipv4/ipv6 checking later */
13751 KASSERT(ip->ip_v == IPVERSION,
13752 ("%s: IP version incorrect: %d", __func__, ip->ip_v));
13757 * Enable TSO and specify the size of the segments. The TCP pseudo
13758 * header checksum is always provided. XXX: Fixme: This is currently
13759 * not the case for IPv6.
13761 if (tso || force_tso) {
13762 KASSERT(force_tso || len > maxseg,
13763 ("%s: len:%d <= tso_segsz:%d", __func__, len, maxseg));
13764 m->m_pkthdr.csum_flags |= CSUM_TSO;
13765 csum_flags |= CSUM_TSO;
13766 m->m_pkthdr.tso_segsz = maxseg;
13768 KASSERT(len + hdrlen == m_length(m, NULL),
13769 ("%s: mbuf chain different than expected: %d + %u != %u",
13770 __func__, len, hdrlen, m_length(m, NULL)));
13773 /* Run HHOOK_TC_ESTABLISHED_OUT helper hooks. */
13774 hhook_run_tcp_est_out(tp, th, &to, len, tso);
13780 if (so->so_options & SO_DEBUG) {
13787 save = ipov->ih_len;
13788 ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen +
13789 * (th->th_off << 2) */ );
13791 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
13795 ipov->ih_len = save;
13797 #endif /* TCPDEBUG */
13799 /* Log to the black box */
13800 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
13801 union tcp_log_stackspecific log;
13803 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts);
13804 /* Record info on type of transmission */
13805 log.u_bbr.flex1 = bbr->r_ctl.rc_hptsi_agg_delay;
13806 log.u_bbr.flex2 = (bbr->r_recovery_bw << 3);
13807 log.u_bbr.flex3 = maxseg;
13808 log.u_bbr.flex4 = delay_calc;
13809 /* Encode filled_all into the upper flex5 bit */
13810 log.u_bbr.flex5 = bbr->rc_past_init_win;
13811 log.u_bbr.flex5 <<= 1;
13812 log.u_bbr.flex5 |= bbr->rc_no_pacing;
13813 log.u_bbr.flex5 <<= 29;
13815 log.u_bbr.flex5 |= 0x80000000;
13816 log.u_bbr.flex5 |= tp->t_maxseg;
13817 log.u_bbr.flex6 = bbr->r_ctl.rc_pace_max_segs;
13818 log.u_bbr.flex7 = (bbr->rc_bbr_state << 8) | bbr_state_val(bbr);
13819 /* lets poke in the low and the high here for debugging */
13820 log.u_bbr.pkts_out = bbr->rc_tp->t_maxseg;
13821 if (rsm || sack_rxmit) {
13823 log.u_bbr.flex8 = 2;
13825 log.u_bbr.flex8 = 1;
13827 log.u_bbr.flex8 = 0;
13829 lgb = tcp_log_event_(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK,
13830 len, &log, false, NULL, NULL, 0, tv);
13835 * Fill in IP length and desired time to live and send to IP level.
13836 * There should be a better way to handle ttl and tos; we could keep
13837 * them in the template, but need a way to checksum without them.
13840 * m->m_pkthdr.len should have been set before cksum calcuration,
13841 * because in6_cksum() need it.
13846 * we separately set hoplimit for every segment, since the
13847 * user might want to change the value via setsockopt. Also,
13848 * desired default hop limit might be changed via Neighbor
13851 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
13854 * Set the packet size here for the benefit of DTrace
13855 * probes. ip6_output() will set it properly; it's supposed
13856 * to include the option header lengths as well.
13858 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
13860 if (V_path_mtu_discovery && maxseg > V_tcp_minmss)
13861 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
13863 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
13865 if (tp->t_state == TCPS_SYN_SENT)
13866 TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th);
13868 TCP_PROBE5(send, NULL, tp, ip6, tp, th);
13869 /* TODO: IPv6 IP6TOS_ECT bit on */
13870 error = ip6_output(m, inp->in6p_outputopts,
13872 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0),
13875 if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL)
13876 mtu = inp->inp_route6.ro_nh->nh_mtu;
13879 #if defined(INET) && defined(INET6)
13884 ip->ip_len = htons(m->m_pkthdr.len);
13887 ip->ip_ttl = in6_selecthlim(inp, NULL);
13890 * If we do path MTU discovery, then we set DF on every
13891 * packet. This might not be the best thing to do according
13892 * to RFC3390 Section 2. However the tcp hostcache migitates
13893 * the problem so it affects only the first tcp connection
13896 * NB: Don't set DF on small MTU/MSS to have a safe
13899 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
13900 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
13901 if (tp->t_port == 0 || len < V_tcp_minmss) {
13902 ip->ip_off |= htons(IP_DF);
13905 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
13908 if (tp->t_state == TCPS_SYN_SENT)
13909 TCP_PROBE5(connect__request, NULL, tp, ip, tp, th);
13911 TCP_PROBE5(send, NULL, tp, ip, tp, th);
13913 error = ip_output(m, inp->inp_options, &inp->inp_route,
13914 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0,
13916 if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL)
13917 mtu = inp->inp_route.ro_nh->nh_mtu;
13923 lgb->tlb_errno = error;
13927 * In transmit state, time the transmission and arrange for the
13928 * retransmit. In persist state, just set snd_max.
13931 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
13932 (tp->t_flags & TF_SACK_PERMIT) &&
13933 tp->rcv_numsacks > 0)
13934 tcp_clean_dsack_blocks(tp);
13935 /* We sent an ack clear the bbr_segs_rcvd count */
13936 bbr->output_error_seen = 0;
13937 bbr->oerror_cnt = 0;
13938 bbr->bbr_segs_rcvd = 0;
13940 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_SNDACK], 1);
13943 (len >= bbr->r_ctl.rc_pace_max_segs))
13944 BBR_STAT_INC(bbr_meets_tso_thresh);
13947 BBR_STAT_INC(bbr_miss_tlp);
13948 bbr_log_type_hrdwtso(tp, bbr, len, 1, what_we_can);
13952 BBR_STAT_INC(bbr_miss_retran);
13953 bbr_log_type_hrdwtso(tp, bbr, len, 2, what_we_can);
13954 } else if ((ctf_outstanding(tp) + bbr->r_ctl.rc_pace_max_segs) > sbavail(sb)) {
13955 BBR_STAT_INC(bbr_miss_tso_app);
13956 bbr_log_type_hrdwtso(tp, bbr, len, 3, what_we_can);
13957 } else if ((ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
13958 bbr->r_ctl.rc_lost_bytes)) + bbr->r_ctl.rc_pace_max_segs) > tp->snd_cwnd) {
13959 BBR_STAT_INC(bbr_miss_tso_cwnd);
13960 bbr_log_type_hrdwtso(tp, bbr, len, 4, what_we_can);
13961 } else if ((ctf_outstanding(tp) + bbr->r_ctl.rc_pace_max_segs) > tp->snd_wnd) {
13962 BBR_STAT_INC(bbr_miss_tso_rwnd);
13963 bbr_log_type_hrdwtso(tp, bbr, len, 5, what_we_can);
13965 BBR_STAT_INC(bbr_miss_unknown);
13966 bbr_log_type_hrdwtso(tp, bbr, len, 6, what_we_can);
13970 /* Do accounting for new sends */
13971 if ((len > 0) && (rsm == NULL)) {
13973 if (tp->snd_una == tp->snd_max) {
13975 * Special case to match google, when
13976 * nothing is in flight the delivered
13977 * time does get updated to the current
13978 * time (see tcp_rate_bsd.c).
13980 bbr->r_ctl.rc_del_time = cts;
13982 if (len >= maxseg) {
13983 idx = (len / maxseg) + 3;
13984 if (idx >= TCP_MSS_ACCT_ATIMER)
13985 counter_u64_add(bbr_out_size[(TCP_MSS_ACCT_ATIMER - 1)], 1);
13987 counter_u64_add(bbr_out_size[idx], 1);
13989 /* smaller than a MSS */
13990 idx = len / (bbr_hptsi_bytes_min - bbr->rc_last_options);
13991 if (idx >= TCP_MSS_SMALL_MAX_SIZE_DIV)
13992 idx = (TCP_MSS_SMALL_MAX_SIZE_DIV - 1);
13993 counter_u64_add(bbr_out_size[(idx + TCP_MSS_SMALL_SIZE_OFF)], 1);
13999 * We must do the send accounting before we log the output,
14000 * otherwise the state of the rsm could change and we account to the
14004 bbr_do_send_accounting(tp, bbr, rsm, len, error);
14006 if (tp->snd_una == tp->snd_max)
14007 bbr->r_ctl.rc_tlp_rxt_last_time = cts;
14010 bbr_log_output(bbr, tp, &to, len, bbr_seq, (uint8_t) flags, error,
14011 cts, mb, &abandon, rsm, 0, sb);
14014 * If bbr_log_output destroys the TCB or sees a TH_RST being
14015 * sent we should hit this condition.
14019 if (bbr->rc_in_persist == 0) {
14021 * Advance snd_nxt over sequence space of this segment.
14024 /* We don't log or do anything with errors */
14027 if (tp->snd_una == tp->snd_max &&
14028 (len || (flags & (TH_SYN | TH_FIN)))) {
14030 * Update the time we just added data since none was
14033 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__);
14034 bbr->rc_tp->t_acktime = ticks;
14036 if (flags & (TH_SYN | TH_FIN) && (rsm == NULL)) {
14037 if (flags & TH_SYN) {
14039 * Smack the snd_max to iss + 1
14040 * if its a FO we will add len below.
14042 tp->snd_max = tp->iss + 1;
14044 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) {
14046 tp->t_flags |= TF_SENTFIN;
14049 if (sack_rxmit == 0)
14050 tp->snd_max += len;
14052 if ((error == 0) && len)
14055 /* Persists case */
14056 int32_t xlen = len;
14061 if (flags & TH_SYN)
14063 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) {
14065 tp->t_flags |= TF_SENTFIN;
14067 if (xlen && (tp->snd_una == tp->snd_max)) {
14069 * Update the time we just added data since none was
14072 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__);
14073 bbr->rc_tp->t_acktime = ticks;
14075 if (sack_rxmit == 0)
14076 tp->snd_max += xlen;
14077 tot_len += (len + optlen + ipoptlen);
14082 * Failures do not advance the seq counter above. For the
14083 * case of ENOBUFS we will fall out and become ack-clocked.
14084 * capping the cwnd at the current flight.
14085 * Everything else will just have to retransmit with the timer
14088 SOCKBUF_UNLOCK_ASSERT(sb);
14089 BBR_STAT_INC(bbr_saw_oerr);
14090 /* Clear all delay/early tracks */
14091 bbr->r_ctl.rc_hptsi_agg_delay = 0;
14092 bbr->r_ctl.rc_agg_early = 0;
14093 bbr->r_agg_early_set = 0;
14094 bbr->output_error_seen = 1;
14095 if (bbr->oerror_cnt < 0xf)
14097 if (bbr_max_net_error_cnt && (bbr->oerror_cnt >= bbr_max_net_error_cnt)) {
14098 /* drop the session */
14099 tcp_set_inp_to_drop(inp, ENETDOWN);
14104 * Make this guy have to get ack's to send
14105 * more but lets make sure we don't
14106 * slam him below a T-O (1MSS).
14108 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) {
14109 tp->snd_cwnd = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
14110 bbr->r_ctl.rc_lost_bytes)) - maxseg;
14111 if (tp->snd_cwnd < maxseg)
14112 tp->snd_cwnd = maxseg;
14114 slot = (bbr_error_base_paceout + 1) << bbr->oerror_cnt;
14115 BBR_STAT_INC(bbr_saw_enobuf);
14116 if (bbr->bbr_hdrw_pacing)
14117 counter_u64_add(bbr_hdwr_pacing_enobuf, 1);
14119 counter_u64_add(bbr_nohdwr_pacing_enobuf, 1);
14121 * Here even in the enobuf's case we want to do our
14122 * state update. The reason being we may have been
14123 * called by the input function. If so we have had
14130 * For some reason the interface we used initially
14131 * to send segments changed to another or lowered
14132 * its MTU. If TSO was active we either got an
14133 * interface without TSO capabilits or TSO was
14134 * turned off. If we obtained mtu from ip_output()
14135 * then update it and try again.
14137 /* Turn on tracing (or try to) */
14141 old_maxseg = tp->t_maxseg;
14142 BBR_STAT_INC(bbr_saw_emsgsiz);
14143 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, csum_flags, tso, cts);
14145 tcp_mss_update(tp, -1, mtu, NULL, NULL);
14146 if (old_maxseg <= tp->t_maxseg) {
14147 /* Huh it did not shrink? */
14148 tp->t_maxseg = old_maxseg - 40;
14149 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, 0, tso, cts);
14152 * Nuke all other things that can interfere
14155 if ((tot_len + len) && (len >= tp->t_maxseg)) {
14156 slot = bbr_get_pacing_delay(bbr,
14157 bbr->r_ctl.rc_bbr_hptsi_gain,
14158 (tot_len + len), cts, 0);
14159 if (slot < bbr_error_base_paceout)
14160 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt;
14162 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt;
14163 bbr->rc_output_starts_timer = 1;
14164 bbr_start_hpts_timer(bbr, tp, cts, 10, slot,
14169 tp->t_softerror = error;
14175 if (TCPS_HAVERCVDSYN(tp->t_state)) {
14176 tp->t_softerror = error;
14180 slot = (bbr_error_base_paceout + 3) << bbr->oerror_cnt;
14181 bbr->rc_output_starts_timer = 1;
14182 bbr_start_hpts_timer(bbr, tp, cts, 11, slot, 0);
14186 } else if (((tp->t_flags & TF_GPUTINPROG) == 0) &&
14189 (bbr->rc_in_persist == 0)) {
14190 tp->gput_seq = bbr_seq;
14191 tp->gput_ack = bbr_seq +
14192 min(sbavail(&so->so_snd) - sb_offset, sendwin);
14194 tp->t_flags |= TF_GPUTINPROG;
14197 KMOD_TCPSTAT_INC(tcps_sndtotal);
14198 if ((bbr->bbr_hdw_pace_ena) &&
14199 (bbr->bbr_attempt_hdwr_pace == 0) &&
14200 (bbr->rc_past_init_win) &&
14201 (bbr->rc_bbr_state != BBR_STATE_STARTUP) &&
14202 (get_filter_value(&bbr->r_ctl.rc_delrate)) &&
14203 (inp->inp_route.ro_nh &&
14204 inp->inp_route.ro_nh->nh_ifp)) {
14206 * We are past the initial window and
14207 * have at least one measurement so we
14208 * could use hardware pacing if its available.
14209 * We have an interface and we have not attempted
14210 * to setup hardware pacing, lets try to now.
14212 uint64_t rate_wanted;
14215 rate_wanted = bbr_get_hardware_rate(bbr);
14216 bbr->bbr_attempt_hdwr_pace = 1;
14217 bbr->r_ctl.crte = tcp_set_pacing_rate(bbr->rc_tp,
14218 inp->inp_route.ro_nh->nh_ifp,
14220 (RS_PACING_GEQ|RS_PACING_SUB_OK),
14222 if (bbr->r_ctl.crte) {
14223 bbr_type_log_hdwr_pacing(bbr,
14224 bbr->r_ctl.crte->ptbl->rs_ifp,
14226 bbr->r_ctl.crte->rate,
14227 __LINE__, cts, err);
14228 BBR_STAT_INC(bbr_hdwr_rl_add_ok);
14229 counter_u64_add(bbr_flows_nohdwr_pacing, -1);
14230 counter_u64_add(bbr_flows_whdwr_pacing, 1);
14231 bbr->bbr_hdrw_pacing = 1;
14232 /* Now what is our gain status? */
14233 if (bbr->r_ctl.crte->rate < rate_wanted) {
14234 /* We have a problem */
14235 bbr_setup_less_of_rate(bbr, cts,
14236 bbr->r_ctl.crte->rate, rate_wanted);
14239 bbr->gain_is_limited = 0;
14240 bbr->skip_gain = 0;
14242 tcp_bbr_tso_size_check(bbr, cts);
14244 bbr_type_log_hdwr_pacing(bbr,
14245 inp->inp_route.ro_nh->nh_ifp,
14248 __LINE__, cts, err);
14249 BBR_STAT_INC(bbr_hdwr_rl_add_fail);
14252 if (bbr->bbr_hdrw_pacing) {
14254 * Worry about cases where the route
14255 * changes or something happened that we
14256 * lost our hardware pacing possibly during
14257 * the last ip_output call.
14259 if (inp->inp_snd_tag == NULL) {
14260 /* A change during ip output disabled hw pacing? */
14261 bbr->bbr_hdrw_pacing = 0;
14262 } else if ((inp->inp_route.ro_nh == NULL) ||
14263 (inp->inp_route.ro_nh->nh_ifp != inp->inp_snd_tag->ifp)) {
14265 * We had an interface or route change,
14266 * detach from the current hdwr pacing
14267 * and setup to re-attempt next go
14270 bbr->bbr_hdrw_pacing = 0;
14271 bbr->bbr_attempt_hdwr_pace = 0;
14272 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp);
14273 tcp_bbr_tso_size_check(bbr, cts);
14277 * Data sent (as far as we can tell). If this advertises a larger
14278 * window than any other segment, then remember the size of the
14279 * advertised window. Any pending ACK has now been sent.
14281 if (SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
14282 tp->rcv_adv = tp->rcv_nxt + recwin;
14284 tp->last_ack_sent = tp->rcv_nxt;
14285 if ((error == 0) &&
14286 (bbr->r_ctl.rc_pace_max_segs > tp->t_maxseg) &&
14287 (doing_tlp == 0) &&
14291 ((flags & TH_RST) == 0) &&
14292 ((flags & TH_SYN) == 0) &&
14293 (IN_RECOVERY(tp->t_flags) == 0) &&
14294 (bbr->rc_in_persist == 0) &&
14295 (tot_len < bbr->r_ctl.rc_pace_max_segs)) {
14297 * For non-tso we need to goto again until we have sent out
14298 * enough data to match what we are hptsi out every hptsi
14301 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
14302 /* Make sure snd_nxt is drug up */
14303 tp->snd_nxt = tp->snd_max;
14311 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
14315 if ((error == 0) && (flags & TH_FIN))
14316 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN);
14317 if ((error == 0) && (flags & TH_RST))
14318 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
14319 if (((flags & (TH_RST | TH_SYN | TH_FIN)) == 0) && tot_len) {
14321 * Calculate/Re-Calculate the hptsi slot in usecs based on
14322 * what we have sent so far
14324 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0);
14325 if (bbr->rc_no_pacing)
14328 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
14330 if (bbr->rc_use_google == 0)
14331 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0);
14332 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked +
14333 bbr->r_ctl.rc_lost_bytes)));
14334 bbr->rc_output_starts_timer = 1;
14335 if (bbr->bbr_use_rack_cheat &&
14337 ((bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts)) != NULL))) {
14338 /* Rack cheats and shotguns out all rxt's 1ms apart */
14342 if (bbr->bbr_hdrw_pacing && (bbr->hw_pacing_set == 0)) {
14344 * We don't change the tso size until some number of sends
14345 * to give the hardware commands time to get down
14346 * to the interface.
14348 bbr->r_ctl.bbr_hdwr_cnt_noset_snt++;
14349 if (bbr->r_ctl.bbr_hdwr_cnt_noset_snt >= bbr_hdwr_pacing_delay_cnt) {
14350 bbr->hw_pacing_set = 1;
14351 tcp_bbr_tso_size_check(bbr, cts);
14354 bbr_start_hpts_timer(bbr, tp, cts, 12, slot, tot_len);
14355 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
14356 /* Make sure snd_nxt is drug up */
14357 tp->snd_nxt = tp->snd_max;
14364 * See bbr_output_wtime() for return values.
14367 bbr_output(struct tcpcb *tp)
14371 struct tcp_bbr *bbr;
14373 NET_EPOCH_ASSERT();
14375 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
14376 INP_WLOCK_ASSERT(tp->t_inpcb);
14377 (void)tcp_get_usecs(&tv);
14378 ret = bbr_output_wtime(tp, &tv);
14383 bbr_mtu_chg(struct tcpcb *tp)
14385 struct tcp_bbr *bbr;
14386 struct bbr_sendmap *rsm, *frsm = NULL;
14390 * The MTU has changed. a) Clear the sack filter. b) Mark everything
14391 * over the current size as SACK_PASS so a retransmit will occur.
14394 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
14395 maxseg = tp->t_maxseg - bbr->rc_last_options;
14396 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una);
14397 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) {
14398 /* Don't mess with ones acked (by sack?) */
14399 if (rsm->r_flags & BBR_ACKED)
14401 if ((rsm->r_end - rsm->r_start) > maxseg) {
14403 * We mark sack-passed on all the previous large
14404 * sends we did. This will force them to retransmit.
14406 rsm->r_flags |= BBR_SACK_PASSED;
14407 if (((rsm->r_flags & BBR_MARKED_LOST) == 0) &&
14408 bbr_is_lost(bbr, rsm, bbr->r_ctl.rc_rcvtime)) {
14409 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start;
14410 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start;
14411 rsm->r_flags |= BBR_MARKED_LOST;
14418 bbr->r_ctl.rc_resend = frsm;
14423 * bbr_ctloutput() must drop the inpcb lock before performing copyin on
14424 * socket option arguments. When it re-acquires the lock after the copy, it
14425 * has to revalidate that the connection is still valid for the socket
14429 bbr_set_sockopt(struct socket *so, struct sockopt *sopt,
14430 struct inpcb *inp, struct tcpcb *tp, struct tcp_bbr *bbr)
14432 struct epoch_tracker et;
14433 int32_t error = 0, optval;
14435 switch (sopt->sopt_name) {
14436 case TCP_RACK_PACE_MAX_SEG:
14437 case TCP_RACK_MIN_TO:
14438 case TCP_RACK_REORD_THRESH:
14439 case TCP_RACK_REORD_FADE:
14440 case TCP_RACK_TLP_THRESH:
14441 case TCP_RACK_PKT_DELAY:
14442 case TCP_BBR_ALGORITHM:
14443 case TCP_BBR_TSLIMITS:
14444 case TCP_BBR_IWINTSO:
14445 case TCP_BBR_RECFORCE:
14446 case TCP_BBR_STARTUP_PG:
14447 case TCP_BBR_DRAIN_PG:
14448 case TCP_BBR_RWND_IS_APP:
14449 case TCP_BBR_PROBE_RTT_INT:
14450 case TCP_BBR_PROBE_RTT_GAIN:
14451 case TCP_BBR_PROBE_RTT_LEN:
14452 case TCP_BBR_STARTUP_LOSS_EXIT:
14453 case TCP_BBR_USEDEL_RATE:
14454 case TCP_BBR_MIN_RTO:
14455 case TCP_BBR_MAX_RTO:
14456 case TCP_BBR_PACE_PER_SEC:
14458 case TCP_BBR_PACE_DEL_TAR:
14459 case TCP_BBR_SEND_IWND_IN_TSO:
14460 case TCP_BBR_EXTRA_STATE:
14461 case TCP_BBR_UTTER_MAX_TSO:
14462 case TCP_BBR_MIN_TOPACEOUT:
14463 case TCP_BBR_FLOOR_MIN_TSO:
14464 case TCP_BBR_TSTMP_RAISES:
14465 case TCP_BBR_POLICER_DETECT:
14466 case TCP_BBR_USE_RACK_CHEAT:
14467 case TCP_DATA_AFTER_CLOSE:
14468 case TCP_BBR_HDWR_PACE:
14469 case TCP_BBR_PACE_SEG_MAX:
14470 case TCP_BBR_PACE_SEG_MIN:
14471 case TCP_BBR_PACE_CROSS:
14472 case TCP_BBR_PACE_OH:
14473 #ifdef NETFLIX_PEAKRATE
14474 case TCP_MAXPEAKRATE:
14476 case TCP_BBR_TMR_PACE_OH:
14477 case TCP_BBR_RACK_RTT_USE:
14478 case TCP_BBR_RETRAN_WTSO:
14481 return (tcp_default_ctloutput(so, sopt, inp, tp));
14485 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
14489 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
14491 return (ECONNRESET);
14493 tp = intotcpcb(inp);
14494 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
14495 switch (sopt->sopt_name) {
14496 case TCP_BBR_PACE_PER_SEC:
14497 BBR_OPTS_INC(tcp_bbr_pace_per_sec);
14498 bbr->r_ctl.bbr_hptsi_per_second = optval;
14500 case TCP_BBR_PACE_DEL_TAR:
14501 BBR_OPTS_INC(tcp_bbr_pace_del_tar);
14502 bbr->r_ctl.bbr_hptsi_segments_delay_tar = optval;
14504 case TCP_BBR_PACE_SEG_MAX:
14505 BBR_OPTS_INC(tcp_bbr_pace_seg_max);
14506 bbr->r_ctl.bbr_hptsi_segments_max = optval;
14508 case TCP_BBR_PACE_SEG_MIN:
14509 BBR_OPTS_INC(tcp_bbr_pace_seg_min);
14510 bbr->r_ctl.bbr_hptsi_bytes_min = optval;
14512 case TCP_BBR_PACE_CROSS:
14513 BBR_OPTS_INC(tcp_bbr_pace_cross);
14514 bbr->r_ctl.bbr_cross_over = optval;
14516 case TCP_BBR_ALGORITHM:
14517 BBR_OPTS_INC(tcp_bbr_algorithm);
14518 if (optval && (bbr->rc_use_google == 0)) {
14519 /* Turn on the google mode */
14520 bbr_google_mode_on(bbr);
14521 if ((optval > 3) && (optval < 500)) {
14523 * Must be at least greater than .3%
14524 * and must be less than 50.0%.
14526 bbr->r_ctl.bbr_google_discount = optval;
14528 } else if ((optval == 0) && (bbr->rc_use_google == 1)) {
14529 /* Turn off the google mode */
14530 bbr_google_mode_off(bbr);
14533 case TCP_BBR_TSLIMITS:
14534 BBR_OPTS_INC(tcp_bbr_tslimits);
14536 bbr->rc_use_ts_limit = 1;
14537 else if (optval == 0)
14538 bbr->rc_use_ts_limit = 0;
14543 case TCP_BBR_IWINTSO:
14544 BBR_OPTS_INC(tcp_bbr_iwintso);
14545 if ((optval >= 0) && (optval < 128)) {
14548 bbr->rc_init_win = optval;
14549 twin = bbr_initial_cwnd(bbr, tp);
14550 if ((bbr->rc_past_init_win == 0) && (twin > tp->snd_cwnd))
14551 tp->snd_cwnd = twin;
14557 case TCP_BBR_STARTUP_PG:
14558 BBR_OPTS_INC(tcp_bbr_startup_pg);
14559 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE)) {
14560 bbr->r_ctl.rc_startup_pg = optval;
14561 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) {
14562 bbr->r_ctl.rc_bbr_hptsi_gain = optval;
14567 case TCP_BBR_DRAIN_PG:
14568 BBR_OPTS_INC(tcp_bbr_drain_pg);
14569 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE))
14570 bbr->r_ctl.rc_drain_pg = optval;
14574 case TCP_BBR_PROBE_RTT_LEN:
14575 BBR_OPTS_INC(tcp_bbr_probertt_len);
14577 reset_time_small(&bbr->r_ctl.rc_rttprop, (optval * USECS_IN_SECOND));
14581 case TCP_BBR_PROBE_RTT_GAIN:
14582 BBR_OPTS_INC(tcp_bbr_probertt_gain);
14583 if (optval <= BBR_UNIT)
14584 bbr->r_ctl.bbr_rttprobe_gain_val = optval;
14588 case TCP_BBR_PROBE_RTT_INT:
14589 BBR_OPTS_INC(tcp_bbr_probe_rtt_int);
14591 bbr->r_ctl.rc_probertt_int = optval;
14595 case TCP_BBR_MIN_TOPACEOUT:
14596 BBR_OPTS_INC(tcp_bbr_topaceout);
14598 bbr->no_pacing_until = 0;
14599 bbr->rc_no_pacing = 0;
14600 } else if (optval <= 0x00ff) {
14601 bbr->no_pacing_until = optval;
14602 if ((bbr->r_ctl.rc_pkt_epoch < bbr->no_pacing_until) &&
14603 (bbr->rc_bbr_state == BBR_STATE_STARTUP)){
14604 /* Turn on no pacing */
14605 bbr->rc_no_pacing = 1;
14610 case TCP_BBR_STARTUP_LOSS_EXIT:
14611 BBR_OPTS_INC(tcp_bbr_startup_loss_exit);
14612 bbr->rc_loss_exit = optval;
14614 case TCP_BBR_USEDEL_RATE:
14617 case TCP_BBR_MIN_RTO:
14618 BBR_OPTS_INC(tcp_bbr_min_rto);
14619 bbr->r_ctl.rc_min_rto_ms = optval;
14621 case TCP_BBR_MAX_RTO:
14622 BBR_OPTS_INC(tcp_bbr_max_rto);
14623 bbr->rc_max_rto_sec = optval;
14625 case TCP_RACK_MIN_TO:
14626 /* Minimum time between rack t-o's in ms */
14627 BBR_OPTS_INC(tcp_rack_min_to);
14628 bbr->r_ctl.rc_min_to = optval;
14630 case TCP_RACK_REORD_THRESH:
14631 /* RACK reorder threshold (shift amount) */
14632 BBR_OPTS_INC(tcp_rack_reord_thresh);
14633 if ((optval > 0) && (optval < 31))
14634 bbr->r_ctl.rc_reorder_shift = optval;
14638 case TCP_RACK_REORD_FADE:
14639 /* Does reordering fade after ms time */
14640 BBR_OPTS_INC(tcp_rack_reord_fade);
14641 bbr->r_ctl.rc_reorder_fade = optval;
14643 case TCP_RACK_TLP_THRESH:
14644 /* RACK TLP theshold i.e. srtt+(srtt/N) */
14645 BBR_OPTS_INC(tcp_rack_tlp_thresh);
14647 bbr->rc_tlp_threshold = optval;
14651 case TCP_BBR_USE_RACK_CHEAT:
14652 BBR_OPTS_INC(tcp_use_rackcheat);
14653 if (bbr->rc_use_google) {
14657 BBR_OPTS_INC(tcp_rack_cheat);
14659 bbr->bbr_use_rack_cheat = 1;
14661 bbr->bbr_use_rack_cheat = 0;
14663 case TCP_BBR_FLOOR_MIN_TSO:
14664 BBR_OPTS_INC(tcp_utter_max_tso);
14665 if ((optval >= 0) && (optval < 40))
14666 bbr->r_ctl.bbr_hptsi_segments_floor = optval;
14670 case TCP_BBR_UTTER_MAX_TSO:
14671 BBR_OPTS_INC(tcp_utter_max_tso);
14672 if ((optval >= 0) && (optval < 0xffff))
14673 bbr->r_ctl.bbr_utter_max = optval;
14678 case TCP_BBR_EXTRA_STATE:
14679 BBR_OPTS_INC(tcp_extra_state);
14681 bbr->rc_use_idle_restart = 1;
14683 bbr->rc_use_idle_restart = 0;
14685 case TCP_BBR_SEND_IWND_IN_TSO:
14686 BBR_OPTS_INC(tcp_iwnd_tso);
14688 bbr->bbr_init_win_cheat = 1;
14689 if (bbr->rc_past_init_win == 0) {
14691 cts = tcp_get_usecs(&bbr->rc_tv);
14692 tcp_bbr_tso_size_check(bbr, cts);
14695 bbr->bbr_init_win_cheat = 0;
14697 case TCP_BBR_HDWR_PACE:
14698 BBR_OPTS_INC(tcp_hdwr_pacing);
14700 bbr->bbr_hdw_pace_ena = 1;
14701 bbr->bbr_attempt_hdwr_pace = 0;
14703 bbr->bbr_hdw_pace_ena = 0;
14705 if (bbr->bbr_hdrw_pacing) {
14706 bbr->bbr_hdrw_pacing = 0;
14707 in_pcbdetach_txrtlmt(bbr->rc_inp);
14714 BBR_OPTS_INC(tcp_delack);
14715 if (optval < 100) {
14716 if (optval == 0) /* off */
14717 tp->t_delayed_ack = 0;
14718 else if (optval == 1) /* on which is 2 */
14719 tp->t_delayed_ack = 2;
14720 else /* higher than 2 and less than 100 */
14721 tp->t_delayed_ack = optval;
14722 if (tp->t_flags & TF_DELACK) {
14723 tp->t_flags &= ~TF_DELACK;
14724 tp->t_flags |= TF_ACKNOW;
14725 NET_EPOCH_ENTER(et);
14727 NET_EPOCH_EXIT(et);
14732 case TCP_RACK_PKT_DELAY:
14733 /* RACK added ms i.e. rack-rtt + reord + N */
14734 BBR_OPTS_INC(tcp_rack_pkt_delay);
14735 bbr->r_ctl.rc_pkt_delay = optval;
14737 #ifdef NETFLIX_PEAKRATE
14738 case TCP_MAXPEAKRATE:
14739 BBR_OPTS_INC(tcp_maxpeak);
14740 error = tcp_set_maxpeakrate(tp, optval);
14742 tp->t_peakrate_thr = tp->t_maxpeakrate;
14745 case TCP_BBR_RETRAN_WTSO:
14746 BBR_OPTS_INC(tcp_retran_wtso);
14748 bbr->rc_resends_use_tso = 1;
14750 bbr->rc_resends_use_tso = 0;
14752 case TCP_DATA_AFTER_CLOSE:
14753 BBR_OPTS_INC(tcp_data_ac);
14755 bbr->rc_allow_data_af_clo = 1;
14757 bbr->rc_allow_data_af_clo = 0;
14759 case TCP_BBR_POLICER_DETECT:
14760 BBR_OPTS_INC(tcp_policer_det);
14761 if (bbr->rc_use_google == 0)
14764 bbr->r_use_policer = 1;
14766 bbr->r_use_policer = 0;
14769 case TCP_BBR_TSTMP_RAISES:
14770 BBR_OPTS_INC(tcp_ts_raises);
14772 bbr->ts_can_raise = 1;
14774 bbr->ts_can_raise = 0;
14776 case TCP_BBR_TMR_PACE_OH:
14777 BBR_OPTS_INC(tcp_pacing_oh_tmr);
14778 if (bbr->rc_use_google) {
14782 bbr->r_ctl.rc_incr_tmrs = 1;
14784 bbr->r_ctl.rc_incr_tmrs = 0;
14787 case TCP_BBR_PACE_OH:
14788 BBR_OPTS_INC(tcp_pacing_oh);
14789 if (bbr->rc_use_google) {
14792 if (optval > (BBR_INCL_TCP_OH|
14794 BBR_INCL_ENET_OH)) {
14798 if (optval & BBR_INCL_TCP_OH)
14799 bbr->r_ctl.rc_inc_tcp_oh = 1;
14801 bbr->r_ctl.rc_inc_tcp_oh = 0;
14802 if (optval & BBR_INCL_IP_OH)
14803 bbr->r_ctl.rc_inc_ip_oh = 1;
14805 bbr->r_ctl.rc_inc_ip_oh = 0;
14806 if (optval & BBR_INCL_ENET_OH)
14807 bbr->r_ctl.rc_inc_enet_oh = 1;
14809 bbr->r_ctl.rc_inc_enet_oh = 0;
14813 return (tcp_default_ctloutput(so, sopt, inp, tp));
14816 #ifdef NETFLIX_STATS
14817 tcp_log_socket_option(tp, sopt->sopt_name, optval, error);
14824 * return 0 on success, error-num on failure
14827 bbr_get_sockopt(struct socket *so, struct sockopt *sopt,
14828 struct inpcb *inp, struct tcpcb *tp, struct tcp_bbr *bbr)
14830 int32_t error, optval;
14833 * Because all our options are either boolean or an int, we can just
14834 * pull everything into optval and then unlock and copy. If we ever
14835 * add a option that is not a int, then this will have quite an
14836 * impact to this routine.
14838 switch (sopt->sopt_name) {
14839 case TCP_BBR_PACE_PER_SEC:
14840 optval = bbr->r_ctl.bbr_hptsi_per_second;
14842 case TCP_BBR_PACE_DEL_TAR:
14843 optval = bbr->r_ctl.bbr_hptsi_segments_delay_tar;
14845 case TCP_BBR_PACE_SEG_MAX:
14846 optval = bbr->r_ctl.bbr_hptsi_segments_max;
14848 case TCP_BBR_MIN_TOPACEOUT:
14849 optval = bbr->no_pacing_until;
14851 case TCP_BBR_PACE_SEG_MIN:
14852 optval = bbr->r_ctl.bbr_hptsi_bytes_min;
14854 case TCP_BBR_PACE_CROSS:
14855 optval = bbr->r_ctl.bbr_cross_over;
14857 case TCP_BBR_ALGORITHM:
14858 optval = bbr->rc_use_google;
14860 case TCP_BBR_TSLIMITS:
14861 optval = bbr->rc_use_ts_limit;
14863 case TCP_BBR_IWINTSO:
14864 optval = bbr->rc_init_win;
14866 case TCP_BBR_STARTUP_PG:
14867 optval = bbr->r_ctl.rc_startup_pg;
14869 case TCP_BBR_DRAIN_PG:
14870 optval = bbr->r_ctl.rc_drain_pg;
14872 case TCP_BBR_PROBE_RTT_INT:
14873 optval = bbr->r_ctl.rc_probertt_int;
14875 case TCP_BBR_PROBE_RTT_LEN:
14876 optval = (bbr->r_ctl.rc_rttprop.cur_time_limit / USECS_IN_SECOND);
14878 case TCP_BBR_PROBE_RTT_GAIN:
14879 optval = bbr->r_ctl.bbr_rttprobe_gain_val;
14881 case TCP_BBR_STARTUP_LOSS_EXIT:
14882 optval = bbr->rc_loss_exit;
14884 case TCP_BBR_USEDEL_RATE:
14887 case TCP_BBR_MIN_RTO:
14888 optval = bbr->r_ctl.rc_min_rto_ms;
14890 case TCP_BBR_MAX_RTO:
14891 optval = bbr->rc_max_rto_sec;
14893 case TCP_RACK_PACE_MAX_SEG:
14894 /* Max segments in a pace */
14895 optval = bbr->r_ctl.rc_pace_max_segs;
14897 case TCP_RACK_MIN_TO:
14898 /* Minimum time between rack t-o's in ms */
14899 optval = bbr->r_ctl.rc_min_to;
14901 case TCP_RACK_REORD_THRESH:
14902 /* RACK reorder threshold (shift amount) */
14903 optval = bbr->r_ctl.rc_reorder_shift;
14905 case TCP_RACK_REORD_FADE:
14906 /* Does reordering fade after ms time */
14907 optval = bbr->r_ctl.rc_reorder_fade;
14909 case TCP_BBR_USE_RACK_CHEAT:
14910 /* Do we use the rack cheat for rxt */
14911 optval = bbr->bbr_use_rack_cheat;
14913 case TCP_BBR_FLOOR_MIN_TSO:
14914 optval = bbr->r_ctl.bbr_hptsi_segments_floor;
14916 case TCP_BBR_UTTER_MAX_TSO:
14917 optval = bbr->r_ctl.bbr_utter_max;
14919 case TCP_BBR_SEND_IWND_IN_TSO:
14920 /* Do we send TSO size segments initially */
14921 optval = bbr->bbr_init_win_cheat;
14923 case TCP_BBR_EXTRA_STATE:
14924 optval = bbr->rc_use_idle_restart;
14926 case TCP_RACK_TLP_THRESH:
14927 /* RACK TLP theshold i.e. srtt+(srtt/N) */
14928 optval = bbr->rc_tlp_threshold;
14930 case TCP_RACK_PKT_DELAY:
14931 /* RACK added ms i.e. rack-rtt + reord + N */
14932 optval = bbr->r_ctl.rc_pkt_delay;
14934 case TCP_BBR_RETRAN_WTSO:
14935 optval = bbr->rc_resends_use_tso;
14937 case TCP_DATA_AFTER_CLOSE:
14938 optval = bbr->rc_allow_data_af_clo;
14941 optval = tp->t_delayed_ack;
14943 case TCP_BBR_HDWR_PACE:
14944 optval = bbr->bbr_hdw_pace_ena;
14946 case TCP_BBR_POLICER_DETECT:
14947 optval = bbr->r_use_policer;
14949 case TCP_BBR_TSTMP_RAISES:
14950 optval = bbr->ts_can_raise;
14952 case TCP_BBR_TMR_PACE_OH:
14953 optval = bbr->r_ctl.rc_incr_tmrs;
14955 case TCP_BBR_PACE_OH:
14957 if (bbr->r_ctl.rc_inc_tcp_oh)
14958 optval |= BBR_INCL_TCP_OH;
14959 if (bbr->r_ctl.rc_inc_ip_oh)
14960 optval |= BBR_INCL_IP_OH;
14961 if (bbr->r_ctl.rc_inc_enet_oh)
14962 optval |= BBR_INCL_ENET_OH;
14965 return (tcp_default_ctloutput(so, sopt, inp, tp));
14969 error = sooptcopyout(sopt, &optval, sizeof optval);
14974 * return 0 on success, error-num on failure
14977 bbr_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
14979 int32_t error = EINVAL;
14980 struct tcp_bbr *bbr;
14982 bbr = (struct tcp_bbr *)tp->t_fb_ptr;
14987 if (sopt->sopt_dir == SOPT_SET) {
14988 return (bbr_set_sockopt(so, sopt, inp, tp, bbr));
14989 } else if (sopt->sopt_dir == SOPT_GET) {
14990 return (bbr_get_sockopt(so, sopt, inp, tp, bbr));
14998 bbr_pru_options(struct tcpcb *tp, int flags)
15000 if (flags & PRUS_OOB)
15001 return (EOPNOTSUPP);
15005 struct tcp_function_block __tcp_bbr = {
15006 .tfb_tcp_block_name = __XSTRING(STACKNAME),
15007 .tfb_tcp_output = bbr_output,
15008 .tfb_do_queued_segments = ctf_do_queued_segments,
15009 .tfb_do_segment_nounlock = bbr_do_segment_nounlock,
15010 .tfb_tcp_do_segment = bbr_do_segment,
15011 .tfb_tcp_ctloutput = bbr_ctloutput,
15012 .tfb_tcp_fb_init = bbr_init,
15013 .tfb_tcp_fb_fini = bbr_fini,
15014 .tfb_tcp_timer_stop_all = bbr_stopall,
15015 .tfb_tcp_timer_activate = bbr_timer_activate,
15016 .tfb_tcp_timer_active = bbr_timer_active,
15017 .tfb_tcp_timer_stop = bbr_timer_stop,
15018 .tfb_tcp_rexmit_tmr = bbr_remxt_tmr,
15019 .tfb_tcp_handoff_ok = bbr_handoff_ok,
15020 .tfb_tcp_mtu_chg = bbr_mtu_chg,
15021 .tfb_pru_options = bbr_pru_options,
15024 static const char *bbr_stack_names[] = {
15025 __XSTRING(STACKNAME),
15027 __XSTRING(STACKALIAS),
15031 static bool bbr_mod_inited = false;
15034 tcp_addbbr(module_t mod, int32_t type, void *data)
15041 printf("Attempting to load " __XSTRING(MODNAME) "\n");
15042 bbr_zone = uma_zcreate(__XSTRING(MODNAME) "_map",
15043 sizeof(struct bbr_sendmap),
15044 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
15045 bbr_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb",
15046 sizeof(struct tcp_bbr),
15047 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
15048 sysctl_ctx_init(&bbr_sysctl_ctx);
15049 bbr_sysctl_root = SYSCTL_ADD_NODE(&bbr_sysctl_ctx,
15050 SYSCTL_STATIC_CHILDREN(_net_inet_tcp),
15053 __XSTRING(STACKALIAS),
15055 __XSTRING(STACKNAME),
15057 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
15059 if (bbr_sysctl_root == NULL) {
15060 printf("Failed to add sysctl node\n");
15064 bbr_init_sysctls();
15065 num_stacks = nitems(bbr_stack_names);
15066 err = register_tcp_functions_as_names(&__tcp_bbr, M_WAITOK,
15067 bbr_stack_names, &num_stacks);
15069 printf("Failed to register %s stack name for "
15070 "%s module\n", bbr_stack_names[num_stacks],
15071 __XSTRING(MODNAME));
15072 sysctl_ctx_free(&bbr_sysctl_ctx);
15074 uma_zdestroy(bbr_zone);
15075 uma_zdestroy(bbr_pcb_zone);
15076 bbr_counter_destroy();
15077 printf("Failed to register " __XSTRING(MODNAME)
15078 " module err:%d\n", err);
15081 tcp_lro_reg_mbufq();
15082 bbr_mod_inited = true;
15083 printf(__XSTRING(MODNAME) " is now available\n");
15086 err = deregister_tcp_functions(&__tcp_bbr, true, false);
15089 err = deregister_tcp_functions(&__tcp_bbr, false, true);
15092 if (bbr_mod_inited) {
15093 uma_zdestroy(bbr_zone);
15094 uma_zdestroy(bbr_pcb_zone);
15095 sysctl_ctx_free(&bbr_sysctl_ctx);
15096 bbr_counter_destroy();
15097 printf(__XSTRING(MODNAME)
15098 " is now no longer available\n");
15099 bbr_mod_inited = false;
15101 tcp_lro_dereg_mbufq();
15105 return (EOPNOTSUPP);
15110 static moduledata_t tcp_bbr = {
15111 .name = __XSTRING(MODNAME),
15112 .evhand = tcp_addbbr,
15116 MODULE_VERSION(MODNAME, 1);
15117 DECLARE_MODULE(MODNAME, tcp_bbr, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
15118 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1);