xref: /freebsd/sys/netinet/tcp_stacks/rack.c (revision 7ee1bdd094d376fdc547e8ca33e472f1d37a7d79)
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 #define	M_TCPFSB	__CONCAT(M_TCPFSB, STACKNAME)
146 #define	M_TCPDO		__CONCAT(M_TCPDO, STACKNAME)
147 
148 MALLOC_DEFINE(M_TCPFSB, "tcp_fsb_" __XSTRING(STACKNAME), "TCP fast send block");
149 MALLOC_DEFINE(M_TCPDO, "tcp_do_" __XSTRING(STACKNAME), "TCP deferred options");
150 MALLOC_DEFINE(M_TCPPCM, "tcp_pcm_" __XSTRING(STACKNAME), "TCP PCM measurement information");
151 
152 struct sysctl_ctx_list rack_sysctl_ctx;
153 struct sysctl_oid *rack_sysctl_root;
154 
155 #define CUM_ACKED 1
156 #define SACKED 2
157 
158 /*
159  * The RACK module incorporates a number of
160  * TCP ideas that have been put out into the IETF
161  * over the last few years:
162  * - Matt Mathis's Rate Halving which slowly drops
163  *    the congestion window so that the ack clock can
164  *    be maintained during a recovery.
165  * - Yuchung Cheng's RACK TCP (for which its named) that
166  *    will stop us using the number of dup acks and instead
167  *    use time as the gage of when we retransmit.
168  * - Reorder Detection of RFC4737 and the Tail-Loss probe draft
169  *    of Dukkipati et.al.
170  * RACK depends on SACK, so if an endpoint arrives that
171  * cannot do SACK the state machine below will shuttle the
172  * connection back to using the "default" TCP stack that is
173  * in FreeBSD.
174  *
175  * To implement RACK the original TCP stack was first decomposed
176  * into a functional state machine with individual states
177  * for each of the possible TCP connection states. The do_segment
178  * functions role in life is to mandate the connection supports SACK
179  * initially and then assure that the RACK state matches the conenction
180  * state before calling the states do_segment function. Each
181  * state is simplified due to the fact that the original do_segment
182  * has been decomposed and we *know* what state we are in (no
183  * switches on the state) and all tests for SACK are gone. This
184  * greatly simplifies what each state does.
185  *
186  * TCP output is also over-written with a new version since it
187  * must maintain the new rack scoreboard.
188  *
189  */
190 static int32_t rack_tlp_thresh = 1;
191 static int32_t rack_tlp_limit = 2;	/* No more than 2 TLPs w-out new data */
192 static int32_t rack_tlp_use_greater = 1;
193 static int32_t rack_reorder_thresh = 2;
194 static int32_t rack_reorder_fade = 60000000;	/* 0 - never fade, def 60,000,000
195 						 * - 60 seconds */
196 static uint32_t rack_pcm_every_n_rounds = 100;
197 static uint32_t rack_pcm_blast = 0;
198 static uint32_t rack_pcm_is_enabled = 1;
199 static uint8_t rack_ssthresh_rest_rto_rec = 0; /* Do we restore ssthresh when we have rec -> rto -> rec */
200 
201 static uint32_t rack_gp_gain_req = 1200;		/* Amount percent wise required to gain to record a round has "gaining" */
202 static uint32_t rack_rnd_cnt_req = 0x10005;		/* Default number of rounds if we are below rack_gp_gain_req where we exit ss */
203 
204 
205 static int32_t rack_rxt_scoreboard_clear_thresh = 2;
206 static int32_t rack_dnd_default = 0;		/* For rr_conf = 3, what is the default for dnd */
207 static int32_t rack_rxt_controls = 0;
208 static int32_t rack_fill_cw_state = 0;
209 static uint8_t rack_req_measurements = 1;
210 /* Attack threshold detections */
211 static uint32_t rack_highest_sack_thresh_seen = 0;
212 static uint32_t rack_highest_move_thresh_seen = 0;
213 static uint32_t rack_merge_out_sacks_on_attack = 0;
214 static int32_t rack_enable_hw_pacing = 0; /* Due to CCSP keep it off by default */
215 static int32_t rack_hw_rate_caps = 0; /* 1; */
216 static int32_t rack_hw_rate_cap_per = 0;	/* 0 -- off  */
217 static int32_t rack_hw_rate_min = 0; /* 1500000;*/
218 static int32_t rack_hw_rate_to_low = 0; /* 1200000; */
219 static int32_t rack_hw_up_only = 0;
220 static int32_t rack_stats_gets_ms_rtt = 1;
221 static int32_t rack_prr_addbackmax = 2;
222 static int32_t rack_do_hystart = 0;
223 static int32_t rack_apply_rtt_with_reduced_conf = 0;
224 static int32_t rack_hibeta_setting = 0;
225 static int32_t rack_default_pacing_divisor = 250;
226 static uint16_t rack_pacing_min_seg = 0;
227 static int32_t rack_timely_off = 0;
228 
229 static uint32_t sad_seg_size_per = 800;	/* 80.0 % */
230 static int32_t rack_pkt_delay = 1000;
231 static int32_t rack_send_a_lot_in_prr = 1;
232 static int32_t rack_min_to = 1000;	/* Number of microsecond  min timeout */
233 static int32_t rack_verbose_logging = 0;
234 static int32_t rack_ignore_data_after_close = 1;
235 static int32_t rack_enable_shared_cwnd = 1;
236 static int32_t rack_use_cmp_acks = 1;
237 static int32_t rack_use_fsb = 1;
238 static int32_t rack_use_rfo = 1;
239 static int32_t rack_use_rsm_rfo = 1;
240 static int32_t rack_max_abc_post_recovery = 2;
241 static int32_t rack_client_low_buf = 0;
242 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 */
243 static int32_t rack_bw_multipler = 0;		/* Limit on fill cw's jump up to be this x gp_est */
244 #ifdef TCP_ACCOUNTING
245 static int32_t rack_tcp_accounting = 0;
246 #endif
247 static int32_t rack_limits_scwnd = 1;
248 static int32_t rack_enable_mqueue_for_nonpaced = 0;
249 static int32_t rack_hybrid_allow_set_maxseg = 0;
250 static int32_t rack_disable_prr = 0;
251 static int32_t use_rack_rr = 1;
252 static int32_t rack_non_rxt_use_cr = 0; /* does a non-rxt in recovery use the configured rate (ss/ca)? */
253 static int32_t rack_persist_min = 250000;	/* 250usec */
254 static int32_t rack_persist_max = 2000000;	/* 2 Second in usec's */
255 static int32_t rack_honors_hpts_min_to =  1;	/* Do we honor the hpts minimum time out for pacing timers */
256 static uint32_t rack_max_reduce = 10;		/* Percent we can reduce slot by */
257 static int32_t rack_sack_not_required = 1;	/* set to one to allow non-sack to use rack */
258 static int32_t rack_limit_time_with_srtt = 0;
259 static int32_t rack_autosndbuf_inc = 20;	/* In percentage form */
260 static int32_t rack_enobuf_hw_boost_mult = 0;	/* How many times the hw rate we boost slot using time_between */
261 static int32_t rack_enobuf_hw_max = 12000;	/* 12 ms in usecs */
262 static int32_t rack_enobuf_hw_min = 10000;	/* 10 ms in usecs */
263 static int32_t rack_hw_rwnd_factor = 2;		/* How many max_segs the rwnd must be before we hold off sending */
264 static int32_t rack_hw_check_queue = 0;		/* Do we always pre-check queue depth of a hw queue */
265 
266 /*
267  * Currently regular tcp has a rto_min of 30ms
268  * the backoff goes 12 times so that ends up
269  * being a total of 122.850 seconds before a
270  * connection is killed.
271  */
272 static uint32_t rack_def_data_window = 20;
273 static uint32_t rack_goal_bdp = 2;
274 static uint32_t rack_min_srtts = 1;
275 static uint32_t rack_min_measure_usec = 0;
276 static int32_t rack_tlp_min = 10000;	/* 10ms */
277 static int32_t rack_rto_min = 30000;	/* 30,000 usec same as main freebsd */
278 static int32_t rack_rto_max = 4000000;	/* 4 seconds in usec's */
279 static const int32_t rack_free_cache = 2;
280 static int32_t rack_hptsi_segments = 40;
281 static int32_t rack_rate_sample_method = USE_RTT_LOW;
282 static int32_t rack_pace_every_seg = 0;
283 static int32_t rack_delayed_ack_time = 40000;	/* 40ms in usecs */
284 static int32_t rack_slot_reduction = 4;
285 static int32_t rack_wma_divisor = 8;		/* For WMA calculation */
286 static int32_t rack_cwnd_block_ends_measure = 0;
287 static int32_t rack_rwnd_block_ends_measure = 0;
288 static int32_t rack_def_profile = 0;
289 
290 static int32_t rack_lower_cwnd_at_tlp = 0;
291 static int32_t rack_always_send_oldest = 0;
292 static int32_t rack_tlp_threshold_use = TLP_USE_TWO_ONE;
293 
294 static uint16_t rack_per_of_gp_ss = 250;	/* 250 % slow-start */
295 static uint16_t rack_per_of_gp_ca = 200;	/* 200 % congestion-avoidance */
296 static uint16_t rack_per_of_gp_rec = 200;	/* 200 % of bw */
297 
298 /* Probertt */
299 static uint16_t rack_per_of_gp_probertt = 60;	/* 60% of bw */
300 static uint16_t rack_per_of_gp_lowthresh = 40;	/* 40% is bottom */
301 static uint16_t rack_per_of_gp_probertt_reduce = 10; /* 10% reduction */
302 static uint16_t rack_atexit_prtt_hbp = 130;	/* Clamp to 130% on exit prtt if highly buffered path */
303 static uint16_t rack_atexit_prtt = 130;	/* Clamp to 100% on exit prtt if non highly buffered path */
304 
305 static uint32_t rack_max_drain_wait = 2;	/* How man gp srtt's before we give up draining */
306 static uint32_t rack_must_drain = 1;		/* How many GP srtt's we *must* wait */
307 static uint32_t rack_probertt_use_min_rtt_entry = 1;	/* Use the min to calculate the goal else gp_srtt */
308 static uint32_t rack_probertt_use_min_rtt_exit = 0;
309 static uint32_t rack_probe_rtt_sets_cwnd = 0;
310 static uint32_t rack_probe_rtt_safety_val = 2000000;	/* No more than 2 sec in probe-rtt */
311 static uint32_t rack_time_between_probertt = 9600000;	/* 9.6 sec in usecs */
312 static uint32_t rack_probertt_gpsrtt_cnt_mul = 0;	/* How many srtt periods does probe-rtt last top fraction */
313 static uint32_t rack_probertt_gpsrtt_cnt_div = 0;	/* How many srtt periods does probe-rtt last bottom fraction */
314 static uint32_t rack_min_probertt_hold = 40000;		/* Equal to delayed ack time */
315 static uint32_t rack_probertt_filter_life = 10000000;
316 static uint32_t rack_probertt_lower_within = 10;
317 static uint32_t rack_min_rtt_movement = 250000;	/* Must move at least 250ms (in microseconds)  to count as a lowering */
318 static int32_t rack_pace_one_seg = 0;		/* Shall we pace for less than 1.4Meg 1MSS at a time */
319 static int32_t rack_probertt_clear_is = 1;
320 static int32_t rack_max_drain_hbp = 1;		/* Extra drain times gpsrtt for highly buffered paths */
321 static int32_t rack_hbp_thresh = 3;		/* what is the divisor max_rtt/min_rtt to decided a hbp */
322 
323 /* Part of pacing */
324 static int32_t rack_max_per_above = 30;		/* When we go to increment stop if above 100+this% */
325 
326 /* Timely information:
327  *
328  * Here we have various control parameters on how
329  * timely may change the multiplier. rack_gain_p5_ub
330  * is associated with timely but not directly influencing
331  * the rate decision like the other variables. It controls
332  * the way fill-cw interacts with timely and caps how much
333  * timely can boost the fill-cw b/w.
334  *
335  * The other values are various boost/shrink numbers as well
336  * as potential caps when adjustments are made to the timely
337  * gain (returned by rack_get_output_gain(). Remember too that
338  * the gain returned can be overriden by other factors such as
339  * probeRTT as well as fixed-rate-pacing.
340  */
341 static int32_t rack_gain_p5_ub = 250;
342 static int32_t rack_gp_per_bw_mul_up = 2;	/* 2% */
343 static int32_t rack_gp_per_bw_mul_down = 4;	/* 4% */
344 static int32_t rack_gp_rtt_maxmul = 3;		/* 3 x maxmin */
345 static int32_t rack_gp_rtt_minmul = 1;		/* minrtt + (minrtt/mindiv) is lower rtt */
346 static int32_t rack_gp_rtt_mindiv = 4;		/* minrtt + (minrtt * minmul/mindiv) is lower rtt */
347 static int32_t rack_gp_decrease_per = 80;	/* Beta value of timely decrease (.8) = 80 */
348 static int32_t rack_gp_increase_per = 2;	/* 2% increase in multiplier */
349 static int32_t rack_per_lower_bound = 50;	/* Don't allow to drop below this multiplier */
350 static int32_t rack_per_upper_bound_ss = 0;	/* Don't allow SS to grow above this */
351 static int32_t rack_per_upper_bound_ca = 0;	/* Don't allow CA to grow above this */
352 static int32_t rack_do_dyn_mul = 0;		/* Are the rack gp multipliers dynamic */
353 static int32_t rack_gp_no_rec_chg = 1;		/* Prohibit recovery from reducing it's multiplier */
354 static int32_t rack_timely_dec_clear = 6;	/* Do we clear decrement count at a value (6)? */
355 static int32_t rack_timely_max_push_rise = 3;	/* One round of pushing */
356 static int32_t rack_timely_max_push_drop = 3;	/* Three round of pushing */
357 static int32_t rack_timely_min_segs = 4;	/* 4 segment minimum */
358 static int32_t rack_timely_no_stopping = 0;
359 static int32_t rack_down_raise_thresh = 100;
360 static int32_t rack_req_segs = 1;
361 static uint64_t rack_bw_rate_cap = 0;
362 static uint64_t rack_fillcw_bw_cap = 3750000;	/* Cap fillcw at 30Mbps */
363 
364 
365 /* Rack specific counters */
366 counter_u64_t rack_saw_enobuf;
367 counter_u64_t rack_saw_enobuf_hw;
368 counter_u64_t rack_saw_enetunreach;
369 counter_u64_t rack_persists_sends;
370 counter_u64_t rack_persists_acks;
371 counter_u64_t rack_persists_loss;
372 counter_u64_t rack_persists_lost_ends;
373 counter_u64_t rack_total_bytes;
374 #ifdef INVARIANTS
375 counter_u64_t rack_adjust_map_bw;
376 #endif
377 /* Tail loss probe counters */
378 counter_u64_t rack_tlp_tot;
379 counter_u64_t rack_tlp_newdata;
380 counter_u64_t rack_tlp_retran;
381 counter_u64_t rack_tlp_retran_bytes;
382 counter_u64_t rack_to_tot;
383 counter_u64_t rack_hot_alloc;
384 counter_u64_t rack_to_alloc;
385 counter_u64_t rack_to_alloc_hard;
386 counter_u64_t rack_to_alloc_emerg;
387 counter_u64_t rack_to_alloc_limited;
388 counter_u64_t rack_alloc_limited_conns;
389 counter_u64_t rack_split_limited;
390 counter_u64_t rack_rxt_clamps_cwnd;
391 counter_u64_t rack_rxt_clamps_cwnd_uniq;
392 
393 counter_u64_t rack_multi_single_eq;
394 counter_u64_t rack_proc_non_comp_ack;
395 
396 counter_u64_t rack_fto_send;
397 counter_u64_t rack_fto_rsm_send;
398 counter_u64_t rack_nfto_resend;
399 counter_u64_t rack_non_fto_send;
400 counter_u64_t rack_extended_rfo;
401 
402 counter_u64_t rack_sack_proc_all;
403 counter_u64_t rack_sack_proc_short;
404 counter_u64_t rack_sack_proc_restart;
405 counter_u64_t rack_sack_attacks_detected;
406 counter_u64_t rack_sack_attacks_reversed;
407 counter_u64_t rack_sack_attacks_suspect;
408 counter_u64_t rack_sack_used_next_merge;
409 counter_u64_t rack_sack_splits;
410 counter_u64_t rack_sack_used_prev_merge;
411 counter_u64_t rack_sack_skipped_acked;
412 counter_u64_t rack_ack_total;
413 counter_u64_t rack_express_sack;
414 counter_u64_t rack_sack_total;
415 counter_u64_t rack_move_none;
416 counter_u64_t rack_move_some;
417 
418 counter_u64_t rack_input_idle_reduces;
419 counter_u64_t rack_collapsed_win;
420 counter_u64_t rack_collapsed_win_seen;
421 counter_u64_t rack_collapsed_win_rxt;
422 counter_u64_t rack_collapsed_win_rxt_bytes;
423 counter_u64_t rack_try_scwnd;
424 counter_u64_t rack_hw_pace_init_fail;
425 counter_u64_t rack_hw_pace_lost;
426 
427 counter_u64_t rack_out_size[TCP_MSS_ACCT_SIZE];
428 counter_u64_t rack_opts_arry[RACK_OPTS_SIZE];
429 
430 
431 #define	RACK_REXMTVAL(tp) max(rack_rto_min, ((tp)->t_srtt + ((tp)->t_rttvar << 2)))
432 
433 #define	RACK_TCPT_RANGESET(tv, value, tvmin, tvmax, slop) do {	\
434 	(tv) = (value) + slop;	 \
435 	if ((u_long)(tv) < (u_long)(tvmin)) \
436 		(tv) = (tvmin); \
437 	if ((u_long)(tv) > (u_long)(tvmax)) \
438 		(tv) = (tvmax); \
439 } while (0)
440 
441 static void
442 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick,  int event, int line);
443 
444 static int
445 rack_process_ack(struct mbuf *m, struct tcphdr *th,
446     struct socket *so, struct tcpcb *tp, struct tcpopt *to,
447     uint32_t tiwin, int32_t tlen, int32_t * ofia, int32_t thflags, int32_t * ret_val, int32_t orig_tlen);
448 static int
449 rack_process_data(struct mbuf *m, struct tcphdr *th,
450     struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
451     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt);
452 static void
453 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack,
454    uint32_t th_ack, uint16_t nsegs, uint16_t type, int32_t recovery);
455 static struct rack_sendmap *rack_alloc(struct tcp_rack *rack);
456 static struct rack_sendmap *rack_alloc_limit(struct tcp_rack *rack,
457     uint8_t limit_type);
458 static struct rack_sendmap *
459 rack_check_recovery_mode(struct tcpcb *tp,
460     uint32_t tsused);
461 static uint32_t
462 rack_grab_rtt(struct tcpcb *tp, struct tcp_rack *rack);
463 static void
464 rack_cong_signal(struct tcpcb *tp,
465 		 uint32_t type, uint32_t ack, int );
466 static void rack_counter_destroy(void);
467 static int
468 rack_ctloutput(struct tcpcb *tp, struct sockopt *sopt);
469 static int32_t rack_ctor(void *mem, int32_t size, void *arg, int32_t how);
470 static void
471 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override);
472 static void
473 rack_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
474     int32_t drop_hdrlen, int32_t tlen, uint8_t iptos);
475 static void rack_dtor(void *mem, int32_t size, void *arg);
476 static void
477 rack_log_alt_to_to_cancel(struct tcp_rack *rack,
478     uint32_t flex1, uint32_t flex2,
479     uint32_t flex3, uint32_t flex4,
480     uint32_t flex5, uint32_t flex6,
481     uint16_t flex7, uint8_t mod);
482 
483 static void
484 rack_log_pacing_delay_calc(struct tcp_rack *rack, uint32_t len, uint32_t slot,
485    uint64_t bw_est, uint64_t bw, uint64_t len_time, int method, int line,
486    struct rack_sendmap *rsm, uint8_t quality);
487 static struct rack_sendmap *
488 rack_find_high_nonack(struct tcp_rack *rack,
489     struct rack_sendmap *rsm);
490 static struct rack_sendmap *rack_find_lowest_rsm(struct tcp_rack *rack);
491 static void rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm);
492 static void rack_fini(struct tcpcb *tp, int32_t tcb_is_purged);
493 static int rack_get_sockopt(struct tcpcb *tp, struct sockopt *sopt);
494 static void
495 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
496 			    tcp_seq th_ack, int line, uint8_t quality);
497 static void
498 rack_log_type_pacing_sizes(struct tcpcb *tp, struct tcp_rack *rack, uint32_t arg1, uint32_t arg2, uint32_t arg3, uint8_t frm);
499 
500 static uint32_t
501 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss);
502 static int32_t rack_handoff_ok(struct tcpcb *tp);
503 static int32_t rack_init(struct tcpcb *tp, void **ptr);
504 static void rack_init_sysctls(void);
505 
506 static void
507 rack_log_ack(struct tcpcb *tp, struct tcpopt *to,
508     struct tcphdr *th, int entered_rec, int dup_ack_struck,
509     int *dsack_seen, int *sacks_seen);
510 static void
511 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
512     uint32_t seq_out, uint16_t th_flags, int32_t err, uint64_t ts,
513     struct rack_sendmap *hintrsm, uint32_t add_flags, struct mbuf *s_mb, uint32_t s_moff, int hw_tls, int segsiz);
514 
515 static uint64_t rack_get_gp_est(struct tcp_rack *rack);
516 
517 
518 static void
519 rack_log_sack_passed(struct tcpcb *tp, struct tcp_rack *rack,
520     struct rack_sendmap *rsm, uint32_t cts);
521 static void rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm);
522 static int32_t rack_output(struct tcpcb *tp);
523 
524 static uint32_t
525 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack,
526     struct sackblk *sack, struct tcpopt *to, struct rack_sendmap **prsm,
527     uint32_t cts, uint32_t segsiz);
528 static void rack_post_recovery(struct tcpcb *tp, uint32_t th_seq);
529 static void rack_remxt_tmr(struct tcpcb *tp);
530 static int rack_set_sockopt(struct tcpcb *tp, struct sockopt *sopt);
531 static void rack_set_state(struct tcpcb *tp, struct tcp_rack *rack);
532 static int32_t rack_stopall(struct tcpcb *tp);
533 static void rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line);
534 static uint32_t
535 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack,
536     struct rack_sendmap *rsm, uint64_t ts, int32_t * lenp, uint32_t add_flag, int segsiz);
537 static void
538 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
539     struct rack_sendmap *rsm, uint64_t ts, uint32_t add_flag, int segsiz);
540 static int
541 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack,
542     struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack);
543 static int32_t tcp_addrack(module_t mod, int32_t type, void *data);
544 static int
545 rack_do_close_wait(struct mbuf *m, struct tcphdr *th,
546     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
547     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
548 
549 static int
550 rack_do_closing(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 thflags, int32_t nxt_pkt, uint8_t iptos);
553 static int
554 rack_do_established(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_fastnewdata(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 nxt_pkt, uint8_t iptos);
561 static int
562 rack_do_fin_wait_1(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_fin_wait_2(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_lastack(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 int
574 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th,
575     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
576     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
577 static int
578 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th,
579     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
580     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
581 static void rack_chk_req_and_hybrid_on_out(struct tcp_rack *rack, tcp_seq seq, uint32_t len, uint64_t cts);
582 struct rack_sendmap *
583 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack,
584     uint32_t tsused);
585 static void tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt,
586     uint32_t len, uint32_t us_tim, int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt);
587 static void
588      tcp_rack_partialack(struct tcpcb *tp);
589 static int
590 rack_set_profile(struct tcp_rack *rack, int prof);
591 static void
592 rack_apply_deferred_options(struct tcp_rack *rack);
593 
594 int32_t rack_clear_counter=0;
595 
596 static uint64_t
597 rack_get_lt_bw(struct tcp_rack *rack)
598 {
599 	struct timeval tv;
600 	uint64_t tim, bytes;
601 
602 	tim = rack->r_ctl.lt_bw_time;
603 	bytes = rack->r_ctl.lt_bw_bytes;
604 	if (rack->lt_bw_up) {
605 		/* Include all the current bytes too */
606 		microuptime(&tv);
607 		bytes += (rack->rc_tp->snd_una - rack->r_ctl.lt_seq);
608 		tim += (tcp_tv_to_lusectick(&tv) - rack->r_ctl.lt_timemark);
609 	}
610 	if ((bytes != 0) && (tim != 0))
611 		return ((bytes * (uint64_t)1000000) / tim);
612 	else
613 		return (0);
614 }
615 
616 static void
617 rack_swap_beta_values(struct tcp_rack *rack, uint8_t flex8)
618 {
619 	struct sockopt sopt;
620 	struct cc_newreno_opts opt;
621 	struct newreno old;
622 	struct tcpcb *tp;
623 	int error, failed = 0;
624 
625 	tp = rack->rc_tp;
626 	if (tp->t_cc == NULL) {
627 		/* Tcb is leaving */
628 		return;
629 	}
630 	rack->rc_pacing_cc_set = 1;
631 	if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0) {
632 		/* Not new-reno we can't play games with beta! */
633 		failed = 1;
634 		goto out;
635 
636 	}
637 	if (CC_ALGO(tp)->ctl_output == NULL)  {
638 		/* Huh, not using new-reno so no swaps.? */
639 		failed = 2;
640 		goto out;
641 	}
642 	old.newreno_flags = CC_NEWRENO_BETA_ECN_ENABLED;
643 	/* Get the current values out */
644 	sopt.sopt_valsize = sizeof(struct cc_newreno_opts);
645 	sopt.sopt_dir = SOPT_GET;
646 	opt.name = CC_NEWRENO_BETA;
647 	error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
648 	if (error)  {
649 		failed = 3;
650 		goto out;
651 	}
652 	old.beta = opt.val;
653 	opt.name = CC_NEWRENO_BETA_ECN;
654 	error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
655 	if (error)  {
656 		failed = 4;
657 		goto out;
658 	}
659 	old.beta_ecn = opt.val;
660 
661 	/* Now lets set in the values we have stored */
662 	sopt.sopt_dir = SOPT_SET;
663 	opt.name = CC_NEWRENO_BETA;
664 	opt.val = rack->r_ctl.rc_saved_beta.beta;
665 	error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
666 	if (error)  {
667 		failed = 5;
668 		goto out;
669 	}
670 	opt.name = CC_NEWRENO_BETA_ECN;
671 	opt.val = rack->r_ctl.rc_saved_beta.beta_ecn;
672 	error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
673 	if (error) {
674 		failed = 6;
675 		goto out;
676 	}
677 	/* Save off the values for restoral */
678 	memcpy(&rack->r_ctl.rc_saved_beta, &old, sizeof(struct newreno));
679 out:
680 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
681 		union tcp_log_stackspecific log;
682 		struct timeval tv;
683 		struct newreno *ptr;
684 
685 		ptr = ((struct newreno *)tp->t_ccv.cc_data);
686 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
687 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
688 		log.u_bbr.flex1 = ptr->beta;
689 		log.u_bbr.flex2 = ptr->beta_ecn;
690 		log.u_bbr.flex3 = ptr->newreno_flags;
691 		log.u_bbr.flex4 = rack->r_ctl.rc_saved_beta.beta;
692 		log.u_bbr.flex5 = rack->r_ctl.rc_saved_beta.beta_ecn;
693 		log.u_bbr.flex6 = failed;
694 		log.u_bbr.flex7 = rack->gp_ready;
695 		log.u_bbr.flex7 <<= 1;
696 		log.u_bbr.flex7 |= rack->use_fixed_rate;
697 		log.u_bbr.flex7 <<= 1;
698 		log.u_bbr.flex7 |= rack->rc_pacing_cc_set;
699 		log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
700 		log.u_bbr.flex8 = flex8;
701 		tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, error,
702 			       0, &log, false, NULL, NULL, 0, &tv);
703 	}
704 }
705 
706 static void
707 rack_set_cc_pacing(struct tcp_rack *rack)
708 {
709 	if (rack->rc_pacing_cc_set)
710 		return;
711 	/*
712 	 * Use the swap utility placing in 3 for flex8 to id a
713 	 * set of a new set of values.
714 	 */
715 	rack->rc_pacing_cc_set = 1;
716 	rack_swap_beta_values(rack, 3);
717 }
718 
719 static void
720 rack_undo_cc_pacing(struct tcp_rack *rack)
721 {
722 	if (rack->rc_pacing_cc_set == 0)
723 		return;
724 	/*
725 	 * Use the swap utility placing in 4 for flex8 to id a
726 	 * restoral of the old values.
727 	 */
728 	rack->rc_pacing_cc_set = 0;
729 	rack_swap_beta_values(rack, 4);
730 }
731 
732 static void
733 rack_remove_pacing(struct tcp_rack *rack)
734 {
735 	if (rack->rc_pacing_cc_set)
736 		rack_undo_cc_pacing(rack);
737 	if (rack->r_ctl.pacing_method & RACK_REG_PACING)
738 		tcp_decrement_paced_conn();
739 	if (rack->r_ctl.pacing_method & RACK_DGP_PACING)
740 		tcp_dec_dgp_pacing_cnt();
741 	rack->rc_always_pace = 0;
742 	rack->r_ctl.pacing_method = RACK_PACING_NONE;
743 	rack->dgp_on = 0;
744 	rack->rc_hybrid_mode = 0;
745 	rack->use_fixed_rate = 0;
746 }
747 
748 static void
749 rack_log_gpset(struct tcp_rack *rack, uint32_t seq_end, uint32_t ack_end_t,
750 	       uint32_t send_end_t, int line, uint8_t mode, struct rack_sendmap *rsm)
751 {
752 	if (tcp_bblogging_on(rack->rc_tp) && (rack_verbose_logging != 0)) {
753 		union tcp_log_stackspecific log;
754 		struct timeval tv;
755 
756 		memset(&log, 0, sizeof(log));
757 		log.u_bbr.flex1 = seq_end;
758 		log.u_bbr.flex2 = rack->rc_tp->gput_seq;
759 		log.u_bbr.flex3 = ack_end_t;
760 		log.u_bbr.flex4 = rack->rc_tp->gput_ts;
761 		log.u_bbr.flex5 = send_end_t;
762 		log.u_bbr.flex6 = rack->rc_tp->gput_ack;
763 		log.u_bbr.flex7 = mode;
764 		log.u_bbr.flex8 = 69;
765 		log.u_bbr.rttProp = rack->r_ctl.rc_gp_cumack_ts;
766 		log.u_bbr.delRate = rack->r_ctl.rc_gp_output_ts;
767 		log.u_bbr.pkts_out = line;
768 		log.u_bbr.cwnd_gain = rack->app_limited_needs_set;
769 		log.u_bbr.pkt_epoch = rack->r_ctl.rc_app_limited_cnt;
770 		log.u_bbr.epoch = rack->r_ctl.current_round;
771 		log.u_bbr.lt_epoch = rack->r_ctl.rc_considered_lost;
772 		if (rsm != NULL) {
773 			log.u_bbr.applimited = rsm->r_start;
774 			log.u_bbr.delivered = rsm->r_end;
775 			log.u_bbr.epoch = rsm->r_flags;
776 		}
777 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
778 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
779 		    &rack->rc_inp->inp_socket->so_rcv,
780 		    &rack->rc_inp->inp_socket->so_snd,
781 		    BBR_LOG_HPTSI_CALC, 0,
782 		    0, &log, false, &tv);
783 	}
784 }
785 
786 static int
787 sysctl_rack_clear(SYSCTL_HANDLER_ARGS)
788 {
789 	uint32_t stat;
790 	int32_t error;
791 
792 	error = SYSCTL_OUT(req, &rack_clear_counter, sizeof(uint32_t));
793 	if (error || req->newptr == NULL)
794 		return error;
795 
796 	error = SYSCTL_IN(req, &stat, sizeof(uint32_t));
797 	if (error)
798 		return (error);
799 	if (stat == 1) {
800 #ifdef INVARIANTS
801 		printf("Clearing RACK counters\n");
802 #endif
803 		counter_u64_zero(rack_tlp_tot);
804 		counter_u64_zero(rack_tlp_newdata);
805 		counter_u64_zero(rack_tlp_retran);
806 		counter_u64_zero(rack_tlp_retran_bytes);
807 		counter_u64_zero(rack_to_tot);
808 		counter_u64_zero(rack_saw_enobuf);
809 		counter_u64_zero(rack_saw_enobuf_hw);
810 		counter_u64_zero(rack_saw_enetunreach);
811 		counter_u64_zero(rack_persists_sends);
812 		counter_u64_zero(rack_total_bytes);
813 		counter_u64_zero(rack_persists_acks);
814 		counter_u64_zero(rack_persists_loss);
815 		counter_u64_zero(rack_persists_lost_ends);
816 #ifdef INVARIANTS
817 		counter_u64_zero(rack_adjust_map_bw);
818 #endif
819 		counter_u64_zero(rack_to_alloc_hard);
820 		counter_u64_zero(rack_to_alloc_emerg);
821 		counter_u64_zero(rack_sack_proc_all);
822 		counter_u64_zero(rack_fto_send);
823 		counter_u64_zero(rack_fto_rsm_send);
824 		counter_u64_zero(rack_extended_rfo);
825 		counter_u64_zero(rack_hw_pace_init_fail);
826 		counter_u64_zero(rack_hw_pace_lost);
827 		counter_u64_zero(rack_non_fto_send);
828 		counter_u64_zero(rack_nfto_resend);
829 		counter_u64_zero(rack_sack_proc_short);
830 		counter_u64_zero(rack_sack_proc_restart);
831 		counter_u64_zero(rack_to_alloc);
832 		counter_u64_zero(rack_to_alloc_limited);
833 		counter_u64_zero(rack_alloc_limited_conns);
834 		counter_u64_zero(rack_split_limited);
835 		counter_u64_zero(rack_rxt_clamps_cwnd);
836 		counter_u64_zero(rack_rxt_clamps_cwnd_uniq);
837 		counter_u64_zero(rack_multi_single_eq);
838 		counter_u64_zero(rack_proc_non_comp_ack);
839 		counter_u64_zero(rack_sack_attacks_detected);
840 		counter_u64_zero(rack_sack_attacks_reversed);
841 		counter_u64_zero(rack_sack_attacks_suspect);
842 		counter_u64_zero(rack_sack_used_next_merge);
843 		counter_u64_zero(rack_sack_used_prev_merge);
844 		counter_u64_zero(rack_sack_splits);
845 		counter_u64_zero(rack_sack_skipped_acked);
846 		counter_u64_zero(rack_ack_total);
847 		counter_u64_zero(rack_express_sack);
848 		counter_u64_zero(rack_sack_total);
849 		counter_u64_zero(rack_move_none);
850 		counter_u64_zero(rack_move_some);
851 		counter_u64_zero(rack_try_scwnd);
852 		counter_u64_zero(rack_collapsed_win);
853 		counter_u64_zero(rack_collapsed_win_rxt);
854 		counter_u64_zero(rack_collapsed_win_seen);
855 		counter_u64_zero(rack_collapsed_win_rxt_bytes);
856 	} else if (stat == 2) {
857 #ifdef INVARIANTS
858 		printf("Clearing RACK option array\n");
859 #endif
860 		COUNTER_ARRAY_ZERO(rack_opts_arry, RACK_OPTS_SIZE);
861 	} else if (stat == 3) {
862 		printf("Rack has no stats counters to clear (use 1 to clear all stats in sysctl node)\n");
863 	} else if (stat == 4) {
864 #ifdef INVARIANTS
865 		printf("Clearing RACK out size array\n");
866 #endif
867 		COUNTER_ARRAY_ZERO(rack_out_size, TCP_MSS_ACCT_SIZE);
868 	}
869 	rack_clear_counter = 0;
870 	return (0);
871 }
872 
873 static void
874 rack_init_sysctls(void)
875 {
876 	struct sysctl_oid *rack_counters;
877 	struct sysctl_oid *rack_attack;
878 	struct sysctl_oid *rack_pacing;
879 	struct sysctl_oid *rack_timely;
880 	struct sysctl_oid *rack_timers;
881 	struct sysctl_oid *rack_tlp;
882 	struct sysctl_oid *rack_misc;
883 	struct sysctl_oid *rack_features;
884 	struct sysctl_oid *rack_measure;
885 	struct sysctl_oid *rack_probertt;
886 	struct sysctl_oid *rack_hw_pacing;
887 
888 	rack_attack = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
889 	    SYSCTL_CHILDREN(rack_sysctl_root),
890 	    OID_AUTO,
891 	    "sack_attack",
892 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
893 	    "Rack Sack Attack Counters and Controls");
894 	rack_counters = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
895 	    SYSCTL_CHILDREN(rack_sysctl_root),
896 	    OID_AUTO,
897 	    "stats",
898 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
899 	    "Rack Counters");
900 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
901 	    SYSCTL_CHILDREN(rack_sysctl_root),
902 	    OID_AUTO, "rate_sample_method", CTLFLAG_RW,
903 	    &rack_rate_sample_method , USE_RTT_LOW,
904 	    "What method should we use for rate sampling 0=high, 1=low ");
905 	/* Probe rtt related controls */
906 	rack_probertt = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
907 	    SYSCTL_CHILDREN(rack_sysctl_root),
908 	    OID_AUTO,
909 	    "probertt",
910 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
911 	    "ProbeRTT related Controls");
912 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
913 	    SYSCTL_CHILDREN(rack_probertt),
914 	    OID_AUTO, "exit_per_hpb", CTLFLAG_RW,
915 	    &rack_atexit_prtt_hbp, 130,
916 	    "What percentage above goodput do we clamp CA/SS to at exit on high-BDP path 110%");
917 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
918 	    SYSCTL_CHILDREN(rack_probertt),
919 	    OID_AUTO, "exit_per_nonhpb", CTLFLAG_RW,
920 	    &rack_atexit_prtt, 130,
921 	    "What percentage above goodput do we clamp CA/SS to at exit on a non high-BDP path 100%");
922 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
923 	    SYSCTL_CHILDREN(rack_probertt),
924 	    OID_AUTO, "gp_per_mul", CTLFLAG_RW,
925 	    &rack_per_of_gp_probertt, 60,
926 	    "What percentage of goodput do we pace at in probertt");
927 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
928 	    SYSCTL_CHILDREN(rack_probertt),
929 	    OID_AUTO, "gp_per_reduce", CTLFLAG_RW,
930 	    &rack_per_of_gp_probertt_reduce, 10,
931 	    "What percentage of goodput do we reduce every gp_srtt");
932 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
933 	    SYSCTL_CHILDREN(rack_probertt),
934 	    OID_AUTO, "gp_per_low", CTLFLAG_RW,
935 	    &rack_per_of_gp_lowthresh, 40,
936 	    "What percentage of goodput do we allow the multiplier to fall to");
937 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
938 	    SYSCTL_CHILDREN(rack_probertt),
939 	    OID_AUTO, "time_between", CTLFLAG_RW,
940 	    & rack_time_between_probertt, 96000000,
941 	    "How many useconds between the lowest rtt falling must past before we enter probertt");
942 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
943 	    SYSCTL_CHILDREN(rack_probertt),
944 	    OID_AUTO, "safety", CTLFLAG_RW,
945 	    &rack_probe_rtt_safety_val, 2000000,
946 	    "If not zero, provides a maximum usecond that you can stay in probertt (2sec = 2000000)");
947 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
948 	    SYSCTL_CHILDREN(rack_probertt),
949 	    OID_AUTO, "sets_cwnd", CTLFLAG_RW,
950 	    &rack_probe_rtt_sets_cwnd, 0,
951 	    "Do we set the cwnd too (if always_lower is on)");
952 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
953 	    SYSCTL_CHILDREN(rack_probertt),
954 	    OID_AUTO, "maxdrainsrtts", CTLFLAG_RW,
955 	    &rack_max_drain_wait, 2,
956 	    "Maximum number of gp_srtt's to hold in drain waiting for flight to reach goal");
957 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
958 	    SYSCTL_CHILDREN(rack_probertt),
959 	    OID_AUTO, "mustdrainsrtts", CTLFLAG_RW,
960 	    &rack_must_drain, 1,
961 	    "We must drain this many gp_srtt's waiting for flight to reach goal");
962 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
963 	    SYSCTL_CHILDREN(rack_probertt),
964 	    OID_AUTO, "goal_use_min_entry", CTLFLAG_RW,
965 	    &rack_probertt_use_min_rtt_entry, 1,
966 	    "Should we use the min-rtt to calculate the goal rtt (else gp_srtt) at entry");
967 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
968 	    SYSCTL_CHILDREN(rack_probertt),
969 	    OID_AUTO, "goal_use_min_exit", CTLFLAG_RW,
970 	    &rack_probertt_use_min_rtt_exit, 0,
971 	    "How to set cwnd at exit, 0 - dynamic, 1 - use min-rtt, 2 - use curgprtt, 3 - entry gp-rtt");
972 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
973 	    SYSCTL_CHILDREN(rack_probertt),
974 	    OID_AUTO, "length_div", CTLFLAG_RW,
975 	    &rack_probertt_gpsrtt_cnt_div, 0,
976 	    "How many recent goodput srtt periods plus hold tim does probertt last (bottom of fraction)");
977 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
978 	    SYSCTL_CHILDREN(rack_probertt),
979 	    OID_AUTO, "length_mul", CTLFLAG_RW,
980 	    &rack_probertt_gpsrtt_cnt_mul, 0,
981 	    "How many recent goodput srtt periods plus hold tim does probertt last (top of fraction)");
982 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
983 	    SYSCTL_CHILDREN(rack_probertt),
984 	    OID_AUTO, "holdtim_at_target", CTLFLAG_RW,
985 	    &rack_min_probertt_hold, 200000,
986 	    "What is the minimum time we hold probertt at target");
987 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
988 	    SYSCTL_CHILDREN(rack_probertt),
989 	    OID_AUTO, "filter_life", CTLFLAG_RW,
990 	    &rack_probertt_filter_life, 10000000,
991 	    "What is the time for the filters life in useconds");
992 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
993 	    SYSCTL_CHILDREN(rack_probertt),
994 	    OID_AUTO, "lower_within", CTLFLAG_RW,
995 	    &rack_probertt_lower_within, 10,
996 	    "If the rtt goes lower within this percentage of the time, go into probe-rtt");
997 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
998 	    SYSCTL_CHILDREN(rack_probertt),
999 	    OID_AUTO, "must_move", CTLFLAG_RW,
1000 	    &rack_min_rtt_movement, 250,
1001 	    "How much is the minimum movement in rtt to count as a drop for probertt purposes");
1002 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1003 	    SYSCTL_CHILDREN(rack_probertt),
1004 	    OID_AUTO, "clear_is_cnts", CTLFLAG_RW,
1005 	    &rack_probertt_clear_is, 1,
1006 	    "Do we clear I/S counts on exiting probe-rtt");
1007 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1008 	    SYSCTL_CHILDREN(rack_probertt),
1009 	    OID_AUTO, "hbp_extra_drain", CTLFLAG_RW,
1010 	    &rack_max_drain_hbp, 1,
1011 	    "How many extra drain gpsrtt's do we get in highly buffered paths");
1012 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1013 	    SYSCTL_CHILDREN(rack_probertt),
1014 	    OID_AUTO, "hbp_threshold", CTLFLAG_RW,
1015 	    &rack_hbp_thresh, 3,
1016 	    "We are highly buffered if min_rtt_seen / max_rtt_seen > this-threshold");
1017 	/* Pacing related sysctls */
1018 	rack_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1019 	    SYSCTL_CHILDREN(rack_sysctl_root),
1020 	    OID_AUTO,
1021 	    "pacing",
1022 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1023 	    "Pacing related Controls");
1024 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1025 	    SYSCTL_CHILDREN(rack_pacing),
1026 	    OID_AUTO, "pcm_enabled", CTLFLAG_RW,
1027 	    &rack_pcm_is_enabled, 1,
1028 	    "Do we by default do PCM measurements?");
1029 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1030 	    SYSCTL_CHILDREN(rack_pacing),
1031 	    OID_AUTO, "pcm_rnds", CTLFLAG_RW,
1032 	    &rack_pcm_every_n_rounds, 100,
1033 	    "How many rounds before we need to do a PCM measurement");
1034 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1035 	    SYSCTL_CHILDREN(rack_pacing),
1036 	    OID_AUTO, "pcm_blast", CTLFLAG_RW,
1037 	    &rack_pcm_blast, 0,
1038 	    "Blast out the full cwnd/rwnd when doing a PCM measurement");
1039 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1040 	    SYSCTL_CHILDREN(rack_pacing),
1041 	    OID_AUTO, "rnd_gp_gain", CTLFLAG_RW,
1042 	    &rack_gp_gain_req, 1200,
1043 	    "How much do we have to increase the GP to record the round 1200 = 120.0");
1044 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1045 	    SYSCTL_CHILDREN(rack_pacing),
1046 	    OID_AUTO, "dgp_out_of_ss_at", CTLFLAG_RW,
1047 	    &rack_rnd_cnt_req, 0x10005,
1048 	    "How many rounds less than rnd_gp_gain will drop us out of SS");
1049 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1050 	    SYSCTL_CHILDREN(rack_pacing),
1051 	    OID_AUTO, "no_timely", CTLFLAG_RW,
1052 	    &rack_timely_off, 0,
1053 	    "Do we not use timely in DGP?");
1054 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1055 	    SYSCTL_CHILDREN(rack_pacing),
1056 	    OID_AUTO, "fillcw", CTLFLAG_RW,
1057 	    &rack_fill_cw_state, 0,
1058 	    "Enable fillcw on new connections (default=0 off)?");
1059 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1060 	    SYSCTL_CHILDREN(rack_pacing),
1061 	    OID_AUTO, "min_burst", CTLFLAG_RW,
1062 	    &rack_pacing_min_seg, 0,
1063 	    "What is the min burst size for pacing (0 disables)?");
1064 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1065 	    SYSCTL_CHILDREN(rack_pacing),
1066 	    OID_AUTO, "divisor", CTLFLAG_RW,
1067 	    &rack_default_pacing_divisor, 250,
1068 	    "What is the default divisor given to the rl code?");
1069 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1070 	    SYSCTL_CHILDREN(rack_pacing),
1071 	    OID_AUTO, "fillcw_max_mult", CTLFLAG_RW,
1072 	    &rack_bw_multipler, 0,
1073 	    "What is the limit multiplier of the current gp_est that fillcw can increase the b/w too, 200 == 200% (0 = off)?");
1074 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1075 	    SYSCTL_CHILDREN(rack_pacing),
1076 	    OID_AUTO, "max_pace_over", CTLFLAG_RW,
1077 	    &rack_max_per_above, 30,
1078 	    "What is the maximum allowable percentage that we can pace above (so 30 = 130% of our goal)");
1079 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1080 	    SYSCTL_CHILDREN(rack_pacing),
1081 	    OID_AUTO, "allow1mss", CTLFLAG_RW,
1082 	    &rack_pace_one_seg, 0,
1083 	    "Do we allow low b/w pacing of 1MSS instead of two (1.2Meg and less)?");
1084 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1085 	    SYSCTL_CHILDREN(rack_pacing),
1086 	    OID_AUTO, "limit_wsrtt", CTLFLAG_RW,
1087 	    &rack_limit_time_with_srtt, 0,
1088 	    "Do we limit pacing time based on srtt");
1089 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1090 	    SYSCTL_CHILDREN(rack_pacing),
1091 	    OID_AUTO, "gp_per_ss", CTLFLAG_RW,
1092 	    &rack_per_of_gp_ss, 250,
1093 	    "If non zero, what percentage of goodput to pace at in slow start");
1094 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1095 	    SYSCTL_CHILDREN(rack_pacing),
1096 	    OID_AUTO, "gp_per_ca", CTLFLAG_RW,
1097 	    &rack_per_of_gp_ca, 150,
1098 	    "If non zero, what percentage of goodput to pace at in congestion avoidance");
1099 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1100 	    SYSCTL_CHILDREN(rack_pacing),
1101 	    OID_AUTO, "gp_per_rec", CTLFLAG_RW,
1102 	    &rack_per_of_gp_rec, 200,
1103 	    "If non zero, what percentage of goodput to pace at in recovery");
1104 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1105 	    SYSCTL_CHILDREN(rack_pacing),
1106 	    OID_AUTO, "pace_max_seg", CTLFLAG_RW,
1107 	    &rack_hptsi_segments, 40,
1108 	    "What size is the max for TSO segments in pacing and burst mitigation");
1109 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1110 	    SYSCTL_CHILDREN(rack_pacing),
1111 	    OID_AUTO, "burst_reduces", CTLFLAG_RW,
1112 	    &rack_slot_reduction, 4,
1113 	    "When doing only burst mitigation what is the reduce divisor");
1114 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1115 	    SYSCTL_CHILDREN(rack_sysctl_root),
1116 	    OID_AUTO, "use_pacing", CTLFLAG_RW,
1117 	    &rack_pace_every_seg, 0,
1118 	    "If set we use pacing, if clear we use only the original burst mitigation");
1119 	SYSCTL_ADD_U64(&rack_sysctl_ctx,
1120 	    SYSCTL_CHILDREN(rack_pacing),
1121 	    OID_AUTO, "rate_cap", CTLFLAG_RW,
1122 	    &rack_bw_rate_cap, 0,
1123 	    "If set we apply this value to the absolute rate cap used by pacing");
1124 	SYSCTL_ADD_U64(&rack_sysctl_ctx,
1125 	    SYSCTL_CHILDREN(rack_pacing),
1126 	    OID_AUTO, "fillcw_cap", CTLFLAG_RW,
1127 	    &rack_fillcw_bw_cap, 3750000,
1128 	    "Do we have an absolute cap on the amount of b/w fillcw can specify (0 = no)?");
1129 	SYSCTL_ADD_U8(&rack_sysctl_ctx,
1130 	    SYSCTL_CHILDREN(rack_sysctl_root),
1131 	    OID_AUTO, "req_measure_cnt", CTLFLAG_RW,
1132 	    &rack_req_measurements, 1,
1133 	    "If doing dynamic pacing, how many measurements must be in before we start pacing?");
1134 	/* Hardware pacing */
1135 	rack_hw_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1136 	    SYSCTL_CHILDREN(rack_sysctl_root),
1137 	    OID_AUTO,
1138 	    "hdwr_pacing",
1139 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1140 	    "Pacing related Controls");
1141 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1142 	    SYSCTL_CHILDREN(rack_hw_pacing),
1143 	    OID_AUTO, "rwnd_factor", CTLFLAG_RW,
1144 	    &rack_hw_rwnd_factor, 2,
1145 	    "How many times does snd_wnd need to be bigger than pace_max_seg so we will hold off and get more acks?");
1146 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1147 	    SYSCTL_CHILDREN(rack_hw_pacing),
1148 	    OID_AUTO, "precheck", CTLFLAG_RW,
1149 	    &rack_hw_check_queue, 0,
1150 	    "Do we always precheck the hdwr pacing queue to avoid ENOBUF's?");
1151 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1152 	    SYSCTL_CHILDREN(rack_hw_pacing),
1153 	    OID_AUTO, "pace_enobuf_mult", CTLFLAG_RW,
1154 	    &rack_enobuf_hw_boost_mult, 0,
1155 	    "By how many time_betweens should we boost the pacing time if we see a ENOBUFS?");
1156 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1157 	    SYSCTL_CHILDREN(rack_hw_pacing),
1158 	    OID_AUTO, "pace_enobuf_max", CTLFLAG_RW,
1159 	    &rack_enobuf_hw_max, 2,
1160 	    "What is the max boost the pacing time if we see a ENOBUFS?");
1161 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1162 	    SYSCTL_CHILDREN(rack_hw_pacing),
1163 	    OID_AUTO, "pace_enobuf_min", CTLFLAG_RW,
1164 	    &rack_enobuf_hw_min, 2,
1165 	    "What is the min boost the pacing time if we see a ENOBUFS?");
1166 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1167 	    SYSCTL_CHILDREN(rack_hw_pacing),
1168 	    OID_AUTO, "enable", CTLFLAG_RW,
1169 	    &rack_enable_hw_pacing, 0,
1170 	    "Should RACK attempt to use hw pacing?");
1171 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1172 	    SYSCTL_CHILDREN(rack_hw_pacing),
1173 	    OID_AUTO, "rate_cap", CTLFLAG_RW,
1174 	    &rack_hw_rate_caps, 0,
1175 	    "Does the highest hardware pacing rate cap the rate we will send at??");
1176 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1177 	    SYSCTL_CHILDREN(rack_hw_pacing),
1178 	    OID_AUTO, "uncap_per", CTLFLAG_RW,
1179 	    &rack_hw_rate_cap_per, 0,
1180 	    "If you go over b/w by this amount you will be uncapped (0 = never)");
1181 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1182 	    SYSCTL_CHILDREN(rack_hw_pacing),
1183 	    OID_AUTO, "rate_min", CTLFLAG_RW,
1184 	    &rack_hw_rate_min, 0,
1185 	    "Do we need a minimum estimate of this many bytes per second in order to engage hw pacing?");
1186 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1187 	    SYSCTL_CHILDREN(rack_hw_pacing),
1188 	    OID_AUTO, "rate_to_low", CTLFLAG_RW,
1189 	    &rack_hw_rate_to_low, 0,
1190 	    "If we fall below this rate, dis-engage hw pacing?");
1191 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1192 	    SYSCTL_CHILDREN(rack_hw_pacing),
1193 	    OID_AUTO, "up_only", CTLFLAG_RW,
1194 	    &rack_hw_up_only, 0,
1195 	    "Do we allow hw pacing to lower the rate selected?");
1196 	rack_timely = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1197 	    SYSCTL_CHILDREN(rack_sysctl_root),
1198 	    OID_AUTO,
1199 	    "timely",
1200 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1201 	    "Rack Timely RTT Controls");
1202 	/* Timely based GP dynmics */
1203 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1204 	    SYSCTL_CHILDREN(rack_timely),
1205 	    OID_AUTO, "upper", CTLFLAG_RW,
1206 	    &rack_gp_per_bw_mul_up, 2,
1207 	    "Rack timely upper range for equal b/w (in percentage)");
1208 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1209 	    SYSCTL_CHILDREN(rack_timely),
1210 	    OID_AUTO, "lower", CTLFLAG_RW,
1211 	    &rack_gp_per_bw_mul_down, 4,
1212 	    "Rack timely lower range for equal b/w (in percentage)");
1213 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1214 	    SYSCTL_CHILDREN(rack_timely),
1215 	    OID_AUTO, "rtt_max_mul", CTLFLAG_RW,
1216 	    &rack_gp_rtt_maxmul, 3,
1217 	    "Rack timely multiplier of lowest rtt for rtt_max");
1218 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1219 	    SYSCTL_CHILDREN(rack_timely),
1220 	    OID_AUTO, "rtt_min_div", CTLFLAG_RW,
1221 	    &rack_gp_rtt_mindiv, 4,
1222 	    "Rack timely divisor used for rtt + (rtt * mul/divisor) for check for lower rtt");
1223 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1224 	    SYSCTL_CHILDREN(rack_timely),
1225 	    OID_AUTO, "rtt_min_mul", CTLFLAG_RW,
1226 	    &rack_gp_rtt_minmul, 1,
1227 	    "Rack timely multiplier used for rtt + (rtt * mul/divisor) for check for lower rtt");
1228 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1229 	    SYSCTL_CHILDREN(rack_timely),
1230 	    OID_AUTO, "decrease", CTLFLAG_RW,
1231 	    &rack_gp_decrease_per, 80,
1232 	    "Rack timely Beta value 80 = .8 (scaled by 100)");
1233 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1234 	    SYSCTL_CHILDREN(rack_timely),
1235 	    OID_AUTO, "increase", CTLFLAG_RW,
1236 	    &rack_gp_increase_per, 2,
1237 	    "Rack timely increase perentage of our GP multiplication factor");
1238 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1239 	    SYSCTL_CHILDREN(rack_timely),
1240 	    OID_AUTO, "lowerbound", CTLFLAG_RW,
1241 	    &rack_per_lower_bound, 50,
1242 	    "Rack timely lowest percentage we allow GP multiplier to fall to");
1243 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1244 	    SYSCTL_CHILDREN(rack_timely),
1245 	    OID_AUTO, "p5_upper", CTLFLAG_RW,
1246 	    &rack_gain_p5_ub, 250,
1247 	    "Profile 5 upper bound to timely gain");
1248 
1249 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1250 	    SYSCTL_CHILDREN(rack_timely),
1251 	    OID_AUTO, "upperboundss", CTLFLAG_RW,
1252 	    &rack_per_upper_bound_ss, 0,
1253 	    "Rack timely highest percentage we allow GP multiplier in SS to raise to (0 is no upperbound)");
1254 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1255 	    SYSCTL_CHILDREN(rack_timely),
1256 	    OID_AUTO, "upperboundca", CTLFLAG_RW,
1257 	    &rack_per_upper_bound_ca, 0,
1258 	    "Rack timely highest percentage we allow GP multiplier to CA raise to (0 is no upperbound)");
1259 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1260 	    SYSCTL_CHILDREN(rack_timely),
1261 	    OID_AUTO, "dynamicgp", CTLFLAG_RW,
1262 	    &rack_do_dyn_mul, 0,
1263 	    "Rack timely do we enable dynmaic timely goodput by default");
1264 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1265 	    SYSCTL_CHILDREN(rack_timely),
1266 	    OID_AUTO, "no_rec_red", CTLFLAG_RW,
1267 	    &rack_gp_no_rec_chg, 1,
1268 	    "Rack timely do we prohibit the recovery multiplier from being lowered");
1269 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1270 	    SYSCTL_CHILDREN(rack_timely),
1271 	    OID_AUTO, "red_clear_cnt", CTLFLAG_RW,
1272 	    &rack_timely_dec_clear, 6,
1273 	    "Rack timely what threshold do we count to before another boost during b/w decent");
1274 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1275 	    SYSCTL_CHILDREN(rack_timely),
1276 	    OID_AUTO, "max_push_rise", CTLFLAG_RW,
1277 	    &rack_timely_max_push_rise, 3,
1278 	    "Rack timely how many times do we push up with b/w increase");
1279 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1280 	    SYSCTL_CHILDREN(rack_timely),
1281 	    OID_AUTO, "max_push_drop", CTLFLAG_RW,
1282 	    &rack_timely_max_push_drop, 3,
1283 	    "Rack timely how many times do we push back on b/w decent");
1284 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1285 	    SYSCTL_CHILDREN(rack_timely),
1286 	    OID_AUTO, "min_segs", CTLFLAG_RW,
1287 	    &rack_timely_min_segs, 4,
1288 	    "Rack timely when setting the cwnd what is the min num segments");
1289 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1290 	    SYSCTL_CHILDREN(rack_timely),
1291 	    OID_AUTO, "nonstop", CTLFLAG_RW,
1292 	    &rack_timely_no_stopping, 0,
1293 	    "Rack timely don't stop increase");
1294 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1295 	    SYSCTL_CHILDREN(rack_timely),
1296 	    OID_AUTO, "dec_raise_thresh", CTLFLAG_RW,
1297 	    &rack_down_raise_thresh, 100,
1298 	    "If the CA or SS is below this threshold raise on the first 3 b/w lowers (0=always)");
1299 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1300 	    SYSCTL_CHILDREN(rack_timely),
1301 	    OID_AUTO, "bottom_drag_segs", CTLFLAG_RW,
1302 	    &rack_req_segs, 1,
1303 	    "Bottom dragging if not these many segments outstanding and room");
1304 
1305 	/* TLP and Rack related parameters */
1306 	rack_tlp = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1307 	    SYSCTL_CHILDREN(rack_sysctl_root),
1308 	    OID_AUTO,
1309 	    "tlp",
1310 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1311 	    "TLP and Rack related Controls");
1312 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1313 	    SYSCTL_CHILDREN(rack_tlp),
1314 	    OID_AUTO, "use_rrr", CTLFLAG_RW,
1315 	    &use_rack_rr, 1,
1316 	    "Do we use Rack Rapid Recovery");
1317 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1318 	    SYSCTL_CHILDREN(rack_tlp),
1319 	    OID_AUTO, "post_rec_labc", CTLFLAG_RW,
1320 	    &rack_max_abc_post_recovery, 2,
1321 	    "Since we do early recovery, do we override the l_abc to a value, if so what?");
1322 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1323 	    SYSCTL_CHILDREN(rack_tlp),
1324 	    OID_AUTO, "nonrxt_use_cr", CTLFLAG_RW,
1325 	    &rack_non_rxt_use_cr, 0,
1326 	    "Do we use ss/ca rate if in recovery we are transmitting a new data chunk");
1327 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1328 	    SYSCTL_CHILDREN(rack_tlp),
1329 	    OID_AUTO, "tlpmethod", CTLFLAG_RW,
1330 	    &rack_tlp_threshold_use, TLP_USE_TWO_ONE,
1331 	    "What method do we do for TLP time calc 0=no-de-ack-comp, 1=ID, 2=2.1, 3=2.2");
1332 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1333 	    SYSCTL_CHILDREN(rack_tlp),
1334 	    OID_AUTO, "limit", CTLFLAG_RW,
1335 	    &rack_tlp_limit, 2,
1336 	    "How many TLP's can be sent without sending new data");
1337 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1338 	    SYSCTL_CHILDREN(rack_tlp),
1339 	    OID_AUTO, "use_greater", CTLFLAG_RW,
1340 	    &rack_tlp_use_greater, 1,
1341 	    "Should we use the rack_rtt time if its greater than srtt");
1342 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1343 	    SYSCTL_CHILDREN(rack_tlp),
1344 	    OID_AUTO, "tlpminto", CTLFLAG_RW,
1345 	    &rack_tlp_min, 10000,
1346 	    "TLP minimum timeout per the specification (in microseconds)");
1347 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1348 	    SYSCTL_CHILDREN(rack_tlp),
1349 	    OID_AUTO, "send_oldest", CTLFLAG_RW,
1350 	    &rack_always_send_oldest, 0,
1351 	    "Should we always send the oldest TLP and RACK-TLP");
1352 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1353 	    SYSCTL_CHILDREN(rack_tlp),
1354 	    OID_AUTO, "tlp_cwnd_flag", CTLFLAG_RW,
1355 	    &rack_lower_cwnd_at_tlp, 0,
1356 	    "When a TLP completes a retran should we enter recovery");
1357 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1358 	    SYSCTL_CHILDREN(rack_tlp),
1359 	    OID_AUTO, "reorder_thresh", CTLFLAG_RW,
1360 	    &rack_reorder_thresh, 2,
1361 	    "What factor for rack will be added when seeing reordering (shift right)");
1362 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1363 	    SYSCTL_CHILDREN(rack_tlp),
1364 	    OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW,
1365 	    &rack_tlp_thresh, 1,
1366 	    "What divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)");
1367 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1368 	    SYSCTL_CHILDREN(rack_tlp),
1369 	    OID_AUTO, "reorder_fade", CTLFLAG_RW,
1370 	    &rack_reorder_fade, 60000000,
1371 	    "Does reorder detection fade, if so how many microseconds (0 means never)");
1372 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1373 	    SYSCTL_CHILDREN(rack_tlp),
1374 	    OID_AUTO, "pktdelay", CTLFLAG_RW,
1375 	    &rack_pkt_delay, 1000,
1376 	    "Extra RACK time (in microseconds) besides reordering thresh");
1377 
1378 	/* Timer related controls */
1379 	rack_timers = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1380 	    SYSCTL_CHILDREN(rack_sysctl_root),
1381 	    OID_AUTO,
1382 	    "timers",
1383 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1384 	    "Timer related controls");
1385 	SYSCTL_ADD_U8(&rack_sysctl_ctx,
1386 	    SYSCTL_CHILDREN(rack_timers),
1387 	    OID_AUTO, "reset_ssth_rec_rto", CTLFLAG_RW,
1388 	    &rack_ssthresh_rest_rto_rec, 0,
1389 	    "When doing recovery -> rto -> recovery do we reset SSthresh?");
1390 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1391 	    SYSCTL_CHILDREN(rack_timers),
1392 	    OID_AUTO, "scoreboard_thresh", CTLFLAG_RW,
1393 	    &rack_rxt_scoreboard_clear_thresh, 2,
1394 	    "How many RTO's are allowed before we clear the scoreboard");
1395 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1396 	    SYSCTL_CHILDREN(rack_timers),
1397 	    OID_AUTO, "honor_hpts_min", CTLFLAG_RW,
1398 	    &rack_honors_hpts_min_to, 1,
1399 	    "Do rack pacing timers honor hpts min timeout");
1400 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1401 	    SYSCTL_CHILDREN(rack_timers),
1402 	    OID_AUTO, "hpts_max_reduce", CTLFLAG_RW,
1403 	    &rack_max_reduce, 10,
1404 	    "Max percentage we will reduce slot by for pacing when we are behind");
1405 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1406 	    SYSCTL_CHILDREN(rack_timers),
1407 	    OID_AUTO, "persmin", CTLFLAG_RW,
1408 	    &rack_persist_min, 250000,
1409 	    "What is the minimum time in microseconds between persists");
1410 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1411 	    SYSCTL_CHILDREN(rack_timers),
1412 	    OID_AUTO, "persmax", CTLFLAG_RW,
1413 	    &rack_persist_max, 2000000,
1414 	    "What is the largest delay in microseconds between persists");
1415 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1416 	    SYSCTL_CHILDREN(rack_timers),
1417 	    OID_AUTO, "delayed_ack", CTLFLAG_RW,
1418 	    &rack_delayed_ack_time, 40000,
1419 	    "Delayed ack time (40ms in microseconds)");
1420 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1421 	    SYSCTL_CHILDREN(rack_timers),
1422 	    OID_AUTO, "minrto", CTLFLAG_RW,
1423 	    &rack_rto_min, 30000,
1424 	    "Minimum RTO in microseconds -- set with caution below 1000 due to TLP");
1425 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1426 	    SYSCTL_CHILDREN(rack_timers),
1427 	    OID_AUTO, "maxrto", CTLFLAG_RW,
1428 	    &rack_rto_max, 4000000,
1429 	    "Maximum RTO in microseconds -- should be at least as large as min_rto");
1430 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1431 	    SYSCTL_CHILDREN(rack_timers),
1432 	    OID_AUTO, "minto", CTLFLAG_RW,
1433 	    &rack_min_to, 1000,
1434 	    "Minimum rack timeout in microseconds");
1435 	/* Measure controls */
1436 	rack_measure = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1437 	    SYSCTL_CHILDREN(rack_sysctl_root),
1438 	    OID_AUTO,
1439 	    "measure",
1440 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1441 	    "Measure related controls");
1442 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1443 	    SYSCTL_CHILDREN(rack_measure),
1444 	    OID_AUTO, "wma_divisor", CTLFLAG_RW,
1445 	    &rack_wma_divisor, 8,
1446 	    "When doing b/w calculation what is the  divisor for the WMA");
1447 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1448 	    SYSCTL_CHILDREN(rack_measure),
1449 	    OID_AUTO, "end_cwnd", CTLFLAG_RW,
1450 	    &rack_cwnd_block_ends_measure, 0,
1451 	    "Does a cwnd just-return end the measurement window (app limited)");
1452 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1453 	    SYSCTL_CHILDREN(rack_measure),
1454 	    OID_AUTO, "end_rwnd", CTLFLAG_RW,
1455 	    &rack_rwnd_block_ends_measure, 0,
1456 	    "Does an rwnd just-return end the measurement window (app limited -- not persists)");
1457 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1458 	    SYSCTL_CHILDREN(rack_measure),
1459 	    OID_AUTO, "min_target", CTLFLAG_RW,
1460 	    &rack_def_data_window, 20,
1461 	    "What is the minimum target window (in mss) for a GP measurements");
1462 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1463 	    SYSCTL_CHILDREN(rack_measure),
1464 	    OID_AUTO, "goal_bdp", CTLFLAG_RW,
1465 	    &rack_goal_bdp, 2,
1466 	    "What is the goal BDP to measure");
1467 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1468 	    SYSCTL_CHILDREN(rack_measure),
1469 	    OID_AUTO, "min_srtts", CTLFLAG_RW,
1470 	    &rack_min_srtts, 1,
1471 	    "What is the goal BDP to measure");
1472 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1473 	    SYSCTL_CHILDREN(rack_measure),
1474 	    OID_AUTO, "min_measure_tim", CTLFLAG_RW,
1475 	    &rack_min_measure_usec, 0,
1476 	    "What is the Minimum time time for a measurement if 0, this is off");
1477 	/* Features */
1478 	rack_features = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1479 	    SYSCTL_CHILDREN(rack_sysctl_root),
1480 	    OID_AUTO,
1481 	    "features",
1482 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1483 	    "Feature controls");
1484 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1485 	    SYSCTL_CHILDREN(rack_features),
1486 	    OID_AUTO, "hybrid_set_maxseg", CTLFLAG_RW,
1487 	    &rack_hybrid_allow_set_maxseg, 0,
1488 	    "Should hybrid pacing allow the setmss command");
1489 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1490 	    SYSCTL_CHILDREN(rack_features),
1491 	    OID_AUTO, "cmpack", CTLFLAG_RW,
1492 	    &rack_use_cmp_acks, 1,
1493 	    "Should RACK have LRO send compressed acks");
1494 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1495 	    SYSCTL_CHILDREN(rack_features),
1496 	    OID_AUTO, "fsb", CTLFLAG_RW,
1497 	    &rack_use_fsb, 1,
1498 	    "Should RACK use the fast send block?");
1499 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1500 	    SYSCTL_CHILDREN(rack_features),
1501 	    OID_AUTO, "rfo", CTLFLAG_RW,
1502 	    &rack_use_rfo, 1,
1503 	    "Should RACK use rack_fast_output()?");
1504 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1505 	    SYSCTL_CHILDREN(rack_features),
1506 	    OID_AUTO, "rsmrfo", CTLFLAG_RW,
1507 	    &rack_use_rsm_rfo, 1,
1508 	    "Should RACK use rack_fast_rsm_output()?");
1509 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1510 	    SYSCTL_CHILDREN(rack_features),
1511 	    OID_AUTO, "non_paced_lro_queue", CTLFLAG_RW,
1512 	    &rack_enable_mqueue_for_nonpaced, 0,
1513 	    "Should RACK use mbuf queuing for non-paced connections");
1514 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1515 	    SYSCTL_CHILDREN(rack_features),
1516 	    OID_AUTO, "hystartplusplus", CTLFLAG_RW,
1517 	    &rack_do_hystart, 0,
1518 	    "Should RACK enable HyStart++ on connections?");
1519 	/* Misc rack controls */
1520 	rack_misc = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1521 	    SYSCTL_CHILDREN(rack_sysctl_root),
1522 	    OID_AUTO,
1523 	    "misc",
1524 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1525 	    "Misc related controls");
1526 #ifdef TCP_ACCOUNTING
1527 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1528 	    SYSCTL_CHILDREN(rack_misc),
1529 	    OID_AUTO, "tcp_acct", CTLFLAG_RW,
1530 	    &rack_tcp_accounting, 0,
1531 	    "Should we turn on TCP accounting for all rack sessions?");
1532 #endif
1533 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1534 	    SYSCTL_CHILDREN(rack_misc),
1535 	    OID_AUTO, "dnd", CTLFLAG_RW,
1536 	    &rack_dnd_default, 0,
1537 	    "Do not disturb default for rack_rrr = 3");
1538 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1539 	    SYSCTL_CHILDREN(rack_misc),
1540 	    OID_AUTO, "sad_seg_per", CTLFLAG_RW,
1541 	    &sad_seg_size_per, 800,
1542 	    "Percentage of segment size needed in a sack 800 = 80.0?");
1543 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1544 	    SYSCTL_CHILDREN(rack_misc),
1545 	    OID_AUTO, "rxt_controls", CTLFLAG_RW,
1546 	    &rack_rxt_controls, 0,
1547 	    "Retransmit sending size controls (valid  values 0, 1, 2 default=1)?");
1548 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1549 	    SYSCTL_CHILDREN(rack_misc),
1550 	    OID_AUTO, "rack_hibeta", CTLFLAG_RW,
1551 	    &rack_hibeta_setting, 0,
1552 	    "Do we ue a high beta (80 instead of 50)?");
1553 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1554 	    SYSCTL_CHILDREN(rack_misc),
1555 	    OID_AUTO, "apply_rtt_with_low_conf", CTLFLAG_RW,
1556 	    &rack_apply_rtt_with_reduced_conf, 0,
1557 	    "When a persist or keep-alive probe is not answered do we calculate rtt on subsequent answers?");
1558 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1559 	    SYSCTL_CHILDREN(rack_misc),
1560 	    OID_AUTO, "rack_dsack_ctl", CTLFLAG_RW,
1561 	    &rack_dsack_std_based, 3,
1562 	    "How do we process dsack with respect to rack timers, bit field, 3 is standards based?");
1563 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1564 	    SYSCTL_CHILDREN(rack_misc),
1565 	    OID_AUTO, "prr_addback_max", CTLFLAG_RW,
1566 	    &rack_prr_addbackmax, 2,
1567 	    "What is the maximum number of MSS we allow to be added back if prr can't send all its data?");
1568 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1569 	    SYSCTL_CHILDREN(rack_misc),
1570 	    OID_AUTO, "stats_gets_ms", CTLFLAG_RW,
1571 	    &rack_stats_gets_ms_rtt, 1,
1572 	    "What do we feed the stats framework (1 = ms_rtt, 0 = us_rtt, 2 = ms_rtt from hdwr, > 2 usec rtt from hdwr)?");
1573 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1574 	    SYSCTL_CHILDREN(rack_misc),
1575 	    OID_AUTO, "clientlowbuf", CTLFLAG_RW,
1576 	    &rack_client_low_buf, 0,
1577 	    "Client low buffer level (below this we are more aggressive in DGP exiting recovery (0 = off)?");
1578 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1579 	    SYSCTL_CHILDREN(rack_misc),
1580 	    OID_AUTO, "defprofile", CTLFLAG_RW,
1581 	    &rack_def_profile, 0,
1582 	    "Should RACK use a default profile (0=no, num == profile num)?");
1583 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1584 	    SYSCTL_CHILDREN(rack_misc),
1585 	    OID_AUTO, "shared_cwnd", CTLFLAG_RW,
1586 	    &rack_enable_shared_cwnd, 1,
1587 	    "Should RACK try to use the shared cwnd on connections where allowed");
1588 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1589 	    SYSCTL_CHILDREN(rack_misc),
1590 	    OID_AUTO, "limits_on_scwnd", CTLFLAG_RW,
1591 	    &rack_limits_scwnd, 1,
1592 	    "Should RACK place low end time limits on the shared cwnd feature");
1593 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1594 	    SYSCTL_CHILDREN(rack_misc),
1595 	    OID_AUTO, "no_prr", CTLFLAG_RW,
1596 	    &rack_disable_prr, 0,
1597 	    "Should RACK not use prr and only pace (must have pacing on)");
1598 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1599 	    SYSCTL_CHILDREN(rack_misc),
1600 	    OID_AUTO, "bb_verbose", CTLFLAG_RW,
1601 	    &rack_verbose_logging, 0,
1602 	    "Should RACK black box logging be verbose");
1603 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1604 	    SYSCTL_CHILDREN(rack_misc),
1605 	    OID_AUTO, "data_after_close", CTLFLAG_RW,
1606 	    &rack_ignore_data_after_close, 1,
1607 	    "Do we hold off sending a RST until all pending data is ack'd");
1608 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1609 	    SYSCTL_CHILDREN(rack_misc),
1610 	    OID_AUTO, "no_sack_needed", CTLFLAG_RW,
1611 	    &rack_sack_not_required, 1,
1612 	    "Do we allow rack to run on connections not supporting SACK");
1613 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1614 	    SYSCTL_CHILDREN(rack_misc),
1615 	    OID_AUTO, "prr_sendalot", CTLFLAG_RW,
1616 	    &rack_send_a_lot_in_prr, 1,
1617 	    "Send a lot in prr");
1618 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1619 	    SYSCTL_CHILDREN(rack_misc),
1620 	    OID_AUTO, "autoscale", CTLFLAG_RW,
1621 	    &rack_autosndbuf_inc, 20,
1622 	    "What percentage should rack scale up its snd buffer by?");
1623 
1624 
1625 	/* Sack Attacker detection stuff */
1626 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1627 	    SYSCTL_CHILDREN(rack_attack),
1628 	    OID_AUTO, "merge_out", CTLFLAG_RW,
1629 	    &rack_merge_out_sacks_on_attack, 0,
1630 	    "Do we merge the sendmap when we decide we are being attacked?");
1631 
1632 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1633 	    SYSCTL_CHILDREN(rack_attack),
1634 	    OID_AUTO, "detect_highsackratio", CTLFLAG_RW,
1635 	    &rack_highest_sack_thresh_seen, 0,
1636 	    "Highest sack to ack ratio seen");
1637 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1638 	    SYSCTL_CHILDREN(rack_attack),
1639 	    OID_AUTO, "detect_highmoveratio", CTLFLAG_RW,
1640 	    &rack_highest_move_thresh_seen, 0,
1641 	    "Highest move to non-move ratio seen");
1642 	rack_ack_total = counter_u64_alloc(M_WAITOK);
1643 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1644 	    SYSCTL_CHILDREN(rack_attack),
1645 	    OID_AUTO, "acktotal", CTLFLAG_RD,
1646 	    &rack_ack_total,
1647 	    "Total number of Ack's");
1648 	rack_express_sack = counter_u64_alloc(M_WAITOK);
1649 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1650 	    SYSCTL_CHILDREN(rack_attack),
1651 	    OID_AUTO, "exp_sacktotal", CTLFLAG_RD,
1652 	    &rack_express_sack,
1653 	    "Total expresss number of Sack's");
1654 	rack_sack_total = counter_u64_alloc(M_WAITOK);
1655 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1656 	    SYSCTL_CHILDREN(rack_attack),
1657 	    OID_AUTO, "sacktotal", CTLFLAG_RD,
1658 	    &rack_sack_total,
1659 	    "Total number of SACKs");
1660 	rack_move_none = counter_u64_alloc(M_WAITOK);
1661 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1662 	    SYSCTL_CHILDREN(rack_attack),
1663 	    OID_AUTO, "move_none", CTLFLAG_RD,
1664 	    &rack_move_none,
1665 	    "Total number of SACK index reuse of positions under threshold");
1666 	rack_move_some = counter_u64_alloc(M_WAITOK);
1667 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1668 	    SYSCTL_CHILDREN(rack_attack),
1669 	    OID_AUTO, "move_some", CTLFLAG_RD,
1670 	    &rack_move_some,
1671 	    "Total number of SACK index reuse of positions over threshold");
1672 	rack_sack_attacks_detected = counter_u64_alloc(M_WAITOK);
1673 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1674 	    SYSCTL_CHILDREN(rack_attack),
1675 	    OID_AUTO, "attacks", CTLFLAG_RD,
1676 	    &rack_sack_attacks_detected,
1677 	    "Total number of SACK attackers that had sack disabled");
1678 	rack_sack_attacks_reversed = counter_u64_alloc(M_WAITOK);
1679 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1680 	    SYSCTL_CHILDREN(rack_attack),
1681 	    OID_AUTO, "reversed", CTLFLAG_RD,
1682 	    &rack_sack_attacks_reversed,
1683 	    "Total number of SACK attackers that were later determined false positive");
1684 	rack_sack_attacks_suspect = counter_u64_alloc(M_WAITOK);
1685 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1686 	    SYSCTL_CHILDREN(rack_attack),
1687 	    OID_AUTO, "suspect", CTLFLAG_RD,
1688 	    &rack_sack_attacks_suspect,
1689 	    "Total number of SACKs that triggered early detection");
1690 
1691 	rack_sack_used_next_merge = counter_u64_alloc(M_WAITOK);
1692 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1693 	    SYSCTL_CHILDREN(rack_attack),
1694 	    OID_AUTO, "nextmerge", CTLFLAG_RD,
1695 	    &rack_sack_used_next_merge,
1696 	    "Total number of times we used the next merge");
1697 	rack_sack_used_prev_merge = counter_u64_alloc(M_WAITOK);
1698 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1699 	    SYSCTL_CHILDREN(rack_attack),
1700 	    OID_AUTO, "prevmerge", CTLFLAG_RD,
1701 	    &rack_sack_used_prev_merge,
1702 	    "Total number of times we used the prev merge");
1703 	/* Counters */
1704 	rack_total_bytes = counter_u64_alloc(M_WAITOK);
1705 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1706 	    SYSCTL_CHILDREN(rack_counters),
1707 	    OID_AUTO, "totalbytes", CTLFLAG_RD,
1708 	    &rack_total_bytes,
1709 	    "Total number of bytes sent");
1710 	rack_fto_send = counter_u64_alloc(M_WAITOK);
1711 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1712 	    SYSCTL_CHILDREN(rack_counters),
1713 	    OID_AUTO, "fto_send", CTLFLAG_RD,
1714 	    &rack_fto_send, "Total number of rack_fast_output sends");
1715 	rack_fto_rsm_send = counter_u64_alloc(M_WAITOK);
1716 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1717 	    SYSCTL_CHILDREN(rack_counters),
1718 	    OID_AUTO, "fto_rsm_send", CTLFLAG_RD,
1719 	    &rack_fto_rsm_send, "Total number of rack_fast_rsm_output sends");
1720 	rack_nfto_resend = counter_u64_alloc(M_WAITOK);
1721 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1722 	    SYSCTL_CHILDREN(rack_counters),
1723 	    OID_AUTO, "nfto_resend", CTLFLAG_RD,
1724 	    &rack_nfto_resend, "Total number of rack_output retransmissions");
1725 	rack_non_fto_send = counter_u64_alloc(M_WAITOK);
1726 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1727 	    SYSCTL_CHILDREN(rack_counters),
1728 	    OID_AUTO, "nfto_send", CTLFLAG_RD,
1729 	    &rack_non_fto_send, "Total number of rack_output first sends");
1730 	rack_extended_rfo = counter_u64_alloc(M_WAITOK);
1731 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1732 	    SYSCTL_CHILDREN(rack_counters),
1733 	    OID_AUTO, "rfo_extended", CTLFLAG_RD,
1734 	    &rack_extended_rfo, "Total number of times we extended rfo");
1735 
1736 	rack_hw_pace_init_fail = counter_u64_alloc(M_WAITOK);
1737 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1738 	    SYSCTL_CHILDREN(rack_counters),
1739 	    OID_AUTO, "hwpace_init_fail", CTLFLAG_RD,
1740 	    &rack_hw_pace_init_fail, "Total number of times we failed to initialize hw pacing");
1741 	rack_hw_pace_lost = counter_u64_alloc(M_WAITOK);
1742 
1743 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1744 	    SYSCTL_CHILDREN(rack_counters),
1745 	    OID_AUTO, "hwpace_lost", CTLFLAG_RD,
1746 	    &rack_hw_pace_lost, "Total number of times we failed to initialize hw pacing");
1747 	rack_tlp_tot = counter_u64_alloc(M_WAITOK);
1748 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1749 	    SYSCTL_CHILDREN(rack_counters),
1750 	    OID_AUTO, "tlp_to_total", CTLFLAG_RD,
1751 	    &rack_tlp_tot,
1752 	    "Total number of tail loss probe expirations");
1753 	rack_tlp_newdata = counter_u64_alloc(M_WAITOK);
1754 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1755 	    SYSCTL_CHILDREN(rack_counters),
1756 	    OID_AUTO, "tlp_new", CTLFLAG_RD,
1757 	    &rack_tlp_newdata,
1758 	    "Total number of tail loss probe sending new data");
1759 	rack_tlp_retran = counter_u64_alloc(M_WAITOK);
1760 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1761 	    SYSCTL_CHILDREN(rack_counters),
1762 	    OID_AUTO, "tlp_retran", CTLFLAG_RD,
1763 	    &rack_tlp_retran,
1764 	    "Total number of tail loss probe sending retransmitted data");
1765 	rack_tlp_retran_bytes = counter_u64_alloc(M_WAITOK);
1766 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1767 	    SYSCTL_CHILDREN(rack_counters),
1768 	    OID_AUTO, "tlp_retran_bytes", CTLFLAG_RD,
1769 	    &rack_tlp_retran_bytes,
1770 	    "Total bytes of tail loss probe sending retransmitted data");
1771 	rack_to_tot = counter_u64_alloc(M_WAITOK);
1772 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1773 	    SYSCTL_CHILDREN(rack_counters),
1774 	    OID_AUTO, "rack_to_tot", CTLFLAG_RD,
1775 	    &rack_to_tot,
1776 	    "Total number of times the rack to expired");
1777 	rack_saw_enobuf = counter_u64_alloc(M_WAITOK);
1778 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1779 	    SYSCTL_CHILDREN(rack_counters),
1780 	    OID_AUTO, "saw_enobufs", CTLFLAG_RD,
1781 	    &rack_saw_enobuf,
1782 	    "Total number of times a sends returned enobuf for non-hdwr paced connections");
1783 	rack_saw_enobuf_hw = counter_u64_alloc(M_WAITOK);
1784 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1785 	    SYSCTL_CHILDREN(rack_counters),
1786 	    OID_AUTO, "saw_enobufs_hw", CTLFLAG_RD,
1787 	    &rack_saw_enobuf_hw,
1788 	    "Total number of times a send returned enobuf for hdwr paced connections");
1789 	rack_saw_enetunreach = counter_u64_alloc(M_WAITOK);
1790 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1791 	    SYSCTL_CHILDREN(rack_counters),
1792 	    OID_AUTO, "saw_enetunreach", CTLFLAG_RD,
1793 	    &rack_saw_enetunreach,
1794 	    "Total number of times a send received a enetunreachable");
1795 	rack_hot_alloc = counter_u64_alloc(M_WAITOK);
1796 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1797 	    SYSCTL_CHILDREN(rack_counters),
1798 	    OID_AUTO, "alloc_hot", CTLFLAG_RD,
1799 	    &rack_hot_alloc,
1800 	    "Total allocations from the top of our list");
1801 	rack_to_alloc = counter_u64_alloc(M_WAITOK);
1802 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1803 	    SYSCTL_CHILDREN(rack_counters),
1804 	    OID_AUTO, "allocs", CTLFLAG_RD,
1805 	    &rack_to_alloc,
1806 	    "Total allocations of tracking structures");
1807 	rack_to_alloc_hard = counter_u64_alloc(M_WAITOK);
1808 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1809 	    SYSCTL_CHILDREN(rack_counters),
1810 	    OID_AUTO, "allochard", CTLFLAG_RD,
1811 	    &rack_to_alloc_hard,
1812 	    "Total allocations done with sleeping the hard way");
1813 	rack_to_alloc_emerg = counter_u64_alloc(M_WAITOK);
1814 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1815 	    SYSCTL_CHILDREN(rack_counters),
1816 	    OID_AUTO, "allocemerg", CTLFLAG_RD,
1817 	    &rack_to_alloc_emerg,
1818 	    "Total allocations done from emergency cache");
1819 	rack_to_alloc_limited = counter_u64_alloc(M_WAITOK);
1820 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1821 	    SYSCTL_CHILDREN(rack_counters),
1822 	    OID_AUTO, "alloc_limited", CTLFLAG_RD,
1823 	    &rack_to_alloc_limited,
1824 	    "Total allocations dropped due to limit");
1825 	rack_alloc_limited_conns = counter_u64_alloc(M_WAITOK);
1826 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1827 	    SYSCTL_CHILDREN(rack_counters),
1828 	    OID_AUTO, "alloc_limited_conns", CTLFLAG_RD,
1829 	    &rack_alloc_limited_conns,
1830 	    "Connections with allocations dropped due to limit");
1831 	rack_split_limited = counter_u64_alloc(M_WAITOK);
1832 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1833 	    SYSCTL_CHILDREN(rack_counters),
1834 	    OID_AUTO, "split_limited", CTLFLAG_RD,
1835 	    &rack_split_limited,
1836 	    "Split allocations dropped due to limit");
1837 	rack_rxt_clamps_cwnd = counter_u64_alloc(M_WAITOK);
1838 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1839 	    SYSCTL_CHILDREN(rack_counters),
1840 	    OID_AUTO, "rxt_clamps_cwnd", CTLFLAG_RD,
1841 	    &rack_rxt_clamps_cwnd,
1842 	    "Number of times that excessive rxt clamped the cwnd down");
1843 	rack_rxt_clamps_cwnd_uniq = counter_u64_alloc(M_WAITOK);
1844 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1845 	    SYSCTL_CHILDREN(rack_counters),
1846 	    OID_AUTO, "rxt_clamps_cwnd_uniq", CTLFLAG_RD,
1847 	    &rack_rxt_clamps_cwnd_uniq,
1848 	    "Number of connections that have had excessive rxt clamped the cwnd down");
1849 	rack_persists_sends = counter_u64_alloc(M_WAITOK);
1850 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1851 	    SYSCTL_CHILDREN(rack_counters),
1852 	    OID_AUTO, "persist_sends", CTLFLAG_RD,
1853 	    &rack_persists_sends,
1854 	    "Number of times we sent a persist probe");
1855 	rack_persists_acks = counter_u64_alloc(M_WAITOK);
1856 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1857 	    SYSCTL_CHILDREN(rack_counters),
1858 	    OID_AUTO, "persist_acks", CTLFLAG_RD,
1859 	    &rack_persists_acks,
1860 	    "Number of times a persist probe was acked");
1861 	rack_persists_loss = counter_u64_alloc(M_WAITOK);
1862 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1863 	    SYSCTL_CHILDREN(rack_counters),
1864 	    OID_AUTO, "persist_loss", CTLFLAG_RD,
1865 	    &rack_persists_loss,
1866 	    "Number of times we detected a lost persist probe (no ack)");
1867 	rack_persists_lost_ends = counter_u64_alloc(M_WAITOK);
1868 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1869 	    SYSCTL_CHILDREN(rack_counters),
1870 	    OID_AUTO, "persist_loss_ends", CTLFLAG_RD,
1871 	    &rack_persists_lost_ends,
1872 	    "Number of lost persist probe (no ack) that the run ended with a PERSIST abort");
1873 #ifdef INVARIANTS
1874 	rack_adjust_map_bw = counter_u64_alloc(M_WAITOK);
1875 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1876 	    SYSCTL_CHILDREN(rack_counters),
1877 	    OID_AUTO, "map_adjust_req", CTLFLAG_RD,
1878 	    &rack_adjust_map_bw,
1879 	    "Number of times we hit the case where the sb went up and down on a sendmap entry");
1880 #endif
1881 	rack_multi_single_eq = counter_u64_alloc(M_WAITOK);
1882 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1883 	    SYSCTL_CHILDREN(rack_counters),
1884 	    OID_AUTO, "cmp_ack_equiv", CTLFLAG_RD,
1885 	    &rack_multi_single_eq,
1886 	    "Number of compressed acks total represented");
1887 	rack_proc_non_comp_ack = counter_u64_alloc(M_WAITOK);
1888 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1889 	    SYSCTL_CHILDREN(rack_counters),
1890 	    OID_AUTO, "cmp_ack_not", CTLFLAG_RD,
1891 	    &rack_proc_non_comp_ack,
1892 	    "Number of non compresseds acks that we processed");
1893 
1894 
1895 	rack_sack_proc_all = counter_u64_alloc(M_WAITOK);
1896 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1897 	    SYSCTL_CHILDREN(rack_counters),
1898 	    OID_AUTO, "sack_long", CTLFLAG_RD,
1899 	    &rack_sack_proc_all,
1900 	    "Total times we had to walk whole list for sack processing");
1901 	rack_sack_proc_restart = counter_u64_alloc(M_WAITOK);
1902 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1903 	    SYSCTL_CHILDREN(rack_counters),
1904 	    OID_AUTO, "sack_restart", CTLFLAG_RD,
1905 	    &rack_sack_proc_restart,
1906 	    "Total times we had to walk whole list due to a restart");
1907 	rack_sack_proc_short = counter_u64_alloc(M_WAITOK);
1908 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1909 	    SYSCTL_CHILDREN(rack_counters),
1910 	    OID_AUTO, "sack_short", CTLFLAG_RD,
1911 	    &rack_sack_proc_short,
1912 	    "Total times we took shortcut for sack processing");
1913 	rack_sack_skipped_acked = counter_u64_alloc(M_WAITOK);
1914 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1915 	    SYSCTL_CHILDREN(rack_attack),
1916 	    OID_AUTO, "skipacked", CTLFLAG_RD,
1917 	    &rack_sack_skipped_acked,
1918 	    "Total number of times we skipped previously sacked");
1919 	rack_sack_splits = counter_u64_alloc(M_WAITOK);
1920 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1921 	    SYSCTL_CHILDREN(rack_attack),
1922 	    OID_AUTO, "ofsplit", CTLFLAG_RD,
1923 	    &rack_sack_splits,
1924 	    "Total number of times we did the old fashion tree split");
1925 	rack_input_idle_reduces = counter_u64_alloc(M_WAITOK);
1926 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1927 	    SYSCTL_CHILDREN(rack_counters),
1928 	    OID_AUTO, "idle_reduce_oninput", CTLFLAG_RD,
1929 	    &rack_input_idle_reduces,
1930 	    "Total number of idle reductions on input");
1931 	rack_collapsed_win_seen = counter_u64_alloc(M_WAITOK);
1932 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1933 	    SYSCTL_CHILDREN(rack_counters),
1934 	    OID_AUTO, "collapsed_win_seen", CTLFLAG_RD,
1935 	    &rack_collapsed_win_seen,
1936 	    "Total number of collapsed window events seen (where our window shrinks)");
1937 
1938 	rack_collapsed_win = counter_u64_alloc(M_WAITOK);
1939 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1940 	    SYSCTL_CHILDREN(rack_counters),
1941 	    OID_AUTO, "collapsed_win", CTLFLAG_RD,
1942 	    &rack_collapsed_win,
1943 	    "Total number of collapsed window events where we mark packets");
1944 	rack_collapsed_win_rxt = counter_u64_alloc(M_WAITOK);
1945 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1946 	    SYSCTL_CHILDREN(rack_counters),
1947 	    OID_AUTO, "collapsed_win_rxt", CTLFLAG_RD,
1948 	    &rack_collapsed_win_rxt,
1949 	    "Total number of packets that were retransmitted");
1950 	rack_collapsed_win_rxt_bytes = counter_u64_alloc(M_WAITOK);
1951 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1952 	    SYSCTL_CHILDREN(rack_counters),
1953 	    OID_AUTO, "collapsed_win_bytes", CTLFLAG_RD,
1954 	    &rack_collapsed_win_rxt_bytes,
1955 	    "Total number of bytes that were retransmitted");
1956 	rack_try_scwnd = counter_u64_alloc(M_WAITOK);
1957 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1958 	    SYSCTL_CHILDREN(rack_counters),
1959 	    OID_AUTO, "tried_scwnd", CTLFLAG_RD,
1960 	    &rack_try_scwnd,
1961 	    "Total number of scwnd attempts");
1962 	COUNTER_ARRAY_ALLOC(rack_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK);
1963 	SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
1964 	    OID_AUTO, "outsize", CTLFLAG_RD,
1965 	    rack_out_size, TCP_MSS_ACCT_SIZE, "MSS send sizes");
1966 	COUNTER_ARRAY_ALLOC(rack_opts_arry, RACK_OPTS_SIZE, M_WAITOK);
1967 	SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
1968 	    OID_AUTO, "opts", CTLFLAG_RD,
1969 	    rack_opts_arry, RACK_OPTS_SIZE, "RACK Option Stats");
1970 	SYSCTL_ADD_PROC(&rack_sysctl_ctx,
1971 	    SYSCTL_CHILDREN(rack_sysctl_root),
1972 	    OID_AUTO, "clear", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1973 	    &rack_clear_counter, 0, sysctl_rack_clear, "IU", "Clear counters");
1974 }
1975 
1976 static uint32_t
1977 rc_init_window(struct tcp_rack *rack)
1978 {
1979 	return (tcp_compute_initwnd(tcp_maxseg(rack->rc_tp)));
1980 
1981 }
1982 
1983 static uint64_t
1984 rack_get_fixed_pacing_bw(struct tcp_rack *rack)
1985 {
1986 	if (IN_FASTRECOVERY(rack->rc_tp->t_flags))
1987 		return (rack->r_ctl.rc_fixed_pacing_rate_rec);
1988 	else if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh)
1989 		return (rack->r_ctl.rc_fixed_pacing_rate_ss);
1990 	else
1991 		return (rack->r_ctl.rc_fixed_pacing_rate_ca);
1992 }
1993 
1994 static void
1995 rack_log_hybrid_bw(struct tcp_rack *rack, uint32_t seq, uint64_t cbw, uint64_t tim,
1996 	uint64_t data, uint8_t mod, uint16_t aux,
1997 	struct tcp_sendfile_track *cur, int line)
1998 {
1999 #ifdef TCP_REQUEST_TRK
2000 	int do_log = 0;
2001 
2002 	/*
2003 	 * The rate cap one is noisy and only should come out when normal BB logging
2004 	 * is enabled, the other logs (not RATE_CAP and NOT CAP_CALC) only come out
2005 	 * once per chunk and make up the BBpoint that can be turned on by the client.
2006 	 */
2007 	if ((mod == HYBRID_LOG_RATE_CAP) || (mod == HYBRID_LOG_CAP_CALC)) {
2008 		/*
2009 		 * The very noisy two need to only come out when
2010 		 * we have verbose logging on.
2011 		 */
2012 		if (rack_verbose_logging != 0)
2013 			do_log = tcp_bblogging_on(rack->rc_tp);
2014 		else
2015 			do_log = 0;
2016 	} else if (mod != HYBRID_LOG_BW_MEASURE) {
2017 		/*
2018 		 * All other less noisy logs here except the measure which
2019 		 * also needs to come out on the point and the log.
2020 		 */
2021 		do_log = tcp_bblogging_on(rack->rc_tp);
2022 	} else {
2023 		do_log = tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING);
2024 	}
2025 
2026 	if (do_log) {
2027 		union tcp_log_stackspecific log;
2028 		struct timeval tv;
2029 		uint64_t lt_bw;
2030 
2031 		/* Convert our ms to a microsecond */
2032 		memset(&log, 0, sizeof(log));
2033 
2034 		log.u_bbr.cwnd_gain = line;
2035 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2036 		log.u_bbr.rttProp = tim;
2037 		log.u_bbr.bw_inuse = cbw;
2038 		log.u_bbr.delRate = rack_get_gp_est(rack);
2039 		lt_bw = rack_get_lt_bw(rack);
2040 		log.u_bbr.flex1 = seq;
2041 		log.u_bbr.pacing_gain = aux;
2042 		/* lt_bw = < flex3 | flex2 > */
2043 		log.u_bbr.flex2 = (uint32_t)(lt_bw & 0x00000000ffffffff);
2044 		log.u_bbr.flex3 = (uint32_t)((lt_bw >> 32) & 0x00000000ffffffff);
2045 		/* Record the last obtained us rtt in inflight */
2046 		if (cur == NULL) {
2047 			/* Make sure we are looking at the right log if an overide comes in */
2048 			cur = rack->r_ctl.rc_last_sft;
2049 		}
2050 		if (rack->r_ctl.rack_rs.rs_flags != RACK_RTT_EMPTY)
2051 			log.u_bbr.inflight = rack->r_ctl.rack_rs.rs_us_rtt;
2052 		else {
2053 			/* Use the last known rtt i.e. the rack-rtt */
2054 			log.u_bbr.inflight = rack->rc_rack_rtt;
2055 		}
2056 		if (cur != NULL) {
2057 			uint64_t off;
2058 
2059 			log.u_bbr.cur_del_rate = cur->deadline;
2060 			if ((mod == HYBRID_LOG_RATE_CAP) || (mod == HYBRID_LOG_CAP_CALC)) {
2061 				/* start = < lost | pkt_epoch > */
2062 				log.u_bbr.pkt_epoch = (uint32_t)(cur->start & 0x00000000ffffffff);
2063 				log.u_bbr.lost = (uint32_t)((cur->start >> 32) & 0x00000000ffffffff);
2064 				log.u_bbr.flex6 = cur->start_seq;
2065 				log.u_bbr.pkts_out = cur->end_seq;
2066 			} else {
2067 				/* start = < lost | pkt_epoch > */
2068 				log.u_bbr.pkt_epoch = (uint32_t)(cur->start & 0x00000000ffffffff);
2069 				log.u_bbr.lost = (uint32_t)((cur->start >> 32) & 0x00000000ffffffff);
2070 				/* end = < pkts_out | flex6 > */
2071 				log.u_bbr.flex6 = (uint32_t)(cur->end & 0x00000000ffffffff);
2072 				log.u_bbr.pkts_out = (uint32_t)((cur->end >> 32) & 0x00000000ffffffff);
2073 			}
2074 			/* first_send = <lt_epoch | epoch> */
2075 			log.u_bbr.epoch = (uint32_t)(cur->first_send & 0x00000000ffffffff);
2076 			log.u_bbr.lt_epoch = (uint32_t)((cur->first_send >> 32) & 0x00000000ffffffff);
2077 			/* localtime = <delivered | applimited>*/
2078 			log.u_bbr.applimited = (uint32_t)(cur->localtime & 0x00000000ffffffff);
2079 			log.u_bbr.delivered = (uint32_t)((cur->localtime >> 32) & 0x00000000ffffffff);
2080 #ifdef TCP_REQUEST_TRK
2081 			off = (uint64_t)(cur) - (uint64_t)(&rack->rc_tp->t_tcpreq_info[0]);
2082 			log.u_bbr.bbr_substate = (uint8_t)(off / sizeof(struct tcp_sendfile_track));
2083 #endif
2084 			log.u_bbr.inhpts = 1;
2085 			log.u_bbr.flex4 = (uint32_t)(rack->rc_tp->t_sndbytes - cur->sent_at_fs);
2086 			log.u_bbr.flex5 = (uint32_t)(rack->rc_tp->t_snd_rxt_bytes - cur->rxt_at_fs);
2087 			log.u_bbr.flex7 = (uint16_t)cur->hybrid_flags;
2088 		} else {
2089 			log.u_bbr.flex7 = 0xffff;
2090 			log.u_bbr.cur_del_rate = 0xffffffffffffffff;
2091 		}
2092 		/*
2093 		 * Compose bbr_state to be a bit wise 0000ADHF
2094 		 * where A is the always_pace flag
2095 		 * where D is the dgp_on flag
2096 		 * where H is the hybrid_mode on flag
2097 		 * where F is the use_fixed_rate flag.
2098 		 */
2099 		log.u_bbr.bbr_state = rack->rc_always_pace;
2100 		log.u_bbr.bbr_state <<= 1;
2101 		log.u_bbr.bbr_state |= rack->dgp_on;
2102 		log.u_bbr.bbr_state <<= 1;
2103 		log.u_bbr.bbr_state |= rack->rc_hybrid_mode;
2104 		log.u_bbr.bbr_state <<= 1;
2105 		log.u_bbr.bbr_state |= rack->use_fixed_rate;
2106 		log.u_bbr.flex8 = mod;
2107 		tcp_log_event(rack->rc_tp, NULL,
2108 		    &rack->rc_inp->inp_socket->so_rcv,
2109 		    &rack->rc_inp->inp_socket->so_snd,
2110 		    TCP_HYBRID_PACING_LOG, 0,
2111 		    0, &log, false, NULL, __func__, __LINE__, &tv);
2112 
2113 	}
2114 #endif
2115 }
2116 
2117 #ifdef TCP_REQUEST_TRK
2118 static void
2119 rack_log_hybrid_sends(struct tcp_rack *rack, struct tcp_sendfile_track *cur, int line)
2120 {
2121 	if (tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING)) {
2122 		union tcp_log_stackspecific log;
2123 		struct timeval tv;
2124 		uint64_t off;
2125 
2126 		/* Convert our ms to a microsecond */
2127 		memset(&log, 0, sizeof(log));
2128 
2129 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2130 		log.u_bbr.delRate = cur->sent_at_fs;
2131 
2132 		if ((cur->flags & TCP_TRK_TRACK_FLG_LSND) == 0) {
2133 			/*
2134 			 * We did not get a new Rules Applied to set so
2135 			 * no overlapping send occured, this means the
2136 			 * current byte counts are correct.
2137 			 */
2138 			log.u_bbr.cur_del_rate = rack->rc_tp->t_sndbytes;
2139 			log.u_bbr.rttProp = rack->rc_tp->t_snd_rxt_bytes;
2140 		} else {
2141 			/*
2142 			 * Overlapping send case, we switched to a new
2143 			 * send and did a rules applied.
2144 			 */
2145 			log.u_bbr.cur_del_rate = cur->sent_at_ls;
2146 			log.u_bbr.rttProp = cur->rxt_at_ls;
2147 		}
2148 		log.u_bbr.bw_inuse = cur->rxt_at_fs;
2149 		log.u_bbr.cwnd_gain = line;
2150 		off = (uint64_t)(cur) - (uint64_t)(&rack->rc_tp->t_tcpreq_info[0]);
2151 		log.u_bbr.bbr_substate = (uint8_t)(off / sizeof(struct tcp_sendfile_track));
2152 		/* start = < flex1 | flex2 > */
2153 		log.u_bbr.flex2 = (uint32_t)(cur->start & 0x00000000ffffffff);
2154 		log.u_bbr.flex1 = (uint32_t)((cur->start >> 32) & 0x00000000ffffffff);
2155 		/* end = < flex3 | flex4 > */
2156 		log.u_bbr.flex4 = (uint32_t)(cur->end & 0x00000000ffffffff);
2157 		log.u_bbr.flex3 = (uint32_t)((cur->end >> 32) & 0x00000000ffffffff);
2158 
2159 		/* localtime = <delivered | applimited>*/
2160 		log.u_bbr.applimited = (uint32_t)(cur->localtime & 0x00000000ffffffff);
2161 		log.u_bbr.delivered = (uint32_t)((cur->localtime >> 32) & 0x00000000ffffffff);
2162 		/* client timestamp = <lt_epoch | epoch>*/
2163 		log.u_bbr.epoch = (uint32_t)(cur->timestamp & 0x00000000ffffffff);
2164 		log.u_bbr.lt_epoch = (uint32_t)((cur->timestamp >> 32) & 0x00000000ffffffff);
2165 		/* now set all the flags in */
2166 		log.u_bbr.pkts_out = cur->hybrid_flags;
2167 		log.u_bbr.lost = cur->playout_ms;
2168 		log.u_bbr.flex6 = cur->flags;
2169 		/*
2170 		 * Last send time  = <flex5 | pkt_epoch>  note we do not distinguish cases
2171 		 * where a false retransmit occurred so first_send  <-> lastsend may
2172 		 * include longer time then it actually took if we have a false rxt.
2173 		 */
2174 		log.u_bbr.pkt_epoch = (uint32_t)(rack->r_ctl.last_tmit_time_acked & 0x00000000ffffffff);
2175 		log.u_bbr.flex5 = (uint32_t)((rack->r_ctl.last_tmit_time_acked >> 32) & 0x00000000ffffffff);
2176 		/*
2177 		 * Compose bbr_state to be a bit wise 0000ADHF
2178 		 * where A is the always_pace flag
2179 		 * where D is the dgp_on flag
2180 		 * where H is the hybrid_mode on flag
2181 		 * where F is the use_fixed_rate flag.
2182 		 */
2183 		log.u_bbr.bbr_state = rack->rc_always_pace;
2184 		log.u_bbr.bbr_state <<= 1;
2185 		log.u_bbr.bbr_state |= rack->dgp_on;
2186 		log.u_bbr.bbr_state <<= 1;
2187 		log.u_bbr.bbr_state |= rack->rc_hybrid_mode;
2188 		log.u_bbr.bbr_state <<= 1;
2189 		log.u_bbr.bbr_state |= rack->use_fixed_rate;
2190 
2191 		log.u_bbr.flex8 = HYBRID_LOG_SENT_LOST;
2192 		tcp_log_event(rack->rc_tp, NULL,
2193 		    &rack->rc_inp->inp_socket->so_rcv,
2194 		    &rack->rc_inp->inp_socket->so_snd,
2195 		    TCP_HYBRID_PACING_LOG, 0,
2196 		    0, &log, false, NULL, __func__, __LINE__, &tv);
2197 	}
2198 }
2199 #endif
2200 
2201 static inline uint64_t
2202 rack_compensate_for_linerate(struct tcp_rack *rack, uint64_t bw)
2203 {
2204 	uint64_t ret_bw, ether;
2205 	uint64_t u_segsiz;
2206 
2207 	ether = rack->rc_tp->t_maxseg + sizeof(struct tcphdr);
2208 	if (rack->r_is_v6){
2209 #ifdef INET6
2210 		ether += sizeof(struct ip6_hdr);
2211 #endif
2212 		ether += 14;	/* eheader size 6+6+2 */
2213 	} else {
2214 #ifdef INET
2215 		ether += sizeof(struct ip);
2216 #endif
2217 		ether += 14;	/* eheader size 6+6+2 */
2218 	}
2219 	u_segsiz = (uint64_t)min(ctf_fixed_maxseg(rack->rc_tp), rack->r_ctl.rc_pace_min_segs);
2220 	ret_bw = bw;
2221 	ret_bw *= ether;
2222 	ret_bw /= u_segsiz;
2223 	return (ret_bw);
2224 }
2225 
2226 static void
2227 rack_rate_cap_bw(struct tcp_rack *rack, uint64_t *bw, int *capped)
2228 {
2229 #ifdef TCP_REQUEST_TRK
2230 	struct timeval tv;
2231 	uint64_t timenow, timeleft, lenleft, lengone, calcbw;
2232 #endif
2233 
2234 	if (rack->r_ctl.bw_rate_cap == 0)
2235 		return;
2236 #ifdef TCP_REQUEST_TRK
2237 	if (rack->rc_catch_up && rack->rc_hybrid_mode &&
2238 	    (rack->r_ctl.rc_last_sft != NULL)) {
2239 		/*
2240 		 * We have a dynamic cap. The original target
2241 		 * is in bw_rate_cap, but we need to look at
2242 		 * how long it is until we hit the deadline.
2243 		 */
2244 		struct tcp_sendfile_track *ent;
2245 
2246       		ent = rack->r_ctl.rc_last_sft;
2247 		microuptime(&tv);
2248 		timenow = tcp_tv_to_lusectick(&tv);
2249 		if (timenow >= ent->deadline) {
2250 			/* No time left we do DGP only */
2251 			rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2252 					   0, 0, 0, HYBRID_LOG_OUTOFTIME, 0, ent, __LINE__);
2253 			rack->r_ctl.bw_rate_cap = 0;
2254 			return;
2255 		}
2256 		/* We have the time */
2257 		timeleft = rack->r_ctl.rc_last_sft->deadline - timenow;
2258 		if (timeleft < HPTS_MSEC_IN_SEC) {
2259 			/* If there is less than a ms left just use DGPs rate */
2260 			rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2261 					   0, timeleft, 0, HYBRID_LOG_OUTOFTIME, 0, ent, __LINE__);
2262 			rack->r_ctl.bw_rate_cap = 0;
2263 			return;
2264 		}
2265 		/*
2266 		 * Now lets find the amount of data left to send.
2267 		 *
2268 		 * Now ideally we want to use the end_seq to figure out how much more
2269 		 * but it might not be possible (only if we have the TRACK_FG_COMP on the entry..
2270 		 */
2271 		if (ent->flags & TCP_TRK_TRACK_FLG_COMP) {
2272 			if (SEQ_GT(ent->end_seq, rack->rc_tp->snd_una))
2273 				lenleft = ent->end_seq - rack->rc_tp->snd_una;
2274 			else {
2275 				/* TSNH, we should catch it at the send */
2276 				rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2277 						   0, timeleft, 0, HYBRID_LOG_CAPERROR, 0, ent, __LINE__);
2278 				rack->r_ctl.bw_rate_cap = 0;
2279 				return;
2280 			}
2281 		} else {
2282 			/*
2283 			 * The hard way, figure out how much is gone and then
2284 			 * take that away from the total the client asked for
2285 			 * (thats off by tls overhead if this is tls).
2286 			 */
2287 			if (SEQ_GT(rack->rc_tp->snd_una, ent->start_seq))
2288 				lengone = rack->rc_tp->snd_una - ent->start_seq;
2289 			else
2290 				lengone = 0;
2291 			if (lengone < (ent->end - ent->start))
2292 				lenleft = (ent->end - ent->start) - lengone;
2293 			else {
2294 				/* TSNH, we should catch it at the send */
2295 				rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2296 						   0, timeleft, lengone, HYBRID_LOG_CAPERROR, 0, ent, __LINE__);
2297 				rack->r_ctl.bw_rate_cap = 0;
2298 				return;
2299 			}
2300 		}
2301 		if (lenleft == 0) {
2302 			/* We have it all sent */
2303 			rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2304 					   0, timeleft, lenleft, HYBRID_LOG_ALLSENT, 0, ent, __LINE__);
2305 			if (rack->r_ctl.bw_rate_cap)
2306 				goto normal_ratecap;
2307 			else
2308 				return;
2309 		}
2310 		calcbw = lenleft * HPTS_USEC_IN_SEC;
2311 		calcbw /= timeleft;
2312 		/* Now we must compensate for IP/TCP overhead */
2313 		calcbw = rack_compensate_for_linerate(rack, calcbw);
2314 		/* Update the bit rate cap */
2315 		rack->r_ctl.bw_rate_cap = calcbw;
2316 		if ((rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_S_MSS) &&
2317 		    (rack_hybrid_allow_set_maxseg == 1) &&
2318 		    ((rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_SETMSS) == 0)) {
2319 			/* Lets set in a smaller mss possibly here to match our rate-cap */
2320 			uint32_t orig_max;
2321 
2322 			orig_max = rack->r_ctl.rc_pace_max_segs;
2323 			rack->r_ctl.rc_last_sft->hybrid_flags |= TCP_HYBRID_PACING_SETMSS;
2324 			rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack, calcbw, ctf_fixed_maxseg(rack->rc_tp));
2325 			rack_log_type_pacing_sizes(rack->rc_tp, rack, rack->r_ctl.client_suggested_maxseg, orig_max, __LINE__, 5);
2326 		}
2327 		rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2328 				   calcbw, timeleft, lenleft, HYBRID_LOG_CAP_CALC, 0, ent, __LINE__);
2329 		if ((calcbw > 0) && (*bw > calcbw)) {
2330 			rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2331 					   *bw, ent->deadline, lenleft, HYBRID_LOG_RATE_CAP, 0, ent, __LINE__);
2332 			*capped = 1;
2333 			*bw = calcbw;
2334 		}
2335 		return;
2336 	}
2337 normal_ratecap:
2338 #endif
2339 	if ((rack->r_ctl.bw_rate_cap > 0) && (*bw > rack->r_ctl.bw_rate_cap)) {
2340 #ifdef TCP_REQUEST_TRK
2341 		if (rack->rc_hybrid_mode &&
2342 		    rack->rc_catch_up &&
2343 		    (rack->r_ctl.rc_last_sft != NULL) &&
2344 		    (rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_S_MSS) &&
2345 		    (rack_hybrid_allow_set_maxseg == 1) &&
2346 		    ((rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_SETMSS) == 0)) {
2347 			/* Lets set in a smaller mss possibly here to match our rate-cap */
2348 			uint32_t orig_max;
2349 
2350 			orig_max = rack->r_ctl.rc_pace_max_segs;
2351 			rack->r_ctl.rc_last_sft->hybrid_flags |= TCP_HYBRID_PACING_SETMSS;
2352 			rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack, rack->r_ctl.bw_rate_cap, ctf_fixed_maxseg(rack->rc_tp));
2353 			rack_log_type_pacing_sizes(rack->rc_tp, rack, rack->r_ctl.client_suggested_maxseg, orig_max, __LINE__, 5);
2354 		}
2355 #endif
2356 		*capped = 1;
2357 		*bw = rack->r_ctl.bw_rate_cap;
2358 		rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2359 				   *bw, 0, 0,
2360 				   HYBRID_LOG_RATE_CAP, 1, NULL, __LINE__);
2361 	}
2362 }
2363 
2364 static uint64_t
2365 rack_get_gp_est(struct tcp_rack *rack)
2366 {
2367 	uint64_t bw, lt_bw, ret_bw;
2368 
2369 	if (rack->rc_gp_filled == 0) {
2370 		/*
2371 		 * We have yet no b/w measurement,
2372 		 * if we have a user set initial bw
2373 		 * return it. If we don't have that and
2374 		 * we have an srtt, use the tcp IW (10) to
2375 		 * calculate a fictional b/w over the SRTT
2376 		 * which is more or less a guess. Note
2377 		 * we don't use our IW from rack on purpose
2378 		 * so if we have like IW=30, we are not
2379 		 * calculating a "huge" b/w.
2380 		 */
2381 		uint64_t srtt;
2382 
2383 		if (rack->dis_lt_bw == 1)
2384 			lt_bw = 0;
2385 		else
2386 			lt_bw = rack_get_lt_bw(rack);
2387 		if (lt_bw) {
2388 			/*
2389 			 * No goodput bw but a long-term b/w does exist
2390 			 * lets use that.
2391 			 */
2392 			ret_bw = lt_bw;
2393 			goto compensate;
2394 		}
2395 		if (rack->r_ctl.init_rate)
2396 			return (rack->r_ctl.init_rate);
2397 
2398 		/* Ok lets come up with the IW guess, if we have a srtt */
2399 		if (rack->rc_tp->t_srtt == 0) {
2400 			/*
2401 			 * Go with old pacing method
2402 			 * i.e. burst mitigation only.
2403 			 */
2404 			return (0);
2405 		}
2406 		/* Ok lets get the initial TCP win (not racks) */
2407 		bw = tcp_compute_initwnd(tcp_maxseg(rack->rc_tp));
2408 		srtt = (uint64_t)rack->rc_tp->t_srtt;
2409 		bw *= (uint64_t)USECS_IN_SECOND;
2410 		bw /= srtt;
2411 		ret_bw = bw;
2412 		goto compensate;
2413 
2414 	}
2415 	if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) {
2416 		/* Averaging is done, we can return the value */
2417 		bw = rack->r_ctl.gp_bw;
2418 	} else {
2419 		/* Still doing initial average must calculate */
2420 		bw = rack->r_ctl.gp_bw / max(rack->r_ctl.num_measurements, 1);
2421 	}
2422 	if (rack->dis_lt_bw) {
2423 		/* We are not using lt-bw */
2424 		ret_bw = bw;
2425 		goto compensate;
2426 	}
2427 	lt_bw = rack_get_lt_bw(rack);
2428 	if (lt_bw == 0) {
2429 		/* If we don't have one then equate it to the gp_bw */
2430 		lt_bw = rack->r_ctl.gp_bw;
2431 	}
2432 	if (rack->use_lesser_lt_bw) {
2433 		if (lt_bw < bw)
2434 			ret_bw = lt_bw;
2435 		else
2436 			ret_bw = bw;
2437 	} else {
2438 		if (lt_bw > bw)
2439 			ret_bw = lt_bw;
2440 		else
2441 			ret_bw = bw;
2442 	}
2443 	/*
2444 	 * Now lets compensate based on the TCP/IP overhead. Our
2445 	 * Goodput estimate does not include this so we must pace out
2446 	 * a bit faster since our pacing calculations do. The pacing
2447 	 * calculations use the base ETHERNET_SEGMENT_SIZE and the segsiz
2448 	 * we are using to do this, so we do that here in the opposite
2449 	 * direction as well. This means that if we are tunneled and the
2450 	 * segsiz is say 1200 bytes we will get quite a boost, but its
2451 	 * compensated for in the pacing time the opposite way.
2452 	 */
2453 compensate:
2454 	ret_bw = rack_compensate_for_linerate(rack, ret_bw);
2455 	return(ret_bw);
2456 }
2457 
2458 
2459 static uint64_t
2460 rack_get_bw(struct tcp_rack *rack)
2461 {
2462 	uint64_t bw;
2463 
2464 	if (rack->use_fixed_rate) {
2465 		/* Return the fixed pacing rate */
2466 		return (rack_get_fixed_pacing_bw(rack));
2467 	}
2468 	bw = rack_get_gp_est(rack);
2469 	return (bw);
2470 }
2471 
2472 static uint16_t
2473 rack_get_output_gain(struct tcp_rack *rack, struct rack_sendmap *rsm)
2474 {
2475 	if (rack->use_fixed_rate) {
2476 		return (100);
2477 	} else if (rack->in_probe_rtt && (rsm == NULL))
2478 		return (rack->r_ctl.rack_per_of_gp_probertt);
2479 	else if ((IN_FASTRECOVERY(rack->rc_tp->t_flags) &&
2480 		  rack->r_ctl.rack_per_of_gp_rec)) {
2481 		if (rsm) {
2482 			/* a retransmission always use the recovery rate */
2483 			return (rack->r_ctl.rack_per_of_gp_rec);
2484 		} else if (rack->rack_rec_nonrxt_use_cr) {
2485 			/* Directed to use the configured rate */
2486 			goto configured_rate;
2487 		} else if (rack->rack_no_prr &&
2488 			   (rack->r_ctl.rack_per_of_gp_rec > 100)) {
2489 			/* No PRR, lets just use the b/w estimate only */
2490 			return (100);
2491 		} else {
2492 			/*
2493 			 * Here we may have a non-retransmit but we
2494 			 * have no overrides, so just use the recovery
2495 			 * rate (prr is in effect).
2496 			 */
2497 			return (rack->r_ctl.rack_per_of_gp_rec);
2498 		}
2499 	}
2500 configured_rate:
2501 	/* For the configured rate we look at our cwnd vs the ssthresh */
2502 	if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh)
2503 		return (rack->r_ctl.rack_per_of_gp_ss);
2504 	else
2505 		return (rack->r_ctl.rack_per_of_gp_ca);
2506 }
2507 
2508 static void
2509 rack_log_dsack_event(struct tcp_rack *rack, uint8_t mod, uint32_t flex4, uint32_t flex5, uint32_t flex6)
2510 {
2511 	/*
2512 	 * Types of logs (mod value)
2513 	 * 1 = dsack_persists reduced by 1 via T-O or fast recovery exit.
2514 	 * 2 = a dsack round begins, persist is reset to 16.
2515 	 * 3 = a dsack round ends
2516 	 * 4 = Dsack option increases rack rtt flex5 is the srtt input, flex6 is thresh
2517 	 * 5 = Socket option set changing the control flags rc_rack_tmr_std_based, rc_rack_use_dsack
2518 	 * 6 = Final rack rtt, flex4 is srtt and flex6 is final limited thresh.
2519 	 */
2520 	if (tcp_bblogging_on(rack->rc_tp)) {
2521 		union tcp_log_stackspecific log;
2522 		struct timeval tv;
2523 
2524 		memset(&log, 0, sizeof(log));
2525 		log.u_bbr.flex1 = rack->rc_rack_tmr_std_based;
2526 		log.u_bbr.flex1 <<= 1;
2527 		log.u_bbr.flex1 |= rack->rc_rack_use_dsack;
2528 		log.u_bbr.flex1 <<= 1;
2529 		log.u_bbr.flex1 |= rack->rc_dsack_round_seen;
2530 		log.u_bbr.flex2 = rack->r_ctl.dsack_round_end;
2531 		log.u_bbr.flex3 = rack->r_ctl.num_dsack;
2532 		log.u_bbr.flex4 = flex4;
2533 		log.u_bbr.flex5 = flex5;
2534 		log.u_bbr.flex6 = flex6;
2535 		log.u_bbr.flex7 = rack->r_ctl.dsack_persist;
2536 		log.u_bbr.flex8 = mod;
2537 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2538 		log.u_bbr.epoch = rack->r_ctl.current_round;
2539 		log.u_bbr.lt_epoch = rack->r_ctl.rc_considered_lost;
2540 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2541 		    &rack->rc_inp->inp_socket->so_rcv,
2542 		    &rack->rc_inp->inp_socket->so_snd,
2543 		    RACK_DSACK_HANDLING, 0,
2544 		    0, &log, false, &tv);
2545 	}
2546 }
2547 
2548 static void
2549 rack_log_hdwr_pacing(struct tcp_rack *rack,
2550 		     uint64_t rate, uint64_t hw_rate, int line,
2551 		     int error, uint16_t mod)
2552 {
2553 	if (tcp_bblogging_on(rack->rc_tp)) {
2554 		union tcp_log_stackspecific log;
2555 		struct timeval tv;
2556 		const struct ifnet *ifp;
2557 		uint64_t ifp64;
2558 
2559 		memset(&log, 0, sizeof(log));
2560 		log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff);
2561 		log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff);
2562 		if (rack->r_ctl.crte) {
2563 			ifp = rack->r_ctl.crte->ptbl->rs_ifp;
2564 		} else if (rack->rc_inp->inp_route.ro_nh &&
2565 			   rack->rc_inp->inp_route.ro_nh->nh_ifp) {
2566 			ifp = rack->rc_inp->inp_route.ro_nh->nh_ifp;
2567 		} else
2568 			ifp = NULL;
2569 		if (ifp) {
2570 			ifp64 = (uintptr_t)ifp;
2571 			log.u_bbr.flex3 = ((ifp64  >> 32) & 0x00000000ffffffff);
2572 			log.u_bbr.flex4 = (ifp64 & 0x00000000ffffffff);
2573 		}
2574 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2575 		log.u_bbr.bw_inuse = rate;
2576 		log.u_bbr.flex5 = line;
2577 		log.u_bbr.flex6 = error;
2578 		log.u_bbr.flex7 = mod;
2579 		log.u_bbr.applimited = rack->r_ctl.rc_pace_max_segs;
2580 		log.u_bbr.flex8 = rack->use_fixed_rate;
2581 		log.u_bbr.flex8 <<= 1;
2582 		log.u_bbr.flex8 |= rack->rack_hdrw_pacing;
2583 		log.u_bbr.pkts_out = rack->rc_tp->t_maxseg;
2584 		log.u_bbr.delRate = rack->r_ctl.crte_prev_rate;
2585 		if (rack->r_ctl.crte)
2586 			log.u_bbr.cur_del_rate = rack->r_ctl.crte->rate;
2587 		else
2588 			log.u_bbr.cur_del_rate = 0;
2589 		log.u_bbr.rttProp = rack->r_ctl.last_hw_bw_req;
2590 		log.u_bbr.epoch = rack->r_ctl.current_round;
2591 		log.u_bbr.lt_epoch = rack->r_ctl.rc_considered_lost;
2592 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2593 		    &rack->rc_inp->inp_socket->so_rcv,
2594 		    &rack->rc_inp->inp_socket->so_snd,
2595 		    BBR_LOG_HDWR_PACE, 0,
2596 		    0, &log, false, &tv);
2597 	}
2598 }
2599 
2600 static uint64_t
2601 rack_get_output_bw(struct tcp_rack *rack, uint64_t bw, struct rack_sendmap *rsm, int *capped)
2602 {
2603 	/*
2604 	 * We allow rack_per_of_gp_xx to dictate our bw rate we want.
2605 	 */
2606 	uint64_t bw_est, high_rate;
2607 	uint64_t gain;
2608 
2609 	gain = (uint64_t)rack_get_output_gain(rack, rsm);
2610 	bw_est = bw * gain;
2611 	bw_est /= (uint64_t)100;
2612 	/* Never fall below the minimum (def 64kbps) */
2613 	if (bw_est < RACK_MIN_BW)
2614 		bw_est = RACK_MIN_BW;
2615 	if (rack->r_rack_hw_rate_caps) {
2616 		/* Rate caps are in place */
2617 		if (rack->r_ctl.crte != NULL) {
2618 			/* We have a hdwr rate already */
2619 			high_rate = tcp_hw_highest_rate(rack->r_ctl.crte);
2620 			if (bw_est >= high_rate) {
2621 				/* We are capping bw at the highest rate table entry */
2622 				if (rack_hw_rate_cap_per &&
2623 				    (((high_rate * (100 + rack_hw_rate_cap_per)) / 100) < bw_est)) {
2624 					rack->r_rack_hw_rate_caps = 0;
2625 					goto done;
2626 				}
2627 				rack_log_hdwr_pacing(rack,
2628 						     bw_est, high_rate, __LINE__,
2629 						     0, 3);
2630 				bw_est = high_rate;
2631 				if (capped)
2632 					*capped = 1;
2633 			}
2634 		} else if ((rack->rack_hdrw_pacing == 0) &&
2635 			   (rack->rack_hdw_pace_ena) &&
2636 			   (rack->rack_attempt_hdwr_pace == 0) &&
2637 			   (rack->rc_inp->inp_route.ro_nh != NULL) &&
2638 			   (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
2639 			/*
2640 			 * Special case, we have not yet attempted hardware
2641 			 * pacing, and yet we may, when we do, find out if we are
2642 			 * above the highest rate. We need to know the maxbw for the interface
2643 			 * in question (if it supports ratelimiting). We get back
2644 			 * a 0, if the interface is not found in the RL lists.
2645 			 */
2646 			high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp);
2647 			if (high_rate) {
2648 				/* Yep, we have a rate is it above this rate? */
2649 				if (bw_est > high_rate) {
2650 					bw_est = high_rate;
2651 					if (capped)
2652 						*capped = 1;
2653 				}
2654 			}
2655 		}
2656 	}
2657 done:
2658 	return (bw_est);
2659 }
2660 
2661 static void
2662 rack_log_retran_reason(struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t tsused, uint32_t thresh, int mod)
2663 {
2664 	if (tcp_bblogging_on(rack->rc_tp)) {
2665 		union tcp_log_stackspecific log;
2666 		struct timeval tv;
2667 
2668 		if ((mod != 1) && (rack_verbose_logging == 0))  {
2669 			/*
2670 			 * We get 3 values currently for mod
2671 			 * 1 - We are retransmitting and this tells the reason.
2672 			 * 2 - We are clearing a dup-ack count.
2673 			 * 3 - We are incrementing a dup-ack count.
2674 			 *
2675 			 * The clear/increment are only logged
2676 			 * if you have BBverbose on.
2677 			 */
2678 			return;
2679 		}
2680 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2681 		log.u_bbr.flex1 = tsused;
2682 		log.u_bbr.flex2 = thresh;
2683 		log.u_bbr.flex3 = rsm->r_flags;
2684 		log.u_bbr.flex4 = rsm->r_dupack;
2685 		log.u_bbr.flex5 = rsm->r_start;
2686 		log.u_bbr.flex6 = rsm->r_end;
2687 		log.u_bbr.flex8 = mod;
2688 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2689 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2690 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2691 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2692 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2693 		log.u_bbr.pacing_gain = rack->r_must_retran;
2694 		log.u_bbr.epoch = rack->r_ctl.current_round;
2695 		log.u_bbr.lt_epoch = rack->r_ctl.rc_considered_lost;
2696 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2697 		    &rack->rc_inp->inp_socket->so_rcv,
2698 		    &rack->rc_inp->inp_socket->so_snd,
2699 		    BBR_LOG_SETTINGS_CHG, 0,
2700 		    0, &log, false, &tv);
2701 	}
2702 }
2703 
2704 static void
2705 rack_log_to_start(struct tcp_rack *rack, uint32_t cts, uint32_t to, int32_t slot, uint8_t which)
2706 {
2707 	if (tcp_bblogging_on(rack->rc_tp)) {
2708 		union tcp_log_stackspecific log;
2709 		struct timeval tv;
2710 
2711 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2712 		log.u_bbr.flex1 = rack->rc_tp->t_srtt;
2713 		log.u_bbr.flex2 = to;
2714 		log.u_bbr.flex3 = rack->r_ctl.rc_hpts_flags;
2715 		log.u_bbr.flex4 = slot;
2716 		log.u_bbr.flex5 = rack->rc_tp->t_hpts_slot;
2717 		log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
2718 		log.u_bbr.flex7 = rack->rc_in_persist;
2719 		log.u_bbr.flex8 = which;
2720 		if (rack->rack_no_prr)
2721 			log.u_bbr.pkts_out = 0;
2722 		else
2723 			log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
2724 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2725 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2726 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2727 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2728 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2729 		log.u_bbr.pacing_gain = rack->r_must_retran;
2730 		log.u_bbr.cwnd_gain = rack->rack_deferred_inited;
2731 		log.u_bbr.pkt_epoch = rack->rc_has_collapsed;
2732 		log.u_bbr.lt_epoch = rack->rc_tp->t_rxtshift;
2733 		log.u_bbr.lost = rack_rto_min;
2734 		log.u_bbr.epoch = rack->r_ctl.roundends;
2735 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
2736 		log.u_bbr.bw_inuse <<= 32;
2737 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
2738 		log.u_bbr.applimited = rack->rc_tp->t_flags2;
2739 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2740 		    &rack->rc_inp->inp_socket->so_rcv,
2741 		    &rack->rc_inp->inp_socket->so_snd,
2742 		    BBR_LOG_TIMERSTAR, 0,
2743 		    0, &log, false, &tv);
2744 	}
2745 }
2746 
2747 static void
2748 rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm)
2749 {
2750 	if (tcp_bblogging_on(rack->rc_tp)) {
2751 		union tcp_log_stackspecific log;
2752 		struct timeval tv;
2753 
2754 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2755 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2756 		log.u_bbr.flex8 = to_num;
2757 		log.u_bbr.flex1 = rack->r_ctl.rc_rack_min_rtt;
2758 		log.u_bbr.flex2 = rack->rc_rack_rtt;
2759 		if (rsm == NULL)
2760 			log.u_bbr.flex3 = 0;
2761 		else
2762 			log.u_bbr.flex3 = rsm->r_end - rsm->r_start;
2763 		if (rack->rack_no_prr)
2764 			log.u_bbr.flex5 = 0;
2765 		else
2766 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
2767 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2768 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2769 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2770 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2771 		log.u_bbr.pacing_gain = rack->r_must_retran;
2772 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
2773 		log.u_bbr.bw_inuse <<= 32;
2774 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
2775 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2776 		    &rack->rc_inp->inp_socket->so_rcv,
2777 		    &rack->rc_inp->inp_socket->so_snd,
2778 		    BBR_LOG_RTO, 0,
2779 		    0, &log, false, &tv);
2780 	}
2781 }
2782 
2783 static void
2784 rack_log_map_chg(struct tcpcb *tp, struct tcp_rack *rack,
2785 		 struct rack_sendmap *prev,
2786 		 struct rack_sendmap *rsm,
2787 		 struct rack_sendmap *next,
2788 		 int flag, uint32_t th_ack, int line)
2789 {
2790 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
2791 		union tcp_log_stackspecific log;
2792 		struct timeval tv;
2793 
2794 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2795 		log.u_bbr.flex8 = flag;
2796 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2797 		log.u_bbr.cur_del_rate = (uintptr_t)prev;
2798 		log.u_bbr.delRate = (uintptr_t)rsm;
2799 		log.u_bbr.rttProp = (uintptr_t)next;
2800 		log.u_bbr.flex7 = 0;
2801 		if (prev) {
2802 			log.u_bbr.flex1 = prev->r_start;
2803 			log.u_bbr.flex2 = prev->r_end;
2804 			log.u_bbr.flex7 |= 0x4;
2805 		}
2806 		if (rsm) {
2807 			log.u_bbr.flex3 = rsm->r_start;
2808 			log.u_bbr.flex4 = rsm->r_end;
2809 			log.u_bbr.flex7 |= 0x2;
2810 		}
2811 		if (next) {
2812 			log.u_bbr.flex5 = next->r_start;
2813 			log.u_bbr.flex6 = next->r_end;
2814 			log.u_bbr.flex7 |= 0x1;
2815 		}
2816 		log.u_bbr.applimited = line;
2817 		log.u_bbr.pkts_out = th_ack;
2818 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2819 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2820 		if (rack->rack_no_prr)
2821 			log.u_bbr.lost = 0;
2822 		else
2823 			log.u_bbr.lost = rack->r_ctl.rc_prr_sndcnt;
2824 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
2825 		log.u_bbr.bw_inuse <<= 32;
2826 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
2827 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2828 		    &rack->rc_inp->inp_socket->so_rcv,
2829 		    &rack->rc_inp->inp_socket->so_snd,
2830 		    TCP_LOG_MAPCHG, 0,
2831 		    0, &log, false, &tv);
2832 	}
2833 }
2834 
2835 static void
2836 rack_log_rtt_upd(struct tcpcb *tp, struct tcp_rack *rack, uint32_t t, uint32_t len,
2837 		 struct rack_sendmap *rsm, int conf)
2838 {
2839 	if (tcp_bblogging_on(tp)) {
2840 		union tcp_log_stackspecific log;
2841 		struct timeval tv;
2842 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2843 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2844 		log.u_bbr.flex1 = t;
2845 		log.u_bbr.flex2 = len;
2846 		log.u_bbr.flex3 = rack->r_ctl.rc_rack_min_rtt;
2847 		log.u_bbr.flex4 = rack->r_ctl.rack_rs.rs_rtt_lowest;
2848 		log.u_bbr.flex5 = rack->r_ctl.rack_rs.rs_rtt_highest;
2849 		log.u_bbr.flex6 = rack->r_ctl.rack_rs.rs_us_rtrcnt;
2850 		log.u_bbr.flex7 = conf;
2851 		log.u_bbr.rttProp = (uint64_t)rack->r_ctl.rack_rs.rs_rtt_tot;
2852 		log.u_bbr.flex8 = rack->r_ctl.rc_rate_sample_method;
2853 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2854 		log.u_bbr.delivered = rack->r_ctl.rack_rs.rs_us_rtrcnt;
2855 		log.u_bbr.pkts_out = rack->r_ctl.rack_rs.rs_flags;
2856 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2857 		if (rsm) {
2858 			log.u_bbr.pkt_epoch = rsm->r_start;
2859 			log.u_bbr.lost = rsm->r_end;
2860 			log.u_bbr.cwnd_gain = rsm->r_rtr_cnt;
2861 			/* We loose any upper of the 24 bits */
2862 			log.u_bbr.pacing_gain = (uint16_t)rsm->r_flags;
2863 		} else {
2864 			/* Its a SYN */
2865 			log.u_bbr.pkt_epoch = rack->rc_tp->iss;
2866 			log.u_bbr.lost = 0;
2867 			log.u_bbr.cwnd_gain = 0;
2868 			log.u_bbr.pacing_gain = 0;
2869 		}
2870 		/* Write out general bits of interest rrs here */
2871 		log.u_bbr.use_lt_bw = rack->rc_highly_buffered;
2872 		log.u_bbr.use_lt_bw <<= 1;
2873 		log.u_bbr.use_lt_bw |= rack->forced_ack;
2874 		log.u_bbr.use_lt_bw <<= 1;
2875 		log.u_bbr.use_lt_bw |= rack->rc_gp_dyn_mul;
2876 		log.u_bbr.use_lt_bw <<= 1;
2877 		log.u_bbr.use_lt_bw |= rack->in_probe_rtt;
2878 		log.u_bbr.use_lt_bw <<= 1;
2879 		log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt;
2880 		log.u_bbr.use_lt_bw <<= 1;
2881 		log.u_bbr.use_lt_bw |= rack->app_limited_needs_set;
2882 		log.u_bbr.use_lt_bw <<= 1;
2883 		log.u_bbr.use_lt_bw |= rack->rc_gp_filled;
2884 		log.u_bbr.use_lt_bw <<= 1;
2885 		log.u_bbr.use_lt_bw |= rack->rc_dragged_bottom;
2886 		log.u_bbr.applimited = rack->r_ctl.rc_target_probertt_flight;
2887 		log.u_bbr.epoch = rack->r_ctl.rc_time_probertt_starts;
2888 		log.u_bbr.lt_epoch = rack->r_ctl.rc_time_probertt_entered;
2889 		log.u_bbr.cur_del_rate = rack->r_ctl.rc_lower_rtt_us_cts;
2890 		log.u_bbr.delRate = rack->r_ctl.rc_gp_srtt;
2891 		log.u_bbr.bw_inuse = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
2892 		log.u_bbr.bw_inuse <<= 32;
2893 		if (rsm)
2894 			log.u_bbr.bw_inuse |= ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]);
2895 		TCP_LOG_EVENTP(tp, NULL,
2896 		    &rack->rc_inp->inp_socket->so_rcv,
2897 		    &rack->rc_inp->inp_socket->so_snd,
2898 		    BBR_LOG_BBRRTT, 0,
2899 		    0, &log, false, &tv);
2900 
2901 
2902 	}
2903 }
2904 
2905 static void
2906 rack_log_rtt_sample(struct tcp_rack *rack, uint32_t rtt)
2907 {
2908 	/*
2909 	 * Log the rtt sample we are
2910 	 * applying to the srtt algorithm in
2911 	 * useconds.
2912 	 */
2913 	if (tcp_bblogging_on(rack->rc_tp)) {
2914 		union tcp_log_stackspecific log;
2915 		struct timeval tv;
2916 
2917 		/* Convert our ms to a microsecond */
2918 		memset(&log, 0, sizeof(log));
2919 		log.u_bbr.flex1 = rtt;
2920 		log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
2921 		log.u_bbr.flex7 = 1;
2922 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2923 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2924 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2925 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2926 		log.u_bbr.pacing_gain = rack->r_must_retran;
2927 		/*
2928 		 * We capture in delRate the upper 32 bits as
2929 		 * the confidence level we had declared, and the
2930 		 * lower 32 bits as the actual RTT using the arrival
2931 		 * timestamp.
2932 		 */
2933 		log.u_bbr.delRate = rack->r_ctl.rack_rs.confidence;
2934 		log.u_bbr.delRate <<= 32;
2935 		log.u_bbr.delRate |= rack->r_ctl.rack_rs.rs_us_rtt;
2936 		/* Lets capture all the things that make up t_rtxcur */
2937 		log.u_bbr.applimited = rack_rto_min;
2938 		log.u_bbr.epoch = rack_rto_max;
2939 		log.u_bbr.lt_epoch = rack->r_ctl.timer_slop;
2940 		log.u_bbr.lost = rack_rto_min;
2941 		log.u_bbr.pkt_epoch = TICKS_2_USEC(tcp_rexmit_slop);
2942 		log.u_bbr.rttProp = RACK_REXMTVAL(rack->rc_tp);
2943 		log.u_bbr.bw_inuse = rack->r_ctl.act_rcv_time.tv_sec;
2944 		log.u_bbr.bw_inuse *= HPTS_USEC_IN_SEC;
2945 		log.u_bbr.bw_inuse += rack->r_ctl.act_rcv_time.tv_usec;
2946 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2947 		    &rack->rc_inp->inp_socket->so_rcv,
2948 		    &rack->rc_inp->inp_socket->so_snd,
2949 		    TCP_LOG_RTT, 0,
2950 		    0, &log, false, &tv);
2951 	}
2952 }
2953 
2954 static void
2955 rack_log_rtt_sample_calc(struct tcp_rack *rack, uint32_t rtt, uint32_t send_time, uint32_t ack_time, int where)
2956 {
2957 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
2958 		union tcp_log_stackspecific log;
2959 		struct timeval tv;
2960 
2961 		/* Convert our ms to a microsecond */
2962 		memset(&log, 0, sizeof(log));
2963 		log.u_bbr.flex1 = rtt;
2964 		log.u_bbr.flex2 = send_time;
2965 		log.u_bbr.flex3 = ack_time;
2966 		log.u_bbr.flex4 = where;
2967 		log.u_bbr.flex7 = 2;
2968 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2969 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
2970 		log.u_bbr.bw_inuse <<= 32;
2971 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
2972 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2973 		    &rack->rc_inp->inp_socket->so_rcv,
2974 		    &rack->rc_inp->inp_socket->so_snd,
2975 		    TCP_LOG_RTT, 0,
2976 		    0, &log, false, &tv);
2977 	}
2978 }
2979 
2980 
2981 static void
2982 rack_log_rtt_sendmap(struct tcp_rack *rack, uint32_t idx, uint64_t tsv, uint32_t tsecho)
2983 {
2984 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
2985 		union tcp_log_stackspecific log;
2986 		struct timeval tv;
2987 
2988 		/* Convert our ms to a microsecond */
2989 		memset(&log, 0, sizeof(log));
2990 		log.u_bbr.flex1 = idx;
2991 		log.u_bbr.flex2 = rack_ts_to_msec(tsv);
2992 		log.u_bbr.flex3 = tsecho;
2993 		log.u_bbr.flex7 = 3;
2994 		log.u_bbr.rttProp = tsv;
2995 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2996 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
2997 		log.u_bbr.bw_inuse <<= 32;
2998 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
2999 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3000 		    &rack->rc_inp->inp_socket->so_rcv,
3001 		    &rack->rc_inp->inp_socket->so_snd,
3002 		    TCP_LOG_RTT, 0,
3003 		    0, &log, false, &tv);
3004 	}
3005 }
3006 
3007 
3008 static inline void
3009 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick,  int event, int line)
3010 {
3011 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
3012 		union tcp_log_stackspecific log;
3013 		struct timeval tv;
3014 
3015 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3016 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3017 		log.u_bbr.flex1 = line;
3018 		log.u_bbr.flex2 = tick;
3019 		log.u_bbr.flex3 = tp->t_maxunacktime;
3020 		log.u_bbr.flex4 = tp->t_acktime;
3021 		log.u_bbr.flex8 = event;
3022 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3023 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3024 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3025 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3026 		log.u_bbr.pacing_gain = rack->r_must_retran;
3027 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
3028 		log.u_bbr.bw_inuse <<= 32;
3029 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
3030 		TCP_LOG_EVENTP(tp, NULL,
3031 		    &rack->rc_inp->inp_socket->so_rcv,
3032 		    &rack->rc_inp->inp_socket->so_snd,
3033 		    BBR_LOG_PROGRESS, 0,
3034 		    0, &log, false, &tv);
3035 	}
3036 }
3037 
3038 static void
3039 rack_log_type_bbrsnd(struct tcp_rack *rack, uint32_t len, uint32_t slot, uint32_t cts, struct timeval *tv, int line)
3040 {
3041 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
3042 		union tcp_log_stackspecific log;
3043 
3044 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3045 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3046 		log.u_bbr.flex1 = slot;
3047 		if (rack->rack_no_prr)
3048 			log.u_bbr.flex2 = 0;
3049 		else
3050 			log.u_bbr.flex2 = rack->r_ctl.rc_prr_sndcnt;
3051 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
3052 		log.u_bbr.flex6 = line;
3053 		log.u_bbr.flex7 = (0x0000ffff & rack->r_ctl.rc_hpts_flags);
3054 		log.u_bbr.flex8 = rack->rc_in_persist;
3055 		log.u_bbr.timeStamp = cts;
3056 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3057 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3058 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3059 		log.u_bbr.pacing_gain = rack->r_must_retran;
3060 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3061 		    &rack->rc_inp->inp_socket->so_rcv,
3062 		    &rack->rc_inp->inp_socket->so_snd,
3063 		    BBR_LOG_BBRSND, 0,
3064 		    0, &log, false, tv);
3065 	}
3066 }
3067 
3068 static void
3069 rack_log_doseg_done(struct tcp_rack *rack, uint32_t cts, int32_t nxt_pkt, int32_t did_out, int way_out, int nsegs)
3070 {
3071 	if (tcp_bblogging_on(rack->rc_tp)) {
3072 		union tcp_log_stackspecific log;
3073 		struct timeval tv;
3074 
3075 		memset(&log, 0, sizeof(log));
3076 		log.u_bbr.flex1 = did_out;
3077 		log.u_bbr.flex2 = nxt_pkt;
3078 		log.u_bbr.flex3 = way_out;
3079 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
3080 		if (rack->rack_no_prr)
3081 			log.u_bbr.flex5 = 0;
3082 		else
3083 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
3084 		log.u_bbr.flex6 = nsegs;
3085 		log.u_bbr.applimited = rack->r_ctl.rc_pace_min_segs;
3086 		log.u_bbr.flex7 = rack->rc_ack_can_sendout_data;	/* Do we have ack-can-send set */
3087 		log.u_bbr.flex7 <<= 1;
3088 		log.u_bbr.flex7 |= rack->r_fast_output;	/* is fast output primed */
3089 		log.u_bbr.flex7 <<= 1;
3090 		log.u_bbr.flex7 |= rack->r_wanted_output;	/* Do we want output */
3091 		log.u_bbr.flex8 = rack->rc_in_persist;
3092 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3093 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3094 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3095 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
3096 		log.u_bbr.use_lt_bw <<= 1;
3097 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
3098 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3099 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3100 		log.u_bbr.pacing_gain = rack->r_must_retran;
3101 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
3102 		log.u_bbr.bw_inuse <<= 32;
3103 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
3104 		log.u_bbr.epoch = rack->rc_inp->inp_socket->so_snd.sb_hiwat;
3105 		log.u_bbr.lt_epoch = rack->rc_inp->inp_socket->so_rcv.sb_hiwat;
3106 		log.u_bbr.lost = rack->rc_tp->t_srtt;
3107 		log.u_bbr.pkt_epoch = rack->rc_tp->rfbuf_cnt;
3108 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3109 		    &rack->rc_inp->inp_socket->so_rcv,
3110 		    &rack->rc_inp->inp_socket->so_snd,
3111 		    BBR_LOG_DOSEG_DONE, 0,
3112 		    0, &log, false, &tv);
3113 	}
3114 }
3115 
3116 static void
3117 rack_log_type_pacing_sizes(struct tcpcb *tp, struct tcp_rack *rack, uint32_t arg1, uint32_t arg2, uint32_t arg3, uint8_t frm)
3118 {
3119 	if (tcp_bblogging_on(rack->rc_tp)) {
3120 		union tcp_log_stackspecific log;
3121 		struct timeval tv;
3122 
3123 		memset(&log, 0, sizeof(log));
3124 		log.u_bbr.flex1 = rack->r_ctl.rc_pace_min_segs;
3125 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
3126 		log.u_bbr.flex4 = arg1;
3127 		log.u_bbr.flex5 = arg2;
3128 		log.u_bbr.flex7 = rack->r_ctl.rc_user_set_min_segs;
3129 		log.u_bbr.flex6 = arg3;
3130 		log.u_bbr.flex8 = frm;
3131 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3132 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3133 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3134 		log.u_bbr.applimited = rack->r_ctl.rc_sacked;
3135 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3136 		log.u_bbr.pacing_gain = rack->r_must_retran;
3137 		TCP_LOG_EVENTP(tp, NULL, &tptosocket(tp)->so_rcv,
3138 		    &tptosocket(tp)->so_snd,
3139 		    TCP_HDWR_PACE_SIZE, 0, 0, &log, false, &tv);
3140 	}
3141 }
3142 
3143 static void
3144 rack_log_type_just_return(struct tcp_rack *rack, uint32_t cts, uint32_t tlen, uint32_t slot,
3145 			  uint8_t hpts_calling, int reason, uint32_t cwnd_to_use)
3146 {
3147 	if (tcp_bblogging_on(rack->rc_tp)) {
3148 		union tcp_log_stackspecific log;
3149 		struct timeval tv;
3150 
3151 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3152 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3153 		log.u_bbr.flex1 = slot;
3154 		log.u_bbr.flex2 = rack->r_ctl.rc_hpts_flags;
3155 		log.u_bbr.flex4 = reason;
3156 		if (rack->rack_no_prr)
3157 			log.u_bbr.flex5 = 0;
3158 		else
3159 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
3160 		log.u_bbr.flex7 = hpts_calling;
3161 		log.u_bbr.flex8 = rack->rc_in_persist;
3162 		log.u_bbr.lt_epoch = cwnd_to_use;
3163 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3164 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3165 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3166 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3167 		log.u_bbr.pacing_gain = rack->r_must_retran;
3168 		log.u_bbr.cwnd_gain = rack->rc_has_collapsed;
3169 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
3170 		log.u_bbr.bw_inuse <<= 32;
3171 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
3172 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3173 		    &rack->rc_inp->inp_socket->so_rcv,
3174 		    &rack->rc_inp->inp_socket->so_snd,
3175 		    BBR_LOG_JUSTRET, 0,
3176 		    tlen, &log, false, &tv);
3177 	}
3178 }
3179 
3180 static void
3181 rack_log_to_cancel(struct tcp_rack *rack, int32_t hpts_removed, int line, uint32_t us_cts,
3182 		   struct timeval *tv, uint32_t flags_on_entry)
3183 {
3184 	if (tcp_bblogging_on(rack->rc_tp)) {
3185 		union tcp_log_stackspecific log;
3186 
3187 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3188 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3189 		log.u_bbr.flex1 = line;
3190 		log.u_bbr.flex2 = rack->r_ctl.rc_last_output_to;
3191 		log.u_bbr.flex3 = flags_on_entry;
3192 		log.u_bbr.flex4 = us_cts;
3193 		if (rack->rack_no_prr)
3194 			log.u_bbr.flex5 = 0;
3195 		else
3196 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
3197 		log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
3198 		log.u_bbr.flex7 = hpts_removed;
3199 		log.u_bbr.flex8 = 1;
3200 		log.u_bbr.applimited = rack->r_ctl.rc_hpts_flags;
3201 		log.u_bbr.timeStamp = us_cts;
3202 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3203 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3204 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3205 		log.u_bbr.pacing_gain = rack->r_must_retran;
3206 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
3207 		log.u_bbr.bw_inuse <<= 32;
3208 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
3209 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3210 		    &rack->rc_inp->inp_socket->so_rcv,
3211 		    &rack->rc_inp->inp_socket->so_snd,
3212 		    BBR_LOG_TIMERCANC, 0,
3213 		    0, &log, false, tv);
3214 	}
3215 }
3216 
3217 static void
3218 rack_log_alt_to_to_cancel(struct tcp_rack *rack,
3219 			  uint32_t flex1, uint32_t flex2,
3220 			  uint32_t flex3, uint32_t flex4,
3221 			  uint32_t flex5, uint32_t flex6,
3222 			  uint16_t flex7, uint8_t mod)
3223 {
3224 	if (tcp_bblogging_on(rack->rc_tp)) {
3225 		union tcp_log_stackspecific log;
3226 		struct timeval tv;
3227 
3228 		if (mod == 1) {
3229 			/* No you can't use 1, its for the real to cancel */
3230 			return;
3231 		}
3232 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3233 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3234 		log.u_bbr.flex1 = flex1;
3235 		log.u_bbr.flex2 = flex2;
3236 		log.u_bbr.flex3 = flex3;
3237 		log.u_bbr.flex4 = flex4;
3238 		log.u_bbr.flex5 = flex5;
3239 		log.u_bbr.flex6 = flex6;
3240 		log.u_bbr.flex7 = flex7;
3241 		log.u_bbr.flex8 = mod;
3242 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3243 		    &rack->rc_inp->inp_socket->so_rcv,
3244 		    &rack->rc_inp->inp_socket->so_snd,
3245 		    BBR_LOG_TIMERCANC, 0,
3246 		    0, &log, false, &tv);
3247 	}
3248 }
3249 
3250 static void
3251 rack_log_to_processing(struct tcp_rack *rack, uint32_t cts, int32_t ret, int32_t timers)
3252 {
3253 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
3254 		union tcp_log_stackspecific log;
3255 		struct timeval tv;
3256 
3257 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3258 		log.u_bbr.flex1 = timers;
3259 		log.u_bbr.flex2 = ret;
3260 		log.u_bbr.flex3 = rack->r_ctl.rc_timer_exp;
3261 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
3262 		log.u_bbr.flex5 = cts;
3263 		if (rack->rack_no_prr)
3264 			log.u_bbr.flex6 = 0;
3265 		else
3266 			log.u_bbr.flex6 = rack->r_ctl.rc_prr_sndcnt;
3267 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3268 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3269 		log.u_bbr.pacing_gain = rack->r_must_retran;
3270 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3271 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3272 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3273 		    &rack->rc_inp->inp_socket->so_rcv,
3274 		    &rack->rc_inp->inp_socket->so_snd,
3275 		    BBR_LOG_TO_PROCESS, 0,
3276 		    0, &log, false, &tv);
3277 	}
3278 }
3279 
3280 static void
3281 rack_log_to_prr(struct tcp_rack *rack, int frm, int orig_cwnd, int line)
3282 {
3283 	if (tcp_bblogging_on(rack->rc_tp)) {
3284 		union tcp_log_stackspecific log;
3285 		struct timeval tv;
3286 
3287 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3288 		log.u_bbr.flex1 = rack->r_ctl.rc_prr_out;
3289 		log.u_bbr.flex2 = rack->r_ctl.rc_prr_recovery_fs;
3290 		if (rack->rack_no_prr)
3291 			log.u_bbr.flex3 = 0;
3292 		else
3293 			log.u_bbr.flex3 = rack->r_ctl.rc_prr_sndcnt;
3294 		log.u_bbr.flex4 = rack->r_ctl.rc_prr_delivered;
3295 		log.u_bbr.flex5 = rack->r_ctl.rc_sacked;
3296 		log.u_bbr.flex6 = rack->r_ctl.rc_holes_rxt;
3297 		log.u_bbr.flex7 = line;
3298 		log.u_bbr.flex8 = frm;
3299 		log.u_bbr.pkts_out = orig_cwnd;
3300 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3301 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3302 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
3303 		log.u_bbr.use_lt_bw <<= 1;
3304 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
3305 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3306 		    &rack->rc_inp->inp_socket->so_rcv,
3307 		    &rack->rc_inp->inp_socket->so_snd,
3308 		    BBR_LOG_BBRUPD, 0,
3309 		    0, &log, false, &tv);
3310 	}
3311 }
3312 
3313 static void
3314 rack_counter_destroy(void)
3315 {
3316 	counter_u64_free(rack_total_bytes);
3317 	counter_u64_free(rack_fto_send);
3318 	counter_u64_free(rack_fto_rsm_send);
3319 	counter_u64_free(rack_nfto_resend);
3320 	counter_u64_free(rack_hw_pace_init_fail);
3321 	counter_u64_free(rack_hw_pace_lost);
3322 	counter_u64_free(rack_non_fto_send);
3323 	counter_u64_free(rack_extended_rfo);
3324 	counter_u64_free(rack_ack_total);
3325 	counter_u64_free(rack_express_sack);
3326 	counter_u64_free(rack_sack_total);
3327 	counter_u64_free(rack_move_none);
3328 	counter_u64_free(rack_move_some);
3329 	counter_u64_free(rack_sack_attacks_detected);
3330 	counter_u64_free(rack_sack_attacks_reversed);
3331 	counter_u64_free(rack_sack_attacks_suspect);
3332 	counter_u64_free(rack_sack_used_next_merge);
3333 	counter_u64_free(rack_sack_used_prev_merge);
3334 	counter_u64_free(rack_tlp_tot);
3335 	counter_u64_free(rack_tlp_newdata);
3336 	counter_u64_free(rack_tlp_retran);
3337 	counter_u64_free(rack_tlp_retran_bytes);
3338 	counter_u64_free(rack_to_tot);
3339 	counter_u64_free(rack_saw_enobuf);
3340 	counter_u64_free(rack_saw_enobuf_hw);
3341 	counter_u64_free(rack_saw_enetunreach);
3342 	counter_u64_free(rack_hot_alloc);
3343 	counter_u64_free(rack_to_alloc);
3344 	counter_u64_free(rack_to_alloc_hard);
3345 	counter_u64_free(rack_to_alloc_emerg);
3346 	counter_u64_free(rack_to_alloc_limited);
3347 	counter_u64_free(rack_alloc_limited_conns);
3348 	counter_u64_free(rack_split_limited);
3349 	counter_u64_free(rack_multi_single_eq);
3350 	counter_u64_free(rack_rxt_clamps_cwnd);
3351 	counter_u64_free(rack_rxt_clamps_cwnd_uniq);
3352 	counter_u64_free(rack_proc_non_comp_ack);
3353 	counter_u64_free(rack_sack_proc_all);
3354 	counter_u64_free(rack_sack_proc_restart);
3355 	counter_u64_free(rack_sack_proc_short);
3356 	counter_u64_free(rack_sack_skipped_acked);
3357 	counter_u64_free(rack_sack_splits);
3358 	counter_u64_free(rack_input_idle_reduces);
3359 	counter_u64_free(rack_collapsed_win);
3360 	counter_u64_free(rack_collapsed_win_rxt);
3361 	counter_u64_free(rack_collapsed_win_rxt_bytes);
3362 	counter_u64_free(rack_collapsed_win_seen);
3363 	counter_u64_free(rack_try_scwnd);
3364 	counter_u64_free(rack_persists_sends);
3365 	counter_u64_free(rack_persists_acks);
3366 	counter_u64_free(rack_persists_loss);
3367 	counter_u64_free(rack_persists_lost_ends);
3368 #ifdef INVARIANTS
3369 	counter_u64_free(rack_adjust_map_bw);
3370 #endif
3371 	COUNTER_ARRAY_FREE(rack_out_size, TCP_MSS_ACCT_SIZE);
3372 	COUNTER_ARRAY_FREE(rack_opts_arry, RACK_OPTS_SIZE);
3373 }
3374 
3375 static struct rack_sendmap *
3376 rack_alloc(struct tcp_rack *rack)
3377 {
3378 	struct rack_sendmap *rsm;
3379 
3380 	/*
3381 	 * First get the top of the list it in
3382 	 * theory is the "hottest" rsm we have,
3383 	 * possibly just freed by ack processing.
3384 	 */
3385 	if (rack->rc_free_cnt > rack_free_cache) {
3386 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
3387 		TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
3388 		counter_u64_add(rack_hot_alloc, 1);
3389 		rack->rc_free_cnt--;
3390 		return (rsm);
3391 	}
3392 	/*
3393 	 * Once we get under our free cache we probably
3394 	 * no longer have a "hot" one available. Lets
3395 	 * get one from UMA.
3396 	 */
3397 	rsm = uma_zalloc(rack_zone, M_NOWAIT);
3398 	if (rsm) {
3399 		rack->r_ctl.rc_num_maps_alloced++;
3400 		counter_u64_add(rack_to_alloc, 1);
3401 		return (rsm);
3402 	}
3403 	/*
3404 	 * Dig in to our aux rsm's (the last two) since
3405 	 * UMA failed to get us one.
3406 	 */
3407 	if (rack->rc_free_cnt) {
3408 		counter_u64_add(rack_to_alloc_emerg, 1);
3409 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
3410 		TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
3411 		rack->rc_free_cnt--;
3412 		return (rsm);
3413 	}
3414 	return (NULL);
3415 }
3416 
3417 static struct rack_sendmap *
3418 rack_alloc_full_limit(struct tcp_rack *rack)
3419 {
3420 	if ((V_tcp_map_entries_limit > 0) &&
3421 	    (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
3422 		counter_u64_add(rack_to_alloc_limited, 1);
3423 		if (!rack->alloc_limit_reported) {
3424 			rack->alloc_limit_reported = 1;
3425 			counter_u64_add(rack_alloc_limited_conns, 1);
3426 		}
3427 		return (NULL);
3428 	}
3429 	return (rack_alloc(rack));
3430 }
3431 
3432 /* wrapper to allocate a sendmap entry, subject to a specific limit */
3433 static struct rack_sendmap *
3434 rack_alloc_limit(struct tcp_rack *rack, uint8_t limit_type)
3435 {
3436 	struct rack_sendmap *rsm;
3437 
3438 	if (limit_type) {
3439 		/* currently there is only one limit type */
3440 		if (rack->r_ctl.rc_split_limit > 0 &&
3441 		    rack->r_ctl.rc_num_split_allocs >= rack->r_ctl.rc_split_limit) {
3442 			counter_u64_add(rack_split_limited, 1);
3443 			if (!rack->alloc_limit_reported) {
3444 				rack->alloc_limit_reported = 1;
3445 				counter_u64_add(rack_alloc_limited_conns, 1);
3446 			}
3447 			return (NULL);
3448 		}
3449 	}
3450 
3451 	/* allocate and mark in the limit type, if set */
3452 	rsm = rack_alloc(rack);
3453 	if (rsm != NULL && limit_type) {
3454 		rsm->r_limit_type = limit_type;
3455 		rack->r_ctl.rc_num_split_allocs++;
3456 	}
3457 	return (rsm);
3458 }
3459 
3460 static void
3461 rack_free_trim(struct tcp_rack *rack)
3462 {
3463 	struct rack_sendmap *rsm;
3464 
3465 	/*
3466 	 * Free up all the tail entries until
3467 	 * we get our list down to the limit.
3468 	 */
3469 	while (rack->rc_free_cnt > rack_free_cache) {
3470 		rsm = TAILQ_LAST(&rack->r_ctl.rc_free, rack_head);
3471 		TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
3472 		rack->rc_free_cnt--;
3473 		rack->r_ctl.rc_num_maps_alloced--;
3474 		uma_zfree(rack_zone, rsm);
3475 	}
3476 }
3477 
3478 static void
3479 rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm)
3480 {
3481 	if (rsm->r_flags & RACK_APP_LIMITED) {
3482 		if (rack->r_ctl.rc_app_limited_cnt > 0) {
3483 			rack->r_ctl.rc_app_limited_cnt--;
3484 		}
3485 	}
3486 	if (rsm->r_limit_type) {
3487 		/* currently there is only one limit type */
3488 		rack->r_ctl.rc_num_split_allocs--;
3489 	}
3490 	if (rsm == rack->r_ctl.rc_first_appl) {
3491 		rack->r_ctl.cleared_app_ack_seq = rsm->r_start + (rsm->r_end - rsm->r_start);
3492 		rack->r_ctl.cleared_app_ack = 1;
3493 		if (rack->r_ctl.rc_app_limited_cnt == 0)
3494 			rack->r_ctl.rc_first_appl = NULL;
3495 		else
3496 			rack->r_ctl.rc_first_appl = tqhash_find(rack->r_ctl.tqh, rsm->r_nseq_appl);
3497 	}
3498 	if (rsm == rack->r_ctl.rc_resend)
3499 		rack->r_ctl.rc_resend = NULL;
3500 	if (rsm == rack->r_ctl.rc_end_appl)
3501 		rack->r_ctl.rc_end_appl = NULL;
3502 	if (rack->r_ctl.rc_tlpsend == rsm)
3503 		rack->r_ctl.rc_tlpsend = NULL;
3504 	if (rack->r_ctl.rc_sacklast == rsm)
3505 		rack->r_ctl.rc_sacklast = NULL;
3506 	memset(rsm, 0, sizeof(struct rack_sendmap));
3507 	/* Make sure we are not going to overrun our count limit of 0xff */
3508 	if ((rack->rc_free_cnt + 1) > RACK_FREE_CNT_MAX) {
3509 		rack_free_trim(rack);
3510 	}
3511 	TAILQ_INSERT_HEAD(&rack->r_ctl.rc_free, rsm, r_tnext);
3512 	rack->rc_free_cnt++;
3513 }
3514 
3515 static uint32_t
3516 rack_get_measure_window(struct tcpcb *tp, struct tcp_rack *rack)
3517 {
3518 	uint64_t srtt, bw, len, tim;
3519 	uint32_t segsiz, def_len, minl;
3520 
3521 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
3522 	def_len = rack_def_data_window * segsiz;
3523 	if (rack->rc_gp_filled == 0) {
3524 		/*
3525 		 * We have no measurement (IW is in flight?) so
3526 		 * we can only guess using our data_window sysctl
3527 		 * value (usually 20MSS).
3528 		 */
3529 		return (def_len);
3530 	}
3531 	/*
3532 	 * Now we have a number of factors to consider.
3533 	 *
3534 	 * 1) We have a desired BDP which is usually
3535 	 *    at least 2.
3536 	 * 2) We have a minimum number of rtt's usually 1 SRTT
3537 	 *    but we allow it too to be more.
3538 	 * 3) We want to make sure a measurement last N useconds (if
3539 	 *    we have set rack_min_measure_usec.
3540 	 *
3541 	 * We handle the first concern here by trying to create a data
3542 	 * window of max(rack_def_data_window, DesiredBDP). The
3543 	 * second concern we handle in not letting the measurement
3544 	 * window end normally until at least the required SRTT's
3545 	 * have gone by which is done further below in
3546 	 * rack_enough_for_measurement(). Finally the third concern
3547 	 * we also handle here by calculating how long that time
3548 	 * would take at the current BW and then return the
3549 	 * max of our first calculation and that length. Note
3550 	 * that if rack_min_measure_usec is 0, we don't deal
3551 	 * with concern 3. Also for both Concern 1 and 3 an
3552 	 * application limited period could end the measurement
3553 	 * earlier.
3554 	 *
3555 	 * So lets calculate the BDP with the "known" b/w using
3556 	 * the SRTT has our rtt and then multiply it by the
3557 	 * goal.
3558 	 */
3559 	bw = rack_get_bw(rack);
3560 	srtt = (uint64_t)tp->t_srtt;
3561 	len = bw * srtt;
3562 	len /= (uint64_t)HPTS_USEC_IN_SEC;
3563 	len *= max(1, rack_goal_bdp);
3564 	/* Now we need to round up to the nearest MSS */
3565 	len = roundup(len, segsiz);
3566 	if (rack_min_measure_usec) {
3567 		/* Now calculate our min length for this b/w */
3568 		tim = rack_min_measure_usec;
3569 		minl = (tim * bw) / (uint64_t)HPTS_USEC_IN_SEC;
3570 		if (minl == 0)
3571 			minl = 1;
3572 		minl = roundup(minl, segsiz);
3573 		if (len < minl)
3574 			len = minl;
3575 	}
3576 	/*
3577 	 * Now if we have a very small window we want
3578 	 * to attempt to get the window that is
3579 	 * as small as possible. This happens on
3580 	 * low b/w connections and we don't want to
3581 	 * span huge numbers of rtt's between measurements.
3582 	 *
3583 	 * We basically include 2 over our "MIN window" so
3584 	 * that the measurement can be shortened (possibly) by
3585 	 * an ack'ed packet.
3586 	 */
3587 	if (len < def_len)
3588 		return (max((uint32_t)len, ((MIN_GP_WIN+2) * segsiz)));
3589 	else
3590 		return (max((uint32_t)len, def_len));
3591 
3592 }
3593 
3594 static int
3595 rack_enough_for_measurement(struct tcpcb *tp, struct tcp_rack *rack, tcp_seq th_ack, uint8_t *quality)
3596 {
3597 	uint32_t tim, srtts, segsiz;
3598 
3599 	/*
3600 	 * Has enough time passed for the GP measurement to be valid?
3601 	 */
3602 	if (SEQ_LT(th_ack, tp->gput_seq)) {
3603 		/* Not enough bytes yet */
3604 		return (0);
3605 	}
3606 	if ((tp->snd_max == tp->snd_una) ||
3607 	    (th_ack == tp->snd_max)){
3608 		/*
3609 		 * All is acked quality of all acked is
3610 		 * usually low or medium, but we in theory could split
3611 		 * all acked into two cases, where you got
3612 		 * a signifigant amount of your window and
3613 		 * where you did not. For now we leave it
3614 		 * but it is something to contemplate in the
3615 		 * future. The danger here is that delayed ack
3616 		 * is effecting the last byte (which is a 50:50 chance).
3617 		 */
3618 		*quality = RACK_QUALITY_ALLACKED;
3619 		return (1);
3620 	}
3621 	if (SEQ_GEQ(th_ack,  tp->gput_ack)) {
3622 		/*
3623 		 * We obtained our entire window of data we wanted
3624 		 * no matter if we are in recovery or not then
3625 		 * its ok since expanding the window does not
3626 		 * make things fuzzy (or at least not as much).
3627 		 */
3628 		*quality = RACK_QUALITY_HIGH;
3629 		return (1);
3630 	}
3631 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
3632 	if (SEQ_LT(th_ack, tp->gput_ack) &&
3633 	    ((th_ack - tp->gput_seq) < max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) {
3634 		/* Not enough bytes yet */
3635 		return (0);
3636 	}
3637 	if (rack->r_ctl.rc_first_appl &&
3638 	    (SEQ_GEQ(th_ack, rack->r_ctl.rc_first_appl->r_end))) {
3639 		/*
3640 		 * We are up to the app limited send point
3641 		 * we have to measure irrespective of the time..
3642 		 */
3643 		*quality = RACK_QUALITY_APPLIMITED;
3644 		return (1);
3645 	}
3646 	/* Now what about time? */
3647 	srtts = (rack->r_ctl.rc_gp_srtt * rack_min_srtts);
3648 	tim = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - tp->gput_ts;
3649 	if ((tim >= srtts) && (IN_RECOVERY(rack->rc_tp->t_flags) == 0)) {
3650 		/*
3651 		 * We do not allow a measurement if we are in recovery
3652 		 * that would shrink the goodput window we wanted.
3653 		 * This is to prevent cloudyness of when the last send
3654 		 * was actually made.
3655 		 */
3656 		*quality = RACK_QUALITY_HIGH;
3657 		return (1);
3658 	}
3659 	/* Nope not even a full SRTT has passed */
3660 	return (0);
3661 }
3662 
3663 static void
3664 rack_log_timely(struct tcp_rack *rack,
3665 		uint32_t logged, uint64_t cur_bw, uint64_t low_bnd,
3666 		uint64_t up_bnd, int line, uint8_t method)
3667 {
3668 	if (tcp_bblogging_on(rack->rc_tp)) {
3669 		union tcp_log_stackspecific log;
3670 		struct timeval tv;
3671 
3672 		memset(&log, 0, sizeof(log));
3673 		log.u_bbr.flex1 = logged;
3674 		log.u_bbr.flex2 = rack->rc_gp_timely_inc_cnt;
3675 		log.u_bbr.flex2 <<= 4;
3676 		log.u_bbr.flex2 |= rack->rc_gp_timely_dec_cnt;
3677 		log.u_bbr.flex2 <<= 4;
3678 		log.u_bbr.flex2 |= rack->rc_gp_incr;
3679 		log.u_bbr.flex2 <<= 4;
3680 		log.u_bbr.flex2 |= rack->rc_gp_bwred;
3681 		log.u_bbr.flex3 = rack->rc_gp_incr;
3682 		log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss;
3683 		log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ca;
3684 		log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_rec;
3685 		log.u_bbr.flex7 = rack->rc_gp_bwred;
3686 		log.u_bbr.flex8 = method;
3687 		log.u_bbr.cur_del_rate = cur_bw;
3688 		log.u_bbr.delRate = low_bnd;
3689 		log.u_bbr.bw_inuse = up_bnd;
3690 		log.u_bbr.rttProp = rack_get_bw(rack);
3691 		log.u_bbr.pkt_epoch = line;
3692 		log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff;
3693 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3694 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3695 		log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt;
3696 		log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt;
3697 		log.u_bbr.cwnd_gain = rack->rc_dragged_bottom;
3698 		log.u_bbr.cwnd_gain <<= 1;
3699 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_rec;
3700 		log.u_bbr.cwnd_gain <<= 1;
3701 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss;
3702 		log.u_bbr.cwnd_gain <<= 1;
3703 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca;
3704 		log.u_bbr.lost = rack->r_ctl.rc_loss_count;
3705 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3706 		    &rack->rc_inp->inp_socket->so_rcv,
3707 		    &rack->rc_inp->inp_socket->so_snd,
3708 		    TCP_TIMELY_WORK, 0,
3709 		    0, &log, false, &tv);
3710 	}
3711 }
3712 
3713 static int
3714 rack_bw_can_be_raised(struct tcp_rack *rack, uint64_t cur_bw, uint64_t last_bw_est, uint16_t mult)
3715 {
3716 	/*
3717 	 * Before we increase we need to know if
3718 	 * the estimate just made was less than
3719 	 * our pacing goal (i.e. (cur_bw * mult) > last_bw_est)
3720 	 *
3721 	 * If we already are pacing at a fast enough
3722 	 * rate to push us faster there is no sense of
3723 	 * increasing.
3724 	 *
3725 	 * We first caculate our actual pacing rate (ss or ca multiplier
3726 	 * times our cur_bw).
3727 	 *
3728 	 * Then we take the last measured rate and multipy by our
3729 	 * maximum pacing overage to give us a max allowable rate.
3730 	 *
3731 	 * If our act_rate is smaller than our max_allowable rate
3732 	 * then we should increase. Else we should hold steady.
3733 	 *
3734 	 */
3735 	uint64_t act_rate, max_allow_rate;
3736 
3737 	if (rack_timely_no_stopping)
3738 		return (1);
3739 
3740 	if ((cur_bw == 0) || (last_bw_est == 0)) {
3741 		/*
3742 		 * Initial startup case or
3743 		 * everything is acked case.
3744 		 */
3745 		rack_log_timely(rack,  mult, cur_bw, 0, 0,
3746 				__LINE__, 9);
3747 		return (1);
3748 	}
3749 	if (mult <= 100) {
3750 		/*
3751 		 * We can always pace at or slightly above our rate.
3752 		 */
3753 		rack_log_timely(rack,  mult, cur_bw, 0, 0,
3754 				__LINE__, 9);
3755 		return (1);
3756 	}
3757 	act_rate = cur_bw * (uint64_t)mult;
3758 	act_rate /= 100;
3759 	max_allow_rate = last_bw_est * ((uint64_t)rack_max_per_above + (uint64_t)100);
3760 	max_allow_rate /= 100;
3761 	if (act_rate < max_allow_rate) {
3762 		/*
3763 		 * Here the rate we are actually pacing at
3764 		 * is smaller than 10% above our last measurement.
3765 		 * This means we are pacing below what we would
3766 		 * like to try to achieve (plus some wiggle room).
3767 		 */
3768 		rack_log_timely(rack,  mult, cur_bw, act_rate, max_allow_rate,
3769 				__LINE__, 9);
3770 		return (1);
3771 	} else {
3772 		/*
3773 		 * Here we are already pacing at least rack_max_per_above(10%)
3774 		 * what we are getting back. This indicates most likely
3775 		 * that we are being limited (cwnd/rwnd/app) and can't
3776 		 * get any more b/w. There is no sense of trying to
3777 		 * raise up the pacing rate its not speeding us up
3778 		 * and we already are pacing faster than we are getting.
3779 		 */
3780 		rack_log_timely(rack,  mult, cur_bw, act_rate, max_allow_rate,
3781 				__LINE__, 8);
3782 		return (0);
3783 	}
3784 }
3785 
3786 static void
3787 rack_validate_multipliers_at_or_above100(struct tcp_rack *rack)
3788 {
3789 	/*
3790 	 * When we drag bottom, we want to assure
3791 	 * that no multiplier is below 1.0, if so
3792 	 * we want to restore it to at least that.
3793 	 */
3794 	if (rack->r_ctl.rack_per_of_gp_rec  < 100) {
3795 		/* This is unlikely we usually do not touch recovery */
3796 		rack->r_ctl.rack_per_of_gp_rec = 100;
3797 	}
3798 	if (rack->r_ctl.rack_per_of_gp_ca < 100) {
3799 		rack->r_ctl.rack_per_of_gp_ca = 100;
3800 	}
3801 	if (rack->r_ctl.rack_per_of_gp_ss < 100) {
3802 		rack->r_ctl.rack_per_of_gp_ss = 100;
3803 	}
3804 }
3805 
3806 static void
3807 rack_validate_multipliers_at_or_below_100(struct tcp_rack *rack)
3808 {
3809 	if (rack->r_ctl.rack_per_of_gp_ca > 100) {
3810 		rack->r_ctl.rack_per_of_gp_ca = 100;
3811 	}
3812 	if (rack->r_ctl.rack_per_of_gp_ss > 100) {
3813 		rack->r_ctl.rack_per_of_gp_ss = 100;
3814 	}
3815 }
3816 
3817 static void
3818 rack_increase_bw_mul(struct tcp_rack *rack, int timely_says, uint64_t cur_bw, uint64_t last_bw_est, int override)
3819 {
3820 	int32_t  calc, logged, plus;
3821 
3822 	logged = 0;
3823 
3824 	if (rack->rc_skip_timely)
3825 		return;
3826 	if (override) {
3827 		/*
3828 		 * override is passed when we are
3829 		 * loosing b/w and making one last
3830 		 * gasp at trying to not loose out
3831 		 * to a new-reno flow.
3832 		 */
3833 		goto extra_boost;
3834 	}
3835 	/* In classic timely we boost by 5x if we have 5 increases in a row, lets not */
3836 	if (rack->rc_gp_incr &&
3837 	    ((rack->rc_gp_timely_inc_cnt + 1) >= RACK_TIMELY_CNT_BOOST)) {
3838 		/*
3839 		 * Reset and get 5 strokes more before the boost. Note
3840 		 * that the count is 0 based so we have to add one.
3841 		 */
3842 extra_boost:
3843 		plus = (uint32_t)rack_gp_increase_per * RACK_TIMELY_CNT_BOOST;
3844 		rack->rc_gp_timely_inc_cnt = 0;
3845 	} else
3846 		plus = (uint32_t)rack_gp_increase_per;
3847 	/* Must be at least 1% increase for true timely increases */
3848 	if ((plus < 1) &&
3849 	    ((rack->r_ctl.rc_rtt_diff <= 0) || (timely_says <= 0)))
3850 		plus = 1;
3851 	if (rack->rc_gp_saw_rec &&
3852 	    (rack->rc_gp_no_rec_chg == 0) &&
3853 	    rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3854 				  rack->r_ctl.rack_per_of_gp_rec)) {
3855 		/* We have been in recovery ding it too */
3856 		calc = rack->r_ctl.rack_per_of_gp_rec + plus;
3857 		if (calc > 0xffff)
3858 			calc = 0xffff;
3859 		logged |= 1;
3860 		rack->r_ctl.rack_per_of_gp_rec = (uint16_t)calc;
3861 		if (rack->r_ctl.rack_per_upper_bound_ca &&
3862 		    (rack->rc_dragged_bottom == 0) &&
3863 		    (rack->r_ctl.rack_per_of_gp_rec > rack->r_ctl.rack_per_upper_bound_ca))
3864 			rack->r_ctl.rack_per_of_gp_rec = rack->r_ctl.rack_per_upper_bound_ca;
3865 	}
3866 	if (rack->rc_gp_saw_ca &&
3867 	    (rack->rc_gp_saw_ss == 0) &&
3868 	    rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3869 				  rack->r_ctl.rack_per_of_gp_ca)) {
3870 		/* In CA */
3871 		calc = rack->r_ctl.rack_per_of_gp_ca + plus;
3872 		if (calc > 0xffff)
3873 			calc = 0xffff;
3874 		logged |= 2;
3875 		rack->r_ctl.rack_per_of_gp_ca = (uint16_t)calc;
3876 		if (rack->r_ctl.rack_per_upper_bound_ca &&
3877 		    (rack->rc_dragged_bottom == 0) &&
3878 		    (rack->r_ctl.rack_per_of_gp_ca > rack->r_ctl.rack_per_upper_bound_ca))
3879 			rack->r_ctl.rack_per_of_gp_ca = rack->r_ctl.rack_per_upper_bound_ca;
3880 	}
3881 	if (rack->rc_gp_saw_ss &&
3882 	    rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3883 				  rack->r_ctl.rack_per_of_gp_ss)) {
3884 		/* In SS */
3885 		calc = rack->r_ctl.rack_per_of_gp_ss + plus;
3886 		if (calc > 0xffff)
3887 			calc = 0xffff;
3888 		rack->r_ctl.rack_per_of_gp_ss = (uint16_t)calc;
3889 		if (rack->r_ctl.rack_per_upper_bound_ss &&
3890 		    (rack->rc_dragged_bottom == 0) &&
3891 		    (rack->r_ctl.rack_per_of_gp_ss > rack->r_ctl.rack_per_upper_bound_ss))
3892 			rack->r_ctl.rack_per_of_gp_ss = rack->r_ctl.rack_per_upper_bound_ss;
3893 		logged |= 4;
3894 	}
3895 	if (logged &&
3896 	    (rack->rc_gp_incr == 0)){
3897 		/* Go into increment mode */
3898 		rack->rc_gp_incr = 1;
3899 		rack->rc_gp_timely_inc_cnt = 0;
3900 	}
3901 	if (rack->rc_gp_incr &&
3902 	    logged &&
3903 	    (rack->rc_gp_timely_inc_cnt < RACK_TIMELY_CNT_BOOST)) {
3904 		rack->rc_gp_timely_inc_cnt++;
3905 	}
3906 	rack_log_timely(rack,  logged, plus, 0, 0,
3907 			__LINE__, 1);
3908 }
3909 
3910 static uint32_t
3911 rack_get_decrease(struct tcp_rack *rack, uint32_t curper, int32_t rtt_diff)
3912 {
3913 	/*-
3914 	 * norm_grad = rtt_diff / minrtt;
3915 	 * new_per = curper * (1 - B * norm_grad)
3916 	 *
3917 	 * B = rack_gp_decrease_per (default 80%)
3918 	 * rtt_dif = input var current rtt-diff
3919 	 * curper = input var current percentage
3920 	 * minrtt = from rack filter
3921 	 *
3922 	 * In order to do the floating point calculations above we
3923 	 * do an integer conversion. The code looks confusing so let me
3924 	 * translate it into something that use more variables and
3925 	 * is clearer for us humans :)
3926 	 *
3927 	 * uint64_t norm_grad, inverse, reduce_by, final_result;
3928 	 * uint32_t perf;
3929 	 *
3930 	 * norm_grad = (((uint64_t)rtt_diff * 1000000) /
3931 	 *             (uint64_t)get_filter_small(&rack->r_ctl.rc_gp_min_rtt));
3932 	 * inverse = ((uint64_t)rack_gp_decrease * (uint64_t)1000000) * norm_grad;
3933 	 * inverse /= 1000000;
3934 	 * reduce_by = (1000000 - inverse);
3935 	 * final_result = (cur_per * reduce_by) / 1000000;
3936 	 * perf = (uint32_t)final_result;
3937 	 */
3938 	uint64_t perf;
3939 
3940 	perf = (((uint64_t)curper * ((uint64_t)1000000 -
3941 		    ((uint64_t)rack_gp_decrease_per * (uint64_t)10000 *
3942 		     (((uint64_t)rtt_diff * (uint64_t)1000000)/
3943 		      (uint64_t)get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt)))/
3944 		     (uint64_t)1000000)) /
3945 		(uint64_t)1000000);
3946 	if (perf > curper) {
3947 		/* TSNH */
3948 		perf = curper - 1;
3949 	}
3950 	return ((uint32_t)perf);
3951 }
3952 
3953 static uint32_t
3954 rack_decrease_highrtt(struct tcp_rack *rack, uint32_t curper, uint32_t rtt)
3955 {
3956 	/*
3957 	 *                                   highrttthresh
3958 	 * result = curper * (1 - (B * ( 1 -  ------          ))
3959 	 *                                     gp_srtt
3960 	 *
3961 	 * B = rack_gp_decrease_per (default .8 i.e. 80)
3962 	 * highrttthresh = filter_min * rack_gp_rtt_maxmul
3963 	 */
3964 	uint64_t perf;
3965 	uint32_t highrttthresh;
3966 
3967 	highrttthresh = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
3968 
3969 	perf = (((uint64_t)curper * ((uint64_t)1000000 -
3970 				     ((uint64_t)rack_gp_decrease_per * ((uint64_t)1000000 -
3971 					((uint64_t)highrttthresh * (uint64_t)1000000) /
3972 						    (uint64_t)rtt)) / 100)) /(uint64_t)1000000);
3973 	if (tcp_bblogging_on(rack->rc_tp)) {
3974 		uint64_t log1;
3975 
3976 		log1 = rtt;
3977 		log1 <<= 32;
3978 		log1 |= highrttthresh;
3979 		rack_log_timely(rack,
3980 				rack_gp_decrease_per,
3981 				(uint64_t)curper,
3982 				log1,
3983 				perf,
3984 				__LINE__,
3985 				15);
3986 	}
3987 	return (perf);
3988 }
3989 
3990 static void
3991 rack_decrease_bw_mul(struct tcp_rack *rack, int timely_says, uint32_t rtt, int32_t rtt_diff)
3992 {
3993 	uint64_t logvar, logvar2, logvar3;
3994 	uint32_t logged, new_per, ss_red, ca_red, rec_red, alt, val;
3995 
3996 	if (rack->rc_skip_timely)
3997 		return;
3998 	if (rack->rc_gp_incr) {
3999 		/* Turn off increment counting */
4000 		rack->rc_gp_incr = 0;
4001 		rack->rc_gp_timely_inc_cnt = 0;
4002 	}
4003 	ss_red = ca_red = rec_red = 0;
4004 	logged = 0;
4005 	/* Calculate the reduction value */
4006 	if (rtt_diff < 0) {
4007 		rtt_diff *= -1;
4008 	}
4009 	/* Must be at least 1% reduction */
4010 	if (rack->rc_gp_saw_rec && (rack->rc_gp_no_rec_chg == 0)) {
4011 		/* We have been in recovery ding it too */
4012 		if (timely_says == 2) {
4013 			new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_rec, rtt);
4014 			alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
4015 			if (alt < new_per)
4016 				val = alt;
4017 			else
4018 				val = new_per;
4019 		} else
4020 			 val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
4021 		if (rack->r_ctl.rack_per_of_gp_rec > val) {
4022 			rec_red = (rack->r_ctl.rack_per_of_gp_rec - val);
4023 			rack->r_ctl.rack_per_of_gp_rec = (uint16_t)val;
4024 		} else {
4025 			rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound;
4026 			rec_red = 0;
4027 		}
4028 		if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_rec)
4029 			rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound;
4030 		logged |= 1;
4031 	}
4032 	if (rack->rc_gp_saw_ss) {
4033 		/* Sent in SS */
4034 		if (timely_says == 2) {
4035 			new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ss, rtt);
4036 			alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ss, rtt_diff);
4037 			if (alt < new_per)
4038 				val = alt;
4039 			else
4040 				val = new_per;
4041 		} else
4042 			val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ss, rtt_diff);
4043 		if (rack->r_ctl.rack_per_of_gp_ss > new_per) {
4044 			ss_red = rack->r_ctl.rack_per_of_gp_ss - val;
4045 			rack->r_ctl.rack_per_of_gp_ss = (uint16_t)val;
4046 		} else {
4047 			ss_red = new_per;
4048 			rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound;
4049 			logvar = new_per;
4050 			logvar <<= 32;
4051 			logvar |= alt;
4052 			logvar2 = (uint32_t)rtt;
4053 			logvar2 <<= 32;
4054 			logvar2 |= (uint32_t)rtt_diff;
4055 			logvar3 = rack_gp_rtt_maxmul;
4056 			logvar3 <<= 32;
4057 			logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
4058 			rack_log_timely(rack, timely_says,
4059 					logvar2, logvar3,
4060 					logvar, __LINE__, 10);
4061 		}
4062 		if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ss)
4063 			rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound;
4064 		logged |= 4;
4065 	} else if (rack->rc_gp_saw_ca) {
4066 		/* Sent in CA */
4067 		if (timely_says == 2) {
4068 			new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ca, rtt);
4069 			alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ca, rtt_diff);
4070 			if (alt < new_per)
4071 				val = alt;
4072 			else
4073 				val = new_per;
4074 		} else
4075 			val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ca, rtt_diff);
4076 		if (rack->r_ctl.rack_per_of_gp_ca > val) {
4077 			ca_red = rack->r_ctl.rack_per_of_gp_ca - val;
4078 			rack->r_ctl.rack_per_of_gp_ca = (uint16_t)val;
4079 		} else {
4080 			rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound;
4081 			ca_red = 0;
4082 			logvar = new_per;
4083 			logvar <<= 32;
4084 			logvar |= alt;
4085 			logvar2 = (uint32_t)rtt;
4086 			logvar2 <<= 32;
4087 			logvar2 |= (uint32_t)rtt_diff;
4088 			logvar3 = rack_gp_rtt_maxmul;
4089 			logvar3 <<= 32;
4090 			logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
4091 			rack_log_timely(rack, timely_says,
4092 					logvar2, logvar3,
4093 					logvar, __LINE__, 10);
4094 		}
4095 		if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ca)
4096 			rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound;
4097 		logged |= 2;
4098 	}
4099 	if (rack->rc_gp_timely_dec_cnt < 0x7) {
4100 		rack->rc_gp_timely_dec_cnt++;
4101 		if (rack_timely_dec_clear &&
4102 		    (rack->rc_gp_timely_dec_cnt == rack_timely_dec_clear))
4103 			rack->rc_gp_timely_dec_cnt = 0;
4104 	}
4105 	logvar = ss_red;
4106 	logvar <<= 32;
4107 	logvar |= ca_red;
4108 	rack_log_timely(rack,  logged, rec_red, rack_per_lower_bound, logvar,
4109 			__LINE__, 2);
4110 }
4111 
4112 static void
4113 rack_log_rtt_shrinks(struct tcp_rack *rack, uint32_t us_cts,
4114 		     uint32_t rtt, uint32_t line, uint8_t reas)
4115 {
4116 	if (tcp_bblogging_on(rack->rc_tp)) {
4117 		union tcp_log_stackspecific log;
4118 		struct timeval tv;
4119 
4120 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
4121 		log.u_bbr.flex1 = line;
4122 		log.u_bbr.flex2 = rack->r_ctl.rc_time_probertt_starts;
4123 		log.u_bbr.flex3 = rack->r_ctl.rc_lower_rtt_us_cts;
4124 		log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss;
4125 		log.u_bbr.flex5 = rtt;
4126 		log.u_bbr.flex6 = rack->rc_highly_buffered;
4127 		log.u_bbr.flex6 <<= 1;
4128 		log.u_bbr.flex6 |= rack->forced_ack;
4129 		log.u_bbr.flex6 <<= 1;
4130 		log.u_bbr.flex6 |= rack->rc_gp_dyn_mul;
4131 		log.u_bbr.flex6 <<= 1;
4132 		log.u_bbr.flex6 |= rack->in_probe_rtt;
4133 		log.u_bbr.flex6 <<= 1;
4134 		log.u_bbr.flex6 |= rack->measure_saw_probe_rtt;
4135 		log.u_bbr.flex7 = rack->r_ctl.rack_per_of_gp_probertt;
4136 		log.u_bbr.pacing_gain = rack->r_ctl.rack_per_of_gp_ca;
4137 		log.u_bbr.cwnd_gain = rack->r_ctl.rack_per_of_gp_rec;
4138 		log.u_bbr.flex8 = reas;
4139 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
4140 		log.u_bbr.delRate = rack_get_bw(rack);
4141 		log.u_bbr.cur_del_rate = rack->r_ctl.rc_highest_us_rtt;
4142 		log.u_bbr.cur_del_rate <<= 32;
4143 		log.u_bbr.cur_del_rate |= rack->r_ctl.rc_lowest_us_rtt;
4144 		log.u_bbr.applimited = rack->r_ctl.rc_time_probertt_entered;
4145 		log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff;
4146 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
4147 		log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt;
4148 		log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt;
4149 		log.u_bbr.pkt_epoch = rack->r_ctl.rc_lower_rtt_us_cts;
4150 		log.u_bbr.delivered = rack->r_ctl.rc_target_probertt_flight;
4151 		log.u_bbr.lost = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
4152 		log.u_bbr.rttProp = us_cts;
4153 		log.u_bbr.rttProp <<= 32;
4154 		log.u_bbr.rttProp |= rack->r_ctl.rc_entry_gp_rtt;
4155 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
4156 		    &rack->rc_inp->inp_socket->so_rcv,
4157 		    &rack->rc_inp->inp_socket->so_snd,
4158 		    BBR_LOG_RTT_SHRINKS, 0,
4159 		    0, &log, false, &rack->r_ctl.act_rcv_time);
4160 	}
4161 }
4162 
4163 static void
4164 rack_set_prtt_target(struct tcp_rack *rack, uint32_t segsiz, uint32_t rtt)
4165 {
4166 	uint64_t bwdp;
4167 
4168 	bwdp = rack_get_bw(rack);
4169 	bwdp *= (uint64_t)rtt;
4170 	bwdp /= (uint64_t)HPTS_USEC_IN_SEC;
4171 	rack->r_ctl.rc_target_probertt_flight = roundup((uint32_t)bwdp, segsiz);
4172 	if (rack->r_ctl.rc_target_probertt_flight < (segsiz * rack_timely_min_segs)) {
4173 		/*
4174 		 * A window protocol must be able to have 4 packets
4175 		 * outstanding as the floor in order to function
4176 		 * (especially considering delayed ack :D).
4177 		 */
4178 		rack->r_ctl.rc_target_probertt_flight = (segsiz * rack_timely_min_segs);
4179 	}
4180 }
4181 
4182 static void
4183 rack_enter_probertt(struct tcp_rack *rack, uint32_t us_cts)
4184 {
4185 	/**
4186 	 * ProbeRTT is a bit different in rack_pacing than in
4187 	 * BBR. It is like BBR in that it uses the lowering of
4188 	 * the RTT as a signal that we saw something new and
4189 	 * counts from there for how long between. But it is
4190 	 * different in that its quite simple. It does not
4191 	 * play with the cwnd and wait until we get down
4192 	 * to N segments outstanding and hold that for
4193 	 * 200ms. Instead it just sets the pacing reduction
4194 	 * rate to a set percentage (70 by default) and hold
4195 	 * that for a number of recent GP Srtt's.
4196 	 */
4197 	uint32_t segsiz;
4198 
4199 	rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
4200 	if (rack->rc_gp_dyn_mul == 0)
4201 		return;
4202 
4203 	if (rack->rc_tp->snd_max == rack->rc_tp->snd_una) {
4204 		/* We are idle */
4205 		return;
4206 	}
4207 	if ((rack->rc_tp->t_flags & TF_GPUTINPROG) &&
4208 	    SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) {
4209 		/*
4210 		 * Stop the goodput now, the idea here is
4211 		 * that future measurements with in_probe_rtt
4212 		 * won't register if they are not greater so
4213 		 * we want to get what info (if any) is available
4214 		 * now.
4215 		 */
4216 		rack_do_goodput_measurement(rack->rc_tp, rack,
4217 					    rack->rc_tp->snd_una, __LINE__,
4218 					    RACK_QUALITY_PROBERTT);
4219 	}
4220 	rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
4221 	rack->r_ctl.rc_time_probertt_entered = us_cts;
4222 	segsiz = min(ctf_fixed_maxseg(rack->rc_tp),
4223 		     rack->r_ctl.rc_pace_min_segs);
4224 	rack->in_probe_rtt = 1;
4225 	rack->measure_saw_probe_rtt = 1;
4226 	rack->r_ctl.rc_time_probertt_starts = 0;
4227 	rack->r_ctl.rc_entry_gp_rtt = rack->r_ctl.rc_gp_srtt;
4228 	if (rack_probertt_use_min_rtt_entry)
4229 		rack_set_prtt_target(rack, segsiz, get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt));
4230 	else
4231 		rack_set_prtt_target(rack, segsiz, rack->r_ctl.rc_gp_srtt);
4232 	rack_log_rtt_shrinks(rack,  us_cts,  get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4233 			     __LINE__, RACK_RTTS_ENTERPROBE);
4234 }
4235 
4236 static void
4237 rack_exit_probertt(struct tcp_rack *rack, uint32_t us_cts)
4238 {
4239 	struct rack_sendmap *rsm;
4240 	uint32_t segsiz;
4241 
4242 	segsiz = min(ctf_fixed_maxseg(rack->rc_tp),
4243 		     rack->r_ctl.rc_pace_min_segs);
4244 	rack->in_probe_rtt = 0;
4245 	if ((rack->rc_tp->t_flags & TF_GPUTINPROG) &&
4246 	    SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) {
4247 		/*
4248 		 * Stop the goodput now, the idea here is
4249 		 * that future measurements with in_probe_rtt
4250 		 * won't register if they are not greater so
4251 		 * we want to get what info (if any) is available
4252 		 * now.
4253 		 */
4254 		rack_do_goodput_measurement(rack->rc_tp, rack,
4255 					    rack->rc_tp->snd_una, __LINE__,
4256 					    RACK_QUALITY_PROBERTT);
4257 	} else if (rack->rc_tp->t_flags & TF_GPUTINPROG) {
4258 		/*
4259 		 * We don't have enough data to make a measurement.
4260 		 * So lets just stop and start here after exiting
4261 		 * probe-rtt. We probably are not interested in
4262 		 * the results anyway.
4263 		 */
4264 		rack->rc_tp->t_flags &= ~TF_GPUTINPROG;
4265 	}
4266 	/*
4267 	 * Measurements through the current snd_max are going
4268 	 * to be limited by the slower pacing rate.
4269 	 *
4270 	 * We need to mark these as app-limited so we
4271 	 * don't collapse the b/w.
4272 	 */
4273 	rsm = tqhash_max(rack->r_ctl.tqh);
4274 	if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) {
4275 		if (rack->r_ctl.rc_app_limited_cnt == 0)
4276 			rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm;
4277 		else {
4278 			/*
4279 			 * Go out to the end app limited and mark
4280 			 * this new one as next and move the end_appl up
4281 			 * to this guy.
4282 			 */
4283 			if (rack->r_ctl.rc_end_appl)
4284 				rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start;
4285 			rack->r_ctl.rc_end_appl = rsm;
4286 		}
4287 		rsm->r_flags |= RACK_APP_LIMITED;
4288 		rack->r_ctl.rc_app_limited_cnt++;
4289 	}
4290 	/*
4291 	 * Now, we need to examine our pacing rate multipliers.
4292 	 * If its under 100%, we need to kick it back up to
4293 	 * 100%. We also don't let it be over our "max" above
4294 	 * the actual rate i.e. 100% + rack_clamp_atexit_prtt.
4295 	 * Note setting clamp_atexit_prtt to 0 has the effect
4296 	 * of setting CA/SS to 100% always at exit (which is
4297 	 * the default behavior).
4298 	 */
4299 	if (rack_probertt_clear_is) {
4300 		rack->rc_gp_incr = 0;
4301 		rack->rc_gp_bwred = 0;
4302 		rack->rc_gp_timely_inc_cnt = 0;
4303 		rack->rc_gp_timely_dec_cnt = 0;
4304 	}
4305 	/* Do we do any clamping at exit? */
4306 	if (rack->rc_highly_buffered && rack_atexit_prtt_hbp) {
4307 		rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt_hbp;
4308 		rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt_hbp;
4309 	}
4310 	if ((rack->rc_highly_buffered == 0) && rack_atexit_prtt) {
4311 		rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt;
4312 		rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt;
4313 	}
4314 	/*
4315 	 * Lets set rtt_diff to 0, so that we will get a "boost"
4316 	 * after exiting.
4317 	 */
4318 	rack->r_ctl.rc_rtt_diff = 0;
4319 
4320 	/* Clear all flags so we start fresh */
4321 	rack->rc_tp->t_bytes_acked = 0;
4322 	rack->rc_tp->t_ccv.flags &= ~CCF_ABC_SENTAWND;
4323 	/*
4324 	 * If configured to, set the cwnd and ssthresh to
4325 	 * our targets.
4326 	 */
4327 	if (rack_probe_rtt_sets_cwnd) {
4328 		uint64_t ebdp;
4329 		uint32_t setto;
4330 
4331 		/* Set ssthresh so we get into CA once we hit our target */
4332 		if (rack_probertt_use_min_rtt_exit == 1) {
4333 			/* Set to min rtt */
4334 			rack_set_prtt_target(rack, segsiz,
4335 					     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt));
4336 		} else if (rack_probertt_use_min_rtt_exit == 2) {
4337 			/* Set to current gp rtt */
4338 			rack_set_prtt_target(rack, segsiz,
4339 					     rack->r_ctl.rc_gp_srtt);
4340 		} else if (rack_probertt_use_min_rtt_exit == 3) {
4341 			/* Set to entry gp rtt */
4342 			rack_set_prtt_target(rack, segsiz,
4343 					     rack->r_ctl.rc_entry_gp_rtt);
4344 		} else {
4345 			uint64_t sum;
4346 			uint32_t setval;
4347 
4348 			sum = rack->r_ctl.rc_entry_gp_rtt;
4349 			sum *= 10;
4350 			sum /= (uint64_t)(max(1, rack->r_ctl.rc_gp_srtt));
4351 			if (sum >= 20) {
4352 				/*
4353 				 * A highly buffered path needs
4354 				 * cwnd space for timely to work.
4355 				 * Lets set things up as if
4356 				 * we are heading back here again.
4357 				 */
4358 				setval = rack->r_ctl.rc_entry_gp_rtt;
4359 			} else if (sum >= 15) {
4360 				/*
4361 				 * Lets take the smaller of the
4362 				 * two since we are just somewhat
4363 				 * buffered.
4364 				 */
4365 				setval = rack->r_ctl.rc_gp_srtt;
4366 				if (setval > rack->r_ctl.rc_entry_gp_rtt)
4367 					setval = rack->r_ctl.rc_entry_gp_rtt;
4368 			} else {
4369 				/*
4370 				 * Here we are not highly buffered
4371 				 * and should pick the min we can to
4372 				 * keep from causing loss.
4373 				 */
4374 				setval = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
4375 			}
4376 			rack_set_prtt_target(rack, segsiz,
4377 					     setval);
4378 		}
4379 		if (rack_probe_rtt_sets_cwnd > 1) {
4380 			/* There is a percentage here to boost */
4381 			ebdp = rack->r_ctl.rc_target_probertt_flight;
4382 			ebdp *= rack_probe_rtt_sets_cwnd;
4383 			ebdp /= 100;
4384 			setto = rack->r_ctl.rc_target_probertt_flight + ebdp;
4385 		} else
4386 			setto = rack->r_ctl.rc_target_probertt_flight;
4387 		rack->rc_tp->snd_cwnd = roundup(setto, segsiz);
4388 		if (rack->rc_tp->snd_cwnd < (segsiz * rack_timely_min_segs)) {
4389 			/* Enforce a min */
4390 			rack->rc_tp->snd_cwnd = segsiz * rack_timely_min_segs;
4391 		}
4392 		/* If we set in the cwnd also set the ssthresh point so we are in CA */
4393 		rack->rc_tp->snd_ssthresh = (rack->rc_tp->snd_cwnd - 1);
4394 	}
4395 	rack_log_rtt_shrinks(rack,  us_cts,
4396 			     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4397 			     __LINE__, RACK_RTTS_EXITPROBE);
4398 	/* Clear times last so log has all the info */
4399 	rack->r_ctl.rc_probertt_sndmax_atexit = rack->rc_tp->snd_max;
4400 	rack->r_ctl.rc_time_probertt_entered = us_cts;
4401 	rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
4402 	rack->r_ctl.rc_time_of_last_probertt = us_cts;
4403 }
4404 
4405 static void
4406 rack_check_probe_rtt(struct tcp_rack *rack, uint32_t us_cts)
4407 {
4408 	/* Check in on probe-rtt */
4409 
4410 	if (rack->rc_gp_filled == 0) {
4411 		/* We do not do p-rtt unless we have gp measurements */
4412 		return;
4413 	}
4414 	if (rack->in_probe_rtt) {
4415 		uint64_t no_overflow;
4416 		uint32_t endtime, must_stay;
4417 
4418 		if (rack->r_ctl.rc_went_idle_time &&
4419 		    ((us_cts - rack->r_ctl.rc_went_idle_time) > rack_min_probertt_hold)) {
4420 			/*
4421 			 * We went idle during prtt, just exit now.
4422 			 */
4423 			rack_exit_probertt(rack, us_cts);
4424 		} else if (rack_probe_rtt_safety_val &&
4425 		    TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered) &&
4426 		    ((us_cts - rack->r_ctl.rc_time_probertt_entered) > rack_probe_rtt_safety_val)) {
4427 			/*
4428 			 * Probe RTT safety value triggered!
4429 			 */
4430 			rack_log_rtt_shrinks(rack,  us_cts,
4431 					     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4432 					     __LINE__, RACK_RTTS_SAFETY);
4433 			rack_exit_probertt(rack, us_cts);
4434 		}
4435 		/* Calculate the max we will wait */
4436 		endtime = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_max_drain_wait);
4437 		if (rack->rc_highly_buffered)
4438 			endtime += (rack->r_ctl.rc_gp_srtt * rack_max_drain_hbp);
4439 		/* Calculate the min we must wait */
4440 		must_stay = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_must_drain);
4441 		if ((ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.rc_target_probertt_flight) &&
4442 		    TSTMP_LT(us_cts, endtime)) {
4443 			uint32_t calc;
4444 			/* Do we lower more? */
4445 no_exit:
4446 			if (TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered))
4447 				calc = us_cts - rack->r_ctl.rc_time_probertt_entered;
4448 			else
4449 				calc = 0;
4450 			calc /= max(rack->r_ctl.rc_gp_srtt, 1);
4451 			if (calc) {
4452 				/* Maybe */
4453 				calc *= rack_per_of_gp_probertt_reduce;
4454 				if (calc > rack_per_of_gp_probertt)
4455 					rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_lowthresh;
4456 				else
4457 					rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt - calc;
4458 				/* Limit it too */
4459 				if (rack->r_ctl.rack_per_of_gp_probertt < rack_per_of_gp_lowthresh)
4460 					rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_lowthresh;
4461 			}
4462 			/* We must reach target or the time set */
4463 			return;
4464 		}
4465 		if (rack->r_ctl.rc_time_probertt_starts == 0) {
4466 			if ((TSTMP_LT(us_cts, must_stay) &&
4467 			     rack->rc_highly_buffered) ||
4468 			     (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) >
4469 			      rack->r_ctl.rc_target_probertt_flight)) {
4470 				/* We are not past the must_stay time */
4471 				goto no_exit;
4472 			}
4473 			rack_log_rtt_shrinks(rack,  us_cts,
4474 					     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4475 					     __LINE__, RACK_RTTS_REACHTARGET);
4476 			rack->r_ctl.rc_time_probertt_starts = us_cts;
4477 			if (rack->r_ctl.rc_time_probertt_starts == 0)
4478 				rack->r_ctl.rc_time_probertt_starts = 1;
4479 			/* Restore back to our rate we want to pace at in prtt */
4480 			rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
4481 		}
4482 		/*
4483 		 * Setup our end time, some number of gp_srtts plus 200ms.
4484 		 */
4485 		no_overflow = ((uint64_t)rack->r_ctl.rc_gp_srtt *
4486 			       (uint64_t)rack_probertt_gpsrtt_cnt_mul);
4487 		if (rack_probertt_gpsrtt_cnt_div)
4488 			endtime = (uint32_t)(no_overflow / (uint64_t)rack_probertt_gpsrtt_cnt_div);
4489 		else
4490 			endtime = 0;
4491 		endtime += rack_min_probertt_hold;
4492 		endtime += rack->r_ctl.rc_time_probertt_starts;
4493 		if (TSTMP_GEQ(us_cts,  endtime)) {
4494 			/* yes, exit probertt */
4495 			rack_exit_probertt(rack, us_cts);
4496 		}
4497 
4498 	} else if ((rack->rc_skip_timely == 0) &&
4499 		   (TSTMP_GT(us_cts, rack->r_ctl.rc_lower_rtt_us_cts)) &&
4500 		   ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= rack_time_between_probertt)) {
4501 		/* Go into probertt, its been too long since we went lower */
4502 		rack_enter_probertt(rack, us_cts);
4503 	}
4504 }
4505 
4506 static void
4507 rack_update_multiplier(struct tcp_rack *rack, int32_t timely_says, uint64_t last_bw_est,
4508 		       uint32_t rtt, int32_t rtt_diff)
4509 {
4510 	uint64_t cur_bw, up_bnd, low_bnd, subfr;
4511 	uint32_t losses;
4512 
4513 	if ((rack->rc_gp_dyn_mul == 0) ||
4514 	    (rack->use_fixed_rate) ||
4515 	    (rack->in_probe_rtt) ||
4516 	    (rack->rc_always_pace == 0)) {
4517 		/* No dynamic GP multiplier in play */
4518 		return;
4519 	}
4520 	losses = rack->r_ctl.rc_loss_count - rack->r_ctl.rc_loss_at_start;
4521 	cur_bw = rack_get_bw(rack);
4522 	/* Calculate our up and down range */
4523 	up_bnd = rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_up;
4524 	up_bnd /= 100;
4525 	up_bnd += rack->r_ctl.last_gp_comp_bw;
4526 
4527 	subfr = (uint64_t)rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_down;
4528 	subfr /= 100;
4529 	low_bnd = rack->r_ctl.last_gp_comp_bw - subfr;
4530 	if ((timely_says == 2) && (rack->r_ctl.rc_no_push_at_mrtt)) {
4531 		/*
4532 		 * This is the case where our RTT is above
4533 		 * the max target and we have been configured
4534 		 * to just do timely no bonus up stuff in that case.
4535 		 *
4536 		 * There are two configurations, set to 1, and we
4537 		 * just do timely if we are over our max. If its
4538 		 * set above 1 then we slam the multipliers down
4539 		 * to 100 and then decrement per timely.
4540 		 */
4541 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4542 				__LINE__, 3);
4543 		if (rack->r_ctl.rc_no_push_at_mrtt > 1)
4544 			rack_validate_multipliers_at_or_below_100(rack);
4545 		rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff);
4546 	} else if ((timely_says != 0) && (last_bw_est < low_bnd) && !losses) {
4547 		/*
4548 		 * We are decreasing this is a bit complicated this
4549 		 * means we are loosing ground. This could be
4550 		 * because another flow entered and we are competing
4551 		 * for b/w with it. This will push the RTT up which
4552 		 * makes timely unusable unless we want to get shoved
4553 		 * into a corner and just be backed off (the age
4554 		 * old problem with delay based CC).
4555 		 *
4556 		 * On the other hand if it was a route change we
4557 		 * would like to stay somewhat contained and not
4558 		 * blow out the buffers.
4559 		 */
4560 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4561 				__LINE__, 3);
4562 		rack->r_ctl.last_gp_comp_bw = cur_bw;
4563 		if (rack->rc_gp_bwred == 0) {
4564 			/* Go into reduction counting */
4565 			rack->rc_gp_bwred = 1;
4566 			rack->rc_gp_timely_dec_cnt = 0;
4567 		}
4568 		if (rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) {
4569 			/*
4570 			 * Push another time with a faster pacing
4571 			 * to try to gain back (we include override to
4572 			 * get a full raise factor).
4573 			 */
4574 			if ((rack->rc_gp_saw_ca && rack->r_ctl.rack_per_of_gp_ca <= rack_down_raise_thresh) ||
4575 			    (rack->rc_gp_saw_ss && rack->r_ctl.rack_per_of_gp_ss <= rack_down_raise_thresh) ||
4576 			    (timely_says == 0) ||
4577 			    (rack_down_raise_thresh == 0)) {
4578 				/*
4579 				 * Do an override up in b/w if we were
4580 				 * below the threshold or if the threshold
4581 				 * is zero we always do the raise.
4582 				 */
4583 				rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 1);
4584 			} else {
4585 				/* Log it stays the same */
4586 				rack_log_timely(rack,  0, last_bw_est, low_bnd, 0,
4587 						__LINE__, 11);
4588 			}
4589 			rack->rc_gp_timely_dec_cnt++;
4590 			/* We are not incrementing really no-count */
4591 			rack->rc_gp_incr = 0;
4592 			rack->rc_gp_timely_inc_cnt = 0;
4593 		} else {
4594 			/*
4595 			 * Lets just use the RTT
4596 			 * information and give up
4597 			 * pushing.
4598 			 */
4599 			goto use_timely;
4600 		}
4601 	} else if ((timely_says != 2) &&
4602 		    !losses &&
4603 		    (last_bw_est > up_bnd)) {
4604 		/*
4605 		 * We are increasing b/w lets keep going, updating
4606 		 * our b/w and ignoring any timely input, unless
4607 		 * of course we are at our max raise (if there is one).
4608 		 */
4609 
4610 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4611 				__LINE__, 3);
4612 		rack->r_ctl.last_gp_comp_bw = cur_bw;
4613 		if (rack->rc_gp_saw_ss &&
4614 		    rack->r_ctl.rack_per_upper_bound_ss &&
4615 		     (rack->r_ctl.rack_per_of_gp_ss == rack->r_ctl.rack_per_upper_bound_ss)) {
4616 			    /*
4617 			     * In cases where we can't go higher
4618 			     * we should just use timely.
4619 			     */
4620 			    goto use_timely;
4621 		}
4622 		if (rack->rc_gp_saw_ca &&
4623 		    rack->r_ctl.rack_per_upper_bound_ca &&
4624 		    (rack->r_ctl.rack_per_of_gp_ca == rack->r_ctl.rack_per_upper_bound_ca)) {
4625 			    /*
4626 			     * In cases where we can't go higher
4627 			     * we should just use timely.
4628 			     */
4629 			    goto use_timely;
4630 		}
4631 		rack->rc_gp_bwred = 0;
4632 		rack->rc_gp_timely_dec_cnt = 0;
4633 		/* You get a set number of pushes if timely is trying to reduce */
4634 		if ((rack->rc_gp_incr < rack_timely_max_push_rise) || (timely_says == 0)) {
4635 			rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4636 		} else {
4637 			/* Log it stays the same */
4638 			rack_log_timely(rack,  0, last_bw_est, up_bnd, 0,
4639 			    __LINE__, 12);
4640 		}
4641 		return;
4642 	} else {
4643 		/*
4644 		 * We are staying between the lower and upper range bounds
4645 		 * so use timely to decide.
4646 		 */
4647 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4648 				__LINE__, 3);
4649 use_timely:
4650 		if (timely_says) {
4651 			rack->rc_gp_incr = 0;
4652 			rack->rc_gp_timely_inc_cnt = 0;
4653 			if ((rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) &&
4654 			    !losses &&
4655 			    (last_bw_est < low_bnd)) {
4656 				/* We are loosing ground */
4657 				rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4658 				rack->rc_gp_timely_dec_cnt++;
4659 				/* We are not incrementing really no-count */
4660 				rack->rc_gp_incr = 0;
4661 				rack->rc_gp_timely_inc_cnt = 0;
4662 			} else
4663 				rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff);
4664 		} else {
4665 			rack->rc_gp_bwred = 0;
4666 			rack->rc_gp_timely_dec_cnt = 0;
4667 			rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4668 		}
4669 	}
4670 }
4671 
4672 static int32_t
4673 rack_make_timely_judgement(struct tcp_rack *rack, uint32_t rtt, int32_t rtt_diff, uint32_t prev_rtt)
4674 {
4675 	int32_t timely_says;
4676 	uint64_t log_mult, log_rtt_a_diff;
4677 
4678 	log_rtt_a_diff = rtt;
4679 	log_rtt_a_diff <<= 32;
4680 	log_rtt_a_diff |= (uint32_t)rtt_diff;
4681 	if (rtt >= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) *
4682 		    rack_gp_rtt_maxmul)) {
4683 		/* Reduce the b/w multiplier */
4684 		timely_says = 2;
4685 		log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
4686 		log_mult <<= 32;
4687 		log_mult |= prev_rtt;
4688 		rack_log_timely(rack,  timely_says, log_mult,
4689 				get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4690 				log_rtt_a_diff, __LINE__, 4);
4691 	} else if (rtt <= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) +
4692 			   ((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) /
4693 			    max(rack_gp_rtt_mindiv , 1)))) {
4694 		/* Increase the b/w multiplier */
4695 		log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) +
4696 			((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) /
4697 			 max(rack_gp_rtt_mindiv , 1));
4698 		log_mult <<= 32;
4699 		log_mult |= prev_rtt;
4700 		timely_says = 0;
4701 		rack_log_timely(rack,  timely_says, log_mult ,
4702 				get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4703 				log_rtt_a_diff, __LINE__, 5);
4704 	} else {
4705 		/*
4706 		 * Use a gradient to find it the timely gradient
4707 		 * is:
4708 		 * grad = rc_rtt_diff / min_rtt;
4709 		 *
4710 		 * anything below or equal to 0 will be
4711 		 * a increase indication. Anything above
4712 		 * zero is a decrease. Note we take care
4713 		 * of the actual gradient calculation
4714 		 * in the reduction (its not needed for
4715 		 * increase).
4716 		 */
4717 		log_mult = prev_rtt;
4718 		if (rtt_diff <= 0) {
4719 			/*
4720 			 * Rttdiff is less than zero, increase the
4721 			 * b/w multiplier (its 0 or negative)
4722 			 */
4723 			timely_says = 0;
4724 			rack_log_timely(rack,  timely_says, log_mult,
4725 					get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 6);
4726 		} else {
4727 			/* Reduce the b/w multiplier */
4728 			timely_says = 1;
4729 			rack_log_timely(rack,  timely_says, log_mult,
4730 					get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 7);
4731 		}
4732 	}
4733 	return (timely_says);
4734 }
4735 
4736 static __inline int
4737 rack_in_gp_window(struct tcpcb *tp, struct rack_sendmap *rsm)
4738 {
4739 	if (SEQ_GEQ(rsm->r_start, tp->gput_seq) &&
4740 	    SEQ_LEQ(rsm->r_end, tp->gput_ack)) {
4741 		/**
4742 		 * This covers the case that the
4743 		 * resent is completely inside
4744 		 * the gp range or up to it.
4745 		 *      |----------------|
4746 		 *      |-----| <or>
4747 		 *            |----|
4748 		 *            <or>   |---|
4749 		 */
4750 		return (1);
4751 	} else if (SEQ_LT(rsm->r_start, tp->gput_seq) &&
4752 		   SEQ_GT(rsm->r_end, tp->gput_seq)){
4753 		/**
4754 		 * This covers the case of
4755 		 *      |--------------|
4756 		 *  |-------->|
4757 		 */
4758 		return (1);
4759 	} else if (SEQ_GEQ(rsm->r_start, tp->gput_seq) &&
4760 		   SEQ_LT(rsm->r_start, tp->gput_ack) &&
4761 		   SEQ_GEQ(rsm->r_end, tp->gput_ack)) {
4762 
4763 		/**
4764 		 * This covers the case of
4765 		 *      |--------------|
4766 		 *              |-------->|
4767 		 */
4768 		return (1);
4769 	}
4770 	return (0);
4771 }
4772 
4773 static __inline void
4774 rack_mark_in_gp_win(struct tcpcb *tp, struct rack_sendmap *rsm)
4775 {
4776 
4777 	if ((tp->t_flags & TF_GPUTINPROG) == 0)
4778 		return;
4779 	/*
4780 	 * We have a Goodput measurement in progress. Mark
4781 	 * the send if its within the window. If its not
4782 	 * in the window make sure it does not have the mark.
4783 	 */
4784 	if (rack_in_gp_window(tp, rsm))
4785 		rsm->r_flags |= RACK_IN_GP_WIN;
4786 	else
4787 		rsm->r_flags &= ~RACK_IN_GP_WIN;
4788 }
4789 
4790 static __inline void
4791 rack_clear_gp_marks(struct tcpcb *tp, struct tcp_rack *rack)
4792 {
4793 	/* A GP measurement is ending, clear all marks on the send map*/
4794 	struct rack_sendmap *rsm = NULL;
4795 
4796 	rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
4797 	if (rsm == NULL) {
4798 		rsm = tqhash_min(rack->r_ctl.tqh);
4799 	}
4800 	/* Nothing left? */
4801 	while ((rsm != NULL) && (SEQ_GEQ(tp->gput_ack, rsm->r_start))){
4802 		rsm->r_flags &= ~RACK_IN_GP_WIN;
4803 		rsm = tqhash_next(rack->r_ctl.tqh, rsm);
4804 	}
4805 }
4806 
4807 
4808 static __inline void
4809 rack_tend_gp_marks(struct tcpcb *tp, struct tcp_rack *rack)
4810 {
4811 	struct rack_sendmap *rsm = NULL;
4812 
4813 	if (tp->snd_una == tp->snd_max) {
4814 		/* Nothing outstanding yet, nothing to do here */
4815 		return;
4816 	}
4817 	if (SEQ_GT(tp->gput_seq, tp->snd_una)) {
4818 		/*
4819 		 * We are measuring ahead of some outstanding
4820 		 * data. We need to walk through up until we get
4821 		 * to gp_seq marking so that no rsm is set incorrectly
4822 		 * with RACK_IN_GP_WIN.
4823 		 */
4824 		rsm = tqhash_min(rack->r_ctl.tqh);
4825 		while (rsm != NULL) {
4826 			rack_mark_in_gp_win(tp, rsm);
4827 			if (SEQ_GEQ(rsm->r_end, tp->gput_seq))
4828 				break;
4829 			rsm = tqhash_next(rack->r_ctl.tqh, rsm);
4830 		}
4831 	}
4832 	if (rsm == NULL) {
4833 		/*
4834 		 * Need to find the GP seq, if rsm is
4835 		 * set we stopped as we hit it.
4836 		 */
4837 		rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
4838 		if (rsm == NULL)
4839 			return;
4840 		rack_mark_in_gp_win(tp, rsm);
4841 	}
4842 	/*
4843 	 * Now we may need to mark already sent rsm, ahead of
4844 	 * gput_seq in the window since they may have been sent
4845 	 * *before* we started our measurment. The rsm, if non-null
4846 	 * has been marked (note if rsm would have been NULL we would have
4847 	 * returned in the previous block). So we go to the next, and continue
4848 	 * until we run out of entries or we exceed the gp_ack value.
4849 	 */
4850 	rsm = tqhash_next(rack->r_ctl.tqh, rsm);
4851 	while (rsm) {
4852 		rack_mark_in_gp_win(tp, rsm);
4853 		if (SEQ_GT(rsm->r_end, tp->gput_ack))
4854 			break;
4855 		rsm = tqhash_next(rack->r_ctl.tqh, rsm);
4856 	}
4857 }
4858 
4859 static void
4860 rack_log_gp_calc(struct tcp_rack *rack, uint32_t add_part, uint32_t sub_part, uint32_t srtt, uint64_t meas_bw, uint64_t utim, uint8_t meth, uint32_t line)
4861 {
4862 	if (tcp_bblogging_on(rack->rc_tp)) {
4863 		union tcp_log_stackspecific log;
4864 		struct timeval tv;
4865 
4866 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
4867 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
4868 		log.u_bbr.flex1 = add_part;
4869 		log.u_bbr.flex2 = sub_part;
4870 		log.u_bbr.flex3 = rack_wma_divisor;
4871 		log.u_bbr.flex4 = srtt;
4872 		log.u_bbr.flex7 = (uint16_t)line;
4873 		log.u_bbr.flex8 = meth;
4874 		log.u_bbr.delRate = rack->r_ctl.gp_bw;
4875 		log.u_bbr.cur_del_rate = meas_bw;
4876 		log.u_bbr.rttProp = utim;
4877 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
4878 		    &rack->rc_inp->inp_socket->so_rcv,
4879 		    &rack->rc_inp->inp_socket->so_snd,
4880 		    BBR_LOG_THRESH_CALC, 0,
4881 		    0, &log, false, &rack->r_ctl.act_rcv_time);
4882 	}
4883 }
4884 
4885 static void
4886 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
4887 			    tcp_seq th_ack, int line, uint8_t quality)
4888 {
4889 	uint64_t tim, bytes_ps, stim, utim;
4890 	uint32_t segsiz, bytes, reqbytes, us_cts;
4891 	int32_t gput, new_rtt_diff, timely_says;
4892 	uint64_t  resid_bw, subpart = 0, addpart = 0, srtt;
4893 	int did_add = 0;
4894 
4895 	us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
4896 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
4897 	if (TSTMP_GEQ(us_cts, tp->gput_ts))
4898 		tim = us_cts - tp->gput_ts;
4899 	else
4900 		tim = 0;
4901 	if (rack->r_ctl.rc_gp_cumack_ts > rack->r_ctl.rc_gp_output_ts)
4902 		stim = rack->r_ctl.rc_gp_cumack_ts - rack->r_ctl.rc_gp_output_ts;
4903 	else
4904 		stim = 0;
4905 	/*
4906 	 * Use the larger of the send time or ack time. This prevents us
4907 	 * from being influenced by ack artifacts to come up with too
4908 	 * high of measurement. Note that since we are spanning over many more
4909 	 * bytes in most of our measurements hopefully that is less likely to
4910 	 * occur.
4911 	 */
4912 	if (tim > stim)
4913 		utim = max(tim, 1);
4914 	else
4915 		utim = max(stim, 1);
4916 	reqbytes = min(rc_init_window(rack), (MIN_GP_WIN * segsiz));
4917 	rack_log_gpset(rack, th_ack, us_cts, rack->r_ctl.rc_gp_cumack_ts, __LINE__, 3, NULL);
4918 	if ((tim == 0) && (stim == 0)) {
4919 		/*
4920 		 * Invalid measurement time, maybe
4921 		 * all on one ack/one send?
4922 		 */
4923 		bytes = 0;
4924 		bytes_ps = 0;
4925 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4926 					   0, 0, 0, 10, __LINE__, NULL, quality);
4927 		goto skip_measurement;
4928 	}
4929 	if (rack->r_ctl.rc_gp_lowrtt == 0xffffffff) {
4930 		/* We never made a us_rtt measurement? */
4931 		bytes = 0;
4932 		bytes_ps = 0;
4933 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4934 					   0, 0, 0, 10, __LINE__, NULL, quality);
4935 		goto skip_measurement;
4936 	}
4937 	/*
4938 	 * Calculate the maximum possible b/w this connection
4939 	 * could have. We base our calculation on the lowest
4940 	 * rtt we have seen during the measurement and the
4941 	 * largest rwnd the client has given us in that time. This
4942 	 * forms a BDP that is the maximum that we could ever
4943 	 * get to the client. Anything larger is not valid.
4944 	 *
4945 	 * I originally had code here that rejected measurements
4946 	 * where the time was less than 1/2 the latest us_rtt.
4947 	 * But after thinking on that I realized its wrong since
4948 	 * say you had a 150Mbps or even 1Gbps link, and you
4949 	 * were a long way away.. example I am in Europe (100ms rtt)
4950 	 * talking to my 1Gbps link in S.C. Now measuring say 150,000
4951 	 * bytes my time would be 1.2ms, and yet my rtt would say
4952 	 * the measurement was invalid the time was < 50ms. The
4953 	 * same thing is true for 150Mb (8ms of time).
4954 	 *
4955 	 * A better way I realized is to look at what the maximum
4956 	 * the connection could possibly do. This is gated on
4957 	 * the lowest RTT we have seen and the highest rwnd.
4958 	 * We should in theory never exceed that, if we are
4959 	 * then something on the path is storing up packets
4960 	 * and then feeding them all at once to our endpoint
4961 	 * messing up our measurement.
4962 	 */
4963 	rack->r_ctl.last_max_bw = rack->r_ctl.rc_gp_high_rwnd;
4964 	rack->r_ctl.last_max_bw *= HPTS_USEC_IN_SEC;
4965 	rack->r_ctl.last_max_bw /= rack->r_ctl.rc_gp_lowrtt;
4966 	if (SEQ_LT(th_ack, tp->gput_seq)) {
4967 		/* No measurement can be made */
4968 		bytes = 0;
4969 		bytes_ps = 0;
4970 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4971 					   0, 0, 0, 10, __LINE__, NULL, quality);
4972 		goto skip_measurement;
4973 	} else
4974 		bytes = (th_ack - tp->gput_seq);
4975 	bytes_ps = (uint64_t)bytes;
4976 	/*
4977 	 * Don't measure a b/w for pacing unless we have gotten at least
4978 	 * an initial windows worth of data in this measurement interval.
4979 	 *
4980 	 * Small numbers of bytes get badly influenced by delayed ack and
4981 	 * other artifacts. Note we take the initial window or our
4982 	 * defined minimum GP (defaulting to 10 which hopefully is the
4983 	 * IW).
4984 	 */
4985 	if (rack->rc_gp_filled == 0) {
4986 		/*
4987 		 * The initial estimate is special. We
4988 		 * have blasted out an IW worth of packets
4989 		 * without a real valid ack ts results. We
4990 		 * then setup the app_limited_needs_set flag,
4991 		 * this should get the first ack in (probably 2
4992 		 * MSS worth) to be recorded as the timestamp.
4993 		 * We thus allow a smaller number of bytes i.e.
4994 		 * IW - 2MSS.
4995 		 */
4996 		reqbytes -= (2 * segsiz);
4997 		/* Also lets fill previous for our first measurement to be neutral */
4998 		rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt;
4999 	}
5000 	if ((bytes_ps < reqbytes) || rack->app_limited_needs_set) {
5001 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
5002 					   rack->r_ctl.rc_app_limited_cnt,
5003 					   0, 0, 10, __LINE__, NULL, quality);
5004 		goto skip_measurement;
5005 	}
5006 	/*
5007 	 * We now need to calculate the Timely like status so
5008 	 * we can update (possibly) the b/w multipliers.
5009 	 */
5010 	new_rtt_diff = (int32_t)rack->r_ctl.rc_gp_srtt - (int32_t)rack->r_ctl.rc_prev_gp_srtt;
5011 	if (rack->rc_gp_filled == 0) {
5012 		/* No previous reading */
5013 		rack->r_ctl.rc_rtt_diff = new_rtt_diff;
5014 	} else {
5015 		if (rack->measure_saw_probe_rtt == 0) {
5016 			/*
5017 			 * We don't want a probertt to be counted
5018 			 * since it will be negative incorrectly. We
5019 			 * expect to be reducing the RTT when we
5020 			 * pace at a slower rate.
5021 			 */
5022 			rack->r_ctl.rc_rtt_diff -= (rack->r_ctl.rc_rtt_diff / 8);
5023 			rack->r_ctl.rc_rtt_diff += (new_rtt_diff / 8);
5024 		}
5025 	}
5026 	timely_says = rack_make_timely_judgement(rack,
5027 	    rack->r_ctl.rc_gp_srtt,
5028 	    rack->r_ctl.rc_rtt_diff,
5029 	    rack->r_ctl.rc_prev_gp_srtt
5030 	);
5031 	bytes_ps *= HPTS_USEC_IN_SEC;
5032 	bytes_ps /= utim;
5033 	if (bytes_ps > rack->r_ctl.last_max_bw) {
5034 		/*
5035 		 * Something is on path playing
5036 		 * since this b/w is not possible based
5037 		 * on our BDP (highest rwnd and lowest rtt
5038 		 * we saw in the measurement window).
5039 		 *
5040 		 * Another option here would be to
5041 		 * instead skip the measurement.
5042 		 */
5043 		rack_log_pacing_delay_calc(rack, bytes, reqbytes,
5044 					   bytes_ps, rack->r_ctl.last_max_bw, 0,
5045 					   11, __LINE__, NULL, quality);
5046 		bytes_ps = rack->r_ctl.last_max_bw;
5047 	}
5048 	/* We store gp for b/w in bytes per second */
5049 	if (rack->rc_gp_filled == 0) {
5050 		/* Initial measurement */
5051 		if (bytes_ps) {
5052 			rack->r_ctl.gp_bw = bytes_ps;
5053 			rack->rc_gp_filled = 1;
5054 			rack->r_ctl.num_measurements = 1;
5055 			rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
5056 		} else {
5057 			rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
5058 						   rack->r_ctl.rc_app_limited_cnt,
5059 						   0, 0, 10, __LINE__, NULL, quality);
5060 		}
5061 		if (tcp_in_hpts(rack->rc_tp) &&
5062 		    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
5063 			/*
5064 			 * Ok we can't trust the pacer in this case
5065 			 * where we transition from un-paced to paced.
5066 			 * Or for that matter when the burst mitigation
5067 			 * was making a wild guess and got it wrong.
5068 			 * Stop the pacer and clear up all the aggregate
5069 			 * delays etc.
5070 			 */
5071 			tcp_hpts_remove(rack->rc_tp);
5072 			rack->r_ctl.rc_hpts_flags = 0;
5073 			rack->r_ctl.rc_last_output_to = 0;
5074 		}
5075 		did_add = 2;
5076 	} else if (rack->r_ctl.num_measurements < RACK_REQ_AVG) {
5077 		/* Still a small number run an average */
5078 		rack->r_ctl.gp_bw += bytes_ps;
5079 		addpart = rack->r_ctl.num_measurements;
5080 		rack->r_ctl.num_measurements++;
5081 		if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) {
5082 			/* We have collected enough to move forward */
5083 			rack->r_ctl.gp_bw /= (uint64_t)rack->r_ctl.num_measurements;
5084 		}
5085 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
5086 		did_add = 3;
5087 	} else {
5088 		/*
5089 		 * We want to take 1/wma of the goodput and add in to 7/8th
5090 		 * of the old value weighted by the srtt. So if your measurement
5091 		 * period is say 2 SRTT's long you would get 1/4 as the
5092 		 * value, if it was like 1/2 SRTT then you would get 1/16th.
5093 		 *
5094 		 * But we must be careful not to take too much i.e. if the
5095 		 * srtt is say 20ms and the measurement is taken over
5096 		 * 400ms our weight would be 400/20 i.e. 20. On the
5097 		 * other hand if we get a measurement over 1ms with a
5098 		 * 10ms rtt we only want to take a much smaller portion.
5099 		 */
5100 		uint8_t meth;
5101 
5102 		if (rack->r_ctl.num_measurements < 0xff) {
5103 			rack->r_ctl.num_measurements++;
5104 		}
5105 		srtt = (uint64_t)tp->t_srtt;
5106 		if (srtt == 0) {
5107 			/*
5108 			 * Strange why did t_srtt go back to zero?
5109 			 */
5110 			if (rack->r_ctl.rc_rack_min_rtt)
5111 				srtt = rack->r_ctl.rc_rack_min_rtt;
5112 			else
5113 				srtt = HPTS_USEC_IN_MSEC;
5114 		}
5115 		/*
5116 		 * XXXrrs: Note for reviewers, in playing with
5117 		 * dynamic pacing I discovered this GP calculation
5118 		 * as done originally leads to some undesired results.
5119 		 * Basically you can get longer measurements contributing
5120 		 * too much to the WMA. Thus I changed it if you are doing
5121 		 * dynamic adjustments to only do the aportioned adjustment
5122 		 * if we have a very small (time wise) measurement. Longer
5123 		 * measurements just get there weight (defaulting to 1/8)
5124 		 * add to the WMA. We may want to think about changing
5125 		 * this to always do that for both sides i.e. dynamic
5126 		 * and non-dynamic... but considering lots of folks
5127 		 * were playing with this I did not want to change the
5128 		 * calculation per.se. without your thoughts.. Lawerence?
5129 		 * Peter??
5130 		 */
5131 		if (rack->rc_gp_dyn_mul == 0) {
5132 			subpart = rack->r_ctl.gp_bw * utim;
5133 			subpart /= (srtt * 8);
5134 			if (subpart < (rack->r_ctl.gp_bw / 2)) {
5135 				/*
5136 				 * The b/w update takes no more
5137 				 * away then 1/2 our running total
5138 				 * so factor it in.
5139 				 */
5140 				addpart = bytes_ps * utim;
5141 				addpart /= (srtt * 8);
5142 				meth = 1;
5143 			} else {
5144 				/*
5145 				 * Don't allow a single measurement
5146 				 * to account for more than 1/2 of the
5147 				 * WMA. This could happen on a retransmission
5148 				 * where utim becomes huge compared to
5149 				 * srtt (multiple retransmissions when using
5150 				 * the sending rate which factors in all the
5151 				 * transmissions from the first one).
5152 				 */
5153 				subpart = rack->r_ctl.gp_bw / 2;
5154 				addpart = bytes_ps / 2;
5155 				meth = 2;
5156 			}
5157 			rack_log_gp_calc(rack, addpart, subpart, srtt, bytes_ps, utim, meth, __LINE__);
5158 			resid_bw = rack->r_ctl.gp_bw - subpart;
5159 			rack->r_ctl.gp_bw = resid_bw + addpart;
5160 			did_add = 1;
5161 		} else {
5162 			if ((utim / srtt) <= 1) {
5163 				/*
5164 				 * The b/w update was over a small period
5165 				 * of time. The idea here is to prevent a small
5166 				 * measurement time period from counting
5167 				 * too much. So we scale it based on the
5168 				 * time so it attributes less than 1/rack_wma_divisor
5169 				 * of its measurement.
5170 				 */
5171 				subpart = rack->r_ctl.gp_bw * utim;
5172 				subpart /= (srtt * rack_wma_divisor);
5173 				addpart = bytes_ps * utim;
5174 				addpart /= (srtt * rack_wma_divisor);
5175 				meth = 3;
5176 			} else {
5177 				/*
5178 				 * The scaled measurement was long
5179 				 * enough so lets just add in the
5180 				 * portion of the measurement i.e. 1/rack_wma_divisor
5181 				 */
5182 				subpart = rack->r_ctl.gp_bw / rack_wma_divisor;
5183 				addpart = bytes_ps / rack_wma_divisor;
5184 				meth = 4;
5185 			}
5186 			if ((rack->measure_saw_probe_rtt == 0) ||
5187 		            (bytes_ps > rack->r_ctl.gp_bw)) {
5188 				/*
5189 				 * For probe-rtt we only add it in
5190 				 * if its larger, all others we just
5191 				 * add in.
5192 				 */
5193 				did_add = 1;
5194 				rack_log_gp_calc(rack, addpart, subpart, srtt, bytes_ps, utim, meth, __LINE__);
5195 				resid_bw = rack->r_ctl.gp_bw - subpart;
5196 				rack->r_ctl.gp_bw = resid_bw + addpart;
5197 			}
5198 		}
5199 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
5200 	}
5201 	/*
5202 	 * We only watch the growth of the GP during the initial startup
5203 	 * or first-slowstart that ensues. If we ever needed to watch
5204 	 * growth of gp outside of that period all we need to do is
5205 	 * remove the first clause of this if (rc_initial_ss_comp).
5206 	 */
5207 	if ((rack->rc_initial_ss_comp == 0) &&
5208 	    (rack->r_ctl.num_measurements >= RACK_REQ_AVG)) {
5209 		uint64_t gp_est;
5210 
5211 		gp_est = bytes_ps;
5212 		if (tcp_bblogging_on(rack->rc_tp)) {
5213 			union tcp_log_stackspecific log;
5214 			struct timeval tv;
5215 
5216 			memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5217 			log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5218 			log.u_bbr.flex1 = rack->r_ctl.current_round;
5219 			log.u_bbr.flex2 = rack->r_ctl.last_rnd_of_gp_rise;
5220 			log.u_bbr.delRate = gp_est;
5221 			log.u_bbr.cur_del_rate = rack->r_ctl.last_gpest;
5222 			log.u_bbr.flex8 = 41;
5223 			(void)tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
5224 					    0, &log, false, NULL, __func__, __LINE__,&tv);
5225 		}
5226 		if ((rack->r_ctl.num_measurements == RACK_REQ_AVG) ||
5227 		    (rack->r_ctl.last_gpest == 0)) {
5228 			/*
5229 			 * The round we get our measurement averaging going
5230 			 * is the base round so it always is the source point
5231 			 * for when we had our first increment. From there on
5232 			 * we only record the round that had a rise.
5233 			 */
5234 			rack->r_ctl.last_rnd_of_gp_rise = rack->r_ctl.current_round;
5235 			rack->r_ctl.last_gpest = rack->r_ctl.gp_bw;
5236 		} else if (gp_est >= rack->r_ctl.last_gpest) {
5237 			/*
5238 			 * Test to see if its gone up enough
5239 			 * to set the round count up to now. Note
5240 			 * that on the seeding of the 4th measurement we
5241 			 */
5242 			gp_est *= 1000;
5243 			gp_est /= rack->r_ctl.last_gpest;
5244 			if ((uint32_t)gp_est > rack->r_ctl.gp_gain_req) {
5245 				/*
5246 				 * We went up enough to record the round.
5247 				 */
5248 				if (tcp_bblogging_on(rack->rc_tp)) {
5249 					union tcp_log_stackspecific log;
5250 					struct timeval tv;
5251 
5252 					memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5253 					log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5254 					log.u_bbr.flex1 = rack->r_ctl.current_round;
5255 					log.u_bbr.flex2 = (uint32_t)gp_est;
5256 					log.u_bbr.flex3 = rack->r_ctl.gp_gain_req;
5257 					log.u_bbr.delRate = gp_est;
5258 					log.u_bbr.cur_del_rate = rack->r_ctl.last_gpest;
5259 					log.u_bbr.flex8 = 42;
5260 					(void)tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
5261 							    0, &log, false, NULL, __func__, __LINE__,&tv);
5262 				}
5263 				rack->r_ctl.last_rnd_of_gp_rise = rack->r_ctl.current_round;
5264 				if (rack->r_ctl.use_gp_not_last == 1)
5265 					rack->r_ctl.last_gpest = rack->r_ctl.gp_bw;
5266 				else
5267 					rack->r_ctl.last_gpest = bytes_ps;
5268 			}
5269 		}
5270 	}
5271 	if ((rack->gp_ready == 0) &&
5272 	    (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) {
5273 		/* We have enough measurements now */
5274 		rack->gp_ready = 1;
5275 		if (rack->dgp_on ||
5276 		    rack->rack_hibeta)
5277 			rack_set_cc_pacing(rack);
5278 		if (rack->defer_options)
5279 			rack_apply_deferred_options(rack);
5280 	}
5281 	rack_log_pacing_delay_calc(rack, subpart, addpart, bytes_ps, stim,
5282 				   rack_get_bw(rack), 22, did_add, NULL, quality);
5283 	/* We do not update any multipliers if we are in or have seen a probe-rtt */
5284 
5285 	if ((rack->measure_saw_probe_rtt == 0) &&
5286 	    rack->rc_gp_rtt_set) {
5287 		if (rack->rc_skip_timely == 0) {
5288 			rack_update_multiplier(rack, timely_says, bytes_ps,
5289 					       rack->r_ctl.rc_gp_srtt,
5290 					       rack->r_ctl.rc_rtt_diff);
5291 		}
5292 	}
5293 	rack_log_pacing_delay_calc(rack, bytes, tim, bytes_ps, stim,
5294 				   rack_get_bw(rack), 3, line, NULL, quality);
5295 	rack_log_pacing_delay_calc(rack,
5296 				   bytes, /* flex2 */
5297 				   tim, /* flex1 */
5298 				   bytes_ps, /* bw_inuse */
5299 				   rack->r_ctl.gp_bw, /* delRate */
5300 				   rack_get_lt_bw(rack), /* rttProp */
5301 				   20, line, NULL, 0);
5302 	/* reset the gp srtt and setup the new prev */
5303 	rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt;
5304 	/* Record the lost count for the next measurement */
5305 	rack->r_ctl.rc_loss_at_start = rack->r_ctl.rc_loss_count;
5306 skip_measurement:
5307 	/*
5308 	 * We restart our diffs based on the gpsrtt in the
5309 	 * measurement window.
5310 	 */
5311 	rack->rc_gp_rtt_set = 0;
5312 	rack->rc_gp_saw_rec = 0;
5313 	rack->rc_gp_saw_ca = 0;
5314 	rack->rc_gp_saw_ss = 0;
5315 	rack->rc_dragged_bottom = 0;
5316 	if (quality == RACK_QUALITY_HIGH) {
5317 		/*
5318 		 * Gput in the stats world is in kbps where bytes_ps is
5319 		 * bytes per second so we do ((x * 8)/ 1000).
5320 		 */
5321 		gput = (int32_t)((bytes_ps << 3) / (uint64_t)1000);
5322 #ifdef STATS
5323 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
5324 					 gput);
5325 		/*
5326 		 * XXXLAS: This is a temporary hack, and should be
5327 		 * chained off VOI_TCP_GPUT when stats(9) grows an
5328 		 * API to deal with chained VOIs.
5329 		 */
5330 		if (tp->t_stats_gput_prev > 0)
5331 			stats_voi_update_abs_s32(tp->t_stats,
5332 						 VOI_TCP_GPUT_ND,
5333 						 ((gput - tp->t_stats_gput_prev) * 100) /
5334 						 tp->t_stats_gput_prev);
5335 #endif
5336 		tp->t_stats_gput_prev = gput;
5337 	}
5338 	tp->t_flags &= ~TF_GPUTINPROG;
5339 	/*
5340 	 * Now are we app limited now and there is space from where we
5341 	 * were to where we want to go?
5342 	 *
5343 	 * We don't do the other case i.e. non-applimited here since
5344 	 * the next send will trigger us picking up the missing data.
5345 	 */
5346 	if (rack->r_ctl.rc_first_appl &&
5347 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
5348 	    rack->r_ctl.rc_app_limited_cnt &&
5349 	    (SEQ_GT(rack->r_ctl.rc_first_appl->r_start, th_ack)) &&
5350 	    ((rack->r_ctl.rc_first_appl->r_end - th_ack) >
5351 	     max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) {
5352 		/*
5353 		 * Yep there is enough outstanding to make a measurement here.
5354 		 */
5355 		struct rack_sendmap *rsm;
5356 
5357 		rack->r_ctl.rc_gp_lowrtt = 0xffffffff;
5358 		rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
5359 		tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
5360 		rack->app_limited_needs_set = 0;
5361 		tp->gput_seq = th_ack;
5362 		if (rack->in_probe_rtt)
5363 			rack->measure_saw_probe_rtt = 1;
5364 		else if ((rack->measure_saw_probe_rtt) &&
5365 			 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
5366 			rack->measure_saw_probe_rtt = 0;
5367 		if ((rack->r_ctl.rc_first_appl->r_end - th_ack) >= rack_get_measure_window(tp, rack)) {
5368 			/* There is a full window to gain info from */
5369 			tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
5370 		} else {
5371 			/* We can only measure up to the applimited point */
5372 			tp->gput_ack = tp->gput_seq + (rack->r_ctl.rc_first_appl->r_end - th_ack);
5373 			if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) {
5374 				/*
5375 				 * We don't have enough to make a measurement.
5376 				 */
5377 				tp->t_flags &= ~TF_GPUTINPROG;
5378 				rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq,
5379 							   0, 0, 0, 6, __LINE__, NULL, quality);
5380 				return;
5381 			}
5382 		}
5383 		if (tp->t_state >= TCPS_FIN_WAIT_1) {
5384 			/*
5385 			 * We will get no more data into the SB
5386 			 * this means we need to have the data available
5387 			 * before we start a measurement.
5388 			 */
5389 			if (sbavail(&tptosocket(tp)->so_snd) < (tp->gput_ack - tp->gput_seq)) {
5390 				/* Nope not enough data. */
5391 				return;
5392 			}
5393 		}
5394 		tp->t_flags |= TF_GPUTINPROG;
5395 		/*
5396 		 * Now we need to find the timestamp of the send at tp->gput_seq
5397 		 * for the send based measurement.
5398 		 */
5399 		rack->r_ctl.rc_gp_cumack_ts = 0;
5400 		rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
5401 		if (rsm) {
5402 			/* Ok send-based limit is set */
5403 			if (SEQ_LT(rsm->r_start, tp->gput_seq)) {
5404 				/*
5405 				 * Move back to include the earlier part
5406 				 * so our ack time lines up right (this may
5407 				 * make an overlapping measurement but thats
5408 				 * ok).
5409 				 */
5410 				tp->gput_seq = rsm->r_start;
5411 			}
5412 			if (rsm->r_flags & RACK_ACKED) {
5413 				struct rack_sendmap *nrsm;
5414 
5415 				tp->gput_ts = (uint32_t)rsm->r_ack_arrival;
5416 				tp->gput_seq = rsm->r_end;
5417 				nrsm = tqhash_next(rack->r_ctl.tqh, rsm);
5418 				if (nrsm)
5419 					rsm = nrsm;
5420 				else {
5421 					rack->app_limited_needs_set = 1;
5422 				}
5423 			} else
5424 				rack->app_limited_needs_set = 1;
5425 			/* We always go from the first send */
5426 			rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[0];
5427 		} else {
5428 			/*
5429 			 * If we don't find the rsm due to some
5430 			 * send-limit set the current time, which
5431 			 * basically disables the send-limit.
5432 			 */
5433 			struct timeval tv;
5434 
5435 			microuptime(&tv);
5436 			rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
5437 		}
5438 		rack_tend_gp_marks(tp, rack);
5439 		rack_log_pacing_delay_calc(rack,
5440 					   tp->gput_seq,
5441 					   tp->gput_ack,
5442 					   (uintptr_t)rsm,
5443 					   tp->gput_ts,
5444 					   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
5445 					   9,
5446 					   __LINE__, rsm, quality);
5447 		rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
5448 	} else {
5449 		/*
5450 		 * To make sure proper timestamp merging occurs, we need to clear
5451 		 * all GP marks if we don't start a measurement.
5452 		 */
5453 		rack_clear_gp_marks(tp, rack);
5454 	}
5455 }
5456 
5457 /*
5458  * CC wrapper hook functions
5459  */
5460 static void
5461 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack, uint32_t th_ack, uint16_t nsegs,
5462     uint16_t type, int32_t post_recovery)
5463 {
5464 	uint32_t prior_cwnd, acked;
5465 	struct tcp_log_buffer *lgb = NULL;
5466 	uint8_t labc_to_use, quality;
5467 
5468 	INP_WLOCK_ASSERT(tptoinpcb(tp));
5469 	tp->t_ccv.nsegs = nsegs;
5470 	acked = tp->t_ccv.bytes_this_ack = (th_ack - tp->snd_una);
5471 	if ((post_recovery) && (rack->r_ctl.rc_early_recovery_segs)) {
5472 		uint32_t max;
5473 
5474 		max = rack->r_ctl.rc_early_recovery_segs * ctf_fixed_maxseg(tp);
5475 		if (tp->t_ccv.bytes_this_ack > max) {
5476 			tp->t_ccv.bytes_this_ack = max;
5477 		}
5478 	}
5479 #ifdef STATS
5480 	stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF,
5481 	    ((int32_t)rack->r_ctl.cwnd_to_use) - tp->snd_wnd);
5482 #endif
5483 	if ((th_ack == tp->snd_max) && rack->lt_bw_up) {
5484 		/*
5485 		 * We will ack all the data, time to end any
5486 		 * lt_bw_up we have running until something
5487 		 * new is sent. Note we need to use the actual
5488 		 * ack_rcv_time which with pacing may be different.
5489 		 */
5490 		uint64_t tmark;
5491 
5492 		rack->r_ctl.lt_bw_bytes += (tp->snd_max - rack->r_ctl.lt_seq);
5493 		rack->r_ctl.lt_seq = tp->snd_max;
5494 		tmark = tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time);
5495 		if (tmark >= rack->r_ctl.lt_timemark) {
5496 			rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark);
5497 		}
5498 		rack->r_ctl.lt_timemark = tmark;
5499 		rack->lt_bw_up = 0;
5500 	}
5501 	quality = RACK_QUALITY_NONE;
5502 	if ((tp->t_flags & TF_GPUTINPROG) &&
5503 	    rack_enough_for_measurement(tp, rack, th_ack, &quality)) {
5504 		/* Measure the Goodput */
5505 		rack_do_goodput_measurement(tp, rack, th_ack, __LINE__, quality);
5506 	}
5507 	/* Which way our we limited, if not cwnd limited no advance in CA */
5508 	if (tp->snd_cwnd <= tp->snd_wnd)
5509 		tp->t_ccv.flags |= CCF_CWND_LIMITED;
5510 	else
5511 		tp->t_ccv.flags &= ~CCF_CWND_LIMITED;
5512 	if (tp->snd_cwnd > tp->snd_ssthresh) {
5513 		tp->t_bytes_acked += min(tp->t_ccv.bytes_this_ack,
5514 			 nsegs * V_tcp_abc_l_var * ctf_fixed_maxseg(tp));
5515 		/* For the setting of a window past use the actual scwnd we are using */
5516 		if (tp->t_bytes_acked >= rack->r_ctl.cwnd_to_use) {
5517 			tp->t_bytes_acked -= rack->r_ctl.cwnd_to_use;
5518 			tp->t_ccv.flags |= CCF_ABC_SENTAWND;
5519 		}
5520 	} else {
5521 		tp->t_ccv.flags &= ~CCF_ABC_SENTAWND;
5522 		tp->t_bytes_acked = 0;
5523 	}
5524 	prior_cwnd = tp->snd_cwnd;
5525 	if ((post_recovery == 0) || (rack_max_abc_post_recovery == 0) || rack->r_use_labc_for_rec ||
5526 	    (rack_client_low_buf && rack->client_bufferlvl &&
5527 	    (rack->client_bufferlvl < rack_client_low_buf)))
5528 		labc_to_use = rack->rc_labc;
5529 	else
5530 		labc_to_use = rack_max_abc_post_recovery;
5531 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
5532 		union tcp_log_stackspecific log;
5533 		struct timeval tv;
5534 
5535 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5536 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5537 		log.u_bbr.flex1 = th_ack;
5538 		log.u_bbr.flex2 = tp->t_ccv.flags;
5539 		log.u_bbr.flex3 = tp->t_ccv.bytes_this_ack;
5540 		log.u_bbr.flex4 = tp->t_ccv.nsegs;
5541 		log.u_bbr.flex5 = labc_to_use;
5542 		log.u_bbr.flex6 = prior_cwnd;
5543 		log.u_bbr.flex7 = V_tcp_do_newsack;
5544 		log.u_bbr.flex8 = 1;
5545 		lgb = tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
5546 				     0, &log, false, NULL, __func__, __LINE__,&tv);
5547 	}
5548 	if (CC_ALGO(tp)->ack_received != NULL) {
5549 		/* XXXLAS: Find a way to live without this */
5550 		tp->t_ccv.curack = th_ack;
5551 		tp->t_ccv.labc = labc_to_use;
5552 		tp->t_ccv.flags |= CCF_USE_LOCAL_ABC;
5553 		CC_ALGO(tp)->ack_received(&tp->t_ccv, type);
5554 	}
5555 	if (lgb) {
5556 		lgb->tlb_stackinfo.u_bbr.flex6 = tp->snd_cwnd;
5557 	}
5558 	if (rack->r_must_retran) {
5559 		if (SEQ_GEQ(th_ack, rack->r_ctl.rc_snd_max_at_rto)) {
5560 			/*
5561 			 * We now are beyond the rxt point so lets disable
5562 			 * the flag.
5563 			 */
5564 			rack->r_ctl.rc_out_at_rto = 0;
5565 			rack->r_must_retran = 0;
5566 		} else if ((prior_cwnd + ctf_fixed_maxseg(tp)) <= tp->snd_cwnd) {
5567 			/*
5568 			 * Only decrement the rc_out_at_rto if the cwnd advances
5569 			 * at least a whole segment. Otherwise next time the peer
5570 			 * acks, we won't be able to send this generaly happens
5571 			 * when we are in Congestion Avoidance.
5572 			 */
5573 			if (acked <= rack->r_ctl.rc_out_at_rto){
5574 				rack->r_ctl.rc_out_at_rto -= acked;
5575 			} else {
5576 				rack->r_ctl.rc_out_at_rto = 0;
5577 			}
5578 		}
5579 	}
5580 #ifdef STATS
5581 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, rack->r_ctl.cwnd_to_use);
5582 #endif
5583 	if (rack->r_ctl.rc_rack_largest_cwnd < rack->r_ctl.cwnd_to_use) {
5584 		rack->r_ctl.rc_rack_largest_cwnd = rack->r_ctl.cwnd_to_use;
5585 	}
5586 	if ((rack->rc_initial_ss_comp == 0) &&
5587 	    (tp->snd_cwnd >= tp->snd_ssthresh)) {
5588 		/*
5589 		 * The cwnd has grown beyond ssthresh we have
5590 		 * entered ca and completed our first Slowstart.
5591 		 */
5592 		rack->rc_initial_ss_comp = 1;
5593 	}
5594 }
5595 
5596 static void
5597 tcp_rack_partialack(struct tcpcb *tp)
5598 {
5599 	struct tcp_rack *rack;
5600 
5601 	rack = (struct tcp_rack *)tp->t_fb_ptr;
5602 	INP_WLOCK_ASSERT(tptoinpcb(tp));
5603 	/*
5604 	 * If we are doing PRR and have enough
5605 	 * room to send <or> we are pacing and prr
5606 	 * is disabled we will want to see if we
5607 	 * can send data (by setting r_wanted_output to
5608 	 * true).
5609 	 */
5610 	if ((rack->r_ctl.rc_prr_sndcnt > 0) ||
5611 	    rack->rack_no_prr)
5612 		rack->r_wanted_output = 1;
5613 }
5614 
5615 static void
5616 rack_exit_recovery(struct tcpcb *tp, struct tcp_rack *rack, int how)
5617 {
5618 	/*
5619 	 * Now exit recovery.
5620 	 */
5621 	EXIT_RECOVERY(tp->t_flags);
5622 }
5623 
5624 static void
5625 rack_post_recovery(struct tcpcb *tp, uint32_t th_ack)
5626 {
5627 	struct tcp_rack *rack;
5628 	uint32_t orig_cwnd;
5629 
5630 	orig_cwnd = tp->snd_cwnd;
5631 	INP_WLOCK_ASSERT(tptoinpcb(tp));
5632 	rack = (struct tcp_rack *)tp->t_fb_ptr;
5633 	/* only alert CC if we alerted when we entered */
5634 	if (CC_ALGO(tp)->post_recovery != NULL) {
5635 		tp->t_ccv.curack = th_ack;
5636 		CC_ALGO(tp)->post_recovery(&tp->t_ccv);
5637 		if (tp->snd_cwnd < tp->snd_ssthresh) {
5638 			/*
5639 			 * Rack has burst control and pacing
5640 			 * so lets not set this any lower than
5641 			 * snd_ssthresh per RFC-6582 (option 2).
5642 			 */
5643 			tp->snd_cwnd = tp->snd_ssthresh;
5644 		}
5645 	}
5646 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
5647 		union tcp_log_stackspecific log;
5648 		struct timeval tv;
5649 
5650 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5651 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5652 		log.u_bbr.flex1 = th_ack;
5653 		log.u_bbr.flex2 = tp->t_ccv.flags;
5654 		log.u_bbr.flex3 = tp->t_ccv.bytes_this_ack;
5655 		log.u_bbr.flex4 = tp->t_ccv.nsegs;
5656 		log.u_bbr.flex5 = V_tcp_abc_l_var;
5657 		log.u_bbr.flex6 = orig_cwnd;
5658 		log.u_bbr.flex7 = V_tcp_do_newsack;
5659 		log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
5660 		log.u_bbr.flex8 = 2;
5661 		tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
5662 			       0, &log, false, NULL, __func__, __LINE__, &tv);
5663 	}
5664 	if ((rack->rack_no_prr == 0) &&
5665 	    (rack->no_prr_addback == 0) &&
5666 	    (rack->r_ctl.rc_prr_sndcnt > 0)) {
5667 		/*
5668 		 * Suck the next prr cnt back into cwnd, but
5669 		 * only do that if we are not application limited.
5670 		 */
5671 		if (ctf_outstanding(tp) <= sbavail(&tptosocket(tp)->so_snd)) {
5672 			/*
5673 			 * We are allowed to add back to the cwnd the amount we did
5674 			 * not get out if:
5675 			 * a) no_prr_addback is off.
5676 			 * b) we are not app limited
5677 			 * c) we are doing prr
5678 			 * <and>
5679 			 * d) it is bounded by rack_prr_addbackmax (if addback is 0, then none).
5680 			 */
5681 			tp->snd_cwnd += min((ctf_fixed_maxseg(tp) * rack_prr_addbackmax),
5682 					    rack->r_ctl.rc_prr_sndcnt);
5683 		}
5684 		rack->r_ctl.rc_prr_sndcnt = 0;
5685 		rack_log_to_prr(rack, 1, 0, __LINE__);
5686 	}
5687 	rack_log_to_prr(rack, 14, orig_cwnd, __LINE__);
5688 	tp->snd_recover = tp->snd_una;
5689 	if (rack->r_ctl.dsack_persist) {
5690 		rack->r_ctl.dsack_persist--;
5691 		if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
5692 			rack->r_ctl.num_dsack = 0;
5693 		}
5694 		rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
5695 	}
5696 	if (rack->rto_from_rec == 1) {
5697 		rack->rto_from_rec = 0;
5698 		if (rack->r_ctl.rto_ssthresh > tp->snd_ssthresh)
5699 			tp->snd_ssthresh = rack->r_ctl.rto_ssthresh;
5700 	}
5701 	rack_exit_recovery(tp, rack, 1);
5702 }
5703 
5704 static void
5705 rack_cong_signal(struct tcpcb *tp, uint32_t type, uint32_t ack, int line)
5706 {
5707 	struct tcp_rack *rack;
5708 	uint32_t ssthresh_enter, cwnd_enter, in_rec_at_entry, orig_cwnd;
5709 
5710 	INP_WLOCK_ASSERT(tptoinpcb(tp));
5711 #ifdef STATS
5712 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type);
5713 #endif
5714 	if (IN_RECOVERY(tp->t_flags) == 0) {
5715 		in_rec_at_entry = 0;
5716 		ssthresh_enter = tp->snd_ssthresh;
5717 		cwnd_enter = tp->snd_cwnd;
5718 	} else
5719 		in_rec_at_entry = 1;
5720 	rack = (struct tcp_rack *)tp->t_fb_ptr;
5721 	switch (type) {
5722 	case CC_NDUPACK:
5723 		tp->t_flags &= ~TF_WASFRECOVERY;
5724 		tp->t_flags &= ~TF_WASCRECOVERY;
5725 		if (!IN_FASTRECOVERY(tp->t_flags)) {
5726 			/* Check if this is the end of the initial Start-up i.e. initial slow-start */
5727 			if (rack->rc_initial_ss_comp == 0) {
5728 				/* Yep it is the end of the initial slowstart */
5729 				rack->rc_initial_ss_comp = 1;
5730 			}
5731 			rack->r_ctl.rc_prr_delivered = 0;
5732 			rack->r_ctl.rc_prr_out = 0;
5733 			rack->r_fast_output = 0;
5734 			if (rack->rack_no_prr == 0) {
5735 				rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
5736 				rack_log_to_prr(rack, 2, in_rec_at_entry, line);
5737 			}
5738 			rack->r_ctl.rc_prr_recovery_fs = tp->snd_max - tp->snd_una;
5739 			tp->snd_recover = tp->snd_max;
5740 			if (tp->t_flags2 & TF2_ECN_PERMIT)
5741 				tp->t_flags2 |= TF2_ECN_SND_CWR;
5742 		}
5743 		break;
5744 	case CC_ECN:
5745 		if (!IN_CONGRECOVERY(tp->t_flags) ||
5746 		    /*
5747 		     * Allow ECN reaction on ACK to CWR, if
5748 		     * that data segment was also CE marked.
5749 		     */
5750 		    SEQ_GEQ(ack, tp->snd_recover)) {
5751 			EXIT_CONGRECOVERY(tp->t_flags);
5752 			KMOD_TCPSTAT_INC(tcps_ecn_rcwnd);
5753 			rack->r_fast_output = 0;
5754 			tp->snd_recover = tp->snd_max + 1;
5755 			if (tp->t_flags2 & TF2_ECN_PERMIT)
5756 				tp->t_flags2 |= TF2_ECN_SND_CWR;
5757 		}
5758 		break;
5759 	case CC_RTO:
5760 		tp->t_dupacks = 0;
5761 		tp->t_bytes_acked = 0;
5762 		rack->r_fast_output = 0;
5763 		if (IN_RECOVERY(tp->t_flags))
5764 			rack_exit_recovery(tp, rack, 2);
5765 		orig_cwnd = tp->snd_cwnd;
5766 		rack_log_to_prr(rack, 16, orig_cwnd, line);
5767 		if (CC_ALGO(tp)->cong_signal == NULL) {
5768 			/* TSNH */
5769 			tp->snd_ssthresh = max(2,
5770 			    min(tp->snd_wnd, rack->r_ctl.cwnd_to_use) / 2 /
5771 			    ctf_fixed_maxseg(tp)) * ctf_fixed_maxseg(tp);
5772 			tp->snd_cwnd = ctf_fixed_maxseg(tp);
5773 		}
5774 		if (tp->t_flags2 & TF2_ECN_PERMIT)
5775 			tp->t_flags2 |= TF2_ECN_SND_CWR;
5776 		break;
5777 	case CC_RTO_ERR:
5778 		KMOD_TCPSTAT_INC(tcps_sndrexmitbad);
5779 		/* RTO was unnecessary, so reset everything. */
5780 		tp->snd_cwnd = tp->snd_cwnd_prev;
5781 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
5782 		tp->snd_recover = tp->snd_recover_prev;
5783 		if (tp->t_flags & TF_WASFRECOVERY) {
5784 			ENTER_FASTRECOVERY(tp->t_flags);
5785 			tp->t_flags &= ~TF_WASFRECOVERY;
5786 		}
5787 		if (tp->t_flags & TF_WASCRECOVERY) {
5788 			ENTER_CONGRECOVERY(tp->t_flags);
5789 			tp->t_flags &= ~TF_WASCRECOVERY;
5790 		}
5791 		tp->snd_nxt = tp->snd_max;
5792 		tp->t_badrxtwin = 0;
5793 		break;
5794 	}
5795 	if ((CC_ALGO(tp)->cong_signal != NULL)  &&
5796 	    (type != CC_RTO)){
5797 		tp->t_ccv.curack = ack;
5798 		CC_ALGO(tp)->cong_signal(&tp->t_ccv, type);
5799 	}
5800 	if ((in_rec_at_entry == 0) && IN_RECOVERY(tp->t_flags)) {
5801 		rack_log_to_prr(rack, 15, cwnd_enter, line);
5802 		rack->r_ctl.dsack_byte_cnt = 0;
5803 		rack->r_ctl.retran_during_recovery = 0;
5804 		rack->r_ctl.rc_cwnd_at_erec = cwnd_enter;
5805 		rack->r_ctl.rc_ssthresh_at_erec = ssthresh_enter;
5806 		rack->r_ent_rec_ns = 1;
5807 	}
5808 }
5809 
5810 static inline void
5811 rack_cc_after_idle(struct tcp_rack *rack, struct tcpcb *tp)
5812 {
5813 	uint32_t i_cwnd;
5814 
5815 	INP_WLOCK_ASSERT(tptoinpcb(tp));
5816 
5817 	if (CC_ALGO(tp)->after_idle != NULL)
5818 		CC_ALGO(tp)->after_idle(&tp->t_ccv);
5819 
5820 	if (tp->snd_cwnd == 1)
5821 		i_cwnd = tp->t_maxseg;		/* SYN(-ACK) lost */
5822 	else
5823 		i_cwnd = rc_init_window(rack);
5824 
5825 	/*
5826 	 * Being idle is no different than the initial window. If the cc
5827 	 * clamps it down below the initial window raise it to the initial
5828 	 * window.
5829 	 */
5830 	if (tp->snd_cwnd < i_cwnd) {
5831 		tp->snd_cwnd = i_cwnd;
5832 	}
5833 }
5834 
5835 /*
5836  * Indicate whether this ack should be delayed.  We can delay the ack if
5837  * following conditions are met:
5838  *	- There is no delayed ack timer in progress.
5839  *	- Our last ack wasn't a 0-sized window. We never want to delay
5840  *	  the ack that opens up a 0-sized window.
5841  *	- LRO wasn't used for this segment. We make sure by checking that the
5842  *	  segment size is not larger than the MSS.
5843  *	- Delayed acks are enabled or this is a half-synchronized T/TCP
5844  *	  connection.
5845  */
5846 #define DELAY_ACK(tp, tlen)			 \
5847 	(((tp->t_flags & TF_RXWIN0SENT) == 0) && \
5848 	((tp->t_flags & TF_DELACK) == 0) &&	 \
5849 	(tlen <= tp->t_maxseg) &&		 \
5850 	(tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN)))
5851 
5852 static struct rack_sendmap *
5853 rack_find_lowest_rsm(struct tcp_rack *rack)
5854 {
5855 	struct rack_sendmap *rsm;
5856 
5857 	/*
5858 	 * Walk the time-order transmitted list looking for an rsm that is
5859 	 * not acked. This will be the one that was sent the longest time
5860 	 * ago that is still outstanding.
5861 	 */
5862 	TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
5863 		if (rsm->r_flags & RACK_ACKED) {
5864 			continue;
5865 		}
5866 		goto finish;
5867 	}
5868 finish:
5869 	return (rsm);
5870 }
5871 
5872 static struct rack_sendmap *
5873 rack_find_high_nonack(struct tcp_rack *rack, struct rack_sendmap *rsm)
5874 {
5875 	struct rack_sendmap *prsm;
5876 
5877 	/*
5878 	 * Walk the sequence order list backward until we hit and arrive at
5879 	 * the highest seq not acked. In theory when this is called it
5880 	 * should be the last segment (which it was not).
5881 	 */
5882 	prsm = rsm;
5883 
5884 	TQHASH_FOREACH_REVERSE_FROM(prsm, rack->r_ctl.tqh) {
5885 		if (prsm->r_flags & (RACK_ACKED | RACK_HAS_FIN)) {
5886 			continue;
5887 		}
5888 		return (prsm);
5889 	}
5890 	return (NULL);
5891 }
5892 
5893 static uint32_t
5894 rack_calc_thresh_rack(struct tcp_rack *rack, uint32_t srtt, uint32_t cts, int line, int log_allowed)
5895 {
5896 	int32_t lro;
5897 	uint32_t thresh;
5898 
5899 	/*
5900 	 * lro is the flag we use to determine if we have seen reordering.
5901 	 * If it gets set we have seen reordering. The reorder logic either
5902 	 * works in one of two ways:
5903 	 *
5904 	 * If reorder-fade is configured, then we track the last time we saw
5905 	 * re-ordering occur. If we reach the point where enough time as
5906 	 * passed we no longer consider reordering has occuring.
5907 	 *
5908 	 * Or if reorder-face is 0, then once we see reordering we consider
5909 	 * the connection to alway be subject to reordering and just set lro
5910 	 * to 1.
5911 	 *
5912 	 * In the end if lro is non-zero we add the extra time for
5913 	 * reordering in.
5914 	 */
5915 	if (srtt == 0)
5916 		srtt = 1;
5917 	if (rack->r_ctl.rc_reorder_ts) {
5918 		if (rack->r_ctl.rc_reorder_fade) {
5919 			if (SEQ_GEQ(cts, rack->r_ctl.rc_reorder_ts)) {
5920 				lro = cts - rack->r_ctl.rc_reorder_ts;
5921 				if (lro == 0) {
5922 					/*
5923 					 * No time as passed since the last
5924 					 * reorder, mark it as reordering.
5925 					 */
5926 					lro = 1;
5927 				}
5928 			} else {
5929 				/* Negative time? */
5930 				lro = 0;
5931 			}
5932 			if (lro > rack->r_ctl.rc_reorder_fade) {
5933 				/* Turn off reordering seen too */
5934 				rack->r_ctl.rc_reorder_ts = 0;
5935 				lro = 0;
5936 			}
5937 		} else {
5938 			/* Reodering does not fade */
5939 			lro = 1;
5940 		}
5941 	} else {
5942 		lro = 0;
5943 	}
5944 	if (rack->rc_rack_tmr_std_based == 0) {
5945 		thresh = srtt + rack->r_ctl.rc_pkt_delay;
5946 	} else {
5947 		/* Standards based pkt-delay is 1/4 srtt */
5948 		thresh = srtt +  (srtt >> 2);
5949 	}
5950 	if (lro && (rack->rc_rack_tmr_std_based == 0)) {
5951 		/* It must be set, if not you get 1/4 rtt */
5952 		if (rack->r_ctl.rc_reorder_shift)
5953 			thresh += (srtt >> rack->r_ctl.rc_reorder_shift);
5954 		else
5955 			thresh += (srtt >> 2);
5956 	}
5957 	if (rack->rc_rack_use_dsack &&
5958 	    lro &&
5959 	    (rack->r_ctl.num_dsack > 0)) {
5960 		/*
5961 		 * We only increase the reordering window if we
5962 		 * have seen reordering <and> we have a DSACK count.
5963 		 */
5964 		thresh += rack->r_ctl.num_dsack * (srtt >> 2);
5965 		if (log_allowed)
5966 			rack_log_dsack_event(rack, 4, line, srtt, thresh);
5967 	}
5968 	/* SRTT * 2 is the ceiling */
5969 	if (thresh > (srtt * 2)) {
5970 		thresh = srtt * 2;
5971 	}
5972 	/* And we don't want it above the RTO max either */
5973 	if (thresh > rack_rto_max) {
5974 		thresh = rack_rto_max;
5975 	}
5976 	if (log_allowed)
5977 		rack_log_dsack_event(rack, 6, line,  srtt, thresh);
5978 	return (thresh);
5979 }
5980 
5981 static uint32_t
5982 rack_calc_thresh_tlp(struct tcpcb *tp, struct tcp_rack *rack,
5983 		     struct rack_sendmap *rsm, uint32_t srtt)
5984 {
5985 	struct rack_sendmap *prsm;
5986 	uint32_t thresh, len;
5987 	int segsiz;
5988 
5989 	if (srtt == 0)
5990 		srtt = 1;
5991 	if (rack->r_ctl.rc_tlp_threshold)
5992 		thresh = srtt + (srtt / rack->r_ctl.rc_tlp_threshold);
5993 	else
5994 		thresh = (srtt * 2);
5995 
5996 	/* Get the previous sent packet, if any */
5997 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
5998 	len = rsm->r_end - rsm->r_start;
5999 	if (rack->rack_tlp_threshold_use == TLP_USE_ID) {
6000 		/* Exactly like the ID */
6001 		if (((tp->snd_max - tp->snd_una) - rack->r_ctl.rc_sacked + rack->r_ctl.rc_holes_rxt) <= segsiz) {
6002 			uint32_t alt_thresh;
6003 			/*
6004 			 * Compensate for delayed-ack with the d-ack time.
6005 			 */
6006 			alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
6007 			if (alt_thresh > thresh)
6008 				thresh = alt_thresh;
6009 		}
6010 	} else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_ONE) {
6011 		/* 2.1 behavior */
6012 		prsm = TAILQ_PREV(rsm, rack_head, r_tnext);
6013 		if (prsm && (len <= segsiz)) {
6014 			/*
6015 			 * Two packets outstanding, thresh should be (2*srtt) +
6016 			 * possible inter-packet delay (if any).
6017 			 */
6018 			uint32_t inter_gap = 0;
6019 			int idx, nidx;
6020 
6021 			idx = rsm->r_rtr_cnt - 1;
6022 			nidx = prsm->r_rtr_cnt - 1;
6023 			if (rsm->r_tim_lastsent[nidx] >= prsm->r_tim_lastsent[idx]) {
6024 				/* Yes it was sent later (or at the same time) */
6025 				inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx];
6026 			}
6027 			thresh += inter_gap;
6028 		} else if (len <= segsiz) {
6029 			/*
6030 			 * Possibly compensate for delayed-ack.
6031 			 */
6032 			uint32_t alt_thresh;
6033 
6034 			alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
6035 			if (alt_thresh > thresh)
6036 				thresh = alt_thresh;
6037 		}
6038 	} else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_TWO) {
6039 		/* 2.2 behavior */
6040 		if (len <= segsiz) {
6041 			uint32_t alt_thresh;
6042 			/*
6043 			 * Compensate for delayed-ack with the d-ack time.
6044 			 */
6045 			alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
6046 			if (alt_thresh > thresh)
6047 				thresh = alt_thresh;
6048 		}
6049 	}
6050 	/* Not above an RTO */
6051 	if (thresh > tp->t_rxtcur) {
6052 		thresh = tp->t_rxtcur;
6053 	}
6054 	/* Not above a RTO max */
6055 	if (thresh > rack_rto_max) {
6056 		thresh = rack_rto_max;
6057 	}
6058 	/* Apply user supplied min TLP */
6059 	if (thresh < rack_tlp_min) {
6060 		thresh = rack_tlp_min;
6061 	}
6062 	return (thresh);
6063 }
6064 
6065 static uint32_t
6066 rack_grab_rtt(struct tcpcb *tp, struct tcp_rack *rack)
6067 {
6068 	/*
6069 	 * We want the rack_rtt which is the
6070 	 * last rtt we measured. However if that
6071 	 * does not exist we fallback to the srtt (which
6072 	 * we probably will never do) and then as a last
6073 	 * resort we use RACK_INITIAL_RTO if no srtt is
6074 	 * yet set.
6075 	 */
6076 	if (rack->rc_rack_rtt)
6077 		return (rack->rc_rack_rtt);
6078 	else if (tp->t_srtt == 0)
6079 		return (RACK_INITIAL_RTO);
6080 	return (tp->t_srtt);
6081 }
6082 
6083 static struct rack_sendmap *
6084 rack_check_recovery_mode(struct tcpcb *tp, uint32_t tsused)
6085 {
6086 	/*
6087 	 * Check to see that we don't need to fall into recovery. We will
6088 	 * need to do so if our oldest transmit is past the time we should
6089 	 * have had an ack.
6090 	 */
6091 	struct tcp_rack *rack;
6092 	struct rack_sendmap *rsm;
6093 	int32_t idx;
6094 	uint32_t srtt, thresh;
6095 
6096 	rack = (struct tcp_rack *)tp->t_fb_ptr;
6097 	if (tqhash_empty(rack->r_ctl.tqh)) {
6098 		return (NULL);
6099 	}
6100 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6101 	if (rsm == NULL)
6102 		return (NULL);
6103 
6104 
6105 	if (rsm->r_flags & RACK_ACKED) {
6106 		rsm = rack_find_lowest_rsm(rack);
6107 		if (rsm == NULL)
6108 			return (NULL);
6109 	}
6110 	idx = rsm->r_rtr_cnt - 1;
6111 	srtt = rack_grab_rtt(tp, rack);
6112 	thresh = rack_calc_thresh_rack(rack, srtt, tsused, __LINE__, 1);
6113 	if (TSTMP_LT(tsused, ((uint32_t)rsm->r_tim_lastsent[idx]))) {
6114 		return (NULL);
6115 	}
6116 	if ((tsused - ((uint32_t)rsm->r_tim_lastsent[idx])) < thresh) {
6117 		return (NULL);
6118 	}
6119 	/* Ok if we reach here we are over-due and this guy can be sent */
6120 	rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__);
6121 	return (rsm);
6122 }
6123 
6124 static uint32_t
6125 rack_get_persists_timer_val(struct tcpcb *tp, struct tcp_rack *rack)
6126 {
6127 	int32_t t;
6128 	int32_t tt;
6129 	uint32_t ret_val;
6130 
6131 	t = (tp->t_srtt + (tp->t_rttvar << 2));
6132 	RACK_TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
6133  	    rack_persist_min, rack_persist_max, rack->r_ctl.timer_slop);
6134 	rack->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT;
6135 	ret_val = (uint32_t)tt;
6136 	return (ret_val);
6137 }
6138 
6139 static uint32_t
6140 rack_timer_start(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int sup_rack)
6141 {
6142 	/*
6143 	 * Start the FR timer, we do this based on getting the first one in
6144 	 * the rc_tmap. Note that if its NULL we must stop the timer. in all
6145 	 * events we need to stop the running timer (if its running) before
6146 	 * starting the new one.
6147 	 */
6148 	uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse;
6149 	uint32_t srtt_cur;
6150 	int32_t idx;
6151 	int32_t is_tlp_timer = 0;
6152 	struct rack_sendmap *rsm;
6153 
6154 	if (rack->t_timers_stopped) {
6155 		/* All timers have been stopped none are to run */
6156 		return (0);
6157 	}
6158 	if (rack->rc_in_persist) {
6159 		/* We can't start any timer in persists */
6160 		return (rack_get_persists_timer_val(tp, rack));
6161 	}
6162 	rack->rc_on_min_to = 0;
6163 	if ((tp->t_state < TCPS_ESTABLISHED) ||
6164 	    ((tp->t_flags & TF_SACK_PERMIT) == 0)) {
6165 		goto activate_rxt;
6166 	}
6167 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6168 	if ((rsm == NULL) || sup_rack) {
6169 		/* Nothing on the send map or no rack */
6170 activate_rxt:
6171 		time_since_sent = 0;
6172 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6173 		if (rsm) {
6174 			/*
6175 			 * Should we discount the RTX timer any?
6176 			 *
6177 			 * We want to discount it the smallest amount.
6178 			 * If a timer (Rack/TLP or RXT) has gone off more
6179 			 * recently thats the discount we want to use (now - timer time).
6180 			 * If the retransmit of the oldest packet was more recent then
6181 			 * we want to use that (now - oldest-packet-last_transmit_time).
6182 			 *
6183 			 */
6184 			idx = rsm->r_rtr_cnt - 1;
6185 			if (TSTMP_GEQ(rack->r_ctl.rc_tlp_rxt_last_time, ((uint32_t)rsm->r_tim_lastsent[idx])))
6186 				tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time;
6187 			else
6188 				tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx];
6189 			if (TSTMP_GT(cts, tstmp_touse))
6190 			    time_since_sent = cts - tstmp_touse;
6191 		}
6192 		if (SEQ_LT(tp->snd_una, tp->snd_max) ||
6193 		    sbavail(&tptosocket(tp)->so_snd)) {
6194 			rack->r_ctl.rc_hpts_flags |= PACE_TMR_RXT;
6195 			to = tp->t_rxtcur;
6196 			if (to > time_since_sent)
6197 				to -= time_since_sent;
6198 			else
6199 				to = rack->r_ctl.rc_min_to;
6200 			if (to == 0)
6201 				to = 1;
6202 			/* Special case for KEEPINIT */
6203 			if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) &&
6204 			    (TP_KEEPINIT(tp) != 0) &&
6205 			    rsm) {
6206 				/*
6207 				 * We have to put a ceiling on the rxt timer
6208 				 * of the keep-init timeout.
6209 				 */
6210 				uint32_t max_time, red;
6211 
6212 				max_time = TICKS_2_USEC(TP_KEEPINIT(tp));
6213 				if (TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) {
6214 					red = (cts - (uint32_t)rsm->r_tim_lastsent[0]);
6215 					if (red < max_time)
6216 						max_time -= red;
6217 					else
6218 						max_time = 1;
6219 				}
6220 				/* Reduce timeout to the keep value if needed */
6221 				if (max_time < to)
6222 					to = max_time;
6223 			}
6224 			return (to);
6225 		}
6226 		return (0);
6227 	}
6228 	if (rsm->r_flags & RACK_ACKED) {
6229 		rsm = rack_find_lowest_rsm(rack);
6230 		if (rsm == NULL) {
6231 			/* No lowest? */
6232 			goto activate_rxt;
6233 		}
6234 	}
6235 	/* Convert from ms to usecs */
6236 	if ((rsm->r_flags & RACK_SACK_PASSED) ||
6237 	    (rsm->r_flags & RACK_RWND_COLLAPSED) ||
6238 	    (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
6239 		if ((tp->t_flags & TF_SENTFIN) &&
6240 		    ((tp->snd_max - tp->snd_una) == 1) &&
6241 		    (rsm->r_flags & RACK_HAS_FIN)) {
6242 			/*
6243 			 * We don't start a rack timer if all we have is a
6244 			 * FIN outstanding.
6245 			 */
6246 			goto activate_rxt;
6247 		}
6248 		if ((rack->use_rack_rr == 0) &&
6249 		    (IN_FASTRECOVERY(tp->t_flags)) &&
6250 		    (rack->rack_no_prr == 0) &&
6251 		     (rack->r_ctl.rc_prr_sndcnt  < ctf_fixed_maxseg(tp))) {
6252 			/*
6253 			 * We are not cheating, in recovery  and
6254 			 * not enough ack's to yet get our next
6255 			 * retransmission out.
6256 			 *
6257 			 * Note that classified attackers do not
6258 			 * get to use the rack-cheat.
6259 			 */
6260 			goto activate_tlp;
6261 		}
6262 		srtt = rack_grab_rtt(tp, rack);
6263 		thresh = rack_calc_thresh_rack(rack, srtt, cts, __LINE__, 1);
6264 		idx = rsm->r_rtr_cnt - 1;
6265 		exp = ((uint32_t)rsm->r_tim_lastsent[idx]) + thresh;
6266 		if (SEQ_GEQ(exp, cts)) {
6267 			to = exp - cts;
6268 			if (to < rack->r_ctl.rc_min_to) {
6269 				to = rack->r_ctl.rc_min_to;
6270 				if (rack->r_rr_config == 3)
6271 					rack->rc_on_min_to = 1;
6272 			}
6273 		} else {
6274 			to = rack->r_ctl.rc_min_to;
6275 			if (rack->r_rr_config == 3)
6276 				rack->rc_on_min_to = 1;
6277 		}
6278 	} else {
6279 		/* Ok we need to do a TLP not RACK */
6280 activate_tlp:
6281 		if ((rack->rc_tlp_in_progress != 0) &&
6282 		    (rack->r_ctl.rc_tlp_cnt_out >= rack_tlp_limit)) {
6283 			/*
6284 			 * The previous send was a TLP and we have sent
6285 			 * N TLP's without sending new data.
6286 			 */
6287 			goto activate_rxt;
6288 		}
6289 		rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
6290 		if (rsm == NULL) {
6291 			/* We found no rsm to TLP with. */
6292 			goto activate_rxt;
6293 		}
6294 		if (rsm->r_flags & RACK_HAS_FIN) {
6295 			/* If its a FIN we dont do TLP */
6296 			rsm = NULL;
6297 			goto activate_rxt;
6298 		}
6299 		idx = rsm->r_rtr_cnt - 1;
6300 		time_since_sent = 0;
6301 		if (TSTMP_GEQ(((uint32_t)rsm->r_tim_lastsent[idx]), rack->r_ctl.rc_tlp_rxt_last_time))
6302 			tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx];
6303 		else
6304 			tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time;
6305 		if (TSTMP_GT(cts, tstmp_touse))
6306 		    time_since_sent = cts - tstmp_touse;
6307 		is_tlp_timer = 1;
6308 		if (tp->t_srtt) {
6309 			if ((rack->rc_srtt_measure_made == 0) &&
6310 			    (tp->t_srtt == 1)) {
6311 				/*
6312 				 * If another stack as run and set srtt to 1,
6313 				 * then the srtt was 0, so lets use the initial.
6314 				 */
6315 				srtt = RACK_INITIAL_RTO;
6316 			} else {
6317 				srtt_cur = tp->t_srtt;
6318 				srtt = srtt_cur;
6319 			}
6320 		} else
6321 			srtt = RACK_INITIAL_RTO;
6322 		/*
6323 		 * If the SRTT is not keeping up and the
6324 		 * rack RTT has spiked we want to use
6325 		 * the last RTT not the smoothed one.
6326 		 */
6327 		if (rack_tlp_use_greater &&
6328 		    tp->t_srtt &&
6329 		    (srtt < rack_grab_rtt(tp, rack))) {
6330 			srtt = rack_grab_rtt(tp, rack);
6331 		}
6332 		thresh = rack_calc_thresh_tlp(tp, rack, rsm, srtt);
6333 		if (thresh > time_since_sent) {
6334 			to = thresh - time_since_sent;
6335 		} else {
6336 			to = rack->r_ctl.rc_min_to;
6337 			rack_log_alt_to_to_cancel(rack,
6338 						  thresh,		/* flex1 */
6339 						  time_since_sent,	/* flex2 */
6340 						  tstmp_touse,		/* flex3 */
6341 						  rack->r_ctl.rc_tlp_rxt_last_time, /* flex4 */
6342 						  (uint32_t)rsm->r_tim_lastsent[idx],
6343 						  srtt,
6344 						  idx, 99);
6345 		}
6346 		if (to < rack_tlp_min) {
6347 			to = rack_tlp_min;
6348 		}
6349 		if (to > TICKS_2_USEC(TCPTV_REXMTMAX)) {
6350 			/*
6351 			 * If the TLP time works out to larger than the max
6352 			 * RTO lets not do TLP.. just RTO.
6353 			 */
6354 			goto activate_rxt;
6355 		}
6356 	}
6357 	if (is_tlp_timer == 0) {
6358 		rack->r_ctl.rc_hpts_flags |= PACE_TMR_RACK;
6359 	} else {
6360 		rack->r_ctl.rc_hpts_flags |= PACE_TMR_TLP;
6361 	}
6362 	if (to == 0)
6363 		to = 1;
6364 	return (to);
6365 }
6366 
6367 static void
6368 rack_enter_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, tcp_seq snd_una)
6369 {
6370 	if (rack->rc_in_persist == 0) {
6371 		if (tp->t_flags & TF_GPUTINPROG) {
6372 			/*
6373 			 * Stop the goodput now, the calling of the
6374 			 * measurement function clears the flag.
6375 			 */
6376 			rack_do_goodput_measurement(tp, rack, tp->snd_una, __LINE__,
6377 						    RACK_QUALITY_PERSIST);
6378 		}
6379 #ifdef NETFLIX_SHARED_CWND
6380 		if (rack->r_ctl.rc_scw) {
6381 			tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
6382 			rack->rack_scwnd_is_idle = 1;
6383 		}
6384 #endif
6385 		rack->r_ctl.rc_went_idle_time = cts;
6386 		if (rack->r_ctl.rc_went_idle_time == 0)
6387 			rack->r_ctl.rc_went_idle_time = 1;
6388 		if (rack->lt_bw_up) {
6389 			/* Suspend our LT BW measurement */
6390 			uint64_t tmark;
6391 
6392 			rack->r_ctl.lt_bw_bytes += (snd_una - rack->r_ctl.lt_seq);
6393 			rack->r_ctl.lt_seq = snd_una;
6394 			tmark = tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time);
6395 			if (tmark >= rack->r_ctl.lt_timemark) {
6396 				rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark);
6397 			}
6398 			rack->r_ctl.lt_timemark = tmark;
6399 			rack->lt_bw_up = 0;
6400 			rack->r_persist_lt_bw_off = 1;
6401 		}
6402 		rack_timer_cancel(tp, rack, cts, __LINE__);
6403 		rack->r_ctl.persist_lost_ends = 0;
6404 		rack->probe_not_answered = 0;
6405 		rack->forced_ack = 0;
6406 		tp->t_rxtshift = 0;
6407 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
6408 			      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
6409 		rack->rc_in_persist = 1;
6410 	}
6411 }
6412 
6413 static void
6414 rack_exit_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6415 {
6416 	if (tcp_in_hpts(rack->rc_tp)) {
6417 		tcp_hpts_remove(rack->rc_tp);
6418 		rack->r_ctl.rc_hpts_flags = 0;
6419 	}
6420 #ifdef NETFLIX_SHARED_CWND
6421 	if (rack->r_ctl.rc_scw) {
6422 		tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
6423 		rack->rack_scwnd_is_idle = 0;
6424 	}
6425 #endif
6426 	if (rack->rc_gp_dyn_mul &&
6427 	    (rack->use_fixed_rate == 0) &&
6428 	    (rack->rc_always_pace)) {
6429 		/*
6430 		 * Do we count this as if a probe-rtt just
6431 		 * finished?
6432 		 */
6433 		uint32_t time_idle, idle_min;
6434 
6435 		time_idle = cts - rack->r_ctl.rc_went_idle_time;
6436 		idle_min = rack_min_probertt_hold;
6437 		if (rack_probertt_gpsrtt_cnt_div) {
6438 			uint64_t extra;
6439 			extra = (uint64_t)rack->r_ctl.rc_gp_srtt *
6440 				(uint64_t)rack_probertt_gpsrtt_cnt_mul;
6441 			extra /= (uint64_t)rack_probertt_gpsrtt_cnt_div;
6442 			idle_min += (uint32_t)extra;
6443 		}
6444 		if (time_idle >= idle_min) {
6445 			/* Yes, we count it as a probe-rtt. */
6446 			uint32_t us_cts;
6447 
6448 			us_cts = tcp_get_usecs(NULL);
6449 			if (rack->in_probe_rtt == 0) {
6450 				rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
6451 				rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts;
6452 				rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts;
6453 				rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts;
6454 			} else {
6455 				rack_exit_probertt(rack, us_cts);
6456 			}
6457 		}
6458 	}
6459 	if (rack->r_persist_lt_bw_off) {
6460 		/* Continue where we left off */
6461 		rack->r_ctl.lt_timemark = tcp_get_u64_usecs(NULL);
6462 		rack->lt_bw_up = 1;
6463 		rack->r_persist_lt_bw_off = 0;
6464 	}
6465 	rack->rc_in_persist = 0;
6466 	rack->r_ctl.rc_went_idle_time = 0;
6467 	tp->t_rxtshift = 0;
6468 	RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
6469 	   rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
6470 	rack->r_ctl.rc_agg_delayed = 0;
6471 	rack->r_early = 0;
6472 	rack->r_late = 0;
6473 	rack->r_ctl.rc_agg_early = 0;
6474 }
6475 
6476 static void
6477 rack_log_hpts_diag(struct tcp_rack *rack, uint32_t cts,
6478 		   struct hpts_diag *diag, struct timeval *tv)
6479 {
6480 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
6481 		union tcp_log_stackspecific log;
6482 
6483 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
6484 		log.u_bbr.flex1 = diag->p_nxt_slot;
6485 		log.u_bbr.flex2 = diag->p_cur_slot;
6486 		log.u_bbr.flex3 = diag->slot_req;
6487 		log.u_bbr.flex4 = diag->inp_hptsslot;
6488 		log.u_bbr.flex5 = diag->slot_remaining;
6489 		log.u_bbr.flex6 = diag->need_new_to;
6490 		log.u_bbr.flex7 = diag->p_hpts_active;
6491 		log.u_bbr.flex8 = diag->p_on_min_sleep;
6492 		/* Hijack other fields as needed */
6493 		log.u_bbr.epoch = diag->have_slept;
6494 		log.u_bbr.lt_epoch = diag->yet_to_sleep;
6495 		log.u_bbr.pkts_out = diag->co_ret;
6496 		log.u_bbr.applimited = diag->hpts_sleep_time;
6497 		log.u_bbr.delivered = diag->p_prev_slot;
6498 		log.u_bbr.inflight = diag->p_runningslot;
6499 		log.u_bbr.bw_inuse = diag->wheel_slot;
6500 		log.u_bbr.rttProp = diag->wheel_cts;
6501 		log.u_bbr.timeStamp = cts;
6502 		log.u_bbr.delRate = diag->maxslots;
6503 		log.u_bbr.cur_del_rate = diag->p_curtick;
6504 		log.u_bbr.cur_del_rate <<= 32;
6505 		log.u_bbr.cur_del_rate |= diag->p_lasttick;
6506 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
6507 		    &rack->rc_inp->inp_socket->so_rcv,
6508 		    &rack->rc_inp->inp_socket->so_snd,
6509 		    BBR_LOG_HPTSDIAG, 0,
6510 		    0, &log, false, tv);
6511 	}
6512 
6513 }
6514 
6515 static void
6516 rack_log_wakeup(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb, uint32_t len, int type)
6517 {
6518 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
6519 		union tcp_log_stackspecific log;
6520 		struct timeval tv;
6521 
6522 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
6523 		log.u_bbr.flex1 = sb->sb_flags;
6524 		log.u_bbr.flex2 = len;
6525 		log.u_bbr.flex3 = sb->sb_state;
6526 		log.u_bbr.flex8 = type;
6527 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
6528 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
6529 		    &rack->rc_inp->inp_socket->so_rcv,
6530 		    &rack->rc_inp->inp_socket->so_snd,
6531 		    TCP_LOG_SB_WAKE, 0,
6532 		    len, &log, false, &tv);
6533 	}
6534 }
6535 
6536 static void
6537 rack_start_hpts_timer (struct tcp_rack *rack, struct tcpcb *tp, uint32_t cts,
6538       int32_t slot, uint32_t tot_len_this_send, int sup_rack)
6539 {
6540 	struct hpts_diag diag;
6541 	struct inpcb *inp = tptoinpcb(tp);
6542 	struct timeval tv;
6543 	uint32_t delayed_ack = 0;
6544 	uint32_t hpts_timeout;
6545 	uint32_t entry_slot = slot;
6546 	uint8_t stopped;
6547 	uint32_t left = 0;
6548 	uint32_t us_cts;
6549 
6550 	if ((tp->t_state == TCPS_CLOSED) ||
6551 	    (tp->t_state == TCPS_LISTEN)) {
6552 		return;
6553 	}
6554 	if (tcp_in_hpts(tp)) {
6555 		/* Already on the pacer */
6556 		return;
6557 	}
6558 	stopped = rack->rc_tmr_stopped;
6559 	if (stopped && TSTMP_GT(rack->r_ctl.rc_timer_exp, cts)) {
6560 		left = rack->r_ctl.rc_timer_exp - cts;
6561 	}
6562 	rack->r_ctl.rc_timer_exp = 0;
6563 	rack->r_ctl.rc_hpts_flags = 0;
6564 	us_cts = tcp_get_usecs(&tv);
6565 	/* Now early/late accounting */
6566 	rack_log_pacing_delay_calc(rack, entry_slot, slot, 0, 0, 0, 26, __LINE__, NULL, 0);
6567 	if (rack->r_early && (rack->rc_ack_can_sendout_data == 0)) {
6568 		/*
6569 		 * We have a early carry over set,
6570 		 * we can always add more time so we
6571 		 * can always make this compensation.
6572 		 *
6573 		 * Note if ack's are allowed to wake us do not
6574 		 * penalize the next timer for being awoke
6575 		 * by an ack aka the rc_agg_early (non-paced mode).
6576 		 */
6577 		slot += rack->r_ctl.rc_agg_early;
6578 		rack->r_early = 0;
6579 		rack->r_ctl.rc_agg_early = 0;
6580 	}
6581 	if ((rack->r_late) &&
6582 	    ((rack->r_use_hpts_min == 0) || (rack->dgp_on == 0))) {
6583 		/*
6584 		 * This is harder, we can
6585 		 * compensate some but it
6586 		 * really depends on what
6587 		 * the current pacing time is.
6588 		 */
6589 		if (rack->r_ctl.rc_agg_delayed >= slot) {
6590 			/*
6591 			 * We can't compensate for it all.
6592 			 * And we have to have some time
6593 			 * on the clock. We always have a min
6594 			 * 10 slots (10 x 10 i.e. 100 usecs).
6595 			 */
6596 			if (slot <= HPTS_TICKS_PER_SLOT) {
6597 				/* We gain delay */
6598 				rack->r_ctl.rc_agg_delayed += (HPTS_TICKS_PER_SLOT - slot);
6599 				slot = HPTS_TICKS_PER_SLOT;
6600 			} else {
6601 				/* We take off some */
6602 				rack->r_ctl.rc_agg_delayed -= (slot - HPTS_TICKS_PER_SLOT);
6603 				slot = HPTS_TICKS_PER_SLOT;
6604 			}
6605 		} else {
6606 			slot -= rack->r_ctl.rc_agg_delayed;
6607 			rack->r_ctl.rc_agg_delayed = 0;
6608 			/* Make sure we have 100 useconds at minimum */
6609 			if (slot < HPTS_TICKS_PER_SLOT) {
6610 				rack->r_ctl.rc_agg_delayed = HPTS_TICKS_PER_SLOT - slot;
6611 				slot = HPTS_TICKS_PER_SLOT;
6612 			}
6613 			if (rack->r_ctl.rc_agg_delayed == 0)
6614 				rack->r_late = 0;
6615 		}
6616 	} else if (rack->r_late) {
6617 		/* r_use_hpts_min is on and so is DGP */
6618 		uint32_t max_red;
6619 
6620 		max_red = (slot * rack->r_ctl.max_reduction) / 100;
6621 		if (max_red >= rack->r_ctl.rc_agg_delayed) {
6622 			slot -= rack->r_ctl.rc_agg_delayed;
6623 			rack->r_ctl.rc_agg_delayed = 0;
6624 		} else {
6625 			slot -= max_red;
6626 			rack->r_ctl.rc_agg_delayed -= max_red;
6627 		}
6628 	}
6629 	if ((rack->r_use_hpts_min == 1) &&
6630 	    (slot > 0) &&
6631 	    (rack->dgp_on == 1)) {
6632 		/*
6633 		 * We are enforcing a min pacing timer
6634 		 * based on our hpts min timeout.
6635 		 */
6636 		uint32_t min;
6637 
6638 		min = get_hpts_min_sleep_time();
6639 		if (min > slot) {
6640 			slot = min;
6641 		}
6642 	}
6643 	hpts_timeout = rack_timer_start(tp, rack, cts, sup_rack);
6644 	if (tp->t_flags & TF_DELACK) {
6645 		delayed_ack = TICKS_2_USEC(tcp_delacktime);
6646 		rack->r_ctl.rc_hpts_flags |= PACE_TMR_DELACK;
6647 	}
6648 	if (delayed_ack && ((hpts_timeout == 0) ||
6649 			    (delayed_ack < hpts_timeout)))
6650 		hpts_timeout = delayed_ack;
6651 	else
6652 		rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
6653 	/*
6654 	 * If no timers are going to run and we will fall off the hptsi
6655 	 * wheel, we resort to a keep-alive timer if its configured.
6656 	 */
6657 	if ((hpts_timeout == 0) &&
6658 	    (slot == 0)) {
6659 		if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
6660 		    (tp->t_state <= TCPS_CLOSING)) {
6661 			/*
6662 			 * Ok we have no timer (persists, rack, tlp, rxt  or
6663 			 * del-ack), we don't have segments being paced. So
6664 			 * all that is left is the keepalive timer.
6665 			 */
6666 			if (TCPS_HAVEESTABLISHED(tp->t_state)) {
6667 				/* Get the established keep-alive time */
6668 				hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp));
6669 			} else {
6670 				/*
6671 				 * Get the initial setup keep-alive time,
6672 				 * note that this is probably not going to
6673 				 * happen, since rack will be running a rxt timer
6674 				 * if a SYN of some sort is outstanding. It is
6675 				 * actually handled in rack_timeout_rxt().
6676 				 */
6677 				hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp));
6678 			}
6679 			rack->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP;
6680 			if (rack->in_probe_rtt) {
6681 				/*
6682 				 * We want to instead not wake up a long time from
6683 				 * now but to wake up about the time we would
6684 				 * exit probe-rtt and initiate a keep-alive ack.
6685 				 * This will get us out of probe-rtt and update
6686 				 * our min-rtt.
6687 				 */
6688 				hpts_timeout = rack_min_probertt_hold;
6689 			}
6690 		}
6691 	}
6692 	if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) ==
6693 	    (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) {
6694 		/*
6695 		 * RACK, TLP, persists and RXT timers all are restartable
6696 		 * based on actions input .. i.e we received a packet (ack
6697 		 * or sack) and that changes things (rw, or snd_una etc).
6698 		 * Thus we can restart them with a new value. For
6699 		 * keep-alive, delayed_ack we keep track of what was left
6700 		 * and restart the timer with a smaller value.
6701 		 */
6702 		if (left < hpts_timeout)
6703 			hpts_timeout = left;
6704 	}
6705 	if (hpts_timeout) {
6706 		/*
6707 		 * Hack alert for now we can't time-out over 2,147,483
6708 		 * seconds (a bit more than 596 hours), which is probably ok
6709 		 * :).
6710 		 */
6711 		if (hpts_timeout > 0x7ffffffe)
6712 			hpts_timeout = 0x7ffffffe;
6713 		rack->r_ctl.rc_timer_exp = cts + hpts_timeout;
6714 	}
6715 	rack_log_pacing_delay_calc(rack, entry_slot, slot, hpts_timeout, 0, 0, 27, __LINE__, NULL, 0);
6716 	if ((rack->gp_ready == 0) &&
6717 	    (rack->use_fixed_rate == 0) &&
6718 	    (hpts_timeout < slot) &&
6719 	    (rack->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) {
6720 		/*
6721 		 * We have no good estimate yet for the
6722 		 * old clunky burst mitigation or the
6723 		 * real pacing. And the tlp or rxt is smaller
6724 		 * than the pacing calculation. Lets not
6725 		 * pace that long since we know the calculation
6726 		 * so far is not accurate.
6727 		 */
6728 		slot = hpts_timeout;
6729 	}
6730 	/**
6731 	 * Turn off all the flags for queuing by default. The
6732 	 * flags have important meanings to what happens when
6733 	 * LRO interacts with the transport. Most likely (by default now)
6734 	 * mbuf_queueing and ack compression are on. So the transport
6735 	 * has a couple of flags that control what happens (if those
6736 	 * are not on then these flags won't have any effect since it
6737 	 * won't go through the queuing LRO path).
6738 	 *
6739 	 * TF2_MBUF_QUEUE_READY - This flags says that I am busy
6740 	 *                        pacing output, so don't disturb. But
6741 	 *                        it also means LRO can wake me if there
6742 	 *                        is a SACK arrival.
6743 	 *
6744 	 * TF2_DONT_SACK_QUEUE - This flag is used in conjunction
6745 	 *                       with the above flag (QUEUE_READY) and
6746 	 *                       when present it says don't even wake me
6747 	 *                       if a SACK arrives.
6748 	 *
6749 	 * The idea behind these flags is that if we are pacing we
6750 	 * set the MBUF_QUEUE_READY and only get woken up if
6751 	 * a SACK arrives (which could change things) or if
6752 	 * our pacing timer expires. If, however, we have a rack
6753 	 * timer running, then we don't even want a sack to wake
6754 	 * us since the rack timer has to expire before we can send.
6755 	 *
6756 	 * Other cases should usually have none of the flags set
6757 	 * so LRO can call into us.
6758 	 */
6759 	tp->t_flags2 &= ~(TF2_DONT_SACK_QUEUE|TF2_MBUF_QUEUE_READY);
6760 	if (slot) {
6761 		rack->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT;
6762 		rack->r_ctl.rc_last_output_to = us_cts + slot;
6763 		/*
6764 		 * A pacing timer (slot) is being set, in
6765 		 * such a case we cannot send (we are blocked by
6766 		 * the timer). So lets tell LRO that it should not
6767 		 * wake us unless there is a SACK. Note this only
6768 		 * will be effective if mbuf queueing is on or
6769 		 * compressed acks are being processed.
6770 		 */
6771 		tp->t_flags2 |= TF2_MBUF_QUEUE_READY;
6772 		/*
6773 		 * But wait if we have a Rack timer running
6774 		 * even a SACK should not disturb us (with
6775 		 * the exception of r_rr_config 3).
6776 		 */
6777 		if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK) ||
6778 		    (IN_RECOVERY(tp->t_flags))) {
6779 			if (rack->r_rr_config != 3)
6780 				tp->t_flags2 |= TF2_DONT_SACK_QUEUE;
6781 			else if (rack->rc_pace_dnd) {
6782 				/*
6783 				 * When DND is on, we only let a sack
6784 				 * interrupt us if we are not in recovery.
6785 				 *
6786 				 * If DND is off, then we never hit here
6787 				 * and let all sacks wake us up.
6788 				 *
6789 				 */
6790 				tp->t_flags2 |= TF2_DONT_SACK_QUEUE;
6791 			}
6792 		}
6793 		if (rack->rc_ack_can_sendout_data) {
6794 			/*
6795 			 * Ahh but wait, this is that special case
6796 			 * where the pacing timer can be disturbed
6797 			 * backout the changes (used for non-paced
6798 			 * burst limiting).
6799 			 */
6800 			tp->t_flags2 &= ~(TF2_DONT_SACK_QUEUE |
6801 			    TF2_MBUF_QUEUE_READY);
6802 		}
6803 		if ((rack->use_rack_rr) &&
6804 		    (rack->r_rr_config < 2) &&
6805 		    ((hpts_timeout) && (hpts_timeout < slot))) {
6806 			/*
6807 			 * Arrange for the hpts to kick back in after the
6808 			 * t-o if the t-o does not cause a send.
6809 			 */
6810 			(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(hpts_timeout),
6811 						   __LINE__, &diag);
6812 			rack_log_hpts_diag(rack, us_cts, &diag, &tv);
6813 			rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
6814 		} else {
6815 			(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(slot),
6816 						   __LINE__, &diag);
6817 			rack_log_hpts_diag(rack, us_cts, &diag, &tv);
6818 			rack_log_to_start(rack, cts, hpts_timeout, slot, 1);
6819 		}
6820 	} else if (hpts_timeout) {
6821 		/*
6822 		 * With respect to t_flags2(?) here, lets let any new acks wake
6823 		 * us up here. Since we are not pacing (no pacing timer), output
6824 		 * can happen so we should let it. If its a Rack timer, then any inbound
6825 		 * packet probably won't change the sending (we will be blocked)
6826 		 * but it may change the prr stats so letting it in (the set defaults
6827 		 * at the start of this block) are good enough.
6828 		 */
6829 		rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
6830 		(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(hpts_timeout),
6831 					   __LINE__, &diag);
6832 		rack_log_hpts_diag(rack, us_cts, &diag, &tv);
6833 		rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
6834 	} else {
6835 		/* No timer starting */
6836 #ifdef INVARIANTS
6837 		if (SEQ_GT(tp->snd_max, tp->snd_una)) {
6838 			panic("tp:%p rack:%p tlts:%d cts:%u slot:%u pto:%u -- no timer started?",
6839 			    tp, rack, tot_len_this_send, cts, slot, hpts_timeout);
6840 		}
6841 #endif
6842 	}
6843 	rack->rc_tmr_stopped = 0;
6844 	if (slot)
6845 		rack_log_type_bbrsnd(rack, tot_len_this_send, slot, us_cts, &tv, __LINE__);
6846 }
6847 
6848 static void
6849 rack_mark_lost(struct tcpcb *tp,
6850     struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t cts)
6851 {
6852 	struct rack_sendmap *nrsm;
6853 	uint32_t thresh,  exp;
6854 
6855 	thresh = rack_calc_thresh_rack(rack, rack_grab_rtt(tp, rack), cts, __LINE__, 0);
6856 	nrsm = rsm;
6857 	TAILQ_FOREACH_FROM(nrsm, &rack->r_ctl.rc_tmap, r_tnext) {
6858 		if ((nrsm->r_flags & RACK_SACK_PASSED) == 0) {
6859 			/* Got up to all that were marked sack-passed */
6860 			break;
6861 		}
6862 		if ((nrsm->r_flags & RACK_WAS_LOST) == 0) {
6863 			exp = ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]) + thresh;
6864 			if (TSTMP_LT(exp, cts) || (exp == cts)) {
6865 				/* We now consider it lost */
6866 				nrsm->r_flags |= RACK_WAS_LOST;
6867 				rack->r_ctl.rc_considered_lost += nrsm->r_end - nrsm->r_start;
6868 			} else {
6869 				/* Past here it won't be lost so stop */
6870 				break;
6871 			}
6872 		}
6873 	}
6874 }
6875 
6876 /*
6877  * RACK Timer, here we simply do logging and house keeping.
6878  * the normal rack_output() function will call the
6879  * appropriate thing to check if we need to do a RACK retransmit.
6880  * We return 1, saying don't proceed with rack_output only
6881  * when all timers have been stopped (destroyed PCB?).
6882  */
6883 static int
6884 rack_timeout_rack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6885 {
6886 	/*
6887 	 * This timer simply provides an internal trigger to send out data.
6888 	 * The check_recovery_mode call will see if there are needed
6889 	 * retransmissions, if so we will enter fast-recovery. The output
6890 	 * call may or may not do the same thing depending on sysctl
6891 	 * settings.
6892 	 */
6893 	struct rack_sendmap *rsm;
6894 
6895 	counter_u64_add(rack_to_tot, 1);
6896 	if (rack->r_state && (rack->r_state != tp->t_state))
6897 		rack_set_state(tp, rack);
6898 	rack->rc_on_min_to = 0;
6899 	rsm = rack_check_recovery_mode(tp, cts);
6900 	rack_log_to_event(rack, RACK_TO_FRM_RACK, rsm);
6901 	if (rsm) {
6902 		/* We need to stroke any lost that are now declared as lost */
6903 		rack_mark_lost(tp, rack, rsm, cts);
6904 		rack->r_ctl.rc_resend = rsm;
6905 		rack->r_timer_override = 1;
6906 		if (rack->use_rack_rr) {
6907 			/*
6908 			 * Don't accumulate extra pacing delay
6909 			 * we are allowing the rack timer to
6910 			 * over-ride pacing i.e. rrr takes precedence
6911 			 * if the pacing interval is longer than the rrr
6912 			 * time (in other words we get the min pacing
6913 			 * time versus rrr pacing time).
6914 			 */
6915 			rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
6916 		}
6917 	}
6918 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK;
6919 	if (rsm == NULL) {
6920 		/* restart a timer and return 1 */
6921 		rack_start_hpts_timer(rack, tp, cts,
6922 				      0, 0, 0);
6923 		return (1);
6924 	}
6925 	return (0);
6926 }
6927 
6928 
6929 
6930 static void
6931 rack_adjust_orig_mlen(struct rack_sendmap *rsm)
6932 {
6933 
6934 	if ((M_TRAILINGROOM(rsm->m) != rsm->orig_t_space)) {
6935 		/*
6936 		 * The trailing space changed, mbufs can grow
6937 		 * at the tail but they can't shrink from
6938 		 * it, KASSERT that. Adjust the orig_m_len to
6939 		 * compensate for this change.
6940 		 */
6941 		KASSERT((rsm->orig_t_space > M_TRAILINGROOM(rsm->m)),
6942 			("mbuf:%p rsm:%p trailing_space:%jd ots:%u oml:%u mlen:%u\n",
6943 			 rsm->m,
6944 			 rsm,
6945 			 (intmax_t)M_TRAILINGROOM(rsm->m),
6946 			 rsm->orig_t_space,
6947 			 rsm->orig_m_len,
6948 			 rsm->m->m_len));
6949 		rsm->orig_m_len += (rsm->orig_t_space - M_TRAILINGROOM(rsm->m));
6950 		rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
6951 	}
6952 	if (rsm->m->m_len < rsm->orig_m_len) {
6953 		/*
6954 		 * Mbuf shrank, trimmed off the top by an ack, our
6955 		 * offset changes.
6956 		 */
6957 		KASSERT((rsm->soff >= (rsm->orig_m_len - rsm->m->m_len)),
6958 			("mbuf:%p len:%u rsm:%p oml:%u soff:%u\n",
6959 			 rsm->m, rsm->m->m_len,
6960 			 rsm, rsm->orig_m_len,
6961 			 rsm->soff));
6962 		if (rsm->soff >= (rsm->orig_m_len - rsm->m->m_len))
6963 			rsm->soff -= (rsm->orig_m_len - rsm->m->m_len);
6964 		else
6965 			rsm->soff = 0;
6966 		rsm->orig_m_len = rsm->m->m_len;
6967 #ifdef INVARIANTS
6968 	} else if (rsm->m->m_len > rsm->orig_m_len) {
6969 		panic("rsm:%p m:%p m_len grew outside of t_space compensation",
6970 		      rsm, rsm->m);
6971 #endif
6972 	}
6973 }
6974 
6975 static void
6976 rack_setup_offset_for_rsm(struct tcp_rack *rack, struct rack_sendmap *src_rsm, struct rack_sendmap *rsm)
6977 {
6978 	struct mbuf *m;
6979 	uint32_t soff;
6980 
6981 	if (src_rsm->m &&
6982 	    ((src_rsm->orig_m_len != src_rsm->m->m_len) ||
6983 	     (M_TRAILINGROOM(src_rsm->m) != src_rsm->orig_t_space))) {
6984 		/* Fix up the orig_m_len and possibly the mbuf offset */
6985 		rack_adjust_orig_mlen(src_rsm);
6986 	}
6987 	m = src_rsm->m;
6988 	soff = src_rsm->soff + (src_rsm->r_end - src_rsm->r_start);
6989 	while (soff >= m->m_len) {
6990 		/* Move out past this mbuf */
6991 		soff -= m->m_len;
6992 		m = m->m_next;
6993 		KASSERT((m != NULL),
6994 			("rsm:%p nrsm:%p hit at soff:%u null m",
6995 			 src_rsm, rsm, soff));
6996 		if (m == NULL) {
6997 			/* This should *not* happen which is why there is a kassert */
6998 			src_rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd,
6999 					       (src_rsm->r_start - rack->rc_tp->snd_una),
7000 					       &src_rsm->soff);
7001 			src_rsm->orig_m_len = src_rsm->m->m_len;
7002 			src_rsm->orig_t_space = M_TRAILINGROOM(src_rsm->m);
7003 			rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd,
7004 					   (rsm->r_start - rack->rc_tp->snd_una),
7005 					   &rsm->soff);
7006 			rsm->orig_m_len = rsm->m->m_len;
7007 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
7008 			return;
7009 		}
7010 	}
7011 	rsm->m = m;
7012 	rsm->soff = soff;
7013 	rsm->orig_m_len = m->m_len;
7014 	rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
7015 }
7016 
7017 static __inline void
7018 rack_clone_rsm(struct tcp_rack *rack, struct rack_sendmap *nrsm,
7019 	       struct rack_sendmap *rsm, uint32_t start)
7020 {
7021 	int idx;
7022 
7023 	nrsm->r_start = start;
7024 	nrsm->r_end = rsm->r_end;
7025 	nrsm->r_rtr_cnt = rsm->r_rtr_cnt;
7026 	nrsm->r_act_rxt_cnt = rsm->r_act_rxt_cnt;
7027 	nrsm->r_flags = rsm->r_flags;
7028 	nrsm->r_dupack = rsm->r_dupack;
7029 	nrsm->r_no_rtt_allowed = rsm->r_no_rtt_allowed;
7030 	nrsm->r_rtr_bytes = 0;
7031 	nrsm->r_fas = rsm->r_fas;
7032 	nrsm->r_bas = rsm->r_bas;
7033 	tqhash_update_end(rack->r_ctl.tqh, rsm, nrsm->r_start);
7034 	nrsm->r_just_ret = rsm->r_just_ret;
7035 	for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) {
7036 		nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx];
7037 	}
7038 	/* Now if we have SYN flag we keep it on the left edge */
7039 	if (nrsm->r_flags & RACK_HAS_SYN)
7040 		nrsm->r_flags &= ~RACK_HAS_SYN;
7041 	/* Now if we have a FIN flag we keep it on the right edge */
7042 	if (rsm->r_flags & RACK_HAS_FIN)
7043 		rsm->r_flags &= ~RACK_HAS_FIN;
7044 	/* Push bit must go to the right edge as well */
7045 	if (rsm->r_flags & RACK_HAD_PUSH)
7046 		rsm->r_flags &= ~RACK_HAD_PUSH;
7047 	/* Clone over the state of the hw_tls flag */
7048 	nrsm->r_hw_tls = rsm->r_hw_tls;
7049 	/*
7050 	 * Now we need to find nrsm's new location in the mbuf chain
7051 	 * we basically calculate a new offset, which is soff +
7052 	 * how much is left in original rsm. Then we walk out the mbuf
7053 	 * chain to find the righ position, it may be the same mbuf
7054 	 * or maybe not.
7055 	 */
7056 	KASSERT(((rsm->m != NULL) ||
7057 		 (rsm->r_flags & (RACK_HAS_SYN|RACK_HAS_FIN))),
7058 		("rsm:%p nrsm:%p rack:%p -- rsm->m is NULL?", rsm, nrsm, rack));
7059 	if (rsm->m)
7060 		rack_setup_offset_for_rsm(rack, rsm, nrsm);
7061 }
7062 
7063 static struct rack_sendmap *
7064 rack_merge_rsm(struct tcp_rack *rack,
7065 	       struct rack_sendmap *l_rsm,
7066 	       struct rack_sendmap *r_rsm)
7067 {
7068 	/*
7069 	 * We are merging two ack'd RSM's,
7070 	 * the l_rsm is on the left (lower seq
7071 	 * values) and the r_rsm is on the right
7072 	 * (higher seq value). The simplest way
7073 	 * to merge these is to move the right
7074 	 * one into the left. I don't think there
7075 	 * is any reason we need to try to find
7076 	 * the oldest (or last oldest retransmitted).
7077 	 */
7078 	rack_log_map_chg(rack->rc_tp, rack, NULL,
7079 			 l_rsm, r_rsm, MAP_MERGE, r_rsm->r_end, __LINE__);
7080 	tqhash_update_end(rack->r_ctl.tqh, l_rsm, r_rsm->r_end);
7081 	if (l_rsm->r_dupack < r_rsm->r_dupack)
7082 		l_rsm->r_dupack = r_rsm->r_dupack;
7083 	if (r_rsm->r_rtr_bytes)
7084 		l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes;
7085 	if (r_rsm->r_in_tmap) {
7086 		/* This really should not happen */
7087 		TAILQ_REMOVE(&rack->r_ctl.rc_tmap, r_rsm, r_tnext);
7088 		r_rsm->r_in_tmap = 0;
7089 	}
7090 
7091 	/* Now the flags */
7092 	if (r_rsm->r_flags & RACK_HAS_FIN)
7093 		l_rsm->r_flags |= RACK_HAS_FIN;
7094 	if (r_rsm->r_flags & RACK_TLP)
7095 		l_rsm->r_flags |= RACK_TLP;
7096 	if (r_rsm->r_flags & RACK_RWND_COLLAPSED)
7097 		l_rsm->r_flags |= RACK_RWND_COLLAPSED;
7098 	if ((r_rsm->r_flags & RACK_APP_LIMITED)  &&
7099 	    ((l_rsm->r_flags & RACK_APP_LIMITED) == 0)) {
7100 		/*
7101 		 * If both are app-limited then let the
7102 		 * free lower the count. If right is app
7103 		 * limited and left is not, transfer.
7104 		 */
7105 		l_rsm->r_flags |= RACK_APP_LIMITED;
7106 		r_rsm->r_flags &= ~RACK_APP_LIMITED;
7107 		if (r_rsm == rack->r_ctl.rc_first_appl)
7108 			rack->r_ctl.rc_first_appl = l_rsm;
7109 	}
7110 	tqhash_remove(rack->r_ctl.tqh, r_rsm, REMOVE_TYPE_MERGE);
7111 	/*
7112 	 * We keep the largest value, which is the newest
7113 	 * send. We do this in case a segment that is
7114 	 * joined together and not part of a GP estimate
7115 	 * later gets expanded into the GP estimate.
7116 	 *
7117 	 * We prohibit the merging of unlike kinds i.e.
7118 	 * all pieces that are in the GP estimate can be
7119 	 * merged and all pieces that are not in a GP estimate
7120 	 * can be merged, but not disimilar pieces. Combine
7121 	 * this with taking the highest here and we should
7122 	 * be ok unless of course the client reneges. Then
7123 	 * all bets are off.
7124 	 */
7125 	if(l_rsm->r_tim_lastsent[(l_rsm->r_rtr_cnt-1)] <
7126 	   r_rsm->r_tim_lastsent[(r_rsm->r_rtr_cnt-1)]) {
7127 		l_rsm->r_tim_lastsent[(l_rsm->r_rtr_cnt-1)] = r_rsm->r_tim_lastsent[(r_rsm->r_rtr_cnt-1)];
7128 	}
7129 	/*
7130 	 * When merging two RSM's we also need to consider the ack time and keep
7131 	 * newest. If the ack gets merged into a measurement then that is the
7132 	 * one we will want to be using.
7133 	 */
7134 	if(l_rsm->r_ack_arrival	 < r_rsm->r_ack_arrival)
7135 		l_rsm->r_ack_arrival = r_rsm->r_ack_arrival;
7136 
7137 	if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) {
7138 		/* Transfer the split limit to the map we free */
7139 		r_rsm->r_limit_type = l_rsm->r_limit_type;
7140 		l_rsm->r_limit_type = 0;
7141 	}
7142 	rack_free(rack, r_rsm);
7143 	l_rsm->r_flags |= RACK_MERGED;
7144 	return (l_rsm);
7145 }
7146 
7147 /*
7148  * TLP Timer, here we simply setup what segment we want to
7149  * have the TLP expire on, the normal rack_output() will then
7150  * send it out.
7151  *
7152  * We return 1, saying don't proceed with rack_output only
7153  * when all timers have been stopped (destroyed PCB?).
7154  */
7155 static int
7156 rack_timeout_tlp(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t *doing_tlp)
7157 {
7158 	/*
7159 	 * Tail Loss Probe.
7160 	 */
7161 	struct rack_sendmap *rsm = NULL;
7162 	int insret __diagused;
7163 	struct socket *so = tptosocket(tp);
7164 	uint32_t amm;
7165 	uint32_t out, avail;
7166 	int collapsed_win = 0;
7167 
7168 	if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
7169 		/* Its not time yet */
7170 		return (0);
7171 	}
7172 	if (ctf_progress_timeout_check(tp, true)) {
7173 		rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
7174 		return (-ETIMEDOUT);	/* tcp_drop() */
7175 	}
7176 	/*
7177 	 * A TLP timer has expired. We have been idle for 2 rtts. So we now
7178 	 * need to figure out how to force a full MSS segment out.
7179 	 */
7180 	rack_log_to_event(rack, RACK_TO_FRM_TLP, NULL);
7181 	rack->r_ctl.retran_during_recovery = 0;
7182 	rack->r_might_revert = 0;
7183 	rack->r_ctl.dsack_byte_cnt = 0;
7184 	counter_u64_add(rack_tlp_tot, 1);
7185 	if (rack->r_state && (rack->r_state != tp->t_state))
7186 		rack_set_state(tp, rack);
7187 	avail = sbavail(&so->so_snd);
7188 	out = tp->snd_max - tp->snd_una;
7189 	if ((out > tp->snd_wnd) || rack->rc_has_collapsed) {
7190 		/* special case, we need a retransmission */
7191 		collapsed_win = 1;
7192 		goto need_retran;
7193 	}
7194 	if (rack->r_ctl.dsack_persist && (rack->r_ctl.rc_tlp_cnt_out >= 1)) {
7195 		rack->r_ctl.dsack_persist--;
7196 		if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
7197 			rack->r_ctl.num_dsack = 0;
7198 		}
7199 		rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
7200 	}
7201 	if ((tp->t_flags & TF_GPUTINPROG) &&
7202 	    (rack->r_ctl.rc_tlp_cnt_out == 1)) {
7203 		/*
7204 		 * If this is the second in a row
7205 		 * TLP and we are doing a measurement
7206 		 * its time to abandon the measurement.
7207 		 * Something is likely broken on
7208 		 * the clients network and measuring a
7209 		 * broken network does us no good.
7210 		 */
7211 		tp->t_flags &= ~TF_GPUTINPROG;
7212 		rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
7213 					   rack->r_ctl.rc_gp_srtt /*flex1*/,
7214 					   tp->gput_seq,
7215 					   0, 0, 18, __LINE__, NULL, 0);
7216 	}
7217 	/*
7218 	 * Check our send oldest always settings, and if
7219 	 * there is an oldest to send jump to the need_retran.
7220 	 */
7221 	if (rack_always_send_oldest && (TAILQ_EMPTY(&rack->r_ctl.rc_tmap) == 0))
7222 		goto need_retran;
7223 
7224 	if (avail > out) {
7225 		/* New data is available */
7226 		amm = avail - out;
7227 		if (amm > ctf_fixed_maxseg(tp)) {
7228 			amm = ctf_fixed_maxseg(tp);
7229 			if ((amm + out) > tp->snd_wnd) {
7230 				/* We are rwnd limited */
7231 				goto need_retran;
7232 			}
7233 		} else if (amm < ctf_fixed_maxseg(tp)) {
7234 			/* not enough to fill a MTU */
7235 			goto need_retran;
7236 		}
7237 		if (IN_FASTRECOVERY(tp->t_flags)) {
7238 			/* Unlikely */
7239 			if (rack->rack_no_prr == 0) {
7240 				if (out + amm <= tp->snd_wnd) {
7241 					rack->r_ctl.rc_prr_sndcnt = amm;
7242 					rack->r_ctl.rc_tlp_new_data = amm;
7243 					rack_log_to_prr(rack, 4, 0, __LINE__);
7244 				}
7245 			} else
7246 				goto need_retran;
7247 		} else {
7248 			/* Set the send-new override */
7249 			if (out + amm <= tp->snd_wnd)
7250 				rack->r_ctl.rc_tlp_new_data = amm;
7251 			else
7252 				goto need_retran;
7253 		}
7254 		rack->r_ctl.rc_tlpsend = NULL;
7255 		counter_u64_add(rack_tlp_newdata, 1);
7256 		goto send;
7257 	}
7258 need_retran:
7259 	/*
7260 	 * Ok we need to arrange the last un-acked segment to be re-sent, or
7261 	 * optionally the first un-acked segment.
7262 	 */
7263 	if (collapsed_win == 0) {
7264 		if (rack_always_send_oldest)
7265 			rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
7266 		else {
7267 			rsm = tqhash_max(rack->r_ctl.tqh);
7268 			if (rsm && (rsm->r_flags & (RACK_ACKED | RACK_HAS_FIN))) {
7269 				rsm = rack_find_high_nonack(rack, rsm);
7270 			}
7271 		}
7272 		if (rsm == NULL) {
7273 #ifdef TCP_BLACKBOX
7274 			tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true);
7275 #endif
7276 			goto out;
7277 		}
7278 	} else {
7279 		/*
7280 		 * We had a collapsed window, lets find
7281 		 * the point before the collapse.
7282 		 */
7283 		if (SEQ_GT((rack->r_ctl.last_collapse_point - 1), rack->rc_tp->snd_una))
7284 			rsm = tqhash_find(rack->r_ctl.tqh, (rack->r_ctl.last_collapse_point - 1));
7285 		else {
7286 			rsm = tqhash_min(rack->r_ctl.tqh);
7287 		}
7288 		if (rsm == NULL) {
7289 			/* Huh */
7290 			goto out;
7291 		}
7292 	}
7293 	if ((rsm->r_end - rsm->r_start) > ctf_fixed_maxseg(tp)) {
7294 		/*
7295 		 * We need to split this the last segment in two.
7296 		 */
7297 		struct rack_sendmap *nrsm;
7298 
7299 		nrsm = rack_alloc_full_limit(rack);
7300 		if (nrsm == NULL) {
7301 			/*
7302 			 * No memory to split, we will just exit and punt
7303 			 * off to the RXT timer.
7304 			 */
7305 			goto out;
7306 		}
7307 		rack_clone_rsm(rack, nrsm, rsm,
7308 			       (rsm->r_end - ctf_fixed_maxseg(tp)));
7309 		rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
7310 #ifndef INVARIANTS
7311 		(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
7312 #else
7313 		if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
7314 			panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p",
7315 			      nrsm, insret, rack, rsm);
7316 		}
7317 #endif
7318 		if (rsm->r_in_tmap) {
7319 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7320 			nrsm->r_in_tmap = 1;
7321 		}
7322 		rsm = nrsm;
7323 	}
7324 	rack->r_ctl.rc_tlpsend = rsm;
7325 send:
7326 	/* Make sure output path knows we are doing a TLP */
7327 	*doing_tlp = 1;
7328 	rack->r_timer_override = 1;
7329 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
7330 	return (0);
7331 out:
7332 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
7333 	return (0);
7334 }
7335 
7336 /*
7337  * Delayed ack Timer, here we simply need to setup the
7338  * ACK_NOW flag and remove the DELACK flag. From there
7339  * the output routine will send the ack out.
7340  *
7341  * We only return 1, saying don't proceed, if all timers
7342  * are stopped (destroyed PCB?).
7343  */
7344 static int
7345 rack_timeout_delack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
7346 {
7347 
7348 	rack_log_to_event(rack, RACK_TO_FRM_DELACK, NULL);
7349 	tp->t_flags &= ~TF_DELACK;
7350 	tp->t_flags |= TF_ACKNOW;
7351 	KMOD_TCPSTAT_INC(tcps_delack);
7352 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
7353 	return (0);
7354 }
7355 
7356 static inline int
7357 rack_send_ack_challange(struct tcp_rack *rack)
7358 {
7359 	struct tcptemp *t_template;
7360 
7361 	t_template = tcpip_maketemplate(rack->rc_inp);
7362 	if (t_template) {
7363 		if (rack->forced_ack == 0) {
7364 			rack->forced_ack = 1;
7365 			rack->r_ctl.forced_ack_ts = tcp_get_usecs(NULL);
7366 		} else {
7367 			rack->probe_not_answered = 1;
7368 		}
7369 		tcp_respond(rack->rc_tp, t_template->tt_ipgen,
7370 			    &t_template->tt_t, (struct mbuf *)NULL,
7371 			    rack->rc_tp->rcv_nxt, rack->rc_tp->snd_una - 1, 0);
7372 		free(t_template, M_TEMP);
7373 		/* This does send an ack so kill any D-ack timer */
7374 		if (rack->rc_tp->t_flags & TF_DELACK)
7375 			rack->rc_tp->t_flags &= ~TF_DELACK;
7376 		return(1);
7377 	} else
7378 		return (0);
7379 
7380 }
7381 
7382 /*
7383  * Persists timer, here we simply send the
7384  * same thing as a keepalive will.
7385  * the one byte send.
7386  *
7387  * We only return 1, saying don't proceed, if all timers
7388  * are stopped (destroyed PCB?).
7389  */
7390 static int
7391 rack_timeout_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
7392 {
7393 	int32_t retval = 1;
7394 
7395 	if (rack->rc_in_persist == 0)
7396 		return (0);
7397 	if (ctf_progress_timeout_check(tp, false)) {
7398 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
7399 		rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
7400 		counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
7401 		return (-ETIMEDOUT);	/* tcp_drop() */
7402 	}
7403 	/*
7404 	 * Persistence timer into zero window. Force a byte to be output, if
7405 	 * possible.
7406 	 */
7407 	KMOD_TCPSTAT_INC(tcps_persisttimeo);
7408 	/*
7409 	 * Hack: if the peer is dead/unreachable, we do not time out if the
7410 	 * window is closed.  After a full backoff, drop the connection if
7411 	 * the idle time (no responses to probes) reaches the maximum
7412 	 * backoff that we would use if retransmitting.
7413 	 */
7414 	if (tp->t_rxtshift >= V_tcp_retries &&
7415 	    (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
7416 	     TICKS_2_USEC(ticks - tp->t_rcvtime) >= RACK_REXMTVAL(tp) * tcp_totbackoff)) {
7417 		KMOD_TCPSTAT_INC(tcps_persistdrop);
7418 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
7419 		counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
7420 		retval = -ETIMEDOUT;	/* tcp_drop() */
7421 		goto out;
7422 	}
7423 	if ((sbavail(&rack->rc_inp->inp_socket->so_snd) == 0) &&
7424 	    tp->snd_una == tp->snd_max)
7425 		rack_exit_persist(tp, rack, cts);
7426 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT;
7427 	/*
7428 	 * If the user has closed the socket then drop a persisting
7429 	 * connection after a much reduced timeout.
7430 	 */
7431 	if (tp->t_state > TCPS_CLOSE_WAIT &&
7432 	    (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
7433 		KMOD_TCPSTAT_INC(tcps_persistdrop);
7434 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
7435 		counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
7436 		retval = -ETIMEDOUT;	/* tcp_drop() */
7437 		goto out;
7438 	}
7439 	if (rack_send_ack_challange(rack)) {
7440 		/* only set it if we were answered */
7441 		if (rack->probe_not_answered) {
7442 			counter_u64_add(rack_persists_loss, 1);
7443 			rack->r_ctl.persist_lost_ends++;
7444 		}
7445 		counter_u64_add(rack_persists_sends, 1);
7446 		counter_u64_add(rack_out_size[TCP_MSS_ACCT_PERSIST], 1);
7447 	}
7448 	if (tp->t_rxtshift < V_tcp_retries)
7449 		tp->t_rxtshift++;
7450 out:
7451 	rack_log_to_event(rack, RACK_TO_FRM_PERSIST, NULL);
7452 	rack_start_hpts_timer(rack, tp, cts,
7453 			      0, 0, 0);
7454 	return (retval);
7455 }
7456 
7457 /*
7458  * If a keepalive goes off, we had no other timers
7459  * happening. We always return 1 here since this
7460  * routine either drops the connection or sends
7461  * out a segment with respond.
7462  */
7463 static int
7464 rack_timeout_keepalive(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
7465 {
7466 	struct inpcb *inp = tptoinpcb(tp);
7467 
7468 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP;
7469 	rack_log_to_event(rack, RACK_TO_FRM_KEEP, NULL);
7470 	/*
7471 	 * Keep-alive timer went off; send something or drop connection if
7472 	 * idle for too long.
7473 	 */
7474 	KMOD_TCPSTAT_INC(tcps_keeptimeo);
7475 	if (tp->t_state < TCPS_ESTABLISHED)
7476 		goto dropit;
7477 	if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
7478 	    tp->t_state <= TCPS_CLOSING) {
7479 		if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
7480 			goto dropit;
7481 		/*
7482 		 * Send a packet designed to force a response if the peer is
7483 		 * up and reachable: either an ACK if the connection is
7484 		 * still alive, or an RST if the peer has closed the
7485 		 * connection due to timeout or reboot. Using sequence
7486 		 * number tp->snd_una-1 causes the transmitted zero-length
7487 		 * segment to lie outside the receive window; by the
7488 		 * protocol spec, this requires the correspondent TCP to
7489 		 * respond.
7490 		 */
7491 		KMOD_TCPSTAT_INC(tcps_keepprobe);
7492 		rack_send_ack_challange(rack);
7493 	}
7494 	rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
7495 	return (1);
7496 dropit:
7497 	KMOD_TCPSTAT_INC(tcps_keepdrops);
7498 	tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
7499 	return (-ETIMEDOUT);	/* tcp_drop() */
7500 }
7501 
7502 /*
7503  * Retransmit helper function, clear up all the ack
7504  * flags and take care of important book keeping.
7505  */
7506 static void
7507 rack_remxt_tmr(struct tcpcb *tp)
7508 {
7509 	/*
7510 	 * The retransmit timer went off, all sack'd blocks must be
7511 	 * un-acked.
7512 	 */
7513 	struct rack_sendmap *rsm, *trsm = NULL;
7514 	struct tcp_rack *rack;
7515 
7516 	rack = (struct tcp_rack *)tp->t_fb_ptr;
7517 	rack_timer_cancel(tp, rack, tcp_get_usecs(NULL), __LINE__);
7518 	rack_log_to_event(rack, RACK_TO_FRM_TMR, NULL);
7519 	rack->r_timer_override = 1;
7520 	rack->r_ctl.rc_snd_max_at_rto = tp->snd_max;
7521 	rack->r_ctl.rc_last_timeout_snduna = tp->snd_una;
7522 	rack->r_late = 0;
7523 	rack->r_early = 0;
7524 	rack->r_ctl.rc_agg_delayed = 0;
7525 	rack->r_ctl.rc_agg_early = 0;
7526 	if (rack->r_state && (rack->r_state != tp->t_state))
7527 		rack_set_state(tp, rack);
7528 	if (tp->t_rxtshift <= rack_rxt_scoreboard_clear_thresh) {
7529 		/*
7530 		 * We do not clear the scoreboard until we have had
7531 		 * more than rack_rxt_scoreboard_clear_thresh time-outs.
7532 		 */
7533 		rack->r_ctl.rc_resend = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
7534 		if (rack->r_ctl.rc_resend != NULL)
7535 			rack->r_ctl.rc_resend->r_flags |= RACK_TO_REXT;
7536 
7537 		return;
7538 	}
7539 	/*
7540 	 * Ideally we would like to be able to
7541 	 * mark SACK-PASS on anything not acked here.
7542 	 *
7543 	 * However, if we do that we would burst out
7544 	 * all that data 1ms apart. This would be unwise,
7545 	 * so for now we will just let the normal rxt timer
7546 	 * and tlp timer take care of it.
7547 	 *
7548 	 * Also we really need to stick them back in sequence
7549 	 * order. This way we send in the proper order and any
7550 	 * sacks that come floating in will "re-ack" the data.
7551 	 * To do this we zap the tmap with an INIT and then
7552 	 * walk through and place every rsm in the tail queue
7553 	 * hash table back in its seq ordered place.
7554 	 */
7555 	TAILQ_INIT(&rack->r_ctl.rc_tmap);
7556 
7557 	TQHASH_FOREACH(rsm, rack->r_ctl.tqh)  {
7558 		rsm->r_dupack = 0;
7559 		if (rack_verbose_logging)
7560 			rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
7561 		/* We must re-add it back to the tlist */
7562 		if (trsm == NULL) {
7563 			TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
7564 		} else {
7565 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, trsm, rsm, r_tnext);
7566 		}
7567 		rsm->r_in_tmap = 1;
7568 		trsm = rsm;
7569 		if (rsm->r_flags & RACK_ACKED)
7570 			rsm->r_flags |= RACK_WAS_ACKED;
7571 		rsm->r_flags &= ~(RACK_ACKED | RACK_SACK_PASSED | RACK_WAS_SACKPASS | RACK_RWND_COLLAPSED | RACK_WAS_LOST);
7572 		rsm->r_flags |= RACK_MUST_RXT;
7573 	}
7574 	/* zero the lost since it's all gone */
7575 	rack->r_ctl.rc_considered_lost = 0;
7576 	/* Clear the count (we just un-acked them) */
7577 	rack->r_ctl.rc_sacked = 0;
7578 	rack->r_ctl.rc_sacklast = NULL;
7579 	/* Clear the tlp rtx mark */
7580 	rack->r_ctl.rc_resend = tqhash_min(rack->r_ctl.tqh);
7581 	if (rack->r_ctl.rc_resend != NULL)
7582 		rack->r_ctl.rc_resend->r_flags |= RACK_TO_REXT;
7583 	rack->r_ctl.rc_prr_sndcnt = 0;
7584 	rack_log_to_prr(rack, 6, 0, __LINE__);
7585 	rack->r_ctl.rc_resend = tqhash_min(rack->r_ctl.tqh);
7586 	if (rack->r_ctl.rc_resend != NULL)
7587 		rack->r_ctl.rc_resend->r_flags |= RACK_TO_REXT;
7588 	if (((tp->t_flags & TF_SACK_PERMIT) == 0) &&
7589 	    ((tp->t_flags & TF_SENTFIN) == 0)) {
7590 		/*
7591 		 * For non-sack customers new data
7592 		 * needs to go out as retransmits until
7593 		 * we retransmit up to snd_max.
7594 		 */
7595 		rack->r_must_retran = 1;
7596 		rack->r_ctl.rc_out_at_rto = ctf_flight_size(rack->rc_tp,
7597 							    rack->r_ctl.rc_sacked);
7598 	}
7599 }
7600 
7601 static void
7602 rack_convert_rtts(struct tcpcb *tp)
7603 {
7604 	tcp_change_time_units(tp, TCP_TMR_GRANULARITY_USEC);
7605 	tp->t_rxtcur = RACK_REXMTVAL(tp);
7606 	if (TCPS_HAVEESTABLISHED(tp->t_state)) {
7607 		tp->t_rxtcur += TICKS_2_USEC(tcp_rexmit_slop);
7608 	}
7609 	if (tp->t_rxtcur > rack_rto_max) {
7610 		tp->t_rxtcur = rack_rto_max;
7611 	}
7612 }
7613 
7614 static void
7615 rack_cc_conn_init(struct tcpcb *tp)
7616 {
7617 	struct tcp_rack *rack;
7618 	uint32_t srtt;
7619 
7620 	rack = (struct tcp_rack *)tp->t_fb_ptr;
7621 	srtt = tp->t_srtt;
7622 	cc_conn_init(tp);
7623 	/*
7624 	 * Now convert to rack's internal format,
7625 	 * if required.
7626 	 */
7627 	if ((srtt == 0) && (tp->t_srtt != 0))
7628 		rack_convert_rtts(tp);
7629 	/*
7630 	 * We want a chance to stay in slowstart as
7631 	 * we create a connection. TCP spec says that
7632 	 * initially ssthresh is infinite. For our
7633 	 * purposes that is the snd_wnd.
7634 	 */
7635 	if (tp->snd_ssthresh < tp->snd_wnd) {
7636 		tp->snd_ssthresh = tp->snd_wnd;
7637 	}
7638 	/*
7639 	 * We also want to assure a IW worth of
7640 	 * data can get inflight.
7641 	 */
7642 	if (rc_init_window(rack) < tp->snd_cwnd)
7643 		tp->snd_cwnd = rc_init_window(rack);
7644 }
7645 
7646 /*
7647  * Re-transmit timeout! If we drop the PCB we will return 1, otherwise
7648  * we will setup to retransmit the lowest seq number outstanding.
7649  */
7650 static int
7651 rack_timeout_rxt(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
7652 {
7653 	struct inpcb *inp = tptoinpcb(tp);
7654 	int32_t rexmt;
7655 	int32_t retval = 0;
7656 	bool isipv6;
7657 
7658 	if ((tp->t_flags & TF_GPUTINPROG) &&
7659 	    (tp->t_rxtshift)) {
7660 		/*
7661 		 * We have had a second timeout
7662 		 * measurements on successive rxt's are not profitable.
7663 		 * It is unlikely to be of any use (the network is
7664 		 * broken or the client went away).
7665 		 */
7666 		tp->t_flags &= ~TF_GPUTINPROG;
7667 		rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
7668 					   rack->r_ctl.rc_gp_srtt /*flex1*/,
7669 					   tp->gput_seq,
7670 					   0, 0, 18, __LINE__, NULL, 0);
7671 	}
7672 	if (ctf_progress_timeout_check(tp, false)) {
7673 		tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
7674 		rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
7675 		return (-ETIMEDOUT);	/* tcp_drop() */
7676 	}
7677 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT;
7678 	rack->r_ctl.retran_during_recovery = 0;
7679 	rack->rc_ack_required = 1;
7680 	rack->r_ctl.dsack_byte_cnt = 0;
7681 	if (IN_RECOVERY(tp->t_flags) &&
7682 	    (rack->rto_from_rec == 0)) {
7683 		/*
7684 		 * Mark that we had a rto while in recovery
7685 		 * and save the ssthresh so if we go back
7686 		 * into recovery we will have a chance
7687 		 * to slowstart back to the level.
7688 		 */
7689 		rack->rto_from_rec = 1;
7690 		rack->r_ctl.rto_ssthresh = tp->snd_ssthresh;
7691 	}
7692 	if (IN_FASTRECOVERY(tp->t_flags))
7693 		tp->t_flags |= TF_WASFRECOVERY;
7694 	else
7695 		tp->t_flags &= ~TF_WASFRECOVERY;
7696 	if (IN_CONGRECOVERY(tp->t_flags))
7697 		tp->t_flags |= TF_WASCRECOVERY;
7698 	else
7699 		tp->t_flags &= ~TF_WASCRECOVERY;
7700 	if (TCPS_HAVEESTABLISHED(tp->t_state) &&
7701 	    (tp->snd_una == tp->snd_max)) {
7702 		/* Nothing outstanding .. nothing to do */
7703 		return (0);
7704 	}
7705 	if (rack->r_ctl.dsack_persist) {
7706 		rack->r_ctl.dsack_persist--;
7707 		if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
7708 			rack->r_ctl.num_dsack = 0;
7709 		}
7710 		rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
7711 	}
7712 	/*
7713 	 * Rack can only run one timer  at a time, so we cannot
7714 	 * run a KEEPINIT (gating SYN sending) and a retransmit
7715 	 * timer for the SYN. So if we are in a front state and
7716 	 * have a KEEPINIT timer we need to check the first transmit
7717 	 * against now to see if we have exceeded the KEEPINIT time
7718 	 * (if one is set).
7719 	 */
7720 	if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) &&
7721 	    (TP_KEEPINIT(tp) != 0)) {
7722 		struct rack_sendmap *rsm;
7723 
7724 		rsm = tqhash_min(rack->r_ctl.tqh);
7725 		if (rsm) {
7726 			/* Ok we have something outstanding to test keepinit with */
7727 			if ((TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) &&
7728 			    ((cts - (uint32_t)rsm->r_tim_lastsent[0]) >= TICKS_2_USEC(TP_KEEPINIT(tp)))) {
7729 				/* We have exceeded the KEEPINIT time */
7730 				tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
7731 				goto drop_it;
7732 			}
7733 		}
7734 	}
7735 	/*
7736 	 * Retransmission timer went off.  Message has not been acked within
7737 	 * retransmit interval.  Back off to a longer retransmit interval
7738 	 * and retransmit one segment.
7739 	 */
7740 	if ((rack->r_ctl.rc_resend == NULL) ||
7741 	    ((rack->r_ctl.rc_resend->r_flags & RACK_RWND_COLLAPSED) == 0)) {
7742 		/*
7743 		 * If the rwnd collapsed on
7744 		 * the one we are retransmitting
7745 		 * it does not count against the
7746 		 * rxt count.
7747 		 */
7748 		tp->t_rxtshift++;
7749 	}
7750 	rack_remxt_tmr(tp);
7751 	if (tp->t_rxtshift > V_tcp_retries) {
7752 		tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
7753 drop_it:
7754 		tp->t_rxtshift = V_tcp_retries;
7755 		KMOD_TCPSTAT_INC(tcps_timeoutdrop);
7756 		/* XXXGL: previously t_softerror was casted to uint16_t */
7757 		MPASS(tp->t_softerror >= 0);
7758 		retval = tp->t_softerror ? -tp->t_softerror : -ETIMEDOUT;
7759 		goto out;	/* tcp_drop() */
7760 	}
7761 	if (tp->t_state == TCPS_SYN_SENT) {
7762 		/*
7763 		 * If the SYN was retransmitted, indicate CWND to be limited
7764 		 * to 1 segment in cc_conn_init().
7765 		 */
7766 		tp->snd_cwnd = 1;
7767 	} else if (tp->t_rxtshift == 1) {
7768 		/*
7769 		 * first retransmit; record ssthresh and cwnd so they can be
7770 		 * recovered if this turns out to be a "bad" retransmit. A
7771 		 * retransmit is considered "bad" if an ACK for this segment
7772 		 * is received within RTT/2 interval; the assumption here is
7773 		 * that the ACK was already in flight.  See "On Estimating
7774 		 * End-to-End Network Path Properties" by Allman and Paxson
7775 		 * for more details.
7776 		 */
7777 		tp->snd_cwnd_prev = tp->snd_cwnd;
7778 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
7779 		tp->snd_recover_prev = tp->snd_recover;
7780 		tp->t_badrxtwin = ticks + (USEC_2_TICKS(tp->t_srtt)/2);
7781 		tp->t_flags |= TF_PREVVALID;
7782 	} else if ((tp->t_flags & TF_RCVD_TSTMP) == 0)
7783 		tp->t_flags &= ~TF_PREVVALID;
7784 	KMOD_TCPSTAT_INC(tcps_rexmttimeo);
7785 	if ((tp->t_state == TCPS_SYN_SENT) ||
7786 	    (tp->t_state == TCPS_SYN_RECEIVED))
7787 		rexmt = RACK_INITIAL_RTO * tcp_backoff[tp->t_rxtshift];
7788 	else
7789 		rexmt = max(rack_rto_min, (tp->t_srtt + (tp->t_rttvar << 2))) * tcp_backoff[tp->t_rxtshift];
7790 
7791 	RACK_TCPT_RANGESET(tp->t_rxtcur, rexmt,
7792 	   max(rack_rto_min, rexmt), rack_rto_max, rack->r_ctl.timer_slop);
7793 	/*
7794 	 * We enter the path for PLMTUD if connection is established or, if
7795 	 * connection is FIN_WAIT_1 status, reason for the last is that if
7796 	 * amount of data we send is very small, we could send it in couple
7797 	 * of packets and process straight to FIN. In that case we won't
7798 	 * catch ESTABLISHED state.
7799 	 */
7800 #ifdef INET6
7801 	isipv6 = (inp->inp_vflag & INP_IPV6) ? true : false;
7802 #else
7803 	isipv6 = false;
7804 #endif
7805 	if (((V_tcp_pmtud_blackhole_detect == 1) ||
7806 	    (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) ||
7807 	    (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) &&
7808 	    ((tp->t_state == TCPS_ESTABLISHED) ||
7809 	    (tp->t_state == TCPS_FIN_WAIT_1))) {
7810 		/*
7811 		 * Idea here is that at each stage of mtu probe (usually,
7812 		 * 1448 -> 1188 -> 524) should be given 2 chances to recover
7813 		 * before further clamping down. 'tp->t_rxtshift % 2 == 0'
7814 		 * should take care of that.
7815 		 */
7816 		if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) ==
7817 		    (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) &&
7818 		    (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 &&
7819 		    tp->t_rxtshift % 2 == 0)) {
7820 			/*
7821 			 * Enter Path MTU Black-hole Detection mechanism: -
7822 			 * Disable Path MTU Discovery (IP "DF" bit). -
7823 			 * Reduce MTU to lower value than what we negotiated
7824 			 * with peer.
7825 			 */
7826 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
7827 				/* Record that we may have found a black hole. */
7828 				tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
7829 				/* Keep track of previous MSS. */
7830 				tp->t_pmtud_saved_maxseg = tp->t_maxseg;
7831 			}
7832 
7833 			/*
7834 			 * Reduce the MSS to blackhole value or to the
7835 			 * default in an attempt to retransmit.
7836 			 */
7837 #ifdef INET6
7838 			if (isipv6 &&
7839 			    tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) {
7840 				/* Use the sysctl tuneable blackhole MSS. */
7841 				tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
7842 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
7843 			} else if (isipv6) {
7844 				/* Use the default MSS. */
7845 				tp->t_maxseg = V_tcp_v6mssdflt;
7846 				/*
7847 				 * Disable Path MTU Discovery when we switch
7848 				 * to minmss.
7849 				 */
7850 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
7851 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
7852 			}
7853 #endif
7854 #if defined(INET6) && defined(INET)
7855 			else
7856 #endif
7857 #ifdef INET
7858 			if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) {
7859 				/* Use the sysctl tuneable blackhole MSS. */
7860 				tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
7861 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
7862 			} else {
7863 				/* Use the default MSS. */
7864 				tp->t_maxseg = V_tcp_mssdflt;
7865 				/*
7866 				 * Disable Path MTU Discovery when we switch
7867 				 * to minmss.
7868 				 */
7869 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
7870 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
7871 			}
7872 #endif
7873 		} else {
7874 			/*
7875 			 * If further retransmissions are still unsuccessful
7876 			 * with a lowered MTU, maybe this isn't a blackhole
7877 			 * and we restore the previous MSS and blackhole
7878 			 * detection flags. The limit '6' is determined by
7879 			 * giving each probe stage (1448, 1188, 524) 2
7880 			 * chances to recover.
7881 			 */
7882 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
7883 			    (tp->t_rxtshift >= 6)) {
7884 				tp->t_flags2 |= TF2_PLPMTU_PMTUD;
7885 				tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
7886 				tp->t_maxseg = tp->t_pmtud_saved_maxseg;
7887 				if (tp->t_maxseg < V_tcp_mssdflt) {
7888 					/*
7889 					 * The MSS is so small we should not
7890 					 * process incoming SACK's since we are
7891 					 * subject to attack in such a case.
7892 					 */
7893 					tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT;
7894 				} else {
7895 					tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT;
7896 				}
7897 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed);
7898 			}
7899 		}
7900 	}
7901 	/*
7902 	 * Disable RFC1323 and SACK if we haven't got any response to
7903 	 * our third SYN to work-around some broken terminal servers
7904 	 * (most of which have hopefully been retired) that have bad VJ
7905 	 * header compression code which trashes TCP segments containing
7906 	 * unknown-to-them TCP options.
7907 	 */
7908 	if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
7909 	    (tp->t_rxtshift == 3))
7910 		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
7911 	/*
7912 	 * If we backed off this far, our srtt estimate is probably bogus.
7913 	 * Clobber it so we'll take the next rtt measurement as our srtt;
7914 	 * move the current srtt into rttvar to keep the current retransmit
7915 	 * times until then.
7916 	 */
7917 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
7918 #ifdef INET6
7919 		if ((inp->inp_vflag & INP_IPV6) != 0)
7920 			in6_losing(inp);
7921 		else
7922 #endif
7923 			in_losing(inp);
7924 		tp->t_rttvar += tp->t_srtt;
7925 		tp->t_srtt = 0;
7926 	}
7927 	sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
7928 	tp->snd_recover = tp->snd_max;
7929 	tp->t_flags |= TF_ACKNOW;
7930 	tp->t_rtttime = 0;
7931 	rack_cong_signal(tp, CC_RTO, tp->snd_una, __LINE__);
7932 out:
7933 	return (retval);
7934 }
7935 
7936 static int
7937 rack_process_timers(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t hpts_calling, uint8_t *doing_tlp)
7938 {
7939 	int32_t ret = 0;
7940 	int32_t timers = (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK);
7941 
7942 	if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
7943 	    (tp->t_flags & TF_GPUTINPROG)) {
7944 		/*
7945 		 * We have a goodput in progress
7946 		 * and we have entered a late state.
7947 		 * Do we have enough data in the sb
7948 		 * to handle the GPUT request?
7949 		 */
7950 		uint32_t bytes;
7951 
7952 		bytes = tp->gput_ack - tp->gput_seq;
7953 		if (SEQ_GT(tp->gput_seq, tp->snd_una))
7954 			bytes += tp->gput_seq - tp->snd_una;
7955 		if (bytes > sbavail(&tptosocket(tp)->so_snd)) {
7956 			/*
7957 			 * There are not enough bytes in the socket
7958 			 * buffer that have been sent to cover this
7959 			 * measurement. Cancel it.
7960 			 */
7961 			rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
7962 						   rack->r_ctl.rc_gp_srtt /*flex1*/,
7963 						   tp->gput_seq,
7964 						   0, 0, 18, __LINE__, NULL, 0);
7965 			tp->t_flags &= ~TF_GPUTINPROG;
7966 		}
7967 	}
7968 	if (timers == 0) {
7969 		return (0);
7970 	}
7971 	if (tp->t_state == TCPS_LISTEN) {
7972 		/* no timers on listen sockets */
7973 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)
7974 			return (0);
7975 		return (1);
7976 	}
7977 	if ((timers & PACE_TMR_RACK) &&
7978 	    rack->rc_on_min_to) {
7979 		/*
7980 		 * For the rack timer when we
7981 		 * are on a min-timeout (which means rrr_conf = 3)
7982 		 * we don't want to check the timer. It may
7983 		 * be going off for a pace and thats ok we
7984 		 * want to send the retransmit (if its ready).
7985 		 *
7986 		 * If its on a normal rack timer (non-min) then
7987 		 * we will check if its expired.
7988 		 */
7989 		goto skip_time_check;
7990 	}
7991 	if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
7992 		uint32_t left;
7993 
7994 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
7995 			ret = -1;
7996 			rack_log_to_processing(rack, cts, ret, 0);
7997 			return (0);
7998 		}
7999 		if (hpts_calling == 0) {
8000 			/*
8001 			 * A user send or queued mbuf (sack) has called us? We
8002 			 * return 0 and let the pacing guards
8003 			 * deal with it if they should or
8004 			 * should not cause a send.
8005 			 */
8006 			ret = -2;
8007 			rack_log_to_processing(rack, cts, ret, 0);
8008 			return (0);
8009 		}
8010 		/*
8011 		 * Ok our timer went off early and we are not paced false
8012 		 * alarm, go back to sleep. We make sure we don't have
8013 		 * no-sack wakeup on since we no longer have a PKT_OUTPUT
8014 		 * flag in place.
8015 		 */
8016 		rack->rc_tp->t_flags2 &= ~TF2_DONT_SACK_QUEUE;
8017 		ret = -3;
8018 		left = rack->r_ctl.rc_timer_exp - cts;
8019 		tcp_hpts_insert(tp, HPTS_MS_TO_SLOTS(left));
8020 		rack_log_to_processing(rack, cts, ret, left);
8021 		return (1);
8022 	}
8023 skip_time_check:
8024 	rack->rc_tmr_stopped = 0;
8025 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK;
8026 	if (timers & PACE_TMR_DELACK) {
8027 		ret = rack_timeout_delack(tp, rack, cts);
8028 	} else if (timers & PACE_TMR_RACK) {
8029 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
8030 		rack->r_fast_output = 0;
8031 		ret = rack_timeout_rack(tp, rack, cts);
8032 	} else if (timers & PACE_TMR_TLP) {
8033 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
8034 		ret = rack_timeout_tlp(tp, rack, cts, doing_tlp);
8035 	} else if (timers & PACE_TMR_RXT) {
8036 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
8037 		rack->r_fast_output = 0;
8038 		ret = rack_timeout_rxt(tp, rack, cts);
8039 	} else if (timers & PACE_TMR_PERSIT) {
8040 		ret = rack_timeout_persist(tp, rack, cts);
8041 	} else if (timers & PACE_TMR_KEEP) {
8042 		ret = rack_timeout_keepalive(tp, rack, cts);
8043 	}
8044 	rack_log_to_processing(rack, cts, ret, timers);
8045 	return (ret);
8046 }
8047 
8048 static void
8049 rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line)
8050 {
8051 	struct timeval tv;
8052 	uint32_t us_cts, flags_on_entry;
8053 	uint8_t hpts_removed = 0;
8054 
8055 	flags_on_entry = rack->r_ctl.rc_hpts_flags;
8056 	us_cts = tcp_get_usecs(&tv);
8057 	if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
8058 	    ((TSTMP_GEQ(us_cts, rack->r_ctl.rc_last_output_to)) ||
8059 	     ((tp->snd_max - tp->snd_una) == 0))) {
8060 		tcp_hpts_remove(rack->rc_tp);
8061 		hpts_removed = 1;
8062 		/* If we were not delayed cancel out the flag. */
8063 		if ((tp->snd_max - tp->snd_una) == 0)
8064 			rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
8065 		rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry);
8066 	}
8067 	if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
8068 		rack->rc_tmr_stopped = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
8069 		if (tcp_in_hpts(rack->rc_tp) &&
8070 		    ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)) {
8071 			/*
8072 			 * Canceling timer's when we have no output being
8073 			 * paced. We also must remove ourselves from the
8074 			 * hpts.
8075 			 */
8076 			tcp_hpts_remove(rack->rc_tp);
8077 			hpts_removed = 1;
8078 		}
8079 		rack->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK);
8080 	}
8081 	if (hpts_removed == 0)
8082 		rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry);
8083 }
8084 
8085 static int
8086 rack_stopall(struct tcpcb *tp)
8087 {
8088 	struct tcp_rack *rack;
8089 
8090 	rack = (struct tcp_rack *)tp->t_fb_ptr;
8091 	rack->t_timers_stopped = 1;
8092 
8093 	tcp_hpts_remove(tp);
8094 
8095 	return (0);
8096 }
8097 
8098 static void
8099 rack_stop_all_timers(struct tcpcb *tp, struct tcp_rack *rack)
8100 {
8101 	/*
8102 	 * Assure no timers are running.
8103 	 */
8104 	if (tcp_timer_active(tp, TT_PERSIST)) {
8105 		/* We enter in persists, set the flag appropriately */
8106 		rack->rc_in_persist = 1;
8107 	}
8108 	if (tcp_in_hpts(rack->rc_tp)) {
8109 		tcp_hpts_remove(rack->rc_tp);
8110 	}
8111 }
8112 
8113 static void
8114 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
8115     struct rack_sendmap *rsm, uint64_t ts, uint32_t add_flag, int segsiz)
8116 {
8117 	int32_t idx;
8118 
8119 	rsm->r_rtr_cnt++;
8120 	if (rsm->r_rtr_cnt > RACK_NUM_OF_RETRANS) {
8121 		rsm->r_rtr_cnt = RACK_NUM_OF_RETRANS;
8122 		rsm->r_flags |= RACK_OVERMAX;
8123 	}
8124 	rsm->r_act_rxt_cnt++;
8125 	/* Peg the count/index */
8126 	rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
8127 	rsm->r_dupack = 0;
8128 	if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & RACK_TLP) == 0)) {
8129 		rack->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start);
8130 		rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start);
8131 	}
8132 	if (rsm->r_flags & RACK_WAS_LOST) {
8133 		/*
8134 		 * We retransmitted it putting it back in flight
8135 		 * remove the lost desgination and reduce the
8136 		 * bytes considered lost.
8137 		 */
8138 		rsm->r_flags  &= ~RACK_WAS_LOST;
8139 		KASSERT((rack->r_ctl.rc_considered_lost >= (rsm->r_end - rsm->r_start)),
8140 			("rsm:%p rack:%p rc_considered_lost goes negative", rsm,  rack));
8141 		if (rack->r_ctl.rc_considered_lost >= (rsm->r_end - rsm->r_start))
8142 			rack->r_ctl.rc_considered_lost -= rsm->r_end - rsm->r_start;
8143 		else
8144 			rack->r_ctl.rc_considered_lost = 0;
8145 	}
8146 	idx = rsm->r_rtr_cnt - 1;
8147 	rsm->r_tim_lastsent[idx] = ts;
8148 	/*
8149 	 * Here we don't add in the len of send, since its already
8150 	 * in snduna <->snd_max.
8151 	 */
8152 	rsm->r_fas = ctf_flight_size(rack->rc_tp,
8153 				     rack->r_ctl.rc_sacked);
8154 	if (rsm->r_flags & RACK_ACKED) {
8155 		/* Problably MTU discovery messing with us */
8156 		rsm->r_flags &= ~RACK_ACKED;
8157 		rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
8158 	}
8159 	if (rsm->r_in_tmap) {
8160 		TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8161 		rsm->r_in_tmap = 0;
8162 	}
8163 	/* Lets make sure it really is in or not the GP window */
8164 	rack_mark_in_gp_win(tp, rsm);
8165 	TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8166 	rsm->r_in_tmap = 1;
8167 	rsm->r_bas = (uint8_t)(((rsm->r_end - rsm->r_start) + segsiz - 1) / segsiz);
8168 	/* Take off the must retransmit flag, if its on */
8169 	if (rsm->r_flags & RACK_MUST_RXT) {
8170 		if (rack->r_must_retran)
8171 			rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start);
8172 		if (SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) {
8173 			/*
8174 			 * We have retransmitted all we need. Clear
8175 			 * any must retransmit flags.
8176 			 */
8177 			rack->r_must_retran = 0;
8178 			rack->r_ctl.rc_out_at_rto = 0;
8179 		}
8180 		rsm->r_flags &= ~RACK_MUST_RXT;
8181 	}
8182 	/* Remove any collapsed flag */
8183 	rsm->r_flags &= ~RACK_RWND_COLLAPSED;
8184 	if (rsm->r_flags & RACK_SACK_PASSED) {
8185 		/* We have retransmitted due to the SACK pass */
8186 		rsm->r_flags &= ~RACK_SACK_PASSED;
8187 		rsm->r_flags |= RACK_WAS_SACKPASS;
8188 	}
8189 }
8190 
8191 static uint32_t
8192 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack,
8193     struct rack_sendmap *rsm, uint64_t ts, int32_t *lenp, uint32_t add_flag, int segsiz)
8194 {
8195 	/*
8196 	 * We (re-)transmitted starting at rsm->r_start for some length
8197 	 * (possibly less than r_end.
8198 	 */
8199 	struct rack_sendmap *nrsm;
8200 	int insret __diagused;
8201 	uint32_t c_end;
8202 	int32_t len;
8203 
8204 	len = *lenp;
8205 	c_end = rsm->r_start + len;
8206 	if (SEQ_GEQ(c_end, rsm->r_end)) {
8207 		/*
8208 		 * We retransmitted the whole piece or more than the whole
8209 		 * slopping into the next rsm.
8210 		 */
8211 		rack_update_rsm(tp, rack, rsm, ts, add_flag, segsiz);
8212 		if (c_end == rsm->r_end) {
8213 			*lenp = 0;
8214 			return (0);
8215 		} else {
8216 			int32_t act_len;
8217 
8218 			/* Hangs over the end return whats left */
8219 			act_len = rsm->r_end - rsm->r_start;
8220 			*lenp = (len - act_len);
8221 			return (rsm->r_end);
8222 		}
8223 		/* We don't get out of this block. */
8224 	}
8225 	/*
8226 	 * Here we retransmitted less than the whole thing which means we
8227 	 * have to split this into what was transmitted and what was not.
8228 	 */
8229 	nrsm = rack_alloc_full_limit(rack);
8230 	if (nrsm == NULL) {
8231 		/*
8232 		 * We can't get memory, so lets not proceed.
8233 		 */
8234 		*lenp = 0;
8235 		return (0);
8236 	}
8237 	/*
8238 	 * So here we are going to take the original rsm and make it what we
8239 	 * retransmitted. nrsm will be the tail portion we did not
8240 	 * retransmit. For example say the chunk was 1, 11 (10 bytes). And
8241 	 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to
8242 	 * 1, 6 and the new piece will be 6, 11.
8243 	 */
8244 	rack_clone_rsm(rack, nrsm, rsm, c_end);
8245 	nrsm->r_dupack = 0;
8246 	rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2);
8247 #ifndef INVARIANTS
8248 	(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
8249 #else
8250 	if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
8251 		panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p",
8252 		      nrsm, insret, rack, rsm);
8253 	}
8254 #endif
8255 	if (rsm->r_in_tmap) {
8256 		TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8257 		nrsm->r_in_tmap = 1;
8258 	}
8259 	rsm->r_flags &= (~RACK_HAS_FIN);
8260 	rack_update_rsm(tp, rack, rsm, ts, add_flag, segsiz);
8261 	/* Log a split of rsm into rsm and nrsm */
8262 	rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
8263 	*lenp = 0;
8264 	return (0);
8265 }
8266 
8267 static void
8268 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
8269 		uint32_t seq_out, uint16_t th_flags, int32_t err, uint64_t cts,
8270 		struct rack_sendmap *hintrsm, uint32_t add_flag, struct mbuf *s_mb,
8271 		uint32_t s_moff, int hw_tls, int segsiz)
8272 {
8273 	struct tcp_rack *rack;
8274 	struct rack_sendmap *rsm, *nrsm;
8275 	int insret __diagused;
8276 
8277 	register uint32_t snd_max, snd_una;
8278 
8279 	/*
8280 	 * Add to the RACK log of packets in flight or retransmitted. If
8281 	 * there is a TS option we will use the TS echoed, if not we will
8282 	 * grab a TS.
8283 	 *
8284 	 * Retransmissions will increment the count and move the ts to its
8285 	 * proper place. Note that if options do not include TS's then we
8286 	 * won't be able to effectively use the ACK for an RTT on a retran.
8287 	 *
8288 	 * Notes about r_start and r_end. Lets consider a send starting at
8289 	 * sequence 1 for 10 bytes. In such an example the r_start would be
8290 	 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11.
8291 	 * This means that r_end is actually the first sequence for the next
8292 	 * slot (11).
8293 	 *
8294 	 */
8295 	/*
8296 	 * If err is set what do we do XXXrrs? should we not add the thing?
8297 	 * -- i.e. return if err != 0 or should we pretend we sent it? --
8298 	 * i.e. proceed with add ** do this for now.
8299 	 */
8300 	INP_WLOCK_ASSERT(tptoinpcb(tp));
8301 	if (err)
8302 		/*
8303 		 * We don't log errors -- we could but snd_max does not
8304 		 * advance in this case either.
8305 		 */
8306 		return;
8307 
8308 	if (th_flags & TH_RST) {
8309 		/*
8310 		 * We don't log resets and we return immediately from
8311 		 * sending
8312 		 */
8313 		return;
8314 	}
8315 	rack = (struct tcp_rack *)tp->t_fb_ptr;
8316 	snd_una = tp->snd_una;
8317 	snd_max = tp->snd_max;
8318 	if (th_flags & (TH_SYN | TH_FIN)) {
8319 		/*
8320 		 * The call to rack_log_output is made before bumping
8321 		 * snd_max. This means we can record one extra byte on a SYN
8322 		 * or FIN if seq_out is adding more on and a FIN is present
8323 		 * (and we are not resending).
8324 		 */
8325 		if ((th_flags & TH_SYN) && (seq_out == tp->iss))
8326 			len++;
8327 		if (th_flags & TH_FIN)
8328 			len++;
8329 	}
8330 	if (SEQ_LEQ((seq_out + len), snd_una)) {
8331 		/* Are sending an old segment to induce an ack (keep-alive)? */
8332 		return;
8333 	}
8334 	if (SEQ_LT(seq_out, snd_una)) {
8335 		/* huh? should we panic? */
8336 		uint32_t end;
8337 
8338 		end = seq_out + len;
8339 		seq_out = snd_una;
8340 		if (SEQ_GEQ(end, seq_out))
8341 			len = end - seq_out;
8342 		else
8343 			len = 0;
8344 	}
8345 	if (len == 0) {
8346 		/* We don't log zero window probes */
8347 		return;
8348 	}
8349 	if (IN_FASTRECOVERY(tp->t_flags)) {
8350 		rack->r_ctl.rc_prr_out += len;
8351 	}
8352 	/* First question is it a retransmission or new? */
8353 	if (seq_out == snd_max) {
8354 		/* Its new */
8355 		rack_chk_req_and_hybrid_on_out(rack, seq_out, len, cts);
8356 again:
8357 		rsm = rack_alloc(rack);
8358 		if (rsm == NULL) {
8359 			/*
8360 			 * Hmm out of memory and the tcb got destroyed while
8361 			 * we tried to wait.
8362 			 */
8363 			return;
8364 		}
8365 		if (th_flags & TH_FIN) {
8366 			rsm->r_flags = RACK_HAS_FIN|add_flag;
8367 		} else {
8368 			rsm->r_flags = add_flag;
8369 		}
8370 		if (hw_tls)
8371 			rsm->r_hw_tls = 1;
8372 		rsm->r_tim_lastsent[0] = cts;
8373 		rsm->r_rtr_cnt = 1;
8374  		rsm->r_act_rxt_cnt = 0;
8375 		rsm->r_rtr_bytes = 0;
8376 		if (th_flags & TH_SYN) {
8377 			/* The data space is one beyond snd_una */
8378 			rsm->r_flags |= RACK_HAS_SYN;
8379 		}
8380 		rsm->r_start = seq_out;
8381 		rsm->r_end = rsm->r_start + len;
8382 		rack_mark_in_gp_win(tp, rsm);
8383 		rsm->r_dupack = 0;
8384 		/*
8385 		 * save off the mbuf location that
8386 		 * sndmbuf_noadv returned (which is
8387 		 * where we started copying from)..
8388 		 */
8389 		rsm->m = s_mb;
8390 		rsm->soff = s_moff;
8391 		/*
8392 		 * Here we do add in the len of send, since its not yet
8393 		 * reflected in in snduna <->snd_max
8394 		 */
8395 		rsm->r_fas = (ctf_flight_size(rack->rc_tp,
8396 					      rack->r_ctl.rc_sacked) +
8397 			      (rsm->r_end - rsm->r_start));
8398 		if ((rack->rc_initial_ss_comp == 0) &&
8399 		    (rack->r_ctl.ss_hi_fs < rsm->r_fas)) {
8400 			   rack->r_ctl.ss_hi_fs = rsm->r_fas;
8401 		}
8402 		/* rsm->m will be NULL if RACK_HAS_SYN or RACK_HAS_FIN is set */
8403 		if (rsm->m) {
8404 			if (rsm->m->m_len <= rsm->soff) {
8405 				/*
8406 				 * XXXrrs Question, will this happen?
8407 				 *
8408 				 * If sbsndptr is set at the correct place
8409 				 * then s_moff should always be somewhere
8410 				 * within rsm->m. But if the sbsndptr was
8411 				 * off then that won't be true. If it occurs
8412 				 * we need to walkout to the correct location.
8413 				 */
8414 				struct mbuf *lm;
8415 
8416 				lm = rsm->m;
8417 				while (lm->m_len <= rsm->soff) {
8418 					rsm->soff -= lm->m_len;
8419 					lm = lm->m_next;
8420 					KASSERT(lm != NULL, ("%s rack:%p lm goes null orig_off:%u origmb:%p rsm->soff:%u",
8421 							     __func__, rack, s_moff, s_mb, rsm->soff));
8422 				}
8423 				rsm->m = lm;
8424 			}
8425 			rsm->orig_m_len = rsm->m->m_len;
8426 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
8427 		} else {
8428 			rsm->orig_m_len = 0;
8429 			rsm->orig_t_space = 0;
8430 		}
8431 		rsm->r_bas = (uint8_t)((len + segsiz - 1) / segsiz);
8432 		rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
8433 		/* Log a new rsm */
8434 		rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_NEW, 0, __LINE__);
8435 #ifndef INVARIANTS
8436 		(void)tqhash_insert(rack->r_ctl.tqh, rsm);
8437 #else
8438 		if ((insret = tqhash_insert(rack->r_ctl.tqh, rsm)) != 0) {
8439 			panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p",
8440 			      nrsm, insret, rack, rsm);
8441 		}
8442 #endif
8443 		TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8444 		rsm->r_in_tmap = 1;
8445 		if (rsm->r_flags & RACK_IS_PCM) {
8446 			rack->r_ctl.pcm_i.send_time = cts;
8447 			rack->r_ctl.pcm_i.eseq = rsm->r_end;
8448 			/* First time through we set the start too */
8449 			if (rack->pcm_in_progress == 0)
8450 				rack->r_ctl.pcm_i.sseq = rsm->r_start;
8451 		}
8452 		/*
8453 		 * Special case detection, is there just a single
8454 		 * packet outstanding when we are not in recovery?
8455 		 *
8456 		 * If this is true mark it so.
8457 		 */
8458 		if ((IN_FASTRECOVERY(tp->t_flags) == 0) &&
8459 		    (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) == ctf_fixed_maxseg(tp))) {
8460 			struct rack_sendmap *prsm;
8461 
8462 			prsm = tqhash_prev(rack->r_ctl.tqh, rsm);
8463 			if (prsm)
8464 				prsm->r_one_out_nr = 1;
8465 		}
8466 		return;
8467 	}
8468 	/*
8469 	 * If we reach here its a retransmission and we need to find it.
8470 	 */
8471 more:
8472 	if (hintrsm && (hintrsm->r_start == seq_out)) {
8473 		rsm = hintrsm;
8474 		hintrsm = NULL;
8475 	} else {
8476 		/* No hints sorry */
8477 		rsm = NULL;
8478 	}
8479 	if ((rsm) && (rsm->r_start == seq_out)) {
8480 		seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag, segsiz);
8481 		if (len == 0) {
8482 			return;
8483 		} else {
8484 			goto more;
8485 		}
8486 	}
8487 	/* Ok it was not the last pointer go through it the hard way. */
8488 refind:
8489 	rsm = tqhash_find(rack->r_ctl.tqh, seq_out);
8490 	if (rsm) {
8491 		if (rsm->r_start == seq_out) {
8492 			seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag, segsiz);
8493 			if (len == 0) {
8494 				return;
8495 			} else {
8496 				goto refind;
8497 			}
8498 		}
8499 		if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) {
8500 			/* Transmitted within this piece */
8501 			/*
8502 			 * Ok we must split off the front and then let the
8503 			 * update do the rest
8504 			 */
8505 			nrsm = rack_alloc_full_limit(rack);
8506 			if (nrsm == NULL) {
8507 				rack_update_rsm(tp, rack, rsm, cts, add_flag, segsiz);
8508 				return;
8509 			}
8510 			/*
8511 			 * copy rsm to nrsm and then trim the front of rsm
8512 			 * to not include this part.
8513 			 */
8514 			rack_clone_rsm(rack, nrsm, rsm, seq_out);
8515 			rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
8516 #ifndef INVARIANTS
8517 			(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
8518 #else
8519 			if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
8520 				panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p",
8521 				      nrsm, insret, rack, rsm);
8522 			}
8523 #endif
8524 			if (rsm->r_in_tmap) {
8525 				TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8526 				nrsm->r_in_tmap = 1;
8527 			}
8528 			rsm->r_flags &= (~RACK_HAS_FIN);
8529 			seq_out = rack_update_entry(tp, rack, nrsm, cts, &len, add_flag, segsiz);
8530 			if (len == 0) {
8531 				return;
8532 			} else if (len > 0)
8533 				goto refind;
8534 		}
8535 	}
8536 	/*
8537 	 * Hmm not found in map did they retransmit both old and on into the
8538 	 * new?
8539 	 */
8540 	if (seq_out == tp->snd_max) {
8541 		goto again;
8542 	} else if (SEQ_LT(seq_out, tp->snd_max)) {
8543 #ifdef INVARIANTS
8544 		printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n",
8545 		       seq_out, len, tp->snd_una, tp->snd_max);
8546 		printf("Starting Dump of all rack entries\n");
8547 		TQHASH_FOREACH(rsm, rack->r_ctl.tqh)  {
8548 			printf("rsm:%p start:%u end:%u\n",
8549 			       rsm, rsm->r_start, rsm->r_end);
8550 		}
8551 		printf("Dump complete\n");
8552 		panic("seq_out not found rack:%p tp:%p",
8553 		      rack, tp);
8554 #endif
8555 	} else {
8556 #ifdef INVARIANTS
8557 		/*
8558 		 * Hmm beyond sndmax? (only if we are using the new rtt-pack
8559 		 * flag)
8560 		 */
8561 		panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p",
8562 		      seq_out, len, tp->snd_max, tp);
8563 #endif
8564 	}
8565 }
8566 
8567 /*
8568  * Record one of the RTT updates from an ack into
8569  * our sample structure.
8570  */
8571 
8572 static void
8573 tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt, uint32_t len, uint32_t us_rtt,
8574 		    int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt)
8575 {
8576 	if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
8577 	    (rack->r_ctl.rack_rs.rs_rtt_lowest > rtt)) {
8578 		rack->r_ctl.rack_rs.rs_rtt_lowest = rtt;
8579 	}
8580 	if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
8581 	    (rack->r_ctl.rack_rs.rs_rtt_highest < rtt)) {
8582 		rack->r_ctl.rack_rs.rs_rtt_highest = rtt;
8583 	}
8584 	if (rack->rc_tp->t_flags & TF_GPUTINPROG) {
8585 	    if (us_rtt < rack->r_ctl.rc_gp_lowrtt)
8586 		rack->r_ctl.rc_gp_lowrtt = us_rtt;
8587 	    if (rack->rc_tp->snd_wnd > rack->r_ctl.rc_gp_high_rwnd)
8588 		    rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
8589 	}
8590 	if ((confidence == 1) &&
8591 	    ((rsm == NULL) ||
8592 	     (rsm->r_just_ret) ||
8593 	     (rsm->r_one_out_nr &&
8594 	      len < (ctf_fixed_maxseg(rack->rc_tp) * 2)))) {
8595 		/*
8596 		 * If the rsm had a just return
8597 		 * hit it then we can't trust the
8598 		 * rtt measurement for buffer deterimination
8599 		 * Note that a confidence of 2, indicates
8600 		 * SACK'd which overrides the r_just_ret or
8601 		 * the r_one_out_nr. If it was a CUM-ACK and
8602 		 * we had only two outstanding, but get an
8603 		 * ack for only 1. Then that also lowers our
8604 		 * confidence.
8605 		 */
8606 		confidence = 0;
8607 	}
8608 	if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
8609 	    (rack->r_ctl.rack_rs.rs_us_rtt > us_rtt)) {
8610 		if (rack->r_ctl.rack_rs.confidence == 0) {
8611 			/*
8612 			 * We take anything with no current confidence
8613 			 * saved.
8614 			 */
8615 			rack->r_ctl.rack_rs.rs_us_rtt = us_rtt;
8616 			rack->r_ctl.rack_rs.confidence = confidence;
8617 			rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt;
8618 		} else if (confidence != 0) {
8619 			/*
8620 			 * Once we have a confident number,
8621 			 * we can update it with a smaller
8622 			 * value since this confident number
8623 			 * may include the DSACK time until
8624 			 * the next segment (the second one) arrived.
8625 			 */
8626 			rack->r_ctl.rack_rs.rs_us_rtt = us_rtt;
8627 			rack->r_ctl.rack_rs.confidence = confidence;
8628 			rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt;
8629 		}
8630 	}
8631 	rack_log_rtt_upd(rack->rc_tp, rack, us_rtt, len, rsm, confidence);
8632 	rack->r_ctl.rack_rs.rs_flags = RACK_RTT_VALID;
8633 	rack->r_ctl.rack_rs.rs_rtt_tot += rtt;
8634 	rack->r_ctl.rack_rs.rs_rtt_cnt++;
8635 }
8636 
8637 /*
8638  * Collect new round-trip time estimate
8639  * and update averages and current timeout.
8640  */
8641 static void
8642 tcp_rack_xmit_timer_commit(struct tcp_rack *rack, struct tcpcb *tp)
8643 {
8644 	int32_t delta;
8645 	int32_t rtt;
8646 
8647 	if (rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY)
8648 		/* No valid sample */
8649 		return;
8650 	if (rack->r_ctl.rc_rate_sample_method == USE_RTT_LOW) {
8651 		/* We are to use the lowest RTT seen in a single ack */
8652 		rtt = rack->r_ctl.rack_rs.rs_rtt_lowest;
8653 	} else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_HIGH) {
8654 		/* We are to use the highest RTT seen in a single ack */
8655 		rtt = rack->r_ctl.rack_rs.rs_rtt_highest;
8656 	} else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_AVG) {
8657 		/* We are to use the average RTT seen in a single ack */
8658 		rtt = (int32_t)(rack->r_ctl.rack_rs.rs_rtt_tot /
8659 				(uint64_t)rack->r_ctl.rack_rs.rs_rtt_cnt);
8660 	} else {
8661 #ifdef INVARIANTS
8662 		panic("Unknown rtt variant %d", rack->r_ctl.rc_rate_sample_method);
8663 #endif
8664 		return;
8665 	}
8666 	if (rtt == 0)
8667 		rtt = 1;
8668 	if (rack->rc_gp_rtt_set == 0) {
8669 		/*
8670 		 * With no RTT we have to accept
8671 		 * even one we are not confident of.
8672 		 */
8673 		rack->r_ctl.rc_gp_srtt = rack->r_ctl.rack_rs.rs_us_rtt;
8674 		rack->rc_gp_rtt_set = 1;
8675 	} else if (rack->r_ctl.rack_rs.confidence) {
8676 		/* update the running gp srtt */
8677 		rack->r_ctl.rc_gp_srtt -= (rack->r_ctl.rc_gp_srtt/8);
8678 		rack->r_ctl.rc_gp_srtt += rack->r_ctl.rack_rs.rs_us_rtt / 8;
8679 	}
8680 	if (rack->r_ctl.rack_rs.confidence) {
8681 		/*
8682 		 * record the low and high for highly buffered path computation,
8683 		 * we only do this if we are confident (not a retransmission).
8684 		 */
8685 		if (rack->r_ctl.rc_highest_us_rtt < rack->r_ctl.rack_rs.rs_us_rtt) {
8686 			rack->r_ctl.rc_highest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
8687 		}
8688 		if (rack->rc_highly_buffered == 0) {
8689 			/*
8690 			 * Currently once we declare a path has
8691 			 * highly buffered there is no going
8692 			 * back, which may be a problem...
8693 			 */
8694 			if ((rack->r_ctl.rc_highest_us_rtt / rack->r_ctl.rc_lowest_us_rtt) > rack_hbp_thresh) {
8695 				rack_log_rtt_shrinks(rack, rack->r_ctl.rack_rs.rs_us_rtt,
8696 						     rack->r_ctl.rc_highest_us_rtt,
8697 						     rack->r_ctl.rc_lowest_us_rtt,
8698 						     RACK_RTTS_SEEHBP);
8699 				rack->rc_highly_buffered = 1;
8700 			}
8701 		}
8702 	}
8703 	if ((rack->r_ctl.rack_rs.confidence) ||
8704 	    (rack->r_ctl.rack_rs.rs_us_rtrcnt == 1)) {
8705 		/*
8706 		 * If we are highly confident of it <or> it was
8707 		 * never retransmitted we accept it as the last us_rtt.
8708 		 */
8709 		rack->r_ctl.rc_last_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
8710 		/* The lowest rtt can be set if its was not retransmited */
8711 		if (rack->r_ctl.rc_lowest_us_rtt > rack->r_ctl.rack_rs.rs_us_rtt) {
8712 			rack->r_ctl.rc_lowest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
8713 			if (rack->r_ctl.rc_lowest_us_rtt == 0)
8714 				rack->r_ctl.rc_lowest_us_rtt = 1;
8715 		}
8716 	}
8717 	rack = (struct tcp_rack *)tp->t_fb_ptr;
8718 	if (tp->t_srtt != 0) {
8719 		/*
8720 		 * We keep a simple srtt in microseconds, like our rtt
8721 		 * measurement. We don't need to do any tricks with shifting
8722 		 * etc. Instead we just add in 1/8th of the new measurement
8723 		 * and subtract out 1/8 of the old srtt. We do the same with
8724 		 * the variance after finding the absolute value of the
8725 		 * difference between this sample and the current srtt.
8726 		 */
8727 		delta = tp->t_srtt - rtt;
8728 		/* Take off 1/8th of the current sRTT */
8729 		tp->t_srtt -= (tp->t_srtt >> 3);
8730 		/* Add in 1/8th of the new RTT just measured */
8731 		tp->t_srtt += (rtt >> 3);
8732 		if (tp->t_srtt <= 0)
8733 			tp->t_srtt = 1;
8734 		/* Now lets make the absolute value of the variance */
8735 		if (delta < 0)
8736 			delta = -delta;
8737 		/* Subtract out 1/8th */
8738 		tp->t_rttvar -= (tp->t_rttvar >> 3);
8739 		/* Add in 1/8th of the new variance we just saw */
8740 		tp->t_rttvar += (delta >> 3);
8741 		if (tp->t_rttvar <= 0)
8742 			tp->t_rttvar = 1;
8743 	} else {
8744 		/*
8745 		 * No rtt measurement yet - use the unsmoothed rtt. Set the
8746 		 * variance to half the rtt (so our first retransmit happens
8747 		 * at 3*rtt).
8748 		 */
8749 		tp->t_srtt = rtt;
8750 		tp->t_rttvar = rtt >> 1;
8751 	}
8752 	rack->rc_srtt_measure_made = 1;
8753 	KMOD_TCPSTAT_INC(tcps_rttupdated);
8754 	if (tp->t_rttupdated < UCHAR_MAX)
8755 		tp->t_rttupdated++;
8756 #ifdef STATS
8757 	if (rack_stats_gets_ms_rtt == 0) {
8758 		/* Send in the microsecond rtt used for rxt timeout purposes */
8759 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt));
8760 	} else if (rack_stats_gets_ms_rtt == 1) {
8761 		/* Send in the millisecond rtt used for rxt timeout purposes */
8762 		int32_t ms_rtt;
8763 
8764 		/* Round up */
8765 		ms_rtt = (rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC;
8766 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt));
8767 	} else if (rack_stats_gets_ms_rtt == 2) {
8768 		/* Send in the millisecond rtt has close to the path RTT as we can get  */
8769 		int32_t ms_rtt;
8770 
8771 		/* Round up */
8772 		ms_rtt = (rack->r_ctl.rack_rs.rs_us_rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC;
8773 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt));
8774 	}  else {
8775 		/* Send in the microsecond rtt has close to the path RTT as we can get  */
8776 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rack->r_ctl.rack_rs.rs_us_rtt));
8777 	}
8778 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_PATHRTT, imax(0, rack->r_ctl.rack_rs.rs_us_rtt));
8779 #endif
8780 	rack->r_ctl.last_rcv_tstmp_for_rtt = tcp_tv_to_mssectick(&rack->r_ctl.act_rcv_time);
8781 	/*
8782 	 * the retransmit should happen at rtt + 4 * rttvar. Because of the
8783 	 * way we do the smoothing, srtt and rttvar will each average +1/2
8784 	 * tick of bias.  When we compute the retransmit timer, we want 1/2
8785 	 * tick of rounding and 1 extra tick because of +-1/2 tick
8786 	 * uncertainty in the firing of the timer.  The bias will give us
8787 	 * exactly the 1.5 tick we need.  But, because the bias is
8788 	 * statistical, we have to test that we don't drop below the minimum
8789 	 * feasible timer (which is 2 ticks).
8790 	 */
8791 	tp->t_rxtshift = 0;
8792 	RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
8793 		      max(rack_rto_min, rtt + 2), rack_rto_max, rack->r_ctl.timer_slop);
8794 	rack_log_rtt_sample(rack, rtt);
8795 	tp->t_softerror = 0;
8796 }
8797 
8798 
8799 static void
8800 rack_apply_updated_usrtt(struct tcp_rack *rack, uint32_t us_rtt, uint32_t us_cts)
8801 {
8802 	/*
8803 	 * Apply to filter the inbound us-rtt at us_cts.
8804 	 */
8805 	uint32_t old_rtt;
8806 
8807 	old_rtt = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
8808 	apply_filter_min_small(&rack->r_ctl.rc_gp_min_rtt,
8809 			       us_rtt, us_cts);
8810 	if (old_rtt > us_rtt) {
8811 		/* We just hit a new lower rtt time */
8812 		rack_log_rtt_shrinks(rack,  us_cts,  old_rtt,
8813 				     __LINE__, RACK_RTTS_NEWRTT);
8814 		/*
8815 		 * Only count it if its lower than what we saw within our
8816 		 * calculated range.
8817 		 */
8818 		if ((old_rtt - us_rtt) > rack_min_rtt_movement) {
8819 			if (rack_probertt_lower_within &&
8820 			    rack->rc_gp_dyn_mul &&
8821 			    (rack->use_fixed_rate == 0) &&
8822 			    (rack->rc_always_pace)) {
8823 				/*
8824 				 * We are seeing a new lower rtt very close
8825 				 * to the time that we would have entered probe-rtt.
8826 				 * This is probably due to the fact that a peer flow
8827 				 * has entered probe-rtt. Lets go in now too.
8828 				 */
8829 				uint32_t val;
8830 
8831 				val = rack_probertt_lower_within * rack_time_between_probertt;
8832 				val /= 100;
8833 				if ((rack->in_probe_rtt == 0)  &&
8834 				    (rack->rc_skip_timely == 0) &&
8835 				    ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= (rack_time_between_probertt - val)))	{
8836 					rack_enter_probertt(rack, us_cts);
8837 				}
8838 			}
8839 			rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
8840 		}
8841 	}
8842 }
8843 
8844 static int
8845 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack,
8846     struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack)
8847 {
8848 	uint32_t us_rtt;
8849 	int32_t i, all;
8850 	uint32_t t, len_acked;
8851 
8852 	if ((rsm->r_flags & RACK_ACKED) ||
8853 	    (rsm->r_flags & RACK_WAS_ACKED))
8854 		/* Already done */
8855 		return (0);
8856 	if (rsm->r_no_rtt_allowed) {
8857 		/* Not allowed */
8858 		return (0);
8859 	}
8860 	if (ack_type == CUM_ACKED) {
8861 		if (SEQ_GT(th_ack, rsm->r_end)) {
8862 			len_acked = rsm->r_end - rsm->r_start;
8863 			all = 1;
8864 		} else {
8865 			len_acked = th_ack - rsm->r_start;
8866 			all = 0;
8867 		}
8868 	} else {
8869 		len_acked = rsm->r_end - rsm->r_start;
8870 		all = 0;
8871 	}
8872 	if (rsm->r_rtr_cnt == 1) {
8873 
8874 		t = cts - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
8875 		if ((int)t <= 0)
8876 			t = 1;
8877 		if (!tp->t_rttlow || tp->t_rttlow > t)
8878 			tp->t_rttlow = t;
8879 		if (!rack->r_ctl.rc_rack_min_rtt ||
8880 		    SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
8881 			rack->r_ctl.rc_rack_min_rtt = t;
8882 			if (rack->r_ctl.rc_rack_min_rtt == 0) {
8883 				rack->r_ctl.rc_rack_min_rtt = 1;
8884 			}
8885 		}
8886 		if (TSTMP_GT(tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time), rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]))
8887 			us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8888 		else
8889 			us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8890 		if (us_rtt == 0)
8891 			us_rtt = 1;
8892 		if (CC_ALGO(tp)->rttsample != NULL) {
8893 			/* Kick the RTT to the CC */
8894 			CC_ALGO(tp)->rttsample(&tp->t_ccv, us_rtt, 1, rsm->r_fas);
8895 		}
8896 		rack_apply_updated_usrtt(rack, us_rtt, tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time));
8897 		if (ack_type == SACKED) {
8898 			rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 1);
8899 			tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt, 2 , rsm, rsm->r_rtr_cnt);
8900 		} else {
8901 			/*
8902 			 * We need to setup what our confidence
8903 			 * is in this ack.
8904 			 *
8905 			 * If the rsm was app limited and it is
8906 			 * less than a mss in length (the end
8907 			 * of the send) then we have a gap. If we
8908 			 * were app limited but say we were sending
8909 			 * multiple MSS's then we are more confident
8910 			 * int it.
8911 			 *
8912 			 * When we are not app-limited then we see if
8913 			 * the rsm is being included in the current
8914 			 * measurement, we tell this by the app_limited_needs_set
8915 			 * flag.
8916 			 *
8917 			 * Note that being cwnd blocked is not applimited
8918 			 * as well as the pacing delay between packets which
8919 			 * are sending only 1 or 2 MSS's also will show up
8920 			 * in the RTT. We probably need to examine this algorithm
8921 			 * a bit more and enhance it to account for the delay
8922 			 * between rsm's. We could do that by saving off the
8923 			 * pacing delay of each rsm (in an rsm) and then
8924 			 * factoring that in somehow though for now I am
8925 			 * not sure how :)
8926 			 */
8927 			int calc_conf = 0;
8928 
8929 			if (rsm->r_flags & RACK_APP_LIMITED) {
8930 				if (all && (len_acked <= ctf_fixed_maxseg(tp)))
8931 					calc_conf = 0;
8932 				else
8933 					calc_conf = 1;
8934 			} else if (rack->app_limited_needs_set == 0) {
8935 				calc_conf = 1;
8936 			} else {
8937 				calc_conf = 0;
8938 			}
8939 			rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 2);
8940 			tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt,
8941 					    calc_conf, rsm, rsm->r_rtr_cnt);
8942 		}
8943 		if ((rsm->r_flags & RACK_TLP) &&
8944 		    (!IN_FASTRECOVERY(tp->t_flags))) {
8945 			/* Segment was a TLP and our retrans matched */
8946 			if (rack->r_ctl.rc_tlp_cwnd_reduce) {
8947 				rack_cong_signal(tp, CC_NDUPACK, th_ack, __LINE__);
8948 			}
8949 		}
8950 		if ((rack->r_ctl.rc_rack_tmit_time == 0) ||
8951 		    (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
8952 			    (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]))) {
8953 			/* New more recent rack_tmit_time */
8954 			rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
8955 			if (rack->r_ctl.rc_rack_tmit_time == 0)
8956 				rack->r_ctl.rc_rack_tmit_time = 1;
8957 			rack->rc_rack_rtt = t;
8958 		}
8959 		return (1);
8960 	}
8961 	/*
8962 	 * We clear the soft/rxtshift since we got an ack.
8963 	 * There is no assurance we will call the commit() function
8964 	 * so we need to clear these to avoid incorrect handling.
8965 	 */
8966 	tp->t_rxtshift = 0;
8967 	RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
8968 		      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
8969 	tp->t_softerror = 0;
8970 	if (to && (to->to_flags & TOF_TS) &&
8971 	    (ack_type == CUM_ACKED) &&
8972 	    (to->to_tsecr) &&
8973 	    ((rsm->r_flags & RACK_OVERMAX) == 0)) {
8974 		/*
8975 		 * Now which timestamp does it match? In this block the ACK
8976 		 * must be coming from a previous transmission.
8977 		 */
8978 		for (i = 0; i < rsm->r_rtr_cnt; i++) {
8979 			if (rack_ts_to_msec(rsm->r_tim_lastsent[i]) == to->to_tsecr) {
8980 				t = cts - (uint32_t)rsm->r_tim_lastsent[i];
8981 				if ((int)t <= 0)
8982 					t = 1;
8983 				if (CC_ALGO(tp)->rttsample != NULL) {
8984 					/*
8985 					 * Kick the RTT to the CC, here
8986 					 * we lie a bit in that we know the
8987 					 * retransmission is correct even though
8988 					 * we retransmitted. This is because
8989 					 * we match the timestamps.
8990 					 */
8991 					if (TSTMP_GT(tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time), rsm->r_tim_lastsent[i]))
8992 						us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - (uint32_t)rsm->r_tim_lastsent[i];
8993 					else
8994 						us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[i];
8995 					CC_ALGO(tp)->rttsample(&tp->t_ccv, us_rtt, 1, rsm->r_fas);
8996 				}
8997 				if ((i + 1) < rsm->r_rtr_cnt) {
8998 					/*
8999 					 * The peer ack'd from our previous
9000 					 * transmission. We have a spurious
9001 					 * retransmission and thus we dont
9002 					 * want to update our rack_rtt.
9003 					 *
9004 					 * Hmm should there be a CC revert here?
9005 					 *
9006 					 */
9007 					return (0);
9008 				}
9009 				if (!tp->t_rttlow || tp->t_rttlow > t)
9010 					tp->t_rttlow = t;
9011 				if (!rack->r_ctl.rc_rack_min_rtt || SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
9012 					rack->r_ctl.rc_rack_min_rtt = t;
9013 					if (rack->r_ctl.rc_rack_min_rtt == 0) {
9014 						rack->r_ctl.rc_rack_min_rtt = 1;
9015 					}
9016 				}
9017 				if ((rack->r_ctl.rc_rack_tmit_time == 0) ||
9018 				    (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
9019 					    (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]))) {
9020 					/* New more recent rack_tmit_time */
9021 					rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
9022 					if (rack->r_ctl.rc_rack_tmit_time == 0)
9023 						rack->r_ctl.rc_rack_tmit_time = 1;
9024 					rack->rc_rack_rtt = t;
9025 				}
9026 				rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[i], cts, 3);
9027 				tcp_rack_xmit_timer(rack, t + 1, len_acked, t, 0, rsm,
9028 						    rsm->r_rtr_cnt);
9029 				return (1);
9030 			}
9031 		}
9032 		/* If we are logging log out the sendmap */
9033 		if (tcp_bblogging_on(rack->rc_tp)) {
9034 			for (i = 0; i < rsm->r_rtr_cnt; i++) {
9035 				rack_log_rtt_sendmap(rack, i, rsm->r_tim_lastsent[i], to->to_tsecr);
9036 			}
9037 		}
9038 		goto ts_not_found;
9039 	} else {
9040 		/*
9041 		 * Ok its a SACK block that we retransmitted. or a windows
9042 		 * machine without timestamps. We can tell nothing from the
9043 		 * time-stamp since its not there or the time the peer last
9044 		 * received a segment that moved forward its cum-ack point.
9045 		 */
9046 ts_not_found:
9047 		i = rsm->r_rtr_cnt - 1;
9048 		t = cts - (uint32_t)rsm->r_tim_lastsent[i];
9049 		if ((int)t <= 0)
9050 			t = 1;
9051 		if (rack->r_ctl.rc_rack_min_rtt && SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
9052 			/*
9053 			 * We retransmitted and the ack came back in less
9054 			 * than the smallest rtt we have observed. We most
9055 			 * likely did an improper retransmit as outlined in
9056 			 * 6.2 Step 2 point 2 in the rack-draft so we
9057 			 * don't want to update our rack_rtt. We in
9058 			 * theory (in future) might want to think about reverting our
9059 			 * cwnd state but we won't for now.
9060 			 */
9061 			return (0);
9062 		} else if (rack->r_ctl.rc_rack_min_rtt) {
9063 			/*
9064 			 * We retransmitted it and the retransmit did the
9065 			 * job.
9066 			 */
9067 			if (!rack->r_ctl.rc_rack_min_rtt ||
9068 			    SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
9069 				rack->r_ctl.rc_rack_min_rtt = t;
9070 				if (rack->r_ctl.rc_rack_min_rtt == 0) {
9071 					rack->r_ctl.rc_rack_min_rtt = 1;
9072 				}
9073 			}
9074 			if ((rack->r_ctl.rc_rack_tmit_time == 0) ||
9075 			    (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
9076 				    (uint32_t)rsm->r_tim_lastsent[i]))) {
9077 				/* New more recent rack_tmit_time */
9078 				rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[i];
9079 				if (rack->r_ctl.rc_rack_tmit_time == 0)
9080 					rack->r_ctl.rc_rack_tmit_time = 1;
9081 				rack->rc_rack_rtt = t;
9082 			}
9083 			return (1);
9084 		}
9085 	}
9086 	return (0);
9087 }
9088 
9089 /*
9090  * Mark the SACK_PASSED flag on all entries prior to rsm send wise.
9091  */
9092 static void
9093 rack_log_sack_passed(struct tcpcb *tp,
9094     struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t cts)
9095 {
9096 	struct rack_sendmap *nrsm;
9097 	uint32_t thresh;
9098 
9099 	/* Get our rxt threshold for lost consideration */
9100 	thresh = rack_calc_thresh_rack(rack, rack_grab_rtt(tp, rack), cts, __LINE__, 0);
9101 	/* Now start looking at rsm's */
9102 	nrsm = rsm;
9103 	TAILQ_FOREACH_REVERSE_FROM(nrsm, &rack->r_ctl.rc_tmap,
9104 	    rack_head, r_tnext) {
9105 		if (nrsm == rsm) {
9106 			/* Skip original segment he is acked */
9107 			continue;
9108 		}
9109 		if (nrsm->r_flags & RACK_ACKED) {
9110 			/*
9111 			 * Skip ack'd segments, though we
9112 			 * should not see these, since tmap
9113 			 * should not have ack'd segments.
9114 			 */
9115 			continue;
9116 		}
9117 		if (nrsm->r_flags & RACK_RWND_COLLAPSED) {
9118 			/*
9119 			 * If the peer dropped the rwnd on
9120 			 * these then we don't worry about them.
9121 			 */
9122 			continue;
9123 		}
9124 		/* Check lost state */
9125 		if ((nrsm->r_flags & RACK_WAS_LOST) == 0) {
9126 			uint32_t exp;
9127 
9128 			exp = ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]) + thresh;
9129 			if (TSTMP_LT(exp, cts) || (exp == cts)) {
9130 				/* We consider it lost */
9131 				nrsm->r_flags |= RACK_WAS_LOST;
9132 				rack->r_ctl.rc_considered_lost += nrsm->r_end - nrsm->r_start;
9133 			}
9134 		}
9135 		if (nrsm->r_flags & RACK_SACK_PASSED) {
9136 			/*
9137 			 * We found one that is already marked
9138 			 * passed, we have been here before and
9139 			 * so all others below this are marked.
9140 			 */
9141 			break;
9142 		}
9143 		nrsm->r_flags |= RACK_SACK_PASSED;
9144 		nrsm->r_flags &= ~RACK_WAS_SACKPASS;
9145 	}
9146 }
9147 
9148 static void
9149 rack_need_set_test(struct tcpcb *tp,
9150 		   struct tcp_rack *rack,
9151 		   struct rack_sendmap *rsm,
9152 		   tcp_seq th_ack,
9153 		   int line,
9154 		   int use_which)
9155 {
9156 	struct rack_sendmap *s_rsm;
9157 
9158 	if ((tp->t_flags & TF_GPUTINPROG) &&
9159 	    SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
9160 		/*
9161 		 * We were app limited, and this ack
9162 		 * butts up or goes beyond the point where we want
9163 		 * to start our next measurement. We need
9164 		 * to record the new gput_ts as here and
9165 		 * possibly update the start sequence.
9166 		 */
9167 		uint32_t seq, ts;
9168 
9169 		if (rsm->r_rtr_cnt > 1) {
9170 			/*
9171 			 * This is a retransmit, can we
9172 			 * really make any assessment at this
9173 			 * point?  We are not really sure of
9174 			 * the timestamp, is it this or the
9175 			 * previous transmission?
9176 			 *
9177 			 * Lets wait for something better that
9178 			 * is not retransmitted.
9179 			 */
9180 			return;
9181 		}
9182 		seq = tp->gput_seq;
9183 		ts = tp->gput_ts;
9184 		rack->app_limited_needs_set = 0;
9185 		tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
9186 		/* Do we start at a new end? */
9187 		if ((use_which == RACK_USE_BEG) &&
9188 		    SEQ_GEQ(rsm->r_start, tp->gput_seq)) {
9189 			/*
9190 			 * When we get an ACK that just eats
9191 			 * up some of the rsm, we set RACK_USE_BEG
9192 			 * since whats at r_start (i.e. th_ack)
9193 			 * is left unacked and thats where the
9194 			 * measurement now starts.
9195 			 */
9196 			tp->gput_seq = rsm->r_start;
9197 		}
9198 		if ((use_which == RACK_USE_END) &&
9199 		    SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
9200 			/*
9201 			 * We use the end when the cumack
9202 			 * is moving forward and completely
9203 			 * deleting the rsm passed so basically
9204 			 * r_end holds th_ack.
9205 			 *
9206 			 * For SACK's we also want to use the end
9207 			 * since this piece just got sacked and
9208 			 * we want to target anything after that
9209 			 * in our measurement.
9210 			 */
9211 			tp->gput_seq = rsm->r_end;
9212 		}
9213 		if (use_which == RACK_USE_END_OR_THACK) {
9214 			/*
9215 			 * special case for ack moving forward,
9216 			 * not a sack, we need to move all the
9217 			 * way up to where this ack cum-ack moves
9218 			 * to.
9219 			 */
9220 			if (SEQ_GT(th_ack, rsm->r_end))
9221 				tp->gput_seq = th_ack;
9222 			else
9223 				tp->gput_seq = rsm->r_end;
9224 		}
9225 		if (SEQ_LT(tp->gput_seq, tp->snd_max))
9226 			s_rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
9227 		else
9228 			s_rsm = NULL;
9229 		/*
9230 		 * Pick up the correct send time if we can the rsm passed in
9231 		 * may be equal to s_rsm if the RACK_USE_BEG was set. For the other
9232 		 * two cases (RACK_USE_THACK or RACK_USE_END) most likely we will
9233 		 * find a different seq i.e. the next send up.
9234 		 *
9235 		 * If that has not been sent, s_rsm will be NULL and we must
9236 		 * arrange it so this function will get called again by setting
9237 		 * app_limited_needs_set.
9238 		 */
9239 		if (s_rsm)
9240 			rack->r_ctl.rc_gp_output_ts = s_rsm->r_tim_lastsent[0];
9241 		else {
9242 			/* If we hit here we have to have *not* sent tp->gput_seq */
9243 			rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[0];
9244 			/* Set it up so we will go through here again */
9245 			rack->app_limited_needs_set = 1;
9246 		}
9247 		if (SEQ_GT(tp->gput_seq, tp->gput_ack)) {
9248 			/*
9249 			 * We moved beyond this guy's range, re-calculate
9250 			 * the new end point.
9251 			 */
9252 			if (rack->rc_gp_filled == 0) {
9253 				tp->gput_ack = tp->gput_seq + max(rc_init_window(rack), (MIN_GP_WIN * ctf_fixed_maxseg(tp)));
9254 			} else {
9255 				tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
9256 			}
9257 		}
9258 		/*
9259 		 * We are moving the goal post, we may be able to clear the
9260 		 * measure_saw_probe_rtt flag.
9261 		 */
9262 		if ((rack->in_probe_rtt == 0) &&
9263 		    (rack->measure_saw_probe_rtt) &&
9264 		    (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
9265 			rack->measure_saw_probe_rtt = 0;
9266 		rack_log_pacing_delay_calc(rack, ts, tp->gput_ts,
9267 					   seq, tp->gput_seq,
9268 					   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) |
9269 					    (uint64_t)rack->r_ctl.rc_gp_output_ts),
9270 					   5, line, NULL, 0);
9271 		if (rack->rc_gp_filled &&
9272 		    ((tp->gput_ack - tp->gput_seq) <
9273 		     max(rc_init_window(rack), (MIN_GP_WIN *
9274 						ctf_fixed_maxseg(tp))))) {
9275 			uint32_t ideal_amount;
9276 
9277 			ideal_amount = rack_get_measure_window(tp, rack);
9278 			if (ideal_amount > sbavail(&tptosocket(tp)->so_snd)) {
9279 				/*
9280 				 * There is no sense of continuing this measurement
9281 				 * because its too small to gain us anything we
9282 				 * trust. Skip it and that way we can start a new
9283 				 * measurement quicker.
9284 				 */
9285 				tp->t_flags &= ~TF_GPUTINPROG;
9286 				rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq,
9287 							   0, 0,
9288 							   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) |
9289 							    (uint64_t)rack->r_ctl.rc_gp_output_ts),
9290 							   6, __LINE__, NULL, 0);
9291 			} else {
9292 				/*
9293 				 * Reset the window further out.
9294 				 */
9295 				tp->gput_ack = tp->gput_seq + ideal_amount;
9296 			}
9297 		}
9298 		rack_tend_gp_marks(tp, rack);
9299 		rack_log_gpset(rack, tp->gput_ack, 0, 0, line, 2, rsm);
9300 	}
9301 }
9302 
9303 static inline int
9304 is_rsm_inside_declared_tlp_block(struct tcp_rack *rack, struct rack_sendmap *rsm)
9305 {
9306 	if (SEQ_LT(rsm->r_end, rack->r_ctl.last_tlp_acked_start)) {
9307 		/* Behind our TLP definition or right at */
9308 		return (0);
9309 	}
9310 	if (SEQ_GT(rsm->r_start, rack->r_ctl.last_tlp_acked_end)) {
9311 		/* The start is beyond or right at our end of TLP definition */
9312 		return (0);
9313 	}
9314 	/* It has to be a sub-part of the original TLP recorded */
9315 	return (1);
9316 }
9317 
9318 static uint32_t
9319 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack, struct sackblk *sack,
9320 		   struct tcpopt *to, struct rack_sendmap **prsm, uint32_t cts,
9321 		   uint32_t segsiz)
9322 {
9323 	uint32_t start, end, changed = 0;
9324 	struct rack_sendmap stack_map;
9325 	struct rack_sendmap *rsm, *nrsm, *prev, *next;
9326 	int insret __diagused;
9327 	int32_t used_ref = 1;
9328 	int can_use_hookery = 0;
9329 
9330 	start = sack->start;
9331 	end = sack->end;
9332 	rsm = *prsm;
9333 
9334 do_rest_ofb:
9335 	if ((rsm == NULL) ||
9336 	    (SEQ_LT(end, rsm->r_start)) ||
9337 	    (SEQ_GEQ(start, rsm->r_end)) ||
9338 	    (SEQ_LT(start, rsm->r_start))) {
9339 		/*
9340 		 * We are not in the right spot,
9341 		 * find the correct spot in the tree.
9342 		 */
9343 		used_ref = 0;
9344 		rsm = tqhash_find(rack->r_ctl.tqh, start);
9345 	}
9346 	if (rsm == NULL) {
9347 		/* TSNH */
9348 		goto out;
9349 	}
9350 	/* Ok we have an ACK for some piece of this rsm */
9351 	if (rsm->r_start != start) {
9352 		if ((rsm->r_flags & RACK_ACKED) == 0) {
9353 			/*
9354 			 * Before any splitting or hookery is
9355 			 * done is it a TLP of interest i.e. rxt?
9356 			 */
9357 			if ((rsm->r_flags & RACK_TLP) &&
9358 			    (rsm->r_rtr_cnt > 1)) {
9359 				/*
9360 				 * We are splitting a rxt TLP, check
9361 				 * if we need to save off the start/end
9362 				 */
9363 				if (rack->rc_last_tlp_acked_set &&
9364 				    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
9365 					/*
9366 					 * We already turned this on since we are inside
9367 					 * the previous one was a partially sack now we
9368 					 * are getting another one (maybe all of it).
9369 					 *
9370 					 */
9371 					rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
9372 					/*
9373 					 * Lets make sure we have all of it though.
9374 					 */
9375 					if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
9376 						rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9377 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9378 								     rack->r_ctl.last_tlp_acked_end);
9379 					}
9380 					if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
9381 						rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9382 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9383 								     rack->r_ctl.last_tlp_acked_end);
9384 					}
9385 				} else {
9386 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9387 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9388 					rack->rc_last_tlp_past_cumack = 0;
9389 					rack->rc_last_tlp_acked_set = 1;
9390 					rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
9391 				}
9392 			}
9393 			/**
9394 			 * Need to split this in two pieces the before and after,
9395 			 * the before remains in the map, the after must be
9396 			 * added. In other words we have:
9397 			 * rsm        |--------------|
9398 			 * sackblk        |------->
9399 			 * rsm will become
9400 			 *     rsm    |---|
9401 			 * and nrsm will be  the sacked piece
9402 			 *     nrsm       |----------|
9403 			 *
9404 			 * But before we start down that path lets
9405 			 * see if the sack spans over on top of
9406 			 * the next guy and it is already sacked.
9407 			 *
9408 			 */
9409 			/*
9410 			 * Hookery can only be used if the two entries
9411 			 * are in the same bucket and neither one of
9412 			 * them staddle the bucket line.
9413 			 */
9414 			next = tqhash_next(rack->r_ctl.tqh, rsm);
9415 			if (next &&
9416 			    (rsm->bindex == next->bindex) &&
9417 			    ((rsm->r_flags & RACK_STRADDLE) == 0) &&
9418 			    ((next->r_flags & RACK_STRADDLE) == 0) &&
9419 			    ((rsm->r_flags & RACK_IS_PCM) == 0) &&
9420 			    ((next->r_flags & RACK_IS_PCM) == 0) &&
9421 			    (rsm->r_flags & RACK_IN_GP_WIN) &&
9422 			    (next->r_flags & RACK_IN_GP_WIN))
9423 				can_use_hookery = 1;
9424 			else
9425 				can_use_hookery = 0;
9426 			if (next && can_use_hookery &&
9427 			    (next->r_flags & RACK_ACKED) &&
9428 			    SEQ_GEQ(end, next->r_start)) {
9429 				/**
9430 				 * So the next one is already acked, and
9431 				 * we can thus by hookery use our stack_map
9432 				 * to reflect the piece being sacked and
9433 				 * then adjust the two tree entries moving
9434 				 * the start and ends around. So we start like:
9435 				 *  rsm     |------------|             (not-acked)
9436 				 *  next                 |-----------| (acked)
9437 				 *  sackblk        |-------->
9438 				 *  We want to end like so:
9439 				 *  rsm     |------|                   (not-acked)
9440 				 *  next           |-----------------| (acked)
9441 				 *  nrsm           |-----|
9442 				 * Where nrsm is a temporary stack piece we
9443 				 * use to update all the gizmos.
9444 				 */
9445 				/* Copy up our fudge block */
9446 				nrsm = &stack_map;
9447 				memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
9448 				/* Now adjust our tree blocks */
9449 				tqhash_update_end(rack->r_ctl.tqh, rsm, start);
9450 				next->r_start = start;
9451  				rsm->r_flags |= RACK_SHUFFLED;
9452 				next->r_flags |= RACK_SHUFFLED;
9453 				/* Now we must adjust back where next->m is */
9454 				rack_setup_offset_for_rsm(rack, rsm, next);
9455 				/*
9456 				 * Which timestamp do we keep? It is rather
9457 				 * important in GP measurements to have the
9458 				 * accurate end of the send window.
9459 				 *
9460 				 * We keep the largest value, which is the newest
9461 				 * send. We do this in case a segment that is
9462 				 * joined together and not part of a GP estimate
9463 				 * later gets expanded into the GP estimate.
9464 				 *
9465 				 * We prohibit the merging of unlike kinds i.e.
9466 				 * all pieces that are in the GP estimate can be
9467 				 * merged and all pieces that are not in a GP estimate
9468 				 * can be merged, but not disimilar pieces. Combine
9469 				 * this with taking the highest here and we should
9470 				 * be ok unless of course the client reneges. Then
9471 				 * all bets are off.
9472 				 */
9473 				if (next->r_tim_lastsent[(next->r_rtr_cnt-1)] <
9474 				    nrsm->r_tim_lastsent[(nrsm->r_rtr_cnt-1)])
9475 					next->r_tim_lastsent[(next->r_rtr_cnt-1)] = nrsm->r_tim_lastsent[(nrsm->r_rtr_cnt-1)];
9476 				/*
9477 				 * And we must keep the newest ack arrival time.
9478 				 */
9479 				if (next->r_ack_arrival <
9480 				    rack_to_usec_ts(&rack->r_ctl.act_rcv_time))
9481 					next->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
9482 
9483 
9484 				/* We don't need to adjust rsm, it did not change */
9485 				/* Clear out the dup ack count of the remainder */
9486 				rsm->r_dupack = 0;
9487 				rsm->r_just_ret = 0;
9488 				rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
9489 				/* Now lets make sure our fudge block is right */
9490 				nrsm->r_start = start;
9491 				/* Now lets update all the stats and such */
9492 				rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0);
9493 				if (rack->app_limited_needs_set)
9494 					rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END);
9495 				changed += (nrsm->r_end - nrsm->r_start);
9496 				rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start);
9497 				if (rsm->r_flags & RACK_WAS_LOST) {
9498 					int my_chg;
9499 
9500 					my_chg = (nrsm->r_end - nrsm->r_start);
9501 					KASSERT((rack->r_ctl.rc_considered_lost >= my_chg),
9502 						("rsm:%p rack:%p rc_considered_lost goes negative", rsm,  rack));
9503 					if (my_chg <= rack->r_ctl.rc_considered_lost)
9504 						rack->r_ctl.rc_considered_lost -= my_chg;
9505 					else
9506 						rack->r_ctl.rc_considered_lost = 0;
9507 				}
9508 				if (nrsm->r_flags & RACK_SACK_PASSED) {
9509 					rack->r_ctl.rc_reorder_ts = cts;
9510 					if (rack->r_ctl.rc_reorder_ts == 0)
9511 						rack->r_ctl.rc_reorder_ts = 1;
9512 				}
9513 				/*
9514 				 * Now we want to go up from rsm (the
9515 				 * one left un-acked) to the next one
9516 				 * in the tmap. We do this so when
9517 				 * we walk backwards we include marking
9518 				 * sack-passed on rsm (The one passed in
9519 				 * is skipped since it is generally called
9520 				 * on something sacked before removing it
9521 				 * from the tmap).
9522 				 */
9523 				if (rsm->r_in_tmap) {
9524 					nrsm = TAILQ_NEXT(rsm, r_tnext);
9525 					/*
9526 					 * Now that we have the next
9527 					 * one walk backwards from there.
9528 					 */
9529 					if (nrsm && nrsm->r_in_tmap)
9530 						rack_log_sack_passed(tp, rack, nrsm, cts);
9531 				}
9532 				/* Now are we done? */
9533 				if (SEQ_LT(end, next->r_end) ||
9534 				    (end == next->r_end)) {
9535 					/* Done with block */
9536 					goto out;
9537 				}
9538 				rack_log_map_chg(tp, rack, &stack_map, rsm, next, MAP_SACK_M1, end, __LINE__);
9539 				counter_u64_add(rack_sack_used_next_merge, 1);
9540 				/* Postion for the next block */
9541 				start = next->r_end;
9542 				rsm = tqhash_next(rack->r_ctl.tqh, next);
9543 				if (rsm == NULL)
9544 					goto out;
9545 			} else {
9546 				/**
9547 				 * We can't use any hookery here, so we
9548 				 * need to split the map. We enter like
9549 				 * so:
9550 				 *  rsm      |--------|
9551 				 *  sackblk       |----->
9552 				 * We will add the new block nrsm and
9553 				 * that will be the new portion, and then
9554 				 * fall through after reseting rsm. So we
9555 				 * split and look like this:
9556 				 *  rsm      |----|
9557 				 *  sackblk       |----->
9558 				 *  nrsm          |---|
9559 				 * We then fall through reseting
9560 				 * rsm to nrsm, so the next block
9561 				 * picks it up.
9562 				 */
9563 				nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
9564 				if (nrsm == NULL) {
9565 					/*
9566 					 * failed XXXrrs what can we do but loose the sack
9567 					 * info?
9568 					 */
9569 					goto out;
9570 				}
9571 				counter_u64_add(rack_sack_splits, 1);
9572 				rack_clone_rsm(rack, nrsm, rsm, start);
9573 				rsm->r_just_ret = 0;
9574 #ifndef INVARIANTS
9575 				(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
9576 #else
9577 				if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
9578 					panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p",
9579 					      nrsm, insret, rack, rsm);
9580 				}
9581 #endif
9582 				if (rsm->r_in_tmap) {
9583 					TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
9584 					nrsm->r_in_tmap = 1;
9585 				}
9586 				rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M2, end, __LINE__);
9587 				rsm->r_flags &= (~RACK_HAS_FIN);
9588 				/* Position us to point to the new nrsm that starts the sack blk */
9589 				rsm = nrsm;
9590 			}
9591 		} else {
9592 			/* Already sacked this piece */
9593 			counter_u64_add(rack_sack_skipped_acked, 1);
9594 			if (end == rsm->r_end) {
9595 				/* Done with block */
9596 				rsm = tqhash_next(rack->r_ctl.tqh, rsm);
9597 				goto out;
9598 			} else if (SEQ_LT(end, rsm->r_end)) {
9599 				/* A partial sack to a already sacked block */
9600 				rsm = tqhash_next(rack->r_ctl.tqh, rsm);
9601 				goto out;
9602 			} else {
9603 				/*
9604 				 * The end goes beyond this guy
9605 				 * reposition the start to the
9606 				 * next block.
9607 				 */
9608 				start = rsm->r_end;
9609 				rsm = tqhash_next(rack->r_ctl.tqh, rsm);
9610 				if (rsm == NULL)
9611 					goto out;
9612 			}
9613 		}
9614 	}
9615 	if (SEQ_GEQ(end, rsm->r_end)) {
9616 		/**
9617 		 * The end of this block is either beyond this guy or right
9618 		 * at this guy. I.e.:
9619 		 *  rsm ---                 |-----|
9620 		 *  end                     |-----|
9621 		 *  <or>
9622 		 *  end                     |---------|
9623 		 */
9624 		if ((rsm->r_flags & RACK_ACKED) == 0) {
9625 			/*
9626 			 * Is it a TLP of interest?
9627 			 */
9628 			if ((rsm->r_flags & RACK_TLP) &&
9629 			    (rsm->r_rtr_cnt > 1)) {
9630 				/*
9631 				 * We are splitting a rxt TLP, check
9632 				 * if we need to save off the start/end
9633 				 */
9634 				if (rack->rc_last_tlp_acked_set &&
9635 				    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
9636 					/*
9637 					 * We already turned this on since we are inside
9638 					 * the previous one was a partially sack now we
9639 					 * are getting another one (maybe all of it).
9640 					 */
9641 					rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
9642 					/*
9643 					 * Lets make sure we have all of it though.
9644 					 */
9645 					if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
9646 						rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9647 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9648 								     rack->r_ctl.last_tlp_acked_end);
9649 					}
9650 					if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
9651 						rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9652 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9653 								     rack->r_ctl.last_tlp_acked_end);
9654 					}
9655 				} else {
9656 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9657 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9658 					rack->rc_last_tlp_past_cumack = 0;
9659 					rack->rc_last_tlp_acked_set = 1;
9660 					rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
9661 				}
9662 			}
9663 			rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0);
9664 			changed += (rsm->r_end - rsm->r_start);
9665 			/* You get a count for acking a whole segment or more */
9666 			if (rsm->r_flags & RACK_WAS_LOST) {
9667 				int my_chg;
9668 
9669 				my_chg = (rsm->r_end - rsm->r_start);
9670 				rsm->r_flags &= ~RACK_WAS_LOST;
9671 				KASSERT((rack->r_ctl.rc_considered_lost >= my_chg),
9672 					("rsm:%p rack:%p rc_considered_lost goes negative", rsm,  rack));
9673 				if (my_chg <= rack->r_ctl.rc_considered_lost)
9674 					rack->r_ctl.rc_considered_lost -= my_chg;
9675 				else
9676 					rack->r_ctl.rc_considered_lost = 0;
9677 			}
9678 			rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
9679 			if (rsm->r_in_tmap) /* should be true */
9680 				rack_log_sack_passed(tp, rack, rsm, cts);
9681 			/* Is Reordering occuring? */
9682 			if (rsm->r_flags & RACK_SACK_PASSED) {
9683 				rsm->r_flags &= ~RACK_SACK_PASSED;
9684 				rack->r_ctl.rc_reorder_ts = cts;
9685 				if (rack->r_ctl.rc_reorder_ts == 0)
9686 					rack->r_ctl.rc_reorder_ts = 1;
9687 			}
9688 			if (rack->app_limited_needs_set)
9689 				rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END);
9690 			rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
9691 			rsm->r_flags |= RACK_ACKED;
9692 			rack_update_pcm_ack(rack, 0, rsm->r_start, rsm->r_end);
9693 			if (rsm->r_in_tmap) {
9694 				TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
9695 				rsm->r_in_tmap = 0;
9696 			}
9697 			rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_SACK_M3, end, __LINE__);
9698 		} else {
9699 			counter_u64_add(rack_sack_skipped_acked, 1);
9700 		}
9701 		if (end == rsm->r_end) {
9702 			/* This block only - done, setup for next */
9703 			goto out;
9704 		}
9705 		/*
9706 		 * There is more not coverend by this rsm move on
9707 		 * to the next block in the tail queue hash table.
9708 		 */
9709 		nrsm = tqhash_next(rack->r_ctl.tqh, rsm);
9710 		start = rsm->r_end;
9711 		rsm = nrsm;
9712 		if (rsm == NULL)
9713 			goto out;
9714 		goto do_rest_ofb;
9715 	}
9716 	/**
9717 	 * The end of this sack block is smaller than
9718 	 * our rsm i.e.:
9719 	 *  rsm ---                 |-----|
9720 	 *  end                     |--|
9721 	 */
9722 	if ((rsm->r_flags & RACK_ACKED) == 0) {
9723 		/*
9724 		 * Is it a TLP of interest?
9725 		 */
9726 		if ((rsm->r_flags & RACK_TLP) &&
9727 		    (rsm->r_rtr_cnt > 1)) {
9728 			/*
9729 			 * We are splitting a rxt TLP, check
9730 			 * if we need to save off the start/end
9731 			 */
9732 			if (rack->rc_last_tlp_acked_set &&
9733 			    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
9734 				/*
9735 				 * We already turned this on since we are inside
9736 				 * the previous one was a partially sack now we
9737 				 * are getting another one (maybe all of it).
9738 				 */
9739 				rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
9740 				/*
9741 				 * Lets make sure we have all of it though.
9742 				 */
9743 				if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
9744 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9745 					rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9746 							     rack->r_ctl.last_tlp_acked_end);
9747 				}
9748 				if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
9749 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9750 					rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9751 							     rack->r_ctl.last_tlp_acked_end);
9752 				}
9753 			} else {
9754 				rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9755 				rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9756 				rack->rc_last_tlp_past_cumack = 0;
9757 				rack->rc_last_tlp_acked_set = 1;
9758 				rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
9759 			}
9760 		}
9761 		/*
9762 		 * Hookery can only be used if the two entries
9763 		 * are in the same bucket and neither one of
9764 		 * them staddle the bucket line.
9765 		 */
9766 		prev = tqhash_prev(rack->r_ctl.tqh, rsm);
9767 		if (prev &&
9768 		    (rsm->bindex == prev->bindex) &&
9769 		    ((rsm->r_flags & RACK_STRADDLE) == 0) &&
9770 		    ((prev->r_flags & RACK_STRADDLE) == 0) &&
9771 		    ((rsm->r_flags & RACK_IS_PCM) == 0) &&
9772 		    ((prev->r_flags & RACK_IS_PCM) == 0) &&
9773 		    (rsm->r_flags & RACK_IN_GP_WIN) &&
9774 		    (prev->r_flags & RACK_IN_GP_WIN))
9775 			can_use_hookery = 1;
9776 		else
9777 			can_use_hookery = 0;
9778 		if (prev && can_use_hookery &&
9779 		    (prev->r_flags & RACK_ACKED)) {
9780 			/**
9781 			 * Goal, we want the right remainder of rsm to shrink
9782 			 * in place and span from (rsm->r_start = end) to rsm->r_end.
9783 			 * We want to expand prev to go all the way
9784 			 * to prev->r_end <- end.
9785 			 * so in the tree we have before:
9786 			 *   prev     |--------|         (acked)
9787 			 *   rsm               |-------| (non-acked)
9788 			 *   sackblk           |-|
9789 			 * We churn it so we end up with
9790 			 *   prev     |----------|       (acked)
9791 			 *   rsm                 |-----| (non-acked)
9792 			 *   nrsm              |-| (temporary)
9793 			 *
9794 			 * Note if either prev/rsm is a TLP we don't
9795 			 * do this.
9796 			 */
9797 			nrsm = &stack_map;
9798 			memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
9799 			tqhash_update_end(rack->r_ctl.tqh, prev, end);
9800 			rsm->r_start = end;
9801 			rsm->r_flags |= RACK_SHUFFLED;
9802 			prev->r_flags |= RACK_SHUFFLED;
9803 			/* Now adjust nrsm (stack copy) to be
9804 			 * the one that is the small
9805 			 * piece that was "sacked".
9806 			 */
9807 			nrsm->r_end = end;
9808 			rsm->r_dupack = 0;
9809 			/*
9810 			 * Which timestamp do we keep? It is rather
9811 			 * important in GP measurements to have the
9812 			 * accurate end of the send window.
9813 			 *
9814 			 * We keep the largest value, which is the newest
9815 			 * send. We do this in case a segment that is
9816 			 * joined together and not part of a GP estimate
9817 			 * later gets expanded into the GP estimate.
9818 			 *
9819 			 * We prohibit the merging of unlike kinds i.e.
9820 			 * all pieces that are in the GP estimate can be
9821 			 * merged and all pieces that are not in a GP estimate
9822 			 * can be merged, but not disimilar pieces. Combine
9823 			 * this with taking the highest here and we should
9824 			 * be ok unless of course the client reneges. Then
9825 			 * all bets are off.
9826 			 */
9827 			if(prev->r_tim_lastsent[(prev->r_rtr_cnt-1)] <
9828 			   nrsm->r_tim_lastsent[(nrsm->r_rtr_cnt-1)]) {
9829 				prev->r_tim_lastsent[(prev->r_rtr_cnt-1)] = nrsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
9830 			}
9831 			/*
9832 			 * And we must keep the newest ack arrival time.
9833 			 */
9834 
9835 			if(prev->r_ack_arrival <
9836 			   rack_to_usec_ts(&rack->r_ctl.act_rcv_time))
9837 				prev->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
9838 
9839 			rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
9840 			/*
9841 			 * Now that the rsm has had its start moved forward
9842 			 * lets go ahead and get its new place in the world.
9843 			 */
9844 			rack_setup_offset_for_rsm(rack, prev, rsm);
9845 			/*
9846 			 * Now nrsm is our new little piece
9847 			 * that is acked (which was merged
9848 			 * to prev). Update the rtt and changed
9849 			 * based on that. Also check for reordering.
9850 			 */
9851 			rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0);
9852 			if (rack->app_limited_needs_set)
9853 				rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END);
9854 			changed += (nrsm->r_end - nrsm->r_start);
9855 			rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start);
9856 			if (rsm->r_flags & RACK_WAS_LOST) {
9857 				int my_chg;
9858 
9859 				my_chg = (nrsm->r_end - nrsm->r_start);
9860 				KASSERT((rack->r_ctl.rc_considered_lost >= my_chg),
9861 					("rsm:%p rack:%p rc_considered_lost goes negative", rsm,  rack));
9862 				if (my_chg <= rack->r_ctl.rc_considered_lost)
9863 					rack->r_ctl.rc_considered_lost -= my_chg;
9864 				else
9865 					rack->r_ctl.rc_considered_lost = 0;
9866 			}
9867 			if (nrsm->r_flags & RACK_SACK_PASSED) {
9868 				rack->r_ctl.rc_reorder_ts = cts;
9869 				if (rack->r_ctl.rc_reorder_ts == 0)
9870 					rack->r_ctl.rc_reorder_ts = 1;
9871 			}
9872 			rack_log_map_chg(tp, rack, prev, &stack_map, rsm, MAP_SACK_M4, end, __LINE__);
9873 			rsm = prev;
9874 			counter_u64_add(rack_sack_used_prev_merge, 1);
9875 		} else {
9876 			/**
9877 			 * This is the case where our previous
9878 			 * block is not acked either, so we must
9879 			 * split the block in two.
9880 			 */
9881 			nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
9882 			if (nrsm == NULL) {
9883 				/* failed rrs what can we do but loose the sack info? */
9884 				goto out;
9885 			}
9886 			if ((rsm->r_flags & RACK_TLP) &&
9887 			    (rsm->r_rtr_cnt > 1)) {
9888 				/*
9889 				 * We are splitting a rxt TLP, check
9890 				 * if we need to save off the start/end
9891 				 */
9892 				if (rack->rc_last_tlp_acked_set &&
9893 				    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
9894 					/*
9895 					 * We already turned this on since this block is inside
9896 					 * the previous one was a partially sack now we
9897 					 * are getting another one (maybe all of it).
9898 					 */
9899 					rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
9900 					/*
9901 					 * Lets make sure we have all of it though.
9902 					 */
9903 					if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
9904 						rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9905 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9906 								     rack->r_ctl.last_tlp_acked_end);
9907 					}
9908 					if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
9909 						rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9910 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9911 								     rack->r_ctl.last_tlp_acked_end);
9912 					}
9913 				} else {
9914 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9915 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9916 					rack->rc_last_tlp_acked_set = 1;
9917 					rack->rc_last_tlp_past_cumack = 0;
9918 					rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
9919 				}
9920 			}
9921 			/**
9922 			 * In this case nrsm becomes
9923 			 * nrsm->r_start = end;
9924 			 * nrsm->r_end = rsm->r_end;
9925 			 * which is un-acked.
9926 			 * <and>
9927 			 * rsm->r_end = nrsm->r_start;
9928 			 * i.e. the remaining un-acked
9929 			 * piece is left on the left
9930 			 * hand side.
9931 			 *
9932 			 * So we start like this
9933 			 * rsm      |----------| (not acked)
9934 			 * sackblk  |---|
9935 			 * build it so we have
9936 			 * rsm      |---|         (acked)
9937 			 * nrsm         |------|  (not acked)
9938 			 */
9939 			counter_u64_add(rack_sack_splits, 1);
9940 			rack_clone_rsm(rack, nrsm, rsm, end);
9941 			rsm->r_flags &= (~RACK_HAS_FIN);
9942 			rsm->r_just_ret = 0;
9943 #ifndef INVARIANTS
9944 			(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
9945 #else
9946 			if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
9947 				panic("Insert in tailq_hash of %p fails ret:% rack:%p rsm:%p",
9948 				      nrsm, insret, rack, rsm);
9949 			}
9950 #endif
9951 			if (rsm->r_in_tmap) {
9952 				TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
9953 				nrsm->r_in_tmap = 1;
9954 			}
9955 			nrsm->r_dupack = 0;
9956 			rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2);
9957 			rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0);
9958 			changed += (rsm->r_end - rsm->r_start);
9959 			if (rsm->r_flags & RACK_WAS_LOST) {
9960 				int my_chg;
9961 
9962 				my_chg = (rsm->r_end - rsm->r_start);
9963 				rsm->r_flags &= ~RACK_WAS_LOST;
9964 				KASSERT((rack->r_ctl.rc_considered_lost >= my_chg),
9965 					("rsm:%p rack:%p rc_considered_lost goes negative", rsm,  rack));
9966 				if (my_chg <= rack->r_ctl.rc_considered_lost)
9967 					rack->r_ctl.rc_considered_lost -= my_chg;
9968 				else
9969 					rack->r_ctl.rc_considered_lost = 0;
9970 			}
9971 			rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
9972 
9973 			if (rsm->r_in_tmap) /* should be true */
9974 				rack_log_sack_passed(tp, rack, rsm, cts);
9975 			/* Is Reordering occuring? */
9976 			if (rsm->r_flags & RACK_SACK_PASSED) {
9977 				rsm->r_flags &= ~RACK_SACK_PASSED;
9978 				rack->r_ctl.rc_reorder_ts = cts;
9979 				if (rack->r_ctl.rc_reorder_ts == 0)
9980 					rack->r_ctl.rc_reorder_ts = 1;
9981 			}
9982 			if (rack->app_limited_needs_set)
9983 				rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END);
9984 			rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
9985 			rsm->r_flags |= RACK_ACKED;
9986 			rack_update_pcm_ack(rack, 0, rsm->r_start, rsm->r_end);
9987 			rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M5, end, __LINE__);
9988 			if (rsm->r_in_tmap) {
9989 				TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
9990 				rsm->r_in_tmap = 0;
9991 			}
9992 		}
9993 	} else if (start != end){
9994 		/*
9995 		 * The block was already acked.
9996 		 */
9997 		counter_u64_add(rack_sack_skipped_acked, 1);
9998 	}
9999 out:
10000 	if (rsm &&
10001 	    ((rsm->r_flags & RACK_TLP) == 0) &&
10002 	    (rsm->r_flags & RACK_ACKED)) {
10003 		/*
10004 		 * Now can we merge where we worked
10005 		 * with either the previous or
10006 		 * next block?
10007 		 */
10008 		next = tqhash_next(rack->r_ctl.tqh, rsm);
10009 		while (next) {
10010 			if (next->r_flags & RACK_TLP)
10011 				break;
10012 			/* Only allow merges between ones in or out of GP window */
10013 			if ((next->r_flags & RACK_IN_GP_WIN) &&
10014 			    ((rsm->r_flags & RACK_IN_GP_WIN) == 0)) {
10015 				break;
10016 			}
10017 			if ((rsm->r_flags & RACK_IN_GP_WIN) &&
10018 			    ((next->r_flags & RACK_IN_GP_WIN) == 0)) {
10019 				break;
10020 			}
10021 			if (rsm->bindex != next->bindex)
10022 				break;
10023 			if (rsm->r_flags & RACK_STRADDLE)
10024 				break;
10025 			if (rsm->r_flags & RACK_IS_PCM)
10026 				break;
10027 			if (next->r_flags & RACK_STRADDLE)
10028 				break;
10029 			if (next->r_flags & RACK_IS_PCM)
10030 				break;
10031 			if (next->r_flags & RACK_ACKED) {
10032 				/* yep this and next can be merged */
10033 				rsm = rack_merge_rsm(rack, rsm, next);
10034 				next = tqhash_next(rack->r_ctl.tqh, rsm);
10035 			} else
10036 				break;
10037 		}
10038 		/* Now what about the previous? */
10039 		prev = tqhash_prev(rack->r_ctl.tqh, rsm);
10040 		while (prev) {
10041 			if (prev->r_flags & RACK_TLP)
10042 				break;
10043 			/* Only allow merges between ones in or out of GP window */
10044 			if ((prev->r_flags & RACK_IN_GP_WIN) &&
10045 			    ((rsm->r_flags & RACK_IN_GP_WIN) == 0)) {
10046 				break;
10047 			}
10048 			if ((rsm->r_flags & RACK_IN_GP_WIN) &&
10049 			    ((prev->r_flags & RACK_IN_GP_WIN) == 0)) {
10050 				break;
10051 			}
10052 			if (rsm->bindex != prev->bindex)
10053 				break;
10054 			if (rsm->r_flags & RACK_STRADDLE)
10055 				break;
10056 			if (rsm->r_flags & RACK_IS_PCM)
10057 				break;
10058 			if (prev->r_flags & RACK_STRADDLE)
10059 				break;
10060 			if (prev->r_flags & RACK_IS_PCM)
10061 				break;
10062 			if (prev->r_flags & RACK_ACKED) {
10063 				/* yep the previous and this can be merged */
10064 				rsm = rack_merge_rsm(rack, prev, rsm);
10065 				prev = tqhash_prev(rack->r_ctl.tqh, rsm);
10066 			} else
10067 				break;
10068 		}
10069 	}
10070 	if (used_ref == 0) {
10071 		counter_u64_add(rack_sack_proc_all, 1);
10072 	} else {
10073 		counter_u64_add(rack_sack_proc_short, 1);
10074 	}
10075 	/* Save off the next one for quick reference. */
10076 	nrsm = tqhash_find(rack->r_ctl.tqh, end);
10077 	*prsm = rack->r_ctl.rc_sacklast = nrsm;
10078 	return (changed);
10079 }
10080 
10081 static void inline
10082 rack_peer_reneges(struct tcp_rack *rack, struct rack_sendmap *rsm, tcp_seq th_ack)
10083 {
10084 	struct rack_sendmap *tmap;
10085 
10086 	tmap = NULL;
10087 	while (rsm && (rsm->r_flags & RACK_ACKED)) {
10088 		/* Its no longer sacked, mark it so */
10089 		rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
10090 #ifdef INVARIANTS
10091 		if (rsm->r_in_tmap) {
10092 			panic("rack:%p rsm:%p flags:0x%x in tmap?",
10093 			      rack, rsm, rsm->r_flags);
10094 		}
10095 #endif
10096 		rsm->r_flags &= ~(RACK_ACKED|RACK_SACK_PASSED|RACK_WAS_SACKPASS);
10097 		/* Rebuild it into our tmap */
10098 		if (tmap == NULL) {
10099 			TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
10100 			tmap = rsm;
10101 		} else {
10102 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, tmap, rsm, r_tnext);
10103 			tmap = rsm;
10104 		}
10105 		tmap->r_in_tmap = 1;
10106 		rsm = tqhash_next(rack->r_ctl.tqh, rsm);
10107 	}
10108 	/*
10109 	 * Now lets possibly clear the sack filter so we start
10110 	 * recognizing sacks that cover this area.
10111 	 */
10112 	sack_filter_clear(&rack->r_ctl.rack_sf, th_ack);
10113 
10114 }
10115 
10116 
10117 static void inline
10118 rack_rsm_sender_update(struct tcp_rack *rack, struct tcpcb *tp, struct rack_sendmap *rsm, uint8_t from)
10119 {
10120 	/*
10121 	 * We look at advancing the end send time for our GP
10122 	 * measurement tracking only as the cumulative acknowledgment
10123 	 * moves forward. You might wonder about this, why not
10124 	 * at every transmission or retransmission within the
10125 	 * GP window update the rc_gp_cumack_ts? Well its rather
10126 	 * nuanced but basically the GP window *may* expand (as
10127 	 * it does below) or worse and harder to track it may shrink.
10128 	 *
10129 	 * This last makes it impossible to track at the time of
10130 	 * the send, since you may set forward your rc_gp_cumack_ts
10131 	 * when you send, because that send *is* in your currently
10132 	 * "guessed" window, but then it shrinks. Now which was
10133 	 * the send time of the last bytes in the window, by the
10134 	 * time you ask that question that part of the sendmap
10135 	 * is freed. So you don't know and you will have too
10136 	 * long of send window. Instead by updating the time
10137 	 * marker only when the cumack advances this assures us
10138 	 * that we will have only the sends in the window of our
10139 	 * GP measurement.
10140 	 *
10141 	 * Another complication from this is the
10142 	 * merging of sendmap entries. During SACK processing this
10143 	 * can happen to conserve the sendmap size. That breaks
10144 	 * everything down in tracking the send window of the GP
10145 	 * estimate. So to prevent that and keep it working with
10146 	 * a tiny bit more limited merging, we only allow like
10147 	 * types to be merged. I.e. if two sends are in the GP window
10148 	 * then its ok to merge them together. If two sends are not
10149 	 * in the GP window its ok to merge them together too. Though
10150 	 * one send in and one send out cannot be merged. We combine
10151 	 * this with never allowing the shrinking of the GP window when
10152 	 * we are in recovery so that we can properly calculate the
10153 	 * sending times.
10154 	 *
10155 	 * This all of course seems complicated, because it is.. :)
10156 	 *
10157 	 * The cum-ack is being advanced upon the sendmap.
10158 	 * If we are not doing a GP estimate don't
10159 	 * proceed.
10160 	 */
10161 	uint64_t ts;
10162 
10163 	if ((tp->t_flags & TF_GPUTINPROG) == 0)
10164 		return;
10165 	/*
10166 	 * If this sendmap entry is going
10167 	 * beyond the measurement window we had picked,
10168 	 * expand the measurement window by that much.
10169 	 */
10170 	if (SEQ_GT(rsm->r_end, tp->gput_ack)) {
10171 		tp->gput_ack = rsm->r_end;
10172 	}
10173 	/*
10174 	 * If we have not setup a ack, then we
10175 	 * have no idea if the newly acked pieces
10176 	 * will be "in our seq measurement range". If
10177 	 * it is when we clear the app_limited_needs_set
10178 	 * flag the timestamp will be updated.
10179 	 */
10180 	if (rack->app_limited_needs_set)
10181 		return;
10182 	/*
10183 	 * Finally, we grab out the latest timestamp
10184 	 * that this packet was sent and then see
10185 	 * if:
10186 	 *  a) The packet touches are newly defined GP range.
10187 	 *  b) The time is greater than (newer) than the
10188 	 *     one we currently have. If so we update
10189 	 *     our sending end time window.
10190 	 *
10191 	 * Note we *do not* do this at send time. The reason
10192 	 * is that if you do you *may* pick up a newer timestamp
10193 	 * for a range you are not going to measure. We project
10194 	 * out how far and then sometimes modify that to be
10195 	 * smaller. If that occurs then you will have a send
10196 	 * that does not belong to the range included.
10197 	 */
10198 	if ((ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]) <=
10199 	    rack->r_ctl.rc_gp_cumack_ts)
10200 		return;
10201 	if (rack_in_gp_window(tp, rsm)) {
10202 		rack->r_ctl.rc_gp_cumack_ts = ts;
10203 		rack_log_gpset(rack, tp->gput_ack, (uint32_t)ts, rsm->r_end,
10204 			       __LINE__, from, rsm);
10205 	}
10206 }
10207 
10208 static void
10209 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)
10210 {
10211 	struct rack_sendmap *rsm;
10212 	/*
10213 	 * The ACK point is advancing to th_ack, we must drop off
10214 	 * the packets in the rack log and calculate any eligble
10215 	 * RTT's.
10216 	 */
10217 
10218 	if (sack_filter_blks_used(&rack->r_ctl.rack_sf)) {
10219 		/*
10220 		 * If we have some sack blocks in the filter
10221 		 * lets prune them out by calling sfb with no blocks.
10222 		 */
10223 		sack_filter_blks(tp, &rack->r_ctl.rack_sf, NULL, 0, th_ack);
10224 	}
10225 	if (SEQ_GT(th_ack, tp->snd_una)) {
10226 		/* Clear any app ack remembered settings */
10227 		rack->r_ctl.cleared_app_ack = 0;
10228 	}
10229 	rack->r_wanted_output = 1;
10230 	if (SEQ_GT(th_ack, tp->snd_una))
10231 		rack->r_ctl.last_cumack_advance = acktime;
10232 
10233 	/* Tend any TLP that has been marked for 1/2 the seq space (its old)  */
10234 	if ((rack->rc_last_tlp_acked_set == 1)&&
10235 	    (rack->rc_last_tlp_past_cumack == 1) &&
10236 	    (SEQ_GT(rack->r_ctl.last_tlp_acked_start, th_ack))) {
10237 		/*
10238 		 * We have reached the point where our last rack
10239 		 * tlp retransmit sequence is ahead of the cum-ack.
10240 		 * This can only happen when the cum-ack moves all
10241 		 * the way around (its been a full 2^^31+1 bytes
10242 		 * or more since we sent a retransmitted TLP). Lets
10243 		 * turn off the valid flag since its not really valid.
10244 		 *
10245 		 * Note since sack's also turn on this event we have
10246 		 * a complication, we have to wait to age it out until
10247 		 * the cum-ack is by the TLP before checking which is
10248 		 * what the next else clause does.
10249 		 */
10250 		rack_log_dsack_event(rack, 9, __LINE__,
10251 				     rack->r_ctl.last_tlp_acked_start,
10252 				     rack->r_ctl.last_tlp_acked_end);
10253 		rack->rc_last_tlp_acked_set = 0;
10254 		rack->rc_last_tlp_past_cumack = 0;
10255 	} else if ((rack->rc_last_tlp_acked_set == 1) &&
10256 		   (rack->rc_last_tlp_past_cumack == 0) &&
10257 		   (SEQ_GEQ(th_ack, rack->r_ctl.last_tlp_acked_end))) {
10258 		/*
10259 		 * It is safe to start aging TLP's out.
10260 		 */
10261 		rack->rc_last_tlp_past_cumack = 1;
10262 	}
10263 	/* We do the same for the tlp send seq as well */
10264 	if ((rack->rc_last_sent_tlp_seq_valid == 1) &&
10265 	    (rack->rc_last_sent_tlp_past_cumack == 1) &&
10266 	    (SEQ_GT(rack->r_ctl.last_sent_tlp_seq,  th_ack))) {
10267 		rack_log_dsack_event(rack, 9, __LINE__,
10268 				     rack->r_ctl.last_sent_tlp_seq,
10269 				     (rack->r_ctl.last_sent_tlp_seq +
10270 				      rack->r_ctl.last_sent_tlp_len));
10271 		rack->rc_last_sent_tlp_seq_valid = 0;
10272 		rack->rc_last_sent_tlp_past_cumack = 0;
10273 	} else if ((rack->rc_last_sent_tlp_seq_valid == 1) &&
10274 		   (rack->rc_last_sent_tlp_past_cumack == 0) &&
10275 		   (SEQ_GEQ(th_ack, rack->r_ctl.last_sent_tlp_seq))) {
10276 		/*
10277 		 * It is safe to start aging TLP's send.
10278 		 */
10279 		rack->rc_last_sent_tlp_past_cumack = 1;
10280 	}
10281 more:
10282 	rsm = tqhash_min(rack->r_ctl.tqh);
10283 	if (rsm == NULL) {
10284 		if ((th_ack - 1) == tp->iss) {
10285 			/*
10286 			 * For the SYN incoming case we will not
10287 			 * have called tcp_output for the sending of
10288 			 * the SYN, so there will be no map. All
10289 			 * other cases should probably be a panic.
10290 			 */
10291 			return;
10292 		}
10293 		if (tp->t_flags & TF_SENTFIN) {
10294 			/* if we sent a FIN we often will not have map */
10295 			return;
10296 		}
10297 #ifdef INVARIANTS
10298 		panic("No rack map tp:%p for state:%d ack:%u rack:%p snd_una:%u snd_max:%u\n",
10299 		      tp,
10300 		      tp->t_state, th_ack, rack,
10301 		      tp->snd_una, tp->snd_max);
10302 #endif
10303 		return;
10304 	}
10305 	if (SEQ_LT(th_ack, rsm->r_start)) {
10306 		/* Huh map is missing this */
10307 #ifdef INVARIANTS
10308 		printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d\n",
10309 		       rsm->r_start,
10310 		       th_ack, tp->t_state, rack->r_state);
10311 #endif
10312 		return;
10313 	}
10314 	rack_update_rtt(tp, rack, rsm, to, cts, CUM_ACKED, th_ack);
10315 
10316 	/* Now was it a retransmitted TLP? */
10317 	if ((rsm->r_flags & RACK_TLP) &&
10318 	    (rsm->r_rtr_cnt > 1)) {
10319 		/*
10320 		 * Yes, this rsm was a TLP and retransmitted, remember that
10321 		 * since if a DSACK comes back on this we don't want
10322 		 * to think of it as a reordered segment. This may
10323 		 * get updated again with possibly even other TLPs
10324 		 * in flight, but thats ok. Only when we don't send
10325 		 * a retransmitted TLP for 1/2 the sequences space
10326 		 * will it get turned off (above).
10327 		 */
10328 		if (rack->rc_last_tlp_acked_set &&
10329 		    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
10330 			/*
10331 			 * We already turned this on since the end matches,
10332 			 * the previous one was a partially ack now we
10333 			 * are getting another one (maybe all of it).
10334 			 */
10335 			rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
10336 			/*
10337 			 * Lets make sure we have all of it though.
10338 			 */
10339 			if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
10340 				rack->r_ctl.last_tlp_acked_start = rsm->r_start;
10341 				rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
10342 						     rack->r_ctl.last_tlp_acked_end);
10343 			}
10344 			if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
10345 				rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10346 				rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
10347 						     rack->r_ctl.last_tlp_acked_end);
10348 			}
10349 		} else {
10350 			rack->rc_last_tlp_past_cumack = 1;
10351 			rack->r_ctl.last_tlp_acked_start = rsm->r_start;
10352 			rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10353 			rack->rc_last_tlp_acked_set = 1;
10354 			rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
10355 		}
10356 	}
10357 	/* Now do we consume the whole thing? */
10358 	rack->r_ctl.last_tmit_time_acked = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
10359 	if (SEQ_GEQ(th_ack, rsm->r_end)) {
10360 		/* Its all consumed. */
10361 		uint32_t left;
10362 		uint8_t newly_acked;
10363 
10364 		if (rsm->r_flags & RACK_WAS_LOST) {
10365 			/*
10366 			 * This can happen when we marked it as lost
10367 			 * and yet before retransmitting we get an ack
10368 			 * which can happen due to reordering.
10369 			 */
10370 			rsm->r_flags  &= ~RACK_WAS_LOST;
10371 			KASSERT((rack->r_ctl.rc_considered_lost >= (rsm->r_end - rsm->r_start)),
10372 				("rsm:%p rack:%p rc_considered_lost goes negative", rsm,  rack));
10373 			if (rack->r_ctl.rc_considered_lost >= (rsm->r_end - rsm->r_start))
10374 				rack->r_ctl.rc_considered_lost -= rsm->r_end - rsm->r_start;
10375 			else
10376 				rack->r_ctl.rc_considered_lost = 0;
10377 		}
10378 		rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_FREE, rsm->r_end, __LINE__);
10379 		rack->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
10380 		rsm->r_rtr_bytes = 0;
10381 		/*
10382 		 * Record the time of highest cumack sent if its in our measurement
10383 		 * window and possibly bump out the end.
10384 		 */
10385 		rack_rsm_sender_update(rack, tp, rsm, 4);
10386 		tqhash_remove(rack->r_ctl.tqh, rsm, REMOVE_TYPE_CUMACK);
10387 		if (rsm->r_in_tmap) {
10388 			TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
10389 			rsm->r_in_tmap = 0;
10390 		}
10391 		newly_acked = 1;
10392 		if (rsm->r_flags & RACK_ACKED) {
10393 			/*
10394 			 * It was acked on the scoreboard -- remove
10395 			 * it from total
10396 			 */
10397 			rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
10398 			newly_acked = 0;
10399 		} else if (rsm->r_flags & RACK_SACK_PASSED) {
10400 			/*
10401 			 * There are segments ACKED on the
10402 			 * scoreboard further up. We are seeing
10403 			 * reordering.
10404 			 */
10405 			rsm->r_flags &= ~RACK_SACK_PASSED;
10406 			rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
10407 			rsm->r_flags |= RACK_ACKED;
10408 			rack->r_ctl.rc_reorder_ts = cts;
10409 			if (rack->r_ctl.rc_reorder_ts == 0)
10410 				rack->r_ctl.rc_reorder_ts = 1;
10411 			if (rack->r_ent_rec_ns) {
10412 				/*
10413 				 * We have sent no more, and we saw an sack
10414 				 * then ack arrive.
10415 				 */
10416 				rack->r_might_revert = 1;
10417 			}
10418 			rack_update_pcm_ack(rack, 1, rsm->r_start, rsm->r_end);
10419 		} else {
10420 			rack_update_pcm_ack(rack, 1, rsm->r_start, rsm->r_end);
10421 		}
10422 		if ((rsm->r_flags & RACK_TO_REXT) &&
10423 		    (tp->t_flags & TF_RCVD_TSTMP) &&
10424 		    (to->to_flags & TOF_TS) &&
10425 		    (to->to_tsecr != 0) &&
10426 		    (tp->t_flags & TF_PREVVALID)) {
10427 			/*
10428 			 * We can use the timestamp to see
10429 			 * if this retransmission was from the
10430 			 * first transmit. If so we made a mistake.
10431 			 */
10432 			tp->t_flags &= ~TF_PREVVALID;
10433 			if (to->to_tsecr == rack_ts_to_msec(rsm->r_tim_lastsent[0])) {
10434 				/* The first transmit is what this ack is for */
10435 				rack_cong_signal(tp, CC_RTO_ERR, th_ack, __LINE__);
10436 			}
10437 		}
10438 		left = th_ack - rsm->r_end;
10439 		if (rack->app_limited_needs_set && newly_acked)
10440 			rack_need_set_test(tp, rack, rsm, th_ack, __LINE__, RACK_USE_END_OR_THACK);
10441 		/* Free back to zone */
10442 		rack_free(rack, rsm);
10443 		if (left) {
10444 			goto more;
10445 		}
10446 		/* Check for reneging */
10447 		rsm = tqhash_min(rack->r_ctl.tqh);
10448 		if (rsm && (rsm->r_flags & RACK_ACKED) && (th_ack == rsm->r_start)) {
10449 			/*
10450 			 * The peer has moved snd_una up to
10451 			 * the edge of this send, i.e. one
10452 			 * that it had previously acked. The only
10453 			 * way that can be true if the peer threw
10454 			 * away data (space issues) that it had
10455 			 * previously sacked (else it would have
10456 			 * given us snd_una up to (rsm->r_end).
10457 			 * We need to undo the acked markings here.
10458 			 *
10459 			 * Note we have to look to make sure th_ack is
10460 			 * our rsm->r_start in case we get an old ack
10461 			 * where th_ack is behind snd_una.
10462 			 */
10463 			rack_peer_reneges(rack, rsm, th_ack);
10464 		}
10465 		return;
10466 	}
10467 	if (rsm->r_flags & RACK_ACKED) {
10468 		/*
10469 		 * It was acked on the scoreboard -- remove it from
10470 		 * total for the part being cum-acked.
10471 		 */
10472 		rack->r_ctl.rc_sacked -= (th_ack - rsm->r_start);
10473 	} else {
10474 		rack_update_pcm_ack(rack, 1, rsm->r_start, th_ack);
10475 	}
10476 	/* And what about the lost flag? */
10477 	if (rsm->r_flags & RACK_WAS_LOST) {
10478 		/*
10479 		 * This can happen when we marked it as lost
10480 		 * and yet before retransmitting we get an ack
10481 		 * which can happen due to reordering. In this
10482 		 * case its only a partial ack of the send.
10483 		 */
10484 		KASSERT((rack->r_ctl.rc_considered_lost >= (th_ack - rsm->r_start)),
10485 			("rsm:%p rack:%p rc_considered_lost goes negative th_ack:%u", rsm,  rack, th_ack));
10486 		if (rack->r_ctl.rc_considered_lost >= (th_ack - rsm->r_start))
10487 			rack->r_ctl.rc_considered_lost -= th_ack - rsm->r_start;
10488 		else
10489 			rack->r_ctl.rc_considered_lost = 0;
10490 	}
10491 	/*
10492 	 * Clear the dup ack count for
10493 	 * the piece that remains.
10494 	 */
10495 	rsm->r_dupack = 0;
10496 	rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
10497 	if (rsm->r_rtr_bytes) {
10498 		/*
10499 		 * It was retransmitted adjust the
10500 		 * sack holes for what was acked.
10501 		 */
10502 		int ack_am;
10503 
10504 		ack_am = (th_ack - rsm->r_start);
10505 		if (ack_am >= rsm->r_rtr_bytes) {
10506 			rack->r_ctl.rc_holes_rxt -= ack_am;
10507 			rsm->r_rtr_bytes -= ack_am;
10508 		}
10509 	}
10510 	/*
10511 	 * Update where the piece starts and record
10512 	 * the time of send of highest cumack sent if
10513 	 * its in our GP range.
10514 	 */
10515 	rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_TRIM_HEAD, th_ack, __LINE__);
10516 	/* Now we need to move our offset forward too */
10517 	if (rsm->m &&
10518 	    ((rsm->orig_m_len != rsm->m->m_len) ||
10519 	     (M_TRAILINGROOM(rsm->m) != rsm->orig_t_space))) {
10520 		/* Fix up the orig_m_len and possibly the mbuf offset */
10521 		rack_adjust_orig_mlen(rsm);
10522 	}
10523 	rsm->soff += (th_ack - rsm->r_start);
10524 	rack_rsm_sender_update(rack, tp, rsm, 5);
10525 	/* The trim will move th_ack into r_start for us */
10526 	tqhash_trim(rack->r_ctl.tqh, th_ack);
10527 	/* Now do we need to move the mbuf fwd too? */
10528 	{
10529 		struct mbuf *m;
10530 		uint32_t soff;
10531 
10532 		m = rsm->m;
10533 		soff = rsm->soff;
10534 		if (m) {
10535 			while (soff >= m->m_len) {
10536 				soff -= m->m_len;
10537 				KASSERT((m->m_next != NULL),
10538 					(" rsm:%p  off:%u soff:%u m:%p",
10539 					 rsm, rsm->soff, soff, m));
10540 				m = m->m_next;
10541 				if (m == NULL) {
10542 					/*
10543 					 * This is a fall-back that prevents a panic. In reality
10544 					 * we should be able to walk the mbuf's and find our place.
10545 					 * At this point snd_una has not been updated with the sbcut() yet
10546 					 * but tqhash_trim did update rsm->r_start so the offset calcuation
10547 					 * should work fine. This is undesirable since we will take cache
10548 					 * hits to access the socket buffer. And even more puzzling is that
10549 					 * it happens occasionally. It should not :(
10550 					 */
10551 					m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd,
10552 						      (rsm->r_start - tp->snd_una),
10553 						      &soff);
10554 					break;
10555 				}
10556 			}
10557 			/*
10558 			 * Now save in our updated values.
10559 			 */
10560 			rsm->m = m;
10561 			rsm->soff = soff;
10562 			rsm->orig_m_len = rsm->m->m_len;
10563 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
10564 		}
10565 	}
10566 	if (rack->app_limited_needs_set &&
10567 	    SEQ_GEQ(th_ack, tp->gput_seq))
10568 		rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_BEG);
10569 }
10570 
10571 static void
10572 rack_handle_might_revert(struct tcpcb *tp, struct tcp_rack *rack)
10573 {
10574 	struct rack_sendmap *rsm;
10575 	int sack_pass_fnd = 0;
10576 
10577 	if (rack->r_might_revert) {
10578 		/*
10579 		 * Ok we have reordering, have not sent anything, we
10580 		 * might want to revert the congestion state if nothing
10581 		 * further has SACK_PASSED on it. Lets check.
10582 		 *
10583 		 * We also get here when we have DSACKs come in for
10584 		 * all the data that we FR'd. Note that a rxt or tlp
10585 		 * timer clears this from happening.
10586 		 */
10587 
10588 		TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
10589 			if (rsm->r_flags & RACK_SACK_PASSED) {
10590 				sack_pass_fnd = 1;
10591 				break;
10592 			}
10593 		}
10594 		if (sack_pass_fnd == 0) {
10595 			/*
10596 			 * We went into recovery
10597 			 * incorrectly due to reordering!
10598 			 */
10599 			int orig_cwnd;
10600 
10601 			rack->r_ent_rec_ns = 0;
10602 			orig_cwnd = tp->snd_cwnd;
10603 			tp->snd_ssthresh = rack->r_ctl.rc_ssthresh_at_erec;
10604 			tp->snd_recover = tp->snd_una;
10605 			rack_log_to_prr(rack, 14, orig_cwnd, __LINE__);
10606 			if (IN_RECOVERY(tp->t_flags)) {
10607 				rack_exit_recovery(tp, rack, 3);
10608 				if ((rack->rto_from_rec == 1) && (rack_ssthresh_rest_rto_rec != 0) ){
10609 					/*
10610 					 * We were in recovery, had an RTO
10611 					 * and then re-entered recovery (more sack's arrived)
10612 					 * and we have properly recorded the old ssthresh from
10613 					 * the first recovery. We want to be able to slow-start
10614 					 * back to this level. The ssthresh from the timeout
10615 					 * and then back into recovery will end up most likely
10616 					 * to be min(cwnd=1mss, 2mss). Which makes it basically
10617 					 * so we get no slow-start after our RTO.
10618 					 */
10619 					rack->rto_from_rec = 0;
10620 					if (rack->r_ctl.rto_ssthresh > tp->snd_ssthresh)
10621 						tp->snd_ssthresh = rack->r_ctl.rto_ssthresh;
10622 				}
10623 			}
10624 		}
10625 		rack->r_might_revert = 0;
10626 	}
10627 }
10628 
10629 
10630 static int
10631 rack_note_dsack(struct tcp_rack *rack, tcp_seq start, tcp_seq end)
10632 {
10633 
10634 	uint32_t am, l_end;
10635 	int was_tlp = 0;
10636 
10637 	if (SEQ_GT(end, start))
10638 		am = end - start;
10639 	else
10640 		am = 0;
10641 	if ((rack->rc_last_tlp_acked_set ) &&
10642 	    (SEQ_GEQ(start, rack->r_ctl.last_tlp_acked_start)) &&
10643 	    (SEQ_LEQ(end, rack->r_ctl.last_tlp_acked_end))) {
10644 		/*
10645 		 * The DSACK is because of a TLP which we don't
10646 		 * do anything with the reordering window over since
10647 		 * it was not reordering that caused the DSACK but
10648 		 * our previous retransmit TLP.
10649 		 */
10650 		rack_log_dsack_event(rack, 7, __LINE__, start, end);
10651 		was_tlp = 1;
10652 		goto skip_dsack_round;
10653 	}
10654 	if (rack->rc_last_sent_tlp_seq_valid) {
10655 		l_end = rack->r_ctl.last_sent_tlp_seq + rack->r_ctl.last_sent_tlp_len;
10656 		if (SEQ_GEQ(start, rack->r_ctl.last_sent_tlp_seq) &&
10657 		    (SEQ_LEQ(end, l_end))) {
10658 			/*
10659 			 * This dsack is from the last sent TLP, ignore it
10660 			 * for reordering purposes.
10661 			 */
10662 			rack_log_dsack_event(rack, 7, __LINE__, start, end);
10663 			was_tlp = 1;
10664 			goto skip_dsack_round;
10665 		}
10666 	}
10667 	if (rack->rc_dsack_round_seen == 0) {
10668 		rack->rc_dsack_round_seen = 1;
10669 		rack->r_ctl.dsack_round_end = rack->rc_tp->snd_max;
10670 		rack->r_ctl.num_dsack++;
10671 		rack->r_ctl.dsack_persist = 16;	/* 16 is from the standard */
10672 		rack_log_dsack_event(rack, 2, __LINE__, 0, 0);
10673 	}
10674 skip_dsack_round:
10675 	/*
10676 	 * We keep track of how many DSACK blocks we get
10677 	 * after a recovery incident.
10678 	 */
10679 	rack->r_ctl.dsack_byte_cnt += am;
10680 	if (!IN_FASTRECOVERY(rack->rc_tp->t_flags) &&
10681 	    rack->r_ctl.retran_during_recovery &&
10682 	    (rack->r_ctl.dsack_byte_cnt >= rack->r_ctl.retran_during_recovery)) {
10683 		/*
10684 		 * False recovery most likely culprit is reordering. If
10685 		 * nothing else is missing we need to revert.
10686 		 */
10687 		rack->r_might_revert = 1;
10688 		rack_handle_might_revert(rack->rc_tp, rack);
10689 		rack->r_might_revert = 0;
10690 		rack->r_ctl.retran_during_recovery = 0;
10691 		rack->r_ctl.dsack_byte_cnt = 0;
10692 	}
10693 	return (was_tlp);
10694 }
10695 
10696 static uint32_t
10697 do_rack_compute_pipe(struct tcpcb *tp, struct tcp_rack *rack, uint32_t snd_una)
10698 {
10699 	return (((tp->snd_max - snd_una) -
10700 		 (rack->r_ctl.rc_sacked + rack->r_ctl.rc_considered_lost)) + rack->r_ctl.rc_holes_rxt);
10701 }
10702 
10703 static int32_t
10704 rack_compute_pipe(struct tcpcb *tp)
10705 {
10706 	return ((int32_t)do_rack_compute_pipe(tp,
10707 					      (struct tcp_rack *)tp->t_fb_ptr,
10708 					      tp->snd_una));
10709 }
10710 
10711 static void
10712 rack_update_prr(struct tcpcb *tp, struct tcp_rack *rack, uint32_t changed, tcp_seq th_ack)
10713 {
10714 	/* Deal with changed and PRR here (in recovery only) */
10715 	uint32_t pipe, snd_una;
10716 
10717 	rack->r_ctl.rc_prr_delivered += changed;
10718 
10719 	if (sbavail(&rack->rc_inp->inp_socket->so_snd) <= (tp->snd_max - tp->snd_una)) {
10720 		/*
10721 		 * It is all outstanding, we are application limited
10722 		 * and thus we don't need more room to send anything.
10723 		 * Note we use tp->snd_una here and not th_ack because
10724 		 * the data as yet not been cut from the sb.
10725 		 */
10726 		rack->r_ctl.rc_prr_sndcnt = 0;
10727 		return;
10728 	}
10729 	/* Compute prr_sndcnt */
10730 	if (SEQ_GT(tp->snd_una, th_ack)) {
10731 		snd_una = tp->snd_una;
10732 	} else {
10733 		snd_una = th_ack;
10734 	}
10735 	pipe = do_rack_compute_pipe(tp, rack, snd_una);
10736 	if (pipe > tp->snd_ssthresh) {
10737 		long sndcnt;
10738 
10739 		sndcnt = rack->r_ctl.rc_prr_delivered * tp->snd_ssthresh;
10740 		if (rack->r_ctl.rc_prr_recovery_fs > 0)
10741 			sndcnt /= (long)rack->r_ctl.rc_prr_recovery_fs;
10742 		else {
10743 			rack->r_ctl.rc_prr_sndcnt = 0;
10744 			rack_log_to_prr(rack, 9, 0, __LINE__);
10745 			sndcnt = 0;
10746 		}
10747 		sndcnt++;
10748 		if (sndcnt > (long)rack->r_ctl.rc_prr_out)
10749 			sndcnt -= rack->r_ctl.rc_prr_out;
10750 		else
10751 			sndcnt = 0;
10752 		rack->r_ctl.rc_prr_sndcnt = sndcnt;
10753 		rack_log_to_prr(rack, 10, 0, __LINE__);
10754 	} else {
10755 		uint32_t limit;
10756 
10757 		if (rack->r_ctl.rc_prr_delivered > rack->r_ctl.rc_prr_out)
10758 			limit = (rack->r_ctl.rc_prr_delivered - rack->r_ctl.rc_prr_out);
10759 		else
10760 			limit = 0;
10761 		if (changed > limit)
10762 			limit = changed;
10763 		limit += ctf_fixed_maxseg(tp);
10764 		if (tp->snd_ssthresh > pipe) {
10765 			rack->r_ctl.rc_prr_sndcnt = min((tp->snd_ssthresh - pipe), limit);
10766 			rack_log_to_prr(rack, 11, 0, __LINE__);
10767 		} else {
10768 			rack->r_ctl.rc_prr_sndcnt = min(0, limit);
10769 			rack_log_to_prr(rack, 12, 0, __LINE__);
10770 		}
10771 	}
10772 }
10773 
10774 static void
10775 rack_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th, int entered_recovery, int dup_ack_struck,
10776 	     int *dsack_seen, int *sacks_seen)
10777 {
10778 	uint32_t changed;
10779 	struct tcp_rack *rack;
10780 	struct rack_sendmap *rsm;
10781 	struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1];
10782 	register uint32_t th_ack;
10783 	int32_t i, j, k, num_sack_blks = 0;
10784 	uint32_t cts, acked, ack_point;
10785 	int loop_start = 0;
10786 	uint32_t tsused;
10787 	uint32_t segsiz;
10788 
10789 
10790 	INP_WLOCK_ASSERT(tptoinpcb(tp));
10791 	if (tcp_get_flags(th) & TH_RST) {
10792 		/* We don't log resets */
10793 		return;
10794 	}
10795 	rack = (struct tcp_rack *)tp->t_fb_ptr;
10796 	cts = tcp_get_usecs(NULL);
10797 	rsm = tqhash_min(rack->r_ctl.tqh);
10798 	changed = 0;
10799 	th_ack = th->th_ack;
10800 	segsiz = ctf_fixed_maxseg(rack->rc_tp);
10801 	if (BYTES_THIS_ACK(tp, th) >=  segsiz) {
10802 		/*
10803 		 * You only get credit for
10804 		 * MSS and greater (and you get extra
10805 		 * credit for larger cum-ack moves).
10806 		 */
10807 		int ac;
10808 
10809 		ac = BYTES_THIS_ACK(tp, th) / ctf_fixed_maxseg(rack->rc_tp);
10810 		counter_u64_add(rack_ack_total, ac);
10811 	}
10812 	if (SEQ_GT(th_ack, tp->snd_una)) {
10813 		rack_log_progress_event(rack, tp, ticks, PROGRESS_UPDATE, __LINE__);
10814 		tp->t_acktime = ticks;
10815 	}
10816 	if (rsm && SEQ_GT(th_ack, rsm->r_start))
10817 		changed = th_ack - rsm->r_start;
10818 	if (changed) {
10819 		rack_process_to_cumack(tp, rack, th_ack, cts, to,
10820 				       tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time));
10821 	}
10822 	if ((to->to_flags & TOF_SACK) == 0) {
10823 		/* We are done nothing left and no sack. */
10824 		rack_handle_might_revert(tp, rack);
10825 		/*
10826 		 * For cases where we struck a dup-ack
10827 		 * with no SACK, add to the changes so
10828 		 * PRR will work right.
10829 		 */
10830 		if (dup_ack_struck && (changed == 0)) {
10831 			changed += ctf_fixed_maxseg(rack->rc_tp);
10832 		}
10833 		goto out;
10834 	}
10835 	/* Sack block processing */
10836 	if (SEQ_GT(th_ack, tp->snd_una))
10837 		ack_point = th_ack;
10838 	else
10839 		ack_point = tp->snd_una;
10840 	for (i = 0; i < to->to_nsacks; i++) {
10841 		bcopy((to->to_sacks + i * TCPOLEN_SACK),
10842 		      &sack, sizeof(sack));
10843 		sack.start = ntohl(sack.start);
10844 		sack.end = ntohl(sack.end);
10845 		if (SEQ_GT(sack.end, sack.start) &&
10846 		    SEQ_GT(sack.start, ack_point) &&
10847 		    SEQ_LT(sack.start, tp->snd_max) &&
10848 		    SEQ_GT(sack.end, ack_point) &&
10849 		    SEQ_LEQ(sack.end, tp->snd_max)) {
10850 			sack_blocks[num_sack_blks] = sack;
10851 			num_sack_blks++;
10852 		} else if (SEQ_LEQ(sack.start, th_ack) &&
10853 			   SEQ_LEQ(sack.end, th_ack)) {
10854 			int was_tlp;
10855 
10856 			if (dsack_seen != NULL)
10857 				*dsack_seen = 1;
10858 			was_tlp = rack_note_dsack(rack, sack.start, sack.end);
10859 			/*
10860 			 * Its a D-SACK block.
10861 			 */
10862 			tcp_record_dsack(tp, sack.start, sack.end, was_tlp);
10863 		}
10864 	}
10865 	if (rack->rc_dsack_round_seen) {
10866 		/* Is the dsack roound over? */
10867 		if (SEQ_GEQ(th_ack, rack->r_ctl.dsack_round_end)) {
10868 			/* Yes it is */
10869 			rack->rc_dsack_round_seen = 0;
10870 			rack_log_dsack_event(rack, 3, __LINE__, 0, 0);
10871 		}
10872 	}
10873 	/*
10874 	 * Sort the SACK blocks so we can update the rack scoreboard with
10875 	 * just one pass.
10876 	 */
10877 	num_sack_blks = sack_filter_blks(tp, &rack->r_ctl.rack_sf, sack_blocks,
10878 					 num_sack_blks, th->th_ack);
10879 	ctf_log_sack_filter(rack->rc_tp, num_sack_blks, sack_blocks);
10880 	if (sacks_seen != NULL)
10881 		*sacks_seen = num_sack_blks;
10882 	if (num_sack_blks == 0) {
10883 		/* Nothing to sack, but we need to update counts */
10884 		goto out_with_totals;
10885 	}
10886 	/* Its a sack of some sort */
10887 	if (num_sack_blks < 2) {
10888 		/* Only one, we don't need to sort */
10889 		goto do_sack_work;
10890 	}
10891 	/* Sort the sacks */
10892 	for (i = 0; i < num_sack_blks; i++) {
10893 		for (j = i + 1; j < num_sack_blks; j++) {
10894 			if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
10895 				sack = sack_blocks[i];
10896 				sack_blocks[i] = sack_blocks[j];
10897 				sack_blocks[j] = sack;
10898 			}
10899 		}
10900 	}
10901 	/*
10902 	 * Now are any of the sack block ends the same (yes some
10903 	 * implementations send these)?
10904 	 */
10905 again:
10906 	if (num_sack_blks == 0)
10907 		goto out_with_totals;
10908 	if (num_sack_blks > 1) {
10909 		for (i = 0; i < num_sack_blks; i++) {
10910 			for (j = i + 1; j < num_sack_blks; j++) {
10911 				if (sack_blocks[i].end == sack_blocks[j].end) {
10912 					/*
10913 					 * Ok these two have the same end we
10914 					 * want the smallest end and then
10915 					 * throw away the larger and start
10916 					 * again.
10917 					 */
10918 					if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) {
10919 						/*
10920 						 * The second block covers
10921 						 * more area use that
10922 						 */
10923 						sack_blocks[i].start = sack_blocks[j].start;
10924 					}
10925 					/*
10926 					 * Now collapse out the dup-sack and
10927 					 * lower the count
10928 					 */
10929 					for (k = (j + 1); k < num_sack_blks; k++) {
10930 						sack_blocks[j].start = sack_blocks[k].start;
10931 						sack_blocks[j].end = sack_blocks[k].end;
10932 						j++;
10933 					}
10934 					num_sack_blks--;
10935 					goto again;
10936 				}
10937 			}
10938 		}
10939 	}
10940 do_sack_work:
10941 	/*
10942 	 * First lets look to see if
10943 	 * we have retransmitted and
10944 	 * can use the transmit next?
10945 	 */
10946 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
10947 	if (rsm &&
10948 	    SEQ_GT(sack_blocks[0].end, rsm->r_start) &&
10949 	    SEQ_LT(sack_blocks[0].start, rsm->r_end)) {
10950 		/*
10951 		 * We probably did the FR and the next
10952 		 * SACK in continues as we would expect.
10953 		 */
10954 		acked = rack_proc_sack_blk(tp, rack, &sack_blocks[0], to, &rsm, cts, segsiz);
10955 		if (acked) {
10956 			rack->r_wanted_output = 1;
10957 			changed += acked;
10958 		}
10959 		if (num_sack_blks == 1) {
10960 			/*
10961 			 * This is what we would expect from
10962 			 * a normal implementation to happen
10963 			 * after we have retransmitted the FR,
10964 			 * i.e the sack-filter pushes down
10965 			 * to 1 block and the next to be retransmitted
10966 			 * is the sequence in the sack block (has more
10967 			 * are acked). Count this as ACK'd data to boost
10968 			 * up the chances of recovering any false positives.
10969 			 */
10970 			counter_u64_add(rack_ack_total, (acked / ctf_fixed_maxseg(rack->rc_tp)));
10971 			counter_u64_add(rack_express_sack, 1);
10972 			goto out_with_totals;
10973 		} else {
10974 			/*
10975 			 * Start the loop through the
10976 			 * rest of blocks, past the first block.
10977 			 */
10978 			loop_start = 1;
10979 		}
10980 	}
10981 	counter_u64_add(rack_sack_total, 1);
10982 	rsm = rack->r_ctl.rc_sacklast;
10983 	for (i = loop_start; i < num_sack_blks; i++) {
10984 		acked = rack_proc_sack_blk(tp, rack, &sack_blocks[i], to, &rsm, cts,  segsiz);
10985 		if (acked) {
10986 			rack->r_wanted_output = 1;
10987 			changed += acked;
10988 		}
10989 	}
10990 out_with_totals:
10991 	if (num_sack_blks > 1) {
10992 		/*
10993 		 * You get an extra stroke if
10994 		 * you have more than one sack-blk, this
10995 		 * could be where we are skipping forward
10996 		 * and the sack-filter is still working, or
10997 		 * it could be an attacker constantly
10998 		 * moving us.
10999 		 */
11000 		counter_u64_add(rack_move_some, 1);
11001 	}
11002 out:
11003 	if (changed) {
11004 		/* Something changed cancel the rack timer */
11005 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
11006 	}
11007 	tsused = tcp_get_usecs(NULL);
11008 	rsm = tcp_rack_output(tp, rack, tsused);
11009 	if ((!IN_FASTRECOVERY(tp->t_flags)) &&
11010 	    rsm &&
11011 	    ((rsm->r_flags & RACK_MUST_RXT) == 0)) {
11012 		/* Enter recovery */
11013 		entered_recovery = 1;
11014 		rack_cong_signal(tp, CC_NDUPACK, th_ack, __LINE__);
11015 		/*
11016 		 * When we enter recovery we need to assure we send
11017 		 * one packet.
11018 		 */
11019 		if (rack->rack_no_prr == 0) {
11020 			rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
11021 			rack_log_to_prr(rack, 8, 0, __LINE__);
11022 		}
11023 		rack->r_timer_override = 1;
11024 		rack->r_early = 0;
11025 		rack->r_ctl.rc_agg_early = 0;
11026 	} else if (IN_FASTRECOVERY(tp->t_flags) &&
11027 		   rsm &&
11028 		   (rack->r_rr_config == 3)) {
11029 		/*
11030 		 * Assure we can output and we get no
11031 		 * remembered pace time except the retransmit.
11032 		 */
11033 		rack->r_timer_override = 1;
11034 		rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
11035 		rack->r_ctl.rc_resend = rsm;
11036 	}
11037 	if (IN_FASTRECOVERY(tp->t_flags) &&
11038 	    (rack->rack_no_prr == 0) &&
11039 	    (entered_recovery == 0)) {
11040 		rack_update_prr(tp, rack, changed, th_ack);
11041 		if ((rsm && (rack->r_ctl.rc_prr_sndcnt >= ctf_fixed_maxseg(tp)) &&
11042 		     ((tcp_in_hpts(rack->rc_tp) == 0) &&
11043 		      ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)))) {
11044 			/*
11045 			 * If you are pacing output you don't want
11046 			 * to override.
11047 			 */
11048 			rack->r_early = 0;
11049 			rack->r_ctl.rc_agg_early = 0;
11050 			rack->r_timer_override = 1;
11051 		}
11052 	}
11053 }
11054 
11055 static void
11056 rack_strike_dupack(struct tcp_rack *rack, tcp_seq th_ack)
11057 {
11058 	struct rack_sendmap *rsm;
11059 
11060 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
11061 	while (rsm) {
11062 		/*
11063 		 * We need to skip anything already set
11064 		 * to be retransmitted.
11065 		 */
11066 		if ((rsm->r_dupack >= DUP_ACK_THRESHOLD)  ||
11067 		    (rsm->r_flags & RACK_MUST_RXT)) {
11068 			rsm = TAILQ_NEXT(rsm, r_tnext);
11069 			continue;
11070 		}
11071 		break;
11072 	}
11073 	if (rsm && (rsm->r_dupack < 0xff)) {
11074 		rsm->r_dupack++;
11075 		if (rsm->r_dupack >= DUP_ACK_THRESHOLD) {
11076 			struct timeval tv;
11077 			uint32_t cts;
11078 			/*
11079 			 * Here we see if we need to retransmit. For
11080 			 * a SACK type connection if enough time has passed
11081 			 * we will get a return of the rsm. For a non-sack
11082 			 * connection we will get the rsm returned if the
11083 			 * dupack value is 3 or more.
11084 			 */
11085 			cts = tcp_get_usecs(&tv);
11086 			rack->r_ctl.rc_resend = tcp_rack_output(rack->rc_tp, rack, cts);
11087 			if (rack->r_ctl.rc_resend != NULL) {
11088 				if (!IN_FASTRECOVERY(rack->rc_tp->t_flags)) {
11089 					rack_cong_signal(rack->rc_tp, CC_NDUPACK,
11090 							 th_ack,  __LINE__);
11091 				}
11092 				rack->r_wanted_output = 1;
11093 				rack->r_timer_override = 1;
11094 				rack_log_retran_reason(rack, rsm, __LINE__, 1, 3);
11095 			}
11096 		} else {
11097 			rack_log_retran_reason(rack, rsm, __LINE__, 0, 3);
11098 		}
11099 	}
11100 }
11101 
11102 static void
11103 rack_check_bottom_drag(struct tcpcb *tp,
11104 		       struct tcp_rack *rack,
11105 		       struct socket *so)
11106 {
11107 	/*
11108 	 * So what is dragging bottom?
11109 	 *
11110 	 * Dragging bottom means you were under pacing and had a
11111 	 * delay in processing inbound acks waiting on our pacing
11112 	 * timer to expire. While you were waiting all of the acknowledgments
11113 	 * for the packets you sent have arrived. This means we are pacing
11114 	 * way underneath the bottleneck to the point where our Goodput
11115 	 * measurements stop working, since they require more than one
11116 	 * ack (usually at least 8 packets worth with multiple acks so we can
11117 	 * gauge the inter-ack times). If that occurs we have a real problem
11118 	 * since we are stuck in a hole that we can't get out of without
11119 	 * something speeding us up.
11120 	 *
11121 	 * We also check to see if we are widdling down to just one segment
11122 	 * outstanding. If this occurs and we have room to send in our cwnd/rwnd
11123 	 * then we are adding the delayed ack interval into our measurments and
11124 	 * we need to speed up slightly.
11125 	 */
11126 	uint32_t segsiz, minseg;
11127 
11128 	segsiz = ctf_fixed_maxseg(tp);
11129 	minseg = segsiz;
11130 	if (tp->snd_max == tp->snd_una) {
11131 		/*
11132 		 * We are doing dynamic pacing and we are way
11133 		 * under. Basically everything got acked while
11134 		 * we were still waiting on the pacer to expire.
11135 		 *
11136 		 * This means we need to boost the b/w in
11137 		 * addition to any earlier boosting of
11138 		 * the multiplier.
11139 		 */
11140 		uint64_t lt_bw;
11141 
11142 		tcp_trace_point(rack->rc_tp, TCP_TP_PACED_BOTTOM);
11143 		lt_bw = rack_get_lt_bw(rack);
11144 		rack->rc_dragged_bottom = 1;
11145 		rack_validate_multipliers_at_or_above100(rack);
11146 		if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_VALID) &&
11147 		    (rack->dis_lt_bw == 0) &&
11148 		    (rack->use_lesser_lt_bw == 0) &&
11149 		    (lt_bw > 0)) {
11150 			/*
11151 			 * Lets use the long-term b/w we have
11152 			 * been getting as a base.
11153 			 */
11154 			if (rack->rc_gp_filled == 0) {
11155 				if (lt_bw > ONE_POINT_TWO_MEG) {
11156 					/*
11157 					 * If we have no measurement
11158 					 * don't let us set in more than
11159 					 * 1.2Mbps. If we are still too
11160 					 * low after pacing with this we
11161 					 * will hopefully have a max b/w
11162 					 * available to sanity check things.
11163 					 */
11164 					lt_bw = ONE_POINT_TWO_MEG;
11165 				}
11166 				rack->r_ctl.rc_rtt_diff = 0;
11167 				rack->r_ctl.gp_bw = lt_bw;
11168 				rack->rc_gp_filled = 1;
11169 				if (rack->r_ctl.num_measurements < RACK_REQ_AVG)
11170 					rack->r_ctl.num_measurements = RACK_REQ_AVG;
11171 				rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
11172 			} else if (lt_bw > rack->r_ctl.gp_bw) {
11173 				rack->r_ctl.rc_rtt_diff = 0;
11174 				if (rack->r_ctl.num_measurements < RACK_REQ_AVG)
11175 					rack->r_ctl.num_measurements = RACK_REQ_AVG;
11176 				rack->r_ctl.gp_bw = lt_bw;
11177 				rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
11178 			} else
11179 				rack_increase_bw_mul(rack, -1, 0, 0, 1);
11180 			if ((rack->gp_ready == 0) &&
11181 			    (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) {
11182 				/* We have enough measurements now */
11183 				rack->gp_ready = 1;
11184 				if (rack->dgp_on ||
11185 				    rack->rack_hibeta)
11186 					rack_set_cc_pacing(rack);
11187 				if (rack->defer_options)
11188 					rack_apply_deferred_options(rack);
11189 			}
11190 		} else {
11191 			/*
11192 			 * zero rtt possibly?, settle for just an old increase.
11193 			 */
11194 			rack_increase_bw_mul(rack, -1, 0, 0, 1);
11195 		}
11196 	} else if ((IN_FASTRECOVERY(tp->t_flags) == 0) &&
11197 		   (sbavail(&so->so_snd) > max((segsiz * (4 + rack_req_segs)),
11198 					       minseg)) &&
11199 		   (rack->r_ctl.cwnd_to_use > max((segsiz * (rack_req_segs + 2)), minseg)) &&
11200 		   (tp->snd_wnd > max((segsiz * (rack_req_segs + 2)), minseg)) &&
11201 		   (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) <=
11202 		    (segsiz * rack_req_segs))) {
11203 		/*
11204 		 * We are doing dynamic GP pacing and
11205 		 * we have everything except 1MSS or less
11206 		 * bytes left out. We are still pacing away.
11207 		 * And there is data that could be sent, This
11208 		 * means we are inserting delayed ack time in
11209 		 * our measurements because we are pacing too slow.
11210 		 */
11211 		rack_validate_multipliers_at_or_above100(rack);
11212 		rack->rc_dragged_bottom = 1;
11213 		rack_increase_bw_mul(rack, -1, 0, 0, 1);
11214 	}
11215 }
11216 
11217 #ifdef TCP_REQUEST_TRK
11218 static void
11219 rack_log_hybrid(struct tcp_rack *rack, uint32_t seq,
11220 		struct tcp_sendfile_track *cur, uint8_t mod, int line, int err)
11221 {
11222 	int do_log;
11223 
11224 	do_log = tcp_bblogging_on(rack->rc_tp);
11225 	if (do_log == 0) {
11226 		if ((do_log = tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING) )== 0)
11227 			return;
11228 		/* We only allow the three below with point logging on */
11229 		if ((mod != HYBRID_LOG_RULES_APP) &&
11230 		    (mod != HYBRID_LOG_RULES_SET) &&
11231 		    (mod != HYBRID_LOG_REQ_COMP))
11232 			return;
11233 
11234 	}
11235 	if (do_log) {
11236 		union tcp_log_stackspecific log;
11237 		struct timeval tv;
11238 
11239 		/* Convert our ms to a microsecond */
11240 		memset(&log, 0, sizeof(log));
11241 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
11242 		log.u_bbr.flex1 = seq;
11243 		log.u_bbr.cwnd_gain = line;
11244 		if (cur != NULL) {
11245 			uint64_t off;
11246 
11247 			log.u_bbr.flex2 = cur->start_seq;
11248 			log.u_bbr.flex3 = cur->end_seq;
11249 			log.u_bbr.flex4 = (uint32_t)((cur->localtime >> 32) & 0x00000000ffffffff);
11250 			log.u_bbr.flex5 = (uint32_t)(cur->localtime & 0x00000000ffffffff);
11251 			log.u_bbr.flex6 = cur->flags;
11252 			log.u_bbr.pkts_out = cur->hybrid_flags;
11253 			log.u_bbr.rttProp = cur->timestamp;
11254 			log.u_bbr.cur_del_rate = cur->cspr;
11255 			log.u_bbr.bw_inuse = cur->start;
11256 			log.u_bbr.applimited = (uint32_t)(cur->end & 0x00000000ffffffff);
11257 			log.u_bbr.delivered = (uint32_t)((cur->end >> 32) & 0x00000000ffffffff) ;
11258 			log.u_bbr.epoch = (uint32_t)(cur->deadline & 0x00000000ffffffff);
11259 			log.u_bbr.lt_epoch = (uint32_t)((cur->deadline >> 32) & 0x00000000ffffffff) ;
11260 			log.u_bbr.inhpts = 1;
11261 #ifdef TCP_REQUEST_TRK
11262 			off = (uint64_t)(cur) - (uint64_t)(&rack->rc_tp->t_tcpreq_info[0]);
11263 			log.u_bbr.use_lt_bw = (uint8_t)(off / sizeof(struct tcp_sendfile_track));
11264 #endif
11265 		} else {
11266 			log.u_bbr.flex2 = err;
11267 		}
11268 		/*
11269 		 * Fill in flex7 to be CHD (catchup|hybrid|DGP)
11270 		 */
11271 		log.u_bbr.flex7 = rack->rc_catch_up;
11272 		log.u_bbr.flex7 <<= 1;
11273 		log.u_bbr.flex7 |= rack->rc_hybrid_mode;
11274 		log.u_bbr.flex7 <<= 1;
11275 		log.u_bbr.flex7 |= rack->dgp_on;
11276 		/*
11277 		 * Compose bbr_state to be a bit wise 0000ADHF
11278 		 * where A is the always_pace flag
11279 		 * where D is the dgp_on flag
11280 		 * where H is the hybrid_mode on flag
11281 		 * where F is the use_fixed_rate flag.
11282 		 */
11283 		log.u_bbr.bbr_state = rack->rc_always_pace;
11284 		log.u_bbr.bbr_state <<= 1;
11285 		log.u_bbr.bbr_state |= rack->dgp_on;
11286 		log.u_bbr.bbr_state <<= 1;
11287 		log.u_bbr.bbr_state |= rack->rc_hybrid_mode;
11288 		log.u_bbr.bbr_state <<= 1;
11289 		log.u_bbr.bbr_state |= rack->use_fixed_rate;
11290 		log.u_bbr.flex8 = mod;
11291 		log.u_bbr.delRate = rack->r_ctl.bw_rate_cap;
11292 		log.u_bbr.bbr_substate = rack->r_ctl.client_suggested_maxseg;
11293 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
11294 		log.u_bbr.pkt_epoch = rack->rc_tp->tcp_hybrid_start;
11295 		log.u_bbr.lost = rack->rc_tp->tcp_hybrid_error;
11296 		log.u_bbr.pacing_gain = (uint16_t)rack->rc_tp->tcp_hybrid_stop;
11297 		tcp_log_event(rack->rc_tp, NULL,
11298 		    &rack->rc_inp->inp_socket->so_rcv,
11299 		    &rack->rc_inp->inp_socket->so_snd,
11300 		    TCP_HYBRID_PACING_LOG, 0,
11301 	            0, &log, false, NULL, __func__, __LINE__, &tv);
11302 	}
11303 }
11304 #endif
11305 
11306 #ifdef TCP_REQUEST_TRK
11307 static void
11308 rack_set_dgp_hybrid_mode(struct tcp_rack *rack, tcp_seq seq, uint32_t len, uint64_t cts)
11309 {
11310 	struct tcp_sendfile_track *rc_cur, *orig_ent;
11311 	struct tcpcb *tp;
11312 	int err = 0;
11313 
11314 	orig_ent = rack->r_ctl.rc_last_sft;
11315 	rc_cur = tcp_req_find_req_for_seq(rack->rc_tp, seq);
11316 	if (rc_cur == NULL) {
11317 		/* If not in the beginning what about the end piece */
11318 		if (rack->rc_hybrid_mode)
11319 			rack_log_hybrid(rack, seq, NULL, HYBRID_LOG_NO_RANGE, __LINE__, err);
11320 		rc_cur = tcp_req_find_req_for_seq(rack->rc_tp, (seq + len - 1));
11321 	} else {
11322 		err = 12345;
11323 	}
11324 	/* If we find no parameters we are in straight DGP mode */
11325 	if(rc_cur == NULL) {
11326 		/* None found for this seq, just DGP for now */
11327 		if (rack->rc_hybrid_mode) {
11328 			rack->r_ctl.client_suggested_maxseg = 0;
11329 			rack->rc_catch_up = 0;
11330 			if (rack->cspr_is_fcc == 0)
11331 				rack->r_ctl.bw_rate_cap = 0;
11332 			else
11333 				rack->r_ctl.fillcw_cap = rack_fillcw_bw_cap;
11334 		}
11335 		if (rack->rc_hybrid_mode) {
11336 			rack_log_hybrid(rack, (seq + len - 1), NULL, HYBRID_LOG_NO_RANGE, __LINE__, err);
11337 		}
11338 		if (rack->r_ctl.rc_last_sft) {
11339 			rack->r_ctl.rc_last_sft = NULL;
11340 		}
11341 		return;
11342 	}
11343 	if ((rc_cur->hybrid_flags & TCP_HYBRID_PACING_WASSET) == 0) {
11344 		/* This entry was never setup for hybrid pacing on/off etc */
11345 		if (rack->rc_hybrid_mode) {
11346 			rack->r_ctl.client_suggested_maxseg = 0;
11347 			rack->rc_catch_up = 0;
11348 			rack->r_ctl.bw_rate_cap = 0;
11349 		}
11350 		if (rack->r_ctl.rc_last_sft) {
11351 			rack->r_ctl.rc_last_sft = NULL;
11352 		}
11353 		if ((rc_cur->flags & TCP_TRK_TRACK_FLG_FSND) == 0) {
11354 			rc_cur->flags |= TCP_TRK_TRACK_FLG_FSND;
11355 			rc_cur->first_send = cts;
11356 			rc_cur->sent_at_fs = rack->rc_tp->t_sndbytes;
11357 			rc_cur->rxt_at_fs = rack->rc_tp->t_snd_rxt_bytes;
11358 		}
11359 		return;
11360 	}
11361 	/*
11362 	 * Ok if we have a new entry *or* have never
11363 	 * set up an entry we need to proceed. If
11364 	 * we have already set it up this entry we
11365 	 * just continue along with what we already
11366 	 * setup.
11367 	 */
11368 	tp = rack->rc_tp;
11369 	if ((rack->r_ctl.rc_last_sft != NULL) &&
11370 	    (rack->r_ctl.rc_last_sft == rc_cur)) {
11371 		/* Its already in place */
11372 		if (rack->rc_hybrid_mode)
11373 			rack_log_hybrid(rack, seq, rc_cur, HYBRID_LOG_ISSAME, __LINE__, 0);
11374 		return;
11375 	}
11376 	if (rack->rc_hybrid_mode == 0) {
11377 		rack->r_ctl.rc_last_sft = rc_cur;
11378 		if (orig_ent) {
11379 			orig_ent->sent_at_ls = rack->rc_tp->t_sndbytes;
11380 			orig_ent->rxt_at_ls = rack->rc_tp->t_snd_rxt_bytes;
11381 			orig_ent->flags |= TCP_TRK_TRACK_FLG_LSND;
11382 		}
11383 		rack_log_hybrid(rack, seq, rc_cur, HYBRID_LOG_RULES_APP, __LINE__, 0);
11384 		return;
11385 	}
11386 	if ((rc_cur->hybrid_flags & TCP_HYBRID_PACING_CSPR) && rc_cur->cspr){
11387 		/* Compensate for all the header overhead's */
11388 		if (rack->cspr_is_fcc == 0)
11389 			rack->r_ctl.bw_rate_cap	= rack_compensate_for_linerate(rack, rc_cur->cspr);
11390 		else
11391 			rack->r_ctl.fillcw_cap =  rack_compensate_for_linerate(rack, rc_cur->cspr);
11392 	} else {
11393 		if (rack->rc_hybrid_mode) {
11394 			if (rack->cspr_is_fcc == 0)
11395 				rack->r_ctl.bw_rate_cap = 0;
11396 			else
11397 				rack->r_ctl.fillcw_cap = rack_fillcw_bw_cap;
11398 		}
11399 	}
11400 	if (rc_cur->hybrid_flags & TCP_HYBRID_PACING_H_MS)
11401 		rack->r_ctl.client_suggested_maxseg = rc_cur->hint_maxseg;
11402 	else
11403 		rack->r_ctl.client_suggested_maxseg = 0;
11404 	if (rc_cur->timestamp == rack->r_ctl.last_tm_mark) {
11405 		/*
11406 		 * It is the same timestamp as the previous one
11407 		 * add the hybrid flag that will indicate we use
11408 		 * sendtime not arrival time for catch-up mode.
11409 		 */
11410 		rc_cur->hybrid_flags |= TCP_HYBRID_PACING_SENDTIME;
11411 	}
11412 	if ((rc_cur->hybrid_flags & TCP_HYBRID_PACING_CU) &&
11413 	    (rc_cur->cspr > 0)) {
11414 		uint64_t len;
11415 
11416 		rack->rc_catch_up = 1;
11417 		/*
11418 		 * Calculate the deadline time, first set the
11419 		 * time to when the request arrived.
11420 		 */
11421 		if (rc_cur->hybrid_flags & TCP_HYBRID_PACING_SENDTIME) {
11422 			/*
11423 			 * For cases where its a duplicate tm (we received more
11424 			 * than one request for a tm) we want to use now, the point
11425 			 * where we are just sending the first bit of the request.
11426 			 */
11427 			rc_cur->deadline = cts;
11428 		} else {
11429 			/*
11430 			 * Here we have a different tm from the last request
11431 			 * so we want to use arrival time as our base.
11432 			 */
11433 			rc_cur->deadline = rc_cur->localtime;
11434 		}
11435 		/*
11436 		 * Next calculate the length and compensate for
11437 		 * TLS if need be.
11438 		 */
11439 		len = rc_cur->end - rc_cur->start;
11440 		if (tp->t_inpcb.inp_socket->so_snd.sb_tls_info) {
11441 			/*
11442 			 * This session is doing TLS. Take a swag guess
11443 			 * at the overhead.
11444 			 */
11445 			len += tcp_estimate_tls_overhead(tp->t_inpcb.inp_socket, len);
11446 		}
11447 		/*
11448 		 * Now considering the size, and the cspr, what is the time that
11449 		 * would be required at the cspr rate. Here we use the raw
11450 		 * cspr value since the client only looks at the raw data. We
11451 		 * do use len which includes TLS overhead, but not the TCP/IP etc.
11452 		 * That will get made up for in the CU pacing rate set.
11453 		 */
11454 		len *= HPTS_USEC_IN_SEC;
11455 		len /= rc_cur->cspr;
11456 		rc_cur->deadline += len;
11457 	} else {
11458 		rack->rc_catch_up = 0;
11459 		rc_cur->deadline = 0;
11460 	}
11461 	if (rack->r_ctl.client_suggested_maxseg != 0) {
11462 		/*
11463 		 * We need to reset the max pace segs if we have a
11464 		 * client_suggested_maxseg.
11465 		 */
11466 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
11467 	}
11468 	if (orig_ent) {
11469 		orig_ent->sent_at_ls = rack->rc_tp->t_sndbytes;
11470 		orig_ent->rxt_at_ls = rack->rc_tp->t_snd_rxt_bytes;
11471 		orig_ent->flags |= TCP_TRK_TRACK_FLG_LSND;
11472 	}
11473 	rack_log_hybrid(rack, seq, rc_cur, HYBRID_LOG_RULES_APP, __LINE__, 0);
11474 	/* Remember it for next time and for CU mode */
11475 	rack->r_ctl.rc_last_sft = rc_cur;
11476 	rack->r_ctl.last_tm_mark = rc_cur->timestamp;
11477 }
11478 #endif
11479 
11480 static void
11481 rack_chk_req_and_hybrid_on_out(struct tcp_rack *rack, tcp_seq seq, uint32_t len, uint64_t cts)
11482 {
11483 #ifdef TCP_REQUEST_TRK
11484 	struct tcp_sendfile_track *ent;
11485 
11486 	ent = rack->r_ctl.rc_last_sft;
11487 	if ((ent == NULL) ||
11488 	    (ent->flags == TCP_TRK_TRACK_FLG_EMPTY) ||
11489 	    (SEQ_GEQ(seq, ent->end_seq))) {
11490 		/* Time to update the track. */
11491 		rack_set_dgp_hybrid_mode(rack, seq, len, cts);
11492 		ent = rack->r_ctl.rc_last_sft;
11493 	}
11494 	/* Out of all */
11495 	if (ent == NULL) {
11496 		return;
11497 	}
11498 	if (SEQ_LT(ent->end_seq, (seq + len))) {
11499 		/*
11500 		 * This is the case where our end_seq guess
11501 		 * was wrong. This is usually due to TLS having
11502 		 * more bytes then our guess. It could also be the
11503 		 * case that the client sent in two requests closely
11504 		 * and the SB is full of both so we are sending part
11505 		 * of each (end|beg). In such a case lets move this
11506 		 * guys end to match the end of this send. That
11507 		 * way it will complete when all of it is acked.
11508 		 */
11509 		ent->end_seq = (seq + len);
11510 		if (rack->rc_hybrid_mode)
11511 			rack_log_hybrid_bw(rack, seq, len, 0, 0, HYBRID_LOG_EXTEND, 0, ent, __LINE__);
11512 	}
11513 	/* Now validate we have set the send time of this one */
11514 	if ((ent->flags & TCP_TRK_TRACK_FLG_FSND) == 0) {
11515 		ent->flags |= TCP_TRK_TRACK_FLG_FSND;
11516 		ent->first_send = cts;
11517 		ent->sent_at_fs = rack->rc_tp->t_sndbytes;
11518 		ent->rxt_at_fs = rack->rc_tp->t_snd_rxt_bytes;
11519 	}
11520 #endif
11521 }
11522 
11523 static void
11524 rack_gain_for_fastoutput(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t acked_amount)
11525 {
11526 	/*
11527 	 * The fast output path is enabled and we
11528 	 * have moved the cumack forward. Lets see if
11529 	 * we can expand forward the fast path length by
11530 	 * that amount. What we would ideally like to
11531 	 * do is increase the number of bytes in the
11532 	 * fast path block (left_to_send) by the
11533 	 * acked amount. However we have to gate that
11534 	 * by two factors:
11535 	 * 1) The amount outstanding and the rwnd of the peer
11536 	 *    (i.e. we don't want to exceed the rwnd of the peer).
11537 	 *    <and>
11538 	 * 2) The amount of data left in the socket buffer (i.e.
11539 	 *    we can't send beyond what is in the buffer).
11540 	 *
11541 	 * Note that this does not take into account any increase
11542 	 * in the cwnd. We will only extend the fast path by
11543 	 * what was acked.
11544 	 */
11545 	uint32_t new_total, gating_val;
11546 
11547 	new_total = acked_amount + rack->r_ctl.fsb.left_to_send;
11548 	gating_val = min((sbavail(&so->so_snd) - (tp->snd_max - tp->snd_una)),
11549 			 (tp->snd_wnd - (tp->snd_max - tp->snd_una)));
11550 	if (new_total <= gating_val) {
11551 		/* We can increase left_to_send by the acked amount */
11552 		counter_u64_add(rack_extended_rfo, 1);
11553 		rack->r_ctl.fsb.left_to_send = new_total;
11554 		KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(&rack->rc_inp->inp_socket->so_snd) - (tp->snd_max - tp->snd_una))),
11555 			("rack:%p left_to_send:%u sbavail:%u out:%u",
11556 			 rack, rack->r_ctl.fsb.left_to_send,
11557 			 sbavail(&rack->rc_inp->inp_socket->so_snd),
11558 			 (tp->snd_max - tp->snd_una)));
11559 
11560 	}
11561 }
11562 
11563 static void
11564 rack_adjust_sendmap_head(struct tcp_rack *rack, struct sockbuf *sb)
11565 {
11566 	/*
11567 	 * Here any sendmap entry that points to the
11568 	 * beginning mbuf must be adjusted to the correct
11569 	 * offset. This must be called with:
11570 	 * 1) The socket buffer locked
11571 	 * 2) snd_una adjusted to its new position.
11572 	 *
11573 	 * Note that (2) implies rack_ack_received has also
11574 	 * been called and all the sbcut's have been done.
11575 	 *
11576 	 * We grab the first mbuf in the socket buffer and
11577 	 * then go through the front of the sendmap, recalculating
11578 	 * the stored offset for any sendmap entry that has
11579 	 * that mbuf. We must use the sb functions to do this
11580 	 * since its possible an add was done has well as
11581 	 * the subtraction we may have just completed. This should
11582 	 * not be a penalty though, since we just referenced the sb
11583 	 * to go in and trim off the mbufs that we freed (of course
11584 	 * there will be a penalty for the sendmap references though).
11585 	 *
11586 	 * Note also with INVARIANT on, we validate with a KASSERT
11587 	 * that the first sendmap entry has a soff of 0.
11588 	 *
11589 	 */
11590 	struct mbuf *m;
11591 	struct rack_sendmap *rsm;
11592 	tcp_seq snd_una;
11593 #ifdef INVARIANTS
11594 	int first_processed = 0;
11595 #endif
11596 
11597 	snd_una = rack->rc_tp->snd_una;
11598 	SOCKBUF_LOCK_ASSERT(sb);
11599 	m = sb->sb_mb;
11600 	rsm = tqhash_min(rack->r_ctl.tqh);
11601 	if ((rsm == NULL) || (m == NULL)) {
11602 		/* Nothing outstanding */
11603 		return;
11604 	}
11605 	/* The very first RSM's mbuf must point to the head mbuf in the sb */
11606 	KASSERT((rsm->m == m),
11607 		("Rack:%p sb:%p rsm:%p -- first rsm mbuf not aligned to sb",
11608 		 rack, sb, rsm));
11609 	while (rsm->m && (rsm->m == m)) {
11610 		/* one to adjust */
11611 #ifdef INVARIANTS
11612 		struct mbuf *tm;
11613 		uint32_t soff;
11614 
11615 		tm = sbsndmbuf(sb, (rsm->r_start - snd_una), &soff);
11616 		if ((rsm->orig_m_len != m->m_len) ||
11617 		    (rsm->orig_t_space != M_TRAILINGROOM(m))){
11618 			rack_adjust_orig_mlen(rsm);
11619 		}
11620 		if (first_processed == 0) {
11621 			KASSERT((rsm->soff == 0),
11622 				("Rack:%p rsm:%p -- rsm at head but soff not zero",
11623 				 rack, rsm));
11624 			first_processed = 1;
11625 		}
11626 		if ((rsm->soff != soff) || (rsm->m != tm)) {
11627 			/*
11628 			 * This is not a fatal error, we anticipate it
11629 			 * might happen (the else code), so we count it here
11630 			 * so that under invariant we can see that it really
11631 			 * does happen.
11632 			 */
11633 			counter_u64_add(rack_adjust_map_bw, 1);
11634 		}
11635 		rsm->m = tm;
11636 		rsm->soff = soff;
11637 		if (tm) {
11638 			rsm->orig_m_len = rsm->m->m_len;
11639 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
11640 		} else {
11641 			rsm->orig_m_len = 0;
11642 			rsm->orig_t_space = 0;
11643 		}
11644 #else
11645 		rsm->m = sbsndmbuf(sb, (rsm->r_start - snd_una), &rsm->soff);
11646 		if (rsm->m) {
11647 			rsm->orig_m_len = rsm->m->m_len;
11648 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
11649 		} else {
11650 			rsm->orig_m_len = 0;
11651 			rsm->orig_t_space = 0;
11652 		}
11653 #endif
11654 		rsm = tqhash_next(rack->r_ctl.tqh, rsm);
11655 		if (rsm == NULL)
11656 			break;
11657 	}
11658 }
11659 
11660 #ifdef TCP_REQUEST_TRK
11661 static inline void
11662 rack_req_check_for_comp(struct tcp_rack *rack, tcp_seq th_ack)
11663 {
11664 	struct tcp_sendfile_track *ent;
11665 	int i;
11666 
11667 	if ((rack->rc_hybrid_mode == 0) &&
11668 	    (tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING) == 0)) {
11669 		/*
11670 		 * Just do normal completions hybrid pacing is not on
11671 		 * and CLDL is off as well.
11672 		 */
11673 		tcp_req_check_for_comp(rack->rc_tp, th_ack);
11674 		return;
11675 	}
11676 	/*
11677 	 * Originally I was just going to find the th_ack associated
11678 	 * with an entry. But then I realized a large strech ack could
11679 	 * in theory ack two or more requests at once. So instead we
11680 	 * need to find all entries that are completed by th_ack not
11681 	 * just a single entry and do our logging.
11682 	 */
11683 	ent = tcp_req_find_a_req_that_is_completed_by(rack->rc_tp, th_ack, &i);
11684 	while (ent != NULL) {
11685 		/*
11686 		 * We may be doing hybrid pacing or CLDL and need more details possibly
11687 		 * so we do it manually instead of calling
11688 		 * tcp_req_check_for_comp()
11689 		 */
11690 		uint64_t laa, tim, data, cbw, ftim;
11691 
11692 		/* Ok this ack frees it */
11693 		rack_log_hybrid(rack, th_ack,
11694 				ent, HYBRID_LOG_REQ_COMP, __LINE__, 0);
11695 		rack_log_hybrid_sends(rack, ent, __LINE__);
11696 		/* calculate the time based on the ack arrival */
11697 		data = ent->end - ent->start;
11698 		laa = tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time);
11699 		if (ent->flags & TCP_TRK_TRACK_FLG_FSND) {
11700 			if (ent->first_send > ent->localtime)
11701 				ftim = ent->first_send;
11702 			else
11703 				ftim = ent->localtime;
11704 		} else {
11705 			/* TSNH */
11706 			ftim = ent->localtime;
11707 		}
11708 		if (laa > ent->localtime)
11709 			tim = laa - ftim;
11710 		else
11711 			tim = 0;
11712 		cbw = data * HPTS_USEC_IN_SEC;
11713 		if (tim > 0)
11714 			cbw /= tim;
11715 		else
11716 			cbw = 0;
11717 		rack_log_hybrid_bw(rack, th_ack, cbw, tim, data, HYBRID_LOG_BW_MEASURE, 0, ent, __LINE__);
11718 		/*
11719 		 * Check to see if we are freeing what we are pointing to send wise
11720 		 * if so be sure to NULL the pointer so we know we are no longer
11721 		 * set to anything.
11722 		 */
11723 		if (ent == rack->r_ctl.rc_last_sft) {
11724 			rack->r_ctl.rc_last_sft = NULL;
11725 			if (rack->rc_hybrid_mode) {
11726 				rack->rc_catch_up = 0;
11727 				if (rack->cspr_is_fcc == 0)
11728 					rack->r_ctl.bw_rate_cap = 0;
11729 				else
11730 					rack->r_ctl.fillcw_cap = rack_fillcw_bw_cap;
11731 				rack->r_ctl.client_suggested_maxseg = 0;
11732 			}
11733 		}
11734 		/* Generate the log that the tcp_netflix call would have */
11735 		tcp_req_log_req_info(rack->rc_tp, ent,
11736 				      i, TCP_TRK_REQ_LOG_FREED, 0, 0);
11737 		/* Free it and see if there is another one */
11738 		tcp_req_free_a_slot(rack->rc_tp, ent);
11739 		ent = tcp_req_find_a_req_that_is_completed_by(rack->rc_tp, th_ack, &i);
11740 	}
11741 }
11742 #endif
11743 
11744 
11745 /*
11746  * Return value of 1, we do not need to call rack_process_data().
11747  * return value of 0, rack_process_data can be called.
11748  * For ret_val if its 0 the TCP is locked, if its non-zero
11749  * its unlocked and probably unsafe to touch the TCB.
11750  */
11751 static int
11752 rack_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so,
11753     struct tcpcb *tp, struct tcpopt *to,
11754     uint32_t tiwin, int32_t tlen,
11755     int32_t * ofia, int32_t thflags, int32_t *ret_val, int32_t orig_tlen)
11756 {
11757 	int32_t ourfinisacked = 0;
11758 	int32_t nsegs, acked_amount;
11759 	int32_t acked;
11760 	struct mbuf *mfree;
11761 	struct tcp_rack *rack;
11762 	int32_t under_pacing = 0;
11763 	int32_t post_recovery = 0;
11764 	uint32_t p_cwnd;
11765 
11766 	INP_WLOCK_ASSERT(tptoinpcb(tp));
11767 
11768 	rack = (struct tcp_rack *)tp->t_fb_ptr;
11769 	if (SEQ_GEQ(tp->snd_una, tp->iss + (65535 << tp->snd_scale))) {
11770 		/* Checking SEG.ACK against ISS is definitely redundant. */
11771 		tp->t_flags2 |= TF2_NO_ISS_CHECK;
11772 	}
11773 	if (!V_tcp_insecure_ack) {
11774 		tcp_seq seq_min;
11775 		bool ghost_ack_check;
11776 
11777 		if (tp->t_flags2 & TF2_NO_ISS_CHECK) {
11778 			/* Check for too old ACKs (RFC 5961, Section 5.2). */
11779 			seq_min = tp->snd_una - tp->max_sndwnd;
11780 			ghost_ack_check = false;
11781 		} else {
11782 			if (SEQ_GT(tp->iss + 1, tp->snd_una - tp->max_sndwnd)) {
11783 				/* Checking for ghost ACKs is stricter. */
11784 				seq_min = tp->iss + 1;
11785 				ghost_ack_check = true;
11786 			} else {
11787 				/*
11788 				 * Checking for too old ACKs (RFC 5961,
11789 				 * Section 5.2) is stricter.
11790 				 */
11791 				seq_min = tp->snd_una - tp->max_sndwnd;
11792 				ghost_ack_check = false;
11793 			}
11794 		}
11795 		if (SEQ_LT(th->th_ack, seq_min)) {
11796 			if (ghost_ack_check)
11797 				TCPSTAT_INC(tcps_rcvghostack);
11798 			else
11799 				TCPSTAT_INC(tcps_rcvacktooold);
11800 			/* Send challenge ACK. */
11801 			ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val);
11802 			rack->r_wanted_output = 1;
11803 			return (1);
11804 		}
11805 	}
11806 	if (SEQ_GT(th->th_ack, tp->snd_max)) {
11807 		ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val);
11808 		rack->r_wanted_output = 1;
11809 		return (1);
11810 	}
11811 	if (rack->gp_ready &&
11812 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
11813 		under_pacing = 1;
11814 	}
11815 	if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) {
11816 		int in_rec, dup_ack_struck = 0;
11817 		int dsack_seen = 0, sacks_seen = 0;
11818 
11819 		in_rec = IN_FASTRECOVERY(tp->t_flags);
11820 		if (rack->rc_in_persist) {
11821 			tp->t_rxtshift = 0;
11822 			RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
11823 				      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
11824 		}
11825 
11826 		if ((th->th_ack == tp->snd_una) &&
11827 		    (tiwin == tp->snd_wnd) &&
11828 		    (orig_tlen == 0) &&
11829 		    ((to->to_flags & TOF_SACK) == 0)) {
11830 			rack_strike_dupack(rack, th->th_ack);
11831 			dup_ack_struck = 1;
11832 		}
11833 		rack_log_ack(tp, to, th, ((in_rec == 0) && IN_FASTRECOVERY(tp->t_flags)),
11834 			     dup_ack_struck, &dsack_seen, &sacks_seen);
11835 
11836 	}
11837 	if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
11838 		/*
11839 		 * Old ack, behind (or duplicate to) the last one rcv'd
11840 		 * Note: We mark reordering is occuring if its
11841 		 * less than and we have not closed our window.
11842 		 */
11843 		if (SEQ_LT(th->th_ack, tp->snd_una) && (sbspace(&so->so_rcv) > ctf_fixed_maxseg(tp))) {
11844 			rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
11845 			if (rack->r_ctl.rc_reorder_ts == 0)
11846 				rack->r_ctl.rc_reorder_ts = 1;
11847 		}
11848 		return (0);
11849 	}
11850 	/*
11851 	 * If we reach this point, ACK is not a duplicate, i.e., it ACKs
11852 	 * something we sent.
11853 	 */
11854 	if (tp->t_flags & TF_NEEDSYN) {
11855 		/*
11856 		 * T/TCP: Connection was half-synchronized, and our SYN has
11857 		 * been ACK'd (so connection is now fully synchronized).  Go
11858 		 * to non-starred state, increment snd_una for ACK of SYN,
11859 		 * and check if we can do window scaling.
11860 		 */
11861 		tp->t_flags &= ~TF_NEEDSYN;
11862 		tp->snd_una++;
11863 		/* Do window scaling? */
11864 		if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
11865 		    (TF_RCVD_SCALE | TF_REQ_SCALE)) {
11866 			tp->rcv_scale = tp->request_r_scale;
11867 			/* Send window already scaled. */
11868 		}
11869 	}
11870 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
11871 
11872 	acked = BYTES_THIS_ACK(tp, th);
11873 	if (acked) {
11874 		/*
11875 		 * Any time we move the cum-ack forward clear
11876 		 * keep-alive tied probe-not-answered. The
11877 		 * persists clears its own on entry.
11878 		 */
11879 		rack->probe_not_answered = 0;
11880 	}
11881 	KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
11882 	KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
11883 	/*
11884 	 * If we just performed our first retransmit, and the ACK arrives
11885 	 * within our recovery window, then it was a mistake to do the
11886 	 * retransmit in the first place.  Recover our original cwnd and
11887 	 * ssthresh, and proceed to transmit where we left off.
11888 	 */
11889 	if ((tp->t_flags & TF_PREVVALID) &&
11890 	    ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
11891 		tp->t_flags &= ~TF_PREVVALID;
11892 		if (tp->t_rxtshift == 1 &&
11893 		    (int)(ticks - tp->t_badrxtwin) < 0)
11894 			rack_cong_signal(tp, CC_RTO_ERR, th->th_ack, __LINE__);
11895 	}
11896 	if (acked) {
11897 		/* assure we are not backed off */
11898 		tp->t_rxtshift = 0;
11899 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
11900 			      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
11901 		rack->rc_tlp_in_progress = 0;
11902 		rack->r_ctl.rc_tlp_cnt_out = 0;
11903 		/*
11904 		 * If it is the RXT timer we want to
11905 		 * stop it, so we can restart a TLP.
11906 		 */
11907 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
11908 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
11909 #ifdef TCP_REQUEST_TRK
11910 		rack_req_check_for_comp(rack, th->th_ack);
11911 #endif
11912 	}
11913 	/*
11914 	 * If we have a timestamp reply, update smoothed round trip time. If
11915 	 * no timestamp is present but transmit timer is running and timed
11916 	 * sequence number was acked, update smoothed round trip time. Since
11917 	 * we now have an rtt measurement, cancel the timer backoff (cf.,
11918 	 * Phil Karn's retransmit alg.). Recompute the initial retransmit
11919 	 * timer.
11920 	 *
11921 	 * Some boxes send broken timestamp replies during the SYN+ACK
11922 	 * phase, ignore timestamps of 0 or we could calculate a huge RTT
11923 	 * and blow up the retransmit timer.
11924 	 */
11925 	/*
11926 	 * If all outstanding data is acked, stop retransmit timer and
11927 	 * remember to restart (more output or persist). If there is more
11928 	 * data to be acked, restart retransmit timer, using current
11929 	 * (possibly backed-off) value.
11930 	 */
11931 	if (acked == 0) {
11932 		if (ofia)
11933 			*ofia = ourfinisacked;
11934 		return (0);
11935 	}
11936 	if (IN_RECOVERY(tp->t_flags)) {
11937 		if (SEQ_LT(th->th_ack, tp->snd_recover) &&
11938 		    (SEQ_LT(th->th_ack, tp->snd_max))) {
11939 			tcp_rack_partialack(tp);
11940 		} else {
11941 			rack_post_recovery(tp, th->th_ack);
11942 			post_recovery = 1;
11943 			/*
11944 			 * Grab the segsiz, multiply by 2 and add the snd_cwnd
11945 			 * that is the max the CC should add if we are exiting
11946 			 * recovery and doing a late add.
11947 			 */
11948 			p_cwnd = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
11949 			p_cwnd <<= 1;
11950 			p_cwnd += tp->snd_cwnd;
11951 		}
11952 	} else if ((rack->rto_from_rec == 1) &&
11953 		   SEQ_GEQ(th->th_ack, tp->snd_recover)) {
11954 		/*
11955 		 * We were in recovery, hit a rxt timeout
11956 		 * and never re-entered recovery. The timeout(s)
11957 		 * made up all the lost data. In such a case
11958 		 * we need to clear the rto_from_rec flag.
11959 		 */
11960 		rack->rto_from_rec = 0;
11961 	}
11962 	/*
11963 	 * Let the congestion control algorithm update congestion control
11964 	 * related information. This typically means increasing the
11965 	 * congestion window.
11966 	 */
11967 	rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, post_recovery);
11968 	if (post_recovery &&
11969 	    (tp->snd_cwnd > p_cwnd)) {
11970 		/* Must be non-newreno (cubic) getting too ahead of itself */
11971 		tp->snd_cwnd = p_cwnd;
11972 	}
11973 	SOCK_SENDBUF_LOCK(so);
11974 	acked_amount = min(acked, (int)sbavail(&so->so_snd));
11975 	tp->snd_wnd -= acked_amount;
11976 	mfree = sbcut_locked(&so->so_snd, acked_amount);
11977 	if ((sbused(&so->so_snd) == 0) &&
11978 	    (acked > acked_amount) &&
11979 	    (tp->t_state >= TCPS_FIN_WAIT_1) &&
11980 	    (tp->t_flags & TF_SENTFIN)) {
11981 		/*
11982 		 * We must be sure our fin
11983 		 * was sent and acked (we can be
11984 		 * in FIN_WAIT_1 without having
11985 		 * sent the fin).
11986 		 */
11987 		ourfinisacked = 1;
11988 	}
11989 	tp->snd_una = th->th_ack;
11990 	/* wakeups? */
11991 	if (acked_amount && sbavail(&so->so_snd))
11992 		rack_adjust_sendmap_head(rack, &so->so_snd);
11993 	rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
11994 	/* NB: sowwakeup_locked() does an implicit unlock. */
11995 	sowwakeup_locked(so);
11996 	m_freem(mfree);
11997 	if (SEQ_GT(tp->snd_una, tp->snd_recover))
11998 		tp->snd_recover = tp->snd_una;
11999 
12000 	if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
12001 		tp->snd_nxt = tp->snd_max;
12002 	}
12003 	if (under_pacing &&
12004 	    (rack->use_fixed_rate == 0) &&
12005 	    (rack->in_probe_rtt == 0) &&
12006 	    rack->rc_gp_dyn_mul &&
12007 	    rack->rc_always_pace) {
12008 		/* Check if we are dragging bottom */
12009 		rack_check_bottom_drag(tp, rack, so);
12010 	}
12011 	if (tp->snd_una == tp->snd_max) {
12012 		/* Nothing left outstanding */
12013 		tp->t_flags &= ~TF_PREVVALID;
12014 		if (rack->r_ctl.rc_went_idle_time == 0)
12015 			rack->r_ctl.rc_went_idle_time = 1;
12016 		rack->r_ctl.retran_during_recovery = 0;
12017 		rack->r_ctl.dsack_byte_cnt = 0;
12018 		rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
12019 		if (sbavail(&tptosocket(tp)->so_snd) == 0)
12020 			tp->t_acktime = 0;
12021 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
12022 		rack->rc_suspicious = 0;
12023 		/* Set need output so persist might get set */
12024 		rack->r_wanted_output = 1;
12025 		sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
12026 		if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
12027 		    (sbavail(&so->so_snd) == 0) &&
12028 		    (tp->t_flags2 & TF2_DROP_AF_DATA)) {
12029 			/*
12030 			 * The socket was gone and the
12031 			 * peer sent data (now or in the past), time to
12032 			 * reset him.
12033 			 */
12034 			*ret_val = 1;
12035 			/* tcp_close will kill the inp pre-log the Reset */
12036 			tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
12037 			tp = tcp_close(tp);
12038 			ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen);
12039 			return (1);
12040 		}
12041 	}
12042 	if (ofia)
12043 		*ofia = ourfinisacked;
12044 	return (0);
12045 }
12046 
12047 
12048 static void
12049 rack_log_collapse(struct tcp_rack *rack, uint32_t cnt, uint32_t split, uint32_t out, int line,
12050 		  int dir, uint32_t flags, struct rack_sendmap *rsm)
12051 {
12052 	if (tcp_bblogging_on(rack->rc_tp)) {
12053 		union tcp_log_stackspecific log;
12054 		struct timeval tv;
12055 
12056 		memset(&log, 0, sizeof(log));
12057 		log.u_bbr.flex1 = cnt;
12058 		log.u_bbr.flex2 = split;
12059 		log.u_bbr.flex3 = out;
12060 		log.u_bbr.flex4 = line;
12061 		log.u_bbr.flex5 = rack->r_must_retran;
12062 		log.u_bbr.flex6 = flags;
12063 		log.u_bbr.flex7 = rack->rc_has_collapsed;
12064 		log.u_bbr.flex8 = dir;	/*
12065 					 * 1 is collapsed, 0 is uncollapsed,
12066 					 * 2 is log of a rsm being marked, 3 is a split.
12067 					 */
12068 		if (rsm == NULL)
12069 			log.u_bbr.rttProp = 0;
12070 		else
12071 			log.u_bbr.rttProp = (uintptr_t)rsm;
12072 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
12073 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
12074 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
12075 		    &rack->rc_inp->inp_socket->so_rcv,
12076 		    &rack->rc_inp->inp_socket->so_snd,
12077 		    TCP_RACK_LOG_COLLAPSE, 0,
12078 		    0, &log, false, &tv);
12079 	}
12080 }
12081 
12082 static void
12083 rack_collapsed_window(struct tcp_rack *rack, uint32_t out, tcp_seq th_ack, int line)
12084 {
12085 	/*
12086 	 * Here all we do is mark the collapsed point and set the flag.
12087 	 * This may happen again and again, but there is no
12088 	 * sense splitting our map until we know where the
12089 	 * peer finally lands in the collapse.
12090 	 */
12091 	tcp_trace_point(rack->rc_tp, TCP_TP_COLLAPSED_WND);
12092 	if ((rack->rc_has_collapsed == 0) ||
12093 	    (rack->r_ctl.last_collapse_point != (th_ack + rack->rc_tp->snd_wnd)))
12094 		counter_u64_add(rack_collapsed_win_seen, 1);
12095 	rack->r_ctl.last_collapse_point = th_ack + rack->rc_tp->snd_wnd;
12096 	rack->r_ctl.high_collapse_point = rack->rc_tp->snd_max;
12097 	rack->rc_has_collapsed = 1;
12098 	rack->r_collapse_point_valid = 1;
12099 	rack_log_collapse(rack, 0, th_ack, rack->r_ctl.last_collapse_point, line, 1, 0, NULL);
12100 }
12101 
12102 static void
12103 rack_un_collapse_window(struct tcp_rack *rack, int line)
12104 {
12105 	struct rack_sendmap *nrsm, *rsm;
12106 	int cnt = 0, split = 0;
12107 	int insret __diagused;
12108 
12109 
12110 	tcp_trace_point(rack->rc_tp, TCP_TP_COLLAPSED_WND);
12111 	rack->rc_has_collapsed = 0;
12112 	rsm = tqhash_find(rack->r_ctl.tqh, rack->r_ctl.last_collapse_point);
12113 	if (rsm == NULL) {
12114 		/* Nothing to do maybe the peer ack'ed it all */
12115 		rack_log_collapse(rack, 0, 0, ctf_outstanding(rack->rc_tp), line, 0, 0, NULL);
12116 		return;
12117 	}
12118 	/* Now do we need to split this one? */
12119 	if (SEQ_GT(rack->r_ctl.last_collapse_point, rsm->r_start)) {
12120 		rack_log_collapse(rack, rsm->r_start, rsm->r_end,
12121 				  rack->r_ctl.last_collapse_point, line, 3, rsm->r_flags, rsm);
12122 		nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
12123 		if (nrsm == NULL) {
12124 			/* We can't get a rsm, mark all? */
12125 			nrsm = rsm;
12126 			goto no_split;
12127 		}
12128 		/* Clone it */
12129 		split = 1;
12130 		rack_clone_rsm(rack, nrsm, rsm, rack->r_ctl.last_collapse_point);
12131 #ifndef INVARIANTS
12132 		(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
12133 #else
12134 		if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
12135 			panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p",
12136 			      nrsm, insret, rack, rsm);
12137 		}
12138 #endif
12139 		rack_log_map_chg(rack->rc_tp, rack, NULL, rsm, nrsm, MAP_SPLIT,
12140 				 rack->r_ctl.last_collapse_point, __LINE__);
12141 		if (rsm->r_in_tmap) {
12142 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
12143 			nrsm->r_in_tmap = 1;
12144 		}
12145 		/*
12146 		 * Set in the new RSM as the
12147 		 * collapsed starting point
12148 		 */
12149 		rsm = nrsm;
12150 	}
12151 
12152 no_split:
12153 	TQHASH_FOREACH_FROM(nrsm, rack->r_ctl.tqh, rsm)  {
12154 		cnt++;
12155 		nrsm->r_flags |= RACK_RWND_COLLAPSED;
12156 		rack_log_collapse(rack, nrsm->r_start, nrsm->r_end, 0, line, 4, nrsm->r_flags, nrsm);
12157 		cnt++;
12158 	}
12159 	if (cnt) {
12160 		counter_u64_add(rack_collapsed_win, 1);
12161 	}
12162 	rack_log_collapse(rack, cnt, split, ctf_outstanding(rack->rc_tp), line, 0, 0, NULL);
12163 }
12164 
12165 static void
12166 rack_handle_delayed_ack(struct tcpcb *tp, struct tcp_rack *rack,
12167 			int32_t tlen, int32_t tfo_syn)
12168 {
12169 	if (DELAY_ACK(tp, tlen) || tfo_syn) {
12170 		rack_timer_cancel(tp, rack,
12171 				  rack->r_ctl.rc_rcvtime, __LINE__);
12172 		tp->t_flags |= TF_DELACK;
12173 	} else {
12174 		rack->r_wanted_output = 1;
12175 		tp->t_flags |= TF_ACKNOW;
12176 	}
12177 }
12178 
12179 static void
12180 rack_validate_fo_sendwin_up(struct tcpcb *tp, struct tcp_rack *rack)
12181 {
12182 	/*
12183 	 * If fast output is in progress, lets validate that
12184 	 * the new window did not shrink on us and make it
12185 	 * so fast output should end.
12186 	 */
12187 	if (rack->r_fast_output) {
12188 		uint32_t out;
12189 
12190 		/*
12191 		 * Calculate what we will send if left as is
12192 		 * and compare that to our send window.
12193 		 */
12194 		out = ctf_outstanding(tp);
12195 		if ((out + rack->r_ctl.fsb.left_to_send) > tp->snd_wnd) {
12196 			/* ok we have an issue */
12197 			if (out >= tp->snd_wnd) {
12198 				/* Turn off fast output the window is met or collapsed */
12199 				rack->r_fast_output = 0;
12200 			} else {
12201 				/* we have some room left */
12202 				rack->r_ctl.fsb.left_to_send = tp->snd_wnd - out;
12203 				if (rack->r_ctl.fsb.left_to_send < ctf_fixed_maxseg(tp)) {
12204 					/* If not at least 1 full segment never mind */
12205 					rack->r_fast_output = 0;
12206 				}
12207 			}
12208 		}
12209 	}
12210 }
12211 
12212 /*
12213  * Return value of 1, the TCB is unlocked and most
12214  * likely gone, return value of 0, the TCP is still
12215  * locked.
12216  */
12217 static int
12218 rack_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so,
12219     struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
12220     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
12221 {
12222 	/*
12223 	 * Update window information. Don't look at window if no ACK: TAC's
12224 	 * send garbage on first SYN.
12225 	 */
12226 	int32_t nsegs;
12227 	int32_t tfo_syn;
12228 	struct tcp_rack *rack;
12229 
12230 	INP_WLOCK_ASSERT(tptoinpcb(tp));
12231 
12232 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12233 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
12234 	if ((thflags & TH_ACK) &&
12235 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
12236 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
12237 	    (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
12238 		/* keep track of pure window updates */
12239 		if (tlen == 0 &&
12240 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
12241 			KMOD_TCPSTAT_INC(tcps_rcvwinupd);
12242 		tp->snd_wnd = tiwin;
12243 		rack_validate_fo_sendwin_up(tp, rack);
12244 		tp->snd_wl1 = th->th_seq;
12245 		tp->snd_wl2 = th->th_ack;
12246 		if (tp->snd_wnd > tp->max_sndwnd)
12247 			tp->max_sndwnd = tp->snd_wnd;
12248 		rack->r_wanted_output = 1;
12249 	} else if (thflags & TH_ACK) {
12250 		if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) {
12251 			tp->snd_wnd = tiwin;
12252 			rack_validate_fo_sendwin_up(tp, rack);
12253 			tp->snd_wl1 = th->th_seq;
12254 			tp->snd_wl2 = th->th_ack;
12255 		}
12256 	}
12257 	if (tp->snd_wnd < ctf_outstanding(tp))
12258 		/* The peer collapsed the window */
12259 		rack_collapsed_window(rack, ctf_outstanding(tp), th->th_ack, __LINE__);
12260 	else if (rack->rc_has_collapsed)
12261 		rack_un_collapse_window(rack, __LINE__);
12262 	if ((rack->r_collapse_point_valid) &&
12263 	    (SEQ_GT(th->th_ack, rack->r_ctl.high_collapse_point)))
12264 		rack->r_collapse_point_valid = 0;
12265 	/* Was persist timer active and now we have window space? */
12266 	if ((rack->rc_in_persist != 0) &&
12267 	    (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
12268 				rack->r_ctl.rc_pace_min_segs))) {
12269 		rack_exit_persist(tp, rack, rack->r_ctl.rc_rcvtime);
12270 		tp->snd_nxt = tp->snd_max;
12271 		/* Make sure we output to start the timer */
12272 		rack->r_wanted_output = 1;
12273 	}
12274 	/* Do we enter persists? */
12275 	if ((rack->rc_in_persist == 0) &&
12276 	    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
12277 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
12278 	    ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) &&
12279 	    sbavail(&tptosocket(tp)->so_snd) &&
12280 	    (sbavail(&tptosocket(tp)->so_snd) > tp->snd_wnd)) {
12281 		/*
12282 		 * Here the rwnd is less than
12283 		 * the pacing size, we are established,
12284 		 * nothing is outstanding, and there is
12285 		 * data to send. Enter persists.
12286 		 */
12287 		rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, tp->snd_una);
12288 	}
12289 	if (tp->t_flags2 & TF2_DROP_AF_DATA) {
12290 		m_freem(m);
12291 		return (0);
12292 	}
12293 	/*
12294 	 * don't process the URG bit, ignore them drag
12295 	 * along the up.
12296 	 */
12297 	tp->rcv_up = tp->rcv_nxt;
12298 
12299 	/*
12300 	 * Process the segment text, merging it into the TCP sequencing
12301 	 * queue, and arranging for acknowledgment of receipt if necessary.
12302 	 * This process logically involves adjusting tp->rcv_wnd as data is
12303 	 * presented to the user (this happens in tcp_usrreq.c, case
12304 	 * PRU_RCVD).  If a FIN has already been received on this connection
12305 	 * then we just ignore the text.
12306 	 */
12307 	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
12308 	    (tp->t_flags & TF_FASTOPEN));
12309 	if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
12310 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
12311 		tcp_seq save_start = th->th_seq;
12312 		tcp_seq save_rnxt  = tp->rcv_nxt;
12313 		int     save_tlen  = tlen;
12314 
12315 		m_adj(m, drop_hdrlen);	/* delayed header drop */
12316 		/*
12317 		 * Insert segment which includes th into TCP reassembly
12318 		 * queue with control block tp.  Set thflags to whether
12319 		 * reassembly now includes a segment with FIN.  This handles
12320 		 * the common case inline (segment is the next to be
12321 		 * received on an established connection, and the queue is
12322 		 * empty), avoiding linkage into and removal from the queue
12323 		 * and repetition of various conversions. Set DELACK for
12324 		 * segments received in order, but ack immediately when
12325 		 * segments are out of order (so fast retransmit can work).
12326 		 */
12327 		if (th->th_seq == tp->rcv_nxt &&
12328 		    SEGQ_EMPTY(tp) &&
12329 		    (TCPS_HAVEESTABLISHED(tp->t_state) ||
12330 		    tfo_syn)) {
12331 #ifdef NETFLIX_SB_LIMITS
12332 			u_int mcnt, appended;
12333 
12334 			if (so->so_rcv.sb_shlim) {
12335 				mcnt = m_memcnt(m);
12336 				appended = 0;
12337 				if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
12338 				    CFO_NOSLEEP, NULL) == false) {
12339 					counter_u64_add(tcp_sb_shlim_fails, 1);
12340 					m_freem(m);
12341 					return (0);
12342 				}
12343 			}
12344 #endif
12345 			rack_handle_delayed_ack(tp, rack, tlen, tfo_syn);
12346 			tp->rcv_nxt += tlen;
12347 			if (tlen &&
12348 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
12349 			    (tp->t_fbyte_in == 0)) {
12350 				tp->t_fbyte_in = ticks;
12351 				if (tp->t_fbyte_in == 0)
12352 					tp->t_fbyte_in = 1;
12353 				if (tp->t_fbyte_out && tp->t_fbyte_in)
12354 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
12355 			}
12356 			thflags = tcp_get_flags(th) & TH_FIN;
12357 			KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs);
12358 			KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
12359 			SOCK_RECVBUF_LOCK(so);
12360 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
12361 				m_freem(m);
12362 			} else {
12363 				int32_t newsize;
12364 
12365 				if (tlen > 0) {
12366 					newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
12367 					if (newsize)
12368 						if (!sbreserve_locked(so, SO_RCV, newsize, NULL))
12369 							so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
12370 				}
12371 #ifdef NETFLIX_SB_LIMITS
12372 				appended =
12373 #endif
12374 					sbappendstream_locked(&so->so_rcv, m, 0);
12375 			}
12376 			rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1);
12377 			/* NB: sorwakeup_locked() does an implicit unlock. */
12378 			sorwakeup_locked(so);
12379 #ifdef NETFLIX_SB_LIMITS
12380 			if (so->so_rcv.sb_shlim && appended != mcnt)
12381 				counter_fo_release(so->so_rcv.sb_shlim,
12382 				    mcnt - appended);
12383 #endif
12384 		} else {
12385 			/*
12386 			 * XXX: Due to the header drop above "th" is
12387 			 * theoretically invalid by now.  Fortunately
12388 			 * m_adj() doesn't actually frees any mbufs when
12389 			 * trimming from the head.
12390 			 */
12391 			tcp_seq temp = save_start;
12392 
12393 			thflags = tcp_reass(tp, th, &temp, &tlen, m);
12394 			tp->t_flags |= TF_ACKNOW;
12395 			if (tp->t_flags & TF_WAKESOR) {
12396 				tp->t_flags &= ~TF_WAKESOR;
12397 				/* NB: sorwakeup_locked() does an implicit unlock. */
12398 				sorwakeup_locked(so);
12399 			}
12400 		}
12401 		if ((tp->t_flags & TF_SACK_PERMIT) &&
12402 		    (save_tlen > 0) &&
12403 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
12404 			if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
12405 				/*
12406 				 * DSACK actually handled in the fastpath
12407 				 * above.
12408 				 */
12409 				tcp_update_sack_list(tp, save_start,
12410 				    save_start + save_tlen);
12411 			} else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
12412 				if ((tp->rcv_numsacks >= 1) &&
12413 				    (tp->sackblks[0].end == save_start)) {
12414 					/*
12415 					 * Partial overlap, recorded at todrop
12416 					 * above.
12417 					 */
12418 					tcp_update_sack_list(tp,
12419 					    tp->sackblks[0].start,
12420 					    tp->sackblks[0].end);
12421 				} else {
12422 					tcp_update_dsack_list(tp, save_start,
12423 					    save_start + save_tlen);
12424 				}
12425 			} else if (tlen >= save_tlen) {
12426 				/* Update of sackblks. */
12427 				tcp_update_dsack_list(tp, save_start,
12428 				    save_start + save_tlen);
12429 			} else if (tlen > 0) {
12430 				tcp_update_dsack_list(tp, save_start,
12431 				    save_start + tlen);
12432 			}
12433 		}
12434 	} else {
12435 		m_freem(m);
12436 		thflags &= ~TH_FIN;
12437 	}
12438 
12439 	/*
12440 	 * If FIN is received ACK the FIN and let the user know that the
12441 	 * connection is closing.
12442 	 */
12443 	if (thflags & TH_FIN) {
12444 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
12445 			/* The socket upcall is handled by socantrcvmore. */
12446 			socantrcvmore(so);
12447 			/*
12448 			 * If connection is half-synchronized (ie NEEDSYN
12449 			 * flag on) then delay ACK, so it may be piggybacked
12450 			 * when SYN is sent. Otherwise, since we received a
12451 			 * FIN then no more input can be expected, send ACK
12452 			 * now.
12453 			 */
12454 			if (tp->t_flags & TF_NEEDSYN) {
12455 				rack_timer_cancel(tp, rack,
12456 				    rack->r_ctl.rc_rcvtime, __LINE__);
12457 				tp->t_flags |= TF_DELACK;
12458 			} else {
12459 				tp->t_flags |= TF_ACKNOW;
12460 			}
12461 			tp->rcv_nxt++;
12462 		}
12463 		switch (tp->t_state) {
12464 			/*
12465 			 * In SYN_RECEIVED and ESTABLISHED STATES enter the
12466 			 * CLOSE_WAIT state.
12467 			 */
12468 		case TCPS_SYN_RECEIVED:
12469 			tp->t_starttime = ticks;
12470 			/* FALLTHROUGH */
12471 		case TCPS_ESTABLISHED:
12472 			rack_timer_cancel(tp, rack,
12473 			    rack->r_ctl.rc_rcvtime, __LINE__);
12474 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
12475 			break;
12476 
12477 			/*
12478 			 * If still in FIN_WAIT_1 STATE FIN has not been
12479 			 * acked so enter the CLOSING state.
12480 			 */
12481 		case TCPS_FIN_WAIT_1:
12482 			rack_timer_cancel(tp, rack,
12483 			    rack->r_ctl.rc_rcvtime, __LINE__);
12484 			tcp_state_change(tp, TCPS_CLOSING);
12485 			break;
12486 
12487 			/*
12488 			 * In FIN_WAIT_2 state enter the TIME_WAIT state,
12489 			 * starting the time-wait timer, turning off the
12490 			 * other standard timers.
12491 			 */
12492 		case TCPS_FIN_WAIT_2:
12493 			rack_timer_cancel(tp, rack,
12494 			    rack->r_ctl.rc_rcvtime, __LINE__);
12495 			tcp_twstart(tp);
12496 			return (1);
12497 		}
12498 	}
12499 	/*
12500 	 * Return any desired output.
12501 	 */
12502 	if ((tp->t_flags & TF_ACKNOW) ||
12503 	    (sbavail(&so->so_snd) > (tp->snd_max - tp->snd_una))) {
12504 		rack->r_wanted_output = 1;
12505 	}
12506 	return (0);
12507 }
12508 
12509 /*
12510  * Here nothing is really faster, its just that we
12511  * have broken out the fast-data path also just like
12512  * the fast-ack.
12513  */
12514 static int
12515 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so,
12516     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12517     uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos)
12518 {
12519 	int32_t nsegs;
12520 	int32_t newsize = 0;	/* automatic sockbuf scaling */
12521 	struct tcp_rack *rack;
12522 #ifdef NETFLIX_SB_LIMITS
12523 	u_int mcnt, appended;
12524 #endif
12525 
12526 	/*
12527 	 * If last ACK falls within this segment's sequence numbers, record
12528 	 * the timestamp. NOTE that the test is modified according to the
12529 	 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
12530 	 */
12531 	if (__predict_false(th->th_seq != tp->rcv_nxt)) {
12532 		return (0);
12533 	}
12534 	if (tiwin && tiwin != tp->snd_wnd) {
12535 		return (0);
12536 	}
12537 	if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) {
12538 		return (0);
12539 	}
12540 	if (__predict_false((to->to_flags & TOF_TS) &&
12541 	    (TSTMP_LT(to->to_tsval, tp->ts_recent)))) {
12542 		return (0);
12543 	}
12544 	if (__predict_false((th->th_ack != tp->snd_una))) {
12545 		return (0);
12546 	}
12547 	if (__predict_false(tlen > sbspace(&so->so_rcv))) {
12548 		return (0);
12549 	}
12550 	if ((to->to_flags & TOF_TS) != 0 &&
12551 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
12552 		tp->ts_recent_age = tcp_ts_getticks();
12553 		tp->ts_recent = to->to_tsval;
12554 	}
12555 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12556 	/*
12557 	 * This is a pure, in-sequence data packet with nothing on the
12558 	 * reassembly queue and we have enough buffer space to take it.
12559 	 */
12560 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
12561 
12562 #ifdef NETFLIX_SB_LIMITS
12563 	if (so->so_rcv.sb_shlim) {
12564 		mcnt = m_memcnt(m);
12565 		appended = 0;
12566 		if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
12567 		    CFO_NOSLEEP, NULL) == false) {
12568 			counter_u64_add(tcp_sb_shlim_fails, 1);
12569 			m_freem(m);
12570 			return (1);
12571 		}
12572 	}
12573 #endif
12574 	/* Clean receiver SACK report if present */
12575 	if (tp->rcv_numsacks)
12576 		tcp_clean_sackreport(tp);
12577 	KMOD_TCPSTAT_INC(tcps_preddat);
12578 	tp->rcv_nxt += tlen;
12579 	if (tlen &&
12580 	    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
12581 	    (tp->t_fbyte_in == 0)) {
12582 		tp->t_fbyte_in = ticks;
12583 		if (tp->t_fbyte_in == 0)
12584 			tp->t_fbyte_in = 1;
12585 		if (tp->t_fbyte_out && tp->t_fbyte_in)
12586 			tp->t_flags2 |= TF2_FBYTES_COMPLETE;
12587 	}
12588 	/*
12589 	 * Pull snd_wl1 up to prevent seq wrap relative to th_seq.
12590 	 */
12591 	tp->snd_wl1 = th->th_seq;
12592 	/*
12593 	 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt.
12594 	 */
12595 	tp->rcv_up = tp->rcv_nxt;
12596 	KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs);
12597 	KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
12598 	newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
12599 
12600 	/* Add data to socket buffer. */
12601 	SOCK_RECVBUF_LOCK(so);
12602 	if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
12603 		m_freem(m);
12604 	} else {
12605 		/*
12606 		 * Set new socket buffer size. Give up when limit is
12607 		 * reached.
12608 		 */
12609 		if (newsize)
12610 			if (!sbreserve_locked(so, SO_RCV, newsize, NULL))
12611 				so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
12612 		m_adj(m, drop_hdrlen);	/* delayed header drop */
12613 #ifdef NETFLIX_SB_LIMITS
12614 		appended =
12615 #endif
12616 			sbappendstream_locked(&so->so_rcv, m, 0);
12617 		ctf_calc_rwin(so, tp);
12618 	}
12619 	rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1);
12620 	/* NB: sorwakeup_locked() does an implicit unlock. */
12621 	sorwakeup_locked(so);
12622 #ifdef NETFLIX_SB_LIMITS
12623 	if (so->so_rcv.sb_shlim && mcnt != appended)
12624 		counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended);
12625 #endif
12626 	rack_handle_delayed_ack(tp, rack, tlen, 0);
12627 	if (tp->snd_una == tp->snd_max)
12628 		sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
12629 	return (1);
12630 }
12631 
12632 /*
12633  * This subfunction is used to try to highly optimize the
12634  * fast path. We again allow window updates that are
12635  * in sequence to remain in the fast-path. We also add
12636  * in the __predict's to attempt to help the compiler.
12637  * Note that if we return a 0, then we can *not* process
12638  * it and the caller should push the packet into the
12639  * slow-path.
12640  */
12641 static int
12642 rack_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
12643     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12644     uint32_t tiwin, int32_t nxt_pkt, uint32_t cts)
12645 {
12646 	int32_t acked;
12647 	int32_t nsegs;
12648 	int32_t under_pacing = 0;
12649 	struct tcp_rack *rack;
12650 
12651 	if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
12652 		/* Old ack, behind (or duplicate to) the last one rcv'd */
12653 		return (0);
12654 	}
12655 	if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) {
12656 		/* Above what we have sent? */
12657 		return (0);
12658 	}
12659 	if (__predict_false(tiwin == 0)) {
12660 		/* zero window */
12661 		return (0);
12662 	}
12663 	if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) {
12664 		/* We need a SYN or a FIN, unlikely.. */
12665 		return (0);
12666 	}
12667 	if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) {
12668 		/* Timestamp is behind .. old ack with seq wrap? */
12669 		return (0);
12670 	}
12671 	if (__predict_false(IN_RECOVERY(tp->t_flags))) {
12672 		/* Still recovering */
12673 		return (0);
12674 	}
12675 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12676 	if (rack->r_ctl.rc_sacked) {
12677 		/* We have sack holes on our scoreboard */
12678 		return (0);
12679 	}
12680 	/* Ok if we reach here, we can process a fast-ack */
12681 	if (rack->gp_ready &&
12682 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
12683 		under_pacing = 1;
12684 	}
12685 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
12686 	rack_log_ack(tp, to, th, 0, 0, NULL, NULL);
12687 	/* Did the window get updated? */
12688 	if (tiwin != tp->snd_wnd) {
12689 		tp->snd_wnd = tiwin;
12690 		rack_validate_fo_sendwin_up(tp, rack);
12691 		tp->snd_wl1 = th->th_seq;
12692 		if (tp->snd_wnd > tp->max_sndwnd)
12693 			tp->max_sndwnd = tp->snd_wnd;
12694 	}
12695 	/* Do we exit persists? */
12696 	if ((rack->rc_in_persist != 0) &&
12697 	    (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
12698 			       rack->r_ctl.rc_pace_min_segs))) {
12699 		rack_exit_persist(tp, rack, cts);
12700 	}
12701 	/* Do we enter persists? */
12702 	if ((rack->rc_in_persist == 0) &&
12703 	    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
12704 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
12705 	    ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) &&
12706 	    sbavail(&tptosocket(tp)->so_snd) &&
12707 	    (sbavail(&tptosocket(tp)->so_snd) > tp->snd_wnd)) {
12708 		/*
12709 		 * Here the rwnd is less than
12710 		 * the pacing size, we are established,
12711 		 * nothing is outstanding, and there is
12712 		 * data to send. Enter persists.
12713 		 */
12714 		rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, th->th_ack);
12715 	}
12716 	/*
12717 	 * If last ACK falls within this segment's sequence numbers, record
12718 	 * the timestamp. NOTE that the test is modified according to the
12719 	 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
12720 	 */
12721 	if ((to->to_flags & TOF_TS) != 0 &&
12722 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
12723 		tp->ts_recent_age = tcp_ts_getticks();
12724 		tp->ts_recent = to->to_tsval;
12725 	}
12726 	/*
12727 	 * This is a pure ack for outstanding data.
12728 	 */
12729 	KMOD_TCPSTAT_INC(tcps_predack);
12730 
12731 	/*
12732 	 * "bad retransmit" recovery.
12733 	 */
12734 	if ((tp->t_flags & TF_PREVVALID) &&
12735 	    ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
12736 		tp->t_flags &= ~TF_PREVVALID;
12737 		if (tp->t_rxtshift == 1 &&
12738 		    (int)(ticks - tp->t_badrxtwin) < 0)
12739 			rack_cong_signal(tp, CC_RTO_ERR, th->th_ack, __LINE__);
12740 	}
12741 	/*
12742 	 * Recalculate the transmit timer / rtt.
12743 	 *
12744 	 * Some boxes send broken timestamp replies during the SYN+ACK
12745 	 * phase, ignore timestamps of 0 or we could calculate a huge RTT
12746 	 * and blow up the retransmit timer.
12747 	 */
12748 	acked = BYTES_THIS_ACK(tp, th);
12749 
12750 #ifdef TCP_HHOOK
12751 	/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
12752 	hhook_run_tcp_est_in(tp, th, to);
12753 #endif
12754 	KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
12755 	KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
12756 	if (acked) {
12757 		struct mbuf *mfree;
12758 
12759 		rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, 0);
12760 		SOCK_SENDBUF_LOCK(so);
12761 		mfree = sbcut_locked(&so->so_snd, acked);
12762 		tp->snd_una = th->th_ack;
12763 		/* Note we want to hold the sb lock through the sendmap adjust */
12764 		rack_adjust_sendmap_head(rack, &so->so_snd);
12765 		/* Wake up the socket if we have room to write more */
12766 		rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
12767 		sowwakeup_locked(so);
12768 		m_freem(mfree);
12769 		tp->t_rxtshift = 0;
12770 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
12771 			      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
12772 		rack->rc_tlp_in_progress = 0;
12773 		rack->r_ctl.rc_tlp_cnt_out = 0;
12774 		/*
12775 		 * If it is the RXT timer we want to
12776 		 * stop it, so we can restart a TLP.
12777 		 */
12778 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
12779 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
12780 
12781 #ifdef TCP_REQUEST_TRK
12782 		rack_req_check_for_comp(rack, th->th_ack);
12783 #endif
12784 	}
12785 	/*
12786 	 * Let the congestion control algorithm update congestion control
12787 	 * related information. This typically means increasing the
12788 	 * congestion window.
12789 	 */
12790 	if (tp->snd_wnd < ctf_outstanding(tp)) {
12791 		/* The peer collapsed the window */
12792 		rack_collapsed_window(rack, ctf_outstanding(tp), th->th_ack, __LINE__);
12793 	} else if (rack->rc_has_collapsed)
12794 		rack_un_collapse_window(rack, __LINE__);
12795 	if ((rack->r_collapse_point_valid) &&
12796 	    (SEQ_GT(tp->snd_una, rack->r_ctl.high_collapse_point)))
12797 		rack->r_collapse_point_valid = 0;
12798 	/*
12799 	 * Pull snd_wl2 up to prevent seq wrap relative to th_ack.
12800 	 */
12801 	tp->snd_wl2 = th->th_ack;
12802 	tp->t_dupacks = 0;
12803 	m_freem(m);
12804 	/* ND6_HINT(tp);	 *//* Some progress has been made. */
12805 
12806 	/*
12807 	 * If all outstanding data are acked, stop retransmit timer,
12808 	 * otherwise restart timer using current (possibly backed-off)
12809 	 * value. If process is waiting for space, wakeup/selwakeup/signal.
12810 	 * If data are ready to send, let tcp_output decide between more
12811 	 * output or persist.
12812 	 */
12813 	if (under_pacing &&
12814 	    (rack->use_fixed_rate == 0) &&
12815 	    (rack->in_probe_rtt == 0) &&
12816 	    rack->rc_gp_dyn_mul &&
12817 	    rack->rc_always_pace) {
12818 		/* Check if we are dragging bottom */
12819 		rack_check_bottom_drag(tp, rack, so);
12820 	}
12821 	if (tp->snd_una == tp->snd_max) {
12822 		tp->t_flags &= ~TF_PREVVALID;
12823 		rack->r_ctl.retran_during_recovery = 0;
12824 		rack->rc_suspicious = 0;
12825 		rack->r_ctl.dsack_byte_cnt = 0;
12826 		rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
12827 		if (rack->r_ctl.rc_went_idle_time == 0)
12828 			rack->r_ctl.rc_went_idle_time = 1;
12829 		rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
12830 		if (sbavail(&tptosocket(tp)->so_snd) == 0)
12831 			tp->t_acktime = 0;
12832 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
12833 	}
12834 	if (acked && rack->r_fast_output)
12835 		rack_gain_for_fastoutput(rack, tp, so, (uint32_t)acked);
12836 	if (sbavail(&so->so_snd)) {
12837 		rack->r_wanted_output = 1;
12838 	}
12839 	return (1);
12840 }
12841 
12842 /*
12843  * Return value of 1, the TCB is unlocked and most
12844  * likely gone, return value of 0, the TCP is still
12845  * locked.
12846  */
12847 static int
12848 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so,
12849     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12850     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
12851 {
12852 	int32_t ret_val = 0;
12853 	int32_t orig_tlen = tlen;
12854 	int32_t todrop;
12855 	int32_t ourfinisacked = 0;
12856 	struct tcp_rack *rack;
12857 
12858 	INP_WLOCK_ASSERT(tptoinpcb(tp));
12859 
12860 	ctf_calc_rwin(so, tp);
12861 	/*
12862 	 * If the state is SYN_SENT: if seg contains an ACK, but not for our
12863 	 * SYN, drop the input. if seg contains a RST, then drop the
12864 	 * connection. if seg does not contain SYN, then drop it. Otherwise
12865 	 * this is an acceptable SYN segment initialize tp->rcv_nxt and
12866 	 * tp->irs if seg contains ack then advance tp->snd_una if seg
12867 	 * contains an ECE and ECN support is enabled, the stream is ECN
12868 	 * capable. if SYN has been acked change to ESTABLISHED else
12869 	 * SYN_RCVD state arrange for segment to be acked (eventually)
12870 	 * continue processing rest of data/controls.
12871 	 */
12872 	if ((thflags & TH_ACK) &&
12873 	    (SEQ_LEQ(th->th_ack, tp->iss) ||
12874 	    SEQ_GT(th->th_ack, tp->snd_max))) {
12875 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
12876 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
12877 		return (1);
12878 	}
12879 	if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) {
12880 		TCP_PROBE5(connect__refused, NULL, tp,
12881 		    mtod(m, const char *), tp, th);
12882 		tp = tcp_drop(tp, ECONNREFUSED);
12883 		ctf_do_drop(m, tp);
12884 		return (1);
12885 	}
12886 	if (thflags & TH_RST) {
12887 		ctf_do_drop(m, tp);
12888 		return (1);
12889 	}
12890 	if (!(thflags & TH_SYN)) {
12891 		ctf_do_drop(m, tp);
12892 		return (1);
12893 	}
12894 	tp->irs = th->th_seq;
12895 	tcp_rcvseqinit(tp);
12896 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12897 	if (thflags & TH_ACK) {
12898 		int tfo_partial = 0;
12899 
12900 		KMOD_TCPSTAT_INC(tcps_connects);
12901 		soisconnected(so);
12902 #ifdef MAC
12903 		mac_socketpeer_set_from_mbuf(m, so);
12904 #endif
12905 		/* Do window scaling on this connection? */
12906 		if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
12907 		    (TF_RCVD_SCALE | TF_REQ_SCALE)) {
12908 			tp->rcv_scale = tp->request_r_scale;
12909 		}
12910 		tp->rcv_adv += min(tp->rcv_wnd,
12911 		    TCP_MAXWIN << tp->rcv_scale);
12912 		/*
12913 		 * If not all the data that was sent in the TFO SYN
12914 		 * has been acked, resend the remainder right away.
12915 		 */
12916 		if ((tp->t_flags & TF_FASTOPEN) &&
12917 		    (tp->snd_una != tp->snd_max)) {
12918 			/* Was it a partial ack? */
12919 			if (SEQ_LT(th->th_ack, tp->snd_max))
12920 				tfo_partial = 1;
12921 		}
12922 		/*
12923 		 * If there's data, delay ACK; if there's also a FIN ACKNOW
12924 		 * will be turned on later.
12925 		 */
12926 		if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial) {
12927 			rack_timer_cancel(tp, rack,
12928 					  rack->r_ctl.rc_rcvtime, __LINE__);
12929 			tp->t_flags |= TF_DELACK;
12930 		} else {
12931 			rack->r_wanted_output = 1;
12932 			tp->t_flags |= TF_ACKNOW;
12933 		}
12934 
12935 		tcp_ecn_input_syn_sent(tp, thflags, iptos);
12936 
12937 		if (SEQ_GT(th->th_ack, tp->snd_una)) {
12938 			/*
12939 			 * We advance snd_una for the
12940 			 * fast open case. If th_ack is
12941 			 * acknowledging data beyond
12942 			 * snd_una we can't just call
12943 			 * ack-processing since the
12944 			 * data stream in our send-map
12945 			 * will start at snd_una + 1 (one
12946 			 * beyond the SYN). If its just
12947 			 * equal we don't need to do that
12948 			 * and there is no send_map.
12949 			 */
12950 			tp->snd_una++;
12951 			if (tfo_partial && (SEQ_GT(tp->snd_max, tp->snd_una))) {
12952 				/*
12953 				 * We sent a SYN with data, and thus have a
12954 				 * sendmap entry with a SYN set. Lets find it
12955 				 * and take off the send bit and the byte and
12956 				 * set it up to be what we send (send it next).
12957 				 */
12958 				struct rack_sendmap *rsm;
12959 
12960 				rsm = tqhash_min(rack->r_ctl.tqh);
12961 				if (rsm) {
12962 					if (rsm->r_flags & RACK_HAS_SYN) {
12963 						rsm->r_flags &= ~RACK_HAS_SYN;
12964 						rsm->r_start++;
12965 					}
12966 					rack->r_ctl.rc_resend = rsm;
12967 				}
12968 			}
12969 		}
12970 		/*
12971 		 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions:
12972 		 * SYN_SENT  --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1
12973 		 */
12974 		tp->t_starttime = ticks;
12975 		if (tp->t_flags & TF_NEEDFIN) {
12976 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
12977 			tp->t_flags &= ~TF_NEEDFIN;
12978 			thflags &= ~TH_SYN;
12979 		} else {
12980 			tcp_state_change(tp, TCPS_ESTABLISHED);
12981 			TCP_PROBE5(connect__established, NULL, tp,
12982 			    mtod(m, const char *), tp, th);
12983 			rack_cc_conn_init(tp);
12984 		}
12985 	} else {
12986 		/*
12987 		 * Received initial SYN in SYN-SENT[*] state => simultaneous
12988 		 * open.  If segment contains CC option and there is a
12989 		 * cached CC, apply TAO test. If it succeeds, connection is *
12990 		 * half-synchronized. Otherwise, do 3-way handshake:
12991 		 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If
12992 		 * there was no CC option, clear cached CC value.
12993 		 */
12994 		tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN);
12995 		tcp_state_change(tp, TCPS_SYN_RECEIVED);
12996 	}
12997 	/*
12998 	 * Advance th->th_seq to correspond to first data byte. If data,
12999 	 * trim to stay within window, dropping FIN if necessary.
13000 	 */
13001 	th->th_seq++;
13002 	if (tlen > tp->rcv_wnd) {
13003 		todrop = tlen - tp->rcv_wnd;
13004 		m_adj(m, -todrop);
13005 		tlen = tp->rcv_wnd;
13006 		thflags &= ~TH_FIN;
13007 		KMOD_TCPSTAT_INC(tcps_rcvpackafterwin);
13008 		KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
13009 	}
13010 	tp->snd_wl1 = th->th_seq - 1;
13011 	tp->rcv_up = th->th_seq;
13012 	/*
13013 	 * Client side of transaction: already sent SYN and data. If the
13014 	 * remote host used T/TCP to validate the SYN, our data will be
13015 	 * ACK'd; if so, enter normal data segment processing in the middle
13016 	 * of step 5, ack processing. Otherwise, goto step 6.
13017 	 */
13018 	if (thflags & TH_ACK) {
13019 		/* For syn-sent we need to possibly update the rtt */
13020 		if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) {
13021 			uint32_t t, mcts;
13022 
13023 			mcts = tcp_ts_getticks();
13024 			t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC;
13025 			if (!tp->t_rttlow || tp->t_rttlow > t)
13026 				tp->t_rttlow = t;
13027 			rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 4);
13028 			tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2);
13029 			tcp_rack_xmit_timer_commit(rack, tp);
13030 		}
13031 		if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen))
13032 			return (ret_val);
13033 		/* We may have changed to FIN_WAIT_1 above */
13034 		if (tp->t_state == TCPS_FIN_WAIT_1) {
13035 			/*
13036 			 * In FIN_WAIT_1 STATE in addition to the processing
13037 			 * for the ESTABLISHED state if our FIN is now
13038 			 * acknowledged then enter FIN_WAIT_2.
13039 			 */
13040 			if (ourfinisacked) {
13041 				/*
13042 				 * If we can't receive any more data, then
13043 				 * closing user can proceed. Starting the
13044 				 * timer is contrary to the specification,
13045 				 * but if we don't get a FIN we'll hang
13046 				 * forever.
13047 				 *
13048 				 * XXXjl: we should release the tp also, and
13049 				 * use a compressed state.
13050 				 */
13051 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
13052 					soisdisconnected(so);
13053 					tcp_timer_activate(tp, TT_2MSL,
13054 					    (tcp_fast_finwait2_recycle ?
13055 					    tcp_finwait2_timeout :
13056 					    TP_MAXIDLE(tp)));
13057 				}
13058 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
13059 			}
13060 		}
13061 	}
13062 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13063 	   tiwin, thflags, nxt_pkt));
13064 }
13065 
13066 /*
13067  * Return value of 1, the TCB is unlocked and most
13068  * likely gone, return value of 0, the TCP is still
13069  * locked.
13070  */
13071 static int
13072 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so,
13073     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13074     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13075 {
13076 	struct tcp_rack *rack;
13077 	int32_t orig_tlen = tlen;
13078 	int32_t ret_val = 0;
13079 	int32_t ourfinisacked = 0;
13080 
13081 	rack = (struct tcp_rack *)tp->t_fb_ptr;
13082 	ctf_calc_rwin(so, tp);
13083 	if ((thflags & TH_RST) ||
13084 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
13085 		return (ctf_process_rst(m, th, so, tp));
13086 	if ((thflags & TH_ACK) &&
13087 	    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
13088 	    SEQ_GT(th->th_ack, tp->snd_max))) {
13089 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
13090 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13091 		return (1);
13092 	}
13093 	if (tp->t_flags & TF_FASTOPEN) {
13094 		/*
13095 		 * When a TFO connection is in SYN_RECEIVED, the
13096 		 * only valid packets are the initial SYN, a
13097 		 * retransmit/copy of the initial SYN (possibly with
13098 		 * a subset of the original data), a valid ACK, a
13099 		 * FIN, or a RST.
13100 		 */
13101 		if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) {
13102 			tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
13103 			ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13104 			return (1);
13105 		} else if (thflags & TH_SYN) {
13106 			/* non-initial SYN is ignored */
13107 			if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) ||
13108 			    (rack->r_ctl.rc_hpts_flags & PACE_TMR_TLP) ||
13109 			    (rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) {
13110 				ctf_do_drop(m, NULL);
13111 				return (0);
13112 			}
13113 		} else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) {
13114 			ctf_do_drop(m, NULL);
13115 			return (0);
13116 		}
13117 	}
13118 
13119 	/*
13120 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13121 	 * it's less than ts_recent, drop it.
13122 	 */
13123 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13124 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13125 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13126 			return (ret_val);
13127 	}
13128 	/*
13129 	 * In the SYN-RECEIVED state, validate that the packet belongs to
13130 	 * this connection before trimming the data to fit the receive
13131 	 * window.  Check the sequence number versus IRS since we know the
13132 	 * sequence numbers haven't wrapped.  This is a partial fix for the
13133 	 * "LAND" DoS attack.
13134 	 */
13135 	if (SEQ_LT(th->th_seq, tp->irs)) {
13136 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
13137 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13138 		return (1);
13139 	}
13140 	if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
13141 		return (ret_val);
13142 	}
13143 	/*
13144 	 * If last ACK falls within this segment's sequence numbers, record
13145 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
13146 	 * from the latest proposal of the tcplw@cray.com list (Braden
13147 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
13148 	 * with our earlier PAWS tests, so this check should be solely
13149 	 * predicated on the sequence space of this segment. 3) That we
13150 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13151 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13152 	 * SEG.Len, This modified check allows us to overcome RFC1323's
13153 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13154 	 * p.869. In such cases, we can still calculate the RTT correctly
13155 	 * when RCV.NXT == Last.ACK.Sent.
13156 	 */
13157 	if ((to->to_flags & TOF_TS) != 0 &&
13158 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13159 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13160 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13161 		tp->ts_recent_age = tcp_ts_getticks();
13162 		tp->ts_recent = to->to_tsval;
13163 	}
13164 	tp->snd_wnd = tiwin;
13165 	rack_validate_fo_sendwin_up(tp, rack);
13166 	/*
13167 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
13168 	 * is on (half-synchronized state), then queue data for later
13169 	 * processing; else drop segment and return.
13170 	 */
13171 	if ((thflags & TH_ACK) == 0) {
13172 		if (tp->t_flags & TF_FASTOPEN) {
13173 			rack_cc_conn_init(tp);
13174 		}
13175 		return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13176 		    tiwin, thflags, nxt_pkt));
13177 	}
13178 	KMOD_TCPSTAT_INC(tcps_connects);
13179 	if (tp->t_flags & TF_SONOTCONN) {
13180 		tp->t_flags &= ~TF_SONOTCONN;
13181 		soisconnected(so);
13182 	}
13183 	/* Do window scaling? */
13184 	if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
13185 	    (TF_RCVD_SCALE | TF_REQ_SCALE)) {
13186 		tp->rcv_scale = tp->request_r_scale;
13187 	}
13188 	/*
13189 	 * Make transitions: SYN-RECEIVED  -> ESTABLISHED SYN-RECEIVED* ->
13190 	 * FIN-WAIT-1
13191 	 */
13192 	tp->t_starttime = ticks;
13193 	if ((tp->t_flags & TF_FASTOPEN) && tp->t_tfo_pending) {
13194 		tcp_fastopen_decrement_counter(tp->t_tfo_pending);
13195 		tp->t_tfo_pending = NULL;
13196 	}
13197 	if (tp->t_flags & TF_NEEDFIN) {
13198 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
13199 		tp->t_flags &= ~TF_NEEDFIN;
13200 	} else {
13201 		tcp_state_change(tp, TCPS_ESTABLISHED);
13202 		TCP_PROBE5(accept__established, NULL, tp,
13203 		    mtod(m, const char *), tp, th);
13204 		/*
13205 		 * TFO connections call cc_conn_init() during SYN
13206 		 * processing.  Calling it again here for such connections
13207 		 * is not harmless as it would undo the snd_cwnd reduction
13208 		 * that occurs when a TFO SYN|ACK is retransmitted.
13209 		 */
13210 		if (!(tp->t_flags & TF_FASTOPEN))
13211 			rack_cc_conn_init(tp);
13212 	}
13213 	/*
13214 	 * Account for the ACK of our SYN prior to
13215 	 * regular ACK processing below, except for
13216 	 * simultaneous SYN, which is handled later.
13217 	 */
13218 	if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
13219 		tp->snd_una++;
13220 	/*
13221 	 * If segment contains data or ACK, will call tcp_reass() later; if
13222 	 * not, do so now to pass queued data to user.
13223 	 */
13224 	if (tlen == 0 && (thflags & TH_FIN) == 0) {
13225 		(void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
13226 		    (struct mbuf *)0);
13227 		if (tp->t_flags & TF_WAKESOR) {
13228 			tp->t_flags &= ~TF_WAKESOR;
13229 			/* NB: sorwakeup_locked() does an implicit unlock. */
13230 			sorwakeup_locked(so);
13231 		}
13232 	}
13233 	tp->snd_wl1 = th->th_seq - 1;
13234 	/* For syn-recv we need to possibly update the rtt */
13235 	if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) {
13236 		uint32_t t, mcts;
13237 
13238 		mcts = tcp_ts_getticks();
13239 		t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC;
13240 		if (!tp->t_rttlow || tp->t_rttlow > t)
13241 			tp->t_rttlow = t;
13242 		rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 5);
13243 		tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2);
13244 		tcp_rack_xmit_timer_commit(rack, tp);
13245 	}
13246 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) {
13247 		return (ret_val);
13248 	}
13249 	if (tp->t_state == TCPS_FIN_WAIT_1) {
13250 		/* We could have went to FIN_WAIT_1 (or EST) above */
13251 		/*
13252 		 * In FIN_WAIT_1 STATE in addition to the processing for the
13253 		 * ESTABLISHED state if our FIN is now acknowledged then
13254 		 * enter FIN_WAIT_2.
13255 		 */
13256 		if (ourfinisacked) {
13257 			/*
13258 			 * If we can't receive any more data, then closing
13259 			 * user can proceed. Starting the timer is contrary
13260 			 * to the specification, but if we don't get a FIN
13261 			 * we'll hang forever.
13262 			 *
13263 			 * XXXjl: we should release the tp also, and use a
13264 			 * compressed state.
13265 			 */
13266 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
13267 				soisdisconnected(so);
13268 				tcp_timer_activate(tp, TT_2MSL,
13269 				    (tcp_fast_finwait2_recycle ?
13270 				    tcp_finwait2_timeout :
13271 				    TP_MAXIDLE(tp)));
13272 			}
13273 			tcp_state_change(tp, TCPS_FIN_WAIT_2);
13274 		}
13275 	}
13276 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13277 	    tiwin, thflags, nxt_pkt));
13278 }
13279 
13280 /*
13281  * Return value of 1, the TCB is unlocked and most
13282  * likely gone, return value of 0, the TCP is still
13283  * locked.
13284  */
13285 static int
13286 rack_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so,
13287     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13288     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13289 {
13290 	int32_t ret_val = 0;
13291 	int32_t orig_tlen = tlen;
13292 	struct tcp_rack *rack;
13293 
13294 	/*
13295 	 * Header prediction: check for the two common cases of a
13296 	 * uni-directional data xfer.  If the packet has no control flags,
13297 	 * is in-sequence, the window didn't change and we're not
13298 	 * retransmitting, it's a candidate.  If the length is zero and the
13299 	 * ack moved forward, we're the sender side of the xfer.  Just free
13300 	 * the data acked & wake any higher level process that was blocked
13301 	 * waiting for space.  If the length is non-zero and the ack didn't
13302 	 * move, we're the receiver side.  If we're getting packets in-order
13303 	 * (the reassembly queue is empty), add the data toc The socket
13304 	 * buffer and note that we need a delayed ack. Make sure that the
13305 	 * hidden state-flags are also off. Since we check for
13306 	 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN.
13307 	 */
13308 	rack = (struct tcp_rack *)tp->t_fb_ptr;
13309 	if (__predict_true(((to->to_flags & TOF_SACK) == 0)) &&
13310 	    __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) == TH_ACK) &&
13311 	    __predict_true(SEGQ_EMPTY(tp)) &&
13312 	    __predict_true(th->th_seq == tp->rcv_nxt)) {
13313 		if (tlen == 0) {
13314 			if (rack_fastack(m, th, so, tp, to, drop_hdrlen, tlen,
13315 			    tiwin, nxt_pkt, rack->r_ctl.rc_rcvtime)) {
13316 				return (0);
13317 			}
13318 		} else {
13319 			if (rack_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen,
13320 			    tiwin, nxt_pkt, iptos)) {
13321 				return (0);
13322 			}
13323 		}
13324 	}
13325 	ctf_calc_rwin(so, tp);
13326 
13327 	if ((thflags & TH_RST) ||
13328 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
13329 		return (ctf_process_rst(m, th, so, tp));
13330 
13331 	/*
13332 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13333 	 * synchronized state.
13334 	 */
13335 	if (thflags & TH_SYN) {
13336 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13337 		return (ret_val);
13338 	}
13339 	/*
13340 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13341 	 * it's less than ts_recent, drop it.
13342 	 */
13343 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13344 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13345 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13346 			return (ret_val);
13347 	}
13348 	if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
13349 		return (ret_val);
13350 	}
13351 	/*
13352 	 * If last ACK falls within this segment's sequence numbers, record
13353 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
13354 	 * from the latest proposal of the tcplw@cray.com list (Braden
13355 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
13356 	 * with our earlier PAWS tests, so this check should be solely
13357 	 * predicated on the sequence space of this segment. 3) That we
13358 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13359 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13360 	 * SEG.Len, This modified check allows us to overcome RFC1323's
13361 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13362 	 * p.869. In such cases, we can still calculate the RTT correctly
13363 	 * when RCV.NXT == Last.ACK.Sent.
13364 	 */
13365 	if ((to->to_flags & TOF_TS) != 0 &&
13366 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13367 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13368 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13369 		tp->ts_recent_age = tcp_ts_getticks();
13370 		tp->ts_recent = to->to_tsval;
13371 	}
13372 	/*
13373 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
13374 	 * is on (half-synchronized state), then queue data for later
13375 	 * processing; else drop segment and return.
13376 	 */
13377 	if ((thflags & TH_ACK) == 0) {
13378 		if (tp->t_flags & TF_NEEDSYN) {
13379 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13380 			    tiwin, thflags, nxt_pkt));
13381 
13382 		} else if (tp->t_flags & TF_ACKNOW) {
13383 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
13384 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
13385 			return (ret_val);
13386 		} else {
13387 			ctf_do_drop(m, NULL);
13388 			return (0);
13389 		}
13390 	}
13391 	/*
13392 	 * Ack processing.
13393 	 */
13394 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val, orig_tlen)) {
13395 		return (ret_val);
13396 	}
13397 	if (sbavail(&so->so_snd)) {
13398 		if (ctf_progress_timeout_check(tp, true)) {
13399 			rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
13400 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13401 			return (1);
13402 		}
13403 	}
13404 	/* State changes only happen in rack_process_data() */
13405 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13406 	    tiwin, thflags, nxt_pkt));
13407 }
13408 
13409 /*
13410  * Return value of 1, the TCB is unlocked and most
13411  * likely gone, return value of 0, the TCP is still
13412  * locked.
13413  */
13414 static int
13415 rack_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so,
13416     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13417     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13418 {
13419 	int32_t ret_val = 0;
13420 	int32_t orig_tlen = tlen;
13421 
13422 	ctf_calc_rwin(so, tp);
13423 	if ((thflags & TH_RST) ||
13424 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
13425 		return (ctf_process_rst(m, th, so, tp));
13426 	/*
13427 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13428 	 * synchronized state.
13429 	 */
13430 	if (thflags & TH_SYN) {
13431 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13432 		return (ret_val);
13433 	}
13434 	/*
13435 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13436 	 * it's less than ts_recent, drop it.
13437 	 */
13438 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13439 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13440 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13441 			return (ret_val);
13442 	}
13443 	if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
13444 		return (ret_val);
13445 	}
13446 	/*
13447 	 * If last ACK falls within this segment's sequence numbers, record
13448 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
13449 	 * from the latest proposal of the tcplw@cray.com list (Braden
13450 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
13451 	 * with our earlier PAWS tests, so this check should be solely
13452 	 * predicated on the sequence space of this segment. 3) That we
13453 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13454 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13455 	 * SEG.Len, This modified check allows us to overcome RFC1323's
13456 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13457 	 * p.869. In such cases, we can still calculate the RTT correctly
13458 	 * when RCV.NXT == Last.ACK.Sent.
13459 	 */
13460 	if ((to->to_flags & TOF_TS) != 0 &&
13461 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13462 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13463 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13464 		tp->ts_recent_age = tcp_ts_getticks();
13465 		tp->ts_recent = to->to_tsval;
13466 	}
13467 	/*
13468 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
13469 	 * is on (half-synchronized state), then queue data for later
13470 	 * processing; else drop segment and return.
13471 	 */
13472 	if ((thflags & TH_ACK) == 0) {
13473 		if (tp->t_flags & TF_NEEDSYN) {
13474 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13475 			    tiwin, thflags, nxt_pkt));
13476 
13477 		} else if (tp->t_flags & TF_ACKNOW) {
13478 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
13479 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
13480 			return (ret_val);
13481 		} else {
13482 			ctf_do_drop(m, NULL);
13483 			return (0);
13484 		}
13485 	}
13486 	/*
13487 	 * Ack processing.
13488 	 */
13489 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val, orig_tlen)) {
13490 		return (ret_val);
13491 	}
13492 	if (sbavail(&so->so_snd)) {
13493 		if (ctf_progress_timeout_check(tp, true)) {
13494 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
13495 						tp, tick, PROGRESS_DROP, __LINE__);
13496 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13497 			return (1);
13498 		}
13499 	}
13500 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13501 	    tiwin, thflags, nxt_pkt));
13502 }
13503 
13504 static int
13505 rack_check_data_after_close(struct mbuf *m,
13506     struct tcpcb *tp, int32_t *tlen, struct tcphdr *th, struct socket *so)
13507 {
13508 	struct tcp_rack *rack;
13509 
13510 	rack = (struct tcp_rack *)tp->t_fb_ptr;
13511 	if (rack->rc_allow_data_af_clo == 0) {
13512 	close_now:
13513 		tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
13514 		/* tcp_close will kill the inp pre-log the Reset */
13515 		tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
13516 		tp = tcp_close(tp);
13517 		KMOD_TCPSTAT_INC(tcps_rcvafterclose);
13518 		ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen));
13519 		return (1);
13520 	}
13521 	if (sbavail(&so->so_snd) == 0)
13522 		goto close_now;
13523 	/* Ok we allow data that is ignored and a followup reset */
13524 	tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
13525 	tp->rcv_nxt = th->th_seq + *tlen;
13526 	tp->t_flags2 |= TF2_DROP_AF_DATA;
13527 	rack->r_wanted_output = 1;
13528 	*tlen = 0;
13529 	return (0);
13530 }
13531 
13532 /*
13533  * Return value of 1, the TCB is unlocked and most
13534  * likely gone, return value of 0, the TCP is still
13535  * locked.
13536  */
13537 static int
13538 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so,
13539     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13540     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13541 {
13542 	int32_t ret_val = 0;
13543 	int32_t orig_tlen = tlen;
13544 	int32_t ourfinisacked = 0;
13545 
13546 	ctf_calc_rwin(so, tp);
13547 
13548 	if ((thflags & TH_RST) ||
13549 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
13550 		return (ctf_process_rst(m, th, so, tp));
13551 	/*
13552 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13553 	 * synchronized state.
13554 	 */
13555 	if (thflags & TH_SYN) {
13556 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13557 		return (ret_val);
13558 	}
13559 	/*
13560 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13561 	 * it's less than ts_recent, drop it.
13562 	 */
13563 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13564 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13565 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13566 			return (ret_val);
13567 	}
13568 	if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
13569 		return (ret_val);
13570 	}
13571 	/*
13572 	 * If new data are received on a connection after the user processes
13573 	 * are gone, then RST the other end.
13574 	 */
13575 	if ((tp->t_flags & TF_CLOSED) && tlen &&
13576 	    rack_check_data_after_close(m, tp, &tlen, th, so))
13577 		return (1);
13578 	/*
13579 	 * If last ACK falls within this segment's sequence numbers, record
13580 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
13581 	 * from the latest proposal of the tcplw@cray.com list (Braden
13582 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
13583 	 * with our earlier PAWS tests, so this check should be solely
13584 	 * predicated on the sequence space of this segment. 3) That we
13585 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13586 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13587 	 * SEG.Len, This modified check allows us to overcome RFC1323's
13588 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13589 	 * p.869. In such cases, we can still calculate the RTT correctly
13590 	 * when RCV.NXT == Last.ACK.Sent.
13591 	 */
13592 	if ((to->to_flags & TOF_TS) != 0 &&
13593 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13594 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13595 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13596 		tp->ts_recent_age = tcp_ts_getticks();
13597 		tp->ts_recent = to->to_tsval;
13598 	}
13599 	/*
13600 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
13601 	 * is on (half-synchronized state), then queue data for later
13602 	 * processing; else drop segment and return.
13603 	 */
13604 	if ((thflags & TH_ACK) == 0) {
13605 		if (tp->t_flags & TF_NEEDSYN) {
13606 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13607 			    tiwin, thflags, nxt_pkt));
13608 		} else if (tp->t_flags & TF_ACKNOW) {
13609 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
13610 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
13611 			return (ret_val);
13612 		} else {
13613 			ctf_do_drop(m, NULL);
13614 			return (0);
13615 		}
13616 	}
13617 	/*
13618 	 * Ack processing.
13619 	 */
13620 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) {
13621 		return (ret_val);
13622 	}
13623 	if (ourfinisacked) {
13624 		/*
13625 		 * If we can't receive any more data, then closing user can
13626 		 * proceed. Starting the timer is contrary to the
13627 		 * specification, but if we don't get a FIN we'll hang
13628 		 * forever.
13629 		 *
13630 		 * XXXjl: we should release the tp also, and use a
13631 		 * compressed state.
13632 		 */
13633 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
13634 			soisdisconnected(so);
13635 			tcp_timer_activate(tp, TT_2MSL,
13636 			    (tcp_fast_finwait2_recycle ?
13637 			    tcp_finwait2_timeout :
13638 			    TP_MAXIDLE(tp)));
13639 		}
13640 		tcp_state_change(tp, TCPS_FIN_WAIT_2);
13641 	}
13642 	if (sbavail(&so->so_snd)) {
13643 		if (ctf_progress_timeout_check(tp, true)) {
13644 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
13645 						tp, tick, PROGRESS_DROP, __LINE__);
13646 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13647 			return (1);
13648 		}
13649 	}
13650 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13651 	    tiwin, thflags, nxt_pkt));
13652 }
13653 
13654 /*
13655  * Return value of 1, the TCB is unlocked and most
13656  * likely gone, return value of 0, the TCP is still
13657  * locked.
13658  */
13659 static int
13660 rack_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so,
13661     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13662     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13663 {
13664 	int32_t ret_val = 0;
13665 	int32_t orig_tlen = tlen;
13666 	int32_t ourfinisacked = 0;
13667 
13668 	ctf_calc_rwin(so, tp);
13669 
13670 	if ((thflags & TH_RST) ||
13671 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
13672 		return (ctf_process_rst(m, th, so, tp));
13673 	/*
13674 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13675 	 * synchronized state.
13676 	 */
13677 	if (thflags & TH_SYN) {
13678 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13679 		return (ret_val);
13680 	}
13681 	/*
13682 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13683 	 * it's less than ts_recent, drop it.
13684 	 */
13685 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13686 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13687 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13688 			return (ret_val);
13689 	}
13690 	if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
13691 		return (ret_val);
13692 	}
13693 	/*
13694 	 * If last ACK falls within this segment's sequence numbers, record
13695 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
13696 	 * from the latest proposal of the tcplw@cray.com list (Braden
13697 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
13698 	 * with our earlier PAWS tests, so this check should be solely
13699 	 * predicated on the sequence space of this segment. 3) That we
13700 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13701 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13702 	 * SEG.Len, This modified check allows us to overcome RFC1323's
13703 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13704 	 * p.869. In such cases, we can still calculate the RTT correctly
13705 	 * when RCV.NXT == Last.ACK.Sent.
13706 	 */
13707 	if ((to->to_flags & TOF_TS) != 0 &&
13708 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13709 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13710 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13711 		tp->ts_recent_age = tcp_ts_getticks();
13712 		tp->ts_recent = to->to_tsval;
13713 	}
13714 	/*
13715 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
13716 	 * is on (half-synchronized state), then queue data for later
13717 	 * processing; else drop segment and return.
13718 	 */
13719 	if ((thflags & TH_ACK) == 0) {
13720 		if (tp->t_flags & TF_NEEDSYN) {
13721 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13722 			    tiwin, thflags, nxt_pkt));
13723 		} else if (tp->t_flags & TF_ACKNOW) {
13724 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
13725 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
13726 			return (ret_val);
13727 		} else {
13728 			ctf_do_drop(m, NULL);
13729 			return (0);
13730 		}
13731 	}
13732 	/*
13733 	 * Ack processing.
13734 	 */
13735 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) {
13736 		return (ret_val);
13737 	}
13738 	if (ourfinisacked) {
13739 		tcp_twstart(tp);
13740 		m_freem(m);
13741 		return (1);
13742 	}
13743 	if (sbavail(&so->so_snd)) {
13744 		if (ctf_progress_timeout_check(tp, true)) {
13745 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
13746 						tp, tick, PROGRESS_DROP, __LINE__);
13747 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13748 			return (1);
13749 		}
13750 	}
13751 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13752 	    tiwin, thflags, nxt_pkt));
13753 }
13754 
13755 /*
13756  * Return value of 1, the TCB is unlocked and most
13757  * likely gone, return value of 0, the TCP is still
13758  * locked.
13759  */
13760 static int
13761 rack_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
13762     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13763     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13764 {
13765 	int32_t ret_val = 0;
13766 	int32_t orig_tlen;
13767 	int32_t ourfinisacked = 0;
13768 
13769 	ctf_calc_rwin(so, tp);
13770 
13771 	if ((thflags & TH_RST) ||
13772 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
13773 		return (ctf_process_rst(m, th, so, tp));
13774 	/*
13775 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13776 	 * synchronized state.
13777 	 */
13778 	if (thflags & TH_SYN) {
13779 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13780 		return (ret_val);
13781 	}
13782 	/*
13783 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13784 	 * it's less than ts_recent, drop it.
13785 	 */
13786 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13787 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13788 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13789 			return (ret_val);
13790 	}
13791 	orig_tlen = tlen;
13792 	if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
13793 		return (ret_val);
13794 	}
13795 	/*
13796 	 * If last ACK falls within this segment's sequence numbers, record
13797 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
13798 	 * from the latest proposal of the tcplw@cray.com list (Braden
13799 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
13800 	 * with our earlier PAWS tests, so this check should be solely
13801 	 * predicated on the sequence space of this segment. 3) That we
13802 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13803 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13804 	 * SEG.Len, This modified check allows us to overcome RFC1323's
13805 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13806 	 * p.869. In such cases, we can still calculate the RTT correctly
13807 	 * when RCV.NXT == Last.ACK.Sent.
13808 	 */
13809 	if ((to->to_flags & TOF_TS) != 0 &&
13810 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13811 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13812 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13813 		tp->ts_recent_age = tcp_ts_getticks();
13814 		tp->ts_recent = to->to_tsval;
13815 	}
13816 	/*
13817 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
13818 	 * is on (half-synchronized state), then queue data for later
13819 	 * processing; else drop segment and return.
13820 	 */
13821 	if ((thflags & TH_ACK) == 0) {
13822 		if (tp->t_flags & TF_NEEDSYN) {
13823 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13824 			    tiwin, thflags, nxt_pkt));
13825 		} else if (tp->t_flags & TF_ACKNOW) {
13826 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
13827 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
13828 			return (ret_val);
13829 		} else {
13830 			ctf_do_drop(m, NULL);
13831 			return (0);
13832 		}
13833 	}
13834 	/*
13835 	 * case TCPS_LAST_ACK: Ack processing.
13836 	 */
13837 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) {
13838 		return (ret_val);
13839 	}
13840 	if (ourfinisacked) {
13841 		tp = tcp_close(tp);
13842 		ctf_do_drop(m, tp);
13843 		return (1);
13844 	}
13845 	if (sbavail(&so->so_snd)) {
13846 		if (ctf_progress_timeout_check(tp, true)) {
13847 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
13848 						tp, tick, PROGRESS_DROP, __LINE__);
13849 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13850 			return (1);
13851 		}
13852 	}
13853 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13854 	    tiwin, thflags, nxt_pkt));
13855 }
13856 
13857 /*
13858  * Return value of 1, the TCB is unlocked and most
13859  * likely gone, return value of 0, the TCP is still
13860  * locked.
13861  */
13862 static int
13863 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so,
13864     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13865     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13866 {
13867 	int32_t ret_val = 0;
13868 	int32_t orig_tlen = tlen;
13869 	int32_t ourfinisacked = 0;
13870 
13871 	ctf_calc_rwin(so, tp);
13872 
13873 	/* Reset receive buffer auto scaling when not in bulk receive mode. */
13874 	if ((thflags & TH_RST) ||
13875 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
13876 		return (ctf_process_rst(m, th, so, tp));
13877 	/*
13878 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13879 	 * synchronized state.
13880 	 */
13881 	if (thflags & TH_SYN) {
13882 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13883 		return (ret_val);
13884 	}
13885 	/*
13886 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13887 	 * it's less than ts_recent, drop it.
13888 	 */
13889 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13890 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13891 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13892 			return (ret_val);
13893 	}
13894 	if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
13895 		return (ret_val);
13896 	}
13897 	/*
13898 	 * If new data are received on a connection after the user processes
13899 	 * are gone, then RST the other end.
13900 	 */
13901 	if ((tp->t_flags & TF_CLOSED) && tlen &&
13902 	    rack_check_data_after_close(m, tp, &tlen, th, so))
13903 		return (1);
13904 	/*
13905 	 * If last ACK falls within this segment's sequence numbers, record
13906 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
13907 	 * from the latest proposal of the tcplw@cray.com list (Braden
13908 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
13909 	 * with our earlier PAWS tests, so this check should be solely
13910 	 * predicated on the sequence space of this segment. 3) That we
13911 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13912 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13913 	 * SEG.Len, This modified check allows us to overcome RFC1323's
13914 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13915 	 * p.869. In such cases, we can still calculate the RTT correctly
13916 	 * when RCV.NXT == Last.ACK.Sent.
13917 	 */
13918 	if ((to->to_flags & TOF_TS) != 0 &&
13919 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13920 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13921 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13922 		tp->ts_recent_age = tcp_ts_getticks();
13923 		tp->ts_recent = to->to_tsval;
13924 	}
13925 	/*
13926 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
13927 	 * is on (half-synchronized state), then queue data for later
13928 	 * processing; else drop segment and return.
13929 	 */
13930 	if ((thflags & TH_ACK) == 0) {
13931 		if (tp->t_flags & TF_NEEDSYN) {
13932 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13933 			    tiwin, thflags, nxt_pkt));
13934 		} else if (tp->t_flags & TF_ACKNOW) {
13935 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
13936 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
13937 			return (ret_val);
13938 		} else {
13939 			ctf_do_drop(m, NULL);
13940 			return (0);
13941 		}
13942 	}
13943 	/*
13944 	 * Ack processing.
13945 	 */
13946 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) {
13947 		return (ret_val);
13948 	}
13949 	if (sbavail(&so->so_snd)) {
13950 		if (ctf_progress_timeout_check(tp, true)) {
13951 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
13952 						tp, tick, PROGRESS_DROP, __LINE__);
13953 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13954 			return (1);
13955 		}
13956 	}
13957 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13958 	    tiwin, thflags, nxt_pkt));
13959 }
13960 
13961 static void inline
13962 rack_clear_rate_sample(struct tcp_rack *rack)
13963 {
13964 	rack->r_ctl.rack_rs.rs_flags = RACK_RTT_EMPTY;
13965 	rack->r_ctl.rack_rs.rs_rtt_cnt = 0;
13966 	rack->r_ctl.rack_rs.rs_rtt_tot = 0;
13967 }
13968 
13969 static void
13970 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override)
13971 {
13972 	uint64_t bw_est, rate_wanted;
13973 	int chged = 0;
13974 	uint32_t user_max, orig_min, orig_max;
13975 
13976 #ifdef TCP_REQUEST_TRK
13977 	if (rack->rc_hybrid_mode &&
13978 	    (rack->r_ctl.rc_pace_max_segs != 0) &&
13979 	    (rack_hybrid_allow_set_maxseg == 1) &&
13980 	    (rack->r_ctl.rc_last_sft != NULL)) {
13981 		rack->r_ctl.rc_last_sft->hybrid_flags &= ~TCP_HYBRID_PACING_SETMSS;
13982 		return;
13983 	}
13984 #endif
13985 	orig_min = rack->r_ctl.rc_pace_min_segs;
13986 	orig_max = rack->r_ctl.rc_pace_max_segs;
13987 	user_max = ctf_fixed_maxseg(tp) * rack->rc_user_set_max_segs;
13988 	if (ctf_fixed_maxseg(tp) != rack->r_ctl.rc_pace_min_segs)
13989 		chged = 1;
13990 	rack->r_ctl.rc_pace_min_segs = ctf_fixed_maxseg(tp);
13991 	if (rack->use_fixed_rate || rack->rc_force_max_seg) {
13992 		if (user_max != rack->r_ctl.rc_pace_max_segs)
13993 			chged = 1;
13994 	}
13995 	if (rack->rc_force_max_seg) {
13996 		rack->r_ctl.rc_pace_max_segs = user_max;
13997 	} else if (rack->use_fixed_rate) {
13998 		bw_est = rack_get_bw(rack);
13999 		if ((rack->r_ctl.crte == NULL) ||
14000 		    (bw_est != rack->r_ctl.crte->rate)) {
14001 			rack->r_ctl.rc_pace_max_segs = user_max;
14002 		} else {
14003 			/* We are pacing right at the hardware rate */
14004 			uint32_t segsiz, pace_one;
14005 
14006 			if (rack_pace_one_seg ||
14007 			    (rack->r_ctl.rc_user_set_min_segs == 1))
14008 				pace_one = 1;
14009 			else
14010 				pace_one = 0;
14011 			segsiz = min(ctf_fixed_maxseg(tp),
14012 				     rack->r_ctl.rc_pace_min_segs);
14013 			rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size_w_divisor(
14014 				tp, bw_est, segsiz, pace_one,
14015 				rack->r_ctl.crte, NULL, rack->r_ctl.pace_len_divisor);
14016 		}
14017 	} else if (rack->rc_always_pace) {
14018 		if (rack->r_ctl.gp_bw ||
14019 		    rack->r_ctl.init_rate) {
14020 			/* We have a rate of some sort set */
14021 			uint32_t  orig;
14022 
14023 			bw_est = rack_get_bw(rack);
14024 			orig = rack->r_ctl.rc_pace_max_segs;
14025 			if (fill_override)
14026 				rate_wanted = *fill_override;
14027 			else
14028 				rate_wanted = rack_get_gp_est(rack);
14029 			if (rate_wanted) {
14030 				/* We have something */
14031 				rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack,
14032 										   rate_wanted,
14033 										   ctf_fixed_maxseg(rack->rc_tp));
14034 			} else
14035 				rack->r_ctl.rc_pace_max_segs = rack->r_ctl.rc_pace_min_segs;
14036 			if (orig != rack->r_ctl.rc_pace_max_segs)
14037 				chged = 1;
14038 		} else if ((rack->r_ctl.gp_bw == 0) &&
14039 			   (rack->r_ctl.rc_pace_max_segs == 0)) {
14040 			/*
14041 			 * If we have nothing limit us to bursting
14042 			 * out IW sized pieces.
14043 			 */
14044 			chged = 1;
14045 			rack->r_ctl.rc_pace_max_segs = rc_init_window(rack);
14046 		}
14047 	}
14048 	if (rack->r_ctl.rc_pace_max_segs > PACE_MAX_IP_BYTES) {
14049 		chged = 1;
14050 		rack->r_ctl.rc_pace_max_segs = PACE_MAX_IP_BYTES;
14051 	}
14052 	if (chged)
14053 		rack_log_type_pacing_sizes(tp, rack, orig_min, orig_max, line, 2);
14054 }
14055 
14056 
14057 static void
14058 rack_init_fsb_block(struct tcpcb *tp, struct tcp_rack *rack, int32_t flags)
14059 {
14060 #ifdef INET6
14061 	struct ip6_hdr *ip6 = NULL;
14062 #endif
14063 #ifdef INET
14064 	struct ip *ip = NULL;
14065 #endif
14066 	struct udphdr *udp = NULL;
14067 
14068 	/* Ok lets fill in the fast block, it can only be used with no IP options! */
14069 #ifdef INET6
14070 	if (rack->r_is_v6) {
14071 		rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
14072 		ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
14073 		if (tp->t_port) {
14074 			rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr);
14075 			udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
14076 			udp->uh_sport = htons(V_tcp_udp_tunneling_port);
14077 			udp->uh_dport = tp->t_port;
14078 			rack->r_ctl.fsb.udp = udp;
14079 			rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1);
14080 		} else
14081 		{
14082 			rack->r_ctl.fsb.th = (struct tcphdr *)(ip6 + 1);
14083 			rack->r_ctl.fsb.udp = NULL;
14084 		}
14085 		tcpip_fillheaders(rack->rc_inp,
14086 				  tp->t_port,
14087 				  ip6, rack->r_ctl.fsb.th);
14088 		rack->r_ctl.fsb.hoplimit = in6_selecthlim(rack->rc_inp, NULL);
14089 	} else
14090 #endif				/* INET6 */
14091 #ifdef INET
14092 	{
14093 		rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr);
14094 		ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
14095 		if (tp->t_port) {
14096 			rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr);
14097 			udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
14098 			udp->uh_sport = htons(V_tcp_udp_tunneling_port);
14099 			udp->uh_dport = tp->t_port;
14100 			rack->r_ctl.fsb.udp = udp;
14101 			rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1);
14102 		} else
14103 		{
14104 			rack->r_ctl.fsb.udp = NULL;
14105 			rack->r_ctl.fsb.th = (struct tcphdr *)(ip + 1);
14106 		}
14107 		tcpip_fillheaders(rack->rc_inp,
14108 				  tp->t_port,
14109 				  ip, rack->r_ctl.fsb.th);
14110 		rack->r_ctl.fsb.hoplimit = tptoinpcb(tp)->inp_ip_ttl;
14111 	}
14112 #endif
14113 	rack->r_ctl.fsb.recwin = lmin(lmax(sbspace(&tptosocket(tp)->so_rcv), 0),
14114 	    (long)TCP_MAXWIN << tp->rcv_scale);
14115 	rack->r_fsb_inited = 1;
14116 }
14117 
14118 static int
14119 rack_init_fsb(struct tcpcb *tp, struct tcp_rack *rack)
14120 {
14121 	/*
14122 	 * Allocate the larger of spaces V6 if available else just
14123 	 * V4 and include udphdr (overbook)
14124 	 */
14125 #ifdef INET6
14126 	rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr) + sizeof(struct udphdr);
14127 #else
14128 	rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr) + sizeof(struct udphdr);
14129 #endif
14130 	rack->r_ctl.fsb.tcp_ip_hdr = malloc(rack->r_ctl.fsb.tcp_ip_hdr_len,
14131 					    M_TCPFSB, M_NOWAIT|M_ZERO);
14132 	if (rack->r_ctl.fsb.tcp_ip_hdr == NULL) {
14133 		return (ENOMEM);
14134 	}
14135 	rack->r_fsb_inited = 0;
14136 	return (0);
14137 }
14138 
14139 static void
14140 rack_log_hystart_event(struct tcp_rack *rack, uint32_t high_seq, uint8_t mod)
14141 {
14142 	/*
14143 	 * Types of logs (mod value)
14144 	 * 20 - Initial round setup
14145 	 * 21 - Rack declares a new round.
14146 	 */
14147 	struct tcpcb *tp;
14148 
14149 	tp = rack->rc_tp;
14150 	if (tcp_bblogging_on(tp)) {
14151 		union tcp_log_stackspecific log;
14152 		struct timeval tv;
14153 
14154 		memset(&log, 0, sizeof(log));
14155 		log.u_bbr.flex1 = rack->r_ctl.current_round;
14156 		log.u_bbr.flex2 = rack->r_ctl.roundends;
14157 		log.u_bbr.flex3 = high_seq;
14158 		log.u_bbr.flex4 = tp->snd_max;
14159 		log.u_bbr.flex8 = mod;
14160 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
14161 		log.u_bbr.cur_del_rate = rack->rc_tp->t_sndbytes;
14162 		log.u_bbr.delRate = rack->rc_tp->t_snd_rxt_bytes;
14163 		TCP_LOG_EVENTP(tp, NULL,
14164 		    &tptosocket(tp)->so_rcv,
14165 		    &tptosocket(tp)->so_snd,
14166 		    TCP_HYSTART, 0,
14167 		    0, &log, false, &tv);
14168 	}
14169 }
14170 
14171 static void
14172 rack_deferred_init(struct tcpcb *tp, struct tcp_rack *rack)
14173 {
14174 	rack->rack_deferred_inited = 1;
14175 	rack->r_ctl.roundends = tp->snd_max;
14176 	rack->r_ctl.rc_high_rwnd = tp->snd_wnd;
14177 	rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
14178 }
14179 
14180 static void
14181 rack_init_retransmit_value(struct tcp_rack *rack, int ctl)
14182 {
14183 	/* Retransmit bit controls.
14184 	 *
14185 	 * The setting of these values control one of
14186 	 * three settings you can have and dictate
14187 	 * how rack does retransmissions. Note this
14188 	 * is in *any* mode i.e. pacing on or off DGP
14189 	 * fixed rate pacing, or just bursting rack.
14190 	 *
14191 	 * 1 - Use full sized retransmits i.e. limit
14192 	 *     the size to whatever the pace_max_segments
14193 	 *     size is.
14194 	 *
14195 	 * 2 - Use pacer min granularity as a guide to
14196 	 *     the size combined with the current calculated
14197 	 *     goodput b/w measurement. So for example if
14198 	 *     the goodput is measured at 20Mbps we would
14199 	 *     calculate 8125 (pacer minimum 250usec in
14200 	 *     that b/w) and then round it up to the next
14201 	 *     MSS i.e. for 1448 mss 6 MSS or 8688 bytes.
14202 	 *
14203 	 * 0 - The rack default 1 MSS (anything not 0/1/2
14204 	 *     fall here too if we are setting via rack_init()).
14205 	 *
14206 	 */
14207 	if (ctl == 1) {
14208 		rack->full_size_rxt = 1;
14209 		rack->shape_rxt_to_pacing_min  = 0;
14210 	} else if (ctl == 2) {
14211 		rack->full_size_rxt = 0;
14212 		rack->shape_rxt_to_pacing_min  = 1;
14213 	} else {
14214 		rack->full_size_rxt = 0;
14215 		rack->shape_rxt_to_pacing_min  = 0;
14216 	}
14217 }
14218 
14219 static void
14220 rack_log_chg_info(struct tcpcb *tp, struct tcp_rack *rack, uint8_t mod,
14221 		  uint32_t flex1,
14222 		  uint32_t flex2,
14223 		  uint32_t flex3)
14224 {
14225 	if (tcp_bblogging_on(rack->rc_tp)) {
14226 		union tcp_log_stackspecific log;
14227 		struct timeval tv;
14228 
14229 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
14230 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
14231 		log.u_bbr.flex8 = mod;
14232 		log.u_bbr.flex1 = flex1;
14233 		log.u_bbr.flex2 = flex2;
14234 		log.u_bbr.flex3 = flex3;
14235 		tcp_log_event(tp, NULL, NULL, NULL, TCP_CHG_QUERY, 0,
14236 			       0, &log, false, NULL, __func__, __LINE__, &tv);
14237 	}
14238 }
14239 
14240 static int
14241 rack_chg_query(struct tcpcb *tp, struct tcp_query_resp *reqr)
14242 {
14243 	struct tcp_rack *rack;
14244 	struct rack_sendmap *rsm;
14245 	int i;
14246 
14247 
14248 	rack = (struct tcp_rack *)tp->t_fb_ptr;
14249 	switch (reqr->req) {
14250 	case TCP_QUERY_SENDMAP:
14251 		if ((reqr->req_param == tp->snd_max) ||
14252 		    (tp->snd_max == tp->snd_una)){
14253 			/* Unlikely */
14254 			return (0);
14255 		}
14256 		rsm = tqhash_find(rack->r_ctl.tqh, reqr->req_param);
14257 		if (rsm == NULL) {
14258 			/* Can't find that seq -- unlikely */
14259 			return (0);
14260 		}
14261 		reqr->sendmap_start = rsm->r_start;
14262 		reqr->sendmap_end = rsm->r_end;
14263 		reqr->sendmap_send_cnt = rsm->r_rtr_cnt;
14264 		reqr->sendmap_fas = rsm->r_fas;
14265 		if (reqr->sendmap_send_cnt > SNDMAP_NRTX)
14266 			reqr->sendmap_send_cnt = SNDMAP_NRTX;
14267 		for(i=0; i<reqr->sendmap_send_cnt; i++)
14268 			reqr->sendmap_time[i] = rsm->r_tim_lastsent[i];
14269 		reqr->sendmap_ack_arrival = rsm->r_ack_arrival;
14270 		reqr->sendmap_flags = rsm->r_flags & SNDMAP_MASK;
14271 		reqr->sendmap_r_rtr_bytes = rsm->r_rtr_bytes;
14272 		reqr->sendmap_dupacks = rsm->r_dupack;
14273 		rack_log_chg_info(tp, rack, 1,
14274 				  rsm->r_start,
14275 				  rsm->r_end,
14276 				  rsm->r_flags);
14277 		return(1);
14278 		break;
14279 	case TCP_QUERY_TIMERS_UP:
14280 		if (rack->r_ctl.rc_hpts_flags == 0) {
14281 			/* no timers up */
14282 			return (0);
14283 		}
14284 		reqr->timer_hpts_flags = rack->r_ctl.rc_hpts_flags;
14285 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
14286 			reqr->timer_pacing_to = rack->r_ctl.rc_last_output_to;
14287 		}
14288 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
14289 			reqr->timer_timer_exp = rack->r_ctl.rc_timer_exp;
14290 		}
14291 		rack_log_chg_info(tp, rack, 2,
14292 				  rack->r_ctl.rc_hpts_flags,
14293 				  rack->r_ctl.rc_last_output_to,
14294 				  rack->r_ctl.rc_timer_exp);
14295 		return (1);
14296 		break;
14297 	case TCP_QUERY_RACK_TIMES:
14298 		/* Reordering items */
14299 		reqr->rack_num_dsacks = rack->r_ctl.num_dsack;
14300 		reqr->rack_reorder_ts = rack->r_ctl.rc_reorder_ts;
14301 		/* Timerstamps and timers */
14302 		reqr->rack_rxt_last_time = rack->r_ctl.rc_tlp_rxt_last_time;
14303 		reqr->rack_min_rtt = rack->r_ctl.rc_rack_min_rtt;
14304 		reqr->rack_rtt = rack->rc_rack_rtt;
14305 		reqr->rack_tmit_time = rack->r_ctl.rc_rack_tmit_time;
14306 		reqr->rack_srtt_measured = rack->rc_srtt_measure_made;
14307 		/* PRR data */
14308 		reqr->rack_sacked = rack->r_ctl.rc_sacked;
14309 		reqr->rack_holes_rxt = rack->r_ctl.rc_holes_rxt;
14310 		reqr->rack_prr_delivered = rack->r_ctl.rc_prr_delivered;
14311 		reqr->rack_prr_recovery_fs = rack->r_ctl.rc_prr_recovery_fs;
14312 		reqr->rack_prr_sndcnt = rack->r_ctl.rc_prr_sndcnt;
14313 		reqr->rack_prr_out = rack->r_ctl.rc_prr_out;
14314 		/* TLP and persists info */
14315 		reqr->rack_tlp_out = rack->rc_tlp_in_progress;
14316 		reqr->rack_tlp_cnt_out = rack->r_ctl.rc_tlp_cnt_out;
14317 		if (rack->rc_in_persist) {
14318 			reqr->rack_time_went_idle = rack->r_ctl.rc_went_idle_time;
14319 			reqr->rack_in_persist = 1;
14320 		} else {
14321 			reqr->rack_time_went_idle = 0;
14322 			reqr->rack_in_persist = 0;
14323 		}
14324 		if (rack->r_wanted_output)
14325 			reqr->rack_wanted_output = 1;
14326 		else
14327 			reqr->rack_wanted_output = 0;
14328 		return (1);
14329 		break;
14330 	default:
14331 		return (-EINVAL);
14332 	}
14333 }
14334 
14335 static void
14336 rack_switch_failed(struct tcpcb *tp)
14337 {
14338 	/*
14339 	 * This method gets called if a stack switch was
14340 	 * attempted and it failed. We are left
14341 	 * but our hpts timers were stopped and we
14342 	 * need to validate time units and t_flags2.
14343 	 */
14344 	struct tcp_rack *rack;
14345 	struct timeval tv;
14346 	uint32_t cts;
14347 	uint32_t toval;
14348 	struct hpts_diag diag;
14349 
14350 	rack = (struct tcp_rack *)tp->t_fb_ptr;
14351 	tcp_change_time_units(tp, TCP_TMR_GRANULARITY_USEC);
14352 	if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
14353 		tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
14354 	else
14355 		tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
14356 	if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
14357 		tp->t_flags2 |= TF2_MBUF_ACKCMP;
14358 	if (tp->t_in_hpts > IHPTS_NONE) {
14359 		/* Strange */
14360 		return;
14361 	}
14362 	cts = tcp_get_usecs(&tv);
14363 	if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
14364 		if (TSTMP_GT(rack->r_ctl.rc_last_output_to, cts)) {
14365 			toval = rack->r_ctl.rc_last_output_to - cts;
14366 		} else {
14367 			/* one slot please */
14368 			toval = HPTS_TICKS_PER_SLOT;
14369 		}
14370 	} else if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
14371 		if (TSTMP_GT(rack->r_ctl.rc_timer_exp, cts)) {
14372 			toval = rack->r_ctl.rc_timer_exp - cts;
14373 		} else {
14374 			/* one slot please */
14375 			toval = HPTS_TICKS_PER_SLOT;
14376 		}
14377 	} else
14378 		toval = HPTS_TICKS_PER_SLOT;
14379 	(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(toval),
14380 				   __LINE__, &diag);
14381 	rack_log_hpts_diag(rack, cts, &diag, &tv);
14382 }
14383 
14384 static int
14385 rack_init_outstanding(struct tcpcb *tp, struct tcp_rack *rack, uint32_t us_cts, void *ptr)
14386 {
14387 	struct rack_sendmap *rsm, *ersm;
14388 	int insret __diagused;
14389 	/*
14390 	 * When initing outstanding, we must be quite careful
14391 	 * to not refer to tp->t_fb_ptr. This has the old rack
14392 	 * pointer in it, not the "new" one (when we are doing
14393 	 * a stack switch).
14394 	 */
14395 
14396 
14397 	if (tp->t_fb->tfb_chg_query == NULL) {
14398 		/* Create a send map for the current outstanding data */
14399 
14400 		rsm = rack_alloc(rack);
14401 		if (rsm == NULL) {
14402 			uma_zfree(rack_pcb_zone, ptr);
14403 			return (ENOMEM);
14404 		}
14405 		rsm->r_no_rtt_allowed = 1;
14406 		rsm->r_tim_lastsent[0] = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
14407 		rsm->r_rtr_cnt = 1;
14408 		rsm->r_rtr_bytes = 0;
14409 		if (tp->t_flags & TF_SENTFIN)
14410 			rsm->r_flags |= RACK_HAS_FIN;
14411 		rsm->r_end = tp->snd_max;
14412 		if (tp->snd_una == tp->iss) {
14413 			/* The data space is one beyond snd_una */
14414 			rsm->r_flags |= RACK_HAS_SYN;
14415 			rsm->r_start = tp->iss;
14416 			rsm->r_end = rsm->r_start + (tp->snd_max - tp->snd_una);
14417 		} else
14418 			rsm->r_start = tp->snd_una;
14419 		rsm->r_dupack = 0;
14420 		if (rack->rc_inp->inp_socket->so_snd.sb_mb != NULL) {
14421 			rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd, 0, &rsm->soff);
14422 			if (rsm->m) {
14423 				rsm->orig_m_len = rsm->m->m_len;
14424 				rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
14425 			} else {
14426 				rsm->orig_m_len = 0;
14427 				rsm->orig_t_space = 0;
14428 			}
14429 		} else {
14430 			/*
14431 			 * This can happen if we have a stand-alone FIN or
14432 			 *  SYN.
14433 			 */
14434 			rsm->m = NULL;
14435 			rsm->orig_m_len = 0;
14436 			rsm->orig_t_space = 0;
14437 			rsm->soff = 0;
14438 		}
14439 #ifdef INVARIANTS
14440 		if ((insret = tqhash_insert(rack->r_ctl.tqh, rsm)) != 0) {
14441 			panic("Insert in tailq_hash fails ret:%d rack:%p rsm:%p",
14442 			      insret, rack, rsm);
14443 		}
14444 #else
14445 		(void)tqhash_insert(rack->r_ctl.tqh, rsm);
14446 #endif
14447 		TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
14448 		rsm->r_in_tmap = 1;
14449 	} else {
14450 		/* We have a query mechanism, lets use it */
14451 		struct tcp_query_resp qr;
14452 		int i;
14453 		tcp_seq at;
14454 
14455 		at = tp->snd_una;
14456 		while (at != tp->snd_max) {
14457 			memset(&qr, 0, sizeof(qr));
14458 			qr.req = TCP_QUERY_SENDMAP;
14459 			qr.req_param = at;
14460 			if ((*tp->t_fb->tfb_chg_query)(tp, &qr) == 0)
14461 				break;
14462 			/* Move forward */
14463 			at = qr.sendmap_end;
14464 			/* Now lets build the entry for this one */
14465 			rsm = rack_alloc(rack);
14466 			if (rsm == NULL) {
14467 				uma_zfree(rack_pcb_zone, ptr);
14468 				return (ENOMEM);
14469 			}
14470 			memset(rsm, 0, sizeof(struct rack_sendmap));
14471 			/* Now configure the rsm and insert it */
14472 			rsm->r_dupack = qr.sendmap_dupacks;
14473 			rsm->r_start = qr.sendmap_start;
14474 			rsm->r_end = qr.sendmap_end;
14475 			if (qr.sendmap_fas)
14476 				rsm->r_fas = qr.sendmap_end;
14477 			else
14478 				rsm->r_fas = rsm->r_start - tp->snd_una;
14479 			/*
14480 			 * We have carefully aligned the bits
14481 			 * so that all we have to do is copy over
14482 			 * the bits with the mask.
14483 			 */
14484 			rsm->r_flags = qr.sendmap_flags & SNDMAP_MASK;
14485 			rsm->r_rtr_bytes = qr.sendmap_r_rtr_bytes;
14486 			rsm->r_rtr_cnt = qr.sendmap_send_cnt;
14487 			rsm->r_ack_arrival = qr.sendmap_ack_arrival;
14488 			for (i=0 ; i<rsm->r_rtr_cnt; i++)
14489 				rsm->r_tim_lastsent[i]	= qr.sendmap_time[i];
14490 			rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd,
14491 					   (rsm->r_start - tp->snd_una), &rsm->soff);
14492 			if (rsm->m) {
14493 				rsm->orig_m_len = rsm->m->m_len;
14494 				rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
14495 			} else {
14496 				rsm->orig_m_len = 0;
14497 				rsm->orig_t_space = 0;
14498 			}
14499 #ifdef INVARIANTS
14500 			if ((insret = tqhash_insert(rack->r_ctl.tqh, rsm)) != 0) {
14501 				panic("Insert in tailq_hash fails ret:%d rack:%p rsm:%p",
14502 				      insret, rack, rsm);
14503 			}
14504 #else
14505 			(void)tqhash_insert(rack->r_ctl.tqh, rsm);
14506 #endif
14507 			if ((rsm->r_flags & RACK_ACKED) == 0)  {
14508 				TAILQ_FOREACH(ersm, &rack->r_ctl.rc_tmap, r_tnext) {
14509 					if (ersm->r_tim_lastsent[(ersm->r_rtr_cnt-1)] >
14510 					    rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]) {
14511 						/*
14512 						 * If the existing ersm was sent at
14513 						 * a later time than the new one, then
14514 						 * the new one should appear ahead of this
14515 						 * ersm.
14516 						 */
14517 						rsm->r_in_tmap = 1;
14518 						TAILQ_INSERT_BEFORE(ersm, rsm, r_tnext);
14519 						break;
14520 					}
14521 				}
14522 				if (rsm->r_in_tmap == 0) {
14523 					/*
14524 					 * Not found so shove it on the tail.
14525 					 */
14526 					TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
14527 					rsm->r_in_tmap = 1;
14528 				}
14529  			} else {
14530 				if ((rack->r_ctl.rc_sacklast == NULL) ||
14531 				    (SEQ_GT(rsm->r_end, rack->r_ctl.rc_sacklast->r_end))) {
14532 					rack->r_ctl.rc_sacklast = rsm;
14533 				}
14534 			}
14535 			rack_log_chg_info(tp, rack, 3,
14536 					  rsm->r_start,
14537 					  rsm->r_end,
14538 					  rsm->r_flags);
14539 		}
14540 	}
14541 	return (0);
14542 }
14543 
14544 
14545 static int32_t
14546 rack_init(struct tcpcb *tp, void **ptr)
14547 {
14548 	struct inpcb *inp = tptoinpcb(tp);
14549 	struct tcp_rack *rack = NULL;
14550 	uint32_t iwin, snt, us_cts;
14551 	size_t sz;
14552 	int err, no_query;
14553 
14554 	tcp_hpts_init(tp);
14555 
14556 	/*
14557 	 * First are we the initial or are we a switched stack?
14558 	 * If we are initing via tcp_newtcppcb the ptr passed
14559 	 * will be tp->t_fb_ptr. If its a stack switch that
14560 	 * has a previous stack we can query it will be a local
14561 	 * var that will in the end be set into t_fb_ptr.
14562 	 */
14563 	if (ptr == &tp->t_fb_ptr)
14564 		no_query = 1;
14565 	else
14566 		no_query = 0;
14567 	*ptr = uma_zalloc(rack_pcb_zone, M_NOWAIT);
14568 	if (*ptr == NULL) {
14569 		/*
14570 		 * We need to allocate memory but cant. The INP and INP_INFO
14571 		 * locks and they are recursive (happens during setup. So a
14572 		 * scheme to drop the locks fails :(
14573 		 *
14574 		 */
14575 		return(ENOMEM);
14576 	}
14577 	memset(*ptr, 0, sizeof(struct tcp_rack));
14578 	rack = (struct tcp_rack *)*ptr;
14579 	rack->r_ctl.tqh = malloc(sizeof(struct tailq_hash), M_TCPFSB, M_NOWAIT);
14580 	if (rack->r_ctl.tqh == NULL) {
14581 		uma_zfree(rack_pcb_zone, rack);
14582 		return(ENOMEM);
14583 	}
14584 	tqhash_init(rack->r_ctl.tqh);
14585 	TAILQ_INIT(&rack->r_ctl.rc_free);
14586 	TAILQ_INIT(&rack->r_ctl.rc_tmap);
14587 	rack->rc_tp = tp;
14588 	rack->rc_inp = inp;
14589 	/* Set the flag */
14590 	rack->r_is_v6 = (inp->inp_vflag & INP_IPV6) != 0;
14591 	/* Probably not needed but lets be sure */
14592 	rack_clear_rate_sample(rack);
14593 	/*
14594 	 * Save off the default values, socket options will poke
14595 	 * at these if pacing is not on or we have not yet
14596 	 * reached where pacing is on (gp_ready/fixed enabled).
14597 	 * When they get set into the CC module (when gp_ready
14598 	 * is enabled or we enable fixed) then we will set these
14599 	 * values into the CC and place in here the old values
14600 	 * so we have a restoral. Then we will set the flag
14601 	 * rc_pacing_cc_set. That way whenever we turn off pacing
14602 	 * or switch off this stack, we will know to go restore
14603 	 * the saved values.
14604 	 *
14605 	 * We specifically put into the beta the ecn value for pacing.
14606 	 */
14607 	rack->rc_new_rnd_needed = 1;
14608 	rack->r_ctl.rc_split_limit = V_tcp_map_split_limit;
14609 	/* We want abe like behavior as well */
14610 
14611 	rack->r_ctl.rc_saved_beta.newreno_flags |= CC_NEWRENO_BETA_ECN_ENABLED;
14612 	rack->r_ctl.rc_reorder_fade = rack_reorder_fade;
14613 	rack->rc_allow_data_af_clo = rack_ignore_data_after_close;
14614 	rack->r_ctl.rc_tlp_threshold = rack_tlp_thresh;
14615 	if (rack_fill_cw_state)
14616 		rack->rc_pace_to_cwnd = 1;
14617 	if (rack_pacing_min_seg)
14618 		rack->r_ctl.rc_user_set_min_segs = rack_pacing_min_seg;
14619 	if (use_rack_rr)
14620 		rack->use_rack_rr = 1;
14621 	if (rack_dnd_default) {
14622 		rack->rc_pace_dnd = 1;
14623 	}
14624 	if (V_tcp_delack_enabled)
14625 		tp->t_delayed_ack = 1;
14626 	else
14627 		tp->t_delayed_ack = 0;
14628 #ifdef TCP_ACCOUNTING
14629 	if (rack_tcp_accounting) {
14630 		tp->t_flags2 |= TF2_TCP_ACCOUNTING;
14631 	}
14632 #endif
14633 	rack->r_ctl.pcm_i.cnt_alloc = RACK_DEFAULT_PCM_ARRAY;
14634 	sz = (sizeof(struct rack_pcm_stats) * rack->r_ctl.pcm_i.cnt_alloc);
14635 	rack->r_ctl.pcm_s = malloc(sz,M_TCPPCM, M_NOWAIT);
14636 	if (rack->r_ctl.pcm_s == NULL) {
14637 		rack->r_ctl.pcm_i.cnt_alloc = 0;
14638 	}
14639 #ifdef NETFLIX_STATS
14640 	rack->r_ctl.side_chan_dis_mask = tcp_sidechannel_disable_mask;
14641 #endif
14642 	rack->r_ctl.rack_per_upper_bound_ss = (uint8_t)rack_per_upper_bound_ss;
14643 	rack->r_ctl.rack_per_upper_bound_ca = (uint8_t)rack_per_upper_bound_ca;
14644 	if (rack_enable_shared_cwnd)
14645 		rack->rack_enable_scwnd = 1;
14646 	rack->r_ctl.pace_len_divisor = rack_default_pacing_divisor;
14647 	rack->rc_user_set_max_segs = rack_hptsi_segments;
14648 	rack->r_ctl.max_reduction = rack_max_reduce;
14649 	rack->rc_force_max_seg = 0;
14650 	TAILQ_INIT(&rack->r_ctl.opt_list);
14651 	rack->r_ctl.rc_saved_beta.beta = V_newreno_beta_ecn;
14652 	rack->r_ctl.rc_saved_beta.beta_ecn = V_newreno_beta_ecn;
14653 	if (rack_hibeta_setting) {
14654 		rack->rack_hibeta = 1;
14655 		if ((rack_hibeta_setting >= 50) &&
14656 		    (rack_hibeta_setting <= 100)) {
14657 			rack->r_ctl.rc_saved_beta.beta = rack_hibeta_setting;
14658 			rack->r_ctl.saved_hibeta = rack_hibeta_setting;
14659 		}
14660 	} else {
14661 		rack->r_ctl.saved_hibeta = 50;
14662 	}
14663 	/*
14664 	 * We initialize to all ones so we never match 0
14665 	 * just in case the client sends in 0, it hopefully
14666 	 * will never have all 1's in ms :-)
14667 	 */
14668 	rack->r_ctl.last_tm_mark = 0xffffffffffffffff;
14669 	rack->r_ctl.rc_reorder_shift = rack_reorder_thresh;
14670 	rack->r_ctl.rc_pkt_delay = rack_pkt_delay;
14671 	rack->r_ctl.rc_tlp_cwnd_reduce = rack_lower_cwnd_at_tlp;
14672 	rack->r_ctl.rc_lowest_us_rtt = 0xffffffff;
14673 	rack->r_ctl.rc_highest_us_rtt = 0;
14674 	rack->r_ctl.bw_rate_cap = rack_bw_rate_cap;
14675 	rack->pcm_enabled = rack_pcm_is_enabled;
14676 	if (rack_fillcw_bw_cap)
14677 		rack->r_ctl.fillcw_cap = rack_fillcw_bw_cap;
14678 	rack->r_ctl.timer_slop = TICKS_2_USEC(tcp_rexmit_slop);
14679 	if (rack_use_cmp_acks)
14680 		rack->r_use_cmp_ack = 1;
14681 	if (rack_disable_prr)
14682 		rack->rack_no_prr = 1;
14683 	if (rack_gp_no_rec_chg)
14684 		rack->rc_gp_no_rec_chg = 1;
14685 	if (rack_pace_every_seg && tcp_can_enable_pacing()) {
14686 		rack->r_ctl.pacing_method |= RACK_REG_PACING;
14687 		rack->rc_always_pace = 1;
14688 		if (rack->rack_hibeta)
14689 			rack_set_cc_pacing(rack);
14690 	} else
14691 		rack->rc_always_pace = 0;
14692 	if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack)
14693 		rack->r_mbuf_queue = 1;
14694 	else
14695 		rack->r_mbuf_queue = 0;
14696 	rack_set_pace_segments(tp, rack, __LINE__, NULL);
14697 	if (rack_limits_scwnd)
14698 		rack->r_limit_scw = 1;
14699 	else
14700 		rack->r_limit_scw = 0;
14701 	rack_init_retransmit_value(rack, rack_rxt_controls);
14702 	rack->rc_labc = V_tcp_abc_l_var;
14703 	if (rack_honors_hpts_min_to)
14704 		rack->r_use_hpts_min = 1;
14705 	if (tp->snd_una != 0) {
14706 		rack->rc_sendvars_notset = 0;
14707 		/*
14708 		 * Make sure any TCP timers are not running.
14709 		 */
14710 		tcp_timer_stop(tp);
14711 	} else {
14712 		/*
14713 		 * Server side, we are called from the
14714 		 * syn-cache. This means none of the
14715 		 * snd_una/max are set yet so we have
14716 		 * to defer this until the first send.
14717 		 */
14718 		rack->rc_sendvars_notset = 1;
14719 	}
14720 
14721 	rack->r_ctl.rc_rate_sample_method = rack_rate_sample_method;
14722 	rack->rack_tlp_threshold_use = rack_tlp_threshold_use;
14723 	rack->r_ctl.rc_prr_sendalot = rack_send_a_lot_in_prr;
14724 	rack->r_ctl.rc_min_to = rack_min_to;
14725 	microuptime(&rack->r_ctl.act_rcv_time);
14726 	rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time;
14727 	rack->r_ctl.rack_per_of_gp_ss = rack_per_of_gp_ss;
14728 	if (rack_hw_up_only)
14729 		rack->r_up_only = 1;
14730 	if (rack_do_dyn_mul) {
14731 		/* When dynamic adjustment is on CA needs to start at 100% */
14732 		rack->rc_gp_dyn_mul = 1;
14733 		if (rack_do_dyn_mul >= 100)
14734 			rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul;
14735 	} else
14736 		rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca;
14737 	rack->r_ctl.rack_per_of_gp_rec = rack_per_of_gp_rec;
14738 	if (rack_timely_off) {
14739 		rack->rc_skip_timely = 1;
14740 	}
14741 	if (rack->rc_skip_timely) {
14742 		rack->r_ctl.rack_per_of_gp_rec = 90;
14743 		rack->r_ctl.rack_per_of_gp_ca = 100;
14744 		rack->r_ctl.rack_per_of_gp_ss = 250;
14745 	}
14746 	rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
14747 	rack->r_ctl.rc_tlp_rxt_last_time = tcp_tv_to_mssectick(&rack->r_ctl.act_rcv_time);
14748 	rack->r_ctl.last_rcv_tstmp_for_rtt = tcp_tv_to_mssectick(&rack->r_ctl.act_rcv_time);
14749 
14750 	setup_time_filter_small(&rack->r_ctl.rc_gp_min_rtt, FILTER_TYPE_MIN,
14751 				rack_probertt_filter_life);
14752 	us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
14753 	rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
14754 	rack->r_ctl.rc_time_of_last_probertt = us_cts;
14755 	rack->r_ctl.rc_went_idle_time = us_cts;
14756 	rack->r_ctl.rc_time_probertt_starts = 0;
14757 
14758 	rack->r_ctl.gp_rnd_thresh = rack_rnd_cnt_req & 0xff;
14759 	if (rack_rnd_cnt_req  & 0x10000)
14760 		rack->r_ctl.gate_to_fs = 1;
14761 	rack->r_ctl.gp_gain_req = rack_gp_gain_req;
14762 	if ((rack_rnd_cnt_req & 0x100) > 0) {
14763 
14764 	}
14765 	if (rack_dsack_std_based & 0x1) {
14766 		/* Basically this means all rack timers are at least (srtt + 1/4 srtt) */
14767 		rack->rc_rack_tmr_std_based = 1;
14768 	}
14769 	if (rack_dsack_std_based & 0x2) {
14770 		/* Basically this means  rack timers are extended based on dsack by up to (2 * srtt) */
14771 		rack->rc_rack_use_dsack = 1;
14772 	}
14773 	/* We require at least one measurement, even if the sysctl is 0 */
14774 	if (rack_req_measurements)
14775 		rack->r_ctl.req_measurements = rack_req_measurements;
14776 	else
14777 		rack->r_ctl.req_measurements = 1;
14778 	if (rack_enable_hw_pacing)
14779 		rack->rack_hdw_pace_ena = 1;
14780 	if (rack_hw_rate_caps)
14781 		rack->r_rack_hw_rate_caps = 1;
14782 	if (rack_non_rxt_use_cr)
14783 		rack->rack_rec_nonrxt_use_cr = 1;
14784 	/* Lets setup the fsb block */
14785 	err = rack_init_fsb(tp, rack);
14786 	if (err) {
14787 		uma_zfree(rack_pcb_zone, *ptr);
14788 		*ptr = NULL;
14789 		return (err);
14790 	}
14791 	if (rack_do_hystart) {
14792 		tp->t_ccv.flags |= CCF_HYSTART_ALLOWED;
14793 		if (rack_do_hystart > 1)
14794 			tp->t_ccv.flags |= CCF_HYSTART_CAN_SH_CWND;
14795 		if (rack_do_hystart > 2)
14796 			tp->t_ccv.flags |= CCF_HYSTART_CONS_SSTH;
14797 	}
14798 	/* Log what we will do with queries */
14799 	rack_log_chg_info(tp, rack, 7,
14800 			  no_query, 0, 0);
14801 	if (rack_def_profile)
14802 		rack_set_profile(rack, rack_def_profile);
14803 	/* Cancel the GP measurement in progress */
14804 	tp->t_flags &= ~TF_GPUTINPROG;
14805 	if ((tp->t_state != TCPS_CLOSED) &&
14806 	    (tp->t_state != TCPS_TIME_WAIT)) {
14807 		/*
14808 		 * We are already open, we may
14809 		 * need to adjust a few things.
14810 		 */
14811 		if (SEQ_GT(tp->snd_max, tp->iss))
14812 			snt = tp->snd_max - tp->iss;
14813 		else
14814 			snt = 0;
14815 		iwin = rc_init_window(rack);
14816 		if ((snt < iwin) &&
14817 		    (no_query == 1)) {
14818 			/* We are not past the initial window
14819 			 * on the first init (i.e. a stack switch
14820 			 * has not yet occured) so we need to make
14821 			 * sure cwnd and ssthresh is correct.
14822 			 */
14823 			if (tp->snd_cwnd < iwin)
14824 				tp->snd_cwnd = iwin;
14825 			/*
14826 			 * If we are within the initial window
14827 			 * we want ssthresh to be unlimited. Setting
14828 			 * it to the rwnd (which the default stack does
14829 			 * and older racks) is not really a good idea
14830 			 * since we want to be in SS and grow both the
14831 			 * cwnd and the rwnd (via dynamic rwnd growth). If
14832 			 * we set it to the rwnd then as the peer grows its
14833 			 * rwnd we will be stuck in CA and never hit SS.
14834 			 *
14835 			 * Its far better to raise it up high (this takes the
14836 			 * risk that there as been a loss already, probably
14837 			 * we should have an indicator in all stacks of loss
14838 			 * but we don't), but considering the normal use this
14839 			 * is a risk worth taking. The consequences of not
14840 			 * hitting SS are far worse than going one more time
14841 			 * into it early on (before we have sent even a IW).
14842 			 * It is highly unlikely that we will have had a loss
14843 			 * before getting the IW out.
14844 			 */
14845 			tp->snd_ssthresh = 0xffffffff;
14846 		}
14847 		/*
14848 		 * Any init based on sequence numbers
14849 		 * should be done in the deferred init path
14850 		 * since we can be CLOSED and not have them
14851 		 * inited when rack_init() is called. We
14852 		 * are not closed so lets call it.
14853 		 */
14854 		rack_deferred_init(tp, rack);
14855 	}
14856 	if ((tp->t_state != TCPS_CLOSED) &&
14857 	    (tp->t_state != TCPS_TIME_WAIT) &&
14858 	    (no_query == 0) &&
14859 	    (tp->snd_una != tp->snd_max))  {
14860 		err = rack_init_outstanding(tp, rack, us_cts, *ptr);
14861 		if (err) {
14862 			*ptr = NULL;
14863 			return(err);
14864 		}
14865 	}
14866 	rack_stop_all_timers(tp, rack);
14867 	/* Setup all the t_flags2 */
14868 	if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
14869 		tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
14870 	else
14871 		tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
14872 	if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
14873 		tp->t_flags2 |= TF2_MBUF_ACKCMP;
14874 	/*
14875 	 * Timers in Rack are kept in microseconds so lets
14876 	 * convert any initial incoming variables
14877 	 * from ticks into usecs. Note that we
14878 	 * also change the values of t_srtt and t_rttvar, if
14879 	 * they are non-zero. They are kept with a 5
14880 	 * bit decimal so we have to carefully convert
14881 	 * these to get the full precision.
14882 	 */
14883 	rack_convert_rtts(tp);
14884 	rack_log_hystart_event(rack, rack->r_ctl.roundends, 20);
14885 	if ((tptoinpcb(tp)->inp_flags & INP_DROPPED) == 0) {
14886 		/* We do not start any timers on DROPPED connections */
14887 		if (tp->t_fb->tfb_chg_query == NULL) {
14888 			rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
14889 		} else {
14890 			struct tcp_query_resp qr;
14891 			int ret;
14892 
14893 			memset(&qr, 0, sizeof(qr));
14894 
14895 			/* Get the misc time stamps and such for rack */
14896 			qr.req = TCP_QUERY_RACK_TIMES;
14897 			ret = (*tp->t_fb->tfb_chg_query)(tp, &qr);
14898 			if (ret == 1) {
14899 				rack->r_ctl.rc_reorder_ts = qr.rack_reorder_ts;
14900 				rack->r_ctl.num_dsack  = qr.rack_num_dsacks;
14901 				rack->r_ctl.rc_tlp_rxt_last_time = qr.rack_rxt_last_time;
14902 				rack->r_ctl.rc_rack_min_rtt = qr.rack_min_rtt;
14903 				rack->rc_rack_rtt = qr.rack_rtt;
14904 				rack->r_ctl.rc_rack_tmit_time = qr.rack_tmit_time;
14905 				rack->r_ctl.rc_sacked = qr.rack_sacked;
14906 				rack->r_ctl.rc_holes_rxt = qr.rack_holes_rxt;
14907 				rack->r_ctl.rc_prr_delivered = qr.rack_prr_delivered;
14908 				rack->r_ctl.rc_prr_recovery_fs = qr.rack_prr_recovery_fs;
14909 				rack->r_ctl.rc_prr_sndcnt = qr.rack_prr_sndcnt;
14910 				rack->r_ctl.rc_prr_out = qr.rack_prr_out;
14911 				if (qr.rack_tlp_out) {
14912 					rack->rc_tlp_in_progress = 1;
14913 					rack->r_ctl.rc_tlp_cnt_out = qr.rack_tlp_cnt_out;
14914 				} else {
14915 					rack->rc_tlp_in_progress = 0;
14916 					rack->r_ctl.rc_tlp_cnt_out = 0;
14917 				}
14918 				if (qr.rack_srtt_measured)
14919 					rack->rc_srtt_measure_made = 1;
14920 				if (qr.rack_in_persist == 1) {
14921 					rack->r_ctl.rc_went_idle_time = qr.rack_time_went_idle;
14922 #ifdef NETFLIX_SHARED_CWND
14923 					if (rack->r_ctl.rc_scw) {
14924 						tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
14925 						rack->rack_scwnd_is_idle = 1;
14926 					}
14927 #endif
14928 					rack->r_ctl.persist_lost_ends = 0;
14929 					rack->probe_not_answered = 0;
14930 					rack->forced_ack = 0;
14931 					tp->t_rxtshift = 0;
14932 					rack->rc_in_persist = 1;
14933 					RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
14934 							   rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
14935 				}
14936 				if (qr.rack_wanted_output)
14937 					rack->r_wanted_output = 1;
14938 				rack_log_chg_info(tp, rack, 6,
14939 						  qr.rack_min_rtt,
14940 						  qr.rack_rtt,
14941 						  qr.rack_reorder_ts);
14942 			}
14943 			/* Get the old stack timers */
14944 			qr.req_param = 0;
14945 			qr.req = TCP_QUERY_TIMERS_UP;
14946 			ret = (*tp->t_fb->tfb_chg_query)(tp, &qr);
14947 			if (ret) {
14948 				/*
14949 				 * non-zero return means we have a timer('s)
14950 				 * to start. Zero means no timer (no keepalive
14951 				 * I suppose).
14952 				 */
14953 				uint32_t tov = 0;
14954 
14955 				rack->r_ctl.rc_hpts_flags = qr.timer_hpts_flags;
14956 				if (qr.timer_hpts_flags & PACE_PKT_OUTPUT) {
14957 					rack->r_ctl.rc_last_output_to = qr.timer_pacing_to;
14958 					if (TSTMP_GT(qr.timer_pacing_to, us_cts))
14959 						tov = qr.timer_pacing_to - us_cts;
14960 					else
14961 						tov = HPTS_TICKS_PER_SLOT;
14962 				}
14963 				if (qr.timer_hpts_flags & PACE_TMR_MASK) {
14964 					rack->r_ctl.rc_timer_exp = qr.timer_timer_exp;
14965 					if (tov == 0) {
14966 						if (TSTMP_GT(qr.timer_timer_exp, us_cts))
14967 							tov = qr.timer_timer_exp - us_cts;
14968 						else
14969 							tov = HPTS_TICKS_PER_SLOT;
14970 					}
14971 				}
14972 				rack_log_chg_info(tp, rack, 4,
14973 						  rack->r_ctl.rc_hpts_flags,
14974 						  rack->r_ctl.rc_last_output_to,
14975 						  rack->r_ctl.rc_timer_exp);
14976 				if (tov) {
14977 					struct hpts_diag diag;
14978 
14979 					(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(tov),
14980 								   __LINE__, &diag);
14981 					rack_log_hpts_diag(rack, us_cts, &diag, &rack->r_ctl.act_rcv_time);
14982 				}
14983 			}
14984 		}
14985 		rack_log_rtt_shrinks(rack,  us_cts,  tp->t_rxtcur,
14986 				     __LINE__, RACK_RTTS_INIT);
14987 	}
14988 	return (0);
14989 }
14990 
14991 static int
14992 rack_handoff_ok(struct tcpcb *tp)
14993 {
14994 	if ((tp->t_state == TCPS_CLOSED) ||
14995 	    (tp->t_state == TCPS_LISTEN)) {
14996 		/* Sure no problem though it may not stick */
14997 		return (0);
14998 	}
14999 	if ((tp->t_state == TCPS_SYN_SENT) ||
15000 	    (tp->t_state == TCPS_SYN_RECEIVED)) {
15001 		/*
15002 		 * We really don't know if you support sack,
15003 		 * you have to get to ESTAB or beyond to tell.
15004 		 */
15005 		return (EAGAIN);
15006 	}
15007 	if ((tp->t_flags & TF_SENTFIN) && ((tp->snd_max - tp->snd_una) > 1)) {
15008 		/*
15009 		 * Rack will only send a FIN after all data is acknowledged.
15010 		 * So in this case we have more data outstanding. We can't
15011 		 * switch stacks until either all data and only the FIN
15012 		 * is left (in which case rack_init() now knows how
15013 		 * to deal with that) <or> all is acknowledged and we
15014 		 * are only left with incoming data, though why you
15015 		 * would want to switch to rack after all data is acknowledged
15016 		 * I have no idea (rrs)!
15017 		 */
15018 		return (EAGAIN);
15019 	}
15020 	if ((tp->t_flags & TF_SACK_PERMIT) || rack_sack_not_required){
15021 		return (0);
15022 	}
15023 	/*
15024 	 * If we reach here we don't do SACK on this connection so we can
15025 	 * never do rack.
15026 	 */
15027 	return (EINVAL);
15028 }
15029 
15030 static void
15031 rack_fini(struct tcpcb *tp, int32_t tcb_is_purged)
15032 {
15033 
15034 	if (tp->t_fb_ptr) {
15035 		uint32_t cnt_free = 0;
15036 		struct tcp_rack *rack;
15037 		struct rack_sendmap *rsm;
15038 
15039 		tcp_handle_orphaned_packets(tp);
15040 		tp->t_flags &= ~TF_FORCEDATA;
15041 		rack = (struct tcp_rack *)tp->t_fb_ptr;
15042 		rack_log_pacing_delay_calc(rack,
15043 					   0,
15044 					   0,
15045 					   0,
15046 					   rack_get_gp_est(rack), /* delRate */
15047 					   rack_get_lt_bw(rack), /* rttProp */
15048 					   20, __LINE__, NULL, 0);
15049 #ifdef NETFLIX_SHARED_CWND
15050 		if (rack->r_ctl.rc_scw) {
15051 			uint32_t limit;
15052 
15053 			if (rack->r_limit_scw)
15054 				limit = max(1, rack->r_ctl.rc_lowest_us_rtt);
15055 			else
15056 				limit = 0;
15057 			tcp_shared_cwnd_free_full(tp, rack->r_ctl.rc_scw,
15058 						  rack->r_ctl.rc_scw_index,
15059 						  limit);
15060 			rack->r_ctl.rc_scw = NULL;
15061 		}
15062 #endif
15063 		if (rack->r_ctl.fsb.tcp_ip_hdr) {
15064 			free(rack->r_ctl.fsb.tcp_ip_hdr, M_TCPFSB);
15065 			rack->r_ctl.fsb.tcp_ip_hdr = NULL;
15066 			rack->r_ctl.fsb.th = NULL;
15067 		}
15068 		if (rack->rc_always_pace == 1) {
15069 			rack_remove_pacing(rack);
15070 		}
15071 		/* Clean up any options if they were not applied */
15072 		while (!TAILQ_EMPTY(&rack->r_ctl.opt_list)) {
15073 			struct deferred_opt_list *dol;
15074 
15075 			dol = TAILQ_FIRST(&rack->r_ctl.opt_list);
15076 			TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next);
15077 			free(dol, M_TCPDO);
15078 		}
15079 		/* rack does not use force data but other stacks may clear it */
15080 		if (rack->r_ctl.crte != NULL) {
15081 			tcp_rel_pacing_rate(rack->r_ctl.crte, tp);
15082 			rack->rack_hdrw_pacing = 0;
15083 			rack->r_ctl.crte = NULL;
15084 		}
15085 #ifdef TCP_BLACKBOX
15086 		tcp_log_flowend(tp);
15087 #endif
15088 		/*
15089 		 * Lets take a different approach to purging just
15090 		 * get each one and free it like a cum-ack would and
15091 		 * not use a foreach loop.
15092 		 */
15093 		rsm = tqhash_min(rack->r_ctl.tqh);
15094 		while (rsm) {
15095 			tqhash_remove(rack->r_ctl.tqh, rsm, REMOVE_TYPE_CUMACK);
15096 			rack->r_ctl.rc_num_maps_alloced--;
15097 			uma_zfree(rack_zone, rsm);
15098 			rsm = tqhash_min(rack->r_ctl.tqh);
15099 		}
15100 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
15101 		while (rsm) {
15102 			TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
15103 			rack->r_ctl.rc_num_maps_alloced--;
15104 			rack->rc_free_cnt--;
15105 			cnt_free++;
15106 			uma_zfree(rack_zone, rsm);
15107 			rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
15108 		}
15109 		if (rack->r_ctl.pcm_s != NULL) {
15110 			free(rack->r_ctl.pcm_s, M_TCPPCM);
15111 			rack->r_ctl.pcm_s = NULL;
15112 			rack->r_ctl.pcm_i.cnt_alloc = 0;
15113 			rack->r_ctl.pcm_i.cnt = 0;
15114 		}
15115 		if ((rack->r_ctl.rc_num_maps_alloced > 0) &&
15116 		    (tcp_bblogging_on(tp))) {
15117 			union tcp_log_stackspecific log;
15118 			struct timeval tv;
15119 
15120 			memset(&log.u_bbr, 0, sizeof(log.u_bbr));
15121 			log.u_bbr.flex8 = 10;
15122 			log.u_bbr.flex1 = rack->r_ctl.rc_num_maps_alloced;
15123 			log.u_bbr.flex2 = rack->rc_free_cnt;
15124 			log.u_bbr.flex3 = cnt_free;
15125 			log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
15126 			rsm = tqhash_min(rack->r_ctl.tqh);
15127 			log.u_bbr.delRate = (uintptr_t)rsm;
15128 			rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
15129 			log.u_bbr.cur_del_rate = (uintptr_t)rsm;
15130 			log.u_bbr.timeStamp = tcp_get_usecs(&tv);
15131 			log.u_bbr.pkt_epoch = __LINE__;
15132 			(void)tcp_log_event(tp, NULL, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
15133 					     0, &log, false, NULL, NULL, 0, &tv);
15134 		}
15135 		KASSERT((rack->r_ctl.rc_num_maps_alloced == 0),
15136 			("rack:%p num_aloc:%u after freeing all?",
15137 			 rack,
15138 			 rack->r_ctl.rc_num_maps_alloced));
15139 		rack->rc_free_cnt = 0;
15140 		free(rack->r_ctl.tqh, M_TCPFSB);
15141 		rack->r_ctl.tqh = NULL;
15142 		uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
15143 		tp->t_fb_ptr = NULL;
15144 	}
15145 	/* Make sure snd_nxt is correctly set */
15146 	tp->snd_nxt = tp->snd_max;
15147 }
15148 
15149 static void
15150 rack_set_state(struct tcpcb *tp, struct tcp_rack *rack)
15151 {
15152 	if ((rack->r_state == TCPS_CLOSED) && (tp->t_state != TCPS_CLOSED)) {
15153 		rack->r_is_v6 = (tptoinpcb(tp)->inp_vflag & INP_IPV6) != 0;
15154 	}
15155 	switch (tp->t_state) {
15156 	case TCPS_SYN_SENT:
15157 		rack->r_state = TCPS_SYN_SENT;
15158 		rack->r_substate = rack_do_syn_sent;
15159 		break;
15160 	case TCPS_SYN_RECEIVED:
15161 		rack->r_state = TCPS_SYN_RECEIVED;
15162 		rack->r_substate = rack_do_syn_recv;
15163 		break;
15164 	case TCPS_ESTABLISHED:
15165 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
15166 		rack->r_state = TCPS_ESTABLISHED;
15167 		rack->r_substate = rack_do_established;
15168 		break;
15169 	case TCPS_CLOSE_WAIT:
15170 		rack->r_state = TCPS_CLOSE_WAIT;
15171 		rack->r_substate = rack_do_close_wait;
15172 		break;
15173 	case TCPS_FIN_WAIT_1:
15174 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
15175 		rack->r_state = TCPS_FIN_WAIT_1;
15176 		rack->r_substate = rack_do_fin_wait_1;
15177 		break;
15178 	case TCPS_CLOSING:
15179 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
15180 		rack->r_state = TCPS_CLOSING;
15181 		rack->r_substate = rack_do_closing;
15182 		break;
15183 	case TCPS_LAST_ACK:
15184 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
15185 		rack->r_state = TCPS_LAST_ACK;
15186 		rack->r_substate = rack_do_lastack;
15187 		break;
15188 	case TCPS_FIN_WAIT_2:
15189 		rack->r_state = TCPS_FIN_WAIT_2;
15190 		rack->r_substate = rack_do_fin_wait_2;
15191 		break;
15192 	case TCPS_LISTEN:
15193 	case TCPS_CLOSED:
15194 	case TCPS_TIME_WAIT:
15195 	default:
15196 		break;
15197 	};
15198 	if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
15199 		rack->rc_tp->t_flags2 |= TF2_MBUF_ACKCMP;
15200 
15201 }
15202 
15203 static void
15204 rack_timer_audit(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb)
15205 {
15206 	/*
15207 	 * We received an ack, and then did not
15208 	 * call send or were bounced out due to the
15209 	 * hpts was running. Now a timer is up as well, is
15210 	 * it the right timer?
15211 	 */
15212 	struct rack_sendmap *rsm;
15213 	int tmr_up;
15214 
15215 	tmr_up = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
15216 	if (tcp_in_hpts(rack->rc_tp) == 0) {
15217 		/*
15218 		 * Ok we probably need some timer up, but no
15219 		 * matter what the mask we are not in hpts. We
15220 		 * may have received an old ack and thus did nothing.
15221 		 */
15222 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
15223 		rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
15224 		return;
15225 	}
15226 	if (rack->rc_in_persist && (tmr_up == PACE_TMR_PERSIT))
15227 		return;
15228 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
15229 	if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) &&
15230 	    (tmr_up == PACE_TMR_RXT)) {
15231 		/* Should be an RXT */
15232 		return;
15233 	}
15234 	if (rsm == NULL) {
15235 		/* Nothing outstanding? */
15236 		if (tp->t_flags & TF_DELACK) {
15237 			if (tmr_up == PACE_TMR_DELACK)
15238 				/* We are supposed to have delayed ack up and we do */
15239 				return;
15240 		} else if (((V_tcp_always_keepalive ||
15241 			     rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
15242 			    (tp->t_state <= TCPS_CLOSING)) &&
15243 			   (tmr_up == PACE_TMR_KEEP) &&
15244 			   (tp->snd_max == tp->snd_una)) {
15245 			/* We should have keep alive up and we do */
15246 			return;
15247 		}
15248 	}
15249 	if (SEQ_GT(tp->snd_max, tp->snd_una) &&
15250 		   ((tmr_up == PACE_TMR_TLP) ||
15251 		    (tmr_up == PACE_TMR_RACK) ||
15252 		    (tmr_up == PACE_TMR_RXT))) {
15253 		/*
15254 		 * Either a Rack, TLP or RXT is fine if  we
15255 		 * have outstanding data.
15256 		 */
15257 		return;
15258 	} else if (tmr_up == PACE_TMR_DELACK) {
15259 		/*
15260 		 * If the delayed ack was going to go off
15261 		 * before the rtx/tlp/rack timer were going to
15262 		 * expire, then that would be the timer in control.
15263 		 * Note we don't check the time here trusting the
15264 		 * code is correct.
15265 		 */
15266 		return;
15267 	}
15268 	/*
15269 	 * Ok the timer originally started is not what we want now.
15270 	 * We will force the hpts to be stopped if any, and restart
15271 	 * with the slot set to what was in the saved slot.
15272 	 */
15273 	if (tcp_in_hpts(rack->rc_tp)) {
15274 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
15275 			uint32_t us_cts;
15276 
15277 			us_cts = tcp_get_usecs(NULL);
15278 			if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
15279 				rack->r_early = 1;
15280 				rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
15281 			}
15282 			rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
15283 		}
15284 		tcp_hpts_remove(rack->rc_tp);
15285 	}
15286 	rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
15287 	rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
15288 }
15289 
15290 
15291 static void
15292 rack_do_win_updates(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tiwin, uint32_t seq, uint32_t ack, uint32_t cts)
15293 {
15294 	if ((SEQ_LT(tp->snd_wl1, seq) ||
15295 	    (tp->snd_wl1 == seq && (SEQ_LT(tp->snd_wl2, ack) ||
15296 	    (tp->snd_wl2 == ack && tiwin > tp->snd_wnd))))) {
15297 		/* keep track of pure window updates */
15298 		if ((tp->snd_wl2 == ack) && (tiwin > tp->snd_wnd))
15299 			KMOD_TCPSTAT_INC(tcps_rcvwinupd);
15300 		tp->snd_wnd = tiwin;
15301 		rack_validate_fo_sendwin_up(tp, rack);
15302 		tp->snd_wl1 = seq;
15303 		tp->snd_wl2 = ack;
15304 		if (tp->snd_wnd > tp->max_sndwnd)
15305 			tp->max_sndwnd = tp->snd_wnd;
15306 	    rack->r_wanted_output = 1;
15307 	} else if ((tp->snd_wl2 == ack) && (tiwin < tp->snd_wnd)) {
15308 		tp->snd_wnd = tiwin;
15309 		rack_validate_fo_sendwin_up(tp, rack);
15310 		tp->snd_wl1 = seq;
15311 		tp->snd_wl2 = ack;
15312 	} else {
15313 		/* Not a valid win update */
15314 		return;
15315 	}
15316 	if (tp->snd_wnd > tp->max_sndwnd)
15317 		tp->max_sndwnd = tp->snd_wnd;
15318 	/* Do we exit persists? */
15319 	if ((rack->rc_in_persist != 0) &&
15320 	    (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
15321 				rack->r_ctl.rc_pace_min_segs))) {
15322 		rack_exit_persist(tp, rack, cts);
15323 	}
15324 	/* Do we enter persists? */
15325 	if ((rack->rc_in_persist == 0) &&
15326 	    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
15327 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
15328 	    ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) &&
15329 	    sbavail(&tptosocket(tp)->so_snd) &&
15330 	    (sbavail(&tptosocket(tp)->so_snd) > tp->snd_wnd)) {
15331 		/*
15332 		 * Here the rwnd is less than
15333 		 * the pacing size, we are established,
15334 		 * nothing is outstanding, and there is
15335 		 * data to send. Enter persists.
15336 		 */
15337 		rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, ack);
15338 	}
15339 }
15340 
15341 static void
15342 rack_log_input_packet(struct tcpcb *tp, struct tcp_rack *rack, struct tcp_ackent *ae, int ackval, uint32_t high_seq)
15343 {
15344 
15345 	if (tcp_bblogging_on(rack->rc_tp)) {
15346 		struct inpcb *inp = tptoinpcb(tp);
15347 		union tcp_log_stackspecific log;
15348 		struct timeval ltv;
15349 		char tcp_hdr_buf[60];
15350 		struct tcphdr *th;
15351 		struct timespec ts;
15352 		uint32_t orig_snd_una;
15353 		uint8_t xx = 0;
15354 
15355 #ifdef TCP_REQUEST_TRK
15356 		struct tcp_sendfile_track *tcp_req;
15357 
15358 		if (SEQ_GT(ae->ack, tp->snd_una)) {
15359 			tcp_req = tcp_req_find_req_for_seq(tp, (ae->ack-1));
15360 		} else {
15361 			tcp_req = tcp_req_find_req_for_seq(tp, ae->ack);
15362 		}
15363 #endif
15364 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
15365 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
15366 		if (rack->rack_no_prr == 0)
15367 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
15368 		else
15369 			log.u_bbr.flex1 = 0;
15370 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
15371 		log.u_bbr.use_lt_bw <<= 1;
15372 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
15373 		log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced;
15374 		log.u_bbr.bbr_state = rack->rc_free_cnt;
15375 		log.u_bbr.inflight = ctf_flight_size(tp, rack->r_ctl.rc_sacked);
15376 		log.u_bbr.pkts_out = tp->t_maxseg;
15377 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
15378 		log.u_bbr.flex7 = 1;
15379 		log.u_bbr.lost = ae->flags;
15380 		log.u_bbr.cwnd_gain = ackval;
15381 		log.u_bbr.pacing_gain = 0x2;
15382 		if (ae->flags & TSTMP_HDWR) {
15383 			/* Record the hardware timestamp if present */
15384 			log.u_bbr.flex3 = M_TSTMP;
15385 			ts.tv_sec = ae->timestamp / 1000000000;
15386 			ts.tv_nsec = ae->timestamp % 1000000000;
15387 			ltv.tv_sec = ts.tv_sec;
15388 			ltv.tv_usec = ts.tv_nsec / 1000;
15389 			log.u_bbr.lt_epoch = tcp_tv_to_usectick(&ltv);
15390 		} else if (ae->flags & TSTMP_LRO) {
15391 			/* Record the LRO the arrival timestamp */
15392 			log.u_bbr.flex3 = M_TSTMP_LRO;
15393 			ts.tv_sec = ae->timestamp / 1000000000;
15394 			ts.tv_nsec = ae->timestamp % 1000000000;
15395 			ltv.tv_sec = ts.tv_sec;
15396 			ltv.tv_usec = ts.tv_nsec / 1000;
15397 			log.u_bbr.flex5 = tcp_tv_to_usectick(&ltv);
15398 		}
15399 		log.u_bbr.timeStamp = tcp_get_usecs(&ltv);
15400 		/* Log the rcv time */
15401 		log.u_bbr.delRate = ae->timestamp;
15402 #ifdef TCP_REQUEST_TRK
15403 		log.u_bbr.applimited = tp->t_tcpreq_closed;
15404 		log.u_bbr.applimited <<= 8;
15405 		log.u_bbr.applimited |= tp->t_tcpreq_open;
15406 		log.u_bbr.applimited <<= 8;
15407 		log.u_bbr.applimited |= tp->t_tcpreq_req;
15408 		if (tcp_req) {
15409 			/* Copy out any client req info */
15410 			/* seconds */
15411 			log.u_bbr.pkt_epoch = (tcp_req->localtime / HPTS_USEC_IN_SEC);
15412 			/* useconds */
15413 			log.u_bbr.delivered = (tcp_req->localtime % HPTS_USEC_IN_SEC);
15414 			log.u_bbr.rttProp = tcp_req->timestamp;
15415 			log.u_bbr.cur_del_rate = tcp_req->start;
15416 			if (tcp_req->flags & TCP_TRK_TRACK_FLG_OPEN) {
15417 				log.u_bbr.flex8 |= 1;
15418 			} else {
15419 				log.u_bbr.flex8 |= 2;
15420 				log.u_bbr.bw_inuse = tcp_req->end;
15421 			}
15422 			log.u_bbr.flex6 = tcp_req->start_seq;
15423 			if (tcp_req->flags & TCP_TRK_TRACK_FLG_COMP) {
15424 				log.u_bbr.flex8 |= 4;
15425 				log.u_bbr.epoch = tcp_req->end_seq;
15426 			}
15427 		}
15428 #endif
15429 		memset(tcp_hdr_buf, 0, sizeof(tcp_hdr_buf));
15430 		th = (struct tcphdr *)tcp_hdr_buf;
15431 		th->th_seq = ae->seq;
15432 		th->th_ack = ae->ack;
15433 		th->th_win = ae->win;
15434 		/* Now fill in the ports */
15435 		th->th_sport = inp->inp_fport;
15436 		th->th_dport = inp->inp_lport;
15437 		tcp_set_flags(th, ae->flags);
15438 		/* Now do we have a timestamp option? */
15439 		if (ae->flags & HAS_TSTMP) {
15440 			u_char *cp;
15441 			uint32_t val;
15442 
15443 			th->th_off = ((sizeof(struct tcphdr) + TCPOLEN_TSTAMP_APPA) >> 2);
15444 			cp = (u_char *)(th + 1);
15445 			*cp = TCPOPT_NOP;
15446 			cp++;
15447 			*cp = TCPOPT_NOP;
15448 			cp++;
15449 			*cp = TCPOPT_TIMESTAMP;
15450 			cp++;
15451 			*cp = TCPOLEN_TIMESTAMP;
15452 			cp++;
15453 			val = htonl(ae->ts_value);
15454 			bcopy((char *)&val,
15455 			      (char *)cp, sizeof(uint32_t));
15456 			val = htonl(ae->ts_echo);
15457 			bcopy((char *)&val,
15458 			      (char *)(cp + 4), sizeof(uint32_t));
15459 		} else
15460 			th->th_off = (sizeof(struct tcphdr) >> 2);
15461 
15462 		/*
15463 		 * For sane logging we need to play a little trick.
15464 		 * If the ack were fully processed we would have moved
15465 		 * snd_una to high_seq, but since compressed acks are
15466 		 * processed in two phases, at this point (logging) snd_una
15467 		 * won't be advanced. So we would see multiple acks showing
15468 		 * the advancement. We can prevent that by "pretending" that
15469 		 * snd_una was advanced and then un-advancing it so that the
15470 		 * logging code has the right value for tlb_snd_una.
15471 		 */
15472 		if (tp->snd_una != high_seq) {
15473 			orig_snd_una = tp->snd_una;
15474 			tp->snd_una = high_seq;
15475 			xx = 1;
15476 		} else
15477 			xx = 0;
15478 		TCP_LOG_EVENTP(tp, th,
15479 			       &tptosocket(tp)->so_rcv,
15480 			       &tptosocket(tp)->so_snd, TCP_LOG_IN, 0,
15481 			       0, &log, true, &ltv);
15482 		if (xx) {
15483 			tp->snd_una = orig_snd_una;
15484 		}
15485 	}
15486 
15487 }
15488 
15489 static void
15490 rack_handle_probe_response(struct tcp_rack *rack, uint32_t tiwin, uint32_t us_cts)
15491 {
15492 	uint32_t us_rtt;
15493 	/*
15494 	 * A persist or keep-alive was forced out, update our
15495 	 * min rtt time. Note now worry about lost responses.
15496 	 * When a subsequent keep-alive or persist times out
15497 	 * and forced_ack is still on, then the last probe
15498 	 * was not responded to. In such cases we have a
15499 	 * sysctl that controls the behavior. Either we apply
15500 	 * the rtt but with reduced confidence (0). Or we just
15501 	 * plain don't apply the rtt estimate. Having data flow
15502 	 * will clear the probe_not_answered flag i.e. cum-ack
15503 	 * move forward <or> exiting and reentering persists.
15504 	 */
15505 
15506 	rack->forced_ack = 0;
15507 	rack->rc_tp->t_rxtshift = 0;
15508 	if ((rack->rc_in_persist &&
15509 	     (tiwin == rack->rc_tp->snd_wnd)) ||
15510 	    (rack->rc_in_persist == 0)) {
15511 		/*
15512 		 * In persists only apply the RTT update if this is
15513 		 * a response to our window probe. And that
15514 		 * means the rwnd sent must match the current
15515 		 * snd_wnd. If it does not, then we got a
15516 		 * window update ack instead. For keepalive
15517 		 * we allow the answer no matter what the window.
15518 		 *
15519 		 * Note that if the probe_not_answered is set then
15520 		 * the forced_ack_ts is the oldest one i.e. the first
15521 		 * probe sent that might have been lost. This assures
15522 		 * us that if we do calculate an RTT it is longer not
15523 		 * some short thing.
15524 		 */
15525 		if (rack->rc_in_persist)
15526 			counter_u64_add(rack_persists_acks, 1);
15527 		us_rtt = us_cts - rack->r_ctl.forced_ack_ts;
15528 		if (us_rtt == 0)
15529 			us_rtt = 1;
15530 		if (rack->probe_not_answered == 0) {
15531 			rack_apply_updated_usrtt(rack, us_rtt, us_cts);
15532 			tcp_rack_xmit_timer(rack, us_rtt, 0, us_rtt, 3, NULL, 1);
15533 		} else {
15534 			/* We have a retransmitted probe here too */
15535 			if (rack_apply_rtt_with_reduced_conf) {
15536 				rack_apply_updated_usrtt(rack, us_rtt, us_cts);
15537 				tcp_rack_xmit_timer(rack, us_rtt, 0, us_rtt, 0, NULL, 1);
15538 			}
15539 		}
15540 	}
15541 }
15542 
15543 static void
15544 rack_new_round_starts(struct tcpcb *tp, struct tcp_rack *rack, uint32_t high_seq)
15545 {
15546 	/*
15547 	 * The next send has occurred mark the end of the round
15548 	 * as when that data gets acknowledged. We can
15549 	 * also do common things we might need to do when
15550 	 * a round begins.
15551 	 */
15552 	rack->r_ctl.roundends = tp->snd_max;
15553 	rack->rc_new_rnd_needed = 0;
15554 	rack_log_hystart_event(rack, tp->snd_max, 4);
15555 }
15556 
15557 
15558 static void
15559 rack_log_pcm(struct tcp_rack *rack, uint8_t mod, uint32_t flex1, uint32_t flex2,
15560 	     uint32_t flex3)
15561 {
15562 	if (tcp_bblogging_on(rack->rc_tp)) {
15563 		union tcp_log_stackspecific log;
15564 		struct timeval tv;
15565 
15566 		(void)tcp_get_usecs(&tv);
15567 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
15568 		log.u_bbr.timeStamp = tcp_tv_to_usectick(&tv);
15569 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
15570 		log.u_bbr.flex8 = mod;
15571 		log.u_bbr.flex1 = flex1;
15572 		log.u_bbr.flex2 = flex2;
15573 		log.u_bbr.flex3 = flex3;
15574 		log.u_bbr.flex4 = rack_pcm_every_n_rounds;
15575 		log.u_bbr.flex5 = rack->r_ctl.pcm_idle_rounds;
15576 		log.u_bbr.bbr_substate = rack->pcm_needed;
15577 		log.u_bbr.bbr_substate <<= 1;
15578 		log.u_bbr.bbr_substate |= rack->pcm_in_progress;
15579 		log.u_bbr.bbr_substate <<= 1;
15580 		log.u_bbr.bbr_substate |= rack->pcm_enabled; /* bits are NIE for Needed, Inprogress, Enabled */
15581 		(void)tcp_log_event(rack->rc_tp, NULL, NULL, NULL, TCP_PCM_MEASURE, ERRNO_UNK,
15582 				    0, &log, false, NULL, NULL, 0, &tv);
15583 	}
15584 }
15585 
15586 static void
15587 rack_new_round_setup(struct tcpcb *tp, struct tcp_rack *rack, uint32_t high_seq)
15588 {
15589 	/*
15590 	 * The round (current_round) has ended. We now
15591 	 * setup for the next round by incrementing the
15592 	 * round numnber and doing any round specific
15593 	 * things.
15594 	 */
15595 	rack_log_hystart_event(rack, high_seq, 21);
15596 	rack->r_ctl.current_round++;
15597 	/* New round (current_round) begins at next send */
15598 	rack->rc_new_rnd_needed = 1;
15599 	if ((rack->pcm_enabled == 1) &&
15600 	    (rack->pcm_needed == 0) &&
15601 	    (rack->pcm_in_progress == 0)) {
15602 		/*
15603 		 * If we have enabled PCM, then we need to
15604 		 * check if the round has adanced to the state
15605 		 * where one is required.
15606 		 */
15607 		int rnds;
15608 
15609 		rnds = rack->r_ctl.current_round - rack->r_ctl.last_pcm_round;
15610 		if ((rnds + rack->r_ctl.pcm_idle_rounds) >= rack_pcm_every_n_rounds) {
15611 			rack->pcm_needed = 1;
15612 			rack_log_pcm(rack, 3, rack->r_ctl.last_pcm_round, rack_pcm_every_n_rounds, rack->r_ctl.current_round );
15613 		} else if (rack_verbose_logging) {
15614 			rack_log_pcm(rack, 3, rack->r_ctl.last_pcm_round, rack_pcm_every_n_rounds, rack->r_ctl.current_round );
15615 		}
15616 	}
15617 	if (tp->t_ccv.flags & CCF_HYSTART_ALLOWED) {
15618 		/* We have hystart enabled send the round info in */
15619 		if (CC_ALGO(tp)->newround != NULL) {
15620 			CC_ALGO(tp)->newround(&tp->t_ccv, rack->r_ctl.current_round);
15621 		}
15622 	}
15623 	/*
15624 	 * For DGP an initial startup check. We want to validate
15625 	 * that we are not just pushing on slow-start and just
15626 	 * not gaining.. i.e. filling buffers without getting any
15627 	 * boost in b/w during the inital slow-start.
15628 	 */
15629 	if (rack->dgp_on &&
15630 	    (rack->rc_initial_ss_comp == 0) &&
15631 	    (tp->snd_cwnd < tp->snd_ssthresh) &&
15632 	    (rack->r_ctl.num_measurements >= RACK_REQ_AVG) &&
15633 	    (rack->r_ctl.gp_rnd_thresh > 0) &&
15634 	    ((rack->r_ctl.current_round - rack->r_ctl.last_rnd_of_gp_rise) >= rack->r_ctl.gp_rnd_thresh)) {
15635 
15636 		/*
15637 		 * We are in the initial SS and we have hd rack_rnd_cnt_req rounds(def:5) where
15638 		 * we have not gained the required amount in the gp_est (120.0% aka 1200). Lets
15639 		 * exit SS.
15640 		 *
15641 		 * Pick up the flight size now as we enter slowstart (not the
15642 		 * cwnd which may be inflated).
15643 		 */
15644 		rack->rc_initial_ss_comp = 1;
15645 
15646 		if (tcp_bblogging_on(rack->rc_tp)) {
15647 			union tcp_log_stackspecific log;
15648 			struct timeval tv;
15649 
15650 			memset(&log.u_bbr, 0, sizeof(log.u_bbr));
15651 			log.u_bbr.timeStamp = tcp_get_usecs(&tv);
15652 			log.u_bbr.flex1 = rack->r_ctl.current_round;
15653 			log.u_bbr.flex2 = rack->r_ctl.last_rnd_of_gp_rise;
15654 			log.u_bbr.flex3 = rack->r_ctl.gp_rnd_thresh;
15655 			log.u_bbr.flex4 = rack->r_ctl.gate_to_fs;
15656 			log.u_bbr.flex5 = rack->r_ctl.ss_hi_fs;
15657 			log.u_bbr.flex8 = 40;
15658 			(void)tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
15659 					    0, &log, false, NULL, __func__, __LINE__,&tv);
15660 		}
15661 		if ((rack->r_ctl.gate_to_fs == 1) &&
15662 		     (tp->snd_cwnd > rack->r_ctl.ss_hi_fs)) {
15663 			tp->snd_cwnd = rack->r_ctl.ss_hi_fs;
15664 		}
15665 		tp->snd_ssthresh = tp->snd_cwnd - 1;
15666 		/* Turn off any fast output running */
15667 		rack->r_fast_output = 0;
15668 	}
15669 }
15670 
15671 static int
15672 rack_do_compressed_ack_processing(struct tcpcb *tp, struct socket *so, struct mbuf *m, int nxt_pkt, struct timeval *tv)
15673 {
15674 	/*
15675 	 * Handle a "special" compressed ack mbuf. Each incoming
15676 	 * ack has only four possible dispositions:
15677 	 *
15678 	 * A) It moves the cum-ack forward
15679 	 * B) It is behind the cum-ack.
15680 	 * C) It is a window-update ack.
15681 	 * D) It is a dup-ack.
15682 	 *
15683 	 * Note that we can have between 1 -> TCP_COMP_ACK_ENTRIES
15684 	 * in the incoming mbuf. We also need to still pay attention
15685 	 * to nxt_pkt since there may be another packet after this
15686 	 * one.
15687 	 */
15688 #ifdef TCP_ACCOUNTING
15689 	uint64_t ts_val;
15690 	uint64_t rdstc;
15691 #endif
15692 	int segsiz;
15693 	struct timespec ts;
15694 	struct tcp_rack *rack;
15695 	struct tcp_ackent *ae;
15696 	uint32_t tiwin, ms_cts, cts, acked, acked_amount, high_seq, win_seq, the_win, win_upd_ack;
15697 	int cnt, i, did_out, ourfinisacked = 0;
15698 	struct tcpopt to_holder, *to = NULL;
15699 #ifdef TCP_ACCOUNTING
15700 	int win_up_req = 0;
15701 #endif
15702 	int nsegs = 0;
15703 	int under_pacing = 0;
15704 	int post_recovery = 0;
15705 #ifdef TCP_ACCOUNTING
15706 	sched_pin();
15707 #endif
15708 	rack = (struct tcp_rack *)tp->t_fb_ptr;
15709 	if (rack->gp_ready &&
15710 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT))
15711 		under_pacing = 1;
15712 
15713 	if (rack->r_state != tp->t_state)
15714 		rack_set_state(tp, rack);
15715 	if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
15716 	    (tp->t_flags & TF_GPUTINPROG)) {
15717 		/*
15718 		 * We have a goodput in progress
15719 		 * and we have entered a late state.
15720 		 * Do we have enough data in the sb
15721 		 * to handle the GPUT request?
15722 		 */
15723 		uint32_t bytes;
15724 
15725 		bytes = tp->gput_ack - tp->gput_seq;
15726 		if (SEQ_GT(tp->gput_seq, tp->snd_una))
15727 			bytes += tp->gput_seq - tp->snd_una;
15728 		if (bytes > sbavail(&tptosocket(tp)->so_snd)) {
15729 			/*
15730 			 * There are not enough bytes in the socket
15731 			 * buffer that have been sent to cover this
15732 			 * measurement. Cancel it.
15733 			 */
15734 			rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
15735 						   rack->r_ctl.rc_gp_srtt /*flex1*/,
15736 						   tp->gput_seq,
15737 						   0, 0, 18, __LINE__, NULL, 0);
15738 			tp->t_flags &= ~TF_GPUTINPROG;
15739 		}
15740 	}
15741 	to = &to_holder;
15742 	to->to_flags = 0;
15743 	KASSERT((m->m_len >= sizeof(struct tcp_ackent)),
15744 		("tp:%p m_cmpack:%p with invalid len:%u", tp, m, m->m_len));
15745 	cnt = m->m_len / sizeof(struct tcp_ackent);
15746 	counter_u64_add(rack_multi_single_eq, cnt);
15747 	high_seq = tp->snd_una;
15748 	the_win = tp->snd_wnd;
15749 	win_seq = tp->snd_wl1;
15750 	win_upd_ack = tp->snd_wl2;
15751 	cts = tcp_tv_to_usectick(tv);
15752 	ms_cts = tcp_tv_to_mssectick(tv);
15753 	rack->r_ctl.rc_rcvtime = cts;
15754 	segsiz = ctf_fixed_maxseg(tp);
15755 	if ((rack->rc_gp_dyn_mul) &&
15756 	    (rack->use_fixed_rate == 0) &&
15757 	    (rack->rc_always_pace)) {
15758 		/* Check in on probertt */
15759 		rack_check_probe_rtt(rack, cts);
15760 	}
15761 	for (i = 0; i < cnt; i++) {
15762 #ifdef TCP_ACCOUNTING
15763 		ts_val = get_cyclecount();
15764 #endif
15765 		rack_clear_rate_sample(rack);
15766 		ae = ((mtod(m, struct tcp_ackent *)) + i);
15767 		if (ae->flags & TH_FIN)
15768 			rack_log_pacing_delay_calc(rack,
15769 						   0,
15770 						   0,
15771 						   0,
15772 						   rack_get_gp_est(rack), /* delRate */
15773 						   rack_get_lt_bw(rack), /* rttProp */
15774 						   20, __LINE__, NULL, 0);
15775 		/* Setup the window */
15776 		tiwin = ae->win << tp->snd_scale;
15777 		if (tiwin > rack->r_ctl.rc_high_rwnd)
15778 			rack->r_ctl.rc_high_rwnd = tiwin;
15779 		/* figure out the type of ack */
15780 		if (SEQ_LT(ae->ack, high_seq)) {
15781 			/* Case B*/
15782 			ae->ack_val_set = ACK_BEHIND;
15783 		} else if (SEQ_GT(ae->ack, high_seq)) {
15784 			/* Case A */
15785 			ae->ack_val_set = ACK_CUMACK;
15786 		} else if ((tiwin == the_win) && (rack->rc_in_persist == 0)){
15787 			/* Case D */
15788 			ae->ack_val_set = ACK_DUPACK;
15789 		} else {
15790 			/* Case C */
15791 			ae->ack_val_set = ACK_RWND;
15792 		}
15793 		rack_log_type_bbrsnd(rack, 0, 0, cts, tv, __LINE__);
15794 		rack_log_input_packet(tp, rack, ae, ae->ack_val_set, high_seq);
15795 		/* Validate timestamp */
15796 		if (ae->flags & HAS_TSTMP) {
15797 			/* Setup for a timestamp */
15798 			to->to_flags = TOF_TS;
15799 			ae->ts_echo -= tp->ts_offset;
15800 			to->to_tsecr = ae->ts_echo;
15801 			to->to_tsval = ae->ts_value;
15802 			/*
15803 			 * If echoed timestamp is later than the current time, fall back to
15804 			 * non RFC1323 RTT calculation.  Normalize timestamp if syncookies
15805 			 * were used when this connection was established.
15806 			 */
15807 			if (TSTMP_GT(ae->ts_echo, ms_cts))
15808 				to->to_tsecr = 0;
15809 			if (tp->ts_recent &&
15810 			    TSTMP_LT(ae->ts_value, tp->ts_recent)) {
15811 				if (ctf_ts_check_ac(tp, (ae->flags & 0xff))) {
15812 #ifdef TCP_ACCOUNTING
15813 					rdstc = get_cyclecount();
15814 					if (rdstc > ts_val) {
15815 						if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
15816 							tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val);
15817 						}
15818 					}
15819 #endif
15820 					continue;
15821 				}
15822 			}
15823 			if (SEQ_LEQ(ae->seq, tp->last_ack_sent) &&
15824 			    SEQ_LEQ(tp->last_ack_sent, ae->seq)) {
15825 				tp->ts_recent_age = tcp_ts_getticks();
15826 				tp->ts_recent = ae->ts_value;
15827 			}
15828 		} else {
15829 			/* Setup for a no options */
15830 			to->to_flags = 0;
15831 		}
15832 		/* Update the rcv time and perform idle reduction possibly */
15833 		if  (tp->t_idle_reduce &&
15834 		     (tp->snd_max == tp->snd_una) &&
15835 		     (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur)) {
15836 			counter_u64_add(rack_input_idle_reduces, 1);
15837 			rack_cc_after_idle(rack, tp);
15838 		}
15839 		tp->t_rcvtime = ticks;
15840 		/* Now what about ECN of a chain of pure ACKs? */
15841 		if (tcp_ecn_input_segment(tp, ae->flags, 0,
15842 			tcp_packets_this_ack(tp, ae->ack),
15843 			ae->codepoint))
15844 			rack_cong_signal(tp, CC_ECN, ae->ack, __LINE__);
15845 #ifdef TCP_ACCOUNTING
15846 		/* Count for the specific type of ack in */
15847 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
15848 			tp->tcp_cnt_counters[ae->ack_val_set]++;
15849 		}
15850 #endif
15851 		/*
15852 		 * Note how we could move up these in the determination
15853 		 * above, but we don't so that way the timestamp checks (and ECN)
15854 		 * is done first before we do any processing on the ACK.
15855 		 * The non-compressed path through the code has this
15856 		 * weakness (noted by @jtl) that it actually does some
15857 		 * processing before verifying the timestamp information.
15858 		 * We don't take that path here which is why we set
15859 		 * the ack_val_set first, do the timestamp and ecn
15860 		 * processing, and then look at what we have setup.
15861 		 */
15862 		if (ae->ack_val_set == ACK_BEHIND) {
15863 			/*
15864 			 * Case B flag reordering, if window is not closed
15865 			 * or it could be a keep-alive or persists
15866 			 */
15867 			if (SEQ_LT(ae->ack, tp->snd_una) && (sbspace(&so->so_rcv) > segsiz)) {
15868 				rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
15869 				if (rack->r_ctl.rc_reorder_ts == 0)
15870 					rack->r_ctl.rc_reorder_ts = 1;
15871 			}
15872 		} else if (ae->ack_val_set == ACK_DUPACK) {
15873 			/* Case D */
15874 			rack_strike_dupack(rack, ae->ack);
15875 		} else if (ae->ack_val_set == ACK_RWND) {
15876 			/* Case C */
15877 			if ((ae->flags & TSTMP_LRO) || (ae->flags & TSTMP_HDWR)) {
15878 				ts.tv_sec = ae->timestamp / 1000000000;
15879 				ts.tv_nsec = ae->timestamp % 1000000000;
15880 				rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
15881 				rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
15882 			} else {
15883 				rack->r_ctl.act_rcv_time = *tv;
15884 			}
15885 			if (rack->forced_ack) {
15886 				rack_handle_probe_response(rack, tiwin,
15887 							   tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time));
15888 			}
15889 #ifdef TCP_ACCOUNTING
15890 			win_up_req = 1;
15891 #endif
15892 			win_upd_ack = ae->ack;
15893 			win_seq = ae->seq;
15894 			the_win = tiwin;
15895 			rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts);
15896 		} else {
15897 			/* Case A */
15898 			if (SEQ_GT(ae->ack, tp->snd_max)) {
15899 				/*
15900 				 * We just send an ack since the incoming
15901 				 * ack is beyond the largest seq we sent.
15902 				 */
15903 				if ((tp->t_flags & TF_ACKNOW) == 0) {
15904 					ctf_ack_war_checks(tp);
15905 					if (tp->t_flags && TF_ACKNOW)
15906 						rack->r_wanted_output = 1;
15907 				}
15908 			} else {
15909 				nsegs++;
15910 				/* If the window changed setup to update */
15911 				if (tiwin != tp->snd_wnd) {
15912 					win_upd_ack = ae->ack;
15913 					win_seq = ae->seq;
15914 					the_win = tiwin;
15915 					rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts);
15916 				}
15917 #ifdef TCP_ACCOUNTING
15918 				/* Account for the acks */
15919 				if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
15920 					tp->tcp_cnt_counters[CNT_OF_ACKS_IN] += (((ae->ack - high_seq) + segsiz - 1) / segsiz);
15921 				}
15922 #endif
15923 				high_seq = ae->ack;
15924 				/* Setup our act_rcv_time */
15925 				if ((ae->flags & TSTMP_LRO) || (ae->flags & TSTMP_HDWR)) {
15926 					ts.tv_sec = ae->timestamp / 1000000000;
15927 					ts.tv_nsec = ae->timestamp % 1000000000;
15928 					rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
15929 					rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
15930 				} else {
15931 					rack->r_ctl.act_rcv_time = *tv;
15932 				}
15933 				rack_process_to_cumack(tp, rack, ae->ack, cts, to,
15934 						       tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time));
15935 #ifdef TCP_REQUEST_TRK
15936 				rack_req_check_for_comp(rack, high_seq);
15937 #endif
15938 				if (rack->rc_dsack_round_seen) {
15939 					/* Is the dsack round over? */
15940 					if (SEQ_GEQ(ae->ack, rack->r_ctl.dsack_round_end)) {
15941 						/* Yes it is */
15942 						rack->rc_dsack_round_seen = 0;
15943 						rack_log_dsack_event(rack, 3, __LINE__, 0, 0);
15944 					}
15945 				}
15946 			}
15947 		}
15948 		/* And lets be sure to commit the rtt measurements for this ack */
15949 		tcp_rack_xmit_timer_commit(rack, tp);
15950 #ifdef TCP_ACCOUNTING
15951 		rdstc = get_cyclecount();
15952 		if (rdstc > ts_val) {
15953 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
15954 				tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val);
15955 				if (ae->ack_val_set == ACK_CUMACK)
15956 					tp->tcp_proc_time[CYC_HANDLE_MAP] += (rdstc - ts_val);
15957 			}
15958 		}
15959 #endif
15960 	}
15961 #ifdef TCP_ACCOUNTING
15962 	ts_val = get_cyclecount();
15963 #endif
15964 	/* Tend to any collapsed window */
15965 	if (SEQ_GT(tp->snd_max, high_seq) && (tp->snd_wnd < (tp->snd_max - high_seq))) {
15966 		/* The peer collapsed the window */
15967 		rack_collapsed_window(rack, (tp->snd_max - high_seq), high_seq, __LINE__);
15968 	} else if (rack->rc_has_collapsed)
15969 		rack_un_collapse_window(rack, __LINE__);
15970 	if ((rack->r_collapse_point_valid) &&
15971 	    (SEQ_GT(high_seq, rack->r_ctl.high_collapse_point)))
15972 		rack->r_collapse_point_valid = 0;
15973 	acked_amount = acked = (high_seq - tp->snd_una);
15974 	if (acked) {
15975 		/*
15976 		 * The draft (v3) calls for us to use SEQ_GEQ, but that
15977 		 * causes issues when we are just going app limited. Lets
15978 		 * instead use SEQ_GT <or> where its equal but more data
15979 		 * is outstanding.
15980 		 *
15981 		 * Also make sure we are on the last ack of a series. We
15982 		 * have to have all the ack's processed in queue to know
15983 		 * if there is something left outstanding.
15984 		 *
15985 		 */
15986 		if (SEQ_GEQ(high_seq, rack->r_ctl.roundends) &&
15987 		    (rack->rc_new_rnd_needed == 0) &&
15988 		    (nxt_pkt == 0)) {
15989 			/*
15990 			 * We have crossed into a new round with
15991 			 * this th_ack value.
15992 			 */
15993 			rack_new_round_setup(tp, rack, high_seq);
15994 		}
15995 		/*
15996 		 * Clear the probe not answered flag
15997 		 * since cum-ack moved forward.
15998 		 */
15999 		rack->probe_not_answered = 0;
16000 		if (tp->t_flags & TF_NEEDSYN) {
16001 			/*
16002 			 * T/TCP: Connection was half-synchronized, and our SYN has
16003 			 * been ACK'd (so connection is now fully synchronized).  Go
16004 			 * to non-starred state, increment snd_una for ACK of SYN,
16005 			 * and check if we can do window scaling.
16006 			 */
16007 			tp->t_flags &= ~TF_NEEDSYN;
16008 			tp->snd_una++;
16009 			acked_amount = acked = (high_seq - tp->snd_una);
16010 		}
16011 		if (acked > sbavail(&so->so_snd))
16012 			acked_amount = sbavail(&so->so_snd);
16013 		if (IN_FASTRECOVERY(tp->t_flags) &&
16014 		    (rack->rack_no_prr == 0))
16015 			rack_update_prr(tp, rack, acked_amount, high_seq);
16016 		if (IN_RECOVERY(tp->t_flags)) {
16017 			if (SEQ_LT(high_seq, tp->snd_recover) &&
16018 			    (SEQ_LT(high_seq, tp->snd_max))) {
16019 				tcp_rack_partialack(tp);
16020 			} else {
16021 				rack_post_recovery(tp, high_seq);
16022 				post_recovery = 1;
16023 			}
16024 		}  else if ((rack->rto_from_rec == 1) &&
16025 			    SEQ_GEQ(high_seq, tp->snd_recover)) {
16026 			/*
16027 			 * We were in recovery, hit a rxt timeout
16028 			 * and never re-entered recovery. The timeout(s)
16029 			 * made up all the lost data. In such a case
16030 			 * we need to clear the rto_from_rec flag.
16031 			 */
16032 			rack->rto_from_rec = 0;
16033 		}
16034 		/* Handle the rack-log-ack part (sendmap) */
16035 		if ((sbused(&so->so_snd) == 0) &&
16036 		    (acked > acked_amount) &&
16037 		    (tp->t_state >= TCPS_FIN_WAIT_1) &&
16038 		    (tp->t_flags & TF_SENTFIN)) {
16039 			/*
16040 			 * We must be sure our fin
16041 			 * was sent and acked (we can be
16042 			 * in FIN_WAIT_1 without having
16043 			 * sent the fin).
16044 			 */
16045 			ourfinisacked = 1;
16046 			/*
16047 			 * Lets make sure snd_una is updated
16048 			 * since most likely acked_amount = 0 (it
16049 			 * should be).
16050 			 */
16051 			tp->snd_una = high_seq;
16052 		}
16053 		/* Did we make a RTO error? */
16054 		if ((tp->t_flags & TF_PREVVALID) &&
16055 		    ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
16056 			tp->t_flags &= ~TF_PREVVALID;
16057 			if (tp->t_rxtshift == 1 &&
16058 			    (int)(ticks - tp->t_badrxtwin) < 0)
16059 				rack_cong_signal(tp, CC_RTO_ERR, high_seq, __LINE__);
16060 		}
16061 		/* Handle the data in the socket buffer */
16062 		KMOD_TCPSTAT_ADD(tcps_rcvackpack, 1);
16063 		KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
16064 		if (acked_amount > 0) {
16065 			uint32_t p_cwnd;
16066 			struct mbuf *mfree;
16067 
16068 			if (post_recovery) {
16069 				/*
16070 				 * Grab the segsiz, multiply by 2 and add the snd_cwnd
16071 				 * that is the max the CC should add if we are exiting
16072 				 * recovery and doing a late add.
16073 				 */
16074 				p_cwnd = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
16075 				p_cwnd <<= 1;
16076 				p_cwnd += tp->snd_cwnd;
16077 			}
16078 			rack_ack_received(tp, rack, high_seq, nsegs, CC_ACK, post_recovery);
16079 			if (post_recovery && (tp->snd_cwnd > p_cwnd)) {
16080 				/* Must be non-newreno (cubic) getting too ahead of itself */
16081 				tp->snd_cwnd = p_cwnd;
16082 			}
16083 			SOCK_SENDBUF_LOCK(so);
16084 			mfree = sbcut_locked(&so->so_snd, acked_amount);
16085 			tp->snd_una = high_seq;
16086 			/* Note we want to hold the sb lock through the sendmap adjust */
16087 			rack_adjust_sendmap_head(rack, &so->so_snd);
16088 			/* Wake up the socket if we have room to write more */
16089 			rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
16090 			sowwakeup_locked(so);
16091 			m_freem(mfree);
16092 		}
16093 		/* update progress */
16094 		tp->t_acktime = ticks;
16095 		rack_log_progress_event(rack, tp, tp->t_acktime,
16096 					PROGRESS_UPDATE, __LINE__);
16097 		/* Clear out shifts and such */
16098 		tp->t_rxtshift = 0;
16099 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
16100 				   rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
16101 		rack->rc_tlp_in_progress = 0;
16102 		rack->r_ctl.rc_tlp_cnt_out = 0;
16103 		/* Send recover and snd_nxt must be dragged along */
16104 		if (SEQ_GT(tp->snd_una, tp->snd_recover))
16105 			tp->snd_recover = tp->snd_una;
16106 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
16107 			tp->snd_nxt = tp->snd_max;
16108 		/*
16109 		 * If the RXT timer is running we want to
16110 		 * stop it, so we can restart a TLP (or new RXT).
16111 		 */
16112 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
16113 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
16114 		tp->snd_wl2 = high_seq;
16115 		tp->t_dupacks = 0;
16116 		if (under_pacing &&
16117 		    (rack->use_fixed_rate == 0) &&
16118 		    (rack->in_probe_rtt == 0) &&
16119 		    rack->rc_gp_dyn_mul &&
16120 		    rack->rc_always_pace) {
16121 			/* Check if we are dragging bottom */
16122 			rack_check_bottom_drag(tp, rack, so);
16123 		}
16124 		if (tp->snd_una == tp->snd_max) {
16125 			tp->t_flags &= ~TF_PREVVALID;
16126 			rack->r_ctl.retran_during_recovery = 0;
16127 			rack->rc_suspicious = 0;
16128 			rack->r_ctl.dsack_byte_cnt = 0;
16129 			rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
16130 			if (rack->r_ctl.rc_went_idle_time == 0)
16131 				rack->r_ctl.rc_went_idle_time = 1;
16132 			rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
16133 			if (sbavail(&tptosocket(tp)->so_snd) == 0)
16134 				tp->t_acktime = 0;
16135 			/* Set so we might enter persists... */
16136 			rack->r_wanted_output = 1;
16137 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
16138 			sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
16139 			if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
16140 			    (sbavail(&so->so_snd) == 0) &&
16141 			    (tp->t_flags2 & TF2_DROP_AF_DATA)) {
16142 				/*
16143 				 * The socket was gone and the
16144 				 * peer sent data (not now in the past), time to
16145 				 * reset him.
16146 				 */
16147 				rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
16148 				/* tcp_close will kill the inp pre-log the Reset */
16149 				tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
16150 #ifdef TCP_ACCOUNTING
16151 				rdstc = get_cyclecount();
16152 				if (rdstc > ts_val) {
16153 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16154 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16155 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16156 					}
16157 				}
16158 #endif
16159 				m_freem(m);
16160 				tp = tcp_close(tp);
16161 				if (tp == NULL) {
16162 #ifdef TCP_ACCOUNTING
16163 					sched_unpin();
16164 #endif
16165 					return (1);
16166 				}
16167 				/*
16168 				 * We would normally do drop-with-reset which would
16169 				 * send back a reset. We can't since we don't have
16170 				 * all the needed bits. Instead lets arrange for
16171 				 * a call to tcp_output(). That way since we
16172 				 * are in the closed state we will generate a reset.
16173 				 *
16174 				 * Note if tcp_accounting is on we don't unpin since
16175 				 * we do that after the goto label.
16176 				 */
16177 				goto send_out_a_rst;
16178 			}
16179 			if ((sbused(&so->so_snd) == 0) &&
16180 			    (tp->t_state >= TCPS_FIN_WAIT_1) &&
16181 			    (tp->t_flags & TF_SENTFIN)) {
16182 				/*
16183 				 * If we can't receive any more data, then closing user can
16184 				 * proceed. Starting the timer is contrary to the
16185 				 * specification, but if we don't get a FIN we'll hang
16186 				 * forever.
16187 				 *
16188 				 */
16189 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
16190 					soisdisconnected(so);
16191 					tcp_timer_activate(tp, TT_2MSL,
16192 							   (tcp_fast_finwait2_recycle ?
16193 							    tcp_finwait2_timeout :
16194 							    TP_MAXIDLE(tp)));
16195 				}
16196 				if (ourfinisacked == 0) {
16197 					/*
16198 					 * We don't change to fin-wait-2 if we have our fin acked
16199 					 * which means we are probably in TCPS_CLOSING.
16200 					 */
16201 					tcp_state_change(tp, TCPS_FIN_WAIT_2);
16202 				}
16203 			}
16204 		}
16205 		/* Wake up the socket if we have room to write more */
16206 		if (sbavail(&so->so_snd)) {
16207 			rack->r_wanted_output = 1;
16208 			if (ctf_progress_timeout_check(tp, true)) {
16209 				rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
16210 							tp, tick, PROGRESS_DROP, __LINE__);
16211 				/*
16212 				 * We cheat here and don't send a RST, we should send one
16213 				 * when the pacer drops the connection.
16214 				 */
16215 #ifdef TCP_ACCOUNTING
16216 				rdstc = get_cyclecount();
16217 				if (rdstc > ts_val) {
16218 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16219 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16220 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16221 					}
16222 				}
16223 				sched_unpin();
16224 #endif
16225 				(void)tcp_drop(tp, ETIMEDOUT);
16226 				m_freem(m);
16227 				return (1);
16228 			}
16229 		}
16230 		if (ourfinisacked) {
16231 			switch(tp->t_state) {
16232 			case TCPS_CLOSING:
16233 #ifdef TCP_ACCOUNTING
16234 				rdstc = get_cyclecount();
16235 				if (rdstc > ts_val) {
16236 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16237 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16238 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16239 					}
16240 				}
16241 				sched_unpin();
16242 #endif
16243 				tcp_twstart(tp);
16244 				m_freem(m);
16245 				return (1);
16246 				break;
16247 			case TCPS_LAST_ACK:
16248 #ifdef TCP_ACCOUNTING
16249 				rdstc = get_cyclecount();
16250 				if (rdstc > ts_val) {
16251 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16252 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16253 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16254 					}
16255 				}
16256 				sched_unpin();
16257 #endif
16258 				tp = tcp_close(tp);
16259 				ctf_do_drop(m, tp);
16260 				return (1);
16261 				break;
16262 			case TCPS_FIN_WAIT_1:
16263 #ifdef TCP_ACCOUNTING
16264 				rdstc = get_cyclecount();
16265 				if (rdstc > ts_val) {
16266 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16267 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16268 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16269 					}
16270 				}
16271 #endif
16272 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
16273 					soisdisconnected(so);
16274 					tcp_timer_activate(tp, TT_2MSL,
16275 							   (tcp_fast_finwait2_recycle ?
16276 							    tcp_finwait2_timeout :
16277 							    TP_MAXIDLE(tp)));
16278 				}
16279 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
16280 				break;
16281 			default:
16282 				break;
16283 			}
16284 		}
16285 		if (rack->r_fast_output) {
16286 			/*
16287 			 * We re doing fast output.. can we expand that?
16288 			 */
16289 			rack_gain_for_fastoutput(rack, tp, so, acked_amount);
16290 		}
16291 #ifdef TCP_ACCOUNTING
16292 		rdstc = get_cyclecount();
16293 		if (rdstc > ts_val) {
16294 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16295 				tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16296 				tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16297 			}
16298 		}
16299 
16300 	} else if (win_up_req) {
16301 		rdstc = get_cyclecount();
16302 		if (rdstc > ts_val) {
16303 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16304 				tp->tcp_proc_time[ACK_RWND] += (rdstc - ts_val);
16305 			}
16306 		}
16307 #endif
16308 	}
16309 	/* Now is there a next packet, if so we are done */
16310 	m_freem(m);
16311 	did_out = 0;
16312 	if (nxt_pkt) {
16313 #ifdef TCP_ACCOUNTING
16314 		sched_unpin();
16315 #endif
16316 		rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 5, nsegs);
16317 		return (0);
16318 	}
16319 	rack_handle_might_revert(tp, rack);
16320 	ctf_calc_rwin(so, tp);
16321 	if ((rack->r_wanted_output != 0) ||
16322 	    (rack->r_fast_output != 0) ||
16323 	    (tp->t_flags & TF_ACKNOW )) {
16324 	send_out_a_rst:
16325 		if (tcp_output(tp) < 0) {
16326 #ifdef TCP_ACCOUNTING
16327 			sched_unpin();
16328 #endif
16329 			return (1);
16330 		}
16331 		did_out = 1;
16332 	}
16333 	if (tp->t_flags2 & TF2_HPTS_CALLS)
16334 		tp->t_flags2 &= ~TF2_HPTS_CALLS;
16335 	rack_free_trim(rack);
16336 #ifdef TCP_ACCOUNTING
16337 	sched_unpin();
16338 #endif
16339 	rack_timer_audit(tp, rack, &so->so_snd);
16340 	rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 6, nsegs);
16341 	return (0);
16342 }
16343 
16344 #define	TCP_LRO_TS_OPTION \
16345     ntohl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \
16346 	  (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)
16347 
16348 static int
16349 rack_do_segment_nounlock(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
16350     int32_t drop_hdrlen, int32_t tlen, uint8_t iptos, int32_t nxt_pkt,
16351     struct timeval *tv)
16352 {
16353 	struct inpcb *inp = tptoinpcb(tp);
16354 	struct socket *so = tptosocket(tp);
16355 #ifdef TCP_ACCOUNTING
16356 	uint64_t ts_val;
16357 #endif
16358 	int32_t thflags, retval, did_out = 0;
16359 	int32_t way_out = 0;
16360 	/*
16361 	 * cts - is the current time from tv (caller gets ts) in microseconds.
16362 	 * ms_cts - is the current time from tv in milliseconds.
16363 	 * us_cts - is the time that LRO or hardware actually got the packet in microseconds.
16364 	 */
16365 	uint32_t cts, us_cts, ms_cts;
16366 	uint32_t tiwin;
16367 	struct timespec ts;
16368 	struct tcpopt to;
16369 	struct tcp_rack *rack;
16370 	struct rack_sendmap *rsm;
16371 	int32_t prev_state = 0;
16372 	int no_output = 0;
16373 	int slot_remaining = 0;
16374 #ifdef TCP_ACCOUNTING
16375 	int ack_val_set = 0xf;
16376 #endif
16377 	int nsegs;
16378 
16379 	NET_EPOCH_ASSERT();
16380 	INP_WLOCK_ASSERT(inp);
16381 
16382 	/*
16383 	 * tv passed from common code is from either M_TSTMP_LRO or
16384 	 * tcp_get_usecs() if no LRO m_pkthdr timestamp is present.
16385 	 */
16386 	rack = (struct tcp_rack *)tp->t_fb_ptr;
16387 	if (rack->rack_deferred_inited == 0) {
16388 		/*
16389 		 * If we are the connecting socket we will
16390 		 * hit rack_init() when no sequence numbers
16391 		 * are setup. This makes it so we must defer
16392 		 * some initialization. Call that now.
16393 		 */
16394 		rack_deferred_init(tp, rack);
16395 	}
16396 	/*
16397 	 * Check to see if we need to skip any output plans. This
16398 	 * can happen in the non-LRO path where we are pacing and
16399 	 * must process the ack coming in but need to defer sending
16400 	 * anything becase a pacing timer is running.
16401 	 */
16402 	us_cts = tcp_tv_to_usectick(tv);
16403 	if (m->m_flags & M_ACKCMP) {
16404 		/*
16405 		 * All compressed ack's are ack's by definition so
16406 		 * remove any ack required flag and then do the processing.
16407 		 */
16408 		rack->rc_ack_required = 0;
16409 		return (rack_do_compressed_ack_processing(tp, so, m, nxt_pkt, tv));
16410 	}
16411 	thflags = tcp_get_flags(th);
16412 	if ((rack->rc_always_pace == 1) &&
16413 	    (rack->rc_ack_can_sendout_data == 0) &&
16414 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
16415 	    (TSTMP_LT(us_cts, rack->r_ctl.rc_last_output_to))) {
16416 		/*
16417 		 * Ok conditions are right for queuing the packets
16418 		 * but we do have to check the flags in the inp, it
16419 		 * could be, if a sack is present, we want to be awoken and
16420 		 * so should process the packets.
16421 		 */
16422 		slot_remaining = rack->r_ctl.rc_last_output_to - us_cts;
16423 		if (rack->rc_tp->t_flags2 & TF2_DONT_SACK_QUEUE) {
16424 			no_output = 1;
16425 		} else {
16426 			/*
16427 			 * If there is no options, or just a
16428 			 * timestamp option, we will want to queue
16429 			 * the packets. This is the same that LRO does
16430 			 * and will need to change with accurate ECN.
16431 			 */
16432 			uint32_t *ts_ptr;
16433 			int optlen;
16434 
16435 			optlen = (th->th_off << 2) - sizeof(struct tcphdr);
16436 			ts_ptr = (uint32_t *)(th + 1);
16437 			if ((optlen == 0) ||
16438 			    ((optlen == TCPOLEN_TSTAMP_APPA) &&
16439 			     (*ts_ptr == TCP_LRO_TS_OPTION)))
16440 				no_output = 1;
16441 		}
16442 		if ((no_output == 1) && (slot_remaining < tcp_min_hptsi_time)) {
16443 			/*
16444 			 * It is unrealistic to think we can pace in less than
16445 			 * the minimum granularity of the pacer (def:250usec). So
16446 			 * if we have less than that time remaining we should go
16447 			 * ahead and allow output to be "early". We will attempt to
16448 			 * make up for it in any pacing time we try to apply on
16449 			 * the outbound packet.
16450 			 */
16451 			no_output = 0;
16452 		}
16453 	}
16454 	/*
16455 	 * If there is a RST or FIN lets dump out the bw
16456 	 * with a FIN the connection may go on but we
16457 	 * may not.
16458 	 */
16459 	if ((thflags & TH_FIN) || (thflags & TH_RST))
16460 		rack_log_pacing_delay_calc(rack,
16461 					   rack->r_ctl.gp_bw,
16462 					   0,
16463 					   0,
16464 					   rack_get_gp_est(rack), /* delRate */
16465 					   rack_get_lt_bw(rack), /* rttProp */
16466 					   20, __LINE__, NULL, 0);
16467 	if (m->m_flags & M_ACKCMP) {
16468 		panic("Impossible reach m has ackcmp? m:%p tp:%p", m, tp);
16469 	}
16470 	cts = tcp_tv_to_usectick(tv);
16471 	ms_cts =  tcp_tv_to_mssectick(tv);
16472 	nsegs = m->m_pkthdr.lro_nsegs;
16473 	counter_u64_add(rack_proc_non_comp_ack, 1);
16474 #ifdef TCP_ACCOUNTING
16475 	sched_pin();
16476 	if (thflags & TH_ACK)
16477 		ts_val = get_cyclecount();
16478 #endif
16479 	if ((m->m_flags & M_TSTMP) ||
16480 	    (m->m_flags & M_TSTMP_LRO)) {
16481 		mbuf_tstmp2timespec(m, &ts);
16482 		rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
16483 		rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
16484 	} else
16485 		rack->r_ctl.act_rcv_time = *tv;
16486 	kern_prefetch(rack, &prev_state);
16487 	prev_state = 0;
16488 	/*
16489 	 * Unscale the window into a 32-bit value. For the SYN_SENT state
16490 	 * the scale is zero.
16491 	 */
16492 	tiwin = th->th_win << tp->snd_scale;
16493 #ifdef TCP_ACCOUNTING
16494 	if (thflags & TH_ACK) {
16495 		/*
16496 		 * We have a tradeoff here. We can either do what we are
16497 		 * doing i.e. pinning to this CPU and then doing the accounting
16498 		 * <or> we could do a critical enter, setup the rdtsc and cpu
16499 		 * as in below, and then validate we are on the same CPU on
16500 		 * exit. I have choosen to not do the critical enter since
16501 		 * that often will gain you a context switch, and instead lock
16502 		 * us (line above this if) to the same CPU with sched_pin(). This
16503 		 * means we may be context switched out for a higher priority
16504 		 * interupt but we won't be moved to another CPU.
16505 		 *
16506 		 * If this occurs (which it won't very often since we most likely
16507 		 * are running this code in interupt context and only a higher
16508 		 * priority will bump us ... clock?) we will falsely add in
16509 		 * to the time the interupt processing time plus the ack processing
16510 		 * time. This is ok since its a rare event.
16511 		 */
16512 		ack_val_set = tcp_do_ack_accounting(tp, th, &to, tiwin,
16513 						    ctf_fixed_maxseg(tp));
16514 	}
16515 #endif
16516 	/*
16517 	 * Parse options on any incoming segment.
16518 	 */
16519 	memset(&to, 0, sizeof(to));
16520 	tcp_dooptions(&to, (u_char *)(th + 1),
16521 	    (th->th_off << 2) - sizeof(struct tcphdr),
16522 	    (thflags & TH_SYN) ? TO_SYN : 0);
16523 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
16524 	    __func__));
16525 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
16526 	    __func__));
16527 	if (tp->t_flags2 & TF2_PROC_SACK_PROHIBIT) {
16528 		/*
16529 		 * We don't look at sack's from the
16530 		 * peer because the MSS is too small which
16531 		 * can subject us to an attack.
16532 		 */
16533 		to.to_flags &= ~TOF_SACK;
16534 	}
16535 	if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
16536 	    (tp->t_flags & TF_GPUTINPROG)) {
16537 		/*
16538 		 * We have a goodput in progress
16539 		 * and we have entered a late state.
16540 		 * Do we have enough data in the sb
16541 		 * to handle the GPUT request?
16542 		 */
16543 		uint32_t bytes;
16544 
16545 		bytes = tp->gput_ack - tp->gput_seq;
16546 		if (SEQ_GT(tp->gput_seq, tp->snd_una))
16547 			bytes += tp->gput_seq - tp->snd_una;
16548 		if (bytes > sbavail(&tptosocket(tp)->so_snd)) {
16549 			/*
16550 			 * There are not enough bytes in the socket
16551 			 * buffer that have been sent to cover this
16552 			 * measurement. Cancel it.
16553 			 */
16554 			rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
16555 						   rack->r_ctl.rc_gp_srtt /*flex1*/,
16556 						   tp->gput_seq,
16557 						   0, 0, 18, __LINE__, NULL, 0);
16558 			tp->t_flags &= ~TF_GPUTINPROG;
16559 		}
16560 	}
16561 	if (tcp_bblogging_on(rack->rc_tp)) {
16562 		union tcp_log_stackspecific log;
16563 		struct timeval ltv;
16564 #ifdef TCP_REQUEST_TRK
16565 		struct tcp_sendfile_track *tcp_req;
16566 
16567 		if (SEQ_GT(th->th_ack, tp->snd_una)) {
16568 			tcp_req = tcp_req_find_req_for_seq(tp, (th->th_ack-1));
16569 		} else {
16570 			tcp_req = tcp_req_find_req_for_seq(tp, th->th_ack);
16571 		}
16572 #endif
16573 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
16574 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
16575 		if (rack->rack_no_prr == 0)
16576 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
16577 		else
16578 			log.u_bbr.flex1 = 0;
16579 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
16580 		log.u_bbr.use_lt_bw <<= 1;
16581 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
16582 		log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced;
16583 		log.u_bbr.bbr_state = rack->rc_free_cnt;
16584 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
16585 		log.u_bbr.pkts_out = rack->rc_tp->t_maxseg;
16586 		log.u_bbr.flex3 = m->m_flags;
16587 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
16588 		log.u_bbr.lost = thflags;
16589 		log.u_bbr.pacing_gain = 0x1;
16590 #ifdef TCP_ACCOUNTING
16591 		log.u_bbr.cwnd_gain = ack_val_set;
16592 #endif
16593 		log.u_bbr.flex7 = 2;
16594 		if (m->m_flags & M_TSTMP) {
16595 			/* Record the hardware timestamp if present */
16596 			mbuf_tstmp2timespec(m, &ts);
16597 			ltv.tv_sec = ts.tv_sec;
16598 			ltv.tv_usec = ts.tv_nsec / 1000;
16599 			log.u_bbr.lt_epoch = tcp_tv_to_usectick(&ltv);
16600 		} else if (m->m_flags & M_TSTMP_LRO) {
16601 			/* Record the LRO the arrival timestamp */
16602 			mbuf_tstmp2timespec(m, &ts);
16603 			ltv.tv_sec = ts.tv_sec;
16604 			ltv.tv_usec = ts.tv_nsec / 1000;
16605 			log.u_bbr.flex5 = tcp_tv_to_usectick(&ltv);
16606 		}
16607 		log.u_bbr.timeStamp = tcp_get_usecs(&ltv);
16608 		/* Log the rcv time */
16609 		log.u_bbr.delRate = m->m_pkthdr.rcv_tstmp;
16610 #ifdef TCP_REQUEST_TRK
16611 		log.u_bbr.applimited = tp->t_tcpreq_closed;
16612 		log.u_bbr.applimited <<= 8;
16613 		log.u_bbr.applimited |= tp->t_tcpreq_open;
16614 		log.u_bbr.applimited <<= 8;
16615 		log.u_bbr.applimited |= tp->t_tcpreq_req;
16616 		if (tcp_req) {
16617 			/* Copy out any client req info */
16618 			/* seconds */
16619 			log.u_bbr.pkt_epoch = (tcp_req->localtime / HPTS_USEC_IN_SEC);
16620 			/* useconds */
16621 			log.u_bbr.delivered = (tcp_req->localtime % HPTS_USEC_IN_SEC);
16622 			log.u_bbr.rttProp = tcp_req->timestamp;
16623 			log.u_bbr.cur_del_rate = tcp_req->start;
16624 			if (tcp_req->flags & TCP_TRK_TRACK_FLG_OPEN) {
16625 				log.u_bbr.flex8 |= 1;
16626 			} else {
16627 				log.u_bbr.flex8 |= 2;
16628 				log.u_bbr.bw_inuse = tcp_req->end;
16629 			}
16630 			log.u_bbr.flex6 = tcp_req->start_seq;
16631 			if (tcp_req->flags & TCP_TRK_TRACK_FLG_COMP) {
16632 				log.u_bbr.flex8 |= 4;
16633 				log.u_bbr.epoch = tcp_req->end_seq;
16634 			}
16635 		}
16636 #endif
16637 		TCP_LOG_EVENTP(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
16638 		    tlen, &log, true, &ltv);
16639 	}
16640 	/* Remove ack required flag if set, we have one  */
16641 	if (thflags & TH_ACK)
16642 		rack->rc_ack_required = 0;
16643 	rack_log_type_bbrsnd(rack, 0, 0, cts, tv, __LINE__);
16644 	if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
16645 		way_out = 4;
16646 		retval = 0;
16647 		m_freem(m);
16648 		goto done_with_input;
16649 	}
16650 	/*
16651 	 * If a segment with the ACK-bit set arrives in the SYN-SENT state
16652 	 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9.
16653 	 */
16654 	if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
16655 	    (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
16656 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
16657 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
16658 #ifdef TCP_ACCOUNTING
16659 		sched_unpin();
16660 #endif
16661 		return (1);
16662 	}
16663 	/*
16664 	 * If timestamps were negotiated during SYN/ACK and a
16665 	 * segment without a timestamp is received, silently drop
16666 	 * the segment, unless it is a RST segment or missing timestamps are
16667 	 * tolerated.
16668 	 * See section 3.2 of RFC 7323.
16669 	 */
16670 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) &&
16671 	    ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) {
16672 		way_out = 5;
16673 		retval = 0;
16674 		m_freem(m);
16675 		goto done_with_input;
16676 	}
16677 	/*
16678 	 * Segment received on connection. Reset idle time and keep-alive
16679 	 * timer. XXX: This should be done after segment validation to
16680 	 * ignore broken/spoofed segs.
16681 	 */
16682 	if  (tp->t_idle_reduce &&
16683 	     (tp->snd_max == tp->snd_una) &&
16684 	     (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur)) {
16685 		counter_u64_add(rack_input_idle_reduces, 1);
16686 		rack_cc_after_idle(rack, tp);
16687 	}
16688 	tp->t_rcvtime = ticks;
16689 #ifdef STATS
16690 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
16691 #endif
16692 	if (tiwin > rack->r_ctl.rc_high_rwnd)
16693 		rack->r_ctl.rc_high_rwnd = tiwin;
16694 	/*
16695 	 * TCP ECN processing. XXXJTL: If we ever use ECN, we need to move
16696 	 * this to occur after we've validated the segment.
16697 	 */
16698 	if (tcp_ecn_input_segment(tp, thflags, tlen,
16699 	    tcp_packets_this_ack(tp, th->th_ack),
16700 	    iptos))
16701 		rack_cong_signal(tp, CC_ECN, th->th_ack, __LINE__);
16702 
16703 	/*
16704 	 * If echoed timestamp is later than the current time, fall back to
16705 	 * non RFC1323 RTT calculation.  Normalize timestamp if syncookies
16706 	 * were used when this connection was established.
16707 	 */
16708 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
16709 		to.to_tsecr -= tp->ts_offset;
16710 		if (TSTMP_GT(to.to_tsecr, ms_cts))
16711 			to.to_tsecr = 0;
16712 	}
16713 	if ((rack->r_rcvpath_rtt_up == 1) &&
16714 	    (to.to_flags & TOF_TS) &&
16715 	    (TSTMP_GEQ(to.to_tsecr, rack->r_ctl.last_rcv_tstmp_for_rtt))) {
16716 		uint32_t rtt = 0;
16717 
16718 		/*
16719 		 * We are receiving only and thus not sending
16720 		 * data to do an RTT. We set a flag when we first
16721 		 * sent this TS to the peer. We now have it back
16722 		 * and have an RTT to share. We log it as a conf
16723 		 * 4, we are not so sure about it.. since we
16724 		 * may have lost an ack.
16725 		 */
16726 		if (TSTMP_GT(cts, rack->r_ctl.last_time_of_arm_rcv))
16727 		    rtt = (cts - rack->r_ctl.last_time_of_arm_rcv);
16728 		rack->r_rcvpath_rtt_up = 0;
16729 		/* Submit and commit the timer */
16730 		if (rtt > 0) {
16731 			tcp_rack_xmit_timer(rack, rtt, 0, rtt, 4, NULL, 1);
16732 			tcp_rack_xmit_timer_commit(rack, tp);
16733 		}
16734 	}
16735 	/*
16736 	 * If its the first time in we need to take care of options and
16737 	 * verify we can do SACK for rack!
16738 	 */
16739 	if (rack->r_state == 0) {
16740 		/* Should be init'd by rack_init() */
16741 		KASSERT(rack->rc_inp != NULL,
16742 		    ("%s: rack->rc_inp unexpectedly NULL", __func__));
16743 		if (rack->rc_inp == NULL) {
16744 			rack->rc_inp = inp;
16745 		}
16746 
16747 		/*
16748 		 * Process options only when we get SYN/ACK back. The SYN
16749 		 * case for incoming connections is handled in tcp_syncache.
16750 		 * According to RFC1323 the window field in a SYN (i.e., a
16751 		 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX
16752 		 * this is traditional behavior, may need to be cleaned up.
16753 		 */
16754 		if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
16755 			/* Handle parallel SYN for ECN */
16756 			tcp_ecn_input_parallel_syn(tp, thflags, iptos);
16757 			if ((to.to_flags & TOF_SCALE) &&
16758 			    (tp->t_flags & TF_REQ_SCALE)) {
16759 				tp->t_flags |= TF_RCVD_SCALE;
16760 				tp->snd_scale = to.to_wscale;
16761 			} else
16762 				tp->t_flags &= ~TF_REQ_SCALE;
16763 			/*
16764 			 * Initial send window.  It will be updated with the
16765 			 * next incoming segment to the scaled value.
16766 			 */
16767 			tp->snd_wnd = th->th_win;
16768 			rack_validate_fo_sendwin_up(tp, rack);
16769 			if ((to.to_flags & TOF_TS) &&
16770 			    (tp->t_flags & TF_REQ_TSTMP)) {
16771 				tp->t_flags |= TF_RCVD_TSTMP;
16772 				tp->ts_recent = to.to_tsval;
16773 				tp->ts_recent_age = cts;
16774 			} else
16775 				tp->t_flags &= ~TF_REQ_TSTMP;
16776 			if (to.to_flags & TOF_MSS) {
16777 				tcp_mss(tp, to.to_mss);
16778 			}
16779 			if ((tp->t_flags & TF_SACK_PERMIT) &&
16780 			    (to.to_flags & TOF_SACKPERM) == 0)
16781 				tp->t_flags &= ~TF_SACK_PERMIT;
16782 			if (tp->t_flags & TF_FASTOPEN) {
16783 				if (to.to_flags & TOF_FASTOPEN) {
16784 					uint16_t mss;
16785 
16786 					if (to.to_flags & TOF_MSS)
16787 						mss = to.to_mss;
16788 					else
16789 						if ((inp->inp_vflag & INP_IPV6) != 0)
16790 							mss = TCP6_MSS;
16791 						else
16792 							mss = TCP_MSS;
16793 					tcp_fastopen_update_cache(tp, mss,
16794 					    to.to_tfo_len, to.to_tfo_cookie);
16795 				} else
16796 					tcp_fastopen_disable_path(tp);
16797 			}
16798 		}
16799 		/*
16800 		 * At this point we are at the initial call. Here we decide
16801 		 * if we are doing RACK or not. We do this by seeing if
16802 		 * TF_SACK_PERMIT is set and the sack-not-required is clear.
16803 		 * The code now does do dup-ack counting so if you don't
16804 		 * switch back you won't get rack & TLP, but you will still
16805 		 * get this stack.
16806 		 */
16807 
16808 		if ((rack_sack_not_required == 0) &&
16809 		    ((tp->t_flags & TF_SACK_PERMIT) == 0)) {
16810 			tcp_switch_back_to_default(tp);
16811 			(*tp->t_fb->tfb_tcp_do_segment)(tp, m, th, drop_hdrlen,
16812 			    tlen, iptos);
16813 #ifdef TCP_ACCOUNTING
16814 			sched_unpin();
16815 #endif
16816 			return (1);
16817 		}
16818 		tcp_set_hpts(tp);
16819 		sack_filter_clear(&rack->r_ctl.rack_sf, th->th_ack);
16820 	}
16821 	if (thflags & TH_FIN)
16822 		tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
16823 	us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
16824 	if ((rack->rc_gp_dyn_mul) &&
16825 	    (rack->use_fixed_rate == 0) &&
16826 	    (rack->rc_always_pace)) {
16827 		/* Check in on probertt */
16828 		rack_check_probe_rtt(rack, cts);
16829 	}
16830 	rack_clear_rate_sample(rack);
16831 	if ((rack->forced_ack) &&
16832 	    ((tcp_get_flags(th) & TH_RST) == 0)) {
16833 		rack_handle_probe_response(rack, tiwin, us_cts);
16834 	}
16835 	/*
16836 	 * This is the one exception case where we set the rack state
16837 	 * always. All other times (timers etc) we must have a rack-state
16838 	 * set (so we assure we have done the checks above for SACK).
16839 	 */
16840 	rack->r_ctl.rc_rcvtime = cts;
16841 	if (rack->r_state != tp->t_state)
16842 		rack_set_state(tp, rack);
16843 	if (SEQ_GT(th->th_ack, tp->snd_una) &&
16844 	    (rsm = tqhash_min(rack->r_ctl.tqh)) != NULL)
16845 		kern_prefetch(rsm, &prev_state);
16846 	prev_state = rack->r_state;
16847 	if ((thflags & TH_RST) &&
16848 	    ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
16849 	      SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
16850 	     (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq))) {
16851 		/* The connection will be killed by a reset check the tracepoint */
16852 		tcp_trace_point(rack->rc_tp, TCP_TP_RESET_RCV);
16853 	}
16854 	retval = (*rack->r_substate) (m, th, so,
16855 	    tp, &to, drop_hdrlen,
16856 	    tlen, tiwin, thflags, nxt_pkt, iptos);
16857 	if (retval == 0) {
16858 		/*
16859 		 * If retval is 1 the tcb is unlocked and most likely the tp
16860 		 * is gone.
16861 		 */
16862 		INP_WLOCK_ASSERT(inp);
16863 		if ((rack->rc_gp_dyn_mul) &&
16864 		    (rack->rc_always_pace) &&
16865 		    (rack->use_fixed_rate == 0) &&
16866 		    rack->in_probe_rtt &&
16867 		    (rack->r_ctl.rc_time_probertt_starts == 0)) {
16868 			/*
16869 			 * If we are going for target, lets recheck before
16870 			 * we output.
16871 			 */
16872 			rack_check_probe_rtt(rack, cts);
16873 		}
16874 		if (rack->set_pacing_done_a_iw == 0) {
16875 			/* How much has been acked? */
16876 			if ((tp->snd_una - tp->iss) > (ctf_fixed_maxseg(tp) * 10)) {
16877 				/* We have enough to set in the pacing segment size */
16878 				rack->set_pacing_done_a_iw = 1;
16879 				rack_set_pace_segments(tp, rack, __LINE__, NULL);
16880 			}
16881 		}
16882 		tcp_rack_xmit_timer_commit(rack, tp);
16883 #ifdef TCP_ACCOUNTING
16884 		/*
16885 		 * If we set the ack_val_se to what ack processing we are doing
16886 		 * we also want to track how many cycles we burned. Note
16887 		 * the bits after tcp_output we let be "free". This is because
16888 		 * we are also tracking the tcp_output times as well. Note the
16889 		 * use of 0xf here since we only have 11 counter (0 - 0xa) and
16890 		 * 0xf cannot be returned and is what we initialize it too to
16891 		 * indicate we are not doing the tabulations.
16892 		 */
16893 		if (ack_val_set != 0xf) {
16894 			uint64_t crtsc;
16895 
16896 			crtsc = get_cyclecount();
16897 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16898 				tp->tcp_proc_time[ack_val_set] += (crtsc - ts_val);
16899 			}
16900 		}
16901 #endif
16902 		if ((nxt_pkt == 0) && (no_output == 0)) {
16903 			if ((rack->r_wanted_output != 0) ||
16904 			    (tp->t_flags & TF_ACKNOW) ||
16905 			    (rack->r_fast_output != 0)) {
16906 
16907 do_output_now:
16908 				if (tcp_output(tp) < 0) {
16909 #ifdef TCP_ACCOUNTING
16910 					sched_unpin();
16911 #endif
16912 					return (1);
16913 				}
16914 				did_out = 1;
16915 			}
16916 			rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
16917 			rack_free_trim(rack);
16918 		} else if ((nxt_pkt == 0) && (tp->t_flags & TF_ACKNOW)) {
16919 			goto do_output_now;
16920 		} else if ((no_output == 1) &&
16921 			   (nxt_pkt == 0)  &&
16922 			   (tcp_in_hpts(rack->rc_tp) == 0)) {
16923 			/*
16924 			 * We are not in hpts and we had a pacing timer up. Use
16925 			 * the remaining time (slot_remaining) to restart the timer.
16926 			 */
16927 			KASSERT ((slot_remaining != 0), ("slot remaining is zero for rack:%p tp:%p", rack, tp));
16928 			rack_start_hpts_timer(rack, tp, cts, slot_remaining, 0, 0);
16929 			rack_free_trim(rack);
16930 		}
16931 		/* Clear the flag, it may have been cleared by output but we may not have  */
16932 		if ((nxt_pkt == 0) && (tp->t_flags2 & TF2_HPTS_CALLS))
16933 			tp->t_flags2 &= ~TF2_HPTS_CALLS;
16934 		/*
16935 		 * The draft (v3) calls for us to use SEQ_GEQ, but that
16936 		 * causes issues when we are just going app limited. Lets
16937 		 * instead use SEQ_GT <or> where its equal but more data
16938 		 * is outstanding.
16939 		 *
16940 		 * Also make sure we are on the last ack of a series. We
16941 		 * have to have all the ack's processed in queue to know
16942 		 * if there is something left outstanding.
16943 		 */
16944 		if (SEQ_GEQ(tp->snd_una, rack->r_ctl.roundends) &&
16945 		    (rack->rc_new_rnd_needed == 0) &&
16946 		    (nxt_pkt == 0)) {
16947 			/*
16948 			 * We have crossed into a new round with
16949 			 * the new snd_unae.
16950 			 */
16951 			rack_new_round_setup(tp, rack, tp->snd_una);
16952 		}
16953 		if ((nxt_pkt == 0) &&
16954 		    ((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) &&
16955 		    (SEQ_GT(tp->snd_max, tp->snd_una) ||
16956 		     (tp->t_flags & TF_DELACK) ||
16957 		     ((V_tcp_always_keepalive || rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
16958 		      (tp->t_state <= TCPS_CLOSING)))) {
16959 			/* We could not send (probably in the hpts but stopped the timer earlier)? */
16960 			if ((tp->snd_max == tp->snd_una) &&
16961 			    ((tp->t_flags & TF_DELACK) == 0) &&
16962 			    (tcp_in_hpts(rack->rc_tp)) &&
16963 			    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
16964 				/* keep alive not needed if we are hptsi output yet */
16965 				;
16966 			} else {
16967 				int late = 0;
16968 				if (tcp_in_hpts(tp)) {
16969 					if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
16970 						us_cts = tcp_get_usecs(NULL);
16971 						if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
16972 							rack->r_early = 1;
16973 							rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
16974 						} else
16975 							late = 1;
16976 						rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
16977 					}
16978 					tcp_hpts_remove(tp);
16979 				}
16980 				if (late && (did_out == 0)) {
16981 					/*
16982 					 * We are late in the sending
16983 					 * and we did not call the output
16984 					 * (this probably should not happen).
16985 					 */
16986 					goto do_output_now;
16987 				}
16988 				rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
16989 			}
16990 			way_out = 1;
16991 		} else if (nxt_pkt == 0) {
16992 			/* Do we have the correct timer running? */
16993 			rack_timer_audit(tp, rack, &so->so_snd);
16994 			way_out = 2;
16995 		}
16996 	done_with_input:
16997 		rack_log_doseg_done(rack, cts, nxt_pkt, did_out, way_out, max(1, nsegs));
16998 		if (did_out)
16999 			rack->r_wanted_output = 0;
17000 	}
17001 
17002 #ifdef TCP_ACCOUNTING
17003 	sched_unpin();
17004 #endif
17005 	return (retval);
17006 }
17007 
17008 static void
17009 rack_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
17010     int32_t drop_hdrlen, int32_t tlen, uint8_t iptos)
17011 {
17012 	struct timeval tv;
17013 
17014 	/* First lets see if we have old packets */
17015 	if (!STAILQ_EMPTY(&tp->t_inqueue)) {
17016 		if (ctf_do_queued_segments(tp, 1)) {
17017 			m_freem(m);
17018 			return;
17019 		}
17020 	}
17021 	if (m->m_flags & M_TSTMP_LRO) {
17022 		mbuf_tstmp2timeval(m, &tv);
17023 	} else {
17024 		/* Should not be should we kassert instead? */
17025 		tcp_get_usecs(&tv);
17026 	}
17027 	if (rack_do_segment_nounlock(tp, m, th, drop_hdrlen, tlen, iptos, 0,
17028 	    &tv) == 0) {
17029 		INP_WUNLOCK(tptoinpcb(tp));
17030 	}
17031 }
17032 
17033 struct rack_sendmap *
17034 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tsused)
17035 {
17036 	struct rack_sendmap *rsm = NULL;
17037 	int32_t idx;
17038 	uint32_t srtt = 0, thresh = 0, ts_low = 0;
17039 
17040 	/* Return the next guy to be re-transmitted */
17041 	if (tqhash_empty(rack->r_ctl.tqh)) {
17042 		return (NULL);
17043 	}
17044 	if (tp->t_flags & TF_SENTFIN) {
17045 		/* retran the end FIN? */
17046 		return (NULL);
17047 	}
17048 	/* ok lets look at this one */
17049 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
17050 	if (rack->r_must_retran && rsm && (rsm->r_flags & RACK_MUST_RXT)) {
17051 		return (rsm);
17052 	}
17053 	if (rsm && ((rsm->r_flags & RACK_ACKED) == 0)) {
17054 		goto check_it;
17055 	}
17056 	rsm = rack_find_lowest_rsm(rack);
17057 	if (rsm == NULL) {
17058 		return (NULL);
17059 	}
17060 check_it:
17061 	if (((rack->rc_tp->t_flags & TF_SACK_PERMIT) == 0) &&
17062 	    (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
17063 		/*
17064 		 * No sack so we automatically do the 3 strikes and
17065 		 * retransmit (no rack timer would be started).
17066 		 */
17067 		return (rsm);
17068 	}
17069 	if (rsm->r_flags & RACK_ACKED) {
17070 		return (NULL);
17071 	}
17072 	if (((rsm->r_flags & RACK_SACK_PASSED) == 0) &&
17073 	    (rsm->r_dupack < DUP_ACK_THRESHOLD)) {
17074 		/* Its not yet ready */
17075 		return (NULL);
17076 	}
17077 	srtt = rack_grab_rtt(tp, rack);
17078 	idx = rsm->r_rtr_cnt - 1;
17079 	ts_low = (uint32_t)rsm->r_tim_lastsent[idx];
17080 	thresh = rack_calc_thresh_rack(rack, srtt, tsused, __LINE__, 1);
17081 	if ((tsused == ts_low) ||
17082 	    (TSTMP_LT(tsused, ts_low))) {
17083 		/* No time since sending */
17084 		return (NULL);
17085 	}
17086 	if ((tsused - ts_low) < thresh) {
17087 		/* It has not been long enough yet */
17088 		return (NULL);
17089 	}
17090 	if ((rsm->r_dupack >= DUP_ACK_THRESHOLD) ||
17091 	    ((rsm->r_flags & RACK_SACK_PASSED))) {
17092 		/*
17093 		 * We have passed the dup-ack threshold <or>
17094 		 * a SACK has indicated this is missing.
17095 		 * Note that if you are a declared attacker
17096 		 * it is only the dup-ack threshold that
17097 		 * will cause retransmits.
17098 		 */
17099 		/* log retransmit reason */
17100 		rack_log_retran_reason(rack, rsm, (tsused - ts_low), thresh, 1);
17101 		rack->r_fast_output = 0;
17102 		return (rsm);
17103 	}
17104 	return (NULL);
17105 }
17106 
17107 static void
17108 rack_log_pacing_delay_calc (struct tcp_rack *rack, uint32_t len, uint32_t slot,
17109 			   uint64_t bw_est, uint64_t bw, uint64_t len_time, int method,
17110 			   int line, struct rack_sendmap *rsm, uint8_t quality)
17111 {
17112 	if (tcp_bblogging_on(rack->rc_tp)) {
17113 		union tcp_log_stackspecific log;
17114 		struct timeval tv;
17115 
17116 		if (rack_verbose_logging == 0) {
17117 			/*
17118 			 * We are not verbose screen out all but
17119 			 * ones we always want.
17120 			 */
17121 			if ((method != 2) &&
17122 			    (method != 3) &&
17123 			    (method != 7) &&
17124 			    (method != 89) &&
17125 			    (method != 14) &&
17126 			    (method != 20)) {
17127 				return;
17128 			}
17129 		}
17130 		memset(&log, 0, sizeof(log));
17131 		log.u_bbr.flex1 = slot;
17132 		log.u_bbr.flex2 = len;
17133 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_min_segs;
17134 		log.u_bbr.flex4 = rack->r_ctl.rc_pace_max_segs;
17135 		log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ss;
17136 		log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_ca;
17137 		log.u_bbr.use_lt_bw = rack->rc_ack_can_sendout_data;
17138 		log.u_bbr.use_lt_bw <<= 1;
17139 		log.u_bbr.use_lt_bw |= rack->r_late;
17140 		log.u_bbr.use_lt_bw <<= 1;
17141 		log.u_bbr.use_lt_bw |= rack->r_early;
17142 		log.u_bbr.use_lt_bw <<= 1;
17143 		log.u_bbr.use_lt_bw |= rack->app_limited_needs_set;
17144 		log.u_bbr.use_lt_bw <<= 1;
17145 		log.u_bbr.use_lt_bw |= rack->rc_gp_filled;
17146 		log.u_bbr.use_lt_bw <<= 1;
17147 		log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt;
17148 		log.u_bbr.use_lt_bw <<= 1;
17149 		log.u_bbr.use_lt_bw |= rack->in_probe_rtt;
17150 		log.u_bbr.use_lt_bw <<= 1;
17151 		log.u_bbr.use_lt_bw |= rack->gp_ready;
17152 		log.u_bbr.pkt_epoch = line;
17153 		log.u_bbr.epoch = rack->r_ctl.rc_agg_delayed;
17154 		log.u_bbr.lt_epoch = rack->r_ctl.rc_agg_early;
17155 		log.u_bbr.applimited = rack->r_ctl.rack_per_of_gp_rec;
17156 		log.u_bbr.bw_inuse = bw_est;
17157 		log.u_bbr.delRate = bw;
17158 		if (rack->r_ctl.gp_bw == 0)
17159 			log.u_bbr.cur_del_rate = 0;
17160 		else
17161 			log.u_bbr.cur_del_rate = rack_get_bw(rack);
17162 		log.u_bbr.rttProp = len_time;
17163 		log.u_bbr.pkts_out = rack->r_ctl.rc_rack_min_rtt;
17164 		log.u_bbr.lost = rack->r_ctl.rc_probertt_sndmax_atexit;
17165 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm);
17166 		if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh) {
17167 			/* We are in slow start */
17168 			log.u_bbr.flex7 = 1;
17169 		} else {
17170 			/* we are on congestion avoidance */
17171 			log.u_bbr.flex7 = 0;
17172 		}
17173 		log.u_bbr.flex8 = method;
17174 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
17175 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
17176 		log.u_bbr.cwnd_gain = rack->rc_gp_saw_rec;
17177 		log.u_bbr.cwnd_gain <<= 1;
17178 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss;
17179 		log.u_bbr.cwnd_gain <<= 1;
17180 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca;
17181 		log.u_bbr.bbr_substate = quality;
17182 		log.u_bbr.bbr_state = rack->dgp_on;
17183 		log.u_bbr.bbr_state <<= 1;
17184 		log.u_bbr.bbr_state |= rack->rc_pace_to_cwnd;
17185 		log.u_bbr.bbr_state <<= 2;
17186 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
17187 		    &rack->rc_inp->inp_socket->so_rcv,
17188 		    &rack->rc_inp->inp_socket->so_snd,
17189 		    BBR_LOG_HPTSI_CALC, 0,
17190 		    0, &log, false, &tv);
17191 	}
17192 }
17193 
17194 static uint32_t
17195 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss)
17196 {
17197 	uint32_t new_tso, user_max, pace_one;
17198 
17199 	user_max = rack->rc_user_set_max_segs * mss;
17200 	if (rack->rc_force_max_seg) {
17201 		return (user_max);
17202 	}
17203 	if (rack->use_fixed_rate &&
17204 	    ((rack->r_ctl.crte == NULL) ||
17205 	     (bw != rack->r_ctl.crte->rate))) {
17206 		/* Use the user mss since we are not exactly matched */
17207 		return (user_max);
17208 	}
17209 	if (rack_pace_one_seg ||
17210 	    (rack->r_ctl.rc_user_set_min_segs == 1))
17211 		pace_one = 1;
17212 	else
17213 		pace_one = 0;
17214 
17215 	new_tso = tcp_get_pacing_burst_size_w_divisor(rack->rc_tp, bw, mss,
17216 		     pace_one, rack->r_ctl.crte, NULL, rack->r_ctl.pace_len_divisor);
17217 	if (new_tso > user_max)
17218 		new_tso = user_max;
17219 	if (rack->rc_hybrid_mode && rack->r_ctl.client_suggested_maxseg) {
17220 		if (((uint32_t)rack->r_ctl.client_suggested_maxseg * mss) > new_tso)
17221 			new_tso = (uint32_t)rack->r_ctl.client_suggested_maxseg * mss;
17222 	}
17223 	if (rack->r_ctl.rc_user_set_min_segs &&
17224 	    ((rack->r_ctl.rc_user_set_min_segs * mss) > new_tso))
17225 	    new_tso = rack->r_ctl.rc_user_set_min_segs * mss;
17226 	return (new_tso);
17227 }
17228 
17229 static uint64_t
17230 rack_arrive_at_discounted_rate(struct tcp_rack *rack, uint64_t window_input, uint32_t *rate_set, uint32_t *gain_b)
17231 {
17232 	uint64_t reduced_win;
17233 	uint32_t gain;
17234 
17235 	if (window_input < rc_init_window(rack)) {
17236 		/*
17237 		 * The cwnd is collapsed to
17238 		 * nearly zero, maybe because of a time-out?
17239 		 * Lets drop back to the lt-bw.
17240 		 */
17241 		reduced_win = rack_get_lt_bw(rack);
17242 		/* Set the flag so the caller knows its a rate and not a reduced window */
17243 		*rate_set = 1;
17244 		gain = 100;
17245 	} else if  (IN_RECOVERY(rack->rc_tp->t_flags)) {
17246 		/*
17247 		 * If we are in recover our cwnd needs to be less for
17248 		 * our pacing consideration.
17249 		 */
17250 		if (rack->rack_hibeta == 0) {
17251 			reduced_win = window_input / 2;
17252 			gain = 50;
17253 		} else {
17254 			reduced_win = window_input * rack->r_ctl.saved_hibeta;
17255 			reduced_win /= 100;
17256 			gain = rack->r_ctl.saved_hibeta;
17257 		}
17258 	} else {
17259 		/*
17260 		 * Apply Timely factor to increase/decrease the
17261 		 * amount we are pacing at.
17262 		 */
17263 		gain = rack_get_output_gain(rack, NULL);
17264 		if (gain > rack_gain_p5_ub) {
17265 			gain = rack_gain_p5_ub;
17266 		}
17267 		reduced_win = window_input * gain;
17268 		reduced_win /= 100;
17269 	}
17270 	if (gain_b != NULL)
17271 		*gain_b = gain;
17272 	/*
17273 	 * What is being returned here is a trimmed down
17274 	 * window values in all cases where rate_set is left
17275 	 * at 0. In one case we actually return the rate (lt_bw).
17276 	 * the "reduced_win" is returned as a slimmed down cwnd that
17277 	 * is then calculated by the caller into a rate when rate_set
17278 	 * is 0.
17279 	 */
17280 	return (reduced_win);
17281 }
17282 
17283 static int32_t
17284 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)
17285 {
17286 	uint64_t lentim, fill_bw;
17287 
17288 	rack->r_via_fill_cw = 0;
17289 	if (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.cwnd_to_use)
17290 		return (slot);
17291 	if ((ctf_outstanding(rack->rc_tp) + (segsiz-1)) > rack->rc_tp->snd_wnd)
17292 		return (slot);
17293 	if (rack->r_ctl.rc_last_us_rtt == 0)
17294 		return (slot);
17295 	if (rack->rc_pace_fill_if_rttin_range &&
17296 	    (rack->r_ctl.rc_last_us_rtt >=
17297 	     (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack->rtt_limit_mul))) {
17298 		/* The rtt is huge, N * smallest, lets not fill */
17299 		return (slot);
17300 	}
17301 	if (rack->r_ctl.fillcw_cap && *rate_wanted >= rack->r_ctl.fillcw_cap)
17302 		return (slot);
17303 	/*
17304 	 * first lets calculate the b/w based on the last us-rtt
17305 	 * and the the smallest send window.
17306 	 */
17307 	fill_bw = min(rack->rc_tp->snd_cwnd, rack->r_ctl.cwnd_to_use);
17308 	if (rack->rc_fillcw_apply_discount) {
17309 		uint32_t rate_set = 0;
17310 
17311 		fill_bw = rack_arrive_at_discounted_rate(rack, fill_bw, &rate_set, NULL);
17312 		if (rate_set) {
17313 			goto at_lt_bw;
17314 		}
17315 	}
17316 	/* Take the rwnd if its smaller */
17317 	if (fill_bw > rack->rc_tp->snd_wnd)
17318 		fill_bw = rack->rc_tp->snd_wnd;
17319 	/* Now lets make it into a b/w */
17320 	fill_bw *= (uint64_t)HPTS_USEC_IN_SEC;
17321 	fill_bw /= (uint64_t)rack->r_ctl.rc_last_us_rtt;
17322 	/* Adjust to any cap */
17323 	if (rack->r_ctl.fillcw_cap && fill_bw >= rack->r_ctl.fillcw_cap)
17324 		fill_bw = rack->r_ctl.fillcw_cap;
17325 
17326 at_lt_bw:
17327 	if (rack_bw_multipler > 0) {
17328 		/*
17329 		 * We want to limit fill-cw to the some multiplier
17330 		 * of the max(lt_bw, gp_est). The normal default
17331 		 * is 0 for off, so a sysctl has enabled it.
17332 		 */
17333 		uint64_t lt_bw, gp, rate;
17334 
17335 		gp = rack_get_gp_est(rack);
17336 		lt_bw = rack_get_lt_bw(rack);
17337 		if (lt_bw > gp)
17338 			rate = lt_bw;
17339 		else
17340 			rate = gp;
17341 		rate *= rack_bw_multipler;
17342 		rate /= 100;
17343 		if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
17344 			union tcp_log_stackspecific log;
17345 			struct timeval tv;
17346 
17347 			memset(&log.u_bbr, 0, sizeof(log.u_bbr));
17348 			log.u_bbr.timeStamp = tcp_get_usecs(&tv);
17349 			log.u_bbr.flex1 = rack_bw_multipler;
17350 			log.u_bbr.flex2 = len;
17351 			log.u_bbr.cur_del_rate = gp;
17352 			log.u_bbr.delRate = lt_bw;
17353 			log.u_bbr.bw_inuse = rate;
17354 			log.u_bbr.rttProp = fill_bw;
17355 			log.u_bbr.flex8 = 44;
17356 			tcp_log_event(rack->rc_tp, NULL, NULL, NULL,
17357 				      BBR_LOG_CWND, 0,
17358 				      0, &log, false, NULL,
17359 				      __func__, __LINE__, &tv);
17360 		}
17361 		if (fill_bw > rate)
17362 			fill_bw = rate;
17363 	}
17364 	/* We are below the min b/w */
17365 	if (non_paced)
17366 		*rate_wanted = fill_bw;
17367 	if ((fill_bw < RACK_MIN_BW) || (fill_bw < *rate_wanted))
17368 		return (slot);
17369 	rack->r_via_fill_cw = 1;
17370 	if (rack->r_rack_hw_rate_caps &&
17371 	    (rack->r_ctl.crte != NULL)) {
17372 		uint64_t high_rate;
17373 
17374 		high_rate = tcp_hw_highest_rate(rack->r_ctl.crte);
17375 		if (fill_bw > high_rate) {
17376 			/* We are capping bw at the highest rate table entry */
17377 			if (*rate_wanted > high_rate) {
17378 				/* The original rate was also capped */
17379 				rack->r_via_fill_cw = 0;
17380 			}
17381 			rack_log_hdwr_pacing(rack,
17382 					     fill_bw, high_rate, __LINE__,
17383 					     0, 3);
17384 			fill_bw = high_rate;
17385 			if (capped)
17386 				*capped = 1;
17387 		}
17388 	} else if ((rack->r_ctl.crte == NULL) &&
17389 		   (rack->rack_hdrw_pacing == 0) &&
17390 		   (rack->rack_hdw_pace_ena) &&
17391 		   rack->r_rack_hw_rate_caps &&
17392 		   (rack->rack_attempt_hdwr_pace == 0) &&
17393 		   (rack->rc_inp->inp_route.ro_nh != NULL) &&
17394 		   (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
17395 		/*
17396 		 * Ok we may have a first attempt that is greater than our top rate
17397 		 * lets check.
17398 		 */
17399 		uint64_t high_rate;
17400 
17401 		high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp);
17402 		if (high_rate) {
17403 			if (fill_bw > high_rate) {
17404 				fill_bw = high_rate;
17405 				if (capped)
17406 					*capped = 1;
17407 			}
17408 		}
17409 	}
17410 	if (rack->r_ctl.bw_rate_cap && (fill_bw > rack->r_ctl.bw_rate_cap)) {
17411 		rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
17412 				   fill_bw, 0, 0, HYBRID_LOG_RATE_CAP, 2, NULL, __LINE__);
17413 		fill_bw = rack->r_ctl.bw_rate_cap;
17414 	}
17415 	/*
17416 	 * Ok fill_bw holds our mythical b/w to fill the cwnd
17417 	 * in an rtt (unless it was capped), what does that
17418 	 * time wise equate too?
17419 	 */
17420 	lentim = (uint64_t)(len) * (uint64_t)HPTS_USEC_IN_SEC;
17421 	lentim /= fill_bw;
17422 	*rate_wanted = fill_bw;
17423 	if (non_paced || (lentim < slot)) {
17424 		rack_log_pacing_delay_calc(rack, len, slot, fill_bw,
17425 					   0, lentim, 12, __LINE__, NULL, 0);
17426 		return ((int32_t)lentim);
17427 	} else
17428 		return (slot);
17429 }
17430 
17431 static int32_t
17432 rack_get_pacing_delay(struct tcp_rack *rack, struct tcpcb *tp, uint32_t len, struct rack_sendmap *rsm, uint32_t segsiz, int line)
17433 {
17434 	uint64_t srtt;
17435 	int32_t slot = 0;
17436 	int can_start_hw_pacing = 1;
17437 	int err;
17438 	int pace_one;
17439 
17440 	if (rack_pace_one_seg ||
17441 	    (rack->r_ctl.rc_user_set_min_segs == 1))
17442 		pace_one = 1;
17443 	else
17444 		pace_one = 0;
17445 	if (rack->rc_always_pace == 0) {
17446 		/*
17447 		 * We use the most optimistic possible cwnd/srtt for
17448 		 * sending calculations. This will make our
17449 		 * calculation anticipate getting more through
17450 		 * quicker then possible. But thats ok we don't want
17451 		 * the peer to have a gap in data sending.
17452 		 */
17453 		uint64_t cwnd, tr_perms = 0;
17454 		int32_t reduce;
17455 
17456 	old_method:
17457 		/*
17458 		 * We keep no precise pacing with the old method
17459 		 * instead we use the pacer to mitigate bursts.
17460 		 */
17461 		if (rack->r_ctl.rc_rack_min_rtt)
17462 			srtt = rack->r_ctl.rc_rack_min_rtt;
17463 		else
17464 			srtt = max(tp->t_srtt, 1);
17465 		if (rack->r_ctl.rc_rack_largest_cwnd)
17466 			cwnd = rack->r_ctl.rc_rack_largest_cwnd;
17467 		else
17468 			cwnd = rack->r_ctl.cwnd_to_use;
17469 		/* Inflate cwnd by 1000 so srtt of usecs is in ms */
17470 		tr_perms = (cwnd * 1000) / srtt;
17471 		if (tr_perms == 0) {
17472 			tr_perms = ctf_fixed_maxseg(tp);
17473 		}
17474 		/*
17475 		 * Calculate how long this will take to drain, if
17476 		 * the calculation comes out to zero, thats ok we
17477 		 * will use send_a_lot to possibly spin around for
17478 		 * more increasing tot_len_this_send to the point
17479 		 * that its going to require a pace, or we hit the
17480 		 * cwnd. Which in that case we are just waiting for
17481 		 * a ACK.
17482 		 */
17483 		slot = len / tr_perms;
17484 		/* Now do we reduce the time so we don't run dry? */
17485 		if (slot && rack_slot_reduction) {
17486 			reduce = (slot / rack_slot_reduction);
17487 			if (reduce < slot) {
17488 				slot -= reduce;
17489 			} else
17490 				slot = 0;
17491 		} else
17492 			reduce = 0;
17493 		slot *= HPTS_USEC_IN_MSEC;
17494 		if (rack->rc_pace_to_cwnd) {
17495 			uint64_t rate_wanted = 0;
17496 
17497 			slot = pace_to_fill_cwnd(rack, slot, len, segsiz, NULL, &rate_wanted, 1);
17498 			rack->rc_ack_can_sendout_data = 1;
17499 			rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, 0, 0, 14, __LINE__, NULL, 0);
17500 		} else
17501 			rack_log_pacing_delay_calc(rack, len, slot, tr_perms, reduce, 0, 7, __LINE__, NULL, 0);
17502 		/*******************************************************/
17503 		/* RRS: We insert non-paced call to stats here for len */
17504 		/*******************************************************/
17505 	} else {
17506 		uint64_t bw_est, res, lentim, rate_wanted;
17507 		uint32_t segs, oh;
17508 		int capped = 0;
17509 		int prev_fill;
17510 
17511 		if ((rack->r_rr_config == 1) && rsm) {
17512 			return (rack->r_ctl.rc_min_to);
17513 		}
17514 		if (rack->use_fixed_rate) {
17515 			rate_wanted = bw_est = rack_get_fixed_pacing_bw(rack);
17516 		} else if ((rack->r_ctl.init_rate == 0) &&
17517 			   (rack->r_ctl.gp_bw == 0)) {
17518 			/* no way to yet do an estimate */
17519 			bw_est = rate_wanted = 0;
17520 		} else if (rack->dgp_on) {
17521 			bw_est = rack_get_bw(rack);
17522 			rate_wanted = rack_get_output_bw(rack, bw_est, rsm, &capped);
17523 		} else {
17524 			uint32_t gain, rate_set = 0;
17525 
17526 			rate_wanted = min(rack->rc_tp->snd_cwnd, rack->r_ctl.cwnd_to_use);
17527 			rate_wanted = rack_arrive_at_discounted_rate(rack, rate_wanted, &rate_set, &gain);
17528 			if (rate_set == 0) {
17529 				if (rate_wanted > rack->rc_tp->snd_wnd)
17530 					rate_wanted = rack->rc_tp->snd_wnd;
17531 				/* Now lets make it into a b/w */
17532 				rate_wanted *= (uint64_t)HPTS_USEC_IN_SEC;
17533 				rate_wanted /= (uint64_t)rack->r_ctl.rc_last_us_rtt;
17534 			}
17535 			bw_est = rate_wanted;
17536 			rack_log_pacing_delay_calc(rack, rack->rc_tp->snd_cwnd,
17537 						   rack->r_ctl.cwnd_to_use,
17538 						   rate_wanted, bw_est,
17539 						   rack->r_ctl.rc_last_us_rtt,
17540 						   88, __LINE__, NULL, gain);
17541 		}
17542 		if ((bw_est == 0) || (rate_wanted == 0) ||
17543 		    ((rack->gp_ready == 0) && (rack->use_fixed_rate == 0))) {
17544 			/*
17545 			 * No way yet to make a b/w estimate or
17546 			 * our raise is set incorrectly.
17547 			 */
17548 			goto old_method;
17549 		}
17550 		rack_rate_cap_bw(rack, &rate_wanted, &capped);
17551 		/* We need to account for all the overheads */
17552 		segs = (len + segsiz - 1) / segsiz;
17553 		/*
17554 		 * We need the diff between 1514 bytes (e-mtu with e-hdr)
17555 		 * and how much data we put in each packet. Yes this
17556 		 * means we may be off if we are larger than 1500 bytes
17557 		 * or smaller. But this just makes us more conservative.
17558 		 */
17559 
17560 		oh =  (tp->t_maxseg - segsiz) + sizeof(struct tcphdr);
17561 		if (rack->r_is_v6) {
17562 #ifdef INET6
17563 			oh += sizeof(struct ip6_hdr);
17564 #endif
17565 		} else {
17566 #ifdef INET
17567 			oh += sizeof(struct ip);
17568 #endif
17569 		}
17570 		/* We add a fixed 14 for the ethernet header */
17571 		oh += 14;
17572 		segs *= oh;
17573 		lentim = (uint64_t)(len + segs) * (uint64_t)HPTS_USEC_IN_SEC;
17574 		res = lentim / rate_wanted;
17575 		slot = (uint32_t)res;
17576 		if (rack_hw_rate_min &&
17577 		    (rate_wanted < rack_hw_rate_min)) {
17578 			can_start_hw_pacing = 0;
17579 			if (rack->r_ctl.crte) {
17580 				/*
17581 				 * Ok we need to release it, we
17582 				 * have fallen too low.
17583 				 */
17584 				tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17585 				rack->r_ctl.crte = NULL;
17586 				rack->rack_attempt_hdwr_pace = 0;
17587 				rack->rack_hdrw_pacing = 0;
17588 			}
17589 		}
17590 		if (rack->r_ctl.crte &&
17591 		    (tcp_hw_highest_rate(rack->r_ctl.crte) < rate_wanted)) {
17592 			/*
17593 			 * We want more than the hardware can give us,
17594 			 * don't start any hw pacing.
17595 			 */
17596 			can_start_hw_pacing = 0;
17597 			if (rack->r_rack_hw_rate_caps == 0) {
17598 				/*
17599 				 * Ok we need to release it, we
17600 				 * want more than the card can give us and
17601 				 * no rate cap is in place. Set it up so
17602 				 * when we want less we can retry.
17603 				 */
17604 				tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17605 				rack->r_ctl.crte = NULL;
17606 				rack->rack_attempt_hdwr_pace = 0;
17607 				rack->rack_hdrw_pacing = 0;
17608 			}
17609 		}
17610 		if ((rack->r_ctl.crte != NULL) && (rack->rc_inp->inp_snd_tag == NULL)) {
17611 			/*
17612 			 * We lost our rate somehow, this can happen
17613 			 * if the interface changed underneath us.
17614 			 */
17615 			tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17616 			rack->r_ctl.crte = NULL;
17617 			/* Lets re-allow attempting to setup pacing */
17618 			rack->rack_hdrw_pacing = 0;
17619 			rack->rack_attempt_hdwr_pace = 0;
17620 			rack_log_hdwr_pacing(rack,
17621 					     rate_wanted, bw_est, __LINE__,
17622 					     0, 6);
17623 		}
17624 		prev_fill = rack->r_via_fill_cw;
17625 		if ((rack->rc_pace_to_cwnd) &&
17626 		    (capped == 0) &&
17627 		    (rack->dgp_on == 1) &&
17628 		    (rack->use_fixed_rate == 0) &&
17629 		    (rack->in_probe_rtt == 0) &&
17630 		    (IN_FASTRECOVERY(rack->rc_tp->t_flags) == 0)) {
17631 			/*
17632 			 * We want to pace at our rate *or* faster to
17633 			 * fill the cwnd to the max if its not full.
17634 			 */
17635 			slot = pace_to_fill_cwnd(rack, slot, (len+segs), segsiz, &capped, &rate_wanted, 0);
17636 			/* Re-check to make sure we are not exceeding our max b/w */
17637 			if ((rack->r_ctl.crte != NULL) &&
17638 			    (tcp_hw_highest_rate(rack->r_ctl.crte) < rate_wanted)) {
17639 				/*
17640 				 * We want more than the hardware can give us,
17641 				 * don't start any hw pacing.
17642 				 */
17643 				can_start_hw_pacing = 0;
17644 				if (rack->r_rack_hw_rate_caps == 0) {
17645 					/*
17646 					 * Ok we need to release it, we
17647 					 * want more than the card can give us and
17648 					 * no rate cap is in place. Set it up so
17649 					 * when we want less we can retry.
17650 					 */
17651 					tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17652 					rack->r_ctl.crte = NULL;
17653 					rack->rack_attempt_hdwr_pace = 0;
17654 					rack->rack_hdrw_pacing = 0;
17655 					rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
17656 				}
17657 			}
17658 		}
17659 		if ((rack->rc_inp->inp_route.ro_nh != NULL) &&
17660 		    (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
17661 			if ((rack->rack_hdw_pace_ena) &&
17662 			    (can_start_hw_pacing > 0) &&
17663 			    (rack->rack_hdrw_pacing == 0) &&
17664 			    (rack->rack_attempt_hdwr_pace == 0)) {
17665 				/*
17666 				 * Lets attempt to turn on hardware pacing
17667 				 * if we can.
17668 				 */
17669 				rack->rack_attempt_hdwr_pace = 1;
17670 				rack->r_ctl.crte = tcp_set_pacing_rate(rack->rc_tp,
17671 								       rack->rc_inp->inp_route.ro_nh->nh_ifp,
17672 								       rate_wanted,
17673 								       RS_PACING_GEQ,
17674 								       &err, &rack->r_ctl.crte_prev_rate);
17675 				if (rack->r_ctl.crte) {
17676 					rack->rack_hdrw_pacing = 1;
17677 					rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size_w_divisor(tp, rate_wanted, segsiz,
17678 													   pace_one, rack->r_ctl.crte,
17679 													   NULL, rack->r_ctl.pace_len_divisor);
17680 					rack_log_hdwr_pacing(rack,
17681 							     rate_wanted, rack->r_ctl.crte->rate, __LINE__,
17682 							     err, 0);
17683 					rack->r_ctl.last_hw_bw_req = rate_wanted;
17684 				} else {
17685 					counter_u64_add(rack_hw_pace_init_fail, 1);
17686 				}
17687 			} else if (rack->rack_hdrw_pacing &&
17688 				   (rack->r_ctl.last_hw_bw_req != rate_wanted)) {
17689 				/* Do we need to adjust our rate? */
17690 				const struct tcp_hwrate_limit_table *nrte;
17691 
17692 				if (rack->r_up_only &&
17693 				    (rate_wanted < rack->r_ctl.crte->rate)) {
17694 					/**
17695 					 * We have four possible states here
17696 					 * having to do with the previous time
17697 					 * and this time.
17698 					 *   previous  |  this-time
17699 					 * A)     0      |     0   -- fill_cw not in the picture
17700 					 * B)     1      |     0   -- we were doing a fill-cw but now are not
17701 					 * C)     1      |     1   -- all rates from fill_cw
17702 					 * D)     0      |     1   -- we were doing non-fill and now we are filling
17703 					 *
17704 					 * For case A, C and D we don't allow a drop. But for
17705 					 * case B where we now our on our steady rate we do
17706 					 * allow a drop.
17707 					 *
17708 					 */
17709 					if (!((prev_fill == 1) && (rack->r_via_fill_cw == 0)))
17710 						goto done_w_hdwr;
17711 				}
17712 				if ((rate_wanted > rack->r_ctl.crte->rate) ||
17713 				    (rate_wanted <= rack->r_ctl.crte_prev_rate)) {
17714 					if (rack_hw_rate_to_low &&
17715 					    (bw_est < rack_hw_rate_to_low)) {
17716 						/*
17717 						 * The pacing rate is too low for hardware, but
17718 						 * do allow hardware pacing to be restarted.
17719 						 */
17720 						rack_log_hdwr_pacing(rack,
17721 								     bw_est, rack->r_ctl.crte->rate, __LINE__,
17722 								     0, 5);
17723 						tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17724 						rack->r_ctl.crte = NULL;
17725 						rack->rack_attempt_hdwr_pace = 0;
17726 						rack->rack_hdrw_pacing = 0;
17727 						rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
17728 						goto done_w_hdwr;
17729 					}
17730 					nrte = tcp_chg_pacing_rate(rack->r_ctl.crte,
17731 								   rack->rc_tp,
17732 								   rack->rc_inp->inp_route.ro_nh->nh_ifp,
17733 								   rate_wanted,
17734 								   RS_PACING_GEQ,
17735 								   &err, &rack->r_ctl.crte_prev_rate);
17736 					if (nrte == NULL) {
17737 						/*
17738 						 * Lost the rate, lets drop hardware pacing
17739 						 * period.
17740 						 */
17741 						rack->rack_hdrw_pacing = 0;
17742 						rack->r_ctl.crte = NULL;
17743 						rack_log_hdwr_pacing(rack,
17744 								     rate_wanted, 0, __LINE__,
17745 								     err, 1);
17746 						rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
17747 						counter_u64_add(rack_hw_pace_lost, 1);
17748 					} else if (nrte != rack->r_ctl.crte) {
17749 						rack->r_ctl.crte = nrte;
17750 						rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size_w_divisor(tp, rate_wanted,
17751 														   segsiz, pace_one, rack->r_ctl.crte,
17752 														   NULL, rack->r_ctl.pace_len_divisor);
17753 						rack_log_hdwr_pacing(rack,
17754 								     rate_wanted, rack->r_ctl.crte->rate, __LINE__,
17755 								     err, 2);
17756 						rack->r_ctl.last_hw_bw_req = rate_wanted;
17757 					}
17758 				} else {
17759 					/* We just need to adjust the segment size */
17760 					rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
17761 					rack_log_hdwr_pacing(rack,
17762 							     rate_wanted, rack->r_ctl.crte->rate, __LINE__,
17763 							     0, 4);
17764 					rack->r_ctl.last_hw_bw_req = rate_wanted;
17765 				}
17766 			}
17767 		}
17768 	done_w_hdwr:
17769 		if (rack_limit_time_with_srtt &&
17770 		    (rack->use_fixed_rate == 0) &&
17771 		    (rack->rack_hdrw_pacing == 0)) {
17772 			/*
17773 			 * Sanity check, we do not allow the pacing delay
17774 			 * to be longer than the SRTT of the path. If it is
17775 			 * a slow path, then adding a packet should increase
17776 			 * the RTT and compensate for this i.e. the srtt will
17777 			 * be greater so the allowed pacing time will be greater.
17778 			 *
17779 			 * Note this restriction is not for where a peak rate
17780 			 * is set, we are doing fixed pacing or hardware pacing.
17781 			 */
17782 			if (rack->rc_tp->t_srtt)
17783 				srtt = rack->rc_tp->t_srtt;
17784 			else
17785 				srtt = RACK_INITIAL_RTO * HPTS_USEC_IN_MSEC;	/* its in ms convert */
17786 			if (srtt < (uint64_t)slot) {
17787 				rack_log_pacing_delay_calc(rack, srtt, slot, rate_wanted, bw_est, lentim, 99, __LINE__, NULL, 0);
17788 				slot = srtt;
17789 			}
17790 		}
17791 		/*******************************************************************/
17792 		/* RRS: We insert paced call to stats here for len and rate_wanted */
17793 		/*******************************************************************/
17794 		rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, bw_est, lentim, 2, __LINE__, rsm, 0);
17795 	}
17796 	if (rack->r_ctl.crte && (rack->r_ctl.crte->rs_num_enobufs > 0)) {
17797 		/*
17798 		 * If this rate is seeing enobufs when it
17799 		 * goes to send then either the nic is out
17800 		 * of gas or we are mis-estimating the time
17801 		 * somehow and not letting the queue empty
17802 		 * completely. Lets add to the pacing time.
17803 		 */
17804 		int hw_boost_delay;
17805 
17806 		hw_boost_delay = rack->r_ctl.crte->time_between * rack_enobuf_hw_boost_mult;
17807 		if (hw_boost_delay > rack_enobuf_hw_max)
17808 			hw_boost_delay = rack_enobuf_hw_max;
17809 		else if (hw_boost_delay < rack_enobuf_hw_min)
17810 			hw_boost_delay = rack_enobuf_hw_min;
17811 		slot += hw_boost_delay;
17812 	}
17813 	return (slot);
17814 }
17815 
17816 static void
17817 rack_start_gp_measurement(struct tcpcb *tp, struct tcp_rack *rack,
17818     tcp_seq startseq, uint32_t sb_offset)
17819 {
17820 	struct rack_sendmap *my_rsm = NULL;
17821 
17822 	if (tp->t_state < TCPS_ESTABLISHED) {
17823 		/*
17824 		 * We don't start any measurements if we are
17825 		 * not at least established.
17826 		 */
17827 		return;
17828 	}
17829 	if (tp->t_state >= TCPS_FIN_WAIT_1) {
17830 		/*
17831 		 * We will get no more data into the SB
17832 		 * this means we need to have the data available
17833 		 * before we start a measurement.
17834 		 */
17835 
17836 		if (sbavail(&tptosocket(tp)->so_snd) <
17837 		    max(rc_init_window(rack),
17838 			(MIN_GP_WIN * ctf_fixed_maxseg(tp)))) {
17839 			/* Nope not enough data */
17840 			return;
17841 		}
17842 	}
17843 	tp->t_flags |= TF_GPUTINPROG;
17844 	rack->r_ctl.rc_gp_cumack_ts = 0;
17845 	rack->r_ctl.rc_gp_lowrtt = 0xffffffff;
17846 	rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
17847 	tp->gput_seq = startseq;
17848 	rack->app_limited_needs_set = 0;
17849 	if (rack->in_probe_rtt)
17850 		rack->measure_saw_probe_rtt = 1;
17851 	else if ((rack->measure_saw_probe_rtt) &&
17852 		 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
17853 		rack->measure_saw_probe_rtt = 0;
17854 	if (rack->rc_gp_filled)
17855 		tp->gput_ts = rack->r_ctl.last_cumack_advance;
17856 	else {
17857 		/* Special case initial measurement */
17858 		struct timeval tv;
17859 
17860 		tp->gput_ts = tcp_get_usecs(&tv);
17861 		rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
17862 	}
17863 	/*
17864 	 * We take a guess out into the future,
17865 	 * if we have no measurement and no
17866 	 * initial rate, we measure the first
17867 	 * initial-windows worth of data to
17868 	 * speed up getting some GP measurement and
17869 	 * thus start pacing.
17870 	 */
17871 	if ((rack->rc_gp_filled == 0) && (rack->r_ctl.init_rate == 0)) {
17872 		rack->app_limited_needs_set = 1;
17873 		tp->gput_ack = startseq + max(rc_init_window(rack),
17874 					      (MIN_GP_WIN * ctf_fixed_maxseg(tp)));
17875 		rack_log_pacing_delay_calc(rack,
17876 					   tp->gput_seq,
17877 					   tp->gput_ack,
17878 					   0,
17879 					   tp->gput_ts,
17880 					   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
17881 					   9,
17882 					   __LINE__, NULL, 0);
17883 		rack_tend_gp_marks(tp, rack);
17884 		rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
17885 		return;
17886 	}
17887 	if (sb_offset) {
17888 		/*
17889 		 * We are out somewhere in the sb
17890 		 * can we use the already outstanding data?
17891 		 */
17892 
17893 		if (rack->r_ctl.rc_app_limited_cnt == 0) {
17894 			/*
17895 			 * Yes first one is good and in this case
17896 			 * the tp->gput_ts is correctly set based on
17897 			 * the last ack that arrived (no need to
17898 			 * set things up when an ack comes in).
17899 			 */
17900 			my_rsm = tqhash_min(rack->r_ctl.tqh);
17901 			if ((my_rsm == NULL) ||
17902 			    (my_rsm->r_rtr_cnt != 1)) {
17903 				/* retransmission? */
17904 				goto use_latest;
17905 			}
17906 		} else {
17907 			if (rack->r_ctl.rc_first_appl == NULL) {
17908 				/*
17909 				 * If rc_first_appl is NULL
17910 				 * then the cnt should be 0.
17911 				 * This is probably an error, maybe
17912 				 * a KASSERT would be approprate.
17913 				 */
17914 				goto use_latest;
17915 			}
17916 			/*
17917 			 * If we have a marker pointer to the last one that is
17918 			 * app limited we can use that, but we need to set
17919 			 * things up so that when it gets ack'ed we record
17920 			 * the ack time (if its not already acked).
17921 			 */
17922 			rack->app_limited_needs_set = 1;
17923 			/*
17924 			 * We want to get to the rsm that is either
17925 			 * next with space i.e. over 1 MSS or the one
17926 			 * after that (after the app-limited).
17927 			 */
17928 			my_rsm = tqhash_next(rack->r_ctl.tqh, rack->r_ctl.rc_first_appl);
17929 			if (my_rsm) {
17930 				if ((my_rsm->r_end - my_rsm->r_start) <= ctf_fixed_maxseg(tp))
17931 					/* Have to use the next one */
17932 					my_rsm = tqhash_next(rack->r_ctl.tqh, my_rsm);
17933 				else {
17934 					/* Use after the first MSS of it is acked */
17935 					tp->gput_seq = my_rsm->r_start + ctf_fixed_maxseg(tp);
17936 					goto start_set;
17937 				}
17938 			}
17939 			if ((my_rsm == NULL) ||
17940 			    (my_rsm->r_rtr_cnt != 1)) {
17941 				/*
17942 				 * Either its a retransmit or
17943 				 * the last is the app-limited one.
17944 				 */
17945 				goto use_latest;
17946 			}
17947 		}
17948 		tp->gput_seq = my_rsm->r_start;
17949 start_set:
17950 		if (my_rsm->r_flags & RACK_ACKED) {
17951 			/*
17952 			 * This one has been acked use the arrival ack time
17953 			 */
17954 			struct rack_sendmap *nrsm;
17955 
17956 			tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival;
17957 			rack->app_limited_needs_set = 0;
17958 			/*
17959 			 * Ok in this path we need to use the r_end now
17960 			 * since this guy is the starting ack.
17961 			 */
17962 			tp->gput_seq = my_rsm->r_end;
17963 			/*
17964 			 * We also need to adjust up the sendtime
17965 			 * to the send of the next data after my_rsm.
17966 			 */
17967 			nrsm = tqhash_next(rack->r_ctl.tqh, my_rsm);
17968 			if (nrsm != NULL)
17969 				my_rsm = nrsm;
17970 			else {
17971 				/*
17972 				 * The next as not been sent, thats the
17973 				 * case for using the latest.
17974 				 */
17975 				goto use_latest;
17976 			}
17977 		}
17978 		rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[0];
17979 		tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
17980 		rack->r_ctl.rc_gp_cumack_ts = 0;
17981 		if ((rack->r_ctl.cleared_app_ack == 1) &&
17982 		    (SEQ_GEQ(rack->r_ctl.cleared_app_ack, tp->gput_seq))) {
17983 			/*
17984 			 * We just cleared an application limited period
17985 			 * so the next seq out needs to skip the first
17986 			 * ack.
17987 			 */
17988 			rack->app_limited_needs_set = 1;
17989 			rack->r_ctl.cleared_app_ack = 0;
17990 		}
17991 		rack_log_pacing_delay_calc(rack,
17992 					   tp->gput_seq,
17993 					   tp->gput_ack,
17994 					   (uintptr_t)my_rsm,
17995 					   tp->gput_ts,
17996 					   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
17997 					   9,
17998 					   __LINE__, my_rsm, 0);
17999 		/* Now lets make sure all are marked as they should be */
18000 		rack_tend_gp_marks(tp, rack);
18001 		rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
18002 		return;
18003 	}
18004 
18005 use_latest:
18006 	/*
18007 	 * We don't know how long we may have been
18008 	 * idle or if this is the first-send. Lets
18009 	 * setup the flag so we will trim off
18010 	 * the first ack'd data so we get a true
18011 	 * measurement.
18012 	 */
18013 	rack->app_limited_needs_set = 1;
18014 	tp->gput_ack = startseq + rack_get_measure_window(tp, rack);
18015 	rack->r_ctl.rc_gp_cumack_ts = 0;
18016 	/* Find this guy so we can pull the send time */
18017 	my_rsm = tqhash_find(rack->r_ctl.tqh, startseq);
18018 	if (my_rsm) {
18019 		rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[0];
18020 		if (my_rsm->r_flags & RACK_ACKED) {
18021 			/*
18022 			 * Unlikely since its probably what was
18023 			 * just transmitted (but I am paranoid).
18024 			 */
18025 			tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival;
18026 			rack->app_limited_needs_set = 0;
18027 		}
18028 		if (SEQ_LT(my_rsm->r_start, tp->gput_seq)) {
18029 			/* This also is unlikely */
18030 			tp->gput_seq = my_rsm->r_start;
18031 		}
18032 	} else {
18033 		/*
18034 		 * TSNH unless we have some send-map limit,
18035 		 * and even at that it should not be hitting
18036 		 * that limit (we should have stopped sending).
18037 		 */
18038 		struct timeval tv;
18039 
18040 		microuptime(&tv);
18041 		rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
18042 	}
18043 	rack_tend_gp_marks(tp, rack);
18044 	rack_log_pacing_delay_calc(rack,
18045 				   tp->gput_seq,
18046 				   tp->gput_ack,
18047 				   (uintptr_t)my_rsm,
18048 				   tp->gput_ts,
18049 				   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
18050 				   9, __LINE__, NULL, 0);
18051 	rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
18052 }
18053 
18054 static inline uint32_t
18055 rack_what_can_we_send(struct tcpcb *tp, struct tcp_rack *rack,  uint32_t cwnd_to_use,
18056     uint32_t avail, int32_t sb_offset)
18057 {
18058 	uint32_t len;
18059 	uint32_t sendwin;
18060 
18061 	if (tp->snd_wnd > cwnd_to_use)
18062 		sendwin = cwnd_to_use;
18063 	else
18064 		sendwin = tp->snd_wnd;
18065 	if (ctf_outstanding(tp) >= tp->snd_wnd) {
18066 		/* We never want to go over our peers rcv-window */
18067 		len = 0;
18068 	} else {
18069 		uint32_t flight;
18070 
18071 		flight = ctf_flight_size(tp, rack->r_ctl.rc_sacked);
18072 		if (flight >= sendwin) {
18073 			/*
18074 			 * We have in flight what we are allowed by cwnd (if
18075 			 * it was rwnd blocking it would have hit above out
18076 			 * >= tp->snd_wnd).
18077 			 */
18078 			return (0);
18079 		}
18080 		len = sendwin - flight;
18081 		if ((len + ctf_outstanding(tp)) > tp->snd_wnd) {
18082 			/* We would send too much (beyond the rwnd) */
18083 			len = tp->snd_wnd - ctf_outstanding(tp);
18084 		}
18085 		if ((len + sb_offset) > avail) {
18086 			/*
18087 			 * We don't have that much in the SB, how much is
18088 			 * there?
18089 			 */
18090 			len = avail - sb_offset;
18091 		}
18092 	}
18093 	return (len);
18094 }
18095 
18096 static void
18097 rack_log_fsb(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t flags,
18098 	     unsigned ipoptlen, int32_t orig_len, int32_t len, int error,
18099 	     int rsm_is_null, int optlen, int line, uint16_t mode)
18100 {
18101 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
18102 		union tcp_log_stackspecific log;
18103 		struct timeval tv;
18104 
18105 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
18106 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
18107 		log.u_bbr.flex1 = error;
18108 		log.u_bbr.flex2 = flags;
18109 		log.u_bbr.flex3 = rsm_is_null;
18110 		log.u_bbr.flex4 = ipoptlen;
18111 		log.u_bbr.flex5 = tp->rcv_numsacks;
18112 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
18113 		log.u_bbr.flex7 = optlen;
18114 		log.u_bbr.flex8 = rack->r_fsb_inited;
18115 		log.u_bbr.applimited = rack->r_fast_output;
18116 		log.u_bbr.bw_inuse = rack_get_bw(rack);
18117 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
18118 		log.u_bbr.cwnd_gain = mode;
18119 		log.u_bbr.pkts_out = orig_len;
18120 		log.u_bbr.lt_epoch = len;
18121 		log.u_bbr.delivered = line;
18122 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
18123 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18124 		tcp_log_event(tp, NULL, &so->so_rcv, &so->so_snd, TCP_LOG_FSB, 0,
18125 			       len, &log, false, NULL, __func__, __LINE__, &tv);
18126 	}
18127 }
18128 
18129 
18130 static struct mbuf *
18131 rack_fo_base_copym(struct mbuf *the_m, uint32_t the_off, int32_t *plen,
18132 		   struct rack_fast_send_blk *fsb,
18133 		   int32_t seglimit, int32_t segsize, int hw_tls)
18134 {
18135 #ifdef KERN_TLS
18136 	struct ktls_session *tls, *ntls;
18137 #ifdef INVARIANTS
18138 	struct mbuf *start;
18139 #endif
18140 #endif
18141 	struct mbuf *m, *n, **np, *smb;
18142 	struct mbuf *top;
18143 	int32_t off, soff;
18144 	int32_t len = *plen;
18145 	int32_t fragsize;
18146 	int32_t len_cp = 0;
18147 	uint32_t mlen, frags;
18148 
18149 	soff = off = the_off;
18150 	smb = m = the_m;
18151 	np = &top;
18152 	top = NULL;
18153 #ifdef KERN_TLS
18154 	if (hw_tls && (m->m_flags & M_EXTPG))
18155 		tls = m->m_epg_tls;
18156 	else
18157 		tls = NULL;
18158 #ifdef INVARIANTS
18159 	start = m;
18160 #endif
18161 #endif
18162 	while (len > 0) {
18163 		if (m == NULL) {
18164 			*plen = len_cp;
18165 			break;
18166 		}
18167 #ifdef KERN_TLS
18168 		if (hw_tls) {
18169 			if (m->m_flags & M_EXTPG)
18170 				ntls = m->m_epg_tls;
18171 			else
18172 				ntls = NULL;
18173 
18174 			/*
18175 			 * Avoid mixing TLS records with handshake
18176 			 * data or TLS records from different
18177 			 * sessions.
18178 			 */
18179 			if (tls != ntls) {
18180 				MPASS(m != start);
18181 				*plen = len_cp;
18182 				break;
18183 			}
18184 		}
18185 #endif
18186 		mlen = min(len, m->m_len - off);
18187 		if (seglimit) {
18188 			/*
18189 			 * For M_EXTPG mbufs, add 3 segments
18190 			 * + 1 in case we are crossing page boundaries
18191 			 * + 2 in case the TLS hdr/trailer are used
18192 			 * It is cheaper to just add the segments
18193 			 * than it is to take the cache miss to look
18194 			 * at the mbuf ext_pgs state in detail.
18195 			 */
18196 			if (m->m_flags & M_EXTPG) {
18197 				fragsize = min(segsize, PAGE_SIZE);
18198 				frags = 3;
18199 			} else {
18200 				fragsize = segsize;
18201 				frags = 0;
18202 			}
18203 
18204 			/* Break if we really can't fit anymore. */
18205 			if ((frags + 1) >= seglimit) {
18206 				*plen =	len_cp;
18207 				break;
18208 			}
18209 
18210 			/*
18211 			 * Reduce size if you can't copy the whole
18212 			 * mbuf. If we can't copy the whole mbuf, also
18213 			 * adjust len so the loop will end after this
18214 			 * mbuf.
18215 			 */
18216 			if ((frags + howmany(mlen, fragsize)) >= seglimit) {
18217 				mlen = (seglimit - frags - 1) * fragsize;
18218 				len = mlen;
18219 				*plen = len_cp + len;
18220 			}
18221 			frags += howmany(mlen, fragsize);
18222 			if (frags == 0)
18223 				frags++;
18224 			seglimit -= frags;
18225 			KASSERT(seglimit > 0,
18226 			    ("%s: seglimit went too low", __func__));
18227 		}
18228 		n = m_get(M_NOWAIT, m->m_type);
18229 		*np = n;
18230 		if (n == NULL)
18231 			goto nospace;
18232 		n->m_len = mlen;
18233 		soff += mlen;
18234 		len_cp += n->m_len;
18235 		if (m->m_flags & (M_EXT | M_EXTPG)) {
18236 			n->m_data = m->m_data + off;
18237 			mb_dupcl(n, m);
18238 		} else {
18239 			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
18240 			    (u_int)n->m_len);
18241 		}
18242 		len -= n->m_len;
18243 		off = 0;
18244 		m = m->m_next;
18245 		np = &n->m_next;
18246 		if (len || (soff == smb->m_len)) {
18247 			/*
18248 			 * We have more so we move forward  or
18249 			 * we have consumed the entire mbuf and
18250 			 * len has fell to 0.
18251 			 */
18252 			soff = 0;
18253 			smb = m;
18254 		}
18255 
18256 	}
18257 	if (fsb != NULL) {
18258 		fsb->m = smb;
18259 		fsb->off = soff;
18260 		if (smb) {
18261 			/*
18262 			 * Save off the size of the mbuf. We do
18263 			 * this so that we can recognize when it
18264 			 * has been trimmed by sbcut() as acks
18265 			 * come in.
18266 			 */
18267 			fsb->o_m_len = smb->m_len;
18268 			fsb->o_t_len = M_TRAILINGROOM(smb);
18269 		} else {
18270 			/*
18271 			 * This is the case where the next mbuf went to NULL. This
18272 			 * means with this copy we have sent everything in the sb.
18273 			 * In theory we could clear the fast_output flag, but lets
18274 			 * not since its possible that we could get more added
18275 			 * and acks that call the extend function which would let
18276 			 * us send more.
18277 			 */
18278 			fsb->o_m_len = 0;
18279 			fsb->o_t_len = 0;
18280 		}
18281 	}
18282 	return (top);
18283 nospace:
18284 	if (top)
18285 		m_freem(top);
18286 	return (NULL);
18287 
18288 }
18289 
18290 /*
18291  * This is a copy of m_copym(), taking the TSO segment size/limit
18292  * constraints into account, and advancing the sndptr as it goes.
18293  */
18294 static struct mbuf *
18295 rack_fo_m_copym(struct tcp_rack *rack, int32_t *plen,
18296 		int32_t seglimit, int32_t segsize, struct mbuf **s_mb, int *s_soff)
18297 {
18298 	struct mbuf *m, *n;
18299 	int32_t soff;
18300 
18301 	m = rack->r_ctl.fsb.m;
18302 	if (M_TRAILINGROOM(m) != rack->r_ctl.fsb.o_t_len) {
18303 		/*
18304 		 * The trailing space changed, mbufs can grow
18305 		 * at the tail but they can't shrink from
18306 		 * it, KASSERT that. Adjust the orig_m_len to
18307 		 * compensate for this change.
18308 		 */
18309 		KASSERT((rack->r_ctl.fsb.o_t_len > M_TRAILINGROOM(m)),
18310 			("mbuf:%p rack:%p trailing_space:%jd ots:%u oml:%u mlen:%u\n",
18311 			 m,
18312 			 rack,
18313 			 (intmax_t)M_TRAILINGROOM(m),
18314 			 rack->r_ctl.fsb.o_t_len,
18315 			 rack->r_ctl.fsb.o_m_len,
18316 			 m->m_len));
18317 		rack->r_ctl.fsb.o_m_len += (rack->r_ctl.fsb.o_t_len - M_TRAILINGROOM(m));
18318 		rack->r_ctl.fsb.o_t_len = M_TRAILINGROOM(m);
18319 	}
18320 	if (m->m_len < rack->r_ctl.fsb.o_m_len) {
18321 		/*
18322 		 * Mbuf shrank, trimmed off the top by an ack, our
18323 		 * offset changes.
18324 		 */
18325 		KASSERT((rack->r_ctl.fsb.off >= (rack->r_ctl.fsb.o_m_len - m->m_len)),
18326 			("mbuf:%p len:%u rack:%p oml:%u soff:%u\n",
18327 			 m, m->m_len,
18328 			 rack, rack->r_ctl.fsb.o_m_len,
18329 			 rack->r_ctl.fsb.off));
18330 
18331 		if (rack->r_ctl.fsb.off >= (rack->r_ctl.fsb.o_m_len- m->m_len))
18332 			rack->r_ctl.fsb.off -= (rack->r_ctl.fsb.o_m_len - m->m_len);
18333 		else
18334 			rack->r_ctl.fsb.off = 0;
18335 		rack->r_ctl.fsb.o_m_len = m->m_len;
18336 #ifdef INVARIANTS
18337 	} else if (m->m_len > rack->r_ctl.fsb.o_m_len) {
18338 		panic("rack:%p m:%p m_len grew outside of t_space compensation",
18339 		      rack, m);
18340 #endif
18341 	}
18342 	soff = rack->r_ctl.fsb.off;
18343 	KASSERT(soff >= 0, ("%s, negative off %d", __FUNCTION__, soff));
18344 	KASSERT(*plen >= 0, ("%s, negative len %d", __FUNCTION__, *plen));
18345 	KASSERT(soff < m->m_len, ("%s rack:%p len:%u m:%p m->m_len:%u < off?",
18346 				 __FUNCTION__,
18347 				 rack, *plen, m, m->m_len));
18348 	/* Save off the right location before we copy and advance */
18349 	*s_soff = soff;
18350 	*s_mb = rack->r_ctl.fsb.m;
18351 	n = rack_fo_base_copym(m, soff, plen,
18352 			       &rack->r_ctl.fsb,
18353 			       seglimit, segsize, rack->r_ctl.fsb.hw_tls);
18354 	return (n);
18355 }
18356 
18357 /* Log the buffer level */
18358 static void
18359 rack_log_queue_level(struct tcpcb *tp, struct tcp_rack *rack,
18360 		     int len, struct timeval *tv,
18361 		     uint32_t cts)
18362 {
18363 	uint32_t p_rate = 0, p_queue = 0, err = 0;
18364 	union tcp_log_stackspecific log;
18365 
18366 #ifdef RATELIMIT
18367 	err = in_pcbquery_txrlevel(rack->rc_inp, &p_queue);
18368 	err = in_pcbquery_txrtlmt(rack->rc_inp,	&p_rate);
18369 #endif
18370 	memset(&log.u_bbr, 0, sizeof(log.u_bbr));
18371 	log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
18372 	log.u_bbr.flex1 = p_rate;
18373 	log.u_bbr.flex2 = p_queue;
18374 	log.u_bbr.flex4 = (uint32_t)rack->r_ctl.crte->using;
18375 	log.u_bbr.flex5 = (uint32_t)rack->r_ctl.crte->rs_num_enobufs;
18376 	log.u_bbr.flex6 = rack->r_ctl.crte->time_between;
18377 	log.u_bbr.flex7 = 99;
18378 	log.u_bbr.flex8 = 0;
18379 	log.u_bbr.pkts_out = err;
18380 	log.u_bbr.delRate = rack->r_ctl.crte->rate;
18381 	log.u_bbr.timeStamp = cts;
18382 	log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18383 	tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_HDWR_PACE, 0,
18384 		       len, &log, false, NULL, __func__, __LINE__, tv);
18385 
18386 }
18387 
18388 static uint32_t
18389 rack_check_queue_level(struct tcp_rack *rack, struct tcpcb *tp,
18390 		       struct timeval *tv, uint32_t cts, int len, uint32_t segsiz)
18391 {
18392 	uint64_t lentime = 0;
18393 #ifdef RATELIMIT
18394 	uint32_t p_rate = 0, p_queue = 0, err;
18395 	union tcp_log_stackspecific log;
18396 	uint64_t bw;
18397 
18398 	err = in_pcbquery_txrlevel(rack->rc_inp, &p_queue);
18399 	/* Failed or queue is zero */
18400 	if (err || (p_queue == 0)) {
18401 		lentime = 0;
18402 		goto out;
18403 	}
18404 	err = in_pcbquery_txrtlmt(rack->rc_inp, &p_rate);
18405 	if (err) {
18406 		lentime = 0;
18407 		goto out;
18408 	}
18409 	/*
18410 	 * If we reach here we have some bytes in
18411 	 * the queue. The number returned is a value
18412 	 * between 0 and 0xffff where ffff is full
18413 	 * and 0 is empty. So how best to make this into
18414 	 * something usable?
18415 	 *
18416 	 * The "safer" way is lets take the b/w gotten
18417 	 * from the query (which should be our b/w rate)
18418 	 * and pretend that a full send (our rc_pace_max_segs)
18419 	 * is outstanding. We factor it so its as if a full
18420 	 * number of our MSS segment is terms of full
18421 	 * ethernet segments are outstanding.
18422 	 */
18423 	bw = p_rate / 8;
18424 	if (bw) {
18425 		lentime = (rack->r_ctl.rc_pace_max_segs / segsiz);
18426 		lentime *= ETHERNET_SEGMENT_SIZE;
18427 		lentime *= (uint64_t)HPTS_USEC_IN_SEC;
18428 		lentime /= bw;
18429 	} else {
18430 		/* TSNH -- KASSERT? */
18431 		lentime = 0;
18432 	}
18433 out:
18434 	if (tcp_bblogging_on(tp)) {
18435 		memset(&log, 0, sizeof(log));
18436 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
18437 		log.u_bbr.flex1 = p_rate;
18438 		log.u_bbr.flex2 = p_queue;
18439 		log.u_bbr.flex4 = (uint32_t)rack->r_ctl.crte->using;
18440 		log.u_bbr.flex5 = (uint32_t)rack->r_ctl.crte->rs_num_enobufs;
18441 		log.u_bbr.flex6 = rack->r_ctl.crte->time_between;
18442 		log.u_bbr.flex7 = 99;
18443 		log.u_bbr.flex8 = 0;
18444 		log.u_bbr.pkts_out = err;
18445 		log.u_bbr.delRate = rack->r_ctl.crte->rate;
18446 		log.u_bbr.cur_del_rate = lentime;
18447 		log.u_bbr.timeStamp = cts;
18448 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18449 		tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_HDWR_PACE, 0,
18450 			       len, &log, false, NULL, __func__, __LINE__,tv);
18451 	}
18452 #endif
18453 	return ((uint32_t)lentime);
18454 }
18455 
18456 static int
18457 rack_fast_rsm_output(struct tcpcb *tp, struct tcp_rack *rack, struct rack_sendmap *rsm,
18458 		     uint64_t ts_val, uint32_t cts, uint32_t ms_cts, struct timeval *tv, int len, uint8_t doing_tlp)
18459 {
18460 	/*
18461 	 * Enter the fast retransmit path. We are given that a sched_pin is
18462 	 * in place (if accounting is compliled in) and the cycle count taken
18463 	 * at the entry is in the ts_val. The concept her is that the rsm
18464 	 * now holds the mbuf offsets and such so we can directly transmit
18465 	 * without a lot of overhead, the len field is already set for
18466 	 * us to prohibit us from sending too much (usually its 1MSS).
18467 	 */
18468 	struct ip *ip = NULL;
18469 	struct udphdr *udp = NULL;
18470 	struct tcphdr *th = NULL;
18471 	struct mbuf *m = NULL;
18472 	struct inpcb *inp;
18473 	uint8_t *cpto;
18474 	struct tcp_log_buffer *lgb;
18475 #ifdef TCP_ACCOUNTING
18476 	uint64_t crtsc;
18477 	int cnt_thru = 1;
18478 #endif
18479 	struct tcpopt to;
18480 	u_char opt[TCP_MAXOLEN];
18481 	uint32_t hdrlen, optlen;
18482 	int32_t slot, segsiz, max_val, tso = 0, error = 0, ulen = 0;
18483 	uint16_t flags;
18484 	uint32_t if_hw_tsomaxsegcount = 0, startseq;
18485 	uint32_t if_hw_tsomaxsegsize;
18486 	int32_t ip_sendflag = IP_NO_SND_TAG_RL;
18487 
18488 #ifdef INET6
18489 	struct ip6_hdr *ip6 = NULL;
18490 
18491 	if (rack->r_is_v6) {
18492 		ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
18493 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
18494 	} else
18495 #endif				/* INET6 */
18496 	{
18497 		ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
18498 		hdrlen = sizeof(struct tcpiphdr);
18499 	}
18500 	if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) {
18501 		goto failed;
18502 	}
18503 	if (doing_tlp) {
18504 		/* Its a TLP add the flag, it may already be there but be sure */
18505 		rsm->r_flags |= RACK_TLP;
18506 	} else {
18507 		/* If it was a TLP it is not not on this retransmit */
18508 		rsm->r_flags &= ~RACK_TLP;
18509 	}
18510 	startseq = rsm->r_start;
18511 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
18512 	inp = rack->rc_inp;
18513 	to.to_flags = 0;
18514 	flags = tcp_outflags[tp->t_state];
18515 	if (flags & (TH_SYN|TH_RST)) {
18516 		goto failed;
18517 	}
18518 	if (rsm->r_flags & RACK_HAS_FIN) {
18519 		/* We can't send a FIN here */
18520 		goto failed;
18521 	}
18522 	if (flags & TH_FIN) {
18523 		/* We never send a FIN */
18524 		flags &= ~TH_FIN;
18525 	}
18526 	if (tp->t_flags & TF_RCVD_TSTMP) {
18527 		to.to_tsval = ms_cts + tp->ts_offset;
18528 		to.to_tsecr = tp->ts_recent;
18529 		to.to_flags = TOF_TS;
18530 	}
18531 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
18532 	/* TCP-MD5 (RFC2385). */
18533 	if (tp->t_flags & TF_SIGNATURE)
18534 		to.to_flags |= TOF_SIGNATURE;
18535 #endif
18536 	optlen = tcp_addoptions(&to, opt);
18537 	hdrlen += optlen;
18538 	udp = rack->r_ctl.fsb.udp;
18539 	if (udp)
18540 		hdrlen += sizeof(struct udphdr);
18541 	if (rack->r_ctl.rc_pace_max_segs)
18542 		max_val = rack->r_ctl.rc_pace_max_segs;
18543 	else if (rack->rc_user_set_max_segs)
18544 		max_val = rack->rc_user_set_max_segs * segsiz;
18545 	else
18546 		max_val = len;
18547 	if ((tp->t_flags & TF_TSO) &&
18548 	    V_tcp_do_tso &&
18549 	    (len > segsiz) &&
18550 	    (tp->t_port == 0))
18551 		tso = 1;
18552 #ifdef INET6
18553 	if (MHLEN < hdrlen + max_linkhdr)
18554 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
18555 	else
18556 #endif
18557 		m = m_gethdr(M_NOWAIT, MT_DATA);
18558 	if (m == NULL)
18559 		goto failed;
18560 	m->m_data += max_linkhdr;
18561 	m->m_len = hdrlen;
18562 	th = rack->r_ctl.fsb.th;
18563 	/* Establish the len to send */
18564 	if (len > max_val)
18565 		len = max_val;
18566 	if ((tso) && (len + optlen > segsiz)) {
18567 		uint32_t if_hw_tsomax;
18568 		int32_t max_len;
18569 
18570 		/* extract TSO information */
18571 		if_hw_tsomax = tp->t_tsomax;
18572 		if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
18573 		if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
18574 		/*
18575 		 * Check if we should limit by maximum payload
18576 		 * length:
18577 		 */
18578 		if (if_hw_tsomax != 0) {
18579 			/* compute maximum TSO length */
18580 			max_len = (if_hw_tsomax - hdrlen -
18581 				   max_linkhdr);
18582 			if (max_len <= 0) {
18583 				goto failed;
18584 			} else if (len > max_len) {
18585 				len = max_len;
18586 			}
18587 		}
18588 		if (len <= segsiz) {
18589 			/*
18590 			 * In case there are too many small fragments don't
18591 			 * use TSO:
18592 			 */
18593 			tso = 0;
18594 		}
18595 	} else {
18596 		tso = 0;
18597 	}
18598 	if ((tso == 0) && (len > segsiz))
18599 		len = segsiz;
18600 	(void)tcp_get_usecs(tv);
18601 	if ((len == 0) ||
18602 	    (len <= MHLEN - hdrlen - max_linkhdr)) {
18603 		goto failed;
18604 	}
18605 	th->th_seq = htonl(rsm->r_start);
18606 	th->th_ack = htonl(tp->rcv_nxt);
18607 	/*
18608 	 * The PUSH bit should only be applied
18609 	 * if the full retransmission is made. If
18610 	 * we are sending less than this is the
18611 	 * left hand edge and should not have
18612 	 * the PUSH bit.
18613 	 */
18614 	if ((rsm->r_flags & RACK_HAD_PUSH) &&
18615 	    (len == (rsm->r_end - rsm->r_start)))
18616 		flags |= TH_PUSH;
18617 	th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale));
18618 	if (th->th_win == 0) {
18619 		tp->t_sndzerowin++;
18620 		tp->t_flags |= TF_RXWIN0SENT;
18621 	} else
18622 		tp->t_flags &= ~TF_RXWIN0SENT;
18623 	if (rsm->r_flags & RACK_TLP) {
18624 		/*
18625 		 * TLP should not count in retran count, but
18626 		 * in its own bin
18627 		 */
18628 		counter_u64_add(rack_tlp_retran, 1);
18629 		counter_u64_add(rack_tlp_retran_bytes, len);
18630 	} else {
18631 		tp->t_sndrexmitpack++;
18632 		KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
18633 		KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
18634 	}
18635 #ifdef STATS
18636 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
18637 				 len);
18638 #endif
18639 	if (rsm->m == NULL)
18640 		goto failed;
18641 	if (rsm->m &&
18642 	    ((rsm->orig_m_len != rsm->m->m_len) ||
18643 	     (M_TRAILINGROOM(rsm->m) != rsm->orig_t_space))) {
18644 		/* Fix up the orig_m_len and possibly the mbuf offset */
18645 		rack_adjust_orig_mlen(rsm);
18646 	}
18647 	m->m_next = rack_fo_base_copym(rsm->m, rsm->soff, &len, NULL, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, rsm->r_hw_tls);
18648 	if (len <= segsiz) {
18649 		/*
18650 		 * Must have ran out of mbufs for the copy
18651 		 * shorten it to no longer need tso. Lets
18652 		 * not put on sendalot since we are low on
18653 		 * mbufs.
18654 		 */
18655 		tso = 0;
18656 	}
18657 	if ((m->m_next == NULL) || (len <= 0)){
18658 		goto failed;
18659 	}
18660 	if (udp) {
18661 		if (rack->r_is_v6)
18662 			ulen = hdrlen + len - sizeof(struct ip6_hdr);
18663 		else
18664 			ulen = hdrlen + len - sizeof(struct ip);
18665 		udp->uh_ulen = htons(ulen);
18666 	}
18667 	m->m_pkthdr.rcvif = (struct ifnet *)0;
18668 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
18669 	    (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) {
18670 		int ect = tcp_ecn_output_established(tp, &flags, len, true);
18671 		if ((tp->t_state == TCPS_SYN_RECEIVED) &&
18672 		    (tp->t_flags2 & TF2_ECN_SND_ECE))
18673 		    tp->t_flags2 &= ~TF2_ECN_SND_ECE;
18674 #ifdef INET6
18675 		if (rack->r_is_v6) {
18676 		    ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
18677 		    ip6->ip6_flow |= htonl(ect << 20);
18678 		}
18679 		else
18680 #endif
18681 		{
18682 		    ip->ip_tos &= ~IPTOS_ECN_MASK;
18683 		    ip->ip_tos |= ect;
18684 		}
18685 	}
18686 	if (rack->r_ctl.crte != NULL) {
18687 		/* See if we can send via the hw queue */
18688 		slot = rack_check_queue_level(rack, tp, tv, cts, len, segsiz);
18689 		/* If there is nothing in queue (no pacing time) we can send via the hw queue */
18690 		if (slot == 0)
18691 			ip_sendflag = 0;
18692 	}
18693 	tcp_set_flags(th, flags);
18694 	m->m_pkthdr.len = hdrlen + len;	/* in6_cksum() need this */
18695 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
18696 	if (to.to_flags & TOF_SIGNATURE) {
18697 		/*
18698 		 * Calculate MD5 signature and put it into the place
18699 		 * determined before.
18700 		 * NOTE: since TCP options buffer doesn't point into
18701 		 * mbuf's data, calculate offset and use it.
18702 		 */
18703 		if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
18704 						       (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
18705 			/*
18706 			 * Do not send segment if the calculation of MD5
18707 			 * digest has failed.
18708 			 */
18709 			goto failed;
18710 		}
18711 	}
18712 #endif
18713 #ifdef INET6
18714 	if (rack->r_is_v6) {
18715 		if (tp->t_port) {
18716 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
18717 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
18718 			udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
18719 			th->th_sum = htons(0);
18720 			UDPSTAT_INC(udps_opackets);
18721 		} else {
18722 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
18723 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
18724 			th->th_sum = in6_cksum_pseudo(ip6,
18725 						      sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
18726 						      0);
18727 		}
18728 	}
18729 #endif
18730 #if defined(INET6) && defined(INET)
18731 	else
18732 #endif
18733 #ifdef INET
18734 	{
18735 		if (tp->t_port) {
18736 			m->m_pkthdr.csum_flags = CSUM_UDP;
18737 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
18738 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
18739 						ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
18740 			th->th_sum = htons(0);
18741 			UDPSTAT_INC(udps_opackets);
18742 		} else {
18743 			m->m_pkthdr.csum_flags = CSUM_TCP;
18744 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
18745 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
18746 					       ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
18747 									IPPROTO_TCP + len + optlen));
18748 		}
18749 		/* IP version must be set here for ipv4/ipv6 checking later */
18750 		KASSERT(ip->ip_v == IPVERSION,
18751 			("%s: IP version incorrect: %d", __func__, ip->ip_v));
18752 	}
18753 #endif
18754 	if (tso) {
18755 		/*
18756 		 * Here we use segsiz since we have no added options besides
18757 		 * any standard timestamp options (no DSACKs or SACKS are sent
18758 		 * via either fast-path).
18759 		 */
18760 		KASSERT(len > segsiz,
18761 			("%s: len <= tso_segsz tp:%p", __func__, tp));
18762 		m->m_pkthdr.csum_flags |= CSUM_TSO;
18763 		m->m_pkthdr.tso_segsz = segsiz;
18764 	}
18765 #ifdef INET6
18766 	if (rack->r_is_v6) {
18767 		ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit;
18768 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
18769 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
18770 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
18771 		else
18772 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
18773 	}
18774 #endif
18775 #if defined(INET) && defined(INET6)
18776 	else
18777 #endif
18778 #ifdef INET
18779 	{
18780 		ip->ip_len = htons(m->m_pkthdr.len);
18781 		ip->ip_ttl = rack->r_ctl.fsb.hoplimit;
18782 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
18783 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
18784 			if (tp->t_port == 0 || len < V_tcp_minmss) {
18785 				ip->ip_off |= htons(IP_DF);
18786 			}
18787 		} else {
18788 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
18789 		}
18790 	}
18791 #endif
18792 	if (doing_tlp == 0) {
18793 		/* Set we retransmitted */
18794 		rack->rc_gp_saw_rec = 1;
18795 	} else {
18796 		/* Its a TLP set ca or ss */
18797 		if (tp->snd_cwnd > tp->snd_ssthresh) {
18798 			/* Set we sent in CA */
18799 			rack->rc_gp_saw_ca = 1;
18800 		} else {
18801 			/* Set we sent in SS */
18802 			rack->rc_gp_saw_ss = 1;
18803 		}
18804 	}
18805 	/* Time to copy in our header */
18806 	cpto = mtod(m, uint8_t *);
18807 	memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
18808 	th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
18809 	if (optlen) {
18810 		bcopy(opt, th + 1, optlen);
18811 		th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
18812 	} else {
18813 		th->th_off = sizeof(struct tcphdr) >> 2;
18814 	}
18815 	if (tcp_bblogging_on(rack->rc_tp)) {
18816 		union tcp_log_stackspecific log;
18817 
18818 		if (rsm->r_flags & RACK_RWND_COLLAPSED) {
18819 			rack_log_collapse(rack, rsm->r_start, rsm->r_end, 0, __LINE__, 5, rsm->r_flags, rsm);
18820 			counter_u64_add(rack_collapsed_win_rxt, 1);
18821 			counter_u64_add(rack_collapsed_win_rxt_bytes, (rsm->r_end - rsm->r_start));
18822 		}
18823 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
18824 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
18825 		if (rack->rack_no_prr)
18826 			log.u_bbr.flex1 = 0;
18827 		else
18828 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
18829 		log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
18830 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
18831 		log.u_bbr.flex4 = max_val;
18832 		/* Save off the early/late values */
18833 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
18834 		log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
18835 		log.u_bbr.bw_inuse = rack_get_bw(rack);
18836 		log.u_bbr.cur_del_rate = rack->r_ctl.gp_bw;
18837 		if (doing_tlp == 0)
18838 			log.u_bbr.flex8 = 1;
18839 		else
18840 			log.u_bbr.flex8 = 2;
18841 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
18842 		log.u_bbr.flex7 = 55;
18843 		log.u_bbr.pkts_out = tp->t_maxseg;
18844 		log.u_bbr.timeStamp = cts;
18845 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18846 		if (rsm->r_rtr_cnt > 0) {
18847 			/*
18848 			 * When we have a retransmit we want to log the
18849 			 * burst at send and flight at send from before.
18850 			 */
18851 			log.u_bbr.flex5 = rsm->r_fas;
18852 			log.u_bbr.bbr_substate = rsm->r_bas;
18853 		} else {
18854 			/*
18855 			 * This is currently unlikely until we do the
18856 			 * packet pair probes but I will add it for completeness.
18857 			 */
18858 			log.u_bbr.flex5 = log.u_bbr.inflight;
18859 			log.u_bbr.bbr_substate = (uint8_t)((len + segsiz - 1)/segsiz);
18860 		}
18861 		log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use;
18862 		log.u_bbr.delivered = 0;
18863 		log.u_bbr.rttProp = (uintptr_t)rsm;
18864 		log.u_bbr.delRate = rsm->r_flags;
18865 		log.u_bbr.delRate <<= 31;
18866 		log.u_bbr.delRate |= rack->r_must_retran;
18867 		log.u_bbr.delRate <<= 1;
18868 		log.u_bbr.delRate |= 1;
18869 		log.u_bbr.pkt_epoch = __LINE__;
18870 		lgb = tcp_log_event(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
18871 				     len, &log, false, NULL, __func__, __LINE__, tv);
18872 	} else
18873 		lgb = NULL;
18874 	if ((rack->r_ctl.crte != NULL) &&
18875 	    tcp_bblogging_on(tp)) {
18876 		rack_log_queue_level(tp, rack, len, tv, cts);
18877 	}
18878 #ifdef INET6
18879 	if (rack->r_is_v6) {
18880 		error = ip6_output(m, inp->in6p_outputopts,
18881 				   &inp->inp_route6,
18882 				   ip_sendflag, NULL, NULL, inp);
18883 	}
18884 	else
18885 #endif
18886 #ifdef INET
18887 	{
18888 		error = ip_output(m, NULL,
18889 				  &inp->inp_route,
18890 				  ip_sendflag, 0, inp);
18891 	}
18892 #endif
18893 	m = NULL;
18894 	if (lgb) {
18895 		lgb->tlb_errno = error;
18896 		lgb = NULL;
18897 	}
18898 	/* Move snd_nxt to snd_max so we don't have false retransmissions */
18899 	tp->snd_nxt = tp->snd_max;
18900 	if (error) {
18901 		goto failed;
18902 	} else if (rack->rc_hw_nobuf && (ip_sendflag != IP_NO_SND_TAG_RL)) {
18903 		rack->rc_hw_nobuf = 0;
18904 		rack->r_ctl.rc_agg_delayed = 0;
18905 		rack->r_early = 0;
18906 		rack->r_late = 0;
18907 		rack->r_ctl.rc_agg_early = 0;
18908 	}
18909 	rack_log_output(tp, &to, len, rsm->r_start, flags, error, rack_to_usec_ts(tv),
18910 			rsm, RACK_SENT_FP, rsm->m, rsm->soff, rsm->r_hw_tls, segsiz);
18911 	if (doing_tlp) {
18912 		rack->rc_tlp_in_progress = 1;
18913 		rack->r_ctl.rc_tlp_cnt_out++;
18914 	}
18915 	if (error == 0) {
18916 		counter_u64_add(rack_total_bytes, len);
18917 		tcp_account_for_send(tp, len, 1, doing_tlp, rsm->r_hw_tls);
18918 		if (doing_tlp) {
18919 			rack->rc_last_sent_tlp_past_cumack = 0;
18920 			rack->rc_last_sent_tlp_seq_valid = 1;
18921 			rack->r_ctl.last_sent_tlp_seq = rsm->r_start;
18922 			rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start;
18923 		}
18924 		if (rack->r_ctl.rc_prr_sndcnt >= len)
18925 			rack->r_ctl.rc_prr_sndcnt -= len;
18926 		else
18927 			rack->r_ctl.rc_prr_sndcnt = 0;
18928 	}
18929 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
18930 	rack->forced_ack = 0;	/* If we send something zap the FA flag */
18931 	if (IN_FASTRECOVERY(tp->t_flags) && rsm)
18932 		rack->r_ctl.retran_during_recovery += len;
18933 	{
18934 		int idx;
18935 
18936 		idx = (len / segsiz) + 3;
18937 		if (idx >= TCP_MSS_ACCT_ATIMER)
18938 			counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
18939 		else
18940 			counter_u64_add(rack_out_size[idx], 1);
18941 	}
18942 	if (tp->t_rtttime == 0) {
18943 		tp->t_rtttime = ticks;
18944 		tp->t_rtseq = startseq;
18945 		KMOD_TCPSTAT_INC(tcps_segstimed);
18946 	}
18947 	counter_u64_add(rack_fto_rsm_send, 1);
18948 	if (error && (error == ENOBUFS)) {
18949 		if (rack->r_ctl.crte != NULL) {
18950 			tcp_trace_point(rack->rc_tp, TCP_TP_HWENOBUF);
18951 			if (tcp_bblogging_on(rack->rc_tp))
18952 				rack_log_queue_level(tp, rack, len, tv, cts);
18953 		} else
18954 			tcp_trace_point(rack->rc_tp, TCP_TP_ENOBUF);
18955 		slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC);
18956 		if (rack->rc_enobuf < 0x7f)
18957 			rack->rc_enobuf++;
18958 		if (slot < (10 * HPTS_USEC_IN_MSEC))
18959 			slot = 10 * HPTS_USEC_IN_MSEC;
18960 		if (rack->r_ctl.crte != NULL) {
18961 			counter_u64_add(rack_saw_enobuf_hw, 1);
18962 			tcp_rl_log_enobuf(rack->r_ctl.crte);
18963 		}
18964 		counter_u64_add(rack_saw_enobuf, 1);
18965 	} else {
18966 		slot = rack_get_pacing_delay(rack, tp, len, NULL, segsiz, __LINE__);
18967 	}
18968 	rack_start_hpts_timer(rack, tp, cts, slot, len, 0);
18969 #ifdef TCP_ACCOUNTING
18970 	crtsc = get_cyclecount();
18971 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
18972 		tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru;
18973 		tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
18974 		tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((len + segsiz - 1) / segsiz);
18975 	}
18976 	sched_unpin();
18977 #endif
18978 	return (0);
18979 failed:
18980 	if (m)
18981 		m_free(m);
18982 	return (-1);
18983 }
18984 
18985 static void
18986 rack_sndbuf_autoscale(struct tcp_rack *rack)
18987 {
18988 	/*
18989 	 * Automatic sizing of send socket buffer.  Often the send buffer
18990 	 * size is not optimally adjusted to the actual network conditions
18991 	 * at hand (delay bandwidth product).  Setting the buffer size too
18992 	 * small limits throughput on links with high bandwidth and high
18993 	 * delay (eg. trans-continental/oceanic links).  Setting the
18994 	 * buffer size too big consumes too much real kernel memory,
18995 	 * especially with many connections on busy servers.
18996 	 *
18997 	 * The criteria to step up the send buffer one notch are:
18998 	 *  1. receive window of remote host is larger than send buffer
18999 	 *     (with a fudge factor of 5/4th);
19000 	 *  2. send buffer is filled to 7/8th with data (so we actually
19001 	 *     have data to make use of it);
19002 	 *  3. send buffer fill has not hit maximal automatic size;
19003 	 *  4. our send window (slow start and cogestion controlled) is
19004 	 *     larger than sent but unacknowledged data in send buffer.
19005 	 *
19006 	 * Note that the rack version moves things much faster since
19007 	 * we want to avoid hitting cache lines in the rack_fast_output()
19008 	 * path so this is called much less often and thus moves
19009 	 * the SB forward by a percentage.
19010 	 */
19011 	struct socket *so;
19012 	struct tcpcb *tp;
19013 	uint32_t sendwin, scaleup;
19014 
19015 	tp = rack->rc_tp;
19016 	so = rack->rc_inp->inp_socket;
19017 	sendwin = min(rack->r_ctl.cwnd_to_use, tp->snd_wnd);
19018 	if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) {
19019 		if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
19020 		    sbused(&so->so_snd) >=
19021 		    (so->so_snd.sb_hiwat / 8 * 7) &&
19022 		    sbused(&so->so_snd) < V_tcp_autosndbuf_max &&
19023 		    sendwin >= (sbused(&so->so_snd) -
19024 		    (tp->snd_max - tp->snd_una))) {
19025 			if (rack_autosndbuf_inc)
19026 				scaleup = (rack_autosndbuf_inc * so->so_snd.sb_hiwat) / 100;
19027 			else
19028 				scaleup = V_tcp_autosndbuf_inc;
19029 			if (scaleup < V_tcp_autosndbuf_inc)
19030 				scaleup = V_tcp_autosndbuf_inc;
19031 			scaleup += so->so_snd.sb_hiwat;
19032 			if (scaleup > V_tcp_autosndbuf_max)
19033 				scaleup = V_tcp_autosndbuf_max;
19034 			if (!sbreserve_locked(so, SO_SND, scaleup, curthread))
19035 				so->so_snd.sb_flags &= ~SB_AUTOSIZE;
19036 		}
19037 	}
19038 }
19039 
19040 static int
19041 rack_fast_output(struct tcpcb *tp, struct tcp_rack *rack, uint64_t ts_val,
19042 		 uint32_t cts, uint32_t ms_cts, struct timeval *tv, long tot_len, int *send_err)
19043 {
19044 	/*
19045 	 * Enter to do fast output. We are given that the sched_pin is
19046 	 * in place (if accounting is compiled in) and the cycle count taken
19047 	 * at entry is in place in ts_val. The idea here is that
19048 	 * we know how many more bytes needs to be sent (presumably either
19049 	 * during pacing or to fill the cwnd and that was greater than
19050 	 * the max-burst). We have how much to send and all the info we
19051 	 * need to just send.
19052 	 */
19053 #ifdef INET
19054 	struct ip *ip = NULL;
19055 #endif
19056 	struct udphdr *udp = NULL;
19057 	struct tcphdr *th = NULL;
19058 	struct mbuf *m, *s_mb;
19059 	struct inpcb *inp;
19060 	uint8_t *cpto;
19061 	struct tcp_log_buffer *lgb;
19062 #ifdef TCP_ACCOUNTING
19063 	uint64_t crtsc;
19064 #endif
19065 	struct tcpopt to;
19066 	u_char opt[TCP_MAXOLEN];
19067 	uint32_t hdrlen, optlen;
19068 #ifdef TCP_ACCOUNTING
19069 	int cnt_thru = 1;
19070 #endif
19071 	int32_t slot, segsiz, len, max_val, tso = 0, sb_offset, error, ulen = 0;
19072 	uint16_t flags;
19073 	uint32_t s_soff;
19074 	uint32_t if_hw_tsomaxsegcount = 0, startseq;
19075 	uint32_t if_hw_tsomaxsegsize;
19076 	uint32_t add_flag = RACK_SENT_FP;
19077 #ifdef INET6
19078 	struct ip6_hdr *ip6 = NULL;
19079 
19080 	if (rack->r_is_v6) {
19081 		ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
19082 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
19083 	} else
19084 #endif				/* INET6 */
19085 	{
19086 #ifdef INET
19087 		ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
19088 		hdrlen = sizeof(struct tcpiphdr);
19089 #endif
19090 	}
19091 	if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) {
19092 		m = NULL;
19093 		goto failed;
19094 	}
19095 	rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
19096 	startseq = tp->snd_max;
19097 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
19098 	inp = rack->rc_inp;
19099 	len = rack->r_ctl.fsb.left_to_send;
19100 	to.to_flags = 0;
19101 	flags = rack->r_ctl.fsb.tcp_flags;
19102 	if (tp->t_flags & TF_RCVD_TSTMP) {
19103 		to.to_tsval = ms_cts + tp->ts_offset;
19104 		to.to_tsecr = tp->ts_recent;
19105 		to.to_flags = TOF_TS;
19106 	}
19107 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
19108 	/* TCP-MD5 (RFC2385). */
19109 	if (tp->t_flags & TF_SIGNATURE)
19110 		to.to_flags |= TOF_SIGNATURE;
19111 #endif
19112 	optlen = tcp_addoptions(&to, opt);
19113 	hdrlen += optlen;
19114 	udp = rack->r_ctl.fsb.udp;
19115 	if (udp)
19116 		hdrlen += sizeof(struct udphdr);
19117 	if (rack->r_ctl.rc_pace_max_segs)
19118 		max_val = rack->r_ctl.rc_pace_max_segs;
19119 	else if (rack->rc_user_set_max_segs)
19120 		max_val = rack->rc_user_set_max_segs * segsiz;
19121 	else
19122 		max_val = len;
19123 	if ((tp->t_flags & TF_TSO) &&
19124 	    V_tcp_do_tso &&
19125 	    (len > segsiz) &&
19126 	    (tp->t_port == 0))
19127 		tso = 1;
19128 again:
19129 #ifdef INET6
19130 	if (MHLEN < hdrlen + max_linkhdr)
19131 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
19132 	else
19133 #endif
19134 		m = m_gethdr(M_NOWAIT, MT_DATA);
19135 	if (m == NULL)
19136 		goto failed;
19137 	m->m_data += max_linkhdr;
19138 	m->m_len = hdrlen;
19139 	th = rack->r_ctl.fsb.th;
19140 	/* Establish the len to send */
19141 	if (len > max_val)
19142 		len = max_val;
19143 	if ((tso) && (len + optlen > segsiz)) {
19144 		uint32_t if_hw_tsomax;
19145 		int32_t max_len;
19146 
19147 		/* extract TSO information */
19148 		if_hw_tsomax = tp->t_tsomax;
19149 		if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
19150 		if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
19151 		/*
19152 		 * Check if we should limit by maximum payload
19153 		 * length:
19154 		 */
19155 		if (if_hw_tsomax != 0) {
19156 			/* compute maximum TSO length */
19157 			max_len = (if_hw_tsomax - hdrlen -
19158 				   max_linkhdr);
19159 			if (max_len <= 0) {
19160 				goto failed;
19161 			} else if (len > max_len) {
19162 				len = max_len;
19163 			}
19164 		}
19165 		if (len <= segsiz) {
19166 			/*
19167 			 * In case there are too many small fragments don't
19168 			 * use TSO:
19169 			 */
19170 			tso = 0;
19171 		}
19172 	} else {
19173 		tso = 0;
19174 	}
19175 	if ((tso == 0) && (len > segsiz))
19176 		len = segsiz;
19177 	(void)tcp_get_usecs(tv);
19178 	if ((len == 0) ||
19179 	    (len <= MHLEN - hdrlen - max_linkhdr)) {
19180 		goto failed;
19181 	}
19182 	sb_offset = tp->snd_max - tp->snd_una;
19183 	th->th_seq = htonl(tp->snd_max);
19184 	th->th_ack = htonl(tp->rcv_nxt);
19185 	th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale));
19186 	if (th->th_win == 0) {
19187 		tp->t_sndzerowin++;
19188 		tp->t_flags |= TF_RXWIN0SENT;
19189 	} else
19190 		tp->t_flags &= ~TF_RXWIN0SENT;
19191 	tp->snd_up = tp->snd_una;	/* drag it along, its deprecated */
19192 	KMOD_TCPSTAT_INC(tcps_sndpack);
19193 	KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
19194 #ifdef STATS
19195 	stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
19196 				 len);
19197 #endif
19198 	if (rack->r_ctl.fsb.m == NULL)
19199 		goto failed;
19200 
19201 	/* s_mb and s_soff are saved for rack_log_output */
19202 	m->m_next = rack_fo_m_copym(rack, &len, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize,
19203 				    &s_mb, &s_soff);
19204 	if (len <= segsiz) {
19205 		/*
19206 		 * Must have ran out of mbufs for the copy
19207 		 * shorten it to no longer need tso. Lets
19208 		 * not put on sendalot since we are low on
19209 		 * mbufs.
19210 		 */
19211 		tso = 0;
19212 	}
19213 	if (rack->r_ctl.fsb.rfo_apply_push &&
19214 	    (len == rack->r_ctl.fsb.left_to_send)) {
19215 		tcp_set_flags(th, flags | TH_PUSH);
19216 		add_flag |= RACK_HAD_PUSH;
19217 	}
19218 	if ((m->m_next == NULL) || (len <= 0)){
19219 		goto failed;
19220 	}
19221 	if (udp) {
19222 		if (rack->r_is_v6)
19223 			ulen = hdrlen + len - sizeof(struct ip6_hdr);
19224 		else
19225 			ulen = hdrlen + len - sizeof(struct ip);
19226 		udp->uh_ulen = htons(ulen);
19227 	}
19228 	m->m_pkthdr.rcvif = (struct ifnet *)0;
19229 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
19230 	    (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) {
19231 		int ect = tcp_ecn_output_established(tp, &flags, len, false);
19232 		if ((tp->t_state == TCPS_SYN_RECEIVED) &&
19233 		    (tp->t_flags2 & TF2_ECN_SND_ECE))
19234 			tp->t_flags2 &= ~TF2_ECN_SND_ECE;
19235 #ifdef INET6
19236 		if (rack->r_is_v6) {
19237 			ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
19238 			ip6->ip6_flow |= htonl(ect << 20);
19239 		}
19240 		else
19241 #endif
19242 		{
19243 #ifdef INET
19244 			ip->ip_tos &= ~IPTOS_ECN_MASK;
19245 			ip->ip_tos |= ect;
19246 #endif
19247 		}
19248 	}
19249 	tcp_set_flags(th, flags);
19250 	m->m_pkthdr.len = hdrlen + len;	/* in6_cksum() need this */
19251 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
19252 	if (to.to_flags & TOF_SIGNATURE) {
19253 		/*
19254 		 * Calculate MD5 signature and put it into the place
19255 		 * determined before.
19256 		 * NOTE: since TCP options buffer doesn't point into
19257 		 * mbuf's data, calculate offset and use it.
19258 		 */
19259 		if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
19260 						       (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
19261 			/*
19262 			 * Do not send segment if the calculation of MD5
19263 			 * digest has failed.
19264 			 */
19265 			goto failed;
19266 		}
19267 	}
19268 #endif
19269 #ifdef INET6
19270 	if (rack->r_is_v6) {
19271 		if (tp->t_port) {
19272 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
19273 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
19274 			udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
19275 			th->th_sum = htons(0);
19276 			UDPSTAT_INC(udps_opackets);
19277 		} else {
19278 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
19279 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
19280 			th->th_sum = in6_cksum_pseudo(ip6,
19281 						      sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
19282 						      0);
19283 		}
19284 	}
19285 #endif
19286 #if defined(INET6) && defined(INET)
19287 	else
19288 #endif
19289 #ifdef INET
19290 	{
19291 		if (tp->t_port) {
19292 			m->m_pkthdr.csum_flags = CSUM_UDP;
19293 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
19294 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
19295 						ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
19296 			th->th_sum = htons(0);
19297 			UDPSTAT_INC(udps_opackets);
19298 		} else {
19299 			m->m_pkthdr.csum_flags = CSUM_TCP;
19300 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
19301 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
19302 					       ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
19303 									IPPROTO_TCP + len + optlen));
19304 		}
19305 		/* IP version must be set here for ipv4/ipv6 checking later */
19306 		KASSERT(ip->ip_v == IPVERSION,
19307 			("%s: IP version incorrect: %d", __func__, ip->ip_v));
19308 	}
19309 #endif
19310 	if (tso) {
19311 		/*
19312 		 * Here we use segsiz since we have no added options besides
19313 		 * any standard timestamp options (no DSACKs or SACKS are sent
19314 		 * via either fast-path).
19315 		 */
19316 		KASSERT(len > segsiz,
19317 			("%s: len <= tso_segsz tp:%p", __func__, tp));
19318 		m->m_pkthdr.csum_flags |= CSUM_TSO;
19319 		m->m_pkthdr.tso_segsz = segsiz;
19320 	}
19321 #ifdef INET6
19322 	if (rack->r_is_v6) {
19323 		ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit;
19324 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
19325 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
19326 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
19327 		else
19328 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
19329 	}
19330 #endif
19331 #if defined(INET) && defined(INET6)
19332 	else
19333 #endif
19334 #ifdef INET
19335 	{
19336 		ip->ip_len = htons(m->m_pkthdr.len);
19337 		ip->ip_ttl = rack->r_ctl.fsb.hoplimit;
19338 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
19339 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
19340 			if (tp->t_port == 0 || len < V_tcp_minmss) {
19341 				ip->ip_off |= htons(IP_DF);
19342 			}
19343 		} else {
19344 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
19345 		}
19346 	}
19347 #endif
19348 	if (tp->snd_cwnd > tp->snd_ssthresh) {
19349 		/* Set we sent in CA */
19350 		rack->rc_gp_saw_ca = 1;
19351 	} else {
19352 		/* Set we sent in SS */
19353 		rack->rc_gp_saw_ss = 1;
19354 	}
19355 	/* Time to copy in our header */
19356 	cpto = mtod(m, uint8_t *);
19357 	memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
19358 	th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
19359 	if (optlen) {
19360 		bcopy(opt, th + 1, optlen);
19361 		th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
19362 	} else {
19363 		th->th_off = sizeof(struct tcphdr) >> 2;
19364 	}
19365 	if ((rack->r_ctl.crte != NULL) &&
19366 	    tcp_bblogging_on(tp)) {
19367 		rack_log_queue_level(tp, rack, len, tv, cts);
19368 	}
19369 	if (tcp_bblogging_on(rack->rc_tp)) {
19370 		union tcp_log_stackspecific log;
19371 
19372 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
19373 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
19374 		if (rack->rack_no_prr)
19375 			log.u_bbr.flex1 = 0;
19376 		else
19377 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
19378 		log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
19379 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
19380 		log.u_bbr.flex4 = max_val;
19381 		/* Save off the early/late values */
19382 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
19383 		log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
19384 		log.u_bbr.bw_inuse = rack_get_bw(rack);
19385 		log.u_bbr.cur_del_rate = rack->r_ctl.gp_bw;
19386 		log.u_bbr.flex8 = 0;
19387 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
19388 		log.u_bbr.flex7 = 44;
19389 		log.u_bbr.pkts_out = tp->t_maxseg;
19390 		log.u_bbr.timeStamp = cts;
19391 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
19392 		log.u_bbr.flex5 = log.u_bbr.inflight;
19393 		log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use;
19394 		log.u_bbr.delivered = 0;
19395 		log.u_bbr.rttProp = 0;
19396 		log.u_bbr.delRate = rack->r_must_retran;
19397 		log.u_bbr.delRate <<= 1;
19398 		log.u_bbr.pkt_epoch = __LINE__;
19399 		/* For fast output no retrans so just inflight and how many mss we send */
19400 		log.u_bbr.flex5 = log.u_bbr.inflight;
19401 		log.u_bbr.bbr_substate = (uint8_t)((len + segsiz - 1)/segsiz);
19402 		lgb = tcp_log_event(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
19403 				     len, &log, false, NULL, __func__, __LINE__, tv);
19404 	} else
19405 		lgb = NULL;
19406 #ifdef INET6
19407 	if (rack->r_is_v6) {
19408 		error = ip6_output(m, inp->in6p_outputopts,
19409 				   &inp->inp_route6,
19410 				   0, NULL, NULL, inp);
19411 	}
19412 #endif
19413 #if defined(INET) && defined(INET6)
19414 	else
19415 #endif
19416 #ifdef INET
19417 	{
19418 		error = ip_output(m, NULL,
19419 				  &inp->inp_route,
19420 				  0, 0, inp);
19421 	}
19422 #endif
19423 	if (lgb) {
19424 		lgb->tlb_errno = error;
19425 		lgb = NULL;
19426 	}
19427 	if (error) {
19428 		*send_err = error;
19429 		m = NULL;
19430 		goto failed;
19431 	} else if (rack->rc_hw_nobuf) {
19432 		rack->rc_hw_nobuf = 0;
19433 		rack->r_ctl.rc_agg_delayed = 0;
19434 		rack->r_early = 0;
19435 		rack->r_late = 0;
19436 		rack->r_ctl.rc_agg_early = 0;
19437 	}
19438 	if ((error == 0) && (rack->lt_bw_up == 0)) {
19439 		/* Unlikely */
19440 		rack->r_ctl.lt_timemark = tcp_tv_to_lusectick(tv);
19441 		rack->r_ctl.lt_seq = tp->snd_una;
19442 		rack->lt_bw_up = 1;
19443 	} else if ((error == 0) &&
19444 		   (((tp->snd_max + len) - rack->r_ctl.lt_seq) > 0x7fffffff)) {
19445 		/*
19446 		 * Need to record what we have since we are
19447 		 * approaching seq wrap.
19448 		 */
19449 		struct timeval tv;
19450 		uint64_t tmark;
19451 
19452 		rack->r_ctl.lt_bw_bytes += (tp->snd_una - rack->r_ctl.lt_seq);
19453 		rack->r_ctl.lt_seq = tp->snd_una;
19454 		tmark = tcp_get_u64_usecs(&tv);
19455 		if (tmark > rack->r_ctl.lt_timemark) {
19456 			rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark);
19457 			rack->r_ctl.lt_timemark = tmark;
19458 		}
19459 	}
19460 	rack_log_output(tp, &to, len, tp->snd_max, flags, error, rack_to_usec_ts(tv),
19461 			NULL, add_flag, s_mb, s_soff, rack->r_ctl.fsb.hw_tls, segsiz);
19462 	if (tp->snd_una == tp->snd_max) {
19463 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
19464 		rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
19465 		tp->t_acktime = ticks;
19466 	}
19467 	counter_u64_add(rack_total_bytes, len);
19468 	tcp_account_for_send(tp, len, 0, 0, rack->r_ctl.fsb.hw_tls);
19469 
19470 	rack->forced_ack = 0;	/* If we send something zap the FA flag */
19471 	tot_len += len;
19472 	if ((tp->t_flags & TF_GPUTINPROG) == 0)
19473 		rack_start_gp_measurement(tp, rack, tp->snd_max, sb_offset);
19474 	tp->snd_max += len;
19475 	tp->snd_nxt = tp->snd_max;
19476 	if (rack->rc_new_rnd_needed) {
19477 		rack_new_round_starts(tp, rack, tp->snd_max);
19478 	}
19479 	{
19480 		int idx;
19481 
19482 		idx = (len / segsiz) + 3;
19483 		if (idx >= TCP_MSS_ACCT_ATIMER)
19484 			counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
19485 		else
19486 			counter_u64_add(rack_out_size[idx], 1);
19487 	}
19488 	if (len <= rack->r_ctl.fsb.left_to_send)
19489 		rack->r_ctl.fsb.left_to_send -= len;
19490 	else
19491 		rack->r_ctl.fsb.left_to_send = 0;
19492 	if (rack->r_ctl.fsb.left_to_send < segsiz) {
19493 		rack->r_fast_output = 0;
19494 		rack->r_ctl.fsb.left_to_send = 0;
19495 		/* At the end of fast_output scale up the sb */
19496 		SOCK_SENDBUF_LOCK(rack->rc_inp->inp_socket);
19497 		rack_sndbuf_autoscale(rack);
19498 		SOCK_SENDBUF_UNLOCK(rack->rc_inp->inp_socket);
19499 	}
19500 	if (tp->t_rtttime == 0) {
19501 		tp->t_rtttime = ticks;
19502 		tp->t_rtseq = startseq;
19503 		KMOD_TCPSTAT_INC(tcps_segstimed);
19504 	}
19505 	if ((rack->r_ctl.fsb.left_to_send >= segsiz) &&
19506 	    (max_val > len) &&
19507 	    (tso == 0)) {
19508 		max_val -= len;
19509 		len = segsiz;
19510 		th = rack->r_ctl.fsb.th;
19511 #ifdef TCP_ACCOUNTING
19512 		cnt_thru++;
19513 #endif
19514 		goto again;
19515 	}
19516 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
19517 	counter_u64_add(rack_fto_send, 1);
19518 	slot = rack_get_pacing_delay(rack, tp, tot_len, NULL, segsiz, __LINE__);
19519 	rack_start_hpts_timer(rack, tp, cts, slot, tot_len, 0);
19520 #ifdef TCP_ACCOUNTING
19521 	crtsc = get_cyclecount();
19522 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19523 		tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru;
19524 		tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
19525 		tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len + segsiz - 1) / segsiz);
19526 	}
19527 	sched_unpin();
19528 #endif
19529 	return (0);
19530 failed:
19531 	if (m)
19532 		m_free(m);
19533 	rack->r_fast_output = 0;
19534 	return (-1);
19535 }
19536 
19537 static inline void
19538 rack_setup_fast_output(struct tcpcb *tp, struct tcp_rack *rack,
19539 		       struct sockbuf *sb,
19540 		       int len, int orig_len, int segsiz, uint32_t pace_max_seg,
19541 		       bool hw_tls,
19542 		       uint16_t flags)
19543 {
19544 	rack->r_fast_output = 1;
19545 	rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
19546 	rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
19547 	rack->r_ctl.fsb.o_t_len = M_TRAILINGROOM(rack->r_ctl.fsb.m);
19548 	rack->r_ctl.fsb.tcp_flags = flags;
19549 	rack->r_ctl.fsb.left_to_send = orig_len - len;
19550 	if (rack->r_ctl.fsb.left_to_send < pace_max_seg) {
19551 		/* Less than a full sized pace, lets not  */
19552 		rack->r_fast_output = 0;
19553 		return;
19554 	} else {
19555 		/* Round down to the nearest pace_max_seg */
19556 		rack->r_ctl.fsb.left_to_send = rounddown(rack->r_ctl.fsb.left_to_send, pace_max_seg);
19557 	}
19558 	if (hw_tls)
19559 		rack->r_ctl.fsb.hw_tls = 1;
19560 	else
19561 		rack->r_ctl.fsb.hw_tls = 0;
19562 	KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(sb) - (tp->snd_max - tp->snd_una))),
19563 		("rack:%p left_to_send:%u sbavail:%u out:%u",
19564 		 rack, rack->r_ctl.fsb.left_to_send, sbavail(sb),
19565 		 (tp->snd_max - tp->snd_una)));
19566 	if (rack->r_ctl.fsb.left_to_send < segsiz)
19567 		rack->r_fast_output = 0;
19568 	else {
19569 		if (rack->r_ctl.fsb.left_to_send == (sbavail(sb) - (tp->snd_max - tp->snd_una)))
19570 			rack->r_ctl.fsb.rfo_apply_push = 1;
19571 		else
19572 			rack->r_ctl.fsb.rfo_apply_push = 0;
19573 	}
19574 }
19575 
19576 static uint32_t
19577 rack_get_hpts_pacing_min_for_bw(struct tcp_rack *rack, int32_t segsiz)
19578 {
19579 	uint64_t min_time;
19580 	uint32_t maxlen;
19581 
19582 	min_time = (uint64_t)get_hpts_min_sleep_time();
19583 	maxlen = (uint32_t)((rack->r_ctl.gp_bw * min_time) / (uint64_t)HPTS_USEC_IN_SEC);
19584 	maxlen = roundup(maxlen, segsiz);
19585 	return (maxlen);
19586 }
19587 
19588 static struct rack_sendmap *
19589 rack_check_collapsed(struct tcp_rack *rack, uint32_t cts)
19590 {
19591 	struct rack_sendmap *rsm = NULL;
19592 	int thresh;
19593 
19594 restart:
19595 	rsm = tqhash_find(rack->r_ctl.tqh, rack->r_ctl.last_collapse_point);
19596 	if ((rsm == NULL) || ((rsm->r_flags & RACK_RWND_COLLAPSED) == 0)) {
19597 		/* Nothing, strange turn off validity  */
19598 		rack->r_collapse_point_valid = 0;
19599 		return (NULL);
19600 	}
19601 	/* Can we send it yet? */
19602 	if (rsm->r_end > (rack->rc_tp->snd_una + rack->rc_tp->snd_wnd)) {
19603 		/*
19604 		 * Receiver window has not grown enough for
19605 		 * the segment to be put on the wire.
19606 		 */
19607 		return (NULL);
19608 	}
19609 	if (rsm->r_flags & RACK_ACKED) {
19610 		/*
19611 		 * It has been sacked, lets move to the
19612 		 * next one if possible.
19613 		 */
19614 		rack->r_ctl.last_collapse_point = rsm->r_end;
19615 		/* Are we done? */
19616 		if (SEQ_GEQ(rack->r_ctl.last_collapse_point,
19617 			    rack->r_ctl.high_collapse_point)) {
19618 			rack->r_collapse_point_valid = 0;
19619 			return (NULL);
19620 		}
19621 		goto restart;
19622 	}
19623 	/* Now has it been long enough ? */
19624 	thresh = rack_calc_thresh_rack(rack, rack_grab_rtt(rack->rc_tp, rack), cts, __LINE__, 1);
19625 	if ((cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])) > thresh) {
19626 		rack_log_collapse(rack, rsm->r_start,
19627 				  (cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])),
19628 				  thresh, __LINE__, 6, rsm->r_flags, rsm);
19629 		return (rsm);
19630 	}
19631 	/* Not enough time */
19632 	rack_log_collapse(rack, rsm->r_start,
19633 			  (cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])),
19634 			  thresh, __LINE__, 7, rsm->r_flags, rsm);
19635 	return (NULL);
19636 }
19637 
19638 static inline void
19639 rack_validate_sizes(struct tcp_rack *rack, int32_t *len, int32_t segsiz, uint32_t pace_max_seg)
19640 {
19641 	if ((rack->full_size_rxt == 0) &&
19642 	    (rack->shape_rxt_to_pacing_min == 0) &&
19643 	    (*len >= segsiz)) {
19644 		*len = segsiz;
19645 	} else if (rack->shape_rxt_to_pacing_min &&
19646 		 rack->gp_ready) {
19647 		/* We use pacing min as shaping len req */
19648 		uint32_t maxlen;
19649 
19650 		maxlen = rack_get_hpts_pacing_min_for_bw(rack, segsiz);
19651 		if (*len > maxlen)
19652 			*len = maxlen;
19653 	} else {
19654 		/*
19655 		 * The else is full_size_rxt is on so send it all
19656 		 * note we do need to check this for exceeding
19657 		 * our max segment size due to the fact that
19658 		 * we do sometimes merge chunks together i.e.
19659 		 * we cannot just assume that we will never have
19660 		 * a chunk greater than pace_max_seg
19661 		 */
19662 		if (*len > pace_max_seg)
19663 			*len = pace_max_seg;
19664 	}
19665 }
19666 
19667 static int
19668 rack_output(struct tcpcb *tp)
19669 {
19670 	struct socket *so;
19671 	uint32_t recwin;
19672 	uint32_t sb_offset, s_moff = 0;
19673 	int32_t len, error = 0;
19674 	uint16_t flags;
19675 	struct mbuf *m, *s_mb = NULL;
19676 	struct mbuf *mb;
19677 	uint32_t if_hw_tsomaxsegcount = 0;
19678 	uint32_t if_hw_tsomaxsegsize;
19679 	int32_t segsiz, minseg;
19680 	long tot_len_this_send = 0;
19681 #ifdef INET
19682 	struct ip *ip = NULL;
19683 #endif
19684 	struct udphdr *udp = NULL;
19685 	struct tcp_rack *rack;
19686 	struct tcphdr *th;
19687 	uint8_t pass = 0;
19688 	uint8_t mark = 0;
19689 	uint8_t check_done = 0;
19690 	uint8_t wanted_cookie = 0;
19691 	u_char opt[TCP_MAXOLEN];
19692 	unsigned ipoptlen, optlen, hdrlen, ulen=0;
19693 	uint32_t rack_seq;
19694 
19695 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
19696 	unsigned ipsec_optlen = 0;
19697 
19698 #endif
19699 	int32_t idle, sendalot;
19700 	uint32_t tot_idle;
19701 	int32_t sub_from_prr = 0;
19702 	volatile int32_t sack_rxmit;
19703 	struct rack_sendmap *rsm = NULL;
19704 	int32_t tso, mtu;
19705 	struct tcpopt to;
19706 	int32_t slot = 0;
19707 	int32_t sup_rack = 0;
19708 	uint32_t cts, ms_cts, delayed, early;
19709 	uint32_t add_flag = RACK_SENT_SP;
19710 	/* The doing_tlp flag will be set by the actual rack_timeout_tlp() */
19711 	uint8_t doing_tlp = 0;
19712 	uint32_t cwnd_to_use, pace_max_seg;
19713 	int32_t do_a_prefetch = 0;
19714 	int32_t prefetch_rsm = 0;
19715 	int32_t orig_len = 0;
19716 	struct timeval tv;
19717 	int32_t prefetch_so_done = 0;
19718 	struct tcp_log_buffer *lgb;
19719 	struct inpcb *inp = tptoinpcb(tp);
19720 	struct sockbuf *sb;
19721 	uint64_t ts_val = 0;
19722 #ifdef TCP_ACCOUNTING
19723 	uint64_t crtsc;
19724 #endif
19725 #ifdef INET6
19726 	struct ip6_hdr *ip6 = NULL;
19727 	int32_t isipv6;
19728 #endif
19729 	bool hpts_calling, hw_tls = false;
19730 
19731 	NET_EPOCH_ASSERT();
19732 	INP_WLOCK_ASSERT(inp);
19733 
19734 	/* setup and take the cache hits here */
19735 	rack = (struct tcp_rack *)tp->t_fb_ptr;
19736 #ifdef TCP_ACCOUNTING
19737 	sched_pin();
19738 	ts_val = get_cyclecount();
19739 #endif
19740 	hpts_calling = !!(tp->t_flags2 & TF2_HPTS_CALLS);
19741 	tp->t_flags2 &= ~TF2_HPTS_CALLS;
19742 #ifdef TCP_OFFLOAD
19743 	if (tp->t_flags & TF_TOE) {
19744 #ifdef TCP_ACCOUNTING
19745 		sched_unpin();
19746 #endif
19747 		return (tcp_offload_output(tp));
19748 	}
19749 #endif
19750 	if (rack->rack_deferred_inited == 0) {
19751 		/*
19752 		 * If we are the connecting socket we will
19753 		 * hit rack_init() when no sequence numbers
19754 		 * are setup. This makes it so we must defer
19755 		 * some initialization. Call that now.
19756 		 */
19757 		rack_deferred_init(tp, rack);
19758 	}
19759 	/*
19760 	 * For TFO connections in SYN_RECEIVED, only allow the initial
19761 	 * SYN|ACK and those sent by the retransmit timer.
19762 	 */
19763 	if ((tp->t_flags & TF_FASTOPEN) &&
19764 	    (tp->t_state == TCPS_SYN_RECEIVED) &&
19765 	    SEQ_GT(tp->snd_max, tp->snd_una) &&    /* initial SYN|ACK sent */
19766 	    (rack->r_ctl.rc_resend == NULL)) {         /* not a retransmit */
19767 #ifdef TCP_ACCOUNTING
19768 		sched_unpin();
19769 #endif
19770 		return (0);
19771 	}
19772 #ifdef INET6
19773 	if (rack->r_state) {
19774 		/* Use the cache line loaded if possible */
19775 		isipv6 = rack->r_is_v6;
19776 	} else {
19777 		isipv6 = (rack->rc_inp->inp_vflag & INP_IPV6) != 0;
19778 	}
19779 #endif
19780 	early = 0;
19781 	cts = tcp_get_usecs(&tv);
19782 	ms_cts = tcp_tv_to_mssectick(&tv);
19783 	if (((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) &&
19784 	    tcp_in_hpts(rack->rc_tp)) {
19785 		/*
19786 		 * We are on the hpts for some timer but not hptsi output.
19787 		 * Remove from the hpts unconditionally.
19788 		 */
19789 		rack_timer_cancel(tp, rack, cts, __LINE__);
19790 	}
19791 	/* Are we pacing and late? */
19792 	if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
19793 	    TSTMP_GEQ(cts, rack->r_ctl.rc_last_output_to)) {
19794 		/* We are delayed */
19795 		delayed = cts - rack->r_ctl.rc_last_output_to;
19796 	} else {
19797 		delayed = 0;
19798 	}
19799 	/* Do the timers, which may override the pacer */
19800 	if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
19801 		int retval;
19802 
19803 		retval = rack_process_timers(tp, rack, cts, hpts_calling,
19804 					     &doing_tlp);
19805 		if (retval != 0) {
19806 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_ATIMER], 1);
19807 #ifdef TCP_ACCOUNTING
19808 			sched_unpin();
19809 #endif
19810 			/*
19811 			 * If timers want tcp_drop(), then pass error out,
19812 			 * otherwise suppress it.
19813 			 */
19814 			return (retval < 0 ? retval : 0);
19815 		}
19816 	}
19817 	if (rack->rc_in_persist) {
19818 		if (tcp_in_hpts(rack->rc_tp) == 0) {
19819 			/* Timer is not running */
19820 			rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
19821 		}
19822 #ifdef TCP_ACCOUNTING
19823 		sched_unpin();
19824 #endif
19825 		return (0);
19826 	}
19827 	if ((rack->rc_ack_required == 1) &&
19828 	    (rack->r_timer_override == 0)){
19829 		/* A timeout occurred and no ack has arrived */
19830 		if (tcp_in_hpts(rack->rc_tp) == 0) {
19831 			/* Timer is not running */
19832 			rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
19833 		}
19834 #ifdef TCP_ACCOUNTING
19835 		sched_unpin();
19836 #endif
19837 		return (0);
19838 	}
19839 	if ((rack->r_timer_override) ||
19840 	    (rack->rc_ack_can_sendout_data) ||
19841 	    (delayed) ||
19842 	    (tp->t_state < TCPS_ESTABLISHED)) {
19843 		rack->rc_ack_can_sendout_data = 0;
19844 		if (tcp_in_hpts(rack->rc_tp))
19845 			tcp_hpts_remove(rack->rc_tp);
19846 	} else if (tcp_in_hpts(rack->rc_tp)) {
19847 		/*
19848 		 * On the hpts you can't pass even if ACKNOW is on, we will
19849 		 * when the hpts fires.
19850 		 */
19851 #ifdef TCP_ACCOUNTING
19852 		crtsc = get_cyclecount();
19853 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19854 			tp->tcp_proc_time[SND_BLOCKED] += (crtsc - ts_val);
19855 			tp->tcp_cnt_counters[SND_BLOCKED]++;
19856 		}
19857 		sched_unpin();
19858 #endif
19859 		counter_u64_add(rack_out_size[TCP_MSS_ACCT_INPACE], 1);
19860 		return (0);
19861 	}
19862 	/* Finish out both pacing early and late accounting */
19863 	if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
19864 	    TSTMP_GT(rack->r_ctl.rc_last_output_to, cts)) {
19865 		early = rack->r_ctl.rc_last_output_to - cts;
19866 	} else
19867 		early = 0;
19868 	if (delayed && (rack->rc_always_pace == 1)) {
19869 		rack->r_ctl.rc_agg_delayed += delayed;
19870 		rack->r_late = 1;
19871 	} else if (early && (rack->rc_always_pace == 1)) {
19872 		rack->r_ctl.rc_agg_early += early;
19873 		rack->r_early = 1;
19874 	} else if (rack->rc_always_pace == 0) {
19875 		/* Non-paced we are not late */
19876 		rack->r_ctl.rc_agg_delayed = rack->r_ctl.rc_agg_early = 0;
19877 		rack->r_early = rack->r_late = 0;
19878 	}
19879 	/* Now that early/late accounting is done turn off the flag */
19880 	rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
19881 	rack->r_wanted_output = 0;
19882 	rack->r_timer_override = 0;
19883 	if ((tp->t_state != rack->r_state) &&
19884 	    TCPS_HAVEESTABLISHED(tp->t_state)) {
19885 		rack_set_state(tp, rack);
19886 	}
19887 	if ((rack->r_fast_output) &&
19888 	    (doing_tlp == 0) &&
19889 	    (tp->rcv_numsacks == 0)) {
19890 		int ret;
19891 
19892 		error = 0;
19893 		ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error);
19894 		if (ret >= 0)
19895 			return(ret);
19896 		else if (error) {
19897 			inp = rack->rc_inp;
19898 			so = inp->inp_socket;
19899 			sb = &so->so_snd;
19900 			goto nomore;
19901 		}
19902 	}
19903 	inp = rack->rc_inp;
19904 	/*
19905 	 * For TFO connections in SYN_SENT or SYN_RECEIVED,
19906 	 * only allow the initial SYN or SYN|ACK and those sent
19907 	 * by the retransmit timer.
19908 	 */
19909 	if ((tp->t_flags & TF_FASTOPEN) &&
19910 	    ((tp->t_state == TCPS_SYN_RECEIVED) ||
19911 	     (tp->t_state == TCPS_SYN_SENT)) &&
19912 	    SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */
19913 	    (tp->t_rxtshift == 0)) {              /* not a retransmit */
19914 		rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
19915 #ifdef TCP_ACCOUNTING
19916 		sched_unpin();
19917 #endif
19918 		return (0);
19919 	}
19920 	/*
19921 	 * Determine length of data that should be transmitted, and flags
19922 	 * that will be used. If there is some data or critical controls
19923 	 * (SYN, RST) to send, then transmit; otherwise, investigate
19924 	 * further.
19925 	 */
19926 	idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
19927 	if (tp->t_idle_reduce) {
19928 		if (idle && (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur))
19929 			rack_cc_after_idle(rack, tp);
19930 	}
19931 	tp->t_flags &= ~TF_LASTIDLE;
19932 	if (idle) {
19933 		if (tp->t_flags & TF_MORETOCOME) {
19934 			tp->t_flags |= TF_LASTIDLE;
19935 			idle = 0;
19936 		}
19937 	}
19938 	if ((tp->snd_una == tp->snd_max) &&
19939 	    rack->r_ctl.rc_went_idle_time &&
19940 	    (cts > rack->r_ctl.rc_went_idle_time)) {
19941 		tot_idle = (cts - rack->r_ctl.rc_went_idle_time);
19942 		if (tot_idle > rack_min_probertt_hold) {
19943 			/* Count as a probe rtt */
19944 			if (rack->in_probe_rtt == 0) {
19945 				rack->r_ctl.rc_lower_rtt_us_cts = cts;
19946 				rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts;
19947 				rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts;
19948 				rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts;
19949 			} else {
19950 				rack_exit_probertt(rack, cts);
19951 			}
19952 		}
19953 	} else
19954 		tot_idle = 0;
19955 	if (rack_use_fsb &&
19956 	    (rack->r_ctl.fsb.tcp_ip_hdr) &&
19957 	    (rack->r_fsb_inited == 0) &&
19958 	    (rack->r_state != TCPS_CLOSED))
19959 		rack_init_fsb_block(tp, rack, tcp_outflags[tp->t_state]);
19960 	if (rack->rc_sendvars_notset == 1) {
19961 		rack->rc_sendvars_notset = 0;
19962 		/*
19963 		 * Make sure any TCP timers (keep-alive) is not running.
19964 		 */
19965 		tcp_timer_stop(tp);
19966 	}
19967 	if ((rack->rack_no_prr == 1) &&
19968 	    (rack->rc_always_pace == 0)) {
19969 		/*
19970 		 * Sanity check before sending, if we have
19971 		 * no-pacing enabled and prr is turned off that
19972 		 * is a logistics error. Correct this by turnning
19973 		 * prr back on. A user *must* set some form of
19974 		 * pacing in order to turn PRR off. We do this
19975 		 * in the output path so that we can avoid socket
19976 		 * option ordering issues that would occur if we
19977 		 * tried to do it while setting rack_no_prr on.
19978 		 */
19979 		rack->rack_no_prr = 0;
19980 	}
19981 	if ((rack->pcm_enabled == 1) &&
19982 	    (rack->pcm_needed == 0) &&
19983 	    (tot_idle > 0)) {
19984 		/*
19985 		 * We have been idle some micro seconds. We need
19986 		 * to factor this in to see if a PCM is needed.
19987 		 */
19988 		uint32_t rtts_idle, rnds;
19989 
19990 		if (tp->t_srtt)
19991 			rtts_idle = tot_idle / tp->t_srtt;
19992 		else
19993 			rtts_idle = 0;
19994 		rnds = rack->r_ctl.current_round - rack->r_ctl.last_pcm_round;
19995 		rack->r_ctl.pcm_idle_rounds += rtts_idle;
19996 		if ((rnds + rack->r_ctl.pcm_idle_rounds)  >= rack_pcm_every_n_rounds) {
19997 			rack->pcm_needed = 1;
19998 			rack_log_pcm(rack, 8, rack->r_ctl.last_pcm_round, rtts_idle, rack->r_ctl.current_round );
19999 		}
20000 	}
20001 again:
20002 	sendalot = 0;
20003 	cts = tcp_get_usecs(&tv);
20004 	ms_cts = tcp_tv_to_mssectick(&tv);
20005 	tso = 0;
20006 	mtu = 0;
20007 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
20008 	minseg = segsiz;
20009 	if (rack->r_ctl.rc_pace_max_segs == 0)
20010 		pace_max_seg = rack->rc_user_set_max_segs * segsiz;
20011 	else
20012 		pace_max_seg = rack->r_ctl.rc_pace_max_segs;
20013 	if (TCPS_HAVEESTABLISHED(tp->t_state) &&
20014 	    (rack->r_ctl.pcm_max_seg == 0)) {
20015 		/*
20016 		 * We set in our first send so we know that the ctf_fixed_maxseg
20017 		 * has been fully set. If we do it in rack_init() we most likely
20018 		 * see 512 bytes so we end up at 5120, not desirable.
20019 		 */
20020 		rack->r_ctl.pcm_max_seg = rc_init_window(rack);
20021 		if (rack->r_ctl.pcm_max_seg < (ctf_fixed_maxseg(tp) * 10)) {
20022 			/*
20023 			 * Assure our initial PCM probe is at least 10 MSS.
20024 			 */
20025 			rack->r_ctl.pcm_max_seg = ctf_fixed_maxseg(tp) * 10;
20026 		}
20027 	}
20028 	if ((rack->r_ctl.pcm_max_seg != 0)  && (rack->pcm_needed == 1)) {
20029 		uint32_t rw_avail, cwa;
20030 
20031 		if (tp->snd_wnd > ctf_outstanding(tp))
20032 			rw_avail = tp->snd_wnd - ctf_outstanding(tp);
20033 		else
20034 			rw_avail = 0;
20035 		if (tp->snd_cwnd > ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked))
20036 			cwa = tp->snd_cwnd -ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
20037 		else
20038 			cwa = 0;
20039 		if ((cwa >= rack->r_ctl.pcm_max_seg) &&
20040 		    (rw_avail > rack->r_ctl.pcm_max_seg)) {
20041 			/* Raise up the max seg for this trip through */
20042 			pace_max_seg = rack->r_ctl.pcm_max_seg;
20043 			/* Disable any fast output */
20044 			rack->r_fast_output = 0;
20045 		}
20046 		if (rack_verbose_logging) {
20047 			rack_log_pcm(rack, 4,
20048 				     cwa, rack->r_ctl.pcm_max_seg, rw_avail);
20049 		}
20050 	}
20051 	sb_offset = tp->snd_max - tp->snd_una;
20052 	cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
20053 	flags = tcp_outflags[tp->t_state];
20054 	while (rack->rc_free_cnt < rack_free_cache) {
20055 		rsm = rack_alloc(rack);
20056 		if (rsm == NULL) {
20057 			if (hpts_calling)
20058 				/* Retry in a ms */
20059 				slot = (1 * HPTS_USEC_IN_MSEC);
20060 			so = inp->inp_socket;
20061 			sb = &so->so_snd;
20062 			goto just_return_nolock;
20063 		}
20064 		TAILQ_INSERT_TAIL(&rack->r_ctl.rc_free, rsm, r_tnext);
20065 		rack->rc_free_cnt++;
20066 		rsm = NULL;
20067 	}
20068 	sack_rxmit = 0;
20069 	len = 0;
20070 	rsm = NULL;
20071 	if (flags & TH_RST) {
20072 		SOCK_SENDBUF_LOCK(inp->inp_socket);
20073 		so = inp->inp_socket;
20074 		sb = &so->so_snd;
20075 		goto send;
20076 	}
20077 	if (rack->r_ctl.rc_resend) {
20078 		/* Retransmit timer */
20079 		rsm = rack->r_ctl.rc_resend;
20080 		rack->r_ctl.rc_resend = NULL;
20081 		len = rsm->r_end - rsm->r_start;
20082 		sack_rxmit = 1;
20083 		sendalot = 0;
20084 		KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
20085 			("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
20086 			 __func__, __LINE__,
20087 			 rsm->r_start, tp->snd_una, tp, rack, rsm));
20088 		sb_offset = rsm->r_start - tp->snd_una;
20089 		rack_validate_sizes(rack, &len, segsiz, pace_max_seg);
20090 	} else if (rack->r_collapse_point_valid &&
20091 		   ((rsm = rack_check_collapsed(rack, cts)) != NULL)) {
20092 		/*
20093 		 * If an RSM is returned then enough time has passed
20094 		 * for us to retransmit it. Move up the collapse point,
20095 		 * since this rsm has its chance to retransmit now.
20096 		 */
20097 		tcp_trace_point(rack->rc_tp, TCP_TP_COLLAPSED_RXT);
20098 		rack->r_ctl.last_collapse_point = rsm->r_end;
20099 		/* Are we done? */
20100 		if (SEQ_GEQ(rack->r_ctl.last_collapse_point,
20101 			    rack->r_ctl.high_collapse_point))
20102 			rack->r_collapse_point_valid = 0;
20103 		sack_rxmit = 1;
20104 		/* We are not doing a TLP */
20105 		doing_tlp = 0;
20106 		len = rsm->r_end - rsm->r_start;
20107 		sb_offset = rsm->r_start - tp->snd_una;
20108 		sendalot = 0;
20109 		rack_validate_sizes(rack, &len, segsiz, pace_max_seg);
20110 	} else if ((rsm = tcp_rack_output(tp, rack, cts)) != NULL) {
20111 		/* We have a retransmit that takes precedence */
20112 		if ((!IN_FASTRECOVERY(tp->t_flags)) &&
20113 		    ((rsm->r_flags & RACK_MUST_RXT) == 0) &&
20114 		    ((tp->t_flags & TF_WASFRECOVERY) == 0)) {
20115 			/* Enter recovery if not induced by a time-out */
20116 			rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__);
20117 		}
20118 #ifdef INVARIANTS
20119 		if (SEQ_LT(rsm->r_start, tp->snd_una)) {
20120 			panic("Huh, tp:%p rack:%p rsm:%p start:%u < snd_una:%u\n",
20121 			      tp, rack, rsm, rsm->r_start, tp->snd_una);
20122 		}
20123 #endif
20124 		len = rsm->r_end - rsm->r_start;
20125 		KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
20126 			("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
20127 			 __func__, __LINE__,
20128 			 rsm->r_start, tp->snd_una, tp, rack, rsm));
20129 		sb_offset = rsm->r_start - tp->snd_una;
20130 		sendalot = 0;
20131 		rack_validate_sizes(rack, &len, segsiz, pace_max_seg);
20132 		if (len > 0) {
20133 			sack_rxmit = 1;
20134 			KMOD_TCPSTAT_INC(tcps_sack_rexmits);
20135 			KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes,
20136 					 min(len, segsiz));
20137 		}
20138 	} else if (rack->r_ctl.rc_tlpsend) {
20139 		/* Tail loss probe */
20140 		long cwin;
20141 		long tlen;
20142 
20143 		/*
20144 		 * Check if we can do a TLP with a RACK'd packet
20145 		 * this can happen if we are not doing the rack
20146 		 * cheat and we skipped to a TLP and it
20147 		 * went off.
20148 		 */
20149 		rsm = rack->r_ctl.rc_tlpsend;
20150 		/* We are doing a TLP make sure the flag is preent */
20151 		rsm->r_flags |= RACK_TLP;
20152 		rack->r_ctl.rc_tlpsend = NULL;
20153 		sack_rxmit = 1;
20154 		tlen = rsm->r_end - rsm->r_start;
20155 		if (tlen > segsiz)
20156 			tlen = segsiz;
20157 		KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
20158 			("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
20159 			 __func__, __LINE__,
20160 			 rsm->r_start, tp->snd_una, tp, rack, rsm));
20161 		sb_offset = rsm->r_start - tp->snd_una;
20162 		cwin = min(tp->snd_wnd, tlen);
20163 		len = cwin;
20164 	}
20165 	if (rack->r_must_retran &&
20166 	    (doing_tlp == 0) &&
20167 	    (SEQ_GT(tp->snd_max, tp->snd_una)) &&
20168 	    (rsm == NULL)) {
20169 		/*
20170 		 * There are two different ways that we
20171 		 * can get into this block:
20172 		 * a) This is a non-sack connection, we had a time-out
20173 		 *    and thus r_must_retran was set and everything
20174 		 *    left outstanding as been marked for retransmit.
20175 		 * b) The MTU of the path shrank, so that everything
20176 		 *    was marked to be retransmitted with the smaller
20177 		 *    mtu and r_must_retran was set.
20178 		 *
20179 		 * This means that we expect the sendmap (outstanding)
20180 		 * to all be marked must. We can use the tmap to
20181 		 * look at them.
20182 		 *
20183 		 */
20184 		int sendwin, flight;
20185 
20186 		sendwin = min(tp->snd_wnd, tp->snd_cwnd);
20187 		flight = ctf_flight_size(tp, rack->r_ctl.rc_out_at_rto);
20188 		if (flight >= sendwin) {
20189 			/*
20190 			 * We can't send yet.
20191 			 */
20192 			so = inp->inp_socket;
20193 			sb = &so->so_snd;
20194 			goto just_return_nolock;
20195 		}
20196 		/*
20197 		 * This is the case a/b mentioned above. All
20198 		 * outstanding/not-acked should be marked.
20199 		 * We can use the tmap to find them.
20200 		 */
20201 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
20202 		if (rsm == NULL) {
20203 			/* TSNH */
20204 			rack->r_must_retran = 0;
20205 			rack->r_ctl.rc_out_at_rto = 0;
20206 			so = inp->inp_socket;
20207 			sb = &so->so_snd;
20208 			goto just_return_nolock;
20209 		}
20210 		if ((rsm->r_flags & RACK_MUST_RXT) == 0) {
20211 			/*
20212 			 * The first one does not have the flag, did we collapse
20213 			 * further up in our list?
20214 			 */
20215 			rack->r_must_retran = 0;
20216 			rack->r_ctl.rc_out_at_rto = 0;
20217 			rsm = NULL;
20218 			sack_rxmit = 0;
20219 		} else {
20220 			sack_rxmit = 1;
20221 			len = rsm->r_end - rsm->r_start;
20222 			sb_offset = rsm->r_start - tp->snd_una;
20223 			sendalot = 0;
20224 			if ((rack->full_size_rxt == 0) &&
20225 			    (rack->shape_rxt_to_pacing_min == 0) &&
20226 			    (len >= segsiz))
20227 				len = segsiz;
20228 			else if (rack->shape_rxt_to_pacing_min &&
20229 				 rack->gp_ready) {
20230 				/* We use pacing min as shaping len req */
20231 				uint32_t maxlen;
20232 
20233 				maxlen = rack_get_hpts_pacing_min_for_bw(rack, segsiz);
20234 				if (len > maxlen)
20235 					len = maxlen;
20236 			}
20237 			/*
20238 			 * Delay removing the flag RACK_MUST_RXT so
20239 			 * that the fastpath for retransmit will
20240 			 * work with this rsm.
20241 			 */
20242 		}
20243 	}
20244 	/*
20245 	 * Enforce a connection sendmap count limit if set
20246 	 * as long as we are not retransmiting.
20247 	 */
20248 	if ((rsm == NULL) &&
20249 	    (V_tcp_map_entries_limit > 0) &&
20250 	    (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
20251 		counter_u64_add(rack_to_alloc_limited, 1);
20252 		if (!rack->alloc_limit_reported) {
20253 			rack->alloc_limit_reported = 1;
20254 			counter_u64_add(rack_alloc_limited_conns, 1);
20255 		}
20256 		so = inp->inp_socket;
20257 		sb = &so->so_snd;
20258 		goto just_return_nolock;
20259 	}
20260 	if (rsm && (rsm->r_flags & RACK_HAS_FIN)) {
20261 		/* we are retransmitting the fin */
20262 		len--;
20263 		if (len) {
20264 			/*
20265 			 * When retransmitting data do *not* include the
20266 			 * FIN. This could happen from a TLP probe.
20267 			 */
20268 			flags &= ~TH_FIN;
20269 		}
20270 	}
20271 	if (rsm && rack->r_fsb_inited &&
20272 	    rack_use_rsm_rfo &&
20273 	    ((rsm->r_flags & RACK_HAS_FIN) == 0)) {
20274 		int ret;
20275 
20276 		ret = rack_fast_rsm_output(tp, rack, rsm, ts_val, cts, ms_cts, &tv, len, doing_tlp);
20277 		if (ret == 0)
20278 			return (0);
20279 	}
20280 	so = inp->inp_socket;
20281 	sb = &so->so_snd;
20282 	if (do_a_prefetch == 0) {
20283 		kern_prefetch(sb, &do_a_prefetch);
20284 		do_a_prefetch = 1;
20285 	}
20286 #ifdef NETFLIX_SHARED_CWND
20287 	if ((tp->t_flags2 & TF2_TCP_SCWND_ALLOWED) &&
20288 	    rack->rack_enable_scwnd) {
20289 		/* We are doing cwnd sharing */
20290 		if (rack->gp_ready &&
20291 		    (rack->rack_attempted_scwnd == 0) &&
20292 		    (rack->r_ctl.rc_scw == NULL) &&
20293 		    tp->t_lib) {
20294 			/* The pcbid is in, lets make an attempt */
20295 			counter_u64_add(rack_try_scwnd, 1);
20296 			rack->rack_attempted_scwnd = 1;
20297 			rack->r_ctl.rc_scw = tcp_shared_cwnd_alloc(tp,
20298 								   &rack->r_ctl.rc_scw_index,
20299 								   segsiz);
20300 		}
20301 		if (rack->r_ctl.rc_scw &&
20302 		    (rack->rack_scwnd_is_idle == 1) &&
20303 		    sbavail(&so->so_snd)) {
20304 			/* we are no longer out of data */
20305 			tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
20306 			rack->rack_scwnd_is_idle = 0;
20307 		}
20308 		if (rack->r_ctl.rc_scw) {
20309 			/* First lets update and get the cwnd */
20310 			rack->r_ctl.cwnd_to_use = cwnd_to_use = tcp_shared_cwnd_update(rack->r_ctl.rc_scw,
20311 										       rack->r_ctl.rc_scw_index,
20312 										       tp->snd_cwnd, tp->snd_wnd, segsiz);
20313 		}
20314 	}
20315 #endif
20316 	/*
20317 	 * Get standard flags, and add SYN or FIN if requested by 'hidden'
20318 	 * state flags.
20319 	 */
20320 	if (tp->t_flags & TF_NEEDFIN)
20321 		flags |= TH_FIN;
20322 	if (tp->t_flags & TF_NEEDSYN)
20323 		flags |= TH_SYN;
20324 	if ((sack_rxmit == 0) && (prefetch_rsm == 0)) {
20325 		void *end_rsm;
20326 		end_rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
20327 		if (end_rsm)
20328 			kern_prefetch(end_rsm, &prefetch_rsm);
20329 		prefetch_rsm = 1;
20330 	}
20331 	SOCK_SENDBUF_LOCK(so);
20332 	if ((sack_rxmit == 0) &&
20333 	    (TCPS_HAVEESTABLISHED(tp->t_state) ||
20334 	    (tp->t_flags & TF_FASTOPEN))) {
20335 		/*
20336 		 * We are not retransmitting (sack_rxmit is 0) so we
20337 		 * are sending new data. This is always based on snd_max.
20338 		 * Now in theory snd_max may be equal to snd_una, if so
20339 		 * then nothing is outstanding and the offset would be 0.
20340 		 */
20341 		uint32_t avail;
20342 
20343 		avail = sbavail(sb);
20344 		if (SEQ_GT(tp->snd_max, tp->snd_una) && avail)
20345 			sb_offset = tp->snd_max - tp->snd_una;
20346 		else
20347 			sb_offset = 0;
20348 		if ((IN_FASTRECOVERY(tp->t_flags) == 0) || rack->rack_no_prr) {
20349 			if (rack->r_ctl.rc_tlp_new_data) {
20350 				/* TLP is forcing out new data */
20351 				if (rack->r_ctl.rc_tlp_new_data > (uint32_t) (avail - sb_offset)) {
20352 					rack->r_ctl.rc_tlp_new_data = (uint32_t) (avail - sb_offset);
20353 				}
20354 				if ((rack->r_ctl.rc_tlp_new_data + sb_offset) > tp->snd_wnd) {
20355 					if (tp->snd_wnd > sb_offset)
20356 						len = tp->snd_wnd - sb_offset;
20357 					else
20358 						len = 0;
20359 				} else {
20360 					len = rack->r_ctl.rc_tlp_new_data;
20361 				}
20362 				rack->r_ctl.rc_tlp_new_data = 0;
20363 			}  else {
20364 				len = rack_what_can_we_send(tp, rack, cwnd_to_use, avail, sb_offset);
20365 			}
20366 			if ((rack->r_ctl.crte == NULL) &&
20367 			    IN_FASTRECOVERY(tp->t_flags) &&
20368 			    (rack->full_size_rxt == 0) &&
20369 			    (rack->shape_rxt_to_pacing_min == 0) &&
20370 			    (len > segsiz)) {
20371 				/*
20372 				 * For prr=off, we need to send only 1 MSS
20373 				 * at a time. We do this because another sack could
20374 				 * be arriving that causes us to send retransmits and
20375 				 * we don't want to be on a long pace due to a larger send
20376 				 * that keeps us from sending out the retransmit.
20377 				 */
20378 				len = segsiz;
20379 			} else if (rack->shape_rxt_to_pacing_min &&
20380 				   rack->gp_ready) {
20381 				/* We use pacing min as shaping len req */
20382 				uint32_t maxlen;
20383 
20384 				maxlen = rack_get_hpts_pacing_min_for_bw(rack, segsiz);
20385 				if (len > maxlen)
20386 					len = maxlen;
20387 			}/* The else is full_size_rxt is on so send it all */
20388 		} else {
20389 			uint32_t outstanding;
20390 			/*
20391 			 * We are inside of a Fast recovery episode, this
20392 			 * is caused by a SACK or 3 dup acks. At this point
20393 			 * we have sent all the retransmissions and we rely
20394 			 * on PRR to dictate what we will send in the form of
20395 			 * new data.
20396 			 */
20397 
20398 			outstanding = tp->snd_max - tp->snd_una;
20399 			if ((rack->r_ctl.rc_prr_sndcnt + outstanding) > tp->snd_wnd) {
20400 				if (tp->snd_wnd > outstanding) {
20401 					len = tp->snd_wnd - outstanding;
20402 					/* Check to see if we have the data */
20403 					if ((sb_offset + len) > avail) {
20404 						/* It does not all fit */
20405 						if (avail > sb_offset)
20406 							len = avail - sb_offset;
20407 						else
20408 							len = 0;
20409 					}
20410 				} else {
20411 					len = 0;
20412 				}
20413 			} else if (avail > sb_offset) {
20414 				len = avail - sb_offset;
20415 			} else {
20416 				len = 0;
20417 			}
20418 			if (len > 0) {
20419 				if (len > rack->r_ctl.rc_prr_sndcnt) {
20420 					len = rack->r_ctl.rc_prr_sndcnt;
20421 				}
20422 				if (len > 0) {
20423 					sub_from_prr = 1;
20424 				}
20425 			}
20426 			if (len > segsiz) {
20427 				/*
20428 				 * We should never send more than a MSS when
20429 				 * retransmitting or sending new data in prr
20430 				 * mode unless the override flag is on. Most
20431 				 * likely the PRR algorithm is not going to
20432 				 * let us send a lot as well :-)
20433 				 */
20434 				if (rack->r_ctl.rc_prr_sendalot == 0) {
20435 					len = segsiz;
20436 				}
20437 			} else if (len < segsiz) {
20438 				/*
20439 				 * Do we send any? The idea here is if the
20440 				 * send empty's the socket buffer we want to
20441 				 * do it. However if not then lets just wait
20442 				 * for our prr_sndcnt to get bigger.
20443 				 */
20444 				long leftinsb;
20445 
20446 				leftinsb = sbavail(sb) - sb_offset;
20447 				if (leftinsb > len) {
20448 					/* This send does not empty the sb */
20449 					len = 0;
20450 				}
20451 			}
20452 		}
20453 	} else if (!TCPS_HAVEESTABLISHED(tp->t_state)) {
20454 		/*
20455 		 * If you have not established
20456 		 * and are not doing FAST OPEN
20457 		 * no data please.
20458 		 */
20459 		if ((sack_rxmit == 0) &&
20460 		    !(tp->t_flags & TF_FASTOPEN)) {
20461 			len = 0;
20462 			sb_offset = 0;
20463 		}
20464 	}
20465 	if (prefetch_so_done == 0) {
20466 		kern_prefetch(so, &prefetch_so_done);
20467 		prefetch_so_done = 1;
20468 	}
20469 	orig_len = len;
20470 	/*
20471 	 * Lop off SYN bit if it has already been sent.  However, if this is
20472 	 * SYN-SENT state and if segment contains data and if we don't know
20473 	 * that foreign host supports TAO, suppress sending segment.
20474 	 */
20475 	if ((flags & TH_SYN) &&
20476 	    SEQ_GT(tp->snd_max, tp->snd_una) &&
20477 	    ((sack_rxmit == 0) &&
20478 	     (tp->t_rxtshift == 0))) {
20479 		/*
20480 		 * When sending additional segments following a TFO SYN|ACK,
20481 		 * do not include the SYN bit.
20482 		 */
20483 		if ((tp->t_flags & TF_FASTOPEN) &&
20484 		    (tp->t_state == TCPS_SYN_RECEIVED))
20485 			flags &= ~TH_SYN;
20486 	}
20487 	/*
20488 	 * Be careful not to send data and/or FIN on SYN segments. This
20489 	 * measure is needed to prevent interoperability problems with not
20490 	 * fully conformant TCP implementations.
20491 	 */
20492 	if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
20493 		len = 0;
20494 		flags &= ~TH_FIN;
20495 	}
20496 	/*
20497 	 * On TFO sockets, ensure no data is sent in the following cases:
20498 	 *
20499 	 *  - When retransmitting SYN|ACK on a passively-created socket
20500 	 *
20501 	 *  - When retransmitting SYN on an actively created socket
20502 	 *
20503 	 *  - When sending a zero-length cookie (cookie request) on an
20504 	 *    actively created socket
20505 	 *
20506 	 *  - When the socket is in the CLOSED state (RST is being sent)
20507 	 */
20508 	if ((tp->t_flags & TF_FASTOPEN) &&
20509 	    (((flags & TH_SYN) && (tp->t_rxtshift > 0)) ||
20510 	     ((tp->t_state == TCPS_SYN_SENT) &&
20511 	      (tp->t_tfo_client_cookie_len == 0)) ||
20512 	     (flags & TH_RST))) {
20513 		sack_rxmit = 0;
20514 		len = 0;
20515 	}
20516 	/* Without fast-open there should never be data sent on a SYN */
20517 	if ((flags & TH_SYN) && !(tp->t_flags & TF_FASTOPEN)) {
20518 		len = 0;
20519 	}
20520 	if ((len > segsiz) && (tcp_dsack_block_exists(tp))) {
20521 		/* We only send 1 MSS if we have a DSACK block */
20522 		add_flag |= RACK_SENT_W_DSACK;
20523 		len = segsiz;
20524 	}
20525 	if (len <= 0) {
20526 		/*
20527 		 * We have nothing to send, or the window shrank, or
20528 		 * is closed, do we need to go into persists?
20529 		 */
20530 		len = 0;
20531 		if ((tp->snd_wnd == 0) &&
20532 		    (TCPS_HAVEESTABLISHED(tp->t_state)) &&
20533 		    (tp->snd_una == tp->snd_max) &&
20534 		    (sb_offset < (int)sbavail(sb))) {
20535 			rack_enter_persist(tp, rack, cts, tp->snd_una);
20536 		}
20537 	} else if ((rsm == NULL) &&
20538 		   (doing_tlp == 0) &&
20539 		   (len < pace_max_seg)) {
20540 		/*
20541 		 * We are not sending a maximum sized segment for
20542 		 * some reason. Should we not send anything (think
20543 		 * sws or persists)?
20544 		 */
20545 		if ((tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg)) &&
20546 		    (TCPS_HAVEESTABLISHED(tp->t_state)) &&
20547 		    (len < minseg) &&
20548 		    (len < (int)(sbavail(sb) - sb_offset))) {
20549 			/*
20550 			 * Here the rwnd is less than
20551 			 * the minimum pacing size, this is not a retransmit,
20552 			 * we are established and
20553 			 * the send is not the last in the socket buffer
20554 			 * we send nothing, and we may enter persists
20555 			 * if nothing is outstanding.
20556 			 */
20557 			len = 0;
20558 			if (tp->snd_max == tp->snd_una) {
20559 				/*
20560 				 * Nothing out we can
20561 				 * go into persists.
20562 				 */
20563 				rack_enter_persist(tp, rack, cts, tp->snd_una);
20564 			}
20565 		} else if ((cwnd_to_use >= max(minseg, (segsiz * 4))) &&
20566 			   (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) &&
20567 			   (len < (int)(sbavail(sb) - sb_offset)) &&
20568 			   (len < minseg)) {
20569 			/*
20570 			 * Here we are not retransmitting, and
20571 			 * the cwnd is not so small that we could
20572 			 * not send at least a min size (rxt timer
20573 			 * not having gone off), We have 2 segments or
20574 			 * more already in flight, its not the tail end
20575 			 * of the socket buffer  and the cwnd is blocking
20576 			 * us from sending out a minimum pacing segment size.
20577 			 * Lets not send anything.
20578 			 */
20579 			len = 0;
20580 		} else if (((tp->snd_wnd - ctf_outstanding(tp)) <
20581 			    min((rack->r_ctl.rc_high_rwnd/2), minseg)) &&
20582 			   (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) &&
20583 			   (len < (int)(sbavail(sb) - sb_offset)) &&
20584 			   (TCPS_HAVEESTABLISHED(tp->t_state))) {
20585 			/*
20586 			 * Here we have a send window but we have
20587 			 * filled it up and we can't send another pacing segment.
20588 			 * We also have in flight more than 2 segments
20589 			 * and we are not completing the sb i.e. we allow
20590 			 * the last bytes of the sb to go out even if
20591 			 * its not a full pacing segment.
20592 			 */
20593 			len = 0;
20594 		} else if ((rack->r_ctl.crte != NULL) &&
20595 			   (tp->snd_wnd >= (pace_max_seg * max(1, rack_hw_rwnd_factor))) &&
20596 			   (cwnd_to_use >= (pace_max_seg + (4 * segsiz))) &&
20597 			   (ctf_flight_size(tp, rack->r_ctl.rc_sacked) >= (2 * segsiz)) &&
20598 			   (len < (int)(sbavail(sb) - sb_offset))) {
20599 			/*
20600 			 * Here we are doing hardware pacing, this is not a TLP,
20601 			 * we are not sending a pace max segment size, there is rwnd
20602 			 * room to send at least N pace_max_seg, the cwnd is greater
20603 			 * than or equal to a full pacing segments plus 4 mss and we have 2 or
20604 			 * more segments in flight and its not the tail of the socket buffer.
20605 			 *
20606 			 * We don't want to send instead we need to get more ack's in to
20607 			 * allow us to send a full pacing segment. Normally, if we are pacing
20608 			 * about the right speed, we should have finished our pacing
20609 			 * send as most of the acks have come back if we are at the
20610 			 * right rate. This is a bit fuzzy since return path delay
20611 			 * can delay the acks, which is why we want to make sure we
20612 			 * have cwnd space to have a bit more than a max pace segments in flight.
20613 			 *
20614 			 * If we have not gotten our acks back we are pacing at too high a
20615 			 * rate delaying will not hurt and will bring our GP estimate down by
20616 			 * injecting the delay. If we don't do this we will send
20617 			 * 2 MSS out in response to the acks being clocked in which
20618 			 * defeats the point of hw-pacing (i.e. to help us get
20619 			 * larger TSO's out).
20620 			 */
20621 			len = 0;
20622 		}
20623 
20624 	}
20625 	/* len will be >= 0 after this point. */
20626 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
20627 	rack_sndbuf_autoscale(rack);
20628 	/*
20629 	 * Decide if we can use TCP Segmentation Offloading (if supported by
20630 	 * hardware).
20631 	 *
20632 	 * TSO may only be used if we are in a pure bulk sending state.  The
20633 	 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP
20634 	 * options prevent using TSO.  With TSO the TCP header is the same
20635 	 * (except for the sequence number) for all generated packets.  This
20636 	 * makes it impossible to transmit any options which vary per
20637 	 * generated segment or packet.
20638 	 *
20639 	 * IPv4 handling has a clear separation of ip options and ip header
20640 	 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does
20641 	 * the right thing below to provide length of just ip options and thus
20642 	 * checking for ipoptlen is enough to decide if ip options are present.
20643 	 */
20644 	ipoptlen = 0;
20645 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
20646 	/*
20647 	 * Pre-calculate here as we save another lookup into the darknesses
20648 	 * of IPsec that way and can actually decide if TSO is ok.
20649 	 */
20650 #ifdef INET6
20651 	if (isipv6 && IPSEC_ENABLED(ipv6))
20652 		ipsec_optlen = IPSEC_HDRSIZE(ipv6, inp);
20653 #ifdef INET
20654 	else
20655 #endif
20656 #endif				/* INET6 */
20657 #ifdef INET
20658 		if (IPSEC_ENABLED(ipv4))
20659 			ipsec_optlen = IPSEC_HDRSIZE(ipv4, inp);
20660 #endif				/* INET */
20661 #endif
20662 
20663 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
20664 	ipoptlen += ipsec_optlen;
20665 #endif
20666 	if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > segsiz &&
20667 	    (tp->t_port == 0) &&
20668 	    ((tp->t_flags & TF_SIGNATURE) == 0) &&
20669 	    sack_rxmit == 0 &&
20670 	    ipoptlen == 0)
20671 		tso = 1;
20672 	{
20673 		uint32_t outstanding __unused;
20674 
20675 		outstanding = tp->snd_max - tp->snd_una;
20676 		if (tp->t_flags & TF_SENTFIN) {
20677 			/*
20678 			 * If we sent a fin, snd_max is 1 higher than
20679 			 * snd_una
20680 			 */
20681 			outstanding--;
20682 		}
20683 		if (sack_rxmit) {
20684 			if ((rsm->r_flags & RACK_HAS_FIN) == 0)
20685 				flags &= ~TH_FIN;
20686 		}
20687 	}
20688 	recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
20689 		      (long)TCP_MAXWIN << tp->rcv_scale);
20690 
20691 	/*
20692 	 * Sender silly window avoidance.   We transmit under the following
20693 	 * conditions when len is non-zero:
20694 	 *
20695 	 * - We have a full segment (or more with TSO) - This is the last
20696 	 * buffer in a write()/send() and we are either idle or running
20697 	 * NODELAY - we've timed out (e.g. persist timer) - we have more
20698 	 * then 1/2 the maximum send window's worth of data (receiver may be
20699 	 * limited the window size) - we need to retransmit
20700 	 */
20701 	if (len) {
20702 		if (len >= segsiz) {
20703 			goto send;
20704 		}
20705 		/*
20706 		 * NOTE! on localhost connections an 'ack' from the remote
20707 		 * end may occur synchronously with the output and cause us
20708 		 * to flush a buffer queued with moretocome.  XXX
20709 		 *
20710 		 */
20711 		if (!(tp->t_flags & TF_MORETOCOME) &&	/* normal case */
20712 		    (idle || (tp->t_flags & TF_NODELAY)) &&
20713 		    ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) &&
20714 		    (tp->t_flags & TF_NOPUSH) == 0) {
20715 			pass = 2;
20716 			goto send;
20717 		}
20718 		if ((tp->snd_una == tp->snd_max) && len) {	/* Nothing outstanding */
20719 			pass = 22;
20720 			goto send;
20721 		}
20722 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) {
20723 			pass = 4;
20724 			goto send;
20725 		}
20726 		if (sack_rxmit) {
20727 			pass = 6;
20728 			goto send;
20729 		}
20730 		if (((tp->snd_wnd - ctf_outstanding(tp)) < segsiz) &&
20731 		    (ctf_outstanding(tp) < (segsiz * 2))) {
20732 			/*
20733 			 * We have less than two MSS outstanding (delayed ack)
20734 			 * and our rwnd will not let us send a full sized
20735 			 * MSS. Lets go ahead and let this small segment
20736 			 * out because we want to try to have at least two
20737 			 * packets inflight to not be caught by delayed ack.
20738 			 */
20739 			pass = 12;
20740 			goto send;
20741 		}
20742 	}
20743 	/*
20744 	 * Sending of standalone window updates.
20745 	 *
20746 	 * Window updates are important when we close our window due to a
20747 	 * full socket buffer and are opening it again after the application
20748 	 * reads data from it.  Once the window has opened again and the
20749 	 * remote end starts to send again the ACK clock takes over and
20750 	 * provides the most current window information.
20751 	 *
20752 	 * We must avoid the silly window syndrome whereas every read from
20753 	 * the receive buffer, no matter how small, causes a window update
20754 	 * to be sent.  We also should avoid sending a flurry of window
20755 	 * updates when the socket buffer had queued a lot of data and the
20756 	 * application is doing small reads.
20757 	 *
20758 	 * Prevent a flurry of pointless window updates by only sending an
20759 	 * update when we can increase the advertized window by more than
20760 	 * 1/4th of the socket buffer capacity.  When the buffer is getting
20761 	 * full or is very small be more aggressive and send an update
20762 	 * whenever we can increase by two mss sized segments. In all other
20763 	 * situations the ACK's to new incoming data will carry further
20764 	 * window increases.
20765 	 *
20766 	 * Don't send an independent window update if a delayed ACK is
20767 	 * pending (it will get piggy-backed on it) or the remote side
20768 	 * already has done a half-close and won't send more data.  Skip
20769 	 * this if the connection is in T/TCP half-open state.
20770 	 */
20771 	if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
20772 	    !(tp->t_flags & TF_DELACK) &&
20773 	    !TCPS_HAVERCVDFIN(tp->t_state)) {
20774 		/*
20775 		 * "adv" is the amount we could increase the window, taking
20776 		 * into account that we are limited by TCP_MAXWIN <<
20777 		 * tp->rcv_scale.
20778 		 */
20779 		int32_t adv;
20780 		int oldwin;
20781 
20782 		adv = recwin;
20783 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
20784 			oldwin = (tp->rcv_adv - tp->rcv_nxt);
20785 			if (adv > oldwin)
20786 				adv -= oldwin;
20787 			else {
20788 				/* We can't increase the window */
20789 				adv = 0;
20790 			}
20791 		} else
20792 			oldwin = 0;
20793 
20794 		/*
20795 		 * If the new window size ends up being the same as or less
20796 		 * than the old size when it is scaled, then don't force
20797 		 * a window update.
20798 		 */
20799 		if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale)
20800 			goto dontupdate;
20801 
20802 		if (adv >= (int32_t)(2 * segsiz) &&
20803 		    (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) ||
20804 		     recwin <= (int32_t)(so->so_rcv.sb_hiwat / 8) ||
20805 		     so->so_rcv.sb_hiwat <= 8 * segsiz)) {
20806 			pass = 7;
20807 			goto send;
20808 		}
20809 		if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat) {
20810 			pass = 23;
20811 			goto send;
20812 		}
20813 	}
20814 dontupdate:
20815 
20816 	/*
20817 	 * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
20818 	 * is also a catch-all for the retransmit timer timeout case.
20819 	 */
20820 	if (tp->t_flags & TF_ACKNOW) {
20821 		pass = 8;
20822 		goto send;
20823 	}
20824 	if (((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) {
20825 		pass = 9;
20826 		goto send;
20827 	}
20828 	/*
20829 	 * If our state indicates that FIN should be sent and we have not
20830 	 * yet done so, then we need to send.
20831 	 */
20832 	if ((flags & TH_FIN) &&
20833 	    (tp->snd_max == tp->snd_una)) {
20834 		pass = 11;
20835 		goto send;
20836 	}
20837 	/*
20838 	 * No reason to send a segment, just return.
20839 	 */
20840 just_return:
20841 	SOCK_SENDBUF_UNLOCK(so);
20842 just_return_nolock:
20843 	{
20844 		int app_limited = CTF_JR_SENT_DATA;
20845 
20846 		if ((tp->t_flags & TF_FASTOPEN) == 0 &&
20847 		    (flags & TH_FIN) &&
20848 		    (len == 0) &&
20849 		    (sbused(sb) == (tp->snd_max - tp->snd_una)) &&
20850 		    ((tp->snd_max - tp->snd_una) <= segsiz)) {
20851 			/*
20852 			 * Ok less than or right at a MSS is
20853 			 * outstanding. The original FreeBSD stack would
20854 			 * have sent a FIN, which can speed things up for
20855 			 * a transactional application doing a MSG_WAITALL.
20856 			 * To speed things up since we do *not* send a FIN
20857 			 * if data is outstanding, we send a "challenge ack".
20858 			 * The idea behind that is instead of having to have
20859 			 * the peer wait for the delayed-ack timer to run off
20860 			 * we send an ack that makes the peer send us an ack.
20861 			 */
20862 			rack_send_ack_challange(rack);
20863 		}
20864 		if (tot_len_this_send > 0) {
20865 			rack->r_ctl.fsb.recwin = recwin;
20866 			slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, NULL, segsiz, __LINE__);
20867 			if ((error == 0) &&
20868 			    rack_use_rfo &&
20869 			    ((flags & (TH_SYN|TH_FIN)) == 0) &&
20870 			    (ipoptlen == 0) &&
20871 			    rack->r_fsb_inited &&
20872 			    TCPS_HAVEESTABLISHED(tp->t_state) &&
20873 			    ((IN_RECOVERY(tp->t_flags)) == 0) &&
20874 			    (rack->r_must_retran == 0) &&
20875 			    ((tp->t_flags & TF_NEEDFIN) == 0) &&
20876 			    (len > 0) && (orig_len > 0) &&
20877 			    (orig_len > len) &&
20878 			    ((orig_len - len) >= segsiz) &&
20879 			    ((optlen == 0) ||
20880 			     ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
20881 				/* We can send at least one more MSS using our fsb */
20882 				rack_setup_fast_output(tp, rack, sb, len, orig_len,
20883 						       segsiz, pace_max_seg, hw_tls, flags);
20884 			} else
20885 				rack->r_fast_output = 0;
20886 			rack_log_fsb(rack, tp, so, flags,
20887 				     ipoptlen, orig_len, len, 0,
20888 				     1, optlen, __LINE__, 1);
20889 			/* Assure when we leave that snd_nxt will point to top */
20890 			if (SEQ_GT(tp->snd_max, tp->snd_nxt))
20891 				tp->snd_nxt = tp->snd_max;
20892 		} else {
20893 			int end_window = 0;
20894 			uint32_t seq = tp->gput_ack;
20895 
20896 			rsm = tqhash_max(rack->r_ctl.tqh);
20897 			if (rsm) {
20898 				/*
20899 				 * Mark the last sent that we just-returned (hinting
20900 				 * that delayed ack may play a role in any rtt measurement).
20901 				 */
20902 				rsm->r_just_ret = 1;
20903 			}
20904 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_JUSTRET], 1);
20905 			rack->r_ctl.rc_agg_delayed = 0;
20906 			rack->r_early = 0;
20907 			rack->r_late = 0;
20908 			rack->r_ctl.rc_agg_early = 0;
20909 			if ((ctf_outstanding(tp) +
20910 			     min(max(segsiz, (rack->r_ctl.rc_high_rwnd/2)),
20911 				 minseg)) >= tp->snd_wnd) {
20912 				/* We are limited by the rwnd */
20913 				app_limited = CTF_JR_RWND_LIMITED;
20914 				if (IN_FASTRECOVERY(tp->t_flags))
20915 					rack->r_ctl.rc_prr_sndcnt = 0;
20916 			} else if (ctf_outstanding(tp) >= sbavail(sb)) {
20917 				/* We are limited by whats available -- app limited */
20918 				app_limited = CTF_JR_APP_LIMITED;
20919 				if (IN_FASTRECOVERY(tp->t_flags))
20920 					rack->r_ctl.rc_prr_sndcnt = 0;
20921 			} else if ((idle == 0) &&
20922 				   ((tp->t_flags & TF_NODELAY) == 0) &&
20923 				   ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) &&
20924 				   (len < segsiz)) {
20925 				/*
20926 				 * No delay is not on and the
20927 				 * user is sending less than 1MSS. This
20928 				 * brings out SWS avoidance so we
20929 				 * don't send. Another app-limited case.
20930 				 */
20931 				app_limited = CTF_JR_APP_LIMITED;
20932 			} else if (tp->t_flags & TF_NOPUSH) {
20933 				/*
20934 				 * The user has requested no push of
20935 				 * the last segment and we are
20936 				 * at the last segment. Another app
20937 				 * limited case.
20938 				 */
20939 				app_limited = CTF_JR_APP_LIMITED;
20940 			} else if ((ctf_outstanding(tp) + minseg) > cwnd_to_use) {
20941 				/* Its the cwnd */
20942 				app_limited = CTF_JR_CWND_LIMITED;
20943 			} else if (IN_FASTRECOVERY(tp->t_flags) &&
20944 				   (rack->rack_no_prr == 0) &&
20945 				   (rack->r_ctl.rc_prr_sndcnt < segsiz)) {
20946 				app_limited = CTF_JR_PRR;
20947 			} else {
20948 				/* Now why here are we not sending? */
20949 #ifdef NOW
20950 #ifdef INVARIANTS
20951 				panic("rack:%p hit JR_ASSESSING case cwnd_to_use:%u?", rack, cwnd_to_use);
20952 #endif
20953 #endif
20954 				app_limited = CTF_JR_ASSESSING;
20955 			}
20956 			/*
20957 			 * App limited in some fashion, for our pacing GP
20958 			 * measurements we don't want any gap (even cwnd).
20959 			 * Close  down the measurement window.
20960 			 */
20961 			if (rack_cwnd_block_ends_measure &&
20962 			    ((app_limited == CTF_JR_CWND_LIMITED) ||
20963 			     (app_limited == CTF_JR_PRR))) {
20964 				/*
20965 				 * The reason we are not sending is
20966 				 * the cwnd (or prr). We have been configured
20967 				 * to end the measurement window in
20968 				 * this case.
20969 				 */
20970 				end_window = 1;
20971 			} else if (rack_rwnd_block_ends_measure &&
20972 				   (app_limited == CTF_JR_RWND_LIMITED)) {
20973 				/*
20974 				 * We are rwnd limited and have been
20975 				 * configured to end the measurement
20976 				 * window in this case.
20977 				 */
20978 				end_window = 1;
20979 			} else if (app_limited == CTF_JR_APP_LIMITED) {
20980 				/*
20981 				 * A true application limited period, we have
20982 				 * ran out of data.
20983 				 */
20984 				end_window = 1;
20985 			} else if (app_limited == CTF_JR_ASSESSING) {
20986 				/*
20987 				 * In the assessing case we hit the end of
20988 				 * the if/else and had no known reason
20989 				 * This will panic us under invariants..
20990 				 *
20991 				 * If we get this out in logs we need to
20992 				 * investagate which reason we missed.
20993 				 */
20994 				end_window = 1;
20995 			}
20996 			if (end_window) {
20997 				uint8_t log = 0;
20998 
20999 				/* Adjust the Gput measurement */
21000 				if ((tp->t_flags & TF_GPUTINPROG) &&
21001 				    SEQ_GT(tp->gput_ack, tp->snd_max)) {
21002 					tp->gput_ack = tp->snd_max;
21003 					if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) {
21004 						/*
21005 						 * There is not enough to measure.
21006 						 */
21007 						tp->t_flags &= ~TF_GPUTINPROG;
21008 						rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
21009 									   rack->r_ctl.rc_gp_srtt /*flex1*/,
21010 									   tp->gput_seq,
21011 									   0, 0, 18, __LINE__, NULL, 0);
21012 					} else
21013 						log = 1;
21014 				}
21015 				/* Mark the last packet has app limited */
21016 				rsm = tqhash_max(rack->r_ctl.tqh);
21017 				if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) {
21018 					if (rack->r_ctl.rc_app_limited_cnt == 0)
21019 						rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm;
21020 					else {
21021 						/*
21022 						 * Go out to the end app limited and mark
21023 						 * this new one as next and move the end_appl up
21024 						 * to this guy.
21025 						 */
21026 						if (rack->r_ctl.rc_end_appl)
21027 							rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start;
21028 						rack->r_ctl.rc_end_appl = rsm;
21029 					}
21030 					rsm->r_flags |= RACK_APP_LIMITED;
21031 					rack->r_ctl.rc_app_limited_cnt++;
21032 				}
21033 				if (log)
21034 					rack_log_pacing_delay_calc(rack,
21035 								   rack->r_ctl.rc_app_limited_cnt, seq,
21036 								   tp->gput_ack, 0, 0, 4, __LINE__, NULL, 0);
21037 			}
21038 		}
21039 		/* Check if we need to go into persists or not */
21040 		if ((tp->snd_max == tp->snd_una) &&
21041 		    TCPS_HAVEESTABLISHED(tp->t_state) &&
21042 		    sbavail(sb) &&
21043 		    (sbavail(sb) > tp->snd_wnd) &&
21044 		    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg))) {
21045 			/* Yes lets make sure to move to persist before timer-start */
21046 			rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, tp->snd_una);
21047 		}
21048 		rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, sup_rack);
21049 		rack_log_type_just_return(rack, cts, tot_len_this_send, slot, hpts_calling, app_limited, cwnd_to_use);
21050 	}
21051 #ifdef NETFLIX_SHARED_CWND
21052 	if ((sbavail(sb) == 0) &&
21053 	    rack->r_ctl.rc_scw) {
21054 		tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
21055 		rack->rack_scwnd_is_idle = 1;
21056 	}
21057 #endif
21058 #ifdef TCP_ACCOUNTING
21059 	if (tot_len_this_send > 0) {
21060 		crtsc = get_cyclecount();
21061 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21062 			tp->tcp_cnt_counters[SND_OUT_DATA]++;
21063 			tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
21064 			tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) / segsiz);
21065 		}
21066 	} else {
21067 		crtsc = get_cyclecount();
21068 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21069 			tp->tcp_cnt_counters[SND_LIMITED]++;
21070 			tp->tcp_proc_time[SND_LIMITED] += (crtsc - ts_val);
21071 		}
21072 	}
21073 	sched_unpin();
21074 #endif
21075 	return (0);
21076 
21077 send:
21078 	if ((rack->r_ctl.crte != NULL) &&
21079 	    (rsm == NULL) &&
21080 	    ((rack->rc_hw_nobuf == 1) ||
21081 	     (rack_hw_check_queue && (check_done == 0)))) {
21082 		/*
21083 		 * We only want to do this once with the hw_check_queue,
21084 		 * for the enobuf case we would only do it once if
21085 		 * we come around to again, the flag will be clear.
21086 		 */
21087 		check_done = 1;
21088 		slot = rack_check_queue_level(rack, tp, &tv, cts, len, segsiz);
21089 		if (slot) {
21090 			rack->r_ctl.rc_agg_delayed = 0;
21091 			rack->r_ctl.rc_agg_early = 0;
21092 			rack->r_early = 0;
21093 			rack->r_late = 0;
21094 			SOCK_SENDBUF_UNLOCK(so);
21095 			goto skip_all_send;
21096 		}
21097 	}
21098 	if (rsm || sack_rxmit)
21099 		counter_u64_add(rack_nfto_resend, 1);
21100 	else
21101 		counter_u64_add(rack_non_fto_send, 1);
21102 	if ((flags & TH_FIN) &&
21103 	    sbavail(sb)) {
21104 		/*
21105 		 * We do not transmit a FIN
21106 		 * with data outstanding. We
21107 		 * need to make it so all data
21108 		 * is acked first.
21109 		 */
21110 		flags &= ~TH_FIN;
21111 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
21112 		    (sbused(sb) == (tp->snd_max - tp->snd_una)) &&
21113 		    ((tp->snd_max - tp->snd_una) <= segsiz)) {
21114 			/*
21115 			 * Ok less than or right at a MSS is
21116 			 * outstanding. The original FreeBSD stack would
21117 			 * have sent a FIN, which can speed things up for
21118 			 * a transactional application doing a MSG_WAITALL.
21119 			 * To speed things up since we do *not* send a FIN
21120 			 * if data is outstanding, we send a "challenge ack".
21121 			 * The idea behind that is instead of having to have
21122 			 * the peer wait for the delayed-ack timer to run off
21123 			 * we send an ack that makes the peer send us an ack.
21124 			 */
21125 			rack_send_ack_challange(rack);
21126 		}
21127 	}
21128 	/* Enforce stack imposed max seg size if we have one */
21129 	if (pace_max_seg &&
21130 	    (len > pace_max_seg)) {
21131 		mark = 1;
21132 		len = pace_max_seg;
21133 	}
21134 	if ((rsm == NULL) &&
21135 	    (rack->pcm_in_progress == 0) &&
21136 	    (rack->r_ctl.pcm_max_seg > 0) &&
21137 	    (len >= rack->r_ctl.pcm_max_seg)) {
21138 		/* It is large enough for a measurement */
21139 		add_flag |= RACK_IS_PCM;
21140 		rack_log_pcm(rack, 5, len, rack->r_ctl.pcm_max_seg,  add_flag);
21141 	} else if (rack_verbose_logging) {
21142 		rack_log_pcm(rack, 6, len, rack->r_ctl.pcm_max_seg,  add_flag);
21143 	}
21144 
21145 	SOCKBUF_LOCK_ASSERT(sb);
21146 	if (len > 0) {
21147 		if (len >= segsiz)
21148 			tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT;
21149 		else
21150 			tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT;
21151 	}
21152 	/*
21153 	 * Before ESTABLISHED, force sending of initial options unless TCP
21154 	 * set not to do any options. NOTE: we assume that the IP/TCP header
21155 	 * plus TCP options always fit in a single mbuf, leaving room for a
21156 	 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr)
21157 	 * + optlen <= MCLBYTES
21158 	 */
21159 	optlen = 0;
21160 #ifdef INET6
21161 	if (isipv6)
21162 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
21163 	else
21164 #endif
21165 		hdrlen = sizeof(struct tcpiphdr);
21166 
21167 	/*
21168 	 * Ok what seq are we sending from. If we have
21169 	 * no rsm to use, then we look at various bits,
21170 	 * if we are putting out a SYN it will be ISS.
21171 	 * If we are retransmitting a FIN it will
21172 	 * be snd_max-1 else its snd_max.
21173 	 */
21174 	if (rsm == NULL) {
21175 		if (flags & TH_SYN)
21176 			rack_seq = tp->iss;
21177 		else if ((flags & TH_FIN) &&
21178 			 (tp->t_flags & TF_SENTFIN))
21179 			rack_seq = tp->snd_max - 1;
21180 		else
21181 			rack_seq = tp->snd_max;
21182 	} else {
21183 		rack_seq = rsm->r_start;
21184 	}
21185 	/*
21186 	 * Compute options for segment. We only have to care about SYN and
21187 	 * established connection segments.  Options for SYN-ACK segments
21188 	 * are handled in TCP syncache.
21189 	 */
21190 	to.to_flags = 0;
21191 	if ((tp->t_flags & TF_NOOPT) == 0) {
21192 		/* Maximum segment size. */
21193 		if (flags & TH_SYN) {
21194 			to.to_mss = tcp_mssopt(&inp->inp_inc);
21195 			if (tp->t_port)
21196 				to.to_mss -= V_tcp_udp_tunneling_overhead;
21197 			to.to_flags |= TOF_MSS;
21198 
21199 			/*
21200 			 * On SYN or SYN|ACK transmits on TFO connections,
21201 			 * only include the TFO option if it is not a
21202 			 * retransmit, as the presence of the TFO option may
21203 			 * have caused the original SYN or SYN|ACK to have
21204 			 * been dropped by a middlebox.
21205 			 */
21206 			if ((tp->t_flags & TF_FASTOPEN) &&
21207 			    (tp->t_rxtshift == 0)) {
21208 				if (tp->t_state == TCPS_SYN_RECEIVED) {
21209 					to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN;
21210 					to.to_tfo_cookie =
21211 						(u_int8_t *)&tp->t_tfo_cookie.server;
21212 					to.to_flags |= TOF_FASTOPEN;
21213 					wanted_cookie = 1;
21214 				} else if (tp->t_state == TCPS_SYN_SENT) {
21215 					to.to_tfo_len =
21216 						tp->t_tfo_client_cookie_len;
21217 					to.to_tfo_cookie =
21218 						tp->t_tfo_cookie.client;
21219 					to.to_flags |= TOF_FASTOPEN;
21220 					wanted_cookie = 1;
21221 					/*
21222 					 * If we wind up having more data to
21223 					 * send with the SYN than can fit in
21224 					 * one segment, don't send any more
21225 					 * until the SYN|ACK comes back from
21226 					 * the other end.
21227 					 */
21228 					sendalot = 0;
21229 				}
21230 			}
21231 		}
21232 		/* Window scaling. */
21233 		if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
21234 			to.to_wscale = tp->request_r_scale;
21235 			to.to_flags |= TOF_SCALE;
21236 		}
21237 		/* Timestamps. */
21238 		if ((tp->t_flags & TF_RCVD_TSTMP) ||
21239 		    ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
21240 			uint32_t ts_to_use;
21241 
21242 			if ((rack->r_rcvpath_rtt_up == 1) &&
21243 			    (ms_cts == rack->r_ctl.last_rcv_tstmp_for_rtt)) {
21244 				/*
21245 				 * When we are doing a rcv_rtt probe all
21246 				 * other timestamps use the next msec. This
21247 				 * is safe since our previous ack is in the
21248 				 * air and we will just have a few more
21249 				 * on the next ms. This assures that only
21250 				 * the one ack has the ms_cts that was on
21251 				 * our ack-probe.
21252 				 */
21253 				ts_to_use = ms_cts + 1;
21254 			} else {
21255 				ts_to_use = ms_cts;
21256 			}
21257 			to.to_tsval = ts_to_use + tp->ts_offset;
21258 			to.to_tsecr = tp->ts_recent;
21259 			to.to_flags |= TOF_TS;
21260 			if ((len == 0) &&
21261 			    (TCPS_HAVEESTABLISHED(tp->t_state)) &&
21262 			    ((ms_cts - rack->r_ctl.last_rcv_tstmp_for_rtt) > RCV_PATH_RTT_MS) &&
21263 			    (tp->snd_una == tp->snd_max) &&
21264 			    (flags & TH_ACK) &&
21265 			    (sbavail(sb) == 0) &&
21266 			    (rack->r_ctl.current_round != 0) &&
21267 			    ((flags & (TH_SYN|TH_FIN)) == 0) &&
21268 			    (rack->r_rcvpath_rtt_up == 0)) {
21269 				rack->r_ctl.last_rcv_tstmp_for_rtt = ms_cts;
21270 				rack->r_ctl.last_time_of_arm_rcv = cts;
21271 				rack->r_rcvpath_rtt_up = 1;
21272 				/* Subtract 1 from seq to force a response */
21273 				rack_seq--;
21274 			}
21275 		}
21276 		/* Set receive buffer autosizing timestamp. */
21277 		if (tp->rfbuf_ts == 0 &&
21278 		    (so->so_rcv.sb_flags & SB_AUTOSIZE)) {
21279 			tp->rfbuf_ts = ms_cts;
21280 		}
21281 		/* Selective ACK's. */
21282 		if (tp->t_flags & TF_SACK_PERMIT) {
21283 			if (flags & TH_SYN)
21284 				to.to_flags |= TOF_SACKPERM;
21285 			else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
21286 				 tp->rcv_numsacks > 0) {
21287 				to.to_flags |= TOF_SACK;
21288 				to.to_nsacks = tp->rcv_numsacks;
21289 				to.to_sacks = (u_char *)tp->sackblks;
21290 			}
21291 		}
21292 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
21293 		/* TCP-MD5 (RFC2385). */
21294 		if (tp->t_flags & TF_SIGNATURE)
21295 			to.to_flags |= TOF_SIGNATURE;
21296 #endif
21297 
21298 		/* Processing the options. */
21299 		hdrlen += optlen = tcp_addoptions(&to, opt);
21300 		/*
21301 		 * If we wanted a TFO option to be added, but it was unable
21302 		 * to fit, ensure no data is sent.
21303 		 */
21304 		if ((tp->t_flags & TF_FASTOPEN) && wanted_cookie &&
21305 		    !(to.to_flags & TOF_FASTOPEN))
21306 			len = 0;
21307 	}
21308 	if (tp->t_port) {
21309 		if (V_tcp_udp_tunneling_port == 0) {
21310 			/* The port was removed?? */
21311 			SOCK_SENDBUF_UNLOCK(so);
21312 #ifdef TCP_ACCOUNTING
21313 			crtsc = get_cyclecount();
21314 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21315 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
21316 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
21317 			}
21318 			sched_unpin();
21319 #endif
21320 			return (EHOSTUNREACH);
21321 		}
21322 		hdrlen += sizeof(struct udphdr);
21323 	}
21324 #ifdef INET6
21325 	if (isipv6)
21326 		ipoptlen = ip6_optlen(inp);
21327 	else
21328 #endif
21329 		if (inp->inp_options)
21330 			ipoptlen = inp->inp_options->m_len -
21331 				offsetof(struct ipoption, ipopt_list);
21332 		else
21333 			ipoptlen = 0;
21334 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
21335 	ipoptlen += ipsec_optlen;
21336 #endif
21337 
21338 	/*
21339 	 * Adjust data length if insertion of options will bump the packet
21340 	 * length beyond the t_maxseg length. Clear the FIN bit because we
21341 	 * cut off the tail of the segment.
21342 	 */
21343 	if (len + optlen + ipoptlen > tp->t_maxseg) {
21344 		if (tso) {
21345 			uint32_t if_hw_tsomax;
21346 			uint32_t moff;
21347 			int32_t max_len;
21348 
21349 			/* extract TSO information */
21350 			if_hw_tsomax = tp->t_tsomax;
21351 			if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
21352 			if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
21353 			KASSERT(ipoptlen == 0,
21354 				("%s: TSO can't do IP options", __func__));
21355 
21356 			/*
21357 			 * Check if we should limit by maximum payload
21358 			 * length:
21359 			 */
21360 			if (if_hw_tsomax != 0) {
21361 				/* compute maximum TSO length */
21362 				max_len = (if_hw_tsomax - hdrlen -
21363 					   max_linkhdr);
21364 				if (max_len <= 0) {
21365 					len = 0;
21366 				} else if (len > max_len) {
21367 					sendalot = 1;
21368 					len = max_len;
21369 					mark = 2;
21370 				}
21371 			}
21372 			/*
21373 			 * Prevent the last segment from being fractional
21374 			 * unless the send sockbuf can be emptied:
21375 			 */
21376 			max_len = (tp->t_maxseg - optlen);
21377 			if ((sb_offset + len) < sbavail(sb)) {
21378 				moff = len % (u_int)max_len;
21379 				if (moff != 0) {
21380 					mark = 3;
21381 					len -= moff;
21382 				}
21383 			}
21384 			/*
21385 			 * In case there are too many small fragments don't
21386 			 * use TSO:
21387 			 */
21388 			if (len <= max_len) {
21389 				mark = 4;
21390 				tso = 0;
21391 			}
21392 			/*
21393 			 * Send the FIN in a separate segment after the bulk
21394 			 * sending is done. We don't trust the TSO
21395 			 * implementations to clear the FIN flag on all but
21396 			 * the last segment.
21397 			 */
21398 			if (tp->t_flags & TF_NEEDFIN) {
21399 				sendalot = 4;
21400 			}
21401 		} else {
21402 			mark = 5;
21403 			if (optlen + ipoptlen >= tp->t_maxseg) {
21404 				/*
21405 				 * Since we don't have enough space to put
21406 				 * the IP header chain and the TCP header in
21407 				 * one packet as required by RFC 7112, don't
21408 				 * send it. Also ensure that at least one
21409 				 * byte of the payload can be put into the
21410 				 * TCP segment.
21411 				 */
21412 				SOCK_SENDBUF_UNLOCK(so);
21413 				error = EMSGSIZE;
21414 				sack_rxmit = 0;
21415 				goto out;
21416 			}
21417 			len = tp->t_maxseg - optlen - ipoptlen;
21418 			sendalot = 5;
21419 		}
21420 	} else {
21421 		tso = 0;
21422 		mark = 6;
21423 	}
21424 	KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET,
21425 		("%s: len > IP_MAXPACKET", __func__));
21426 #ifdef DIAGNOSTIC
21427 #ifdef INET6
21428 	if (max_linkhdr + hdrlen > MCLBYTES)
21429 #else
21430 		if (max_linkhdr + hdrlen > MHLEN)
21431 #endif
21432 			panic("tcphdr too big");
21433 #endif
21434 
21435 	/*
21436 	 * This KASSERT is here to catch edge cases at a well defined place.
21437 	 * Before, those had triggered (random) panic conditions further
21438 	 * down.
21439 	 */
21440 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
21441 	if ((len == 0) &&
21442 	    (flags & TH_FIN) &&
21443 	    (sbused(sb))) {
21444 		/*
21445 		 * We have outstanding data, don't send a fin by itself!.
21446 		 *
21447 		 * Check to see if we need to send a challenge ack.
21448 		 */
21449 		if ((sbused(sb) == (tp->snd_max - tp->snd_una)) &&
21450 		    ((tp->snd_max - tp->snd_una) <= segsiz)) {
21451 			/*
21452 			 * Ok less than or right at a MSS is
21453 			 * outstanding. The original FreeBSD stack would
21454 			 * have sent a FIN, which can speed things up for
21455 			 * a transactional application doing a MSG_WAITALL.
21456 			 * To speed things up since we do *not* send a FIN
21457 			 * if data is outstanding, we send a "challenge ack".
21458 			 * The idea behind that is instead of having to have
21459 			 * the peer wait for the delayed-ack timer to run off
21460 			 * we send an ack that makes the peer send us an ack.
21461 			 */
21462 			rack_send_ack_challange(rack);
21463 		}
21464 		goto just_return;
21465 	}
21466 	/*
21467 	 * Grab a header mbuf, attaching a copy of data to be transmitted,
21468 	 * and initialize the header from the template for sends on this
21469 	 * connection.
21470 	 */
21471 	hw_tls = tp->t_nic_ktls_xmit != 0;
21472 	if (len) {
21473 		uint32_t max_val;
21474 		uint32_t moff;
21475 
21476 		if (pace_max_seg)
21477 			max_val = pace_max_seg;
21478 		else
21479 			max_val = len;
21480 		/*
21481 		 * We allow a limit on sending with hptsi.
21482 		 */
21483 		if (len > max_val) {
21484 			mark = 7;
21485 			len = max_val;
21486 		}
21487 #ifdef INET6
21488 		if (MHLEN < hdrlen + max_linkhdr)
21489 			m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
21490 		else
21491 #endif
21492 			m = m_gethdr(M_NOWAIT, MT_DATA);
21493 
21494 		if (m == NULL) {
21495 			SOCK_SENDBUF_UNLOCK(so);
21496 			error = ENOBUFS;
21497 			sack_rxmit = 0;
21498 			goto out;
21499 		}
21500 		m->m_data += max_linkhdr;
21501 		m->m_len = hdrlen;
21502 
21503 		/*
21504 		 * Start the m_copy functions from the closest mbuf to the
21505 		 * sb_offset in the socket buffer chain.
21506 		 */
21507 		mb = sbsndptr_noadv(sb, sb_offset, &moff);
21508 		s_mb = mb;
21509 		s_moff = moff;
21510 		if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) {
21511 			m_copydata(mb, moff, (int)len,
21512 				   mtod(m, caddr_t)+hdrlen);
21513 			/*
21514 			 * If we are not retransmitting advance the
21515 			 * sndptr to help remember the next place in
21516 			 * the sb.
21517 			 */
21518 			if (rsm == NULL)
21519 				sbsndptr_adv(sb, mb, len);
21520 			m->m_len += len;
21521 		} else {
21522 			struct sockbuf *msb;
21523 
21524 			/*
21525 			 * If we are not retransmitting pass in msb so
21526 			 * the socket buffer can be advanced. Otherwise
21527 			 * set it to NULL if its a retransmission since
21528 			 * we don't want to change the sb remembered
21529 			 * location.
21530 			 */
21531 			if (rsm == NULL)
21532 				msb = sb;
21533 			else
21534 				msb = NULL;
21535 			m->m_next = tcp_m_copym(
21536 				mb, moff, &len,
21537 				if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, msb,
21538 				((rsm == NULL) ? hw_tls : 0)
21539 #ifdef NETFLIX_COPY_ARGS
21540 				, &s_mb, &s_moff
21541 #endif
21542 				);
21543 			if (len <= (tp->t_maxseg - optlen)) {
21544 				/*
21545 				 * Must have ran out of mbufs for the copy
21546 				 * shorten it to no longer need tso. Lets
21547 				 * not put on sendalot since we are low on
21548 				 * mbufs.
21549 				 */
21550 				tso = 0;
21551 			}
21552 			if (m->m_next == NULL) {
21553 				SOCK_SENDBUF_UNLOCK(so);
21554 				(void)m_free(m);
21555 				error = ENOBUFS;
21556 				sack_rxmit = 0;
21557 				goto out;
21558 			}
21559 		}
21560 		if (sack_rxmit) {
21561 			if (rsm && (rsm->r_flags & RACK_TLP)) {
21562 				/*
21563 				 * TLP should not count in retran count, but
21564 				 * in its own bin
21565 				 */
21566 				counter_u64_add(rack_tlp_retran, 1);
21567 				counter_u64_add(rack_tlp_retran_bytes, len);
21568 			} else {
21569 				tp->t_sndrexmitpack++;
21570 				KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
21571 				KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
21572 			}
21573 #ifdef STATS
21574 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
21575 						 len);
21576 #endif
21577 		} else {
21578 			KMOD_TCPSTAT_INC(tcps_sndpack);
21579 			KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
21580 #ifdef STATS
21581 			stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
21582 						 len);
21583 #endif
21584 		}
21585 		/*
21586 		 * If we're sending everything we've got, set PUSH. (This
21587 		 * will keep happy those implementations which only give
21588 		 * data to the user when a buffer fills or a PUSH comes in.)
21589 		 */
21590 		if (sb_offset + len == sbused(sb) &&
21591 		    sbused(sb) &&
21592 		    !(flags & TH_SYN)) {
21593 			flags |= TH_PUSH;
21594 			add_flag |= RACK_HAD_PUSH;
21595 		}
21596 
21597 		SOCK_SENDBUF_UNLOCK(so);
21598 	} else {
21599 		SOCK_SENDBUF_UNLOCK(so);
21600 		if (tp->t_flags & TF_ACKNOW)
21601 			KMOD_TCPSTAT_INC(tcps_sndacks);
21602 		else if (flags & (TH_SYN | TH_FIN | TH_RST))
21603 			KMOD_TCPSTAT_INC(tcps_sndctrl);
21604 		else
21605 			KMOD_TCPSTAT_INC(tcps_sndwinup);
21606 
21607 		m = m_gethdr(M_NOWAIT, MT_DATA);
21608 		if (m == NULL) {
21609 			error = ENOBUFS;
21610 			sack_rxmit = 0;
21611 			goto out;
21612 		}
21613 #ifdef INET6
21614 		if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
21615 		    MHLEN >= hdrlen) {
21616 			M_ALIGN(m, hdrlen);
21617 		} else
21618 #endif
21619 			m->m_data += max_linkhdr;
21620 		m->m_len = hdrlen;
21621 	}
21622 	SOCK_SENDBUF_UNLOCK_ASSERT(so);
21623 	m->m_pkthdr.rcvif = (struct ifnet *)0;
21624 #ifdef MAC
21625 	mac_inpcb_create_mbuf(inp, m);
21626 #endif
21627 	if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) &&  rack->r_fsb_inited) {
21628 #ifdef INET6
21629 		if (isipv6)
21630 			ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
21631 		else
21632 #endif				/* INET6 */
21633 #ifdef INET
21634 			ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
21635 #endif
21636 		th = rack->r_ctl.fsb.th;
21637 		udp = rack->r_ctl.fsb.udp;
21638 		if (udp) {
21639 #ifdef INET6
21640 			if (isipv6)
21641 				ulen = hdrlen + len - sizeof(struct ip6_hdr);
21642 			else
21643 #endif				/* INET6 */
21644 				ulen = hdrlen + len - sizeof(struct ip);
21645 			udp->uh_ulen = htons(ulen);
21646 		}
21647 	} else {
21648 #ifdef INET6
21649 		if (isipv6) {
21650 			ip6 = mtod(m, struct ip6_hdr *);
21651 			if (tp->t_port) {
21652 				udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
21653 				udp->uh_sport = htons(V_tcp_udp_tunneling_port);
21654 				udp->uh_dport = tp->t_port;
21655 				ulen = hdrlen + len - sizeof(struct ip6_hdr);
21656 				udp->uh_ulen = htons(ulen);
21657 				th = (struct tcphdr *)(udp + 1);
21658 			} else
21659 				th = (struct tcphdr *)(ip6 + 1);
21660 			tcpip_fillheaders(inp, tp->t_port, ip6, th);
21661 		} else
21662 #endif				/* INET6 */
21663 		{
21664 #ifdef INET
21665 			ip = mtod(m, struct ip *);
21666 			if (tp->t_port) {
21667 				udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
21668 				udp->uh_sport = htons(V_tcp_udp_tunneling_port);
21669 				udp->uh_dport = tp->t_port;
21670 				ulen = hdrlen + len - sizeof(struct ip);
21671 				udp->uh_ulen = htons(ulen);
21672 				th = (struct tcphdr *)(udp + 1);
21673 			} else
21674 				th = (struct tcphdr *)(ip + 1);
21675 			tcpip_fillheaders(inp, tp->t_port, ip, th);
21676 #endif
21677 		}
21678 	}
21679 	/*
21680 	 * If we are starting a connection, send ECN setup SYN packet. If we
21681 	 * are on a retransmit, we may resend those bits a number of times
21682 	 * as per RFC 3168.
21683 	 */
21684 	if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn) {
21685 		flags |= tcp_ecn_output_syn_sent(tp);
21686 	}
21687 	/* Also handle parallel SYN for ECN */
21688 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
21689 	    (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) {
21690 		int ect = tcp_ecn_output_established(tp, &flags, len, sack_rxmit);
21691 		if ((tp->t_state == TCPS_SYN_RECEIVED) &&
21692 		    (tp->t_flags2 & TF2_ECN_SND_ECE))
21693 			tp->t_flags2 &= ~TF2_ECN_SND_ECE;
21694 #ifdef INET6
21695 		if (isipv6) {
21696 			ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
21697 			ip6->ip6_flow |= htonl(ect << 20);
21698 		}
21699 		else
21700 #endif
21701 		{
21702 #ifdef INET
21703 			ip->ip_tos &= ~IPTOS_ECN_MASK;
21704 			ip->ip_tos |= ect;
21705 #endif
21706 		}
21707 	}
21708 	th->th_seq = htonl(rack_seq);
21709 	th->th_ack = htonl(tp->rcv_nxt);
21710 	tcp_set_flags(th, flags);
21711 	/*
21712 	 * Calculate receive window.  Don't shrink window, but avoid silly
21713 	 * window syndrome.
21714 	 * If a RST segment is sent, advertise a window of zero.
21715 	 */
21716 	if (flags & TH_RST) {
21717 		recwin = 0;
21718 	} else {
21719 		if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
21720 		    recwin < (long)segsiz) {
21721 			recwin = 0;
21722 		}
21723 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) &&
21724 		    recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
21725 			recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
21726 	}
21727 
21728 	/*
21729 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or
21730 	 * <SYN,ACK>) segment itself is never scaled.  The <SYN,ACK> case is
21731 	 * handled in syncache.
21732 	 */
21733 	if (flags & TH_SYN)
21734 		th->th_win = htons((u_short)
21735 				   (min(sbspace(&so->so_rcv), TCP_MAXWIN)));
21736 	else {
21737 		/* Avoid shrinking window with window scaling. */
21738 		recwin = roundup2(recwin, 1 << tp->rcv_scale);
21739 		th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
21740 	}
21741 	/*
21742 	 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0
21743 	 * window.  This may cause the remote transmitter to stall.  This
21744 	 * flag tells soreceive() to disable delayed acknowledgements when
21745 	 * draining the buffer.  This can occur if the receiver is
21746 	 * attempting to read more data than can be buffered prior to
21747 	 * transmitting on the connection.
21748 	 */
21749 	if (th->th_win == 0) {
21750 		tp->t_sndzerowin++;
21751 		tp->t_flags |= TF_RXWIN0SENT;
21752 	} else
21753 		tp->t_flags &= ~TF_RXWIN0SENT;
21754 	tp->snd_up = tp->snd_una;	/* drag it along, its deprecated */
21755 	/* Now are we using fsb?, if so copy the template data to the mbuf */
21756 	if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) && rack->r_fsb_inited) {
21757 		uint8_t *cpto;
21758 
21759 		cpto = mtod(m, uint8_t *);
21760 		memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
21761 		/*
21762 		 * We have just copied in:
21763 		 * IP/IP6
21764 		 * <optional udphdr>
21765 		 * tcphdr (no options)
21766 		 *
21767 		 * We need to grab the correct pointers into the mbuf
21768 		 * for both the tcp header, and possibly the udp header (if tunneling).
21769 		 * We do this by using the offset in the copy buffer and adding it
21770 		 * to the mbuf base pointer (cpto).
21771 		 */
21772 #ifdef INET6
21773 		if (isipv6)
21774 			ip6 = mtod(m, struct ip6_hdr *);
21775 		else
21776 #endif				/* INET6 */
21777 #ifdef INET
21778 			ip = mtod(m, struct ip *);
21779 #endif
21780 		th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
21781 		/* If we have a udp header lets set it into the mbuf as well */
21782 		if (udp)
21783 			udp = (struct udphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.udp - rack->r_ctl.fsb.tcp_ip_hdr));
21784 	}
21785 	if (optlen) {
21786 		bcopy(opt, th + 1, optlen);
21787 		th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
21788 	}
21789 	/*
21790 	 * Put TCP length in extended header, and then checksum extended
21791 	 * header and data.
21792 	 */
21793 	m->m_pkthdr.len = hdrlen + len;	/* in6_cksum() need this */
21794 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
21795 	if (to.to_flags & TOF_SIGNATURE) {
21796 		/*
21797 		 * Calculate MD5 signature and put it into the place
21798 		 * determined before.
21799 		 * NOTE: since TCP options buffer doesn't point into
21800 		 * mbuf's data, calculate offset and use it.
21801 		 */
21802 		if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
21803 						       (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
21804 			/*
21805 			 * Do not send segment if the calculation of MD5
21806 			 * digest has failed.
21807 			 */
21808 			goto out;
21809 		}
21810 	}
21811 #endif
21812 #ifdef INET6
21813 	if (isipv6) {
21814 		/*
21815 		 * ip6_plen is not need to be filled now, and will be filled
21816 		 * in ip6_output.
21817 		 */
21818 		if (tp->t_port) {
21819 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
21820 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
21821 			udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
21822 			th->th_sum = htons(0);
21823 			UDPSTAT_INC(udps_opackets);
21824 		} else {
21825 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
21826 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
21827 			th->th_sum = in6_cksum_pseudo(ip6,
21828 						      sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
21829 						      0);
21830 		}
21831 	}
21832 #endif
21833 #if defined(INET6) && defined(INET)
21834 	else
21835 #endif
21836 #ifdef INET
21837 	{
21838 		if (tp->t_port) {
21839 			m->m_pkthdr.csum_flags = CSUM_UDP;
21840 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
21841 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
21842 						ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
21843 			th->th_sum = htons(0);
21844 			UDPSTAT_INC(udps_opackets);
21845 		} else {
21846 			m->m_pkthdr.csum_flags = CSUM_TCP;
21847 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
21848 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
21849 					       ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
21850 									IPPROTO_TCP + len + optlen));
21851 		}
21852 		/* IP version must be set here for ipv4/ipv6 checking later */
21853 		KASSERT(ip->ip_v == IPVERSION,
21854 			("%s: IP version incorrect: %d", __func__, ip->ip_v));
21855 	}
21856 #endif
21857 	/*
21858 	 * Enable TSO and specify the size of the segments. The TCP pseudo
21859 	 * header checksum is always provided. XXX: Fixme: This is currently
21860 	 * not the case for IPv6.
21861 	 */
21862 	if (tso) {
21863 		/*
21864 		 * Here we must use t_maxseg and the optlen since
21865 		 * the optlen may include SACK's (or DSACK).
21866 		 */
21867 		KASSERT(len > tp->t_maxseg - optlen,
21868 			("%s: len <= tso_segsz", __func__));
21869 		m->m_pkthdr.csum_flags |= CSUM_TSO;
21870 		m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen;
21871 	}
21872 	KASSERT(len + hdrlen == m_length(m, NULL),
21873 		("%s: mbuf chain different than expected: %d + %u != %u",
21874 		 __func__, len, hdrlen, m_length(m, NULL)));
21875 
21876 #ifdef TCP_HHOOK
21877 	/* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */
21878 	hhook_run_tcp_est_out(tp, th, &to, len, tso);
21879 #endif
21880 	if ((rack->r_ctl.crte != NULL) &&
21881 	    (rack->rc_hw_nobuf == 0) &&
21882 	    tcp_bblogging_on(tp)) {
21883 		rack_log_queue_level(tp, rack, len, &tv, cts);
21884 	}
21885 	/* We're getting ready to send; log now. */
21886 	if (tcp_bblogging_on(rack->rc_tp)) {
21887 		union tcp_log_stackspecific log;
21888 
21889 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
21890 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
21891 		if (rack->rack_no_prr)
21892 			log.u_bbr.flex1 = 0;
21893 		else
21894 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
21895 		log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
21896 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
21897 		log.u_bbr.flex4 = orig_len;
21898 		/* Save off the early/late values */
21899 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
21900 		log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
21901 		log.u_bbr.bw_inuse = rack_get_bw(rack);
21902 		log.u_bbr.cur_del_rate = rack->r_ctl.gp_bw;
21903 		log.u_bbr.flex8 = 0;
21904 		if (rsm) {
21905 			if (rsm->r_flags & RACK_RWND_COLLAPSED) {
21906 				rack_log_collapse(rack, rsm->r_start, rsm->r_end, 0, __LINE__, 5, rsm->r_flags, rsm);
21907 				counter_u64_add(rack_collapsed_win_rxt, 1);
21908 				counter_u64_add(rack_collapsed_win_rxt_bytes, (rsm->r_end - rsm->r_start));
21909 			}
21910 			if (doing_tlp)
21911 				log.u_bbr.flex8 = 2;
21912 			else
21913 				log.u_bbr.flex8 = 1;
21914 		} else {
21915 			if (doing_tlp)
21916 				log.u_bbr.flex8 = 3;
21917 		}
21918 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm);
21919 		log.u_bbr.flex7 = mark;
21920 		log.u_bbr.flex7 <<= 8;
21921 		log.u_bbr.flex7 |= pass;
21922 		log.u_bbr.pkts_out = tp->t_maxseg;
21923 		log.u_bbr.timeStamp = cts;
21924 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
21925 		if (rsm && (rsm->r_rtr_cnt > 0)) {
21926 			/*
21927 			 * When we have a retransmit we want to log the
21928 			 * burst at send and flight at send from before.
21929 			 */
21930 			log.u_bbr.flex5 = rsm->r_fas;
21931 			log.u_bbr.bbr_substate = rsm->r_bas;
21932 		} else {
21933 			/*
21934 			 * New transmits we log in flex5 the inflight again as
21935 			 * well as the number of segments in our send in the
21936 			 * substate field.
21937 			 */
21938 			log.u_bbr.flex5 = log.u_bbr.inflight;
21939 			log.u_bbr.bbr_substate = (uint8_t)((len + segsiz - 1)/segsiz);
21940 		}
21941 		log.u_bbr.lt_epoch = cwnd_to_use;
21942 		log.u_bbr.delivered = sendalot;
21943 		log.u_bbr.rttProp = (uintptr_t)rsm;
21944 		log.u_bbr.pkt_epoch = __LINE__;
21945 		if (rsm) {
21946 			log.u_bbr.delRate = rsm->r_flags;
21947 			log.u_bbr.delRate <<= 31;
21948 			log.u_bbr.delRate |= rack->r_must_retran;
21949 			log.u_bbr.delRate <<= 1;
21950 			log.u_bbr.delRate |= (sack_rxmit & 0x00000001);
21951 		} else {
21952 			log.u_bbr.delRate = rack->r_must_retran;
21953 			log.u_bbr.delRate <<= 1;
21954 			log.u_bbr.delRate |= (sack_rxmit & 0x00000001);
21955 		}
21956 		lgb = tcp_log_event(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK,
21957 				    len, &log, false, NULL, __func__, __LINE__, &tv);
21958 	} else
21959 		lgb = NULL;
21960 
21961 	/*
21962 	 * Fill in IP length and desired time to live and send to IP level.
21963 	 * There should be a better way to handle ttl and tos; we could keep
21964 	 * them in the template, but need a way to checksum without them.
21965 	 */
21966 	/*
21967 	 * m->m_pkthdr.len should have been set before cksum calcuration,
21968 	 * because in6_cksum() need it.
21969 	 */
21970 #ifdef INET6
21971 	if (isipv6) {
21972 		/*
21973 		 * we separately set hoplimit for every segment, since the
21974 		 * user might want to change the value via setsockopt. Also,
21975 		 * desired default hop limit might be changed via Neighbor
21976 		 * Discovery.
21977 		 */
21978 		rack->r_ctl.fsb.hoplimit = ip6->ip6_hlim = in6_selecthlim(inp, NULL);
21979 
21980 		/*
21981 		 * Set the packet size here for the benefit of DTrace
21982 		 * probes. ip6_output() will set it properly; it's supposed
21983 		 * to include the option header lengths as well.
21984 		 */
21985 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
21986 
21987 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
21988 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
21989 		else
21990 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
21991 
21992 		if (tp->t_state == TCPS_SYN_SENT)
21993 			TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th);
21994 
21995 		TCP_PROBE5(send, NULL, tp, ip6, tp, th);
21996 		/* TODO: IPv6 IP6TOS_ECT bit on */
21997 		error = ip6_output(m,
21998 				   inp->in6p_outputopts,
21999 				   &inp->inp_route6,
22000 				   ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0),
22001 				   NULL, NULL, inp);
22002 
22003 		if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL)
22004 			mtu = inp->inp_route6.ro_nh->nh_mtu;
22005 	}
22006 #endif				/* INET6 */
22007 #if defined(INET) && defined(INET6)
22008 	else
22009 #endif
22010 #ifdef INET
22011 	{
22012 		ip->ip_len = htons(m->m_pkthdr.len);
22013 #ifdef INET6
22014 		if (inp->inp_vflag & INP_IPV6PROTO)
22015 			ip->ip_ttl = in6_selecthlim(inp, NULL);
22016 #endif				/* INET6 */
22017 		rack->r_ctl.fsb.hoplimit = ip->ip_ttl;
22018 		/*
22019 		 * If we do path MTU discovery, then we set DF on every
22020 		 * packet. This might not be the best thing to do according
22021 		 * to RFC3390 Section 2. However the tcp hostcache migitates
22022 		 * the problem so it affects only the first tcp connection
22023 		 * with a host.
22024 		 *
22025 		 * NB: Don't set DF on small MTU/MSS to have a safe
22026 		 * fallback.
22027 		 */
22028 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
22029 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
22030 			if (tp->t_port == 0 || len < V_tcp_minmss) {
22031 				ip->ip_off |= htons(IP_DF);
22032 			}
22033 		} else {
22034 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
22035 		}
22036 
22037 		if (tp->t_state == TCPS_SYN_SENT)
22038 			TCP_PROBE5(connect__request, NULL, tp, ip, tp, th);
22039 
22040 		TCP_PROBE5(send, NULL, tp, ip, tp, th);
22041 
22042 		error = ip_output(m,
22043 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
22044 				  inp->inp_options,
22045 #else
22046 				  NULL,
22047 #endif
22048 				  &inp->inp_route,
22049 				  ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0,
22050 				  inp);
22051 		if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL)
22052 			mtu = inp->inp_route.ro_nh->nh_mtu;
22053 	}
22054 #endif				/* INET */
22055 	if (lgb) {
22056 		lgb->tlb_errno = error;
22057 		lgb = NULL;
22058 	}
22059 
22060 out:
22061 	/*
22062 	 * In transmit state, time the transmission and arrange for the
22063 	 * retransmit.  In persist state, just set snd_max.
22064 	 */
22065 	rack_log_output(tp, &to, len, rack_seq, (uint8_t) flags, error,
22066 			rack_to_usec_ts(&tv),
22067 			rsm, add_flag, s_mb, s_moff, hw_tls, segsiz);
22068 	if (error == 0) {
22069 		if (add_flag & RACK_IS_PCM) {
22070 			/* We just launched a PCM */
22071 			/* rrs here log */
22072 			rack->pcm_in_progress = 1;
22073 			rack->pcm_needed = 0;
22074 			rack_log_pcm(rack, 7, len, rack->r_ctl.pcm_max_seg,  add_flag);
22075 		}
22076 		if (rsm == NULL) {
22077 			if (rack->lt_bw_up == 0) {
22078 				rack->r_ctl.lt_timemark = tcp_tv_to_lusectick(&tv);
22079 				rack->r_ctl.lt_seq = tp->snd_una;
22080 				rack->lt_bw_up = 1;
22081 			} else if (((rack_seq + len) - rack->r_ctl.lt_seq) > 0x7fffffff) {
22082 				/*
22083 				 * Need to record what we have since we are
22084 				 * approaching seq wrap.
22085 				 */
22086 				uint64_t tmark;
22087 
22088 				rack->r_ctl.lt_bw_bytes += (tp->snd_una - rack->r_ctl.lt_seq);
22089 				rack->r_ctl.lt_seq = tp->snd_una;
22090 				tmark = tcp_get_u64_usecs(&tv);
22091 				if (tmark > rack->r_ctl.lt_timemark) {
22092 					rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark);
22093 					rack->r_ctl.lt_timemark = tmark;
22094 				}
22095 			}
22096 		}
22097 		rack->forced_ack = 0;	/* If we send something zap the FA flag */
22098 		counter_u64_add(rack_total_bytes, len);
22099 		tcp_account_for_send(tp, len, (rsm != NULL), doing_tlp, hw_tls);
22100 		if (rsm && doing_tlp) {
22101 			rack->rc_last_sent_tlp_past_cumack = 0;
22102 			rack->rc_last_sent_tlp_seq_valid = 1;
22103 			rack->r_ctl.last_sent_tlp_seq = rsm->r_start;
22104 			rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start;
22105 		}
22106 		if (rack->rc_hw_nobuf) {
22107 			rack->rc_hw_nobuf = 0;
22108 			rack->r_ctl.rc_agg_delayed = 0;
22109 			rack->r_early = 0;
22110 			rack->r_late = 0;
22111 			rack->r_ctl.rc_agg_early = 0;
22112 		}
22113 		if (rsm && (doing_tlp == 0)) {
22114 			/* Set we retransmitted */
22115 			rack->rc_gp_saw_rec = 1;
22116 		} else {
22117 			if (cwnd_to_use > tp->snd_ssthresh) {
22118 				/* Set we sent in CA */
22119 				rack->rc_gp_saw_ca = 1;
22120 			} else {
22121 				/* Set we sent in SS */
22122 				rack->rc_gp_saw_ss = 1;
22123 			}
22124 		}
22125 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
22126 		    (tp->t_flags & TF_SACK_PERMIT) &&
22127 		    tp->rcv_numsacks > 0)
22128 			tcp_clean_dsack_blocks(tp);
22129 		tot_len_this_send += len;
22130 		if (len == 0) {
22131 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_SNDACK], 1);
22132 		} else {
22133 			int idx;
22134 
22135 			idx = (len / segsiz) + 3;
22136 			if (idx >= TCP_MSS_ACCT_ATIMER)
22137 				counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
22138 			else
22139 				counter_u64_add(rack_out_size[idx], 1);
22140 		}
22141 	}
22142 	if ((rack->rack_no_prr == 0) &&
22143 	    sub_from_prr &&
22144 	    (error == 0)) {
22145 		if (rack->r_ctl.rc_prr_sndcnt >= len)
22146 			rack->r_ctl.rc_prr_sndcnt -= len;
22147 		else
22148 			rack->r_ctl.rc_prr_sndcnt = 0;
22149 	}
22150 	sub_from_prr = 0;
22151 	if (doing_tlp) {
22152 		/* Make sure the TLP is added */
22153 		add_flag |= RACK_TLP;
22154 	} else if (rsm) {
22155 		/* If its a resend without TLP then it must not have the flag */
22156 		rsm->r_flags &= ~RACK_TLP;
22157 	}
22158 
22159 
22160 	if ((error == 0) &&
22161 	    (len > 0) &&
22162 	    (tp->snd_una == tp->snd_max))
22163 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
22164 
22165 	{
22166 		/*
22167 		 * This block is not associated with the above error == 0 test.
22168 		 * It is used to advance snd_max if we have a new transmit.
22169 		 */
22170 		tcp_seq startseq = tp->snd_max;
22171 
22172 
22173 		if (rsm && (doing_tlp == 0))
22174 			rack->r_ctl.rc_loss_count += rsm->r_end - rsm->r_start;
22175 		if (error)
22176 			/* We don't log or do anything with errors */
22177 			goto nomore;
22178 		if (doing_tlp == 0) {
22179 			if (rsm == NULL) {
22180 				/*
22181 				 * Not a retransmission of some
22182 				 * sort, new data is going out so
22183 				 * clear our TLP count and flag.
22184 				 */
22185 				rack->rc_tlp_in_progress = 0;
22186 				rack->r_ctl.rc_tlp_cnt_out = 0;
22187 			}
22188 		} else {
22189 			/*
22190 			 * We have just sent a TLP, mark that it is true
22191 			 * and make sure our in progress is set so we
22192 			 * continue to check the count.
22193 			 */
22194 			rack->rc_tlp_in_progress = 1;
22195 			rack->r_ctl.rc_tlp_cnt_out++;
22196 		}
22197 		/*
22198 		 * If we are retransmitting we are done, snd_max
22199 		 * does not get updated.
22200 		 */
22201 		if (sack_rxmit)
22202 			goto nomore;
22203 		if ((tp->snd_una == tp->snd_max) && (len > 0)) {
22204 			/*
22205 			 * Update the time we just added data since
22206 			 * nothing was outstanding.
22207 			 */
22208 			rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
22209 			tp->t_acktime = ticks;
22210 		}
22211 		/*
22212 		 * Now for special SYN/FIN handling.
22213 		 */
22214 		if (flags & (TH_SYN | TH_FIN)) {
22215 			if ((flags & TH_SYN) &&
22216 			    ((tp->t_flags & TF_SENTSYN) == 0)) {
22217 				tp->snd_max++;
22218 				tp->t_flags |= TF_SENTSYN;
22219 			}
22220 			if ((flags & TH_FIN) &&
22221 			    ((tp->t_flags & TF_SENTFIN) == 0)) {
22222 				tp->snd_max++;
22223 				tp->t_flags |= TF_SENTFIN;
22224 			}
22225 		}
22226 		tp->snd_max += len;
22227 		if (rack->rc_new_rnd_needed) {
22228 			rack_new_round_starts(tp, rack, tp->snd_max);
22229 		}
22230 		/*
22231 		 * Time this transmission if not a retransmission and
22232 		 * not currently timing anything.
22233 		 * This is only relevant in case of switching back to
22234 		 * the base stack.
22235 		 */
22236 		if (tp->t_rtttime == 0) {
22237 			tp->t_rtttime = ticks;
22238 			tp->t_rtseq = startseq;
22239 			KMOD_TCPSTAT_INC(tcps_segstimed);
22240 		}
22241 		if (len &&
22242 		    ((tp->t_flags & TF_GPUTINPROG) == 0))
22243 			rack_start_gp_measurement(tp, rack, startseq, sb_offset);
22244 		/*
22245 		 * If we are doing FO we need to update the mbuf position and subtract
22246 		 * this happens when the peer sends us duplicate information and
22247 		 * we thus want to send a DSACK.
22248 		 *
22249 		 * XXXRRS: This brings to mind a ?, when we send a DSACK block is TSO
22250 		 * turned off? If not then we are going to echo multiple DSACK blocks
22251 		 * out (with the TSO), which we should not be doing.
22252 		 */
22253 		if (rack->r_fast_output && len) {
22254 			if (rack->r_ctl.fsb.left_to_send > len)
22255 				rack->r_ctl.fsb.left_to_send -= len;
22256 			else
22257 				rack->r_ctl.fsb.left_to_send = 0;
22258 			if (rack->r_ctl.fsb.left_to_send < segsiz)
22259 				rack->r_fast_output = 0;
22260 			if (rack->r_fast_output) {
22261 				rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
22262 				rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
22263 				rack->r_ctl.fsb.o_t_len = M_TRAILINGROOM(rack->r_ctl.fsb.m);
22264 			}
22265 		}
22266 		if (rack_pcm_blast == 0) {
22267 			if ((orig_len > len) &&
22268 			    (add_flag & RACK_IS_PCM) &&
22269 			    (len < pace_max_seg) &&
22270 			    ((pace_max_seg - len) > segsiz)) {
22271 				/*
22272 				 * We are doing a PCM measurement and we did
22273 				 * not get enough data in the TSO to meet the
22274 				 * burst requirement.
22275 				 */
22276 				uint32_t n_len;
22277 
22278 				n_len = (orig_len - len);
22279 				orig_len -= len;
22280 				pace_max_seg -= len;
22281 				len = n_len;
22282 				sb_offset = tp->snd_max - tp->snd_una;
22283 				/* Re-lock for the next spin */
22284 				SOCK_SENDBUF_LOCK(so);
22285 				goto send;
22286 			}
22287 		} else {
22288 			if ((orig_len > len) &&
22289 			    (add_flag & RACK_IS_PCM) &&
22290 			    ((orig_len - len) > segsiz)) {
22291 				/*
22292 				 * We are doing a PCM measurement and we did
22293 				 * not get enough data in the TSO to meet the
22294 				 * burst requirement.
22295 				 */
22296 				uint32_t n_len;
22297 
22298 				n_len = (orig_len - len);
22299 				orig_len -= len;
22300 				len = n_len;
22301 				sb_offset = tp->snd_max - tp->snd_una;
22302 				/* Re-lock for the next spin */
22303 				SOCK_SENDBUF_LOCK(so);
22304 				goto send;
22305 			}
22306 		}
22307 	}
22308 nomore:
22309 	if (error) {
22310 		rack->r_ctl.rc_agg_delayed = 0;
22311 		rack->r_early = 0;
22312 		rack->r_late = 0;
22313 		rack->r_ctl.rc_agg_early = 0;
22314 		SOCKBUF_UNLOCK_ASSERT(sb);	/* Check gotos. */
22315 		/*
22316 		 * Failures do not advance the seq counter above. For the
22317 		 * case of ENOBUFS we will fall out and retry in 1ms with
22318 		 * the hpts. Everything else will just have to retransmit
22319 		 * with the timer.
22320 		 *
22321 		 * In any case, we do not want to loop around for another
22322 		 * send without a good reason.
22323 		 */
22324 		sendalot = 0;
22325 		switch (error) {
22326 		case EPERM:
22327 		case EACCES:
22328 			tp->t_softerror = error;
22329 #ifdef TCP_ACCOUNTING
22330 			crtsc = get_cyclecount();
22331 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22332 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
22333 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
22334 			}
22335 			sched_unpin();
22336 #endif
22337 			return (error);
22338 		case ENOBUFS:
22339 			/*
22340 			 * Pace us right away to retry in a some
22341 			 * time
22342 			 */
22343 			if (rack->r_ctl.crte != NULL) {
22344 				tcp_trace_point(rack->rc_tp, TCP_TP_HWENOBUF);
22345 				if (tcp_bblogging_on(rack->rc_tp))
22346 					rack_log_queue_level(tp, rack, len, &tv, cts);
22347 			} else
22348 				tcp_trace_point(rack->rc_tp, TCP_TP_ENOBUF);
22349 			slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC);
22350 			if (rack->rc_enobuf < 0x7f)
22351 				rack->rc_enobuf++;
22352 			if (slot < (10 * HPTS_USEC_IN_MSEC))
22353 				slot = 10 * HPTS_USEC_IN_MSEC;
22354 			if (rack->r_ctl.crte != NULL) {
22355 				counter_u64_add(rack_saw_enobuf_hw, 1);
22356 				tcp_rl_log_enobuf(rack->r_ctl.crte);
22357 			}
22358 			counter_u64_add(rack_saw_enobuf, 1);
22359 			goto enobufs;
22360 		case EMSGSIZE:
22361 			/*
22362 			 * For some reason the interface we used initially
22363 			 * to send segments changed to another or lowered
22364 			 * its MTU. If TSO was active we either got an
22365 			 * interface without TSO capabilits or TSO was
22366 			 * turned off. If we obtained mtu from ip_output()
22367 			 * then update it and try again.
22368 			 */
22369 			if (tso)
22370 				tp->t_flags &= ~TF_TSO;
22371 			if (mtu != 0) {
22372 				int saved_mtu;
22373 
22374 				saved_mtu = tp->t_maxseg;
22375 				tcp_mss_update(tp, -1, mtu, NULL, NULL);
22376 				if (saved_mtu > tp->t_maxseg) {
22377 					goto again;
22378 				}
22379 			}
22380 			slot = 10 * HPTS_USEC_IN_MSEC;
22381 			rack_start_hpts_timer(rack, tp, cts, slot, 0, 0);
22382 #ifdef TCP_ACCOUNTING
22383 			crtsc = get_cyclecount();
22384 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22385 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
22386 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
22387 			}
22388 			sched_unpin();
22389 #endif
22390 			return (error);
22391 		case ENETUNREACH:
22392 			counter_u64_add(rack_saw_enetunreach, 1);
22393 			/* FALLTHROUGH */
22394 		case EHOSTDOWN:
22395 		case EHOSTUNREACH:
22396 		case ENETDOWN:
22397 			if (TCPS_HAVERCVDSYN(tp->t_state)) {
22398 				tp->t_softerror = error;
22399 				error = 0;
22400 			}
22401 			/* FALLTHROUGH */
22402 		default:
22403 			slot = 10 * HPTS_USEC_IN_MSEC;
22404 			rack_start_hpts_timer(rack, tp, cts, slot, 0, 0);
22405 #ifdef TCP_ACCOUNTING
22406 			crtsc = get_cyclecount();
22407 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22408 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
22409 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
22410 			}
22411 			sched_unpin();
22412 #endif
22413 			return (error);
22414 		}
22415 	} else {
22416 		rack->rc_enobuf = 0;
22417 		if (IN_FASTRECOVERY(tp->t_flags) && rsm)
22418 			rack->r_ctl.retran_during_recovery += len;
22419 	}
22420 	KMOD_TCPSTAT_INC(tcps_sndtotal);
22421 
22422 	/*
22423 	 * Data sent (as far as we can tell). If this advertises a larger
22424 	 * window than any other segment, then remember the size of the
22425 	 * advertised window. Any pending ACK has now been sent.
22426 	 */
22427 	if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
22428 		tp->rcv_adv = tp->rcv_nxt + recwin;
22429 
22430 	tp->last_ack_sent = tp->rcv_nxt;
22431 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
22432 enobufs:
22433 	if (sendalot) {
22434 		/* Do we need to turn off sendalot? */
22435 		if (pace_max_seg &&
22436 		    (tot_len_this_send >= pace_max_seg)) {
22437 			/* We hit our max. */
22438 			sendalot = 0;
22439 		}
22440 	}
22441 	if ((error == 0) && (flags & TH_FIN))
22442 		tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN);
22443 	if (flags & TH_RST) {
22444 		/*
22445 		 * We don't send again after sending a RST.
22446 		 */
22447 		slot = 0;
22448 		sendalot = 0;
22449 		if (error == 0)
22450 			tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
22451 	} else if ((slot == 0) && (sendalot == 0) && tot_len_this_send) {
22452 		/*
22453 		 * Get our pacing rate, if an error
22454 		 * occurred in sending (ENOBUF) we would
22455 		 * hit the else if with slot preset. Other
22456 		 * errors return.
22457 		 */
22458 		slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, rsm, segsiz, __LINE__);
22459 	}
22460 	/* We have sent clear the flag */
22461 	rack->r_ent_rec_ns = 0;
22462 	if (rack->r_must_retran) {
22463 		if (rsm) {
22464 			rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start);
22465 			if (SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) {
22466 				/*
22467 				 * We have retransmitted all.
22468 				 */
22469 				rack->r_must_retran = 0;
22470 				rack->r_ctl.rc_out_at_rto = 0;
22471 			}
22472 		} else if (SEQ_GEQ(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) {
22473 			/*
22474 			 * Sending new data will also kill
22475 			 * the loop.
22476 			 */
22477 			rack->r_must_retran = 0;
22478 			rack->r_ctl.rc_out_at_rto = 0;
22479 		}
22480 	}
22481 	rack->r_ctl.fsb.recwin = recwin;
22482 	if ((tp->t_flags & (TF_WASCRECOVERY|TF_WASFRECOVERY)) &&
22483 	    SEQ_GT(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) {
22484 		/*
22485 		 * We hit an RTO and now have past snd_max at the RTO
22486 		 * clear all the WAS flags.
22487 		 */
22488 		tp->t_flags &= ~(TF_WASCRECOVERY|TF_WASFRECOVERY);
22489 	}
22490 	if (slot) {
22491 		/* set the rack tcb into the slot N */
22492 		if ((error == 0) &&
22493 		    rack_use_rfo &&
22494 		    ((flags & (TH_SYN|TH_FIN)) == 0) &&
22495 		    (rsm == NULL) &&
22496 		    (ipoptlen == 0) &&
22497 		    rack->r_fsb_inited &&
22498 		    TCPS_HAVEESTABLISHED(tp->t_state) &&
22499 		    ((IN_RECOVERY(tp->t_flags)) == 0) &&
22500 		    (rack->r_must_retran == 0) &&
22501 		    ((tp->t_flags & TF_NEEDFIN) == 0) &&
22502 		    (len > 0) && (orig_len > 0) &&
22503 		    (orig_len > len) &&
22504 		    ((orig_len - len) >= segsiz) &&
22505 		    ((optlen == 0) ||
22506 		     ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
22507 			/* We can send at least one more MSS using our fsb */
22508 			rack_setup_fast_output(tp, rack, sb, len, orig_len,
22509 					       segsiz, pace_max_seg, hw_tls, flags);
22510 		} else
22511 			rack->r_fast_output = 0;
22512 		rack_log_fsb(rack, tp, so, flags,
22513 			     ipoptlen, orig_len, len, error,
22514 			     (rsm == NULL), optlen, __LINE__, 2);
22515 	} else if (sendalot) {
22516 		int ret;
22517 
22518 		sack_rxmit = 0;
22519 		if ((error == 0) &&
22520 		    rack_use_rfo &&
22521 		    ((flags & (TH_SYN|TH_FIN)) == 0) &&
22522 		    (rsm == NULL) &&
22523 		    (ipoptlen == 0) &&
22524 		    (rack->r_must_retran == 0) &&
22525 		    rack->r_fsb_inited &&
22526 		    TCPS_HAVEESTABLISHED(tp->t_state) &&
22527 		    ((IN_RECOVERY(tp->t_flags)) == 0) &&
22528 		    ((tp->t_flags & TF_NEEDFIN) == 0) &&
22529 		    (len > 0) && (orig_len > 0) &&
22530 		    (orig_len > len) &&
22531 		    ((orig_len - len) >= segsiz) &&
22532 		    ((optlen == 0) ||
22533 		     ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
22534 			/* we can use fast_output for more */
22535 			rack_setup_fast_output(tp, rack, sb, len, orig_len,
22536 					       segsiz, pace_max_seg, hw_tls, flags);
22537 			if (rack->r_fast_output) {
22538 				error = 0;
22539 				ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error);
22540 				if (ret >= 0)
22541 					return (ret);
22542 			        else if (error)
22543 					goto nomore;
22544 
22545 			}
22546 		}
22547 		goto again;
22548 	}
22549 skip_all_send:
22550 	/* Assure when we leave that snd_nxt will point to top */
22551 	if (SEQ_GT(tp->snd_max, tp->snd_nxt))
22552 		tp->snd_nxt = tp->snd_max;
22553 	rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, 0);
22554 #ifdef TCP_ACCOUNTING
22555 	crtsc = get_cyclecount() - ts_val;
22556 	if (tot_len_this_send) {
22557 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22558 			tp->tcp_cnt_counters[SND_OUT_DATA]++;
22559 			tp->tcp_proc_time[SND_OUT_DATA] += crtsc;
22560 			tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) /segsiz);
22561 		}
22562 	} else {
22563 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22564 			tp->tcp_cnt_counters[SND_OUT_ACK]++;
22565 			tp->tcp_proc_time[SND_OUT_ACK] += crtsc;
22566 		}
22567 	}
22568 	sched_unpin();
22569 #endif
22570 	if (error == ENOBUFS)
22571 		error = 0;
22572 	return (error);
22573 }
22574 
22575 static void
22576 rack_update_seg(struct tcp_rack *rack)
22577 {
22578 	uint32_t orig_val;
22579 
22580 	orig_val = rack->r_ctl.rc_pace_max_segs;
22581 	rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
22582 	if (orig_val != rack->r_ctl.rc_pace_max_segs)
22583 		rack_log_pacing_delay_calc(rack, 0, 0, orig_val, 0, 0, 15, __LINE__, NULL, 0);
22584 }
22585 
22586 static void
22587 rack_mtu_change(struct tcpcb *tp)
22588 {
22589 	/*
22590 	 * The MSS may have changed
22591 	 */
22592 	struct tcp_rack *rack;
22593 	struct rack_sendmap *rsm;
22594 
22595 	rack = (struct tcp_rack *)tp->t_fb_ptr;
22596 	if (rack->r_ctl.rc_pace_min_segs != ctf_fixed_maxseg(tp)) {
22597 		/*
22598 		 * The MTU has changed we need to resend everything
22599 		 * since all we have sent is lost. We first fix
22600 		 * up the mtu though.
22601 		 */
22602 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
22603 		/* We treat this like a full retransmit timeout without the cwnd adjustment */
22604 		rack_remxt_tmr(tp);
22605 		rack->r_fast_output = 0;
22606 		rack->r_ctl.rc_out_at_rto = ctf_flight_size(tp,
22607 						rack->r_ctl.rc_sacked);
22608 		rack->r_ctl.rc_snd_max_at_rto = tp->snd_max;
22609 		rack->r_must_retran = 1;
22610 		/* Mark all inflight to needing to be rxt'd */
22611 		TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
22612 			rsm->r_flags |= (RACK_MUST_RXT|RACK_PMTU_CHG);
22613 		}
22614 	}
22615 	sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
22616 	/* We don't use snd_nxt to retransmit */
22617 	tp->snd_nxt = tp->snd_max;
22618 }
22619 
22620 static int
22621 rack_set_dgp(struct tcp_rack *rack)
22622 {
22623 	if (rack->dgp_on == 1)
22624 		return(0);
22625 	if ((rack->use_fixed_rate == 1) &&
22626 	    (rack->rc_always_pace == 1)) {
22627 		/*
22628 		 * We are already pacing another
22629 		 * way.
22630 		 */
22631 		return (EBUSY);
22632 	}
22633 	if (rack->rc_always_pace == 1) {
22634 		rack_remove_pacing(rack);
22635 	}
22636 	if (tcp_incr_dgp_pacing_cnt() == 0)
22637 		return (ENOSPC);
22638 	rack->r_ctl.pacing_method |= RACK_DGP_PACING;
22639 	rack->rc_fillcw_apply_discount = 0;
22640 	rack->dgp_on = 1;
22641 	rack->rc_always_pace = 1;
22642 	rack->rc_pace_dnd = 1;
22643 	rack->use_fixed_rate = 0;
22644 	if (rack->gp_ready)
22645 		rack_set_cc_pacing(rack);
22646 	rack->rc_tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
22647 	rack->rack_attempt_hdwr_pace = 0;
22648 	/* rxt settings */
22649 	rack->full_size_rxt = 1;
22650 	rack->shape_rxt_to_pacing_min  = 0;
22651 	/* cmpack=1 */
22652 	rack->r_use_cmp_ack = 1;
22653 	if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state) &&
22654 	    rack->r_use_cmp_ack)
22655 		rack->rc_tp->t_flags2 |= TF2_MBUF_ACKCMP;
22656 	/* scwnd=1 */
22657 	rack->rack_enable_scwnd = 1;
22658 	/* dynamic=100 */
22659 	rack->rc_gp_dyn_mul = 1;
22660 	/* gp_inc_ca */
22661 	rack->r_ctl.rack_per_of_gp_ca = 100;
22662 	/* rrr_conf=3 */
22663 	rack->r_rr_config = 3;
22664 	/* npush=2 */
22665 	rack->r_ctl.rc_no_push_at_mrtt = 2;
22666 	/* fillcw=1 */
22667 	rack->rc_pace_to_cwnd = 1;
22668 	rack->rc_pace_fill_if_rttin_range = 0;
22669 	rack->rtt_limit_mul = 0;
22670 	/* noprr=1 */
22671 	rack->rack_no_prr = 1;
22672 	/* lscwnd=1 */
22673 	rack->r_limit_scw = 1;
22674 	/* gp_inc_rec */
22675 	rack->r_ctl.rack_per_of_gp_rec = 90;
22676 	return (0);
22677 }
22678 
22679 static int
22680 rack_set_profile(struct tcp_rack *rack, int prof)
22681 {
22682 	int err = EINVAL;
22683 	if (prof == 1) {
22684 		/*
22685 		 * Profile 1 is "standard" DGP. It ignores
22686 		 * client buffer level.
22687 		 */
22688 		err = rack_set_dgp(rack);
22689 		if (err)
22690 			return (err);
22691 	} else if (prof == 6) {
22692 		err = rack_set_dgp(rack);
22693 		if (err)
22694 			return (err);
22695 		/*
22696 		 * Profile 6 tweaks DGP so that it will apply to
22697 		 * fill-cw the same settings that profile5 does
22698 		 * to replace DGP. It gets then the max(dgp-rate, fillcw(discounted).
22699 		 */
22700 		rack->rc_fillcw_apply_discount = 1;
22701 	} else if (prof == 0) {
22702 		/* This changes things back to the default settings */
22703 		if (rack->rc_always_pace == 1) {
22704 			rack_remove_pacing(rack);
22705 		} else {
22706 			/* Make sure any stray flags are off */
22707 			rack->dgp_on = 0;
22708 			rack->rc_hybrid_mode = 0;
22709 			rack->use_fixed_rate = 0;
22710 		}
22711 		err = 0;
22712 		if (rack_fill_cw_state)
22713 			rack->rc_pace_to_cwnd = 1;
22714 		else
22715 			rack->rc_pace_to_cwnd = 0;
22716 
22717 		if (rack_pace_every_seg && tcp_can_enable_pacing()) {
22718 			rack->r_ctl.pacing_method |= RACK_REG_PACING;
22719 			rack->rc_always_pace = 1;
22720 			if (rack->rack_hibeta)
22721 				rack_set_cc_pacing(rack);
22722 		} else
22723 			rack->rc_always_pace = 0;
22724 		if (rack_dsack_std_based & 0x1) {
22725 			/* Basically this means all rack timers are at least (srtt + 1/4 srtt) */
22726 			rack->rc_rack_tmr_std_based = 1;
22727 		}
22728 		if (rack_dsack_std_based & 0x2) {
22729 			/* Basically this means  rack timers are extended based on dsack by up to (2 * srtt) */
22730 			rack->rc_rack_use_dsack = 1;
22731 		}
22732 		if (rack_use_cmp_acks)
22733 			rack->r_use_cmp_ack = 1;
22734 		else
22735 			rack->r_use_cmp_ack = 0;
22736 		if (rack_disable_prr)
22737 			rack->rack_no_prr = 1;
22738 		else
22739 			rack->rack_no_prr = 0;
22740 		if (rack_gp_no_rec_chg)
22741 			rack->rc_gp_no_rec_chg = 1;
22742 		else
22743 			rack->rc_gp_no_rec_chg = 0;
22744 		if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack) {
22745 			rack->r_mbuf_queue = 1;
22746 			if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state))
22747 				rack->rc_tp->t_flags2 |= TF2_MBUF_ACKCMP;
22748 			rack->rc_tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
22749 		} else {
22750 			rack->r_mbuf_queue = 0;
22751 			rack->rc_tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
22752 		}
22753 		if (rack_enable_shared_cwnd)
22754 			rack->rack_enable_scwnd = 1;
22755 		else
22756 			rack->rack_enable_scwnd = 0;
22757 		if (rack_do_dyn_mul) {
22758 			/* When dynamic adjustment is on CA needs to start at 100% */
22759 			rack->rc_gp_dyn_mul = 1;
22760 			if (rack_do_dyn_mul >= 100)
22761 				rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul;
22762 		} else {
22763 			rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca;
22764 			rack->rc_gp_dyn_mul = 0;
22765 		}
22766 		rack->r_rr_config = 0;
22767 		rack->r_ctl.rc_no_push_at_mrtt = 0;
22768 		rack->rc_pace_fill_if_rttin_range = 0;
22769 		rack->rtt_limit_mul = 0;
22770 
22771 		if (rack_enable_hw_pacing)
22772 			rack->rack_hdw_pace_ena = 1;
22773 		else
22774 			rack->rack_hdw_pace_ena = 0;
22775 		if (rack_disable_prr)
22776 			rack->rack_no_prr = 1;
22777 		else
22778 			rack->rack_no_prr = 0;
22779 		if (rack_limits_scwnd)
22780 			rack->r_limit_scw  = 1;
22781 		else
22782 			rack->r_limit_scw  = 0;
22783 		rack_init_retransmit_value(rack, rack_rxt_controls);
22784 		err = 0;
22785 	}
22786 	return (err);
22787 }
22788 
22789 static int
22790 rack_add_deferred_option(struct tcp_rack *rack, int sopt_name, uint64_t loptval)
22791 {
22792 	struct deferred_opt_list *dol;
22793 
22794 	dol = malloc(sizeof(struct deferred_opt_list),
22795 		     M_TCPDO, M_NOWAIT|M_ZERO);
22796 	if (dol == NULL) {
22797 		/*
22798 		 * No space yikes -- fail out..
22799 		 */
22800 		return (0);
22801 	}
22802 	dol->optname = sopt_name;
22803 	dol->optval = loptval;
22804 	TAILQ_INSERT_TAIL(&rack->r_ctl.opt_list, dol, next);
22805 	return (1);
22806 }
22807 
22808 static int
22809 process_hybrid_pacing(struct tcp_rack *rack, struct tcp_hybrid_req *hybrid)
22810 {
22811 #ifdef TCP_REQUEST_TRK
22812 	struct tcp_sendfile_track *sft;
22813 	struct timeval tv;
22814 	tcp_seq seq;
22815 	int err;
22816 
22817 	microuptime(&tv);
22818 
22819 	/* Make sure no fixed rate is on */
22820 	rack->use_fixed_rate = 0;
22821 	rack->r_ctl.rc_fixed_pacing_rate_rec = 0;
22822 	rack->r_ctl.rc_fixed_pacing_rate_ca = 0;
22823 	rack->r_ctl.rc_fixed_pacing_rate_ss = 0;
22824 	/* Now allocate or find our entry that will have these settings */
22825 	sft = tcp_req_alloc_req_full(rack->rc_tp, &hybrid->req, tcp_tv_to_lusectick(&tv), 0);
22826 	if (sft == NULL) {
22827 		rack->rc_tp->tcp_hybrid_error++;
22828 		/* no space, where would it have gone? */
22829 		seq = rack->rc_tp->snd_una + rack->rc_tp->t_inpcb.inp_socket->so_snd.sb_ccc;
22830 		rack_log_hybrid(rack, seq, NULL, HYBRID_LOG_NO_ROOM, __LINE__, 0);
22831 		return (ENOSPC);
22832 	}
22833 	/* mask our internal flags */
22834 	hybrid->hybrid_flags &= TCP_HYBRID_PACING_USER_MASK;
22835 	/* The seq will be snd_una + everything in the buffer */
22836 	seq = sft->start_seq;
22837 	if ((hybrid->hybrid_flags & TCP_HYBRID_PACING_ENABLE) == 0) {
22838 		/* Disabling hybrid pacing */
22839 		if (rack->rc_hybrid_mode) {
22840 			rack_set_profile(rack, 0);
22841 			rack->rc_tp->tcp_hybrid_stop++;
22842 		}
22843 		rack_log_hybrid(rack, seq, sft, HYBRID_LOG_TURNED_OFF, __LINE__, 0);
22844 		return (0);
22845 	}
22846 	if (rack->dgp_on == 0) {
22847 		/*
22848 		 * If we have not yet turned DGP on, do so
22849 		 * now setting pure DGP mode, no buffer level
22850 		 * response.
22851 		 */
22852 		if ((err = rack_set_profile(rack, 1)) != 0){
22853 			/* Failed to turn pacing on */
22854 			rack->rc_tp->tcp_hybrid_error++;
22855 			rack_log_hybrid(rack, seq, sft, HYBRID_LOG_NO_PACING, __LINE__, 0);
22856 			return (err);
22857 		}
22858 	}
22859 	/*
22860 	 * Now we must switch to hybrid mode as well which also
22861 	 * means moving to regular pacing.
22862 	 */
22863 	if (rack->rc_hybrid_mode == 0) {
22864 		/* First time */
22865 		if (tcp_can_enable_pacing()) {
22866 			rack->r_ctl.pacing_method |= RACK_REG_PACING;
22867 			rack->rc_hybrid_mode = 1;
22868 		} else {
22869 			return (ENOSPC);
22870 		}
22871 		if (rack->r_ctl.pacing_method & RACK_DGP_PACING) {
22872 			/*
22873 			 * This should be true.
22874 			 */
22875 			tcp_dec_dgp_pacing_cnt();
22876 			rack->r_ctl.pacing_method &= ~RACK_DGP_PACING;
22877 		}
22878 	}
22879 	/* Now set in our flags */
22880 	sft->hybrid_flags = hybrid->hybrid_flags | TCP_HYBRID_PACING_WASSET;
22881 	if (hybrid->hybrid_flags & TCP_HYBRID_PACING_CSPR)
22882 		sft->cspr = hybrid->cspr;
22883 	else
22884 		sft->cspr = 0;
22885 	if (hybrid->hybrid_flags & TCP_HYBRID_PACING_H_MS)
22886 		sft->hint_maxseg = hybrid->hint_maxseg;
22887 	else
22888 		sft->hint_maxseg = 0;
22889 	rack->rc_tp->tcp_hybrid_start++;
22890 	rack_log_hybrid(rack, seq, sft, HYBRID_LOG_RULES_SET, __LINE__,0);
22891 	return (0);
22892 #else
22893 	return (ENOTSUP);
22894 #endif
22895 }
22896 
22897 static int
22898 rack_stack_information(struct tcpcb *tp, struct stack_specific_info *si)
22899 {
22900 	/* We pulled a SSI info log out what was there */
22901 	si->bytes_transmitted = tp->t_sndbytes;
22902 	si->bytes_retransmitted = tp->t_snd_rxt_bytes;
22903 	return (0);
22904 }
22905 
22906 static int
22907 rack_process_option(struct tcpcb *tp, struct tcp_rack *rack, int sopt_name,
22908 		    uint32_t optval, uint64_t loptval, struct tcp_hybrid_req *hybrid)
22909 
22910 {
22911 	struct epoch_tracker et;
22912 	struct sockopt sopt;
22913 	struct cc_newreno_opts opt;
22914 	uint64_t val;
22915 	int error = 0;
22916 	uint16_t ca, ss;
22917 
22918 	switch (sopt_name) {
22919 	case TCP_RACK_SET_RXT_OPTIONS:
22920 		if (optval <= 2) {
22921 			rack_init_retransmit_value(rack, optval);
22922 		} else {
22923 			/*
22924 			 * You must send in 0, 1 or 2 all else is
22925 			 * invalid.
22926 			 */
22927 			error = EINVAL;
22928 		}
22929 		break;
22930 	case TCP_RACK_DSACK_OPT:
22931 		RACK_OPTS_INC(tcp_rack_dsack_opt);
22932 		if (optval & 0x1) {
22933 			rack->rc_rack_tmr_std_based = 1;
22934 		} else {
22935 			rack->rc_rack_tmr_std_based = 0;
22936 		}
22937 		if (optval & 0x2) {
22938 			rack->rc_rack_use_dsack = 1;
22939 		} else {
22940 			rack->rc_rack_use_dsack = 0;
22941 		}
22942 		rack_log_dsack_event(rack, 5, __LINE__, 0, 0);
22943 		break;
22944 	case TCP_RACK_PACING_DIVISOR:
22945 		RACK_OPTS_INC(tcp_rack_pacing_divisor);
22946 		if (optval == 0) {
22947 			rack->r_ctl.pace_len_divisor = rack_default_pacing_divisor;
22948 		} else {
22949 			if (optval < RL_MIN_DIVISOR)
22950 				rack->r_ctl.pace_len_divisor = RL_MIN_DIVISOR;
22951 			else
22952 				rack->r_ctl.pace_len_divisor = optval;
22953 		}
22954 		break;
22955 	case TCP_RACK_HI_BETA:
22956 		RACK_OPTS_INC(tcp_rack_hi_beta);
22957 		if (optval > 0) {
22958 			rack->rack_hibeta = 1;
22959 			if ((optval >= 50) &&
22960 			    (optval <= 100)) {
22961 				/*
22962 				 * User wants to set a custom beta.
22963 				 */
22964 				rack->r_ctl.saved_hibeta = optval;
22965 				if (rack->rc_pacing_cc_set)
22966 					rack_undo_cc_pacing(rack);
22967 				rack->r_ctl.rc_saved_beta.beta = optval;
22968 			}
22969 			if (rack->rc_pacing_cc_set == 0)
22970 				rack_set_cc_pacing(rack);
22971 		} else {
22972 			rack->rack_hibeta = 0;
22973 			if (rack->rc_pacing_cc_set)
22974 				rack_undo_cc_pacing(rack);
22975 		}
22976 		break;
22977 	case TCP_RACK_PACING_BETA:
22978 		error = EINVAL;
22979 		break;
22980 	case TCP_RACK_TIMER_SLOP:
22981 		RACK_OPTS_INC(tcp_rack_timer_slop);
22982 		rack->r_ctl.timer_slop = optval;
22983 		if (rack->rc_tp->t_srtt) {
22984 			/*
22985 			 * If we have an SRTT lets update t_rxtcur
22986 			 * to have the new slop.
22987 			 */
22988 			RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
22989 					   rack_rto_min, rack_rto_max,
22990 					   rack->r_ctl.timer_slop);
22991 		}
22992 		break;
22993 	case TCP_RACK_PACING_BETA_ECN:
22994 		RACK_OPTS_INC(tcp_rack_beta_ecn);
22995 		if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0) {
22996 			/* This only works for newreno. */
22997 			error = EINVAL;
22998 			break;
22999 		}
23000 		if (rack->rc_pacing_cc_set) {
23001 			/*
23002 			 * Set them into the real CC module
23003 			 * whats in the rack pcb is the old values
23004 			 * to be used on restoral/
23005 			 */
23006 			sopt.sopt_dir = SOPT_SET;
23007 			opt.name = CC_NEWRENO_BETA_ECN;
23008 			opt.val = optval;
23009 			if (CC_ALGO(tp)->ctl_output != NULL)
23010 				error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
23011 			else
23012 				error = ENOENT;
23013 		} else {
23014 			/*
23015 			 * Not pacing yet so set it into our local
23016 			 * rack pcb storage.
23017 			 */
23018 			rack->r_ctl.rc_saved_beta.beta_ecn = optval;
23019 			rack->r_ctl.rc_saved_beta.newreno_flags = CC_NEWRENO_BETA_ECN_ENABLED;
23020 		}
23021 		break;
23022 	case TCP_DEFER_OPTIONS:
23023 		RACK_OPTS_INC(tcp_defer_opt);
23024 		if (optval) {
23025 			if (rack->gp_ready) {
23026 				/* Too late */
23027 				error = EINVAL;
23028 				break;
23029 			}
23030 			rack->defer_options = 1;
23031 		} else
23032 			rack->defer_options = 0;
23033 		break;
23034 	case TCP_RACK_MEASURE_CNT:
23035 		RACK_OPTS_INC(tcp_rack_measure_cnt);
23036 		if (optval && (optval <= 0xff)) {
23037 			rack->r_ctl.req_measurements = optval;
23038 		} else
23039 			error = EINVAL;
23040 		break;
23041 	case TCP_REC_ABC_VAL:
23042 		RACK_OPTS_INC(tcp_rec_abc_val);
23043 		if (optval > 0)
23044 			rack->r_use_labc_for_rec = 1;
23045 		else
23046 			rack->r_use_labc_for_rec = 0;
23047 		break;
23048 	case TCP_RACK_ABC_VAL:
23049 		RACK_OPTS_INC(tcp_rack_abc_val);
23050 		if ((optval > 0) && (optval < 255))
23051 			rack->rc_labc = optval;
23052 		else
23053 			error = EINVAL;
23054 		break;
23055 	case TCP_HDWR_UP_ONLY:
23056 		RACK_OPTS_INC(tcp_pacing_up_only);
23057 		if (optval)
23058 			rack->r_up_only = 1;
23059 		else
23060 			rack->r_up_only = 0;
23061 		break;
23062 	case TCP_FILLCW_RATE_CAP:		/*  URL:fillcw_cap */
23063 		RACK_OPTS_INC(tcp_fillcw_rate_cap);
23064 		rack->r_ctl.fillcw_cap = loptval;
23065 		break;
23066 	case TCP_PACING_RATE_CAP:
23067 		RACK_OPTS_INC(tcp_pacing_rate_cap);
23068 		if ((rack->dgp_on == 1) &&
23069 		    (rack->r_ctl.pacing_method & RACK_DGP_PACING)) {
23070 			/*
23071 			 * If we are doing DGP we need to switch
23072 			 * to using the pacing limit.
23073 			 */
23074 			if (tcp_can_enable_pacing() == 0) {
23075 				error = ENOSPC;
23076 				break;
23077 			}
23078 			/*
23079 			 * Now change up the flags and counts to be correct.
23080 			 */
23081 			rack->r_ctl.pacing_method |= RACK_REG_PACING;
23082 			tcp_dec_dgp_pacing_cnt();
23083 			rack->r_ctl.pacing_method &= ~RACK_DGP_PACING;
23084 		}
23085 		rack->r_ctl.bw_rate_cap = loptval;
23086 		break;
23087 	case TCP_HYBRID_PACING:
23088 		if (hybrid == NULL) {
23089 			error = EINVAL;
23090 			break;
23091 		}
23092 		if (rack->r_ctl.side_chan_dis_mask & HYBRID_DIS_MASK) {
23093 			error = EPERM;
23094 			break;
23095 		}
23096 		error = process_hybrid_pacing(rack, hybrid);
23097 		break;
23098 	case TCP_SIDECHAN_DIS:			/*  URL:scodm */
23099 		if (optval)
23100 			rack->r_ctl.side_chan_dis_mask = optval;
23101 		else
23102 			rack->r_ctl.side_chan_dis_mask = 0;
23103 		break;
23104 	case TCP_RACK_PROFILE:
23105 		RACK_OPTS_INC(tcp_profile);
23106 		error = rack_set_profile(rack, optval);
23107 		break;
23108 	case TCP_USE_CMP_ACKS:
23109 		RACK_OPTS_INC(tcp_use_cmp_acks);
23110 		if ((optval == 0) && (tp->t_flags2 & TF2_MBUF_ACKCMP)) {
23111 			/* You can't turn it off once its on! */
23112 			error = EINVAL;
23113 		} else if ((optval == 1) && (rack->r_use_cmp_ack == 0)) {
23114 			rack->r_use_cmp_ack = 1;
23115 			rack->r_mbuf_queue = 1;
23116 			tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
23117 		}
23118 		if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
23119 			tp->t_flags2 |= TF2_MBUF_ACKCMP;
23120 		break;
23121 	case TCP_SHARED_CWND_TIME_LIMIT:
23122 		RACK_OPTS_INC(tcp_lscwnd);
23123 		if (optval)
23124 			rack->r_limit_scw = 1;
23125 		else
23126 			rack->r_limit_scw = 0;
23127 		break;
23128 	case TCP_RACK_DGP_IN_REC:
23129 		error = EINVAL;
23130 		break;
23131  	case TCP_RACK_PACE_TO_FILL:
23132 		RACK_OPTS_INC(tcp_fillcw);
23133 		if (optval == 0)
23134 			rack->rc_pace_to_cwnd = 0;
23135 		else {
23136 			rack->rc_pace_to_cwnd = 1;
23137 		}
23138 		if ((optval >= rack_gp_rtt_maxmul) &&
23139 		    rack_gp_rtt_maxmul &&
23140 		    (optval < 0xf)) {
23141 			rack->rc_pace_fill_if_rttin_range = 1;
23142 			rack->rtt_limit_mul = optval;
23143 		} else {
23144 			rack->rc_pace_fill_if_rttin_range = 0;
23145 			rack->rtt_limit_mul = 0;
23146 		}
23147 		break;
23148 	case TCP_RACK_NO_PUSH_AT_MAX:
23149 		RACK_OPTS_INC(tcp_npush);
23150 		if (optval == 0)
23151 			rack->r_ctl.rc_no_push_at_mrtt = 0;
23152 		else if (optval < 0xff)
23153 			rack->r_ctl.rc_no_push_at_mrtt = optval;
23154 		else
23155 			error = EINVAL;
23156 		break;
23157 	case TCP_SHARED_CWND_ENABLE:
23158 		RACK_OPTS_INC(tcp_rack_scwnd);
23159 		if (optval == 0)
23160 			rack->rack_enable_scwnd = 0;
23161 		else
23162 			rack->rack_enable_scwnd = 1;
23163 		break;
23164 	case TCP_RACK_MBUF_QUEUE:
23165 		/* Now do we use the LRO mbuf-queue feature */
23166 		RACK_OPTS_INC(tcp_rack_mbufq);
23167 		if (optval || rack->r_use_cmp_ack)
23168 			rack->r_mbuf_queue = 1;
23169 		else
23170 			rack->r_mbuf_queue = 0;
23171 		if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
23172 			tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
23173 		else
23174 			tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
23175 		break;
23176 	case TCP_RACK_NONRXT_CFG_RATE:
23177 		RACK_OPTS_INC(tcp_rack_cfg_rate);
23178 		if (optval == 0)
23179 			rack->rack_rec_nonrxt_use_cr = 0;
23180 		else
23181 			rack->rack_rec_nonrxt_use_cr = 1;
23182 		break;
23183 	case TCP_NO_PRR:
23184 		RACK_OPTS_INC(tcp_rack_noprr);
23185 		if (optval == 0)
23186 			rack->rack_no_prr = 0;
23187 		else if (optval == 1)
23188 			rack->rack_no_prr = 1;
23189 		else if (optval == 2)
23190 			rack->no_prr_addback = 1;
23191 		else
23192 			error = EINVAL;
23193 		break;
23194 	case RACK_CSPR_IS_FCC:			/*  URL:csprisfcc */
23195 		if (optval > 0)
23196 			rack->cspr_is_fcc = 1;
23197 		else
23198 			rack->cspr_is_fcc = 0;
23199 		break;
23200 	case TCP_TIMELY_DYN_ADJ:
23201 		RACK_OPTS_INC(tcp_timely_dyn);
23202 		if (optval == 0)
23203 			rack->rc_gp_dyn_mul = 0;
23204 		else {
23205 			rack->rc_gp_dyn_mul = 1;
23206 			if (optval >= 100) {
23207 				/*
23208 				 * If the user sets something 100 or more
23209 				 * its the gp_ca value.
23210 				 */
23211 				rack->r_ctl.rack_per_of_gp_ca  = optval;
23212 			}
23213 		}
23214 		break;
23215 	case TCP_RACK_DO_DETECTION:
23216 		error = EINVAL;
23217 		break;
23218 	case TCP_RACK_TLP_USE:
23219 		if ((optval < TLP_USE_ID) || (optval > TLP_USE_TWO_TWO)) {
23220 			error = EINVAL;
23221 			break;
23222 		}
23223 		RACK_OPTS_INC(tcp_tlp_use);
23224 		rack->rack_tlp_threshold_use = optval;
23225 		break;
23226 	case TCP_RACK_TLP_REDUCE:
23227 		/* RACK TLP cwnd reduction (bool) */
23228 		RACK_OPTS_INC(tcp_rack_tlp_reduce);
23229 		rack->r_ctl.rc_tlp_cwnd_reduce = optval;
23230 		break;
23231 		/*  Pacing related ones */
23232 	case TCP_RACK_PACE_ALWAYS:
23233 		/*
23234 		 * zero is old rack method, 1 is new
23235 		 * method using a pacing rate.
23236 		 */
23237 		RACK_OPTS_INC(tcp_rack_pace_always);
23238 		if (rack->r_ctl.side_chan_dis_mask & CCSP_DIS_MASK) {
23239 			error = EPERM;
23240 			break;
23241 		}
23242 		if (optval > 0) {
23243 			if (rack->rc_always_pace) {
23244 				error = EALREADY;
23245 				break;
23246 			} else if (tcp_can_enable_pacing()) {
23247 				rack->r_ctl.pacing_method |= RACK_REG_PACING;
23248 				rack->rc_always_pace = 1;
23249 				if (rack->rack_hibeta)
23250 					rack_set_cc_pacing(rack);
23251 			}
23252 			else {
23253 				error = ENOSPC;
23254 				break;
23255 			}
23256 		} else {
23257 			if (rack->rc_always_pace == 1) {
23258 				rack_remove_pacing(rack);
23259 			}
23260 		}
23261 		if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
23262 			tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
23263 		else
23264 			tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
23265 		/* A rate may be set irate or other, if so set seg size */
23266 		rack_update_seg(rack);
23267 		break;
23268 	case TCP_BBR_RACK_INIT_RATE:
23269 		RACK_OPTS_INC(tcp_initial_rate);
23270 		val = optval;
23271 		/* Change from kbits per second to bytes per second */
23272 		val *= 1000;
23273 		val /= 8;
23274 		rack->r_ctl.init_rate = val;
23275 		if (rack->rc_always_pace)
23276 			rack_update_seg(rack);
23277 		break;
23278 	case TCP_BBR_IWINTSO:
23279 		error = EINVAL;
23280 		break;
23281 	case TCP_RACK_FORCE_MSEG:
23282 		RACK_OPTS_INC(tcp_rack_force_max_seg);
23283 		if (optval)
23284 			rack->rc_force_max_seg = 1;
23285 		else
23286 			rack->rc_force_max_seg = 0;
23287 		break;
23288 	case TCP_RACK_PACE_MIN_SEG:
23289 		RACK_OPTS_INC(tcp_rack_min_seg);
23290 		rack->r_ctl.rc_user_set_min_segs = (0x0000ffff & optval);
23291 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
23292 		break;
23293 	case TCP_RACK_PACE_MAX_SEG:
23294 		/* Max segments size in a pace in bytes */
23295 		RACK_OPTS_INC(tcp_rack_max_seg);
23296 		if ((rack->dgp_on == 1) &&
23297 		    (rack->r_ctl.pacing_method & RACK_DGP_PACING)) {
23298 			/*
23299 			 * If we set a max-seg and are doing DGP then
23300 			 * we now fall under the pacing limits not the
23301 			 * DGP ones.
23302 			 */
23303 			if (tcp_can_enable_pacing() == 0) {
23304 				error = ENOSPC;
23305 				break;
23306 			}
23307 			/*
23308 			 * Now change up the flags and counts to be correct.
23309 			 */
23310 			rack->r_ctl.pacing_method |= RACK_REG_PACING;
23311 			tcp_dec_dgp_pacing_cnt();
23312 			rack->r_ctl.pacing_method &= ~RACK_DGP_PACING;
23313 		}
23314 		if (optval <= MAX_USER_SET_SEG)
23315 			rack->rc_user_set_max_segs = optval;
23316 		else
23317 			rack->rc_user_set_max_segs = MAX_USER_SET_SEG;
23318 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
23319 		break;
23320 	case TCP_RACK_PACE_RATE_REC:
23321 		/* Set the fixed pacing rate in Bytes per second ca */
23322 		RACK_OPTS_INC(tcp_rack_pace_rate_rec);
23323 		if (rack->r_ctl.side_chan_dis_mask & CCSP_DIS_MASK) {
23324 			error = EPERM;
23325 			break;
23326 		}
23327 		if (rack->dgp_on) {
23328 			/*
23329 			 * We are already pacing another
23330 			 * way.
23331 			 */
23332 			error = EBUSY;
23333 			break;
23334 		}
23335 		rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
23336 		if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0)
23337 			rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
23338 		if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0)
23339 			rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
23340 		rack->use_fixed_rate = 1;
23341 		if (rack->rack_hibeta)
23342 			rack_set_cc_pacing(rack);
23343 		rack_log_pacing_delay_calc(rack,
23344 					   rack->r_ctl.rc_fixed_pacing_rate_ss,
23345 					   rack->r_ctl.rc_fixed_pacing_rate_ca,
23346 					   rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
23347 					   __LINE__, NULL,0);
23348 		break;
23349 
23350 	case TCP_RACK_PACE_RATE_SS:
23351 		/* Set the fixed pacing rate in Bytes per second ca */
23352 		RACK_OPTS_INC(tcp_rack_pace_rate_ss);
23353 		if (rack->r_ctl.side_chan_dis_mask & CCSP_DIS_MASK) {
23354 			error = EPERM;
23355 			break;
23356 		}
23357 		if (rack->dgp_on) {
23358 			/*
23359 			 * We are already pacing another
23360 			 * way.
23361 			 */
23362 			error = EBUSY;
23363 			break;
23364 		}
23365 		rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
23366 		if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0)
23367 			rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
23368 		if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0)
23369 			rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
23370 		rack->use_fixed_rate = 1;
23371 		if (rack->rack_hibeta)
23372 			rack_set_cc_pacing(rack);
23373 		rack_log_pacing_delay_calc(rack,
23374 					   rack->r_ctl.rc_fixed_pacing_rate_ss,
23375 					   rack->r_ctl.rc_fixed_pacing_rate_ca,
23376 					   rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
23377 					   __LINE__, NULL, 0);
23378 		break;
23379 
23380 	case TCP_RACK_PACE_RATE_CA:
23381 		/* Set the fixed pacing rate in Bytes per second ca */
23382 		RACK_OPTS_INC(tcp_rack_pace_rate_ca);
23383 		if (rack->r_ctl.side_chan_dis_mask & CCSP_DIS_MASK) {
23384 			error = EPERM;
23385 			break;
23386 		}
23387 		if (rack->dgp_on) {
23388 			/*
23389 			 * We are already pacing another
23390 			 * way.
23391 			 */
23392 			error = EBUSY;
23393 			break;
23394 		}
23395 		rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
23396 		if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0)
23397 			rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
23398 		if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0)
23399 			rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
23400 		rack->use_fixed_rate = 1;
23401 		if (rack->rack_hibeta)
23402 			rack_set_cc_pacing(rack);
23403 		rack_log_pacing_delay_calc(rack,
23404 					   rack->r_ctl.rc_fixed_pacing_rate_ss,
23405 					   rack->r_ctl.rc_fixed_pacing_rate_ca,
23406 					   rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
23407 					   __LINE__, NULL, 0);
23408 		break;
23409 	case TCP_RACK_GP_INCREASE_REC:
23410 		RACK_OPTS_INC(tcp_gp_inc_rec);
23411 		rack->r_ctl.rack_per_of_gp_rec = optval;
23412 		rack_log_pacing_delay_calc(rack,
23413 					   rack->r_ctl.rack_per_of_gp_ss,
23414 					   rack->r_ctl.rack_per_of_gp_ca,
23415 					   rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
23416 					   __LINE__, NULL, 0);
23417 		break;
23418 	case TCP_RACK_GP_INCREASE_CA:
23419 		RACK_OPTS_INC(tcp_gp_inc_ca);
23420 		ca = optval;
23421 		if (ca < 100) {
23422 			/*
23423 			 * We don't allow any reduction
23424 			 * over the GP b/w.
23425 			 */
23426 			error = EINVAL;
23427 			break;
23428 		}
23429 		rack->r_ctl.rack_per_of_gp_ca = ca;
23430 		rack_log_pacing_delay_calc(rack,
23431 					   rack->r_ctl.rack_per_of_gp_ss,
23432 					   rack->r_ctl.rack_per_of_gp_ca,
23433 					   rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
23434 					   __LINE__, NULL, 0);
23435 		break;
23436 	case TCP_RACK_GP_INCREASE_SS:
23437 		RACK_OPTS_INC(tcp_gp_inc_ss);
23438 		ss = optval;
23439 		if (ss < 100) {
23440 			/*
23441 			 * We don't allow any reduction
23442 			 * over the GP b/w.
23443 			 */
23444 			error = EINVAL;
23445 			break;
23446 		}
23447 		rack->r_ctl.rack_per_of_gp_ss = ss;
23448 		rack_log_pacing_delay_calc(rack,
23449 					   rack->r_ctl.rack_per_of_gp_ss,
23450 					   rack->r_ctl.rack_per_of_gp_ca,
23451 					   rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
23452 					   __LINE__, NULL, 0);
23453 		break;
23454 	case TCP_RACK_RR_CONF:
23455 		RACK_OPTS_INC(tcp_rack_rrr_no_conf_rate);
23456 		if (optval && optval <= 3)
23457 			rack->r_rr_config = optval;
23458 		else
23459 			rack->r_rr_config = 0;
23460 		break;
23461 	case TCP_PACING_DND:			/*  URL:dnd */
23462 		if (optval > 0)
23463 			rack->rc_pace_dnd = 1;
23464 		else
23465 			rack->rc_pace_dnd = 0;
23466 		break;
23467 	case TCP_HDWR_RATE_CAP:
23468 		RACK_OPTS_INC(tcp_hdwr_rate_cap);
23469 		if (optval) {
23470 			if (rack->r_rack_hw_rate_caps == 0)
23471 				rack->r_rack_hw_rate_caps = 1;
23472 			else
23473 				error = EALREADY;
23474 		} else {
23475 			rack->r_rack_hw_rate_caps = 0;
23476 		}
23477 		break;
23478 	case TCP_DGP_UPPER_BOUNDS:
23479 	{
23480 		uint8_t val;
23481 		val = optval & 0x0000ff;
23482 		rack->r_ctl.rack_per_upper_bound_ca = val;
23483 		val = (optval >> 16) & 0x0000ff;
23484 		rack->r_ctl.rack_per_upper_bound_ss = val;
23485 		break;
23486 	}
23487 	case TCP_SS_EEXIT:			/*  URL:eexit */
23488 		if (optval > 0) {
23489 			rack->r_ctl.gp_rnd_thresh =  optval & 0x0ff;
23490 			if (optval & 0x10000) {
23491 				rack->r_ctl.gate_to_fs = 1;
23492 			} else {
23493 				rack->r_ctl.gate_to_fs = 0;
23494 			}
23495 			if (optval & 0x20000) {
23496 				rack->r_ctl.use_gp_not_last = 1;
23497 			} else {
23498 				rack->r_ctl.use_gp_not_last = 0;
23499 			}
23500 			if (optval & 0xfffc0000) {
23501 				uint32_t v;
23502 
23503 				v = (optval >> 18) & 0x00003fff;
23504 				if (v >= 1000)
23505 					rack->r_ctl.gp_gain_req = v;
23506 			}
23507 		} else {
23508 			/* We do not do ss early exit at all */
23509 			rack->rc_initial_ss_comp = 1;
23510 			rack->r_ctl.gp_rnd_thresh = 0;
23511 		}
23512 		break;
23513 	case TCP_RACK_SPLIT_LIMIT:
23514 		RACK_OPTS_INC(tcp_split_limit);
23515 		rack->r_ctl.rc_split_limit = optval;
23516 		break;
23517 	case TCP_BBR_HDWR_PACE:
23518 		RACK_OPTS_INC(tcp_hdwr_pacing);
23519 		if (optval){
23520 			if (rack->rack_hdrw_pacing == 0) {
23521 				rack->rack_hdw_pace_ena = 1;
23522 				rack->rack_attempt_hdwr_pace = 0;
23523 			} else
23524 				error = EALREADY;
23525 		} else {
23526 			rack->rack_hdw_pace_ena = 0;
23527 #ifdef RATELIMIT
23528 			if (rack->r_ctl.crte != NULL) {
23529 				rack->rack_hdrw_pacing = 0;
23530 				rack->rack_attempt_hdwr_pace = 0;
23531 				tcp_rel_pacing_rate(rack->r_ctl.crte, tp);
23532 				rack->r_ctl.crte = NULL;
23533 			}
23534 #endif
23535 		}
23536 		break;
23537 		/*  End Pacing related ones */
23538 	case TCP_RACK_PRR_SENDALOT:
23539 		/* Allow PRR to send more than one seg */
23540 		RACK_OPTS_INC(tcp_rack_prr_sendalot);
23541 		rack->r_ctl.rc_prr_sendalot = optval;
23542 		break;
23543 	case TCP_RACK_MIN_TO:
23544 		/* Minimum time between rack t-o's in ms */
23545 		RACK_OPTS_INC(tcp_rack_min_to);
23546 		rack->r_ctl.rc_min_to = optval;
23547 		break;
23548 	case TCP_RACK_EARLY_SEG:
23549 		/* If early recovery max segments */
23550 		RACK_OPTS_INC(tcp_rack_early_seg);
23551 		rack->r_ctl.rc_early_recovery_segs = optval;
23552 		break;
23553 	case TCP_RACK_ENABLE_HYSTART:
23554 	{
23555 		if (optval) {
23556 			tp->t_ccv.flags |= CCF_HYSTART_ALLOWED;
23557 			if (rack_do_hystart > RACK_HYSTART_ON)
23558 				tp->t_ccv.flags |= CCF_HYSTART_CAN_SH_CWND;
23559 			if (rack_do_hystart > RACK_HYSTART_ON_W_SC)
23560 				tp->t_ccv.flags |= CCF_HYSTART_CONS_SSTH;
23561 		} else {
23562 			tp->t_ccv.flags &= ~(CCF_HYSTART_ALLOWED|CCF_HYSTART_CAN_SH_CWND|CCF_HYSTART_CONS_SSTH);
23563 		}
23564 	}
23565 	break;
23566 	case TCP_RACK_REORD_THRESH:
23567 		/* RACK reorder threshold (shift amount) */
23568 		RACK_OPTS_INC(tcp_rack_reord_thresh);
23569 		if ((optval > 0) && (optval < 31))
23570 			rack->r_ctl.rc_reorder_shift = optval;
23571 		else
23572 			error = EINVAL;
23573 		break;
23574 	case TCP_RACK_REORD_FADE:
23575 		/* Does reordering fade after ms time */
23576 		RACK_OPTS_INC(tcp_rack_reord_fade);
23577 		rack->r_ctl.rc_reorder_fade = optval;
23578 		break;
23579 	case TCP_RACK_TLP_THRESH:
23580 		/* RACK TLP theshold i.e. srtt+(srtt/N) */
23581 		RACK_OPTS_INC(tcp_rack_tlp_thresh);
23582 		if (optval)
23583 			rack->r_ctl.rc_tlp_threshold = optval;
23584 		else
23585 			error = EINVAL;
23586 		break;
23587 	case TCP_BBR_USE_RACK_RR:
23588 		RACK_OPTS_INC(tcp_rack_rr);
23589 		if (optval)
23590 			rack->use_rack_rr = 1;
23591 		else
23592 			rack->use_rack_rr = 0;
23593 		break;
23594 	case TCP_RACK_PKT_DELAY:
23595 		/* RACK added ms i.e. rack-rtt + reord + N */
23596 		RACK_OPTS_INC(tcp_rack_pkt_delay);
23597 		rack->r_ctl.rc_pkt_delay = optval;
23598 		break;
23599 	case TCP_DELACK:
23600 		RACK_OPTS_INC(tcp_rack_delayed_ack);
23601 		if (optval == 0)
23602 			tp->t_delayed_ack = 0;
23603 		else
23604 			tp->t_delayed_ack = 1;
23605 		if (tp->t_flags & TF_DELACK) {
23606 			tp->t_flags &= ~TF_DELACK;
23607 			tp->t_flags |= TF_ACKNOW;
23608 			NET_EPOCH_ENTER(et);
23609 			rack_output(tp);
23610 			NET_EPOCH_EXIT(et);
23611 		}
23612 		break;
23613 
23614 	case TCP_BBR_RACK_RTT_USE:
23615 		RACK_OPTS_INC(tcp_rack_rtt_use);
23616 		if ((optval != USE_RTT_HIGH) &&
23617 		    (optval != USE_RTT_LOW) &&
23618 		    (optval != USE_RTT_AVG))
23619 			error = EINVAL;
23620 		else
23621 			rack->r_ctl.rc_rate_sample_method = optval;
23622 		break;
23623 	case TCP_HONOR_HPTS_MIN:
23624 		RACK_OPTS_INC(tcp_honor_hpts);
23625 		if (optval) {
23626 			rack->r_use_hpts_min = 1;
23627 			/*
23628 			 * Must be between 2 - 80% to be a reduction else
23629 			 * we keep the default (10%).
23630 			 */
23631 			if ((optval > 1) && (optval <= 80)) {
23632 				rack->r_ctl.max_reduction = optval;
23633 			}
23634 		} else
23635 			rack->r_use_hpts_min = 0;
23636 		break;
23637 	case TCP_REC_IS_DYN:			/*  URL:dynrec */
23638 		RACK_OPTS_INC(tcp_dyn_rec);
23639 		if (optval)
23640 			rack->rc_gp_no_rec_chg = 1;
23641 		else
23642 			rack->rc_gp_no_rec_chg = 0;
23643 		break;
23644 	case TCP_NO_TIMELY:
23645 		RACK_OPTS_INC(tcp_notimely);
23646 		if (optval) {
23647 			rack->rc_skip_timely = 1;
23648 			rack->r_ctl.rack_per_of_gp_rec = 90;
23649 			rack->r_ctl.rack_per_of_gp_ca = 100;
23650 			rack->r_ctl.rack_per_of_gp_ss = 250;
23651 		} else {
23652 			rack->rc_skip_timely = 0;
23653 		}
23654 		break;
23655 	case TCP_GP_USE_LTBW:
23656 		if (optval == 0) {
23657 			rack->use_lesser_lt_bw = 0;
23658 			rack->dis_lt_bw = 1;
23659 		} else if (optval == 1) {
23660 			rack->use_lesser_lt_bw = 1;
23661 			rack->dis_lt_bw = 0;
23662 		} else if (optval == 2) {
23663 			rack->use_lesser_lt_bw = 0;
23664 			rack->dis_lt_bw = 0;
23665 		}
23666 		break;
23667 	case TCP_DATA_AFTER_CLOSE:
23668 		RACK_OPTS_INC(tcp_data_after_close);
23669 		if (optval)
23670 			rack->rc_allow_data_af_clo = 1;
23671 		else
23672 			rack->rc_allow_data_af_clo = 0;
23673 		break;
23674 	default:
23675 		break;
23676 	}
23677 	tcp_log_socket_option(tp, sopt_name, optval, error);
23678 	return (error);
23679 }
23680 
23681 static void
23682 rack_inherit(struct tcpcb *tp, struct inpcb *parent)
23683 {
23684 	/*
23685 	 * A new connection has been created (tp) and
23686 	 * the parent is the inpcb given. We want to
23687 	 * apply a read-lock to the parent (we are already
23688 	 * holding a write lock on the tp) and copy anything
23689 	 * out of the rack specific data as long as its tfb is
23690 	 * the same as ours i.e. we are the same stack. Otherwise
23691 	 * we just return.
23692 	 */
23693 	struct tcpcb *par;
23694 	struct tcp_rack *dest, *src;
23695 	int cnt = 0;
23696 
23697 	par = intotcpcb(parent);
23698 	if (par->t_fb != tp->t_fb) {
23699 		/* Not the same stack */
23700 		tcp_log_socket_option(tp, 0, 0, 1);
23701 		return;
23702 	}
23703 	/* Ok if we reach here lets setup the two rack pointers */
23704 	dest = (struct tcp_rack *)tp->t_fb_ptr;
23705 	src = (struct tcp_rack *)par->t_fb_ptr;
23706 	if ((src == NULL) || (dest == NULL)) {
23707 		/* Huh? */
23708 		tcp_log_socket_option(tp, 0, 0, 2);
23709 		return;
23710 	}
23711 	/* Now copy out anything we wish to inherit i.e. things in socket-options */
23712 	/* TCP_RACK_PROFILE we can't know but we can set DGP if its on */
23713 	if ((src->dgp_on) && (dest->dgp_on == 0)) {
23714 		/* Profile 1 had to be set via sock opt */
23715 		rack_set_dgp(dest);
23716 		cnt++;
23717 	}
23718 	/* TCP_RACK_SET_RXT_OPTIONS */
23719 	if (dest->full_size_rxt != src->full_size_rxt) {
23720 		dest->full_size_rxt = src->full_size_rxt;
23721 		cnt++;
23722 	}
23723 	if (dest->shape_rxt_to_pacing_min  != src->shape_rxt_to_pacing_min) {
23724 		dest->shape_rxt_to_pacing_min = src->shape_rxt_to_pacing_min;
23725 		cnt++;
23726 	}
23727 	/* TCP_RACK_DSACK_OPT */
23728 	if (dest->rc_rack_tmr_std_based != src->rc_rack_tmr_std_based) {
23729 		dest->rc_rack_tmr_std_based = src->rc_rack_tmr_std_based;
23730 		cnt++;
23731 	}
23732 	if (dest->rc_rack_use_dsack != src->rc_rack_use_dsack) {
23733 		dest->rc_rack_use_dsack = src->rc_rack_use_dsack;
23734 		cnt++;
23735 	}
23736 	/* TCP_RACK_PACING_DIVISOR */
23737 	if (dest->r_ctl.pace_len_divisor != src->r_ctl.pace_len_divisor) {
23738 		dest->r_ctl.pace_len_divisor = src->r_ctl.pace_len_divisor;
23739 		cnt++;
23740 	}
23741 	/* TCP_RACK_HI_BETA */
23742 	if (src->rack_hibeta != dest->rack_hibeta) {
23743 		cnt++;
23744 		if (src->rack_hibeta) {
23745 			dest->r_ctl.rc_saved_beta.beta = src->r_ctl.rc_saved_beta.beta;
23746 			dest->rack_hibeta = 1;
23747 		} else {
23748 			dest->rack_hibeta = 0;
23749 		}
23750 	}
23751 	/* TCP_RACK_TIMER_SLOP */
23752 	if (dest->r_ctl.timer_slop != src->r_ctl.timer_slop) {
23753 		dest->r_ctl.timer_slop = src->r_ctl.timer_slop;
23754 		cnt++;
23755 	}
23756 	/* TCP_RACK_PACING_BETA_ECN */
23757 	if (dest->r_ctl.rc_saved_beta.beta_ecn != src->r_ctl.rc_saved_beta.beta_ecn) {
23758 		dest->r_ctl.rc_saved_beta.beta_ecn = src->r_ctl.rc_saved_beta.beta_ecn;
23759 		cnt++;
23760 	}
23761 	if (dest->r_ctl.rc_saved_beta.newreno_flags != src->r_ctl.rc_saved_beta.newreno_flags) {
23762 		dest->r_ctl.rc_saved_beta.newreno_flags = src->r_ctl.rc_saved_beta.newreno_flags;
23763 		cnt++;
23764 	}
23765 	/* We do not do TCP_DEFER_OPTIONS */
23766 	/* TCP_RACK_MEASURE_CNT */
23767 	if (dest->r_ctl.req_measurements != src->r_ctl.req_measurements) {
23768 		dest->r_ctl.req_measurements = src->r_ctl.req_measurements;
23769 		cnt++;
23770 	}
23771 	/* TCP_HDWR_UP_ONLY */
23772 	if (dest->r_up_only != src->r_up_only) {
23773 		dest->r_up_only = src->r_up_only;
23774 		cnt++;
23775 	}
23776 	/* TCP_FILLCW_RATE_CAP */
23777 	if (dest->r_ctl.fillcw_cap != src->r_ctl.fillcw_cap) {
23778 		dest->r_ctl.fillcw_cap = src->r_ctl.fillcw_cap;
23779 		cnt++;
23780 	}
23781 	/* TCP_PACING_RATE_CAP */
23782 	if (dest->r_ctl.bw_rate_cap != src->r_ctl.bw_rate_cap) {
23783 		dest->r_ctl.bw_rate_cap = src->r_ctl.bw_rate_cap;
23784 		cnt++;
23785 	}
23786 	/* A listener can't set TCP_HYBRID_PACING */
23787 	/* TCP_SIDECHAN_DIS */
23788 	if (dest->r_ctl.side_chan_dis_mask != src->r_ctl.side_chan_dis_mask) {
23789 		dest->r_ctl.side_chan_dis_mask = src->r_ctl.side_chan_dis_mask;
23790 		cnt++;
23791 	}
23792 	/* TCP_SHARED_CWND_TIME_LIMIT */
23793 	if (dest->r_limit_scw != src->r_limit_scw) {
23794 		dest->r_limit_scw = src->r_limit_scw;
23795 		cnt++;
23796 	}
23797 	/* TCP_RACK_PACE_TO_FILL */
23798 	if (dest->rc_pace_to_cwnd != src->rc_pace_to_cwnd) {
23799 		dest->rc_pace_to_cwnd = src->rc_pace_to_cwnd;
23800 		cnt++;
23801 	}
23802 	if (dest->rc_pace_fill_if_rttin_range != src->rc_pace_fill_if_rttin_range) {
23803 		dest->rc_pace_fill_if_rttin_range = src->rc_pace_fill_if_rttin_range;
23804 		cnt++;
23805 	}
23806 	if (dest->rtt_limit_mul != src->rtt_limit_mul) {
23807 		dest->rtt_limit_mul = src->rtt_limit_mul;
23808 		cnt++;
23809 	}
23810 	/* TCP_RACK_NO_PUSH_AT_MAX */
23811 	if (dest->r_ctl.rc_no_push_at_mrtt != src->r_ctl.rc_no_push_at_mrtt) {
23812 		dest->r_ctl.rc_no_push_at_mrtt = src->r_ctl.rc_no_push_at_mrtt;
23813 		cnt++;
23814 	}
23815 	/* TCP_SHARED_CWND_ENABLE */
23816 	if (dest->rack_enable_scwnd != src->rack_enable_scwnd) {
23817 		dest->rack_enable_scwnd = src->rack_enable_scwnd;
23818 		cnt++;
23819 	}
23820 	/* TCP_USE_CMP_ACKS */
23821 	if (dest->r_use_cmp_ack != src->r_use_cmp_ack) {
23822 		dest->r_use_cmp_ack = src->r_use_cmp_ack;
23823 		cnt++;
23824 	}
23825 
23826 	if (dest->r_mbuf_queue != src->r_mbuf_queue) {
23827 		dest->r_mbuf_queue = src->r_mbuf_queue;
23828 		cnt++;
23829 	}
23830 	/* TCP_RACK_MBUF_QUEUE */
23831 	if (dest->r_mbuf_queue != src->r_mbuf_queue) {
23832 		dest->r_mbuf_queue = src->r_mbuf_queue;
23833 		cnt++;
23834 	}
23835 	if  (dest->r_mbuf_queue || dest->rc_always_pace || dest->r_use_cmp_ack) {
23836 		tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
23837 	} else {
23838 		tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
23839 	}
23840 	if (dest->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state)) {
23841 		tp->t_flags2 |= TF2_MBUF_ACKCMP;
23842 	}
23843 	/* TCP_RACK_NONRXT_CFG_RATE */
23844 	if (dest->rack_rec_nonrxt_use_cr != src->rack_rec_nonrxt_use_cr) {
23845 		dest->rack_rec_nonrxt_use_cr = src->rack_rec_nonrxt_use_cr;
23846 		cnt++;
23847 	}
23848 	/* TCP_NO_PRR */
23849 	if (dest->rack_no_prr != src->rack_no_prr) {
23850 		dest->rack_no_prr = src->rack_no_prr;
23851 		cnt++;
23852 	}
23853 	if (dest->no_prr_addback != src->no_prr_addback) {
23854 		dest->no_prr_addback = src->no_prr_addback;
23855 		cnt++;
23856 	}
23857 	/* RACK_CSPR_IS_FCC */
23858 	if (dest->cspr_is_fcc != src->cspr_is_fcc) {
23859 		dest->cspr_is_fcc = src->cspr_is_fcc;
23860 		cnt++;
23861 	}
23862 	/* TCP_TIMELY_DYN_ADJ */
23863 	if (dest->rc_gp_dyn_mul != src->rc_gp_dyn_mul) {
23864 		dest->rc_gp_dyn_mul = src->rc_gp_dyn_mul;
23865 		cnt++;
23866 	}
23867 	if (dest->r_ctl.rack_per_of_gp_ca != src->r_ctl.rack_per_of_gp_ca) {
23868 		dest->r_ctl.rack_per_of_gp_ca = src->r_ctl.rack_per_of_gp_ca;
23869 		cnt++;
23870 	}
23871 	/* TCP_RACK_TLP_USE */
23872 	if (dest->rack_tlp_threshold_use != src->rack_tlp_threshold_use) {
23873 		dest->rack_tlp_threshold_use = src->rack_tlp_threshold_use;
23874 		cnt++;
23875 	}
23876 	/* we don't allow inheritence of TCP_RACK_PACE_ALWAYS */
23877 	/* TCP_BBR_RACK_INIT_RATE */
23878 	if (dest->r_ctl.init_rate != src->r_ctl.init_rate) {
23879 		dest->r_ctl.init_rate = src->r_ctl.init_rate;
23880 		cnt++;
23881 	}
23882 	/* TCP_RACK_FORCE_MSEG */
23883 	if (dest->rc_force_max_seg != src->rc_force_max_seg) {
23884 		dest->rc_force_max_seg = src->rc_force_max_seg;
23885 		cnt++;
23886 	}
23887 	/* TCP_RACK_PACE_MIN_SEG */
23888 	if (dest->r_ctl.rc_user_set_min_segs != src->r_ctl.rc_user_set_min_segs) {
23889 		dest->r_ctl.rc_user_set_min_segs = src->r_ctl.rc_user_set_min_segs;
23890 		cnt++;
23891 	}
23892 	/* we don't allow TCP_RACK_PACE_MAX_SEG */
23893 	/* TCP_RACK_PACE_RATE_REC, TCP_RACK_PACE_RATE_SS,  TCP_RACK_PACE_RATE_CA */
23894 	if (dest->r_ctl.rc_fixed_pacing_rate_ca != src->r_ctl.rc_fixed_pacing_rate_ca) {
23895 		dest->r_ctl.rc_fixed_pacing_rate_ca = src->r_ctl.rc_fixed_pacing_rate_ca;
23896 		cnt++;
23897 	}
23898 	if (dest->r_ctl.rc_fixed_pacing_rate_ss != src->r_ctl.rc_fixed_pacing_rate_ss) {
23899 		dest->r_ctl.rc_fixed_pacing_rate_ss = src->r_ctl.rc_fixed_pacing_rate_ss;
23900 		cnt++;
23901 	}
23902 	if (dest->r_ctl.rc_fixed_pacing_rate_rec != src->r_ctl.rc_fixed_pacing_rate_rec) {
23903 		dest->r_ctl.rc_fixed_pacing_rate_rec = src->r_ctl.rc_fixed_pacing_rate_rec;
23904 		cnt++;
23905 	}
23906 	/* TCP_RACK_GP_INCREASE_REC, TCP_RACK_GP_INCREASE_CA, TCP_RACK_GP_INCREASE_SS */
23907 	if (dest->r_ctl.rack_per_of_gp_rec != src->r_ctl.rack_per_of_gp_rec) {
23908 		dest->r_ctl.rack_per_of_gp_rec = src->r_ctl.rack_per_of_gp_rec;
23909 		cnt++;
23910 	}
23911 	if (dest->r_ctl.rack_per_of_gp_ca != src->r_ctl.rack_per_of_gp_ca) {
23912 		dest->r_ctl.rack_per_of_gp_ca = src->r_ctl.rack_per_of_gp_ca;
23913 		cnt++;
23914 	}
23915 
23916 	if (dest->r_ctl.rack_per_of_gp_ss != src->r_ctl.rack_per_of_gp_ss) {
23917 		dest->r_ctl.rack_per_of_gp_ss = src->r_ctl.rack_per_of_gp_ss;
23918 		cnt++;
23919 	}
23920 	/* TCP_RACK_RR_CONF */
23921 	if (dest->r_rr_config != src->r_rr_config) {
23922 		dest->r_rr_config = src->r_rr_config;
23923 		cnt++;
23924 	}
23925 	/* TCP_PACING_DND */
23926 	if (dest->rc_pace_dnd != src->rc_pace_dnd) {
23927 		dest->rc_pace_dnd = src->rc_pace_dnd;
23928 		cnt++;
23929 	}
23930 	/* TCP_HDWR_RATE_CAP */
23931 	if (dest->r_rack_hw_rate_caps != src->r_rack_hw_rate_caps) {
23932 		dest->r_rack_hw_rate_caps = src->r_rack_hw_rate_caps;
23933 		cnt++;
23934 	}
23935 	/* TCP_DGP_UPPER_BOUNDS */
23936 	if (dest->r_ctl.rack_per_upper_bound_ca != src->r_ctl.rack_per_upper_bound_ca) {
23937 		dest->r_ctl.rack_per_upper_bound_ca = src->r_ctl.rack_per_upper_bound_ca;
23938 		cnt++;
23939 	}
23940 	if (dest->r_ctl.rack_per_upper_bound_ss != src->r_ctl.rack_per_upper_bound_ss) {
23941 		dest->r_ctl.rack_per_upper_bound_ss = src->r_ctl.rack_per_upper_bound_ss;
23942 		cnt++;
23943 	}
23944 	/* TCP_SS_EEXIT */
23945 	if (dest->r_ctl.gp_rnd_thresh != src->r_ctl.gp_rnd_thresh) {
23946 		dest->r_ctl.gp_rnd_thresh = src->r_ctl.gp_rnd_thresh;
23947 		cnt++;
23948 	}
23949 	if (dest->r_ctl.gate_to_fs != src->r_ctl.gate_to_fs) {
23950 		dest->r_ctl.gate_to_fs = src->r_ctl.gate_to_fs;
23951 		cnt++;
23952 	}
23953 	if (dest->r_ctl.use_gp_not_last != src->r_ctl.use_gp_not_last) {
23954 		dest->r_ctl.use_gp_not_last = src->r_ctl.use_gp_not_last;
23955 		cnt++;
23956 	}
23957 	if (dest->r_ctl.gp_gain_req != src->r_ctl.gp_gain_req) {
23958 		dest->r_ctl.gp_gain_req = src->r_ctl.gp_gain_req;
23959 		cnt++;
23960 	}
23961 	/* TCP_BBR_HDWR_PACE */
23962 	if (dest->rack_hdw_pace_ena != src->rack_hdw_pace_ena) {
23963 		dest->rack_hdw_pace_ena = src->rack_hdw_pace_ena;
23964 		cnt++;
23965 	}
23966 	if (dest->rack_attempt_hdwr_pace != src->rack_attempt_hdwr_pace) {
23967 		dest->rack_attempt_hdwr_pace = src->rack_attempt_hdwr_pace;
23968 		cnt++;
23969 	}
23970 	/* TCP_RACK_PRR_SENDALOT */
23971 	if (dest->r_ctl.rc_prr_sendalot != src->r_ctl.rc_prr_sendalot) {
23972 		dest->r_ctl.rc_prr_sendalot = src->r_ctl.rc_prr_sendalot;
23973 		cnt++;
23974 	}
23975 	/* TCP_RACK_MIN_TO */
23976 	if (dest->r_ctl.rc_min_to != src->r_ctl.rc_min_to) {
23977 		dest->r_ctl.rc_min_to = src->r_ctl.rc_min_to;
23978 		cnt++;
23979 	}
23980 	/* TCP_RACK_EARLY_SEG */
23981 	if (dest->r_ctl.rc_early_recovery_segs != src->r_ctl.rc_early_recovery_segs) {
23982 		dest->r_ctl.rc_early_recovery_segs = src->r_ctl.rc_early_recovery_segs;
23983 		cnt++;
23984 	}
23985 	/* TCP_RACK_ENABLE_HYSTART */
23986 	if (par->t_ccv.flags != tp->t_ccv.flags) {
23987 		cnt++;
23988 		if (par->t_ccv.flags & CCF_HYSTART_ALLOWED) {
23989 			tp->t_ccv.flags |= CCF_HYSTART_ALLOWED;
23990 			if (rack_do_hystart > RACK_HYSTART_ON)
23991 				tp->t_ccv.flags |= CCF_HYSTART_CAN_SH_CWND;
23992 			if (rack_do_hystart > RACK_HYSTART_ON_W_SC)
23993 				tp->t_ccv.flags |= CCF_HYSTART_CONS_SSTH;
23994 		} else {
23995 			tp->t_ccv.flags &= ~(CCF_HYSTART_ALLOWED|CCF_HYSTART_CAN_SH_CWND|CCF_HYSTART_CONS_SSTH);
23996 		}
23997 	}
23998 	/* TCP_RACK_REORD_THRESH */
23999 	if (dest->r_ctl.rc_reorder_shift != src->r_ctl.rc_reorder_shift) {
24000 		dest->r_ctl.rc_reorder_shift = src->r_ctl.rc_reorder_shift;
24001 		cnt++;
24002 	}
24003 	/* TCP_RACK_REORD_FADE */
24004 	if (dest->r_ctl.rc_reorder_fade != src->r_ctl.rc_reorder_fade) {
24005 		dest->r_ctl.rc_reorder_fade = src->r_ctl.rc_reorder_fade;
24006 		cnt++;
24007 	}
24008 	/* TCP_RACK_TLP_THRESH */
24009 	if (dest->r_ctl.rc_tlp_threshold != src->r_ctl.rc_tlp_threshold) {
24010 		dest->r_ctl.rc_tlp_threshold = src->r_ctl.rc_tlp_threshold;
24011 		cnt++;
24012 	}
24013 	/* TCP_BBR_USE_RACK_RR */
24014 	if (dest->use_rack_rr != src->use_rack_rr) {
24015 		dest->use_rack_rr = src->use_rack_rr;
24016 		cnt++;
24017 	}
24018 	/* TCP_RACK_PKT_DELAY */
24019 	if (dest->r_ctl.rc_pkt_delay != src->r_ctl.rc_pkt_delay) {
24020 		dest->r_ctl.rc_pkt_delay = src->r_ctl.rc_pkt_delay;
24021 		cnt++;
24022 	}
24023 	/* TCP_DELACK will get copied via the main code if applicable */
24024 	/* TCP_BBR_RACK_RTT_USE */
24025 	if (dest->r_ctl.rc_rate_sample_method != src->r_ctl.rc_rate_sample_method) {
24026 		dest->r_ctl.rc_rate_sample_method = src->r_ctl.rc_rate_sample_method;
24027 		cnt++;
24028 	}
24029 	/* TCP_HONOR_HPTS_MIN */
24030 	if (dest->r_use_hpts_min != src->r_use_hpts_min) {
24031 		dest->r_use_hpts_min = src->r_use_hpts_min;
24032 		cnt++;
24033 	}
24034 	if (dest->r_ctl.max_reduction != src->r_ctl.max_reduction) {
24035 		dest->r_ctl.max_reduction = src->r_ctl.max_reduction;
24036 		cnt++;
24037 	}
24038 	/* TCP_REC_IS_DYN */
24039 	if (dest->rc_gp_no_rec_chg != src->rc_gp_no_rec_chg) {
24040 		dest->rc_gp_no_rec_chg = src->rc_gp_no_rec_chg;
24041 		cnt++;
24042 	}
24043 	if (dest->rc_skip_timely != src->rc_skip_timely) {
24044 		dest->rc_skip_timely = src->rc_skip_timely;
24045 		cnt++;
24046 	}
24047 	/* TCP_DATA_AFTER_CLOSE */
24048 	if (dest->rc_allow_data_af_clo != src->rc_allow_data_af_clo) {
24049 		dest->rc_allow_data_af_clo = src->rc_allow_data_af_clo;
24050 		cnt++;
24051 	}
24052 	/* TCP_GP_USE_LTBW */
24053 	if (src->use_lesser_lt_bw != dest->use_lesser_lt_bw) {
24054 		dest->use_lesser_lt_bw = src->use_lesser_lt_bw;
24055 		cnt++;
24056 	}
24057 	if (dest->dis_lt_bw != src->dis_lt_bw) {
24058 		dest->dis_lt_bw = src->dis_lt_bw;
24059 		cnt++;
24060 	}
24061 	tcp_log_socket_option(tp, 0, cnt, 0);
24062 }
24063 
24064 
24065 static void
24066 rack_apply_deferred_options(struct tcp_rack *rack)
24067 {
24068 	struct deferred_opt_list *dol, *sdol;
24069 	uint32_t s_optval;
24070 
24071 	TAILQ_FOREACH_SAFE(dol, &rack->r_ctl.opt_list, next, sdol) {
24072 		TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next);
24073 		/* Disadvantage of deferal is you loose the error return */
24074 		s_optval = (uint32_t)dol->optval;
24075 		(void)rack_process_option(rack->rc_tp, rack, dol->optname, s_optval, dol->optval, NULL);
24076 		free(dol, M_TCPDO);
24077 	}
24078 }
24079 
24080 static void
24081 rack_hw_tls_change(struct tcpcb *tp, int chg)
24082 {
24083 	/* Update HW tls state */
24084 	struct tcp_rack *rack;
24085 
24086 	rack = (struct tcp_rack *)tp->t_fb_ptr;
24087 	if (chg)
24088 		rack->r_ctl.fsb.hw_tls = 1;
24089 	else
24090 		rack->r_ctl.fsb.hw_tls = 0;
24091 }
24092 
24093 static int
24094 rack_pru_options(struct tcpcb *tp, int flags)
24095 {
24096 	if (flags & PRUS_OOB)
24097 		return (EOPNOTSUPP);
24098 	return (0);
24099 }
24100 
24101 static bool
24102 rack_wake_check(struct tcpcb *tp)
24103 {
24104 	struct tcp_rack *rack;
24105 	struct timeval tv;
24106 	uint32_t cts;
24107 
24108 	rack = (struct tcp_rack *)tp->t_fb_ptr;
24109 	if (rack->r_ctl.rc_hpts_flags) {
24110 		cts = tcp_get_usecs(&tv);
24111 		if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == PACE_PKT_OUTPUT){
24112 			/*
24113 			 * Pacing timer is up, check if we are ready.
24114 			 */
24115 			if (TSTMP_GEQ(cts, rack->r_ctl.rc_last_output_to))
24116 				return (true);
24117 		} else if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) != 0) {
24118 			/*
24119 			 * A timer is up, check if we are ready.
24120 			 */
24121 			if (TSTMP_GEQ(cts, rack->r_ctl.rc_timer_exp))
24122 				return (true);
24123 		}
24124 	}
24125 	return (false);
24126 }
24127 
24128 static struct tcp_function_block __tcp_rack = {
24129 	.tfb_tcp_block_name = __XSTRING(STACKNAME),
24130 	.tfb_tcp_output = rack_output,
24131 	.tfb_do_queued_segments = ctf_do_queued_segments,
24132 	.tfb_do_segment_nounlock = rack_do_segment_nounlock,
24133 	.tfb_tcp_do_segment = rack_do_segment,
24134 	.tfb_tcp_ctloutput = rack_ctloutput,
24135 	.tfb_tcp_fb_init = rack_init,
24136 	.tfb_tcp_fb_fini = rack_fini,
24137 	.tfb_tcp_timer_stop_all = rack_stopall,
24138 	.tfb_tcp_rexmit_tmr = rack_remxt_tmr,
24139 	.tfb_tcp_handoff_ok = rack_handoff_ok,
24140 	.tfb_tcp_mtu_chg = rack_mtu_change,
24141 	.tfb_pru_options = rack_pru_options,
24142 	.tfb_hwtls_change = rack_hw_tls_change,
24143 	.tfb_chg_query = rack_chg_query,
24144 	.tfb_switch_failed = rack_switch_failed,
24145 	.tfb_early_wake_check = rack_wake_check,
24146 	.tfb_compute_pipe = rack_compute_pipe,
24147 	.tfb_stack_info = rack_stack_information,
24148 	.tfb_inherit = rack_inherit,
24149 	.tfb_flags = TCP_FUNC_OUTPUT_CANDROP | TCP_FUNC_DEFAULT_OK,
24150 
24151 };
24152 
24153 /*
24154  * rack_ctloutput() must drop the inpcb lock before performing copyin on
24155  * socket option arguments.  When it re-acquires the lock after the copy, it
24156  * has to revalidate that the connection is still valid for the socket
24157  * option.
24158  */
24159 static int
24160 rack_set_sockopt(struct tcpcb *tp, struct sockopt *sopt)
24161 {
24162 	struct inpcb *inp = tptoinpcb(tp);
24163 #ifdef INET
24164 	struct ip *ip;
24165 #endif
24166 	struct tcp_rack *rack;
24167 	struct tcp_hybrid_req hybrid;
24168 	uint64_t loptval;
24169 	int32_t error = 0, optval;
24170 
24171 	rack = (struct tcp_rack *)tp->t_fb_ptr;
24172 	if (rack == NULL) {
24173 		INP_WUNLOCK(inp);
24174 		return (EINVAL);
24175 	}
24176 #ifdef INET
24177 	ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
24178 #endif
24179 
24180 	switch (sopt->sopt_level) {
24181 #ifdef INET6
24182 	case IPPROTO_IPV6:
24183 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
24184 		switch (sopt->sopt_name) {
24185 		case IPV6_USE_MIN_MTU:
24186 			tcp6_use_min_mtu(tp);
24187 			break;
24188 		}
24189 		INP_WUNLOCK(inp);
24190 		return (0);
24191 #endif
24192 #ifdef INET
24193 	case IPPROTO_IP:
24194 		switch (sopt->sopt_name) {
24195 		case IP_TOS:
24196 			/*
24197 			 * The DSCP codepoint has changed, update the fsb.
24198 			 */
24199 			ip->ip_tos = rack->rc_inp->inp_ip_tos;
24200 			break;
24201 		case IP_TTL:
24202 			/*
24203 			 * The TTL has changed, update the fsb.
24204 			 */
24205 			ip->ip_ttl = rack->rc_inp->inp_ip_ttl;
24206 			break;
24207 		}
24208 		INP_WUNLOCK(inp);
24209 		return (0);
24210 #endif
24211 #ifdef SO_PEERPRIO
24212 	case SOL_SOCKET:
24213 		switch (sopt->sopt_name) {
24214 		case SO_PEERPRIO:			/*  SC-URL:bs */
24215 			/* Already read in and sanity checked in sosetopt(). */
24216 			if (inp->inp_socket) {
24217 				rack->client_bufferlvl = inp->inp_socket->so_peerprio;
24218 			}
24219 			break;
24220 		}
24221 		INP_WUNLOCK(inp);
24222 		return (0);
24223 #endif
24224 	case IPPROTO_TCP:
24225 		switch (sopt->sopt_name) {
24226 		case TCP_RACK_TLP_REDUCE:		/*  URL:tlp_reduce */
24227 		/*  Pacing related ones */
24228 		case TCP_RACK_PACE_ALWAYS:		/*  URL:pace_always */
24229 		case TCP_BBR_RACK_INIT_RATE:		/*  URL:irate */
24230 		case TCP_RACK_PACE_MIN_SEG:		/*  URL:pace_min_seg */
24231 		case TCP_RACK_PACE_MAX_SEG:		/*  URL:pace_max_seg */
24232 		case TCP_RACK_FORCE_MSEG:		/*  URL:force_max_seg */
24233 		case TCP_RACK_PACE_RATE_CA:		/*  URL:pr_ca */
24234 		case TCP_RACK_PACE_RATE_SS:		/*  URL:pr_ss*/
24235 		case TCP_RACK_PACE_RATE_REC:		/*  URL:pr_rec */
24236 		case TCP_RACK_GP_INCREASE_CA:		/*  URL:gp_inc_ca */
24237 		case TCP_RACK_GP_INCREASE_SS:		/*  URL:gp_inc_ss */
24238 		case TCP_RACK_GP_INCREASE_REC:		/*  URL:gp_inc_rec */
24239 		case TCP_RACK_RR_CONF:			/*  URL:rrr_conf */
24240 		case TCP_BBR_HDWR_PACE:			/*  URL:hdwrpace */
24241 		case TCP_HDWR_RATE_CAP:			/*  URL:hdwrcap boolean */
24242 		case TCP_PACING_RATE_CAP:		/*  URL:cap  -- used by side-channel */
24243 		case TCP_HDWR_UP_ONLY:			/*  URL:uponly -- hardware pacing  boolean */
24244 		case TCP_FILLCW_RATE_CAP:		/*  URL:fillcw_cap */
24245 		case TCP_RACK_PACING_BETA_ECN:		/*  URL:pacing_beta_ecn */
24246 		case TCP_RACK_PACE_TO_FILL:		/*  URL:fillcw */
24247 			/* End pacing related */
24248 		case TCP_DELACK:			/*  URL:delack (in base TCP i.e. tcp_hints along with cc etc ) */
24249 		case TCP_RACK_PRR_SENDALOT:		/*  URL:prr_sendalot */
24250 		case TCP_RACK_MIN_TO:			/*  URL:min_to */
24251 		case TCP_RACK_EARLY_SEG:		/*  URL:early_seg */
24252 		case TCP_RACK_REORD_THRESH:		/*  URL:reord_thresh */
24253 		case TCP_RACK_REORD_FADE:		/*  URL:reord_fade */
24254 		case TCP_RACK_TLP_THRESH:		/*  URL:tlp_thresh */
24255 		case TCP_RACK_PKT_DELAY:		/*  URL:pkt_delay */
24256 		case TCP_RACK_TLP_USE:			/*  URL:tlp_use */
24257 		case TCP_BBR_RACK_RTT_USE:		/*  URL:rttuse */
24258 		case TCP_BBR_USE_RACK_RR:		/*  URL:rackrr */
24259 		case TCP_NO_PRR:			/*  URL:noprr */
24260 		case TCP_TIMELY_DYN_ADJ:      		/*  URL:dynamic */
24261 		case TCP_DATA_AFTER_CLOSE:		/*  no URL */
24262 		case TCP_RACK_NONRXT_CFG_RATE:		/*  URL:nonrxtcr */
24263 		case TCP_SHARED_CWND_ENABLE:		/*  URL:scwnd */
24264 		case TCP_RACK_MBUF_QUEUE:		/*  URL:mqueue */
24265 		case TCP_RACK_NO_PUSH_AT_MAX:		/*  URL:npush */
24266 		case TCP_SHARED_CWND_TIME_LIMIT:	/*  URL:lscwnd */
24267 		case TCP_RACK_PROFILE:			/*  URL:profile */
24268 		case TCP_SIDECHAN_DIS:			/*  URL:scodm */
24269 		case TCP_HYBRID_PACING:			/*  URL:pacing=hybrid */
24270 		case TCP_USE_CMP_ACKS:			/*  URL:cmpack */
24271 		case TCP_RACK_ABC_VAL:			/*  URL:labc */
24272 		case TCP_REC_ABC_VAL:			/*  URL:reclabc */
24273 		case TCP_RACK_MEASURE_CNT:		/*  URL:measurecnt */
24274 		case TCP_DEFER_OPTIONS:			/*  URL:defer */
24275 		case TCP_RACK_DSACK_OPT:		/*  URL:dsack */
24276 		case TCP_RACK_TIMER_SLOP:		/*  URL:timer_slop */
24277 		case TCP_RACK_ENABLE_HYSTART:		/*  URL:hystart */
24278 		case TCP_RACK_SET_RXT_OPTIONS:		/*  URL:rxtsz */
24279 		case TCP_RACK_HI_BETA:			/*  URL:hibeta */
24280 		case TCP_RACK_SPLIT_LIMIT:		/*  URL:split */
24281 		case TCP_SS_EEXIT:			/*  URL:eexit */
24282 		case TCP_DGP_UPPER_BOUNDS:		/*  URL:upper */
24283 		case TCP_RACK_PACING_DIVISOR:		/*  URL:divisor */
24284 		case TCP_PACING_DND:			/*  URL:dnd */
24285 		case TCP_NO_TIMELY:			/*  URL:notimely */
24286 		case RACK_CSPR_IS_FCC:			/*  URL:csprisfcc */
24287 		case TCP_HONOR_HPTS_MIN:		/*  URL:hptsmin */
24288 		case TCP_REC_IS_DYN:			/*  URL:dynrec */
24289 		case TCP_GP_USE_LTBW:			/*  URL:useltbw */
24290 			goto process_opt;
24291 			break;
24292 		default:
24293 			/* Filter off all unknown options to the base stack */
24294 			return (tcp_default_ctloutput(tp, sopt));
24295 			break;
24296 		}
24297 	default:
24298 		INP_WUNLOCK(inp);
24299 		return (0);
24300 	}
24301 process_opt:
24302 	INP_WUNLOCK(inp);
24303 	if ((sopt->sopt_name == TCP_PACING_RATE_CAP) ||
24304 	    (sopt->sopt_name == TCP_FILLCW_RATE_CAP)) {
24305 		error = sooptcopyin(sopt, &loptval, sizeof(loptval), sizeof(loptval));
24306 		/*
24307 		 * We truncate it down to 32 bits for the socket-option trace this
24308 		 * means rates > 34Gbps won't show right, but thats probably ok.
24309 		 */
24310 		optval = (uint32_t)loptval;
24311 	} else if (sopt->sopt_name == TCP_HYBRID_PACING) {
24312 		error = sooptcopyin(sopt, &hybrid, sizeof(hybrid), sizeof(hybrid));
24313 	} else {
24314 		error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
24315 		/* Save it in 64 bit form too */
24316 		loptval = optval;
24317 	}
24318 	if (error)
24319 		return (error);
24320 	INP_WLOCK(inp);
24321 	if (tp->t_fb != &__tcp_rack) {
24322 		INP_WUNLOCK(inp);
24323 		return (ENOPROTOOPT);
24324 	}
24325 	if (rack->defer_options && (rack->gp_ready == 0) &&
24326 	    (sopt->sopt_name != TCP_DEFER_OPTIONS) &&
24327 	    (sopt->sopt_name != TCP_HYBRID_PACING) &&
24328 	    (sopt->sopt_name != TCP_RACK_SET_RXT_OPTIONS) &&
24329 	    (sopt->sopt_name != TCP_RACK_PACING_BETA_ECN) &&
24330 	    (sopt->sopt_name != TCP_RACK_MEASURE_CNT)) {
24331 		/* Options are being deferred */
24332 		if (rack_add_deferred_option(rack, sopt->sopt_name, loptval)) {
24333 			INP_WUNLOCK(inp);
24334 			return (0);
24335 		} else {
24336 			/* No memory to defer, fail */
24337 			INP_WUNLOCK(inp);
24338 			return (ENOMEM);
24339 		}
24340 	}
24341 	error = rack_process_option(tp, rack, sopt->sopt_name, optval, loptval, &hybrid);
24342 	INP_WUNLOCK(inp);
24343 	return (error);
24344 }
24345 
24346 static void
24347 rack_fill_info(struct tcpcb *tp, struct tcp_info *ti)
24348 {
24349 
24350 	INP_WLOCK_ASSERT(tptoinpcb(tp));
24351 	bzero(ti, sizeof(*ti));
24352 
24353 	ti->tcpi_state = tp->t_state;
24354 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
24355 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
24356 	if (tp->t_flags & TF_SACK_PERMIT)
24357 		ti->tcpi_options |= TCPI_OPT_SACK;
24358 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
24359 		ti->tcpi_options |= TCPI_OPT_WSCALE;
24360 		ti->tcpi_snd_wscale = tp->snd_scale;
24361 		ti->tcpi_rcv_wscale = tp->rcv_scale;
24362 	}
24363 	if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
24364 		ti->tcpi_options |= TCPI_OPT_ECN;
24365 	if (tp->t_flags & TF_FASTOPEN)
24366 		ti->tcpi_options |= TCPI_OPT_TFO;
24367 	/* still kept in ticks is t_rcvtime */
24368 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
24369 	/* Since we hold everything in precise useconds this is easy */
24370 	ti->tcpi_rtt = tp->t_srtt;
24371 	ti->tcpi_rttvar = tp->t_rttvar;
24372 	ti->tcpi_rto = tp->t_rxtcur;
24373 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
24374 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
24375 	/*
24376 	 * FreeBSD-specific extension fields for tcp_info.
24377 	 */
24378 	ti->tcpi_rcv_space = tp->rcv_wnd;
24379 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
24380 	ti->tcpi_snd_wnd = tp->snd_wnd;
24381 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
24382 	ti->tcpi_snd_nxt = tp->snd_nxt;
24383 	ti->tcpi_snd_mss = tp->t_maxseg;
24384 	ti->tcpi_rcv_mss = tp->t_maxseg;
24385 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
24386 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
24387 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
24388 	ti->tcpi_total_tlp = tp->t_sndtlppack;
24389 	ti->tcpi_total_tlp_bytes = tp->t_sndtlpbyte;
24390 	ti->tcpi_rttmin = tp->t_rttlow;
24391 #ifdef NETFLIX_STATS
24392 	memcpy(&ti->tcpi_rxsyninfo, &tp->t_rxsyninfo, sizeof(struct tcpsyninfo));
24393 #endif
24394 #ifdef TCP_OFFLOAD
24395 	if (tp->t_flags & TF_TOE) {
24396 		ti->tcpi_options |= TCPI_OPT_TOE;
24397 		tcp_offload_tcp_info(tp, ti);
24398 	}
24399 #endif
24400 }
24401 
24402 static int
24403 rack_get_sockopt(struct tcpcb *tp, struct sockopt *sopt)
24404 {
24405 	struct inpcb *inp = tptoinpcb(tp);
24406 	struct tcp_rack *rack;
24407 	int32_t error, optval;
24408 	uint64_t val, loptval;
24409 	struct	tcp_info ti;
24410 	/*
24411 	 * Because all our options are either boolean or an int, we can just
24412 	 * pull everything into optval and then unlock and copy. If we ever
24413 	 * add a option that is not a int, then this will have quite an
24414 	 * impact to this routine.
24415 	 */
24416 	error = 0;
24417 	rack = (struct tcp_rack *)tp->t_fb_ptr;
24418 	if (rack == NULL) {
24419 		INP_WUNLOCK(inp);
24420 		return (EINVAL);
24421 	}
24422 	switch (sopt->sopt_name) {
24423 	case TCP_INFO:
24424 		/* First get the info filled */
24425 		rack_fill_info(tp, &ti);
24426 		/* Fix up the rtt related fields if needed */
24427 		INP_WUNLOCK(inp);
24428 		error = sooptcopyout(sopt, &ti, sizeof ti);
24429 		return (error);
24430 	/*
24431 	 * Beta is the congestion control value for NewReno that influences how
24432 	 * much of a backoff happens when loss is detected. It is normally set
24433 	 * to 50 for 50% i.e. the cwnd is reduced to 50% of its previous value
24434 	 * when you exit recovery.
24435 	 */
24436 	case TCP_RACK_PACING_BETA:
24437 		if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0)
24438 			error = EINVAL;
24439 		else if (rack->rc_pacing_cc_set == 0)
24440 			optval = rack->r_ctl.rc_saved_beta.beta;
24441 		else {
24442 			/*
24443 			 * Reach out into the CC data and report back what
24444 			 * I have previously set. Yeah it looks hackish but
24445 			 * we don't want to report the saved values.
24446 			 */
24447 			if (tp->t_ccv.cc_data)
24448 				optval = ((struct newreno *)tp->t_ccv.cc_data)->beta;
24449 			else
24450 				error = EINVAL;
24451 		}
24452 		break;
24453 	/*
24454 	 * Beta_ecn is the congestion control value for NewReno that influences how
24455 	 * much of a backoff happens when a ECN mark is detected. It is normally set
24456 	 * to 80 for 80% i.e. the cwnd is reduced by 20% of its previous value when
24457 	 * you exit recovery. Note that classic ECN has a beta of 50, it is only
24458 	 * ABE Ecn that uses this "less" value, but we do too with pacing :)
24459 	 */
24460 	case TCP_RACK_PACING_BETA_ECN:
24461 		if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0)
24462 			error = EINVAL;
24463 		else if (rack->rc_pacing_cc_set == 0)
24464 			optval = rack->r_ctl.rc_saved_beta.beta_ecn;
24465 		else {
24466 			/*
24467 			 * Reach out into the CC data and report back what
24468 			 * I have previously set. Yeah it looks hackish but
24469 			 * we don't want to report the saved values.
24470 			 */
24471 			if (tp->t_ccv.cc_data)
24472 				optval = ((struct newreno *)tp->t_ccv.cc_data)->beta_ecn;
24473 			else
24474 				error = EINVAL;
24475 		}
24476 		break;
24477 	case TCP_RACK_DSACK_OPT:
24478 		optval = 0;
24479 		if (rack->rc_rack_tmr_std_based) {
24480 			optval |= 1;
24481 		}
24482 		if (rack->rc_rack_use_dsack) {
24483 			optval |= 2;
24484 		}
24485 		break;
24486 	case TCP_RACK_ENABLE_HYSTART:
24487 	{
24488 		if (tp->t_ccv.flags & CCF_HYSTART_ALLOWED) {
24489 			optval = RACK_HYSTART_ON;
24490 			if (tp->t_ccv.flags & CCF_HYSTART_CAN_SH_CWND)
24491 				optval = RACK_HYSTART_ON_W_SC;
24492 			if (tp->t_ccv.flags & CCF_HYSTART_CONS_SSTH)
24493 				optval = RACK_HYSTART_ON_W_SC_C;
24494 		} else {
24495 			optval = RACK_HYSTART_OFF;
24496 		}
24497 	}
24498 	break;
24499 	case TCP_RACK_DGP_IN_REC:
24500 		error = EINVAL;
24501 		break;
24502 	case TCP_RACK_HI_BETA:
24503 		optval = rack->rack_hibeta;
24504 		break;
24505 	case TCP_DEFER_OPTIONS:
24506 		optval = rack->defer_options;
24507 		break;
24508 	case TCP_RACK_MEASURE_CNT:
24509 		optval = rack->r_ctl.req_measurements;
24510 		break;
24511 	case TCP_REC_ABC_VAL:
24512 		optval = rack->r_use_labc_for_rec;
24513 		break;
24514 	case TCP_RACK_ABC_VAL:
24515 		optval = rack->rc_labc;
24516 		break;
24517 	case TCP_HDWR_UP_ONLY:
24518 		optval= rack->r_up_only;
24519 		break;
24520 	case TCP_FILLCW_RATE_CAP:
24521 		loptval = rack->r_ctl.fillcw_cap;
24522 		break;
24523 	case TCP_PACING_RATE_CAP:
24524 		loptval = rack->r_ctl.bw_rate_cap;
24525 		break;
24526 	case TCP_RACK_PROFILE:
24527 		/* You cannot retrieve a profile, its write only */
24528 		error = EINVAL;
24529 		break;
24530 	case TCP_SIDECHAN_DIS:
24531 		optval = rack->r_ctl.side_chan_dis_mask;
24532 		break;
24533 	case TCP_HYBRID_PACING:
24534 		/* You cannot retrieve hybrid pacing information, its write only */
24535 		error = EINVAL;
24536 		break;
24537 	case TCP_USE_CMP_ACKS:
24538 		optval = rack->r_use_cmp_ack;
24539 		break;
24540 	case TCP_RACK_PACE_TO_FILL:
24541 		optval = rack->rc_pace_to_cwnd;
24542 		break;
24543 	case TCP_RACK_NO_PUSH_AT_MAX:
24544 		optval = rack->r_ctl.rc_no_push_at_mrtt;
24545 		break;
24546 	case TCP_SHARED_CWND_ENABLE:
24547 		optval = rack->rack_enable_scwnd;
24548 		break;
24549 	case TCP_RACK_NONRXT_CFG_RATE:
24550 		optval = rack->rack_rec_nonrxt_use_cr;
24551 		break;
24552 	case TCP_NO_PRR:
24553 		if (rack->rack_no_prr  == 1)
24554 			optval = 1;
24555 		else if (rack->no_prr_addback == 1)
24556 			optval = 2;
24557 		else
24558 			optval = 0;
24559 		break;
24560 	case TCP_GP_USE_LTBW:
24561 		if (rack->dis_lt_bw) {
24562 			/* It is not used */
24563 			optval = 0;
24564 		} else if (rack->use_lesser_lt_bw) {
24565 			/* we use min() */
24566 			optval = 1;
24567 		} else {
24568 			/* we use max() */
24569 			optval = 2;
24570 		}
24571 		break;
24572 	case TCP_RACK_DO_DETECTION:
24573 		error = EINVAL;
24574 		break;
24575 	case TCP_RACK_MBUF_QUEUE:
24576 		/* Now do we use the LRO mbuf-queue feature */
24577 		optval = rack->r_mbuf_queue;
24578 		break;
24579 	case RACK_CSPR_IS_FCC:
24580 		optval = rack->cspr_is_fcc;
24581 		break;
24582 	case TCP_TIMELY_DYN_ADJ:
24583 		optval = rack->rc_gp_dyn_mul;
24584 		break;
24585 	case TCP_BBR_IWINTSO:
24586 		error = EINVAL;
24587 		break;
24588 	case TCP_RACK_TLP_REDUCE:
24589 		/* RACK TLP cwnd reduction (bool) */
24590 		optval = rack->r_ctl.rc_tlp_cwnd_reduce;
24591 		break;
24592 	case TCP_BBR_RACK_INIT_RATE:
24593 		val = rack->r_ctl.init_rate;
24594 		/* convert to kbits per sec */
24595 		val *= 8;
24596 		val /= 1000;
24597 		optval = (uint32_t)val;
24598 		break;
24599 	case TCP_RACK_FORCE_MSEG:
24600 		optval = rack->rc_force_max_seg;
24601 		break;
24602 	case TCP_RACK_PACE_MIN_SEG:
24603 		optval = rack->r_ctl.rc_user_set_min_segs;
24604 		break;
24605 	case TCP_RACK_PACE_MAX_SEG:
24606 		/* Max segments in a pace */
24607 		optval = rack->rc_user_set_max_segs;
24608 		break;
24609 	case TCP_RACK_PACE_ALWAYS:
24610 		/* Use the always pace method */
24611 		optval = rack->rc_always_pace;
24612 		break;
24613 	case TCP_RACK_PRR_SENDALOT:
24614 		/* Allow PRR to send more than one seg */
24615 		optval = rack->r_ctl.rc_prr_sendalot;
24616 		break;
24617 	case TCP_RACK_MIN_TO:
24618 		/* Minimum time between rack t-o's in ms */
24619 		optval = rack->r_ctl.rc_min_to;
24620 		break;
24621 	case TCP_RACK_SPLIT_LIMIT:
24622 		optval = rack->r_ctl.rc_split_limit;
24623 		break;
24624 	case TCP_RACK_EARLY_SEG:
24625 		/* If early recovery max segments */
24626 		optval = rack->r_ctl.rc_early_recovery_segs;
24627 		break;
24628 	case TCP_RACK_REORD_THRESH:
24629 		/* RACK reorder threshold (shift amount) */
24630 		optval = rack->r_ctl.rc_reorder_shift;
24631 		break;
24632 	case TCP_SS_EEXIT:
24633 		if (rack->r_ctl.gp_rnd_thresh) {
24634 			uint32_t v;
24635 
24636 			v = rack->r_ctl.gp_gain_req;
24637 			v <<= 17;
24638 			optval = v | (rack->r_ctl.gp_rnd_thresh & 0xff);
24639 			if (rack->r_ctl.gate_to_fs == 1)
24640 				optval |= 0x10000;
24641 		} else
24642 			optval = 0;
24643 		break;
24644 	case TCP_RACK_REORD_FADE:
24645 		/* Does reordering fade after ms time */
24646 		optval = rack->r_ctl.rc_reorder_fade;
24647 		break;
24648 	case TCP_BBR_USE_RACK_RR:
24649 		/* Do we use the rack cheat for rxt */
24650 		optval = rack->use_rack_rr;
24651 		break;
24652 	case TCP_RACK_RR_CONF:
24653 		optval = rack->r_rr_config;
24654 		break;
24655 	case TCP_HDWR_RATE_CAP:
24656 		optval = rack->r_rack_hw_rate_caps;
24657 		break;
24658 	case TCP_BBR_HDWR_PACE:
24659 		optval = rack->rack_hdw_pace_ena;
24660 		break;
24661 	case TCP_RACK_TLP_THRESH:
24662 		/* RACK TLP theshold i.e. srtt+(srtt/N) */
24663 		optval = rack->r_ctl.rc_tlp_threshold;
24664 		break;
24665 	case TCP_RACK_PKT_DELAY:
24666 		/* RACK added ms i.e. rack-rtt + reord + N */
24667 		optval = rack->r_ctl.rc_pkt_delay;
24668 		break;
24669 	case TCP_RACK_TLP_USE:
24670 		optval = rack->rack_tlp_threshold_use;
24671 		break;
24672 	case TCP_PACING_DND:
24673 		optval = rack->rc_pace_dnd;
24674 		break;
24675 	case TCP_RACK_PACE_RATE_CA:
24676 		optval = rack->r_ctl.rc_fixed_pacing_rate_ca;
24677 		break;
24678 	case TCP_RACK_PACE_RATE_SS:
24679 		optval = rack->r_ctl.rc_fixed_pacing_rate_ss;
24680 		break;
24681 	case TCP_RACK_PACE_RATE_REC:
24682 		optval = rack->r_ctl.rc_fixed_pacing_rate_rec;
24683 		break;
24684 	case TCP_DGP_UPPER_BOUNDS:
24685 		optval = rack->r_ctl.rack_per_upper_bound_ss;
24686 		optval <<= 16;
24687 		optval |= rack->r_ctl.rack_per_upper_bound_ca;
24688 		break;
24689 	case TCP_RACK_GP_INCREASE_SS:
24690 		optval = rack->r_ctl.rack_per_of_gp_ca;
24691 		break;
24692 	case TCP_RACK_GP_INCREASE_CA:
24693 		optval = rack->r_ctl.rack_per_of_gp_ss;
24694 		break;
24695 	case TCP_RACK_PACING_DIVISOR:
24696 		optval = rack->r_ctl.pace_len_divisor;
24697 		break;
24698 	case TCP_BBR_RACK_RTT_USE:
24699 		optval = rack->r_ctl.rc_rate_sample_method;
24700 		break;
24701 	case TCP_DELACK:
24702 		optval = tp->t_delayed_ack;
24703 		break;
24704 	case TCP_DATA_AFTER_CLOSE:
24705 		optval = rack->rc_allow_data_af_clo;
24706 		break;
24707 	case TCP_SHARED_CWND_TIME_LIMIT:
24708 		optval = rack->r_limit_scw;
24709 		break;
24710 	case TCP_HONOR_HPTS_MIN:
24711 		if (rack->r_use_hpts_min)
24712 			optval = rack->r_ctl.max_reduction;
24713 		else
24714 			optval = 0;
24715 		break;
24716 	case TCP_REC_IS_DYN:
24717 		optval = rack->rc_gp_no_rec_chg;
24718 		break;
24719 	case TCP_NO_TIMELY:
24720 		optval = rack->rc_skip_timely;
24721 		break;
24722 	case TCP_RACK_TIMER_SLOP:
24723 		optval = rack->r_ctl.timer_slop;
24724 		break;
24725 	default:
24726 		return (tcp_default_ctloutput(tp, sopt));
24727 		break;
24728 	}
24729 	INP_WUNLOCK(inp);
24730 	if (error == 0) {
24731 		if ((sopt->sopt_name == TCP_PACING_RATE_CAP) ||
24732 		    (sopt->sopt_name == TCP_FILLCW_RATE_CAP))
24733 			error = sooptcopyout(sopt, &loptval, sizeof loptval);
24734 		else
24735 			error = sooptcopyout(sopt, &optval, sizeof optval);
24736 	}
24737 	return (error);
24738 }
24739 
24740 static int
24741 rack_ctloutput(struct tcpcb *tp, struct sockopt *sopt)
24742 {
24743 	if (sopt->sopt_dir == SOPT_SET) {
24744 		return (rack_set_sockopt(tp, sopt));
24745 	} else if (sopt->sopt_dir == SOPT_GET) {
24746 		return (rack_get_sockopt(tp, sopt));
24747 	} else {
24748 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
24749 	}
24750 }
24751 
24752 static const char *rack_stack_names[] = {
24753 	__XSTRING(STACKNAME),
24754 #ifdef STACKALIAS
24755 	__XSTRING(STACKALIAS),
24756 #endif
24757 };
24758 
24759 static int
24760 rack_ctor(void *mem, int32_t size, void *arg, int32_t how)
24761 {
24762 	memset(mem, 0, size);
24763 	return (0);
24764 }
24765 
24766 static void
24767 rack_dtor(void *mem, int32_t size, void *arg)
24768 {
24769 
24770 }
24771 
24772 static bool rack_mod_inited = false;
24773 
24774 static int
24775 tcp_addrack(module_t mod, int32_t type, void *data)
24776 {
24777 	int32_t err = 0;
24778 	int num_stacks;
24779 
24780 	switch (type) {
24781 	case MOD_LOAD:
24782 		rack_zone = uma_zcreate(__XSTRING(MODNAME) "_map",
24783 		    sizeof(struct rack_sendmap),
24784 		    rack_ctor, rack_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
24785 
24786 		rack_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb",
24787 		    sizeof(struct tcp_rack),
24788 		    rack_ctor, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
24789 
24790 		sysctl_ctx_init(&rack_sysctl_ctx);
24791 		rack_sysctl_root = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
24792 		    SYSCTL_STATIC_CHILDREN(_net_inet_tcp),
24793 		    OID_AUTO,
24794 #ifdef STACKALIAS
24795 		    __XSTRING(STACKALIAS),
24796 #else
24797 		    __XSTRING(STACKNAME),
24798 #endif
24799 		    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
24800 		    "");
24801 		if (rack_sysctl_root == NULL) {
24802 			printf("Failed to add sysctl node\n");
24803 			err = EFAULT;
24804 			goto free_uma;
24805 		}
24806 		rack_init_sysctls();
24807 		num_stacks = nitems(rack_stack_names);
24808 		err = register_tcp_functions_as_names(&__tcp_rack, M_WAITOK,
24809 		    rack_stack_names, &num_stacks);
24810 		if (err) {
24811 			printf("Failed to register %s stack name for "
24812 			    "%s module\n", rack_stack_names[num_stacks],
24813 			    __XSTRING(MODNAME));
24814 			sysctl_ctx_free(&rack_sysctl_ctx);
24815 free_uma:
24816 			uma_zdestroy(rack_zone);
24817 			uma_zdestroy(rack_pcb_zone);
24818 			rack_counter_destroy();
24819 			printf("Failed to register rack module -- err:%d\n", err);
24820 			return (err);
24821 		}
24822 		tcp_lro_reg_mbufq();
24823 		rack_mod_inited = true;
24824 		break;
24825 	case MOD_QUIESCE:
24826 		err = deregister_tcp_functions(&__tcp_rack, true, false);
24827 		break;
24828 	case MOD_UNLOAD:
24829 		err = deregister_tcp_functions(&__tcp_rack, false, true);
24830 		if (err == EBUSY)
24831 			break;
24832 		if (rack_mod_inited) {
24833 			uma_zdestroy(rack_zone);
24834 			uma_zdestroy(rack_pcb_zone);
24835 			sysctl_ctx_free(&rack_sysctl_ctx);
24836 			rack_counter_destroy();
24837 			rack_mod_inited = false;
24838 		}
24839 		tcp_lro_dereg_mbufq();
24840 		err = 0;
24841 		break;
24842 	default:
24843 		return (EOPNOTSUPP);
24844 	}
24845 	return (err);
24846 }
24847 
24848 static moduledata_t tcp_rack = {
24849 	.name = __XSTRING(MODNAME),
24850 	.evhand = tcp_addrack,
24851 	.priv = 0
24852 };
24853 
24854 MODULE_VERSION(MODNAME, 1);
24855 DECLARE_MODULE(MODNAME, tcp_rack, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
24856 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1);
24857 
24858 #endif /* #if !defined(INET) && !defined(INET6) */
24859