xref: /freebsd/sys/netinet/tcp_stacks/rack.c (revision d0b2dbfa)
1 /*-
2  * Copyright (c) 2016-2020 Netflix, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  *
25  */
26 
27 #include <sys/cdefs.h>
28 #include "opt_inet.h"
29 #include "opt_inet6.h"
30 #include "opt_ipsec.h"
31 #include "opt_ratelimit.h"
32 #include "opt_kern_tls.h"
33 #if defined(INET) || defined(INET6)
34 #include <sys/param.h>
35 #include <sys/arb.h>
36 #include <sys/module.h>
37 #include <sys/kernel.h>
38 #ifdef TCP_HHOOK
39 #include <sys/hhook.h>
40 #endif
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/mbuf.h>
46 #include <sys/proc.h>		/* for proc0 declaration */
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
51 #ifdef STATS
52 #include <sys/qmath.h>
53 #include <sys/tree.h>
54 #include <sys/stats.h> /* Must come after qmath.h and tree.h */
55 #else
56 #include <sys/tree.h>
57 #endif
58 #include <sys/refcount.h>
59 #include <sys/queue.h>
60 #include <sys/tim_filter.h>
61 #include <sys/smp.h>
62 #include <sys/kthread.h>
63 #include <sys/kern_prefetch.h>
64 #include <sys/protosw.h>
65 #ifdef TCP_ACCOUNTING
66 #include <sys/sched.h>
67 #include <machine/cpu.h>
68 #endif
69 #include <vm/uma.h>
70 
71 #include <net/route.h>
72 #include <net/route/nhop.h>
73 #include <net/vnet.h>
74 
75 #define TCPSTATES		/* for logging */
76 
77 #include <netinet/in.h>
78 #include <netinet/in_kdtrace.h>
79 #include <netinet/in_pcb.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
82 #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
83 #include <netinet/ip_var.h>
84 #include <netinet/ip6.h>
85 #include <netinet6/in6_pcb.h>
86 #include <netinet6/ip6_var.h>
87 #include <netinet/tcp.h>
88 #define	TCPOUTFLAGS
89 #include <netinet/tcp_fsm.h>
90 #include <netinet/tcp_seq.h>
91 #include <netinet/tcp_timer.h>
92 #include <netinet/tcp_var.h>
93 #include <netinet/tcp_log_buf.h>
94 #include <netinet/tcp_syncache.h>
95 #include <netinet/tcp_hpts.h>
96 #include <netinet/tcp_ratelimit.h>
97 #include <netinet/tcp_accounting.h>
98 #include <netinet/tcpip.h>
99 #include <netinet/cc/cc.h>
100 #include <netinet/cc/cc_newreno.h>
101 #include <netinet/tcp_fastopen.h>
102 #include <netinet/tcp_lro.h>
103 #ifdef NETFLIX_SHARED_CWND
104 #include <netinet/tcp_shared_cwnd.h>
105 #endif
106 #ifdef TCP_OFFLOAD
107 #include <netinet/tcp_offload.h>
108 #endif
109 #ifdef INET6
110 #include <netinet6/tcp6_var.h>
111 #endif
112 #include <netinet/tcp_ecn.h>
113 
114 #include <netipsec/ipsec_support.h>
115 
116 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
117 #include <netipsec/ipsec.h>
118 #include <netipsec/ipsec6.h>
119 #endif				/* IPSEC */
120 
121 #include <netinet/udp.h>
122 #include <netinet/udp_var.h>
123 #include <machine/in_cksum.h>
124 
125 #ifdef MAC
126 #include <security/mac/mac_framework.h>
127 #endif
128 #include "sack_filter.h"
129 #include "tcp_rack.h"
130 #include "tailq_hash.h"
131 #include "rack_bbr_common.h"
132 
133 uma_zone_t rack_zone;
134 uma_zone_t rack_pcb_zone;
135 
136 #ifndef TICKS2SBT
137 #define	TICKS2SBT(__t)	(tick_sbt * ((sbintime_t)(__t)))
138 #endif
139 
140 VNET_DECLARE(uint32_t, newreno_beta);
141 VNET_DECLARE(uint32_t, newreno_beta_ecn);
142 #define V_newreno_beta VNET(newreno_beta)
143 #define V_newreno_beta_ecn VNET(newreno_beta_ecn)
144 
145 
146 MALLOC_DEFINE(M_TCPFSB, "tcp_fsb", "TCP fast send block");
147 MALLOC_DEFINE(M_TCPDO, "tcp_do", "TCP deferred options");
148 
149 struct sysctl_ctx_list rack_sysctl_ctx;
150 struct sysctl_oid *rack_sysctl_root;
151 
152 #define CUM_ACKED 1
153 #define SACKED 2
154 
155 /*
156  * The RACK module incorporates a number of
157  * TCP ideas that have been put out into the IETF
158  * over the last few years:
159  * - Matt Mathis's Rate Halving which slowly drops
160  *    the congestion window so that the ack clock can
161  *    be maintained during a recovery.
162  * - Yuchung Cheng's RACK TCP (for which its named) that
163  *    will stop us using the number of dup acks and instead
164  *    use time as the gage of when we retransmit.
165  * - Reorder Detection of RFC4737 and the Tail-Loss probe draft
166  *    of Dukkipati et.al.
167  * RACK depends on SACK, so if an endpoint arrives that
168  * cannot do SACK the state machine below will shuttle the
169  * connection back to using the "default" TCP stack that is
170  * in FreeBSD.
171  *
172  * To implement RACK the original TCP stack was first decomposed
173  * into a functional state machine with individual states
174  * for each of the possible TCP connection states. The do_segment
175  * functions role in life is to mandate the connection supports SACK
176  * initially and then assure that the RACK state matches the conenction
177  * state before calling the states do_segment function. Each
178  * state is simplified due to the fact that the original do_segment
179  * has been decomposed and we *know* what state we are in (no
180  * switches on the state) and all tests for SACK are gone. This
181  * greatly simplifies what each state does.
182  *
183  * TCP output is also over-written with a new version since it
184  * must maintain the new rack scoreboard.
185  *
186  */
187 static int32_t rack_tlp_thresh = 1;
188 static int32_t rack_tlp_limit = 2;	/* No more than 2 TLPs w-out new data */
189 static int32_t rack_tlp_use_greater = 1;
190 static int32_t rack_reorder_thresh = 2;
191 static int32_t rack_reorder_fade = 60000000;	/* 0 - never fade, def 60,000,000
192 						 * - 60 seconds */
193 static uint32_t rack_clamp_ss_upper = 110;
194 static uint32_t rack_clamp_ca_upper = 105;
195 static uint32_t rack_rxt_min_rnds = 10;	/* Min rounds if drastic rxt clamp is in place */
196 static uint32_t rack_unclamp_round_thresh = 100;	/* number of perfect rounds before we unclamp */
197 static uint32_t rack_unclamp_rxt_thresh = 5;	/* .5%  and under */
198 static uint64_t rack_rxt_clamp_thresh = 0;	/* Do we do the rxt clamp thing */
199 static int32_t rack_dnd_default = 0;		/* For rr_conf = 3, what is the default for dnd */
200 static int32_t rack_rxt_controls = 0;
201 static int32_t rack_fill_cw_state = 0;
202 static uint8_t rack_req_measurements = 1;
203 /* Attack threshold detections */
204 static uint32_t rack_highest_sack_thresh_seen = 0;
205 static uint32_t rack_highest_move_thresh_seen = 0;
206 static uint32_t rack_merge_out_sacks_on_attack = 0;
207 static int32_t rack_enable_hw_pacing = 0; /* Due to CCSP keep it off by default */
208 static int32_t rack_hw_pace_extra_slots = 0;	/* 2 extra MSS time betweens */
209 static int32_t rack_hw_rate_caps = 0; /* 1; */
210 static int32_t rack_hw_rate_cap_per = 0;	/* 0 -- off  */
211 static int32_t rack_hw_rate_min = 0; /* 1500000;*/
212 static int32_t rack_hw_rate_to_low = 0; /* 1200000; */
213 static int32_t rack_hw_up_only = 0;
214 static int32_t rack_stats_gets_ms_rtt = 1;
215 static int32_t rack_prr_addbackmax = 2;
216 static int32_t rack_do_hystart = 0;
217 static int32_t rack_apply_rtt_with_reduced_conf = 0;
218 static int32_t rack_hibeta_setting = 0;
219 static int32_t rack_default_pacing_divisor = 250;
220 static int32_t rack_uses_full_dgp_in_rec = 1;
221 static uint16_t rack_pacing_min_seg = 0;
222 
223 
224 static uint32_t sad_seg_size_per = 800;	/* 80.0 % */
225 static int32_t rack_pkt_delay = 1000;
226 static int32_t rack_send_a_lot_in_prr = 1;
227 static int32_t rack_min_to = 1000;	/* Number of microsecond  min timeout */
228 static int32_t rack_verbose_logging = 0;
229 static int32_t rack_ignore_data_after_close = 1;
230 static int32_t rack_enable_shared_cwnd = 1;
231 static int32_t rack_use_cmp_acks = 1;
232 static int32_t rack_use_fsb = 1;
233 static int32_t rack_use_rfo = 1;
234 static int32_t rack_use_rsm_rfo = 1;
235 static int32_t rack_max_abc_post_recovery = 2;
236 static int32_t rack_client_low_buf = 0;
237 static int32_t rack_dsack_std_based = 0x3;	/* bit field bit 1 sets rc_rack_tmr_std_based and bit 2 sets rc_rack_use_dsack */
238 static int32_t rack_bw_multipler = 2;		/* Limit on fill cw's jump up to be this x gp_est */
239 #ifdef TCP_ACCOUNTING
240 static int32_t rack_tcp_accounting = 0;
241 #endif
242 static int32_t rack_limits_scwnd = 1;
243 static int32_t rack_enable_mqueue_for_nonpaced = 0;
244 static int32_t rack_hybrid_allow_set_maxseg = 0;
245 static int32_t rack_disable_prr = 0;
246 static int32_t use_rack_rr = 1;
247 static int32_t rack_non_rxt_use_cr = 0; /* does a non-rxt in recovery use the configured rate (ss/ca)? */
248 static int32_t rack_persist_min = 250000;	/* 250usec */
249 static int32_t rack_persist_max = 2000000;	/* 2 Second in usec's */
250 static int32_t rack_sack_not_required = 1;	/* set to one to allow non-sack to use rack */
251 static int32_t rack_default_init_window = 0;	/* Use system default */
252 static int32_t rack_limit_time_with_srtt = 0;
253 static int32_t rack_autosndbuf_inc = 20;	/* In percentage form */
254 static int32_t rack_enobuf_hw_boost_mult = 0;	/* How many times the hw rate we boost slot using time_between */
255 static int32_t rack_enobuf_hw_max = 12000;	/* 12 ms in usecs */
256 static int32_t rack_enobuf_hw_min = 10000;	/* 10 ms in usecs */
257 static int32_t rack_hw_rwnd_factor = 2;		/* How many max_segs the rwnd must be before we hold off sending */
258 static int32_t rack_hw_check_queue = 0;		/* Do we always pre-check queue depth of a hw queue */
259 static int32_t rack_full_buffer_discount = 10;
260 /*
261  * Currently regular tcp has a rto_min of 30ms
262  * the backoff goes 12 times so that ends up
263  * being a total of 122.850 seconds before a
264  * connection is killed.
265  */
266 static uint32_t rack_def_data_window = 20;
267 static uint32_t rack_goal_bdp = 2;
268 static uint32_t rack_min_srtts = 1;
269 static uint32_t rack_min_measure_usec = 0;
270 static int32_t rack_tlp_min = 10000;	/* 10ms */
271 static int32_t rack_rto_min = 30000;	/* 30,000 usec same as main freebsd */
272 static int32_t rack_rto_max = 4000000;	/* 4 seconds in usec's */
273 static const int32_t rack_free_cache = 2;
274 static int32_t rack_hptsi_segments = 40;
275 static int32_t rack_rate_sample_method = USE_RTT_LOW;
276 static int32_t rack_pace_every_seg = 0;
277 static int32_t rack_delayed_ack_time = 40000;	/* 40ms in usecs */
278 static int32_t rack_slot_reduction = 4;
279 static int32_t rack_wma_divisor = 8;		/* For WMA calculation */
280 static int32_t rack_cwnd_block_ends_measure = 0;
281 static int32_t rack_rwnd_block_ends_measure = 0;
282 static int32_t rack_def_profile = 0;
283 
284 static int32_t rack_lower_cwnd_at_tlp = 0;
285 static int32_t rack_limited_retran = 0;
286 static int32_t rack_always_send_oldest = 0;
287 static int32_t rack_tlp_threshold_use = TLP_USE_TWO_ONE;
288 
289 static uint16_t rack_per_of_gp_ss = 250;	/* 250 % slow-start */
290 static uint16_t rack_per_of_gp_ca = 200;	/* 200 % congestion-avoidance */
291 static uint16_t rack_per_of_gp_rec = 200;	/* 200 % of bw */
292 
293 /* Probertt */
294 static uint16_t rack_per_of_gp_probertt = 60;	/* 60% of bw */
295 static uint16_t rack_per_of_gp_lowthresh = 40;	/* 40% is bottom */
296 static uint16_t rack_per_of_gp_probertt_reduce = 10; /* 10% reduction */
297 static uint16_t rack_atexit_prtt_hbp = 130;	/* Clamp to 130% on exit prtt if highly buffered path */
298 static uint16_t rack_atexit_prtt = 130;	/* Clamp to 100% on exit prtt if non highly buffered path */
299 
300 static uint32_t rack_max_drain_wait = 2;	/* How man gp srtt's before we give up draining */
301 static uint32_t rack_must_drain = 1;		/* How many GP srtt's we *must* wait */
302 static uint32_t rack_probertt_use_min_rtt_entry = 1;	/* Use the min to calculate the goal else gp_srtt */
303 static uint32_t rack_probertt_use_min_rtt_exit = 0;
304 static uint32_t rack_probe_rtt_sets_cwnd = 0;
305 static uint32_t rack_probe_rtt_safety_val = 2000000;	/* No more than 2 sec in probe-rtt */
306 static uint32_t rack_time_between_probertt = 9600000;	/* 9.6 sec in usecs */
307 static uint32_t rack_probertt_gpsrtt_cnt_mul = 0;	/* How many srtt periods does probe-rtt last top fraction */
308 static uint32_t rack_probertt_gpsrtt_cnt_div = 0;	/* How many srtt periods does probe-rtt last bottom fraction */
309 static uint32_t rack_min_probertt_hold = 40000;		/* Equal to delayed ack time */
310 static uint32_t rack_probertt_filter_life = 10000000;
311 static uint32_t rack_probertt_lower_within = 10;
312 static uint32_t rack_min_rtt_movement = 250000;	/* Must move at least 250ms (in microseconds)  to count as a lowering */
313 static int32_t rack_pace_one_seg = 0;		/* Shall we pace for less than 1.4Meg 1MSS at a time */
314 static int32_t rack_probertt_clear_is = 1;
315 static int32_t rack_max_drain_hbp = 1;		/* Extra drain times gpsrtt for highly buffered paths */
316 static int32_t rack_hbp_thresh = 3;		/* what is the divisor max_rtt/min_rtt to decided a hbp */
317 
318 /* Part of pacing */
319 static int32_t rack_max_per_above = 30;		/* When we go to increment stop if above 100+this% */
320 
321 /* Timely information:
322  *
323  * Here we have various control parameters on how
324  * timely may change the multiplier. rack_gain_p5_ub
325  * is associated with timely but not directly influencing
326  * the rate decision like the other variables. It controls
327  * the way fill-cw interacts with timely and caps how much
328  * timely can boost the fill-cw b/w.
329  *
330  * The other values are various boost/shrink numbers as well
331  * as potential caps when adjustments are made to the timely
332  * gain (returned by rack_get_output_gain(). Remember too that
333  * the gain returned can be overriden by other factors such as
334  * probeRTT as well as fixed-rate-pacing.
335  */
336 static int32_t rack_gain_p5_ub = 250;
337 static int32_t rack_gp_per_bw_mul_up = 2;	/* 2% */
338 static int32_t rack_gp_per_bw_mul_down = 4;	/* 4% */
339 static int32_t rack_gp_rtt_maxmul = 3;		/* 3 x maxmin */
340 static int32_t rack_gp_rtt_minmul = 1;		/* minrtt + (minrtt/mindiv) is lower rtt */
341 static int32_t rack_gp_rtt_mindiv = 4;		/* minrtt + (minrtt * minmul/mindiv) is lower rtt */
342 static int32_t rack_gp_decrease_per = 80;	/* Beta value of timely decrease (.8) = 80 */
343 static int32_t rack_gp_increase_per = 2;	/* 2% increase in multiplier */
344 static int32_t rack_per_lower_bound = 50;	/* Don't allow to drop below this multiplier */
345 static int32_t rack_per_upper_bound_ss = 0;	/* Don't allow SS to grow above this */
346 static int32_t rack_per_upper_bound_ca = 0;	/* Don't allow CA to grow above this */
347 static int32_t rack_do_dyn_mul = 0;		/* Are the rack gp multipliers dynamic */
348 static int32_t rack_gp_no_rec_chg = 1;		/* Prohibit recovery from reducing it's multiplier */
349 static int32_t rack_timely_dec_clear = 6;	/* Do we clear decrement count at a value (6)? */
350 static int32_t rack_timely_max_push_rise = 3;	/* One round of pushing */
351 static int32_t rack_timely_max_push_drop = 3;	/* Three round of pushing */
352 static int32_t rack_timely_min_segs = 4;	/* 4 segment minimum */
353 static int32_t rack_use_max_for_nobackoff = 0;
354 static int32_t rack_timely_int_timely_only = 0;	/* do interim timely's only use the timely algo (no b/w changes)? */
355 static int32_t rack_timely_no_stopping = 0;
356 static int32_t rack_down_raise_thresh = 100;
357 static int32_t rack_req_segs = 1;
358 static uint64_t rack_bw_rate_cap = 0;
359 
360 
361 /* Rack specific counters */
362 counter_u64_t rack_saw_enobuf;
363 counter_u64_t rack_saw_enobuf_hw;
364 counter_u64_t rack_saw_enetunreach;
365 counter_u64_t rack_persists_sends;
366 counter_u64_t rack_persists_acks;
367 counter_u64_t rack_persists_loss;
368 counter_u64_t rack_persists_lost_ends;
369 counter_u64_t rack_total_bytes;
370 #ifdef INVARIANTS
371 counter_u64_t rack_adjust_map_bw;
372 #endif
373 /* Tail loss probe counters */
374 counter_u64_t rack_tlp_tot;
375 counter_u64_t rack_tlp_newdata;
376 counter_u64_t rack_tlp_retran;
377 counter_u64_t rack_tlp_retran_bytes;
378 counter_u64_t rack_to_tot;
379 counter_u64_t rack_hot_alloc;
380 counter_u64_t rack_to_alloc;
381 counter_u64_t rack_to_alloc_hard;
382 counter_u64_t rack_to_alloc_emerg;
383 counter_u64_t rack_to_alloc_limited;
384 counter_u64_t rack_alloc_limited_conns;
385 counter_u64_t rack_split_limited;
386 counter_u64_t rack_rxt_clamps_cwnd;
387 counter_u64_t rack_rxt_clamps_cwnd_uniq;
388 
389 counter_u64_t rack_multi_single_eq;
390 counter_u64_t rack_proc_non_comp_ack;
391 
392 counter_u64_t rack_fto_send;
393 counter_u64_t rack_fto_rsm_send;
394 counter_u64_t rack_nfto_resend;
395 counter_u64_t rack_non_fto_send;
396 counter_u64_t rack_extended_rfo;
397 
398 counter_u64_t rack_sack_proc_all;
399 counter_u64_t rack_sack_proc_short;
400 counter_u64_t rack_sack_proc_restart;
401 counter_u64_t rack_sack_attacks_detected;
402 counter_u64_t rack_sack_attacks_reversed;
403 counter_u64_t rack_sack_attacks_suspect;
404 counter_u64_t rack_sack_used_next_merge;
405 counter_u64_t rack_sack_splits;
406 counter_u64_t rack_sack_used_prev_merge;
407 counter_u64_t rack_sack_skipped_acked;
408 counter_u64_t rack_ack_total;
409 counter_u64_t rack_express_sack;
410 counter_u64_t rack_sack_total;
411 counter_u64_t rack_move_none;
412 counter_u64_t rack_move_some;
413 
414 counter_u64_t rack_input_idle_reduces;
415 counter_u64_t rack_collapsed_win;
416 counter_u64_t rack_collapsed_win_seen;
417 counter_u64_t rack_collapsed_win_rxt;
418 counter_u64_t rack_collapsed_win_rxt_bytes;
419 counter_u64_t rack_try_scwnd;
420 counter_u64_t rack_hw_pace_init_fail;
421 counter_u64_t rack_hw_pace_lost;
422 
423 counter_u64_t rack_out_size[TCP_MSS_ACCT_SIZE];
424 counter_u64_t rack_opts_arry[RACK_OPTS_SIZE];
425 
426 
427 #define	RACK_REXMTVAL(tp) max(rack_rto_min, ((tp)->t_srtt + ((tp)->t_rttvar << 2)))
428 
429 #define	RACK_TCPT_RANGESET(tv, value, tvmin, tvmax, slop) do {	\
430 	(tv) = (value) + slop;	 \
431 	if ((u_long)(tv) < (u_long)(tvmin)) \
432 		(tv) = (tvmin); \
433 	if ((u_long)(tv) > (u_long)(tvmax)) \
434 		(tv) = (tvmax); \
435 } while (0)
436 
437 static void
438 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick,  int event, int line);
439 
440 static int
441 rack_process_ack(struct mbuf *m, struct tcphdr *th,
442     struct socket *so, struct tcpcb *tp, struct tcpopt *to,
443     uint32_t tiwin, int32_t tlen, int32_t * ofia, int32_t thflags, int32_t * ret_val);
444 static int
445 rack_process_data(struct mbuf *m, struct tcphdr *th,
446     struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
447     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt);
448 static void
449 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack,
450    uint32_t th_ack, uint16_t nsegs, uint16_t type, int32_t recovery);
451 static struct rack_sendmap *rack_alloc(struct tcp_rack *rack);
452 static struct rack_sendmap *rack_alloc_limit(struct tcp_rack *rack,
453     uint8_t limit_type);
454 static struct rack_sendmap *
455 rack_check_recovery_mode(struct tcpcb *tp,
456     uint32_t tsused);
457 static void
458 rack_cong_signal(struct tcpcb *tp,
459 		 uint32_t type, uint32_t ack, int );
460 static void rack_counter_destroy(void);
461 static int
462 rack_ctloutput(struct tcpcb *tp, struct sockopt *sopt);
463 static int32_t rack_ctor(void *mem, int32_t size, void *arg, int32_t how);
464 static void
465 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override);
466 static void
467 rack_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
468     int32_t drop_hdrlen, int32_t tlen, uint8_t iptos);
469 static void rack_dtor(void *mem, int32_t size, void *arg);
470 static void
471 rack_log_alt_to_to_cancel(struct tcp_rack *rack,
472     uint32_t flex1, uint32_t flex2,
473     uint32_t flex3, uint32_t flex4,
474     uint32_t flex5, uint32_t flex6,
475     uint16_t flex7, uint8_t mod);
476 
477 static void
478 rack_log_pacing_delay_calc(struct tcp_rack *rack, uint32_t len, uint32_t slot,
479    uint64_t bw_est, uint64_t bw, uint64_t len_time, int method, int line,
480    struct rack_sendmap *rsm, uint8_t quality);
481 static struct rack_sendmap *
482 rack_find_high_nonack(struct tcp_rack *rack,
483     struct rack_sendmap *rsm);
484 static struct rack_sendmap *rack_find_lowest_rsm(struct tcp_rack *rack);
485 static void rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm);
486 static void rack_fini(struct tcpcb *tp, int32_t tcb_is_purged);
487 static int rack_get_sockopt(struct tcpcb *tp, struct sockopt *sopt);
488 static void
489 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
490 			    tcp_seq th_ack, int line, uint8_t quality);
491 static void
492 rack_log_type_pacing_sizes(struct tcpcb *tp, struct tcp_rack *rack, uint32_t arg1, uint32_t arg2, uint32_t arg3, uint8_t frm);
493 
494 static uint32_t
495 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss);
496 static int32_t rack_handoff_ok(struct tcpcb *tp);
497 static int32_t rack_init(struct tcpcb *tp, void **ptr);
498 static void rack_init_sysctls(void);
499 
500 static void
501 rack_log_ack(struct tcpcb *tp, struct tcpopt *to,
502     struct tcphdr *th, int entered_rec, int dup_ack_struck,
503     int *dsack_seen, int *sacks_seen);
504 static void
505 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
506     uint32_t seq_out, uint16_t th_flags, int32_t err, uint64_t ts,
507     struct rack_sendmap *hintrsm, uint16_t add_flags, struct mbuf *s_mb, uint32_t s_moff, int hw_tls, int segsiz);
508 
509 static uint64_t rack_get_gp_est(struct tcp_rack *rack);
510 
511 static void
512 rack_log_sack_passed(struct tcpcb *tp, struct tcp_rack *rack,
513     struct rack_sendmap *rsm);
514 static void rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm);
515 static int32_t rack_output(struct tcpcb *tp);
516 
517 static uint32_t
518 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack,
519     struct sackblk *sack, struct tcpopt *to, struct rack_sendmap **prsm,
520     uint32_t cts, int *no_extra, int *moved_two, uint32_t segsiz);
521 static void rack_post_recovery(struct tcpcb *tp, uint32_t th_seq);
522 static void rack_remxt_tmr(struct tcpcb *tp);
523 static int rack_set_sockopt(struct tcpcb *tp, struct sockopt *sopt);
524 static void rack_set_state(struct tcpcb *tp, struct tcp_rack *rack);
525 static int32_t rack_stopall(struct tcpcb *tp);
526 static void rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line);
527 static uint32_t
528 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack,
529     struct rack_sendmap *rsm, uint64_t ts, int32_t * lenp, uint16_t add_flag, int segsiz);
530 static void
531 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
532     struct rack_sendmap *rsm, uint64_t ts, uint16_t add_flag, int segsiz);
533 static int
534 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack,
535     struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack);
536 static int32_t tcp_addrack(module_t mod, int32_t type, void *data);
537 static int
538 rack_do_close_wait(struct mbuf *m, struct tcphdr *th,
539     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
540     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
541 static int
542 rack_do_closing(struct mbuf *m, struct tcphdr *th,
543     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
544     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
545 static int
546 rack_do_established(struct mbuf *m, struct tcphdr *th,
547     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
548     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
549 static int
550 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th,
551     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
552     int32_t tlen, uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos);
553 static int
554 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th,
555     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
556     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
557 static int
558 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th,
559     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
560     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
561 static int
562 rack_do_lastack(struct mbuf *m, struct tcphdr *th,
563     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
564     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
565 static int
566 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th,
567     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
568     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
569 static int
570 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th,
571     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
572     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
573 static void rack_chk_req_and_hybrid_on_out(struct tcp_rack *rack, tcp_seq seq, uint32_t len, uint64_t cts);
574 struct rack_sendmap *
575 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack,
576     uint32_t tsused);
577 static void tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt,
578     uint32_t len, uint32_t us_tim, int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt);
579 static void
580      tcp_rack_partialack(struct tcpcb *tp);
581 static int
582 rack_set_profile(struct tcp_rack *rack, int prof);
583 static void
584 rack_apply_deferred_options(struct tcp_rack *rack);
585 
586 int32_t rack_clear_counter=0;
587 
588 static uint64_t
589 rack_get_lt_bw(struct tcp_rack *rack)
590 {
591 	struct timeval tv;
592 	uint64_t tim, bytes;
593 
594 	tim = rack->r_ctl.lt_bw_time;
595 	bytes = rack->r_ctl.lt_bw_bytes;
596 	if (rack->lt_bw_up) {
597 		/* Include all the current bytes too */
598 		microuptime(&tv);
599 		bytes += (rack->rc_tp->snd_una - rack->r_ctl.lt_seq);
600 		tim += (tcp_tv_to_lusectick(&tv) - rack->r_ctl.lt_timemark);
601 	}
602 	if ((bytes != 0) && (tim != 0))
603 		return ((bytes * (uint64_t)1000000) / tim);
604 	else
605 		return (0);
606 }
607 
608 static void
609 rack_swap_beta_values(struct tcp_rack *rack, uint8_t flex8)
610 {
611 	struct sockopt sopt;
612 	struct cc_newreno_opts opt;
613 	struct newreno old;
614 	struct tcpcb *tp;
615 	int error, failed = 0;
616 
617 	tp = rack->rc_tp;
618 	if (tp->t_cc == NULL) {
619 		/* Tcb is leaving */
620 		return;
621 	}
622 	rack->rc_pacing_cc_set = 1;
623 	if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0) {
624 		/* Not new-reno we can't play games with beta! */
625 		failed = 1;
626 		goto out;
627 
628 	}
629 	if (CC_ALGO(tp)->ctl_output == NULL)  {
630 		/* Huh, not using new-reno so no swaps.? */
631 		failed = 2;
632 		goto out;
633 	}
634 	/* Get the current values out */
635 	sopt.sopt_valsize = sizeof(struct cc_newreno_opts);
636 	sopt.sopt_dir = SOPT_GET;
637 	opt.name = CC_NEWRENO_BETA;
638 	error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
639 	if (error)  {
640 		failed = 3;
641 		goto out;
642 	}
643 	old.beta = opt.val;
644 	opt.name = CC_NEWRENO_BETA_ECN;
645 	error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
646 	if (error)  {
647 		failed = 4;
648 		goto out;
649 	}
650 	old.beta_ecn = opt.val;
651 
652 	/* Now lets set in the values we have stored */
653 	sopt.sopt_dir = SOPT_SET;
654 	opt.name = CC_NEWRENO_BETA;
655 	opt.val = rack->r_ctl.rc_saved_beta.beta;
656 	error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
657 	if (error)  {
658 		failed = 5;
659 		goto out;
660 	}
661 	opt.name = CC_NEWRENO_BETA_ECN;
662 	opt.val = rack->r_ctl.rc_saved_beta.beta_ecn;
663 	error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
664 	if (error) {
665 		failed = 6;
666 		goto out;
667 	}
668 	/* Save off the values for restoral */
669 	memcpy(&rack->r_ctl.rc_saved_beta, &old, sizeof(struct newreno));
670 out:
671 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
672 		union tcp_log_stackspecific log;
673 		struct timeval tv;
674 		struct newreno *ptr;
675 
676 		ptr = ((struct newreno *)tp->t_ccv.cc_data);
677 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
678 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
679 		log.u_bbr.flex1 = ptr->beta;
680 		log.u_bbr.flex2 = ptr->beta_ecn;
681 		log.u_bbr.flex3 = ptr->newreno_flags;
682 		log.u_bbr.flex4 = rack->r_ctl.rc_saved_beta.beta;
683 		log.u_bbr.flex5 = rack->r_ctl.rc_saved_beta.beta_ecn;
684 		log.u_bbr.flex6 = failed;
685 		log.u_bbr.flex7 = rack->gp_ready;
686 		log.u_bbr.flex7 <<= 1;
687 		log.u_bbr.flex7 |= rack->use_fixed_rate;
688 		log.u_bbr.flex7 <<= 1;
689 		log.u_bbr.flex7 |= rack->rc_pacing_cc_set;
690 		log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
691 		log.u_bbr.flex8 = flex8;
692 		tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, error,
693 			       0, &log, false, NULL, NULL, 0, &tv);
694 	}
695 }
696 
697 static void
698 rack_set_cc_pacing(struct tcp_rack *rack)
699 {
700 	if (rack->rc_pacing_cc_set)
701 		return;
702 	/*
703 	 * Use the swap utility placing in 3 for flex8 to id a
704 	 * set of a new set of values.
705 	 */
706 	rack->rc_pacing_cc_set = 1;
707 	rack_swap_beta_values(rack, 3);
708 }
709 
710 static void
711 rack_undo_cc_pacing(struct tcp_rack *rack)
712 {
713 	if (rack->rc_pacing_cc_set == 0)
714 		return;
715 	/*
716 	 * Use the swap utility placing in 4 for flex8 to id a
717 	 * restoral of the old values.
718 	 */
719 	rack->rc_pacing_cc_set = 0;
720 	rack_swap_beta_values(rack, 4);
721 }
722 
723 static void
724 rack_log_gpset(struct tcp_rack *rack, uint32_t seq_end, uint32_t ack_end_t,
725 	       uint32_t send_end_t, int line, uint8_t mode, struct rack_sendmap *rsm)
726 {
727 	if (tcp_bblogging_on(rack->rc_tp) && (rack_verbose_logging != 0)) {
728 		union tcp_log_stackspecific log;
729 		struct timeval tv;
730 
731 		memset(&log, 0, sizeof(log));
732 		log.u_bbr.flex1 = seq_end;
733 		log.u_bbr.flex2 = rack->rc_tp->gput_seq;
734 		log.u_bbr.flex3 = ack_end_t;
735 		log.u_bbr.flex4 = rack->rc_tp->gput_ts;
736 		log.u_bbr.flex5 = send_end_t;
737 		log.u_bbr.flex6 = rack->rc_tp->gput_ack;
738 		log.u_bbr.flex7 = mode;
739 		log.u_bbr.flex8 = 69;
740 		log.u_bbr.rttProp = rack->r_ctl.rc_gp_cumack_ts;
741 		log.u_bbr.delRate = rack->r_ctl.rc_gp_output_ts;
742 		log.u_bbr.pkts_out = line;
743 		log.u_bbr.cwnd_gain = rack->app_limited_needs_set;
744 		log.u_bbr.pkt_epoch = rack->r_ctl.rc_app_limited_cnt;
745 		if (rsm != NULL) {
746 			log.u_bbr.applimited = rsm->r_start;
747 			log.u_bbr.delivered = rsm->r_end;
748 			log.u_bbr.epoch = rsm->r_flags;
749 		}
750 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
751 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
752 		    &rack->rc_inp->inp_socket->so_rcv,
753 		    &rack->rc_inp->inp_socket->so_snd,
754 		    BBR_LOG_HPTSI_CALC, 0,
755 		    0, &log, false, &tv);
756 	}
757 }
758 
759 static int
760 sysctl_rack_clear(SYSCTL_HANDLER_ARGS)
761 {
762 	uint32_t stat;
763 	int32_t error;
764 
765 	error = SYSCTL_OUT(req, &rack_clear_counter, sizeof(uint32_t));
766 	if (error || req->newptr == NULL)
767 		return error;
768 
769 	error = SYSCTL_IN(req, &stat, sizeof(uint32_t));
770 	if (error)
771 		return (error);
772 	if (stat == 1) {
773 #ifdef INVARIANTS
774 		printf("Clearing RACK counters\n");
775 #endif
776 		counter_u64_zero(rack_tlp_tot);
777 		counter_u64_zero(rack_tlp_newdata);
778 		counter_u64_zero(rack_tlp_retran);
779 		counter_u64_zero(rack_tlp_retran_bytes);
780 		counter_u64_zero(rack_to_tot);
781 		counter_u64_zero(rack_saw_enobuf);
782 		counter_u64_zero(rack_saw_enobuf_hw);
783 		counter_u64_zero(rack_saw_enetunreach);
784 		counter_u64_zero(rack_persists_sends);
785 		counter_u64_zero(rack_total_bytes);
786 		counter_u64_zero(rack_persists_acks);
787 		counter_u64_zero(rack_persists_loss);
788 		counter_u64_zero(rack_persists_lost_ends);
789 #ifdef INVARIANTS
790 		counter_u64_zero(rack_adjust_map_bw);
791 #endif
792 		counter_u64_zero(rack_to_alloc_hard);
793 		counter_u64_zero(rack_to_alloc_emerg);
794 		counter_u64_zero(rack_sack_proc_all);
795 		counter_u64_zero(rack_fto_send);
796 		counter_u64_zero(rack_fto_rsm_send);
797 		counter_u64_zero(rack_extended_rfo);
798 		counter_u64_zero(rack_hw_pace_init_fail);
799 		counter_u64_zero(rack_hw_pace_lost);
800 		counter_u64_zero(rack_non_fto_send);
801 		counter_u64_zero(rack_nfto_resend);
802 		counter_u64_zero(rack_sack_proc_short);
803 		counter_u64_zero(rack_sack_proc_restart);
804 		counter_u64_zero(rack_to_alloc);
805 		counter_u64_zero(rack_to_alloc_limited);
806 		counter_u64_zero(rack_alloc_limited_conns);
807 		counter_u64_zero(rack_split_limited);
808 		counter_u64_zero(rack_rxt_clamps_cwnd);
809 		counter_u64_zero(rack_rxt_clamps_cwnd_uniq);
810 		counter_u64_zero(rack_multi_single_eq);
811 		counter_u64_zero(rack_proc_non_comp_ack);
812 		counter_u64_zero(rack_sack_attacks_detected);
813 		counter_u64_zero(rack_sack_attacks_reversed);
814 		counter_u64_zero(rack_sack_attacks_suspect);
815 		counter_u64_zero(rack_sack_used_next_merge);
816 		counter_u64_zero(rack_sack_used_prev_merge);
817 		counter_u64_zero(rack_sack_splits);
818 		counter_u64_zero(rack_sack_skipped_acked);
819 		counter_u64_zero(rack_ack_total);
820 		counter_u64_zero(rack_express_sack);
821 		counter_u64_zero(rack_sack_total);
822 		counter_u64_zero(rack_move_none);
823 		counter_u64_zero(rack_move_some);
824 		counter_u64_zero(rack_try_scwnd);
825 		counter_u64_zero(rack_collapsed_win);
826 		counter_u64_zero(rack_collapsed_win_rxt);
827 		counter_u64_zero(rack_collapsed_win_seen);
828 		counter_u64_zero(rack_collapsed_win_rxt_bytes);
829 	} else if (stat == 2) {
830 #ifdef INVARIANTS
831 		printf("Clearing RACK option array\n");
832 #endif
833 		COUNTER_ARRAY_ZERO(rack_opts_arry, RACK_OPTS_SIZE);
834 	} else if (stat == 3) {
835 		printf("Rack has no stats counters to clear (use 1 to clear all stats in sysctl node)\n");
836 	} else if (stat == 4) {
837 #ifdef INVARIANTS
838 		printf("Clearing RACK out size array\n");
839 #endif
840 		COUNTER_ARRAY_ZERO(rack_out_size, TCP_MSS_ACCT_SIZE);
841 	}
842 	rack_clear_counter = 0;
843 	return (0);
844 }
845 
846 static void
847 rack_init_sysctls(void)
848 {
849 	struct sysctl_oid *rack_counters;
850 	struct sysctl_oid *rack_attack;
851 	struct sysctl_oid *rack_pacing;
852 	struct sysctl_oid *rack_timely;
853 	struct sysctl_oid *rack_timers;
854 	struct sysctl_oid *rack_tlp;
855 	struct sysctl_oid *rack_misc;
856 	struct sysctl_oid *rack_features;
857 	struct sysctl_oid *rack_measure;
858 	struct sysctl_oid *rack_probertt;
859 	struct sysctl_oid *rack_hw_pacing;
860 
861 	rack_attack = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
862 	    SYSCTL_CHILDREN(rack_sysctl_root),
863 	    OID_AUTO,
864 	    "sack_attack",
865 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
866 	    "Rack Sack Attack Counters and Controls");
867 	rack_counters = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
868 	    SYSCTL_CHILDREN(rack_sysctl_root),
869 	    OID_AUTO,
870 	    "stats",
871 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
872 	    "Rack Counters");
873 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
874 	    SYSCTL_CHILDREN(rack_sysctl_root),
875 	    OID_AUTO, "rate_sample_method", CTLFLAG_RW,
876 	    &rack_rate_sample_method , USE_RTT_LOW,
877 	    "What method should we use for rate sampling 0=high, 1=low ");
878 	/* Probe rtt related controls */
879 	rack_probertt = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
880 	    SYSCTL_CHILDREN(rack_sysctl_root),
881 	    OID_AUTO,
882 	    "probertt",
883 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
884 	    "ProbeRTT related Controls");
885 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
886 	    SYSCTL_CHILDREN(rack_probertt),
887 	    OID_AUTO, "exit_per_hpb", CTLFLAG_RW,
888 	    &rack_atexit_prtt_hbp, 130,
889 	    "What percentage above goodput do we clamp CA/SS to at exit on high-BDP path 110%");
890 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
891 	    SYSCTL_CHILDREN(rack_probertt),
892 	    OID_AUTO, "exit_per_nonhpb", CTLFLAG_RW,
893 	    &rack_atexit_prtt, 130,
894 	    "What percentage above goodput do we clamp CA/SS to at exit on a non high-BDP path 100%");
895 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
896 	    SYSCTL_CHILDREN(rack_probertt),
897 	    OID_AUTO, "gp_per_mul", CTLFLAG_RW,
898 	    &rack_per_of_gp_probertt, 60,
899 	    "What percentage of goodput do we pace at in probertt");
900 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
901 	    SYSCTL_CHILDREN(rack_probertt),
902 	    OID_AUTO, "gp_per_reduce", CTLFLAG_RW,
903 	    &rack_per_of_gp_probertt_reduce, 10,
904 	    "What percentage of goodput do we reduce every gp_srtt");
905 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
906 	    SYSCTL_CHILDREN(rack_probertt),
907 	    OID_AUTO, "gp_per_low", CTLFLAG_RW,
908 	    &rack_per_of_gp_lowthresh, 40,
909 	    "What percentage of goodput do we allow the multiplier to fall to");
910 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
911 	    SYSCTL_CHILDREN(rack_probertt),
912 	    OID_AUTO, "time_between", CTLFLAG_RW,
913 	    & rack_time_between_probertt, 96000000,
914 	    "How many useconds between the lowest rtt falling must past before we enter probertt");
915 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
916 	    SYSCTL_CHILDREN(rack_probertt),
917 	    OID_AUTO, "safety", CTLFLAG_RW,
918 	    &rack_probe_rtt_safety_val, 2000000,
919 	    "If not zero, provides a maximum usecond that you can stay in probertt (2sec = 2000000)");
920 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
921 	    SYSCTL_CHILDREN(rack_probertt),
922 	    OID_AUTO, "sets_cwnd", CTLFLAG_RW,
923 	    &rack_probe_rtt_sets_cwnd, 0,
924 	    "Do we set the cwnd too (if always_lower is on)");
925 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
926 	    SYSCTL_CHILDREN(rack_probertt),
927 	    OID_AUTO, "maxdrainsrtts", CTLFLAG_RW,
928 	    &rack_max_drain_wait, 2,
929 	    "Maximum number of gp_srtt's to hold in drain waiting for flight to reach goal");
930 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
931 	    SYSCTL_CHILDREN(rack_probertt),
932 	    OID_AUTO, "mustdrainsrtts", CTLFLAG_RW,
933 	    &rack_must_drain, 1,
934 	    "We must drain this many gp_srtt's waiting for flight to reach goal");
935 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
936 	    SYSCTL_CHILDREN(rack_probertt),
937 	    OID_AUTO, "goal_use_min_entry", CTLFLAG_RW,
938 	    &rack_probertt_use_min_rtt_entry, 1,
939 	    "Should we use the min-rtt to calculate the goal rtt (else gp_srtt) at entry");
940 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
941 	    SYSCTL_CHILDREN(rack_probertt),
942 	    OID_AUTO, "goal_use_min_exit", CTLFLAG_RW,
943 	    &rack_probertt_use_min_rtt_exit, 0,
944 	    "How to set cwnd at exit, 0 - dynamic, 1 - use min-rtt, 2 - use curgprtt, 3 - entry gp-rtt");
945 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
946 	    SYSCTL_CHILDREN(rack_probertt),
947 	    OID_AUTO, "length_div", CTLFLAG_RW,
948 	    &rack_probertt_gpsrtt_cnt_div, 0,
949 	    "How many recent goodput srtt periods plus hold tim does probertt last (bottom of fraction)");
950 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
951 	    SYSCTL_CHILDREN(rack_probertt),
952 	    OID_AUTO, "length_mul", CTLFLAG_RW,
953 	    &rack_probertt_gpsrtt_cnt_mul, 0,
954 	    "How many recent goodput srtt periods plus hold tim does probertt last (top of fraction)");
955 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
956 	    SYSCTL_CHILDREN(rack_probertt),
957 	    OID_AUTO, "holdtim_at_target", CTLFLAG_RW,
958 	    &rack_min_probertt_hold, 200000,
959 	    "What is the minimum time we hold probertt at target");
960 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
961 	    SYSCTL_CHILDREN(rack_probertt),
962 	    OID_AUTO, "filter_life", CTLFLAG_RW,
963 	    &rack_probertt_filter_life, 10000000,
964 	    "What is the time for the filters life in useconds");
965 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
966 	    SYSCTL_CHILDREN(rack_probertt),
967 	    OID_AUTO, "lower_within", CTLFLAG_RW,
968 	    &rack_probertt_lower_within, 10,
969 	    "If the rtt goes lower within this percentage of the time, go into probe-rtt");
970 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
971 	    SYSCTL_CHILDREN(rack_probertt),
972 	    OID_AUTO, "must_move", CTLFLAG_RW,
973 	    &rack_min_rtt_movement, 250,
974 	    "How much is the minimum movement in rtt to count as a drop for probertt purposes");
975 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
976 	    SYSCTL_CHILDREN(rack_probertt),
977 	    OID_AUTO, "clear_is_cnts", CTLFLAG_RW,
978 	    &rack_probertt_clear_is, 1,
979 	    "Do we clear I/S counts on exiting probe-rtt");
980 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
981 	    SYSCTL_CHILDREN(rack_probertt),
982 	    OID_AUTO, "hbp_extra_drain", CTLFLAG_RW,
983 	    &rack_max_drain_hbp, 1,
984 	    "How many extra drain gpsrtt's do we get in highly buffered paths");
985 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
986 	    SYSCTL_CHILDREN(rack_probertt),
987 	    OID_AUTO, "hbp_threshold", CTLFLAG_RW,
988 	    &rack_hbp_thresh, 3,
989 	    "We are highly buffered if min_rtt_seen / max_rtt_seen > this-threshold");
990 	/* Pacing related sysctls */
991 	rack_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
992 	    SYSCTL_CHILDREN(rack_sysctl_root),
993 	    OID_AUTO,
994 	    "pacing",
995 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
996 	    "Pacing related Controls");
997 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
998 	    SYSCTL_CHILDREN(rack_pacing),
999 	    OID_AUTO, "fulldgpinrec", CTLFLAG_RW,
1000 	    &rack_uses_full_dgp_in_rec, 1,
1001 	    "Do we use all DGP features in recovery (fillcw, timely et.al.)?");
1002 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1003 	    SYSCTL_CHILDREN(rack_pacing),
1004 	    OID_AUTO, "fullbufdisc", CTLFLAG_RW,
1005 	    &rack_full_buffer_discount, 10,
1006 	    "What percentage b/w reduction over the GP estimate for a full buffer (default=0 off)?");
1007 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1008 	    SYSCTL_CHILDREN(rack_pacing),
1009 	    OID_AUTO, "fillcw", CTLFLAG_RW,
1010 	    &rack_fill_cw_state, 0,
1011 	    "Enable fillcw on new connections (default=0 off)?");
1012 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1013 	    SYSCTL_CHILDREN(rack_pacing),
1014 	    OID_AUTO, "min_burst", CTLFLAG_RW,
1015 	    &rack_pacing_min_seg, 0,
1016 	    "What is the min burst size for pacing (0 disables)?");
1017 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1018 	    SYSCTL_CHILDREN(rack_pacing),
1019 	    OID_AUTO, "divisor", CTLFLAG_RW,
1020 	    &rack_default_pacing_divisor, 4,
1021 	    "What is the default divisor given to the rl code?");
1022 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1023 	    SYSCTL_CHILDREN(rack_pacing),
1024 	    OID_AUTO, "fillcw_max_mult", CTLFLAG_RW,
1025 	    &rack_bw_multipler, 2,
1026 	    "What is the multiplier of the current gp_est that fillcw can increase the b/w too?");
1027 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1028 	    SYSCTL_CHILDREN(rack_pacing),
1029 	    OID_AUTO, "max_pace_over", CTLFLAG_RW,
1030 	    &rack_max_per_above, 30,
1031 	    "What is the maximum allowable percentage that we can pace above (so 30 = 130% of our goal)");
1032 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1033 	    SYSCTL_CHILDREN(rack_pacing),
1034 	    OID_AUTO, "allow1mss", CTLFLAG_RW,
1035 	    &rack_pace_one_seg, 0,
1036 	    "Do we allow low b/w pacing of 1MSS instead of two (1.2Meg and less)?");
1037 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1038 	    SYSCTL_CHILDREN(rack_pacing),
1039 	    OID_AUTO, "limit_wsrtt", CTLFLAG_RW,
1040 	    &rack_limit_time_with_srtt, 0,
1041 	    "Do we limit pacing time based on srtt");
1042 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1043 	    SYSCTL_CHILDREN(rack_pacing),
1044 	    OID_AUTO, "init_win", CTLFLAG_RW,
1045 	    &rack_default_init_window, 0,
1046 	    "Do we have a rack initial window 0 = system default");
1047 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1048 	    SYSCTL_CHILDREN(rack_pacing),
1049 	    OID_AUTO, "gp_per_ss", CTLFLAG_RW,
1050 	    &rack_per_of_gp_ss, 250,
1051 	    "If non zero, what percentage of goodput to pace at in slow start");
1052 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1053 	    SYSCTL_CHILDREN(rack_pacing),
1054 	    OID_AUTO, "gp_per_ca", CTLFLAG_RW,
1055 	    &rack_per_of_gp_ca, 150,
1056 	    "If non zero, what percentage of goodput to pace at in congestion avoidance");
1057 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1058 	    SYSCTL_CHILDREN(rack_pacing),
1059 	    OID_AUTO, "gp_per_rec", CTLFLAG_RW,
1060 	    &rack_per_of_gp_rec, 200,
1061 	    "If non zero, what percentage of goodput to pace at in recovery");
1062 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1063 	    SYSCTL_CHILDREN(rack_pacing),
1064 	    OID_AUTO, "pace_max_seg", CTLFLAG_RW,
1065 	    &rack_hptsi_segments, 40,
1066 	    "What size is the max for TSO segments in pacing and burst mitigation");
1067 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1068 	    SYSCTL_CHILDREN(rack_pacing),
1069 	    OID_AUTO, "burst_reduces", CTLFLAG_RW,
1070 	    &rack_slot_reduction, 4,
1071 	    "When doing only burst mitigation what is the reduce divisor");
1072 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1073 	    SYSCTL_CHILDREN(rack_sysctl_root),
1074 	    OID_AUTO, "use_pacing", CTLFLAG_RW,
1075 	    &rack_pace_every_seg, 0,
1076 	    "If set we use pacing, if clear we use only the original burst mitigation");
1077 	SYSCTL_ADD_U64(&rack_sysctl_ctx,
1078 	    SYSCTL_CHILDREN(rack_pacing),
1079 	    OID_AUTO, "rate_cap", CTLFLAG_RW,
1080 	    &rack_bw_rate_cap, 0,
1081 	    "If set we apply this value to the absolute rate cap used by pacing");
1082 	SYSCTL_ADD_U8(&rack_sysctl_ctx,
1083 	    SYSCTL_CHILDREN(rack_sysctl_root),
1084 	    OID_AUTO, "req_measure_cnt", CTLFLAG_RW,
1085 	    &rack_req_measurements, 1,
1086 	    "If doing dynamic pacing, how many measurements must be in before we start pacing?");
1087 	/* Hardware pacing */
1088 	rack_hw_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1089 	    SYSCTL_CHILDREN(rack_sysctl_root),
1090 	    OID_AUTO,
1091 	    "hdwr_pacing",
1092 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1093 	    "Pacing related Controls");
1094 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1095 	    SYSCTL_CHILDREN(rack_hw_pacing),
1096 	    OID_AUTO, "rwnd_factor", CTLFLAG_RW,
1097 	    &rack_hw_rwnd_factor, 2,
1098 	    "How many times does snd_wnd need to be bigger than pace_max_seg so we will hold off and get more acks?");
1099 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1100 	    SYSCTL_CHILDREN(rack_hw_pacing),
1101 	    OID_AUTO, "precheck", CTLFLAG_RW,
1102 	    &rack_hw_check_queue, 0,
1103 	    "Do we always precheck the hdwr pacing queue to avoid ENOBUF's?");
1104 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1105 	    SYSCTL_CHILDREN(rack_hw_pacing),
1106 	    OID_AUTO, "pace_enobuf_mult", CTLFLAG_RW,
1107 	    &rack_enobuf_hw_boost_mult, 0,
1108 	    "By how many time_betweens should we boost the pacing time if we see a ENOBUFS?");
1109 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1110 	    SYSCTL_CHILDREN(rack_hw_pacing),
1111 	    OID_AUTO, "pace_enobuf_max", CTLFLAG_RW,
1112 	    &rack_enobuf_hw_max, 2,
1113 	    "What is the max boost the pacing time if we see a ENOBUFS?");
1114 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1115 	    SYSCTL_CHILDREN(rack_hw_pacing),
1116 	    OID_AUTO, "pace_enobuf_min", CTLFLAG_RW,
1117 	    &rack_enobuf_hw_min, 2,
1118 	    "What is the min boost the pacing time if we see a ENOBUFS?");
1119 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1120 	    SYSCTL_CHILDREN(rack_hw_pacing),
1121 	    OID_AUTO, "enable", CTLFLAG_RW,
1122 	    &rack_enable_hw_pacing, 0,
1123 	    "Should RACK attempt to use hw pacing?");
1124 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1125 	    SYSCTL_CHILDREN(rack_hw_pacing),
1126 	    OID_AUTO, "rate_cap", CTLFLAG_RW,
1127 	    &rack_hw_rate_caps, 0,
1128 	    "Does the highest hardware pacing rate cap the rate we will send at??");
1129 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1130 	    SYSCTL_CHILDREN(rack_hw_pacing),
1131 	    OID_AUTO, "uncap_per", CTLFLAG_RW,
1132 	    &rack_hw_rate_cap_per, 0,
1133 	    "If you go over b/w by this amount you will be uncapped (0 = never)");
1134 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1135 	    SYSCTL_CHILDREN(rack_hw_pacing),
1136 	    OID_AUTO, "rate_min", CTLFLAG_RW,
1137 	    &rack_hw_rate_min, 0,
1138 	    "Do we need a minimum estimate of this many bytes per second in order to engage hw pacing?");
1139 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1140 	    SYSCTL_CHILDREN(rack_hw_pacing),
1141 	    OID_AUTO, "rate_to_low", CTLFLAG_RW,
1142 	    &rack_hw_rate_to_low, 0,
1143 	    "If we fall below this rate, dis-engage hw pacing?");
1144 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1145 	    SYSCTL_CHILDREN(rack_hw_pacing),
1146 	    OID_AUTO, "up_only", CTLFLAG_RW,
1147 	    &rack_hw_up_only, 0,
1148 	    "Do we allow hw pacing to lower the rate selected?");
1149 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1150 	    SYSCTL_CHILDREN(rack_hw_pacing),
1151 	    OID_AUTO, "extra_mss_precise", CTLFLAG_RW,
1152 	    &rack_hw_pace_extra_slots, 0,
1153 	    "If the rates between software and hardware match precisely how many extra time_betweens do we get?");
1154 	rack_timely = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1155 	    SYSCTL_CHILDREN(rack_sysctl_root),
1156 	    OID_AUTO,
1157 	    "timely",
1158 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1159 	    "Rack Timely RTT Controls");
1160 	/* Timely based GP dynmics */
1161 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1162 	    SYSCTL_CHILDREN(rack_timely),
1163 	    OID_AUTO, "upper", CTLFLAG_RW,
1164 	    &rack_gp_per_bw_mul_up, 2,
1165 	    "Rack timely upper range for equal b/w (in percentage)");
1166 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1167 	    SYSCTL_CHILDREN(rack_timely),
1168 	    OID_AUTO, "lower", CTLFLAG_RW,
1169 	    &rack_gp_per_bw_mul_down, 4,
1170 	    "Rack timely lower range for equal b/w (in percentage)");
1171 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1172 	    SYSCTL_CHILDREN(rack_timely),
1173 	    OID_AUTO, "rtt_max_mul", CTLFLAG_RW,
1174 	    &rack_gp_rtt_maxmul, 3,
1175 	    "Rack timely multiplier of lowest rtt for rtt_max");
1176 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1177 	    SYSCTL_CHILDREN(rack_timely),
1178 	    OID_AUTO, "rtt_min_div", CTLFLAG_RW,
1179 	    &rack_gp_rtt_mindiv, 4,
1180 	    "Rack timely divisor used for rtt + (rtt * mul/divisor) for check for lower rtt");
1181 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1182 	    SYSCTL_CHILDREN(rack_timely),
1183 	    OID_AUTO, "rtt_min_mul", CTLFLAG_RW,
1184 	    &rack_gp_rtt_minmul, 1,
1185 	    "Rack timely multiplier used for rtt + (rtt * mul/divisor) for check for lower rtt");
1186 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1187 	    SYSCTL_CHILDREN(rack_timely),
1188 	    OID_AUTO, "decrease", CTLFLAG_RW,
1189 	    &rack_gp_decrease_per, 80,
1190 	    "Rack timely Beta value 80 = .8 (scaled by 100)");
1191 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1192 	    SYSCTL_CHILDREN(rack_timely),
1193 	    OID_AUTO, "increase", CTLFLAG_RW,
1194 	    &rack_gp_increase_per, 2,
1195 	    "Rack timely increase perentage of our GP multiplication factor");
1196 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1197 	    SYSCTL_CHILDREN(rack_timely),
1198 	    OID_AUTO, "lowerbound", CTLFLAG_RW,
1199 	    &rack_per_lower_bound, 50,
1200 	    "Rack timely lowest percentage we allow GP multiplier to fall to");
1201 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1202 	    SYSCTL_CHILDREN(rack_timely),
1203 	    OID_AUTO, "p5_upper", CTLFLAG_RW,
1204 	    &rack_gain_p5_ub, 250,
1205 	    "Profile 5 upper bound to timely gain");
1206 
1207 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1208 	    SYSCTL_CHILDREN(rack_timely),
1209 	    OID_AUTO, "upperboundss", CTLFLAG_RW,
1210 	    &rack_per_upper_bound_ss, 0,
1211 	    "Rack timely highest percentage we allow GP multiplier in SS to raise to (0 is no upperbound)");
1212 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1213 	    SYSCTL_CHILDREN(rack_timely),
1214 	    OID_AUTO, "upperboundca", CTLFLAG_RW,
1215 	    &rack_per_upper_bound_ca, 0,
1216 	    "Rack timely highest percentage we allow GP multiplier to CA raise to (0 is no upperbound)");
1217 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1218 	    SYSCTL_CHILDREN(rack_timely),
1219 	    OID_AUTO, "dynamicgp", CTLFLAG_RW,
1220 	    &rack_do_dyn_mul, 0,
1221 	    "Rack timely do we enable dynmaic timely goodput by default");
1222 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1223 	    SYSCTL_CHILDREN(rack_timely),
1224 	    OID_AUTO, "no_rec_red", CTLFLAG_RW,
1225 	    &rack_gp_no_rec_chg, 1,
1226 	    "Rack timely do we prohibit the recovery multiplier from being lowered");
1227 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1228 	    SYSCTL_CHILDREN(rack_timely),
1229 	    OID_AUTO, "red_clear_cnt", CTLFLAG_RW,
1230 	    &rack_timely_dec_clear, 6,
1231 	    "Rack timely what threshold do we count to before another boost during b/w decent");
1232 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1233 	    SYSCTL_CHILDREN(rack_timely),
1234 	    OID_AUTO, "max_push_rise", CTLFLAG_RW,
1235 	    &rack_timely_max_push_rise, 3,
1236 	    "Rack timely how many times do we push up with b/w increase");
1237 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1238 	    SYSCTL_CHILDREN(rack_timely),
1239 	    OID_AUTO, "max_push_drop", CTLFLAG_RW,
1240 	    &rack_timely_max_push_drop, 3,
1241 	    "Rack timely how many times do we push back on b/w decent");
1242 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1243 	    SYSCTL_CHILDREN(rack_timely),
1244 	    OID_AUTO, "min_segs", CTLFLAG_RW,
1245 	    &rack_timely_min_segs, 4,
1246 	    "Rack timely when setting the cwnd what is the min num segments");
1247 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1248 	    SYSCTL_CHILDREN(rack_timely),
1249 	    OID_AUTO, "noback_max", CTLFLAG_RW,
1250 	    &rack_use_max_for_nobackoff, 0,
1251 	    "Rack timely when deciding if to backoff on a loss, do we use under max rtt else min");
1252 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1253 	    SYSCTL_CHILDREN(rack_timely),
1254 	    OID_AUTO, "interim_timely_only", CTLFLAG_RW,
1255 	    &rack_timely_int_timely_only, 0,
1256 	    "Rack timely when doing interim timely's do we only do timely (no b/w consideration)");
1257 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1258 	    SYSCTL_CHILDREN(rack_timely),
1259 	    OID_AUTO, "nonstop", CTLFLAG_RW,
1260 	    &rack_timely_no_stopping, 0,
1261 	    "Rack timely don't stop increase");
1262 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1263 	    SYSCTL_CHILDREN(rack_timely),
1264 	    OID_AUTO, "dec_raise_thresh", CTLFLAG_RW,
1265 	    &rack_down_raise_thresh, 100,
1266 	    "If the CA or SS is below this threshold raise on the first 3 b/w lowers (0=always)");
1267 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1268 	    SYSCTL_CHILDREN(rack_timely),
1269 	    OID_AUTO, "bottom_drag_segs", CTLFLAG_RW,
1270 	    &rack_req_segs, 1,
1271 	    "Bottom dragging if not these many segments outstanding and room");
1272 
1273 	/* TLP and Rack related parameters */
1274 	rack_tlp = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1275 	    SYSCTL_CHILDREN(rack_sysctl_root),
1276 	    OID_AUTO,
1277 	    "tlp",
1278 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1279 	    "TLP and Rack related Controls");
1280 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1281 	    SYSCTL_CHILDREN(rack_tlp),
1282 	    OID_AUTO, "use_rrr", CTLFLAG_RW,
1283 	    &use_rack_rr, 1,
1284 	    "Do we use Rack Rapid Recovery");
1285 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1286 	    SYSCTL_CHILDREN(rack_tlp),
1287 	    OID_AUTO, "post_rec_labc", CTLFLAG_RW,
1288 	    &rack_max_abc_post_recovery, 2,
1289 	    "Since we do early recovery, do we override the l_abc to a value, if so what?");
1290 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1291 	    SYSCTL_CHILDREN(rack_tlp),
1292 	    OID_AUTO, "nonrxt_use_cr", CTLFLAG_RW,
1293 	    &rack_non_rxt_use_cr, 0,
1294 	    "Do we use ss/ca rate if in recovery we are transmitting a new data chunk");
1295 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1296 	    SYSCTL_CHILDREN(rack_tlp),
1297 	    OID_AUTO, "tlpmethod", CTLFLAG_RW,
1298 	    &rack_tlp_threshold_use, TLP_USE_TWO_ONE,
1299 	    "What method do we do for TLP time calc 0=no-de-ack-comp, 1=ID, 2=2.1, 3=2.2");
1300 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1301 	    SYSCTL_CHILDREN(rack_tlp),
1302 	    OID_AUTO, "limit", CTLFLAG_RW,
1303 	    &rack_tlp_limit, 2,
1304 	    "How many TLP's can be sent without sending new data");
1305 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1306 	    SYSCTL_CHILDREN(rack_tlp),
1307 	    OID_AUTO, "use_greater", CTLFLAG_RW,
1308 	    &rack_tlp_use_greater, 1,
1309 	    "Should we use the rack_rtt time if its greater than srtt");
1310 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1311 	    SYSCTL_CHILDREN(rack_tlp),
1312 	    OID_AUTO, "tlpminto", CTLFLAG_RW,
1313 	    &rack_tlp_min, 10000,
1314 	    "TLP minimum timeout per the specification (in microseconds)");
1315 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1316 	    SYSCTL_CHILDREN(rack_tlp),
1317 	    OID_AUTO, "send_oldest", CTLFLAG_RW,
1318 	    &rack_always_send_oldest, 0,
1319 	    "Should we always send the oldest TLP and RACK-TLP");
1320 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1321 	    SYSCTL_CHILDREN(rack_tlp),
1322 	    OID_AUTO, "rack_tlimit", CTLFLAG_RW,
1323 	    &rack_limited_retran, 0,
1324 	    "How many times can a rack timeout drive out sends");
1325 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1326 	    SYSCTL_CHILDREN(rack_tlp),
1327 	    OID_AUTO, "tlp_cwnd_flag", CTLFLAG_RW,
1328 	    &rack_lower_cwnd_at_tlp, 0,
1329 	    "When a TLP completes a retran should we enter recovery");
1330 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1331 	    SYSCTL_CHILDREN(rack_tlp),
1332 	    OID_AUTO, "reorder_thresh", CTLFLAG_RW,
1333 	    &rack_reorder_thresh, 2,
1334 	    "What factor for rack will be added when seeing reordering (shift right)");
1335 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1336 	    SYSCTL_CHILDREN(rack_tlp),
1337 	    OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW,
1338 	    &rack_tlp_thresh, 1,
1339 	    "What divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)");
1340 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1341 	    SYSCTL_CHILDREN(rack_tlp),
1342 	    OID_AUTO, "reorder_fade", CTLFLAG_RW,
1343 	    &rack_reorder_fade, 60000000,
1344 	    "Does reorder detection fade, if so how many microseconds (0 means never)");
1345 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1346 	    SYSCTL_CHILDREN(rack_tlp),
1347 	    OID_AUTO, "pktdelay", CTLFLAG_RW,
1348 	    &rack_pkt_delay, 1000,
1349 	    "Extra RACK time (in microseconds) besides reordering thresh");
1350 
1351 	/* Timer related controls */
1352 	rack_timers = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1353 	    SYSCTL_CHILDREN(rack_sysctl_root),
1354 	    OID_AUTO,
1355 	    "timers",
1356 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1357 	    "Timer related controls");
1358 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1359 	    SYSCTL_CHILDREN(rack_timers),
1360 	    OID_AUTO, "persmin", CTLFLAG_RW,
1361 	    &rack_persist_min, 250000,
1362 	    "What is the minimum time in microseconds between persists");
1363 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1364 	    SYSCTL_CHILDREN(rack_timers),
1365 	    OID_AUTO, "persmax", CTLFLAG_RW,
1366 	    &rack_persist_max, 2000000,
1367 	    "What is the largest delay in microseconds between persists");
1368 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1369 	    SYSCTL_CHILDREN(rack_timers),
1370 	    OID_AUTO, "delayed_ack", CTLFLAG_RW,
1371 	    &rack_delayed_ack_time, 40000,
1372 	    "Delayed ack time (40ms in microseconds)");
1373 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1374 	    SYSCTL_CHILDREN(rack_timers),
1375 	    OID_AUTO, "minrto", CTLFLAG_RW,
1376 	    &rack_rto_min, 30000,
1377 	    "Minimum RTO in microseconds -- set with caution below 1000 due to TLP");
1378 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1379 	    SYSCTL_CHILDREN(rack_timers),
1380 	    OID_AUTO, "maxrto", CTLFLAG_RW,
1381 	    &rack_rto_max, 4000000,
1382 	    "Maximum RTO in microseconds -- should be at least as large as min_rto");
1383 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1384 	    SYSCTL_CHILDREN(rack_timers),
1385 	    OID_AUTO, "minto", CTLFLAG_RW,
1386 	    &rack_min_to, 1000,
1387 	    "Minimum rack timeout in microseconds");
1388 	/* Measure controls */
1389 	rack_measure = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1390 	    SYSCTL_CHILDREN(rack_sysctl_root),
1391 	    OID_AUTO,
1392 	    "measure",
1393 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1394 	    "Measure related controls");
1395 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1396 	    SYSCTL_CHILDREN(rack_measure),
1397 	    OID_AUTO, "wma_divisor", CTLFLAG_RW,
1398 	    &rack_wma_divisor, 8,
1399 	    "When doing b/w calculation what is the  divisor for the WMA");
1400 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1401 	    SYSCTL_CHILDREN(rack_measure),
1402 	    OID_AUTO, "end_cwnd", CTLFLAG_RW,
1403 	    &rack_cwnd_block_ends_measure, 0,
1404 	    "Does a cwnd just-return end the measurement window (app limited)");
1405 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1406 	    SYSCTL_CHILDREN(rack_measure),
1407 	    OID_AUTO, "end_rwnd", CTLFLAG_RW,
1408 	    &rack_rwnd_block_ends_measure, 0,
1409 	    "Does an rwnd just-return end the measurement window (app limited -- not persists)");
1410 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1411 	    SYSCTL_CHILDREN(rack_measure),
1412 	    OID_AUTO, "min_target", CTLFLAG_RW,
1413 	    &rack_def_data_window, 20,
1414 	    "What is the minimum target window (in mss) for a GP measurements");
1415 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1416 	    SYSCTL_CHILDREN(rack_measure),
1417 	    OID_AUTO, "goal_bdp", CTLFLAG_RW,
1418 	    &rack_goal_bdp, 2,
1419 	    "What is the goal BDP to measure");
1420 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1421 	    SYSCTL_CHILDREN(rack_measure),
1422 	    OID_AUTO, "min_srtts", CTLFLAG_RW,
1423 	    &rack_min_srtts, 1,
1424 	    "What is the goal BDP to measure");
1425 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1426 	    SYSCTL_CHILDREN(rack_measure),
1427 	    OID_AUTO, "min_measure_tim", CTLFLAG_RW,
1428 	    &rack_min_measure_usec, 0,
1429 	    "What is the Minimum time time for a measurement if 0, this is off");
1430 	/* Features */
1431 	rack_features = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1432 	    SYSCTL_CHILDREN(rack_sysctl_root),
1433 	    OID_AUTO,
1434 	    "features",
1435 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1436 	    "Feature controls");
1437 	SYSCTL_ADD_U64(&rack_sysctl_ctx,
1438 	    SYSCTL_CHILDREN(rack_features),
1439 	    OID_AUTO, "rxt_clamp_thresh", CTLFLAG_RW,
1440 	    &rack_rxt_clamp_thresh, 0,
1441 	    "Bit encoded clamping setup bits CCCC CCCCC UUUU UULF PPPP PPPP PPPP PPPP");
1442 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1443 	    SYSCTL_CHILDREN(rack_features),
1444 	    OID_AUTO, "hybrid_set_maxseg", CTLFLAG_RW,
1445 	    &rack_hybrid_allow_set_maxseg, 0,
1446 	    "Should hybrid pacing allow the setmss command");
1447 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1448 	    SYSCTL_CHILDREN(rack_features),
1449 	    OID_AUTO, "cmpack", CTLFLAG_RW,
1450 	    &rack_use_cmp_acks, 1,
1451 	    "Should RACK have LRO send compressed acks");
1452 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1453 	    SYSCTL_CHILDREN(rack_features),
1454 	    OID_AUTO, "fsb", CTLFLAG_RW,
1455 	    &rack_use_fsb, 1,
1456 	    "Should RACK use the fast send block?");
1457 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1458 	    SYSCTL_CHILDREN(rack_features),
1459 	    OID_AUTO, "rfo", CTLFLAG_RW,
1460 	    &rack_use_rfo, 1,
1461 	    "Should RACK use rack_fast_output()?");
1462 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1463 	    SYSCTL_CHILDREN(rack_features),
1464 	    OID_AUTO, "rsmrfo", CTLFLAG_RW,
1465 	    &rack_use_rsm_rfo, 1,
1466 	    "Should RACK use rack_fast_rsm_output()?");
1467 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1468 	    SYSCTL_CHILDREN(rack_features),
1469 	    OID_AUTO, "non_paced_lro_queue", CTLFLAG_RW,
1470 	    &rack_enable_mqueue_for_nonpaced, 0,
1471 	    "Should RACK use mbuf queuing for non-paced connections");
1472 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1473 	    SYSCTL_CHILDREN(rack_features),
1474 	    OID_AUTO, "hystartplusplus", CTLFLAG_RW,
1475 	    &rack_do_hystart, 0,
1476 	    "Should RACK enable HyStart++ on connections?");
1477 	/* Misc rack controls */
1478 	rack_misc = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1479 	    SYSCTL_CHILDREN(rack_sysctl_root),
1480 	    OID_AUTO,
1481 	    "misc",
1482 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1483 	    "Misc related controls");
1484 #ifdef TCP_ACCOUNTING
1485 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1486 	    SYSCTL_CHILDREN(rack_misc),
1487 	    OID_AUTO, "tcp_acct", CTLFLAG_RW,
1488 	    &rack_tcp_accounting, 0,
1489 	    "Should we turn on TCP accounting for all rack sessions?");
1490 #endif
1491 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1492 	    SYSCTL_CHILDREN(rack_misc),
1493 	    OID_AUTO, "dnd", CTLFLAG_RW,
1494 	    &rack_dnd_default, 0,
1495 	    "Do not disturb default for rack_rrr = 3");
1496 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1497 	    SYSCTL_CHILDREN(rack_misc),
1498 	    OID_AUTO, "sad_seg_per", CTLFLAG_RW,
1499 	    &sad_seg_size_per, 800,
1500 	    "Percentage of segment size needed in a sack 800 = 80.0?");
1501 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1502 	    SYSCTL_CHILDREN(rack_misc),
1503 	    OID_AUTO, "rxt_controls", CTLFLAG_RW,
1504 	    &rack_rxt_controls, 0,
1505 	    "Retransmit sending size controls (valid  values 0, 1, 2 default=1)?");
1506 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1507 	    SYSCTL_CHILDREN(rack_misc),
1508 	    OID_AUTO, "rack_hibeta", CTLFLAG_RW,
1509 	    &rack_hibeta_setting, 0,
1510 	    "Do we ue a high beta (80 instead of 50)?");
1511 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1512 	    SYSCTL_CHILDREN(rack_misc),
1513 	    OID_AUTO, "apply_rtt_with_low_conf", CTLFLAG_RW,
1514 	    &rack_apply_rtt_with_reduced_conf, 0,
1515 	    "When a persist or keep-alive probe is not answered do we calculate rtt on subsequent answers?");
1516 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1517 	    SYSCTL_CHILDREN(rack_misc),
1518 	    OID_AUTO, "rack_dsack_ctl", CTLFLAG_RW,
1519 	    &rack_dsack_std_based, 3,
1520 	    "How do we process dsack with respect to rack timers, bit field, 3 is standards based?");
1521 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1522 	    SYSCTL_CHILDREN(rack_misc),
1523 	    OID_AUTO, "prr_addback_max", CTLFLAG_RW,
1524 	    &rack_prr_addbackmax, 2,
1525 	    "What is the maximum number of MSS we allow to be added back if prr can't send all its data?");
1526 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1527 	    SYSCTL_CHILDREN(rack_misc),
1528 	    OID_AUTO, "stats_gets_ms", CTLFLAG_RW,
1529 	    &rack_stats_gets_ms_rtt, 1,
1530 	    "What do we feed the stats framework (1 = ms_rtt, 0 = us_rtt, 2 = ms_rtt from hdwr, > 2 usec rtt from hdwr)?");
1531 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1532 	    SYSCTL_CHILDREN(rack_misc),
1533 	    OID_AUTO, "clientlowbuf", CTLFLAG_RW,
1534 	    &rack_client_low_buf, 0,
1535 	    "Client low buffer level (below this we are more aggressive in DGP exiting recovery (0 = off)?");
1536 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1537 	    SYSCTL_CHILDREN(rack_misc),
1538 	    OID_AUTO, "defprofile", CTLFLAG_RW,
1539 	    &rack_def_profile, 0,
1540 	    "Should RACK use a default profile (0=no, num == profile num)?");
1541 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1542 	    SYSCTL_CHILDREN(rack_misc),
1543 	    OID_AUTO, "shared_cwnd", CTLFLAG_RW,
1544 	    &rack_enable_shared_cwnd, 1,
1545 	    "Should RACK try to use the shared cwnd on connections where allowed");
1546 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1547 	    SYSCTL_CHILDREN(rack_misc),
1548 	    OID_AUTO, "limits_on_scwnd", CTLFLAG_RW,
1549 	    &rack_limits_scwnd, 1,
1550 	    "Should RACK place low end time limits on the shared cwnd feature");
1551 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1552 	    SYSCTL_CHILDREN(rack_misc),
1553 	    OID_AUTO, "no_prr", CTLFLAG_RW,
1554 	    &rack_disable_prr, 0,
1555 	    "Should RACK not use prr and only pace (must have pacing on)");
1556 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1557 	    SYSCTL_CHILDREN(rack_misc),
1558 	    OID_AUTO, "bb_verbose", CTLFLAG_RW,
1559 	    &rack_verbose_logging, 0,
1560 	    "Should RACK black box logging be verbose");
1561 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1562 	    SYSCTL_CHILDREN(rack_misc),
1563 	    OID_AUTO, "data_after_close", CTLFLAG_RW,
1564 	    &rack_ignore_data_after_close, 1,
1565 	    "Do we hold off sending a RST until all pending data is ack'd");
1566 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1567 	    SYSCTL_CHILDREN(rack_misc),
1568 	    OID_AUTO, "no_sack_needed", CTLFLAG_RW,
1569 	    &rack_sack_not_required, 1,
1570 	    "Do we allow rack to run on connections not supporting SACK");
1571 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1572 	    SYSCTL_CHILDREN(rack_misc),
1573 	    OID_AUTO, "prr_sendalot", CTLFLAG_RW,
1574 	    &rack_send_a_lot_in_prr, 1,
1575 	    "Send a lot in prr");
1576 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1577 	    SYSCTL_CHILDREN(rack_misc),
1578 	    OID_AUTO, "autoscale", CTLFLAG_RW,
1579 	    &rack_autosndbuf_inc, 20,
1580 	    "What percentage should rack scale up its snd buffer by?");
1581 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1582 	    SYSCTL_CHILDREN(rack_misc),
1583 	    OID_AUTO, "rnds_for_rxt_clamp", CTLFLAG_RW,
1584 	    &rack_rxt_min_rnds, 10,
1585 	    "Number of rounds needed between RTT clamps due to high loss rates");
1586 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1587 	    SYSCTL_CHILDREN(rack_misc),
1588 	    OID_AUTO, "rnds_for_unclamp", CTLFLAG_RW,
1589 	    &rack_unclamp_round_thresh, 100,
1590 	    "Number of rounds needed with no loss to unclamp");
1591 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1592 	    SYSCTL_CHILDREN(rack_misc),
1593 	    OID_AUTO, "rxt_threshs_for_unclamp", CTLFLAG_RW,
1594 	    &rack_unclamp_rxt_thresh, 5,
1595 	   "Percentage of retransmits we need to be under to unclamp (5 = .5 percent)\n");
1596 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1597 	    SYSCTL_CHILDREN(rack_misc),
1598 	    OID_AUTO, "clamp_ss_upper", CTLFLAG_RW,
1599 	    &rack_clamp_ss_upper, 110,
1600 	    "Clamp percentage ceiling in SS?");
1601 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1602 	    SYSCTL_CHILDREN(rack_misc),
1603 	    OID_AUTO, "clamp_ca_upper", CTLFLAG_RW,
1604 	    &rack_clamp_ca_upper, 110,
1605 	    "Clamp percentage ceiling in CA?");
1606 	/* Sack Attacker detection stuff */
1607 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1608 	    SYSCTL_CHILDREN(rack_attack),
1609 	    OID_AUTO, "merge_out", CTLFLAG_RW,
1610 	    &rack_merge_out_sacks_on_attack, 0,
1611 	    "Do we merge the sendmap when we decide we are being attacked?");
1612 
1613 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1614 	    SYSCTL_CHILDREN(rack_attack),
1615 	    OID_AUTO, "detect_highsackratio", CTLFLAG_RW,
1616 	    &rack_highest_sack_thresh_seen, 0,
1617 	    "Highest sack to ack ratio seen");
1618 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1619 	    SYSCTL_CHILDREN(rack_attack),
1620 	    OID_AUTO, "detect_highmoveratio", CTLFLAG_RW,
1621 	    &rack_highest_move_thresh_seen, 0,
1622 	    "Highest move to non-move ratio seen");
1623 	rack_ack_total = counter_u64_alloc(M_WAITOK);
1624 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1625 	    SYSCTL_CHILDREN(rack_attack),
1626 	    OID_AUTO, "acktotal", CTLFLAG_RD,
1627 	    &rack_ack_total,
1628 	    "Total number of Ack's");
1629 	rack_express_sack = counter_u64_alloc(M_WAITOK);
1630 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1631 	    SYSCTL_CHILDREN(rack_attack),
1632 	    OID_AUTO, "exp_sacktotal", CTLFLAG_RD,
1633 	    &rack_express_sack,
1634 	    "Total expresss number of Sack's");
1635 	rack_sack_total = counter_u64_alloc(M_WAITOK);
1636 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1637 	    SYSCTL_CHILDREN(rack_attack),
1638 	    OID_AUTO, "sacktotal", CTLFLAG_RD,
1639 	    &rack_sack_total,
1640 	    "Total number of SACKs");
1641 	rack_move_none = counter_u64_alloc(M_WAITOK);
1642 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1643 	    SYSCTL_CHILDREN(rack_attack),
1644 	    OID_AUTO, "move_none", CTLFLAG_RD,
1645 	    &rack_move_none,
1646 	    "Total number of SACK index reuse of positions under threshold");
1647 	rack_move_some = counter_u64_alloc(M_WAITOK);
1648 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1649 	    SYSCTL_CHILDREN(rack_attack),
1650 	    OID_AUTO, "move_some", CTLFLAG_RD,
1651 	    &rack_move_some,
1652 	    "Total number of SACK index reuse of positions over threshold");
1653 	rack_sack_attacks_detected = counter_u64_alloc(M_WAITOK);
1654 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1655 	    SYSCTL_CHILDREN(rack_attack),
1656 	    OID_AUTO, "attacks", CTLFLAG_RD,
1657 	    &rack_sack_attacks_detected,
1658 	    "Total number of SACK attackers that had sack disabled");
1659 	rack_sack_attacks_reversed = counter_u64_alloc(M_WAITOK);
1660 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1661 	    SYSCTL_CHILDREN(rack_attack),
1662 	    OID_AUTO, "reversed", CTLFLAG_RD,
1663 	    &rack_sack_attacks_reversed,
1664 	    "Total number of SACK attackers that were later determined false positive");
1665 	rack_sack_attacks_suspect = counter_u64_alloc(M_WAITOK);
1666 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1667 	    SYSCTL_CHILDREN(rack_attack),
1668 	    OID_AUTO, "suspect", CTLFLAG_RD,
1669 	    &rack_sack_attacks_suspect,
1670 	    "Total number of SACKs that triggered early detection");
1671 
1672 	rack_sack_used_next_merge = counter_u64_alloc(M_WAITOK);
1673 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1674 	    SYSCTL_CHILDREN(rack_attack),
1675 	    OID_AUTO, "nextmerge", CTLFLAG_RD,
1676 	    &rack_sack_used_next_merge,
1677 	    "Total number of times we used the next merge");
1678 	rack_sack_used_prev_merge = counter_u64_alloc(M_WAITOK);
1679 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1680 	    SYSCTL_CHILDREN(rack_attack),
1681 	    OID_AUTO, "prevmerge", CTLFLAG_RD,
1682 	    &rack_sack_used_prev_merge,
1683 	    "Total number of times we used the prev merge");
1684 	/* Counters */
1685 	rack_total_bytes = counter_u64_alloc(M_WAITOK);
1686 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1687 	    SYSCTL_CHILDREN(rack_counters),
1688 	    OID_AUTO, "totalbytes", CTLFLAG_RD,
1689 	    &rack_total_bytes,
1690 	    "Total number of bytes sent");
1691 	rack_fto_send = counter_u64_alloc(M_WAITOK);
1692 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1693 	    SYSCTL_CHILDREN(rack_counters),
1694 	    OID_AUTO, "fto_send", CTLFLAG_RD,
1695 	    &rack_fto_send, "Total number of rack_fast_output sends");
1696 	rack_fto_rsm_send = counter_u64_alloc(M_WAITOK);
1697 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1698 	    SYSCTL_CHILDREN(rack_counters),
1699 	    OID_AUTO, "fto_rsm_send", CTLFLAG_RD,
1700 	    &rack_fto_rsm_send, "Total number of rack_fast_rsm_output sends");
1701 	rack_nfto_resend = counter_u64_alloc(M_WAITOK);
1702 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1703 	    SYSCTL_CHILDREN(rack_counters),
1704 	    OID_AUTO, "nfto_resend", CTLFLAG_RD,
1705 	    &rack_nfto_resend, "Total number of rack_output retransmissions");
1706 	rack_non_fto_send = counter_u64_alloc(M_WAITOK);
1707 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1708 	    SYSCTL_CHILDREN(rack_counters),
1709 	    OID_AUTO, "nfto_send", CTLFLAG_RD,
1710 	    &rack_non_fto_send, "Total number of rack_output first sends");
1711 	rack_extended_rfo = counter_u64_alloc(M_WAITOK);
1712 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1713 	    SYSCTL_CHILDREN(rack_counters),
1714 	    OID_AUTO, "rfo_extended", CTLFLAG_RD,
1715 	    &rack_extended_rfo, "Total number of times we extended rfo");
1716 
1717 	rack_hw_pace_init_fail = counter_u64_alloc(M_WAITOK);
1718 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1719 	    SYSCTL_CHILDREN(rack_counters),
1720 	    OID_AUTO, "hwpace_init_fail", CTLFLAG_RD,
1721 	    &rack_hw_pace_init_fail, "Total number of times we failed to initialize hw pacing");
1722 	rack_hw_pace_lost = counter_u64_alloc(M_WAITOK);
1723 
1724 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1725 	    SYSCTL_CHILDREN(rack_counters),
1726 	    OID_AUTO, "hwpace_lost", CTLFLAG_RD,
1727 	    &rack_hw_pace_lost, "Total number of times we failed to initialize hw pacing");
1728 	rack_tlp_tot = counter_u64_alloc(M_WAITOK);
1729 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1730 	    SYSCTL_CHILDREN(rack_counters),
1731 	    OID_AUTO, "tlp_to_total", CTLFLAG_RD,
1732 	    &rack_tlp_tot,
1733 	    "Total number of tail loss probe expirations");
1734 	rack_tlp_newdata = counter_u64_alloc(M_WAITOK);
1735 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1736 	    SYSCTL_CHILDREN(rack_counters),
1737 	    OID_AUTO, "tlp_new", CTLFLAG_RD,
1738 	    &rack_tlp_newdata,
1739 	    "Total number of tail loss probe sending new data");
1740 	rack_tlp_retran = counter_u64_alloc(M_WAITOK);
1741 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1742 	    SYSCTL_CHILDREN(rack_counters),
1743 	    OID_AUTO, "tlp_retran", CTLFLAG_RD,
1744 	    &rack_tlp_retran,
1745 	    "Total number of tail loss probe sending retransmitted data");
1746 	rack_tlp_retran_bytes = counter_u64_alloc(M_WAITOK);
1747 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1748 	    SYSCTL_CHILDREN(rack_counters),
1749 	    OID_AUTO, "tlp_retran_bytes", CTLFLAG_RD,
1750 	    &rack_tlp_retran_bytes,
1751 	    "Total bytes of tail loss probe sending retransmitted data");
1752 	rack_to_tot = counter_u64_alloc(M_WAITOK);
1753 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1754 	    SYSCTL_CHILDREN(rack_counters),
1755 	    OID_AUTO, "rack_to_tot", CTLFLAG_RD,
1756 	    &rack_to_tot,
1757 	    "Total number of times the rack to expired");
1758 	rack_saw_enobuf = counter_u64_alloc(M_WAITOK);
1759 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1760 	    SYSCTL_CHILDREN(rack_counters),
1761 	    OID_AUTO, "saw_enobufs", CTLFLAG_RD,
1762 	    &rack_saw_enobuf,
1763 	    "Total number of times a sends returned enobuf for non-hdwr paced connections");
1764 	rack_saw_enobuf_hw = counter_u64_alloc(M_WAITOK);
1765 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1766 	    SYSCTL_CHILDREN(rack_counters),
1767 	    OID_AUTO, "saw_enobufs_hw", CTLFLAG_RD,
1768 	    &rack_saw_enobuf_hw,
1769 	    "Total number of times a send returned enobuf for hdwr paced connections");
1770 	rack_saw_enetunreach = counter_u64_alloc(M_WAITOK);
1771 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1772 	    SYSCTL_CHILDREN(rack_counters),
1773 	    OID_AUTO, "saw_enetunreach", CTLFLAG_RD,
1774 	    &rack_saw_enetunreach,
1775 	    "Total number of times a send received a enetunreachable");
1776 	rack_hot_alloc = counter_u64_alloc(M_WAITOK);
1777 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1778 	    SYSCTL_CHILDREN(rack_counters),
1779 	    OID_AUTO, "alloc_hot", CTLFLAG_RD,
1780 	    &rack_hot_alloc,
1781 	    "Total allocations from the top of our list");
1782 	rack_to_alloc = counter_u64_alloc(M_WAITOK);
1783 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1784 	    SYSCTL_CHILDREN(rack_counters),
1785 	    OID_AUTO, "allocs", CTLFLAG_RD,
1786 	    &rack_to_alloc,
1787 	    "Total allocations of tracking structures");
1788 	rack_to_alloc_hard = counter_u64_alloc(M_WAITOK);
1789 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1790 	    SYSCTL_CHILDREN(rack_counters),
1791 	    OID_AUTO, "allochard", CTLFLAG_RD,
1792 	    &rack_to_alloc_hard,
1793 	    "Total allocations done with sleeping the hard way");
1794 	rack_to_alloc_emerg = counter_u64_alloc(M_WAITOK);
1795 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1796 	    SYSCTL_CHILDREN(rack_counters),
1797 	    OID_AUTO, "allocemerg", CTLFLAG_RD,
1798 	    &rack_to_alloc_emerg,
1799 	    "Total allocations done from emergency cache");
1800 	rack_to_alloc_limited = counter_u64_alloc(M_WAITOK);
1801 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1802 	    SYSCTL_CHILDREN(rack_counters),
1803 	    OID_AUTO, "alloc_limited", CTLFLAG_RD,
1804 	    &rack_to_alloc_limited,
1805 	    "Total allocations dropped due to limit");
1806 	rack_alloc_limited_conns = counter_u64_alloc(M_WAITOK);
1807 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1808 	    SYSCTL_CHILDREN(rack_counters),
1809 	    OID_AUTO, "alloc_limited_conns", CTLFLAG_RD,
1810 	    &rack_alloc_limited_conns,
1811 	    "Connections with allocations dropped due to limit");
1812 	rack_split_limited = counter_u64_alloc(M_WAITOK);
1813 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1814 	    SYSCTL_CHILDREN(rack_counters),
1815 	    OID_AUTO, "split_limited", CTLFLAG_RD,
1816 	    &rack_split_limited,
1817 	    "Split allocations dropped due to limit");
1818 	rack_rxt_clamps_cwnd = counter_u64_alloc(M_WAITOK);
1819 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1820 	    SYSCTL_CHILDREN(rack_counters),
1821 	    OID_AUTO, "rxt_clamps_cwnd", CTLFLAG_RD,
1822 	    &rack_rxt_clamps_cwnd,
1823 	    "Number of times that excessive rxt clamped the cwnd down");
1824 	rack_rxt_clamps_cwnd_uniq = counter_u64_alloc(M_WAITOK);
1825 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1826 	    SYSCTL_CHILDREN(rack_counters),
1827 	    OID_AUTO, "rxt_clamps_cwnd_uniq", CTLFLAG_RD,
1828 	    &rack_rxt_clamps_cwnd_uniq,
1829 	    "Number of connections that have had excessive rxt clamped the cwnd down");
1830 	rack_persists_sends = counter_u64_alloc(M_WAITOK);
1831 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1832 	    SYSCTL_CHILDREN(rack_counters),
1833 	    OID_AUTO, "persist_sends", CTLFLAG_RD,
1834 	    &rack_persists_sends,
1835 	    "Number of times we sent a persist probe");
1836 	rack_persists_acks = counter_u64_alloc(M_WAITOK);
1837 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1838 	    SYSCTL_CHILDREN(rack_counters),
1839 	    OID_AUTO, "persist_acks", CTLFLAG_RD,
1840 	    &rack_persists_acks,
1841 	    "Number of times a persist probe was acked");
1842 	rack_persists_loss = counter_u64_alloc(M_WAITOK);
1843 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1844 	    SYSCTL_CHILDREN(rack_counters),
1845 	    OID_AUTO, "persist_loss", CTLFLAG_RD,
1846 	    &rack_persists_loss,
1847 	    "Number of times we detected a lost persist probe (no ack)");
1848 	rack_persists_lost_ends = counter_u64_alloc(M_WAITOK);
1849 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1850 	    SYSCTL_CHILDREN(rack_counters),
1851 	    OID_AUTO, "persist_loss_ends", CTLFLAG_RD,
1852 	    &rack_persists_lost_ends,
1853 	    "Number of lost persist probe (no ack) that the run ended with a PERSIST abort");
1854 #ifdef INVARIANTS
1855 	rack_adjust_map_bw = counter_u64_alloc(M_WAITOK);
1856 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1857 	    SYSCTL_CHILDREN(rack_counters),
1858 	    OID_AUTO, "map_adjust_req", CTLFLAG_RD,
1859 	    &rack_adjust_map_bw,
1860 	    "Number of times we hit the case where the sb went up and down on a sendmap entry");
1861 #endif
1862 	rack_multi_single_eq = counter_u64_alloc(M_WAITOK);
1863 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1864 	    SYSCTL_CHILDREN(rack_counters),
1865 	    OID_AUTO, "cmp_ack_equiv", CTLFLAG_RD,
1866 	    &rack_multi_single_eq,
1867 	    "Number of compressed acks total represented");
1868 	rack_proc_non_comp_ack = counter_u64_alloc(M_WAITOK);
1869 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1870 	    SYSCTL_CHILDREN(rack_counters),
1871 	    OID_AUTO, "cmp_ack_not", CTLFLAG_RD,
1872 	    &rack_proc_non_comp_ack,
1873 	    "Number of non compresseds acks that we processed");
1874 
1875 
1876 	rack_sack_proc_all = counter_u64_alloc(M_WAITOK);
1877 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1878 	    SYSCTL_CHILDREN(rack_counters),
1879 	    OID_AUTO, "sack_long", CTLFLAG_RD,
1880 	    &rack_sack_proc_all,
1881 	    "Total times we had to walk whole list for sack processing");
1882 	rack_sack_proc_restart = counter_u64_alloc(M_WAITOK);
1883 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1884 	    SYSCTL_CHILDREN(rack_counters),
1885 	    OID_AUTO, "sack_restart", CTLFLAG_RD,
1886 	    &rack_sack_proc_restart,
1887 	    "Total times we had to walk whole list due to a restart");
1888 	rack_sack_proc_short = counter_u64_alloc(M_WAITOK);
1889 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1890 	    SYSCTL_CHILDREN(rack_counters),
1891 	    OID_AUTO, "sack_short", CTLFLAG_RD,
1892 	    &rack_sack_proc_short,
1893 	    "Total times we took shortcut for sack processing");
1894 	rack_sack_skipped_acked = counter_u64_alloc(M_WAITOK);
1895 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1896 	    SYSCTL_CHILDREN(rack_attack),
1897 	    OID_AUTO, "skipacked", CTLFLAG_RD,
1898 	    &rack_sack_skipped_acked,
1899 	    "Total number of times we skipped previously sacked");
1900 	rack_sack_splits = counter_u64_alloc(M_WAITOK);
1901 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1902 	    SYSCTL_CHILDREN(rack_attack),
1903 	    OID_AUTO, "ofsplit", CTLFLAG_RD,
1904 	    &rack_sack_splits,
1905 	    "Total number of times we did the old fashion tree split");
1906 	rack_input_idle_reduces = counter_u64_alloc(M_WAITOK);
1907 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1908 	    SYSCTL_CHILDREN(rack_counters),
1909 	    OID_AUTO, "idle_reduce_oninput", CTLFLAG_RD,
1910 	    &rack_input_idle_reduces,
1911 	    "Total number of idle reductions on input");
1912 	rack_collapsed_win_seen = counter_u64_alloc(M_WAITOK);
1913 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1914 	    SYSCTL_CHILDREN(rack_counters),
1915 	    OID_AUTO, "collapsed_win_seen", CTLFLAG_RD,
1916 	    &rack_collapsed_win_seen,
1917 	    "Total number of collapsed window events seen (where our window shrinks)");
1918 
1919 	rack_collapsed_win = counter_u64_alloc(M_WAITOK);
1920 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1921 	    SYSCTL_CHILDREN(rack_counters),
1922 	    OID_AUTO, "collapsed_win", CTLFLAG_RD,
1923 	    &rack_collapsed_win,
1924 	    "Total number of collapsed window events where we mark packets");
1925 	rack_collapsed_win_rxt = counter_u64_alloc(M_WAITOK);
1926 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1927 	    SYSCTL_CHILDREN(rack_counters),
1928 	    OID_AUTO, "collapsed_win_rxt", CTLFLAG_RD,
1929 	    &rack_collapsed_win_rxt,
1930 	    "Total number of packets that were retransmitted");
1931 	rack_collapsed_win_rxt_bytes = counter_u64_alloc(M_WAITOK);
1932 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1933 	    SYSCTL_CHILDREN(rack_counters),
1934 	    OID_AUTO, "collapsed_win_bytes", CTLFLAG_RD,
1935 	    &rack_collapsed_win_rxt_bytes,
1936 	    "Total number of bytes that were retransmitted");
1937 	rack_try_scwnd = counter_u64_alloc(M_WAITOK);
1938 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1939 	    SYSCTL_CHILDREN(rack_counters),
1940 	    OID_AUTO, "tried_scwnd", CTLFLAG_RD,
1941 	    &rack_try_scwnd,
1942 	    "Total number of scwnd attempts");
1943 	COUNTER_ARRAY_ALLOC(rack_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK);
1944 	SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
1945 	    OID_AUTO, "outsize", CTLFLAG_RD,
1946 	    rack_out_size, TCP_MSS_ACCT_SIZE, "MSS send sizes");
1947 	COUNTER_ARRAY_ALLOC(rack_opts_arry, RACK_OPTS_SIZE, M_WAITOK);
1948 	SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
1949 	    OID_AUTO, "opts", CTLFLAG_RD,
1950 	    rack_opts_arry, RACK_OPTS_SIZE, "RACK Option Stats");
1951 	SYSCTL_ADD_PROC(&rack_sysctl_ctx,
1952 	    SYSCTL_CHILDREN(rack_sysctl_root),
1953 	    OID_AUTO, "clear", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1954 	    &rack_clear_counter, 0, sysctl_rack_clear, "IU", "Clear counters");
1955 }
1956 
1957 static uint32_t
1958 rc_init_window(struct tcp_rack *rack)
1959 {
1960 	uint32_t win;
1961 
1962 	if (rack->rc_init_win == 0) {
1963 		/*
1964 		 * Nothing set by the user, use the system stack
1965 		 * default.
1966 		 */
1967 		return (tcp_compute_initwnd(tcp_maxseg(rack->rc_tp)));
1968 	}
1969 	win = ctf_fixed_maxseg(rack->rc_tp) * rack->rc_init_win;
1970 	return (win);
1971 }
1972 
1973 static uint64_t
1974 rack_get_fixed_pacing_bw(struct tcp_rack *rack)
1975 {
1976 	if (IN_FASTRECOVERY(rack->rc_tp->t_flags))
1977 		return (rack->r_ctl.rc_fixed_pacing_rate_rec);
1978 	else if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh)
1979 		return (rack->r_ctl.rc_fixed_pacing_rate_ss);
1980 	else
1981 		return (rack->r_ctl.rc_fixed_pacing_rate_ca);
1982 }
1983 
1984 static void
1985 rack_log_hybrid_bw(struct tcp_rack *rack, uint32_t seq, uint64_t cbw, uint64_t tim,
1986 	uint64_t data, uint8_t mod, uint16_t aux,
1987 	struct tcp_sendfile_track *cur, int line)
1988 {
1989 #ifdef TCP_REQUEST_TRK
1990 	int do_log = 0;
1991 
1992 	/*
1993 	 * The rate cap one is noisy and only should come out when normal BB logging
1994 	 * is enabled, the other logs (not RATE_CAP and NOT CAP_CALC) only come out
1995 	 * once per chunk and make up the BBpoint that can be turned on by the client.
1996 	 */
1997 	if ((mod == HYBRID_LOG_RATE_CAP) || (mod == HYBRID_LOG_CAP_CALC)) {
1998 		/*
1999 		 * The very noisy two need to only come out when
2000 		 * we have verbose logging on.
2001 		 */
2002 		if (rack_verbose_logging != 0)
2003 			do_log = tcp_bblogging_on(rack->rc_tp);
2004 		else
2005 			do_log = 0;
2006 	} else if (mod != HYBRID_LOG_BW_MEASURE) {
2007 		/*
2008 		 * All other less noisy logs here except the measure which
2009 		 * also needs to come out on the point and the log.
2010 		 */
2011 		do_log = tcp_bblogging_on(rack->rc_tp);
2012 	} else {
2013 		do_log = tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING);
2014 	}
2015 
2016 	if (do_log) {
2017 		union tcp_log_stackspecific log;
2018 		struct timeval tv;
2019 		uint64_t lt_bw;
2020 
2021 		/* Convert our ms to a microsecond */
2022 		memset(&log, 0, sizeof(log));
2023 
2024 		log.u_bbr.cwnd_gain = line;
2025 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2026 		log.u_bbr.rttProp = tim;
2027 		log.u_bbr.bw_inuse = cbw;
2028 		log.u_bbr.delRate = rack_get_gp_est(rack);
2029 		lt_bw = rack_get_lt_bw(rack);
2030 		log.u_bbr.flex1 = seq;
2031 		log.u_bbr.pacing_gain = aux;
2032 		/* lt_bw = < flex3 | flex2 > */
2033 		log.u_bbr.flex2 = (uint32_t)(lt_bw & 0x00000000ffffffff);
2034 		log.u_bbr.flex3 = (uint32_t)((lt_bw >> 32) & 0x00000000ffffffff);
2035 		/* Record the last obtained us rtt in inflight */
2036 		if (cur == NULL) {
2037 			/* Make sure we are looking at the right log if an overide comes in */
2038 			cur = rack->r_ctl.rc_last_sft;
2039 		}
2040 		if (rack->r_ctl.rack_rs.rs_flags != RACK_RTT_EMPTY)
2041 			log.u_bbr.inflight = rack->r_ctl.rack_rs.rs_us_rtt;
2042 		else {
2043 			/* Use the last known rtt i.e. the rack-rtt */
2044 			log.u_bbr.inflight = rack->rc_rack_rtt;
2045 		}
2046 		if (cur != NULL) {
2047 			uint64_t off;
2048 
2049 			log.u_bbr.cur_del_rate = cur->deadline;
2050 			if ((mod == HYBRID_LOG_RATE_CAP) || (mod == HYBRID_LOG_CAP_CALC)) {
2051 				/* start = < lost | pkt_epoch > */
2052 				log.u_bbr.pkt_epoch = (uint32_t)(cur->start & 0x00000000ffffffff);
2053 				log.u_bbr.lost = (uint32_t)((cur->start >> 32) & 0x00000000ffffffff);
2054 				log.u_bbr.flex6 = cur->start_seq;
2055 				log.u_bbr.pkts_out = cur->end_seq;
2056 			} else {
2057 				/* start = < lost | pkt_epoch > */
2058 				log.u_bbr.pkt_epoch = (uint32_t)(cur->start & 0x00000000ffffffff);
2059 				log.u_bbr.lost = (uint32_t)((cur->start >> 32) & 0x00000000ffffffff);
2060 				/* end = < pkts_out | flex6 > */
2061 				log.u_bbr.flex6 = (uint32_t)(cur->end & 0x00000000ffffffff);
2062 				log.u_bbr.pkts_out = (uint32_t)((cur->end >> 32) & 0x00000000ffffffff);
2063 			}
2064 			/* first_send = <lt_epoch | epoch> */
2065 			log.u_bbr.epoch = (uint32_t)(cur->first_send & 0x00000000ffffffff);
2066 			log.u_bbr.lt_epoch = (uint32_t)((cur->first_send >> 32) & 0x00000000ffffffff);
2067 			/* localtime = <delivered | applimited>*/
2068 			log.u_bbr.applimited = (uint32_t)(cur->localtime & 0x00000000ffffffff);
2069 			log.u_bbr.delivered = (uint32_t)((cur->localtime >> 32) & 0x00000000ffffffff);
2070 #ifdef TCP_REQUEST_TRK
2071 			off = (uint64_t)(cur) - (uint64_t)(&rack->rc_tp->t_tcpreq_info[0]);
2072 			log.u_bbr.bbr_substate = (uint8_t)(off / sizeof(struct tcp_sendfile_track));
2073 #endif
2074 			log.u_bbr.flex4 = (uint32_t)(rack->rc_tp->t_sndbytes - cur->sent_at_fs);
2075 			log.u_bbr.flex5 = (uint32_t)(rack->rc_tp->t_snd_rxt_bytes - cur->rxt_at_fs);
2076 			log.u_bbr.flex7 = (uint16_t)cur->hybrid_flags;
2077 		} else {
2078 			log.u_bbr.flex7 = 0xffff;
2079 			log.u_bbr.cur_del_rate = 0xffffffffffffffff;
2080 		}
2081 		/*
2082 		 * Compose bbr_state to be a bit wise 0000ADHF
2083 		 * where A is the always_pace flag
2084 		 * where D is the dgp_on flag
2085 		 * where H is the hybrid_mode on flag
2086 		 * where F is the use_fixed_rate flag.
2087 		 */
2088 		log.u_bbr.bbr_state = rack->rc_always_pace;
2089 		log.u_bbr.bbr_state <<= 1;
2090 		log.u_bbr.bbr_state |= rack->dgp_on;
2091 		log.u_bbr.bbr_state <<= 1;
2092 		log.u_bbr.bbr_state |= rack->rc_hybrid_mode;
2093 		log.u_bbr.bbr_state <<= 1;
2094 		log.u_bbr.bbr_state |= rack->use_fixed_rate;
2095 		log.u_bbr.flex8 = mod;
2096 		tcp_log_event(rack->rc_tp, NULL,
2097 		    &rack->rc_inp->inp_socket->so_rcv,
2098 		    &rack->rc_inp->inp_socket->so_snd,
2099 		    TCP_HYBRID_PACING_LOG, 0,
2100 		    0, &log, false, NULL, __func__, __LINE__, &tv);
2101 
2102 	}
2103 #endif
2104 }
2105 
2106 #ifdef TCP_REQUEST_TRK
2107 static void
2108 rack_log_hybrid_sends(struct tcp_rack *rack, struct tcp_sendfile_track *cur, int line)
2109 {
2110 	if (tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING)) {
2111 		union tcp_log_stackspecific log;
2112 		struct timeval tv;
2113 		uint64_t off;
2114 
2115 		/* Convert our ms to a microsecond */
2116 		memset(&log, 0, sizeof(log));
2117 
2118 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2119 		log.u_bbr.cur_del_rate = rack->rc_tp->t_sndbytes;
2120 		log.u_bbr.delRate = cur->sent_at_fs;
2121 		log.u_bbr.rttProp = rack->rc_tp->t_snd_rxt_bytes;
2122 		log.u_bbr.bw_inuse = cur->rxt_at_fs;
2123 		log.u_bbr.cwnd_gain = line;
2124 		off = (uint64_t)(cur) - (uint64_t)(&rack->rc_tp->t_tcpreq_info[0]);
2125 		log.u_bbr.bbr_substate = (uint8_t)(off / sizeof(struct tcp_sendfile_track));
2126 		/* start = < flex1 | flex2 > */
2127 		log.u_bbr.flex2 = (uint32_t)(cur->start & 0x00000000ffffffff);
2128 		log.u_bbr.flex1 = (uint32_t)((cur->start >> 32) & 0x00000000ffffffff);
2129 		/* end = < flex3 | flex4 > */
2130 		log.u_bbr.flex4 = (uint32_t)(cur->end & 0x00000000ffffffff);
2131 		log.u_bbr.flex3 = (uint32_t)((cur->end >> 32) & 0x00000000ffffffff);
2132 
2133 		/* localtime = <delivered | applimited>*/
2134 		log.u_bbr.applimited = (uint32_t)(cur->localtime & 0x00000000ffffffff);
2135 		log.u_bbr.delivered = (uint32_t)((cur->localtime >> 32) & 0x00000000ffffffff);
2136 		/* client timestamp = <lt_epoch | epoch>*/
2137 		log.u_bbr.epoch = (uint32_t)(cur->timestamp & 0x00000000ffffffff);
2138 		log.u_bbr.lt_epoch = (uint32_t)((cur->timestamp >> 32) & 0x00000000ffffffff);
2139 		/* now set all the flags in */
2140 		log.u_bbr.pkts_out = cur->hybrid_flags;
2141 		log.u_bbr.flex6 = cur->flags;
2142 		/*
2143 		 * Last send time  = <flex5 | pkt_epoch>  note we do not distinguish cases
2144 		 * where a false retransmit occurred so first_send  <-> lastsend may
2145 		 * include longer time then it actually took if we have a false rxt.
2146 		 */
2147 		log.u_bbr.pkt_epoch = (uint32_t)(rack->r_ctl.last_tmit_time_acked & 0x00000000ffffffff);
2148 		log.u_bbr.flex5 = (uint32_t)((rack->r_ctl.last_tmit_time_acked >> 32) & 0x00000000ffffffff);
2149 
2150 		log.u_bbr.flex8 = HYBRID_LOG_SENT_LOST;
2151 		tcp_log_event(rack->rc_tp, NULL,
2152 		    &rack->rc_inp->inp_socket->so_rcv,
2153 		    &rack->rc_inp->inp_socket->so_snd,
2154 		    TCP_HYBRID_PACING_LOG, 0,
2155 		    0, &log, false, NULL, __func__, __LINE__, &tv);
2156 	}
2157 }
2158 #endif
2159 
2160 static inline uint64_t
2161 rack_compensate_for_linerate(struct tcp_rack *rack, uint64_t bw)
2162 {
2163 	uint64_t ret_bw, ether;
2164 	uint64_t u_segsiz;
2165 
2166 	ether = rack->rc_tp->t_maxseg + sizeof(struct tcphdr);
2167 	if (rack->r_is_v6){
2168 #ifdef INET6
2169 		ether += sizeof(struct ip6_hdr);
2170 #endif
2171 		ether += 14;	/* eheader size 6+6+2 */
2172 	} else {
2173 #ifdef INET
2174 		ether += sizeof(struct ip);
2175 #endif
2176 		ether += 14;	/* eheader size 6+6+2 */
2177 	}
2178 	u_segsiz = (uint64_t)min(ctf_fixed_maxseg(rack->rc_tp), rack->r_ctl.rc_pace_min_segs);
2179 	ret_bw = bw;
2180 	ret_bw *= ether;
2181 	ret_bw /= u_segsiz;
2182 	return (ret_bw);
2183 }
2184 
2185 static void
2186 rack_rate_cap_bw(struct tcp_rack *rack, uint64_t *bw, int *capped)
2187 {
2188 #ifdef TCP_REQUEST_TRK
2189 	struct timeval tv;
2190 	uint64_t timenow, timeleft, lenleft, lengone, calcbw;
2191 #endif
2192 
2193 	if (rack->r_ctl.bw_rate_cap == 0)
2194 		return;
2195 #ifdef TCP_REQUEST_TRK
2196 	if (rack->rc_catch_up && rack->rc_hybrid_mode &&
2197 	    (rack->r_ctl.rc_last_sft != NULL)) {
2198 		/*
2199 		 * We have a dynamic cap. The original target
2200 		 * is in bw_rate_cap, but we need to look at
2201 		 * how long it is until we hit the deadline.
2202 		 */
2203 		struct tcp_sendfile_track *ent;
2204 
2205       		ent = rack->r_ctl.rc_last_sft;
2206 		microuptime(&tv);
2207 		timenow = tcp_tv_to_lusectick(&tv);
2208 		if (timenow >= ent->deadline) {
2209 			/* No time left we do DGP only */
2210 			rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2211 					   0, 0, 0, HYBRID_LOG_OUTOFTIME, 0, ent, __LINE__);
2212 			rack->r_ctl.bw_rate_cap = 0;
2213 			return;
2214 		}
2215 		/* We have the time */
2216 		timeleft = rack->r_ctl.rc_last_sft->deadline - timenow;
2217 		if (timeleft < HPTS_MSEC_IN_SEC) {
2218 			/* If there is less than a ms left just use DGPs rate */
2219 			rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2220 					   0, timeleft, 0, HYBRID_LOG_OUTOFTIME, 0, ent, __LINE__);
2221 			rack->r_ctl.bw_rate_cap = 0;
2222 			return;
2223 		}
2224 		/*
2225 		 * Now lets find the amount of data left to send.
2226 		 *
2227 		 * Now ideally we want to use the end_seq to figure out how much more
2228 		 * but it might not be possible (only if we have the TRACK_FG_COMP on the entry..
2229 		 */
2230 		if (ent->flags & TCP_TRK_TRACK_FLG_COMP) {
2231 			if (SEQ_GT(ent->end_seq, rack->rc_tp->snd_una))
2232 				lenleft = ent->end_seq - rack->rc_tp->snd_una;
2233 			else {
2234 				/* TSNH, we should catch it at the send */
2235 				rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2236 						   0, timeleft, 0, HYBRID_LOG_CAPERROR, 0, ent, __LINE__);
2237 				rack->r_ctl.bw_rate_cap = 0;
2238 				return;
2239 			}
2240 		} else {
2241 			/*
2242 			 * The hard way, figure out how much is gone and then
2243 			 * take that away from the total the client asked for
2244 			 * (thats off by tls overhead if this is tls).
2245 			 */
2246 			if (SEQ_GT(rack->rc_tp->snd_una, ent->start_seq))
2247 				lengone = rack->rc_tp->snd_una - ent->start_seq;
2248 			else
2249 				lengone = 0;
2250 			if (lengone < (ent->end - ent->start))
2251 				lenleft = (ent->end - ent->start) - lengone;
2252 			else {
2253 				/* TSNH, we should catch it at the send */
2254 				rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2255 						   0, timeleft, lengone, HYBRID_LOG_CAPERROR, 0, ent, __LINE__);
2256 				rack->r_ctl.bw_rate_cap = 0;
2257 				return;
2258 			}
2259 		}
2260 		if (lenleft == 0) {
2261 			/* We have it all sent */
2262 			rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2263 					   0, timeleft, lenleft, HYBRID_LOG_ALLSENT, 0, ent, __LINE__);
2264 			if (rack->r_ctl.bw_rate_cap)
2265 				goto normal_ratecap;
2266 			else
2267 				return;
2268 		}
2269 		calcbw = lenleft * HPTS_USEC_IN_SEC;
2270 		calcbw /= timeleft;
2271 		/* Now we must compensate for IP/TCP overhead */
2272 		calcbw = rack_compensate_for_linerate(rack, calcbw);
2273 		/* Update the bit rate cap */
2274 		rack->r_ctl.bw_rate_cap = calcbw;
2275 		if ((rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_S_MSS) &&
2276 		    (rack_hybrid_allow_set_maxseg == 1) &&
2277 		    ((rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_SETMSS) == 0)) {
2278 			/* Lets set in a smaller mss possibly here to match our rate-cap */
2279 			uint32_t orig_max;
2280 
2281 			orig_max = rack->r_ctl.rc_pace_max_segs;
2282 			rack->r_ctl.rc_last_sft->hybrid_flags |= TCP_HYBRID_PACING_SETMSS;
2283 			rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack, calcbw, ctf_fixed_maxseg(rack->rc_tp));
2284 			rack_log_type_pacing_sizes(rack->rc_tp, rack, rack->r_ctl.client_suggested_maxseg, orig_max, __LINE__, 5);
2285 		}
2286 		rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2287 				   calcbw, timeleft, lenleft, HYBRID_LOG_CAP_CALC, 0, ent, __LINE__);
2288 		if ((calcbw > 0) && (*bw > calcbw)) {
2289 			rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2290 					   *bw, ent->deadline, lenleft, HYBRID_LOG_RATE_CAP, 0, ent, __LINE__);
2291 			*capped = 1;
2292 			*bw = calcbw;
2293 		}
2294 		return;
2295 	}
2296 normal_ratecap:
2297 #endif
2298 	if ((rack->r_ctl.bw_rate_cap > 0) && (*bw > rack->r_ctl.bw_rate_cap)) {
2299 #ifdef TCP_REQUEST_TRK
2300 		if (rack->rc_hybrid_mode &&
2301 		    rack->rc_catch_up &&
2302 		    (rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_S_MSS) &&
2303 		    (rack_hybrid_allow_set_maxseg == 1) &&
2304 		    ((rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_SETMSS) == 0)) {
2305 			/* Lets set in a smaller mss possibly here to match our rate-cap */
2306 			uint32_t orig_max;
2307 
2308 			orig_max = rack->r_ctl.rc_pace_max_segs;
2309 			rack->r_ctl.rc_last_sft->hybrid_flags |= TCP_HYBRID_PACING_SETMSS;
2310 			rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack, rack->r_ctl.bw_rate_cap, ctf_fixed_maxseg(rack->rc_tp));
2311 			rack_log_type_pacing_sizes(rack->rc_tp, rack, rack->r_ctl.client_suggested_maxseg, orig_max, __LINE__, 5);
2312 		}
2313 #endif
2314 		*capped = 1;
2315 		*bw = rack->r_ctl.bw_rate_cap;
2316 		rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2317 				   *bw, 0, 0,
2318 				   HYBRID_LOG_RATE_CAP, 1, NULL, __LINE__);
2319 	}
2320 }
2321 
2322 static uint64_t
2323 rack_get_gp_est(struct tcp_rack *rack)
2324 {
2325 	uint64_t bw, lt_bw, ret_bw;
2326 
2327 	if (rack->rc_gp_filled == 0) {
2328 		/*
2329 		 * We have yet no b/w measurement,
2330 		 * if we have a user set initial bw
2331 		 * return it. If we don't have that and
2332 		 * we have an srtt, use the tcp IW (10) to
2333 		 * calculate a fictional b/w over the SRTT
2334 		 * which is more or less a guess. Note
2335 		 * we don't use our IW from rack on purpose
2336 		 * so if we have like IW=30, we are not
2337 		 * calculating a "huge" b/w.
2338 		 */
2339 		uint64_t srtt;
2340 
2341 		lt_bw = rack_get_lt_bw(rack);
2342 		if (lt_bw) {
2343 			/*
2344 			 * No goodput bw but a long-term b/w does exist
2345 			 * lets use that.
2346 			 */
2347 			ret_bw = lt_bw;
2348 			goto compensate;
2349 		}
2350 		if (rack->r_ctl.init_rate)
2351 			return (rack->r_ctl.init_rate);
2352 
2353 		/* Ok lets come up with the IW guess, if we have a srtt */
2354 		if (rack->rc_tp->t_srtt == 0) {
2355 			/*
2356 			 * Go with old pacing method
2357 			 * i.e. burst mitigation only.
2358 			 */
2359 			return (0);
2360 		}
2361 		/* Ok lets get the initial TCP win (not racks) */
2362 		bw = tcp_compute_initwnd(tcp_maxseg(rack->rc_tp));
2363 		srtt = (uint64_t)rack->rc_tp->t_srtt;
2364 		bw *= (uint64_t)USECS_IN_SECOND;
2365 		bw /= srtt;
2366 		ret_bw = bw;
2367 		goto compensate;
2368 
2369 	}
2370 	if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) {
2371 		/* Averaging is done, we can return the value */
2372 		bw = rack->r_ctl.gp_bw;
2373 	} else {
2374 		/* Still doing initial average must calculate */
2375 		bw = rack->r_ctl.gp_bw / max(rack->r_ctl.num_measurements, 1);
2376 	}
2377 	lt_bw = rack_get_lt_bw(rack);
2378 	if (lt_bw == 0) {
2379 		/* If we don't have one then equate it to the gp_bw */
2380 		lt_bw = rack->r_ctl.gp_bw;
2381 	}
2382 	if ((rack->r_cwnd_was_clamped == 1) && (rack->r_clamped_gets_lower > 0)){
2383 		/*  if clamped take the lowest */
2384 		if (lt_bw < bw)
2385 			ret_bw = lt_bw;
2386 		else
2387 			ret_bw = bw;
2388 	} else {
2389 		/* If not set for clamped to get lowest, take the highest */
2390 		if (lt_bw > bw)
2391 			ret_bw = lt_bw;
2392 		else
2393 			ret_bw = bw;
2394 	}
2395 	/*
2396 	 * Now lets compensate based on the TCP/IP overhead. Our
2397 	 * Goodput estimate does not include this so we must pace out
2398 	 * a bit faster since our pacing calculations do. The pacing
2399 	 * calculations use the base ETHERNET_SEGMENT_SIZE and the segsiz
2400 	 * we are using to do this, so we do that here in the opposite
2401 	 * direction as well. This means that if we are tunneled and the
2402 	 * segsiz is say 1200 bytes we will get quite a boost, but its
2403 	 * compensated for in the pacing time the opposite way.
2404 	 */
2405 compensate:
2406 	ret_bw = rack_compensate_for_linerate(rack, ret_bw);
2407 	return(ret_bw);
2408 }
2409 
2410 
2411 static uint64_t
2412 rack_get_bw(struct tcp_rack *rack)
2413 {
2414 	uint64_t bw;
2415 
2416 	if (rack->use_fixed_rate) {
2417 		/* Return the fixed pacing rate */
2418 		return (rack_get_fixed_pacing_bw(rack));
2419 	}
2420 	bw = rack_get_gp_est(rack);
2421 	return (bw);
2422 }
2423 
2424 static uint16_t
2425 rack_get_output_gain(struct tcp_rack *rack, struct rack_sendmap *rsm)
2426 {
2427 	if (rack->use_fixed_rate) {
2428 		return (100);
2429 	} else if (rack->in_probe_rtt && (rsm == NULL))
2430 		return (rack->r_ctl.rack_per_of_gp_probertt);
2431 	else if ((IN_FASTRECOVERY(rack->rc_tp->t_flags) &&
2432 		  rack->r_ctl.rack_per_of_gp_rec)) {
2433 		if (rsm) {
2434 			/* a retransmission always use the recovery rate */
2435 			return (rack->r_ctl.rack_per_of_gp_rec);
2436 		} else if (rack->rack_rec_nonrxt_use_cr) {
2437 			/* Directed to use the configured rate */
2438 			goto configured_rate;
2439 		} else if (rack->rack_no_prr &&
2440 			   (rack->r_ctl.rack_per_of_gp_rec > 100)) {
2441 			/* No PRR, lets just use the b/w estimate only */
2442 			return (100);
2443 		} else {
2444 			/*
2445 			 * Here we may have a non-retransmit but we
2446 			 * have no overrides, so just use the recovery
2447 			 * rate (prr is in effect).
2448 			 */
2449 			return (rack->r_ctl.rack_per_of_gp_rec);
2450 		}
2451 	}
2452 configured_rate:
2453 	/* For the configured rate we look at our cwnd vs the ssthresh */
2454 	if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh)
2455 		return (rack->r_ctl.rack_per_of_gp_ss);
2456 	else
2457 		return (rack->r_ctl.rack_per_of_gp_ca);
2458 }
2459 
2460 static void
2461 rack_log_dsack_event(struct tcp_rack *rack, uint8_t mod, uint32_t flex4, uint32_t flex5, uint32_t flex6)
2462 {
2463 	/*
2464 	 * Types of logs (mod value)
2465 	 * 1 = dsack_persists reduced by 1 via T-O or fast recovery exit.
2466 	 * 2 = a dsack round begins, persist is reset to 16.
2467 	 * 3 = a dsack round ends
2468 	 * 4 = Dsack option increases rack rtt flex5 is the srtt input, flex6 is thresh
2469 	 * 5 = Socket option set changing the control flags rc_rack_tmr_std_based, rc_rack_use_dsack
2470 	 * 6 = Final rack rtt, flex4 is srtt and flex6 is final limited thresh.
2471 	 */
2472 	if (tcp_bblogging_on(rack->rc_tp)) {
2473 		union tcp_log_stackspecific log;
2474 		struct timeval tv;
2475 
2476 		memset(&log, 0, sizeof(log));
2477 		log.u_bbr.flex1 = rack->rc_rack_tmr_std_based;
2478 		log.u_bbr.flex1 <<= 1;
2479 		log.u_bbr.flex1 |= rack->rc_rack_use_dsack;
2480 		log.u_bbr.flex1 <<= 1;
2481 		log.u_bbr.flex1 |= rack->rc_dsack_round_seen;
2482 		log.u_bbr.flex2 = rack->r_ctl.dsack_round_end;
2483 		log.u_bbr.flex3 = rack->r_ctl.num_dsack;
2484 		log.u_bbr.flex4 = flex4;
2485 		log.u_bbr.flex5 = flex5;
2486 		log.u_bbr.flex6 = flex6;
2487 		log.u_bbr.flex7 = rack->r_ctl.dsack_persist;
2488 		log.u_bbr.flex8 = mod;
2489 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2490 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2491 		    &rack->rc_inp->inp_socket->so_rcv,
2492 		    &rack->rc_inp->inp_socket->so_snd,
2493 		    RACK_DSACK_HANDLING, 0,
2494 		    0, &log, false, &tv);
2495 	}
2496 }
2497 
2498 static void
2499 rack_log_hdwr_pacing(struct tcp_rack *rack,
2500 		     uint64_t rate, uint64_t hw_rate, int line,
2501 		     int error, uint16_t mod)
2502 {
2503 	if (tcp_bblogging_on(rack->rc_tp)) {
2504 		union tcp_log_stackspecific log;
2505 		struct timeval tv;
2506 		const struct ifnet *ifp;
2507 
2508 		memset(&log, 0, sizeof(log));
2509 		log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff);
2510 		log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff);
2511 		if (rack->r_ctl.crte) {
2512 			ifp = rack->r_ctl.crte->ptbl->rs_ifp;
2513 		} else if (rack->rc_inp->inp_route.ro_nh &&
2514 			   rack->rc_inp->inp_route.ro_nh->nh_ifp) {
2515 			ifp = rack->rc_inp->inp_route.ro_nh->nh_ifp;
2516 		} else
2517 			ifp = NULL;
2518 		if (ifp) {
2519 			log.u_bbr.flex3 = (((uint64_t)ifp  >> 32) & 0x00000000ffffffff);
2520 			log.u_bbr.flex4 = ((uint64_t)ifp & 0x00000000ffffffff);
2521 		}
2522 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2523 		log.u_bbr.bw_inuse = rate;
2524 		log.u_bbr.flex5 = line;
2525 		log.u_bbr.flex6 = error;
2526 		log.u_bbr.flex7 = mod;
2527 		log.u_bbr.applimited = rack->r_ctl.rc_pace_max_segs;
2528 		log.u_bbr.flex8 = rack->use_fixed_rate;
2529 		log.u_bbr.flex8 <<= 1;
2530 		log.u_bbr.flex8 |= rack->rack_hdrw_pacing;
2531 		log.u_bbr.pkts_out = rack->rc_tp->t_maxseg;
2532 		log.u_bbr.delRate = rack->r_ctl.crte_prev_rate;
2533 		if (rack->r_ctl.crte)
2534 			log.u_bbr.cur_del_rate = rack->r_ctl.crte->rate;
2535 		else
2536 			log.u_bbr.cur_del_rate = 0;
2537 		log.u_bbr.rttProp = rack->r_ctl.last_hw_bw_req;
2538 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2539 		    &rack->rc_inp->inp_socket->so_rcv,
2540 		    &rack->rc_inp->inp_socket->so_snd,
2541 		    BBR_LOG_HDWR_PACE, 0,
2542 		    0, &log, false, &tv);
2543 	}
2544 }
2545 
2546 static uint64_t
2547 rack_get_output_bw(struct tcp_rack *rack, uint64_t bw, struct rack_sendmap *rsm, int *capped)
2548 {
2549 	/*
2550 	 * We allow rack_per_of_gp_xx to dictate our bw rate we want.
2551 	 */
2552 	uint64_t bw_est, high_rate;
2553 	uint64_t gain;
2554 
2555 	if ((rack->r_pacing_discount == 0) ||
2556 	    (rack_full_buffer_discount == 0)) {
2557 		/*
2558 		 * No buffer level based discount from client buffer
2559 		 * level is enabled or the feature is disabled.
2560 		 */
2561 		gain = (uint64_t)rack_get_output_gain(rack, rsm);
2562 		bw_est = bw * gain;
2563 		bw_est /= (uint64_t)100;
2564 	} else {
2565 		/*
2566 		 * We have a discount in place apply it with
2567 		 * just a 100% gain (we get no boost if the buffer
2568 		 * is full).
2569 		 */
2570 		uint64_t discount;
2571 
2572 		discount = bw * (uint64_t)(rack_full_buffer_discount * rack->r_ctl.pacing_discount_amm);
2573 		discount /= 100;
2574 		/* What %% of the b/w do we discount */
2575 		bw_est = bw - discount;
2576 	}
2577 	/* Never fall below the minimum (def 64kbps) */
2578 	if (bw_est < RACK_MIN_BW)
2579 		bw_est = RACK_MIN_BW;
2580 	if (rack->r_rack_hw_rate_caps) {
2581 		/* Rate caps are in place */
2582 		if (rack->r_ctl.crte != NULL) {
2583 			/* We have a hdwr rate already */
2584 			high_rate = tcp_hw_highest_rate(rack->r_ctl.crte);
2585 			if (bw_est >= high_rate) {
2586 				/* We are capping bw at the highest rate table entry */
2587 				if (rack_hw_rate_cap_per &&
2588 				    (((high_rate * (100 + rack_hw_rate_cap_per)) / 100) < bw_est)) {
2589 					rack->r_rack_hw_rate_caps = 0;
2590 					goto done;
2591 				}
2592 				rack_log_hdwr_pacing(rack,
2593 						     bw_est, high_rate, __LINE__,
2594 						     0, 3);
2595 				bw_est = high_rate;
2596 				if (capped)
2597 					*capped = 1;
2598 			}
2599 		} else if ((rack->rack_hdrw_pacing == 0) &&
2600 			   (rack->rack_hdw_pace_ena) &&
2601 			   (rack->rack_attempt_hdwr_pace == 0) &&
2602 			   (rack->rc_inp->inp_route.ro_nh != NULL) &&
2603 			   (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
2604 			/*
2605 			 * Special case, we have not yet attempted hardware
2606 			 * pacing, and yet we may, when we do, find out if we are
2607 			 * above the highest rate. We need to know the maxbw for the interface
2608 			 * in question (if it supports ratelimiting). We get back
2609 			 * a 0, if the interface is not found in the RL lists.
2610 			 */
2611 			high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp);
2612 			if (high_rate) {
2613 				/* Yep, we have a rate is it above this rate? */
2614 				if (bw_est > high_rate) {
2615 					bw_est = high_rate;
2616 					if (capped)
2617 						*capped = 1;
2618 				}
2619 			}
2620 		}
2621 	}
2622 done:
2623 	return (bw_est);
2624 }
2625 
2626 static void
2627 rack_log_retran_reason(struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t tsused, uint32_t thresh, int mod)
2628 {
2629 	if (tcp_bblogging_on(rack->rc_tp)) {
2630 		union tcp_log_stackspecific log;
2631 		struct timeval tv;
2632 
2633 		if (rack->sack_attack_disable > 0)
2634 			goto log_anyway;
2635 		if ((mod != 1) && (rack_verbose_logging == 0))  {
2636 			/*
2637 			 * We get 3 values currently for mod
2638 			 * 1 - We are retransmitting and this tells the reason.
2639 			 * 2 - We are clearing a dup-ack count.
2640 			 * 3 - We are incrementing a dup-ack count.
2641 			 *
2642 			 * The clear/increment are only logged
2643 			 * if you have BBverbose on.
2644 			 */
2645 			return;
2646 		}
2647 log_anyway:
2648 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2649 		log.u_bbr.flex1 = tsused;
2650 		log.u_bbr.flex2 = thresh;
2651 		log.u_bbr.flex3 = rsm->r_flags;
2652 		log.u_bbr.flex4 = rsm->r_dupack;
2653 		log.u_bbr.flex5 = rsm->r_start;
2654 		log.u_bbr.flex6 = rsm->r_end;
2655 		log.u_bbr.flex8 = mod;
2656 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2657 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2658 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2659 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2660 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2661 		log.u_bbr.pacing_gain = rack->r_must_retran;
2662 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2663 		    &rack->rc_inp->inp_socket->so_rcv,
2664 		    &rack->rc_inp->inp_socket->so_snd,
2665 		    BBR_LOG_SETTINGS_CHG, 0,
2666 		    0, &log, false, &tv);
2667 	}
2668 }
2669 
2670 static void
2671 rack_log_to_start(struct tcp_rack *rack, uint32_t cts, uint32_t to, int32_t slot, uint8_t which)
2672 {
2673 	if (tcp_bblogging_on(rack->rc_tp)) {
2674 		union tcp_log_stackspecific log;
2675 		struct timeval tv;
2676 
2677 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2678 		log.u_bbr.flex1 = rack->rc_tp->t_srtt;
2679 		log.u_bbr.flex2 = to;
2680 		log.u_bbr.flex3 = rack->r_ctl.rc_hpts_flags;
2681 		log.u_bbr.flex4 = slot;
2682 		log.u_bbr.flex5 = rack->rc_tp->t_hpts_slot;
2683 		log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
2684 		log.u_bbr.flex7 = rack->rc_in_persist;
2685 		log.u_bbr.flex8 = which;
2686 		if (rack->rack_no_prr)
2687 			log.u_bbr.pkts_out = 0;
2688 		else
2689 			log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
2690 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2691 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2692 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2693 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2694 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2695 		log.u_bbr.pacing_gain = rack->r_must_retran;
2696 		log.u_bbr.cwnd_gain = rack->rack_deferred_inited;
2697 		log.u_bbr.pkt_epoch = rack->rc_has_collapsed;
2698 		log.u_bbr.lt_epoch = rack->rc_tp->t_rxtshift;
2699 		log.u_bbr.lost = rack_rto_min;
2700 		log.u_bbr.epoch = rack->r_ctl.roundends;
2701 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2702 		    &rack->rc_inp->inp_socket->so_rcv,
2703 		    &rack->rc_inp->inp_socket->so_snd,
2704 		    BBR_LOG_TIMERSTAR, 0,
2705 		    0, &log, false, &tv);
2706 	}
2707 }
2708 
2709 static void
2710 rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm)
2711 {
2712 	if (tcp_bblogging_on(rack->rc_tp)) {
2713 		union tcp_log_stackspecific log;
2714 		struct timeval tv;
2715 
2716 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2717 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2718 		log.u_bbr.flex8 = to_num;
2719 		log.u_bbr.flex1 = rack->r_ctl.rc_rack_min_rtt;
2720 		log.u_bbr.flex2 = rack->rc_rack_rtt;
2721 		if (rsm == NULL)
2722 			log.u_bbr.flex3 = 0;
2723 		else
2724 			log.u_bbr.flex3 = rsm->r_end - rsm->r_start;
2725 		if (rack->rack_no_prr)
2726 			log.u_bbr.flex5 = 0;
2727 		else
2728 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
2729 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2730 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2731 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2732 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2733 		log.u_bbr.pacing_gain = rack->r_must_retran;
2734 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2735 		    &rack->rc_inp->inp_socket->so_rcv,
2736 		    &rack->rc_inp->inp_socket->so_snd,
2737 		    BBR_LOG_RTO, 0,
2738 		    0, &log, false, &tv);
2739 	}
2740 }
2741 
2742 static void
2743 rack_log_map_chg(struct tcpcb *tp, struct tcp_rack *rack,
2744 		 struct rack_sendmap *prev,
2745 		 struct rack_sendmap *rsm,
2746 		 struct rack_sendmap *next,
2747 		 int flag, uint32_t th_ack, int line)
2748 {
2749 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
2750 		union tcp_log_stackspecific log;
2751 		struct timeval tv;
2752 
2753 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2754 		log.u_bbr.flex8 = flag;
2755 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2756 		log.u_bbr.cur_del_rate = (uint64_t)prev;
2757 		log.u_bbr.delRate = (uint64_t)rsm;
2758 		log.u_bbr.rttProp = (uint64_t)next;
2759 		log.u_bbr.flex7 = 0;
2760 		if (prev) {
2761 			log.u_bbr.flex1 = prev->r_start;
2762 			log.u_bbr.flex2 = prev->r_end;
2763 			log.u_bbr.flex7 |= 0x4;
2764 		}
2765 		if (rsm) {
2766 			log.u_bbr.flex3 = rsm->r_start;
2767 			log.u_bbr.flex4 = rsm->r_end;
2768 			log.u_bbr.flex7 |= 0x2;
2769 		}
2770 		if (next) {
2771 			log.u_bbr.flex5 = next->r_start;
2772 			log.u_bbr.flex6 = next->r_end;
2773 			log.u_bbr.flex7 |= 0x1;
2774 		}
2775 		log.u_bbr.applimited = line;
2776 		log.u_bbr.pkts_out = th_ack;
2777 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2778 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2779 		if (rack->rack_no_prr)
2780 			log.u_bbr.lost = 0;
2781 		else
2782 			log.u_bbr.lost = rack->r_ctl.rc_prr_sndcnt;
2783 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2784 		    &rack->rc_inp->inp_socket->so_rcv,
2785 		    &rack->rc_inp->inp_socket->so_snd,
2786 		    TCP_LOG_MAPCHG, 0,
2787 		    0, &log, false, &tv);
2788 	}
2789 }
2790 
2791 static void
2792 rack_log_rtt_upd(struct tcpcb *tp, struct tcp_rack *rack, uint32_t t, uint32_t len,
2793 		 struct rack_sendmap *rsm, int conf)
2794 {
2795 	if (tcp_bblogging_on(tp)) {
2796 		union tcp_log_stackspecific log;
2797 		struct timeval tv;
2798 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2799 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2800 		log.u_bbr.flex1 = t;
2801 		log.u_bbr.flex2 = len;
2802 		log.u_bbr.flex3 = rack->r_ctl.rc_rack_min_rtt;
2803 		log.u_bbr.flex4 = rack->r_ctl.rack_rs.rs_rtt_lowest;
2804 		log.u_bbr.flex5 = rack->r_ctl.rack_rs.rs_rtt_highest;
2805 		log.u_bbr.flex6 = rack->r_ctl.rack_rs.rs_us_rtrcnt;
2806 		log.u_bbr.flex7 = conf;
2807 		log.u_bbr.rttProp = (uint64_t)rack->r_ctl.rack_rs.rs_rtt_tot;
2808 		log.u_bbr.flex8 = rack->r_ctl.rc_rate_sample_method;
2809 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2810 		log.u_bbr.delivered = rack->r_ctl.rack_rs.rs_us_rtrcnt;
2811 		log.u_bbr.pkts_out = rack->r_ctl.rack_rs.rs_flags;
2812 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2813 		if (rsm) {
2814 			log.u_bbr.pkt_epoch = rsm->r_start;
2815 			log.u_bbr.lost = rsm->r_end;
2816 			log.u_bbr.cwnd_gain = rsm->r_rtr_cnt;
2817 			/* We loose any upper of the 24 bits */
2818 			log.u_bbr.pacing_gain = (uint16_t)rsm->r_flags;
2819 		} else {
2820 			/* Its a SYN */
2821 			log.u_bbr.pkt_epoch = rack->rc_tp->iss;
2822 			log.u_bbr.lost = 0;
2823 			log.u_bbr.cwnd_gain = 0;
2824 			log.u_bbr.pacing_gain = 0;
2825 		}
2826 		/* Write out general bits of interest rrs here */
2827 		log.u_bbr.use_lt_bw = rack->rc_highly_buffered;
2828 		log.u_bbr.use_lt_bw <<= 1;
2829 		log.u_bbr.use_lt_bw |= rack->forced_ack;
2830 		log.u_bbr.use_lt_bw <<= 1;
2831 		log.u_bbr.use_lt_bw |= rack->rc_gp_dyn_mul;
2832 		log.u_bbr.use_lt_bw <<= 1;
2833 		log.u_bbr.use_lt_bw |= rack->in_probe_rtt;
2834 		log.u_bbr.use_lt_bw <<= 1;
2835 		log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt;
2836 		log.u_bbr.use_lt_bw <<= 1;
2837 		log.u_bbr.use_lt_bw |= rack->app_limited_needs_set;
2838 		log.u_bbr.use_lt_bw <<= 1;
2839 		log.u_bbr.use_lt_bw |= rack->rc_gp_filled;
2840 		log.u_bbr.use_lt_bw <<= 1;
2841 		log.u_bbr.use_lt_bw |= rack->rc_dragged_bottom;
2842 		log.u_bbr.applimited = rack->r_ctl.rc_target_probertt_flight;
2843 		log.u_bbr.epoch = rack->r_ctl.rc_time_probertt_starts;
2844 		log.u_bbr.lt_epoch = rack->r_ctl.rc_time_probertt_entered;
2845 		log.u_bbr.cur_del_rate = rack->r_ctl.rc_lower_rtt_us_cts;
2846 		log.u_bbr.delRate = rack->r_ctl.rc_gp_srtt;
2847 		log.u_bbr.bw_inuse = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
2848 		log.u_bbr.bw_inuse <<= 32;
2849 		if (rsm)
2850 			log.u_bbr.bw_inuse |= ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]);
2851 		TCP_LOG_EVENTP(tp, NULL,
2852 		    &rack->rc_inp->inp_socket->so_rcv,
2853 		    &rack->rc_inp->inp_socket->so_snd,
2854 		    BBR_LOG_BBRRTT, 0,
2855 		    0, &log, false, &tv);
2856 
2857 
2858 	}
2859 }
2860 
2861 static void
2862 rack_log_rtt_sample(struct tcp_rack *rack, uint32_t rtt)
2863 {
2864 	/*
2865 	 * Log the rtt sample we are
2866 	 * applying to the srtt algorithm in
2867 	 * useconds.
2868 	 */
2869 	if (tcp_bblogging_on(rack->rc_tp)) {
2870 		union tcp_log_stackspecific log;
2871 		struct timeval tv;
2872 
2873 		/* Convert our ms to a microsecond */
2874 		memset(&log, 0, sizeof(log));
2875 		log.u_bbr.flex1 = rtt;
2876 		log.u_bbr.flex2 = rack->r_ctl.ack_count;
2877 		log.u_bbr.flex3 = rack->r_ctl.sack_count;
2878 		log.u_bbr.flex4 = rack->r_ctl.sack_noextra_move;
2879 		log.u_bbr.flex5 = rack->r_ctl.sack_moved_extra;
2880 		log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
2881 		log.u_bbr.flex7 = 1;
2882 		log.u_bbr.flex8 = rack->sack_attack_disable;
2883 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2884 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2885 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2886 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2887 		log.u_bbr.pacing_gain = rack->r_must_retran;
2888 		/*
2889 		 * We capture in delRate the upper 32 bits as
2890 		 * the confidence level we had declared, and the
2891 		 * lower 32 bits as the actual RTT using the arrival
2892 		 * timestamp.
2893 		 */
2894 		log.u_bbr.delRate = rack->r_ctl.rack_rs.confidence;
2895 		log.u_bbr.delRate <<= 32;
2896 		log.u_bbr.delRate |= rack->r_ctl.rack_rs.rs_us_rtt;
2897 		/* Lets capture all the things that make up t_rtxcur */
2898 		log.u_bbr.applimited = rack_rto_min;
2899 		log.u_bbr.epoch = rack_rto_max;
2900 		log.u_bbr.lt_epoch = rack->r_ctl.timer_slop;
2901 		log.u_bbr.lost = rack_rto_min;
2902 		log.u_bbr.pkt_epoch = TICKS_2_USEC(tcp_rexmit_slop);
2903 		log.u_bbr.rttProp = RACK_REXMTVAL(rack->rc_tp);
2904 		log.u_bbr.bw_inuse = rack->r_ctl.act_rcv_time.tv_sec;
2905 		log.u_bbr.bw_inuse *= HPTS_USEC_IN_SEC;
2906 		log.u_bbr.bw_inuse += rack->r_ctl.act_rcv_time.tv_usec;
2907 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2908 		    &rack->rc_inp->inp_socket->so_rcv,
2909 		    &rack->rc_inp->inp_socket->so_snd,
2910 		    TCP_LOG_RTT, 0,
2911 		    0, &log, false, &tv);
2912 	}
2913 }
2914 
2915 static void
2916 rack_log_rtt_sample_calc(struct tcp_rack *rack, uint32_t rtt, uint32_t send_time, uint32_t ack_time, int where)
2917 {
2918 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
2919 		union tcp_log_stackspecific log;
2920 		struct timeval tv;
2921 
2922 		/* Convert our ms to a microsecond */
2923 		memset(&log, 0, sizeof(log));
2924 		log.u_bbr.flex1 = rtt;
2925 		log.u_bbr.flex2 = send_time;
2926 		log.u_bbr.flex3 = ack_time;
2927 		log.u_bbr.flex4 = where;
2928 		log.u_bbr.flex7 = 2;
2929 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2930 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2931 		    &rack->rc_inp->inp_socket->so_rcv,
2932 		    &rack->rc_inp->inp_socket->so_snd,
2933 		    TCP_LOG_RTT, 0,
2934 		    0, &log, false, &tv);
2935 	}
2936 }
2937 
2938 
2939 static void
2940 rack_log_rtt_sendmap(struct tcp_rack *rack, uint32_t idx, uint64_t tsv, uint32_t tsecho)
2941 {
2942 	if (tcp_bblogging_on(rack->rc_tp)) {
2943 		union tcp_log_stackspecific log;
2944 		struct timeval tv;
2945 
2946 		/* Convert our ms to a microsecond */
2947 		memset(&log, 0, sizeof(log));
2948 		log.u_bbr.flex1 = idx;
2949 		log.u_bbr.flex2 = rack_ts_to_msec(tsv);
2950 		log.u_bbr.flex3 = tsecho;
2951 		log.u_bbr.flex7 = 3;
2952 		log.u_bbr.rttProp = tsv;
2953 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2954 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2955 		    &rack->rc_inp->inp_socket->so_rcv,
2956 		    &rack->rc_inp->inp_socket->so_snd,
2957 		    TCP_LOG_RTT, 0,
2958 		    0, &log, false, &tv);
2959 	}
2960 }
2961 
2962 
2963 static inline void
2964 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick,  int event, int line)
2965 {
2966 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
2967 		union tcp_log_stackspecific log;
2968 		struct timeval tv;
2969 
2970 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2971 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2972 		log.u_bbr.flex1 = line;
2973 		log.u_bbr.flex2 = tick;
2974 		log.u_bbr.flex3 = tp->t_maxunacktime;
2975 		log.u_bbr.flex4 = tp->t_acktime;
2976 		log.u_bbr.flex8 = event;
2977 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2978 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2979 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2980 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2981 		log.u_bbr.pacing_gain = rack->r_must_retran;
2982 		TCP_LOG_EVENTP(tp, NULL,
2983 		    &rack->rc_inp->inp_socket->so_rcv,
2984 		    &rack->rc_inp->inp_socket->so_snd,
2985 		    BBR_LOG_PROGRESS, 0,
2986 		    0, &log, false, &tv);
2987 	}
2988 }
2989 
2990 static void
2991 rack_log_type_bbrsnd(struct tcp_rack *rack, uint32_t len, uint32_t slot, uint32_t cts, struct timeval *tv, int line)
2992 {
2993 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
2994 		union tcp_log_stackspecific log;
2995 
2996 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2997 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2998 		log.u_bbr.flex1 = slot;
2999 		if (rack->rack_no_prr)
3000 			log.u_bbr.flex2 = 0;
3001 		else
3002 			log.u_bbr.flex2 = rack->r_ctl.rc_prr_sndcnt;
3003 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
3004 		log.u_bbr.flex5 = rack->r_ctl.ack_during_sd;
3005 		log.u_bbr.flex6 = line;
3006 		log.u_bbr.flex7 = (0x0000ffff & rack->r_ctl.rc_hpts_flags);
3007 		log.u_bbr.flex8 = rack->rc_in_persist;
3008 		log.u_bbr.timeStamp = cts;
3009 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3010 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3011 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3012 		log.u_bbr.pacing_gain = rack->r_must_retran;
3013 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3014 		    &rack->rc_inp->inp_socket->so_rcv,
3015 		    &rack->rc_inp->inp_socket->so_snd,
3016 		    BBR_LOG_BBRSND, 0,
3017 		    0, &log, false, tv);
3018 	}
3019 }
3020 
3021 static void
3022 rack_log_doseg_done(struct tcp_rack *rack, uint32_t cts, int32_t nxt_pkt, int32_t did_out, int way_out, int nsegs)
3023 {
3024 	if (tcp_bblogging_on(rack->rc_tp)) {
3025 		union tcp_log_stackspecific log;
3026 		struct timeval tv;
3027 
3028 		memset(&log, 0, sizeof(log));
3029 		log.u_bbr.flex1 = did_out;
3030 		log.u_bbr.flex2 = nxt_pkt;
3031 		log.u_bbr.flex3 = way_out;
3032 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
3033 		if (rack->rack_no_prr)
3034 			log.u_bbr.flex5 = 0;
3035 		else
3036 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
3037 		log.u_bbr.flex6 = nsegs;
3038 		log.u_bbr.applimited = rack->r_ctl.rc_pace_min_segs;
3039 		log.u_bbr.flex7 = rack->rc_ack_can_sendout_data;	/* Do we have ack-can-send set */
3040 		log.u_bbr.flex7 <<= 1;
3041 		log.u_bbr.flex7 |= rack->r_fast_output;	/* is fast output primed */
3042 		log.u_bbr.flex7 <<= 1;
3043 		log.u_bbr.flex7 |= rack->r_wanted_output;	/* Do we want output */
3044 		log.u_bbr.flex8 = rack->rc_in_persist;
3045 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3046 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3047 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3048 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
3049 		log.u_bbr.use_lt_bw <<= 1;
3050 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
3051 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3052 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3053 		log.u_bbr.pacing_gain = rack->r_must_retran;
3054 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3055 		    &rack->rc_inp->inp_socket->so_rcv,
3056 		    &rack->rc_inp->inp_socket->so_snd,
3057 		    BBR_LOG_DOSEG_DONE, 0,
3058 		    0, &log, false, &tv);
3059 	}
3060 }
3061 
3062 static void
3063 rack_log_type_pacing_sizes(struct tcpcb *tp, struct tcp_rack *rack, uint32_t arg1, uint32_t arg2, uint32_t arg3, uint8_t frm)
3064 {
3065 	if (tcp_bblogging_on(rack->rc_tp)) {
3066 		union tcp_log_stackspecific log;
3067 		struct timeval tv;
3068 
3069 		memset(&log, 0, sizeof(log));
3070 		log.u_bbr.flex1 = rack->r_ctl.rc_pace_min_segs;
3071 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
3072 		log.u_bbr.flex4 = arg1;
3073 		log.u_bbr.flex5 = arg2;
3074 		log.u_bbr.flex7 = rack->r_ctl.rc_user_set_min_segs;
3075 		log.u_bbr.flex6 = arg3;
3076 		log.u_bbr.flex8 = frm;
3077 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3078 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3079 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3080 		log.u_bbr.applimited = rack->r_ctl.rc_sacked;
3081 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3082 		log.u_bbr.pacing_gain = rack->r_must_retran;
3083 		TCP_LOG_EVENTP(tp, NULL, &tptosocket(tp)->so_rcv,
3084 		    &tptosocket(tp)->so_snd,
3085 		    TCP_HDWR_PACE_SIZE, 0, 0, &log, false, &tv);
3086 	}
3087 }
3088 
3089 static void
3090 rack_log_type_just_return(struct tcp_rack *rack, uint32_t cts, uint32_t tlen, uint32_t slot,
3091 			  uint8_t hpts_calling, int reason, uint32_t cwnd_to_use)
3092 {
3093 	if (tcp_bblogging_on(rack->rc_tp)) {
3094 		union tcp_log_stackspecific log;
3095 		struct timeval tv;
3096 
3097 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3098 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3099 		log.u_bbr.flex1 = slot;
3100 		log.u_bbr.flex2 = rack->r_ctl.rc_hpts_flags;
3101 		log.u_bbr.flex4 = reason;
3102 		if (rack->rack_no_prr)
3103 			log.u_bbr.flex5 = 0;
3104 		else
3105 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
3106 		log.u_bbr.flex7 = hpts_calling;
3107 		log.u_bbr.flex8 = rack->rc_in_persist;
3108 		log.u_bbr.lt_epoch = cwnd_to_use;
3109 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3110 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3111 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3112 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3113 		log.u_bbr.pacing_gain = rack->r_must_retran;
3114 		log.u_bbr.cwnd_gain = rack->rc_has_collapsed;
3115 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3116 		    &rack->rc_inp->inp_socket->so_rcv,
3117 		    &rack->rc_inp->inp_socket->so_snd,
3118 		    BBR_LOG_JUSTRET, 0,
3119 		    tlen, &log, false, &tv);
3120 	}
3121 }
3122 
3123 static void
3124 rack_log_to_cancel(struct tcp_rack *rack, int32_t hpts_removed, int line, uint32_t us_cts,
3125 		   struct timeval *tv, uint32_t flags_on_entry)
3126 {
3127 	if (tcp_bblogging_on(rack->rc_tp)) {
3128 		union tcp_log_stackspecific log;
3129 
3130 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3131 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3132 		log.u_bbr.flex1 = line;
3133 		log.u_bbr.flex2 = rack->r_ctl.rc_last_output_to;
3134 		log.u_bbr.flex3 = flags_on_entry;
3135 		log.u_bbr.flex4 = us_cts;
3136 		if (rack->rack_no_prr)
3137 			log.u_bbr.flex5 = 0;
3138 		else
3139 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
3140 		log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
3141 		log.u_bbr.flex7 = hpts_removed;
3142 		log.u_bbr.flex8 = 1;
3143 		log.u_bbr.applimited = rack->r_ctl.rc_hpts_flags;
3144 		log.u_bbr.timeStamp = us_cts;
3145 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3146 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3147 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3148 		log.u_bbr.pacing_gain = rack->r_must_retran;
3149 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3150 		    &rack->rc_inp->inp_socket->so_rcv,
3151 		    &rack->rc_inp->inp_socket->so_snd,
3152 		    BBR_LOG_TIMERCANC, 0,
3153 		    0, &log, false, tv);
3154 	}
3155 }
3156 
3157 static void
3158 rack_log_alt_to_to_cancel(struct tcp_rack *rack,
3159 			  uint32_t flex1, uint32_t flex2,
3160 			  uint32_t flex3, uint32_t flex4,
3161 			  uint32_t flex5, uint32_t flex6,
3162 			  uint16_t flex7, uint8_t mod)
3163 {
3164 	if (tcp_bblogging_on(rack->rc_tp)) {
3165 		union tcp_log_stackspecific log;
3166 		struct timeval tv;
3167 
3168 		if (mod == 1) {
3169 			/* No you can't use 1, its for the real to cancel */
3170 			return;
3171 		}
3172 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3173 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3174 		log.u_bbr.flex1 = flex1;
3175 		log.u_bbr.flex2 = flex2;
3176 		log.u_bbr.flex3 = flex3;
3177 		log.u_bbr.flex4 = flex4;
3178 		log.u_bbr.flex5 = flex5;
3179 		log.u_bbr.flex6 = flex6;
3180 		log.u_bbr.flex7 = flex7;
3181 		log.u_bbr.flex8 = mod;
3182 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3183 		    &rack->rc_inp->inp_socket->so_rcv,
3184 		    &rack->rc_inp->inp_socket->so_snd,
3185 		    BBR_LOG_TIMERCANC, 0,
3186 		    0, &log, false, &tv);
3187 	}
3188 }
3189 
3190 static void
3191 rack_log_to_processing(struct tcp_rack *rack, uint32_t cts, int32_t ret, int32_t timers)
3192 {
3193 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
3194 		union tcp_log_stackspecific log;
3195 		struct timeval tv;
3196 
3197 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3198 		log.u_bbr.flex1 = timers;
3199 		log.u_bbr.flex2 = ret;
3200 		log.u_bbr.flex3 = rack->r_ctl.rc_timer_exp;
3201 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
3202 		log.u_bbr.flex5 = cts;
3203 		if (rack->rack_no_prr)
3204 			log.u_bbr.flex6 = 0;
3205 		else
3206 			log.u_bbr.flex6 = rack->r_ctl.rc_prr_sndcnt;
3207 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3208 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3209 		log.u_bbr.pacing_gain = rack->r_must_retran;
3210 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3211 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3212 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3213 		    &rack->rc_inp->inp_socket->so_rcv,
3214 		    &rack->rc_inp->inp_socket->so_snd,
3215 		    BBR_LOG_TO_PROCESS, 0,
3216 		    0, &log, false, &tv);
3217 	}
3218 }
3219 
3220 static void
3221 rack_log_to_prr(struct tcp_rack *rack, int frm, int orig_cwnd, int line)
3222 {
3223 	if (tcp_bblogging_on(rack->rc_tp)) {
3224 		union tcp_log_stackspecific log;
3225 		struct timeval tv;
3226 
3227 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3228 		log.u_bbr.flex1 = rack->r_ctl.rc_prr_out;
3229 		log.u_bbr.flex2 = rack->r_ctl.rc_prr_recovery_fs;
3230 		if (rack->rack_no_prr)
3231 			log.u_bbr.flex3 = 0;
3232 		else
3233 			log.u_bbr.flex3 = rack->r_ctl.rc_prr_sndcnt;
3234 		log.u_bbr.flex4 = rack->r_ctl.rc_prr_delivered;
3235 		log.u_bbr.flex5 = rack->r_ctl.rc_sacked;
3236 		log.u_bbr.flex6 = rack->r_ctl.rc_holes_rxt;
3237 		log.u_bbr.flex7 = line;
3238 		log.u_bbr.flex8 = frm;
3239 		log.u_bbr.pkts_out = orig_cwnd;
3240 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3241 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3242 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
3243 		log.u_bbr.use_lt_bw <<= 1;
3244 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
3245 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3246 		    &rack->rc_inp->inp_socket->so_rcv,
3247 		    &rack->rc_inp->inp_socket->so_snd,
3248 		    BBR_LOG_BBRUPD, 0,
3249 		    0, &log, false, &tv);
3250 	}
3251 }
3252 
3253 #ifdef TCP_SAD_DETECTION
3254 static void
3255 rack_log_sad(struct tcp_rack *rack, int event)
3256 {
3257 	if (tcp_bblogging_on(rack->rc_tp)) {
3258 		union tcp_log_stackspecific log;
3259 		struct timeval tv;
3260 
3261 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3262 		log.u_bbr.flex1 = rack->r_ctl.sack_count;
3263 		log.u_bbr.flex2 = rack->r_ctl.ack_count;
3264 		log.u_bbr.flex3 = rack->r_ctl.sack_moved_extra;
3265 		log.u_bbr.flex4 = rack->r_ctl.sack_noextra_move;
3266 		log.u_bbr.flex5 = rack->r_ctl.rc_num_maps_alloced;
3267 		log.u_bbr.flex6 = tcp_sack_to_ack_thresh;
3268 		log.u_bbr.pkts_out = tcp_sack_to_move_thresh;
3269 		log.u_bbr.lt_epoch = (tcp_force_detection << 8);
3270 		log.u_bbr.lt_epoch |= rack->do_detection;
3271 		log.u_bbr.applimited = tcp_map_minimum;
3272 		log.u_bbr.flex7 = rack->sack_attack_disable;
3273 		log.u_bbr.flex8 = event;
3274 		log.u_bbr.bbr_state = rack->rc_suspicious;
3275 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3276 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3277 		log.u_bbr.delivered = tcp_sad_decay_val;
3278 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3279 		    &rack->rc_inp->inp_socket->so_rcv,
3280 		    &rack->rc_inp->inp_socket->so_snd,
3281 		    TCP_SAD_DETECT, 0,
3282 		    0, &log, false, &tv);
3283 	}
3284 }
3285 #endif
3286 
3287 static void
3288 rack_counter_destroy(void)
3289 {
3290 	counter_u64_free(rack_total_bytes);
3291 	counter_u64_free(rack_fto_send);
3292 	counter_u64_free(rack_fto_rsm_send);
3293 	counter_u64_free(rack_nfto_resend);
3294 	counter_u64_free(rack_hw_pace_init_fail);
3295 	counter_u64_free(rack_hw_pace_lost);
3296 	counter_u64_free(rack_non_fto_send);
3297 	counter_u64_free(rack_extended_rfo);
3298 	counter_u64_free(rack_ack_total);
3299 	counter_u64_free(rack_express_sack);
3300 	counter_u64_free(rack_sack_total);
3301 	counter_u64_free(rack_move_none);
3302 	counter_u64_free(rack_move_some);
3303 	counter_u64_free(rack_sack_attacks_detected);
3304 	counter_u64_free(rack_sack_attacks_reversed);
3305 	counter_u64_free(rack_sack_attacks_suspect);
3306 	counter_u64_free(rack_sack_used_next_merge);
3307 	counter_u64_free(rack_sack_used_prev_merge);
3308 	counter_u64_free(rack_tlp_tot);
3309 	counter_u64_free(rack_tlp_newdata);
3310 	counter_u64_free(rack_tlp_retran);
3311 	counter_u64_free(rack_tlp_retran_bytes);
3312 	counter_u64_free(rack_to_tot);
3313 	counter_u64_free(rack_saw_enobuf);
3314 	counter_u64_free(rack_saw_enobuf_hw);
3315 	counter_u64_free(rack_saw_enetunreach);
3316 	counter_u64_free(rack_hot_alloc);
3317 	counter_u64_free(rack_to_alloc);
3318 	counter_u64_free(rack_to_alloc_hard);
3319 	counter_u64_free(rack_to_alloc_emerg);
3320 	counter_u64_free(rack_to_alloc_limited);
3321 	counter_u64_free(rack_alloc_limited_conns);
3322 	counter_u64_free(rack_split_limited);
3323 	counter_u64_free(rack_multi_single_eq);
3324 	counter_u64_free(rack_rxt_clamps_cwnd);
3325 	counter_u64_free(rack_rxt_clamps_cwnd_uniq);
3326 	counter_u64_free(rack_proc_non_comp_ack);
3327 	counter_u64_free(rack_sack_proc_all);
3328 	counter_u64_free(rack_sack_proc_restart);
3329 	counter_u64_free(rack_sack_proc_short);
3330 	counter_u64_free(rack_sack_skipped_acked);
3331 	counter_u64_free(rack_sack_splits);
3332 	counter_u64_free(rack_input_idle_reduces);
3333 	counter_u64_free(rack_collapsed_win);
3334 	counter_u64_free(rack_collapsed_win_rxt);
3335 	counter_u64_free(rack_collapsed_win_rxt_bytes);
3336 	counter_u64_free(rack_collapsed_win_seen);
3337 	counter_u64_free(rack_try_scwnd);
3338 	counter_u64_free(rack_persists_sends);
3339 	counter_u64_free(rack_persists_acks);
3340 	counter_u64_free(rack_persists_loss);
3341 	counter_u64_free(rack_persists_lost_ends);
3342 #ifdef INVARIANTS
3343 	counter_u64_free(rack_adjust_map_bw);
3344 #endif
3345 	COUNTER_ARRAY_FREE(rack_out_size, TCP_MSS_ACCT_SIZE);
3346 	COUNTER_ARRAY_FREE(rack_opts_arry, RACK_OPTS_SIZE);
3347 }
3348 
3349 static struct rack_sendmap *
3350 rack_alloc(struct tcp_rack *rack)
3351 {
3352 	struct rack_sendmap *rsm;
3353 
3354 	/*
3355 	 * First get the top of the list it in
3356 	 * theory is the "hottest" rsm we have,
3357 	 * possibly just freed by ack processing.
3358 	 */
3359 	if (rack->rc_free_cnt > rack_free_cache) {
3360 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
3361 		TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
3362 		counter_u64_add(rack_hot_alloc, 1);
3363 		rack->rc_free_cnt--;
3364 		return (rsm);
3365 	}
3366 	/*
3367 	 * Once we get under our free cache we probably
3368 	 * no longer have a "hot" one available. Lets
3369 	 * get one from UMA.
3370 	 */
3371 	rsm = uma_zalloc(rack_zone, M_NOWAIT);
3372 	if (rsm) {
3373 		rack->r_ctl.rc_num_maps_alloced++;
3374 		counter_u64_add(rack_to_alloc, 1);
3375 		return (rsm);
3376 	}
3377 	/*
3378 	 * Dig in to our aux rsm's (the last two) since
3379 	 * UMA failed to get us one.
3380 	 */
3381 	if (rack->rc_free_cnt) {
3382 		counter_u64_add(rack_to_alloc_emerg, 1);
3383 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
3384 		TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
3385 		rack->rc_free_cnt--;
3386 		return (rsm);
3387 	}
3388 	return (NULL);
3389 }
3390 
3391 static struct rack_sendmap *
3392 rack_alloc_full_limit(struct tcp_rack *rack)
3393 {
3394 	if ((V_tcp_map_entries_limit > 0) &&
3395 	    (rack->do_detection == 0) &&
3396 	    (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
3397 		counter_u64_add(rack_to_alloc_limited, 1);
3398 		if (!rack->alloc_limit_reported) {
3399 			rack->alloc_limit_reported = 1;
3400 			counter_u64_add(rack_alloc_limited_conns, 1);
3401 		}
3402 		return (NULL);
3403 	}
3404 	return (rack_alloc(rack));
3405 }
3406 
3407 /* wrapper to allocate a sendmap entry, subject to a specific limit */
3408 static struct rack_sendmap *
3409 rack_alloc_limit(struct tcp_rack *rack, uint8_t limit_type)
3410 {
3411 	struct rack_sendmap *rsm;
3412 
3413 	if (limit_type) {
3414 		/* currently there is only one limit type */
3415 		if (rack->r_ctl.rc_split_limit > 0 &&
3416 		    (rack->do_detection == 0) &&
3417 		    rack->r_ctl.rc_num_split_allocs >= rack->r_ctl.rc_split_limit) {
3418 			counter_u64_add(rack_split_limited, 1);
3419 			if (!rack->alloc_limit_reported) {
3420 				rack->alloc_limit_reported = 1;
3421 				counter_u64_add(rack_alloc_limited_conns, 1);
3422 			}
3423 			return (NULL);
3424 #ifdef TCP_SAD_DETECTION
3425 		} else if ((tcp_sad_limit != 0) &&
3426 			   (rack->do_detection == 1) &&
3427 			   (rack->r_ctl.rc_num_split_allocs >= tcp_sad_limit)) {
3428 			counter_u64_add(rack_split_limited, 1);
3429 			if (!rack->alloc_limit_reported) {
3430 				rack->alloc_limit_reported = 1;
3431 				counter_u64_add(rack_alloc_limited_conns, 1);
3432 			}
3433 			return (NULL);
3434 #endif
3435 		}
3436 	}
3437 
3438 	/* allocate and mark in the limit type, if set */
3439 	rsm = rack_alloc(rack);
3440 	if (rsm != NULL && limit_type) {
3441 		rsm->r_limit_type = limit_type;
3442 		rack->r_ctl.rc_num_split_allocs++;
3443 	}
3444 	return (rsm);
3445 }
3446 
3447 static void
3448 rack_free_trim(struct tcp_rack *rack)
3449 {
3450 	struct rack_sendmap *rsm;
3451 
3452 	/*
3453 	 * Free up all the tail entries until
3454 	 * we get our list down to the limit.
3455 	 */
3456 	while (rack->rc_free_cnt > rack_free_cache) {
3457 		rsm = TAILQ_LAST(&rack->r_ctl.rc_free, rack_head);
3458 		TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
3459 		rack->rc_free_cnt--;
3460 		rack->r_ctl.rc_num_maps_alloced--;
3461 		uma_zfree(rack_zone, rsm);
3462 	}
3463 }
3464 
3465 static void
3466 rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm)
3467 {
3468 	if (rsm->r_flags & RACK_APP_LIMITED) {
3469 		if (rack->r_ctl.rc_app_limited_cnt > 0) {
3470 			rack->r_ctl.rc_app_limited_cnt--;
3471 		}
3472 	}
3473 	if (rsm->r_limit_type) {
3474 		/* currently there is only one limit type */
3475 		rack->r_ctl.rc_num_split_allocs--;
3476 	}
3477 	if (rsm == rack->r_ctl.rc_first_appl) {
3478 		if (rack->r_ctl.rc_app_limited_cnt == 0)
3479 			rack->r_ctl.rc_first_appl = NULL;
3480 		else
3481 			rack->r_ctl.rc_first_appl = tqhash_find(rack->r_ctl.tqh, rsm->r_nseq_appl);
3482 	}
3483 	if (rsm == rack->r_ctl.rc_resend)
3484 		rack->r_ctl.rc_resend = NULL;
3485 	if (rsm == rack->r_ctl.rc_end_appl)
3486 		rack->r_ctl.rc_end_appl = NULL;
3487 	if (rack->r_ctl.rc_tlpsend == rsm)
3488 		rack->r_ctl.rc_tlpsend = NULL;
3489 	if (rack->r_ctl.rc_sacklast == rsm)
3490 		rack->r_ctl.rc_sacklast = NULL;
3491 	memset(rsm, 0, sizeof(struct rack_sendmap));
3492 	/* Make sure we are not going to overrun our count limit of 0xff */
3493 	if ((rack->rc_free_cnt + 1) > 0xff) {
3494 		rack_free_trim(rack);
3495 	}
3496 	TAILQ_INSERT_HEAD(&rack->r_ctl.rc_free, rsm, r_tnext);
3497 	rack->rc_free_cnt++;
3498 }
3499 
3500 static uint32_t
3501 rack_get_measure_window(struct tcpcb *tp, struct tcp_rack *rack)
3502 {
3503 	uint64_t srtt, bw, len, tim;
3504 	uint32_t segsiz, def_len, minl;
3505 
3506 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
3507 	def_len = rack_def_data_window * segsiz;
3508 	if (rack->rc_gp_filled == 0) {
3509 		/*
3510 		 * We have no measurement (IW is in flight?) so
3511 		 * we can only guess using our data_window sysctl
3512 		 * value (usually 20MSS).
3513 		 */
3514 		return (def_len);
3515 	}
3516 	/*
3517 	 * Now we have a number of factors to consider.
3518 	 *
3519 	 * 1) We have a desired BDP which is usually
3520 	 *    at least 2.
3521 	 * 2) We have a minimum number of rtt's usually 1 SRTT
3522 	 *    but we allow it too to be more.
3523 	 * 3) We want to make sure a measurement last N useconds (if
3524 	 *    we have set rack_min_measure_usec.
3525 	 *
3526 	 * We handle the first concern here by trying to create a data
3527 	 * window of max(rack_def_data_window, DesiredBDP). The
3528 	 * second concern we handle in not letting the measurement
3529 	 * window end normally until at least the required SRTT's
3530 	 * have gone by which is done further below in
3531 	 * rack_enough_for_measurement(). Finally the third concern
3532 	 * we also handle here by calculating how long that time
3533 	 * would take at the current BW and then return the
3534 	 * max of our first calculation and that length. Note
3535 	 * that if rack_min_measure_usec is 0, we don't deal
3536 	 * with concern 3. Also for both Concern 1 and 3 an
3537 	 * application limited period could end the measurement
3538 	 * earlier.
3539 	 *
3540 	 * So lets calculate the BDP with the "known" b/w using
3541 	 * the SRTT has our rtt and then multiply it by the
3542 	 * goal.
3543 	 */
3544 	bw = rack_get_bw(rack);
3545 	srtt = (uint64_t)tp->t_srtt;
3546 	len = bw * srtt;
3547 	len /= (uint64_t)HPTS_USEC_IN_SEC;
3548 	len *= max(1, rack_goal_bdp);
3549 	/* Now we need to round up to the nearest MSS */
3550 	len = roundup(len, segsiz);
3551 	if (rack_min_measure_usec) {
3552 		/* Now calculate our min length for this b/w */
3553 		tim = rack_min_measure_usec;
3554 		minl = (tim * bw) / (uint64_t)HPTS_USEC_IN_SEC;
3555 		if (minl == 0)
3556 			minl = 1;
3557 		minl = roundup(minl, segsiz);
3558 		if (len < minl)
3559 			len = minl;
3560 	}
3561 	/*
3562 	 * Now if we have a very small window we want
3563 	 * to attempt to get the window that is
3564 	 * as small as possible. This happens on
3565 	 * low b/w connections and we don't want to
3566 	 * span huge numbers of rtt's between measurements.
3567 	 *
3568 	 * We basically include 2 over our "MIN window" so
3569 	 * that the measurement can be shortened (possibly) by
3570 	 * an ack'ed packet.
3571 	 */
3572 	if (len < def_len)
3573 		return (max((uint32_t)len, ((MIN_GP_WIN+2) * segsiz)));
3574 	else
3575 		return (max((uint32_t)len, def_len));
3576 
3577 }
3578 
3579 static int
3580 rack_enough_for_measurement(struct tcpcb *tp, struct tcp_rack *rack, tcp_seq th_ack, uint8_t *quality)
3581 {
3582 	uint32_t tim, srtts, segsiz;
3583 
3584 	/*
3585 	 * Has enough time passed for the GP measurement to be valid?
3586 	 */
3587 	if (SEQ_LT(th_ack, tp->gput_seq)) {
3588 		/* Not enough bytes yet */
3589 		return (0);
3590 	}
3591 	if ((tp->snd_max == tp->snd_una) ||
3592 	    (th_ack == tp->snd_max)){
3593 		/*
3594 		 * All is acked quality of all acked is
3595 		 * usually low or medium, but we in theory could split
3596 		 * all acked into two cases, where you got
3597 		 * a signifigant amount of your window and
3598 		 * where you did not. For now we leave it
3599 		 * but it is something to contemplate in the
3600 		 * future. The danger here is that delayed ack
3601 		 * is effecting the last byte (which is a 50:50 chance).
3602 		 */
3603 		*quality = RACK_QUALITY_ALLACKED;
3604 		return (1);
3605 	}
3606 	if (SEQ_GEQ(th_ack,  tp->gput_ack)) {
3607 		/*
3608 		 * We obtained our entire window of data we wanted
3609 		 * no matter if we are in recovery or not then
3610 		 * its ok since expanding the window does not
3611 		 * make things fuzzy (or at least not as much).
3612 		 */
3613 		*quality = RACK_QUALITY_HIGH;
3614 		return (1);
3615 	}
3616 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
3617 	if (SEQ_LT(th_ack, tp->gput_ack) &&
3618 	    ((th_ack - tp->gput_seq) < max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) {
3619 		/* Not enough bytes yet */
3620 		return (0);
3621 	}
3622 	if (rack->r_ctl.rc_first_appl &&
3623 	    (SEQ_GEQ(th_ack, rack->r_ctl.rc_first_appl->r_end))) {
3624 		/*
3625 		 * We are up to the app limited send point
3626 		 * we have to measure irrespective of the time..
3627 		 */
3628 		*quality = RACK_QUALITY_APPLIMITED;
3629 		return (1);
3630 	}
3631 	/* Now what about time? */
3632 	srtts = (rack->r_ctl.rc_gp_srtt * rack_min_srtts);
3633 	tim = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - tp->gput_ts;
3634 	if ((tim >= srtts) && (IN_RECOVERY(rack->rc_tp->t_flags) == 0)) {
3635 		/*
3636 		 * We do not allow a measurement if we are in recovery
3637 		 * that would shrink the goodput window we wanted.
3638 		 * This is to prevent cloudyness of when the last send
3639 		 * was actually made.
3640 		 */
3641 		*quality = RACK_QUALITY_HIGH;
3642 		return (1);
3643 	}
3644 	/* Nope not even a full SRTT has passed */
3645 	return (0);
3646 }
3647 
3648 static void
3649 rack_log_timely(struct tcp_rack *rack,
3650 		uint32_t logged, uint64_t cur_bw, uint64_t low_bnd,
3651 		uint64_t up_bnd, int line, uint8_t method)
3652 {
3653 	if (tcp_bblogging_on(rack->rc_tp)) {
3654 		union tcp_log_stackspecific log;
3655 		struct timeval tv;
3656 
3657 		memset(&log, 0, sizeof(log));
3658 		log.u_bbr.flex1 = logged;
3659 		log.u_bbr.flex2 = rack->rc_gp_timely_inc_cnt;
3660 		log.u_bbr.flex2 <<= 4;
3661 		log.u_bbr.flex2 |= rack->rc_gp_timely_dec_cnt;
3662 		log.u_bbr.flex2 <<= 4;
3663 		log.u_bbr.flex2 |= rack->rc_gp_incr;
3664 		log.u_bbr.flex2 <<= 4;
3665 		log.u_bbr.flex2 |= rack->rc_gp_bwred;
3666 		log.u_bbr.flex3 = rack->rc_gp_incr;
3667 		log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss;
3668 		log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ca;
3669 		log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_rec;
3670 		log.u_bbr.flex7 = rack->rc_gp_bwred;
3671 		log.u_bbr.flex8 = method;
3672 		log.u_bbr.cur_del_rate = cur_bw;
3673 		log.u_bbr.delRate = low_bnd;
3674 		log.u_bbr.bw_inuse = up_bnd;
3675 		log.u_bbr.rttProp = rack_get_bw(rack);
3676 		log.u_bbr.pkt_epoch = line;
3677 		log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff;
3678 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3679 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3680 		log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt;
3681 		log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt;
3682 		log.u_bbr.cwnd_gain = rack->rc_dragged_bottom;
3683 		log.u_bbr.cwnd_gain <<= 1;
3684 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_rec;
3685 		log.u_bbr.cwnd_gain <<= 1;
3686 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss;
3687 		log.u_bbr.cwnd_gain <<= 1;
3688 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca;
3689 		log.u_bbr.lost = rack->r_ctl.rc_loss_count;
3690 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3691 		    &rack->rc_inp->inp_socket->so_rcv,
3692 		    &rack->rc_inp->inp_socket->so_snd,
3693 		    TCP_TIMELY_WORK, 0,
3694 		    0, &log, false, &tv);
3695 	}
3696 }
3697 
3698 static int
3699 rack_bw_can_be_raised(struct tcp_rack *rack, uint64_t cur_bw, uint64_t last_bw_est, uint16_t mult)
3700 {
3701 	/*
3702 	 * Before we increase we need to know if
3703 	 * the estimate just made was less than
3704 	 * our pacing goal (i.e. (cur_bw * mult) > last_bw_est)
3705 	 *
3706 	 * If we already are pacing at a fast enough
3707 	 * rate to push us faster there is no sense of
3708 	 * increasing.
3709 	 *
3710 	 * We first caculate our actual pacing rate (ss or ca multiplier
3711 	 * times our cur_bw).
3712 	 *
3713 	 * Then we take the last measured rate and multipy by our
3714 	 * maximum pacing overage to give us a max allowable rate.
3715 	 *
3716 	 * If our act_rate is smaller than our max_allowable rate
3717 	 * then we should increase. Else we should hold steady.
3718 	 *
3719 	 */
3720 	uint64_t act_rate, max_allow_rate;
3721 
3722 	if (rack_timely_no_stopping)
3723 		return (1);
3724 
3725 	if ((cur_bw == 0) || (last_bw_est == 0)) {
3726 		/*
3727 		 * Initial startup case or
3728 		 * everything is acked case.
3729 		 */
3730 		rack_log_timely(rack,  mult, cur_bw, 0, 0,
3731 				__LINE__, 9);
3732 		return (1);
3733 	}
3734 	if (mult <= 100) {
3735 		/*
3736 		 * We can always pace at or slightly above our rate.
3737 		 */
3738 		rack_log_timely(rack,  mult, cur_bw, 0, 0,
3739 				__LINE__, 9);
3740 		return (1);
3741 	}
3742 	act_rate = cur_bw * (uint64_t)mult;
3743 	act_rate /= 100;
3744 	max_allow_rate = last_bw_est * ((uint64_t)rack_max_per_above + (uint64_t)100);
3745 	max_allow_rate /= 100;
3746 	if (act_rate < max_allow_rate) {
3747 		/*
3748 		 * Here the rate we are actually pacing at
3749 		 * is smaller than 10% above our last measurement.
3750 		 * This means we are pacing below what we would
3751 		 * like to try to achieve (plus some wiggle room).
3752 		 */
3753 		rack_log_timely(rack,  mult, cur_bw, act_rate, max_allow_rate,
3754 				__LINE__, 9);
3755 		return (1);
3756 	} else {
3757 		/*
3758 		 * Here we are already pacing at least rack_max_per_above(10%)
3759 		 * what we are getting back. This indicates most likely
3760 		 * that we are being limited (cwnd/rwnd/app) and can't
3761 		 * get any more b/w. There is no sense of trying to
3762 		 * raise up the pacing rate its not speeding us up
3763 		 * and we already are pacing faster than we are getting.
3764 		 */
3765 		rack_log_timely(rack,  mult, cur_bw, act_rate, max_allow_rate,
3766 				__LINE__, 8);
3767 		return (0);
3768 	}
3769 }
3770 
3771 static void
3772 rack_validate_multipliers_at_or_above100(struct tcp_rack *rack)
3773 {
3774 	/*
3775 	 * When we drag bottom, we want to assure
3776 	 * that no multiplier is below 1.0, if so
3777 	 * we want to restore it to at least that.
3778 	 */
3779 	if (rack->r_ctl.rack_per_of_gp_rec  < 100) {
3780 		/* This is unlikely we usually do not touch recovery */
3781 		rack->r_ctl.rack_per_of_gp_rec = 100;
3782 	}
3783 	if (rack->r_ctl.rack_per_of_gp_ca < 100) {
3784 		rack->r_ctl.rack_per_of_gp_ca = 100;
3785 	}
3786 	if (rack->r_ctl.rack_per_of_gp_ss < 100) {
3787 		rack->r_ctl.rack_per_of_gp_ss = 100;
3788 	}
3789 }
3790 
3791 static void
3792 rack_validate_multipliers_at_or_below_100(struct tcp_rack *rack)
3793 {
3794 	if (rack->r_ctl.rack_per_of_gp_ca > 100) {
3795 		rack->r_ctl.rack_per_of_gp_ca = 100;
3796 	}
3797 	if (rack->r_ctl.rack_per_of_gp_ss > 100) {
3798 		rack->r_ctl.rack_per_of_gp_ss = 100;
3799 	}
3800 }
3801 
3802 static void
3803 rack_increase_bw_mul(struct tcp_rack *rack, int timely_says, uint64_t cur_bw, uint64_t last_bw_est, int override)
3804 {
3805 	int32_t  calc, logged, plus;
3806 
3807 	logged = 0;
3808 
3809 	if (override) {
3810 		/*
3811 		 * override is passed when we are
3812 		 * loosing b/w and making one last
3813 		 * gasp at trying to not loose out
3814 		 * to a new-reno flow.
3815 		 */
3816 		goto extra_boost;
3817 	}
3818 	/* In classic timely we boost by 5x if we have 5 increases in a row, lets not */
3819 	if (rack->rc_gp_incr &&
3820 	    ((rack->rc_gp_timely_inc_cnt + 1) >= RACK_TIMELY_CNT_BOOST)) {
3821 		/*
3822 		 * Reset and get 5 strokes more before the boost. Note
3823 		 * that the count is 0 based so we have to add one.
3824 		 */
3825 extra_boost:
3826 		plus = (uint32_t)rack_gp_increase_per * RACK_TIMELY_CNT_BOOST;
3827 		rack->rc_gp_timely_inc_cnt = 0;
3828 	} else
3829 		plus = (uint32_t)rack_gp_increase_per;
3830 	/* Must be at least 1% increase for true timely increases */
3831 	if ((plus < 1) &&
3832 	    ((rack->r_ctl.rc_rtt_diff <= 0) || (timely_says <= 0)))
3833 		plus = 1;
3834 	if (rack->rc_gp_saw_rec &&
3835 	    (rack->rc_gp_no_rec_chg == 0) &&
3836 	    rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3837 				  rack->r_ctl.rack_per_of_gp_rec)) {
3838 		/* We have been in recovery ding it too */
3839 		calc = rack->r_ctl.rack_per_of_gp_rec + plus;
3840 		if (calc > 0xffff)
3841 			calc = 0xffff;
3842 		logged |= 1;
3843 		rack->r_ctl.rack_per_of_gp_rec = (uint16_t)calc;
3844 		if (rack->r_ctl.rack_per_upper_bound_ca &&
3845 		    (rack->rc_dragged_bottom == 0) &&
3846 		    (rack->r_ctl.rack_per_of_gp_rec > rack->r_ctl.rack_per_upper_bound_ca))
3847 			rack->r_ctl.rack_per_of_gp_rec = rack->r_ctl.rack_per_upper_bound_ca;
3848 	}
3849 	if (rack->rc_gp_saw_ca &&
3850 	    (rack->rc_gp_saw_ss == 0) &&
3851 	    rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3852 				  rack->r_ctl.rack_per_of_gp_ca)) {
3853 		/* In CA */
3854 		calc = rack->r_ctl.rack_per_of_gp_ca + plus;
3855 		if (calc > 0xffff)
3856 			calc = 0xffff;
3857 		logged |= 2;
3858 		rack->r_ctl.rack_per_of_gp_ca = (uint16_t)calc;
3859 		if (rack->r_ctl.rack_per_upper_bound_ca &&
3860 		    (rack->rc_dragged_bottom == 0) &&
3861 		    (rack->r_ctl.rack_per_of_gp_ca > rack->r_ctl.rack_per_upper_bound_ca))
3862 			rack->r_ctl.rack_per_of_gp_ca = rack->r_ctl.rack_per_upper_bound_ca;
3863 	}
3864 	if (rack->rc_gp_saw_ss &&
3865 	    rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3866 				  rack->r_ctl.rack_per_of_gp_ss)) {
3867 		/* In SS */
3868 		calc = rack->r_ctl.rack_per_of_gp_ss + plus;
3869 		if (calc > 0xffff)
3870 			calc = 0xffff;
3871 		rack->r_ctl.rack_per_of_gp_ss = (uint16_t)calc;
3872 		if (rack->r_ctl.rack_per_upper_bound_ss &&
3873 		    (rack->rc_dragged_bottom == 0) &&
3874 		    (rack->r_ctl.rack_per_of_gp_ss > rack->r_ctl.rack_per_upper_bound_ss))
3875 			rack->r_ctl.rack_per_of_gp_ss = rack->r_ctl.rack_per_upper_bound_ss;
3876 		logged |= 4;
3877 	}
3878 	if (logged &&
3879 	    (rack->rc_gp_incr == 0)){
3880 		/* Go into increment mode */
3881 		rack->rc_gp_incr = 1;
3882 		rack->rc_gp_timely_inc_cnt = 0;
3883 	}
3884 	if (rack->rc_gp_incr &&
3885 	    logged &&
3886 	    (rack->rc_gp_timely_inc_cnt < RACK_TIMELY_CNT_BOOST)) {
3887 		rack->rc_gp_timely_inc_cnt++;
3888 	}
3889 	rack_log_timely(rack,  logged, plus, 0, 0,
3890 			__LINE__, 1);
3891 }
3892 
3893 static uint32_t
3894 rack_get_decrease(struct tcp_rack *rack, uint32_t curper, int32_t rtt_diff)
3895 {
3896 	/*-
3897 	 * norm_grad = rtt_diff / minrtt;
3898 	 * new_per = curper * (1 - B * norm_grad)
3899 	 *
3900 	 * B = rack_gp_decrease_per (default 80%)
3901 	 * rtt_dif = input var current rtt-diff
3902 	 * curper = input var current percentage
3903 	 * minrtt = from rack filter
3904 	 *
3905 	 * In order to do the floating point calculations above we
3906 	 * do an integer conversion. The code looks confusing so let me
3907 	 * translate it into something that use more variables and
3908 	 * is clearer for us humans :)
3909 	 *
3910 	 * uint64_t norm_grad, inverse, reduce_by, final_result;
3911 	 * uint32_t perf;
3912 	 *
3913 	 * norm_grad = (((uint64_t)rtt_diff * 1000000) /
3914 	 *             (uint64_t)get_filter_small(&rack->r_ctl.rc_gp_min_rtt));
3915 	 * inverse = ((uint64_t)rack_gp_decrease * (uint64_t)1000000) * norm_grad;
3916 	 * inverse /= 1000000;
3917 	 * reduce_by = (1000000 - inverse);
3918 	 * final_result = (cur_per * reduce_by) / 1000000;
3919 	 * perf = (uint32_t)final_result;
3920 	 */
3921 	uint64_t perf;
3922 
3923 	perf = (((uint64_t)curper * ((uint64_t)1000000 -
3924 		    ((uint64_t)rack_gp_decrease_per * (uint64_t)10000 *
3925 		     (((uint64_t)rtt_diff * (uint64_t)1000000)/
3926 		      (uint64_t)get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt)))/
3927 		     (uint64_t)1000000)) /
3928 		(uint64_t)1000000);
3929 	if (perf > curper) {
3930 		/* TSNH */
3931 		perf = curper - 1;
3932 	}
3933 	return ((uint32_t)perf);
3934 }
3935 
3936 static uint32_t
3937 rack_decrease_highrtt(struct tcp_rack *rack, uint32_t curper, uint32_t rtt)
3938 {
3939 	/*
3940 	 *                                   highrttthresh
3941 	 * result = curper * (1 - (B * ( 1 -  ------          ))
3942 	 *                                     gp_srtt
3943 	 *
3944 	 * B = rack_gp_decrease_per (default .8 i.e. 80)
3945 	 * highrttthresh = filter_min * rack_gp_rtt_maxmul
3946 	 */
3947 	uint64_t perf;
3948 	uint32_t highrttthresh;
3949 
3950 	highrttthresh = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
3951 
3952 	perf = (((uint64_t)curper * ((uint64_t)1000000 -
3953 				     ((uint64_t)rack_gp_decrease_per * ((uint64_t)1000000 -
3954 					((uint64_t)highrttthresh * (uint64_t)1000000) /
3955 						    (uint64_t)rtt)) / 100)) /(uint64_t)1000000);
3956 	if (tcp_bblogging_on(rack->rc_tp)) {
3957 		uint64_t log1;
3958 
3959 		log1 = rtt;
3960 		log1 <<= 32;
3961 		log1 |= highrttthresh;
3962 		rack_log_timely(rack,
3963 				rack_gp_decrease_per,
3964 				(uint64_t)curper,
3965 				log1,
3966 				perf,
3967 				__LINE__,
3968 				15);
3969 	}
3970 	return (perf);
3971 }
3972 
3973 static void
3974 rack_decrease_bw_mul(struct tcp_rack *rack, int timely_says, uint32_t rtt, int32_t rtt_diff)
3975 {
3976 	uint64_t logvar, logvar2, logvar3;
3977 	uint32_t logged, new_per, ss_red, ca_red, rec_red, alt, val;
3978 
3979 	if (rack->rc_gp_incr) {
3980 		/* Turn off increment counting */
3981 		rack->rc_gp_incr = 0;
3982 		rack->rc_gp_timely_inc_cnt = 0;
3983 	}
3984 	ss_red = ca_red = rec_red = 0;
3985 	logged = 0;
3986 	/* Calculate the reduction value */
3987 	if (rtt_diff < 0) {
3988 		rtt_diff *= -1;
3989 	}
3990 	/* Must be at least 1% reduction */
3991 	if (rack->rc_gp_saw_rec && (rack->rc_gp_no_rec_chg == 0)) {
3992 		/* We have been in recovery ding it too */
3993 		if (timely_says == 2) {
3994 			new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_rec, rtt);
3995 			alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
3996 			if (alt < new_per)
3997 				val = alt;
3998 			else
3999 				val = new_per;
4000 		} else
4001 			 val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
4002 		if (rack->r_ctl.rack_per_of_gp_rec > val) {
4003 			rec_red = (rack->r_ctl.rack_per_of_gp_rec - val);
4004 			rack->r_ctl.rack_per_of_gp_rec = (uint16_t)val;
4005 		} else {
4006 			rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound;
4007 			rec_red = 0;
4008 		}
4009 		if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_rec)
4010 			rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound;
4011 		logged |= 1;
4012 	}
4013 	if (rack->rc_gp_saw_ss) {
4014 		/* Sent in SS */
4015 		if (timely_says == 2) {
4016 			new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ss, rtt);
4017 			alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ss, rtt_diff);
4018 			if (alt < new_per)
4019 				val = alt;
4020 			else
4021 				val = new_per;
4022 		} else
4023 			val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ss, rtt_diff);
4024 		if (rack->r_ctl.rack_per_of_gp_ss > new_per) {
4025 			ss_red = rack->r_ctl.rack_per_of_gp_ss - val;
4026 			rack->r_ctl.rack_per_of_gp_ss = (uint16_t)val;
4027 		} else {
4028 			ss_red = new_per;
4029 			rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound;
4030 			logvar = new_per;
4031 			logvar <<= 32;
4032 			logvar |= alt;
4033 			logvar2 = (uint32_t)rtt;
4034 			logvar2 <<= 32;
4035 			logvar2 |= (uint32_t)rtt_diff;
4036 			logvar3 = rack_gp_rtt_maxmul;
4037 			logvar3 <<= 32;
4038 			logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
4039 			rack_log_timely(rack, timely_says,
4040 					logvar2, logvar3,
4041 					logvar, __LINE__, 10);
4042 		}
4043 		if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ss)
4044 			rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound;
4045 		logged |= 4;
4046 	} else if (rack->rc_gp_saw_ca) {
4047 		/* Sent in CA */
4048 		if (timely_says == 2) {
4049 			new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ca, rtt);
4050 			alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ca, rtt_diff);
4051 			if (alt < new_per)
4052 				val = alt;
4053 			else
4054 				val = new_per;
4055 		} else
4056 			val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ca, rtt_diff);
4057 		if (rack->r_ctl.rack_per_of_gp_ca > val) {
4058 			ca_red = rack->r_ctl.rack_per_of_gp_ca - val;
4059 			rack->r_ctl.rack_per_of_gp_ca = (uint16_t)val;
4060 		} else {
4061 			rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound;
4062 			ca_red = 0;
4063 			logvar = new_per;
4064 			logvar <<= 32;
4065 			logvar |= alt;
4066 			logvar2 = (uint32_t)rtt;
4067 			logvar2 <<= 32;
4068 			logvar2 |= (uint32_t)rtt_diff;
4069 			logvar3 = rack_gp_rtt_maxmul;
4070 			logvar3 <<= 32;
4071 			logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
4072 			rack_log_timely(rack, timely_says,
4073 					logvar2, logvar3,
4074 					logvar, __LINE__, 10);
4075 		}
4076 		if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ca)
4077 			rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound;
4078 		logged |= 2;
4079 	}
4080 	if (rack->rc_gp_timely_dec_cnt < 0x7) {
4081 		rack->rc_gp_timely_dec_cnt++;
4082 		if (rack_timely_dec_clear &&
4083 		    (rack->rc_gp_timely_dec_cnt == rack_timely_dec_clear))
4084 			rack->rc_gp_timely_dec_cnt = 0;
4085 	}
4086 	logvar = ss_red;
4087 	logvar <<= 32;
4088 	logvar |= ca_red;
4089 	rack_log_timely(rack,  logged, rec_red, rack_per_lower_bound, logvar,
4090 			__LINE__, 2);
4091 }
4092 
4093 static void
4094 rack_log_rtt_shrinks(struct tcp_rack *rack, uint32_t us_cts,
4095 		     uint32_t rtt, uint32_t line, uint8_t reas)
4096 {
4097 	if (tcp_bblogging_on(rack->rc_tp)) {
4098 		union tcp_log_stackspecific log;
4099 		struct timeval tv;
4100 
4101 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
4102 		log.u_bbr.flex1 = line;
4103 		log.u_bbr.flex2 = rack->r_ctl.rc_time_probertt_starts;
4104 		log.u_bbr.flex3 = rack->r_ctl.rc_lower_rtt_us_cts;
4105 		log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss;
4106 		log.u_bbr.flex5 = rtt;
4107 		log.u_bbr.flex6 = rack->rc_highly_buffered;
4108 		log.u_bbr.flex6 <<= 1;
4109 		log.u_bbr.flex6 |= rack->forced_ack;
4110 		log.u_bbr.flex6 <<= 1;
4111 		log.u_bbr.flex6 |= rack->rc_gp_dyn_mul;
4112 		log.u_bbr.flex6 <<= 1;
4113 		log.u_bbr.flex6 |= rack->in_probe_rtt;
4114 		log.u_bbr.flex6 <<= 1;
4115 		log.u_bbr.flex6 |= rack->measure_saw_probe_rtt;
4116 		log.u_bbr.flex7 = rack->r_ctl.rack_per_of_gp_probertt;
4117 		log.u_bbr.pacing_gain = rack->r_ctl.rack_per_of_gp_ca;
4118 		log.u_bbr.cwnd_gain = rack->r_ctl.rack_per_of_gp_rec;
4119 		log.u_bbr.flex8 = reas;
4120 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
4121 		log.u_bbr.delRate = rack_get_bw(rack);
4122 		log.u_bbr.cur_del_rate = rack->r_ctl.rc_highest_us_rtt;
4123 		log.u_bbr.cur_del_rate <<= 32;
4124 		log.u_bbr.cur_del_rate |= rack->r_ctl.rc_lowest_us_rtt;
4125 		log.u_bbr.applimited = rack->r_ctl.rc_time_probertt_entered;
4126 		log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff;
4127 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
4128 		log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt;
4129 		log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt;
4130 		log.u_bbr.pkt_epoch = rack->r_ctl.rc_lower_rtt_us_cts;
4131 		log.u_bbr.delivered = rack->r_ctl.rc_target_probertt_flight;
4132 		log.u_bbr.lost = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
4133 		log.u_bbr.rttProp = us_cts;
4134 		log.u_bbr.rttProp <<= 32;
4135 		log.u_bbr.rttProp |= rack->r_ctl.rc_entry_gp_rtt;
4136 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
4137 		    &rack->rc_inp->inp_socket->so_rcv,
4138 		    &rack->rc_inp->inp_socket->so_snd,
4139 		    BBR_LOG_RTT_SHRINKS, 0,
4140 		    0, &log, false, &rack->r_ctl.act_rcv_time);
4141 	}
4142 }
4143 
4144 static void
4145 rack_set_prtt_target(struct tcp_rack *rack, uint32_t segsiz, uint32_t rtt)
4146 {
4147 	uint64_t bwdp;
4148 
4149 	bwdp = rack_get_bw(rack);
4150 	bwdp *= (uint64_t)rtt;
4151 	bwdp /= (uint64_t)HPTS_USEC_IN_SEC;
4152 	rack->r_ctl.rc_target_probertt_flight = roundup((uint32_t)bwdp, segsiz);
4153 	if (rack->r_ctl.rc_target_probertt_flight < (segsiz * rack_timely_min_segs)) {
4154 		/*
4155 		 * A window protocol must be able to have 4 packets
4156 		 * outstanding as the floor in order to function
4157 		 * (especially considering delayed ack :D).
4158 		 */
4159 		rack->r_ctl.rc_target_probertt_flight = (segsiz * rack_timely_min_segs);
4160 	}
4161 }
4162 
4163 static void
4164 rack_enter_probertt(struct tcp_rack *rack, uint32_t us_cts)
4165 {
4166 	/**
4167 	 * ProbeRTT is a bit different in rack_pacing than in
4168 	 * BBR. It is like BBR in that it uses the lowering of
4169 	 * the RTT as a signal that we saw something new and
4170 	 * counts from there for how long between. But it is
4171 	 * different in that its quite simple. It does not
4172 	 * play with the cwnd and wait until we get down
4173 	 * to N segments outstanding and hold that for
4174 	 * 200ms. Instead it just sets the pacing reduction
4175 	 * rate to a set percentage (70 by default) and hold
4176 	 * that for a number of recent GP Srtt's.
4177 	 */
4178 	uint32_t segsiz;
4179 
4180 	if (rack->rc_gp_dyn_mul == 0)
4181 		return;
4182 
4183 	if (rack->rc_tp->snd_max == rack->rc_tp->snd_una) {
4184 		/* We are idle */
4185 		return;
4186 	}
4187 	if ((rack->rc_tp->t_flags & TF_GPUTINPROG) &&
4188 	    SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) {
4189 		/*
4190 		 * Stop the goodput now, the idea here is
4191 		 * that future measurements with in_probe_rtt
4192 		 * won't register if they are not greater so
4193 		 * we want to get what info (if any) is available
4194 		 * now.
4195 		 */
4196 		rack_do_goodput_measurement(rack->rc_tp, rack,
4197 					    rack->rc_tp->snd_una, __LINE__,
4198 					    RACK_QUALITY_PROBERTT);
4199 	}
4200 	rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
4201 	rack->r_ctl.rc_time_probertt_entered = us_cts;
4202 	segsiz = min(ctf_fixed_maxseg(rack->rc_tp),
4203 		     rack->r_ctl.rc_pace_min_segs);
4204 	rack->in_probe_rtt = 1;
4205 	rack->measure_saw_probe_rtt = 1;
4206 	rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
4207 	rack->r_ctl.rc_time_probertt_starts = 0;
4208 	rack->r_ctl.rc_entry_gp_rtt = rack->r_ctl.rc_gp_srtt;
4209 	if (rack_probertt_use_min_rtt_entry)
4210 		rack_set_prtt_target(rack, segsiz, get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt));
4211 	else
4212 		rack_set_prtt_target(rack, segsiz, rack->r_ctl.rc_gp_srtt);
4213 	rack_log_rtt_shrinks(rack,  us_cts,  get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4214 			     __LINE__, RACK_RTTS_ENTERPROBE);
4215 }
4216 
4217 static void
4218 rack_exit_probertt(struct tcp_rack *rack, uint32_t us_cts)
4219 {
4220 	struct rack_sendmap *rsm;
4221 	uint32_t segsiz;
4222 
4223 	segsiz = min(ctf_fixed_maxseg(rack->rc_tp),
4224 		     rack->r_ctl.rc_pace_min_segs);
4225 	rack->in_probe_rtt = 0;
4226 	if ((rack->rc_tp->t_flags & TF_GPUTINPROG) &&
4227 	    SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) {
4228 		/*
4229 		 * Stop the goodput now, the idea here is
4230 		 * that future measurements with in_probe_rtt
4231 		 * won't register if they are not greater so
4232 		 * we want to get what info (if any) is available
4233 		 * now.
4234 		 */
4235 		rack_do_goodput_measurement(rack->rc_tp, rack,
4236 					    rack->rc_tp->snd_una, __LINE__,
4237 					    RACK_QUALITY_PROBERTT);
4238 	} else if (rack->rc_tp->t_flags & TF_GPUTINPROG) {
4239 		/*
4240 		 * We don't have enough data to make a measurement.
4241 		 * So lets just stop and start here after exiting
4242 		 * probe-rtt. We probably are not interested in
4243 		 * the results anyway.
4244 		 */
4245 		rack->rc_tp->t_flags &= ~TF_GPUTINPROG;
4246 	}
4247 	/*
4248 	 * Measurements through the current snd_max are going
4249 	 * to be limited by the slower pacing rate.
4250 	 *
4251 	 * We need to mark these as app-limited so we
4252 	 * don't collapse the b/w.
4253 	 */
4254 	rsm = tqhash_max(rack->r_ctl.tqh);
4255 	if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) {
4256 		if (rack->r_ctl.rc_app_limited_cnt == 0)
4257 			rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm;
4258 		else {
4259 			/*
4260 			 * Go out to the end app limited and mark
4261 			 * this new one as next and move the end_appl up
4262 			 * to this guy.
4263 			 */
4264 			if (rack->r_ctl.rc_end_appl)
4265 				rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start;
4266 			rack->r_ctl.rc_end_appl = rsm;
4267 		}
4268 		rsm->r_flags |= RACK_APP_LIMITED;
4269 		rack->r_ctl.rc_app_limited_cnt++;
4270 	}
4271 	/*
4272 	 * Now, we need to examine our pacing rate multipliers.
4273 	 * If its under 100%, we need to kick it back up to
4274 	 * 100%. We also don't let it be over our "max" above
4275 	 * the actual rate i.e. 100% + rack_clamp_atexit_prtt.
4276 	 * Note setting clamp_atexit_prtt to 0 has the effect
4277 	 * of setting CA/SS to 100% always at exit (which is
4278 	 * the default behavior).
4279 	 */
4280 	if (rack_probertt_clear_is) {
4281 		rack->rc_gp_incr = 0;
4282 		rack->rc_gp_bwred = 0;
4283 		rack->rc_gp_timely_inc_cnt = 0;
4284 		rack->rc_gp_timely_dec_cnt = 0;
4285 	}
4286 	/* Do we do any clamping at exit? */
4287 	if (rack->rc_highly_buffered && rack_atexit_prtt_hbp) {
4288 		rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt_hbp;
4289 		rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt_hbp;
4290 	}
4291 	if ((rack->rc_highly_buffered == 0) && rack_atexit_prtt) {
4292 		rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt;
4293 		rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt;
4294 	}
4295 	/*
4296 	 * Lets set rtt_diff to 0, so that we will get a "boost"
4297 	 * after exiting.
4298 	 */
4299 	rack->r_ctl.rc_rtt_diff = 0;
4300 
4301 	/* Clear all flags so we start fresh */
4302 	rack->rc_tp->t_bytes_acked = 0;
4303 	rack->rc_tp->t_ccv.flags &= ~CCF_ABC_SENTAWND;
4304 	/*
4305 	 * If configured to, set the cwnd and ssthresh to
4306 	 * our targets.
4307 	 */
4308 	if (rack_probe_rtt_sets_cwnd) {
4309 		uint64_t ebdp;
4310 		uint32_t setto;
4311 
4312 		/* Set ssthresh so we get into CA once we hit our target */
4313 		if (rack_probertt_use_min_rtt_exit == 1) {
4314 			/* Set to min rtt */
4315 			rack_set_prtt_target(rack, segsiz,
4316 					     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt));
4317 		} else if (rack_probertt_use_min_rtt_exit == 2) {
4318 			/* Set to current gp rtt */
4319 			rack_set_prtt_target(rack, segsiz,
4320 					     rack->r_ctl.rc_gp_srtt);
4321 		} else if (rack_probertt_use_min_rtt_exit == 3) {
4322 			/* Set to entry gp rtt */
4323 			rack_set_prtt_target(rack, segsiz,
4324 					     rack->r_ctl.rc_entry_gp_rtt);
4325 		} else {
4326 			uint64_t sum;
4327 			uint32_t setval;
4328 
4329 			sum = rack->r_ctl.rc_entry_gp_rtt;
4330 			sum *= 10;
4331 			sum /= (uint64_t)(max(1, rack->r_ctl.rc_gp_srtt));
4332 			if (sum >= 20) {
4333 				/*
4334 				 * A highly buffered path needs
4335 				 * cwnd space for timely to work.
4336 				 * Lets set things up as if
4337 				 * we are heading back here again.
4338 				 */
4339 				setval = rack->r_ctl.rc_entry_gp_rtt;
4340 			} else if (sum >= 15) {
4341 				/*
4342 				 * Lets take the smaller of the
4343 				 * two since we are just somewhat
4344 				 * buffered.
4345 				 */
4346 				setval = rack->r_ctl.rc_gp_srtt;
4347 				if (setval > rack->r_ctl.rc_entry_gp_rtt)
4348 					setval = rack->r_ctl.rc_entry_gp_rtt;
4349 			} else {
4350 				/*
4351 				 * Here we are not highly buffered
4352 				 * and should pick the min we can to
4353 				 * keep from causing loss.
4354 				 */
4355 				setval = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
4356 			}
4357 			rack_set_prtt_target(rack, segsiz,
4358 					     setval);
4359 		}
4360 		if (rack_probe_rtt_sets_cwnd > 1) {
4361 			/* There is a percentage here to boost */
4362 			ebdp = rack->r_ctl.rc_target_probertt_flight;
4363 			ebdp *= rack_probe_rtt_sets_cwnd;
4364 			ebdp /= 100;
4365 			setto = rack->r_ctl.rc_target_probertt_flight + ebdp;
4366 		} else
4367 			setto = rack->r_ctl.rc_target_probertt_flight;
4368 		rack->rc_tp->snd_cwnd = roundup(setto, segsiz);
4369 		if (rack->rc_tp->snd_cwnd < (segsiz * rack_timely_min_segs)) {
4370 			/* Enforce a min */
4371 			rack->rc_tp->snd_cwnd = segsiz * rack_timely_min_segs;
4372 		}
4373 		/* If we set in the cwnd also set the ssthresh point so we are in CA */
4374 		rack->rc_tp->snd_ssthresh = (rack->rc_tp->snd_cwnd - 1);
4375 	}
4376 	rack_log_rtt_shrinks(rack,  us_cts,
4377 			     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4378 			     __LINE__, RACK_RTTS_EXITPROBE);
4379 	/* Clear times last so log has all the info */
4380 	rack->r_ctl.rc_probertt_sndmax_atexit = rack->rc_tp->snd_max;
4381 	rack->r_ctl.rc_time_probertt_entered = us_cts;
4382 	rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
4383 	rack->r_ctl.rc_time_of_last_probertt = us_cts;
4384 }
4385 
4386 static void
4387 rack_check_probe_rtt(struct tcp_rack *rack, uint32_t us_cts)
4388 {
4389 	/* Check in on probe-rtt */
4390 	if (rack->rc_gp_filled == 0) {
4391 		/* We do not do p-rtt unless we have gp measurements */
4392 		return;
4393 	}
4394 	if (rack->in_probe_rtt) {
4395 		uint64_t no_overflow;
4396 		uint32_t endtime, must_stay;
4397 
4398 		if (rack->r_ctl.rc_went_idle_time &&
4399 		    ((us_cts - rack->r_ctl.rc_went_idle_time) > rack_min_probertt_hold)) {
4400 			/*
4401 			 * We went idle during prtt, just exit now.
4402 			 */
4403 			rack_exit_probertt(rack, us_cts);
4404 		} else if (rack_probe_rtt_safety_val &&
4405 		    TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered) &&
4406 		    ((us_cts - rack->r_ctl.rc_time_probertt_entered) > rack_probe_rtt_safety_val)) {
4407 			/*
4408 			 * Probe RTT safety value triggered!
4409 			 */
4410 			rack_log_rtt_shrinks(rack,  us_cts,
4411 					     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4412 					     __LINE__, RACK_RTTS_SAFETY);
4413 			rack_exit_probertt(rack, us_cts);
4414 		}
4415 		/* Calculate the max we will wait */
4416 		endtime = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_max_drain_wait);
4417 		if (rack->rc_highly_buffered)
4418 			endtime += (rack->r_ctl.rc_gp_srtt * rack_max_drain_hbp);
4419 		/* Calculate the min we must wait */
4420 		must_stay = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_must_drain);
4421 		if ((ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.rc_target_probertt_flight) &&
4422 		    TSTMP_LT(us_cts, endtime)) {
4423 			uint32_t calc;
4424 			/* Do we lower more? */
4425 no_exit:
4426 			if (TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered))
4427 				calc = us_cts - rack->r_ctl.rc_time_probertt_entered;
4428 			else
4429 				calc = 0;
4430 			calc /= max(rack->r_ctl.rc_gp_srtt, 1);
4431 			if (calc) {
4432 				/* Maybe */
4433 				calc *= rack_per_of_gp_probertt_reduce;
4434 				rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt - calc;
4435 				/* Limit it too */
4436 				if (rack->r_ctl.rack_per_of_gp_probertt < rack_per_of_gp_lowthresh)
4437 					rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_lowthresh;
4438 			}
4439 			/* We must reach target or the time set */
4440 			return;
4441 		}
4442 		if (rack->r_ctl.rc_time_probertt_starts == 0) {
4443 			if ((TSTMP_LT(us_cts, must_stay) &&
4444 			     rack->rc_highly_buffered) ||
4445 			     (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) >
4446 			      rack->r_ctl.rc_target_probertt_flight)) {
4447 				/* We are not past the must_stay time */
4448 				goto no_exit;
4449 			}
4450 			rack_log_rtt_shrinks(rack,  us_cts,
4451 					     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4452 					     __LINE__, RACK_RTTS_REACHTARGET);
4453 			rack->r_ctl.rc_time_probertt_starts = us_cts;
4454 			if (rack->r_ctl.rc_time_probertt_starts == 0)
4455 				rack->r_ctl.rc_time_probertt_starts = 1;
4456 			/* Restore back to our rate we want to pace at in prtt */
4457 			rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
4458 		}
4459 		/*
4460 		 * Setup our end time, some number of gp_srtts plus 200ms.
4461 		 */
4462 		no_overflow = ((uint64_t)rack->r_ctl.rc_gp_srtt *
4463 			       (uint64_t)rack_probertt_gpsrtt_cnt_mul);
4464 		if (rack_probertt_gpsrtt_cnt_div)
4465 			endtime = (uint32_t)(no_overflow / (uint64_t)rack_probertt_gpsrtt_cnt_div);
4466 		else
4467 			endtime = 0;
4468 		endtime += rack_min_probertt_hold;
4469 		endtime += rack->r_ctl.rc_time_probertt_starts;
4470 		if (TSTMP_GEQ(us_cts,  endtime)) {
4471 			/* yes, exit probertt */
4472 			rack_exit_probertt(rack, us_cts);
4473 		}
4474 
4475 	} else if ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= rack_time_between_probertt) {
4476 		/* Go into probertt, its been too long since we went lower */
4477 		rack_enter_probertt(rack, us_cts);
4478 	}
4479 }
4480 
4481 static void
4482 rack_update_multiplier(struct tcp_rack *rack, int32_t timely_says, uint64_t last_bw_est,
4483 		       uint32_t rtt, int32_t rtt_diff)
4484 {
4485 	uint64_t cur_bw, up_bnd, low_bnd, subfr;
4486 	uint32_t losses;
4487 
4488 	if ((rack->rc_gp_dyn_mul == 0) ||
4489 	    (rack->use_fixed_rate) ||
4490 	    (rack->in_probe_rtt) ||
4491 	    (rack->rc_always_pace == 0)) {
4492 		/* No dynamic GP multiplier in play */
4493 		return;
4494 	}
4495 	losses = rack->r_ctl.rc_loss_count - rack->r_ctl.rc_loss_at_start;
4496 	cur_bw = rack_get_bw(rack);
4497 	/* Calculate our up and down range */
4498 	up_bnd = rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_up;
4499 	up_bnd /= 100;
4500 	up_bnd += rack->r_ctl.last_gp_comp_bw;
4501 
4502 	subfr = (uint64_t)rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_down;
4503 	subfr /= 100;
4504 	low_bnd = rack->r_ctl.last_gp_comp_bw - subfr;
4505 	if ((timely_says == 2) && (rack->r_ctl.rc_no_push_at_mrtt)) {
4506 		/*
4507 		 * This is the case where our RTT is above
4508 		 * the max target and we have been configured
4509 		 * to just do timely no bonus up stuff in that case.
4510 		 *
4511 		 * There are two configurations, set to 1, and we
4512 		 * just do timely if we are over our max. If its
4513 		 * set above 1 then we slam the multipliers down
4514 		 * to 100 and then decrement per timely.
4515 		 */
4516 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4517 				__LINE__, 3);
4518 		if (rack->r_ctl.rc_no_push_at_mrtt > 1)
4519 			rack_validate_multipliers_at_or_below_100(rack);
4520 		rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff);
4521 	} else if ((timely_says != 0) && (last_bw_est < low_bnd) && !losses) {
4522 		/*
4523 		 * We are decreasing this is a bit complicated this
4524 		 * means we are loosing ground. This could be
4525 		 * because another flow entered and we are competing
4526 		 * for b/w with it. This will push the RTT up which
4527 		 * makes timely unusable unless we want to get shoved
4528 		 * into a corner and just be backed off (the age
4529 		 * old problem with delay based CC).
4530 		 *
4531 		 * On the other hand if it was a route change we
4532 		 * would like to stay somewhat contained and not
4533 		 * blow out the buffers.
4534 		 */
4535 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4536 				__LINE__, 3);
4537 		rack->r_ctl.last_gp_comp_bw = cur_bw;
4538 		if (rack->rc_gp_bwred == 0) {
4539 			/* Go into reduction counting */
4540 			rack->rc_gp_bwred = 1;
4541 			rack->rc_gp_timely_dec_cnt = 0;
4542 		}
4543 		if (rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) {
4544 			/*
4545 			 * Push another time with a faster pacing
4546 			 * to try to gain back (we include override to
4547 			 * get a full raise factor).
4548 			 */
4549 			if ((rack->rc_gp_saw_ca && rack->r_ctl.rack_per_of_gp_ca <= rack_down_raise_thresh) ||
4550 			    (rack->rc_gp_saw_ss && rack->r_ctl.rack_per_of_gp_ss <= rack_down_raise_thresh) ||
4551 			    (timely_says == 0) ||
4552 			    (rack_down_raise_thresh == 0)) {
4553 				/*
4554 				 * Do an override up in b/w if we were
4555 				 * below the threshold or if the threshold
4556 				 * is zero we always do the raise.
4557 				 */
4558 				rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 1);
4559 			} else {
4560 				/* Log it stays the same */
4561 				rack_log_timely(rack,  0, last_bw_est, low_bnd, 0,
4562 						__LINE__, 11);
4563 			}
4564 			rack->rc_gp_timely_dec_cnt++;
4565 			/* We are not incrementing really no-count */
4566 			rack->rc_gp_incr = 0;
4567 			rack->rc_gp_timely_inc_cnt = 0;
4568 		} else {
4569 			/*
4570 			 * Lets just use the RTT
4571 			 * information and give up
4572 			 * pushing.
4573 			 */
4574 			goto use_timely;
4575 		}
4576 	} else if ((timely_says != 2) &&
4577 		    !losses &&
4578 		    (last_bw_est > up_bnd)) {
4579 		/*
4580 		 * We are increasing b/w lets keep going, updating
4581 		 * our b/w and ignoring any timely input, unless
4582 		 * of course we are at our max raise (if there is one).
4583 		 */
4584 
4585 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4586 				__LINE__, 3);
4587 		rack->r_ctl.last_gp_comp_bw = cur_bw;
4588 		if (rack->rc_gp_saw_ss &&
4589 		    rack->r_ctl.rack_per_upper_bound_ss &&
4590 		     (rack->r_ctl.rack_per_of_gp_ss == rack->r_ctl.rack_per_upper_bound_ss)) {
4591 			    /*
4592 			     * In cases where we can't go higher
4593 			     * we should just use timely.
4594 			     */
4595 			    goto use_timely;
4596 		}
4597 		if (rack->rc_gp_saw_ca &&
4598 		    rack->r_ctl.rack_per_upper_bound_ca &&
4599 		    (rack->r_ctl.rack_per_of_gp_ca == rack->r_ctl.rack_per_upper_bound_ca)) {
4600 			    /*
4601 			     * In cases where we can't go higher
4602 			     * we should just use timely.
4603 			     */
4604 			    goto use_timely;
4605 		}
4606 		rack->rc_gp_bwred = 0;
4607 		rack->rc_gp_timely_dec_cnt = 0;
4608 		/* You get a set number of pushes if timely is trying to reduce */
4609 		if ((rack->rc_gp_incr < rack_timely_max_push_rise) || (timely_says == 0)) {
4610 			rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4611 		} else {
4612 			/* Log it stays the same */
4613 			rack_log_timely(rack,  0, last_bw_est, up_bnd, 0,
4614 			    __LINE__, 12);
4615 		}
4616 		return;
4617 	} else {
4618 		/*
4619 		 * We are staying between the lower and upper range bounds
4620 		 * so use timely to decide.
4621 		 */
4622 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4623 				__LINE__, 3);
4624 use_timely:
4625 		if (timely_says) {
4626 			rack->rc_gp_incr = 0;
4627 			rack->rc_gp_timely_inc_cnt = 0;
4628 			if ((rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) &&
4629 			    !losses &&
4630 			    (last_bw_est < low_bnd)) {
4631 				/* We are loosing ground */
4632 				rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4633 				rack->rc_gp_timely_dec_cnt++;
4634 				/* We are not incrementing really no-count */
4635 				rack->rc_gp_incr = 0;
4636 				rack->rc_gp_timely_inc_cnt = 0;
4637 			} else
4638 				rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff);
4639 		} else {
4640 			rack->rc_gp_bwred = 0;
4641 			rack->rc_gp_timely_dec_cnt = 0;
4642 			rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4643 		}
4644 	}
4645 }
4646 
4647 static int32_t
4648 rack_make_timely_judgement(struct tcp_rack *rack, uint32_t rtt, int32_t rtt_diff, uint32_t prev_rtt)
4649 {
4650 	int32_t timely_says;
4651 	uint64_t log_mult, log_rtt_a_diff;
4652 
4653 	log_rtt_a_diff = rtt;
4654 	log_rtt_a_diff <<= 32;
4655 	log_rtt_a_diff |= (uint32_t)rtt_diff;
4656 	if (rtt >= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) *
4657 		    rack_gp_rtt_maxmul)) {
4658 		/* Reduce the b/w multiplier */
4659 		timely_says = 2;
4660 		log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
4661 		log_mult <<= 32;
4662 		log_mult |= prev_rtt;
4663 		rack_log_timely(rack,  timely_says, log_mult,
4664 				get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4665 				log_rtt_a_diff, __LINE__, 4);
4666 	} else if (rtt <= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) +
4667 			   ((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) /
4668 			    max(rack_gp_rtt_mindiv , 1)))) {
4669 		/* Increase the b/w multiplier */
4670 		log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) +
4671 			((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) /
4672 			 max(rack_gp_rtt_mindiv , 1));
4673 		log_mult <<= 32;
4674 		log_mult |= prev_rtt;
4675 		timely_says = 0;
4676 		rack_log_timely(rack,  timely_says, log_mult ,
4677 				get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4678 				log_rtt_a_diff, __LINE__, 5);
4679 	} else {
4680 		/*
4681 		 * Use a gradient to find it the timely gradient
4682 		 * is:
4683 		 * grad = rc_rtt_diff / min_rtt;
4684 		 *
4685 		 * anything below or equal to 0 will be
4686 		 * a increase indication. Anything above
4687 		 * zero is a decrease. Note we take care
4688 		 * of the actual gradient calculation
4689 		 * in the reduction (its not needed for
4690 		 * increase).
4691 		 */
4692 		log_mult = prev_rtt;
4693 		if (rtt_diff <= 0) {
4694 			/*
4695 			 * Rttdiff is less than zero, increase the
4696 			 * b/w multiplier (its 0 or negative)
4697 			 */
4698 			timely_says = 0;
4699 			rack_log_timely(rack,  timely_says, log_mult,
4700 					get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 6);
4701 		} else {
4702 			/* Reduce the b/w multiplier */
4703 			timely_says = 1;
4704 			rack_log_timely(rack,  timely_says, log_mult,
4705 					get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 7);
4706 		}
4707 	}
4708 	return (timely_says);
4709 }
4710 
4711 static __inline int
4712 rack_in_gp_window(struct tcpcb *tp, struct rack_sendmap *rsm)
4713 {
4714 	if (SEQ_GEQ(rsm->r_start, tp->gput_seq) &&
4715 	    SEQ_LEQ(rsm->r_end, tp->gput_ack)) {
4716 		/**
4717 		 * This covers the case that the
4718 		 * resent is completely inside
4719 		 * the gp range or up to it.
4720 		 *      |----------------|
4721 		 *      |-----| <or>
4722 		 *            |----|
4723 		 *            <or>   |---|
4724 		 */
4725 		return (1);
4726 	} else if (SEQ_LT(rsm->r_start, tp->gput_seq) &&
4727 		   SEQ_GT(rsm->r_end, tp->gput_seq)){
4728 		/**
4729 		 * This covers the case of
4730 		 *      |--------------|
4731 		 *  |-------->|
4732 		 */
4733 		return (1);
4734 	} else if (SEQ_GEQ(rsm->r_start, tp->gput_seq) &&
4735 		   SEQ_LT(rsm->r_start, tp->gput_ack) &&
4736 		   SEQ_GEQ(rsm->r_end, tp->gput_ack)) {
4737 
4738 		/**
4739 		 * This covers the case of
4740 		 *      |--------------|
4741 		 *              |-------->|
4742 		 */
4743 		return (1);
4744 	}
4745 	return (0);
4746 }
4747 
4748 static __inline void
4749 rack_mark_in_gp_win(struct tcpcb *tp, struct rack_sendmap *rsm)
4750 {
4751 
4752 	if ((tp->t_flags & TF_GPUTINPROG) == 0)
4753 		return;
4754 	/*
4755 	 * We have a Goodput measurement in progress. Mark
4756 	 * the send if its within the window. If its not
4757 	 * in the window make sure it does not have the mark.
4758 	 */
4759 	if (rack_in_gp_window(tp, rsm))
4760 		rsm->r_flags |= RACK_IN_GP_WIN;
4761 	else
4762 		rsm->r_flags &= ~RACK_IN_GP_WIN;
4763 }
4764 
4765 static __inline void
4766 rack_clear_gp_marks(struct tcpcb *tp, struct tcp_rack *rack)
4767 {
4768 	/* A GP measurement is ending, clear all marks on the send map*/
4769 	struct rack_sendmap *rsm = NULL;
4770 
4771 	rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
4772 	if (rsm == NULL) {
4773 		rsm = tqhash_min(rack->r_ctl.tqh);
4774 	}
4775 	/* Nothing left? */
4776 	while ((rsm != NULL) && (SEQ_GEQ(tp->gput_ack, rsm->r_start))){
4777 		rsm->r_flags &= ~RACK_IN_GP_WIN;
4778 		rsm = tqhash_next(rack->r_ctl.tqh, rsm);
4779 	}
4780 }
4781 
4782 
4783 static __inline void
4784 rack_tend_gp_marks(struct tcpcb *tp, struct tcp_rack *rack)
4785 {
4786 	struct rack_sendmap *rsm = NULL;
4787 
4788 	if (tp->snd_una == tp->snd_max) {
4789 		/* Nothing outstanding yet, nothing to do here */
4790 		return;
4791 	}
4792 	if (SEQ_GT(tp->gput_seq, tp->snd_una)) {
4793 		/*
4794 		 * We are measuring ahead of some outstanding
4795 		 * data. We need to walk through up until we get
4796 		 * to gp_seq marking so that no rsm is set incorrectly
4797 		 * with RACK_IN_GP_WIN.
4798 		 */
4799 		rsm = tqhash_min(rack->r_ctl.tqh);
4800 		while (rsm != NULL) {
4801 			rack_mark_in_gp_win(tp, rsm);
4802 			if (SEQ_GEQ(rsm->r_end, tp->gput_seq))
4803 				break;
4804 			rsm = tqhash_next(rack->r_ctl.tqh, rsm);
4805 		}
4806 	}
4807 	if (rsm == NULL) {
4808 		/*
4809 		 * Need to find the GP seq, if rsm is
4810 		 * set we stopped as we hit it.
4811 		 */
4812 		rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
4813 		if (rsm == NULL)
4814 			return;
4815 		rack_mark_in_gp_win(tp, rsm);
4816 	}
4817 	/*
4818 	 * Now we may need to mark already sent rsm, ahead of
4819 	 * gput_seq in the window since they may have been sent
4820 	 * *before* we started our measurment. The rsm, if non-null
4821 	 * has been marked (note if rsm would have been NULL we would have
4822 	 * returned in the previous block). So we go to the next, and continue
4823 	 * until we run out of entries or we exceed the gp_ack value.
4824 	 */
4825 	rsm = tqhash_next(rack->r_ctl.tqh, rsm);
4826 	while (rsm) {
4827 		rack_mark_in_gp_win(tp, rsm);
4828 		if (SEQ_GT(rsm->r_end, tp->gput_ack))
4829 			break;
4830 		rsm = tqhash_next(rack->r_ctl.tqh, rsm);
4831 	}
4832 }
4833 
4834 static void
4835 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
4836 			    tcp_seq th_ack, int line, uint8_t quality)
4837 {
4838 	uint64_t tim, bytes_ps, stim, utim;
4839 	uint32_t segsiz, bytes, reqbytes, us_cts;
4840 	int32_t gput, new_rtt_diff, timely_says;
4841 	uint64_t  resid_bw, subpart = 0, addpart = 0, srtt;
4842 	int did_add = 0;
4843 
4844 	us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
4845 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
4846 	if (TSTMP_GEQ(us_cts, tp->gput_ts))
4847 		tim = us_cts - tp->gput_ts;
4848 	else
4849 		tim = 0;
4850 	if (rack->r_ctl.rc_gp_cumack_ts > rack->r_ctl.rc_gp_output_ts)
4851 		stim = rack->r_ctl.rc_gp_cumack_ts - rack->r_ctl.rc_gp_output_ts;
4852 	else
4853 		stim = 0;
4854 	/*
4855 	 * Use the larger of the send time or ack time. This prevents us
4856 	 * from being influenced by ack artifacts to come up with too
4857 	 * high of measurement. Note that since we are spanning over many more
4858 	 * bytes in most of our measurements hopefully that is less likely to
4859 	 * occur.
4860 	 */
4861 	if (tim > stim)
4862 		utim = max(tim, 1);
4863 	else
4864 		utim = max(stim, 1);
4865 	reqbytes = min(rc_init_window(rack), (MIN_GP_WIN * segsiz));
4866 	rack_log_gpset(rack, th_ack, us_cts, rack->r_ctl.rc_gp_cumack_ts, __LINE__, 3, NULL);
4867 	if ((tim == 0) && (stim == 0)) {
4868 		/*
4869 		 * Invalid measurement time, maybe
4870 		 * all on one ack/one send?
4871 		 */
4872 		bytes = 0;
4873 		bytes_ps = 0;
4874 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4875 					   0, 0, 0, 10, __LINE__, NULL, quality);
4876 		goto skip_measurement;
4877 	}
4878 	if (rack->r_ctl.rc_gp_lowrtt == 0xffffffff) {
4879 		/* We never made a us_rtt measurement? */
4880 		bytes = 0;
4881 		bytes_ps = 0;
4882 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4883 					   0, 0, 0, 10, __LINE__, NULL, quality);
4884 		goto skip_measurement;
4885 	}
4886 	/*
4887 	 * Calculate the maximum possible b/w this connection
4888 	 * could have. We base our calculation on the lowest
4889 	 * rtt we have seen during the measurement and the
4890 	 * largest rwnd the client has given us in that time. This
4891 	 * forms a BDP that is the maximum that we could ever
4892 	 * get to the client. Anything larger is not valid.
4893 	 *
4894 	 * I originally had code here that rejected measurements
4895 	 * where the time was less than 1/2 the latest us_rtt.
4896 	 * But after thinking on that I realized its wrong since
4897 	 * say you had a 150Mbps or even 1Gbps link, and you
4898 	 * were a long way away.. example I am in Europe (100ms rtt)
4899 	 * talking to my 1Gbps link in S.C. Now measuring say 150,000
4900 	 * bytes my time would be 1.2ms, and yet my rtt would say
4901 	 * the measurement was invalid the time was < 50ms. The
4902 	 * same thing is true for 150Mb (8ms of time).
4903 	 *
4904 	 * A better way I realized is to look at what the maximum
4905 	 * the connection could possibly do. This is gated on
4906 	 * the lowest RTT we have seen and the highest rwnd.
4907 	 * We should in theory never exceed that, if we are
4908 	 * then something on the path is storing up packets
4909 	 * and then feeding them all at once to our endpoint
4910 	 * messing up our measurement.
4911 	 */
4912 	rack->r_ctl.last_max_bw = rack->r_ctl.rc_gp_high_rwnd;
4913 	rack->r_ctl.last_max_bw *= HPTS_USEC_IN_SEC;
4914 	rack->r_ctl.last_max_bw /= rack->r_ctl.rc_gp_lowrtt;
4915 	if (SEQ_LT(th_ack, tp->gput_seq)) {
4916 		/* No measurement can be made */
4917 		bytes = 0;
4918 		bytes_ps = 0;
4919 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4920 					   0, 0, 0, 10, __LINE__, NULL, quality);
4921 		goto skip_measurement;
4922 	} else
4923 		bytes = (th_ack - tp->gput_seq);
4924 	bytes_ps = (uint64_t)bytes;
4925 	/*
4926 	 * Don't measure a b/w for pacing unless we have gotten at least
4927 	 * an initial windows worth of data in this measurement interval.
4928 	 *
4929 	 * Small numbers of bytes get badly influenced by delayed ack and
4930 	 * other artifacts. Note we take the initial window or our
4931 	 * defined minimum GP (defaulting to 10 which hopefully is the
4932 	 * IW).
4933 	 */
4934 	if (rack->rc_gp_filled == 0) {
4935 		/*
4936 		 * The initial estimate is special. We
4937 		 * have blasted out an IW worth of packets
4938 		 * without a real valid ack ts results. We
4939 		 * then setup the app_limited_needs_set flag,
4940 		 * this should get the first ack in (probably 2
4941 		 * MSS worth) to be recorded as the timestamp.
4942 		 * We thus allow a smaller number of bytes i.e.
4943 		 * IW - 2MSS.
4944 		 */
4945 		reqbytes -= (2 * segsiz);
4946 		/* Also lets fill previous for our first measurement to be neutral */
4947 		rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt;
4948 	}
4949 	if ((bytes_ps < reqbytes) || rack->app_limited_needs_set) {
4950 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4951 					   rack->r_ctl.rc_app_limited_cnt,
4952 					   0, 0, 10, __LINE__, NULL, quality);
4953 		goto skip_measurement;
4954 	}
4955 	/*
4956 	 * We now need to calculate the Timely like status so
4957 	 * we can update (possibly) the b/w multipliers.
4958 	 */
4959 	new_rtt_diff = (int32_t)rack->r_ctl.rc_gp_srtt - (int32_t)rack->r_ctl.rc_prev_gp_srtt;
4960 	if (rack->rc_gp_filled == 0) {
4961 		/* No previous reading */
4962 		rack->r_ctl.rc_rtt_diff = new_rtt_diff;
4963 	} else {
4964 		if (rack->measure_saw_probe_rtt == 0) {
4965 			/*
4966 			 * We don't want a probertt to be counted
4967 			 * since it will be negative incorrectly. We
4968 			 * expect to be reducing the RTT when we
4969 			 * pace at a slower rate.
4970 			 */
4971 			rack->r_ctl.rc_rtt_diff -= (rack->r_ctl.rc_rtt_diff / 8);
4972 			rack->r_ctl.rc_rtt_diff += (new_rtt_diff / 8);
4973 		}
4974 	}
4975 	timely_says = rack_make_timely_judgement(rack,
4976 	    rack->r_ctl.rc_gp_srtt,
4977 	    rack->r_ctl.rc_rtt_diff,
4978 	    rack->r_ctl.rc_prev_gp_srtt
4979 	);
4980 	bytes_ps *= HPTS_USEC_IN_SEC;
4981 	bytes_ps /= utim;
4982 	if (bytes_ps > rack->r_ctl.last_max_bw) {
4983 		/*
4984 		 * Something is on path playing
4985 		 * since this b/w is not possible based
4986 		 * on our BDP (highest rwnd and lowest rtt
4987 		 * we saw in the measurement window).
4988 		 *
4989 		 * Another option here would be to
4990 		 * instead skip the measurement.
4991 		 */
4992 		rack_log_pacing_delay_calc(rack, bytes, reqbytes,
4993 					   bytes_ps, rack->r_ctl.last_max_bw, 0,
4994 					   11, __LINE__, NULL, quality);
4995 		bytes_ps = rack->r_ctl.last_max_bw;
4996 	}
4997 	/* We store gp for b/w in bytes per second */
4998 	if (rack->rc_gp_filled == 0) {
4999 		/* Initial measurement */
5000 		if (bytes_ps) {
5001 			rack->r_ctl.gp_bw = bytes_ps;
5002 			rack->rc_gp_filled = 1;
5003 			rack->r_ctl.num_measurements = 1;
5004 			rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
5005 		} else {
5006 			rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
5007 						   rack->r_ctl.rc_app_limited_cnt,
5008 						   0, 0, 10, __LINE__, NULL, quality);
5009 		}
5010 		if (tcp_in_hpts(rack->rc_tp) &&
5011 		    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
5012 			/*
5013 			 * Ok we can't trust the pacer in this case
5014 			 * where we transition from un-paced to paced.
5015 			 * Or for that matter when the burst mitigation
5016 			 * was making a wild guess and got it wrong.
5017 			 * Stop the pacer and clear up all the aggregate
5018 			 * delays etc.
5019 			 */
5020 			tcp_hpts_remove(rack->rc_tp);
5021 			rack->r_ctl.rc_hpts_flags = 0;
5022 			rack->r_ctl.rc_last_output_to = 0;
5023 		}
5024 		did_add = 2;
5025 	} else if (rack->r_ctl.num_measurements < RACK_REQ_AVG) {
5026 		/* Still a small number run an average */
5027 		rack->r_ctl.gp_bw += bytes_ps;
5028 		addpart = rack->r_ctl.num_measurements;
5029 		rack->r_ctl.num_measurements++;
5030 		if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) {
5031 			/* We have collected enough to move forward */
5032 			rack->r_ctl.gp_bw /= (uint64_t)rack->r_ctl.num_measurements;
5033 		}
5034 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
5035 		did_add = 3;
5036 	} else {
5037 		/*
5038 		 * We want to take 1/wma of the goodput and add in to 7/8th
5039 		 * of the old value weighted by the srtt. So if your measurement
5040 		 * period is say 2 SRTT's long you would get 1/4 as the
5041 		 * value, if it was like 1/2 SRTT then you would get 1/16th.
5042 		 *
5043 		 * But we must be careful not to take too much i.e. if the
5044 		 * srtt is say 20ms and the measurement is taken over
5045 		 * 400ms our weight would be 400/20 i.e. 20. On the
5046 		 * other hand if we get a measurement over 1ms with a
5047 		 * 10ms rtt we only want to take a much smaller portion.
5048 		 */
5049 		if (rack->r_ctl.num_measurements < 0xff) {
5050 			rack->r_ctl.num_measurements++;
5051 		}
5052 		srtt = (uint64_t)tp->t_srtt;
5053 		if (srtt == 0) {
5054 			/*
5055 			 * Strange why did t_srtt go back to zero?
5056 			 */
5057 			if (rack->r_ctl.rc_rack_min_rtt)
5058 				srtt = rack->r_ctl.rc_rack_min_rtt;
5059 			else
5060 				srtt = HPTS_USEC_IN_MSEC;
5061 		}
5062 		/*
5063 		 * XXXrrs: Note for reviewers, in playing with
5064 		 * dynamic pacing I discovered this GP calculation
5065 		 * as done originally leads to some undesired results.
5066 		 * Basically you can get longer measurements contributing
5067 		 * too much to the WMA. Thus I changed it if you are doing
5068 		 * dynamic adjustments to only do the aportioned adjustment
5069 		 * if we have a very small (time wise) measurement. Longer
5070 		 * measurements just get there weight (defaulting to 1/8)
5071 		 * add to the WMA. We may want to think about changing
5072 		 * this to always do that for both sides i.e. dynamic
5073 		 * and non-dynamic... but considering lots of folks
5074 		 * were playing with this I did not want to change the
5075 		 * calculation per.se. without your thoughts.. Lawerence?
5076 		 * Peter??
5077 		 */
5078 		if (rack->rc_gp_dyn_mul == 0) {
5079 			subpart = rack->r_ctl.gp_bw * utim;
5080 			subpart /= (srtt * 8);
5081 			if (subpart < (rack->r_ctl.gp_bw / 2)) {
5082 				/*
5083 				 * The b/w update takes no more
5084 				 * away then 1/2 our running total
5085 				 * so factor it in.
5086 				 */
5087 				addpart = bytes_ps * utim;
5088 				addpart /= (srtt * 8);
5089 			} else {
5090 				/*
5091 				 * Don't allow a single measurement
5092 				 * to account for more than 1/2 of the
5093 				 * WMA. This could happen on a retransmission
5094 				 * where utim becomes huge compared to
5095 				 * srtt (multiple retransmissions when using
5096 				 * the sending rate which factors in all the
5097 				 * transmissions from the first one).
5098 				 */
5099 				subpart = rack->r_ctl.gp_bw / 2;
5100 				addpart = bytes_ps / 2;
5101 			}
5102 			resid_bw = rack->r_ctl.gp_bw - subpart;
5103 			rack->r_ctl.gp_bw = resid_bw + addpart;
5104 			did_add = 1;
5105 		} else {
5106 			if ((utim / srtt) <= 1) {
5107 				/*
5108 				 * The b/w update was over a small period
5109 				 * of time. The idea here is to prevent a small
5110 				 * measurement time period from counting
5111 				 * too much. So we scale it based on the
5112 				 * time so it attributes less than 1/rack_wma_divisor
5113 				 * of its measurement.
5114 				 */
5115 				subpart = rack->r_ctl.gp_bw * utim;
5116 				subpart /= (srtt * rack_wma_divisor);
5117 				addpart = bytes_ps * utim;
5118 				addpart /= (srtt * rack_wma_divisor);
5119 			} else {
5120 				/*
5121 				 * The scaled measurement was long
5122 				 * enough so lets just add in the
5123 				 * portion of the measurement i.e. 1/rack_wma_divisor
5124 				 */
5125 				subpart = rack->r_ctl.gp_bw / rack_wma_divisor;
5126 				addpart = bytes_ps / rack_wma_divisor;
5127 			}
5128 			if ((rack->measure_saw_probe_rtt == 0) ||
5129 		            (bytes_ps > rack->r_ctl.gp_bw)) {
5130 				/*
5131 				 * For probe-rtt we only add it in
5132 				 * if its larger, all others we just
5133 				 * add in.
5134 				 */
5135 				did_add = 1;
5136 				resid_bw = rack->r_ctl.gp_bw - subpart;
5137 				rack->r_ctl.gp_bw = resid_bw + addpart;
5138 			}
5139 		}
5140 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
5141 	}
5142 	if ((rack->gp_ready == 0) &&
5143 	    (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) {
5144 		/* We have enough measurements now */
5145 		rack->gp_ready = 1;
5146 		if (rack->dgp_on ||
5147 		    rack->rack_hibeta)
5148 			rack_set_cc_pacing(rack);
5149 		if (rack->defer_options)
5150 			rack_apply_deferred_options(rack);
5151 	}
5152 	rack_log_pacing_delay_calc(rack, subpart, addpart, bytes_ps, stim,
5153 				   rack_get_bw(rack), 22, did_add, NULL, quality);
5154 	/* We do not update any multipliers if we are in or have seen a probe-rtt */
5155 	if ((rack->measure_saw_probe_rtt == 0) && rack->rc_gp_rtt_set)
5156 		rack_update_multiplier(rack, timely_says, bytes_ps,
5157 				       rack->r_ctl.rc_gp_srtt,
5158 				       rack->r_ctl.rc_rtt_diff);
5159 	rack_log_pacing_delay_calc(rack, bytes, tim, bytes_ps, stim,
5160 				   rack_get_bw(rack), 3, line, NULL, quality);
5161 	rack_log_pacing_delay_calc(rack,
5162 				   bytes, /* flex2 */
5163 				   tim, /* flex1 */
5164 				   bytes_ps, /* bw_inuse */
5165 				   rack->r_ctl.gp_bw, /* delRate */
5166 				   rack_get_lt_bw(rack), /* rttProp */
5167 				   20, line, NULL, 0);
5168 	/* reset the gp srtt and setup the new prev */
5169 	rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt;
5170 	/* Record the lost count for the next measurement */
5171 	rack->r_ctl.rc_loss_at_start = rack->r_ctl.rc_loss_count;
5172 skip_measurement:
5173 	/*
5174 	 * We restart our diffs based on the gpsrtt in the
5175 	 * measurement window.
5176 	 */
5177 	rack->rc_gp_rtt_set = 0;
5178 	rack->rc_gp_saw_rec = 0;
5179 	rack->rc_gp_saw_ca = 0;
5180 	rack->rc_gp_saw_ss = 0;
5181 	rack->rc_dragged_bottom = 0;
5182 
5183 	if (quality == RACK_QUALITY_HIGH) {
5184 		/*
5185 		 * Gput in the stats world is in kbps where bytes_ps is
5186 		 * bytes per second so we do ((x * 8)/ 1000).
5187 		 */
5188 		gput = (int32_t)((bytes_ps << 3) / (uint64_t)1000);
5189 #ifdef STATS
5190 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
5191 					 gput);
5192 		/*
5193 		 * XXXLAS: This is a temporary hack, and should be
5194 		 * chained off VOI_TCP_GPUT when stats(9) grows an
5195 		 * API to deal with chained VOIs.
5196 		 */
5197 		if (tp->t_stats_gput_prev > 0)
5198 			stats_voi_update_abs_s32(tp->t_stats,
5199 						 VOI_TCP_GPUT_ND,
5200 						 ((gput - tp->t_stats_gput_prev) * 100) /
5201 						 tp->t_stats_gput_prev);
5202 #endif
5203 		tp->t_stats_gput_prev = gput;
5204 	}
5205 	tp->t_flags &= ~TF_GPUTINPROG;
5206 	/*
5207 	 * Now are we app limited now and there is space from where we
5208 	 * were to where we want to go?
5209 	 *
5210 	 * We don't do the other case i.e. non-applimited here since
5211 	 * the next send will trigger us picking up the missing data.
5212 	 */
5213 	if (rack->r_ctl.rc_first_appl &&
5214 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
5215 	    rack->r_ctl.rc_app_limited_cnt &&
5216 	    (SEQ_GT(rack->r_ctl.rc_first_appl->r_start, th_ack)) &&
5217 	    ((rack->r_ctl.rc_first_appl->r_end - th_ack) >
5218 	     max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) {
5219 		/*
5220 		 * Yep there is enough outstanding to make a measurement here.
5221 		 */
5222 		struct rack_sendmap *rsm;
5223 
5224 		rack->r_ctl.rc_gp_lowrtt = 0xffffffff;
5225 		rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
5226 		tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
5227 		rack->app_limited_needs_set = 0;
5228 		tp->gput_seq = th_ack;
5229 		if (rack->in_probe_rtt)
5230 			rack->measure_saw_probe_rtt = 1;
5231 		else if ((rack->measure_saw_probe_rtt) &&
5232 			 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
5233 			rack->measure_saw_probe_rtt = 0;
5234 		if ((rack->r_ctl.rc_first_appl->r_end - th_ack) >= rack_get_measure_window(tp, rack)) {
5235 			/* There is a full window to gain info from */
5236 			tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
5237 		} else {
5238 			/* We can only measure up to the applimited point */
5239 			tp->gput_ack = tp->gput_seq + (rack->r_ctl.rc_first_appl->r_end - th_ack);
5240 			if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) {
5241 				/*
5242 				 * We don't have enough to make a measurement.
5243 				 */
5244 				tp->t_flags &= ~TF_GPUTINPROG;
5245 				rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq,
5246 							   0, 0, 0, 6, __LINE__, NULL, quality);
5247 				return;
5248 			}
5249 		}
5250 		if (tp->t_state >= TCPS_FIN_WAIT_1) {
5251 			/*
5252 			 * We will get no more data into the SB
5253 			 * this means we need to have the data available
5254 			 * before we start a measurement.
5255 			 */
5256 			if (sbavail(&tptosocket(tp)->so_snd) < (tp->gput_ack - tp->gput_seq)) {
5257 				/* Nope not enough data. */
5258 				return;
5259 			}
5260 		}
5261 		tp->t_flags |= TF_GPUTINPROG;
5262 		/*
5263 		 * Now we need to find the timestamp of the send at tp->gput_seq
5264 		 * for the send based measurement.
5265 		 */
5266 		rack->r_ctl.rc_gp_cumack_ts = 0;
5267 		rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
5268 		if (rsm) {
5269 			/* Ok send-based limit is set */
5270 			if (SEQ_LT(rsm->r_start, tp->gput_seq)) {
5271 				/*
5272 				 * Move back to include the earlier part
5273 				 * so our ack time lines up right (this may
5274 				 * make an overlapping measurement but thats
5275 				 * ok).
5276 				 */
5277 				tp->gput_seq = rsm->r_start;
5278 			}
5279 			if (rsm->r_flags & RACK_ACKED) {
5280 				struct rack_sendmap *nrsm;
5281 
5282 				tp->gput_ts = (uint32_t)rsm->r_ack_arrival;
5283 				tp->gput_seq = rsm->r_end;
5284 				nrsm = tqhash_next(rack->r_ctl.tqh, rsm);
5285 				if (nrsm)
5286 					rsm = nrsm;
5287 				else {
5288 					rack->app_limited_needs_set = 1;
5289 				}
5290 			} else
5291 				rack->app_limited_needs_set = 1;
5292 			/* We always go from the first send */
5293 			rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[0];
5294 		} else {
5295 			/*
5296 			 * If we don't find the rsm due to some
5297 			 * send-limit set the current time, which
5298 			 * basically disables the send-limit.
5299 			 */
5300 			struct timeval tv;
5301 
5302 			microuptime(&tv);
5303 			rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
5304 		}
5305 		rack_tend_gp_marks(tp, rack);
5306 		rack_log_pacing_delay_calc(rack,
5307 					   tp->gput_seq,
5308 					   tp->gput_ack,
5309 					   (uint64_t)rsm,
5310 					   tp->gput_ts,
5311 					   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
5312 					   9,
5313 					   __LINE__, rsm, quality);
5314 		rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
5315 	} else {
5316 		/*
5317 		 * To make sure proper timestamp merging occurs, we need to clear
5318 		 * all GP marks if we don't start a measurement.
5319 		 */
5320 		rack_clear_gp_marks(tp, rack);
5321 	}
5322 }
5323 
5324 /*
5325  * CC wrapper hook functions
5326  */
5327 static void
5328 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack, uint32_t th_ack, uint16_t nsegs,
5329     uint16_t type, int32_t recovery)
5330 {
5331 	uint32_t prior_cwnd, acked;
5332 	struct tcp_log_buffer *lgb = NULL;
5333 	uint8_t labc_to_use, quality;
5334 
5335 	INP_WLOCK_ASSERT(tptoinpcb(tp));
5336 	tp->t_ccv.nsegs = nsegs;
5337 	acked = tp->t_ccv.bytes_this_ack = (th_ack - tp->snd_una);
5338 	if ((recovery) && (rack->r_ctl.rc_early_recovery_segs)) {
5339 		uint32_t max;
5340 
5341 		max = rack->r_ctl.rc_early_recovery_segs * ctf_fixed_maxseg(tp);
5342 		if (tp->t_ccv.bytes_this_ack > max) {
5343 			tp->t_ccv.bytes_this_ack = max;
5344 		}
5345 	}
5346 #ifdef STATS
5347 	stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF,
5348 	    ((int32_t)rack->r_ctl.cwnd_to_use) - tp->snd_wnd);
5349 #endif
5350 	if ((th_ack == tp->snd_max) && rack->lt_bw_up) {
5351 		/* We will ack all, time
5352 		 * to end any lt_bw_up we
5353 		 * have running until something
5354 		 * new is sent.
5355 		 */
5356 		struct timeval tv;
5357 
5358 		rack->r_ctl.lt_bw_bytes += (tp->snd_max - rack->r_ctl.lt_seq);
5359 		rack->r_ctl.lt_seq = tp->snd_max;
5360 		(void)tcp_get_usecs(&tv);
5361 		rack->r_ctl.lt_bw_time += (tcp_tv_to_lusectick(&tv) - rack->r_ctl.lt_timemark);
5362 		rack->lt_bw_up = 0;
5363 	}
5364 	quality = RACK_QUALITY_NONE;
5365 	if ((tp->t_flags & TF_GPUTINPROG) &&
5366 	    rack_enough_for_measurement(tp, rack, th_ack, &quality)) {
5367 		/* Measure the Goodput */
5368 		rack_do_goodput_measurement(tp, rack, th_ack, __LINE__, quality);
5369 	}
5370 	/* Which way our we limited, if not cwnd limited no advance in CA */
5371 	if (tp->snd_cwnd <= tp->snd_wnd)
5372 		tp->t_ccv.flags |= CCF_CWND_LIMITED;
5373 	else
5374 		tp->t_ccv.flags &= ~CCF_CWND_LIMITED;
5375 	if (tp->snd_cwnd > tp->snd_ssthresh) {
5376 		tp->t_bytes_acked += min(tp->t_ccv.bytes_this_ack,
5377 			 nsegs * V_tcp_abc_l_var * ctf_fixed_maxseg(tp));
5378 		/* For the setting of a window past use the actual scwnd we are using */
5379 		if (tp->t_bytes_acked >= rack->r_ctl.cwnd_to_use) {
5380 			tp->t_bytes_acked -= rack->r_ctl.cwnd_to_use;
5381 			tp->t_ccv.flags |= CCF_ABC_SENTAWND;
5382 		}
5383 	} else {
5384 		tp->t_ccv.flags &= ~CCF_ABC_SENTAWND;
5385 		tp->t_bytes_acked = 0;
5386 	}
5387 	prior_cwnd = tp->snd_cwnd;
5388 	if ((recovery == 0) || (rack_max_abc_post_recovery == 0) || rack->r_use_labc_for_rec ||
5389 	    (rack_client_low_buf && rack->client_bufferlvl &&
5390 	    (rack->client_bufferlvl < rack_client_low_buf)))
5391 		labc_to_use = rack->rc_labc;
5392 	else
5393 		labc_to_use = rack_max_abc_post_recovery;
5394 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
5395 		union tcp_log_stackspecific log;
5396 		struct timeval tv;
5397 
5398 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5399 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5400 		log.u_bbr.flex1 = th_ack;
5401 		log.u_bbr.flex2 = tp->t_ccv.flags;
5402 		log.u_bbr.flex3 = tp->t_ccv.bytes_this_ack;
5403 		log.u_bbr.flex4 = tp->t_ccv.nsegs;
5404 		log.u_bbr.flex5 = labc_to_use;
5405 		log.u_bbr.flex6 = prior_cwnd;
5406 		log.u_bbr.flex7 = V_tcp_do_newsack;
5407 		log.u_bbr.flex8 = 1;
5408 		lgb = tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
5409 				     0, &log, false, NULL, __func__, __LINE__,&tv);
5410 	}
5411 	if (CC_ALGO(tp)->ack_received != NULL) {
5412 		/* XXXLAS: Find a way to live without this */
5413 		tp->t_ccv.curack = th_ack;
5414 		tp->t_ccv.labc = labc_to_use;
5415 		tp->t_ccv.flags |= CCF_USE_LOCAL_ABC;
5416 		CC_ALGO(tp)->ack_received(&tp->t_ccv, type);
5417 	}
5418 	if (lgb) {
5419 		lgb->tlb_stackinfo.u_bbr.flex6 = tp->snd_cwnd;
5420 	}
5421 	if (rack->r_must_retran) {
5422 		if (SEQ_GEQ(th_ack, rack->r_ctl.rc_snd_max_at_rto)) {
5423 			/*
5424 			 * We now are beyond the rxt point so lets disable
5425 			 * the flag.
5426 			 */
5427 			rack->r_ctl.rc_out_at_rto = 0;
5428 			rack->r_must_retran = 0;
5429 		} else if ((prior_cwnd + ctf_fixed_maxseg(tp)) <= tp->snd_cwnd) {
5430 			/*
5431 			 * Only decrement the rc_out_at_rto if the cwnd advances
5432 			 * at least a whole segment. Otherwise next time the peer
5433 			 * acks, we won't be able to send this generaly happens
5434 			 * when we are in Congestion Avoidance.
5435 			 */
5436 			if (acked <= rack->r_ctl.rc_out_at_rto){
5437 				rack->r_ctl.rc_out_at_rto -= acked;
5438 			} else {
5439 				rack->r_ctl.rc_out_at_rto = 0;
5440 			}
5441 		}
5442 	}
5443 #ifdef STATS
5444 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, rack->r_ctl.cwnd_to_use);
5445 #endif
5446 	if (rack->r_ctl.rc_rack_largest_cwnd < rack->r_ctl.cwnd_to_use) {
5447 		rack->r_ctl.rc_rack_largest_cwnd = rack->r_ctl.cwnd_to_use;
5448 	}
5449 }
5450 
5451 static void
5452 tcp_rack_partialack(struct tcpcb *tp)
5453 {
5454 	struct tcp_rack *rack;
5455 
5456 	rack = (struct tcp_rack *)tp->t_fb_ptr;
5457 	INP_WLOCK_ASSERT(tptoinpcb(tp));
5458 	/*
5459 	 * If we are doing PRR and have enough
5460 	 * room to send <or> we are pacing and prr
5461 	 * is disabled we will want to see if we
5462 	 * can send data (by setting r_wanted_output to
5463 	 * true).
5464 	 */
5465 	if ((rack->r_ctl.rc_prr_sndcnt > 0) ||
5466 	    rack->rack_no_prr)
5467 		rack->r_wanted_output = 1;
5468 }
5469 
5470 static inline void
5471 rack_set_most_aggr(struct tcp_rack *rack)
5472 {
5473 	rack->r_fill_less_agg = 0;
5474 	/* Once the cwnd as been clamped we don't do fill_cw */
5475 	if (rack->r_cwnd_was_clamped == 0)
5476 		rack->rc_pace_to_cwnd = 1;
5477 	rack->r_pacing_discount = 0;
5478 }
5479 
5480 static inline void
5481 rack_limit_fillcw(struct tcp_rack *rack)
5482 {
5483 	rack->r_fill_less_agg = 1;
5484 	/* Once the cwnd as been clamped we don't do fill_cw */
5485 	if (rack->r_cwnd_was_clamped == 0)
5486 		rack->rc_pace_to_cwnd = 1;
5487 	rack->r_pacing_discount = 0;
5488 }
5489 
5490 static inline void
5491 rack_disable_fillcw(struct tcp_rack *rack)
5492 {
5493 	rack->r_fill_less_agg = 1;
5494 	rack->rc_pace_to_cwnd = 0;
5495 	rack->r_pacing_discount = 0;
5496 }
5497 
5498 static void
5499 rack_client_buffer_level_set(struct tcp_rack *rack)
5500 {
5501 	/*
5502 	 * Only if DGP is on do we do anything that
5503 	 * changes stack behavior. If DGP is off all
5504 	 * we will do is issue a BB log (if BB logging is
5505 	 * on) and return.
5506 	 */
5507 	if (rack->dgp_on == 0) {
5508 		rack_log_pacing_delay_calc(rack, 0, rack->client_bufferlvl,
5509 					   0, 0, 0, 30, __LINE__, NULL, 0);
5510 		return;
5511 	}
5512 	if (IN_RECOVERY(rack->rc_tp->t_flags) && rack->r_ctl.full_dgp_in_rec) {
5513 		goto set_most_agg;
5514 	}
5515 	/*
5516 	 * We are in DGP so what setting should we
5517 	 * apply based on where the client is?
5518 	 */
5519 	switch(rack->r_ctl.rc_dgp_bl_agg) {
5520 	default:
5521 	case DGP_LEVEL0:
5522 set_most_agg:
5523 		rack_set_most_aggr(rack);
5524 		break;
5525 	case DGP_LEVEL1:
5526 		if (rack->client_bufferlvl == 4)
5527 			rack_limit_fillcw(rack);
5528 		else if (rack->client_bufferlvl == 5)
5529 			rack_disable_fillcw(rack);
5530 		else
5531 			rack_set_most_aggr(rack);
5532 		break;
5533 	case DGP_LEVEL2:
5534 		if (rack->client_bufferlvl == 3)
5535 			rack_limit_fillcw(rack);
5536 		else if (rack->client_bufferlvl == 4)
5537 			rack_disable_fillcw(rack);
5538 		else if (rack->client_bufferlvl == 5) {
5539 			rack_disable_fillcw(rack);
5540 			rack->r_pacing_discount = 1;
5541 			rack->r_ctl.pacing_discount_amm = 1;
5542 		} else
5543 			rack_set_most_aggr(rack);
5544 		break;
5545 	case DGP_LEVEL3:
5546 		if (rack->client_bufferlvl == 2)
5547 			rack_limit_fillcw(rack);
5548 		else if (rack->client_bufferlvl == 3)
5549 			rack_disable_fillcw(rack);
5550 		else if (rack->client_bufferlvl == 4) {
5551 			rack_disable_fillcw(rack);
5552 			rack->r_pacing_discount = 1;
5553 			rack->r_ctl.pacing_discount_amm = 1;
5554 		} else if (rack->client_bufferlvl == 5) {
5555 			rack_disable_fillcw(rack);
5556 			rack->r_pacing_discount = 1;
5557 			rack->r_ctl.pacing_discount_amm = 2;
5558 		} else
5559 			rack_set_most_aggr(rack);
5560 		break;
5561 	}
5562 	rack_log_pacing_delay_calc(rack, rack->r_ctl.rc_dgp_bl_agg, rack->client_bufferlvl, 0,
5563 				   0, 0, 30, __LINE__, NULL, 0);
5564 }
5565 
5566 static void
5567 do_rack_check_for_unclamp(struct tcpcb *tp, struct tcp_rack *rack)
5568 {
5569 	/*
5570 	 * Can we unclamp. We unclamp if more than
5571 	 * N rounds have transpired with no loss.
5572 	 */
5573 	uint64_t snds, rxts, rxt_per;
5574 	uint32_t rnds;
5575 
5576 	rnds = rack->r_ctl.current_round - rack->r_ctl.last_rnd_rxt_clamped;
5577 	if ((rack_unclamp_round_thresh > 0) &&
5578 	    (rnds >= rack_unclamp_round_thresh)) {
5579 		snds = tp->t_sndbytes - rack->r_ctl.last_sndbytes;
5580 		KASSERT ((snds > 0), ("rack:%p tp:%p snds:%ju is 0", rack, tp,
5581 		    (uintmax_t)snds));
5582 		rxts = tp->t_snd_rxt_bytes - rack->r_ctl.last_snd_rxt_bytes;
5583 		rxt_per = rxts * 1000;
5584 		rxt_per /= snds;
5585 		if ((uint32_t)rxt_per <= rack_unclamp_rxt_thresh) {
5586 			/* Unclamp */
5587 			if (tcp_bblogging_on(rack->rc_tp)) {
5588 				union tcp_log_stackspecific log;
5589 				struct timeval tv;
5590 
5591 				memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5592 				log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5593 				log.u_bbr.flex3 = rnds;
5594 				log.u_bbr.flex4 = rack_unclamp_round_thresh;
5595 				log.u_bbr.flex5 = (uint32_t)rxt_per;
5596 				log.u_bbr.flex8 = 6;
5597 				log.u_bbr.pkt_epoch = rack->r_ctl.rc_pace_max_segs;
5598 				log.u_bbr.bbr_state = rack->rc_pace_to_cwnd;
5599 				log.u_bbr.delivered = rack->r_ctl.num_of_clamps_applied;
5600 				log.u_bbr.applimited = rack->r_ctl.max_clamps;
5601 				log.u_bbr.epoch = rack->r_ctl.clamp_options;
5602 				log.u_bbr.cur_del_rate = rxts;
5603 				log.u_bbr.bw_inuse = rack_get_lt_bw(rack);
5604 				log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
5605 				log.u_bbr.lt_epoch = (uint32_t)((rack->r_ctl.gp_bw >> 32) & 0x00000000ffffffff);
5606 				log.u_bbr.pkts_out = (uint32_t)(rack->r_ctl.gp_bw & 0x00000000ffffffff);
5607 				tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
5608 					      0, &log, false, NULL, NULL, 0, &tv);
5609 			}
5610 			rack->r_ctl.num_of_clamps_applied = 0;
5611 			rack->r_cwnd_was_clamped = 0;
5612 			rack->excess_rxt_on = 1;
5613 			if (rack->r_ctl.clamp_options) {
5614 				/*
5615 				 * We only allow fillcw to be toggled
5616 				 * if you are setting a max seg too.
5617 				 */
5618 				if (rack->r_ctl.clamp_options & 0x1) {
5619 					if ((rack->rc_pace_to_cwnd == 0) && (rack->dgp_on == 0)) {
5620 						/* turn on fill cw  for non-dgp*/
5621 						rack->rc_pace_to_cwnd = 0;
5622 					} else if ((rack->dgp_on == 1) && (rack->rc_pace_to_cwnd == 1)) {
5623 						/* For DGP we want it off */
5624 						rack->rc_pace_to_cwnd = 1;
5625 					}
5626 				}
5627 			}
5628 			if (rack->dgp_on) {
5629 				/* Reset all multipliers to 100.0 so just the measured bw */
5630 				/* Crash any per boosts down to 100% */
5631 				rack->r_ctl.rack_per_of_gp_rec = 100;
5632 				rack->r_ctl.rack_per_of_gp_ss = 100;
5633 				rack->r_ctl.rack_per_of_gp_ca = 100;
5634 				/* Set in an upper bound for ss/ca % increase */
5635 				rack->r_ctl.rack_per_upper_bound_ss = (uint8_t)rack_per_upper_bound_ss;
5636 				rack->r_ctl.rack_per_upper_bound_ca = (uint8_t)rack_per_upper_bound_ca;
5637 			}
5638 		}
5639 	}
5640 }
5641 
5642 static void
5643 do_rack_excess_rxt(struct tcpcb *tp, struct tcp_rack *rack)
5644 {
5645 	/*
5646 	 * Rack excess rxt accounting is turned on. If we
5647 	 * are above a threshold of rxt's in at least N
5648 	 * rounds, then back off the cwnd and ssthresh
5649 	 * to fit into the long-term b/w.
5650 	 */
5651 	uint64_t snds, rxts, rxt_per, lt_bw, bdp;
5652 	uint32_t rnds, new_cwnd, new_ssthresh, rtt, shared_cwnd_was_enabled = 0;
5653 
5654 	/* Is it shut off by 0 rounds? */
5655 	if (rack_rxt_min_rnds == 0)
5656 		return;
5657 	if ((rack->r_ctl.max_clamps > 0) &&
5658 	    (rack->r_ctl.num_of_clamps_applied >= rack->r_ctl.max_clamps)) {
5659 		/*
5660 		 * The idea, if max_clamps is set, is that if clamping it
5661 		 * N times did not work again, then there is no sense
5662 		 * clamping it again. The link is just a lossy link and
5663 		 * our clamps are doing no good. Turn it off so we don't come
5664 		 * back here again.
5665 		 */
5666 		rack->excess_rxt_on = 0;
5667 		rack->r_cwnd_was_clamped = 0;
5668 		rack->r_ctl.num_of_clamps_applied = 0;
5669 		return;
5670 	}
5671 	snds = tp->t_sndbytes - rack->r_ctl.last_sndbytes;
5672 	rxts = tp->t_snd_rxt_bytes - rack->r_ctl.last_snd_rxt_bytes;
5673 	rnds = rack->r_ctl.current_round - rack->r_ctl.last_rnd_rxt_clamped;
5674 	/* Has enough rounds progressed for us to re-measure? */
5675 	if ((rnds >= rack_rxt_min_rnds) &&
5676 	    (rack->r_ctl.rxt_threshold > 0)){
5677 		rxt_per = rxts * 1000;
5678 		rxt_per /= snds;
5679 		if (rxt_per >= rack->r_ctl.rxt_threshold) {
5680 			/*
5681 			 * Action required:
5682 			 *  We are above our excess retransmit level, lets
5683 			 *  cut down the cwnd and ssthresh to match the long-term
5684 			 *  b/w we are getting.
5685 			 */
5686 			/* First disable scwnd if enabled */
5687 #ifdef NETFLIX_SHARED_CWND
5688 			rack->rack_enable_scwnd = 0;
5689 			if (rack->r_ctl.rc_scw) {
5690 				uint32_t limit;
5691 
5692 				shared_cwnd_was_enabled = 1;
5693 				if (rack->r_limit_scw)
5694 					limit = max(1, rack->r_ctl.rc_lowest_us_rtt);
5695 				else
5696 					limit = 0;
5697 				tcp_shared_cwnd_free_full(tp, rack->r_ctl.rc_scw,
5698 							  rack->r_ctl.rc_scw_index,
5699 							  limit);
5700 				rack->r_ctl.rc_scw = NULL;
5701 			}
5702 
5703 #endif
5704 			/* Calculate what the cwnd and ssthresh should be */
5705 			tcp_trace_point(rack->rc_tp, TCP_TP_EXCESS_RXT);
5706 			lt_bw = rack_get_lt_bw(rack);
5707 			if (lt_bw == 0) {
5708 				/*
5709 				 * No lt_bw, lets chop things to one MSS
5710 				 * and the ssthresh to the iwnd.
5711 				 */
5712 reset_to_iw:
5713 				new_cwnd = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
5714 				new_ssthresh = tcp_compute_initwnd(tcp_maxseg(tp));
5715 			} else {
5716 				rtt = rack->rc_rack_rtt;
5717 				if (rtt == 0) {
5718 					/* If we have no rack_rtt drop to the IW situation */
5719 					goto reset_to_iw;
5720 				}
5721 				bdp = lt_bw * (uint64_t)rtt;
5722 				bdp /= HPTS_USEC_IN_SEC;
5723 				new_cwnd = (uint32_t)bdp;
5724 				new_ssthresh = new_cwnd - 1;
5725 				if (new_cwnd < ctf_fixed_maxseg(tp)) {
5726 					/* Rock bottom, goto IW settings  */
5727 					goto reset_to_iw;
5728 				}
5729 			}
5730 			rack->r_cwnd_was_clamped = 1;
5731 			rack->r_ctl.num_of_clamps_applied++;
5732 			/* Reset the counter fromn now */
5733 			tp->t_bytes_acked = 0;
5734 			/*
5735 			 * Now what about options?
5736 			 * We look at the bottom  8 bits:
5737 			 * F = fill cw bit (toggle it if set)
5738 			 * S = Segment bits
5739 			 * M = set max segment bit
5740 			 *
5741 			 * SSSS SSMF
5742 			 */
5743 			if (rack->r_ctl.clamp_options) {
5744 				if (rack->r_ctl.clamp_options & 0x1) {
5745 					if ((rack->rc_pace_to_cwnd == 0) && (rack->dgp_on == 0)) {
5746 						/* turn on fill cw  for non-dgp*/
5747 						rack->rc_pace_to_cwnd = 1;
5748 					} else if ((rack->dgp_on == 1) && (rack->rc_pace_to_cwnd == 1)) {
5749 						/* For DGP we want it off */
5750 						rack->rc_pace_to_cwnd = 0;
5751 					}
5752 				}
5753 			}
5754 			if (rack->dgp_on) {
5755 				/* Reset all multipliers to 100.0 so just the measured bw */
5756 				/* Crash any per boosts down to 100% */
5757 				rack->r_ctl.rack_per_of_gp_rec = 100;
5758 				rack->r_ctl.rack_per_of_gp_ss = 100;
5759 				rack->r_ctl.rack_per_of_gp_ca = 100;
5760 				/* Set in an upper bound for ss/ca % increase */
5761 				rack->r_ctl.rack_per_upper_bound_ss = (uint8_t)rack_clamp_ss_upper;
5762 				rack->r_ctl.rack_per_upper_bound_ca = (uint8_t)rack_clamp_ca_upper;
5763 				/* Now move to the lt_bw */
5764 				rack->r_ctl.gp_bw = lt_bw;
5765 				rack->rc_gp_filled = 1;
5766 				rack->r_ctl.num_measurements = RACK_REQ_AVG;
5767 			}
5768 			if (tcp_bblogging_on(rack->rc_tp)) {
5769 				union tcp_log_stackspecific log;
5770 				struct timeval tv;
5771 
5772 				memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5773 				log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5774 				log.u_bbr.flex1 = new_cwnd;
5775 				log.u_bbr.flex2 = new_ssthresh;
5776 				log.u_bbr.flex3 = rnds;
5777 				log.u_bbr.flex4 = rack_rxt_min_rnds;
5778 				log.u_bbr.flex5 = rtt;
5779 				log.u_bbr.flex6 = shared_cwnd_was_enabled;
5780 				log.u_bbr.flex8 = 5;
5781 				log.u_bbr.pkt_epoch = rack->r_ctl.rc_pace_max_segs;
5782 				log.u_bbr.bbr_state = rack->rc_pace_to_cwnd;
5783 				log.u_bbr.delivered = rack->r_ctl.num_of_clamps_applied;
5784 				log.u_bbr.applimited = rack->r_ctl.max_clamps;
5785 				log.u_bbr.epoch = rack->r_ctl.clamp_options;
5786 				log.u_bbr.cur_del_rate = rxts;
5787 				log.u_bbr.delRate = snds;
5788 				log.u_bbr.rttProp = rack->r_ctl.rxt_threshold;
5789 				log.u_bbr.bw_inuse = lt_bw;
5790 				log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
5791 				log.u_bbr.lt_epoch = (uint32_t)((rack->r_ctl.gp_bw >> 32) & 0x00000000ffffffff);
5792 				log.u_bbr.pkts_out = (uint32_t)(rack->r_ctl.gp_bw & 0x00000000ffffffff);
5793 				tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
5794 					       0, &log, false, NULL, NULL, 0, &tv);
5795 			}
5796 			/* Update our point where we did it */
5797 			if (rack->r_ctl.already_had_a_excess == 0) {
5798 				rack->r_ctl.already_had_a_excess = 1;
5799 				counter_u64_add(rack_rxt_clamps_cwnd_uniq, 1);
5800 			}
5801 			counter_u64_add(rack_rxt_clamps_cwnd, 1);
5802 			rack->r_ctl.last_sndbytes = tp->t_sndbytes;
5803 			rack->r_ctl.last_snd_rxt_bytes = tp->t_snd_rxt_bytes;
5804 			rack->r_ctl.last_rnd_rxt_clamped = rack->r_ctl.current_round;
5805 			if (new_cwnd < tp->snd_cwnd)
5806 				tp->snd_cwnd = new_cwnd;
5807 			if (new_ssthresh < tp->snd_ssthresh)
5808 				tp->snd_ssthresh = new_ssthresh;
5809 		}
5810 	}
5811 }
5812 
5813 static void
5814 rack_post_recovery(struct tcpcb *tp, uint32_t th_ack)
5815 {
5816 	struct tcp_rack *rack;
5817 	uint32_t orig_cwnd;
5818 
5819 	orig_cwnd = tp->snd_cwnd;
5820 	INP_WLOCK_ASSERT(tptoinpcb(tp));
5821 	rack = (struct tcp_rack *)tp->t_fb_ptr;
5822 	/* only alert CC if we alerted when we entered */
5823 	if (CC_ALGO(tp)->post_recovery != NULL) {
5824 		tp->t_ccv.curack = th_ack;
5825 		CC_ALGO(tp)->post_recovery(&tp->t_ccv);
5826 		if (tp->snd_cwnd < tp->snd_ssthresh) {
5827 			/*
5828 			 * Rack has burst control and pacing
5829 			 * so lets not set this any lower than
5830 			 * snd_ssthresh per RFC-6582 (option 2).
5831 			 */
5832 			tp->snd_cwnd = tp->snd_ssthresh;
5833 		}
5834 	}
5835 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
5836 		union tcp_log_stackspecific log;
5837 		struct timeval tv;
5838 
5839 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5840 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5841 		log.u_bbr.flex1 = th_ack;
5842 		log.u_bbr.flex2 = tp->t_ccv.flags;
5843 		log.u_bbr.flex3 = tp->t_ccv.bytes_this_ack;
5844 		log.u_bbr.flex4 = tp->t_ccv.nsegs;
5845 		log.u_bbr.flex5 = V_tcp_abc_l_var;
5846 		log.u_bbr.flex6 = orig_cwnd;
5847 		log.u_bbr.flex7 = V_tcp_do_newsack;
5848 		log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
5849 		log.u_bbr.flex8 = 2;
5850 		tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
5851 			       0, &log, false, NULL, __func__, __LINE__, &tv);
5852 	}
5853 	if ((rack->rack_no_prr == 0) &&
5854 	    (rack->no_prr_addback == 0) &&
5855 	    (rack->r_ctl.rc_prr_sndcnt > 0)) {
5856 		/*
5857 		 * Suck the next prr cnt back into cwnd, but
5858 		 * only do that if we are not application limited.
5859 		 */
5860 		if (ctf_outstanding(tp) <= sbavail(&tptosocket(tp)->so_snd)) {
5861 			/*
5862 			 * We are allowed to add back to the cwnd the amount we did
5863 			 * not get out if:
5864 			 * a) no_prr_addback is off.
5865 			 * b) we are not app limited
5866 			 * c) we are doing prr
5867 			 * <and>
5868 			 * d) it is bounded by rack_prr_addbackmax (if addback is 0, then none).
5869 			 */
5870 			tp->snd_cwnd += min((ctf_fixed_maxseg(tp) * rack_prr_addbackmax),
5871 					    rack->r_ctl.rc_prr_sndcnt);
5872 		}
5873 		rack->r_ctl.rc_prr_sndcnt = 0;
5874 		rack_log_to_prr(rack, 1, 0, __LINE__);
5875 	}
5876 	rack_log_to_prr(rack, 14, orig_cwnd, __LINE__);
5877 	tp->snd_recover = tp->snd_una;
5878 	if (rack->r_ctl.dsack_persist) {
5879 		rack->r_ctl.dsack_persist--;
5880 		if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
5881 			rack->r_ctl.num_dsack = 0;
5882 		}
5883 		rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
5884 	}
5885 	EXIT_RECOVERY(tp->t_flags);
5886 	if (rack->r_ctl.full_dgp_in_rec)
5887 		rack_client_buffer_level_set(rack);
5888 }
5889 
5890 static void
5891 rack_cong_signal(struct tcpcb *tp, uint32_t type, uint32_t ack, int line)
5892 {
5893 	struct tcp_rack *rack;
5894 	uint32_t ssthresh_enter, cwnd_enter, in_rec_at_entry, orig_cwnd;
5895 
5896 	INP_WLOCK_ASSERT(tptoinpcb(tp));
5897 #ifdef STATS
5898 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type);
5899 #endif
5900 	if (IN_RECOVERY(tp->t_flags) == 0) {
5901 		in_rec_at_entry = 0;
5902 		ssthresh_enter = tp->snd_ssthresh;
5903 		cwnd_enter = tp->snd_cwnd;
5904 	} else
5905 		in_rec_at_entry = 1;
5906 	rack = (struct tcp_rack *)tp->t_fb_ptr;
5907 	switch (type) {
5908 	case CC_NDUPACK:
5909 		tp->t_flags &= ~TF_WASFRECOVERY;
5910 		tp->t_flags &= ~TF_WASCRECOVERY;
5911 		if (!IN_FASTRECOVERY(tp->t_flags)) {
5912 			if (rack->dgp_on && rack->r_cwnd_was_clamped) {
5913 				/* Reset the gains so that on exit we will be softer longer */
5914 				rack->r_ctl.rack_per_of_gp_rec = 100;
5915 				rack->r_ctl.rack_per_of_gp_ss = 98;
5916 				rack->r_ctl.rack_per_of_gp_ca = 98;
5917 			}
5918 			rack->r_ctl.rc_prr_delivered = 0;
5919 			rack->r_ctl.rc_prr_out = 0;
5920 			rack->r_fast_output = 0;
5921 			if (rack->rack_no_prr == 0) {
5922 				rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
5923 				rack_log_to_prr(rack, 2, in_rec_at_entry, line);
5924 			}
5925 			rack->r_ctl.rc_prr_recovery_fs = tp->snd_max - tp->snd_una;
5926 			tp->snd_recover = tp->snd_max;
5927 			if (tp->t_flags2 & TF2_ECN_PERMIT)
5928 				tp->t_flags2 |= TF2_ECN_SND_CWR;
5929 		}
5930 		break;
5931 	case CC_ECN:
5932 		if (!IN_CONGRECOVERY(tp->t_flags) ||
5933 		    /*
5934 		     * Allow ECN reaction on ACK to CWR, if
5935 		     * that data segment was also CE marked.
5936 		     */
5937 		    SEQ_GEQ(ack, tp->snd_recover)) {
5938 			EXIT_CONGRECOVERY(tp->t_flags);
5939 			KMOD_TCPSTAT_INC(tcps_ecn_rcwnd);
5940 			rack->r_fast_output = 0;
5941 			tp->snd_recover = tp->snd_max + 1;
5942 			if (tp->t_flags2 & TF2_ECN_PERMIT)
5943 				tp->t_flags2 |= TF2_ECN_SND_CWR;
5944 		}
5945 		break;
5946 	case CC_RTO:
5947 		tp->t_dupacks = 0;
5948 		tp->t_bytes_acked = 0;
5949 		rack->r_fast_output = 0;
5950 		EXIT_RECOVERY(tp->t_flags);
5951 		tp->snd_ssthresh = max(2, min(tp->snd_wnd, rack->r_ctl.cwnd_to_use) / 2 /
5952 		    ctf_fixed_maxseg(tp)) * ctf_fixed_maxseg(tp);
5953 		orig_cwnd = tp->snd_cwnd;
5954 		tp->snd_cwnd = ctf_fixed_maxseg(tp);
5955 		rack_log_to_prr(rack, 16, orig_cwnd, line);
5956 		if (tp->t_flags2 & TF2_ECN_PERMIT)
5957 			tp->t_flags2 |= TF2_ECN_SND_CWR;
5958 		break;
5959 	case CC_RTO_ERR:
5960 		KMOD_TCPSTAT_INC(tcps_sndrexmitbad);
5961 		/* RTO was unnecessary, so reset everything. */
5962 		tp->snd_cwnd = tp->snd_cwnd_prev;
5963 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
5964 		tp->snd_recover = tp->snd_recover_prev;
5965 		if (tp->t_flags & TF_WASFRECOVERY) {
5966 			ENTER_FASTRECOVERY(tp->t_flags);
5967 			tp->t_flags &= ~TF_WASFRECOVERY;
5968 		}
5969 		if (tp->t_flags & TF_WASCRECOVERY) {
5970 			ENTER_CONGRECOVERY(tp->t_flags);
5971 			tp->t_flags &= ~TF_WASCRECOVERY;
5972 		}
5973 		tp->snd_nxt = tp->snd_max;
5974 		tp->t_badrxtwin = 0;
5975 		break;
5976 	}
5977 	if ((CC_ALGO(tp)->cong_signal != NULL)  &&
5978 	    (type != CC_RTO)){
5979 		tp->t_ccv.curack = ack;
5980 		CC_ALGO(tp)->cong_signal(&tp->t_ccv, type);
5981 	}
5982 	if ((in_rec_at_entry == 0) && IN_RECOVERY(tp->t_flags)) {
5983 		rack_log_to_prr(rack, 15, cwnd_enter, line);
5984 		if (rack->r_ctl.full_dgp_in_rec)
5985 			rack_client_buffer_level_set(rack);
5986 		rack->r_ctl.dsack_byte_cnt = 0;
5987 		rack->r_ctl.retran_during_recovery = 0;
5988 		rack->r_ctl.rc_cwnd_at_erec = cwnd_enter;
5989 		rack->r_ctl.rc_ssthresh_at_erec = ssthresh_enter;
5990 		rack->r_ent_rec_ns = 1;
5991 	}
5992 }
5993 
5994 static inline void
5995 rack_cc_after_idle(struct tcp_rack *rack, struct tcpcb *tp)
5996 {
5997 	uint32_t i_cwnd;
5998 
5999 	INP_WLOCK_ASSERT(tptoinpcb(tp));
6000 
6001 	if (CC_ALGO(tp)->after_idle != NULL)
6002 		CC_ALGO(tp)->after_idle(&tp->t_ccv);
6003 
6004 	if (tp->snd_cwnd == 1)
6005 		i_cwnd = tp->t_maxseg;		/* SYN(-ACK) lost */
6006 	else
6007 		i_cwnd = rc_init_window(rack);
6008 
6009 	/*
6010 	 * Being idle is no different than the initial window. If the cc
6011 	 * clamps it down below the initial window raise it to the initial
6012 	 * window.
6013 	 */
6014 	if (tp->snd_cwnd < i_cwnd) {
6015 		tp->snd_cwnd = i_cwnd;
6016 	}
6017 }
6018 
6019 /*
6020  * Indicate whether this ack should be delayed.  We can delay the ack if
6021  * following conditions are met:
6022  *	- There is no delayed ack timer in progress.
6023  *	- Our last ack wasn't a 0-sized window. We never want to delay
6024  *	  the ack that opens up a 0-sized window.
6025  *	- LRO wasn't used for this segment. We make sure by checking that the
6026  *	  segment size is not larger than the MSS.
6027  *	- Delayed acks are enabled or this is a half-synchronized T/TCP
6028  *	  connection.
6029  */
6030 #define DELAY_ACK(tp, tlen)			 \
6031 	(((tp->t_flags & TF_RXWIN0SENT) == 0) && \
6032 	((tp->t_flags & TF_DELACK) == 0) &&	 \
6033 	(tlen <= tp->t_maxseg) &&		 \
6034 	(tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN)))
6035 
6036 static struct rack_sendmap *
6037 rack_find_lowest_rsm(struct tcp_rack *rack)
6038 {
6039 	struct rack_sendmap *rsm;
6040 
6041 	/*
6042 	 * Walk the time-order transmitted list looking for an rsm that is
6043 	 * not acked. This will be the one that was sent the longest time
6044 	 * ago that is still outstanding.
6045 	 */
6046 	TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
6047 		if (rsm->r_flags & RACK_ACKED) {
6048 			continue;
6049 		}
6050 		goto finish;
6051 	}
6052 finish:
6053 	return (rsm);
6054 }
6055 
6056 static struct rack_sendmap *
6057 rack_find_high_nonack(struct tcp_rack *rack, struct rack_sendmap *rsm)
6058 {
6059 	struct rack_sendmap *prsm;
6060 
6061 	/*
6062 	 * Walk the sequence order list backward until we hit and arrive at
6063 	 * the highest seq not acked. In theory when this is called it
6064 	 * should be the last segment (which it was not).
6065 	 */
6066 	prsm = rsm;
6067 
6068 	TQHASH_FOREACH_REVERSE_FROM(prsm, rack->r_ctl.tqh) {
6069 		if (prsm->r_flags & (RACK_ACKED | RACK_HAS_FIN)) {
6070 			continue;
6071 		}
6072 		return (prsm);
6073 	}
6074 	return (NULL);
6075 }
6076 
6077 static uint32_t
6078 rack_calc_thresh_rack(struct tcp_rack *rack, uint32_t srtt, uint32_t cts)
6079 {
6080 	int32_t lro;
6081 	uint32_t thresh;
6082 
6083 	/*
6084 	 * lro is the flag we use to determine if we have seen reordering.
6085 	 * If it gets set we have seen reordering. The reorder logic either
6086 	 * works in one of two ways:
6087 	 *
6088 	 * If reorder-fade is configured, then we track the last time we saw
6089 	 * re-ordering occur. If we reach the point where enough time as
6090 	 * passed we no longer consider reordering has occuring.
6091 	 *
6092 	 * Or if reorder-face is 0, then once we see reordering we consider
6093 	 * the connection to alway be subject to reordering and just set lro
6094 	 * to 1.
6095 	 *
6096 	 * In the end if lro is non-zero we add the extra time for
6097 	 * reordering in.
6098 	 */
6099 	if (srtt == 0)
6100 		srtt = 1;
6101 	if (rack->r_ctl.rc_reorder_ts) {
6102 		if (rack->r_ctl.rc_reorder_fade) {
6103 			if (SEQ_GEQ(cts, rack->r_ctl.rc_reorder_ts)) {
6104 				lro = cts - rack->r_ctl.rc_reorder_ts;
6105 				if (lro == 0) {
6106 					/*
6107 					 * No time as passed since the last
6108 					 * reorder, mark it as reordering.
6109 					 */
6110 					lro = 1;
6111 				}
6112 			} else {
6113 				/* Negative time? */
6114 				lro = 0;
6115 			}
6116 			if (lro > rack->r_ctl.rc_reorder_fade) {
6117 				/* Turn off reordering seen too */
6118 				rack->r_ctl.rc_reorder_ts = 0;
6119 				lro = 0;
6120 			}
6121 		} else {
6122 			/* Reodering does not fade */
6123 			lro = 1;
6124 		}
6125 	} else {
6126 		lro = 0;
6127 	}
6128 	if (rack->rc_rack_tmr_std_based == 0) {
6129 		thresh = srtt + rack->r_ctl.rc_pkt_delay;
6130 	} else {
6131 		/* Standards based pkt-delay is 1/4 srtt */
6132 		thresh = srtt +  (srtt >> 2);
6133 	}
6134 	if (lro && (rack->rc_rack_tmr_std_based == 0)) {
6135 		/* It must be set, if not you get 1/4 rtt */
6136 		if (rack->r_ctl.rc_reorder_shift)
6137 			thresh += (srtt >> rack->r_ctl.rc_reorder_shift);
6138 		else
6139 			thresh += (srtt >> 2);
6140 	}
6141 	if (rack->rc_rack_use_dsack &&
6142 	    lro &&
6143 	    (rack->r_ctl.num_dsack > 0)) {
6144 		/*
6145 		 * We only increase the reordering window if we
6146 		 * have seen reordering <and> we have a DSACK count.
6147 		 */
6148 		thresh += rack->r_ctl.num_dsack * (srtt >> 2);
6149 		rack_log_dsack_event(rack, 4, __LINE__, srtt, thresh);
6150 	}
6151 	/* SRTT * 2 is the ceiling */
6152 	if (thresh > (srtt * 2)) {
6153 		thresh = srtt * 2;
6154 	}
6155 	/* And we don't want it above the RTO max either */
6156 	if (thresh > rack_rto_max) {
6157 		thresh = rack_rto_max;
6158 	}
6159 	rack_log_dsack_event(rack, 6, __LINE__, srtt, thresh);
6160 	return (thresh);
6161 }
6162 
6163 static uint32_t
6164 rack_calc_thresh_tlp(struct tcpcb *tp, struct tcp_rack *rack,
6165 		     struct rack_sendmap *rsm, uint32_t srtt)
6166 {
6167 	struct rack_sendmap *prsm;
6168 	uint32_t thresh, len;
6169 	int segsiz;
6170 
6171 	if (srtt == 0)
6172 		srtt = 1;
6173 	if (rack->r_ctl.rc_tlp_threshold)
6174 		thresh = srtt + (srtt / rack->r_ctl.rc_tlp_threshold);
6175 	else
6176 		thresh = (srtt * 2);
6177 
6178 	/* Get the previous sent packet, if any */
6179 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
6180 	len = rsm->r_end - rsm->r_start;
6181 	if (rack->rack_tlp_threshold_use == TLP_USE_ID) {
6182 		/* Exactly like the ID */
6183 		if (((tp->snd_max - tp->snd_una) - rack->r_ctl.rc_sacked + rack->r_ctl.rc_holes_rxt) <= segsiz) {
6184 			uint32_t alt_thresh;
6185 			/*
6186 			 * Compensate for delayed-ack with the d-ack time.
6187 			 */
6188 			alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
6189 			if (alt_thresh > thresh)
6190 				thresh = alt_thresh;
6191 		}
6192 	} else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_ONE) {
6193 		/* 2.1 behavior */
6194 		prsm = TAILQ_PREV(rsm, rack_head, r_tnext);
6195 		if (prsm && (len <= segsiz)) {
6196 			/*
6197 			 * Two packets outstanding, thresh should be (2*srtt) +
6198 			 * possible inter-packet delay (if any).
6199 			 */
6200 			uint32_t inter_gap = 0;
6201 			int idx, nidx;
6202 
6203 			idx = rsm->r_rtr_cnt - 1;
6204 			nidx = prsm->r_rtr_cnt - 1;
6205 			if (rsm->r_tim_lastsent[nidx] >= prsm->r_tim_lastsent[idx]) {
6206 				/* Yes it was sent later (or at the same time) */
6207 				inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx];
6208 			}
6209 			thresh += inter_gap;
6210 		} else if (len <= segsiz) {
6211 			/*
6212 			 * Possibly compensate for delayed-ack.
6213 			 */
6214 			uint32_t alt_thresh;
6215 
6216 			alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
6217 			if (alt_thresh > thresh)
6218 				thresh = alt_thresh;
6219 		}
6220 	} else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_TWO) {
6221 		/* 2.2 behavior */
6222 		if (len <= segsiz) {
6223 			uint32_t alt_thresh;
6224 			/*
6225 			 * Compensate for delayed-ack with the d-ack time.
6226 			 */
6227 			alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
6228 			if (alt_thresh > thresh)
6229 				thresh = alt_thresh;
6230 		}
6231 	}
6232 	/* Not above an RTO */
6233 	if (thresh > tp->t_rxtcur) {
6234 		thresh = tp->t_rxtcur;
6235 	}
6236 	/* Not above a RTO max */
6237 	if (thresh > rack_rto_max) {
6238 		thresh = rack_rto_max;
6239 	}
6240 	/* Apply user supplied min TLP */
6241 	if (thresh < rack_tlp_min) {
6242 		thresh = rack_tlp_min;
6243 	}
6244 	return (thresh);
6245 }
6246 
6247 static uint32_t
6248 rack_grab_rtt(struct tcpcb *tp, struct tcp_rack *rack)
6249 {
6250 	/*
6251 	 * We want the rack_rtt which is the
6252 	 * last rtt we measured. However if that
6253 	 * does not exist we fallback to the srtt (which
6254 	 * we probably will never do) and then as a last
6255 	 * resort we use RACK_INITIAL_RTO if no srtt is
6256 	 * yet set.
6257 	 */
6258 	if (rack->rc_rack_rtt)
6259 		return (rack->rc_rack_rtt);
6260 	else if (tp->t_srtt == 0)
6261 		return (RACK_INITIAL_RTO);
6262 	return (tp->t_srtt);
6263 }
6264 
6265 static struct rack_sendmap *
6266 rack_check_recovery_mode(struct tcpcb *tp, uint32_t tsused)
6267 {
6268 	/*
6269 	 * Check to see that we don't need to fall into recovery. We will
6270 	 * need to do so if our oldest transmit is past the time we should
6271 	 * have had an ack.
6272 	 */
6273 	struct tcp_rack *rack;
6274 	struct rack_sendmap *rsm;
6275 	int32_t idx;
6276 	uint32_t srtt, thresh;
6277 
6278 	rack = (struct tcp_rack *)tp->t_fb_ptr;
6279 	if (tqhash_empty(rack->r_ctl.tqh)) {
6280 		return (NULL);
6281 	}
6282 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6283 	if (rsm == NULL)
6284 		return (NULL);
6285 
6286 
6287 	if (rsm->r_flags & RACK_ACKED) {
6288 		rsm = rack_find_lowest_rsm(rack);
6289 		if (rsm == NULL)
6290 			return (NULL);
6291 	}
6292 	idx = rsm->r_rtr_cnt - 1;
6293 	srtt = rack_grab_rtt(tp, rack);
6294 	thresh = rack_calc_thresh_rack(rack, srtt, tsused);
6295 	if (TSTMP_LT(tsused, ((uint32_t)rsm->r_tim_lastsent[idx]))) {
6296 		return (NULL);
6297 	}
6298 	if ((tsused - ((uint32_t)rsm->r_tim_lastsent[idx])) < thresh) {
6299 		return (NULL);
6300 	}
6301 	/* Ok if we reach here we are over-due and this guy can be sent */
6302 	rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__);
6303 	return (rsm);
6304 }
6305 
6306 static uint32_t
6307 rack_get_persists_timer_val(struct tcpcb *tp, struct tcp_rack *rack)
6308 {
6309 	int32_t t;
6310 	int32_t tt;
6311 	uint32_t ret_val;
6312 
6313 	t = (tp->t_srtt + (tp->t_rttvar << 2));
6314 	RACK_TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
6315  	    rack_persist_min, rack_persist_max, rack->r_ctl.timer_slop);
6316 	rack->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT;
6317 	ret_val = (uint32_t)tt;
6318 	return (ret_val);
6319 }
6320 
6321 static uint32_t
6322 rack_timer_start(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int sup_rack)
6323 {
6324 	/*
6325 	 * Start the FR timer, we do this based on getting the first one in
6326 	 * the rc_tmap. Note that if its NULL we must stop the timer. in all
6327 	 * events we need to stop the running timer (if its running) before
6328 	 * starting the new one.
6329 	 */
6330 	uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse;
6331 	uint32_t srtt_cur;
6332 	int32_t idx;
6333 	int32_t is_tlp_timer = 0;
6334 	struct rack_sendmap *rsm;
6335 
6336 	if (rack->t_timers_stopped) {
6337 		/* All timers have been stopped none are to run */
6338 		return (0);
6339 	}
6340 	if (rack->rc_in_persist) {
6341 		/* We can't start any timer in persists */
6342 		return (rack_get_persists_timer_val(tp, rack));
6343 	}
6344 	rack->rc_on_min_to = 0;
6345 	if ((tp->t_state < TCPS_ESTABLISHED) ||
6346 	    (rack->sack_attack_disable > 0) ||
6347 	    ((tp->t_flags & TF_SACK_PERMIT) == 0)) {
6348 		goto activate_rxt;
6349 	}
6350 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6351 	if ((rsm == NULL) || sup_rack) {
6352 		/* Nothing on the send map or no rack */
6353 activate_rxt:
6354 		time_since_sent = 0;
6355 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6356 		if (rsm) {
6357 			/*
6358 			 * Should we discount the RTX timer any?
6359 			 *
6360 			 * We want to discount it the smallest amount.
6361 			 * If a timer (Rack/TLP or RXT) has gone off more
6362 			 * recently thats the discount we want to use (now - timer time).
6363 			 * If the retransmit of the oldest packet was more recent then
6364 			 * we want to use that (now - oldest-packet-last_transmit_time).
6365 			 *
6366 			 */
6367 			idx = rsm->r_rtr_cnt - 1;
6368 			if (TSTMP_GEQ(rack->r_ctl.rc_tlp_rxt_last_time, ((uint32_t)rsm->r_tim_lastsent[idx])))
6369 				tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time;
6370 			else
6371 				tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx];
6372 			if (TSTMP_GT(cts, tstmp_touse))
6373 			    time_since_sent = cts - tstmp_touse;
6374 		}
6375 		if (SEQ_LT(tp->snd_una, tp->snd_max) ||
6376 		    sbavail(&tptosocket(tp)->so_snd)) {
6377 			rack->r_ctl.rc_hpts_flags |= PACE_TMR_RXT;
6378 			to = tp->t_rxtcur;
6379 			if (to > time_since_sent)
6380 				to -= time_since_sent;
6381 			else
6382 				to = rack->r_ctl.rc_min_to;
6383 			if (to == 0)
6384 				to = 1;
6385 			/* Special case for KEEPINIT */
6386 			if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) &&
6387 			    (TP_KEEPINIT(tp) != 0) &&
6388 			    rsm) {
6389 				/*
6390 				 * We have to put a ceiling on the rxt timer
6391 				 * of the keep-init timeout.
6392 				 */
6393 				uint32_t max_time, red;
6394 
6395 				max_time = TICKS_2_USEC(TP_KEEPINIT(tp));
6396 				if (TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) {
6397 					red = (cts - (uint32_t)rsm->r_tim_lastsent[0]);
6398 					if (red < max_time)
6399 						max_time -= red;
6400 					else
6401 						max_time = 1;
6402 				}
6403 				/* Reduce timeout to the keep value if needed */
6404 				if (max_time < to)
6405 					to = max_time;
6406 			}
6407 			return (to);
6408 		}
6409 		return (0);
6410 	}
6411 	if (rsm->r_flags & RACK_ACKED) {
6412 		rsm = rack_find_lowest_rsm(rack);
6413 		if (rsm == NULL) {
6414 			/* No lowest? */
6415 			goto activate_rxt;
6416 		}
6417 	}
6418 	if (rack->sack_attack_disable) {
6419 		/*
6420 		 * We don't want to do
6421 		 * any TLP's if you are an attacker.
6422 		 * Though if you are doing what
6423 		 * is expected you may still have
6424 		 * SACK-PASSED marks.
6425 		 */
6426 		goto activate_rxt;
6427 	}
6428 	/* Convert from ms to usecs */
6429 	if ((rsm->r_flags & RACK_SACK_PASSED) ||
6430 	    (rsm->r_flags & RACK_RWND_COLLAPSED) ||
6431 	    (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
6432 		if ((tp->t_flags & TF_SENTFIN) &&
6433 		    ((tp->snd_max - tp->snd_una) == 1) &&
6434 		    (rsm->r_flags & RACK_HAS_FIN)) {
6435 			/*
6436 			 * We don't start a rack timer if all we have is a
6437 			 * FIN outstanding.
6438 			 */
6439 			goto activate_rxt;
6440 		}
6441 		if ((rack->use_rack_rr == 0) &&
6442 		    (IN_FASTRECOVERY(tp->t_flags)) &&
6443 		    (rack->rack_no_prr == 0) &&
6444 		     (rack->r_ctl.rc_prr_sndcnt  < ctf_fixed_maxseg(tp))) {
6445 			/*
6446 			 * We are not cheating, in recovery  and
6447 			 * not enough ack's to yet get our next
6448 			 * retransmission out.
6449 			 *
6450 			 * Note that classified attackers do not
6451 			 * get to use the rack-cheat.
6452 			 */
6453 			goto activate_tlp;
6454 		}
6455 		srtt = rack_grab_rtt(tp, rack);
6456 		thresh = rack_calc_thresh_rack(rack, srtt, cts);
6457 		idx = rsm->r_rtr_cnt - 1;
6458 		exp = ((uint32_t)rsm->r_tim_lastsent[idx]) + thresh;
6459 		if (SEQ_GEQ(exp, cts)) {
6460 			to = exp - cts;
6461 			if (to < rack->r_ctl.rc_min_to) {
6462 				to = rack->r_ctl.rc_min_to;
6463 				if (rack->r_rr_config == 3)
6464 					rack->rc_on_min_to = 1;
6465 			}
6466 		} else {
6467 			to = rack->r_ctl.rc_min_to;
6468 			if (rack->r_rr_config == 3)
6469 				rack->rc_on_min_to = 1;
6470 		}
6471 	} else {
6472 		/* Ok we need to do a TLP not RACK */
6473 activate_tlp:
6474 		if ((rack->rc_tlp_in_progress != 0) &&
6475 		    (rack->r_ctl.rc_tlp_cnt_out >= rack_tlp_limit)) {
6476 			/*
6477 			 * The previous send was a TLP and we have sent
6478 			 * N TLP's without sending new data.
6479 			 */
6480 			goto activate_rxt;
6481 		}
6482 		rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
6483 		if (rsm == NULL) {
6484 			/* We found no rsm to TLP with. */
6485 			goto activate_rxt;
6486 		}
6487 		if (rsm->r_flags & RACK_HAS_FIN) {
6488 			/* If its a FIN we dont do TLP */
6489 			rsm = NULL;
6490 			goto activate_rxt;
6491 		}
6492 		idx = rsm->r_rtr_cnt - 1;
6493 		time_since_sent = 0;
6494 		if (TSTMP_GEQ(((uint32_t)rsm->r_tim_lastsent[idx]), rack->r_ctl.rc_tlp_rxt_last_time))
6495 			tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx];
6496 		else
6497 			tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time;
6498 		if (TSTMP_GT(cts, tstmp_touse))
6499 		    time_since_sent = cts - tstmp_touse;
6500 		is_tlp_timer = 1;
6501 		if (tp->t_srtt) {
6502 			if ((rack->rc_srtt_measure_made == 0) &&
6503 			    (tp->t_srtt == 1)) {
6504 				/*
6505 				 * If another stack as run and set srtt to 1,
6506 				 * then the srtt was 0, so lets use the initial.
6507 				 */
6508 				srtt = RACK_INITIAL_RTO;
6509 			} else {
6510 				srtt_cur = tp->t_srtt;
6511 				srtt = srtt_cur;
6512 			}
6513 		} else
6514 			srtt = RACK_INITIAL_RTO;
6515 		/*
6516 		 * If the SRTT is not keeping up and the
6517 		 * rack RTT has spiked we want to use
6518 		 * the last RTT not the smoothed one.
6519 		 */
6520 		if (rack_tlp_use_greater &&
6521 		    tp->t_srtt &&
6522 		    (srtt < rack_grab_rtt(tp, rack))) {
6523 			srtt = rack_grab_rtt(tp, rack);
6524 		}
6525 		thresh = rack_calc_thresh_tlp(tp, rack, rsm, srtt);
6526 		if (thresh > time_since_sent) {
6527 			to = thresh - time_since_sent;
6528 		} else {
6529 			to = rack->r_ctl.rc_min_to;
6530 			rack_log_alt_to_to_cancel(rack,
6531 						  thresh,		/* flex1 */
6532 						  time_since_sent,	/* flex2 */
6533 						  tstmp_touse,		/* flex3 */
6534 						  rack->r_ctl.rc_tlp_rxt_last_time, /* flex4 */
6535 						  (uint32_t)rsm->r_tim_lastsent[idx],
6536 						  srtt,
6537 						  idx, 99);
6538 		}
6539 		if (to < rack_tlp_min) {
6540 			to = rack_tlp_min;
6541 		}
6542 		if (to > TICKS_2_USEC(TCPTV_REXMTMAX)) {
6543 			/*
6544 			 * If the TLP time works out to larger than the max
6545 			 * RTO lets not do TLP.. just RTO.
6546 			 */
6547 			goto activate_rxt;
6548 		}
6549 	}
6550 	if (is_tlp_timer == 0) {
6551 		rack->r_ctl.rc_hpts_flags |= PACE_TMR_RACK;
6552 	} else {
6553 		rack->r_ctl.rc_hpts_flags |= PACE_TMR_TLP;
6554 	}
6555 	if (to == 0)
6556 		to = 1;
6557 	return (to);
6558 }
6559 
6560 static void
6561 rack_enter_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, tcp_seq snd_una)
6562 {
6563 	struct timeval tv;
6564 
6565 	if (rack->rc_in_persist == 0) {
6566 		if (tp->t_flags & TF_GPUTINPROG) {
6567 			/*
6568 			 * Stop the goodput now, the calling of the
6569 			 * measurement function clears the flag.
6570 			 */
6571 			rack_do_goodput_measurement(tp, rack, tp->snd_una, __LINE__,
6572 						    RACK_QUALITY_PERSIST);
6573 		}
6574 #ifdef NETFLIX_SHARED_CWND
6575 		if (rack->r_ctl.rc_scw) {
6576 			tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
6577 			rack->rack_scwnd_is_idle = 1;
6578 		}
6579 #endif
6580 		rack->r_ctl.rc_went_idle_time = tcp_get_usecs(&tv);
6581 		if (rack->lt_bw_up) {
6582 			/* Suspend our LT BW measurement */
6583 			uint64_t tmark;
6584 
6585 			rack->r_ctl.lt_bw_bytes += (snd_una - rack->r_ctl.lt_seq);
6586 			rack->r_ctl.lt_seq = snd_una;
6587 			tmark = tcp_tv_to_lusectick(&tv);
6588 			rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark);
6589 			rack->r_ctl.lt_timemark = tmark;
6590 			rack->lt_bw_up = 0;
6591 			rack->r_persist_lt_bw_off = 1;
6592 		}
6593 		if (rack->r_ctl.rc_went_idle_time == 0)
6594 			rack->r_ctl.rc_went_idle_time = 1;
6595 		rack_timer_cancel(tp, rack, cts, __LINE__);
6596 		rack->r_ctl.persist_lost_ends = 0;
6597 		rack->probe_not_answered = 0;
6598 		rack->forced_ack = 0;
6599 		tp->t_rxtshift = 0;
6600 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
6601 			      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
6602 		rack->rc_in_persist = 1;
6603 	}
6604 }
6605 
6606 static void
6607 rack_exit_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6608 {
6609 	struct timeval tv;
6610 	uint32_t t_time;
6611 
6612 	if (tcp_in_hpts(rack->rc_tp)) {
6613 		tcp_hpts_remove(rack->rc_tp);
6614 		rack->r_ctl.rc_hpts_flags = 0;
6615 	}
6616 #ifdef NETFLIX_SHARED_CWND
6617 	if (rack->r_ctl.rc_scw) {
6618 		tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
6619 		rack->rack_scwnd_is_idle = 0;
6620 	}
6621 #endif
6622 	t_time = tcp_get_usecs(&tv);
6623 	if (rack->rc_gp_dyn_mul &&
6624 	    (rack->use_fixed_rate == 0) &&
6625 	    (rack->rc_always_pace)) {
6626 		/*
6627 		 * Do we count this as if a probe-rtt just
6628 		 * finished?
6629 		 */
6630 		uint32_t time_idle, idle_min;
6631 
6632 		time_idle = t_time - rack->r_ctl.rc_went_idle_time;
6633 		idle_min = rack_min_probertt_hold;
6634 		if (rack_probertt_gpsrtt_cnt_div) {
6635 			uint64_t extra;
6636 			extra = (uint64_t)rack->r_ctl.rc_gp_srtt *
6637 				(uint64_t)rack_probertt_gpsrtt_cnt_mul;
6638 			extra /= (uint64_t)rack_probertt_gpsrtt_cnt_div;
6639 			idle_min += (uint32_t)extra;
6640 		}
6641 		if (time_idle >= idle_min) {
6642 			/* Yes, we count it as a probe-rtt. */
6643 			uint32_t us_cts;
6644 
6645 			us_cts = tcp_get_usecs(NULL);
6646 			if (rack->in_probe_rtt == 0) {
6647 				rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
6648 				rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts;
6649 				rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts;
6650 				rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts;
6651 			} else {
6652 				rack_exit_probertt(rack, us_cts);
6653 			}
6654 		}
6655 	}
6656 	if (rack->r_persist_lt_bw_off) {
6657 		/* Continue where we left off */
6658 		rack->r_ctl.lt_timemark = tcp_tv_to_lusectick(&tv);
6659 		rack->lt_bw_up = 1;
6660 		rack->r_persist_lt_bw_off = 0;
6661 	}
6662 	rack->rc_in_persist = 0;
6663 	rack->r_ctl.rc_went_idle_time = 0;
6664 	tp->t_rxtshift = 0;
6665 	RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
6666 	   rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
6667 	rack->r_ctl.rc_agg_delayed = 0;
6668 	rack->r_early = 0;
6669 	rack->r_late = 0;
6670 	rack->r_ctl.rc_agg_early = 0;
6671 }
6672 
6673 static void
6674 rack_log_hpts_diag(struct tcp_rack *rack, uint32_t cts,
6675 		   struct hpts_diag *diag, struct timeval *tv)
6676 {
6677 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
6678 		union tcp_log_stackspecific log;
6679 
6680 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
6681 		log.u_bbr.flex1 = diag->p_nxt_slot;
6682 		log.u_bbr.flex2 = diag->p_cur_slot;
6683 		log.u_bbr.flex3 = diag->slot_req;
6684 		log.u_bbr.flex4 = diag->inp_hptsslot;
6685 		log.u_bbr.flex5 = diag->slot_remaining;
6686 		log.u_bbr.flex6 = diag->need_new_to;
6687 		log.u_bbr.flex7 = diag->p_hpts_active;
6688 		log.u_bbr.flex8 = diag->p_on_min_sleep;
6689 		/* Hijack other fields as needed */
6690 		log.u_bbr.epoch = diag->have_slept;
6691 		log.u_bbr.lt_epoch = diag->yet_to_sleep;
6692 		log.u_bbr.pkts_out = diag->co_ret;
6693 		log.u_bbr.applimited = diag->hpts_sleep_time;
6694 		log.u_bbr.delivered = diag->p_prev_slot;
6695 		log.u_bbr.inflight = diag->p_runningslot;
6696 		log.u_bbr.bw_inuse = diag->wheel_slot;
6697 		log.u_bbr.rttProp = diag->wheel_cts;
6698 		log.u_bbr.timeStamp = cts;
6699 		log.u_bbr.delRate = diag->maxslots;
6700 		log.u_bbr.cur_del_rate = diag->p_curtick;
6701 		log.u_bbr.cur_del_rate <<= 32;
6702 		log.u_bbr.cur_del_rate |= diag->p_lasttick;
6703 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
6704 		    &rack->rc_inp->inp_socket->so_rcv,
6705 		    &rack->rc_inp->inp_socket->so_snd,
6706 		    BBR_LOG_HPTSDIAG, 0,
6707 		    0, &log, false, tv);
6708 	}
6709 
6710 }
6711 
6712 static void
6713 rack_log_wakeup(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb, uint32_t len, int type)
6714 {
6715 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
6716 		union tcp_log_stackspecific log;
6717 		struct timeval tv;
6718 
6719 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
6720 		log.u_bbr.flex1 = sb->sb_flags;
6721 		log.u_bbr.flex2 = len;
6722 		log.u_bbr.flex3 = sb->sb_state;
6723 		log.u_bbr.flex8 = type;
6724 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
6725 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
6726 		    &rack->rc_inp->inp_socket->so_rcv,
6727 		    &rack->rc_inp->inp_socket->so_snd,
6728 		    TCP_LOG_SB_WAKE, 0,
6729 		    len, &log, false, &tv);
6730 	}
6731 }
6732 
6733 static void
6734 rack_start_hpts_timer(struct tcp_rack *rack, struct tcpcb *tp, uint32_t cts,
6735       int32_t slot, uint32_t tot_len_this_send, int sup_rack)
6736 {
6737 	struct hpts_diag diag;
6738 	struct inpcb *inp = tptoinpcb(tp);
6739 	struct timeval tv;
6740 	uint32_t delayed_ack = 0;
6741 	uint32_t hpts_timeout;
6742 	uint32_t entry_slot = slot;
6743 	uint8_t stopped;
6744 	uint32_t left = 0;
6745 	uint32_t us_cts;
6746 
6747 	if ((tp->t_state == TCPS_CLOSED) ||
6748 	    (tp->t_state == TCPS_LISTEN)) {
6749 		return;
6750 	}
6751 	if (tcp_in_hpts(tp)) {
6752 		/* Already on the pacer */
6753 		return;
6754 	}
6755 	stopped = rack->rc_tmr_stopped;
6756 	if (stopped && TSTMP_GT(rack->r_ctl.rc_timer_exp, cts)) {
6757 		left = rack->r_ctl.rc_timer_exp - cts;
6758 	}
6759 	rack->r_ctl.rc_timer_exp = 0;
6760 	rack->r_ctl.rc_hpts_flags = 0;
6761 	us_cts = tcp_get_usecs(&tv);
6762 	/* Now early/late accounting */
6763 	rack_log_pacing_delay_calc(rack, entry_slot, slot, 0, 0, 0, 26, __LINE__, NULL, 0);
6764 	if (rack->r_early && (rack->rc_ack_can_sendout_data == 0)) {
6765 		/*
6766 		 * We have a early carry over set,
6767 		 * we can always add more time so we
6768 		 * can always make this compensation.
6769 		 *
6770 		 * Note if ack's are allowed to wake us do not
6771 		 * penalize the next timer for being awoke
6772 		 * by an ack aka the rc_agg_early (non-paced mode).
6773 		 */
6774 		slot += rack->r_ctl.rc_agg_early;
6775 		rack->r_early = 0;
6776 		rack->r_ctl.rc_agg_early = 0;
6777 	}
6778 	if (rack->r_late) {
6779 		/*
6780 		 * This is harder, we can
6781 		 * compensate some but it
6782 		 * really depends on what
6783 		 * the current pacing time is.
6784 		 */
6785 		if (rack->r_ctl.rc_agg_delayed >= slot) {
6786 			/*
6787 			 * We can't compensate for it all.
6788 			 * And we have to have some time
6789 			 * on the clock. We always have a min
6790 			 * 10 slots (10 x 10 i.e. 100 usecs).
6791 			 */
6792 			if (slot <= HPTS_TICKS_PER_SLOT) {
6793 				/* We gain delay */
6794 				rack->r_ctl.rc_agg_delayed += (HPTS_TICKS_PER_SLOT - slot);
6795 				slot = HPTS_TICKS_PER_SLOT;
6796 			} else {
6797 				/* We take off some */
6798 				rack->r_ctl.rc_agg_delayed -= (slot - HPTS_TICKS_PER_SLOT);
6799 				slot = HPTS_TICKS_PER_SLOT;
6800 			}
6801 		} else {
6802 			slot -= rack->r_ctl.rc_agg_delayed;
6803 			rack->r_ctl.rc_agg_delayed = 0;
6804 			/* Make sure we have 100 useconds at minimum */
6805 			if (slot < HPTS_TICKS_PER_SLOT) {
6806 				rack->r_ctl.rc_agg_delayed = HPTS_TICKS_PER_SLOT - slot;
6807 				slot = HPTS_TICKS_PER_SLOT;
6808 			}
6809 			if (rack->r_ctl.rc_agg_delayed == 0)
6810 				rack->r_late = 0;
6811 		}
6812 	}
6813 	hpts_timeout = rack_timer_start(tp, rack, cts, sup_rack);
6814 #ifdef TCP_SAD_DETECTION
6815 	if (rack->sack_attack_disable &&
6816 	    (rack->r_ctl.ack_during_sd > 0) &&
6817 	    (slot < tcp_sad_pacing_interval)) {
6818 		/*
6819 		 * We have a potential attacker on
6820 		 * the line. We have possibly some
6821 		 * (or now) pacing time set. We want to
6822 		 * slow down the processing of sacks by some
6823 		 * amount (if it is an attacker). Set the default
6824 		 * slot for attackers in place (unless the original
6825 		 * interval is longer). Its stored in
6826 		 * micro-seconds, so lets convert to msecs.
6827 		 */
6828 		slot = tcp_sad_pacing_interval;
6829 		rack_log_type_bbrsnd(rack, tot_len_this_send, slot, us_cts, &tv, __LINE__);
6830 		rack->r_ctl.ack_during_sd = 0;
6831 	}
6832 #endif
6833 	if (tp->t_flags & TF_DELACK) {
6834 		delayed_ack = TICKS_2_USEC(tcp_delacktime);
6835 		rack->r_ctl.rc_hpts_flags |= PACE_TMR_DELACK;
6836 	}
6837 	if (delayed_ack && ((hpts_timeout == 0) ||
6838 			    (delayed_ack < hpts_timeout)))
6839 		hpts_timeout = delayed_ack;
6840 	else
6841 		rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
6842 	/*
6843 	 * If no timers are going to run and we will fall off the hptsi
6844 	 * wheel, we resort to a keep-alive timer if its configured.
6845 	 */
6846 	if ((hpts_timeout == 0) &&
6847 	    (slot == 0)) {
6848 		if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
6849 		    (tp->t_state <= TCPS_CLOSING)) {
6850 			/*
6851 			 * Ok we have no timer (persists, rack, tlp, rxt  or
6852 			 * del-ack), we don't have segments being paced. So
6853 			 * all that is left is the keepalive timer.
6854 			 */
6855 			if (TCPS_HAVEESTABLISHED(tp->t_state)) {
6856 				/* Get the established keep-alive time */
6857 				hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp));
6858 			} else {
6859 				/*
6860 				 * Get the initial setup keep-alive time,
6861 				 * note that this is probably not going to
6862 				 * happen, since rack will be running a rxt timer
6863 				 * if a SYN of some sort is outstanding. It is
6864 				 * actually handled in rack_timeout_rxt().
6865 				 */
6866 				hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp));
6867 			}
6868 			rack->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP;
6869 			if (rack->in_probe_rtt) {
6870 				/*
6871 				 * We want to instead not wake up a long time from
6872 				 * now but to wake up about the time we would
6873 				 * exit probe-rtt and initiate a keep-alive ack.
6874 				 * This will get us out of probe-rtt and update
6875 				 * our min-rtt.
6876 				 */
6877 				hpts_timeout = rack_min_probertt_hold;
6878 			}
6879 		}
6880 	}
6881 	if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) ==
6882 	    (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) {
6883 		/*
6884 		 * RACK, TLP, persists and RXT timers all are restartable
6885 		 * based on actions input .. i.e we received a packet (ack
6886 		 * or sack) and that changes things (rw, or snd_una etc).
6887 		 * Thus we can restart them with a new value. For
6888 		 * keep-alive, delayed_ack we keep track of what was left
6889 		 * and restart the timer with a smaller value.
6890 		 */
6891 		if (left < hpts_timeout)
6892 			hpts_timeout = left;
6893 	}
6894 	if (hpts_timeout) {
6895 		/*
6896 		 * Hack alert for now we can't time-out over 2,147,483
6897 		 * seconds (a bit more than 596 hours), which is probably ok
6898 		 * :).
6899 		 */
6900 		if (hpts_timeout > 0x7ffffffe)
6901 			hpts_timeout = 0x7ffffffe;
6902 		rack->r_ctl.rc_timer_exp = cts + hpts_timeout;
6903 	}
6904 	rack_log_pacing_delay_calc(rack, entry_slot, slot, hpts_timeout, 0, 0, 27, __LINE__, NULL, 0);
6905 	if ((rack->gp_ready == 0) &&
6906 	    (rack->use_fixed_rate == 0) &&
6907 	    (hpts_timeout < slot) &&
6908 	    (rack->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) {
6909 		/*
6910 		 * We have no good estimate yet for the
6911 		 * old clunky burst mitigation or the
6912 		 * real pacing. And the tlp or rxt is smaller
6913 		 * than the pacing calculation. Lets not
6914 		 * pace that long since we know the calculation
6915 		 * so far is not accurate.
6916 		 */
6917 		slot = hpts_timeout;
6918 	}
6919 	/**
6920 	 * Turn off all the flags for queuing by default. The
6921 	 * flags have important meanings to what happens when
6922 	 * LRO interacts with the transport. Most likely (by default now)
6923 	 * mbuf_queueing and ack compression are on. So the transport
6924 	 * has a couple of flags that control what happens (if those
6925 	 * are not on then these flags won't have any effect since it
6926 	 * won't go through the queuing LRO path).
6927 	 *
6928 	 * TF2_MBUF_QUEUE_READY - This flags says that I am busy
6929 	 *                        pacing output, so don't disturb. But
6930 	 *                        it also means LRO can wake me if there
6931 	 *                        is a SACK arrival.
6932 	 *
6933 	 * TF2_DONT_SACK_QUEUE - This flag is used in conjunction
6934 	 *                       with the above flag (QUEUE_READY) and
6935 	 *                       when present it says don't even wake me
6936 	 *                       if a SACK arrives.
6937 	 *
6938 	 * The idea behind these flags is that if we are pacing we
6939 	 * set the MBUF_QUEUE_READY and only get woken up if
6940 	 * a SACK arrives (which could change things) or if
6941 	 * our pacing timer expires. If, however, we have a rack
6942 	 * timer running, then we don't even want a sack to wake
6943 	 * us since the rack timer has to expire before we can send.
6944 	 *
6945 	 * Other cases should usually have none of the flags set
6946 	 * so LRO can call into us.
6947 	 */
6948 	tp->t_flags2 &= ~(TF2_DONT_SACK_QUEUE|TF2_MBUF_QUEUE_READY);
6949 	if (slot) {
6950 		rack->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT;
6951 		rack->r_ctl.rc_last_output_to = us_cts + slot;
6952 		/*
6953 		 * A pacing timer (slot) is being set, in
6954 		 * such a case we cannot send (we are blocked by
6955 		 * the timer). So lets tell LRO that it should not
6956 		 * wake us unless there is a SACK. Note this only
6957 		 * will be effective if mbuf queueing is on or
6958 		 * compressed acks are being processed.
6959 		 */
6960 		tp->t_flags2 |= TF2_MBUF_QUEUE_READY;
6961 		/*
6962 		 * But wait if we have a Rack timer running
6963 		 * even a SACK should not disturb us (with
6964 		 * the exception of r_rr_config 3).
6965 		 */
6966 		if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK) ||
6967 		    (IN_RECOVERY(tp->t_flags))) {
6968 			if (rack->r_rr_config != 3)
6969 				tp->t_flags2 |= TF2_DONT_SACK_QUEUE;
6970 			else if (rack->rc_pace_dnd) {
6971 				/*
6972 				 * When DND is on, we only let a sack
6973 				 * interrupt us if we are not in recovery.
6974 				 *
6975 				 * If DND is off, then we never hit here
6976 				 * and let all sacks wake us up.
6977 				 *
6978 				 */
6979 				tp->t_flags2 |= TF2_DONT_SACK_QUEUE;
6980 			}
6981 		}
6982 		/* For sack attackers we want to ignore sack */
6983 		if (rack->sack_attack_disable == 1) {
6984 			tp->t_flags2 |= (TF2_DONT_SACK_QUEUE |
6985 			    TF2_MBUF_QUEUE_READY);
6986 		} else if (rack->rc_ack_can_sendout_data) {
6987 			/*
6988 			 * Ahh but wait, this is that special case
6989 			 * where the pacing timer can be disturbed
6990 			 * backout the changes (used for non-paced
6991 			 * burst limiting).
6992 			 */
6993 			tp->t_flags2 &= ~(TF2_DONT_SACK_QUEUE |
6994 			    TF2_MBUF_QUEUE_READY);
6995 		}
6996 		if ((rack->use_rack_rr) &&
6997 		    (rack->r_rr_config < 2) &&
6998 		    ((hpts_timeout) && (hpts_timeout < slot))) {
6999 			/*
7000 			 * Arrange for the hpts to kick back in after the
7001 			 * t-o if the t-o does not cause a send.
7002 			 */
7003 			(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(hpts_timeout),
7004 						   __LINE__, &diag);
7005 			rack_log_hpts_diag(rack, us_cts, &diag, &tv);
7006 			rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
7007 		} else {
7008 			(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(slot),
7009 						   __LINE__, &diag);
7010 			rack_log_hpts_diag(rack, us_cts, &diag, &tv);
7011 			rack_log_to_start(rack, cts, hpts_timeout, slot, 1);
7012 		}
7013 	} else if (hpts_timeout) {
7014 		/*
7015 		 * With respect to t_flags2(?) here, lets let any new acks wake
7016 		 * us up here. Since we are not pacing (no pacing timer), output
7017 		 * can happen so we should let it. If its a Rack timer, then any inbound
7018 		 * packet probably won't change the sending (we will be blocked)
7019 		 * but it may change the prr stats so letting it in (the set defaults
7020 		 * at the start of this block) are good enough.
7021 		 */
7022 		rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
7023 		(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(hpts_timeout),
7024 					   __LINE__, &diag);
7025 		rack_log_hpts_diag(rack, us_cts, &diag, &tv);
7026 		rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
7027 	} else {
7028 		/* No timer starting */
7029 #ifdef INVARIANTS
7030 		if (SEQ_GT(tp->snd_max, tp->snd_una)) {
7031 			panic("tp:%p rack:%p tlts:%d cts:%u slot:%u pto:%u -- no timer started?",
7032 			    tp, rack, tot_len_this_send, cts, slot, hpts_timeout);
7033 		}
7034 #endif
7035 	}
7036 	rack->rc_tmr_stopped = 0;
7037 	if (slot)
7038 		rack_log_type_bbrsnd(rack, tot_len_this_send, slot, us_cts, &tv, __LINE__);
7039 }
7040 
7041 /*
7042  * RACK Timer, here we simply do logging and house keeping.
7043  * the normal rack_output() function will call the
7044  * appropriate thing to check if we need to do a RACK retransmit.
7045  * We return 1, saying don't proceed with rack_output only
7046  * when all timers have been stopped (destroyed PCB?).
7047  */
7048 static int
7049 rack_timeout_rack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
7050 {
7051 	/*
7052 	 * This timer simply provides an internal trigger to send out data.
7053 	 * The check_recovery_mode call will see if there are needed
7054 	 * retransmissions, if so we will enter fast-recovery. The output
7055 	 * call may or may not do the same thing depending on sysctl
7056 	 * settings.
7057 	 */
7058 	struct rack_sendmap *rsm;
7059 
7060 	counter_u64_add(rack_to_tot, 1);
7061 	if (rack->r_state && (rack->r_state != tp->t_state))
7062 		rack_set_state(tp, rack);
7063 	rack->rc_on_min_to = 0;
7064 	rsm = rack_check_recovery_mode(tp, cts);
7065 	rack_log_to_event(rack, RACK_TO_FRM_RACK, rsm);
7066 	if (rsm) {
7067 		rack->r_ctl.rc_resend = rsm;
7068 		rack->r_timer_override = 1;
7069 		if (rack->use_rack_rr) {
7070 			/*
7071 			 * Don't accumulate extra pacing delay
7072 			 * we are allowing the rack timer to
7073 			 * over-ride pacing i.e. rrr takes precedence
7074 			 * if the pacing interval is longer than the rrr
7075 			 * time (in other words we get the min pacing
7076 			 * time versus rrr pacing time).
7077 			 */
7078 			rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
7079 		}
7080 	}
7081 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK;
7082 	if (rsm == NULL) {
7083 		/* restart a timer and return 1 */
7084 		rack_start_hpts_timer(rack, tp, cts,
7085 				      0, 0, 0);
7086 		return (1);
7087 	}
7088 	return (0);
7089 }
7090 
7091 
7092 
7093 static void
7094 rack_adjust_orig_mlen(struct rack_sendmap *rsm)
7095 {
7096 
7097 	if ((M_TRAILINGROOM(rsm->m) != rsm->orig_t_space)) {
7098 		/*
7099 		 * The trailing space changed, mbufs can grow
7100 		 * at the tail but they can't shrink from
7101 		 * it, KASSERT that. Adjust the orig_m_len to
7102 		 * compensate for this change.
7103 		 */
7104 		KASSERT((rsm->orig_t_space > M_TRAILINGROOM(rsm->m)),
7105 			("mbuf:%p rsm:%p trailing_space:%jd ots:%u oml:%u mlen:%u\n",
7106 			 rsm->m,
7107 			 rsm,
7108 			 (intmax_t)M_TRAILINGROOM(rsm->m),
7109 			 rsm->orig_t_space,
7110 			 rsm->orig_m_len,
7111 			 rsm->m->m_len));
7112 		rsm->orig_m_len += (rsm->orig_t_space - M_TRAILINGROOM(rsm->m));
7113 		rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
7114 	}
7115 	if (rsm->m->m_len < rsm->orig_m_len) {
7116 		/*
7117 		 * Mbuf shrank, trimmed off the top by an ack, our
7118 		 * offset changes.
7119 		 */
7120 		KASSERT((rsm->soff >= (rsm->orig_m_len - rsm->m->m_len)),
7121 			("mbuf:%p len:%u rsm:%p oml:%u soff:%u\n",
7122 			 rsm->m, rsm->m->m_len,
7123 			 rsm, rsm->orig_m_len,
7124 			 rsm->soff));
7125 		if (rsm->soff >= (rsm->orig_m_len - rsm->m->m_len))
7126 			rsm->soff -= (rsm->orig_m_len - rsm->m->m_len);
7127 		else
7128 			rsm->soff = 0;
7129 		rsm->orig_m_len = rsm->m->m_len;
7130 #ifdef INVARIANTS
7131 	} else if (rsm->m->m_len > rsm->orig_m_len) {
7132 		panic("rsm:%p m:%p m_len grew outside of t_space compensation",
7133 		      rsm, rsm->m);
7134 #endif
7135 	}
7136 }
7137 
7138 static void
7139 rack_setup_offset_for_rsm(struct tcp_rack *rack, struct rack_sendmap *src_rsm, struct rack_sendmap *rsm)
7140 {
7141 	struct mbuf *m;
7142 	uint32_t soff;
7143 
7144 	if (src_rsm->m &&
7145 	    ((src_rsm->orig_m_len != src_rsm->m->m_len) ||
7146 	     (M_TRAILINGROOM(src_rsm->m) != src_rsm->orig_t_space))) {
7147 		/* Fix up the orig_m_len and possibly the mbuf offset */
7148 		rack_adjust_orig_mlen(src_rsm);
7149 	}
7150 	m = src_rsm->m;
7151 	soff = src_rsm->soff + (src_rsm->r_end - src_rsm->r_start);
7152 	while (soff >= m->m_len) {
7153 		/* Move out past this mbuf */
7154 		soff -= m->m_len;
7155 		m = m->m_next;
7156 		KASSERT((m != NULL),
7157 			("rsm:%p nrsm:%p hit at soff:%u null m",
7158 			 src_rsm, rsm, soff));
7159 		if (m == NULL) {
7160 			/* This should *not* happen which is why there is a kassert */
7161 			src_rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd,
7162 					       (src_rsm->r_start - rack->rc_tp->snd_una),
7163 					       &src_rsm->soff);
7164 			src_rsm->orig_m_len = src_rsm->m->m_len;
7165 			src_rsm->orig_t_space = M_TRAILINGROOM(src_rsm->m);
7166 			rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd,
7167 					   (rsm->r_start - rack->rc_tp->snd_una),
7168 					   &rsm->soff);
7169 			rsm->orig_m_len = rsm->m->m_len;
7170 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
7171 			return;
7172 		}
7173 	}
7174 	rsm->m = m;
7175 	rsm->soff = soff;
7176 	rsm->orig_m_len = m->m_len;
7177 	rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
7178 }
7179 
7180 static __inline void
7181 rack_clone_rsm(struct tcp_rack *rack, struct rack_sendmap *nrsm,
7182 	       struct rack_sendmap *rsm, uint32_t start)
7183 {
7184 	int idx;
7185 
7186 	nrsm->r_start = start;
7187 	nrsm->r_end = rsm->r_end;
7188 	nrsm->r_rtr_cnt = rsm->r_rtr_cnt;
7189 	nrsm->r_flags = rsm->r_flags;
7190 	nrsm->r_dupack = rsm->r_dupack;
7191 	nrsm->r_no_rtt_allowed = rsm->r_no_rtt_allowed;
7192 	nrsm->r_rtr_bytes = 0;
7193 	nrsm->r_fas = rsm->r_fas;
7194 	nrsm->r_bas = rsm->r_bas;
7195 	rsm->r_end = nrsm->r_start;
7196 	nrsm->r_just_ret = rsm->r_just_ret;
7197 	for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) {
7198 		nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx];
7199 	}
7200 	/* Now if we have SYN flag we keep it on the left edge */
7201 	if (nrsm->r_flags & RACK_HAS_SYN)
7202 		nrsm->r_flags &= ~RACK_HAS_SYN;
7203 	/* Now if we have a FIN flag we keep it on the right edge */
7204 	if (rsm->r_flags & RACK_HAS_FIN)
7205 		rsm->r_flags &= ~RACK_HAS_FIN;
7206 	/* Push bit must go to the right edge as well */
7207 	if (rsm->r_flags & RACK_HAD_PUSH)
7208 		rsm->r_flags &= ~RACK_HAD_PUSH;
7209 	/* Clone over the state of the hw_tls flag */
7210 	nrsm->r_hw_tls = rsm->r_hw_tls;
7211 	/*
7212 	 * Now we need to find nrsm's new location in the mbuf chain
7213 	 * we basically calculate a new offset, which is soff +
7214 	 * how much is left in original rsm. Then we walk out the mbuf
7215 	 * chain to find the righ position, it may be the same mbuf
7216 	 * or maybe not.
7217 	 */
7218 	KASSERT(((rsm->m != NULL) ||
7219 		 (rsm->r_flags & (RACK_HAS_SYN|RACK_HAS_FIN))),
7220 		("rsm:%p nrsm:%p rack:%p -- rsm->m is NULL?", rsm, nrsm, rack));
7221 	if (rsm->m)
7222 		rack_setup_offset_for_rsm(rack, rsm, nrsm);
7223 }
7224 
7225 static struct rack_sendmap *
7226 rack_merge_rsm(struct tcp_rack *rack,
7227 	       struct rack_sendmap *l_rsm,
7228 	       struct rack_sendmap *r_rsm)
7229 {
7230 	/*
7231 	 * We are merging two ack'd RSM's,
7232 	 * the l_rsm is on the left (lower seq
7233 	 * values) and the r_rsm is on the right
7234 	 * (higher seq value). The simplest way
7235 	 * to merge these is to move the right
7236 	 * one into the left. I don't think there
7237 	 * is any reason we need to try to find
7238 	 * the oldest (or last oldest retransmitted).
7239 	 */
7240 	rack_log_map_chg(rack->rc_tp, rack, NULL,
7241 			 l_rsm, r_rsm, MAP_MERGE, r_rsm->r_end, __LINE__);
7242 	l_rsm->r_end = r_rsm->r_end;
7243 	if (l_rsm->r_dupack < r_rsm->r_dupack)
7244 		l_rsm->r_dupack = r_rsm->r_dupack;
7245 	if (r_rsm->r_rtr_bytes)
7246 		l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes;
7247 	if (r_rsm->r_in_tmap) {
7248 		/* This really should not happen */
7249 		TAILQ_REMOVE(&rack->r_ctl.rc_tmap, r_rsm, r_tnext);
7250 		r_rsm->r_in_tmap = 0;
7251 	}
7252 
7253 	/* Now the flags */
7254 	if (r_rsm->r_flags & RACK_HAS_FIN)
7255 		l_rsm->r_flags |= RACK_HAS_FIN;
7256 	if (r_rsm->r_flags & RACK_TLP)
7257 		l_rsm->r_flags |= RACK_TLP;
7258 	if (r_rsm->r_flags & RACK_RWND_COLLAPSED)
7259 		l_rsm->r_flags |= RACK_RWND_COLLAPSED;
7260 	if ((r_rsm->r_flags & RACK_APP_LIMITED)  &&
7261 	    ((l_rsm->r_flags & RACK_APP_LIMITED) == 0)) {
7262 		/*
7263 		 * If both are app-limited then let the
7264 		 * free lower the count. If right is app
7265 		 * limited and left is not, transfer.
7266 		 */
7267 		l_rsm->r_flags |= RACK_APP_LIMITED;
7268 		r_rsm->r_flags &= ~RACK_APP_LIMITED;
7269 		if (r_rsm == rack->r_ctl.rc_first_appl)
7270 			rack->r_ctl.rc_first_appl = l_rsm;
7271 	}
7272 	tqhash_remove(rack->r_ctl.tqh, r_rsm, REMOVE_TYPE_MERGE);
7273 	/*
7274 	 * We keep the largest value, which is the newest
7275 	 * send. We do this in case a segment that is
7276 	 * joined together and not part of a GP estimate
7277 	 * later gets expanded into the GP estimate.
7278 	 *
7279 	 * We prohibit the merging of unlike kinds i.e.
7280 	 * all pieces that are in the GP estimate can be
7281 	 * merged and all pieces that are not in a GP estimate
7282 	 * can be merged, but not disimilar pieces. Combine
7283 	 * this with taking the highest here and we should
7284 	 * be ok unless of course the client reneges. Then
7285 	 * all bets are off.
7286 	 */
7287 	if(l_rsm->r_tim_lastsent[(l_rsm->r_rtr_cnt-1)] <
7288 	   r_rsm->r_tim_lastsent[(r_rsm->r_rtr_cnt-1)]) {
7289 		l_rsm->r_tim_lastsent[(l_rsm->r_rtr_cnt-1)] = r_rsm->r_tim_lastsent[(r_rsm->r_rtr_cnt-1)];
7290 	}
7291 	/*
7292 	 * When merging two RSM's we also need to consider the ack time and keep
7293 	 * newest. If the ack gets merged into a measurement then that is the
7294 	 * one we will want to be using.
7295 	 */
7296 	if(l_rsm->r_ack_arrival	 < r_rsm->r_ack_arrival)
7297 		l_rsm->r_ack_arrival = r_rsm->r_ack_arrival;
7298 
7299 	if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) {
7300 		/* Transfer the split limit to the map we free */
7301 		r_rsm->r_limit_type = l_rsm->r_limit_type;
7302 		l_rsm->r_limit_type = 0;
7303 	}
7304 	rack_free(rack, r_rsm);
7305 	l_rsm->r_flags |= RACK_MERGED;
7306 	return (l_rsm);
7307 }
7308 
7309 /*
7310  * TLP Timer, here we simply setup what segment we want to
7311  * have the TLP expire on, the normal rack_output() will then
7312  * send it out.
7313  *
7314  * We return 1, saying don't proceed with rack_output only
7315  * when all timers have been stopped (destroyed PCB?).
7316  */
7317 static int
7318 rack_timeout_tlp(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t *doing_tlp)
7319 {
7320 	/*
7321 	 * Tail Loss Probe.
7322 	 */
7323 	struct rack_sendmap *rsm = NULL;
7324 	int insret __diagused;
7325 	struct socket *so = tptosocket(tp);
7326 	uint32_t amm;
7327 	uint32_t out, avail;
7328 	int collapsed_win = 0;
7329 
7330 	if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
7331 		/* Its not time yet */
7332 		return (0);
7333 	}
7334 	if (ctf_progress_timeout_check(tp, true)) {
7335 		rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
7336 		return (-ETIMEDOUT);	/* tcp_drop() */
7337 	}
7338 	/*
7339 	 * A TLP timer has expired. We have been idle for 2 rtts. So we now
7340 	 * need to figure out how to force a full MSS segment out.
7341 	 */
7342 	rack_log_to_event(rack, RACK_TO_FRM_TLP, NULL);
7343 	rack->r_ctl.retran_during_recovery = 0;
7344 	rack->r_ctl.dsack_byte_cnt = 0;
7345 	counter_u64_add(rack_tlp_tot, 1);
7346 	if (rack->r_state && (rack->r_state != tp->t_state))
7347 		rack_set_state(tp, rack);
7348 	avail = sbavail(&so->so_snd);
7349 	out = tp->snd_max - tp->snd_una;
7350 	if ((out > tp->snd_wnd) || rack->rc_has_collapsed) {
7351 		/* special case, we need a retransmission */
7352 		collapsed_win = 1;
7353 		goto need_retran;
7354 	}
7355 	if (rack->r_ctl.dsack_persist && (rack->r_ctl.rc_tlp_cnt_out >= 1)) {
7356 		rack->r_ctl.dsack_persist--;
7357 		if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
7358 			rack->r_ctl.num_dsack = 0;
7359 		}
7360 		rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
7361 	}
7362 	if ((tp->t_flags & TF_GPUTINPROG) &&
7363 	    (rack->r_ctl.rc_tlp_cnt_out == 1)) {
7364 		/*
7365 		 * If this is the second in a row
7366 		 * TLP and we are doing a measurement
7367 		 * its time to abandon the measurement.
7368 		 * Something is likely broken on
7369 		 * the clients network and measuring a
7370 		 * broken network does us no good.
7371 		 */
7372 		tp->t_flags &= ~TF_GPUTINPROG;
7373 		rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
7374 					   rack->r_ctl.rc_gp_srtt /*flex1*/,
7375 					   tp->gput_seq,
7376 					   0, 0, 18, __LINE__, NULL, 0);
7377 	}
7378 	/*
7379 	 * Check our send oldest always settings, and if
7380 	 * there is an oldest to send jump to the need_retran.
7381 	 */
7382 	if (rack_always_send_oldest && (TAILQ_EMPTY(&rack->r_ctl.rc_tmap) == 0))
7383 		goto need_retran;
7384 
7385 	if (avail > out) {
7386 		/* New data is available */
7387 		amm = avail - out;
7388 		if (amm > ctf_fixed_maxseg(tp)) {
7389 			amm = ctf_fixed_maxseg(tp);
7390 			if ((amm + out) > tp->snd_wnd) {
7391 				/* We are rwnd limited */
7392 				goto need_retran;
7393 			}
7394 		} else if (amm < ctf_fixed_maxseg(tp)) {
7395 			/* not enough to fill a MTU */
7396 			goto need_retran;
7397 		}
7398 		if (IN_FASTRECOVERY(tp->t_flags)) {
7399 			/* Unlikely */
7400 			if (rack->rack_no_prr == 0) {
7401 				if (out + amm <= tp->snd_wnd) {
7402 					rack->r_ctl.rc_prr_sndcnt = amm;
7403 					rack->r_ctl.rc_tlp_new_data = amm;
7404 					rack_log_to_prr(rack, 4, 0, __LINE__);
7405 				}
7406 			} else
7407 				goto need_retran;
7408 		} else {
7409 			/* Set the send-new override */
7410 			if (out + amm <= tp->snd_wnd)
7411 				rack->r_ctl.rc_tlp_new_data = amm;
7412 			else
7413 				goto need_retran;
7414 		}
7415 		rack->r_ctl.rc_tlpsend = NULL;
7416 		counter_u64_add(rack_tlp_newdata, 1);
7417 		goto send;
7418 	}
7419 need_retran:
7420 	/*
7421 	 * Ok we need to arrange the last un-acked segment to be re-sent, or
7422 	 * optionally the first un-acked segment.
7423 	 */
7424 	if (collapsed_win == 0) {
7425 		if (rack_always_send_oldest)
7426 			rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
7427 		else {
7428 			rsm = tqhash_max(rack->r_ctl.tqh);
7429 			if (rsm && (rsm->r_flags & (RACK_ACKED | RACK_HAS_FIN))) {
7430 				rsm = rack_find_high_nonack(rack, rsm);
7431 			}
7432 		}
7433 		if (rsm == NULL) {
7434 #ifdef TCP_BLACKBOX
7435 			tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true);
7436 #endif
7437 			goto out;
7438 		}
7439 	} else {
7440 		/*
7441 		 * We had a collapsed window, lets find
7442 		 * the point before the collapse.
7443 		 */
7444 		if (SEQ_GT((rack->r_ctl.last_collapse_point - 1), rack->rc_tp->snd_una))
7445 			rsm = tqhash_find(rack->r_ctl.tqh, (rack->r_ctl.last_collapse_point - 1));
7446 		else {
7447 			rsm = tqhash_min(rack->r_ctl.tqh);
7448 		}
7449 		if (rsm == NULL) {
7450 			/* Huh */
7451 			goto out;
7452 		}
7453 	}
7454 	if ((rsm->r_end - rsm->r_start) > ctf_fixed_maxseg(tp)) {
7455 		/*
7456 		 * We need to split this the last segment in two.
7457 		 */
7458 		struct rack_sendmap *nrsm;
7459 
7460 		nrsm = rack_alloc_full_limit(rack);
7461 		if (nrsm == NULL) {
7462 			/*
7463 			 * No memory to split, we will just exit and punt
7464 			 * off to the RXT timer.
7465 			 */
7466 			goto out;
7467 		}
7468 		rack_clone_rsm(rack, nrsm, rsm,
7469 			       (rsm->r_end - ctf_fixed_maxseg(tp)));
7470 		rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
7471 #ifndef INVARIANTS
7472 		(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
7473 #else
7474 		if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
7475 			panic("Insert in rb tree of %p fails ret:%d rack:%p rsm:%p",
7476 			      nrsm, insret, rack, rsm);
7477 		}
7478 #endif
7479 		if (rsm->r_in_tmap) {
7480 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7481 			nrsm->r_in_tmap = 1;
7482 		}
7483 		rsm = nrsm;
7484 	}
7485 	rack->r_ctl.rc_tlpsend = rsm;
7486 send:
7487 	/* Make sure output path knows we are doing a TLP */
7488 	*doing_tlp = 1;
7489 	rack->r_timer_override = 1;
7490 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
7491 	return (0);
7492 out:
7493 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
7494 	return (0);
7495 }
7496 
7497 /*
7498  * Delayed ack Timer, here we simply need to setup the
7499  * ACK_NOW flag and remove the DELACK flag. From there
7500  * the output routine will send the ack out.
7501  *
7502  * We only return 1, saying don't proceed, if all timers
7503  * are stopped (destroyed PCB?).
7504  */
7505 static int
7506 rack_timeout_delack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
7507 {
7508 
7509 	rack_log_to_event(rack, RACK_TO_FRM_DELACK, NULL);
7510 	tp->t_flags &= ~TF_DELACK;
7511 	tp->t_flags |= TF_ACKNOW;
7512 	KMOD_TCPSTAT_INC(tcps_delack);
7513 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
7514 	return (0);
7515 }
7516 
7517 /*
7518  * Persists timer, here we simply send the
7519  * same thing as a keepalive will.
7520  * the one byte send.
7521  *
7522  * We only return 1, saying don't proceed, if all timers
7523  * are stopped (destroyed PCB?).
7524  */
7525 static int
7526 rack_timeout_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
7527 {
7528 	struct tcptemp *t_template;
7529 	int32_t retval = 1;
7530 
7531 	if (rack->rc_in_persist == 0)
7532 		return (0);
7533 	if (ctf_progress_timeout_check(tp, false)) {
7534 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
7535 		rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
7536 		counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
7537 		return (-ETIMEDOUT);	/* tcp_drop() */
7538 	}
7539 	/*
7540 	 * Persistence timer into zero window. Force a byte to be output, if
7541 	 * possible.
7542 	 */
7543 	KMOD_TCPSTAT_INC(tcps_persisttimeo);
7544 	/*
7545 	 * Hack: if the peer is dead/unreachable, we do not time out if the
7546 	 * window is closed.  After a full backoff, drop the connection if
7547 	 * the idle time (no responses to probes) reaches the maximum
7548 	 * backoff that we would use if retransmitting.
7549 	 */
7550 	if (tp->t_rxtshift >= V_tcp_retries &&
7551 	    (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
7552 	     TICKS_2_USEC(ticks - tp->t_rcvtime) >= RACK_REXMTVAL(tp) * tcp_totbackoff)) {
7553 		KMOD_TCPSTAT_INC(tcps_persistdrop);
7554 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
7555 		counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
7556 		retval = -ETIMEDOUT;	/* tcp_drop() */
7557 		goto out;
7558 	}
7559 	if ((sbavail(&rack->rc_inp->inp_socket->so_snd) == 0) &&
7560 	    tp->snd_una == tp->snd_max)
7561 		rack_exit_persist(tp, rack, cts);
7562 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT;
7563 	/*
7564 	 * If the user has closed the socket then drop a persisting
7565 	 * connection after a much reduced timeout.
7566 	 */
7567 	if (tp->t_state > TCPS_CLOSE_WAIT &&
7568 	    (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
7569 		KMOD_TCPSTAT_INC(tcps_persistdrop);
7570 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
7571 		counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
7572 		retval = -ETIMEDOUT;	/* tcp_drop() */
7573 		goto out;
7574 	}
7575 	t_template = tcpip_maketemplate(rack->rc_inp);
7576 	if (t_template) {
7577 		/* only set it if we were answered */
7578 		if (rack->forced_ack == 0) {
7579 			rack->forced_ack = 1;
7580 			rack->r_ctl.forced_ack_ts = tcp_get_usecs(NULL);
7581 		} else {
7582 			rack->probe_not_answered = 1;
7583 			counter_u64_add(rack_persists_loss, 1);
7584 			rack->r_ctl.persist_lost_ends++;
7585 		}
7586 		counter_u64_add(rack_persists_sends, 1);
7587 		counter_u64_add(rack_out_size[TCP_MSS_ACCT_PERSIST], 1);
7588 		tcp_respond(tp, t_template->tt_ipgen,
7589 			    &t_template->tt_t, (struct mbuf *)NULL,
7590 			    tp->rcv_nxt, tp->snd_una - 1, 0);
7591 		/* This sends an ack */
7592 		if (tp->t_flags & TF_DELACK)
7593 			tp->t_flags &= ~TF_DELACK;
7594 		free(t_template, M_TEMP);
7595 	}
7596 	if (tp->t_rxtshift < V_tcp_retries)
7597 		tp->t_rxtshift++;
7598 out:
7599 	rack_log_to_event(rack, RACK_TO_FRM_PERSIST, NULL);
7600 	rack_start_hpts_timer(rack, tp, cts,
7601 			      0, 0, 0);
7602 	return (retval);
7603 }
7604 
7605 /*
7606  * If a keepalive goes off, we had no other timers
7607  * happening. We always return 1 here since this
7608  * routine either drops the connection or sends
7609  * out a segment with respond.
7610  */
7611 static int
7612 rack_timeout_keepalive(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
7613 {
7614 	struct tcptemp *t_template;
7615 	struct inpcb *inp = tptoinpcb(tp);
7616 
7617 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP;
7618 	rack_log_to_event(rack, RACK_TO_FRM_KEEP, NULL);
7619 	/*
7620 	 * Keep-alive timer went off; send something or drop connection if
7621 	 * idle for too long.
7622 	 */
7623 	KMOD_TCPSTAT_INC(tcps_keeptimeo);
7624 	if (tp->t_state < TCPS_ESTABLISHED)
7625 		goto dropit;
7626 	if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
7627 	    tp->t_state <= TCPS_CLOSING) {
7628 		if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
7629 			goto dropit;
7630 		/*
7631 		 * Send a packet designed to force a response if the peer is
7632 		 * up and reachable: either an ACK if the connection is
7633 		 * still alive, or an RST if the peer has closed the
7634 		 * connection due to timeout or reboot. Using sequence
7635 		 * number tp->snd_una-1 causes the transmitted zero-length
7636 		 * segment to lie outside the receive window; by the
7637 		 * protocol spec, this requires the correspondent TCP to
7638 		 * respond.
7639 		 */
7640 		KMOD_TCPSTAT_INC(tcps_keepprobe);
7641 		t_template = tcpip_maketemplate(inp);
7642 		if (t_template) {
7643 			if (rack->forced_ack == 0) {
7644 				rack->forced_ack = 1;
7645 				rack->r_ctl.forced_ack_ts = tcp_get_usecs(NULL);
7646 			} else {
7647 				rack->probe_not_answered = 1;
7648 			}
7649 			tcp_respond(tp, t_template->tt_ipgen,
7650 			    &t_template->tt_t, (struct mbuf *)NULL,
7651 			    tp->rcv_nxt, tp->snd_una - 1, 0);
7652 			free(t_template, M_TEMP);
7653 		}
7654 	}
7655 	rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
7656 	return (1);
7657 dropit:
7658 	KMOD_TCPSTAT_INC(tcps_keepdrops);
7659 	tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
7660 	return (-ETIMEDOUT);	/* tcp_drop() */
7661 }
7662 
7663 /*
7664  * Retransmit helper function, clear up all the ack
7665  * flags and take care of important book keeping.
7666  */
7667 static void
7668 rack_remxt_tmr(struct tcpcb *tp)
7669 {
7670 	/*
7671 	 * The retransmit timer went off, all sack'd blocks must be
7672 	 * un-acked.
7673 	 */
7674 	struct rack_sendmap *rsm, *trsm = NULL;
7675 	struct tcp_rack *rack;
7676 
7677 	rack = (struct tcp_rack *)tp->t_fb_ptr;
7678 	rack_timer_cancel(tp, rack, tcp_get_usecs(NULL), __LINE__);
7679 	rack_log_to_event(rack, RACK_TO_FRM_TMR, NULL);
7680 	if (rack->r_state && (rack->r_state != tp->t_state))
7681 		rack_set_state(tp, rack);
7682 	/*
7683 	 * Ideally we would like to be able to
7684 	 * mark SACK-PASS on anything not acked here.
7685 	 *
7686 	 * However, if we do that we would burst out
7687 	 * all that data 1ms apart. This would be unwise,
7688 	 * so for now we will just let the normal rxt timer
7689 	 * and tlp timer take care of it.
7690 	 *
7691 	 * Also we really need to stick them back in sequence
7692 	 * order. This way we send in the proper order and any
7693 	 * sacks that come floating in will "re-ack" the data.
7694 	 * To do this we zap the tmap with an INIT and then
7695 	 * walk through and place every rsm in the RB tree
7696 	 * back in its seq ordered place.
7697 	 */
7698 	TAILQ_INIT(&rack->r_ctl.rc_tmap);
7699 
7700 	TQHASH_FOREACH(rsm, rack->r_ctl.tqh)  {
7701 		rsm->r_dupack = 0;
7702 		if (rack_verbose_logging)
7703 			rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
7704 		/* We must re-add it back to the tlist */
7705 		if (trsm == NULL) {
7706 			TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
7707 		} else {
7708 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, trsm, rsm, r_tnext);
7709 		}
7710 		rsm->r_in_tmap = 1;
7711 		trsm = rsm;
7712 		if (rsm->r_flags & RACK_ACKED)
7713 			rsm->r_flags |= RACK_WAS_ACKED;
7714 		rsm->r_flags &= ~(RACK_ACKED | RACK_SACK_PASSED | RACK_WAS_SACKPASS | RACK_RWND_COLLAPSED);
7715 		rsm->r_flags |= RACK_MUST_RXT;
7716 	}
7717 	/* Clear the count (we just un-acked them) */
7718 	rack->r_ctl.rc_last_timeout_snduna = tp->snd_una;
7719 	rack->r_ctl.rc_sacked = 0;
7720 	rack->r_ctl.rc_sacklast = NULL;
7721 	rack->r_ctl.rc_agg_delayed = 0;
7722 	rack->r_early = 0;
7723 	rack->r_ctl.rc_agg_early = 0;
7724 	rack->r_late = 0;
7725 	/* Clear the tlp rtx mark */
7726 	rack->r_ctl.rc_resend = tqhash_min(rack->r_ctl.tqh);
7727 	if (rack->r_ctl.rc_resend != NULL)
7728 		rack->r_ctl.rc_resend->r_flags |= RACK_TO_REXT;
7729 	rack->r_ctl.rc_prr_sndcnt = 0;
7730 	rack_log_to_prr(rack, 6, 0, __LINE__);
7731 	rack->r_timer_override = 1;
7732 	if ((((tp->t_flags & TF_SACK_PERMIT) == 0)
7733 #ifdef TCP_SAD_DETECTION
7734 	    || (rack->sack_attack_disable != 0)
7735 #endif
7736 		    ) && ((tp->t_flags & TF_SENTFIN) == 0)) {
7737 		/*
7738 		 * For non-sack customers new data
7739 		 * needs to go out as retransmits until
7740 		 * we retransmit up to snd_max.
7741 		 */
7742 		rack->r_must_retran = 1;
7743 		rack->r_ctl.rc_out_at_rto = ctf_flight_size(rack->rc_tp,
7744 						rack->r_ctl.rc_sacked);
7745 	}
7746 	rack->r_ctl.rc_snd_max_at_rto = tp->snd_max;
7747 }
7748 
7749 static void
7750 rack_convert_rtts(struct tcpcb *tp)
7751 {
7752 	tcp_change_time_units(tp, TCP_TMR_GRANULARITY_USEC);
7753 	tp->t_rxtcur = RACK_REXMTVAL(tp);
7754 	if (TCPS_HAVEESTABLISHED(tp->t_state)) {
7755 		tp->t_rxtcur += TICKS_2_USEC(tcp_rexmit_slop);
7756 	}
7757 	if (tp->t_rxtcur > rack_rto_max) {
7758 		tp->t_rxtcur = rack_rto_max;
7759 	}
7760 }
7761 
7762 static void
7763 rack_cc_conn_init(struct tcpcb *tp)
7764 {
7765 	struct tcp_rack *rack;
7766 	uint32_t srtt;
7767 
7768 	rack = (struct tcp_rack *)tp->t_fb_ptr;
7769 	srtt = tp->t_srtt;
7770 	cc_conn_init(tp);
7771 	/*
7772 	 * Now convert to rack's internal format,
7773 	 * if required.
7774 	 */
7775 	if ((srtt == 0) && (tp->t_srtt != 0))
7776 		rack_convert_rtts(tp);
7777 	/*
7778 	 * We want a chance to stay in slowstart as
7779 	 * we create a connection. TCP spec says that
7780 	 * initially ssthresh is infinite. For our
7781 	 * purposes that is the snd_wnd.
7782 	 */
7783 	if (tp->snd_ssthresh < tp->snd_wnd) {
7784 		tp->snd_ssthresh = tp->snd_wnd;
7785 	}
7786 	/*
7787 	 * We also want to assure a IW worth of
7788 	 * data can get inflight.
7789 	 */
7790 	if (rc_init_window(rack) < tp->snd_cwnd)
7791 		tp->snd_cwnd = rc_init_window(rack);
7792 }
7793 
7794 /*
7795  * Re-transmit timeout! If we drop the PCB we will return 1, otherwise
7796  * we will setup to retransmit the lowest seq number outstanding.
7797  */
7798 static int
7799 rack_timeout_rxt(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
7800 {
7801 	struct inpcb *inp = tptoinpcb(tp);
7802 	int32_t rexmt;
7803 	int32_t retval = 0;
7804 	bool isipv6;
7805 
7806 	if ((tp->t_flags & TF_GPUTINPROG) &&
7807 	    (tp->t_rxtshift)) {
7808 		/*
7809 		 * We have had a second timeout
7810 		 * measurements on successive rxt's are not profitable.
7811 		 * It is unlikely to be of any use (the network is
7812 		 * broken or the client went away).
7813 		 */
7814 		tp->t_flags &= ~TF_GPUTINPROG;
7815 		rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
7816 					   rack->r_ctl.rc_gp_srtt /*flex1*/,
7817 					   tp->gput_seq,
7818 					   0, 0, 18, __LINE__, NULL, 0);
7819 	}
7820 	if (ctf_progress_timeout_check(tp, false)) {
7821 		tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
7822 		rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
7823 		return (-ETIMEDOUT);	/* tcp_drop() */
7824 	}
7825 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT;
7826 	rack->r_ctl.retran_during_recovery = 0;
7827 	rack->rc_ack_required = 1;
7828 	rack->r_ctl.dsack_byte_cnt = 0;
7829 	if (IN_FASTRECOVERY(tp->t_flags))
7830 		tp->t_flags |= TF_WASFRECOVERY;
7831 	else
7832 		tp->t_flags &= ~TF_WASFRECOVERY;
7833 	if (IN_CONGRECOVERY(tp->t_flags))
7834 		tp->t_flags |= TF_WASCRECOVERY;
7835 	else
7836 		tp->t_flags &= ~TF_WASCRECOVERY;
7837 	if (TCPS_HAVEESTABLISHED(tp->t_state) &&
7838 	    (tp->snd_una == tp->snd_max)) {
7839 		/* Nothing outstanding .. nothing to do */
7840 		return (0);
7841 	}
7842 	if (rack->r_ctl.dsack_persist) {
7843 		rack->r_ctl.dsack_persist--;
7844 		if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
7845 			rack->r_ctl.num_dsack = 0;
7846 		}
7847 		rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
7848 	}
7849 	/*
7850 	 * Rack can only run one timer  at a time, so we cannot
7851 	 * run a KEEPINIT (gating SYN sending) and a retransmit
7852 	 * timer for the SYN. So if we are in a front state and
7853 	 * have a KEEPINIT timer we need to check the first transmit
7854 	 * against now to see if we have exceeded the KEEPINIT time
7855 	 * (if one is set).
7856 	 */
7857 	if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) &&
7858 	    (TP_KEEPINIT(tp) != 0)) {
7859 		struct rack_sendmap *rsm;
7860 
7861 		rsm = tqhash_min(rack->r_ctl.tqh);
7862 		if (rsm) {
7863 			/* Ok we have something outstanding to test keepinit with */
7864 			if ((TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) &&
7865 			    ((cts - (uint32_t)rsm->r_tim_lastsent[0]) >= TICKS_2_USEC(TP_KEEPINIT(tp)))) {
7866 				/* We have exceeded the KEEPINIT time */
7867 				tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
7868 				goto drop_it;
7869 			}
7870 		}
7871 	}
7872 	/*
7873 	 * Retransmission timer went off.  Message has not been acked within
7874 	 * retransmit interval.  Back off to a longer retransmit interval
7875 	 * and retransmit one segment.
7876 	 */
7877 	rack_remxt_tmr(tp);
7878 	if ((rack->r_ctl.rc_resend == NULL) ||
7879 	    ((rack->r_ctl.rc_resend->r_flags & RACK_RWND_COLLAPSED) == 0)) {
7880 		/*
7881 		 * If the rwnd collapsed on
7882 		 * the one we are retransmitting
7883 		 * it does not count against the
7884 		 * rxt count.
7885 		 */
7886 		tp->t_rxtshift++;
7887 	}
7888 	if (tp->t_rxtshift > V_tcp_retries) {
7889 		tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
7890 drop_it:
7891 		tp->t_rxtshift = V_tcp_retries;
7892 		KMOD_TCPSTAT_INC(tcps_timeoutdrop);
7893 		/* XXXGL: previously t_softerror was casted to uint16_t */
7894 		MPASS(tp->t_softerror >= 0);
7895 		retval = tp->t_softerror ? -tp->t_softerror : -ETIMEDOUT;
7896 		goto out;	/* tcp_drop() */
7897 	}
7898 	if (tp->t_state == TCPS_SYN_SENT) {
7899 		/*
7900 		 * If the SYN was retransmitted, indicate CWND to be limited
7901 		 * to 1 segment in cc_conn_init().
7902 		 */
7903 		tp->snd_cwnd = 1;
7904 	} else if (tp->t_rxtshift == 1) {
7905 		/*
7906 		 * first retransmit; record ssthresh and cwnd so they can be
7907 		 * recovered if this turns out to be a "bad" retransmit. A
7908 		 * retransmit is considered "bad" if an ACK for this segment
7909 		 * is received within RTT/2 interval; the assumption here is
7910 		 * that the ACK was already in flight.  See "On Estimating
7911 		 * End-to-End Network Path Properties" by Allman and Paxson
7912 		 * for more details.
7913 		 */
7914 		tp->snd_cwnd_prev = tp->snd_cwnd;
7915 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
7916 		tp->snd_recover_prev = tp->snd_recover;
7917 		tp->t_badrxtwin = ticks + (USEC_2_TICKS(tp->t_srtt)/2);
7918 		tp->t_flags |= TF_PREVVALID;
7919 	} else if ((tp->t_flags & TF_RCVD_TSTMP) == 0)
7920 		tp->t_flags &= ~TF_PREVVALID;
7921 	KMOD_TCPSTAT_INC(tcps_rexmttimeo);
7922 	if ((tp->t_state == TCPS_SYN_SENT) ||
7923 	    (tp->t_state == TCPS_SYN_RECEIVED))
7924 		rexmt = RACK_INITIAL_RTO * tcp_backoff[tp->t_rxtshift];
7925 	else
7926 		rexmt = max(rack_rto_min, (tp->t_srtt + (tp->t_rttvar << 2))) * tcp_backoff[tp->t_rxtshift];
7927 
7928 	RACK_TCPT_RANGESET(tp->t_rxtcur, rexmt,
7929 	   max(rack_rto_min, rexmt), rack_rto_max, rack->r_ctl.timer_slop);
7930 	/*
7931 	 * We enter the path for PLMTUD if connection is established or, if
7932 	 * connection is FIN_WAIT_1 status, reason for the last is that if
7933 	 * amount of data we send is very small, we could send it in couple
7934 	 * of packets and process straight to FIN. In that case we won't
7935 	 * catch ESTABLISHED state.
7936 	 */
7937 #ifdef INET6
7938 	isipv6 = (inp->inp_vflag & INP_IPV6) ? true : false;
7939 #else
7940 	isipv6 = false;
7941 #endif
7942 	if (((V_tcp_pmtud_blackhole_detect == 1) ||
7943 	    (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) ||
7944 	    (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) &&
7945 	    ((tp->t_state == TCPS_ESTABLISHED) ||
7946 	    (tp->t_state == TCPS_FIN_WAIT_1))) {
7947 		/*
7948 		 * Idea here is that at each stage of mtu probe (usually,
7949 		 * 1448 -> 1188 -> 524) should be given 2 chances to recover
7950 		 * before further clamping down. 'tp->t_rxtshift % 2 == 0'
7951 		 * should take care of that.
7952 		 */
7953 		if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) ==
7954 		    (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) &&
7955 		    (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 &&
7956 		    tp->t_rxtshift % 2 == 0)) {
7957 			/*
7958 			 * Enter Path MTU Black-hole Detection mechanism: -
7959 			 * Disable Path MTU Discovery (IP "DF" bit). -
7960 			 * Reduce MTU to lower value than what we negotiated
7961 			 * with peer.
7962 			 */
7963 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
7964 				/* Record that we may have found a black hole. */
7965 				tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
7966 				/* Keep track of previous MSS. */
7967 				tp->t_pmtud_saved_maxseg = tp->t_maxseg;
7968 			}
7969 
7970 			/*
7971 			 * Reduce the MSS to blackhole value or to the
7972 			 * default in an attempt to retransmit.
7973 			 */
7974 #ifdef INET6
7975 			if (isipv6 &&
7976 			    tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) {
7977 				/* Use the sysctl tuneable blackhole MSS. */
7978 				tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
7979 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
7980 			} else if (isipv6) {
7981 				/* Use the default MSS. */
7982 				tp->t_maxseg = V_tcp_v6mssdflt;
7983 				/*
7984 				 * Disable Path MTU Discovery when we switch
7985 				 * to minmss.
7986 				 */
7987 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
7988 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
7989 			}
7990 #endif
7991 #if defined(INET6) && defined(INET)
7992 			else
7993 #endif
7994 #ifdef INET
7995 			if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) {
7996 				/* Use the sysctl tuneable blackhole MSS. */
7997 				tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
7998 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
7999 			} else {
8000 				/* Use the default MSS. */
8001 				tp->t_maxseg = V_tcp_mssdflt;
8002 				/*
8003 				 * Disable Path MTU Discovery when we switch
8004 				 * to minmss.
8005 				 */
8006 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
8007 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
8008 			}
8009 #endif
8010 		} else {
8011 			/*
8012 			 * If further retransmissions are still unsuccessful
8013 			 * with a lowered MTU, maybe this isn't a blackhole
8014 			 * and we restore the previous MSS and blackhole
8015 			 * detection flags. The limit '6' is determined by
8016 			 * giving each probe stage (1448, 1188, 524) 2
8017 			 * chances to recover.
8018 			 */
8019 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
8020 			    (tp->t_rxtshift >= 6)) {
8021 				tp->t_flags2 |= TF2_PLPMTU_PMTUD;
8022 				tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
8023 				tp->t_maxseg = tp->t_pmtud_saved_maxseg;
8024 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed);
8025 			}
8026 		}
8027 	}
8028 	/*
8029 	 * Disable RFC1323 and SACK if we haven't got any response to
8030 	 * our third SYN to work-around some broken terminal servers
8031 	 * (most of which have hopefully been retired) that have bad VJ
8032 	 * header compression code which trashes TCP segments containing
8033 	 * unknown-to-them TCP options.
8034 	 */
8035 	if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
8036 	    (tp->t_rxtshift == 3))
8037 		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
8038 	/*
8039 	 * If we backed off this far, our srtt estimate is probably bogus.
8040 	 * Clobber it so we'll take the next rtt measurement as our srtt;
8041 	 * move the current srtt into rttvar to keep the current retransmit
8042 	 * times until then.
8043 	 */
8044 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
8045 #ifdef INET6
8046 		if ((inp->inp_vflag & INP_IPV6) != 0)
8047 			in6_losing(inp);
8048 		else
8049 #endif
8050 			in_losing(inp);
8051 		tp->t_rttvar += tp->t_srtt;
8052 		tp->t_srtt = 0;
8053 	}
8054 	sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
8055 	tp->snd_recover = tp->snd_max;
8056 	tp->t_flags |= TF_ACKNOW;
8057 	tp->t_rtttime = 0;
8058 	rack_cong_signal(tp, CC_RTO, tp->snd_una, __LINE__);
8059 out:
8060 	return (retval);
8061 }
8062 
8063 static int
8064 rack_process_timers(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t hpts_calling, uint8_t *doing_tlp)
8065 {
8066 	int32_t ret = 0;
8067 	int32_t timers = (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK);
8068 
8069 	if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
8070 	    (tp->t_flags & TF_GPUTINPROG)) {
8071 		/*
8072 		 * We have a goodput in progress
8073 		 * and we have entered a late state.
8074 		 * Do we have enough data in the sb
8075 		 * to handle the GPUT request?
8076 		 */
8077 		uint32_t bytes;
8078 
8079 		bytes = tp->gput_ack - tp->gput_seq;
8080 		if (SEQ_GT(tp->gput_seq, tp->snd_una))
8081 			bytes += tp->gput_seq - tp->snd_una;
8082 		if (bytes > sbavail(&tptosocket(tp)->so_snd)) {
8083 			/*
8084 			 * There are not enough bytes in the socket
8085 			 * buffer that have been sent to cover this
8086 			 * measurement. Cancel it.
8087 			 */
8088 			rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
8089 						   rack->r_ctl.rc_gp_srtt /*flex1*/,
8090 						   tp->gput_seq,
8091 						   0, 0, 18, __LINE__, NULL, 0);
8092 			tp->t_flags &= ~TF_GPUTINPROG;
8093 		}
8094 	}
8095 	if (timers == 0) {
8096 		return (0);
8097 	}
8098 	if (tp->t_state == TCPS_LISTEN) {
8099 		/* no timers on listen sockets */
8100 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)
8101 			return (0);
8102 		return (1);
8103 	}
8104 	if ((timers & PACE_TMR_RACK) &&
8105 	    rack->rc_on_min_to) {
8106 		/*
8107 		 * For the rack timer when we
8108 		 * are on a min-timeout (which means rrr_conf = 3)
8109 		 * we don't want to check the timer. It may
8110 		 * be going off for a pace and thats ok we
8111 		 * want to send the retransmit (if its ready).
8112 		 *
8113 		 * If its on a normal rack timer (non-min) then
8114 		 * we will check if its expired.
8115 		 */
8116 		goto skip_time_check;
8117 	}
8118 	if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
8119 		uint32_t left;
8120 
8121 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
8122 			ret = -1;
8123 			rack_log_to_processing(rack, cts, ret, 0);
8124 			return (0);
8125 		}
8126 		if (hpts_calling == 0) {
8127 			/*
8128 			 * A user send or queued mbuf (sack) has called us? We
8129 			 * return 0 and let the pacing guards
8130 			 * deal with it if they should or
8131 			 * should not cause a send.
8132 			 */
8133 			ret = -2;
8134 			rack_log_to_processing(rack, cts, ret, 0);
8135 			return (0);
8136 		}
8137 		/*
8138 		 * Ok our timer went off early and we are not paced false
8139 		 * alarm, go back to sleep. We make sure we don't have
8140 		 * no-sack wakeup on since we no longer have a PKT_OUTPUT
8141 		 * flag in place.
8142 		 */
8143 		rack->rc_tp->t_flags2 &= ~TF2_DONT_SACK_QUEUE;
8144 		ret = -3;
8145 		left = rack->r_ctl.rc_timer_exp - cts;
8146 		tcp_hpts_insert(tp, HPTS_MS_TO_SLOTS(left));
8147 		rack_log_to_processing(rack, cts, ret, left);
8148 		return (1);
8149 	}
8150 skip_time_check:
8151 	rack->rc_tmr_stopped = 0;
8152 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK;
8153 	if (timers & PACE_TMR_DELACK) {
8154 		ret = rack_timeout_delack(tp, rack, cts);
8155 	} else if (timers & PACE_TMR_RACK) {
8156 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
8157 		rack->r_fast_output = 0;
8158 		ret = rack_timeout_rack(tp, rack, cts);
8159 	} else if (timers & PACE_TMR_TLP) {
8160 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
8161 		ret = rack_timeout_tlp(tp, rack, cts, doing_tlp);
8162 	} else if (timers & PACE_TMR_RXT) {
8163 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
8164 		rack->r_fast_output = 0;
8165 		ret = rack_timeout_rxt(tp, rack, cts);
8166 	} else if (timers & PACE_TMR_PERSIT) {
8167 		ret = rack_timeout_persist(tp, rack, cts);
8168 	} else if (timers & PACE_TMR_KEEP) {
8169 		ret = rack_timeout_keepalive(tp, rack, cts);
8170 	}
8171 	rack_log_to_processing(rack, cts, ret, timers);
8172 	return (ret);
8173 }
8174 
8175 static void
8176 rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line)
8177 {
8178 	struct timeval tv;
8179 	uint32_t us_cts, flags_on_entry;
8180 	uint8_t hpts_removed = 0;
8181 
8182 	flags_on_entry = rack->r_ctl.rc_hpts_flags;
8183 	us_cts = tcp_get_usecs(&tv);
8184 	if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
8185 	    ((TSTMP_GEQ(us_cts, rack->r_ctl.rc_last_output_to)) ||
8186 	     ((tp->snd_max - tp->snd_una) == 0))) {
8187 		tcp_hpts_remove(rack->rc_tp);
8188 		hpts_removed = 1;
8189 		/* If we were not delayed cancel out the flag. */
8190 		if ((tp->snd_max - tp->snd_una) == 0)
8191 			rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
8192 		rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry);
8193 	}
8194 	if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
8195 		rack->rc_tmr_stopped = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
8196 		if (tcp_in_hpts(rack->rc_tp) &&
8197 		    ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)) {
8198 			/*
8199 			 * Canceling timer's when we have no output being
8200 			 * paced. We also must remove ourselves from the
8201 			 * hpts.
8202 			 */
8203 			tcp_hpts_remove(rack->rc_tp);
8204 			hpts_removed = 1;
8205 		}
8206 		rack->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK);
8207 	}
8208 	if (hpts_removed == 0)
8209 		rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry);
8210 }
8211 
8212 static int
8213 rack_stopall(struct tcpcb *tp)
8214 {
8215 	struct tcp_rack *rack;
8216 	rack = (struct tcp_rack *)tp->t_fb_ptr;
8217 	rack->t_timers_stopped = 1;
8218 	return (0);
8219 }
8220 
8221 static void
8222 rack_stop_all_timers(struct tcpcb *tp, struct tcp_rack *rack)
8223 {
8224 	/*
8225 	 * Assure no timers are running.
8226 	 */
8227 	if (tcp_timer_active(tp, TT_PERSIST)) {
8228 		/* We enter in persists, set the flag appropriately */
8229 		rack->rc_in_persist = 1;
8230 	}
8231 	if (tcp_in_hpts(rack->rc_tp)) {
8232 		tcp_hpts_remove(rack->rc_tp);
8233 	}
8234 }
8235 
8236 static void
8237 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
8238     struct rack_sendmap *rsm, uint64_t ts, uint16_t add_flag, int segsiz)
8239 {
8240 	int32_t idx;
8241 
8242 	rsm->r_rtr_cnt++;
8243 	rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
8244 	rsm->r_dupack = 0;
8245 	if (rsm->r_rtr_cnt > RACK_NUM_OF_RETRANS) {
8246 		rsm->r_rtr_cnt = RACK_NUM_OF_RETRANS;
8247 		rsm->r_flags |= RACK_OVERMAX;
8248 	}
8249 	if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & RACK_TLP) == 0)) {
8250 		rack->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start);
8251 		rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start);
8252 	}
8253 	idx = rsm->r_rtr_cnt - 1;
8254 	rsm->r_tim_lastsent[idx] = ts;
8255 	/*
8256 	 * Here we don't add in the len of send, since its already
8257 	 * in snduna <->snd_max.
8258 	 */
8259 	rsm->r_fas = ctf_flight_size(rack->rc_tp,
8260 				     rack->r_ctl.rc_sacked);
8261 	if (rsm->r_flags & RACK_ACKED) {
8262 		/* Problably MTU discovery messing with us */
8263 		rsm->r_flags &= ~RACK_ACKED;
8264 		rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
8265 	}
8266 	if (rsm->r_in_tmap) {
8267 		TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8268 		rsm->r_in_tmap = 0;
8269 	}
8270 	/* Lets make sure it really is in or not the GP window */
8271 	rack_mark_in_gp_win(tp, rsm);
8272 	TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8273 	rsm->r_in_tmap = 1;
8274 	rsm->r_bas = (uint8_t)(((rsm->r_end - rsm->r_start) + segsiz - 1) / segsiz);
8275 	/* Take off the must retransmit flag, if its on */
8276 	if (rsm->r_flags & RACK_MUST_RXT) {
8277 		if (rack->r_must_retran)
8278 			rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start);
8279 		if (SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) {
8280 			/*
8281 			 * We have retransmitted all we need. Clear
8282 			 * any must retransmit flags.
8283 			 */
8284 			rack->r_must_retran = 0;
8285 			rack->r_ctl.rc_out_at_rto = 0;
8286 		}
8287 		rsm->r_flags &= ~RACK_MUST_RXT;
8288 	}
8289 	/* Remove any collapsed flag */
8290 	rsm->r_flags &= ~RACK_RWND_COLLAPSED;
8291 	if (rsm->r_flags & RACK_SACK_PASSED) {
8292 		/* We have retransmitted due to the SACK pass */
8293 		rsm->r_flags &= ~RACK_SACK_PASSED;
8294 		rsm->r_flags |= RACK_WAS_SACKPASS;
8295 	}
8296 }
8297 
8298 static uint32_t
8299 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack,
8300     struct rack_sendmap *rsm, uint64_t ts, int32_t *lenp, uint16_t add_flag, int segsiz)
8301 {
8302 	/*
8303 	 * We (re-)transmitted starting at rsm->r_start for some length
8304 	 * (possibly less than r_end.
8305 	 */
8306 	struct rack_sendmap *nrsm;
8307 	int insret __diagused;
8308 	uint32_t c_end;
8309 	int32_t len;
8310 
8311 	len = *lenp;
8312 	c_end = rsm->r_start + len;
8313 	if (SEQ_GEQ(c_end, rsm->r_end)) {
8314 		/*
8315 		 * We retransmitted the whole piece or more than the whole
8316 		 * slopping into the next rsm.
8317 		 */
8318 		rack_update_rsm(tp, rack, rsm, ts, add_flag, segsiz);
8319 		if (c_end == rsm->r_end) {
8320 			*lenp = 0;
8321 			return (0);
8322 		} else {
8323 			int32_t act_len;
8324 
8325 			/* Hangs over the end return whats left */
8326 			act_len = rsm->r_end - rsm->r_start;
8327 			*lenp = (len - act_len);
8328 			return (rsm->r_end);
8329 		}
8330 		/* We don't get out of this block. */
8331 	}
8332 	/*
8333 	 * Here we retransmitted less than the whole thing which means we
8334 	 * have to split this into what was transmitted and what was not.
8335 	 */
8336 	nrsm = rack_alloc_full_limit(rack);
8337 	if (nrsm == NULL) {
8338 		/*
8339 		 * We can't get memory, so lets not proceed.
8340 		 */
8341 		*lenp = 0;
8342 		return (0);
8343 	}
8344 	/*
8345 	 * So here we are going to take the original rsm and make it what we
8346 	 * retransmitted. nrsm will be the tail portion we did not
8347 	 * retransmit. For example say the chunk was 1, 11 (10 bytes). And
8348 	 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to
8349 	 * 1, 6 and the new piece will be 6, 11.
8350 	 */
8351 	rack_clone_rsm(rack, nrsm, rsm, c_end);
8352 	nrsm->r_dupack = 0;
8353 	rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2);
8354 #ifndef INVARIANTS
8355 	(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
8356 #else
8357 	if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
8358 		panic("Insert in rb tree of %p fails ret:%d rack:%p rsm:%p",
8359 		      nrsm, insret, rack, rsm);
8360 	}
8361 #endif
8362 	if (rsm->r_in_tmap) {
8363 		TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8364 		nrsm->r_in_tmap = 1;
8365 	}
8366 	rsm->r_flags &= (~RACK_HAS_FIN);
8367 	rack_update_rsm(tp, rack, rsm, ts, add_flag, segsiz);
8368 	/* Log a split of rsm into rsm and nrsm */
8369 	rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
8370 	*lenp = 0;
8371 	return (0);
8372 }
8373 
8374 static void
8375 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
8376 		uint32_t seq_out, uint16_t th_flags, int32_t err, uint64_t cts,
8377 		struct rack_sendmap *hintrsm, uint16_t add_flag, struct mbuf *s_mb,
8378 		uint32_t s_moff, int hw_tls, int segsiz)
8379 {
8380 	struct tcp_rack *rack;
8381 	struct rack_sendmap *rsm, *nrsm;
8382 	int insret __diagused;
8383 
8384 	register uint32_t snd_max, snd_una;
8385 
8386 	/*
8387 	 * Add to the RACK log of packets in flight or retransmitted. If
8388 	 * there is a TS option we will use the TS echoed, if not we will
8389 	 * grab a TS.
8390 	 *
8391 	 * Retransmissions will increment the count and move the ts to its
8392 	 * proper place. Note that if options do not include TS's then we
8393 	 * won't be able to effectively use the ACK for an RTT on a retran.
8394 	 *
8395 	 * Notes about r_start and r_end. Lets consider a send starting at
8396 	 * sequence 1 for 10 bytes. In such an example the r_start would be
8397 	 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11.
8398 	 * This means that r_end is actually the first sequence for the next
8399 	 * slot (11).
8400 	 *
8401 	 */
8402 	/*
8403 	 * If err is set what do we do XXXrrs? should we not add the thing?
8404 	 * -- i.e. return if err != 0 or should we pretend we sent it? --
8405 	 * i.e. proceed with add ** do this for now.
8406 	 */
8407 	INP_WLOCK_ASSERT(tptoinpcb(tp));
8408 	if (err)
8409 		/*
8410 		 * We don't log errors -- we could but snd_max does not
8411 		 * advance in this case either.
8412 		 */
8413 		return;
8414 
8415 	if (th_flags & TH_RST) {
8416 		/*
8417 		 * We don't log resets and we return immediately from
8418 		 * sending
8419 		 */
8420 		return;
8421 	}
8422 	rack = (struct tcp_rack *)tp->t_fb_ptr;
8423 	snd_una = tp->snd_una;
8424 	snd_max = tp->snd_max;
8425 	if (th_flags & (TH_SYN | TH_FIN)) {
8426 		/*
8427 		 * The call to rack_log_output is made before bumping
8428 		 * snd_max. This means we can record one extra byte on a SYN
8429 		 * or FIN if seq_out is adding more on and a FIN is present
8430 		 * (and we are not resending).
8431 		 */
8432 		if ((th_flags & TH_SYN) && (seq_out == tp->iss))
8433 			len++;
8434 		if (th_flags & TH_FIN)
8435 			len++;
8436 		if (SEQ_LT(snd_max, tp->snd_nxt)) {
8437 			/*
8438 			 * The add/update as not been done for the FIN/SYN
8439 			 * yet.
8440 			 */
8441 			snd_max = tp->snd_nxt;
8442 		}
8443 	}
8444 	if (SEQ_LEQ((seq_out + len), snd_una)) {
8445 		/* Are sending an old segment to induce an ack (keep-alive)? */
8446 		return;
8447 	}
8448 	if (SEQ_LT(seq_out, snd_una)) {
8449 		/* huh? should we panic? */
8450 		uint32_t end;
8451 
8452 		end = seq_out + len;
8453 		seq_out = snd_una;
8454 		if (SEQ_GEQ(end, seq_out))
8455 			len = end - seq_out;
8456 		else
8457 			len = 0;
8458 	}
8459 	if (len == 0) {
8460 		/* We don't log zero window probes */
8461 		return;
8462 	}
8463 	if (IN_FASTRECOVERY(tp->t_flags)) {
8464 		rack->r_ctl.rc_prr_out += len;
8465 	}
8466 	/* First question is it a retransmission or new? */
8467 	if (seq_out == snd_max) {
8468 		/* Its new */
8469 		rack_chk_req_and_hybrid_on_out(rack, seq_out, len, cts);
8470 again:
8471 		rsm = rack_alloc(rack);
8472 		if (rsm == NULL) {
8473 			/*
8474 			 * Hmm out of memory and the tcb got destroyed while
8475 			 * we tried to wait.
8476 			 */
8477 			return;
8478 		}
8479 		if (th_flags & TH_FIN) {
8480 			rsm->r_flags = RACK_HAS_FIN|add_flag;
8481 		} else {
8482 			rsm->r_flags = add_flag;
8483 		}
8484 		if (hw_tls)
8485 			rsm->r_hw_tls = 1;
8486 		rsm->r_tim_lastsent[0] = cts;
8487 		rsm->r_rtr_cnt = 1;
8488 		rsm->r_rtr_bytes = 0;
8489 		if (th_flags & TH_SYN) {
8490 			/* The data space is one beyond snd_una */
8491 			rsm->r_flags |= RACK_HAS_SYN;
8492 		}
8493 		rsm->r_start = seq_out;
8494 		rsm->r_end = rsm->r_start + len;
8495 		rack_mark_in_gp_win(tp, rsm);
8496 		rsm->r_dupack = 0;
8497 		/*
8498 		 * save off the mbuf location that
8499 		 * sndmbuf_noadv returned (which is
8500 		 * where we started copying from)..
8501 		 */
8502 		rsm->m = s_mb;
8503 		rsm->soff = s_moff;
8504 		/*
8505 		 * Here we do add in the len of send, since its not yet
8506 		 * reflected in in snduna <->snd_max
8507 		 */
8508 		rsm->r_fas = (ctf_flight_size(rack->rc_tp,
8509 					      rack->r_ctl.rc_sacked) +
8510 			      (rsm->r_end - rsm->r_start));
8511 		/* rsm->m will be NULL if RACK_HAS_SYN or RACK_HAS_FIN is set */
8512 		if (rsm->m) {
8513 			if (rsm->m->m_len <= rsm->soff) {
8514 				/*
8515 				 * XXXrrs Question, will this happen?
8516 				 *
8517 				 * If sbsndptr is set at the correct place
8518 				 * then s_moff should always be somewhere
8519 				 * within rsm->m. But if the sbsndptr was
8520 				 * off then that won't be true. If it occurs
8521 				 * we need to walkout to the correct location.
8522 				 */
8523 				struct mbuf *lm;
8524 
8525 				lm = rsm->m;
8526 				while (lm->m_len <= rsm->soff) {
8527 					rsm->soff -= lm->m_len;
8528 					lm = lm->m_next;
8529 					KASSERT(lm != NULL, ("%s rack:%p lm goes null orig_off:%u origmb:%p rsm->soff:%u",
8530 							     __func__, rack, s_moff, s_mb, rsm->soff));
8531 				}
8532 				rsm->m = lm;
8533 			}
8534 			rsm->orig_m_len = rsm->m->m_len;
8535 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
8536 		} else {
8537 			rsm->orig_m_len = 0;
8538 			rsm->orig_t_space = 0;
8539 		}
8540 		rsm->r_bas = (uint8_t)((len + segsiz - 1) / segsiz);
8541 		rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
8542 		/* Log a new rsm */
8543 		rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_NEW, 0, __LINE__);
8544 #ifndef INVARIANTS
8545 		(void)tqhash_insert(rack->r_ctl.tqh, rsm);
8546 #else
8547 		if ((insret = tqhash_insert(rack->r_ctl.tqh, rsm)) != 0) {
8548 			panic("Insert in rb tree of %p fails ret:%d rack:%p rsm:%p",
8549 			      nrsm, insret, rack, rsm);
8550 		}
8551 #endif
8552 		TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8553 		rsm->r_in_tmap = 1;
8554 		/*
8555 		 * Special case detection, is there just a single
8556 		 * packet outstanding when we are not in recovery?
8557 		 *
8558 		 * If this is true mark it so.
8559 		 */
8560 		if ((IN_FASTRECOVERY(tp->t_flags) == 0) &&
8561 		    (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) == ctf_fixed_maxseg(tp))) {
8562 			struct rack_sendmap *prsm;
8563 
8564 			prsm = tqhash_prev(rack->r_ctl.tqh, rsm);
8565 			if (prsm)
8566 				prsm->r_one_out_nr = 1;
8567 		}
8568 		return;
8569 	}
8570 	/*
8571 	 * If we reach here its a retransmission and we need to find it.
8572 	 */
8573 more:
8574 	if (hintrsm && (hintrsm->r_start == seq_out)) {
8575 		rsm = hintrsm;
8576 		hintrsm = NULL;
8577 	} else {
8578 		/* No hints sorry */
8579 		rsm = NULL;
8580 	}
8581 	if ((rsm) && (rsm->r_start == seq_out)) {
8582 		seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag, segsiz);
8583 		if (len == 0) {
8584 			return;
8585 		} else {
8586 			goto more;
8587 		}
8588 	}
8589 	/* Ok it was not the last pointer go through it the hard way. */
8590 refind:
8591 	rsm = tqhash_find(rack->r_ctl.tqh, seq_out);
8592 	if (rsm) {
8593 		if (rsm->r_start == seq_out) {
8594 			seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag, segsiz);
8595 			if (len == 0) {
8596 				return;
8597 			} else {
8598 				goto refind;
8599 			}
8600 		}
8601 		if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) {
8602 			/* Transmitted within this piece */
8603 			/*
8604 			 * Ok we must split off the front and then let the
8605 			 * update do the rest
8606 			 */
8607 			nrsm = rack_alloc_full_limit(rack);
8608 			if (nrsm == NULL) {
8609 				rack_update_rsm(tp, rack, rsm, cts, add_flag, segsiz);
8610 				return;
8611 			}
8612 			/*
8613 			 * copy rsm to nrsm and then trim the front of rsm
8614 			 * to not include this part.
8615 			 */
8616 			rack_clone_rsm(rack, nrsm, rsm, seq_out);
8617 			rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
8618 #ifndef INVARIANTS
8619 			(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
8620 #else
8621 			if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
8622 				panic("Insert in rb tree of %p fails ret:%d rack:%p rsm:%p",
8623 				      nrsm, insret, rack, rsm);
8624 			}
8625 #endif
8626 			if (rsm->r_in_tmap) {
8627 				TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8628 				nrsm->r_in_tmap = 1;
8629 			}
8630 			rsm->r_flags &= (~RACK_HAS_FIN);
8631 			seq_out = rack_update_entry(tp, rack, nrsm, cts, &len, add_flag, segsiz);
8632 			if (len == 0) {
8633 				return;
8634 			} else if (len > 0)
8635 				goto refind;
8636 		}
8637 	}
8638 	/*
8639 	 * Hmm not found in map did they retransmit both old and on into the
8640 	 * new?
8641 	 */
8642 	if (seq_out == tp->snd_max) {
8643 		goto again;
8644 	} else if (SEQ_LT(seq_out, tp->snd_max)) {
8645 #ifdef INVARIANTS
8646 		printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n",
8647 		       seq_out, len, tp->snd_una, tp->snd_max);
8648 		printf("Starting Dump of all rack entries\n");
8649 		TQHASH_FOREACH(rsm, rack->r_ctl.tqh)  {
8650 			printf("rsm:%p start:%u end:%u\n",
8651 			       rsm, rsm->r_start, rsm->r_end);
8652 		}
8653 		printf("Dump complete\n");
8654 		panic("seq_out not found rack:%p tp:%p",
8655 		      rack, tp);
8656 #endif
8657 	} else {
8658 #ifdef INVARIANTS
8659 		/*
8660 		 * Hmm beyond sndmax? (only if we are using the new rtt-pack
8661 		 * flag)
8662 		 */
8663 		panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p",
8664 		      seq_out, len, tp->snd_max, tp);
8665 #endif
8666 	}
8667 }
8668 
8669 /*
8670  * Record one of the RTT updates from an ack into
8671  * our sample structure.
8672  */
8673 
8674 static void
8675 tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt, uint32_t len, uint32_t us_rtt,
8676 		    int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt)
8677 {
8678 	if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
8679 	    (rack->r_ctl.rack_rs.rs_rtt_lowest > rtt)) {
8680 		rack->r_ctl.rack_rs.rs_rtt_lowest = rtt;
8681 	}
8682 	if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
8683 	    (rack->r_ctl.rack_rs.rs_rtt_highest < rtt)) {
8684 		rack->r_ctl.rack_rs.rs_rtt_highest = rtt;
8685 	}
8686 	if (rack->rc_tp->t_flags & TF_GPUTINPROG) {
8687 	    if (us_rtt < rack->r_ctl.rc_gp_lowrtt)
8688 		rack->r_ctl.rc_gp_lowrtt = us_rtt;
8689 	    if (rack->rc_tp->snd_wnd > rack->r_ctl.rc_gp_high_rwnd)
8690 		    rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
8691 	}
8692 	if ((confidence == 1) &&
8693 	    ((rsm == NULL) ||
8694 	     (rsm->r_just_ret) ||
8695 	     (rsm->r_one_out_nr &&
8696 	      len < (ctf_fixed_maxseg(rack->rc_tp) * 2)))) {
8697 		/*
8698 		 * If the rsm had a just return
8699 		 * hit it then we can't trust the
8700 		 * rtt measurement for buffer deterimination
8701 		 * Note that a confidence of 2, indicates
8702 		 * SACK'd which overrides the r_just_ret or
8703 		 * the r_one_out_nr. If it was a CUM-ACK and
8704 		 * we had only two outstanding, but get an
8705 		 * ack for only 1. Then that also lowers our
8706 		 * confidence.
8707 		 */
8708 		confidence = 0;
8709 	}
8710 	if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
8711 	    (rack->r_ctl.rack_rs.rs_us_rtt > us_rtt)) {
8712 		if (rack->r_ctl.rack_rs.confidence == 0) {
8713 			/*
8714 			 * We take anything with no current confidence
8715 			 * saved.
8716 			 */
8717 			rack->r_ctl.rack_rs.rs_us_rtt = us_rtt;
8718 			rack->r_ctl.rack_rs.confidence = confidence;
8719 			rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt;
8720 		} else if (confidence != 0) {
8721 			/*
8722 			 * Once we have a confident number,
8723 			 * we can update it with a smaller
8724 			 * value since this confident number
8725 			 * may include the DSACK time until
8726 			 * the next segment (the second one) arrived.
8727 			 */
8728 			rack->r_ctl.rack_rs.rs_us_rtt = us_rtt;
8729 			rack->r_ctl.rack_rs.confidence = confidence;
8730 			rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt;
8731 		}
8732 	}
8733 	rack_log_rtt_upd(rack->rc_tp, rack, us_rtt, len, rsm, confidence);
8734 	rack->r_ctl.rack_rs.rs_flags = RACK_RTT_VALID;
8735 	rack->r_ctl.rack_rs.rs_rtt_tot += rtt;
8736 	rack->r_ctl.rack_rs.rs_rtt_cnt++;
8737 }
8738 
8739 /*
8740  * Collect new round-trip time estimate
8741  * and update averages and current timeout.
8742  */
8743 static void
8744 tcp_rack_xmit_timer_commit(struct tcp_rack *rack, struct tcpcb *tp)
8745 {
8746 	int32_t delta;
8747 	int32_t rtt;
8748 
8749 	if (rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY)
8750 		/* No valid sample */
8751 		return;
8752 	if (rack->r_ctl.rc_rate_sample_method == USE_RTT_LOW) {
8753 		/* We are to use the lowest RTT seen in a single ack */
8754 		rtt = rack->r_ctl.rack_rs.rs_rtt_lowest;
8755 	} else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_HIGH) {
8756 		/* We are to use the highest RTT seen in a single ack */
8757 		rtt = rack->r_ctl.rack_rs.rs_rtt_highest;
8758 	} else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_AVG) {
8759 		/* We are to use the average RTT seen in a single ack */
8760 		rtt = (int32_t)(rack->r_ctl.rack_rs.rs_rtt_tot /
8761 				(uint64_t)rack->r_ctl.rack_rs.rs_rtt_cnt);
8762 	} else {
8763 #ifdef INVARIANTS
8764 		panic("Unknown rtt variant %d", rack->r_ctl.rc_rate_sample_method);
8765 #endif
8766 		return;
8767 	}
8768 	if (rtt == 0)
8769 		rtt = 1;
8770 	if (rack->rc_gp_rtt_set == 0) {
8771 		/*
8772 		 * With no RTT we have to accept
8773 		 * even one we are not confident of.
8774 		 */
8775 		rack->r_ctl.rc_gp_srtt = rack->r_ctl.rack_rs.rs_us_rtt;
8776 		rack->rc_gp_rtt_set = 1;
8777 	} else if (rack->r_ctl.rack_rs.confidence) {
8778 		/* update the running gp srtt */
8779 		rack->r_ctl.rc_gp_srtt -= (rack->r_ctl.rc_gp_srtt/8);
8780 		rack->r_ctl.rc_gp_srtt += rack->r_ctl.rack_rs.rs_us_rtt / 8;
8781 	}
8782 	if (rack->r_ctl.rack_rs.confidence) {
8783 		/*
8784 		 * record the low and high for highly buffered path computation,
8785 		 * we only do this if we are confident (not a retransmission).
8786 		 */
8787 		if (rack->r_ctl.rc_highest_us_rtt < rack->r_ctl.rack_rs.rs_us_rtt) {
8788 			rack->r_ctl.rc_highest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
8789 		}
8790 		if (rack->rc_highly_buffered == 0) {
8791 			/*
8792 			 * Currently once we declare a path has
8793 			 * highly buffered there is no going
8794 			 * back, which may be a problem...
8795 			 */
8796 			if ((rack->r_ctl.rc_highest_us_rtt / rack->r_ctl.rc_lowest_us_rtt) > rack_hbp_thresh) {
8797 				rack_log_rtt_shrinks(rack, rack->r_ctl.rack_rs.rs_us_rtt,
8798 						     rack->r_ctl.rc_highest_us_rtt,
8799 						     rack->r_ctl.rc_lowest_us_rtt,
8800 						     RACK_RTTS_SEEHBP);
8801 				rack->rc_highly_buffered = 1;
8802 			}
8803 		}
8804 	}
8805 	if ((rack->r_ctl.rack_rs.confidence) ||
8806 	    (rack->r_ctl.rack_rs.rs_us_rtrcnt == 1)) {
8807 		/*
8808 		 * If we are highly confident of it <or> it was
8809 		 * never retransmitted we accept it as the last us_rtt.
8810 		 */
8811 		rack->r_ctl.rc_last_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
8812 		/* The lowest rtt can be set if its was not retransmited */
8813 		if (rack->r_ctl.rc_lowest_us_rtt > rack->r_ctl.rack_rs.rs_us_rtt) {
8814 			rack->r_ctl.rc_lowest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
8815 			if (rack->r_ctl.rc_lowest_us_rtt == 0)
8816 				rack->r_ctl.rc_lowest_us_rtt = 1;
8817 		}
8818 	}
8819 	rack = (struct tcp_rack *)tp->t_fb_ptr;
8820 	if (tp->t_srtt != 0) {
8821 		/*
8822 		 * We keep a simple srtt in microseconds, like our rtt
8823 		 * measurement. We don't need to do any tricks with shifting
8824 		 * etc. Instead we just add in 1/8th of the new measurement
8825 		 * and subtract out 1/8 of the old srtt. We do the same with
8826 		 * the variance after finding the absolute value of the
8827 		 * difference between this sample and the current srtt.
8828 		 */
8829 		delta = tp->t_srtt - rtt;
8830 		/* Take off 1/8th of the current sRTT */
8831 		tp->t_srtt -= (tp->t_srtt >> 3);
8832 		/* Add in 1/8th of the new RTT just measured */
8833 		tp->t_srtt += (rtt >> 3);
8834 		if (tp->t_srtt <= 0)
8835 			tp->t_srtt = 1;
8836 		/* Now lets make the absolute value of the variance */
8837 		if (delta < 0)
8838 			delta = -delta;
8839 		/* Subtract out 1/8th */
8840 		tp->t_rttvar -= (tp->t_rttvar >> 3);
8841 		/* Add in 1/8th of the new variance we just saw */
8842 		tp->t_rttvar += (delta >> 3);
8843 		if (tp->t_rttvar <= 0)
8844 			tp->t_rttvar = 1;
8845 	} else {
8846 		/*
8847 		 * No rtt measurement yet - use the unsmoothed rtt. Set the
8848 		 * variance to half the rtt (so our first retransmit happens
8849 		 * at 3*rtt).
8850 		 */
8851 		tp->t_srtt = rtt;
8852 		tp->t_rttvar = rtt >> 1;
8853 	}
8854 	rack->rc_srtt_measure_made = 1;
8855 	KMOD_TCPSTAT_INC(tcps_rttupdated);
8856 	if (tp->t_rttupdated < UCHAR_MAX)
8857 		tp->t_rttupdated++;
8858 #ifdef STATS
8859 	if (rack_stats_gets_ms_rtt == 0) {
8860 		/* Send in the microsecond rtt used for rxt timeout purposes */
8861 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt));
8862 	} else if (rack_stats_gets_ms_rtt == 1) {
8863 		/* Send in the millisecond rtt used for rxt timeout purposes */
8864 		int32_t ms_rtt;
8865 
8866 		/* Round up */
8867 		ms_rtt = (rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC;
8868 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt));
8869 	} else if (rack_stats_gets_ms_rtt == 2) {
8870 		/* Send in the millisecond rtt has close to the path RTT as we can get  */
8871 		int32_t ms_rtt;
8872 
8873 		/* Round up */
8874 		ms_rtt = (rack->r_ctl.rack_rs.rs_us_rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC;
8875 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt));
8876 	}  else {
8877 		/* Send in the microsecond rtt has close to the path RTT as we can get  */
8878 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rack->r_ctl.rack_rs.rs_us_rtt));
8879 	}
8880 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_PATHRTT, imax(0, rack->r_ctl.rack_rs.rs_us_rtt));
8881 #endif
8882 	/*
8883 	 * the retransmit should happen at rtt + 4 * rttvar. Because of the
8884 	 * way we do the smoothing, srtt and rttvar will each average +1/2
8885 	 * tick of bias.  When we compute the retransmit timer, we want 1/2
8886 	 * tick of rounding and 1 extra tick because of +-1/2 tick
8887 	 * uncertainty in the firing of the timer.  The bias will give us
8888 	 * exactly the 1.5 tick we need.  But, because the bias is
8889 	 * statistical, we have to test that we don't drop below the minimum
8890 	 * feasible timer (which is 2 ticks).
8891 	 */
8892 	tp->t_rxtshift = 0;
8893 	RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
8894 		      max(rack_rto_min, rtt + 2), rack_rto_max, rack->r_ctl.timer_slop);
8895 	rack_log_rtt_sample(rack, rtt);
8896 	tp->t_softerror = 0;
8897 }
8898 
8899 
8900 static void
8901 rack_apply_updated_usrtt(struct tcp_rack *rack, uint32_t us_rtt, uint32_t us_cts)
8902 {
8903 	/*
8904 	 * Apply to filter the inbound us-rtt at us_cts.
8905 	 */
8906 	uint32_t old_rtt;
8907 
8908 	old_rtt = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
8909 	apply_filter_min_small(&rack->r_ctl.rc_gp_min_rtt,
8910 			       us_rtt, us_cts);
8911 	if (old_rtt > us_rtt) {
8912 		/* We just hit a new lower rtt time */
8913 		rack_log_rtt_shrinks(rack,  us_cts,  old_rtt,
8914 				     __LINE__, RACK_RTTS_NEWRTT);
8915 		/*
8916 		 * Only count it if its lower than what we saw within our
8917 		 * calculated range.
8918 		 */
8919 		if ((old_rtt - us_rtt) > rack_min_rtt_movement) {
8920 			if (rack_probertt_lower_within &&
8921 			    rack->rc_gp_dyn_mul &&
8922 			    (rack->use_fixed_rate == 0) &&
8923 			    (rack->rc_always_pace)) {
8924 				/*
8925 				 * We are seeing a new lower rtt very close
8926 				 * to the time that we would have entered probe-rtt.
8927 				 * This is probably due to the fact that a peer flow
8928 				 * has entered probe-rtt. Lets go in now too.
8929 				 */
8930 				uint32_t val;
8931 
8932 				val = rack_probertt_lower_within * rack_time_between_probertt;
8933 				val /= 100;
8934 				if ((rack->in_probe_rtt == 0)  &&
8935 				    ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= (rack_time_between_probertt - val)))	{
8936 					rack_enter_probertt(rack, us_cts);
8937 				}
8938 			}
8939 			rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
8940 		}
8941 	}
8942 }
8943 
8944 static int
8945 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack,
8946     struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack)
8947 {
8948 	uint32_t us_rtt;
8949 	int32_t i, all;
8950 	uint32_t t, len_acked;
8951 
8952 	if ((rsm->r_flags & RACK_ACKED) ||
8953 	    (rsm->r_flags & RACK_WAS_ACKED))
8954 		/* Already done */
8955 		return (0);
8956 	if (rsm->r_no_rtt_allowed) {
8957 		/* Not allowed */
8958 		return (0);
8959 	}
8960 	if (ack_type == CUM_ACKED) {
8961 		if (SEQ_GT(th_ack, rsm->r_end)) {
8962 			len_acked = rsm->r_end - rsm->r_start;
8963 			all = 1;
8964 		} else {
8965 			len_acked = th_ack - rsm->r_start;
8966 			all = 0;
8967 		}
8968 	} else {
8969 		len_acked = rsm->r_end - rsm->r_start;
8970 		all = 0;
8971 	}
8972 	if (rsm->r_rtr_cnt == 1) {
8973 
8974 		t = cts - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
8975 		if ((int)t <= 0)
8976 			t = 1;
8977 		if (!tp->t_rttlow || tp->t_rttlow > t)
8978 			tp->t_rttlow = t;
8979 		if (!rack->r_ctl.rc_rack_min_rtt ||
8980 		    SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
8981 			rack->r_ctl.rc_rack_min_rtt = t;
8982 			if (rack->r_ctl.rc_rack_min_rtt == 0) {
8983 				rack->r_ctl.rc_rack_min_rtt = 1;
8984 			}
8985 		}
8986 		if (TSTMP_GT(tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time), rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]))
8987 			us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8988 		else
8989 			us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8990 		if (us_rtt == 0)
8991 			us_rtt = 1;
8992 		if (CC_ALGO(tp)->rttsample != NULL) {
8993 			/* Kick the RTT to the CC */
8994 			CC_ALGO(tp)->rttsample(&tp->t_ccv, us_rtt, 1, rsm->r_fas);
8995 		}
8996 		rack_apply_updated_usrtt(rack, us_rtt, tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time));
8997 		if (ack_type == SACKED) {
8998 			rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 1);
8999 			tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt, 2 , rsm, rsm->r_rtr_cnt);
9000 		} else {
9001 			/*
9002 			 * We need to setup what our confidence
9003 			 * is in this ack.
9004 			 *
9005 			 * If the rsm was app limited and it is
9006 			 * less than a mss in length (the end
9007 			 * of the send) then we have a gap. If we
9008 			 * were app limited but say we were sending
9009 			 * multiple MSS's then we are more confident
9010 			 * int it.
9011 			 *
9012 			 * When we are not app-limited then we see if
9013 			 * the rsm is being included in the current
9014 			 * measurement, we tell this by the app_limited_needs_set
9015 			 * flag.
9016 			 *
9017 			 * Note that being cwnd blocked is not applimited
9018 			 * as well as the pacing delay between packets which
9019 			 * are sending only 1 or 2 MSS's also will show up
9020 			 * in the RTT. We probably need to examine this algorithm
9021 			 * a bit more and enhance it to account for the delay
9022 			 * between rsm's. We could do that by saving off the
9023 			 * pacing delay of each rsm (in an rsm) and then
9024 			 * factoring that in somehow though for now I am
9025 			 * not sure how :)
9026 			 */
9027 			int calc_conf = 0;
9028 
9029 			if (rsm->r_flags & RACK_APP_LIMITED) {
9030 				if (all && (len_acked <= ctf_fixed_maxseg(tp)))
9031 					calc_conf = 0;
9032 				else
9033 					calc_conf = 1;
9034 			} else if (rack->app_limited_needs_set == 0) {
9035 				calc_conf = 1;
9036 			} else {
9037 				calc_conf = 0;
9038 			}
9039 			rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 2);
9040 			tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt,
9041 					    calc_conf, rsm, rsm->r_rtr_cnt);
9042 		}
9043 		if ((rsm->r_flags & RACK_TLP) &&
9044 		    (!IN_FASTRECOVERY(tp->t_flags))) {
9045 			/* Segment was a TLP and our retrans matched */
9046 			if (rack->r_ctl.rc_tlp_cwnd_reduce) {
9047 				rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__);
9048 			}
9049 		}
9050 		if ((rack->r_ctl.rc_rack_tmit_time == 0) ||
9051 		    (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
9052 			    (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]))) {
9053 			/* New more recent rack_tmit_time */
9054 			rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
9055 			if (rack->r_ctl.rc_rack_tmit_time == 0)
9056 				rack->r_ctl.rc_rack_tmit_time = 1;
9057 			rack->rc_rack_rtt = t;
9058 		}
9059 		return (1);
9060 	}
9061 	/*
9062 	 * We clear the soft/rxtshift since we got an ack.
9063 	 * There is no assurance we will call the commit() function
9064 	 * so we need to clear these to avoid incorrect handling.
9065 	 */
9066 	tp->t_rxtshift = 0;
9067 	RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
9068 		      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
9069 	tp->t_softerror = 0;
9070 	if (to && (to->to_flags & TOF_TS) &&
9071 	    (ack_type == CUM_ACKED) &&
9072 	    (to->to_tsecr) &&
9073 	    ((rsm->r_flags & RACK_OVERMAX) == 0)) {
9074 		/*
9075 		 * Now which timestamp does it match? In this block the ACK
9076 		 * must be coming from a previous transmission.
9077 		 */
9078 		for (i = 0; i < rsm->r_rtr_cnt; i++) {
9079 			if (rack_ts_to_msec(rsm->r_tim_lastsent[i]) == to->to_tsecr) {
9080 				t = cts - (uint32_t)rsm->r_tim_lastsent[i];
9081 				if ((int)t <= 0)
9082 					t = 1;
9083 				if (CC_ALGO(tp)->rttsample != NULL) {
9084 					/*
9085 					 * Kick the RTT to the CC, here
9086 					 * we lie a bit in that we know the
9087 					 * retransmission is correct even though
9088 					 * we retransmitted. This is because
9089 					 * we match the timestamps.
9090 					 */
9091 					if (TSTMP_GT(tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time), rsm->r_tim_lastsent[i]))
9092 						us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - (uint32_t)rsm->r_tim_lastsent[i];
9093 					else
9094 						us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[i];
9095 					CC_ALGO(tp)->rttsample(&tp->t_ccv, us_rtt, 1, rsm->r_fas);
9096 				}
9097 				if ((i + 1) < rsm->r_rtr_cnt) {
9098 					/*
9099 					 * The peer ack'd from our previous
9100 					 * transmission. We have a spurious
9101 					 * retransmission and thus we dont
9102 					 * want to update our rack_rtt.
9103 					 *
9104 					 * Hmm should there be a CC revert here?
9105 					 *
9106 					 */
9107 					return (0);
9108 				}
9109 				if (!tp->t_rttlow || tp->t_rttlow > t)
9110 					tp->t_rttlow = t;
9111 				if (!rack->r_ctl.rc_rack_min_rtt || SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
9112 					rack->r_ctl.rc_rack_min_rtt = t;
9113 					if (rack->r_ctl.rc_rack_min_rtt == 0) {
9114 						rack->r_ctl.rc_rack_min_rtt = 1;
9115 					}
9116 				}
9117 				if ((rack->r_ctl.rc_rack_tmit_time == 0) ||
9118 				    (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
9119 					    (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]))) {
9120 					/* New more recent rack_tmit_time */
9121 					rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
9122 					if (rack->r_ctl.rc_rack_tmit_time == 0)
9123 						rack->r_ctl.rc_rack_tmit_time = 1;
9124 					rack->rc_rack_rtt = t;
9125 				}
9126 				rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[i], cts, 3);
9127 				tcp_rack_xmit_timer(rack, t + 1, len_acked, t, 0, rsm,
9128 						    rsm->r_rtr_cnt);
9129 				return (1);
9130 			}
9131 		}
9132 		/* If we are logging log out the sendmap */
9133 		if (tcp_bblogging_on(rack->rc_tp)) {
9134 			for (i = 0; i < rsm->r_rtr_cnt; i++) {
9135 				rack_log_rtt_sendmap(rack, i, rsm->r_tim_lastsent[i], to->to_tsecr);
9136 			}
9137 		}
9138 		goto ts_not_found;
9139 	} else {
9140 		/*
9141 		 * Ok its a SACK block that we retransmitted. or a windows
9142 		 * machine without timestamps. We can tell nothing from the
9143 		 * time-stamp since its not there or the time the peer last
9144 		 * recieved a segment that moved forward its cum-ack point.
9145 		 */
9146 ts_not_found:
9147 		i = rsm->r_rtr_cnt - 1;
9148 		t = cts - (uint32_t)rsm->r_tim_lastsent[i];
9149 		if ((int)t <= 0)
9150 			t = 1;
9151 		if (rack->r_ctl.rc_rack_min_rtt && SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
9152 			/*
9153 			 * We retransmitted and the ack came back in less
9154 			 * than the smallest rtt we have observed. We most
9155 			 * likely did an improper retransmit as outlined in
9156 			 * 6.2 Step 2 point 2 in the rack-draft so we
9157 			 * don't want to update our rack_rtt. We in
9158 			 * theory (in future) might want to think about reverting our
9159 			 * cwnd state but we won't for now.
9160 			 */
9161 			return (0);
9162 		} else if (rack->r_ctl.rc_rack_min_rtt) {
9163 			/*
9164 			 * We retransmitted it and the retransmit did the
9165 			 * job.
9166 			 */
9167 			if (!rack->r_ctl.rc_rack_min_rtt ||
9168 			    SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
9169 				rack->r_ctl.rc_rack_min_rtt = t;
9170 				if (rack->r_ctl.rc_rack_min_rtt == 0) {
9171 					rack->r_ctl.rc_rack_min_rtt = 1;
9172 				}
9173 			}
9174 			if ((rack->r_ctl.rc_rack_tmit_time == 0) ||
9175 			    (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
9176 				    (uint32_t)rsm->r_tim_lastsent[i]))) {
9177 				/* New more recent rack_tmit_time */
9178 				rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[i];
9179 				if (rack->r_ctl.rc_rack_tmit_time == 0)
9180 					rack->r_ctl.rc_rack_tmit_time = 1;
9181 				rack->rc_rack_rtt = t;
9182 			}
9183 			return (1);
9184 		}
9185 	}
9186 	return (0);
9187 }
9188 
9189 /*
9190  * Mark the SACK_PASSED flag on all entries prior to rsm send wise.
9191  */
9192 static void
9193 rack_log_sack_passed(struct tcpcb *tp,
9194     struct tcp_rack *rack, struct rack_sendmap *rsm)
9195 {
9196 	struct rack_sendmap *nrsm;
9197 
9198 	nrsm = rsm;
9199 	TAILQ_FOREACH_REVERSE_FROM(nrsm, &rack->r_ctl.rc_tmap,
9200 	    rack_head, r_tnext) {
9201 		if (nrsm == rsm) {
9202 			/* Skip original segment he is acked */
9203 			continue;
9204 		}
9205 		if (nrsm->r_flags & RACK_ACKED) {
9206 			/*
9207 			 * Skip ack'd segments, though we
9208 			 * should not see these, since tmap
9209 			 * should not have ack'd segments.
9210 			 */
9211 			continue;
9212 		}
9213 		if (nrsm->r_flags & RACK_RWND_COLLAPSED) {
9214 			/*
9215 			 * If the peer dropped the rwnd on
9216 			 * these then we don't worry about them.
9217 			 */
9218 			continue;
9219 		}
9220 		if (nrsm->r_flags & RACK_SACK_PASSED) {
9221 			/*
9222 			 * We found one that is already marked
9223 			 * passed, we have been here before and
9224 			 * so all others below this are marked.
9225 			 */
9226 			break;
9227 		}
9228 		nrsm->r_flags |= RACK_SACK_PASSED;
9229 		nrsm->r_flags &= ~RACK_WAS_SACKPASS;
9230 	}
9231 }
9232 
9233 static void
9234 rack_need_set_test(struct tcpcb *tp,
9235 		   struct tcp_rack *rack,
9236 		   struct rack_sendmap *rsm,
9237 		   tcp_seq th_ack,
9238 		   int line,
9239 		   int use_which)
9240 {
9241 	struct rack_sendmap *s_rsm;
9242 
9243 	if ((tp->t_flags & TF_GPUTINPROG) &&
9244 	    SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
9245 		/*
9246 		 * We were app limited, and this ack
9247 		 * butts up or goes beyond the point where we want
9248 		 * to start our next measurement. We need
9249 		 * to record the new gput_ts as here and
9250 		 * possibly update the start sequence.
9251 		 */
9252 		uint32_t seq, ts;
9253 
9254 		if (rsm->r_rtr_cnt > 1) {
9255 			/*
9256 			 * This is a retransmit, can we
9257 			 * really make any assessment at this
9258 			 * point?  We are not really sure of
9259 			 * the timestamp, is it this or the
9260 			 * previous transmission?
9261 			 *
9262 			 * Lets wait for something better that
9263 			 * is not retransmitted.
9264 			 */
9265 			return;
9266 		}
9267 		seq = tp->gput_seq;
9268 		ts = tp->gput_ts;
9269 		rack->app_limited_needs_set = 0;
9270 		tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
9271 		/* Do we start at a new end? */
9272 		if ((use_which == RACK_USE_BEG) &&
9273 		    SEQ_GEQ(rsm->r_start, tp->gput_seq)) {
9274 			/*
9275 			 * When we get an ACK that just eats
9276 			 * up some of the rsm, we set RACK_USE_BEG
9277 			 * since whats at r_start (i.e. th_ack)
9278 			 * is left unacked and thats where the
9279 			 * measurement now starts.
9280 			 */
9281 			tp->gput_seq = rsm->r_start;
9282 		}
9283 		if ((use_which == RACK_USE_END) &&
9284 		    SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
9285 			/*
9286 			 * We use the end when the cumack
9287 			 * is moving forward and completely
9288 			 * deleting the rsm passed so basically
9289 			 * r_end holds th_ack.
9290 			 *
9291 			 * For SACK's we also want to use the end
9292 			 * since this piece just got sacked and
9293 			 * we want to target anything after that
9294 			 * in our measurement.
9295 			 */
9296 			tp->gput_seq = rsm->r_end;
9297 		}
9298 		if (use_which == RACK_USE_END_OR_THACK) {
9299 			/*
9300 			 * special case for ack moving forward,
9301 			 * not a sack, we need to move all the
9302 			 * way up to where this ack cum-ack moves
9303 			 * to.
9304 			 */
9305 			if (SEQ_GT(th_ack, rsm->r_end))
9306 				tp->gput_seq = th_ack;
9307 			else
9308 				tp->gput_seq = rsm->r_end;
9309 		}
9310 		if (SEQ_LT(tp->gput_seq, tp->snd_max))
9311 			s_rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
9312 		else
9313 			s_rsm = NULL;
9314 		/*
9315 		 * Pick up the correct send time if we can the rsm passed in
9316 		 * may be equal to s_rsm if the RACK_USE_BEG was set. For the other
9317 		 * two cases (RACK_USE_THACK or RACK_USE_END) most likely we will
9318 		 * find a different seq i.e. the next send up.
9319 		 *
9320 		 * If that has not been sent, s_rsm will be NULL and we must
9321 		 * arrange it so this function will get called again by setting
9322 		 * app_limited_needs_set.
9323 		 */
9324 		if (s_rsm)
9325 			rack->r_ctl.rc_gp_output_ts = s_rsm->r_tim_lastsent[0];
9326 		else {
9327 			/* If we hit here we have to have *not* sent tp->gput_seq */
9328 			rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[0];
9329 			/* Set it up so we will go through here again */
9330 			rack->app_limited_needs_set = 1;
9331 		}
9332 		if (SEQ_GT(tp->gput_seq, tp->gput_ack)) {
9333 			/*
9334 			 * We moved beyond this guy's range, re-calculate
9335 			 * the new end point.
9336 			 */
9337 			if (rack->rc_gp_filled == 0) {
9338 				tp->gput_ack = tp->gput_seq + max(rc_init_window(rack), (MIN_GP_WIN * ctf_fixed_maxseg(tp)));
9339 			} else {
9340 				tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
9341 			}
9342 		}
9343 		/*
9344 		 * We are moving the goal post, we may be able to clear the
9345 		 * measure_saw_probe_rtt flag.
9346 		 */
9347 		if ((rack->in_probe_rtt == 0) &&
9348 		    (rack->measure_saw_probe_rtt) &&
9349 		    (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
9350 			rack->measure_saw_probe_rtt = 0;
9351 		rack_log_pacing_delay_calc(rack, ts, tp->gput_ts,
9352 					   seq, tp->gput_seq,
9353 					   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) |
9354 					    (uint64_t)rack->r_ctl.rc_gp_output_ts),
9355 					   5, line, NULL, 0);
9356 		if (rack->rc_gp_filled &&
9357 		    ((tp->gput_ack - tp->gput_seq) <
9358 		     max(rc_init_window(rack), (MIN_GP_WIN *
9359 						ctf_fixed_maxseg(tp))))) {
9360 			uint32_t ideal_amount;
9361 
9362 			ideal_amount = rack_get_measure_window(tp, rack);
9363 			if (ideal_amount > sbavail(&tptosocket(tp)->so_snd)) {
9364 				/*
9365 				 * There is no sense of continuing this measurement
9366 				 * because its too small to gain us anything we
9367 				 * trust. Skip it and that way we can start a new
9368 				 * measurement quicker.
9369 				 */
9370 				tp->t_flags &= ~TF_GPUTINPROG;
9371 				rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq,
9372 							   0, 0,
9373 							   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) |
9374 							    (uint64_t)rack->r_ctl.rc_gp_output_ts),
9375 							   6, __LINE__, NULL, 0);
9376 			} else {
9377 				/*
9378 				 * Reset the window further out.
9379 				 */
9380 				tp->gput_ack = tp->gput_seq + ideal_amount;
9381 			}
9382 		}
9383 		rack_tend_gp_marks(tp, rack);
9384 		rack_log_gpset(rack, tp->gput_ack, 0, 0, line, 2, rsm);
9385 	}
9386 }
9387 
9388 static inline int
9389 is_rsm_inside_declared_tlp_block(struct tcp_rack *rack, struct rack_sendmap *rsm)
9390 {
9391 	if (SEQ_LT(rsm->r_end, rack->r_ctl.last_tlp_acked_start)) {
9392 		/* Behind our TLP definition or right at */
9393 		return (0);
9394 	}
9395 	if (SEQ_GT(rsm->r_start, rack->r_ctl.last_tlp_acked_end)) {
9396 		/* The start is beyond or right at our end of TLP definition */
9397 		return (0);
9398 	}
9399 	/* It has to be a sub-part of the original TLP recorded */
9400 	return (1);
9401 }
9402 
9403 
9404 
9405 static uint32_t
9406 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack, struct sackblk *sack,
9407 		   struct tcpopt *to, struct rack_sendmap **prsm, uint32_t cts,
9408 		   int *no_extra,
9409 		   int *moved_two, uint32_t segsiz)
9410 {
9411 	uint32_t start, end, changed = 0;
9412 	struct rack_sendmap stack_map;
9413 	struct rack_sendmap *rsm, *nrsm, *prev, *next;
9414 	int insret __diagused;
9415 	int32_t used_ref = 1;
9416 	int moved = 0;
9417 #ifdef TCP_SAD_DETECTION
9418 	int allow_segsiz;
9419 	int first_time_through = 1;
9420 #endif
9421 	int noextra = 0;
9422 	int can_use_hookery = 0;
9423 
9424 	start = sack->start;
9425 	end = sack->end;
9426 	rsm = *prsm;
9427 
9428 #ifdef TCP_SAD_DETECTION
9429 	/*
9430 	 * There are a strange number of proxys and meddle boxes in the world
9431 	 * that seem to cut up segments on different boundaries. This gets us
9432 	 * smaller sacks that are still ok in terms of it being an attacker.
9433 	 * We use the base segsiz to calculate an allowable smallness but
9434 	 * also enforce a min on the segsiz in case it is an attacker playing
9435 	 * games with MSS. So basically if the sack arrives and it is
9436 	 * larger than a worse case 960 bytes, we don't classify the guy
9437 	 * as supicious.
9438 	 */
9439 	allow_segsiz = max(segsiz, 1200) * sad_seg_size_per;
9440 	allow_segsiz /= 1000;
9441 #endif
9442 do_rest_ofb:
9443 	if ((rsm == NULL) ||
9444 	    (SEQ_LT(end, rsm->r_start)) ||
9445 	    (SEQ_GEQ(start, rsm->r_end)) ||
9446 	    (SEQ_LT(start, rsm->r_start))) {
9447 		/*
9448 		 * We are not in the right spot,
9449 		 * find the correct spot in the tree.
9450 		 */
9451 		used_ref = 0;
9452 		rsm = tqhash_find(rack->r_ctl.tqh, start);
9453 		moved++;
9454 	}
9455 	if (rsm == NULL) {
9456 		/* TSNH */
9457 		goto out;
9458 	}
9459 #ifdef TCP_SAD_DETECTION
9460 	/* Now we must check for suspicous activity */
9461 	if ((first_time_through == 1) &&
9462 	    ((end - start) < min((rsm->r_end - rsm->r_start), allow_segsiz)) &&
9463 	    ((rsm->r_flags & RACK_PMTU_CHG) == 0) &&
9464 	    ((rsm->r_flags & RACK_TLP) == 0)) {
9465 		/*
9466 		 * Its less than a full MSS or the segment being acked
9467 		 * this should only happen if the rsm in question had the
9468 		 * r_just_ret flag set <and> the end matches the end of
9469 		 * the rsm block.
9470 		 *
9471 		 * Note we do not look at segments that have had TLP's on
9472 		 * them since we can get un-reported rwnd collapses that
9473 		 * basically we TLP on and then we get back a sack block
9474 		 * that goes from the start to only a small way.
9475 		 *
9476 		 */
9477 		int loss, ok;
9478 
9479 		ok = 0;
9480 		if (SEQ_GEQ(end, rsm->r_end)) {
9481 			if (rsm->r_just_ret == 1) {
9482 				/* This was at the end of a send which is ok */
9483 				ok = 1;
9484 			} else {
9485 				/* A bit harder was it the end of our segment */
9486 				int segs, len;
9487 
9488 				len = (rsm->r_end - rsm->r_start);
9489 				segs = len / segsiz;
9490 				segs *= segsiz;
9491 				if ((segs + (rsm->r_end - start)) == len) {
9492 					/*
9493 					 * So this last bit was the
9494 					 * end of our send if we cut it
9495 					 * up into segsiz pieces so its ok.
9496 					 */
9497 					ok = 1;
9498 				}
9499 			}
9500 		}
9501 		if (ok == 0) {
9502 			/*
9503 			 * This guy is doing something suspicious
9504 			 * lets start detection.
9505 			 */
9506 			if (rack->rc_suspicious == 0) {
9507 				tcp_trace_point(rack->rc_tp, TCP_TP_SAD_SUSPECT);
9508 				counter_u64_add(rack_sack_attacks_suspect, 1);
9509 				rack->rc_suspicious = 1;
9510 				rack_log_sad(rack, 4);
9511 				if (tcp_bblogging_on(rack->rc_tp)) {
9512 					union tcp_log_stackspecific log;
9513 					struct timeval tv;
9514 
9515 					memset(&log.u_bbr, 0, sizeof(log.u_bbr));
9516 					log.u_bbr.flex1 = end;
9517 					log.u_bbr.flex2 = start;
9518 					log.u_bbr.flex3 = rsm->r_end;
9519 					log.u_bbr.flex4 = rsm->r_start;
9520 					log.u_bbr.flex5 = segsiz;
9521 					log.u_bbr.flex6 = rsm->r_fas;
9522 					log.u_bbr.flex7 = rsm->r_bas;
9523 					log.u_bbr.flex8 = 5;
9524 					log.u_bbr.pkts_out = rsm->r_flags;
9525 					log.u_bbr.bbr_state = rack->rc_suspicious;
9526 					log.u_bbr.bbr_substate = rsm->r_just_ret;
9527 					log.u_bbr.timeStamp = tcp_get_usecs(&tv);
9528 					log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
9529 					TCP_LOG_EVENTP(rack->rc_tp, NULL,
9530 						       &rack->rc_inp->inp_socket->so_rcv,
9531 						       &rack->rc_inp->inp_socket->so_snd,
9532 						       TCP_SAD_DETECTION, 0,
9533 						       0, &log, false, &tv);
9534 				}
9535 			}
9536 			/* You loose some ack count every time you sack
9537 			 * a small bit that is not butting to the end of
9538 			 * what we have sent. This is because we never
9539 			 * send small bits unless its the end of the sb.
9540 			 * Anyone sending a sack that is not at the end
9541 			 * is thus very very suspicious.
9542 			 */
9543 			loss = (segsiz/2) / (end - start);
9544 			if (loss < rack->r_ctl.ack_count)
9545 				rack->r_ctl.ack_count -= loss;
9546 			else
9547 				rack->r_ctl.ack_count = 0;
9548 		}
9549 	}
9550 	first_time_through = 0;
9551 #endif
9552 	/* Ok we have an ACK for some piece of this rsm */
9553 	if (rsm->r_start != start) {
9554 		if ((rsm->r_flags & RACK_ACKED) == 0) {
9555 			/*
9556 			 * Before any splitting or hookery is
9557 			 * done is it a TLP of interest i.e. rxt?
9558 			 */
9559 			if ((rsm->r_flags & RACK_TLP) &&
9560 			    (rsm->r_rtr_cnt > 1)) {
9561 				/*
9562 				 * We are splitting a rxt TLP, check
9563 				 * if we need to save off the start/end
9564 				 */
9565 				if (rack->rc_last_tlp_acked_set &&
9566 				    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
9567 					/*
9568 					 * We already turned this on since we are inside
9569 					 * the previous one was a partially sack now we
9570 					 * are getting another one (maybe all of it).
9571 					 *
9572 					 */
9573 					rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
9574 					/*
9575 					 * Lets make sure we have all of it though.
9576 					 */
9577 					if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
9578 						rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9579 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9580 								     rack->r_ctl.last_tlp_acked_end);
9581 					}
9582 					if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
9583 						rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9584 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9585 								     rack->r_ctl.last_tlp_acked_end);
9586 					}
9587 				} else {
9588 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9589 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9590 					rack->rc_last_tlp_past_cumack = 0;
9591 					rack->rc_last_tlp_acked_set = 1;
9592 					rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
9593 				}
9594 			}
9595 			/**
9596 			 * Need to split this in two pieces the before and after,
9597 			 * the before remains in the map, the after must be
9598 			 * added. In other words we have:
9599 			 * rsm        |--------------|
9600 			 * sackblk        |------->
9601 			 * rsm will become
9602 			 *     rsm    |---|
9603 			 * and nrsm will be  the sacked piece
9604 			 *     nrsm       |----------|
9605 			 *
9606 			 * But before we start down that path lets
9607 			 * see if the sack spans over on top of
9608 			 * the next guy and it is already sacked.
9609 			 *
9610 			 */
9611 			/*
9612 			 * Hookery can only be used if the two entries
9613 			 * are in the same bucket and neither one of
9614 			 * them staddle the bucket line.
9615 			 */
9616 			next = tqhash_next(rack->r_ctl.tqh, rsm);
9617 			if (next &&
9618 			    (rsm->bindex == next->bindex) &&
9619 			    ((rsm->r_flags & RACK_STRADDLE) == 0) &&
9620 			    ((next->r_flags & RACK_STRADDLE) == 0) &&
9621 			    (rsm->r_flags & RACK_IN_GP_WIN) &&
9622 			    (next->r_flags & RACK_IN_GP_WIN))
9623 				can_use_hookery = 1;
9624 			else if (next &&
9625 				 (rsm->bindex == next->bindex) &&
9626 				 ((rsm->r_flags & RACK_STRADDLE) == 0) &&
9627 				 ((next->r_flags & RACK_STRADDLE) == 0) &&
9628 				 ((rsm->r_flags & RACK_IN_GP_WIN) == 0) &&
9629 				 ((next->r_flags & RACK_IN_GP_WIN) == 0))
9630 				can_use_hookery = 1;
9631 			else
9632 				can_use_hookery = 0;
9633 			if (next && can_use_hookery &&
9634 			    (next->r_flags & RACK_ACKED) &&
9635 			    SEQ_GEQ(end, next->r_start)) {
9636 				/**
9637 				 * So the next one is already acked, and
9638 				 * we can thus by hookery use our stack_map
9639 				 * to reflect the piece being sacked and
9640 				 * then adjust the two tree entries moving
9641 				 * the start and ends around. So we start like:
9642 				 *  rsm     |------------|             (not-acked)
9643 				 *  next                 |-----------| (acked)
9644 				 *  sackblk        |-------->
9645 				 *  We want to end like so:
9646 				 *  rsm     |------|                   (not-acked)
9647 				 *  next           |-----------------| (acked)
9648 				 *  nrsm           |-----|
9649 				 * Where nrsm is a temporary stack piece we
9650 				 * use to update all the gizmos.
9651 				 */
9652 				/* Copy up our fudge block */
9653 				noextra++;
9654 				nrsm = &stack_map;
9655 				memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
9656 				/* Now adjust our tree blocks */
9657 				rsm->r_end = start;
9658 				next->r_start = start;
9659  				rsm->r_flags |= RACK_SHUFFLED;
9660 				next->r_flags |= RACK_SHUFFLED;
9661 				/* Now we must adjust back where next->m is */
9662 				rack_setup_offset_for_rsm(rack, rsm, next);
9663 				/*
9664 				 * Which timestamp do we keep? It is rather
9665 				 * important in GP measurements to have the
9666 				 * accurate end of the send window.
9667 				 *
9668 				 * We keep the largest value, which is the newest
9669 				 * send. We do this in case a segment that is
9670 				 * joined together and not part of a GP estimate
9671 				 * later gets expanded into the GP estimate.
9672 				 *
9673 				 * We prohibit the merging of unlike kinds i.e.
9674 				 * all pieces that are in the GP estimate can be
9675 				 * merged and all pieces that are not in a GP estimate
9676 				 * can be merged, but not disimilar pieces. Combine
9677 				 * this with taking the highest here and we should
9678 				 * be ok unless of course the client reneges. Then
9679 				 * all bets are off.
9680 				 */
9681 				if (next->r_tim_lastsent[(next->r_rtr_cnt-1)] <
9682 				    nrsm->r_tim_lastsent[(nrsm->r_rtr_cnt-1)])
9683 					next->r_tim_lastsent[(next->r_rtr_cnt-1)] = nrsm->r_tim_lastsent[(nrsm->r_rtr_cnt-1)];
9684 				/*
9685 				 * And we must keep the newest ack arrival time.
9686 				 */
9687 				if (next->r_ack_arrival <
9688 				    rack_to_usec_ts(&rack->r_ctl.act_rcv_time))
9689 					next->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
9690 
9691 
9692 				/* We don't need to adjust rsm, it did not change */
9693 				/* Clear out the dup ack count of the remainder */
9694 				rsm->r_dupack = 0;
9695 				rsm->r_just_ret = 0;
9696 				rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
9697 				/* Now lets make sure our fudge block is right */
9698 				nrsm->r_start = start;
9699 				/* Now lets update all the stats and such */
9700 				rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0);
9701 				if (rack->app_limited_needs_set)
9702 					rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END);
9703 				changed += (nrsm->r_end - nrsm->r_start);
9704 				/* You get a count for acking a whole segment or more */
9705 				if ((nrsm->r_end - nrsm->r_start) >= segsiz)
9706 					rack->r_ctl.ack_count += ((nrsm->r_end - nrsm->r_start) / segsiz);
9707 				rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start);
9708 				if (nrsm->r_flags & RACK_SACK_PASSED) {
9709 					rack->r_ctl.rc_reorder_ts = cts;
9710 					if (rack->r_ctl.rc_reorder_ts == 0)
9711 						rack->r_ctl.rc_reorder_ts = 1;
9712 				}
9713 				/*
9714 				 * Now we want to go up from rsm (the
9715 				 * one left un-acked) to the next one
9716 				 * in the tmap. We do this so when
9717 				 * we walk backwards we include marking
9718 				 * sack-passed on rsm (The one passed in
9719 				 * is skipped since it is generally called
9720 				 * on something sacked before removing it
9721 				 * from the tmap).
9722 				 */
9723 				if (rsm->r_in_tmap) {
9724 					nrsm = TAILQ_NEXT(rsm, r_tnext);
9725 					/*
9726 					 * Now that we have the next
9727 					 * one walk backwards from there.
9728 					 */
9729 					if (nrsm && nrsm->r_in_tmap)
9730 						rack_log_sack_passed(tp, rack, nrsm);
9731 				}
9732 				/* Now are we done? */
9733 				if (SEQ_LT(end, next->r_end) ||
9734 				    (end == next->r_end)) {
9735 					/* Done with block */
9736 					goto out;
9737 				}
9738 				rack_log_map_chg(tp, rack, &stack_map, rsm, next, MAP_SACK_M1, end, __LINE__);
9739 				counter_u64_add(rack_sack_used_next_merge, 1);
9740 				/* Postion for the next block */
9741 				start = next->r_end;
9742 				rsm = tqhash_next(rack->r_ctl.tqh, next);
9743 				if (rsm == NULL)
9744 					goto out;
9745 			} else {
9746 				/**
9747 				 * We can't use any hookery here, so we
9748 				 * need to split the map. We enter like
9749 				 * so:
9750 				 *  rsm      |--------|
9751 				 *  sackblk       |----->
9752 				 * We will add the new block nrsm and
9753 				 * that will be the new portion, and then
9754 				 * fall through after reseting rsm. So we
9755 				 * split and look like this:
9756 				 *  rsm      |----|
9757 				 *  sackblk       |----->
9758 				 *  nrsm          |---|
9759 				 * We then fall through reseting
9760 				 * rsm to nrsm, so the next block
9761 				 * picks it up.
9762 				 */
9763 				nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
9764 				if (nrsm == NULL) {
9765 					/*
9766 					 * failed XXXrrs what can we do but loose the sack
9767 					 * info?
9768 					 */
9769 					goto out;
9770 				}
9771 				counter_u64_add(rack_sack_splits, 1);
9772 				rack_clone_rsm(rack, nrsm, rsm, start);
9773 				moved++;
9774 				rsm->r_just_ret = 0;
9775 #ifndef INVARIANTS
9776 				(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
9777 #else
9778 				if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
9779 					panic("Insert in rb tree of %p fails ret:%d rack:%p rsm:%p",
9780 					      nrsm, insret, rack, rsm);
9781 				}
9782 #endif
9783 				if (rsm->r_in_tmap) {
9784 					TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
9785 					nrsm->r_in_tmap = 1;
9786 				}
9787 				rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M2, end, __LINE__);
9788 				rsm->r_flags &= (~RACK_HAS_FIN);
9789 				/* Position us to point to the new nrsm that starts the sack blk */
9790 				rsm = nrsm;
9791 			}
9792 		} else {
9793 			/* Already sacked this piece */
9794 			counter_u64_add(rack_sack_skipped_acked, 1);
9795 			moved++;
9796 			if (end == rsm->r_end) {
9797 				/* Done with block */
9798 				rsm = tqhash_next(rack->r_ctl.tqh, rsm);
9799 				goto out;
9800 			} else if (SEQ_LT(end, rsm->r_end)) {
9801 				/* A partial sack to a already sacked block */
9802 				moved++;
9803 				rsm = tqhash_next(rack->r_ctl.tqh, rsm);
9804 				goto out;
9805 			} else {
9806 				/*
9807 				 * The end goes beyond this guy
9808 				 * reposition the start to the
9809 				 * next block.
9810 				 */
9811 				start = rsm->r_end;
9812 				rsm = tqhash_next(rack->r_ctl.tqh, rsm);
9813 				if (rsm == NULL)
9814 					goto out;
9815 			}
9816 		}
9817 	}
9818 	if (SEQ_GEQ(end, rsm->r_end)) {
9819 		/**
9820 		 * The end of this block is either beyond this guy or right
9821 		 * at this guy. I.e.:
9822 		 *  rsm ---                 |-----|
9823 		 *  end                     |-----|
9824 		 *  <or>
9825 		 *  end                     |---------|
9826 		 */
9827 		if ((rsm->r_flags & RACK_ACKED) == 0) {
9828 			/*
9829 			 * Is it a TLP of interest?
9830 			 */
9831 			if ((rsm->r_flags & RACK_TLP) &&
9832 			    (rsm->r_rtr_cnt > 1)) {
9833 				/*
9834 				 * We are splitting a rxt TLP, check
9835 				 * if we need to save off the start/end
9836 				 */
9837 				if (rack->rc_last_tlp_acked_set &&
9838 				    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
9839 					/*
9840 					 * We already turned this on since we are inside
9841 					 * the previous one was a partially sack now we
9842 					 * are getting another one (maybe all of it).
9843 					 */
9844 					rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
9845 					/*
9846 					 * Lets make sure we have all of it though.
9847 					 */
9848 					if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
9849 						rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9850 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9851 								     rack->r_ctl.last_tlp_acked_end);
9852 					}
9853 					if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
9854 						rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9855 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9856 								     rack->r_ctl.last_tlp_acked_end);
9857 					}
9858 				} else {
9859 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9860 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9861 					rack->rc_last_tlp_past_cumack = 0;
9862 					rack->rc_last_tlp_acked_set = 1;
9863 					rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
9864 				}
9865 			}
9866 			rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0);
9867 			changed += (rsm->r_end - rsm->r_start);
9868 			/* You get a count for acking a whole segment or more */
9869 			if ((rsm->r_end - rsm->r_start) >= segsiz)
9870 				rack->r_ctl.ack_count += ((rsm->r_end - rsm->r_start) / segsiz);
9871 			rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
9872 			if (rsm->r_in_tmap) /* should be true */
9873 				rack_log_sack_passed(tp, rack, rsm);
9874 			/* Is Reordering occuring? */
9875 			if (rsm->r_flags & RACK_SACK_PASSED) {
9876 				rsm->r_flags &= ~RACK_SACK_PASSED;
9877 				rack->r_ctl.rc_reorder_ts = cts;
9878 				if (rack->r_ctl.rc_reorder_ts == 0)
9879 					rack->r_ctl.rc_reorder_ts = 1;
9880 			}
9881 			if (rack->app_limited_needs_set)
9882 				rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END);
9883 			rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
9884 			rsm->r_flags |= RACK_ACKED;
9885 			if (rsm->r_in_tmap) {
9886 				TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
9887 				rsm->r_in_tmap = 0;
9888 			}
9889 			rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_SACK_M3, end, __LINE__);
9890 		} else {
9891 			counter_u64_add(rack_sack_skipped_acked, 1);
9892 			moved++;
9893 		}
9894 		if (end == rsm->r_end) {
9895 			/* This block only - done, setup for next */
9896 			goto out;
9897 		}
9898 		/*
9899 		 * There is more not coverend by this rsm move on
9900 		 * to the next block in the RB tree.
9901 		 */
9902 		nrsm = tqhash_next(rack->r_ctl.tqh, rsm);
9903 		start = rsm->r_end;
9904 		rsm = nrsm;
9905 		if (rsm == NULL)
9906 			goto out;
9907 		goto do_rest_ofb;
9908 	}
9909 	/**
9910 	 * The end of this sack block is smaller than
9911 	 * our rsm i.e.:
9912 	 *  rsm ---                 |-----|
9913 	 *  end                     |--|
9914 	 */
9915 	if ((rsm->r_flags & RACK_ACKED) == 0) {
9916 		/*
9917 		 * Is it a TLP of interest?
9918 		 */
9919 		if ((rsm->r_flags & RACK_TLP) &&
9920 		    (rsm->r_rtr_cnt > 1)) {
9921 			/*
9922 			 * We are splitting a rxt TLP, check
9923 			 * if we need to save off the start/end
9924 			 */
9925 			if (rack->rc_last_tlp_acked_set &&
9926 			    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
9927 				/*
9928 				 * We already turned this on since we are inside
9929 				 * the previous one was a partially sack now we
9930 				 * are getting another one (maybe all of it).
9931 				 */
9932 				rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
9933 				/*
9934 				 * Lets make sure we have all of it though.
9935 				 */
9936 				if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
9937 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9938 					rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9939 							     rack->r_ctl.last_tlp_acked_end);
9940 				}
9941 				if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
9942 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9943 					rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9944 							     rack->r_ctl.last_tlp_acked_end);
9945 				}
9946 			} else {
9947 				rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9948 				rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9949 				rack->rc_last_tlp_past_cumack = 0;
9950 				rack->rc_last_tlp_acked_set = 1;
9951 				rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
9952 			}
9953 		}
9954 		/*
9955 		 * Hookery can only be used if the two entries
9956 		 * are in the same bucket and neither one of
9957 		 * them staddle the bucket line.
9958 		 */
9959 		prev = tqhash_prev(rack->r_ctl.tqh, rsm);
9960 		if (prev &&
9961 		    (rsm->bindex == prev->bindex) &&
9962 		    ((rsm->r_flags & RACK_STRADDLE) == 0) &&
9963 		    ((prev->r_flags & RACK_STRADDLE) == 0) &&
9964 		    (rsm->r_flags & RACK_IN_GP_WIN) &&
9965 		    (prev->r_flags & RACK_IN_GP_WIN))
9966 			can_use_hookery = 1;
9967 		else if (prev &&
9968 			 (rsm->bindex == prev->bindex) &&
9969 			 ((rsm->r_flags & RACK_STRADDLE) == 0) &&
9970 			 ((prev->r_flags & RACK_STRADDLE) == 0) &&
9971 			 ((rsm->r_flags & RACK_IN_GP_WIN) == 0) &&
9972 			 ((prev->r_flags & RACK_IN_GP_WIN) == 0))
9973 			can_use_hookery = 1;
9974 		else
9975 			can_use_hookery = 0;
9976 
9977 		if (prev && can_use_hookery &&
9978 		    (prev->r_flags & RACK_ACKED)) {
9979 			/**
9980 			 * Goal, we want the right remainder of rsm to shrink
9981 			 * in place and span from (rsm->r_start = end) to rsm->r_end.
9982 			 * We want to expand prev to go all the way
9983 			 * to prev->r_end <- end.
9984 			 * so in the tree we have before:
9985 			 *   prev     |--------|         (acked)
9986 			 *   rsm               |-------| (non-acked)
9987 			 *   sackblk           |-|
9988 			 * We churn it so we end up with
9989 			 *   prev     |----------|       (acked)
9990 			 *   rsm                 |-----| (non-acked)
9991 			 *   nrsm              |-| (temporary)
9992 			 *
9993 			 * Note if either prev/rsm is a TLP we don't
9994 			 * do this.
9995 			 */
9996 			noextra++;
9997 			nrsm = &stack_map;
9998 			memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
9999 			prev->r_end = end;
10000 			rsm->r_start = end;
10001 			rsm->r_flags |= RACK_SHUFFLED;
10002 			prev->r_flags |= RACK_SHUFFLED;
10003 			/* Now adjust nrsm (stack copy) to be
10004 			 * the one that is the small
10005 			 * piece that was "sacked".
10006 			 */
10007 			nrsm->r_end = end;
10008 			rsm->r_dupack = 0;
10009 			/*
10010 			 * Which timestamp do we keep? It is rather
10011 			 * important in GP measurements to have the
10012 			 * accurate end of the send window.
10013 			 *
10014 			 * We keep the largest value, which is the newest
10015 			 * send. We do this in case a segment that is
10016 			 * joined together and not part of a GP estimate
10017 			 * later gets expanded into the GP estimate.
10018 			 *
10019 			 * We prohibit the merging of unlike kinds i.e.
10020 			 * all pieces that are in the GP estimate can be
10021 			 * merged and all pieces that are not in a GP estimate
10022 			 * can be merged, but not disimilar pieces. Combine
10023 			 * this with taking the highest here and we should
10024 			 * be ok unless of course the client reneges. Then
10025 			 * all bets are off.
10026 			 */
10027 			if(prev->r_tim_lastsent[(prev->r_rtr_cnt-1)] <
10028 			   nrsm->r_tim_lastsent[(nrsm->r_rtr_cnt-1)]) {
10029 				prev->r_tim_lastsent[(prev->r_rtr_cnt-1)] = nrsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
10030 			}
10031 			/*
10032 			 * And we must keep the newest ack arrival time.
10033 			 */
10034 
10035 			if(prev->r_ack_arrival <
10036 			   rack_to_usec_ts(&rack->r_ctl.act_rcv_time))
10037 				prev->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
10038 
10039 			rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
10040 			/*
10041 			 * Now that the rsm has had its start moved forward
10042 			 * lets go ahead and get its new place in the world.
10043 			 */
10044 			rack_setup_offset_for_rsm(rack, prev, rsm);
10045 			/*
10046 			 * Now nrsm is our new little piece
10047 			 * that is acked (which was merged
10048 			 * to prev). Update the rtt and changed
10049 			 * based on that. Also check for reordering.
10050 			 */
10051 			rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0);
10052 			if (rack->app_limited_needs_set)
10053 				rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END);
10054 			changed += (nrsm->r_end - nrsm->r_start);
10055 			/* You get a count for acking a whole segment or more */
10056 			if ((nrsm->r_end - nrsm->r_start) >= segsiz)
10057 				rack->r_ctl.ack_count += ((nrsm->r_end - nrsm->r_start) / segsiz);
10058 
10059 			rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start);
10060 			if (nrsm->r_flags & RACK_SACK_PASSED) {
10061 				rack->r_ctl.rc_reorder_ts = cts;
10062 				if (rack->r_ctl.rc_reorder_ts == 0)
10063 					rack->r_ctl.rc_reorder_ts = 1;
10064 			}
10065 			rack_log_map_chg(tp, rack, prev, &stack_map, rsm, MAP_SACK_M4, end, __LINE__);
10066 			rsm = prev;
10067 			counter_u64_add(rack_sack_used_prev_merge, 1);
10068 		} else {
10069 			/**
10070 			 * This is the case where our previous
10071 			 * block is not acked either, so we must
10072 			 * split the block in two.
10073 			 */
10074 			nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
10075 			if (nrsm == NULL) {
10076 				/* failed rrs what can we do but loose the sack info? */
10077 				goto out;
10078 			}
10079 			if ((rsm->r_flags & RACK_TLP) &&
10080 			    (rsm->r_rtr_cnt > 1)) {
10081 				/*
10082 				 * We are splitting a rxt TLP, check
10083 				 * if we need to save off the start/end
10084 				 */
10085 				if (rack->rc_last_tlp_acked_set &&
10086 				    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
10087 					/*
10088 					 * We already turned this on since this block is inside
10089 					 * the previous one was a partially sack now we
10090 					 * are getting another one (maybe all of it).
10091 					 */
10092 					rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
10093 					/*
10094 					 * Lets make sure we have all of it though.
10095 					 */
10096 					if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
10097 						rack->r_ctl.last_tlp_acked_start = rsm->r_start;
10098 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
10099 								     rack->r_ctl.last_tlp_acked_end);
10100 					}
10101 					if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
10102 						rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10103 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
10104 								     rack->r_ctl.last_tlp_acked_end);
10105 					}
10106 				} else {
10107 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
10108 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10109 					rack->rc_last_tlp_acked_set = 1;
10110 					rack->rc_last_tlp_past_cumack = 0;
10111 					rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
10112 				}
10113 			}
10114 			/**
10115 			 * In this case nrsm becomes
10116 			 * nrsm->r_start = end;
10117 			 * nrsm->r_end = rsm->r_end;
10118 			 * which is un-acked.
10119 			 * <and>
10120 			 * rsm->r_end = nrsm->r_start;
10121 			 * i.e. the remaining un-acked
10122 			 * piece is left on the left
10123 			 * hand side.
10124 			 *
10125 			 * So we start like this
10126 			 * rsm      |----------| (not acked)
10127 			 * sackblk  |---|
10128 			 * build it so we have
10129 			 * rsm      |---|         (acked)
10130 			 * nrsm         |------|  (not acked)
10131 			 */
10132 			counter_u64_add(rack_sack_splits, 1);
10133 			rack_clone_rsm(rack, nrsm, rsm, end);
10134 			moved++;
10135 			rsm->r_flags &= (~RACK_HAS_FIN);
10136 			rsm->r_just_ret = 0;
10137 #ifndef INVARIANTS
10138 			(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
10139 #else
10140 			if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
10141 				panic("Insert in rb tree of %p fails ret:% rack:%p rsm:%p",
10142 				      nrsm, insret, rack, rsm);
10143 			}
10144 #endif
10145 			if (rsm->r_in_tmap) {
10146 				TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
10147 				nrsm->r_in_tmap = 1;
10148 			}
10149 			nrsm->r_dupack = 0;
10150 			rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2);
10151 			rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0);
10152 			changed += (rsm->r_end - rsm->r_start);
10153 			/* You get a count for acking a whole segment or more */
10154 			if ((rsm->r_end - rsm->r_start) >= segsiz)
10155 				rack->r_ctl.ack_count += ((rsm->r_end - rsm->r_start) / segsiz);
10156 
10157 			rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
10158 			if (rsm->r_in_tmap) /* should be true */
10159 				rack_log_sack_passed(tp, rack, rsm);
10160 			/* Is Reordering occuring? */
10161 			if (rsm->r_flags & RACK_SACK_PASSED) {
10162 				rsm->r_flags &= ~RACK_SACK_PASSED;
10163 				rack->r_ctl.rc_reorder_ts = cts;
10164 				if (rack->r_ctl.rc_reorder_ts == 0)
10165 					rack->r_ctl.rc_reorder_ts = 1;
10166 			}
10167 			if (rack->app_limited_needs_set)
10168 				rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END);
10169 			rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
10170 			rsm->r_flags |= RACK_ACKED;
10171 			rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M5, end, __LINE__);
10172 			if (rsm->r_in_tmap) {
10173 				TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
10174 				rsm->r_in_tmap = 0;
10175 			}
10176 		}
10177 	} else if (start != end){
10178 		/*
10179 		 * The block was already acked.
10180 		 */
10181 		counter_u64_add(rack_sack_skipped_acked, 1);
10182 		moved++;
10183 	}
10184 out:
10185 	if (rsm &&
10186 	    ((rsm->r_flags & RACK_TLP) == 0) &&
10187 	    (rsm->r_flags & RACK_ACKED)) {
10188 		/*
10189 		 * Now can we merge where we worked
10190 		 * with either the previous or
10191 		 * next block?
10192 		 */
10193 		next = tqhash_next(rack->r_ctl.tqh, rsm);
10194 		while (next) {
10195 			if (next->r_flags & RACK_TLP)
10196 				break;
10197 			/* Only allow merges between ones in or out of GP window */
10198 			if ((next->r_flags & RACK_IN_GP_WIN) &&
10199 			    ((rsm->r_flags & RACK_IN_GP_WIN) == 0)) {
10200 				break;
10201 			}
10202 			if ((rsm->r_flags & RACK_IN_GP_WIN) &&
10203 			    ((next->r_flags & RACK_IN_GP_WIN) == 0)) {
10204 				break;
10205 			}
10206 			if (rsm->bindex != next->bindex)
10207 				break;
10208 			if (rsm->r_flags & RACK_STRADDLE)
10209 				break;
10210 			if (next->r_flags & RACK_STRADDLE)
10211 				break;
10212 			if (next->r_flags & RACK_ACKED) {
10213 				/* yep this and next can be merged */
10214 				rsm = rack_merge_rsm(rack, rsm, next);
10215 				noextra++;
10216 				next = tqhash_next(rack->r_ctl.tqh, rsm);
10217 			} else
10218 				break;
10219 		}
10220 		/* Now what about the previous? */
10221 		prev = tqhash_prev(rack->r_ctl.tqh, rsm);
10222 		while (prev) {
10223 			if (prev->r_flags & RACK_TLP)
10224 				break;
10225 			/* Only allow merges between ones in or out of GP window */
10226 			if ((prev->r_flags & RACK_IN_GP_WIN) &&
10227 			    ((rsm->r_flags & RACK_IN_GP_WIN) == 0)) {
10228 				break;
10229 			}
10230 			if ((rsm->r_flags & RACK_IN_GP_WIN) &&
10231 			    ((prev->r_flags & RACK_IN_GP_WIN) == 0)) {
10232 				break;
10233 			}
10234 			if (rsm->bindex != prev->bindex)
10235 				break;
10236 			if (rsm->r_flags & RACK_STRADDLE)
10237 				break;
10238 			if (prev->r_flags & RACK_STRADDLE)
10239 				break;
10240 			if (prev->r_flags & RACK_ACKED) {
10241 				/* yep the previous and this can be merged */
10242 				rsm = rack_merge_rsm(rack, prev, rsm);
10243 				noextra++;
10244 				prev = tqhash_prev(rack->r_ctl.tqh, rsm);
10245 			} else
10246 				break;
10247 		}
10248 	}
10249 	if (used_ref == 0) {
10250 		counter_u64_add(rack_sack_proc_all, 1);
10251 	} else {
10252 		counter_u64_add(rack_sack_proc_short, 1);
10253 	}
10254 	/* Save off the next one for quick reference. */
10255 	nrsm = tqhash_find(rack->r_ctl.tqh, end);
10256 	*prsm = rack->r_ctl.rc_sacklast = nrsm;
10257 	/* Pass back the moved. */
10258 	*moved_two = moved;
10259 	*no_extra = noextra;
10260 	return (changed);
10261 }
10262 
10263 static void inline
10264 rack_peer_reneges(struct tcp_rack *rack, struct rack_sendmap *rsm, tcp_seq th_ack)
10265 {
10266 	struct rack_sendmap *tmap;
10267 
10268 	tmap = NULL;
10269 	while (rsm && (rsm->r_flags & RACK_ACKED)) {
10270 		/* Its no longer sacked, mark it so */
10271 		rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
10272 #ifdef INVARIANTS
10273 		if (rsm->r_in_tmap) {
10274 			panic("rack:%p rsm:%p flags:0x%x in tmap?",
10275 			      rack, rsm, rsm->r_flags);
10276 		}
10277 #endif
10278 		rsm->r_flags &= ~(RACK_ACKED|RACK_SACK_PASSED|RACK_WAS_SACKPASS);
10279 		/* Rebuild it into our tmap */
10280 		if (tmap == NULL) {
10281 			TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
10282 			tmap = rsm;
10283 		} else {
10284 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, tmap, rsm, r_tnext);
10285 			tmap = rsm;
10286 		}
10287 		tmap->r_in_tmap = 1;
10288 		rsm = tqhash_next(rack->r_ctl.tqh, rsm);
10289 	}
10290 	/*
10291 	 * Now lets possibly clear the sack filter so we start
10292 	 * recognizing sacks that cover this area.
10293 	 */
10294 	sack_filter_clear(&rack->r_ctl.rack_sf, th_ack);
10295 
10296 }
10297 
10298 static void
10299 rack_do_decay(struct tcp_rack *rack)
10300 {
10301 	struct timeval res;
10302 
10303 #define	timersub(tvp, uvp, vvp)						\
10304 	do {								\
10305 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
10306 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
10307 		if ((vvp)->tv_usec < 0) {				\
10308 			(vvp)->tv_sec--;				\
10309 			(vvp)->tv_usec += 1000000;			\
10310 		}							\
10311 	} while (0)
10312 
10313 	timersub(&rack->r_ctl.act_rcv_time, &rack->r_ctl.rc_last_time_decay, &res);
10314 #undef timersub
10315 
10316 	rack->r_ctl.input_pkt++;
10317 	if ((rack->rc_in_persist) ||
10318 	    (res.tv_sec >= 1) ||
10319 	    (rack->rc_tp->snd_max == rack->rc_tp->snd_una)) {
10320 		/*
10321 		 * Check for decay of non-SAD,
10322 		 * we want all SAD detection metrics to
10323 		 * decay 1/4 per second (or more) passed.
10324 		 * Current default is 800 so it decays
10325 		 * 80% every second.
10326 		 */
10327 #ifdef TCP_SAD_DETECTION
10328 		uint32_t pkt_delta;
10329 
10330 		pkt_delta = rack->r_ctl.input_pkt - rack->r_ctl.saved_input_pkt;
10331 #endif
10332 		/* Update our saved tracking values */
10333 		rack->r_ctl.saved_input_pkt = rack->r_ctl.input_pkt;
10334 		rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time;
10335 		/* Now do we escape without decay? */
10336 #ifdef TCP_SAD_DETECTION
10337 		if (rack->rc_in_persist ||
10338 		    (rack->rc_tp->snd_max == rack->rc_tp->snd_una) ||
10339 		    (pkt_delta < tcp_sad_low_pps)){
10340 			/*
10341 			 * We don't decay idle connections
10342 			 * or ones that have a low input pps.
10343 			 */
10344 			return;
10345 		}
10346 		/* Decay the counters */
10347 		rack->r_ctl.ack_count = ctf_decay_count(rack->r_ctl.ack_count,
10348 							tcp_sad_decay_val);
10349 		rack->r_ctl.sack_count = ctf_decay_count(rack->r_ctl.sack_count,
10350 							 tcp_sad_decay_val);
10351 		rack->r_ctl.sack_moved_extra = ctf_decay_count(rack->r_ctl.sack_moved_extra,
10352 							       tcp_sad_decay_val);
10353 		rack->r_ctl.sack_noextra_move = ctf_decay_count(rack->r_ctl.sack_noextra_move,
10354 								tcp_sad_decay_val);
10355 #endif
10356 	}
10357 }
10358 
10359 static void inline
10360 rack_rsm_sender_update(struct tcp_rack *rack, struct tcpcb *tp, struct rack_sendmap *rsm, uint8_t from)
10361 {
10362 	/*
10363 	 * We look at advancing the end send time for our GP
10364 	 * measurement tracking only as the cumulative acknowledgment
10365 	 * moves forward. You might wonder about this, why not
10366 	 * at every transmission or retransmission within the
10367 	 * GP window update the rc_gp_cumack_ts? Well its rather
10368 	 * nuanced but basically the GP window *may* expand (as
10369 	 * it does below) or worse and harder to track it may shrink.
10370 	 *
10371 	 * This last makes it impossible to track at the time of
10372 	 * the send, since you may set forward your rc_gp_cumack_ts
10373 	 * when you send, because that send *is* in your currently
10374 	 * "guessed" window, but then it shrinks. Now which was
10375 	 * the send time of the last bytes in the window, by the
10376 	 * time you ask that question that part of the sendmap
10377 	 * is freed. So you don't know and you will have too
10378 	 * long of send window. Instead by updating the time
10379 	 * marker only when the cumack advances this assures us
10380 	 * that we will have only the sends in the window of our
10381 	 * GP measurement.
10382 	 *
10383 	 * Another complication from this is the
10384 	 * merging of sendmap entries. During SACK processing this
10385 	 * can happen to conserve the sendmap size. That breaks
10386 	 * everything down in tracking the send window of the GP
10387 	 * estimate. So to prevent that and keep it working with
10388 	 * a tiny bit more limited merging, we only allow like
10389 	 * types to be merged. I.e. if two sends are in the GP window
10390 	 * then its ok to merge them together. If two sends are not
10391 	 * in the GP window its ok to merge them together too. Though
10392 	 * one send in and one send out cannot be merged. We combine
10393 	 * this with never allowing the shrinking of the GP window when
10394 	 * we are in recovery so that we can properly calculate the
10395 	 * sending times.
10396 	 *
10397 	 * This all of course seems complicated, because it is.. :)
10398 	 *
10399 	 * The cum-ack is being advanced upon the sendmap.
10400 	 * If we are not doing a GP estimate don't
10401 	 * proceed.
10402 	 */
10403 	uint64_t ts;
10404 
10405 	if ((tp->t_flags & TF_GPUTINPROG) == 0)
10406 		return;
10407 	/*
10408 	 * If this sendmap entry is going
10409 	 * beyond the measurement window we had picked,
10410 	 * expand the measurement window by that much.
10411 	 */
10412 	if (SEQ_GT(rsm->r_end, tp->gput_ack)) {
10413 		tp->gput_ack = rsm->r_end;
10414 	}
10415 	/*
10416 	 * If we have not setup a ack, then we
10417 	 * have no idea if the newly acked pieces
10418 	 * will be "in our seq measurement range". If
10419 	 * it is when we clear the app_limited_needs_set
10420 	 * flag the timestamp will be updated.
10421 	 */
10422 	if (rack->app_limited_needs_set)
10423 		return;
10424 	/*
10425 	 * Finally, we grab out the latest timestamp
10426 	 * that this packet was sent and then see
10427 	 * if:
10428 	 *  a) The packet touches are newly defined GP range.
10429 	 *  b) The time is greater than (newer) than the
10430 	 *     one we currently have. If so we update
10431 	 *     our sending end time window.
10432 	 *
10433 	 * Note we *do not* do this at send time. The reason
10434 	 * is that if you do you *may* pick up a newer timestamp
10435 	 * for a range you are not going to measure. We project
10436 	 * out how far and then sometimes modify that to be
10437 	 * smaller. If that occurs then you will have a send
10438 	 * that does not belong to the range included.
10439 	 */
10440 	if ((ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]) <=
10441 	    rack->r_ctl.rc_gp_cumack_ts)
10442 		return;
10443 	if (rack_in_gp_window(tp, rsm)) {
10444 		rack->r_ctl.rc_gp_cumack_ts = ts;
10445 		rack_log_gpset(rack, tp->gput_ack, (uint32_t)ts, rsm->r_end,
10446 			       __LINE__, from, rsm);
10447 	}
10448 }
10449 
10450 static void
10451 rack_process_to_cumack(struct tcpcb *tp, struct tcp_rack *rack, register uint32_t th_ack, uint32_t cts, struct tcpopt *to, uint64_t acktime)
10452 {
10453 	struct rack_sendmap *rsm;
10454 	/*
10455 	 * The ACK point is advancing to th_ack, we must drop off
10456 	 * the packets in the rack log and calculate any eligble
10457 	 * RTT's.
10458 	 */
10459 
10460 	rack->r_wanted_output = 1;
10461 	if (SEQ_GT(th_ack, tp->snd_una))
10462 		rack->r_ctl.last_cumack_advance = acktime;
10463 
10464 	/* Tend any TLP that has been marked for 1/2 the seq space (its old)  */
10465 	if ((rack->rc_last_tlp_acked_set == 1)&&
10466 	    (rack->rc_last_tlp_past_cumack == 1) &&
10467 	    (SEQ_GT(rack->r_ctl.last_tlp_acked_start, th_ack))) {
10468 		/*
10469 		 * We have reached the point where our last rack
10470 		 * tlp retransmit sequence is ahead of the cum-ack.
10471 		 * This can only happen when the cum-ack moves all
10472 		 * the way around (its been a full 2^^31+1 bytes
10473 		 * or more since we sent a retransmitted TLP). Lets
10474 		 * turn off the valid flag since its not really valid.
10475 		 *
10476 		 * Note since sack's also turn on this event we have
10477 		 * a complication, we have to wait to age it out until
10478 		 * the cum-ack is by the TLP before checking which is
10479 		 * what the next else clause does.
10480 		 */
10481 		rack_log_dsack_event(rack, 9, __LINE__,
10482 				     rack->r_ctl.last_tlp_acked_start,
10483 				     rack->r_ctl.last_tlp_acked_end);
10484 		rack->rc_last_tlp_acked_set = 0;
10485 		rack->rc_last_tlp_past_cumack = 0;
10486 	} else if ((rack->rc_last_tlp_acked_set == 1) &&
10487 		   (rack->rc_last_tlp_past_cumack == 0) &&
10488 		   (SEQ_GEQ(th_ack, rack->r_ctl.last_tlp_acked_end))) {
10489 		/*
10490 		 * It is safe to start aging TLP's out.
10491 		 */
10492 		rack->rc_last_tlp_past_cumack = 1;
10493 	}
10494 	/* We do the same for the tlp send seq as well */
10495 	if ((rack->rc_last_sent_tlp_seq_valid == 1) &&
10496 	    (rack->rc_last_sent_tlp_past_cumack == 1) &&
10497 	    (SEQ_GT(rack->r_ctl.last_sent_tlp_seq,  th_ack))) {
10498 		rack_log_dsack_event(rack, 9, __LINE__,
10499 				     rack->r_ctl.last_sent_tlp_seq,
10500 				     (rack->r_ctl.last_sent_tlp_seq +
10501 				      rack->r_ctl.last_sent_tlp_len));
10502 		rack->rc_last_sent_tlp_seq_valid = 0;
10503 		rack->rc_last_sent_tlp_past_cumack = 0;
10504 	} else if ((rack->rc_last_sent_tlp_seq_valid == 1) &&
10505 		   (rack->rc_last_sent_tlp_past_cumack == 0) &&
10506 		   (SEQ_GEQ(th_ack, rack->r_ctl.last_sent_tlp_seq))) {
10507 		/*
10508 		 * It is safe to start aging TLP's send.
10509 		 */
10510 		rack->rc_last_sent_tlp_past_cumack = 1;
10511 	}
10512 more:
10513 	rsm = tqhash_min(rack->r_ctl.tqh);
10514 	if (rsm == NULL) {
10515 		if ((th_ack - 1) == tp->iss) {
10516 			/*
10517 			 * For the SYN incoming case we will not
10518 			 * have called tcp_output for the sending of
10519 			 * the SYN, so there will be no map. All
10520 			 * other cases should probably be a panic.
10521 			 */
10522 			return;
10523 		}
10524 		if (tp->t_flags & TF_SENTFIN) {
10525 			/* if we sent a FIN we often will not have map */
10526 			return;
10527 		}
10528 #ifdef INVARIANTS
10529 		panic("No rack map tp:%p for state:%d ack:%u rack:%p snd_una:%u snd_max:%u snd_nxt:%u\n",
10530 		      tp,
10531 		      tp->t_state, th_ack, rack,
10532 		      tp->snd_una, tp->snd_max, tp->snd_nxt);
10533 #endif
10534 		return;
10535 	}
10536 	if (SEQ_LT(th_ack, rsm->r_start)) {
10537 		/* Huh map is missing this */
10538 #ifdef INVARIANTS
10539 		printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d\n",
10540 		       rsm->r_start,
10541 		       th_ack, tp->t_state, rack->r_state);
10542 #endif
10543 		return;
10544 	}
10545 	rack_update_rtt(tp, rack, rsm, to, cts, CUM_ACKED, th_ack);
10546 
10547 	/* Now was it a retransmitted TLP? */
10548 	if ((rsm->r_flags & RACK_TLP) &&
10549 	    (rsm->r_rtr_cnt > 1)) {
10550 		/*
10551 		 * Yes, this rsm was a TLP and retransmitted, remember that
10552 		 * since if a DSACK comes back on this we don't want
10553 		 * to think of it as a reordered segment. This may
10554 		 * get updated again with possibly even other TLPs
10555 		 * in flight, but thats ok. Only when we don't send
10556 		 * a retransmitted TLP for 1/2 the sequences space
10557 		 * will it get turned off (above).
10558 		 */
10559 		if (rack->rc_last_tlp_acked_set &&
10560 		    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
10561 			/*
10562 			 * We already turned this on since the end matches,
10563 			 * the previous one was a partially ack now we
10564 			 * are getting another one (maybe all of it).
10565 			 */
10566 			rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
10567 			/*
10568 			 * Lets make sure we have all of it though.
10569 			 */
10570 			if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
10571 				rack->r_ctl.last_tlp_acked_start = rsm->r_start;
10572 				rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
10573 						     rack->r_ctl.last_tlp_acked_end);
10574 			}
10575 			if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
10576 				rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10577 				rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
10578 						     rack->r_ctl.last_tlp_acked_end);
10579 			}
10580 		} else {
10581 			rack->rc_last_tlp_past_cumack = 1;
10582 			rack->r_ctl.last_tlp_acked_start = rsm->r_start;
10583 			rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10584 			rack->rc_last_tlp_acked_set = 1;
10585 			rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
10586 		}
10587 	}
10588 	/* Now do we consume the whole thing? */
10589 	rack->r_ctl.last_tmit_time_acked = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
10590 	if (SEQ_GEQ(th_ack, rsm->r_end)) {
10591 		/* Its all consumed. */
10592 		uint32_t left;
10593 		uint8_t newly_acked;
10594 
10595 		rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_FREE, rsm->r_end, __LINE__);
10596 		rack->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
10597 		rsm->r_rtr_bytes = 0;
10598 		/*
10599 		 * Record the time of highest cumack sent if its in our measurement
10600 		 * window and possibly bump out the end.
10601 		 */
10602 		rack_rsm_sender_update(rack, tp, rsm, 4);
10603 		tqhash_remove(rack->r_ctl.tqh, rsm, REMOVE_TYPE_CUMACK);
10604 		if (rsm->r_in_tmap) {
10605 			TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
10606 			rsm->r_in_tmap = 0;
10607 		}
10608 		newly_acked = 1;
10609 		if (rsm->r_flags & RACK_ACKED) {
10610 			/*
10611 			 * It was acked on the scoreboard -- remove
10612 			 * it from total
10613 			 */
10614 			rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
10615 			newly_acked = 0;
10616 		} else if (rsm->r_flags & RACK_SACK_PASSED) {
10617 			/*
10618 			 * There are segments ACKED on the
10619 			 * scoreboard further up. We are seeing
10620 			 * reordering.
10621 			 */
10622 			rsm->r_flags &= ~RACK_SACK_PASSED;
10623 			rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
10624 			rsm->r_flags |= RACK_ACKED;
10625 			rack->r_ctl.rc_reorder_ts = cts;
10626 			if (rack->r_ctl.rc_reorder_ts == 0)
10627 				rack->r_ctl.rc_reorder_ts = 1;
10628 			if (rack->r_ent_rec_ns) {
10629 				/*
10630 				 * We have sent no more, and we saw an sack
10631 				 * then ack arrive.
10632 				 */
10633 				rack->r_might_revert = 1;
10634 			}
10635 		}
10636 		if ((rsm->r_flags & RACK_TO_REXT) &&
10637 		    (tp->t_flags & TF_RCVD_TSTMP) &&
10638 		    (to->to_flags & TOF_TS) &&
10639 		    (to->to_tsecr != 0) &&
10640 		    (tp->t_flags & TF_PREVVALID)) {
10641 			/*
10642 			 * We can use the timestamp to see
10643 			 * if this retransmission was from the
10644 			 * first transmit. If so we made a mistake.
10645 			 */
10646 			tp->t_flags &= ~TF_PREVVALID;
10647 			if (to->to_tsecr == rack_ts_to_msec(rsm->r_tim_lastsent[0])) {
10648 				/* The first transmit is what this ack is for */
10649 				rack_cong_signal(tp, CC_RTO_ERR, th_ack, __LINE__);
10650 			}
10651 		}
10652 		left = th_ack - rsm->r_end;
10653 		if (rack->app_limited_needs_set && newly_acked)
10654 			rack_need_set_test(tp, rack, rsm, th_ack, __LINE__, RACK_USE_END_OR_THACK);
10655 		/* Free back to zone */
10656 		rack_free(rack, rsm);
10657 		if (left) {
10658 			goto more;
10659 		}
10660 		/* Check for reneging */
10661 		rsm = tqhash_min(rack->r_ctl.tqh);
10662 		if (rsm && (rsm->r_flags & RACK_ACKED) && (th_ack == rsm->r_start)) {
10663 			/*
10664 			 * The peer has moved snd_una up to
10665 			 * the edge of this send, i.e. one
10666 			 * that it had previously acked. The only
10667 			 * way that can be true if the peer threw
10668 			 * away data (space issues) that it had
10669 			 * previously sacked (else it would have
10670 			 * given us snd_una up to (rsm->r_end).
10671 			 * We need to undo the acked markings here.
10672 			 *
10673 			 * Note we have to look to make sure th_ack is
10674 			 * our rsm->r_start in case we get an old ack
10675 			 * where th_ack is behind snd_una.
10676 			 */
10677 			rack_peer_reneges(rack, rsm, th_ack);
10678 		}
10679 		return;
10680 	}
10681 	if (rsm->r_flags & RACK_ACKED) {
10682 		/*
10683 		 * It was acked on the scoreboard -- remove it from
10684 		 * total for the part being cum-acked.
10685 		 */
10686 		rack->r_ctl.rc_sacked -= (th_ack - rsm->r_start);
10687 	}
10688 	/*
10689 	 * Clear the dup ack count for
10690 	 * the piece that remains.
10691 	 */
10692 	rsm->r_dupack = 0;
10693 	rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
10694 	if (rsm->r_rtr_bytes) {
10695 		/*
10696 		 * It was retransmitted adjust the
10697 		 * sack holes for what was acked.
10698 		 */
10699 		int ack_am;
10700 
10701 		ack_am = (th_ack - rsm->r_start);
10702 		if (ack_am >= rsm->r_rtr_bytes) {
10703 			rack->r_ctl.rc_holes_rxt -= ack_am;
10704 			rsm->r_rtr_bytes -= ack_am;
10705 		}
10706 	}
10707 	/*
10708 	 * Update where the piece starts and record
10709 	 * the time of send of highest cumack sent if
10710 	 * its in our GP range.
10711 	 */
10712 	rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_TRIM_HEAD, th_ack, __LINE__);
10713 	/* Now we need to move our offset forward too */
10714 	if (rsm->m &&
10715 	    ((rsm->orig_m_len != rsm->m->m_len) ||
10716 	     (M_TRAILINGROOM(rsm->m) != rsm->orig_t_space))) {
10717 		/* Fix up the orig_m_len and possibly the mbuf offset */
10718 		rack_adjust_orig_mlen(rsm);
10719 	}
10720 	rsm->soff += (th_ack - rsm->r_start);
10721 	rack_rsm_sender_update(rack, tp, rsm, 5);
10722 	/* The trim will move th_ack into r_start for us */
10723 	tqhash_trim(rack->r_ctl.tqh, th_ack);
10724 	/* Now do we need to move the mbuf fwd too? */
10725 	{
10726 		struct mbuf *m;
10727 		uint32_t soff;
10728 
10729 		m = rsm->m;
10730 		soff = rsm->soff;
10731 		if (m) {
10732 			while (soff >= m->m_len) {
10733 				soff -= m->m_len;
10734 				KASSERT((m->m_next != NULL),
10735 					(" rsm:%p  off:%u soff:%u m:%p",
10736 					 rsm, rsm->soff, soff, m));
10737 				m = m->m_next;
10738 				if (m == NULL) {
10739 					/*
10740 					 * This is a fall-back that prevents a panic. In reality
10741 					 * we should be able to walk the mbuf's and find our place.
10742 					 * At this point snd_una has not been updated with the sbcut() yet
10743 					 * but tqhash_trim did update rsm->r_start so the offset calcuation
10744 					 * should work fine. This is undesirable since we will take cache
10745 					 * hits to access the socket buffer. And even more puzzling is that
10746 					 * it happens occasionally. It should not :(
10747 					 */
10748 					m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd,
10749 						      (rsm->r_start - tp->snd_una),
10750 						      &soff);
10751 					break;
10752 				}
10753 			}
10754 			/*
10755 			 * Now save in our updated values.
10756 			 */
10757 			rsm->m = m;
10758 			rsm->soff = soff;
10759 			rsm->orig_m_len = rsm->m->m_len;
10760 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
10761 		}
10762 	}
10763 	if (rack->app_limited_needs_set &&
10764 	    SEQ_GEQ(th_ack, tp->gput_seq))
10765 		rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_BEG);
10766 }
10767 
10768 static void
10769 rack_handle_might_revert(struct tcpcb *tp, struct tcp_rack *rack)
10770 {
10771 	struct rack_sendmap *rsm;
10772 	int sack_pass_fnd = 0;
10773 
10774 	if (rack->r_might_revert) {
10775 		/*
10776 		 * Ok we have reordering, have not sent anything, we
10777 		 * might want to revert the congestion state if nothing
10778 		 * further has SACK_PASSED on it. Lets check.
10779 		 *
10780 		 * We also get here when we have DSACKs come in for
10781 		 * all the data that we FR'd. Note that a rxt or tlp
10782 		 * timer clears this from happening.
10783 		 */
10784 
10785 		TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
10786 			if (rsm->r_flags & RACK_SACK_PASSED) {
10787 				sack_pass_fnd = 1;
10788 				break;
10789 			}
10790 		}
10791 		if (sack_pass_fnd == 0) {
10792 			/*
10793 			 * We went into recovery
10794 			 * incorrectly due to reordering!
10795 			 */
10796 			int orig_cwnd;
10797 
10798 			rack->r_ent_rec_ns = 0;
10799 			orig_cwnd = tp->snd_cwnd;
10800 			tp->snd_ssthresh = rack->r_ctl.rc_ssthresh_at_erec;
10801 			tp->snd_recover = tp->snd_una;
10802 			rack_log_to_prr(rack, 14, orig_cwnd, __LINE__);
10803 			EXIT_RECOVERY(tp->t_flags);
10804 		}
10805 		rack->r_might_revert = 0;
10806 	}
10807 }
10808 
10809 #ifdef TCP_SAD_DETECTION
10810 
10811 static void
10812 rack_merge_out_sacks(struct tcp_rack *rack)
10813 {
10814 	struct rack_sendmap *cur, *next, *rsm, *trsm = NULL;
10815 
10816 	cur = tqhash_min(rack->r_ctl.tqh);
10817 	while(cur) {
10818 		next = tqhash_next(rack->r_ctl.tqh, cur);
10819 		/*
10820 		 * The idea is to go through all and merge back
10821 		 * together the pieces sent together,
10822 		 */
10823 		if ((next != NULL) &&
10824 		    (cur->r_tim_lastsent[0] == next->r_tim_lastsent[0])) {
10825 			rack_merge_rsm(rack, cur, next);
10826 		} else {
10827 			cur = next;
10828 		}
10829 	}
10830 	/*
10831 	 * now treat it like a rxt event, everything is outstanding
10832 	 * and sent nothing acvked and dupacks are all zero. If this
10833 	 * is not an attacker it will have to dupack its way through
10834 	 * it all.
10835 	 */
10836 	TAILQ_INIT(&rack->r_ctl.rc_tmap);
10837 	TQHASH_FOREACH(rsm, rack->r_ctl.tqh)  {
10838 		rsm->r_dupack = 0;
10839 		/* We must re-add it back to the tlist */
10840 		if (trsm == NULL) {
10841 			TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
10842 		} else {
10843 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, trsm, rsm, r_tnext);
10844 		}
10845 		rsm->r_in_tmap = 1;
10846 		trsm = rsm;
10847 		rsm->r_flags &= ~(RACK_ACKED | RACK_SACK_PASSED | RACK_WAS_SACKPASS | RACK_RWND_COLLAPSED);
10848 	}
10849 	sack_filter_clear(&rack->r_ctl.rack_sf, rack->rc_tp->snd_una);
10850 }
10851 
10852 static void
10853 rack_do_detection(struct tcpcb *tp, struct tcp_rack *rack,  uint32_t bytes_this_ack, uint32_t segsiz)
10854 {
10855 	int do_detection = 0;
10856 
10857 	if (rack->sack_attack_disable || rack->rc_suspicious) {
10858 		/*
10859 		 * If we have been disabled we must detect
10860 		 * to possibly reverse it. Or if the guy has
10861 		 * sent in suspicious sacks we want to do detection too.
10862 		 */
10863 		do_detection = 1;
10864 
10865 	} else if  ((rack->do_detection || tcp_force_detection) &&
10866 		    (tcp_sack_to_ack_thresh > 0) &&
10867 		    (tcp_sack_to_move_thresh > 0) &&
10868 		    (rack->r_ctl.rc_num_maps_alloced > tcp_map_minimum)) {
10869 		/*
10870 		 * We only detect here if:
10871 		 * 1) System wide forcing is on <or> do_detection is on
10872 		 *   <and>
10873 		 * 2) We have thresholds for move and ack (set one to 0 and we are off)
10874 		 *   <and>
10875 		 * 3) We have maps allocated larger than our min (500).
10876 		 */
10877 		do_detection = 1;
10878 	}
10879 	if (do_detection > 0) {
10880 		/*
10881 		 * We have thresholds set to find
10882 		 * possible attackers and disable sack.
10883 		 * Check them.
10884 		 */
10885 		uint64_t ackratio, moveratio, movetotal;
10886 
10887 		/* Log detecting */
10888 		rack_log_sad(rack, 1);
10889 		/* Do we establish a ack ratio */
10890 		if ((rack->r_ctl.sack_count > tcp_map_minimum)  ||
10891 		    (rack->rc_suspicious == 1) ||
10892 		    (rack->sack_attack_disable > 0)) {
10893 			ackratio = (uint64_t)(rack->r_ctl.sack_count);
10894 			ackratio *= (uint64_t)(1000);
10895 			if (rack->r_ctl.ack_count)
10896 				ackratio /= (uint64_t)(rack->r_ctl.ack_count);
10897 			else {
10898 				/* We can hit this due to ack totals degregation (via small sacks) */
10899 				ackratio = 1000;
10900 			}
10901 		} else {
10902 			/*
10903 			 * No ack ratio needed if we have not
10904 			 * seen more sacks then the number of map entries.
10905 			 * The exception to that is if we have disabled sack then
10906 			 * we need to find a ratio.
10907 			 */
10908 			ackratio = 0;
10909 		}
10910 
10911 		if ((rack->sack_attack_disable == 0) &&
10912 		    (ackratio > rack_highest_sack_thresh_seen))
10913 			rack_highest_sack_thresh_seen = (uint32_t)ackratio;
10914 		/* Do we establish a move ratio? */
10915 		if ((rack->r_ctl.sack_moved_extra > tcp_map_minimum) ||
10916 		    (rack->rc_suspicious == 1) ||
10917 		    (rack->sack_attack_disable > 0)) {
10918 			/*
10919 			 * We need to have more sack moves than maps
10920 			 * allocated to have a move ratio considered.
10921 			 */
10922 			movetotal = rack->r_ctl.sack_moved_extra;
10923 			movetotal += rack->r_ctl.sack_noextra_move;
10924 			moveratio = rack->r_ctl.sack_moved_extra;
10925 			moveratio *= (uint64_t)1000;
10926 			if (movetotal)
10927 				moveratio /= movetotal;
10928 			else {
10929 				/* No moves, thats pretty good */
10930 				moveratio = 0;
10931 			}
10932 		} else {
10933 			/*
10934 			 * Not enough moves have occured to consider
10935 			 * if we are out of whack in that ratio.
10936 			 * The exception to that is if we have disabled sack then
10937 			 * we need to find a ratio.
10938 			 */
10939 			moveratio = 0;
10940 		}
10941 		if ((rack->sack_attack_disable == 0) &&
10942 		    (moveratio > rack_highest_move_thresh_seen))
10943 			rack_highest_move_thresh_seen = (uint32_t)moveratio;
10944 		/* Now the tests */
10945 		if (rack->sack_attack_disable == 0) {
10946 			/* Not disabled, do we need to disable? */
10947 			if ((ackratio > tcp_sack_to_ack_thresh) &&
10948 			    (moveratio > tcp_sack_to_move_thresh)) {
10949 				/* Disable sack processing */
10950 				tcp_trace_point(rack->rc_tp, TCP_TP_SAD_TRIGGERED);
10951 				rack->sack_attack_disable = 1;
10952 				/* set it so we have the built in delay */
10953 				rack->r_ctl.ack_during_sd = 1;
10954 				if (rack_merge_out_sacks_on_attack)
10955 					rack_merge_out_sacks(rack);
10956 				counter_u64_add(rack_sack_attacks_detected, 1);
10957 				tcp_trace_point(rack->rc_tp, TCP_TP_SAD_TRIGGERED);
10958 				/* Clamp the cwnd at flight size */
10959 				rack->r_ctl.rc_saved_cwnd = rack->rc_tp->snd_cwnd;
10960 				rack->rc_tp->snd_cwnd = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
10961 				rack_log_sad(rack, 2);
10962 			}
10963 		} else {
10964 			/* We are sack-disabled check for false positives */
10965 			if ((ackratio <= tcp_restoral_thresh) ||
10966 			    ((rack_merge_out_sacks_on_attack == 0) &&
10967 			     (rack->rc_suspicious == 0) &&
10968 			     (rack->r_ctl.rc_num_maps_alloced <= (tcp_map_minimum/2)))) {
10969 				rack->sack_attack_disable = 0;
10970 				rack_log_sad(rack, 3);
10971 				/* Restart counting */
10972 				rack->r_ctl.sack_count = 0;
10973 				rack->r_ctl.sack_moved_extra = 0;
10974 				rack->r_ctl.sack_noextra_move = 1;
10975 				rack->rc_suspicious = 0;
10976 				rack->r_ctl.ack_count = max(1,
10977 							    (bytes_this_ack / segsiz));
10978 
10979 				counter_u64_add(rack_sack_attacks_reversed, 1);
10980 				/* Restore the cwnd */
10981 				if (rack->r_ctl.rc_saved_cwnd > rack->rc_tp->snd_cwnd)
10982 					rack->rc_tp->snd_cwnd = rack->r_ctl.rc_saved_cwnd;
10983 			}
10984 		}
10985 	}
10986 }
10987 #endif
10988 
10989 static int
10990 rack_note_dsack(struct tcp_rack *rack, tcp_seq start, tcp_seq end)
10991 {
10992 
10993 	uint32_t am, l_end;
10994 	int was_tlp = 0;
10995 
10996 	if (SEQ_GT(end, start))
10997 		am = end - start;
10998 	else
10999 		am = 0;
11000 	if ((rack->rc_last_tlp_acked_set ) &&
11001 	    (SEQ_GEQ(start, rack->r_ctl.last_tlp_acked_start)) &&
11002 	    (SEQ_LEQ(end, rack->r_ctl.last_tlp_acked_end))) {
11003 		/*
11004 		 * The DSACK is because of a TLP which we don't
11005 		 * do anything with the reordering window over since
11006 		 * it was not reordering that caused the DSACK but
11007 		 * our previous retransmit TLP.
11008 		 */
11009 		rack_log_dsack_event(rack, 7, __LINE__, start, end);
11010 		was_tlp = 1;
11011 		goto skip_dsack_round;
11012 	}
11013 	if (rack->rc_last_sent_tlp_seq_valid) {
11014 		l_end = rack->r_ctl.last_sent_tlp_seq + rack->r_ctl.last_sent_tlp_len;
11015 		if (SEQ_GEQ(start, rack->r_ctl.last_sent_tlp_seq) &&
11016 		    (SEQ_LEQ(end, l_end))) {
11017 			/*
11018 			 * This dsack is from the last sent TLP, ignore it
11019 			 * for reordering purposes.
11020 			 */
11021 			rack_log_dsack_event(rack, 7, __LINE__, start, end);
11022 			was_tlp = 1;
11023 			goto skip_dsack_round;
11024 		}
11025 	}
11026 	if (rack->rc_dsack_round_seen == 0) {
11027 		rack->rc_dsack_round_seen = 1;
11028 		rack->r_ctl.dsack_round_end = rack->rc_tp->snd_max;
11029 		rack->r_ctl.num_dsack++;
11030 		rack->r_ctl.dsack_persist = 16;	/* 16 is from the standard */
11031 		rack_log_dsack_event(rack, 2, __LINE__, 0, 0);
11032 	}
11033 skip_dsack_round:
11034 	/*
11035 	 * We keep track of how many DSACK blocks we get
11036 	 * after a recovery incident.
11037 	 */
11038 	rack->r_ctl.dsack_byte_cnt += am;
11039 	if (!IN_FASTRECOVERY(rack->rc_tp->t_flags) &&
11040 	    rack->r_ctl.retran_during_recovery &&
11041 	    (rack->r_ctl.dsack_byte_cnt >= rack->r_ctl.retran_during_recovery)) {
11042 		/*
11043 		 * False recovery most likely culprit is reordering. If
11044 		 * nothing else is missing we need to revert.
11045 		 */
11046 		rack->r_might_revert = 1;
11047 		rack_handle_might_revert(rack->rc_tp, rack);
11048 		rack->r_might_revert = 0;
11049 		rack->r_ctl.retran_during_recovery = 0;
11050 		rack->r_ctl.dsack_byte_cnt = 0;
11051 	}
11052 	return (was_tlp);
11053 }
11054 
11055 static uint32_t
11056 do_rack_compute_pipe(struct tcpcb *tp, struct tcp_rack *rack, uint32_t snd_una)
11057 {
11058 	return (((tp->snd_max - snd_una) - rack->r_ctl.rc_sacked) + rack->r_ctl.rc_holes_rxt);
11059 }
11060 
11061 static int32_t
11062 rack_compute_pipe(struct tcpcb *tp)
11063 {
11064 	return ((int32_t)do_rack_compute_pipe(tp,
11065 					      (struct tcp_rack *)tp->t_fb_ptr,
11066 					      tp->snd_una));
11067 }
11068 
11069 static void
11070 rack_update_prr(struct tcpcb *tp, struct tcp_rack *rack, uint32_t changed, tcp_seq th_ack)
11071 {
11072 	/* Deal with changed and PRR here (in recovery only) */
11073 	uint32_t pipe, snd_una;
11074 
11075 	rack->r_ctl.rc_prr_delivered += changed;
11076 
11077 	if (sbavail(&rack->rc_inp->inp_socket->so_snd) <= (tp->snd_max - tp->snd_una)) {
11078 		/*
11079 		 * It is all outstanding, we are application limited
11080 		 * and thus we don't need more room to send anything.
11081 		 * Note we use tp->snd_una here and not th_ack because
11082 		 * the data as yet not been cut from the sb.
11083 		 */
11084 		rack->r_ctl.rc_prr_sndcnt = 0;
11085 		return;
11086 	}
11087 	/* Compute prr_sndcnt */
11088 	if (SEQ_GT(tp->snd_una, th_ack)) {
11089 		snd_una = tp->snd_una;
11090 	} else {
11091 		snd_una = th_ack;
11092 	}
11093 	pipe = do_rack_compute_pipe(tp, rack, snd_una);
11094 	if (pipe > tp->snd_ssthresh) {
11095 		long sndcnt;
11096 
11097 		sndcnt = rack->r_ctl.rc_prr_delivered * tp->snd_ssthresh;
11098 		if (rack->r_ctl.rc_prr_recovery_fs > 0)
11099 			sndcnt /= (long)rack->r_ctl.rc_prr_recovery_fs;
11100 		else {
11101 			rack->r_ctl.rc_prr_sndcnt = 0;
11102 			rack_log_to_prr(rack, 9, 0, __LINE__);
11103 			sndcnt = 0;
11104 		}
11105 		sndcnt++;
11106 		if (sndcnt > (long)rack->r_ctl.rc_prr_out)
11107 			sndcnt -= rack->r_ctl.rc_prr_out;
11108 		else
11109 			sndcnt = 0;
11110 		rack->r_ctl.rc_prr_sndcnt = sndcnt;
11111 		rack_log_to_prr(rack, 10, 0, __LINE__);
11112 	} else {
11113 		uint32_t limit;
11114 
11115 		if (rack->r_ctl.rc_prr_delivered > rack->r_ctl.rc_prr_out)
11116 			limit = (rack->r_ctl.rc_prr_delivered - rack->r_ctl.rc_prr_out);
11117 		else
11118 			limit = 0;
11119 		if (changed > limit)
11120 			limit = changed;
11121 		limit += ctf_fixed_maxseg(tp);
11122 		if (tp->snd_ssthresh > pipe) {
11123 			rack->r_ctl.rc_prr_sndcnt = min((tp->snd_ssthresh - pipe), limit);
11124 			rack_log_to_prr(rack, 11, 0, __LINE__);
11125 		} else {
11126 			rack->r_ctl.rc_prr_sndcnt = min(0, limit);
11127 			rack_log_to_prr(rack, 12, 0, __LINE__);
11128 		}
11129 	}
11130 }
11131 
11132 static void
11133 rack_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th, int entered_recovery, int dup_ack_struck,
11134 	     int *dsack_seen, int *sacks_seen)
11135 {
11136 	uint32_t changed;
11137 	struct tcp_rack *rack;
11138 	struct rack_sendmap *rsm;
11139 	struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1];
11140 	register uint32_t th_ack;
11141 	int32_t i, j, k, num_sack_blks = 0;
11142 	uint32_t cts, acked, ack_point;
11143 	int loop_start = 0, moved_two = 0, no_extra = 0;
11144 	uint32_t tsused;
11145 	uint32_t segsiz, o_cnt;
11146 
11147 
11148 	INP_WLOCK_ASSERT(tptoinpcb(tp));
11149 	if (tcp_get_flags(th) & TH_RST) {
11150 		/* We don't log resets */
11151 		return;
11152 	}
11153 	rack = (struct tcp_rack *)tp->t_fb_ptr;
11154 	cts = tcp_get_usecs(NULL);
11155 	rsm = tqhash_min(rack->r_ctl.tqh);
11156 	changed = 0;
11157 	th_ack = th->th_ack;
11158 	if (rack->sack_attack_disable == 0)
11159 		rack_do_decay(rack);
11160 	segsiz = ctf_fixed_maxseg(rack->rc_tp);
11161 	if (BYTES_THIS_ACK(tp, th) >=  segsiz) {
11162 		/*
11163 		 * You only get credit for
11164 		 * MSS and greater (and you get extra
11165 		 * credit for larger cum-ack moves).
11166 		 */
11167 		int ac;
11168 
11169 		ac = BYTES_THIS_ACK(tp, th) / ctf_fixed_maxseg(rack->rc_tp);
11170 		rack->r_ctl.ack_count += ac;
11171 		counter_u64_add(rack_ack_total, ac);
11172 	}
11173 	if (rack->r_ctl.ack_count > 0xfff00000) {
11174 		/*
11175 		 * reduce the number to keep us under
11176 		 * a uint32_t.
11177 		 */
11178 		rack->r_ctl.ack_count /= 2;
11179 		rack->r_ctl.sack_count /= 2;
11180 	}
11181 	if (SEQ_GT(th_ack, tp->snd_una)) {
11182 		rack_log_progress_event(rack, tp, ticks, PROGRESS_UPDATE, __LINE__);
11183 		tp->t_acktime = ticks;
11184 	}
11185 	if (rsm && SEQ_GT(th_ack, rsm->r_start))
11186 		changed = th_ack - rsm->r_start;
11187 	if (changed) {
11188 		rack_process_to_cumack(tp, rack, th_ack, cts, to,
11189 				       tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time));
11190 	}
11191 	if ((to->to_flags & TOF_SACK) == 0) {
11192 		/* We are done nothing left and no sack. */
11193 		rack_handle_might_revert(tp, rack);
11194 		/*
11195 		 * For cases where we struck a dup-ack
11196 		 * with no SACK, add to the changes so
11197 		 * PRR will work right.
11198 		 */
11199 		if (dup_ack_struck && (changed == 0)) {
11200 			changed += ctf_fixed_maxseg(rack->rc_tp);
11201 		}
11202 		goto out;
11203 	}
11204 	/* Sack block processing */
11205 	if (SEQ_GT(th_ack, tp->snd_una))
11206 		ack_point = th_ack;
11207 	else
11208 		ack_point = tp->snd_una;
11209 	for (i = 0; i < to->to_nsacks; i++) {
11210 		bcopy((to->to_sacks + i * TCPOLEN_SACK),
11211 		      &sack, sizeof(sack));
11212 		sack.start = ntohl(sack.start);
11213 		sack.end = ntohl(sack.end);
11214 		if (SEQ_GT(sack.end, sack.start) &&
11215 		    SEQ_GT(sack.start, ack_point) &&
11216 		    SEQ_LT(sack.start, tp->snd_max) &&
11217 		    SEQ_GT(sack.end, ack_point) &&
11218 		    SEQ_LEQ(sack.end, tp->snd_max)) {
11219 			sack_blocks[num_sack_blks] = sack;
11220 			num_sack_blks++;
11221 		} else if (SEQ_LEQ(sack.start, th_ack) &&
11222 			   SEQ_LEQ(sack.end, th_ack)) {
11223 			int was_tlp;
11224 
11225 			if (dsack_seen != NULL)
11226 				*dsack_seen = 1;
11227 			was_tlp = rack_note_dsack(rack, sack.start, sack.end);
11228 			/*
11229 			 * Its a D-SACK block.
11230 			 */
11231 			tcp_record_dsack(tp, sack.start, sack.end, was_tlp);
11232 		}
11233 	}
11234 	if (rack->rc_dsack_round_seen) {
11235 		/* Is the dsack roound over? */
11236 		if (SEQ_GEQ(th_ack, rack->r_ctl.dsack_round_end)) {
11237 			/* Yes it is */
11238 			rack->rc_dsack_round_seen = 0;
11239 			rack_log_dsack_event(rack, 3, __LINE__, 0, 0);
11240 		}
11241 	}
11242 	/*
11243 	 * Sort the SACK blocks so we can update the rack scoreboard with
11244 	 * just one pass.
11245 	 */
11246 	o_cnt = num_sack_blks;
11247 	num_sack_blks = sack_filter_blks(&rack->r_ctl.rack_sf, sack_blocks,
11248 					 num_sack_blks, th->th_ack);
11249 	ctf_log_sack_filter(rack->rc_tp, num_sack_blks, sack_blocks);
11250 	if (sacks_seen != NULL)
11251 		*sacks_seen = num_sack_blks;
11252 	if (num_sack_blks == 0) {
11253 		/* Nothing to sack, but we need to update counts */
11254 		if ((o_cnt == 1) &&
11255 		    (*dsack_seen != 1))
11256 			rack->r_ctl.sack_count++;
11257 		else if (o_cnt > 1)
11258 			rack->r_ctl.sack_count++;
11259 		goto out_with_totals;
11260 	}
11261 	if (rack->sack_attack_disable) {
11262 		/*
11263 		 * An attacker disablement is in place, for
11264 		 * every sack block that is not at least a full MSS
11265 		 * count up sack_count.
11266 		 */
11267 		for (i = 0; i < num_sack_blks; i++) {
11268 			if ((sack_blocks[i].end - sack_blocks[i].start) < segsiz) {
11269 				rack->r_ctl.sack_count++;
11270 			}
11271 			if (rack->r_ctl.sack_count > 0xfff00000) {
11272 				/*
11273 				 * reduce the number to keep us under
11274 				 * a uint32_t.
11275 				 */
11276 				rack->r_ctl.ack_count /= 2;
11277 				rack->r_ctl.sack_count /= 2;
11278 			}
11279 		}
11280 		goto out;
11281 	}
11282 	/* Its a sack of some sort */
11283 	rack->r_ctl.sack_count += num_sack_blks;
11284 	if (rack->r_ctl.sack_count > 0xfff00000) {
11285 		/*
11286 		 * reduce the number to keep us under
11287 		 * a uint32_t.
11288 		 */
11289 		rack->r_ctl.ack_count /= 2;
11290 		rack->r_ctl.sack_count /= 2;
11291 	}
11292 	if (num_sack_blks < 2) {
11293 		/* Only one, we don't need to sort */
11294 		goto do_sack_work;
11295 	}
11296 	/* Sort the sacks */
11297 	for (i = 0; i < num_sack_blks; i++) {
11298 		for (j = i + 1; j < num_sack_blks; j++) {
11299 			if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
11300 				sack = sack_blocks[i];
11301 				sack_blocks[i] = sack_blocks[j];
11302 				sack_blocks[j] = sack;
11303 			}
11304 		}
11305 	}
11306 	/*
11307 	 * Now are any of the sack block ends the same (yes some
11308 	 * implementations send these)?
11309 	 */
11310 again:
11311 	if (num_sack_blks == 0)
11312 		goto out_with_totals;
11313 	if (num_sack_blks > 1) {
11314 		for (i = 0; i < num_sack_blks; i++) {
11315 			for (j = i + 1; j < num_sack_blks; j++) {
11316 				if (sack_blocks[i].end == sack_blocks[j].end) {
11317 					/*
11318 					 * Ok these two have the same end we
11319 					 * want the smallest end and then
11320 					 * throw away the larger and start
11321 					 * again.
11322 					 */
11323 					if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) {
11324 						/*
11325 						 * The second block covers
11326 						 * more area use that
11327 						 */
11328 						sack_blocks[i].start = sack_blocks[j].start;
11329 					}
11330 					/*
11331 					 * Now collapse out the dup-sack and
11332 					 * lower the count
11333 					 */
11334 					for (k = (j + 1); k < num_sack_blks; k++) {
11335 						sack_blocks[j].start = sack_blocks[k].start;
11336 						sack_blocks[j].end = sack_blocks[k].end;
11337 						j++;
11338 					}
11339 					num_sack_blks--;
11340 					goto again;
11341 				}
11342 			}
11343 		}
11344 	}
11345 do_sack_work:
11346 	/*
11347 	 * First lets look to see if
11348 	 * we have retransmitted and
11349 	 * can use the transmit next?
11350 	 */
11351 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
11352 	if (rsm &&
11353 	    SEQ_GT(sack_blocks[0].end, rsm->r_start) &&
11354 	    SEQ_LT(sack_blocks[0].start, rsm->r_end)) {
11355 		/*
11356 		 * We probably did the FR and the next
11357 		 * SACK in continues as we would expect.
11358 		 */
11359 		acked = rack_proc_sack_blk(tp, rack, &sack_blocks[0], to, &rsm, cts, &no_extra, &moved_two, segsiz);
11360 		if (acked) {
11361 			rack->r_wanted_output = 1;
11362 			changed += acked;
11363 		}
11364 		if (num_sack_blks == 1) {
11365 			/*
11366 			 * This is what we would expect from
11367 			 * a normal implementation to happen
11368 			 * after we have retransmitted the FR,
11369 			 * i.e the sack-filter pushes down
11370 			 * to 1 block and the next to be retransmitted
11371 			 * is the sequence in the sack block (has more
11372 			 * are acked). Count this as ACK'd data to boost
11373 			 * up the chances of recovering any false positives.
11374 			 */
11375 			rack->r_ctl.ack_count += (acked / ctf_fixed_maxseg(rack->rc_tp));
11376 			counter_u64_add(rack_ack_total, (acked / ctf_fixed_maxseg(rack->rc_tp)));
11377 			counter_u64_add(rack_express_sack, 1);
11378 			if (rack->r_ctl.ack_count > 0xfff00000) {
11379 				/*
11380 				 * reduce the number to keep us under
11381 				 * a uint32_t.
11382 				 */
11383 				rack->r_ctl.ack_count /= 2;
11384 				rack->r_ctl.sack_count /= 2;
11385 			}
11386 			if (moved_two) {
11387 				/*
11388 				 * If we did not get a SACK for at least a MSS and
11389 				 * had to move at all, or if we moved more than our
11390 				 * threshold, it counts against the "extra" move.
11391 				 */
11392 				rack->r_ctl.sack_moved_extra += moved_two;
11393 				rack->r_ctl.sack_noextra_move += no_extra;
11394 				counter_u64_add(rack_move_some, 1);
11395 			} else {
11396 				/*
11397 				 * else we did not have to move
11398 				 * any more than we would expect.
11399 				 */
11400 				rack->r_ctl.sack_noextra_move += no_extra;
11401 				rack->r_ctl.sack_noextra_move++;
11402 				counter_u64_add(rack_move_none, 1);
11403 			}
11404 			if ((rack->r_ctl.sack_moved_extra > 0xfff00000) ||
11405 			    (rack->r_ctl.sack_noextra_move > 0xfff00000)) {
11406 				rack->r_ctl.sack_moved_extra /= 2;
11407 				rack->r_ctl.sack_noextra_move /= 2;
11408 			}
11409 			goto out_with_totals;
11410 		} else {
11411 			/*
11412 			 * Start the loop through the
11413 			 * rest of blocks, past the first block.
11414 			 */
11415 			loop_start = 1;
11416 		}
11417 	}
11418 	counter_u64_add(rack_sack_total, 1);
11419 	rsm = rack->r_ctl.rc_sacklast;
11420 	for (i = loop_start; i < num_sack_blks; i++) {
11421 		acked = rack_proc_sack_blk(tp, rack, &sack_blocks[i], to, &rsm, cts, &no_extra, &moved_two, segsiz);
11422 		if (acked) {
11423 			rack->r_wanted_output = 1;
11424 			changed += acked;
11425 		}
11426 		if (moved_two) {
11427 			/*
11428 			 * If we did not get a SACK for at least a MSS and
11429 			 * had to move at all, or if we moved more than our
11430 			 * threshold, it counts against the "extra" move.
11431 			 */
11432 			rack->r_ctl.sack_moved_extra += moved_two;
11433 			rack->r_ctl.sack_noextra_move += no_extra;
11434 			counter_u64_add(rack_move_some, 1);
11435 		} else {
11436 			/*
11437 			 * else we did not have to move
11438 			 * any more than we would expect.
11439 			 */
11440 			rack->r_ctl.sack_noextra_move += no_extra;
11441 			rack->r_ctl.sack_noextra_move++;
11442 			counter_u64_add(rack_move_none, 1);
11443 		}
11444 		if ((rack->r_ctl.sack_moved_extra > 0xfff00000) ||
11445 		    (rack->r_ctl.sack_noextra_move > 0xfff00000)) {
11446 			rack->r_ctl.sack_moved_extra /= 2;
11447 			rack->r_ctl.sack_noextra_move /= 2;
11448 		}
11449 		if (moved_two && (acked < ctf_fixed_maxseg(rack->rc_tp))) {
11450 			/*
11451 			 * If the SACK was not a full MSS then
11452 			 * we add to sack_count the number of
11453 			 * MSS's (or possibly more than
11454 			 * a MSS if its a TSO send) we had to skip by.
11455 			 */
11456 			rack->r_ctl.sack_count += moved_two;
11457 			if (rack->r_ctl.sack_count > 0xfff00000) {
11458 				rack->r_ctl.ack_count /= 2;
11459 				rack->r_ctl.sack_count /= 2;
11460 			}
11461 			counter_u64_add(rack_sack_total, moved_two);
11462 		}
11463 		/*
11464 		 * Now we need to setup for the next
11465 		 * round. First we make sure we won't
11466 		 * exceed the size of our uint32_t on
11467 		 * the various counts, and then clear out
11468 		 * moved_two.
11469 		 */
11470 		moved_two = 0;
11471 		no_extra = 0;
11472 	}
11473 out_with_totals:
11474 	if (num_sack_blks > 1) {
11475 		/*
11476 		 * You get an extra stroke if
11477 		 * you have more than one sack-blk, this
11478 		 * could be where we are skipping forward
11479 		 * and the sack-filter is still working, or
11480 		 * it could be an attacker constantly
11481 		 * moving us.
11482 		 */
11483 		rack->r_ctl.sack_moved_extra++;
11484 		counter_u64_add(rack_move_some, 1);
11485 	}
11486 out:
11487 #ifdef TCP_SAD_DETECTION
11488 	rack_do_detection(tp, rack, BYTES_THIS_ACK(tp, th), ctf_fixed_maxseg(rack->rc_tp));
11489 #endif
11490 	if (changed) {
11491 		/* Something changed cancel the rack timer */
11492 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
11493 	}
11494 	tsused = tcp_get_usecs(NULL);
11495 	rsm = tcp_rack_output(tp, rack, tsused);
11496 	if ((!IN_FASTRECOVERY(tp->t_flags)) &&
11497 	    rsm &&
11498 	    ((rsm->r_flags & RACK_MUST_RXT) == 0)) {
11499 		/* Enter recovery */
11500 		entered_recovery = 1;
11501 		rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__);
11502 		/*
11503 		 * When we enter recovery we need to assure we send
11504 		 * one packet.
11505 		 */
11506 		if (rack->rack_no_prr == 0) {
11507 			rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
11508 			rack_log_to_prr(rack, 8, 0, __LINE__);
11509 		}
11510 		rack->r_timer_override = 1;
11511 		rack->r_early = 0;
11512 		rack->r_ctl.rc_agg_early = 0;
11513 	} else if (IN_FASTRECOVERY(tp->t_flags) &&
11514 		   rsm &&
11515 		   (rack->r_rr_config == 3)) {
11516 		/*
11517 		 * Assure we can output and we get no
11518 		 * remembered pace time except the retransmit.
11519 		 */
11520 		rack->r_timer_override = 1;
11521 		rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
11522 		rack->r_ctl.rc_resend = rsm;
11523 	}
11524 	if (IN_FASTRECOVERY(tp->t_flags) &&
11525 	    (rack->rack_no_prr == 0) &&
11526 	    (entered_recovery == 0)) {
11527 		rack_update_prr(tp, rack, changed, th_ack);
11528 		if ((rsm && (rack->r_ctl.rc_prr_sndcnt >= ctf_fixed_maxseg(tp)) &&
11529 		     ((tcp_in_hpts(rack->rc_tp) == 0) &&
11530 		      ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)))) {
11531 			/*
11532 			 * If you are pacing output you don't want
11533 			 * to override.
11534 			 */
11535 			rack->r_early = 0;
11536 			rack->r_ctl.rc_agg_early = 0;
11537 			rack->r_timer_override = 1;
11538 		}
11539 	}
11540 }
11541 
11542 static void
11543 rack_strike_dupack(struct tcp_rack *rack)
11544 {
11545 	struct rack_sendmap *rsm;
11546 
11547 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
11548 	while (rsm) {
11549 		/*
11550 		 * We need to skip anything already set
11551 		 * to be retransmitted.
11552 		 */
11553 		if ((rsm->r_dupack >= DUP_ACK_THRESHOLD)  ||
11554 		    (rsm->r_flags & RACK_MUST_RXT)) {
11555 			rsm = TAILQ_NEXT(rsm, r_tnext);
11556 			continue;
11557 		}
11558 		break;
11559 	}
11560 	if (rsm && (rsm->r_dupack < 0xff)) {
11561 		rsm->r_dupack++;
11562 		if (rsm->r_dupack >= DUP_ACK_THRESHOLD) {
11563 			struct timeval tv;
11564 			uint32_t cts;
11565 			/*
11566 			 * Here we see if we need to retransmit. For
11567 			 * a SACK type connection if enough time has passed
11568 			 * we will get a return of the rsm. For a non-sack
11569 			 * connection we will get the rsm returned if the
11570 			 * dupack value is 3 or more.
11571 			 */
11572 			cts = tcp_get_usecs(&tv);
11573 			rack->r_ctl.rc_resend = tcp_rack_output(rack->rc_tp, rack, cts);
11574 			if (rack->r_ctl.rc_resend != NULL) {
11575 				if (!IN_FASTRECOVERY(rack->rc_tp->t_flags)) {
11576 					rack_cong_signal(rack->rc_tp, CC_NDUPACK,
11577 							 rack->rc_tp->snd_una, __LINE__);
11578 				}
11579 				rack->r_wanted_output = 1;
11580 				rack->r_timer_override = 1;
11581 				rack_log_retran_reason(rack, rsm, __LINE__, 1, 3);
11582 			}
11583 		} else {
11584 			rack_log_retran_reason(rack, rsm, __LINE__, 0, 3);
11585 		}
11586 	}
11587 }
11588 
11589 static void
11590 rack_check_bottom_drag(struct tcpcb *tp,
11591 		       struct tcp_rack *rack,
11592 		       struct socket *so)
11593 {
11594 	uint32_t segsiz, minseg;
11595 
11596 	segsiz = ctf_fixed_maxseg(tp);
11597 	minseg = segsiz;
11598 	if (tp->snd_max == tp->snd_una) {
11599 		/*
11600 		 * We are doing dynamic pacing and we are way
11601 		 * under. Basically everything got acked while
11602 		 * we were still waiting on the pacer to expire.
11603 		 *
11604 		 * This means we need to boost the b/w in
11605 		 * addition to any earlier boosting of
11606 		 * the multiplier.
11607 		 */
11608 		uint64_t lt_bw;
11609 
11610 		lt_bw = rack_get_lt_bw(rack);
11611 		rack->rc_dragged_bottom = 1;
11612 		rack_validate_multipliers_at_or_above100(rack);
11613 		if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_VALID) &&
11614 		    (lt_bw > 0)) {
11615 			/*
11616 			 * Lets use the long-term b/w we have
11617 			 * been getting as a base.
11618 			 */
11619 			if (rack->rc_gp_filled == 0) {
11620 				if (lt_bw > ONE_POINT_TWO_MEG) {
11621 					/*
11622 					 * If we have no measurement
11623 					 * don't let us set in more than
11624 					 * 1.2Mbps. If we are still too
11625 					 * low after pacing with this we
11626 					 * will hopefully have a max b/w
11627 					 * available to sanity check things.
11628 					 */
11629 					lt_bw = ONE_POINT_TWO_MEG;
11630 				}
11631 				rack->r_ctl.rc_rtt_diff = 0;
11632 				rack->r_ctl.gp_bw = lt_bw;
11633 				rack->rc_gp_filled = 1;
11634 				if (rack->r_ctl.num_measurements < RACK_REQ_AVG)
11635 					rack->r_ctl.num_measurements = RACK_REQ_AVG;
11636 				rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
11637 			} else if (lt_bw > rack->r_ctl.gp_bw) {
11638 				rack->r_ctl.rc_rtt_diff = 0;
11639 				if (rack->r_ctl.num_measurements < RACK_REQ_AVG)
11640 					rack->r_ctl.num_measurements = RACK_REQ_AVG;
11641 				rack->r_ctl.gp_bw = lt_bw;
11642 				rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
11643 			} else
11644 				rack_increase_bw_mul(rack, -1, 0, 0, 1);
11645 			if ((rack->gp_ready == 0) &&
11646 			    (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) {
11647 				/* We have enough measurements now */
11648 				rack->gp_ready = 1;
11649 				if (rack->dgp_on ||
11650 				    rack->rack_hibeta)
11651 					rack_set_cc_pacing(rack);
11652 				if (rack->defer_options)
11653 					rack_apply_deferred_options(rack);
11654 			}
11655 		} else {
11656 			/*
11657 			 * zero rtt possibly?, settle for just an old increase.
11658 			 */
11659 			rack_increase_bw_mul(rack, -1, 0, 0, 1);
11660 		}
11661 	} else if ((IN_FASTRECOVERY(tp->t_flags) == 0) &&
11662 		   (sbavail(&so->so_snd) > max((segsiz * (4 + rack_req_segs)),
11663 					       minseg)) &&
11664 		   (rack->r_ctl.cwnd_to_use > max((segsiz * (rack_req_segs + 2)), minseg)) &&
11665 		   (tp->snd_wnd > max((segsiz * (rack_req_segs + 2)), minseg)) &&
11666 		   (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) <=
11667 		    (segsiz * rack_req_segs))) {
11668 		/*
11669 		 * We are doing dynamic GP pacing and
11670 		 * we have everything except 1MSS or less
11671 		 * bytes left out. We are still pacing away.
11672 		 * And there is data that could be sent, This
11673 		 * means we are inserting delayed ack time in
11674 		 * our measurements because we are pacing too slow.
11675 		 */
11676 		rack_validate_multipliers_at_or_above100(rack);
11677 		rack->rc_dragged_bottom = 1;
11678 		rack_increase_bw_mul(rack, -1, 0, 0, 1);
11679 	}
11680 }
11681 
11682 #ifdef TCP_REQUEST_TRK
11683 static void
11684 rack_log_hybrid(struct tcp_rack *rack, uint32_t seq,
11685 		struct tcp_sendfile_track *cur, uint8_t mod, int line, int err)
11686 {
11687 	int do_log;
11688 
11689 	do_log = tcp_bblogging_on(rack->rc_tp);
11690 	if (do_log == 0) {
11691 		if ((do_log = tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING) )== 0)
11692 			return;
11693 		/* We only allow the three below with point logging on */
11694 		if ((mod != HYBRID_LOG_RULES_APP) &&
11695 		    (mod != HYBRID_LOG_RULES_SET) &&
11696 		    (mod != HYBRID_LOG_REQ_COMP))
11697 			return;
11698 
11699 	}
11700 	if (do_log) {
11701 		union tcp_log_stackspecific log;
11702 		struct timeval tv;
11703 
11704 		/* Convert our ms to a microsecond */
11705 		memset(&log, 0, sizeof(log));
11706 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
11707 		log.u_bbr.flex1 = seq;
11708 		log.u_bbr.cwnd_gain = line;
11709 		if (cur != NULL) {
11710 			uint64_t off;
11711 
11712 			log.u_bbr.flex2 = cur->start_seq;
11713 			log.u_bbr.flex3 = cur->end_seq;
11714 			log.u_bbr.flex4 = (uint32_t)((cur->localtime >> 32) & 0x00000000ffffffff);
11715 			log.u_bbr.flex5 = (uint32_t)(cur->localtime & 0x00000000ffffffff);
11716 			log.u_bbr.flex6 = cur->flags;
11717 			log.u_bbr.pkts_out = cur->hybrid_flags;
11718 			log.u_bbr.rttProp = cur->timestamp;
11719 			log.u_bbr.cur_del_rate = cur->cspr;
11720 			log.u_bbr.bw_inuse = cur->start;
11721 			log.u_bbr.applimited = (uint32_t)(cur->end & 0x00000000ffffffff);
11722 			log.u_bbr.delivered = (uint32_t)((cur->end >> 32) & 0x00000000ffffffff) ;
11723 			log.u_bbr.epoch = (uint32_t)(cur->deadline & 0x00000000ffffffff);
11724 			log.u_bbr.lt_epoch = (uint32_t)((cur->deadline >> 32) & 0x00000000ffffffff) ;
11725 			log.u_bbr.bbr_state = 1;
11726 #ifdef TCP_REQUEST_TRK
11727 			off = (uint64_t)(cur) - (uint64_t)(&rack->rc_tp->t_tcpreq_info[0]);
11728 			log.u_bbr.use_lt_bw = (uint8_t)(off / sizeof(struct tcp_sendfile_track));
11729 #endif
11730 		} else {
11731 			log.u_bbr.flex2 = err;
11732 		}
11733 		/*
11734 		 * Fill in flex7 to be CHD (catchup|hybrid|DGP)
11735 		 */
11736 		log.u_bbr.flex7 = rack->rc_catch_up;
11737 		log.u_bbr.flex7 <<= 1;
11738 		log.u_bbr.flex7 |= rack->rc_hybrid_mode;
11739 		log.u_bbr.flex7 <<= 1;
11740 		log.u_bbr.flex7 |= rack->dgp_on;
11741 		log.u_bbr.flex8 = mod;
11742 		log.u_bbr.delRate = rack->r_ctl.bw_rate_cap;
11743 		log.u_bbr.bbr_substate = rack->r_ctl.client_suggested_maxseg;
11744 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
11745 		log.u_bbr.pkt_epoch = rack->rc_tp->tcp_hybrid_start;
11746 		log.u_bbr.lost = rack->rc_tp->tcp_hybrid_error;
11747 		log.u_bbr.pacing_gain = (uint16_t)rack->rc_tp->tcp_hybrid_stop;
11748 		tcp_log_event(rack->rc_tp, NULL,
11749 		    &rack->rc_inp->inp_socket->so_rcv,
11750 		    &rack->rc_inp->inp_socket->so_snd,
11751 		    TCP_HYBRID_PACING_LOG, 0,
11752 	            0, &log, false, NULL, __func__, __LINE__, &tv);
11753 	}
11754 }
11755 #endif
11756 
11757 #ifdef TCP_REQUEST_TRK
11758 static void
11759 rack_set_dgp_hybrid_mode(struct tcp_rack *rack, tcp_seq seq, uint32_t len)
11760 {
11761 	struct tcp_sendfile_track *rc_cur;
11762 	struct tcpcb *tp;
11763 	int err = 0;
11764 
11765 	rc_cur = tcp_req_find_req_for_seq(rack->rc_tp, seq);
11766 	if (rc_cur == NULL) {
11767 		/* If not in the beginning what about the end piece */
11768 		if (rack->rc_hybrid_mode)
11769 			rack_log_hybrid(rack, seq, NULL, HYBRID_LOG_NO_RANGE, __LINE__, err);
11770 		rc_cur = tcp_req_find_req_for_seq(rack->rc_tp, (seq + len - 1));
11771 	} else {
11772 		err = 12345;
11773 	}
11774 	/* If we find no parameters we are in straight DGP mode */
11775 	if(rc_cur == NULL) {
11776 		/* None found for this seq, just DGP for now */
11777 		rack->r_ctl.client_suggested_maxseg = 0;
11778 		rack->rc_catch_up = 0;
11779 		rack->r_ctl.bw_rate_cap = 0;
11780 		if (rack->rc_hybrid_mode)
11781 			rack_log_hybrid(rack, (seq + len - 1), NULL, HYBRID_LOG_NO_RANGE, __LINE__, err);
11782 		if (rack->r_ctl.rc_last_sft) {
11783 			rack->r_ctl.rc_last_sft = NULL;
11784 		}
11785 		return;
11786 	}
11787 	if ((rc_cur->hybrid_flags & TCP_HYBRID_PACING_WASSET) == 0) {
11788 		/* This entry was never setup for hybrid pacing on/off etc */
11789 		return;
11790 	}
11791 	/*
11792 	 * Ok if we have a new entry *or* have never
11793 	 * set up an entry we need to proceed. If
11794 	 * we have already set it up this entry we
11795 	 * just continue along with what we already
11796 	 * setup.
11797 	 */
11798 	tp = rack->rc_tp;
11799 	if ((rack->r_ctl.rc_last_sft != NULL) &&
11800 	    (rack->r_ctl.rc_last_sft == rc_cur)) {
11801 		/* Its already in place */
11802 		if (rack->rc_hybrid_mode)
11803 			rack_log_hybrid(rack, seq, rc_cur, HYBRID_LOG_ISSAME, __LINE__, 0);
11804 		return;
11805 	}
11806 	if (rack->rc_hybrid_mode == 0) {
11807 		rack->r_ctl.rc_last_sft = rc_cur;
11808 		rack_log_hybrid(rack, seq, rc_cur, HYBRID_LOG_RULES_APP, __LINE__, 0);
11809 		return;
11810 	}
11811 	if ((rc_cur->hybrid_flags & TCP_HYBRID_PACING_CSPR) && rc_cur->cspr){
11812 		/* Compensate for all the header overhead's */
11813 		rack->r_ctl.bw_rate_cap	= rack_compensate_for_linerate(rack, rc_cur->cspr);
11814 	} else
11815 		rack->r_ctl.bw_rate_cap = 0;
11816 	if (rc_cur->hybrid_flags & TCP_HYBRID_PACING_H_MS)
11817 		rack->r_ctl.client_suggested_maxseg = rc_cur->hint_maxseg;
11818 	else
11819 		rack->r_ctl.client_suggested_maxseg = 0;
11820 	if ((rc_cur->hybrid_flags & TCP_HYBRID_PACING_CU) &&
11821 	    (rc_cur->cspr > 0)) {
11822 		uint64_t len;
11823 
11824 		rack->rc_catch_up = 1;
11825 		/*
11826 		 * Calculate the deadline time, first set the
11827 		 * time to when the request arrived.
11828 		 */
11829 		rc_cur->deadline = rc_cur->localtime;
11830 		/*
11831 		 * Next calculate the length and compensate for
11832 		 * TLS if need be.
11833 		 */
11834 		len = rc_cur->end - rc_cur->start;
11835 		if (tp->t_inpcb.inp_socket->so_snd.sb_tls_info) {
11836 			/*
11837 			 * This session is doing TLS. Take a swag guess
11838 			 * at the overhead.
11839 			 */
11840 			len += tcp_estimate_tls_overhead(tp->t_inpcb.inp_socket, len);
11841 		}
11842 		/*
11843 		 * Now considering the size, and the cspr, what is the time that
11844 		 * would be required at the cspr rate. Here we use the raw
11845 		 * cspr value since the client only looks at the raw data. We
11846 		 * do use len which includes TLS overhead, but not the TCP/IP etc.
11847 		 * That will get made up for in the CU pacing rate set.
11848 		 */
11849 		len *= HPTS_USEC_IN_SEC;
11850 		len /= rc_cur->cspr;
11851 		rc_cur->deadline += len;
11852 	} else {
11853 		rack->rc_catch_up = 0;
11854 		rc_cur->deadline = 0;
11855 	}
11856 	if (rack->r_ctl.client_suggested_maxseg != 0) {
11857 		/*
11858 		 * We need to reset the max pace segs if we have a
11859 		 * client_suggested_maxseg.
11860 		 */
11861 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
11862 	}
11863 	rack_log_hybrid(rack, seq, rc_cur, HYBRID_LOG_RULES_APP, __LINE__, 0);
11864 	/* Remember it for next time and for CU mode */
11865 	rack->r_ctl.rc_last_sft = rc_cur;
11866 }
11867 #endif
11868 
11869 static void
11870 rack_chk_req_and_hybrid_on_out(struct tcp_rack *rack, tcp_seq seq, uint32_t len, uint64_t cts)
11871 {
11872 #ifdef TCP_REQUEST_TRK
11873 	struct tcp_sendfile_track *ent;
11874 
11875 	ent = rack->r_ctl.rc_last_sft;
11876 	if ((ent == NULL) ||
11877 	    (ent->flags == TCP_TRK_TRACK_FLG_EMPTY) ||
11878 	    (SEQ_GEQ(seq, ent->end_seq))) {
11879 		/* Time to update the track. */
11880 		rack_set_dgp_hybrid_mode(rack, seq, len);
11881 		ent = rack->r_ctl.rc_last_sft;
11882 	}
11883 	/* Out of all */
11884 	if (ent == NULL) {
11885 		return;
11886 	}
11887 	if (SEQ_LT(ent->end_seq, (seq + len))) {
11888 		/*
11889 		 * This is the case where our end_seq guess
11890 		 * was wrong. This is usually due to TLS having
11891 		 * more bytes then our guess. It could also be the
11892 		 * case that the client sent in two requests closely
11893 		 * and the SB is full of both so we are sending part
11894 		 * of each (end|beg). In such a case lets move this
11895 		 * guys end to match the end of this send. That
11896 		 * way it will complete when all of it is acked.
11897 		 */
11898 		ent->end_seq = (seq + len);
11899 		if (rack->rc_hybrid_mode)
11900 			rack_log_hybrid_bw(rack, seq, len, 0, 0, HYBRID_LOG_EXTEND, 0, ent, __LINE__);
11901 	}
11902 	/* Now validate we have set the send time of this one */
11903 	if ((ent->flags & TCP_TRK_TRACK_FLG_FSND) == 0) {
11904 		ent->flags |= TCP_TRK_TRACK_FLG_FSND;
11905 		ent->first_send = cts;
11906 		ent->sent_at_fs = rack->rc_tp->t_sndbytes;
11907 		ent->rxt_at_fs = rack->rc_tp->t_snd_rxt_bytes;
11908 	}
11909 #endif
11910 }
11911 
11912 static void
11913 rack_gain_for_fastoutput(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t acked_amount)
11914 {
11915 	/*
11916 	 * The fast output path is enabled and we
11917 	 * have moved the cumack forward. Lets see if
11918 	 * we can expand forward the fast path length by
11919 	 * that amount. What we would ideally like to
11920 	 * do is increase the number of bytes in the
11921 	 * fast path block (left_to_send) by the
11922 	 * acked amount. However we have to gate that
11923 	 * by two factors:
11924 	 * 1) The amount outstanding and the rwnd of the peer
11925 	 *    (i.e. we don't want to exceed the rwnd of the peer).
11926 	 *    <and>
11927 	 * 2) The amount of data left in the socket buffer (i.e.
11928 	 *    we can't send beyond what is in the buffer).
11929 	 *
11930 	 * Note that this does not take into account any increase
11931 	 * in the cwnd. We will only extend the fast path by
11932 	 * what was acked.
11933 	 */
11934 	uint32_t new_total, gating_val;
11935 
11936 	new_total = acked_amount + rack->r_ctl.fsb.left_to_send;
11937 	gating_val = min((sbavail(&so->so_snd) - (tp->snd_max - tp->snd_una)),
11938 			 (tp->snd_wnd - (tp->snd_max - tp->snd_una)));
11939 	if (new_total <= gating_val) {
11940 		/* We can increase left_to_send by the acked amount */
11941 		counter_u64_add(rack_extended_rfo, 1);
11942 		rack->r_ctl.fsb.left_to_send = new_total;
11943 		KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(&rack->rc_inp->inp_socket->so_snd) - (tp->snd_max - tp->snd_una))),
11944 			("rack:%p left_to_send:%u sbavail:%u out:%u",
11945 			 rack, rack->r_ctl.fsb.left_to_send,
11946 			 sbavail(&rack->rc_inp->inp_socket->so_snd),
11947 			 (tp->snd_max - tp->snd_una)));
11948 
11949 	}
11950 }
11951 
11952 static void
11953 rack_adjust_sendmap_head(struct tcp_rack *rack, struct sockbuf *sb)
11954 {
11955 	/*
11956 	 * Here any sendmap entry that points to the
11957 	 * beginning mbuf must be adjusted to the correct
11958 	 * offset. This must be called with:
11959 	 * 1) The socket buffer locked
11960 	 * 2) snd_una adjusted to its new position.
11961 	 *
11962 	 * Note that (2) implies rack_ack_received has also
11963 	 * been called and all the sbcut's have been done.
11964 	 *
11965 	 * We grab the first mbuf in the socket buffer and
11966 	 * then go through the front of the sendmap, recalculating
11967 	 * the stored offset for any sendmap entry that has
11968 	 * that mbuf. We must use the sb functions to do this
11969 	 * since its possible an add was done has well as
11970 	 * the subtraction we may have just completed. This should
11971 	 * not be a penalty though, since we just referenced the sb
11972 	 * to go in and trim off the mbufs that we freed (of course
11973 	 * there will be a penalty for the sendmap references though).
11974 	 *
11975 	 * Note also with INVARIANT on, we validate with a KASSERT
11976 	 * that the first sendmap entry has a soff of 0.
11977 	 *
11978 	 */
11979 	struct mbuf *m;
11980 	struct rack_sendmap *rsm;
11981 	tcp_seq snd_una;
11982 #ifdef INVARIANTS
11983 	int first_processed = 0;
11984 #endif
11985 
11986 	snd_una = rack->rc_tp->snd_una;
11987 	SOCKBUF_LOCK_ASSERT(sb);
11988 	m = sb->sb_mb;
11989 	rsm = tqhash_min(rack->r_ctl.tqh);
11990 	if ((rsm == NULL) || (m == NULL)) {
11991 		/* Nothing outstanding */
11992 		return;
11993 	}
11994 	/* The very first RSM's mbuf must point to the head mbuf in the sb */
11995 	KASSERT((rsm->m == m),
11996 		("Rack:%p sb:%p rsm:%p -- first rsm mbuf not aligned to sb",
11997 		 rack, sb, rsm));
11998 	while (rsm->m && (rsm->m == m)) {
11999 		/* one to adjust */
12000 #ifdef INVARIANTS
12001 		struct mbuf *tm;
12002 		uint32_t soff;
12003 
12004 		tm = sbsndmbuf(sb, (rsm->r_start - snd_una), &soff);
12005 		if ((rsm->orig_m_len != m->m_len) ||
12006 		    (rsm->orig_t_space != M_TRAILINGROOM(m))){
12007 			rack_adjust_orig_mlen(rsm);
12008 		}
12009 		if (first_processed == 0) {
12010 			KASSERT((rsm->soff == 0),
12011 				("Rack:%p rsm:%p -- rsm at head but soff not zero",
12012 				 rack, rsm));
12013 			first_processed = 1;
12014 		}
12015 		if ((rsm->soff != soff) || (rsm->m != tm)) {
12016 			/*
12017 			 * This is not a fatal error, we anticipate it
12018 			 * might happen (the else code), so we count it here
12019 			 * so that under invariant we can see that it really
12020 			 * does happen.
12021 			 */
12022 			counter_u64_add(rack_adjust_map_bw, 1);
12023 		}
12024 		rsm->m = tm;
12025 		rsm->soff = soff;
12026 		if (tm) {
12027 			rsm->orig_m_len = rsm->m->m_len;
12028 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
12029 		} else {
12030 			rsm->orig_m_len = 0;
12031 			rsm->orig_t_space = 0;
12032 		}
12033 #else
12034 		rsm->m = sbsndmbuf(sb, (rsm->r_start - snd_una), &rsm->soff);
12035 		if (rsm->m) {
12036 			rsm->orig_m_len = rsm->m->m_len;
12037 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
12038 		} else {
12039 			rsm->orig_m_len = 0;
12040 			rsm->orig_t_space = 0;
12041 		}
12042 #endif
12043 		rsm = tqhash_next(rack->r_ctl.tqh, rsm);
12044 		if (rsm == NULL)
12045 			break;
12046 	}
12047 }
12048 
12049 #ifdef TCP_REQUEST_TRK
12050 static inline void
12051 rack_req_check_for_comp(struct tcp_rack *rack, tcp_seq th_ack)
12052 {
12053 	struct tcp_sendfile_track *ent;
12054 	int i;
12055 
12056 	if ((rack->rc_hybrid_mode == 0) &&
12057 	    (tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING) == 0)) {
12058 		/*
12059 		 * Just do normal completions hybrid pacing is not on
12060 		 * and CLDL is off as well.
12061 		 */
12062 		tcp_req_check_for_comp(rack->rc_tp, th_ack);
12063 		return;
12064 	}
12065 	/*
12066 	 * Originally I was just going to find the th_ack associated
12067 	 * with an entry. But then I realized a large strech ack could
12068 	 * in theory ack two or more requests at once. So instead we
12069 	 * need to find all entries that are completed by th_ack not
12070 	 * just a single entry and do our logging.
12071 	 */
12072 	ent = tcp_req_find_a_req_that_is_completed_by(rack->rc_tp, th_ack, &i);
12073 	while (ent != NULL) {
12074 		/*
12075 		 * We may be doing hybrid pacing or CLDL and need more details possibly
12076 		 * so we do it manually instead of calling
12077 		 * tcp_req_check_for_comp()
12078 		 */
12079 		uint64_t laa, tim, data, cbw, ftim;
12080 
12081 		/* Ok this ack frees it */
12082 		rack_log_hybrid(rack, th_ack,
12083 				ent, HYBRID_LOG_REQ_COMP, __LINE__, 0);
12084 		rack_log_hybrid_sends(rack, ent, __LINE__);
12085 		/* calculate the time based on the ack arrival */
12086 		data = ent->end - ent->start;
12087 		laa = tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time);
12088 		if (ent->flags & TCP_TRK_TRACK_FLG_FSND) {
12089 			if (ent->first_send > ent->localtime)
12090 				ftim = ent->first_send;
12091 			else
12092 				ftim = ent->localtime;
12093 		} else {
12094 			/* TSNH */
12095 			ftim = ent->localtime;
12096 		}
12097 		if (laa > ent->localtime)
12098 			tim = laa - ftim;
12099 		else
12100 			tim = 0;
12101 		cbw = data * HPTS_USEC_IN_SEC;
12102 		if (tim > 0)
12103 			cbw /= tim;
12104 		else
12105 			cbw = 0;
12106 		rack_log_hybrid_bw(rack, th_ack, cbw, tim, data, HYBRID_LOG_BW_MEASURE, 0, ent, __LINE__);
12107 		/*
12108 		 * Check to see if we are freeing what we are pointing to send wise
12109 		 * if so be sure to NULL the pointer so we know we are no longer
12110 		 * set to anything.
12111 		 */
12112 		if (ent == rack->r_ctl.rc_last_sft)
12113 			rack->r_ctl.rc_last_sft = NULL;
12114 		/* Generate the log that the tcp_netflix call would have */
12115 		tcp_req_log_req_info(rack->rc_tp, ent,
12116 				      i, TCP_TRK_REQ_LOG_FREED, 0, 0);
12117 		/* Free it and see if there is another one */
12118 		tcp_req_free_a_slot(rack->rc_tp, ent);
12119 		ent = tcp_req_find_a_req_that_is_completed_by(rack->rc_tp, th_ack, &i);
12120 	}
12121 }
12122 #endif
12123 
12124 
12125 /*
12126  * Return value of 1, we do not need to call rack_process_data().
12127  * return value of 0, rack_process_data can be called.
12128  * For ret_val if its 0 the TCP is locked, if its non-zero
12129  * its unlocked and probably unsafe to touch the TCB.
12130  */
12131 static int
12132 rack_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so,
12133     struct tcpcb *tp, struct tcpopt *to,
12134     uint32_t tiwin, int32_t tlen,
12135     int32_t * ofia, int32_t thflags, int32_t *ret_val)
12136 {
12137 	int32_t ourfinisacked = 0;
12138 	int32_t nsegs, acked_amount;
12139 	int32_t acked;
12140 	struct mbuf *mfree;
12141 	struct tcp_rack *rack;
12142 	int32_t under_pacing = 0;
12143 	int32_t recovery = 0;
12144 
12145 	INP_WLOCK_ASSERT(tptoinpcb(tp));
12146 
12147 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12148 	if (SEQ_GT(th->th_ack, tp->snd_max)) {
12149 		__ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val,
12150 				      &rack->r_ctl.challenge_ack_ts,
12151 				      &rack->r_ctl.challenge_ack_cnt);
12152 		rack->r_wanted_output = 1;
12153 		return (1);
12154 	}
12155 	if (rack->gp_ready &&
12156 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
12157 		under_pacing = 1;
12158 	}
12159 	if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) {
12160 		int in_rec, dup_ack_struck = 0;
12161 		int dsack_seen = 0, sacks_seen = 0;
12162 
12163 		in_rec = IN_FASTRECOVERY(tp->t_flags);
12164 		if (rack->rc_in_persist) {
12165 			tp->t_rxtshift = 0;
12166 			RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
12167 				      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
12168 		}
12169 
12170 		if ((th->th_ack == tp->snd_una) &&
12171 		    (tiwin == tp->snd_wnd) &&
12172 		    ((to->to_flags & TOF_SACK) == 0)) {
12173 			rack_strike_dupack(rack);
12174 			dup_ack_struck = 1;
12175 		}
12176 		rack_log_ack(tp, to, th, ((in_rec == 0) && IN_FASTRECOVERY(tp->t_flags)),
12177 			     dup_ack_struck, &dsack_seen, &sacks_seen);
12178 		if ((rack->sack_attack_disable > 0) &&
12179 		    (th->th_ack == tp->snd_una) &&
12180 		    (tiwin == tp->snd_wnd) &&
12181 		    (dsack_seen == 0) &&
12182 		    (sacks_seen > 0)) {
12183 			/*
12184 			 * If sacks have been disabled we may
12185 			 * want to strike a dup-ack "ignoring" the
12186 			 * sack as long as the sack was not a "dsack". Note
12187 			 * that if no sack is sent (TOF_SACK is off) then the
12188 			 * normal dsack code above rack_log_ack() would have
12189 			 * already struck. So this is just to catch the case
12190 			 * were we are ignoring sacks from this guy due to
12191 			 * it being a suspected attacker.
12192 			 */
12193 			rack_strike_dupack(rack);
12194 		}
12195 
12196 	}
12197 	if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
12198 		/*
12199 		 * Old ack, behind (or duplicate to) the last one rcv'd
12200 		 * Note: We mark reordering is occuring if its
12201 		 * less than and we have not closed our window.
12202 		 */
12203 		if (SEQ_LT(th->th_ack, tp->snd_una) && (sbspace(&so->so_rcv) > ctf_fixed_maxseg(tp))) {
12204 			rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
12205 			if (rack->r_ctl.rc_reorder_ts == 0)
12206 				rack->r_ctl.rc_reorder_ts = 1;
12207 		}
12208 		return (0);
12209 	}
12210 	/*
12211 	 * If we reach this point, ACK is not a duplicate, i.e., it ACKs
12212 	 * something we sent.
12213 	 */
12214 	if (tp->t_flags & TF_NEEDSYN) {
12215 		/*
12216 		 * T/TCP: Connection was half-synchronized, and our SYN has
12217 		 * been ACK'd (so connection is now fully synchronized).  Go
12218 		 * to non-starred state, increment snd_una for ACK of SYN,
12219 		 * and check if we can do window scaling.
12220 		 */
12221 		tp->t_flags &= ~TF_NEEDSYN;
12222 		tp->snd_una++;
12223 		/* Do window scaling? */
12224 		if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
12225 		    (TF_RCVD_SCALE | TF_REQ_SCALE)) {
12226 			tp->rcv_scale = tp->request_r_scale;
12227 			/* Send window already scaled. */
12228 		}
12229 	}
12230 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
12231 
12232 	acked = BYTES_THIS_ACK(tp, th);
12233 	if (acked) {
12234 		/*
12235 		 * Any time we move the cum-ack forward clear
12236 		 * keep-alive tied probe-not-answered. The
12237 		 * persists clears its own on entry.
12238 		 */
12239 		rack->probe_not_answered = 0;
12240 	}
12241 	KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
12242 	KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
12243 	/*
12244 	 * If we just performed our first retransmit, and the ACK arrives
12245 	 * within our recovery window, then it was a mistake to do the
12246 	 * retransmit in the first place.  Recover our original cwnd and
12247 	 * ssthresh, and proceed to transmit where we left off.
12248 	 */
12249 	if ((tp->t_flags & TF_PREVVALID) &&
12250 	    ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
12251 		tp->t_flags &= ~TF_PREVVALID;
12252 		if (tp->t_rxtshift == 1 &&
12253 		    (int)(ticks - tp->t_badrxtwin) < 0)
12254 			rack_cong_signal(tp, CC_RTO_ERR, th->th_ack, __LINE__);
12255 	}
12256 	if (acked) {
12257 		/* assure we are not backed off */
12258 		tp->t_rxtshift = 0;
12259 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
12260 			      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
12261 		rack->rc_tlp_in_progress = 0;
12262 		rack->r_ctl.rc_tlp_cnt_out = 0;
12263 		/*
12264 		 * If it is the RXT timer we want to
12265 		 * stop it, so we can restart a TLP.
12266 		 */
12267 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
12268 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
12269 #ifdef TCP_REQUEST_TRK
12270 		rack_req_check_for_comp(rack, th->th_ack);
12271 #endif
12272 	}
12273 	/*
12274 	 * If we have a timestamp reply, update smoothed round trip time. If
12275 	 * no timestamp is present but transmit timer is running and timed
12276 	 * sequence number was acked, update smoothed round trip time. Since
12277 	 * we now have an rtt measurement, cancel the timer backoff (cf.,
12278 	 * Phil Karn's retransmit alg.). Recompute the initial retransmit
12279 	 * timer.
12280 	 *
12281 	 * Some boxes send broken timestamp replies during the SYN+ACK
12282 	 * phase, ignore timestamps of 0 or we could calculate a huge RTT
12283 	 * and blow up the retransmit timer.
12284 	 */
12285 	/*
12286 	 * If all outstanding data is acked, stop retransmit timer and
12287 	 * remember to restart (more output or persist). If there is more
12288 	 * data to be acked, restart retransmit timer, using current
12289 	 * (possibly backed-off) value.
12290 	 */
12291 	if (acked == 0) {
12292 		if (ofia)
12293 			*ofia = ourfinisacked;
12294 		return (0);
12295 	}
12296 	if (IN_RECOVERY(tp->t_flags)) {
12297 		if (SEQ_LT(th->th_ack, tp->snd_recover) &&
12298 		    (SEQ_LT(th->th_ack, tp->snd_max))) {
12299 			tcp_rack_partialack(tp);
12300 		} else {
12301 			rack_post_recovery(tp, th->th_ack);
12302 			recovery = 1;
12303 		}
12304 	}
12305 	/*
12306 	 * Let the congestion control algorithm update congestion control
12307 	 * related information. This typically means increasing the
12308 	 * congestion window.
12309 	 */
12310 	rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, recovery);
12311 	SOCKBUF_LOCK(&so->so_snd);
12312 	acked_amount = min(acked, (int)sbavail(&so->so_snd));
12313 	tp->snd_wnd -= acked_amount;
12314 	mfree = sbcut_locked(&so->so_snd, acked_amount);
12315 	if ((sbused(&so->so_snd) == 0) &&
12316 	    (acked > acked_amount) &&
12317 	    (tp->t_state >= TCPS_FIN_WAIT_1) &&
12318 	    (tp->t_flags & TF_SENTFIN)) {
12319 		/*
12320 		 * We must be sure our fin
12321 		 * was sent and acked (we can be
12322 		 * in FIN_WAIT_1 without having
12323 		 * sent the fin).
12324 		 */
12325 		ourfinisacked = 1;
12326 	}
12327 	tp->snd_una = th->th_ack;
12328 	/* wakeups? */
12329 	if (acked_amount && sbavail(&so->so_snd))
12330 		rack_adjust_sendmap_head(rack, &so->so_snd);
12331 	rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
12332 	/* NB: sowwakeup_locked() does an implicit unlock. */
12333 	sowwakeup_locked(so);
12334 	/* now check the rxt clamps */
12335 	if ((recovery == 1) &&
12336 	    (rack->excess_rxt_on) &&
12337 	    (rack->r_cwnd_was_clamped == 0))  {
12338 		do_rack_excess_rxt(tp, rack);
12339 	} else if (rack->r_cwnd_was_clamped)
12340 		do_rack_check_for_unclamp(tp, rack);
12341 	m_freem(mfree);
12342 	if (SEQ_GT(tp->snd_una, tp->snd_recover))
12343 		tp->snd_recover = tp->snd_una;
12344 
12345 	if (SEQ_LT(tp->snd_nxt, tp->snd_una)) {
12346 		tp->snd_nxt = tp->snd_una;
12347 	}
12348 	if (under_pacing &&
12349 	    (rack->use_fixed_rate == 0) &&
12350 	    (rack->in_probe_rtt == 0) &&
12351 	    rack->rc_gp_dyn_mul &&
12352 	    rack->rc_always_pace) {
12353 		/* Check if we are dragging bottom */
12354 		rack_check_bottom_drag(tp, rack, so);
12355 	}
12356 	if (tp->snd_una == tp->snd_max) {
12357 		/* Nothing left outstanding */
12358 		tp->t_flags &= ~TF_PREVVALID;
12359 		rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
12360 		rack->r_ctl.retran_during_recovery = 0;
12361 		rack->r_ctl.dsack_byte_cnt = 0;
12362 		if (rack->r_ctl.rc_went_idle_time == 0)
12363 			rack->r_ctl.rc_went_idle_time = 1;
12364 		rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
12365 		if (sbavail(&tptosocket(tp)->so_snd) == 0)
12366 			tp->t_acktime = 0;
12367 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
12368 		rack->rc_suspicious = 0;
12369 		/* Set need output so persist might get set */
12370 		rack->r_wanted_output = 1;
12371 		sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
12372 		if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
12373 		    (sbavail(&so->so_snd) == 0) &&
12374 		    (tp->t_flags2 & TF2_DROP_AF_DATA)) {
12375 			/*
12376 			 * The socket was gone and the
12377 			 * peer sent data (now or in the past), time to
12378 			 * reset him.
12379 			 */
12380 			*ret_val = 1;
12381 			/* tcp_close will kill the inp pre-log the Reset */
12382 			tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
12383 			tp = tcp_close(tp);
12384 			ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen);
12385 			return (1);
12386 		}
12387 	}
12388 	if (ofia)
12389 		*ofia = ourfinisacked;
12390 	return (0);
12391 }
12392 
12393 
12394 static void
12395 rack_log_collapse(struct tcp_rack *rack, uint32_t cnt, uint32_t split, uint32_t out, int line,
12396 		  int dir, uint32_t flags, struct rack_sendmap *rsm)
12397 {
12398 	if (tcp_bblogging_on(rack->rc_tp)) {
12399 		union tcp_log_stackspecific log;
12400 		struct timeval tv;
12401 
12402 		memset(&log, 0, sizeof(log));
12403 		log.u_bbr.flex1 = cnt;
12404 		log.u_bbr.flex2 = split;
12405 		log.u_bbr.flex3 = out;
12406 		log.u_bbr.flex4 = line;
12407 		log.u_bbr.flex5 = rack->r_must_retran;
12408 		log.u_bbr.flex6 = flags;
12409 		log.u_bbr.flex7 = rack->rc_has_collapsed;
12410 		log.u_bbr.flex8 = dir;	/*
12411 					 * 1 is collapsed, 0 is uncollapsed,
12412 					 * 2 is log of a rsm being marked, 3 is a split.
12413 					 */
12414 		if (rsm == NULL)
12415 			log.u_bbr.rttProp = 0;
12416 		else
12417 			log.u_bbr.rttProp = (uint64_t)rsm;
12418 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
12419 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
12420 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
12421 		    &rack->rc_inp->inp_socket->so_rcv,
12422 		    &rack->rc_inp->inp_socket->so_snd,
12423 		    TCP_RACK_LOG_COLLAPSE, 0,
12424 		    0, &log, false, &tv);
12425 	}
12426 }
12427 
12428 static void
12429 rack_collapsed_window(struct tcp_rack *rack, uint32_t out, tcp_seq th_ack, int line)
12430 {
12431 	/*
12432 	 * Here all we do is mark the collapsed point and set the flag.
12433 	 * This may happen again and again, but there is no
12434 	 * sense splitting our map until we know where the
12435 	 * peer finally lands in the collapse.
12436 	 */
12437 	tcp_trace_point(rack->rc_tp, TCP_TP_COLLAPSED_WND);
12438 	if ((rack->rc_has_collapsed == 0) ||
12439 	    (rack->r_ctl.last_collapse_point != (th_ack + rack->rc_tp->snd_wnd)))
12440 		counter_u64_add(rack_collapsed_win_seen, 1);
12441 	rack->r_ctl.last_collapse_point = th_ack + rack->rc_tp->snd_wnd;
12442 	rack->r_ctl.high_collapse_point = rack->rc_tp->snd_max;
12443 	rack->rc_has_collapsed = 1;
12444 	rack->r_collapse_point_valid = 1;
12445 	rack_log_collapse(rack, 0, th_ack, rack->r_ctl.last_collapse_point, line, 1, 0, NULL);
12446 }
12447 
12448 static void
12449 rack_un_collapse_window(struct tcp_rack *rack, int line)
12450 {
12451 	struct rack_sendmap *nrsm, *rsm;
12452 	int cnt = 0, split = 0;
12453 	int insret __diagused;
12454 
12455 
12456 	tcp_trace_point(rack->rc_tp, TCP_TP_COLLAPSED_WND);
12457 	rack->rc_has_collapsed = 0;
12458 	rsm = tqhash_find(rack->r_ctl.tqh, rack->r_ctl.last_collapse_point);
12459 	if (rsm == NULL) {
12460 		/* Nothing to do maybe the peer ack'ed it all */
12461 		rack_log_collapse(rack, 0, 0, ctf_outstanding(rack->rc_tp), line, 0, 0, NULL);
12462 		return;
12463 	}
12464 	/* Now do we need to split this one? */
12465 	if (SEQ_GT(rack->r_ctl.last_collapse_point, rsm->r_start)) {
12466 		rack_log_collapse(rack, rsm->r_start, rsm->r_end,
12467 				  rack->r_ctl.last_collapse_point, line, 3, rsm->r_flags, rsm);
12468 		nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
12469 		if (nrsm == NULL) {
12470 			/* We can't get a rsm, mark all? */
12471 			nrsm = rsm;
12472 			goto no_split;
12473 		}
12474 		/* Clone it */
12475 		split = 1;
12476 		rack_clone_rsm(rack, nrsm, rsm, rack->r_ctl.last_collapse_point);
12477 #ifndef INVARIANTS
12478 		(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
12479 #else
12480 		if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
12481 			panic("Insert in rb tree of %p fails ret:%d rack:%p rsm:%p",
12482 			      nrsm, insret, rack, rsm);
12483 		}
12484 #endif
12485 		rack_log_map_chg(rack->rc_tp, rack, NULL, rsm, nrsm, MAP_SPLIT,
12486 				 rack->r_ctl.last_collapse_point, __LINE__);
12487 		if (rsm->r_in_tmap) {
12488 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
12489 			nrsm->r_in_tmap = 1;
12490 		}
12491 		/*
12492 		 * Set in the new RSM as the
12493 		 * collapsed starting point
12494 		 */
12495 		rsm = nrsm;
12496 	}
12497 
12498 no_split:
12499 	TQHASH_FOREACH_FROM(nrsm, rack->r_ctl.tqh, rsm)  {
12500 		cnt++;
12501 		nrsm->r_flags |= RACK_RWND_COLLAPSED;
12502 		rack_log_collapse(rack, nrsm->r_start, nrsm->r_end, 0, line, 4, nrsm->r_flags, nrsm);
12503 		cnt++;
12504 	}
12505 	if (cnt) {
12506 		counter_u64_add(rack_collapsed_win, 1);
12507 	}
12508 	rack_log_collapse(rack, cnt, split, ctf_outstanding(rack->rc_tp), line, 0, 0, NULL);
12509 }
12510 
12511 static void
12512 rack_handle_delayed_ack(struct tcpcb *tp, struct tcp_rack *rack,
12513 			int32_t tlen, int32_t tfo_syn)
12514 {
12515 	if (DELAY_ACK(tp, tlen) || tfo_syn) {
12516 		rack_timer_cancel(tp, rack,
12517 				  rack->r_ctl.rc_rcvtime, __LINE__);
12518 		tp->t_flags |= TF_DELACK;
12519 	} else {
12520 		rack->r_wanted_output = 1;
12521 		tp->t_flags |= TF_ACKNOW;
12522 	}
12523 }
12524 
12525 static void
12526 rack_validate_fo_sendwin_up(struct tcpcb *tp, struct tcp_rack *rack)
12527 {
12528 	/*
12529 	 * If fast output is in progress, lets validate that
12530 	 * the new window did not shrink on us and make it
12531 	 * so fast output should end.
12532 	 */
12533 	if (rack->r_fast_output) {
12534 		uint32_t out;
12535 
12536 		/*
12537 		 * Calculate what we will send if left as is
12538 		 * and compare that to our send window.
12539 		 */
12540 		out = ctf_outstanding(tp);
12541 		if ((out + rack->r_ctl.fsb.left_to_send) > tp->snd_wnd) {
12542 			/* ok we have an issue */
12543 			if (out >= tp->snd_wnd) {
12544 				/* Turn off fast output the window is met or collapsed */
12545 				rack->r_fast_output = 0;
12546 			} else {
12547 				/* we have some room left */
12548 				rack->r_ctl.fsb.left_to_send = tp->snd_wnd - out;
12549 				if (rack->r_ctl.fsb.left_to_send < ctf_fixed_maxseg(tp)) {
12550 					/* If not at least 1 full segment never mind */
12551 					rack->r_fast_output = 0;
12552 				}
12553 			}
12554 		}
12555 	}
12556 }
12557 
12558 
12559 /*
12560  * Return value of 1, the TCB is unlocked and most
12561  * likely gone, return value of 0, the TCP is still
12562  * locked.
12563  */
12564 static int
12565 rack_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so,
12566     struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
12567     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
12568 {
12569 	/*
12570 	 * Update window information. Don't look at window if no ACK: TAC's
12571 	 * send garbage on first SYN.
12572 	 */
12573 	int32_t nsegs;
12574 	int32_t tfo_syn;
12575 	struct tcp_rack *rack;
12576 
12577 	INP_WLOCK_ASSERT(tptoinpcb(tp));
12578 
12579 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12580 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
12581 	if ((thflags & TH_ACK) &&
12582 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
12583 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
12584 	    (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
12585 		/* keep track of pure window updates */
12586 		if (tlen == 0 &&
12587 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
12588 			KMOD_TCPSTAT_INC(tcps_rcvwinupd);
12589 		tp->snd_wnd = tiwin;
12590 		rack_validate_fo_sendwin_up(tp, rack);
12591 		tp->snd_wl1 = th->th_seq;
12592 		tp->snd_wl2 = th->th_ack;
12593 		if (tp->snd_wnd > tp->max_sndwnd)
12594 			tp->max_sndwnd = tp->snd_wnd;
12595 		rack->r_wanted_output = 1;
12596 	} else if (thflags & TH_ACK) {
12597 		if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) {
12598 			tp->snd_wnd = tiwin;
12599 			rack_validate_fo_sendwin_up(tp, rack);
12600 			tp->snd_wl1 = th->th_seq;
12601 			tp->snd_wl2 = th->th_ack;
12602 		}
12603 	}
12604 	if (tp->snd_wnd < ctf_outstanding(tp))
12605 		/* The peer collapsed the window */
12606 		rack_collapsed_window(rack, ctf_outstanding(tp), th->th_ack, __LINE__);
12607 	else if (rack->rc_has_collapsed)
12608 		rack_un_collapse_window(rack, __LINE__);
12609 	if ((rack->r_collapse_point_valid) &&
12610 	    (SEQ_GT(th->th_ack, rack->r_ctl.high_collapse_point)))
12611 		rack->r_collapse_point_valid = 0;
12612 	/* Was persist timer active and now we have window space? */
12613 	if ((rack->rc_in_persist != 0) &&
12614 	    (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
12615 				rack->r_ctl.rc_pace_min_segs))) {
12616 		rack_exit_persist(tp, rack, rack->r_ctl.rc_rcvtime);
12617 		tp->snd_nxt = tp->snd_max;
12618 		/* Make sure we output to start the timer */
12619 		rack->r_wanted_output = 1;
12620 	}
12621 	/* Do we enter persists? */
12622 	if ((rack->rc_in_persist == 0) &&
12623 	    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
12624 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
12625 	    ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) &&
12626 	    sbavail(&tptosocket(tp)->so_snd) &&
12627 	    (sbavail(&tptosocket(tp)->so_snd) > tp->snd_wnd)) {
12628 		/*
12629 		 * Here the rwnd is less than
12630 		 * the pacing size, we are established,
12631 		 * nothing is outstanding, and there is
12632 		 * data to send. Enter persists.
12633 		 */
12634 		rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, tp->snd_una);
12635 	}
12636 	if (tp->t_flags2 & TF2_DROP_AF_DATA) {
12637 		m_freem(m);
12638 		return (0);
12639 	}
12640 	/*
12641 	 * don't process the URG bit, ignore them drag
12642 	 * along the up.
12643 	 */
12644 	tp->rcv_up = tp->rcv_nxt;
12645 
12646 	/*
12647 	 * Process the segment text, merging it into the TCP sequencing
12648 	 * queue, and arranging for acknowledgment of receipt if necessary.
12649 	 * This process logically involves adjusting tp->rcv_wnd as data is
12650 	 * presented to the user (this happens in tcp_usrreq.c, case
12651 	 * PRU_RCVD).  If a FIN has already been received on this connection
12652 	 * then we just ignore the text.
12653 	 */
12654 	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
12655 		   IS_FASTOPEN(tp->t_flags));
12656 	if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
12657 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
12658 		tcp_seq save_start = th->th_seq;
12659 		tcp_seq save_rnxt  = tp->rcv_nxt;
12660 		int     save_tlen  = tlen;
12661 
12662 		m_adj(m, drop_hdrlen);	/* delayed header drop */
12663 		/*
12664 		 * Insert segment which includes th into TCP reassembly
12665 		 * queue with control block tp.  Set thflags to whether
12666 		 * reassembly now includes a segment with FIN.  This handles
12667 		 * the common case inline (segment is the next to be
12668 		 * received on an established connection, and the queue is
12669 		 * empty), avoiding linkage into and removal from the queue
12670 		 * and repetition of various conversions. Set DELACK for
12671 		 * segments received in order, but ack immediately when
12672 		 * segments are out of order (so fast retransmit can work).
12673 		 */
12674 		if (th->th_seq == tp->rcv_nxt &&
12675 		    SEGQ_EMPTY(tp) &&
12676 		    (TCPS_HAVEESTABLISHED(tp->t_state) ||
12677 		    tfo_syn)) {
12678 #ifdef NETFLIX_SB_LIMITS
12679 			u_int mcnt, appended;
12680 
12681 			if (so->so_rcv.sb_shlim) {
12682 				mcnt = m_memcnt(m);
12683 				appended = 0;
12684 				if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
12685 				    CFO_NOSLEEP, NULL) == false) {
12686 					counter_u64_add(tcp_sb_shlim_fails, 1);
12687 					m_freem(m);
12688 					return (0);
12689 				}
12690 			}
12691 #endif
12692 			rack_handle_delayed_ack(tp, rack, tlen, tfo_syn);
12693 			tp->rcv_nxt += tlen;
12694 			if (tlen &&
12695 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
12696 			    (tp->t_fbyte_in == 0)) {
12697 				tp->t_fbyte_in = ticks;
12698 				if (tp->t_fbyte_in == 0)
12699 					tp->t_fbyte_in = 1;
12700 				if (tp->t_fbyte_out && tp->t_fbyte_in)
12701 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
12702 			}
12703 			thflags = tcp_get_flags(th) & TH_FIN;
12704 			KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs);
12705 			KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
12706 			SOCKBUF_LOCK(&so->so_rcv);
12707 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
12708 				m_freem(m);
12709 			} else
12710 #ifdef NETFLIX_SB_LIMITS
12711 				appended =
12712 #endif
12713 					sbappendstream_locked(&so->so_rcv, m, 0);
12714 
12715 			rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1);
12716 			/* NB: sorwakeup_locked() does an implicit unlock. */
12717 			sorwakeup_locked(so);
12718 #ifdef NETFLIX_SB_LIMITS
12719 			if (so->so_rcv.sb_shlim && appended != mcnt)
12720 				counter_fo_release(so->so_rcv.sb_shlim,
12721 				    mcnt - appended);
12722 #endif
12723 		} else {
12724 			/*
12725 			 * XXX: Due to the header drop above "th" is
12726 			 * theoretically invalid by now.  Fortunately
12727 			 * m_adj() doesn't actually frees any mbufs when
12728 			 * trimming from the head.
12729 			 */
12730 			tcp_seq temp = save_start;
12731 
12732 			thflags = tcp_reass(tp, th, &temp, &tlen, m);
12733 			tp->t_flags |= TF_ACKNOW;
12734 			if (tp->t_flags & TF_WAKESOR) {
12735 				tp->t_flags &= ~TF_WAKESOR;
12736 				/* NB: sorwakeup_locked() does an implicit unlock. */
12737 				sorwakeup_locked(so);
12738 			}
12739 		}
12740 		if ((tp->t_flags & TF_SACK_PERMIT) &&
12741 		    (save_tlen > 0) &&
12742 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
12743 			if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
12744 				/*
12745 				 * DSACK actually handled in the fastpath
12746 				 * above.
12747 				 */
12748 				tcp_update_sack_list(tp, save_start,
12749 				    save_start + save_tlen);
12750 			} else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
12751 				if ((tp->rcv_numsacks >= 1) &&
12752 				    (tp->sackblks[0].end == save_start)) {
12753 					/*
12754 					 * Partial overlap, recorded at todrop
12755 					 * above.
12756 					 */
12757 					tcp_update_sack_list(tp,
12758 					    tp->sackblks[0].start,
12759 					    tp->sackblks[0].end);
12760 				} else {
12761 					tcp_update_dsack_list(tp, save_start,
12762 					    save_start + save_tlen);
12763 				}
12764 			} else if (tlen >= save_tlen) {
12765 				/* Update of sackblks. */
12766 				tcp_update_dsack_list(tp, save_start,
12767 				    save_start + save_tlen);
12768 			} else if (tlen > 0) {
12769 				tcp_update_dsack_list(tp, save_start,
12770 				    save_start + tlen);
12771 			}
12772 		}
12773 	} else {
12774 		m_freem(m);
12775 		thflags &= ~TH_FIN;
12776 	}
12777 
12778 	/*
12779 	 * If FIN is received ACK the FIN and let the user know that the
12780 	 * connection is closing.
12781 	 */
12782 	if (thflags & TH_FIN) {
12783 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
12784 			/* The socket upcall is handled by socantrcvmore. */
12785 			socantrcvmore(so);
12786 			/*
12787 			 * If connection is half-synchronized (ie NEEDSYN
12788 			 * flag on) then delay ACK, so it may be piggybacked
12789 			 * when SYN is sent. Otherwise, since we received a
12790 			 * FIN then no more input can be expected, send ACK
12791 			 * now.
12792 			 */
12793 			if (tp->t_flags & TF_NEEDSYN) {
12794 				rack_timer_cancel(tp, rack,
12795 				    rack->r_ctl.rc_rcvtime, __LINE__);
12796 				tp->t_flags |= TF_DELACK;
12797 			} else {
12798 				tp->t_flags |= TF_ACKNOW;
12799 			}
12800 			tp->rcv_nxt++;
12801 		}
12802 		switch (tp->t_state) {
12803 			/*
12804 			 * In SYN_RECEIVED and ESTABLISHED STATES enter the
12805 			 * CLOSE_WAIT state.
12806 			 */
12807 		case TCPS_SYN_RECEIVED:
12808 			tp->t_starttime = ticks;
12809 			/* FALLTHROUGH */
12810 		case TCPS_ESTABLISHED:
12811 			rack_timer_cancel(tp, rack,
12812 			    rack->r_ctl.rc_rcvtime, __LINE__);
12813 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
12814 			break;
12815 
12816 			/*
12817 			 * If still in FIN_WAIT_1 STATE FIN has not been
12818 			 * acked so enter the CLOSING state.
12819 			 */
12820 		case TCPS_FIN_WAIT_1:
12821 			rack_timer_cancel(tp, rack,
12822 			    rack->r_ctl.rc_rcvtime, __LINE__);
12823 			tcp_state_change(tp, TCPS_CLOSING);
12824 			break;
12825 
12826 			/*
12827 			 * In FIN_WAIT_2 state enter the TIME_WAIT state,
12828 			 * starting the time-wait timer, turning off the
12829 			 * other standard timers.
12830 			 */
12831 		case TCPS_FIN_WAIT_2:
12832 			rack_timer_cancel(tp, rack,
12833 			    rack->r_ctl.rc_rcvtime, __LINE__);
12834 			tcp_twstart(tp);
12835 			return (1);
12836 		}
12837 	}
12838 	/*
12839 	 * Return any desired output.
12840 	 */
12841 	if ((tp->t_flags & TF_ACKNOW) ||
12842 	    (sbavail(&so->so_snd) > (tp->snd_max - tp->snd_una))) {
12843 		rack->r_wanted_output = 1;
12844 	}
12845 	return (0);
12846 }
12847 
12848 /*
12849  * Here nothing is really faster, its just that we
12850  * have broken out the fast-data path also just like
12851  * the fast-ack.
12852  */
12853 static int
12854 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so,
12855     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12856     uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos)
12857 {
12858 	int32_t nsegs;
12859 	int32_t newsize = 0;	/* automatic sockbuf scaling */
12860 	struct tcp_rack *rack;
12861 #ifdef NETFLIX_SB_LIMITS
12862 	u_int mcnt, appended;
12863 #endif
12864 
12865 	/*
12866 	 * If last ACK falls within this segment's sequence numbers, record
12867 	 * the timestamp. NOTE that the test is modified according to the
12868 	 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
12869 	 */
12870 	if (__predict_false(th->th_seq != tp->rcv_nxt)) {
12871 		return (0);
12872 	}
12873 	if (__predict_false(tp->snd_nxt != tp->snd_max)) {
12874 		return (0);
12875 	}
12876 	if (tiwin && tiwin != tp->snd_wnd) {
12877 		return (0);
12878 	}
12879 	if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) {
12880 		return (0);
12881 	}
12882 	if (__predict_false((to->to_flags & TOF_TS) &&
12883 	    (TSTMP_LT(to->to_tsval, tp->ts_recent)))) {
12884 		return (0);
12885 	}
12886 	if (__predict_false((th->th_ack != tp->snd_una))) {
12887 		return (0);
12888 	}
12889 	if (__predict_false(tlen > sbspace(&so->so_rcv))) {
12890 		return (0);
12891 	}
12892 	if ((to->to_flags & TOF_TS) != 0 &&
12893 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
12894 		tp->ts_recent_age = tcp_ts_getticks();
12895 		tp->ts_recent = to->to_tsval;
12896 	}
12897 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12898 	/*
12899 	 * This is a pure, in-sequence data packet with nothing on the
12900 	 * reassembly queue and we have enough buffer space to take it.
12901 	 */
12902 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
12903 
12904 #ifdef NETFLIX_SB_LIMITS
12905 	if (so->so_rcv.sb_shlim) {
12906 		mcnt = m_memcnt(m);
12907 		appended = 0;
12908 		if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
12909 		    CFO_NOSLEEP, NULL) == false) {
12910 			counter_u64_add(tcp_sb_shlim_fails, 1);
12911 			m_freem(m);
12912 			return (1);
12913 		}
12914 	}
12915 #endif
12916 	/* Clean receiver SACK report if present */
12917 	if (tp->rcv_numsacks)
12918 		tcp_clean_sackreport(tp);
12919 	KMOD_TCPSTAT_INC(tcps_preddat);
12920 	tp->rcv_nxt += tlen;
12921 	if (tlen &&
12922 	    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
12923 	    (tp->t_fbyte_in == 0)) {
12924 		tp->t_fbyte_in = ticks;
12925 		if (tp->t_fbyte_in == 0)
12926 			tp->t_fbyte_in = 1;
12927 		if (tp->t_fbyte_out && tp->t_fbyte_in)
12928 			tp->t_flags2 |= TF2_FBYTES_COMPLETE;
12929 	}
12930 	/*
12931 	 * Pull snd_wl1 up to prevent seq wrap relative to th_seq.
12932 	 */
12933 	tp->snd_wl1 = th->th_seq;
12934 	/*
12935 	 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt.
12936 	 */
12937 	tp->rcv_up = tp->rcv_nxt;
12938 	KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs);
12939 	KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
12940 	newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
12941 
12942 	/* Add data to socket buffer. */
12943 	SOCKBUF_LOCK(&so->so_rcv);
12944 	if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
12945 		m_freem(m);
12946 	} else {
12947 		/*
12948 		 * Set new socket buffer size. Give up when limit is
12949 		 * reached.
12950 		 */
12951 		if (newsize)
12952 			if (!sbreserve_locked(so, SO_RCV, newsize, NULL))
12953 				so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
12954 		m_adj(m, drop_hdrlen);	/* delayed header drop */
12955 #ifdef NETFLIX_SB_LIMITS
12956 		appended =
12957 #endif
12958 			sbappendstream_locked(&so->so_rcv, m, 0);
12959 		ctf_calc_rwin(so, tp);
12960 	}
12961 	rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1);
12962 	/* NB: sorwakeup_locked() does an implicit unlock. */
12963 	sorwakeup_locked(so);
12964 #ifdef NETFLIX_SB_LIMITS
12965 	if (so->so_rcv.sb_shlim && mcnt != appended)
12966 		counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended);
12967 #endif
12968 	rack_handle_delayed_ack(tp, rack, tlen, 0);
12969 	if (tp->snd_una == tp->snd_max)
12970 		sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
12971 	return (1);
12972 }
12973 
12974 /*
12975  * This subfunction is used to try to highly optimize the
12976  * fast path. We again allow window updates that are
12977  * in sequence to remain in the fast-path. We also add
12978  * in the __predict's to attempt to help the compiler.
12979  * Note that if we return a 0, then we can *not* process
12980  * it and the caller should push the packet into the
12981  * slow-path.
12982  */
12983 static int
12984 rack_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
12985     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12986     uint32_t tiwin, int32_t nxt_pkt, uint32_t cts)
12987 {
12988 	int32_t acked;
12989 	int32_t nsegs;
12990 	int32_t under_pacing = 0;
12991 	struct tcp_rack *rack;
12992 
12993 	if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
12994 		/* Old ack, behind (or duplicate to) the last one rcv'd */
12995 		return (0);
12996 	}
12997 	if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) {
12998 		/* Above what we have sent? */
12999 		return (0);
13000 	}
13001 	if (__predict_false(tp->snd_nxt != tp->snd_max)) {
13002 		/* We are retransmitting */
13003 		return (0);
13004 	}
13005 	if (__predict_false(tiwin == 0)) {
13006 		/* zero window */
13007 		return (0);
13008 	}
13009 	if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) {
13010 		/* We need a SYN or a FIN, unlikely.. */
13011 		return (0);
13012 	}
13013 	if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) {
13014 		/* Timestamp is behind .. old ack with seq wrap? */
13015 		return (0);
13016 	}
13017 	if (__predict_false(IN_RECOVERY(tp->t_flags))) {
13018 		/* Still recovering */
13019 		return (0);
13020 	}
13021 	rack = (struct tcp_rack *)tp->t_fb_ptr;
13022 	if (rack->r_ctl.rc_sacked) {
13023 		/* We have sack holes on our scoreboard */
13024 		return (0);
13025 	}
13026 	/* Ok if we reach here, we can process a fast-ack */
13027 	if (rack->gp_ready &&
13028 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
13029 		under_pacing = 1;
13030 	}
13031 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
13032 	rack_log_ack(tp, to, th, 0, 0, NULL, NULL);
13033 	/* Did the window get updated? */
13034 	if (tiwin != tp->snd_wnd) {
13035 		tp->snd_wnd = tiwin;
13036 		rack_validate_fo_sendwin_up(tp, rack);
13037 		tp->snd_wl1 = th->th_seq;
13038 		if (tp->snd_wnd > tp->max_sndwnd)
13039 			tp->max_sndwnd = tp->snd_wnd;
13040 	}
13041 	/* Do we exit persists? */
13042 	if ((rack->rc_in_persist != 0) &&
13043 	    (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
13044 			       rack->r_ctl.rc_pace_min_segs))) {
13045 		rack_exit_persist(tp, rack, cts);
13046 	}
13047 	/* Do we enter persists? */
13048 	if ((rack->rc_in_persist == 0) &&
13049 	    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
13050 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
13051 	    ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) &&
13052 	    sbavail(&tptosocket(tp)->so_snd) &&
13053 	    (sbavail(&tptosocket(tp)->so_snd) > tp->snd_wnd)) {
13054 		/*
13055 		 * Here the rwnd is less than
13056 		 * the pacing size, we are established,
13057 		 * nothing is outstanding, and there is
13058 		 * data to send. Enter persists.
13059 		 */
13060 		rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, th->th_ack);
13061 	}
13062 	/*
13063 	 * If last ACK falls within this segment's sequence numbers, record
13064 	 * the timestamp. NOTE that the test is modified according to the
13065 	 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
13066 	 */
13067 	if ((to->to_flags & TOF_TS) != 0 &&
13068 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
13069 		tp->ts_recent_age = tcp_ts_getticks();
13070 		tp->ts_recent = to->to_tsval;
13071 	}
13072 	/*
13073 	 * This is a pure ack for outstanding data.
13074 	 */
13075 	KMOD_TCPSTAT_INC(tcps_predack);
13076 
13077 	/*
13078 	 * "bad retransmit" recovery.
13079 	 */
13080 	if ((tp->t_flags & TF_PREVVALID) &&
13081 	    ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
13082 		tp->t_flags &= ~TF_PREVVALID;
13083 		if (tp->t_rxtshift == 1 &&
13084 		    (int)(ticks - tp->t_badrxtwin) < 0)
13085 			rack_cong_signal(tp, CC_RTO_ERR, th->th_ack, __LINE__);
13086 	}
13087 	/*
13088 	 * Recalculate the transmit timer / rtt.
13089 	 *
13090 	 * Some boxes send broken timestamp replies during the SYN+ACK
13091 	 * phase, ignore timestamps of 0 or we could calculate a huge RTT
13092 	 * and blow up the retransmit timer.
13093 	 */
13094 	acked = BYTES_THIS_ACK(tp, th);
13095 
13096 #ifdef TCP_HHOOK
13097 	/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
13098 	hhook_run_tcp_est_in(tp, th, to);
13099 #endif
13100 	KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
13101 	KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
13102 	if (acked) {
13103 		struct mbuf *mfree;
13104 
13105 		rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, 0);
13106 		SOCKBUF_LOCK(&so->so_snd);
13107 		mfree = sbcut_locked(&so->so_snd, acked);
13108 		tp->snd_una = th->th_ack;
13109 		/* Note we want to hold the sb lock through the sendmap adjust */
13110 		rack_adjust_sendmap_head(rack, &so->so_snd);
13111 		/* Wake up the socket if we have room to write more */
13112 		rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
13113 		sowwakeup_locked(so);
13114 		m_freem(mfree);
13115 		tp->t_rxtshift = 0;
13116 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
13117 			      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
13118 		rack->rc_tlp_in_progress = 0;
13119 		rack->r_ctl.rc_tlp_cnt_out = 0;
13120 		/*
13121 		 * If it is the RXT timer we want to
13122 		 * stop it, so we can restart a TLP.
13123 		 */
13124 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
13125 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
13126 
13127 #ifdef TCP_REQUEST_TRK
13128 		rack_req_check_for_comp(rack, th->th_ack);
13129 #endif
13130 	}
13131 	/*
13132 	 * Let the congestion control algorithm update congestion control
13133 	 * related information. This typically means increasing the
13134 	 * congestion window.
13135 	 */
13136 	if (tp->snd_wnd < ctf_outstanding(tp)) {
13137 		/* The peer collapsed the window */
13138 		rack_collapsed_window(rack, ctf_outstanding(tp), th->th_ack, __LINE__);
13139 	} else if (rack->rc_has_collapsed)
13140 		rack_un_collapse_window(rack, __LINE__);
13141 	if ((rack->r_collapse_point_valid) &&
13142 	    (SEQ_GT(tp->snd_una, rack->r_ctl.high_collapse_point)))
13143 		rack->r_collapse_point_valid = 0;
13144 	/*
13145 	 * Pull snd_wl2 up to prevent seq wrap relative to th_ack.
13146 	 */
13147 	tp->snd_wl2 = th->th_ack;
13148 	tp->t_dupacks = 0;
13149 	m_freem(m);
13150 	/* ND6_HINT(tp);	 *//* Some progress has been made. */
13151 
13152 	/*
13153 	 * If all outstanding data are acked, stop retransmit timer,
13154 	 * otherwise restart timer using current (possibly backed-off)
13155 	 * value. If process is waiting for space, wakeup/selwakeup/signal.
13156 	 * If data are ready to send, let tcp_output decide between more
13157 	 * output or persist.
13158 	 */
13159 	if (under_pacing &&
13160 	    (rack->use_fixed_rate == 0) &&
13161 	    (rack->in_probe_rtt == 0) &&
13162 	    rack->rc_gp_dyn_mul &&
13163 	    rack->rc_always_pace) {
13164 		/* Check if we are dragging bottom */
13165 		rack_check_bottom_drag(tp, rack, so);
13166 	}
13167 	if (tp->snd_una == tp->snd_max) {
13168 		tp->t_flags &= ~TF_PREVVALID;
13169 		rack->r_ctl.retran_during_recovery = 0;
13170 		rack->rc_suspicious = 0;
13171 		rack->r_ctl.dsack_byte_cnt = 0;
13172 		rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
13173 		if (rack->r_ctl.rc_went_idle_time == 0)
13174 			rack->r_ctl.rc_went_idle_time = 1;
13175 		rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
13176 		if (sbavail(&tptosocket(tp)->so_snd) == 0)
13177 			tp->t_acktime = 0;
13178 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
13179 	}
13180 	if (acked && rack->r_fast_output)
13181 		rack_gain_for_fastoutput(rack, tp, so, (uint32_t)acked);
13182 	if (sbavail(&so->so_snd)) {
13183 		rack->r_wanted_output = 1;
13184 	}
13185 	return (1);
13186 }
13187 
13188 /*
13189  * Return value of 1, the TCB is unlocked and most
13190  * likely gone, return value of 0, the TCP is still
13191  * locked.
13192  */
13193 static int
13194 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so,
13195     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13196     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13197 {
13198 	int32_t ret_val = 0;
13199 	int32_t todrop;
13200 	int32_t ourfinisacked = 0;
13201 	struct tcp_rack *rack;
13202 
13203 	INP_WLOCK_ASSERT(tptoinpcb(tp));
13204 
13205 	ctf_calc_rwin(so, tp);
13206 	/*
13207 	 * If the state is SYN_SENT: if seg contains an ACK, but not for our
13208 	 * SYN, drop the input. if seg contains a RST, then drop the
13209 	 * connection. if seg does not contain SYN, then drop it. Otherwise
13210 	 * this is an acceptable SYN segment initialize tp->rcv_nxt and
13211 	 * tp->irs if seg contains ack then advance tp->snd_una if seg
13212 	 * contains an ECE and ECN support is enabled, the stream is ECN
13213 	 * capable. if SYN has been acked change to ESTABLISHED else
13214 	 * SYN_RCVD state arrange for segment to be acked (eventually)
13215 	 * continue processing rest of data/controls.
13216 	 */
13217 	if ((thflags & TH_ACK) &&
13218 	    (SEQ_LEQ(th->th_ack, tp->iss) ||
13219 	    SEQ_GT(th->th_ack, tp->snd_max))) {
13220 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
13221 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13222 		return (1);
13223 	}
13224 	if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) {
13225 		TCP_PROBE5(connect__refused, NULL, tp,
13226 		    mtod(m, const char *), tp, th);
13227 		tp = tcp_drop(tp, ECONNREFUSED);
13228 		ctf_do_drop(m, tp);
13229 		return (1);
13230 	}
13231 	if (thflags & TH_RST) {
13232 		ctf_do_drop(m, tp);
13233 		return (1);
13234 	}
13235 	if (!(thflags & TH_SYN)) {
13236 		ctf_do_drop(m, tp);
13237 		return (1);
13238 	}
13239 	tp->irs = th->th_seq;
13240 	tcp_rcvseqinit(tp);
13241 	rack = (struct tcp_rack *)tp->t_fb_ptr;
13242 	if (thflags & TH_ACK) {
13243 		int tfo_partial = 0;
13244 
13245 		KMOD_TCPSTAT_INC(tcps_connects);
13246 		soisconnected(so);
13247 #ifdef MAC
13248 		mac_socketpeer_set_from_mbuf(m, so);
13249 #endif
13250 		/* Do window scaling on this connection? */
13251 		if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
13252 		    (TF_RCVD_SCALE | TF_REQ_SCALE)) {
13253 			tp->rcv_scale = tp->request_r_scale;
13254 		}
13255 		tp->rcv_adv += min(tp->rcv_wnd,
13256 		    TCP_MAXWIN << tp->rcv_scale);
13257 		/*
13258 		 * If not all the data that was sent in the TFO SYN
13259 		 * has been acked, resend the remainder right away.
13260 		 */
13261 		if (IS_FASTOPEN(tp->t_flags) &&
13262 		    (tp->snd_una != tp->snd_max)) {
13263 			tp->snd_nxt = th->th_ack;
13264 			tfo_partial = 1;
13265 		}
13266 		/*
13267 		 * If there's data, delay ACK; if there's also a FIN ACKNOW
13268 		 * will be turned on later.
13269 		 */
13270 		if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial) {
13271 			rack_timer_cancel(tp, rack,
13272 					  rack->r_ctl.rc_rcvtime, __LINE__);
13273 			tp->t_flags |= TF_DELACK;
13274 		} else {
13275 			rack->r_wanted_output = 1;
13276 			tp->t_flags |= TF_ACKNOW;
13277 		}
13278 
13279 		tcp_ecn_input_syn_sent(tp, thflags, iptos);
13280 
13281 		if (SEQ_GT(th->th_ack, tp->snd_una)) {
13282 			/*
13283 			 * We advance snd_una for the
13284 			 * fast open case. If th_ack is
13285 			 * acknowledging data beyond
13286 			 * snd_una we can't just call
13287 			 * ack-processing since the
13288 			 * data stream in our send-map
13289 			 * will start at snd_una + 1 (one
13290 			 * beyond the SYN). If its just
13291 			 * equal we don't need to do that
13292 			 * and there is no send_map.
13293 			 */
13294 			tp->snd_una++;
13295 		}
13296 		/*
13297 		 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions:
13298 		 * SYN_SENT  --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1
13299 		 */
13300 		tp->t_starttime = ticks;
13301 		if (tp->t_flags & TF_NEEDFIN) {
13302 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
13303 			tp->t_flags &= ~TF_NEEDFIN;
13304 			thflags &= ~TH_SYN;
13305 		} else {
13306 			tcp_state_change(tp, TCPS_ESTABLISHED);
13307 			TCP_PROBE5(connect__established, NULL, tp,
13308 			    mtod(m, const char *), tp, th);
13309 			rack_cc_conn_init(tp);
13310 		}
13311 	} else {
13312 		/*
13313 		 * Received initial SYN in SYN-SENT[*] state => simultaneous
13314 		 * open.  If segment contains CC option and there is a
13315 		 * cached CC, apply TAO test. If it succeeds, connection is *
13316 		 * half-synchronized. Otherwise, do 3-way handshake:
13317 		 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If
13318 		 * there was no CC option, clear cached CC value.
13319 		 */
13320 		tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN);
13321 		tcp_state_change(tp, TCPS_SYN_RECEIVED);
13322 	}
13323 	/*
13324 	 * Advance th->th_seq to correspond to first data byte. If data,
13325 	 * trim to stay within window, dropping FIN if necessary.
13326 	 */
13327 	th->th_seq++;
13328 	if (tlen > tp->rcv_wnd) {
13329 		todrop = tlen - tp->rcv_wnd;
13330 		m_adj(m, -todrop);
13331 		tlen = tp->rcv_wnd;
13332 		thflags &= ~TH_FIN;
13333 		KMOD_TCPSTAT_INC(tcps_rcvpackafterwin);
13334 		KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
13335 	}
13336 	tp->snd_wl1 = th->th_seq - 1;
13337 	tp->rcv_up = th->th_seq;
13338 	/*
13339 	 * Client side of transaction: already sent SYN and data. If the
13340 	 * remote host used T/TCP to validate the SYN, our data will be
13341 	 * ACK'd; if so, enter normal data segment processing in the middle
13342 	 * of step 5, ack processing. Otherwise, goto step 6.
13343 	 */
13344 	if (thflags & TH_ACK) {
13345 		/* For syn-sent we need to possibly update the rtt */
13346 		if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) {
13347 			uint32_t t, mcts;
13348 
13349 			mcts = tcp_ts_getticks();
13350 			t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC;
13351 			if (!tp->t_rttlow || tp->t_rttlow > t)
13352 				tp->t_rttlow = t;
13353 			rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 4);
13354 			tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2);
13355 			tcp_rack_xmit_timer_commit(rack, tp);
13356 		}
13357 		if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val))
13358 			return (ret_val);
13359 		/* We may have changed to FIN_WAIT_1 above */
13360 		if (tp->t_state == TCPS_FIN_WAIT_1) {
13361 			/*
13362 			 * In FIN_WAIT_1 STATE in addition to the processing
13363 			 * for the ESTABLISHED state if our FIN is now
13364 			 * acknowledged then enter FIN_WAIT_2.
13365 			 */
13366 			if (ourfinisacked) {
13367 				/*
13368 				 * If we can't receive any more data, then
13369 				 * closing user can proceed. Starting the
13370 				 * timer is contrary to the specification,
13371 				 * but if we don't get a FIN we'll hang
13372 				 * forever.
13373 				 *
13374 				 * XXXjl: we should release the tp also, and
13375 				 * use a compressed state.
13376 				 */
13377 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
13378 					soisdisconnected(so);
13379 					tcp_timer_activate(tp, TT_2MSL,
13380 					    (tcp_fast_finwait2_recycle ?
13381 					    tcp_finwait2_timeout :
13382 					    TP_MAXIDLE(tp)));
13383 				}
13384 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
13385 			}
13386 		}
13387 	}
13388 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13389 	   tiwin, thflags, nxt_pkt));
13390 }
13391 
13392 /*
13393  * Return value of 1, the TCB is unlocked and most
13394  * likely gone, return value of 0, the TCP is still
13395  * locked.
13396  */
13397 static int
13398 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so,
13399     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13400     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13401 {
13402 	struct tcp_rack *rack;
13403 	int32_t ret_val = 0;
13404 	int32_t ourfinisacked = 0;
13405 
13406 	rack = (struct tcp_rack *)tp->t_fb_ptr;
13407 	ctf_calc_rwin(so, tp);
13408 	if ((thflags & TH_RST) ||
13409 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
13410 		return (__ctf_process_rst(m, th, so, tp,
13411 					  &rack->r_ctl.challenge_ack_ts,
13412 					  &rack->r_ctl.challenge_ack_cnt));
13413 	if ((thflags & TH_ACK) &&
13414 	    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
13415 	    SEQ_GT(th->th_ack, tp->snd_max))) {
13416 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
13417 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13418 		return (1);
13419 	}
13420 	if (IS_FASTOPEN(tp->t_flags)) {
13421 		/*
13422 		 * When a TFO connection is in SYN_RECEIVED, the
13423 		 * only valid packets are the initial SYN, a
13424 		 * retransmit/copy of the initial SYN (possibly with
13425 		 * a subset of the original data), a valid ACK, a
13426 		 * FIN, or a RST.
13427 		 */
13428 		if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) {
13429 			tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
13430 			ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13431 			return (1);
13432 		} else if (thflags & TH_SYN) {
13433 			/* non-initial SYN is ignored */
13434 			if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) ||
13435 			    (rack->r_ctl.rc_hpts_flags & PACE_TMR_TLP) ||
13436 			    (rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) {
13437 				ctf_do_drop(m, NULL);
13438 				return (0);
13439 			}
13440 		} else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) {
13441 			ctf_do_drop(m, NULL);
13442 			return (0);
13443 		}
13444 	}
13445 
13446 	/*
13447 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13448 	 * it's less than ts_recent, drop it.
13449 	 */
13450 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13451 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13452 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13453 			return (ret_val);
13454 	}
13455 	/*
13456 	 * In the SYN-RECEIVED state, validate that the packet belongs to
13457 	 * this connection before trimming the data to fit the receive
13458 	 * window.  Check the sequence number versus IRS since we know the
13459 	 * sequence numbers haven't wrapped.  This is a partial fix for the
13460 	 * "LAND" DoS attack.
13461 	 */
13462 	if (SEQ_LT(th->th_seq, tp->irs)) {
13463 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
13464 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13465 		return (1);
13466 	}
13467 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
13468 			      &rack->r_ctl.challenge_ack_ts,
13469 			      &rack->r_ctl.challenge_ack_cnt)) {
13470 		return (ret_val);
13471 	}
13472 	/*
13473 	 * If last ACK falls within this segment's sequence numbers, record
13474 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
13475 	 * from the latest proposal of the tcplw@cray.com list (Braden
13476 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
13477 	 * with our earlier PAWS tests, so this check should be solely
13478 	 * predicated on the sequence space of this segment. 3) That we
13479 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13480 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13481 	 * SEG.Len, This modified check allows us to overcome RFC1323's
13482 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13483 	 * p.869. In such cases, we can still calculate the RTT correctly
13484 	 * when RCV.NXT == Last.ACK.Sent.
13485 	 */
13486 	if ((to->to_flags & TOF_TS) != 0 &&
13487 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13488 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13489 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13490 		tp->ts_recent_age = tcp_ts_getticks();
13491 		tp->ts_recent = to->to_tsval;
13492 	}
13493 	tp->snd_wnd = tiwin;
13494 	rack_validate_fo_sendwin_up(tp, rack);
13495 	/*
13496 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
13497 	 * is on (half-synchronized state), then queue data for later
13498 	 * processing; else drop segment and return.
13499 	 */
13500 	if ((thflags & TH_ACK) == 0) {
13501 		if (IS_FASTOPEN(tp->t_flags)) {
13502 			rack_cc_conn_init(tp);
13503 		}
13504 		return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13505 		    tiwin, thflags, nxt_pkt));
13506 	}
13507 	KMOD_TCPSTAT_INC(tcps_connects);
13508 	if (tp->t_flags & TF_SONOTCONN) {
13509 		tp->t_flags &= ~TF_SONOTCONN;
13510 		soisconnected(so);
13511 	}
13512 	/* Do window scaling? */
13513 	if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
13514 	    (TF_RCVD_SCALE | TF_REQ_SCALE)) {
13515 		tp->rcv_scale = tp->request_r_scale;
13516 	}
13517 	/*
13518 	 * Make transitions: SYN-RECEIVED  -> ESTABLISHED SYN-RECEIVED* ->
13519 	 * FIN-WAIT-1
13520 	 */
13521 	tp->t_starttime = ticks;
13522 	if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
13523 		tcp_fastopen_decrement_counter(tp->t_tfo_pending);
13524 		tp->t_tfo_pending = NULL;
13525 	}
13526 	if (tp->t_flags & TF_NEEDFIN) {
13527 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
13528 		tp->t_flags &= ~TF_NEEDFIN;
13529 	} else {
13530 		tcp_state_change(tp, TCPS_ESTABLISHED);
13531 		TCP_PROBE5(accept__established, NULL, tp,
13532 		    mtod(m, const char *), tp, th);
13533 		/*
13534 		 * TFO connections call cc_conn_init() during SYN
13535 		 * processing.  Calling it again here for such connections
13536 		 * is not harmless as it would undo the snd_cwnd reduction
13537 		 * that occurs when a TFO SYN|ACK is retransmitted.
13538 		 */
13539 		if (!IS_FASTOPEN(tp->t_flags))
13540 			rack_cc_conn_init(tp);
13541 	}
13542 	/*
13543 	 * Account for the ACK of our SYN prior to
13544 	 * regular ACK processing below, except for
13545 	 * simultaneous SYN, which is handled later.
13546 	 */
13547 	if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
13548 		tp->snd_una++;
13549 	/*
13550 	 * If segment contains data or ACK, will call tcp_reass() later; if
13551 	 * not, do so now to pass queued data to user.
13552 	 */
13553 	if (tlen == 0 && (thflags & TH_FIN) == 0) {
13554 		(void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
13555 		    (struct mbuf *)0);
13556 		if (tp->t_flags & TF_WAKESOR) {
13557 			tp->t_flags &= ~TF_WAKESOR;
13558 			/* NB: sorwakeup_locked() does an implicit unlock. */
13559 			sorwakeup_locked(so);
13560 		}
13561 	}
13562 	tp->snd_wl1 = th->th_seq - 1;
13563 	/* For syn-recv we need to possibly update the rtt */
13564 	if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) {
13565 		uint32_t t, mcts;
13566 
13567 		mcts = tcp_ts_getticks();
13568 		t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC;
13569 		if (!tp->t_rttlow || tp->t_rttlow > t)
13570 			tp->t_rttlow = t;
13571 		rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 5);
13572 		tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2);
13573 		tcp_rack_xmit_timer_commit(rack, tp);
13574 	}
13575 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
13576 		return (ret_val);
13577 	}
13578 	if (tp->t_state == TCPS_FIN_WAIT_1) {
13579 		/* We could have went to FIN_WAIT_1 (or EST) above */
13580 		/*
13581 		 * In FIN_WAIT_1 STATE in addition to the processing for the
13582 		 * ESTABLISHED state if our FIN is now acknowledged then
13583 		 * enter FIN_WAIT_2.
13584 		 */
13585 		if (ourfinisacked) {
13586 			/*
13587 			 * If we can't receive any more data, then closing
13588 			 * user can proceed. Starting the timer is contrary
13589 			 * to the specification, but if we don't get a FIN
13590 			 * we'll hang forever.
13591 			 *
13592 			 * XXXjl: we should release the tp also, and use a
13593 			 * compressed state.
13594 			 */
13595 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
13596 				soisdisconnected(so);
13597 				tcp_timer_activate(tp, TT_2MSL,
13598 				    (tcp_fast_finwait2_recycle ?
13599 				    tcp_finwait2_timeout :
13600 				    TP_MAXIDLE(tp)));
13601 			}
13602 			tcp_state_change(tp, TCPS_FIN_WAIT_2);
13603 		}
13604 	}
13605 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13606 	    tiwin, thflags, nxt_pkt));
13607 }
13608 
13609 /*
13610  * Return value of 1, the TCB is unlocked and most
13611  * likely gone, return value of 0, the TCP is still
13612  * locked.
13613  */
13614 static int
13615 rack_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so,
13616     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13617     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13618 {
13619 	int32_t ret_val = 0;
13620 	struct tcp_rack *rack;
13621 
13622 	/*
13623 	 * Header prediction: check for the two common cases of a
13624 	 * uni-directional data xfer.  If the packet has no control flags,
13625 	 * is in-sequence, the window didn't change and we're not
13626 	 * retransmitting, it's a candidate.  If the length is zero and the
13627 	 * ack moved forward, we're the sender side of the xfer.  Just free
13628 	 * the data acked & wake any higher level process that was blocked
13629 	 * waiting for space.  If the length is non-zero and the ack didn't
13630 	 * move, we're the receiver side.  If we're getting packets in-order
13631 	 * (the reassembly queue is empty), add the data toc The socket
13632 	 * buffer and note that we need a delayed ack. Make sure that the
13633 	 * hidden state-flags are also off. Since we check for
13634 	 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN.
13635 	 */
13636 	rack = (struct tcp_rack *)tp->t_fb_ptr;
13637 	if (__predict_true(((to->to_flags & TOF_SACK) == 0)) &&
13638 	    __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) == TH_ACK) &&
13639 	    __predict_true(SEGQ_EMPTY(tp)) &&
13640 	    __predict_true(th->th_seq == tp->rcv_nxt)) {
13641 		if (tlen == 0) {
13642 			if (rack_fastack(m, th, so, tp, to, drop_hdrlen, tlen,
13643 			    tiwin, nxt_pkt, rack->r_ctl.rc_rcvtime)) {
13644 				return (0);
13645 			}
13646 		} else {
13647 			if (rack_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen,
13648 			    tiwin, nxt_pkt, iptos)) {
13649 				return (0);
13650 			}
13651 		}
13652 	}
13653 	ctf_calc_rwin(so, tp);
13654 
13655 	if ((thflags & TH_RST) ||
13656 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
13657 		return (__ctf_process_rst(m, th, so, tp,
13658 					  &rack->r_ctl.challenge_ack_ts,
13659 					  &rack->r_ctl.challenge_ack_cnt));
13660 
13661 	/*
13662 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13663 	 * synchronized state.
13664 	 */
13665 	if (thflags & TH_SYN) {
13666 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13667 		return (ret_val);
13668 	}
13669 	/*
13670 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13671 	 * it's less than ts_recent, drop it.
13672 	 */
13673 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13674 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13675 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13676 			return (ret_val);
13677 	}
13678 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
13679 			      &rack->r_ctl.challenge_ack_ts,
13680 			      &rack->r_ctl.challenge_ack_cnt)) {
13681 		return (ret_val);
13682 	}
13683 	/*
13684 	 * If last ACK falls within this segment's sequence numbers, record
13685 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
13686 	 * from the latest proposal of the tcplw@cray.com list (Braden
13687 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
13688 	 * with our earlier PAWS tests, so this check should be solely
13689 	 * predicated on the sequence space of this segment. 3) That we
13690 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13691 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13692 	 * SEG.Len, This modified check allows us to overcome RFC1323's
13693 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13694 	 * p.869. In such cases, we can still calculate the RTT correctly
13695 	 * when RCV.NXT == Last.ACK.Sent.
13696 	 */
13697 	if ((to->to_flags & TOF_TS) != 0 &&
13698 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13699 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13700 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13701 		tp->ts_recent_age = tcp_ts_getticks();
13702 		tp->ts_recent = to->to_tsval;
13703 	}
13704 	/*
13705 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
13706 	 * is on (half-synchronized state), then queue data for later
13707 	 * processing; else drop segment and return.
13708 	 */
13709 	if ((thflags & TH_ACK) == 0) {
13710 		if (tp->t_flags & TF_NEEDSYN) {
13711 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13712 			    tiwin, thflags, nxt_pkt));
13713 
13714 		} else if (tp->t_flags & TF_ACKNOW) {
13715 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
13716 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
13717 			return (ret_val);
13718 		} else {
13719 			ctf_do_drop(m, NULL);
13720 			return (0);
13721 		}
13722 	}
13723 	/*
13724 	 * Ack processing.
13725 	 */
13726 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
13727 		return (ret_val);
13728 	}
13729 	if (sbavail(&so->so_snd)) {
13730 		if (ctf_progress_timeout_check(tp, true)) {
13731 			rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
13732 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13733 			return (1);
13734 		}
13735 	}
13736 	/* State changes only happen in rack_process_data() */
13737 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13738 	    tiwin, thflags, nxt_pkt));
13739 }
13740 
13741 /*
13742  * Return value of 1, the TCB is unlocked and most
13743  * likely gone, return value of 0, the TCP is still
13744  * locked.
13745  */
13746 static int
13747 rack_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so,
13748     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13749     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13750 {
13751 	int32_t ret_val = 0;
13752 	struct tcp_rack *rack;
13753 
13754 	rack = (struct tcp_rack *)tp->t_fb_ptr;
13755 	ctf_calc_rwin(so, tp);
13756 	if ((thflags & TH_RST) ||
13757 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
13758 		return (__ctf_process_rst(m, th, so, tp,
13759 					  &rack->r_ctl.challenge_ack_ts,
13760 					  &rack->r_ctl.challenge_ack_cnt));
13761 	/*
13762 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13763 	 * synchronized state.
13764 	 */
13765 	if (thflags & TH_SYN) {
13766 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13767 		return (ret_val);
13768 	}
13769 	/*
13770 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13771 	 * it's less than ts_recent, drop it.
13772 	 */
13773 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13774 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13775 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13776 			return (ret_val);
13777 	}
13778 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
13779 			      &rack->r_ctl.challenge_ack_ts,
13780 			      &rack->r_ctl.challenge_ack_cnt)) {
13781 		return (ret_val);
13782 	}
13783 	/*
13784 	 * If last ACK falls within this segment's sequence numbers, record
13785 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
13786 	 * from the latest proposal of the tcplw@cray.com list (Braden
13787 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
13788 	 * with our earlier PAWS tests, so this check should be solely
13789 	 * predicated on the sequence space of this segment. 3) That we
13790 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13791 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13792 	 * SEG.Len, This modified check allows us to overcome RFC1323's
13793 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13794 	 * p.869. In such cases, we can still calculate the RTT correctly
13795 	 * when RCV.NXT == Last.ACK.Sent.
13796 	 */
13797 	if ((to->to_flags & TOF_TS) != 0 &&
13798 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13799 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13800 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13801 		tp->ts_recent_age = tcp_ts_getticks();
13802 		tp->ts_recent = to->to_tsval;
13803 	}
13804 	/*
13805 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
13806 	 * is on (half-synchronized state), then queue data for later
13807 	 * processing; else drop segment and return.
13808 	 */
13809 	if ((thflags & TH_ACK) == 0) {
13810 		if (tp->t_flags & TF_NEEDSYN) {
13811 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13812 			    tiwin, thflags, nxt_pkt));
13813 
13814 		} else if (tp->t_flags & TF_ACKNOW) {
13815 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
13816 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
13817 			return (ret_val);
13818 		} else {
13819 			ctf_do_drop(m, NULL);
13820 			return (0);
13821 		}
13822 	}
13823 	/*
13824 	 * Ack processing.
13825 	 */
13826 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
13827 		return (ret_val);
13828 	}
13829 	if (sbavail(&so->so_snd)) {
13830 		if (ctf_progress_timeout_check(tp, true)) {
13831 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
13832 						tp, tick, PROGRESS_DROP, __LINE__);
13833 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13834 			return (1);
13835 		}
13836 	}
13837 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13838 	    tiwin, thflags, nxt_pkt));
13839 }
13840 
13841 static int
13842 rack_check_data_after_close(struct mbuf *m,
13843     struct tcpcb *tp, int32_t *tlen, struct tcphdr *th, struct socket *so)
13844 {
13845 	struct tcp_rack *rack;
13846 
13847 	rack = (struct tcp_rack *)tp->t_fb_ptr;
13848 	if (rack->rc_allow_data_af_clo == 0) {
13849 	close_now:
13850 		tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
13851 		/* tcp_close will kill the inp pre-log the Reset */
13852 		tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
13853 		tp = tcp_close(tp);
13854 		KMOD_TCPSTAT_INC(tcps_rcvafterclose);
13855 		ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen));
13856 		return (1);
13857 	}
13858 	if (sbavail(&so->so_snd) == 0)
13859 		goto close_now;
13860 	/* Ok we allow data that is ignored and a followup reset */
13861 	tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
13862 	tp->rcv_nxt = th->th_seq + *tlen;
13863 	tp->t_flags2 |= TF2_DROP_AF_DATA;
13864 	rack->r_wanted_output = 1;
13865 	*tlen = 0;
13866 	return (0);
13867 }
13868 
13869 /*
13870  * Return value of 1, the TCB is unlocked and most
13871  * likely gone, return value of 0, the TCP is still
13872  * locked.
13873  */
13874 static int
13875 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so,
13876     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13877     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13878 {
13879 	int32_t ret_val = 0;
13880 	int32_t ourfinisacked = 0;
13881 	struct tcp_rack *rack;
13882 
13883 	rack = (struct tcp_rack *)tp->t_fb_ptr;
13884 	ctf_calc_rwin(so, tp);
13885 
13886 	if ((thflags & TH_RST) ||
13887 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
13888 		return (__ctf_process_rst(m, th, so, tp,
13889 					  &rack->r_ctl.challenge_ack_ts,
13890 					  &rack->r_ctl.challenge_ack_cnt));
13891 	/*
13892 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13893 	 * synchronized state.
13894 	 */
13895 	if (thflags & TH_SYN) {
13896 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13897 		return (ret_val);
13898 	}
13899 	/*
13900 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13901 	 * it's less than ts_recent, drop it.
13902 	 */
13903 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13904 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13905 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13906 			return (ret_val);
13907 	}
13908 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
13909 			      &rack->r_ctl.challenge_ack_ts,
13910 			      &rack->r_ctl.challenge_ack_cnt)) {
13911 		return (ret_val);
13912 	}
13913 	/*
13914 	 * If new data are received on a connection after the user processes
13915 	 * are gone, then RST the other end.
13916 	 */
13917 	if ((tp->t_flags & TF_CLOSED) && tlen &&
13918 	    rack_check_data_after_close(m, tp, &tlen, th, so))
13919 		return (1);
13920 	/*
13921 	 * If last ACK falls within this segment's sequence numbers, record
13922 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
13923 	 * from the latest proposal of the tcplw@cray.com list (Braden
13924 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
13925 	 * with our earlier PAWS tests, so this check should be solely
13926 	 * predicated on the sequence space of this segment. 3) That we
13927 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13928 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13929 	 * SEG.Len, This modified check allows us to overcome RFC1323's
13930 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13931 	 * p.869. In such cases, we can still calculate the RTT correctly
13932 	 * when RCV.NXT == Last.ACK.Sent.
13933 	 */
13934 	if ((to->to_flags & TOF_TS) != 0 &&
13935 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13936 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13937 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13938 		tp->ts_recent_age = tcp_ts_getticks();
13939 		tp->ts_recent = to->to_tsval;
13940 	}
13941 	/*
13942 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
13943 	 * is on (half-synchronized state), then queue data for later
13944 	 * processing; else drop segment and return.
13945 	 */
13946 	if ((thflags & TH_ACK) == 0) {
13947 		if (tp->t_flags & TF_NEEDSYN) {
13948 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13949 			    tiwin, thflags, nxt_pkt));
13950 		} else if (tp->t_flags & TF_ACKNOW) {
13951 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
13952 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
13953 			return (ret_val);
13954 		} else {
13955 			ctf_do_drop(m, NULL);
13956 			return (0);
13957 		}
13958 	}
13959 	/*
13960 	 * Ack processing.
13961 	 */
13962 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
13963 		return (ret_val);
13964 	}
13965 	if (ourfinisacked) {
13966 		/*
13967 		 * If we can't receive any more data, then closing user can
13968 		 * proceed. Starting the timer is contrary to the
13969 		 * specification, but if we don't get a FIN we'll hang
13970 		 * forever.
13971 		 *
13972 		 * XXXjl: we should release the tp also, and use a
13973 		 * compressed state.
13974 		 */
13975 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
13976 			soisdisconnected(so);
13977 			tcp_timer_activate(tp, TT_2MSL,
13978 			    (tcp_fast_finwait2_recycle ?
13979 			    tcp_finwait2_timeout :
13980 			    TP_MAXIDLE(tp)));
13981 		}
13982 		tcp_state_change(tp, TCPS_FIN_WAIT_2);
13983 	}
13984 	if (sbavail(&so->so_snd)) {
13985 		if (ctf_progress_timeout_check(tp, true)) {
13986 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
13987 						tp, tick, PROGRESS_DROP, __LINE__);
13988 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13989 			return (1);
13990 		}
13991 	}
13992 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13993 	    tiwin, thflags, nxt_pkt));
13994 }
13995 
13996 /*
13997  * Return value of 1, the TCB is unlocked and most
13998  * likely gone, return value of 0, the TCP is still
13999  * locked.
14000  */
14001 static int
14002 rack_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so,
14003     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
14004     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
14005 {
14006 	int32_t ret_val = 0;
14007 	int32_t ourfinisacked = 0;
14008 	struct tcp_rack *rack;
14009 
14010 	rack = (struct tcp_rack *)tp->t_fb_ptr;
14011 	ctf_calc_rwin(so, tp);
14012 
14013 	if ((thflags & TH_RST) ||
14014 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
14015 		return (__ctf_process_rst(m, th, so, tp,
14016 					  &rack->r_ctl.challenge_ack_ts,
14017 					  &rack->r_ctl.challenge_ack_cnt));
14018 	/*
14019 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
14020 	 * synchronized state.
14021 	 */
14022 	if (thflags & TH_SYN) {
14023 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
14024 		return (ret_val);
14025 	}
14026 	/*
14027 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
14028 	 * it's less than ts_recent, drop it.
14029 	 */
14030 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
14031 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
14032 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
14033 			return (ret_val);
14034 	}
14035 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
14036 			      &rack->r_ctl.challenge_ack_ts,
14037 			      &rack->r_ctl.challenge_ack_cnt)) {
14038 		return (ret_val);
14039 	}
14040 	/*
14041 	 * If new data are received on a connection after the user processes
14042 	 * are gone, then RST the other end.
14043 	 */
14044 	if ((tp->t_flags & TF_CLOSED) && tlen &&
14045 	    rack_check_data_after_close(m, tp, &tlen, th, so))
14046 		return (1);
14047 	/*
14048 	 * If last ACK falls within this segment's sequence numbers, record
14049 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
14050 	 * from the latest proposal of the tcplw@cray.com list (Braden
14051 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
14052 	 * with our earlier PAWS tests, so this check should be solely
14053 	 * predicated on the sequence space of this segment. 3) That we
14054 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
14055 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
14056 	 * SEG.Len, This modified check allows us to overcome RFC1323's
14057 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
14058 	 * p.869. In such cases, we can still calculate the RTT correctly
14059 	 * when RCV.NXT == Last.ACK.Sent.
14060 	 */
14061 	if ((to->to_flags & TOF_TS) != 0 &&
14062 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
14063 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
14064 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
14065 		tp->ts_recent_age = tcp_ts_getticks();
14066 		tp->ts_recent = to->to_tsval;
14067 	}
14068 	/*
14069 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
14070 	 * is on (half-synchronized state), then queue data for later
14071 	 * processing; else drop segment and return.
14072 	 */
14073 	if ((thflags & TH_ACK) == 0) {
14074 		if (tp->t_flags & TF_NEEDSYN) {
14075 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
14076 			    tiwin, thflags, nxt_pkt));
14077 		} else if (tp->t_flags & TF_ACKNOW) {
14078 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
14079 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
14080 			return (ret_val);
14081 		} else {
14082 			ctf_do_drop(m, NULL);
14083 			return (0);
14084 		}
14085 	}
14086 	/*
14087 	 * Ack processing.
14088 	 */
14089 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
14090 		return (ret_val);
14091 	}
14092 	if (ourfinisacked) {
14093 		tcp_twstart(tp);
14094 		m_freem(m);
14095 		return (1);
14096 	}
14097 	if (sbavail(&so->so_snd)) {
14098 		if (ctf_progress_timeout_check(tp, true)) {
14099 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
14100 						tp, tick, PROGRESS_DROP, __LINE__);
14101 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
14102 			return (1);
14103 		}
14104 	}
14105 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
14106 	    tiwin, thflags, nxt_pkt));
14107 }
14108 
14109 /*
14110  * Return value of 1, the TCB is unlocked and most
14111  * likely gone, return value of 0, the TCP is still
14112  * locked.
14113  */
14114 static int
14115 rack_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
14116     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
14117     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
14118 {
14119 	int32_t ret_val = 0;
14120 	int32_t ourfinisacked = 0;
14121 	struct tcp_rack *rack;
14122 
14123 	rack = (struct tcp_rack *)tp->t_fb_ptr;
14124 	ctf_calc_rwin(so, tp);
14125 
14126 	if ((thflags & TH_RST) ||
14127 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
14128 		return (__ctf_process_rst(m, th, so, tp,
14129 					  &rack->r_ctl.challenge_ack_ts,
14130 					  &rack->r_ctl.challenge_ack_cnt));
14131 	/*
14132 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
14133 	 * synchronized state.
14134 	 */
14135 	if (thflags & TH_SYN) {
14136 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
14137 		return (ret_val);
14138 	}
14139 	/*
14140 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
14141 	 * it's less than ts_recent, drop it.
14142 	 */
14143 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
14144 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
14145 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
14146 			return (ret_val);
14147 	}
14148 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
14149 			      &rack->r_ctl.challenge_ack_ts,
14150 			      &rack->r_ctl.challenge_ack_cnt)) {
14151 		return (ret_val);
14152 	}
14153 	/*
14154 	 * If new data are received on a connection after the user processes
14155 	 * are gone, then RST the other end.
14156 	 */
14157 	if ((tp->t_flags & TF_CLOSED) && tlen &&
14158 	    rack_check_data_after_close(m, tp, &tlen, th, so))
14159 		return (1);
14160 	/*
14161 	 * If last ACK falls within this segment's sequence numbers, record
14162 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
14163 	 * from the latest proposal of the tcplw@cray.com list (Braden
14164 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
14165 	 * with our earlier PAWS tests, so this check should be solely
14166 	 * predicated on the sequence space of this segment. 3) That we
14167 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
14168 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
14169 	 * SEG.Len, This modified check allows us to overcome RFC1323's
14170 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
14171 	 * p.869. In such cases, we can still calculate the RTT correctly
14172 	 * when RCV.NXT == Last.ACK.Sent.
14173 	 */
14174 	if ((to->to_flags & TOF_TS) != 0 &&
14175 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
14176 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
14177 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
14178 		tp->ts_recent_age = tcp_ts_getticks();
14179 		tp->ts_recent = to->to_tsval;
14180 	}
14181 	/*
14182 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
14183 	 * is on (half-synchronized state), then queue data for later
14184 	 * processing; else drop segment and return.
14185 	 */
14186 	if ((thflags & TH_ACK) == 0) {
14187 		if (tp->t_flags & TF_NEEDSYN) {
14188 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
14189 			    tiwin, thflags, nxt_pkt));
14190 		} else if (tp->t_flags & TF_ACKNOW) {
14191 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
14192 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
14193 			return (ret_val);
14194 		} else {
14195 			ctf_do_drop(m, NULL);
14196 			return (0);
14197 		}
14198 	}
14199 	/*
14200 	 * case TCPS_LAST_ACK: Ack processing.
14201 	 */
14202 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
14203 		return (ret_val);
14204 	}
14205 	if (ourfinisacked) {
14206 		tp = tcp_close(tp);
14207 		ctf_do_drop(m, tp);
14208 		return (1);
14209 	}
14210 	if (sbavail(&so->so_snd)) {
14211 		if (ctf_progress_timeout_check(tp, true)) {
14212 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
14213 						tp, tick, PROGRESS_DROP, __LINE__);
14214 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
14215 			return (1);
14216 		}
14217 	}
14218 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
14219 	    tiwin, thflags, nxt_pkt));
14220 }
14221 
14222 /*
14223  * Return value of 1, the TCB is unlocked and most
14224  * likely gone, return value of 0, the TCP is still
14225  * locked.
14226  */
14227 static int
14228 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so,
14229     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
14230     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
14231 {
14232 	int32_t ret_val = 0;
14233 	int32_t ourfinisacked = 0;
14234 	struct tcp_rack *rack;
14235 
14236 	rack = (struct tcp_rack *)tp->t_fb_ptr;
14237 	ctf_calc_rwin(so, tp);
14238 
14239 	/* Reset receive buffer auto scaling when not in bulk receive mode. */
14240 	if ((thflags & TH_RST) ||
14241 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
14242 		return (__ctf_process_rst(m, th, so, tp,
14243 					  &rack->r_ctl.challenge_ack_ts,
14244 					  &rack->r_ctl.challenge_ack_cnt));
14245 	/*
14246 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
14247 	 * synchronized state.
14248 	 */
14249 	if (thflags & TH_SYN) {
14250 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
14251 		return (ret_val);
14252 	}
14253 	/*
14254 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
14255 	 * it's less than ts_recent, drop it.
14256 	 */
14257 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
14258 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
14259 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
14260 			return (ret_val);
14261 	}
14262 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
14263 			      &rack->r_ctl.challenge_ack_ts,
14264 			      &rack->r_ctl.challenge_ack_cnt)) {
14265 		return (ret_val);
14266 	}
14267 	/*
14268 	 * If new data are received on a connection after the user processes
14269 	 * are gone, then RST the other end.
14270 	 */
14271 	if ((tp->t_flags & TF_CLOSED) && tlen &&
14272 	    rack_check_data_after_close(m, tp, &tlen, th, so))
14273 		return (1);
14274 	/*
14275 	 * If last ACK falls within this segment's sequence numbers, record
14276 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
14277 	 * from the latest proposal of the tcplw@cray.com list (Braden
14278 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
14279 	 * with our earlier PAWS tests, so this check should be solely
14280 	 * predicated on the sequence space of this segment. 3) That we
14281 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
14282 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
14283 	 * SEG.Len, This modified check allows us to overcome RFC1323's
14284 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
14285 	 * p.869. In such cases, we can still calculate the RTT correctly
14286 	 * when RCV.NXT == Last.ACK.Sent.
14287 	 */
14288 	if ((to->to_flags & TOF_TS) != 0 &&
14289 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
14290 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
14291 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
14292 		tp->ts_recent_age = tcp_ts_getticks();
14293 		tp->ts_recent = to->to_tsval;
14294 	}
14295 	/*
14296 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
14297 	 * is on (half-synchronized state), then queue data for later
14298 	 * processing; else drop segment and return.
14299 	 */
14300 	if ((thflags & TH_ACK) == 0) {
14301 		if (tp->t_flags & TF_NEEDSYN) {
14302 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
14303 			    tiwin, thflags, nxt_pkt));
14304 		} else if (tp->t_flags & TF_ACKNOW) {
14305 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
14306 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
14307 			return (ret_val);
14308 		} else {
14309 			ctf_do_drop(m, NULL);
14310 			return (0);
14311 		}
14312 	}
14313 	/*
14314 	 * Ack processing.
14315 	 */
14316 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
14317 		return (ret_val);
14318 	}
14319 	if (sbavail(&so->so_snd)) {
14320 		if (ctf_progress_timeout_check(tp, true)) {
14321 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
14322 						tp, tick, PROGRESS_DROP, __LINE__);
14323 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
14324 			return (1);
14325 		}
14326 	}
14327 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
14328 	    tiwin, thflags, nxt_pkt));
14329 }
14330 
14331 static void inline
14332 rack_clear_rate_sample(struct tcp_rack *rack)
14333 {
14334 	rack->r_ctl.rack_rs.rs_flags = RACK_RTT_EMPTY;
14335 	rack->r_ctl.rack_rs.rs_rtt_cnt = 0;
14336 	rack->r_ctl.rack_rs.rs_rtt_tot = 0;
14337 }
14338 
14339 static void
14340 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override)
14341 {
14342 	uint64_t bw_est, rate_wanted;
14343 	int chged = 0;
14344 	uint32_t user_max, orig_min, orig_max;
14345 
14346 #ifdef TCP_REQUEST_TRK
14347 	if (rack->rc_hybrid_mode &&
14348 	    (rack->r_ctl.rc_pace_max_segs != 0) &&
14349 	    (rack_hybrid_allow_set_maxseg == 1) &&
14350 	    (rack->r_ctl.rc_last_sft != NULL)) {
14351 		rack->r_ctl.rc_last_sft->hybrid_flags &= ~TCP_HYBRID_PACING_SETMSS;
14352 		return;
14353 	}
14354 #endif
14355 	orig_min = rack->r_ctl.rc_pace_min_segs;
14356 	orig_max = rack->r_ctl.rc_pace_max_segs;
14357 	user_max = ctf_fixed_maxseg(tp) * rack->rc_user_set_max_segs;
14358 	if (ctf_fixed_maxseg(tp) != rack->r_ctl.rc_pace_min_segs)
14359 		chged = 1;
14360 	rack->r_ctl.rc_pace_min_segs = ctf_fixed_maxseg(tp);
14361 	if (rack->use_fixed_rate || rack->rc_force_max_seg) {
14362 		if (user_max != rack->r_ctl.rc_pace_max_segs)
14363 			chged = 1;
14364 	}
14365 	if (rack->rc_force_max_seg) {
14366 		rack->r_ctl.rc_pace_max_segs = user_max;
14367 	} else if (rack->use_fixed_rate) {
14368 		bw_est = rack_get_bw(rack);
14369 		if ((rack->r_ctl.crte == NULL) ||
14370 		    (bw_est != rack->r_ctl.crte->rate)) {
14371 			rack->r_ctl.rc_pace_max_segs = user_max;
14372 		} else {
14373 			/* We are pacing right at the hardware rate */
14374 			uint32_t segsiz, pace_one;
14375 
14376 			if (rack_pace_one_seg ||
14377 			    (rack->r_ctl.rc_user_set_min_segs == 1))
14378 				pace_one = 1;
14379 			else
14380 				pace_one = 0;
14381 			segsiz = min(ctf_fixed_maxseg(tp),
14382 				     rack->r_ctl.rc_pace_min_segs);
14383 			rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size_w_divisor(
14384 				tp, bw_est, segsiz, pace_one,
14385 				rack->r_ctl.crte, NULL, rack->r_ctl.pace_len_divisor);
14386 		}
14387 	} else if (rack->rc_always_pace) {
14388 		if (rack->r_ctl.gp_bw ||
14389 		    rack->r_ctl.init_rate) {
14390 			/* We have a rate of some sort set */
14391 			uint32_t  orig;
14392 
14393 			bw_est = rack_get_bw(rack);
14394 			orig = rack->r_ctl.rc_pace_max_segs;
14395 			if (fill_override)
14396 				rate_wanted = *fill_override;
14397 			else
14398 				rate_wanted = rack_get_gp_est(rack);
14399 			if (rate_wanted) {
14400 				/* We have something */
14401 				rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack,
14402 										   rate_wanted,
14403 										   ctf_fixed_maxseg(rack->rc_tp));
14404 			} else
14405 				rack->r_ctl.rc_pace_max_segs = rack->r_ctl.rc_pace_min_segs;
14406 			if (orig != rack->r_ctl.rc_pace_max_segs)
14407 				chged = 1;
14408 		} else if ((rack->r_ctl.gp_bw == 0) &&
14409 			   (rack->r_ctl.rc_pace_max_segs == 0)) {
14410 			/*
14411 			 * If we have nothing limit us to bursting
14412 			 * out IW sized pieces.
14413 			 */
14414 			chged = 1;
14415 			rack->r_ctl.rc_pace_max_segs = rc_init_window(rack);
14416 		}
14417 	}
14418 	if (rack->r_ctl.rc_pace_max_segs > PACE_MAX_IP_BYTES) {
14419 		chged = 1;
14420 		rack->r_ctl.rc_pace_max_segs = PACE_MAX_IP_BYTES;
14421 	}
14422 	if (chged)
14423 		rack_log_type_pacing_sizes(tp, rack, orig_min, orig_max, line, 2);
14424 }
14425 
14426 
14427 static void
14428 rack_init_fsb_block(struct tcpcb *tp, struct tcp_rack *rack, int32_t flags)
14429 {
14430 #ifdef INET6
14431 	struct ip6_hdr *ip6 = NULL;
14432 #endif
14433 #ifdef INET
14434 	struct ip *ip = NULL;
14435 #endif
14436 	struct udphdr *udp = NULL;
14437 
14438 	/* Ok lets fill in the fast block, it can only be used with no IP options! */
14439 #ifdef INET6
14440 	if (rack->r_is_v6) {
14441 		rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
14442 		ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
14443 		if (tp->t_port) {
14444 			rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr);
14445 			udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
14446 			udp->uh_sport = htons(V_tcp_udp_tunneling_port);
14447 			udp->uh_dport = tp->t_port;
14448 			rack->r_ctl.fsb.udp = udp;
14449 			rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1);
14450 		} else
14451 		{
14452 			rack->r_ctl.fsb.th = (struct tcphdr *)(ip6 + 1);
14453 			rack->r_ctl.fsb.udp = NULL;
14454 		}
14455 		tcpip_fillheaders(rack->rc_inp,
14456 				  tp->t_port,
14457 				  ip6, rack->r_ctl.fsb.th);
14458 		rack->r_ctl.fsb.hoplimit = in6_selecthlim(rack->rc_inp, NULL);
14459 	} else
14460 #endif				/* INET6 */
14461 #ifdef INET
14462 	{
14463 		rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr);
14464 		ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
14465 		if (tp->t_port) {
14466 			rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr);
14467 			udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
14468 			udp->uh_sport = htons(V_tcp_udp_tunneling_port);
14469 			udp->uh_dport = tp->t_port;
14470 			rack->r_ctl.fsb.udp = udp;
14471 			rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1);
14472 		} else
14473 		{
14474 			rack->r_ctl.fsb.udp = NULL;
14475 			rack->r_ctl.fsb.th = (struct tcphdr *)(ip + 1);
14476 		}
14477 		tcpip_fillheaders(rack->rc_inp,
14478 				  tp->t_port,
14479 				  ip, rack->r_ctl.fsb.th);
14480 		rack->r_ctl.fsb.hoplimit = tptoinpcb(tp)->inp_ip_ttl;
14481 	}
14482 #endif
14483 	rack->r_ctl.fsb.recwin = lmin(lmax(sbspace(&tptosocket(tp)->so_rcv), 0),
14484 	    (long)TCP_MAXWIN << tp->rcv_scale);
14485 	rack->r_fsb_inited = 1;
14486 }
14487 
14488 static int
14489 rack_init_fsb(struct tcpcb *tp, struct tcp_rack *rack)
14490 {
14491 	/*
14492 	 * Allocate the larger of spaces V6 if available else just
14493 	 * V4 and include udphdr (overbook)
14494 	 */
14495 #ifdef INET6
14496 	rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr) + sizeof(struct udphdr);
14497 #else
14498 	rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr) + sizeof(struct udphdr);
14499 #endif
14500 	rack->r_ctl.fsb.tcp_ip_hdr = malloc(rack->r_ctl.fsb.tcp_ip_hdr_len,
14501 					    M_TCPFSB, M_NOWAIT|M_ZERO);
14502 	if (rack->r_ctl.fsb.tcp_ip_hdr == NULL) {
14503 		return (ENOMEM);
14504 	}
14505 	rack->r_fsb_inited = 0;
14506 	return (0);
14507 }
14508 
14509 static void
14510 rack_log_hystart_event(struct tcp_rack *rack, uint32_t high_seq, uint8_t mod)
14511 {
14512 	/*
14513 	 * Types of logs (mod value)
14514 	 * 20 - Initial round setup
14515 	 * 21 - Rack declares a new round.
14516 	 */
14517 	struct tcpcb *tp;
14518 
14519 	tp = rack->rc_tp;
14520 	if (tcp_bblogging_on(tp)) {
14521 		union tcp_log_stackspecific log;
14522 		struct timeval tv;
14523 
14524 		memset(&log, 0, sizeof(log));
14525 		log.u_bbr.flex1 = rack->r_ctl.current_round;
14526 		log.u_bbr.flex2 = rack->r_ctl.roundends;
14527 		log.u_bbr.flex3 = high_seq;
14528 		log.u_bbr.flex4 = tp->snd_max;
14529 		log.u_bbr.flex8 = mod;
14530 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
14531 		log.u_bbr.cur_del_rate = rack->rc_tp->t_sndbytes;
14532 		log.u_bbr.delRate = rack->rc_tp->t_snd_rxt_bytes;
14533 		TCP_LOG_EVENTP(tp, NULL,
14534 		    &tptosocket(tp)->so_rcv,
14535 		    &tptosocket(tp)->so_snd,
14536 		    TCP_HYSTART, 0,
14537 		    0, &log, false, &tv);
14538 	}
14539 }
14540 
14541 static void
14542 rack_deferred_init(struct tcpcb *tp, struct tcp_rack *rack)
14543 {
14544 	rack->rack_deferred_inited = 1;
14545 	rack->r_ctl.roundends = tp->snd_max;
14546 	rack->r_ctl.rc_high_rwnd = tp->snd_wnd;
14547 	rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
14548 }
14549 
14550 static void
14551 rack_init_retransmit_value(struct tcp_rack *rack, int ctl)
14552 {
14553 	/* Retransmit bit controls.
14554 	 *
14555 	 * The setting of these values control one of
14556 	 * three settings you can have and dictate
14557 	 * how rack does retransmissions. Note this
14558 	 * is in *any* mode i.e. pacing on or off DGP
14559 	 * fixed rate pacing, or just bursting rack.
14560 	 *
14561 	 * 1 - Use full sized retransmits i.e. limit
14562 	 *     the size to whatever the pace_max_segments
14563 	 *     size is.
14564 	 *
14565 	 * 2 - Use pacer min granularity as a guide to
14566 	 *     the size combined with the current calculated
14567 	 *     goodput b/w measurement. So for example if
14568 	 *     the goodput is measured at 20Mbps we would
14569 	 *     calculate 8125 (pacer minimum 250usec in
14570 	 *     that b/w) and then round it up to the next
14571 	 *     MSS i.e. for 1448 mss 6 MSS or 8688 bytes.
14572 	 *
14573 	 * 0 - The rack default 1 MSS (anything not 0/1/2
14574 	 *     fall here too if we are setting via rack_init()).
14575 	 *
14576 	 */
14577 	if (ctl == 1) {
14578 		rack->full_size_rxt = 1;
14579 		rack->shape_rxt_to_pacing_min  = 0;
14580 	} else if (ctl == 2) {
14581 		rack->full_size_rxt = 0;
14582 		rack->shape_rxt_to_pacing_min  = 1;
14583 	} else {
14584 		rack->full_size_rxt = 0;
14585 		rack->shape_rxt_to_pacing_min  = 0;
14586 	}
14587 }
14588 
14589 static void
14590 rack_log_chg_info(struct tcpcb *tp, struct tcp_rack *rack, uint8_t mod,
14591 		  uint32_t flex1,
14592 		  uint32_t flex2,
14593 		  uint32_t flex3)
14594 {
14595 	if (tcp_bblogging_on(rack->rc_tp)) {
14596 		union tcp_log_stackspecific log;
14597 		struct timeval tv;
14598 
14599 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
14600 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
14601 		log.u_bbr.flex8 = mod;
14602 		log.u_bbr.flex1 = flex1;
14603 		log.u_bbr.flex2 = flex2;
14604 		log.u_bbr.flex3 = flex3;
14605 		tcp_log_event(tp, NULL, NULL, NULL, TCP_CHG_QUERY, 0,
14606 			       0, &log, false, NULL, __func__, __LINE__, &tv);
14607 	}
14608 }
14609 
14610 static int
14611 rack_chg_query(struct tcpcb *tp, struct tcp_query_resp *reqr)
14612 {
14613 	struct tcp_rack *rack;
14614 	struct rack_sendmap *rsm;
14615 	int i;
14616 
14617 
14618 	rack = (struct tcp_rack *)tp->t_fb_ptr;
14619 	switch (reqr->req) {
14620 	case TCP_QUERY_SENDMAP:
14621 		if ((reqr->req_param == tp->snd_max) ||
14622 		    (tp->snd_max == tp->snd_una)){
14623 			/* Unlikely */
14624 			return (0);
14625 		}
14626 		rsm = tqhash_find(rack->r_ctl.tqh, reqr->req_param);
14627 		if (rsm == NULL) {
14628 			/* Can't find that seq -- unlikely */
14629 			return (0);
14630 		}
14631 		reqr->sendmap_start = rsm->r_start;
14632 		reqr->sendmap_end = rsm->r_end;
14633 		reqr->sendmap_send_cnt = rsm->r_rtr_cnt;
14634 		reqr->sendmap_fas = rsm->r_fas;
14635 		if (reqr->sendmap_send_cnt > SNDMAP_NRTX)
14636 			reqr->sendmap_send_cnt = SNDMAP_NRTX;
14637 		for(i=0; i<reqr->sendmap_send_cnt; i++)
14638 			reqr->sendmap_time[i] = rsm->r_tim_lastsent[i];
14639 		reqr->sendmap_ack_arrival = rsm->r_ack_arrival;
14640 		reqr->sendmap_flags = rsm->r_flags & SNDMAP_MASK;
14641 		reqr->sendmap_r_rtr_bytes = rsm->r_rtr_bytes;
14642 		reqr->sendmap_dupacks = rsm->r_dupack;
14643 		rack_log_chg_info(tp, rack, 1,
14644 				  rsm->r_start,
14645 				  rsm->r_end,
14646 				  rsm->r_flags);
14647 		return(1);
14648 		break;
14649 	case TCP_QUERY_TIMERS_UP:
14650 		if (rack->r_ctl.rc_hpts_flags == 0) {
14651 			/* no timers up */
14652 			return (0);
14653 		}
14654 		reqr->timer_hpts_flags = rack->r_ctl.rc_hpts_flags;
14655 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
14656 			reqr->timer_pacing_to = rack->r_ctl.rc_last_output_to;
14657 		}
14658 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
14659 			reqr->timer_timer_exp = rack->r_ctl.rc_timer_exp;
14660 		}
14661 		rack_log_chg_info(tp, rack, 2,
14662 				  rack->r_ctl.rc_hpts_flags,
14663 				  rack->r_ctl.rc_last_output_to,
14664 				  rack->r_ctl.rc_timer_exp);
14665 		return (1);
14666 		break;
14667 	case TCP_QUERY_RACK_TIMES:
14668 		/* Reordering items */
14669 		reqr->rack_num_dsacks = rack->r_ctl.num_dsack;
14670 		reqr->rack_reorder_ts = rack->r_ctl.rc_reorder_ts;
14671 		/* Timerstamps and timers */
14672 		reqr->rack_rxt_last_time = rack->r_ctl.rc_tlp_rxt_last_time;
14673 		reqr->rack_min_rtt = rack->r_ctl.rc_rack_min_rtt;
14674 		reqr->rack_rtt = rack->rc_rack_rtt;
14675 		reqr->rack_tmit_time = rack->r_ctl.rc_rack_tmit_time;
14676 		reqr->rack_srtt_measured = rack->rc_srtt_measure_made;
14677 		/* PRR data */
14678 		reqr->rack_sacked = rack->r_ctl.rc_sacked;
14679 		reqr->rack_holes_rxt = rack->r_ctl.rc_holes_rxt;
14680 		reqr->rack_prr_delivered = rack->r_ctl.rc_prr_delivered;
14681 		reqr->rack_prr_recovery_fs = rack->r_ctl.rc_prr_recovery_fs;
14682 		reqr->rack_prr_sndcnt = rack->r_ctl.rc_prr_sndcnt;
14683 		reqr->rack_prr_out = rack->r_ctl.rc_prr_out;
14684 		/* TLP and persists info */
14685 		reqr->rack_tlp_out = rack->rc_tlp_in_progress;
14686 		reqr->rack_tlp_cnt_out = rack->r_ctl.rc_tlp_cnt_out;
14687 		if (rack->rc_in_persist) {
14688 			reqr->rack_time_went_idle = rack->r_ctl.rc_went_idle_time;
14689 			reqr->rack_in_persist = 1;
14690 		} else {
14691 			reqr->rack_time_went_idle = 0;
14692 			reqr->rack_in_persist = 0;
14693 		}
14694 		if (rack->r_wanted_output)
14695 			reqr->rack_wanted_output = 1;
14696 		else
14697 			reqr->rack_wanted_output = 0;
14698 		return (1);
14699 		break;
14700 	default:
14701 		return (-EINVAL);
14702 	}
14703 }
14704 
14705 static void
14706 rack_switch_failed(struct tcpcb *tp)
14707 {
14708 	/*
14709 	 * This method gets called if a stack switch was
14710 	 * attempted and it failed. We are left
14711 	 * but our hpts timers were stopped and we
14712 	 * need to validate time units and t_flags2.
14713 	 */
14714 	struct tcp_rack *rack;
14715 	struct timeval tv;
14716 	uint32_t cts;
14717 	uint32_t toval;
14718 	struct hpts_diag diag;
14719 
14720 	rack = (struct tcp_rack *)tp->t_fb_ptr;
14721 	tcp_change_time_units(tp, TCP_TMR_GRANULARITY_USEC);
14722 	if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
14723 		tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
14724 	else
14725 		tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
14726 	if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
14727 		tp->t_flags2 |= TF2_MBUF_ACKCMP;
14728 	if (tp->t_in_hpts > IHPTS_NONE) {
14729 		/* Strange */
14730 		return;
14731 	}
14732 	cts = tcp_get_usecs(&tv);
14733 	if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
14734 		if (TSTMP_GT(rack->r_ctl.rc_last_output_to, cts)) {
14735 			toval = rack->r_ctl.rc_last_output_to - cts;
14736 		} else {
14737 			/* one slot please */
14738 			toval = HPTS_TICKS_PER_SLOT;
14739 		}
14740 	} else if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
14741 		if (TSTMP_GT(rack->r_ctl.rc_timer_exp, cts)) {
14742 			toval = rack->r_ctl.rc_timer_exp - cts;
14743 		} else {
14744 			/* one slot please */
14745 			toval = HPTS_TICKS_PER_SLOT;
14746 		}
14747 	} else
14748 		toval = HPTS_TICKS_PER_SLOT;
14749 	(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(toval),
14750 				   __LINE__, &diag);
14751 	rack_log_hpts_diag(rack, cts, &diag, &tv);
14752 }
14753 
14754 static int
14755 rack_init_outstanding(struct tcpcb *tp, struct tcp_rack *rack, uint32_t us_cts, void *ptr)
14756 {
14757 	struct rack_sendmap *rsm, *ersm;
14758 	int insret __diagused;
14759 	/*
14760 	 * When initing outstanding, we must be quite careful
14761 	 * to not refer to tp->t_fb_ptr. This has the old rack
14762 	 * pointer in it, not the "new" one (when we are doing
14763 	 * a stack switch).
14764 	 */
14765 
14766 
14767 	if (tp->t_fb->tfb_chg_query == NULL) {
14768 		/* Create a send map for the current outstanding data */
14769 
14770 		rsm = rack_alloc(rack);
14771 		if (rsm == NULL) {
14772 			uma_zfree(rack_pcb_zone, ptr);
14773 			return (ENOMEM);
14774 		}
14775 		rsm->r_no_rtt_allowed = 1;
14776 		rsm->r_tim_lastsent[0] = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
14777 		rsm->r_rtr_cnt = 1;
14778 		rsm->r_rtr_bytes = 0;
14779 		if (tp->t_flags & TF_SENTFIN)
14780 			rsm->r_flags |= RACK_HAS_FIN;
14781 		rsm->r_end = tp->snd_max;
14782 		if (tp->snd_una == tp->iss) {
14783 			/* The data space is one beyond snd_una */
14784 			rsm->r_flags |= RACK_HAS_SYN;
14785 			rsm->r_start = tp->iss;
14786 			rsm->r_end = rsm->r_start + (tp->snd_max - tp->snd_una);
14787 		} else
14788 			rsm->r_start = tp->snd_una;
14789 		rsm->r_dupack = 0;
14790 		if (rack->rc_inp->inp_socket->so_snd.sb_mb != NULL) {
14791 			rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd, 0, &rsm->soff);
14792 			if (rsm->m) {
14793 				rsm->orig_m_len = rsm->m->m_len;
14794 				rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
14795 			} else {
14796 				rsm->orig_m_len = 0;
14797 				rsm->orig_t_space = 0;
14798 			}
14799 		} else {
14800 			/*
14801 			 * This can happen if we have a stand-alone FIN or
14802 			 *  SYN.
14803 			 */
14804 			rsm->m = NULL;
14805 			rsm->orig_m_len = 0;
14806 			rsm->orig_t_space = 0;
14807 			rsm->soff = 0;
14808 		}
14809 #ifdef INVARIANTS
14810 		if ((insret = tqhash_insert(rack->r_ctl.tqh, rsm)) != 0) {
14811 			panic("Insert in rb tree fails ret:%d rack:%p rsm:%p",
14812 			      insret, rack, rsm);
14813 		}
14814 #else
14815 		(void)tqhash_insert(rack->r_ctl.tqh, rsm);
14816 #endif
14817 		TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
14818 		rsm->r_in_tmap = 1;
14819 	} else {
14820 		/* We have a query mechanism, lets use it */
14821 		struct tcp_query_resp qr;
14822 		int i;
14823 		tcp_seq at;
14824 
14825 		at = tp->snd_una;
14826 		while (at != tp->snd_max) {
14827 			memset(&qr, 0, sizeof(qr));
14828 			qr.req = TCP_QUERY_SENDMAP;
14829 			qr.req_param = at;
14830 			if ((*tp->t_fb->tfb_chg_query)(tp, &qr) == 0)
14831 				break;
14832 			/* Move forward */
14833 			at = qr.sendmap_end;
14834 			/* Now lets build the entry for this one */
14835 			rsm = rack_alloc(rack);
14836 			if (rsm == NULL) {
14837 				uma_zfree(rack_pcb_zone, ptr);
14838 				return (ENOMEM);
14839 			}
14840 			memset(rsm, 0, sizeof(struct rack_sendmap));
14841 			/* Now configure the rsm and insert it */
14842 			rsm->r_dupack = qr.sendmap_dupacks;
14843 			rsm->r_start = qr.sendmap_start;
14844 			rsm->r_end = qr.sendmap_end;
14845 			if (qr.sendmap_fas)
14846 				rsm->r_fas = qr.sendmap_end;
14847 			else
14848 				rsm->r_fas = rsm->r_start - tp->snd_una;
14849 			/*
14850 			 * We have carefully aligned the bits
14851 			 * so that all we have to do is copy over
14852 			 * the bits with the mask.
14853 			 */
14854 			rsm->r_flags = qr.sendmap_flags & SNDMAP_MASK;
14855 			rsm->r_rtr_bytes = qr.sendmap_r_rtr_bytes;
14856 			rsm->r_rtr_cnt = qr.sendmap_send_cnt;
14857 			rsm->r_ack_arrival = qr.sendmap_ack_arrival;
14858 			for (i=0 ; i<rsm->r_rtr_cnt; i++)
14859 				rsm->r_tim_lastsent[i]	= qr.sendmap_time[i];
14860 			rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd,
14861 					   (rsm->r_start - tp->snd_una), &rsm->soff);
14862 			if (rsm->m) {
14863 				rsm->orig_m_len = rsm->m->m_len;
14864 				rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
14865 			} else {
14866 				rsm->orig_m_len = 0;
14867 				rsm->orig_t_space = 0;
14868 			}
14869 #ifdef INVARIANTS
14870 			if ((insret = tqhash_insert(rack->r_ctl.tqh, rsm)) != 0) {
14871 				panic("Insert in rb tree fails ret:%d rack:%p rsm:%p",
14872 				      insret, rack, rsm);
14873 			}
14874 #else
14875 			(void)tqhash_insert(rack->r_ctl.tqh, rsm);
14876 #endif
14877 			if ((rsm->r_flags & RACK_ACKED) == 0)  {
14878 				TAILQ_FOREACH(ersm, &rack->r_ctl.rc_tmap, r_tnext) {
14879 					if (ersm->r_tim_lastsent[(ersm->r_rtr_cnt-1)] >
14880 					    rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]) {
14881 						/*
14882 						 * If the existing ersm was sent at
14883 						 * a later time than the new one, then
14884 						 * the new one should appear ahead of this
14885 						 * ersm.
14886 						 */
14887 						rsm->r_in_tmap = 1;
14888 						TAILQ_INSERT_BEFORE(ersm, rsm, r_tnext);
14889 						break;
14890 					}
14891 				}
14892 				if (rsm->r_in_tmap == 0) {
14893 					/*
14894 					 * Not found so shove it on the tail.
14895 					 */
14896 					TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
14897 					rsm->r_in_tmap = 1;
14898 				}
14899  			} else {
14900 				if ((rack->r_ctl.rc_sacklast == NULL) ||
14901 				    (SEQ_GT(rsm->r_end, rack->r_ctl.rc_sacklast->r_end))) {
14902 					rack->r_ctl.rc_sacklast = rsm;
14903 				}
14904 			}
14905 			rack_log_chg_info(tp, rack, 3,
14906 					  rsm->r_start,
14907 					  rsm->r_end,
14908 					  rsm->r_flags);
14909 		}
14910 	}
14911 	return (0);
14912 }
14913 
14914 static void
14915 rack_translate_clamp_value(struct tcp_rack *rack, uint32_t optval)
14916 {
14917 	/*
14918 	 * P = percent bits
14919 	 * F = fill cw bit -- Toggle fillcw if this bit is set.
14920 	 * S = Segment bits
14921 	 * M = set max segment bit
14922 	 * U = Unclamined
14923 	 * C = If set to non-zero override the max number of clamps.
14924 	 * L = Bit to indicate if clamped gets lower.
14925 	 *
14926 	 * CCCC CCCCC UUUU UULF PPPP PPPP PPPP PPPP
14927 	 *
14928 	 * The lowest 3 nibbles is the perentage .1 - 6553.5%
14929 	 * where 10.1 = 101, max 6553.5
14930 	 * The upper 16 bits  holds some options.
14931 	 * The F bit will turn on fill-cw on if you are
14932 	 * not pacing, it will turn it off if dgp is on.
14933 	 * The L bit will change it so when clamped we get
14934 	 * the min(gp, lt-bw) for dgp.
14935 	 */
14936 	uint16_t per;
14937 
14938 	rack->r_ctl.saved_rxt_clamp_val = optval;
14939 	per = optval & 0x0000ffff;
14940 	rack->r_ctl.rxt_threshold = (uint64_t)(per & 0xffff);
14941 	if (optval > 0) {
14942 		uint16_t clamp_opt;
14943 
14944 		rack->excess_rxt_on = 1;
14945 		clamp_opt = ((optval & 0xffff0000) >> 16);
14946 		rack->r_ctl.clamp_options = clamp_opt & 0x00ff;
14947 		if (clamp_opt & 0xff00) {
14948 			/* A max clamps is also present */
14949 			rack->r_ctl.max_clamps = (clamp_opt >> 8);
14950 		} else {
14951 			/* No specified clamps means no limit */
14952 			rack->r_ctl.max_clamps = 0;
14953 		}
14954 		if (rack->r_ctl.clamp_options & 0x0002) {
14955 			rack->r_clamped_gets_lower  = 1;
14956 		} else {
14957 			rack->r_clamped_gets_lower  = 0;
14958 		}
14959 	} else {
14960 		/* Turn it off back to default */
14961 		rack->excess_rxt_on = 0;
14962 		rack->r_clamped_gets_lower  = 0;
14963 	}
14964 
14965 }
14966 
14967 
14968 static int32_t
14969 rack_init(struct tcpcb *tp, void **ptr)
14970 {
14971 	struct inpcb *inp = tptoinpcb(tp);
14972 	struct tcp_rack *rack = NULL;
14973 	uint32_t iwin, snt, us_cts;
14974 	int err, no_query;
14975 
14976 	/*
14977 	 * First are we the initial or are we a switched stack?
14978 	 * If we are initing via tcp_newtcppcb the ptr passed
14979 	 * will be tp->t_fb_ptr. If its a stack switch that
14980 	 * has a previous stack we can query it will be a local
14981 	 * var that will in the end be set into t_fb_ptr.
14982 	 */
14983 	if (ptr == &tp->t_fb_ptr)
14984 		no_query = 1;
14985 	else
14986 		no_query = 0;
14987 	*ptr = uma_zalloc(rack_pcb_zone, M_NOWAIT);
14988 	if (*ptr == NULL) {
14989 		/*
14990 		 * We need to allocate memory but cant. The INP and INP_INFO
14991 		 * locks and they are recursive (happens during setup. So a
14992 		 * scheme to drop the locks fails :(
14993 		 *
14994 		 */
14995 		return(ENOMEM);
14996 	}
14997 	memset(*ptr, 0, sizeof(struct tcp_rack));
14998 	rack = (struct tcp_rack *)*ptr;
14999 	rack->r_ctl.tqh = malloc(sizeof(struct tailq_hash), M_TCPFSB, M_NOWAIT);
15000 	if (rack->r_ctl.tqh == NULL) {
15001 		uma_zfree(rack_pcb_zone, rack);
15002 		return(ENOMEM);
15003 	}
15004 	tqhash_init(rack->r_ctl.tqh);
15005 	TAILQ_INIT(&rack->r_ctl.rc_free);
15006 	TAILQ_INIT(&rack->r_ctl.rc_tmap);
15007 	rack->rc_tp = tp;
15008 	rack->rc_inp = inp;
15009 	/* Set the flag */
15010 	rack->r_is_v6 = (inp->inp_vflag & INP_IPV6) != 0;
15011 	/* Probably not needed but lets be sure */
15012 	rack_clear_rate_sample(rack);
15013 	/*
15014 	 * Save off the default values, socket options will poke
15015 	 * at these if pacing is not on or we have not yet
15016 	 * reached where pacing is on (gp_ready/fixed enabled).
15017 	 * When they get set into the CC module (when gp_ready
15018 	 * is enabled or we enable fixed) then we will set these
15019 	 * values into the CC and place in here the old values
15020 	 * so we have a restoral. Then we will set the flag
15021 	 * rc_pacing_cc_set. That way whenever we turn off pacing
15022 	 * or switch off this stack, we will know to go restore
15023 	 * the saved values.
15024 	 *
15025 	 * We specifically put into the beta the ecn value for pacing.
15026 	 */
15027 	rack->rc_new_rnd_needed = 1;
15028 	rack->r_ctl.rc_split_limit = V_tcp_map_split_limit;
15029 	/* We want abe like behavior as well */
15030 	rack->r_ctl.rc_saved_beta.newreno_flags |= CC_NEWRENO_BETA_ECN_ENABLED;
15031 	rack->r_ctl.rc_reorder_fade = rack_reorder_fade;
15032 	rack->rc_allow_data_af_clo = rack_ignore_data_after_close;
15033 	rack->r_ctl.rc_tlp_threshold = rack_tlp_thresh;
15034 	if (rack_rxt_clamp_thresh) {
15035 		rack_translate_clamp_value(rack, rack_rxt_clamp_thresh);
15036 		rack->excess_rxt_on = 1;
15037 	}
15038 	if (rack_uses_full_dgp_in_rec)
15039 		rack->r_ctl.full_dgp_in_rec = 1;
15040 	if (rack_fill_cw_state)
15041 		rack->rc_pace_to_cwnd = 1;
15042 	if (rack_pacing_min_seg)
15043 		rack->r_ctl.rc_user_set_min_segs = rack_pacing_min_seg;
15044 	if (use_rack_rr)
15045 		rack->use_rack_rr = 1;
15046 	if (rack_dnd_default) {
15047 		rack->rc_pace_dnd = 1;
15048 	}
15049 	if (V_tcp_delack_enabled)
15050 		tp->t_delayed_ack = 1;
15051 	else
15052 		tp->t_delayed_ack = 0;
15053 #ifdef TCP_ACCOUNTING
15054 	if (rack_tcp_accounting) {
15055 		tp->t_flags2 |= TF2_TCP_ACCOUNTING;
15056 	}
15057 #endif
15058 	rack->r_ctl.rack_per_upper_bound_ss = (uint8_t)rack_per_upper_bound_ss;
15059 	rack->r_ctl.rack_per_upper_bound_ca = (uint8_t)rack_per_upper_bound_ca;
15060 	if (rack_enable_shared_cwnd)
15061 		rack->rack_enable_scwnd = 1;
15062 	rack->r_ctl.pace_len_divisor = rack_default_pacing_divisor;
15063 	rack->rc_user_set_max_segs = rack_hptsi_segments;
15064 	rack->rc_force_max_seg = 0;
15065 	TAILQ_INIT(&rack->r_ctl.opt_list);
15066 	rack->r_ctl.rc_saved_beta.beta = V_newreno_beta_ecn;
15067 	rack->r_ctl.rc_saved_beta.beta_ecn = V_newreno_beta_ecn;
15068 	if (rack_hibeta_setting) {
15069 		rack->rack_hibeta = 1;
15070 		if ((rack_hibeta_setting >= 50) &&
15071 		    (rack_hibeta_setting <= 100)) {
15072 			rack->r_ctl.rc_saved_beta.beta = rack_hibeta_setting;
15073 			rack->r_ctl.saved_hibeta = rack_hibeta_setting;
15074 		}
15075 	} else {
15076 		rack->r_ctl.saved_hibeta = 50;
15077 	}
15078 	rack->r_ctl.rc_reorder_shift = rack_reorder_thresh;
15079 	rack->r_ctl.rc_pkt_delay = rack_pkt_delay;
15080 	rack->r_ctl.rc_tlp_cwnd_reduce = rack_lower_cwnd_at_tlp;
15081 	rack->r_ctl.rc_lowest_us_rtt = 0xffffffff;
15082 	rack->r_ctl.rc_highest_us_rtt = 0;
15083 	rack->r_ctl.bw_rate_cap = rack_bw_rate_cap;
15084 	rack->r_ctl.timer_slop = TICKS_2_USEC(tcp_rexmit_slop);
15085 	if (rack_use_cmp_acks)
15086 		rack->r_use_cmp_ack = 1;
15087 	if (rack_disable_prr)
15088 		rack->rack_no_prr = 1;
15089 	if (rack_gp_no_rec_chg)
15090 		rack->rc_gp_no_rec_chg = 1;
15091 	if (rack_pace_every_seg && tcp_can_enable_pacing()) {
15092 		rack->rc_always_pace = 1;
15093 		if (rack->rack_hibeta)
15094 			rack_set_cc_pacing(rack);
15095 	} else
15096 		rack->rc_always_pace = 0;
15097 	if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack)
15098 		rack->r_mbuf_queue = 1;
15099 	else
15100 		rack->r_mbuf_queue = 0;
15101 	rack_set_pace_segments(tp, rack, __LINE__, NULL);
15102 	if (rack_limits_scwnd)
15103 		rack->r_limit_scw = 1;
15104 	else
15105 		rack->r_limit_scw = 0;
15106 	rack_init_retransmit_value(rack, rack_rxt_controls);
15107 	rack->rc_labc = V_tcp_abc_l_var;
15108 	rack->r_ctl.rc_rate_sample_method = rack_rate_sample_method;
15109 	rack->rack_tlp_threshold_use = rack_tlp_threshold_use;
15110 	rack->r_ctl.rc_prr_sendalot = rack_send_a_lot_in_prr;
15111 	rack->r_ctl.rc_min_to = rack_min_to;
15112 	microuptime(&rack->r_ctl.act_rcv_time);
15113 	rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time;
15114 	rack->rc_init_win = rack_default_init_window;
15115 	rack->r_ctl.rack_per_of_gp_ss = rack_per_of_gp_ss;
15116 	if (rack_hw_up_only)
15117 		rack->r_up_only = 1;
15118 	if (rack_do_dyn_mul) {
15119 		/* When dynamic adjustment is on CA needs to start at 100% */
15120 		rack->rc_gp_dyn_mul = 1;
15121 		if (rack_do_dyn_mul >= 100)
15122 			rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul;
15123 	} else
15124 		rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca;
15125 	rack->r_ctl.rack_per_of_gp_rec = rack_per_of_gp_rec;
15126 	rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
15127 	rack->r_ctl.rc_tlp_rxt_last_time = tcp_tv_to_mssectick(&rack->r_ctl.act_rcv_time);
15128 	setup_time_filter_small(&rack->r_ctl.rc_gp_min_rtt, FILTER_TYPE_MIN,
15129 				rack_probertt_filter_life);
15130 	us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
15131 	rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
15132 	rack->r_ctl.rc_time_of_last_probertt = us_cts;
15133 	rack->r_ctl.challenge_ack_ts = tcp_ts_getticks();
15134 	rack->r_ctl.rc_time_probertt_starts = 0;
15135 	if (rack_dsack_std_based & 0x1) {
15136 		/* Basically this means all rack timers are at least (srtt + 1/4 srtt) */
15137 		rack->rc_rack_tmr_std_based = 1;
15138 	}
15139 	if (rack_dsack_std_based & 0x2) {
15140 		/* Basically this means  rack timers are extended based on dsack by up to (2 * srtt) */
15141 		rack->rc_rack_use_dsack = 1;
15142 	}
15143 	/* We require at least one measurement, even if the sysctl is 0 */
15144 	if (rack_req_measurements)
15145 		rack->r_ctl.req_measurements = rack_req_measurements;
15146 	else
15147 		rack->r_ctl.req_measurements = 1;
15148 	if (rack_enable_hw_pacing)
15149 		rack->rack_hdw_pace_ena = 1;
15150 	if (rack_hw_rate_caps)
15151 		rack->r_rack_hw_rate_caps = 1;
15152 #ifdef TCP_SAD_DETECTION
15153 	rack->do_detection = 1;
15154 #else
15155 	rack->do_detection = 0;
15156 #endif
15157 	if (rack_non_rxt_use_cr)
15158 		rack->rack_rec_nonrxt_use_cr = 1;
15159 	/* Lets setup the fsb block */
15160 	err = rack_init_fsb(tp, rack);
15161 	if (err) {
15162 		uma_zfree(rack_pcb_zone, *ptr);
15163 		*ptr = NULL;
15164 		return (err);
15165 	}
15166 	if (rack_do_hystart) {
15167 		tp->t_ccv.flags |= CCF_HYSTART_ALLOWED;
15168 		if (rack_do_hystart > 1)
15169 			tp->t_ccv.flags |= CCF_HYSTART_CAN_SH_CWND;
15170 		if (rack_do_hystart > 2)
15171 			tp->t_ccv.flags |= CCF_HYSTART_CONS_SSTH;
15172 	}
15173 	/* Log what we will do with queries */
15174 	rack_log_chg_info(tp, rack, 7,
15175 			  no_query, 0, 0);
15176 	if (rack_def_profile)
15177 		rack_set_profile(rack, rack_def_profile);
15178 	/* Cancel the GP measurement in progress */
15179 	tp->t_flags &= ~TF_GPUTINPROG;
15180 	if ((tp->t_state != TCPS_CLOSED) &&
15181 	    (tp->t_state != TCPS_TIME_WAIT)) {
15182 		/*
15183 		 * We are already open, we may
15184 		 * need to adjust a few things.
15185 		 */
15186 		if (SEQ_GT(tp->snd_max, tp->iss))
15187 			snt = tp->snd_max - tp->iss;
15188 		else
15189 			snt = 0;
15190 		iwin = rc_init_window(rack);
15191 		if ((snt < iwin) &&
15192 		    (no_query == 1)) {
15193 			/* We are not past the initial window
15194 			 * on the first init (i.e. a stack switch
15195 			 * has not yet occured) so we need to make
15196 			 * sure cwnd and ssthresh is correct.
15197 			 */
15198 			if (tp->snd_cwnd < iwin)
15199 				tp->snd_cwnd = iwin;
15200 			/*
15201 			 * If we are within the initial window
15202 			 * we want ssthresh to be unlimited. Setting
15203 			 * it to the rwnd (which the default stack does
15204 			 * and older racks) is not really a good idea
15205 			 * since we want to be in SS and grow both the
15206 			 * cwnd and the rwnd (via dynamic rwnd growth). If
15207 			 * we set it to the rwnd then as the peer grows its
15208 			 * rwnd we will be stuck in CA and never hit SS.
15209 			 *
15210 			 * Its far better to raise it up high (this takes the
15211 			 * risk that there as been a loss already, probably
15212 			 * we should have an indicator in all stacks of loss
15213 			 * but we don't), but considering the normal use this
15214 			 * is a risk worth taking. The consequences of not
15215 			 * hitting SS are far worse than going one more time
15216 			 * into it early on (before we have sent even a IW).
15217 			 * It is highly unlikely that we will have had a loss
15218 			 * before getting the IW out.
15219 			 */
15220 			tp->snd_ssthresh = 0xffffffff;
15221 		}
15222 		/*
15223 		 * Any init based on sequence numbers
15224 		 * should be done in the deferred init path
15225 		 * since we can be CLOSED and not have them
15226 		 * inited when rack_init() is called. We
15227 		 * are not closed so lets call it.
15228 		 */
15229 		rack_deferred_init(tp, rack);
15230 	}
15231 	if ((tp->t_state != TCPS_CLOSED) &&
15232 	    (tp->t_state != TCPS_TIME_WAIT) &&
15233 	    (no_query == 0) &&
15234 	    (tp->snd_una != tp->snd_max))  {
15235 		err = rack_init_outstanding(tp, rack, us_cts, *ptr);
15236 		if (err) {
15237 			*ptr = NULL;
15238 			return(err);
15239 		}
15240 	}
15241 	rack_stop_all_timers(tp, rack);
15242 	/* Setup all the t_flags2 */
15243 	if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
15244 		tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
15245 	else
15246 		tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
15247 	if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
15248 		tp->t_flags2 |= TF2_MBUF_ACKCMP;
15249 	/*
15250 	 * Timers in Rack are kept in microseconds so lets
15251 	 * convert any initial incoming variables
15252 	 * from ticks into usecs. Note that we
15253 	 * also change the values of t_srtt and t_rttvar, if
15254 	 * they are non-zero. They are kept with a 5
15255 	 * bit decimal so we have to carefully convert
15256 	 * these to get the full precision.
15257 	 */
15258 	rack_convert_rtts(tp);
15259 	rack_log_hystart_event(rack, rack->r_ctl.roundends, 20);
15260 	if ((tptoinpcb(tp)->inp_flags & INP_DROPPED) == 0) {
15261 		/* We do not start any timers on DROPPED connections */
15262 		if (tp->t_fb->tfb_chg_query == NULL) {
15263 			rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
15264 		} else {
15265 			struct tcp_query_resp qr;
15266 			int ret;
15267 
15268 			memset(&qr, 0, sizeof(qr));
15269 
15270 			/* Get the misc time stamps and such for rack */
15271 			qr.req = TCP_QUERY_RACK_TIMES;
15272 			ret = (*tp->t_fb->tfb_chg_query)(tp, &qr);
15273 			if (ret == 1) {
15274 				rack->r_ctl.rc_reorder_ts = qr.rack_reorder_ts;
15275 				rack->r_ctl.num_dsack  = qr.rack_num_dsacks;
15276 				rack->r_ctl.rc_tlp_rxt_last_time = qr.rack_rxt_last_time;
15277 				rack->r_ctl.rc_rack_min_rtt = qr.rack_min_rtt;
15278 				rack->rc_rack_rtt = qr.rack_rtt;
15279 				rack->r_ctl.rc_rack_tmit_time = qr.rack_tmit_time;
15280 				rack->r_ctl.rc_sacked = qr.rack_sacked;
15281 				rack->r_ctl.rc_holes_rxt = qr.rack_holes_rxt;
15282 				rack->r_ctl.rc_prr_delivered = qr.rack_prr_delivered;
15283 				rack->r_ctl.rc_prr_recovery_fs = qr.rack_prr_recovery_fs;
15284 				rack->r_ctl.rc_prr_sndcnt = qr.rack_prr_sndcnt;
15285 				rack->r_ctl.rc_prr_out = qr.rack_prr_out;
15286 				if (qr.rack_tlp_out) {
15287 					rack->rc_tlp_in_progress = 1;
15288 					rack->r_ctl.rc_tlp_cnt_out = qr.rack_tlp_cnt_out;
15289 				} else {
15290 					rack->rc_tlp_in_progress = 0;
15291 					rack->r_ctl.rc_tlp_cnt_out = 0;
15292 				}
15293 				if (qr.rack_srtt_measured)
15294 					rack->rc_srtt_measure_made = 1;
15295 				if (qr.rack_in_persist == 1) {
15296 					rack->r_ctl.rc_went_idle_time = qr.rack_time_went_idle;
15297 #ifdef NETFLIX_SHARED_CWND
15298 					if (rack->r_ctl.rc_scw) {
15299 						tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
15300 						rack->rack_scwnd_is_idle = 1;
15301 					}
15302 #endif
15303 					rack->r_ctl.persist_lost_ends = 0;
15304 					rack->probe_not_answered = 0;
15305 					rack->forced_ack = 0;
15306 					tp->t_rxtshift = 0;
15307 					rack->rc_in_persist = 1;
15308 					RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
15309 							   rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
15310 				}
15311 				if (qr.rack_wanted_output)
15312 					rack->r_wanted_output = 1;
15313 				rack_log_chg_info(tp, rack, 6,
15314 						  qr.rack_min_rtt,
15315 						  qr.rack_rtt,
15316 						  qr.rack_reorder_ts);
15317 			}
15318 			/* Get the old stack timers */
15319 			qr.req_param = 0;
15320 			qr.req = TCP_QUERY_TIMERS_UP;
15321 			ret = (*tp->t_fb->tfb_chg_query)(tp, &qr);
15322 			if (ret) {
15323 				/*
15324 				 * non-zero return means we have a timer('s)
15325 				 * to start. Zero means no timer (no keepalive
15326 				 * I suppose).
15327 				 */
15328 				uint32_t tov = 0;
15329 
15330 				rack->r_ctl.rc_hpts_flags = qr.timer_hpts_flags;
15331 				if (qr.timer_hpts_flags & PACE_PKT_OUTPUT) {
15332 					rack->r_ctl.rc_last_output_to = qr.timer_pacing_to;
15333 					if (TSTMP_GT(qr.timer_pacing_to, us_cts))
15334 						tov = qr.timer_pacing_to - us_cts;
15335 					else
15336 						tov = HPTS_TICKS_PER_SLOT;
15337 				}
15338 				if (qr.timer_hpts_flags & PACE_TMR_MASK) {
15339 					rack->r_ctl.rc_timer_exp = qr.timer_timer_exp;
15340 					if (tov == 0) {
15341 						if (TSTMP_GT(qr.timer_timer_exp, us_cts))
15342 							tov = qr.timer_timer_exp - us_cts;
15343 						else
15344 							tov = HPTS_TICKS_PER_SLOT;
15345 					}
15346 				}
15347 				rack_log_chg_info(tp, rack, 4,
15348 						  rack->r_ctl.rc_hpts_flags,
15349 						  rack->r_ctl.rc_last_output_to,
15350 						  rack->r_ctl.rc_timer_exp);
15351 				if (tov) {
15352 					struct hpts_diag diag;
15353 
15354 					(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(tov),
15355 								   __LINE__, &diag);
15356 					rack_log_hpts_diag(rack, us_cts, &diag, &rack->r_ctl.act_rcv_time);
15357 				}
15358 			}
15359 		}
15360 		rack_log_rtt_shrinks(rack,  us_cts,  tp->t_rxtcur,
15361 				     __LINE__, RACK_RTTS_INIT);
15362 	}
15363 	return (0);
15364 }
15365 
15366 static int
15367 rack_handoff_ok(struct tcpcb *tp)
15368 {
15369 	if ((tp->t_state == TCPS_CLOSED) ||
15370 	    (tp->t_state == TCPS_LISTEN)) {
15371 		/* Sure no problem though it may not stick */
15372 		return (0);
15373 	}
15374 	if ((tp->t_state == TCPS_SYN_SENT) ||
15375 	    (tp->t_state == TCPS_SYN_RECEIVED)) {
15376 		/*
15377 		 * We really don't know if you support sack,
15378 		 * you have to get to ESTAB or beyond to tell.
15379 		 */
15380 		return (EAGAIN);
15381 	}
15382 	if ((tp->t_flags & TF_SENTFIN) && ((tp->snd_max - tp->snd_una) > 1)) {
15383 		/*
15384 		 * Rack will only send a FIN after all data is acknowledged.
15385 		 * So in this case we have more data outstanding. We can't
15386 		 * switch stacks until either all data and only the FIN
15387 		 * is left (in which case rack_init() now knows how
15388 		 * to deal with that) <or> all is acknowledged and we
15389 		 * are only left with incoming data, though why you
15390 		 * would want to switch to rack after all data is acknowledged
15391 		 * I have no idea (rrs)!
15392 		 */
15393 		return (EAGAIN);
15394 	}
15395 	if ((tp->t_flags & TF_SACK_PERMIT) || rack_sack_not_required){
15396 		return (0);
15397 	}
15398 	/*
15399 	 * If we reach here we don't do SACK on this connection so we can
15400 	 * never do rack.
15401 	 */
15402 	return (EINVAL);
15403 }
15404 
15405 static void
15406 rack_fini(struct tcpcb *tp, int32_t tcb_is_purged)
15407 {
15408 
15409 	if (tp->t_fb_ptr) {
15410 		uint32_t cnt_free = 0;
15411 		struct tcp_rack *rack;
15412 		struct rack_sendmap *rsm;
15413 
15414 		tcp_handle_orphaned_packets(tp);
15415 		tp->t_flags &= ~TF_FORCEDATA;
15416 		rack = (struct tcp_rack *)tp->t_fb_ptr;
15417 		rack_log_pacing_delay_calc(rack,
15418 					   0,
15419 					   0,
15420 					   0,
15421 					   rack_get_gp_est(rack), /* delRate */
15422 					   rack_get_lt_bw(rack), /* rttProp */
15423 					   20, __LINE__, NULL, 0);
15424 #ifdef NETFLIX_SHARED_CWND
15425 		if (rack->r_ctl.rc_scw) {
15426 			uint32_t limit;
15427 
15428 			if (rack->r_limit_scw)
15429 				limit = max(1, rack->r_ctl.rc_lowest_us_rtt);
15430 			else
15431 				limit = 0;
15432 			tcp_shared_cwnd_free_full(tp, rack->r_ctl.rc_scw,
15433 						  rack->r_ctl.rc_scw_index,
15434 						  limit);
15435 			rack->r_ctl.rc_scw = NULL;
15436 		}
15437 #endif
15438 		if (rack->r_ctl.fsb.tcp_ip_hdr) {
15439 			free(rack->r_ctl.fsb.tcp_ip_hdr, M_TCPFSB);
15440 			rack->r_ctl.fsb.tcp_ip_hdr = NULL;
15441 			rack->r_ctl.fsb.th = NULL;
15442 		}
15443 		if (rack->rc_always_pace) {
15444 			tcp_decrement_paced_conn();
15445 			rack_undo_cc_pacing(rack);
15446 			rack->rc_always_pace = 0;
15447 		}
15448 		/* Clean up any options if they were not applied */
15449 		while (!TAILQ_EMPTY(&rack->r_ctl.opt_list)) {
15450 			struct deferred_opt_list *dol;
15451 
15452 			dol = TAILQ_FIRST(&rack->r_ctl.opt_list);
15453 			TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next);
15454 			free(dol, M_TCPDO);
15455 		}
15456 		/* rack does not use force data but other stacks may clear it */
15457 		if (rack->r_ctl.crte != NULL) {
15458 			tcp_rel_pacing_rate(rack->r_ctl.crte, tp);
15459 			rack->rack_hdrw_pacing = 0;
15460 			rack->r_ctl.crte = NULL;
15461 		}
15462 #ifdef TCP_BLACKBOX
15463 		tcp_log_flowend(tp);
15464 #endif
15465 		/*
15466 		 * Lets take a different approach to purging just
15467 		 * get each one and free it like a cum-ack would and
15468 		 * not use a foreach loop.
15469 		 */
15470 		rsm = tqhash_min(rack->r_ctl.tqh);
15471 		while (rsm) {
15472 			tqhash_remove(rack->r_ctl.tqh, rsm, REMOVE_TYPE_CUMACK);
15473 			rack->r_ctl.rc_num_maps_alloced--;
15474 			uma_zfree(rack_zone, rsm);
15475 			rsm = tqhash_min(rack->r_ctl.tqh);
15476 		}
15477 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
15478 		while (rsm) {
15479 			TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
15480 			rack->r_ctl.rc_num_maps_alloced--;
15481 			rack->rc_free_cnt--;
15482 			cnt_free++;
15483 			uma_zfree(rack_zone, rsm);
15484 			rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
15485 		}
15486 		if ((rack->r_ctl.rc_num_maps_alloced > 0) &&
15487 		    (tcp_bblogging_on(tp))) {
15488 			union tcp_log_stackspecific log;
15489 			struct timeval tv;
15490 
15491 			memset(&log.u_bbr, 0, sizeof(log.u_bbr));
15492 			log.u_bbr.flex8 = 10;
15493 			log.u_bbr.flex1 = rack->r_ctl.rc_num_maps_alloced;
15494 			log.u_bbr.flex2 = rack->rc_free_cnt;
15495 			log.u_bbr.flex3 = cnt_free;
15496 			log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
15497 			rsm = tqhash_min(rack->r_ctl.tqh);
15498 			log.u_bbr.delRate = (uint64_t)rsm;
15499 			rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
15500 			log.u_bbr.cur_del_rate = (uint64_t)rsm;
15501 			log.u_bbr.timeStamp = tcp_get_usecs(&tv);
15502 			log.u_bbr.pkt_epoch = __LINE__;
15503 			(void)tcp_log_event(tp, NULL, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
15504 					     0, &log, false, NULL, NULL, 0, &tv);
15505 		}
15506 		KASSERT((rack->r_ctl.rc_num_maps_alloced == 0),
15507 			("rack:%p num_aloc:%u after freeing all?",
15508 			 rack,
15509 			 rack->r_ctl.rc_num_maps_alloced));
15510 		rack->rc_free_cnt = 0;
15511 		free(rack->r_ctl.tqh, M_TCPFSB);
15512 		rack->r_ctl.tqh = NULL;
15513 		uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
15514 		tp->t_fb_ptr = NULL;
15515 	}
15516 	/* Make sure snd_nxt is correctly set */
15517 	tp->snd_nxt = tp->snd_max;
15518 }
15519 
15520 static void
15521 rack_set_state(struct tcpcb *tp, struct tcp_rack *rack)
15522 {
15523 	if ((rack->r_state == TCPS_CLOSED) && (tp->t_state != TCPS_CLOSED)) {
15524 		rack->r_is_v6 = (tptoinpcb(tp)->inp_vflag & INP_IPV6) != 0;
15525 	}
15526 	switch (tp->t_state) {
15527 	case TCPS_SYN_SENT:
15528 		rack->r_state = TCPS_SYN_SENT;
15529 		rack->r_substate = rack_do_syn_sent;
15530 		break;
15531 	case TCPS_SYN_RECEIVED:
15532 		rack->r_state = TCPS_SYN_RECEIVED;
15533 		rack->r_substate = rack_do_syn_recv;
15534 		break;
15535 	case TCPS_ESTABLISHED:
15536 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
15537 		rack->r_state = TCPS_ESTABLISHED;
15538 		rack->r_substate = rack_do_established;
15539 		break;
15540 	case TCPS_CLOSE_WAIT:
15541 		rack->r_state = TCPS_CLOSE_WAIT;
15542 		rack->r_substate = rack_do_close_wait;
15543 		break;
15544 	case TCPS_FIN_WAIT_1:
15545 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
15546 		rack->r_state = TCPS_FIN_WAIT_1;
15547 		rack->r_substate = rack_do_fin_wait_1;
15548 		break;
15549 	case TCPS_CLOSING:
15550 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
15551 		rack->r_state = TCPS_CLOSING;
15552 		rack->r_substate = rack_do_closing;
15553 		break;
15554 	case TCPS_LAST_ACK:
15555 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
15556 		rack->r_state = TCPS_LAST_ACK;
15557 		rack->r_substate = rack_do_lastack;
15558 		break;
15559 	case TCPS_FIN_WAIT_2:
15560 		rack->r_state = TCPS_FIN_WAIT_2;
15561 		rack->r_substate = rack_do_fin_wait_2;
15562 		break;
15563 	case TCPS_LISTEN:
15564 	case TCPS_CLOSED:
15565 	case TCPS_TIME_WAIT:
15566 	default:
15567 		break;
15568 	};
15569 	if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
15570 		rack->rc_tp->t_flags2 |= TF2_MBUF_ACKCMP;
15571 
15572 }
15573 
15574 static void
15575 rack_timer_audit(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb)
15576 {
15577 	/*
15578 	 * We received an ack, and then did not
15579 	 * call send or were bounced out due to the
15580 	 * hpts was running. Now a timer is up as well, is
15581 	 * it the right timer?
15582 	 */
15583 	struct rack_sendmap *rsm;
15584 	int tmr_up;
15585 
15586 	tmr_up = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
15587 	if (rack->rc_in_persist && (tmr_up == PACE_TMR_PERSIT))
15588 		return;
15589 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
15590 	if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) &&
15591 	    (tmr_up == PACE_TMR_RXT)) {
15592 		/* Should be an RXT */
15593 		return;
15594 	}
15595 	if (rsm == NULL) {
15596 		/* Nothing outstanding? */
15597 		if (tp->t_flags & TF_DELACK) {
15598 			if (tmr_up == PACE_TMR_DELACK)
15599 				/* We are supposed to have delayed ack up and we do */
15600 				return;
15601 		} else if (sbavail(&tptosocket(tp)->so_snd) && (tmr_up == PACE_TMR_RXT)) {
15602 			/*
15603 			 * if we hit enobufs then we would expect the possibility
15604 			 * of nothing outstanding and the RXT up (and the hptsi timer).
15605 			 */
15606 			return;
15607 		} else if (((V_tcp_always_keepalive ||
15608 			     rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
15609 			    (tp->t_state <= TCPS_CLOSING)) &&
15610 			   (tmr_up == PACE_TMR_KEEP) &&
15611 			   (tp->snd_max == tp->snd_una)) {
15612 			/* We should have keep alive up and we do */
15613 			return;
15614 		}
15615 	}
15616 	if (SEQ_GT(tp->snd_max, tp->snd_una) &&
15617 		   ((tmr_up == PACE_TMR_TLP) ||
15618 		    (tmr_up == PACE_TMR_RACK) ||
15619 		    (tmr_up == PACE_TMR_RXT))) {
15620 		/*
15621 		 * Either a Rack, TLP or RXT is fine if  we
15622 		 * have outstanding data.
15623 		 */
15624 		return;
15625 	} else if (tmr_up == PACE_TMR_DELACK) {
15626 		/*
15627 		 * If the delayed ack was going to go off
15628 		 * before the rtx/tlp/rack timer were going to
15629 		 * expire, then that would be the timer in control.
15630 		 * Note we don't check the time here trusting the
15631 		 * code is correct.
15632 		 */
15633 		return;
15634 	}
15635 	/*
15636 	 * Ok the timer originally started is not what we want now.
15637 	 * We will force the hpts to be stopped if any, and restart
15638 	 * with the slot set to what was in the saved slot.
15639 	 */
15640 	if (tcp_in_hpts(rack->rc_tp)) {
15641 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
15642 			uint32_t us_cts;
15643 
15644 			us_cts = tcp_get_usecs(NULL);
15645 			if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
15646 				rack->r_early = 1;
15647 				rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
15648 			}
15649 			rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
15650 		}
15651 		tcp_hpts_remove(rack->rc_tp);
15652 	}
15653 	rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
15654 	rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
15655 }
15656 
15657 
15658 static void
15659 rack_do_win_updates(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tiwin, uint32_t seq, uint32_t ack, uint32_t cts)
15660 {
15661 	if ((SEQ_LT(tp->snd_wl1, seq) ||
15662 	    (tp->snd_wl1 == seq && (SEQ_LT(tp->snd_wl2, ack) ||
15663 	    (tp->snd_wl2 == ack && tiwin > tp->snd_wnd))))) {
15664 		/* keep track of pure window updates */
15665 		if ((tp->snd_wl2 == ack) && (tiwin > tp->snd_wnd))
15666 			KMOD_TCPSTAT_INC(tcps_rcvwinupd);
15667 		tp->snd_wnd = tiwin;
15668 		rack_validate_fo_sendwin_up(tp, rack);
15669 		tp->snd_wl1 = seq;
15670 		tp->snd_wl2 = ack;
15671 		if (tp->snd_wnd > tp->max_sndwnd)
15672 			tp->max_sndwnd = tp->snd_wnd;
15673 	    rack->r_wanted_output = 1;
15674 	} else if ((tp->snd_wl2 == ack) && (tiwin < tp->snd_wnd)) {
15675 		tp->snd_wnd = tiwin;
15676 		rack_validate_fo_sendwin_up(tp, rack);
15677 		tp->snd_wl1 = seq;
15678 		tp->snd_wl2 = ack;
15679 	} else {
15680 		/* Not a valid win update */
15681 		return;
15682 	}
15683 	if (tp->snd_wnd > tp->max_sndwnd)
15684 		tp->max_sndwnd = tp->snd_wnd;
15685 	/* Do we exit persists? */
15686 	if ((rack->rc_in_persist != 0) &&
15687 	    (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
15688 				rack->r_ctl.rc_pace_min_segs))) {
15689 		rack_exit_persist(tp, rack, cts);
15690 	}
15691 	/* Do we enter persists? */
15692 	if ((rack->rc_in_persist == 0) &&
15693 	    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
15694 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
15695 	    ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) &&
15696 	    sbavail(&tptosocket(tp)->so_snd) &&
15697 	    (sbavail(&tptosocket(tp)->so_snd) > tp->snd_wnd)) {
15698 		/*
15699 		 * Here the rwnd is less than
15700 		 * the pacing size, we are established,
15701 		 * nothing is outstanding, and there is
15702 		 * data to send. Enter persists.
15703 		 */
15704 		rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, ack);
15705 	}
15706 }
15707 
15708 static void
15709 rack_log_input_packet(struct tcpcb *tp, struct tcp_rack *rack, struct tcp_ackent *ae, int ackval, uint32_t high_seq)
15710 {
15711 
15712 	if (tcp_bblogging_on(rack->rc_tp)) {
15713 		struct inpcb *inp = tptoinpcb(tp);
15714 		union tcp_log_stackspecific log;
15715 		struct timeval ltv;
15716 		char tcp_hdr_buf[60];
15717 		struct tcphdr *th;
15718 		struct timespec ts;
15719 		uint32_t orig_snd_una;
15720 		uint8_t xx = 0;
15721 
15722 #ifdef TCP_REQUEST_TRK
15723 		struct tcp_sendfile_track *tcp_req;
15724 
15725 		if (SEQ_GT(ae->ack, tp->snd_una)) {
15726 			tcp_req = tcp_req_find_req_for_seq(tp, (ae->ack-1));
15727 		} else {
15728 			tcp_req = tcp_req_find_req_for_seq(tp, ae->ack);
15729 		}
15730 #endif
15731 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
15732 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
15733 		if (rack->rack_no_prr == 0)
15734 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
15735 		else
15736 			log.u_bbr.flex1 = 0;
15737 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
15738 		log.u_bbr.use_lt_bw <<= 1;
15739 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
15740 		log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced;
15741 		log.u_bbr.bbr_state = rack->rc_free_cnt;
15742 		log.u_bbr.inflight = ctf_flight_size(tp, rack->r_ctl.rc_sacked);
15743 		log.u_bbr.pkts_out = tp->t_maxseg;
15744 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
15745 		log.u_bbr.flex7 = 1;
15746 		log.u_bbr.lost = ae->flags;
15747 		log.u_bbr.cwnd_gain = ackval;
15748 		log.u_bbr.pacing_gain = 0x2;
15749 		if (ae->flags & TSTMP_HDWR) {
15750 			/* Record the hardware timestamp if present */
15751 			log.u_bbr.flex3 = M_TSTMP;
15752 			ts.tv_sec = ae->timestamp / 1000000000;
15753 			ts.tv_nsec = ae->timestamp % 1000000000;
15754 			ltv.tv_sec = ts.tv_sec;
15755 			ltv.tv_usec = ts.tv_nsec / 1000;
15756 			log.u_bbr.lt_epoch = tcp_tv_to_usectick(&ltv);
15757 		} else if (ae->flags & TSTMP_LRO) {
15758 			/* Record the LRO the arrival timestamp */
15759 			log.u_bbr.flex3 = M_TSTMP_LRO;
15760 			ts.tv_sec = ae->timestamp / 1000000000;
15761 			ts.tv_nsec = ae->timestamp % 1000000000;
15762 			ltv.tv_sec = ts.tv_sec;
15763 			ltv.tv_usec = ts.tv_nsec / 1000;
15764 			log.u_bbr.flex5 = tcp_tv_to_usectick(&ltv);
15765 		}
15766 		log.u_bbr.timeStamp = tcp_get_usecs(&ltv);
15767 		/* Log the rcv time */
15768 		log.u_bbr.delRate = ae->timestamp;
15769 #ifdef TCP_REQUEST_TRK
15770 		log.u_bbr.applimited = tp->t_tcpreq_closed;
15771 		log.u_bbr.applimited <<= 8;
15772 		log.u_bbr.applimited |= tp->t_tcpreq_open;
15773 		log.u_bbr.applimited <<= 8;
15774 		log.u_bbr.applimited |= tp->t_tcpreq_req;
15775 		if (tcp_req) {
15776 			/* Copy out any client req info */
15777 			/* seconds */
15778 			log.u_bbr.pkt_epoch = (tcp_req->localtime / HPTS_USEC_IN_SEC);
15779 			/* useconds */
15780 			log.u_bbr.delivered = (tcp_req->localtime % HPTS_USEC_IN_SEC);
15781 			log.u_bbr.rttProp = tcp_req->timestamp;
15782 			log.u_bbr.cur_del_rate = tcp_req->start;
15783 			if (tcp_req->flags & TCP_TRK_TRACK_FLG_OPEN) {
15784 				log.u_bbr.flex8 |= 1;
15785 			} else {
15786 				log.u_bbr.flex8 |= 2;
15787 				log.u_bbr.bw_inuse = tcp_req->end;
15788 			}
15789 			log.u_bbr.flex6 = tcp_req->start_seq;
15790 			if (tcp_req->flags & TCP_TRK_TRACK_FLG_COMP) {
15791 				log.u_bbr.flex8 |= 4;
15792 				log.u_bbr.epoch = tcp_req->end_seq;
15793 			}
15794 		}
15795 #endif
15796 		memset(tcp_hdr_buf, 0, sizeof(tcp_hdr_buf));
15797 		th = (struct tcphdr *)tcp_hdr_buf;
15798 		th->th_seq = ae->seq;
15799 		th->th_ack = ae->ack;
15800 		th->th_win = ae->win;
15801 		/* Now fill in the ports */
15802 		th->th_sport = inp->inp_fport;
15803 		th->th_dport = inp->inp_lport;
15804 		tcp_set_flags(th, ae->flags);
15805 		/* Now do we have a timestamp option? */
15806 		if (ae->flags & HAS_TSTMP) {
15807 			u_char *cp;
15808 			uint32_t val;
15809 
15810 			th->th_off = ((sizeof(struct tcphdr) + TCPOLEN_TSTAMP_APPA) >> 2);
15811 			cp = (u_char *)(th + 1);
15812 			*cp = TCPOPT_NOP;
15813 			cp++;
15814 			*cp = TCPOPT_NOP;
15815 			cp++;
15816 			*cp = TCPOPT_TIMESTAMP;
15817 			cp++;
15818 			*cp = TCPOLEN_TIMESTAMP;
15819 			cp++;
15820 			val = htonl(ae->ts_value);
15821 			bcopy((char *)&val,
15822 			      (char *)cp, sizeof(uint32_t));
15823 			val = htonl(ae->ts_echo);
15824 			bcopy((char *)&val,
15825 			      (char *)(cp + 4), sizeof(uint32_t));
15826 		} else
15827 			th->th_off = (sizeof(struct tcphdr) >> 2);
15828 
15829 		/*
15830 		 * For sane logging we need to play a little trick.
15831 		 * If the ack were fully processed we would have moved
15832 		 * snd_una to high_seq, but since compressed acks are
15833 		 * processed in two phases, at this point (logging) snd_una
15834 		 * won't be advanced. So we would see multiple acks showing
15835 		 * the advancement. We can prevent that by "pretending" that
15836 		 * snd_una was advanced and then un-advancing it so that the
15837 		 * logging code has the right value for tlb_snd_una.
15838 		 */
15839 		if (tp->snd_una != high_seq) {
15840 			orig_snd_una = tp->snd_una;
15841 			tp->snd_una = high_seq;
15842 			xx = 1;
15843 		} else
15844 			xx = 0;
15845 		TCP_LOG_EVENTP(tp, th,
15846 			       &tptosocket(tp)->so_rcv,
15847 			       &tptosocket(tp)->so_snd, TCP_LOG_IN, 0,
15848 			       0, &log, true, &ltv);
15849 		if (xx) {
15850 			tp->snd_una = orig_snd_una;
15851 		}
15852 	}
15853 
15854 }
15855 
15856 static void
15857 rack_handle_probe_response(struct tcp_rack *rack, uint32_t tiwin, uint32_t us_cts)
15858 {
15859 	uint32_t us_rtt;
15860 	/*
15861 	 * A persist or keep-alive was forced out, update our
15862 	 * min rtt time. Note now worry about lost responses.
15863 	 * When a subsequent keep-alive or persist times out
15864 	 * and forced_ack is still on, then the last probe
15865 	 * was not responded to. In such cases we have a
15866 	 * sysctl that controls the behavior. Either we apply
15867 	 * the rtt but with reduced confidence (0). Or we just
15868 	 * plain don't apply the rtt estimate. Having data flow
15869 	 * will clear the probe_not_answered flag i.e. cum-ack
15870 	 * move forward <or> exiting and reentering persists.
15871 	 */
15872 
15873 	rack->forced_ack = 0;
15874 	rack->rc_tp->t_rxtshift = 0;
15875 	if ((rack->rc_in_persist &&
15876 	     (tiwin == rack->rc_tp->snd_wnd)) ||
15877 	    (rack->rc_in_persist == 0)) {
15878 		/*
15879 		 * In persists only apply the RTT update if this is
15880 		 * a response to our window probe. And that
15881 		 * means the rwnd sent must match the current
15882 		 * snd_wnd. If it does not, then we got a
15883 		 * window update ack instead. For keepalive
15884 		 * we allow the answer no matter what the window.
15885 		 *
15886 		 * Note that if the probe_not_answered is set then
15887 		 * the forced_ack_ts is the oldest one i.e. the first
15888 		 * probe sent that might have been lost. This assures
15889 		 * us that if we do calculate an RTT it is longer not
15890 		 * some short thing.
15891 		 */
15892 		if (rack->rc_in_persist)
15893 			counter_u64_add(rack_persists_acks, 1);
15894 		us_rtt = us_cts - rack->r_ctl.forced_ack_ts;
15895 		if (us_rtt == 0)
15896 			us_rtt = 1;
15897 		if (rack->probe_not_answered == 0) {
15898 			rack_apply_updated_usrtt(rack, us_rtt, us_cts);
15899 			tcp_rack_xmit_timer(rack, us_rtt, 0, us_rtt, 3, NULL, 1);
15900 		} else {
15901 			/* We have a retransmitted probe here too */
15902 			if (rack_apply_rtt_with_reduced_conf) {
15903 				rack_apply_updated_usrtt(rack, us_rtt, us_cts);
15904 				tcp_rack_xmit_timer(rack, us_rtt, 0, us_rtt, 0, NULL, 1);
15905 			}
15906 		}
15907 	}
15908 }
15909 
15910 static int
15911 rack_do_compressed_ack_processing(struct tcpcb *tp, struct socket *so, struct mbuf *m, int nxt_pkt, struct timeval *tv)
15912 {
15913 	/*
15914 	 * Handle a "special" compressed ack mbuf. Each incoming
15915 	 * ack has only four possible dispositions:
15916 	 *
15917 	 * A) It moves the cum-ack forward
15918 	 * B) It is behind the cum-ack.
15919 	 * C) It is a window-update ack.
15920 	 * D) It is a dup-ack.
15921 	 *
15922 	 * Note that we can have between 1 -> TCP_COMP_ACK_ENTRIES
15923 	 * in the incoming mbuf. We also need to still pay attention
15924 	 * to nxt_pkt since there may be another packet after this
15925 	 * one.
15926 	 */
15927 #ifdef TCP_ACCOUNTING
15928 	uint64_t ts_val;
15929 	uint64_t rdstc;
15930 #endif
15931 	int segsiz;
15932 	struct timespec ts;
15933 	struct tcp_rack *rack;
15934 	struct tcp_ackent *ae;
15935 	uint32_t tiwin, ms_cts, cts, acked, acked_amount, high_seq, win_seq, the_win, win_upd_ack;
15936 	int cnt, i, did_out, ourfinisacked = 0;
15937 	struct tcpopt to_holder, *to = NULL;
15938 #ifdef TCP_ACCOUNTING
15939 	int win_up_req = 0;
15940 #endif
15941 	int nsegs = 0;
15942 	int under_pacing = 0;
15943 	int recovery = 0;
15944 #ifdef TCP_ACCOUNTING
15945 	sched_pin();
15946 #endif
15947 	rack = (struct tcp_rack *)tp->t_fb_ptr;
15948 	if (rack->gp_ready &&
15949 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT))
15950 		under_pacing = 1;
15951 
15952 	if (rack->r_state != tp->t_state)
15953 		rack_set_state(tp, rack);
15954 	if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
15955 	    (tp->t_flags & TF_GPUTINPROG)) {
15956 		/*
15957 		 * We have a goodput in progress
15958 		 * and we have entered a late state.
15959 		 * Do we have enough data in the sb
15960 		 * to handle the GPUT request?
15961 		 */
15962 		uint32_t bytes;
15963 
15964 		bytes = tp->gput_ack - tp->gput_seq;
15965 		if (SEQ_GT(tp->gput_seq, tp->snd_una))
15966 			bytes += tp->gput_seq - tp->snd_una;
15967 		if (bytes > sbavail(&tptosocket(tp)->so_snd)) {
15968 			/*
15969 			 * There are not enough bytes in the socket
15970 			 * buffer that have been sent to cover this
15971 			 * measurement. Cancel it.
15972 			 */
15973 			rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
15974 						   rack->r_ctl.rc_gp_srtt /*flex1*/,
15975 						   tp->gput_seq,
15976 						   0, 0, 18, __LINE__, NULL, 0);
15977 			tp->t_flags &= ~TF_GPUTINPROG;
15978 		}
15979 	}
15980 	to = &to_holder;
15981 	to->to_flags = 0;
15982 	KASSERT((m->m_len >= sizeof(struct tcp_ackent)),
15983 		("tp:%p m_cmpack:%p with invalid len:%u", tp, m, m->m_len));
15984 	cnt = m->m_len / sizeof(struct tcp_ackent);
15985 	counter_u64_add(rack_multi_single_eq, cnt);
15986 	high_seq = tp->snd_una;
15987 	the_win = tp->snd_wnd;
15988 	win_seq = tp->snd_wl1;
15989 	win_upd_ack = tp->snd_wl2;
15990 	cts = tcp_tv_to_usectick(tv);
15991 	ms_cts = tcp_tv_to_mssectick(tv);
15992 	rack->r_ctl.rc_rcvtime = cts;
15993 	segsiz = ctf_fixed_maxseg(tp);
15994 	if ((rack->rc_gp_dyn_mul) &&
15995 	    (rack->use_fixed_rate == 0) &&
15996 	    (rack->rc_always_pace)) {
15997 		/* Check in on probertt */
15998 		rack_check_probe_rtt(rack, cts);
15999 	}
16000 	for (i = 0; i < cnt; i++) {
16001 #ifdef TCP_ACCOUNTING
16002 		ts_val = get_cyclecount();
16003 #endif
16004 		rack_clear_rate_sample(rack);
16005 		ae = ((mtod(m, struct tcp_ackent *)) + i);
16006 		if (ae->flags & TH_FIN)
16007 			rack_log_pacing_delay_calc(rack,
16008 						   0,
16009 						   0,
16010 						   0,
16011 						   rack_get_gp_est(rack), /* delRate */
16012 						   rack_get_lt_bw(rack), /* rttProp */
16013 						   20, __LINE__, NULL, 0);
16014 		/* Setup the window */
16015 		tiwin = ae->win << tp->snd_scale;
16016 		if (tiwin > rack->r_ctl.rc_high_rwnd)
16017 			rack->r_ctl.rc_high_rwnd = tiwin;
16018 		/* figure out the type of ack */
16019 		if (SEQ_LT(ae->ack, high_seq)) {
16020 			/* Case B*/
16021 			ae->ack_val_set = ACK_BEHIND;
16022 		} else if (SEQ_GT(ae->ack, high_seq)) {
16023 			/* Case A */
16024 			ae->ack_val_set = ACK_CUMACK;
16025 		} else if ((tiwin == the_win) && (rack->rc_in_persist == 0)){
16026 			/* Case D */
16027 			ae->ack_val_set = ACK_DUPACK;
16028 		} else {
16029 			/* Case C */
16030 			ae->ack_val_set = ACK_RWND;
16031 		}
16032 		if (rack->sack_attack_disable > 0) {
16033 			rack_log_type_bbrsnd(rack, 0, 0, cts, tv, __LINE__);
16034 			rack->r_ctl.ack_during_sd++;
16035 		}
16036 		rack_log_input_packet(tp, rack, ae, ae->ack_val_set, high_seq);
16037 		/* Validate timestamp */
16038 		if (ae->flags & HAS_TSTMP) {
16039 			/* Setup for a timestamp */
16040 			to->to_flags = TOF_TS;
16041 			ae->ts_echo -= tp->ts_offset;
16042 			to->to_tsecr = ae->ts_echo;
16043 			to->to_tsval = ae->ts_value;
16044 			/*
16045 			 * If echoed timestamp is later than the current time, fall back to
16046 			 * non RFC1323 RTT calculation.  Normalize timestamp if syncookies
16047 			 * were used when this connection was established.
16048 			 */
16049 			if (TSTMP_GT(ae->ts_echo, ms_cts))
16050 				to->to_tsecr = 0;
16051 			if (tp->ts_recent &&
16052 			    TSTMP_LT(ae->ts_value, tp->ts_recent)) {
16053 				if (ctf_ts_check_ac(tp, (ae->flags & 0xff))) {
16054 #ifdef TCP_ACCOUNTING
16055 					rdstc = get_cyclecount();
16056 					if (rdstc > ts_val) {
16057 						if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16058 							tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val);
16059 						}
16060 					}
16061 #endif
16062 					continue;
16063 				}
16064 			}
16065 			if (SEQ_LEQ(ae->seq, tp->last_ack_sent) &&
16066 			    SEQ_LEQ(tp->last_ack_sent, ae->seq)) {
16067 				tp->ts_recent_age = tcp_ts_getticks();
16068 				tp->ts_recent = ae->ts_value;
16069 			}
16070 		} else {
16071 			/* Setup for a no options */
16072 			to->to_flags = 0;
16073 		}
16074 		/* Update the rcv time and perform idle reduction possibly */
16075 		if  (tp->t_idle_reduce &&
16076 		     (tp->snd_max == tp->snd_una) &&
16077 		     (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur)) {
16078 			counter_u64_add(rack_input_idle_reduces, 1);
16079 			rack_cc_after_idle(rack, tp);
16080 		}
16081 		tp->t_rcvtime = ticks;
16082 		/* Now what about ECN of a chain of pure ACKs? */
16083 		if (tcp_ecn_input_segment(tp, ae->flags, 0,
16084 			tcp_packets_this_ack(tp, ae->ack),
16085 			ae->codepoint))
16086 			rack_cong_signal(tp, CC_ECN, ae->ack, __LINE__);
16087 #ifdef TCP_ACCOUNTING
16088 		/* Count for the specific type of ack in */
16089 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16090 			tp->tcp_cnt_counters[ae->ack_val_set]++;
16091 		}
16092 #endif
16093 		/*
16094 		 * Note how we could move up these in the determination
16095 		 * above, but we don't so that way the timestamp checks (and ECN)
16096 		 * is done first before we do any processing on the ACK.
16097 		 * The non-compressed path through the code has this
16098 		 * weakness (noted by @jtl) that it actually does some
16099 		 * processing before verifying the timestamp information.
16100 		 * We don't take that path here which is why we set
16101 		 * the ack_val_set first, do the timestamp and ecn
16102 		 * processing, and then look at what we have setup.
16103 		 */
16104 		if (ae->ack_val_set == ACK_BEHIND) {
16105 			/*
16106 			 * Case B flag reordering, if window is not closed
16107 			 * or it could be a keep-alive or persists
16108 			 */
16109 			if (SEQ_LT(ae->ack, tp->snd_una) && (sbspace(&so->so_rcv) > segsiz)) {
16110 				rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
16111 				if (rack->r_ctl.rc_reorder_ts == 0)
16112 					rack->r_ctl.rc_reorder_ts = 1;
16113 			}
16114 		} else if (ae->ack_val_set == ACK_DUPACK) {
16115 			/* Case D */
16116 			rack_strike_dupack(rack);
16117 		} else if (ae->ack_val_set == ACK_RWND) {
16118 			/* Case C */
16119 			if ((ae->flags & TSTMP_LRO) || (ae->flags & TSTMP_HDWR)) {
16120 				ts.tv_sec = ae->timestamp / 1000000000;
16121 				ts.tv_nsec = ae->timestamp % 1000000000;
16122 				rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
16123 				rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
16124 			} else {
16125 				rack->r_ctl.act_rcv_time = *tv;
16126 			}
16127 			if (rack->forced_ack) {
16128 				rack_handle_probe_response(rack, tiwin,
16129 							   tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time));
16130 			}
16131 #ifdef TCP_ACCOUNTING
16132 			win_up_req = 1;
16133 #endif
16134 			win_upd_ack = ae->ack;
16135 			win_seq = ae->seq;
16136 			the_win = tiwin;
16137 			rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts);
16138 		} else {
16139 			/* Case A */
16140 			if (SEQ_GT(ae->ack, tp->snd_max)) {
16141 				/*
16142 				 * We just send an ack since the incoming
16143 				 * ack is beyond the largest seq we sent.
16144 				 */
16145 				if ((tp->t_flags & TF_ACKNOW) == 0) {
16146 					ctf_ack_war_checks(tp, &rack->r_ctl.challenge_ack_ts, &rack->r_ctl.challenge_ack_cnt);
16147 					if (tp->t_flags && TF_ACKNOW)
16148 						rack->r_wanted_output = 1;
16149 				}
16150 			} else {
16151 				nsegs++;
16152 				/* If the window changed setup to update */
16153 				if (tiwin != tp->snd_wnd) {
16154 					win_upd_ack = ae->ack;
16155 					win_seq = ae->seq;
16156 					the_win = tiwin;
16157 					rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts);
16158 				}
16159 #ifdef TCP_ACCOUNTING
16160 				/* Account for the acks */
16161 				if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16162 					tp->tcp_cnt_counters[CNT_OF_ACKS_IN] += (((ae->ack - high_seq) + segsiz - 1) / segsiz);
16163 				}
16164 #endif
16165 				high_seq = ae->ack;
16166 				if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp))
16167 					rack_log_hystart_event(rack, high_seq, 8);
16168 				/* Setup our act_rcv_time */
16169 				if ((ae->flags & TSTMP_LRO) || (ae->flags & TSTMP_HDWR)) {
16170 					ts.tv_sec = ae->timestamp / 1000000000;
16171 					ts.tv_nsec = ae->timestamp % 1000000000;
16172 					rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
16173 					rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
16174 				} else {
16175 					rack->r_ctl.act_rcv_time = *tv;
16176 				}
16177 				rack_process_to_cumack(tp, rack, ae->ack, cts, to,
16178 						       tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time));
16179 #ifdef TCP_REQUEST_TRK
16180 				rack_req_check_for_comp(rack, high_seq);
16181 #endif
16182 				if (rack->rc_dsack_round_seen) {
16183 					/* Is the dsack round over? */
16184 					if (SEQ_GEQ(ae->ack, rack->r_ctl.dsack_round_end)) {
16185 						/* Yes it is */
16186 						rack->rc_dsack_round_seen = 0;
16187 						rack_log_dsack_event(rack, 3, __LINE__, 0, 0);
16188 					}
16189 				}
16190 			}
16191 		}
16192 		/* And lets be sure to commit the rtt measurements for this ack */
16193 		tcp_rack_xmit_timer_commit(rack, tp);
16194 #ifdef TCP_ACCOUNTING
16195 		rdstc = get_cyclecount();
16196 		if (rdstc > ts_val) {
16197 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16198 				tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val);
16199 				if (ae->ack_val_set == ACK_CUMACK)
16200 					tp->tcp_proc_time[CYC_HANDLE_MAP] += (rdstc - ts_val);
16201 			}
16202 		}
16203 #endif
16204 	}
16205 #ifdef TCP_ACCOUNTING
16206 	ts_val = get_cyclecount();
16207 #endif
16208 	/* Tend to any collapsed window */
16209 	if (SEQ_GT(tp->snd_max, high_seq) && (tp->snd_wnd < (tp->snd_max - high_seq))) {
16210 		/* The peer collapsed the window */
16211 		rack_collapsed_window(rack, (tp->snd_max - high_seq), high_seq, __LINE__);
16212 	} else if (rack->rc_has_collapsed)
16213 		rack_un_collapse_window(rack, __LINE__);
16214 	if ((rack->r_collapse_point_valid) &&
16215 	    (SEQ_GT(high_seq, rack->r_ctl.high_collapse_point)))
16216 		rack->r_collapse_point_valid = 0;
16217 	acked_amount = acked = (high_seq - tp->snd_una);
16218 	if (acked) {
16219 		/*
16220 		 * The draft (v3) calls for us to use SEQ_GEQ, but that
16221 		 * causes issues when we are just going app limited. Lets
16222 		 * instead use SEQ_GT <or> where its equal but more data
16223 		 * is outstanding.
16224 		 *
16225 		 * Also make sure we are on the last ack of a series. We
16226 		 * have to have all the ack's processed in queue to know
16227 		 * if there is something left outstanding.
16228 		 *
16229 		 */
16230 		if (SEQ_GEQ(high_seq, rack->r_ctl.roundends) &&
16231 		    (rack->rc_new_rnd_needed == 0) &&
16232 		    (nxt_pkt == 0)) {
16233 			rack_log_hystart_event(rack, high_seq, 21);
16234 			rack->r_ctl.current_round++;
16235 			/* Force the next send to setup the next round */
16236 			rack->rc_new_rnd_needed = 1;
16237 			if (CC_ALGO(tp)->newround != NULL) {
16238 				CC_ALGO(tp)->newround(&tp->t_ccv, rack->r_ctl.current_round);
16239 			}
16240 		}
16241 		/*
16242 		 * Clear the probe not answered flag
16243 		 * since cum-ack moved forward.
16244 		 */
16245 		rack->probe_not_answered = 0;
16246 		if (rack->sack_attack_disable == 0)
16247 			rack_do_decay(rack);
16248 		if (acked >= segsiz) {
16249 			/*
16250 			 * You only get credit for
16251 			 * MSS and greater (and you get extra
16252 			 * credit for larger cum-ack moves).
16253 			 */
16254 			int ac;
16255 
16256 			ac = acked / segsiz;
16257 			rack->r_ctl.ack_count += ac;
16258 			counter_u64_add(rack_ack_total, ac);
16259 		}
16260 		if (rack->r_ctl.ack_count > 0xfff00000) {
16261 			/*
16262 			 * reduce the number to keep us under
16263 			 * a uint32_t.
16264 			 */
16265 			rack->r_ctl.ack_count /= 2;
16266 			rack->r_ctl.sack_count /= 2;
16267 		}
16268 		if (tp->t_flags & TF_NEEDSYN) {
16269 			/*
16270 			 * T/TCP: Connection was half-synchronized, and our SYN has
16271 			 * been ACK'd (so connection is now fully synchronized).  Go
16272 			 * to non-starred state, increment snd_una for ACK of SYN,
16273 			 * and check if we can do window scaling.
16274 			 */
16275 			tp->t_flags &= ~TF_NEEDSYN;
16276 			tp->snd_una++;
16277 			acked_amount = acked = (high_seq - tp->snd_una);
16278 		}
16279 		if (acked > sbavail(&so->so_snd))
16280 			acked_amount = sbavail(&so->so_snd);
16281 #ifdef TCP_SAD_DETECTION
16282 		/*
16283 		 * We only care on a cum-ack move if we are in a sack-disabled
16284 		 * state. We have already added in to the ack_count, and we never
16285 		 * would disable on a cum-ack move, so we only care to do the
16286 		 * detection if it may "undo" it, i.e. we were in disabled already.
16287 		 */
16288 		if (rack->sack_attack_disable)
16289 			rack_do_detection(tp, rack, acked_amount, segsiz);
16290 #endif
16291 		if (IN_FASTRECOVERY(tp->t_flags) &&
16292 		    (rack->rack_no_prr == 0))
16293 			rack_update_prr(tp, rack, acked_amount, high_seq);
16294 		if (IN_RECOVERY(tp->t_flags)) {
16295 			if (SEQ_LT(high_seq, tp->snd_recover) &&
16296 			    (SEQ_LT(high_seq, tp->snd_max))) {
16297 				tcp_rack_partialack(tp);
16298 			} else {
16299 				rack_post_recovery(tp, high_seq);
16300 				recovery = 1;
16301 			}
16302 		}
16303 		/* Handle the rack-log-ack part (sendmap) */
16304 		if ((sbused(&so->so_snd) == 0) &&
16305 		    (acked > acked_amount) &&
16306 		    (tp->t_state >= TCPS_FIN_WAIT_1) &&
16307 		    (tp->t_flags & TF_SENTFIN)) {
16308 			/*
16309 			 * We must be sure our fin
16310 			 * was sent and acked (we can be
16311 			 * in FIN_WAIT_1 without having
16312 			 * sent the fin).
16313 			 */
16314 			ourfinisacked = 1;
16315 			/*
16316 			 * Lets make sure snd_una is updated
16317 			 * since most likely acked_amount = 0 (it
16318 			 * should be).
16319 			 */
16320 			tp->snd_una = high_seq;
16321 		}
16322 		/* Did we make a RTO error? */
16323 		if ((tp->t_flags & TF_PREVVALID) &&
16324 		    ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
16325 			tp->t_flags &= ~TF_PREVVALID;
16326 			if (tp->t_rxtshift == 1 &&
16327 			    (int)(ticks - tp->t_badrxtwin) < 0)
16328 				rack_cong_signal(tp, CC_RTO_ERR, high_seq, __LINE__);
16329 		}
16330 		/* Handle the data in the socket buffer */
16331 		KMOD_TCPSTAT_ADD(tcps_rcvackpack, 1);
16332 		KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
16333 		if (acked_amount > 0) {
16334 			struct mbuf *mfree;
16335 
16336 			rack_ack_received(tp, rack, high_seq, nsegs, CC_ACK, recovery);
16337 			SOCKBUF_LOCK(&so->so_snd);
16338 			mfree = sbcut_locked(&so->so_snd, acked_amount);
16339 			tp->snd_una = high_seq;
16340 			/* Note we want to hold the sb lock through the sendmap adjust */
16341 			rack_adjust_sendmap_head(rack, &so->so_snd);
16342 			/* Wake up the socket if we have room to write more */
16343 			rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
16344 			sowwakeup_locked(so);
16345 			if ((recovery == 1) &&
16346 			    (rack->excess_rxt_on) &&
16347 			    (rack->r_cwnd_was_clamped == 0)) {
16348 				do_rack_excess_rxt(tp, rack);
16349 			} else if (rack->r_cwnd_was_clamped)
16350 				do_rack_check_for_unclamp(tp, rack);
16351 			m_freem(mfree);
16352 		}
16353 		/* update progress */
16354 		tp->t_acktime = ticks;
16355 		rack_log_progress_event(rack, tp, tp->t_acktime,
16356 					PROGRESS_UPDATE, __LINE__);
16357 		/* Clear out shifts and such */
16358 		tp->t_rxtshift = 0;
16359 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
16360 				   rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
16361 		rack->rc_tlp_in_progress = 0;
16362 		rack->r_ctl.rc_tlp_cnt_out = 0;
16363 		/* Send recover and snd_nxt must be dragged along */
16364 		if (SEQ_GT(tp->snd_una, tp->snd_recover))
16365 			tp->snd_recover = tp->snd_una;
16366 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
16367 			tp->snd_nxt = tp->snd_una;
16368 		/*
16369 		 * If the RXT timer is running we want to
16370 		 * stop it, so we can restart a TLP (or new RXT).
16371 		 */
16372 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
16373 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
16374 		tp->snd_wl2 = high_seq;
16375 		tp->t_dupacks = 0;
16376 		if (under_pacing &&
16377 		    (rack->use_fixed_rate == 0) &&
16378 		    (rack->in_probe_rtt == 0) &&
16379 		    rack->rc_gp_dyn_mul &&
16380 		    rack->rc_always_pace) {
16381 			/* Check if we are dragging bottom */
16382 			rack_check_bottom_drag(tp, rack, so);
16383 		}
16384 		if (tp->snd_una == tp->snd_max) {
16385 			tp->t_flags &= ~TF_PREVVALID;
16386 			rack->r_ctl.retran_during_recovery = 0;
16387 			rack->rc_suspicious = 0;
16388 			rack->r_ctl.dsack_byte_cnt = 0;
16389 			rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
16390 			if (rack->r_ctl.rc_went_idle_time == 0)
16391 				rack->r_ctl.rc_went_idle_time = 1;
16392 			rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
16393 			if (sbavail(&tptosocket(tp)->so_snd) == 0)
16394 				tp->t_acktime = 0;
16395 			/* Set so we might enter persists... */
16396 			rack->r_wanted_output = 1;
16397 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
16398 			sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
16399 			if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
16400 			    (sbavail(&so->so_snd) == 0) &&
16401 			    (tp->t_flags2 & TF2_DROP_AF_DATA)) {
16402 				/*
16403 				 * The socket was gone and the
16404 				 * peer sent data (not now in the past), time to
16405 				 * reset him.
16406 				 */
16407 				rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
16408 				/* tcp_close will kill the inp pre-log the Reset */
16409 				tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
16410 #ifdef TCP_ACCOUNTING
16411 				rdstc = get_cyclecount();
16412 				if (rdstc > ts_val) {
16413 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16414 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16415 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16416 					}
16417 				}
16418 #endif
16419 				m_freem(m);
16420 				tp = tcp_close(tp);
16421 				if (tp == NULL) {
16422 #ifdef TCP_ACCOUNTING
16423 					sched_unpin();
16424 #endif
16425 					return (1);
16426 				}
16427 				/*
16428 				 * We would normally do drop-with-reset which would
16429 				 * send back a reset. We can't since we don't have
16430 				 * all the needed bits. Instead lets arrange for
16431 				 * a call to tcp_output(). That way since we
16432 				 * are in the closed state we will generate a reset.
16433 				 *
16434 				 * Note if tcp_accounting is on we don't unpin since
16435 				 * we do that after the goto label.
16436 				 */
16437 				goto send_out_a_rst;
16438 			}
16439 			if ((sbused(&so->so_snd) == 0) &&
16440 			    (tp->t_state >= TCPS_FIN_WAIT_1) &&
16441 			    (tp->t_flags & TF_SENTFIN)) {
16442 				/*
16443 				 * If we can't receive any more data, then closing user can
16444 				 * proceed. Starting the timer is contrary to the
16445 				 * specification, but if we don't get a FIN we'll hang
16446 				 * forever.
16447 				 *
16448 				 */
16449 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
16450 					soisdisconnected(so);
16451 					tcp_timer_activate(tp, TT_2MSL,
16452 							   (tcp_fast_finwait2_recycle ?
16453 							    tcp_finwait2_timeout :
16454 							    TP_MAXIDLE(tp)));
16455 				}
16456 				if (ourfinisacked == 0) {
16457 					/*
16458 					 * We don't change to fin-wait-2 if we have our fin acked
16459 					 * which means we are probably in TCPS_CLOSING.
16460 					 */
16461 					tcp_state_change(tp, TCPS_FIN_WAIT_2);
16462 				}
16463 			}
16464 		}
16465 		/* Wake up the socket if we have room to write more */
16466 		if (sbavail(&so->so_snd)) {
16467 			rack->r_wanted_output = 1;
16468 			if (ctf_progress_timeout_check(tp, true)) {
16469 				rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
16470 							tp, tick, PROGRESS_DROP, __LINE__);
16471 				/*
16472 				 * We cheat here and don't send a RST, we should send one
16473 				 * when the pacer drops the connection.
16474 				 */
16475 #ifdef TCP_ACCOUNTING
16476 				rdstc = get_cyclecount();
16477 				if (rdstc > ts_val) {
16478 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16479 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16480 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16481 					}
16482 				}
16483 				sched_unpin();
16484 #endif
16485 				(void)tcp_drop(tp, ETIMEDOUT);
16486 				m_freem(m);
16487 				return (1);
16488 			}
16489 		}
16490 		if (ourfinisacked) {
16491 			switch(tp->t_state) {
16492 			case TCPS_CLOSING:
16493 #ifdef TCP_ACCOUNTING
16494 				rdstc = get_cyclecount();
16495 				if (rdstc > ts_val) {
16496 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16497 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16498 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16499 					}
16500 				}
16501 				sched_unpin();
16502 #endif
16503 				tcp_twstart(tp);
16504 				m_freem(m);
16505 				return (1);
16506 				break;
16507 			case TCPS_LAST_ACK:
16508 #ifdef TCP_ACCOUNTING
16509 				rdstc = get_cyclecount();
16510 				if (rdstc > ts_val) {
16511 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16512 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16513 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16514 					}
16515 				}
16516 				sched_unpin();
16517 #endif
16518 				tp = tcp_close(tp);
16519 				ctf_do_drop(m, tp);
16520 				return (1);
16521 				break;
16522 			case TCPS_FIN_WAIT_1:
16523 #ifdef TCP_ACCOUNTING
16524 				rdstc = get_cyclecount();
16525 				if (rdstc > ts_val) {
16526 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16527 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16528 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16529 					}
16530 				}
16531 #endif
16532 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
16533 					soisdisconnected(so);
16534 					tcp_timer_activate(tp, TT_2MSL,
16535 							   (tcp_fast_finwait2_recycle ?
16536 							    tcp_finwait2_timeout :
16537 							    TP_MAXIDLE(tp)));
16538 				}
16539 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
16540 				break;
16541 			default:
16542 				break;
16543 			}
16544 		}
16545 		if (rack->r_fast_output) {
16546 			/*
16547 			 * We re doing fast output.. can we expand that?
16548 			 */
16549 			rack_gain_for_fastoutput(rack, tp, so, acked_amount);
16550 		}
16551 #ifdef TCP_ACCOUNTING
16552 		rdstc = get_cyclecount();
16553 		if (rdstc > ts_val) {
16554 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16555 				tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16556 				tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16557 			}
16558 		}
16559 
16560 	} else if (win_up_req) {
16561 		rdstc = get_cyclecount();
16562 		if (rdstc > ts_val) {
16563 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16564 				tp->tcp_proc_time[ACK_RWND] += (rdstc - ts_val);
16565 			}
16566 		}
16567 #endif
16568 	}
16569 	/* Now is there a next packet, if so we are done */
16570 	m_freem(m);
16571 	did_out = 0;
16572 	if (nxt_pkt) {
16573 #ifdef TCP_ACCOUNTING
16574 		sched_unpin();
16575 #endif
16576 		rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 5, nsegs);
16577 		return (0);
16578 	}
16579 	rack_handle_might_revert(tp, rack);
16580 	ctf_calc_rwin(so, tp);
16581 	if ((rack->r_wanted_output != 0) || (rack->r_fast_output != 0)) {
16582 	send_out_a_rst:
16583 		if (tcp_output(tp) < 0) {
16584 #ifdef TCP_ACCOUNTING
16585 			sched_unpin();
16586 #endif
16587 			return (1);
16588 		}
16589 		did_out = 1;
16590 	}
16591 	if (tp->t_flags2 & TF2_HPTS_CALLS)
16592 		tp->t_flags2 &= ~TF2_HPTS_CALLS;
16593 	rack_free_trim(rack);
16594 #ifdef TCP_ACCOUNTING
16595 	sched_unpin();
16596 #endif
16597 	rack_timer_audit(tp, rack, &so->so_snd);
16598 	rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 6, nsegs);
16599 	return (0);
16600 }
16601 
16602 #define	TCP_LRO_TS_OPTION \
16603     ntohl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \
16604 	  (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)
16605 
16606 static int
16607 rack_do_segment_nounlock(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
16608     int32_t drop_hdrlen, int32_t tlen, uint8_t iptos, int32_t nxt_pkt,
16609     struct timeval *tv)
16610 {
16611 	struct inpcb *inp = tptoinpcb(tp);
16612 	struct socket *so = tptosocket(tp);
16613 #ifdef TCP_ACCOUNTING
16614 	uint64_t ts_val;
16615 #endif
16616 	int32_t thflags, retval, did_out = 0;
16617 	int32_t way_out = 0;
16618 	/*
16619 	 * cts - is the current time from tv (caller gets ts) in microseconds.
16620 	 * ms_cts - is the current time from tv in milliseconds.
16621 	 * us_cts - is the time that LRO or hardware actually got the packet in microseconds.
16622 	 */
16623 	uint32_t cts, us_cts, ms_cts;
16624 	uint32_t tiwin, high_seq;
16625 	struct timespec ts;
16626 	struct tcpopt to;
16627 	struct tcp_rack *rack;
16628 	struct rack_sendmap *rsm;
16629 	int32_t prev_state = 0;
16630 	int no_output = 0;
16631 	int slot_remaining = 0;
16632 #ifdef TCP_ACCOUNTING
16633 	int ack_val_set = 0xf;
16634 #endif
16635 	int nsegs;
16636 
16637 	NET_EPOCH_ASSERT();
16638 	INP_WLOCK_ASSERT(inp);
16639 
16640 	/*
16641 	 * tv passed from common code is from either M_TSTMP_LRO or
16642 	 * tcp_get_usecs() if no LRO m_pkthdr timestamp is present.
16643 	 */
16644 	rack = (struct tcp_rack *)tp->t_fb_ptr;
16645 	if (rack->rack_deferred_inited == 0) {
16646 		/*
16647 		 * If we are the connecting socket we will
16648 		 * hit rack_init() when no sequence numbers
16649 		 * are setup. This makes it so we must defer
16650 		 * some initialization. Call that now.
16651 		 */
16652 		rack_deferred_init(tp, rack);
16653 	}
16654 	/*
16655 	 * Check to see if we need to skip any output plans. This
16656 	 * can happen in the non-LRO path where we are pacing and
16657 	 * must process the ack coming in but need to defer sending
16658 	 * anything becase a pacing timer is running.
16659 	 */
16660 	us_cts = tcp_tv_to_usectick(tv);
16661 	if (m->m_flags & M_ACKCMP) {
16662 		/*
16663 		 * All compressed ack's are ack's by definition so
16664 		 * remove any ack required flag and then do the processing.
16665 		 */
16666 		rack->rc_ack_required = 0;
16667 		return (rack_do_compressed_ack_processing(tp, so, m, nxt_pkt, tv));
16668 	}
16669 	thflags = tcp_get_flags(th);
16670 	if ((rack->rc_always_pace == 1) &&
16671 	    (rack->rc_ack_can_sendout_data == 0) &&
16672 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
16673 	    (TSTMP_LT(us_cts, rack->r_ctl.rc_last_output_to))) {
16674 		/*
16675 		 * Ok conditions are right for queuing the packets
16676 		 * but we do have to check the flags in the inp, it
16677 		 * could be, if a sack is present, we want to be awoken and
16678 		 * so should process the packets.
16679 		 */
16680 		slot_remaining = rack->r_ctl.rc_last_output_to - us_cts;
16681 		if (rack->rc_tp->t_flags2 & TF2_DONT_SACK_QUEUE) {
16682 			no_output = 1;
16683 		} else {
16684 			/*
16685 			 * If there is no options, or just a
16686 			 * timestamp option, we will want to queue
16687 			 * the packets. This is the same that LRO does
16688 			 * and will need to change with accurate ECN.
16689 			 */
16690 			uint32_t *ts_ptr;
16691 			int optlen;
16692 
16693 			optlen = (th->th_off << 2) - sizeof(struct tcphdr);
16694 			ts_ptr = (uint32_t *)(th + 1);
16695 			if ((optlen == 0) ||
16696 			    ((optlen == TCPOLEN_TSTAMP_APPA) &&
16697 			     (*ts_ptr == TCP_LRO_TS_OPTION)))
16698 				no_output = 1;
16699 		}
16700 		if ((no_output == 1) && (slot_remaining < tcp_min_hptsi_time)) {
16701 			/*
16702 			 * It is unrealistic to think we can pace in less than
16703 			 * the minimum granularity of the pacer (def:250usec). So
16704 			 * if we have less than that time remaining we should go
16705 			 * ahead and allow output to be "early". We will attempt to
16706 			 * make up for it in any pacing time we try to apply on
16707 			 * the outbound packet.
16708 			 */
16709 			no_output = 0;
16710 		}
16711 	}
16712 	/*
16713 	 * If there is a RST or FIN lets dump out the bw
16714 	 * with a FIN the connection may go on but we
16715 	 * may not.
16716 	 */
16717 	if ((thflags & TH_FIN) || (thflags & TH_RST))
16718 		rack_log_pacing_delay_calc(rack,
16719 					   rack->r_ctl.gp_bw,
16720 					   0,
16721 					   0,
16722 					   rack_get_gp_est(rack), /* delRate */
16723 					   rack_get_lt_bw(rack), /* rttProp */
16724 					   20, __LINE__, NULL, 0);
16725 	if (m->m_flags & M_ACKCMP) {
16726 		panic("Impossible reach m has ackcmp? m:%p tp:%p", m, tp);
16727 	}
16728 	cts = tcp_tv_to_usectick(tv);
16729 	ms_cts =  tcp_tv_to_mssectick(tv);
16730 	nsegs = m->m_pkthdr.lro_nsegs;
16731 	counter_u64_add(rack_proc_non_comp_ack, 1);
16732 #ifdef TCP_ACCOUNTING
16733 	sched_pin();
16734 	if (thflags & TH_ACK)
16735 		ts_val = get_cyclecount();
16736 #endif
16737 	if ((m->m_flags & M_TSTMP) ||
16738 	    (m->m_flags & M_TSTMP_LRO)) {
16739 		mbuf_tstmp2timespec(m, &ts);
16740 		rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
16741 		rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
16742 	} else
16743 		rack->r_ctl.act_rcv_time = *tv;
16744 	kern_prefetch(rack, &prev_state);
16745 	prev_state = 0;
16746 	/*
16747 	 * Unscale the window into a 32-bit value. For the SYN_SENT state
16748 	 * the scale is zero.
16749 	 */
16750 	tiwin = th->th_win << tp->snd_scale;
16751 #ifdef TCP_ACCOUNTING
16752 	if (thflags & TH_ACK) {
16753 		/*
16754 		 * We have a tradeoff here. We can either do what we are
16755 		 * doing i.e. pinning to this CPU and then doing the accounting
16756 		 * <or> we could do a critical enter, setup the rdtsc and cpu
16757 		 * as in below, and then validate we are on the same CPU on
16758 		 * exit. I have choosen to not do the critical enter since
16759 		 * that often will gain you a context switch, and instead lock
16760 		 * us (line above this if) to the same CPU with sched_pin(). This
16761 		 * means we may be context switched out for a higher priority
16762 		 * interupt but we won't be moved to another CPU.
16763 		 *
16764 		 * If this occurs (which it won't very often since we most likely
16765 		 * are running this code in interupt context and only a higher
16766 		 * priority will bump us ... clock?) we will falsely add in
16767 		 * to the time the interupt processing time plus the ack processing
16768 		 * time. This is ok since its a rare event.
16769 		 */
16770 		ack_val_set = tcp_do_ack_accounting(tp, th, &to, tiwin,
16771 						    ctf_fixed_maxseg(tp));
16772 	}
16773 #endif
16774 	/*
16775 	 * Parse options on any incoming segment.
16776 	 */
16777 	memset(&to, 0, sizeof(to));
16778 	tcp_dooptions(&to, (u_char *)(th + 1),
16779 	    (th->th_off << 2) - sizeof(struct tcphdr),
16780 	    (thflags & TH_SYN) ? TO_SYN : 0);
16781 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
16782 	    __func__));
16783 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
16784 	    __func__));
16785 
16786 	if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
16787 	    (tp->t_flags & TF_GPUTINPROG)) {
16788 		/*
16789 		 * We have a goodput in progress
16790 		 * and we have entered a late state.
16791 		 * Do we have enough data in the sb
16792 		 * to handle the GPUT request?
16793 		 */
16794 		uint32_t bytes;
16795 
16796 		bytes = tp->gput_ack - tp->gput_seq;
16797 		if (SEQ_GT(tp->gput_seq, tp->snd_una))
16798 			bytes += tp->gput_seq - tp->snd_una;
16799 		if (bytes > sbavail(&tptosocket(tp)->so_snd)) {
16800 			/*
16801 			 * There are not enough bytes in the socket
16802 			 * buffer that have been sent to cover this
16803 			 * measurement. Cancel it.
16804 			 */
16805 			rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
16806 						   rack->r_ctl.rc_gp_srtt /*flex1*/,
16807 						   tp->gput_seq,
16808 						   0, 0, 18, __LINE__, NULL, 0);
16809 			tp->t_flags &= ~TF_GPUTINPROG;
16810 		}
16811 	}
16812 	high_seq = th->th_ack;
16813 	if (tcp_bblogging_on(rack->rc_tp)) {
16814 		union tcp_log_stackspecific log;
16815 		struct timeval ltv;
16816 #ifdef TCP_REQUEST_TRK
16817 		struct tcp_sendfile_track *tcp_req;
16818 
16819 		if (SEQ_GT(th->th_ack, tp->snd_una)) {
16820 			tcp_req = tcp_req_find_req_for_seq(tp, (th->th_ack-1));
16821 		} else {
16822 			tcp_req = tcp_req_find_req_for_seq(tp, th->th_ack);
16823 		}
16824 #endif
16825 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
16826 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
16827 		if (rack->rack_no_prr == 0)
16828 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
16829 		else
16830 			log.u_bbr.flex1 = 0;
16831 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
16832 		log.u_bbr.use_lt_bw <<= 1;
16833 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
16834 		log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced;
16835 		log.u_bbr.bbr_state = rack->rc_free_cnt;
16836 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
16837 		log.u_bbr.pkts_out = rack->rc_tp->t_maxseg;
16838 		log.u_bbr.flex3 = m->m_flags;
16839 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
16840 		log.u_bbr.lost = thflags;
16841 		log.u_bbr.pacing_gain = 0x1;
16842 #ifdef TCP_ACCOUNTING
16843 		log.u_bbr.cwnd_gain = ack_val_set;
16844 #endif
16845 		log.u_bbr.flex7 = 2;
16846 		if (m->m_flags & M_TSTMP) {
16847 			/* Record the hardware timestamp if present */
16848 			mbuf_tstmp2timespec(m, &ts);
16849 			ltv.tv_sec = ts.tv_sec;
16850 			ltv.tv_usec = ts.tv_nsec / 1000;
16851 			log.u_bbr.lt_epoch = tcp_tv_to_usectick(&ltv);
16852 		} else if (m->m_flags & M_TSTMP_LRO) {
16853 			/* Record the LRO the arrival timestamp */
16854 			mbuf_tstmp2timespec(m, &ts);
16855 			ltv.tv_sec = ts.tv_sec;
16856 			ltv.tv_usec = ts.tv_nsec / 1000;
16857 			log.u_bbr.flex5 = tcp_tv_to_usectick(&ltv);
16858 		}
16859 		log.u_bbr.timeStamp = tcp_get_usecs(&ltv);
16860 		/* Log the rcv time */
16861 		log.u_bbr.delRate = m->m_pkthdr.rcv_tstmp;
16862 #ifdef TCP_REQUEST_TRK
16863 		log.u_bbr.applimited = tp->t_tcpreq_closed;
16864 		log.u_bbr.applimited <<= 8;
16865 		log.u_bbr.applimited |= tp->t_tcpreq_open;
16866 		log.u_bbr.applimited <<= 8;
16867 		log.u_bbr.applimited |= tp->t_tcpreq_req;
16868 		if (tcp_req) {
16869 			/* Copy out any client req info */
16870 			/* seconds */
16871 			log.u_bbr.pkt_epoch = (tcp_req->localtime / HPTS_USEC_IN_SEC);
16872 			/* useconds */
16873 			log.u_bbr.delivered = (tcp_req->localtime % HPTS_USEC_IN_SEC);
16874 			log.u_bbr.rttProp = tcp_req->timestamp;
16875 			log.u_bbr.cur_del_rate = tcp_req->start;
16876 			if (tcp_req->flags & TCP_TRK_TRACK_FLG_OPEN) {
16877 				log.u_bbr.flex8 |= 1;
16878 			} else {
16879 				log.u_bbr.flex8 |= 2;
16880 				log.u_bbr.bw_inuse = tcp_req->end;
16881 			}
16882 			log.u_bbr.flex6 = tcp_req->start_seq;
16883 			if (tcp_req->flags & TCP_TRK_TRACK_FLG_COMP) {
16884 				log.u_bbr.flex8 |= 4;
16885 				log.u_bbr.epoch = tcp_req->end_seq;
16886 			}
16887 		}
16888 #endif
16889 		TCP_LOG_EVENTP(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
16890 		    tlen, &log, true, &ltv);
16891 	}
16892 	/* Remove ack required flag if set, we have one  */
16893 	if (thflags & TH_ACK)
16894 		rack->rc_ack_required = 0;
16895 	if (rack->sack_attack_disable > 0) {
16896 		rack->r_ctl.ack_during_sd++;
16897 		rack_log_type_bbrsnd(rack, 0, 0, cts, tv, __LINE__);
16898 	}
16899 	if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
16900 		way_out = 4;
16901 		retval = 0;
16902 		m_freem(m);
16903 		goto done_with_input;
16904 	}
16905 	/*
16906 	 * If a segment with the ACK-bit set arrives in the SYN-SENT state
16907 	 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9.
16908 	 */
16909 	if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
16910 	    (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
16911 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
16912 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
16913 #ifdef TCP_ACCOUNTING
16914 		sched_unpin();
16915 #endif
16916 		return (1);
16917 	}
16918 	/*
16919 	 * If timestamps were negotiated during SYN/ACK and a
16920 	 * segment without a timestamp is received, silently drop
16921 	 * the segment, unless it is a RST segment or missing timestamps are
16922 	 * tolerated.
16923 	 * See section 3.2 of RFC 7323.
16924 	 */
16925 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) &&
16926 	    ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) {
16927 		way_out = 5;
16928 		retval = 0;
16929 		m_freem(m);
16930 		goto done_with_input;
16931 	}
16932 
16933 	/*
16934 	 * Segment received on connection. Reset idle time and keep-alive
16935 	 * timer. XXX: This should be done after segment validation to
16936 	 * ignore broken/spoofed segs.
16937 	 */
16938 	if  (tp->t_idle_reduce &&
16939 	     (tp->snd_max == tp->snd_una) &&
16940 	     (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur)) {
16941 		counter_u64_add(rack_input_idle_reduces, 1);
16942 		rack_cc_after_idle(rack, tp);
16943 	}
16944 	tp->t_rcvtime = ticks;
16945 #ifdef STATS
16946 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
16947 #endif
16948 	if (tiwin > rack->r_ctl.rc_high_rwnd)
16949 		rack->r_ctl.rc_high_rwnd = tiwin;
16950 	/*
16951 	 * TCP ECN processing. XXXJTL: If we ever use ECN, we need to move
16952 	 * this to occur after we've validated the segment.
16953 	 */
16954 	if (tcp_ecn_input_segment(tp, thflags, tlen,
16955 	    tcp_packets_this_ack(tp, th->th_ack),
16956 	    iptos))
16957 		rack_cong_signal(tp, CC_ECN, th->th_ack, __LINE__);
16958 
16959 	/*
16960 	 * If echoed timestamp is later than the current time, fall back to
16961 	 * non RFC1323 RTT calculation.  Normalize timestamp if syncookies
16962 	 * were used when this connection was established.
16963 	 */
16964 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
16965 		to.to_tsecr -= tp->ts_offset;
16966 		if (TSTMP_GT(to.to_tsecr, ms_cts))
16967 			to.to_tsecr = 0;
16968 	}
16969 
16970 	/*
16971 	 * If its the first time in we need to take care of options and
16972 	 * verify we can do SACK for rack!
16973 	 */
16974 	if (rack->r_state == 0) {
16975 		/* Should be init'd by rack_init() */
16976 		KASSERT(rack->rc_inp != NULL,
16977 		    ("%s: rack->rc_inp unexpectedly NULL", __func__));
16978 		if (rack->rc_inp == NULL) {
16979 			rack->rc_inp = inp;
16980 		}
16981 
16982 		/*
16983 		 * Process options only when we get SYN/ACK back. The SYN
16984 		 * case for incoming connections is handled in tcp_syncache.
16985 		 * According to RFC1323 the window field in a SYN (i.e., a
16986 		 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX
16987 		 * this is traditional behavior, may need to be cleaned up.
16988 		 */
16989 		if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
16990 			/* Handle parallel SYN for ECN */
16991 			tcp_ecn_input_parallel_syn(tp, thflags, iptos);
16992 			if ((to.to_flags & TOF_SCALE) &&
16993 			    (tp->t_flags & TF_REQ_SCALE)) {
16994 				tp->t_flags |= TF_RCVD_SCALE;
16995 				tp->snd_scale = to.to_wscale;
16996 			} else
16997 				tp->t_flags &= ~TF_REQ_SCALE;
16998 			/*
16999 			 * Initial send window.  It will be updated with the
17000 			 * next incoming segment to the scaled value.
17001 			 */
17002 			tp->snd_wnd = th->th_win;
17003 			rack_validate_fo_sendwin_up(tp, rack);
17004 			if ((to.to_flags & TOF_TS) &&
17005 			    (tp->t_flags & TF_REQ_TSTMP)) {
17006 				tp->t_flags |= TF_RCVD_TSTMP;
17007 				tp->ts_recent = to.to_tsval;
17008 				tp->ts_recent_age = cts;
17009 			} else
17010 				tp->t_flags &= ~TF_REQ_TSTMP;
17011 			if (to.to_flags & TOF_MSS) {
17012 				tcp_mss(tp, to.to_mss);
17013 			}
17014 			if ((tp->t_flags & TF_SACK_PERMIT) &&
17015 			    (to.to_flags & TOF_SACKPERM) == 0)
17016 				tp->t_flags &= ~TF_SACK_PERMIT;
17017 			if (IS_FASTOPEN(tp->t_flags)) {
17018 				if (to.to_flags & TOF_FASTOPEN) {
17019 					uint16_t mss;
17020 
17021 					if (to.to_flags & TOF_MSS)
17022 						mss = to.to_mss;
17023 					else
17024 						if ((inp->inp_vflag & INP_IPV6) != 0)
17025 							mss = TCP6_MSS;
17026 						else
17027 							mss = TCP_MSS;
17028 					tcp_fastopen_update_cache(tp, mss,
17029 					    to.to_tfo_len, to.to_tfo_cookie);
17030 				} else
17031 					tcp_fastopen_disable_path(tp);
17032 			}
17033 		}
17034 		/*
17035 		 * At this point we are at the initial call. Here we decide
17036 		 * if we are doing RACK or not. We do this by seeing if
17037 		 * TF_SACK_PERMIT is set and the sack-not-required is clear.
17038 		 * The code now does do dup-ack counting so if you don't
17039 		 * switch back you won't get rack & TLP, but you will still
17040 		 * get this stack.
17041 		 */
17042 
17043 		if ((rack_sack_not_required == 0) &&
17044 		    ((tp->t_flags & TF_SACK_PERMIT) == 0)) {
17045 			tcp_switch_back_to_default(tp);
17046 			(*tp->t_fb->tfb_tcp_do_segment)(tp, m, th, drop_hdrlen,
17047 			    tlen, iptos);
17048 #ifdef TCP_ACCOUNTING
17049 			sched_unpin();
17050 #endif
17051 			return (1);
17052 		}
17053 		tcp_set_hpts(tp);
17054 		sack_filter_clear(&rack->r_ctl.rack_sf, th->th_ack);
17055 	}
17056 	if (thflags & TH_FIN)
17057 		tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
17058 	us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
17059 	if ((rack->rc_gp_dyn_mul) &&
17060 	    (rack->use_fixed_rate == 0) &&
17061 	    (rack->rc_always_pace)) {
17062 		/* Check in on probertt */
17063 		rack_check_probe_rtt(rack, us_cts);
17064 	}
17065 	rack_clear_rate_sample(rack);
17066 	if ((rack->forced_ack) &&
17067 	    ((tcp_get_flags(th) & TH_RST) == 0)) {
17068 		rack_handle_probe_response(rack, tiwin, us_cts);
17069 	}
17070 	/*
17071 	 * This is the one exception case where we set the rack state
17072 	 * always. All other times (timers etc) we must have a rack-state
17073 	 * set (so we assure we have done the checks above for SACK).
17074 	 */
17075 	rack->r_ctl.rc_rcvtime = cts;
17076 	if (rack->r_state != tp->t_state)
17077 		rack_set_state(tp, rack);
17078 	if (SEQ_GT(th->th_ack, tp->snd_una) &&
17079 	    (rsm = tqhash_min(rack->r_ctl.tqh)) != NULL)
17080 		kern_prefetch(rsm, &prev_state);
17081 	prev_state = rack->r_state;
17082 	if ((thflags & TH_RST) &&
17083 	    ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
17084 	      SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
17085 	     (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq))) {
17086 		/* The connection will be killed by a reset check the tracepoint */
17087 		tcp_trace_point(rack->rc_tp, TCP_TP_RESET_RCV);
17088 	}
17089 	retval = (*rack->r_substate) (m, th, so,
17090 	    tp, &to, drop_hdrlen,
17091 	    tlen, tiwin, thflags, nxt_pkt, iptos);
17092 	if (retval == 0) {
17093 		/*
17094 		 * If retval is 1 the tcb is unlocked and most likely the tp
17095 		 * is gone.
17096 		 */
17097 		INP_WLOCK_ASSERT(inp);
17098 		if ((rack->rc_gp_dyn_mul) &&
17099 		    (rack->rc_always_pace) &&
17100 		    (rack->use_fixed_rate == 0) &&
17101 		    rack->in_probe_rtt &&
17102 		    (rack->r_ctl.rc_time_probertt_starts == 0)) {
17103 			/*
17104 			 * If we are going for target, lets recheck before
17105 			 * we output.
17106 			 */
17107 			rack_check_probe_rtt(rack, us_cts);
17108 		}
17109 		if (rack->set_pacing_done_a_iw == 0) {
17110 			/* How much has been acked? */
17111 			if ((tp->snd_una - tp->iss) > (ctf_fixed_maxseg(tp) * 10)) {
17112 				/* We have enough to set in the pacing segment size */
17113 				rack->set_pacing_done_a_iw = 1;
17114 				rack_set_pace_segments(tp, rack, __LINE__, NULL);
17115 			}
17116 		}
17117 		tcp_rack_xmit_timer_commit(rack, tp);
17118 #ifdef TCP_ACCOUNTING
17119 		/*
17120 		 * If we set the ack_val_se to what ack processing we are doing
17121 		 * we also want to track how many cycles we burned. Note
17122 		 * the bits after tcp_output we let be "free". This is because
17123 		 * we are also tracking the tcp_output times as well. Note the
17124 		 * use of 0xf here since we only have 11 counter (0 - 0xa) and
17125 		 * 0xf cannot be returned and is what we initialize it too to
17126 		 * indicate we are not doing the tabulations.
17127 		 */
17128 		if (ack_val_set != 0xf) {
17129 			uint64_t crtsc;
17130 
17131 			crtsc = get_cyclecount();
17132 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17133 				tp->tcp_proc_time[ack_val_set] += (crtsc - ts_val);
17134 			}
17135 		}
17136 #endif
17137 		if ((nxt_pkt == 0) && (no_output == 0)) {
17138 			if ((rack->r_wanted_output != 0) || (rack->r_fast_output != 0)) {
17139 do_output_now:
17140 				if (tcp_output(tp) < 0) {
17141 #ifdef TCP_ACCOUNTING
17142 					sched_unpin();
17143 #endif
17144 					return (1);
17145 				}
17146 				did_out = 1;
17147 			}
17148 			rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
17149 			rack_free_trim(rack);
17150 		} else if ((no_output == 1) &&
17151 			   (nxt_pkt == 0)  &&
17152 			   (tcp_in_hpts(rack->rc_tp) == 0)) {
17153 			/*
17154 			 * We are not in hpts and we had a pacing timer up. Use
17155 			 * the remaining time (slot_remaining) to restart the timer.
17156 			 */
17157 			KASSERT ((slot_remaining != 0), ("slot remaining is zero for rack:%p tp:%p", rack, tp));
17158 			rack_start_hpts_timer(rack, tp, cts, slot_remaining, 0, 0);
17159 			rack_free_trim(rack);
17160 		}
17161 		/* Clear the flag, it may have been cleared by output but we may not have  */
17162 		if ((nxt_pkt == 0) && (tp->t_flags2 & TF2_HPTS_CALLS))
17163 			tp->t_flags2 &= ~TF2_HPTS_CALLS;
17164 		/* Update any rounds needed */
17165 		if (rack_verbose_logging &&  tcp_bblogging_on(rack->rc_tp))
17166 			rack_log_hystart_event(rack, high_seq, 8);
17167 		/*
17168 		 * The draft (v3) calls for us to use SEQ_GEQ, but that
17169 		 * causes issues when we are just going app limited. Lets
17170 		 * instead use SEQ_GT <or> where its equal but more data
17171 		 * is outstanding.
17172 		 *
17173 		 * Also make sure we are on the last ack of a series. We
17174 		 * have to have all the ack's processed in queue to know
17175 		 * if there is something left outstanding.
17176 		 */
17177 		if (SEQ_GEQ(tp->snd_una, rack->r_ctl.roundends) &&
17178 		    (rack->rc_new_rnd_needed == 0) &&
17179 		    (nxt_pkt == 0)) {
17180 			rack_log_hystart_event(rack, tp->snd_una, 21);
17181 			rack->r_ctl.current_round++;
17182 			/* Force the next send to setup the next round */
17183 			rack->rc_new_rnd_needed = 1;
17184 			if (CC_ALGO(tp)->newround != NULL) {
17185 				CC_ALGO(tp)->newround(&tp->t_ccv, rack->r_ctl.current_round);
17186 			}
17187 		}
17188 		if ((nxt_pkt == 0) &&
17189 		    ((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) &&
17190 		    (SEQ_GT(tp->snd_max, tp->snd_una) ||
17191 		     (tp->t_flags & TF_DELACK) ||
17192 		     ((V_tcp_always_keepalive || rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
17193 		      (tp->t_state <= TCPS_CLOSING)))) {
17194 			/* We could not send (probably in the hpts but stopped the timer earlier)? */
17195 			if ((tp->snd_max == tp->snd_una) &&
17196 			    ((tp->t_flags & TF_DELACK) == 0) &&
17197 			    (tcp_in_hpts(rack->rc_tp)) &&
17198 			    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
17199 				/* keep alive not needed if we are hptsi output yet */
17200 				;
17201 			} else {
17202 				int late = 0;
17203 				if (tcp_in_hpts(tp)) {
17204 					if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
17205 						us_cts = tcp_get_usecs(NULL);
17206 						if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
17207 							rack->r_early = 1;
17208 							rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
17209 						} else
17210 							late = 1;
17211 						rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
17212 					}
17213 					tcp_hpts_remove(tp);
17214 				}
17215 				if (late && (did_out == 0)) {
17216 					/*
17217 					 * We are late in the sending
17218 					 * and we did not call the output
17219 					 * (this probably should not happen).
17220 					 */
17221 					goto do_output_now;
17222 				}
17223 				rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
17224 			}
17225 			way_out = 1;
17226 		} else if (nxt_pkt == 0) {
17227 			/* Do we have the correct timer running? */
17228 			rack_timer_audit(tp, rack, &so->so_snd);
17229 			way_out = 2;
17230 		}
17231 	done_with_input:
17232 		rack_log_doseg_done(rack, cts, nxt_pkt, did_out, way_out, max(1, nsegs));
17233 		if (did_out)
17234 			rack->r_wanted_output = 0;
17235 	}
17236 #ifdef TCP_ACCOUNTING
17237 	sched_unpin();
17238 #endif
17239 	return (retval);
17240 }
17241 
17242 static void
17243 rack_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
17244     int32_t drop_hdrlen, int32_t tlen, uint8_t iptos)
17245 {
17246 	struct timeval tv;
17247 
17248 	/* First lets see if we have old packets */
17249 	if (!STAILQ_EMPTY(&tp->t_inqueue)) {
17250 		if (ctf_do_queued_segments(tp, 1)) {
17251 			m_freem(m);
17252 			return;
17253 		}
17254 	}
17255 	if (m->m_flags & M_TSTMP_LRO) {
17256 		mbuf_tstmp2timeval(m, &tv);
17257 	} else {
17258 		/* Should not be should we kassert instead? */
17259 		tcp_get_usecs(&tv);
17260 	}
17261 	if (rack_do_segment_nounlock(tp, m, th, drop_hdrlen, tlen, iptos, 0,
17262 	    &tv) == 0) {
17263 		INP_WUNLOCK(tptoinpcb(tp));
17264 	}
17265 }
17266 
17267 struct rack_sendmap *
17268 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tsused)
17269 {
17270 	struct rack_sendmap *rsm = NULL;
17271 	int32_t idx;
17272 	uint32_t srtt = 0, thresh = 0, ts_low = 0;
17273 	int no_sack = 0;
17274 
17275 	/* Return the next guy to be re-transmitted */
17276 	if (tqhash_empty(rack->r_ctl.tqh)) {
17277 		return (NULL);
17278 	}
17279 	if (tp->t_flags & TF_SENTFIN) {
17280 		/* retran the end FIN? */
17281 		return (NULL);
17282 	}
17283 	/* ok lets look at this one */
17284 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
17285 	if (rack->r_must_retran && rsm && (rsm->r_flags & RACK_MUST_RXT)) {
17286 		return (rsm);
17287 	}
17288 	if (rsm && ((rsm->r_flags & RACK_ACKED) == 0)) {
17289 		goto check_it;
17290 	}
17291 	rsm = rack_find_lowest_rsm(rack);
17292 	if (rsm == NULL) {
17293 		return (NULL);
17294 	}
17295 check_it:
17296 	if (((rack->rc_tp->t_flags & TF_SACK_PERMIT) == 0) ||
17297 	    (rack->sack_attack_disable > 0)) {
17298 		no_sack = 1;
17299 	}
17300 	if ((no_sack > 0) &&
17301 	    (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
17302 		/*
17303 		 * No sack so we automatically do the 3 strikes and
17304 		 * retransmit (no rack timer would be started).
17305 		 */
17306 		return (rsm);
17307 	}
17308 	if (rsm->r_flags & RACK_ACKED) {
17309 		return (NULL);
17310 	}
17311 	if (((rsm->r_flags & RACK_SACK_PASSED) == 0) &&
17312 	    (rsm->r_dupack < DUP_ACK_THRESHOLD)) {
17313 		/* Its not yet ready */
17314 		return (NULL);
17315 	}
17316 	srtt = rack_grab_rtt(tp, rack);
17317 	idx = rsm->r_rtr_cnt - 1;
17318 	ts_low = (uint32_t)rsm->r_tim_lastsent[idx];
17319 	thresh = rack_calc_thresh_rack(rack, srtt, tsused);
17320 	if ((tsused == ts_low) ||
17321 	    (TSTMP_LT(tsused, ts_low))) {
17322 		/* No time since sending */
17323 		return (NULL);
17324 	}
17325 	if ((tsused - ts_low) < thresh) {
17326 		/* It has not been long enough yet */
17327 		return (NULL);
17328 	}
17329 	if ((rsm->r_dupack >= DUP_ACK_THRESHOLD) ||
17330 	    ((rsm->r_flags & RACK_SACK_PASSED) &&
17331 	     (rack->sack_attack_disable == 0))) {
17332 		/*
17333 		 * We have passed the dup-ack threshold <or>
17334 		 * a SACK has indicated this is missing.
17335 		 * Note that if you are a declared attacker
17336 		 * it is only the dup-ack threshold that
17337 		 * will cause retransmits.
17338 		 */
17339 		/* log retransmit reason */
17340 		rack_log_retran_reason(rack, rsm, (tsused - ts_low), thresh, 1);
17341 		rack->r_fast_output = 0;
17342 		return (rsm);
17343 	}
17344 	return (NULL);
17345 }
17346 
17347 static void
17348 rack_log_pacing_delay_calc(struct tcp_rack *rack, uint32_t len, uint32_t slot,
17349 			   uint64_t bw_est, uint64_t bw, uint64_t len_time, int method,
17350 			   int line, struct rack_sendmap *rsm, uint8_t quality)
17351 {
17352 	if (tcp_bblogging_on(rack->rc_tp)) {
17353 		union tcp_log_stackspecific log;
17354 		struct timeval tv;
17355 
17356 		if (rack_verbose_logging == 0) {
17357 			/*
17358 			 * We are not verbose screen out all but
17359 			 * ones we always want.
17360 			 */
17361 			if ((method != 2) &&
17362 			    (method != 3) &&
17363 			    (method != 7) &&
17364 			    (method != 14) &&
17365 			    (method != 20)) {
17366 				return;
17367 			}
17368 		}
17369 		memset(&log, 0, sizeof(log));
17370 		log.u_bbr.flex1 = slot;
17371 		log.u_bbr.flex2 = len;
17372 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_min_segs;
17373 		log.u_bbr.flex4 = rack->r_ctl.rc_pace_max_segs;
17374 		log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ss;
17375 		log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_ca;
17376 		log.u_bbr.use_lt_bw = rack->rc_ack_can_sendout_data;
17377 		log.u_bbr.use_lt_bw <<= 1;
17378 		log.u_bbr.use_lt_bw |= rack->r_late;
17379 		log.u_bbr.use_lt_bw <<= 1;
17380 		log.u_bbr.use_lt_bw |= rack->r_early;
17381 		log.u_bbr.use_lt_bw <<= 1;
17382 		log.u_bbr.use_lt_bw |= rack->app_limited_needs_set;
17383 		log.u_bbr.use_lt_bw <<= 1;
17384 		log.u_bbr.use_lt_bw |= rack->rc_gp_filled;
17385 		log.u_bbr.use_lt_bw <<= 1;
17386 		log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt;
17387 		log.u_bbr.use_lt_bw <<= 1;
17388 		log.u_bbr.use_lt_bw |= rack->in_probe_rtt;
17389 		log.u_bbr.use_lt_bw <<= 1;
17390 		log.u_bbr.use_lt_bw |= rack->gp_ready;
17391 		log.u_bbr.pkt_epoch = line;
17392 		log.u_bbr.epoch = rack->r_ctl.rc_agg_delayed;
17393 		log.u_bbr.lt_epoch = rack->r_ctl.rc_agg_early;
17394 		log.u_bbr.applimited = rack->r_ctl.rack_per_of_gp_rec;
17395 		log.u_bbr.bw_inuse = bw_est;
17396 		log.u_bbr.delRate = bw;
17397 		if (rack->r_ctl.gp_bw == 0)
17398 			log.u_bbr.cur_del_rate = 0;
17399 		else
17400 			log.u_bbr.cur_del_rate = rack_get_bw(rack);
17401 		log.u_bbr.rttProp = len_time;
17402 		log.u_bbr.pkts_out = rack->r_ctl.rc_rack_min_rtt;
17403 		log.u_bbr.lost = rack->r_ctl.rc_probertt_sndmax_atexit;
17404 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm);
17405 		if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh) {
17406 			/* We are in slow start */
17407 			log.u_bbr.flex7 = 1;
17408 		} else {
17409 			/* we are on congestion avoidance */
17410 			log.u_bbr.flex7 = 0;
17411 		}
17412 		log.u_bbr.flex8 = method;
17413 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
17414 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
17415 		log.u_bbr.cwnd_gain = rack->rc_gp_saw_rec;
17416 		log.u_bbr.cwnd_gain <<= 1;
17417 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss;
17418 		log.u_bbr.cwnd_gain <<= 1;
17419 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca;
17420 		log.u_bbr.bbr_substate = quality;
17421 		log.u_bbr.bbr_state = rack->dgp_on;
17422 		log.u_bbr.bbr_state <<= 1;
17423 		log.u_bbr.bbr_state |= rack->r_fill_less_agg;
17424 		log.u_bbr.bbr_state <<= 1;
17425 		log.u_bbr.bbr_state |= rack->rc_pace_to_cwnd;
17426 		log.u_bbr.bbr_state <<= 2;
17427 		log.u_bbr.bbr_state |= rack->r_pacing_discount;
17428 		log.u_bbr.flex7 = ((rack->r_ctl.pacing_discount_amm << 1) | log.u_bbr.flex7);
17429 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
17430 		    &rack->rc_inp->inp_socket->so_rcv,
17431 		    &rack->rc_inp->inp_socket->so_snd,
17432 		    BBR_LOG_HPTSI_CALC, 0,
17433 		    0, &log, false, &tv);
17434 	}
17435 }
17436 
17437 static uint32_t
17438 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss)
17439 {
17440 	uint32_t new_tso, user_max, pace_one;
17441 
17442 	user_max = rack->rc_user_set_max_segs * mss;
17443 	if (rack->rc_force_max_seg) {
17444 		return (user_max);
17445 	}
17446 	if (rack->use_fixed_rate &&
17447 	    ((rack->r_ctl.crte == NULL) ||
17448 	     (bw != rack->r_ctl.crte->rate))) {
17449 		/* Use the user mss since we are not exactly matched */
17450 		return (user_max);
17451 	}
17452 	if (rack_pace_one_seg ||
17453 	    (rack->r_ctl.rc_user_set_min_segs == 1))
17454 		pace_one = 1;
17455 	else
17456 		pace_one = 0;
17457 
17458 	new_tso = tcp_get_pacing_burst_size_w_divisor(rack->rc_tp, bw, mss,
17459 		     pace_one, rack->r_ctl.crte, NULL, rack->r_ctl.pace_len_divisor);
17460 	if (new_tso > user_max)
17461 		new_tso = user_max;
17462 	if (rack->rc_hybrid_mode && rack->r_ctl.client_suggested_maxseg) {
17463 		if (((uint32_t)rack->r_ctl.client_suggested_maxseg * mss) > new_tso)
17464 			new_tso = (uint32_t)rack->r_ctl.client_suggested_maxseg * mss;
17465 	}
17466 	if (rack->r_ctl.rc_user_set_min_segs &&
17467 	    ((rack->r_ctl.rc_user_set_min_segs * mss) > new_tso))
17468 	    new_tso = rack->r_ctl.rc_user_set_min_segs * mss;
17469 	return (new_tso);
17470 }
17471 
17472 static uint64_t
17473 rack_arrive_at_discounted_rate(struct tcp_rack *rack, uint64_t window_input, uint32_t *rate_set, uint32_t *gain_b)
17474 {
17475 	uint64_t reduced_win;
17476 	uint32_t gain;
17477 
17478 	if (window_input < rc_init_window(rack)) {
17479 		/*
17480 		 * The cwnd is collapsed to
17481 		 * nearly zero, maybe because of a time-out?
17482 		 * Lets drop back to the lt-bw.
17483 		 */
17484 		reduced_win = rack_get_lt_bw(rack);
17485 		/* Set the flag so the caller knows its a rate and not a reduced window */
17486 		*rate_set = 1;
17487 		gain = 100;
17488 	} else if  (IN_RECOVERY(rack->rc_tp->t_flags)) {
17489 		/*
17490 		 * If we are in recover our cwnd needs to be less for
17491 		 * our pacing consideration.
17492 		 */
17493 		if (rack->rack_hibeta == 0) {
17494 			reduced_win = window_input / 2;
17495 			gain = 50;
17496 		} else {
17497 			reduced_win = window_input * rack->r_ctl.saved_hibeta;
17498 			reduced_win /= 100;
17499 			gain = rack->r_ctl.saved_hibeta;
17500 		}
17501 	} else {
17502 		/*
17503 		 * Apply Timely factor to increase/decrease the
17504 		 * amount we are pacing at.
17505 		 */
17506 		gain = rack_get_output_gain(rack, NULL);
17507 		if (gain > rack_gain_p5_ub) {
17508 			gain = rack_gain_p5_ub;
17509 		}
17510 		reduced_win = window_input * gain;
17511 		reduced_win /= 100;
17512 	}
17513 	if (gain_b != NULL)
17514 		*gain_b = gain;
17515 	/*
17516 	 * What is being returned here is a trimmed down
17517 	 * window values in all cases where rate_set is left
17518 	 * at 0. In one case we actually return the rate (lt_bw).
17519 	 * the "reduced_win" is returned as a slimmed down cwnd that
17520 	 * is then calculated by the caller into a rate when rate_set
17521 	 * is 0.
17522 	 */
17523 	return (reduced_win);
17524 }
17525 
17526 static int32_t
17527 pace_to_fill_cwnd(struct tcp_rack *rack, int32_t slot, uint32_t len, uint32_t segsiz, int *capped, uint64_t *rate_wanted, uint8_t non_paced)
17528 {
17529 	uint64_t lentim, fill_bw;
17530 
17531 	/* Lets first see if we are full, if so continue with normal rate */
17532 	rack->r_via_fill_cw = 0;
17533 	if (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.cwnd_to_use)
17534 		return (slot);
17535 	if ((ctf_outstanding(rack->rc_tp) + (segsiz-1)) > rack->rc_tp->snd_wnd)
17536 		return (slot);
17537 	if (rack->r_ctl.rc_last_us_rtt == 0)
17538 		return (slot);
17539 	if (rack->rc_pace_fill_if_rttin_range &&
17540 	    (rack->r_ctl.rc_last_us_rtt >=
17541 	     (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack->rtt_limit_mul))) {
17542 		/* The rtt is huge, N * smallest, lets not fill */
17543 		return (slot);
17544 	}
17545 	/*
17546 	 * first lets calculate the b/w based on the last us-rtt
17547 	 * and the the smallest send window.
17548 	 */
17549 	fill_bw = min(rack->rc_tp->snd_cwnd, rack->r_ctl.cwnd_to_use);
17550 	if (rack->rc_fillcw_apply_discount) {
17551 		uint32_t rate_set = 0;
17552 
17553 		fill_bw = rack_arrive_at_discounted_rate(rack, fill_bw, &rate_set, NULL);
17554 		if (rate_set) {
17555 			goto at_lt_bw;
17556 		}
17557 	}
17558 	/* Take the rwnd if its smaller */
17559 	if (fill_bw > rack->rc_tp->snd_wnd)
17560 		fill_bw = rack->rc_tp->snd_wnd;
17561 	/* Now lets make it into a b/w */
17562 	fill_bw *= (uint64_t)HPTS_USEC_IN_SEC;
17563 	fill_bw /= (uint64_t)rack->r_ctl.rc_last_us_rtt;
17564 at_lt_bw:
17565 	if (rack->r_fill_less_agg) {
17566 		/*
17567 		 * We want the average of the rate_wanted
17568 		 * and our fill-cw calculated bw. We also want
17569 		 * to cap any increase to be no more than
17570 		 * X times the lt_bw (where X is the rack_bw_multipler).
17571 		 */
17572 		uint64_t lt_bw, rate;
17573 
17574 		lt_bw = rack_get_lt_bw(rack);
17575 		if (lt_bw > *rate_wanted)
17576 			rate = lt_bw;
17577 		else
17578 			rate = *rate_wanted;
17579 		fill_bw += rate;
17580 		fill_bw /= 2;
17581 		if (rack_bw_multipler && (fill_bw > (rate * rack_bw_multipler))) {
17582 			fill_bw = rate * rack_bw_multipler;
17583 		}
17584 	}
17585 	/* We are below the min b/w */
17586 	if (non_paced)
17587 		*rate_wanted = fill_bw;
17588 	if ((fill_bw < RACK_MIN_BW) || (fill_bw < *rate_wanted))
17589 		return (slot);
17590 	rack->r_via_fill_cw = 1;
17591 	if (rack->r_rack_hw_rate_caps &&
17592 	    (rack->r_ctl.crte != NULL)) {
17593 		uint64_t high_rate;
17594 
17595 		high_rate = tcp_hw_highest_rate(rack->r_ctl.crte);
17596 		if (fill_bw > high_rate) {
17597 			/* We are capping bw at the highest rate table entry */
17598 			if (*rate_wanted > high_rate) {
17599 				/* The original rate was also capped */
17600 				rack->r_via_fill_cw = 0;
17601 			}
17602 			rack_log_hdwr_pacing(rack,
17603 					     fill_bw, high_rate, __LINE__,
17604 					     0, 3);
17605 			fill_bw = high_rate;
17606 			if (capped)
17607 				*capped = 1;
17608 		}
17609 	} else if ((rack->r_ctl.crte == NULL) &&
17610 		   (rack->rack_hdrw_pacing == 0) &&
17611 		   (rack->rack_hdw_pace_ena) &&
17612 		   rack->r_rack_hw_rate_caps &&
17613 		   (rack->rack_attempt_hdwr_pace == 0) &&
17614 		   (rack->rc_inp->inp_route.ro_nh != NULL) &&
17615 		   (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
17616 		/*
17617 		 * Ok we may have a first attempt that is greater than our top rate
17618 		 * lets check.
17619 		 */
17620 		uint64_t high_rate;
17621 
17622 		high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp);
17623 		if (high_rate) {
17624 			if (fill_bw > high_rate) {
17625 				fill_bw = high_rate;
17626 				if (capped)
17627 					*capped = 1;
17628 			}
17629 		}
17630 	}
17631 	if (rack->r_ctl.bw_rate_cap && (fill_bw > rack->r_ctl.bw_rate_cap)) {
17632 		if (rack->rc_hybrid_mode)
17633 			rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
17634 					   fill_bw, 0, 0, HYBRID_LOG_RATE_CAP, 2, NULL, __LINE__);
17635 		fill_bw = rack->r_ctl.bw_rate_cap;
17636 	}
17637 	/*
17638 	 * Ok fill_bw holds our mythical b/w to fill the cwnd
17639 	 * in an rtt (unless it was capped), what does that
17640 	 * time wise equate too?
17641 	 */
17642 	lentim = (uint64_t)(len) * (uint64_t)HPTS_USEC_IN_SEC;
17643 	lentim /= fill_bw;
17644 	*rate_wanted = fill_bw;
17645 	if (non_paced || (lentim < slot)) {
17646 		rack_log_pacing_delay_calc(rack, len, slot, fill_bw,
17647 					   0, lentim, 12, __LINE__, NULL, 0);
17648 		return ((int32_t)lentim);
17649 	} else
17650 		return (slot);
17651 }
17652 
17653 static int32_t
17654 rack_get_pacing_delay(struct tcp_rack *rack, struct tcpcb *tp, uint32_t len, struct rack_sendmap *rsm, uint32_t segsiz)
17655 {
17656 	uint64_t srtt;
17657 	int32_t slot = 0;
17658 	int32_t minslot = 0;
17659 	int can_start_hw_pacing = 1;
17660 	int err;
17661 	int pace_one;
17662 
17663 	if (rack_pace_one_seg ||
17664 	    (rack->r_ctl.rc_user_set_min_segs == 1))
17665 		pace_one = 1;
17666 	else
17667 		pace_one = 0;
17668 	if (rack->rc_always_pace == 0) {
17669 		/*
17670 		 * We use the most optimistic possible cwnd/srtt for
17671 		 * sending calculations. This will make our
17672 		 * calculation anticipate getting more through
17673 		 * quicker then possible. But thats ok we don't want
17674 		 * the peer to have a gap in data sending.
17675 		 */
17676 		uint64_t cwnd, tr_perms = 0;
17677 		int32_t reduce = 0;
17678 
17679 	old_method:
17680 		/*
17681 		 * We keep no precise pacing with the old method
17682 		 * instead we use the pacer to mitigate bursts.
17683 		 */
17684 		if (rack->r_ctl.rc_rack_min_rtt)
17685 			srtt = rack->r_ctl.rc_rack_min_rtt;
17686 		else
17687 			srtt = max(tp->t_srtt, 1);
17688 		if (rack->r_ctl.rc_rack_largest_cwnd)
17689 			cwnd = rack->r_ctl.rc_rack_largest_cwnd;
17690 		else
17691 			cwnd = rack->r_ctl.cwnd_to_use;
17692 		/* Inflate cwnd by 1000 so srtt of usecs is in ms */
17693 		tr_perms = (cwnd * 1000) / srtt;
17694 		if (tr_perms == 0) {
17695 			tr_perms = ctf_fixed_maxseg(tp);
17696 		}
17697 		/*
17698 		 * Calculate how long this will take to drain, if
17699 		 * the calculation comes out to zero, thats ok we
17700 		 * will use send_a_lot to possibly spin around for
17701 		 * more increasing tot_len_this_send to the point
17702 		 * that its going to require a pace, or we hit the
17703 		 * cwnd. Which in that case we are just waiting for
17704 		 * a ACK.
17705 		 */
17706 		slot = len / tr_perms;
17707 		/* Now do we reduce the time so we don't run dry? */
17708 		if (slot && rack_slot_reduction) {
17709 			reduce = (slot / rack_slot_reduction);
17710 			if (reduce < slot) {
17711 				slot -= reduce;
17712 			} else
17713 				slot = 0;
17714 		}
17715 		slot *= HPTS_USEC_IN_MSEC;
17716 		if (rack->rc_pace_to_cwnd) {
17717 			uint64_t rate_wanted = 0;
17718 
17719 			slot = pace_to_fill_cwnd(rack, slot, len, segsiz, NULL, &rate_wanted, 1);
17720 			rack->rc_ack_can_sendout_data = 1;
17721 			rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, 0, 0, 14, __LINE__, NULL, 0);
17722 		} else
17723 			rack_log_pacing_delay_calc(rack, len, slot, tr_perms, reduce, 0, 7, __LINE__, NULL, 0);
17724 		/*******************************************************/
17725 		/* RRS: We insert non-paced call to stats here for len */
17726 		/*******************************************************/
17727 	} else {
17728 		uint64_t bw_est, res, lentim, rate_wanted;
17729 		uint32_t segs, oh;
17730 		int capped = 0;
17731 		int prev_fill;
17732 
17733 		if ((rack->r_rr_config == 1) && rsm) {
17734 			return (rack->r_ctl.rc_min_to);
17735 		}
17736 		if (rack->use_fixed_rate) {
17737 			rate_wanted = bw_est = rack_get_fixed_pacing_bw(rack);
17738 		} else if ((rack->r_ctl.init_rate == 0) &&
17739 			   (rack->r_ctl.gp_bw == 0)) {
17740 			/* no way to yet do an estimate */
17741 			bw_est = rate_wanted = 0;
17742 		} else if (rack->dgp_on) {
17743 			bw_est = rack_get_bw(rack);
17744 			rate_wanted = rack_get_output_bw(rack, bw_est, rsm, &capped);
17745 		} else {
17746 			uint32_t gain, rate_set = 0;
17747 
17748 			rate_wanted = min(rack->rc_tp->snd_cwnd, rack->r_ctl.cwnd_to_use);
17749 			rate_wanted = rack_arrive_at_discounted_rate(rack, rate_wanted, &rate_set, &gain);
17750 			if (rate_set == 0) {
17751 				if (rate_wanted > rack->rc_tp->snd_wnd)
17752 					rate_wanted = rack->rc_tp->snd_wnd;
17753 				/* Now lets make it into a b/w */
17754 				rate_wanted *= (uint64_t)HPTS_USEC_IN_SEC;
17755 				rate_wanted /= (uint64_t)rack->r_ctl.rc_last_us_rtt;
17756 			}
17757 			bw_est = rate_wanted;
17758 			rack_log_pacing_delay_calc(rack, rack->rc_tp->snd_cwnd,
17759 						   rack->r_ctl.cwnd_to_use,
17760 						   rate_wanted, bw_est,
17761 						   rack->r_ctl.rc_last_us_rtt,
17762 						   88, __LINE__, NULL, gain);
17763 		}
17764 		if ((bw_est == 0) || (rate_wanted == 0) ||
17765 		    ((rack->gp_ready == 0) && (rack->use_fixed_rate == 0))) {
17766 			/*
17767 			 * No way yet to make a b/w estimate or
17768 			 * our raise is set incorrectly.
17769 			 */
17770 			goto old_method;
17771 		}
17772 		rack_rate_cap_bw(rack, &rate_wanted, &capped);
17773 		/* We need to account for all the overheads */
17774 		segs = (len + segsiz - 1) / segsiz;
17775 		/*
17776 		 * We need the diff between 1514 bytes (e-mtu with e-hdr)
17777 		 * and how much data we put in each packet. Yes this
17778 		 * means we may be off if we are larger than 1500 bytes
17779 		 * or smaller. But this just makes us more conservative.
17780 		 */
17781 
17782 		oh =  (tp->t_maxseg - segsiz) + sizeof(struct tcphdr);
17783 		if (rack->r_is_v6) {
17784 #ifdef INET6
17785 			oh += sizeof(struct ip6_hdr);
17786 #endif
17787 		} else {
17788 #ifdef INET
17789 			oh += sizeof(struct ip);
17790 #endif
17791 		}
17792 		/* We add a fixed 14 for the ethernet header */
17793 		oh += 14;
17794 		segs *= oh;
17795 		lentim = (uint64_t)(len + segs) * (uint64_t)HPTS_USEC_IN_SEC;
17796 		res = lentim / rate_wanted;
17797 		slot = (uint32_t)res;
17798 		if (rack_hw_rate_min &&
17799 		    (rate_wanted < rack_hw_rate_min)) {
17800 			can_start_hw_pacing = 0;
17801 			if (rack->r_ctl.crte) {
17802 				/*
17803 				 * Ok we need to release it, we
17804 				 * have fallen too low.
17805 				 */
17806 				tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17807 				rack->r_ctl.crte = NULL;
17808 				rack->rack_attempt_hdwr_pace = 0;
17809 				rack->rack_hdrw_pacing = 0;
17810 			}
17811 		}
17812 		if (rack->r_ctl.crte &&
17813 		    (tcp_hw_highest_rate(rack->r_ctl.crte) < rate_wanted)) {
17814 			/*
17815 			 * We want more than the hardware can give us,
17816 			 * don't start any hw pacing.
17817 			 */
17818 			can_start_hw_pacing = 0;
17819 			if (rack->r_rack_hw_rate_caps == 0) {
17820 				/*
17821 				 * Ok we need to release it, we
17822 				 * want more than the card can give us and
17823 				 * no rate cap is in place. Set it up so
17824 				 * when we want less we can retry.
17825 				 */
17826 				tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17827 				rack->r_ctl.crte = NULL;
17828 				rack->rack_attempt_hdwr_pace = 0;
17829 				rack->rack_hdrw_pacing = 0;
17830 			}
17831 		}
17832 		if ((rack->r_ctl.crte != NULL) && (rack->rc_inp->inp_snd_tag == NULL)) {
17833 			/*
17834 			 * We lost our rate somehow, this can happen
17835 			 * if the interface changed underneath us.
17836 			 */
17837 			tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17838 			rack->r_ctl.crte = NULL;
17839 			/* Lets re-allow attempting to setup pacing */
17840 			rack->rack_hdrw_pacing = 0;
17841 			rack->rack_attempt_hdwr_pace = 0;
17842 			rack_log_hdwr_pacing(rack,
17843 					     rate_wanted, bw_est, __LINE__,
17844 					     0, 6);
17845 		}
17846 		prev_fill = rack->r_via_fill_cw;
17847 		if ((rack->rc_pace_to_cwnd) &&
17848 		    (capped == 0) &&
17849 		    (rack->dgp_on == 1) &&
17850 		    (rack->use_fixed_rate == 0) &&
17851 		    (rack->in_probe_rtt == 0) &&
17852 		    (IN_FASTRECOVERY(rack->rc_tp->t_flags) == 0)) {
17853 			/*
17854 			 * We want to pace at our rate *or* faster to
17855 			 * fill the cwnd to the max if its not full.
17856 			 */
17857 			slot = pace_to_fill_cwnd(rack, slot, (len+segs), segsiz, &capped, &rate_wanted, 0);
17858 			/* Re-check to make sure we are not exceeding our max b/w */
17859 			if ((rack->r_ctl.crte != NULL) &&
17860 			    (tcp_hw_highest_rate(rack->r_ctl.crte) < rate_wanted)) {
17861 				/*
17862 				 * We want more than the hardware can give us,
17863 				 * don't start any hw pacing.
17864 				 */
17865 				can_start_hw_pacing = 0;
17866 				if (rack->r_rack_hw_rate_caps == 0) {
17867 					/*
17868 					 * Ok we need to release it, we
17869 					 * want more than the card can give us and
17870 					 * no rate cap is in place. Set it up so
17871 					 * when we want less we can retry.
17872 					 */
17873 					tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17874 					rack->r_ctl.crte = NULL;
17875 					rack->rack_attempt_hdwr_pace = 0;
17876 					rack->rack_hdrw_pacing = 0;
17877 					rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
17878 				}
17879 			}
17880 		}
17881 		if ((rack->rc_inp->inp_route.ro_nh != NULL) &&
17882 		    (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
17883 			if ((rack->rack_hdw_pace_ena) &&
17884 			    (can_start_hw_pacing > 0) &&
17885 			    (rack->rack_hdrw_pacing == 0) &&
17886 			    (rack->rack_attempt_hdwr_pace == 0)) {
17887 				/*
17888 				 * Lets attempt to turn on hardware pacing
17889 				 * if we can.
17890 				 */
17891 				rack->rack_attempt_hdwr_pace = 1;
17892 				rack->r_ctl.crte = tcp_set_pacing_rate(rack->rc_tp,
17893 								       rack->rc_inp->inp_route.ro_nh->nh_ifp,
17894 								       rate_wanted,
17895 								       RS_PACING_GEQ,
17896 								       &err, &rack->r_ctl.crte_prev_rate);
17897 				if (rack->r_ctl.crte) {
17898 					rack->rack_hdrw_pacing = 1;
17899 					rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size_w_divisor(tp, rate_wanted, segsiz,
17900 													   pace_one, rack->r_ctl.crte,
17901 													   NULL, rack->r_ctl.pace_len_divisor);
17902 					rack_log_hdwr_pacing(rack,
17903 							     rate_wanted, rack->r_ctl.crte->rate, __LINE__,
17904 							     err, 0);
17905 					rack->r_ctl.last_hw_bw_req = rate_wanted;
17906 				} else {
17907 					counter_u64_add(rack_hw_pace_init_fail, 1);
17908 				}
17909 			} else if (rack->rack_hdrw_pacing &&
17910 				   (rack->r_ctl.last_hw_bw_req != rate_wanted)) {
17911 				/* Do we need to adjust our rate? */
17912 				const struct tcp_hwrate_limit_table *nrte;
17913 
17914 				if (rack->r_up_only &&
17915 				    (rate_wanted < rack->r_ctl.crte->rate)) {
17916 					/**
17917 					 * We have four possible states here
17918 					 * having to do with the previous time
17919 					 * and this time.
17920 					 *   previous  |  this-time
17921 					 * A)     0      |     0   -- fill_cw not in the picture
17922 					 * B)     1      |     0   -- we were doing a fill-cw but now are not
17923 					 * C)     1      |     1   -- all rates from fill_cw
17924 					 * D)     0      |     1   -- we were doing non-fill and now we are filling
17925 					 *
17926 					 * For case A, C and D we don't allow a drop. But for
17927 					 * case B where we now our on our steady rate we do
17928 					 * allow a drop.
17929 					 *
17930 					 */
17931 					if (!((prev_fill == 1) && (rack->r_via_fill_cw == 0)))
17932 						goto done_w_hdwr;
17933 				}
17934 				if ((rate_wanted > rack->r_ctl.crte->rate) ||
17935 				    (rate_wanted <= rack->r_ctl.crte_prev_rate)) {
17936 					if (rack_hw_rate_to_low &&
17937 					    (bw_est < rack_hw_rate_to_low)) {
17938 						/*
17939 						 * The pacing rate is too low for hardware, but
17940 						 * do allow hardware pacing to be restarted.
17941 						 */
17942 						rack_log_hdwr_pacing(rack,
17943 								     bw_est, rack->r_ctl.crte->rate, __LINE__,
17944 								     0, 5);
17945 						tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17946 						rack->r_ctl.crte = NULL;
17947 						rack->rack_attempt_hdwr_pace = 0;
17948 						rack->rack_hdrw_pacing = 0;
17949 						rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
17950 						goto done_w_hdwr;
17951 					}
17952 					nrte = tcp_chg_pacing_rate(rack->r_ctl.crte,
17953 								   rack->rc_tp,
17954 								   rack->rc_inp->inp_route.ro_nh->nh_ifp,
17955 								   rate_wanted,
17956 								   RS_PACING_GEQ,
17957 								   &err, &rack->r_ctl.crte_prev_rate);
17958 					if (nrte == NULL) {
17959 						/*
17960 						 * Lost the rate, lets drop hardware pacing
17961 						 * period.
17962 						 */
17963 						rack->rack_hdrw_pacing = 0;
17964 						rack->r_ctl.crte = NULL;
17965 						rack_log_hdwr_pacing(rack,
17966 								     rate_wanted, 0, __LINE__,
17967 								     err, 1);
17968 						rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
17969 						counter_u64_add(rack_hw_pace_lost, 1);
17970 					} else if (nrte != rack->r_ctl.crte) {
17971 						rack->r_ctl.crte = nrte;
17972 						rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size_w_divisor(tp, rate_wanted,
17973 														   segsiz, pace_one, rack->r_ctl.crte,
17974 														   NULL, rack->r_ctl.pace_len_divisor);
17975 						rack_log_hdwr_pacing(rack,
17976 								     rate_wanted, rack->r_ctl.crte->rate, __LINE__,
17977 								     err, 2);
17978 						rack->r_ctl.last_hw_bw_req = rate_wanted;
17979 					}
17980 				} else {
17981 					/* We just need to adjust the segment size */
17982 					rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
17983 					rack_log_hdwr_pacing(rack,
17984 							     rate_wanted, rack->r_ctl.crte->rate, __LINE__,
17985 							     0, 4);
17986 					rack->r_ctl.last_hw_bw_req = rate_wanted;
17987 				}
17988 			}
17989 		}
17990 		if (minslot && (minslot > slot)) {
17991 			rack_log_pacing_delay_calc(rack, minslot, slot, rack->r_ctl.crte->rate, bw_est, lentim,
17992 						   98, __LINE__, NULL, 0);
17993 			slot = minslot;
17994 		}
17995 	done_w_hdwr:
17996 		if (rack_limit_time_with_srtt &&
17997 		    (rack->use_fixed_rate == 0) &&
17998 		    (rack->rack_hdrw_pacing == 0)) {
17999 			/*
18000 			 * Sanity check, we do not allow the pacing delay
18001 			 * to be longer than the SRTT of the path. If it is
18002 			 * a slow path, then adding a packet should increase
18003 			 * the RTT and compensate for this i.e. the srtt will
18004 			 * be greater so the allowed pacing time will be greater.
18005 			 *
18006 			 * Note this restriction is not for where a peak rate
18007 			 * is set, we are doing fixed pacing or hardware pacing.
18008 			 */
18009 			if (rack->rc_tp->t_srtt)
18010 				srtt = rack->rc_tp->t_srtt;
18011 			else
18012 				srtt = RACK_INITIAL_RTO * HPTS_USEC_IN_MSEC;	/* its in ms convert */
18013 			if (srtt < (uint64_t)slot) {
18014 				rack_log_pacing_delay_calc(rack, srtt, slot, rate_wanted, bw_est, lentim, 99, __LINE__, NULL, 0);
18015 				slot = srtt;
18016 			}
18017 		}
18018 		/*******************************************************************/
18019 		/* RRS: We insert paced call to stats here for len and rate_wanted */
18020 		/*******************************************************************/
18021 		rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, bw_est, lentim, 2, __LINE__, rsm, 0);
18022 	}
18023 	if (rack->r_ctl.crte && (rack->r_ctl.crte->rs_num_enobufs > 0)) {
18024 		/*
18025 		 * If this rate is seeing enobufs when it
18026 		 * goes to send then either the nic is out
18027 		 * of gas or we are mis-estimating the time
18028 		 * somehow and not letting the queue empty
18029 		 * completely. Lets add to the pacing time.
18030 		 */
18031 		int hw_boost_delay;
18032 
18033 		hw_boost_delay = rack->r_ctl.crte->time_between * rack_enobuf_hw_boost_mult;
18034 		if (hw_boost_delay > rack_enobuf_hw_max)
18035 			hw_boost_delay = rack_enobuf_hw_max;
18036 		else if (hw_boost_delay < rack_enobuf_hw_min)
18037 			hw_boost_delay = rack_enobuf_hw_min;
18038 		slot += hw_boost_delay;
18039 	}
18040 	return (slot);
18041 }
18042 
18043 static void
18044 rack_start_gp_measurement(struct tcpcb *tp, struct tcp_rack *rack,
18045     tcp_seq startseq, uint32_t sb_offset)
18046 {
18047 	struct rack_sendmap *my_rsm = NULL;
18048 
18049 	if (tp->t_state < TCPS_ESTABLISHED) {
18050 		/*
18051 		 * We don't start any measurements if we are
18052 		 * not at least established.
18053 		 */
18054 		return;
18055 	}
18056 	if (tp->t_state >= TCPS_FIN_WAIT_1) {
18057 		/*
18058 		 * We will get no more data into the SB
18059 		 * this means we need to have the data available
18060 		 * before we start a measurement.
18061 		 */
18062 
18063 		if (sbavail(&tptosocket(tp)->so_snd) <
18064 		    max(rc_init_window(rack),
18065 			(MIN_GP_WIN * ctf_fixed_maxseg(tp)))) {
18066 			/* Nope not enough data */
18067 			return;
18068 		}
18069 	}
18070 	tp->t_flags |= TF_GPUTINPROG;
18071 	rack->r_ctl.rc_gp_cumack_ts = 0;
18072 	rack->r_ctl.rc_gp_lowrtt = 0xffffffff;
18073 	rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
18074 	tp->gput_seq = startseq;
18075 	rack->app_limited_needs_set = 0;
18076 	if (rack->in_probe_rtt)
18077 		rack->measure_saw_probe_rtt = 1;
18078 	else if ((rack->measure_saw_probe_rtt) &&
18079 		 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
18080 		rack->measure_saw_probe_rtt = 0;
18081 	if (rack->rc_gp_filled)
18082 		tp->gput_ts = rack->r_ctl.last_cumack_advance;
18083 	else {
18084 		/* Special case initial measurement */
18085 		struct timeval tv;
18086 
18087 		tp->gput_ts = tcp_get_usecs(&tv);
18088 		rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
18089 	}
18090 	/*
18091 	 * We take a guess out into the future,
18092 	 * if we have no measurement and no
18093 	 * initial rate, we measure the first
18094 	 * initial-windows worth of data to
18095 	 * speed up getting some GP measurement and
18096 	 * thus start pacing.
18097 	 */
18098 	if ((rack->rc_gp_filled == 0) && (rack->r_ctl.init_rate == 0)) {
18099 		rack->app_limited_needs_set = 1;
18100 		tp->gput_ack = startseq + max(rc_init_window(rack),
18101 					      (MIN_GP_WIN * ctf_fixed_maxseg(tp)));
18102 		rack_log_pacing_delay_calc(rack,
18103 					   tp->gput_seq,
18104 					   tp->gput_ack,
18105 					   0,
18106 					   tp->gput_ts,
18107 					   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
18108 					   9,
18109 					   __LINE__, NULL, 0);
18110 		rack_tend_gp_marks(tp, rack);
18111 		rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
18112 		return;
18113 	}
18114 	if (sb_offset) {
18115 		/*
18116 		 * We are out somewhere in the sb
18117 		 * can we use the already outstanding data?
18118 		 */
18119 
18120 		if (rack->r_ctl.rc_app_limited_cnt == 0) {
18121 			/*
18122 			 * Yes first one is good and in this case
18123 			 * the tp->gput_ts is correctly set based on
18124 			 * the last ack that arrived (no need to
18125 			 * set things up when an ack comes in).
18126 			 */
18127 			my_rsm = tqhash_min(rack->r_ctl.tqh);
18128 			if ((my_rsm == NULL) ||
18129 			    (my_rsm->r_rtr_cnt != 1)) {
18130 				/* retransmission? */
18131 				goto use_latest;
18132 			}
18133 		} else {
18134 			if (rack->r_ctl.rc_first_appl == NULL) {
18135 				/*
18136 				 * If rc_first_appl is NULL
18137 				 * then the cnt should be 0.
18138 				 * This is probably an error, maybe
18139 				 * a KASSERT would be approprate.
18140 				 */
18141 				goto use_latest;
18142 			}
18143 			/*
18144 			 * If we have a marker pointer to the last one that is
18145 			 * app limited we can use that, but we need to set
18146 			 * things up so that when it gets ack'ed we record
18147 			 * the ack time (if its not already acked).
18148 			 */
18149 			rack->app_limited_needs_set = 1;
18150 			/*
18151 			 * We want to get to the rsm that is either
18152 			 * next with space i.e. over 1 MSS or the one
18153 			 * after that (after the app-limited).
18154 			 */
18155 			my_rsm = tqhash_next(rack->r_ctl.tqh, rack->r_ctl.rc_first_appl);
18156 			if (my_rsm) {
18157 				if ((my_rsm->r_end - my_rsm->r_start) <= ctf_fixed_maxseg(tp))
18158 					/* Have to use the next one */
18159 					my_rsm = tqhash_next(rack->r_ctl.tqh, my_rsm);
18160 				else {
18161 					/* Use after the first MSS of it is acked */
18162 					tp->gput_seq = my_rsm->r_start + ctf_fixed_maxseg(tp);
18163 					goto start_set;
18164 				}
18165 			}
18166 			if ((my_rsm == NULL) ||
18167 			    (my_rsm->r_rtr_cnt != 1)) {
18168 				/*
18169 				 * Either its a retransmit or
18170 				 * the last is the app-limited one.
18171 				 */
18172 				goto use_latest;
18173 			}
18174 		}
18175 		tp->gput_seq = my_rsm->r_start;
18176 start_set:
18177 		if (my_rsm->r_flags & RACK_ACKED) {
18178 			/*
18179 			 * This one has been acked use the arrival ack time
18180 			 */
18181 			struct rack_sendmap *nrsm;
18182 
18183 			tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival;
18184 			rack->app_limited_needs_set = 0;
18185 			/*
18186 			 * Ok in this path we need to use the r_end now
18187 			 * since this guy is the starting ack.
18188 			 */
18189 			tp->gput_seq = my_rsm->r_end;
18190 			/*
18191 			 * We also need to adjust up the sendtime
18192 			 * to the send of the next data after my_rsm.
18193 			 */
18194 			nrsm = tqhash_next(rack->r_ctl.tqh, my_rsm);
18195 			if (nrsm != NULL)
18196 				my_rsm = nrsm;
18197 			else {
18198 				/*
18199 				 * The next as not been sent, thats the
18200 				 * case for using the latest.
18201 				 */
18202 				goto use_latest;
18203 			}
18204 		}
18205 		rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[0];
18206 		tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
18207 		rack->r_ctl.rc_gp_cumack_ts = 0;
18208 		rack_log_pacing_delay_calc(rack,
18209 					   tp->gput_seq,
18210 					   tp->gput_ack,
18211 					   (uint64_t)my_rsm,
18212 					   tp->gput_ts,
18213 					   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
18214 					   9,
18215 					   __LINE__, my_rsm, 0);
18216 		/* Now lets make sure all are marked as they should be */
18217 		rack_tend_gp_marks(tp, rack);
18218 		rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
18219 		return;
18220 	}
18221 
18222 use_latest:
18223 	/*
18224 	 * We don't know how long we may have been
18225 	 * idle or if this is the first-send. Lets
18226 	 * setup the flag so we will trim off
18227 	 * the first ack'd data so we get a true
18228 	 * measurement.
18229 	 */
18230 	rack->app_limited_needs_set = 1;
18231 	tp->gput_ack = startseq + rack_get_measure_window(tp, rack);
18232 	rack->r_ctl.rc_gp_cumack_ts = 0;
18233 	/* Find this guy so we can pull the send time */
18234 	my_rsm = tqhash_find(rack->r_ctl.tqh, startseq);
18235 	if (my_rsm) {
18236 		rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[0];
18237 		if (my_rsm->r_flags & RACK_ACKED) {
18238 			/*
18239 			 * Unlikely since its probably what was
18240 			 * just transmitted (but I am paranoid).
18241 			 */
18242 			tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival;
18243 			rack->app_limited_needs_set = 0;
18244 		}
18245 		if (SEQ_LT(my_rsm->r_start, tp->gput_seq)) {
18246 			/* This also is unlikely */
18247 			tp->gput_seq = my_rsm->r_start;
18248 		}
18249 	} else {
18250 		/*
18251 		 * TSNH unless we have some send-map limit,
18252 		 * and even at that it should not be hitting
18253 		 * that limit (we should have stopped sending).
18254 		 */
18255 		struct timeval tv;
18256 
18257 		microuptime(&tv);
18258 		rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
18259 	}
18260 	rack_tend_gp_marks(tp, rack);
18261 	rack_log_pacing_delay_calc(rack,
18262 				   tp->gput_seq,
18263 				   tp->gput_ack,
18264 				   (uint64_t)my_rsm,
18265 				   tp->gput_ts,
18266 				   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
18267 				   9, __LINE__, NULL, 0);
18268 	rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
18269 }
18270 
18271 static inline uint32_t
18272 rack_what_can_we_send(struct tcpcb *tp, struct tcp_rack *rack,  uint32_t cwnd_to_use,
18273     uint32_t avail, int32_t sb_offset)
18274 {
18275 	uint32_t len;
18276 	uint32_t sendwin;
18277 
18278 	if (tp->snd_wnd > cwnd_to_use)
18279 		sendwin = cwnd_to_use;
18280 	else
18281 		sendwin = tp->snd_wnd;
18282 	if (ctf_outstanding(tp) >= tp->snd_wnd) {
18283 		/* We never want to go over our peers rcv-window */
18284 		len = 0;
18285 	} else {
18286 		uint32_t flight;
18287 
18288 		flight = ctf_flight_size(tp, rack->r_ctl.rc_sacked);
18289 		if (flight >= sendwin) {
18290 			/*
18291 			 * We have in flight what we are allowed by cwnd (if
18292 			 * it was rwnd blocking it would have hit above out
18293 			 * >= tp->snd_wnd).
18294 			 */
18295 			return (0);
18296 		}
18297 		len = sendwin - flight;
18298 		if ((len + ctf_outstanding(tp)) > tp->snd_wnd) {
18299 			/* We would send too much (beyond the rwnd) */
18300 			len = tp->snd_wnd - ctf_outstanding(tp);
18301 		}
18302 		if ((len + sb_offset) > avail) {
18303 			/*
18304 			 * We don't have that much in the SB, how much is
18305 			 * there?
18306 			 */
18307 			len = avail - sb_offset;
18308 		}
18309 	}
18310 	return (len);
18311 }
18312 
18313 static void
18314 rack_log_fsb(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t flags,
18315 	     unsigned ipoptlen, int32_t orig_len, int32_t len, int error,
18316 	     int rsm_is_null, int optlen, int line, uint16_t mode)
18317 {
18318 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
18319 		union tcp_log_stackspecific log;
18320 		struct timeval tv;
18321 
18322 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
18323 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
18324 		log.u_bbr.flex1 = error;
18325 		log.u_bbr.flex2 = flags;
18326 		log.u_bbr.flex3 = rsm_is_null;
18327 		log.u_bbr.flex4 = ipoptlen;
18328 		log.u_bbr.flex5 = tp->rcv_numsacks;
18329 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
18330 		log.u_bbr.flex7 = optlen;
18331 		log.u_bbr.flex8 = rack->r_fsb_inited;
18332 		log.u_bbr.applimited = rack->r_fast_output;
18333 		log.u_bbr.bw_inuse = rack_get_bw(rack);
18334 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
18335 		log.u_bbr.cwnd_gain = mode;
18336 		log.u_bbr.pkts_out = orig_len;
18337 		log.u_bbr.lt_epoch = len;
18338 		log.u_bbr.delivered = line;
18339 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
18340 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18341 		tcp_log_event(tp, NULL, &so->so_rcv, &so->so_snd, TCP_LOG_FSB, 0,
18342 			       len, &log, false, NULL, __func__, __LINE__, &tv);
18343 	}
18344 }
18345 
18346 
18347 static struct mbuf *
18348 rack_fo_base_copym(struct mbuf *the_m, uint32_t the_off, int32_t *plen,
18349 		   struct rack_fast_send_blk *fsb,
18350 		   int32_t seglimit, int32_t segsize, int hw_tls)
18351 {
18352 #ifdef KERN_TLS
18353 	struct ktls_session *tls, *ntls;
18354 #ifdef INVARIANTS
18355 	struct mbuf *start;
18356 #endif
18357 #endif
18358 	struct mbuf *m, *n, **np, *smb;
18359 	struct mbuf *top;
18360 	int32_t off, soff;
18361 	int32_t len = *plen;
18362 	int32_t fragsize;
18363 	int32_t len_cp = 0;
18364 	uint32_t mlen, frags;
18365 
18366 	soff = off = the_off;
18367 	smb = m = the_m;
18368 	np = &top;
18369 	top = NULL;
18370 #ifdef KERN_TLS
18371 	if (hw_tls && (m->m_flags & M_EXTPG))
18372 		tls = m->m_epg_tls;
18373 	else
18374 		tls = NULL;
18375 #ifdef INVARIANTS
18376 	start = m;
18377 #endif
18378 #endif
18379 	while (len > 0) {
18380 		if (m == NULL) {
18381 			*plen = len_cp;
18382 			break;
18383 		}
18384 #ifdef KERN_TLS
18385 		if (hw_tls) {
18386 			if (m->m_flags & M_EXTPG)
18387 				ntls = m->m_epg_tls;
18388 			else
18389 				ntls = NULL;
18390 
18391 			/*
18392 			 * Avoid mixing TLS records with handshake
18393 			 * data or TLS records from different
18394 			 * sessions.
18395 			 */
18396 			if (tls != ntls) {
18397 				MPASS(m != start);
18398 				*plen = len_cp;
18399 				break;
18400 			}
18401 		}
18402 #endif
18403 		mlen = min(len, m->m_len - off);
18404 		if (seglimit) {
18405 			/*
18406 			 * For M_EXTPG mbufs, add 3 segments
18407 			 * + 1 in case we are crossing page boundaries
18408 			 * + 2 in case the TLS hdr/trailer are used
18409 			 * It is cheaper to just add the segments
18410 			 * than it is to take the cache miss to look
18411 			 * at the mbuf ext_pgs state in detail.
18412 			 */
18413 			if (m->m_flags & M_EXTPG) {
18414 				fragsize = min(segsize, PAGE_SIZE);
18415 				frags = 3;
18416 			} else {
18417 				fragsize = segsize;
18418 				frags = 0;
18419 			}
18420 
18421 			/* Break if we really can't fit anymore. */
18422 			if ((frags + 1) >= seglimit) {
18423 				*plen =	len_cp;
18424 				break;
18425 			}
18426 
18427 			/*
18428 			 * Reduce size if you can't copy the whole
18429 			 * mbuf. If we can't copy the whole mbuf, also
18430 			 * adjust len so the loop will end after this
18431 			 * mbuf.
18432 			 */
18433 			if ((frags + howmany(mlen, fragsize)) >= seglimit) {
18434 				mlen = (seglimit - frags - 1) * fragsize;
18435 				len = mlen;
18436 				*plen = len_cp + len;
18437 			}
18438 			frags += howmany(mlen, fragsize);
18439 			if (frags == 0)
18440 				frags++;
18441 			seglimit -= frags;
18442 			KASSERT(seglimit > 0,
18443 			    ("%s: seglimit went too low", __func__));
18444 		}
18445 		n = m_get(M_NOWAIT, m->m_type);
18446 		*np = n;
18447 		if (n == NULL)
18448 			goto nospace;
18449 		n->m_len = mlen;
18450 		soff += mlen;
18451 		len_cp += n->m_len;
18452 		if (m->m_flags & (M_EXT|M_EXTPG)) {
18453 			n->m_data = m->m_data + off;
18454 			mb_dupcl(n, m);
18455 		} else {
18456 			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
18457 			    (u_int)n->m_len);
18458 		}
18459 		len -= n->m_len;
18460 		off = 0;
18461 		m = m->m_next;
18462 		np = &n->m_next;
18463 		if (len || (soff == smb->m_len)) {
18464 			/*
18465 			 * We have more so we move forward  or
18466 			 * we have consumed the entire mbuf and
18467 			 * len has fell to 0.
18468 			 */
18469 			soff = 0;
18470 			smb = m;
18471 		}
18472 
18473 	}
18474 	if (fsb != NULL) {
18475 		fsb->m = smb;
18476 		fsb->off = soff;
18477 		if (smb) {
18478 			/*
18479 			 * Save off the size of the mbuf. We do
18480 			 * this so that we can recognize when it
18481 			 * has been trimmed by sbcut() as acks
18482 			 * come in.
18483 			 */
18484 			fsb->o_m_len = smb->m_len;
18485 			fsb->o_t_len = M_TRAILINGROOM(smb);
18486 		} else {
18487 			/*
18488 			 * This is the case where the next mbuf went to NULL. This
18489 			 * means with this copy we have sent everything in the sb.
18490 			 * In theory we could clear the fast_output flag, but lets
18491 			 * not since its possible that we could get more added
18492 			 * and acks that call the extend function which would let
18493 			 * us send more.
18494 			 */
18495 			fsb->o_m_len = 0;
18496 			fsb->o_t_len = 0;
18497 		}
18498 	}
18499 	return (top);
18500 nospace:
18501 	if (top)
18502 		m_freem(top);
18503 	return (NULL);
18504 
18505 }
18506 
18507 /*
18508  * This is a copy of m_copym(), taking the TSO segment size/limit
18509  * constraints into account, and advancing the sndptr as it goes.
18510  */
18511 static struct mbuf *
18512 rack_fo_m_copym(struct tcp_rack *rack, int32_t *plen,
18513 		int32_t seglimit, int32_t segsize, struct mbuf **s_mb, int *s_soff)
18514 {
18515 	struct mbuf *m, *n;
18516 	int32_t soff;
18517 
18518 	m = rack->r_ctl.fsb.m;
18519 	if (M_TRAILINGROOM(m) != rack->r_ctl.fsb.o_t_len) {
18520 		/*
18521 		 * The trailing space changed, mbufs can grow
18522 		 * at the tail but they can't shrink from
18523 		 * it, KASSERT that. Adjust the orig_m_len to
18524 		 * compensate for this change.
18525 		 */
18526 		KASSERT((rack->r_ctl.fsb.o_t_len > M_TRAILINGROOM(m)),
18527 			("mbuf:%p rack:%p trailing_space:%jd ots:%u oml:%u mlen:%u\n",
18528 			 m,
18529 			 rack,
18530 			 (intmax_t)M_TRAILINGROOM(m),
18531 			 rack->r_ctl.fsb.o_t_len,
18532 			 rack->r_ctl.fsb.o_m_len,
18533 			 m->m_len));
18534 		rack->r_ctl.fsb.o_m_len += (rack->r_ctl.fsb.o_t_len - M_TRAILINGROOM(m));
18535 		rack->r_ctl.fsb.o_t_len = M_TRAILINGROOM(m);
18536 	}
18537 	if (m->m_len < rack->r_ctl.fsb.o_m_len) {
18538 		/*
18539 		 * Mbuf shrank, trimmed off the top by an ack, our
18540 		 * offset changes.
18541 		 */
18542 		KASSERT((rack->r_ctl.fsb.off >= (rack->r_ctl.fsb.o_m_len - m->m_len)),
18543 			("mbuf:%p len:%u rack:%p oml:%u soff:%u\n",
18544 			 m, m->m_len,
18545 			 rack, rack->r_ctl.fsb.o_m_len,
18546 			 rack->r_ctl.fsb.off));
18547 
18548 		if (rack->r_ctl.fsb.off >= (rack->r_ctl.fsb.o_m_len- m->m_len))
18549 			rack->r_ctl.fsb.off -= (rack->r_ctl.fsb.o_m_len - m->m_len);
18550 		else
18551 			rack->r_ctl.fsb.off = 0;
18552 		rack->r_ctl.fsb.o_m_len = m->m_len;
18553 #ifdef INVARIANTS
18554 	} else if (m->m_len > rack->r_ctl.fsb.o_m_len) {
18555 		panic("rack:%p m:%p m_len grew outside of t_space compensation",
18556 		      rack, m);
18557 #endif
18558 	}
18559 	soff = rack->r_ctl.fsb.off;
18560 	KASSERT(soff >= 0, ("%s, negative off %d", __FUNCTION__, soff));
18561 	KASSERT(*plen >= 0, ("%s, negative len %d", __FUNCTION__, *plen));
18562 	KASSERT(soff < m->m_len, ("%s rack:%p len:%u m:%p m->m_len:%u < off?",
18563 				 __FUNCTION__,
18564 				 rack, *plen, m, m->m_len));
18565 	/* Save off the right location before we copy and advance */
18566 	*s_soff = soff;
18567 	*s_mb = rack->r_ctl.fsb.m;
18568 	n = rack_fo_base_copym(m, soff, plen,
18569 			       &rack->r_ctl.fsb,
18570 			       seglimit, segsize, rack->r_ctl.fsb.hw_tls);
18571 	return (n);
18572 }
18573 
18574 /* Log the buffer level */
18575 static void
18576 rack_log_queue_level(struct tcpcb *tp, struct tcp_rack *rack,
18577 		     int len, struct timeval *tv,
18578 		     uint32_t cts)
18579 {
18580 	uint32_t p_rate = 0, p_queue = 0, err = 0;
18581 	union tcp_log_stackspecific log;
18582 
18583 #ifdef RATELIMIT
18584 	err = in_pcbquery_txrlevel(rack->rc_inp, &p_queue);
18585 	err = in_pcbquery_txrtlmt(rack->rc_inp,	&p_rate);
18586 #endif
18587 	memset(&log.u_bbr, 0, sizeof(log.u_bbr));
18588 	log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
18589 	log.u_bbr.flex1 = p_rate;
18590 	log.u_bbr.flex2 = p_queue;
18591 	log.u_bbr.flex4 = (uint32_t)rack->r_ctl.crte->using;
18592 	log.u_bbr.flex5 = (uint32_t)rack->r_ctl.crte->rs_num_enobufs;
18593 	log.u_bbr.flex6 = rack->r_ctl.crte->time_between;
18594 	log.u_bbr.flex7 = 99;
18595 	log.u_bbr.flex8 = 0;
18596 	log.u_bbr.pkts_out = err;
18597 	log.u_bbr.delRate = rack->r_ctl.crte->rate;
18598 	log.u_bbr.timeStamp = cts;
18599 	log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18600 	tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_HDWR_PACE, 0,
18601 		       len, &log, false, NULL, __func__, __LINE__, tv);
18602 
18603 }
18604 
18605 static uint32_t
18606 rack_check_queue_level(struct tcp_rack *rack, struct tcpcb *tp,
18607 		       struct timeval *tv, uint32_t cts, int len, uint32_t segsiz)
18608 {
18609 	uint64_t lentime = 0;
18610 #ifdef RATELIMIT
18611 	uint32_t p_rate = 0, p_queue = 0, err;
18612 	union tcp_log_stackspecific log;
18613 	uint64_t bw;
18614 
18615 	err = in_pcbquery_txrlevel(rack->rc_inp, &p_queue);
18616 	/* Failed or queue is zero */
18617 	if (err || (p_queue == 0)) {
18618 		lentime = 0;
18619 		goto out;
18620 	}
18621 	err = in_pcbquery_txrtlmt(rack->rc_inp, &p_rate);
18622 	if (err) {
18623 		lentime = 0;
18624 		goto out;
18625 	}
18626 	/*
18627 	 * If we reach here we have some bytes in
18628 	 * the queue. The number returned is a value
18629 	 * between 0 and 0xffff where ffff is full
18630 	 * and 0 is empty. So how best to make this into
18631 	 * something usable?
18632 	 *
18633 	 * The "safer" way is lets take the b/w gotten
18634 	 * from the query (which should be our b/w rate)
18635 	 * and pretend that a full send (our rc_pace_max_segs)
18636 	 * is outstanding. We factor it so its as if a full
18637 	 * number of our MSS segment is terms of full
18638 	 * ethernet segments are outstanding.
18639 	 */
18640 	bw = p_rate / 8;
18641 	if (bw) {
18642 		lentime = (rack->r_ctl.rc_pace_max_segs / segsiz);
18643 		lentime *= ETHERNET_SEGMENT_SIZE;
18644 		lentime *= (uint64_t)HPTS_USEC_IN_SEC;
18645 		lentime /= bw;
18646 	} else {
18647 		/* TSNH -- KASSERT? */
18648 		lentime = 0;
18649 	}
18650 out:
18651 	if (tcp_bblogging_on(tp)) {
18652 		memset(&log, 0, sizeof(log));
18653 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
18654 		log.u_bbr.flex1 = p_rate;
18655 		log.u_bbr.flex2 = p_queue;
18656 		log.u_bbr.flex4 = (uint32_t)rack->r_ctl.crte->using;
18657 		log.u_bbr.flex5 = (uint32_t)rack->r_ctl.crte->rs_num_enobufs;
18658 		log.u_bbr.flex6 = rack->r_ctl.crte->time_between;
18659 		log.u_bbr.flex7 = 99;
18660 		log.u_bbr.flex8 = 0;
18661 		log.u_bbr.pkts_out = err;
18662 		log.u_bbr.delRate = rack->r_ctl.crte->rate;
18663 		log.u_bbr.cur_del_rate = lentime;
18664 		log.u_bbr.timeStamp = cts;
18665 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18666 		tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_HDWR_PACE, 0,
18667 			       len, &log, false, NULL, __func__, __LINE__,tv);
18668 	}
18669 #endif
18670 	return ((uint32_t)lentime);
18671 }
18672 
18673 static int
18674 rack_fast_rsm_output(struct tcpcb *tp, struct tcp_rack *rack, struct rack_sendmap *rsm,
18675 		     uint64_t ts_val, uint32_t cts, uint32_t ms_cts, struct timeval *tv, int len, uint8_t doing_tlp)
18676 {
18677 	/*
18678 	 * Enter the fast retransmit path. We are given that a sched_pin is
18679 	 * in place (if accounting is compliled in) and the cycle count taken
18680 	 * at the entry is in the ts_val. The concept her is that the rsm
18681 	 * now holds the mbuf offsets and such so we can directly transmit
18682 	 * without a lot of overhead, the len field is already set for
18683 	 * us to prohibit us from sending too much (usually its 1MSS).
18684 	 */
18685 	struct ip *ip = NULL;
18686 	struct udphdr *udp = NULL;
18687 	struct tcphdr *th = NULL;
18688 	struct mbuf *m = NULL;
18689 	struct inpcb *inp;
18690 	uint8_t *cpto;
18691 	struct tcp_log_buffer *lgb;
18692 #ifdef TCP_ACCOUNTING
18693 	uint64_t crtsc;
18694 	int cnt_thru = 1;
18695 #endif
18696 	struct tcpopt to;
18697 	u_char opt[TCP_MAXOLEN];
18698 	uint32_t hdrlen, optlen;
18699 	int32_t slot, segsiz, max_val, tso = 0, error = 0, ulen = 0;
18700 	uint16_t flags;
18701 	uint32_t if_hw_tsomaxsegcount = 0, startseq;
18702 	uint32_t if_hw_tsomaxsegsize;
18703 	int32_t ip_sendflag = IP_NO_SND_TAG_RL;
18704 
18705 #ifdef INET6
18706 	struct ip6_hdr *ip6 = NULL;
18707 
18708 	if (rack->r_is_v6) {
18709 		ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
18710 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
18711 	} else
18712 #endif				/* INET6 */
18713 	{
18714 		ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
18715 		hdrlen = sizeof(struct tcpiphdr);
18716 	}
18717 	if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) {
18718 		goto failed;
18719 	}
18720 	if (doing_tlp) {
18721 		/* Its a TLP add the flag, it may already be there but be sure */
18722 		rsm->r_flags |= RACK_TLP;
18723 	} else {
18724 		/* If it was a TLP it is not not on this retransmit */
18725 		rsm->r_flags &= ~RACK_TLP;
18726 	}
18727 	startseq = rsm->r_start;
18728 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
18729 	inp = rack->rc_inp;
18730 	to.to_flags = 0;
18731 	flags = tcp_outflags[tp->t_state];
18732 	if (flags & (TH_SYN|TH_RST)) {
18733 		goto failed;
18734 	}
18735 	if (rsm->r_flags & RACK_HAS_FIN) {
18736 		/* We can't send a FIN here */
18737 		goto failed;
18738 	}
18739 	if (flags & TH_FIN) {
18740 		/* We never send a FIN */
18741 		flags &= ~TH_FIN;
18742 	}
18743 	if (tp->t_flags & TF_RCVD_TSTMP) {
18744 		to.to_tsval = ms_cts + tp->ts_offset;
18745 		to.to_tsecr = tp->ts_recent;
18746 		to.to_flags = TOF_TS;
18747 	}
18748 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
18749 	/* TCP-MD5 (RFC2385). */
18750 	if (tp->t_flags & TF_SIGNATURE)
18751 		to.to_flags |= TOF_SIGNATURE;
18752 #endif
18753 	optlen = tcp_addoptions(&to, opt);
18754 	hdrlen += optlen;
18755 	udp = rack->r_ctl.fsb.udp;
18756 	if (udp)
18757 		hdrlen += sizeof(struct udphdr);
18758 	if (rack->r_ctl.rc_pace_max_segs)
18759 		max_val = rack->r_ctl.rc_pace_max_segs;
18760 	else if (rack->rc_user_set_max_segs)
18761 		max_val = rack->rc_user_set_max_segs * segsiz;
18762 	else
18763 		max_val = len;
18764 	if ((tp->t_flags & TF_TSO) &&
18765 	    V_tcp_do_tso &&
18766 	    (len > segsiz) &&
18767 	    (tp->t_port == 0))
18768 		tso = 1;
18769 #ifdef INET6
18770 	if (MHLEN < hdrlen + max_linkhdr)
18771 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
18772 	else
18773 #endif
18774 		m = m_gethdr(M_NOWAIT, MT_DATA);
18775 	if (m == NULL)
18776 		goto failed;
18777 	m->m_data += max_linkhdr;
18778 	m->m_len = hdrlen;
18779 	th = rack->r_ctl.fsb.th;
18780 	/* Establish the len to send */
18781 	if (len > max_val)
18782 		len = max_val;
18783 	if ((tso) && (len + optlen > segsiz)) {
18784 		uint32_t if_hw_tsomax;
18785 		int32_t max_len;
18786 
18787 		/* extract TSO information */
18788 		if_hw_tsomax = tp->t_tsomax;
18789 		if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
18790 		if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
18791 		/*
18792 		 * Check if we should limit by maximum payload
18793 		 * length:
18794 		 */
18795 		if (if_hw_tsomax != 0) {
18796 			/* compute maximum TSO length */
18797 			max_len = (if_hw_tsomax - hdrlen -
18798 				   max_linkhdr);
18799 			if (max_len <= 0) {
18800 				goto failed;
18801 			} else if (len > max_len) {
18802 				len = max_len;
18803 			}
18804 		}
18805 		if (len <= segsiz) {
18806 			/*
18807 			 * In case there are too many small fragments don't
18808 			 * use TSO:
18809 			 */
18810 			tso = 0;
18811 		}
18812 	} else {
18813 		tso = 0;
18814 	}
18815 	if ((tso == 0) && (len > segsiz))
18816 		len = segsiz;
18817 	(void)tcp_get_usecs(tv);
18818 	if ((len == 0) ||
18819 	    (len <= MHLEN - hdrlen - max_linkhdr)) {
18820 		goto failed;
18821 	}
18822 	th->th_seq = htonl(rsm->r_start);
18823 	th->th_ack = htonl(tp->rcv_nxt);
18824 	/*
18825 	 * The PUSH bit should only be applied
18826 	 * if the full retransmission is made. If
18827 	 * we are sending less than this is the
18828 	 * left hand edge and should not have
18829 	 * the PUSH bit.
18830 	 */
18831 	if ((rsm->r_flags & RACK_HAD_PUSH) &&
18832 	    (len == (rsm->r_end - rsm->r_start)))
18833 		flags |= TH_PUSH;
18834 	th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale));
18835 	if (th->th_win == 0) {
18836 		tp->t_sndzerowin++;
18837 		tp->t_flags |= TF_RXWIN0SENT;
18838 	} else
18839 		tp->t_flags &= ~TF_RXWIN0SENT;
18840 	if (rsm->r_flags & RACK_TLP) {
18841 		/*
18842 		 * TLP should not count in retran count, but
18843 		 * in its own bin
18844 		 */
18845 		counter_u64_add(rack_tlp_retran, 1);
18846 		counter_u64_add(rack_tlp_retran_bytes, len);
18847 	} else {
18848 		tp->t_sndrexmitpack++;
18849 		KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
18850 		KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
18851 	}
18852 #ifdef STATS
18853 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
18854 				 len);
18855 #endif
18856 	if (rsm->m == NULL)
18857 		goto failed;
18858 	if (rsm->m &&
18859 	    ((rsm->orig_m_len != rsm->m->m_len) ||
18860 	     (M_TRAILINGROOM(rsm->m) != rsm->orig_t_space))) {
18861 		/* Fix up the orig_m_len and possibly the mbuf offset */
18862 		rack_adjust_orig_mlen(rsm);
18863 	}
18864 	m->m_next = rack_fo_base_copym(rsm->m, rsm->soff, &len, NULL, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, rsm->r_hw_tls);
18865 	if (len <= segsiz) {
18866 		/*
18867 		 * Must have ran out of mbufs for the copy
18868 		 * shorten it to no longer need tso. Lets
18869 		 * not put on sendalot since we are low on
18870 		 * mbufs.
18871 		 */
18872 		tso = 0;
18873 	}
18874 	if ((m->m_next == NULL) || (len <= 0)){
18875 		goto failed;
18876 	}
18877 	if (udp) {
18878 		if (rack->r_is_v6)
18879 			ulen = hdrlen + len - sizeof(struct ip6_hdr);
18880 		else
18881 			ulen = hdrlen + len - sizeof(struct ip);
18882 		udp->uh_ulen = htons(ulen);
18883 	}
18884 	m->m_pkthdr.rcvif = (struct ifnet *)0;
18885 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
18886 	    (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) {
18887 		int ect = tcp_ecn_output_established(tp, &flags, len, true);
18888 		if ((tp->t_state == TCPS_SYN_RECEIVED) &&
18889 		    (tp->t_flags2 & TF2_ECN_SND_ECE))
18890 		    tp->t_flags2 &= ~TF2_ECN_SND_ECE;
18891 #ifdef INET6
18892 		if (rack->r_is_v6) {
18893 		    ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
18894 		    ip6->ip6_flow |= htonl(ect << 20);
18895 		}
18896 		else
18897 #endif
18898 		{
18899 		    ip->ip_tos &= ~IPTOS_ECN_MASK;
18900 		    ip->ip_tos |= ect;
18901 		}
18902 	}
18903 	if (rack->r_ctl.crte != NULL) {
18904 		/* See if we can send via the hw queue */
18905 		slot = rack_check_queue_level(rack, tp, tv, cts, len, segsiz);
18906 		/* If there is nothing in queue (no pacing time) we can send via the hw queue */
18907 		if (slot == 0)
18908 			ip_sendflag = 0;
18909 	}
18910 	tcp_set_flags(th, flags);
18911 	m->m_pkthdr.len = hdrlen + len;	/* in6_cksum() need this */
18912 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
18913 	if (to.to_flags & TOF_SIGNATURE) {
18914 		/*
18915 		 * Calculate MD5 signature and put it into the place
18916 		 * determined before.
18917 		 * NOTE: since TCP options buffer doesn't point into
18918 		 * mbuf's data, calculate offset and use it.
18919 		 */
18920 		if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
18921 						       (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
18922 			/*
18923 			 * Do not send segment if the calculation of MD5
18924 			 * digest has failed.
18925 			 */
18926 			goto failed;
18927 		}
18928 	}
18929 #endif
18930 #ifdef INET6
18931 	if (rack->r_is_v6) {
18932 		if (tp->t_port) {
18933 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
18934 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
18935 			udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
18936 			th->th_sum = htons(0);
18937 			UDPSTAT_INC(udps_opackets);
18938 		} else {
18939 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
18940 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
18941 			th->th_sum = in6_cksum_pseudo(ip6,
18942 						      sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
18943 						      0);
18944 		}
18945 	}
18946 #endif
18947 #if defined(INET6) && defined(INET)
18948 	else
18949 #endif
18950 #ifdef INET
18951 	{
18952 		if (tp->t_port) {
18953 			m->m_pkthdr.csum_flags = CSUM_UDP;
18954 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
18955 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
18956 						ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
18957 			th->th_sum = htons(0);
18958 			UDPSTAT_INC(udps_opackets);
18959 		} else {
18960 			m->m_pkthdr.csum_flags = CSUM_TCP;
18961 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
18962 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
18963 					       ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
18964 									IPPROTO_TCP + len + optlen));
18965 		}
18966 		/* IP version must be set here for ipv4/ipv6 checking later */
18967 		KASSERT(ip->ip_v == IPVERSION,
18968 			("%s: IP version incorrect: %d", __func__, ip->ip_v));
18969 	}
18970 #endif
18971 	if (tso) {
18972 		/*
18973 		 * Here we use segsiz since we have no added options besides
18974 		 * any standard timestamp options (no DSACKs or SACKS are sent
18975 		 * via either fast-path).
18976 		 */
18977 		KASSERT(len > segsiz,
18978 			("%s: len <= tso_segsz tp:%p", __func__, tp));
18979 		m->m_pkthdr.csum_flags |= CSUM_TSO;
18980 		m->m_pkthdr.tso_segsz = segsiz;
18981 	}
18982 #ifdef INET6
18983 	if (rack->r_is_v6) {
18984 		ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit;
18985 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
18986 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
18987 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
18988 		else
18989 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
18990 	}
18991 #endif
18992 #if defined(INET) && defined(INET6)
18993 	else
18994 #endif
18995 #ifdef INET
18996 	{
18997 		ip->ip_len = htons(m->m_pkthdr.len);
18998 		ip->ip_ttl = rack->r_ctl.fsb.hoplimit;
18999 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
19000 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
19001 			if (tp->t_port == 0 || len < V_tcp_minmss) {
19002 				ip->ip_off |= htons(IP_DF);
19003 			}
19004 		} else {
19005 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
19006 		}
19007 	}
19008 #endif
19009 	if (doing_tlp == 0) {
19010 		/* Set we retransmitted */
19011 		rack->rc_gp_saw_rec = 1;
19012 	} else {
19013 		/* Its a TLP set ca or ss */
19014 		if (tp->snd_cwnd > tp->snd_ssthresh) {
19015 			/* Set we sent in CA */
19016 			rack->rc_gp_saw_ca = 1;
19017 		} else {
19018 			/* Set we sent in SS */
19019 			rack->rc_gp_saw_ss = 1;
19020 		}
19021 	}
19022 	/* Time to copy in our header */
19023 	cpto = mtod(m, uint8_t *);
19024 	memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
19025 	th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
19026 	if (optlen) {
19027 		bcopy(opt, th + 1, optlen);
19028 		th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
19029 	} else {
19030 		th->th_off = sizeof(struct tcphdr) >> 2;
19031 	}
19032 	if (tcp_bblogging_on(rack->rc_tp)) {
19033 		union tcp_log_stackspecific log;
19034 
19035 		if (rsm->r_flags & RACK_RWND_COLLAPSED) {
19036 			rack_log_collapse(rack, rsm->r_start, rsm->r_end, 0, __LINE__, 5, rsm->r_flags, rsm);
19037 			counter_u64_add(rack_collapsed_win_rxt, 1);
19038 			counter_u64_add(rack_collapsed_win_rxt_bytes, (rsm->r_end - rsm->r_start));
19039 		}
19040 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
19041 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
19042 		if (rack->rack_no_prr)
19043 			log.u_bbr.flex1 = 0;
19044 		else
19045 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
19046 		log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
19047 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
19048 		log.u_bbr.flex4 = max_val;
19049 		/* Save off the early/late values */
19050 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
19051 		log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
19052 		log.u_bbr.bw_inuse = rack_get_bw(rack);
19053 		log.u_bbr.cur_del_rate = rack->r_ctl.gp_bw;
19054 		if (doing_tlp == 0)
19055 			log.u_bbr.flex8 = 1;
19056 		else
19057 			log.u_bbr.flex8 = 2;
19058 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
19059 		log.u_bbr.flex7 = 55;
19060 		log.u_bbr.pkts_out = tp->t_maxseg;
19061 		log.u_bbr.timeStamp = cts;
19062 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
19063 		if (rsm && (rsm->r_rtr_cnt > 0)) {
19064 			/*
19065 			 * When we have a retransmit we want to log the
19066 			 * burst at send and flight at send from before.
19067 			 */
19068 			log.u_bbr.flex5 = rsm->r_fas;
19069 			log.u_bbr.bbr_substate = rsm->r_bas;
19070 		} else {
19071 			/*
19072 			 * This is currently unlikely until we do the
19073 			 * packet pair probes but I will add it for completeness.
19074 			 */
19075 			log.u_bbr.flex5 = log.u_bbr.inflight;
19076 			log.u_bbr.bbr_substate = (uint8_t)((len + segsiz - 1)/segsiz);
19077 		}
19078 		log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use;
19079 		log.u_bbr.delivered = 0;
19080 		log.u_bbr.rttProp = (uint64_t)rsm;
19081 		log.u_bbr.delRate = rsm->r_flags;
19082 		log.u_bbr.delRate <<= 31;
19083 		log.u_bbr.delRate |= rack->r_must_retran;
19084 		log.u_bbr.delRate <<= 1;
19085 		log.u_bbr.delRate |= 1;
19086 		log.u_bbr.pkt_epoch = __LINE__;
19087 		lgb = tcp_log_event(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
19088 				     len, &log, false, NULL, __func__, __LINE__, tv);
19089 	} else
19090 		lgb = NULL;
19091 	if ((rack->r_ctl.crte != NULL) &&
19092 	    tcp_bblogging_on(tp)) {
19093 		rack_log_queue_level(tp, rack, len, tv, cts);
19094 	}
19095 #ifdef INET6
19096 	if (rack->r_is_v6) {
19097 		error = ip6_output(m, inp->in6p_outputopts,
19098 				   &inp->inp_route6,
19099 				   ip_sendflag, NULL, NULL, inp);
19100 	}
19101 	else
19102 #endif
19103 #ifdef INET
19104 	{
19105 		error = ip_output(m, NULL,
19106 				  &inp->inp_route,
19107 				  ip_sendflag, 0, inp);
19108 	}
19109 #endif
19110 	m = NULL;
19111 	if (lgb) {
19112 		lgb->tlb_errno = error;
19113 		lgb = NULL;
19114 	}
19115 	if (error) {
19116 		goto failed;
19117 	} else if (rack->rc_hw_nobuf && (ip_sendflag != IP_NO_SND_TAG_RL)) {
19118 		rack->rc_hw_nobuf = 0;
19119 		rack->r_ctl.rc_agg_delayed = 0;
19120 		rack->r_early = 0;
19121 		rack->r_late = 0;
19122 		rack->r_ctl.rc_agg_early = 0;
19123 	}
19124 
19125 	rack_log_output(tp, &to, len, rsm->r_start, flags, error, rack_to_usec_ts(tv),
19126 			rsm, RACK_SENT_FP, rsm->m, rsm->soff, rsm->r_hw_tls, segsiz);
19127 	if (doing_tlp) {
19128 		rack->rc_tlp_in_progress = 1;
19129 		rack->r_ctl.rc_tlp_cnt_out++;
19130 	}
19131 	if (error == 0) {
19132 		counter_u64_add(rack_total_bytes, len);
19133 		tcp_account_for_send(tp, len, 1, doing_tlp, rsm->r_hw_tls);
19134 		if (doing_tlp) {
19135 			rack->rc_last_sent_tlp_past_cumack = 0;
19136 			rack->rc_last_sent_tlp_seq_valid = 1;
19137 			rack->r_ctl.last_sent_tlp_seq = rsm->r_start;
19138 			rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start;
19139 		}
19140 		if (rack->r_ctl.rc_prr_sndcnt >= len)
19141 			rack->r_ctl.rc_prr_sndcnt -= len;
19142 		else
19143 			rack->r_ctl.rc_prr_sndcnt = 0;
19144 	}
19145 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
19146 	rack->forced_ack = 0;	/* If we send something zap the FA flag */
19147 	if (IN_FASTRECOVERY(tp->t_flags) && rsm)
19148 		rack->r_ctl.retran_during_recovery += len;
19149 	{
19150 		int idx;
19151 
19152 		idx = (len / segsiz) + 3;
19153 		if (idx >= TCP_MSS_ACCT_ATIMER)
19154 			counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
19155 		else
19156 			counter_u64_add(rack_out_size[idx], 1);
19157 	}
19158 	if (tp->t_rtttime == 0) {
19159 		tp->t_rtttime = ticks;
19160 		tp->t_rtseq = startseq;
19161 		KMOD_TCPSTAT_INC(tcps_segstimed);
19162 	}
19163 	counter_u64_add(rack_fto_rsm_send, 1);
19164 	if (error && (error == ENOBUFS)) {
19165 		if (rack->r_ctl.crte != NULL) {
19166 			tcp_trace_point(rack->rc_tp, TCP_TP_HWENOBUF);
19167 			if (tcp_bblogging_on(rack->rc_tp))
19168 				rack_log_queue_level(tp, rack, len, tv, cts);
19169 		} else
19170 			tcp_trace_point(rack->rc_tp, TCP_TP_ENOBUF);
19171 		slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC);
19172 		if (rack->rc_enobuf < 0x7f)
19173 			rack->rc_enobuf++;
19174 		if (slot < (10 * HPTS_USEC_IN_MSEC))
19175 			slot = 10 * HPTS_USEC_IN_MSEC;
19176 		if (rack->r_ctl.crte != NULL) {
19177 			counter_u64_add(rack_saw_enobuf_hw, 1);
19178 			tcp_rl_log_enobuf(rack->r_ctl.crte);
19179 		}
19180 		counter_u64_add(rack_saw_enobuf, 1);
19181 	} else
19182 		slot = rack_get_pacing_delay(rack, tp, len, NULL, segsiz);
19183 	if ((slot == 0) ||
19184 	    (rack->rc_always_pace == 0) ||
19185 	    (rack->r_rr_config == 1)) {
19186 		/*
19187 		 * We have no pacing set or we
19188 		 * are using old-style rack or
19189 		 * we are overridden to use the old 1ms pacing.
19190 		 */
19191 		slot = rack->r_ctl.rc_min_to;
19192 	}
19193 	rack_start_hpts_timer(rack, tp, cts, slot, len, 0);
19194 #ifdef TCP_ACCOUNTING
19195 	crtsc = get_cyclecount();
19196 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19197 		tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru;
19198 	}
19199 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19200 		tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
19201 	}
19202 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19203 		tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((len + segsiz - 1) / segsiz);
19204 	}
19205 	sched_unpin();
19206 #endif
19207 	return (0);
19208 failed:
19209 	if (m)
19210 		m_free(m);
19211 	return (-1);
19212 }
19213 
19214 static void
19215 rack_sndbuf_autoscale(struct tcp_rack *rack)
19216 {
19217 	/*
19218 	 * Automatic sizing of send socket buffer.  Often the send buffer
19219 	 * size is not optimally adjusted to the actual network conditions
19220 	 * at hand (delay bandwidth product).  Setting the buffer size too
19221 	 * small limits throughput on links with high bandwidth and high
19222 	 * delay (eg. trans-continental/oceanic links).  Setting the
19223 	 * buffer size too big consumes too much real kernel memory,
19224 	 * especially with many connections on busy servers.
19225 	 *
19226 	 * The criteria to step up the send buffer one notch are:
19227 	 *  1. receive window of remote host is larger than send buffer
19228 	 *     (with a fudge factor of 5/4th);
19229 	 *  2. send buffer is filled to 7/8th with data (so we actually
19230 	 *     have data to make use of it);
19231 	 *  3. send buffer fill has not hit maximal automatic size;
19232 	 *  4. our send window (slow start and cogestion controlled) is
19233 	 *     larger than sent but unacknowledged data in send buffer.
19234 	 *
19235 	 * Note that the rack version moves things much faster since
19236 	 * we want to avoid hitting cache lines in the rack_fast_output()
19237 	 * path so this is called much less often and thus moves
19238 	 * the SB forward by a percentage.
19239 	 */
19240 	struct socket *so;
19241 	struct tcpcb *tp;
19242 	uint32_t sendwin, scaleup;
19243 
19244 	tp = rack->rc_tp;
19245 	so = rack->rc_inp->inp_socket;
19246 	sendwin = min(rack->r_ctl.cwnd_to_use, tp->snd_wnd);
19247 	if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) {
19248 		if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
19249 		    sbused(&so->so_snd) >=
19250 		    (so->so_snd.sb_hiwat / 8 * 7) &&
19251 		    sbused(&so->so_snd) < V_tcp_autosndbuf_max &&
19252 		    sendwin >= (sbused(&so->so_snd) -
19253 		    (tp->snd_nxt - tp->snd_una))) {
19254 			if (rack_autosndbuf_inc)
19255 				scaleup = (rack_autosndbuf_inc * so->so_snd.sb_hiwat) / 100;
19256 			else
19257 				scaleup = V_tcp_autosndbuf_inc;
19258 			if (scaleup < V_tcp_autosndbuf_inc)
19259 				scaleup = V_tcp_autosndbuf_inc;
19260 			scaleup += so->so_snd.sb_hiwat;
19261 			if (scaleup > V_tcp_autosndbuf_max)
19262 				scaleup = V_tcp_autosndbuf_max;
19263 			if (!sbreserve_locked(so, SO_SND, scaleup, curthread))
19264 				so->so_snd.sb_flags &= ~SB_AUTOSIZE;
19265 		}
19266 	}
19267 }
19268 
19269 static int
19270 rack_fast_output(struct tcpcb *tp, struct tcp_rack *rack, uint64_t ts_val,
19271 		 uint32_t cts, uint32_t ms_cts, struct timeval *tv, long tot_len, int *send_err)
19272 {
19273 	/*
19274 	 * Enter to do fast output. We are given that the sched_pin is
19275 	 * in place (if accounting is compiled in) and the cycle count taken
19276 	 * at entry is in place in ts_val. The idea here is that
19277 	 * we know how many more bytes needs to be sent (presumably either
19278 	 * during pacing or to fill the cwnd and that was greater than
19279 	 * the max-burst). We have how much to send and all the info we
19280 	 * need to just send.
19281 	 */
19282 #ifdef INET
19283 	struct ip *ip = NULL;
19284 #endif
19285 	struct udphdr *udp = NULL;
19286 	struct tcphdr *th = NULL;
19287 	struct mbuf *m, *s_mb;
19288 	struct inpcb *inp;
19289 	uint8_t *cpto;
19290 	struct tcp_log_buffer *lgb;
19291 #ifdef TCP_ACCOUNTING
19292 	uint64_t crtsc;
19293 #endif
19294 	struct tcpopt to;
19295 	u_char opt[TCP_MAXOLEN];
19296 	uint32_t hdrlen, optlen;
19297 #ifdef TCP_ACCOUNTING
19298 	int cnt_thru = 1;
19299 #endif
19300 	int32_t slot, segsiz, len, max_val, tso = 0, sb_offset, error, ulen = 0;
19301 	uint16_t flags;
19302 	uint32_t s_soff;
19303 	uint32_t if_hw_tsomaxsegcount = 0, startseq;
19304 	uint32_t if_hw_tsomaxsegsize;
19305 	uint16_t add_flag = RACK_SENT_FP;
19306 #ifdef INET6
19307 	struct ip6_hdr *ip6 = NULL;
19308 
19309 	if (rack->r_is_v6) {
19310 		ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
19311 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
19312 	} else
19313 #endif				/* INET6 */
19314 	{
19315 #ifdef INET
19316 		ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
19317 		hdrlen = sizeof(struct tcpiphdr);
19318 #endif
19319 	}
19320 	if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) {
19321 		m = NULL;
19322 		goto failed;
19323 	}
19324 	rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
19325 	startseq = tp->snd_max;
19326 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
19327 	inp = rack->rc_inp;
19328 	len = rack->r_ctl.fsb.left_to_send;
19329 	to.to_flags = 0;
19330 	flags = rack->r_ctl.fsb.tcp_flags;
19331 	if (tp->t_flags & TF_RCVD_TSTMP) {
19332 		to.to_tsval = ms_cts + tp->ts_offset;
19333 		to.to_tsecr = tp->ts_recent;
19334 		to.to_flags = TOF_TS;
19335 	}
19336 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
19337 	/* TCP-MD5 (RFC2385). */
19338 	if (tp->t_flags & TF_SIGNATURE)
19339 		to.to_flags |= TOF_SIGNATURE;
19340 #endif
19341 	optlen = tcp_addoptions(&to, opt);
19342 	hdrlen += optlen;
19343 	udp = rack->r_ctl.fsb.udp;
19344 	if (udp)
19345 		hdrlen += sizeof(struct udphdr);
19346 	if (rack->r_ctl.rc_pace_max_segs)
19347 		max_val = rack->r_ctl.rc_pace_max_segs;
19348 	else if (rack->rc_user_set_max_segs)
19349 		max_val = rack->rc_user_set_max_segs * segsiz;
19350 	else
19351 		max_val = len;
19352 	if ((tp->t_flags & TF_TSO) &&
19353 	    V_tcp_do_tso &&
19354 	    (len > segsiz) &&
19355 	    (tp->t_port == 0))
19356 		tso = 1;
19357 again:
19358 #ifdef INET6
19359 	if (MHLEN < hdrlen + max_linkhdr)
19360 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
19361 	else
19362 #endif
19363 		m = m_gethdr(M_NOWAIT, MT_DATA);
19364 	if (m == NULL)
19365 		goto failed;
19366 	m->m_data += max_linkhdr;
19367 	m->m_len = hdrlen;
19368 	th = rack->r_ctl.fsb.th;
19369 	/* Establish the len to send */
19370 	if (len > max_val)
19371 		len = max_val;
19372 	if ((tso) && (len + optlen > segsiz)) {
19373 		uint32_t if_hw_tsomax;
19374 		int32_t max_len;
19375 
19376 		/* extract TSO information */
19377 		if_hw_tsomax = tp->t_tsomax;
19378 		if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
19379 		if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
19380 		/*
19381 		 * Check if we should limit by maximum payload
19382 		 * length:
19383 		 */
19384 		if (if_hw_tsomax != 0) {
19385 			/* compute maximum TSO length */
19386 			max_len = (if_hw_tsomax - hdrlen -
19387 				   max_linkhdr);
19388 			if (max_len <= 0) {
19389 				goto failed;
19390 			} else if (len > max_len) {
19391 				len = max_len;
19392 			}
19393 		}
19394 		if (len <= segsiz) {
19395 			/*
19396 			 * In case there are too many small fragments don't
19397 			 * use TSO:
19398 			 */
19399 			tso = 0;
19400 		}
19401 	} else {
19402 		tso = 0;
19403 	}
19404 	if ((tso == 0) && (len > segsiz))
19405 		len = segsiz;
19406 	(void)tcp_get_usecs(tv);
19407 	if ((len == 0) ||
19408 	    (len <= MHLEN - hdrlen - max_linkhdr)) {
19409 		goto failed;
19410 	}
19411 	sb_offset = tp->snd_max - tp->snd_una;
19412 	th->th_seq = htonl(tp->snd_max);
19413 	th->th_ack = htonl(tp->rcv_nxt);
19414 	th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale));
19415 	if (th->th_win == 0) {
19416 		tp->t_sndzerowin++;
19417 		tp->t_flags |= TF_RXWIN0SENT;
19418 	} else
19419 		tp->t_flags &= ~TF_RXWIN0SENT;
19420 	tp->snd_up = tp->snd_una;	/* drag it along, its deprecated */
19421 	KMOD_TCPSTAT_INC(tcps_sndpack);
19422 	KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
19423 #ifdef STATS
19424 	stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
19425 				 len);
19426 #endif
19427 	if (rack->r_ctl.fsb.m == NULL)
19428 		goto failed;
19429 
19430 	/* s_mb and s_soff are saved for rack_log_output */
19431 	m->m_next = rack_fo_m_copym(rack, &len, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize,
19432 				    &s_mb, &s_soff);
19433 	if (len <= segsiz) {
19434 		/*
19435 		 * Must have ran out of mbufs for the copy
19436 		 * shorten it to no longer need tso. Lets
19437 		 * not put on sendalot since we are low on
19438 		 * mbufs.
19439 		 */
19440 		tso = 0;
19441 	}
19442 	if (rack->r_ctl.fsb.rfo_apply_push &&
19443 	    (len == rack->r_ctl.fsb.left_to_send)) {
19444 		tcp_set_flags(th, flags | TH_PUSH);
19445 		add_flag |= RACK_HAD_PUSH;
19446 	}
19447 	if ((m->m_next == NULL) || (len <= 0)){
19448 		goto failed;
19449 	}
19450 	if (udp) {
19451 		if (rack->r_is_v6)
19452 			ulen = hdrlen + len - sizeof(struct ip6_hdr);
19453 		else
19454 			ulen = hdrlen + len - sizeof(struct ip);
19455 		udp->uh_ulen = htons(ulen);
19456 	}
19457 	m->m_pkthdr.rcvif = (struct ifnet *)0;
19458 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
19459 	    (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) {
19460 		int ect = tcp_ecn_output_established(tp, &flags, len, false);
19461 		if ((tp->t_state == TCPS_SYN_RECEIVED) &&
19462 		    (tp->t_flags2 & TF2_ECN_SND_ECE))
19463 			tp->t_flags2 &= ~TF2_ECN_SND_ECE;
19464 #ifdef INET6
19465 		if (rack->r_is_v6) {
19466 			ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
19467 			ip6->ip6_flow |= htonl(ect << 20);
19468 		}
19469 		else
19470 #endif
19471 		{
19472 #ifdef INET
19473 			ip->ip_tos &= ~IPTOS_ECN_MASK;
19474 			ip->ip_tos |= ect;
19475 #endif
19476 		}
19477 	}
19478 	tcp_set_flags(th, flags);
19479 	m->m_pkthdr.len = hdrlen + len;	/* in6_cksum() need this */
19480 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
19481 	if (to.to_flags & TOF_SIGNATURE) {
19482 		/*
19483 		 * Calculate MD5 signature and put it into the place
19484 		 * determined before.
19485 		 * NOTE: since TCP options buffer doesn't point into
19486 		 * mbuf's data, calculate offset and use it.
19487 		 */
19488 		if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
19489 						       (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
19490 			/*
19491 			 * Do not send segment if the calculation of MD5
19492 			 * digest has failed.
19493 			 */
19494 			goto failed;
19495 		}
19496 	}
19497 #endif
19498 #ifdef INET6
19499 	if (rack->r_is_v6) {
19500 		if (tp->t_port) {
19501 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
19502 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
19503 			udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
19504 			th->th_sum = htons(0);
19505 			UDPSTAT_INC(udps_opackets);
19506 		} else {
19507 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
19508 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
19509 			th->th_sum = in6_cksum_pseudo(ip6,
19510 						      sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
19511 						      0);
19512 		}
19513 	}
19514 #endif
19515 #if defined(INET6) && defined(INET)
19516 	else
19517 #endif
19518 #ifdef INET
19519 	{
19520 		if (tp->t_port) {
19521 			m->m_pkthdr.csum_flags = CSUM_UDP;
19522 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
19523 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
19524 						ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
19525 			th->th_sum = htons(0);
19526 			UDPSTAT_INC(udps_opackets);
19527 		} else {
19528 			m->m_pkthdr.csum_flags = CSUM_TCP;
19529 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
19530 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
19531 					       ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
19532 									IPPROTO_TCP + len + optlen));
19533 		}
19534 		/* IP version must be set here for ipv4/ipv6 checking later */
19535 		KASSERT(ip->ip_v == IPVERSION,
19536 			("%s: IP version incorrect: %d", __func__, ip->ip_v));
19537 	}
19538 #endif
19539 	if (tso) {
19540 		/*
19541 		 * Here we use segsiz since we have no added options besides
19542 		 * any standard timestamp options (no DSACKs or SACKS are sent
19543 		 * via either fast-path).
19544 		 */
19545 		KASSERT(len > segsiz,
19546 			("%s: len <= tso_segsz tp:%p", __func__, tp));
19547 		m->m_pkthdr.csum_flags |= CSUM_TSO;
19548 		m->m_pkthdr.tso_segsz = segsiz;
19549 	}
19550 #ifdef INET6
19551 	if (rack->r_is_v6) {
19552 		ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit;
19553 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
19554 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
19555 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
19556 		else
19557 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
19558 	}
19559 #endif
19560 #if defined(INET) && defined(INET6)
19561 	else
19562 #endif
19563 #ifdef INET
19564 	{
19565 		ip->ip_len = htons(m->m_pkthdr.len);
19566 		ip->ip_ttl = rack->r_ctl.fsb.hoplimit;
19567 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
19568 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
19569 			if (tp->t_port == 0 || len < V_tcp_minmss) {
19570 				ip->ip_off |= htons(IP_DF);
19571 			}
19572 		} else {
19573 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
19574 		}
19575 	}
19576 #endif
19577 	if (tp->snd_cwnd > tp->snd_ssthresh) {
19578 		/* Set we sent in CA */
19579 		rack->rc_gp_saw_ca = 1;
19580 	} else {
19581 		/* Set we sent in SS */
19582 		rack->rc_gp_saw_ss = 1;
19583 	}
19584 	/* Time to copy in our header */
19585 	cpto = mtod(m, uint8_t *);
19586 	memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
19587 	th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
19588 	if (optlen) {
19589 		bcopy(opt, th + 1, optlen);
19590 		th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
19591 	} else {
19592 		th->th_off = sizeof(struct tcphdr) >> 2;
19593 	}
19594 	if ((rack->r_ctl.crte != NULL) &&
19595 	    tcp_bblogging_on(tp)) {
19596 		rack_log_queue_level(tp, rack, len, tv, cts);
19597 	}
19598 	if (tcp_bblogging_on(rack->rc_tp)) {
19599 		union tcp_log_stackspecific log;
19600 
19601 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
19602 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
19603 		if (rack->rack_no_prr)
19604 			log.u_bbr.flex1 = 0;
19605 		else
19606 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
19607 		log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
19608 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
19609 		log.u_bbr.flex4 = max_val;
19610 		/* Save off the early/late values */
19611 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
19612 		log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
19613 		log.u_bbr.bw_inuse = rack_get_bw(rack);
19614 		log.u_bbr.cur_del_rate = rack->r_ctl.gp_bw;
19615 		log.u_bbr.flex8 = 0;
19616 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
19617 		log.u_bbr.flex7 = 44;
19618 		log.u_bbr.pkts_out = tp->t_maxseg;
19619 		log.u_bbr.timeStamp = cts;
19620 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
19621 		log.u_bbr.flex5 = log.u_bbr.inflight;
19622 		log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use;
19623 		log.u_bbr.delivered = 0;
19624 		log.u_bbr.rttProp = 0;
19625 		log.u_bbr.delRate = rack->r_must_retran;
19626 		log.u_bbr.delRate <<= 1;
19627 		log.u_bbr.pkt_epoch = __LINE__;
19628 		/* For fast output no retrans so just inflight and how many mss we send */
19629 		log.u_bbr.flex5 = log.u_bbr.inflight;
19630 		log.u_bbr.bbr_substate = (uint8_t)((len + segsiz - 1)/segsiz);
19631 		lgb = tcp_log_event(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
19632 				     len, &log, false, NULL, __func__, __LINE__, tv);
19633 	} else
19634 		lgb = NULL;
19635 #ifdef INET6
19636 	if (rack->r_is_v6) {
19637 		error = ip6_output(m, inp->in6p_outputopts,
19638 				   &inp->inp_route6,
19639 				   0, NULL, NULL, inp);
19640 	}
19641 #endif
19642 #if defined(INET) && defined(INET6)
19643 	else
19644 #endif
19645 #ifdef INET
19646 	{
19647 		error = ip_output(m, NULL,
19648 				  &inp->inp_route,
19649 				  0, 0, inp);
19650 	}
19651 #endif
19652 	if (lgb) {
19653 		lgb->tlb_errno = error;
19654 		lgb = NULL;
19655 	}
19656 	if (error) {
19657 		*send_err = error;
19658 		m = NULL;
19659 		goto failed;
19660 	} else if (rack->rc_hw_nobuf) {
19661 		rack->rc_hw_nobuf = 0;
19662 		rack->r_ctl.rc_agg_delayed = 0;
19663 		rack->r_early = 0;
19664 		rack->r_late = 0;
19665 		rack->r_ctl.rc_agg_early = 0;
19666 	}
19667 	if ((error == 0) && (rack->lt_bw_up == 0)) {
19668 		/* Unlikely */
19669 		rack->r_ctl.lt_timemark = tcp_tv_to_lusectick(tv);
19670 		rack->r_ctl.lt_seq = tp->snd_una;
19671 		rack->lt_bw_up = 1;
19672 	}
19673 	rack_log_output(tp, &to, len, tp->snd_max, flags, error, rack_to_usec_ts(tv),
19674 			NULL, add_flag, s_mb, s_soff, rack->r_ctl.fsb.hw_tls, segsiz);
19675 	m = NULL;
19676 	if (tp->snd_una == tp->snd_max) {
19677 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
19678 		rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
19679 		tp->t_acktime = ticks;
19680 	}
19681 	counter_u64_add(rack_total_bytes, len);
19682 	tcp_account_for_send(tp, len, 0, 0, rack->r_ctl.fsb.hw_tls);
19683 
19684 	rack->forced_ack = 0;	/* If we send something zap the FA flag */
19685 	tot_len += len;
19686 	if ((tp->t_flags & TF_GPUTINPROG) == 0)
19687 		rack_start_gp_measurement(tp, rack, tp->snd_max, sb_offset);
19688 	tp->snd_max += len;
19689 	tp->snd_nxt = tp->snd_max;
19690 	if (rack->rc_new_rnd_needed) {
19691 		/*
19692 		 * Update the rnd to start ticking not
19693 		 * that from a time perspective all of
19694 		 * the preceding idle time is "in the round"
19695 		 */
19696 		rack->rc_new_rnd_needed = 0;
19697 		rack->r_ctl.roundends = tp->snd_max;
19698 	}
19699 	{
19700 		int idx;
19701 
19702 		idx = (len / segsiz) + 3;
19703 		if (idx >= TCP_MSS_ACCT_ATIMER)
19704 			counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
19705 		else
19706 			counter_u64_add(rack_out_size[idx], 1);
19707 	}
19708 	if (len <= rack->r_ctl.fsb.left_to_send)
19709 		rack->r_ctl.fsb.left_to_send -= len;
19710 	else
19711 		rack->r_ctl.fsb.left_to_send = 0;
19712 	if (rack->r_ctl.fsb.left_to_send < segsiz) {
19713 		rack->r_fast_output = 0;
19714 		rack->r_ctl.fsb.left_to_send = 0;
19715 		/* At the end of fast_output scale up the sb */
19716 		SOCKBUF_LOCK(&rack->rc_inp->inp_socket->so_snd);
19717 		rack_sndbuf_autoscale(rack);
19718 		SOCKBUF_UNLOCK(&rack->rc_inp->inp_socket->so_snd);
19719 	}
19720 	if (tp->t_rtttime == 0) {
19721 		tp->t_rtttime = ticks;
19722 		tp->t_rtseq = startseq;
19723 		KMOD_TCPSTAT_INC(tcps_segstimed);
19724 	}
19725 	if ((rack->r_ctl.fsb.left_to_send >= segsiz) &&
19726 	    (max_val > len) &&
19727 	    (tso == 0)) {
19728 		max_val -= len;
19729 		len = segsiz;
19730 		th = rack->r_ctl.fsb.th;
19731 #ifdef TCP_ACCOUNTING
19732 		cnt_thru++;
19733 #endif
19734 		goto again;
19735 	}
19736 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
19737 	counter_u64_add(rack_fto_send, 1);
19738 	slot = rack_get_pacing_delay(rack, tp, tot_len, NULL, segsiz);
19739 	rack_start_hpts_timer(rack, tp, cts, slot, tot_len, 0);
19740 #ifdef TCP_ACCOUNTING
19741 	crtsc = get_cyclecount();
19742 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19743 		tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru;
19744 	}
19745 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19746 		tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
19747 	}
19748 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19749 		tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len + segsiz - 1) / segsiz);
19750 	}
19751 	sched_unpin();
19752 #endif
19753 	return (0);
19754 failed:
19755 	if (m)
19756 		m_free(m);
19757 	rack->r_fast_output = 0;
19758 	return (-1);
19759 }
19760 
19761 static inline void
19762 rack_setup_fast_output(struct tcpcb *tp, struct tcp_rack *rack,
19763 		       struct sockbuf *sb,
19764 		       int len, int orig_len, int segsiz, uint32_t pace_max_seg,
19765 		       bool hw_tls,
19766 		       uint16_t flags)
19767 {
19768 	rack->r_fast_output = 1;
19769 	rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
19770 	rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
19771 	rack->r_ctl.fsb.o_t_len = M_TRAILINGROOM(rack->r_ctl.fsb.m);
19772 	rack->r_ctl.fsb.tcp_flags = flags;
19773 	rack->r_ctl.fsb.left_to_send = orig_len - len;
19774 	if (rack->r_ctl.fsb.left_to_send < pace_max_seg) {
19775 		/* Less than a full sized pace, lets not  */
19776 		rack->r_fast_output = 0;
19777 		return;
19778 	} else {
19779 		/* Round down to the nearest pace_max_seg */
19780 		rack->r_ctl.fsb.left_to_send = rounddown(rack->r_ctl.fsb.left_to_send, pace_max_seg);
19781 	}
19782 	if (hw_tls)
19783 		rack->r_ctl.fsb.hw_tls = 1;
19784 	else
19785 		rack->r_ctl.fsb.hw_tls = 0;
19786 	KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(sb) - (tp->snd_max - tp->snd_una))),
19787 		("rack:%p left_to_send:%u sbavail:%u out:%u",
19788 		 rack, rack->r_ctl.fsb.left_to_send, sbavail(sb),
19789 		 (tp->snd_max - tp->snd_una)));
19790 	if (rack->r_ctl.fsb.left_to_send < segsiz)
19791 		rack->r_fast_output = 0;
19792 	else {
19793 		if (rack->r_ctl.fsb.left_to_send == (sbavail(sb) - (tp->snd_max - tp->snd_una)))
19794 			rack->r_ctl.fsb.rfo_apply_push = 1;
19795 		else
19796 			rack->r_ctl.fsb.rfo_apply_push = 0;
19797 	}
19798 }
19799 
19800 static uint32_t
19801 rack_get_hpts_pacing_min_for_bw(struct tcp_rack *rack, int32_t segsiz)
19802 {
19803 	uint64_t min_time;
19804 	uint32_t maxlen;
19805 
19806 	min_time = (uint64_t)get_hpts_min_sleep_time();
19807 	maxlen = (uint32_t)((rack->r_ctl.gp_bw * min_time) / (uint64_t)HPTS_USEC_IN_SEC);
19808 	maxlen = roundup(maxlen, segsiz);
19809 	return (maxlen);
19810 }
19811 
19812 static struct rack_sendmap *
19813 rack_check_collapsed(struct tcp_rack *rack, uint32_t cts)
19814 {
19815 	struct rack_sendmap *rsm = NULL;
19816 	int thresh;
19817 
19818 restart:
19819 	rsm = tqhash_find(rack->r_ctl.tqh, rack->r_ctl.last_collapse_point);
19820 	if ((rsm == NULL) || ((rsm->r_flags & RACK_RWND_COLLAPSED) == 0)) {
19821 		/* Nothing, strange turn off validity  */
19822 		rack->r_collapse_point_valid = 0;
19823 		return (NULL);
19824 	}
19825 	/* Can we send it yet? */
19826 	if (rsm->r_end > (rack->rc_tp->snd_una + rack->rc_tp->snd_wnd)) {
19827 		/*
19828 		 * Receiver window has not grown enough for
19829 		 * the segment to be put on the wire.
19830 		 */
19831 		return (NULL);
19832 	}
19833 	if (rsm->r_flags & RACK_ACKED) {
19834 		/*
19835 		 * It has been sacked, lets move to the
19836 		 * next one if possible.
19837 		 */
19838 		rack->r_ctl.last_collapse_point = rsm->r_end;
19839 		/* Are we done? */
19840 		if (SEQ_GEQ(rack->r_ctl.last_collapse_point,
19841 			    rack->r_ctl.high_collapse_point)) {
19842 			rack->r_collapse_point_valid = 0;
19843 			return (NULL);
19844 		}
19845 		goto restart;
19846 	}
19847 	/* Now has it been long enough ? */
19848 	thresh = rack_calc_thresh_rack(rack, rack_grab_rtt(rack->rc_tp, rack), cts);
19849 	if ((cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])) > thresh) {
19850 		rack_log_collapse(rack, rsm->r_start,
19851 				  (cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])),
19852 				  thresh, __LINE__, 6, rsm->r_flags, rsm);
19853 		return (rsm);
19854 	}
19855 	/* Not enough time */
19856 	rack_log_collapse(rack, rsm->r_start,
19857 			  (cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])),
19858 			  thresh, __LINE__, 7, rsm->r_flags, rsm);
19859 	return (NULL);
19860 }
19861 
19862 static inline void
19863 rack_validate_sizes(struct tcp_rack *rack, int32_t *len, int32_t segsiz, uint32_t pace_max_seg)
19864 {
19865 	if ((rack->full_size_rxt == 0) &&
19866 	    (rack->shape_rxt_to_pacing_min == 0) &&
19867 	    (*len >= segsiz)) {
19868 		*len = segsiz;
19869 	} else if (rack->shape_rxt_to_pacing_min &&
19870 		 rack->gp_ready) {
19871 		/* We use pacing min as shaping len req */
19872 		uint32_t maxlen;
19873 
19874 		maxlen = rack_get_hpts_pacing_min_for_bw(rack, segsiz);
19875 		if (*len > maxlen)
19876 			*len = maxlen;
19877 	} else {
19878 		/*
19879 		 * The else is full_size_rxt is on so send it all
19880 		 * note we do need to check this for exceeding
19881 		 * our max segment size due to the fact that
19882 		 * we do sometimes merge chunks together i.e.
19883 		 * we cannot just assume that we will never have
19884 		 * a chunk greater than pace_max_seg
19885 		 */
19886 		if (*len > pace_max_seg)
19887 			*len = pace_max_seg;
19888 	}
19889 }
19890 
19891 static int
19892 rack_output(struct tcpcb *tp)
19893 {
19894 	struct socket *so;
19895 	uint32_t recwin;
19896 	uint32_t sb_offset, s_moff = 0;
19897 	int32_t len, error = 0;
19898 	uint16_t flags;
19899 	struct mbuf *m, *s_mb = NULL;
19900 	struct mbuf *mb;
19901 	uint32_t if_hw_tsomaxsegcount = 0;
19902 	uint32_t if_hw_tsomaxsegsize;
19903 	int32_t segsiz, minseg;
19904 	long tot_len_this_send = 0;
19905 #ifdef INET
19906 	struct ip *ip = NULL;
19907 #endif
19908 	struct udphdr *udp = NULL;
19909 	struct tcp_rack *rack;
19910 	struct tcphdr *th;
19911 	uint8_t pass = 0;
19912 	uint8_t mark = 0;
19913 	uint8_t check_done = 0;
19914 	uint8_t wanted_cookie = 0;
19915 	u_char opt[TCP_MAXOLEN];
19916 	unsigned ipoptlen, optlen, hdrlen, ulen=0;
19917 	uint32_t rack_seq;
19918 
19919 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
19920 	unsigned ipsec_optlen = 0;
19921 
19922 #endif
19923 	int32_t idle, sendalot;
19924 	int32_t sub_from_prr = 0;
19925 	volatile int32_t sack_rxmit;
19926 	struct rack_sendmap *rsm = NULL;
19927 	int32_t tso, mtu;
19928 	struct tcpopt to;
19929 	int32_t slot = 0;
19930 	int32_t sup_rack = 0;
19931 	uint32_t cts, ms_cts, delayed, early;
19932 	uint16_t add_flag = RACK_SENT_SP;
19933 	/* The doing_tlp flag will be set by the actual rack_timeout_tlp() */
19934 	uint8_t doing_tlp = 0;
19935 	uint32_t cwnd_to_use, pace_max_seg;
19936 	int32_t do_a_prefetch = 0;
19937 	int32_t prefetch_rsm = 0;
19938 	int32_t orig_len = 0;
19939 	struct timeval tv;
19940 	int32_t prefetch_so_done = 0;
19941 	struct tcp_log_buffer *lgb;
19942 	struct inpcb *inp = tptoinpcb(tp);
19943 	struct sockbuf *sb;
19944 	uint64_t ts_val = 0;
19945 #ifdef TCP_ACCOUNTING
19946 	uint64_t crtsc;
19947 #endif
19948 #ifdef INET6
19949 	struct ip6_hdr *ip6 = NULL;
19950 	int32_t isipv6;
19951 #endif
19952 	bool hpts_calling, hw_tls = false;
19953 
19954 	NET_EPOCH_ASSERT();
19955 	INP_WLOCK_ASSERT(inp);
19956 
19957 	/* setup and take the cache hits here */
19958 	rack = (struct tcp_rack *)tp->t_fb_ptr;
19959 #ifdef TCP_ACCOUNTING
19960 	sched_pin();
19961 	ts_val = get_cyclecount();
19962 #endif
19963 	hpts_calling = !!(tp->t_flags2 & TF2_HPTS_CALLS);
19964 	tp->t_flags2 &= ~TF2_HPTS_CALLS;
19965 #ifdef TCP_OFFLOAD
19966 	if (tp->t_flags & TF_TOE) {
19967 #ifdef TCP_ACCOUNTING
19968 		sched_unpin();
19969 #endif
19970 		return (tcp_offload_output(tp));
19971 	}
19972 #endif
19973 	if (rack->rack_deferred_inited == 0) {
19974 		/*
19975 		 * If we are the connecting socket we will
19976 		 * hit rack_init() when no sequence numbers
19977 		 * are setup. This makes it so we must defer
19978 		 * some initialization. Call that now.
19979 		 */
19980 		rack_deferred_init(tp, rack);
19981 	}
19982 	/*
19983 	 * For TFO connections in SYN_RECEIVED, only allow the initial
19984 	 * SYN|ACK and those sent by the retransmit timer.
19985 	 */
19986 	if (IS_FASTOPEN(tp->t_flags) &&
19987 	    (tp->t_state == TCPS_SYN_RECEIVED) &&
19988 	    SEQ_GT(tp->snd_max, tp->snd_una) &&    /* initial SYN|ACK sent */
19989 	    (rack->r_ctl.rc_resend == NULL)) {         /* not a retransmit */
19990 #ifdef TCP_ACCOUNTING
19991 		sched_unpin();
19992 #endif
19993 		return (0);
19994 	}
19995 #ifdef INET6
19996 	if (rack->r_state) {
19997 		/* Use the cache line loaded if possible */
19998 		isipv6 = rack->r_is_v6;
19999 	} else {
20000 		isipv6 = (rack->rc_inp->inp_vflag & INP_IPV6) != 0;
20001 	}
20002 #endif
20003 	early = 0;
20004 	cts = tcp_get_usecs(&tv);
20005 	ms_cts = tcp_tv_to_mssectick(&tv);
20006 	if (((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) &&
20007 	    tcp_in_hpts(rack->rc_tp)) {
20008 		/*
20009 		 * We are on the hpts for some timer but not hptsi output.
20010 		 * Remove from the hpts unconditionally.
20011 		 */
20012 		rack_timer_cancel(tp, rack, cts, __LINE__);
20013 	}
20014 	/* Are we pacing and late? */
20015 	if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
20016 	    TSTMP_GEQ(cts, rack->r_ctl.rc_last_output_to)) {
20017 		/* We are delayed */
20018 		delayed = cts - rack->r_ctl.rc_last_output_to;
20019 	} else {
20020 		delayed = 0;
20021 	}
20022 	/* Do the timers, which may override the pacer */
20023 	if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
20024 		int retval;
20025 
20026 		retval = rack_process_timers(tp, rack, cts, hpts_calling,
20027 					     &doing_tlp);
20028 		if (retval != 0) {
20029 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_ATIMER], 1);
20030 #ifdef TCP_ACCOUNTING
20031 			sched_unpin();
20032 #endif
20033 			/*
20034 			 * If timers want tcp_drop(), then pass error out,
20035 			 * otherwise suppress it.
20036 			 */
20037 			return (retval < 0 ? retval : 0);
20038 		}
20039 	}
20040 	if (rack->rc_in_persist) {
20041 		if (tcp_in_hpts(rack->rc_tp) == 0) {
20042 			/* Timer is not running */
20043 			rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
20044 		}
20045 #ifdef TCP_ACCOUNTING
20046 		sched_unpin();
20047 #endif
20048 		return (0);
20049 	}
20050 	if ((rack->rc_ack_required == 1) &&
20051 	    (rack->r_timer_override == 0)){
20052 		/* A timeout occurred and no ack has arrived */
20053 		if (tcp_in_hpts(rack->rc_tp) == 0) {
20054 			/* Timer is not running */
20055 			rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
20056 		}
20057 #ifdef TCP_ACCOUNTING
20058 		sched_unpin();
20059 #endif
20060 		return (0);
20061 	}
20062 	if ((rack->r_timer_override) ||
20063 	    (rack->rc_ack_can_sendout_data) ||
20064 	    (delayed) ||
20065 	    (tp->t_state < TCPS_ESTABLISHED)) {
20066 		rack->rc_ack_can_sendout_data = 0;
20067 		if (tcp_in_hpts(rack->rc_tp))
20068 			tcp_hpts_remove(rack->rc_tp);
20069 	} else if (tcp_in_hpts(rack->rc_tp)) {
20070 		/*
20071 		 * On the hpts you can't pass even if ACKNOW is on, we will
20072 		 * when the hpts fires.
20073 		 */
20074 #ifdef TCP_ACCOUNTING
20075 		crtsc = get_cyclecount();
20076 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
20077 			tp->tcp_proc_time[SND_BLOCKED] += (crtsc - ts_val);
20078 		}
20079 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
20080 			tp->tcp_cnt_counters[SND_BLOCKED]++;
20081 		}
20082 		sched_unpin();
20083 #endif
20084 		counter_u64_add(rack_out_size[TCP_MSS_ACCT_INPACE], 1);
20085 		return (0);
20086 	}
20087 	/* Finish out both pacing early and late accounting */
20088 	if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
20089 	    TSTMP_GT(rack->r_ctl.rc_last_output_to, cts)) {
20090 		early = rack->r_ctl.rc_last_output_to - cts;
20091 	} else
20092 		early = 0;
20093 	if (delayed) {
20094 		rack->r_ctl.rc_agg_delayed += delayed;
20095 		rack->r_late = 1;
20096 	} else if (early) {
20097 		rack->r_ctl.rc_agg_early += early;
20098 		rack->r_early = 1;
20099 	}
20100 	/* Now that early/late accounting is done turn off the flag */
20101 	rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
20102 	rack->r_wanted_output = 0;
20103 	rack->r_timer_override = 0;
20104 	if ((tp->t_state != rack->r_state) &&
20105 	    TCPS_HAVEESTABLISHED(tp->t_state)) {
20106 		rack_set_state(tp, rack);
20107 	}
20108 	if ((rack->r_fast_output) &&
20109 	    (doing_tlp == 0) &&
20110 	    (tp->rcv_numsacks == 0)) {
20111 		int ret;
20112 
20113 		error = 0;
20114 		ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error);
20115 		if (ret >= 0)
20116 			return(ret);
20117 		else if (error) {
20118 			inp = rack->rc_inp;
20119 			so = inp->inp_socket;
20120 			sb = &so->so_snd;
20121 			goto nomore;
20122 		}
20123 	}
20124 	inp = rack->rc_inp;
20125 	/*
20126 	 * For TFO connections in SYN_SENT or SYN_RECEIVED,
20127 	 * only allow the initial SYN or SYN|ACK and those sent
20128 	 * by the retransmit timer.
20129 	 */
20130 	if (IS_FASTOPEN(tp->t_flags) &&
20131 	    ((tp->t_state == TCPS_SYN_RECEIVED) ||
20132 	     (tp->t_state == TCPS_SYN_SENT)) &&
20133 	    SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */
20134 	    (tp->t_rxtshift == 0)) {              /* not a retransmit */
20135 		cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
20136 		so = inp->inp_socket;
20137 		sb = &so->so_snd;
20138 		goto just_return_nolock;
20139 	}
20140 	/*
20141 	 * Determine length of data that should be transmitted, and flags
20142 	 * that will be used. If there is some data or critical controls
20143 	 * (SYN, RST) to send, then transmit; otherwise, investigate
20144 	 * further.
20145 	 */
20146 	idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
20147 	if (tp->t_idle_reduce) {
20148 		if (idle && (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur))
20149 			rack_cc_after_idle(rack, tp);
20150 	}
20151 	tp->t_flags &= ~TF_LASTIDLE;
20152 	if (idle) {
20153 		if (tp->t_flags & TF_MORETOCOME) {
20154 			tp->t_flags |= TF_LASTIDLE;
20155 			idle = 0;
20156 		}
20157 	}
20158 	if ((tp->snd_una == tp->snd_max) &&
20159 	    rack->r_ctl.rc_went_idle_time &&
20160 	    TSTMP_GT(cts, rack->r_ctl.rc_went_idle_time)) {
20161 		idle = cts - rack->r_ctl.rc_went_idle_time;
20162 		if (idle > rack_min_probertt_hold) {
20163 			/* Count as a probe rtt */
20164 			if (rack->in_probe_rtt == 0) {
20165 				rack->r_ctl.rc_lower_rtt_us_cts = cts;
20166 				rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts;
20167 				rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts;
20168 				rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts;
20169 			} else {
20170 				rack_exit_probertt(rack, cts);
20171 			}
20172 		}
20173 		idle = 0;
20174 	}
20175 	if (rack_use_fsb &&
20176 	    (rack->r_ctl.fsb.tcp_ip_hdr) &&
20177 	    (rack->r_fsb_inited == 0) &&
20178 	    (rack->r_state != TCPS_CLOSED))
20179 		rack_init_fsb_block(tp, rack, tcp_outflags[tp->t_state]);
20180 again:
20181 	/*
20182 	 * If we've recently taken a timeout, snd_max will be greater than
20183 	 * snd_nxt.  There may be SACK information that allows us to avoid
20184 	 * resending already delivered data.  Adjust snd_nxt accordingly.
20185 	 */
20186 	sendalot = 0;
20187 	cts = tcp_get_usecs(&tv);
20188 	ms_cts = tcp_tv_to_mssectick(&tv);
20189 	tso = 0;
20190 	mtu = 0;
20191 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
20192 	minseg = segsiz;
20193 	if (rack->r_ctl.rc_pace_max_segs == 0)
20194 		pace_max_seg = rack->rc_user_set_max_segs * segsiz;
20195 	else
20196 		pace_max_seg = rack->r_ctl.rc_pace_max_segs;
20197 	sb_offset = tp->snd_max - tp->snd_una;
20198 	cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
20199 	flags = tcp_outflags[tp->t_state];
20200 	while (rack->rc_free_cnt < rack_free_cache) {
20201 		rsm = rack_alloc(rack);
20202 		if (rsm == NULL) {
20203 			if (hpts_calling)
20204 				/* Retry in a ms */
20205 				slot = (1 * HPTS_USEC_IN_MSEC);
20206 			so = inp->inp_socket;
20207 			sb = &so->so_snd;
20208 			goto just_return_nolock;
20209 		}
20210 		TAILQ_INSERT_TAIL(&rack->r_ctl.rc_free, rsm, r_tnext);
20211 		rack->rc_free_cnt++;
20212 		rsm = NULL;
20213 	}
20214 	sack_rxmit = 0;
20215 	len = 0;
20216 	rsm = NULL;
20217 	if (flags & TH_RST) {
20218 		SOCKBUF_LOCK(&inp->inp_socket->so_snd);
20219 		so = inp->inp_socket;
20220 		sb = &so->so_snd;
20221 		goto send;
20222 	}
20223 	if (rack->r_ctl.rc_resend) {
20224 		/* Retransmit timer */
20225 		rsm = rack->r_ctl.rc_resend;
20226 		rack->r_ctl.rc_resend = NULL;
20227 		len = rsm->r_end - rsm->r_start;
20228 		sack_rxmit = 1;
20229 		sendalot = 0;
20230 		KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
20231 			("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
20232 			 __func__, __LINE__,
20233 			 rsm->r_start, tp->snd_una, tp, rack, rsm));
20234 		sb_offset = rsm->r_start - tp->snd_una;
20235 		rack_validate_sizes(rack, &len, segsiz, pace_max_seg);
20236 	} else if (rack->r_collapse_point_valid &&
20237 		   ((rsm = rack_check_collapsed(rack, cts)) != NULL)) {
20238 		/*
20239 		 * If an RSM is returned then enough time has passed
20240 		 * for us to retransmit it. Move up the collapse point,
20241 		 * since this rsm has its chance to retransmit now.
20242 		 */
20243 		tcp_trace_point(rack->rc_tp, TCP_TP_COLLAPSED_RXT);
20244 		rack->r_ctl.last_collapse_point = rsm->r_end;
20245 		/* Are we done? */
20246 		if (SEQ_GEQ(rack->r_ctl.last_collapse_point,
20247 			    rack->r_ctl.high_collapse_point))
20248 			rack->r_collapse_point_valid = 0;
20249 		sack_rxmit = 1;
20250 		/* We are not doing a TLP */
20251 		doing_tlp = 0;
20252 		len = rsm->r_end - rsm->r_start;
20253 		sb_offset = rsm->r_start - tp->snd_una;
20254 		sendalot = 0;
20255 		rack_validate_sizes(rack, &len, segsiz, pace_max_seg);
20256 	} else if ((rsm = tcp_rack_output(tp, rack, cts)) != NULL) {
20257 		/* We have a retransmit that takes precedence */
20258 		if ((!IN_FASTRECOVERY(tp->t_flags)) &&
20259 		    ((rsm->r_flags & RACK_MUST_RXT) == 0) &&
20260 		    ((tp->t_flags & TF_WASFRECOVERY) == 0)) {
20261 			/* Enter recovery if not induced by a time-out */
20262 			rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__);
20263 		}
20264 #ifdef INVARIANTS
20265 		if (SEQ_LT(rsm->r_start, tp->snd_una)) {
20266 			panic("Huh, tp:%p rack:%p rsm:%p start:%u < snd_una:%u\n",
20267 			      tp, rack, rsm, rsm->r_start, tp->snd_una);
20268 		}
20269 #endif
20270 		len = rsm->r_end - rsm->r_start;
20271 		KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
20272 			("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
20273 			 __func__, __LINE__,
20274 			 rsm->r_start, tp->snd_una, tp, rack, rsm));
20275 		sb_offset = rsm->r_start - tp->snd_una;
20276 		sendalot = 0;
20277 		rack_validate_sizes(rack, &len, segsiz, pace_max_seg);
20278 		if (len > 0) {
20279 			sack_rxmit = 1;
20280 			KMOD_TCPSTAT_INC(tcps_sack_rexmits);
20281 			KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes,
20282 					 min(len, segsiz));
20283 		}
20284 	} else if (rack->r_ctl.rc_tlpsend) {
20285 		/* Tail loss probe */
20286 		long cwin;
20287 		long tlen;
20288 
20289 		/*
20290 		 * Check if we can do a TLP with a RACK'd packet
20291 		 * this can happen if we are not doing the rack
20292 		 * cheat and we skipped to a TLP and it
20293 		 * went off.
20294 		 */
20295 		rsm = rack->r_ctl.rc_tlpsend;
20296 		/* We are doing a TLP make sure the flag is preent */
20297 		rsm->r_flags |= RACK_TLP;
20298 		rack->r_ctl.rc_tlpsend = NULL;
20299 		sack_rxmit = 1;
20300 		tlen = rsm->r_end - rsm->r_start;
20301 		if (tlen > segsiz)
20302 			tlen = segsiz;
20303 		KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
20304 			("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
20305 			 __func__, __LINE__,
20306 			 rsm->r_start, tp->snd_una, tp, rack, rsm));
20307 		sb_offset = rsm->r_start - tp->snd_una;
20308 		cwin = min(tp->snd_wnd, tlen);
20309 		len = cwin;
20310 	}
20311 	if (rack->r_must_retran &&
20312 	    (doing_tlp == 0) &&
20313 	    (SEQ_GT(tp->snd_max, tp->snd_una)) &&
20314 	    (rsm == NULL)) {
20315 		/*
20316 		 * There are two different ways that we
20317 		 * can get into this block:
20318 		 * a) This is a non-sack connection, we had a time-out
20319 		 *    and thus r_must_retran was set and everything
20320 		 *    left outstanding as been marked for retransmit.
20321 		 * b) The MTU of the path shrank, so that everything
20322 		 *    was marked to be retransmitted with the smaller
20323 		 *    mtu and r_must_retran was set.
20324 		 *
20325 		 * This means that we expect the sendmap (outstanding)
20326 		 * to all be marked must. We can use the tmap to
20327 		 * look at them.
20328 		 *
20329 		 */
20330 		int sendwin, flight;
20331 
20332 		sendwin = min(tp->snd_wnd, tp->snd_cwnd);
20333 		flight = ctf_flight_size(tp, rack->r_ctl.rc_out_at_rto);
20334 		if (flight >= sendwin) {
20335 			/*
20336 			 * We can't send yet.
20337 			 */
20338 			so = inp->inp_socket;
20339 			sb = &so->so_snd;
20340 			goto just_return_nolock;
20341 		}
20342 		/*
20343 		 * This is the case a/b mentioned above. All
20344 		 * outstanding/not-acked should be marked.
20345 		 * We can use the tmap to find them.
20346 		 */
20347 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
20348 		if (rsm == NULL) {
20349 			/* TSNH */
20350 			rack->r_must_retran = 0;
20351 			rack->r_ctl.rc_out_at_rto = 0;
20352 			so = inp->inp_socket;
20353 			sb = &so->so_snd;
20354 			goto just_return_nolock;
20355 		}
20356 		if ((rsm->r_flags & RACK_MUST_RXT) == 0) {
20357 			/*
20358 			 * The first one does not have the flag, did we collapse
20359 			 * further up in our list?
20360 			 */
20361 			rack->r_must_retran = 0;
20362 			rack->r_ctl.rc_out_at_rto = 0;
20363 			rsm = NULL;
20364 			sack_rxmit = 0;
20365 		} else {
20366 			sack_rxmit = 1;
20367 			len = rsm->r_end - rsm->r_start;
20368 			sb_offset = rsm->r_start - tp->snd_una;
20369 			sendalot = 0;
20370 			if ((rack->full_size_rxt == 0) &&
20371 			    (rack->shape_rxt_to_pacing_min == 0) &&
20372 			    (len >= segsiz))
20373 				len = segsiz;
20374 			else if (rack->shape_rxt_to_pacing_min &&
20375 				 rack->gp_ready) {
20376 				/* We use pacing min as shaping len req */
20377 				uint32_t maxlen;
20378 
20379 				maxlen = rack_get_hpts_pacing_min_for_bw(rack, segsiz);
20380 				if (len > maxlen)
20381 					len = maxlen;
20382 			}
20383 			/*
20384 			 * Delay removing the flag RACK_MUST_RXT so
20385 			 * that the fastpath for retransmit will
20386 			 * work with this rsm.
20387 			 */
20388 		}
20389 	}
20390 	/*
20391 	 * Enforce a connection sendmap count limit if set
20392 	 * as long as we are not retransmiting.
20393 	 */
20394 	if ((rsm == NULL) &&
20395 	    (rack->do_detection == 0) &&
20396 	    (V_tcp_map_entries_limit > 0) &&
20397 	    (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
20398 		counter_u64_add(rack_to_alloc_limited, 1);
20399 		if (!rack->alloc_limit_reported) {
20400 			rack->alloc_limit_reported = 1;
20401 			counter_u64_add(rack_alloc_limited_conns, 1);
20402 		}
20403 		so = inp->inp_socket;
20404 		sb = &so->so_snd;
20405 		goto just_return_nolock;
20406 	}
20407 	if (rsm && (rsm->r_flags & RACK_HAS_FIN)) {
20408 		/* we are retransmitting the fin */
20409 		len--;
20410 		if (len) {
20411 			/*
20412 			 * When retransmitting data do *not* include the
20413 			 * FIN. This could happen from a TLP probe.
20414 			 */
20415 			flags &= ~TH_FIN;
20416 		}
20417 	}
20418 	if (rsm && rack->r_fsb_inited &&
20419 	    rack_use_rsm_rfo &&
20420 	    ((rsm->r_flags & RACK_HAS_FIN) == 0)) {
20421 		int ret;
20422 
20423 		ret = rack_fast_rsm_output(tp, rack, rsm, ts_val, cts, ms_cts, &tv, len, doing_tlp);
20424 		if (ret == 0)
20425 			return (0);
20426 	}
20427 	so = inp->inp_socket;
20428 	sb = &so->so_snd;
20429 	if (do_a_prefetch == 0) {
20430 		kern_prefetch(sb, &do_a_prefetch);
20431 		do_a_prefetch = 1;
20432 	}
20433 #ifdef NETFLIX_SHARED_CWND
20434 	if ((tp->t_flags2 & TF2_TCP_SCWND_ALLOWED) &&
20435 	    rack->rack_enable_scwnd) {
20436 		/* We are doing cwnd sharing */
20437 		if (rack->gp_ready &&
20438 		    (rack->rack_attempted_scwnd == 0) &&
20439 		    (rack->r_ctl.rc_scw == NULL) &&
20440 		    tp->t_lib) {
20441 			/* The pcbid is in, lets make an attempt */
20442 			counter_u64_add(rack_try_scwnd, 1);
20443 			rack->rack_attempted_scwnd = 1;
20444 			rack->r_ctl.rc_scw = tcp_shared_cwnd_alloc(tp,
20445 								   &rack->r_ctl.rc_scw_index,
20446 								   segsiz);
20447 		}
20448 		if (rack->r_ctl.rc_scw &&
20449 		    (rack->rack_scwnd_is_idle == 1) &&
20450 		    sbavail(&so->so_snd)) {
20451 			/* we are no longer out of data */
20452 			tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
20453 			rack->rack_scwnd_is_idle = 0;
20454 		}
20455 		if (rack->r_ctl.rc_scw) {
20456 			/* First lets update and get the cwnd */
20457 			rack->r_ctl.cwnd_to_use = cwnd_to_use = tcp_shared_cwnd_update(rack->r_ctl.rc_scw,
20458 										       rack->r_ctl.rc_scw_index,
20459 										       tp->snd_cwnd, tp->snd_wnd, segsiz);
20460 		}
20461 	}
20462 #endif
20463 	/*
20464 	 * Get standard flags, and add SYN or FIN if requested by 'hidden'
20465 	 * state flags.
20466 	 */
20467 	if (tp->t_flags & TF_NEEDFIN)
20468 		flags |= TH_FIN;
20469 	if (tp->t_flags & TF_NEEDSYN)
20470 		flags |= TH_SYN;
20471 	if ((sack_rxmit == 0) && (prefetch_rsm == 0)) {
20472 		void *end_rsm;
20473 		end_rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
20474 		if (end_rsm)
20475 			kern_prefetch(end_rsm, &prefetch_rsm);
20476 		prefetch_rsm = 1;
20477 	}
20478 	SOCKBUF_LOCK(sb);
20479 	/*
20480 	 * If snd_nxt == snd_max and we have transmitted a FIN, the
20481 	 * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a
20482 	 * negative length.  This can also occur when TCP opens up its
20483 	 * congestion window while receiving additional duplicate acks after
20484 	 * fast-retransmit because TCP will reset snd_nxt to snd_max after
20485 	 * the fast-retransmit.
20486 	 *
20487 	 * In the normal retransmit-FIN-only case, however, snd_nxt will be
20488 	 * set to snd_una, the sb_offset will be 0, and the length may wind
20489 	 * up 0.
20490 	 *
20491 	 * If sack_rxmit is true we are retransmitting from the scoreboard
20492 	 * in which case len is already set.
20493 	 */
20494 	if ((sack_rxmit == 0) &&
20495 	    (TCPS_HAVEESTABLISHED(tp->t_state) || IS_FASTOPEN(tp->t_flags))) {
20496 		uint32_t avail;
20497 
20498 		avail = sbavail(sb);
20499 		if (SEQ_GT(tp->snd_nxt, tp->snd_una) && avail)
20500 			sb_offset = tp->snd_nxt - tp->snd_una;
20501 		else
20502 			sb_offset = 0;
20503 		if ((IN_FASTRECOVERY(tp->t_flags) == 0) || rack->rack_no_prr) {
20504 			if (rack->r_ctl.rc_tlp_new_data) {
20505 				/* TLP is forcing out new data */
20506 				if (rack->r_ctl.rc_tlp_new_data > (uint32_t) (avail - sb_offset)) {
20507 					rack->r_ctl.rc_tlp_new_data = (uint32_t) (avail - sb_offset);
20508 				}
20509 				if ((rack->r_ctl.rc_tlp_new_data + sb_offset) > tp->snd_wnd) {
20510 					if (tp->snd_wnd > sb_offset)
20511 						len = tp->snd_wnd - sb_offset;
20512 					else
20513 						len = 0;
20514 				} else {
20515 					len = rack->r_ctl.rc_tlp_new_data;
20516 				}
20517 				rack->r_ctl.rc_tlp_new_data = 0;
20518 			}  else {
20519 				len = rack_what_can_we_send(tp, rack, cwnd_to_use, avail, sb_offset);
20520 			}
20521 			if ((rack->r_ctl.crte == NULL) &&
20522 			    IN_FASTRECOVERY(tp->t_flags) &&
20523 			    (rack->full_size_rxt == 0) &&
20524 			    (rack->shape_rxt_to_pacing_min == 0) &&
20525 			    (len > segsiz)) {
20526 				/*
20527 				 * For prr=off, we need to send only 1 MSS
20528 				 * at a time. We do this because another sack could
20529 				 * be arriving that causes us to send retransmits and
20530 				 * we don't want to be on a long pace due to a larger send
20531 				 * that keeps us from sending out the retransmit.
20532 				 */
20533 				len = segsiz;
20534 			} else if (rack->shape_rxt_to_pacing_min &&
20535 				   rack->gp_ready) {
20536 				/* We use pacing min as shaping len req */
20537 				uint32_t maxlen;
20538 
20539 				maxlen = rack_get_hpts_pacing_min_for_bw(rack, segsiz);
20540 				if (len > maxlen)
20541 					len = maxlen;
20542 			}/* The else is full_size_rxt is on so send it all */
20543 		} else {
20544 			uint32_t outstanding;
20545 			/*
20546 			 * We are inside of a Fast recovery episode, this
20547 			 * is caused by a SACK or 3 dup acks. At this point
20548 			 * we have sent all the retransmissions and we rely
20549 			 * on PRR to dictate what we will send in the form of
20550 			 * new data.
20551 			 */
20552 
20553 			outstanding = tp->snd_max - tp->snd_una;
20554 			if ((rack->r_ctl.rc_prr_sndcnt + outstanding) > tp->snd_wnd) {
20555 				if (tp->snd_wnd > outstanding) {
20556 					len = tp->snd_wnd - outstanding;
20557 					/* Check to see if we have the data */
20558 					if ((sb_offset + len) > avail) {
20559 						/* It does not all fit */
20560 						if (avail > sb_offset)
20561 							len = avail - sb_offset;
20562 						else
20563 							len = 0;
20564 					}
20565 				} else {
20566 					len = 0;
20567 				}
20568 			} else if (avail > sb_offset) {
20569 				len = avail - sb_offset;
20570 			} else {
20571 				len = 0;
20572 			}
20573 			if (len > 0) {
20574 				if (len > rack->r_ctl.rc_prr_sndcnt) {
20575 					len = rack->r_ctl.rc_prr_sndcnt;
20576 				}
20577 				if (len > 0) {
20578 					sub_from_prr = 1;
20579 				}
20580 			}
20581 			if (len > segsiz) {
20582 				/*
20583 				 * We should never send more than a MSS when
20584 				 * retransmitting or sending new data in prr
20585 				 * mode unless the override flag is on. Most
20586 				 * likely the PRR algorithm is not going to
20587 				 * let us send a lot as well :-)
20588 				 */
20589 				if (rack->r_ctl.rc_prr_sendalot == 0) {
20590 					len = segsiz;
20591 				}
20592 			} else if (len < segsiz) {
20593 				/*
20594 				 * Do we send any? The idea here is if the
20595 				 * send empty's the socket buffer we want to
20596 				 * do it. However if not then lets just wait
20597 				 * for our prr_sndcnt to get bigger.
20598 				 */
20599 				long leftinsb;
20600 
20601 				leftinsb = sbavail(sb) - sb_offset;
20602 				if (leftinsb > len) {
20603 					/* This send does not empty the sb */
20604 					len = 0;
20605 				}
20606 			}
20607 		}
20608 	} else if (!TCPS_HAVEESTABLISHED(tp->t_state)) {
20609 		/*
20610 		 * If you have not established
20611 		 * and are not doing FAST OPEN
20612 		 * no data please.
20613 		 */
20614 		if ((sack_rxmit == 0) &&
20615 		    (!IS_FASTOPEN(tp->t_flags))){
20616 			len = 0;
20617 			sb_offset = 0;
20618 		}
20619 	}
20620 	if (prefetch_so_done == 0) {
20621 		kern_prefetch(so, &prefetch_so_done);
20622 		prefetch_so_done = 1;
20623 	}
20624 	/*
20625 	 * Lop off SYN bit if it has already been sent.  However, if this is
20626 	 * SYN-SENT state and if segment contains data and if we don't know
20627 	 * that foreign host supports TAO, suppress sending segment.
20628 	 */
20629 	if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una) &&
20630 	    ((sack_rxmit == 0) && (tp->t_rxtshift == 0))) {
20631 		/*
20632 		 * When sending additional segments following a TFO SYN|ACK,
20633 		 * do not include the SYN bit.
20634 		 */
20635 		if (IS_FASTOPEN(tp->t_flags) &&
20636 		    (tp->t_state == TCPS_SYN_RECEIVED))
20637 			flags &= ~TH_SYN;
20638 	}
20639 	/*
20640 	 * Be careful not to send data and/or FIN on SYN segments. This
20641 	 * measure is needed to prevent interoperability problems with not
20642 	 * fully conformant TCP implementations.
20643 	 */
20644 	if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
20645 		len = 0;
20646 		flags &= ~TH_FIN;
20647 	}
20648 	/*
20649 	 * On TFO sockets, ensure no data is sent in the following cases:
20650 	 *
20651 	 *  - When retransmitting SYN|ACK on a passively-created socket
20652 	 *
20653 	 *  - When retransmitting SYN on an actively created socket
20654 	 *
20655 	 *  - When sending a zero-length cookie (cookie request) on an
20656 	 *    actively created socket
20657 	 *
20658 	 *  - When the socket is in the CLOSED state (RST is being sent)
20659 	 */
20660 	if (IS_FASTOPEN(tp->t_flags) &&
20661 	    (((flags & TH_SYN) && (tp->t_rxtshift > 0)) ||
20662 	     ((tp->t_state == TCPS_SYN_SENT) &&
20663 	      (tp->t_tfo_client_cookie_len == 0)) ||
20664 	     (flags & TH_RST))) {
20665 		sack_rxmit = 0;
20666 		len = 0;
20667 	}
20668 	/* Without fast-open there should never be data sent on a SYN */
20669 	if ((flags & TH_SYN) && (!IS_FASTOPEN(tp->t_flags))) {
20670 		tp->snd_nxt = tp->iss;
20671 		len = 0;
20672 	}
20673 	if ((len > segsiz) && (tcp_dsack_block_exists(tp))) {
20674 		/* We only send 1 MSS if we have a DSACK block */
20675 		add_flag |= RACK_SENT_W_DSACK;
20676 		len = segsiz;
20677 	}
20678 	orig_len = len;
20679 	if (len <= 0) {
20680 		/*
20681 		 * If FIN has been sent but not acked, but we haven't been
20682 		 * called to retransmit, len will be < 0.  Otherwise, window
20683 		 * shrank after we sent into it.  If window shrank to 0,
20684 		 * cancel pending retransmit, pull snd_nxt back to (closed)
20685 		 * window, and set the persist timer if it isn't already
20686 		 * going.  If the window didn't close completely, just wait
20687 		 * for an ACK.
20688 		 *
20689 		 * We also do a general check here to ensure that we will
20690 		 * set the persist timer when we have data to send, but a
20691 		 * 0-byte window. This makes sure the persist timer is set
20692 		 * even if the packet hits one of the "goto send" lines
20693 		 * below.
20694 		 */
20695 		len = 0;
20696 		if ((tp->snd_wnd == 0) &&
20697 		    (TCPS_HAVEESTABLISHED(tp->t_state)) &&
20698 		    (tp->snd_una == tp->snd_max) &&
20699 		    (sb_offset < (int)sbavail(sb))) {
20700 			rack_enter_persist(tp, rack, cts, tp->snd_una);
20701 		}
20702 	} else if ((rsm == NULL) &&
20703 		   (doing_tlp == 0) &&
20704 		   (len < pace_max_seg)) {
20705 		/*
20706 		 * We are not sending a maximum sized segment for
20707 		 * some reason. Should we not send anything (think
20708 		 * sws or persists)?
20709 		 */
20710 		if ((tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg)) &&
20711 		    (TCPS_HAVEESTABLISHED(tp->t_state)) &&
20712 		    (len < minseg) &&
20713 		    (len < (int)(sbavail(sb) - sb_offset))) {
20714 			/*
20715 			 * Here the rwnd is less than
20716 			 * the minimum pacing size, this is not a retransmit,
20717 			 * we are established and
20718 			 * the send is not the last in the socket buffer
20719 			 * we send nothing, and we may enter persists
20720 			 * if nothing is outstanding.
20721 			 */
20722 			len = 0;
20723 			if (tp->snd_max == tp->snd_una) {
20724 				/*
20725 				 * Nothing out we can
20726 				 * go into persists.
20727 				 */
20728 				rack_enter_persist(tp, rack, cts, tp->snd_una);
20729 			}
20730 		} else if ((cwnd_to_use >= max(minseg, (segsiz * 4))) &&
20731 			   (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) &&
20732 			   (len < (int)(sbavail(sb) - sb_offset)) &&
20733 			   (len < minseg)) {
20734 			/*
20735 			 * Here we are not retransmitting, and
20736 			 * the cwnd is not so small that we could
20737 			 * not send at least a min size (rxt timer
20738 			 * not having gone off), We have 2 segments or
20739 			 * more already in flight, its not the tail end
20740 			 * of the socket buffer  and the cwnd is blocking
20741 			 * us from sending out a minimum pacing segment size.
20742 			 * Lets not send anything.
20743 			 */
20744 			len = 0;
20745 		} else if (((tp->snd_wnd - ctf_outstanding(tp)) <
20746 			    min((rack->r_ctl.rc_high_rwnd/2), minseg)) &&
20747 			   (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) &&
20748 			   (len < (int)(sbavail(sb) - sb_offset)) &&
20749 			   (TCPS_HAVEESTABLISHED(tp->t_state))) {
20750 			/*
20751 			 * Here we have a send window but we have
20752 			 * filled it up and we can't send another pacing segment.
20753 			 * We also have in flight more than 2 segments
20754 			 * and we are not completing the sb i.e. we allow
20755 			 * the last bytes of the sb to go out even if
20756 			 * its not a full pacing segment.
20757 			 */
20758 			len = 0;
20759 		} else if ((rack->r_ctl.crte != NULL) &&
20760 			   (tp->snd_wnd >= (pace_max_seg * max(1, rack_hw_rwnd_factor))) &&
20761 			   (cwnd_to_use >= (pace_max_seg + (4 * segsiz))) &&
20762 			   (ctf_flight_size(tp, rack->r_ctl.rc_sacked) >= (2 * segsiz)) &&
20763 			   (len < (int)(sbavail(sb) - sb_offset))) {
20764 			/*
20765 			 * Here we are doing hardware pacing, this is not a TLP,
20766 			 * we are not sending a pace max segment size, there is rwnd
20767 			 * room to send at least N pace_max_seg, the cwnd is greater
20768 			 * than or equal to a full pacing segments plus 4 mss and we have 2 or
20769 			 * more segments in flight and its not the tail of the socket buffer.
20770 			 *
20771 			 * We don't want to send instead we need to get more ack's in to
20772 			 * allow us to send a full pacing segment. Normally, if we are pacing
20773 			 * about the right speed, we should have finished our pacing
20774 			 * send as most of the acks have come back if we are at the
20775 			 * right rate. This is a bit fuzzy since return path delay
20776 			 * can delay the acks, which is why we want to make sure we
20777 			 * have cwnd space to have a bit more than a max pace segments in flight.
20778 			 *
20779 			 * If we have not gotten our acks back we are pacing at too high a
20780 			 * rate delaying will not hurt and will bring our GP estimate down by
20781 			 * injecting the delay. If we don't do this we will send
20782 			 * 2 MSS out in response to the acks being clocked in which
20783 			 * defeats the point of hw-pacing (i.e. to help us get
20784 			 * larger TSO's out).
20785 			 */
20786 			len = 0;
20787 		}
20788 
20789 	}
20790 	/* len will be >= 0 after this point. */
20791 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
20792 	rack_sndbuf_autoscale(rack);
20793 	/*
20794 	 * Decide if we can use TCP Segmentation Offloading (if supported by
20795 	 * hardware).
20796 	 *
20797 	 * TSO may only be used if we are in a pure bulk sending state.  The
20798 	 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP
20799 	 * options prevent using TSO.  With TSO the TCP header is the same
20800 	 * (except for the sequence number) for all generated packets.  This
20801 	 * makes it impossible to transmit any options which vary per
20802 	 * generated segment or packet.
20803 	 *
20804 	 * IPv4 handling has a clear separation of ip options and ip header
20805 	 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does
20806 	 * the right thing below to provide length of just ip options and thus
20807 	 * checking for ipoptlen is enough to decide if ip options are present.
20808 	 */
20809 	ipoptlen = 0;
20810 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
20811 	/*
20812 	 * Pre-calculate here as we save another lookup into the darknesses
20813 	 * of IPsec that way and can actually decide if TSO is ok.
20814 	 */
20815 #ifdef INET6
20816 	if (isipv6 && IPSEC_ENABLED(ipv6))
20817 		ipsec_optlen = IPSEC_HDRSIZE(ipv6, inp);
20818 #ifdef INET
20819 	else
20820 #endif
20821 #endif				/* INET6 */
20822 #ifdef INET
20823 		if (IPSEC_ENABLED(ipv4))
20824 			ipsec_optlen = IPSEC_HDRSIZE(ipv4, inp);
20825 #endif				/* INET */
20826 #endif
20827 
20828 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
20829 	ipoptlen += ipsec_optlen;
20830 #endif
20831 	if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > segsiz &&
20832 	    (tp->t_port == 0) &&
20833 	    ((tp->t_flags & TF_SIGNATURE) == 0) &&
20834 	    tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
20835 	    ipoptlen == 0)
20836 		tso = 1;
20837 	{
20838 		uint32_t outstanding __unused;
20839 
20840 		outstanding = tp->snd_max - tp->snd_una;
20841 		if (tp->t_flags & TF_SENTFIN) {
20842 			/*
20843 			 * If we sent a fin, snd_max is 1 higher than
20844 			 * snd_una
20845 			 */
20846 			outstanding--;
20847 		}
20848 		if (sack_rxmit) {
20849 			if ((rsm->r_flags & RACK_HAS_FIN) == 0)
20850 				flags &= ~TH_FIN;
20851 		} else {
20852 			if (SEQ_LT(tp->snd_nxt + len, tp->snd_una +
20853 				   sbused(sb)))
20854 				flags &= ~TH_FIN;
20855 		}
20856 	}
20857 	recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
20858 		      (long)TCP_MAXWIN << tp->rcv_scale);
20859 
20860 	/*
20861 	 * Sender silly window avoidance.   We transmit under the following
20862 	 * conditions when len is non-zero:
20863 	 *
20864 	 * - We have a full segment (or more with TSO) - This is the last
20865 	 * buffer in a write()/send() and we are either idle or running
20866 	 * NODELAY - we've timed out (e.g. persist timer) - we have more
20867 	 * then 1/2 the maximum send window's worth of data (receiver may be
20868 	 * limited the window size) - we need to retransmit
20869 	 */
20870 	if (len) {
20871 		if (len >= segsiz) {
20872 			goto send;
20873 		}
20874 		/*
20875 		 * NOTE! on localhost connections an 'ack' from the remote
20876 		 * end may occur synchronously with the output and cause us
20877 		 * to flush a buffer queued with moretocome.  XXX
20878 		 *
20879 		 */
20880 		if (!(tp->t_flags & TF_MORETOCOME) &&	/* normal case */
20881 		    (idle || (tp->t_flags & TF_NODELAY)) &&
20882 		    ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) &&
20883 		    (tp->t_flags & TF_NOPUSH) == 0) {
20884 			pass = 2;
20885 			goto send;
20886 		}
20887 		if ((tp->snd_una == tp->snd_max) && len) {	/* Nothing outstanding */
20888 			pass = 22;
20889 			goto send;
20890 		}
20891 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) {
20892 			pass = 4;
20893 			goto send;
20894 		}
20895 		if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {	/* retransmit case */
20896 			pass = 5;
20897 			goto send;
20898 		}
20899 		if (sack_rxmit) {
20900 			pass = 6;
20901 			goto send;
20902 		}
20903 		if (((tp->snd_wnd - ctf_outstanding(tp)) < segsiz) &&
20904 		    (ctf_outstanding(tp) < (segsiz * 2))) {
20905 			/*
20906 			 * We have less than two MSS outstanding (delayed ack)
20907 			 * and our rwnd will not let us send a full sized
20908 			 * MSS. Lets go ahead and let this small segment
20909 			 * out because we want to try to have at least two
20910 			 * packets inflight to not be caught by delayed ack.
20911 			 */
20912 			pass = 12;
20913 			goto send;
20914 		}
20915 	}
20916 	/*
20917 	 * Sending of standalone window updates.
20918 	 *
20919 	 * Window updates are important when we close our window due to a
20920 	 * full socket buffer and are opening it again after the application
20921 	 * reads data from it.  Once the window has opened again and the
20922 	 * remote end starts to send again the ACK clock takes over and
20923 	 * provides the most current window information.
20924 	 *
20925 	 * We must avoid the silly window syndrome whereas every read from
20926 	 * the receive buffer, no matter how small, causes a window update
20927 	 * to be sent.  We also should avoid sending a flurry of window
20928 	 * updates when the socket buffer had queued a lot of data and the
20929 	 * application is doing small reads.
20930 	 *
20931 	 * Prevent a flurry of pointless window updates by only sending an
20932 	 * update when we can increase the advertized window by more than
20933 	 * 1/4th of the socket buffer capacity.  When the buffer is getting
20934 	 * full or is very small be more aggressive and send an update
20935 	 * whenever we can increase by two mss sized segments. In all other
20936 	 * situations the ACK's to new incoming data will carry further
20937 	 * window increases.
20938 	 *
20939 	 * Don't send an independent window update if a delayed ACK is
20940 	 * pending (it will get piggy-backed on it) or the remote side
20941 	 * already has done a half-close and won't send more data.  Skip
20942 	 * this if the connection is in T/TCP half-open state.
20943 	 */
20944 	if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
20945 	    !(tp->t_flags & TF_DELACK) &&
20946 	    !TCPS_HAVERCVDFIN(tp->t_state)) {
20947 		/*
20948 		 * "adv" is the amount we could increase the window, taking
20949 		 * into account that we are limited by TCP_MAXWIN <<
20950 		 * tp->rcv_scale.
20951 		 */
20952 		int32_t adv;
20953 		int oldwin;
20954 
20955 		adv = recwin;
20956 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
20957 			oldwin = (tp->rcv_adv - tp->rcv_nxt);
20958 			if (adv > oldwin)
20959 				adv -= oldwin;
20960 			else {
20961 				/* We can't increase the window */
20962 				adv = 0;
20963 			}
20964 		} else
20965 			oldwin = 0;
20966 
20967 		/*
20968 		 * If the new window size ends up being the same as or less
20969 		 * than the old size when it is scaled, then don't force
20970 		 * a window update.
20971 		 */
20972 		if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale)
20973 			goto dontupdate;
20974 
20975 		if (adv >= (int32_t)(2 * segsiz) &&
20976 		    (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) ||
20977 		     recwin <= (int32_t)(so->so_rcv.sb_hiwat / 8) ||
20978 		     so->so_rcv.sb_hiwat <= 8 * segsiz)) {
20979 			pass = 7;
20980 			goto send;
20981 		}
20982 		if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat) {
20983 			pass = 23;
20984 			goto send;
20985 		}
20986 	}
20987 dontupdate:
20988 
20989 	/*
20990 	 * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
20991 	 * is also a catch-all for the retransmit timer timeout case.
20992 	 */
20993 	if (tp->t_flags & TF_ACKNOW) {
20994 		pass = 8;
20995 		goto send;
20996 	}
20997 	if (((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) {
20998 		pass = 9;
20999 		goto send;
21000 	}
21001 	/*
21002 	 * If our state indicates that FIN should be sent and we have not
21003 	 * yet done so, then we need to send.
21004 	 */
21005 	if ((flags & TH_FIN) &&
21006 	    (tp->snd_nxt == tp->snd_una)) {
21007 		pass = 11;
21008 		goto send;
21009 	}
21010 	/*
21011 	 * No reason to send a segment, just return.
21012 	 */
21013 just_return:
21014 	SOCKBUF_UNLOCK(sb);
21015 just_return_nolock:
21016 	{
21017 		int app_limited = CTF_JR_SENT_DATA;
21018 
21019 		if (tot_len_this_send > 0) {
21020 			/* Make sure snd_nxt is up to max */
21021 			rack->r_ctl.fsb.recwin = recwin;
21022 			slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, NULL, segsiz);
21023 			if ((error == 0) &&
21024 			    rack_use_rfo &&
21025 			    ((flags & (TH_SYN|TH_FIN)) == 0) &&
21026 			    (ipoptlen == 0) &&
21027 			    (tp->snd_nxt == tp->snd_max) &&
21028 			    (tp->rcv_numsacks == 0) &&
21029 			    rack->r_fsb_inited &&
21030 			    TCPS_HAVEESTABLISHED(tp->t_state) &&
21031 			    ((IN_RECOVERY(tp->t_flags)) == 0) &&
21032 			    (rack->r_must_retran == 0) &&
21033 			    ((tp->t_flags & TF_NEEDFIN) == 0) &&
21034 			    (len > 0) && (orig_len > 0) &&
21035 			    (orig_len > len) &&
21036 			    ((orig_len - len) >= segsiz) &&
21037 			    ((optlen == 0) ||
21038 			     ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
21039 				/* We can send at least one more MSS using our fsb */
21040 				rack_setup_fast_output(tp, rack, sb, len, orig_len,
21041 						       segsiz, pace_max_seg, hw_tls, flags);
21042 			} else
21043 				rack->r_fast_output = 0;
21044 
21045 
21046 			rack_log_fsb(rack, tp, so, flags,
21047 				     ipoptlen, orig_len, len, 0,
21048 				     1, optlen, __LINE__, 1);
21049 			if (SEQ_GT(tp->snd_max, tp->snd_nxt))
21050 				tp->snd_nxt = tp->snd_max;
21051 		} else {
21052 			int end_window = 0;
21053 			uint32_t seq = tp->gput_ack;
21054 
21055 			rsm = tqhash_max(rack->r_ctl.tqh);
21056 			if (rsm) {
21057 				/*
21058 				 * Mark the last sent that we just-returned (hinting
21059 				 * that delayed ack may play a role in any rtt measurement).
21060 				 */
21061 				rsm->r_just_ret = 1;
21062 			}
21063 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_JUSTRET], 1);
21064 			rack->r_ctl.rc_agg_delayed = 0;
21065 			rack->r_early = 0;
21066 			rack->r_late = 0;
21067 			rack->r_ctl.rc_agg_early = 0;
21068 			if ((ctf_outstanding(tp) +
21069 			     min(max(segsiz, (rack->r_ctl.rc_high_rwnd/2)),
21070 				 minseg)) >= tp->snd_wnd) {
21071 				/* We are limited by the rwnd */
21072 				app_limited = CTF_JR_RWND_LIMITED;
21073 				if (IN_FASTRECOVERY(tp->t_flags))
21074 					rack->r_ctl.rc_prr_sndcnt = 0;
21075 			} else if (ctf_outstanding(tp) >= sbavail(sb)) {
21076 				/* We are limited by whats available -- app limited */
21077 				app_limited = CTF_JR_APP_LIMITED;
21078 				if (IN_FASTRECOVERY(tp->t_flags))
21079 					rack->r_ctl.rc_prr_sndcnt = 0;
21080 			} else if ((idle == 0) &&
21081 				   ((tp->t_flags & TF_NODELAY) == 0) &&
21082 				   ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) &&
21083 				   (len < segsiz)) {
21084 				/*
21085 				 * No delay is not on and the
21086 				 * user is sending less than 1MSS. This
21087 				 * brings out SWS avoidance so we
21088 				 * don't send. Another app-limited case.
21089 				 */
21090 				app_limited = CTF_JR_APP_LIMITED;
21091 			} else if (tp->t_flags & TF_NOPUSH) {
21092 				/*
21093 				 * The user has requested no push of
21094 				 * the last segment and we are
21095 				 * at the last segment. Another app
21096 				 * limited case.
21097 				 */
21098 				app_limited = CTF_JR_APP_LIMITED;
21099 			} else if ((ctf_outstanding(tp) + minseg) > cwnd_to_use) {
21100 				/* Its the cwnd */
21101 				app_limited = CTF_JR_CWND_LIMITED;
21102 			} else if (IN_FASTRECOVERY(tp->t_flags) &&
21103 				   (rack->rack_no_prr == 0) &&
21104 				   (rack->r_ctl.rc_prr_sndcnt < segsiz)) {
21105 				app_limited = CTF_JR_PRR;
21106 			} else {
21107 				/* Now why here are we not sending? */
21108 #ifdef NOW
21109 #ifdef INVARIANTS
21110 				panic("rack:%p hit JR_ASSESSING case cwnd_to_use:%u?", rack, cwnd_to_use);
21111 #endif
21112 #endif
21113 				app_limited = CTF_JR_ASSESSING;
21114 			}
21115 			/*
21116 			 * App limited in some fashion, for our pacing GP
21117 			 * measurements we don't want any gap (even cwnd).
21118 			 * Close  down the measurement window.
21119 			 */
21120 			if (rack_cwnd_block_ends_measure &&
21121 			    ((app_limited == CTF_JR_CWND_LIMITED) ||
21122 			     (app_limited == CTF_JR_PRR))) {
21123 				/*
21124 				 * The reason we are not sending is
21125 				 * the cwnd (or prr). We have been configured
21126 				 * to end the measurement window in
21127 				 * this case.
21128 				 */
21129 				end_window = 1;
21130 			} else if (rack_rwnd_block_ends_measure &&
21131 				   (app_limited == CTF_JR_RWND_LIMITED)) {
21132 				/*
21133 				 * We are rwnd limited and have been
21134 				 * configured to end the measurement
21135 				 * window in this case.
21136 				 */
21137 				end_window = 1;
21138 			} else if (app_limited == CTF_JR_APP_LIMITED) {
21139 				/*
21140 				 * A true application limited period, we have
21141 				 * ran out of data.
21142 				 */
21143 				end_window = 1;
21144 			} else if (app_limited == CTF_JR_ASSESSING) {
21145 				/*
21146 				 * In the assessing case we hit the end of
21147 				 * the if/else and had no known reason
21148 				 * This will panic us under invariants..
21149 				 *
21150 				 * If we get this out in logs we need to
21151 				 * investagate which reason we missed.
21152 				 */
21153 				end_window = 1;
21154 			}
21155 			if (end_window) {
21156 				uint8_t log = 0;
21157 
21158 				/* Adjust the Gput measurement */
21159 				if ((tp->t_flags & TF_GPUTINPROG) &&
21160 				    SEQ_GT(tp->gput_ack, tp->snd_max)) {
21161 					tp->gput_ack = tp->snd_max;
21162 					if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) {
21163 						/*
21164 						 * There is not enough to measure.
21165 						 */
21166 						tp->t_flags &= ~TF_GPUTINPROG;
21167 						rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
21168 									   rack->r_ctl.rc_gp_srtt /*flex1*/,
21169 									   tp->gput_seq,
21170 									   0, 0, 18, __LINE__, NULL, 0);
21171 					} else
21172 						log = 1;
21173 				}
21174 				/* Mark the last packet has app limited */
21175 				rsm = tqhash_max(rack->r_ctl.tqh);
21176 				if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) {
21177 					if (rack->r_ctl.rc_app_limited_cnt == 0)
21178 						rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm;
21179 					else {
21180 						/*
21181 						 * Go out to the end app limited and mark
21182 						 * this new one as next and move the end_appl up
21183 						 * to this guy.
21184 						 */
21185 						if (rack->r_ctl.rc_end_appl)
21186 							rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start;
21187 						rack->r_ctl.rc_end_appl = rsm;
21188 					}
21189 					rsm->r_flags |= RACK_APP_LIMITED;
21190 					rack->r_ctl.rc_app_limited_cnt++;
21191 				}
21192 				if (log)
21193 					rack_log_pacing_delay_calc(rack,
21194 								   rack->r_ctl.rc_app_limited_cnt, seq,
21195 								   tp->gput_ack, 0, 0, 4, __LINE__, NULL, 0);
21196 			}
21197 		}
21198 		/* Check if we need to go into persists or not */
21199 		if ((tp->snd_max == tp->snd_una) &&
21200 		    TCPS_HAVEESTABLISHED(tp->t_state) &&
21201 		    sbavail(sb) &&
21202 		    (sbavail(sb) > tp->snd_wnd) &&
21203 		    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg))) {
21204 			/* Yes lets make sure to move to persist before timer-start */
21205 			rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, tp->snd_una);
21206 		}
21207 		rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, sup_rack);
21208 		rack_log_type_just_return(rack, cts, tot_len_this_send, slot, hpts_calling, app_limited, cwnd_to_use);
21209 	}
21210 #ifdef NETFLIX_SHARED_CWND
21211 	if ((sbavail(sb) == 0) &&
21212 	    rack->r_ctl.rc_scw) {
21213 		tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
21214 		rack->rack_scwnd_is_idle = 1;
21215 	}
21216 #endif
21217 #ifdef TCP_ACCOUNTING
21218 	if (tot_len_this_send > 0) {
21219 		crtsc = get_cyclecount();
21220 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21221 			tp->tcp_cnt_counters[SND_OUT_DATA]++;
21222 		}
21223 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21224 			tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
21225 		}
21226 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21227 			tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) / segsiz);
21228 		}
21229 	} else {
21230 		crtsc = get_cyclecount();
21231 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21232 			tp->tcp_cnt_counters[SND_LIMITED]++;
21233 		}
21234 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21235 			tp->tcp_proc_time[SND_LIMITED] += (crtsc - ts_val);
21236 		}
21237 	}
21238 	sched_unpin();
21239 #endif
21240 	return (0);
21241 
21242 send:
21243 	if ((rack->r_ctl.crte != NULL) &&
21244 	    (rsm == NULL) &&
21245 	    ((rack->rc_hw_nobuf == 1) ||
21246 	     (rack_hw_check_queue && (check_done == 0)))) {
21247 		/*
21248 		 * We only want to do this once with the hw_check_queue,
21249 		 * for the enobuf case we would only do it once if
21250 		 * we come around to again, the flag will be clear.
21251 		 */
21252 		check_done = 1;
21253 		slot = rack_check_queue_level(rack, tp, &tv, cts, len, segsiz);
21254 		if (slot) {
21255 			rack->r_ctl.rc_agg_delayed = 0;
21256 			rack->r_ctl.rc_agg_early = 0;
21257 			rack->r_early = 0;
21258 			rack->r_late = 0;
21259 			SOCKBUF_UNLOCK(&so->so_snd);
21260 			goto skip_all_send;
21261 		}
21262 	}
21263 	if (rsm || sack_rxmit)
21264 		counter_u64_add(rack_nfto_resend, 1);
21265 	else
21266 		counter_u64_add(rack_non_fto_send, 1);
21267 	if ((flags & TH_FIN) &&
21268 	    sbavail(sb)) {
21269 		/*
21270 		 * We do not transmit a FIN
21271 		 * with data outstanding. We
21272 		 * need to make it so all data
21273 		 * is acked first.
21274 		 */
21275 		flags &= ~TH_FIN;
21276 	}
21277 	/* Enforce stack imposed max seg size if we have one */
21278 	if (rack->r_ctl.rc_pace_max_segs &&
21279 	    (len > rack->r_ctl.rc_pace_max_segs)) {
21280 		mark = 1;
21281 		len = rack->r_ctl.rc_pace_max_segs;
21282 	}
21283 	SOCKBUF_LOCK_ASSERT(sb);
21284 	if (len > 0) {
21285 		if (len >= segsiz)
21286 			tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT;
21287 		else
21288 			tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT;
21289 	}
21290 	/*
21291 	 * Before ESTABLISHED, force sending of initial options unless TCP
21292 	 * set not to do any options. NOTE: we assume that the IP/TCP header
21293 	 * plus TCP options always fit in a single mbuf, leaving room for a
21294 	 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr)
21295 	 * + optlen <= MCLBYTES
21296 	 */
21297 	optlen = 0;
21298 #ifdef INET6
21299 	if (isipv6)
21300 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
21301 	else
21302 #endif
21303 		hdrlen = sizeof(struct tcpiphdr);
21304 
21305 	/*
21306 	 * Compute options for segment. We only have to care about SYN and
21307 	 * established connection segments.  Options for SYN-ACK segments
21308 	 * are handled in TCP syncache.
21309 	 */
21310 	to.to_flags = 0;
21311 	if ((tp->t_flags & TF_NOOPT) == 0) {
21312 		/* Maximum segment size. */
21313 		if (flags & TH_SYN) {
21314 			tp->snd_nxt = tp->iss;
21315 			to.to_mss = tcp_mssopt(&inp->inp_inc);
21316 			if (tp->t_port)
21317 				to.to_mss -= V_tcp_udp_tunneling_overhead;
21318 			to.to_flags |= TOF_MSS;
21319 
21320 			/*
21321 			 * On SYN or SYN|ACK transmits on TFO connections,
21322 			 * only include the TFO option if it is not a
21323 			 * retransmit, as the presence of the TFO option may
21324 			 * have caused the original SYN or SYN|ACK to have
21325 			 * been dropped by a middlebox.
21326 			 */
21327 			if (IS_FASTOPEN(tp->t_flags) &&
21328 			    (tp->t_rxtshift == 0)) {
21329 				if (tp->t_state == TCPS_SYN_RECEIVED) {
21330 					to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN;
21331 					to.to_tfo_cookie =
21332 						(u_int8_t *)&tp->t_tfo_cookie.server;
21333 					to.to_flags |= TOF_FASTOPEN;
21334 					wanted_cookie = 1;
21335 				} else if (tp->t_state == TCPS_SYN_SENT) {
21336 					to.to_tfo_len =
21337 						tp->t_tfo_client_cookie_len;
21338 					to.to_tfo_cookie =
21339 						tp->t_tfo_cookie.client;
21340 					to.to_flags |= TOF_FASTOPEN;
21341 					wanted_cookie = 1;
21342 					/*
21343 					 * If we wind up having more data to
21344 					 * send with the SYN than can fit in
21345 					 * one segment, don't send any more
21346 					 * until the SYN|ACK comes back from
21347 					 * the other end.
21348 					 */
21349 					sendalot = 0;
21350 				}
21351 			}
21352 		}
21353 		/* Window scaling. */
21354 		if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
21355 			to.to_wscale = tp->request_r_scale;
21356 			to.to_flags |= TOF_SCALE;
21357 		}
21358 		/* Timestamps. */
21359 		if ((tp->t_flags & TF_RCVD_TSTMP) ||
21360 		    ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
21361 			to.to_tsval = ms_cts + tp->ts_offset;
21362 			to.to_tsecr = tp->ts_recent;
21363 			to.to_flags |= TOF_TS;
21364 		}
21365 		/* Set receive buffer autosizing timestamp. */
21366 		if (tp->rfbuf_ts == 0 &&
21367 		    (so->so_rcv.sb_flags & SB_AUTOSIZE))
21368 			tp->rfbuf_ts = tcp_ts_getticks();
21369 		/* Selective ACK's. */
21370 		if (tp->t_flags & TF_SACK_PERMIT) {
21371 			if (flags & TH_SYN)
21372 				to.to_flags |= TOF_SACKPERM;
21373 			else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
21374 				 tp->rcv_numsacks > 0) {
21375 				to.to_flags |= TOF_SACK;
21376 				to.to_nsacks = tp->rcv_numsacks;
21377 				to.to_sacks = (u_char *)tp->sackblks;
21378 			}
21379 		}
21380 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
21381 		/* TCP-MD5 (RFC2385). */
21382 		if (tp->t_flags & TF_SIGNATURE)
21383 			to.to_flags |= TOF_SIGNATURE;
21384 #endif
21385 
21386 		/* Processing the options. */
21387 		hdrlen += optlen = tcp_addoptions(&to, opt);
21388 		/*
21389 		 * If we wanted a TFO option to be added, but it was unable
21390 		 * to fit, ensure no data is sent.
21391 		 */
21392 		if (IS_FASTOPEN(tp->t_flags) && wanted_cookie &&
21393 		    !(to.to_flags & TOF_FASTOPEN))
21394 			len = 0;
21395 	}
21396 	if (tp->t_port) {
21397 		if (V_tcp_udp_tunneling_port == 0) {
21398 			/* The port was removed?? */
21399 			SOCKBUF_UNLOCK(&so->so_snd);
21400 #ifdef TCP_ACCOUNTING
21401 			crtsc = get_cyclecount();
21402 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21403 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
21404 			}
21405 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21406 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
21407 			}
21408 			sched_unpin();
21409 #endif
21410 			return (EHOSTUNREACH);
21411 		}
21412 		hdrlen += sizeof(struct udphdr);
21413 	}
21414 #ifdef INET6
21415 	if (isipv6)
21416 		ipoptlen = ip6_optlen(inp);
21417 	else
21418 #endif
21419 		if (inp->inp_options)
21420 			ipoptlen = inp->inp_options->m_len -
21421 				offsetof(struct ipoption, ipopt_list);
21422 		else
21423 			ipoptlen = 0;
21424 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
21425 	ipoptlen += ipsec_optlen;
21426 #endif
21427 
21428 	/*
21429 	 * Adjust data length if insertion of options will bump the packet
21430 	 * length beyond the t_maxseg length. Clear the FIN bit because we
21431 	 * cut off the tail of the segment.
21432 	 */
21433 	if (len + optlen + ipoptlen > tp->t_maxseg) {
21434 		if (tso) {
21435 			uint32_t if_hw_tsomax;
21436 			uint32_t moff;
21437 			int32_t max_len;
21438 
21439 			/* extract TSO information */
21440 			if_hw_tsomax = tp->t_tsomax;
21441 			if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
21442 			if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
21443 			KASSERT(ipoptlen == 0,
21444 				("%s: TSO can't do IP options", __func__));
21445 
21446 			/*
21447 			 * Check if we should limit by maximum payload
21448 			 * length:
21449 			 */
21450 			if (if_hw_tsomax != 0) {
21451 				/* compute maximum TSO length */
21452 				max_len = (if_hw_tsomax - hdrlen -
21453 					   max_linkhdr);
21454 				if (max_len <= 0) {
21455 					len = 0;
21456 				} else if (len > max_len) {
21457 					sendalot = 1;
21458 					len = max_len;
21459 					mark = 2;
21460 				}
21461 			}
21462 			/*
21463 			 * Prevent the last segment from being fractional
21464 			 * unless the send sockbuf can be emptied:
21465 			 */
21466 			max_len = (tp->t_maxseg - optlen);
21467 			if ((sb_offset + len) < sbavail(sb)) {
21468 				moff = len % (u_int)max_len;
21469 				if (moff != 0) {
21470 					mark = 3;
21471 					len -= moff;
21472 				}
21473 			}
21474 			/*
21475 			 * In case there are too many small fragments don't
21476 			 * use TSO:
21477 			 */
21478 			if (len <= max_len) {
21479 				mark = 4;
21480 				tso = 0;
21481 			}
21482 			/*
21483 			 * Send the FIN in a separate segment after the bulk
21484 			 * sending is done. We don't trust the TSO
21485 			 * implementations to clear the FIN flag on all but
21486 			 * the last segment.
21487 			 */
21488 			if (tp->t_flags & TF_NEEDFIN) {
21489 				sendalot = 4;
21490 			}
21491 		} else {
21492 			mark = 5;
21493 			if (optlen + ipoptlen >= tp->t_maxseg) {
21494 				/*
21495 				 * Since we don't have enough space to put
21496 				 * the IP header chain and the TCP header in
21497 				 * one packet as required by RFC 7112, don't
21498 				 * send it. Also ensure that at least one
21499 				 * byte of the payload can be put into the
21500 				 * TCP segment.
21501 				 */
21502 				SOCKBUF_UNLOCK(&so->so_snd);
21503 				error = EMSGSIZE;
21504 				sack_rxmit = 0;
21505 				goto out;
21506 			}
21507 			len = tp->t_maxseg - optlen - ipoptlen;
21508 			sendalot = 5;
21509 		}
21510 	} else {
21511 		tso = 0;
21512 		mark = 6;
21513 	}
21514 	KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET,
21515 		("%s: len > IP_MAXPACKET", __func__));
21516 #ifdef DIAGNOSTIC
21517 #ifdef INET6
21518 	if (max_linkhdr + hdrlen > MCLBYTES)
21519 #else
21520 		if (max_linkhdr + hdrlen > MHLEN)
21521 #endif
21522 			panic("tcphdr too big");
21523 #endif
21524 
21525 	/*
21526 	 * This KASSERT is here to catch edge cases at a well defined place.
21527 	 * Before, those had triggered (random) panic conditions further
21528 	 * down.
21529 	 */
21530 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
21531 	if ((len == 0) &&
21532 	    (flags & TH_FIN) &&
21533 	    (sbused(sb))) {
21534 		/*
21535 		 * We have outstanding data, don't send a fin by itself!.
21536 		 */
21537 		goto just_return;
21538 	}
21539 	/*
21540 	 * Grab a header mbuf, attaching a copy of data to be transmitted,
21541 	 * and initialize the header from the template for sends on this
21542 	 * connection.
21543 	 */
21544 	hw_tls = tp->t_nic_ktls_xmit != 0;
21545 	if (len) {
21546 		uint32_t max_val;
21547 		uint32_t moff;
21548 
21549 		if (rack->r_ctl.rc_pace_max_segs)
21550 			max_val = rack->r_ctl.rc_pace_max_segs;
21551 		else if (rack->rc_user_set_max_segs)
21552 			max_val = rack->rc_user_set_max_segs * segsiz;
21553 		else
21554 			max_val = len;
21555 		/*
21556 		 * We allow a limit on sending with hptsi.
21557 		 */
21558 		if (len > max_val) {
21559 			mark = 7;
21560 			len = max_val;
21561 		}
21562 #ifdef INET6
21563 		if (MHLEN < hdrlen + max_linkhdr)
21564 			m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
21565 		else
21566 #endif
21567 			m = m_gethdr(M_NOWAIT, MT_DATA);
21568 
21569 		if (m == NULL) {
21570 			SOCKBUF_UNLOCK(sb);
21571 			error = ENOBUFS;
21572 			sack_rxmit = 0;
21573 			goto out;
21574 		}
21575 		m->m_data += max_linkhdr;
21576 		m->m_len = hdrlen;
21577 
21578 		/*
21579 		 * Start the m_copy functions from the closest mbuf to the
21580 		 * sb_offset in the socket buffer chain.
21581 		 */
21582 		mb = sbsndptr_noadv(sb, sb_offset, &moff);
21583 		s_mb = mb;
21584 		s_moff = moff;
21585 		if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) {
21586 			m_copydata(mb, moff, (int)len,
21587 				   mtod(m, caddr_t)+hdrlen);
21588 			if (SEQ_LT(tp->snd_nxt, tp->snd_max))
21589 				sbsndptr_adv(sb, mb, len);
21590 			m->m_len += len;
21591 		} else {
21592 			struct sockbuf *msb;
21593 
21594 			if (SEQ_LT(tp->snd_nxt, tp->snd_max))
21595 				msb = NULL;
21596 			else
21597 				msb = sb;
21598 			m->m_next = tcp_m_copym(
21599 				mb, moff, &len,
21600 				if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, msb,
21601 				((rsm == NULL) ? hw_tls : 0)
21602 #ifdef NETFLIX_COPY_ARGS
21603 				, &s_mb, &s_moff
21604 #endif
21605 				);
21606 			if (len <= (tp->t_maxseg - optlen)) {
21607 				/*
21608 				 * Must have ran out of mbufs for the copy
21609 				 * shorten it to no longer need tso. Lets
21610 				 * not put on sendalot since we are low on
21611 				 * mbufs.
21612 				 */
21613 				tso = 0;
21614 			}
21615 			if (m->m_next == NULL) {
21616 				SOCKBUF_UNLOCK(sb);
21617 				(void)m_free(m);
21618 				error = ENOBUFS;
21619 				sack_rxmit = 0;
21620 				goto out;
21621 			}
21622 		}
21623 		if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) {
21624 			if (rsm && (rsm->r_flags & RACK_TLP)) {
21625 				/*
21626 				 * TLP should not count in retran count, but
21627 				 * in its own bin
21628 				 */
21629 				counter_u64_add(rack_tlp_retran, 1);
21630 				counter_u64_add(rack_tlp_retran_bytes, len);
21631 			} else {
21632 				tp->t_sndrexmitpack++;
21633 				KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
21634 				KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
21635 			}
21636 #ifdef STATS
21637 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
21638 						 len);
21639 #endif
21640 		} else {
21641 			KMOD_TCPSTAT_INC(tcps_sndpack);
21642 			KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
21643 #ifdef STATS
21644 			stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
21645 						 len);
21646 #endif
21647 		}
21648 		/*
21649 		 * If we're sending everything we've got, set PUSH. (This
21650 		 * will keep happy those implementations which only give
21651 		 * data to the user when a buffer fills or a PUSH comes in.)
21652 		 */
21653 		if (sb_offset + len == sbused(sb) &&
21654 		    sbused(sb) &&
21655 		    !(flags & TH_SYN)) {
21656 			flags |= TH_PUSH;
21657 			add_flag |= RACK_HAD_PUSH;
21658 		}
21659 
21660 		SOCKBUF_UNLOCK(sb);
21661 	} else {
21662 		SOCKBUF_UNLOCK(sb);
21663 		if (tp->t_flags & TF_ACKNOW)
21664 			KMOD_TCPSTAT_INC(tcps_sndacks);
21665 		else if (flags & (TH_SYN | TH_FIN | TH_RST))
21666 			KMOD_TCPSTAT_INC(tcps_sndctrl);
21667 		else
21668 			KMOD_TCPSTAT_INC(tcps_sndwinup);
21669 
21670 		m = m_gethdr(M_NOWAIT, MT_DATA);
21671 		if (m == NULL) {
21672 			error = ENOBUFS;
21673 			sack_rxmit = 0;
21674 			goto out;
21675 		}
21676 #ifdef INET6
21677 		if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
21678 		    MHLEN >= hdrlen) {
21679 			M_ALIGN(m, hdrlen);
21680 		} else
21681 #endif
21682 			m->m_data += max_linkhdr;
21683 		m->m_len = hdrlen;
21684 	}
21685 	SOCKBUF_UNLOCK_ASSERT(sb);
21686 	m->m_pkthdr.rcvif = (struct ifnet *)0;
21687 #ifdef MAC
21688 	mac_inpcb_create_mbuf(inp, m);
21689 #endif
21690 	if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) &&  rack->r_fsb_inited) {
21691 #ifdef INET6
21692 		if (isipv6)
21693 			ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
21694 		else
21695 #endif				/* INET6 */
21696 #ifdef INET
21697 			ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
21698 #endif
21699 		th = rack->r_ctl.fsb.th;
21700 		udp = rack->r_ctl.fsb.udp;
21701 		if (udp) {
21702 #ifdef INET6
21703 			if (isipv6)
21704 				ulen = hdrlen + len - sizeof(struct ip6_hdr);
21705 			else
21706 #endif				/* INET6 */
21707 				ulen = hdrlen + len - sizeof(struct ip);
21708 			udp->uh_ulen = htons(ulen);
21709 		}
21710 	} else {
21711 #ifdef INET6
21712 		if (isipv6) {
21713 			ip6 = mtod(m, struct ip6_hdr *);
21714 			if (tp->t_port) {
21715 				udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
21716 				udp->uh_sport = htons(V_tcp_udp_tunneling_port);
21717 				udp->uh_dport = tp->t_port;
21718 				ulen = hdrlen + len - sizeof(struct ip6_hdr);
21719 				udp->uh_ulen = htons(ulen);
21720 				th = (struct tcphdr *)(udp + 1);
21721 			} else
21722 				th = (struct tcphdr *)(ip6 + 1);
21723 			tcpip_fillheaders(inp, tp->t_port, ip6, th);
21724 		} else
21725 #endif				/* INET6 */
21726 		{
21727 #ifdef INET
21728 			ip = mtod(m, struct ip *);
21729 			if (tp->t_port) {
21730 				udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
21731 				udp->uh_sport = htons(V_tcp_udp_tunneling_port);
21732 				udp->uh_dport = tp->t_port;
21733 				ulen = hdrlen + len - sizeof(struct ip);
21734 				udp->uh_ulen = htons(ulen);
21735 				th = (struct tcphdr *)(udp + 1);
21736 			} else
21737 				th = (struct tcphdr *)(ip + 1);
21738 			tcpip_fillheaders(inp, tp->t_port, ip, th);
21739 #endif
21740 		}
21741 	}
21742 	/*
21743 	 * Fill in fields, remembering maximum advertised window for use in
21744 	 * delaying messages about window sizes. If resending a FIN, be sure
21745 	 * not to use a new sequence number.
21746 	 */
21747 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
21748 	    tp->snd_nxt == tp->snd_max)
21749 		tp->snd_nxt--;
21750 	/*
21751 	 * If we are starting a connection, send ECN setup SYN packet. If we
21752 	 * are on a retransmit, we may resend those bits a number of times
21753 	 * as per RFC 3168.
21754 	 */
21755 	if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn) {
21756 		flags |= tcp_ecn_output_syn_sent(tp);
21757 	}
21758 	/* Also handle parallel SYN for ECN */
21759 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
21760 	    (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) {
21761 		int ect = tcp_ecn_output_established(tp, &flags, len, sack_rxmit);
21762 		if ((tp->t_state == TCPS_SYN_RECEIVED) &&
21763 		    (tp->t_flags2 & TF2_ECN_SND_ECE))
21764 			tp->t_flags2 &= ~TF2_ECN_SND_ECE;
21765 #ifdef INET6
21766 		if (isipv6) {
21767 			ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
21768 			ip6->ip6_flow |= htonl(ect << 20);
21769 		}
21770 		else
21771 #endif
21772 		{
21773 #ifdef INET
21774 			ip->ip_tos &= ~IPTOS_ECN_MASK;
21775 			ip->ip_tos |= ect;
21776 #endif
21777 		}
21778 	}
21779 	/*
21780 	 * If we are doing retransmissions, then snd_nxt will not reflect
21781 	 * the first unsent octet.  For ACK only packets, we do not want the
21782 	 * sequence number of the retransmitted packet, we want the sequence
21783 	 * number of the next unsent octet.  So, if there is no data (and no
21784 	 * SYN or FIN), use snd_max instead of snd_nxt when filling in
21785 	 * ti_seq.  But if we are in persist state, snd_max might reflect
21786 	 * one byte beyond the right edge of the window, so use snd_nxt in
21787 	 * that case, since we know we aren't doing a retransmission.
21788 	 * (retransmit and persist are mutually exclusive...)
21789 	 */
21790 	if (sack_rxmit == 0) {
21791 		if (len || (flags & (TH_SYN | TH_FIN))) {
21792 			th->th_seq = htonl(tp->snd_nxt);
21793 			rack_seq = tp->snd_nxt;
21794 		} else {
21795 			th->th_seq = htonl(tp->snd_max);
21796 			rack_seq = tp->snd_max;
21797 		}
21798 	} else {
21799 		th->th_seq = htonl(rsm->r_start);
21800 		rack_seq = rsm->r_start;
21801 	}
21802 	th->th_ack = htonl(tp->rcv_nxt);
21803 	tcp_set_flags(th, flags);
21804 	/*
21805 	 * Calculate receive window.  Don't shrink window, but avoid silly
21806 	 * window syndrome.
21807 	 * If a RST segment is sent, advertise a window of zero.
21808 	 */
21809 	if (flags & TH_RST) {
21810 		recwin = 0;
21811 	} else {
21812 		if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
21813 		    recwin < (long)segsiz) {
21814 			recwin = 0;
21815 		}
21816 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) &&
21817 		    recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
21818 			recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
21819 	}
21820 
21821 	/*
21822 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or
21823 	 * <SYN,ACK>) segment itself is never scaled.  The <SYN,ACK> case is
21824 	 * handled in syncache.
21825 	 */
21826 	if (flags & TH_SYN)
21827 		th->th_win = htons((u_short)
21828 				   (min(sbspace(&so->so_rcv), TCP_MAXWIN)));
21829 	else {
21830 		/* Avoid shrinking window with window scaling. */
21831 		recwin = roundup2(recwin, 1 << tp->rcv_scale);
21832 		th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
21833 	}
21834 	/*
21835 	 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0
21836 	 * window.  This may cause the remote transmitter to stall.  This
21837 	 * flag tells soreceive() to disable delayed acknowledgements when
21838 	 * draining the buffer.  This can occur if the receiver is
21839 	 * attempting to read more data than can be buffered prior to
21840 	 * transmitting on the connection.
21841 	 */
21842 	if (th->th_win == 0) {
21843 		tp->t_sndzerowin++;
21844 		tp->t_flags |= TF_RXWIN0SENT;
21845 	} else
21846 		tp->t_flags &= ~TF_RXWIN0SENT;
21847 	tp->snd_up = tp->snd_una;	/* drag it along, its deprecated */
21848 	/* Now are we using fsb?, if so copy the template data to the mbuf */
21849 	if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) && rack->r_fsb_inited) {
21850 		uint8_t *cpto;
21851 
21852 		cpto = mtod(m, uint8_t *);
21853 		memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
21854 		/*
21855 		 * We have just copied in:
21856 		 * IP/IP6
21857 		 * <optional udphdr>
21858 		 * tcphdr (no options)
21859 		 *
21860 		 * We need to grab the correct pointers into the mbuf
21861 		 * for both the tcp header, and possibly the udp header (if tunneling).
21862 		 * We do this by using the offset in the copy buffer and adding it
21863 		 * to the mbuf base pointer (cpto).
21864 		 */
21865 #ifdef INET6
21866 		if (isipv6)
21867 			ip6 = mtod(m, struct ip6_hdr *);
21868 		else
21869 #endif				/* INET6 */
21870 #ifdef INET
21871 			ip = mtod(m, struct ip *);
21872 #endif
21873 		th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
21874 		/* If we have a udp header lets set it into the mbuf as well */
21875 		if (udp)
21876 			udp = (struct udphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.udp - rack->r_ctl.fsb.tcp_ip_hdr));
21877 	}
21878 	if (optlen) {
21879 		bcopy(opt, th + 1, optlen);
21880 		th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
21881 	}
21882 	/*
21883 	 * Put TCP length in extended header, and then checksum extended
21884 	 * header and data.
21885 	 */
21886 	m->m_pkthdr.len = hdrlen + len;	/* in6_cksum() need this */
21887 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
21888 	if (to.to_flags & TOF_SIGNATURE) {
21889 		/*
21890 		 * Calculate MD5 signature and put it into the place
21891 		 * determined before.
21892 		 * NOTE: since TCP options buffer doesn't point into
21893 		 * mbuf's data, calculate offset and use it.
21894 		 */
21895 		if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
21896 						       (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
21897 			/*
21898 			 * Do not send segment if the calculation of MD5
21899 			 * digest has failed.
21900 			 */
21901 			goto out;
21902 		}
21903 	}
21904 #endif
21905 #ifdef INET6
21906 	if (isipv6) {
21907 		/*
21908 		 * ip6_plen is not need to be filled now, and will be filled
21909 		 * in ip6_output.
21910 		 */
21911 		if (tp->t_port) {
21912 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
21913 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
21914 			udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
21915 			th->th_sum = htons(0);
21916 			UDPSTAT_INC(udps_opackets);
21917 		} else {
21918 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
21919 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
21920 			th->th_sum = in6_cksum_pseudo(ip6,
21921 						      sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
21922 						      0);
21923 		}
21924 	}
21925 #endif
21926 #if defined(INET6) && defined(INET)
21927 	else
21928 #endif
21929 #ifdef INET
21930 	{
21931 		if (tp->t_port) {
21932 			m->m_pkthdr.csum_flags = CSUM_UDP;
21933 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
21934 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
21935 						ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
21936 			th->th_sum = htons(0);
21937 			UDPSTAT_INC(udps_opackets);
21938 		} else {
21939 			m->m_pkthdr.csum_flags = CSUM_TCP;
21940 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
21941 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
21942 					       ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
21943 									IPPROTO_TCP + len + optlen));
21944 		}
21945 		/* IP version must be set here for ipv4/ipv6 checking later */
21946 		KASSERT(ip->ip_v == IPVERSION,
21947 			("%s: IP version incorrect: %d", __func__, ip->ip_v));
21948 	}
21949 #endif
21950 	/*
21951 	 * Enable TSO and specify the size of the segments. The TCP pseudo
21952 	 * header checksum is always provided. XXX: Fixme: This is currently
21953 	 * not the case for IPv6.
21954 	 */
21955 	if (tso) {
21956 		/*
21957 		 * Here we must use t_maxseg and the optlen since
21958 		 * the optlen may include SACK's (or DSACK).
21959 		 */
21960 		KASSERT(len > tp->t_maxseg - optlen,
21961 			("%s: len <= tso_segsz", __func__));
21962 		m->m_pkthdr.csum_flags |= CSUM_TSO;
21963 		m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen;
21964 	}
21965 	KASSERT(len + hdrlen == m_length(m, NULL),
21966 		("%s: mbuf chain different than expected: %d + %u != %u",
21967 		 __func__, len, hdrlen, m_length(m, NULL)));
21968 
21969 #ifdef TCP_HHOOK
21970 	/* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */
21971 	hhook_run_tcp_est_out(tp, th, &to, len, tso);
21972 #endif
21973 	if ((rack->r_ctl.crte != NULL) &&
21974 	    (rack->rc_hw_nobuf == 0) &&
21975 	    tcp_bblogging_on(tp)) {
21976 		rack_log_queue_level(tp, rack, len, &tv, cts);
21977 	}
21978 	/* We're getting ready to send; log now. */
21979 	if (tcp_bblogging_on(rack->rc_tp)) {
21980 		union tcp_log_stackspecific log;
21981 
21982 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
21983 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
21984 		if (rack->rack_no_prr)
21985 			log.u_bbr.flex1 = 0;
21986 		else
21987 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
21988 		log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
21989 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
21990 		log.u_bbr.flex4 = orig_len;
21991 		/* Save off the early/late values */
21992 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
21993 		log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
21994 		log.u_bbr.bw_inuse = rack_get_bw(rack);
21995 		log.u_bbr.cur_del_rate = rack->r_ctl.gp_bw;
21996 		log.u_bbr.flex8 = 0;
21997 		if (rsm) {
21998 			if (rsm->r_flags & RACK_RWND_COLLAPSED) {
21999 				rack_log_collapse(rack, rsm->r_start, rsm->r_end, 0, __LINE__, 5, rsm->r_flags, rsm);
22000 				counter_u64_add(rack_collapsed_win_rxt, 1);
22001 				counter_u64_add(rack_collapsed_win_rxt_bytes, (rsm->r_end - rsm->r_start));
22002 			}
22003 			if (doing_tlp)
22004 				log.u_bbr.flex8 = 2;
22005 			else
22006 				log.u_bbr.flex8 = 1;
22007 		} else {
22008 			if (doing_tlp)
22009 				log.u_bbr.flex8 = 3;
22010 		}
22011 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm);
22012 		log.u_bbr.flex7 = mark;
22013 		log.u_bbr.flex7 <<= 8;
22014 		log.u_bbr.flex7 |= pass;
22015 		log.u_bbr.pkts_out = tp->t_maxseg;
22016 		log.u_bbr.timeStamp = cts;
22017 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
22018 		if (rsm && (rsm->r_rtr_cnt > 0)) {
22019 			/*
22020 			 * When we have a retransmit we want to log the
22021 			 * burst at send and flight at send from before.
22022 			 */
22023 			log.u_bbr.flex5 = rsm->r_fas;
22024 			log.u_bbr.bbr_substate = rsm->r_bas;
22025 		} else {
22026 			/*
22027 			 * New transmits we log in flex5 the inflight again as
22028 			 * well as the number of segments in our send in the
22029 			 * substate field.
22030 			 */
22031 			log.u_bbr.flex5 = log.u_bbr.inflight;
22032 			log.u_bbr.bbr_substate = (uint8_t)((len + segsiz - 1)/segsiz);
22033 		}
22034 		log.u_bbr.lt_epoch = cwnd_to_use;
22035 		log.u_bbr.delivered = sendalot;
22036 		log.u_bbr.rttProp = (uint64_t)rsm;
22037 		log.u_bbr.pkt_epoch = __LINE__;
22038 		if (rsm) {
22039 			log.u_bbr.delRate = rsm->r_flags;
22040 			log.u_bbr.delRate <<= 31;
22041 			log.u_bbr.delRate |= rack->r_must_retran;
22042 			log.u_bbr.delRate <<= 1;
22043 			log.u_bbr.delRate |= (sack_rxmit & 0x00000001);
22044 		} else {
22045 			log.u_bbr.delRate = rack->r_must_retran;
22046 			log.u_bbr.delRate <<= 1;
22047 			log.u_bbr.delRate |= (sack_rxmit & 0x00000001);
22048 		}
22049 		lgb = tcp_log_event(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK,
22050 				    len, &log, false, NULL, __func__, __LINE__, &tv);
22051 	} else
22052 		lgb = NULL;
22053 
22054 	/*
22055 	 * Fill in IP length and desired time to live and send to IP level.
22056 	 * There should be a better way to handle ttl and tos; we could keep
22057 	 * them in the template, but need a way to checksum without them.
22058 	 */
22059 	/*
22060 	 * m->m_pkthdr.len should have been set before cksum calcuration,
22061 	 * because in6_cksum() need it.
22062 	 */
22063 #ifdef INET6
22064 	if (isipv6) {
22065 		/*
22066 		 * we separately set hoplimit for every segment, since the
22067 		 * user might want to change the value via setsockopt. Also,
22068 		 * desired default hop limit might be changed via Neighbor
22069 		 * Discovery.
22070 		 */
22071 		rack->r_ctl.fsb.hoplimit = ip6->ip6_hlim = in6_selecthlim(inp, NULL);
22072 
22073 		/*
22074 		 * Set the packet size here for the benefit of DTrace
22075 		 * probes. ip6_output() will set it properly; it's supposed
22076 		 * to include the option header lengths as well.
22077 		 */
22078 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
22079 
22080 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
22081 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
22082 		else
22083 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
22084 
22085 		if (tp->t_state == TCPS_SYN_SENT)
22086 			TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th);
22087 
22088 		TCP_PROBE5(send, NULL, tp, ip6, tp, th);
22089 		/* TODO: IPv6 IP6TOS_ECT bit on */
22090 		error = ip6_output(m,
22091 				   inp->in6p_outputopts,
22092 				   &inp->inp_route6,
22093 				   ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0),
22094 				   NULL, NULL, inp);
22095 
22096 		if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL)
22097 			mtu = inp->inp_route6.ro_nh->nh_mtu;
22098 	}
22099 #endif				/* INET6 */
22100 #if defined(INET) && defined(INET6)
22101 	else
22102 #endif
22103 #ifdef INET
22104 	{
22105 		ip->ip_len = htons(m->m_pkthdr.len);
22106 #ifdef INET6
22107 		if (inp->inp_vflag & INP_IPV6PROTO)
22108 			ip->ip_ttl = in6_selecthlim(inp, NULL);
22109 #endif				/* INET6 */
22110 		rack->r_ctl.fsb.hoplimit = ip->ip_ttl;
22111 		/*
22112 		 * If we do path MTU discovery, then we set DF on every
22113 		 * packet. This might not be the best thing to do according
22114 		 * to RFC3390 Section 2. However the tcp hostcache migitates
22115 		 * the problem so it affects only the first tcp connection
22116 		 * with a host.
22117 		 *
22118 		 * NB: Don't set DF on small MTU/MSS to have a safe
22119 		 * fallback.
22120 		 */
22121 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
22122 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
22123 			if (tp->t_port == 0 || len < V_tcp_minmss) {
22124 				ip->ip_off |= htons(IP_DF);
22125 			}
22126 		} else {
22127 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
22128 		}
22129 
22130 		if (tp->t_state == TCPS_SYN_SENT)
22131 			TCP_PROBE5(connect__request, NULL, tp, ip, tp, th);
22132 
22133 		TCP_PROBE5(send, NULL, tp, ip, tp, th);
22134 
22135 		error = ip_output(m,
22136 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
22137 				  inp->inp_options,
22138 #else
22139 				  NULL,
22140 #endif
22141 				  &inp->inp_route,
22142 				  ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0,
22143 				  inp);
22144 		if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL)
22145 			mtu = inp->inp_route.ro_nh->nh_mtu;
22146 	}
22147 #endif				/* INET */
22148 
22149 out:
22150 	if (lgb) {
22151 		lgb->tlb_errno = error;
22152 		lgb = NULL;
22153 	}
22154 	/*
22155 	 * In transmit state, time the transmission and arrange for the
22156 	 * retransmit.  In persist state, just set snd_max.
22157 	 */
22158 	rack_log_output(tp, &to, len, rack_seq, (uint8_t) flags, error,
22159 			rack_to_usec_ts(&tv),
22160 			rsm, add_flag, s_mb, s_moff, hw_tls, segsiz);
22161 	if (error == 0) {
22162 		if (rsm == NULL) {
22163 			if (rack->lt_bw_up == 0) {
22164 				rack->r_ctl.lt_timemark = tcp_tv_to_lusectick(&tv);
22165 				rack->r_ctl.lt_seq = tp->snd_una;
22166 				rack->lt_bw_up = 1;
22167 			} else if (((rack_seq + len) - rack->r_ctl.lt_seq) > 0x7fffffff) {
22168 				/*
22169 				 * Need to record what we have since we are
22170 				 * approaching seq wrap.
22171 				 */
22172 				uint64_t tmark;
22173 
22174 				rack->r_ctl.lt_bw_bytes += (tp->snd_una - rack->r_ctl.lt_seq);
22175 				rack->r_ctl.lt_seq = tp->snd_una;
22176 				tmark = tcp_tv_to_lusectick(&tv);
22177 				rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark);
22178 				rack->r_ctl.lt_timemark = tmark;
22179 			}
22180 		}
22181 		rack->forced_ack = 0;	/* If we send something zap the FA flag */
22182 		counter_u64_add(rack_total_bytes, len);
22183 		tcp_account_for_send(tp, len, (rsm != NULL), doing_tlp, hw_tls);
22184 		if (rsm && doing_tlp) {
22185 			rack->rc_last_sent_tlp_past_cumack = 0;
22186 			rack->rc_last_sent_tlp_seq_valid = 1;
22187 			rack->r_ctl.last_sent_tlp_seq = rsm->r_start;
22188 			rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start;
22189 		}
22190 		if (rack->rc_hw_nobuf) {
22191 			rack->rc_hw_nobuf = 0;
22192 			rack->r_ctl.rc_agg_delayed = 0;
22193 			rack->r_early = 0;
22194 			rack->r_late = 0;
22195 			rack->r_ctl.rc_agg_early = 0;
22196 		}
22197 		if (rsm && (doing_tlp == 0)) {
22198 			/* Set we retransmitted */
22199 			rack->rc_gp_saw_rec = 1;
22200 		} else {
22201 			if (cwnd_to_use > tp->snd_ssthresh) {
22202 				/* Set we sent in CA */
22203 				rack->rc_gp_saw_ca = 1;
22204 			} else {
22205 				/* Set we sent in SS */
22206 				rack->rc_gp_saw_ss = 1;
22207 			}
22208 		}
22209 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
22210 		    (tp->t_flags & TF_SACK_PERMIT) &&
22211 		    tp->rcv_numsacks > 0)
22212 			tcp_clean_dsack_blocks(tp);
22213 		tot_len_this_send += len;
22214 		if (len == 0) {
22215 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_SNDACK], 1);
22216 		} else {
22217 			int idx;
22218 
22219 			idx = (len / segsiz) + 3;
22220 			if (idx >= TCP_MSS_ACCT_ATIMER)
22221 				counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
22222 			else
22223 				counter_u64_add(rack_out_size[idx], 1);
22224 		}
22225 	}
22226 	if ((rack->rack_no_prr == 0) &&
22227 	    sub_from_prr &&
22228 	    (error == 0)) {
22229 		if (rack->r_ctl.rc_prr_sndcnt >= len)
22230 			rack->r_ctl.rc_prr_sndcnt -= len;
22231 		else
22232 			rack->r_ctl.rc_prr_sndcnt = 0;
22233 	}
22234 	sub_from_prr = 0;
22235 	if (doing_tlp) {
22236 		/* Make sure the TLP is added */
22237 		add_flag |= RACK_TLP;
22238 	} else if (rsm) {
22239 		/* If its a resend without TLP then it must not have the flag */
22240 		rsm->r_flags &= ~RACK_TLP;
22241 	}
22242 
22243 
22244 	if ((error == 0) &&
22245 	    (len > 0) &&
22246 	    (tp->snd_una == tp->snd_max))
22247 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
22248 	{
22249 		tcp_seq startseq = tp->snd_nxt;
22250 
22251 		/* Track our lost count */
22252 		if (rsm && (doing_tlp == 0))
22253 			rack->r_ctl.rc_loss_count += rsm->r_end - rsm->r_start;
22254 		/*
22255 		 * Advance snd_nxt over sequence space of this segment.
22256 		 */
22257 		if (error)
22258 			/* We don't log or do anything with errors */
22259 			goto nomore;
22260 		if (doing_tlp == 0) {
22261 			if (rsm == NULL) {
22262 				/*
22263 				 * Not a retransmission of some
22264 				 * sort, new data is going out so
22265 				 * clear our TLP count and flag.
22266 				 */
22267 				rack->rc_tlp_in_progress = 0;
22268 				rack->r_ctl.rc_tlp_cnt_out = 0;
22269 			}
22270 		} else {
22271 			/*
22272 			 * We have just sent a TLP, mark that it is true
22273 			 * and make sure our in progress is set so we
22274 			 * continue to check the count.
22275 			 */
22276 			rack->rc_tlp_in_progress = 1;
22277 			rack->r_ctl.rc_tlp_cnt_out++;
22278 		}
22279 		if (flags & (TH_SYN | TH_FIN)) {
22280 			if (flags & TH_SYN)
22281 				tp->snd_nxt++;
22282 			if (flags & TH_FIN) {
22283 				tp->snd_nxt++;
22284 				tp->t_flags |= TF_SENTFIN;
22285 			}
22286 		}
22287 		/* In the ENOBUFS case we do *not* update snd_max */
22288 		if (sack_rxmit)
22289 			goto nomore;
22290 
22291 		tp->snd_nxt += len;
22292 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
22293 			if (tp->snd_una == tp->snd_max) {
22294 				/*
22295 				 * Update the time we just added data since
22296 				 * none was outstanding.
22297 				 */
22298 				rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
22299 				tp->t_acktime = ticks;
22300 			}
22301 			tp->snd_max = tp->snd_nxt;
22302 			if (rack->rc_new_rnd_needed) {
22303 				/*
22304 				 * Update the rnd to start ticking not
22305 				 * that from a time perspective all of
22306 				 * the preceding idle time is "in the round"
22307 				 */
22308 				rack->rc_new_rnd_needed = 0;
22309 				rack->r_ctl.roundends = tp->snd_max;
22310 			}
22311 			/*
22312 			 * Time this transmission if not a retransmission and
22313 			 * not currently timing anything.
22314 			 * This is only relevant in case of switching back to
22315 			 * the base stack.
22316 			 */
22317 			if (tp->t_rtttime == 0) {
22318 				tp->t_rtttime = ticks;
22319 				tp->t_rtseq = startseq;
22320 				KMOD_TCPSTAT_INC(tcps_segstimed);
22321 			}
22322 			if (len &&
22323 			    ((tp->t_flags & TF_GPUTINPROG) == 0))
22324 				rack_start_gp_measurement(tp, rack, startseq, sb_offset);
22325 		}
22326 		/*
22327 		 * If we are doing FO we need to update the mbuf position and subtract
22328 		 * this happens when the peer sends us duplicate information and
22329 		 * we thus want to send a DSACK.
22330 		 *
22331 		 * XXXRRS: This brings to mind a ?, when we send a DSACK block is TSO
22332 		 * turned off? If not then we are going to echo multiple DSACK blocks
22333 		 * out (with the TSO), which we should not be doing.
22334 		 */
22335 		if (rack->r_fast_output && len) {
22336 			if (rack->r_ctl.fsb.left_to_send > len)
22337 				rack->r_ctl.fsb.left_to_send -= len;
22338 			else
22339 				rack->r_ctl.fsb.left_to_send = 0;
22340 			if (rack->r_ctl.fsb.left_to_send < segsiz)
22341 				rack->r_fast_output = 0;
22342 			if (rack->r_fast_output) {
22343 				rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
22344 				rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
22345 				rack->r_ctl.fsb.o_t_len = M_TRAILINGROOM(rack->r_ctl.fsb.m);
22346 			}
22347 		}
22348 	}
22349 nomore:
22350 	if (error) {
22351 		rack->r_ctl.rc_agg_delayed = 0;
22352 		rack->r_early = 0;
22353 		rack->r_late = 0;
22354 		rack->r_ctl.rc_agg_early = 0;
22355 		SOCKBUF_UNLOCK_ASSERT(sb);	/* Check gotos. */
22356 		/*
22357 		 * Failures do not advance the seq counter above. For the
22358 		 * case of ENOBUFS we will fall out and retry in 1ms with
22359 		 * the hpts. Everything else will just have to retransmit
22360 		 * with the timer.
22361 		 *
22362 		 * In any case, we do not want to loop around for another
22363 		 * send without a good reason.
22364 		 */
22365 		sendalot = 0;
22366 		switch (error) {
22367 		case EPERM:
22368 			tp->t_softerror = error;
22369 #ifdef TCP_ACCOUNTING
22370 			crtsc = get_cyclecount();
22371 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22372 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
22373 			}
22374 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22375 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
22376 			}
22377 			sched_unpin();
22378 #endif
22379 			return (error);
22380 		case ENOBUFS:
22381 			/*
22382 			 * Pace us right away to retry in a some
22383 			 * time
22384 			 */
22385 			if (rack->r_ctl.crte != NULL) {
22386 				tcp_trace_point(rack->rc_tp, TCP_TP_HWENOBUF);
22387 				if (tcp_bblogging_on(rack->rc_tp))
22388 					rack_log_queue_level(tp, rack, len, &tv, cts);
22389 			} else
22390 				tcp_trace_point(rack->rc_tp, TCP_TP_ENOBUF);
22391 			slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC);
22392 			if (rack->rc_enobuf < 0x7f)
22393 				rack->rc_enobuf++;
22394 			if (slot < (10 * HPTS_USEC_IN_MSEC))
22395 				slot = 10 * HPTS_USEC_IN_MSEC;
22396 			if (rack->r_ctl.crte != NULL) {
22397 				counter_u64_add(rack_saw_enobuf_hw, 1);
22398 				tcp_rl_log_enobuf(rack->r_ctl.crte);
22399 			}
22400 			counter_u64_add(rack_saw_enobuf, 1);
22401 			goto enobufs;
22402 		case EMSGSIZE:
22403 			/*
22404 			 * For some reason the interface we used initially
22405 			 * to send segments changed to another or lowered
22406 			 * its MTU. If TSO was active we either got an
22407 			 * interface without TSO capabilits or TSO was
22408 			 * turned off. If we obtained mtu from ip_output()
22409 			 * then update it and try again.
22410 			 */
22411 			if (tso)
22412 				tp->t_flags &= ~TF_TSO;
22413 			if (mtu != 0) {
22414 				int saved_mtu;
22415 
22416 				saved_mtu = tp->t_maxseg;
22417 				tcp_mss_update(tp, -1, mtu, NULL, NULL);
22418 				if (saved_mtu > tp->t_maxseg) {
22419 					goto again;
22420 				}
22421 			}
22422 			slot = 10 * HPTS_USEC_IN_MSEC;
22423 			rack_start_hpts_timer(rack, tp, cts, slot, 0, 0);
22424 #ifdef TCP_ACCOUNTING
22425 			crtsc = get_cyclecount();
22426 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22427 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
22428 			}
22429 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22430 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
22431 			}
22432 			sched_unpin();
22433 #endif
22434 			return (error);
22435 		case ENETUNREACH:
22436 			counter_u64_add(rack_saw_enetunreach, 1);
22437 		case EHOSTDOWN:
22438 		case EHOSTUNREACH:
22439 		case ENETDOWN:
22440 			if (TCPS_HAVERCVDSYN(tp->t_state)) {
22441 				tp->t_softerror = error;
22442 			}
22443 			/* FALLTHROUGH */
22444 		default:
22445 			slot = 10 * HPTS_USEC_IN_MSEC;
22446 			rack_start_hpts_timer(rack, tp, cts, slot, 0, 0);
22447 #ifdef TCP_ACCOUNTING
22448 			crtsc = get_cyclecount();
22449 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22450 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
22451 			}
22452 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22453 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
22454 			}
22455 			sched_unpin();
22456 #endif
22457 			return (error);
22458 		}
22459 	} else {
22460 		rack->rc_enobuf = 0;
22461 		if (IN_FASTRECOVERY(tp->t_flags) && rsm)
22462 			rack->r_ctl.retran_during_recovery += len;
22463 	}
22464 	KMOD_TCPSTAT_INC(tcps_sndtotal);
22465 
22466 	/*
22467 	 * Data sent (as far as we can tell). If this advertises a larger
22468 	 * window than any other segment, then remember the size of the
22469 	 * advertised window. Any pending ACK has now been sent.
22470 	 */
22471 	if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
22472 		tp->rcv_adv = tp->rcv_nxt + recwin;
22473 
22474 	tp->last_ack_sent = tp->rcv_nxt;
22475 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
22476 enobufs:
22477 	if (sendalot) {
22478 		/* Do we need to turn off sendalot? */
22479 		if (rack->r_ctl.rc_pace_max_segs &&
22480 		    (tot_len_this_send >= rack->r_ctl.rc_pace_max_segs)) {
22481 			/* We hit our max. */
22482 			sendalot = 0;
22483 		} else if ((rack->rc_user_set_max_segs) &&
22484 			   (tot_len_this_send >= (rack->rc_user_set_max_segs * segsiz))) {
22485 			/* We hit the user defined max */
22486 			sendalot = 0;
22487 		}
22488 	}
22489 	if ((error == 0) && (flags & TH_FIN))
22490 		tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN);
22491 	if (flags & TH_RST) {
22492 		/*
22493 		 * We don't send again after sending a RST.
22494 		 */
22495 		slot = 0;
22496 		sendalot = 0;
22497 		if (error == 0)
22498 			tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
22499 	} else if ((slot == 0) && (sendalot == 0) && tot_len_this_send) {
22500 		/*
22501 		 * Get our pacing rate, if an error
22502 		 * occurred in sending (ENOBUF) we would
22503 		 * hit the else if with slot preset. Other
22504 		 * errors return.
22505 		 */
22506 		slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, rsm, segsiz);
22507 	}
22508 	if (rsm &&
22509 	    (rsm->r_flags & RACK_HAS_SYN) == 0 &&
22510 	    rack->use_rack_rr) {
22511 		/* Its a retransmit and we use the rack cheat? */
22512 		if ((slot == 0) ||
22513 		    (rack->rc_always_pace == 0) ||
22514 		    (rack->r_rr_config == 1)) {
22515 			/*
22516 			 * We have no pacing set or we
22517 			 * are using old-style rack or
22518 			 * we are overridden to use the old 1ms pacing.
22519 			 */
22520 			slot = rack->r_ctl.rc_min_to;
22521 		}
22522 	}
22523 	/* We have sent clear the flag */
22524 	rack->r_ent_rec_ns = 0;
22525 	if (rack->r_must_retran) {
22526 		if (rsm) {
22527 			rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start);
22528 			if (SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) {
22529 				/*
22530 				 * We have retransmitted all.
22531 				 */
22532 				rack->r_must_retran = 0;
22533 				rack->r_ctl.rc_out_at_rto = 0;
22534 			}
22535 		} else if (SEQ_GEQ(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) {
22536 			/*
22537 			 * Sending new data will also kill
22538 			 * the loop.
22539 			 */
22540 			rack->r_must_retran = 0;
22541 			rack->r_ctl.rc_out_at_rto = 0;
22542 		}
22543 	}
22544 	rack->r_ctl.fsb.recwin = recwin;
22545 	if ((tp->t_flags & (TF_WASCRECOVERY|TF_WASFRECOVERY)) &&
22546 	    SEQ_GT(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) {
22547 		/*
22548 		 * We hit an RTO and now have past snd_max at the RTO
22549 		 * clear all the WAS flags.
22550 		 */
22551 		tp->t_flags &= ~(TF_WASCRECOVERY|TF_WASFRECOVERY);
22552 	}
22553 	if (slot) {
22554 		/* set the rack tcb into the slot N */
22555 		if ((error == 0) &&
22556 		    rack_use_rfo &&
22557 		    ((flags & (TH_SYN|TH_FIN)) == 0) &&
22558 		    (rsm == NULL) &&
22559 		    (tp->snd_nxt == tp->snd_max) &&
22560 		    (ipoptlen == 0) &&
22561 		    (tp->rcv_numsacks == 0) &&
22562 		    rack->r_fsb_inited &&
22563 		    TCPS_HAVEESTABLISHED(tp->t_state) &&
22564 		    ((IN_RECOVERY(tp->t_flags)) == 0) &&
22565 		    (rack->r_must_retran == 0) &&
22566 		    ((tp->t_flags & TF_NEEDFIN) == 0) &&
22567 		    (len > 0) && (orig_len > 0) &&
22568 		    (orig_len > len) &&
22569 		    ((orig_len - len) >= segsiz) &&
22570 		    ((optlen == 0) ||
22571 		     ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
22572 			/* We can send at least one more MSS using our fsb */
22573 			rack_setup_fast_output(tp, rack, sb, len, orig_len,
22574 					       segsiz, pace_max_seg, hw_tls, flags);
22575 		} else
22576 			rack->r_fast_output = 0;
22577 		rack_log_fsb(rack, tp, so, flags,
22578 			     ipoptlen, orig_len, len, error,
22579 			     (rsm == NULL), optlen, __LINE__, 2);
22580 	} else if (sendalot) {
22581 		int ret;
22582 
22583 		sack_rxmit = 0;
22584 		if ((error == 0) &&
22585 		    rack_use_rfo &&
22586 		    ((flags & (TH_SYN|TH_FIN)) == 0) &&
22587 		    (rsm == NULL) &&
22588 		    (ipoptlen == 0) &&
22589 		    (tp->rcv_numsacks == 0) &&
22590 		    (tp->snd_nxt == tp->snd_max) &&
22591 		    (rack->r_must_retran == 0) &&
22592 		    rack->r_fsb_inited &&
22593 		    TCPS_HAVEESTABLISHED(tp->t_state) &&
22594 		    ((IN_RECOVERY(tp->t_flags)) == 0) &&
22595 		    ((tp->t_flags & TF_NEEDFIN) == 0) &&
22596 		    (len > 0) && (orig_len > 0) &&
22597 		    (orig_len > len) &&
22598 		    ((orig_len - len) >= segsiz) &&
22599 		    ((optlen == 0) ||
22600 		     ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
22601 			/* we can use fast_output for more */
22602 			rack_setup_fast_output(tp, rack, sb, len, orig_len,
22603 					       segsiz, pace_max_seg, hw_tls, flags);
22604 			if (rack->r_fast_output) {
22605 				error = 0;
22606 				ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error);
22607 				if (ret >= 0)
22608 					return (ret);
22609 			        else if (error)
22610 					goto nomore;
22611 
22612 			}
22613 		}
22614 		goto again;
22615 	}
22616 	/* Assure when we leave that snd_nxt will point to top */
22617 skip_all_send:
22618 	if (SEQ_GT(tp->snd_max, tp->snd_nxt))
22619 		tp->snd_nxt = tp->snd_max;
22620 	rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, 0);
22621 #ifdef TCP_ACCOUNTING
22622 	crtsc = get_cyclecount() - ts_val;
22623 	if (tot_len_this_send) {
22624 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22625 			tp->tcp_cnt_counters[SND_OUT_DATA]++;
22626 		}
22627 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22628 			tp->tcp_proc_time[SND_OUT_DATA] += crtsc;
22629 		}
22630 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22631 			tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) /segsiz);
22632 		}
22633 	} else {
22634 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22635 			tp->tcp_cnt_counters[SND_OUT_ACK]++;
22636 		}
22637 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22638 			tp->tcp_proc_time[SND_OUT_ACK] += crtsc;
22639 		}
22640 	}
22641 	sched_unpin();
22642 #endif
22643 	if (error == ENOBUFS)
22644 		error = 0;
22645 	return (error);
22646 }
22647 
22648 static void
22649 rack_update_seg(struct tcp_rack *rack)
22650 {
22651 	uint32_t orig_val;
22652 
22653 	orig_val = rack->r_ctl.rc_pace_max_segs;
22654 	rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
22655 	if (orig_val != rack->r_ctl.rc_pace_max_segs)
22656 		rack_log_pacing_delay_calc(rack, 0, 0, orig_val, 0, 0, 15, __LINE__, NULL, 0);
22657 }
22658 
22659 static void
22660 rack_mtu_change(struct tcpcb *tp)
22661 {
22662 	/*
22663 	 * The MSS may have changed
22664 	 */
22665 	struct tcp_rack *rack;
22666 	struct rack_sendmap *rsm;
22667 
22668 	rack = (struct tcp_rack *)tp->t_fb_ptr;
22669 	if (rack->r_ctl.rc_pace_min_segs != ctf_fixed_maxseg(tp)) {
22670 		/*
22671 		 * The MTU has changed we need to resend everything
22672 		 * since all we have sent is lost. We first fix
22673 		 * up the mtu though.
22674 		 */
22675 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
22676 		/* We treat this like a full retransmit timeout without the cwnd adjustment */
22677 		rack_remxt_tmr(tp);
22678 		rack->r_fast_output = 0;
22679 		rack->r_ctl.rc_out_at_rto = ctf_flight_size(tp,
22680 						rack->r_ctl.rc_sacked);
22681 		rack->r_ctl.rc_snd_max_at_rto = tp->snd_max;
22682 		rack->r_must_retran = 1;
22683 		/* Mark all inflight to needing to be rxt'd */
22684 		TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
22685 			rsm->r_flags |= (RACK_MUST_RXT|RACK_PMTU_CHG);
22686 		}
22687 	}
22688 	sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
22689 	/* We don't use snd_nxt to retransmit */
22690 	tp->snd_nxt = tp->snd_max;
22691 }
22692 
22693 static int
22694 rack_set_dgp(struct tcp_rack *rack)
22695 {
22696 	/* pace_always=1 */
22697 	if (rack->rc_always_pace == 0) {
22698 		if (tcp_can_enable_pacing() == 0)
22699 			return (EBUSY);
22700 	}
22701 	rack->rc_fillcw_apply_discount = 0;
22702 	rack->dgp_on = 1;
22703 	rack->rc_always_pace = 1;
22704 	rack->use_fixed_rate = 0;
22705 	if (rack->gp_ready)
22706 		rack_set_cc_pacing(rack);
22707 	rack->rc_tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
22708 	rack->rack_attempt_hdwr_pace = 0;
22709 	/* rxt settings */
22710 	rack->full_size_rxt = 1;
22711 	rack->shape_rxt_to_pacing_min  = 0;
22712 	/* cmpack=1 */
22713 	rack->r_use_cmp_ack = 1;
22714 	if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state) &&
22715 	    rack->r_use_cmp_ack)
22716 		rack->rc_tp->t_flags2 |= TF2_MBUF_ACKCMP;
22717 	/* scwnd=1 */
22718 	rack->rack_enable_scwnd = 1;
22719 	/* dynamic=100 */
22720 	rack->rc_gp_dyn_mul = 1;
22721 	/* gp_inc_ca */
22722 	rack->r_ctl.rack_per_of_gp_ca = 100;
22723 	/* rrr_conf=3 */
22724 	rack->r_rr_config = 3;
22725 	/* npush=2 */
22726 	rack->r_ctl.rc_no_push_at_mrtt = 2;
22727 	/* fillcw=1 */
22728 	if (rack->r_cwnd_was_clamped == 0) {
22729 		rack->rc_pace_to_cwnd = 1;
22730 	} else {
22731 		rack->rc_pace_to_cwnd = 0;
22732 		/* Reset all multipliers to 100.0 so just the measured bw */
22733 		rack->r_ctl.rack_per_of_gp_ss = 100;
22734 		rack->r_ctl.rack_per_of_gp_ca = 100;
22735 	}
22736 	rack->rc_pace_fill_if_rttin_range = 0;
22737 	rack->rtt_limit_mul = 0;
22738 	/* noprr=1 */
22739 	rack->rack_no_prr = 1;
22740 	/* lscwnd=1 */
22741 	rack->r_limit_scw = 1;
22742 	/* gp_inc_rec */
22743 	rack->r_ctl.rack_per_of_gp_rec = 90;
22744 	rack_client_buffer_level_set(rack);
22745 	return (0);
22746 }
22747 
22748 
22749 
22750 static int
22751 rack_set_profile(struct tcp_rack *rack, int prof)
22752 {
22753 	int err = EINVAL;
22754 	if (prof == 1) {
22755 		/*
22756 		 * Profile 1 is "standard" DGP. It ignores
22757 		 * client buffer level.
22758 		 */
22759 		rack->r_ctl.rc_dgp_bl_agg = DGP_LEVEL0;
22760 		err = rack_set_dgp(rack);
22761 		if (err)
22762 			return (err);
22763 	} else if (prof == 2) {
22764 		/*
22765 		 * Profile 2 is DGP. Less aggressive with
22766 		 * respect to client buffer level.
22767 		 */
22768 		rack->r_ctl.rc_dgp_bl_agg = DGP_LEVEL1;
22769 		err = rack_set_dgp(rack);
22770 		if (err)
22771 			return (err);
22772 	} else if (prof == 3) {
22773 		/*
22774 		 * Profile 3 is DGP. Even Less aggressive with
22775 		 * respect to client buffer level.
22776 		 */
22777 		rack->r_ctl.rc_dgp_bl_agg = DGP_LEVEL2;
22778 		err = rack_set_dgp(rack);
22779 		if (err)
22780 			return (err);
22781 	} else if (prof == 4) {
22782 		/*
22783 		 * Profile 4 is DGP with the most responsiveness
22784 		 * to client buffer level.
22785 		 */
22786 		rack->r_ctl.rc_dgp_bl_agg = DGP_LEVEL3;
22787 		err = rack_set_dgp(rack);
22788 		if (err)
22789 			return (err);
22790 	} else if (prof == 5) {
22791 		err = rack_set_dgp(rack);
22792 		if (err)
22793 			return (err);
22794 		/*
22795 		 * By turning DGP off we change the rate
22796 		 * picked to be only the one the cwnd and rtt
22797 		 * get us.
22798 		 */
22799 		rack->dgp_on = 0;
22800 	} else if (prof == 6) {
22801 		err = rack_set_dgp(rack);
22802 		if (err)
22803 			return (err);
22804 		/*
22805 		 * Profile 6 tweaks DGP so that it will apply to
22806 		 * fill-cw the same settings that profile5 does
22807 		 * to replace DGP. It gets then the max(dgp-rate, fillcw(discounted).
22808 		 */
22809 		rack->rc_fillcw_apply_discount = 1;
22810 	} else if (prof == 0) {
22811 		/* This changes things back to the default settings */
22812 		rack->dgp_on = 0;
22813 		rack->rc_hybrid_mode = 0;
22814 		err = 0;
22815 		if (rack_fill_cw_state)
22816 			rack->rc_pace_to_cwnd = 1;
22817 		else
22818 			rack->rc_pace_to_cwnd = 0;
22819 		if (rack->rc_always_pace) {
22820 			tcp_decrement_paced_conn();
22821 			rack_undo_cc_pacing(rack);
22822 			rack->rc_always_pace = 0;
22823 		}
22824 		if (rack_pace_every_seg && tcp_can_enable_pacing()) {
22825 			rack->rc_always_pace = 1;
22826 			if (rack->rack_hibeta)
22827 				rack_set_cc_pacing(rack);
22828 		} else
22829 			rack->rc_always_pace = 0;
22830 		if (rack_dsack_std_based & 0x1) {
22831 			/* Basically this means all rack timers are at least (srtt + 1/4 srtt) */
22832 			rack->rc_rack_tmr_std_based = 1;
22833 		}
22834 		if (rack_dsack_std_based & 0x2) {
22835 			/* Basically this means  rack timers are extended based on dsack by up to (2 * srtt) */
22836 			rack->rc_rack_use_dsack = 1;
22837 		}
22838 		if (rack_use_cmp_acks)
22839 			rack->r_use_cmp_ack = 1;
22840 		else
22841 			rack->r_use_cmp_ack = 0;
22842 		if (rack_disable_prr)
22843 			rack->rack_no_prr = 1;
22844 		else
22845 			rack->rack_no_prr = 0;
22846 		if (rack_gp_no_rec_chg)
22847 			rack->rc_gp_no_rec_chg = 1;
22848 		else
22849 			rack->rc_gp_no_rec_chg = 0;
22850 		if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack) {
22851 			rack->r_mbuf_queue = 1;
22852 			if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state))
22853 				rack->rc_tp->t_flags2 |= TF2_MBUF_ACKCMP;
22854 			rack->rc_tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
22855 		} else {
22856 			rack->r_mbuf_queue = 0;
22857 			rack->rc_tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
22858 		}
22859 		if (rack_enable_shared_cwnd)
22860 			rack->rack_enable_scwnd = 1;
22861 		else
22862 			rack->rack_enable_scwnd = 0;
22863 		if (rack_do_dyn_mul) {
22864 			/* When dynamic adjustment is on CA needs to start at 100% */
22865 			rack->rc_gp_dyn_mul = 1;
22866 			if (rack_do_dyn_mul >= 100)
22867 				rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul;
22868 		} else {
22869 			rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca;
22870 			rack->rc_gp_dyn_mul = 0;
22871 		}
22872 		rack->r_rr_config = 0;
22873 		rack->r_ctl.rc_no_push_at_mrtt = 0;
22874 		rack->rc_pace_to_cwnd = 0;
22875 		rack->rc_pace_fill_if_rttin_range = 0;
22876 		rack->rtt_limit_mul = 0;
22877 
22878 		if (rack_enable_hw_pacing)
22879 			rack->rack_hdw_pace_ena = 1;
22880 		else
22881 			rack->rack_hdw_pace_ena = 0;
22882 		if (rack_disable_prr)
22883 			rack->rack_no_prr = 1;
22884 		else
22885 			rack->rack_no_prr = 0;
22886 		if (rack_limits_scwnd)
22887 			rack->r_limit_scw  = 1;
22888 		else
22889 			rack->r_limit_scw  = 0;
22890 		rack_init_retransmit_value(rack, rack_rxt_controls);
22891 		err = 0;
22892 	}
22893 	return (err);
22894 }
22895 
22896 static int
22897 rack_add_deferred_option(struct tcp_rack *rack, int sopt_name, uint64_t loptval)
22898 {
22899 	struct deferred_opt_list *dol;
22900 
22901 	dol = malloc(sizeof(struct deferred_opt_list),
22902 		     M_TCPFSB, M_NOWAIT|M_ZERO);
22903 	if (dol == NULL) {
22904 		/*
22905 		 * No space yikes -- fail out..
22906 		 */
22907 		return (0);
22908 	}
22909 	dol->optname = sopt_name;
22910 	dol->optval = loptval;
22911 	TAILQ_INSERT_TAIL(&rack->r_ctl.opt_list, dol, next);
22912 	return (1);
22913 }
22914 
22915 static int
22916 process_hybrid_pacing(struct tcp_rack *rack, struct tcp_hybrid_req *hybrid)
22917 {
22918 #ifdef TCP_REQUEST_TRK
22919 	struct tcp_sendfile_track *sft;
22920 	struct timeval tv;
22921 	tcp_seq seq;
22922 	int err;
22923 
22924 	microuptime(&tv);
22925 
22926 	/*
22927 	 * If BB logging is not on we need to look at the DTL flag.
22928 	 * If its on already then those reasons override the DTL input.
22929 	 * We do this with any request, you can turn DTL on, but it does
22930 	 * not turn off at least from hybrid pacing requests.
22931 	 */
22932 	if (tcp_bblogging_on(rack->rc_tp) == 0) {
22933 		if (hybrid->hybrid_flags & TCP_HYBRID_PACING_DTL) {
22934 			/* Turn on BB point logging  */
22935 			tcp_set_bblog_state(rack->rc_tp, TCP_LOG_VIA_BBPOINTS,
22936 					    TCP_BBPOINT_REQ_LEVEL_LOGGING);
22937 		}
22938 	}
22939 	/* Make sure no fixed rate is on */
22940 	rack->use_fixed_rate = 0;
22941 	rack->r_ctl.rc_fixed_pacing_rate_rec = 0;
22942 	rack->r_ctl.rc_fixed_pacing_rate_ca = 0;
22943 	rack->r_ctl.rc_fixed_pacing_rate_ss = 0;
22944 	/* Now allocate or find our entry that will have these settings */
22945 	sft = tcp_req_alloc_req_full(rack->rc_tp, &hybrid->req, tcp_tv_to_lusectick(&tv), 0);
22946 	if (sft == NULL) {
22947 		rack->rc_tp->tcp_hybrid_error++;
22948 		/* no space, where would it have gone? */
22949 		seq = rack->rc_tp->snd_una + rack->rc_tp->t_inpcb.inp_socket->so_snd.sb_ccc;
22950 		rack_log_hybrid(rack, seq, NULL, HYBRID_LOG_NO_ROOM, __LINE__, 0);
22951 		return (ENOSPC);
22952 	}
22953 	/* The seq will be snd_una + everything in the buffer */
22954 	seq = sft->start_seq;
22955 	if ((hybrid->hybrid_flags & TCP_HYBRID_PACING_ENABLE) == 0) {
22956 		/* Disabling hybrid pacing */
22957 		if (rack->rc_hybrid_mode) {
22958 			rack_set_profile(rack, 0);
22959 			rack->rc_tp->tcp_hybrid_stop++;
22960 		}
22961 		rack_log_hybrid(rack, seq, sft, HYBRID_LOG_TURNED_OFF, __LINE__, 0);
22962 		return (0);
22963 	}
22964 	if (rack->dgp_on == 0) {
22965 		/*
22966 		 * If we have not yet turned DGP on, do so
22967 		 * now setting pure DGP mode, no buffer level
22968 		 * response.
22969 		 */
22970 		if ((err = rack_set_profile(rack, 1)) != 0){
22971 			/* Failed to turn pacing on */
22972 			rack->rc_tp->tcp_hybrid_error++;
22973 			rack_log_hybrid(rack, seq, sft, HYBRID_LOG_NO_PACING, __LINE__, 0);
22974 			return (err);
22975 		}
22976 	}
22977 	/* Now set in our flags */
22978 	sft->hybrid_flags = hybrid->hybrid_flags | TCP_HYBRID_PACING_WASSET;
22979 	if (hybrid->hybrid_flags & TCP_HYBRID_PACING_CSPR)
22980 		sft->cspr = hybrid->cspr;
22981 	else
22982 		sft->cspr = 0;
22983 	if (hybrid->hybrid_flags & TCP_HYBRID_PACING_H_MS)
22984 		sft->hint_maxseg = hybrid->hint_maxseg;
22985 	else
22986 		sft->hint_maxseg = 0;
22987 	rack->rc_hybrid_mode = 1;
22988 	rack->rc_tp->tcp_hybrid_start++;
22989 	rack_log_hybrid(rack, seq, sft, HYBRID_LOG_RULES_SET, __LINE__,0);
22990 	return (0);
22991 #else
22992 	return (ENOTSUP);
22993 #endif
22994 }
22995 
22996 static int
22997 rack_process_option(struct tcpcb *tp, struct tcp_rack *rack, int sopt_name,
22998 		    uint32_t optval, uint64_t loptval, struct tcp_hybrid_req *hybrid)
22999 
23000 {
23001 	struct epoch_tracker et;
23002 	struct sockopt sopt;
23003 	struct cc_newreno_opts opt;
23004 	uint64_t val;
23005 	int error = 0;
23006 	uint16_t ca, ss;
23007 
23008 	switch (sopt_name) {
23009 	case TCP_RACK_SET_RXT_OPTIONS:
23010 		if ((optval >= 0) && (optval <= 2)) {
23011 			rack_init_retransmit_value(rack, optval);
23012 		} else {
23013 			/*
23014 			 * You must send in 0, 1 or 2 all else is
23015 			 * invalid.
23016 			 */
23017 			error = EINVAL;
23018 		}
23019 		break;
23020 	case TCP_RACK_DSACK_OPT:
23021 		RACK_OPTS_INC(tcp_rack_dsack_opt);
23022 		if (optval & 0x1) {
23023 			rack->rc_rack_tmr_std_based = 1;
23024 		} else {
23025 			rack->rc_rack_tmr_std_based = 0;
23026 		}
23027 		if (optval & 0x2) {
23028 			rack->rc_rack_use_dsack = 1;
23029 		} else {
23030 			rack->rc_rack_use_dsack = 0;
23031 		}
23032 		rack_log_dsack_event(rack, 5, __LINE__, 0, 0);
23033 		break;
23034 	case TCP_RACK_PACING_DIVISOR:
23035 		RACK_OPTS_INC(tcp_rack_pacing_divisor);
23036 		if (optval == 0) {
23037 			rack->r_ctl.pace_len_divisor = rack_default_pacing_divisor;
23038 		} else {
23039 			if (optval < RL_MIN_DIVISOR)
23040 				rack->r_ctl.pace_len_divisor = RL_MIN_DIVISOR;
23041 			else
23042 				rack->r_ctl.pace_len_divisor = optval;
23043 		}
23044 		break;
23045 	case TCP_RACK_HI_BETA:
23046 		RACK_OPTS_INC(tcp_rack_hi_beta);
23047 		if (optval > 0) {
23048 			rack->rack_hibeta = 1;
23049 			if ((optval >= 50) &&
23050 			    (optval <= 100)) {
23051 				/*
23052 				 * User wants to set a custom beta.
23053 				 */
23054 				rack->r_ctl.saved_hibeta = optval;
23055 				if (rack->rc_pacing_cc_set)
23056 					rack_undo_cc_pacing(rack);
23057 				rack->r_ctl.rc_saved_beta.beta = optval;
23058 			}
23059 			if (rack->rc_pacing_cc_set == 0)
23060 				rack_set_cc_pacing(rack);
23061 		} else {
23062 			rack->rack_hibeta = 0;
23063 			if (rack->rc_pacing_cc_set)
23064 				rack_undo_cc_pacing(rack);
23065 		}
23066 		break;
23067 	case TCP_RACK_PACING_BETA:
23068 		RACK_OPTS_INC(tcp_rack_beta);
23069 		if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0) {
23070 			/* This only works for newreno. */
23071 			error = EINVAL;
23072 			break;
23073 		}
23074 		if (rack->rc_pacing_cc_set) {
23075 			/*
23076 			 * Set them into the real CC module
23077 			 * whats in the rack pcb is the old values
23078 			 * to be used on restoral/
23079 			 */
23080 			sopt.sopt_dir = SOPT_SET;
23081 			opt.name = CC_NEWRENO_BETA;
23082 			opt.val = optval;
23083 			if (CC_ALGO(tp)->ctl_output != NULL)
23084 				error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
23085 			else {
23086 				error = ENOENT;
23087 				break;
23088 			}
23089 		} else {
23090 			/*
23091 			 * Not pacing yet so set it into our local
23092 			 * rack pcb storage.
23093 			 */
23094 			rack->r_ctl.rc_saved_beta.beta = optval;
23095 		}
23096 		break;
23097 	case TCP_RACK_TIMER_SLOP:
23098 		RACK_OPTS_INC(tcp_rack_timer_slop);
23099 		rack->r_ctl.timer_slop = optval;
23100 		if (rack->rc_tp->t_srtt) {
23101 			/*
23102 			 * If we have an SRTT lets update t_rxtcur
23103 			 * to have the new slop.
23104 			 */
23105 			RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
23106 					   rack_rto_min, rack_rto_max,
23107 					   rack->r_ctl.timer_slop);
23108 		}
23109 		break;
23110 	case TCP_RACK_PACING_BETA_ECN:
23111 		RACK_OPTS_INC(tcp_rack_beta_ecn);
23112 		if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0) {
23113 			/* This only works for newreno. */
23114 			error = EINVAL;
23115 			break;
23116 		}
23117 		if (rack->rc_pacing_cc_set) {
23118 			/*
23119 			 * Set them into the real CC module
23120 			 * whats in the rack pcb is the old values
23121 			 * to be used on restoral/
23122 			 */
23123 			sopt.sopt_dir = SOPT_SET;
23124 			opt.name = CC_NEWRENO_BETA_ECN;
23125 			opt.val = optval;
23126 			if (CC_ALGO(tp)->ctl_output != NULL)
23127 				error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
23128 			else
23129 				error = ENOENT;
23130 		} else {
23131 			/*
23132 			 * Not pacing yet so set it into our local
23133 			 * rack pcb storage.
23134 			 */
23135 			rack->r_ctl.rc_saved_beta.beta_ecn = optval;
23136 			rack->r_ctl.rc_saved_beta.newreno_flags = CC_NEWRENO_BETA_ECN_ENABLED;
23137 		}
23138 		break;
23139 	case TCP_DEFER_OPTIONS:
23140 		RACK_OPTS_INC(tcp_defer_opt);
23141 		if (optval) {
23142 			if (rack->gp_ready) {
23143 				/* Too late */
23144 				error = EINVAL;
23145 				break;
23146 			}
23147 			rack->defer_options = 1;
23148 		} else
23149 			rack->defer_options = 0;
23150 		break;
23151 	case TCP_RACK_MEASURE_CNT:
23152 		RACK_OPTS_INC(tcp_rack_measure_cnt);
23153 		if (optval && (optval <= 0xff)) {
23154 			rack->r_ctl.req_measurements = optval;
23155 		} else
23156 			error = EINVAL;
23157 		break;
23158 	case TCP_REC_ABC_VAL:
23159 		RACK_OPTS_INC(tcp_rec_abc_val);
23160 		if (optval > 0)
23161 			rack->r_use_labc_for_rec = 1;
23162 		else
23163 			rack->r_use_labc_for_rec = 0;
23164 		break;
23165 	case TCP_RACK_ABC_VAL:
23166 		RACK_OPTS_INC(tcp_rack_abc_val);
23167 		if ((optval > 0) && (optval < 255))
23168 			rack->rc_labc = optval;
23169 		else
23170 			error = EINVAL;
23171 		break;
23172 	case TCP_HDWR_UP_ONLY:
23173 		RACK_OPTS_INC(tcp_pacing_up_only);
23174 		if (optval)
23175 			rack->r_up_only = 1;
23176 		else
23177 			rack->r_up_only = 0;
23178 		break;
23179 	case TCP_PACING_RATE_CAP:
23180 		RACK_OPTS_INC(tcp_pacing_rate_cap);
23181 		rack->r_ctl.bw_rate_cap = loptval;
23182 		break;
23183 	case TCP_HYBRID_PACING:
23184 		if (hybrid == NULL) {
23185 			error = EINVAL;
23186 			break;
23187 		}
23188 		error = process_hybrid_pacing(rack, hybrid);
23189 		break;
23190 	case TCP_RACK_PROFILE:
23191 		RACK_OPTS_INC(tcp_profile);
23192 		error = rack_set_profile(rack, optval);
23193 		break;
23194 	case TCP_USE_CMP_ACKS:
23195 		RACK_OPTS_INC(tcp_use_cmp_acks);
23196 		if ((optval == 0) && (tp->t_flags2 & TF2_MBUF_ACKCMP)) {
23197 			/* You can't turn it off once its on! */
23198 			error = EINVAL;
23199 		} else if ((optval == 1) && (rack->r_use_cmp_ack == 0)) {
23200 			rack->r_use_cmp_ack = 1;
23201 			rack->r_mbuf_queue = 1;
23202 			tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
23203 		}
23204 		if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
23205 			tp->t_flags2 |= TF2_MBUF_ACKCMP;
23206 		break;
23207 	case TCP_SHARED_CWND_TIME_LIMIT:
23208 		RACK_OPTS_INC(tcp_lscwnd);
23209 		if (optval)
23210 			rack->r_limit_scw = 1;
23211 		else
23212 			rack->r_limit_scw = 0;
23213 		break;
23214 	case TCP_RACK_DGP_IN_REC:
23215 		RACK_OPTS_INC(tcp_dgp_in_rec);
23216 		if (optval)
23217 			rack->r_ctl.full_dgp_in_rec = 1;
23218 		else
23219 			rack->r_ctl.full_dgp_in_rec = 0;
23220 		break;
23221 	case TCP_RXT_CLAMP:
23222 		RACK_OPTS_INC(tcp_rxt_clamp);
23223 		rack_translate_clamp_value(rack, optval);
23224 		break;
23225  	case TCP_RACK_PACE_TO_FILL:
23226 		RACK_OPTS_INC(tcp_fillcw);
23227 		if (optval == 0)
23228 			rack->rc_pace_to_cwnd = 0;
23229 		else {
23230 			rack->rc_pace_to_cwnd = 1;
23231 			if (optval > 1)
23232 				rack->r_fill_less_agg = 1;
23233 		}
23234 		if ((optval >= rack_gp_rtt_maxmul) &&
23235 		    rack_gp_rtt_maxmul &&
23236 		    (optval < 0xf)) {
23237 			rack->rc_pace_fill_if_rttin_range = 1;
23238 			rack->rtt_limit_mul = optval;
23239 		} else {
23240 			rack->rc_pace_fill_if_rttin_range = 0;
23241 			rack->rtt_limit_mul = 0;
23242 		}
23243 		break;
23244 	case TCP_RACK_NO_PUSH_AT_MAX:
23245 		RACK_OPTS_INC(tcp_npush);
23246 		if (optval == 0)
23247 			rack->r_ctl.rc_no_push_at_mrtt = 0;
23248 		else if (optval < 0xff)
23249 			rack->r_ctl.rc_no_push_at_mrtt = optval;
23250 		else
23251 			error = EINVAL;
23252 		break;
23253 	case TCP_SHARED_CWND_ENABLE:
23254 		RACK_OPTS_INC(tcp_rack_scwnd);
23255 		if (optval == 0)
23256 			rack->rack_enable_scwnd = 0;
23257 		else
23258 			rack->rack_enable_scwnd = 1;
23259 		break;
23260 	case TCP_RACK_MBUF_QUEUE:
23261 		/* Now do we use the LRO mbuf-queue feature */
23262 		RACK_OPTS_INC(tcp_rack_mbufq);
23263 		if (optval || rack->r_use_cmp_ack)
23264 			rack->r_mbuf_queue = 1;
23265 		else
23266 			rack->r_mbuf_queue = 0;
23267 		if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
23268 			tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
23269 		else
23270 			tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
23271 		break;
23272 	case TCP_RACK_NONRXT_CFG_RATE:
23273 		RACK_OPTS_INC(tcp_rack_cfg_rate);
23274 		if (optval == 0)
23275 			rack->rack_rec_nonrxt_use_cr = 0;
23276 		else
23277 			rack->rack_rec_nonrxt_use_cr = 1;
23278 		break;
23279 	case TCP_NO_PRR:
23280 		RACK_OPTS_INC(tcp_rack_noprr);
23281 		if (optval == 0)
23282 			rack->rack_no_prr = 0;
23283 		else if (optval == 1)
23284 			rack->rack_no_prr = 1;
23285 		else if (optval == 2)
23286 			rack->no_prr_addback = 1;
23287 		else
23288 			error = EINVAL;
23289 		break;
23290 	case TCP_TIMELY_DYN_ADJ:
23291 		RACK_OPTS_INC(tcp_timely_dyn);
23292 		if (optval == 0)
23293 			rack->rc_gp_dyn_mul = 0;
23294 		else {
23295 			rack->rc_gp_dyn_mul = 1;
23296 			if (optval >= 100) {
23297 				/*
23298 				 * If the user sets something 100 or more
23299 				 * its the gp_ca value.
23300 				 */
23301 				rack->r_ctl.rack_per_of_gp_ca  = optval;
23302 			}
23303 		}
23304 		break;
23305 	case TCP_RACK_DO_DETECTION:
23306 		RACK_OPTS_INC(tcp_rack_do_detection);
23307 		if (optval == 0)
23308 			rack->do_detection = 0;
23309 		else
23310 			rack->do_detection = 1;
23311 		break;
23312 	case TCP_RACK_TLP_USE:
23313 		if ((optval < TLP_USE_ID) || (optval > TLP_USE_TWO_TWO)) {
23314 			error = EINVAL;
23315 			break;
23316 		}
23317 		RACK_OPTS_INC(tcp_tlp_use);
23318 		rack->rack_tlp_threshold_use = optval;
23319 		break;
23320 	case TCP_RACK_TLP_REDUCE:
23321 		/* RACK TLP cwnd reduction (bool) */
23322 		RACK_OPTS_INC(tcp_rack_tlp_reduce);
23323 		rack->r_ctl.rc_tlp_cwnd_reduce = optval;
23324 		break;
23325 		/*  Pacing related ones */
23326 	case TCP_RACK_PACE_ALWAYS:
23327 		/*
23328 		 * zero is old rack method, 1 is new
23329 		 * method using a pacing rate.
23330 		 */
23331 		RACK_OPTS_INC(tcp_rack_pace_always);
23332 		if (optval > 0) {
23333 			if (rack->rc_always_pace) {
23334 				error = EALREADY;
23335 				break;
23336 			} else if (tcp_can_enable_pacing()) {
23337 				rack->rc_always_pace = 1;
23338 				if (rack->rack_hibeta)
23339 					rack_set_cc_pacing(rack);
23340 			}
23341 			else {
23342 				error = ENOSPC;
23343 				break;
23344 			}
23345 		} else {
23346 			if (rack->rc_always_pace) {
23347 				tcp_decrement_paced_conn();
23348 				rack->rc_always_pace = 0;
23349 				rack_undo_cc_pacing(rack);
23350 			}
23351 		}
23352 		if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
23353 			tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
23354 		else
23355 			tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
23356 		/* A rate may be set irate or other, if so set seg size */
23357 		rack_update_seg(rack);
23358 		break;
23359 	case TCP_BBR_RACK_INIT_RATE:
23360 		RACK_OPTS_INC(tcp_initial_rate);
23361 		val = optval;
23362 		/* Change from kbits per second to bytes per second */
23363 		val *= 1000;
23364 		val /= 8;
23365 		rack->r_ctl.init_rate = val;
23366 		if (rack->rc_init_win != rack_default_init_window) {
23367 			uint32_t win, snt;
23368 
23369 			/*
23370 			 * Options don't always get applied
23371 			 * in the order you think. So in order
23372 			 * to assure we update a cwnd we need
23373 			 * to check and see if we are still
23374 			 * where we should raise the cwnd.
23375 			 */
23376 			win = rc_init_window(rack);
23377 			if (SEQ_GT(tp->snd_max, tp->iss))
23378 				snt = tp->snd_max - tp->iss;
23379 			else
23380 				snt = 0;
23381 			if ((snt < win) &&
23382 			    (tp->snd_cwnd < win))
23383 				tp->snd_cwnd = win;
23384 		}
23385 		if (rack->rc_always_pace)
23386 			rack_update_seg(rack);
23387 		break;
23388 	case TCP_BBR_IWINTSO:
23389 		RACK_OPTS_INC(tcp_initial_win);
23390 		if (optval && (optval <= 0xff)) {
23391 			uint32_t win, snt;
23392 
23393 			rack->rc_init_win = optval;
23394 			win = rc_init_window(rack);
23395 			if (SEQ_GT(tp->snd_max, tp->iss))
23396 				snt = tp->snd_max - tp->iss;
23397 			else
23398 				snt = 0;
23399 			if ((snt < win) &&
23400 			    (tp->t_srtt |
23401 			     rack->r_ctl.init_rate)) {
23402 				/*
23403 				 * We are not past the initial window
23404 				 * and we have some bases for pacing,
23405 				 * so we need to possibly adjust up
23406 				 * the cwnd. Note even if we don't set
23407 				 * the cwnd, its still ok to raise the rc_init_win
23408 				 * which can be used coming out of idle when we
23409 				 * would have a rate.
23410 				 */
23411 				if (tp->snd_cwnd < win)
23412 					tp->snd_cwnd = win;
23413 			}
23414 			if (rack->rc_always_pace)
23415 				rack_update_seg(rack);
23416 		} else
23417 			error = EINVAL;
23418 		break;
23419 	case TCP_RACK_FORCE_MSEG:
23420 		RACK_OPTS_INC(tcp_rack_force_max_seg);
23421 		if (optval)
23422 			rack->rc_force_max_seg = 1;
23423 		else
23424 			rack->rc_force_max_seg = 0;
23425 		break;
23426 	case TCP_RACK_PACE_MIN_SEG:
23427 		RACK_OPTS_INC(tcp_rack_min_seg);
23428 		rack->r_ctl.rc_user_set_min_segs = (0x0000ffff & optval);
23429 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
23430 		break;
23431 	case TCP_RACK_PACE_MAX_SEG:
23432 		/* Max segments size in a pace in bytes */
23433 		RACK_OPTS_INC(tcp_rack_max_seg);
23434 		if (optval <= MAX_USER_SET_SEG)
23435 			rack->rc_user_set_max_segs = optval;
23436 		else
23437 			rack->rc_user_set_max_segs = MAX_USER_SET_SEG;
23438 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
23439 		break;
23440 	case TCP_RACK_PACE_RATE_REC:
23441 		/* Set the fixed pacing rate in Bytes per second ca */
23442 		RACK_OPTS_INC(tcp_rack_pace_rate_rec);
23443 		rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
23444 		if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0)
23445 			rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
23446 		if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0)
23447 			rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
23448 		rack->use_fixed_rate = 1;
23449 		if (rack->rack_hibeta)
23450 			rack_set_cc_pacing(rack);
23451 		rack_log_pacing_delay_calc(rack,
23452 					   rack->r_ctl.rc_fixed_pacing_rate_ss,
23453 					   rack->r_ctl.rc_fixed_pacing_rate_ca,
23454 					   rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
23455 					   __LINE__, NULL,0);
23456 		break;
23457 
23458 	case TCP_RACK_PACE_RATE_SS:
23459 		/* Set the fixed pacing rate in Bytes per second ca */
23460 		RACK_OPTS_INC(tcp_rack_pace_rate_ss);
23461 		rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
23462 		if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0)
23463 			rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
23464 		if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0)
23465 			rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
23466 		rack->use_fixed_rate = 1;
23467 		if (rack->rack_hibeta)
23468 			rack_set_cc_pacing(rack);
23469 		rack_log_pacing_delay_calc(rack,
23470 					   rack->r_ctl.rc_fixed_pacing_rate_ss,
23471 					   rack->r_ctl.rc_fixed_pacing_rate_ca,
23472 					   rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
23473 					   __LINE__, NULL, 0);
23474 		break;
23475 
23476 	case TCP_RACK_PACE_RATE_CA:
23477 		/* Set the fixed pacing rate in Bytes per second ca */
23478 		RACK_OPTS_INC(tcp_rack_pace_rate_ca);
23479 		rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
23480 		if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0)
23481 			rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
23482 		if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0)
23483 			rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
23484 		rack->use_fixed_rate = 1;
23485 		if (rack->rack_hibeta)
23486 			rack_set_cc_pacing(rack);
23487 		rack_log_pacing_delay_calc(rack,
23488 					   rack->r_ctl.rc_fixed_pacing_rate_ss,
23489 					   rack->r_ctl.rc_fixed_pacing_rate_ca,
23490 					   rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
23491 					   __LINE__, NULL, 0);
23492 		break;
23493 	case TCP_RACK_GP_INCREASE_REC:
23494 		RACK_OPTS_INC(tcp_gp_inc_rec);
23495 		rack->r_ctl.rack_per_of_gp_rec = optval;
23496 		rack_log_pacing_delay_calc(rack,
23497 					   rack->r_ctl.rack_per_of_gp_ss,
23498 					   rack->r_ctl.rack_per_of_gp_ca,
23499 					   rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
23500 					   __LINE__, NULL, 0);
23501 		break;
23502 	case TCP_RACK_GP_INCREASE_CA:
23503 		RACK_OPTS_INC(tcp_gp_inc_ca);
23504 		ca = optval;
23505 		if (ca < 100) {
23506 			/*
23507 			 * We don't allow any reduction
23508 			 * over the GP b/w.
23509 			 */
23510 			error = EINVAL;
23511 			break;
23512 		}
23513 		rack->r_ctl.rack_per_of_gp_ca = ca;
23514 		rack_log_pacing_delay_calc(rack,
23515 					   rack->r_ctl.rack_per_of_gp_ss,
23516 					   rack->r_ctl.rack_per_of_gp_ca,
23517 					   rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
23518 					   __LINE__, NULL, 0);
23519 		break;
23520 	case TCP_RACK_GP_INCREASE_SS:
23521 		RACK_OPTS_INC(tcp_gp_inc_ss);
23522 		ss = optval;
23523 		if (ss < 100) {
23524 			/*
23525 			 * We don't allow any reduction
23526 			 * over the GP b/w.
23527 			 */
23528 			error = EINVAL;
23529 			break;
23530 		}
23531 		rack->r_ctl.rack_per_of_gp_ss = ss;
23532 		rack_log_pacing_delay_calc(rack,
23533 					   rack->r_ctl.rack_per_of_gp_ss,
23534 					   rack->r_ctl.rack_per_of_gp_ca,
23535 					   rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
23536 					   __LINE__, NULL, 0);
23537 		break;
23538 	case TCP_RACK_RR_CONF:
23539 		RACK_OPTS_INC(tcp_rack_rrr_no_conf_rate);
23540 		if (optval && optval <= 3)
23541 			rack->r_rr_config = optval;
23542 		else
23543 			rack->r_rr_config = 0;
23544 		break;
23545 	case TCP_PACING_DND:			/*  URL:dnd */
23546 		if (optval > 0)
23547 			rack->rc_pace_dnd = 1;
23548 		else
23549 			rack->rc_pace_dnd = 0;
23550 		break;
23551 	case TCP_HDWR_RATE_CAP:
23552 		RACK_OPTS_INC(tcp_hdwr_rate_cap);
23553 		if (optval) {
23554 			if (rack->r_rack_hw_rate_caps == 0)
23555 				rack->r_rack_hw_rate_caps = 1;
23556 			else
23557 				error = EALREADY;
23558 		} else {
23559 			rack->r_rack_hw_rate_caps = 0;
23560 		}
23561 		break;
23562 	case TCP_RACK_SPLIT_LIMIT:
23563 		RACK_OPTS_INC(tcp_split_limit);
23564 		rack->r_ctl.rc_split_limit = optval;
23565 		break;
23566 	case TCP_BBR_HDWR_PACE:
23567 		RACK_OPTS_INC(tcp_hdwr_pacing);
23568 		if (optval){
23569 			if (rack->rack_hdrw_pacing == 0) {
23570 				rack->rack_hdw_pace_ena = 1;
23571 				rack->rack_attempt_hdwr_pace = 0;
23572 			} else
23573 				error = EALREADY;
23574 		} else {
23575 			rack->rack_hdw_pace_ena = 0;
23576 #ifdef RATELIMIT
23577 			if (rack->r_ctl.crte != NULL) {
23578 				rack->rack_hdrw_pacing = 0;
23579 				rack->rack_attempt_hdwr_pace = 0;
23580 				tcp_rel_pacing_rate(rack->r_ctl.crte, tp);
23581 				rack->r_ctl.crte = NULL;
23582 			}
23583 #endif
23584 		}
23585 		break;
23586 		/*  End Pacing related ones */
23587 	case TCP_RACK_PRR_SENDALOT:
23588 		/* Allow PRR to send more than one seg */
23589 		RACK_OPTS_INC(tcp_rack_prr_sendalot);
23590 		rack->r_ctl.rc_prr_sendalot = optval;
23591 		break;
23592 	case TCP_RACK_MIN_TO:
23593 		/* Minimum time between rack t-o's in ms */
23594 		RACK_OPTS_INC(tcp_rack_min_to);
23595 		rack->r_ctl.rc_min_to = optval;
23596 		break;
23597 	case TCP_RACK_EARLY_SEG:
23598 		/* If early recovery max segments */
23599 		RACK_OPTS_INC(tcp_rack_early_seg);
23600 		rack->r_ctl.rc_early_recovery_segs = optval;
23601 		break;
23602 	case TCP_RACK_ENABLE_HYSTART:
23603 	{
23604 		if (optval) {
23605 			tp->t_ccv.flags |= CCF_HYSTART_ALLOWED;
23606 			if (rack_do_hystart > RACK_HYSTART_ON)
23607 				tp->t_ccv.flags |= CCF_HYSTART_CAN_SH_CWND;
23608 			if (rack_do_hystart > RACK_HYSTART_ON_W_SC)
23609 				tp->t_ccv.flags |= CCF_HYSTART_CONS_SSTH;
23610 		} else {
23611 			tp->t_ccv.flags &= ~(CCF_HYSTART_ALLOWED|CCF_HYSTART_CAN_SH_CWND|CCF_HYSTART_CONS_SSTH);
23612 		}
23613 	}
23614 	break;
23615 	case TCP_RACK_REORD_THRESH:
23616 		/* RACK reorder threshold (shift amount) */
23617 		RACK_OPTS_INC(tcp_rack_reord_thresh);
23618 		if ((optval > 0) && (optval < 31))
23619 			rack->r_ctl.rc_reorder_shift = optval;
23620 		else
23621 			error = EINVAL;
23622 		break;
23623 	case TCP_RACK_REORD_FADE:
23624 		/* Does reordering fade after ms time */
23625 		RACK_OPTS_INC(tcp_rack_reord_fade);
23626 		rack->r_ctl.rc_reorder_fade = optval;
23627 		break;
23628 	case TCP_RACK_TLP_THRESH:
23629 		/* RACK TLP theshold i.e. srtt+(srtt/N) */
23630 		RACK_OPTS_INC(tcp_rack_tlp_thresh);
23631 		if (optval)
23632 			rack->r_ctl.rc_tlp_threshold = optval;
23633 		else
23634 			error = EINVAL;
23635 		break;
23636 	case TCP_BBR_USE_RACK_RR:
23637 		RACK_OPTS_INC(tcp_rack_rr);
23638 		if (optval)
23639 			rack->use_rack_rr = 1;
23640 		else
23641 			rack->use_rack_rr = 0;
23642 		break;
23643 	case TCP_RACK_PKT_DELAY:
23644 		/* RACK added ms i.e. rack-rtt + reord + N */
23645 		RACK_OPTS_INC(tcp_rack_pkt_delay);
23646 		rack->r_ctl.rc_pkt_delay = optval;
23647 		break;
23648 	case TCP_DELACK:
23649 		RACK_OPTS_INC(tcp_rack_delayed_ack);
23650 		if (optval == 0)
23651 			tp->t_delayed_ack = 0;
23652 		else
23653 			tp->t_delayed_ack = 1;
23654 		if (tp->t_flags & TF_DELACK) {
23655 			tp->t_flags &= ~TF_DELACK;
23656 			tp->t_flags |= TF_ACKNOW;
23657 			NET_EPOCH_ENTER(et);
23658 			rack_output(tp);
23659 			NET_EPOCH_EXIT(et);
23660 		}
23661 		break;
23662 
23663 	case TCP_BBR_RACK_RTT_USE:
23664 		RACK_OPTS_INC(tcp_rack_rtt_use);
23665 		if ((optval != USE_RTT_HIGH) &&
23666 		    (optval != USE_RTT_LOW) &&
23667 		    (optval != USE_RTT_AVG))
23668 			error = EINVAL;
23669 		else
23670 			rack->r_ctl.rc_rate_sample_method = optval;
23671 		break;
23672 	case TCP_DATA_AFTER_CLOSE:
23673 		RACK_OPTS_INC(tcp_data_after_close);
23674 		if (optval)
23675 			rack->rc_allow_data_af_clo = 1;
23676 		else
23677 			rack->rc_allow_data_af_clo = 0;
23678 		break;
23679 	default:
23680 		break;
23681 	}
23682 	tcp_log_socket_option(tp, sopt_name, optval, error);
23683 	return (error);
23684 }
23685 
23686 
23687 static void
23688 rack_apply_deferred_options(struct tcp_rack *rack)
23689 {
23690 	struct deferred_opt_list *dol, *sdol;
23691 	uint32_t s_optval;
23692 
23693 	TAILQ_FOREACH_SAFE(dol, &rack->r_ctl.opt_list, next, sdol) {
23694 		TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next);
23695 		/* Disadvantage of deferal is you loose the error return */
23696 		s_optval = (uint32_t)dol->optval;
23697 		(void)rack_process_option(rack->rc_tp, rack, dol->optname, s_optval, dol->optval, NULL);
23698 		free(dol, M_TCPDO);
23699 	}
23700 }
23701 
23702 static void
23703 rack_hw_tls_change(struct tcpcb *tp, int chg)
23704 {
23705 	/* Update HW tls state */
23706 	struct tcp_rack *rack;
23707 
23708 	rack = (struct tcp_rack *)tp->t_fb_ptr;
23709 	if (chg)
23710 		rack->r_ctl.fsb.hw_tls = 1;
23711 	else
23712 		rack->r_ctl.fsb.hw_tls = 0;
23713 }
23714 
23715 static int
23716 rack_pru_options(struct tcpcb *tp, int flags)
23717 {
23718 	if (flags & PRUS_OOB)
23719 		return (EOPNOTSUPP);
23720 	return (0);
23721 }
23722 
23723 static bool
23724 rack_wake_check(struct tcpcb *tp)
23725 {
23726 	struct tcp_rack *rack;
23727 	struct timeval tv;
23728 	uint32_t cts;
23729 
23730 	rack = (struct tcp_rack *)tp->t_fb_ptr;
23731 	if (rack->r_ctl.rc_hpts_flags) {
23732 		cts = tcp_get_usecs(&tv);
23733 		if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == PACE_PKT_OUTPUT){
23734 			/*
23735 			 * Pacing timer is up, check if we are ready.
23736 			 */
23737 			if (TSTMP_GEQ(cts, rack->r_ctl.rc_last_output_to))
23738 				return (true);
23739 		} else if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) != 0) {
23740 			/*
23741 			 * A timer is up, check if we are ready.
23742 			 */
23743 			if (TSTMP_GEQ(cts, rack->r_ctl.rc_timer_exp))
23744 				return (true);
23745 		}
23746 	}
23747 	return (false);
23748 }
23749 
23750 static struct tcp_function_block __tcp_rack = {
23751 	.tfb_tcp_block_name = __XSTRING(STACKNAME),
23752 	.tfb_tcp_output = rack_output,
23753 	.tfb_do_queued_segments = ctf_do_queued_segments,
23754 	.tfb_do_segment_nounlock = rack_do_segment_nounlock,
23755 	.tfb_tcp_do_segment = rack_do_segment,
23756 	.tfb_tcp_ctloutput = rack_ctloutput,
23757 	.tfb_tcp_fb_init = rack_init,
23758 	.tfb_tcp_fb_fini = rack_fini,
23759 	.tfb_tcp_timer_stop_all = rack_stopall,
23760 	.tfb_tcp_rexmit_tmr = rack_remxt_tmr,
23761 	.tfb_tcp_handoff_ok = rack_handoff_ok,
23762 	.tfb_tcp_mtu_chg = rack_mtu_change,
23763 	.tfb_pru_options = rack_pru_options,
23764 	.tfb_hwtls_change = rack_hw_tls_change,
23765 	.tfb_chg_query = rack_chg_query,
23766 	.tfb_switch_failed = rack_switch_failed,
23767 	.tfb_early_wake_check = rack_wake_check,
23768 	.tfb_compute_pipe = rack_compute_pipe,
23769 	.tfb_flags = TCP_FUNC_OUTPUT_CANDROP,
23770 };
23771 
23772 /*
23773  * rack_ctloutput() must drop the inpcb lock before performing copyin on
23774  * socket option arguments.  When it re-acquires the lock after the copy, it
23775  * has to revalidate that the connection is still valid for the socket
23776  * option.
23777  */
23778 static int
23779 rack_set_sockopt(struct tcpcb *tp, struct sockopt *sopt)
23780 {
23781 	struct inpcb *inp = tptoinpcb(tp);
23782 #ifdef INET
23783 	struct ip *ip;
23784 #endif
23785 	struct tcp_rack *rack;
23786 	struct tcp_hybrid_req hybrid;
23787 	uint64_t loptval;
23788 	int32_t error = 0, optval;
23789 
23790 	rack = (struct tcp_rack *)tp->t_fb_ptr;
23791 	if (rack == NULL) {
23792 		INP_WUNLOCK(inp);
23793 		return (EINVAL);
23794 	}
23795 #ifdef INET
23796 	ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
23797 #endif
23798 
23799 	switch (sopt->sopt_level) {
23800 #ifdef INET6
23801 	case IPPROTO_IPV6:
23802 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
23803 		switch (sopt->sopt_name) {
23804 		case IPV6_USE_MIN_MTU:
23805 			tcp6_use_min_mtu(tp);
23806 			break;
23807 		}
23808 		INP_WUNLOCK(inp);
23809 		return (0);
23810 #endif
23811 #ifdef INET
23812 	case IPPROTO_IP:
23813 		switch (sopt->sopt_name) {
23814 		case IP_TOS:
23815 			/*
23816 			 * The DSCP codepoint has changed, update the fsb.
23817 			 */
23818 			ip->ip_tos = rack->rc_inp->inp_ip_tos;
23819 			break;
23820 		case IP_TTL:
23821 			/*
23822 			 * The TTL has changed, update the fsb.
23823 			 */
23824 			ip->ip_ttl = rack->rc_inp->inp_ip_ttl;
23825 			break;
23826 		}
23827 		INP_WUNLOCK(inp);
23828 		return (0);
23829 #endif
23830 #ifdef SO_PEERPRIO
23831 	case SOL_SOCKET:
23832 		switch (sopt->sopt_name) {
23833 		case SO_PEERPRIO:			/*  SC-URL:bs */
23834 			/* Already read in and sanity checked in sosetopt(). */
23835 			if (inp->inp_socket) {
23836 				rack->client_bufferlvl = inp->inp_socket->so_peerprio;
23837 				rack_client_buffer_level_set(rack);
23838 			}
23839 			break;
23840 		}
23841 		INP_WUNLOCK(inp);
23842 		return (0);
23843 #endif
23844 	case IPPROTO_TCP:
23845 		switch (sopt->sopt_name) {
23846 		case TCP_RACK_TLP_REDUCE:		/*  URL:tlp_reduce */
23847 		/*  Pacing related ones */
23848 		case TCP_RACK_PACE_ALWAYS:		/*  URL:pace_always */
23849 		case TCP_BBR_RACK_INIT_RATE:		/*  URL:irate */
23850 		case TCP_BBR_IWINTSO:			/*  URL:tso_iwin */
23851 		case TCP_RACK_PACE_MIN_SEG:		/*  URL:pace_min_seg */
23852 		case TCP_RACK_PACE_MAX_SEG:		/*  URL:pace_max_seg */
23853 		case TCP_RACK_FORCE_MSEG:		/*  URL:force_max_seg */
23854 		case TCP_RACK_PACE_RATE_CA:		/*  URL:pr_ca */
23855 		case TCP_RACK_PACE_RATE_SS:		/*  URL:pr_ss*/
23856 		case TCP_RACK_PACE_RATE_REC:		/*  URL:pr_rec */
23857 		case TCP_RACK_GP_INCREASE_CA:		/*  URL:gp_inc_ca */
23858 		case TCP_RACK_GP_INCREASE_SS:		/*  URL:gp_inc_ss */
23859 		case TCP_RACK_GP_INCREASE_REC:		/*  URL:gp_inc_rec */
23860 		case TCP_RACK_RR_CONF:			/*  URL:rrr_conf */
23861 		case TCP_BBR_HDWR_PACE:			/*  URL:hdwrpace */
23862 		case TCP_HDWR_RATE_CAP:			/*  URL:hdwrcap boolean */
23863 		case TCP_PACING_RATE_CAP:		/*  URL:cap  -- used by side-channel */
23864 		case TCP_HDWR_UP_ONLY:			/*  URL:uponly -- hardware pacing  boolean */
23865 		case TCP_RACK_PACING_BETA:		/*  URL:pacing_beta */
23866 		case TCP_RACK_PACING_BETA_ECN:		/*  URL:pacing_beta_ecn */
23867 		case TCP_RACK_PACE_TO_FILL:		/*  URL:fillcw */
23868 		case TCP_RACK_DGP_IN_REC:		/*  URL:dgpinrec */
23869 			/* End pacing related */
23870 		case TCP_RXT_CLAMP:			/*  URL:rxtclamp */
23871 		case TCP_DELACK:			/*  URL:delack (in base TCP i.e. tcp_hints along with cc etc ) */
23872 		case TCP_RACK_PRR_SENDALOT:		/*  URL:prr_sendalot */
23873 		case TCP_RACK_MIN_TO:			/*  URL:min_to */
23874 		case TCP_RACK_EARLY_SEG:		/*  URL:early_seg */
23875 		case TCP_RACK_REORD_THRESH:		/*  URL:reord_thresh */
23876 		case TCP_RACK_REORD_FADE:		/*  URL:reord_fade */
23877 		case TCP_RACK_TLP_THRESH:		/*  URL:tlp_thresh */
23878 		case TCP_RACK_PKT_DELAY:		/*  URL:pkt_delay */
23879 		case TCP_RACK_TLP_USE:			/*  URL:tlp_use */
23880 		case TCP_BBR_RACK_RTT_USE:		/*  URL:rttuse */
23881 		case TCP_BBR_USE_RACK_RR:		/*  URL:rackrr */
23882 		case TCP_RACK_DO_DETECTION:		/*  URL:detect */
23883 		case TCP_NO_PRR:			/*  URL:noprr */
23884 		case TCP_TIMELY_DYN_ADJ:      		/*  URL:dynamic */
23885 		case TCP_DATA_AFTER_CLOSE:		/*  no URL */
23886 		case TCP_RACK_NONRXT_CFG_RATE:		/*  URL:nonrxtcr */
23887 		case TCP_SHARED_CWND_ENABLE:		/*  URL:scwnd */
23888 		case TCP_RACK_MBUF_QUEUE:		/*  URL:mqueue */
23889 		case TCP_RACK_NO_PUSH_AT_MAX:		/*  URL:npush */
23890 		case TCP_SHARED_CWND_TIME_LIMIT:	/*  URL:lscwnd */
23891 		case TCP_RACK_PROFILE:			/*  URL:profile */
23892 		case TCP_HYBRID_PACING:			/*  URL:hybrid */
23893 		case TCP_USE_CMP_ACKS:			/*  URL:cmpack */
23894 		case TCP_RACK_ABC_VAL:			/*  URL:labc */
23895 		case TCP_REC_ABC_VAL:			/*  URL:reclabc */
23896 		case TCP_RACK_MEASURE_CNT:		/*  URL:measurecnt */
23897 		case TCP_DEFER_OPTIONS:			/*  URL:defer */
23898 		case TCP_RACK_DSACK_OPT:		/*  URL:dsack */
23899 		case TCP_RACK_TIMER_SLOP:		/*  URL:timer_slop */
23900 		case TCP_RACK_ENABLE_HYSTART:		/*  URL:hystart */
23901 		case TCP_RACK_SET_RXT_OPTIONS:		/*  URL:rxtsz */
23902 		case TCP_RACK_HI_BETA:			/*  URL:hibeta */
23903 		case TCP_RACK_SPLIT_LIMIT:		/*  URL:split */
23904 		case TCP_RACK_PACING_DIVISOR:		/*  URL:divisor */
23905 		case TCP_PACING_DND:			/*  URL:dnd */
23906 			goto process_opt;
23907 			break;
23908 		default:
23909 			/* Filter off all unknown options to the base stack */
23910 			return (tcp_default_ctloutput(tp, sopt));
23911 			break;
23912 		}
23913 
23914 	default:
23915 		INP_WUNLOCK(inp);
23916 		return (0);
23917 	}
23918 process_opt:
23919 	INP_WUNLOCK(inp);
23920 	if (sopt->sopt_name == TCP_PACING_RATE_CAP) {
23921 		error = sooptcopyin(sopt, &loptval, sizeof(loptval), sizeof(loptval));
23922 		/*
23923 		 * We truncate it down to 32 bits for the socket-option trace this
23924 		 * means rates > 34Gbps won't show right, but thats probably ok.
23925 		 */
23926 		optval = (uint32_t)loptval;
23927 	} else if (sopt->sopt_name == TCP_HYBRID_PACING) {
23928 		error = sooptcopyin(sopt, &hybrid, sizeof(hybrid), sizeof(hybrid));
23929 	} else {
23930 		error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
23931 		/* Save it in 64 bit form too */
23932 		loptval = optval;
23933 	}
23934 	if (error)
23935 		return (error);
23936 	INP_WLOCK(inp);
23937 	if (tp->t_fb != &__tcp_rack) {
23938 		INP_WUNLOCK(inp);
23939 		return (ENOPROTOOPT);
23940 	}
23941 	if (rack->defer_options && (rack->gp_ready == 0) &&
23942 	    (sopt->sopt_name != TCP_DEFER_OPTIONS) &&
23943 	    (sopt->sopt_name != TCP_HYBRID_PACING) &&
23944 	    (sopt->sopt_name != TCP_RACK_PACING_BETA) &&
23945 	    (sopt->sopt_name != TCP_RACK_SET_RXT_OPTIONS) &&
23946 	    (sopt->sopt_name != TCP_RACK_PACING_BETA_ECN) &&
23947 	    (sopt->sopt_name != TCP_RACK_MEASURE_CNT)) {
23948 		/* Options are beind deferred */
23949 		if (rack_add_deferred_option(rack, sopt->sopt_name, loptval)) {
23950 			INP_WUNLOCK(inp);
23951 			return (0);
23952 		} else {
23953 			/* No memory to defer, fail */
23954 			INP_WUNLOCK(inp);
23955 			return (ENOMEM);
23956 		}
23957 	}
23958 	error = rack_process_option(tp, rack, sopt->sopt_name, optval, loptval, &hybrid);
23959 	INP_WUNLOCK(inp);
23960 	return (error);
23961 }
23962 
23963 static void
23964 rack_fill_info(struct tcpcb *tp, struct tcp_info *ti)
23965 {
23966 
23967 	INP_WLOCK_ASSERT(tptoinpcb(tp));
23968 	bzero(ti, sizeof(*ti));
23969 
23970 	ti->tcpi_state = tp->t_state;
23971 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
23972 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
23973 	if (tp->t_flags & TF_SACK_PERMIT)
23974 		ti->tcpi_options |= TCPI_OPT_SACK;
23975 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
23976 		ti->tcpi_options |= TCPI_OPT_WSCALE;
23977 		ti->tcpi_snd_wscale = tp->snd_scale;
23978 		ti->tcpi_rcv_wscale = tp->rcv_scale;
23979 	}
23980 	if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
23981 		ti->tcpi_options |= TCPI_OPT_ECN;
23982 	if (tp->t_flags & TF_FASTOPEN)
23983 		ti->tcpi_options |= TCPI_OPT_TFO;
23984 	/* still kept in ticks is t_rcvtime */
23985 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
23986 	/* Since we hold everything in precise useconds this is easy */
23987 	ti->tcpi_rtt = tp->t_srtt;
23988 	ti->tcpi_rttvar = tp->t_rttvar;
23989 	ti->tcpi_rto = tp->t_rxtcur;
23990 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
23991 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
23992 	/*
23993 	 * FreeBSD-specific extension fields for tcp_info.
23994 	 */
23995 	ti->tcpi_rcv_space = tp->rcv_wnd;
23996 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
23997 	ti->tcpi_snd_wnd = tp->snd_wnd;
23998 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
23999 	ti->tcpi_snd_nxt = tp->snd_nxt;
24000 	ti->tcpi_snd_mss = tp->t_maxseg;
24001 	ti->tcpi_rcv_mss = tp->t_maxseg;
24002 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
24003 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
24004 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
24005 	ti->tcpi_total_tlp = tp->t_sndtlppack;
24006 	ti->tcpi_total_tlp_bytes = tp->t_sndtlpbyte;
24007 #ifdef NETFLIX_STATS
24008 	memcpy(&ti->tcpi_rxsyninfo, &tp->t_rxsyninfo, sizeof(struct tcpsyninfo));
24009 #endif
24010 #ifdef TCP_OFFLOAD
24011 	if (tp->t_flags & TF_TOE) {
24012 		ti->tcpi_options |= TCPI_OPT_TOE;
24013 		tcp_offload_tcp_info(tp, ti);
24014 	}
24015 #endif
24016 }
24017 
24018 static int
24019 rack_get_sockopt(struct tcpcb *tp, struct sockopt *sopt)
24020 {
24021 	struct inpcb *inp = tptoinpcb(tp);
24022 	struct tcp_rack *rack;
24023 	int32_t error, optval;
24024 	uint64_t val, loptval;
24025 	struct	tcp_info ti;
24026 	/*
24027 	 * Because all our options are either boolean or an int, we can just
24028 	 * pull everything into optval and then unlock and copy. If we ever
24029 	 * add a option that is not a int, then this will have quite an
24030 	 * impact to this routine.
24031 	 */
24032 	error = 0;
24033 	rack = (struct tcp_rack *)tp->t_fb_ptr;
24034 	if (rack == NULL) {
24035 		INP_WUNLOCK(inp);
24036 		return (EINVAL);
24037 	}
24038 	switch (sopt->sopt_name) {
24039 	case TCP_INFO:
24040 		/* First get the info filled */
24041 		rack_fill_info(tp, &ti);
24042 		/* Fix up the rtt related fields if needed */
24043 		INP_WUNLOCK(inp);
24044 		error = sooptcopyout(sopt, &ti, sizeof ti);
24045 		return (error);
24046 	/*
24047 	 * Beta is the congestion control value for NewReno that influences how
24048 	 * much of a backoff happens when loss is detected. It is normally set
24049 	 * to 50 for 50% i.e. the cwnd is reduced to 50% of its previous value
24050 	 * when you exit recovery.
24051 	 */
24052 	case TCP_RACK_PACING_BETA:
24053 		if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0)
24054 			error = EINVAL;
24055 		else if (rack->rc_pacing_cc_set == 0)
24056 			optval = rack->r_ctl.rc_saved_beta.beta;
24057 		else {
24058 			/*
24059 			 * Reach out into the CC data and report back what
24060 			 * I have previously set. Yeah it looks hackish but
24061 			 * we don't want to report the saved values.
24062 			 */
24063 			if (tp->t_ccv.cc_data)
24064 				optval = ((struct newreno *)tp->t_ccv.cc_data)->beta;
24065 			else
24066 				error = EINVAL;
24067 		}
24068 		break;
24069 		/*
24070 		 * Beta_ecn is the congestion control value for NewReno that influences how
24071 		 * much of a backoff happens when a ECN mark is detected. It is normally set
24072 		 * to 80 for 80% i.e. the cwnd is reduced by 20% of its previous value when
24073 		 * you exit recovery. Note that classic ECN has a beta of 50, it is only
24074 		 * ABE Ecn that uses this "less" value, but we do too with pacing :)
24075 		 */
24076 
24077 	case TCP_RACK_PACING_BETA_ECN:
24078 		if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0)
24079 			error = EINVAL;
24080 		else if (rack->rc_pacing_cc_set == 0)
24081 			optval = rack->r_ctl.rc_saved_beta.beta_ecn;
24082 		else {
24083 			/*
24084 			 * Reach out into the CC data and report back what
24085 			 * I have previously set. Yeah it looks hackish but
24086 			 * we don't want to report the saved values.
24087 			 */
24088 			if (tp->t_ccv.cc_data)
24089 				optval = ((struct newreno *)tp->t_ccv.cc_data)->beta_ecn;
24090 			else
24091 				error = EINVAL;
24092 		}
24093 		break;
24094 	case TCP_RACK_DSACK_OPT:
24095 		optval = 0;
24096 		if (rack->rc_rack_tmr_std_based) {
24097 			optval |= 1;
24098 		}
24099 		if (rack->rc_rack_use_dsack) {
24100 			optval |= 2;
24101 		}
24102 		break;
24103  	case TCP_RACK_ENABLE_HYSTART:
24104 	{
24105 		if (tp->t_ccv.flags & CCF_HYSTART_ALLOWED) {
24106 			optval = RACK_HYSTART_ON;
24107 			if (tp->t_ccv.flags & CCF_HYSTART_CAN_SH_CWND)
24108 				optval = RACK_HYSTART_ON_W_SC;
24109 			if (tp->t_ccv.flags & CCF_HYSTART_CONS_SSTH)
24110 				optval = RACK_HYSTART_ON_W_SC_C;
24111 		} else {
24112 			optval = RACK_HYSTART_OFF;
24113 		}
24114 	}
24115 	break;
24116 	case TCP_RACK_DGP_IN_REC:
24117 		optval = rack->r_ctl.full_dgp_in_rec;
24118 		break;
24119 	case TCP_RACK_HI_BETA:
24120 		optval = rack->rack_hibeta;
24121 		break;
24122 	case TCP_RXT_CLAMP:
24123 		optval = rack->r_ctl.saved_rxt_clamp_val;
24124 		break;
24125 	case TCP_DEFER_OPTIONS:
24126 		optval = rack->defer_options;
24127 		break;
24128 	case TCP_RACK_MEASURE_CNT:
24129 		optval = rack->r_ctl.req_measurements;
24130 		break;
24131 	case TCP_REC_ABC_VAL:
24132 		optval = rack->r_use_labc_for_rec;
24133 		break;
24134 	case TCP_RACK_ABC_VAL:
24135 		optval = rack->rc_labc;
24136 		break;
24137 	case TCP_HDWR_UP_ONLY:
24138 		optval= rack->r_up_only;
24139 		break;
24140 	case TCP_PACING_RATE_CAP:
24141 		loptval = rack->r_ctl.bw_rate_cap;
24142 		break;
24143 	case TCP_RACK_PROFILE:
24144 		/* You cannot retrieve a profile, its write only */
24145 		error = EINVAL;
24146 		break;
24147 	case TCP_HYBRID_PACING:
24148 		/* You cannot retrieve hybrid pacing information, its write only */
24149 		error = EINVAL;
24150 		break;
24151 	case TCP_USE_CMP_ACKS:
24152 		optval = rack->r_use_cmp_ack;
24153 		break;
24154 	case TCP_RACK_PACE_TO_FILL:
24155 		optval = rack->rc_pace_to_cwnd;
24156 		if (optval && rack->r_fill_less_agg)
24157 			optval++;
24158 		break;
24159 	case TCP_RACK_NO_PUSH_AT_MAX:
24160 		optval = rack->r_ctl.rc_no_push_at_mrtt;
24161 		break;
24162 	case TCP_SHARED_CWND_ENABLE:
24163 		optval = rack->rack_enable_scwnd;
24164 		break;
24165 	case TCP_RACK_NONRXT_CFG_RATE:
24166 		optval = rack->rack_rec_nonrxt_use_cr;
24167 		break;
24168 	case TCP_NO_PRR:
24169 		if (rack->rack_no_prr  == 1)
24170 			optval = 1;
24171 		else if (rack->no_prr_addback == 1)
24172 			optval = 2;
24173 		else
24174 			optval = 0;
24175 		break;
24176 	case TCP_RACK_DO_DETECTION:
24177 		optval = rack->do_detection;
24178 		break;
24179 	case TCP_RACK_MBUF_QUEUE:
24180 		/* Now do we use the LRO mbuf-queue feature */
24181 		optval = rack->r_mbuf_queue;
24182 		break;
24183 	case TCP_TIMELY_DYN_ADJ:
24184 		optval = rack->rc_gp_dyn_mul;
24185 		break;
24186 	case TCP_BBR_IWINTSO:
24187 		optval = rack->rc_init_win;
24188 		break;
24189 	case TCP_RACK_TLP_REDUCE:
24190 		/* RACK TLP cwnd reduction (bool) */
24191 		optval = rack->r_ctl.rc_tlp_cwnd_reduce;
24192 		break;
24193 	case TCP_BBR_RACK_INIT_RATE:
24194 		val = rack->r_ctl.init_rate;
24195 		/* convert to kbits per sec */
24196 		val *= 8;
24197 		val /= 1000;
24198 		optval = (uint32_t)val;
24199 		break;
24200 	case TCP_RACK_FORCE_MSEG:
24201 		optval = rack->rc_force_max_seg;
24202 		break;
24203 	case TCP_RACK_PACE_MIN_SEG:
24204 		optval = rack->r_ctl.rc_user_set_min_segs;
24205 		break;
24206 	case TCP_RACK_PACE_MAX_SEG:
24207 		/* Max segments in a pace */
24208 		optval = rack->rc_user_set_max_segs;
24209 		break;
24210 	case TCP_RACK_PACE_ALWAYS:
24211 		/* Use the always pace method */
24212 		optval = rack->rc_always_pace;
24213 		break;
24214 	case TCP_RACK_PRR_SENDALOT:
24215 		/* Allow PRR to send more than one seg */
24216 		optval = rack->r_ctl.rc_prr_sendalot;
24217 		break;
24218 	case TCP_RACK_MIN_TO:
24219 		/* Minimum time between rack t-o's in ms */
24220 		optval = rack->r_ctl.rc_min_to;
24221 		break;
24222 	case TCP_RACK_SPLIT_LIMIT:
24223 		optval = rack->r_ctl.rc_split_limit;
24224 		break;
24225 	case TCP_RACK_EARLY_SEG:
24226 		/* If early recovery max segments */
24227 		optval = rack->r_ctl.rc_early_recovery_segs;
24228 		break;
24229 	case TCP_RACK_REORD_THRESH:
24230 		/* RACK reorder threshold (shift amount) */
24231 		optval = rack->r_ctl.rc_reorder_shift;
24232 		break;
24233 	case TCP_RACK_REORD_FADE:
24234 		/* Does reordering fade after ms time */
24235 		optval = rack->r_ctl.rc_reorder_fade;
24236 		break;
24237 	case TCP_BBR_USE_RACK_RR:
24238 		/* Do we use the rack cheat for rxt */
24239 		optval = rack->use_rack_rr;
24240 		break;
24241 	case TCP_RACK_RR_CONF:
24242 		optval = rack->r_rr_config;
24243 		break;
24244 	case TCP_HDWR_RATE_CAP:
24245 		optval = rack->r_rack_hw_rate_caps;
24246 		break;
24247 	case TCP_BBR_HDWR_PACE:
24248 		optval = rack->rack_hdw_pace_ena;
24249 		break;
24250 	case TCP_RACK_TLP_THRESH:
24251 		/* RACK TLP theshold i.e. srtt+(srtt/N) */
24252 		optval = rack->r_ctl.rc_tlp_threshold;
24253 		break;
24254 	case TCP_RACK_PKT_DELAY:
24255 		/* RACK added ms i.e. rack-rtt + reord + N */
24256 		optval = rack->r_ctl.rc_pkt_delay;
24257 		break;
24258 	case TCP_RACK_TLP_USE:
24259 		optval = rack->rack_tlp_threshold_use;
24260 		break;
24261 	case TCP_PACING_DND:
24262 		optval = rack->rc_pace_dnd;
24263 		break;
24264 	case TCP_RACK_PACE_RATE_CA:
24265 		optval = rack->r_ctl.rc_fixed_pacing_rate_ca;
24266 		break;
24267 	case TCP_RACK_PACE_RATE_SS:
24268 		optval = rack->r_ctl.rc_fixed_pacing_rate_ss;
24269 		break;
24270 	case TCP_RACK_PACE_RATE_REC:
24271 		optval = rack->r_ctl.rc_fixed_pacing_rate_rec;
24272 		break;
24273 	case TCP_RACK_GP_INCREASE_SS:
24274 		optval = rack->r_ctl.rack_per_of_gp_ca;
24275 		break;
24276 	case TCP_RACK_GP_INCREASE_CA:
24277 		optval = rack->r_ctl.rack_per_of_gp_ss;
24278 		break;
24279 	case TCP_RACK_PACING_DIVISOR:
24280 		optval = rack->r_ctl.pace_len_divisor;
24281 		break;
24282 	case TCP_BBR_RACK_RTT_USE:
24283 		optval = rack->r_ctl.rc_rate_sample_method;
24284 		break;
24285 	case TCP_DELACK:
24286 		optval = tp->t_delayed_ack;
24287 		break;
24288 	case TCP_DATA_AFTER_CLOSE:
24289 		optval = rack->rc_allow_data_af_clo;
24290 		break;
24291 	case TCP_SHARED_CWND_TIME_LIMIT:
24292 		optval = rack->r_limit_scw;
24293 		break;
24294 	case TCP_RACK_TIMER_SLOP:
24295 		optval = rack->r_ctl.timer_slop;
24296 		break;
24297 	default:
24298 		return (tcp_default_ctloutput(tp, sopt));
24299 		break;
24300 	}
24301 	INP_WUNLOCK(inp);
24302 	if (error == 0) {
24303 		if (TCP_PACING_RATE_CAP)
24304 			error = sooptcopyout(sopt, &loptval, sizeof loptval);
24305 		else
24306 			error = sooptcopyout(sopt, &optval, sizeof optval);
24307 	}
24308 	return (error);
24309 }
24310 
24311 static int
24312 rack_ctloutput(struct tcpcb *tp, struct sockopt *sopt)
24313 {
24314 	if (sopt->sopt_dir == SOPT_SET) {
24315 		return (rack_set_sockopt(tp, sopt));
24316 	} else if (sopt->sopt_dir == SOPT_GET) {
24317 		return (rack_get_sockopt(tp, sopt));
24318 	} else {
24319 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
24320 	}
24321 }
24322 
24323 static const char *rack_stack_names[] = {
24324 	__XSTRING(STACKNAME),
24325 #ifdef STACKALIAS
24326 	__XSTRING(STACKALIAS),
24327 #endif
24328 };
24329 
24330 static int
24331 rack_ctor(void *mem, int32_t size, void *arg, int32_t how)
24332 {
24333 	memset(mem, 0, size);
24334 	return (0);
24335 }
24336 
24337 static void
24338 rack_dtor(void *mem, int32_t size, void *arg)
24339 {
24340 
24341 }
24342 
24343 static bool rack_mod_inited = false;
24344 
24345 static int
24346 tcp_addrack(module_t mod, int32_t type, void *data)
24347 {
24348 	int32_t err = 0;
24349 	int num_stacks;
24350 
24351 	switch (type) {
24352 	case MOD_LOAD:
24353 		rack_zone = uma_zcreate(__XSTRING(MODNAME) "_map",
24354 		    sizeof(struct rack_sendmap),
24355 		    rack_ctor, rack_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
24356 
24357 		rack_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb",
24358 		    sizeof(struct tcp_rack),
24359 		    rack_ctor, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
24360 
24361 		sysctl_ctx_init(&rack_sysctl_ctx);
24362 		rack_sysctl_root = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
24363 		    SYSCTL_STATIC_CHILDREN(_net_inet_tcp),
24364 		    OID_AUTO,
24365 #ifdef STACKALIAS
24366 		    __XSTRING(STACKALIAS),
24367 #else
24368 		    __XSTRING(STACKNAME),
24369 #endif
24370 		    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
24371 		    "");
24372 		if (rack_sysctl_root == NULL) {
24373 			printf("Failed to add sysctl node\n");
24374 			err = EFAULT;
24375 			goto free_uma;
24376 		}
24377 		rack_init_sysctls();
24378 		num_stacks = nitems(rack_stack_names);
24379 		err = register_tcp_functions_as_names(&__tcp_rack, M_WAITOK,
24380 		    rack_stack_names, &num_stacks);
24381 		if (err) {
24382 			printf("Failed to register %s stack name for "
24383 			    "%s module\n", rack_stack_names[num_stacks],
24384 			    __XSTRING(MODNAME));
24385 			sysctl_ctx_free(&rack_sysctl_ctx);
24386 free_uma:
24387 			uma_zdestroy(rack_zone);
24388 			uma_zdestroy(rack_pcb_zone);
24389 			rack_counter_destroy();
24390 			printf("Failed to register rack module -- err:%d\n", err);
24391 			return (err);
24392 		}
24393 		tcp_lro_reg_mbufq();
24394 		rack_mod_inited = true;
24395 		break;
24396 	case MOD_QUIESCE:
24397 		err = deregister_tcp_functions(&__tcp_rack, true, false);
24398 		break;
24399 	case MOD_UNLOAD:
24400 		err = deregister_tcp_functions(&__tcp_rack, false, true);
24401 		if (err == EBUSY)
24402 			break;
24403 		if (rack_mod_inited) {
24404 			uma_zdestroy(rack_zone);
24405 			uma_zdestroy(rack_pcb_zone);
24406 			sysctl_ctx_free(&rack_sysctl_ctx);
24407 			rack_counter_destroy();
24408 			rack_mod_inited = false;
24409 		}
24410 		tcp_lro_dereg_mbufq();
24411 		err = 0;
24412 		break;
24413 	default:
24414 		return (EOPNOTSUPP);
24415 	}
24416 	return (err);
24417 }
24418 
24419 static moduledata_t tcp_rack = {
24420 	.name = __XSTRING(MODNAME),
24421 	.evhand = tcp_addrack,
24422 	.priv = 0
24423 };
24424 
24425 MODULE_VERSION(MODNAME, 1);
24426 DECLARE_MODULE(MODNAME, tcp_rack, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
24427 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1);
24428 
24429 #endif /* #if !defined(INET) && !defined(INET6) */
24430