xref: /freebsd/sys/netinet/tcp_stacks/rack.c (revision f6d489f4)
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 uint16_t rack_policer_rxt_thresh= 0;	/* 499 = 49.9%, 0 is off  */
197 static uint8_t rack_policer_avg_thresh = 0; /* 3.2 */
198 static uint8_t rack_policer_med_thresh = 0; /* 1 - 16 */
199 static uint16_t rack_policer_bucket_reserve = 20; /* How much % is reserved in the bucket */
200 static uint64_t rack_pol_min_bw = 125000;	/* 1mbps in Bytes per sec */
201 static uint32_t rack_policer_data_thresh = 64000;	/* 64,000 bytes must be sent before we engage */
202 static uint32_t rack_policing_do_bw_comp = 1;
203 static uint32_t rack_pcm_every_n_rounds = 100;
204 static uint32_t rack_pcm_blast = 0;
205 static uint32_t rack_pcm_is_enabled = 1;
206 static uint8_t rack_req_del_mss = 18;	/* How many segments need to be sent in a recovery episode to do policer_detection */
207 static uint8_t rack_ssthresh_rest_rto_rec = 0; /* Do we restore ssthresh when we have rec -> rto -> rec */
208 
209 static uint32_t rack_gp_gain_req = 1200;		/* Amount percent wise required to gain to record a round has "gaining" */
210 static uint32_t rack_rnd_cnt_req = 0x10005;		/* Default number of rounds if we are below rack_gp_gain_req where we exit ss */
211 
212 
213 static int32_t rack_rxt_scoreboard_clear_thresh = 2;
214 static int32_t rack_dnd_default = 0;		/* For rr_conf = 3, what is the default for dnd */
215 static int32_t rack_rxt_controls = 0;
216 static int32_t rack_fill_cw_state = 0;
217 static uint8_t rack_req_measurements = 1;
218 /* Attack threshold detections */
219 static uint32_t rack_highest_sack_thresh_seen = 0;
220 static uint32_t rack_highest_move_thresh_seen = 0;
221 static uint32_t rack_merge_out_sacks_on_attack = 0;
222 static int32_t rack_enable_hw_pacing = 0; /* Due to CCSP keep it off by default */
223 static int32_t rack_hw_pace_extra_slots = 0;	/* 2 extra MSS time betweens */
224 static int32_t rack_hw_rate_caps = 0; /* 1; */
225 static int32_t rack_hw_rate_cap_per = 0;	/* 0 -- off  */
226 static int32_t rack_hw_rate_min = 0; /* 1500000;*/
227 static int32_t rack_hw_rate_to_low = 0; /* 1200000; */
228 static int32_t rack_hw_up_only = 0;
229 static int32_t rack_stats_gets_ms_rtt = 1;
230 static int32_t rack_prr_addbackmax = 2;
231 static int32_t rack_do_hystart = 0;
232 static int32_t rack_apply_rtt_with_reduced_conf = 0;
233 static int32_t rack_hibeta_setting = 0;
234 static int32_t rack_default_pacing_divisor = 250;
235 static uint16_t rack_pacing_min_seg = 0;
236 static int32_t rack_timely_off = 0;
237 
238 static uint32_t sad_seg_size_per = 800;	/* 80.0 % */
239 static int32_t rack_pkt_delay = 1000;
240 static int32_t rack_send_a_lot_in_prr = 1;
241 static int32_t rack_min_to = 1000;	/* Number of microsecond  min timeout */
242 static int32_t rack_verbose_logging = 0;
243 static int32_t rack_ignore_data_after_close = 1;
244 static int32_t rack_enable_shared_cwnd = 1;
245 static int32_t rack_use_cmp_acks = 1;
246 static int32_t rack_use_fsb = 1;
247 static int32_t rack_use_rfo = 1;
248 static int32_t rack_use_rsm_rfo = 1;
249 static int32_t rack_max_abc_post_recovery = 2;
250 static int32_t rack_client_low_buf = 0;
251 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 */
252 static int32_t rack_bw_multipler = 0;		/* Limit on fill cw's jump up to be this x gp_est */
253 #ifdef TCP_ACCOUNTING
254 static int32_t rack_tcp_accounting = 0;
255 #endif
256 static int32_t rack_limits_scwnd = 1;
257 static int32_t rack_enable_mqueue_for_nonpaced = 0;
258 static int32_t rack_hybrid_allow_set_maxseg = 0;
259 static int32_t rack_disable_prr = 0;
260 static int32_t use_rack_rr = 1;
261 static int32_t rack_non_rxt_use_cr = 0; /* does a non-rxt in recovery use the configured rate (ss/ca)? */
262 static int32_t rack_persist_min = 250000;	/* 250usec */
263 static int32_t rack_persist_max = 2000000;	/* 2 Second in usec's */
264 static int32_t rack_honors_hpts_min_to =  1;	/* Do we honor the hpts minimum time out for pacing timers */
265 static uint32_t rack_max_reduce = 10;		/* Percent we can reduce slot by */
266 static int32_t rack_sack_not_required = 1;	/* set to one to allow non-sack to use rack */
267 static int32_t rack_limit_time_with_srtt = 0;
268 static int32_t rack_autosndbuf_inc = 20;	/* In percentage form */
269 static int32_t rack_enobuf_hw_boost_mult = 0;	/* How many times the hw rate we boost slot using time_between */
270 static int32_t rack_enobuf_hw_max = 12000;	/* 12 ms in usecs */
271 static int32_t rack_enobuf_hw_min = 10000;	/* 10 ms in usecs */
272 static int32_t rack_hw_rwnd_factor = 2;		/* How many max_segs the rwnd must be before we hold off sending */
273 static int32_t rack_hw_check_queue = 0;		/* Do we always pre-check queue depth of a hw queue */
274 static int32_t rack_full_buffer_discount = 10;
275 /*
276  * Currently regular tcp has a rto_min of 30ms
277  * the backoff goes 12 times so that ends up
278  * being a total of 122.850 seconds before a
279  * connection is killed.
280  */
281 static uint32_t rack_def_data_window = 20;
282 static uint32_t rack_goal_bdp = 2;
283 static uint32_t rack_min_srtts = 1;
284 static uint32_t rack_min_measure_usec = 0;
285 static int32_t rack_tlp_min = 10000;	/* 10ms */
286 static int32_t rack_rto_min = 30000;	/* 30,000 usec same as main freebsd */
287 static int32_t rack_rto_max = 4000000;	/* 4 seconds in usec's */
288 static const int32_t rack_free_cache = 2;
289 static int32_t rack_hptsi_segments = 40;
290 static int32_t rack_rate_sample_method = USE_RTT_LOW;
291 static int32_t rack_pace_every_seg = 0;
292 static int32_t rack_delayed_ack_time = 40000;	/* 40ms in usecs */
293 static int32_t rack_slot_reduction = 4;
294 static int32_t rack_wma_divisor = 8;		/* For WMA calculation */
295 static int32_t rack_cwnd_block_ends_measure = 0;
296 static int32_t rack_rwnd_block_ends_measure = 0;
297 static int32_t rack_def_profile = 0;
298 
299 static int32_t rack_lower_cwnd_at_tlp = 0;
300 static int32_t rack_always_send_oldest = 0;
301 static int32_t rack_tlp_threshold_use = TLP_USE_TWO_ONE;
302 
303 static uint16_t rack_per_of_gp_ss = 250;	/* 250 % slow-start */
304 static uint16_t rack_per_of_gp_ca = 200;	/* 200 % congestion-avoidance */
305 static uint16_t rack_per_of_gp_rec = 200;	/* 200 % of bw */
306 
307 /* Probertt */
308 static uint16_t rack_per_of_gp_probertt = 60;	/* 60% of bw */
309 static uint16_t rack_per_of_gp_lowthresh = 40;	/* 40% is bottom */
310 static uint16_t rack_per_of_gp_probertt_reduce = 10; /* 10% reduction */
311 static uint16_t rack_atexit_prtt_hbp = 130;	/* Clamp to 130% on exit prtt if highly buffered path */
312 static uint16_t rack_atexit_prtt = 130;	/* Clamp to 100% on exit prtt if non highly buffered path */
313 
314 static uint32_t rack_max_drain_wait = 2;	/* How man gp srtt's before we give up draining */
315 static uint32_t rack_must_drain = 1;		/* How many GP srtt's we *must* wait */
316 static uint32_t rack_probertt_use_min_rtt_entry = 1;	/* Use the min to calculate the goal else gp_srtt */
317 static uint32_t rack_probertt_use_min_rtt_exit = 0;
318 static uint32_t rack_probe_rtt_sets_cwnd = 0;
319 static uint32_t rack_probe_rtt_safety_val = 2000000;	/* No more than 2 sec in probe-rtt */
320 static uint32_t rack_time_between_probertt = 9600000;	/* 9.6 sec in usecs */
321 static uint32_t rack_probertt_gpsrtt_cnt_mul = 0;	/* How many srtt periods does probe-rtt last top fraction */
322 static uint32_t rack_probertt_gpsrtt_cnt_div = 0;	/* How many srtt periods does probe-rtt last bottom fraction */
323 static uint32_t rack_min_probertt_hold = 40000;		/* Equal to delayed ack time */
324 static uint32_t rack_probertt_filter_life = 10000000;
325 static uint32_t rack_probertt_lower_within = 10;
326 static uint32_t rack_min_rtt_movement = 250000;	/* Must move at least 250ms (in microseconds)  to count as a lowering */
327 static int32_t rack_pace_one_seg = 0;		/* Shall we pace for less than 1.4Meg 1MSS at a time */
328 static int32_t rack_probertt_clear_is = 1;
329 static int32_t rack_max_drain_hbp = 1;		/* Extra drain times gpsrtt for highly buffered paths */
330 static int32_t rack_hbp_thresh = 3;		/* what is the divisor max_rtt/min_rtt to decided a hbp */
331 
332 /* Part of pacing */
333 static int32_t rack_max_per_above = 30;		/* When we go to increment stop if above 100+this% */
334 
335 /* Timely information:
336  *
337  * Here we have various control parameters on how
338  * timely may change the multiplier. rack_gain_p5_ub
339  * is associated with timely but not directly influencing
340  * the rate decision like the other variables. It controls
341  * the way fill-cw interacts with timely and caps how much
342  * timely can boost the fill-cw b/w.
343  *
344  * The other values are various boost/shrink numbers as well
345  * as potential caps when adjustments are made to the timely
346  * gain (returned by rack_get_output_gain(). Remember too that
347  * the gain returned can be overriden by other factors such as
348  * probeRTT as well as fixed-rate-pacing.
349  */
350 static int32_t rack_gain_p5_ub = 250;
351 static int32_t rack_gp_per_bw_mul_up = 2;	/* 2% */
352 static int32_t rack_gp_per_bw_mul_down = 4;	/* 4% */
353 static int32_t rack_gp_rtt_maxmul = 3;		/* 3 x maxmin */
354 static int32_t rack_gp_rtt_minmul = 1;		/* minrtt + (minrtt/mindiv) is lower rtt */
355 static int32_t rack_gp_rtt_mindiv = 4;		/* minrtt + (minrtt * minmul/mindiv) is lower rtt */
356 static int32_t rack_gp_decrease_per = 80;	/* Beta value of timely decrease (.8) = 80 */
357 static int32_t rack_gp_increase_per = 2;	/* 2% increase in multiplier */
358 static int32_t rack_per_lower_bound = 50;	/* Don't allow to drop below this multiplier */
359 static int32_t rack_per_upper_bound_ss = 0;	/* Don't allow SS to grow above this */
360 static int32_t rack_per_upper_bound_ca = 0;	/* Don't allow CA to grow above this */
361 static int32_t rack_do_dyn_mul = 0;		/* Are the rack gp multipliers dynamic */
362 static int32_t rack_gp_no_rec_chg = 1;		/* Prohibit recovery from reducing it's multiplier */
363 static int32_t rack_timely_dec_clear = 6;	/* Do we clear decrement count at a value (6)? */
364 static int32_t rack_timely_max_push_rise = 3;	/* One round of pushing */
365 static int32_t rack_timely_max_push_drop = 3;	/* Three round of pushing */
366 static int32_t rack_timely_min_segs = 4;	/* 4 segment minimum */
367 static int32_t rack_use_max_for_nobackoff = 0;
368 static int32_t rack_timely_int_timely_only = 0;	/* do interim timely's only use the timely algo (no b/w changes)? */
369 static int32_t rack_timely_no_stopping = 0;
370 static int32_t rack_down_raise_thresh = 100;
371 static int32_t rack_req_segs = 1;
372 static uint64_t rack_bw_rate_cap = 0;
373 static uint64_t rack_fillcw_bw_cap = 3750000;	/* Cap fillcw at 30Mbps */
374 
375 
376 /* Rack specific counters */
377 counter_u64_t rack_saw_enobuf;
378 counter_u64_t rack_saw_enobuf_hw;
379 counter_u64_t rack_saw_enetunreach;
380 counter_u64_t rack_persists_sends;
381 counter_u64_t rack_persists_acks;
382 counter_u64_t rack_persists_loss;
383 counter_u64_t rack_persists_lost_ends;
384 counter_u64_t rack_total_bytes;
385 #ifdef INVARIANTS
386 counter_u64_t rack_adjust_map_bw;
387 #endif
388 /* Tail loss probe counters */
389 counter_u64_t rack_tlp_tot;
390 counter_u64_t rack_tlp_newdata;
391 counter_u64_t rack_tlp_retran;
392 counter_u64_t rack_tlp_retran_bytes;
393 counter_u64_t rack_to_tot;
394 counter_u64_t rack_hot_alloc;
395 counter_u64_t tcp_policer_detected;
396 counter_u64_t rack_to_alloc;
397 counter_u64_t rack_to_alloc_hard;
398 counter_u64_t rack_to_alloc_emerg;
399 counter_u64_t rack_to_alloc_limited;
400 counter_u64_t rack_alloc_limited_conns;
401 counter_u64_t rack_split_limited;
402 counter_u64_t rack_rxt_clamps_cwnd;
403 counter_u64_t rack_rxt_clamps_cwnd_uniq;
404 
405 counter_u64_t rack_multi_single_eq;
406 counter_u64_t rack_proc_non_comp_ack;
407 
408 counter_u64_t rack_fto_send;
409 counter_u64_t rack_fto_rsm_send;
410 counter_u64_t rack_nfto_resend;
411 counter_u64_t rack_non_fto_send;
412 counter_u64_t rack_extended_rfo;
413 
414 counter_u64_t rack_sack_proc_all;
415 counter_u64_t rack_sack_proc_short;
416 counter_u64_t rack_sack_proc_restart;
417 counter_u64_t rack_sack_attacks_detected;
418 counter_u64_t rack_sack_attacks_reversed;
419 counter_u64_t rack_sack_attacks_suspect;
420 counter_u64_t rack_sack_used_next_merge;
421 counter_u64_t rack_sack_splits;
422 counter_u64_t rack_sack_used_prev_merge;
423 counter_u64_t rack_sack_skipped_acked;
424 counter_u64_t rack_ack_total;
425 counter_u64_t rack_express_sack;
426 counter_u64_t rack_sack_total;
427 counter_u64_t rack_move_none;
428 counter_u64_t rack_move_some;
429 
430 counter_u64_t rack_input_idle_reduces;
431 counter_u64_t rack_collapsed_win;
432 counter_u64_t rack_collapsed_win_seen;
433 counter_u64_t rack_collapsed_win_rxt;
434 counter_u64_t rack_collapsed_win_rxt_bytes;
435 counter_u64_t rack_try_scwnd;
436 counter_u64_t rack_hw_pace_init_fail;
437 counter_u64_t rack_hw_pace_lost;
438 
439 counter_u64_t rack_out_size[TCP_MSS_ACCT_SIZE];
440 counter_u64_t rack_opts_arry[RACK_OPTS_SIZE];
441 
442 
443 #define	RACK_REXMTVAL(tp) max(rack_rto_min, ((tp)->t_srtt + ((tp)->t_rttvar << 2)))
444 
445 #define	RACK_TCPT_RANGESET(tv, value, tvmin, tvmax, slop) do {	\
446 	(tv) = (value) + slop;	 \
447 	if ((u_long)(tv) < (u_long)(tvmin)) \
448 		(tv) = (tvmin); \
449 	if ((u_long)(tv) > (u_long)(tvmax)) \
450 		(tv) = (tvmax); \
451 } while (0)
452 
453 static void
454 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick,  int event, int line);
455 
456 static int
457 rack_process_ack(struct mbuf *m, struct tcphdr *th,
458     struct socket *so, struct tcpcb *tp, struct tcpopt *to,
459     uint32_t tiwin, int32_t tlen, int32_t * ofia, int32_t thflags, int32_t * ret_val, int32_t orig_tlen);
460 static int
461 rack_process_data(struct mbuf *m, struct tcphdr *th,
462     struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
463     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt);
464 static void
465 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack,
466    uint32_t th_ack, uint16_t nsegs, uint16_t type, int32_t recovery);
467 static struct rack_sendmap *rack_alloc(struct tcp_rack *rack);
468 static struct rack_sendmap *rack_alloc_limit(struct tcp_rack *rack,
469     uint8_t limit_type);
470 static struct rack_sendmap *
471 rack_check_recovery_mode(struct tcpcb *tp,
472     uint32_t tsused);
473 static uint32_t
474 rack_grab_rtt(struct tcpcb *tp, struct tcp_rack *rack);
475 static void
476 rack_cong_signal(struct tcpcb *tp,
477 		 uint32_t type, uint32_t ack, int );
478 static void rack_counter_destroy(void);
479 static int
480 rack_ctloutput(struct tcpcb *tp, struct sockopt *sopt);
481 static int32_t rack_ctor(void *mem, int32_t size, void *arg, int32_t how);
482 static void
483 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override);
484 static void
485 rack_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
486     int32_t drop_hdrlen, int32_t tlen, uint8_t iptos);
487 static void rack_dtor(void *mem, int32_t size, void *arg);
488 static void
489 rack_log_alt_to_to_cancel(struct tcp_rack *rack,
490     uint32_t flex1, uint32_t flex2,
491     uint32_t flex3, uint32_t flex4,
492     uint32_t flex5, uint32_t flex6,
493     uint16_t flex7, uint8_t mod);
494 
495 static void
496 rack_log_pacing_delay_calc(struct tcp_rack *rack, uint32_t len, uint32_t slot,
497    uint64_t bw_est, uint64_t bw, uint64_t len_time, int method, int line,
498    struct rack_sendmap *rsm, uint8_t quality);
499 static struct rack_sendmap *
500 rack_find_high_nonack(struct tcp_rack *rack,
501     struct rack_sendmap *rsm);
502 static struct rack_sendmap *rack_find_lowest_rsm(struct tcp_rack *rack);
503 static void rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm);
504 static void rack_fini(struct tcpcb *tp, int32_t tcb_is_purged);
505 static int rack_get_sockopt(struct tcpcb *tp, struct sockopt *sopt);
506 static void
507 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
508 			    tcp_seq th_ack, int line, uint8_t quality);
509 static void
510 rack_log_type_pacing_sizes(struct tcpcb *tp, struct tcp_rack *rack, uint32_t arg1, uint32_t arg2, uint32_t arg3, uint8_t frm);
511 
512 static uint32_t
513 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss);
514 static int32_t rack_handoff_ok(struct tcpcb *tp);
515 static int32_t rack_init(struct tcpcb *tp, void **ptr);
516 static void rack_init_sysctls(void);
517 
518 static void
519 rack_log_ack(struct tcpcb *tp, struct tcpopt *to,
520     struct tcphdr *th, int entered_rec, int dup_ack_struck,
521     int *dsack_seen, int *sacks_seen);
522 static void
523 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
524     uint32_t seq_out, uint16_t th_flags, int32_t err, uint64_t ts,
525     struct rack_sendmap *hintrsm, uint32_t add_flags, struct mbuf *s_mb, uint32_t s_moff, int hw_tls, int segsiz);
526 
527 static uint64_t rack_get_gp_est(struct tcp_rack *rack);
528 
529 
530 static void
531 rack_log_sack_passed(struct tcpcb *tp, struct tcp_rack *rack,
532     struct rack_sendmap *rsm, uint32_t cts);
533 static void rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm);
534 static int32_t rack_output(struct tcpcb *tp);
535 
536 static uint32_t
537 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack,
538     struct sackblk *sack, struct tcpopt *to, struct rack_sendmap **prsm,
539     uint32_t cts, int *no_extra, int *moved_two, uint32_t segsiz);
540 static void rack_post_recovery(struct tcpcb *tp, uint32_t th_seq);
541 static void rack_remxt_tmr(struct tcpcb *tp);
542 static int rack_set_sockopt(struct tcpcb *tp, struct sockopt *sopt);
543 static void rack_set_state(struct tcpcb *tp, struct tcp_rack *rack);
544 static int32_t rack_stopall(struct tcpcb *tp);
545 static void rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line);
546 static uint32_t
547 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack,
548     struct rack_sendmap *rsm, uint64_t ts, int32_t * lenp, uint32_t add_flag, int segsiz);
549 static void
550 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
551     struct rack_sendmap *rsm, uint64_t ts, uint32_t add_flag, int segsiz);
552 static int
553 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack,
554     struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack);
555 static int32_t tcp_addrack(module_t mod, int32_t type, void *data);
556 static int
557 rack_do_close_wait(struct mbuf *m, struct tcphdr *th,
558     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
559     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
560 
561 static void
562 rack_peg_rxt(struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t segsiz);
563 
564 static int
565 rack_do_closing(struct mbuf *m, struct tcphdr *th,
566     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
567     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
568 static int
569 rack_do_established(struct mbuf *m, struct tcphdr *th,
570     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
571     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
572 static int
573 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th,
574     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
575     int32_t tlen, uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos);
576 static int
577 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th,
578     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
579     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
580 static int
581 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th,
582     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
583     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
584 static int
585 rack_do_lastack(struct mbuf *m, struct tcphdr *th,
586     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
587     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
588 static int
589 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th,
590     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
591     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
592 static int
593 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th,
594     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
595     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
596 static void rack_chk_req_and_hybrid_on_out(struct tcp_rack *rack, tcp_seq seq, uint32_t len, uint64_t cts);
597 struct rack_sendmap *
598 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack,
599     uint32_t tsused);
600 static void tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt,
601     uint32_t len, uint32_t us_tim, int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt);
602 static void
603      tcp_rack_partialack(struct tcpcb *tp);
604 static int
605 rack_set_profile(struct tcp_rack *rack, int prof);
606 static void
607 rack_apply_deferred_options(struct tcp_rack *rack);
608 
609 int32_t rack_clear_counter=0;
610 
611 static uint64_t
612 rack_get_lt_bw(struct tcp_rack *rack)
613 {
614 	struct timeval tv;
615 	uint64_t tim, bytes;
616 
617 	tim = rack->r_ctl.lt_bw_time;
618 	bytes = rack->r_ctl.lt_bw_bytes;
619 	if (rack->lt_bw_up) {
620 		/* Include all the current bytes too */
621 		microuptime(&tv);
622 		bytes += (rack->rc_tp->snd_una - rack->r_ctl.lt_seq);
623 		tim += (tcp_tv_to_lusectick(&tv) - rack->r_ctl.lt_timemark);
624 	}
625 	if ((bytes != 0) && (tim != 0))
626 		return ((bytes * (uint64_t)1000000) / tim);
627 	else
628 		return (0);
629 }
630 
631 static void
632 rack_swap_beta_values(struct tcp_rack *rack, uint8_t flex8)
633 {
634 	struct sockopt sopt;
635 	struct cc_newreno_opts opt;
636 	struct newreno old;
637 	struct tcpcb *tp;
638 	int error, failed = 0;
639 
640 	tp = rack->rc_tp;
641 	if (tp->t_cc == NULL) {
642 		/* Tcb is leaving */
643 		return;
644 	}
645 	rack->rc_pacing_cc_set = 1;
646 	if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0) {
647 		/* Not new-reno we can't play games with beta! */
648 		failed = 1;
649 		goto out;
650 
651 	}
652 	if (CC_ALGO(tp)->ctl_output == NULL)  {
653 		/* Huh, not using new-reno so no swaps.? */
654 		failed = 2;
655 		goto out;
656 	}
657 	/* Get the current values out */
658 	sopt.sopt_valsize = sizeof(struct cc_newreno_opts);
659 	sopt.sopt_dir = SOPT_GET;
660 	opt.name = CC_NEWRENO_BETA;
661 	error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
662 	if (error)  {
663 		failed = 3;
664 		goto out;
665 	}
666 	old.beta = opt.val;
667 	opt.name = CC_NEWRENO_BETA_ECN;
668 	error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
669 	if (error)  {
670 		failed = 4;
671 		goto out;
672 	}
673 	old.beta_ecn = opt.val;
674 
675 	/* Now lets set in the values we have stored */
676 	sopt.sopt_dir = SOPT_SET;
677 	opt.name = CC_NEWRENO_BETA;
678 	opt.val = rack->r_ctl.rc_saved_beta.beta;
679 	error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
680 	if (error)  {
681 		failed = 5;
682 		goto out;
683 	}
684 	opt.name = CC_NEWRENO_BETA_ECN;
685 	opt.val = rack->r_ctl.rc_saved_beta.beta_ecn;
686 	error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
687 	if (error) {
688 		failed = 6;
689 		goto out;
690 	}
691 	/* Save off the values for restoral */
692 	memcpy(&rack->r_ctl.rc_saved_beta, &old, sizeof(struct newreno));
693 out:
694 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
695 		union tcp_log_stackspecific log;
696 		struct timeval tv;
697 		struct newreno *ptr;
698 
699 		ptr = ((struct newreno *)tp->t_ccv.cc_data);
700 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
701 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
702 		log.u_bbr.flex1 = ptr->beta;
703 		log.u_bbr.flex2 = ptr->beta_ecn;
704 		log.u_bbr.flex3 = ptr->newreno_flags;
705 		log.u_bbr.flex4 = rack->r_ctl.rc_saved_beta.beta;
706 		log.u_bbr.flex5 = rack->r_ctl.rc_saved_beta.beta_ecn;
707 		log.u_bbr.flex6 = failed;
708 		log.u_bbr.flex7 = rack->gp_ready;
709 		log.u_bbr.flex7 <<= 1;
710 		log.u_bbr.flex7 |= rack->use_fixed_rate;
711 		log.u_bbr.flex7 <<= 1;
712 		log.u_bbr.flex7 |= rack->rc_pacing_cc_set;
713 		log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
714 		log.u_bbr.flex8 = flex8;
715 		tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, error,
716 			       0, &log, false, NULL, NULL, 0, &tv);
717 	}
718 }
719 
720 static void
721 rack_set_cc_pacing(struct tcp_rack *rack)
722 {
723 	if (rack->rc_pacing_cc_set)
724 		return;
725 	/*
726 	 * Use the swap utility placing in 3 for flex8 to id a
727 	 * set of a new set of values.
728 	 */
729 	rack->rc_pacing_cc_set = 1;
730 	rack_swap_beta_values(rack, 3);
731 }
732 
733 static void
734 rack_undo_cc_pacing(struct tcp_rack *rack)
735 {
736 	if (rack->rc_pacing_cc_set == 0)
737 		return;
738 	/*
739 	 * Use the swap utility placing in 4 for flex8 to id a
740 	 * restoral of the old values.
741 	 */
742 	rack->rc_pacing_cc_set = 0;
743 	rack_swap_beta_values(rack, 4);
744 }
745 
746 static void
747 rack_remove_pacing(struct tcp_rack *rack)
748 {
749 	if (rack->rc_pacing_cc_set)
750 		rack_undo_cc_pacing(rack);
751 	if (rack->r_ctl.pacing_method & RACK_REG_PACING)
752 		tcp_decrement_paced_conn();
753 	if (rack->r_ctl.pacing_method & RACK_DGP_PACING)
754 		tcp_dec_dgp_pacing_cnt();
755 	rack->rc_always_pace = 0;
756 	rack->r_ctl.pacing_method = RACK_PACING_NONE;
757 	rack->dgp_on = 0;
758 	rack->rc_hybrid_mode = 0;
759 	rack->use_fixed_rate = 0;
760 }
761 
762 static void
763 rack_log_gpset(struct tcp_rack *rack, uint32_t seq_end, uint32_t ack_end_t,
764 	       uint32_t send_end_t, int line, uint8_t mode, struct rack_sendmap *rsm)
765 {
766 	if (tcp_bblogging_on(rack->rc_tp) && (rack_verbose_logging != 0)) {
767 		union tcp_log_stackspecific log;
768 		struct timeval tv;
769 
770 		memset(&log, 0, sizeof(log));
771 		log.u_bbr.flex1 = seq_end;
772 		log.u_bbr.flex2 = rack->rc_tp->gput_seq;
773 		log.u_bbr.flex3 = ack_end_t;
774 		log.u_bbr.flex4 = rack->rc_tp->gput_ts;
775 		log.u_bbr.flex5 = send_end_t;
776 		log.u_bbr.flex6 = rack->rc_tp->gput_ack;
777 		log.u_bbr.flex7 = mode;
778 		log.u_bbr.flex8 = 69;
779 		log.u_bbr.rttProp = rack->r_ctl.rc_gp_cumack_ts;
780 		log.u_bbr.delRate = rack->r_ctl.rc_gp_output_ts;
781 		log.u_bbr.pkts_out = line;
782 		log.u_bbr.cwnd_gain = rack->app_limited_needs_set;
783 		log.u_bbr.pkt_epoch = rack->r_ctl.rc_app_limited_cnt;
784 		log.u_bbr.epoch = rack->r_ctl.current_round;
785 		log.u_bbr.lt_epoch = rack->r_ctl.rc_considered_lost;
786 		if (rsm != NULL) {
787 			log.u_bbr.applimited = rsm->r_start;
788 			log.u_bbr.delivered = rsm->r_end;
789 			log.u_bbr.epoch = rsm->r_flags;
790 		}
791 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
792 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
793 		    &rack->rc_inp->inp_socket->so_rcv,
794 		    &rack->rc_inp->inp_socket->so_snd,
795 		    BBR_LOG_HPTSI_CALC, 0,
796 		    0, &log, false, &tv);
797 	}
798 }
799 
800 static int
801 sysctl_rack_clear(SYSCTL_HANDLER_ARGS)
802 {
803 	uint32_t stat;
804 	int32_t error;
805 
806 	error = SYSCTL_OUT(req, &rack_clear_counter, sizeof(uint32_t));
807 	if (error || req->newptr == NULL)
808 		return error;
809 
810 	error = SYSCTL_IN(req, &stat, sizeof(uint32_t));
811 	if (error)
812 		return (error);
813 	if (stat == 1) {
814 #ifdef INVARIANTS
815 		printf("Clearing RACK counters\n");
816 #endif
817 		counter_u64_zero(rack_tlp_tot);
818 		counter_u64_zero(rack_tlp_newdata);
819 		counter_u64_zero(rack_tlp_retran);
820 		counter_u64_zero(rack_tlp_retran_bytes);
821 		counter_u64_zero(rack_to_tot);
822 		counter_u64_zero(rack_saw_enobuf);
823 		counter_u64_zero(rack_saw_enobuf_hw);
824 		counter_u64_zero(rack_saw_enetunreach);
825 		counter_u64_zero(rack_persists_sends);
826 		counter_u64_zero(rack_total_bytes);
827 		counter_u64_zero(rack_persists_acks);
828 		counter_u64_zero(rack_persists_loss);
829 		counter_u64_zero(rack_persists_lost_ends);
830 #ifdef INVARIANTS
831 		counter_u64_zero(rack_adjust_map_bw);
832 #endif
833 		counter_u64_zero(rack_to_alloc_hard);
834 		counter_u64_zero(rack_to_alloc_emerg);
835 		counter_u64_zero(rack_sack_proc_all);
836 		counter_u64_zero(rack_fto_send);
837 		counter_u64_zero(rack_fto_rsm_send);
838 		counter_u64_zero(rack_extended_rfo);
839 		counter_u64_zero(rack_hw_pace_init_fail);
840 		counter_u64_zero(rack_hw_pace_lost);
841 		counter_u64_zero(rack_non_fto_send);
842 		counter_u64_zero(rack_nfto_resend);
843 		counter_u64_zero(rack_sack_proc_short);
844 		counter_u64_zero(rack_sack_proc_restart);
845 		counter_u64_zero(rack_to_alloc);
846 		counter_u64_zero(rack_to_alloc_limited);
847 		counter_u64_zero(rack_alloc_limited_conns);
848 		counter_u64_zero(rack_split_limited);
849 		counter_u64_zero(rack_rxt_clamps_cwnd);
850 		counter_u64_zero(rack_rxt_clamps_cwnd_uniq);
851 		counter_u64_zero(rack_multi_single_eq);
852 		counter_u64_zero(rack_proc_non_comp_ack);
853 		counter_u64_zero(rack_sack_attacks_detected);
854 		counter_u64_zero(rack_sack_attacks_reversed);
855 		counter_u64_zero(rack_sack_attacks_suspect);
856 		counter_u64_zero(rack_sack_used_next_merge);
857 		counter_u64_zero(rack_sack_used_prev_merge);
858 		counter_u64_zero(rack_sack_splits);
859 		counter_u64_zero(rack_sack_skipped_acked);
860 		counter_u64_zero(rack_ack_total);
861 		counter_u64_zero(rack_express_sack);
862 		counter_u64_zero(rack_sack_total);
863 		counter_u64_zero(rack_move_none);
864 		counter_u64_zero(rack_move_some);
865 		counter_u64_zero(rack_try_scwnd);
866 		counter_u64_zero(rack_collapsed_win);
867 		counter_u64_zero(rack_collapsed_win_rxt);
868 		counter_u64_zero(rack_collapsed_win_seen);
869 		counter_u64_zero(rack_collapsed_win_rxt_bytes);
870 	} else if (stat == 2) {
871 #ifdef INVARIANTS
872 		printf("Clearing RACK option array\n");
873 #endif
874 		COUNTER_ARRAY_ZERO(rack_opts_arry, RACK_OPTS_SIZE);
875 	} else if (stat == 3) {
876 		printf("Rack has no stats counters to clear (use 1 to clear all stats in sysctl node)\n");
877 	} else if (stat == 4) {
878 #ifdef INVARIANTS
879 		printf("Clearing RACK out size array\n");
880 #endif
881 		COUNTER_ARRAY_ZERO(rack_out_size, TCP_MSS_ACCT_SIZE);
882 	}
883 	rack_clear_counter = 0;
884 	return (0);
885 }
886 
887 static void
888 rack_init_sysctls(void)
889 {
890 	struct sysctl_oid *rack_counters;
891 	struct sysctl_oid *rack_attack;
892 	struct sysctl_oid *rack_pacing;
893 	struct sysctl_oid *rack_timely;
894 	struct sysctl_oid *rack_timers;
895 	struct sysctl_oid *rack_tlp;
896 	struct sysctl_oid *rack_misc;
897 	struct sysctl_oid *rack_features;
898 	struct sysctl_oid *rack_measure;
899 	struct sysctl_oid *rack_probertt;
900 	struct sysctl_oid *rack_hw_pacing;
901 	struct sysctl_oid *rack_policing;
902 
903 	rack_attack = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
904 	    SYSCTL_CHILDREN(rack_sysctl_root),
905 	    OID_AUTO,
906 	    "sack_attack",
907 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
908 	    "Rack Sack Attack Counters and Controls");
909 	rack_counters = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
910 	    SYSCTL_CHILDREN(rack_sysctl_root),
911 	    OID_AUTO,
912 	    "stats",
913 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
914 	    "Rack Counters");
915 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
916 	    SYSCTL_CHILDREN(rack_sysctl_root),
917 	    OID_AUTO, "rate_sample_method", CTLFLAG_RW,
918 	    &rack_rate_sample_method , USE_RTT_LOW,
919 	    "What method should we use for rate sampling 0=high, 1=low ");
920 	/* Probe rtt related controls */
921 	rack_probertt = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
922 	    SYSCTL_CHILDREN(rack_sysctl_root),
923 	    OID_AUTO,
924 	    "probertt",
925 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
926 	    "ProbeRTT related Controls");
927 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
928 	    SYSCTL_CHILDREN(rack_probertt),
929 	    OID_AUTO, "exit_per_hpb", CTLFLAG_RW,
930 	    &rack_atexit_prtt_hbp, 130,
931 	    "What percentage above goodput do we clamp CA/SS to at exit on high-BDP path 110%");
932 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
933 	    SYSCTL_CHILDREN(rack_probertt),
934 	    OID_AUTO, "exit_per_nonhpb", CTLFLAG_RW,
935 	    &rack_atexit_prtt, 130,
936 	    "What percentage above goodput do we clamp CA/SS to at exit on a non high-BDP path 100%");
937 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
938 	    SYSCTL_CHILDREN(rack_probertt),
939 	    OID_AUTO, "gp_per_mul", CTLFLAG_RW,
940 	    &rack_per_of_gp_probertt, 60,
941 	    "What percentage of goodput do we pace at in probertt");
942 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
943 	    SYSCTL_CHILDREN(rack_probertt),
944 	    OID_AUTO, "gp_per_reduce", CTLFLAG_RW,
945 	    &rack_per_of_gp_probertt_reduce, 10,
946 	    "What percentage of goodput do we reduce every gp_srtt");
947 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
948 	    SYSCTL_CHILDREN(rack_probertt),
949 	    OID_AUTO, "gp_per_low", CTLFLAG_RW,
950 	    &rack_per_of_gp_lowthresh, 40,
951 	    "What percentage of goodput do we allow the multiplier to fall to");
952 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
953 	    SYSCTL_CHILDREN(rack_probertt),
954 	    OID_AUTO, "time_between", CTLFLAG_RW,
955 	    & rack_time_between_probertt, 96000000,
956 	    "How many useconds between the lowest rtt falling must past before we enter probertt");
957 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
958 	    SYSCTL_CHILDREN(rack_probertt),
959 	    OID_AUTO, "safety", CTLFLAG_RW,
960 	    &rack_probe_rtt_safety_val, 2000000,
961 	    "If not zero, provides a maximum usecond that you can stay in probertt (2sec = 2000000)");
962 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
963 	    SYSCTL_CHILDREN(rack_probertt),
964 	    OID_AUTO, "sets_cwnd", CTLFLAG_RW,
965 	    &rack_probe_rtt_sets_cwnd, 0,
966 	    "Do we set the cwnd too (if always_lower is on)");
967 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
968 	    SYSCTL_CHILDREN(rack_probertt),
969 	    OID_AUTO, "maxdrainsrtts", CTLFLAG_RW,
970 	    &rack_max_drain_wait, 2,
971 	    "Maximum number of gp_srtt's to hold in drain waiting for flight to reach goal");
972 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
973 	    SYSCTL_CHILDREN(rack_probertt),
974 	    OID_AUTO, "mustdrainsrtts", CTLFLAG_RW,
975 	    &rack_must_drain, 1,
976 	    "We must drain this many gp_srtt's waiting for flight to reach goal");
977 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
978 	    SYSCTL_CHILDREN(rack_probertt),
979 	    OID_AUTO, "goal_use_min_entry", CTLFLAG_RW,
980 	    &rack_probertt_use_min_rtt_entry, 1,
981 	    "Should we use the min-rtt to calculate the goal rtt (else gp_srtt) at entry");
982 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
983 	    SYSCTL_CHILDREN(rack_probertt),
984 	    OID_AUTO, "goal_use_min_exit", CTLFLAG_RW,
985 	    &rack_probertt_use_min_rtt_exit, 0,
986 	    "How to set cwnd at exit, 0 - dynamic, 1 - use min-rtt, 2 - use curgprtt, 3 - entry gp-rtt");
987 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
988 	    SYSCTL_CHILDREN(rack_probertt),
989 	    OID_AUTO, "length_div", CTLFLAG_RW,
990 	    &rack_probertt_gpsrtt_cnt_div, 0,
991 	    "How many recent goodput srtt periods plus hold tim does probertt last (bottom of fraction)");
992 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
993 	    SYSCTL_CHILDREN(rack_probertt),
994 	    OID_AUTO, "length_mul", CTLFLAG_RW,
995 	    &rack_probertt_gpsrtt_cnt_mul, 0,
996 	    "How many recent goodput srtt periods plus hold tim does probertt last (top of fraction)");
997 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
998 	    SYSCTL_CHILDREN(rack_probertt),
999 	    OID_AUTO, "holdtim_at_target", CTLFLAG_RW,
1000 	    &rack_min_probertt_hold, 200000,
1001 	    "What is the minimum time we hold probertt at target");
1002 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1003 	    SYSCTL_CHILDREN(rack_probertt),
1004 	    OID_AUTO, "filter_life", CTLFLAG_RW,
1005 	    &rack_probertt_filter_life, 10000000,
1006 	    "What is the time for the filters life in useconds");
1007 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1008 	    SYSCTL_CHILDREN(rack_probertt),
1009 	    OID_AUTO, "lower_within", CTLFLAG_RW,
1010 	    &rack_probertt_lower_within, 10,
1011 	    "If the rtt goes lower within this percentage of the time, go into probe-rtt");
1012 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1013 	    SYSCTL_CHILDREN(rack_probertt),
1014 	    OID_AUTO, "must_move", CTLFLAG_RW,
1015 	    &rack_min_rtt_movement, 250,
1016 	    "How much is the minimum movement in rtt to count as a drop for probertt purposes");
1017 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1018 	    SYSCTL_CHILDREN(rack_probertt),
1019 	    OID_AUTO, "clear_is_cnts", CTLFLAG_RW,
1020 	    &rack_probertt_clear_is, 1,
1021 	    "Do we clear I/S counts on exiting probe-rtt");
1022 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1023 	    SYSCTL_CHILDREN(rack_probertt),
1024 	    OID_AUTO, "hbp_extra_drain", CTLFLAG_RW,
1025 	    &rack_max_drain_hbp, 1,
1026 	    "How many extra drain gpsrtt's do we get in highly buffered paths");
1027 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1028 	    SYSCTL_CHILDREN(rack_probertt),
1029 	    OID_AUTO, "hbp_threshold", CTLFLAG_RW,
1030 	    &rack_hbp_thresh, 3,
1031 	    "We are highly buffered if min_rtt_seen / max_rtt_seen > this-threshold");
1032 	/* Pacing related sysctls */
1033 	rack_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1034 	    SYSCTL_CHILDREN(rack_sysctl_root),
1035 	    OID_AUTO,
1036 	    "pacing",
1037 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1038 	    "Pacing related Controls");
1039 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1040 	    SYSCTL_CHILDREN(rack_pacing),
1041 	    OID_AUTO, "pcm_enabled", CTLFLAG_RW,
1042 	    &rack_pcm_is_enabled, 1,
1043 	    "Do we by default do PCM measurements?");
1044 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1045 	    SYSCTL_CHILDREN(rack_pacing),
1046 	    OID_AUTO, "pcm_rnds", CTLFLAG_RW,
1047 	    &rack_pcm_every_n_rounds, 100,
1048 	    "How many rounds before we need to do a PCM measurement");
1049 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1050 	    SYSCTL_CHILDREN(rack_pacing),
1051 	    OID_AUTO, "pcm_blast", CTLFLAG_RW,
1052 	    &rack_pcm_blast, 0,
1053 	    "Blast out the full cwnd/rwnd when doing a PCM measurement");
1054 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1055 	    SYSCTL_CHILDREN(rack_pacing),
1056 	    OID_AUTO, "rnd_gp_gain", CTLFLAG_RW,
1057 	    &rack_gp_gain_req, 1200,
1058 	    "How much do we have to increase the GP to record the round 1200 = 120.0");
1059 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1060 	    SYSCTL_CHILDREN(rack_pacing),
1061 	    OID_AUTO, "dgp_out_of_ss_at", CTLFLAG_RW,
1062 	    &rack_rnd_cnt_req, 0x10005,
1063 	    "How many rounds less than rnd_gp_gain will drop us out of SS");
1064 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1065 	    SYSCTL_CHILDREN(rack_pacing),
1066 	    OID_AUTO, "no_timely", CTLFLAG_RW,
1067 	    &rack_timely_off, 0,
1068 	    "Do we not use timely in DGP?");
1069 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1070 	    SYSCTL_CHILDREN(rack_pacing),
1071 	    OID_AUTO, "fullbufdisc", CTLFLAG_RW,
1072 	    &rack_full_buffer_discount, 10,
1073 	    "What percentage b/w reduction over the GP estimate for a full buffer (default=0 off)?");
1074 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1075 	    SYSCTL_CHILDREN(rack_pacing),
1076 	    OID_AUTO, "fillcw", CTLFLAG_RW,
1077 	    &rack_fill_cw_state, 0,
1078 	    "Enable fillcw on new connections (default=0 off)?");
1079 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1080 	    SYSCTL_CHILDREN(rack_pacing),
1081 	    OID_AUTO, "min_burst", CTLFLAG_RW,
1082 	    &rack_pacing_min_seg, 0,
1083 	    "What is the min burst size for pacing (0 disables)?");
1084 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1085 	    SYSCTL_CHILDREN(rack_pacing),
1086 	    OID_AUTO, "divisor", CTLFLAG_RW,
1087 	    &rack_default_pacing_divisor, 250,
1088 	    "What is the default divisor given to the rl code?");
1089 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1090 	    SYSCTL_CHILDREN(rack_pacing),
1091 	    OID_AUTO, "fillcw_max_mult", CTLFLAG_RW,
1092 	    &rack_bw_multipler, 0,
1093 	    "What is the limit multiplier of the current gp_est that fillcw can increase the b/w too, 200 == 200% (0 = off)?");
1094 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1095 	    SYSCTL_CHILDREN(rack_pacing),
1096 	    OID_AUTO, "max_pace_over", CTLFLAG_RW,
1097 	    &rack_max_per_above, 30,
1098 	    "What is the maximum allowable percentage that we can pace above (so 30 = 130% of our goal)");
1099 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1100 	    SYSCTL_CHILDREN(rack_pacing),
1101 	    OID_AUTO, "allow1mss", CTLFLAG_RW,
1102 	    &rack_pace_one_seg, 0,
1103 	    "Do we allow low b/w pacing of 1MSS instead of two (1.2Meg and less)?");
1104 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1105 	    SYSCTL_CHILDREN(rack_pacing),
1106 	    OID_AUTO, "limit_wsrtt", CTLFLAG_RW,
1107 	    &rack_limit_time_with_srtt, 0,
1108 	    "Do we limit pacing time based on srtt");
1109 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1110 	    SYSCTL_CHILDREN(rack_pacing),
1111 	    OID_AUTO, "gp_per_ss", CTLFLAG_RW,
1112 	    &rack_per_of_gp_ss, 250,
1113 	    "If non zero, what percentage of goodput to pace at in slow start");
1114 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1115 	    SYSCTL_CHILDREN(rack_pacing),
1116 	    OID_AUTO, "gp_per_ca", CTLFLAG_RW,
1117 	    &rack_per_of_gp_ca, 150,
1118 	    "If non zero, what percentage of goodput to pace at in congestion avoidance");
1119 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1120 	    SYSCTL_CHILDREN(rack_pacing),
1121 	    OID_AUTO, "gp_per_rec", CTLFLAG_RW,
1122 	    &rack_per_of_gp_rec, 200,
1123 	    "If non zero, what percentage of goodput to pace at in recovery");
1124 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1125 	    SYSCTL_CHILDREN(rack_pacing),
1126 	    OID_AUTO, "pace_max_seg", CTLFLAG_RW,
1127 	    &rack_hptsi_segments, 40,
1128 	    "What size is the max for TSO segments in pacing and burst mitigation");
1129 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1130 	    SYSCTL_CHILDREN(rack_pacing),
1131 	    OID_AUTO, "burst_reduces", CTLFLAG_RW,
1132 	    &rack_slot_reduction, 4,
1133 	    "When doing only burst mitigation what is the reduce divisor");
1134 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1135 	    SYSCTL_CHILDREN(rack_sysctl_root),
1136 	    OID_AUTO, "use_pacing", CTLFLAG_RW,
1137 	    &rack_pace_every_seg, 0,
1138 	    "If set we use pacing, if clear we use only the original burst mitigation");
1139 	SYSCTL_ADD_U64(&rack_sysctl_ctx,
1140 	    SYSCTL_CHILDREN(rack_pacing),
1141 	    OID_AUTO, "rate_cap", CTLFLAG_RW,
1142 	    &rack_bw_rate_cap, 0,
1143 	    "If set we apply this value to the absolute rate cap used by pacing");
1144 	SYSCTL_ADD_U64(&rack_sysctl_ctx,
1145 	    SYSCTL_CHILDREN(rack_pacing),
1146 	    OID_AUTO, "fillcw_cap", CTLFLAG_RW,
1147 	    &rack_fillcw_bw_cap, 3750000,
1148 	    "Do we have an absolute cap on the amount of b/w fillcw can specify (0 = no)?");
1149 	SYSCTL_ADD_U8(&rack_sysctl_ctx,
1150 	    SYSCTL_CHILDREN(rack_sysctl_root),
1151 	    OID_AUTO, "req_measure_cnt", CTLFLAG_RW,
1152 	    &rack_req_measurements, 1,
1153 	    "If doing dynamic pacing, how many measurements must be in before we start pacing?");
1154 	/* Hardware pacing */
1155 	rack_hw_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1156 	    SYSCTL_CHILDREN(rack_sysctl_root),
1157 	    OID_AUTO,
1158 	    "hdwr_pacing",
1159 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1160 	    "Pacing related Controls");
1161 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1162 	    SYSCTL_CHILDREN(rack_hw_pacing),
1163 	    OID_AUTO, "rwnd_factor", CTLFLAG_RW,
1164 	    &rack_hw_rwnd_factor, 2,
1165 	    "How many times does snd_wnd need to be bigger than pace_max_seg so we will hold off and get more acks?");
1166 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1167 	    SYSCTL_CHILDREN(rack_hw_pacing),
1168 	    OID_AUTO, "precheck", CTLFLAG_RW,
1169 	    &rack_hw_check_queue, 0,
1170 	    "Do we always precheck the hdwr pacing queue to avoid ENOBUF's?");
1171 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1172 	    SYSCTL_CHILDREN(rack_hw_pacing),
1173 	    OID_AUTO, "pace_enobuf_mult", CTLFLAG_RW,
1174 	    &rack_enobuf_hw_boost_mult, 0,
1175 	    "By how many time_betweens should we boost the pacing time if we see a ENOBUFS?");
1176 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1177 	    SYSCTL_CHILDREN(rack_hw_pacing),
1178 	    OID_AUTO, "pace_enobuf_max", CTLFLAG_RW,
1179 	    &rack_enobuf_hw_max, 2,
1180 	    "What is the max boost the pacing time if we see a ENOBUFS?");
1181 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1182 	    SYSCTL_CHILDREN(rack_hw_pacing),
1183 	    OID_AUTO, "pace_enobuf_min", CTLFLAG_RW,
1184 	    &rack_enobuf_hw_min, 2,
1185 	    "What is the min boost the pacing time if we see a ENOBUFS?");
1186 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1187 	    SYSCTL_CHILDREN(rack_hw_pacing),
1188 	    OID_AUTO, "enable", CTLFLAG_RW,
1189 	    &rack_enable_hw_pacing, 0,
1190 	    "Should RACK attempt to use hw pacing?");
1191 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1192 	    SYSCTL_CHILDREN(rack_hw_pacing),
1193 	    OID_AUTO, "rate_cap", CTLFLAG_RW,
1194 	    &rack_hw_rate_caps, 0,
1195 	    "Does the highest hardware pacing rate cap the rate we will send at??");
1196 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1197 	    SYSCTL_CHILDREN(rack_hw_pacing),
1198 	    OID_AUTO, "uncap_per", CTLFLAG_RW,
1199 	    &rack_hw_rate_cap_per, 0,
1200 	    "If you go over b/w by this amount you will be uncapped (0 = never)");
1201 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1202 	    SYSCTL_CHILDREN(rack_hw_pacing),
1203 	    OID_AUTO, "rate_min", CTLFLAG_RW,
1204 	    &rack_hw_rate_min, 0,
1205 	    "Do we need a minimum estimate of this many bytes per second in order to engage hw pacing?");
1206 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1207 	    SYSCTL_CHILDREN(rack_hw_pacing),
1208 	    OID_AUTO, "rate_to_low", CTLFLAG_RW,
1209 	    &rack_hw_rate_to_low, 0,
1210 	    "If we fall below this rate, dis-engage hw pacing?");
1211 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1212 	    SYSCTL_CHILDREN(rack_hw_pacing),
1213 	    OID_AUTO, "up_only", CTLFLAG_RW,
1214 	    &rack_hw_up_only, 0,
1215 	    "Do we allow hw pacing to lower the rate selected?");
1216 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1217 	    SYSCTL_CHILDREN(rack_hw_pacing),
1218 	    OID_AUTO, "extra_mss_precise", CTLFLAG_RW,
1219 	    &rack_hw_pace_extra_slots, 0,
1220 	    "If the rates between software and hardware match precisely how many extra time_betweens do we get?");
1221 	rack_timely = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1222 	    SYSCTL_CHILDREN(rack_sysctl_root),
1223 	    OID_AUTO,
1224 	    "timely",
1225 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1226 	    "Rack Timely RTT Controls");
1227 	/* Timely based GP dynmics */
1228 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1229 	    SYSCTL_CHILDREN(rack_timely),
1230 	    OID_AUTO, "upper", CTLFLAG_RW,
1231 	    &rack_gp_per_bw_mul_up, 2,
1232 	    "Rack timely upper range for equal b/w (in percentage)");
1233 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1234 	    SYSCTL_CHILDREN(rack_timely),
1235 	    OID_AUTO, "lower", CTLFLAG_RW,
1236 	    &rack_gp_per_bw_mul_down, 4,
1237 	    "Rack timely lower range for equal b/w (in percentage)");
1238 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1239 	    SYSCTL_CHILDREN(rack_timely),
1240 	    OID_AUTO, "rtt_max_mul", CTLFLAG_RW,
1241 	    &rack_gp_rtt_maxmul, 3,
1242 	    "Rack timely multiplier of lowest rtt for rtt_max");
1243 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1244 	    SYSCTL_CHILDREN(rack_timely),
1245 	    OID_AUTO, "rtt_min_div", CTLFLAG_RW,
1246 	    &rack_gp_rtt_mindiv, 4,
1247 	    "Rack timely divisor used for rtt + (rtt * mul/divisor) for check for lower rtt");
1248 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1249 	    SYSCTL_CHILDREN(rack_timely),
1250 	    OID_AUTO, "rtt_min_mul", CTLFLAG_RW,
1251 	    &rack_gp_rtt_minmul, 1,
1252 	    "Rack timely multiplier used for rtt + (rtt * mul/divisor) for check for lower rtt");
1253 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1254 	    SYSCTL_CHILDREN(rack_timely),
1255 	    OID_AUTO, "decrease", CTLFLAG_RW,
1256 	    &rack_gp_decrease_per, 80,
1257 	    "Rack timely Beta value 80 = .8 (scaled by 100)");
1258 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1259 	    SYSCTL_CHILDREN(rack_timely),
1260 	    OID_AUTO, "increase", CTLFLAG_RW,
1261 	    &rack_gp_increase_per, 2,
1262 	    "Rack timely increase perentage of our GP multiplication factor");
1263 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1264 	    SYSCTL_CHILDREN(rack_timely),
1265 	    OID_AUTO, "lowerbound", CTLFLAG_RW,
1266 	    &rack_per_lower_bound, 50,
1267 	    "Rack timely lowest percentage we allow GP multiplier to fall to");
1268 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1269 	    SYSCTL_CHILDREN(rack_timely),
1270 	    OID_AUTO, "p5_upper", CTLFLAG_RW,
1271 	    &rack_gain_p5_ub, 250,
1272 	    "Profile 5 upper bound to timely gain");
1273 
1274 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1275 	    SYSCTL_CHILDREN(rack_timely),
1276 	    OID_AUTO, "upperboundss", CTLFLAG_RW,
1277 	    &rack_per_upper_bound_ss, 0,
1278 	    "Rack timely highest percentage we allow GP multiplier in SS to raise to (0 is no upperbound)");
1279 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1280 	    SYSCTL_CHILDREN(rack_timely),
1281 	    OID_AUTO, "upperboundca", CTLFLAG_RW,
1282 	    &rack_per_upper_bound_ca, 0,
1283 	    "Rack timely highest percentage we allow GP multiplier to CA raise to (0 is no upperbound)");
1284 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1285 	    SYSCTL_CHILDREN(rack_timely),
1286 	    OID_AUTO, "dynamicgp", CTLFLAG_RW,
1287 	    &rack_do_dyn_mul, 0,
1288 	    "Rack timely do we enable dynmaic timely goodput by default");
1289 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1290 	    SYSCTL_CHILDREN(rack_timely),
1291 	    OID_AUTO, "no_rec_red", CTLFLAG_RW,
1292 	    &rack_gp_no_rec_chg, 1,
1293 	    "Rack timely do we prohibit the recovery multiplier from being lowered");
1294 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1295 	    SYSCTL_CHILDREN(rack_timely),
1296 	    OID_AUTO, "red_clear_cnt", CTLFLAG_RW,
1297 	    &rack_timely_dec_clear, 6,
1298 	    "Rack timely what threshold do we count to before another boost during b/w decent");
1299 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1300 	    SYSCTL_CHILDREN(rack_timely),
1301 	    OID_AUTO, "max_push_rise", CTLFLAG_RW,
1302 	    &rack_timely_max_push_rise, 3,
1303 	    "Rack timely how many times do we push up with b/w increase");
1304 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1305 	    SYSCTL_CHILDREN(rack_timely),
1306 	    OID_AUTO, "max_push_drop", CTLFLAG_RW,
1307 	    &rack_timely_max_push_drop, 3,
1308 	    "Rack timely how many times do we push back on b/w decent");
1309 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1310 	    SYSCTL_CHILDREN(rack_timely),
1311 	    OID_AUTO, "min_segs", CTLFLAG_RW,
1312 	    &rack_timely_min_segs, 4,
1313 	    "Rack timely when setting the cwnd what is the min num segments");
1314 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1315 	    SYSCTL_CHILDREN(rack_timely),
1316 	    OID_AUTO, "noback_max", CTLFLAG_RW,
1317 	    &rack_use_max_for_nobackoff, 0,
1318 	    "Rack timely when deciding if to backoff on a loss, do we use under max rtt else min");
1319 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1320 	    SYSCTL_CHILDREN(rack_timely),
1321 	    OID_AUTO, "interim_timely_only", CTLFLAG_RW,
1322 	    &rack_timely_int_timely_only, 0,
1323 	    "Rack timely when doing interim timely's do we only do timely (no b/w consideration)");
1324 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1325 	    SYSCTL_CHILDREN(rack_timely),
1326 	    OID_AUTO, "nonstop", CTLFLAG_RW,
1327 	    &rack_timely_no_stopping, 0,
1328 	    "Rack timely don't stop increase");
1329 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1330 	    SYSCTL_CHILDREN(rack_timely),
1331 	    OID_AUTO, "dec_raise_thresh", CTLFLAG_RW,
1332 	    &rack_down_raise_thresh, 100,
1333 	    "If the CA or SS is below this threshold raise on the first 3 b/w lowers (0=always)");
1334 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1335 	    SYSCTL_CHILDREN(rack_timely),
1336 	    OID_AUTO, "bottom_drag_segs", CTLFLAG_RW,
1337 	    &rack_req_segs, 1,
1338 	    "Bottom dragging if not these many segments outstanding and room");
1339 
1340 	/* TLP and Rack related parameters */
1341 	rack_tlp = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1342 	    SYSCTL_CHILDREN(rack_sysctl_root),
1343 	    OID_AUTO,
1344 	    "tlp",
1345 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1346 	    "TLP and Rack related Controls");
1347 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1348 	    SYSCTL_CHILDREN(rack_tlp),
1349 	    OID_AUTO, "use_rrr", CTLFLAG_RW,
1350 	    &use_rack_rr, 1,
1351 	    "Do we use Rack Rapid Recovery");
1352 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1353 	    SYSCTL_CHILDREN(rack_tlp),
1354 	    OID_AUTO, "post_rec_labc", CTLFLAG_RW,
1355 	    &rack_max_abc_post_recovery, 2,
1356 	    "Since we do early recovery, do we override the l_abc to a value, if so what?");
1357 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1358 	    SYSCTL_CHILDREN(rack_tlp),
1359 	    OID_AUTO, "nonrxt_use_cr", CTLFLAG_RW,
1360 	    &rack_non_rxt_use_cr, 0,
1361 	    "Do we use ss/ca rate if in recovery we are transmitting a new data chunk");
1362 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1363 	    SYSCTL_CHILDREN(rack_tlp),
1364 	    OID_AUTO, "tlpmethod", CTLFLAG_RW,
1365 	    &rack_tlp_threshold_use, TLP_USE_TWO_ONE,
1366 	    "What method do we do for TLP time calc 0=no-de-ack-comp, 1=ID, 2=2.1, 3=2.2");
1367 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1368 	    SYSCTL_CHILDREN(rack_tlp),
1369 	    OID_AUTO, "limit", CTLFLAG_RW,
1370 	    &rack_tlp_limit, 2,
1371 	    "How many TLP's can be sent without sending new data");
1372 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1373 	    SYSCTL_CHILDREN(rack_tlp),
1374 	    OID_AUTO, "use_greater", CTLFLAG_RW,
1375 	    &rack_tlp_use_greater, 1,
1376 	    "Should we use the rack_rtt time if its greater than srtt");
1377 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1378 	    SYSCTL_CHILDREN(rack_tlp),
1379 	    OID_AUTO, "tlpminto", CTLFLAG_RW,
1380 	    &rack_tlp_min, 10000,
1381 	    "TLP minimum timeout per the specification (in microseconds)");
1382 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1383 	    SYSCTL_CHILDREN(rack_tlp),
1384 	    OID_AUTO, "send_oldest", CTLFLAG_RW,
1385 	    &rack_always_send_oldest, 0,
1386 	    "Should we always send the oldest TLP and RACK-TLP");
1387 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1388 	    SYSCTL_CHILDREN(rack_tlp),
1389 	    OID_AUTO, "tlp_cwnd_flag", CTLFLAG_RW,
1390 	    &rack_lower_cwnd_at_tlp, 0,
1391 	    "When a TLP completes a retran should we enter recovery");
1392 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1393 	    SYSCTL_CHILDREN(rack_tlp),
1394 	    OID_AUTO, "reorder_thresh", CTLFLAG_RW,
1395 	    &rack_reorder_thresh, 2,
1396 	    "What factor for rack will be added when seeing reordering (shift right)");
1397 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1398 	    SYSCTL_CHILDREN(rack_tlp),
1399 	    OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW,
1400 	    &rack_tlp_thresh, 1,
1401 	    "What divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)");
1402 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1403 	    SYSCTL_CHILDREN(rack_tlp),
1404 	    OID_AUTO, "reorder_fade", CTLFLAG_RW,
1405 	    &rack_reorder_fade, 60000000,
1406 	    "Does reorder detection fade, if so how many microseconds (0 means never)");
1407 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1408 	    SYSCTL_CHILDREN(rack_tlp),
1409 	    OID_AUTO, "pktdelay", CTLFLAG_RW,
1410 	    &rack_pkt_delay, 1000,
1411 	    "Extra RACK time (in microseconds) besides reordering thresh");
1412 
1413 	/* Timer related controls */
1414 	rack_timers = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1415 	    SYSCTL_CHILDREN(rack_sysctl_root),
1416 	    OID_AUTO,
1417 	    "timers",
1418 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1419 	    "Timer related controls");
1420 	SYSCTL_ADD_U8(&rack_sysctl_ctx,
1421 	    SYSCTL_CHILDREN(rack_timers),
1422 	    OID_AUTO, "reset_ssth_rec_rto", CTLFLAG_RW,
1423 	    &rack_ssthresh_rest_rto_rec, 0,
1424 	    "When doing recovery -> rto -> recovery do we reset SSthresh?");
1425 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1426 	    SYSCTL_CHILDREN(rack_timers),
1427 	    OID_AUTO, "scoreboard_thresh", CTLFLAG_RW,
1428 	    &rack_rxt_scoreboard_clear_thresh, 2,
1429 	    "How many RTO's are allowed before we clear the scoreboard");
1430 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1431 	    SYSCTL_CHILDREN(rack_timers),
1432 	    OID_AUTO, "honor_hpts_min", CTLFLAG_RW,
1433 	    &rack_honors_hpts_min_to, 1,
1434 	    "Do rack pacing timers honor hpts min timeout");
1435 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1436 	    SYSCTL_CHILDREN(rack_timers),
1437 	    OID_AUTO, "hpts_max_reduce", CTLFLAG_RW,
1438 	    &rack_max_reduce, 10,
1439 	    "Max percentage we will reduce slot by for pacing when we are behind");
1440 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1441 	    SYSCTL_CHILDREN(rack_timers),
1442 	    OID_AUTO, "persmin", CTLFLAG_RW,
1443 	    &rack_persist_min, 250000,
1444 	    "What is the minimum time in microseconds between persists");
1445 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1446 	    SYSCTL_CHILDREN(rack_timers),
1447 	    OID_AUTO, "persmax", CTLFLAG_RW,
1448 	    &rack_persist_max, 2000000,
1449 	    "What is the largest delay in microseconds between persists");
1450 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1451 	    SYSCTL_CHILDREN(rack_timers),
1452 	    OID_AUTO, "delayed_ack", CTLFLAG_RW,
1453 	    &rack_delayed_ack_time, 40000,
1454 	    "Delayed ack time (40ms in microseconds)");
1455 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1456 	    SYSCTL_CHILDREN(rack_timers),
1457 	    OID_AUTO, "minrto", CTLFLAG_RW,
1458 	    &rack_rto_min, 30000,
1459 	    "Minimum RTO in microseconds -- set with caution below 1000 due to TLP");
1460 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1461 	    SYSCTL_CHILDREN(rack_timers),
1462 	    OID_AUTO, "maxrto", CTLFLAG_RW,
1463 	    &rack_rto_max, 4000000,
1464 	    "Maximum RTO in microseconds -- should be at least as large as min_rto");
1465 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1466 	    SYSCTL_CHILDREN(rack_timers),
1467 	    OID_AUTO, "minto", CTLFLAG_RW,
1468 	    &rack_min_to, 1000,
1469 	    "Minimum rack timeout in microseconds");
1470 	/* Measure controls */
1471 	rack_measure = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1472 	    SYSCTL_CHILDREN(rack_sysctl_root),
1473 	    OID_AUTO,
1474 	    "measure",
1475 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1476 	    "Measure related controls");
1477 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1478 	    SYSCTL_CHILDREN(rack_measure),
1479 	    OID_AUTO, "wma_divisor", CTLFLAG_RW,
1480 	    &rack_wma_divisor, 8,
1481 	    "When doing b/w calculation what is the  divisor for the WMA");
1482 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1483 	    SYSCTL_CHILDREN(rack_measure),
1484 	    OID_AUTO, "end_cwnd", CTLFLAG_RW,
1485 	    &rack_cwnd_block_ends_measure, 0,
1486 	    "Does a cwnd just-return end the measurement window (app limited)");
1487 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1488 	    SYSCTL_CHILDREN(rack_measure),
1489 	    OID_AUTO, "end_rwnd", CTLFLAG_RW,
1490 	    &rack_rwnd_block_ends_measure, 0,
1491 	    "Does an rwnd just-return end the measurement window (app limited -- not persists)");
1492 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1493 	    SYSCTL_CHILDREN(rack_measure),
1494 	    OID_AUTO, "min_target", CTLFLAG_RW,
1495 	    &rack_def_data_window, 20,
1496 	    "What is the minimum target window (in mss) for a GP measurements");
1497 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1498 	    SYSCTL_CHILDREN(rack_measure),
1499 	    OID_AUTO, "goal_bdp", CTLFLAG_RW,
1500 	    &rack_goal_bdp, 2,
1501 	    "What is the goal BDP to measure");
1502 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1503 	    SYSCTL_CHILDREN(rack_measure),
1504 	    OID_AUTO, "min_srtts", CTLFLAG_RW,
1505 	    &rack_min_srtts, 1,
1506 	    "What is the goal BDP to measure");
1507 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1508 	    SYSCTL_CHILDREN(rack_measure),
1509 	    OID_AUTO, "min_measure_tim", CTLFLAG_RW,
1510 	    &rack_min_measure_usec, 0,
1511 	    "What is the Minimum time time for a measurement if 0, this is off");
1512 	/* Features */
1513 	rack_features = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1514 	    SYSCTL_CHILDREN(rack_sysctl_root),
1515 	    OID_AUTO,
1516 	    "features",
1517 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1518 	    "Feature controls");
1519 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1520 	    SYSCTL_CHILDREN(rack_features),
1521 	    OID_AUTO, "hybrid_set_maxseg", CTLFLAG_RW,
1522 	    &rack_hybrid_allow_set_maxseg, 0,
1523 	    "Should hybrid pacing allow the setmss command");
1524 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1525 	    SYSCTL_CHILDREN(rack_features),
1526 	    OID_AUTO, "cmpack", CTLFLAG_RW,
1527 	    &rack_use_cmp_acks, 1,
1528 	    "Should RACK have LRO send compressed acks");
1529 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1530 	    SYSCTL_CHILDREN(rack_features),
1531 	    OID_AUTO, "fsb", CTLFLAG_RW,
1532 	    &rack_use_fsb, 1,
1533 	    "Should RACK use the fast send block?");
1534 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1535 	    SYSCTL_CHILDREN(rack_features),
1536 	    OID_AUTO, "rfo", CTLFLAG_RW,
1537 	    &rack_use_rfo, 1,
1538 	    "Should RACK use rack_fast_output()?");
1539 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1540 	    SYSCTL_CHILDREN(rack_features),
1541 	    OID_AUTO, "rsmrfo", CTLFLAG_RW,
1542 	    &rack_use_rsm_rfo, 1,
1543 	    "Should RACK use rack_fast_rsm_output()?");
1544 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1545 	    SYSCTL_CHILDREN(rack_features),
1546 	    OID_AUTO, "non_paced_lro_queue", CTLFLAG_RW,
1547 	    &rack_enable_mqueue_for_nonpaced, 0,
1548 	    "Should RACK use mbuf queuing for non-paced connections");
1549 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1550 	    SYSCTL_CHILDREN(rack_features),
1551 	    OID_AUTO, "hystartplusplus", CTLFLAG_RW,
1552 	    &rack_do_hystart, 0,
1553 	    "Should RACK enable HyStart++ on connections?");
1554 	/* Policer detection */
1555 	rack_policing = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1556 	    SYSCTL_CHILDREN(rack_sysctl_root),
1557 	    OID_AUTO,
1558 	    "policing",
1559 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1560 	    "policer detection");
1561 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1562 	    SYSCTL_CHILDREN(rack_policing),
1563 	    OID_AUTO, "rxt_thresh", CTLFLAG_RW,
1564 	    &rack_policer_rxt_thresh, 0,
1565 	   "Percentage of retransmits we need to be a possible policer (499 = 49.9 percent)");
1566 	SYSCTL_ADD_U8(&rack_sysctl_ctx,
1567 	    SYSCTL_CHILDREN(rack_policing),
1568 	    OID_AUTO, "avg_thresh", CTLFLAG_RW,
1569 	    &rack_policer_avg_thresh, 0,
1570 	    "What threshold of average retransmits needed to recover a lost packet (1 - 169 aka 21 = 2.1)?");
1571 	SYSCTL_ADD_U8(&rack_sysctl_ctx,
1572 	    SYSCTL_CHILDREN(rack_policing),
1573 	    OID_AUTO, "med_thresh", CTLFLAG_RW,
1574 	    &rack_policer_med_thresh, 0,
1575 	    "What threshold of Median retransmits needed to recover a lost packet (1 - 16)?");
1576 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1577 	    SYSCTL_CHILDREN(rack_policing),
1578 	    OID_AUTO, "data_thresh", CTLFLAG_RW,
1579 	    &rack_policer_data_thresh, 64000,
1580 	    "How many bytes must have gotten through before we can start doing policer detection?");
1581 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1582 	    SYSCTL_CHILDREN(rack_policing),
1583 	    OID_AUTO, "bwcomp", CTLFLAG_RW,
1584 	    &rack_policing_do_bw_comp, 1,
1585 	    "Do we raise up low b/w so that at least pace_max_seg can be sent in the srtt?");
1586 	SYSCTL_ADD_U8(&rack_sysctl_ctx,
1587 	    SYSCTL_CHILDREN(rack_policing),
1588 	    OID_AUTO, "recmss", CTLFLAG_RW,
1589 	    &rack_req_del_mss, 18,
1590 	    "How many MSS must be delivered during recovery to engage policer detection?");
1591 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1592 	    SYSCTL_CHILDREN(rack_policing),
1593 	    OID_AUTO, "res_div", CTLFLAG_RW,
1594 	    &rack_policer_bucket_reserve, 20,
1595 	    "What percentage is reserved in the policer bucket?");
1596 	SYSCTL_ADD_U64(&rack_sysctl_ctx,
1597 	    SYSCTL_CHILDREN(rack_policing),
1598 	    OID_AUTO, "min_comp_bw", CTLFLAG_RW,
1599 	    &rack_pol_min_bw, 125000,
1600 	    "Do we have a min b/w for b/w compensation (0 = no)?");
1601 	/* Misc rack controls */
1602 	rack_misc = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1603 	    SYSCTL_CHILDREN(rack_sysctl_root),
1604 	    OID_AUTO,
1605 	    "misc",
1606 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1607 	    "Misc related controls");
1608 #ifdef TCP_ACCOUNTING
1609 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1610 	    SYSCTL_CHILDREN(rack_misc),
1611 	    OID_AUTO, "tcp_acct", CTLFLAG_RW,
1612 	    &rack_tcp_accounting, 0,
1613 	    "Should we turn on TCP accounting for all rack sessions?");
1614 #endif
1615 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1616 	    SYSCTL_CHILDREN(rack_misc),
1617 	    OID_AUTO, "dnd", CTLFLAG_RW,
1618 	    &rack_dnd_default, 0,
1619 	    "Do not disturb default for rack_rrr = 3");
1620 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1621 	    SYSCTL_CHILDREN(rack_misc),
1622 	    OID_AUTO, "sad_seg_per", CTLFLAG_RW,
1623 	    &sad_seg_size_per, 800,
1624 	    "Percentage of segment size needed in a sack 800 = 80.0?");
1625 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1626 	    SYSCTL_CHILDREN(rack_misc),
1627 	    OID_AUTO, "rxt_controls", CTLFLAG_RW,
1628 	    &rack_rxt_controls, 0,
1629 	    "Retransmit sending size controls (valid  values 0, 1, 2 default=1)?");
1630 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1631 	    SYSCTL_CHILDREN(rack_misc),
1632 	    OID_AUTO, "rack_hibeta", CTLFLAG_RW,
1633 	    &rack_hibeta_setting, 0,
1634 	    "Do we ue a high beta (80 instead of 50)?");
1635 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1636 	    SYSCTL_CHILDREN(rack_misc),
1637 	    OID_AUTO, "apply_rtt_with_low_conf", CTLFLAG_RW,
1638 	    &rack_apply_rtt_with_reduced_conf, 0,
1639 	    "When a persist or keep-alive probe is not answered do we calculate rtt on subsequent answers?");
1640 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1641 	    SYSCTL_CHILDREN(rack_misc),
1642 	    OID_AUTO, "rack_dsack_ctl", CTLFLAG_RW,
1643 	    &rack_dsack_std_based, 3,
1644 	    "How do we process dsack with respect to rack timers, bit field, 3 is standards based?");
1645 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1646 	    SYSCTL_CHILDREN(rack_misc),
1647 	    OID_AUTO, "prr_addback_max", CTLFLAG_RW,
1648 	    &rack_prr_addbackmax, 2,
1649 	    "What is the maximum number of MSS we allow to be added back if prr can't send all its data?");
1650 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1651 	    SYSCTL_CHILDREN(rack_misc),
1652 	    OID_AUTO, "stats_gets_ms", CTLFLAG_RW,
1653 	    &rack_stats_gets_ms_rtt, 1,
1654 	    "What do we feed the stats framework (1 = ms_rtt, 0 = us_rtt, 2 = ms_rtt from hdwr, > 2 usec rtt from hdwr)?");
1655 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1656 	    SYSCTL_CHILDREN(rack_misc),
1657 	    OID_AUTO, "clientlowbuf", CTLFLAG_RW,
1658 	    &rack_client_low_buf, 0,
1659 	    "Client low buffer level (below this we are more aggressive in DGP exiting recovery (0 = off)?");
1660 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1661 	    SYSCTL_CHILDREN(rack_misc),
1662 	    OID_AUTO, "defprofile", CTLFLAG_RW,
1663 	    &rack_def_profile, 0,
1664 	    "Should RACK use a default profile (0=no, num == profile num)?");
1665 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1666 	    SYSCTL_CHILDREN(rack_misc),
1667 	    OID_AUTO, "shared_cwnd", CTLFLAG_RW,
1668 	    &rack_enable_shared_cwnd, 1,
1669 	    "Should RACK try to use the shared cwnd on connections where allowed");
1670 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1671 	    SYSCTL_CHILDREN(rack_misc),
1672 	    OID_AUTO, "limits_on_scwnd", CTLFLAG_RW,
1673 	    &rack_limits_scwnd, 1,
1674 	    "Should RACK place low end time limits on the shared cwnd feature");
1675 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1676 	    SYSCTL_CHILDREN(rack_misc),
1677 	    OID_AUTO, "no_prr", CTLFLAG_RW,
1678 	    &rack_disable_prr, 0,
1679 	    "Should RACK not use prr and only pace (must have pacing on)");
1680 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1681 	    SYSCTL_CHILDREN(rack_misc),
1682 	    OID_AUTO, "bb_verbose", CTLFLAG_RW,
1683 	    &rack_verbose_logging, 0,
1684 	    "Should RACK black box logging be verbose");
1685 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1686 	    SYSCTL_CHILDREN(rack_misc),
1687 	    OID_AUTO, "data_after_close", CTLFLAG_RW,
1688 	    &rack_ignore_data_after_close, 1,
1689 	    "Do we hold off sending a RST until all pending data is ack'd");
1690 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1691 	    SYSCTL_CHILDREN(rack_misc),
1692 	    OID_AUTO, "no_sack_needed", CTLFLAG_RW,
1693 	    &rack_sack_not_required, 1,
1694 	    "Do we allow rack to run on connections not supporting SACK");
1695 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1696 	    SYSCTL_CHILDREN(rack_misc),
1697 	    OID_AUTO, "prr_sendalot", CTLFLAG_RW,
1698 	    &rack_send_a_lot_in_prr, 1,
1699 	    "Send a lot in prr");
1700 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1701 	    SYSCTL_CHILDREN(rack_misc),
1702 	    OID_AUTO, "autoscale", CTLFLAG_RW,
1703 	    &rack_autosndbuf_inc, 20,
1704 	    "What percentage should rack scale up its snd buffer by?");
1705 
1706 
1707 	/* Sack Attacker detection stuff */
1708 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1709 	    SYSCTL_CHILDREN(rack_attack),
1710 	    OID_AUTO, "merge_out", CTLFLAG_RW,
1711 	    &rack_merge_out_sacks_on_attack, 0,
1712 	    "Do we merge the sendmap when we decide we are being attacked?");
1713 
1714 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1715 	    SYSCTL_CHILDREN(rack_attack),
1716 	    OID_AUTO, "detect_highsackratio", CTLFLAG_RW,
1717 	    &rack_highest_sack_thresh_seen, 0,
1718 	    "Highest sack to ack ratio seen");
1719 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1720 	    SYSCTL_CHILDREN(rack_attack),
1721 	    OID_AUTO, "detect_highmoveratio", CTLFLAG_RW,
1722 	    &rack_highest_move_thresh_seen, 0,
1723 	    "Highest move to non-move ratio seen");
1724 	rack_ack_total = counter_u64_alloc(M_WAITOK);
1725 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1726 	    SYSCTL_CHILDREN(rack_attack),
1727 	    OID_AUTO, "acktotal", CTLFLAG_RD,
1728 	    &rack_ack_total,
1729 	    "Total number of Ack's");
1730 	rack_express_sack = counter_u64_alloc(M_WAITOK);
1731 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1732 	    SYSCTL_CHILDREN(rack_attack),
1733 	    OID_AUTO, "exp_sacktotal", CTLFLAG_RD,
1734 	    &rack_express_sack,
1735 	    "Total expresss number of Sack's");
1736 	rack_sack_total = counter_u64_alloc(M_WAITOK);
1737 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1738 	    SYSCTL_CHILDREN(rack_attack),
1739 	    OID_AUTO, "sacktotal", CTLFLAG_RD,
1740 	    &rack_sack_total,
1741 	    "Total number of SACKs");
1742 	rack_move_none = counter_u64_alloc(M_WAITOK);
1743 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1744 	    SYSCTL_CHILDREN(rack_attack),
1745 	    OID_AUTO, "move_none", CTLFLAG_RD,
1746 	    &rack_move_none,
1747 	    "Total number of SACK index reuse of positions under threshold");
1748 	rack_move_some = counter_u64_alloc(M_WAITOK);
1749 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1750 	    SYSCTL_CHILDREN(rack_attack),
1751 	    OID_AUTO, "move_some", CTLFLAG_RD,
1752 	    &rack_move_some,
1753 	    "Total number of SACK index reuse of positions over threshold");
1754 	rack_sack_attacks_detected = counter_u64_alloc(M_WAITOK);
1755 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1756 	    SYSCTL_CHILDREN(rack_attack),
1757 	    OID_AUTO, "attacks", CTLFLAG_RD,
1758 	    &rack_sack_attacks_detected,
1759 	    "Total number of SACK attackers that had sack disabled");
1760 	rack_sack_attacks_reversed = counter_u64_alloc(M_WAITOK);
1761 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1762 	    SYSCTL_CHILDREN(rack_attack),
1763 	    OID_AUTO, "reversed", CTLFLAG_RD,
1764 	    &rack_sack_attacks_reversed,
1765 	    "Total number of SACK attackers that were later determined false positive");
1766 	rack_sack_attacks_suspect = counter_u64_alloc(M_WAITOK);
1767 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1768 	    SYSCTL_CHILDREN(rack_attack),
1769 	    OID_AUTO, "suspect", CTLFLAG_RD,
1770 	    &rack_sack_attacks_suspect,
1771 	    "Total number of SACKs that triggered early detection");
1772 
1773 	rack_sack_used_next_merge = counter_u64_alloc(M_WAITOK);
1774 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1775 	    SYSCTL_CHILDREN(rack_attack),
1776 	    OID_AUTO, "nextmerge", CTLFLAG_RD,
1777 	    &rack_sack_used_next_merge,
1778 	    "Total number of times we used the next merge");
1779 	rack_sack_used_prev_merge = counter_u64_alloc(M_WAITOK);
1780 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1781 	    SYSCTL_CHILDREN(rack_attack),
1782 	    OID_AUTO, "prevmerge", CTLFLAG_RD,
1783 	    &rack_sack_used_prev_merge,
1784 	    "Total number of times we used the prev merge");
1785 	/* Counters */
1786 	rack_total_bytes = counter_u64_alloc(M_WAITOK);
1787 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1788 	    SYSCTL_CHILDREN(rack_counters),
1789 	    OID_AUTO, "totalbytes", CTLFLAG_RD,
1790 	    &rack_total_bytes,
1791 	    "Total number of bytes sent");
1792 	rack_fto_send = counter_u64_alloc(M_WAITOK);
1793 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1794 	    SYSCTL_CHILDREN(rack_counters),
1795 	    OID_AUTO, "fto_send", CTLFLAG_RD,
1796 	    &rack_fto_send, "Total number of rack_fast_output sends");
1797 	rack_fto_rsm_send = counter_u64_alloc(M_WAITOK);
1798 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1799 	    SYSCTL_CHILDREN(rack_counters),
1800 	    OID_AUTO, "fto_rsm_send", CTLFLAG_RD,
1801 	    &rack_fto_rsm_send, "Total number of rack_fast_rsm_output sends");
1802 	rack_nfto_resend = counter_u64_alloc(M_WAITOK);
1803 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1804 	    SYSCTL_CHILDREN(rack_counters),
1805 	    OID_AUTO, "nfto_resend", CTLFLAG_RD,
1806 	    &rack_nfto_resend, "Total number of rack_output retransmissions");
1807 	rack_non_fto_send = counter_u64_alloc(M_WAITOK);
1808 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1809 	    SYSCTL_CHILDREN(rack_counters),
1810 	    OID_AUTO, "nfto_send", CTLFLAG_RD,
1811 	    &rack_non_fto_send, "Total number of rack_output first sends");
1812 	rack_extended_rfo = counter_u64_alloc(M_WAITOK);
1813 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1814 	    SYSCTL_CHILDREN(rack_counters),
1815 	    OID_AUTO, "rfo_extended", CTLFLAG_RD,
1816 	    &rack_extended_rfo, "Total number of times we extended rfo");
1817 
1818 	rack_hw_pace_init_fail = counter_u64_alloc(M_WAITOK);
1819 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1820 	    SYSCTL_CHILDREN(rack_counters),
1821 	    OID_AUTO, "hwpace_init_fail", CTLFLAG_RD,
1822 	    &rack_hw_pace_init_fail, "Total number of times we failed to initialize hw pacing");
1823 	rack_hw_pace_lost = counter_u64_alloc(M_WAITOK);
1824 
1825 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1826 	    SYSCTL_CHILDREN(rack_counters),
1827 	    OID_AUTO, "hwpace_lost", CTLFLAG_RD,
1828 	    &rack_hw_pace_lost, "Total number of times we failed to initialize hw pacing");
1829 	rack_tlp_tot = counter_u64_alloc(M_WAITOK);
1830 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1831 	    SYSCTL_CHILDREN(rack_counters),
1832 	    OID_AUTO, "tlp_to_total", CTLFLAG_RD,
1833 	    &rack_tlp_tot,
1834 	    "Total number of tail loss probe expirations");
1835 	rack_tlp_newdata = counter_u64_alloc(M_WAITOK);
1836 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1837 	    SYSCTL_CHILDREN(rack_counters),
1838 	    OID_AUTO, "tlp_new", CTLFLAG_RD,
1839 	    &rack_tlp_newdata,
1840 	    "Total number of tail loss probe sending new data");
1841 	rack_tlp_retran = counter_u64_alloc(M_WAITOK);
1842 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1843 	    SYSCTL_CHILDREN(rack_counters),
1844 	    OID_AUTO, "tlp_retran", CTLFLAG_RD,
1845 	    &rack_tlp_retran,
1846 	    "Total number of tail loss probe sending retransmitted data");
1847 	rack_tlp_retran_bytes = counter_u64_alloc(M_WAITOK);
1848 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1849 	    SYSCTL_CHILDREN(rack_counters),
1850 	    OID_AUTO, "tlp_retran_bytes", CTLFLAG_RD,
1851 	    &rack_tlp_retran_bytes,
1852 	    "Total bytes of tail loss probe sending retransmitted data");
1853 	rack_to_tot = counter_u64_alloc(M_WAITOK);
1854 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1855 	    SYSCTL_CHILDREN(rack_counters),
1856 	    OID_AUTO, "rack_to_tot", CTLFLAG_RD,
1857 	    &rack_to_tot,
1858 	    "Total number of times the rack to expired");
1859 	rack_saw_enobuf = counter_u64_alloc(M_WAITOK);
1860 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1861 	    SYSCTL_CHILDREN(rack_counters),
1862 	    OID_AUTO, "saw_enobufs", CTLFLAG_RD,
1863 	    &rack_saw_enobuf,
1864 	    "Total number of times a sends returned enobuf for non-hdwr paced connections");
1865 	rack_saw_enobuf_hw = counter_u64_alloc(M_WAITOK);
1866 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1867 	    SYSCTL_CHILDREN(rack_counters),
1868 	    OID_AUTO, "saw_enobufs_hw", CTLFLAG_RD,
1869 	    &rack_saw_enobuf_hw,
1870 	    "Total number of times a send returned enobuf for hdwr paced connections");
1871 	rack_saw_enetunreach = counter_u64_alloc(M_WAITOK);
1872 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1873 	    SYSCTL_CHILDREN(rack_counters),
1874 	    OID_AUTO, "saw_enetunreach", CTLFLAG_RD,
1875 	    &rack_saw_enetunreach,
1876 	    "Total number of times a send received a enetunreachable");
1877 	rack_hot_alloc = counter_u64_alloc(M_WAITOK);
1878 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1879 	    SYSCTL_CHILDREN(rack_counters),
1880 	    OID_AUTO, "alloc_hot", CTLFLAG_RD,
1881 	    &rack_hot_alloc,
1882 	    "Total allocations from the top of our list");
1883 	tcp_policer_detected = counter_u64_alloc(M_WAITOK);
1884 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1885 	    SYSCTL_CHILDREN(rack_counters),
1886 	    OID_AUTO, "policer_detected", CTLFLAG_RD,
1887 	    &tcp_policer_detected,
1888 	    "Total policer_detections");
1889 
1890 	rack_to_alloc = counter_u64_alloc(M_WAITOK);
1891 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1892 	    SYSCTL_CHILDREN(rack_counters),
1893 	    OID_AUTO, "allocs", CTLFLAG_RD,
1894 	    &rack_to_alloc,
1895 	    "Total allocations of tracking structures");
1896 	rack_to_alloc_hard = counter_u64_alloc(M_WAITOK);
1897 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1898 	    SYSCTL_CHILDREN(rack_counters),
1899 	    OID_AUTO, "allochard", CTLFLAG_RD,
1900 	    &rack_to_alloc_hard,
1901 	    "Total allocations done with sleeping the hard way");
1902 	rack_to_alloc_emerg = counter_u64_alloc(M_WAITOK);
1903 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1904 	    SYSCTL_CHILDREN(rack_counters),
1905 	    OID_AUTO, "allocemerg", CTLFLAG_RD,
1906 	    &rack_to_alloc_emerg,
1907 	    "Total allocations done from emergency cache");
1908 	rack_to_alloc_limited = counter_u64_alloc(M_WAITOK);
1909 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1910 	    SYSCTL_CHILDREN(rack_counters),
1911 	    OID_AUTO, "alloc_limited", CTLFLAG_RD,
1912 	    &rack_to_alloc_limited,
1913 	    "Total allocations dropped due to limit");
1914 	rack_alloc_limited_conns = counter_u64_alloc(M_WAITOK);
1915 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1916 	    SYSCTL_CHILDREN(rack_counters),
1917 	    OID_AUTO, "alloc_limited_conns", CTLFLAG_RD,
1918 	    &rack_alloc_limited_conns,
1919 	    "Connections with allocations dropped due to limit");
1920 	rack_split_limited = counter_u64_alloc(M_WAITOK);
1921 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1922 	    SYSCTL_CHILDREN(rack_counters),
1923 	    OID_AUTO, "split_limited", CTLFLAG_RD,
1924 	    &rack_split_limited,
1925 	    "Split allocations dropped due to limit");
1926 	rack_rxt_clamps_cwnd = counter_u64_alloc(M_WAITOK);
1927 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1928 	    SYSCTL_CHILDREN(rack_counters),
1929 	    OID_AUTO, "rxt_clamps_cwnd", CTLFLAG_RD,
1930 	    &rack_rxt_clamps_cwnd,
1931 	    "Number of times that excessive rxt clamped the cwnd down");
1932 	rack_rxt_clamps_cwnd_uniq = counter_u64_alloc(M_WAITOK);
1933 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1934 	    SYSCTL_CHILDREN(rack_counters),
1935 	    OID_AUTO, "rxt_clamps_cwnd_uniq", CTLFLAG_RD,
1936 	    &rack_rxt_clamps_cwnd_uniq,
1937 	    "Number of connections that have had excessive rxt clamped the cwnd down");
1938 	rack_persists_sends = counter_u64_alloc(M_WAITOK);
1939 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1940 	    SYSCTL_CHILDREN(rack_counters),
1941 	    OID_AUTO, "persist_sends", CTLFLAG_RD,
1942 	    &rack_persists_sends,
1943 	    "Number of times we sent a persist probe");
1944 	rack_persists_acks = counter_u64_alloc(M_WAITOK);
1945 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1946 	    SYSCTL_CHILDREN(rack_counters),
1947 	    OID_AUTO, "persist_acks", CTLFLAG_RD,
1948 	    &rack_persists_acks,
1949 	    "Number of times a persist probe was acked");
1950 	rack_persists_loss = counter_u64_alloc(M_WAITOK);
1951 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1952 	    SYSCTL_CHILDREN(rack_counters),
1953 	    OID_AUTO, "persist_loss", CTLFLAG_RD,
1954 	    &rack_persists_loss,
1955 	    "Number of times we detected a lost persist probe (no ack)");
1956 	rack_persists_lost_ends = counter_u64_alloc(M_WAITOK);
1957 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1958 	    SYSCTL_CHILDREN(rack_counters),
1959 	    OID_AUTO, "persist_loss_ends", CTLFLAG_RD,
1960 	    &rack_persists_lost_ends,
1961 	    "Number of lost persist probe (no ack) that the run ended with a PERSIST abort");
1962 #ifdef INVARIANTS
1963 	rack_adjust_map_bw = counter_u64_alloc(M_WAITOK);
1964 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1965 	    SYSCTL_CHILDREN(rack_counters),
1966 	    OID_AUTO, "map_adjust_req", CTLFLAG_RD,
1967 	    &rack_adjust_map_bw,
1968 	    "Number of times we hit the case where the sb went up and down on a sendmap entry");
1969 #endif
1970 	rack_multi_single_eq = counter_u64_alloc(M_WAITOK);
1971 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1972 	    SYSCTL_CHILDREN(rack_counters),
1973 	    OID_AUTO, "cmp_ack_equiv", CTLFLAG_RD,
1974 	    &rack_multi_single_eq,
1975 	    "Number of compressed acks total represented");
1976 	rack_proc_non_comp_ack = counter_u64_alloc(M_WAITOK);
1977 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1978 	    SYSCTL_CHILDREN(rack_counters),
1979 	    OID_AUTO, "cmp_ack_not", CTLFLAG_RD,
1980 	    &rack_proc_non_comp_ack,
1981 	    "Number of non compresseds acks that we processed");
1982 
1983 
1984 	rack_sack_proc_all = counter_u64_alloc(M_WAITOK);
1985 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1986 	    SYSCTL_CHILDREN(rack_counters),
1987 	    OID_AUTO, "sack_long", CTLFLAG_RD,
1988 	    &rack_sack_proc_all,
1989 	    "Total times we had to walk whole list for sack processing");
1990 	rack_sack_proc_restart = counter_u64_alloc(M_WAITOK);
1991 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1992 	    SYSCTL_CHILDREN(rack_counters),
1993 	    OID_AUTO, "sack_restart", CTLFLAG_RD,
1994 	    &rack_sack_proc_restart,
1995 	    "Total times we had to walk whole list due to a restart");
1996 	rack_sack_proc_short = counter_u64_alloc(M_WAITOK);
1997 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1998 	    SYSCTL_CHILDREN(rack_counters),
1999 	    OID_AUTO, "sack_short", CTLFLAG_RD,
2000 	    &rack_sack_proc_short,
2001 	    "Total times we took shortcut for sack processing");
2002 	rack_sack_skipped_acked = counter_u64_alloc(M_WAITOK);
2003 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
2004 	    SYSCTL_CHILDREN(rack_attack),
2005 	    OID_AUTO, "skipacked", CTLFLAG_RD,
2006 	    &rack_sack_skipped_acked,
2007 	    "Total number of times we skipped previously sacked");
2008 	rack_sack_splits = counter_u64_alloc(M_WAITOK);
2009 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
2010 	    SYSCTL_CHILDREN(rack_attack),
2011 	    OID_AUTO, "ofsplit", CTLFLAG_RD,
2012 	    &rack_sack_splits,
2013 	    "Total number of times we did the old fashion tree split");
2014 	rack_input_idle_reduces = counter_u64_alloc(M_WAITOK);
2015 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
2016 	    SYSCTL_CHILDREN(rack_counters),
2017 	    OID_AUTO, "idle_reduce_oninput", CTLFLAG_RD,
2018 	    &rack_input_idle_reduces,
2019 	    "Total number of idle reductions on input");
2020 	rack_collapsed_win_seen = counter_u64_alloc(M_WAITOK);
2021 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
2022 	    SYSCTL_CHILDREN(rack_counters),
2023 	    OID_AUTO, "collapsed_win_seen", CTLFLAG_RD,
2024 	    &rack_collapsed_win_seen,
2025 	    "Total number of collapsed window events seen (where our window shrinks)");
2026 
2027 	rack_collapsed_win = counter_u64_alloc(M_WAITOK);
2028 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
2029 	    SYSCTL_CHILDREN(rack_counters),
2030 	    OID_AUTO, "collapsed_win", CTLFLAG_RD,
2031 	    &rack_collapsed_win,
2032 	    "Total number of collapsed window events where we mark packets");
2033 	rack_collapsed_win_rxt = counter_u64_alloc(M_WAITOK);
2034 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
2035 	    SYSCTL_CHILDREN(rack_counters),
2036 	    OID_AUTO, "collapsed_win_rxt", CTLFLAG_RD,
2037 	    &rack_collapsed_win_rxt,
2038 	    "Total number of packets that were retransmitted");
2039 	rack_collapsed_win_rxt_bytes = counter_u64_alloc(M_WAITOK);
2040 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
2041 	    SYSCTL_CHILDREN(rack_counters),
2042 	    OID_AUTO, "collapsed_win_bytes", CTLFLAG_RD,
2043 	    &rack_collapsed_win_rxt_bytes,
2044 	    "Total number of bytes that were retransmitted");
2045 	rack_try_scwnd = counter_u64_alloc(M_WAITOK);
2046 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
2047 	    SYSCTL_CHILDREN(rack_counters),
2048 	    OID_AUTO, "tried_scwnd", CTLFLAG_RD,
2049 	    &rack_try_scwnd,
2050 	    "Total number of scwnd attempts");
2051 	COUNTER_ARRAY_ALLOC(rack_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK);
2052 	SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
2053 	    OID_AUTO, "outsize", CTLFLAG_RD,
2054 	    rack_out_size, TCP_MSS_ACCT_SIZE, "MSS send sizes");
2055 	COUNTER_ARRAY_ALLOC(rack_opts_arry, RACK_OPTS_SIZE, M_WAITOK);
2056 	SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
2057 	    OID_AUTO, "opts", CTLFLAG_RD,
2058 	    rack_opts_arry, RACK_OPTS_SIZE, "RACK Option Stats");
2059 	SYSCTL_ADD_PROC(&rack_sysctl_ctx,
2060 	    SYSCTL_CHILDREN(rack_sysctl_root),
2061 	    OID_AUTO, "clear", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
2062 	    &rack_clear_counter, 0, sysctl_rack_clear, "IU", "Clear counters");
2063 }
2064 
2065 static uint32_t
2066 rc_init_window(struct tcp_rack *rack)
2067 {
2068 	return (tcp_compute_initwnd(tcp_maxseg(rack->rc_tp)));
2069 
2070 }
2071 
2072 static uint64_t
2073 rack_get_fixed_pacing_bw(struct tcp_rack *rack)
2074 {
2075 	if (IN_FASTRECOVERY(rack->rc_tp->t_flags))
2076 		return (rack->r_ctl.rc_fixed_pacing_rate_rec);
2077 	else if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh)
2078 		return (rack->r_ctl.rc_fixed_pacing_rate_ss);
2079 	else
2080 		return (rack->r_ctl.rc_fixed_pacing_rate_ca);
2081 }
2082 
2083 static void
2084 rack_log_hybrid_bw(struct tcp_rack *rack, uint32_t seq, uint64_t cbw, uint64_t tim,
2085 	uint64_t data, uint8_t mod, uint16_t aux,
2086 	struct tcp_sendfile_track *cur, int line)
2087 {
2088 #ifdef TCP_REQUEST_TRK
2089 	int do_log = 0;
2090 
2091 	/*
2092 	 * The rate cap one is noisy and only should come out when normal BB logging
2093 	 * is enabled, the other logs (not RATE_CAP and NOT CAP_CALC) only come out
2094 	 * once per chunk and make up the BBpoint that can be turned on by the client.
2095 	 */
2096 	if ((mod == HYBRID_LOG_RATE_CAP) || (mod == HYBRID_LOG_CAP_CALC)) {
2097 		/*
2098 		 * The very noisy two need to only come out when
2099 		 * we have verbose logging on.
2100 		 */
2101 		if (rack_verbose_logging != 0)
2102 			do_log = tcp_bblogging_on(rack->rc_tp);
2103 		else
2104 			do_log = 0;
2105 	} else if (mod != HYBRID_LOG_BW_MEASURE) {
2106 		/*
2107 		 * All other less noisy logs here except the measure which
2108 		 * also needs to come out on the point and the log.
2109 		 */
2110 		do_log = tcp_bblogging_on(rack->rc_tp);
2111 	} else {
2112 		do_log = tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING);
2113 	}
2114 
2115 	if (do_log) {
2116 		union tcp_log_stackspecific log;
2117 		struct timeval tv;
2118 		uint64_t lt_bw;
2119 
2120 		/* Convert our ms to a microsecond */
2121 		memset(&log, 0, sizeof(log));
2122 
2123 		log.u_bbr.cwnd_gain = line;
2124 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2125 		log.u_bbr.rttProp = tim;
2126 		log.u_bbr.bw_inuse = cbw;
2127 		log.u_bbr.delRate = rack_get_gp_est(rack);
2128 		lt_bw = rack_get_lt_bw(rack);
2129 		log.u_bbr.flex1 = seq;
2130 		log.u_bbr.pacing_gain = aux;
2131 		/* lt_bw = < flex3 | flex2 > */
2132 		log.u_bbr.flex2 = (uint32_t)(lt_bw & 0x00000000ffffffff);
2133 		log.u_bbr.flex3 = (uint32_t)((lt_bw >> 32) & 0x00000000ffffffff);
2134 		/* Record the last obtained us rtt in inflight */
2135 		if (cur == NULL) {
2136 			/* Make sure we are looking at the right log if an overide comes in */
2137 			cur = rack->r_ctl.rc_last_sft;
2138 		}
2139 		if (rack->r_ctl.rack_rs.rs_flags != RACK_RTT_EMPTY)
2140 			log.u_bbr.inflight = rack->r_ctl.rack_rs.rs_us_rtt;
2141 		else {
2142 			/* Use the last known rtt i.e. the rack-rtt */
2143 			log.u_bbr.inflight = rack->rc_rack_rtt;
2144 		}
2145 		if (cur != NULL) {
2146 			uint64_t off;
2147 
2148 			log.u_bbr.cur_del_rate = cur->deadline;
2149 			if ((mod == HYBRID_LOG_RATE_CAP) || (mod == HYBRID_LOG_CAP_CALC)) {
2150 				/* start = < lost | pkt_epoch > */
2151 				log.u_bbr.pkt_epoch = (uint32_t)(cur->start & 0x00000000ffffffff);
2152 				log.u_bbr.lost = (uint32_t)((cur->start >> 32) & 0x00000000ffffffff);
2153 				log.u_bbr.flex6 = cur->start_seq;
2154 				log.u_bbr.pkts_out = cur->end_seq;
2155 			} else {
2156 				/* start = < lost | pkt_epoch > */
2157 				log.u_bbr.pkt_epoch = (uint32_t)(cur->start & 0x00000000ffffffff);
2158 				log.u_bbr.lost = (uint32_t)((cur->start >> 32) & 0x00000000ffffffff);
2159 				/* end = < pkts_out | flex6 > */
2160 				log.u_bbr.flex6 = (uint32_t)(cur->end & 0x00000000ffffffff);
2161 				log.u_bbr.pkts_out = (uint32_t)((cur->end >> 32) & 0x00000000ffffffff);
2162 			}
2163 			/* first_send = <lt_epoch | epoch> */
2164 			log.u_bbr.epoch = (uint32_t)(cur->first_send & 0x00000000ffffffff);
2165 			log.u_bbr.lt_epoch = (uint32_t)((cur->first_send >> 32) & 0x00000000ffffffff);
2166 			/* localtime = <delivered | applimited>*/
2167 			log.u_bbr.applimited = (uint32_t)(cur->localtime & 0x00000000ffffffff);
2168 			log.u_bbr.delivered = (uint32_t)((cur->localtime >> 32) & 0x00000000ffffffff);
2169 #ifdef TCP_REQUEST_TRK
2170 			off = (uint64_t)(cur) - (uint64_t)(&rack->rc_tp->t_tcpreq_info[0]);
2171 			log.u_bbr.bbr_substate = (uint8_t)(off / sizeof(struct tcp_sendfile_track));
2172 #endif
2173 			log.u_bbr.inhpts = 1;
2174 			log.u_bbr.flex4 = (uint32_t)(rack->rc_tp->t_sndbytes - cur->sent_at_fs);
2175 			log.u_bbr.flex5 = (uint32_t)(rack->rc_tp->t_snd_rxt_bytes - cur->rxt_at_fs);
2176 			log.u_bbr.flex7 = (uint16_t)cur->hybrid_flags;
2177 		} else {
2178 			log.u_bbr.flex7 = 0xffff;
2179 			log.u_bbr.cur_del_rate = 0xffffffffffffffff;
2180 		}
2181 		/*
2182 		 * Compose bbr_state to be a bit wise 0000ADHF
2183 		 * where A is the always_pace flag
2184 		 * where D is the dgp_on flag
2185 		 * where H is the hybrid_mode on flag
2186 		 * where F is the use_fixed_rate flag.
2187 		 */
2188 		log.u_bbr.bbr_state = rack->rc_always_pace;
2189 		log.u_bbr.bbr_state <<= 1;
2190 		log.u_bbr.bbr_state |= rack->dgp_on;
2191 		log.u_bbr.bbr_state <<= 1;
2192 		log.u_bbr.bbr_state |= rack->rc_hybrid_mode;
2193 		log.u_bbr.bbr_state <<= 1;
2194 		log.u_bbr.bbr_state |= rack->use_fixed_rate;
2195 		log.u_bbr.flex8 = mod;
2196 		tcp_log_event(rack->rc_tp, NULL,
2197 		    &rack->rc_inp->inp_socket->so_rcv,
2198 		    &rack->rc_inp->inp_socket->so_snd,
2199 		    TCP_HYBRID_PACING_LOG, 0,
2200 		    0, &log, false, NULL, __func__, __LINE__, &tv);
2201 
2202 	}
2203 #endif
2204 }
2205 
2206 #ifdef TCP_REQUEST_TRK
2207 static void
2208 rack_log_hybrid_sends(struct tcp_rack *rack, struct tcp_sendfile_track *cur, int line)
2209 {
2210 	if (tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING)) {
2211 		union tcp_log_stackspecific log;
2212 		struct timeval tv;
2213 		uint64_t off;
2214 
2215 		/* Convert our ms to a microsecond */
2216 		memset(&log, 0, sizeof(log));
2217 
2218 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2219 		log.u_bbr.delRate = cur->sent_at_fs;
2220 
2221 		if ((cur->flags & TCP_TRK_TRACK_FLG_LSND) == 0) {
2222 			/*
2223 			 * We did not get a new Rules Applied to set so
2224 			 * no overlapping send occured, this means the
2225 			 * current byte counts are correct.
2226 			 */
2227 			log.u_bbr.cur_del_rate = rack->rc_tp->t_sndbytes;
2228 			log.u_bbr.rttProp = rack->rc_tp->t_snd_rxt_bytes;
2229 		} else {
2230 			/*
2231 			 * Overlapping send case, we switched to a new
2232 			 * send and did a rules applied.
2233 			 */
2234 			log.u_bbr.cur_del_rate = cur->sent_at_ls;
2235 			log.u_bbr.rttProp = cur->rxt_at_ls;
2236 		}
2237 		log.u_bbr.bw_inuse = cur->rxt_at_fs;
2238 		log.u_bbr.cwnd_gain = line;
2239 		off = (uint64_t)(cur) - (uint64_t)(&rack->rc_tp->t_tcpreq_info[0]);
2240 		log.u_bbr.bbr_substate = (uint8_t)(off / sizeof(struct tcp_sendfile_track));
2241 		/* start = < flex1 | flex2 > */
2242 		log.u_bbr.flex2 = (uint32_t)(cur->start & 0x00000000ffffffff);
2243 		log.u_bbr.flex1 = (uint32_t)((cur->start >> 32) & 0x00000000ffffffff);
2244 		/* end = < flex3 | flex4 > */
2245 		log.u_bbr.flex4 = (uint32_t)(cur->end & 0x00000000ffffffff);
2246 		log.u_bbr.flex3 = (uint32_t)((cur->end >> 32) & 0x00000000ffffffff);
2247 
2248 		/* localtime = <delivered | applimited>*/
2249 		log.u_bbr.applimited = (uint32_t)(cur->localtime & 0x00000000ffffffff);
2250 		log.u_bbr.delivered = (uint32_t)((cur->localtime >> 32) & 0x00000000ffffffff);
2251 		/* client timestamp = <lt_epoch | epoch>*/
2252 		log.u_bbr.epoch = (uint32_t)(cur->timestamp & 0x00000000ffffffff);
2253 		log.u_bbr.lt_epoch = (uint32_t)((cur->timestamp >> 32) & 0x00000000ffffffff);
2254 		/* now set all the flags in */
2255 		log.u_bbr.pkts_out = cur->hybrid_flags;
2256 		log.u_bbr.lost = cur->playout_ms;
2257 		log.u_bbr.flex6 = cur->flags;
2258 		/*
2259 		 * Last send time  = <flex5 | pkt_epoch>  note we do not distinguish cases
2260 		 * where a false retransmit occurred so first_send  <-> lastsend may
2261 		 * include longer time then it actually took if we have a false rxt.
2262 		 */
2263 		log.u_bbr.pkt_epoch = (uint32_t)(rack->r_ctl.last_tmit_time_acked & 0x00000000ffffffff);
2264 		log.u_bbr.flex5 = (uint32_t)((rack->r_ctl.last_tmit_time_acked >> 32) & 0x00000000ffffffff);
2265 		/*
2266 		 * Compose bbr_state to be a bit wise 0000ADHF
2267 		 * where A is the always_pace flag
2268 		 * where D is the dgp_on flag
2269 		 * where H is the hybrid_mode on flag
2270 		 * where F is the use_fixed_rate flag.
2271 		 */
2272 		log.u_bbr.bbr_state = rack->rc_always_pace;
2273 		log.u_bbr.bbr_state <<= 1;
2274 		log.u_bbr.bbr_state |= rack->dgp_on;
2275 		log.u_bbr.bbr_state <<= 1;
2276 		log.u_bbr.bbr_state |= rack->rc_hybrid_mode;
2277 		log.u_bbr.bbr_state <<= 1;
2278 		log.u_bbr.bbr_state |= rack->use_fixed_rate;
2279 
2280 		log.u_bbr.flex8 = HYBRID_LOG_SENT_LOST;
2281 		tcp_log_event(rack->rc_tp, NULL,
2282 		    &rack->rc_inp->inp_socket->so_rcv,
2283 		    &rack->rc_inp->inp_socket->so_snd,
2284 		    TCP_HYBRID_PACING_LOG, 0,
2285 		    0, &log, false, NULL, __func__, __LINE__, &tv);
2286 	}
2287 }
2288 #endif
2289 
2290 static inline uint64_t
2291 rack_compensate_for_linerate(struct tcp_rack *rack, uint64_t bw)
2292 {
2293 	uint64_t ret_bw, ether;
2294 	uint64_t u_segsiz;
2295 
2296 	ether = rack->rc_tp->t_maxseg + sizeof(struct tcphdr);
2297 	if (rack->r_is_v6){
2298 #ifdef INET6
2299 		ether += sizeof(struct ip6_hdr);
2300 #endif
2301 		ether += 14;	/* eheader size 6+6+2 */
2302 	} else {
2303 #ifdef INET
2304 		ether += sizeof(struct ip);
2305 #endif
2306 		ether += 14;	/* eheader size 6+6+2 */
2307 	}
2308 	u_segsiz = (uint64_t)min(ctf_fixed_maxseg(rack->rc_tp), rack->r_ctl.rc_pace_min_segs);
2309 	ret_bw = bw;
2310 	ret_bw *= ether;
2311 	ret_bw /= u_segsiz;
2312 	return (ret_bw);
2313 }
2314 
2315 static void
2316 rack_rate_cap_bw(struct tcp_rack *rack, uint64_t *bw, int *capped)
2317 {
2318 #ifdef TCP_REQUEST_TRK
2319 	struct timeval tv;
2320 	uint64_t timenow, timeleft, lenleft, lengone, calcbw;
2321 #endif
2322 
2323 	if (rack->r_ctl.bw_rate_cap == 0)
2324 		return;
2325 #ifdef TCP_REQUEST_TRK
2326 	if (rack->rc_catch_up && rack->rc_hybrid_mode &&
2327 	    (rack->r_ctl.rc_last_sft != NULL)) {
2328 		/*
2329 		 * We have a dynamic cap. The original target
2330 		 * is in bw_rate_cap, but we need to look at
2331 		 * how long it is until we hit the deadline.
2332 		 */
2333 		struct tcp_sendfile_track *ent;
2334 
2335       		ent = rack->r_ctl.rc_last_sft;
2336 		microuptime(&tv);
2337 		timenow = tcp_tv_to_lusectick(&tv);
2338 		if (timenow >= ent->deadline) {
2339 			/* No time left we do DGP only */
2340 			rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2341 					   0, 0, 0, HYBRID_LOG_OUTOFTIME, 0, ent, __LINE__);
2342 			rack->r_ctl.bw_rate_cap = 0;
2343 			return;
2344 		}
2345 		/* We have the time */
2346 		timeleft = rack->r_ctl.rc_last_sft->deadline - timenow;
2347 		if (timeleft < HPTS_MSEC_IN_SEC) {
2348 			/* If there is less than a ms left just use DGPs rate */
2349 			rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2350 					   0, timeleft, 0, HYBRID_LOG_OUTOFTIME, 0, ent, __LINE__);
2351 			rack->r_ctl.bw_rate_cap = 0;
2352 			return;
2353 		}
2354 		/*
2355 		 * Now lets find the amount of data left to send.
2356 		 *
2357 		 * Now ideally we want to use the end_seq to figure out how much more
2358 		 * but it might not be possible (only if we have the TRACK_FG_COMP on the entry..
2359 		 */
2360 		if (ent->flags & TCP_TRK_TRACK_FLG_COMP) {
2361 			if (SEQ_GT(ent->end_seq, rack->rc_tp->snd_una))
2362 				lenleft = ent->end_seq - rack->rc_tp->snd_una;
2363 			else {
2364 				/* TSNH, we should catch it at the send */
2365 				rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2366 						   0, timeleft, 0, HYBRID_LOG_CAPERROR, 0, ent, __LINE__);
2367 				rack->r_ctl.bw_rate_cap = 0;
2368 				return;
2369 			}
2370 		} else {
2371 			/*
2372 			 * The hard way, figure out how much is gone and then
2373 			 * take that away from the total the client asked for
2374 			 * (thats off by tls overhead if this is tls).
2375 			 */
2376 			if (SEQ_GT(rack->rc_tp->snd_una, ent->start_seq))
2377 				lengone = rack->rc_tp->snd_una - ent->start_seq;
2378 			else
2379 				lengone = 0;
2380 			if (lengone < (ent->end - ent->start))
2381 				lenleft = (ent->end - ent->start) - lengone;
2382 			else {
2383 				/* TSNH, we should catch it at the send */
2384 				rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2385 						   0, timeleft, lengone, HYBRID_LOG_CAPERROR, 0, ent, __LINE__);
2386 				rack->r_ctl.bw_rate_cap = 0;
2387 				return;
2388 			}
2389 		}
2390 		if (lenleft == 0) {
2391 			/* We have it all sent */
2392 			rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2393 					   0, timeleft, lenleft, HYBRID_LOG_ALLSENT, 0, ent, __LINE__);
2394 			if (rack->r_ctl.bw_rate_cap)
2395 				goto normal_ratecap;
2396 			else
2397 				return;
2398 		}
2399 		calcbw = lenleft * HPTS_USEC_IN_SEC;
2400 		calcbw /= timeleft;
2401 		/* Now we must compensate for IP/TCP overhead */
2402 		calcbw = rack_compensate_for_linerate(rack, calcbw);
2403 		/* Update the bit rate cap */
2404 		rack->r_ctl.bw_rate_cap = calcbw;
2405 		if ((rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_S_MSS) &&
2406 		    (rack_hybrid_allow_set_maxseg == 1) &&
2407 		    ((rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_SETMSS) == 0)) {
2408 			/* Lets set in a smaller mss possibly here to match our rate-cap */
2409 			uint32_t orig_max;
2410 
2411 			orig_max = rack->r_ctl.rc_pace_max_segs;
2412 			rack->r_ctl.rc_last_sft->hybrid_flags |= TCP_HYBRID_PACING_SETMSS;
2413 			rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack, calcbw, ctf_fixed_maxseg(rack->rc_tp));
2414 			rack_log_type_pacing_sizes(rack->rc_tp, rack, rack->r_ctl.client_suggested_maxseg, orig_max, __LINE__, 5);
2415 		}
2416 		rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2417 				   calcbw, timeleft, lenleft, HYBRID_LOG_CAP_CALC, 0, ent, __LINE__);
2418 		if ((calcbw > 0) && (*bw > calcbw)) {
2419 			rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2420 					   *bw, ent->deadline, lenleft, HYBRID_LOG_RATE_CAP, 0, ent, __LINE__);
2421 			*capped = 1;
2422 			*bw = calcbw;
2423 		}
2424 		return;
2425 	}
2426 normal_ratecap:
2427 #endif
2428 	if ((rack->r_ctl.bw_rate_cap > 0) && (*bw > rack->r_ctl.bw_rate_cap)) {
2429 #ifdef TCP_REQUEST_TRK
2430 		if (rack->rc_hybrid_mode &&
2431 		    rack->rc_catch_up &&
2432 		    (rack->r_ctl.rc_last_sft != NULL) &&
2433 		    (rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_S_MSS) &&
2434 		    (rack_hybrid_allow_set_maxseg == 1) &&
2435 		    ((rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_SETMSS) == 0)) {
2436 			/* Lets set in a smaller mss possibly here to match our rate-cap */
2437 			uint32_t orig_max;
2438 
2439 			orig_max = rack->r_ctl.rc_pace_max_segs;
2440 			rack->r_ctl.rc_last_sft->hybrid_flags |= TCP_HYBRID_PACING_SETMSS;
2441 			rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack, rack->r_ctl.bw_rate_cap, ctf_fixed_maxseg(rack->rc_tp));
2442 			rack_log_type_pacing_sizes(rack->rc_tp, rack, rack->r_ctl.client_suggested_maxseg, orig_max, __LINE__, 5);
2443 		}
2444 #endif
2445 		*capped = 1;
2446 		*bw = rack->r_ctl.bw_rate_cap;
2447 		rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2448 				   *bw, 0, 0,
2449 				   HYBRID_LOG_RATE_CAP, 1, NULL, __LINE__);
2450 	}
2451 }
2452 
2453 static uint64_t
2454 rack_get_gp_est(struct tcp_rack *rack)
2455 {
2456 	uint64_t bw, lt_bw, ret_bw;
2457 
2458 	if (rack->rc_gp_filled == 0) {
2459 		/*
2460 		 * We have yet no b/w measurement,
2461 		 * if we have a user set initial bw
2462 		 * return it. If we don't have that and
2463 		 * we have an srtt, use the tcp IW (10) to
2464 		 * calculate a fictional b/w over the SRTT
2465 		 * which is more or less a guess. Note
2466 		 * we don't use our IW from rack on purpose
2467 		 * so if we have like IW=30, we are not
2468 		 * calculating a "huge" b/w.
2469 		 */
2470 		uint64_t srtt;
2471 
2472 		if (rack->dis_lt_bw == 1)
2473 			lt_bw = 0;
2474 		else
2475 			lt_bw = rack_get_lt_bw(rack);
2476 		if (lt_bw) {
2477 			/*
2478 			 * No goodput bw but a long-term b/w does exist
2479 			 * lets use that.
2480 			 */
2481 			ret_bw = lt_bw;
2482 			goto compensate;
2483 		}
2484 		if (rack->r_ctl.init_rate)
2485 			return (rack->r_ctl.init_rate);
2486 
2487 		/* Ok lets come up with the IW guess, if we have a srtt */
2488 		if (rack->rc_tp->t_srtt == 0) {
2489 			/*
2490 			 * Go with old pacing method
2491 			 * i.e. burst mitigation only.
2492 			 */
2493 			return (0);
2494 		}
2495 		/* Ok lets get the initial TCP win (not racks) */
2496 		bw = tcp_compute_initwnd(tcp_maxseg(rack->rc_tp));
2497 		srtt = (uint64_t)rack->rc_tp->t_srtt;
2498 		bw *= (uint64_t)USECS_IN_SECOND;
2499 		bw /= srtt;
2500 		ret_bw = bw;
2501 		goto compensate;
2502 
2503 	}
2504 	if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) {
2505 		/* Averaging is done, we can return the value */
2506 		bw = rack->r_ctl.gp_bw;
2507 	} else {
2508 		/* Still doing initial average must calculate */
2509 		bw = rack->r_ctl.gp_bw / max(rack->r_ctl.num_measurements, 1);
2510 	}
2511 	if (rack->dis_lt_bw) {
2512 		/* We are not using lt-bw */
2513 		ret_bw = bw;
2514 		goto compensate;
2515 	}
2516 	lt_bw = rack_get_lt_bw(rack);
2517 	if (lt_bw == 0) {
2518 		/* If we don't have one then equate it to the gp_bw */
2519 		lt_bw = rack->r_ctl.gp_bw;
2520 	}
2521 	if (rack->use_lesser_lt_bw) {
2522 		if (lt_bw < bw)
2523 			ret_bw = lt_bw;
2524 		else
2525 			ret_bw = bw;
2526 	} else {
2527 		if (lt_bw > bw)
2528 			ret_bw = lt_bw;
2529 		else
2530 			ret_bw = bw;
2531 	}
2532 	/*
2533 	 * Now lets compensate based on the TCP/IP overhead. Our
2534 	 * Goodput estimate does not include this so we must pace out
2535 	 * a bit faster since our pacing calculations do. The pacing
2536 	 * calculations use the base ETHERNET_SEGMENT_SIZE and the segsiz
2537 	 * we are using to do this, so we do that here in the opposite
2538 	 * direction as well. This means that if we are tunneled and the
2539 	 * segsiz is say 1200 bytes we will get quite a boost, but its
2540 	 * compensated for in the pacing time the opposite way.
2541 	 */
2542 compensate:
2543 	ret_bw = rack_compensate_for_linerate(rack, ret_bw);
2544 	return(ret_bw);
2545 }
2546 
2547 
2548 static uint64_t
2549 rack_get_bw(struct tcp_rack *rack)
2550 {
2551 	uint64_t bw;
2552 
2553 	if (rack->use_fixed_rate) {
2554 		/* Return the fixed pacing rate */
2555 		return (rack_get_fixed_pacing_bw(rack));
2556 	}
2557 	bw = rack_get_gp_est(rack);
2558 	return (bw);
2559 }
2560 
2561 static uint16_t
2562 rack_get_output_gain(struct tcp_rack *rack, struct rack_sendmap *rsm)
2563 {
2564 	if (rack->use_fixed_rate) {
2565 		return (100);
2566 	} else if (rack->in_probe_rtt && (rsm == NULL))
2567 		return (rack->r_ctl.rack_per_of_gp_probertt);
2568 	else if ((IN_FASTRECOVERY(rack->rc_tp->t_flags) &&
2569 		  rack->r_ctl.rack_per_of_gp_rec)) {
2570 		if (rsm) {
2571 			/* a retransmission always use the recovery rate */
2572 			return (rack->r_ctl.rack_per_of_gp_rec);
2573 		} else if (rack->rack_rec_nonrxt_use_cr) {
2574 			/* Directed to use the configured rate */
2575 			goto configured_rate;
2576 		} else if (rack->rack_no_prr &&
2577 			   (rack->r_ctl.rack_per_of_gp_rec > 100)) {
2578 			/* No PRR, lets just use the b/w estimate only */
2579 			return (100);
2580 		} else {
2581 			/*
2582 			 * Here we may have a non-retransmit but we
2583 			 * have no overrides, so just use the recovery
2584 			 * rate (prr is in effect).
2585 			 */
2586 			return (rack->r_ctl.rack_per_of_gp_rec);
2587 		}
2588 	}
2589 configured_rate:
2590 	/* For the configured rate we look at our cwnd vs the ssthresh */
2591 	if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh)
2592 		return (rack->r_ctl.rack_per_of_gp_ss);
2593 	else
2594 		return (rack->r_ctl.rack_per_of_gp_ca);
2595 }
2596 
2597 static void
2598 rack_log_dsack_event(struct tcp_rack *rack, uint8_t mod, uint32_t flex4, uint32_t flex5, uint32_t flex6)
2599 {
2600 	/*
2601 	 * Types of logs (mod value)
2602 	 * 1 = dsack_persists reduced by 1 via T-O or fast recovery exit.
2603 	 * 2 = a dsack round begins, persist is reset to 16.
2604 	 * 3 = a dsack round ends
2605 	 * 4 = Dsack option increases rack rtt flex5 is the srtt input, flex6 is thresh
2606 	 * 5 = Socket option set changing the control flags rc_rack_tmr_std_based, rc_rack_use_dsack
2607 	 * 6 = Final rack rtt, flex4 is srtt and flex6 is final limited thresh.
2608 	 */
2609 	if (tcp_bblogging_on(rack->rc_tp)) {
2610 		union tcp_log_stackspecific log;
2611 		struct timeval tv;
2612 
2613 		memset(&log, 0, sizeof(log));
2614 		log.u_bbr.flex1 = rack->rc_rack_tmr_std_based;
2615 		log.u_bbr.flex1 <<= 1;
2616 		log.u_bbr.flex1 |= rack->rc_rack_use_dsack;
2617 		log.u_bbr.flex1 <<= 1;
2618 		log.u_bbr.flex1 |= rack->rc_dsack_round_seen;
2619 		log.u_bbr.flex2 = rack->r_ctl.dsack_round_end;
2620 		log.u_bbr.flex3 = rack->r_ctl.num_dsack;
2621 		log.u_bbr.flex4 = flex4;
2622 		log.u_bbr.flex5 = flex5;
2623 		log.u_bbr.flex6 = flex6;
2624 		log.u_bbr.flex7 = rack->r_ctl.dsack_persist;
2625 		log.u_bbr.flex8 = mod;
2626 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2627 		log.u_bbr.epoch = rack->r_ctl.current_round;
2628 		log.u_bbr.lt_epoch = rack->r_ctl.rc_considered_lost;
2629 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2630 		    &rack->rc_inp->inp_socket->so_rcv,
2631 		    &rack->rc_inp->inp_socket->so_snd,
2632 		    RACK_DSACK_HANDLING, 0,
2633 		    0, &log, false, &tv);
2634 	}
2635 }
2636 
2637 static void
2638 rack_log_hdwr_pacing(struct tcp_rack *rack,
2639 		     uint64_t rate, uint64_t hw_rate, int line,
2640 		     int error, uint16_t mod)
2641 {
2642 	if (tcp_bblogging_on(rack->rc_tp)) {
2643 		union tcp_log_stackspecific log;
2644 		struct timeval tv;
2645 		const struct ifnet *ifp;
2646 
2647 		memset(&log, 0, sizeof(log));
2648 		log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff);
2649 		log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff);
2650 		if (rack->r_ctl.crte) {
2651 			ifp = rack->r_ctl.crte->ptbl->rs_ifp;
2652 		} else if (rack->rc_inp->inp_route.ro_nh &&
2653 			   rack->rc_inp->inp_route.ro_nh->nh_ifp) {
2654 			ifp = rack->rc_inp->inp_route.ro_nh->nh_ifp;
2655 		} else
2656 			ifp = NULL;
2657 		if (ifp) {
2658 			log.u_bbr.flex3 = (((uint64_t)ifp  >> 32) & 0x00000000ffffffff);
2659 			log.u_bbr.flex4 = ((uint64_t)ifp & 0x00000000ffffffff);
2660 		}
2661 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2662 		log.u_bbr.bw_inuse = rate;
2663 		log.u_bbr.flex5 = line;
2664 		log.u_bbr.flex6 = error;
2665 		log.u_bbr.flex7 = mod;
2666 		log.u_bbr.applimited = rack->r_ctl.rc_pace_max_segs;
2667 		log.u_bbr.flex8 = rack->use_fixed_rate;
2668 		log.u_bbr.flex8 <<= 1;
2669 		log.u_bbr.flex8 |= rack->rack_hdrw_pacing;
2670 		log.u_bbr.pkts_out = rack->rc_tp->t_maxseg;
2671 		log.u_bbr.delRate = rack->r_ctl.crte_prev_rate;
2672 		if (rack->r_ctl.crte)
2673 			log.u_bbr.cur_del_rate = rack->r_ctl.crte->rate;
2674 		else
2675 			log.u_bbr.cur_del_rate = 0;
2676 		log.u_bbr.rttProp = rack->r_ctl.last_hw_bw_req;
2677 		log.u_bbr.epoch = rack->r_ctl.current_round;
2678 		log.u_bbr.lt_epoch = rack->r_ctl.rc_considered_lost;
2679 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2680 		    &rack->rc_inp->inp_socket->so_rcv,
2681 		    &rack->rc_inp->inp_socket->so_snd,
2682 		    BBR_LOG_HDWR_PACE, 0,
2683 		    0, &log, false, &tv);
2684 	}
2685 }
2686 
2687 static uint64_t
2688 rack_get_output_bw(struct tcp_rack *rack, uint64_t bw, struct rack_sendmap *rsm, int *capped)
2689 {
2690 	/*
2691 	 * We allow rack_per_of_gp_xx to dictate our bw rate we want.
2692 	 */
2693 	uint64_t bw_est, high_rate;
2694 	uint64_t gain;
2695 
2696 	gain = (uint64_t)rack_get_output_gain(rack, rsm);
2697 	bw_est = bw * gain;
2698 	bw_est /= (uint64_t)100;
2699 	/* Never fall below the minimum (def 64kbps) */
2700 	if (bw_est < RACK_MIN_BW)
2701 		bw_est = RACK_MIN_BW;
2702 	if (rack->r_rack_hw_rate_caps) {
2703 		/* Rate caps are in place */
2704 		if (rack->r_ctl.crte != NULL) {
2705 			/* We have a hdwr rate already */
2706 			high_rate = tcp_hw_highest_rate(rack->r_ctl.crte);
2707 			if (bw_est >= high_rate) {
2708 				/* We are capping bw at the highest rate table entry */
2709 				if (rack_hw_rate_cap_per &&
2710 				    (((high_rate * (100 + rack_hw_rate_cap_per)) / 100) < bw_est)) {
2711 					rack->r_rack_hw_rate_caps = 0;
2712 					goto done;
2713 				}
2714 				rack_log_hdwr_pacing(rack,
2715 						     bw_est, high_rate, __LINE__,
2716 						     0, 3);
2717 				bw_est = high_rate;
2718 				if (capped)
2719 					*capped = 1;
2720 			}
2721 		} else if ((rack->rack_hdrw_pacing == 0) &&
2722 			   (rack->rack_hdw_pace_ena) &&
2723 			   (rack->rack_attempt_hdwr_pace == 0) &&
2724 			   (rack->rc_inp->inp_route.ro_nh != NULL) &&
2725 			   (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
2726 			/*
2727 			 * Special case, we have not yet attempted hardware
2728 			 * pacing, and yet we may, when we do, find out if we are
2729 			 * above the highest rate. We need to know the maxbw for the interface
2730 			 * in question (if it supports ratelimiting). We get back
2731 			 * a 0, if the interface is not found in the RL lists.
2732 			 */
2733 			high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp);
2734 			if (high_rate) {
2735 				/* Yep, we have a rate is it above this rate? */
2736 				if (bw_est > high_rate) {
2737 					bw_est = high_rate;
2738 					if (capped)
2739 						*capped = 1;
2740 				}
2741 			}
2742 		}
2743 	}
2744 done:
2745 	return (bw_est);
2746 }
2747 
2748 static void
2749 rack_log_retran_reason(struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t tsused, uint32_t thresh, int mod)
2750 {
2751 	if (tcp_bblogging_on(rack->rc_tp)) {
2752 		union tcp_log_stackspecific log;
2753 		struct timeval tv;
2754 
2755 		if (rack->sack_attack_disable > 0)
2756 			goto log_anyway;
2757 		if ((mod != 1) && (rack_verbose_logging == 0))  {
2758 			/*
2759 			 * We get 3 values currently for mod
2760 			 * 1 - We are retransmitting and this tells the reason.
2761 			 * 2 - We are clearing a dup-ack count.
2762 			 * 3 - We are incrementing a dup-ack count.
2763 			 *
2764 			 * The clear/increment are only logged
2765 			 * if you have BBverbose on.
2766 			 */
2767 			return;
2768 		}
2769 log_anyway:
2770 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2771 		log.u_bbr.flex1 = tsused;
2772 		log.u_bbr.flex2 = thresh;
2773 		log.u_bbr.flex3 = rsm->r_flags;
2774 		log.u_bbr.flex4 = rsm->r_dupack;
2775 		log.u_bbr.flex5 = rsm->r_start;
2776 		log.u_bbr.flex6 = rsm->r_end;
2777 		log.u_bbr.flex8 = mod;
2778 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2779 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2780 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2781 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2782 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2783 		log.u_bbr.pacing_gain = rack->r_must_retran;
2784 		log.u_bbr.epoch = rack->r_ctl.current_round;
2785 		log.u_bbr.lt_epoch = rack->r_ctl.rc_considered_lost;
2786 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2787 		    &rack->rc_inp->inp_socket->so_rcv,
2788 		    &rack->rc_inp->inp_socket->so_snd,
2789 		    BBR_LOG_SETTINGS_CHG, 0,
2790 		    0, &log, false, &tv);
2791 	}
2792 }
2793 
2794 static void
2795 rack_log_to_start(struct tcp_rack *rack, uint32_t cts, uint32_t to, int32_t slot, uint8_t which)
2796 {
2797 	if (tcp_bblogging_on(rack->rc_tp)) {
2798 		union tcp_log_stackspecific log;
2799 		struct timeval tv;
2800 
2801 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2802 		log.u_bbr.flex1 = rack->rc_tp->t_srtt;
2803 		log.u_bbr.flex2 = to;
2804 		log.u_bbr.flex3 = rack->r_ctl.rc_hpts_flags;
2805 		log.u_bbr.flex4 = slot;
2806 		log.u_bbr.flex5 = rack->rc_tp->t_hpts_slot;
2807 		log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
2808 		log.u_bbr.flex7 = rack->rc_in_persist;
2809 		log.u_bbr.flex8 = which;
2810 		if (rack->rack_no_prr)
2811 			log.u_bbr.pkts_out = 0;
2812 		else
2813 			log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
2814 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2815 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2816 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2817 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2818 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2819 		log.u_bbr.pacing_gain = rack->r_must_retran;
2820 		log.u_bbr.cwnd_gain = rack->rack_deferred_inited;
2821 		log.u_bbr.pkt_epoch = rack->rc_has_collapsed;
2822 		log.u_bbr.lt_epoch = rack->rc_tp->t_rxtshift;
2823 		log.u_bbr.lost = rack_rto_min;
2824 		log.u_bbr.epoch = rack->r_ctl.roundends;
2825 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
2826 		log.u_bbr.bw_inuse <<= 32;
2827 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
2828 		log.u_bbr.applimited = rack->rc_tp->t_flags2;
2829 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2830 		    &rack->rc_inp->inp_socket->so_rcv,
2831 		    &rack->rc_inp->inp_socket->so_snd,
2832 		    BBR_LOG_TIMERSTAR, 0,
2833 		    0, &log, false, &tv);
2834 	}
2835 }
2836 
2837 static void
2838 rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm)
2839 {
2840 	if (tcp_bblogging_on(rack->rc_tp)) {
2841 		union tcp_log_stackspecific log;
2842 		struct timeval tv;
2843 
2844 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2845 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2846 		log.u_bbr.flex8 = to_num;
2847 		log.u_bbr.flex1 = rack->r_ctl.rc_rack_min_rtt;
2848 		log.u_bbr.flex2 = rack->rc_rack_rtt;
2849 		if (rsm == NULL)
2850 			log.u_bbr.flex3 = 0;
2851 		else
2852 			log.u_bbr.flex3 = rsm->r_end - rsm->r_start;
2853 		if (rack->rack_no_prr)
2854 			log.u_bbr.flex5 = 0;
2855 		else
2856 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
2857 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2858 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2859 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2860 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2861 		log.u_bbr.pacing_gain = rack->r_must_retran;
2862 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
2863 		log.u_bbr.bw_inuse <<= 32;
2864 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
2865 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2866 		    &rack->rc_inp->inp_socket->so_rcv,
2867 		    &rack->rc_inp->inp_socket->so_snd,
2868 		    BBR_LOG_RTO, 0,
2869 		    0, &log, false, &tv);
2870 	}
2871 }
2872 
2873 static void
2874 rack_log_map_chg(struct tcpcb *tp, struct tcp_rack *rack,
2875 		 struct rack_sendmap *prev,
2876 		 struct rack_sendmap *rsm,
2877 		 struct rack_sendmap *next,
2878 		 int flag, uint32_t th_ack, int line)
2879 {
2880 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
2881 		union tcp_log_stackspecific log;
2882 		struct timeval tv;
2883 
2884 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2885 		log.u_bbr.flex8 = flag;
2886 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2887 		log.u_bbr.cur_del_rate = (uint64_t)prev;
2888 		log.u_bbr.delRate = (uint64_t)rsm;
2889 		log.u_bbr.rttProp = (uint64_t)next;
2890 		log.u_bbr.flex7 = 0;
2891 		if (prev) {
2892 			log.u_bbr.flex1 = prev->r_start;
2893 			log.u_bbr.flex2 = prev->r_end;
2894 			log.u_bbr.flex7 |= 0x4;
2895 		}
2896 		if (rsm) {
2897 			log.u_bbr.flex3 = rsm->r_start;
2898 			log.u_bbr.flex4 = rsm->r_end;
2899 			log.u_bbr.flex7 |= 0x2;
2900 		}
2901 		if (next) {
2902 			log.u_bbr.flex5 = next->r_start;
2903 			log.u_bbr.flex6 = next->r_end;
2904 			log.u_bbr.flex7 |= 0x1;
2905 		}
2906 		log.u_bbr.applimited = line;
2907 		log.u_bbr.pkts_out = th_ack;
2908 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2909 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2910 		if (rack->rack_no_prr)
2911 			log.u_bbr.lost = 0;
2912 		else
2913 			log.u_bbr.lost = rack->r_ctl.rc_prr_sndcnt;
2914 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
2915 		log.u_bbr.bw_inuse <<= 32;
2916 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
2917 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2918 		    &rack->rc_inp->inp_socket->so_rcv,
2919 		    &rack->rc_inp->inp_socket->so_snd,
2920 		    TCP_LOG_MAPCHG, 0,
2921 		    0, &log, false, &tv);
2922 	}
2923 }
2924 
2925 static void
2926 rack_log_rtt_upd(struct tcpcb *tp, struct tcp_rack *rack, uint32_t t, uint32_t len,
2927 		 struct rack_sendmap *rsm, int conf)
2928 {
2929 	if (tcp_bblogging_on(tp)) {
2930 		union tcp_log_stackspecific log;
2931 		struct timeval tv;
2932 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2933 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2934 		log.u_bbr.flex1 = t;
2935 		log.u_bbr.flex2 = len;
2936 		log.u_bbr.flex3 = rack->r_ctl.rc_rack_min_rtt;
2937 		log.u_bbr.flex4 = rack->r_ctl.rack_rs.rs_rtt_lowest;
2938 		log.u_bbr.flex5 = rack->r_ctl.rack_rs.rs_rtt_highest;
2939 		log.u_bbr.flex6 = rack->r_ctl.rack_rs.rs_us_rtrcnt;
2940 		log.u_bbr.flex7 = conf;
2941 		log.u_bbr.rttProp = (uint64_t)rack->r_ctl.rack_rs.rs_rtt_tot;
2942 		log.u_bbr.flex8 = rack->r_ctl.rc_rate_sample_method;
2943 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2944 		log.u_bbr.delivered = rack->r_ctl.rack_rs.rs_us_rtrcnt;
2945 		log.u_bbr.pkts_out = rack->r_ctl.rack_rs.rs_flags;
2946 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2947 		if (rsm) {
2948 			log.u_bbr.pkt_epoch = rsm->r_start;
2949 			log.u_bbr.lost = rsm->r_end;
2950 			log.u_bbr.cwnd_gain = rsm->r_rtr_cnt;
2951 			/* We loose any upper of the 24 bits */
2952 			log.u_bbr.pacing_gain = (uint16_t)rsm->r_flags;
2953 		} else {
2954 			/* Its a SYN */
2955 			log.u_bbr.pkt_epoch = rack->rc_tp->iss;
2956 			log.u_bbr.lost = 0;
2957 			log.u_bbr.cwnd_gain = 0;
2958 			log.u_bbr.pacing_gain = 0;
2959 		}
2960 		/* Write out general bits of interest rrs here */
2961 		log.u_bbr.use_lt_bw = rack->rc_highly_buffered;
2962 		log.u_bbr.use_lt_bw <<= 1;
2963 		log.u_bbr.use_lt_bw |= rack->forced_ack;
2964 		log.u_bbr.use_lt_bw <<= 1;
2965 		log.u_bbr.use_lt_bw |= rack->rc_gp_dyn_mul;
2966 		log.u_bbr.use_lt_bw <<= 1;
2967 		log.u_bbr.use_lt_bw |= rack->in_probe_rtt;
2968 		log.u_bbr.use_lt_bw <<= 1;
2969 		log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt;
2970 		log.u_bbr.use_lt_bw <<= 1;
2971 		log.u_bbr.use_lt_bw |= rack->app_limited_needs_set;
2972 		log.u_bbr.use_lt_bw <<= 1;
2973 		log.u_bbr.use_lt_bw |= rack->rc_gp_filled;
2974 		log.u_bbr.use_lt_bw <<= 1;
2975 		log.u_bbr.use_lt_bw |= rack->rc_dragged_bottom;
2976 		log.u_bbr.applimited = rack->r_ctl.rc_target_probertt_flight;
2977 		log.u_bbr.epoch = rack->r_ctl.rc_time_probertt_starts;
2978 		log.u_bbr.lt_epoch = rack->r_ctl.rc_time_probertt_entered;
2979 		log.u_bbr.cur_del_rate = rack->r_ctl.rc_lower_rtt_us_cts;
2980 		log.u_bbr.delRate = rack->r_ctl.rc_gp_srtt;
2981 		log.u_bbr.bw_inuse = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
2982 		log.u_bbr.bw_inuse <<= 32;
2983 		if (rsm)
2984 			log.u_bbr.bw_inuse |= ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]);
2985 		TCP_LOG_EVENTP(tp, NULL,
2986 		    &rack->rc_inp->inp_socket->so_rcv,
2987 		    &rack->rc_inp->inp_socket->so_snd,
2988 		    BBR_LOG_BBRRTT, 0,
2989 		    0, &log, false, &tv);
2990 
2991 
2992 	}
2993 }
2994 
2995 static void
2996 rack_log_rtt_sample(struct tcp_rack *rack, uint32_t rtt)
2997 {
2998 	/*
2999 	 * Log the rtt sample we are
3000 	 * applying to the srtt algorithm in
3001 	 * useconds.
3002 	 */
3003 	if (tcp_bblogging_on(rack->rc_tp)) {
3004 		union tcp_log_stackspecific log;
3005 		struct timeval tv;
3006 
3007 		/* Convert our ms to a microsecond */
3008 		memset(&log, 0, sizeof(log));
3009 		log.u_bbr.flex1 = rtt;
3010 		log.u_bbr.flex2 = rack->r_ctl.ack_count;
3011 		log.u_bbr.flex3 = rack->r_ctl.sack_count;
3012 		log.u_bbr.flex4 = rack->r_ctl.sack_noextra_move;
3013 		log.u_bbr.flex5 = rack->r_ctl.sack_moved_extra;
3014 		log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
3015 		log.u_bbr.flex7 = 1;
3016 		log.u_bbr.flex8 = rack->sack_attack_disable;
3017 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3018 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3019 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3020 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3021 		log.u_bbr.pacing_gain = rack->r_must_retran;
3022 		/*
3023 		 * We capture in delRate the upper 32 bits as
3024 		 * the confidence level we had declared, and the
3025 		 * lower 32 bits as the actual RTT using the arrival
3026 		 * timestamp.
3027 		 */
3028 		log.u_bbr.delRate = rack->r_ctl.rack_rs.confidence;
3029 		log.u_bbr.delRate <<= 32;
3030 		log.u_bbr.delRate |= rack->r_ctl.rack_rs.rs_us_rtt;
3031 		/* Lets capture all the things that make up t_rtxcur */
3032 		log.u_bbr.applimited = rack_rto_min;
3033 		log.u_bbr.epoch = rack_rto_max;
3034 		log.u_bbr.lt_epoch = rack->r_ctl.timer_slop;
3035 		log.u_bbr.lost = rack_rto_min;
3036 		log.u_bbr.pkt_epoch = TICKS_2_USEC(tcp_rexmit_slop);
3037 		log.u_bbr.rttProp = RACK_REXMTVAL(rack->rc_tp);
3038 		log.u_bbr.bw_inuse = rack->r_ctl.act_rcv_time.tv_sec;
3039 		log.u_bbr.bw_inuse *= HPTS_USEC_IN_SEC;
3040 		log.u_bbr.bw_inuse += rack->r_ctl.act_rcv_time.tv_usec;
3041 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3042 		    &rack->rc_inp->inp_socket->so_rcv,
3043 		    &rack->rc_inp->inp_socket->so_snd,
3044 		    TCP_LOG_RTT, 0,
3045 		    0, &log, false, &tv);
3046 	}
3047 }
3048 
3049 static void
3050 rack_log_rtt_sample_calc(struct tcp_rack *rack, uint32_t rtt, uint32_t send_time, uint32_t ack_time, int where)
3051 {
3052 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
3053 		union tcp_log_stackspecific log;
3054 		struct timeval tv;
3055 
3056 		/* Convert our ms to a microsecond */
3057 		memset(&log, 0, sizeof(log));
3058 		log.u_bbr.flex1 = rtt;
3059 		log.u_bbr.flex2 = send_time;
3060 		log.u_bbr.flex3 = ack_time;
3061 		log.u_bbr.flex4 = where;
3062 		log.u_bbr.flex7 = 2;
3063 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3064 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
3065 		log.u_bbr.bw_inuse <<= 32;
3066 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
3067 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3068 		    &rack->rc_inp->inp_socket->so_rcv,
3069 		    &rack->rc_inp->inp_socket->so_snd,
3070 		    TCP_LOG_RTT, 0,
3071 		    0, &log, false, &tv);
3072 	}
3073 }
3074 
3075 
3076 static void
3077 rack_log_rtt_sendmap(struct tcp_rack *rack, uint32_t idx, uint64_t tsv, uint32_t tsecho)
3078 {
3079 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
3080 		union tcp_log_stackspecific log;
3081 		struct timeval tv;
3082 
3083 		/* Convert our ms to a microsecond */
3084 		memset(&log, 0, sizeof(log));
3085 		log.u_bbr.flex1 = idx;
3086 		log.u_bbr.flex2 = rack_ts_to_msec(tsv);
3087 		log.u_bbr.flex3 = tsecho;
3088 		log.u_bbr.flex7 = 3;
3089 		log.u_bbr.rttProp = tsv;
3090 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3091 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
3092 		log.u_bbr.bw_inuse <<= 32;
3093 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
3094 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3095 		    &rack->rc_inp->inp_socket->so_rcv,
3096 		    &rack->rc_inp->inp_socket->so_snd,
3097 		    TCP_LOG_RTT, 0,
3098 		    0, &log, false, &tv);
3099 	}
3100 }
3101 
3102 
3103 static inline void
3104 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick,  int event, int line)
3105 {
3106 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
3107 		union tcp_log_stackspecific log;
3108 		struct timeval tv;
3109 
3110 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3111 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3112 		log.u_bbr.flex1 = line;
3113 		log.u_bbr.flex2 = tick;
3114 		log.u_bbr.flex3 = tp->t_maxunacktime;
3115 		log.u_bbr.flex4 = tp->t_acktime;
3116 		log.u_bbr.flex8 = event;
3117 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3118 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3119 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3120 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3121 		log.u_bbr.pacing_gain = rack->r_must_retran;
3122 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
3123 		log.u_bbr.bw_inuse <<= 32;
3124 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
3125 		TCP_LOG_EVENTP(tp, NULL,
3126 		    &rack->rc_inp->inp_socket->so_rcv,
3127 		    &rack->rc_inp->inp_socket->so_snd,
3128 		    BBR_LOG_PROGRESS, 0,
3129 		    0, &log, false, &tv);
3130 	}
3131 }
3132 
3133 static void
3134 rack_log_type_bbrsnd(struct tcp_rack *rack, uint32_t len, uint32_t slot, uint32_t cts, struct timeval *tv, int line)
3135 {
3136 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
3137 		union tcp_log_stackspecific log;
3138 
3139 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3140 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3141 		log.u_bbr.flex1 = slot;
3142 		if (rack->rack_no_prr)
3143 			log.u_bbr.flex2 = 0;
3144 		else
3145 			log.u_bbr.flex2 = rack->r_ctl.rc_prr_sndcnt;
3146 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
3147 		log.u_bbr.flex5 = rack->r_ctl.ack_during_sd;
3148 		log.u_bbr.flex6 = line;
3149 		log.u_bbr.flex7 = (0x0000ffff & rack->r_ctl.rc_hpts_flags);
3150 		log.u_bbr.flex8 = rack->rc_in_persist;
3151 		log.u_bbr.timeStamp = cts;
3152 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3153 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3154 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3155 		log.u_bbr.pacing_gain = rack->r_must_retran;
3156 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3157 		    &rack->rc_inp->inp_socket->so_rcv,
3158 		    &rack->rc_inp->inp_socket->so_snd,
3159 		    BBR_LOG_BBRSND, 0,
3160 		    0, &log, false, tv);
3161 	}
3162 }
3163 
3164 static void
3165 rack_log_doseg_done(struct tcp_rack *rack, uint32_t cts, int32_t nxt_pkt, int32_t did_out, int way_out, int nsegs)
3166 {
3167 	if (tcp_bblogging_on(rack->rc_tp)) {
3168 		union tcp_log_stackspecific log;
3169 		struct timeval tv;
3170 
3171 		memset(&log, 0, sizeof(log));
3172 		log.u_bbr.flex1 = did_out;
3173 		log.u_bbr.flex2 = nxt_pkt;
3174 		log.u_bbr.flex3 = way_out;
3175 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
3176 		if (rack->rack_no_prr)
3177 			log.u_bbr.flex5 = 0;
3178 		else
3179 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
3180 		log.u_bbr.flex6 = nsegs;
3181 		log.u_bbr.applimited = rack->r_ctl.rc_pace_min_segs;
3182 		log.u_bbr.flex7 = rack->rc_ack_can_sendout_data;	/* Do we have ack-can-send set */
3183 		log.u_bbr.flex7 <<= 1;
3184 		log.u_bbr.flex7 |= rack->r_fast_output;	/* is fast output primed */
3185 		log.u_bbr.flex7 <<= 1;
3186 		log.u_bbr.flex7 |= rack->r_wanted_output;	/* Do we want output */
3187 		log.u_bbr.flex8 = rack->rc_in_persist;
3188 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3189 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3190 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3191 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
3192 		log.u_bbr.use_lt_bw <<= 1;
3193 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
3194 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3195 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3196 		log.u_bbr.pacing_gain = rack->r_must_retran;
3197 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
3198 		log.u_bbr.bw_inuse <<= 32;
3199 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
3200 		log.u_bbr.epoch = rack->rc_inp->inp_socket->so_snd.sb_hiwat;
3201 		log.u_bbr.lt_epoch = rack->rc_inp->inp_socket->so_rcv.sb_hiwat;
3202 		log.u_bbr.lost = rack->rc_tp->t_srtt;
3203 		log.u_bbr.pkt_epoch = rack->rc_tp->rfbuf_cnt;
3204 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3205 		    &rack->rc_inp->inp_socket->so_rcv,
3206 		    &rack->rc_inp->inp_socket->so_snd,
3207 		    BBR_LOG_DOSEG_DONE, 0,
3208 		    0, &log, false, &tv);
3209 	}
3210 }
3211 
3212 static void
3213 rack_log_type_pacing_sizes(struct tcpcb *tp, struct tcp_rack *rack, uint32_t arg1, uint32_t arg2, uint32_t arg3, uint8_t frm)
3214 {
3215 	if (tcp_bblogging_on(rack->rc_tp)) {
3216 		union tcp_log_stackspecific log;
3217 		struct timeval tv;
3218 
3219 		memset(&log, 0, sizeof(log));
3220 		log.u_bbr.flex1 = rack->r_ctl.rc_pace_min_segs;
3221 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
3222 		log.u_bbr.flex4 = arg1;
3223 		log.u_bbr.flex5 = arg2;
3224 		log.u_bbr.flex7 = rack->r_ctl.rc_user_set_min_segs;
3225 		log.u_bbr.flex6 = arg3;
3226 		log.u_bbr.flex8 = frm;
3227 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3228 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3229 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3230 		log.u_bbr.applimited = rack->r_ctl.rc_sacked;
3231 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3232 		log.u_bbr.pacing_gain = rack->r_must_retran;
3233 		TCP_LOG_EVENTP(tp, NULL, &tptosocket(tp)->so_rcv,
3234 		    &tptosocket(tp)->so_snd,
3235 		    TCP_HDWR_PACE_SIZE, 0, 0, &log, false, &tv);
3236 	}
3237 }
3238 
3239 static void
3240 rack_log_type_just_return(struct tcp_rack *rack, uint32_t cts, uint32_t tlen, uint32_t slot,
3241 			  uint8_t hpts_calling, int reason, uint32_t cwnd_to_use)
3242 {
3243 	if (tcp_bblogging_on(rack->rc_tp)) {
3244 		union tcp_log_stackspecific log;
3245 		struct timeval tv;
3246 
3247 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3248 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3249 		log.u_bbr.flex1 = slot;
3250 		log.u_bbr.flex2 = rack->r_ctl.rc_hpts_flags;
3251 		log.u_bbr.flex4 = reason;
3252 		if (rack->rack_no_prr)
3253 			log.u_bbr.flex5 = 0;
3254 		else
3255 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
3256 		log.u_bbr.flex7 = hpts_calling;
3257 		log.u_bbr.flex8 = rack->rc_in_persist;
3258 		log.u_bbr.lt_epoch = cwnd_to_use;
3259 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3260 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3261 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3262 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3263 		log.u_bbr.pacing_gain = rack->r_must_retran;
3264 		log.u_bbr.cwnd_gain = rack->rc_has_collapsed;
3265 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
3266 		log.u_bbr.bw_inuse <<= 32;
3267 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
3268 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3269 		    &rack->rc_inp->inp_socket->so_rcv,
3270 		    &rack->rc_inp->inp_socket->so_snd,
3271 		    BBR_LOG_JUSTRET, 0,
3272 		    tlen, &log, false, &tv);
3273 	}
3274 }
3275 
3276 static void
3277 rack_log_to_cancel(struct tcp_rack *rack, int32_t hpts_removed, int line, uint32_t us_cts,
3278 		   struct timeval *tv, uint32_t flags_on_entry)
3279 {
3280 	if (tcp_bblogging_on(rack->rc_tp)) {
3281 		union tcp_log_stackspecific log;
3282 
3283 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3284 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3285 		log.u_bbr.flex1 = line;
3286 		log.u_bbr.flex2 = rack->r_ctl.rc_last_output_to;
3287 		log.u_bbr.flex3 = flags_on_entry;
3288 		log.u_bbr.flex4 = us_cts;
3289 		if (rack->rack_no_prr)
3290 			log.u_bbr.flex5 = 0;
3291 		else
3292 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
3293 		log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
3294 		log.u_bbr.flex7 = hpts_removed;
3295 		log.u_bbr.flex8 = 1;
3296 		log.u_bbr.applimited = rack->r_ctl.rc_hpts_flags;
3297 		log.u_bbr.timeStamp = us_cts;
3298 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3299 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3300 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3301 		log.u_bbr.pacing_gain = rack->r_must_retran;
3302 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
3303 		log.u_bbr.bw_inuse <<= 32;
3304 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
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_TIMERCANC, 0,
3309 		    0, &log, false, tv);
3310 	}
3311 }
3312 
3313 static void
3314 rack_log_alt_to_to_cancel(struct tcp_rack *rack,
3315 			  uint32_t flex1, uint32_t flex2,
3316 			  uint32_t flex3, uint32_t flex4,
3317 			  uint32_t flex5, uint32_t flex6,
3318 			  uint16_t flex7, uint8_t mod)
3319 {
3320 	if (tcp_bblogging_on(rack->rc_tp)) {
3321 		union tcp_log_stackspecific log;
3322 		struct timeval tv;
3323 
3324 		if (mod == 1) {
3325 			/* No you can't use 1, its for the real to cancel */
3326 			return;
3327 		}
3328 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3329 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3330 		log.u_bbr.flex1 = flex1;
3331 		log.u_bbr.flex2 = flex2;
3332 		log.u_bbr.flex3 = flex3;
3333 		log.u_bbr.flex4 = flex4;
3334 		log.u_bbr.flex5 = flex5;
3335 		log.u_bbr.flex6 = flex6;
3336 		log.u_bbr.flex7 = flex7;
3337 		log.u_bbr.flex8 = mod;
3338 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3339 		    &rack->rc_inp->inp_socket->so_rcv,
3340 		    &rack->rc_inp->inp_socket->so_snd,
3341 		    BBR_LOG_TIMERCANC, 0,
3342 		    0, &log, false, &tv);
3343 	}
3344 }
3345 
3346 static void
3347 rack_log_to_processing(struct tcp_rack *rack, uint32_t cts, int32_t ret, int32_t timers)
3348 {
3349 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
3350 		union tcp_log_stackspecific log;
3351 		struct timeval tv;
3352 
3353 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3354 		log.u_bbr.flex1 = timers;
3355 		log.u_bbr.flex2 = ret;
3356 		log.u_bbr.flex3 = rack->r_ctl.rc_timer_exp;
3357 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
3358 		log.u_bbr.flex5 = cts;
3359 		if (rack->rack_no_prr)
3360 			log.u_bbr.flex6 = 0;
3361 		else
3362 			log.u_bbr.flex6 = rack->r_ctl.rc_prr_sndcnt;
3363 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3364 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3365 		log.u_bbr.pacing_gain = rack->r_must_retran;
3366 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3367 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3368 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3369 		    &rack->rc_inp->inp_socket->so_rcv,
3370 		    &rack->rc_inp->inp_socket->so_snd,
3371 		    BBR_LOG_TO_PROCESS, 0,
3372 		    0, &log, false, &tv);
3373 	}
3374 }
3375 
3376 static void
3377 rack_log_to_prr(struct tcp_rack *rack, int frm, int orig_cwnd, int line)
3378 {
3379 	if (tcp_bblogging_on(rack->rc_tp)) {
3380 		union tcp_log_stackspecific log;
3381 		struct timeval tv;
3382 
3383 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3384 		log.u_bbr.flex1 = rack->r_ctl.rc_prr_out;
3385 		log.u_bbr.flex2 = rack->r_ctl.rc_prr_recovery_fs;
3386 		if (rack->rack_no_prr)
3387 			log.u_bbr.flex3 = 0;
3388 		else
3389 			log.u_bbr.flex3 = rack->r_ctl.rc_prr_sndcnt;
3390 		log.u_bbr.flex4 = rack->r_ctl.rc_prr_delivered;
3391 		log.u_bbr.flex5 = rack->r_ctl.rc_sacked;
3392 		log.u_bbr.flex6 = rack->r_ctl.rc_holes_rxt;
3393 		log.u_bbr.flex7 = line;
3394 		log.u_bbr.flex8 = frm;
3395 		log.u_bbr.pkts_out = orig_cwnd;
3396 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3397 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3398 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
3399 		log.u_bbr.use_lt_bw <<= 1;
3400 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
3401 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3402 		    &rack->rc_inp->inp_socket->so_rcv,
3403 		    &rack->rc_inp->inp_socket->so_snd,
3404 		    BBR_LOG_BBRUPD, 0,
3405 		    0, &log, false, &tv);
3406 	}
3407 }
3408 
3409 #ifdef TCP_SAD_DETECTION
3410 static void
3411 rack_log_sad(struct tcp_rack *rack, int event)
3412 {
3413 	if (tcp_bblogging_on(rack->rc_tp)) {
3414 		union tcp_log_stackspecific log;
3415 		struct timeval tv;
3416 
3417 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3418 		log.u_bbr.flex1 = rack->r_ctl.sack_count;
3419 		log.u_bbr.flex2 = rack->r_ctl.ack_count;
3420 		log.u_bbr.flex3 = rack->r_ctl.sack_moved_extra;
3421 		log.u_bbr.flex4 = rack->r_ctl.sack_noextra_move;
3422 		log.u_bbr.flex5 = rack->r_ctl.rc_num_maps_alloced;
3423 		log.u_bbr.flex6 = tcp_sack_to_ack_thresh;
3424 		log.u_bbr.pkts_out = tcp_sack_to_move_thresh;
3425 		log.u_bbr.lt_epoch = (tcp_force_detection << 8);
3426 		log.u_bbr.lt_epoch |= rack->do_detection;
3427 		log.u_bbr.applimited = tcp_map_minimum;
3428 		log.u_bbr.flex7 = rack->sack_attack_disable;
3429 		log.u_bbr.flex8 = event;
3430 		log.u_bbr.bbr_state = rack->rc_suspicious;
3431 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3432 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3433 		log.u_bbr.delivered = tcp_sad_decay_val;
3434 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3435 		    &rack->rc_inp->inp_socket->so_rcv,
3436 		    &rack->rc_inp->inp_socket->so_snd,
3437 		    TCP_SAD_DETECT, 0,
3438 		    0, &log, false, &tv);
3439 	}
3440 }
3441 #endif
3442 
3443 static void
3444 rack_counter_destroy(void)
3445 {
3446 	counter_u64_free(rack_total_bytes);
3447 	counter_u64_free(rack_fto_send);
3448 	counter_u64_free(rack_fto_rsm_send);
3449 	counter_u64_free(rack_nfto_resend);
3450 	counter_u64_free(rack_hw_pace_init_fail);
3451 	counter_u64_free(rack_hw_pace_lost);
3452 	counter_u64_free(rack_non_fto_send);
3453 	counter_u64_free(rack_extended_rfo);
3454 	counter_u64_free(rack_ack_total);
3455 	counter_u64_free(rack_express_sack);
3456 	counter_u64_free(rack_sack_total);
3457 	counter_u64_free(rack_move_none);
3458 	counter_u64_free(rack_move_some);
3459 	counter_u64_free(rack_sack_attacks_detected);
3460 	counter_u64_free(rack_sack_attacks_reversed);
3461 	counter_u64_free(rack_sack_attacks_suspect);
3462 	counter_u64_free(rack_sack_used_next_merge);
3463 	counter_u64_free(rack_sack_used_prev_merge);
3464 	counter_u64_free(rack_tlp_tot);
3465 	counter_u64_free(rack_tlp_newdata);
3466 	counter_u64_free(rack_tlp_retran);
3467 	counter_u64_free(rack_tlp_retran_bytes);
3468 	counter_u64_free(rack_to_tot);
3469 	counter_u64_free(rack_saw_enobuf);
3470 	counter_u64_free(rack_saw_enobuf_hw);
3471 	counter_u64_free(rack_saw_enetunreach);
3472 	counter_u64_free(rack_hot_alloc);
3473 	counter_u64_free(tcp_policer_detected);
3474 	counter_u64_free(rack_to_alloc);
3475 	counter_u64_free(rack_to_alloc_hard);
3476 	counter_u64_free(rack_to_alloc_emerg);
3477 	counter_u64_free(rack_to_alloc_limited);
3478 	counter_u64_free(rack_alloc_limited_conns);
3479 	counter_u64_free(rack_split_limited);
3480 	counter_u64_free(rack_multi_single_eq);
3481 	counter_u64_free(rack_rxt_clamps_cwnd);
3482 	counter_u64_free(rack_rxt_clamps_cwnd_uniq);
3483 	counter_u64_free(rack_proc_non_comp_ack);
3484 	counter_u64_free(rack_sack_proc_all);
3485 	counter_u64_free(rack_sack_proc_restart);
3486 	counter_u64_free(rack_sack_proc_short);
3487 	counter_u64_free(rack_sack_skipped_acked);
3488 	counter_u64_free(rack_sack_splits);
3489 	counter_u64_free(rack_input_idle_reduces);
3490 	counter_u64_free(rack_collapsed_win);
3491 	counter_u64_free(rack_collapsed_win_rxt);
3492 	counter_u64_free(rack_collapsed_win_rxt_bytes);
3493 	counter_u64_free(rack_collapsed_win_seen);
3494 	counter_u64_free(rack_try_scwnd);
3495 	counter_u64_free(rack_persists_sends);
3496 	counter_u64_free(rack_persists_acks);
3497 	counter_u64_free(rack_persists_loss);
3498 	counter_u64_free(rack_persists_lost_ends);
3499 #ifdef INVARIANTS
3500 	counter_u64_free(rack_adjust_map_bw);
3501 #endif
3502 	COUNTER_ARRAY_FREE(rack_out_size, TCP_MSS_ACCT_SIZE);
3503 	COUNTER_ARRAY_FREE(rack_opts_arry, RACK_OPTS_SIZE);
3504 }
3505 
3506 static struct rack_sendmap *
3507 rack_alloc(struct tcp_rack *rack)
3508 {
3509 	struct rack_sendmap *rsm;
3510 
3511 	/*
3512 	 * First get the top of the list it in
3513 	 * theory is the "hottest" rsm we have,
3514 	 * possibly just freed by ack processing.
3515 	 */
3516 	if (rack->rc_free_cnt > rack_free_cache) {
3517 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
3518 		TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
3519 		counter_u64_add(rack_hot_alloc, 1);
3520 		rack->rc_free_cnt--;
3521 		return (rsm);
3522 	}
3523 	/*
3524 	 * Once we get under our free cache we probably
3525 	 * no longer have a "hot" one available. Lets
3526 	 * get one from UMA.
3527 	 */
3528 	rsm = uma_zalloc(rack_zone, M_NOWAIT);
3529 	if (rsm) {
3530 		rack->r_ctl.rc_num_maps_alloced++;
3531 		counter_u64_add(rack_to_alloc, 1);
3532 		return (rsm);
3533 	}
3534 	/*
3535 	 * Dig in to our aux rsm's (the last two) since
3536 	 * UMA failed to get us one.
3537 	 */
3538 	if (rack->rc_free_cnt) {
3539 		counter_u64_add(rack_to_alloc_emerg, 1);
3540 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
3541 		TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
3542 		rack->rc_free_cnt--;
3543 		return (rsm);
3544 	}
3545 	return (NULL);
3546 }
3547 
3548 static struct rack_sendmap *
3549 rack_alloc_full_limit(struct tcp_rack *rack)
3550 {
3551 	if ((V_tcp_map_entries_limit > 0) &&
3552 	    (rack->do_detection == 0) &&
3553 	    (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
3554 		counter_u64_add(rack_to_alloc_limited, 1);
3555 		if (!rack->alloc_limit_reported) {
3556 			rack->alloc_limit_reported = 1;
3557 			counter_u64_add(rack_alloc_limited_conns, 1);
3558 		}
3559 		return (NULL);
3560 	}
3561 	return (rack_alloc(rack));
3562 }
3563 
3564 /* wrapper to allocate a sendmap entry, subject to a specific limit */
3565 static struct rack_sendmap *
3566 rack_alloc_limit(struct tcp_rack *rack, uint8_t limit_type)
3567 {
3568 	struct rack_sendmap *rsm;
3569 
3570 	if (limit_type) {
3571 		/* currently there is only one limit type */
3572 		if (rack->r_ctl.rc_split_limit > 0 &&
3573 		    (rack->do_detection == 0) &&
3574 		    rack->r_ctl.rc_num_split_allocs >= rack->r_ctl.rc_split_limit) {
3575 			counter_u64_add(rack_split_limited, 1);
3576 			if (!rack->alloc_limit_reported) {
3577 				rack->alloc_limit_reported = 1;
3578 				counter_u64_add(rack_alloc_limited_conns, 1);
3579 			}
3580 			return (NULL);
3581 #ifdef TCP_SAD_DETECTION
3582 		} else if ((tcp_sad_limit != 0) &&
3583 			   (rack->do_detection == 1) &&
3584 			   (rack->r_ctl.rc_num_split_allocs >= tcp_sad_limit)) {
3585 			counter_u64_add(rack_split_limited, 1);
3586 			if (!rack->alloc_limit_reported) {
3587 				rack->alloc_limit_reported = 1;
3588 				counter_u64_add(rack_alloc_limited_conns, 1);
3589 			}
3590 			return (NULL);
3591 #endif
3592 		}
3593 	}
3594 
3595 	/* allocate and mark in the limit type, if set */
3596 	rsm = rack_alloc(rack);
3597 	if (rsm != NULL && limit_type) {
3598 		rsm->r_limit_type = limit_type;
3599 		rack->r_ctl.rc_num_split_allocs++;
3600 	}
3601 	return (rsm);
3602 }
3603 
3604 static void
3605 rack_free_trim(struct tcp_rack *rack)
3606 {
3607 	struct rack_sendmap *rsm;
3608 
3609 	/*
3610 	 * Free up all the tail entries until
3611 	 * we get our list down to the limit.
3612 	 */
3613 	while (rack->rc_free_cnt > rack_free_cache) {
3614 		rsm = TAILQ_LAST(&rack->r_ctl.rc_free, rack_head);
3615 		TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
3616 		rack->rc_free_cnt--;
3617 		rack->r_ctl.rc_num_maps_alloced--;
3618 		uma_zfree(rack_zone, rsm);
3619 	}
3620 }
3621 
3622 static void
3623 rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm)
3624 {
3625 	if (rsm->r_flags & RACK_APP_LIMITED) {
3626 		if (rack->r_ctl.rc_app_limited_cnt > 0) {
3627 			rack->r_ctl.rc_app_limited_cnt--;
3628 		}
3629 	}
3630 	if (rsm->r_limit_type) {
3631 		/* currently there is only one limit type */
3632 		rack->r_ctl.rc_num_split_allocs--;
3633 	}
3634 	if (rsm == rack->r_ctl.rc_first_appl) {
3635 		rack->r_ctl.cleared_app_ack_seq = rsm->r_start + (rsm->r_end - rsm->r_start);
3636 		rack->r_ctl.cleared_app_ack = 1;
3637 		if (rack->r_ctl.rc_app_limited_cnt == 0)
3638 			rack->r_ctl.rc_first_appl = NULL;
3639 		else
3640 			rack->r_ctl.rc_first_appl = tqhash_find(rack->r_ctl.tqh, rsm->r_nseq_appl);
3641 	}
3642 	if (rsm == rack->r_ctl.rc_resend)
3643 		rack->r_ctl.rc_resend = NULL;
3644 	if (rsm == rack->r_ctl.rc_end_appl)
3645 		rack->r_ctl.rc_end_appl = NULL;
3646 	if (rack->r_ctl.rc_tlpsend == rsm)
3647 		rack->r_ctl.rc_tlpsend = NULL;
3648 	if (rack->r_ctl.rc_sacklast == rsm)
3649 		rack->r_ctl.rc_sacklast = NULL;
3650 	memset(rsm, 0, sizeof(struct rack_sendmap));
3651 	/* Make sure we are not going to overrun our count limit of 0xff */
3652 	if ((rack->rc_free_cnt + 1) > RACK_FREE_CNT_MAX) {
3653 		rack_free_trim(rack);
3654 	}
3655 	TAILQ_INSERT_HEAD(&rack->r_ctl.rc_free, rsm, r_tnext);
3656 	rack->rc_free_cnt++;
3657 }
3658 
3659 static uint32_t
3660 rack_get_measure_window(struct tcpcb *tp, struct tcp_rack *rack)
3661 {
3662 	uint64_t srtt, bw, len, tim;
3663 	uint32_t segsiz, def_len, minl;
3664 
3665 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
3666 	def_len = rack_def_data_window * segsiz;
3667 	if (rack->rc_gp_filled == 0) {
3668 		/*
3669 		 * We have no measurement (IW is in flight?) so
3670 		 * we can only guess using our data_window sysctl
3671 		 * value (usually 20MSS).
3672 		 */
3673 		return (def_len);
3674 	}
3675 	/*
3676 	 * Now we have a number of factors to consider.
3677 	 *
3678 	 * 1) We have a desired BDP which is usually
3679 	 *    at least 2.
3680 	 * 2) We have a minimum number of rtt's usually 1 SRTT
3681 	 *    but we allow it too to be more.
3682 	 * 3) We want to make sure a measurement last N useconds (if
3683 	 *    we have set rack_min_measure_usec.
3684 	 *
3685 	 * We handle the first concern here by trying to create a data
3686 	 * window of max(rack_def_data_window, DesiredBDP). The
3687 	 * second concern we handle in not letting the measurement
3688 	 * window end normally until at least the required SRTT's
3689 	 * have gone by which is done further below in
3690 	 * rack_enough_for_measurement(). Finally the third concern
3691 	 * we also handle here by calculating how long that time
3692 	 * would take at the current BW and then return the
3693 	 * max of our first calculation and that length. Note
3694 	 * that if rack_min_measure_usec is 0, we don't deal
3695 	 * with concern 3. Also for both Concern 1 and 3 an
3696 	 * application limited period could end the measurement
3697 	 * earlier.
3698 	 *
3699 	 * So lets calculate the BDP with the "known" b/w using
3700 	 * the SRTT has our rtt and then multiply it by the
3701 	 * goal.
3702 	 */
3703 	bw = rack_get_bw(rack);
3704 	srtt = (uint64_t)tp->t_srtt;
3705 	len = bw * srtt;
3706 	len /= (uint64_t)HPTS_USEC_IN_SEC;
3707 	len *= max(1, rack_goal_bdp);
3708 	/* Now we need to round up to the nearest MSS */
3709 	len = roundup(len, segsiz);
3710 	if (rack_min_measure_usec) {
3711 		/* Now calculate our min length for this b/w */
3712 		tim = rack_min_measure_usec;
3713 		minl = (tim * bw) / (uint64_t)HPTS_USEC_IN_SEC;
3714 		if (minl == 0)
3715 			minl = 1;
3716 		minl = roundup(minl, segsiz);
3717 		if (len < minl)
3718 			len = minl;
3719 	}
3720 	/*
3721 	 * Now if we have a very small window we want
3722 	 * to attempt to get the window that is
3723 	 * as small as possible. This happens on
3724 	 * low b/w connections and we don't want to
3725 	 * span huge numbers of rtt's between measurements.
3726 	 *
3727 	 * We basically include 2 over our "MIN window" so
3728 	 * that the measurement can be shortened (possibly) by
3729 	 * an ack'ed packet.
3730 	 */
3731 	if (len < def_len)
3732 		return (max((uint32_t)len, ((MIN_GP_WIN+2) * segsiz)));
3733 	else
3734 		return (max((uint32_t)len, def_len));
3735 
3736 }
3737 
3738 static int
3739 rack_enough_for_measurement(struct tcpcb *tp, struct tcp_rack *rack, tcp_seq th_ack, uint8_t *quality)
3740 {
3741 	uint32_t tim, srtts, segsiz;
3742 
3743 	/*
3744 	 * Has enough time passed for the GP measurement to be valid?
3745 	 */
3746 	if (SEQ_LT(th_ack, tp->gput_seq)) {
3747 		/* Not enough bytes yet */
3748 		return (0);
3749 	}
3750 	if ((tp->snd_max == tp->snd_una) ||
3751 	    (th_ack == tp->snd_max)){
3752 		/*
3753 		 * All is acked quality of all acked is
3754 		 * usually low or medium, but we in theory could split
3755 		 * all acked into two cases, where you got
3756 		 * a signifigant amount of your window and
3757 		 * where you did not. For now we leave it
3758 		 * but it is something to contemplate in the
3759 		 * future. The danger here is that delayed ack
3760 		 * is effecting the last byte (which is a 50:50 chance).
3761 		 */
3762 		*quality = RACK_QUALITY_ALLACKED;
3763 		return (1);
3764 	}
3765 	if (SEQ_GEQ(th_ack,  tp->gput_ack)) {
3766 		/*
3767 		 * We obtained our entire window of data we wanted
3768 		 * no matter if we are in recovery or not then
3769 		 * its ok since expanding the window does not
3770 		 * make things fuzzy (or at least not as much).
3771 		 */
3772 		*quality = RACK_QUALITY_HIGH;
3773 		return (1);
3774 	}
3775 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
3776 	if (SEQ_LT(th_ack, tp->gput_ack) &&
3777 	    ((th_ack - tp->gput_seq) < max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) {
3778 		/* Not enough bytes yet */
3779 		return (0);
3780 	}
3781 	if (rack->r_ctl.rc_first_appl &&
3782 	    (SEQ_GEQ(th_ack, rack->r_ctl.rc_first_appl->r_end))) {
3783 		/*
3784 		 * We are up to the app limited send point
3785 		 * we have to measure irrespective of the time..
3786 		 */
3787 		*quality = RACK_QUALITY_APPLIMITED;
3788 		return (1);
3789 	}
3790 	/* Now what about time? */
3791 	srtts = (rack->r_ctl.rc_gp_srtt * rack_min_srtts);
3792 	tim = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - tp->gput_ts;
3793 	if ((tim >= srtts) && (IN_RECOVERY(rack->rc_tp->t_flags) == 0)) {
3794 		/*
3795 		 * We do not allow a measurement if we are in recovery
3796 		 * that would shrink the goodput window we wanted.
3797 		 * This is to prevent cloudyness of when the last send
3798 		 * was actually made.
3799 		 */
3800 		*quality = RACK_QUALITY_HIGH;
3801 		return (1);
3802 	}
3803 	/* Nope not even a full SRTT has passed */
3804 	return (0);
3805 }
3806 
3807 static void
3808 rack_log_timely(struct tcp_rack *rack,
3809 		uint32_t logged, uint64_t cur_bw, uint64_t low_bnd,
3810 		uint64_t up_bnd, int line, uint8_t method)
3811 {
3812 	if (tcp_bblogging_on(rack->rc_tp)) {
3813 		union tcp_log_stackspecific log;
3814 		struct timeval tv;
3815 
3816 		memset(&log, 0, sizeof(log));
3817 		log.u_bbr.flex1 = logged;
3818 		log.u_bbr.flex2 = rack->rc_gp_timely_inc_cnt;
3819 		log.u_bbr.flex2 <<= 4;
3820 		log.u_bbr.flex2 |= rack->rc_gp_timely_dec_cnt;
3821 		log.u_bbr.flex2 <<= 4;
3822 		log.u_bbr.flex2 |= rack->rc_gp_incr;
3823 		log.u_bbr.flex2 <<= 4;
3824 		log.u_bbr.flex2 |= rack->rc_gp_bwred;
3825 		log.u_bbr.flex3 = rack->rc_gp_incr;
3826 		log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss;
3827 		log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ca;
3828 		log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_rec;
3829 		log.u_bbr.flex7 = rack->rc_gp_bwred;
3830 		log.u_bbr.flex8 = method;
3831 		log.u_bbr.cur_del_rate = cur_bw;
3832 		log.u_bbr.delRate = low_bnd;
3833 		log.u_bbr.bw_inuse = up_bnd;
3834 		log.u_bbr.rttProp = rack_get_bw(rack);
3835 		log.u_bbr.pkt_epoch = line;
3836 		log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff;
3837 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3838 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3839 		log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt;
3840 		log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt;
3841 		log.u_bbr.cwnd_gain = rack->rc_dragged_bottom;
3842 		log.u_bbr.cwnd_gain <<= 1;
3843 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_rec;
3844 		log.u_bbr.cwnd_gain <<= 1;
3845 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss;
3846 		log.u_bbr.cwnd_gain <<= 1;
3847 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca;
3848 		log.u_bbr.lost = rack->r_ctl.rc_loss_count;
3849 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3850 		    &rack->rc_inp->inp_socket->so_rcv,
3851 		    &rack->rc_inp->inp_socket->so_snd,
3852 		    TCP_TIMELY_WORK, 0,
3853 		    0, &log, false, &tv);
3854 	}
3855 }
3856 
3857 static int
3858 rack_bw_can_be_raised(struct tcp_rack *rack, uint64_t cur_bw, uint64_t last_bw_est, uint16_t mult)
3859 {
3860 	/*
3861 	 * Before we increase we need to know if
3862 	 * the estimate just made was less than
3863 	 * our pacing goal (i.e. (cur_bw * mult) > last_bw_est)
3864 	 *
3865 	 * If we already are pacing at a fast enough
3866 	 * rate to push us faster there is no sense of
3867 	 * increasing.
3868 	 *
3869 	 * We first caculate our actual pacing rate (ss or ca multiplier
3870 	 * times our cur_bw).
3871 	 *
3872 	 * Then we take the last measured rate and multipy by our
3873 	 * maximum pacing overage to give us a max allowable rate.
3874 	 *
3875 	 * If our act_rate is smaller than our max_allowable rate
3876 	 * then we should increase. Else we should hold steady.
3877 	 *
3878 	 */
3879 	uint64_t act_rate, max_allow_rate;
3880 
3881 	if (rack_timely_no_stopping)
3882 		return (1);
3883 
3884 	if ((cur_bw == 0) || (last_bw_est == 0)) {
3885 		/*
3886 		 * Initial startup case or
3887 		 * everything is acked case.
3888 		 */
3889 		rack_log_timely(rack,  mult, cur_bw, 0, 0,
3890 				__LINE__, 9);
3891 		return (1);
3892 	}
3893 	if (mult <= 100) {
3894 		/*
3895 		 * We can always pace at or slightly above our rate.
3896 		 */
3897 		rack_log_timely(rack,  mult, cur_bw, 0, 0,
3898 				__LINE__, 9);
3899 		return (1);
3900 	}
3901 	act_rate = cur_bw * (uint64_t)mult;
3902 	act_rate /= 100;
3903 	max_allow_rate = last_bw_est * ((uint64_t)rack_max_per_above + (uint64_t)100);
3904 	max_allow_rate /= 100;
3905 	if (act_rate < max_allow_rate) {
3906 		/*
3907 		 * Here the rate we are actually pacing at
3908 		 * is smaller than 10% above our last measurement.
3909 		 * This means we are pacing below what we would
3910 		 * like to try to achieve (plus some wiggle room).
3911 		 */
3912 		rack_log_timely(rack,  mult, cur_bw, act_rate, max_allow_rate,
3913 				__LINE__, 9);
3914 		return (1);
3915 	} else {
3916 		/*
3917 		 * Here we are already pacing at least rack_max_per_above(10%)
3918 		 * what we are getting back. This indicates most likely
3919 		 * that we are being limited (cwnd/rwnd/app) and can't
3920 		 * get any more b/w. There is no sense of trying to
3921 		 * raise up the pacing rate its not speeding us up
3922 		 * and we already are pacing faster than we are getting.
3923 		 */
3924 		rack_log_timely(rack,  mult, cur_bw, act_rate, max_allow_rate,
3925 				__LINE__, 8);
3926 		return (0);
3927 	}
3928 }
3929 
3930 static void
3931 rack_validate_multipliers_at_or_above100(struct tcp_rack *rack)
3932 {
3933 	/*
3934 	 * When we drag bottom, we want to assure
3935 	 * that no multiplier is below 1.0, if so
3936 	 * we want to restore it to at least that.
3937 	 */
3938 	if (rack->r_ctl.rack_per_of_gp_rec  < 100) {
3939 		/* This is unlikely we usually do not touch recovery */
3940 		rack->r_ctl.rack_per_of_gp_rec = 100;
3941 	}
3942 	if (rack->r_ctl.rack_per_of_gp_ca < 100) {
3943 		rack->r_ctl.rack_per_of_gp_ca = 100;
3944 	}
3945 	if (rack->r_ctl.rack_per_of_gp_ss < 100) {
3946 		rack->r_ctl.rack_per_of_gp_ss = 100;
3947 	}
3948 }
3949 
3950 static void
3951 rack_validate_multipliers_at_or_below_100(struct tcp_rack *rack)
3952 {
3953 	if (rack->r_ctl.rack_per_of_gp_ca > 100) {
3954 		rack->r_ctl.rack_per_of_gp_ca = 100;
3955 	}
3956 	if (rack->r_ctl.rack_per_of_gp_ss > 100) {
3957 		rack->r_ctl.rack_per_of_gp_ss = 100;
3958 	}
3959 }
3960 
3961 static void
3962 rack_increase_bw_mul(struct tcp_rack *rack, int timely_says, uint64_t cur_bw, uint64_t last_bw_est, int override)
3963 {
3964 	int32_t  calc, logged, plus;
3965 
3966 	logged = 0;
3967 
3968 	if (rack->rc_skip_timely)
3969 		return;
3970 	if (override) {
3971 		/*
3972 		 * override is passed when we are
3973 		 * loosing b/w and making one last
3974 		 * gasp at trying to not loose out
3975 		 * to a new-reno flow.
3976 		 */
3977 		goto extra_boost;
3978 	}
3979 	/* In classic timely we boost by 5x if we have 5 increases in a row, lets not */
3980 	if (rack->rc_gp_incr &&
3981 	    ((rack->rc_gp_timely_inc_cnt + 1) >= RACK_TIMELY_CNT_BOOST)) {
3982 		/*
3983 		 * Reset and get 5 strokes more before the boost. Note
3984 		 * that the count is 0 based so we have to add one.
3985 		 */
3986 extra_boost:
3987 		plus = (uint32_t)rack_gp_increase_per * RACK_TIMELY_CNT_BOOST;
3988 		rack->rc_gp_timely_inc_cnt = 0;
3989 	} else
3990 		plus = (uint32_t)rack_gp_increase_per;
3991 	/* Must be at least 1% increase for true timely increases */
3992 	if ((plus < 1) &&
3993 	    ((rack->r_ctl.rc_rtt_diff <= 0) || (timely_says <= 0)))
3994 		plus = 1;
3995 	if (rack->rc_gp_saw_rec &&
3996 	    (rack->rc_gp_no_rec_chg == 0) &&
3997 	    rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3998 				  rack->r_ctl.rack_per_of_gp_rec)) {
3999 		/* We have been in recovery ding it too */
4000 		calc = rack->r_ctl.rack_per_of_gp_rec + plus;
4001 		if (calc > 0xffff)
4002 			calc = 0xffff;
4003 		logged |= 1;
4004 		rack->r_ctl.rack_per_of_gp_rec = (uint16_t)calc;
4005 		if (rack->r_ctl.rack_per_upper_bound_ca &&
4006 		    (rack->rc_dragged_bottom == 0) &&
4007 		    (rack->r_ctl.rack_per_of_gp_rec > rack->r_ctl.rack_per_upper_bound_ca))
4008 			rack->r_ctl.rack_per_of_gp_rec = rack->r_ctl.rack_per_upper_bound_ca;
4009 	}
4010 	if (rack->rc_gp_saw_ca &&
4011 	    (rack->rc_gp_saw_ss == 0) &&
4012 	    rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
4013 				  rack->r_ctl.rack_per_of_gp_ca)) {
4014 		/* In CA */
4015 		calc = rack->r_ctl.rack_per_of_gp_ca + plus;
4016 		if (calc > 0xffff)
4017 			calc = 0xffff;
4018 		logged |= 2;
4019 		rack->r_ctl.rack_per_of_gp_ca = (uint16_t)calc;
4020 		if (rack->r_ctl.rack_per_upper_bound_ca &&
4021 		    (rack->rc_dragged_bottom == 0) &&
4022 		    (rack->r_ctl.rack_per_of_gp_ca > rack->r_ctl.rack_per_upper_bound_ca))
4023 			rack->r_ctl.rack_per_of_gp_ca = rack->r_ctl.rack_per_upper_bound_ca;
4024 	}
4025 	if (rack->rc_gp_saw_ss &&
4026 	    rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
4027 				  rack->r_ctl.rack_per_of_gp_ss)) {
4028 		/* In SS */
4029 		calc = rack->r_ctl.rack_per_of_gp_ss + plus;
4030 		if (calc > 0xffff)
4031 			calc = 0xffff;
4032 		rack->r_ctl.rack_per_of_gp_ss = (uint16_t)calc;
4033 		if (rack->r_ctl.rack_per_upper_bound_ss &&
4034 		    (rack->rc_dragged_bottom == 0) &&
4035 		    (rack->r_ctl.rack_per_of_gp_ss > rack->r_ctl.rack_per_upper_bound_ss))
4036 			rack->r_ctl.rack_per_of_gp_ss = rack->r_ctl.rack_per_upper_bound_ss;
4037 		logged |= 4;
4038 	}
4039 	if (logged &&
4040 	    (rack->rc_gp_incr == 0)){
4041 		/* Go into increment mode */
4042 		rack->rc_gp_incr = 1;
4043 		rack->rc_gp_timely_inc_cnt = 0;
4044 	}
4045 	if (rack->rc_gp_incr &&
4046 	    logged &&
4047 	    (rack->rc_gp_timely_inc_cnt < RACK_TIMELY_CNT_BOOST)) {
4048 		rack->rc_gp_timely_inc_cnt++;
4049 	}
4050 	rack_log_timely(rack,  logged, plus, 0, 0,
4051 			__LINE__, 1);
4052 }
4053 
4054 static uint32_t
4055 rack_get_decrease(struct tcp_rack *rack, uint32_t curper, int32_t rtt_diff)
4056 {
4057 	/*-
4058 	 * norm_grad = rtt_diff / minrtt;
4059 	 * new_per = curper * (1 - B * norm_grad)
4060 	 *
4061 	 * B = rack_gp_decrease_per (default 80%)
4062 	 * rtt_dif = input var current rtt-diff
4063 	 * curper = input var current percentage
4064 	 * minrtt = from rack filter
4065 	 *
4066 	 * In order to do the floating point calculations above we
4067 	 * do an integer conversion. The code looks confusing so let me
4068 	 * translate it into something that use more variables and
4069 	 * is clearer for us humans :)
4070 	 *
4071 	 * uint64_t norm_grad, inverse, reduce_by, final_result;
4072 	 * uint32_t perf;
4073 	 *
4074 	 * norm_grad = (((uint64_t)rtt_diff * 1000000) /
4075 	 *             (uint64_t)get_filter_small(&rack->r_ctl.rc_gp_min_rtt));
4076 	 * inverse = ((uint64_t)rack_gp_decrease * (uint64_t)1000000) * norm_grad;
4077 	 * inverse /= 1000000;
4078 	 * reduce_by = (1000000 - inverse);
4079 	 * final_result = (cur_per * reduce_by) / 1000000;
4080 	 * perf = (uint32_t)final_result;
4081 	 */
4082 	uint64_t perf;
4083 
4084 	perf = (((uint64_t)curper * ((uint64_t)1000000 -
4085 		    ((uint64_t)rack_gp_decrease_per * (uint64_t)10000 *
4086 		     (((uint64_t)rtt_diff * (uint64_t)1000000)/
4087 		      (uint64_t)get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt)))/
4088 		     (uint64_t)1000000)) /
4089 		(uint64_t)1000000);
4090 	if (perf > curper) {
4091 		/* TSNH */
4092 		perf = curper - 1;
4093 	}
4094 	return ((uint32_t)perf);
4095 }
4096 
4097 static uint32_t
4098 rack_decrease_highrtt(struct tcp_rack *rack, uint32_t curper, uint32_t rtt)
4099 {
4100 	/*
4101 	 *                                   highrttthresh
4102 	 * result = curper * (1 - (B * ( 1 -  ------          ))
4103 	 *                                     gp_srtt
4104 	 *
4105 	 * B = rack_gp_decrease_per (default .8 i.e. 80)
4106 	 * highrttthresh = filter_min * rack_gp_rtt_maxmul
4107 	 */
4108 	uint64_t perf;
4109 	uint32_t highrttthresh;
4110 
4111 	highrttthresh = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
4112 
4113 	perf = (((uint64_t)curper * ((uint64_t)1000000 -
4114 				     ((uint64_t)rack_gp_decrease_per * ((uint64_t)1000000 -
4115 					((uint64_t)highrttthresh * (uint64_t)1000000) /
4116 						    (uint64_t)rtt)) / 100)) /(uint64_t)1000000);
4117 	if (tcp_bblogging_on(rack->rc_tp)) {
4118 		uint64_t log1;
4119 
4120 		log1 = rtt;
4121 		log1 <<= 32;
4122 		log1 |= highrttthresh;
4123 		rack_log_timely(rack,
4124 				rack_gp_decrease_per,
4125 				(uint64_t)curper,
4126 				log1,
4127 				perf,
4128 				__LINE__,
4129 				15);
4130 	}
4131 	return (perf);
4132 }
4133 
4134 static void
4135 rack_decrease_bw_mul(struct tcp_rack *rack, int timely_says, uint32_t rtt, int32_t rtt_diff)
4136 {
4137 	uint64_t logvar, logvar2, logvar3;
4138 	uint32_t logged, new_per, ss_red, ca_red, rec_red, alt, val;
4139 
4140 	if (rack->rc_skip_timely)
4141 		return;
4142 	if (rack->rc_gp_incr) {
4143 		/* Turn off increment counting */
4144 		rack->rc_gp_incr = 0;
4145 		rack->rc_gp_timely_inc_cnt = 0;
4146 	}
4147 	ss_red = ca_red = rec_red = 0;
4148 	logged = 0;
4149 	/* Calculate the reduction value */
4150 	if (rtt_diff < 0) {
4151 		rtt_diff *= -1;
4152 	}
4153 	/* Must be at least 1% reduction */
4154 	if (rack->rc_gp_saw_rec && (rack->rc_gp_no_rec_chg == 0)) {
4155 		/* We have been in recovery ding it too */
4156 		if (timely_says == 2) {
4157 			new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_rec, rtt);
4158 			alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
4159 			if (alt < new_per)
4160 				val = alt;
4161 			else
4162 				val = new_per;
4163 		} else
4164 			 val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
4165 		if (rack->r_ctl.rack_per_of_gp_rec > val) {
4166 			rec_red = (rack->r_ctl.rack_per_of_gp_rec - val);
4167 			rack->r_ctl.rack_per_of_gp_rec = (uint16_t)val;
4168 		} else {
4169 			rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound;
4170 			rec_red = 0;
4171 		}
4172 		if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_rec)
4173 			rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound;
4174 		logged |= 1;
4175 	}
4176 	if (rack->rc_gp_saw_ss) {
4177 		/* Sent in SS */
4178 		if (timely_says == 2) {
4179 			new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ss, rtt);
4180 			alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ss, rtt_diff);
4181 			if (alt < new_per)
4182 				val = alt;
4183 			else
4184 				val = new_per;
4185 		} else
4186 			val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ss, rtt_diff);
4187 		if (rack->r_ctl.rack_per_of_gp_ss > new_per) {
4188 			ss_red = rack->r_ctl.rack_per_of_gp_ss - val;
4189 			rack->r_ctl.rack_per_of_gp_ss = (uint16_t)val;
4190 		} else {
4191 			ss_red = new_per;
4192 			rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound;
4193 			logvar = new_per;
4194 			logvar <<= 32;
4195 			logvar |= alt;
4196 			logvar2 = (uint32_t)rtt;
4197 			logvar2 <<= 32;
4198 			logvar2 |= (uint32_t)rtt_diff;
4199 			logvar3 = rack_gp_rtt_maxmul;
4200 			logvar3 <<= 32;
4201 			logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
4202 			rack_log_timely(rack, timely_says,
4203 					logvar2, logvar3,
4204 					logvar, __LINE__, 10);
4205 		}
4206 		if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ss)
4207 			rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound;
4208 		logged |= 4;
4209 	} else if (rack->rc_gp_saw_ca) {
4210 		/* Sent in CA */
4211 		if (timely_says == 2) {
4212 			new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ca, rtt);
4213 			alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ca, rtt_diff);
4214 			if (alt < new_per)
4215 				val = alt;
4216 			else
4217 				val = new_per;
4218 		} else
4219 			val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ca, rtt_diff);
4220 		if (rack->r_ctl.rack_per_of_gp_ca > val) {
4221 			ca_red = rack->r_ctl.rack_per_of_gp_ca - val;
4222 			rack->r_ctl.rack_per_of_gp_ca = (uint16_t)val;
4223 		} else {
4224 			rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound;
4225 			ca_red = 0;
4226 			logvar = new_per;
4227 			logvar <<= 32;
4228 			logvar |= alt;
4229 			logvar2 = (uint32_t)rtt;
4230 			logvar2 <<= 32;
4231 			logvar2 |= (uint32_t)rtt_diff;
4232 			logvar3 = rack_gp_rtt_maxmul;
4233 			logvar3 <<= 32;
4234 			logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
4235 			rack_log_timely(rack, timely_says,
4236 					logvar2, logvar3,
4237 					logvar, __LINE__, 10);
4238 		}
4239 		if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ca)
4240 			rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound;
4241 		logged |= 2;
4242 	}
4243 	if (rack->rc_gp_timely_dec_cnt < 0x7) {
4244 		rack->rc_gp_timely_dec_cnt++;
4245 		if (rack_timely_dec_clear &&
4246 		    (rack->rc_gp_timely_dec_cnt == rack_timely_dec_clear))
4247 			rack->rc_gp_timely_dec_cnt = 0;
4248 	}
4249 	logvar = ss_red;
4250 	logvar <<= 32;
4251 	logvar |= ca_red;
4252 	rack_log_timely(rack,  logged, rec_red, rack_per_lower_bound, logvar,
4253 			__LINE__, 2);
4254 }
4255 
4256 static void
4257 rack_log_rtt_shrinks(struct tcp_rack *rack, uint32_t us_cts,
4258 		     uint32_t rtt, uint32_t line, uint8_t reas)
4259 {
4260 	if (tcp_bblogging_on(rack->rc_tp)) {
4261 		union tcp_log_stackspecific log;
4262 		struct timeval tv;
4263 
4264 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
4265 		log.u_bbr.flex1 = line;
4266 		log.u_bbr.flex2 = rack->r_ctl.rc_time_probertt_starts;
4267 		log.u_bbr.flex3 = rack->r_ctl.rc_lower_rtt_us_cts;
4268 		log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss;
4269 		log.u_bbr.flex5 = rtt;
4270 		log.u_bbr.flex6 = rack->rc_highly_buffered;
4271 		log.u_bbr.flex6 <<= 1;
4272 		log.u_bbr.flex6 |= rack->forced_ack;
4273 		log.u_bbr.flex6 <<= 1;
4274 		log.u_bbr.flex6 |= rack->rc_gp_dyn_mul;
4275 		log.u_bbr.flex6 <<= 1;
4276 		log.u_bbr.flex6 |= rack->in_probe_rtt;
4277 		log.u_bbr.flex6 <<= 1;
4278 		log.u_bbr.flex6 |= rack->measure_saw_probe_rtt;
4279 		log.u_bbr.flex7 = rack->r_ctl.rack_per_of_gp_probertt;
4280 		log.u_bbr.pacing_gain = rack->r_ctl.rack_per_of_gp_ca;
4281 		log.u_bbr.cwnd_gain = rack->r_ctl.rack_per_of_gp_rec;
4282 		log.u_bbr.flex8 = reas;
4283 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
4284 		log.u_bbr.delRate = rack_get_bw(rack);
4285 		log.u_bbr.cur_del_rate = rack->r_ctl.rc_highest_us_rtt;
4286 		log.u_bbr.cur_del_rate <<= 32;
4287 		log.u_bbr.cur_del_rate |= rack->r_ctl.rc_lowest_us_rtt;
4288 		log.u_bbr.applimited = rack->r_ctl.rc_time_probertt_entered;
4289 		log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff;
4290 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
4291 		log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt;
4292 		log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt;
4293 		log.u_bbr.pkt_epoch = rack->r_ctl.rc_lower_rtt_us_cts;
4294 		log.u_bbr.delivered = rack->r_ctl.rc_target_probertt_flight;
4295 		log.u_bbr.lost = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
4296 		log.u_bbr.rttProp = us_cts;
4297 		log.u_bbr.rttProp <<= 32;
4298 		log.u_bbr.rttProp |= rack->r_ctl.rc_entry_gp_rtt;
4299 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
4300 		    &rack->rc_inp->inp_socket->so_rcv,
4301 		    &rack->rc_inp->inp_socket->so_snd,
4302 		    BBR_LOG_RTT_SHRINKS, 0,
4303 		    0, &log, false, &rack->r_ctl.act_rcv_time);
4304 	}
4305 }
4306 
4307 static void
4308 rack_set_prtt_target(struct tcp_rack *rack, uint32_t segsiz, uint32_t rtt)
4309 {
4310 	uint64_t bwdp;
4311 
4312 	bwdp = rack_get_bw(rack);
4313 	bwdp *= (uint64_t)rtt;
4314 	bwdp /= (uint64_t)HPTS_USEC_IN_SEC;
4315 	rack->r_ctl.rc_target_probertt_flight = roundup((uint32_t)bwdp, segsiz);
4316 	if (rack->r_ctl.rc_target_probertt_flight < (segsiz * rack_timely_min_segs)) {
4317 		/*
4318 		 * A window protocol must be able to have 4 packets
4319 		 * outstanding as the floor in order to function
4320 		 * (especially considering delayed ack :D).
4321 		 */
4322 		rack->r_ctl.rc_target_probertt_flight = (segsiz * rack_timely_min_segs);
4323 	}
4324 }
4325 
4326 static void
4327 rack_enter_probertt(struct tcp_rack *rack, uint32_t us_cts)
4328 {
4329 	/**
4330 	 * ProbeRTT is a bit different in rack_pacing than in
4331 	 * BBR. It is like BBR in that it uses the lowering of
4332 	 * the RTT as a signal that we saw something new and
4333 	 * counts from there for how long between. But it is
4334 	 * different in that its quite simple. It does not
4335 	 * play with the cwnd and wait until we get down
4336 	 * to N segments outstanding and hold that for
4337 	 * 200ms. Instead it just sets the pacing reduction
4338 	 * rate to a set percentage (70 by default) and hold
4339 	 * that for a number of recent GP Srtt's.
4340 	 */
4341 	uint32_t segsiz;
4342 
4343 	rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
4344 	if (rack->rc_gp_dyn_mul == 0)
4345 		return;
4346 
4347 	if (rack->rc_tp->snd_max == rack->rc_tp->snd_una) {
4348 		/* We are idle */
4349 		return;
4350 	}
4351 	if ((rack->rc_tp->t_flags & TF_GPUTINPROG) &&
4352 	    SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) {
4353 		/*
4354 		 * Stop the goodput now, the idea here is
4355 		 * that future measurements with in_probe_rtt
4356 		 * won't register if they are not greater so
4357 		 * we want to get what info (if any) is available
4358 		 * now.
4359 		 */
4360 		rack_do_goodput_measurement(rack->rc_tp, rack,
4361 					    rack->rc_tp->snd_una, __LINE__,
4362 					    RACK_QUALITY_PROBERTT);
4363 	}
4364 	rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
4365 	rack->r_ctl.rc_time_probertt_entered = us_cts;
4366 	segsiz = min(ctf_fixed_maxseg(rack->rc_tp),
4367 		     rack->r_ctl.rc_pace_min_segs);
4368 	rack->in_probe_rtt = 1;
4369 	rack->measure_saw_probe_rtt = 1;
4370 	rack->r_ctl.rc_time_probertt_starts = 0;
4371 	rack->r_ctl.rc_entry_gp_rtt = rack->r_ctl.rc_gp_srtt;
4372 	if (rack_probertt_use_min_rtt_entry)
4373 		rack_set_prtt_target(rack, segsiz, get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt));
4374 	else
4375 		rack_set_prtt_target(rack, segsiz, rack->r_ctl.rc_gp_srtt);
4376 	rack_log_rtt_shrinks(rack,  us_cts,  get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4377 			     __LINE__, RACK_RTTS_ENTERPROBE);
4378 }
4379 
4380 static void
4381 rack_exit_probertt(struct tcp_rack *rack, uint32_t us_cts)
4382 {
4383 	struct rack_sendmap *rsm;
4384 	uint32_t segsiz;
4385 
4386 	segsiz = min(ctf_fixed_maxseg(rack->rc_tp),
4387 		     rack->r_ctl.rc_pace_min_segs);
4388 	rack->in_probe_rtt = 0;
4389 	if ((rack->rc_tp->t_flags & TF_GPUTINPROG) &&
4390 	    SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) {
4391 		/*
4392 		 * Stop the goodput now, the idea here is
4393 		 * that future measurements with in_probe_rtt
4394 		 * won't register if they are not greater so
4395 		 * we want to get what info (if any) is available
4396 		 * now.
4397 		 */
4398 		rack_do_goodput_measurement(rack->rc_tp, rack,
4399 					    rack->rc_tp->snd_una, __LINE__,
4400 					    RACK_QUALITY_PROBERTT);
4401 	} else if (rack->rc_tp->t_flags & TF_GPUTINPROG) {
4402 		/*
4403 		 * We don't have enough data to make a measurement.
4404 		 * So lets just stop and start here after exiting
4405 		 * probe-rtt. We probably are not interested in
4406 		 * the results anyway.
4407 		 */
4408 		rack->rc_tp->t_flags &= ~TF_GPUTINPROG;
4409 	}
4410 	/*
4411 	 * Measurements through the current snd_max are going
4412 	 * to be limited by the slower pacing rate.
4413 	 *
4414 	 * We need to mark these as app-limited so we
4415 	 * don't collapse the b/w.
4416 	 */
4417 	rsm = tqhash_max(rack->r_ctl.tqh);
4418 	if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) {
4419 		if (rack->r_ctl.rc_app_limited_cnt == 0)
4420 			rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm;
4421 		else {
4422 			/*
4423 			 * Go out to the end app limited and mark
4424 			 * this new one as next and move the end_appl up
4425 			 * to this guy.
4426 			 */
4427 			if (rack->r_ctl.rc_end_appl)
4428 				rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start;
4429 			rack->r_ctl.rc_end_appl = rsm;
4430 		}
4431 		rsm->r_flags |= RACK_APP_LIMITED;
4432 		rack->r_ctl.rc_app_limited_cnt++;
4433 	}
4434 	/*
4435 	 * Now, we need to examine our pacing rate multipliers.
4436 	 * If its under 100%, we need to kick it back up to
4437 	 * 100%. We also don't let it be over our "max" above
4438 	 * the actual rate i.e. 100% + rack_clamp_atexit_prtt.
4439 	 * Note setting clamp_atexit_prtt to 0 has the effect
4440 	 * of setting CA/SS to 100% always at exit (which is
4441 	 * the default behavior).
4442 	 */
4443 	if (rack_probertt_clear_is) {
4444 		rack->rc_gp_incr = 0;
4445 		rack->rc_gp_bwred = 0;
4446 		rack->rc_gp_timely_inc_cnt = 0;
4447 		rack->rc_gp_timely_dec_cnt = 0;
4448 	}
4449 	/* Do we do any clamping at exit? */
4450 	if (rack->rc_highly_buffered && rack_atexit_prtt_hbp) {
4451 		rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt_hbp;
4452 		rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt_hbp;
4453 	}
4454 	if ((rack->rc_highly_buffered == 0) && rack_atexit_prtt) {
4455 		rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt;
4456 		rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt;
4457 	}
4458 	/*
4459 	 * Lets set rtt_diff to 0, so that we will get a "boost"
4460 	 * after exiting.
4461 	 */
4462 	rack->r_ctl.rc_rtt_diff = 0;
4463 
4464 	/* Clear all flags so we start fresh */
4465 	rack->rc_tp->t_bytes_acked = 0;
4466 	rack->rc_tp->t_ccv.flags &= ~CCF_ABC_SENTAWND;
4467 	/*
4468 	 * If configured to, set the cwnd and ssthresh to
4469 	 * our targets.
4470 	 */
4471 	if (rack_probe_rtt_sets_cwnd) {
4472 		uint64_t ebdp;
4473 		uint32_t setto;
4474 
4475 		/* Set ssthresh so we get into CA once we hit our target */
4476 		if (rack_probertt_use_min_rtt_exit == 1) {
4477 			/* Set to min rtt */
4478 			rack_set_prtt_target(rack, segsiz,
4479 					     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt));
4480 		} else if (rack_probertt_use_min_rtt_exit == 2) {
4481 			/* Set to current gp rtt */
4482 			rack_set_prtt_target(rack, segsiz,
4483 					     rack->r_ctl.rc_gp_srtt);
4484 		} else if (rack_probertt_use_min_rtt_exit == 3) {
4485 			/* Set to entry gp rtt */
4486 			rack_set_prtt_target(rack, segsiz,
4487 					     rack->r_ctl.rc_entry_gp_rtt);
4488 		} else {
4489 			uint64_t sum;
4490 			uint32_t setval;
4491 
4492 			sum = rack->r_ctl.rc_entry_gp_rtt;
4493 			sum *= 10;
4494 			sum /= (uint64_t)(max(1, rack->r_ctl.rc_gp_srtt));
4495 			if (sum >= 20) {
4496 				/*
4497 				 * A highly buffered path needs
4498 				 * cwnd space for timely to work.
4499 				 * Lets set things up as if
4500 				 * we are heading back here again.
4501 				 */
4502 				setval = rack->r_ctl.rc_entry_gp_rtt;
4503 			} else if (sum >= 15) {
4504 				/*
4505 				 * Lets take the smaller of the
4506 				 * two since we are just somewhat
4507 				 * buffered.
4508 				 */
4509 				setval = rack->r_ctl.rc_gp_srtt;
4510 				if (setval > rack->r_ctl.rc_entry_gp_rtt)
4511 					setval = rack->r_ctl.rc_entry_gp_rtt;
4512 			} else {
4513 				/*
4514 				 * Here we are not highly buffered
4515 				 * and should pick the min we can to
4516 				 * keep from causing loss.
4517 				 */
4518 				setval = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
4519 			}
4520 			rack_set_prtt_target(rack, segsiz,
4521 					     setval);
4522 		}
4523 		if (rack_probe_rtt_sets_cwnd > 1) {
4524 			/* There is a percentage here to boost */
4525 			ebdp = rack->r_ctl.rc_target_probertt_flight;
4526 			ebdp *= rack_probe_rtt_sets_cwnd;
4527 			ebdp /= 100;
4528 			setto = rack->r_ctl.rc_target_probertt_flight + ebdp;
4529 		} else
4530 			setto = rack->r_ctl.rc_target_probertt_flight;
4531 		rack->rc_tp->snd_cwnd = roundup(setto, segsiz);
4532 		if (rack->rc_tp->snd_cwnd < (segsiz * rack_timely_min_segs)) {
4533 			/* Enforce a min */
4534 			rack->rc_tp->snd_cwnd = segsiz * rack_timely_min_segs;
4535 		}
4536 		/* If we set in the cwnd also set the ssthresh point so we are in CA */
4537 		rack->rc_tp->snd_ssthresh = (rack->rc_tp->snd_cwnd - 1);
4538 	}
4539 	rack_log_rtt_shrinks(rack,  us_cts,
4540 			     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4541 			     __LINE__, RACK_RTTS_EXITPROBE);
4542 	/* Clear times last so log has all the info */
4543 	rack->r_ctl.rc_probertt_sndmax_atexit = rack->rc_tp->snd_max;
4544 	rack->r_ctl.rc_time_probertt_entered = us_cts;
4545 	rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
4546 	rack->r_ctl.rc_time_of_last_probertt = us_cts;
4547 }
4548 
4549 static void
4550 rack_check_probe_rtt(struct tcp_rack *rack, uint32_t us_cts)
4551 {
4552 	/* Check in on probe-rtt */
4553 
4554 	if (rack->rc_gp_filled == 0) {
4555 		/* We do not do p-rtt unless we have gp measurements */
4556 		return;
4557 	}
4558 	if (rack->in_probe_rtt) {
4559 		uint64_t no_overflow;
4560 		uint32_t endtime, must_stay;
4561 
4562 		if (rack->r_ctl.rc_went_idle_time &&
4563 		    ((us_cts - rack->r_ctl.rc_went_idle_time) > rack_min_probertt_hold)) {
4564 			/*
4565 			 * We went idle during prtt, just exit now.
4566 			 */
4567 			rack_exit_probertt(rack, us_cts);
4568 		} else if (rack_probe_rtt_safety_val &&
4569 		    TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered) &&
4570 		    ((us_cts - rack->r_ctl.rc_time_probertt_entered) > rack_probe_rtt_safety_val)) {
4571 			/*
4572 			 * Probe RTT safety value triggered!
4573 			 */
4574 			rack_log_rtt_shrinks(rack,  us_cts,
4575 					     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4576 					     __LINE__, RACK_RTTS_SAFETY);
4577 			rack_exit_probertt(rack, us_cts);
4578 		}
4579 		/* Calculate the max we will wait */
4580 		endtime = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_max_drain_wait);
4581 		if (rack->rc_highly_buffered)
4582 			endtime += (rack->r_ctl.rc_gp_srtt * rack_max_drain_hbp);
4583 		/* Calculate the min we must wait */
4584 		must_stay = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_must_drain);
4585 		if ((ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.rc_target_probertt_flight) &&
4586 		    TSTMP_LT(us_cts, endtime)) {
4587 			uint32_t calc;
4588 			/* Do we lower more? */
4589 no_exit:
4590 			if (TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered))
4591 				calc = us_cts - rack->r_ctl.rc_time_probertt_entered;
4592 			else
4593 				calc = 0;
4594 			calc /= max(rack->r_ctl.rc_gp_srtt, 1);
4595 			if (calc) {
4596 				/* Maybe */
4597 				calc *= rack_per_of_gp_probertt_reduce;
4598 				if (calc > rack_per_of_gp_probertt)
4599 					rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_lowthresh;
4600 				else
4601 					rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt - calc;
4602 				/* Limit it too */
4603 				if (rack->r_ctl.rack_per_of_gp_probertt < rack_per_of_gp_lowthresh)
4604 					rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_lowthresh;
4605 			}
4606 			/* We must reach target or the time set */
4607 			return;
4608 		}
4609 		if (rack->r_ctl.rc_time_probertt_starts == 0) {
4610 			if ((TSTMP_LT(us_cts, must_stay) &&
4611 			     rack->rc_highly_buffered) ||
4612 			     (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) >
4613 			      rack->r_ctl.rc_target_probertt_flight)) {
4614 				/* We are not past the must_stay time */
4615 				goto no_exit;
4616 			}
4617 			rack_log_rtt_shrinks(rack,  us_cts,
4618 					     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4619 					     __LINE__, RACK_RTTS_REACHTARGET);
4620 			rack->r_ctl.rc_time_probertt_starts = us_cts;
4621 			if (rack->r_ctl.rc_time_probertt_starts == 0)
4622 				rack->r_ctl.rc_time_probertt_starts = 1;
4623 			/* Restore back to our rate we want to pace at in prtt */
4624 			rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
4625 		}
4626 		/*
4627 		 * Setup our end time, some number of gp_srtts plus 200ms.
4628 		 */
4629 		no_overflow = ((uint64_t)rack->r_ctl.rc_gp_srtt *
4630 			       (uint64_t)rack_probertt_gpsrtt_cnt_mul);
4631 		if (rack_probertt_gpsrtt_cnt_div)
4632 			endtime = (uint32_t)(no_overflow / (uint64_t)rack_probertt_gpsrtt_cnt_div);
4633 		else
4634 			endtime = 0;
4635 		endtime += rack_min_probertt_hold;
4636 		endtime += rack->r_ctl.rc_time_probertt_starts;
4637 		if (TSTMP_GEQ(us_cts,  endtime)) {
4638 			/* yes, exit probertt */
4639 			rack_exit_probertt(rack, us_cts);
4640 		}
4641 
4642 	} else if ((rack->rc_skip_timely == 0) &&
4643 		   (TSTMP_GT(us_cts, rack->r_ctl.rc_lower_rtt_us_cts)) &&
4644 		   ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= rack_time_between_probertt)) {
4645 		/* Go into probertt, its been too long since we went lower */
4646 		rack_enter_probertt(rack, us_cts);
4647 	}
4648 }
4649 
4650 static void
4651 rack_update_multiplier(struct tcp_rack *rack, int32_t timely_says, uint64_t last_bw_est,
4652 		       uint32_t rtt, int32_t rtt_diff)
4653 {
4654 	uint64_t cur_bw, up_bnd, low_bnd, subfr;
4655 	uint32_t losses;
4656 
4657 	if ((rack->rc_gp_dyn_mul == 0) ||
4658 	    (rack->use_fixed_rate) ||
4659 	    (rack->in_probe_rtt) ||
4660 	    (rack->rc_always_pace == 0)) {
4661 		/* No dynamic GP multiplier in play */
4662 		return;
4663 	}
4664 	losses = rack->r_ctl.rc_loss_count - rack->r_ctl.rc_loss_at_start;
4665 	cur_bw = rack_get_bw(rack);
4666 	/* Calculate our up and down range */
4667 	up_bnd = rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_up;
4668 	up_bnd /= 100;
4669 	up_bnd += rack->r_ctl.last_gp_comp_bw;
4670 
4671 	subfr = (uint64_t)rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_down;
4672 	subfr /= 100;
4673 	low_bnd = rack->r_ctl.last_gp_comp_bw - subfr;
4674 	if ((timely_says == 2) && (rack->r_ctl.rc_no_push_at_mrtt)) {
4675 		/*
4676 		 * This is the case where our RTT is above
4677 		 * the max target and we have been configured
4678 		 * to just do timely no bonus up stuff in that case.
4679 		 *
4680 		 * There are two configurations, set to 1, and we
4681 		 * just do timely if we are over our max. If its
4682 		 * set above 1 then we slam the multipliers down
4683 		 * to 100 and then decrement per timely.
4684 		 */
4685 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4686 				__LINE__, 3);
4687 		if (rack->r_ctl.rc_no_push_at_mrtt > 1)
4688 			rack_validate_multipliers_at_or_below_100(rack);
4689 		rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff);
4690 	} else if ((timely_says != 0) && (last_bw_est < low_bnd) && !losses) {
4691 		/*
4692 		 * We are decreasing this is a bit complicated this
4693 		 * means we are loosing ground. This could be
4694 		 * because another flow entered and we are competing
4695 		 * for b/w with it. This will push the RTT up which
4696 		 * makes timely unusable unless we want to get shoved
4697 		 * into a corner and just be backed off (the age
4698 		 * old problem with delay based CC).
4699 		 *
4700 		 * On the other hand if it was a route change we
4701 		 * would like to stay somewhat contained and not
4702 		 * blow out the buffers.
4703 		 */
4704 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4705 				__LINE__, 3);
4706 		rack->r_ctl.last_gp_comp_bw = cur_bw;
4707 		if (rack->rc_gp_bwred == 0) {
4708 			/* Go into reduction counting */
4709 			rack->rc_gp_bwred = 1;
4710 			rack->rc_gp_timely_dec_cnt = 0;
4711 		}
4712 		if (rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) {
4713 			/*
4714 			 * Push another time with a faster pacing
4715 			 * to try to gain back (we include override to
4716 			 * get a full raise factor).
4717 			 */
4718 			if ((rack->rc_gp_saw_ca && rack->r_ctl.rack_per_of_gp_ca <= rack_down_raise_thresh) ||
4719 			    (rack->rc_gp_saw_ss && rack->r_ctl.rack_per_of_gp_ss <= rack_down_raise_thresh) ||
4720 			    (timely_says == 0) ||
4721 			    (rack_down_raise_thresh == 0)) {
4722 				/*
4723 				 * Do an override up in b/w if we were
4724 				 * below the threshold or if the threshold
4725 				 * is zero we always do the raise.
4726 				 */
4727 				rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 1);
4728 			} else {
4729 				/* Log it stays the same */
4730 				rack_log_timely(rack,  0, last_bw_est, low_bnd, 0,
4731 						__LINE__, 11);
4732 			}
4733 			rack->rc_gp_timely_dec_cnt++;
4734 			/* We are not incrementing really no-count */
4735 			rack->rc_gp_incr = 0;
4736 			rack->rc_gp_timely_inc_cnt = 0;
4737 		} else {
4738 			/*
4739 			 * Lets just use the RTT
4740 			 * information and give up
4741 			 * pushing.
4742 			 */
4743 			goto use_timely;
4744 		}
4745 	} else if ((timely_says != 2) &&
4746 		    !losses &&
4747 		    (last_bw_est > up_bnd)) {
4748 		/*
4749 		 * We are increasing b/w lets keep going, updating
4750 		 * our b/w and ignoring any timely input, unless
4751 		 * of course we are at our max raise (if there is one).
4752 		 */
4753 
4754 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4755 				__LINE__, 3);
4756 		rack->r_ctl.last_gp_comp_bw = cur_bw;
4757 		if (rack->rc_gp_saw_ss &&
4758 		    rack->r_ctl.rack_per_upper_bound_ss &&
4759 		     (rack->r_ctl.rack_per_of_gp_ss == rack->r_ctl.rack_per_upper_bound_ss)) {
4760 			    /*
4761 			     * In cases where we can't go higher
4762 			     * we should just use timely.
4763 			     */
4764 			    goto use_timely;
4765 		}
4766 		if (rack->rc_gp_saw_ca &&
4767 		    rack->r_ctl.rack_per_upper_bound_ca &&
4768 		    (rack->r_ctl.rack_per_of_gp_ca == rack->r_ctl.rack_per_upper_bound_ca)) {
4769 			    /*
4770 			     * In cases where we can't go higher
4771 			     * we should just use timely.
4772 			     */
4773 			    goto use_timely;
4774 		}
4775 		rack->rc_gp_bwred = 0;
4776 		rack->rc_gp_timely_dec_cnt = 0;
4777 		/* You get a set number of pushes if timely is trying to reduce */
4778 		if ((rack->rc_gp_incr < rack_timely_max_push_rise) || (timely_says == 0)) {
4779 			rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4780 		} else {
4781 			/* Log it stays the same */
4782 			rack_log_timely(rack,  0, last_bw_est, up_bnd, 0,
4783 			    __LINE__, 12);
4784 		}
4785 		return;
4786 	} else {
4787 		/*
4788 		 * We are staying between the lower and upper range bounds
4789 		 * so use timely to decide.
4790 		 */
4791 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4792 				__LINE__, 3);
4793 use_timely:
4794 		if (timely_says) {
4795 			rack->rc_gp_incr = 0;
4796 			rack->rc_gp_timely_inc_cnt = 0;
4797 			if ((rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) &&
4798 			    !losses &&
4799 			    (last_bw_est < low_bnd)) {
4800 				/* We are loosing ground */
4801 				rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4802 				rack->rc_gp_timely_dec_cnt++;
4803 				/* We are not incrementing really no-count */
4804 				rack->rc_gp_incr = 0;
4805 				rack->rc_gp_timely_inc_cnt = 0;
4806 			} else
4807 				rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff);
4808 		} else {
4809 			rack->rc_gp_bwred = 0;
4810 			rack->rc_gp_timely_dec_cnt = 0;
4811 			rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4812 		}
4813 	}
4814 }
4815 
4816 static int32_t
4817 rack_make_timely_judgement(struct tcp_rack *rack, uint32_t rtt, int32_t rtt_diff, uint32_t prev_rtt)
4818 {
4819 	int32_t timely_says;
4820 	uint64_t log_mult, log_rtt_a_diff;
4821 
4822 	log_rtt_a_diff = rtt;
4823 	log_rtt_a_diff <<= 32;
4824 	log_rtt_a_diff |= (uint32_t)rtt_diff;
4825 	if (rtt >= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) *
4826 		    rack_gp_rtt_maxmul)) {
4827 		/* Reduce the b/w multiplier */
4828 		timely_says = 2;
4829 		log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
4830 		log_mult <<= 32;
4831 		log_mult |= prev_rtt;
4832 		rack_log_timely(rack,  timely_says, log_mult,
4833 				get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4834 				log_rtt_a_diff, __LINE__, 4);
4835 	} else if (rtt <= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) +
4836 			   ((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) /
4837 			    max(rack_gp_rtt_mindiv , 1)))) {
4838 		/* Increase the b/w multiplier */
4839 		log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) +
4840 			((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) /
4841 			 max(rack_gp_rtt_mindiv , 1));
4842 		log_mult <<= 32;
4843 		log_mult |= prev_rtt;
4844 		timely_says = 0;
4845 		rack_log_timely(rack,  timely_says, log_mult ,
4846 				get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4847 				log_rtt_a_diff, __LINE__, 5);
4848 	} else {
4849 		/*
4850 		 * Use a gradient to find it the timely gradient
4851 		 * is:
4852 		 * grad = rc_rtt_diff / min_rtt;
4853 		 *
4854 		 * anything below or equal to 0 will be
4855 		 * a increase indication. Anything above
4856 		 * zero is a decrease. Note we take care
4857 		 * of the actual gradient calculation
4858 		 * in the reduction (its not needed for
4859 		 * increase).
4860 		 */
4861 		log_mult = prev_rtt;
4862 		if (rtt_diff <= 0) {
4863 			/*
4864 			 * Rttdiff is less than zero, increase the
4865 			 * b/w multiplier (its 0 or negative)
4866 			 */
4867 			timely_says = 0;
4868 			rack_log_timely(rack,  timely_says, log_mult,
4869 					get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 6);
4870 		} else {
4871 			/* Reduce the b/w multiplier */
4872 			timely_says = 1;
4873 			rack_log_timely(rack,  timely_says, log_mult,
4874 					get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 7);
4875 		}
4876 	}
4877 	return (timely_says);
4878 }
4879 
4880 static __inline int
4881 rack_in_gp_window(struct tcpcb *tp, struct rack_sendmap *rsm)
4882 {
4883 	if (SEQ_GEQ(rsm->r_start, tp->gput_seq) &&
4884 	    SEQ_LEQ(rsm->r_end, tp->gput_ack)) {
4885 		/**
4886 		 * This covers the case that the
4887 		 * resent is completely inside
4888 		 * the gp range or up to it.
4889 		 *      |----------------|
4890 		 *      |-----| <or>
4891 		 *            |----|
4892 		 *            <or>   |---|
4893 		 */
4894 		return (1);
4895 	} else if (SEQ_LT(rsm->r_start, tp->gput_seq) &&
4896 		   SEQ_GT(rsm->r_end, tp->gput_seq)){
4897 		/**
4898 		 * This covers the case of
4899 		 *      |--------------|
4900 		 *  |-------->|
4901 		 */
4902 		return (1);
4903 	} else if (SEQ_GEQ(rsm->r_start, tp->gput_seq) &&
4904 		   SEQ_LT(rsm->r_start, tp->gput_ack) &&
4905 		   SEQ_GEQ(rsm->r_end, tp->gput_ack)) {
4906 
4907 		/**
4908 		 * This covers the case of
4909 		 *      |--------------|
4910 		 *              |-------->|
4911 		 */
4912 		return (1);
4913 	}
4914 	return (0);
4915 }
4916 
4917 static __inline void
4918 rack_mark_in_gp_win(struct tcpcb *tp, struct rack_sendmap *rsm)
4919 {
4920 
4921 	if ((tp->t_flags & TF_GPUTINPROG) == 0)
4922 		return;
4923 	/*
4924 	 * We have a Goodput measurement in progress. Mark
4925 	 * the send if its within the window. If its not
4926 	 * in the window make sure it does not have the mark.
4927 	 */
4928 	if (rack_in_gp_window(tp, rsm))
4929 		rsm->r_flags |= RACK_IN_GP_WIN;
4930 	else
4931 		rsm->r_flags &= ~RACK_IN_GP_WIN;
4932 }
4933 
4934 static __inline void
4935 rack_clear_gp_marks(struct tcpcb *tp, struct tcp_rack *rack)
4936 {
4937 	/* A GP measurement is ending, clear all marks on the send map*/
4938 	struct rack_sendmap *rsm = NULL;
4939 
4940 	rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
4941 	if (rsm == NULL) {
4942 		rsm = tqhash_min(rack->r_ctl.tqh);
4943 	}
4944 	/* Nothing left? */
4945 	while ((rsm != NULL) && (SEQ_GEQ(tp->gput_ack, rsm->r_start))){
4946 		rsm->r_flags &= ~RACK_IN_GP_WIN;
4947 		rsm = tqhash_next(rack->r_ctl.tqh, rsm);
4948 	}
4949 }
4950 
4951 
4952 static __inline void
4953 rack_tend_gp_marks(struct tcpcb *tp, struct tcp_rack *rack)
4954 {
4955 	struct rack_sendmap *rsm = NULL;
4956 
4957 	if (tp->snd_una == tp->snd_max) {
4958 		/* Nothing outstanding yet, nothing to do here */
4959 		return;
4960 	}
4961 	if (SEQ_GT(tp->gput_seq, tp->snd_una)) {
4962 		/*
4963 		 * We are measuring ahead of some outstanding
4964 		 * data. We need to walk through up until we get
4965 		 * to gp_seq marking so that no rsm is set incorrectly
4966 		 * with RACK_IN_GP_WIN.
4967 		 */
4968 		rsm = tqhash_min(rack->r_ctl.tqh);
4969 		while (rsm != NULL) {
4970 			rack_mark_in_gp_win(tp, rsm);
4971 			if (SEQ_GEQ(rsm->r_end, tp->gput_seq))
4972 				break;
4973 			rsm = tqhash_next(rack->r_ctl.tqh, rsm);
4974 		}
4975 	}
4976 	if (rsm == NULL) {
4977 		/*
4978 		 * Need to find the GP seq, if rsm is
4979 		 * set we stopped as we hit it.
4980 		 */
4981 		rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
4982 		if (rsm == NULL)
4983 			return;
4984 		rack_mark_in_gp_win(tp, rsm);
4985 	}
4986 	/*
4987 	 * Now we may need to mark already sent rsm, ahead of
4988 	 * gput_seq in the window since they may have been sent
4989 	 * *before* we started our measurment. The rsm, if non-null
4990 	 * has been marked (note if rsm would have been NULL we would have
4991 	 * returned in the previous block). So we go to the next, and continue
4992 	 * until we run out of entries or we exceed the gp_ack value.
4993 	 */
4994 	rsm = tqhash_next(rack->r_ctl.tqh, rsm);
4995 	while (rsm) {
4996 		rack_mark_in_gp_win(tp, rsm);
4997 		if (SEQ_GT(rsm->r_end, tp->gput_ack))
4998 			break;
4999 		rsm = tqhash_next(rack->r_ctl.tqh, rsm);
5000 	}
5001 }
5002 
5003 static void
5004 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)
5005 {
5006 	if (tcp_bblogging_on(rack->rc_tp)) {
5007 		union tcp_log_stackspecific log;
5008 		struct timeval tv;
5009 
5010 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5011 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5012 		log.u_bbr.flex1 = add_part;
5013 		log.u_bbr.flex2 = sub_part;
5014 		log.u_bbr.flex3 = rack_wma_divisor;
5015 		log.u_bbr.flex4 = srtt;
5016 		log.u_bbr.flex7 = (uint16_t)line;
5017 		log.u_bbr.flex8 = meth;
5018 		log.u_bbr.delRate = rack->r_ctl.gp_bw;
5019 		log.u_bbr.cur_del_rate = meas_bw;
5020 		log.u_bbr.rttProp = utim;
5021 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
5022 		    &rack->rc_inp->inp_socket->so_rcv,
5023 		    &rack->rc_inp->inp_socket->so_snd,
5024 		    BBR_LOG_THRESH_CALC, 0,
5025 		    0, &log, false, &rack->r_ctl.act_rcv_time);
5026 	}
5027 }
5028 
5029 static void
5030 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
5031 			    tcp_seq th_ack, int line, uint8_t quality)
5032 {
5033 	uint64_t tim, bytes_ps, stim, utim;
5034 	uint32_t segsiz, bytes, reqbytes, us_cts;
5035 	int32_t gput, new_rtt_diff, timely_says;
5036 	uint64_t  resid_bw, subpart = 0, addpart = 0, srtt;
5037 	int did_add = 0;
5038 
5039 	us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
5040 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
5041 	if (TSTMP_GEQ(us_cts, tp->gput_ts))
5042 		tim = us_cts - tp->gput_ts;
5043 	else
5044 		tim = 0;
5045 	if (rack->r_ctl.rc_gp_cumack_ts > rack->r_ctl.rc_gp_output_ts)
5046 		stim = rack->r_ctl.rc_gp_cumack_ts - rack->r_ctl.rc_gp_output_ts;
5047 	else
5048 		stim = 0;
5049 	/*
5050 	 * Use the larger of the send time or ack time. This prevents us
5051 	 * from being influenced by ack artifacts to come up with too
5052 	 * high of measurement. Note that since we are spanning over many more
5053 	 * bytes in most of our measurements hopefully that is less likely to
5054 	 * occur.
5055 	 */
5056 	if (tim > stim)
5057 		utim = max(tim, 1);
5058 	else
5059 		utim = max(stim, 1);
5060 	reqbytes = min(rc_init_window(rack), (MIN_GP_WIN * segsiz));
5061 	rack_log_gpset(rack, th_ack, us_cts, rack->r_ctl.rc_gp_cumack_ts, __LINE__, 3, NULL);
5062 	if ((tim == 0) && (stim == 0)) {
5063 		/*
5064 		 * Invalid measurement time, maybe
5065 		 * all on one ack/one send?
5066 		 */
5067 		bytes = 0;
5068 		bytes_ps = 0;
5069 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
5070 					   0, 0, 0, 10, __LINE__, NULL, quality);
5071 		goto skip_measurement;
5072 	}
5073 	if (rack->r_ctl.rc_gp_lowrtt == 0xffffffff) {
5074 		/* We never made a us_rtt measurement? */
5075 		bytes = 0;
5076 		bytes_ps = 0;
5077 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
5078 					   0, 0, 0, 10, __LINE__, NULL, quality);
5079 		goto skip_measurement;
5080 	}
5081 	/*
5082 	 * Calculate the maximum possible b/w this connection
5083 	 * could have. We base our calculation on the lowest
5084 	 * rtt we have seen during the measurement and the
5085 	 * largest rwnd the client has given us in that time. This
5086 	 * forms a BDP that is the maximum that we could ever
5087 	 * get to the client. Anything larger is not valid.
5088 	 *
5089 	 * I originally had code here that rejected measurements
5090 	 * where the time was less than 1/2 the latest us_rtt.
5091 	 * But after thinking on that I realized its wrong since
5092 	 * say you had a 150Mbps or even 1Gbps link, and you
5093 	 * were a long way away.. example I am in Europe (100ms rtt)
5094 	 * talking to my 1Gbps link in S.C. Now measuring say 150,000
5095 	 * bytes my time would be 1.2ms, and yet my rtt would say
5096 	 * the measurement was invalid the time was < 50ms. The
5097 	 * same thing is true for 150Mb (8ms of time).
5098 	 *
5099 	 * A better way I realized is to look at what the maximum
5100 	 * the connection could possibly do. This is gated on
5101 	 * the lowest RTT we have seen and the highest rwnd.
5102 	 * We should in theory never exceed that, if we are
5103 	 * then something on the path is storing up packets
5104 	 * and then feeding them all at once to our endpoint
5105 	 * messing up our measurement.
5106 	 */
5107 	rack->r_ctl.last_max_bw = rack->r_ctl.rc_gp_high_rwnd;
5108 	rack->r_ctl.last_max_bw *= HPTS_USEC_IN_SEC;
5109 	rack->r_ctl.last_max_bw /= rack->r_ctl.rc_gp_lowrtt;
5110 	if (SEQ_LT(th_ack, tp->gput_seq)) {
5111 		/* No measurement can be made */
5112 		bytes = 0;
5113 		bytes_ps = 0;
5114 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
5115 					   0, 0, 0, 10, __LINE__, NULL, quality);
5116 		goto skip_measurement;
5117 	} else
5118 		bytes = (th_ack - tp->gput_seq);
5119 	bytes_ps = (uint64_t)bytes;
5120 	/*
5121 	 * Don't measure a b/w for pacing unless we have gotten at least
5122 	 * an initial windows worth of data in this measurement interval.
5123 	 *
5124 	 * Small numbers of bytes get badly influenced by delayed ack and
5125 	 * other artifacts. Note we take the initial window or our
5126 	 * defined minimum GP (defaulting to 10 which hopefully is the
5127 	 * IW).
5128 	 */
5129 	if (rack->rc_gp_filled == 0) {
5130 		/*
5131 		 * The initial estimate is special. We
5132 		 * have blasted out an IW worth of packets
5133 		 * without a real valid ack ts results. We
5134 		 * then setup the app_limited_needs_set flag,
5135 		 * this should get the first ack in (probably 2
5136 		 * MSS worth) to be recorded as the timestamp.
5137 		 * We thus allow a smaller number of bytes i.e.
5138 		 * IW - 2MSS.
5139 		 */
5140 		reqbytes -= (2 * segsiz);
5141 		/* Also lets fill previous for our first measurement to be neutral */
5142 		rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt;
5143 	}
5144 	if ((bytes_ps < reqbytes) || rack->app_limited_needs_set) {
5145 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
5146 					   rack->r_ctl.rc_app_limited_cnt,
5147 					   0, 0, 10, __LINE__, NULL, quality);
5148 		goto skip_measurement;
5149 	}
5150 	/*
5151 	 * We now need to calculate the Timely like status so
5152 	 * we can update (possibly) the b/w multipliers.
5153 	 */
5154 	new_rtt_diff = (int32_t)rack->r_ctl.rc_gp_srtt - (int32_t)rack->r_ctl.rc_prev_gp_srtt;
5155 	if (rack->rc_gp_filled == 0) {
5156 		/* No previous reading */
5157 		rack->r_ctl.rc_rtt_diff = new_rtt_diff;
5158 	} else {
5159 		if (rack->measure_saw_probe_rtt == 0) {
5160 			/*
5161 			 * We don't want a probertt to be counted
5162 			 * since it will be negative incorrectly. We
5163 			 * expect to be reducing the RTT when we
5164 			 * pace at a slower rate.
5165 			 */
5166 			rack->r_ctl.rc_rtt_diff -= (rack->r_ctl.rc_rtt_diff / 8);
5167 			rack->r_ctl.rc_rtt_diff += (new_rtt_diff / 8);
5168 		}
5169 	}
5170 	timely_says = rack_make_timely_judgement(rack,
5171 	    rack->r_ctl.rc_gp_srtt,
5172 	    rack->r_ctl.rc_rtt_diff,
5173 	    rack->r_ctl.rc_prev_gp_srtt
5174 	);
5175 	bytes_ps *= HPTS_USEC_IN_SEC;
5176 	bytes_ps /= utim;
5177 	if (bytes_ps > rack->r_ctl.last_max_bw) {
5178 		/*
5179 		 * Something is on path playing
5180 		 * since this b/w is not possible based
5181 		 * on our BDP (highest rwnd and lowest rtt
5182 		 * we saw in the measurement window).
5183 		 *
5184 		 * Another option here would be to
5185 		 * instead skip the measurement.
5186 		 */
5187 		rack_log_pacing_delay_calc(rack, bytes, reqbytes,
5188 					   bytes_ps, rack->r_ctl.last_max_bw, 0,
5189 					   11, __LINE__, NULL, quality);
5190 		bytes_ps = rack->r_ctl.last_max_bw;
5191 	}
5192 	/* We store gp for b/w in bytes per second */
5193 	if (rack->rc_gp_filled == 0) {
5194 		/* Initial measurement */
5195 		if (bytes_ps) {
5196 			rack->r_ctl.gp_bw = bytes_ps;
5197 			rack->rc_gp_filled = 1;
5198 			rack->r_ctl.num_measurements = 1;
5199 			rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
5200 		} else {
5201 			rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
5202 						   rack->r_ctl.rc_app_limited_cnt,
5203 						   0, 0, 10, __LINE__, NULL, quality);
5204 		}
5205 		if (tcp_in_hpts(rack->rc_tp) &&
5206 		    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
5207 			/*
5208 			 * Ok we can't trust the pacer in this case
5209 			 * where we transition from un-paced to paced.
5210 			 * Or for that matter when the burst mitigation
5211 			 * was making a wild guess and got it wrong.
5212 			 * Stop the pacer and clear up all the aggregate
5213 			 * delays etc.
5214 			 */
5215 			tcp_hpts_remove(rack->rc_tp);
5216 			rack->r_ctl.rc_hpts_flags = 0;
5217 			rack->r_ctl.rc_last_output_to = 0;
5218 		}
5219 		did_add = 2;
5220 	} else if (rack->r_ctl.num_measurements < RACK_REQ_AVG) {
5221 		/* Still a small number run an average */
5222 		rack->r_ctl.gp_bw += bytes_ps;
5223 		addpart = rack->r_ctl.num_measurements;
5224 		rack->r_ctl.num_measurements++;
5225 		if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) {
5226 			/* We have collected enough to move forward */
5227 			rack->r_ctl.gp_bw /= (uint64_t)rack->r_ctl.num_measurements;
5228 		}
5229 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
5230 		did_add = 3;
5231 	} else {
5232 		/*
5233 		 * We want to take 1/wma of the goodput and add in to 7/8th
5234 		 * of the old value weighted by the srtt. So if your measurement
5235 		 * period is say 2 SRTT's long you would get 1/4 as the
5236 		 * value, if it was like 1/2 SRTT then you would get 1/16th.
5237 		 *
5238 		 * But we must be careful not to take too much i.e. if the
5239 		 * srtt is say 20ms and the measurement is taken over
5240 		 * 400ms our weight would be 400/20 i.e. 20. On the
5241 		 * other hand if we get a measurement over 1ms with a
5242 		 * 10ms rtt we only want to take a much smaller portion.
5243 		 */
5244 		uint8_t meth;
5245 
5246 		if (rack->r_ctl.num_measurements < 0xff) {
5247 			rack->r_ctl.num_measurements++;
5248 		}
5249 		srtt = (uint64_t)tp->t_srtt;
5250 		if (srtt == 0) {
5251 			/*
5252 			 * Strange why did t_srtt go back to zero?
5253 			 */
5254 			if (rack->r_ctl.rc_rack_min_rtt)
5255 				srtt = rack->r_ctl.rc_rack_min_rtt;
5256 			else
5257 				srtt = HPTS_USEC_IN_MSEC;
5258 		}
5259 		/*
5260 		 * XXXrrs: Note for reviewers, in playing with
5261 		 * dynamic pacing I discovered this GP calculation
5262 		 * as done originally leads to some undesired results.
5263 		 * Basically you can get longer measurements contributing
5264 		 * too much to the WMA. Thus I changed it if you are doing
5265 		 * dynamic adjustments to only do the aportioned adjustment
5266 		 * if we have a very small (time wise) measurement. Longer
5267 		 * measurements just get there weight (defaulting to 1/8)
5268 		 * add to the WMA. We may want to think about changing
5269 		 * this to always do that for both sides i.e. dynamic
5270 		 * and non-dynamic... but considering lots of folks
5271 		 * were playing with this I did not want to change the
5272 		 * calculation per.se. without your thoughts.. Lawerence?
5273 		 * Peter??
5274 		 */
5275 		if (rack->rc_gp_dyn_mul == 0) {
5276 			subpart = rack->r_ctl.gp_bw * utim;
5277 			subpart /= (srtt * 8);
5278 			if (subpart < (rack->r_ctl.gp_bw / 2)) {
5279 				/*
5280 				 * The b/w update takes no more
5281 				 * away then 1/2 our running total
5282 				 * so factor it in.
5283 				 */
5284 				addpart = bytes_ps * utim;
5285 				addpart /= (srtt * 8);
5286 				meth = 1;
5287 			} else {
5288 				/*
5289 				 * Don't allow a single measurement
5290 				 * to account for more than 1/2 of the
5291 				 * WMA. This could happen on a retransmission
5292 				 * where utim becomes huge compared to
5293 				 * srtt (multiple retransmissions when using
5294 				 * the sending rate which factors in all the
5295 				 * transmissions from the first one).
5296 				 */
5297 				subpart = rack->r_ctl.gp_bw / 2;
5298 				addpart = bytes_ps / 2;
5299 				meth = 2;
5300 			}
5301 			rack_log_gp_calc(rack, addpart, subpart, srtt, bytes_ps, utim, meth, __LINE__);
5302 			resid_bw = rack->r_ctl.gp_bw - subpart;
5303 			rack->r_ctl.gp_bw = resid_bw + addpart;
5304 			did_add = 1;
5305 		} else {
5306 			if ((utim / srtt) <= 1) {
5307 				/*
5308 				 * The b/w update was over a small period
5309 				 * of time. The idea here is to prevent a small
5310 				 * measurement time period from counting
5311 				 * too much. So we scale it based on the
5312 				 * time so it attributes less than 1/rack_wma_divisor
5313 				 * of its measurement.
5314 				 */
5315 				subpart = rack->r_ctl.gp_bw * utim;
5316 				subpart /= (srtt * rack_wma_divisor);
5317 				addpart = bytes_ps * utim;
5318 				addpart /= (srtt * rack_wma_divisor);
5319 				meth = 3;
5320 			} else {
5321 				/*
5322 				 * The scaled measurement was long
5323 				 * enough so lets just add in the
5324 				 * portion of the measurement i.e. 1/rack_wma_divisor
5325 				 */
5326 				subpart = rack->r_ctl.gp_bw / rack_wma_divisor;
5327 				addpart = bytes_ps / rack_wma_divisor;
5328 				meth = 4;
5329 			}
5330 			if ((rack->measure_saw_probe_rtt == 0) ||
5331 		            (bytes_ps > rack->r_ctl.gp_bw)) {
5332 				/*
5333 				 * For probe-rtt we only add it in
5334 				 * if its larger, all others we just
5335 				 * add in.
5336 				 */
5337 				did_add = 1;
5338 				rack_log_gp_calc(rack, addpart, subpart, srtt, bytes_ps, utim, meth, __LINE__);
5339 				resid_bw = rack->r_ctl.gp_bw - subpart;
5340 				rack->r_ctl.gp_bw = resid_bw + addpart;
5341 			}
5342 		}
5343 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
5344 	}
5345 	/*
5346 	 * We only watch the growth of the GP during the initial startup
5347 	 * or first-slowstart that ensues. If we ever needed to watch
5348 	 * growth of gp outside of that period all we need to do is
5349 	 * remove the first clause of this if (rc_initial_ss_comp).
5350 	 */
5351 	if ((rack->rc_initial_ss_comp == 0) &&
5352 	    (rack->r_ctl.num_measurements >= RACK_REQ_AVG)) {
5353 		uint64_t gp_est;
5354 
5355 		gp_est = bytes_ps;
5356 		if (tcp_bblogging_on(rack->rc_tp)) {
5357 			union tcp_log_stackspecific log;
5358 			struct timeval tv;
5359 
5360 			memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5361 			log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5362 			log.u_bbr.flex1 = rack->r_ctl.current_round;
5363 			log.u_bbr.flex2 = rack->r_ctl.last_rnd_of_gp_rise;
5364 			log.u_bbr.delRate = gp_est;
5365 			log.u_bbr.cur_del_rate = rack->r_ctl.last_gpest;
5366 			log.u_bbr.flex8 = 41;
5367 			(void)tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
5368 					    0, &log, false, NULL, __func__, __LINE__,&tv);
5369 		}
5370 		if ((rack->r_ctl.num_measurements == RACK_REQ_AVG) ||
5371 		    (rack->r_ctl.last_gpest == 0)) {
5372 			/*
5373 			 * The round we get our measurement averaging going
5374 			 * is the base round so it always is the source point
5375 			 * for when we had our first increment. From there on
5376 			 * we only record the round that had a rise.
5377 			 */
5378 			rack->r_ctl.last_rnd_of_gp_rise = rack->r_ctl.current_round;
5379 			rack->r_ctl.last_gpest = rack->r_ctl.gp_bw;
5380 		} else if (gp_est >= rack->r_ctl.last_gpest) {
5381 			/*
5382 			 * Test to see if its gone up enough
5383 			 * to set the round count up to now. Note
5384 			 * that on the seeding of the 4th measurement we
5385 			 */
5386 			gp_est *= 1000;
5387 			gp_est /= rack->r_ctl.last_gpest;
5388 			if ((uint32_t)gp_est > rack->r_ctl.gp_gain_req) {
5389 				/*
5390 				 * We went up enough to record the round.
5391 				 */
5392 				if (tcp_bblogging_on(rack->rc_tp)) {
5393 					union tcp_log_stackspecific log;
5394 					struct timeval tv;
5395 
5396 					memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5397 					log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5398 					log.u_bbr.flex1 = rack->r_ctl.current_round;
5399 					log.u_bbr.flex2 = (uint32_t)gp_est;
5400 					log.u_bbr.flex3 = rack->r_ctl.gp_gain_req;
5401 					log.u_bbr.delRate = gp_est;
5402 					log.u_bbr.cur_del_rate = rack->r_ctl.last_gpest;
5403 					log.u_bbr.flex8 = 42;
5404 					(void)tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
5405 							    0, &log, false, NULL, __func__, __LINE__,&tv);
5406 				}
5407 				rack->r_ctl.last_rnd_of_gp_rise = rack->r_ctl.current_round;
5408 				if (rack->r_ctl.use_gp_not_last == 1)
5409 					rack->r_ctl.last_gpest = rack->r_ctl.gp_bw;
5410 				else
5411 					rack->r_ctl.last_gpest = bytes_ps;
5412 			}
5413 		}
5414 	}
5415 	if ((rack->gp_ready == 0) &&
5416 	    (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) {
5417 		/* We have enough measurements now */
5418 		rack->gp_ready = 1;
5419 		if (rack->dgp_on ||
5420 		    rack->rack_hibeta)
5421 			rack_set_cc_pacing(rack);
5422 		if (rack->defer_options)
5423 			rack_apply_deferred_options(rack);
5424 	}
5425 	rack_log_pacing_delay_calc(rack, subpart, addpart, bytes_ps, stim,
5426 				   rack_get_bw(rack), 22, did_add, NULL, quality);
5427 	/* We do not update any multipliers if we are in or have seen a probe-rtt */
5428 
5429 	if ((rack->measure_saw_probe_rtt == 0) &&
5430 	    rack->rc_gp_rtt_set) {
5431 		if (rack->rc_skip_timely == 0) {
5432 			rack_update_multiplier(rack, timely_says, bytes_ps,
5433 					       rack->r_ctl.rc_gp_srtt,
5434 					       rack->r_ctl.rc_rtt_diff);
5435 		}
5436 	}
5437 	rack_log_pacing_delay_calc(rack, bytes, tim, bytes_ps, stim,
5438 				   rack_get_bw(rack), 3, line, NULL, quality);
5439 	rack_log_pacing_delay_calc(rack,
5440 				   bytes, /* flex2 */
5441 				   tim, /* flex1 */
5442 				   bytes_ps, /* bw_inuse */
5443 				   rack->r_ctl.gp_bw, /* delRate */
5444 				   rack_get_lt_bw(rack), /* rttProp */
5445 				   20, line, NULL, 0);
5446 	/* reset the gp srtt and setup the new prev */
5447 	rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt;
5448 	/* Record the lost count for the next measurement */
5449 	rack->r_ctl.rc_loss_at_start = rack->r_ctl.rc_loss_count;
5450 skip_measurement:
5451 	/*
5452 	 * We restart our diffs based on the gpsrtt in the
5453 	 * measurement window.
5454 	 */
5455 	rack->rc_gp_rtt_set = 0;
5456 	rack->rc_gp_saw_rec = 0;
5457 	rack->rc_gp_saw_ca = 0;
5458 	rack->rc_gp_saw_ss = 0;
5459 	rack->rc_dragged_bottom = 0;
5460 	if (quality == RACK_QUALITY_HIGH) {
5461 		/*
5462 		 * Gput in the stats world is in kbps where bytes_ps is
5463 		 * bytes per second so we do ((x * 8)/ 1000).
5464 		 */
5465 		gput = (int32_t)((bytes_ps << 3) / (uint64_t)1000);
5466 #ifdef STATS
5467 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
5468 					 gput);
5469 		/*
5470 		 * XXXLAS: This is a temporary hack, and should be
5471 		 * chained off VOI_TCP_GPUT when stats(9) grows an
5472 		 * API to deal with chained VOIs.
5473 		 */
5474 		if (tp->t_stats_gput_prev > 0)
5475 			stats_voi_update_abs_s32(tp->t_stats,
5476 						 VOI_TCP_GPUT_ND,
5477 						 ((gput - tp->t_stats_gput_prev) * 100) /
5478 						 tp->t_stats_gput_prev);
5479 #endif
5480 		tp->t_stats_gput_prev = gput;
5481 	}
5482 	tp->t_flags &= ~TF_GPUTINPROG;
5483 	/*
5484 	 * Now are we app limited now and there is space from where we
5485 	 * were to where we want to go?
5486 	 *
5487 	 * We don't do the other case i.e. non-applimited here since
5488 	 * the next send will trigger us picking up the missing data.
5489 	 */
5490 	if (rack->r_ctl.rc_first_appl &&
5491 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
5492 	    rack->r_ctl.rc_app_limited_cnt &&
5493 	    (SEQ_GT(rack->r_ctl.rc_first_appl->r_start, th_ack)) &&
5494 	    ((rack->r_ctl.rc_first_appl->r_end - th_ack) >
5495 	     max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) {
5496 		/*
5497 		 * Yep there is enough outstanding to make a measurement here.
5498 		 */
5499 		struct rack_sendmap *rsm;
5500 
5501 		rack->r_ctl.rc_gp_lowrtt = 0xffffffff;
5502 		rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
5503 		tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
5504 		rack->app_limited_needs_set = 0;
5505 		tp->gput_seq = th_ack;
5506 		if (rack->in_probe_rtt)
5507 			rack->measure_saw_probe_rtt = 1;
5508 		else if ((rack->measure_saw_probe_rtt) &&
5509 			 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
5510 			rack->measure_saw_probe_rtt = 0;
5511 		if ((rack->r_ctl.rc_first_appl->r_end - th_ack) >= rack_get_measure_window(tp, rack)) {
5512 			/* There is a full window to gain info from */
5513 			tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
5514 		} else {
5515 			/* We can only measure up to the applimited point */
5516 			tp->gput_ack = tp->gput_seq + (rack->r_ctl.rc_first_appl->r_end - th_ack);
5517 			if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) {
5518 				/*
5519 				 * We don't have enough to make a measurement.
5520 				 */
5521 				tp->t_flags &= ~TF_GPUTINPROG;
5522 				rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq,
5523 							   0, 0, 0, 6, __LINE__, NULL, quality);
5524 				return;
5525 			}
5526 		}
5527 		if (tp->t_state >= TCPS_FIN_WAIT_1) {
5528 			/*
5529 			 * We will get no more data into the SB
5530 			 * this means we need to have the data available
5531 			 * before we start a measurement.
5532 			 */
5533 			if (sbavail(&tptosocket(tp)->so_snd) < (tp->gput_ack - tp->gput_seq)) {
5534 				/* Nope not enough data. */
5535 				return;
5536 			}
5537 		}
5538 		tp->t_flags |= TF_GPUTINPROG;
5539 		/*
5540 		 * Now we need to find the timestamp of the send at tp->gput_seq
5541 		 * for the send based measurement.
5542 		 */
5543 		rack->r_ctl.rc_gp_cumack_ts = 0;
5544 		rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
5545 		if (rsm) {
5546 			/* Ok send-based limit is set */
5547 			if (SEQ_LT(rsm->r_start, tp->gput_seq)) {
5548 				/*
5549 				 * Move back to include the earlier part
5550 				 * so our ack time lines up right (this may
5551 				 * make an overlapping measurement but thats
5552 				 * ok).
5553 				 */
5554 				tp->gput_seq = rsm->r_start;
5555 			}
5556 			if (rsm->r_flags & RACK_ACKED) {
5557 				struct rack_sendmap *nrsm;
5558 
5559 				tp->gput_ts = (uint32_t)rsm->r_ack_arrival;
5560 				tp->gput_seq = rsm->r_end;
5561 				nrsm = tqhash_next(rack->r_ctl.tqh, rsm);
5562 				if (nrsm)
5563 					rsm = nrsm;
5564 				else {
5565 					rack->app_limited_needs_set = 1;
5566 				}
5567 			} else
5568 				rack->app_limited_needs_set = 1;
5569 			/* We always go from the first send */
5570 			rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[0];
5571 		} else {
5572 			/*
5573 			 * If we don't find the rsm due to some
5574 			 * send-limit set the current time, which
5575 			 * basically disables the send-limit.
5576 			 */
5577 			struct timeval tv;
5578 
5579 			microuptime(&tv);
5580 			rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
5581 		}
5582 		rack_tend_gp_marks(tp, rack);
5583 		rack_log_pacing_delay_calc(rack,
5584 					   tp->gput_seq,
5585 					   tp->gput_ack,
5586 					   (uint64_t)rsm,
5587 					   tp->gput_ts,
5588 					   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
5589 					   9,
5590 					   __LINE__, rsm, quality);
5591 		rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
5592 	} else {
5593 		/*
5594 		 * To make sure proper timestamp merging occurs, we need to clear
5595 		 * all GP marks if we don't start a measurement.
5596 		 */
5597 		rack_clear_gp_marks(tp, rack);
5598 	}
5599 }
5600 
5601 /*
5602  * CC wrapper hook functions
5603  */
5604 static void
5605 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack, uint32_t th_ack, uint16_t nsegs,
5606     uint16_t type, int32_t post_recovery)
5607 {
5608 	uint32_t prior_cwnd, acked;
5609 	struct tcp_log_buffer *lgb = NULL;
5610 	uint8_t labc_to_use, quality;
5611 
5612 	INP_WLOCK_ASSERT(tptoinpcb(tp));
5613 	tp->t_ccv.nsegs = nsegs;
5614 	acked = tp->t_ccv.bytes_this_ack = (th_ack - tp->snd_una);
5615 	if ((post_recovery) && (rack->r_ctl.rc_early_recovery_segs)) {
5616 		uint32_t max;
5617 
5618 		max = rack->r_ctl.rc_early_recovery_segs * ctf_fixed_maxseg(tp);
5619 		if (tp->t_ccv.bytes_this_ack > max) {
5620 			tp->t_ccv.bytes_this_ack = max;
5621 		}
5622 	}
5623 #ifdef STATS
5624 	stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF,
5625 	    ((int32_t)rack->r_ctl.cwnd_to_use) - tp->snd_wnd);
5626 #endif
5627 	if ((th_ack == tp->snd_max) && rack->lt_bw_up) {
5628 		/*
5629 		 * We will ack all the data, time to end any
5630 		 * lt_bw_up we have running until something
5631 		 * new is sent. Note we need to use the actual
5632 		 * ack_rcv_time which with pacing may be different.
5633 		 */
5634 		uint64_t tmark;
5635 
5636 		rack->r_ctl.lt_bw_bytes += (tp->snd_max - rack->r_ctl.lt_seq);
5637 		rack->r_ctl.lt_seq = tp->snd_max;
5638 		tmark = tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time);
5639 		if (tmark >= rack->r_ctl.lt_timemark) {
5640 			rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark);
5641 		}
5642 		rack->r_ctl.lt_timemark = tmark;
5643 		rack->lt_bw_up = 0;
5644 	}
5645 	quality = RACK_QUALITY_NONE;
5646 	if ((tp->t_flags & TF_GPUTINPROG) &&
5647 	    rack_enough_for_measurement(tp, rack, th_ack, &quality)) {
5648 		/* Measure the Goodput */
5649 		rack_do_goodput_measurement(tp, rack, th_ack, __LINE__, quality);
5650 	}
5651 	/* Which way our we limited, if not cwnd limited no advance in CA */
5652 	if (tp->snd_cwnd <= tp->snd_wnd)
5653 		tp->t_ccv.flags |= CCF_CWND_LIMITED;
5654 	else
5655 		tp->t_ccv.flags &= ~CCF_CWND_LIMITED;
5656 	if (tp->snd_cwnd > tp->snd_ssthresh) {
5657 		tp->t_bytes_acked += min(tp->t_ccv.bytes_this_ack,
5658 			 nsegs * V_tcp_abc_l_var * ctf_fixed_maxseg(tp));
5659 		/* For the setting of a window past use the actual scwnd we are using */
5660 		if (tp->t_bytes_acked >= rack->r_ctl.cwnd_to_use) {
5661 			tp->t_bytes_acked -= rack->r_ctl.cwnd_to_use;
5662 			tp->t_ccv.flags |= CCF_ABC_SENTAWND;
5663 		}
5664 	} else {
5665 		tp->t_ccv.flags &= ~CCF_ABC_SENTAWND;
5666 		tp->t_bytes_acked = 0;
5667 	}
5668 	prior_cwnd = tp->snd_cwnd;
5669 	if ((post_recovery == 0) || (rack_max_abc_post_recovery == 0) || rack->r_use_labc_for_rec ||
5670 	    (rack_client_low_buf && rack->client_bufferlvl &&
5671 	    (rack->client_bufferlvl < rack_client_low_buf)))
5672 		labc_to_use = rack->rc_labc;
5673 	else
5674 		labc_to_use = rack_max_abc_post_recovery;
5675 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
5676 		union tcp_log_stackspecific log;
5677 		struct timeval tv;
5678 
5679 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5680 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5681 		log.u_bbr.flex1 = th_ack;
5682 		log.u_bbr.flex2 = tp->t_ccv.flags;
5683 		log.u_bbr.flex3 = tp->t_ccv.bytes_this_ack;
5684 		log.u_bbr.flex4 = tp->t_ccv.nsegs;
5685 		log.u_bbr.flex5 = labc_to_use;
5686 		log.u_bbr.flex6 = prior_cwnd;
5687 		log.u_bbr.flex7 = V_tcp_do_newsack;
5688 		log.u_bbr.flex8 = 1;
5689 		lgb = tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
5690 				     0, &log, false, NULL, __func__, __LINE__,&tv);
5691 	}
5692 	if (CC_ALGO(tp)->ack_received != NULL) {
5693 		/* XXXLAS: Find a way to live without this */
5694 		tp->t_ccv.curack = th_ack;
5695 		tp->t_ccv.labc = labc_to_use;
5696 		tp->t_ccv.flags |= CCF_USE_LOCAL_ABC;
5697 		CC_ALGO(tp)->ack_received(&tp->t_ccv, type);
5698 	}
5699 	if (lgb) {
5700 		lgb->tlb_stackinfo.u_bbr.flex6 = tp->snd_cwnd;
5701 	}
5702 	if (rack->r_must_retran) {
5703 		if (SEQ_GEQ(th_ack, rack->r_ctl.rc_snd_max_at_rto)) {
5704 			/*
5705 			 * We now are beyond the rxt point so lets disable
5706 			 * the flag.
5707 			 */
5708 			rack->r_ctl.rc_out_at_rto = 0;
5709 			rack->r_must_retran = 0;
5710 		} else if ((prior_cwnd + ctf_fixed_maxseg(tp)) <= tp->snd_cwnd) {
5711 			/*
5712 			 * Only decrement the rc_out_at_rto if the cwnd advances
5713 			 * at least a whole segment. Otherwise next time the peer
5714 			 * acks, we won't be able to send this generaly happens
5715 			 * when we are in Congestion Avoidance.
5716 			 */
5717 			if (acked <= rack->r_ctl.rc_out_at_rto){
5718 				rack->r_ctl.rc_out_at_rto -= acked;
5719 			} else {
5720 				rack->r_ctl.rc_out_at_rto = 0;
5721 			}
5722 		}
5723 	}
5724 #ifdef STATS
5725 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, rack->r_ctl.cwnd_to_use);
5726 #endif
5727 	if (rack->r_ctl.rc_rack_largest_cwnd < rack->r_ctl.cwnd_to_use) {
5728 		rack->r_ctl.rc_rack_largest_cwnd = rack->r_ctl.cwnd_to_use;
5729 	}
5730 	if ((rack->rc_initial_ss_comp == 0) &&
5731 	    (tp->snd_cwnd >= tp->snd_ssthresh)) {
5732 		/*
5733 		 * The cwnd has grown beyond ssthresh we have
5734 		 * entered ca and completed our first Slowstart.
5735 		 */
5736 		rack->rc_initial_ss_comp = 1;
5737 	}
5738 }
5739 
5740 static void
5741 tcp_rack_partialack(struct tcpcb *tp)
5742 {
5743 	struct tcp_rack *rack;
5744 
5745 	rack = (struct tcp_rack *)tp->t_fb_ptr;
5746 	INP_WLOCK_ASSERT(tptoinpcb(tp));
5747 	/*
5748 	 * If we are doing PRR and have enough
5749 	 * room to send <or> we are pacing and prr
5750 	 * is disabled we will want to see if we
5751 	 * can send data (by setting r_wanted_output to
5752 	 * true).
5753 	 */
5754 	if ((rack->r_ctl.rc_prr_sndcnt > 0) ||
5755 	    rack->rack_no_prr)
5756 		rack->r_wanted_output = 1;
5757 }
5758 
5759 static inline uint64_t
5760 rack_get_rxt_per(uint64_t snds,  uint64_t rxts)
5761 {
5762 	uint64_t rxt_per;
5763 
5764 	if (snds > 0) {
5765 		rxt_per = rxts * 1000;
5766 		rxt_per /= snds;
5767 	} else {
5768 		/* This is an unlikely path */
5769 		if (rxts) {
5770 			/* Its the max it was all re-transmits */
5771 			rxt_per = 0xffffffffffffffff;
5772 		} else {
5773 			rxt_per = 0;
5774 		}
5775 	}
5776 	return (rxt_per);
5777 }
5778 
5779 static void
5780 policer_detection_log(struct tcp_rack *rack, uint32_t flex1, uint32_t flex2, uint32_t flex3, uint32_t flex4, uint8_t flex8)
5781 {
5782 	if (tcp_bblogging_on(rack->rc_tp)) {
5783 		union tcp_log_stackspecific log;
5784 		struct timeval tv;
5785 
5786 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5787 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5788 		log.u_bbr.flex1 = flex1;
5789 		log.u_bbr.flex2 = flex2;
5790 		log.u_bbr.flex3 = flex3;
5791 		log.u_bbr.flex4 = flex4;
5792 		log.u_bbr.flex5 = rack->r_ctl.current_policer_bucket;
5793 		log.u_bbr.flex6 = rack->r_ctl.policer_bucket_size;
5794 		log.u_bbr.flex7 = 0;
5795 		log.u_bbr.flex8 = flex8;
5796 		log.u_bbr.bw_inuse = rack->r_ctl.policer_bw;
5797 		log.u_bbr.applimited = rack->r_ctl.current_round;
5798 		log.u_bbr.epoch = rack->r_ctl.policer_max_seg;
5799 		log.u_bbr.delivered = (uint32_t)rack->r_ctl.bytes_acked_in_recovery;
5800 		log.u_bbr.cur_del_rate = rack->rc_tp->t_sndbytes;
5801 		log.u_bbr.delRate = rack->rc_tp->t_snd_rxt_bytes;
5802 		log.u_bbr.rttProp = rack->r_ctl.gp_bw;
5803 		log.u_bbr.bbr_state = rack->rc_policer_detected;
5804 		log.u_bbr.bbr_substate = 0;
5805 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
5806 		log.u_bbr.use_lt_bw = rack->policer_detect_on;
5807 		log.u_bbr.lt_epoch = 0;
5808 		log.u_bbr.pkts_out = 0;
5809 		tcp_log_event(rack->rc_tp, NULL, NULL, NULL, TCP_POLICER_DET, 0,
5810 			      0, &log, false, NULL, NULL, 0, &tv);
5811 	}
5812 
5813 }
5814 
5815 static void
5816 policer_detection(struct tcpcb *tp, struct tcp_rack *rack, int post_recovery)
5817 {
5818 	/*
5819 	 * Rack excess rxt accounting is turned on. If we
5820 	 * are above a threshold of rxt's in at least N
5821 	 * rounds, then back off the cwnd and ssthresh
5822 	 * to fit into the long-term b/w.
5823 	 */
5824 
5825 	uint32_t pkts, mid, med, alt_med, avg, segsiz, tot_retran_pkt_count = 0;
5826 	uint32_t cnt_of_mape_rxt = 0;
5827 	uint64_t snds, rxts, rxt_per, tim, del, del_bw;
5828 	int i;
5829 	struct timeval tv;
5830 
5831 
5832 	/*
5833 	 * First is there enough packets delivered during recovery to make
5834 	 * a determiniation of b/w?
5835 	 */
5836 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
5837 	if ((rack->rc_policer_detected == 0) &&
5838 	    (rack->r_ctl.policer_del_mss > 0) &&
5839 	    ((uint32_t)rack->r_ctl.policer_del_mss > ((rack->r_ctl.bytes_acked_in_recovery + segsiz - 1)/segsiz))) {
5840 		/*
5841 		 * Not enough data sent in recovery for initial detection. Once
5842 		 * we have deteced a policer we allow less than the threshold (polcer_del_mss)
5843 		 * amount of data in a recovery to let us fall through and double check
5844 		 * our policer settings and possibly expand or collapse the bucket size and
5845 		 * the polcier b/w.
5846 		 *
5847 		 * Once you are declared to be policed. this block of code cannot be
5848 		 * reached, instead blocks further down will re-check the policer detection
5849 		 * triggers and possibly reset the measurements if somehow we have let the
5850 		 * policer bucket size grow too large.
5851 		 */
5852 		if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
5853 			policer_detection_log(rack, rack->r_ctl.policer_del_mss,
5854 					      ((rack->r_ctl.bytes_acked_in_recovery + segsiz - 1)/segsiz),
5855 					      rack->r_ctl.bytes_acked_in_recovery, segsiz, 18);
5856 		}
5857 		return;
5858 	}
5859 	tcp_get_usecs(&tv);
5860 	tim = tcp_tv_to_lusectick(&tv) - rack->r_ctl.time_entered_recovery;
5861 	del = rack->r_ctl.bytes_acked_in_recovery;
5862 	if (tim > 0)
5863 		del_bw = (del * (uint64_t)1000000) / tim;
5864 	else
5865 		del_bw = 0;
5866 	/* B/W compensation? */
5867 
5868 	if (rack->r_ctl.pol_bw_comp && ((rack->r_ctl.policer_bw > 0) ||
5869 					(del_bw > 0))) {
5870 		/*
5871 		 * Sanity check now that the data is in. How long does it
5872 		 * take for us to pace out two of our policer_max_seg's?
5873 		 *
5874 		 * If it is longer than the RTT then we are set
5875 		 * too slow, maybe because of not enough data
5876 		 * sent during recovery.
5877 		 */
5878 		uint64_t lentime, res, srtt, max_delbw, alt_bw;
5879 
5880 		srtt = (uint64_t)rack_grab_rtt(tp, rack);
5881 		if ((tp->t_srtt > 0) && (srtt > tp->t_srtt))
5882 			srtt = tp->t_srtt;
5883 		lentime = rack->r_ctl.policer_max_seg * (uint64_t)HPTS_USEC_IN_SEC * 2;
5884 		if (del_bw > rack->r_ctl.policer_bw) {
5885 			max_delbw = del_bw;
5886 		} else {
5887 			max_delbw = rack->r_ctl.policer_bw;
5888 		}
5889 		res = lentime / max_delbw;
5890 		if ((srtt > 0) && (res > srtt)) {
5891 			/*
5892 			 * At this rate we can not get two policer_maxsegs
5893 			 * out before the ack arrives back.
5894 			 *
5895 			 * Lets at least get it raised up so that
5896 			 * we can be a bit faster than that if possible.
5897 			 */
5898 			lentime = (rack->r_ctl.policer_max_seg * 2);
5899 			tim = srtt;
5900 			alt_bw = (lentime * (uint64_t)HPTS_USEC_IN_SEC) / tim;
5901 			if (alt_bw > max_delbw) {
5902 				uint64_t cap_alt_bw;
5903 
5904 				cap_alt_bw = (max_delbw + (max_delbw * rack->r_ctl.pol_bw_comp));
5905 				if ((rack_pol_min_bw > 0) && (cap_alt_bw < rack_pol_min_bw)) {
5906 					/* We place a min on the cap which defaults to 1Mbps */
5907 					cap_alt_bw = rack_pol_min_bw;
5908 				}
5909 				if (alt_bw <= cap_alt_bw) {
5910 					/* It should be */
5911 					del_bw = alt_bw;
5912 					policer_detection_log(rack,
5913 							      (uint32_t)tim,
5914 							      rack->r_ctl.policer_max_seg,
5915 							      0,
5916 							      0,
5917 							      16);
5918 				} else {
5919 					/*
5920 					 * This is an odd case where likely the RTT is very very
5921 					 * low. And yet it is still being policed. We don't want
5922 					 * to get more than (rack_policing_do_bw_comp+1) x del-rate
5923 					 * where del-rate is what we got in recovery for either the
5924 					 * first Policer Detection(PD) or this PD we are on now.
5925 					 */
5926 					del_bw = cap_alt_bw;
5927 					policer_detection_log(rack,
5928 							      (uint32_t)tim,
5929 							      rack->r_ctl.policer_max_seg,
5930 							      (uint32_t)max_delbw,
5931 							      (rack->r_ctl.pol_bw_comp + 1),
5932 							      16);
5933 				}
5934 			}
5935 		}
5936 	}
5937 	snds = tp->t_sndbytes - rack->r_ctl.last_policer_sndbytes;
5938 	rxts = tp->t_snd_rxt_bytes - rack->r_ctl.last_policer_snd_rxt_bytes;
5939 	rxt_per = rack_get_rxt_per(snds,  rxts);
5940 	/* Figure up the average  and median */
5941 	for(i = 0; i < RETRAN_CNT_SIZE; i++) {
5942 		if (rack->r_ctl.rc_cnt_of_retran[i] > 0) {
5943 			tot_retran_pkt_count += (i + 1) * rack->r_ctl.rc_cnt_of_retran[i];
5944 			cnt_of_mape_rxt  += rack->r_ctl.rc_cnt_of_retran[i];
5945 		}
5946 	}
5947 	if (cnt_of_mape_rxt)
5948 		avg = (tot_retran_pkt_count * 10)/cnt_of_mape_rxt;
5949 	else
5950 		avg = 0;
5951 	alt_med = med = 0;
5952 	mid = tot_retran_pkt_count/2;
5953 	for(i = 0; i < RETRAN_CNT_SIZE; i++) {
5954 		pkts = (i + 1) * rack->r_ctl.rc_cnt_of_retran[i];
5955 		if (mid > pkts) {
5956 			mid -= pkts;
5957 			continue;
5958 		}
5959 		med = (i + 1);
5960 		break;
5961 	}
5962 	mid = cnt_of_mape_rxt / 2;
5963 	for(i = 0; i < RETRAN_CNT_SIZE; i++) {
5964 		if (mid > rack->r_ctl.rc_cnt_of_retran[i]) {
5965 			mid -= rack->r_ctl.rc_cnt_of_retran[i];
5966 			continue;
5967 		}
5968 		alt_med = (i + 1);
5969 		break;
5970 	}
5971 	if (rack->r_ctl.policer_alt_median) {
5972 		/* Swap the medians */
5973 		uint32_t swap;
5974 
5975 		swap = med;
5976 		med = alt_med;
5977 		alt_med = swap;
5978 	}
5979 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
5980 		union tcp_log_stackspecific log;
5981 		struct timeval tv;
5982 
5983 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5984 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5985 		log.u_bbr.flex1 = avg;
5986 		log.u_bbr.flex2 = med;
5987 		log.u_bbr.flex3 = (uint32_t)rxt_per;
5988 		log.u_bbr.flex4 = rack->r_ctl.policer_avg_threshold;
5989 		log.u_bbr.flex5 = rack->r_ctl.policer_med_threshold;
5990 		log.u_bbr.flex6 = rack->r_ctl.policer_rxt_threshold;
5991 		log.u_bbr.flex7 = rack->r_ctl.policer_alt_median;
5992 		log.u_bbr.flex8 = 1;
5993 		log.u_bbr.delivered = rack->r_ctl.policer_bucket_size;
5994 		log.u_bbr.applimited = rack->r_ctl.current_round;
5995 		log.u_bbr.epoch = rack->r_ctl.policer_max_seg;
5996 		log.u_bbr.bw_inuse = del_bw;
5997 		log.u_bbr.cur_del_rate = rxts;
5998 		log.u_bbr.delRate = snds;
5999 		log.u_bbr.rttProp = rack->r_ctl.gp_bw;
6000 		log.u_bbr.bbr_state = rack->rc_policer_detected;
6001 		log.u_bbr.bbr_substate = 0;
6002 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
6003 		log.u_bbr.use_lt_bw = rack->policer_detect_on;
6004 		log.u_bbr.lt_epoch = (uint32_t)tim;
6005 		log.u_bbr.pkts_out = rack->r_ctl.bytes_acked_in_recovery;
6006 		tcp_log_event(tp, NULL, NULL, NULL, TCP_POLICER_DET, 0,
6007 			      0, &log, false, NULL, NULL, 0, &tv);
6008 	}
6009 	if (med == RETRAN_CNT_SIZE) {
6010 		/*
6011 		 * If the median is the maximum, then what we
6012 		 * likely have here is a network breakage. Either that
6013 		 * or we are so unlucky that all of our traffic is being
6014 		 * dropped and having to be retransmitted the maximum times
6015 		 * and this just is not how a policer works.
6016 		 *
6017 		 * If it is truely a policer eventually we will come
6018 		 * through and it won't be the maximum.
6019 		 */
6020 		return;
6021 	}
6022 	/* Has enough rounds progressed for us to re-measure? */
6023 	if ((rxt_per >= (uint64_t)rack->r_ctl.policer_rxt_threshold) &&
6024 	    (avg >= rack->r_ctl.policer_avg_threshold) &&
6025 	    (med >= rack->r_ctl.policer_med_threshold)) {
6026 		/*
6027 		 * We hit all thresholds that indicate we are
6028 		 * being policed. Now we may be doing this from a rack timeout
6029 		 * which then means the rest of recovery will hopefully go
6030 		 * smoother as we pace. At the end of recovery we will
6031 		 * fall back in here and reset the values using the
6032 		 * results of the entire recovery episode (we could also
6033 		 * hit this as we exit recovery as well which means only
6034 		 * one time in here).
6035 		 *
6036 		 * This is done explicitly that if we hit the thresholds
6037 		 * again in a second recovery we overwrite the values. We do
6038 		 * that because over time, as we pace the policer_bucket_size may
6039 		 * continue to grow. This then provides more and more times when
6040 		 * we are not pacing to the policer rate. This lets us compensate
6041 		 * for when we hit a false positive and those flows continue to
6042 		 * increase. However if its a real policer we will then get over its
6043 		 * limit, over time, again and thus end up back here hitting the
6044 		 * thresholds again.
6045 		 *
6046 		 * The alternative to this is to instead whenever we pace due to
6047 		 * policing in rack_policed_sending we could add the amount len paced to the
6048 		 * idle_snd_una value (which decreases the amount in last_amount_before_rec
6049 		 * since that is always [th_ack - idle_snd_una]). This would then prevent
6050 		 * the polcier_bucket_size from growing in additional recovery episodes
6051 		 * Which would then mean false  postives would be pretty much stuck
6052 		 * after things got back to normal (assuming that what caused the
6053 		 * false positive was a small network outage).
6054 		 *
6055 		 */
6056 		tcp_trace_point(rack->rc_tp, TCP_TP_POLICER_DET);
6057 		if (rack->rc_policer_detected == 0) {
6058 			/*
6059 			 * Increment the stat that tells us we identified
6060 			 * a policer only once. Note that if we ever allow
6061 			 * the flag to be cleared (reverted) then we need
6062 			 * to adjust this to not do multi-counting.
6063 			 */
6064 			counter_u64_add(tcp_policer_detected, 1);
6065 		}
6066 		rack->r_ctl.last_policer_sndbytes = tp->t_sndbytes;
6067 		rack->r_ctl.last_policer_snd_rxt_bytes = tp->t_snd_rxt_bytes;
6068 		rack->r_ctl.policer_bw = del_bw;
6069 		rack->r_ctl.policer_max_seg = tcp_get_pacing_burst_size_w_divisor(rack->rc_tp,
6070 										  rack->r_ctl.policer_bw,
6071 										  min(ctf_fixed_maxseg(rack->rc_tp),
6072 										      rack->r_ctl.rc_pace_min_segs),
6073 										  0, NULL,
6074 										  NULL, rack->r_ctl.pace_len_divisor);
6075 		/* Now what about the policer bucket size */
6076 		rack->r_ctl.policer_bucket_size = rack->r_ctl.last_amount_before_rec;
6077 		if (rack->r_ctl.policer_bucket_size < rack->r_ctl.policer_max_seg) {
6078 			/* We must be able to send our max-seg or else chaos ensues */
6079 			rack->r_ctl.policer_bucket_size = rack->r_ctl.policer_max_seg * 2;
6080 		}
6081 		if (rack->rc_policer_detected == 0)
6082 			rack->r_ctl.current_policer_bucket = 0;
6083 		if (tcp_bblogging_on(rack->rc_tp)) {
6084 			union tcp_log_stackspecific log;
6085 			struct timeval tv;
6086 
6087 			memset(&log.u_bbr, 0, sizeof(log.u_bbr));
6088 			log.u_bbr.timeStamp = tcp_get_usecs(&tv);
6089 			log.u_bbr.flex1 = avg;
6090 			log.u_bbr.flex2 = med;
6091 			log.u_bbr.flex3 = rxt_per;
6092 			log.u_bbr.flex4 = rack->r_ctl.policer_avg_threshold;
6093 			log.u_bbr.flex5 = rack->r_ctl.policer_med_threshold;
6094 			log.u_bbr.flex6 = rack->r_ctl.policer_rxt_threshold;
6095 			log.u_bbr.flex7 = rack->r_ctl.policer_alt_median;
6096 			log.u_bbr.flex8 = 2;
6097 			log.u_bbr.applimited = rack->r_ctl.current_round;
6098 			log.u_bbr.bw_inuse = del_bw;
6099 			log.u_bbr.delivered = rack->r_ctl.policer_bucket_size;
6100 			log.u_bbr.cur_del_rate = rxts;
6101 			log.u_bbr.delRate = snds;
6102 			log.u_bbr.rttProp = rack->r_ctl.gp_bw;
6103 			log.u_bbr.bbr_state = rack->rc_policer_detected;
6104 			log.u_bbr.bbr_substate = 0;
6105 			log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
6106 			log.u_bbr.use_lt_bw = rack->policer_detect_on;
6107 			log.u_bbr.epoch = rack->r_ctl.policer_max_seg;
6108 			log.u_bbr.lt_epoch = (uint32_t)tim;
6109 			log.u_bbr.pkts_out = rack->r_ctl.bytes_acked_in_recovery;
6110 			tcp_log_event(tp, NULL, NULL, NULL, TCP_POLICER_DET, 0,
6111 				      0, &log, false, NULL, NULL, 0, &tv);
6112 			/*
6113 			 * Put out an added log, 19, for the sole purpose
6114 			 * of getting the txt/rxt so that we can benchmark
6115 			 * in read-bbrlog the ongoing rxt rate after our
6116 			 * policer invocation in the HYSTART announcments.
6117 			 */
6118 			memset(&log.u_bbr, 0, sizeof(log.u_bbr));
6119 			log.u_bbr.timeStamp = tcp_tv_to_usectick(&tv);
6120 			log.u_bbr.flex1 = alt_med;
6121 			log.u_bbr.flex8 = 19;
6122 			log.u_bbr.cur_del_rate = tp->t_sndbytes;
6123 			log.u_bbr.delRate = tp->t_snd_rxt_bytes;
6124 			tcp_log_event(tp, NULL, NULL, NULL, TCP_POLICER_DET, 0,
6125 				      0, &log, false, NULL, NULL, 0, &tv);
6126 		}
6127 		/* Turn off any fast output, thats ended */
6128 		rack->r_fast_output = 0;
6129 		/* Mark the time for credits */
6130 		rack->r_ctl.last_sendtime = tcp_get_u64_usecs(NULL);
6131 		if (rack->r_rr_config < 2) {
6132 			/*
6133 			 * We need to be stricter on the RR config so
6134 			 * the pacing has priority.
6135 			 */
6136 			rack->r_rr_config = 2;
6137 		}
6138 		policer_detection_log(rack,
6139 				      rack->r_ctl.idle_snd_una,
6140 				      rack->r_ctl.ack_for_idle,
6141 				      0,
6142 				      (uint32_t)tim,
6143 				      14);
6144 		rack->rc_policer_detected = 1;
6145 	} else if ((rack->rc_policer_detected == 1) &&
6146 		   (post_recovery == 1)) {
6147 		/*
6148 		 * If we are exiting recovery and have already detected
6149 		 * we need to possibly update the values.
6150 		 *
6151 		 * First: Update the idle -> recovery sent value.
6152 		 */
6153 		uint32_t srtt;
6154 
6155 		if (rack->r_ctl.last_amount_before_rec > rack->r_ctl.policer_bucket_size) {
6156 			rack->r_ctl.policer_bucket_size = rack->r_ctl.last_amount_before_rec;
6157 		}
6158 		srtt = (uint64_t)rack_grab_rtt(tp, rack);
6159 		if ((tp->t_srtt > 0) && (srtt > tp->t_srtt))
6160 			srtt = tp->t_srtt;
6161 		if ((srtt != 0) &&
6162 		    (tim < (uint64_t)srtt)) {
6163 			/*
6164 			 * Not long enough.
6165 			 */
6166 			if (rack_verbose_logging)
6167 				policer_detection_log(rack,
6168 						      (uint32_t)tim,
6169 						      0,
6170 						      0,
6171 						      0,
6172 						      15);
6173 			return;
6174 		}
6175 		/*
6176 		 * Finally update the b/w if its grown.
6177 		 */
6178 		if (del_bw > rack->r_ctl.policer_bw) {
6179 			rack->r_ctl.policer_bw = del_bw;
6180 			rack->r_ctl.policer_max_seg = tcp_get_pacing_burst_size_w_divisor(rack->rc_tp,
6181 											  rack->r_ctl.policer_bw,
6182 											  min(ctf_fixed_maxseg(rack->rc_tp),
6183 											      rack->r_ctl.rc_pace_min_segs),
6184 											  0, NULL,
6185 											  NULL, rack->r_ctl.pace_len_divisor);
6186 			if (rack->r_ctl.policer_bucket_size < rack->r_ctl.policer_max_seg) {
6187 				/* We must be able to send our max-seg or else chaos ensues */
6188 				rack->r_ctl.policer_bucket_size = rack->r_ctl.policer_max_seg * 2;
6189 			}
6190 		}
6191 		policer_detection_log(rack,
6192 				      rack->r_ctl.idle_snd_una,
6193 				      rack->r_ctl.ack_for_idle,
6194 				      0,
6195 				      (uint32_t)tim,
6196 				      3);
6197 	}
6198 }
6199 
6200 static void
6201 rack_exit_recovery(struct tcpcb *tp, struct tcp_rack *rack, int how)
6202 {
6203 	/* now check with the policer if on */
6204 	if (rack->policer_detect_on == 1) {
6205 		policer_detection(tp, rack, 1);
6206 	}
6207 	/*
6208 	 * Now exit recovery, note we must do the idle set after the policer_detection
6209 	 * to get the amount acked prior to recovery correct.
6210 	 */
6211 	rack->r_ctl.idle_snd_una = tp->snd_una;
6212 	EXIT_RECOVERY(tp->t_flags);
6213 }
6214 
6215 static void
6216 rack_post_recovery(struct tcpcb *tp, uint32_t th_ack)
6217 {
6218 	struct tcp_rack *rack;
6219 	uint32_t orig_cwnd;
6220 
6221 	orig_cwnd = tp->snd_cwnd;
6222 	INP_WLOCK_ASSERT(tptoinpcb(tp));
6223 	rack = (struct tcp_rack *)tp->t_fb_ptr;
6224 	/* only alert CC if we alerted when we entered */
6225 	if (CC_ALGO(tp)->post_recovery != NULL) {
6226 		tp->t_ccv.curack = th_ack;
6227 		CC_ALGO(tp)->post_recovery(&tp->t_ccv);
6228 		if (tp->snd_cwnd < tp->snd_ssthresh) {
6229 			/*
6230 			 * Rack has burst control and pacing
6231 			 * so lets not set this any lower than
6232 			 * snd_ssthresh per RFC-6582 (option 2).
6233 			 */
6234 			tp->snd_cwnd = tp->snd_ssthresh;
6235 		}
6236 	}
6237 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
6238 		union tcp_log_stackspecific log;
6239 		struct timeval tv;
6240 
6241 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
6242 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
6243 		log.u_bbr.flex1 = th_ack;
6244 		log.u_bbr.flex2 = tp->t_ccv.flags;
6245 		log.u_bbr.flex3 = tp->t_ccv.bytes_this_ack;
6246 		log.u_bbr.flex4 = tp->t_ccv.nsegs;
6247 		log.u_bbr.flex5 = V_tcp_abc_l_var;
6248 		log.u_bbr.flex6 = orig_cwnd;
6249 		log.u_bbr.flex7 = V_tcp_do_newsack;
6250 		log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
6251 		log.u_bbr.flex8 = 2;
6252 		tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
6253 			       0, &log, false, NULL, __func__, __LINE__, &tv);
6254 	}
6255 	if ((rack->rack_no_prr == 0) &&
6256 	    (rack->no_prr_addback == 0) &&
6257 	    (rack->r_ctl.rc_prr_sndcnt > 0)) {
6258 		/*
6259 		 * Suck the next prr cnt back into cwnd, but
6260 		 * only do that if we are not application limited.
6261 		 */
6262 		if (ctf_outstanding(tp) <= sbavail(&tptosocket(tp)->so_snd)) {
6263 			/*
6264 			 * We are allowed to add back to the cwnd the amount we did
6265 			 * not get out if:
6266 			 * a) no_prr_addback is off.
6267 			 * b) we are not app limited
6268 			 * c) we are doing prr
6269 			 * <and>
6270 			 * d) it is bounded by rack_prr_addbackmax (if addback is 0, then none).
6271 			 */
6272 			tp->snd_cwnd += min((ctf_fixed_maxseg(tp) * rack_prr_addbackmax),
6273 					    rack->r_ctl.rc_prr_sndcnt);
6274 		}
6275 		rack->r_ctl.rc_prr_sndcnt = 0;
6276 		rack_log_to_prr(rack, 1, 0, __LINE__);
6277 	}
6278 	rack_log_to_prr(rack, 14, orig_cwnd, __LINE__);
6279 	tp->snd_recover = tp->snd_una;
6280 	if (rack->r_ctl.dsack_persist) {
6281 		rack->r_ctl.dsack_persist--;
6282 		if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
6283 			rack->r_ctl.num_dsack = 0;
6284 		}
6285 		rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
6286 	}
6287 	if (rack->rto_from_rec == 1) {
6288 		rack->rto_from_rec = 0;
6289 		if (rack->r_ctl.rto_ssthresh > tp->snd_ssthresh)
6290 			tp->snd_ssthresh = rack->r_ctl.rto_ssthresh;
6291 	}
6292 	rack_exit_recovery(tp, rack, 1);
6293 }
6294 
6295 static void
6296 rack_cong_signal(struct tcpcb *tp, uint32_t type, uint32_t ack, int line)
6297 {
6298 	struct tcp_rack *rack;
6299 	uint32_t ssthresh_enter, cwnd_enter, in_rec_at_entry, orig_cwnd;
6300 
6301 	INP_WLOCK_ASSERT(tptoinpcb(tp));
6302 #ifdef STATS
6303 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type);
6304 #endif
6305 	if (IN_RECOVERY(tp->t_flags) == 0) {
6306 		in_rec_at_entry = 0;
6307 		ssthresh_enter = tp->snd_ssthresh;
6308 		cwnd_enter = tp->snd_cwnd;
6309 	} else
6310 		in_rec_at_entry = 1;
6311 	rack = (struct tcp_rack *)tp->t_fb_ptr;
6312 	switch (type) {
6313 	case CC_NDUPACK:
6314 		tp->t_flags &= ~TF_WASFRECOVERY;
6315 		tp->t_flags &= ~TF_WASCRECOVERY;
6316 		if (!IN_FASTRECOVERY(tp->t_flags)) {
6317 			struct rack_sendmap *rsm;
6318 			struct timeval tv;
6319 			uint32_t segsiz;
6320 
6321 			/* Check if this is the end of the initial Start-up i.e. initial slow-start */
6322 			if (rack->rc_initial_ss_comp == 0) {
6323 				/* Yep it is the end of the initial slowstart */
6324 				rack->rc_initial_ss_comp = 1;
6325 			}
6326 			microuptime(&tv);
6327 			rack->r_ctl.time_entered_recovery = tcp_tv_to_lusectick(&tv);
6328 			if (SEQ_GEQ(ack, tp->snd_una)) {
6329 				/*
6330 				 * The ack is above snd_una. Lets see
6331 				 * if we can establish a postive distance from
6332 				 * our idle mark.
6333 				 */
6334 				rack->r_ctl.ack_for_idle = ack;
6335 				if (SEQ_GT(ack, rack->r_ctl.idle_snd_una)) {
6336 					rack->r_ctl.last_amount_before_rec = ack - rack->r_ctl.idle_snd_una;
6337 				} else {
6338 					/* No data thru yet */
6339 					rack->r_ctl.last_amount_before_rec = 0;
6340 				}
6341 			} else if (SEQ_GT(tp->snd_una, rack->r_ctl.idle_snd_una)) {
6342 				/*
6343 				 * The ack is out of order and behind the snd_una. It may
6344 				 * have contained SACK information which we processed else
6345 				 * we would have rejected it.
6346 				 */
6347 				rack->r_ctl.ack_for_idle = tp->snd_una;
6348 				rack->r_ctl.last_amount_before_rec = tp->snd_una - rack->r_ctl.idle_snd_una;
6349 			} else {
6350 				rack->r_ctl.ack_for_idle = ack;
6351 				rack->r_ctl.last_amount_before_rec = 0;
6352 			}
6353 			if (rack->rc_policer_detected) {
6354 				/*
6355 				 * If we are being policed and we have a loss, it
6356 				 * means our bucket is now empty. This can happen
6357 				 * where some other flow on the same host sends
6358 				 * that this connection is not aware of.
6359 				 */
6360 				rack->r_ctl.current_policer_bucket = 0;
6361 				if (rack_verbose_logging)
6362 					policer_detection_log(rack, rack->r_ctl.last_amount_before_rec, 0, 0, 0, 4);
6363 				if (rack->r_ctl.last_amount_before_rec > rack->r_ctl.policer_bucket_size) {
6364 					rack->r_ctl.policer_bucket_size = rack->r_ctl.last_amount_before_rec;
6365 				}
6366 			}
6367 			memset(rack->r_ctl.rc_cnt_of_retran, 0, sizeof(rack->r_ctl.rc_cnt_of_retran));
6368 			segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
6369 			TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
6370 				/*
6371 				 * Go through the outstanding and re-peg
6372 				 * any that should have been left in the
6373 				 * retransmit list (on a double recovery).
6374 				 */
6375 				if (rsm->r_act_rxt_cnt > 0) {
6376 					rack_peg_rxt(rack, rsm, segsiz);
6377 				}
6378 			}
6379 			rack->r_ctl.bytes_acked_in_recovery = 0;
6380 			rack->r_ctl.rc_prr_delivered = 0;
6381 			rack->r_ctl.rc_prr_out = 0;
6382 			rack->r_fast_output = 0;
6383 			if (rack->rack_no_prr == 0) {
6384 				rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
6385 				rack_log_to_prr(rack, 2, in_rec_at_entry, line);
6386 			}
6387 			rack->r_ctl.rc_prr_recovery_fs = tp->snd_max - tp->snd_una;
6388 			tp->snd_recover = tp->snd_max;
6389 			if (tp->t_flags2 & TF2_ECN_PERMIT)
6390 				tp->t_flags2 |= TF2_ECN_SND_CWR;
6391 		}
6392 		break;
6393 	case CC_ECN:
6394 		if (!IN_CONGRECOVERY(tp->t_flags) ||
6395 		    /*
6396 		     * Allow ECN reaction on ACK to CWR, if
6397 		     * that data segment was also CE marked.
6398 		     */
6399 		    SEQ_GEQ(ack, tp->snd_recover)) {
6400 			EXIT_CONGRECOVERY(tp->t_flags);
6401 			KMOD_TCPSTAT_INC(tcps_ecn_rcwnd);
6402 			rack->r_fast_output = 0;
6403 			tp->snd_recover = tp->snd_max + 1;
6404 			if (tp->t_flags2 & TF2_ECN_PERMIT)
6405 				tp->t_flags2 |= TF2_ECN_SND_CWR;
6406 		}
6407 		break;
6408 	case CC_RTO:
6409 		tp->t_dupacks = 0;
6410 		tp->t_bytes_acked = 0;
6411 		rack->r_fast_output = 0;
6412 		if (IN_RECOVERY(tp->t_flags))
6413 			rack_exit_recovery(tp, rack, 2);
6414 		rack->r_ctl.bytes_acked_in_recovery = 0;
6415 		rack->r_ctl.time_entered_recovery = 0;
6416 		orig_cwnd = tp->snd_cwnd;
6417 		rack_log_to_prr(rack, 16, orig_cwnd, line);
6418 		if (CC_ALGO(tp)->cong_signal == NULL) {
6419 			/* TSNH */
6420 			tp->snd_ssthresh = max(2,
6421 			    min(tp->snd_wnd, rack->r_ctl.cwnd_to_use) / 2 /
6422 			    ctf_fixed_maxseg(tp)) * ctf_fixed_maxseg(tp);
6423 			tp->snd_cwnd = ctf_fixed_maxseg(tp);
6424 		}
6425 		if (tp->t_flags2 & TF2_ECN_PERMIT)
6426 			tp->t_flags2 |= TF2_ECN_SND_CWR;
6427 		break;
6428 	case CC_RTO_ERR:
6429 		KMOD_TCPSTAT_INC(tcps_sndrexmitbad);
6430 		/* RTO was unnecessary, so reset everything. */
6431 		tp->snd_cwnd = tp->snd_cwnd_prev;
6432 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
6433 		tp->snd_recover = tp->snd_recover_prev;
6434 		if (tp->t_flags & TF_WASFRECOVERY) {
6435 			ENTER_FASTRECOVERY(tp->t_flags);
6436 			tp->t_flags &= ~TF_WASFRECOVERY;
6437 		}
6438 		if (tp->t_flags & TF_WASCRECOVERY) {
6439 			ENTER_CONGRECOVERY(tp->t_flags);
6440 			tp->t_flags &= ~TF_WASCRECOVERY;
6441 		}
6442 		tp->snd_nxt = tp->snd_max;
6443 		tp->t_badrxtwin = 0;
6444 		break;
6445 	}
6446 	if ((CC_ALGO(tp)->cong_signal != NULL)  &&
6447 	    (type != CC_RTO)){
6448 		tp->t_ccv.curack = ack;
6449 		CC_ALGO(tp)->cong_signal(&tp->t_ccv, type);
6450 	}
6451 	if ((in_rec_at_entry == 0) && IN_RECOVERY(tp->t_flags)) {
6452 		rack_log_to_prr(rack, 15, cwnd_enter, line);
6453 		rack->r_ctl.dsack_byte_cnt = 0;
6454 		rack->r_ctl.retran_during_recovery = 0;
6455 		rack->r_ctl.rc_cwnd_at_erec = cwnd_enter;
6456 		rack->r_ctl.rc_ssthresh_at_erec = ssthresh_enter;
6457 		rack->r_ent_rec_ns = 1;
6458 	}
6459 }
6460 
6461 static inline void
6462 rack_cc_after_idle(struct tcp_rack *rack, struct tcpcb *tp)
6463 {
6464 	uint32_t i_cwnd;
6465 
6466 	INP_WLOCK_ASSERT(tptoinpcb(tp));
6467 
6468 	if (CC_ALGO(tp)->after_idle != NULL)
6469 		CC_ALGO(tp)->after_idle(&tp->t_ccv);
6470 
6471 	if (tp->snd_cwnd == 1)
6472 		i_cwnd = tp->t_maxseg;		/* SYN(-ACK) lost */
6473 	else
6474 		i_cwnd = rc_init_window(rack);
6475 
6476 	/*
6477 	 * Being idle is no different than the initial window. If the cc
6478 	 * clamps it down below the initial window raise it to the initial
6479 	 * window.
6480 	 */
6481 	if (tp->snd_cwnd < i_cwnd) {
6482 		tp->snd_cwnd = i_cwnd;
6483 	}
6484 }
6485 
6486 /*
6487  * Indicate whether this ack should be delayed.  We can delay the ack if
6488  * following conditions are met:
6489  *	- There is no delayed ack timer in progress.
6490  *	- Our last ack wasn't a 0-sized window. We never want to delay
6491  *	  the ack that opens up a 0-sized window.
6492  *	- LRO wasn't used for this segment. We make sure by checking that the
6493  *	  segment size is not larger than the MSS.
6494  *	- Delayed acks are enabled or this is a half-synchronized T/TCP
6495  *	  connection.
6496  */
6497 #define DELAY_ACK(tp, tlen)			 \
6498 	(((tp->t_flags & TF_RXWIN0SENT) == 0) && \
6499 	((tp->t_flags & TF_DELACK) == 0) &&	 \
6500 	(tlen <= tp->t_maxseg) &&		 \
6501 	(tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN)))
6502 
6503 static struct rack_sendmap *
6504 rack_find_lowest_rsm(struct tcp_rack *rack)
6505 {
6506 	struct rack_sendmap *rsm;
6507 
6508 	/*
6509 	 * Walk the time-order transmitted list looking for an rsm that is
6510 	 * not acked. This will be the one that was sent the longest time
6511 	 * ago that is still outstanding.
6512 	 */
6513 	TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
6514 		if (rsm->r_flags & RACK_ACKED) {
6515 			continue;
6516 		}
6517 		goto finish;
6518 	}
6519 finish:
6520 	return (rsm);
6521 }
6522 
6523 static struct rack_sendmap *
6524 rack_find_high_nonack(struct tcp_rack *rack, struct rack_sendmap *rsm)
6525 {
6526 	struct rack_sendmap *prsm;
6527 
6528 	/*
6529 	 * Walk the sequence order list backward until we hit and arrive at
6530 	 * the highest seq not acked. In theory when this is called it
6531 	 * should be the last segment (which it was not).
6532 	 */
6533 	prsm = rsm;
6534 
6535 	TQHASH_FOREACH_REVERSE_FROM(prsm, rack->r_ctl.tqh) {
6536 		if (prsm->r_flags & (RACK_ACKED | RACK_HAS_FIN)) {
6537 			continue;
6538 		}
6539 		return (prsm);
6540 	}
6541 	return (NULL);
6542 }
6543 
6544 static uint32_t
6545 rack_calc_thresh_rack(struct tcp_rack *rack, uint32_t srtt, uint32_t cts, int line, int log_allowed)
6546 {
6547 	int32_t lro;
6548 	uint32_t thresh;
6549 
6550 	/*
6551 	 * lro is the flag we use to determine if we have seen reordering.
6552 	 * If it gets set we have seen reordering. The reorder logic either
6553 	 * works in one of two ways:
6554 	 *
6555 	 * If reorder-fade is configured, then we track the last time we saw
6556 	 * re-ordering occur. If we reach the point where enough time as
6557 	 * passed we no longer consider reordering has occuring.
6558 	 *
6559 	 * Or if reorder-face is 0, then once we see reordering we consider
6560 	 * the connection to alway be subject to reordering and just set lro
6561 	 * to 1.
6562 	 *
6563 	 * In the end if lro is non-zero we add the extra time for
6564 	 * reordering in.
6565 	 */
6566 	if (srtt == 0)
6567 		srtt = 1;
6568 	if (rack->r_ctl.rc_reorder_ts) {
6569 		if (rack->r_ctl.rc_reorder_fade) {
6570 			if (SEQ_GEQ(cts, rack->r_ctl.rc_reorder_ts)) {
6571 				lro = cts - rack->r_ctl.rc_reorder_ts;
6572 				if (lro == 0) {
6573 					/*
6574 					 * No time as passed since the last
6575 					 * reorder, mark it as reordering.
6576 					 */
6577 					lro = 1;
6578 				}
6579 			} else {
6580 				/* Negative time? */
6581 				lro = 0;
6582 			}
6583 			if (lro > rack->r_ctl.rc_reorder_fade) {
6584 				/* Turn off reordering seen too */
6585 				rack->r_ctl.rc_reorder_ts = 0;
6586 				lro = 0;
6587 			}
6588 		} else {
6589 			/* Reodering does not fade */
6590 			lro = 1;
6591 		}
6592 	} else {
6593 		lro = 0;
6594 	}
6595 	if (rack->rc_rack_tmr_std_based == 0) {
6596 		thresh = srtt + rack->r_ctl.rc_pkt_delay;
6597 	} else {
6598 		/* Standards based pkt-delay is 1/4 srtt */
6599 		thresh = srtt +  (srtt >> 2);
6600 	}
6601 	if (lro && (rack->rc_rack_tmr_std_based == 0)) {
6602 		/* It must be set, if not you get 1/4 rtt */
6603 		if (rack->r_ctl.rc_reorder_shift)
6604 			thresh += (srtt >> rack->r_ctl.rc_reorder_shift);
6605 		else
6606 			thresh += (srtt >> 2);
6607 	}
6608 	if (rack->rc_rack_use_dsack &&
6609 	    lro &&
6610 	    (rack->r_ctl.num_dsack > 0)) {
6611 		/*
6612 		 * We only increase the reordering window if we
6613 		 * have seen reordering <and> we have a DSACK count.
6614 		 */
6615 		thresh += rack->r_ctl.num_dsack * (srtt >> 2);
6616 		if (log_allowed)
6617 			rack_log_dsack_event(rack, 4, line, srtt, thresh);
6618 	}
6619 	/* SRTT * 2 is the ceiling */
6620 	if (thresh > (srtt * 2)) {
6621 		thresh = srtt * 2;
6622 	}
6623 	/* And we don't want it above the RTO max either */
6624 	if (thresh > rack_rto_max) {
6625 		thresh = rack_rto_max;
6626 	}
6627 	if (log_allowed)
6628 		rack_log_dsack_event(rack, 6, line,  srtt, thresh);
6629 	return (thresh);
6630 }
6631 
6632 static uint32_t
6633 rack_calc_thresh_tlp(struct tcpcb *tp, struct tcp_rack *rack,
6634 		     struct rack_sendmap *rsm, uint32_t srtt)
6635 {
6636 	struct rack_sendmap *prsm;
6637 	uint32_t thresh, len;
6638 	int segsiz;
6639 
6640 	if (srtt == 0)
6641 		srtt = 1;
6642 	if (rack->r_ctl.rc_tlp_threshold)
6643 		thresh = srtt + (srtt / rack->r_ctl.rc_tlp_threshold);
6644 	else
6645 		thresh = (srtt * 2);
6646 
6647 	/* Get the previous sent packet, if any */
6648 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
6649 	len = rsm->r_end - rsm->r_start;
6650 	if (rack->rack_tlp_threshold_use == TLP_USE_ID) {
6651 		/* Exactly like the ID */
6652 		if (((tp->snd_max - tp->snd_una) - rack->r_ctl.rc_sacked + rack->r_ctl.rc_holes_rxt) <= segsiz) {
6653 			uint32_t alt_thresh;
6654 			/*
6655 			 * Compensate for delayed-ack with the d-ack time.
6656 			 */
6657 			alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
6658 			if (alt_thresh > thresh)
6659 				thresh = alt_thresh;
6660 		}
6661 	} else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_ONE) {
6662 		/* 2.1 behavior */
6663 		prsm = TAILQ_PREV(rsm, rack_head, r_tnext);
6664 		if (prsm && (len <= segsiz)) {
6665 			/*
6666 			 * Two packets outstanding, thresh should be (2*srtt) +
6667 			 * possible inter-packet delay (if any).
6668 			 */
6669 			uint32_t inter_gap = 0;
6670 			int idx, nidx;
6671 
6672 			idx = rsm->r_rtr_cnt - 1;
6673 			nidx = prsm->r_rtr_cnt - 1;
6674 			if (rsm->r_tim_lastsent[nidx] >= prsm->r_tim_lastsent[idx]) {
6675 				/* Yes it was sent later (or at the same time) */
6676 				inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx];
6677 			}
6678 			thresh += inter_gap;
6679 		} else if (len <= segsiz) {
6680 			/*
6681 			 * Possibly compensate for delayed-ack.
6682 			 */
6683 			uint32_t alt_thresh;
6684 
6685 			alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
6686 			if (alt_thresh > thresh)
6687 				thresh = alt_thresh;
6688 		}
6689 	} else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_TWO) {
6690 		/* 2.2 behavior */
6691 		if (len <= segsiz) {
6692 			uint32_t alt_thresh;
6693 			/*
6694 			 * Compensate for delayed-ack with the d-ack time.
6695 			 */
6696 			alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
6697 			if (alt_thresh > thresh)
6698 				thresh = alt_thresh;
6699 		}
6700 	}
6701 	/* Not above an RTO */
6702 	if (thresh > tp->t_rxtcur) {
6703 		thresh = tp->t_rxtcur;
6704 	}
6705 	/* Not above a RTO max */
6706 	if (thresh > rack_rto_max) {
6707 		thresh = rack_rto_max;
6708 	}
6709 	/* Apply user supplied min TLP */
6710 	if (thresh < rack_tlp_min) {
6711 		thresh = rack_tlp_min;
6712 	}
6713 	return (thresh);
6714 }
6715 
6716 static uint32_t
6717 rack_grab_rtt(struct tcpcb *tp, struct tcp_rack *rack)
6718 {
6719 	/*
6720 	 * We want the rack_rtt which is the
6721 	 * last rtt we measured. However if that
6722 	 * does not exist we fallback to the srtt (which
6723 	 * we probably will never do) and then as a last
6724 	 * resort we use RACK_INITIAL_RTO if no srtt is
6725 	 * yet set.
6726 	 */
6727 	if (rack->rc_rack_rtt)
6728 		return (rack->rc_rack_rtt);
6729 	else if (tp->t_srtt == 0)
6730 		return (RACK_INITIAL_RTO);
6731 	return (tp->t_srtt);
6732 }
6733 
6734 static struct rack_sendmap *
6735 rack_check_recovery_mode(struct tcpcb *tp, uint32_t tsused)
6736 {
6737 	/*
6738 	 * Check to see that we don't need to fall into recovery. We will
6739 	 * need to do so if our oldest transmit is past the time we should
6740 	 * have had an ack.
6741 	 */
6742 	struct tcp_rack *rack;
6743 	struct rack_sendmap *rsm;
6744 	int32_t idx;
6745 	uint32_t srtt, thresh;
6746 
6747 	rack = (struct tcp_rack *)tp->t_fb_ptr;
6748 	if (tqhash_empty(rack->r_ctl.tqh)) {
6749 		return (NULL);
6750 	}
6751 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6752 	if (rsm == NULL)
6753 		return (NULL);
6754 
6755 
6756 	if (rsm->r_flags & RACK_ACKED) {
6757 		rsm = rack_find_lowest_rsm(rack);
6758 		if (rsm == NULL)
6759 			return (NULL);
6760 	}
6761 	idx = rsm->r_rtr_cnt - 1;
6762 	srtt = rack_grab_rtt(tp, rack);
6763 	thresh = rack_calc_thresh_rack(rack, srtt, tsused, __LINE__, 1);
6764 	if (TSTMP_LT(tsused, ((uint32_t)rsm->r_tim_lastsent[idx]))) {
6765 		return (NULL);
6766 	}
6767 	if ((tsused - ((uint32_t)rsm->r_tim_lastsent[idx])) < thresh) {
6768 		return (NULL);
6769 	}
6770 	/* Ok if we reach here we are over-due and this guy can be sent */
6771 	rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__);
6772 	return (rsm);
6773 }
6774 
6775 static uint32_t
6776 rack_get_persists_timer_val(struct tcpcb *tp, struct tcp_rack *rack)
6777 {
6778 	int32_t t;
6779 	int32_t tt;
6780 	uint32_t ret_val;
6781 
6782 	t = (tp->t_srtt + (tp->t_rttvar << 2));
6783 	RACK_TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
6784  	    rack_persist_min, rack_persist_max, rack->r_ctl.timer_slop);
6785 	rack->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT;
6786 	ret_val = (uint32_t)tt;
6787 	return (ret_val);
6788 }
6789 
6790 static uint32_t
6791 rack_timer_start(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int sup_rack)
6792 {
6793 	/*
6794 	 * Start the FR timer, we do this based on getting the first one in
6795 	 * the rc_tmap. Note that if its NULL we must stop the timer. in all
6796 	 * events we need to stop the running timer (if its running) before
6797 	 * starting the new one.
6798 	 */
6799 	uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse;
6800 	uint32_t srtt_cur;
6801 	int32_t idx;
6802 	int32_t is_tlp_timer = 0;
6803 	struct rack_sendmap *rsm;
6804 
6805 	if (rack->t_timers_stopped) {
6806 		/* All timers have been stopped none are to run */
6807 		return (0);
6808 	}
6809 	if (rack->rc_in_persist) {
6810 		/* We can't start any timer in persists */
6811 		return (rack_get_persists_timer_val(tp, rack));
6812 	}
6813 	rack->rc_on_min_to = 0;
6814 	if ((tp->t_state < TCPS_ESTABLISHED) ||
6815 	    (rack->sack_attack_disable > 0) ||
6816 	    ((tp->t_flags & TF_SACK_PERMIT) == 0)) {
6817 		goto activate_rxt;
6818 	}
6819 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6820 	if ((rsm == NULL) || sup_rack) {
6821 		/* Nothing on the send map or no rack */
6822 activate_rxt:
6823 		time_since_sent = 0;
6824 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6825 		if (rsm) {
6826 			/*
6827 			 * Should we discount the RTX timer any?
6828 			 *
6829 			 * We want to discount it the smallest amount.
6830 			 * If a timer (Rack/TLP or RXT) has gone off more
6831 			 * recently thats the discount we want to use (now - timer time).
6832 			 * If the retransmit of the oldest packet was more recent then
6833 			 * we want to use that (now - oldest-packet-last_transmit_time).
6834 			 *
6835 			 */
6836 			idx = rsm->r_rtr_cnt - 1;
6837 			if (TSTMP_GEQ(rack->r_ctl.rc_tlp_rxt_last_time, ((uint32_t)rsm->r_tim_lastsent[idx])))
6838 				tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time;
6839 			else
6840 				tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx];
6841 			if (TSTMP_GT(cts, tstmp_touse))
6842 			    time_since_sent = cts - tstmp_touse;
6843 		}
6844 		if (SEQ_LT(tp->snd_una, tp->snd_max) ||
6845 		    sbavail(&tptosocket(tp)->so_snd)) {
6846 			rack->r_ctl.rc_hpts_flags |= PACE_TMR_RXT;
6847 			to = tp->t_rxtcur;
6848 			if (to > time_since_sent)
6849 				to -= time_since_sent;
6850 			else
6851 				to = rack->r_ctl.rc_min_to;
6852 			if (to == 0)
6853 				to = 1;
6854 			/* Special case for KEEPINIT */
6855 			if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) &&
6856 			    (TP_KEEPINIT(tp) != 0) &&
6857 			    rsm) {
6858 				/*
6859 				 * We have to put a ceiling on the rxt timer
6860 				 * of the keep-init timeout.
6861 				 */
6862 				uint32_t max_time, red;
6863 
6864 				max_time = TICKS_2_USEC(TP_KEEPINIT(tp));
6865 				if (TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) {
6866 					red = (cts - (uint32_t)rsm->r_tim_lastsent[0]);
6867 					if (red < max_time)
6868 						max_time -= red;
6869 					else
6870 						max_time = 1;
6871 				}
6872 				/* Reduce timeout to the keep value if needed */
6873 				if (max_time < to)
6874 					to = max_time;
6875 			}
6876 			return (to);
6877 		}
6878 		return (0);
6879 	}
6880 	if (rsm->r_flags & RACK_ACKED) {
6881 		rsm = rack_find_lowest_rsm(rack);
6882 		if (rsm == NULL) {
6883 			/* No lowest? */
6884 			goto activate_rxt;
6885 		}
6886 	}
6887 	if (rack->sack_attack_disable) {
6888 		/*
6889 		 * We don't want to do
6890 		 * any TLP's if you are an attacker.
6891 		 * Though if you are doing what
6892 		 * is expected you may still have
6893 		 * SACK-PASSED marks.
6894 		 */
6895 		goto activate_rxt;
6896 	}
6897 	/* Convert from ms to usecs */
6898 	if ((rsm->r_flags & RACK_SACK_PASSED) ||
6899 	    (rsm->r_flags & RACK_RWND_COLLAPSED) ||
6900 	    (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
6901 		if ((tp->t_flags & TF_SENTFIN) &&
6902 		    ((tp->snd_max - tp->snd_una) == 1) &&
6903 		    (rsm->r_flags & RACK_HAS_FIN)) {
6904 			/*
6905 			 * We don't start a rack timer if all we have is a
6906 			 * FIN outstanding.
6907 			 */
6908 			goto activate_rxt;
6909 		}
6910 		if ((rack->use_rack_rr == 0) &&
6911 		    (IN_FASTRECOVERY(tp->t_flags)) &&
6912 		    (rack->rack_no_prr == 0) &&
6913 		     (rack->r_ctl.rc_prr_sndcnt  < ctf_fixed_maxseg(tp))) {
6914 			/*
6915 			 * We are not cheating, in recovery  and
6916 			 * not enough ack's to yet get our next
6917 			 * retransmission out.
6918 			 *
6919 			 * Note that classified attackers do not
6920 			 * get to use the rack-cheat.
6921 			 */
6922 			goto activate_tlp;
6923 		}
6924 		srtt = rack_grab_rtt(tp, rack);
6925 		thresh = rack_calc_thresh_rack(rack, srtt, cts, __LINE__, 1);
6926 		idx = rsm->r_rtr_cnt - 1;
6927 		exp = ((uint32_t)rsm->r_tim_lastsent[idx]) + thresh;
6928 		if (SEQ_GEQ(exp, cts)) {
6929 			to = exp - cts;
6930 			if (to < rack->r_ctl.rc_min_to) {
6931 				to = rack->r_ctl.rc_min_to;
6932 				if (rack->r_rr_config == 3)
6933 					rack->rc_on_min_to = 1;
6934 			}
6935 		} else {
6936 			to = rack->r_ctl.rc_min_to;
6937 			if (rack->r_rr_config == 3)
6938 				rack->rc_on_min_to = 1;
6939 		}
6940 	} else {
6941 		/* Ok we need to do a TLP not RACK */
6942 activate_tlp:
6943 		if ((rack->rc_tlp_in_progress != 0) &&
6944 		    (rack->r_ctl.rc_tlp_cnt_out >= rack_tlp_limit)) {
6945 			/*
6946 			 * The previous send was a TLP and we have sent
6947 			 * N TLP's without sending new data.
6948 			 */
6949 			goto activate_rxt;
6950 		}
6951 		rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
6952 		if (rsm == NULL) {
6953 			/* We found no rsm to TLP with. */
6954 			goto activate_rxt;
6955 		}
6956 		if (rsm->r_flags & RACK_HAS_FIN) {
6957 			/* If its a FIN we dont do TLP */
6958 			rsm = NULL;
6959 			goto activate_rxt;
6960 		}
6961 		idx = rsm->r_rtr_cnt - 1;
6962 		time_since_sent = 0;
6963 		if (TSTMP_GEQ(((uint32_t)rsm->r_tim_lastsent[idx]), rack->r_ctl.rc_tlp_rxt_last_time))
6964 			tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx];
6965 		else
6966 			tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time;
6967 		if (TSTMP_GT(cts, tstmp_touse))
6968 		    time_since_sent = cts - tstmp_touse;
6969 		is_tlp_timer = 1;
6970 		if (tp->t_srtt) {
6971 			if ((rack->rc_srtt_measure_made == 0) &&
6972 			    (tp->t_srtt == 1)) {
6973 				/*
6974 				 * If another stack as run and set srtt to 1,
6975 				 * then the srtt was 0, so lets use the initial.
6976 				 */
6977 				srtt = RACK_INITIAL_RTO;
6978 			} else {
6979 				srtt_cur = tp->t_srtt;
6980 				srtt = srtt_cur;
6981 			}
6982 		} else
6983 			srtt = RACK_INITIAL_RTO;
6984 		/*
6985 		 * If the SRTT is not keeping up and the
6986 		 * rack RTT has spiked we want to use
6987 		 * the last RTT not the smoothed one.
6988 		 */
6989 		if (rack_tlp_use_greater &&
6990 		    tp->t_srtt &&
6991 		    (srtt < rack_grab_rtt(tp, rack))) {
6992 			srtt = rack_grab_rtt(tp, rack);
6993 		}
6994 		thresh = rack_calc_thresh_tlp(tp, rack, rsm, srtt);
6995 		if (thresh > time_since_sent) {
6996 			to = thresh - time_since_sent;
6997 		} else {
6998 			to = rack->r_ctl.rc_min_to;
6999 			rack_log_alt_to_to_cancel(rack,
7000 						  thresh,		/* flex1 */
7001 						  time_since_sent,	/* flex2 */
7002 						  tstmp_touse,		/* flex3 */
7003 						  rack->r_ctl.rc_tlp_rxt_last_time, /* flex4 */
7004 						  (uint32_t)rsm->r_tim_lastsent[idx],
7005 						  srtt,
7006 						  idx, 99);
7007 		}
7008 		if (to < rack_tlp_min) {
7009 			to = rack_tlp_min;
7010 		}
7011 		if (to > TICKS_2_USEC(TCPTV_REXMTMAX)) {
7012 			/*
7013 			 * If the TLP time works out to larger than the max
7014 			 * RTO lets not do TLP.. just RTO.
7015 			 */
7016 			goto activate_rxt;
7017 		}
7018 	}
7019 	if (is_tlp_timer == 0) {
7020 		rack->r_ctl.rc_hpts_flags |= PACE_TMR_RACK;
7021 	} else {
7022 		rack->r_ctl.rc_hpts_flags |= PACE_TMR_TLP;
7023 	}
7024 	if (to == 0)
7025 		to = 1;
7026 	return (to);
7027 }
7028 
7029 static void
7030 rack_enter_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, tcp_seq snd_una)
7031 {
7032 	if (rack->rc_in_persist == 0) {
7033 		if (tp->t_flags & TF_GPUTINPROG) {
7034 			/*
7035 			 * Stop the goodput now, the calling of the
7036 			 * measurement function clears the flag.
7037 			 */
7038 			rack_do_goodput_measurement(tp, rack, tp->snd_una, __LINE__,
7039 						    RACK_QUALITY_PERSIST);
7040 		}
7041 #ifdef NETFLIX_SHARED_CWND
7042 		if (rack->r_ctl.rc_scw) {
7043 			tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
7044 			rack->rack_scwnd_is_idle = 1;
7045 		}
7046 #endif
7047 		rack->r_ctl.rc_went_idle_time = cts;
7048 		if (rack->r_ctl.rc_went_idle_time == 0)
7049 			rack->r_ctl.rc_went_idle_time = 1;
7050 		if (rack->lt_bw_up) {
7051 			/* Suspend our LT BW measurement */
7052 			uint64_t tmark;
7053 
7054 			rack->r_ctl.lt_bw_bytes += (snd_una - rack->r_ctl.lt_seq);
7055 			rack->r_ctl.lt_seq = snd_una;
7056 			tmark = tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time);
7057 			if (tmark >= rack->r_ctl.lt_timemark) {
7058 				rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark);
7059 			}
7060 			rack->r_ctl.lt_timemark = tmark;
7061 			rack->lt_bw_up = 0;
7062 			rack->r_persist_lt_bw_off = 1;
7063 		}
7064 		rack_timer_cancel(tp, rack, cts, __LINE__);
7065 		rack->r_ctl.persist_lost_ends = 0;
7066 		rack->probe_not_answered = 0;
7067 		rack->forced_ack = 0;
7068 		tp->t_rxtshift = 0;
7069 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
7070 			      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
7071 		rack->rc_in_persist = 1;
7072 	}
7073 }
7074 
7075 static void
7076 rack_exit_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
7077 {
7078 	if (tcp_in_hpts(rack->rc_tp)) {
7079 		tcp_hpts_remove(rack->rc_tp);
7080 		rack->r_ctl.rc_hpts_flags = 0;
7081 	}
7082 #ifdef NETFLIX_SHARED_CWND
7083 	if (rack->r_ctl.rc_scw) {
7084 		tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
7085 		rack->rack_scwnd_is_idle = 0;
7086 	}
7087 #endif
7088 	if (rack->rc_gp_dyn_mul &&
7089 	    (rack->use_fixed_rate == 0) &&
7090 	    (rack->rc_always_pace)) {
7091 		/*
7092 		 * Do we count this as if a probe-rtt just
7093 		 * finished?
7094 		 */
7095 		uint32_t time_idle, idle_min;
7096 
7097 		time_idle = cts - rack->r_ctl.rc_went_idle_time;
7098 		idle_min = rack_min_probertt_hold;
7099 		if (rack_probertt_gpsrtt_cnt_div) {
7100 			uint64_t extra;
7101 			extra = (uint64_t)rack->r_ctl.rc_gp_srtt *
7102 				(uint64_t)rack_probertt_gpsrtt_cnt_mul;
7103 			extra /= (uint64_t)rack_probertt_gpsrtt_cnt_div;
7104 			idle_min += (uint32_t)extra;
7105 		}
7106 		if (time_idle >= idle_min) {
7107 			/* Yes, we count it as a probe-rtt. */
7108 			uint32_t us_cts;
7109 
7110 			us_cts = tcp_get_usecs(NULL);
7111 			if (rack->in_probe_rtt == 0) {
7112 				rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
7113 				rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts;
7114 				rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts;
7115 				rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts;
7116 			} else {
7117 				rack_exit_probertt(rack, us_cts);
7118 			}
7119 		}
7120 	}
7121 	if (rack->r_persist_lt_bw_off) {
7122 		/* Continue where we left off */
7123 		rack->r_ctl.lt_timemark = tcp_get_u64_usecs(NULL);
7124 		rack->lt_bw_up = 1;
7125 		rack->r_persist_lt_bw_off = 0;
7126 	}
7127 	rack->r_ctl.idle_snd_una = tp->snd_una;
7128 	rack->rc_in_persist = 0;
7129 	rack->r_ctl.rc_went_idle_time = 0;
7130 	tp->t_rxtshift = 0;
7131 	RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
7132 	   rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
7133 	rack->r_ctl.rc_agg_delayed = 0;
7134 	rack->r_early = 0;
7135 	rack->r_late = 0;
7136 	rack->r_ctl.rc_agg_early = 0;
7137 }
7138 
7139 static void
7140 rack_log_hpts_diag(struct tcp_rack *rack, uint32_t cts,
7141 		   struct hpts_diag *diag, struct timeval *tv)
7142 {
7143 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
7144 		union tcp_log_stackspecific log;
7145 
7146 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
7147 		log.u_bbr.flex1 = diag->p_nxt_slot;
7148 		log.u_bbr.flex2 = diag->p_cur_slot;
7149 		log.u_bbr.flex3 = diag->slot_req;
7150 		log.u_bbr.flex4 = diag->inp_hptsslot;
7151 		log.u_bbr.flex5 = diag->slot_remaining;
7152 		log.u_bbr.flex6 = diag->need_new_to;
7153 		log.u_bbr.flex7 = diag->p_hpts_active;
7154 		log.u_bbr.flex8 = diag->p_on_min_sleep;
7155 		/* Hijack other fields as needed */
7156 		log.u_bbr.epoch = diag->have_slept;
7157 		log.u_bbr.lt_epoch = diag->yet_to_sleep;
7158 		log.u_bbr.pkts_out = diag->co_ret;
7159 		log.u_bbr.applimited = diag->hpts_sleep_time;
7160 		log.u_bbr.delivered = diag->p_prev_slot;
7161 		log.u_bbr.inflight = diag->p_runningslot;
7162 		log.u_bbr.bw_inuse = diag->wheel_slot;
7163 		log.u_bbr.rttProp = diag->wheel_cts;
7164 		log.u_bbr.timeStamp = cts;
7165 		log.u_bbr.delRate = diag->maxslots;
7166 		log.u_bbr.cur_del_rate = diag->p_curtick;
7167 		log.u_bbr.cur_del_rate <<= 32;
7168 		log.u_bbr.cur_del_rate |= diag->p_lasttick;
7169 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
7170 		    &rack->rc_inp->inp_socket->so_rcv,
7171 		    &rack->rc_inp->inp_socket->so_snd,
7172 		    BBR_LOG_HPTSDIAG, 0,
7173 		    0, &log, false, tv);
7174 	}
7175 
7176 }
7177 
7178 static void
7179 rack_log_wakeup(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb, uint32_t len, int type)
7180 {
7181 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
7182 		union tcp_log_stackspecific log;
7183 		struct timeval tv;
7184 
7185 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
7186 		log.u_bbr.flex1 = sb->sb_flags;
7187 		log.u_bbr.flex2 = len;
7188 		log.u_bbr.flex3 = sb->sb_state;
7189 		log.u_bbr.flex8 = type;
7190 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
7191 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
7192 		    &rack->rc_inp->inp_socket->so_rcv,
7193 		    &rack->rc_inp->inp_socket->so_snd,
7194 		    TCP_LOG_SB_WAKE, 0,
7195 		    len, &log, false, &tv);
7196 	}
7197 }
7198 
7199 static void
7200 rack_start_hpts_timer (struct tcp_rack *rack, struct tcpcb *tp, uint32_t cts,
7201       int32_t slot, uint32_t tot_len_this_send, int sup_rack)
7202 {
7203 	struct hpts_diag diag;
7204 	struct inpcb *inp = tptoinpcb(tp);
7205 	struct timeval tv;
7206 	uint32_t delayed_ack = 0;
7207 	uint32_t hpts_timeout;
7208 	uint32_t entry_slot = slot;
7209 	uint8_t stopped;
7210 	uint32_t left = 0;
7211 	uint32_t us_cts;
7212 
7213 	if ((tp->t_state == TCPS_CLOSED) ||
7214 	    (tp->t_state == TCPS_LISTEN)) {
7215 		return;
7216 	}
7217 	if (tcp_in_hpts(tp)) {
7218 		/* Already on the pacer */
7219 		return;
7220 	}
7221 	stopped = rack->rc_tmr_stopped;
7222 	if (stopped && TSTMP_GT(rack->r_ctl.rc_timer_exp, cts)) {
7223 		left = rack->r_ctl.rc_timer_exp - cts;
7224 	}
7225 	rack->r_ctl.rc_timer_exp = 0;
7226 	rack->r_ctl.rc_hpts_flags = 0;
7227 	us_cts = tcp_get_usecs(&tv);
7228 	/* Now early/late accounting */
7229 	rack_log_pacing_delay_calc(rack, entry_slot, slot, 0, 0, 0, 26, __LINE__, NULL, 0);
7230 	if (rack->r_early && (rack->rc_ack_can_sendout_data == 0)) {
7231 		/*
7232 		 * We have a early carry over set,
7233 		 * we can always add more time so we
7234 		 * can always make this compensation.
7235 		 *
7236 		 * Note if ack's are allowed to wake us do not
7237 		 * penalize the next timer for being awoke
7238 		 * by an ack aka the rc_agg_early (non-paced mode).
7239 		 */
7240 		slot += rack->r_ctl.rc_agg_early;
7241 		rack->r_early = 0;
7242 		rack->r_ctl.rc_agg_early = 0;
7243 	}
7244 	if ((rack->r_late) &&
7245 	    ((rack->r_use_hpts_min == 0) || (rack->dgp_on == 0))) {
7246 		/*
7247 		 * This is harder, we can
7248 		 * compensate some but it
7249 		 * really depends on what
7250 		 * the current pacing time is.
7251 		 */
7252 		if (rack->r_ctl.rc_agg_delayed >= slot) {
7253 			/*
7254 			 * We can't compensate for it all.
7255 			 * And we have to have some time
7256 			 * on the clock. We always have a min
7257 			 * 10 slots (10 x 10 i.e. 100 usecs).
7258 			 */
7259 			if (slot <= HPTS_TICKS_PER_SLOT) {
7260 				/* We gain delay */
7261 				rack->r_ctl.rc_agg_delayed += (HPTS_TICKS_PER_SLOT - slot);
7262 				slot = HPTS_TICKS_PER_SLOT;
7263 			} else {
7264 				/* We take off some */
7265 				rack->r_ctl.rc_agg_delayed -= (slot - HPTS_TICKS_PER_SLOT);
7266 				slot = HPTS_TICKS_PER_SLOT;
7267 			}
7268 		} else {
7269 			slot -= rack->r_ctl.rc_agg_delayed;
7270 			rack->r_ctl.rc_agg_delayed = 0;
7271 			/* Make sure we have 100 useconds at minimum */
7272 			if (slot < HPTS_TICKS_PER_SLOT) {
7273 				rack->r_ctl.rc_agg_delayed = HPTS_TICKS_PER_SLOT - slot;
7274 				slot = HPTS_TICKS_PER_SLOT;
7275 			}
7276 			if (rack->r_ctl.rc_agg_delayed == 0)
7277 				rack->r_late = 0;
7278 		}
7279 	} else if (rack->r_late) {
7280 		/* r_use_hpts_min is on and so is DGP */
7281 		uint32_t max_red;
7282 
7283 		max_red = (slot * rack->r_ctl.max_reduction) / 100;
7284 		if (max_red >= rack->r_ctl.rc_agg_delayed) {
7285 			slot -= rack->r_ctl.rc_agg_delayed;
7286 			rack->r_ctl.rc_agg_delayed = 0;
7287 		} else {
7288 			slot -= max_red;
7289 			rack->r_ctl.rc_agg_delayed -= max_red;
7290 		}
7291 	}
7292 	if ((rack->r_use_hpts_min == 1) &&
7293 	    (slot > 0) &&
7294 	    (rack->dgp_on == 1)) {
7295 		/*
7296 		 * We are enforcing a min pacing timer
7297 		 * based on our hpts min timeout.
7298 		 */
7299 		uint32_t min;
7300 
7301 		min = get_hpts_min_sleep_time();
7302 		if (min > slot) {
7303 			slot = min;
7304 		}
7305 	}
7306 	hpts_timeout = rack_timer_start(tp, rack, cts, sup_rack);
7307 #ifdef TCP_SAD_DETECTION
7308 	if (rack->sack_attack_disable &&
7309 	    (rack->r_ctl.ack_during_sd > 0) &&
7310 	    (slot < tcp_sad_pacing_interval)) {
7311 		/*
7312 		 * We have a potential attacker on
7313 		 * the line. We have possibly some
7314 		 * (or now) pacing time set. We want to
7315 		 * slow down the processing of sacks by some
7316 		 * amount (if it is an attacker). Set the default
7317 		 * slot for attackers in place (unless the original
7318 		 * interval is longer). Its stored in
7319 		 * micro-seconds, so lets convert to msecs.
7320 		 */
7321 		slot = tcp_sad_pacing_interval;
7322 		rack_log_type_bbrsnd(rack, tot_len_this_send, slot, us_cts, &tv, __LINE__);
7323 		rack->r_ctl.ack_during_sd = 0;
7324 	}
7325 #endif
7326 	if (tp->t_flags & TF_DELACK) {
7327 		delayed_ack = TICKS_2_USEC(tcp_delacktime);
7328 		rack->r_ctl.rc_hpts_flags |= PACE_TMR_DELACK;
7329 	}
7330 	if (delayed_ack && ((hpts_timeout == 0) ||
7331 			    (delayed_ack < hpts_timeout)))
7332 		hpts_timeout = delayed_ack;
7333 	else
7334 		rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
7335 	/*
7336 	 * If no timers are going to run and we will fall off the hptsi
7337 	 * wheel, we resort to a keep-alive timer if its configured.
7338 	 */
7339 	if ((hpts_timeout == 0) &&
7340 	    (slot == 0)) {
7341 		if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
7342 		    (tp->t_state <= TCPS_CLOSING)) {
7343 			/*
7344 			 * Ok we have no timer (persists, rack, tlp, rxt  or
7345 			 * del-ack), we don't have segments being paced. So
7346 			 * all that is left is the keepalive timer.
7347 			 */
7348 			if (TCPS_HAVEESTABLISHED(tp->t_state)) {
7349 				/* Get the established keep-alive time */
7350 				hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp));
7351 			} else {
7352 				/*
7353 				 * Get the initial setup keep-alive time,
7354 				 * note that this is probably not going to
7355 				 * happen, since rack will be running a rxt timer
7356 				 * if a SYN of some sort is outstanding. It is
7357 				 * actually handled in rack_timeout_rxt().
7358 				 */
7359 				hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp));
7360 			}
7361 			rack->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP;
7362 			if (rack->in_probe_rtt) {
7363 				/*
7364 				 * We want to instead not wake up a long time from
7365 				 * now but to wake up about the time we would
7366 				 * exit probe-rtt and initiate a keep-alive ack.
7367 				 * This will get us out of probe-rtt and update
7368 				 * our min-rtt.
7369 				 */
7370 				hpts_timeout = rack_min_probertt_hold;
7371 			}
7372 		}
7373 	}
7374 	if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) ==
7375 	    (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) {
7376 		/*
7377 		 * RACK, TLP, persists and RXT timers all are restartable
7378 		 * based on actions input .. i.e we received a packet (ack
7379 		 * or sack) and that changes things (rw, or snd_una etc).
7380 		 * Thus we can restart them with a new value. For
7381 		 * keep-alive, delayed_ack we keep track of what was left
7382 		 * and restart the timer with a smaller value.
7383 		 */
7384 		if (left < hpts_timeout)
7385 			hpts_timeout = left;
7386 	}
7387 	if (hpts_timeout) {
7388 		/*
7389 		 * Hack alert for now we can't time-out over 2,147,483
7390 		 * seconds (a bit more than 596 hours), which is probably ok
7391 		 * :).
7392 		 */
7393 		if (hpts_timeout > 0x7ffffffe)
7394 			hpts_timeout = 0x7ffffffe;
7395 		rack->r_ctl.rc_timer_exp = cts + hpts_timeout;
7396 	}
7397 	rack_log_pacing_delay_calc(rack, entry_slot, slot, hpts_timeout, 0, 0, 27, __LINE__, NULL, 0);
7398 	if ((rack->gp_ready == 0) &&
7399 	    (rack->use_fixed_rate == 0) &&
7400 	    (hpts_timeout < slot) &&
7401 	    (rack->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) {
7402 		/*
7403 		 * We have no good estimate yet for the
7404 		 * old clunky burst mitigation or the
7405 		 * real pacing. And the tlp or rxt is smaller
7406 		 * than the pacing calculation. Lets not
7407 		 * pace that long since we know the calculation
7408 		 * so far is not accurate.
7409 		 */
7410 		slot = hpts_timeout;
7411 	}
7412 	/**
7413 	 * Turn off all the flags for queuing by default. The
7414 	 * flags have important meanings to what happens when
7415 	 * LRO interacts with the transport. Most likely (by default now)
7416 	 * mbuf_queueing and ack compression are on. So the transport
7417 	 * has a couple of flags that control what happens (if those
7418 	 * are not on then these flags won't have any effect since it
7419 	 * won't go through the queuing LRO path).
7420 	 *
7421 	 * TF2_MBUF_QUEUE_READY - This flags says that I am busy
7422 	 *                        pacing output, so don't disturb. But
7423 	 *                        it also means LRO can wake me if there
7424 	 *                        is a SACK arrival.
7425 	 *
7426 	 * TF2_DONT_SACK_QUEUE - This flag is used in conjunction
7427 	 *                       with the above flag (QUEUE_READY) and
7428 	 *                       when present it says don't even wake me
7429 	 *                       if a SACK arrives.
7430 	 *
7431 	 * The idea behind these flags is that if we are pacing we
7432 	 * set the MBUF_QUEUE_READY and only get woken up if
7433 	 * a SACK arrives (which could change things) or if
7434 	 * our pacing timer expires. If, however, we have a rack
7435 	 * timer running, then we don't even want a sack to wake
7436 	 * us since the rack timer has to expire before we can send.
7437 	 *
7438 	 * Other cases should usually have none of the flags set
7439 	 * so LRO can call into us.
7440 	 */
7441 	tp->t_flags2 &= ~(TF2_DONT_SACK_QUEUE|TF2_MBUF_QUEUE_READY);
7442 	if (slot) {
7443 		rack->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT;
7444 		rack->r_ctl.rc_last_output_to = us_cts + slot;
7445 		/*
7446 		 * A pacing timer (slot) is being set, in
7447 		 * such a case we cannot send (we are blocked by
7448 		 * the timer). So lets tell LRO that it should not
7449 		 * wake us unless there is a SACK. Note this only
7450 		 * will be effective if mbuf queueing is on or
7451 		 * compressed acks are being processed.
7452 		 */
7453 		tp->t_flags2 |= TF2_MBUF_QUEUE_READY;
7454 		/*
7455 		 * But wait if we have a Rack timer running
7456 		 * even a SACK should not disturb us (with
7457 		 * the exception of r_rr_config 3).
7458 		 */
7459 		if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK) ||
7460 		    (IN_RECOVERY(tp->t_flags))) {
7461 			if (rack->r_rr_config != 3)
7462 				tp->t_flags2 |= TF2_DONT_SACK_QUEUE;
7463 			else if (rack->rc_pace_dnd) {
7464 				/*
7465 				 * When DND is on, we only let a sack
7466 				 * interrupt us if we are not in recovery.
7467 				 *
7468 				 * If DND is off, then we never hit here
7469 				 * and let all sacks wake us up.
7470 				 *
7471 				 */
7472 				tp->t_flags2 |= TF2_DONT_SACK_QUEUE;
7473 			}
7474 		}
7475 		/* For sack attackers we want to ignore sack */
7476 		if (rack->sack_attack_disable == 1) {
7477 			tp->t_flags2 |= (TF2_DONT_SACK_QUEUE |
7478 			    TF2_MBUF_QUEUE_READY);
7479 		} else if (rack->rc_ack_can_sendout_data) {
7480 			/*
7481 			 * Ahh but wait, this is that special case
7482 			 * where the pacing timer can be disturbed
7483 			 * backout the changes (used for non-paced
7484 			 * burst limiting).
7485 			 */
7486 			tp->t_flags2 &= ~(TF2_DONT_SACK_QUEUE |
7487 			    TF2_MBUF_QUEUE_READY);
7488 		}
7489 		if ((rack->use_rack_rr) &&
7490 		    (rack->r_rr_config < 2) &&
7491 		    ((hpts_timeout) && (hpts_timeout < slot))) {
7492 			/*
7493 			 * Arrange for the hpts to kick back in after the
7494 			 * t-o if the t-o does not cause a send.
7495 			 */
7496 			(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(hpts_timeout),
7497 						   __LINE__, &diag);
7498 			rack_log_hpts_diag(rack, us_cts, &diag, &tv);
7499 			rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
7500 		} else {
7501 			(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(slot),
7502 						   __LINE__, &diag);
7503 			rack_log_hpts_diag(rack, us_cts, &diag, &tv);
7504 			rack_log_to_start(rack, cts, hpts_timeout, slot, 1);
7505 		}
7506 	} else if (hpts_timeout) {
7507 		/*
7508 		 * With respect to t_flags2(?) here, lets let any new acks wake
7509 		 * us up here. Since we are not pacing (no pacing timer), output
7510 		 * can happen so we should let it. If its a Rack timer, then any inbound
7511 		 * packet probably won't change the sending (we will be blocked)
7512 		 * but it may change the prr stats so letting it in (the set defaults
7513 		 * at the start of this block) are good enough.
7514 		 */
7515 		rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
7516 		(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(hpts_timeout),
7517 					   __LINE__, &diag);
7518 		rack_log_hpts_diag(rack, us_cts, &diag, &tv);
7519 		rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
7520 	} else {
7521 		/* No timer starting */
7522 #ifdef INVARIANTS
7523 		if (SEQ_GT(tp->snd_max, tp->snd_una)) {
7524 			panic("tp:%p rack:%p tlts:%d cts:%u slot:%u pto:%u -- no timer started?",
7525 			    tp, rack, tot_len_this_send, cts, slot, hpts_timeout);
7526 		}
7527 #endif
7528 	}
7529 	rack->rc_tmr_stopped = 0;
7530 	if (slot)
7531 		rack_log_type_bbrsnd(rack, tot_len_this_send, slot, us_cts, &tv, __LINE__);
7532 }
7533 
7534 static void
7535 rack_mark_lost(struct tcpcb *tp,
7536     struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t cts)
7537 {
7538 	struct rack_sendmap *nrsm;
7539 	uint32_t thresh,  exp;
7540 
7541 	thresh = rack_calc_thresh_rack(rack, rack_grab_rtt(tp, rack), cts, __LINE__, 0);
7542 	nrsm = rsm;
7543 	TAILQ_FOREACH_FROM(nrsm, &rack->r_ctl.rc_tmap, r_tnext) {
7544 		if ((nrsm->r_flags & RACK_SACK_PASSED) == 0) {
7545 			/* Got up to all that were marked sack-passed */
7546 			break;
7547 		}
7548 		if ((nrsm->r_flags & RACK_WAS_LOST) == 0) {
7549 			exp = ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]) + thresh;
7550 			if (TSTMP_LT(exp, cts) || (exp == cts)) {
7551 				/* We now consider it lost */
7552 				nrsm->r_flags |= RACK_WAS_LOST;
7553 				rack->r_ctl.rc_considered_lost += nrsm->r_end - nrsm->r_start;
7554 			} else {
7555 				/* Past here it won't be lost so stop */
7556 				break;
7557 			}
7558 		}
7559 	}
7560 }
7561 
7562 /*
7563  * RACK Timer, here we simply do logging and house keeping.
7564  * the normal rack_output() function will call the
7565  * appropriate thing to check if we need to do a RACK retransmit.
7566  * We return 1, saying don't proceed with rack_output only
7567  * when all timers have been stopped (destroyed PCB?).
7568  */
7569 static int
7570 rack_timeout_rack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
7571 {
7572 	/*
7573 	 * This timer simply provides an internal trigger to send out data.
7574 	 * The check_recovery_mode call will see if there are needed
7575 	 * retransmissions, if so we will enter fast-recovery. The output
7576 	 * call may or may not do the same thing depending on sysctl
7577 	 * settings.
7578 	 */
7579 	struct rack_sendmap *rsm;
7580 
7581 	counter_u64_add(rack_to_tot, 1);
7582 	if (rack->r_state && (rack->r_state != tp->t_state))
7583 		rack_set_state(tp, rack);
7584 	rack->rc_on_min_to = 0;
7585 	rsm = rack_check_recovery_mode(tp, cts);
7586 	rack_log_to_event(rack, RACK_TO_FRM_RACK, rsm);
7587 	if (rsm) {
7588 		/* We need to stroke any lost that are now declared as lost */
7589 		rack_mark_lost(tp, rack, rsm, cts);
7590 		rack->r_ctl.rc_resend = rsm;
7591 		rack->r_timer_override = 1;
7592 		if (rack->use_rack_rr) {
7593 			/*
7594 			 * Don't accumulate extra pacing delay
7595 			 * we are allowing the rack timer to
7596 			 * over-ride pacing i.e. rrr takes precedence
7597 			 * if the pacing interval is longer than the rrr
7598 			 * time (in other words we get the min pacing
7599 			 * time versus rrr pacing time).
7600 			 */
7601 			rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
7602 		}
7603 	}
7604 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK;
7605 	if (rsm == NULL) {
7606 		/* restart a timer and return 1 */
7607 		rack_start_hpts_timer(rack, tp, cts,
7608 				      0, 0, 0);
7609 		return (1);
7610 	}
7611 	if ((rack->policer_detect_on == 1) &&
7612 	    (rack->rc_policer_detected == 0)) {
7613 		/*
7614 		 * We do this early if we have not
7615 		 * deteceted to attempt to detect
7616 		 * quicker. Normally we want to do this
7617 		 * as recovery exits (and we will again).
7618 		 */
7619 		policer_detection(tp, rack, 0);
7620 	}
7621 	return (0);
7622 }
7623 
7624 
7625 
7626 static void
7627 rack_adjust_orig_mlen(struct rack_sendmap *rsm)
7628 {
7629 
7630 	if ((M_TRAILINGROOM(rsm->m) != rsm->orig_t_space)) {
7631 		/*
7632 		 * The trailing space changed, mbufs can grow
7633 		 * at the tail but they can't shrink from
7634 		 * it, KASSERT that. Adjust the orig_m_len to
7635 		 * compensate for this change.
7636 		 */
7637 		KASSERT((rsm->orig_t_space > M_TRAILINGROOM(rsm->m)),
7638 			("mbuf:%p rsm:%p trailing_space:%jd ots:%u oml:%u mlen:%u\n",
7639 			 rsm->m,
7640 			 rsm,
7641 			 (intmax_t)M_TRAILINGROOM(rsm->m),
7642 			 rsm->orig_t_space,
7643 			 rsm->orig_m_len,
7644 			 rsm->m->m_len));
7645 		rsm->orig_m_len += (rsm->orig_t_space - M_TRAILINGROOM(rsm->m));
7646 		rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
7647 	}
7648 	if (rsm->m->m_len < rsm->orig_m_len) {
7649 		/*
7650 		 * Mbuf shrank, trimmed off the top by an ack, our
7651 		 * offset changes.
7652 		 */
7653 		KASSERT((rsm->soff >= (rsm->orig_m_len - rsm->m->m_len)),
7654 			("mbuf:%p len:%u rsm:%p oml:%u soff:%u\n",
7655 			 rsm->m, rsm->m->m_len,
7656 			 rsm, rsm->orig_m_len,
7657 			 rsm->soff));
7658 		if (rsm->soff >= (rsm->orig_m_len - rsm->m->m_len))
7659 			rsm->soff -= (rsm->orig_m_len - rsm->m->m_len);
7660 		else
7661 			rsm->soff = 0;
7662 		rsm->orig_m_len = rsm->m->m_len;
7663 #ifdef INVARIANTS
7664 	} else if (rsm->m->m_len > rsm->orig_m_len) {
7665 		panic("rsm:%p m:%p m_len grew outside of t_space compensation",
7666 		      rsm, rsm->m);
7667 #endif
7668 	}
7669 }
7670 
7671 static void
7672 rack_setup_offset_for_rsm(struct tcp_rack *rack, struct rack_sendmap *src_rsm, struct rack_sendmap *rsm)
7673 {
7674 	struct mbuf *m;
7675 	uint32_t soff;
7676 
7677 	if (src_rsm->m &&
7678 	    ((src_rsm->orig_m_len != src_rsm->m->m_len) ||
7679 	     (M_TRAILINGROOM(src_rsm->m) != src_rsm->orig_t_space))) {
7680 		/* Fix up the orig_m_len and possibly the mbuf offset */
7681 		rack_adjust_orig_mlen(src_rsm);
7682 	}
7683 	m = src_rsm->m;
7684 	soff = src_rsm->soff + (src_rsm->r_end - src_rsm->r_start);
7685 	while (soff >= m->m_len) {
7686 		/* Move out past this mbuf */
7687 		soff -= m->m_len;
7688 		m = m->m_next;
7689 		KASSERT((m != NULL),
7690 			("rsm:%p nrsm:%p hit at soff:%u null m",
7691 			 src_rsm, rsm, soff));
7692 		if (m == NULL) {
7693 			/* This should *not* happen which is why there is a kassert */
7694 			src_rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd,
7695 					       (src_rsm->r_start - rack->rc_tp->snd_una),
7696 					       &src_rsm->soff);
7697 			src_rsm->orig_m_len = src_rsm->m->m_len;
7698 			src_rsm->orig_t_space = M_TRAILINGROOM(src_rsm->m);
7699 			rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd,
7700 					   (rsm->r_start - rack->rc_tp->snd_una),
7701 					   &rsm->soff);
7702 			rsm->orig_m_len = rsm->m->m_len;
7703 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
7704 			return;
7705 		}
7706 	}
7707 	rsm->m = m;
7708 	rsm->soff = soff;
7709 	rsm->orig_m_len = m->m_len;
7710 	rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
7711 }
7712 
7713 static __inline void
7714 rack_clone_rsm(struct tcp_rack *rack, struct rack_sendmap *nrsm,
7715 	       struct rack_sendmap *rsm, uint32_t start)
7716 {
7717 	int idx;
7718 
7719 	nrsm->r_start = start;
7720 	nrsm->r_end = rsm->r_end;
7721 	nrsm->r_rtr_cnt = rsm->r_rtr_cnt;
7722 	nrsm->r_act_rxt_cnt = rsm->r_act_rxt_cnt;
7723 	nrsm->r_flags = rsm->r_flags;
7724 	nrsm->r_dupack = rsm->r_dupack;
7725 	nrsm->r_no_rtt_allowed = rsm->r_no_rtt_allowed;
7726 	nrsm->r_rtr_bytes = 0;
7727 	nrsm->r_fas = rsm->r_fas;
7728 	nrsm->r_bas = rsm->r_bas;
7729 	tqhash_update_end(rack->r_ctl.tqh, rsm, nrsm->r_start);
7730 	nrsm->r_just_ret = rsm->r_just_ret;
7731 	for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) {
7732 		nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx];
7733 	}
7734 	/* Now if we have SYN flag we keep it on the left edge */
7735 	if (nrsm->r_flags & RACK_HAS_SYN)
7736 		nrsm->r_flags &= ~RACK_HAS_SYN;
7737 	/* Now if we have a FIN flag we keep it on the right edge */
7738 	if (rsm->r_flags & RACK_HAS_FIN)
7739 		rsm->r_flags &= ~RACK_HAS_FIN;
7740 	/* Push bit must go to the right edge as well */
7741 	if (rsm->r_flags & RACK_HAD_PUSH)
7742 		rsm->r_flags &= ~RACK_HAD_PUSH;
7743 	/* Clone over the state of the hw_tls flag */
7744 	nrsm->r_hw_tls = rsm->r_hw_tls;
7745 	/*
7746 	 * Now we need to find nrsm's new location in the mbuf chain
7747 	 * we basically calculate a new offset, which is soff +
7748 	 * how much is left in original rsm. Then we walk out the mbuf
7749 	 * chain to find the righ position, it may be the same mbuf
7750 	 * or maybe not.
7751 	 */
7752 	KASSERT(((rsm->m != NULL) ||
7753 		 (rsm->r_flags & (RACK_HAS_SYN|RACK_HAS_FIN))),
7754 		("rsm:%p nrsm:%p rack:%p -- rsm->m is NULL?", rsm, nrsm, rack));
7755 	if (rsm->m)
7756 		rack_setup_offset_for_rsm(rack, rsm, nrsm);
7757 }
7758 
7759 static struct rack_sendmap *
7760 rack_merge_rsm(struct tcp_rack *rack,
7761 	       struct rack_sendmap *l_rsm,
7762 	       struct rack_sendmap *r_rsm)
7763 {
7764 	/*
7765 	 * We are merging two ack'd RSM's,
7766 	 * the l_rsm is on the left (lower seq
7767 	 * values) and the r_rsm is on the right
7768 	 * (higher seq value). The simplest way
7769 	 * to merge these is to move the right
7770 	 * one into the left. I don't think there
7771 	 * is any reason we need to try to find
7772 	 * the oldest (or last oldest retransmitted).
7773 	 */
7774 	rack_log_map_chg(rack->rc_tp, rack, NULL,
7775 			 l_rsm, r_rsm, MAP_MERGE, r_rsm->r_end, __LINE__);
7776 	tqhash_update_end(rack->r_ctl.tqh, l_rsm, r_rsm->r_end);
7777 	if (l_rsm->r_dupack < r_rsm->r_dupack)
7778 		l_rsm->r_dupack = r_rsm->r_dupack;
7779 	if (r_rsm->r_rtr_bytes)
7780 		l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes;
7781 	if (r_rsm->r_in_tmap) {
7782 		/* This really should not happen */
7783 		TAILQ_REMOVE(&rack->r_ctl.rc_tmap, r_rsm, r_tnext);
7784 		r_rsm->r_in_tmap = 0;
7785 	}
7786 
7787 	/* Now the flags */
7788 	if (r_rsm->r_flags & RACK_HAS_FIN)
7789 		l_rsm->r_flags |= RACK_HAS_FIN;
7790 	if (r_rsm->r_flags & RACK_TLP)
7791 		l_rsm->r_flags |= RACK_TLP;
7792 	if (r_rsm->r_flags & RACK_RWND_COLLAPSED)
7793 		l_rsm->r_flags |= RACK_RWND_COLLAPSED;
7794 	if ((r_rsm->r_flags & RACK_APP_LIMITED)  &&
7795 	    ((l_rsm->r_flags & RACK_APP_LIMITED) == 0)) {
7796 		/*
7797 		 * If both are app-limited then let the
7798 		 * free lower the count. If right is app
7799 		 * limited and left is not, transfer.
7800 		 */
7801 		l_rsm->r_flags |= RACK_APP_LIMITED;
7802 		r_rsm->r_flags &= ~RACK_APP_LIMITED;
7803 		if (r_rsm == rack->r_ctl.rc_first_appl)
7804 			rack->r_ctl.rc_first_appl = l_rsm;
7805 	}
7806 	tqhash_remove(rack->r_ctl.tqh, r_rsm, REMOVE_TYPE_MERGE);
7807 	/*
7808 	 * We keep the largest value, which is the newest
7809 	 * send. We do this in case a segment that is
7810 	 * joined together and not part of a GP estimate
7811 	 * later gets expanded into the GP estimate.
7812 	 *
7813 	 * We prohibit the merging of unlike kinds i.e.
7814 	 * all pieces that are in the GP estimate can be
7815 	 * merged and all pieces that are not in a GP estimate
7816 	 * can be merged, but not disimilar pieces. Combine
7817 	 * this with taking the highest here and we should
7818 	 * be ok unless of course the client reneges. Then
7819 	 * all bets are off.
7820 	 */
7821 	if(l_rsm->r_tim_lastsent[(l_rsm->r_rtr_cnt-1)] <
7822 	   r_rsm->r_tim_lastsent[(r_rsm->r_rtr_cnt-1)]) {
7823 		l_rsm->r_tim_lastsent[(l_rsm->r_rtr_cnt-1)] = r_rsm->r_tim_lastsent[(r_rsm->r_rtr_cnt-1)];
7824 	}
7825 	/*
7826 	 * When merging two RSM's we also need to consider the ack time and keep
7827 	 * newest. If the ack gets merged into a measurement then that is the
7828 	 * one we will want to be using.
7829 	 */
7830 	if(l_rsm->r_ack_arrival	 < r_rsm->r_ack_arrival)
7831 		l_rsm->r_ack_arrival = r_rsm->r_ack_arrival;
7832 
7833 	if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) {
7834 		/* Transfer the split limit to the map we free */
7835 		r_rsm->r_limit_type = l_rsm->r_limit_type;
7836 		l_rsm->r_limit_type = 0;
7837 	}
7838 	rack_free(rack, r_rsm);
7839 	l_rsm->r_flags |= RACK_MERGED;
7840 	return (l_rsm);
7841 }
7842 
7843 /*
7844  * TLP Timer, here we simply setup what segment we want to
7845  * have the TLP expire on, the normal rack_output() will then
7846  * send it out.
7847  *
7848  * We return 1, saying don't proceed with rack_output only
7849  * when all timers have been stopped (destroyed PCB?).
7850  */
7851 static int
7852 rack_timeout_tlp(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t *doing_tlp)
7853 {
7854 	/*
7855 	 * Tail Loss Probe.
7856 	 */
7857 	struct rack_sendmap *rsm = NULL;
7858 	int insret __diagused;
7859 	struct socket *so = tptosocket(tp);
7860 	uint32_t amm;
7861 	uint32_t out, avail;
7862 	int collapsed_win = 0;
7863 
7864 	if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
7865 		/* Its not time yet */
7866 		return (0);
7867 	}
7868 	if (ctf_progress_timeout_check(tp, true)) {
7869 		rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
7870 		return (-ETIMEDOUT);	/* tcp_drop() */
7871 	}
7872 	/*
7873 	 * A TLP timer has expired. We have been idle for 2 rtts. So we now
7874 	 * need to figure out how to force a full MSS segment out.
7875 	 */
7876 	rack_log_to_event(rack, RACK_TO_FRM_TLP, NULL);
7877 	rack->r_ctl.retran_during_recovery = 0;
7878 	rack->r_might_revert = 0;
7879 	rack->r_ctl.dsack_byte_cnt = 0;
7880 	counter_u64_add(rack_tlp_tot, 1);
7881 	if (rack->r_state && (rack->r_state != tp->t_state))
7882 		rack_set_state(tp, rack);
7883 	avail = sbavail(&so->so_snd);
7884 	out = tp->snd_max - tp->snd_una;
7885 	if ((out > tp->snd_wnd) || rack->rc_has_collapsed) {
7886 		/* special case, we need a retransmission */
7887 		collapsed_win = 1;
7888 		goto need_retran;
7889 	}
7890 	if (rack->r_ctl.dsack_persist && (rack->r_ctl.rc_tlp_cnt_out >= 1)) {
7891 		rack->r_ctl.dsack_persist--;
7892 		if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
7893 			rack->r_ctl.num_dsack = 0;
7894 		}
7895 		rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
7896 	}
7897 	if ((tp->t_flags & TF_GPUTINPROG) &&
7898 	    (rack->r_ctl.rc_tlp_cnt_out == 1)) {
7899 		/*
7900 		 * If this is the second in a row
7901 		 * TLP and we are doing a measurement
7902 		 * its time to abandon the measurement.
7903 		 * Something is likely broken on
7904 		 * the clients network and measuring a
7905 		 * broken network does us no good.
7906 		 */
7907 		tp->t_flags &= ~TF_GPUTINPROG;
7908 		rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
7909 					   rack->r_ctl.rc_gp_srtt /*flex1*/,
7910 					   tp->gput_seq,
7911 					   0, 0, 18, __LINE__, NULL, 0);
7912 	}
7913 	/*
7914 	 * Check our send oldest always settings, and if
7915 	 * there is an oldest to send jump to the need_retran.
7916 	 */
7917 	if (rack_always_send_oldest && (TAILQ_EMPTY(&rack->r_ctl.rc_tmap) == 0))
7918 		goto need_retran;
7919 
7920 	if (avail > out) {
7921 		/* New data is available */
7922 		amm = avail - out;
7923 		if (amm > ctf_fixed_maxseg(tp)) {
7924 			amm = ctf_fixed_maxseg(tp);
7925 			if ((amm + out) > tp->snd_wnd) {
7926 				/* We are rwnd limited */
7927 				goto need_retran;
7928 			}
7929 		} else if (amm < ctf_fixed_maxseg(tp)) {
7930 			/* not enough to fill a MTU */
7931 			goto need_retran;
7932 		}
7933 		if (IN_FASTRECOVERY(tp->t_flags)) {
7934 			/* Unlikely */
7935 			if (rack->rack_no_prr == 0) {
7936 				if (out + amm <= tp->snd_wnd) {
7937 					rack->r_ctl.rc_prr_sndcnt = amm;
7938 					rack->r_ctl.rc_tlp_new_data = amm;
7939 					rack_log_to_prr(rack, 4, 0, __LINE__);
7940 				}
7941 			} else
7942 				goto need_retran;
7943 		} else {
7944 			/* Set the send-new override */
7945 			if (out + amm <= tp->snd_wnd)
7946 				rack->r_ctl.rc_tlp_new_data = amm;
7947 			else
7948 				goto need_retran;
7949 		}
7950 		rack->r_ctl.rc_tlpsend = NULL;
7951 		counter_u64_add(rack_tlp_newdata, 1);
7952 		goto send;
7953 	}
7954 need_retran:
7955 	/*
7956 	 * Ok we need to arrange the last un-acked segment to be re-sent, or
7957 	 * optionally the first un-acked segment.
7958 	 */
7959 	if (collapsed_win == 0) {
7960 		if (rack_always_send_oldest)
7961 			rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
7962 		else {
7963 			rsm = tqhash_max(rack->r_ctl.tqh);
7964 			if (rsm && (rsm->r_flags & (RACK_ACKED | RACK_HAS_FIN))) {
7965 				rsm = rack_find_high_nonack(rack, rsm);
7966 			}
7967 		}
7968 		if (rsm == NULL) {
7969 #ifdef TCP_BLACKBOX
7970 			tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true);
7971 #endif
7972 			goto out;
7973 		}
7974 	} else {
7975 		/*
7976 		 * We had a collapsed window, lets find
7977 		 * the point before the collapse.
7978 		 */
7979 		if (SEQ_GT((rack->r_ctl.last_collapse_point - 1), rack->rc_tp->snd_una))
7980 			rsm = tqhash_find(rack->r_ctl.tqh, (rack->r_ctl.last_collapse_point - 1));
7981 		else {
7982 			rsm = tqhash_min(rack->r_ctl.tqh);
7983 		}
7984 		if (rsm == NULL) {
7985 			/* Huh */
7986 			goto out;
7987 		}
7988 	}
7989 	if ((rsm->r_end - rsm->r_start) > ctf_fixed_maxseg(tp)) {
7990 		/*
7991 		 * We need to split this the last segment in two.
7992 		 */
7993 		struct rack_sendmap *nrsm;
7994 
7995 		nrsm = rack_alloc_full_limit(rack);
7996 		if (nrsm == NULL) {
7997 			/*
7998 			 * No memory to split, we will just exit and punt
7999 			 * off to the RXT timer.
8000 			 */
8001 			goto out;
8002 		}
8003 		rack_clone_rsm(rack, nrsm, rsm,
8004 			       (rsm->r_end - ctf_fixed_maxseg(tp)));
8005 		rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
8006 #ifndef INVARIANTS
8007 		(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
8008 #else
8009 		if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
8010 			panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p",
8011 			      nrsm, insret, rack, rsm);
8012 		}
8013 #endif
8014 		if (rsm->r_in_tmap) {
8015 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8016 			nrsm->r_in_tmap = 1;
8017 		}
8018 		rsm = nrsm;
8019 	}
8020 	rack->r_ctl.rc_tlpsend = rsm;
8021 send:
8022 	/* Make sure output path knows we are doing a TLP */
8023 	*doing_tlp = 1;
8024 	rack->r_timer_override = 1;
8025 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
8026 	return (0);
8027 out:
8028 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
8029 	return (0);
8030 }
8031 
8032 /*
8033  * Delayed ack Timer, here we simply need to setup the
8034  * ACK_NOW flag and remove the DELACK flag. From there
8035  * the output routine will send the ack out.
8036  *
8037  * We only return 1, saying don't proceed, if all timers
8038  * are stopped (destroyed PCB?).
8039  */
8040 static int
8041 rack_timeout_delack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
8042 {
8043 
8044 	rack_log_to_event(rack, RACK_TO_FRM_DELACK, NULL);
8045 	tp->t_flags &= ~TF_DELACK;
8046 	tp->t_flags |= TF_ACKNOW;
8047 	KMOD_TCPSTAT_INC(tcps_delack);
8048 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
8049 	return (0);
8050 }
8051 
8052 static inline int
8053 rack_send_ack_challange(struct tcp_rack *rack)
8054 {
8055 	struct tcptemp *t_template;
8056 
8057 	t_template = tcpip_maketemplate(rack->rc_inp);
8058 	if (t_template) {
8059 		if (rack->forced_ack == 0) {
8060 			rack->forced_ack = 1;
8061 			rack->r_ctl.forced_ack_ts = tcp_get_usecs(NULL);
8062 		} else {
8063 			rack->probe_not_answered = 1;
8064 		}
8065 		tcp_respond(rack->rc_tp, t_template->tt_ipgen,
8066 			    &t_template->tt_t, (struct mbuf *)NULL,
8067 			    rack->rc_tp->rcv_nxt, rack->rc_tp->snd_una - 1, 0);
8068 		free(t_template, M_TEMP);
8069 		/* This does send an ack so kill any D-ack timer */
8070 		if (rack->rc_tp->t_flags & TF_DELACK)
8071 			rack->rc_tp->t_flags &= ~TF_DELACK;
8072 		return(1);
8073 	} else
8074 		return (0);
8075 
8076 }
8077 
8078 /*
8079  * Persists timer, here we simply send the
8080  * same thing as a keepalive will.
8081  * the one byte send.
8082  *
8083  * We only return 1, saying don't proceed, if all timers
8084  * are stopped (destroyed PCB?).
8085  */
8086 static int
8087 rack_timeout_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
8088 {
8089 	int32_t retval = 1;
8090 
8091 	if (rack->rc_in_persist == 0)
8092 		return (0);
8093 	if (ctf_progress_timeout_check(tp, false)) {
8094 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
8095 		rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
8096 		counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
8097 		return (-ETIMEDOUT);	/* tcp_drop() */
8098 	}
8099 	/*
8100 	 * Persistence timer into zero window. Force a byte to be output, if
8101 	 * possible.
8102 	 */
8103 	KMOD_TCPSTAT_INC(tcps_persisttimeo);
8104 	/*
8105 	 * Hack: if the peer is dead/unreachable, we do not time out if the
8106 	 * window is closed.  After a full backoff, drop the connection if
8107 	 * the idle time (no responses to probes) reaches the maximum
8108 	 * backoff that we would use if retransmitting.
8109 	 */
8110 	if (tp->t_rxtshift >= V_tcp_retries &&
8111 	    (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
8112 	     TICKS_2_USEC(ticks - tp->t_rcvtime) >= RACK_REXMTVAL(tp) * tcp_totbackoff)) {
8113 		KMOD_TCPSTAT_INC(tcps_persistdrop);
8114 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
8115 		counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
8116 		retval = -ETIMEDOUT;	/* tcp_drop() */
8117 		goto out;
8118 	}
8119 	if ((sbavail(&rack->rc_inp->inp_socket->so_snd) == 0) &&
8120 	    tp->snd_una == tp->snd_max)
8121 		rack_exit_persist(tp, rack, cts);
8122 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT;
8123 	/*
8124 	 * If the user has closed the socket then drop a persisting
8125 	 * connection after a much reduced timeout.
8126 	 */
8127 	if (tp->t_state > TCPS_CLOSE_WAIT &&
8128 	    (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
8129 		KMOD_TCPSTAT_INC(tcps_persistdrop);
8130 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
8131 		counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
8132 		retval = -ETIMEDOUT;	/* tcp_drop() */
8133 		goto out;
8134 	}
8135 	if (rack_send_ack_challange(rack)) {
8136 		/* only set it if we were answered */
8137 		if (rack->probe_not_answered) {
8138 			counter_u64_add(rack_persists_loss, 1);
8139 			rack->r_ctl.persist_lost_ends++;
8140 		}
8141 		counter_u64_add(rack_persists_sends, 1);
8142 		counter_u64_add(rack_out_size[TCP_MSS_ACCT_PERSIST], 1);
8143 	}
8144 	if (tp->t_rxtshift < V_tcp_retries)
8145 		tp->t_rxtshift++;
8146 out:
8147 	rack_log_to_event(rack, RACK_TO_FRM_PERSIST, NULL);
8148 	rack_start_hpts_timer(rack, tp, cts,
8149 			      0, 0, 0);
8150 	return (retval);
8151 }
8152 
8153 /*
8154  * If a keepalive goes off, we had no other timers
8155  * happening. We always return 1 here since this
8156  * routine either drops the connection or sends
8157  * out a segment with respond.
8158  */
8159 static int
8160 rack_timeout_keepalive(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
8161 {
8162 	struct inpcb *inp = tptoinpcb(tp);
8163 
8164 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP;
8165 	rack_log_to_event(rack, RACK_TO_FRM_KEEP, NULL);
8166 	/*
8167 	 * Keep-alive timer went off; send something or drop connection if
8168 	 * idle for too long.
8169 	 */
8170 	KMOD_TCPSTAT_INC(tcps_keeptimeo);
8171 	if (tp->t_state < TCPS_ESTABLISHED)
8172 		goto dropit;
8173 	if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
8174 	    tp->t_state <= TCPS_CLOSING) {
8175 		if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
8176 			goto dropit;
8177 		/*
8178 		 * Send a packet designed to force a response if the peer is
8179 		 * up and reachable: either an ACK if the connection is
8180 		 * still alive, or an RST if the peer has closed the
8181 		 * connection due to timeout or reboot. Using sequence
8182 		 * number tp->snd_una-1 causes the transmitted zero-length
8183 		 * segment to lie outside the receive window; by the
8184 		 * protocol spec, this requires the correspondent TCP to
8185 		 * respond.
8186 		 */
8187 		KMOD_TCPSTAT_INC(tcps_keepprobe);
8188 		rack_send_ack_challange(rack);
8189 	}
8190 	rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
8191 	return (1);
8192 dropit:
8193 	KMOD_TCPSTAT_INC(tcps_keepdrops);
8194 	tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
8195 	return (-ETIMEDOUT);	/* tcp_drop() */
8196 }
8197 
8198 /*
8199  * Retransmit helper function, clear up all the ack
8200  * flags and take care of important book keeping.
8201  */
8202 static void
8203 rack_remxt_tmr(struct tcpcb *tp)
8204 {
8205 	/*
8206 	 * The retransmit timer went off, all sack'd blocks must be
8207 	 * un-acked.
8208 	 */
8209 	struct rack_sendmap *rsm, *trsm = NULL;
8210 	struct tcp_rack *rack;
8211 
8212 	rack = (struct tcp_rack *)tp->t_fb_ptr;
8213 	rack_timer_cancel(tp, rack, tcp_get_usecs(NULL), __LINE__);
8214 	rack_log_to_event(rack, RACK_TO_FRM_TMR, NULL);
8215 	rack->r_timer_override = 1;
8216 	rack->r_ctl.rc_snd_max_at_rto = tp->snd_max;
8217 	rack->r_ctl.rc_last_timeout_snduna = tp->snd_una;
8218 	rack->r_late = 0;
8219 	rack->r_early = 0;
8220 	rack->r_ctl.rc_agg_delayed = 0;
8221 	rack->r_ctl.rc_agg_early = 0;
8222 	if (rack->r_state && (rack->r_state != tp->t_state))
8223 		rack_set_state(tp, rack);
8224 	if (tp->t_rxtshift <= rack_rxt_scoreboard_clear_thresh) {
8225 		/*
8226 		 * We do not clear the scoreboard until we have had
8227 		 * more than rack_rxt_scoreboard_clear_thresh time-outs.
8228 		 */
8229 		rack->r_ctl.rc_resend = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
8230 		if (rack->r_ctl.rc_resend != NULL)
8231 			rack->r_ctl.rc_resend->r_flags |= RACK_TO_REXT;
8232 
8233 		return;
8234 	}
8235 	/*
8236 	 * Ideally we would like to be able to
8237 	 * mark SACK-PASS on anything not acked here.
8238 	 *
8239 	 * However, if we do that we would burst out
8240 	 * all that data 1ms apart. This would be unwise,
8241 	 * so for now we will just let the normal rxt timer
8242 	 * and tlp timer take care of it.
8243 	 *
8244 	 * Also we really need to stick them back in sequence
8245 	 * order. This way we send in the proper order and any
8246 	 * sacks that come floating in will "re-ack" the data.
8247 	 * To do this we zap the tmap with an INIT and then
8248 	 * walk through and place every rsm in the tail queue
8249 	 * hash table back in its seq ordered place.
8250 	 */
8251 	TAILQ_INIT(&rack->r_ctl.rc_tmap);
8252 
8253 	TQHASH_FOREACH(rsm, rack->r_ctl.tqh)  {
8254 		rsm->r_dupack = 0;
8255 		if (rack_verbose_logging)
8256 			rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
8257 		/* We must re-add it back to the tlist */
8258 		if (trsm == NULL) {
8259 			TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8260 		} else {
8261 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, trsm, rsm, r_tnext);
8262 		}
8263 		rsm->r_in_tmap = 1;
8264 		trsm = rsm;
8265 		if (rsm->r_flags & RACK_ACKED)
8266 			rsm->r_flags |= RACK_WAS_ACKED;
8267 		rsm->r_flags &= ~(RACK_ACKED | RACK_SACK_PASSED | RACK_WAS_SACKPASS | RACK_RWND_COLLAPSED | RACK_WAS_LOST);
8268 		rsm->r_flags |= RACK_MUST_RXT;
8269 	}
8270 	/* zero the lost since it's all gone */
8271 	rack->r_ctl.rc_considered_lost = 0;
8272 	/* Clear the count (we just un-acked them) */
8273 	rack->r_ctl.rc_sacked = 0;
8274 	rack->r_ctl.rc_sacklast = NULL;
8275 	/* Clear the tlp rtx mark */
8276 	rack->r_ctl.rc_resend = tqhash_min(rack->r_ctl.tqh);
8277 	if (rack->r_ctl.rc_resend != NULL)
8278 		rack->r_ctl.rc_resend->r_flags |= RACK_TO_REXT;
8279 	rack->r_ctl.rc_prr_sndcnt = 0;
8280 	rack_log_to_prr(rack, 6, 0, __LINE__);
8281 	rack->r_ctl.rc_resend = tqhash_min(rack->r_ctl.tqh);
8282 	if (rack->r_ctl.rc_resend != NULL)
8283 		rack->r_ctl.rc_resend->r_flags |= RACK_TO_REXT;
8284 	if ((((tp->t_flags & TF_SACK_PERMIT) == 0)
8285 #ifdef TCP_SAD_DETECTION
8286 	     || (rack->sack_attack_disable != 0)
8287 #endif
8288 		    ) && ((tp->t_flags & TF_SENTFIN) == 0)) {
8289 		/*
8290 		 * For non-sack customers new data
8291 		 * needs to go out as retransmits until
8292 		 * we retransmit up to snd_max.
8293 		 */
8294 		rack->r_must_retran = 1;
8295 		rack->r_ctl.rc_out_at_rto = ctf_flight_size(rack->rc_tp,
8296 							    rack->r_ctl.rc_sacked);
8297 	}
8298 }
8299 
8300 static void
8301 rack_convert_rtts(struct tcpcb *tp)
8302 {
8303 	tcp_change_time_units(tp, TCP_TMR_GRANULARITY_USEC);
8304 	tp->t_rxtcur = RACK_REXMTVAL(tp);
8305 	if (TCPS_HAVEESTABLISHED(tp->t_state)) {
8306 		tp->t_rxtcur += TICKS_2_USEC(tcp_rexmit_slop);
8307 	}
8308 	if (tp->t_rxtcur > rack_rto_max) {
8309 		tp->t_rxtcur = rack_rto_max;
8310 	}
8311 }
8312 
8313 static void
8314 rack_cc_conn_init(struct tcpcb *tp)
8315 {
8316 	struct tcp_rack *rack;
8317 	uint32_t srtt;
8318 
8319 	rack = (struct tcp_rack *)tp->t_fb_ptr;
8320 	srtt = tp->t_srtt;
8321 	cc_conn_init(tp);
8322 	/*
8323 	 * Now convert to rack's internal format,
8324 	 * if required.
8325 	 */
8326 	if ((srtt == 0) && (tp->t_srtt != 0))
8327 		rack_convert_rtts(tp);
8328 	/*
8329 	 * We want a chance to stay in slowstart as
8330 	 * we create a connection. TCP spec says that
8331 	 * initially ssthresh is infinite. For our
8332 	 * purposes that is the snd_wnd.
8333 	 */
8334 	if (tp->snd_ssthresh < tp->snd_wnd) {
8335 		tp->snd_ssthresh = tp->snd_wnd;
8336 	}
8337 	/*
8338 	 * We also want to assure a IW worth of
8339 	 * data can get inflight.
8340 	 */
8341 	if (rc_init_window(rack) < tp->snd_cwnd)
8342 		tp->snd_cwnd = rc_init_window(rack);
8343 }
8344 
8345 /*
8346  * Re-transmit timeout! If we drop the PCB we will return 1, otherwise
8347  * we will setup to retransmit the lowest seq number outstanding.
8348  */
8349 static int
8350 rack_timeout_rxt(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
8351 {
8352 	struct inpcb *inp = tptoinpcb(tp);
8353 	int32_t rexmt;
8354 	int32_t retval = 0;
8355 	bool isipv6;
8356 
8357 	if ((tp->t_flags & TF_GPUTINPROG) &&
8358 	    (tp->t_rxtshift)) {
8359 		/*
8360 		 * We have had a second timeout
8361 		 * measurements on successive rxt's are not profitable.
8362 		 * It is unlikely to be of any use (the network is
8363 		 * broken or the client went away).
8364 		 */
8365 		tp->t_flags &= ~TF_GPUTINPROG;
8366 		rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
8367 					   rack->r_ctl.rc_gp_srtt /*flex1*/,
8368 					   tp->gput_seq,
8369 					   0, 0, 18, __LINE__, NULL, 0);
8370 	}
8371 	if (ctf_progress_timeout_check(tp, false)) {
8372 		tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
8373 		rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
8374 		return (-ETIMEDOUT);	/* tcp_drop() */
8375 	}
8376 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT;
8377 	rack->r_ctl.retran_during_recovery = 0;
8378 	rack->rc_ack_required = 1;
8379 	rack->r_ctl.dsack_byte_cnt = 0;
8380 	if (IN_RECOVERY(tp->t_flags) &&
8381 	    (rack->rto_from_rec == 0)) {
8382 		/*
8383 		 * Mark that we had a rto while in recovery
8384 		 * and save the ssthresh so if we go back
8385 		 * into recovery we will have a chance
8386 		 * to slowstart back to the level.
8387 		 */
8388 		rack->rto_from_rec = 1;
8389 		rack->r_ctl.rto_ssthresh = tp->snd_ssthresh;
8390 	}
8391 	if (IN_FASTRECOVERY(tp->t_flags))
8392 		tp->t_flags |= TF_WASFRECOVERY;
8393 	else
8394 		tp->t_flags &= ~TF_WASFRECOVERY;
8395 	if (IN_CONGRECOVERY(tp->t_flags))
8396 		tp->t_flags |= TF_WASCRECOVERY;
8397 	else
8398 		tp->t_flags &= ~TF_WASCRECOVERY;
8399 	if (TCPS_HAVEESTABLISHED(tp->t_state) &&
8400 	    (tp->snd_una == tp->snd_max)) {
8401 		/* Nothing outstanding .. nothing to do */
8402 		return (0);
8403 	}
8404 	if (rack->r_ctl.dsack_persist) {
8405 		rack->r_ctl.dsack_persist--;
8406 		if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
8407 			rack->r_ctl.num_dsack = 0;
8408 		}
8409 		rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
8410 	}
8411 	/*
8412 	 * Rack can only run one timer  at a time, so we cannot
8413 	 * run a KEEPINIT (gating SYN sending) and a retransmit
8414 	 * timer for the SYN. So if we are in a front state and
8415 	 * have a KEEPINIT timer we need to check the first transmit
8416 	 * against now to see if we have exceeded the KEEPINIT time
8417 	 * (if one is set).
8418 	 */
8419 	if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) &&
8420 	    (TP_KEEPINIT(tp) != 0)) {
8421 		struct rack_sendmap *rsm;
8422 
8423 		rsm = tqhash_min(rack->r_ctl.tqh);
8424 		if (rsm) {
8425 			/* Ok we have something outstanding to test keepinit with */
8426 			if ((TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) &&
8427 			    ((cts - (uint32_t)rsm->r_tim_lastsent[0]) >= TICKS_2_USEC(TP_KEEPINIT(tp)))) {
8428 				/* We have exceeded the KEEPINIT time */
8429 				tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
8430 				goto drop_it;
8431 			}
8432 		}
8433 	}
8434 	/*
8435 	 * Retransmission timer went off.  Message has not been acked within
8436 	 * retransmit interval.  Back off to a longer retransmit interval
8437 	 * and retransmit one segment.
8438 	 */
8439 	if ((rack->r_ctl.rc_resend == NULL) ||
8440 	    ((rack->r_ctl.rc_resend->r_flags & RACK_RWND_COLLAPSED) == 0)) {
8441 		/*
8442 		 * If the rwnd collapsed on
8443 		 * the one we are retransmitting
8444 		 * it does not count against the
8445 		 * rxt count.
8446 		 */
8447 		tp->t_rxtshift++;
8448 	}
8449 	rack_remxt_tmr(tp);
8450 	if (tp->t_rxtshift > V_tcp_retries) {
8451 		tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
8452 drop_it:
8453 		tp->t_rxtshift = V_tcp_retries;
8454 		KMOD_TCPSTAT_INC(tcps_timeoutdrop);
8455 		/* XXXGL: previously t_softerror was casted to uint16_t */
8456 		MPASS(tp->t_softerror >= 0);
8457 		retval = tp->t_softerror ? -tp->t_softerror : -ETIMEDOUT;
8458 		goto out;	/* tcp_drop() */
8459 	}
8460 	if (tp->t_state == TCPS_SYN_SENT) {
8461 		/*
8462 		 * If the SYN was retransmitted, indicate CWND to be limited
8463 		 * to 1 segment in cc_conn_init().
8464 		 */
8465 		tp->snd_cwnd = 1;
8466 	} else if (tp->t_rxtshift == 1) {
8467 		/*
8468 		 * first retransmit; record ssthresh and cwnd so they can be
8469 		 * recovered if this turns out to be a "bad" retransmit. A
8470 		 * retransmit is considered "bad" if an ACK for this segment
8471 		 * is received within RTT/2 interval; the assumption here is
8472 		 * that the ACK was already in flight.  See "On Estimating
8473 		 * End-to-End Network Path Properties" by Allman and Paxson
8474 		 * for more details.
8475 		 */
8476 		tp->snd_cwnd_prev = tp->snd_cwnd;
8477 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
8478 		tp->snd_recover_prev = tp->snd_recover;
8479 		tp->t_badrxtwin = ticks + (USEC_2_TICKS(tp->t_srtt)/2);
8480 		tp->t_flags |= TF_PREVVALID;
8481 	} else if ((tp->t_flags & TF_RCVD_TSTMP) == 0)
8482 		tp->t_flags &= ~TF_PREVVALID;
8483 	KMOD_TCPSTAT_INC(tcps_rexmttimeo);
8484 	if ((tp->t_state == TCPS_SYN_SENT) ||
8485 	    (tp->t_state == TCPS_SYN_RECEIVED))
8486 		rexmt = RACK_INITIAL_RTO * tcp_backoff[tp->t_rxtshift];
8487 	else
8488 		rexmt = max(rack_rto_min, (tp->t_srtt + (tp->t_rttvar << 2))) * tcp_backoff[tp->t_rxtshift];
8489 
8490 	RACK_TCPT_RANGESET(tp->t_rxtcur, rexmt,
8491 	   max(rack_rto_min, rexmt), rack_rto_max, rack->r_ctl.timer_slop);
8492 	/*
8493 	 * We enter the path for PLMTUD if connection is established or, if
8494 	 * connection is FIN_WAIT_1 status, reason for the last is that if
8495 	 * amount of data we send is very small, we could send it in couple
8496 	 * of packets and process straight to FIN. In that case we won't
8497 	 * catch ESTABLISHED state.
8498 	 */
8499 #ifdef INET6
8500 	isipv6 = (inp->inp_vflag & INP_IPV6) ? true : false;
8501 #else
8502 	isipv6 = false;
8503 #endif
8504 	if (((V_tcp_pmtud_blackhole_detect == 1) ||
8505 	    (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) ||
8506 	    (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) &&
8507 	    ((tp->t_state == TCPS_ESTABLISHED) ||
8508 	    (tp->t_state == TCPS_FIN_WAIT_1))) {
8509 		/*
8510 		 * Idea here is that at each stage of mtu probe (usually,
8511 		 * 1448 -> 1188 -> 524) should be given 2 chances to recover
8512 		 * before further clamping down. 'tp->t_rxtshift % 2 == 0'
8513 		 * should take care of that.
8514 		 */
8515 		if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) ==
8516 		    (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) &&
8517 		    (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 &&
8518 		    tp->t_rxtshift % 2 == 0)) {
8519 			/*
8520 			 * Enter Path MTU Black-hole Detection mechanism: -
8521 			 * Disable Path MTU Discovery (IP "DF" bit). -
8522 			 * Reduce MTU to lower value than what we negotiated
8523 			 * with peer.
8524 			 */
8525 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
8526 				/* Record that we may have found a black hole. */
8527 				tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
8528 				/* Keep track of previous MSS. */
8529 				tp->t_pmtud_saved_maxseg = tp->t_maxseg;
8530 			}
8531 
8532 			/*
8533 			 * Reduce the MSS to blackhole value or to the
8534 			 * default in an attempt to retransmit.
8535 			 */
8536 #ifdef INET6
8537 			if (isipv6 &&
8538 			    tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) {
8539 				/* Use the sysctl tuneable blackhole MSS. */
8540 				tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
8541 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
8542 			} else if (isipv6) {
8543 				/* Use the default MSS. */
8544 				tp->t_maxseg = V_tcp_v6mssdflt;
8545 				/*
8546 				 * Disable Path MTU Discovery when we switch
8547 				 * to minmss.
8548 				 */
8549 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
8550 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
8551 			}
8552 #endif
8553 #if defined(INET6) && defined(INET)
8554 			else
8555 #endif
8556 #ifdef INET
8557 			if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) {
8558 				/* Use the sysctl tuneable blackhole MSS. */
8559 				tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
8560 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
8561 			} else {
8562 				/* Use the default MSS. */
8563 				tp->t_maxseg = V_tcp_mssdflt;
8564 				/*
8565 				 * Disable Path MTU Discovery when we switch
8566 				 * to minmss.
8567 				 */
8568 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
8569 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
8570 			}
8571 #endif
8572 		} else {
8573 			/*
8574 			 * If further retransmissions are still unsuccessful
8575 			 * with a lowered MTU, maybe this isn't a blackhole
8576 			 * and we restore the previous MSS and blackhole
8577 			 * detection flags. The limit '6' is determined by
8578 			 * giving each probe stage (1448, 1188, 524) 2
8579 			 * chances to recover.
8580 			 */
8581 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
8582 			    (tp->t_rxtshift >= 6)) {
8583 				tp->t_flags2 |= TF2_PLPMTU_PMTUD;
8584 				tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
8585 				tp->t_maxseg = tp->t_pmtud_saved_maxseg;
8586 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed);
8587 			}
8588 		}
8589 	}
8590 	/*
8591 	 * Disable RFC1323 and SACK if we haven't got any response to
8592 	 * our third SYN to work-around some broken terminal servers
8593 	 * (most of which have hopefully been retired) that have bad VJ
8594 	 * header compression code which trashes TCP segments containing
8595 	 * unknown-to-them TCP options.
8596 	 */
8597 	if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
8598 	    (tp->t_rxtshift == 3))
8599 		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
8600 	/*
8601 	 * If we backed off this far, our srtt estimate is probably bogus.
8602 	 * Clobber it so we'll take the next rtt measurement as our srtt;
8603 	 * move the current srtt into rttvar to keep the current retransmit
8604 	 * times until then.
8605 	 */
8606 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
8607 #ifdef INET6
8608 		if ((inp->inp_vflag & INP_IPV6) != 0)
8609 			in6_losing(inp);
8610 		else
8611 #endif
8612 			in_losing(inp);
8613 		tp->t_rttvar += tp->t_srtt;
8614 		tp->t_srtt = 0;
8615 	}
8616 	sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
8617 	tp->snd_recover = tp->snd_max;
8618 	tp->t_flags |= TF_ACKNOW;
8619 	tp->t_rtttime = 0;
8620 	rack_cong_signal(tp, CC_RTO, tp->snd_una, __LINE__);
8621 out:
8622 	return (retval);
8623 }
8624 
8625 static int
8626 rack_process_timers(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t hpts_calling, uint8_t *doing_tlp)
8627 {
8628 	int32_t ret = 0;
8629 	int32_t timers = (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK);
8630 
8631 	if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
8632 	    (tp->t_flags & TF_GPUTINPROG)) {
8633 		/*
8634 		 * We have a goodput in progress
8635 		 * and we have entered a late state.
8636 		 * Do we have enough data in the sb
8637 		 * to handle the GPUT request?
8638 		 */
8639 		uint32_t bytes;
8640 
8641 		bytes = tp->gput_ack - tp->gput_seq;
8642 		if (SEQ_GT(tp->gput_seq, tp->snd_una))
8643 			bytes += tp->gput_seq - tp->snd_una;
8644 		if (bytes > sbavail(&tptosocket(tp)->so_snd)) {
8645 			/*
8646 			 * There are not enough bytes in the socket
8647 			 * buffer that have been sent to cover this
8648 			 * measurement. Cancel it.
8649 			 */
8650 			rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
8651 						   rack->r_ctl.rc_gp_srtt /*flex1*/,
8652 						   tp->gput_seq,
8653 						   0, 0, 18, __LINE__, NULL, 0);
8654 			tp->t_flags &= ~TF_GPUTINPROG;
8655 		}
8656 	}
8657 	if (timers == 0) {
8658 		return (0);
8659 	}
8660 	if (tp->t_state == TCPS_LISTEN) {
8661 		/* no timers on listen sockets */
8662 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)
8663 			return (0);
8664 		return (1);
8665 	}
8666 	if ((timers & PACE_TMR_RACK) &&
8667 	    rack->rc_on_min_to) {
8668 		/*
8669 		 * For the rack timer when we
8670 		 * are on a min-timeout (which means rrr_conf = 3)
8671 		 * we don't want to check the timer. It may
8672 		 * be going off for a pace and thats ok we
8673 		 * want to send the retransmit (if its ready).
8674 		 *
8675 		 * If its on a normal rack timer (non-min) then
8676 		 * we will check if its expired.
8677 		 */
8678 		goto skip_time_check;
8679 	}
8680 	if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
8681 		uint32_t left;
8682 
8683 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
8684 			ret = -1;
8685 			rack_log_to_processing(rack, cts, ret, 0);
8686 			return (0);
8687 		}
8688 		if (hpts_calling == 0) {
8689 			/*
8690 			 * A user send or queued mbuf (sack) has called us? We
8691 			 * return 0 and let the pacing guards
8692 			 * deal with it if they should or
8693 			 * should not cause a send.
8694 			 */
8695 			ret = -2;
8696 			rack_log_to_processing(rack, cts, ret, 0);
8697 			return (0);
8698 		}
8699 		/*
8700 		 * Ok our timer went off early and we are not paced false
8701 		 * alarm, go back to sleep. We make sure we don't have
8702 		 * no-sack wakeup on since we no longer have a PKT_OUTPUT
8703 		 * flag in place.
8704 		 */
8705 		rack->rc_tp->t_flags2 &= ~TF2_DONT_SACK_QUEUE;
8706 		ret = -3;
8707 		left = rack->r_ctl.rc_timer_exp - cts;
8708 		tcp_hpts_insert(tp, HPTS_MS_TO_SLOTS(left));
8709 		rack_log_to_processing(rack, cts, ret, left);
8710 		return (1);
8711 	}
8712 skip_time_check:
8713 	rack->rc_tmr_stopped = 0;
8714 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK;
8715 	if (timers & PACE_TMR_DELACK) {
8716 		ret = rack_timeout_delack(tp, rack, cts);
8717 	} else if (timers & PACE_TMR_RACK) {
8718 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
8719 		rack->r_fast_output = 0;
8720 		ret = rack_timeout_rack(tp, rack, cts);
8721 	} else if (timers & PACE_TMR_TLP) {
8722 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
8723 		ret = rack_timeout_tlp(tp, rack, cts, doing_tlp);
8724 	} else if (timers & PACE_TMR_RXT) {
8725 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
8726 		rack->r_fast_output = 0;
8727 		ret = rack_timeout_rxt(tp, rack, cts);
8728 	} else if (timers & PACE_TMR_PERSIT) {
8729 		ret = rack_timeout_persist(tp, rack, cts);
8730 	} else if (timers & PACE_TMR_KEEP) {
8731 		ret = rack_timeout_keepalive(tp, rack, cts);
8732 	}
8733 	rack_log_to_processing(rack, cts, ret, timers);
8734 	return (ret);
8735 }
8736 
8737 static void
8738 rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line)
8739 {
8740 	struct timeval tv;
8741 	uint32_t us_cts, flags_on_entry;
8742 	uint8_t hpts_removed = 0;
8743 
8744 	flags_on_entry = rack->r_ctl.rc_hpts_flags;
8745 	us_cts = tcp_get_usecs(&tv);
8746 	if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
8747 	    ((TSTMP_GEQ(us_cts, rack->r_ctl.rc_last_output_to)) ||
8748 	     ((tp->snd_max - tp->snd_una) == 0))) {
8749 		tcp_hpts_remove(rack->rc_tp);
8750 		hpts_removed = 1;
8751 		/* If we were not delayed cancel out the flag. */
8752 		if ((tp->snd_max - tp->snd_una) == 0)
8753 			rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
8754 		rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry);
8755 	}
8756 	if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
8757 		rack->rc_tmr_stopped = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
8758 		if (tcp_in_hpts(rack->rc_tp) &&
8759 		    ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)) {
8760 			/*
8761 			 * Canceling timer's when we have no output being
8762 			 * paced. We also must remove ourselves from the
8763 			 * hpts.
8764 			 */
8765 			tcp_hpts_remove(rack->rc_tp);
8766 			hpts_removed = 1;
8767 		}
8768 		rack->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK);
8769 	}
8770 	if (hpts_removed == 0)
8771 		rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry);
8772 }
8773 
8774 static int
8775 rack_stopall(struct tcpcb *tp)
8776 {
8777 	struct tcp_rack *rack;
8778 
8779 	rack = (struct tcp_rack *)tp->t_fb_ptr;
8780 	rack->t_timers_stopped = 1;
8781 
8782 	tcp_hpts_remove(tp);
8783 
8784 	return (0);
8785 }
8786 
8787 static void
8788 rack_stop_all_timers(struct tcpcb *tp, struct tcp_rack *rack)
8789 {
8790 	/*
8791 	 * Assure no timers are running.
8792 	 */
8793 	if (tcp_timer_active(tp, TT_PERSIST)) {
8794 		/* We enter in persists, set the flag appropriately */
8795 		rack->rc_in_persist = 1;
8796 	}
8797 	if (tcp_in_hpts(rack->rc_tp)) {
8798 		tcp_hpts_remove(rack->rc_tp);
8799 	}
8800 }
8801 
8802 /*
8803  * We maintain an array fo 16 (RETRAN_CNT_SIZE) entries. This
8804  * array is zeroed at the start of recovery. Each time a segment
8805  * is retransmitted, we translate that into a number of packets
8806  * (based on segsiz) and based on how many times its been retransmitted
8807  * increment by the number of packets the counter that represents
8808  * retansmitted N times. Index 0 is retransmitted 1 time, index 1
8809  * is retransmitted 2 times etc.
8810  *
8811  * So for example when we send a 4344 byte transmission with a 1448
8812  * byte segsize, and its the third time we have retransmitted this
8813  * segment, we would add to the rc_cnt_of_retran[2] the value of
8814  * 3. That represents 3 MSS were retransmitted 3 times (index is
8815  * the number of times retranmitted minus 1).
8816  */
8817 static void
8818 rack_peg_rxt(struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t segsiz)
8819 {
8820 	int idx;
8821 	uint32_t peg;
8822 
8823 	peg = ((rsm->r_end - rsm->r_start) + segsiz) - 1;
8824 	peg /= segsiz;
8825 	idx = rsm->r_act_rxt_cnt - 1;
8826 	if (idx >= RETRAN_CNT_SIZE)
8827 		idx = RETRAN_CNT_SIZE - 1;
8828 	/* Max of a uint16_t retransmits in a bucket */
8829 	if ((rack->r_ctl.rc_cnt_of_retran[idx] + peg) < 0xffff)
8830 		rack->r_ctl.rc_cnt_of_retran[idx] += peg;
8831 	else
8832 		rack->r_ctl.rc_cnt_of_retran[idx] = 0xffff;
8833 }
8834 
8835 /*
8836  * We maintain an array fo 16 (RETRAN_CNT_SIZE) entries. This
8837  * array is zeroed at the start of recovery. Each time a segment
8838  * is retransmitted, we translate that into a number of packets
8839  * (based on segsiz) and based on how many times its been retransmitted
8840  * increment by the number of packets the counter that represents
8841  * retansmitted N times. Index 0 is retransmitted 1 time, index 1
8842  * is retransmitted 2 times etc.
8843  *
8844  * The rack_unpeg_rxt is used when we go to retransmit a segment
8845  * again. Basically if the segment had previously been retransmitted
8846  * say 3 times (as our previous example illustrated in the comment
8847  * above rack_peg_rxt() prior to calling that and incrementing
8848  * r_ack_rxt_cnt we would have called rack_unpeg_rxt() that would
8849  * subtract back the previous add from its last rxt (in this
8850  * example r_act_cnt would have been 2 for 2 retransmissions. So
8851  * we would have subtracted 3 from rc_cnt_of_reetran[1] to remove
8852  * those 3 segments. You will see this in the rack_update_rsm()
8853  * below where we do:
8854  *	if (rsm->r_act_rxt_cnt > 0) {
8855  *		rack_unpeg_rxt(rack, rsm, segsiz);
8856  *	}
8857  *	rsm->r_act_rxt_cnt++;
8858  *	rack_peg_rxt(rack, rsm, segsiz);
8859  *
8860  * This effectively moves the count from rc_cnt_of_retran[1] to
8861  * rc_cnt_of_retran[2].
8862  */
8863 static void
8864 rack_unpeg_rxt(struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t segsiz)
8865 {
8866 	int idx;
8867 	uint32_t peg;
8868 
8869 	idx = rsm->r_act_rxt_cnt - 1;
8870 	if (idx >= RETRAN_CNT_SIZE)
8871 		idx = RETRAN_CNT_SIZE - 1;
8872 	peg = ((rsm->r_end - rsm->r_start) + segsiz) - 1;
8873 	peg /= segsiz;
8874 	if (peg < rack->r_ctl.rc_cnt_of_retran[idx])
8875 		rack->r_ctl.rc_cnt_of_retran[idx] -= peg;
8876 	else {
8877 		/* TSNH */
8878 		rack->r_ctl.rc_cnt_of_retran[idx] = 0;
8879 	}
8880 }
8881 
8882 static void
8883 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
8884     struct rack_sendmap *rsm, uint64_t ts, uint32_t add_flag, int segsiz)
8885 {
8886 	int32_t idx;
8887 
8888 	rsm->r_rtr_cnt++;
8889 	if (rsm->r_rtr_cnt > RACK_NUM_OF_RETRANS) {
8890 		rsm->r_rtr_cnt = RACK_NUM_OF_RETRANS;
8891 		rsm->r_flags |= RACK_OVERMAX;
8892 	}
8893 	if (rsm->r_act_rxt_cnt > 0) {
8894 		/* Drop the count back for this, its retransmitting again */
8895 		rack_unpeg_rxt(rack, rsm, segsiz);
8896 	}
8897 	rsm->r_act_rxt_cnt++;
8898 	/* Peg the count/index */
8899 	rack_peg_rxt(rack, rsm, segsiz);
8900 	rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
8901 	rsm->r_dupack = 0;
8902 	if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & RACK_TLP) == 0)) {
8903 		rack->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start);
8904 		rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start);
8905 	}
8906 	if (rsm->r_flags & RACK_WAS_LOST) {
8907 		/*
8908 		 * We retransmitted it putting it back in flight
8909 		 * remove the lost desgination and reduce the
8910 		 * bytes considered lost.
8911 		 */
8912 		rsm->r_flags  &= ~RACK_WAS_LOST;
8913 		KASSERT((rack->r_ctl.rc_considered_lost >= (rsm->r_end - rsm->r_start)),
8914 			("rsm:%p rack:%p rc_considered_lost goes negative", rsm,  rack));
8915 		if (rack->r_ctl.rc_considered_lost >= (rsm->r_end - rsm->r_start))
8916 			rack->r_ctl.rc_considered_lost -= rsm->r_end - rsm->r_start;
8917 		else
8918 			rack->r_ctl.rc_considered_lost = 0;
8919 	}
8920 	idx = rsm->r_rtr_cnt - 1;
8921 	rsm->r_tim_lastsent[idx] = ts;
8922 	/*
8923 	 * Here we don't add in the len of send, since its already
8924 	 * in snduna <->snd_max.
8925 	 */
8926 	rsm->r_fas = ctf_flight_size(rack->rc_tp,
8927 				     rack->r_ctl.rc_sacked);
8928 	if (rsm->r_flags & RACK_ACKED) {
8929 		/* Problably MTU discovery messing with us */
8930 		rsm->r_flags &= ~RACK_ACKED;
8931 		rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
8932 	}
8933 	if (rsm->r_in_tmap) {
8934 		TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8935 		rsm->r_in_tmap = 0;
8936 	}
8937 	/* Lets make sure it really is in or not the GP window */
8938 	rack_mark_in_gp_win(tp, rsm);
8939 	TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8940 	rsm->r_in_tmap = 1;
8941 	rsm->r_bas = (uint8_t)(((rsm->r_end - rsm->r_start) + segsiz - 1) / segsiz);
8942 	/* Take off the must retransmit flag, if its on */
8943 	if (rsm->r_flags & RACK_MUST_RXT) {
8944 		if (rack->r_must_retran)
8945 			rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start);
8946 		if (SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) {
8947 			/*
8948 			 * We have retransmitted all we need. Clear
8949 			 * any must retransmit flags.
8950 			 */
8951 			rack->r_must_retran = 0;
8952 			rack->r_ctl.rc_out_at_rto = 0;
8953 		}
8954 		rsm->r_flags &= ~RACK_MUST_RXT;
8955 	}
8956 	/* Remove any collapsed flag */
8957 	rsm->r_flags &= ~RACK_RWND_COLLAPSED;
8958 	if (rsm->r_flags & RACK_SACK_PASSED) {
8959 		/* We have retransmitted due to the SACK pass */
8960 		rsm->r_flags &= ~RACK_SACK_PASSED;
8961 		rsm->r_flags |= RACK_WAS_SACKPASS;
8962 	}
8963 }
8964 
8965 static uint32_t
8966 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack,
8967     struct rack_sendmap *rsm, uint64_t ts, int32_t *lenp, uint32_t add_flag, int segsiz)
8968 {
8969 	/*
8970 	 * We (re-)transmitted starting at rsm->r_start for some length
8971 	 * (possibly less than r_end.
8972 	 */
8973 	struct rack_sendmap *nrsm;
8974 	int insret __diagused;
8975 	uint32_t c_end;
8976 	int32_t len;
8977 
8978 	len = *lenp;
8979 	c_end = rsm->r_start + len;
8980 	if (SEQ_GEQ(c_end, rsm->r_end)) {
8981 		/*
8982 		 * We retransmitted the whole piece or more than the whole
8983 		 * slopping into the next rsm.
8984 		 */
8985 		rack_update_rsm(tp, rack, rsm, ts, add_flag, segsiz);
8986 		if (c_end == rsm->r_end) {
8987 			*lenp = 0;
8988 			return (0);
8989 		} else {
8990 			int32_t act_len;
8991 
8992 			/* Hangs over the end return whats left */
8993 			act_len = rsm->r_end - rsm->r_start;
8994 			*lenp = (len - act_len);
8995 			return (rsm->r_end);
8996 		}
8997 		/* We don't get out of this block. */
8998 	}
8999 	/*
9000 	 * Here we retransmitted less than the whole thing which means we
9001 	 * have to split this into what was transmitted and what was not.
9002 	 */
9003 	nrsm = rack_alloc_full_limit(rack);
9004 	if (nrsm == NULL) {
9005 		/*
9006 		 * We can't get memory, so lets not proceed.
9007 		 */
9008 		*lenp = 0;
9009 		return (0);
9010 	}
9011 	/*
9012 	 * So here we are going to take the original rsm and make it what we
9013 	 * retransmitted. nrsm will be the tail portion we did not
9014 	 * retransmit. For example say the chunk was 1, 11 (10 bytes). And
9015 	 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to
9016 	 * 1, 6 and the new piece will be 6, 11.
9017 	 */
9018 	rack_clone_rsm(rack, nrsm, rsm, c_end);
9019 	nrsm->r_dupack = 0;
9020 	rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2);
9021 #ifndef INVARIANTS
9022 	(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
9023 #else
9024 	if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
9025 		panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p",
9026 		      nrsm, insret, rack, rsm);
9027 	}
9028 #endif
9029 	if (rsm->r_in_tmap) {
9030 		TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
9031 		nrsm->r_in_tmap = 1;
9032 	}
9033 	rsm->r_flags &= (~RACK_HAS_FIN);
9034 	rack_update_rsm(tp, rack, rsm, ts, add_flag, segsiz);
9035 	/* Log a split of rsm into rsm and nrsm */
9036 	rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
9037 	*lenp = 0;
9038 	return (0);
9039 }
9040 
9041 static void
9042 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
9043 		uint32_t seq_out, uint16_t th_flags, int32_t err, uint64_t cts,
9044 		struct rack_sendmap *hintrsm, uint32_t add_flag, struct mbuf *s_mb,
9045 		uint32_t s_moff, int hw_tls, int segsiz)
9046 {
9047 	struct tcp_rack *rack;
9048 	struct rack_sendmap *rsm, *nrsm;
9049 	int insret __diagused;
9050 
9051 	register uint32_t snd_max, snd_una;
9052 
9053 	/*
9054 	 * Add to the RACK log of packets in flight or retransmitted. If
9055 	 * there is a TS option we will use the TS echoed, if not we will
9056 	 * grab a TS.
9057 	 *
9058 	 * Retransmissions will increment the count and move the ts to its
9059 	 * proper place. Note that if options do not include TS's then we
9060 	 * won't be able to effectively use the ACK for an RTT on a retran.
9061 	 *
9062 	 * Notes about r_start and r_end. Lets consider a send starting at
9063 	 * sequence 1 for 10 bytes. In such an example the r_start would be
9064 	 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11.
9065 	 * This means that r_end is actually the first sequence for the next
9066 	 * slot (11).
9067 	 *
9068 	 */
9069 	/*
9070 	 * If err is set what do we do XXXrrs? should we not add the thing?
9071 	 * -- i.e. return if err != 0 or should we pretend we sent it? --
9072 	 * i.e. proceed with add ** do this for now.
9073 	 */
9074 	INP_WLOCK_ASSERT(tptoinpcb(tp));
9075 	if (err)
9076 		/*
9077 		 * We don't log errors -- we could but snd_max does not
9078 		 * advance in this case either.
9079 		 */
9080 		return;
9081 
9082 	if (th_flags & TH_RST) {
9083 		/*
9084 		 * We don't log resets and we return immediately from
9085 		 * sending
9086 		 */
9087 		return;
9088 	}
9089 	rack = (struct tcp_rack *)tp->t_fb_ptr;
9090 	snd_una = tp->snd_una;
9091 	snd_max = tp->snd_max;
9092 	if (th_flags & (TH_SYN | TH_FIN)) {
9093 		/*
9094 		 * The call to rack_log_output is made before bumping
9095 		 * snd_max. This means we can record one extra byte on a SYN
9096 		 * or FIN if seq_out is adding more on and a FIN is present
9097 		 * (and we are not resending).
9098 		 */
9099 		if ((th_flags & TH_SYN) && (seq_out == tp->iss))
9100 			len++;
9101 		if (th_flags & TH_FIN)
9102 			len++;
9103 	}
9104 	if (SEQ_LEQ((seq_out + len), snd_una)) {
9105 		/* Are sending an old segment to induce an ack (keep-alive)? */
9106 		return;
9107 	}
9108 	if (SEQ_LT(seq_out, snd_una)) {
9109 		/* huh? should we panic? */
9110 		uint32_t end;
9111 
9112 		end = seq_out + len;
9113 		seq_out = snd_una;
9114 		if (SEQ_GEQ(end, seq_out))
9115 			len = end - seq_out;
9116 		else
9117 			len = 0;
9118 	}
9119 	if (len == 0) {
9120 		/* We don't log zero window probes */
9121 		return;
9122 	}
9123 	if (IN_FASTRECOVERY(tp->t_flags)) {
9124 		rack->r_ctl.rc_prr_out += len;
9125 	}
9126 	/* First question is it a retransmission or new? */
9127 	if (seq_out == snd_max) {
9128 		/* Its new */
9129 		rack_chk_req_and_hybrid_on_out(rack, seq_out, len, cts);
9130 again:
9131 		rsm = rack_alloc(rack);
9132 		if (rsm == NULL) {
9133 			/*
9134 			 * Hmm out of memory and the tcb got destroyed while
9135 			 * we tried to wait.
9136 			 */
9137 			return;
9138 		}
9139 		if (th_flags & TH_FIN) {
9140 			rsm->r_flags = RACK_HAS_FIN|add_flag;
9141 		} else {
9142 			rsm->r_flags = add_flag;
9143 		}
9144 		if (hw_tls)
9145 			rsm->r_hw_tls = 1;
9146 		rsm->r_tim_lastsent[0] = cts;
9147 		rsm->r_rtr_cnt = 1;
9148  		rsm->r_act_rxt_cnt = 0;
9149 		rsm->r_rtr_bytes = 0;
9150 		if (th_flags & TH_SYN) {
9151 			/* The data space is one beyond snd_una */
9152 			rsm->r_flags |= RACK_HAS_SYN;
9153 		}
9154 		rsm->r_start = seq_out;
9155 		rsm->r_end = rsm->r_start + len;
9156 		rack_mark_in_gp_win(tp, rsm);
9157 		rsm->r_dupack = 0;
9158 		/*
9159 		 * save off the mbuf location that
9160 		 * sndmbuf_noadv returned (which is
9161 		 * where we started copying from)..
9162 		 */
9163 		rsm->m = s_mb;
9164 		rsm->soff = s_moff;
9165 		/*
9166 		 * Here we do add in the len of send, since its not yet
9167 		 * reflected in in snduna <->snd_max
9168 		 */
9169 		rsm->r_fas = (ctf_flight_size(rack->rc_tp,
9170 					      rack->r_ctl.rc_sacked) +
9171 			      (rsm->r_end - rsm->r_start));
9172 		if ((rack->rc_initial_ss_comp == 0) &&
9173 		    (rack->r_ctl.ss_hi_fs < rsm->r_fas)) {
9174 			   rack->r_ctl.ss_hi_fs = rsm->r_fas;
9175 		}
9176 		/* rsm->m will be NULL if RACK_HAS_SYN or RACK_HAS_FIN is set */
9177 		if (rsm->m) {
9178 			if (rsm->m->m_len <= rsm->soff) {
9179 				/*
9180 				 * XXXrrs Question, will this happen?
9181 				 *
9182 				 * If sbsndptr is set at the correct place
9183 				 * then s_moff should always be somewhere
9184 				 * within rsm->m. But if the sbsndptr was
9185 				 * off then that won't be true. If it occurs
9186 				 * we need to walkout to the correct location.
9187 				 */
9188 				struct mbuf *lm;
9189 
9190 				lm = rsm->m;
9191 				while (lm->m_len <= rsm->soff) {
9192 					rsm->soff -= lm->m_len;
9193 					lm = lm->m_next;
9194 					KASSERT(lm != NULL, ("%s rack:%p lm goes null orig_off:%u origmb:%p rsm->soff:%u",
9195 							     __func__, rack, s_moff, s_mb, rsm->soff));
9196 				}
9197 				rsm->m = lm;
9198 			}
9199 			rsm->orig_m_len = rsm->m->m_len;
9200 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
9201 		} else {
9202 			rsm->orig_m_len = 0;
9203 			rsm->orig_t_space = 0;
9204 		}
9205 		rsm->r_bas = (uint8_t)((len + segsiz - 1) / segsiz);
9206 		rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
9207 		/* Log a new rsm */
9208 		rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_NEW, 0, __LINE__);
9209 #ifndef INVARIANTS
9210 		(void)tqhash_insert(rack->r_ctl.tqh, rsm);
9211 #else
9212 		if ((insret = tqhash_insert(rack->r_ctl.tqh, rsm)) != 0) {
9213 			panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p",
9214 			      nrsm, insret, rack, rsm);
9215 		}
9216 #endif
9217 		TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
9218 		rsm->r_in_tmap = 1;
9219 		if (rsm->r_flags & RACK_IS_PCM) {
9220 			rack->r_ctl.pcm_i.send_time = cts;
9221 			rack->r_ctl.pcm_i.eseq = rsm->r_end;
9222 			/* First time through we set the start too */
9223 			if (rack->pcm_in_progress == 0)
9224 				rack->r_ctl.pcm_i.sseq = rsm->r_start;
9225 		}
9226 		/*
9227 		 * Special case detection, is there just a single
9228 		 * packet outstanding when we are not in recovery?
9229 		 *
9230 		 * If this is true mark it so.
9231 		 */
9232 		if ((IN_FASTRECOVERY(tp->t_flags) == 0) &&
9233 		    (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) == ctf_fixed_maxseg(tp))) {
9234 			struct rack_sendmap *prsm;
9235 
9236 			prsm = tqhash_prev(rack->r_ctl.tqh, rsm);
9237 			if (prsm)
9238 				prsm->r_one_out_nr = 1;
9239 		}
9240 		return;
9241 	}
9242 	/*
9243 	 * If we reach here its a retransmission and we need to find it.
9244 	 */
9245 more:
9246 	if (hintrsm && (hintrsm->r_start == seq_out)) {
9247 		rsm = hintrsm;
9248 		hintrsm = NULL;
9249 	} else {
9250 		/* No hints sorry */
9251 		rsm = NULL;
9252 	}
9253 	if ((rsm) && (rsm->r_start == seq_out)) {
9254 		seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag, segsiz);
9255 		if (len == 0) {
9256 			return;
9257 		} else {
9258 			goto more;
9259 		}
9260 	}
9261 	/* Ok it was not the last pointer go through it the hard way. */
9262 refind:
9263 	rsm = tqhash_find(rack->r_ctl.tqh, seq_out);
9264 	if (rsm) {
9265 		if (rsm->r_start == seq_out) {
9266 			seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag, segsiz);
9267 			if (len == 0) {
9268 				return;
9269 			} else {
9270 				goto refind;
9271 			}
9272 		}
9273 		if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) {
9274 			/* Transmitted within this piece */
9275 			/*
9276 			 * Ok we must split off the front and then let the
9277 			 * update do the rest
9278 			 */
9279 			nrsm = rack_alloc_full_limit(rack);
9280 			if (nrsm == NULL) {
9281 				rack_update_rsm(tp, rack, rsm, cts, add_flag, segsiz);
9282 				return;
9283 			}
9284 			/*
9285 			 * copy rsm to nrsm and then trim the front of rsm
9286 			 * to not include this part.
9287 			 */
9288 			rack_clone_rsm(rack, nrsm, rsm, seq_out);
9289 			rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
9290 #ifndef INVARIANTS
9291 			(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
9292 #else
9293 			if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
9294 				panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p",
9295 				      nrsm, insret, rack, rsm);
9296 			}
9297 #endif
9298 			if (rsm->r_in_tmap) {
9299 				TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
9300 				nrsm->r_in_tmap = 1;
9301 			}
9302 			rsm->r_flags &= (~RACK_HAS_FIN);
9303 			seq_out = rack_update_entry(tp, rack, nrsm, cts, &len, add_flag, segsiz);
9304 			if (len == 0) {
9305 				return;
9306 			} else if (len > 0)
9307 				goto refind;
9308 		}
9309 	}
9310 	/*
9311 	 * Hmm not found in map did they retransmit both old and on into the
9312 	 * new?
9313 	 */
9314 	if (seq_out == tp->snd_max) {
9315 		goto again;
9316 	} else if (SEQ_LT(seq_out, tp->snd_max)) {
9317 #ifdef INVARIANTS
9318 		printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n",
9319 		       seq_out, len, tp->snd_una, tp->snd_max);
9320 		printf("Starting Dump of all rack entries\n");
9321 		TQHASH_FOREACH(rsm, rack->r_ctl.tqh)  {
9322 			printf("rsm:%p start:%u end:%u\n",
9323 			       rsm, rsm->r_start, rsm->r_end);
9324 		}
9325 		printf("Dump complete\n");
9326 		panic("seq_out not found rack:%p tp:%p",
9327 		      rack, tp);
9328 #endif
9329 	} else {
9330 #ifdef INVARIANTS
9331 		/*
9332 		 * Hmm beyond sndmax? (only if we are using the new rtt-pack
9333 		 * flag)
9334 		 */
9335 		panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p",
9336 		      seq_out, len, tp->snd_max, tp);
9337 #endif
9338 	}
9339 }
9340 
9341 /*
9342  * Record one of the RTT updates from an ack into
9343  * our sample structure.
9344  */
9345 
9346 static void
9347 tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt, uint32_t len, uint32_t us_rtt,
9348 		    int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt)
9349 {
9350 	if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
9351 	    (rack->r_ctl.rack_rs.rs_rtt_lowest > rtt)) {
9352 		rack->r_ctl.rack_rs.rs_rtt_lowest = rtt;
9353 	}
9354 	if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
9355 	    (rack->r_ctl.rack_rs.rs_rtt_highest < rtt)) {
9356 		rack->r_ctl.rack_rs.rs_rtt_highest = rtt;
9357 	}
9358 	if (rack->rc_tp->t_flags & TF_GPUTINPROG) {
9359 	    if (us_rtt < rack->r_ctl.rc_gp_lowrtt)
9360 		rack->r_ctl.rc_gp_lowrtt = us_rtt;
9361 	    if (rack->rc_tp->snd_wnd > rack->r_ctl.rc_gp_high_rwnd)
9362 		    rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
9363 	}
9364 	if ((confidence == 1) &&
9365 	    ((rsm == NULL) ||
9366 	     (rsm->r_just_ret) ||
9367 	     (rsm->r_one_out_nr &&
9368 	      len < (ctf_fixed_maxseg(rack->rc_tp) * 2)))) {
9369 		/*
9370 		 * If the rsm had a just return
9371 		 * hit it then we can't trust the
9372 		 * rtt measurement for buffer deterimination
9373 		 * Note that a confidence of 2, indicates
9374 		 * SACK'd which overrides the r_just_ret or
9375 		 * the r_one_out_nr. If it was a CUM-ACK and
9376 		 * we had only two outstanding, but get an
9377 		 * ack for only 1. Then that also lowers our
9378 		 * confidence.
9379 		 */
9380 		confidence = 0;
9381 	}
9382 	if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
9383 	    (rack->r_ctl.rack_rs.rs_us_rtt > us_rtt)) {
9384 		if (rack->r_ctl.rack_rs.confidence == 0) {
9385 			/*
9386 			 * We take anything with no current confidence
9387 			 * saved.
9388 			 */
9389 			rack->r_ctl.rack_rs.rs_us_rtt = us_rtt;
9390 			rack->r_ctl.rack_rs.confidence = confidence;
9391 			rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt;
9392 		} else if (confidence != 0) {
9393 			/*
9394 			 * Once we have a confident number,
9395 			 * we can update it with a smaller
9396 			 * value since this confident number
9397 			 * may include the DSACK time until
9398 			 * the next segment (the second one) arrived.
9399 			 */
9400 			rack->r_ctl.rack_rs.rs_us_rtt = us_rtt;
9401 			rack->r_ctl.rack_rs.confidence = confidence;
9402 			rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt;
9403 		}
9404 	}
9405 	rack_log_rtt_upd(rack->rc_tp, rack, us_rtt, len, rsm, confidence);
9406 	rack->r_ctl.rack_rs.rs_flags = RACK_RTT_VALID;
9407 	rack->r_ctl.rack_rs.rs_rtt_tot += rtt;
9408 	rack->r_ctl.rack_rs.rs_rtt_cnt++;
9409 }
9410 
9411 /*
9412  * Collect new round-trip time estimate
9413  * and update averages and current timeout.
9414  */
9415 static void
9416 tcp_rack_xmit_timer_commit(struct tcp_rack *rack, struct tcpcb *tp)
9417 {
9418 	int32_t delta;
9419 	int32_t rtt;
9420 
9421 	if (rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY)
9422 		/* No valid sample */
9423 		return;
9424 	if (rack->r_ctl.rc_rate_sample_method == USE_RTT_LOW) {
9425 		/* We are to use the lowest RTT seen in a single ack */
9426 		rtt = rack->r_ctl.rack_rs.rs_rtt_lowest;
9427 	} else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_HIGH) {
9428 		/* We are to use the highest RTT seen in a single ack */
9429 		rtt = rack->r_ctl.rack_rs.rs_rtt_highest;
9430 	} else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_AVG) {
9431 		/* We are to use the average RTT seen in a single ack */
9432 		rtt = (int32_t)(rack->r_ctl.rack_rs.rs_rtt_tot /
9433 				(uint64_t)rack->r_ctl.rack_rs.rs_rtt_cnt);
9434 	} else {
9435 #ifdef INVARIANTS
9436 		panic("Unknown rtt variant %d", rack->r_ctl.rc_rate_sample_method);
9437 #endif
9438 		return;
9439 	}
9440 	if (rtt == 0)
9441 		rtt = 1;
9442 	if (rack->rc_gp_rtt_set == 0) {
9443 		/*
9444 		 * With no RTT we have to accept
9445 		 * even one we are not confident of.
9446 		 */
9447 		rack->r_ctl.rc_gp_srtt = rack->r_ctl.rack_rs.rs_us_rtt;
9448 		rack->rc_gp_rtt_set = 1;
9449 	} else if (rack->r_ctl.rack_rs.confidence) {
9450 		/* update the running gp srtt */
9451 		rack->r_ctl.rc_gp_srtt -= (rack->r_ctl.rc_gp_srtt/8);
9452 		rack->r_ctl.rc_gp_srtt += rack->r_ctl.rack_rs.rs_us_rtt / 8;
9453 	}
9454 	if (rack->r_ctl.rack_rs.confidence) {
9455 		/*
9456 		 * record the low and high for highly buffered path computation,
9457 		 * we only do this if we are confident (not a retransmission).
9458 		 */
9459 		if (rack->r_ctl.rc_highest_us_rtt < rack->r_ctl.rack_rs.rs_us_rtt) {
9460 			rack->r_ctl.rc_highest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
9461 		}
9462 		if (rack->rc_highly_buffered == 0) {
9463 			/*
9464 			 * Currently once we declare a path has
9465 			 * highly buffered there is no going
9466 			 * back, which may be a problem...
9467 			 */
9468 			if ((rack->r_ctl.rc_highest_us_rtt / rack->r_ctl.rc_lowest_us_rtt) > rack_hbp_thresh) {
9469 				rack_log_rtt_shrinks(rack, rack->r_ctl.rack_rs.rs_us_rtt,
9470 						     rack->r_ctl.rc_highest_us_rtt,
9471 						     rack->r_ctl.rc_lowest_us_rtt,
9472 						     RACK_RTTS_SEEHBP);
9473 				rack->rc_highly_buffered = 1;
9474 			}
9475 		}
9476 	}
9477 	if ((rack->r_ctl.rack_rs.confidence) ||
9478 	    (rack->r_ctl.rack_rs.rs_us_rtrcnt == 1)) {
9479 		/*
9480 		 * If we are highly confident of it <or> it was
9481 		 * never retransmitted we accept it as the last us_rtt.
9482 		 */
9483 		rack->r_ctl.rc_last_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
9484 		/* The lowest rtt can be set if its was not retransmited */
9485 		if (rack->r_ctl.rc_lowest_us_rtt > rack->r_ctl.rack_rs.rs_us_rtt) {
9486 			rack->r_ctl.rc_lowest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
9487 			if (rack->r_ctl.rc_lowest_us_rtt == 0)
9488 				rack->r_ctl.rc_lowest_us_rtt = 1;
9489 		}
9490 	}
9491 	rack = (struct tcp_rack *)tp->t_fb_ptr;
9492 	if (tp->t_srtt != 0) {
9493 		/*
9494 		 * We keep a simple srtt in microseconds, like our rtt
9495 		 * measurement. We don't need to do any tricks with shifting
9496 		 * etc. Instead we just add in 1/8th of the new measurement
9497 		 * and subtract out 1/8 of the old srtt. We do the same with
9498 		 * the variance after finding the absolute value of the
9499 		 * difference between this sample and the current srtt.
9500 		 */
9501 		delta = tp->t_srtt - rtt;
9502 		/* Take off 1/8th of the current sRTT */
9503 		tp->t_srtt -= (tp->t_srtt >> 3);
9504 		/* Add in 1/8th of the new RTT just measured */
9505 		tp->t_srtt += (rtt >> 3);
9506 		if (tp->t_srtt <= 0)
9507 			tp->t_srtt = 1;
9508 		/* Now lets make the absolute value of the variance */
9509 		if (delta < 0)
9510 			delta = -delta;
9511 		/* Subtract out 1/8th */
9512 		tp->t_rttvar -= (tp->t_rttvar >> 3);
9513 		/* Add in 1/8th of the new variance we just saw */
9514 		tp->t_rttvar += (delta >> 3);
9515 		if (tp->t_rttvar <= 0)
9516 			tp->t_rttvar = 1;
9517 	} else {
9518 		/*
9519 		 * No rtt measurement yet - use the unsmoothed rtt. Set the
9520 		 * variance to half the rtt (so our first retransmit happens
9521 		 * at 3*rtt).
9522 		 */
9523 		tp->t_srtt = rtt;
9524 		tp->t_rttvar = rtt >> 1;
9525 	}
9526 	rack->rc_srtt_measure_made = 1;
9527 	KMOD_TCPSTAT_INC(tcps_rttupdated);
9528 	if (tp->t_rttupdated < UCHAR_MAX)
9529 		tp->t_rttupdated++;
9530 #ifdef STATS
9531 	if (rack_stats_gets_ms_rtt == 0) {
9532 		/* Send in the microsecond rtt used for rxt timeout purposes */
9533 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt));
9534 	} else if (rack_stats_gets_ms_rtt == 1) {
9535 		/* Send in the millisecond rtt used for rxt timeout purposes */
9536 		int32_t ms_rtt;
9537 
9538 		/* Round up */
9539 		ms_rtt = (rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC;
9540 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt));
9541 	} else if (rack_stats_gets_ms_rtt == 2) {
9542 		/* Send in the millisecond rtt has close to the path RTT as we can get  */
9543 		int32_t ms_rtt;
9544 
9545 		/* Round up */
9546 		ms_rtt = (rack->r_ctl.rack_rs.rs_us_rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC;
9547 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt));
9548 	}  else {
9549 		/* Send in the microsecond rtt has close to the path RTT as we can get  */
9550 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rack->r_ctl.rack_rs.rs_us_rtt));
9551 	}
9552 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_PATHRTT, imax(0, rack->r_ctl.rack_rs.rs_us_rtt));
9553 #endif
9554 	rack->r_ctl.last_rcv_tstmp_for_rtt = tcp_tv_to_mssectick(&rack->r_ctl.act_rcv_time);
9555 	/*
9556 	 * the retransmit should happen at rtt + 4 * rttvar. Because of the
9557 	 * way we do the smoothing, srtt and rttvar will each average +1/2
9558 	 * tick of bias.  When we compute the retransmit timer, we want 1/2
9559 	 * tick of rounding and 1 extra tick because of +-1/2 tick
9560 	 * uncertainty in the firing of the timer.  The bias will give us
9561 	 * exactly the 1.5 tick we need.  But, because the bias is
9562 	 * statistical, we have to test that we don't drop below the minimum
9563 	 * feasible timer (which is 2 ticks).
9564 	 */
9565 	tp->t_rxtshift = 0;
9566 	RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
9567 		      max(rack_rto_min, rtt + 2), rack_rto_max, rack->r_ctl.timer_slop);
9568 	rack_log_rtt_sample(rack, rtt);
9569 	tp->t_softerror = 0;
9570 }
9571 
9572 
9573 static void
9574 rack_apply_updated_usrtt(struct tcp_rack *rack, uint32_t us_rtt, uint32_t us_cts)
9575 {
9576 	/*
9577 	 * Apply to filter the inbound us-rtt at us_cts.
9578 	 */
9579 	uint32_t old_rtt;
9580 
9581 	old_rtt = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
9582 	apply_filter_min_small(&rack->r_ctl.rc_gp_min_rtt,
9583 			       us_rtt, us_cts);
9584 	if (old_rtt > us_rtt) {
9585 		/* We just hit a new lower rtt time */
9586 		rack_log_rtt_shrinks(rack,  us_cts,  old_rtt,
9587 				     __LINE__, RACK_RTTS_NEWRTT);
9588 		/*
9589 		 * Only count it if its lower than what we saw within our
9590 		 * calculated range.
9591 		 */
9592 		if ((old_rtt - us_rtt) > rack_min_rtt_movement) {
9593 			if (rack_probertt_lower_within &&
9594 			    rack->rc_gp_dyn_mul &&
9595 			    (rack->use_fixed_rate == 0) &&
9596 			    (rack->rc_always_pace)) {
9597 				/*
9598 				 * We are seeing a new lower rtt very close
9599 				 * to the time that we would have entered probe-rtt.
9600 				 * This is probably due to the fact that a peer flow
9601 				 * has entered probe-rtt. Lets go in now too.
9602 				 */
9603 				uint32_t val;
9604 
9605 				val = rack_probertt_lower_within * rack_time_between_probertt;
9606 				val /= 100;
9607 				if ((rack->in_probe_rtt == 0)  &&
9608 				    (rack->rc_skip_timely == 0) &&
9609 				    ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= (rack_time_between_probertt - val)))	{
9610 					rack_enter_probertt(rack, us_cts);
9611 				}
9612 			}
9613 			rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
9614 		}
9615 	}
9616 }
9617 
9618 static int
9619 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack,
9620     struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack)
9621 {
9622 	uint32_t us_rtt;
9623 	int32_t i, all;
9624 	uint32_t t, len_acked;
9625 
9626 	if ((rsm->r_flags & RACK_ACKED) ||
9627 	    (rsm->r_flags & RACK_WAS_ACKED))
9628 		/* Already done */
9629 		return (0);
9630 	if (rsm->r_no_rtt_allowed) {
9631 		/* Not allowed */
9632 		return (0);
9633 	}
9634 	if (ack_type == CUM_ACKED) {
9635 		if (SEQ_GT(th_ack, rsm->r_end)) {
9636 			len_acked = rsm->r_end - rsm->r_start;
9637 			all = 1;
9638 		} else {
9639 			len_acked = th_ack - rsm->r_start;
9640 			all = 0;
9641 		}
9642 	} else {
9643 		len_acked = rsm->r_end - rsm->r_start;
9644 		all = 0;
9645 	}
9646 	if (rsm->r_rtr_cnt == 1) {
9647 
9648 		t = cts - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
9649 		if ((int)t <= 0)
9650 			t = 1;
9651 		if (!tp->t_rttlow || tp->t_rttlow > t)
9652 			tp->t_rttlow = t;
9653 		if (!rack->r_ctl.rc_rack_min_rtt ||
9654 		    SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
9655 			rack->r_ctl.rc_rack_min_rtt = t;
9656 			if (rack->r_ctl.rc_rack_min_rtt == 0) {
9657 				rack->r_ctl.rc_rack_min_rtt = 1;
9658 			}
9659 		}
9660 		if (TSTMP_GT(tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time), rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]))
9661 			us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
9662 		else
9663 			us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
9664 		if (us_rtt == 0)
9665 			us_rtt = 1;
9666 		if (CC_ALGO(tp)->rttsample != NULL) {
9667 			/* Kick the RTT to the CC */
9668 			CC_ALGO(tp)->rttsample(&tp->t_ccv, us_rtt, 1, rsm->r_fas);
9669 		}
9670 		rack_apply_updated_usrtt(rack, us_rtt, tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time));
9671 		if (ack_type == SACKED) {
9672 			rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 1);
9673 			tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt, 2 , rsm, rsm->r_rtr_cnt);
9674 		} else {
9675 			/*
9676 			 * We need to setup what our confidence
9677 			 * is in this ack.
9678 			 *
9679 			 * If the rsm was app limited and it is
9680 			 * less than a mss in length (the end
9681 			 * of the send) then we have a gap. If we
9682 			 * were app limited but say we were sending
9683 			 * multiple MSS's then we are more confident
9684 			 * int it.
9685 			 *
9686 			 * When we are not app-limited then we see if
9687 			 * the rsm is being included in the current
9688 			 * measurement, we tell this by the app_limited_needs_set
9689 			 * flag.
9690 			 *
9691 			 * Note that being cwnd blocked is not applimited
9692 			 * as well as the pacing delay between packets which
9693 			 * are sending only 1 or 2 MSS's also will show up
9694 			 * in the RTT. We probably need to examine this algorithm
9695 			 * a bit more and enhance it to account for the delay
9696 			 * between rsm's. We could do that by saving off the
9697 			 * pacing delay of each rsm (in an rsm) and then
9698 			 * factoring that in somehow though for now I am
9699 			 * not sure how :)
9700 			 */
9701 			int calc_conf = 0;
9702 
9703 			if (rsm->r_flags & RACK_APP_LIMITED) {
9704 				if (all && (len_acked <= ctf_fixed_maxseg(tp)))
9705 					calc_conf = 0;
9706 				else
9707 					calc_conf = 1;
9708 			} else if (rack->app_limited_needs_set == 0) {
9709 				calc_conf = 1;
9710 			} else {
9711 				calc_conf = 0;
9712 			}
9713 			rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 2);
9714 			tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt,
9715 					    calc_conf, rsm, rsm->r_rtr_cnt);
9716 		}
9717 		if ((rsm->r_flags & RACK_TLP) &&
9718 		    (!IN_FASTRECOVERY(tp->t_flags))) {
9719 			/* Segment was a TLP and our retrans matched */
9720 			if (rack->r_ctl.rc_tlp_cwnd_reduce) {
9721 				rack_cong_signal(tp, CC_NDUPACK, th_ack, __LINE__);
9722 			}
9723 		}
9724 		if ((rack->r_ctl.rc_rack_tmit_time == 0) ||
9725 		    (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
9726 			    (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]))) {
9727 			/* New more recent rack_tmit_time */
9728 			rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
9729 			if (rack->r_ctl.rc_rack_tmit_time == 0)
9730 				rack->r_ctl.rc_rack_tmit_time = 1;
9731 			rack->rc_rack_rtt = t;
9732 		}
9733 		return (1);
9734 	}
9735 	/*
9736 	 * We clear the soft/rxtshift since we got an ack.
9737 	 * There is no assurance we will call the commit() function
9738 	 * so we need to clear these to avoid incorrect handling.
9739 	 */
9740 	tp->t_rxtshift = 0;
9741 	RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
9742 		      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
9743 	tp->t_softerror = 0;
9744 	if (to && (to->to_flags & TOF_TS) &&
9745 	    (ack_type == CUM_ACKED) &&
9746 	    (to->to_tsecr) &&
9747 	    ((rsm->r_flags & RACK_OVERMAX) == 0)) {
9748 		/*
9749 		 * Now which timestamp does it match? In this block the ACK
9750 		 * must be coming from a previous transmission.
9751 		 */
9752 		for (i = 0; i < rsm->r_rtr_cnt; i++) {
9753 			if (rack_ts_to_msec(rsm->r_tim_lastsent[i]) == to->to_tsecr) {
9754 				t = cts - (uint32_t)rsm->r_tim_lastsent[i];
9755 				if ((int)t <= 0)
9756 					t = 1;
9757 				if (CC_ALGO(tp)->rttsample != NULL) {
9758 					/*
9759 					 * Kick the RTT to the CC, here
9760 					 * we lie a bit in that we know the
9761 					 * retransmission is correct even though
9762 					 * we retransmitted. This is because
9763 					 * we match the timestamps.
9764 					 */
9765 					if (TSTMP_GT(tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time), rsm->r_tim_lastsent[i]))
9766 						us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - (uint32_t)rsm->r_tim_lastsent[i];
9767 					else
9768 						us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[i];
9769 					CC_ALGO(tp)->rttsample(&tp->t_ccv, us_rtt, 1, rsm->r_fas);
9770 				}
9771 				if ((i + 1) < rsm->r_rtr_cnt) {
9772 					/*
9773 					 * The peer ack'd from our previous
9774 					 * transmission. We have a spurious
9775 					 * retransmission and thus we dont
9776 					 * want to update our rack_rtt.
9777 					 *
9778 					 * Hmm should there be a CC revert here?
9779 					 *
9780 					 */
9781 					return (0);
9782 				}
9783 				if (!tp->t_rttlow || tp->t_rttlow > t)
9784 					tp->t_rttlow = t;
9785 				if (!rack->r_ctl.rc_rack_min_rtt || SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
9786 					rack->r_ctl.rc_rack_min_rtt = t;
9787 					if (rack->r_ctl.rc_rack_min_rtt == 0) {
9788 						rack->r_ctl.rc_rack_min_rtt = 1;
9789 					}
9790 				}
9791 				if ((rack->r_ctl.rc_rack_tmit_time == 0) ||
9792 				    (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
9793 					    (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]))) {
9794 					/* New more recent rack_tmit_time */
9795 					rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
9796 					if (rack->r_ctl.rc_rack_tmit_time == 0)
9797 						rack->r_ctl.rc_rack_tmit_time = 1;
9798 					rack->rc_rack_rtt = t;
9799 				}
9800 				rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[i], cts, 3);
9801 				tcp_rack_xmit_timer(rack, t + 1, len_acked, t, 0, rsm,
9802 						    rsm->r_rtr_cnt);
9803 				return (1);
9804 			}
9805 		}
9806 		/* If we are logging log out the sendmap */
9807 		if (tcp_bblogging_on(rack->rc_tp)) {
9808 			for (i = 0; i < rsm->r_rtr_cnt; i++) {
9809 				rack_log_rtt_sendmap(rack, i, rsm->r_tim_lastsent[i], to->to_tsecr);
9810 			}
9811 		}
9812 		goto ts_not_found;
9813 	} else {
9814 		/*
9815 		 * Ok its a SACK block that we retransmitted. or a windows
9816 		 * machine without timestamps. We can tell nothing from the
9817 		 * time-stamp since its not there or the time the peer last
9818 		 * received a segment that moved forward its cum-ack point.
9819 		 */
9820 ts_not_found:
9821 		i = rsm->r_rtr_cnt - 1;
9822 		t = cts - (uint32_t)rsm->r_tim_lastsent[i];
9823 		if ((int)t <= 0)
9824 			t = 1;
9825 		if (rack->r_ctl.rc_rack_min_rtt && SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
9826 			/*
9827 			 * We retransmitted and the ack came back in less
9828 			 * than the smallest rtt we have observed. We most
9829 			 * likely did an improper retransmit as outlined in
9830 			 * 6.2 Step 2 point 2 in the rack-draft so we
9831 			 * don't want to update our rack_rtt. We in
9832 			 * theory (in future) might want to think about reverting our
9833 			 * cwnd state but we won't for now.
9834 			 */
9835 			return (0);
9836 		} else if (rack->r_ctl.rc_rack_min_rtt) {
9837 			/*
9838 			 * We retransmitted it and the retransmit did the
9839 			 * job.
9840 			 */
9841 			if (!rack->r_ctl.rc_rack_min_rtt ||
9842 			    SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
9843 				rack->r_ctl.rc_rack_min_rtt = t;
9844 				if (rack->r_ctl.rc_rack_min_rtt == 0) {
9845 					rack->r_ctl.rc_rack_min_rtt = 1;
9846 				}
9847 			}
9848 			if ((rack->r_ctl.rc_rack_tmit_time == 0) ||
9849 			    (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
9850 				    (uint32_t)rsm->r_tim_lastsent[i]))) {
9851 				/* New more recent rack_tmit_time */
9852 				rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[i];
9853 				if (rack->r_ctl.rc_rack_tmit_time == 0)
9854 					rack->r_ctl.rc_rack_tmit_time = 1;
9855 				rack->rc_rack_rtt = t;
9856 			}
9857 			return (1);
9858 		}
9859 	}
9860 	return (0);
9861 }
9862 
9863 /*
9864  * Mark the SACK_PASSED flag on all entries prior to rsm send wise.
9865  */
9866 static void
9867 rack_log_sack_passed(struct tcpcb *tp,
9868     struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t cts)
9869 {
9870 	struct rack_sendmap *nrsm;
9871 	uint32_t thresh;
9872 
9873 	/* Get our rxt threshold for lost consideration */
9874 	thresh = rack_calc_thresh_rack(rack, rack_grab_rtt(tp, rack), cts, __LINE__, 0);
9875 	/* Now start looking at rsm's */
9876 	nrsm = rsm;
9877 	TAILQ_FOREACH_REVERSE_FROM(nrsm, &rack->r_ctl.rc_tmap,
9878 	    rack_head, r_tnext) {
9879 		if (nrsm == rsm) {
9880 			/* Skip original segment he is acked */
9881 			continue;
9882 		}
9883 		if (nrsm->r_flags & RACK_ACKED) {
9884 			/*
9885 			 * Skip ack'd segments, though we
9886 			 * should not see these, since tmap
9887 			 * should not have ack'd segments.
9888 			 */
9889 			continue;
9890 		}
9891 		if (nrsm->r_flags & RACK_RWND_COLLAPSED) {
9892 			/*
9893 			 * If the peer dropped the rwnd on
9894 			 * these then we don't worry about them.
9895 			 */
9896 			continue;
9897 		}
9898 		/* Check lost state */
9899 		if ((nrsm->r_flags & RACK_WAS_LOST) == 0) {
9900 			uint32_t exp;
9901 
9902 			exp = ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]) + thresh;
9903 			if (TSTMP_LT(exp, cts) || (exp == cts)) {
9904 				/* We consider it lost */
9905 				nrsm->r_flags |= RACK_WAS_LOST;
9906 				rack->r_ctl.rc_considered_lost += nrsm->r_end - nrsm->r_start;
9907 			}
9908 		}
9909 		if (nrsm->r_flags & RACK_SACK_PASSED) {
9910 			/*
9911 			 * We found one that is already marked
9912 			 * passed, we have been here before and
9913 			 * so all others below this are marked.
9914 			 */
9915 			break;
9916 		}
9917 		nrsm->r_flags |= RACK_SACK_PASSED;
9918 		nrsm->r_flags &= ~RACK_WAS_SACKPASS;
9919 	}
9920 }
9921 
9922 static void
9923 rack_need_set_test(struct tcpcb *tp,
9924 		   struct tcp_rack *rack,
9925 		   struct rack_sendmap *rsm,
9926 		   tcp_seq th_ack,
9927 		   int line,
9928 		   int use_which)
9929 {
9930 	struct rack_sendmap *s_rsm;
9931 
9932 	if ((tp->t_flags & TF_GPUTINPROG) &&
9933 	    SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
9934 		/*
9935 		 * We were app limited, and this ack
9936 		 * butts up or goes beyond the point where we want
9937 		 * to start our next measurement. We need
9938 		 * to record the new gput_ts as here and
9939 		 * possibly update the start sequence.
9940 		 */
9941 		uint32_t seq, ts;
9942 
9943 		if (rsm->r_rtr_cnt > 1) {
9944 			/*
9945 			 * This is a retransmit, can we
9946 			 * really make any assessment at this
9947 			 * point?  We are not really sure of
9948 			 * the timestamp, is it this or the
9949 			 * previous transmission?
9950 			 *
9951 			 * Lets wait for something better that
9952 			 * is not retransmitted.
9953 			 */
9954 			return;
9955 		}
9956 		seq = tp->gput_seq;
9957 		ts = tp->gput_ts;
9958 		rack->app_limited_needs_set = 0;
9959 		tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
9960 		/* Do we start at a new end? */
9961 		if ((use_which == RACK_USE_BEG) &&
9962 		    SEQ_GEQ(rsm->r_start, tp->gput_seq)) {
9963 			/*
9964 			 * When we get an ACK that just eats
9965 			 * up some of the rsm, we set RACK_USE_BEG
9966 			 * since whats at r_start (i.e. th_ack)
9967 			 * is left unacked and thats where the
9968 			 * measurement now starts.
9969 			 */
9970 			tp->gput_seq = rsm->r_start;
9971 		}
9972 		if ((use_which == RACK_USE_END) &&
9973 		    SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
9974 			/*
9975 			 * We use the end when the cumack
9976 			 * is moving forward and completely
9977 			 * deleting the rsm passed so basically
9978 			 * r_end holds th_ack.
9979 			 *
9980 			 * For SACK's we also want to use the end
9981 			 * since this piece just got sacked and
9982 			 * we want to target anything after that
9983 			 * in our measurement.
9984 			 */
9985 			tp->gput_seq = rsm->r_end;
9986 		}
9987 		if (use_which == RACK_USE_END_OR_THACK) {
9988 			/*
9989 			 * special case for ack moving forward,
9990 			 * not a sack, we need to move all the
9991 			 * way up to where this ack cum-ack moves
9992 			 * to.
9993 			 */
9994 			if (SEQ_GT(th_ack, rsm->r_end))
9995 				tp->gput_seq = th_ack;
9996 			else
9997 				tp->gput_seq = rsm->r_end;
9998 		}
9999 		if (SEQ_LT(tp->gput_seq, tp->snd_max))
10000 			s_rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
10001 		else
10002 			s_rsm = NULL;
10003 		/*
10004 		 * Pick up the correct send time if we can the rsm passed in
10005 		 * may be equal to s_rsm if the RACK_USE_BEG was set. For the other
10006 		 * two cases (RACK_USE_THACK or RACK_USE_END) most likely we will
10007 		 * find a different seq i.e. the next send up.
10008 		 *
10009 		 * If that has not been sent, s_rsm will be NULL and we must
10010 		 * arrange it so this function will get called again by setting
10011 		 * app_limited_needs_set.
10012 		 */
10013 		if (s_rsm)
10014 			rack->r_ctl.rc_gp_output_ts = s_rsm->r_tim_lastsent[0];
10015 		else {
10016 			/* If we hit here we have to have *not* sent tp->gput_seq */
10017 			rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[0];
10018 			/* Set it up so we will go through here again */
10019 			rack->app_limited_needs_set = 1;
10020 		}
10021 		if (SEQ_GT(tp->gput_seq, tp->gput_ack)) {
10022 			/*
10023 			 * We moved beyond this guy's range, re-calculate
10024 			 * the new end point.
10025 			 */
10026 			if (rack->rc_gp_filled == 0) {
10027 				tp->gput_ack = tp->gput_seq + max(rc_init_window(rack), (MIN_GP_WIN * ctf_fixed_maxseg(tp)));
10028 			} else {
10029 				tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
10030 			}
10031 		}
10032 		/*
10033 		 * We are moving the goal post, we may be able to clear the
10034 		 * measure_saw_probe_rtt flag.
10035 		 */
10036 		if ((rack->in_probe_rtt == 0) &&
10037 		    (rack->measure_saw_probe_rtt) &&
10038 		    (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
10039 			rack->measure_saw_probe_rtt = 0;
10040 		rack_log_pacing_delay_calc(rack, ts, tp->gput_ts,
10041 					   seq, tp->gput_seq,
10042 					   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) |
10043 					    (uint64_t)rack->r_ctl.rc_gp_output_ts),
10044 					   5, line, NULL, 0);
10045 		if (rack->rc_gp_filled &&
10046 		    ((tp->gput_ack - tp->gput_seq) <
10047 		     max(rc_init_window(rack), (MIN_GP_WIN *
10048 						ctf_fixed_maxseg(tp))))) {
10049 			uint32_t ideal_amount;
10050 
10051 			ideal_amount = rack_get_measure_window(tp, rack);
10052 			if (ideal_amount > sbavail(&tptosocket(tp)->so_snd)) {
10053 				/*
10054 				 * There is no sense of continuing this measurement
10055 				 * because its too small to gain us anything we
10056 				 * trust. Skip it and that way we can start a new
10057 				 * measurement quicker.
10058 				 */
10059 				tp->t_flags &= ~TF_GPUTINPROG;
10060 				rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq,
10061 							   0, 0,
10062 							   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) |
10063 							    (uint64_t)rack->r_ctl.rc_gp_output_ts),
10064 							   6, __LINE__, NULL, 0);
10065 			} else {
10066 				/*
10067 				 * Reset the window further out.
10068 				 */
10069 				tp->gput_ack = tp->gput_seq + ideal_amount;
10070 			}
10071 		}
10072 		rack_tend_gp_marks(tp, rack);
10073 		rack_log_gpset(rack, tp->gput_ack, 0, 0, line, 2, rsm);
10074 	}
10075 }
10076 
10077 static inline int
10078 is_rsm_inside_declared_tlp_block(struct tcp_rack *rack, struct rack_sendmap *rsm)
10079 {
10080 	if (SEQ_LT(rsm->r_end, rack->r_ctl.last_tlp_acked_start)) {
10081 		/* Behind our TLP definition or right at */
10082 		return (0);
10083 	}
10084 	if (SEQ_GT(rsm->r_start, rack->r_ctl.last_tlp_acked_end)) {
10085 		/* The start is beyond or right at our end of TLP definition */
10086 		return (0);
10087 	}
10088 	/* It has to be a sub-part of the original TLP recorded */
10089 	return (1);
10090 }
10091 
10092 static uint32_t
10093 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack, struct sackblk *sack,
10094 		   struct tcpopt *to, struct rack_sendmap **prsm, uint32_t cts,
10095 		   int *no_extra,
10096 		   int *moved_two, uint32_t segsiz)
10097 {
10098 	uint32_t start, end, changed = 0;
10099 	struct rack_sendmap stack_map;
10100 	struct rack_sendmap *rsm, *nrsm, *prev, *next;
10101 	int insret __diagused;
10102 	int32_t used_ref = 1;
10103 	int moved = 0;
10104 #ifdef TCP_SAD_DETECTION
10105 	int allow_segsiz;
10106 	int first_time_through = 1;
10107 #endif
10108 	int noextra = 0;
10109 	int can_use_hookery = 0;
10110 
10111 	start = sack->start;
10112 	end = sack->end;
10113 	rsm = *prsm;
10114 
10115 #ifdef TCP_SAD_DETECTION
10116 	/*
10117 	 * There are a strange number of proxys and meddle boxes in the world
10118 	 * that seem to cut up segments on different boundaries. This gets us
10119 	 * smaller sacks that are still ok in terms of it being an attacker.
10120 	 * We use the base segsiz to calculate an allowable smallness but
10121 	 * also enforce a min on the segsiz in case it is an attacker playing
10122 	 * games with MSS. So basically if the sack arrives and it is
10123 	 * larger than a worse case 960 bytes, we don't classify the guy
10124 	 * as supicious.
10125 	 */
10126 	allow_segsiz = max(segsiz, 1200) * sad_seg_size_per;
10127 	allow_segsiz /= 1000;
10128 #endif
10129 do_rest_ofb:
10130 	if ((rsm == NULL) ||
10131 	    (SEQ_LT(end, rsm->r_start)) ||
10132 	    (SEQ_GEQ(start, rsm->r_end)) ||
10133 	    (SEQ_LT(start, rsm->r_start))) {
10134 		/*
10135 		 * We are not in the right spot,
10136 		 * find the correct spot in the tree.
10137 		 */
10138 		used_ref = 0;
10139 		rsm = tqhash_find(rack->r_ctl.tqh, start);
10140 		moved++;
10141 	}
10142 	if (rsm == NULL) {
10143 		/* TSNH */
10144 		goto out;
10145 	}
10146 #ifdef TCP_SAD_DETECTION
10147 	/* Now we must check for suspicous activity */
10148 	if ((first_time_through == 1) &&
10149 	    ((end - start) < min((rsm->r_end - rsm->r_start), allow_segsiz)) &&
10150 	    ((rsm->r_flags & RACK_PMTU_CHG) == 0) &&
10151 	    ((rsm->r_flags & RACK_TLP) == 0)) {
10152 		/*
10153 		 * Its less than a full MSS or the segment being acked
10154 		 * this should only happen if the rsm in question had the
10155 		 * r_just_ret flag set <and> the end matches the end of
10156 		 * the rsm block.
10157 		 *
10158 		 * Note we do not look at segments that have had TLP's on
10159 		 * them since we can get un-reported rwnd collapses that
10160 		 * basically we TLP on and then we get back a sack block
10161 		 * that goes from the start to only a small way.
10162 		 *
10163 		 */
10164 		int loss, ok;
10165 
10166 		ok = 0;
10167 		if (SEQ_GEQ(end, rsm->r_end)) {
10168 			if (rsm->r_just_ret == 1) {
10169 				/* This was at the end of a send which is ok */
10170 				ok = 1;
10171 			} else {
10172 				/* A bit harder was it the end of our segment */
10173 				int segs, len;
10174 
10175 				len = (rsm->r_end - rsm->r_start);
10176 				segs = len / segsiz;
10177 				segs *= segsiz;
10178 				if ((segs + (rsm->r_end - start)) == len) {
10179 					/*
10180 					 * So this last bit was the
10181 					 * end of our send if we cut it
10182 					 * up into segsiz pieces so its ok.
10183 					 */
10184 					ok = 1;
10185 				}
10186 			}
10187 		}
10188 		if (ok == 0) {
10189 			/*
10190 			 * This guy is doing something suspicious
10191 			 * lets start detection.
10192 			 */
10193 			if (rack->rc_suspicious == 0) {
10194 				tcp_trace_point(rack->rc_tp, TCP_TP_SAD_SUSPECT);
10195 				counter_u64_add(rack_sack_attacks_suspect, 1);
10196 				rack->rc_suspicious = 1;
10197 				rack_log_sad(rack, 4);
10198 				if (tcp_bblogging_on(rack->rc_tp)) {
10199 					union tcp_log_stackspecific log;
10200 					struct timeval tv;
10201 
10202 					memset(&log.u_bbr, 0, sizeof(log.u_bbr));
10203 					log.u_bbr.flex1 = end;
10204 					log.u_bbr.flex2 = start;
10205 					log.u_bbr.flex3 = rsm->r_end;
10206 					log.u_bbr.flex4 = rsm->r_start;
10207 					log.u_bbr.flex5 = segsiz;
10208 					log.u_bbr.flex6 = rsm->r_fas;
10209 					log.u_bbr.flex7 = rsm->r_bas;
10210 					log.u_bbr.flex8 = 5;
10211 					log.u_bbr.pkts_out = rsm->r_flags;
10212 					log.u_bbr.bbr_state = rack->rc_suspicious;
10213 					log.u_bbr.bbr_substate = rsm->r_just_ret;
10214 					log.u_bbr.timeStamp = tcp_get_usecs(&tv);
10215 					log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
10216 					TCP_LOG_EVENTP(rack->rc_tp, NULL,
10217 						       &rack->rc_inp->inp_socket->so_rcv,
10218 						       &rack->rc_inp->inp_socket->so_snd,
10219 						       TCP_SAD_DETECTION, 0,
10220 						       0, &log, false, &tv);
10221 				}
10222 			}
10223 			/* You loose some ack count every time you sack
10224 			 * a small bit that is not butting to the end of
10225 			 * what we have sent. This is because we never
10226 			 * send small bits unless its the end of the sb.
10227 			 * Anyone sending a sack that is not at the end
10228 			 * is thus very very suspicious.
10229 			 */
10230 			loss = (segsiz/2) / (end - start);
10231 			if (loss < rack->r_ctl.ack_count)
10232 				rack->r_ctl.ack_count -= loss;
10233 			else
10234 				rack->r_ctl.ack_count = 0;
10235 		}
10236 	}
10237 	first_time_through = 0;
10238 #endif
10239 	/* Ok we have an ACK for some piece of this rsm */
10240 	if (rsm->r_start != start) {
10241 		if ((rsm->r_flags & RACK_ACKED) == 0) {
10242 			/*
10243 			 * Before any splitting or hookery is
10244 			 * done is it a TLP of interest i.e. rxt?
10245 			 */
10246 			if ((rsm->r_flags & RACK_TLP) &&
10247 			    (rsm->r_rtr_cnt > 1)) {
10248 				/*
10249 				 * We are splitting a rxt TLP, check
10250 				 * if we need to save off the start/end
10251 				 */
10252 				if (rack->rc_last_tlp_acked_set &&
10253 				    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
10254 					/*
10255 					 * We already turned this on since we are inside
10256 					 * the previous one was a partially sack now we
10257 					 * are getting another one (maybe all of it).
10258 					 *
10259 					 */
10260 					rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
10261 					/*
10262 					 * Lets make sure we have all of it though.
10263 					 */
10264 					if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
10265 						rack->r_ctl.last_tlp_acked_start = rsm->r_start;
10266 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
10267 								     rack->r_ctl.last_tlp_acked_end);
10268 					}
10269 					if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
10270 						rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10271 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
10272 								     rack->r_ctl.last_tlp_acked_end);
10273 					}
10274 				} else {
10275 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
10276 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10277 					rack->rc_last_tlp_past_cumack = 0;
10278 					rack->rc_last_tlp_acked_set = 1;
10279 					rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
10280 				}
10281 			}
10282 			/**
10283 			 * Need to split this in two pieces the before and after,
10284 			 * the before remains in the map, the after must be
10285 			 * added. In other words we have:
10286 			 * rsm        |--------------|
10287 			 * sackblk        |------->
10288 			 * rsm will become
10289 			 *     rsm    |---|
10290 			 * and nrsm will be  the sacked piece
10291 			 *     nrsm       |----------|
10292 			 *
10293 			 * But before we start down that path lets
10294 			 * see if the sack spans over on top of
10295 			 * the next guy and it is already sacked.
10296 			 *
10297 			 */
10298 			/*
10299 			 * Hookery can only be used if the two entries
10300 			 * are in the same bucket and neither one of
10301 			 * them staddle the bucket line.
10302 			 */
10303 			next = tqhash_next(rack->r_ctl.tqh, rsm);
10304 			if (next &&
10305 			    (rsm->bindex == next->bindex) &&
10306 			    ((rsm->r_flags & RACK_STRADDLE) == 0) &&
10307 			    ((next->r_flags & RACK_STRADDLE) == 0) &&
10308 			    ((rsm->r_flags & RACK_IS_PCM) == 0) &&
10309 			    ((next->r_flags & RACK_IS_PCM) == 0) &&
10310 			    (rsm->r_flags & RACK_IN_GP_WIN) &&
10311 			    (next->r_flags & RACK_IN_GP_WIN))
10312 				can_use_hookery = 1;
10313 			else
10314 				can_use_hookery = 0;
10315 			if (next && can_use_hookery &&
10316 			    (next->r_flags & RACK_ACKED) &&
10317 			    SEQ_GEQ(end, next->r_start)) {
10318 				/**
10319 				 * So the next one is already acked, and
10320 				 * we can thus by hookery use our stack_map
10321 				 * to reflect the piece being sacked and
10322 				 * then adjust the two tree entries moving
10323 				 * the start and ends around. So we start like:
10324 				 *  rsm     |------------|             (not-acked)
10325 				 *  next                 |-----------| (acked)
10326 				 *  sackblk        |-------->
10327 				 *  We want to end like so:
10328 				 *  rsm     |------|                   (not-acked)
10329 				 *  next           |-----------------| (acked)
10330 				 *  nrsm           |-----|
10331 				 * Where nrsm is a temporary stack piece we
10332 				 * use to update all the gizmos.
10333 				 */
10334 				/* Copy up our fudge block */
10335 				noextra++;
10336 				nrsm = &stack_map;
10337 				memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
10338 				/* Now adjust our tree blocks */
10339 				tqhash_update_end(rack->r_ctl.tqh, rsm, start);
10340 				next->r_start = start;
10341  				rsm->r_flags |= RACK_SHUFFLED;
10342 				next->r_flags |= RACK_SHUFFLED;
10343 				/* Now we must adjust back where next->m is */
10344 				rack_setup_offset_for_rsm(rack, rsm, next);
10345 				/*
10346 				 * Which timestamp do we keep? It is rather
10347 				 * important in GP measurements to have the
10348 				 * accurate end of the send window.
10349 				 *
10350 				 * We keep the largest value, which is the newest
10351 				 * send. We do this in case a segment that is
10352 				 * joined together and not part of a GP estimate
10353 				 * later gets expanded into the GP estimate.
10354 				 *
10355 				 * We prohibit the merging of unlike kinds i.e.
10356 				 * all pieces that are in the GP estimate can be
10357 				 * merged and all pieces that are not in a GP estimate
10358 				 * can be merged, but not disimilar pieces. Combine
10359 				 * this with taking the highest here and we should
10360 				 * be ok unless of course the client reneges. Then
10361 				 * all bets are off.
10362 				 */
10363 				if (next->r_tim_lastsent[(next->r_rtr_cnt-1)] <
10364 				    nrsm->r_tim_lastsent[(nrsm->r_rtr_cnt-1)])
10365 					next->r_tim_lastsent[(next->r_rtr_cnt-1)] = nrsm->r_tim_lastsent[(nrsm->r_rtr_cnt-1)];
10366 				/*
10367 				 * And we must keep the newest ack arrival time.
10368 				 */
10369 				if (next->r_ack_arrival <
10370 				    rack_to_usec_ts(&rack->r_ctl.act_rcv_time))
10371 					next->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
10372 
10373 
10374 				/* We don't need to adjust rsm, it did not change */
10375 				/* Clear out the dup ack count of the remainder */
10376 				rsm->r_dupack = 0;
10377 				rsm->r_just_ret = 0;
10378 				rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
10379 				/* Now lets make sure our fudge block is right */
10380 				nrsm->r_start = start;
10381 				/* Now lets update all the stats and such */
10382 				rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0);
10383 				if (rack->app_limited_needs_set)
10384 					rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END);
10385 				changed += (nrsm->r_end - nrsm->r_start);
10386 				/* You get a count for acking a whole segment or more */
10387 				if ((nrsm->r_end - nrsm->r_start) >= segsiz)
10388 					rack->r_ctl.ack_count += ((nrsm->r_end - nrsm->r_start) / segsiz);
10389 				rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start);
10390 				if (rsm->r_flags & RACK_WAS_LOST) {
10391 					int my_chg;
10392 
10393 					my_chg = (nrsm->r_end - nrsm->r_start);
10394 					KASSERT((rack->r_ctl.rc_considered_lost >= my_chg),
10395 						("rsm:%p rack:%p rc_considered_lost goes negative", rsm,  rack));
10396 					if (my_chg <= rack->r_ctl.rc_considered_lost)
10397 						rack->r_ctl.rc_considered_lost -= my_chg;
10398 					else
10399 						rack->r_ctl.rc_considered_lost = 0;
10400 				}
10401 				if (nrsm->r_flags & RACK_SACK_PASSED) {
10402 					rack->r_ctl.rc_reorder_ts = cts;
10403 					if (rack->r_ctl.rc_reorder_ts == 0)
10404 						rack->r_ctl.rc_reorder_ts = 1;
10405 				}
10406 				/*
10407 				 * Now we want to go up from rsm (the
10408 				 * one left un-acked) to the next one
10409 				 * in the tmap. We do this so when
10410 				 * we walk backwards we include marking
10411 				 * sack-passed on rsm (The one passed in
10412 				 * is skipped since it is generally called
10413 				 * on something sacked before removing it
10414 				 * from the tmap).
10415 				 */
10416 				if (rsm->r_in_tmap) {
10417 					nrsm = TAILQ_NEXT(rsm, r_tnext);
10418 					/*
10419 					 * Now that we have the next
10420 					 * one walk backwards from there.
10421 					 */
10422 					if (nrsm && nrsm->r_in_tmap)
10423 						rack_log_sack_passed(tp, rack, nrsm, cts);
10424 				}
10425 				/* Now are we done? */
10426 				if (SEQ_LT(end, next->r_end) ||
10427 				    (end == next->r_end)) {
10428 					/* Done with block */
10429 					goto out;
10430 				}
10431 				rack_log_map_chg(tp, rack, &stack_map, rsm, next, MAP_SACK_M1, end, __LINE__);
10432 				counter_u64_add(rack_sack_used_next_merge, 1);
10433 				/* Postion for the next block */
10434 				start = next->r_end;
10435 				rsm = tqhash_next(rack->r_ctl.tqh, next);
10436 				if (rsm == NULL)
10437 					goto out;
10438 			} else {
10439 				/**
10440 				 * We can't use any hookery here, so we
10441 				 * need to split the map. We enter like
10442 				 * so:
10443 				 *  rsm      |--------|
10444 				 *  sackblk       |----->
10445 				 * We will add the new block nrsm and
10446 				 * that will be the new portion, and then
10447 				 * fall through after reseting rsm. So we
10448 				 * split and look like this:
10449 				 *  rsm      |----|
10450 				 *  sackblk       |----->
10451 				 *  nrsm          |---|
10452 				 * We then fall through reseting
10453 				 * rsm to nrsm, so the next block
10454 				 * picks it up.
10455 				 */
10456 				nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
10457 				if (nrsm == NULL) {
10458 					/*
10459 					 * failed XXXrrs what can we do but loose the sack
10460 					 * info?
10461 					 */
10462 					goto out;
10463 				}
10464 				counter_u64_add(rack_sack_splits, 1);
10465 				rack_clone_rsm(rack, nrsm, rsm, start);
10466 				moved++;
10467 				rsm->r_just_ret = 0;
10468 #ifndef INVARIANTS
10469 				(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
10470 #else
10471 				if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
10472 					panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p",
10473 					      nrsm, insret, rack, rsm);
10474 				}
10475 #endif
10476 				if (rsm->r_in_tmap) {
10477 					TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
10478 					nrsm->r_in_tmap = 1;
10479 				}
10480 				rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M2, end, __LINE__);
10481 				rsm->r_flags &= (~RACK_HAS_FIN);
10482 				/* Position us to point to the new nrsm that starts the sack blk */
10483 				rsm = nrsm;
10484 			}
10485 		} else {
10486 			/* Already sacked this piece */
10487 			counter_u64_add(rack_sack_skipped_acked, 1);
10488 			moved++;
10489 			if (end == rsm->r_end) {
10490 				/* Done with block */
10491 				rsm = tqhash_next(rack->r_ctl.tqh, rsm);
10492 				goto out;
10493 			} else if (SEQ_LT(end, rsm->r_end)) {
10494 				/* A partial sack to a already sacked block */
10495 				moved++;
10496 				rsm = tqhash_next(rack->r_ctl.tqh, rsm);
10497 				goto out;
10498 			} else {
10499 				/*
10500 				 * The end goes beyond this guy
10501 				 * reposition the start to the
10502 				 * next block.
10503 				 */
10504 				start = rsm->r_end;
10505 				rsm = tqhash_next(rack->r_ctl.tqh, rsm);
10506 				if (rsm == NULL)
10507 					goto out;
10508 			}
10509 		}
10510 	}
10511 	if (SEQ_GEQ(end, rsm->r_end)) {
10512 		/**
10513 		 * The end of this block is either beyond this guy or right
10514 		 * at this guy. I.e.:
10515 		 *  rsm ---                 |-----|
10516 		 *  end                     |-----|
10517 		 *  <or>
10518 		 *  end                     |---------|
10519 		 */
10520 		if ((rsm->r_flags & RACK_ACKED) == 0) {
10521 			/*
10522 			 * Is it a TLP of interest?
10523 			 */
10524 			if ((rsm->r_flags & RACK_TLP) &&
10525 			    (rsm->r_rtr_cnt > 1)) {
10526 				/*
10527 				 * We are splitting a rxt TLP, check
10528 				 * if we need to save off the start/end
10529 				 */
10530 				if (rack->rc_last_tlp_acked_set &&
10531 				    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
10532 					/*
10533 					 * We already turned this on since we are inside
10534 					 * the previous one was a partially sack now we
10535 					 * are getting another one (maybe all of it).
10536 					 */
10537 					rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
10538 					/*
10539 					 * Lets make sure we have all of it though.
10540 					 */
10541 					if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
10542 						rack->r_ctl.last_tlp_acked_start = rsm->r_start;
10543 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
10544 								     rack->r_ctl.last_tlp_acked_end);
10545 					}
10546 					if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
10547 						rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10548 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
10549 								     rack->r_ctl.last_tlp_acked_end);
10550 					}
10551 				} else {
10552 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
10553 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10554 					rack->rc_last_tlp_past_cumack = 0;
10555 					rack->rc_last_tlp_acked_set = 1;
10556 					rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
10557 				}
10558 			}
10559 			rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0);
10560 			changed += (rsm->r_end - rsm->r_start);
10561 			/* You get a count for acking a whole segment or more */
10562 			if ((rsm->r_end - rsm->r_start) >= segsiz)
10563 				rack->r_ctl.ack_count += ((rsm->r_end - rsm->r_start) / segsiz);
10564 			if (rsm->r_flags & RACK_WAS_LOST) {
10565 				int my_chg;
10566 
10567 				my_chg = (rsm->r_end - rsm->r_start);
10568 				rsm->r_flags &= ~RACK_WAS_LOST;
10569 				KASSERT((rack->r_ctl.rc_considered_lost >= my_chg),
10570 					("rsm:%p rack:%p rc_considered_lost goes negative", rsm,  rack));
10571 				if (my_chg <= rack->r_ctl.rc_considered_lost)
10572 					rack->r_ctl.rc_considered_lost -= my_chg;
10573 				else
10574 					rack->r_ctl.rc_considered_lost = 0;
10575 			}
10576 			rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
10577 			if (rsm->r_in_tmap) /* should be true */
10578 				rack_log_sack_passed(tp, rack, rsm, cts);
10579 			/* Is Reordering occuring? */
10580 			if (rsm->r_flags & RACK_SACK_PASSED) {
10581 				rsm->r_flags &= ~RACK_SACK_PASSED;
10582 				rack->r_ctl.rc_reorder_ts = cts;
10583 				if (rack->r_ctl.rc_reorder_ts == 0)
10584 					rack->r_ctl.rc_reorder_ts = 1;
10585 			}
10586 			if (rack->app_limited_needs_set)
10587 				rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END);
10588 			rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
10589 			rsm->r_flags |= RACK_ACKED;
10590 			rack_update_pcm_ack(rack, 0, rsm->r_start, rsm->r_end);
10591 			if (rsm->r_in_tmap) {
10592 				TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
10593 				rsm->r_in_tmap = 0;
10594 			}
10595 			rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_SACK_M3, end, __LINE__);
10596 		} else {
10597 			counter_u64_add(rack_sack_skipped_acked, 1);
10598 			moved++;
10599 		}
10600 		if (end == rsm->r_end) {
10601 			/* This block only - done, setup for next */
10602 			goto out;
10603 		}
10604 		/*
10605 		 * There is more not coverend by this rsm move on
10606 		 * to the next block in the tail queue hash table.
10607 		 */
10608 		nrsm = tqhash_next(rack->r_ctl.tqh, rsm);
10609 		start = rsm->r_end;
10610 		rsm = nrsm;
10611 		if (rsm == NULL)
10612 			goto out;
10613 		goto do_rest_ofb;
10614 	}
10615 	/**
10616 	 * The end of this sack block is smaller than
10617 	 * our rsm i.e.:
10618 	 *  rsm ---                 |-----|
10619 	 *  end                     |--|
10620 	 */
10621 	if ((rsm->r_flags & RACK_ACKED) == 0) {
10622 		/*
10623 		 * Is it a TLP of interest?
10624 		 */
10625 		if ((rsm->r_flags & RACK_TLP) &&
10626 		    (rsm->r_rtr_cnt > 1)) {
10627 			/*
10628 			 * We are splitting a rxt TLP, check
10629 			 * if we need to save off the start/end
10630 			 */
10631 			if (rack->rc_last_tlp_acked_set &&
10632 			    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
10633 				/*
10634 				 * We already turned this on since we are inside
10635 				 * the previous one was a partially sack now we
10636 				 * are getting another one (maybe all of it).
10637 				 */
10638 				rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
10639 				/*
10640 				 * Lets make sure we have all of it though.
10641 				 */
10642 				if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
10643 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
10644 					rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
10645 							     rack->r_ctl.last_tlp_acked_end);
10646 				}
10647 				if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
10648 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10649 					rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
10650 							     rack->r_ctl.last_tlp_acked_end);
10651 				}
10652 			} else {
10653 				rack->r_ctl.last_tlp_acked_start = rsm->r_start;
10654 				rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10655 				rack->rc_last_tlp_past_cumack = 0;
10656 				rack->rc_last_tlp_acked_set = 1;
10657 				rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
10658 			}
10659 		}
10660 		/*
10661 		 * Hookery can only be used if the two entries
10662 		 * are in the same bucket and neither one of
10663 		 * them staddle the bucket line.
10664 		 */
10665 		prev = tqhash_prev(rack->r_ctl.tqh, rsm);
10666 		if (prev &&
10667 		    (rsm->bindex == prev->bindex) &&
10668 		    ((rsm->r_flags & RACK_STRADDLE) == 0) &&
10669 		    ((prev->r_flags & RACK_STRADDLE) == 0) &&
10670 		    ((rsm->r_flags & RACK_IS_PCM) == 0) &&
10671 		    ((prev->r_flags & RACK_IS_PCM) == 0) &&
10672 		    (rsm->r_flags & RACK_IN_GP_WIN) &&
10673 		    (prev->r_flags & RACK_IN_GP_WIN))
10674 			can_use_hookery = 1;
10675 		else
10676 			can_use_hookery = 0;
10677 		if (prev && can_use_hookery &&
10678 		    (prev->r_flags & RACK_ACKED)) {
10679 			/**
10680 			 * Goal, we want the right remainder of rsm to shrink
10681 			 * in place and span from (rsm->r_start = end) to rsm->r_end.
10682 			 * We want to expand prev to go all the way
10683 			 * to prev->r_end <- end.
10684 			 * so in the tree we have before:
10685 			 *   prev     |--------|         (acked)
10686 			 *   rsm               |-------| (non-acked)
10687 			 *   sackblk           |-|
10688 			 * We churn it so we end up with
10689 			 *   prev     |----------|       (acked)
10690 			 *   rsm                 |-----| (non-acked)
10691 			 *   nrsm              |-| (temporary)
10692 			 *
10693 			 * Note if either prev/rsm is a TLP we don't
10694 			 * do this.
10695 			 */
10696 			noextra++;
10697 			nrsm = &stack_map;
10698 			memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
10699 			tqhash_update_end(rack->r_ctl.tqh, prev, end);
10700 			rsm->r_start = end;
10701 			rsm->r_flags |= RACK_SHUFFLED;
10702 			prev->r_flags |= RACK_SHUFFLED;
10703 			/* Now adjust nrsm (stack copy) to be
10704 			 * the one that is the small
10705 			 * piece that was "sacked".
10706 			 */
10707 			nrsm->r_end = end;
10708 			rsm->r_dupack = 0;
10709 			/*
10710 			 * Which timestamp do we keep? It is rather
10711 			 * important in GP measurements to have the
10712 			 * accurate end of the send window.
10713 			 *
10714 			 * We keep the largest value, which is the newest
10715 			 * send. We do this in case a segment that is
10716 			 * joined together and not part of a GP estimate
10717 			 * later gets expanded into the GP estimate.
10718 			 *
10719 			 * We prohibit the merging of unlike kinds i.e.
10720 			 * all pieces that are in the GP estimate can be
10721 			 * merged and all pieces that are not in a GP estimate
10722 			 * can be merged, but not disimilar pieces. Combine
10723 			 * this with taking the highest here and we should
10724 			 * be ok unless of course the client reneges. Then
10725 			 * all bets are off.
10726 			 */
10727 			if(prev->r_tim_lastsent[(prev->r_rtr_cnt-1)] <
10728 			   nrsm->r_tim_lastsent[(nrsm->r_rtr_cnt-1)]) {
10729 				prev->r_tim_lastsent[(prev->r_rtr_cnt-1)] = nrsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
10730 			}
10731 			/*
10732 			 * And we must keep the newest ack arrival time.
10733 			 */
10734 
10735 			if(prev->r_ack_arrival <
10736 			   rack_to_usec_ts(&rack->r_ctl.act_rcv_time))
10737 				prev->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
10738 
10739 			rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
10740 			/*
10741 			 * Now that the rsm has had its start moved forward
10742 			 * lets go ahead and get its new place in the world.
10743 			 */
10744 			rack_setup_offset_for_rsm(rack, prev, rsm);
10745 			/*
10746 			 * Now nrsm is our new little piece
10747 			 * that is acked (which was merged
10748 			 * to prev). Update the rtt and changed
10749 			 * based on that. Also check for reordering.
10750 			 */
10751 			rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0);
10752 			if (rack->app_limited_needs_set)
10753 				rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END);
10754 			changed += (nrsm->r_end - nrsm->r_start);
10755 			/* You get a count for acking a whole segment or more */
10756 			if ((nrsm->r_end - nrsm->r_start) >= segsiz)
10757 				rack->r_ctl.ack_count += ((nrsm->r_end - nrsm->r_start) / segsiz);
10758 
10759 			rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start);
10760 			if (rsm->r_flags & RACK_WAS_LOST) {
10761 				int my_chg;
10762 
10763 				my_chg = (nrsm->r_end - nrsm->r_start);
10764 				KASSERT((rack->r_ctl.rc_considered_lost >= my_chg),
10765 					("rsm:%p rack:%p rc_considered_lost goes negative", rsm,  rack));
10766 				if (my_chg <= rack->r_ctl.rc_considered_lost)
10767 					rack->r_ctl.rc_considered_lost -= my_chg;
10768 				else
10769 					rack->r_ctl.rc_considered_lost = 0;
10770 			}
10771 			if (nrsm->r_flags & RACK_SACK_PASSED) {
10772 				rack->r_ctl.rc_reorder_ts = cts;
10773 				if (rack->r_ctl.rc_reorder_ts == 0)
10774 					rack->r_ctl.rc_reorder_ts = 1;
10775 			}
10776 			rack_log_map_chg(tp, rack, prev, &stack_map, rsm, MAP_SACK_M4, end, __LINE__);
10777 			rsm = prev;
10778 			counter_u64_add(rack_sack_used_prev_merge, 1);
10779 		} else {
10780 			/**
10781 			 * This is the case where our previous
10782 			 * block is not acked either, so we must
10783 			 * split the block in two.
10784 			 */
10785 			nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
10786 			if (nrsm == NULL) {
10787 				/* failed rrs what can we do but loose the sack info? */
10788 				goto out;
10789 			}
10790 			if ((rsm->r_flags & RACK_TLP) &&
10791 			    (rsm->r_rtr_cnt > 1)) {
10792 				/*
10793 				 * We are splitting a rxt TLP, check
10794 				 * if we need to save off the start/end
10795 				 */
10796 				if (rack->rc_last_tlp_acked_set &&
10797 				    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
10798 					/*
10799 					 * We already turned this on since this block is inside
10800 					 * the previous one was a partially sack now we
10801 					 * are getting another one (maybe all of it).
10802 					 */
10803 					rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
10804 					/*
10805 					 * Lets make sure we have all of it though.
10806 					 */
10807 					if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
10808 						rack->r_ctl.last_tlp_acked_start = rsm->r_start;
10809 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
10810 								     rack->r_ctl.last_tlp_acked_end);
10811 					}
10812 					if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
10813 						rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10814 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
10815 								     rack->r_ctl.last_tlp_acked_end);
10816 					}
10817 				} else {
10818 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
10819 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10820 					rack->rc_last_tlp_acked_set = 1;
10821 					rack->rc_last_tlp_past_cumack = 0;
10822 					rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
10823 				}
10824 			}
10825 			/**
10826 			 * In this case nrsm becomes
10827 			 * nrsm->r_start = end;
10828 			 * nrsm->r_end = rsm->r_end;
10829 			 * which is un-acked.
10830 			 * <and>
10831 			 * rsm->r_end = nrsm->r_start;
10832 			 * i.e. the remaining un-acked
10833 			 * piece is left on the left
10834 			 * hand side.
10835 			 *
10836 			 * So we start like this
10837 			 * rsm      |----------| (not acked)
10838 			 * sackblk  |---|
10839 			 * build it so we have
10840 			 * rsm      |---|         (acked)
10841 			 * nrsm         |------|  (not acked)
10842 			 */
10843 			counter_u64_add(rack_sack_splits, 1);
10844 			rack_clone_rsm(rack, nrsm, rsm, end);
10845 			moved++;
10846 			rsm->r_flags &= (~RACK_HAS_FIN);
10847 			rsm->r_just_ret = 0;
10848 #ifndef INVARIANTS
10849 			(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
10850 #else
10851 			if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
10852 				panic("Insert in tailq_hash of %p fails ret:% rack:%p rsm:%p",
10853 				      nrsm, insret, rack, rsm);
10854 			}
10855 #endif
10856 			if (rsm->r_in_tmap) {
10857 				TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
10858 				nrsm->r_in_tmap = 1;
10859 			}
10860 			nrsm->r_dupack = 0;
10861 			rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2);
10862 			rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0);
10863 			changed += (rsm->r_end - rsm->r_start);
10864 			/* You get a count for acking a whole segment or more */
10865 			if ((rsm->r_end - rsm->r_start) >= segsiz)
10866 				rack->r_ctl.ack_count += ((rsm->r_end - rsm->r_start) / segsiz);
10867 			if (rsm->r_flags & RACK_WAS_LOST) {
10868 				int my_chg;
10869 
10870 				my_chg = (rsm->r_end - rsm->r_start);
10871 				rsm->r_flags &= ~RACK_WAS_LOST;
10872 				KASSERT((rack->r_ctl.rc_considered_lost >= my_chg),
10873 					("rsm:%p rack:%p rc_considered_lost goes negative", rsm,  rack));
10874 				if (my_chg <= rack->r_ctl.rc_considered_lost)
10875 					rack->r_ctl.rc_considered_lost -= my_chg;
10876 				else
10877 					rack->r_ctl.rc_considered_lost = 0;
10878 			}
10879 			rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
10880 
10881 			if (rsm->r_in_tmap) /* should be true */
10882 				rack_log_sack_passed(tp, rack, rsm, cts);
10883 			/* Is Reordering occuring? */
10884 			if (rsm->r_flags & RACK_SACK_PASSED) {
10885 				rsm->r_flags &= ~RACK_SACK_PASSED;
10886 				rack->r_ctl.rc_reorder_ts = cts;
10887 				if (rack->r_ctl.rc_reorder_ts == 0)
10888 					rack->r_ctl.rc_reorder_ts = 1;
10889 			}
10890 			if (rack->app_limited_needs_set)
10891 				rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END);
10892 			rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
10893 			rsm->r_flags |= RACK_ACKED;
10894 			rack_update_pcm_ack(rack, 0, rsm->r_start, rsm->r_end);
10895 			rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M5, end, __LINE__);
10896 			if (rsm->r_in_tmap) {
10897 				TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
10898 				rsm->r_in_tmap = 0;
10899 			}
10900 		}
10901 	} else if (start != end){
10902 		/*
10903 		 * The block was already acked.
10904 		 */
10905 		counter_u64_add(rack_sack_skipped_acked, 1);
10906 		moved++;
10907 	}
10908 out:
10909 	if (rsm &&
10910 	    ((rsm->r_flags & RACK_TLP) == 0) &&
10911 	    (rsm->r_flags & RACK_ACKED)) {
10912 		/*
10913 		 * Now can we merge where we worked
10914 		 * with either the previous or
10915 		 * next block?
10916 		 */
10917 		next = tqhash_next(rack->r_ctl.tqh, rsm);
10918 		while (next) {
10919 			if (next->r_flags & RACK_TLP)
10920 				break;
10921 			/* Only allow merges between ones in or out of GP window */
10922 			if ((next->r_flags & RACK_IN_GP_WIN) &&
10923 			    ((rsm->r_flags & RACK_IN_GP_WIN) == 0)) {
10924 				break;
10925 			}
10926 			if ((rsm->r_flags & RACK_IN_GP_WIN) &&
10927 			    ((next->r_flags & RACK_IN_GP_WIN) == 0)) {
10928 				break;
10929 			}
10930 			if (rsm->bindex != next->bindex)
10931 				break;
10932 			if (rsm->r_flags & RACK_STRADDLE)
10933 				break;
10934 			if (rsm->r_flags & RACK_IS_PCM)
10935 				break;
10936 			if (next->r_flags & RACK_STRADDLE)
10937 				break;
10938 			if (next->r_flags & RACK_IS_PCM)
10939 				break;
10940 			if (next->r_flags & RACK_ACKED) {
10941 				/* yep this and next can be merged */
10942 				rsm = rack_merge_rsm(rack, rsm, next);
10943 				noextra++;
10944 				next = tqhash_next(rack->r_ctl.tqh, rsm);
10945 			} else
10946 				break;
10947 		}
10948 		/* Now what about the previous? */
10949 		prev = tqhash_prev(rack->r_ctl.tqh, rsm);
10950 		while (prev) {
10951 			if (prev->r_flags & RACK_TLP)
10952 				break;
10953 			/* Only allow merges between ones in or out of GP window */
10954 			if ((prev->r_flags & RACK_IN_GP_WIN) &&
10955 			    ((rsm->r_flags & RACK_IN_GP_WIN) == 0)) {
10956 				break;
10957 			}
10958 			if ((rsm->r_flags & RACK_IN_GP_WIN) &&
10959 			    ((prev->r_flags & RACK_IN_GP_WIN) == 0)) {
10960 				break;
10961 			}
10962 			if (rsm->bindex != prev->bindex)
10963 				break;
10964 			if (rsm->r_flags & RACK_STRADDLE)
10965 				break;
10966 			if (rsm->r_flags & RACK_IS_PCM)
10967 				break;
10968 			if (prev->r_flags & RACK_STRADDLE)
10969 				break;
10970 			if (prev->r_flags & RACK_IS_PCM)
10971 				break;
10972 			if (prev->r_flags & RACK_ACKED) {
10973 				/* yep the previous and this can be merged */
10974 				rsm = rack_merge_rsm(rack, prev, rsm);
10975 				noextra++;
10976 				prev = tqhash_prev(rack->r_ctl.tqh, rsm);
10977 			} else
10978 				break;
10979 		}
10980 	}
10981 	if (used_ref == 0) {
10982 		counter_u64_add(rack_sack_proc_all, 1);
10983 	} else {
10984 		counter_u64_add(rack_sack_proc_short, 1);
10985 	}
10986 	/* Save off the next one for quick reference. */
10987 	nrsm = tqhash_find(rack->r_ctl.tqh, end);
10988 	*prsm = rack->r_ctl.rc_sacklast = nrsm;
10989 	/* Pass back the moved. */
10990 	*moved_two = moved;
10991 	*no_extra = noextra;
10992 	if (IN_RECOVERY(tp->t_flags)) {
10993 		rack->r_ctl.bytes_acked_in_recovery += changed;
10994 	}
10995 	return (changed);
10996 }
10997 
10998 static void inline
10999 rack_peer_reneges(struct tcp_rack *rack, struct rack_sendmap *rsm, tcp_seq th_ack)
11000 {
11001 	struct rack_sendmap *tmap;
11002 
11003 	tmap = NULL;
11004 	while (rsm && (rsm->r_flags & RACK_ACKED)) {
11005 		/* Its no longer sacked, mark it so */
11006 		rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
11007 #ifdef INVARIANTS
11008 		if (rsm->r_in_tmap) {
11009 			panic("rack:%p rsm:%p flags:0x%x in tmap?",
11010 			      rack, rsm, rsm->r_flags);
11011 		}
11012 #endif
11013 		rsm->r_flags &= ~(RACK_ACKED|RACK_SACK_PASSED|RACK_WAS_SACKPASS);
11014 		/* Rebuild it into our tmap */
11015 		if (tmap == NULL) {
11016 			TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
11017 			tmap = rsm;
11018 		} else {
11019 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, tmap, rsm, r_tnext);
11020 			tmap = rsm;
11021 		}
11022 		tmap->r_in_tmap = 1;
11023 		rsm = tqhash_next(rack->r_ctl.tqh, rsm);
11024 	}
11025 	/*
11026 	 * Now lets possibly clear the sack filter so we start
11027 	 * recognizing sacks that cover this area.
11028 	 */
11029 	sack_filter_clear(&rack->r_ctl.rack_sf, th_ack);
11030 
11031 }
11032 
11033 static void
11034 rack_do_decay(struct tcp_rack *rack)
11035 {
11036 	struct timeval res;
11037 
11038 #define	timersub(tvp, uvp, vvp)						\
11039 	do {								\
11040 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
11041 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
11042 		if ((vvp)->tv_usec < 0) {				\
11043 			(vvp)->tv_sec--;				\
11044 			(vvp)->tv_usec += 1000000;			\
11045 		}							\
11046 	} while (0)
11047 
11048 	timersub(&rack->r_ctl.act_rcv_time, &rack->r_ctl.rc_last_time_decay, &res);
11049 #undef timersub
11050 
11051 	rack->r_ctl.input_pkt++;
11052 	if ((rack->rc_in_persist) ||
11053 	    (res.tv_sec >= 1) ||
11054 	    (rack->rc_tp->snd_max == rack->rc_tp->snd_una)) {
11055 		/*
11056 		 * Check for decay of non-SAD,
11057 		 * we want all SAD detection metrics to
11058 		 * decay 1/4 per second (or more) passed.
11059 		 * Current default is 800 so it decays
11060 		 * 80% every second.
11061 		 */
11062 #ifdef TCP_SAD_DETECTION
11063 		uint32_t pkt_delta;
11064 
11065 		pkt_delta = rack->r_ctl.input_pkt - rack->r_ctl.saved_input_pkt;
11066 #endif
11067 		/* Update our saved tracking values */
11068 		rack->r_ctl.saved_input_pkt = rack->r_ctl.input_pkt;
11069 		rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time;
11070 		/* Now do we escape without decay? */
11071 #ifdef TCP_SAD_DETECTION
11072 		if (rack->rc_in_persist ||
11073 		    (rack->rc_tp->snd_max == rack->rc_tp->snd_una) ||
11074 		    (pkt_delta < tcp_sad_low_pps)){
11075 			/*
11076 			 * We don't decay idle connections
11077 			 * or ones that have a low input pps.
11078 			 */
11079 			return;
11080 		}
11081 		/* Decay the counters */
11082 		rack->r_ctl.ack_count = ctf_decay_count(rack->r_ctl.ack_count,
11083 							tcp_sad_decay_val);
11084 		rack->r_ctl.sack_count = ctf_decay_count(rack->r_ctl.sack_count,
11085 							 tcp_sad_decay_val);
11086 		rack->r_ctl.sack_moved_extra = ctf_decay_count(rack->r_ctl.sack_moved_extra,
11087 							       tcp_sad_decay_val);
11088 		rack->r_ctl.sack_noextra_move = ctf_decay_count(rack->r_ctl.sack_noextra_move,
11089 								tcp_sad_decay_val);
11090 #endif
11091 	}
11092 }
11093 
11094 static void inline
11095 rack_rsm_sender_update(struct tcp_rack *rack, struct tcpcb *tp, struct rack_sendmap *rsm, uint8_t from)
11096 {
11097 	/*
11098 	 * We look at advancing the end send time for our GP
11099 	 * measurement tracking only as the cumulative acknowledgment
11100 	 * moves forward. You might wonder about this, why not
11101 	 * at every transmission or retransmission within the
11102 	 * GP window update the rc_gp_cumack_ts? Well its rather
11103 	 * nuanced but basically the GP window *may* expand (as
11104 	 * it does below) or worse and harder to track it may shrink.
11105 	 *
11106 	 * This last makes it impossible to track at the time of
11107 	 * the send, since you may set forward your rc_gp_cumack_ts
11108 	 * when you send, because that send *is* in your currently
11109 	 * "guessed" window, but then it shrinks. Now which was
11110 	 * the send time of the last bytes in the window, by the
11111 	 * time you ask that question that part of the sendmap
11112 	 * is freed. So you don't know and you will have too
11113 	 * long of send window. Instead by updating the time
11114 	 * marker only when the cumack advances this assures us
11115 	 * that we will have only the sends in the window of our
11116 	 * GP measurement.
11117 	 *
11118 	 * Another complication from this is the
11119 	 * merging of sendmap entries. During SACK processing this
11120 	 * can happen to conserve the sendmap size. That breaks
11121 	 * everything down in tracking the send window of the GP
11122 	 * estimate. So to prevent that and keep it working with
11123 	 * a tiny bit more limited merging, we only allow like
11124 	 * types to be merged. I.e. if two sends are in the GP window
11125 	 * then its ok to merge them together. If two sends are not
11126 	 * in the GP window its ok to merge them together too. Though
11127 	 * one send in and one send out cannot be merged. We combine
11128 	 * this with never allowing the shrinking of the GP window when
11129 	 * we are in recovery so that we can properly calculate the
11130 	 * sending times.
11131 	 *
11132 	 * This all of course seems complicated, because it is.. :)
11133 	 *
11134 	 * The cum-ack is being advanced upon the sendmap.
11135 	 * If we are not doing a GP estimate don't
11136 	 * proceed.
11137 	 */
11138 	uint64_t ts;
11139 
11140 	if ((tp->t_flags & TF_GPUTINPROG) == 0)
11141 		return;
11142 	/*
11143 	 * If this sendmap entry is going
11144 	 * beyond the measurement window we had picked,
11145 	 * expand the measurement window by that much.
11146 	 */
11147 	if (SEQ_GT(rsm->r_end, tp->gput_ack)) {
11148 		tp->gput_ack = rsm->r_end;
11149 	}
11150 	/*
11151 	 * If we have not setup a ack, then we
11152 	 * have no idea if the newly acked pieces
11153 	 * will be "in our seq measurement range". If
11154 	 * it is when we clear the app_limited_needs_set
11155 	 * flag the timestamp will be updated.
11156 	 */
11157 	if (rack->app_limited_needs_set)
11158 		return;
11159 	/*
11160 	 * Finally, we grab out the latest timestamp
11161 	 * that this packet was sent and then see
11162 	 * if:
11163 	 *  a) The packet touches are newly defined GP range.
11164 	 *  b) The time is greater than (newer) than the
11165 	 *     one we currently have. If so we update
11166 	 *     our sending end time window.
11167 	 *
11168 	 * Note we *do not* do this at send time. The reason
11169 	 * is that if you do you *may* pick up a newer timestamp
11170 	 * for a range you are not going to measure. We project
11171 	 * out how far and then sometimes modify that to be
11172 	 * smaller. If that occurs then you will have a send
11173 	 * that does not belong to the range included.
11174 	 */
11175 	if ((ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]) <=
11176 	    rack->r_ctl.rc_gp_cumack_ts)
11177 		return;
11178 	if (rack_in_gp_window(tp, rsm)) {
11179 		rack->r_ctl.rc_gp_cumack_ts = ts;
11180 		rack_log_gpset(rack, tp->gput_ack, (uint32_t)ts, rsm->r_end,
11181 			       __LINE__, from, rsm);
11182 	}
11183 }
11184 
11185 static void
11186 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)
11187 {
11188 	struct rack_sendmap *rsm;
11189 	/*
11190 	 * The ACK point is advancing to th_ack, we must drop off
11191 	 * the packets in the rack log and calculate any eligble
11192 	 * RTT's.
11193 	 */
11194 
11195 	if (sack_filter_blks_used(&rack->r_ctl.rack_sf)) {
11196 		/*
11197 		 * If we have some sack blocks in the filter
11198 		 * lets prune them out by calling sfb with no blocks.
11199 		 */
11200 		sack_filter_blks(&rack->r_ctl.rack_sf, NULL, 0, th_ack);
11201 	}
11202 	if (SEQ_GT(th_ack, tp->snd_una)) {
11203 		/* Clear any app ack remembered settings */
11204 		rack->r_ctl.cleared_app_ack = 0;
11205 	}
11206 	rack->r_wanted_output = 1;
11207 	if (SEQ_GT(th_ack, tp->snd_una))
11208 		rack->r_ctl.last_cumack_advance = acktime;
11209 
11210 	/* Tend any TLP that has been marked for 1/2 the seq space (its old)  */
11211 	if ((rack->rc_last_tlp_acked_set == 1)&&
11212 	    (rack->rc_last_tlp_past_cumack == 1) &&
11213 	    (SEQ_GT(rack->r_ctl.last_tlp_acked_start, th_ack))) {
11214 		/*
11215 		 * We have reached the point where our last rack
11216 		 * tlp retransmit sequence is ahead of the cum-ack.
11217 		 * This can only happen when the cum-ack moves all
11218 		 * the way around (its been a full 2^^31+1 bytes
11219 		 * or more since we sent a retransmitted TLP). Lets
11220 		 * turn off the valid flag since its not really valid.
11221 		 *
11222 		 * Note since sack's also turn on this event we have
11223 		 * a complication, we have to wait to age it out until
11224 		 * the cum-ack is by the TLP before checking which is
11225 		 * what the next else clause does.
11226 		 */
11227 		rack_log_dsack_event(rack, 9, __LINE__,
11228 				     rack->r_ctl.last_tlp_acked_start,
11229 				     rack->r_ctl.last_tlp_acked_end);
11230 		rack->rc_last_tlp_acked_set = 0;
11231 		rack->rc_last_tlp_past_cumack = 0;
11232 	} else if ((rack->rc_last_tlp_acked_set == 1) &&
11233 		   (rack->rc_last_tlp_past_cumack == 0) &&
11234 		   (SEQ_GEQ(th_ack, rack->r_ctl.last_tlp_acked_end))) {
11235 		/*
11236 		 * It is safe to start aging TLP's out.
11237 		 */
11238 		rack->rc_last_tlp_past_cumack = 1;
11239 	}
11240 	/* We do the same for the tlp send seq as well */
11241 	if ((rack->rc_last_sent_tlp_seq_valid == 1) &&
11242 	    (rack->rc_last_sent_tlp_past_cumack == 1) &&
11243 	    (SEQ_GT(rack->r_ctl.last_sent_tlp_seq,  th_ack))) {
11244 		rack_log_dsack_event(rack, 9, __LINE__,
11245 				     rack->r_ctl.last_sent_tlp_seq,
11246 				     (rack->r_ctl.last_sent_tlp_seq +
11247 				      rack->r_ctl.last_sent_tlp_len));
11248 		rack->rc_last_sent_tlp_seq_valid = 0;
11249 		rack->rc_last_sent_tlp_past_cumack = 0;
11250 	} else if ((rack->rc_last_sent_tlp_seq_valid == 1) &&
11251 		   (rack->rc_last_sent_tlp_past_cumack == 0) &&
11252 		   (SEQ_GEQ(th_ack, rack->r_ctl.last_sent_tlp_seq))) {
11253 		/*
11254 		 * It is safe to start aging TLP's send.
11255 		 */
11256 		rack->rc_last_sent_tlp_past_cumack = 1;
11257 	}
11258 more:
11259 	rsm = tqhash_min(rack->r_ctl.tqh);
11260 	if (rsm == NULL) {
11261 		if ((th_ack - 1) == tp->iss) {
11262 			/*
11263 			 * For the SYN incoming case we will not
11264 			 * have called tcp_output for the sending of
11265 			 * the SYN, so there will be no map. All
11266 			 * other cases should probably be a panic.
11267 			 */
11268 			return;
11269 		}
11270 		if (tp->t_flags & TF_SENTFIN) {
11271 			/* if we sent a FIN we often will not have map */
11272 			return;
11273 		}
11274 #ifdef INVARIANTS
11275 		panic("No rack map tp:%p for state:%d ack:%u rack:%p snd_una:%u snd_max:%u\n",
11276 		      tp,
11277 		      tp->t_state, th_ack, rack,
11278 		      tp->snd_una, tp->snd_max);
11279 #endif
11280 		return;
11281 	}
11282 	if (SEQ_LT(th_ack, rsm->r_start)) {
11283 		/* Huh map is missing this */
11284 #ifdef INVARIANTS
11285 		printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d\n",
11286 		       rsm->r_start,
11287 		       th_ack, tp->t_state, rack->r_state);
11288 #endif
11289 		return;
11290 	}
11291 	rack_update_rtt(tp, rack, rsm, to, cts, CUM_ACKED, th_ack);
11292 
11293 	/* Now was it a retransmitted TLP? */
11294 	if ((rsm->r_flags & RACK_TLP) &&
11295 	    (rsm->r_rtr_cnt > 1)) {
11296 		/*
11297 		 * Yes, this rsm was a TLP and retransmitted, remember that
11298 		 * since if a DSACK comes back on this we don't want
11299 		 * to think of it as a reordered segment. This may
11300 		 * get updated again with possibly even other TLPs
11301 		 * in flight, but thats ok. Only when we don't send
11302 		 * a retransmitted TLP for 1/2 the sequences space
11303 		 * will it get turned off (above).
11304 		 */
11305 		if (rack->rc_last_tlp_acked_set &&
11306 		    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
11307 			/*
11308 			 * We already turned this on since the end matches,
11309 			 * the previous one was a partially ack now we
11310 			 * are getting another one (maybe all of it).
11311 			 */
11312 			rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
11313 			/*
11314 			 * Lets make sure we have all of it though.
11315 			 */
11316 			if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
11317 				rack->r_ctl.last_tlp_acked_start = rsm->r_start;
11318 				rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
11319 						     rack->r_ctl.last_tlp_acked_end);
11320 			}
11321 			if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
11322 				rack->r_ctl.last_tlp_acked_end = rsm->r_end;
11323 				rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
11324 						     rack->r_ctl.last_tlp_acked_end);
11325 			}
11326 		} else {
11327 			rack->rc_last_tlp_past_cumack = 1;
11328 			rack->r_ctl.last_tlp_acked_start = rsm->r_start;
11329 			rack->r_ctl.last_tlp_acked_end = rsm->r_end;
11330 			rack->rc_last_tlp_acked_set = 1;
11331 			rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
11332 		}
11333 	}
11334 	/* Now do we consume the whole thing? */
11335 	rack->r_ctl.last_tmit_time_acked = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
11336 	if (SEQ_GEQ(th_ack, rsm->r_end)) {
11337 		/* Its all consumed. */
11338 		uint32_t left;
11339 		uint8_t newly_acked;
11340 
11341 		if (rsm->r_flags & RACK_WAS_LOST) {
11342 			/*
11343 			 * This can happen when we marked it as lost
11344 			 * and yet before retransmitting we get an ack
11345 			 * which can happen due to reordering.
11346 			 */
11347 			rsm->r_flags  &= ~RACK_WAS_LOST;
11348 			KASSERT((rack->r_ctl.rc_considered_lost >= (rsm->r_end - rsm->r_start)),
11349 				("rsm:%p rack:%p rc_considered_lost goes negative", rsm,  rack));
11350 			if (rack->r_ctl.rc_considered_lost >= (rsm->r_end - rsm->r_start))
11351 				rack->r_ctl.rc_considered_lost -= rsm->r_end - rsm->r_start;
11352 			else
11353 				rack->r_ctl.rc_considered_lost = 0;
11354 		}
11355 		rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_FREE, rsm->r_end, __LINE__);
11356 		rack->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
11357 		rsm->r_rtr_bytes = 0;
11358 		/*
11359 		 * Record the time of highest cumack sent if its in our measurement
11360 		 * window and possibly bump out the end.
11361 		 */
11362 		rack_rsm_sender_update(rack, tp, rsm, 4);
11363 		tqhash_remove(rack->r_ctl.tqh, rsm, REMOVE_TYPE_CUMACK);
11364 		if (rsm->r_in_tmap) {
11365 			TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
11366 			rsm->r_in_tmap = 0;
11367 		}
11368 		newly_acked = 1;
11369 		if (((rsm->r_flags & RACK_ACKED) == 0) &&
11370 		    (IN_RECOVERY(tp->t_flags))) {
11371 			rack->r_ctl.bytes_acked_in_recovery += (rsm->r_end - rsm->r_start);
11372 		}
11373 		if (rsm->r_flags & RACK_ACKED) {
11374 			/*
11375 			 * It was acked on the scoreboard -- remove
11376 			 * it from total
11377 			 */
11378 			rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
11379 			newly_acked = 0;
11380 		} else if (rsm->r_flags & RACK_SACK_PASSED) {
11381 			/*
11382 			 * There are segments ACKED on the
11383 			 * scoreboard further up. We are seeing
11384 			 * reordering.
11385 			 */
11386 			rsm->r_flags &= ~RACK_SACK_PASSED;
11387 			rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
11388 			rsm->r_flags |= RACK_ACKED;
11389 			rack->r_ctl.rc_reorder_ts = cts;
11390 			if (rack->r_ctl.rc_reorder_ts == 0)
11391 				rack->r_ctl.rc_reorder_ts = 1;
11392 			if (rack->r_ent_rec_ns) {
11393 				/*
11394 				 * We have sent no more, and we saw an sack
11395 				 * then ack arrive.
11396 				 */
11397 				rack->r_might_revert = 1;
11398 			}
11399 			rack_update_pcm_ack(rack, 1, rsm->r_start, rsm->r_end);
11400 		} else {
11401 			rack_update_pcm_ack(rack, 1, rsm->r_start, rsm->r_end);
11402 		}
11403 		if ((rsm->r_flags & RACK_TO_REXT) &&
11404 		    (tp->t_flags & TF_RCVD_TSTMP) &&
11405 		    (to->to_flags & TOF_TS) &&
11406 		    (to->to_tsecr != 0) &&
11407 		    (tp->t_flags & TF_PREVVALID)) {
11408 			/*
11409 			 * We can use the timestamp to see
11410 			 * if this retransmission was from the
11411 			 * first transmit. If so we made a mistake.
11412 			 */
11413 			tp->t_flags &= ~TF_PREVVALID;
11414 			if (to->to_tsecr == rack_ts_to_msec(rsm->r_tim_lastsent[0])) {
11415 				/* The first transmit is what this ack is for */
11416 				rack_cong_signal(tp, CC_RTO_ERR, th_ack, __LINE__);
11417 			}
11418 		}
11419 		left = th_ack - rsm->r_end;
11420 		if (rack->app_limited_needs_set && newly_acked)
11421 			rack_need_set_test(tp, rack, rsm, th_ack, __LINE__, RACK_USE_END_OR_THACK);
11422 		/* Free back to zone */
11423 		rack_free(rack, rsm);
11424 		if (left) {
11425 			goto more;
11426 		}
11427 		/* Check for reneging */
11428 		rsm = tqhash_min(rack->r_ctl.tqh);
11429 		if (rsm && (rsm->r_flags & RACK_ACKED) && (th_ack == rsm->r_start)) {
11430 			/*
11431 			 * The peer has moved snd_una up to
11432 			 * the edge of this send, i.e. one
11433 			 * that it had previously acked. The only
11434 			 * way that can be true if the peer threw
11435 			 * away data (space issues) that it had
11436 			 * previously sacked (else it would have
11437 			 * given us snd_una up to (rsm->r_end).
11438 			 * We need to undo the acked markings here.
11439 			 *
11440 			 * Note we have to look to make sure th_ack is
11441 			 * our rsm->r_start in case we get an old ack
11442 			 * where th_ack is behind snd_una.
11443 			 */
11444 			rack_peer_reneges(rack, rsm, th_ack);
11445 		}
11446 		return;
11447 	}
11448 	if (rsm->r_flags & RACK_ACKED) {
11449 		/*
11450 		 * It was acked on the scoreboard -- remove it from
11451 		 * total for the part being cum-acked.
11452 		 */
11453 		rack->r_ctl.rc_sacked -= (th_ack - rsm->r_start);
11454 	} else {
11455 		if (((rsm->r_flags & RACK_ACKED) == 0) &&
11456 		    (IN_RECOVERY(tp->t_flags))) {
11457 			rack->r_ctl.bytes_acked_in_recovery += (th_ack - rsm->r_start);
11458 		}
11459 		rack_update_pcm_ack(rack, 1, rsm->r_start, th_ack);
11460 	}
11461 	/* And what about the lost flag? */
11462 	if (rsm->r_flags & RACK_WAS_LOST) {
11463 		/*
11464 		 * This can happen when we marked it as lost
11465 		 * and yet before retransmitting we get an ack
11466 		 * which can happen due to reordering. In this
11467 		 * case its only a partial ack of the send.
11468 		 */
11469 		KASSERT((rack->r_ctl.rc_considered_lost >= (th_ack - rsm->r_start)),
11470 			("rsm:%p rack:%p rc_considered_lost goes negative th_ack:%u", rsm,  rack, th_ack));
11471 		if (rack->r_ctl.rc_considered_lost >= (th_ack - rsm->r_start))
11472 			rack->r_ctl.rc_considered_lost -= th_ack - rsm->r_start;
11473 		else
11474 			rack->r_ctl.rc_considered_lost = 0;
11475 	}
11476 	/*
11477 	 * Clear the dup ack count for
11478 	 * the piece that remains.
11479 	 */
11480 	rsm->r_dupack = 0;
11481 	rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
11482 	if (rsm->r_rtr_bytes) {
11483 		/*
11484 		 * It was retransmitted adjust the
11485 		 * sack holes for what was acked.
11486 		 */
11487 		int ack_am;
11488 
11489 		ack_am = (th_ack - rsm->r_start);
11490 		if (ack_am >= rsm->r_rtr_bytes) {
11491 			rack->r_ctl.rc_holes_rxt -= ack_am;
11492 			rsm->r_rtr_bytes -= ack_am;
11493 		}
11494 	}
11495 	/*
11496 	 * Update where the piece starts and record
11497 	 * the time of send of highest cumack sent if
11498 	 * its in our GP range.
11499 	 */
11500 	rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_TRIM_HEAD, th_ack, __LINE__);
11501 	/* Now we need to move our offset forward too */
11502 	if (rsm->m &&
11503 	    ((rsm->orig_m_len != rsm->m->m_len) ||
11504 	     (M_TRAILINGROOM(rsm->m) != rsm->orig_t_space))) {
11505 		/* Fix up the orig_m_len and possibly the mbuf offset */
11506 		rack_adjust_orig_mlen(rsm);
11507 	}
11508 	rsm->soff += (th_ack - rsm->r_start);
11509 	rack_rsm_sender_update(rack, tp, rsm, 5);
11510 	/* The trim will move th_ack into r_start for us */
11511 	tqhash_trim(rack->r_ctl.tqh, th_ack);
11512 	/* Now do we need to move the mbuf fwd too? */
11513 	{
11514 		struct mbuf *m;
11515 		uint32_t soff;
11516 
11517 		m = rsm->m;
11518 		soff = rsm->soff;
11519 		if (m) {
11520 			while (soff >= m->m_len) {
11521 				soff -= m->m_len;
11522 				KASSERT((m->m_next != NULL),
11523 					(" rsm:%p  off:%u soff:%u m:%p",
11524 					 rsm, rsm->soff, soff, m));
11525 				m = m->m_next;
11526 				if (m == NULL) {
11527 					/*
11528 					 * This is a fall-back that prevents a panic. In reality
11529 					 * we should be able to walk the mbuf's and find our place.
11530 					 * At this point snd_una has not been updated with the sbcut() yet
11531 					 * but tqhash_trim did update rsm->r_start so the offset calcuation
11532 					 * should work fine. This is undesirable since we will take cache
11533 					 * hits to access the socket buffer. And even more puzzling is that
11534 					 * it happens occasionally. It should not :(
11535 					 */
11536 					m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd,
11537 						      (rsm->r_start - tp->snd_una),
11538 						      &soff);
11539 					break;
11540 				}
11541 			}
11542 			/*
11543 			 * Now save in our updated values.
11544 			 */
11545 			rsm->m = m;
11546 			rsm->soff = soff;
11547 			rsm->orig_m_len = rsm->m->m_len;
11548 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
11549 		}
11550 	}
11551 	if (rack->app_limited_needs_set &&
11552 	    SEQ_GEQ(th_ack, tp->gput_seq))
11553 		rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_BEG);
11554 }
11555 
11556 static void
11557 rack_handle_might_revert(struct tcpcb *tp, struct tcp_rack *rack)
11558 {
11559 	struct rack_sendmap *rsm;
11560 	int sack_pass_fnd = 0;
11561 
11562 	if (rack->r_might_revert) {
11563 		/*
11564 		 * Ok we have reordering, have not sent anything, we
11565 		 * might want to revert the congestion state if nothing
11566 		 * further has SACK_PASSED on it. Lets check.
11567 		 *
11568 		 * We also get here when we have DSACKs come in for
11569 		 * all the data that we FR'd. Note that a rxt or tlp
11570 		 * timer clears this from happening.
11571 		 */
11572 
11573 		TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
11574 			if (rsm->r_flags & RACK_SACK_PASSED) {
11575 				sack_pass_fnd = 1;
11576 				break;
11577 			}
11578 		}
11579 		if (sack_pass_fnd == 0) {
11580 			/*
11581 			 * We went into recovery
11582 			 * incorrectly due to reordering!
11583 			 */
11584 			int orig_cwnd;
11585 
11586 			rack->r_ent_rec_ns = 0;
11587 			orig_cwnd = tp->snd_cwnd;
11588 			tp->snd_ssthresh = rack->r_ctl.rc_ssthresh_at_erec;
11589 			tp->snd_recover = tp->snd_una;
11590 			rack_log_to_prr(rack, 14, orig_cwnd, __LINE__);
11591 			if (IN_RECOVERY(tp->t_flags)) {
11592 				rack_exit_recovery(tp, rack, 3);
11593 				if ((rack->rto_from_rec == 1) && (rack_ssthresh_rest_rto_rec != 0) ){
11594 					/*
11595 					 * We were in recovery, had an RTO
11596 					 * and then re-entered recovery (more sack's arrived)
11597 					 * and we have properly recorded the old ssthresh from
11598 					 * the first recovery. We want to be able to slow-start
11599 					 * back to this level. The ssthresh from the timeout
11600 					 * and then back into recovery will end up most likely
11601 					 * to be min(cwnd=1mss, 2mss). Which makes it basically
11602 					 * so we get no slow-start after our RTO.
11603 					 */
11604 					rack->rto_from_rec = 0;
11605 					if (rack->r_ctl.rto_ssthresh > tp->snd_ssthresh)
11606 						tp->snd_ssthresh = rack->r_ctl.rto_ssthresh;
11607 				}
11608 			}
11609 			rack->r_ctl.bytes_acked_in_recovery = 0;
11610 			rack->r_ctl.time_entered_recovery = 0;
11611 		}
11612 		rack->r_might_revert = 0;
11613 	}
11614 }
11615 
11616 #ifdef TCP_SAD_DETECTION
11617 
11618 static void
11619 rack_merge_out_sacks(struct tcp_rack *rack)
11620 {
11621 	struct rack_sendmap *cur, *next, *rsm, *trsm = NULL;
11622 
11623 	cur = tqhash_min(rack->r_ctl.tqh);
11624 	while(cur) {
11625 		next = tqhash_next(rack->r_ctl.tqh, cur);
11626 		/*
11627 		 * The idea is to go through all and merge back
11628 		 * together the pieces sent together,
11629 		 */
11630 		if ((next != NULL) &&
11631 		    (cur->r_tim_lastsent[0] == next->r_tim_lastsent[0])) {
11632 			rack_merge_rsm(rack, cur, next);
11633 		} else {
11634 			cur = next;
11635 		}
11636 	}
11637 	/*
11638 	 * now treat it like a rxt event, everything is outstanding
11639 	 * and sent nothing acvked and dupacks are all zero. If this
11640 	 * is not an attacker it will have to dupack its way through
11641 	 * it all.
11642 	 */
11643 	TAILQ_INIT(&rack->r_ctl.rc_tmap);
11644 	TQHASH_FOREACH(rsm, rack->r_ctl.tqh)  {
11645 		rsm->r_dupack = 0;
11646 		/* We must re-add it back to the tlist */
11647 		if (trsm == NULL) {
11648 			TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
11649 		} else {
11650 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, trsm, rsm, r_tnext);
11651 		}
11652 		rsm->r_in_tmap = 1;
11653 		trsm = rsm;
11654 		rsm->r_flags &= ~(RACK_ACKED | RACK_SACK_PASSED | RACK_WAS_SACKPASS | RACK_RWND_COLLAPSED);
11655 	}
11656 	sack_filter_clear(&rack->r_ctl.rack_sf, rack->rc_tp->snd_una);
11657 }
11658 
11659 static void
11660 rack_do_detection(struct tcpcb *tp, struct tcp_rack *rack,  uint32_t bytes_this_ack, uint32_t segsiz)
11661 {
11662 	int do_detection = 0;
11663 
11664 	if (rack->sack_attack_disable || rack->rc_suspicious) {
11665 		/*
11666 		 * If we have been disabled we must detect
11667 		 * to possibly reverse it. Or if the guy has
11668 		 * sent in suspicious sacks we want to do detection too.
11669 		 */
11670 		do_detection = 1;
11671 
11672 	} else if  ((rack->do_detection || tcp_force_detection) &&
11673 		    (tcp_sack_to_ack_thresh > 0) &&
11674 		    (tcp_sack_to_move_thresh > 0) &&
11675 		    (rack->r_ctl.rc_num_maps_alloced > tcp_map_minimum)) {
11676 		/*
11677 		 * We only detect here if:
11678 		 * 1) System wide forcing is on <or> do_detection is on
11679 		 *   <and>
11680 		 * 2) We have thresholds for move and ack (set one to 0 and we are off)
11681 		 *   <and>
11682 		 * 3) We have maps allocated larger than our min (500).
11683 		 */
11684 		do_detection = 1;
11685 	}
11686 	if (do_detection > 0) {
11687 		/*
11688 		 * We have thresholds set to find
11689 		 * possible attackers and disable sack.
11690 		 * Check them.
11691 		 */
11692 		uint64_t ackratio, moveratio, movetotal;
11693 
11694 		/* Log detecting */
11695 		rack_log_sad(rack, 1);
11696 		/* Do we establish a ack ratio */
11697 		if ((rack->r_ctl.sack_count > tcp_map_minimum)  ||
11698 		    (rack->rc_suspicious == 1) ||
11699 		    (rack->sack_attack_disable > 0)) {
11700 			ackratio = (uint64_t)(rack->r_ctl.sack_count);
11701 			ackratio *= (uint64_t)(1000);
11702 			if (rack->r_ctl.ack_count)
11703 				ackratio /= (uint64_t)(rack->r_ctl.ack_count);
11704 			else {
11705 				/* We can hit this due to ack totals degregation (via small sacks) */
11706 				ackratio = 1000;
11707 			}
11708 		} else {
11709 			/*
11710 			 * No ack ratio needed if we have not
11711 			 * seen more sacks then the number of map entries.
11712 			 * The exception to that is if we have disabled sack then
11713 			 * we need to find a ratio.
11714 			 */
11715 			ackratio = 0;
11716 		}
11717 
11718 		if ((rack->sack_attack_disable == 0) &&
11719 		    (ackratio > rack_highest_sack_thresh_seen))
11720 			rack_highest_sack_thresh_seen = (uint32_t)ackratio;
11721 		/* Do we establish a move ratio? */
11722 		if ((rack->r_ctl.sack_moved_extra > tcp_map_minimum) ||
11723 		    (rack->rc_suspicious == 1) ||
11724 		    (rack->sack_attack_disable > 0)) {
11725 			/*
11726 			 * We need to have more sack moves than maps
11727 			 * allocated to have a move ratio considered.
11728 			 */
11729 			movetotal = rack->r_ctl.sack_moved_extra;
11730 			movetotal += rack->r_ctl.sack_noextra_move;
11731 			moveratio = rack->r_ctl.sack_moved_extra;
11732 			moveratio *= (uint64_t)1000;
11733 			if (movetotal)
11734 				moveratio /= movetotal;
11735 			else {
11736 				/* No moves, thats pretty good */
11737 				moveratio = 0;
11738 			}
11739 		} else {
11740 			/*
11741 			 * Not enough moves have occured to consider
11742 			 * if we are out of whack in that ratio.
11743 			 * The exception to that is if we have disabled sack then
11744 			 * we need to find a ratio.
11745 			 */
11746 			moveratio = 0;
11747 		}
11748 		if ((rack->sack_attack_disable == 0) &&
11749 		    (moveratio > rack_highest_move_thresh_seen))
11750 			rack_highest_move_thresh_seen = (uint32_t)moveratio;
11751 		/* Now the tests */
11752 		if (rack->sack_attack_disable == 0) {
11753 			/* Not disabled, do we need to disable? */
11754 			if ((ackratio > tcp_sack_to_ack_thresh) &&
11755 			    (moveratio > tcp_sack_to_move_thresh)) {
11756 				/* Disable sack processing */
11757 				tcp_trace_point(rack->rc_tp, TCP_TP_SAD_TRIGGERED);
11758 				rack->sack_attack_disable = 1;
11759 				/* set it so we have the built in delay */
11760 				rack->r_ctl.ack_during_sd = 1;
11761 				if (rack_merge_out_sacks_on_attack)
11762 					rack_merge_out_sacks(rack);
11763 				counter_u64_add(rack_sack_attacks_detected, 1);
11764 				tcp_trace_point(rack->rc_tp, TCP_TP_SAD_TRIGGERED);
11765 				/* Clamp the cwnd at flight size */
11766 				rack->r_ctl.rc_saved_cwnd = rack->rc_tp->snd_cwnd;
11767 				rack->rc_tp->snd_cwnd = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
11768 				rack_log_sad(rack, 2);
11769 			}
11770 		} else {
11771 			/* We are sack-disabled check for false positives */
11772 			if ((ackratio <= tcp_restoral_thresh) ||
11773 			    ((rack_merge_out_sacks_on_attack == 0) &&
11774 			     (rack->rc_suspicious == 0) &&
11775 			     (rack->r_ctl.rc_num_maps_alloced <= (tcp_map_minimum/2)))) {
11776 				rack->sack_attack_disable = 0;
11777 				rack_log_sad(rack, 3);
11778 				/* Restart counting */
11779 				rack->r_ctl.sack_count = 0;
11780 				rack->r_ctl.sack_moved_extra = 0;
11781 				rack->r_ctl.sack_noextra_move = 1;
11782 				rack->rc_suspicious = 0;
11783 				rack->r_ctl.ack_count = max(1,
11784 							    (bytes_this_ack / segsiz));
11785 
11786 				counter_u64_add(rack_sack_attacks_reversed, 1);
11787 				/* Restore the cwnd */
11788 				if (rack->r_ctl.rc_saved_cwnd > rack->rc_tp->snd_cwnd)
11789 					rack->rc_tp->snd_cwnd = rack->r_ctl.rc_saved_cwnd;
11790 			}
11791 		}
11792 	}
11793 }
11794 #endif
11795 
11796 static int
11797 rack_note_dsack(struct tcp_rack *rack, tcp_seq start, tcp_seq end)
11798 {
11799 
11800 	uint32_t am, l_end;
11801 	int was_tlp = 0;
11802 
11803 	if (SEQ_GT(end, start))
11804 		am = end - start;
11805 	else
11806 		am = 0;
11807 	if ((rack->rc_last_tlp_acked_set ) &&
11808 	    (SEQ_GEQ(start, rack->r_ctl.last_tlp_acked_start)) &&
11809 	    (SEQ_LEQ(end, rack->r_ctl.last_tlp_acked_end))) {
11810 		/*
11811 		 * The DSACK is because of a TLP which we don't
11812 		 * do anything with the reordering window over since
11813 		 * it was not reordering that caused the DSACK but
11814 		 * our previous retransmit TLP.
11815 		 */
11816 		rack_log_dsack_event(rack, 7, __LINE__, start, end);
11817 		was_tlp = 1;
11818 		goto skip_dsack_round;
11819 	}
11820 	if (rack->rc_last_sent_tlp_seq_valid) {
11821 		l_end = rack->r_ctl.last_sent_tlp_seq + rack->r_ctl.last_sent_tlp_len;
11822 		if (SEQ_GEQ(start, rack->r_ctl.last_sent_tlp_seq) &&
11823 		    (SEQ_LEQ(end, l_end))) {
11824 			/*
11825 			 * This dsack is from the last sent TLP, ignore it
11826 			 * for reordering purposes.
11827 			 */
11828 			rack_log_dsack_event(rack, 7, __LINE__, start, end);
11829 			was_tlp = 1;
11830 			goto skip_dsack_round;
11831 		}
11832 	}
11833 	if (rack->rc_dsack_round_seen == 0) {
11834 		rack->rc_dsack_round_seen = 1;
11835 		rack->r_ctl.dsack_round_end = rack->rc_tp->snd_max;
11836 		rack->r_ctl.num_dsack++;
11837 		rack->r_ctl.dsack_persist = 16;	/* 16 is from the standard */
11838 		rack_log_dsack_event(rack, 2, __LINE__, 0, 0);
11839 	}
11840 skip_dsack_round:
11841 	/*
11842 	 * We keep track of how many DSACK blocks we get
11843 	 * after a recovery incident.
11844 	 */
11845 	rack->r_ctl.dsack_byte_cnt += am;
11846 	if (!IN_FASTRECOVERY(rack->rc_tp->t_flags) &&
11847 	    rack->r_ctl.retran_during_recovery &&
11848 	    (rack->r_ctl.dsack_byte_cnt >= rack->r_ctl.retran_during_recovery)) {
11849 		/*
11850 		 * False recovery most likely culprit is reordering. If
11851 		 * nothing else is missing we need to revert.
11852 		 */
11853 		rack->r_might_revert = 1;
11854 		rack_handle_might_revert(rack->rc_tp, rack);
11855 		rack->r_might_revert = 0;
11856 		rack->r_ctl.retran_during_recovery = 0;
11857 		rack->r_ctl.dsack_byte_cnt = 0;
11858 	}
11859 	return (was_tlp);
11860 }
11861 
11862 static uint32_t
11863 do_rack_compute_pipe(struct tcpcb *tp, struct tcp_rack *rack, uint32_t snd_una)
11864 {
11865 	return (((tp->snd_max - snd_una) -
11866 		 (rack->r_ctl.rc_sacked + rack->r_ctl.rc_considered_lost)) + rack->r_ctl.rc_holes_rxt);
11867 }
11868 
11869 static int32_t
11870 rack_compute_pipe(struct tcpcb *tp)
11871 {
11872 	return ((int32_t)do_rack_compute_pipe(tp,
11873 					      (struct tcp_rack *)tp->t_fb_ptr,
11874 					      tp->snd_una));
11875 }
11876 
11877 static void
11878 rack_update_prr(struct tcpcb *tp, struct tcp_rack *rack, uint32_t changed, tcp_seq th_ack)
11879 {
11880 	/* Deal with changed and PRR here (in recovery only) */
11881 	uint32_t pipe, snd_una;
11882 
11883 	rack->r_ctl.rc_prr_delivered += changed;
11884 
11885 	if (sbavail(&rack->rc_inp->inp_socket->so_snd) <= (tp->snd_max - tp->snd_una)) {
11886 		/*
11887 		 * It is all outstanding, we are application limited
11888 		 * and thus we don't need more room to send anything.
11889 		 * Note we use tp->snd_una here and not th_ack because
11890 		 * the data as yet not been cut from the sb.
11891 		 */
11892 		rack->r_ctl.rc_prr_sndcnt = 0;
11893 		return;
11894 	}
11895 	/* Compute prr_sndcnt */
11896 	if (SEQ_GT(tp->snd_una, th_ack)) {
11897 		snd_una = tp->snd_una;
11898 	} else {
11899 		snd_una = th_ack;
11900 	}
11901 	pipe = do_rack_compute_pipe(tp, rack, snd_una);
11902 	if (pipe > tp->snd_ssthresh) {
11903 		long sndcnt;
11904 
11905 		sndcnt = rack->r_ctl.rc_prr_delivered * tp->snd_ssthresh;
11906 		if (rack->r_ctl.rc_prr_recovery_fs > 0)
11907 			sndcnt /= (long)rack->r_ctl.rc_prr_recovery_fs;
11908 		else {
11909 			rack->r_ctl.rc_prr_sndcnt = 0;
11910 			rack_log_to_prr(rack, 9, 0, __LINE__);
11911 			sndcnt = 0;
11912 		}
11913 		sndcnt++;
11914 		if (sndcnt > (long)rack->r_ctl.rc_prr_out)
11915 			sndcnt -= rack->r_ctl.rc_prr_out;
11916 		else
11917 			sndcnt = 0;
11918 		rack->r_ctl.rc_prr_sndcnt = sndcnt;
11919 		rack_log_to_prr(rack, 10, 0, __LINE__);
11920 	} else {
11921 		uint32_t limit;
11922 
11923 		if (rack->r_ctl.rc_prr_delivered > rack->r_ctl.rc_prr_out)
11924 			limit = (rack->r_ctl.rc_prr_delivered - rack->r_ctl.rc_prr_out);
11925 		else
11926 			limit = 0;
11927 		if (changed > limit)
11928 			limit = changed;
11929 		limit += ctf_fixed_maxseg(tp);
11930 		if (tp->snd_ssthresh > pipe) {
11931 			rack->r_ctl.rc_prr_sndcnt = min((tp->snd_ssthresh - pipe), limit);
11932 			rack_log_to_prr(rack, 11, 0, __LINE__);
11933 		} else {
11934 			rack->r_ctl.rc_prr_sndcnt = min(0, limit);
11935 			rack_log_to_prr(rack, 12, 0, __LINE__);
11936 		}
11937 	}
11938 }
11939 
11940 static void
11941 rack_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th, int entered_recovery, int dup_ack_struck,
11942 	     int *dsack_seen, int *sacks_seen)
11943 {
11944 	uint32_t changed;
11945 	struct tcp_rack *rack;
11946 	struct rack_sendmap *rsm;
11947 	struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1];
11948 	register uint32_t th_ack;
11949 	int32_t i, j, k, num_sack_blks = 0;
11950 	uint32_t cts, acked, ack_point;
11951 	int loop_start = 0, moved_two = 0, no_extra = 0;
11952 	uint32_t tsused;
11953 	uint32_t segsiz, o_cnt;
11954 
11955 
11956 	INP_WLOCK_ASSERT(tptoinpcb(tp));
11957 	if (tcp_get_flags(th) & TH_RST) {
11958 		/* We don't log resets */
11959 		return;
11960 	}
11961 	rack = (struct tcp_rack *)tp->t_fb_ptr;
11962 	cts = tcp_get_usecs(NULL);
11963 	rsm = tqhash_min(rack->r_ctl.tqh);
11964 	changed = 0;
11965 	th_ack = th->th_ack;
11966 	if (rack->sack_attack_disable == 0)
11967 		rack_do_decay(rack);
11968 	segsiz = ctf_fixed_maxseg(rack->rc_tp);
11969 	if (BYTES_THIS_ACK(tp, th) >=  segsiz) {
11970 		/*
11971 		 * You only get credit for
11972 		 * MSS and greater (and you get extra
11973 		 * credit for larger cum-ack moves).
11974 		 */
11975 		int ac;
11976 
11977 		ac = BYTES_THIS_ACK(tp, th) / ctf_fixed_maxseg(rack->rc_tp);
11978 		rack->r_ctl.ack_count += ac;
11979 		counter_u64_add(rack_ack_total, ac);
11980 	}
11981 	if (rack->r_ctl.ack_count > 0xfff00000) {
11982 		/*
11983 		 * reduce the number to keep us under
11984 		 * a uint32_t.
11985 		 */
11986 		rack->r_ctl.ack_count /= 2;
11987 		rack->r_ctl.sack_count /= 2;
11988 	}
11989 	if (SEQ_GT(th_ack, tp->snd_una)) {
11990 		rack_log_progress_event(rack, tp, ticks, PROGRESS_UPDATE, __LINE__);
11991 		tp->t_acktime = ticks;
11992 	}
11993 	if (rsm && SEQ_GT(th_ack, rsm->r_start))
11994 		changed = th_ack - rsm->r_start;
11995 	if (changed) {
11996 		rack_process_to_cumack(tp, rack, th_ack, cts, to,
11997 				       tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time));
11998 	}
11999 	if ((to->to_flags & TOF_SACK) == 0) {
12000 		/* We are done nothing left and no sack. */
12001 		rack_handle_might_revert(tp, rack);
12002 		/*
12003 		 * For cases where we struck a dup-ack
12004 		 * with no SACK, add to the changes so
12005 		 * PRR will work right.
12006 		 */
12007 		if (dup_ack_struck && (changed == 0)) {
12008 			changed += ctf_fixed_maxseg(rack->rc_tp);
12009 		}
12010 		goto out;
12011 	}
12012 	/* Sack block processing */
12013 	if (SEQ_GT(th_ack, tp->snd_una))
12014 		ack_point = th_ack;
12015 	else
12016 		ack_point = tp->snd_una;
12017 	for (i = 0; i < to->to_nsacks; i++) {
12018 		bcopy((to->to_sacks + i * TCPOLEN_SACK),
12019 		      &sack, sizeof(sack));
12020 		sack.start = ntohl(sack.start);
12021 		sack.end = ntohl(sack.end);
12022 		if (SEQ_GT(sack.end, sack.start) &&
12023 		    SEQ_GT(sack.start, ack_point) &&
12024 		    SEQ_LT(sack.start, tp->snd_max) &&
12025 		    SEQ_GT(sack.end, ack_point) &&
12026 		    SEQ_LEQ(sack.end, tp->snd_max)) {
12027 			sack_blocks[num_sack_blks] = sack;
12028 			num_sack_blks++;
12029 		} else if (SEQ_LEQ(sack.start, th_ack) &&
12030 			   SEQ_LEQ(sack.end, th_ack)) {
12031 			int was_tlp;
12032 
12033 			if (dsack_seen != NULL)
12034 				*dsack_seen = 1;
12035 			was_tlp = rack_note_dsack(rack, sack.start, sack.end);
12036 			/*
12037 			 * Its a D-SACK block.
12038 			 */
12039 			tcp_record_dsack(tp, sack.start, sack.end, was_tlp);
12040 		}
12041 	}
12042 	if (rack->rc_dsack_round_seen) {
12043 		/* Is the dsack roound over? */
12044 		if (SEQ_GEQ(th_ack, rack->r_ctl.dsack_round_end)) {
12045 			/* Yes it is */
12046 			rack->rc_dsack_round_seen = 0;
12047 			rack_log_dsack_event(rack, 3, __LINE__, 0, 0);
12048 		}
12049 	}
12050 	/*
12051 	 * Sort the SACK blocks so we can update the rack scoreboard with
12052 	 * just one pass.
12053 	 */
12054 	o_cnt = num_sack_blks;
12055 	num_sack_blks = sack_filter_blks(&rack->r_ctl.rack_sf, sack_blocks,
12056 					 num_sack_blks, th->th_ack);
12057 	ctf_log_sack_filter(rack->rc_tp, num_sack_blks, sack_blocks);
12058 	if (sacks_seen != NULL)
12059 		*sacks_seen = num_sack_blks;
12060 	if (num_sack_blks == 0) {
12061 		/* Nothing to sack, but we need to update counts */
12062 		if ((o_cnt == 1) &&
12063 		    (*dsack_seen != 1))
12064 			rack->r_ctl.sack_count++;
12065 		else if (o_cnt > 1)
12066 			rack->r_ctl.sack_count++;
12067 		goto out_with_totals;
12068 	}
12069 	if (rack->sack_attack_disable) {
12070 		/*
12071 		 * An attacker disablement is in place, for
12072 		 * every sack block that is not at least a full MSS
12073 		 * count up sack_count.
12074 		 */
12075 		for (i = 0; i < num_sack_blks; i++) {
12076 			if ((sack_blocks[i].end - sack_blocks[i].start) < segsiz) {
12077 				rack->r_ctl.sack_count++;
12078 			}
12079 			if (rack->r_ctl.sack_count > 0xfff00000) {
12080 				/*
12081 				 * reduce the number to keep us under
12082 				 * a uint32_t.
12083 				 */
12084 				rack->r_ctl.ack_count /= 2;
12085 				rack->r_ctl.sack_count /= 2;
12086 			}
12087 		}
12088 		goto out;
12089 	}
12090 	/* Its a sack of some sort */
12091 	rack->r_ctl.sack_count += num_sack_blks;
12092 	if (rack->r_ctl.sack_count > 0xfff00000) {
12093 		/*
12094 		 * reduce the number to keep us under
12095 		 * a uint32_t.
12096 		 */
12097 		rack->r_ctl.ack_count /= 2;
12098 		rack->r_ctl.sack_count /= 2;
12099 	}
12100 	if (num_sack_blks < 2) {
12101 		/* Only one, we don't need to sort */
12102 		goto do_sack_work;
12103 	}
12104 	/* Sort the sacks */
12105 	for (i = 0; i < num_sack_blks; i++) {
12106 		for (j = i + 1; j < num_sack_blks; j++) {
12107 			if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
12108 				sack = sack_blocks[i];
12109 				sack_blocks[i] = sack_blocks[j];
12110 				sack_blocks[j] = sack;
12111 			}
12112 		}
12113 	}
12114 	/*
12115 	 * Now are any of the sack block ends the same (yes some
12116 	 * implementations send these)?
12117 	 */
12118 again:
12119 	if (num_sack_blks == 0)
12120 		goto out_with_totals;
12121 	if (num_sack_blks > 1) {
12122 		for (i = 0; i < num_sack_blks; i++) {
12123 			for (j = i + 1; j < num_sack_blks; j++) {
12124 				if (sack_blocks[i].end == sack_blocks[j].end) {
12125 					/*
12126 					 * Ok these two have the same end we
12127 					 * want the smallest end and then
12128 					 * throw away the larger and start
12129 					 * again.
12130 					 */
12131 					if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) {
12132 						/*
12133 						 * The second block covers
12134 						 * more area use that
12135 						 */
12136 						sack_blocks[i].start = sack_blocks[j].start;
12137 					}
12138 					/*
12139 					 * Now collapse out the dup-sack and
12140 					 * lower the count
12141 					 */
12142 					for (k = (j + 1); k < num_sack_blks; k++) {
12143 						sack_blocks[j].start = sack_blocks[k].start;
12144 						sack_blocks[j].end = sack_blocks[k].end;
12145 						j++;
12146 					}
12147 					num_sack_blks--;
12148 					goto again;
12149 				}
12150 			}
12151 		}
12152 	}
12153 do_sack_work:
12154 	/*
12155 	 * First lets look to see if
12156 	 * we have retransmitted and
12157 	 * can use the transmit next?
12158 	 */
12159 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
12160 	if (rsm &&
12161 	    SEQ_GT(sack_blocks[0].end, rsm->r_start) &&
12162 	    SEQ_LT(sack_blocks[0].start, rsm->r_end)) {
12163 		/*
12164 		 * We probably did the FR and the next
12165 		 * SACK in continues as we would expect.
12166 		 */
12167 		acked = rack_proc_sack_blk(tp, rack, &sack_blocks[0], to, &rsm, cts, &no_extra, &moved_two, segsiz);
12168 		if (acked) {
12169 			rack->r_wanted_output = 1;
12170 			changed += acked;
12171 		}
12172 		if (num_sack_blks == 1) {
12173 			/*
12174 			 * This is what we would expect from
12175 			 * a normal implementation to happen
12176 			 * after we have retransmitted the FR,
12177 			 * i.e the sack-filter pushes down
12178 			 * to 1 block and the next to be retransmitted
12179 			 * is the sequence in the sack block (has more
12180 			 * are acked). Count this as ACK'd data to boost
12181 			 * up the chances of recovering any false positives.
12182 			 */
12183 			rack->r_ctl.ack_count += (acked / ctf_fixed_maxseg(rack->rc_tp));
12184 			counter_u64_add(rack_ack_total, (acked / ctf_fixed_maxseg(rack->rc_tp)));
12185 			counter_u64_add(rack_express_sack, 1);
12186 			if (rack->r_ctl.ack_count > 0xfff00000) {
12187 				/*
12188 				 * reduce the number to keep us under
12189 				 * a uint32_t.
12190 				 */
12191 				rack->r_ctl.ack_count /= 2;
12192 				rack->r_ctl.sack_count /= 2;
12193 			}
12194 			if (moved_two) {
12195 				/*
12196 				 * If we did not get a SACK for at least a MSS and
12197 				 * had to move at all, or if we moved more than our
12198 				 * threshold, it counts against the "extra" move.
12199 				 */
12200 				rack->r_ctl.sack_moved_extra += moved_two;
12201 				rack->r_ctl.sack_noextra_move += no_extra;
12202 				counter_u64_add(rack_move_some, 1);
12203 			} else {
12204 				/*
12205 				 * else we did not have to move
12206 				 * any more than we would expect.
12207 				 */
12208 				rack->r_ctl.sack_noextra_move += no_extra;
12209 				rack->r_ctl.sack_noextra_move++;
12210 				counter_u64_add(rack_move_none, 1);
12211 			}
12212 			if ((rack->r_ctl.sack_moved_extra > 0xfff00000) ||
12213 			    (rack->r_ctl.sack_noextra_move > 0xfff00000)) {
12214 				rack->r_ctl.sack_moved_extra /= 2;
12215 				rack->r_ctl.sack_noextra_move /= 2;
12216 			}
12217 			goto out_with_totals;
12218 		} else {
12219 			/*
12220 			 * Start the loop through the
12221 			 * rest of blocks, past the first block.
12222 			 */
12223 			loop_start = 1;
12224 		}
12225 	}
12226 	counter_u64_add(rack_sack_total, 1);
12227 	rsm = rack->r_ctl.rc_sacklast;
12228 	for (i = loop_start; i < num_sack_blks; i++) {
12229 		acked = rack_proc_sack_blk(tp, rack, &sack_blocks[i], to, &rsm, cts, &no_extra, &moved_two, segsiz);
12230 		if (acked) {
12231 			rack->r_wanted_output = 1;
12232 			changed += acked;
12233 		}
12234 		if (moved_two) {
12235 			/*
12236 			 * If we did not get a SACK for at least a MSS and
12237 			 * had to move at all, or if we moved more than our
12238 			 * threshold, it counts against the "extra" move.
12239 			 */
12240 			rack->r_ctl.sack_moved_extra += moved_two;
12241 			rack->r_ctl.sack_noextra_move += no_extra;
12242 			counter_u64_add(rack_move_some, 1);
12243 		} else {
12244 			/*
12245 			 * else we did not have to move
12246 			 * any more than we would expect.
12247 			 */
12248 			rack->r_ctl.sack_noextra_move += no_extra;
12249 			rack->r_ctl.sack_noextra_move++;
12250 			counter_u64_add(rack_move_none, 1);
12251 		}
12252 		if ((rack->r_ctl.sack_moved_extra > 0xfff00000) ||
12253 		    (rack->r_ctl.sack_noextra_move > 0xfff00000)) {
12254 			rack->r_ctl.sack_moved_extra /= 2;
12255 			rack->r_ctl.sack_noextra_move /= 2;
12256 		}
12257 		if (moved_two && (acked < ctf_fixed_maxseg(rack->rc_tp))) {
12258 			/*
12259 			 * If the SACK was not a full MSS then
12260 			 * we add to sack_count the number of
12261 			 * MSS's (or possibly more than
12262 			 * a MSS if its a TSO send) we had to skip by.
12263 			 */
12264 			rack->r_ctl.sack_count += moved_two;
12265 			if (rack->r_ctl.sack_count > 0xfff00000) {
12266 				rack->r_ctl.ack_count /= 2;
12267 				rack->r_ctl.sack_count /= 2;
12268 			}
12269 			counter_u64_add(rack_sack_total, moved_two);
12270 		}
12271 		/*
12272 		 * Now we need to setup for the next
12273 		 * round. First we make sure we won't
12274 		 * exceed the size of our uint32_t on
12275 		 * the various counts, and then clear out
12276 		 * moved_two.
12277 		 */
12278 		moved_two = 0;
12279 		no_extra = 0;
12280 	}
12281 out_with_totals:
12282 	if (num_sack_blks > 1) {
12283 		/*
12284 		 * You get an extra stroke if
12285 		 * you have more than one sack-blk, this
12286 		 * could be where we are skipping forward
12287 		 * and the sack-filter is still working, or
12288 		 * it could be an attacker constantly
12289 		 * moving us.
12290 		 */
12291 		rack->r_ctl.sack_moved_extra++;
12292 		counter_u64_add(rack_move_some, 1);
12293 	}
12294 out:
12295 #ifdef TCP_SAD_DETECTION
12296 	rack_do_detection(tp, rack, BYTES_THIS_ACK(tp, th), ctf_fixed_maxseg(rack->rc_tp));
12297 #endif
12298 	if (changed) {
12299 		/* Something changed cancel the rack timer */
12300 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
12301 	}
12302 	tsused = tcp_get_usecs(NULL);
12303 	rsm = tcp_rack_output(tp, rack, tsused);
12304 	if ((!IN_FASTRECOVERY(tp->t_flags)) &&
12305 	    rsm &&
12306 	    ((rsm->r_flags & RACK_MUST_RXT) == 0)) {
12307 		/* Enter recovery */
12308 		entered_recovery = 1;
12309 		rack_cong_signal(tp, CC_NDUPACK, th_ack, __LINE__);
12310 		/*
12311 		 * When we enter recovery we need to assure we send
12312 		 * one packet.
12313 		 */
12314 		if (rack->rack_no_prr == 0) {
12315 			rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
12316 			rack_log_to_prr(rack, 8, 0, __LINE__);
12317 		}
12318 		rack->r_timer_override = 1;
12319 		rack->r_early = 0;
12320 		rack->r_ctl.rc_agg_early = 0;
12321 	} else if (IN_FASTRECOVERY(tp->t_flags) &&
12322 		   rsm &&
12323 		   (rack->r_rr_config == 3)) {
12324 		/*
12325 		 * Assure we can output and we get no
12326 		 * remembered pace time except the retransmit.
12327 		 */
12328 		rack->r_timer_override = 1;
12329 		rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
12330 		rack->r_ctl.rc_resend = rsm;
12331 	}
12332 	if (IN_FASTRECOVERY(tp->t_flags) &&
12333 	    (rack->rack_no_prr == 0) &&
12334 	    (entered_recovery == 0)) {
12335 		rack_update_prr(tp, rack, changed, th_ack);
12336 		if ((rsm && (rack->r_ctl.rc_prr_sndcnt >= ctf_fixed_maxseg(tp)) &&
12337 		     ((tcp_in_hpts(rack->rc_tp) == 0) &&
12338 		      ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)))) {
12339 			/*
12340 			 * If you are pacing output you don't want
12341 			 * to override.
12342 			 */
12343 			rack->r_early = 0;
12344 			rack->r_ctl.rc_agg_early = 0;
12345 			rack->r_timer_override = 1;
12346 		}
12347 	}
12348 }
12349 
12350 static void
12351 rack_strike_dupack(struct tcp_rack *rack, tcp_seq th_ack)
12352 {
12353 	struct rack_sendmap *rsm;
12354 
12355 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
12356 	while (rsm) {
12357 		/*
12358 		 * We need to skip anything already set
12359 		 * to be retransmitted.
12360 		 */
12361 		if ((rsm->r_dupack >= DUP_ACK_THRESHOLD)  ||
12362 		    (rsm->r_flags & RACK_MUST_RXT)) {
12363 			rsm = TAILQ_NEXT(rsm, r_tnext);
12364 			continue;
12365 		}
12366 		break;
12367 	}
12368 	if (rsm && (rsm->r_dupack < 0xff)) {
12369 		rsm->r_dupack++;
12370 		if (rsm->r_dupack >= DUP_ACK_THRESHOLD) {
12371 			struct timeval tv;
12372 			uint32_t cts;
12373 			/*
12374 			 * Here we see if we need to retransmit. For
12375 			 * a SACK type connection if enough time has passed
12376 			 * we will get a return of the rsm. For a non-sack
12377 			 * connection we will get the rsm returned if the
12378 			 * dupack value is 3 or more.
12379 			 */
12380 			cts = tcp_get_usecs(&tv);
12381 			rack->r_ctl.rc_resend = tcp_rack_output(rack->rc_tp, rack, cts);
12382 			if (rack->r_ctl.rc_resend != NULL) {
12383 				if (!IN_FASTRECOVERY(rack->rc_tp->t_flags)) {
12384 					rack_cong_signal(rack->rc_tp, CC_NDUPACK,
12385 							 th_ack,  __LINE__);
12386 				}
12387 				rack->r_wanted_output = 1;
12388 				rack->r_timer_override = 1;
12389 				rack_log_retran_reason(rack, rsm, __LINE__, 1, 3);
12390 			}
12391 		} else {
12392 			rack_log_retran_reason(rack, rsm, __LINE__, 0, 3);
12393 		}
12394 	}
12395 }
12396 
12397 static void
12398 rack_check_bottom_drag(struct tcpcb *tp,
12399 		       struct tcp_rack *rack,
12400 		       struct socket *so)
12401 {
12402 	/*
12403 	 * So what is dragging bottom?
12404 	 *
12405 	 * Dragging bottom means you were under pacing and had a
12406 	 * delay in processing inbound acks waiting on our pacing
12407 	 * timer to expire. While you were waiting all of the acknowledgments
12408 	 * for the packets you sent have arrived. This means we are pacing
12409 	 * way underneath the bottleneck to the point where our Goodput
12410 	 * measurements stop working, since they require more than one
12411 	 * ack (usually at least 8 packets worth with multiple acks so we can
12412 	 * gauge the inter-ack times). If that occurs we have a real problem
12413 	 * since we are stuck in a hole that we can't get out of without
12414 	 * something speeding us up.
12415 	 *
12416 	 * We also check to see if we are widdling down to just one segment
12417 	 * outstanding. If this occurs and we have room to send in our cwnd/rwnd
12418 	 * then we are adding the delayed ack interval into our measurments and
12419 	 * we need to speed up slightly.
12420 	 */
12421 	uint32_t segsiz, minseg;
12422 
12423 	segsiz = ctf_fixed_maxseg(tp);
12424 	minseg = segsiz;
12425 	if (tp->snd_max == tp->snd_una) {
12426 		/*
12427 		 * We are doing dynamic pacing and we are way
12428 		 * under. Basically everything got acked while
12429 		 * we were still waiting on the pacer to expire.
12430 		 *
12431 		 * This means we need to boost the b/w in
12432 		 * addition to any earlier boosting of
12433 		 * the multiplier.
12434 		 */
12435 		uint64_t lt_bw;
12436 
12437 		tcp_trace_point(rack->rc_tp, TCP_TP_PACED_BOTTOM);
12438 		lt_bw = rack_get_lt_bw(rack);
12439 		rack->rc_dragged_bottom = 1;
12440 		rack_validate_multipliers_at_or_above100(rack);
12441 		if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_VALID) &&
12442 		    (rack->dis_lt_bw == 0) &&
12443 		    (rack->use_lesser_lt_bw == 0) &&
12444 		    (lt_bw > 0)) {
12445 			/*
12446 			 * Lets use the long-term b/w we have
12447 			 * been getting as a base.
12448 			 */
12449 			if (rack->rc_gp_filled == 0) {
12450 				if (lt_bw > ONE_POINT_TWO_MEG) {
12451 					/*
12452 					 * If we have no measurement
12453 					 * don't let us set in more than
12454 					 * 1.2Mbps. If we are still too
12455 					 * low after pacing with this we
12456 					 * will hopefully have a max b/w
12457 					 * available to sanity check things.
12458 					 */
12459 					lt_bw = ONE_POINT_TWO_MEG;
12460 				}
12461 				rack->r_ctl.rc_rtt_diff = 0;
12462 				rack->r_ctl.gp_bw = lt_bw;
12463 				rack->rc_gp_filled = 1;
12464 				if (rack->r_ctl.num_measurements < RACK_REQ_AVG)
12465 					rack->r_ctl.num_measurements = RACK_REQ_AVG;
12466 				rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
12467 			} else if (lt_bw > rack->r_ctl.gp_bw) {
12468 				rack->r_ctl.rc_rtt_diff = 0;
12469 				if (rack->r_ctl.num_measurements < RACK_REQ_AVG)
12470 					rack->r_ctl.num_measurements = RACK_REQ_AVG;
12471 				rack->r_ctl.gp_bw = lt_bw;
12472 				rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
12473 			} else
12474 				rack_increase_bw_mul(rack, -1, 0, 0, 1);
12475 			if ((rack->gp_ready == 0) &&
12476 			    (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) {
12477 				/* We have enough measurements now */
12478 				rack->gp_ready = 1;
12479 				if (rack->dgp_on ||
12480 				    rack->rack_hibeta)
12481 					rack_set_cc_pacing(rack);
12482 				if (rack->defer_options)
12483 					rack_apply_deferred_options(rack);
12484 			}
12485 		} else {
12486 			/*
12487 			 * zero rtt possibly?, settle for just an old increase.
12488 			 */
12489 			rack_increase_bw_mul(rack, -1, 0, 0, 1);
12490 		}
12491 	} else if ((IN_FASTRECOVERY(tp->t_flags) == 0) &&
12492 		   (sbavail(&so->so_snd) > max((segsiz * (4 + rack_req_segs)),
12493 					       minseg)) &&
12494 		   (rack->r_ctl.cwnd_to_use > max((segsiz * (rack_req_segs + 2)), minseg)) &&
12495 		   (tp->snd_wnd > max((segsiz * (rack_req_segs + 2)), minseg)) &&
12496 		   (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) <=
12497 		    (segsiz * rack_req_segs))) {
12498 		/*
12499 		 * We are doing dynamic GP pacing and
12500 		 * we have everything except 1MSS or less
12501 		 * bytes left out. We are still pacing away.
12502 		 * And there is data that could be sent, This
12503 		 * means we are inserting delayed ack time in
12504 		 * our measurements because we are pacing too slow.
12505 		 */
12506 		rack_validate_multipliers_at_or_above100(rack);
12507 		rack->rc_dragged_bottom = 1;
12508 		rack_increase_bw_mul(rack, -1, 0, 0, 1);
12509 	}
12510 }
12511 
12512 #ifdef TCP_REQUEST_TRK
12513 static void
12514 rack_log_hybrid(struct tcp_rack *rack, uint32_t seq,
12515 		struct tcp_sendfile_track *cur, uint8_t mod, int line, int err)
12516 {
12517 	int do_log;
12518 
12519 	do_log = tcp_bblogging_on(rack->rc_tp);
12520 	if (do_log == 0) {
12521 		if ((do_log = tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING) )== 0)
12522 			return;
12523 		/* We only allow the three below with point logging on */
12524 		if ((mod != HYBRID_LOG_RULES_APP) &&
12525 		    (mod != HYBRID_LOG_RULES_SET) &&
12526 		    (mod != HYBRID_LOG_REQ_COMP))
12527 			return;
12528 
12529 	}
12530 	if (do_log) {
12531 		union tcp_log_stackspecific log;
12532 		struct timeval tv;
12533 
12534 		/* Convert our ms to a microsecond */
12535 		memset(&log, 0, sizeof(log));
12536 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
12537 		log.u_bbr.flex1 = seq;
12538 		log.u_bbr.cwnd_gain = line;
12539 		if (cur != NULL) {
12540 			uint64_t off;
12541 
12542 			log.u_bbr.flex2 = cur->start_seq;
12543 			log.u_bbr.flex3 = cur->end_seq;
12544 			log.u_bbr.flex4 = (uint32_t)((cur->localtime >> 32) & 0x00000000ffffffff);
12545 			log.u_bbr.flex5 = (uint32_t)(cur->localtime & 0x00000000ffffffff);
12546 			log.u_bbr.flex6 = cur->flags;
12547 			log.u_bbr.pkts_out = cur->hybrid_flags;
12548 			log.u_bbr.rttProp = cur->timestamp;
12549 			log.u_bbr.cur_del_rate = cur->cspr;
12550 			log.u_bbr.bw_inuse = cur->start;
12551 			log.u_bbr.applimited = (uint32_t)(cur->end & 0x00000000ffffffff);
12552 			log.u_bbr.delivered = (uint32_t)((cur->end >> 32) & 0x00000000ffffffff) ;
12553 			log.u_bbr.epoch = (uint32_t)(cur->deadline & 0x00000000ffffffff);
12554 			log.u_bbr.lt_epoch = (uint32_t)((cur->deadline >> 32) & 0x00000000ffffffff) ;
12555 			log.u_bbr.inhpts = 1;
12556 #ifdef TCP_REQUEST_TRK
12557 			off = (uint64_t)(cur) - (uint64_t)(&rack->rc_tp->t_tcpreq_info[0]);
12558 			log.u_bbr.use_lt_bw = (uint8_t)(off / sizeof(struct tcp_sendfile_track));
12559 #endif
12560 		} else {
12561 			log.u_bbr.flex2 = err;
12562 		}
12563 		/*
12564 		 * Fill in flex7 to be CHD (catchup|hybrid|DGP)
12565 		 */
12566 		log.u_bbr.flex7 = rack->rc_catch_up;
12567 		log.u_bbr.flex7 <<= 1;
12568 		log.u_bbr.flex7 |= rack->rc_hybrid_mode;
12569 		log.u_bbr.flex7 <<= 1;
12570 		log.u_bbr.flex7 |= rack->dgp_on;
12571 		/*
12572 		 * Compose bbr_state to be a bit wise 0000ADHF
12573 		 * where A is the always_pace flag
12574 		 * where D is the dgp_on flag
12575 		 * where H is the hybrid_mode on flag
12576 		 * where F is the use_fixed_rate flag.
12577 		 */
12578 		log.u_bbr.bbr_state = rack->rc_always_pace;
12579 		log.u_bbr.bbr_state <<= 1;
12580 		log.u_bbr.bbr_state |= rack->dgp_on;
12581 		log.u_bbr.bbr_state <<= 1;
12582 		log.u_bbr.bbr_state |= rack->rc_hybrid_mode;
12583 		log.u_bbr.bbr_state <<= 1;
12584 		log.u_bbr.bbr_state |= rack->use_fixed_rate;
12585 		log.u_bbr.flex8 = mod;
12586 		log.u_bbr.delRate = rack->r_ctl.bw_rate_cap;
12587 		log.u_bbr.bbr_substate = rack->r_ctl.client_suggested_maxseg;
12588 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
12589 		log.u_bbr.pkt_epoch = rack->rc_tp->tcp_hybrid_start;
12590 		log.u_bbr.lost = rack->rc_tp->tcp_hybrid_error;
12591 		log.u_bbr.pacing_gain = (uint16_t)rack->rc_tp->tcp_hybrid_stop;
12592 		tcp_log_event(rack->rc_tp, NULL,
12593 		    &rack->rc_inp->inp_socket->so_rcv,
12594 		    &rack->rc_inp->inp_socket->so_snd,
12595 		    TCP_HYBRID_PACING_LOG, 0,
12596 	            0, &log, false, NULL, __func__, __LINE__, &tv);
12597 	}
12598 }
12599 #endif
12600 
12601 #ifdef TCP_REQUEST_TRK
12602 static void
12603 rack_set_dgp_hybrid_mode(struct tcp_rack *rack, tcp_seq seq, uint32_t len, uint64_t cts)
12604 {
12605 	struct tcp_sendfile_track *rc_cur, *orig_ent;
12606 	struct tcpcb *tp;
12607 	int err = 0;
12608 
12609 	orig_ent = rack->r_ctl.rc_last_sft;
12610 	rc_cur = tcp_req_find_req_for_seq(rack->rc_tp, seq);
12611 	if (rc_cur == NULL) {
12612 		/* If not in the beginning what about the end piece */
12613 		if (rack->rc_hybrid_mode)
12614 			rack_log_hybrid(rack, seq, NULL, HYBRID_LOG_NO_RANGE, __LINE__, err);
12615 		rc_cur = tcp_req_find_req_for_seq(rack->rc_tp, (seq + len - 1));
12616 	} else {
12617 		err = 12345;
12618 	}
12619 	/* If we find no parameters we are in straight DGP mode */
12620 	if(rc_cur == NULL) {
12621 		/* None found for this seq, just DGP for now */
12622 		if (rack->rc_hybrid_mode) {
12623 			rack->r_ctl.client_suggested_maxseg = 0;
12624 			rack->rc_catch_up = 0;
12625 			if (rack->cspr_is_fcc == 0)
12626 				rack->r_ctl.bw_rate_cap = 0;
12627 			else
12628 				rack->r_ctl.fillcw_cap = rack_fillcw_bw_cap;
12629 		}
12630 		if (rack->rc_hybrid_mode) {
12631 			rack_log_hybrid(rack, (seq + len - 1), NULL, HYBRID_LOG_NO_RANGE, __LINE__, err);
12632 		}
12633 		if (rack->r_ctl.rc_last_sft) {
12634 			rack->r_ctl.rc_last_sft = NULL;
12635 		}
12636 		return;
12637 	}
12638 	if ((rc_cur->hybrid_flags & TCP_HYBRID_PACING_WASSET) == 0) {
12639 		/* This entry was never setup for hybrid pacing on/off etc */
12640 		if (rack->rc_hybrid_mode) {
12641 			rack->r_ctl.client_suggested_maxseg = 0;
12642 			rack->rc_catch_up = 0;
12643 			rack->r_ctl.bw_rate_cap = 0;
12644 		}
12645 		if (rack->r_ctl.rc_last_sft) {
12646 			rack->r_ctl.rc_last_sft = NULL;
12647 		}
12648 		if ((rc_cur->flags & TCP_TRK_TRACK_FLG_FSND) == 0) {
12649 			rc_cur->flags |= TCP_TRK_TRACK_FLG_FSND;
12650 			rc_cur->first_send = cts;
12651 			rc_cur->sent_at_fs = rack->rc_tp->t_sndbytes;
12652 			rc_cur->rxt_at_fs = rack->rc_tp->t_snd_rxt_bytes;
12653 		}
12654 		return;
12655 	}
12656 	/*
12657 	 * Ok if we have a new entry *or* have never
12658 	 * set up an entry we need to proceed. If
12659 	 * we have already set it up this entry we
12660 	 * just continue along with what we already
12661 	 * setup.
12662 	 */
12663 	tp = rack->rc_tp;
12664 	if ((rack->r_ctl.rc_last_sft != NULL) &&
12665 	    (rack->r_ctl.rc_last_sft == rc_cur)) {
12666 		/* Its already in place */
12667 		if (rack->rc_hybrid_mode)
12668 			rack_log_hybrid(rack, seq, rc_cur, HYBRID_LOG_ISSAME, __LINE__, 0);
12669 		return;
12670 	}
12671 	if (rack->rc_hybrid_mode == 0) {
12672 		rack->r_ctl.rc_last_sft = rc_cur;
12673 		if (orig_ent) {
12674 			orig_ent->sent_at_ls = rack->rc_tp->t_sndbytes;
12675 			orig_ent->rxt_at_ls = rack->rc_tp->t_snd_rxt_bytes;
12676 			orig_ent->flags |= TCP_TRK_TRACK_FLG_LSND;
12677 		}
12678 		rack_log_hybrid(rack, seq, rc_cur, HYBRID_LOG_RULES_APP, __LINE__, 0);
12679 		return;
12680 	}
12681 	if ((rc_cur->hybrid_flags & TCP_HYBRID_PACING_CSPR) && rc_cur->cspr){
12682 		/* Compensate for all the header overhead's */
12683 		if (rack->cspr_is_fcc == 0)
12684 			rack->r_ctl.bw_rate_cap	= rack_compensate_for_linerate(rack, rc_cur->cspr);
12685 		else
12686 			rack->r_ctl.fillcw_cap =  rack_compensate_for_linerate(rack, rc_cur->cspr);
12687 	} else {
12688 		if (rack->rc_hybrid_mode) {
12689 			if (rack->cspr_is_fcc == 0)
12690 				rack->r_ctl.bw_rate_cap = 0;
12691 			else
12692 				rack->r_ctl.fillcw_cap = rack_fillcw_bw_cap;
12693 		}
12694 	}
12695 	if (rc_cur->hybrid_flags & TCP_HYBRID_PACING_H_MS)
12696 		rack->r_ctl.client_suggested_maxseg = rc_cur->hint_maxseg;
12697 	else
12698 		rack->r_ctl.client_suggested_maxseg = 0;
12699 	if (rc_cur->timestamp == rack->r_ctl.last_tm_mark) {
12700 		/*
12701 		 * It is the same timestamp as the previous one
12702 		 * add the hybrid flag that will indicate we use
12703 		 * sendtime not arrival time for catch-up mode.
12704 		 */
12705 		rc_cur->hybrid_flags |= TCP_HYBRID_PACING_SENDTIME;
12706 	}
12707 	if ((rc_cur->hybrid_flags & TCP_HYBRID_PACING_CU) &&
12708 	    (rc_cur->cspr > 0)) {
12709 		uint64_t len;
12710 
12711 		rack->rc_catch_up = 1;
12712 		/*
12713 		 * Calculate the deadline time, first set the
12714 		 * time to when the request arrived.
12715 		 */
12716 		if (rc_cur->hybrid_flags & TCP_HYBRID_PACING_SENDTIME) {
12717 			/*
12718 			 * For cases where its a duplicate tm (we received more
12719 			 * than one request for a tm) we want to use now, the point
12720 			 * where we are just sending the first bit of the request.
12721 			 */
12722 			rc_cur->deadline = cts;
12723 		} else {
12724 			/*
12725 			 * Here we have a different tm from the last request
12726 			 * so we want to use arrival time as our base.
12727 			 */
12728 			rc_cur->deadline = rc_cur->localtime;
12729 		}
12730 		/*
12731 		 * Next calculate the length and compensate for
12732 		 * TLS if need be.
12733 		 */
12734 		len = rc_cur->end - rc_cur->start;
12735 		if (tp->t_inpcb.inp_socket->so_snd.sb_tls_info) {
12736 			/*
12737 			 * This session is doing TLS. Take a swag guess
12738 			 * at the overhead.
12739 			 */
12740 			len += tcp_estimate_tls_overhead(tp->t_inpcb.inp_socket, len);
12741 		}
12742 		/*
12743 		 * Now considering the size, and the cspr, what is the time that
12744 		 * would be required at the cspr rate. Here we use the raw
12745 		 * cspr value since the client only looks at the raw data. We
12746 		 * do use len which includes TLS overhead, but not the TCP/IP etc.
12747 		 * That will get made up for in the CU pacing rate set.
12748 		 */
12749 		len *= HPTS_USEC_IN_SEC;
12750 		len /= rc_cur->cspr;
12751 		rc_cur->deadline += len;
12752 	} else {
12753 		rack->rc_catch_up = 0;
12754 		rc_cur->deadline = 0;
12755 	}
12756 	if (rack->r_ctl.client_suggested_maxseg != 0) {
12757 		/*
12758 		 * We need to reset the max pace segs if we have a
12759 		 * client_suggested_maxseg.
12760 		 */
12761 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
12762 	}
12763 	if (orig_ent) {
12764 		orig_ent->sent_at_ls = rack->rc_tp->t_sndbytes;
12765 		orig_ent->rxt_at_ls = rack->rc_tp->t_snd_rxt_bytes;
12766 		orig_ent->flags |= TCP_TRK_TRACK_FLG_LSND;
12767 	}
12768 	rack_log_hybrid(rack, seq, rc_cur, HYBRID_LOG_RULES_APP, __LINE__, 0);
12769 	/* Remember it for next time and for CU mode */
12770 	rack->r_ctl.rc_last_sft = rc_cur;
12771 	rack->r_ctl.last_tm_mark = rc_cur->timestamp;
12772 }
12773 #endif
12774 
12775 static void
12776 rack_chk_req_and_hybrid_on_out(struct tcp_rack *rack, tcp_seq seq, uint32_t len, uint64_t cts)
12777 {
12778 #ifdef TCP_REQUEST_TRK
12779 	struct tcp_sendfile_track *ent;
12780 
12781 	ent = rack->r_ctl.rc_last_sft;
12782 	if ((ent == NULL) ||
12783 	    (ent->flags == TCP_TRK_TRACK_FLG_EMPTY) ||
12784 	    (SEQ_GEQ(seq, ent->end_seq))) {
12785 		/* Time to update the track. */
12786 		rack_set_dgp_hybrid_mode(rack, seq, len, cts);
12787 		ent = rack->r_ctl.rc_last_sft;
12788 	}
12789 	/* Out of all */
12790 	if (ent == NULL) {
12791 		return;
12792 	}
12793 	if (SEQ_LT(ent->end_seq, (seq + len))) {
12794 		/*
12795 		 * This is the case where our end_seq guess
12796 		 * was wrong. This is usually due to TLS having
12797 		 * more bytes then our guess. It could also be the
12798 		 * case that the client sent in two requests closely
12799 		 * and the SB is full of both so we are sending part
12800 		 * of each (end|beg). In such a case lets move this
12801 		 * guys end to match the end of this send. That
12802 		 * way it will complete when all of it is acked.
12803 		 */
12804 		ent->end_seq = (seq + len);
12805 		if (rack->rc_hybrid_mode)
12806 			rack_log_hybrid_bw(rack, seq, len, 0, 0, HYBRID_LOG_EXTEND, 0, ent, __LINE__);
12807 	}
12808 	/* Now validate we have set the send time of this one */
12809 	if ((ent->flags & TCP_TRK_TRACK_FLG_FSND) == 0) {
12810 		ent->flags |= TCP_TRK_TRACK_FLG_FSND;
12811 		ent->first_send = cts;
12812 		ent->sent_at_fs = rack->rc_tp->t_sndbytes;
12813 		ent->rxt_at_fs = rack->rc_tp->t_snd_rxt_bytes;
12814 	}
12815 #endif
12816 }
12817 
12818 static void
12819 rack_gain_for_fastoutput(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t acked_amount)
12820 {
12821 	/*
12822 	 * The fast output path is enabled and we
12823 	 * have moved the cumack forward. Lets see if
12824 	 * we can expand forward the fast path length by
12825 	 * that amount. What we would ideally like to
12826 	 * do is increase the number of bytes in the
12827 	 * fast path block (left_to_send) by the
12828 	 * acked amount. However we have to gate that
12829 	 * by two factors:
12830 	 * 1) The amount outstanding and the rwnd of the peer
12831 	 *    (i.e. we don't want to exceed the rwnd of the peer).
12832 	 *    <and>
12833 	 * 2) The amount of data left in the socket buffer (i.e.
12834 	 *    we can't send beyond what is in the buffer).
12835 	 *
12836 	 * Note that this does not take into account any increase
12837 	 * in the cwnd. We will only extend the fast path by
12838 	 * what was acked.
12839 	 */
12840 	uint32_t new_total, gating_val;
12841 
12842 	new_total = acked_amount + rack->r_ctl.fsb.left_to_send;
12843 	gating_val = min((sbavail(&so->so_snd) - (tp->snd_max - tp->snd_una)),
12844 			 (tp->snd_wnd - (tp->snd_max - tp->snd_una)));
12845 	if (new_total <= gating_val) {
12846 		/* We can increase left_to_send by the acked amount */
12847 		counter_u64_add(rack_extended_rfo, 1);
12848 		rack->r_ctl.fsb.left_to_send = new_total;
12849 		KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(&rack->rc_inp->inp_socket->so_snd) - (tp->snd_max - tp->snd_una))),
12850 			("rack:%p left_to_send:%u sbavail:%u out:%u",
12851 			 rack, rack->r_ctl.fsb.left_to_send,
12852 			 sbavail(&rack->rc_inp->inp_socket->so_snd),
12853 			 (tp->snd_max - tp->snd_una)));
12854 
12855 	}
12856 }
12857 
12858 static void
12859 rack_adjust_sendmap_head(struct tcp_rack *rack, struct sockbuf *sb)
12860 {
12861 	/*
12862 	 * Here any sendmap entry that points to the
12863 	 * beginning mbuf must be adjusted to the correct
12864 	 * offset. This must be called with:
12865 	 * 1) The socket buffer locked
12866 	 * 2) snd_una adjusted to its new position.
12867 	 *
12868 	 * Note that (2) implies rack_ack_received has also
12869 	 * been called and all the sbcut's have been done.
12870 	 *
12871 	 * We grab the first mbuf in the socket buffer and
12872 	 * then go through the front of the sendmap, recalculating
12873 	 * the stored offset for any sendmap entry that has
12874 	 * that mbuf. We must use the sb functions to do this
12875 	 * since its possible an add was done has well as
12876 	 * the subtraction we may have just completed. This should
12877 	 * not be a penalty though, since we just referenced the sb
12878 	 * to go in and trim off the mbufs that we freed (of course
12879 	 * there will be a penalty for the sendmap references though).
12880 	 *
12881 	 * Note also with INVARIANT on, we validate with a KASSERT
12882 	 * that the first sendmap entry has a soff of 0.
12883 	 *
12884 	 */
12885 	struct mbuf *m;
12886 	struct rack_sendmap *rsm;
12887 	tcp_seq snd_una;
12888 #ifdef INVARIANTS
12889 	int first_processed = 0;
12890 #endif
12891 
12892 	snd_una = rack->rc_tp->snd_una;
12893 	SOCKBUF_LOCK_ASSERT(sb);
12894 	m = sb->sb_mb;
12895 	rsm = tqhash_min(rack->r_ctl.tqh);
12896 	if ((rsm == NULL) || (m == NULL)) {
12897 		/* Nothing outstanding */
12898 		return;
12899 	}
12900 	/* The very first RSM's mbuf must point to the head mbuf in the sb */
12901 	KASSERT((rsm->m == m),
12902 		("Rack:%p sb:%p rsm:%p -- first rsm mbuf not aligned to sb",
12903 		 rack, sb, rsm));
12904 	while (rsm->m && (rsm->m == m)) {
12905 		/* one to adjust */
12906 #ifdef INVARIANTS
12907 		struct mbuf *tm;
12908 		uint32_t soff;
12909 
12910 		tm = sbsndmbuf(sb, (rsm->r_start - snd_una), &soff);
12911 		if ((rsm->orig_m_len != m->m_len) ||
12912 		    (rsm->orig_t_space != M_TRAILINGROOM(m))){
12913 			rack_adjust_orig_mlen(rsm);
12914 		}
12915 		if (first_processed == 0) {
12916 			KASSERT((rsm->soff == 0),
12917 				("Rack:%p rsm:%p -- rsm at head but soff not zero",
12918 				 rack, rsm));
12919 			first_processed = 1;
12920 		}
12921 		if ((rsm->soff != soff) || (rsm->m != tm)) {
12922 			/*
12923 			 * This is not a fatal error, we anticipate it
12924 			 * might happen (the else code), so we count it here
12925 			 * so that under invariant we can see that it really
12926 			 * does happen.
12927 			 */
12928 			counter_u64_add(rack_adjust_map_bw, 1);
12929 		}
12930 		rsm->m = tm;
12931 		rsm->soff = soff;
12932 		if (tm) {
12933 			rsm->orig_m_len = rsm->m->m_len;
12934 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
12935 		} else {
12936 			rsm->orig_m_len = 0;
12937 			rsm->orig_t_space = 0;
12938 		}
12939 #else
12940 		rsm->m = sbsndmbuf(sb, (rsm->r_start - snd_una), &rsm->soff);
12941 		if (rsm->m) {
12942 			rsm->orig_m_len = rsm->m->m_len;
12943 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
12944 		} else {
12945 			rsm->orig_m_len = 0;
12946 			rsm->orig_t_space = 0;
12947 		}
12948 #endif
12949 		rsm = tqhash_next(rack->r_ctl.tqh, rsm);
12950 		if (rsm == NULL)
12951 			break;
12952 	}
12953 }
12954 
12955 #ifdef TCP_REQUEST_TRK
12956 static inline void
12957 rack_req_check_for_comp(struct tcp_rack *rack, tcp_seq th_ack)
12958 {
12959 	struct tcp_sendfile_track *ent;
12960 	int i;
12961 
12962 	if ((rack->rc_hybrid_mode == 0) &&
12963 	    (tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING) == 0)) {
12964 		/*
12965 		 * Just do normal completions hybrid pacing is not on
12966 		 * and CLDL is off as well.
12967 		 */
12968 		tcp_req_check_for_comp(rack->rc_tp, th_ack);
12969 		return;
12970 	}
12971 	/*
12972 	 * Originally I was just going to find the th_ack associated
12973 	 * with an entry. But then I realized a large strech ack could
12974 	 * in theory ack two or more requests at once. So instead we
12975 	 * need to find all entries that are completed by th_ack not
12976 	 * just a single entry and do our logging.
12977 	 */
12978 	ent = tcp_req_find_a_req_that_is_completed_by(rack->rc_tp, th_ack, &i);
12979 	while (ent != NULL) {
12980 		/*
12981 		 * We may be doing hybrid pacing or CLDL and need more details possibly
12982 		 * so we do it manually instead of calling
12983 		 * tcp_req_check_for_comp()
12984 		 */
12985 		uint64_t laa, tim, data, cbw, ftim;
12986 
12987 		/* Ok this ack frees it */
12988 		rack_log_hybrid(rack, th_ack,
12989 				ent, HYBRID_LOG_REQ_COMP, __LINE__, 0);
12990 		rack_log_hybrid_sends(rack, ent, __LINE__);
12991 		/* calculate the time based on the ack arrival */
12992 		data = ent->end - ent->start;
12993 		laa = tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time);
12994 		if (ent->flags & TCP_TRK_TRACK_FLG_FSND) {
12995 			if (ent->first_send > ent->localtime)
12996 				ftim = ent->first_send;
12997 			else
12998 				ftim = ent->localtime;
12999 		} else {
13000 			/* TSNH */
13001 			ftim = ent->localtime;
13002 		}
13003 		if (laa > ent->localtime)
13004 			tim = laa - ftim;
13005 		else
13006 			tim = 0;
13007 		cbw = data * HPTS_USEC_IN_SEC;
13008 		if (tim > 0)
13009 			cbw /= tim;
13010 		else
13011 			cbw = 0;
13012 		rack_log_hybrid_bw(rack, th_ack, cbw, tim, data, HYBRID_LOG_BW_MEASURE, 0, ent, __LINE__);
13013 		/*
13014 		 * Check to see if we are freeing what we are pointing to send wise
13015 		 * if so be sure to NULL the pointer so we know we are no longer
13016 		 * set to anything.
13017 		 */
13018 		if (ent == rack->r_ctl.rc_last_sft) {
13019 			rack->r_ctl.rc_last_sft = NULL;
13020 			if (rack->rc_hybrid_mode) {
13021 				rack->rc_catch_up = 0;
13022 				if (rack->cspr_is_fcc == 0)
13023 					rack->r_ctl.bw_rate_cap = 0;
13024 				else
13025 					rack->r_ctl.fillcw_cap = rack_fillcw_bw_cap;
13026 				rack->r_ctl.client_suggested_maxseg = 0;
13027 			}
13028 		}
13029 		/* Generate the log that the tcp_netflix call would have */
13030 		tcp_req_log_req_info(rack->rc_tp, ent,
13031 				      i, TCP_TRK_REQ_LOG_FREED, 0, 0);
13032 		/* Free it and see if there is another one */
13033 		tcp_req_free_a_slot(rack->rc_tp, ent);
13034 		ent = tcp_req_find_a_req_that_is_completed_by(rack->rc_tp, th_ack, &i);
13035 	}
13036 }
13037 #endif
13038 
13039 
13040 /*
13041  * Return value of 1, we do not need to call rack_process_data().
13042  * return value of 0, rack_process_data can be called.
13043  * For ret_val if its 0 the TCP is locked, if its non-zero
13044  * its unlocked and probably unsafe to touch the TCB.
13045  */
13046 static int
13047 rack_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so,
13048     struct tcpcb *tp, struct tcpopt *to,
13049     uint32_t tiwin, int32_t tlen,
13050     int32_t * ofia, int32_t thflags, int32_t *ret_val, int32_t orig_tlen)
13051 {
13052 	int32_t ourfinisacked = 0;
13053 	int32_t nsegs, acked_amount;
13054 	int32_t acked;
13055 	struct mbuf *mfree;
13056 	struct tcp_rack *rack;
13057 	int32_t under_pacing = 0;
13058 	int32_t post_recovery = 0;
13059 	uint32_t p_cwnd;
13060 
13061 	INP_WLOCK_ASSERT(tptoinpcb(tp));
13062 
13063 	rack = (struct tcp_rack *)tp->t_fb_ptr;
13064 	if (SEQ_GT(th->th_ack, tp->snd_max)) {
13065 		__ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val,
13066 				      &rack->r_ctl.challenge_ack_ts,
13067 				      &rack->r_ctl.challenge_ack_cnt);
13068 		rack->r_wanted_output = 1;
13069 		return (1);
13070 	}
13071 	if (rack->gp_ready &&
13072 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
13073 		under_pacing = 1;
13074 	}
13075 	if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) {
13076 		int in_rec, dup_ack_struck = 0;
13077 		int dsack_seen = 0, sacks_seen = 0;
13078 
13079 		in_rec = IN_FASTRECOVERY(tp->t_flags);
13080 		if (rack->rc_in_persist) {
13081 			tp->t_rxtshift = 0;
13082 			RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
13083 				      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
13084 		}
13085 
13086 		if ((th->th_ack == tp->snd_una) &&
13087 		    (tiwin == tp->snd_wnd) &&
13088 		    (orig_tlen == 0) &&
13089 		    ((to->to_flags & TOF_SACK) == 0)) {
13090 			rack_strike_dupack(rack, th->th_ack);
13091 			dup_ack_struck = 1;
13092 		}
13093 		rack_log_ack(tp, to, th, ((in_rec == 0) && IN_FASTRECOVERY(tp->t_flags)),
13094 			     dup_ack_struck, &dsack_seen, &sacks_seen);
13095 		if ((rack->sack_attack_disable > 0) &&
13096 		    (th->th_ack == tp->snd_una) &&
13097 		    (tiwin == tp->snd_wnd) &&
13098 		    (orig_tlen == 0) &&
13099 		    (dsack_seen == 0) &&
13100 		    (sacks_seen > 0)) {
13101 			/*
13102 			 * If sacks have been disabled we may
13103 			 * want to strike a dup-ack "ignoring" the
13104 			 * sack as long as the sack was not a "dsack". Note
13105 			 * that if no sack is sent (TOF_SACK is off) then the
13106 			 * normal dsack code above rack_log_ack() would have
13107 			 * already struck. So this is just to catch the case
13108 			 * were we are ignoring sacks from this guy due to
13109 			 * it being a suspected attacker.
13110 			 */
13111 			rack_strike_dupack(rack, th->th_ack);
13112 		}
13113 
13114 	}
13115 	if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
13116 		/*
13117 		 * Old ack, behind (or duplicate to) the last one rcv'd
13118 		 * Note: We mark reordering is occuring if its
13119 		 * less than and we have not closed our window.
13120 		 */
13121 		if (SEQ_LT(th->th_ack, tp->snd_una) && (sbspace(&so->so_rcv) > ctf_fixed_maxseg(tp))) {
13122 			rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
13123 			if (rack->r_ctl.rc_reorder_ts == 0)
13124 				rack->r_ctl.rc_reorder_ts = 1;
13125 		}
13126 		return (0);
13127 	}
13128 	/*
13129 	 * If we reach this point, ACK is not a duplicate, i.e., it ACKs
13130 	 * something we sent.
13131 	 */
13132 	if (tp->t_flags & TF_NEEDSYN) {
13133 		/*
13134 		 * T/TCP: Connection was half-synchronized, and our SYN has
13135 		 * been ACK'd (so connection is now fully synchronized).  Go
13136 		 * to non-starred state, increment snd_una for ACK of SYN,
13137 		 * and check if we can do window scaling.
13138 		 */
13139 		tp->t_flags &= ~TF_NEEDSYN;
13140 		tp->snd_una++;
13141 		/* Do window scaling? */
13142 		if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
13143 		    (TF_RCVD_SCALE | TF_REQ_SCALE)) {
13144 			tp->rcv_scale = tp->request_r_scale;
13145 			/* Send window already scaled. */
13146 		}
13147 	}
13148 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
13149 
13150 	acked = BYTES_THIS_ACK(tp, th);
13151 	if (acked) {
13152 		/*
13153 		 * Any time we move the cum-ack forward clear
13154 		 * keep-alive tied probe-not-answered. The
13155 		 * persists clears its own on entry.
13156 		 */
13157 		rack->probe_not_answered = 0;
13158 	}
13159 	KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
13160 	KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
13161 	/*
13162 	 * If we just performed our first retransmit, and the ACK arrives
13163 	 * within our recovery window, then it was a mistake to do the
13164 	 * retransmit in the first place.  Recover our original cwnd and
13165 	 * ssthresh, and proceed to transmit where we left off.
13166 	 */
13167 	if ((tp->t_flags & TF_PREVVALID) &&
13168 	    ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
13169 		tp->t_flags &= ~TF_PREVVALID;
13170 		if (tp->t_rxtshift == 1 &&
13171 		    (int)(ticks - tp->t_badrxtwin) < 0)
13172 			rack_cong_signal(tp, CC_RTO_ERR, th->th_ack, __LINE__);
13173 	}
13174 	if (acked) {
13175 		/* assure we are not backed off */
13176 		tp->t_rxtshift = 0;
13177 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
13178 			      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
13179 		rack->rc_tlp_in_progress = 0;
13180 		rack->r_ctl.rc_tlp_cnt_out = 0;
13181 		/*
13182 		 * If it is the RXT timer we want to
13183 		 * stop it, so we can restart a TLP.
13184 		 */
13185 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
13186 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
13187 #ifdef TCP_REQUEST_TRK
13188 		rack_req_check_for_comp(rack, th->th_ack);
13189 #endif
13190 	}
13191 	/*
13192 	 * If we have a timestamp reply, update smoothed round trip time. If
13193 	 * no timestamp is present but transmit timer is running and timed
13194 	 * sequence number was acked, update smoothed round trip time. Since
13195 	 * we now have an rtt measurement, cancel the timer backoff (cf.,
13196 	 * Phil Karn's retransmit alg.). Recompute the initial retransmit
13197 	 * timer.
13198 	 *
13199 	 * Some boxes send broken timestamp replies during the SYN+ACK
13200 	 * phase, ignore timestamps of 0 or we could calculate a huge RTT
13201 	 * and blow up the retransmit timer.
13202 	 */
13203 	/*
13204 	 * If all outstanding data is acked, stop retransmit timer and
13205 	 * remember to restart (more output or persist). If there is more
13206 	 * data to be acked, restart retransmit timer, using current
13207 	 * (possibly backed-off) value.
13208 	 */
13209 	if (acked == 0) {
13210 		if (ofia)
13211 			*ofia = ourfinisacked;
13212 		return (0);
13213 	}
13214 	if (IN_RECOVERY(tp->t_flags)) {
13215 		if (SEQ_LT(th->th_ack, tp->snd_recover) &&
13216 		    (SEQ_LT(th->th_ack, tp->snd_max))) {
13217 			tcp_rack_partialack(tp);
13218 		} else {
13219 			rack_post_recovery(tp, th->th_ack);
13220 			post_recovery = 1;
13221 			/*
13222 			 * Grab the segsiz, multiply by 2 and add the snd_cwnd
13223 			 * that is the max the CC should add if we are exiting
13224 			 * recovery and doing a late add.
13225 			 */
13226 			p_cwnd = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
13227 			p_cwnd <<= 1;
13228 			p_cwnd += tp->snd_cwnd;
13229 		}
13230 	} else if ((rack->rto_from_rec == 1) &&
13231 		   SEQ_GEQ(th->th_ack, tp->snd_recover)) {
13232 		/*
13233 		 * We were in recovery, hit a rxt timeout
13234 		 * and never re-entered recovery. The timeout(s)
13235 		 * made up all the lost data. In such a case
13236 		 * we need to clear the rto_from_rec flag.
13237 		 */
13238 		rack->rto_from_rec = 0;
13239 	}
13240 	/*
13241 	 * Let the congestion control algorithm update congestion control
13242 	 * related information. This typically means increasing the
13243 	 * congestion window.
13244 	 */
13245 	rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, post_recovery);
13246 	if (post_recovery &&
13247 	    (tp->snd_cwnd > p_cwnd)) {
13248 		/* Must be non-newreno (cubic) getting too ahead of itself */
13249 		tp->snd_cwnd = p_cwnd;
13250 	}
13251 	SOCKBUF_LOCK(&so->so_snd);
13252 	acked_amount = min(acked, (int)sbavail(&so->so_snd));
13253 	tp->snd_wnd -= acked_amount;
13254 	mfree = sbcut_locked(&so->so_snd, acked_amount);
13255 	if ((sbused(&so->so_snd) == 0) &&
13256 	    (acked > acked_amount) &&
13257 	    (tp->t_state >= TCPS_FIN_WAIT_1) &&
13258 	    (tp->t_flags & TF_SENTFIN)) {
13259 		/*
13260 		 * We must be sure our fin
13261 		 * was sent and acked (we can be
13262 		 * in FIN_WAIT_1 without having
13263 		 * sent the fin).
13264 		 */
13265 		ourfinisacked = 1;
13266 	}
13267 	tp->snd_una = th->th_ack;
13268 	/* wakeups? */
13269 	if (acked_amount && sbavail(&so->so_snd))
13270 		rack_adjust_sendmap_head(rack, &so->so_snd);
13271 	rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
13272 	/* NB: sowwakeup_locked() does an implicit unlock. */
13273 	sowwakeup_locked(so);
13274 	m_freem(mfree);
13275 	if (SEQ_GT(tp->snd_una, tp->snd_recover))
13276 		tp->snd_recover = tp->snd_una;
13277 
13278 	if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
13279 		tp->snd_nxt = tp->snd_max;
13280 	}
13281 	if (under_pacing &&
13282 	    (rack->use_fixed_rate == 0) &&
13283 	    (rack->in_probe_rtt == 0) &&
13284 	    rack->rc_gp_dyn_mul &&
13285 	    rack->rc_always_pace) {
13286 		/* Check if we are dragging bottom */
13287 		rack_check_bottom_drag(tp, rack, so);
13288 	}
13289 	if (tp->snd_una == tp->snd_max) {
13290 		/* Nothing left outstanding */
13291 		tp->t_flags &= ~TF_PREVVALID;
13292 		rack->r_ctl.idle_snd_una = tp->snd_una;
13293 		rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
13294 		if (rack->r_ctl.rc_went_idle_time == 0)
13295 			rack->r_ctl.rc_went_idle_time = 1;
13296 		rack->r_ctl.retran_during_recovery = 0;
13297 		rack->r_ctl.dsack_byte_cnt = 0;
13298 		rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
13299 		if (sbavail(&tptosocket(tp)->so_snd) == 0)
13300 			tp->t_acktime = 0;
13301 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
13302 		rack->rc_suspicious = 0;
13303 		/* Set need output so persist might get set */
13304 		rack->r_wanted_output = 1;
13305 		sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
13306 		if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
13307 		    (sbavail(&so->so_snd) == 0) &&
13308 		    (tp->t_flags2 & TF2_DROP_AF_DATA)) {
13309 			/*
13310 			 * The socket was gone and the
13311 			 * peer sent data (now or in the past), time to
13312 			 * reset him.
13313 			 */
13314 			*ret_val = 1;
13315 			/* tcp_close will kill the inp pre-log the Reset */
13316 			tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
13317 			tp = tcp_close(tp);
13318 			ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen);
13319 			return (1);
13320 		}
13321 	}
13322 	if (ofia)
13323 		*ofia = ourfinisacked;
13324 	return (0);
13325 }
13326 
13327 
13328 static void
13329 rack_log_collapse(struct tcp_rack *rack, uint32_t cnt, uint32_t split, uint32_t out, int line,
13330 		  int dir, uint32_t flags, struct rack_sendmap *rsm)
13331 {
13332 	if (tcp_bblogging_on(rack->rc_tp)) {
13333 		union tcp_log_stackspecific log;
13334 		struct timeval tv;
13335 
13336 		memset(&log, 0, sizeof(log));
13337 		log.u_bbr.flex1 = cnt;
13338 		log.u_bbr.flex2 = split;
13339 		log.u_bbr.flex3 = out;
13340 		log.u_bbr.flex4 = line;
13341 		log.u_bbr.flex5 = rack->r_must_retran;
13342 		log.u_bbr.flex6 = flags;
13343 		log.u_bbr.flex7 = rack->rc_has_collapsed;
13344 		log.u_bbr.flex8 = dir;	/*
13345 					 * 1 is collapsed, 0 is uncollapsed,
13346 					 * 2 is log of a rsm being marked, 3 is a split.
13347 					 */
13348 		if (rsm == NULL)
13349 			log.u_bbr.rttProp = 0;
13350 		else
13351 			log.u_bbr.rttProp = (uint64_t)rsm;
13352 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
13353 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
13354 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
13355 		    &rack->rc_inp->inp_socket->so_rcv,
13356 		    &rack->rc_inp->inp_socket->so_snd,
13357 		    TCP_RACK_LOG_COLLAPSE, 0,
13358 		    0, &log, false, &tv);
13359 	}
13360 }
13361 
13362 static void
13363 rack_collapsed_window(struct tcp_rack *rack, uint32_t out, tcp_seq th_ack, int line)
13364 {
13365 	/*
13366 	 * Here all we do is mark the collapsed point and set the flag.
13367 	 * This may happen again and again, but there is no
13368 	 * sense splitting our map until we know where the
13369 	 * peer finally lands in the collapse.
13370 	 */
13371 	tcp_trace_point(rack->rc_tp, TCP_TP_COLLAPSED_WND);
13372 	if ((rack->rc_has_collapsed == 0) ||
13373 	    (rack->r_ctl.last_collapse_point != (th_ack + rack->rc_tp->snd_wnd)))
13374 		counter_u64_add(rack_collapsed_win_seen, 1);
13375 	rack->r_ctl.last_collapse_point = th_ack + rack->rc_tp->snd_wnd;
13376 	rack->r_ctl.high_collapse_point = rack->rc_tp->snd_max;
13377 	rack->rc_has_collapsed = 1;
13378 	rack->r_collapse_point_valid = 1;
13379 	rack_log_collapse(rack, 0, th_ack, rack->r_ctl.last_collapse_point, line, 1, 0, NULL);
13380 }
13381 
13382 static void
13383 rack_un_collapse_window(struct tcp_rack *rack, int line)
13384 {
13385 	struct rack_sendmap *nrsm, *rsm;
13386 	int cnt = 0, split = 0;
13387 	int insret __diagused;
13388 
13389 
13390 	tcp_trace_point(rack->rc_tp, TCP_TP_COLLAPSED_WND);
13391 	rack->rc_has_collapsed = 0;
13392 	rsm = tqhash_find(rack->r_ctl.tqh, rack->r_ctl.last_collapse_point);
13393 	if (rsm == NULL) {
13394 		/* Nothing to do maybe the peer ack'ed it all */
13395 		rack_log_collapse(rack, 0, 0, ctf_outstanding(rack->rc_tp), line, 0, 0, NULL);
13396 		return;
13397 	}
13398 	/* Now do we need to split this one? */
13399 	if (SEQ_GT(rack->r_ctl.last_collapse_point, rsm->r_start)) {
13400 		rack_log_collapse(rack, rsm->r_start, rsm->r_end,
13401 				  rack->r_ctl.last_collapse_point, line, 3, rsm->r_flags, rsm);
13402 		nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
13403 		if (nrsm == NULL) {
13404 			/* We can't get a rsm, mark all? */
13405 			nrsm = rsm;
13406 			goto no_split;
13407 		}
13408 		/* Clone it */
13409 		split = 1;
13410 		rack_clone_rsm(rack, nrsm, rsm, rack->r_ctl.last_collapse_point);
13411 #ifndef INVARIANTS
13412 		(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
13413 #else
13414 		if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
13415 			panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p",
13416 			      nrsm, insret, rack, rsm);
13417 		}
13418 #endif
13419 		rack_log_map_chg(rack->rc_tp, rack, NULL, rsm, nrsm, MAP_SPLIT,
13420 				 rack->r_ctl.last_collapse_point, __LINE__);
13421 		if (rsm->r_in_tmap) {
13422 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
13423 			nrsm->r_in_tmap = 1;
13424 		}
13425 		/*
13426 		 * Set in the new RSM as the
13427 		 * collapsed starting point
13428 		 */
13429 		rsm = nrsm;
13430 	}
13431 
13432 no_split:
13433 	TQHASH_FOREACH_FROM(nrsm, rack->r_ctl.tqh, rsm)  {
13434 		cnt++;
13435 		nrsm->r_flags |= RACK_RWND_COLLAPSED;
13436 		rack_log_collapse(rack, nrsm->r_start, nrsm->r_end, 0, line, 4, nrsm->r_flags, nrsm);
13437 		cnt++;
13438 	}
13439 	if (cnt) {
13440 		counter_u64_add(rack_collapsed_win, 1);
13441 	}
13442 	rack_log_collapse(rack, cnt, split, ctf_outstanding(rack->rc_tp), line, 0, 0, NULL);
13443 }
13444 
13445 static void
13446 rack_handle_delayed_ack(struct tcpcb *tp, struct tcp_rack *rack,
13447 			int32_t tlen, int32_t tfo_syn)
13448 {
13449 	if (DELAY_ACK(tp, tlen) || tfo_syn) {
13450 		rack_timer_cancel(tp, rack,
13451 				  rack->r_ctl.rc_rcvtime, __LINE__);
13452 		tp->t_flags |= TF_DELACK;
13453 	} else {
13454 		rack->r_wanted_output = 1;
13455 		tp->t_flags |= TF_ACKNOW;
13456 	}
13457 }
13458 
13459 static void
13460 rack_validate_fo_sendwin_up(struct tcpcb *tp, struct tcp_rack *rack)
13461 {
13462 	/*
13463 	 * If fast output is in progress, lets validate that
13464 	 * the new window did not shrink on us and make it
13465 	 * so fast output should end.
13466 	 */
13467 	if (rack->r_fast_output) {
13468 		uint32_t out;
13469 
13470 		/*
13471 		 * Calculate what we will send if left as is
13472 		 * and compare that to our send window.
13473 		 */
13474 		out = ctf_outstanding(tp);
13475 		if ((out + rack->r_ctl.fsb.left_to_send) > tp->snd_wnd) {
13476 			/* ok we have an issue */
13477 			if (out >= tp->snd_wnd) {
13478 				/* Turn off fast output the window is met or collapsed */
13479 				rack->r_fast_output = 0;
13480 			} else {
13481 				/* we have some room left */
13482 				rack->r_ctl.fsb.left_to_send = tp->snd_wnd - out;
13483 				if (rack->r_ctl.fsb.left_to_send < ctf_fixed_maxseg(tp)) {
13484 					/* If not at least 1 full segment never mind */
13485 					rack->r_fast_output = 0;
13486 				}
13487 			}
13488 		}
13489 	}
13490 }
13491 
13492 /*
13493  * Return value of 1, the TCB is unlocked and most
13494  * likely gone, return value of 0, the TCP is still
13495  * locked.
13496  */
13497 static int
13498 rack_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so,
13499     struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
13500     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
13501 {
13502 	/*
13503 	 * Update window information. Don't look at window if no ACK: TAC's
13504 	 * send garbage on first SYN.
13505 	 */
13506 	int32_t nsegs;
13507 	int32_t tfo_syn;
13508 	struct tcp_rack *rack;
13509 
13510 	INP_WLOCK_ASSERT(tptoinpcb(tp));
13511 
13512 	rack = (struct tcp_rack *)tp->t_fb_ptr;
13513 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
13514 	if ((thflags & TH_ACK) &&
13515 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
13516 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
13517 	    (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
13518 		/* keep track of pure window updates */
13519 		if (tlen == 0 &&
13520 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
13521 			KMOD_TCPSTAT_INC(tcps_rcvwinupd);
13522 		tp->snd_wnd = tiwin;
13523 		rack_validate_fo_sendwin_up(tp, rack);
13524 		tp->snd_wl1 = th->th_seq;
13525 		tp->snd_wl2 = th->th_ack;
13526 		if (tp->snd_wnd > tp->max_sndwnd)
13527 			tp->max_sndwnd = tp->snd_wnd;
13528 		rack->r_wanted_output = 1;
13529 	} else if (thflags & TH_ACK) {
13530 		if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) {
13531 			tp->snd_wnd = tiwin;
13532 			rack_validate_fo_sendwin_up(tp, rack);
13533 			tp->snd_wl1 = th->th_seq;
13534 			tp->snd_wl2 = th->th_ack;
13535 		}
13536 	}
13537 	if (tp->snd_wnd < ctf_outstanding(tp))
13538 		/* The peer collapsed the window */
13539 		rack_collapsed_window(rack, ctf_outstanding(tp), th->th_ack, __LINE__);
13540 	else if (rack->rc_has_collapsed)
13541 		rack_un_collapse_window(rack, __LINE__);
13542 	if ((rack->r_collapse_point_valid) &&
13543 	    (SEQ_GT(th->th_ack, rack->r_ctl.high_collapse_point)))
13544 		rack->r_collapse_point_valid = 0;
13545 	/* Was persist timer active and now we have window space? */
13546 	if ((rack->rc_in_persist != 0) &&
13547 	    (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
13548 				rack->r_ctl.rc_pace_min_segs))) {
13549 		rack_exit_persist(tp, rack, rack->r_ctl.rc_rcvtime);
13550 		tp->snd_nxt = tp->snd_max;
13551 		/* Make sure we output to start the timer */
13552 		rack->r_wanted_output = 1;
13553 	}
13554 	/* Do we enter persists? */
13555 	if ((rack->rc_in_persist == 0) &&
13556 	    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
13557 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
13558 	    ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) &&
13559 	    sbavail(&tptosocket(tp)->so_snd) &&
13560 	    (sbavail(&tptosocket(tp)->so_snd) > tp->snd_wnd)) {
13561 		/*
13562 		 * Here the rwnd is less than
13563 		 * the pacing size, we are established,
13564 		 * nothing is outstanding, and there is
13565 		 * data to send. Enter persists.
13566 		 */
13567 		rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, tp->snd_una);
13568 	}
13569 	if (tp->t_flags2 & TF2_DROP_AF_DATA) {
13570 		m_freem(m);
13571 		return (0);
13572 	}
13573 	/*
13574 	 * don't process the URG bit, ignore them drag
13575 	 * along the up.
13576 	 */
13577 	tp->rcv_up = tp->rcv_nxt;
13578 
13579 	/*
13580 	 * Process the segment text, merging it into the TCP sequencing
13581 	 * queue, and arranging for acknowledgment of receipt if necessary.
13582 	 * This process logically involves adjusting tp->rcv_wnd as data is
13583 	 * presented to the user (this happens in tcp_usrreq.c, case
13584 	 * PRU_RCVD).  If a FIN has already been received on this connection
13585 	 * then we just ignore the text.
13586 	 */
13587 	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
13588 		   IS_FASTOPEN(tp->t_flags));
13589 	if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
13590 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
13591 		tcp_seq save_start = th->th_seq;
13592 		tcp_seq save_rnxt  = tp->rcv_nxt;
13593 		int     save_tlen  = tlen;
13594 
13595 		m_adj(m, drop_hdrlen);	/* delayed header drop */
13596 		/*
13597 		 * Insert segment which includes th into TCP reassembly
13598 		 * queue with control block tp.  Set thflags to whether
13599 		 * reassembly now includes a segment with FIN.  This handles
13600 		 * the common case inline (segment is the next to be
13601 		 * received on an established connection, and the queue is
13602 		 * empty), avoiding linkage into and removal from the queue
13603 		 * and repetition of various conversions. Set DELACK for
13604 		 * segments received in order, but ack immediately when
13605 		 * segments are out of order (so fast retransmit can work).
13606 		 */
13607 		if (th->th_seq == tp->rcv_nxt &&
13608 		    SEGQ_EMPTY(tp) &&
13609 		    (TCPS_HAVEESTABLISHED(tp->t_state) ||
13610 		    tfo_syn)) {
13611 #ifdef NETFLIX_SB_LIMITS
13612 			u_int mcnt, appended;
13613 
13614 			if (so->so_rcv.sb_shlim) {
13615 				mcnt = m_memcnt(m);
13616 				appended = 0;
13617 				if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
13618 				    CFO_NOSLEEP, NULL) == false) {
13619 					counter_u64_add(tcp_sb_shlim_fails, 1);
13620 					m_freem(m);
13621 					return (0);
13622 				}
13623 			}
13624 #endif
13625 			rack_handle_delayed_ack(tp, rack, tlen, tfo_syn);
13626 			tp->rcv_nxt += tlen;
13627 			if (tlen &&
13628 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
13629 			    (tp->t_fbyte_in == 0)) {
13630 				tp->t_fbyte_in = ticks;
13631 				if (tp->t_fbyte_in == 0)
13632 					tp->t_fbyte_in = 1;
13633 				if (tp->t_fbyte_out && tp->t_fbyte_in)
13634 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
13635 			}
13636 			thflags = tcp_get_flags(th) & TH_FIN;
13637 			KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs);
13638 			KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
13639 			SOCKBUF_LOCK(&so->so_rcv);
13640 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
13641 				m_freem(m);
13642 			} else {
13643 				int32_t newsize;
13644 
13645 				if (tlen > 0) {
13646 					newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
13647 					if (newsize)
13648 						if (!sbreserve_locked(so, SO_RCV, newsize, NULL))
13649 							so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
13650 				}
13651 #ifdef NETFLIX_SB_LIMITS
13652 				appended =
13653 #endif
13654 					sbappendstream_locked(&so->so_rcv, m, 0);
13655 			}
13656 			rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1);
13657 			/* NB: sorwakeup_locked() does an implicit unlock. */
13658 			sorwakeup_locked(so);
13659 #ifdef NETFLIX_SB_LIMITS
13660 			if (so->so_rcv.sb_shlim && appended != mcnt)
13661 				counter_fo_release(so->so_rcv.sb_shlim,
13662 				    mcnt - appended);
13663 #endif
13664 		} else {
13665 			/*
13666 			 * XXX: Due to the header drop above "th" is
13667 			 * theoretically invalid by now.  Fortunately
13668 			 * m_adj() doesn't actually frees any mbufs when
13669 			 * trimming from the head.
13670 			 */
13671 			tcp_seq temp = save_start;
13672 
13673 			thflags = tcp_reass(tp, th, &temp, &tlen, m);
13674 			tp->t_flags |= TF_ACKNOW;
13675 			if (tp->t_flags & TF_WAKESOR) {
13676 				tp->t_flags &= ~TF_WAKESOR;
13677 				/* NB: sorwakeup_locked() does an implicit unlock. */
13678 				sorwakeup_locked(so);
13679 			}
13680 		}
13681 		if ((tp->t_flags & TF_SACK_PERMIT) &&
13682 		    (save_tlen > 0) &&
13683 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
13684 			if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
13685 				/*
13686 				 * DSACK actually handled in the fastpath
13687 				 * above.
13688 				 */
13689 				tcp_update_sack_list(tp, save_start,
13690 				    save_start + save_tlen);
13691 			} else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
13692 				if ((tp->rcv_numsacks >= 1) &&
13693 				    (tp->sackblks[0].end == save_start)) {
13694 					/*
13695 					 * Partial overlap, recorded at todrop
13696 					 * above.
13697 					 */
13698 					tcp_update_sack_list(tp,
13699 					    tp->sackblks[0].start,
13700 					    tp->sackblks[0].end);
13701 				} else {
13702 					tcp_update_dsack_list(tp, save_start,
13703 					    save_start + save_tlen);
13704 				}
13705 			} else if (tlen >= save_tlen) {
13706 				/* Update of sackblks. */
13707 				tcp_update_dsack_list(tp, save_start,
13708 				    save_start + save_tlen);
13709 			} else if (tlen > 0) {
13710 				tcp_update_dsack_list(tp, save_start,
13711 				    save_start + tlen);
13712 			}
13713 		}
13714 	} else {
13715 		m_freem(m);
13716 		thflags &= ~TH_FIN;
13717 	}
13718 
13719 	/*
13720 	 * If FIN is received ACK the FIN and let the user know that the
13721 	 * connection is closing.
13722 	 */
13723 	if (thflags & TH_FIN) {
13724 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
13725 			/* The socket upcall is handled by socantrcvmore. */
13726 			socantrcvmore(so);
13727 			/*
13728 			 * If connection is half-synchronized (ie NEEDSYN
13729 			 * flag on) then delay ACK, so it may be piggybacked
13730 			 * when SYN is sent. Otherwise, since we received a
13731 			 * FIN then no more input can be expected, send ACK
13732 			 * now.
13733 			 */
13734 			if (tp->t_flags & TF_NEEDSYN) {
13735 				rack_timer_cancel(tp, rack,
13736 				    rack->r_ctl.rc_rcvtime, __LINE__);
13737 				tp->t_flags |= TF_DELACK;
13738 			} else {
13739 				tp->t_flags |= TF_ACKNOW;
13740 			}
13741 			tp->rcv_nxt++;
13742 		}
13743 		switch (tp->t_state) {
13744 			/*
13745 			 * In SYN_RECEIVED and ESTABLISHED STATES enter the
13746 			 * CLOSE_WAIT state.
13747 			 */
13748 		case TCPS_SYN_RECEIVED:
13749 			tp->t_starttime = ticks;
13750 			/* FALLTHROUGH */
13751 		case TCPS_ESTABLISHED:
13752 			rack_timer_cancel(tp, rack,
13753 			    rack->r_ctl.rc_rcvtime, __LINE__);
13754 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
13755 			break;
13756 
13757 			/*
13758 			 * If still in FIN_WAIT_1 STATE FIN has not been
13759 			 * acked so enter the CLOSING state.
13760 			 */
13761 		case TCPS_FIN_WAIT_1:
13762 			rack_timer_cancel(tp, rack,
13763 			    rack->r_ctl.rc_rcvtime, __LINE__);
13764 			tcp_state_change(tp, TCPS_CLOSING);
13765 			break;
13766 
13767 			/*
13768 			 * In FIN_WAIT_2 state enter the TIME_WAIT state,
13769 			 * starting the time-wait timer, turning off the
13770 			 * other standard timers.
13771 			 */
13772 		case TCPS_FIN_WAIT_2:
13773 			rack_timer_cancel(tp, rack,
13774 			    rack->r_ctl.rc_rcvtime, __LINE__);
13775 			tcp_twstart(tp);
13776 			return (1);
13777 		}
13778 	}
13779 	/*
13780 	 * Return any desired output.
13781 	 */
13782 	if ((tp->t_flags & TF_ACKNOW) ||
13783 	    (sbavail(&so->so_snd) > (tp->snd_max - tp->snd_una))) {
13784 		rack->r_wanted_output = 1;
13785 	}
13786 	return (0);
13787 }
13788 
13789 /*
13790  * Here nothing is really faster, its just that we
13791  * have broken out the fast-data path also just like
13792  * the fast-ack.
13793  */
13794 static int
13795 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so,
13796     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13797     uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos)
13798 {
13799 	int32_t nsegs;
13800 	int32_t newsize = 0;	/* automatic sockbuf scaling */
13801 	struct tcp_rack *rack;
13802 #ifdef NETFLIX_SB_LIMITS
13803 	u_int mcnt, appended;
13804 #endif
13805 
13806 	/*
13807 	 * If last ACK falls within this segment's sequence numbers, record
13808 	 * the timestamp. NOTE that the test is modified according to the
13809 	 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
13810 	 */
13811 	if (__predict_false(th->th_seq != tp->rcv_nxt)) {
13812 		return (0);
13813 	}
13814 	if (tiwin && tiwin != tp->snd_wnd) {
13815 		return (0);
13816 	}
13817 	if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) {
13818 		return (0);
13819 	}
13820 	if (__predict_false((to->to_flags & TOF_TS) &&
13821 	    (TSTMP_LT(to->to_tsval, tp->ts_recent)))) {
13822 		return (0);
13823 	}
13824 	if (__predict_false((th->th_ack != tp->snd_una))) {
13825 		return (0);
13826 	}
13827 	if (__predict_false(tlen > sbspace(&so->so_rcv))) {
13828 		return (0);
13829 	}
13830 	if ((to->to_flags & TOF_TS) != 0 &&
13831 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
13832 		tp->ts_recent_age = tcp_ts_getticks();
13833 		tp->ts_recent = to->to_tsval;
13834 	}
13835 	rack = (struct tcp_rack *)tp->t_fb_ptr;
13836 	/*
13837 	 * This is a pure, in-sequence data packet with nothing on the
13838 	 * reassembly queue and we have enough buffer space to take it.
13839 	 */
13840 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
13841 
13842 #ifdef NETFLIX_SB_LIMITS
13843 	if (so->so_rcv.sb_shlim) {
13844 		mcnt = m_memcnt(m);
13845 		appended = 0;
13846 		if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
13847 		    CFO_NOSLEEP, NULL) == false) {
13848 			counter_u64_add(tcp_sb_shlim_fails, 1);
13849 			m_freem(m);
13850 			return (1);
13851 		}
13852 	}
13853 #endif
13854 	/* Clean receiver SACK report if present */
13855 	if (tp->rcv_numsacks)
13856 		tcp_clean_sackreport(tp);
13857 	KMOD_TCPSTAT_INC(tcps_preddat);
13858 	tp->rcv_nxt += tlen;
13859 	if (tlen &&
13860 	    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
13861 	    (tp->t_fbyte_in == 0)) {
13862 		tp->t_fbyte_in = ticks;
13863 		if (tp->t_fbyte_in == 0)
13864 			tp->t_fbyte_in = 1;
13865 		if (tp->t_fbyte_out && tp->t_fbyte_in)
13866 			tp->t_flags2 |= TF2_FBYTES_COMPLETE;
13867 	}
13868 	/*
13869 	 * Pull snd_wl1 up to prevent seq wrap relative to th_seq.
13870 	 */
13871 	tp->snd_wl1 = th->th_seq;
13872 	/*
13873 	 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt.
13874 	 */
13875 	tp->rcv_up = tp->rcv_nxt;
13876 	KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs);
13877 	KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
13878 	newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
13879 
13880 	/* Add data to socket buffer. */
13881 	SOCKBUF_LOCK(&so->so_rcv);
13882 	if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
13883 		m_freem(m);
13884 	} else {
13885 		/*
13886 		 * Set new socket buffer size. Give up when limit is
13887 		 * reached.
13888 		 */
13889 		if (newsize)
13890 			if (!sbreserve_locked(so, SO_RCV, newsize, NULL))
13891 				so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
13892 		m_adj(m, drop_hdrlen);	/* delayed header drop */
13893 #ifdef NETFLIX_SB_LIMITS
13894 		appended =
13895 #endif
13896 			sbappendstream_locked(&so->so_rcv, m, 0);
13897 		ctf_calc_rwin(so, tp);
13898 	}
13899 	rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1);
13900 	/* NB: sorwakeup_locked() does an implicit unlock. */
13901 	sorwakeup_locked(so);
13902 #ifdef NETFLIX_SB_LIMITS
13903 	if (so->so_rcv.sb_shlim && mcnt != appended)
13904 		counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended);
13905 #endif
13906 	rack_handle_delayed_ack(tp, rack, tlen, 0);
13907 	if (tp->snd_una == tp->snd_max)
13908 		sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
13909 	return (1);
13910 }
13911 
13912 /*
13913  * This subfunction is used to try to highly optimize the
13914  * fast path. We again allow window updates that are
13915  * in sequence to remain in the fast-path. We also add
13916  * in the __predict's to attempt to help the compiler.
13917  * Note that if we return a 0, then we can *not* process
13918  * it and the caller should push the packet into the
13919  * slow-path.
13920  */
13921 static int
13922 rack_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
13923     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13924     uint32_t tiwin, int32_t nxt_pkt, uint32_t cts)
13925 {
13926 	int32_t acked;
13927 	int32_t nsegs;
13928 	int32_t under_pacing = 0;
13929 	struct tcp_rack *rack;
13930 
13931 	if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
13932 		/* Old ack, behind (or duplicate to) the last one rcv'd */
13933 		return (0);
13934 	}
13935 	if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) {
13936 		/* Above what we have sent? */
13937 		return (0);
13938 	}
13939 	if (__predict_false(tiwin == 0)) {
13940 		/* zero window */
13941 		return (0);
13942 	}
13943 	if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) {
13944 		/* We need a SYN or a FIN, unlikely.. */
13945 		return (0);
13946 	}
13947 	if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) {
13948 		/* Timestamp is behind .. old ack with seq wrap? */
13949 		return (0);
13950 	}
13951 	if (__predict_false(IN_RECOVERY(tp->t_flags))) {
13952 		/* Still recovering */
13953 		return (0);
13954 	}
13955 	rack = (struct tcp_rack *)tp->t_fb_ptr;
13956 	if (rack->r_ctl.rc_sacked) {
13957 		/* We have sack holes on our scoreboard */
13958 		return (0);
13959 	}
13960 	/* Ok if we reach here, we can process a fast-ack */
13961 	if (rack->gp_ready &&
13962 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
13963 		under_pacing = 1;
13964 	}
13965 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
13966 	rack_log_ack(tp, to, th, 0, 0, NULL, NULL);
13967 	/* Did the window get updated? */
13968 	if (tiwin != tp->snd_wnd) {
13969 		tp->snd_wnd = tiwin;
13970 		rack_validate_fo_sendwin_up(tp, rack);
13971 		tp->snd_wl1 = th->th_seq;
13972 		if (tp->snd_wnd > tp->max_sndwnd)
13973 			tp->max_sndwnd = tp->snd_wnd;
13974 	}
13975 	/* Do we exit persists? */
13976 	if ((rack->rc_in_persist != 0) &&
13977 	    (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
13978 			       rack->r_ctl.rc_pace_min_segs))) {
13979 		rack_exit_persist(tp, rack, cts);
13980 	}
13981 	/* Do we enter persists? */
13982 	if ((rack->rc_in_persist == 0) &&
13983 	    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
13984 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
13985 	    ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) &&
13986 	    sbavail(&tptosocket(tp)->so_snd) &&
13987 	    (sbavail(&tptosocket(tp)->so_snd) > tp->snd_wnd)) {
13988 		/*
13989 		 * Here the rwnd is less than
13990 		 * the pacing size, we are established,
13991 		 * nothing is outstanding, and there is
13992 		 * data to send. Enter persists.
13993 		 */
13994 		rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, th->th_ack);
13995 	}
13996 	/*
13997 	 * If last ACK falls within this segment's sequence numbers, record
13998 	 * the timestamp. NOTE that the test is modified according to the
13999 	 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
14000 	 */
14001 	if ((to->to_flags & TOF_TS) != 0 &&
14002 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
14003 		tp->ts_recent_age = tcp_ts_getticks();
14004 		tp->ts_recent = to->to_tsval;
14005 	}
14006 	/*
14007 	 * This is a pure ack for outstanding data.
14008 	 */
14009 	KMOD_TCPSTAT_INC(tcps_predack);
14010 
14011 	/*
14012 	 * "bad retransmit" recovery.
14013 	 */
14014 	if ((tp->t_flags & TF_PREVVALID) &&
14015 	    ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
14016 		tp->t_flags &= ~TF_PREVVALID;
14017 		if (tp->t_rxtshift == 1 &&
14018 		    (int)(ticks - tp->t_badrxtwin) < 0)
14019 			rack_cong_signal(tp, CC_RTO_ERR, th->th_ack, __LINE__);
14020 	}
14021 	/*
14022 	 * Recalculate the transmit timer / rtt.
14023 	 *
14024 	 * Some boxes send broken timestamp replies during the SYN+ACK
14025 	 * phase, ignore timestamps of 0 or we could calculate a huge RTT
14026 	 * and blow up the retransmit timer.
14027 	 */
14028 	acked = BYTES_THIS_ACK(tp, th);
14029 
14030 #ifdef TCP_HHOOK
14031 	/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
14032 	hhook_run_tcp_est_in(tp, th, to);
14033 #endif
14034 	KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
14035 	KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
14036 	if (acked) {
14037 		struct mbuf *mfree;
14038 
14039 		rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, 0);
14040 		SOCKBUF_LOCK(&so->so_snd);
14041 		mfree = sbcut_locked(&so->so_snd, acked);
14042 		tp->snd_una = th->th_ack;
14043 		/* Note we want to hold the sb lock through the sendmap adjust */
14044 		rack_adjust_sendmap_head(rack, &so->so_snd);
14045 		/* Wake up the socket if we have room to write more */
14046 		rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
14047 		sowwakeup_locked(so);
14048 		m_freem(mfree);
14049 		tp->t_rxtshift = 0;
14050 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
14051 			      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
14052 		rack->rc_tlp_in_progress = 0;
14053 		rack->r_ctl.rc_tlp_cnt_out = 0;
14054 		/*
14055 		 * If it is the RXT timer we want to
14056 		 * stop it, so we can restart a TLP.
14057 		 */
14058 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
14059 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
14060 
14061 #ifdef TCP_REQUEST_TRK
14062 		rack_req_check_for_comp(rack, th->th_ack);
14063 #endif
14064 	}
14065 	/*
14066 	 * Let the congestion control algorithm update congestion control
14067 	 * related information. This typically means increasing the
14068 	 * congestion window.
14069 	 */
14070 	if (tp->snd_wnd < ctf_outstanding(tp)) {
14071 		/* The peer collapsed the window */
14072 		rack_collapsed_window(rack, ctf_outstanding(tp), th->th_ack, __LINE__);
14073 	} else if (rack->rc_has_collapsed)
14074 		rack_un_collapse_window(rack, __LINE__);
14075 	if ((rack->r_collapse_point_valid) &&
14076 	    (SEQ_GT(tp->snd_una, rack->r_ctl.high_collapse_point)))
14077 		rack->r_collapse_point_valid = 0;
14078 	/*
14079 	 * Pull snd_wl2 up to prevent seq wrap relative to th_ack.
14080 	 */
14081 	tp->snd_wl2 = th->th_ack;
14082 	tp->t_dupacks = 0;
14083 	m_freem(m);
14084 	/* ND6_HINT(tp);	 *//* Some progress has been made. */
14085 
14086 	/*
14087 	 * If all outstanding data are acked, stop retransmit timer,
14088 	 * otherwise restart timer using current (possibly backed-off)
14089 	 * value. If process is waiting for space, wakeup/selwakeup/signal.
14090 	 * If data are ready to send, let tcp_output decide between more
14091 	 * output or persist.
14092 	 */
14093 	if (under_pacing &&
14094 	    (rack->use_fixed_rate == 0) &&
14095 	    (rack->in_probe_rtt == 0) &&
14096 	    rack->rc_gp_dyn_mul &&
14097 	    rack->rc_always_pace) {
14098 		/* Check if we are dragging bottom */
14099 		rack_check_bottom_drag(tp, rack, so);
14100 	}
14101 	if (tp->snd_una == tp->snd_max) {
14102 		tp->t_flags &= ~TF_PREVVALID;
14103 		rack->r_ctl.retran_during_recovery = 0;
14104 		rack->rc_suspicious = 0;
14105 		rack->r_ctl.dsack_byte_cnt = 0;
14106 		rack->r_ctl.idle_snd_una = tp->snd_una;
14107 		rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
14108 		if (rack->r_ctl.rc_went_idle_time == 0)
14109 			rack->r_ctl.rc_went_idle_time = 1;
14110 		rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
14111 		if (sbavail(&tptosocket(tp)->so_snd) == 0)
14112 			tp->t_acktime = 0;
14113 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
14114 	}
14115 	if (acked && rack->r_fast_output)
14116 		rack_gain_for_fastoutput(rack, tp, so, (uint32_t)acked);
14117 	if (sbavail(&so->so_snd)) {
14118 		rack->r_wanted_output = 1;
14119 	}
14120 	return (1);
14121 }
14122 
14123 /*
14124  * Return value of 1, the TCB is unlocked and most
14125  * likely gone, return value of 0, the TCP is still
14126  * locked.
14127  */
14128 static int
14129 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so,
14130     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
14131     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
14132 {
14133 	int32_t ret_val = 0;
14134 	int32_t orig_tlen = tlen;
14135 	int32_t todrop;
14136 	int32_t ourfinisacked = 0;
14137 	struct tcp_rack *rack;
14138 
14139 	INP_WLOCK_ASSERT(tptoinpcb(tp));
14140 
14141 	ctf_calc_rwin(so, tp);
14142 	/*
14143 	 * If the state is SYN_SENT: if seg contains an ACK, but not for our
14144 	 * SYN, drop the input. if seg contains a RST, then drop the
14145 	 * connection. if seg does not contain SYN, then drop it. Otherwise
14146 	 * this is an acceptable SYN segment initialize tp->rcv_nxt and
14147 	 * tp->irs if seg contains ack then advance tp->snd_una if seg
14148 	 * contains an ECE and ECN support is enabled, the stream is ECN
14149 	 * capable. if SYN has been acked change to ESTABLISHED else
14150 	 * SYN_RCVD state arrange for segment to be acked (eventually)
14151 	 * continue processing rest of data/controls.
14152 	 */
14153 	if ((thflags & TH_ACK) &&
14154 	    (SEQ_LEQ(th->th_ack, tp->iss) ||
14155 	    SEQ_GT(th->th_ack, tp->snd_max))) {
14156 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
14157 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
14158 		return (1);
14159 	}
14160 	if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) {
14161 		TCP_PROBE5(connect__refused, NULL, tp,
14162 		    mtod(m, const char *), tp, th);
14163 		tp = tcp_drop(tp, ECONNREFUSED);
14164 		ctf_do_drop(m, tp);
14165 		return (1);
14166 	}
14167 	if (thflags & TH_RST) {
14168 		ctf_do_drop(m, tp);
14169 		return (1);
14170 	}
14171 	if (!(thflags & TH_SYN)) {
14172 		ctf_do_drop(m, tp);
14173 		return (1);
14174 	}
14175 	tp->irs = th->th_seq;
14176 	tcp_rcvseqinit(tp);
14177 	rack = (struct tcp_rack *)tp->t_fb_ptr;
14178 	if (thflags & TH_ACK) {
14179 		int tfo_partial = 0;
14180 
14181 		KMOD_TCPSTAT_INC(tcps_connects);
14182 		soisconnected(so);
14183 #ifdef MAC
14184 		mac_socketpeer_set_from_mbuf(m, so);
14185 #endif
14186 		/* Do window scaling on this connection? */
14187 		if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
14188 		    (TF_RCVD_SCALE | TF_REQ_SCALE)) {
14189 			tp->rcv_scale = tp->request_r_scale;
14190 		}
14191 		tp->rcv_adv += min(tp->rcv_wnd,
14192 		    TCP_MAXWIN << tp->rcv_scale);
14193 		/*
14194 		 * If not all the data that was sent in the TFO SYN
14195 		 * has been acked, resend the remainder right away.
14196 		 */
14197 		if (IS_FASTOPEN(tp->t_flags) &&
14198 		    (tp->snd_una != tp->snd_max)) {
14199 			/* Was it a partial ack? */
14200 			if (SEQ_LT(th->th_ack, tp->snd_max))
14201 				tfo_partial = 1;
14202 		}
14203 		/*
14204 		 * If there's data, delay ACK; if there's also a FIN ACKNOW
14205 		 * will be turned on later.
14206 		 */
14207 		if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial) {
14208 			rack_timer_cancel(tp, rack,
14209 					  rack->r_ctl.rc_rcvtime, __LINE__);
14210 			tp->t_flags |= TF_DELACK;
14211 		} else {
14212 			rack->r_wanted_output = 1;
14213 			tp->t_flags |= TF_ACKNOW;
14214 		}
14215 
14216 		tcp_ecn_input_syn_sent(tp, thflags, iptos);
14217 
14218 		if (SEQ_GT(th->th_ack, tp->snd_una)) {
14219 			/*
14220 			 * We advance snd_una for the
14221 			 * fast open case. If th_ack is
14222 			 * acknowledging data beyond
14223 			 * snd_una we can't just call
14224 			 * ack-processing since the
14225 			 * data stream in our send-map
14226 			 * will start at snd_una + 1 (one
14227 			 * beyond the SYN). If its just
14228 			 * equal we don't need to do that
14229 			 * and there is no send_map.
14230 			 */
14231 			tp->snd_una++;
14232 			if (tfo_partial && (SEQ_GT(tp->snd_max, tp->snd_una))) {
14233 				/*
14234 				 * We sent a SYN with data, and thus have a
14235 				 * sendmap entry with a SYN set. Lets find it
14236 				 * and take off the send bit and the byte and
14237 				 * set it up to be what we send (send it next).
14238 				 */
14239 				struct rack_sendmap *rsm;
14240 
14241 				rsm = tqhash_min(rack->r_ctl.tqh);
14242 				if (rsm) {
14243 					if (rsm->r_flags & RACK_HAS_SYN) {
14244 						rsm->r_flags &= ~RACK_HAS_SYN;
14245 						rsm->r_start++;
14246 					}
14247 					rack->r_ctl.rc_resend = rsm;
14248 				}
14249 			}
14250 		}
14251 		/*
14252 		 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions:
14253 		 * SYN_SENT  --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1
14254 		 */
14255 		tp->t_starttime = ticks;
14256 		if (tp->t_flags & TF_NEEDFIN) {
14257 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
14258 			tp->t_flags &= ~TF_NEEDFIN;
14259 			thflags &= ~TH_SYN;
14260 		} else {
14261 			tcp_state_change(tp, TCPS_ESTABLISHED);
14262 			TCP_PROBE5(connect__established, NULL, tp,
14263 			    mtod(m, const char *), tp, th);
14264 			rack_cc_conn_init(tp);
14265 		}
14266 	} else {
14267 		/*
14268 		 * Received initial SYN in SYN-SENT[*] state => simultaneous
14269 		 * open.  If segment contains CC option and there is a
14270 		 * cached CC, apply TAO test. If it succeeds, connection is *
14271 		 * half-synchronized. Otherwise, do 3-way handshake:
14272 		 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If
14273 		 * there was no CC option, clear cached CC value.
14274 		 */
14275 		tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN);
14276 		tcp_state_change(tp, TCPS_SYN_RECEIVED);
14277 	}
14278 	/*
14279 	 * Advance th->th_seq to correspond to first data byte. If data,
14280 	 * trim to stay within window, dropping FIN if necessary.
14281 	 */
14282 	th->th_seq++;
14283 	if (tlen > tp->rcv_wnd) {
14284 		todrop = tlen - tp->rcv_wnd;
14285 		m_adj(m, -todrop);
14286 		tlen = tp->rcv_wnd;
14287 		thflags &= ~TH_FIN;
14288 		KMOD_TCPSTAT_INC(tcps_rcvpackafterwin);
14289 		KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
14290 	}
14291 	tp->snd_wl1 = th->th_seq - 1;
14292 	tp->rcv_up = th->th_seq;
14293 	/*
14294 	 * Client side of transaction: already sent SYN and data. If the
14295 	 * remote host used T/TCP to validate the SYN, our data will be
14296 	 * ACK'd; if so, enter normal data segment processing in the middle
14297 	 * of step 5, ack processing. Otherwise, goto step 6.
14298 	 */
14299 	if (thflags & TH_ACK) {
14300 		/* For syn-sent we need to possibly update the rtt */
14301 		if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) {
14302 			uint32_t t, mcts;
14303 
14304 			mcts = tcp_ts_getticks();
14305 			t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC;
14306 			if (!tp->t_rttlow || tp->t_rttlow > t)
14307 				tp->t_rttlow = t;
14308 			rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 4);
14309 			tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2);
14310 			tcp_rack_xmit_timer_commit(rack, tp);
14311 		}
14312 		if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen))
14313 			return (ret_val);
14314 		/* We may have changed to FIN_WAIT_1 above */
14315 		if (tp->t_state == TCPS_FIN_WAIT_1) {
14316 			/*
14317 			 * In FIN_WAIT_1 STATE in addition to the processing
14318 			 * for the ESTABLISHED state if our FIN is now
14319 			 * acknowledged then enter FIN_WAIT_2.
14320 			 */
14321 			if (ourfinisacked) {
14322 				/*
14323 				 * If we can't receive any more data, then
14324 				 * closing user can proceed. Starting the
14325 				 * timer is contrary to the specification,
14326 				 * but if we don't get a FIN we'll hang
14327 				 * forever.
14328 				 *
14329 				 * XXXjl: we should release the tp also, and
14330 				 * use a compressed state.
14331 				 */
14332 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
14333 					soisdisconnected(so);
14334 					tcp_timer_activate(tp, TT_2MSL,
14335 					    (tcp_fast_finwait2_recycle ?
14336 					    tcp_finwait2_timeout :
14337 					    TP_MAXIDLE(tp)));
14338 				}
14339 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
14340 			}
14341 		}
14342 	}
14343 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
14344 	   tiwin, thflags, nxt_pkt));
14345 }
14346 
14347 /*
14348  * Return value of 1, the TCB is unlocked and most
14349  * likely gone, return value of 0, the TCP is still
14350  * locked.
14351  */
14352 static int
14353 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so,
14354     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
14355     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
14356 {
14357 	struct tcp_rack *rack;
14358 	int32_t orig_tlen = tlen;
14359 	int32_t ret_val = 0;
14360 	int32_t ourfinisacked = 0;
14361 
14362 	rack = (struct tcp_rack *)tp->t_fb_ptr;
14363 	ctf_calc_rwin(so, tp);
14364 	if ((thflags & TH_RST) ||
14365 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
14366 		return (__ctf_process_rst(m, th, so, tp,
14367 					  &rack->r_ctl.challenge_ack_ts,
14368 					  &rack->r_ctl.challenge_ack_cnt));
14369 	if ((thflags & TH_ACK) &&
14370 	    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
14371 	    SEQ_GT(th->th_ack, tp->snd_max))) {
14372 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
14373 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
14374 		return (1);
14375 	}
14376 	if (IS_FASTOPEN(tp->t_flags)) {
14377 		/*
14378 		 * When a TFO connection is in SYN_RECEIVED, the
14379 		 * only valid packets are the initial SYN, a
14380 		 * retransmit/copy of the initial SYN (possibly with
14381 		 * a subset of the original data), a valid ACK, a
14382 		 * FIN, or a RST.
14383 		 */
14384 		if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) {
14385 			tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
14386 			ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
14387 			return (1);
14388 		} else if (thflags & TH_SYN) {
14389 			/* non-initial SYN is ignored */
14390 			if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) ||
14391 			    (rack->r_ctl.rc_hpts_flags & PACE_TMR_TLP) ||
14392 			    (rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) {
14393 				ctf_do_drop(m, NULL);
14394 				return (0);
14395 			}
14396 		} else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) {
14397 			ctf_do_drop(m, NULL);
14398 			return (0);
14399 		}
14400 	}
14401 
14402 	/*
14403 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
14404 	 * it's less than ts_recent, drop it.
14405 	 */
14406 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
14407 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
14408 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
14409 			return (ret_val);
14410 	}
14411 	/*
14412 	 * In the SYN-RECEIVED state, validate that the packet belongs to
14413 	 * this connection before trimming the data to fit the receive
14414 	 * window.  Check the sequence number versus IRS since we know the
14415 	 * sequence numbers haven't wrapped.  This is a partial fix for the
14416 	 * "LAND" DoS attack.
14417 	 */
14418 	if (SEQ_LT(th->th_seq, tp->irs)) {
14419 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
14420 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
14421 		return (1);
14422 	}
14423 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
14424 			      &rack->r_ctl.challenge_ack_ts,
14425 			      &rack->r_ctl.challenge_ack_cnt)) {
14426 		return (ret_val);
14427 	}
14428 	/*
14429 	 * If last ACK falls within this segment's sequence numbers, record
14430 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
14431 	 * from the latest proposal of the tcplw@cray.com list (Braden
14432 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
14433 	 * with our earlier PAWS tests, so this check should be solely
14434 	 * predicated on the sequence space of this segment. 3) That we
14435 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
14436 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
14437 	 * SEG.Len, This modified check allows us to overcome RFC1323's
14438 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
14439 	 * p.869. In such cases, we can still calculate the RTT correctly
14440 	 * when RCV.NXT == Last.ACK.Sent.
14441 	 */
14442 	if ((to->to_flags & TOF_TS) != 0 &&
14443 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
14444 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
14445 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
14446 		tp->ts_recent_age = tcp_ts_getticks();
14447 		tp->ts_recent = to->to_tsval;
14448 	}
14449 	tp->snd_wnd = tiwin;
14450 	rack_validate_fo_sendwin_up(tp, rack);
14451 	/*
14452 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
14453 	 * is on (half-synchronized state), then queue data for later
14454 	 * processing; else drop segment and return.
14455 	 */
14456 	if ((thflags & TH_ACK) == 0) {
14457 		if (IS_FASTOPEN(tp->t_flags)) {
14458 			rack_cc_conn_init(tp);
14459 		}
14460 		return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
14461 		    tiwin, thflags, nxt_pkt));
14462 	}
14463 	KMOD_TCPSTAT_INC(tcps_connects);
14464 	if (tp->t_flags & TF_SONOTCONN) {
14465 		tp->t_flags &= ~TF_SONOTCONN;
14466 		soisconnected(so);
14467 	}
14468 	/* Do window scaling? */
14469 	if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
14470 	    (TF_RCVD_SCALE | TF_REQ_SCALE)) {
14471 		tp->rcv_scale = tp->request_r_scale;
14472 	}
14473 	/*
14474 	 * Make transitions: SYN-RECEIVED  -> ESTABLISHED SYN-RECEIVED* ->
14475 	 * FIN-WAIT-1
14476 	 */
14477 	tp->t_starttime = ticks;
14478 	if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
14479 		tcp_fastopen_decrement_counter(tp->t_tfo_pending);
14480 		tp->t_tfo_pending = NULL;
14481 	}
14482 	if (tp->t_flags & TF_NEEDFIN) {
14483 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
14484 		tp->t_flags &= ~TF_NEEDFIN;
14485 	} else {
14486 		tcp_state_change(tp, TCPS_ESTABLISHED);
14487 		TCP_PROBE5(accept__established, NULL, tp,
14488 		    mtod(m, const char *), tp, th);
14489 		/*
14490 		 * TFO connections call cc_conn_init() during SYN
14491 		 * processing.  Calling it again here for such connections
14492 		 * is not harmless as it would undo the snd_cwnd reduction
14493 		 * that occurs when a TFO SYN|ACK is retransmitted.
14494 		 */
14495 		if (!IS_FASTOPEN(tp->t_flags))
14496 			rack_cc_conn_init(tp);
14497 	}
14498 	/*
14499 	 * Account for the ACK of our SYN prior to
14500 	 * regular ACK processing below, except for
14501 	 * simultaneous SYN, which is handled later.
14502 	 */
14503 	if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
14504 		tp->snd_una++;
14505 	/*
14506 	 * If segment contains data or ACK, will call tcp_reass() later; if
14507 	 * not, do so now to pass queued data to user.
14508 	 */
14509 	if (tlen == 0 && (thflags & TH_FIN) == 0) {
14510 		(void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
14511 		    (struct mbuf *)0);
14512 		if (tp->t_flags & TF_WAKESOR) {
14513 			tp->t_flags &= ~TF_WAKESOR;
14514 			/* NB: sorwakeup_locked() does an implicit unlock. */
14515 			sorwakeup_locked(so);
14516 		}
14517 	}
14518 	tp->snd_wl1 = th->th_seq - 1;
14519 	/* For syn-recv we need to possibly update the rtt */
14520 	if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) {
14521 		uint32_t t, mcts;
14522 
14523 		mcts = tcp_ts_getticks();
14524 		t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC;
14525 		if (!tp->t_rttlow || tp->t_rttlow > t)
14526 			tp->t_rttlow = t;
14527 		rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 5);
14528 		tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2);
14529 		tcp_rack_xmit_timer_commit(rack, tp);
14530 	}
14531 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) {
14532 		return (ret_val);
14533 	}
14534 	if (tp->t_state == TCPS_FIN_WAIT_1) {
14535 		/* We could have went to FIN_WAIT_1 (or EST) above */
14536 		/*
14537 		 * In FIN_WAIT_1 STATE in addition to the processing for the
14538 		 * ESTABLISHED state if our FIN is now acknowledged then
14539 		 * enter FIN_WAIT_2.
14540 		 */
14541 		if (ourfinisacked) {
14542 			/*
14543 			 * If we can't receive any more data, then closing
14544 			 * user can proceed. Starting the timer is contrary
14545 			 * to the specification, but if we don't get a FIN
14546 			 * we'll hang forever.
14547 			 *
14548 			 * XXXjl: we should release the tp also, and use a
14549 			 * compressed state.
14550 			 */
14551 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
14552 				soisdisconnected(so);
14553 				tcp_timer_activate(tp, TT_2MSL,
14554 				    (tcp_fast_finwait2_recycle ?
14555 				    tcp_finwait2_timeout :
14556 				    TP_MAXIDLE(tp)));
14557 			}
14558 			tcp_state_change(tp, TCPS_FIN_WAIT_2);
14559 		}
14560 	}
14561 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
14562 	    tiwin, thflags, nxt_pkt));
14563 }
14564 
14565 /*
14566  * Return value of 1, the TCB is unlocked and most
14567  * likely gone, return value of 0, the TCP is still
14568  * locked.
14569  */
14570 static int
14571 rack_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so,
14572     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
14573     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
14574 {
14575 	int32_t ret_val = 0;
14576 	int32_t orig_tlen = tlen;
14577 	struct tcp_rack *rack;
14578 
14579 	/*
14580 	 * Header prediction: check for the two common cases of a
14581 	 * uni-directional data xfer.  If the packet has no control flags,
14582 	 * is in-sequence, the window didn't change and we're not
14583 	 * retransmitting, it's a candidate.  If the length is zero and the
14584 	 * ack moved forward, we're the sender side of the xfer.  Just free
14585 	 * the data acked & wake any higher level process that was blocked
14586 	 * waiting for space.  If the length is non-zero and the ack didn't
14587 	 * move, we're the receiver side.  If we're getting packets in-order
14588 	 * (the reassembly queue is empty), add the data toc The socket
14589 	 * buffer and note that we need a delayed ack. Make sure that the
14590 	 * hidden state-flags are also off. Since we check for
14591 	 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN.
14592 	 */
14593 	rack = (struct tcp_rack *)tp->t_fb_ptr;
14594 	if (__predict_true(((to->to_flags & TOF_SACK) == 0)) &&
14595 	    __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) == TH_ACK) &&
14596 	    __predict_true(SEGQ_EMPTY(tp)) &&
14597 	    __predict_true(th->th_seq == tp->rcv_nxt)) {
14598 		if (tlen == 0) {
14599 			if (rack_fastack(m, th, so, tp, to, drop_hdrlen, tlen,
14600 			    tiwin, nxt_pkt, rack->r_ctl.rc_rcvtime)) {
14601 				return (0);
14602 			}
14603 		} else {
14604 			if (rack_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen,
14605 			    tiwin, nxt_pkt, iptos)) {
14606 				return (0);
14607 			}
14608 		}
14609 	}
14610 	ctf_calc_rwin(so, tp);
14611 
14612 	if ((thflags & TH_RST) ||
14613 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
14614 		return (__ctf_process_rst(m, th, so, tp,
14615 					  &rack->r_ctl.challenge_ack_ts,
14616 					  &rack->r_ctl.challenge_ack_cnt));
14617 
14618 	/*
14619 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
14620 	 * synchronized state.
14621 	 */
14622 	if (thflags & TH_SYN) {
14623 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
14624 		return (ret_val);
14625 	}
14626 	/*
14627 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
14628 	 * it's less than ts_recent, drop it.
14629 	 */
14630 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
14631 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
14632 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
14633 			return (ret_val);
14634 	}
14635 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
14636 			      &rack->r_ctl.challenge_ack_ts,
14637 			      &rack->r_ctl.challenge_ack_cnt)) {
14638 		return (ret_val);
14639 	}
14640 	/*
14641 	 * If last ACK falls within this segment's sequence numbers, record
14642 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
14643 	 * from the latest proposal of the tcplw@cray.com list (Braden
14644 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
14645 	 * with our earlier PAWS tests, so this check should be solely
14646 	 * predicated on the sequence space of this segment. 3) That we
14647 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
14648 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
14649 	 * SEG.Len, This modified check allows us to overcome RFC1323's
14650 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
14651 	 * p.869. In such cases, we can still calculate the RTT correctly
14652 	 * when RCV.NXT == Last.ACK.Sent.
14653 	 */
14654 	if ((to->to_flags & TOF_TS) != 0 &&
14655 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
14656 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
14657 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
14658 		tp->ts_recent_age = tcp_ts_getticks();
14659 		tp->ts_recent = to->to_tsval;
14660 	}
14661 	/*
14662 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
14663 	 * is on (half-synchronized state), then queue data for later
14664 	 * processing; else drop segment and return.
14665 	 */
14666 	if ((thflags & TH_ACK) == 0) {
14667 		if (tp->t_flags & TF_NEEDSYN) {
14668 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
14669 			    tiwin, thflags, nxt_pkt));
14670 
14671 		} else if (tp->t_flags & TF_ACKNOW) {
14672 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
14673 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
14674 			return (ret_val);
14675 		} else {
14676 			ctf_do_drop(m, NULL);
14677 			return (0);
14678 		}
14679 	}
14680 	/*
14681 	 * Ack processing.
14682 	 */
14683 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val, orig_tlen)) {
14684 		return (ret_val);
14685 	}
14686 	if (sbavail(&so->so_snd)) {
14687 		if (ctf_progress_timeout_check(tp, true)) {
14688 			rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
14689 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
14690 			return (1);
14691 		}
14692 	}
14693 	/* State changes only happen in rack_process_data() */
14694 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
14695 	    tiwin, thflags, nxt_pkt));
14696 }
14697 
14698 /*
14699  * Return value of 1, the TCB is unlocked and most
14700  * likely gone, return value of 0, the TCP is still
14701  * locked.
14702  */
14703 static int
14704 rack_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so,
14705     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
14706     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
14707 {
14708 	int32_t ret_val = 0;
14709 	int32_t orig_tlen = tlen;
14710 	struct tcp_rack *rack;
14711 
14712 	rack = (struct tcp_rack *)tp->t_fb_ptr;
14713 	ctf_calc_rwin(so, tp);
14714 	if ((thflags & TH_RST) ||
14715 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
14716 		return (__ctf_process_rst(m, th, so, tp,
14717 					  &rack->r_ctl.challenge_ack_ts,
14718 					  &rack->r_ctl.challenge_ack_cnt));
14719 	/*
14720 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
14721 	 * synchronized state.
14722 	 */
14723 	if (thflags & TH_SYN) {
14724 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
14725 		return (ret_val);
14726 	}
14727 	/*
14728 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
14729 	 * it's less than ts_recent, drop it.
14730 	 */
14731 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
14732 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
14733 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
14734 			return (ret_val);
14735 	}
14736 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
14737 			      &rack->r_ctl.challenge_ack_ts,
14738 			      &rack->r_ctl.challenge_ack_cnt)) {
14739 		return (ret_val);
14740 	}
14741 	/*
14742 	 * If last ACK falls within this segment's sequence numbers, record
14743 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
14744 	 * from the latest proposal of the tcplw@cray.com list (Braden
14745 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
14746 	 * with our earlier PAWS tests, so this check should be solely
14747 	 * predicated on the sequence space of this segment. 3) That we
14748 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
14749 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
14750 	 * SEG.Len, This modified check allows us to overcome RFC1323's
14751 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
14752 	 * p.869. In such cases, we can still calculate the RTT correctly
14753 	 * when RCV.NXT == Last.ACK.Sent.
14754 	 */
14755 	if ((to->to_flags & TOF_TS) != 0 &&
14756 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
14757 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
14758 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
14759 		tp->ts_recent_age = tcp_ts_getticks();
14760 		tp->ts_recent = to->to_tsval;
14761 	}
14762 	/*
14763 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
14764 	 * is on (half-synchronized state), then queue data for later
14765 	 * processing; else drop segment and return.
14766 	 */
14767 	if ((thflags & TH_ACK) == 0) {
14768 		if (tp->t_flags & TF_NEEDSYN) {
14769 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
14770 			    tiwin, thflags, nxt_pkt));
14771 
14772 		} else if (tp->t_flags & TF_ACKNOW) {
14773 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
14774 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
14775 			return (ret_val);
14776 		} else {
14777 			ctf_do_drop(m, NULL);
14778 			return (0);
14779 		}
14780 	}
14781 	/*
14782 	 * Ack processing.
14783 	 */
14784 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val, orig_tlen)) {
14785 		return (ret_val);
14786 	}
14787 	if (sbavail(&so->so_snd)) {
14788 		if (ctf_progress_timeout_check(tp, true)) {
14789 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
14790 						tp, tick, PROGRESS_DROP, __LINE__);
14791 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
14792 			return (1);
14793 		}
14794 	}
14795 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
14796 	    tiwin, thflags, nxt_pkt));
14797 }
14798 
14799 static int
14800 rack_check_data_after_close(struct mbuf *m,
14801     struct tcpcb *tp, int32_t *tlen, struct tcphdr *th, struct socket *so)
14802 {
14803 	struct tcp_rack *rack;
14804 
14805 	rack = (struct tcp_rack *)tp->t_fb_ptr;
14806 	if (rack->rc_allow_data_af_clo == 0) {
14807 	close_now:
14808 		tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
14809 		/* tcp_close will kill the inp pre-log the Reset */
14810 		tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
14811 		tp = tcp_close(tp);
14812 		KMOD_TCPSTAT_INC(tcps_rcvafterclose);
14813 		ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen));
14814 		return (1);
14815 	}
14816 	if (sbavail(&so->so_snd) == 0)
14817 		goto close_now;
14818 	/* Ok we allow data that is ignored and a followup reset */
14819 	tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
14820 	tp->rcv_nxt = th->th_seq + *tlen;
14821 	tp->t_flags2 |= TF2_DROP_AF_DATA;
14822 	rack->r_wanted_output = 1;
14823 	*tlen = 0;
14824 	return (0);
14825 }
14826 
14827 /*
14828  * Return value of 1, the TCB is unlocked and most
14829  * likely gone, return value of 0, the TCP is still
14830  * locked.
14831  */
14832 static int
14833 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so,
14834     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
14835     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
14836 {
14837 	int32_t ret_val = 0;
14838 	int32_t orig_tlen = tlen;
14839 	int32_t ourfinisacked = 0;
14840 	struct tcp_rack *rack;
14841 
14842 	rack = (struct tcp_rack *)tp->t_fb_ptr;
14843 	ctf_calc_rwin(so, tp);
14844 
14845 	if ((thflags & TH_RST) ||
14846 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
14847 		return (__ctf_process_rst(m, th, so, tp,
14848 					  &rack->r_ctl.challenge_ack_ts,
14849 					  &rack->r_ctl.challenge_ack_cnt));
14850 	/*
14851 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
14852 	 * synchronized state.
14853 	 */
14854 	if (thflags & TH_SYN) {
14855 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
14856 		return (ret_val);
14857 	}
14858 	/*
14859 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
14860 	 * it's less than ts_recent, drop it.
14861 	 */
14862 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
14863 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
14864 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
14865 			return (ret_val);
14866 	}
14867 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
14868 			      &rack->r_ctl.challenge_ack_ts,
14869 			      &rack->r_ctl.challenge_ack_cnt)) {
14870 		return (ret_val);
14871 	}
14872 	/*
14873 	 * If new data are received on a connection after the user processes
14874 	 * are gone, then RST the other end.
14875 	 */
14876 	if ((tp->t_flags & TF_CLOSED) && tlen &&
14877 	    rack_check_data_after_close(m, tp, &tlen, th, so))
14878 		return (1);
14879 	/*
14880 	 * If last ACK falls within this segment's sequence numbers, record
14881 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
14882 	 * from the latest proposal of the tcplw@cray.com list (Braden
14883 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
14884 	 * with our earlier PAWS tests, so this check should be solely
14885 	 * predicated on the sequence space of this segment. 3) That we
14886 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
14887 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
14888 	 * SEG.Len, This modified check allows us to overcome RFC1323's
14889 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
14890 	 * p.869. In such cases, we can still calculate the RTT correctly
14891 	 * when RCV.NXT == Last.ACK.Sent.
14892 	 */
14893 	if ((to->to_flags & TOF_TS) != 0 &&
14894 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
14895 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
14896 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
14897 		tp->ts_recent_age = tcp_ts_getticks();
14898 		tp->ts_recent = to->to_tsval;
14899 	}
14900 	/*
14901 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
14902 	 * is on (half-synchronized state), then queue data for later
14903 	 * processing; else drop segment and return.
14904 	 */
14905 	if ((thflags & TH_ACK) == 0) {
14906 		if (tp->t_flags & TF_NEEDSYN) {
14907 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
14908 			    tiwin, thflags, nxt_pkt));
14909 		} else if (tp->t_flags & TF_ACKNOW) {
14910 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
14911 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
14912 			return (ret_val);
14913 		} else {
14914 			ctf_do_drop(m, NULL);
14915 			return (0);
14916 		}
14917 	}
14918 	/*
14919 	 * Ack processing.
14920 	 */
14921 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) {
14922 		return (ret_val);
14923 	}
14924 	if (ourfinisacked) {
14925 		/*
14926 		 * If we can't receive any more data, then closing user can
14927 		 * proceed. Starting the timer is contrary to the
14928 		 * specification, but if we don't get a FIN we'll hang
14929 		 * forever.
14930 		 *
14931 		 * XXXjl: we should release the tp also, and use a
14932 		 * compressed state.
14933 		 */
14934 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
14935 			soisdisconnected(so);
14936 			tcp_timer_activate(tp, TT_2MSL,
14937 			    (tcp_fast_finwait2_recycle ?
14938 			    tcp_finwait2_timeout :
14939 			    TP_MAXIDLE(tp)));
14940 		}
14941 		tcp_state_change(tp, TCPS_FIN_WAIT_2);
14942 	}
14943 	if (sbavail(&so->so_snd)) {
14944 		if (ctf_progress_timeout_check(tp, true)) {
14945 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
14946 						tp, tick, PROGRESS_DROP, __LINE__);
14947 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
14948 			return (1);
14949 		}
14950 	}
14951 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
14952 	    tiwin, thflags, nxt_pkt));
14953 }
14954 
14955 /*
14956  * Return value of 1, the TCB is unlocked and most
14957  * likely gone, return value of 0, the TCP is still
14958  * locked.
14959  */
14960 static int
14961 rack_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so,
14962     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
14963     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
14964 {
14965 	int32_t ret_val = 0;
14966 	int32_t orig_tlen = tlen;
14967 	int32_t ourfinisacked = 0;
14968 	struct tcp_rack *rack;
14969 
14970 	rack = (struct tcp_rack *)tp->t_fb_ptr;
14971 	ctf_calc_rwin(so, tp);
14972 
14973 	if ((thflags & TH_RST) ||
14974 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
14975 		return (__ctf_process_rst(m, th, so, tp,
14976 					  &rack->r_ctl.challenge_ack_ts,
14977 					  &rack->r_ctl.challenge_ack_cnt));
14978 	/*
14979 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
14980 	 * synchronized state.
14981 	 */
14982 	if (thflags & TH_SYN) {
14983 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
14984 		return (ret_val);
14985 	}
14986 	/*
14987 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
14988 	 * it's less than ts_recent, drop it.
14989 	 */
14990 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
14991 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
14992 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
14993 			return (ret_val);
14994 	}
14995 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
14996 			      &rack->r_ctl.challenge_ack_ts,
14997 			      &rack->r_ctl.challenge_ack_cnt)) {
14998 		return (ret_val);
14999 	}
15000 	/*
15001 	 * If new data are received on a connection after the user processes
15002 	 * are gone, then RST the other end.
15003 	 */
15004 	if ((tp->t_flags & TF_CLOSED) && tlen &&
15005 	    rack_check_data_after_close(m, tp, &tlen, th, so))
15006 		return (1);
15007 	/*
15008 	 * If last ACK falls within this segment's sequence numbers, record
15009 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
15010 	 * from the latest proposal of the tcplw@cray.com list (Braden
15011 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
15012 	 * with our earlier PAWS tests, so this check should be solely
15013 	 * predicated on the sequence space of this segment. 3) That we
15014 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
15015 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
15016 	 * SEG.Len, This modified check allows us to overcome RFC1323's
15017 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
15018 	 * p.869. In such cases, we can still calculate the RTT correctly
15019 	 * when RCV.NXT == Last.ACK.Sent.
15020 	 */
15021 	if ((to->to_flags & TOF_TS) != 0 &&
15022 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
15023 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
15024 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
15025 		tp->ts_recent_age = tcp_ts_getticks();
15026 		tp->ts_recent = to->to_tsval;
15027 	}
15028 	/*
15029 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
15030 	 * is on (half-synchronized state), then queue data for later
15031 	 * processing; else drop segment and return.
15032 	 */
15033 	if ((thflags & TH_ACK) == 0) {
15034 		if (tp->t_flags & TF_NEEDSYN) {
15035 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
15036 			    tiwin, thflags, nxt_pkt));
15037 		} else if (tp->t_flags & TF_ACKNOW) {
15038 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
15039 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
15040 			return (ret_val);
15041 		} else {
15042 			ctf_do_drop(m, NULL);
15043 			return (0);
15044 		}
15045 	}
15046 	/*
15047 	 * Ack processing.
15048 	 */
15049 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) {
15050 		return (ret_val);
15051 	}
15052 	if (ourfinisacked) {
15053 		tcp_twstart(tp);
15054 		m_freem(m);
15055 		return (1);
15056 	}
15057 	if (sbavail(&so->so_snd)) {
15058 		if (ctf_progress_timeout_check(tp, true)) {
15059 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
15060 						tp, tick, PROGRESS_DROP, __LINE__);
15061 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
15062 			return (1);
15063 		}
15064 	}
15065 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
15066 	    tiwin, thflags, nxt_pkt));
15067 }
15068 
15069 /*
15070  * Return value of 1, the TCB is unlocked and most
15071  * likely gone, return value of 0, the TCP is still
15072  * locked.
15073  */
15074 static int
15075 rack_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
15076     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
15077     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
15078 {
15079 	int32_t ret_val = 0;
15080 	int32_t orig_tlen;
15081 	int32_t ourfinisacked = 0;
15082 	struct tcp_rack *rack;
15083 
15084 	rack = (struct tcp_rack *)tp->t_fb_ptr;
15085 	ctf_calc_rwin(so, tp);
15086 
15087 	if ((thflags & TH_RST) ||
15088 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
15089 		return (__ctf_process_rst(m, th, so, tp,
15090 					  &rack->r_ctl.challenge_ack_ts,
15091 					  &rack->r_ctl.challenge_ack_cnt));
15092 	/*
15093 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
15094 	 * synchronized state.
15095 	 */
15096 	if (thflags & TH_SYN) {
15097 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
15098 		return (ret_val);
15099 	}
15100 	/*
15101 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
15102 	 * it's less than ts_recent, drop it.
15103 	 */
15104 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
15105 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
15106 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
15107 			return (ret_val);
15108 	}
15109 	orig_tlen = tlen;
15110 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
15111 			      &rack->r_ctl.challenge_ack_ts,
15112 			      &rack->r_ctl.challenge_ack_cnt)) {
15113 		return (ret_val);
15114 	}
15115 	/*
15116 	 * If new data are received on a connection after the user processes
15117 	 * are gone, then RST the other end.
15118 	 */
15119 	if ((tp->t_flags & TF_CLOSED) && tlen &&
15120 	    rack_check_data_after_close(m, tp, &tlen, th, so))
15121 		return (1);
15122 	/*
15123 	 * If last ACK falls within this segment's sequence numbers, record
15124 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
15125 	 * from the latest proposal of the tcplw@cray.com list (Braden
15126 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
15127 	 * with our earlier PAWS tests, so this check should be solely
15128 	 * predicated on the sequence space of this segment. 3) That we
15129 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
15130 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
15131 	 * SEG.Len, This modified check allows us to overcome RFC1323's
15132 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
15133 	 * p.869. In such cases, we can still calculate the RTT correctly
15134 	 * when RCV.NXT == Last.ACK.Sent.
15135 	 */
15136 	if ((to->to_flags & TOF_TS) != 0 &&
15137 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
15138 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
15139 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
15140 		tp->ts_recent_age = tcp_ts_getticks();
15141 		tp->ts_recent = to->to_tsval;
15142 	}
15143 	/*
15144 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
15145 	 * is on (half-synchronized state), then queue data for later
15146 	 * processing; else drop segment and return.
15147 	 */
15148 	if ((thflags & TH_ACK) == 0) {
15149 		if (tp->t_flags & TF_NEEDSYN) {
15150 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
15151 			    tiwin, thflags, nxt_pkt));
15152 		} else if (tp->t_flags & TF_ACKNOW) {
15153 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
15154 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
15155 			return (ret_val);
15156 		} else {
15157 			ctf_do_drop(m, NULL);
15158 			return (0);
15159 		}
15160 	}
15161 	/*
15162 	 * case TCPS_LAST_ACK: Ack processing.
15163 	 */
15164 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) {
15165 		return (ret_val);
15166 	}
15167 	if (ourfinisacked) {
15168 		tp = tcp_close(tp);
15169 		ctf_do_drop(m, tp);
15170 		return (1);
15171 	}
15172 	if (sbavail(&so->so_snd)) {
15173 		if (ctf_progress_timeout_check(tp, true)) {
15174 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
15175 						tp, tick, PROGRESS_DROP, __LINE__);
15176 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
15177 			return (1);
15178 		}
15179 	}
15180 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
15181 	    tiwin, thflags, nxt_pkt));
15182 }
15183 
15184 /*
15185  * Return value of 1, the TCB is unlocked and most
15186  * likely gone, return value of 0, the TCP is still
15187  * locked.
15188  */
15189 static int
15190 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so,
15191     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
15192     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
15193 {
15194 	int32_t ret_val = 0;
15195 	int32_t orig_tlen = tlen;
15196 	int32_t ourfinisacked = 0;
15197 	struct tcp_rack *rack;
15198 
15199 	rack = (struct tcp_rack *)tp->t_fb_ptr;
15200 	ctf_calc_rwin(so, tp);
15201 
15202 	/* Reset receive buffer auto scaling when not in bulk receive mode. */
15203 	if ((thflags & TH_RST) ||
15204 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
15205 		return (__ctf_process_rst(m, th, so, tp,
15206 					  &rack->r_ctl.challenge_ack_ts,
15207 					  &rack->r_ctl.challenge_ack_cnt));
15208 	/*
15209 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
15210 	 * synchronized state.
15211 	 */
15212 	if (thflags & TH_SYN) {
15213 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
15214 		return (ret_val);
15215 	}
15216 	/*
15217 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
15218 	 * it's less than ts_recent, drop it.
15219 	 */
15220 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
15221 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
15222 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
15223 			return (ret_val);
15224 	}
15225 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
15226 			      &rack->r_ctl.challenge_ack_ts,
15227 			      &rack->r_ctl.challenge_ack_cnt)) {
15228 		return (ret_val);
15229 	}
15230 	/*
15231 	 * If new data are received on a connection after the user processes
15232 	 * are gone, then RST the other end.
15233 	 */
15234 	if ((tp->t_flags & TF_CLOSED) && tlen &&
15235 	    rack_check_data_after_close(m, tp, &tlen, th, so))
15236 		return (1);
15237 	/*
15238 	 * If last ACK falls within this segment's sequence numbers, record
15239 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
15240 	 * from the latest proposal of the tcplw@cray.com list (Braden
15241 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
15242 	 * with our earlier PAWS tests, so this check should be solely
15243 	 * predicated on the sequence space of this segment. 3) That we
15244 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
15245 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
15246 	 * SEG.Len, This modified check allows us to overcome RFC1323's
15247 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
15248 	 * p.869. In such cases, we can still calculate the RTT correctly
15249 	 * when RCV.NXT == Last.ACK.Sent.
15250 	 */
15251 	if ((to->to_flags & TOF_TS) != 0 &&
15252 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
15253 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
15254 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
15255 		tp->ts_recent_age = tcp_ts_getticks();
15256 		tp->ts_recent = to->to_tsval;
15257 	}
15258 	/*
15259 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
15260 	 * is on (half-synchronized state), then queue data for later
15261 	 * processing; else drop segment and return.
15262 	 */
15263 	if ((thflags & TH_ACK) == 0) {
15264 		if (tp->t_flags & TF_NEEDSYN) {
15265 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
15266 			    tiwin, thflags, nxt_pkt));
15267 		} else if (tp->t_flags & TF_ACKNOW) {
15268 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
15269 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
15270 			return (ret_val);
15271 		} else {
15272 			ctf_do_drop(m, NULL);
15273 			return (0);
15274 		}
15275 	}
15276 	/*
15277 	 * Ack processing.
15278 	 */
15279 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) {
15280 		return (ret_val);
15281 	}
15282 	if (sbavail(&so->so_snd)) {
15283 		if (ctf_progress_timeout_check(tp, true)) {
15284 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
15285 						tp, tick, PROGRESS_DROP, __LINE__);
15286 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
15287 			return (1);
15288 		}
15289 	}
15290 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
15291 	    tiwin, thflags, nxt_pkt));
15292 }
15293 
15294 static void inline
15295 rack_clear_rate_sample(struct tcp_rack *rack)
15296 {
15297 	rack->r_ctl.rack_rs.rs_flags = RACK_RTT_EMPTY;
15298 	rack->r_ctl.rack_rs.rs_rtt_cnt = 0;
15299 	rack->r_ctl.rack_rs.rs_rtt_tot = 0;
15300 }
15301 
15302 static void
15303 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override)
15304 {
15305 	uint64_t bw_est, rate_wanted;
15306 	int chged = 0;
15307 	uint32_t user_max, orig_min, orig_max;
15308 
15309 #ifdef TCP_REQUEST_TRK
15310 	if (rack->rc_hybrid_mode &&
15311 	    (rack->r_ctl.rc_pace_max_segs != 0) &&
15312 	    (rack_hybrid_allow_set_maxseg == 1) &&
15313 	    (rack->r_ctl.rc_last_sft != NULL)) {
15314 		rack->r_ctl.rc_last_sft->hybrid_flags &= ~TCP_HYBRID_PACING_SETMSS;
15315 		return;
15316 	}
15317 #endif
15318 	orig_min = rack->r_ctl.rc_pace_min_segs;
15319 	orig_max = rack->r_ctl.rc_pace_max_segs;
15320 	user_max = ctf_fixed_maxseg(tp) * rack->rc_user_set_max_segs;
15321 	if (ctf_fixed_maxseg(tp) != rack->r_ctl.rc_pace_min_segs)
15322 		chged = 1;
15323 	rack->r_ctl.rc_pace_min_segs = ctf_fixed_maxseg(tp);
15324 	if (rack->use_fixed_rate || rack->rc_force_max_seg) {
15325 		if (user_max != rack->r_ctl.rc_pace_max_segs)
15326 			chged = 1;
15327 	}
15328 	if (rack->rc_force_max_seg) {
15329 		rack->r_ctl.rc_pace_max_segs = user_max;
15330 	} else if (rack->use_fixed_rate) {
15331 		bw_est = rack_get_bw(rack);
15332 		if ((rack->r_ctl.crte == NULL) ||
15333 		    (bw_est != rack->r_ctl.crte->rate)) {
15334 			rack->r_ctl.rc_pace_max_segs = user_max;
15335 		} else {
15336 			/* We are pacing right at the hardware rate */
15337 			uint32_t segsiz, pace_one;
15338 
15339 			if (rack_pace_one_seg ||
15340 			    (rack->r_ctl.rc_user_set_min_segs == 1))
15341 				pace_one = 1;
15342 			else
15343 				pace_one = 0;
15344 			segsiz = min(ctf_fixed_maxseg(tp),
15345 				     rack->r_ctl.rc_pace_min_segs);
15346 			rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size_w_divisor(
15347 				tp, bw_est, segsiz, pace_one,
15348 				rack->r_ctl.crte, NULL, rack->r_ctl.pace_len_divisor);
15349 		}
15350 	} else if (rack->rc_always_pace) {
15351 		if (rack->r_ctl.gp_bw ||
15352 		    rack->r_ctl.init_rate) {
15353 			/* We have a rate of some sort set */
15354 			uint32_t  orig;
15355 
15356 			bw_est = rack_get_bw(rack);
15357 			orig = rack->r_ctl.rc_pace_max_segs;
15358 			if (fill_override)
15359 				rate_wanted = *fill_override;
15360 			else
15361 				rate_wanted = rack_get_gp_est(rack);
15362 			if (rate_wanted) {
15363 				/* We have something */
15364 				rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack,
15365 										   rate_wanted,
15366 										   ctf_fixed_maxseg(rack->rc_tp));
15367 			} else
15368 				rack->r_ctl.rc_pace_max_segs = rack->r_ctl.rc_pace_min_segs;
15369 			if (orig != rack->r_ctl.rc_pace_max_segs)
15370 				chged = 1;
15371 		} else if ((rack->r_ctl.gp_bw == 0) &&
15372 			   (rack->r_ctl.rc_pace_max_segs == 0)) {
15373 			/*
15374 			 * If we have nothing limit us to bursting
15375 			 * out IW sized pieces.
15376 			 */
15377 			chged = 1;
15378 			rack->r_ctl.rc_pace_max_segs = rc_init_window(rack);
15379 		}
15380 	}
15381 	if (rack->r_ctl.rc_pace_max_segs > PACE_MAX_IP_BYTES) {
15382 		chged = 1;
15383 		rack->r_ctl.rc_pace_max_segs = PACE_MAX_IP_BYTES;
15384 	}
15385 	if (chged)
15386 		rack_log_type_pacing_sizes(tp, rack, orig_min, orig_max, line, 2);
15387 }
15388 
15389 
15390 static void
15391 rack_init_fsb_block(struct tcpcb *tp, struct tcp_rack *rack, int32_t flags)
15392 {
15393 #ifdef INET6
15394 	struct ip6_hdr *ip6 = NULL;
15395 #endif
15396 #ifdef INET
15397 	struct ip *ip = NULL;
15398 #endif
15399 	struct udphdr *udp = NULL;
15400 
15401 	/* Ok lets fill in the fast block, it can only be used with no IP options! */
15402 #ifdef INET6
15403 	if (rack->r_is_v6) {
15404 		rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
15405 		ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
15406 		if (tp->t_port) {
15407 			rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr);
15408 			udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
15409 			udp->uh_sport = htons(V_tcp_udp_tunneling_port);
15410 			udp->uh_dport = tp->t_port;
15411 			rack->r_ctl.fsb.udp = udp;
15412 			rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1);
15413 		} else
15414 		{
15415 			rack->r_ctl.fsb.th = (struct tcphdr *)(ip6 + 1);
15416 			rack->r_ctl.fsb.udp = NULL;
15417 		}
15418 		tcpip_fillheaders(rack->rc_inp,
15419 				  tp->t_port,
15420 				  ip6, rack->r_ctl.fsb.th);
15421 		rack->r_ctl.fsb.hoplimit = in6_selecthlim(rack->rc_inp, NULL);
15422 	} else
15423 #endif				/* INET6 */
15424 #ifdef INET
15425 	{
15426 		rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr);
15427 		ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
15428 		if (tp->t_port) {
15429 			rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr);
15430 			udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
15431 			udp->uh_sport = htons(V_tcp_udp_tunneling_port);
15432 			udp->uh_dport = tp->t_port;
15433 			rack->r_ctl.fsb.udp = udp;
15434 			rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1);
15435 		} else
15436 		{
15437 			rack->r_ctl.fsb.udp = NULL;
15438 			rack->r_ctl.fsb.th = (struct tcphdr *)(ip + 1);
15439 		}
15440 		tcpip_fillheaders(rack->rc_inp,
15441 				  tp->t_port,
15442 				  ip, rack->r_ctl.fsb.th);
15443 		rack->r_ctl.fsb.hoplimit = tptoinpcb(tp)->inp_ip_ttl;
15444 	}
15445 #endif
15446 	rack->r_ctl.fsb.recwin = lmin(lmax(sbspace(&tptosocket(tp)->so_rcv), 0),
15447 	    (long)TCP_MAXWIN << tp->rcv_scale);
15448 	rack->r_fsb_inited = 1;
15449 }
15450 
15451 static int
15452 rack_init_fsb(struct tcpcb *tp, struct tcp_rack *rack)
15453 {
15454 	/*
15455 	 * Allocate the larger of spaces V6 if available else just
15456 	 * V4 and include udphdr (overbook)
15457 	 */
15458 #ifdef INET6
15459 	rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr) + sizeof(struct udphdr);
15460 #else
15461 	rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr) + sizeof(struct udphdr);
15462 #endif
15463 	rack->r_ctl.fsb.tcp_ip_hdr = malloc(rack->r_ctl.fsb.tcp_ip_hdr_len,
15464 					    M_TCPFSB, M_NOWAIT|M_ZERO);
15465 	if (rack->r_ctl.fsb.tcp_ip_hdr == NULL) {
15466 		return (ENOMEM);
15467 	}
15468 	rack->r_fsb_inited = 0;
15469 	return (0);
15470 }
15471 
15472 static void
15473 rack_log_hystart_event(struct tcp_rack *rack, uint32_t high_seq, uint8_t mod)
15474 {
15475 	/*
15476 	 * Types of logs (mod value)
15477 	 * 20 - Initial round setup
15478 	 * 21 - Rack declares a new round.
15479 	 */
15480 	struct tcpcb *tp;
15481 
15482 	tp = rack->rc_tp;
15483 	if (tcp_bblogging_on(tp)) {
15484 		union tcp_log_stackspecific log;
15485 		struct timeval tv;
15486 
15487 		memset(&log, 0, sizeof(log));
15488 		log.u_bbr.flex1 = rack->r_ctl.current_round;
15489 		log.u_bbr.flex2 = rack->r_ctl.roundends;
15490 		log.u_bbr.flex3 = high_seq;
15491 		log.u_bbr.flex4 = tp->snd_max;
15492 		log.u_bbr.flex8 = mod;
15493 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
15494 		log.u_bbr.cur_del_rate = rack->rc_tp->t_sndbytes;
15495 		log.u_bbr.delRate = rack->rc_tp->t_snd_rxt_bytes;
15496 		TCP_LOG_EVENTP(tp, NULL,
15497 		    &tptosocket(tp)->so_rcv,
15498 		    &tptosocket(tp)->so_snd,
15499 		    TCP_HYSTART, 0,
15500 		    0, &log, false, &tv);
15501 	}
15502 }
15503 
15504 static void
15505 rack_deferred_init(struct tcpcb *tp, struct tcp_rack *rack)
15506 {
15507 	rack->rack_deferred_inited = 1;
15508 	rack->r_ctl.roundends = tp->snd_max;
15509 	rack->r_ctl.rc_high_rwnd = tp->snd_wnd;
15510 	rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
15511 }
15512 
15513 static void
15514 rack_init_retransmit_value(struct tcp_rack *rack, int ctl)
15515 {
15516 	/* Retransmit bit controls.
15517 	 *
15518 	 * The setting of these values control one of
15519 	 * three settings you can have and dictate
15520 	 * how rack does retransmissions. Note this
15521 	 * is in *any* mode i.e. pacing on or off DGP
15522 	 * fixed rate pacing, or just bursting rack.
15523 	 *
15524 	 * 1 - Use full sized retransmits i.e. limit
15525 	 *     the size to whatever the pace_max_segments
15526 	 *     size is.
15527 	 *
15528 	 * 2 - Use pacer min granularity as a guide to
15529 	 *     the size combined with the current calculated
15530 	 *     goodput b/w measurement. So for example if
15531 	 *     the goodput is measured at 20Mbps we would
15532 	 *     calculate 8125 (pacer minimum 250usec in
15533 	 *     that b/w) and then round it up to the next
15534 	 *     MSS i.e. for 1448 mss 6 MSS or 8688 bytes.
15535 	 *
15536 	 * 0 - The rack default 1 MSS (anything not 0/1/2
15537 	 *     fall here too if we are setting via rack_init()).
15538 	 *
15539 	 */
15540 	if (ctl == 1) {
15541 		rack->full_size_rxt = 1;
15542 		rack->shape_rxt_to_pacing_min  = 0;
15543 	} else if (ctl == 2) {
15544 		rack->full_size_rxt = 0;
15545 		rack->shape_rxt_to_pacing_min  = 1;
15546 	} else {
15547 		rack->full_size_rxt = 0;
15548 		rack->shape_rxt_to_pacing_min  = 0;
15549 	}
15550 }
15551 
15552 static void
15553 rack_log_chg_info(struct tcpcb *tp, struct tcp_rack *rack, uint8_t mod,
15554 		  uint32_t flex1,
15555 		  uint32_t flex2,
15556 		  uint32_t flex3)
15557 {
15558 	if (tcp_bblogging_on(rack->rc_tp)) {
15559 		union tcp_log_stackspecific log;
15560 		struct timeval tv;
15561 
15562 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
15563 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
15564 		log.u_bbr.flex8 = mod;
15565 		log.u_bbr.flex1 = flex1;
15566 		log.u_bbr.flex2 = flex2;
15567 		log.u_bbr.flex3 = flex3;
15568 		tcp_log_event(tp, NULL, NULL, NULL, TCP_CHG_QUERY, 0,
15569 			       0, &log, false, NULL, __func__, __LINE__, &tv);
15570 	}
15571 }
15572 
15573 static int
15574 rack_chg_query(struct tcpcb *tp, struct tcp_query_resp *reqr)
15575 {
15576 	struct tcp_rack *rack;
15577 	struct rack_sendmap *rsm;
15578 	int i;
15579 
15580 
15581 	rack = (struct tcp_rack *)tp->t_fb_ptr;
15582 	switch (reqr->req) {
15583 	case TCP_QUERY_SENDMAP:
15584 		if ((reqr->req_param == tp->snd_max) ||
15585 		    (tp->snd_max == tp->snd_una)){
15586 			/* Unlikely */
15587 			return (0);
15588 		}
15589 		rsm = tqhash_find(rack->r_ctl.tqh, reqr->req_param);
15590 		if (rsm == NULL) {
15591 			/* Can't find that seq -- unlikely */
15592 			return (0);
15593 		}
15594 		reqr->sendmap_start = rsm->r_start;
15595 		reqr->sendmap_end = rsm->r_end;
15596 		reqr->sendmap_send_cnt = rsm->r_rtr_cnt;
15597 		reqr->sendmap_fas = rsm->r_fas;
15598 		if (reqr->sendmap_send_cnt > SNDMAP_NRTX)
15599 			reqr->sendmap_send_cnt = SNDMAP_NRTX;
15600 		for(i=0; i<reqr->sendmap_send_cnt; i++)
15601 			reqr->sendmap_time[i] = rsm->r_tim_lastsent[i];
15602 		reqr->sendmap_ack_arrival = rsm->r_ack_arrival;
15603 		reqr->sendmap_flags = rsm->r_flags & SNDMAP_MASK;
15604 		reqr->sendmap_r_rtr_bytes = rsm->r_rtr_bytes;
15605 		reqr->sendmap_dupacks = rsm->r_dupack;
15606 		rack_log_chg_info(tp, rack, 1,
15607 				  rsm->r_start,
15608 				  rsm->r_end,
15609 				  rsm->r_flags);
15610 		return(1);
15611 		break;
15612 	case TCP_QUERY_TIMERS_UP:
15613 		if (rack->r_ctl.rc_hpts_flags == 0) {
15614 			/* no timers up */
15615 			return (0);
15616 		}
15617 		reqr->timer_hpts_flags = rack->r_ctl.rc_hpts_flags;
15618 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
15619 			reqr->timer_pacing_to = rack->r_ctl.rc_last_output_to;
15620 		}
15621 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
15622 			reqr->timer_timer_exp = rack->r_ctl.rc_timer_exp;
15623 		}
15624 		rack_log_chg_info(tp, rack, 2,
15625 				  rack->r_ctl.rc_hpts_flags,
15626 				  rack->r_ctl.rc_last_output_to,
15627 				  rack->r_ctl.rc_timer_exp);
15628 		return (1);
15629 		break;
15630 	case TCP_QUERY_RACK_TIMES:
15631 		/* Reordering items */
15632 		reqr->rack_num_dsacks = rack->r_ctl.num_dsack;
15633 		reqr->rack_reorder_ts = rack->r_ctl.rc_reorder_ts;
15634 		/* Timerstamps and timers */
15635 		reqr->rack_rxt_last_time = rack->r_ctl.rc_tlp_rxt_last_time;
15636 		reqr->rack_min_rtt = rack->r_ctl.rc_rack_min_rtt;
15637 		reqr->rack_rtt = rack->rc_rack_rtt;
15638 		reqr->rack_tmit_time = rack->r_ctl.rc_rack_tmit_time;
15639 		reqr->rack_srtt_measured = rack->rc_srtt_measure_made;
15640 		/* PRR data */
15641 		reqr->rack_sacked = rack->r_ctl.rc_sacked;
15642 		reqr->rack_holes_rxt = rack->r_ctl.rc_holes_rxt;
15643 		reqr->rack_prr_delivered = rack->r_ctl.rc_prr_delivered;
15644 		reqr->rack_prr_recovery_fs = rack->r_ctl.rc_prr_recovery_fs;
15645 		reqr->rack_prr_sndcnt = rack->r_ctl.rc_prr_sndcnt;
15646 		reqr->rack_prr_out = rack->r_ctl.rc_prr_out;
15647 		/* TLP and persists info */
15648 		reqr->rack_tlp_out = rack->rc_tlp_in_progress;
15649 		reqr->rack_tlp_cnt_out = rack->r_ctl.rc_tlp_cnt_out;
15650 		if (rack->rc_in_persist) {
15651 			reqr->rack_time_went_idle = rack->r_ctl.rc_went_idle_time;
15652 			reqr->rack_in_persist = 1;
15653 		} else {
15654 			reqr->rack_time_went_idle = 0;
15655 			reqr->rack_in_persist = 0;
15656 		}
15657 		if (rack->r_wanted_output)
15658 			reqr->rack_wanted_output = 1;
15659 		else
15660 			reqr->rack_wanted_output = 0;
15661 		return (1);
15662 		break;
15663 	default:
15664 		return (-EINVAL);
15665 	}
15666 }
15667 
15668 static void
15669 rack_switch_failed(struct tcpcb *tp)
15670 {
15671 	/*
15672 	 * This method gets called if a stack switch was
15673 	 * attempted and it failed. We are left
15674 	 * but our hpts timers were stopped and we
15675 	 * need to validate time units and t_flags2.
15676 	 */
15677 	struct tcp_rack *rack;
15678 	struct timeval tv;
15679 	uint32_t cts;
15680 	uint32_t toval;
15681 	struct hpts_diag diag;
15682 
15683 	rack = (struct tcp_rack *)tp->t_fb_ptr;
15684 	tcp_change_time_units(tp, TCP_TMR_GRANULARITY_USEC);
15685 	if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
15686 		tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
15687 	else
15688 		tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
15689 	if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
15690 		tp->t_flags2 |= TF2_MBUF_ACKCMP;
15691 	if (tp->t_in_hpts > IHPTS_NONE) {
15692 		/* Strange */
15693 		return;
15694 	}
15695 	cts = tcp_get_usecs(&tv);
15696 	if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
15697 		if (TSTMP_GT(rack->r_ctl.rc_last_output_to, cts)) {
15698 			toval = rack->r_ctl.rc_last_output_to - cts;
15699 		} else {
15700 			/* one slot please */
15701 			toval = HPTS_TICKS_PER_SLOT;
15702 		}
15703 	} else if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
15704 		if (TSTMP_GT(rack->r_ctl.rc_timer_exp, cts)) {
15705 			toval = rack->r_ctl.rc_timer_exp - cts;
15706 		} else {
15707 			/* one slot please */
15708 			toval = HPTS_TICKS_PER_SLOT;
15709 		}
15710 	} else
15711 		toval = HPTS_TICKS_PER_SLOT;
15712 	(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(toval),
15713 				   __LINE__, &diag);
15714 	rack_log_hpts_diag(rack, cts, &diag, &tv);
15715 }
15716 
15717 static int
15718 rack_init_outstanding(struct tcpcb *tp, struct tcp_rack *rack, uint32_t us_cts, void *ptr)
15719 {
15720 	struct rack_sendmap *rsm, *ersm;
15721 	int insret __diagused;
15722 	/*
15723 	 * When initing outstanding, we must be quite careful
15724 	 * to not refer to tp->t_fb_ptr. This has the old rack
15725 	 * pointer in it, not the "new" one (when we are doing
15726 	 * a stack switch).
15727 	 */
15728 
15729 
15730 	if (tp->t_fb->tfb_chg_query == NULL) {
15731 		/* Create a send map for the current outstanding data */
15732 
15733 		rsm = rack_alloc(rack);
15734 		if (rsm == NULL) {
15735 			uma_zfree(rack_pcb_zone, ptr);
15736 			return (ENOMEM);
15737 		}
15738 		rsm->r_no_rtt_allowed = 1;
15739 		rsm->r_tim_lastsent[0] = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
15740 		rsm->r_rtr_cnt = 1;
15741 		rsm->r_rtr_bytes = 0;
15742 		if (tp->t_flags & TF_SENTFIN)
15743 			rsm->r_flags |= RACK_HAS_FIN;
15744 		rsm->r_end = tp->snd_max;
15745 		if (tp->snd_una == tp->iss) {
15746 			/* The data space is one beyond snd_una */
15747 			rsm->r_flags |= RACK_HAS_SYN;
15748 			rsm->r_start = tp->iss;
15749 			rsm->r_end = rsm->r_start + (tp->snd_max - tp->snd_una);
15750 		} else
15751 			rsm->r_start = tp->snd_una;
15752 		rsm->r_dupack = 0;
15753 		if (rack->rc_inp->inp_socket->so_snd.sb_mb != NULL) {
15754 			rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd, 0, &rsm->soff);
15755 			if (rsm->m) {
15756 				rsm->orig_m_len = rsm->m->m_len;
15757 				rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
15758 			} else {
15759 				rsm->orig_m_len = 0;
15760 				rsm->orig_t_space = 0;
15761 			}
15762 		} else {
15763 			/*
15764 			 * This can happen if we have a stand-alone FIN or
15765 			 *  SYN.
15766 			 */
15767 			rsm->m = NULL;
15768 			rsm->orig_m_len = 0;
15769 			rsm->orig_t_space = 0;
15770 			rsm->soff = 0;
15771 		}
15772 #ifdef INVARIANTS
15773 		if ((insret = tqhash_insert(rack->r_ctl.tqh, rsm)) != 0) {
15774 			panic("Insert in tailq_hash fails ret:%d rack:%p rsm:%p",
15775 			      insret, rack, rsm);
15776 		}
15777 #else
15778 		(void)tqhash_insert(rack->r_ctl.tqh, rsm);
15779 #endif
15780 		TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
15781 		rsm->r_in_tmap = 1;
15782 	} else {
15783 		/* We have a query mechanism, lets use it */
15784 		struct tcp_query_resp qr;
15785 		int i;
15786 		tcp_seq at;
15787 
15788 		at = tp->snd_una;
15789 		while (at != tp->snd_max) {
15790 			memset(&qr, 0, sizeof(qr));
15791 			qr.req = TCP_QUERY_SENDMAP;
15792 			qr.req_param = at;
15793 			if ((*tp->t_fb->tfb_chg_query)(tp, &qr) == 0)
15794 				break;
15795 			/* Move forward */
15796 			at = qr.sendmap_end;
15797 			/* Now lets build the entry for this one */
15798 			rsm = rack_alloc(rack);
15799 			if (rsm == NULL) {
15800 				uma_zfree(rack_pcb_zone, ptr);
15801 				return (ENOMEM);
15802 			}
15803 			memset(rsm, 0, sizeof(struct rack_sendmap));
15804 			/* Now configure the rsm and insert it */
15805 			rsm->r_dupack = qr.sendmap_dupacks;
15806 			rsm->r_start = qr.sendmap_start;
15807 			rsm->r_end = qr.sendmap_end;
15808 			if (qr.sendmap_fas)
15809 				rsm->r_fas = qr.sendmap_end;
15810 			else
15811 				rsm->r_fas = rsm->r_start - tp->snd_una;
15812 			/*
15813 			 * We have carefully aligned the bits
15814 			 * so that all we have to do is copy over
15815 			 * the bits with the mask.
15816 			 */
15817 			rsm->r_flags = qr.sendmap_flags & SNDMAP_MASK;
15818 			rsm->r_rtr_bytes = qr.sendmap_r_rtr_bytes;
15819 			rsm->r_rtr_cnt = qr.sendmap_send_cnt;
15820 			rsm->r_ack_arrival = qr.sendmap_ack_arrival;
15821 			for (i=0 ; i<rsm->r_rtr_cnt; i++)
15822 				rsm->r_tim_lastsent[i]	= qr.sendmap_time[i];
15823 			rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd,
15824 					   (rsm->r_start - tp->snd_una), &rsm->soff);
15825 			if (rsm->m) {
15826 				rsm->orig_m_len = rsm->m->m_len;
15827 				rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
15828 			} else {
15829 				rsm->orig_m_len = 0;
15830 				rsm->orig_t_space = 0;
15831 			}
15832 #ifdef INVARIANTS
15833 			if ((insret = tqhash_insert(rack->r_ctl.tqh, rsm)) != 0) {
15834 				panic("Insert in tailq_hash fails ret:%d rack:%p rsm:%p",
15835 				      insret, rack, rsm);
15836 			}
15837 #else
15838 			(void)tqhash_insert(rack->r_ctl.tqh, rsm);
15839 #endif
15840 			if ((rsm->r_flags & RACK_ACKED) == 0)  {
15841 				TAILQ_FOREACH(ersm, &rack->r_ctl.rc_tmap, r_tnext) {
15842 					if (ersm->r_tim_lastsent[(ersm->r_rtr_cnt-1)] >
15843 					    rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]) {
15844 						/*
15845 						 * If the existing ersm was sent at
15846 						 * a later time than the new one, then
15847 						 * the new one should appear ahead of this
15848 						 * ersm.
15849 						 */
15850 						rsm->r_in_tmap = 1;
15851 						TAILQ_INSERT_BEFORE(ersm, rsm, r_tnext);
15852 						break;
15853 					}
15854 				}
15855 				if (rsm->r_in_tmap == 0) {
15856 					/*
15857 					 * Not found so shove it on the tail.
15858 					 */
15859 					TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
15860 					rsm->r_in_tmap = 1;
15861 				}
15862  			} else {
15863 				if ((rack->r_ctl.rc_sacklast == NULL) ||
15864 				    (SEQ_GT(rsm->r_end, rack->r_ctl.rc_sacklast->r_end))) {
15865 					rack->r_ctl.rc_sacklast = rsm;
15866 				}
15867 			}
15868 			rack_log_chg_info(tp, rack, 3,
15869 					  rsm->r_start,
15870 					  rsm->r_end,
15871 					  rsm->r_flags);
15872 		}
15873 	}
15874 	return (0);
15875 }
15876 
15877 static void
15878 rack_translate_policer_detect(struct tcp_rack *rack, uint32_t optval)
15879 {
15880 	/*
15881 	 * P = Percent of retransmits 499 = 49.9%
15882 	 * A = Average number 1 (.1%) -> 169 (16.9%)
15883 	 * M = Median number of retrans 1 - 16
15884 	 * MMMM MMMM AAAA AAAA PPPP PPPP PPPP PPPP
15885 	 *
15886 	 */
15887 	uint16_t per, upp;
15888 
15889 	per = optval & 0x0000ffff;
15890 	rack->r_ctl.policer_rxt_threshold = (uint32_t)(per & 0xffff);
15891 	upp = ((optval & 0xffff0000) >> 16);
15892 	rack->r_ctl.policer_avg_threshold = (0x00ff & upp);
15893 	rack->r_ctl.policer_med_threshold = ((upp >> 8) & 0x00ff);
15894 	if ((rack->r_ctl.policer_rxt_threshold > 0) &&
15895 	    (rack->r_ctl.policer_avg_threshold > 0) &&
15896 	    (rack->r_ctl.policer_med_threshold > 0)) {
15897 		rack->policer_detect_on = 1;
15898 	} else {
15899 		rack->policer_detect_on = 0;
15900 	}
15901 	rack->r_ctl.saved_policer_val = optval;
15902 	policer_detection_log(rack, optval,
15903 			      rack->r_ctl.policer_avg_threshold,
15904 			      rack->r_ctl.policer_med_threshold,
15905 			      rack->r_ctl.policer_rxt_threshold, 11);
15906 }
15907 
15908 static int32_t
15909 rack_init(struct tcpcb *tp, void **ptr)
15910 {
15911 	struct inpcb *inp = tptoinpcb(tp);
15912 	struct tcp_rack *rack = NULL;
15913 	uint32_t iwin, snt, us_cts;
15914 	size_t sz;
15915 	int err, no_query;
15916 
15917 	tcp_hpts_init(tp);
15918 
15919 	/*
15920 	 * First are we the initial or are we a switched stack?
15921 	 * If we are initing via tcp_newtcppcb the ptr passed
15922 	 * will be tp->t_fb_ptr. If its a stack switch that
15923 	 * has a previous stack we can query it will be a local
15924 	 * var that will in the end be set into t_fb_ptr.
15925 	 */
15926 	if (ptr == &tp->t_fb_ptr)
15927 		no_query = 1;
15928 	else
15929 		no_query = 0;
15930 	*ptr = uma_zalloc(rack_pcb_zone, M_NOWAIT);
15931 	if (*ptr == NULL) {
15932 		/*
15933 		 * We need to allocate memory but cant. The INP and INP_INFO
15934 		 * locks and they are recursive (happens during setup. So a
15935 		 * scheme to drop the locks fails :(
15936 		 *
15937 		 */
15938 		return(ENOMEM);
15939 	}
15940 	memset(*ptr, 0, sizeof(struct tcp_rack));
15941 	rack = (struct tcp_rack *)*ptr;
15942 	rack->r_ctl.tqh = malloc(sizeof(struct tailq_hash), M_TCPFSB, M_NOWAIT);
15943 	if (rack->r_ctl.tqh == NULL) {
15944 		uma_zfree(rack_pcb_zone, rack);
15945 		return(ENOMEM);
15946 	}
15947 	tqhash_init(rack->r_ctl.tqh);
15948 	TAILQ_INIT(&rack->r_ctl.rc_free);
15949 	TAILQ_INIT(&rack->r_ctl.rc_tmap);
15950 	rack->rc_tp = tp;
15951 	rack->rc_inp = inp;
15952 	/* Set the flag */
15953 	rack->r_is_v6 = (inp->inp_vflag & INP_IPV6) != 0;
15954 	/* Probably not needed but lets be sure */
15955 	rack_clear_rate_sample(rack);
15956 	/*
15957 	 * Save off the default values, socket options will poke
15958 	 * at these if pacing is not on or we have not yet
15959 	 * reached where pacing is on (gp_ready/fixed enabled).
15960 	 * When they get set into the CC module (when gp_ready
15961 	 * is enabled or we enable fixed) then we will set these
15962 	 * values into the CC and place in here the old values
15963 	 * so we have a restoral. Then we will set the flag
15964 	 * rc_pacing_cc_set. That way whenever we turn off pacing
15965 	 * or switch off this stack, we will know to go restore
15966 	 * the saved values.
15967 	 *
15968 	 * We specifically put into the beta the ecn value for pacing.
15969 	 */
15970 	rack->rc_new_rnd_needed = 1;
15971 	rack->r_ctl.rc_split_limit = V_tcp_map_split_limit;
15972 	/* We want abe like behavior as well */
15973 
15974 	rack->r_ctl.rc_saved_beta.newreno_flags |= CC_NEWRENO_BETA_ECN_ENABLED;
15975 	rack->r_ctl.rc_reorder_fade = rack_reorder_fade;
15976 	rack->rc_allow_data_af_clo = rack_ignore_data_after_close;
15977 	rack->r_ctl.rc_tlp_threshold = rack_tlp_thresh;
15978 	rack->r_ctl.policer_del_mss = rack_req_del_mss;
15979 	if ((rack_policer_rxt_thresh > 0) &&
15980 	    (rack_policer_avg_thresh > 0) &&
15981 	    (rack_policer_med_thresh > 0)) {
15982 		rack->r_ctl.policer_rxt_threshold = rack_policer_rxt_thresh;
15983 		rack->r_ctl.policer_avg_threshold = rack_policer_avg_thresh;
15984 		rack->r_ctl.policer_med_threshold = rack_policer_med_thresh;
15985 		rack->policer_detect_on = 1;
15986 	} else {
15987 		rack->policer_detect_on = 0;
15988 	}
15989 	if (rack_fill_cw_state)
15990 		rack->rc_pace_to_cwnd = 1;
15991 	if (rack_pacing_min_seg)
15992 		rack->r_ctl.rc_user_set_min_segs = rack_pacing_min_seg;
15993 	if (use_rack_rr)
15994 		rack->use_rack_rr = 1;
15995 	if (rack_dnd_default) {
15996 		rack->rc_pace_dnd = 1;
15997 	}
15998 	if (V_tcp_delack_enabled)
15999 		tp->t_delayed_ack = 1;
16000 	else
16001 		tp->t_delayed_ack = 0;
16002 #ifdef TCP_ACCOUNTING
16003 	if (rack_tcp_accounting) {
16004 		tp->t_flags2 |= TF2_TCP_ACCOUNTING;
16005 	}
16006 #endif
16007 	rack->r_ctl.pcm_i.cnt_alloc = RACK_DEFAULT_PCM_ARRAY;
16008 	sz = (sizeof(struct rack_pcm_stats) * rack->r_ctl.pcm_i.cnt_alloc);
16009 	rack->r_ctl.pcm_s = malloc(sz,M_TCPPCM, M_NOWAIT);
16010 	if (rack->r_ctl.pcm_s == NULL) {
16011 		rack->r_ctl.pcm_i.cnt_alloc = 0;
16012 	}
16013 #ifdef NETFLIX_STATS
16014 	rack->r_ctl.side_chan_dis_mask = tcp_sidechannel_disable_mask;
16015 #endif
16016 	rack->r_ctl.rack_per_upper_bound_ss = (uint8_t)rack_per_upper_bound_ss;
16017 	rack->r_ctl.rack_per_upper_bound_ca = (uint8_t)rack_per_upper_bound_ca;
16018 	if (rack_enable_shared_cwnd)
16019 		rack->rack_enable_scwnd = 1;
16020 	rack->r_ctl.pace_len_divisor = rack_default_pacing_divisor;
16021 	rack->rc_user_set_max_segs = rack_hptsi_segments;
16022 	rack->r_ctl.max_reduction = rack_max_reduce;
16023 	rack->rc_force_max_seg = 0;
16024 	TAILQ_INIT(&rack->r_ctl.opt_list);
16025 	rack->r_ctl.rc_saved_beta.beta = V_newreno_beta_ecn;
16026 	rack->r_ctl.rc_saved_beta.beta_ecn = V_newreno_beta_ecn;
16027 	if (rack_hibeta_setting) {
16028 		rack->rack_hibeta = 1;
16029 		if ((rack_hibeta_setting >= 50) &&
16030 		    (rack_hibeta_setting <= 100)) {
16031 			rack->r_ctl.rc_saved_beta.beta = rack_hibeta_setting;
16032 			rack->r_ctl.saved_hibeta = rack_hibeta_setting;
16033 		}
16034 	} else {
16035 		rack->r_ctl.saved_hibeta = 50;
16036 	}
16037 	/*
16038 	 * We initialize to all ones so we never match 0
16039 	 * just in case the client sends in 0, it hopefully
16040 	 * will never have all 1's in ms :-)
16041 	 */
16042 	rack->r_ctl.last_tm_mark = 0xffffffffffffffff;
16043 	rack->r_ctl.rc_reorder_shift = rack_reorder_thresh;
16044 	rack->r_ctl.rc_pkt_delay = rack_pkt_delay;
16045 	rack->r_ctl.pol_bw_comp = rack_policing_do_bw_comp;
16046 	rack->r_ctl.rc_tlp_cwnd_reduce = rack_lower_cwnd_at_tlp;
16047 	rack->r_ctl.rc_lowest_us_rtt = 0xffffffff;
16048 	rack->r_ctl.rc_highest_us_rtt = 0;
16049 	rack->r_ctl.bw_rate_cap = rack_bw_rate_cap;
16050 	rack->pcm_enabled = rack_pcm_is_enabled;
16051 	if (rack_fillcw_bw_cap)
16052 		rack->r_ctl.fillcw_cap = rack_fillcw_bw_cap;
16053 	rack->r_ctl.timer_slop = TICKS_2_USEC(tcp_rexmit_slop);
16054 	if (rack_use_cmp_acks)
16055 		rack->r_use_cmp_ack = 1;
16056 	if (rack_disable_prr)
16057 		rack->rack_no_prr = 1;
16058 	if (rack_gp_no_rec_chg)
16059 		rack->rc_gp_no_rec_chg = 1;
16060 	if (rack_pace_every_seg && tcp_can_enable_pacing()) {
16061 		rack->r_ctl.pacing_method |= RACK_REG_PACING;
16062 		rack->rc_always_pace = 1;
16063 		if (rack->rack_hibeta)
16064 			rack_set_cc_pacing(rack);
16065 	} else
16066 		rack->rc_always_pace = 0;
16067 	if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack)
16068 		rack->r_mbuf_queue = 1;
16069 	else
16070 		rack->r_mbuf_queue = 0;
16071 	rack_set_pace_segments(tp, rack, __LINE__, NULL);
16072 	if (rack_limits_scwnd)
16073 		rack->r_limit_scw = 1;
16074 	else
16075 		rack->r_limit_scw = 0;
16076 	rack_init_retransmit_value(rack, rack_rxt_controls);
16077 	rack->rc_labc = V_tcp_abc_l_var;
16078 	if (rack_honors_hpts_min_to)
16079 		rack->r_use_hpts_min = 1;
16080 	if (tp->snd_una != 0) {
16081 		rack->r_ctl.idle_snd_una = tp->snd_una;
16082 		rack->rc_sendvars_notset = 0;
16083 		/*
16084 		 * Make sure any TCP timers are not running.
16085 		 */
16086 		tcp_timer_stop(tp);
16087 	} else {
16088 		/*
16089 		 * Server side, we are called from the
16090 		 * syn-cache. This means none of the
16091 		 * snd_una/max are set yet so we have
16092 		 * to defer this until the first send.
16093 		 */
16094 		rack->rc_sendvars_notset = 1;
16095 	}
16096 
16097 	rack->r_ctl.rc_rate_sample_method = rack_rate_sample_method;
16098 	rack->rack_tlp_threshold_use = rack_tlp_threshold_use;
16099 	rack->r_ctl.rc_prr_sendalot = rack_send_a_lot_in_prr;
16100 	rack->r_ctl.rc_min_to = rack_min_to;
16101 	microuptime(&rack->r_ctl.act_rcv_time);
16102 	rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time;
16103 	rack->r_ctl.rack_per_of_gp_ss = rack_per_of_gp_ss;
16104 	if (rack_hw_up_only)
16105 		rack->r_up_only = 1;
16106 	if (rack_do_dyn_mul) {
16107 		/* When dynamic adjustment is on CA needs to start at 100% */
16108 		rack->rc_gp_dyn_mul = 1;
16109 		if (rack_do_dyn_mul >= 100)
16110 			rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul;
16111 	} else
16112 		rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca;
16113 	rack->r_ctl.rack_per_of_gp_rec = rack_per_of_gp_rec;
16114 	if (rack_timely_off) {
16115 		rack->rc_skip_timely = 1;
16116 	}
16117 	if (rack->rc_skip_timely) {
16118 		rack->r_ctl.rack_per_of_gp_rec = 90;
16119 		rack->r_ctl.rack_per_of_gp_ca = 100;
16120 		rack->r_ctl.rack_per_of_gp_ss = 250;
16121 	}
16122 	rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
16123 	rack->r_ctl.rc_tlp_rxt_last_time = tcp_tv_to_mssectick(&rack->r_ctl.act_rcv_time);
16124 	rack->r_ctl.last_rcv_tstmp_for_rtt = tcp_tv_to_mssectick(&rack->r_ctl.act_rcv_time);
16125 
16126 	setup_time_filter_small(&rack->r_ctl.rc_gp_min_rtt, FILTER_TYPE_MIN,
16127 				rack_probertt_filter_life);
16128 	us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
16129 	rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
16130 	rack->r_ctl.rc_time_of_last_probertt = us_cts;
16131 	rack->r_ctl.rc_went_idle_time = us_cts;
16132 	rack->r_ctl.challenge_ack_ts = tcp_ts_getticks() - (tcp_ack_war_time_window + 1);
16133 	rack->r_ctl.rc_time_probertt_starts = 0;
16134 
16135 	rack->r_ctl.gp_rnd_thresh = rack_rnd_cnt_req & 0xff;
16136 	if (rack_rnd_cnt_req  & 0x10000)
16137 		rack->r_ctl.gate_to_fs = 1;
16138 	rack->r_ctl.gp_gain_req = rack_gp_gain_req;
16139 	if ((rack_rnd_cnt_req & 0x100) > 0) {
16140 
16141 	}
16142 	if (rack_dsack_std_based & 0x1) {
16143 		/* Basically this means all rack timers are at least (srtt + 1/4 srtt) */
16144 		rack->rc_rack_tmr_std_based = 1;
16145 	}
16146 	if (rack_dsack_std_based & 0x2) {
16147 		/* Basically this means  rack timers are extended based on dsack by up to (2 * srtt) */
16148 		rack->rc_rack_use_dsack = 1;
16149 	}
16150 	/* We require at least one measurement, even if the sysctl is 0 */
16151 	if (rack_req_measurements)
16152 		rack->r_ctl.req_measurements = rack_req_measurements;
16153 	else
16154 		rack->r_ctl.req_measurements = 1;
16155 	if (rack_enable_hw_pacing)
16156 		rack->rack_hdw_pace_ena = 1;
16157 	if (rack_hw_rate_caps)
16158 		rack->r_rack_hw_rate_caps = 1;
16159 #ifdef TCP_SAD_DETECTION
16160 	rack->do_detection = 1;
16161 #else
16162 	rack->do_detection = 0;
16163 #endif
16164 	if (rack_non_rxt_use_cr)
16165 		rack->rack_rec_nonrxt_use_cr = 1;
16166 	/* Lets setup the fsb block */
16167 	err = rack_init_fsb(tp, rack);
16168 	if (err) {
16169 		uma_zfree(rack_pcb_zone, *ptr);
16170 		*ptr = NULL;
16171 		return (err);
16172 	}
16173 	if (rack_do_hystart) {
16174 		tp->t_ccv.flags |= CCF_HYSTART_ALLOWED;
16175 		if (rack_do_hystart > 1)
16176 			tp->t_ccv.flags |= CCF_HYSTART_CAN_SH_CWND;
16177 		if (rack_do_hystart > 2)
16178 			tp->t_ccv.flags |= CCF_HYSTART_CONS_SSTH;
16179 	}
16180 	/* Log what we will do with queries */
16181 	rack_log_chg_info(tp, rack, 7,
16182 			  no_query, 0, 0);
16183 	if (rack_def_profile)
16184 		rack_set_profile(rack, rack_def_profile);
16185 	/* Cancel the GP measurement in progress */
16186 	tp->t_flags &= ~TF_GPUTINPROG;
16187 	if ((tp->t_state != TCPS_CLOSED) &&
16188 	    (tp->t_state != TCPS_TIME_WAIT)) {
16189 		/*
16190 		 * We are already open, we may
16191 		 * need to adjust a few things.
16192 		 */
16193 		if (SEQ_GT(tp->snd_max, tp->iss))
16194 			snt = tp->snd_max - tp->iss;
16195 		else
16196 			snt = 0;
16197 		iwin = rc_init_window(rack);
16198 		if ((snt < iwin) &&
16199 		    (no_query == 1)) {
16200 			/* We are not past the initial window
16201 			 * on the first init (i.e. a stack switch
16202 			 * has not yet occured) so we need to make
16203 			 * sure cwnd and ssthresh is correct.
16204 			 */
16205 			if (tp->snd_cwnd < iwin)
16206 				tp->snd_cwnd = iwin;
16207 			/*
16208 			 * If we are within the initial window
16209 			 * we want ssthresh to be unlimited. Setting
16210 			 * it to the rwnd (which the default stack does
16211 			 * and older racks) is not really a good idea
16212 			 * since we want to be in SS and grow both the
16213 			 * cwnd and the rwnd (via dynamic rwnd growth). If
16214 			 * we set it to the rwnd then as the peer grows its
16215 			 * rwnd we will be stuck in CA and never hit SS.
16216 			 *
16217 			 * Its far better to raise it up high (this takes the
16218 			 * risk that there as been a loss already, probably
16219 			 * we should have an indicator in all stacks of loss
16220 			 * but we don't), but considering the normal use this
16221 			 * is a risk worth taking. The consequences of not
16222 			 * hitting SS are far worse than going one more time
16223 			 * into it early on (before we have sent even a IW).
16224 			 * It is highly unlikely that we will have had a loss
16225 			 * before getting the IW out.
16226 			 */
16227 			tp->snd_ssthresh = 0xffffffff;
16228 		}
16229 		/*
16230 		 * Any init based on sequence numbers
16231 		 * should be done in the deferred init path
16232 		 * since we can be CLOSED and not have them
16233 		 * inited when rack_init() is called. We
16234 		 * are not closed so lets call it.
16235 		 */
16236 		rack_deferred_init(tp, rack);
16237 	}
16238 	if ((tp->t_state != TCPS_CLOSED) &&
16239 	    (tp->t_state != TCPS_TIME_WAIT) &&
16240 	    (no_query == 0) &&
16241 	    (tp->snd_una != tp->snd_max))  {
16242 		err = rack_init_outstanding(tp, rack, us_cts, *ptr);
16243 		if (err) {
16244 			*ptr = NULL;
16245 			return(err);
16246 		}
16247 	}
16248 	rack_stop_all_timers(tp, rack);
16249 	/* Setup all the t_flags2 */
16250 	if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
16251 		tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
16252 	else
16253 		tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
16254 	if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
16255 		tp->t_flags2 |= TF2_MBUF_ACKCMP;
16256 	/*
16257 	 * Timers in Rack are kept in microseconds so lets
16258 	 * convert any initial incoming variables
16259 	 * from ticks into usecs. Note that we
16260 	 * also change the values of t_srtt and t_rttvar, if
16261 	 * they are non-zero. They are kept with a 5
16262 	 * bit decimal so we have to carefully convert
16263 	 * these to get the full precision.
16264 	 */
16265 	rack_convert_rtts(tp);
16266 	rack_log_hystart_event(rack, rack->r_ctl.roundends, 20);
16267 	if ((tptoinpcb(tp)->inp_flags & INP_DROPPED) == 0) {
16268 		/* We do not start any timers on DROPPED connections */
16269 		if (tp->t_fb->tfb_chg_query == NULL) {
16270 			rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
16271 		} else {
16272 			struct tcp_query_resp qr;
16273 			int ret;
16274 
16275 			memset(&qr, 0, sizeof(qr));
16276 
16277 			/* Get the misc time stamps and such for rack */
16278 			qr.req = TCP_QUERY_RACK_TIMES;
16279 			ret = (*tp->t_fb->tfb_chg_query)(tp, &qr);
16280 			if (ret == 1) {
16281 				rack->r_ctl.rc_reorder_ts = qr.rack_reorder_ts;
16282 				rack->r_ctl.num_dsack  = qr.rack_num_dsacks;
16283 				rack->r_ctl.rc_tlp_rxt_last_time = qr.rack_rxt_last_time;
16284 				rack->r_ctl.rc_rack_min_rtt = qr.rack_min_rtt;
16285 				rack->rc_rack_rtt = qr.rack_rtt;
16286 				rack->r_ctl.rc_rack_tmit_time = qr.rack_tmit_time;
16287 				rack->r_ctl.rc_sacked = qr.rack_sacked;
16288 				rack->r_ctl.rc_holes_rxt = qr.rack_holes_rxt;
16289 				rack->r_ctl.rc_prr_delivered = qr.rack_prr_delivered;
16290 				rack->r_ctl.rc_prr_recovery_fs = qr.rack_prr_recovery_fs;
16291 				rack->r_ctl.rc_prr_sndcnt = qr.rack_prr_sndcnt;
16292 				rack->r_ctl.rc_prr_out = qr.rack_prr_out;
16293 				if (qr.rack_tlp_out) {
16294 					rack->rc_tlp_in_progress = 1;
16295 					rack->r_ctl.rc_tlp_cnt_out = qr.rack_tlp_cnt_out;
16296 				} else {
16297 					rack->rc_tlp_in_progress = 0;
16298 					rack->r_ctl.rc_tlp_cnt_out = 0;
16299 				}
16300 				if (qr.rack_srtt_measured)
16301 					rack->rc_srtt_measure_made = 1;
16302 				if (qr.rack_in_persist == 1) {
16303 					rack->r_ctl.rc_went_idle_time = qr.rack_time_went_idle;
16304 #ifdef NETFLIX_SHARED_CWND
16305 					if (rack->r_ctl.rc_scw) {
16306 						tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
16307 						rack->rack_scwnd_is_idle = 1;
16308 					}
16309 #endif
16310 					rack->r_ctl.persist_lost_ends = 0;
16311 					rack->probe_not_answered = 0;
16312 					rack->forced_ack = 0;
16313 					tp->t_rxtshift = 0;
16314 					rack->rc_in_persist = 1;
16315 					RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
16316 							   rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
16317 				}
16318 				if (qr.rack_wanted_output)
16319 					rack->r_wanted_output = 1;
16320 				rack_log_chg_info(tp, rack, 6,
16321 						  qr.rack_min_rtt,
16322 						  qr.rack_rtt,
16323 						  qr.rack_reorder_ts);
16324 			}
16325 			/* Get the old stack timers */
16326 			qr.req_param = 0;
16327 			qr.req = TCP_QUERY_TIMERS_UP;
16328 			ret = (*tp->t_fb->tfb_chg_query)(tp, &qr);
16329 			if (ret) {
16330 				/*
16331 				 * non-zero return means we have a timer('s)
16332 				 * to start. Zero means no timer (no keepalive
16333 				 * I suppose).
16334 				 */
16335 				uint32_t tov = 0;
16336 
16337 				rack->r_ctl.rc_hpts_flags = qr.timer_hpts_flags;
16338 				if (qr.timer_hpts_flags & PACE_PKT_OUTPUT) {
16339 					rack->r_ctl.rc_last_output_to = qr.timer_pacing_to;
16340 					if (TSTMP_GT(qr.timer_pacing_to, us_cts))
16341 						tov = qr.timer_pacing_to - us_cts;
16342 					else
16343 						tov = HPTS_TICKS_PER_SLOT;
16344 				}
16345 				if (qr.timer_hpts_flags & PACE_TMR_MASK) {
16346 					rack->r_ctl.rc_timer_exp = qr.timer_timer_exp;
16347 					if (tov == 0) {
16348 						if (TSTMP_GT(qr.timer_timer_exp, us_cts))
16349 							tov = qr.timer_timer_exp - us_cts;
16350 						else
16351 							tov = HPTS_TICKS_PER_SLOT;
16352 					}
16353 				}
16354 				rack_log_chg_info(tp, rack, 4,
16355 						  rack->r_ctl.rc_hpts_flags,
16356 						  rack->r_ctl.rc_last_output_to,
16357 						  rack->r_ctl.rc_timer_exp);
16358 				if (tov) {
16359 					struct hpts_diag diag;
16360 
16361 					(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(tov),
16362 								   __LINE__, &diag);
16363 					rack_log_hpts_diag(rack, us_cts, &diag, &rack->r_ctl.act_rcv_time);
16364 				}
16365 			}
16366 		}
16367 		rack_log_rtt_shrinks(rack,  us_cts,  tp->t_rxtcur,
16368 				     __LINE__, RACK_RTTS_INIT);
16369 	}
16370 	return (0);
16371 }
16372 
16373 static int
16374 rack_handoff_ok(struct tcpcb *tp)
16375 {
16376 	if ((tp->t_state == TCPS_CLOSED) ||
16377 	    (tp->t_state == TCPS_LISTEN)) {
16378 		/* Sure no problem though it may not stick */
16379 		return (0);
16380 	}
16381 	if ((tp->t_state == TCPS_SYN_SENT) ||
16382 	    (tp->t_state == TCPS_SYN_RECEIVED)) {
16383 		/*
16384 		 * We really don't know if you support sack,
16385 		 * you have to get to ESTAB or beyond to tell.
16386 		 */
16387 		return (EAGAIN);
16388 	}
16389 	if ((tp->t_flags & TF_SENTFIN) && ((tp->snd_max - tp->snd_una) > 1)) {
16390 		/*
16391 		 * Rack will only send a FIN after all data is acknowledged.
16392 		 * So in this case we have more data outstanding. We can't
16393 		 * switch stacks until either all data and only the FIN
16394 		 * is left (in which case rack_init() now knows how
16395 		 * to deal with that) <or> all is acknowledged and we
16396 		 * are only left with incoming data, though why you
16397 		 * would want to switch to rack after all data is acknowledged
16398 		 * I have no idea (rrs)!
16399 		 */
16400 		return (EAGAIN);
16401 	}
16402 	if ((tp->t_flags & TF_SACK_PERMIT) || rack_sack_not_required){
16403 		return (0);
16404 	}
16405 	/*
16406 	 * If we reach here we don't do SACK on this connection so we can
16407 	 * never do rack.
16408 	 */
16409 	return (EINVAL);
16410 }
16411 
16412 static void
16413 rack_fini(struct tcpcb *tp, int32_t tcb_is_purged)
16414 {
16415 
16416 	if (tp->t_fb_ptr) {
16417 		uint32_t cnt_free = 0;
16418 		struct tcp_rack *rack;
16419 		struct rack_sendmap *rsm;
16420 
16421 		tcp_handle_orphaned_packets(tp);
16422 		tp->t_flags &= ~TF_FORCEDATA;
16423 		rack = (struct tcp_rack *)tp->t_fb_ptr;
16424 		rack_log_pacing_delay_calc(rack,
16425 					   0,
16426 					   0,
16427 					   0,
16428 					   rack_get_gp_est(rack), /* delRate */
16429 					   rack_get_lt_bw(rack), /* rttProp */
16430 					   20, __LINE__, NULL, 0);
16431 #ifdef NETFLIX_SHARED_CWND
16432 		if (rack->r_ctl.rc_scw) {
16433 			uint32_t limit;
16434 
16435 			if (rack->r_limit_scw)
16436 				limit = max(1, rack->r_ctl.rc_lowest_us_rtt);
16437 			else
16438 				limit = 0;
16439 			tcp_shared_cwnd_free_full(tp, rack->r_ctl.rc_scw,
16440 						  rack->r_ctl.rc_scw_index,
16441 						  limit);
16442 			rack->r_ctl.rc_scw = NULL;
16443 		}
16444 #endif
16445 		if (rack->r_ctl.fsb.tcp_ip_hdr) {
16446 			free(rack->r_ctl.fsb.tcp_ip_hdr, M_TCPFSB);
16447 			rack->r_ctl.fsb.tcp_ip_hdr = NULL;
16448 			rack->r_ctl.fsb.th = NULL;
16449 		}
16450 		if (rack->rc_always_pace == 1) {
16451 			rack_remove_pacing(rack);
16452 		}
16453 		/* Clean up any options if they were not applied */
16454 		while (!TAILQ_EMPTY(&rack->r_ctl.opt_list)) {
16455 			struct deferred_opt_list *dol;
16456 
16457 			dol = TAILQ_FIRST(&rack->r_ctl.opt_list);
16458 			TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next);
16459 			free(dol, M_TCPDO);
16460 		}
16461 		/* rack does not use force data but other stacks may clear it */
16462 		if (rack->r_ctl.crte != NULL) {
16463 			tcp_rel_pacing_rate(rack->r_ctl.crte, tp);
16464 			rack->rack_hdrw_pacing = 0;
16465 			rack->r_ctl.crte = NULL;
16466 		}
16467 #ifdef TCP_BLACKBOX
16468 		tcp_log_flowend(tp);
16469 #endif
16470 		/*
16471 		 * Lets take a different approach to purging just
16472 		 * get each one and free it like a cum-ack would and
16473 		 * not use a foreach loop.
16474 		 */
16475 		rsm = tqhash_min(rack->r_ctl.tqh);
16476 		while (rsm) {
16477 			tqhash_remove(rack->r_ctl.tqh, rsm, REMOVE_TYPE_CUMACK);
16478 			rack->r_ctl.rc_num_maps_alloced--;
16479 			uma_zfree(rack_zone, rsm);
16480 			rsm = tqhash_min(rack->r_ctl.tqh);
16481 		}
16482 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
16483 		while (rsm) {
16484 			TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
16485 			rack->r_ctl.rc_num_maps_alloced--;
16486 			rack->rc_free_cnt--;
16487 			cnt_free++;
16488 			uma_zfree(rack_zone, rsm);
16489 			rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
16490 		}
16491 		if (rack->r_ctl.pcm_s != NULL) {
16492 			free(rack->r_ctl.pcm_s, M_TCPPCM);
16493 			rack->r_ctl.pcm_s = NULL;
16494 			rack->r_ctl.pcm_i.cnt_alloc = 0;
16495 			rack->r_ctl.pcm_i.cnt = 0;
16496 		}
16497 		if ((rack->r_ctl.rc_num_maps_alloced > 0) &&
16498 		    (tcp_bblogging_on(tp))) {
16499 			union tcp_log_stackspecific log;
16500 			struct timeval tv;
16501 
16502 			memset(&log.u_bbr, 0, sizeof(log.u_bbr));
16503 			log.u_bbr.flex8 = 10;
16504 			log.u_bbr.flex1 = rack->r_ctl.rc_num_maps_alloced;
16505 			log.u_bbr.flex2 = rack->rc_free_cnt;
16506 			log.u_bbr.flex3 = cnt_free;
16507 			log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
16508 			rsm = tqhash_min(rack->r_ctl.tqh);
16509 			log.u_bbr.delRate = (uint64_t)rsm;
16510 			rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
16511 			log.u_bbr.cur_del_rate = (uint64_t)rsm;
16512 			log.u_bbr.timeStamp = tcp_get_usecs(&tv);
16513 			log.u_bbr.pkt_epoch = __LINE__;
16514 			(void)tcp_log_event(tp, NULL, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
16515 					     0, &log, false, NULL, NULL, 0, &tv);
16516 		}
16517 		KASSERT((rack->r_ctl.rc_num_maps_alloced == 0),
16518 			("rack:%p num_aloc:%u after freeing all?",
16519 			 rack,
16520 			 rack->r_ctl.rc_num_maps_alloced));
16521 		rack->rc_free_cnt = 0;
16522 		free(rack->r_ctl.tqh, M_TCPFSB);
16523 		rack->r_ctl.tqh = NULL;
16524 		uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
16525 		tp->t_fb_ptr = NULL;
16526 	}
16527 	/* Make sure snd_nxt is correctly set */
16528 	tp->snd_nxt = tp->snd_max;
16529 }
16530 
16531 static void
16532 rack_set_state(struct tcpcb *tp, struct tcp_rack *rack)
16533 {
16534 	if ((rack->r_state == TCPS_CLOSED) && (tp->t_state != TCPS_CLOSED)) {
16535 		rack->r_is_v6 = (tptoinpcb(tp)->inp_vflag & INP_IPV6) != 0;
16536 	}
16537 	switch (tp->t_state) {
16538 	case TCPS_SYN_SENT:
16539 		rack->r_state = TCPS_SYN_SENT;
16540 		rack->r_substate = rack_do_syn_sent;
16541 		break;
16542 	case TCPS_SYN_RECEIVED:
16543 		rack->r_state = TCPS_SYN_RECEIVED;
16544 		rack->r_substate = rack_do_syn_recv;
16545 		break;
16546 	case TCPS_ESTABLISHED:
16547 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
16548 		rack->r_state = TCPS_ESTABLISHED;
16549 		rack->r_substate = rack_do_established;
16550 		break;
16551 	case TCPS_CLOSE_WAIT:
16552 		rack->r_state = TCPS_CLOSE_WAIT;
16553 		rack->r_substate = rack_do_close_wait;
16554 		break;
16555 	case TCPS_FIN_WAIT_1:
16556 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
16557 		rack->r_state = TCPS_FIN_WAIT_1;
16558 		rack->r_substate = rack_do_fin_wait_1;
16559 		break;
16560 	case TCPS_CLOSING:
16561 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
16562 		rack->r_state = TCPS_CLOSING;
16563 		rack->r_substate = rack_do_closing;
16564 		break;
16565 	case TCPS_LAST_ACK:
16566 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
16567 		rack->r_state = TCPS_LAST_ACK;
16568 		rack->r_substate = rack_do_lastack;
16569 		break;
16570 	case TCPS_FIN_WAIT_2:
16571 		rack->r_state = TCPS_FIN_WAIT_2;
16572 		rack->r_substate = rack_do_fin_wait_2;
16573 		break;
16574 	case TCPS_LISTEN:
16575 	case TCPS_CLOSED:
16576 	case TCPS_TIME_WAIT:
16577 	default:
16578 		break;
16579 	};
16580 	if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
16581 		rack->rc_tp->t_flags2 |= TF2_MBUF_ACKCMP;
16582 
16583 }
16584 
16585 static void
16586 rack_timer_audit(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb)
16587 {
16588 	/*
16589 	 * We received an ack, and then did not
16590 	 * call send or were bounced out due to the
16591 	 * hpts was running. Now a timer is up as well, is
16592 	 * it the right timer?
16593 	 */
16594 	struct rack_sendmap *rsm;
16595 	int tmr_up;
16596 
16597 	tmr_up = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
16598 	if (tcp_in_hpts(rack->rc_tp) == 0) {
16599 		/*
16600 		 * Ok we probably need some timer up, but no
16601 		 * matter what the mask we are not in hpts. We
16602 		 * may have received an old ack and thus did nothing.
16603 		 */
16604 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
16605 		rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
16606 		return;
16607 	}
16608 	if (rack->rc_in_persist && (tmr_up == PACE_TMR_PERSIT))
16609 		return;
16610 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
16611 	if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) &&
16612 	    (tmr_up == PACE_TMR_RXT)) {
16613 		/* Should be an RXT */
16614 		return;
16615 	}
16616 	if (rsm == NULL) {
16617 		/* Nothing outstanding? */
16618 		if (tp->t_flags & TF_DELACK) {
16619 			if (tmr_up == PACE_TMR_DELACK)
16620 				/* We are supposed to have delayed ack up and we do */
16621 				return;
16622 		} else if (sbavail(&tptosocket(tp)->so_snd) && (tmr_up == PACE_TMR_RXT)) {
16623 			/*
16624 			 * if we hit enobufs then we would expect the possibility
16625 			 * of nothing outstanding and the RXT up (and the hptsi timer).
16626 			 */
16627 			return;
16628 		} else if (((V_tcp_always_keepalive ||
16629 			     rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
16630 			    (tp->t_state <= TCPS_CLOSING)) &&
16631 			   (tmr_up == PACE_TMR_KEEP) &&
16632 			   (tp->snd_max == tp->snd_una)) {
16633 			/* We should have keep alive up and we do */
16634 			return;
16635 		}
16636 	}
16637 	if (SEQ_GT(tp->snd_max, tp->snd_una) &&
16638 		   ((tmr_up == PACE_TMR_TLP) ||
16639 		    (tmr_up == PACE_TMR_RACK) ||
16640 		    (tmr_up == PACE_TMR_RXT))) {
16641 		/*
16642 		 * Either a Rack, TLP or RXT is fine if  we
16643 		 * have outstanding data.
16644 		 */
16645 		return;
16646 	} else if (tmr_up == PACE_TMR_DELACK) {
16647 		/*
16648 		 * If the delayed ack was going to go off
16649 		 * before the rtx/tlp/rack timer were going to
16650 		 * expire, then that would be the timer in control.
16651 		 * Note we don't check the time here trusting the
16652 		 * code is correct.
16653 		 */
16654 		return;
16655 	}
16656 	/*
16657 	 * Ok the timer originally started is not what we want now.
16658 	 * We will force the hpts to be stopped if any, and restart
16659 	 * with the slot set to what was in the saved slot.
16660 	 */
16661 	if (tcp_in_hpts(rack->rc_tp)) {
16662 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
16663 			uint32_t us_cts;
16664 
16665 			us_cts = tcp_get_usecs(NULL);
16666 			if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
16667 				rack->r_early = 1;
16668 				rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
16669 			}
16670 			rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
16671 		}
16672 		tcp_hpts_remove(rack->rc_tp);
16673 	}
16674 	rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
16675 	rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
16676 }
16677 
16678 
16679 static void
16680 rack_do_win_updates(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tiwin, uint32_t seq, uint32_t ack, uint32_t cts)
16681 {
16682 	if ((SEQ_LT(tp->snd_wl1, seq) ||
16683 	    (tp->snd_wl1 == seq && (SEQ_LT(tp->snd_wl2, ack) ||
16684 	    (tp->snd_wl2 == ack && tiwin > tp->snd_wnd))))) {
16685 		/* keep track of pure window updates */
16686 		if ((tp->snd_wl2 == ack) && (tiwin > tp->snd_wnd))
16687 			KMOD_TCPSTAT_INC(tcps_rcvwinupd);
16688 		tp->snd_wnd = tiwin;
16689 		rack_validate_fo_sendwin_up(tp, rack);
16690 		tp->snd_wl1 = seq;
16691 		tp->snd_wl2 = ack;
16692 		if (tp->snd_wnd > tp->max_sndwnd)
16693 			tp->max_sndwnd = tp->snd_wnd;
16694 	    rack->r_wanted_output = 1;
16695 	} else if ((tp->snd_wl2 == ack) && (tiwin < tp->snd_wnd)) {
16696 		tp->snd_wnd = tiwin;
16697 		rack_validate_fo_sendwin_up(tp, rack);
16698 		tp->snd_wl1 = seq;
16699 		tp->snd_wl2 = ack;
16700 	} else {
16701 		/* Not a valid win update */
16702 		return;
16703 	}
16704 	if (tp->snd_wnd > tp->max_sndwnd)
16705 		tp->max_sndwnd = tp->snd_wnd;
16706 	/* Do we exit persists? */
16707 	if ((rack->rc_in_persist != 0) &&
16708 	    (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
16709 				rack->r_ctl.rc_pace_min_segs))) {
16710 		rack_exit_persist(tp, rack, cts);
16711 	}
16712 	/* Do we enter persists? */
16713 	if ((rack->rc_in_persist == 0) &&
16714 	    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
16715 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
16716 	    ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) &&
16717 	    sbavail(&tptosocket(tp)->so_snd) &&
16718 	    (sbavail(&tptosocket(tp)->so_snd) > tp->snd_wnd)) {
16719 		/*
16720 		 * Here the rwnd is less than
16721 		 * the pacing size, we are established,
16722 		 * nothing is outstanding, and there is
16723 		 * data to send. Enter persists.
16724 		 */
16725 		rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, ack);
16726 	}
16727 }
16728 
16729 static void
16730 rack_log_input_packet(struct tcpcb *tp, struct tcp_rack *rack, struct tcp_ackent *ae, int ackval, uint32_t high_seq)
16731 {
16732 
16733 	if (tcp_bblogging_on(rack->rc_tp)) {
16734 		struct inpcb *inp = tptoinpcb(tp);
16735 		union tcp_log_stackspecific log;
16736 		struct timeval ltv;
16737 		char tcp_hdr_buf[60];
16738 		struct tcphdr *th;
16739 		struct timespec ts;
16740 		uint32_t orig_snd_una;
16741 		uint8_t xx = 0;
16742 
16743 #ifdef TCP_REQUEST_TRK
16744 		struct tcp_sendfile_track *tcp_req;
16745 
16746 		if (SEQ_GT(ae->ack, tp->snd_una)) {
16747 			tcp_req = tcp_req_find_req_for_seq(tp, (ae->ack-1));
16748 		} else {
16749 			tcp_req = tcp_req_find_req_for_seq(tp, ae->ack);
16750 		}
16751 #endif
16752 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
16753 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
16754 		if (rack->rack_no_prr == 0)
16755 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
16756 		else
16757 			log.u_bbr.flex1 = 0;
16758 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
16759 		log.u_bbr.use_lt_bw <<= 1;
16760 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
16761 		log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced;
16762 		log.u_bbr.bbr_state = rack->rc_free_cnt;
16763 		log.u_bbr.inflight = ctf_flight_size(tp, rack->r_ctl.rc_sacked);
16764 		log.u_bbr.pkts_out = tp->t_maxseg;
16765 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
16766 		log.u_bbr.flex7 = 1;
16767 		log.u_bbr.lost = ae->flags;
16768 		log.u_bbr.cwnd_gain = ackval;
16769 		log.u_bbr.pacing_gain = 0x2;
16770 		if (ae->flags & TSTMP_HDWR) {
16771 			/* Record the hardware timestamp if present */
16772 			log.u_bbr.flex3 = M_TSTMP;
16773 			ts.tv_sec = ae->timestamp / 1000000000;
16774 			ts.tv_nsec = ae->timestamp % 1000000000;
16775 			ltv.tv_sec = ts.tv_sec;
16776 			ltv.tv_usec = ts.tv_nsec / 1000;
16777 			log.u_bbr.lt_epoch = tcp_tv_to_usectick(&ltv);
16778 		} else if (ae->flags & TSTMP_LRO) {
16779 			/* Record the LRO the arrival timestamp */
16780 			log.u_bbr.flex3 = M_TSTMP_LRO;
16781 			ts.tv_sec = ae->timestamp / 1000000000;
16782 			ts.tv_nsec = ae->timestamp % 1000000000;
16783 			ltv.tv_sec = ts.tv_sec;
16784 			ltv.tv_usec = ts.tv_nsec / 1000;
16785 			log.u_bbr.flex5 = tcp_tv_to_usectick(&ltv);
16786 		}
16787 		log.u_bbr.timeStamp = tcp_get_usecs(&ltv);
16788 		/* Log the rcv time */
16789 		log.u_bbr.delRate = ae->timestamp;
16790 #ifdef TCP_REQUEST_TRK
16791 		log.u_bbr.applimited = tp->t_tcpreq_closed;
16792 		log.u_bbr.applimited <<= 8;
16793 		log.u_bbr.applimited |= tp->t_tcpreq_open;
16794 		log.u_bbr.applimited <<= 8;
16795 		log.u_bbr.applimited |= tp->t_tcpreq_req;
16796 		if (tcp_req) {
16797 			/* Copy out any client req info */
16798 			/* seconds */
16799 			log.u_bbr.pkt_epoch = (tcp_req->localtime / HPTS_USEC_IN_SEC);
16800 			/* useconds */
16801 			log.u_bbr.delivered = (tcp_req->localtime % HPTS_USEC_IN_SEC);
16802 			log.u_bbr.rttProp = tcp_req->timestamp;
16803 			log.u_bbr.cur_del_rate = tcp_req->start;
16804 			if (tcp_req->flags & TCP_TRK_TRACK_FLG_OPEN) {
16805 				log.u_bbr.flex8 |= 1;
16806 			} else {
16807 				log.u_bbr.flex8 |= 2;
16808 				log.u_bbr.bw_inuse = tcp_req->end;
16809 			}
16810 			log.u_bbr.flex6 = tcp_req->start_seq;
16811 			if (tcp_req->flags & TCP_TRK_TRACK_FLG_COMP) {
16812 				log.u_bbr.flex8 |= 4;
16813 				log.u_bbr.epoch = tcp_req->end_seq;
16814 			}
16815 		}
16816 #endif
16817 		memset(tcp_hdr_buf, 0, sizeof(tcp_hdr_buf));
16818 		th = (struct tcphdr *)tcp_hdr_buf;
16819 		th->th_seq = ae->seq;
16820 		th->th_ack = ae->ack;
16821 		th->th_win = ae->win;
16822 		/* Now fill in the ports */
16823 		th->th_sport = inp->inp_fport;
16824 		th->th_dport = inp->inp_lport;
16825 		tcp_set_flags(th, ae->flags);
16826 		/* Now do we have a timestamp option? */
16827 		if (ae->flags & HAS_TSTMP) {
16828 			u_char *cp;
16829 			uint32_t val;
16830 
16831 			th->th_off = ((sizeof(struct tcphdr) + TCPOLEN_TSTAMP_APPA) >> 2);
16832 			cp = (u_char *)(th + 1);
16833 			*cp = TCPOPT_NOP;
16834 			cp++;
16835 			*cp = TCPOPT_NOP;
16836 			cp++;
16837 			*cp = TCPOPT_TIMESTAMP;
16838 			cp++;
16839 			*cp = TCPOLEN_TIMESTAMP;
16840 			cp++;
16841 			val = htonl(ae->ts_value);
16842 			bcopy((char *)&val,
16843 			      (char *)cp, sizeof(uint32_t));
16844 			val = htonl(ae->ts_echo);
16845 			bcopy((char *)&val,
16846 			      (char *)(cp + 4), sizeof(uint32_t));
16847 		} else
16848 			th->th_off = (sizeof(struct tcphdr) >> 2);
16849 
16850 		/*
16851 		 * For sane logging we need to play a little trick.
16852 		 * If the ack were fully processed we would have moved
16853 		 * snd_una to high_seq, but since compressed acks are
16854 		 * processed in two phases, at this point (logging) snd_una
16855 		 * won't be advanced. So we would see multiple acks showing
16856 		 * the advancement. We can prevent that by "pretending" that
16857 		 * snd_una was advanced and then un-advancing it so that the
16858 		 * logging code has the right value for tlb_snd_una.
16859 		 */
16860 		if (tp->snd_una != high_seq) {
16861 			orig_snd_una = tp->snd_una;
16862 			tp->snd_una = high_seq;
16863 			xx = 1;
16864 		} else
16865 			xx = 0;
16866 		TCP_LOG_EVENTP(tp, th,
16867 			       &tptosocket(tp)->so_rcv,
16868 			       &tptosocket(tp)->so_snd, TCP_LOG_IN, 0,
16869 			       0, &log, true, &ltv);
16870 		if (xx) {
16871 			tp->snd_una = orig_snd_una;
16872 		}
16873 	}
16874 
16875 }
16876 
16877 static void
16878 rack_handle_probe_response(struct tcp_rack *rack, uint32_t tiwin, uint32_t us_cts)
16879 {
16880 	uint32_t us_rtt;
16881 	/*
16882 	 * A persist or keep-alive was forced out, update our
16883 	 * min rtt time. Note now worry about lost responses.
16884 	 * When a subsequent keep-alive or persist times out
16885 	 * and forced_ack is still on, then the last probe
16886 	 * was not responded to. In such cases we have a
16887 	 * sysctl that controls the behavior. Either we apply
16888 	 * the rtt but with reduced confidence (0). Or we just
16889 	 * plain don't apply the rtt estimate. Having data flow
16890 	 * will clear the probe_not_answered flag i.e. cum-ack
16891 	 * move forward <or> exiting and reentering persists.
16892 	 */
16893 
16894 	rack->forced_ack = 0;
16895 	rack->rc_tp->t_rxtshift = 0;
16896 	if ((rack->rc_in_persist &&
16897 	     (tiwin == rack->rc_tp->snd_wnd)) ||
16898 	    (rack->rc_in_persist == 0)) {
16899 		/*
16900 		 * In persists only apply the RTT update if this is
16901 		 * a response to our window probe. And that
16902 		 * means the rwnd sent must match the current
16903 		 * snd_wnd. If it does not, then we got a
16904 		 * window update ack instead. For keepalive
16905 		 * we allow the answer no matter what the window.
16906 		 *
16907 		 * Note that if the probe_not_answered is set then
16908 		 * the forced_ack_ts is the oldest one i.e. the first
16909 		 * probe sent that might have been lost. This assures
16910 		 * us that if we do calculate an RTT it is longer not
16911 		 * some short thing.
16912 		 */
16913 		if (rack->rc_in_persist)
16914 			counter_u64_add(rack_persists_acks, 1);
16915 		us_rtt = us_cts - rack->r_ctl.forced_ack_ts;
16916 		if (us_rtt == 0)
16917 			us_rtt = 1;
16918 		if (rack->probe_not_answered == 0) {
16919 			rack_apply_updated_usrtt(rack, us_rtt, us_cts);
16920 			tcp_rack_xmit_timer(rack, us_rtt, 0, us_rtt, 3, NULL, 1);
16921 		} else {
16922 			/* We have a retransmitted probe here too */
16923 			if (rack_apply_rtt_with_reduced_conf) {
16924 				rack_apply_updated_usrtt(rack, us_rtt, us_cts);
16925 				tcp_rack_xmit_timer(rack, us_rtt, 0, us_rtt, 0, NULL, 1);
16926 			}
16927 		}
16928 	}
16929 }
16930 
16931 static void
16932 rack_new_round_starts(struct tcpcb *tp, struct tcp_rack *rack, uint32_t high_seq)
16933 {
16934 	/*
16935 	 * The next send has occurred mark the end of the round
16936 	 * as when that data gets acknowledged. We can
16937 	 * also do common things we might need to do when
16938 	 * a round begins.
16939 	 */
16940 	rack->r_ctl.roundends = tp->snd_max;
16941 	rack->rc_new_rnd_needed = 0;
16942 	rack_log_hystart_event(rack, tp->snd_max, 4);
16943 }
16944 
16945 
16946 static void
16947 rack_log_pcm(struct tcp_rack *rack, uint8_t mod, uint32_t flex1, uint32_t flex2,
16948 	     uint32_t flex3)
16949 {
16950 	if (tcp_bblogging_on(rack->rc_tp)) {
16951 		union tcp_log_stackspecific log;
16952 		struct timeval tv;
16953 
16954 		(void)tcp_get_usecs(&tv);
16955 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
16956 		log.u_bbr.timeStamp = tcp_tv_to_usectick(&tv);
16957 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
16958 		log.u_bbr.flex8 = mod;
16959 		log.u_bbr.flex1 = flex1;
16960 		log.u_bbr.flex2 = flex2;
16961 		log.u_bbr.flex3 = flex3;
16962 		log.u_bbr.flex4 = rack_pcm_every_n_rounds;
16963 		log.u_bbr.flex5 = rack->r_ctl.pcm_idle_rounds;
16964 		log.u_bbr.bbr_substate = rack->pcm_needed;
16965 		log.u_bbr.bbr_substate <<= 1;
16966 		log.u_bbr.bbr_substate |= rack->pcm_in_progress;
16967 		log.u_bbr.bbr_substate <<= 1;
16968 		log.u_bbr.bbr_substate |= rack->pcm_enabled; /* bits are NIE for Needed, Inprogress, Enabled */
16969 		(void)tcp_log_event(rack->rc_tp, NULL, NULL, NULL, TCP_PCM_MEASURE, ERRNO_UNK,
16970 				    0, &log, false, NULL, NULL, 0, &tv);
16971 	}
16972 }
16973 
16974 static void
16975 rack_new_round_setup(struct tcpcb *tp, struct tcp_rack *rack, uint32_t high_seq)
16976 {
16977 	/*
16978 	 * The round (current_round) has ended. We now
16979 	 * setup for the next round by incrementing the
16980 	 * round numnber and doing any round specific
16981 	 * things.
16982 	 */
16983 	rack_log_hystart_event(rack, high_seq, 21);
16984 	rack->r_ctl.current_round++;
16985 	/* New round (current_round) begins at next send */
16986 	rack->rc_new_rnd_needed = 1;
16987 	if ((rack->pcm_enabled == 1) &&
16988 	    (rack->pcm_needed == 0) &&
16989 	    (rack->pcm_in_progress == 0)) {
16990 		/*
16991 		 * If we have enabled PCM, then we need to
16992 		 * check if the round has adanced to the state
16993 		 * where one is required.
16994 		 */
16995 		int rnds;
16996 
16997 		rnds = rack->r_ctl.current_round - rack->r_ctl.last_pcm_round;
16998 		if ((rnds + rack->r_ctl.pcm_idle_rounds) >= rack_pcm_every_n_rounds) {
16999 			rack->pcm_needed = 1;
17000 			rack_log_pcm(rack, 3, rack->r_ctl.last_pcm_round, rack_pcm_every_n_rounds, rack->r_ctl.current_round );
17001 		} else if (rack_verbose_logging) {
17002 			rack_log_pcm(rack, 3, rack->r_ctl.last_pcm_round, rack_pcm_every_n_rounds, rack->r_ctl.current_round );
17003 		}
17004 	}
17005 	if (tp->t_ccv.flags & CCF_HYSTART_ALLOWED) {
17006 		/* We have hystart enabled send the round info in */
17007 		if (CC_ALGO(tp)->newround != NULL) {
17008 			CC_ALGO(tp)->newround(&tp->t_ccv, rack->r_ctl.current_round);
17009 		}
17010 	}
17011 	/*
17012 	 * For DGP an initial startup check. We want to validate
17013 	 * that we are not just pushing on slow-start and just
17014 	 * not gaining.. i.e. filling buffers without getting any
17015 	 * boost in b/w during the inital slow-start.
17016 	 */
17017 	if (rack->dgp_on &&
17018 	    (rack->rc_initial_ss_comp == 0) &&
17019 	    (tp->snd_cwnd < tp->snd_ssthresh) &&
17020 	    (rack->r_ctl.num_measurements >= RACK_REQ_AVG) &&
17021 	    (rack->r_ctl.gp_rnd_thresh > 0) &&
17022 	    ((rack->r_ctl.current_round - rack->r_ctl.last_rnd_of_gp_rise) >= rack->r_ctl.gp_rnd_thresh)) {
17023 
17024 		/*
17025 		 * We are in the initial SS and we have hd rack_rnd_cnt_req rounds(def:5) where
17026 		 * we have not gained the required amount in the gp_est (120.0% aka 1200). Lets
17027 		 * exit SS.
17028 		 *
17029 		 * Pick up the flight size now as we enter slowstart (not the
17030 		 * cwnd which may be inflated).
17031 		 */
17032 		rack->rc_initial_ss_comp = 1;
17033 
17034 		if (tcp_bblogging_on(rack->rc_tp)) {
17035 			union tcp_log_stackspecific log;
17036 			struct timeval tv;
17037 
17038 			memset(&log.u_bbr, 0, sizeof(log.u_bbr));
17039 			log.u_bbr.timeStamp = tcp_get_usecs(&tv);
17040 			log.u_bbr.flex1 = rack->r_ctl.current_round;
17041 			log.u_bbr.flex2 = rack->r_ctl.last_rnd_of_gp_rise;
17042 			log.u_bbr.flex3 = rack->r_ctl.gp_rnd_thresh;
17043 			log.u_bbr.flex5 = rack->r_ctl.gate_to_fs;
17044 			log.u_bbr.flex5 = rack->r_ctl.ss_hi_fs;
17045 			log.u_bbr.flex8 = 40;
17046 			(void)tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
17047 					    0, &log, false, NULL, __func__, __LINE__,&tv);
17048 		}
17049 		if ((rack->r_ctl.gate_to_fs == 1) &&
17050 		     (tp->snd_cwnd > rack->r_ctl.ss_hi_fs)) {
17051 			tp->snd_cwnd = rack->r_ctl.ss_hi_fs;
17052 		}
17053 		tp->snd_ssthresh = tp->snd_cwnd - 1;
17054 		/* Turn off any fast output running */
17055 		rack->r_fast_output = 0;
17056 	}
17057 }
17058 
17059 static int
17060 rack_do_compressed_ack_processing(struct tcpcb *tp, struct socket *so, struct mbuf *m, int nxt_pkt, struct timeval *tv)
17061 {
17062 	/*
17063 	 * Handle a "special" compressed ack mbuf. Each incoming
17064 	 * ack has only four possible dispositions:
17065 	 *
17066 	 * A) It moves the cum-ack forward
17067 	 * B) It is behind the cum-ack.
17068 	 * C) It is a window-update ack.
17069 	 * D) It is a dup-ack.
17070 	 *
17071 	 * Note that we can have between 1 -> TCP_COMP_ACK_ENTRIES
17072 	 * in the incoming mbuf. We also need to still pay attention
17073 	 * to nxt_pkt since there may be another packet after this
17074 	 * one.
17075 	 */
17076 #ifdef TCP_ACCOUNTING
17077 	uint64_t ts_val;
17078 	uint64_t rdstc;
17079 #endif
17080 	int segsiz;
17081 	struct timespec ts;
17082 	struct tcp_rack *rack;
17083 	struct tcp_ackent *ae;
17084 	uint32_t tiwin, ms_cts, cts, acked, acked_amount, high_seq, win_seq, the_win, win_upd_ack;
17085 	int cnt, i, did_out, ourfinisacked = 0;
17086 	struct tcpopt to_holder, *to = NULL;
17087 #ifdef TCP_ACCOUNTING
17088 	int win_up_req = 0;
17089 #endif
17090 	int nsegs = 0;
17091 	int under_pacing = 0;
17092 	int post_recovery = 0;
17093 #ifdef TCP_ACCOUNTING
17094 	sched_pin();
17095 #endif
17096 	rack = (struct tcp_rack *)tp->t_fb_ptr;
17097 	if (rack->gp_ready &&
17098 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT))
17099 		under_pacing = 1;
17100 
17101 	if (rack->r_state != tp->t_state)
17102 		rack_set_state(tp, rack);
17103 	if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
17104 	    (tp->t_flags & TF_GPUTINPROG)) {
17105 		/*
17106 		 * We have a goodput in progress
17107 		 * and we have entered a late state.
17108 		 * Do we have enough data in the sb
17109 		 * to handle the GPUT request?
17110 		 */
17111 		uint32_t bytes;
17112 
17113 		bytes = tp->gput_ack - tp->gput_seq;
17114 		if (SEQ_GT(tp->gput_seq, tp->snd_una))
17115 			bytes += tp->gput_seq - tp->snd_una;
17116 		if (bytes > sbavail(&tptosocket(tp)->so_snd)) {
17117 			/*
17118 			 * There are not enough bytes in the socket
17119 			 * buffer that have been sent to cover this
17120 			 * measurement. Cancel it.
17121 			 */
17122 			rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
17123 						   rack->r_ctl.rc_gp_srtt /*flex1*/,
17124 						   tp->gput_seq,
17125 						   0, 0, 18, __LINE__, NULL, 0);
17126 			tp->t_flags &= ~TF_GPUTINPROG;
17127 		}
17128 	}
17129 	to = &to_holder;
17130 	to->to_flags = 0;
17131 	KASSERT((m->m_len >= sizeof(struct tcp_ackent)),
17132 		("tp:%p m_cmpack:%p with invalid len:%u", tp, m, m->m_len));
17133 	cnt = m->m_len / sizeof(struct tcp_ackent);
17134 	counter_u64_add(rack_multi_single_eq, cnt);
17135 	high_seq = tp->snd_una;
17136 	the_win = tp->snd_wnd;
17137 	win_seq = tp->snd_wl1;
17138 	win_upd_ack = tp->snd_wl2;
17139 	cts = tcp_tv_to_usectick(tv);
17140 	ms_cts = tcp_tv_to_mssectick(tv);
17141 	rack->r_ctl.rc_rcvtime = cts;
17142 	segsiz = ctf_fixed_maxseg(tp);
17143 	if ((rack->rc_gp_dyn_mul) &&
17144 	    (rack->use_fixed_rate == 0) &&
17145 	    (rack->rc_always_pace)) {
17146 		/* Check in on probertt */
17147 		rack_check_probe_rtt(rack, cts);
17148 	}
17149 	for (i = 0; i < cnt; i++) {
17150 #ifdef TCP_ACCOUNTING
17151 		ts_val = get_cyclecount();
17152 #endif
17153 		rack_clear_rate_sample(rack);
17154 		ae = ((mtod(m, struct tcp_ackent *)) + i);
17155 		if (ae->flags & TH_FIN)
17156 			rack_log_pacing_delay_calc(rack,
17157 						   0,
17158 						   0,
17159 						   0,
17160 						   rack_get_gp_est(rack), /* delRate */
17161 						   rack_get_lt_bw(rack), /* rttProp */
17162 						   20, __LINE__, NULL, 0);
17163 		/* Setup the window */
17164 		tiwin = ae->win << tp->snd_scale;
17165 		if (tiwin > rack->r_ctl.rc_high_rwnd)
17166 			rack->r_ctl.rc_high_rwnd = tiwin;
17167 		/* figure out the type of ack */
17168 		if (SEQ_LT(ae->ack, high_seq)) {
17169 			/* Case B*/
17170 			ae->ack_val_set = ACK_BEHIND;
17171 		} else if (SEQ_GT(ae->ack, high_seq)) {
17172 			/* Case A */
17173 			ae->ack_val_set = ACK_CUMACK;
17174 		} else if ((tiwin == the_win) && (rack->rc_in_persist == 0)){
17175 			/* Case D */
17176 			ae->ack_val_set = ACK_DUPACK;
17177 		} else {
17178 			/* Case C */
17179 			ae->ack_val_set = ACK_RWND;
17180 		}
17181 		if (rack->sack_attack_disable > 0) {
17182 			rack_log_type_bbrsnd(rack, 0, 0, cts, tv, __LINE__);
17183 			rack->r_ctl.ack_during_sd++;
17184 		}
17185 		rack_log_input_packet(tp, rack, ae, ae->ack_val_set, high_seq);
17186 		/* Validate timestamp */
17187 		if (ae->flags & HAS_TSTMP) {
17188 			/* Setup for a timestamp */
17189 			to->to_flags = TOF_TS;
17190 			ae->ts_echo -= tp->ts_offset;
17191 			to->to_tsecr = ae->ts_echo;
17192 			to->to_tsval = ae->ts_value;
17193 			/*
17194 			 * If echoed timestamp is later than the current time, fall back to
17195 			 * non RFC1323 RTT calculation.  Normalize timestamp if syncookies
17196 			 * were used when this connection was established.
17197 			 */
17198 			if (TSTMP_GT(ae->ts_echo, ms_cts))
17199 				to->to_tsecr = 0;
17200 			if (tp->ts_recent &&
17201 			    TSTMP_LT(ae->ts_value, tp->ts_recent)) {
17202 				if (ctf_ts_check_ac(tp, (ae->flags & 0xff))) {
17203 #ifdef TCP_ACCOUNTING
17204 					rdstc = get_cyclecount();
17205 					if (rdstc > ts_val) {
17206 						if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17207 							tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val);
17208 						}
17209 					}
17210 #endif
17211 					continue;
17212 				}
17213 			}
17214 			if (SEQ_LEQ(ae->seq, tp->last_ack_sent) &&
17215 			    SEQ_LEQ(tp->last_ack_sent, ae->seq)) {
17216 				tp->ts_recent_age = tcp_ts_getticks();
17217 				tp->ts_recent = ae->ts_value;
17218 			}
17219 		} else {
17220 			/* Setup for a no options */
17221 			to->to_flags = 0;
17222 		}
17223 		/* Update the rcv time and perform idle reduction possibly */
17224 		if  (tp->t_idle_reduce &&
17225 		     (tp->snd_max == tp->snd_una) &&
17226 		     (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur)) {
17227 			counter_u64_add(rack_input_idle_reduces, 1);
17228 			rack_cc_after_idle(rack, tp);
17229 		}
17230 		tp->t_rcvtime = ticks;
17231 		/* Now what about ECN of a chain of pure ACKs? */
17232 		if (tcp_ecn_input_segment(tp, ae->flags, 0,
17233 			tcp_packets_this_ack(tp, ae->ack),
17234 			ae->codepoint))
17235 			rack_cong_signal(tp, CC_ECN, ae->ack, __LINE__);
17236 #ifdef TCP_ACCOUNTING
17237 		/* Count for the specific type of ack in */
17238 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17239 			tp->tcp_cnt_counters[ae->ack_val_set]++;
17240 		}
17241 #endif
17242 		/*
17243 		 * Note how we could move up these in the determination
17244 		 * above, but we don't so that way the timestamp checks (and ECN)
17245 		 * is done first before we do any processing on the ACK.
17246 		 * The non-compressed path through the code has this
17247 		 * weakness (noted by @jtl) that it actually does some
17248 		 * processing before verifying the timestamp information.
17249 		 * We don't take that path here which is why we set
17250 		 * the ack_val_set first, do the timestamp and ecn
17251 		 * processing, and then look at what we have setup.
17252 		 */
17253 		if (ae->ack_val_set == ACK_BEHIND) {
17254 			/*
17255 			 * Case B flag reordering, if window is not closed
17256 			 * or it could be a keep-alive or persists
17257 			 */
17258 			if (SEQ_LT(ae->ack, tp->snd_una) && (sbspace(&so->so_rcv) > segsiz)) {
17259 				rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
17260 				if (rack->r_ctl.rc_reorder_ts == 0)
17261 					rack->r_ctl.rc_reorder_ts = 1;
17262 			}
17263 		} else if (ae->ack_val_set == ACK_DUPACK) {
17264 			/* Case D */
17265 			rack_strike_dupack(rack, ae->ack);
17266 		} else if (ae->ack_val_set == ACK_RWND) {
17267 			/* Case C */
17268 			if ((ae->flags & TSTMP_LRO) || (ae->flags & TSTMP_HDWR)) {
17269 				ts.tv_sec = ae->timestamp / 1000000000;
17270 				ts.tv_nsec = ae->timestamp % 1000000000;
17271 				rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
17272 				rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
17273 			} else {
17274 				rack->r_ctl.act_rcv_time = *tv;
17275 			}
17276 			if (rack->forced_ack) {
17277 				rack_handle_probe_response(rack, tiwin,
17278 							   tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time));
17279 			}
17280 #ifdef TCP_ACCOUNTING
17281 			win_up_req = 1;
17282 #endif
17283 			win_upd_ack = ae->ack;
17284 			win_seq = ae->seq;
17285 			the_win = tiwin;
17286 			rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts);
17287 		} else {
17288 			/* Case A */
17289 			if (SEQ_GT(ae->ack, tp->snd_max)) {
17290 				/*
17291 				 * We just send an ack since the incoming
17292 				 * ack is beyond the largest seq we sent.
17293 				 */
17294 				if ((tp->t_flags & TF_ACKNOW) == 0) {
17295 					ctf_ack_war_checks(tp, &rack->r_ctl.challenge_ack_ts, &rack->r_ctl.challenge_ack_cnt);
17296 					if (tp->t_flags && TF_ACKNOW)
17297 						rack->r_wanted_output = 1;
17298 				}
17299 			} else {
17300 				nsegs++;
17301 				/* If the window changed setup to update */
17302 				if (tiwin != tp->snd_wnd) {
17303 					win_upd_ack = ae->ack;
17304 					win_seq = ae->seq;
17305 					the_win = tiwin;
17306 					rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts);
17307 				}
17308 #ifdef TCP_ACCOUNTING
17309 				/* Account for the acks */
17310 				if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17311 					tp->tcp_cnt_counters[CNT_OF_ACKS_IN] += (((ae->ack - high_seq) + segsiz - 1) / segsiz);
17312 				}
17313 #endif
17314 				high_seq = ae->ack;
17315 				/* Setup our act_rcv_time */
17316 				if ((ae->flags & TSTMP_LRO) || (ae->flags & TSTMP_HDWR)) {
17317 					ts.tv_sec = ae->timestamp / 1000000000;
17318 					ts.tv_nsec = ae->timestamp % 1000000000;
17319 					rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
17320 					rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
17321 				} else {
17322 					rack->r_ctl.act_rcv_time = *tv;
17323 				}
17324 				rack_process_to_cumack(tp, rack, ae->ack, cts, to,
17325 						       tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time));
17326 #ifdef TCP_REQUEST_TRK
17327 				rack_req_check_for_comp(rack, high_seq);
17328 #endif
17329 				if (rack->rc_dsack_round_seen) {
17330 					/* Is the dsack round over? */
17331 					if (SEQ_GEQ(ae->ack, rack->r_ctl.dsack_round_end)) {
17332 						/* Yes it is */
17333 						rack->rc_dsack_round_seen = 0;
17334 						rack_log_dsack_event(rack, 3, __LINE__, 0, 0);
17335 					}
17336 				}
17337 			}
17338 		}
17339 		/* And lets be sure to commit the rtt measurements for this ack */
17340 		tcp_rack_xmit_timer_commit(rack, tp);
17341 #ifdef TCP_ACCOUNTING
17342 		rdstc = get_cyclecount();
17343 		if (rdstc > ts_val) {
17344 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17345 				tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val);
17346 				if (ae->ack_val_set == ACK_CUMACK)
17347 					tp->tcp_proc_time[CYC_HANDLE_MAP] += (rdstc - ts_val);
17348 			}
17349 		}
17350 #endif
17351 	}
17352 #ifdef TCP_ACCOUNTING
17353 	ts_val = get_cyclecount();
17354 #endif
17355 	/* Tend to any collapsed window */
17356 	if (SEQ_GT(tp->snd_max, high_seq) && (tp->snd_wnd < (tp->snd_max - high_seq))) {
17357 		/* The peer collapsed the window */
17358 		rack_collapsed_window(rack, (tp->snd_max - high_seq), high_seq, __LINE__);
17359 	} else if (rack->rc_has_collapsed)
17360 		rack_un_collapse_window(rack, __LINE__);
17361 	if ((rack->r_collapse_point_valid) &&
17362 	    (SEQ_GT(high_seq, rack->r_ctl.high_collapse_point)))
17363 		rack->r_collapse_point_valid = 0;
17364 	acked_amount = acked = (high_seq - tp->snd_una);
17365 	if (acked) {
17366 		/*
17367 		 * The draft (v3) calls for us to use SEQ_GEQ, but that
17368 		 * causes issues when we are just going app limited. Lets
17369 		 * instead use SEQ_GT <or> where its equal but more data
17370 		 * is outstanding.
17371 		 *
17372 		 * Also make sure we are on the last ack of a series. We
17373 		 * have to have all the ack's processed in queue to know
17374 		 * if there is something left outstanding.
17375 		 *
17376 		 */
17377 		if (SEQ_GEQ(high_seq, rack->r_ctl.roundends) &&
17378 		    (rack->rc_new_rnd_needed == 0) &&
17379 		    (nxt_pkt == 0)) {
17380 			/*
17381 			 * We have crossed into a new round with
17382 			 * this th_ack value.
17383 			 */
17384 			rack_new_round_setup(tp, rack, high_seq);
17385 		}
17386 		/*
17387 		 * Clear the probe not answered flag
17388 		 * since cum-ack moved forward.
17389 		 */
17390 		rack->probe_not_answered = 0;
17391 		if (rack->sack_attack_disable == 0)
17392 			rack_do_decay(rack);
17393 		if (acked >= segsiz) {
17394 			/*
17395 			 * You only get credit for
17396 			 * MSS and greater (and you get extra
17397 			 * credit for larger cum-ack moves).
17398 			 */
17399 			int ac;
17400 
17401 			ac = acked / segsiz;
17402 			rack->r_ctl.ack_count += ac;
17403 			counter_u64_add(rack_ack_total, ac);
17404 		}
17405 		if (rack->r_ctl.ack_count > 0xfff00000) {
17406 			/*
17407 			 * reduce the number to keep us under
17408 			 * a uint32_t.
17409 			 */
17410 			rack->r_ctl.ack_count /= 2;
17411 			rack->r_ctl.sack_count /= 2;
17412 		}
17413 		if (tp->t_flags & TF_NEEDSYN) {
17414 			/*
17415 			 * T/TCP: Connection was half-synchronized, and our SYN has
17416 			 * been ACK'd (so connection is now fully synchronized).  Go
17417 			 * to non-starred state, increment snd_una for ACK of SYN,
17418 			 * and check if we can do window scaling.
17419 			 */
17420 			tp->t_flags &= ~TF_NEEDSYN;
17421 			tp->snd_una++;
17422 			acked_amount = acked = (high_seq - tp->snd_una);
17423 		}
17424 		if (acked > sbavail(&so->so_snd))
17425 			acked_amount = sbavail(&so->so_snd);
17426 #ifdef TCP_SAD_DETECTION
17427 		/*
17428 		 * We only care on a cum-ack move if we are in a sack-disabled
17429 		 * state. We have already added in to the ack_count, and we never
17430 		 * would disable on a cum-ack move, so we only care to do the
17431 		 * detection if it may "undo" it, i.e. we were in disabled already.
17432 		 */
17433 		if (rack->sack_attack_disable)
17434 			rack_do_detection(tp, rack, acked_amount, segsiz);
17435 #endif
17436 		if (IN_FASTRECOVERY(tp->t_flags) &&
17437 		    (rack->rack_no_prr == 0))
17438 			rack_update_prr(tp, rack, acked_amount, high_seq);
17439 		if (IN_RECOVERY(tp->t_flags)) {
17440 			if (SEQ_LT(high_seq, tp->snd_recover) &&
17441 			    (SEQ_LT(high_seq, tp->snd_max))) {
17442 				tcp_rack_partialack(tp);
17443 			} else {
17444 				rack_post_recovery(tp, high_seq);
17445 				post_recovery = 1;
17446 			}
17447 		}  else if ((rack->rto_from_rec == 1) &&
17448 			    SEQ_GEQ(high_seq, tp->snd_recover)) {
17449 			/*
17450 			 * We were in recovery, hit a rxt timeout
17451 			 * and never re-entered recovery. The timeout(s)
17452 			 * made up all the lost data. In such a case
17453 			 * we need to clear the rto_from_rec flag.
17454 			 */
17455 			rack->rto_from_rec = 0;
17456 		}
17457 		/* Handle the rack-log-ack part (sendmap) */
17458 		if ((sbused(&so->so_snd) == 0) &&
17459 		    (acked > acked_amount) &&
17460 		    (tp->t_state >= TCPS_FIN_WAIT_1) &&
17461 		    (tp->t_flags & TF_SENTFIN)) {
17462 			/*
17463 			 * We must be sure our fin
17464 			 * was sent and acked (we can be
17465 			 * in FIN_WAIT_1 without having
17466 			 * sent the fin).
17467 			 */
17468 			ourfinisacked = 1;
17469 			/*
17470 			 * Lets make sure snd_una is updated
17471 			 * since most likely acked_amount = 0 (it
17472 			 * should be).
17473 			 */
17474 			tp->snd_una = high_seq;
17475 		}
17476 		/* Did we make a RTO error? */
17477 		if ((tp->t_flags & TF_PREVVALID) &&
17478 		    ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
17479 			tp->t_flags &= ~TF_PREVVALID;
17480 			if (tp->t_rxtshift == 1 &&
17481 			    (int)(ticks - tp->t_badrxtwin) < 0)
17482 				rack_cong_signal(tp, CC_RTO_ERR, high_seq, __LINE__);
17483 		}
17484 		/* Handle the data in the socket buffer */
17485 		KMOD_TCPSTAT_ADD(tcps_rcvackpack, 1);
17486 		KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
17487 		if (acked_amount > 0) {
17488 			uint32_t p_cwnd;
17489 			struct mbuf *mfree;
17490 
17491 			if (post_recovery) {
17492 				/*
17493 				 * Grab the segsiz, multiply by 2 and add the snd_cwnd
17494 				 * that is the max the CC should add if we are exiting
17495 				 * recovery and doing a late add.
17496 				 */
17497 				p_cwnd = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
17498 				p_cwnd <<= 1;
17499 				p_cwnd += tp->snd_cwnd;
17500 			}
17501 			rack_ack_received(tp, rack, high_seq, nsegs, CC_ACK, post_recovery);
17502 			if (post_recovery && (tp->snd_cwnd > p_cwnd)) {
17503 				/* Must be non-newreno (cubic) getting too ahead of itself */
17504 				tp->snd_cwnd = p_cwnd;
17505 			}
17506 			SOCKBUF_LOCK(&so->so_snd);
17507 			mfree = sbcut_locked(&so->so_snd, acked_amount);
17508 			tp->snd_una = high_seq;
17509 			/* Note we want to hold the sb lock through the sendmap adjust */
17510 			rack_adjust_sendmap_head(rack, &so->so_snd);
17511 			/* Wake up the socket if we have room to write more */
17512 			rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
17513 			sowwakeup_locked(so);
17514 			m_freem(mfree);
17515 		}
17516 		/* update progress */
17517 		tp->t_acktime = ticks;
17518 		rack_log_progress_event(rack, tp, tp->t_acktime,
17519 					PROGRESS_UPDATE, __LINE__);
17520 		/* Clear out shifts and such */
17521 		tp->t_rxtshift = 0;
17522 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
17523 				   rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
17524 		rack->rc_tlp_in_progress = 0;
17525 		rack->r_ctl.rc_tlp_cnt_out = 0;
17526 		/* Send recover and snd_nxt must be dragged along */
17527 		if (SEQ_GT(tp->snd_una, tp->snd_recover))
17528 			tp->snd_recover = tp->snd_una;
17529 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
17530 			tp->snd_nxt = tp->snd_max;
17531 		/*
17532 		 * If the RXT timer is running we want to
17533 		 * stop it, so we can restart a TLP (or new RXT).
17534 		 */
17535 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
17536 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
17537 		tp->snd_wl2 = high_seq;
17538 		tp->t_dupacks = 0;
17539 		if (under_pacing &&
17540 		    (rack->use_fixed_rate == 0) &&
17541 		    (rack->in_probe_rtt == 0) &&
17542 		    rack->rc_gp_dyn_mul &&
17543 		    rack->rc_always_pace) {
17544 			/* Check if we are dragging bottom */
17545 			rack_check_bottom_drag(tp, rack, so);
17546 		}
17547 		if (tp->snd_una == tp->snd_max) {
17548 			tp->t_flags &= ~TF_PREVVALID;
17549 			rack->r_ctl.retran_during_recovery = 0;
17550 			rack->rc_suspicious = 0;
17551 			rack->r_ctl.dsack_byte_cnt = 0;
17552 			rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
17553 			if (rack->r_ctl.rc_went_idle_time == 0)
17554 				rack->r_ctl.rc_went_idle_time = 1;
17555 			rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
17556 			if (sbavail(&tptosocket(tp)->so_snd) == 0)
17557 				tp->t_acktime = 0;
17558 			/* Set so we might enter persists... */
17559 			rack->r_wanted_output = 1;
17560 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
17561 			sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
17562 			if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
17563 			    (sbavail(&so->so_snd) == 0) &&
17564 			    (tp->t_flags2 & TF2_DROP_AF_DATA)) {
17565 				/*
17566 				 * The socket was gone and the
17567 				 * peer sent data (not now in the past), time to
17568 				 * reset him.
17569 				 */
17570 				rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
17571 				/* tcp_close will kill the inp pre-log the Reset */
17572 				tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
17573 #ifdef TCP_ACCOUNTING
17574 				rdstc = get_cyclecount();
17575 				if (rdstc > ts_val) {
17576 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17577 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
17578 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
17579 					}
17580 				}
17581 #endif
17582 				m_freem(m);
17583 				tp = tcp_close(tp);
17584 				if (tp == NULL) {
17585 #ifdef TCP_ACCOUNTING
17586 					sched_unpin();
17587 #endif
17588 					return (1);
17589 				}
17590 				/*
17591 				 * We would normally do drop-with-reset which would
17592 				 * send back a reset. We can't since we don't have
17593 				 * all the needed bits. Instead lets arrange for
17594 				 * a call to tcp_output(). That way since we
17595 				 * are in the closed state we will generate a reset.
17596 				 *
17597 				 * Note if tcp_accounting is on we don't unpin since
17598 				 * we do that after the goto label.
17599 				 */
17600 				goto send_out_a_rst;
17601 			}
17602 			if ((sbused(&so->so_snd) == 0) &&
17603 			    (tp->t_state >= TCPS_FIN_WAIT_1) &&
17604 			    (tp->t_flags & TF_SENTFIN)) {
17605 				/*
17606 				 * If we can't receive any more data, then closing user can
17607 				 * proceed. Starting the timer is contrary to the
17608 				 * specification, but if we don't get a FIN we'll hang
17609 				 * forever.
17610 				 *
17611 				 */
17612 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
17613 					soisdisconnected(so);
17614 					tcp_timer_activate(tp, TT_2MSL,
17615 							   (tcp_fast_finwait2_recycle ?
17616 							    tcp_finwait2_timeout :
17617 							    TP_MAXIDLE(tp)));
17618 				}
17619 				if (ourfinisacked == 0) {
17620 					/*
17621 					 * We don't change to fin-wait-2 if we have our fin acked
17622 					 * which means we are probably in TCPS_CLOSING.
17623 					 */
17624 					tcp_state_change(tp, TCPS_FIN_WAIT_2);
17625 				}
17626 			}
17627 		}
17628 		/* Wake up the socket if we have room to write more */
17629 		if (sbavail(&so->so_snd)) {
17630 			rack->r_wanted_output = 1;
17631 			if (ctf_progress_timeout_check(tp, true)) {
17632 				rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
17633 							tp, tick, PROGRESS_DROP, __LINE__);
17634 				/*
17635 				 * We cheat here and don't send a RST, we should send one
17636 				 * when the pacer drops the connection.
17637 				 */
17638 #ifdef TCP_ACCOUNTING
17639 				rdstc = get_cyclecount();
17640 				if (rdstc > ts_val) {
17641 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17642 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
17643 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
17644 					}
17645 				}
17646 				sched_unpin();
17647 #endif
17648 				(void)tcp_drop(tp, ETIMEDOUT);
17649 				m_freem(m);
17650 				return (1);
17651 			}
17652 		}
17653 		if (ourfinisacked) {
17654 			switch(tp->t_state) {
17655 			case TCPS_CLOSING:
17656 #ifdef TCP_ACCOUNTING
17657 				rdstc = get_cyclecount();
17658 				if (rdstc > ts_val) {
17659 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17660 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
17661 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
17662 					}
17663 				}
17664 				sched_unpin();
17665 #endif
17666 				tcp_twstart(tp);
17667 				m_freem(m);
17668 				return (1);
17669 				break;
17670 			case TCPS_LAST_ACK:
17671 #ifdef TCP_ACCOUNTING
17672 				rdstc = get_cyclecount();
17673 				if (rdstc > ts_val) {
17674 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17675 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
17676 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
17677 					}
17678 				}
17679 				sched_unpin();
17680 #endif
17681 				tp = tcp_close(tp);
17682 				ctf_do_drop(m, tp);
17683 				return (1);
17684 				break;
17685 			case TCPS_FIN_WAIT_1:
17686 #ifdef TCP_ACCOUNTING
17687 				rdstc = get_cyclecount();
17688 				if (rdstc > ts_val) {
17689 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17690 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
17691 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
17692 					}
17693 				}
17694 #endif
17695 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
17696 					soisdisconnected(so);
17697 					tcp_timer_activate(tp, TT_2MSL,
17698 							   (tcp_fast_finwait2_recycle ?
17699 							    tcp_finwait2_timeout :
17700 							    TP_MAXIDLE(tp)));
17701 				}
17702 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
17703 				break;
17704 			default:
17705 				break;
17706 			}
17707 		}
17708 		if (rack->r_fast_output) {
17709 			/*
17710 			 * We re doing fast output.. can we expand that?
17711 			 */
17712 			rack_gain_for_fastoutput(rack, tp, so, acked_amount);
17713 		}
17714 #ifdef TCP_ACCOUNTING
17715 		rdstc = get_cyclecount();
17716 		if (rdstc > ts_val) {
17717 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17718 				tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
17719 				tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
17720 			}
17721 		}
17722 
17723 	} else if (win_up_req) {
17724 		rdstc = get_cyclecount();
17725 		if (rdstc > ts_val) {
17726 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17727 				tp->tcp_proc_time[ACK_RWND] += (rdstc - ts_val);
17728 			}
17729 		}
17730 #endif
17731 	}
17732 	/* Now is there a next packet, if so we are done */
17733 	m_freem(m);
17734 	did_out = 0;
17735 	if (nxt_pkt) {
17736 #ifdef TCP_ACCOUNTING
17737 		sched_unpin();
17738 #endif
17739 		rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 5, nsegs);
17740 		return (0);
17741 	}
17742 	rack_handle_might_revert(tp, rack);
17743 	ctf_calc_rwin(so, tp);
17744 	if ((rack->r_wanted_output != 0) ||
17745 	    (rack->r_fast_output != 0) ||
17746 	    (tp->t_flags & TF_ACKNOW )) {
17747 	send_out_a_rst:
17748 		if (tcp_output(tp) < 0) {
17749 #ifdef TCP_ACCOUNTING
17750 			sched_unpin();
17751 #endif
17752 			return (1);
17753 		}
17754 		did_out = 1;
17755 	}
17756 	if (tp->t_flags2 & TF2_HPTS_CALLS)
17757 		tp->t_flags2 &= ~TF2_HPTS_CALLS;
17758 	rack_free_trim(rack);
17759 #ifdef TCP_ACCOUNTING
17760 	sched_unpin();
17761 #endif
17762 	rack_timer_audit(tp, rack, &so->so_snd);
17763 	rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 6, nsegs);
17764 	return (0);
17765 }
17766 
17767 #define	TCP_LRO_TS_OPTION \
17768     ntohl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \
17769 	  (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)
17770 
17771 static int
17772 rack_do_segment_nounlock(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
17773     int32_t drop_hdrlen, int32_t tlen, uint8_t iptos, int32_t nxt_pkt,
17774     struct timeval *tv)
17775 {
17776 	struct inpcb *inp = tptoinpcb(tp);
17777 	struct socket *so = tptosocket(tp);
17778 #ifdef TCP_ACCOUNTING
17779 	uint64_t ts_val;
17780 #endif
17781 	int32_t thflags, retval, did_out = 0;
17782 	int32_t way_out = 0;
17783 	/*
17784 	 * cts - is the current time from tv (caller gets ts) in microseconds.
17785 	 * ms_cts - is the current time from tv in milliseconds.
17786 	 * us_cts - is the time that LRO or hardware actually got the packet in microseconds.
17787 	 */
17788 	uint32_t cts, us_cts, ms_cts;
17789 	uint32_t tiwin;
17790 	struct timespec ts;
17791 	struct tcpopt to;
17792 	struct tcp_rack *rack;
17793 	struct rack_sendmap *rsm;
17794 	int32_t prev_state = 0;
17795 	int no_output = 0;
17796 	int slot_remaining = 0;
17797 #ifdef TCP_ACCOUNTING
17798 	int ack_val_set = 0xf;
17799 #endif
17800 	int nsegs;
17801 
17802 	NET_EPOCH_ASSERT();
17803 	INP_WLOCK_ASSERT(inp);
17804 
17805 	/*
17806 	 * tv passed from common code is from either M_TSTMP_LRO or
17807 	 * tcp_get_usecs() if no LRO m_pkthdr timestamp is present.
17808 	 */
17809 	rack = (struct tcp_rack *)tp->t_fb_ptr;
17810 	if (rack->rack_deferred_inited == 0) {
17811 		/*
17812 		 * If we are the connecting socket we will
17813 		 * hit rack_init() when no sequence numbers
17814 		 * are setup. This makes it so we must defer
17815 		 * some initialization. Call that now.
17816 		 */
17817 		rack_deferred_init(tp, rack);
17818 	}
17819 	/*
17820 	 * Check to see if we need to skip any output plans. This
17821 	 * can happen in the non-LRO path where we are pacing and
17822 	 * must process the ack coming in but need to defer sending
17823 	 * anything becase a pacing timer is running.
17824 	 */
17825 	us_cts = tcp_tv_to_usectick(tv);
17826 	if (m->m_flags & M_ACKCMP) {
17827 		/*
17828 		 * All compressed ack's are ack's by definition so
17829 		 * remove any ack required flag and then do the processing.
17830 		 */
17831 		rack->rc_ack_required = 0;
17832 		return (rack_do_compressed_ack_processing(tp, so, m, nxt_pkt, tv));
17833 	}
17834 	thflags = tcp_get_flags(th);
17835 	if ((rack->rc_always_pace == 1) &&
17836 	    (rack->rc_ack_can_sendout_data == 0) &&
17837 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
17838 	    (TSTMP_LT(us_cts, rack->r_ctl.rc_last_output_to))) {
17839 		/*
17840 		 * Ok conditions are right for queuing the packets
17841 		 * but we do have to check the flags in the inp, it
17842 		 * could be, if a sack is present, we want to be awoken and
17843 		 * so should process the packets.
17844 		 */
17845 		slot_remaining = rack->r_ctl.rc_last_output_to - us_cts;
17846 		if (rack->rc_tp->t_flags2 & TF2_DONT_SACK_QUEUE) {
17847 			no_output = 1;
17848 		} else {
17849 			/*
17850 			 * If there is no options, or just a
17851 			 * timestamp option, we will want to queue
17852 			 * the packets. This is the same that LRO does
17853 			 * and will need to change with accurate ECN.
17854 			 */
17855 			uint32_t *ts_ptr;
17856 			int optlen;
17857 
17858 			optlen = (th->th_off << 2) - sizeof(struct tcphdr);
17859 			ts_ptr = (uint32_t *)(th + 1);
17860 			if ((optlen == 0) ||
17861 			    ((optlen == TCPOLEN_TSTAMP_APPA) &&
17862 			     (*ts_ptr == TCP_LRO_TS_OPTION)))
17863 				no_output = 1;
17864 		}
17865 		if ((no_output == 1) && (slot_remaining < tcp_min_hptsi_time)) {
17866 			/*
17867 			 * It is unrealistic to think we can pace in less than
17868 			 * the minimum granularity of the pacer (def:250usec). So
17869 			 * if we have less than that time remaining we should go
17870 			 * ahead and allow output to be "early". We will attempt to
17871 			 * make up for it in any pacing time we try to apply on
17872 			 * the outbound packet.
17873 			 */
17874 			no_output = 0;
17875 		}
17876 	}
17877 	/*
17878 	 * If there is a RST or FIN lets dump out the bw
17879 	 * with a FIN the connection may go on but we
17880 	 * may not.
17881 	 */
17882 	if ((thflags & TH_FIN) || (thflags & TH_RST))
17883 		rack_log_pacing_delay_calc(rack,
17884 					   rack->r_ctl.gp_bw,
17885 					   0,
17886 					   0,
17887 					   rack_get_gp_est(rack), /* delRate */
17888 					   rack_get_lt_bw(rack), /* rttProp */
17889 					   20, __LINE__, NULL, 0);
17890 	if (m->m_flags & M_ACKCMP) {
17891 		panic("Impossible reach m has ackcmp? m:%p tp:%p", m, tp);
17892 	}
17893 	cts = tcp_tv_to_usectick(tv);
17894 	ms_cts =  tcp_tv_to_mssectick(tv);
17895 	nsegs = m->m_pkthdr.lro_nsegs;
17896 	counter_u64_add(rack_proc_non_comp_ack, 1);
17897 #ifdef TCP_ACCOUNTING
17898 	sched_pin();
17899 	if (thflags & TH_ACK)
17900 		ts_val = get_cyclecount();
17901 #endif
17902 	if ((m->m_flags & M_TSTMP) ||
17903 	    (m->m_flags & M_TSTMP_LRO)) {
17904 		mbuf_tstmp2timespec(m, &ts);
17905 		rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
17906 		rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
17907 	} else
17908 		rack->r_ctl.act_rcv_time = *tv;
17909 	kern_prefetch(rack, &prev_state);
17910 	prev_state = 0;
17911 	/*
17912 	 * Unscale the window into a 32-bit value. For the SYN_SENT state
17913 	 * the scale is zero.
17914 	 */
17915 	tiwin = th->th_win << tp->snd_scale;
17916 #ifdef TCP_ACCOUNTING
17917 	if (thflags & TH_ACK) {
17918 		/*
17919 		 * We have a tradeoff here. We can either do what we are
17920 		 * doing i.e. pinning to this CPU and then doing the accounting
17921 		 * <or> we could do a critical enter, setup the rdtsc and cpu
17922 		 * as in below, and then validate we are on the same CPU on
17923 		 * exit. I have choosen to not do the critical enter since
17924 		 * that often will gain you a context switch, and instead lock
17925 		 * us (line above this if) to the same CPU with sched_pin(). This
17926 		 * means we may be context switched out for a higher priority
17927 		 * interupt but we won't be moved to another CPU.
17928 		 *
17929 		 * If this occurs (which it won't very often since we most likely
17930 		 * are running this code in interupt context and only a higher
17931 		 * priority will bump us ... clock?) we will falsely add in
17932 		 * to the time the interupt processing time plus the ack processing
17933 		 * time. This is ok since its a rare event.
17934 		 */
17935 		ack_val_set = tcp_do_ack_accounting(tp, th, &to, tiwin,
17936 						    ctf_fixed_maxseg(tp));
17937 	}
17938 #endif
17939 	/*
17940 	 * Parse options on any incoming segment.
17941 	 */
17942 	memset(&to, 0, sizeof(to));
17943 	tcp_dooptions(&to, (u_char *)(th + 1),
17944 	    (th->th_off << 2) - sizeof(struct tcphdr),
17945 	    (thflags & TH_SYN) ? TO_SYN : 0);
17946 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
17947 	    __func__));
17948 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
17949 	    __func__));
17950 
17951 	if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
17952 	    (tp->t_flags & TF_GPUTINPROG)) {
17953 		/*
17954 		 * We have a goodput in progress
17955 		 * and we have entered a late state.
17956 		 * Do we have enough data in the sb
17957 		 * to handle the GPUT request?
17958 		 */
17959 		uint32_t bytes;
17960 
17961 		bytes = tp->gput_ack - tp->gput_seq;
17962 		if (SEQ_GT(tp->gput_seq, tp->snd_una))
17963 			bytes += tp->gput_seq - tp->snd_una;
17964 		if (bytes > sbavail(&tptosocket(tp)->so_snd)) {
17965 			/*
17966 			 * There are not enough bytes in the socket
17967 			 * buffer that have been sent to cover this
17968 			 * measurement. Cancel it.
17969 			 */
17970 			rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
17971 						   rack->r_ctl.rc_gp_srtt /*flex1*/,
17972 						   tp->gput_seq,
17973 						   0, 0, 18, __LINE__, NULL, 0);
17974 			tp->t_flags &= ~TF_GPUTINPROG;
17975 		}
17976 	}
17977 	if (tcp_bblogging_on(rack->rc_tp)) {
17978 		union tcp_log_stackspecific log;
17979 		struct timeval ltv;
17980 #ifdef TCP_REQUEST_TRK
17981 		struct tcp_sendfile_track *tcp_req;
17982 
17983 		if (SEQ_GT(th->th_ack, tp->snd_una)) {
17984 			tcp_req = tcp_req_find_req_for_seq(tp, (th->th_ack-1));
17985 		} else {
17986 			tcp_req = tcp_req_find_req_for_seq(tp, th->th_ack);
17987 		}
17988 #endif
17989 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
17990 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
17991 		if (rack->rack_no_prr == 0)
17992 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
17993 		else
17994 			log.u_bbr.flex1 = 0;
17995 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
17996 		log.u_bbr.use_lt_bw <<= 1;
17997 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
17998 		log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced;
17999 		log.u_bbr.bbr_state = rack->rc_free_cnt;
18000 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18001 		log.u_bbr.pkts_out = rack->rc_tp->t_maxseg;
18002 		log.u_bbr.flex3 = m->m_flags;
18003 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
18004 		log.u_bbr.lost = thflags;
18005 		log.u_bbr.pacing_gain = 0x1;
18006 #ifdef TCP_ACCOUNTING
18007 		log.u_bbr.cwnd_gain = ack_val_set;
18008 #endif
18009 		log.u_bbr.flex7 = 2;
18010 		if (m->m_flags & M_TSTMP) {
18011 			/* Record the hardware timestamp if present */
18012 			mbuf_tstmp2timespec(m, &ts);
18013 			ltv.tv_sec = ts.tv_sec;
18014 			ltv.tv_usec = ts.tv_nsec / 1000;
18015 			log.u_bbr.lt_epoch = tcp_tv_to_usectick(&ltv);
18016 		} else if (m->m_flags & M_TSTMP_LRO) {
18017 			/* Record the LRO the arrival timestamp */
18018 			mbuf_tstmp2timespec(m, &ts);
18019 			ltv.tv_sec = ts.tv_sec;
18020 			ltv.tv_usec = ts.tv_nsec / 1000;
18021 			log.u_bbr.flex5 = tcp_tv_to_usectick(&ltv);
18022 		}
18023 		log.u_bbr.timeStamp = tcp_get_usecs(&ltv);
18024 		/* Log the rcv time */
18025 		log.u_bbr.delRate = m->m_pkthdr.rcv_tstmp;
18026 #ifdef TCP_REQUEST_TRK
18027 		log.u_bbr.applimited = tp->t_tcpreq_closed;
18028 		log.u_bbr.applimited <<= 8;
18029 		log.u_bbr.applimited |= tp->t_tcpreq_open;
18030 		log.u_bbr.applimited <<= 8;
18031 		log.u_bbr.applimited |= tp->t_tcpreq_req;
18032 		if (tcp_req) {
18033 			/* Copy out any client req info */
18034 			/* seconds */
18035 			log.u_bbr.pkt_epoch = (tcp_req->localtime / HPTS_USEC_IN_SEC);
18036 			/* useconds */
18037 			log.u_bbr.delivered = (tcp_req->localtime % HPTS_USEC_IN_SEC);
18038 			log.u_bbr.rttProp = tcp_req->timestamp;
18039 			log.u_bbr.cur_del_rate = tcp_req->start;
18040 			if (tcp_req->flags & TCP_TRK_TRACK_FLG_OPEN) {
18041 				log.u_bbr.flex8 |= 1;
18042 			} else {
18043 				log.u_bbr.flex8 |= 2;
18044 				log.u_bbr.bw_inuse = tcp_req->end;
18045 			}
18046 			log.u_bbr.flex6 = tcp_req->start_seq;
18047 			if (tcp_req->flags & TCP_TRK_TRACK_FLG_COMP) {
18048 				log.u_bbr.flex8 |= 4;
18049 				log.u_bbr.epoch = tcp_req->end_seq;
18050 			}
18051 		}
18052 #endif
18053 		TCP_LOG_EVENTP(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
18054 		    tlen, &log, true, &ltv);
18055 	}
18056 	/* Remove ack required flag if set, we have one  */
18057 	if (thflags & TH_ACK)
18058 		rack->rc_ack_required = 0;
18059 	if (rack->sack_attack_disable > 0) {
18060 		rack->r_ctl.ack_during_sd++;
18061 		rack_log_type_bbrsnd(rack, 0, 0, cts, tv, __LINE__);
18062 	}
18063 	if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
18064 		way_out = 4;
18065 		retval = 0;
18066 		m_freem(m);
18067 		goto done_with_input;
18068 	}
18069 	/*
18070 	 * If a segment with the ACK-bit set arrives in the SYN-SENT state
18071 	 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9.
18072 	 */
18073 	if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
18074 	    (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
18075 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
18076 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
18077 #ifdef TCP_ACCOUNTING
18078 		sched_unpin();
18079 #endif
18080 		return (1);
18081 	}
18082 	/*
18083 	 * If timestamps were negotiated during SYN/ACK and a
18084 	 * segment without a timestamp is received, silently drop
18085 	 * the segment, unless it is a RST segment or missing timestamps are
18086 	 * tolerated.
18087 	 * See section 3.2 of RFC 7323.
18088 	 */
18089 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) &&
18090 	    ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) {
18091 		way_out = 5;
18092 		retval = 0;
18093 		m_freem(m);
18094 		goto done_with_input;
18095 	}
18096 	/*
18097 	 * Segment received on connection. Reset idle time and keep-alive
18098 	 * timer. XXX: This should be done after segment validation to
18099 	 * ignore broken/spoofed segs.
18100 	 */
18101 	if  (tp->t_idle_reduce &&
18102 	     (tp->snd_max == tp->snd_una) &&
18103 	     (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur)) {
18104 		counter_u64_add(rack_input_idle_reduces, 1);
18105 		rack_cc_after_idle(rack, tp);
18106 	}
18107 	tp->t_rcvtime = ticks;
18108 #ifdef STATS
18109 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
18110 #endif
18111 	if (tiwin > rack->r_ctl.rc_high_rwnd)
18112 		rack->r_ctl.rc_high_rwnd = tiwin;
18113 	/*
18114 	 * TCP ECN processing. XXXJTL: If we ever use ECN, we need to move
18115 	 * this to occur after we've validated the segment.
18116 	 */
18117 	if (tcp_ecn_input_segment(tp, thflags, tlen,
18118 	    tcp_packets_this_ack(tp, th->th_ack),
18119 	    iptos))
18120 		rack_cong_signal(tp, CC_ECN, th->th_ack, __LINE__);
18121 
18122 	/*
18123 	 * If echoed timestamp is later than the current time, fall back to
18124 	 * non RFC1323 RTT calculation.  Normalize timestamp if syncookies
18125 	 * were used when this connection was established.
18126 	 */
18127 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
18128 		to.to_tsecr -= tp->ts_offset;
18129 		if (TSTMP_GT(to.to_tsecr, ms_cts))
18130 			to.to_tsecr = 0;
18131 	}
18132 	if ((rack->r_rcvpath_rtt_up == 1) &&
18133 	    (to.to_flags & TOF_TS) &&
18134 	    (TSTMP_GEQ(to.to_tsecr, rack->r_ctl.last_rcv_tstmp_for_rtt))) {
18135 		uint32_t rtt = 0;
18136 
18137 		/*
18138 		 * We are receiving only and thus not sending
18139 		 * data to do an RTT. We set a flag when we first
18140 		 * sent this TS to the peer. We now have it back
18141 		 * and have an RTT to share. We log it as a conf
18142 		 * 4, we are not so sure about it.. since we
18143 		 * may have lost an ack.
18144 		 */
18145 		if (TSTMP_GT(cts, rack->r_ctl.last_time_of_arm_rcv))
18146 		    rtt = (cts - rack->r_ctl.last_time_of_arm_rcv);
18147 		rack->r_rcvpath_rtt_up = 0;
18148 		/* Submit and commit the timer */
18149 		if (rtt > 0) {
18150 			tcp_rack_xmit_timer(rack, rtt, 0, rtt, 4, NULL, 1);
18151 			tcp_rack_xmit_timer_commit(rack, tp);
18152 		}
18153 	}
18154 	/*
18155 	 * If its the first time in we need to take care of options and
18156 	 * verify we can do SACK for rack!
18157 	 */
18158 	if (rack->r_state == 0) {
18159 		/* Should be init'd by rack_init() */
18160 		KASSERT(rack->rc_inp != NULL,
18161 		    ("%s: rack->rc_inp unexpectedly NULL", __func__));
18162 		if (rack->rc_inp == NULL) {
18163 			rack->rc_inp = inp;
18164 		}
18165 
18166 		/*
18167 		 * Process options only when we get SYN/ACK back. The SYN
18168 		 * case for incoming connections is handled in tcp_syncache.
18169 		 * According to RFC1323 the window field in a SYN (i.e., a
18170 		 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX
18171 		 * this is traditional behavior, may need to be cleaned up.
18172 		 */
18173 		if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
18174 			/* Handle parallel SYN for ECN */
18175 			tcp_ecn_input_parallel_syn(tp, thflags, iptos);
18176 			if ((to.to_flags & TOF_SCALE) &&
18177 			    (tp->t_flags & TF_REQ_SCALE)) {
18178 				tp->t_flags |= TF_RCVD_SCALE;
18179 				tp->snd_scale = to.to_wscale;
18180 			} else
18181 				tp->t_flags &= ~TF_REQ_SCALE;
18182 			/*
18183 			 * Initial send window.  It will be updated with the
18184 			 * next incoming segment to the scaled value.
18185 			 */
18186 			tp->snd_wnd = th->th_win;
18187 			rack_validate_fo_sendwin_up(tp, rack);
18188 			if ((to.to_flags & TOF_TS) &&
18189 			    (tp->t_flags & TF_REQ_TSTMP)) {
18190 				tp->t_flags |= TF_RCVD_TSTMP;
18191 				tp->ts_recent = to.to_tsval;
18192 				tp->ts_recent_age = cts;
18193 			} else
18194 				tp->t_flags &= ~TF_REQ_TSTMP;
18195 			if (to.to_flags & TOF_MSS) {
18196 				tcp_mss(tp, to.to_mss);
18197 			}
18198 			if ((tp->t_flags & TF_SACK_PERMIT) &&
18199 			    (to.to_flags & TOF_SACKPERM) == 0)
18200 				tp->t_flags &= ~TF_SACK_PERMIT;
18201 			if (IS_FASTOPEN(tp->t_flags)) {
18202 				if (to.to_flags & TOF_FASTOPEN) {
18203 					uint16_t mss;
18204 
18205 					if (to.to_flags & TOF_MSS)
18206 						mss = to.to_mss;
18207 					else
18208 						if ((inp->inp_vflag & INP_IPV6) != 0)
18209 							mss = TCP6_MSS;
18210 						else
18211 							mss = TCP_MSS;
18212 					tcp_fastopen_update_cache(tp, mss,
18213 					    to.to_tfo_len, to.to_tfo_cookie);
18214 				} else
18215 					tcp_fastopen_disable_path(tp);
18216 			}
18217 		}
18218 		/*
18219 		 * At this point we are at the initial call. Here we decide
18220 		 * if we are doing RACK or not. We do this by seeing if
18221 		 * TF_SACK_PERMIT is set and the sack-not-required is clear.
18222 		 * The code now does do dup-ack counting so if you don't
18223 		 * switch back you won't get rack & TLP, but you will still
18224 		 * get this stack.
18225 		 */
18226 
18227 		if ((rack_sack_not_required == 0) &&
18228 		    ((tp->t_flags & TF_SACK_PERMIT) == 0)) {
18229 			tcp_switch_back_to_default(tp);
18230 			(*tp->t_fb->tfb_tcp_do_segment)(tp, m, th, drop_hdrlen,
18231 			    tlen, iptos);
18232 #ifdef TCP_ACCOUNTING
18233 			sched_unpin();
18234 #endif
18235 			return (1);
18236 		}
18237 		tcp_set_hpts(tp);
18238 		sack_filter_clear(&rack->r_ctl.rack_sf, th->th_ack);
18239 	}
18240 	if (thflags & TH_FIN)
18241 		tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
18242 	us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
18243 	if ((rack->rc_gp_dyn_mul) &&
18244 	    (rack->use_fixed_rate == 0) &&
18245 	    (rack->rc_always_pace)) {
18246 		/* Check in on probertt */
18247 		rack_check_probe_rtt(rack, cts);
18248 	}
18249 	rack_clear_rate_sample(rack);
18250 	if ((rack->forced_ack) &&
18251 	    ((tcp_get_flags(th) & TH_RST) == 0)) {
18252 		rack_handle_probe_response(rack, tiwin, us_cts);
18253 	}
18254 	/*
18255 	 * This is the one exception case where we set the rack state
18256 	 * always. All other times (timers etc) we must have a rack-state
18257 	 * set (so we assure we have done the checks above for SACK).
18258 	 */
18259 	rack->r_ctl.rc_rcvtime = cts;
18260 	if (rack->r_state != tp->t_state)
18261 		rack_set_state(tp, rack);
18262 	if (SEQ_GT(th->th_ack, tp->snd_una) &&
18263 	    (rsm = tqhash_min(rack->r_ctl.tqh)) != NULL)
18264 		kern_prefetch(rsm, &prev_state);
18265 	prev_state = rack->r_state;
18266 	if ((thflags & TH_RST) &&
18267 	    ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
18268 	      SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
18269 	     (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq))) {
18270 		/* The connection will be killed by a reset check the tracepoint */
18271 		tcp_trace_point(rack->rc_tp, TCP_TP_RESET_RCV);
18272 	}
18273 	retval = (*rack->r_substate) (m, th, so,
18274 	    tp, &to, drop_hdrlen,
18275 	    tlen, tiwin, thflags, nxt_pkt, iptos);
18276 	if (retval == 0) {
18277 		/*
18278 		 * If retval is 1 the tcb is unlocked and most likely the tp
18279 		 * is gone.
18280 		 */
18281 		INP_WLOCK_ASSERT(inp);
18282 		if ((rack->rc_gp_dyn_mul) &&
18283 		    (rack->rc_always_pace) &&
18284 		    (rack->use_fixed_rate == 0) &&
18285 		    rack->in_probe_rtt &&
18286 		    (rack->r_ctl.rc_time_probertt_starts == 0)) {
18287 			/*
18288 			 * If we are going for target, lets recheck before
18289 			 * we output.
18290 			 */
18291 			rack_check_probe_rtt(rack, cts);
18292 		}
18293 		if (rack->set_pacing_done_a_iw == 0) {
18294 			/* How much has been acked? */
18295 			if ((tp->snd_una - tp->iss) > (ctf_fixed_maxseg(tp) * 10)) {
18296 				/* We have enough to set in the pacing segment size */
18297 				rack->set_pacing_done_a_iw = 1;
18298 				rack_set_pace_segments(tp, rack, __LINE__, NULL);
18299 			}
18300 		}
18301 		tcp_rack_xmit_timer_commit(rack, tp);
18302 #ifdef TCP_ACCOUNTING
18303 		/*
18304 		 * If we set the ack_val_se to what ack processing we are doing
18305 		 * we also want to track how many cycles we burned. Note
18306 		 * the bits after tcp_output we let be "free". This is because
18307 		 * we are also tracking the tcp_output times as well. Note the
18308 		 * use of 0xf here since we only have 11 counter (0 - 0xa) and
18309 		 * 0xf cannot be returned and is what we initialize it too to
18310 		 * indicate we are not doing the tabulations.
18311 		 */
18312 		if (ack_val_set != 0xf) {
18313 			uint64_t crtsc;
18314 
18315 			crtsc = get_cyclecount();
18316 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
18317 				tp->tcp_proc_time[ack_val_set] += (crtsc - ts_val);
18318 			}
18319 		}
18320 #endif
18321 		if ((nxt_pkt == 0) && (no_output == 0)) {
18322 			if ((rack->r_wanted_output != 0) ||
18323 			    (tp->t_flags & TF_ACKNOW) ||
18324 			    (rack->r_fast_output != 0)) {
18325 
18326 do_output_now:
18327 				if (tcp_output(tp) < 0) {
18328 #ifdef TCP_ACCOUNTING
18329 					sched_unpin();
18330 #endif
18331 					return (1);
18332 				}
18333 				did_out = 1;
18334 			}
18335 			rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
18336 			rack_free_trim(rack);
18337 		} else if ((nxt_pkt == 0) && (tp->t_flags & TF_ACKNOW)) {
18338 			goto do_output_now;
18339 		} else if ((no_output == 1) &&
18340 			   (nxt_pkt == 0)  &&
18341 			   (tcp_in_hpts(rack->rc_tp) == 0)) {
18342 			/*
18343 			 * We are not in hpts and we had a pacing timer up. Use
18344 			 * the remaining time (slot_remaining) to restart the timer.
18345 			 */
18346 			KASSERT ((slot_remaining != 0), ("slot remaining is zero for rack:%p tp:%p", rack, tp));
18347 			rack_start_hpts_timer(rack, tp, cts, slot_remaining, 0, 0);
18348 			rack_free_trim(rack);
18349 		}
18350 		/* Clear the flag, it may have been cleared by output but we may not have  */
18351 		if ((nxt_pkt == 0) && (tp->t_flags2 & TF2_HPTS_CALLS))
18352 			tp->t_flags2 &= ~TF2_HPTS_CALLS;
18353 		/*
18354 		 * The draft (v3) calls for us to use SEQ_GEQ, but that
18355 		 * causes issues when we are just going app limited. Lets
18356 		 * instead use SEQ_GT <or> where its equal but more data
18357 		 * is outstanding.
18358 		 *
18359 		 * Also make sure we are on the last ack of a series. We
18360 		 * have to have all the ack's processed in queue to know
18361 		 * if there is something left outstanding.
18362 		 */
18363 		if (SEQ_GEQ(tp->snd_una, rack->r_ctl.roundends) &&
18364 		    (rack->rc_new_rnd_needed == 0) &&
18365 		    (nxt_pkt == 0)) {
18366 			/*
18367 			 * We have crossed into a new round with
18368 			 * the new snd_unae.
18369 			 */
18370 			rack_new_round_setup(tp, rack, tp->snd_una);
18371 		}
18372 		if ((nxt_pkt == 0) &&
18373 		    ((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) &&
18374 		    (SEQ_GT(tp->snd_max, tp->snd_una) ||
18375 		     (tp->t_flags & TF_DELACK) ||
18376 		     ((V_tcp_always_keepalive || rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
18377 		      (tp->t_state <= TCPS_CLOSING)))) {
18378 			/* We could not send (probably in the hpts but stopped the timer earlier)? */
18379 			if ((tp->snd_max == tp->snd_una) &&
18380 			    ((tp->t_flags & TF_DELACK) == 0) &&
18381 			    (tcp_in_hpts(rack->rc_tp)) &&
18382 			    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
18383 				/* keep alive not needed if we are hptsi output yet */
18384 				;
18385 			} else {
18386 				int late = 0;
18387 				if (tcp_in_hpts(tp)) {
18388 					if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
18389 						us_cts = tcp_get_usecs(NULL);
18390 						if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
18391 							rack->r_early = 1;
18392 							rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
18393 						} else
18394 							late = 1;
18395 						rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
18396 					}
18397 					tcp_hpts_remove(tp);
18398 				}
18399 				if (late && (did_out == 0)) {
18400 					/*
18401 					 * We are late in the sending
18402 					 * and we did not call the output
18403 					 * (this probably should not happen).
18404 					 */
18405 					goto do_output_now;
18406 				}
18407 				rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
18408 			}
18409 			way_out = 1;
18410 		} else if (nxt_pkt == 0) {
18411 			/* Do we have the correct timer running? */
18412 			rack_timer_audit(tp, rack, &so->so_snd);
18413 			way_out = 2;
18414 		}
18415 	done_with_input:
18416 		rack_log_doseg_done(rack, cts, nxt_pkt, did_out, way_out, max(1, nsegs));
18417 		if (did_out)
18418 			rack->r_wanted_output = 0;
18419 	}
18420 
18421 #ifdef TCP_ACCOUNTING
18422 	sched_unpin();
18423 #endif
18424 	return (retval);
18425 }
18426 
18427 static void
18428 rack_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
18429     int32_t drop_hdrlen, int32_t tlen, uint8_t iptos)
18430 {
18431 	struct timeval tv;
18432 
18433 	/* First lets see if we have old packets */
18434 	if (!STAILQ_EMPTY(&tp->t_inqueue)) {
18435 		if (ctf_do_queued_segments(tp, 1)) {
18436 			m_freem(m);
18437 			return;
18438 		}
18439 	}
18440 	if (m->m_flags & M_TSTMP_LRO) {
18441 		mbuf_tstmp2timeval(m, &tv);
18442 	} else {
18443 		/* Should not be should we kassert instead? */
18444 		tcp_get_usecs(&tv);
18445 	}
18446 	if (rack_do_segment_nounlock(tp, m, th, drop_hdrlen, tlen, iptos, 0,
18447 	    &tv) == 0) {
18448 		INP_WUNLOCK(tptoinpcb(tp));
18449 	}
18450 }
18451 
18452 struct rack_sendmap *
18453 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tsused)
18454 {
18455 	struct rack_sendmap *rsm = NULL;
18456 	int32_t idx;
18457 	uint32_t srtt = 0, thresh = 0, ts_low = 0;
18458 	int no_sack = 0;
18459 
18460 	/* Return the next guy to be re-transmitted */
18461 	if (tqhash_empty(rack->r_ctl.tqh)) {
18462 		return (NULL);
18463 	}
18464 	if (tp->t_flags & TF_SENTFIN) {
18465 		/* retran the end FIN? */
18466 		return (NULL);
18467 	}
18468 	/* ok lets look at this one */
18469 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
18470 	if (rack->r_must_retran && rsm && (rsm->r_flags & RACK_MUST_RXT)) {
18471 		return (rsm);
18472 	}
18473 	if (rsm && ((rsm->r_flags & RACK_ACKED) == 0)) {
18474 		goto check_it;
18475 	}
18476 	rsm = rack_find_lowest_rsm(rack);
18477 	if (rsm == NULL) {
18478 		return (NULL);
18479 	}
18480 check_it:
18481 	if (((rack->rc_tp->t_flags & TF_SACK_PERMIT) == 0) ||
18482 	    (rack->sack_attack_disable > 0)) {
18483 		no_sack = 1;
18484 	}
18485 	if ((no_sack > 0) &&
18486 	    (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
18487 		/*
18488 		 * No sack so we automatically do the 3 strikes and
18489 		 * retransmit (no rack timer would be started).
18490 		 */
18491 		return (rsm);
18492 	}
18493 	if (rsm->r_flags & RACK_ACKED) {
18494 		return (NULL);
18495 	}
18496 	if (((rsm->r_flags & RACK_SACK_PASSED) == 0) &&
18497 	    (rsm->r_dupack < DUP_ACK_THRESHOLD)) {
18498 		/* Its not yet ready */
18499 		return (NULL);
18500 	}
18501 	srtt = rack_grab_rtt(tp, rack);
18502 	idx = rsm->r_rtr_cnt - 1;
18503 	ts_low = (uint32_t)rsm->r_tim_lastsent[idx];
18504 	thresh = rack_calc_thresh_rack(rack, srtt, tsused, __LINE__, 1);
18505 	if ((tsused == ts_low) ||
18506 	    (TSTMP_LT(tsused, ts_low))) {
18507 		/* No time since sending */
18508 		return (NULL);
18509 	}
18510 	if ((tsused - ts_low) < thresh) {
18511 		/* It has not been long enough yet */
18512 		return (NULL);
18513 	}
18514 	if ((rsm->r_dupack >= DUP_ACK_THRESHOLD) ||
18515 	    ((rsm->r_flags & RACK_SACK_PASSED) &&
18516 	     (rack->sack_attack_disable == 0))) {
18517 		/*
18518 		 * We have passed the dup-ack threshold <or>
18519 		 * a SACK has indicated this is missing.
18520 		 * Note that if you are a declared attacker
18521 		 * it is only the dup-ack threshold that
18522 		 * will cause retransmits.
18523 		 */
18524 		/* log retransmit reason */
18525 		rack_log_retran_reason(rack, rsm, (tsused - ts_low), thresh, 1);
18526 		rack->r_fast_output = 0;
18527 		return (rsm);
18528 	}
18529 	return (NULL);
18530 }
18531 
18532 static void
18533 rack_log_pacing_delay_calc (struct tcp_rack *rack, uint32_t len, uint32_t slot,
18534 			   uint64_t bw_est, uint64_t bw, uint64_t len_time, int method,
18535 			   int line, struct rack_sendmap *rsm, uint8_t quality)
18536 {
18537 	if (tcp_bblogging_on(rack->rc_tp)) {
18538 		union tcp_log_stackspecific log;
18539 		struct timeval tv;
18540 
18541 		if (rack_verbose_logging == 0) {
18542 			/*
18543 			 * We are not verbose screen out all but
18544 			 * ones we always want.
18545 			 */
18546 			if ((method != 2) &&
18547 			    (method != 3) &&
18548 			    (method != 7) &&
18549 			    (method != 89) &&
18550 			    (method != 14) &&
18551 			    (method != 20)) {
18552 				return;
18553 			}
18554 		}
18555 		memset(&log, 0, sizeof(log));
18556 		log.u_bbr.flex1 = slot;
18557 		log.u_bbr.flex2 = len;
18558 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_min_segs;
18559 		log.u_bbr.flex4 = rack->r_ctl.rc_pace_max_segs;
18560 		log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ss;
18561 		log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_ca;
18562 		log.u_bbr.use_lt_bw = rack->rc_ack_can_sendout_data;
18563 		log.u_bbr.use_lt_bw <<= 1;
18564 		log.u_bbr.use_lt_bw |= rack->r_late;
18565 		log.u_bbr.use_lt_bw <<= 1;
18566 		log.u_bbr.use_lt_bw |= rack->r_early;
18567 		log.u_bbr.use_lt_bw <<= 1;
18568 		log.u_bbr.use_lt_bw |= rack->app_limited_needs_set;
18569 		log.u_bbr.use_lt_bw <<= 1;
18570 		log.u_bbr.use_lt_bw |= rack->rc_gp_filled;
18571 		log.u_bbr.use_lt_bw <<= 1;
18572 		log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt;
18573 		log.u_bbr.use_lt_bw <<= 1;
18574 		log.u_bbr.use_lt_bw |= rack->in_probe_rtt;
18575 		log.u_bbr.use_lt_bw <<= 1;
18576 		log.u_bbr.use_lt_bw |= rack->gp_ready;
18577 		log.u_bbr.pkt_epoch = line;
18578 		log.u_bbr.epoch = rack->r_ctl.rc_agg_delayed;
18579 		log.u_bbr.lt_epoch = rack->r_ctl.rc_agg_early;
18580 		log.u_bbr.applimited = rack->r_ctl.rack_per_of_gp_rec;
18581 		log.u_bbr.bw_inuse = bw_est;
18582 		log.u_bbr.delRate = bw;
18583 		if (rack->r_ctl.gp_bw == 0)
18584 			log.u_bbr.cur_del_rate = 0;
18585 		else
18586 			log.u_bbr.cur_del_rate = rack_get_bw(rack);
18587 		log.u_bbr.rttProp = len_time;
18588 		log.u_bbr.pkts_out = rack->r_ctl.rc_rack_min_rtt;
18589 		log.u_bbr.lost = rack->r_ctl.rc_probertt_sndmax_atexit;
18590 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm);
18591 		if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh) {
18592 			/* We are in slow start */
18593 			log.u_bbr.flex7 = 1;
18594 		} else {
18595 			/* we are on congestion avoidance */
18596 			log.u_bbr.flex7 = 0;
18597 		}
18598 		log.u_bbr.flex8 = method;
18599 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
18600 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18601 		log.u_bbr.cwnd_gain = rack->rc_gp_saw_rec;
18602 		log.u_bbr.cwnd_gain <<= 1;
18603 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss;
18604 		log.u_bbr.cwnd_gain <<= 1;
18605 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca;
18606 		log.u_bbr.bbr_substate = quality;
18607 		log.u_bbr.bbr_state = rack->dgp_on;
18608 		log.u_bbr.bbr_state <<= 1;
18609 		log.u_bbr.bbr_state |= rack->rc_pace_to_cwnd;
18610 		log.u_bbr.bbr_state <<= 2;
18611 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
18612 		    &rack->rc_inp->inp_socket->so_rcv,
18613 		    &rack->rc_inp->inp_socket->so_snd,
18614 		    BBR_LOG_HPTSI_CALC, 0,
18615 		    0, &log, false, &tv);
18616 	}
18617 }
18618 
18619 static uint32_t
18620 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss)
18621 {
18622 	uint32_t new_tso, user_max, pace_one;
18623 
18624 	user_max = rack->rc_user_set_max_segs * mss;
18625 	if (rack->rc_force_max_seg) {
18626 		return (user_max);
18627 	}
18628 	if (rack->use_fixed_rate &&
18629 	    ((rack->r_ctl.crte == NULL) ||
18630 	     (bw != rack->r_ctl.crte->rate))) {
18631 		/* Use the user mss since we are not exactly matched */
18632 		return (user_max);
18633 	}
18634 	if (rack_pace_one_seg ||
18635 	    (rack->r_ctl.rc_user_set_min_segs == 1))
18636 		pace_one = 1;
18637 	else
18638 		pace_one = 0;
18639 
18640 	new_tso = tcp_get_pacing_burst_size_w_divisor(rack->rc_tp, bw, mss,
18641 		     pace_one, rack->r_ctl.crte, NULL, rack->r_ctl.pace_len_divisor);
18642 	if (new_tso > user_max)
18643 		new_tso = user_max;
18644 	if (rack->rc_hybrid_mode && rack->r_ctl.client_suggested_maxseg) {
18645 		if (((uint32_t)rack->r_ctl.client_suggested_maxseg * mss) > new_tso)
18646 			new_tso = (uint32_t)rack->r_ctl.client_suggested_maxseg * mss;
18647 	}
18648 	if (rack->r_ctl.rc_user_set_min_segs &&
18649 	    ((rack->r_ctl.rc_user_set_min_segs * mss) > new_tso))
18650 	    new_tso = rack->r_ctl.rc_user_set_min_segs * mss;
18651 	return (new_tso);
18652 }
18653 
18654 static uint64_t
18655 rack_arrive_at_discounted_rate(struct tcp_rack *rack, uint64_t window_input, uint32_t *rate_set, uint32_t *gain_b)
18656 {
18657 	uint64_t reduced_win;
18658 	uint32_t gain;
18659 
18660 	if (window_input < rc_init_window(rack)) {
18661 		/*
18662 		 * The cwnd is collapsed to
18663 		 * nearly zero, maybe because of a time-out?
18664 		 * Lets drop back to the lt-bw.
18665 		 */
18666 		reduced_win = rack_get_lt_bw(rack);
18667 		/* Set the flag so the caller knows its a rate and not a reduced window */
18668 		*rate_set = 1;
18669 		gain = 100;
18670 	} else if  (IN_RECOVERY(rack->rc_tp->t_flags)) {
18671 		/*
18672 		 * If we are in recover our cwnd needs to be less for
18673 		 * our pacing consideration.
18674 		 */
18675 		if (rack->rack_hibeta == 0) {
18676 			reduced_win = window_input / 2;
18677 			gain = 50;
18678 		} else {
18679 			reduced_win = window_input * rack->r_ctl.saved_hibeta;
18680 			reduced_win /= 100;
18681 			gain = rack->r_ctl.saved_hibeta;
18682 		}
18683 	} else {
18684 		/*
18685 		 * Apply Timely factor to increase/decrease the
18686 		 * amount we are pacing at.
18687 		 */
18688 		gain = rack_get_output_gain(rack, NULL);
18689 		if (gain > rack_gain_p5_ub) {
18690 			gain = rack_gain_p5_ub;
18691 		}
18692 		reduced_win = window_input * gain;
18693 		reduced_win /= 100;
18694 	}
18695 	if (gain_b != NULL)
18696 		*gain_b = gain;
18697 	/*
18698 	 * What is being returned here is a trimmed down
18699 	 * window values in all cases where rate_set is left
18700 	 * at 0. In one case we actually return the rate (lt_bw).
18701 	 * the "reduced_win" is returned as a slimmed down cwnd that
18702 	 * is then calculated by the caller into a rate when rate_set
18703 	 * is 0.
18704 	 */
18705 	return (reduced_win);
18706 }
18707 
18708 static int32_t
18709 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)
18710 {
18711 	uint64_t lentim, fill_bw;
18712 
18713 	rack->r_via_fill_cw = 0;
18714 	if (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.cwnd_to_use)
18715 		return (slot);
18716 	if ((ctf_outstanding(rack->rc_tp) + (segsiz-1)) > rack->rc_tp->snd_wnd)
18717 		return (slot);
18718 	if (rack->r_ctl.rc_last_us_rtt == 0)
18719 		return (slot);
18720 	if (rack->rc_pace_fill_if_rttin_range &&
18721 	    (rack->r_ctl.rc_last_us_rtt >=
18722 	     (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack->rtt_limit_mul))) {
18723 		/* The rtt is huge, N * smallest, lets not fill */
18724 		return (slot);
18725 	}
18726 	if (rack->r_ctl.fillcw_cap && *rate_wanted >= rack->r_ctl.fillcw_cap)
18727 		return (slot);
18728 	/*
18729 	 * first lets calculate the b/w based on the last us-rtt
18730 	 * and the the smallest send window.
18731 	 */
18732 	fill_bw = min(rack->rc_tp->snd_cwnd, rack->r_ctl.cwnd_to_use);
18733 	if (rack->rc_fillcw_apply_discount) {
18734 		uint32_t rate_set = 0;
18735 
18736 		fill_bw = rack_arrive_at_discounted_rate(rack, fill_bw, &rate_set, NULL);
18737 		if (rate_set) {
18738 			goto at_lt_bw;
18739 		}
18740 	}
18741 	/* Take the rwnd if its smaller */
18742 	if (fill_bw > rack->rc_tp->snd_wnd)
18743 		fill_bw = rack->rc_tp->snd_wnd;
18744 	/* Now lets make it into a b/w */
18745 	fill_bw *= (uint64_t)HPTS_USEC_IN_SEC;
18746 	fill_bw /= (uint64_t)rack->r_ctl.rc_last_us_rtt;
18747 	/* Adjust to any cap */
18748 	if (rack->r_ctl.fillcw_cap && fill_bw >= rack->r_ctl.fillcw_cap)
18749 		fill_bw = rack->r_ctl.fillcw_cap;
18750 
18751 at_lt_bw:
18752 	if (rack_bw_multipler > 0) {
18753 		/*
18754 		 * We want to limit fill-cw to the some multiplier
18755 		 * of the max(lt_bw, gp_est). The normal default
18756 		 * is 0 for off, so a sysctl has enabled it.
18757 		 */
18758 		uint64_t lt_bw, gp, rate;
18759 
18760 		gp = rack_get_gp_est(rack);
18761 		lt_bw = rack_get_lt_bw(rack);
18762 		if (lt_bw > gp)
18763 			rate = lt_bw;
18764 		else
18765 			rate = gp;
18766 		rate *= rack_bw_multipler;
18767 		rate /= 100;
18768 		if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
18769 			union tcp_log_stackspecific log;
18770 			struct timeval tv;
18771 
18772 			memset(&log.u_bbr, 0, sizeof(log.u_bbr));
18773 			log.u_bbr.timeStamp = tcp_get_usecs(&tv);
18774 			log.u_bbr.flex1 = rack_bw_multipler;
18775 			log.u_bbr.flex2 = len;
18776 			log.u_bbr.cur_del_rate = gp;
18777 			log.u_bbr.delRate = lt_bw;
18778 			log.u_bbr.bw_inuse = rate;
18779 			log.u_bbr.rttProp = fill_bw;
18780 			log.u_bbr.flex8 = 44;
18781 			tcp_log_event(rack->rc_tp, NULL, NULL, NULL,
18782 				      BBR_LOG_CWND, 0,
18783 				      0, &log, false, NULL,
18784 				      __func__, __LINE__, &tv);
18785 		}
18786 		if (fill_bw > rate)
18787 			fill_bw = rate;
18788 	}
18789 	/* We are below the min b/w */
18790 	if (non_paced)
18791 		*rate_wanted = fill_bw;
18792 	if ((fill_bw < RACK_MIN_BW) || (fill_bw < *rate_wanted))
18793 		return (slot);
18794 	rack->r_via_fill_cw = 1;
18795 	if (rack->r_rack_hw_rate_caps &&
18796 	    (rack->r_ctl.crte != NULL)) {
18797 		uint64_t high_rate;
18798 
18799 		high_rate = tcp_hw_highest_rate(rack->r_ctl.crte);
18800 		if (fill_bw > high_rate) {
18801 			/* We are capping bw at the highest rate table entry */
18802 			if (*rate_wanted > high_rate) {
18803 				/* The original rate was also capped */
18804 				rack->r_via_fill_cw = 0;
18805 			}
18806 			rack_log_hdwr_pacing(rack,
18807 					     fill_bw, high_rate, __LINE__,
18808 					     0, 3);
18809 			fill_bw = high_rate;
18810 			if (capped)
18811 				*capped = 1;
18812 		}
18813 	} else if ((rack->r_ctl.crte == NULL) &&
18814 		   (rack->rack_hdrw_pacing == 0) &&
18815 		   (rack->rack_hdw_pace_ena) &&
18816 		   rack->r_rack_hw_rate_caps &&
18817 		   (rack->rack_attempt_hdwr_pace == 0) &&
18818 		   (rack->rc_inp->inp_route.ro_nh != NULL) &&
18819 		   (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
18820 		/*
18821 		 * Ok we may have a first attempt that is greater than our top rate
18822 		 * lets check.
18823 		 */
18824 		uint64_t high_rate;
18825 
18826 		high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp);
18827 		if (high_rate) {
18828 			if (fill_bw > high_rate) {
18829 				fill_bw = high_rate;
18830 				if (capped)
18831 					*capped = 1;
18832 			}
18833 		}
18834 	}
18835 	if (rack->r_ctl.bw_rate_cap && (fill_bw > rack->r_ctl.bw_rate_cap)) {
18836 		rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
18837 				   fill_bw, 0, 0, HYBRID_LOG_RATE_CAP, 2, NULL, __LINE__);
18838 		fill_bw = rack->r_ctl.bw_rate_cap;
18839 	}
18840 	/*
18841 	 * Ok fill_bw holds our mythical b/w to fill the cwnd
18842 	 * in an rtt (unless it was capped), what does that
18843 	 * time wise equate too?
18844 	 */
18845 	lentim = (uint64_t)(len) * (uint64_t)HPTS_USEC_IN_SEC;
18846 	lentim /= fill_bw;
18847 	*rate_wanted = fill_bw;
18848 	if (non_paced || (lentim < slot)) {
18849 		rack_log_pacing_delay_calc(rack, len, slot, fill_bw,
18850 					   0, lentim, 12, __LINE__, NULL, 0);
18851 		return ((int32_t)lentim);
18852 	} else
18853 		return (slot);
18854 }
18855 
18856 static uint32_t
18857 rack_policer_check_send(struct tcp_rack *rack, uint32_t len, uint32_t segsiz, uint32_t *needs)
18858 {
18859 	uint64_t calc;
18860 
18861 	rack->rc_policer_should_pace = 0;
18862 	calc = rack_policer_bucket_reserve * rack->r_ctl.policer_bucket_size;
18863 	calc /= 100;
18864 	/*
18865 	 * Now lets look at if we want more than is in the bucket <or>
18866 	 * we want more than is reserved in the bucket.
18867 	 */
18868 	if (rack_verbose_logging > 0)
18869 		policer_detection_log(rack, len, segsiz, calc, rack->r_ctl.current_policer_bucket, 8);
18870 	if ((calc > rack->r_ctl.current_policer_bucket) ||
18871 	    (len >= (rack->r_ctl.current_policer_bucket - calc))) {
18872 		/*
18873 		 * We may want to pace depending on if we are going
18874 		 * into the reserve or not.
18875 		 */
18876 		uint32_t newlen;
18877 
18878 		if (calc > rack->r_ctl.current_policer_bucket) {
18879 			/*
18880 			 * This will eat into the reserve if we
18881 			 * don't have room at all some lines
18882 			 * below will catch it.
18883 			 */
18884 			newlen = rack->r_ctl.policer_max_seg;
18885 			rack->rc_policer_should_pace = 1;
18886 		} else {
18887 			/*
18888 			 * We have all of the reserve plus something in the bucket
18889 			 * that we can give out.
18890 			 */
18891 			newlen = rack->r_ctl.current_policer_bucket - calc;
18892 			if (newlen < rack->r_ctl.policer_max_seg) {
18893 				/*
18894 				 * Into the reserve to get a full policer_max_seg
18895 				 * so we set the len to that and eat into
18896 				 * the reserve. If we go over the code
18897 				 * below will make us wait.
18898 				 */
18899 				newlen = rack->r_ctl.policer_max_seg;
18900 				rack->rc_policer_should_pace = 1;
18901 			}
18902 		}
18903 		if (newlen > rack->r_ctl.current_policer_bucket) {
18904 			/* We have to wait some */
18905 			*needs = newlen - rack->r_ctl.current_policer_bucket;
18906 			return (0);
18907 		}
18908 		if (rack_verbose_logging > 0)
18909 			policer_detection_log(rack, len, segsiz, newlen, 0, 9);
18910 		len = newlen;
18911 	} /* else we have all len available above the reserve */
18912 	if (rack_verbose_logging > 0)
18913 		policer_detection_log(rack, len, segsiz, calc, 0, 10);
18914 	return (len);
18915 }
18916 
18917 static uint32_t
18918 rack_policed_sending(struct tcp_rack *rack, struct tcpcb *tp, uint32_t len, uint32_t segsiz, int call_line)
18919 {
18920 	/*
18921 	 * Given a send of len, and a token bucket set at current_policer_bucket_size
18922 	 * are we close enough to the end of the bucket that we need to pace? If so
18923 	 * calculate out a time and return it. Otherwise subtract the tokens from
18924 	 * the bucket.
18925 	 */
18926 	uint64_t calc;
18927 
18928 	if ((rack->r_ctl.policer_bw == 0) ||
18929 	    (rack->r_ctl.policer_bucket_size < segsiz)) {
18930 		/*
18931 		 * We should have an estimate here...
18932 		 */
18933 		return (0);
18934 	}
18935 	calc = (uint64_t)rack_policer_bucket_reserve * (uint64_t)rack->r_ctl.policer_bucket_size;
18936 	calc /= 100;
18937 	if ((rack->r_ctl.current_policer_bucket < len) ||
18938 	    (rack->rc_policer_should_pace == 1) ||
18939 	    ((rack->r_ctl.current_policer_bucket - len) <= (uint32_t)calc)) {
18940 		/* we need to pace */
18941 		uint64_t lentim, res;
18942 		uint32_t slot;
18943 
18944 		lentim = (uint64_t)len * (uint64_t)HPTS_USEC_IN_SEC;
18945 		res = lentim / rack->r_ctl.policer_bw;
18946 		slot = (uint32_t)res;
18947 		if (rack->r_ctl.current_policer_bucket > len)
18948 			rack->r_ctl.current_policer_bucket -= len;
18949 		else
18950 			rack->r_ctl.current_policer_bucket = 0;
18951 		policer_detection_log(rack, len, slot, (uint32_t)rack_policer_bucket_reserve, call_line, 5);
18952 		rack->rc_policer_should_pace = 0;
18953 		return(slot);
18954 	}
18955 	/* Just take tokens out of the bucket and let rack do whatever it would have */
18956 	policer_detection_log(rack, len, 0, (uint32_t)rack_policer_bucket_reserve, call_line, 6);
18957 	if (len < rack->r_ctl.current_policer_bucket) {
18958 		rack->r_ctl.current_policer_bucket -= len;
18959 	} else {
18960 		rack->r_ctl.current_policer_bucket = 0;
18961 	}
18962 	return (0);
18963 }
18964 
18965 
18966 static int32_t
18967 rack_get_pacing_delay(struct tcp_rack *rack, struct tcpcb *tp, uint32_t len, struct rack_sendmap *rsm, uint32_t segsiz, int line)
18968 {
18969 	uint64_t srtt;
18970 	int32_t slot = 0;
18971 	int32_t minslot = 0;
18972 	int can_start_hw_pacing = 1;
18973 	int err;
18974 	int pace_one;
18975 
18976 	if (rack_pace_one_seg ||
18977 	    (rack->r_ctl.rc_user_set_min_segs == 1))
18978 		pace_one = 1;
18979 	else
18980 		pace_one = 0;
18981 	if (rack->rc_policer_detected == 1) {
18982 		/*
18983 		 * A policer has been detected and we
18984 		 * have all of our data (policer-bw and
18985 		 * policer bucket size) calculated. Call
18986 		 * into the function to find out if we are
18987 		 * overriding the time.
18988 		 */
18989 		slot = rack_policed_sending(rack, tp, len, segsiz, line);
18990 		if (slot) {
18991 			uint64_t logbw;
18992 
18993 			logbw = rack->r_ctl.current_policer_bucket;
18994 			logbw <<= 32;
18995 			logbw |= rack->r_ctl.policer_bucket_size;
18996 			rack_log_pacing_delay_calc(rack, len, slot, rack->r_ctl.policer_bw, logbw, 0, 89, __LINE__, NULL, 0);
18997 			return(slot);
18998 		}
18999 	}
19000 	if (rack->rc_always_pace == 0) {
19001 		/*
19002 		 * We use the most optimistic possible cwnd/srtt for
19003 		 * sending calculations. This will make our
19004 		 * calculation anticipate getting more through
19005 		 * quicker then possible. But thats ok we don't want
19006 		 * the peer to have a gap in data sending.
19007 		 */
19008 		uint64_t cwnd, tr_perms = 0;
19009 		int32_t reduce = 0;
19010 
19011 	old_method:
19012 		/*
19013 		 * We keep no precise pacing with the old method
19014 		 * instead we use the pacer to mitigate bursts.
19015 		 */
19016 		if (rack->r_ctl.rc_rack_min_rtt)
19017 			srtt = rack->r_ctl.rc_rack_min_rtt;
19018 		else
19019 			srtt = max(tp->t_srtt, 1);
19020 		if (rack->r_ctl.rc_rack_largest_cwnd)
19021 			cwnd = rack->r_ctl.rc_rack_largest_cwnd;
19022 		else
19023 			cwnd = rack->r_ctl.cwnd_to_use;
19024 		/* Inflate cwnd by 1000 so srtt of usecs is in ms */
19025 		tr_perms = (cwnd * 1000) / srtt;
19026 		if (tr_perms == 0) {
19027 			tr_perms = ctf_fixed_maxseg(tp);
19028 		}
19029 		/*
19030 		 * Calculate how long this will take to drain, if
19031 		 * the calculation comes out to zero, thats ok we
19032 		 * will use send_a_lot to possibly spin around for
19033 		 * more increasing tot_len_this_send to the point
19034 		 * that its going to require a pace, or we hit the
19035 		 * cwnd. Which in that case we are just waiting for
19036 		 * a ACK.
19037 		 */
19038 		slot = len / tr_perms;
19039 		/* Now do we reduce the time so we don't run dry? */
19040 		if (slot && rack_slot_reduction) {
19041 			reduce = (slot / rack_slot_reduction);
19042 			if (reduce < slot) {
19043 				slot -= reduce;
19044 			} else
19045 				slot = 0;
19046 		}
19047 		slot *= HPTS_USEC_IN_MSEC;
19048 		if (rack->rc_pace_to_cwnd) {
19049 			uint64_t rate_wanted = 0;
19050 
19051 			slot = pace_to_fill_cwnd(rack, slot, len, segsiz, NULL, &rate_wanted, 1);
19052 			rack->rc_ack_can_sendout_data = 1;
19053 			rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, 0, 0, 14, __LINE__, NULL, 0);
19054 		} else
19055 			rack_log_pacing_delay_calc(rack, len, slot, tr_perms, reduce, 0, 7, __LINE__, NULL, 0);
19056 		/*******************************************************/
19057 		/* RRS: We insert non-paced call to stats here for len */
19058 		/*******************************************************/
19059 	} else {
19060 		uint64_t bw_est, res, lentim, rate_wanted;
19061 		uint32_t segs, oh;
19062 		int capped = 0;
19063 		int prev_fill;
19064 
19065 		if ((rack->r_rr_config == 1) && rsm) {
19066 			return (rack->r_ctl.rc_min_to);
19067 		}
19068 		if (rack->use_fixed_rate) {
19069 			rate_wanted = bw_est = rack_get_fixed_pacing_bw(rack);
19070 		} else if ((rack->r_ctl.init_rate == 0) &&
19071 			   (rack->r_ctl.gp_bw == 0)) {
19072 			/* no way to yet do an estimate */
19073 			bw_est = rate_wanted = 0;
19074 		} else if (rack->dgp_on) {
19075 			bw_est = rack_get_bw(rack);
19076 			rate_wanted = rack_get_output_bw(rack, bw_est, rsm, &capped);
19077 		} else {
19078 			uint32_t gain, rate_set = 0;
19079 
19080 			rate_wanted = min(rack->rc_tp->snd_cwnd, rack->r_ctl.cwnd_to_use);
19081 			rate_wanted = rack_arrive_at_discounted_rate(rack, rate_wanted, &rate_set, &gain);
19082 			if (rate_set == 0) {
19083 				if (rate_wanted > rack->rc_tp->snd_wnd)
19084 					rate_wanted = rack->rc_tp->snd_wnd;
19085 				/* Now lets make it into a b/w */
19086 				rate_wanted *= (uint64_t)HPTS_USEC_IN_SEC;
19087 				rate_wanted /= (uint64_t)rack->r_ctl.rc_last_us_rtt;
19088 			}
19089 			bw_est = rate_wanted;
19090 			rack_log_pacing_delay_calc(rack, rack->rc_tp->snd_cwnd,
19091 						   rack->r_ctl.cwnd_to_use,
19092 						   rate_wanted, bw_est,
19093 						   rack->r_ctl.rc_last_us_rtt,
19094 						   88, __LINE__, NULL, gain);
19095 		}
19096 		if ((bw_est == 0) || (rate_wanted == 0) ||
19097 		    ((rack->gp_ready == 0) && (rack->use_fixed_rate == 0))) {
19098 			/*
19099 			 * No way yet to make a b/w estimate or
19100 			 * our raise is set incorrectly.
19101 			 */
19102 			goto old_method;
19103 		}
19104 		rack_rate_cap_bw(rack, &rate_wanted, &capped);
19105 		/* We need to account for all the overheads */
19106 		segs = (len + segsiz - 1) / segsiz;
19107 		/*
19108 		 * We need the diff between 1514 bytes (e-mtu with e-hdr)
19109 		 * and how much data we put in each packet. Yes this
19110 		 * means we may be off if we are larger than 1500 bytes
19111 		 * or smaller. But this just makes us more conservative.
19112 		 */
19113 
19114 		oh =  (tp->t_maxseg - segsiz) + sizeof(struct tcphdr);
19115 		if (rack->r_is_v6) {
19116 #ifdef INET6
19117 			oh += sizeof(struct ip6_hdr);
19118 #endif
19119 		} else {
19120 #ifdef INET
19121 			oh += sizeof(struct ip);
19122 #endif
19123 		}
19124 		/* We add a fixed 14 for the ethernet header */
19125 		oh += 14;
19126 		segs *= oh;
19127 		lentim = (uint64_t)(len + segs) * (uint64_t)HPTS_USEC_IN_SEC;
19128 		res = lentim / rate_wanted;
19129 		slot = (uint32_t)res;
19130 		if (rack_hw_rate_min &&
19131 		    (rate_wanted < rack_hw_rate_min)) {
19132 			can_start_hw_pacing = 0;
19133 			if (rack->r_ctl.crte) {
19134 				/*
19135 				 * Ok we need to release it, we
19136 				 * have fallen too low.
19137 				 */
19138 				tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
19139 				rack->r_ctl.crte = NULL;
19140 				rack->rack_attempt_hdwr_pace = 0;
19141 				rack->rack_hdrw_pacing = 0;
19142 			}
19143 		}
19144 		if (rack->r_ctl.crte &&
19145 		    (tcp_hw_highest_rate(rack->r_ctl.crte) < rate_wanted)) {
19146 			/*
19147 			 * We want more than the hardware can give us,
19148 			 * don't start any hw pacing.
19149 			 */
19150 			can_start_hw_pacing = 0;
19151 			if (rack->r_rack_hw_rate_caps == 0) {
19152 				/*
19153 				 * Ok we need to release it, we
19154 				 * want more than the card can give us and
19155 				 * no rate cap is in place. Set it up so
19156 				 * when we want less we can retry.
19157 				 */
19158 				tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
19159 				rack->r_ctl.crte = NULL;
19160 				rack->rack_attempt_hdwr_pace = 0;
19161 				rack->rack_hdrw_pacing = 0;
19162 			}
19163 		}
19164 		if ((rack->r_ctl.crte != NULL) && (rack->rc_inp->inp_snd_tag == NULL)) {
19165 			/*
19166 			 * We lost our rate somehow, this can happen
19167 			 * if the interface changed underneath us.
19168 			 */
19169 			tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
19170 			rack->r_ctl.crte = NULL;
19171 			/* Lets re-allow attempting to setup pacing */
19172 			rack->rack_hdrw_pacing = 0;
19173 			rack->rack_attempt_hdwr_pace = 0;
19174 			rack_log_hdwr_pacing(rack,
19175 					     rate_wanted, bw_est, __LINE__,
19176 					     0, 6);
19177 		}
19178 		prev_fill = rack->r_via_fill_cw;
19179 		if ((rack->rc_pace_to_cwnd) &&
19180 		    (capped == 0) &&
19181 		    (rack->dgp_on == 1) &&
19182 		    (rack->use_fixed_rate == 0) &&
19183 		    (rack->in_probe_rtt == 0) &&
19184 		    (IN_FASTRECOVERY(rack->rc_tp->t_flags) == 0)) {
19185 			/*
19186 			 * We want to pace at our rate *or* faster to
19187 			 * fill the cwnd to the max if its not full.
19188 			 */
19189 			slot = pace_to_fill_cwnd(rack, slot, (len+segs), segsiz, &capped, &rate_wanted, 0);
19190 			/* Re-check to make sure we are not exceeding our max b/w */
19191 			if ((rack->r_ctl.crte != NULL) &&
19192 			    (tcp_hw_highest_rate(rack->r_ctl.crte) < rate_wanted)) {
19193 				/*
19194 				 * We want more than the hardware can give us,
19195 				 * don't start any hw pacing.
19196 				 */
19197 				can_start_hw_pacing = 0;
19198 				if (rack->r_rack_hw_rate_caps == 0) {
19199 					/*
19200 					 * Ok we need to release it, we
19201 					 * want more than the card can give us and
19202 					 * no rate cap is in place. Set it up so
19203 					 * when we want less we can retry.
19204 					 */
19205 					tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
19206 					rack->r_ctl.crte = NULL;
19207 					rack->rack_attempt_hdwr_pace = 0;
19208 					rack->rack_hdrw_pacing = 0;
19209 					rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
19210 				}
19211 			}
19212 		}
19213 		if ((rack->rc_inp->inp_route.ro_nh != NULL) &&
19214 		    (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
19215 			if ((rack->rack_hdw_pace_ena) &&
19216 			    (can_start_hw_pacing > 0) &&
19217 			    (rack->rack_hdrw_pacing == 0) &&
19218 			    (rack->rack_attempt_hdwr_pace == 0)) {
19219 				/*
19220 				 * Lets attempt to turn on hardware pacing
19221 				 * if we can.
19222 				 */
19223 				rack->rack_attempt_hdwr_pace = 1;
19224 				rack->r_ctl.crte = tcp_set_pacing_rate(rack->rc_tp,
19225 								       rack->rc_inp->inp_route.ro_nh->nh_ifp,
19226 								       rate_wanted,
19227 								       RS_PACING_GEQ,
19228 								       &err, &rack->r_ctl.crte_prev_rate);
19229 				if (rack->r_ctl.crte) {
19230 					rack->rack_hdrw_pacing = 1;
19231 					rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size_w_divisor(tp, rate_wanted, segsiz,
19232 													   pace_one, rack->r_ctl.crte,
19233 													   NULL, rack->r_ctl.pace_len_divisor);
19234 					rack_log_hdwr_pacing(rack,
19235 							     rate_wanted, rack->r_ctl.crte->rate, __LINE__,
19236 							     err, 0);
19237 					rack->r_ctl.last_hw_bw_req = rate_wanted;
19238 				} else {
19239 					counter_u64_add(rack_hw_pace_init_fail, 1);
19240 				}
19241 			} else if (rack->rack_hdrw_pacing &&
19242 				   (rack->r_ctl.last_hw_bw_req != rate_wanted)) {
19243 				/* Do we need to adjust our rate? */
19244 				const struct tcp_hwrate_limit_table *nrte;
19245 
19246 				if (rack->r_up_only &&
19247 				    (rate_wanted < rack->r_ctl.crte->rate)) {
19248 					/**
19249 					 * We have four possible states here
19250 					 * having to do with the previous time
19251 					 * and this time.
19252 					 *   previous  |  this-time
19253 					 * A)     0      |     0   -- fill_cw not in the picture
19254 					 * B)     1      |     0   -- we were doing a fill-cw but now are not
19255 					 * C)     1      |     1   -- all rates from fill_cw
19256 					 * D)     0      |     1   -- we were doing non-fill and now we are filling
19257 					 *
19258 					 * For case A, C and D we don't allow a drop. But for
19259 					 * case B where we now our on our steady rate we do
19260 					 * allow a drop.
19261 					 *
19262 					 */
19263 					if (!((prev_fill == 1) && (rack->r_via_fill_cw == 0)))
19264 						goto done_w_hdwr;
19265 				}
19266 				if ((rate_wanted > rack->r_ctl.crte->rate) ||
19267 				    (rate_wanted <= rack->r_ctl.crte_prev_rate)) {
19268 					if (rack_hw_rate_to_low &&
19269 					    (bw_est < rack_hw_rate_to_low)) {
19270 						/*
19271 						 * The pacing rate is too low for hardware, but
19272 						 * do allow hardware pacing to be restarted.
19273 						 */
19274 						rack_log_hdwr_pacing(rack,
19275 								     bw_est, rack->r_ctl.crte->rate, __LINE__,
19276 								     0, 5);
19277 						tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
19278 						rack->r_ctl.crte = NULL;
19279 						rack->rack_attempt_hdwr_pace = 0;
19280 						rack->rack_hdrw_pacing = 0;
19281 						rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
19282 						goto done_w_hdwr;
19283 					}
19284 					nrte = tcp_chg_pacing_rate(rack->r_ctl.crte,
19285 								   rack->rc_tp,
19286 								   rack->rc_inp->inp_route.ro_nh->nh_ifp,
19287 								   rate_wanted,
19288 								   RS_PACING_GEQ,
19289 								   &err, &rack->r_ctl.crte_prev_rate);
19290 					if (nrte == NULL) {
19291 						/*
19292 						 * Lost the rate, lets drop hardware pacing
19293 						 * period.
19294 						 */
19295 						rack->rack_hdrw_pacing = 0;
19296 						rack->r_ctl.crte = NULL;
19297 						rack_log_hdwr_pacing(rack,
19298 								     rate_wanted, 0, __LINE__,
19299 								     err, 1);
19300 						rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
19301 						counter_u64_add(rack_hw_pace_lost, 1);
19302 					} else if (nrte != rack->r_ctl.crte) {
19303 						rack->r_ctl.crte = nrte;
19304 						rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size_w_divisor(tp, rate_wanted,
19305 														   segsiz, pace_one, rack->r_ctl.crte,
19306 														   NULL, rack->r_ctl.pace_len_divisor);
19307 						rack_log_hdwr_pacing(rack,
19308 								     rate_wanted, rack->r_ctl.crte->rate, __LINE__,
19309 								     err, 2);
19310 						rack->r_ctl.last_hw_bw_req = rate_wanted;
19311 					}
19312 				} else {
19313 					/* We just need to adjust the segment size */
19314 					rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
19315 					rack_log_hdwr_pacing(rack,
19316 							     rate_wanted, rack->r_ctl.crte->rate, __LINE__,
19317 							     0, 4);
19318 					rack->r_ctl.last_hw_bw_req = rate_wanted;
19319 				}
19320 			}
19321 		}
19322 		if (minslot && (minslot > slot)) {
19323 			rack_log_pacing_delay_calc(rack, minslot, slot, rack->r_ctl.crte->rate, bw_est, lentim,
19324 						   98, __LINE__, NULL, 0);
19325 			slot = minslot;
19326 		}
19327 	done_w_hdwr:
19328 		if (rack_limit_time_with_srtt &&
19329 		    (rack->use_fixed_rate == 0) &&
19330 		    (rack->rack_hdrw_pacing == 0)) {
19331 			/*
19332 			 * Sanity check, we do not allow the pacing delay
19333 			 * to be longer than the SRTT of the path. If it is
19334 			 * a slow path, then adding a packet should increase
19335 			 * the RTT and compensate for this i.e. the srtt will
19336 			 * be greater so the allowed pacing time will be greater.
19337 			 *
19338 			 * Note this restriction is not for where a peak rate
19339 			 * is set, we are doing fixed pacing or hardware pacing.
19340 			 */
19341 			if (rack->rc_tp->t_srtt)
19342 				srtt = rack->rc_tp->t_srtt;
19343 			else
19344 				srtt = RACK_INITIAL_RTO * HPTS_USEC_IN_MSEC;	/* its in ms convert */
19345 			if (srtt < (uint64_t)slot) {
19346 				rack_log_pacing_delay_calc(rack, srtt, slot, rate_wanted, bw_est, lentim, 99, __LINE__, NULL, 0);
19347 				slot = srtt;
19348 			}
19349 		}
19350 		/*******************************************************************/
19351 		/* RRS: We insert paced call to stats here for len and rate_wanted */
19352 		/*******************************************************************/
19353 		rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, bw_est, lentim, 2, __LINE__, rsm, 0);
19354 	}
19355 	if (rack->r_ctl.crte && (rack->r_ctl.crte->rs_num_enobufs > 0)) {
19356 		/*
19357 		 * If this rate is seeing enobufs when it
19358 		 * goes to send then either the nic is out
19359 		 * of gas or we are mis-estimating the time
19360 		 * somehow and not letting the queue empty
19361 		 * completely. Lets add to the pacing time.
19362 		 */
19363 		int hw_boost_delay;
19364 
19365 		hw_boost_delay = rack->r_ctl.crte->time_between * rack_enobuf_hw_boost_mult;
19366 		if (hw_boost_delay > rack_enobuf_hw_max)
19367 			hw_boost_delay = rack_enobuf_hw_max;
19368 		else if (hw_boost_delay < rack_enobuf_hw_min)
19369 			hw_boost_delay = rack_enobuf_hw_min;
19370 		slot += hw_boost_delay;
19371 	}
19372 	return (slot);
19373 }
19374 
19375 static void
19376 rack_start_gp_measurement(struct tcpcb *tp, struct tcp_rack *rack,
19377     tcp_seq startseq, uint32_t sb_offset)
19378 {
19379 	struct rack_sendmap *my_rsm = NULL;
19380 
19381 	if (tp->t_state < TCPS_ESTABLISHED) {
19382 		/*
19383 		 * We don't start any measurements if we are
19384 		 * not at least established.
19385 		 */
19386 		return;
19387 	}
19388 	if (tp->t_state >= TCPS_FIN_WAIT_1) {
19389 		/*
19390 		 * We will get no more data into the SB
19391 		 * this means we need to have the data available
19392 		 * before we start a measurement.
19393 		 */
19394 
19395 		if (sbavail(&tptosocket(tp)->so_snd) <
19396 		    max(rc_init_window(rack),
19397 			(MIN_GP_WIN * ctf_fixed_maxseg(tp)))) {
19398 			/* Nope not enough data */
19399 			return;
19400 		}
19401 	}
19402 	tp->t_flags |= TF_GPUTINPROG;
19403 	rack->r_ctl.rc_gp_cumack_ts = 0;
19404 	rack->r_ctl.rc_gp_lowrtt = 0xffffffff;
19405 	rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
19406 	tp->gput_seq = startseq;
19407 	rack->app_limited_needs_set = 0;
19408 	if (rack->in_probe_rtt)
19409 		rack->measure_saw_probe_rtt = 1;
19410 	else if ((rack->measure_saw_probe_rtt) &&
19411 		 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
19412 		rack->measure_saw_probe_rtt = 0;
19413 	if (rack->rc_gp_filled)
19414 		tp->gput_ts = rack->r_ctl.last_cumack_advance;
19415 	else {
19416 		/* Special case initial measurement */
19417 		struct timeval tv;
19418 
19419 		tp->gput_ts = tcp_get_usecs(&tv);
19420 		rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
19421 	}
19422 	/*
19423 	 * We take a guess out into the future,
19424 	 * if we have no measurement and no
19425 	 * initial rate, we measure the first
19426 	 * initial-windows worth of data to
19427 	 * speed up getting some GP measurement and
19428 	 * thus start pacing.
19429 	 */
19430 	if ((rack->rc_gp_filled == 0) && (rack->r_ctl.init_rate == 0)) {
19431 		rack->app_limited_needs_set = 1;
19432 		tp->gput_ack = startseq + max(rc_init_window(rack),
19433 					      (MIN_GP_WIN * ctf_fixed_maxseg(tp)));
19434 		rack_log_pacing_delay_calc(rack,
19435 					   tp->gput_seq,
19436 					   tp->gput_ack,
19437 					   0,
19438 					   tp->gput_ts,
19439 					   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
19440 					   9,
19441 					   __LINE__, NULL, 0);
19442 		rack_tend_gp_marks(tp, rack);
19443 		rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
19444 		return;
19445 	}
19446 	if (sb_offset) {
19447 		/*
19448 		 * We are out somewhere in the sb
19449 		 * can we use the already outstanding data?
19450 		 */
19451 
19452 		if (rack->r_ctl.rc_app_limited_cnt == 0) {
19453 			/*
19454 			 * Yes first one is good and in this case
19455 			 * the tp->gput_ts is correctly set based on
19456 			 * the last ack that arrived (no need to
19457 			 * set things up when an ack comes in).
19458 			 */
19459 			my_rsm = tqhash_min(rack->r_ctl.tqh);
19460 			if ((my_rsm == NULL) ||
19461 			    (my_rsm->r_rtr_cnt != 1)) {
19462 				/* retransmission? */
19463 				goto use_latest;
19464 			}
19465 		} else {
19466 			if (rack->r_ctl.rc_first_appl == NULL) {
19467 				/*
19468 				 * If rc_first_appl is NULL
19469 				 * then the cnt should be 0.
19470 				 * This is probably an error, maybe
19471 				 * a KASSERT would be approprate.
19472 				 */
19473 				goto use_latest;
19474 			}
19475 			/*
19476 			 * If we have a marker pointer to the last one that is
19477 			 * app limited we can use that, but we need to set
19478 			 * things up so that when it gets ack'ed we record
19479 			 * the ack time (if its not already acked).
19480 			 */
19481 			rack->app_limited_needs_set = 1;
19482 			/*
19483 			 * We want to get to the rsm that is either
19484 			 * next with space i.e. over 1 MSS or the one
19485 			 * after that (after the app-limited).
19486 			 */
19487 			my_rsm = tqhash_next(rack->r_ctl.tqh, rack->r_ctl.rc_first_appl);
19488 			if (my_rsm) {
19489 				if ((my_rsm->r_end - my_rsm->r_start) <= ctf_fixed_maxseg(tp))
19490 					/* Have to use the next one */
19491 					my_rsm = tqhash_next(rack->r_ctl.tqh, my_rsm);
19492 				else {
19493 					/* Use after the first MSS of it is acked */
19494 					tp->gput_seq = my_rsm->r_start + ctf_fixed_maxseg(tp);
19495 					goto start_set;
19496 				}
19497 			}
19498 			if ((my_rsm == NULL) ||
19499 			    (my_rsm->r_rtr_cnt != 1)) {
19500 				/*
19501 				 * Either its a retransmit or
19502 				 * the last is the app-limited one.
19503 				 */
19504 				goto use_latest;
19505 			}
19506 		}
19507 		tp->gput_seq = my_rsm->r_start;
19508 start_set:
19509 		if (my_rsm->r_flags & RACK_ACKED) {
19510 			/*
19511 			 * This one has been acked use the arrival ack time
19512 			 */
19513 			struct rack_sendmap *nrsm;
19514 
19515 			tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival;
19516 			rack->app_limited_needs_set = 0;
19517 			/*
19518 			 * Ok in this path we need to use the r_end now
19519 			 * since this guy is the starting ack.
19520 			 */
19521 			tp->gput_seq = my_rsm->r_end;
19522 			/*
19523 			 * We also need to adjust up the sendtime
19524 			 * to the send of the next data after my_rsm.
19525 			 */
19526 			nrsm = tqhash_next(rack->r_ctl.tqh, my_rsm);
19527 			if (nrsm != NULL)
19528 				my_rsm = nrsm;
19529 			else {
19530 				/*
19531 				 * The next as not been sent, thats the
19532 				 * case for using the latest.
19533 				 */
19534 				goto use_latest;
19535 			}
19536 		}
19537 		rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[0];
19538 		tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
19539 		rack->r_ctl.rc_gp_cumack_ts = 0;
19540 		if ((rack->r_ctl.cleared_app_ack == 1) &&
19541 		    (SEQ_GEQ(rack->r_ctl.cleared_app_ack, tp->gput_seq))) {
19542 			/*
19543 			 * We just cleared an application limited period
19544 			 * so the next seq out needs to skip the first
19545 			 * ack.
19546 			 */
19547 			rack->app_limited_needs_set = 1;
19548 			rack->r_ctl.cleared_app_ack = 0;
19549 		}
19550 		rack_log_pacing_delay_calc(rack,
19551 					   tp->gput_seq,
19552 					   tp->gput_ack,
19553 					   (uint64_t)my_rsm,
19554 					   tp->gput_ts,
19555 					   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
19556 					   9,
19557 					   __LINE__, my_rsm, 0);
19558 		/* Now lets make sure all are marked as they should be */
19559 		rack_tend_gp_marks(tp, rack);
19560 		rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
19561 		return;
19562 	}
19563 
19564 use_latest:
19565 	/*
19566 	 * We don't know how long we may have been
19567 	 * idle or if this is the first-send. Lets
19568 	 * setup the flag so we will trim off
19569 	 * the first ack'd data so we get a true
19570 	 * measurement.
19571 	 */
19572 	rack->app_limited_needs_set = 1;
19573 	tp->gput_ack = startseq + rack_get_measure_window(tp, rack);
19574 	rack->r_ctl.rc_gp_cumack_ts = 0;
19575 	/* Find this guy so we can pull the send time */
19576 	my_rsm = tqhash_find(rack->r_ctl.tqh, startseq);
19577 	if (my_rsm) {
19578 		rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[0];
19579 		if (my_rsm->r_flags & RACK_ACKED) {
19580 			/*
19581 			 * Unlikely since its probably what was
19582 			 * just transmitted (but I am paranoid).
19583 			 */
19584 			tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival;
19585 			rack->app_limited_needs_set = 0;
19586 		}
19587 		if (SEQ_LT(my_rsm->r_start, tp->gput_seq)) {
19588 			/* This also is unlikely */
19589 			tp->gput_seq = my_rsm->r_start;
19590 		}
19591 	} else {
19592 		/*
19593 		 * TSNH unless we have some send-map limit,
19594 		 * and even at that it should not be hitting
19595 		 * that limit (we should have stopped sending).
19596 		 */
19597 		struct timeval tv;
19598 
19599 		microuptime(&tv);
19600 		rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
19601 	}
19602 	rack_tend_gp_marks(tp, rack);
19603 	rack_log_pacing_delay_calc(rack,
19604 				   tp->gput_seq,
19605 				   tp->gput_ack,
19606 				   (uint64_t)my_rsm,
19607 				   tp->gput_ts,
19608 				   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
19609 				   9, __LINE__, NULL, 0);
19610 	rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
19611 }
19612 
19613 static inline uint32_t
19614 rack_what_can_we_send(struct tcpcb *tp, struct tcp_rack *rack,  uint32_t cwnd_to_use,
19615     uint32_t avail, int32_t sb_offset)
19616 {
19617 	uint32_t len;
19618 	uint32_t sendwin;
19619 
19620 	if (tp->snd_wnd > cwnd_to_use)
19621 		sendwin = cwnd_to_use;
19622 	else
19623 		sendwin = tp->snd_wnd;
19624 	if (ctf_outstanding(tp) >= tp->snd_wnd) {
19625 		/* We never want to go over our peers rcv-window */
19626 		len = 0;
19627 	} else {
19628 		uint32_t flight;
19629 
19630 		flight = ctf_flight_size(tp, rack->r_ctl.rc_sacked);
19631 		if (flight >= sendwin) {
19632 			/*
19633 			 * We have in flight what we are allowed by cwnd (if
19634 			 * it was rwnd blocking it would have hit above out
19635 			 * >= tp->snd_wnd).
19636 			 */
19637 			return (0);
19638 		}
19639 		len = sendwin - flight;
19640 		if ((len + ctf_outstanding(tp)) > tp->snd_wnd) {
19641 			/* We would send too much (beyond the rwnd) */
19642 			len = tp->snd_wnd - ctf_outstanding(tp);
19643 		}
19644 		if ((len + sb_offset) > avail) {
19645 			/*
19646 			 * We don't have that much in the SB, how much is
19647 			 * there?
19648 			 */
19649 			len = avail - sb_offset;
19650 		}
19651 	}
19652 	return (len);
19653 }
19654 
19655 static void
19656 rack_log_fsb(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t flags,
19657 	     unsigned ipoptlen, int32_t orig_len, int32_t len, int error,
19658 	     int rsm_is_null, int optlen, int line, uint16_t mode)
19659 {
19660 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
19661 		union tcp_log_stackspecific log;
19662 		struct timeval tv;
19663 
19664 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
19665 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
19666 		log.u_bbr.flex1 = error;
19667 		log.u_bbr.flex2 = flags;
19668 		log.u_bbr.flex3 = rsm_is_null;
19669 		log.u_bbr.flex4 = ipoptlen;
19670 		log.u_bbr.flex5 = tp->rcv_numsacks;
19671 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
19672 		log.u_bbr.flex7 = optlen;
19673 		log.u_bbr.flex8 = rack->r_fsb_inited;
19674 		log.u_bbr.applimited = rack->r_fast_output;
19675 		log.u_bbr.bw_inuse = rack_get_bw(rack);
19676 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
19677 		log.u_bbr.cwnd_gain = mode;
19678 		log.u_bbr.pkts_out = orig_len;
19679 		log.u_bbr.lt_epoch = len;
19680 		log.u_bbr.delivered = line;
19681 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
19682 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
19683 		tcp_log_event(tp, NULL, &so->so_rcv, &so->so_snd, TCP_LOG_FSB, 0,
19684 			       len, &log, false, NULL, __func__, __LINE__, &tv);
19685 	}
19686 }
19687 
19688 
19689 static struct mbuf *
19690 rack_fo_base_copym(struct mbuf *the_m, uint32_t the_off, int32_t *plen,
19691 		   struct rack_fast_send_blk *fsb,
19692 		   int32_t seglimit, int32_t segsize, int hw_tls)
19693 {
19694 #ifdef KERN_TLS
19695 	struct ktls_session *tls, *ntls;
19696 #ifdef INVARIANTS
19697 	struct mbuf *start;
19698 #endif
19699 #endif
19700 	struct mbuf *m, *n, **np, *smb;
19701 	struct mbuf *top;
19702 	int32_t off, soff;
19703 	int32_t len = *plen;
19704 	int32_t fragsize;
19705 	int32_t len_cp = 0;
19706 	uint32_t mlen, frags;
19707 
19708 	soff = off = the_off;
19709 	smb = m = the_m;
19710 	np = &top;
19711 	top = NULL;
19712 #ifdef KERN_TLS
19713 	if (hw_tls && (m->m_flags & M_EXTPG))
19714 		tls = m->m_epg_tls;
19715 	else
19716 		tls = NULL;
19717 #ifdef INVARIANTS
19718 	start = m;
19719 #endif
19720 #endif
19721 	while (len > 0) {
19722 		if (m == NULL) {
19723 			*plen = len_cp;
19724 			break;
19725 		}
19726 #ifdef KERN_TLS
19727 		if (hw_tls) {
19728 			if (m->m_flags & M_EXTPG)
19729 				ntls = m->m_epg_tls;
19730 			else
19731 				ntls = NULL;
19732 
19733 			/*
19734 			 * Avoid mixing TLS records with handshake
19735 			 * data or TLS records from different
19736 			 * sessions.
19737 			 */
19738 			if (tls != ntls) {
19739 				MPASS(m != start);
19740 				*plen = len_cp;
19741 				break;
19742 			}
19743 		}
19744 #endif
19745 		mlen = min(len, m->m_len - off);
19746 		if (seglimit) {
19747 			/*
19748 			 * For M_EXTPG mbufs, add 3 segments
19749 			 * + 1 in case we are crossing page boundaries
19750 			 * + 2 in case the TLS hdr/trailer are used
19751 			 * It is cheaper to just add the segments
19752 			 * than it is to take the cache miss to look
19753 			 * at the mbuf ext_pgs state in detail.
19754 			 */
19755 			if (m->m_flags & M_EXTPG) {
19756 				fragsize = min(segsize, PAGE_SIZE);
19757 				frags = 3;
19758 			} else {
19759 				fragsize = segsize;
19760 				frags = 0;
19761 			}
19762 
19763 			/* Break if we really can't fit anymore. */
19764 			if ((frags + 1) >= seglimit) {
19765 				*plen =	len_cp;
19766 				break;
19767 			}
19768 
19769 			/*
19770 			 * Reduce size if you can't copy the whole
19771 			 * mbuf. If we can't copy the whole mbuf, also
19772 			 * adjust len so the loop will end after this
19773 			 * mbuf.
19774 			 */
19775 			if ((frags + howmany(mlen, fragsize)) >= seglimit) {
19776 				mlen = (seglimit - frags - 1) * fragsize;
19777 				len = mlen;
19778 				*plen = len_cp + len;
19779 			}
19780 			frags += howmany(mlen, fragsize);
19781 			if (frags == 0)
19782 				frags++;
19783 			seglimit -= frags;
19784 			KASSERT(seglimit > 0,
19785 			    ("%s: seglimit went too low", __func__));
19786 		}
19787 		n = m_get(M_NOWAIT, m->m_type);
19788 		*np = n;
19789 		if (n == NULL)
19790 			goto nospace;
19791 		n->m_len = mlen;
19792 		soff += mlen;
19793 		len_cp += n->m_len;
19794 		if (m->m_flags & (M_EXT | M_EXTPG)) {
19795 			n->m_data = m->m_data + off;
19796 			mb_dupcl(n, m);
19797 		} else {
19798 			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
19799 			    (u_int)n->m_len);
19800 		}
19801 		len -= n->m_len;
19802 		off = 0;
19803 		m = m->m_next;
19804 		np = &n->m_next;
19805 		if (len || (soff == smb->m_len)) {
19806 			/*
19807 			 * We have more so we move forward  or
19808 			 * we have consumed the entire mbuf and
19809 			 * len has fell to 0.
19810 			 */
19811 			soff = 0;
19812 			smb = m;
19813 		}
19814 
19815 	}
19816 	if (fsb != NULL) {
19817 		fsb->m = smb;
19818 		fsb->off = soff;
19819 		if (smb) {
19820 			/*
19821 			 * Save off the size of the mbuf. We do
19822 			 * this so that we can recognize when it
19823 			 * has been trimmed by sbcut() as acks
19824 			 * come in.
19825 			 */
19826 			fsb->o_m_len = smb->m_len;
19827 			fsb->o_t_len = M_TRAILINGROOM(smb);
19828 		} else {
19829 			/*
19830 			 * This is the case where the next mbuf went to NULL. This
19831 			 * means with this copy we have sent everything in the sb.
19832 			 * In theory we could clear the fast_output flag, but lets
19833 			 * not since its possible that we could get more added
19834 			 * and acks that call the extend function which would let
19835 			 * us send more.
19836 			 */
19837 			fsb->o_m_len = 0;
19838 			fsb->o_t_len = 0;
19839 		}
19840 	}
19841 	return (top);
19842 nospace:
19843 	if (top)
19844 		m_freem(top);
19845 	return (NULL);
19846 
19847 }
19848 
19849 /*
19850  * This is a copy of m_copym(), taking the TSO segment size/limit
19851  * constraints into account, and advancing the sndptr as it goes.
19852  */
19853 static struct mbuf *
19854 rack_fo_m_copym(struct tcp_rack *rack, int32_t *plen,
19855 		int32_t seglimit, int32_t segsize, struct mbuf **s_mb, int *s_soff)
19856 {
19857 	struct mbuf *m, *n;
19858 	int32_t soff;
19859 
19860 	m = rack->r_ctl.fsb.m;
19861 	if (M_TRAILINGROOM(m) != rack->r_ctl.fsb.o_t_len) {
19862 		/*
19863 		 * The trailing space changed, mbufs can grow
19864 		 * at the tail but they can't shrink from
19865 		 * it, KASSERT that. Adjust the orig_m_len to
19866 		 * compensate for this change.
19867 		 */
19868 		KASSERT((rack->r_ctl.fsb.o_t_len > M_TRAILINGROOM(m)),
19869 			("mbuf:%p rack:%p trailing_space:%jd ots:%u oml:%u mlen:%u\n",
19870 			 m,
19871 			 rack,
19872 			 (intmax_t)M_TRAILINGROOM(m),
19873 			 rack->r_ctl.fsb.o_t_len,
19874 			 rack->r_ctl.fsb.o_m_len,
19875 			 m->m_len));
19876 		rack->r_ctl.fsb.o_m_len += (rack->r_ctl.fsb.o_t_len - M_TRAILINGROOM(m));
19877 		rack->r_ctl.fsb.o_t_len = M_TRAILINGROOM(m);
19878 	}
19879 	if (m->m_len < rack->r_ctl.fsb.o_m_len) {
19880 		/*
19881 		 * Mbuf shrank, trimmed off the top by an ack, our
19882 		 * offset changes.
19883 		 */
19884 		KASSERT((rack->r_ctl.fsb.off >= (rack->r_ctl.fsb.o_m_len - m->m_len)),
19885 			("mbuf:%p len:%u rack:%p oml:%u soff:%u\n",
19886 			 m, m->m_len,
19887 			 rack, rack->r_ctl.fsb.o_m_len,
19888 			 rack->r_ctl.fsb.off));
19889 
19890 		if (rack->r_ctl.fsb.off >= (rack->r_ctl.fsb.o_m_len- m->m_len))
19891 			rack->r_ctl.fsb.off -= (rack->r_ctl.fsb.o_m_len - m->m_len);
19892 		else
19893 			rack->r_ctl.fsb.off = 0;
19894 		rack->r_ctl.fsb.o_m_len = m->m_len;
19895 #ifdef INVARIANTS
19896 	} else if (m->m_len > rack->r_ctl.fsb.o_m_len) {
19897 		panic("rack:%p m:%p m_len grew outside of t_space compensation",
19898 		      rack, m);
19899 #endif
19900 	}
19901 	soff = rack->r_ctl.fsb.off;
19902 	KASSERT(soff >= 0, ("%s, negative off %d", __FUNCTION__, soff));
19903 	KASSERT(*plen >= 0, ("%s, negative len %d", __FUNCTION__, *plen));
19904 	KASSERT(soff < m->m_len, ("%s rack:%p len:%u m:%p m->m_len:%u < off?",
19905 				 __FUNCTION__,
19906 				 rack, *plen, m, m->m_len));
19907 	/* Save off the right location before we copy and advance */
19908 	*s_soff = soff;
19909 	*s_mb = rack->r_ctl.fsb.m;
19910 	n = rack_fo_base_copym(m, soff, plen,
19911 			       &rack->r_ctl.fsb,
19912 			       seglimit, segsize, rack->r_ctl.fsb.hw_tls);
19913 	return (n);
19914 }
19915 
19916 /* Log the buffer level */
19917 static void
19918 rack_log_queue_level(struct tcpcb *tp, struct tcp_rack *rack,
19919 		     int len, struct timeval *tv,
19920 		     uint32_t cts)
19921 {
19922 	uint32_t p_rate = 0, p_queue = 0, err = 0;
19923 	union tcp_log_stackspecific log;
19924 
19925 #ifdef RATELIMIT
19926 	err = in_pcbquery_txrlevel(rack->rc_inp, &p_queue);
19927 	err = in_pcbquery_txrtlmt(rack->rc_inp,	&p_rate);
19928 #endif
19929 	memset(&log.u_bbr, 0, sizeof(log.u_bbr));
19930 	log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
19931 	log.u_bbr.flex1 = p_rate;
19932 	log.u_bbr.flex2 = p_queue;
19933 	log.u_bbr.flex4 = (uint32_t)rack->r_ctl.crte->using;
19934 	log.u_bbr.flex5 = (uint32_t)rack->r_ctl.crte->rs_num_enobufs;
19935 	log.u_bbr.flex6 = rack->r_ctl.crte->time_between;
19936 	log.u_bbr.flex7 = 99;
19937 	log.u_bbr.flex8 = 0;
19938 	log.u_bbr.pkts_out = err;
19939 	log.u_bbr.delRate = rack->r_ctl.crte->rate;
19940 	log.u_bbr.timeStamp = cts;
19941 	log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
19942 	tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_HDWR_PACE, 0,
19943 		       len, &log, false, NULL, __func__, __LINE__, tv);
19944 
19945 }
19946 
19947 static uint32_t
19948 rack_check_queue_level(struct tcp_rack *rack, struct tcpcb *tp,
19949 		       struct timeval *tv, uint32_t cts, int len, uint32_t segsiz)
19950 {
19951 	uint64_t lentime = 0;
19952 #ifdef RATELIMIT
19953 	uint32_t p_rate = 0, p_queue = 0, err;
19954 	union tcp_log_stackspecific log;
19955 	uint64_t bw;
19956 
19957 	err = in_pcbquery_txrlevel(rack->rc_inp, &p_queue);
19958 	/* Failed or queue is zero */
19959 	if (err || (p_queue == 0)) {
19960 		lentime = 0;
19961 		goto out;
19962 	}
19963 	err = in_pcbquery_txrtlmt(rack->rc_inp, &p_rate);
19964 	if (err) {
19965 		lentime = 0;
19966 		goto out;
19967 	}
19968 	/*
19969 	 * If we reach here we have some bytes in
19970 	 * the queue. The number returned is a value
19971 	 * between 0 and 0xffff where ffff is full
19972 	 * and 0 is empty. So how best to make this into
19973 	 * something usable?
19974 	 *
19975 	 * The "safer" way is lets take the b/w gotten
19976 	 * from the query (which should be our b/w rate)
19977 	 * and pretend that a full send (our rc_pace_max_segs)
19978 	 * is outstanding. We factor it so its as if a full
19979 	 * number of our MSS segment is terms of full
19980 	 * ethernet segments are outstanding.
19981 	 */
19982 	bw = p_rate / 8;
19983 	if (bw) {
19984 		lentime = (rack->r_ctl.rc_pace_max_segs / segsiz);
19985 		lentime *= ETHERNET_SEGMENT_SIZE;
19986 		lentime *= (uint64_t)HPTS_USEC_IN_SEC;
19987 		lentime /= bw;
19988 	} else {
19989 		/* TSNH -- KASSERT? */
19990 		lentime = 0;
19991 	}
19992 out:
19993 	if (tcp_bblogging_on(tp)) {
19994 		memset(&log, 0, sizeof(log));
19995 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
19996 		log.u_bbr.flex1 = p_rate;
19997 		log.u_bbr.flex2 = p_queue;
19998 		log.u_bbr.flex4 = (uint32_t)rack->r_ctl.crte->using;
19999 		log.u_bbr.flex5 = (uint32_t)rack->r_ctl.crte->rs_num_enobufs;
20000 		log.u_bbr.flex6 = rack->r_ctl.crte->time_between;
20001 		log.u_bbr.flex7 = 99;
20002 		log.u_bbr.flex8 = 0;
20003 		log.u_bbr.pkts_out = err;
20004 		log.u_bbr.delRate = rack->r_ctl.crte->rate;
20005 		log.u_bbr.cur_del_rate = lentime;
20006 		log.u_bbr.timeStamp = cts;
20007 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
20008 		tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_HDWR_PACE, 0,
20009 			       len, &log, false, NULL, __func__, __LINE__,tv);
20010 	}
20011 #endif
20012 	return ((uint32_t)lentime);
20013 }
20014 
20015 static int
20016 rack_fast_rsm_output(struct tcpcb *tp, struct tcp_rack *rack, struct rack_sendmap *rsm,
20017 		     uint64_t ts_val, uint32_t cts, uint32_t ms_cts, struct timeval *tv, int len, uint8_t doing_tlp)
20018 {
20019 	/*
20020 	 * Enter the fast retransmit path. We are given that a sched_pin is
20021 	 * in place (if accounting is compliled in) and the cycle count taken
20022 	 * at the entry is in the ts_val. The concept her is that the rsm
20023 	 * now holds the mbuf offsets and such so we can directly transmit
20024 	 * without a lot of overhead, the len field is already set for
20025 	 * us to prohibit us from sending too much (usually its 1MSS).
20026 	 */
20027 	struct ip *ip = NULL;
20028 	struct udphdr *udp = NULL;
20029 	struct tcphdr *th = NULL;
20030 	struct mbuf *m = NULL;
20031 	struct inpcb *inp;
20032 	uint8_t *cpto;
20033 	struct tcp_log_buffer *lgb;
20034 #ifdef TCP_ACCOUNTING
20035 	uint64_t crtsc;
20036 	int cnt_thru = 1;
20037 #endif
20038 	struct tcpopt to;
20039 	u_char opt[TCP_MAXOLEN];
20040 	uint32_t hdrlen, optlen;
20041 	int32_t slot, segsiz, max_val, tso = 0, error = 0, ulen = 0;
20042 	uint16_t flags;
20043 	uint32_t if_hw_tsomaxsegcount = 0, startseq;
20044 	uint32_t if_hw_tsomaxsegsize;
20045 	int32_t ip_sendflag = IP_NO_SND_TAG_RL;
20046 
20047 #ifdef INET6
20048 	struct ip6_hdr *ip6 = NULL;
20049 
20050 	if (rack->r_is_v6) {
20051 		ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
20052 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
20053 	} else
20054 #endif				/* INET6 */
20055 	{
20056 		ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
20057 		hdrlen = sizeof(struct tcpiphdr);
20058 	}
20059 	if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) {
20060 		goto failed;
20061 	}
20062 	if (doing_tlp) {
20063 		/* Its a TLP add the flag, it may already be there but be sure */
20064 		rsm->r_flags |= RACK_TLP;
20065 	} else {
20066 		/* If it was a TLP it is not not on this retransmit */
20067 		rsm->r_flags &= ~RACK_TLP;
20068 	}
20069 	startseq = rsm->r_start;
20070 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
20071 	inp = rack->rc_inp;
20072 	to.to_flags = 0;
20073 	flags = tcp_outflags[tp->t_state];
20074 	if (flags & (TH_SYN|TH_RST)) {
20075 		goto failed;
20076 	}
20077 	if (rsm->r_flags & RACK_HAS_FIN) {
20078 		/* We can't send a FIN here */
20079 		goto failed;
20080 	}
20081 	if (flags & TH_FIN) {
20082 		/* We never send a FIN */
20083 		flags &= ~TH_FIN;
20084 	}
20085 	if (tp->t_flags & TF_RCVD_TSTMP) {
20086 		to.to_tsval = ms_cts + tp->ts_offset;
20087 		to.to_tsecr = tp->ts_recent;
20088 		to.to_flags = TOF_TS;
20089 	}
20090 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
20091 	/* TCP-MD5 (RFC2385). */
20092 	if (tp->t_flags & TF_SIGNATURE)
20093 		to.to_flags |= TOF_SIGNATURE;
20094 #endif
20095 	optlen = tcp_addoptions(&to, opt);
20096 	hdrlen += optlen;
20097 	udp = rack->r_ctl.fsb.udp;
20098 	if (udp)
20099 		hdrlen += sizeof(struct udphdr);
20100 	if (rack->r_ctl.rc_pace_max_segs)
20101 		max_val = rack->r_ctl.rc_pace_max_segs;
20102 	else if (rack->rc_user_set_max_segs)
20103 		max_val = rack->rc_user_set_max_segs * segsiz;
20104 	else
20105 		max_val = len;
20106 	if ((tp->t_flags & TF_TSO) &&
20107 	    V_tcp_do_tso &&
20108 	    (len > segsiz) &&
20109 	    (tp->t_port == 0))
20110 		tso = 1;
20111 #ifdef INET6
20112 	if (MHLEN < hdrlen + max_linkhdr)
20113 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
20114 	else
20115 #endif
20116 		m = m_gethdr(M_NOWAIT, MT_DATA);
20117 	if (m == NULL)
20118 		goto failed;
20119 	m->m_data += max_linkhdr;
20120 	m->m_len = hdrlen;
20121 	th = rack->r_ctl.fsb.th;
20122 	/* Establish the len to send */
20123 	if (len > max_val)
20124 		len = max_val;
20125 	if ((tso) && (len + optlen > segsiz)) {
20126 		uint32_t if_hw_tsomax;
20127 		int32_t max_len;
20128 
20129 		/* extract TSO information */
20130 		if_hw_tsomax = tp->t_tsomax;
20131 		if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
20132 		if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
20133 		/*
20134 		 * Check if we should limit by maximum payload
20135 		 * length:
20136 		 */
20137 		if (if_hw_tsomax != 0) {
20138 			/* compute maximum TSO length */
20139 			max_len = (if_hw_tsomax - hdrlen -
20140 				   max_linkhdr);
20141 			if (max_len <= 0) {
20142 				goto failed;
20143 			} else if (len > max_len) {
20144 				len = max_len;
20145 			}
20146 		}
20147 		if (len <= segsiz) {
20148 			/*
20149 			 * In case there are too many small fragments don't
20150 			 * use TSO:
20151 			 */
20152 			tso = 0;
20153 		}
20154 	} else {
20155 		tso = 0;
20156 	}
20157 	if ((tso == 0) && (len > segsiz))
20158 		len = segsiz;
20159 	(void)tcp_get_usecs(tv);
20160 	if ((len == 0) ||
20161 	    (len <= MHLEN - hdrlen - max_linkhdr)) {
20162 		goto failed;
20163 	}
20164 	th->th_seq = htonl(rsm->r_start);
20165 	th->th_ack = htonl(tp->rcv_nxt);
20166 	/*
20167 	 * The PUSH bit should only be applied
20168 	 * if the full retransmission is made. If
20169 	 * we are sending less than this is the
20170 	 * left hand edge and should not have
20171 	 * the PUSH bit.
20172 	 */
20173 	if ((rsm->r_flags & RACK_HAD_PUSH) &&
20174 	    (len == (rsm->r_end - rsm->r_start)))
20175 		flags |= TH_PUSH;
20176 	th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale));
20177 	if (th->th_win == 0) {
20178 		tp->t_sndzerowin++;
20179 		tp->t_flags |= TF_RXWIN0SENT;
20180 	} else
20181 		tp->t_flags &= ~TF_RXWIN0SENT;
20182 	if (rsm->r_flags & RACK_TLP) {
20183 		/*
20184 		 * TLP should not count in retran count, but
20185 		 * in its own bin
20186 		 */
20187 		counter_u64_add(rack_tlp_retran, 1);
20188 		counter_u64_add(rack_tlp_retran_bytes, len);
20189 	} else {
20190 		tp->t_sndrexmitpack++;
20191 		KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
20192 		KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
20193 	}
20194 #ifdef STATS
20195 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
20196 				 len);
20197 #endif
20198 	if (rsm->m == NULL)
20199 		goto failed;
20200 	if (rsm->m &&
20201 	    ((rsm->orig_m_len != rsm->m->m_len) ||
20202 	     (M_TRAILINGROOM(rsm->m) != rsm->orig_t_space))) {
20203 		/* Fix up the orig_m_len and possibly the mbuf offset */
20204 		rack_adjust_orig_mlen(rsm);
20205 	}
20206 	m->m_next = rack_fo_base_copym(rsm->m, rsm->soff, &len, NULL, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, rsm->r_hw_tls);
20207 	if (len <= segsiz) {
20208 		/*
20209 		 * Must have ran out of mbufs for the copy
20210 		 * shorten it to no longer need tso. Lets
20211 		 * not put on sendalot since we are low on
20212 		 * mbufs.
20213 		 */
20214 		tso = 0;
20215 	}
20216 	if ((m->m_next == NULL) || (len <= 0)){
20217 		goto failed;
20218 	}
20219 	if (udp) {
20220 		if (rack->r_is_v6)
20221 			ulen = hdrlen + len - sizeof(struct ip6_hdr);
20222 		else
20223 			ulen = hdrlen + len - sizeof(struct ip);
20224 		udp->uh_ulen = htons(ulen);
20225 	}
20226 	m->m_pkthdr.rcvif = (struct ifnet *)0;
20227 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
20228 	    (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) {
20229 		int ect = tcp_ecn_output_established(tp, &flags, len, true);
20230 		if ((tp->t_state == TCPS_SYN_RECEIVED) &&
20231 		    (tp->t_flags2 & TF2_ECN_SND_ECE))
20232 		    tp->t_flags2 &= ~TF2_ECN_SND_ECE;
20233 #ifdef INET6
20234 		if (rack->r_is_v6) {
20235 		    ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
20236 		    ip6->ip6_flow |= htonl(ect << 20);
20237 		}
20238 		else
20239 #endif
20240 		{
20241 		    ip->ip_tos &= ~IPTOS_ECN_MASK;
20242 		    ip->ip_tos |= ect;
20243 		}
20244 	}
20245 	if (rack->r_ctl.crte != NULL) {
20246 		/* See if we can send via the hw queue */
20247 		slot = rack_check_queue_level(rack, tp, tv, cts, len, segsiz);
20248 		/* If there is nothing in queue (no pacing time) we can send via the hw queue */
20249 		if (slot == 0)
20250 			ip_sendflag = 0;
20251 	}
20252 	tcp_set_flags(th, flags);
20253 	m->m_pkthdr.len = hdrlen + len;	/* in6_cksum() need this */
20254 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
20255 	if (to.to_flags & TOF_SIGNATURE) {
20256 		/*
20257 		 * Calculate MD5 signature and put it into the place
20258 		 * determined before.
20259 		 * NOTE: since TCP options buffer doesn't point into
20260 		 * mbuf's data, calculate offset and use it.
20261 		 */
20262 		if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
20263 						       (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
20264 			/*
20265 			 * Do not send segment if the calculation of MD5
20266 			 * digest has failed.
20267 			 */
20268 			goto failed;
20269 		}
20270 	}
20271 #endif
20272 #ifdef INET6
20273 	if (rack->r_is_v6) {
20274 		if (tp->t_port) {
20275 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
20276 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
20277 			udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
20278 			th->th_sum = htons(0);
20279 			UDPSTAT_INC(udps_opackets);
20280 		} else {
20281 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
20282 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
20283 			th->th_sum = in6_cksum_pseudo(ip6,
20284 						      sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
20285 						      0);
20286 		}
20287 	}
20288 #endif
20289 #if defined(INET6) && defined(INET)
20290 	else
20291 #endif
20292 #ifdef INET
20293 	{
20294 		if (tp->t_port) {
20295 			m->m_pkthdr.csum_flags = CSUM_UDP;
20296 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
20297 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
20298 						ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
20299 			th->th_sum = htons(0);
20300 			UDPSTAT_INC(udps_opackets);
20301 		} else {
20302 			m->m_pkthdr.csum_flags = CSUM_TCP;
20303 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
20304 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
20305 					       ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
20306 									IPPROTO_TCP + len + optlen));
20307 		}
20308 		/* IP version must be set here for ipv4/ipv6 checking later */
20309 		KASSERT(ip->ip_v == IPVERSION,
20310 			("%s: IP version incorrect: %d", __func__, ip->ip_v));
20311 	}
20312 #endif
20313 	if (tso) {
20314 		/*
20315 		 * Here we use segsiz since we have no added options besides
20316 		 * any standard timestamp options (no DSACKs or SACKS are sent
20317 		 * via either fast-path).
20318 		 */
20319 		KASSERT(len > segsiz,
20320 			("%s: len <= tso_segsz tp:%p", __func__, tp));
20321 		m->m_pkthdr.csum_flags |= CSUM_TSO;
20322 		m->m_pkthdr.tso_segsz = segsiz;
20323 	}
20324 #ifdef INET6
20325 	if (rack->r_is_v6) {
20326 		ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit;
20327 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
20328 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
20329 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
20330 		else
20331 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
20332 	}
20333 #endif
20334 #if defined(INET) && defined(INET6)
20335 	else
20336 #endif
20337 #ifdef INET
20338 	{
20339 		ip->ip_len = htons(m->m_pkthdr.len);
20340 		ip->ip_ttl = rack->r_ctl.fsb.hoplimit;
20341 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
20342 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
20343 			if (tp->t_port == 0 || len < V_tcp_minmss) {
20344 				ip->ip_off |= htons(IP_DF);
20345 			}
20346 		} else {
20347 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
20348 		}
20349 	}
20350 #endif
20351 	if (doing_tlp == 0) {
20352 		/* Set we retransmitted */
20353 		rack->rc_gp_saw_rec = 1;
20354 	} else {
20355 		/* Its a TLP set ca or ss */
20356 		if (tp->snd_cwnd > tp->snd_ssthresh) {
20357 			/* Set we sent in CA */
20358 			rack->rc_gp_saw_ca = 1;
20359 		} else {
20360 			/* Set we sent in SS */
20361 			rack->rc_gp_saw_ss = 1;
20362 		}
20363 	}
20364 	/* Time to copy in our header */
20365 	cpto = mtod(m, uint8_t *);
20366 	memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
20367 	th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
20368 	if (optlen) {
20369 		bcopy(opt, th + 1, optlen);
20370 		th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
20371 	} else {
20372 		th->th_off = sizeof(struct tcphdr) >> 2;
20373 	}
20374 	if (tcp_bblogging_on(rack->rc_tp)) {
20375 		union tcp_log_stackspecific log;
20376 
20377 		if (rsm->r_flags & RACK_RWND_COLLAPSED) {
20378 			rack_log_collapse(rack, rsm->r_start, rsm->r_end, 0, __LINE__, 5, rsm->r_flags, rsm);
20379 			counter_u64_add(rack_collapsed_win_rxt, 1);
20380 			counter_u64_add(rack_collapsed_win_rxt_bytes, (rsm->r_end - rsm->r_start));
20381 		}
20382 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
20383 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
20384 		if (rack->rack_no_prr)
20385 			log.u_bbr.flex1 = 0;
20386 		else
20387 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
20388 		log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
20389 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
20390 		log.u_bbr.flex4 = max_val;
20391 		/* Save off the early/late values */
20392 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
20393 		log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
20394 		log.u_bbr.bw_inuse = rack_get_bw(rack);
20395 		log.u_bbr.cur_del_rate = rack->r_ctl.gp_bw;
20396 		if (doing_tlp == 0)
20397 			log.u_bbr.flex8 = 1;
20398 		else
20399 			log.u_bbr.flex8 = 2;
20400 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
20401 		log.u_bbr.flex7 = 55;
20402 		log.u_bbr.pkts_out = tp->t_maxseg;
20403 		log.u_bbr.timeStamp = cts;
20404 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
20405 		if (rsm && (rsm->r_rtr_cnt > 0)) {
20406 			/*
20407 			 * When we have a retransmit we want to log the
20408 			 * burst at send and flight at send from before.
20409 			 */
20410 			log.u_bbr.flex5 = rsm->r_fas;
20411 			log.u_bbr.bbr_substate = rsm->r_bas;
20412 		} else {
20413 			/*
20414 			 * This is currently unlikely until we do the
20415 			 * packet pair probes but I will add it for completeness.
20416 			 */
20417 			log.u_bbr.flex5 = log.u_bbr.inflight;
20418 			log.u_bbr.bbr_substate = (uint8_t)((len + segsiz - 1)/segsiz);
20419 		}
20420 		log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use;
20421 		log.u_bbr.delivered = 0;
20422 		log.u_bbr.rttProp = (uint64_t)rsm;
20423 		log.u_bbr.delRate = rsm->r_flags;
20424 		log.u_bbr.delRate <<= 31;
20425 		log.u_bbr.delRate |= rack->r_must_retran;
20426 		log.u_bbr.delRate <<= 1;
20427 		log.u_bbr.delRate |= 1;
20428 		log.u_bbr.pkt_epoch = __LINE__;
20429 		lgb = tcp_log_event(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
20430 				     len, &log, false, NULL, __func__, __LINE__, tv);
20431 	} else
20432 		lgb = NULL;
20433 	if ((rack->r_ctl.crte != NULL) &&
20434 	    tcp_bblogging_on(tp)) {
20435 		rack_log_queue_level(tp, rack, len, tv, cts);
20436 	}
20437 #ifdef INET6
20438 	if (rack->r_is_v6) {
20439 		error = ip6_output(m, inp->in6p_outputopts,
20440 				   &inp->inp_route6,
20441 				   ip_sendflag, NULL, NULL, inp);
20442 	}
20443 	else
20444 #endif
20445 #ifdef INET
20446 	{
20447 		error = ip_output(m, NULL,
20448 				  &inp->inp_route,
20449 				  ip_sendflag, 0, inp);
20450 	}
20451 #endif
20452 	m = NULL;
20453 	if (lgb) {
20454 		lgb->tlb_errno = error;
20455 		lgb = NULL;
20456 	}
20457 	/* Move snd_nxt to snd_max so we don't have false retransmissions */
20458 	tp->snd_nxt = tp->snd_max;
20459 	if (error) {
20460 		goto failed;
20461 	} else if (rack->rc_hw_nobuf && (ip_sendflag != IP_NO_SND_TAG_RL)) {
20462 		rack->rc_hw_nobuf = 0;
20463 		rack->r_ctl.rc_agg_delayed = 0;
20464 		rack->r_early = 0;
20465 		rack->r_late = 0;
20466 		rack->r_ctl.rc_agg_early = 0;
20467 	}
20468 	rack_log_output(tp, &to, len, rsm->r_start, flags, error, rack_to_usec_ts(tv),
20469 			rsm, RACK_SENT_FP, rsm->m, rsm->soff, rsm->r_hw_tls, segsiz);
20470 	if (doing_tlp) {
20471 		rack->rc_tlp_in_progress = 1;
20472 		rack->r_ctl.rc_tlp_cnt_out++;
20473 	}
20474 	if (error == 0) {
20475 		counter_u64_add(rack_total_bytes, len);
20476 		tcp_account_for_send(tp, len, 1, doing_tlp, rsm->r_hw_tls);
20477 		if (doing_tlp) {
20478 			rack->rc_last_sent_tlp_past_cumack = 0;
20479 			rack->rc_last_sent_tlp_seq_valid = 1;
20480 			rack->r_ctl.last_sent_tlp_seq = rsm->r_start;
20481 			rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start;
20482 		}
20483 		if (rack->r_ctl.rc_prr_sndcnt >= len)
20484 			rack->r_ctl.rc_prr_sndcnt -= len;
20485 		else
20486 			rack->r_ctl.rc_prr_sndcnt = 0;
20487 	}
20488 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
20489 	rack->forced_ack = 0;	/* If we send something zap the FA flag */
20490 	if (IN_FASTRECOVERY(tp->t_flags) && rsm)
20491 		rack->r_ctl.retran_during_recovery += len;
20492 	{
20493 		int idx;
20494 
20495 		idx = (len / segsiz) + 3;
20496 		if (idx >= TCP_MSS_ACCT_ATIMER)
20497 			counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
20498 		else
20499 			counter_u64_add(rack_out_size[idx], 1);
20500 	}
20501 	if (tp->t_rtttime == 0) {
20502 		tp->t_rtttime = ticks;
20503 		tp->t_rtseq = startseq;
20504 		KMOD_TCPSTAT_INC(tcps_segstimed);
20505 	}
20506 	counter_u64_add(rack_fto_rsm_send, 1);
20507 	if (error && (error == ENOBUFS)) {
20508 		if (rack->r_ctl.crte != NULL) {
20509 			tcp_trace_point(rack->rc_tp, TCP_TP_HWENOBUF);
20510 			if (tcp_bblogging_on(rack->rc_tp))
20511 				rack_log_queue_level(tp, rack, len, tv, cts);
20512 		} else
20513 			tcp_trace_point(rack->rc_tp, TCP_TP_ENOBUF);
20514 		slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC);
20515 		if (rack->rc_enobuf < 0x7f)
20516 			rack->rc_enobuf++;
20517 		if (slot < (10 * HPTS_USEC_IN_MSEC))
20518 			slot = 10 * HPTS_USEC_IN_MSEC;
20519 		if (rack->r_ctl.crte != NULL) {
20520 			counter_u64_add(rack_saw_enobuf_hw, 1);
20521 			tcp_rl_log_enobuf(rack->r_ctl.crte);
20522 		}
20523 		counter_u64_add(rack_saw_enobuf, 1);
20524 	} else {
20525 		slot = rack_get_pacing_delay(rack, tp, len, NULL, segsiz, __LINE__);
20526 	}
20527 	rack_start_hpts_timer(rack, tp, cts, slot, len, 0);
20528 #ifdef TCP_ACCOUNTING
20529 	crtsc = get_cyclecount();
20530 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
20531 		tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru;
20532 	}
20533 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
20534 		tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
20535 	}
20536 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
20537 		tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((len + segsiz - 1) / segsiz);
20538 	}
20539 	sched_unpin();
20540 #endif
20541 	return (0);
20542 failed:
20543 	if (m)
20544 		m_free(m);
20545 	return (-1);
20546 }
20547 
20548 static void
20549 rack_sndbuf_autoscale(struct tcp_rack *rack)
20550 {
20551 	/*
20552 	 * Automatic sizing of send socket buffer.  Often the send buffer
20553 	 * size is not optimally adjusted to the actual network conditions
20554 	 * at hand (delay bandwidth product).  Setting the buffer size too
20555 	 * small limits throughput on links with high bandwidth and high
20556 	 * delay (eg. trans-continental/oceanic links).  Setting the
20557 	 * buffer size too big consumes too much real kernel memory,
20558 	 * especially with many connections on busy servers.
20559 	 *
20560 	 * The criteria to step up the send buffer one notch are:
20561 	 *  1. receive window of remote host is larger than send buffer
20562 	 *     (with a fudge factor of 5/4th);
20563 	 *  2. send buffer is filled to 7/8th with data (so we actually
20564 	 *     have data to make use of it);
20565 	 *  3. send buffer fill has not hit maximal automatic size;
20566 	 *  4. our send window (slow start and cogestion controlled) is
20567 	 *     larger than sent but unacknowledged data in send buffer.
20568 	 *
20569 	 * Note that the rack version moves things much faster since
20570 	 * we want to avoid hitting cache lines in the rack_fast_output()
20571 	 * path so this is called much less often and thus moves
20572 	 * the SB forward by a percentage.
20573 	 */
20574 	struct socket *so;
20575 	struct tcpcb *tp;
20576 	uint32_t sendwin, scaleup;
20577 
20578 	tp = rack->rc_tp;
20579 	so = rack->rc_inp->inp_socket;
20580 	sendwin = min(rack->r_ctl.cwnd_to_use, tp->snd_wnd);
20581 	if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) {
20582 		if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
20583 		    sbused(&so->so_snd) >=
20584 		    (so->so_snd.sb_hiwat / 8 * 7) &&
20585 		    sbused(&so->so_snd) < V_tcp_autosndbuf_max &&
20586 		    sendwin >= (sbused(&so->so_snd) -
20587 		    (tp->snd_max - tp->snd_una))) {
20588 			if (rack_autosndbuf_inc)
20589 				scaleup = (rack_autosndbuf_inc * so->so_snd.sb_hiwat) / 100;
20590 			else
20591 				scaleup = V_tcp_autosndbuf_inc;
20592 			if (scaleup < V_tcp_autosndbuf_inc)
20593 				scaleup = V_tcp_autosndbuf_inc;
20594 			scaleup += so->so_snd.sb_hiwat;
20595 			if (scaleup > V_tcp_autosndbuf_max)
20596 				scaleup = V_tcp_autosndbuf_max;
20597 			if (!sbreserve_locked(so, SO_SND, scaleup, curthread))
20598 				so->so_snd.sb_flags &= ~SB_AUTOSIZE;
20599 		}
20600 	}
20601 }
20602 
20603 static int
20604 rack_fast_output(struct tcpcb *tp, struct tcp_rack *rack, uint64_t ts_val,
20605 		 uint32_t cts, uint32_t ms_cts, struct timeval *tv, long tot_len, int *send_err)
20606 {
20607 	/*
20608 	 * Enter to do fast output. We are given that the sched_pin is
20609 	 * in place (if accounting is compiled in) and the cycle count taken
20610 	 * at entry is in place in ts_val. The idea here is that
20611 	 * we know how many more bytes needs to be sent (presumably either
20612 	 * during pacing or to fill the cwnd and that was greater than
20613 	 * the max-burst). We have how much to send and all the info we
20614 	 * need to just send.
20615 	 */
20616 #ifdef INET
20617 	struct ip *ip = NULL;
20618 #endif
20619 	struct udphdr *udp = NULL;
20620 	struct tcphdr *th = NULL;
20621 	struct mbuf *m, *s_mb;
20622 	struct inpcb *inp;
20623 	uint8_t *cpto;
20624 	struct tcp_log_buffer *lgb;
20625 #ifdef TCP_ACCOUNTING
20626 	uint64_t crtsc;
20627 #endif
20628 	struct tcpopt to;
20629 	u_char opt[TCP_MAXOLEN];
20630 	uint32_t hdrlen, optlen;
20631 #ifdef TCP_ACCOUNTING
20632 	int cnt_thru = 1;
20633 #endif
20634 	int32_t slot, segsiz, len, max_val, tso = 0, sb_offset, error, ulen = 0;
20635 	uint16_t flags;
20636 	uint32_t s_soff;
20637 	uint32_t if_hw_tsomaxsegcount = 0, startseq;
20638 	uint32_t if_hw_tsomaxsegsize;
20639 	uint32_t add_flag = RACK_SENT_FP;
20640 #ifdef INET6
20641 	struct ip6_hdr *ip6 = NULL;
20642 
20643 	if (rack->r_is_v6) {
20644 		ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
20645 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
20646 	} else
20647 #endif				/* INET6 */
20648 	{
20649 #ifdef INET
20650 		ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
20651 		hdrlen = sizeof(struct tcpiphdr);
20652 #endif
20653 	}
20654 	if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) {
20655 		m = NULL;
20656 		goto failed;
20657 	}
20658 	rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
20659 	startseq = tp->snd_max;
20660 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
20661 	inp = rack->rc_inp;
20662 	len = rack->r_ctl.fsb.left_to_send;
20663 	to.to_flags = 0;
20664 	flags = rack->r_ctl.fsb.tcp_flags;
20665 	if (tp->t_flags & TF_RCVD_TSTMP) {
20666 		to.to_tsval = ms_cts + tp->ts_offset;
20667 		to.to_tsecr = tp->ts_recent;
20668 		to.to_flags = TOF_TS;
20669 	}
20670 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
20671 	/* TCP-MD5 (RFC2385). */
20672 	if (tp->t_flags & TF_SIGNATURE)
20673 		to.to_flags |= TOF_SIGNATURE;
20674 #endif
20675 	optlen = tcp_addoptions(&to, opt);
20676 	hdrlen += optlen;
20677 	udp = rack->r_ctl.fsb.udp;
20678 	if (udp)
20679 		hdrlen += sizeof(struct udphdr);
20680 	if (rack->r_ctl.rc_pace_max_segs)
20681 		max_val = rack->r_ctl.rc_pace_max_segs;
20682 	else if (rack->rc_user_set_max_segs)
20683 		max_val = rack->rc_user_set_max_segs * segsiz;
20684 	else
20685 		max_val = len;
20686 	if ((tp->t_flags & TF_TSO) &&
20687 	    V_tcp_do_tso &&
20688 	    (len > segsiz) &&
20689 	    (tp->t_port == 0))
20690 		tso = 1;
20691 again:
20692 #ifdef INET6
20693 	if (MHLEN < hdrlen + max_linkhdr)
20694 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
20695 	else
20696 #endif
20697 		m = m_gethdr(M_NOWAIT, MT_DATA);
20698 	if (m == NULL)
20699 		goto failed;
20700 	m->m_data += max_linkhdr;
20701 	m->m_len = hdrlen;
20702 	th = rack->r_ctl.fsb.th;
20703 	/* Establish the len to send */
20704 	if (len > max_val)
20705 		len = max_val;
20706 	if ((tso) && (len + optlen > segsiz)) {
20707 		uint32_t if_hw_tsomax;
20708 		int32_t max_len;
20709 
20710 		/* extract TSO information */
20711 		if_hw_tsomax = tp->t_tsomax;
20712 		if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
20713 		if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
20714 		/*
20715 		 * Check if we should limit by maximum payload
20716 		 * length:
20717 		 */
20718 		if (if_hw_tsomax != 0) {
20719 			/* compute maximum TSO length */
20720 			max_len = (if_hw_tsomax - hdrlen -
20721 				   max_linkhdr);
20722 			if (max_len <= 0) {
20723 				goto failed;
20724 			} else if (len > max_len) {
20725 				len = max_len;
20726 			}
20727 		}
20728 		if (len <= segsiz) {
20729 			/*
20730 			 * In case there are too many small fragments don't
20731 			 * use TSO:
20732 			 */
20733 			tso = 0;
20734 		}
20735 	} else {
20736 		tso = 0;
20737 	}
20738 	if ((tso == 0) && (len > segsiz))
20739 		len = segsiz;
20740 	(void)tcp_get_usecs(tv);
20741 	if ((len == 0) ||
20742 	    (len <= MHLEN - hdrlen - max_linkhdr)) {
20743 		goto failed;
20744 	}
20745 	sb_offset = tp->snd_max - tp->snd_una;
20746 	th->th_seq = htonl(tp->snd_max);
20747 	th->th_ack = htonl(tp->rcv_nxt);
20748 	th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale));
20749 	if (th->th_win == 0) {
20750 		tp->t_sndzerowin++;
20751 		tp->t_flags |= TF_RXWIN0SENT;
20752 	} else
20753 		tp->t_flags &= ~TF_RXWIN0SENT;
20754 	tp->snd_up = tp->snd_una;	/* drag it along, its deprecated */
20755 	KMOD_TCPSTAT_INC(tcps_sndpack);
20756 	KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
20757 #ifdef STATS
20758 	stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
20759 				 len);
20760 #endif
20761 	if (rack->r_ctl.fsb.m == NULL)
20762 		goto failed;
20763 
20764 	/* s_mb and s_soff are saved for rack_log_output */
20765 	m->m_next = rack_fo_m_copym(rack, &len, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize,
20766 				    &s_mb, &s_soff);
20767 	if (len <= segsiz) {
20768 		/*
20769 		 * Must have ran out of mbufs for the copy
20770 		 * shorten it to no longer need tso. Lets
20771 		 * not put on sendalot since we are low on
20772 		 * mbufs.
20773 		 */
20774 		tso = 0;
20775 	}
20776 	if (rack->r_ctl.fsb.rfo_apply_push &&
20777 	    (len == rack->r_ctl.fsb.left_to_send)) {
20778 		tcp_set_flags(th, flags | TH_PUSH);
20779 		add_flag |= RACK_HAD_PUSH;
20780 	}
20781 	if ((m->m_next == NULL) || (len <= 0)){
20782 		goto failed;
20783 	}
20784 	if (udp) {
20785 		if (rack->r_is_v6)
20786 			ulen = hdrlen + len - sizeof(struct ip6_hdr);
20787 		else
20788 			ulen = hdrlen + len - sizeof(struct ip);
20789 		udp->uh_ulen = htons(ulen);
20790 	}
20791 	m->m_pkthdr.rcvif = (struct ifnet *)0;
20792 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
20793 	    (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) {
20794 		int ect = tcp_ecn_output_established(tp, &flags, len, false);
20795 		if ((tp->t_state == TCPS_SYN_RECEIVED) &&
20796 		    (tp->t_flags2 & TF2_ECN_SND_ECE))
20797 			tp->t_flags2 &= ~TF2_ECN_SND_ECE;
20798 #ifdef INET6
20799 		if (rack->r_is_v6) {
20800 			ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
20801 			ip6->ip6_flow |= htonl(ect << 20);
20802 		}
20803 		else
20804 #endif
20805 		{
20806 #ifdef INET
20807 			ip->ip_tos &= ~IPTOS_ECN_MASK;
20808 			ip->ip_tos |= ect;
20809 #endif
20810 		}
20811 	}
20812 	tcp_set_flags(th, flags);
20813 	m->m_pkthdr.len = hdrlen + len;	/* in6_cksum() need this */
20814 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
20815 	if (to.to_flags & TOF_SIGNATURE) {
20816 		/*
20817 		 * Calculate MD5 signature and put it into the place
20818 		 * determined before.
20819 		 * NOTE: since TCP options buffer doesn't point into
20820 		 * mbuf's data, calculate offset and use it.
20821 		 */
20822 		if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
20823 						       (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
20824 			/*
20825 			 * Do not send segment if the calculation of MD5
20826 			 * digest has failed.
20827 			 */
20828 			goto failed;
20829 		}
20830 	}
20831 #endif
20832 #ifdef INET6
20833 	if (rack->r_is_v6) {
20834 		if (tp->t_port) {
20835 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
20836 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
20837 			udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
20838 			th->th_sum = htons(0);
20839 			UDPSTAT_INC(udps_opackets);
20840 		} else {
20841 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
20842 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
20843 			th->th_sum = in6_cksum_pseudo(ip6,
20844 						      sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
20845 						      0);
20846 		}
20847 	}
20848 #endif
20849 #if defined(INET6) && defined(INET)
20850 	else
20851 #endif
20852 #ifdef INET
20853 	{
20854 		if (tp->t_port) {
20855 			m->m_pkthdr.csum_flags = CSUM_UDP;
20856 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
20857 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
20858 						ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
20859 			th->th_sum = htons(0);
20860 			UDPSTAT_INC(udps_opackets);
20861 		} else {
20862 			m->m_pkthdr.csum_flags = CSUM_TCP;
20863 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
20864 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
20865 					       ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
20866 									IPPROTO_TCP + len + optlen));
20867 		}
20868 		/* IP version must be set here for ipv4/ipv6 checking later */
20869 		KASSERT(ip->ip_v == IPVERSION,
20870 			("%s: IP version incorrect: %d", __func__, ip->ip_v));
20871 	}
20872 #endif
20873 	if (tso) {
20874 		/*
20875 		 * Here we use segsiz since we have no added options besides
20876 		 * any standard timestamp options (no DSACKs or SACKS are sent
20877 		 * via either fast-path).
20878 		 */
20879 		KASSERT(len > segsiz,
20880 			("%s: len <= tso_segsz tp:%p", __func__, tp));
20881 		m->m_pkthdr.csum_flags |= CSUM_TSO;
20882 		m->m_pkthdr.tso_segsz = segsiz;
20883 	}
20884 #ifdef INET6
20885 	if (rack->r_is_v6) {
20886 		ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit;
20887 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
20888 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
20889 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
20890 		else
20891 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
20892 	}
20893 #endif
20894 #if defined(INET) && defined(INET6)
20895 	else
20896 #endif
20897 #ifdef INET
20898 	{
20899 		ip->ip_len = htons(m->m_pkthdr.len);
20900 		ip->ip_ttl = rack->r_ctl.fsb.hoplimit;
20901 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
20902 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
20903 			if (tp->t_port == 0 || len < V_tcp_minmss) {
20904 				ip->ip_off |= htons(IP_DF);
20905 			}
20906 		} else {
20907 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
20908 		}
20909 	}
20910 #endif
20911 	if (tp->snd_cwnd > tp->snd_ssthresh) {
20912 		/* Set we sent in CA */
20913 		rack->rc_gp_saw_ca = 1;
20914 	} else {
20915 		/* Set we sent in SS */
20916 		rack->rc_gp_saw_ss = 1;
20917 	}
20918 	/* Time to copy in our header */
20919 	cpto = mtod(m, uint8_t *);
20920 	memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
20921 	th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
20922 	if (optlen) {
20923 		bcopy(opt, th + 1, optlen);
20924 		th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
20925 	} else {
20926 		th->th_off = sizeof(struct tcphdr) >> 2;
20927 	}
20928 	if ((rack->r_ctl.crte != NULL) &&
20929 	    tcp_bblogging_on(tp)) {
20930 		rack_log_queue_level(tp, rack, len, tv, cts);
20931 	}
20932 	if (tcp_bblogging_on(rack->rc_tp)) {
20933 		union tcp_log_stackspecific log;
20934 
20935 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
20936 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
20937 		if (rack->rack_no_prr)
20938 			log.u_bbr.flex1 = 0;
20939 		else
20940 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
20941 		log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
20942 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
20943 		log.u_bbr.flex4 = max_val;
20944 		/* Save off the early/late values */
20945 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
20946 		log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
20947 		log.u_bbr.bw_inuse = rack_get_bw(rack);
20948 		log.u_bbr.cur_del_rate = rack->r_ctl.gp_bw;
20949 		log.u_bbr.flex8 = 0;
20950 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
20951 		log.u_bbr.flex7 = 44;
20952 		log.u_bbr.pkts_out = tp->t_maxseg;
20953 		log.u_bbr.timeStamp = cts;
20954 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
20955 		log.u_bbr.flex5 = log.u_bbr.inflight;
20956 		log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use;
20957 		log.u_bbr.delivered = 0;
20958 		log.u_bbr.rttProp = 0;
20959 		log.u_bbr.delRate = rack->r_must_retran;
20960 		log.u_bbr.delRate <<= 1;
20961 		log.u_bbr.pkt_epoch = __LINE__;
20962 		/* For fast output no retrans so just inflight and how many mss we send */
20963 		log.u_bbr.flex5 = log.u_bbr.inflight;
20964 		log.u_bbr.bbr_substate = (uint8_t)((len + segsiz - 1)/segsiz);
20965 		lgb = tcp_log_event(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
20966 				     len, &log, false, NULL, __func__, __LINE__, tv);
20967 	} else
20968 		lgb = NULL;
20969 #ifdef INET6
20970 	if (rack->r_is_v6) {
20971 		error = ip6_output(m, inp->in6p_outputopts,
20972 				   &inp->inp_route6,
20973 				   0, NULL, NULL, inp);
20974 	}
20975 #endif
20976 #if defined(INET) && defined(INET6)
20977 	else
20978 #endif
20979 #ifdef INET
20980 	{
20981 		error = ip_output(m, NULL,
20982 				  &inp->inp_route,
20983 				  0, 0, inp);
20984 	}
20985 #endif
20986 	if (lgb) {
20987 		lgb->tlb_errno = error;
20988 		lgb = NULL;
20989 	}
20990 	if (error) {
20991 		*send_err = error;
20992 		m = NULL;
20993 		goto failed;
20994 	} else if (rack->rc_hw_nobuf) {
20995 		rack->rc_hw_nobuf = 0;
20996 		rack->r_ctl.rc_agg_delayed = 0;
20997 		rack->r_early = 0;
20998 		rack->r_late = 0;
20999 		rack->r_ctl.rc_agg_early = 0;
21000 	}
21001 	if ((error == 0) && (rack->lt_bw_up == 0)) {
21002 		/* Unlikely */
21003 		rack->r_ctl.lt_timemark = tcp_tv_to_lusectick(tv);
21004 		rack->r_ctl.lt_seq = tp->snd_una;
21005 		rack->lt_bw_up = 1;
21006 	} else if ((error == 0) &&
21007 		   (((tp->snd_max + len) - rack->r_ctl.lt_seq) > 0x7fffffff)) {
21008 		/*
21009 		 * Need to record what we have since we are
21010 		 * approaching seq wrap.
21011 		 */
21012 		struct timeval tv;
21013 		uint64_t tmark;
21014 
21015 		rack->r_ctl.lt_bw_bytes += (tp->snd_una - rack->r_ctl.lt_seq);
21016 		rack->r_ctl.lt_seq = tp->snd_una;
21017 		tmark = tcp_get_u64_usecs(&tv);
21018 		if (tmark > rack->r_ctl.lt_timemark) {
21019 			rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark);
21020 			rack->r_ctl.lt_timemark = tmark;
21021 		}
21022 	}
21023 	rack_log_output(tp, &to, len, tp->snd_max, flags, error, rack_to_usec_ts(tv),
21024 			NULL, add_flag, s_mb, s_soff, rack->r_ctl.fsb.hw_tls, segsiz);
21025 	m = NULL;
21026 	if (tp->snd_una == tp->snd_max) {
21027 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
21028 		rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
21029 		tp->t_acktime = ticks;
21030 	}
21031 	counter_u64_add(rack_total_bytes, len);
21032 	tcp_account_for_send(tp, len, 0, 0, rack->r_ctl.fsb.hw_tls);
21033 
21034 	rack->forced_ack = 0;	/* If we send something zap the FA flag */
21035 	tot_len += len;
21036 	if ((tp->t_flags & TF_GPUTINPROG) == 0)
21037 		rack_start_gp_measurement(tp, rack, tp->snd_max, sb_offset);
21038 	tp->snd_max += len;
21039 	tp->snd_nxt = tp->snd_max;
21040 	if (rack->rc_new_rnd_needed) {
21041 		rack_new_round_starts(tp, rack, tp->snd_max);
21042 	}
21043 	{
21044 		int idx;
21045 
21046 		idx = (len / segsiz) + 3;
21047 		if (idx >= TCP_MSS_ACCT_ATIMER)
21048 			counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
21049 		else
21050 			counter_u64_add(rack_out_size[idx], 1);
21051 	}
21052 	if (len <= rack->r_ctl.fsb.left_to_send)
21053 		rack->r_ctl.fsb.left_to_send -= len;
21054 	else
21055 		rack->r_ctl.fsb.left_to_send = 0;
21056 	if (rack->r_ctl.fsb.left_to_send < segsiz) {
21057 		rack->r_fast_output = 0;
21058 		rack->r_ctl.fsb.left_to_send = 0;
21059 		/* At the end of fast_output scale up the sb */
21060 		SOCKBUF_LOCK(&rack->rc_inp->inp_socket->so_snd);
21061 		rack_sndbuf_autoscale(rack);
21062 		SOCKBUF_UNLOCK(&rack->rc_inp->inp_socket->so_snd);
21063 	}
21064 	if (tp->t_rtttime == 0) {
21065 		tp->t_rtttime = ticks;
21066 		tp->t_rtseq = startseq;
21067 		KMOD_TCPSTAT_INC(tcps_segstimed);
21068 	}
21069 	if ((rack->r_ctl.fsb.left_to_send >= segsiz) &&
21070 	    (max_val > len) &&
21071 	    (tso == 0)) {
21072 		max_val -= len;
21073 		len = segsiz;
21074 		th = rack->r_ctl.fsb.th;
21075 #ifdef TCP_ACCOUNTING
21076 		cnt_thru++;
21077 #endif
21078 		goto again;
21079 	}
21080 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
21081 	counter_u64_add(rack_fto_send, 1);
21082 	slot = rack_get_pacing_delay(rack, tp, tot_len, NULL, segsiz, __LINE__);
21083 	rack_start_hpts_timer(rack, tp, cts, slot, tot_len, 0);
21084 #ifdef TCP_ACCOUNTING
21085 	crtsc = get_cyclecount();
21086 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21087 		tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru;
21088 	}
21089 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21090 		tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
21091 	}
21092 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21093 		tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len + segsiz - 1) / segsiz);
21094 	}
21095 	sched_unpin();
21096 #endif
21097 	return (0);
21098 failed:
21099 	if (m)
21100 		m_free(m);
21101 	rack->r_fast_output = 0;
21102 	return (-1);
21103 }
21104 
21105 static inline void
21106 rack_setup_fast_output(struct tcpcb *tp, struct tcp_rack *rack,
21107 		       struct sockbuf *sb,
21108 		       int len, int orig_len, int segsiz, uint32_t pace_max_seg,
21109 		       bool hw_tls,
21110 		       uint16_t flags)
21111 {
21112 	rack->r_fast_output = 1;
21113 	rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
21114 	rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
21115 	rack->r_ctl.fsb.o_t_len = M_TRAILINGROOM(rack->r_ctl.fsb.m);
21116 	rack->r_ctl.fsb.tcp_flags = flags;
21117 	rack->r_ctl.fsb.left_to_send = orig_len - len;
21118 	if (rack->r_ctl.fsb.left_to_send < pace_max_seg) {
21119 		/* Less than a full sized pace, lets not  */
21120 		rack->r_fast_output = 0;
21121 		return;
21122 	} else {
21123 		/* Round down to the nearest pace_max_seg */
21124 		rack->r_ctl.fsb.left_to_send = rounddown(rack->r_ctl.fsb.left_to_send, pace_max_seg);
21125 	}
21126 	if (hw_tls)
21127 		rack->r_ctl.fsb.hw_tls = 1;
21128 	else
21129 		rack->r_ctl.fsb.hw_tls = 0;
21130 	KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(sb) - (tp->snd_max - tp->snd_una))),
21131 		("rack:%p left_to_send:%u sbavail:%u out:%u",
21132 		 rack, rack->r_ctl.fsb.left_to_send, sbavail(sb),
21133 		 (tp->snd_max - tp->snd_una)));
21134 	if (rack->r_ctl.fsb.left_to_send < segsiz)
21135 		rack->r_fast_output = 0;
21136 	else {
21137 		if (rack->r_ctl.fsb.left_to_send == (sbavail(sb) - (tp->snd_max - tp->snd_una)))
21138 			rack->r_ctl.fsb.rfo_apply_push = 1;
21139 		else
21140 			rack->r_ctl.fsb.rfo_apply_push = 0;
21141 	}
21142 }
21143 
21144 static uint32_t
21145 rack_get_hpts_pacing_min_for_bw(struct tcp_rack *rack, int32_t segsiz)
21146 {
21147 	uint64_t min_time;
21148 	uint32_t maxlen;
21149 
21150 	min_time = (uint64_t)get_hpts_min_sleep_time();
21151 	maxlen = (uint32_t)((rack->r_ctl.gp_bw * min_time) / (uint64_t)HPTS_USEC_IN_SEC);
21152 	maxlen = roundup(maxlen, segsiz);
21153 	return (maxlen);
21154 }
21155 
21156 static struct rack_sendmap *
21157 rack_check_collapsed(struct tcp_rack *rack, uint32_t cts)
21158 {
21159 	struct rack_sendmap *rsm = NULL;
21160 	int thresh;
21161 
21162 restart:
21163 	rsm = tqhash_find(rack->r_ctl.tqh, rack->r_ctl.last_collapse_point);
21164 	if ((rsm == NULL) || ((rsm->r_flags & RACK_RWND_COLLAPSED) == 0)) {
21165 		/* Nothing, strange turn off validity  */
21166 		rack->r_collapse_point_valid = 0;
21167 		return (NULL);
21168 	}
21169 	/* Can we send it yet? */
21170 	if (rsm->r_end > (rack->rc_tp->snd_una + rack->rc_tp->snd_wnd)) {
21171 		/*
21172 		 * Receiver window has not grown enough for
21173 		 * the segment to be put on the wire.
21174 		 */
21175 		return (NULL);
21176 	}
21177 	if (rsm->r_flags & RACK_ACKED) {
21178 		/*
21179 		 * It has been sacked, lets move to the
21180 		 * next one if possible.
21181 		 */
21182 		rack->r_ctl.last_collapse_point = rsm->r_end;
21183 		/* Are we done? */
21184 		if (SEQ_GEQ(rack->r_ctl.last_collapse_point,
21185 			    rack->r_ctl.high_collapse_point)) {
21186 			rack->r_collapse_point_valid = 0;
21187 			return (NULL);
21188 		}
21189 		goto restart;
21190 	}
21191 	/* Now has it been long enough ? */
21192 	thresh = rack_calc_thresh_rack(rack, rack_grab_rtt(rack->rc_tp, rack), cts, __LINE__, 1);
21193 	if ((cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])) > thresh) {
21194 		rack_log_collapse(rack, rsm->r_start,
21195 				  (cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])),
21196 				  thresh, __LINE__, 6, rsm->r_flags, rsm);
21197 		return (rsm);
21198 	}
21199 	/* Not enough time */
21200 	rack_log_collapse(rack, rsm->r_start,
21201 			  (cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])),
21202 			  thresh, __LINE__, 7, rsm->r_flags, rsm);
21203 	return (NULL);
21204 }
21205 
21206 static void
21207 rack_credit_back_policer_idle_time(struct tcp_rack *rack, uint64_t idle_t, int line)
21208 {
21209 	/*
21210 	 * We were idle some time (idle_t) and so our policer bucket
21211 	 * needs to grow. It can go no higher than policer_bucket_size.
21212 	 */
21213 	uint64_t len;
21214 
21215 	len = idle_t * rack->r_ctl.policer_bw;
21216 	len /= HPTS_USEC_IN_SEC;
21217 	rack->r_ctl.current_policer_bucket += (uint32_t)len;
21218 	if (rack->r_ctl.policer_bucket_size < rack->r_ctl.current_policer_bucket) {
21219 		rack->r_ctl.current_policer_bucket = rack->r_ctl.policer_bucket_size;
21220 	}
21221 	if (rack_verbose_logging > 0)
21222 		policer_detection_log(rack, (uint32_t)len, line, (uint32_t)idle_t, 0, 7);
21223 }
21224 
21225 static inline void
21226 rack_validate_sizes(struct tcp_rack *rack, int32_t *len, int32_t segsiz, uint32_t pace_max_seg)
21227 {
21228 	if ((rack->full_size_rxt == 0) &&
21229 	    (rack->shape_rxt_to_pacing_min == 0) &&
21230 	    (*len >= segsiz)) {
21231 		*len = segsiz;
21232 	} else if (rack->shape_rxt_to_pacing_min &&
21233 		 rack->gp_ready) {
21234 		/* We use pacing min as shaping len req */
21235 		uint32_t maxlen;
21236 
21237 		maxlen = rack_get_hpts_pacing_min_for_bw(rack, segsiz);
21238 		if (*len > maxlen)
21239 			*len = maxlen;
21240 	} else {
21241 		/*
21242 		 * The else is full_size_rxt is on so send it all
21243 		 * note we do need to check this for exceeding
21244 		 * our max segment size due to the fact that
21245 		 * we do sometimes merge chunks together i.e.
21246 		 * we cannot just assume that we will never have
21247 		 * a chunk greater than pace_max_seg
21248 		 */
21249 		if (*len > pace_max_seg)
21250 			*len = pace_max_seg;
21251 	}
21252 }
21253 
21254 static int
21255 rack_output(struct tcpcb *tp)
21256 {
21257 	struct socket *so;
21258 	uint32_t recwin;
21259 	uint32_t sb_offset, s_moff = 0;
21260 	int32_t len, error = 0;
21261 	uint16_t flags;
21262 	struct mbuf *m, *s_mb = NULL;
21263 	struct mbuf *mb;
21264 	uint32_t if_hw_tsomaxsegcount = 0;
21265 	uint32_t if_hw_tsomaxsegsize;
21266 	int32_t segsiz, minseg;
21267 	long tot_len_this_send = 0;
21268 #ifdef INET
21269 	struct ip *ip = NULL;
21270 #endif
21271 	struct udphdr *udp = NULL;
21272 	struct tcp_rack *rack;
21273 	struct tcphdr *th;
21274 	uint8_t pass = 0;
21275 	uint8_t mark = 0;
21276 	uint8_t check_done = 0;
21277 	uint8_t wanted_cookie = 0;
21278 	u_char opt[TCP_MAXOLEN];
21279 	unsigned ipoptlen, optlen, hdrlen, ulen=0;
21280 	uint32_t rack_seq;
21281 
21282 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
21283 	unsigned ipsec_optlen = 0;
21284 
21285 #endif
21286 	int32_t idle, sendalot, tot_idle;
21287 	int32_t sub_from_prr = 0;
21288 	volatile int32_t sack_rxmit;
21289 	struct rack_sendmap *rsm = NULL;
21290 	int32_t tso, mtu;
21291 	struct tcpopt to;
21292 	int32_t slot = 0;
21293 	int32_t sup_rack = 0;
21294 	uint32_t cts, ms_cts, delayed, early;
21295 	uint32_t add_flag = RACK_SENT_SP;
21296 	/* The doing_tlp flag will be set by the actual rack_timeout_tlp() */
21297 	uint8_t doing_tlp = 0;
21298 	uint32_t cwnd_to_use, pace_max_seg;
21299 	int32_t do_a_prefetch = 0;
21300 	int32_t prefetch_rsm = 0;
21301 	int32_t orig_len = 0;
21302 	struct timeval tv;
21303 	int32_t prefetch_so_done = 0;
21304 	struct tcp_log_buffer *lgb;
21305 	struct inpcb *inp = tptoinpcb(tp);
21306 	struct sockbuf *sb;
21307 	uint64_t ts_val = 0;
21308 #ifdef TCP_ACCOUNTING
21309 	uint64_t crtsc;
21310 #endif
21311 #ifdef INET6
21312 	struct ip6_hdr *ip6 = NULL;
21313 	int32_t isipv6;
21314 #endif
21315 	bool hpts_calling, hw_tls = false;
21316 
21317 	NET_EPOCH_ASSERT();
21318 	INP_WLOCK_ASSERT(inp);
21319 
21320 	/* setup and take the cache hits here */
21321 	rack = (struct tcp_rack *)tp->t_fb_ptr;
21322 #ifdef TCP_ACCOUNTING
21323 	sched_pin();
21324 	ts_val = get_cyclecount();
21325 #endif
21326 	hpts_calling = !!(tp->t_flags2 & TF2_HPTS_CALLS);
21327 	tp->t_flags2 &= ~TF2_HPTS_CALLS;
21328 #ifdef TCP_OFFLOAD
21329 	if (tp->t_flags & TF_TOE) {
21330 #ifdef TCP_ACCOUNTING
21331 		sched_unpin();
21332 #endif
21333 		return (tcp_offload_output(tp));
21334 	}
21335 #endif
21336 	if (rack->rack_deferred_inited == 0) {
21337 		/*
21338 		 * If we are the connecting socket we will
21339 		 * hit rack_init() when no sequence numbers
21340 		 * are setup. This makes it so we must defer
21341 		 * some initialization. Call that now.
21342 		 */
21343 		rack_deferred_init(tp, rack);
21344 	}
21345 	/*
21346 	 * For TFO connections in SYN_RECEIVED, only allow the initial
21347 	 * SYN|ACK and those sent by the retransmit timer.
21348 	 */
21349 	if (IS_FASTOPEN(tp->t_flags) &&
21350 	    (tp->t_state == TCPS_SYN_RECEIVED) &&
21351 	    SEQ_GT(tp->snd_max, tp->snd_una) &&    /* initial SYN|ACK sent */
21352 	    (rack->r_ctl.rc_resend == NULL)) {         /* not a retransmit */
21353 #ifdef TCP_ACCOUNTING
21354 		sched_unpin();
21355 #endif
21356 		return (0);
21357 	}
21358 #ifdef INET6
21359 	if (rack->r_state) {
21360 		/* Use the cache line loaded if possible */
21361 		isipv6 = rack->r_is_v6;
21362 	} else {
21363 		isipv6 = (rack->rc_inp->inp_vflag & INP_IPV6) != 0;
21364 	}
21365 #endif
21366 	early = 0;
21367 	cts = tcp_get_usecs(&tv);
21368 	ms_cts = tcp_tv_to_mssectick(&tv);
21369 	if (((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) &&
21370 	    tcp_in_hpts(rack->rc_tp)) {
21371 		/*
21372 		 * We are on the hpts for some timer but not hptsi output.
21373 		 * Remove from the hpts unconditionally.
21374 		 */
21375 		rack_timer_cancel(tp, rack, cts, __LINE__);
21376 	}
21377 	/* Are we pacing and late? */
21378 	if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
21379 	    TSTMP_GEQ(cts, rack->r_ctl.rc_last_output_to)) {
21380 		/* We are delayed */
21381 		delayed = cts - rack->r_ctl.rc_last_output_to;
21382 	} else {
21383 		delayed = 0;
21384 	}
21385 	/* Do the timers, which may override the pacer */
21386 	if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
21387 		int retval;
21388 
21389 		retval = rack_process_timers(tp, rack, cts, hpts_calling,
21390 					     &doing_tlp);
21391 		if (retval != 0) {
21392 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_ATIMER], 1);
21393 #ifdef TCP_ACCOUNTING
21394 			sched_unpin();
21395 #endif
21396 			/*
21397 			 * If timers want tcp_drop(), then pass error out,
21398 			 * otherwise suppress it.
21399 			 */
21400 			return (retval < 0 ? retval : 0);
21401 		}
21402 	}
21403 	if (rack->rc_in_persist) {
21404 		if (tcp_in_hpts(rack->rc_tp) == 0) {
21405 			/* Timer is not running */
21406 			rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
21407 		}
21408 #ifdef TCP_ACCOUNTING
21409 		sched_unpin();
21410 #endif
21411 		return (0);
21412 	}
21413 	if ((rack->rc_ack_required == 1) &&
21414 	    (rack->r_timer_override == 0)){
21415 		/* A timeout occurred and no ack has arrived */
21416 		if (tcp_in_hpts(rack->rc_tp) == 0) {
21417 			/* Timer is not running */
21418 			rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
21419 		}
21420 #ifdef TCP_ACCOUNTING
21421 		sched_unpin();
21422 #endif
21423 		return (0);
21424 	}
21425 	if ((rack->r_timer_override) ||
21426 	    (rack->rc_ack_can_sendout_data) ||
21427 	    (delayed) ||
21428 	    (tp->t_state < TCPS_ESTABLISHED)) {
21429 		rack->rc_ack_can_sendout_data = 0;
21430 		if (tcp_in_hpts(rack->rc_tp))
21431 			tcp_hpts_remove(rack->rc_tp);
21432 	} else if (tcp_in_hpts(rack->rc_tp)) {
21433 		/*
21434 		 * On the hpts you can't pass even if ACKNOW is on, we will
21435 		 * when the hpts fires.
21436 		 */
21437 #ifdef TCP_ACCOUNTING
21438 		crtsc = get_cyclecount();
21439 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21440 			tp->tcp_proc_time[SND_BLOCKED] += (crtsc - ts_val);
21441 		}
21442 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21443 			tp->tcp_cnt_counters[SND_BLOCKED]++;
21444 		}
21445 		sched_unpin();
21446 #endif
21447 		counter_u64_add(rack_out_size[TCP_MSS_ACCT_INPACE], 1);
21448 		return (0);
21449 	}
21450 	/* Finish out both pacing early and late accounting */
21451 	if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
21452 	    TSTMP_GT(rack->r_ctl.rc_last_output_to, cts)) {
21453 		early = rack->r_ctl.rc_last_output_to - cts;
21454 	} else
21455 		early = 0;
21456 	if (delayed && (rack->rc_always_pace == 1)) {
21457 		rack->r_ctl.rc_agg_delayed += delayed;
21458 		rack->r_late = 1;
21459 	} else if (early && (rack->rc_always_pace == 1)) {
21460 		rack->r_ctl.rc_agg_early += early;
21461 		rack->r_early = 1;
21462 	} else if (rack->rc_always_pace == 0) {
21463 		/* Non-paced we are not late */
21464 		rack->r_ctl.rc_agg_delayed = rack->r_ctl.rc_agg_early = 0;
21465 		rack->r_early = rack->r_late = 0;
21466 	}
21467 	/* Now that early/late accounting is done turn off the flag */
21468 	rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
21469 	rack->r_wanted_output = 0;
21470 	rack->r_timer_override = 0;
21471 	if ((tp->t_state != rack->r_state) &&
21472 	    TCPS_HAVEESTABLISHED(tp->t_state)) {
21473 		rack_set_state(tp, rack);
21474 	}
21475 	if ((rack->r_fast_output) &&
21476 	    (doing_tlp == 0) &&
21477 	    (tp->rcv_numsacks == 0)) {
21478 		int ret;
21479 
21480 		error = 0;
21481 		ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error);
21482 		if (ret >= 0)
21483 			return(ret);
21484 		else if (error) {
21485 			inp = rack->rc_inp;
21486 			so = inp->inp_socket;
21487 			sb = &so->so_snd;
21488 			goto nomore;
21489 		}
21490 	}
21491 	inp = rack->rc_inp;
21492 	/*
21493 	 * For TFO connections in SYN_SENT or SYN_RECEIVED,
21494 	 * only allow the initial SYN or SYN|ACK and those sent
21495 	 * by the retransmit timer.
21496 	 */
21497 	if (IS_FASTOPEN(tp->t_flags) &&
21498 	    ((tp->t_state == TCPS_SYN_RECEIVED) ||
21499 	     (tp->t_state == TCPS_SYN_SENT)) &&
21500 	    SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */
21501 	    (tp->t_rxtshift == 0)) {              /* not a retransmit */
21502 		cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
21503 		so = inp->inp_socket;
21504 		sb = &so->so_snd;
21505 		goto just_return_nolock;
21506 	}
21507 	/*
21508 	 * Determine length of data that should be transmitted, and flags
21509 	 * that will be used. If there is some data or critical controls
21510 	 * (SYN, RST) to send, then transmit; otherwise, investigate
21511 	 * further.
21512 	 */
21513 	idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
21514 	if (tp->t_idle_reduce) {
21515 		if (idle && (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur))
21516 			rack_cc_after_idle(rack, tp);
21517 	}
21518 	tp->t_flags &= ~TF_LASTIDLE;
21519 	if (idle) {
21520 		if (tp->t_flags & TF_MORETOCOME) {
21521 			tp->t_flags |= TF_LASTIDLE;
21522 			idle = 0;
21523 		}
21524 	}
21525 	if ((tp->snd_una == tp->snd_max) &&
21526 	    rack->r_ctl.rc_went_idle_time &&
21527 	    (cts > rack->r_ctl.rc_went_idle_time)) {
21528 		tot_idle = idle = (cts - rack->r_ctl.rc_went_idle_time);
21529 		if (idle > (uint64_t)rack_min_probertt_hold) {
21530 			/* Count as a probe rtt */
21531 			if (rack->in_probe_rtt == 0) {
21532 				rack->r_ctl.rc_lower_rtt_us_cts = cts;
21533 				rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts;
21534 				rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts;
21535 				rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts;
21536 			} else {
21537 				rack_exit_probertt(rack, cts);
21538 			}
21539 		}
21540 		idle = 0;
21541 	}
21542 	if(rack->policer_detect_on) {
21543 		/*
21544 		 * If we are doing policer detetion we at a minium
21545 		 * record the time but if possible add back to
21546 		 * the bucket based on the idle time.
21547 		 */
21548 		uint64_t idle_t, u64_cts;
21549 
21550 		segsiz = min(ctf_fixed_maxseg(tp),
21551 			     rack->r_ctl.rc_pace_min_segs);
21552 		u64_cts = tcp_tv_to_lusectick(&tv);
21553 		if ((rack->rc_policer_detected == 1) &&
21554 		    (rack->r_ctl.policer_bucket_size > segsiz) &&
21555 		    (rack->r_ctl.policer_bw > 0) &&
21556 		    (u64_cts > rack->r_ctl.last_sendtime)) {
21557 			/* We are being policed add back the time */
21558 			idle_t = u64_cts - rack->r_ctl.last_sendtime;
21559 			rack_credit_back_policer_idle_time(rack, idle_t, __LINE__);
21560 		}
21561 		rack->r_ctl.last_sendtime = u64_cts;
21562 	}
21563 	if (rack_use_fsb &&
21564 	    (rack->r_ctl.fsb.tcp_ip_hdr) &&
21565 	    (rack->r_fsb_inited == 0) &&
21566 	    (rack->r_state != TCPS_CLOSED))
21567 		rack_init_fsb_block(tp, rack, tcp_outflags[tp->t_state]);
21568 	if (rack->rc_sendvars_notset == 1) {
21569 		rack->r_ctl.idle_snd_una = tp->snd_una;
21570 		rack->rc_sendvars_notset = 0;
21571 		/*
21572 		 * Make sure any TCP timers (keep-alive) is not running.
21573 		 */
21574 		tcp_timer_stop(tp);
21575 	}
21576 	if ((rack->rack_no_prr == 1) &&
21577 	    (rack->rc_always_pace == 0)) {
21578 		/*
21579 		 * Sanity check before sending, if we have
21580 		 * no-pacing enabled and prr is turned off that
21581 		 * is a logistics error. Correct this by turnning
21582 		 * prr back on. A user *must* set some form of
21583 		 * pacing in order to turn PRR off. We do this
21584 		 * in the output path so that we can avoid socket
21585 		 * option ordering issues that would occur if we
21586 		 * tried to do it while setting rack_no_prr on.
21587 		 */
21588 		rack->rack_no_prr = 0;
21589 	}
21590 	if ((rack->pcm_enabled == 1) &&
21591 	    (rack->pcm_needed == 0) &&
21592 	    (tot_idle > 0)) {
21593 		/*
21594 		 * We have been idle some micro seconds. We need
21595 		 * to factor this in to see if a PCM is needed.
21596 		 */
21597 		uint32_t rtts_idle, rnds;
21598 
21599 		if (tp->t_srtt)
21600 			rtts_idle = tot_idle / tp->t_srtt;
21601 		else
21602 			rtts_idle = 0;
21603 		rnds = rack->r_ctl.current_round - rack->r_ctl.last_pcm_round;
21604 		rack->r_ctl.pcm_idle_rounds += rtts_idle;
21605 		if ((rnds + rack->r_ctl.pcm_idle_rounds)  >= rack_pcm_every_n_rounds) {
21606 			rack->pcm_needed = 1;
21607 			rack_log_pcm(rack, 8, rack->r_ctl.last_pcm_round, rtts_idle, rack->r_ctl.current_round );
21608 		}
21609 	}
21610 again:
21611 	sendalot = 0;
21612 	cts = tcp_get_usecs(&tv);
21613 	ms_cts = tcp_tv_to_mssectick(&tv);
21614 	tso = 0;
21615 	mtu = 0;
21616 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
21617 	minseg = segsiz;
21618 	if (rack->r_ctl.rc_pace_max_segs == 0)
21619 		pace_max_seg = rack->rc_user_set_max_segs * segsiz;
21620 	else
21621 		pace_max_seg = rack->r_ctl.rc_pace_max_segs;
21622 	if (TCPS_HAVEESTABLISHED(tp->t_state) &&
21623 	    (rack->r_ctl.pcm_max_seg == 0)) {
21624 		/*
21625 		 * We set in our first send so we know that the ctf_fixed_maxseg
21626 		 * has been fully set. If we do it in rack_init() we most likely
21627 		 * see 512 bytes so we end up at 5120, not desirable.
21628 		 */
21629 		rack->r_ctl.pcm_max_seg = rc_init_window(rack);
21630 		if (rack->r_ctl.pcm_max_seg < (ctf_fixed_maxseg(tp) * 10)) {
21631 			/*
21632 			 * Assure our initial PCM probe is at least 10 MSS.
21633 			 */
21634 			rack->r_ctl.pcm_max_seg = ctf_fixed_maxseg(tp) * 10;
21635 		}
21636 	}
21637 	if ((rack->r_ctl.pcm_max_seg != 0)  && (rack->pcm_needed == 1)) {
21638 		uint32_t rw_avail, cwa;
21639 
21640 		if (tp->snd_wnd > ctf_outstanding(tp))
21641 			rw_avail = tp->snd_wnd - ctf_outstanding(tp);
21642 		else
21643 			rw_avail = 0;
21644 		if (tp->snd_cwnd > ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked))
21645 			cwa = tp->snd_cwnd -ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
21646 		else
21647 			cwa = 0;
21648 		if ((cwa >= rack->r_ctl.pcm_max_seg) &&
21649 		    (rw_avail > rack->r_ctl.pcm_max_seg)) {
21650 			/* Raise up the max seg for this trip through */
21651 			pace_max_seg = rack->r_ctl.pcm_max_seg;
21652 			/* Disable any fast output */
21653 			rack->r_fast_output = 0;
21654 		}
21655 		if (rack_verbose_logging) {
21656 			rack_log_pcm(rack, 4,
21657 				     cwa, rack->r_ctl.pcm_max_seg, rw_avail);
21658 		}
21659 	}
21660 	sb_offset = tp->snd_max - tp->snd_una;
21661 	cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
21662 	flags = tcp_outflags[tp->t_state];
21663 	while (rack->rc_free_cnt < rack_free_cache) {
21664 		rsm = rack_alloc(rack);
21665 		if (rsm == NULL) {
21666 			if (hpts_calling)
21667 				/* Retry in a ms */
21668 				slot = (1 * HPTS_USEC_IN_MSEC);
21669 			so = inp->inp_socket;
21670 			sb = &so->so_snd;
21671 			goto just_return_nolock;
21672 		}
21673 		TAILQ_INSERT_TAIL(&rack->r_ctl.rc_free, rsm, r_tnext);
21674 		rack->rc_free_cnt++;
21675 		rsm = NULL;
21676 	}
21677 	sack_rxmit = 0;
21678 	len = 0;
21679 	rsm = NULL;
21680 	if (flags & TH_RST) {
21681 		SOCKBUF_LOCK(&inp->inp_socket->so_snd);
21682 		so = inp->inp_socket;
21683 		sb = &so->so_snd;
21684 		goto send;
21685 	}
21686 	if (rack->r_ctl.rc_resend) {
21687 		/* Retransmit timer */
21688 		rsm = rack->r_ctl.rc_resend;
21689 		rack->r_ctl.rc_resend = NULL;
21690 		len = rsm->r_end - rsm->r_start;
21691 		sack_rxmit = 1;
21692 		sendalot = 0;
21693 		KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
21694 			("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
21695 			 __func__, __LINE__,
21696 			 rsm->r_start, tp->snd_una, tp, rack, rsm));
21697 		sb_offset = rsm->r_start - tp->snd_una;
21698 		rack_validate_sizes(rack, &len, segsiz, pace_max_seg);
21699 	} else if (rack->r_collapse_point_valid &&
21700 		   ((rsm = rack_check_collapsed(rack, cts)) != NULL)) {
21701 		/*
21702 		 * If an RSM is returned then enough time has passed
21703 		 * for us to retransmit it. Move up the collapse point,
21704 		 * since this rsm has its chance to retransmit now.
21705 		 */
21706 		tcp_trace_point(rack->rc_tp, TCP_TP_COLLAPSED_RXT);
21707 		rack->r_ctl.last_collapse_point = rsm->r_end;
21708 		/* Are we done? */
21709 		if (SEQ_GEQ(rack->r_ctl.last_collapse_point,
21710 			    rack->r_ctl.high_collapse_point))
21711 			rack->r_collapse_point_valid = 0;
21712 		sack_rxmit = 1;
21713 		/* We are not doing a TLP */
21714 		doing_tlp = 0;
21715 		len = rsm->r_end - rsm->r_start;
21716 		sb_offset = rsm->r_start - tp->snd_una;
21717 		sendalot = 0;
21718 		rack_validate_sizes(rack, &len, segsiz, pace_max_seg);
21719 	} else if ((rsm = tcp_rack_output(tp, rack, cts)) != NULL) {
21720 		/* We have a retransmit that takes precedence */
21721 		if ((!IN_FASTRECOVERY(tp->t_flags)) &&
21722 		    ((rsm->r_flags & RACK_MUST_RXT) == 0) &&
21723 		    ((tp->t_flags & TF_WASFRECOVERY) == 0)) {
21724 			/* Enter recovery if not induced by a time-out */
21725 			rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__);
21726 		}
21727 #ifdef INVARIANTS
21728 		if (SEQ_LT(rsm->r_start, tp->snd_una)) {
21729 			panic("Huh, tp:%p rack:%p rsm:%p start:%u < snd_una:%u\n",
21730 			      tp, rack, rsm, rsm->r_start, tp->snd_una);
21731 		}
21732 #endif
21733 		len = rsm->r_end - rsm->r_start;
21734 		KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
21735 			("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
21736 			 __func__, __LINE__,
21737 			 rsm->r_start, tp->snd_una, tp, rack, rsm));
21738 		sb_offset = rsm->r_start - tp->snd_una;
21739 		sendalot = 0;
21740 		rack_validate_sizes(rack, &len, segsiz, pace_max_seg);
21741 		if (len > 0) {
21742 			sack_rxmit = 1;
21743 			KMOD_TCPSTAT_INC(tcps_sack_rexmits);
21744 			KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes,
21745 					 min(len, segsiz));
21746 		}
21747 	} else if (rack->r_ctl.rc_tlpsend) {
21748 		/* Tail loss probe */
21749 		long cwin;
21750 		long tlen;
21751 
21752 		/*
21753 		 * Check if we can do a TLP with a RACK'd packet
21754 		 * this can happen if we are not doing the rack
21755 		 * cheat and we skipped to a TLP and it
21756 		 * went off.
21757 		 */
21758 		rsm = rack->r_ctl.rc_tlpsend;
21759 		/* We are doing a TLP make sure the flag is preent */
21760 		rsm->r_flags |= RACK_TLP;
21761 		rack->r_ctl.rc_tlpsend = NULL;
21762 		sack_rxmit = 1;
21763 		tlen = rsm->r_end - rsm->r_start;
21764 		if (tlen > segsiz)
21765 			tlen = segsiz;
21766 		KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
21767 			("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
21768 			 __func__, __LINE__,
21769 			 rsm->r_start, tp->snd_una, tp, rack, rsm));
21770 		sb_offset = rsm->r_start - tp->snd_una;
21771 		cwin = min(tp->snd_wnd, tlen);
21772 		len = cwin;
21773 	}
21774 	if (rack->r_must_retran &&
21775 	    (doing_tlp == 0) &&
21776 	    (SEQ_GT(tp->snd_max, tp->snd_una)) &&
21777 	    (rsm == NULL)) {
21778 		/*
21779 		 * There are two different ways that we
21780 		 * can get into this block:
21781 		 * a) This is a non-sack connection, we had a time-out
21782 		 *    and thus r_must_retran was set and everything
21783 		 *    left outstanding as been marked for retransmit.
21784 		 * b) The MTU of the path shrank, so that everything
21785 		 *    was marked to be retransmitted with the smaller
21786 		 *    mtu and r_must_retran was set.
21787 		 *
21788 		 * This means that we expect the sendmap (outstanding)
21789 		 * to all be marked must. We can use the tmap to
21790 		 * look at them.
21791 		 *
21792 		 */
21793 		int sendwin, flight;
21794 
21795 		sendwin = min(tp->snd_wnd, tp->snd_cwnd);
21796 		flight = ctf_flight_size(tp, rack->r_ctl.rc_out_at_rto);
21797 		if (flight >= sendwin) {
21798 			/*
21799 			 * We can't send yet.
21800 			 */
21801 			so = inp->inp_socket;
21802 			sb = &so->so_snd;
21803 			goto just_return_nolock;
21804 		}
21805 		/*
21806 		 * This is the case a/b mentioned above. All
21807 		 * outstanding/not-acked should be marked.
21808 		 * We can use the tmap to find them.
21809 		 */
21810 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
21811 		if (rsm == NULL) {
21812 			/* TSNH */
21813 			rack->r_must_retran = 0;
21814 			rack->r_ctl.rc_out_at_rto = 0;
21815 			so = inp->inp_socket;
21816 			sb = &so->so_snd;
21817 			goto just_return_nolock;
21818 		}
21819 		if ((rsm->r_flags & RACK_MUST_RXT) == 0) {
21820 			/*
21821 			 * The first one does not have the flag, did we collapse
21822 			 * further up in our list?
21823 			 */
21824 			rack->r_must_retran = 0;
21825 			rack->r_ctl.rc_out_at_rto = 0;
21826 			rsm = NULL;
21827 			sack_rxmit = 0;
21828 		} else {
21829 			sack_rxmit = 1;
21830 			len = rsm->r_end - rsm->r_start;
21831 			sb_offset = rsm->r_start - tp->snd_una;
21832 			sendalot = 0;
21833 			if ((rack->full_size_rxt == 0) &&
21834 			    (rack->shape_rxt_to_pacing_min == 0) &&
21835 			    (len >= segsiz))
21836 				len = segsiz;
21837 			else if (rack->shape_rxt_to_pacing_min &&
21838 				 rack->gp_ready) {
21839 				/* We use pacing min as shaping len req */
21840 				uint32_t maxlen;
21841 
21842 				maxlen = rack_get_hpts_pacing_min_for_bw(rack, segsiz);
21843 				if (len > maxlen)
21844 					len = maxlen;
21845 			}
21846 			/*
21847 			 * Delay removing the flag RACK_MUST_RXT so
21848 			 * that the fastpath for retransmit will
21849 			 * work with this rsm.
21850 			 */
21851 		}
21852 	}
21853 	/*
21854 	 * Enforce a connection sendmap count limit if set
21855 	 * as long as we are not retransmiting.
21856 	 */
21857 	if ((rsm == NULL) &&
21858 	    (rack->do_detection == 0) &&
21859 	    (V_tcp_map_entries_limit > 0) &&
21860 	    (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
21861 		counter_u64_add(rack_to_alloc_limited, 1);
21862 		if (!rack->alloc_limit_reported) {
21863 			rack->alloc_limit_reported = 1;
21864 			counter_u64_add(rack_alloc_limited_conns, 1);
21865 		}
21866 		so = inp->inp_socket;
21867 		sb = &so->so_snd;
21868 		goto just_return_nolock;
21869 	}
21870 	if (rsm && (rsm->r_flags & RACK_HAS_FIN)) {
21871 		/* we are retransmitting the fin */
21872 		len--;
21873 		if (len) {
21874 			/*
21875 			 * When retransmitting data do *not* include the
21876 			 * FIN. This could happen from a TLP probe.
21877 			 */
21878 			flags &= ~TH_FIN;
21879 		}
21880 	}
21881 	if (rsm && rack->r_fsb_inited &&
21882 	    rack_use_rsm_rfo &&
21883 	    ((rsm->r_flags & RACK_HAS_FIN) == 0)) {
21884 		int ret;
21885 
21886 		if ((rack->rc_policer_detected == 1) &&
21887 		    (rack->r_ctl.policer_bucket_size > segsiz) &&
21888 		    (rack->r_ctl.policer_bw > 0)) {
21889 			/* Check to see if there is room */
21890 			if (rack->r_ctl.current_policer_bucket < len) {
21891 				goto skip_fast_output;
21892 			}
21893 		}
21894 		ret = rack_fast_rsm_output(tp, rack, rsm, ts_val, cts, ms_cts, &tv, len, doing_tlp);
21895 		if (ret == 0)
21896 			return (0);
21897 	}
21898 skip_fast_output:
21899 	so = inp->inp_socket;
21900 	sb = &so->so_snd;
21901 	if (do_a_prefetch == 0) {
21902 		kern_prefetch(sb, &do_a_prefetch);
21903 		do_a_prefetch = 1;
21904 	}
21905 #ifdef NETFLIX_SHARED_CWND
21906 	if ((tp->t_flags2 & TF2_TCP_SCWND_ALLOWED) &&
21907 	    rack->rack_enable_scwnd) {
21908 		/* We are doing cwnd sharing */
21909 		if (rack->gp_ready &&
21910 		    (rack->rack_attempted_scwnd == 0) &&
21911 		    (rack->r_ctl.rc_scw == NULL) &&
21912 		    tp->t_lib) {
21913 			/* The pcbid is in, lets make an attempt */
21914 			counter_u64_add(rack_try_scwnd, 1);
21915 			rack->rack_attempted_scwnd = 1;
21916 			rack->r_ctl.rc_scw = tcp_shared_cwnd_alloc(tp,
21917 								   &rack->r_ctl.rc_scw_index,
21918 								   segsiz);
21919 		}
21920 		if (rack->r_ctl.rc_scw &&
21921 		    (rack->rack_scwnd_is_idle == 1) &&
21922 		    sbavail(&so->so_snd)) {
21923 			/* we are no longer out of data */
21924 			tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
21925 			rack->rack_scwnd_is_idle = 0;
21926 		}
21927 		if (rack->r_ctl.rc_scw) {
21928 			/* First lets update and get the cwnd */
21929 			rack->r_ctl.cwnd_to_use = cwnd_to_use = tcp_shared_cwnd_update(rack->r_ctl.rc_scw,
21930 										       rack->r_ctl.rc_scw_index,
21931 										       tp->snd_cwnd, tp->snd_wnd, segsiz);
21932 		}
21933 	}
21934 #endif
21935 	/*
21936 	 * Get standard flags, and add SYN or FIN if requested by 'hidden'
21937 	 * state flags.
21938 	 */
21939 	if (tp->t_flags & TF_NEEDFIN)
21940 		flags |= TH_FIN;
21941 	if (tp->t_flags & TF_NEEDSYN)
21942 		flags |= TH_SYN;
21943 	if ((sack_rxmit == 0) && (prefetch_rsm == 0)) {
21944 		void *end_rsm;
21945 		end_rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
21946 		if (end_rsm)
21947 			kern_prefetch(end_rsm, &prefetch_rsm);
21948 		prefetch_rsm = 1;
21949 	}
21950 	SOCKBUF_LOCK(sb);
21951 	if ((sack_rxmit == 0) &&
21952 	    (TCPS_HAVEESTABLISHED(tp->t_state) || IS_FASTOPEN(tp->t_flags))) {
21953 		/*
21954 		 * We are not retransmitting (sack_rxmit is 0) so we
21955 		 * are sending new data. This is always based on snd_max.
21956 		 * Now in theory snd_max may be equal to snd_una, if so
21957 		 * then nothing is outstanding and the offset would be 0.
21958 		 */
21959 		uint32_t avail;
21960 
21961 		avail = sbavail(sb);
21962 		if (SEQ_GT(tp->snd_max, tp->snd_una) && avail)
21963 			sb_offset = tp->snd_max - tp->snd_una;
21964 		else
21965 			sb_offset = 0;
21966 		if ((IN_FASTRECOVERY(tp->t_flags) == 0) || rack->rack_no_prr) {
21967 			if (rack->r_ctl.rc_tlp_new_data) {
21968 				/* TLP is forcing out new data */
21969 				if (rack->r_ctl.rc_tlp_new_data > (uint32_t) (avail - sb_offset)) {
21970 					rack->r_ctl.rc_tlp_new_data = (uint32_t) (avail - sb_offset);
21971 				}
21972 				if ((rack->r_ctl.rc_tlp_new_data + sb_offset) > tp->snd_wnd) {
21973 					if (tp->snd_wnd > sb_offset)
21974 						len = tp->snd_wnd - sb_offset;
21975 					else
21976 						len = 0;
21977 				} else {
21978 					len = rack->r_ctl.rc_tlp_new_data;
21979 				}
21980 				rack->r_ctl.rc_tlp_new_data = 0;
21981 			}  else {
21982 				len = rack_what_can_we_send(tp, rack, cwnd_to_use, avail, sb_offset);
21983 			}
21984 			if ((rack->r_ctl.crte == NULL) &&
21985 			    IN_FASTRECOVERY(tp->t_flags) &&
21986 			    (rack->full_size_rxt == 0) &&
21987 			    (rack->shape_rxt_to_pacing_min == 0) &&
21988 			    (len > segsiz)) {
21989 				/*
21990 				 * For prr=off, we need to send only 1 MSS
21991 				 * at a time. We do this because another sack could
21992 				 * be arriving that causes us to send retransmits and
21993 				 * we don't want to be on a long pace due to a larger send
21994 				 * that keeps us from sending out the retransmit.
21995 				 */
21996 				len = segsiz;
21997 			} else if (rack->shape_rxt_to_pacing_min &&
21998 				   rack->gp_ready) {
21999 				/* We use pacing min as shaping len req */
22000 				uint32_t maxlen;
22001 
22002 				maxlen = rack_get_hpts_pacing_min_for_bw(rack, segsiz);
22003 				if (len > maxlen)
22004 					len = maxlen;
22005 			}/* The else is full_size_rxt is on so send it all */
22006 		} else {
22007 			uint32_t outstanding;
22008 			/*
22009 			 * We are inside of a Fast recovery episode, this
22010 			 * is caused by a SACK or 3 dup acks. At this point
22011 			 * we have sent all the retransmissions and we rely
22012 			 * on PRR to dictate what we will send in the form of
22013 			 * new data.
22014 			 */
22015 
22016 			outstanding = tp->snd_max - tp->snd_una;
22017 			if ((rack->r_ctl.rc_prr_sndcnt + outstanding) > tp->snd_wnd) {
22018 				if (tp->snd_wnd > outstanding) {
22019 					len = tp->snd_wnd - outstanding;
22020 					/* Check to see if we have the data */
22021 					if ((sb_offset + len) > avail) {
22022 						/* It does not all fit */
22023 						if (avail > sb_offset)
22024 							len = avail - sb_offset;
22025 						else
22026 							len = 0;
22027 					}
22028 				} else {
22029 					len = 0;
22030 				}
22031 			} else if (avail > sb_offset) {
22032 				len = avail - sb_offset;
22033 			} else {
22034 				len = 0;
22035 			}
22036 			if (len > 0) {
22037 				if (len > rack->r_ctl.rc_prr_sndcnt) {
22038 					len = rack->r_ctl.rc_prr_sndcnt;
22039 				}
22040 				if (len > 0) {
22041 					sub_from_prr = 1;
22042 				}
22043 			}
22044 			if (len > segsiz) {
22045 				/*
22046 				 * We should never send more than a MSS when
22047 				 * retransmitting or sending new data in prr
22048 				 * mode unless the override flag is on. Most
22049 				 * likely the PRR algorithm is not going to
22050 				 * let us send a lot as well :-)
22051 				 */
22052 				if (rack->r_ctl.rc_prr_sendalot == 0) {
22053 					len = segsiz;
22054 				}
22055 			} else if (len < segsiz) {
22056 				/*
22057 				 * Do we send any? The idea here is if the
22058 				 * send empty's the socket buffer we want to
22059 				 * do it. However if not then lets just wait
22060 				 * for our prr_sndcnt to get bigger.
22061 				 */
22062 				long leftinsb;
22063 
22064 				leftinsb = sbavail(sb) - sb_offset;
22065 				if (leftinsb > len) {
22066 					/* This send does not empty the sb */
22067 					len = 0;
22068 				}
22069 			}
22070 		}
22071 	} else if (!TCPS_HAVEESTABLISHED(tp->t_state)) {
22072 		/*
22073 		 * If you have not established
22074 		 * and are not doing FAST OPEN
22075 		 * no data please.
22076 		 */
22077 		if ((sack_rxmit == 0) &&
22078 		    (!IS_FASTOPEN(tp->t_flags))){
22079 			len = 0;
22080 			sb_offset = 0;
22081 		}
22082 	}
22083 	if (prefetch_so_done == 0) {
22084 		kern_prefetch(so, &prefetch_so_done);
22085 		prefetch_so_done = 1;
22086 	}
22087 	orig_len = len;
22088 	if ((rack->rc_policer_detected == 1) &&
22089 	    (rack->r_ctl.policer_bucket_size > segsiz) &&
22090 	    (rack->r_ctl.policer_bw > 0) &&
22091 	    (len > 0)) {
22092 		/*
22093 		 * Ok we believe we have a policer watching
22094 		 * what we send, can we send len? If not can
22095 		 * we tune it down to a smaller value?
22096 		 */
22097 		uint32_t plen, buck_needs;
22098 
22099 		plen = rack_policer_check_send(rack, len, segsiz, &buck_needs);
22100 		if (plen == 0) {
22101 			/*
22102 			 * We are not allowed to send. How long
22103 			 * do we need to pace for i.e. how long
22104 			 * before len is available to send?
22105 			 */
22106 			uint64_t lentime;
22107 
22108 			lentime = buck_needs;
22109 			lentime *= HPTS_USEC_IN_SEC;
22110 			lentime /= rack->r_ctl.policer_bw;
22111 			slot = (uint32_t)lentime;
22112 			tot_len_this_send = 0;
22113 			SOCKBUF_UNLOCK(sb);
22114 			if (rack_verbose_logging > 0)
22115 				policer_detection_log(rack, len, slot, buck_needs, 0, 12);
22116 			rack_start_hpts_timer(rack, tp, cts, slot, 0, 0);
22117 			rack_log_type_just_return(rack, cts, 0, slot, hpts_calling, 0, cwnd_to_use);
22118 			goto just_return_clean;
22119 		}
22120 		if (plen < len) {
22121 			sendalot = 0;
22122 			len = plen;
22123 		}
22124 	}
22125 	/*
22126 	 * Lop off SYN bit if it has already been sent.  However, if this is
22127 	 * SYN-SENT state and if segment contains data and if we don't know
22128 	 * that foreign host supports TAO, suppress sending segment.
22129 	 */
22130 	if ((flags & TH_SYN) &&
22131 	    SEQ_GT(tp->snd_max, tp->snd_una) &&
22132 	    ((sack_rxmit == 0) &&
22133 	     (tp->t_rxtshift == 0))) {
22134 		/*
22135 		 * When sending additional segments following a TFO SYN|ACK,
22136 		 * do not include the SYN bit.
22137 		 */
22138 		if (IS_FASTOPEN(tp->t_flags) &&
22139 		    (tp->t_state == TCPS_SYN_RECEIVED))
22140 			flags &= ~TH_SYN;
22141 	}
22142 	/*
22143 	 * Be careful not to send data and/or FIN on SYN segments. This
22144 	 * measure is needed to prevent interoperability problems with not
22145 	 * fully conformant TCP implementations.
22146 	 */
22147 	if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
22148 		len = 0;
22149 		flags &= ~TH_FIN;
22150 	}
22151 	/*
22152 	 * On TFO sockets, ensure no data is sent in the following cases:
22153 	 *
22154 	 *  - When retransmitting SYN|ACK on a passively-created socket
22155 	 *
22156 	 *  - When retransmitting SYN on an actively created socket
22157 	 *
22158 	 *  - When sending a zero-length cookie (cookie request) on an
22159 	 *    actively created socket
22160 	 *
22161 	 *  - When the socket is in the CLOSED state (RST is being sent)
22162 	 */
22163 	if (IS_FASTOPEN(tp->t_flags) &&
22164 	    (((flags & TH_SYN) && (tp->t_rxtshift > 0)) ||
22165 	     ((tp->t_state == TCPS_SYN_SENT) &&
22166 	      (tp->t_tfo_client_cookie_len == 0)) ||
22167 	     (flags & TH_RST))) {
22168 		sack_rxmit = 0;
22169 		len = 0;
22170 	}
22171 	/* Without fast-open there should never be data sent on a SYN */
22172 	if ((flags & TH_SYN) && (!IS_FASTOPEN(tp->t_flags))) {
22173 		len = 0;
22174 	}
22175 	if ((len > segsiz) && (tcp_dsack_block_exists(tp))) {
22176 		/* We only send 1 MSS if we have a DSACK block */
22177 		add_flag |= RACK_SENT_W_DSACK;
22178 		len = segsiz;
22179 	}
22180 	if (len <= 0) {
22181 		/*
22182 		 * We have nothing to send, or the window shrank, or
22183 		 * is closed, do we need to go into persists?
22184 		 */
22185 		len = 0;
22186 		if ((tp->snd_wnd == 0) &&
22187 		    (TCPS_HAVEESTABLISHED(tp->t_state)) &&
22188 		    (tp->snd_una == tp->snd_max) &&
22189 		    (sb_offset < (int)sbavail(sb))) {
22190 			rack_enter_persist(tp, rack, cts, tp->snd_una);
22191 		}
22192 	} else if ((rsm == NULL) &&
22193 		   (doing_tlp == 0) &&
22194 		   (len < pace_max_seg)) {
22195 		/*
22196 		 * We are not sending a maximum sized segment for
22197 		 * some reason. Should we not send anything (think
22198 		 * sws or persists)?
22199 		 */
22200 		if ((tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg)) &&
22201 		    (TCPS_HAVEESTABLISHED(tp->t_state)) &&
22202 		    (len < minseg) &&
22203 		    (len < (int)(sbavail(sb) - sb_offset))) {
22204 			/*
22205 			 * Here the rwnd is less than
22206 			 * the minimum pacing size, this is not a retransmit,
22207 			 * we are established and
22208 			 * the send is not the last in the socket buffer
22209 			 * we send nothing, and we may enter persists
22210 			 * if nothing is outstanding.
22211 			 */
22212 			len = 0;
22213 			if (tp->snd_max == tp->snd_una) {
22214 				/*
22215 				 * Nothing out we can
22216 				 * go into persists.
22217 				 */
22218 				rack_enter_persist(tp, rack, cts, tp->snd_una);
22219 			}
22220 		} else if ((cwnd_to_use >= max(minseg, (segsiz * 4))) &&
22221 			   (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) &&
22222 			   (len < (int)(sbavail(sb) - sb_offset)) &&
22223 			   (len < minseg)) {
22224 			/*
22225 			 * Here we are not retransmitting, and
22226 			 * the cwnd is not so small that we could
22227 			 * not send at least a min size (rxt timer
22228 			 * not having gone off), We have 2 segments or
22229 			 * more already in flight, its not the tail end
22230 			 * of the socket buffer  and the cwnd is blocking
22231 			 * us from sending out a minimum pacing segment size.
22232 			 * Lets not send anything.
22233 			 */
22234 			len = 0;
22235 		} else if (((tp->snd_wnd - ctf_outstanding(tp)) <
22236 			    min((rack->r_ctl.rc_high_rwnd/2), minseg)) &&
22237 			   (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) &&
22238 			   (len < (int)(sbavail(sb) - sb_offset)) &&
22239 			   (TCPS_HAVEESTABLISHED(tp->t_state))) {
22240 			/*
22241 			 * Here we have a send window but we have
22242 			 * filled it up and we can't send another pacing segment.
22243 			 * We also have in flight more than 2 segments
22244 			 * and we are not completing the sb i.e. we allow
22245 			 * the last bytes of the sb to go out even if
22246 			 * its not a full pacing segment.
22247 			 */
22248 			len = 0;
22249 		} else if ((rack->r_ctl.crte != NULL) &&
22250 			   (tp->snd_wnd >= (pace_max_seg * max(1, rack_hw_rwnd_factor))) &&
22251 			   (cwnd_to_use >= (pace_max_seg + (4 * segsiz))) &&
22252 			   (ctf_flight_size(tp, rack->r_ctl.rc_sacked) >= (2 * segsiz)) &&
22253 			   (len < (int)(sbavail(sb) - sb_offset))) {
22254 			/*
22255 			 * Here we are doing hardware pacing, this is not a TLP,
22256 			 * we are not sending a pace max segment size, there is rwnd
22257 			 * room to send at least N pace_max_seg, the cwnd is greater
22258 			 * than or equal to a full pacing segments plus 4 mss and we have 2 or
22259 			 * more segments in flight and its not the tail of the socket buffer.
22260 			 *
22261 			 * We don't want to send instead we need to get more ack's in to
22262 			 * allow us to send a full pacing segment. Normally, if we are pacing
22263 			 * about the right speed, we should have finished our pacing
22264 			 * send as most of the acks have come back if we are at the
22265 			 * right rate. This is a bit fuzzy since return path delay
22266 			 * can delay the acks, which is why we want to make sure we
22267 			 * have cwnd space to have a bit more than a max pace segments in flight.
22268 			 *
22269 			 * If we have not gotten our acks back we are pacing at too high a
22270 			 * rate delaying will not hurt and will bring our GP estimate down by
22271 			 * injecting the delay. If we don't do this we will send
22272 			 * 2 MSS out in response to the acks being clocked in which
22273 			 * defeats the point of hw-pacing (i.e. to help us get
22274 			 * larger TSO's out).
22275 			 */
22276 			len = 0;
22277 		}
22278 
22279 	}
22280 	/* len will be >= 0 after this point. */
22281 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
22282 	rack_sndbuf_autoscale(rack);
22283 	/*
22284 	 * Decide if we can use TCP Segmentation Offloading (if supported by
22285 	 * hardware).
22286 	 *
22287 	 * TSO may only be used if we are in a pure bulk sending state.  The
22288 	 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP
22289 	 * options prevent using TSO.  With TSO the TCP header is the same
22290 	 * (except for the sequence number) for all generated packets.  This
22291 	 * makes it impossible to transmit any options which vary per
22292 	 * generated segment or packet.
22293 	 *
22294 	 * IPv4 handling has a clear separation of ip options and ip header
22295 	 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does
22296 	 * the right thing below to provide length of just ip options and thus
22297 	 * checking for ipoptlen is enough to decide if ip options are present.
22298 	 */
22299 	ipoptlen = 0;
22300 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
22301 	/*
22302 	 * Pre-calculate here as we save another lookup into the darknesses
22303 	 * of IPsec that way and can actually decide if TSO is ok.
22304 	 */
22305 #ifdef INET6
22306 	if (isipv6 && IPSEC_ENABLED(ipv6))
22307 		ipsec_optlen = IPSEC_HDRSIZE(ipv6, inp);
22308 #ifdef INET
22309 	else
22310 #endif
22311 #endif				/* INET6 */
22312 #ifdef INET
22313 		if (IPSEC_ENABLED(ipv4))
22314 			ipsec_optlen = IPSEC_HDRSIZE(ipv4, inp);
22315 #endif				/* INET */
22316 #endif
22317 
22318 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
22319 	ipoptlen += ipsec_optlen;
22320 #endif
22321 	if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > segsiz &&
22322 	    (tp->t_port == 0) &&
22323 	    ((tp->t_flags & TF_SIGNATURE) == 0) &&
22324 	    tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
22325 	    ipoptlen == 0)
22326 		tso = 1;
22327 	{
22328 		uint32_t outstanding __unused;
22329 
22330 		outstanding = tp->snd_max - tp->snd_una;
22331 		if (tp->t_flags & TF_SENTFIN) {
22332 			/*
22333 			 * If we sent a fin, snd_max is 1 higher than
22334 			 * snd_una
22335 			 */
22336 			outstanding--;
22337 		}
22338 		if (sack_rxmit) {
22339 			if ((rsm->r_flags & RACK_HAS_FIN) == 0)
22340 				flags &= ~TH_FIN;
22341 		}
22342 	}
22343 	recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
22344 		      (long)TCP_MAXWIN << tp->rcv_scale);
22345 
22346 	/*
22347 	 * Sender silly window avoidance.   We transmit under the following
22348 	 * conditions when len is non-zero:
22349 	 *
22350 	 * - We have a full segment (or more with TSO) - This is the last
22351 	 * buffer in a write()/send() and we are either idle or running
22352 	 * NODELAY - we've timed out (e.g. persist timer) - we have more
22353 	 * then 1/2 the maximum send window's worth of data (receiver may be
22354 	 * limited the window size) - we need to retransmit
22355 	 */
22356 	if (len) {
22357 		if (len >= segsiz) {
22358 			goto send;
22359 		}
22360 		/*
22361 		 * NOTE! on localhost connections an 'ack' from the remote
22362 		 * end may occur synchronously with the output and cause us
22363 		 * to flush a buffer queued with moretocome.  XXX
22364 		 *
22365 		 */
22366 		if (!(tp->t_flags & TF_MORETOCOME) &&	/* normal case */
22367 		    (idle || (tp->t_flags & TF_NODELAY)) &&
22368 		    ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) &&
22369 		    (tp->t_flags & TF_NOPUSH) == 0) {
22370 			pass = 2;
22371 			goto send;
22372 		}
22373 		if ((tp->snd_una == tp->snd_max) && len) {	/* Nothing outstanding */
22374 			pass = 22;
22375 			goto send;
22376 		}
22377 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) {
22378 			pass = 4;
22379 			goto send;
22380 		}
22381 		if (sack_rxmit) {
22382 			pass = 6;
22383 			goto send;
22384 		}
22385 		if (((tp->snd_wnd - ctf_outstanding(tp)) < segsiz) &&
22386 		    (ctf_outstanding(tp) < (segsiz * 2))) {
22387 			/*
22388 			 * We have less than two MSS outstanding (delayed ack)
22389 			 * and our rwnd will not let us send a full sized
22390 			 * MSS. Lets go ahead and let this small segment
22391 			 * out because we want to try to have at least two
22392 			 * packets inflight to not be caught by delayed ack.
22393 			 */
22394 			pass = 12;
22395 			goto send;
22396 		}
22397 	}
22398 	/*
22399 	 * Sending of standalone window updates.
22400 	 *
22401 	 * Window updates are important when we close our window due to a
22402 	 * full socket buffer and are opening it again after the application
22403 	 * reads data from it.  Once the window has opened again and the
22404 	 * remote end starts to send again the ACK clock takes over and
22405 	 * provides the most current window information.
22406 	 *
22407 	 * We must avoid the silly window syndrome whereas every read from
22408 	 * the receive buffer, no matter how small, causes a window update
22409 	 * to be sent.  We also should avoid sending a flurry of window
22410 	 * updates when the socket buffer had queued a lot of data and the
22411 	 * application is doing small reads.
22412 	 *
22413 	 * Prevent a flurry of pointless window updates by only sending an
22414 	 * update when we can increase the advertized window by more than
22415 	 * 1/4th of the socket buffer capacity.  When the buffer is getting
22416 	 * full or is very small be more aggressive and send an update
22417 	 * whenever we can increase by two mss sized segments. In all other
22418 	 * situations the ACK's to new incoming data will carry further
22419 	 * window increases.
22420 	 *
22421 	 * Don't send an independent window update if a delayed ACK is
22422 	 * pending (it will get piggy-backed on it) or the remote side
22423 	 * already has done a half-close and won't send more data.  Skip
22424 	 * this if the connection is in T/TCP half-open state.
22425 	 */
22426 	if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
22427 	    !(tp->t_flags & TF_DELACK) &&
22428 	    !TCPS_HAVERCVDFIN(tp->t_state)) {
22429 		/*
22430 		 * "adv" is the amount we could increase the window, taking
22431 		 * into account that we are limited by TCP_MAXWIN <<
22432 		 * tp->rcv_scale.
22433 		 */
22434 		int32_t adv;
22435 		int oldwin;
22436 
22437 		adv = recwin;
22438 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
22439 			oldwin = (tp->rcv_adv - tp->rcv_nxt);
22440 			if (adv > oldwin)
22441 				adv -= oldwin;
22442 			else {
22443 				/* We can't increase the window */
22444 				adv = 0;
22445 			}
22446 		} else
22447 			oldwin = 0;
22448 
22449 		/*
22450 		 * If the new window size ends up being the same as or less
22451 		 * than the old size when it is scaled, then don't force
22452 		 * a window update.
22453 		 */
22454 		if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale)
22455 			goto dontupdate;
22456 
22457 		if (adv >= (int32_t)(2 * segsiz) &&
22458 		    (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) ||
22459 		     recwin <= (int32_t)(so->so_rcv.sb_hiwat / 8) ||
22460 		     so->so_rcv.sb_hiwat <= 8 * segsiz)) {
22461 			pass = 7;
22462 			goto send;
22463 		}
22464 		if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat) {
22465 			pass = 23;
22466 			goto send;
22467 		}
22468 	}
22469 dontupdate:
22470 
22471 	/*
22472 	 * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
22473 	 * is also a catch-all for the retransmit timer timeout case.
22474 	 */
22475 	if (tp->t_flags & TF_ACKNOW) {
22476 		pass = 8;
22477 		goto send;
22478 	}
22479 	if (((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) {
22480 		pass = 9;
22481 		goto send;
22482 	}
22483 	/*
22484 	 * If our state indicates that FIN should be sent and we have not
22485 	 * yet done so, then we need to send.
22486 	 */
22487 	if ((flags & TH_FIN) &&
22488 	    (tp->snd_max == tp->snd_una)) {
22489 		pass = 11;
22490 		goto send;
22491 	}
22492 	/*
22493 	 * No reason to send a segment, just return.
22494 	 */
22495 just_return:
22496 	SOCKBUF_UNLOCK(sb);
22497 just_return_nolock:
22498 	{
22499 		int app_limited = CTF_JR_SENT_DATA;
22500 
22501 		if ((IS_FASTOPEN(tp->t_flags) == 0) &&
22502 		    (flags & TH_FIN) &&
22503 		    (len == 0) &&
22504 		    (sbused(sb) == (tp->snd_max - tp->snd_una)) &&
22505 		    ((tp->snd_max - tp->snd_una) <= segsiz)) {
22506 			/*
22507 			 * Ok less than or right at a MSS is
22508 			 * outstanding. The original FreeBSD stack would
22509 			 * have sent a FIN, which can speed things up for
22510 			 * a transactional application doing a MSG_WAITALL.
22511 			 * To speed things up since we do *not* send a FIN
22512 			 * if data is outstanding, we send a "challenge ack".
22513 			 * The idea behind that is instead of having to have
22514 			 * the peer wait for the delayed-ack timer to run off
22515 			 * we send an ack that makes the peer send us an ack.
22516 			 */
22517 			rack_send_ack_challange(rack);
22518 		}
22519 		if (tot_len_this_send > 0) {
22520 			rack->r_ctl.fsb.recwin = recwin;
22521 			slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, NULL, segsiz, __LINE__);
22522 			if ((error == 0) &&
22523 			    (rack->rc_policer_detected == 0)  &&
22524 			    rack_use_rfo &&
22525 			    ((flags & (TH_SYN|TH_FIN)) == 0) &&
22526 			    (ipoptlen == 0) &&
22527 			    (tp->rcv_numsacks == 0) &&
22528 			    rack->r_fsb_inited &&
22529 			    TCPS_HAVEESTABLISHED(tp->t_state) &&
22530 			    ((IN_RECOVERY(tp->t_flags)) == 0) &&
22531 			    (rack->r_must_retran == 0) &&
22532 			    ((tp->t_flags & TF_NEEDFIN) == 0) &&
22533 			    (len > 0) && (orig_len > 0) &&
22534 			    (orig_len > len) &&
22535 			    ((orig_len - len) >= segsiz) &&
22536 			    ((optlen == 0) ||
22537 			     ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
22538 				/* We can send at least one more MSS using our fsb */
22539 				rack_setup_fast_output(tp, rack, sb, len, orig_len,
22540 						       segsiz, pace_max_seg, hw_tls, flags);
22541 			} else
22542 				rack->r_fast_output = 0;
22543 			rack_log_fsb(rack, tp, so, flags,
22544 				     ipoptlen, orig_len, len, 0,
22545 				     1, optlen, __LINE__, 1);
22546 			/* Assure when we leave that snd_nxt will point to top */
22547 			if (SEQ_GT(tp->snd_max, tp->snd_nxt))
22548 				tp->snd_nxt = tp->snd_max;
22549 		} else {
22550 			int end_window = 0;
22551 			uint32_t seq = tp->gput_ack;
22552 
22553 			rsm = tqhash_max(rack->r_ctl.tqh);
22554 			if (rsm) {
22555 				/*
22556 				 * Mark the last sent that we just-returned (hinting
22557 				 * that delayed ack may play a role in any rtt measurement).
22558 				 */
22559 				rsm->r_just_ret = 1;
22560 			}
22561 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_JUSTRET], 1);
22562 			rack->r_ctl.rc_agg_delayed = 0;
22563 			rack->r_early = 0;
22564 			rack->r_late = 0;
22565 			rack->r_ctl.rc_agg_early = 0;
22566 			if ((ctf_outstanding(tp) +
22567 			     min(max(segsiz, (rack->r_ctl.rc_high_rwnd/2)),
22568 				 minseg)) >= tp->snd_wnd) {
22569 				/* We are limited by the rwnd */
22570 				app_limited = CTF_JR_RWND_LIMITED;
22571 				if (IN_FASTRECOVERY(tp->t_flags))
22572 					rack->r_ctl.rc_prr_sndcnt = 0;
22573 			} else if (ctf_outstanding(tp) >= sbavail(sb)) {
22574 				/* We are limited by whats available -- app limited */
22575 				app_limited = CTF_JR_APP_LIMITED;
22576 				if (IN_FASTRECOVERY(tp->t_flags))
22577 					rack->r_ctl.rc_prr_sndcnt = 0;
22578 			} else if ((idle == 0) &&
22579 				   ((tp->t_flags & TF_NODELAY) == 0) &&
22580 				   ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) &&
22581 				   (len < segsiz)) {
22582 				/*
22583 				 * No delay is not on and the
22584 				 * user is sending less than 1MSS. This
22585 				 * brings out SWS avoidance so we
22586 				 * don't send. Another app-limited case.
22587 				 */
22588 				app_limited = CTF_JR_APP_LIMITED;
22589 			} else if (tp->t_flags & TF_NOPUSH) {
22590 				/*
22591 				 * The user has requested no push of
22592 				 * the last segment and we are
22593 				 * at the last segment. Another app
22594 				 * limited case.
22595 				 */
22596 				app_limited = CTF_JR_APP_LIMITED;
22597 			} else if ((ctf_outstanding(tp) + minseg) > cwnd_to_use) {
22598 				/* Its the cwnd */
22599 				app_limited = CTF_JR_CWND_LIMITED;
22600 			} else if (IN_FASTRECOVERY(tp->t_flags) &&
22601 				   (rack->rack_no_prr == 0) &&
22602 				   (rack->r_ctl.rc_prr_sndcnt < segsiz)) {
22603 				app_limited = CTF_JR_PRR;
22604 			} else {
22605 				/* Now why here are we not sending? */
22606 #ifdef NOW
22607 #ifdef INVARIANTS
22608 				panic("rack:%p hit JR_ASSESSING case cwnd_to_use:%u?", rack, cwnd_to_use);
22609 #endif
22610 #endif
22611 				app_limited = CTF_JR_ASSESSING;
22612 			}
22613 			/*
22614 			 * App limited in some fashion, for our pacing GP
22615 			 * measurements we don't want any gap (even cwnd).
22616 			 * Close  down the measurement window.
22617 			 */
22618 			if (rack_cwnd_block_ends_measure &&
22619 			    ((app_limited == CTF_JR_CWND_LIMITED) ||
22620 			     (app_limited == CTF_JR_PRR))) {
22621 				/*
22622 				 * The reason we are not sending is
22623 				 * the cwnd (or prr). We have been configured
22624 				 * to end the measurement window in
22625 				 * this case.
22626 				 */
22627 				end_window = 1;
22628 			} else if (rack_rwnd_block_ends_measure &&
22629 				   (app_limited == CTF_JR_RWND_LIMITED)) {
22630 				/*
22631 				 * We are rwnd limited and have been
22632 				 * configured to end the measurement
22633 				 * window in this case.
22634 				 */
22635 				end_window = 1;
22636 			} else if (app_limited == CTF_JR_APP_LIMITED) {
22637 				/*
22638 				 * A true application limited period, we have
22639 				 * ran out of data.
22640 				 */
22641 				end_window = 1;
22642 			} else if (app_limited == CTF_JR_ASSESSING) {
22643 				/*
22644 				 * In the assessing case we hit the end of
22645 				 * the if/else and had no known reason
22646 				 * This will panic us under invariants..
22647 				 *
22648 				 * If we get this out in logs we need to
22649 				 * investagate which reason we missed.
22650 				 */
22651 				end_window = 1;
22652 			}
22653 			if (end_window) {
22654 				uint8_t log = 0;
22655 
22656 				/* Adjust the Gput measurement */
22657 				if ((tp->t_flags & TF_GPUTINPROG) &&
22658 				    SEQ_GT(tp->gput_ack, tp->snd_max)) {
22659 					tp->gput_ack = tp->snd_max;
22660 					if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) {
22661 						/*
22662 						 * There is not enough to measure.
22663 						 */
22664 						tp->t_flags &= ~TF_GPUTINPROG;
22665 						rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
22666 									   rack->r_ctl.rc_gp_srtt /*flex1*/,
22667 									   tp->gput_seq,
22668 									   0, 0, 18, __LINE__, NULL, 0);
22669 					} else
22670 						log = 1;
22671 				}
22672 				/* Mark the last packet has app limited */
22673 				rsm = tqhash_max(rack->r_ctl.tqh);
22674 				if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) {
22675 					if (rack->r_ctl.rc_app_limited_cnt == 0)
22676 						rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm;
22677 					else {
22678 						/*
22679 						 * Go out to the end app limited and mark
22680 						 * this new one as next and move the end_appl up
22681 						 * to this guy.
22682 						 */
22683 						if (rack->r_ctl.rc_end_appl)
22684 							rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start;
22685 						rack->r_ctl.rc_end_appl = rsm;
22686 					}
22687 					rsm->r_flags |= RACK_APP_LIMITED;
22688 					rack->r_ctl.rc_app_limited_cnt++;
22689 				}
22690 				if (log)
22691 					rack_log_pacing_delay_calc(rack,
22692 								   rack->r_ctl.rc_app_limited_cnt, seq,
22693 								   tp->gput_ack, 0, 0, 4, __LINE__, NULL, 0);
22694 			}
22695 		}
22696 		/* Check if we need to go into persists or not */
22697 		if ((tp->snd_max == tp->snd_una) &&
22698 		    TCPS_HAVEESTABLISHED(tp->t_state) &&
22699 		    sbavail(sb) &&
22700 		    (sbavail(sb) > tp->snd_wnd) &&
22701 		    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg))) {
22702 			/* Yes lets make sure to move to persist before timer-start */
22703 			rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, tp->snd_una);
22704 		}
22705 		rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, sup_rack);
22706 		rack_log_type_just_return(rack, cts, tot_len_this_send, slot, hpts_calling, app_limited, cwnd_to_use);
22707 	}
22708 just_return_clean:
22709 #ifdef NETFLIX_SHARED_CWND
22710 	if ((sbavail(sb) == 0) &&
22711 	    rack->r_ctl.rc_scw) {
22712 		tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
22713 		rack->rack_scwnd_is_idle = 1;
22714 	}
22715 #endif
22716 #ifdef TCP_ACCOUNTING
22717 	if (tot_len_this_send > 0) {
22718 		crtsc = get_cyclecount();
22719 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22720 			tp->tcp_cnt_counters[SND_OUT_DATA]++;
22721 		}
22722 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22723 			tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
22724 		}
22725 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22726 			tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) / segsiz);
22727 		}
22728 	} else {
22729 		crtsc = get_cyclecount();
22730 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22731 			tp->tcp_cnt_counters[SND_LIMITED]++;
22732 		}
22733 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22734 			tp->tcp_proc_time[SND_LIMITED] += (crtsc - ts_val);
22735 		}
22736 	}
22737 	sched_unpin();
22738 #endif
22739 	return (0);
22740 
22741 send:
22742 	if ((rack->r_ctl.crte != NULL) &&
22743 	    (rsm == NULL) &&
22744 	    ((rack->rc_hw_nobuf == 1) ||
22745 	     (rack_hw_check_queue && (check_done == 0)))) {
22746 		/*
22747 		 * We only want to do this once with the hw_check_queue,
22748 		 * for the enobuf case we would only do it once if
22749 		 * we come around to again, the flag will be clear.
22750 		 */
22751 		check_done = 1;
22752 		slot = rack_check_queue_level(rack, tp, &tv, cts, len, segsiz);
22753 		if (slot) {
22754 			rack->r_ctl.rc_agg_delayed = 0;
22755 			rack->r_ctl.rc_agg_early = 0;
22756 			rack->r_early = 0;
22757 			rack->r_late = 0;
22758 			SOCKBUF_UNLOCK(&so->so_snd);
22759 			goto skip_all_send;
22760 		}
22761 	}
22762 	if (rsm || sack_rxmit)
22763 		counter_u64_add(rack_nfto_resend, 1);
22764 	else
22765 		counter_u64_add(rack_non_fto_send, 1);
22766 	if ((flags & TH_FIN) &&
22767 	    sbavail(sb)) {
22768 		/*
22769 		 * We do not transmit a FIN
22770 		 * with data outstanding. We
22771 		 * need to make it so all data
22772 		 * is acked first.
22773 		 */
22774 		flags &= ~TH_FIN;
22775 		if ((sbused(sb) == (tp->snd_max - tp->snd_una)) &&
22776 		    ((tp->snd_max - tp->snd_una) <= segsiz)) {
22777 			/*
22778 			 * Ok less than or right at a MSS is
22779 			 * outstanding. The original FreeBSD stack would
22780 			 * have sent a FIN, which can speed things up for
22781 			 * a transactional application doing a MSG_WAITALL.
22782 			 * To speed things up since we do *not* send a FIN
22783 			 * if data is outstanding, we send a "challenge ack".
22784 			 * The idea behind that is instead of having to have
22785 			 * the peer wait for the delayed-ack timer to run off
22786 			 * we send an ack that makes the peer send us an ack.
22787 			 */
22788 			rack_send_ack_challange(rack);
22789 		}
22790 	}
22791 	/* Enforce stack imposed max seg size if we have one */
22792 	if (pace_max_seg &&
22793 	    (len > pace_max_seg)) {
22794 		mark = 1;
22795 		len = pace_max_seg;
22796 	}
22797 	if ((rsm == NULL) &&
22798 	    (rack->pcm_in_progress == 0) &&
22799 	    (rack->r_ctl.pcm_max_seg > 0) &&
22800 	    (len >= rack->r_ctl.pcm_max_seg)) {
22801 		/* It is large enough for a measurement */
22802 		add_flag |= RACK_IS_PCM;
22803 		rack_log_pcm(rack, 5, len, rack->r_ctl.pcm_max_seg,  add_flag);
22804 	} else if (rack_verbose_logging) {
22805 		rack_log_pcm(rack, 6, len, rack->r_ctl.pcm_max_seg,  add_flag);
22806 	}
22807 
22808 	SOCKBUF_LOCK_ASSERT(sb);
22809 	if (len > 0) {
22810 		if (len >= segsiz)
22811 			tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT;
22812 		else
22813 			tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT;
22814 	}
22815 	/*
22816 	 * Before ESTABLISHED, force sending of initial options unless TCP
22817 	 * set not to do any options. NOTE: we assume that the IP/TCP header
22818 	 * plus TCP options always fit in a single mbuf, leaving room for a
22819 	 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr)
22820 	 * + optlen <= MCLBYTES
22821 	 */
22822 	optlen = 0;
22823 #ifdef INET6
22824 	if (isipv6)
22825 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
22826 	else
22827 #endif
22828 		hdrlen = sizeof(struct tcpiphdr);
22829 
22830 	/*
22831 	 * Ok what seq are we sending from. If we have
22832 	 * no rsm to use, then we look at various bits,
22833 	 * if we are putting out a SYN it will be ISS.
22834 	 * If we are retransmitting a FIN it will
22835 	 * be snd_max-1 else its snd_max.
22836 	 */
22837 	if (rsm == NULL) {
22838 		if (flags & TH_SYN)
22839 			rack_seq = tp->iss;
22840 		else if ((flags & TH_FIN) &&
22841 			 (tp->t_flags & TF_SENTFIN))
22842 			rack_seq = tp->snd_max - 1;
22843 		else
22844 			rack_seq = tp->snd_max;
22845 	} else {
22846 		rack_seq = rsm->r_start;
22847 	}
22848 	/*
22849 	 * Compute options for segment. We only have to care about SYN and
22850 	 * established connection segments.  Options for SYN-ACK segments
22851 	 * are handled in TCP syncache.
22852 	 */
22853 	to.to_flags = 0;
22854 	if ((tp->t_flags & TF_NOOPT) == 0) {
22855 		/* Maximum segment size. */
22856 		if (flags & TH_SYN) {
22857 			to.to_mss = tcp_mssopt(&inp->inp_inc);
22858 			if (tp->t_port)
22859 				to.to_mss -= V_tcp_udp_tunneling_overhead;
22860 			to.to_flags |= TOF_MSS;
22861 
22862 			/*
22863 			 * On SYN or SYN|ACK transmits on TFO connections,
22864 			 * only include the TFO option if it is not a
22865 			 * retransmit, as the presence of the TFO option may
22866 			 * have caused the original SYN or SYN|ACK to have
22867 			 * been dropped by a middlebox.
22868 			 */
22869 			if (IS_FASTOPEN(tp->t_flags) &&
22870 			    (tp->t_rxtshift == 0)) {
22871 				if (tp->t_state == TCPS_SYN_RECEIVED) {
22872 					to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN;
22873 					to.to_tfo_cookie =
22874 						(u_int8_t *)&tp->t_tfo_cookie.server;
22875 					to.to_flags |= TOF_FASTOPEN;
22876 					wanted_cookie = 1;
22877 				} else if (tp->t_state == TCPS_SYN_SENT) {
22878 					to.to_tfo_len =
22879 						tp->t_tfo_client_cookie_len;
22880 					to.to_tfo_cookie =
22881 						tp->t_tfo_cookie.client;
22882 					to.to_flags |= TOF_FASTOPEN;
22883 					wanted_cookie = 1;
22884 					/*
22885 					 * If we wind up having more data to
22886 					 * send with the SYN than can fit in
22887 					 * one segment, don't send any more
22888 					 * until the SYN|ACK comes back from
22889 					 * the other end.
22890 					 */
22891 					sendalot = 0;
22892 				}
22893 			}
22894 		}
22895 		/* Window scaling. */
22896 		if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
22897 			to.to_wscale = tp->request_r_scale;
22898 			to.to_flags |= TOF_SCALE;
22899 		}
22900 		/* Timestamps. */
22901 		if ((tp->t_flags & TF_RCVD_TSTMP) ||
22902 		    ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
22903 			uint32_t ts_to_use;
22904 
22905 			if ((rack->r_rcvpath_rtt_up == 1) &&
22906 			    (ms_cts == rack->r_ctl.last_rcv_tstmp_for_rtt)) {
22907 				/*
22908 				 * When we are doing a rcv_rtt probe all
22909 				 * other timestamps use the next msec. This
22910 				 * is safe since our previous ack is in the
22911 				 * air and we will just have a few more
22912 				 * on the next ms. This assures that only
22913 				 * the one ack has the ms_cts that was on
22914 				 * our ack-probe.
22915 				 */
22916 				ts_to_use = ms_cts + 1;
22917 			} else {
22918 				ts_to_use = ms_cts;
22919 			}
22920 			to.to_tsval = ts_to_use + tp->ts_offset;
22921 			to.to_tsecr = tp->ts_recent;
22922 			to.to_flags |= TOF_TS;
22923 			if ((len == 0) &&
22924 			    (TCPS_HAVEESTABLISHED(tp->t_state)) &&
22925 			    ((ms_cts - rack->r_ctl.last_rcv_tstmp_for_rtt) > RCV_PATH_RTT_MS) &&
22926 			    (tp->snd_una == tp->snd_max) &&
22927 			    (flags & TH_ACK) &&
22928 			    (sbavail(sb) == 0) &&
22929 			    (rack->r_ctl.current_round != 0) &&
22930 			    ((flags & (TH_SYN|TH_FIN)) == 0) &&
22931 			    (rack->r_rcvpath_rtt_up == 0)) {
22932 				rack->r_ctl.last_rcv_tstmp_for_rtt = ms_cts;
22933 				rack->r_ctl.last_time_of_arm_rcv = cts;
22934 				rack->r_rcvpath_rtt_up = 1;
22935 				/* Subtract 1 from seq to force a response */
22936 				rack_seq--;
22937 			}
22938 		}
22939 		/* Set receive buffer autosizing timestamp. */
22940 		if (tp->rfbuf_ts == 0 &&
22941 		    (so->so_rcv.sb_flags & SB_AUTOSIZE)) {
22942 			tp->rfbuf_ts = ms_cts;
22943 		}
22944 		/* Selective ACK's. */
22945 		if (tp->t_flags & TF_SACK_PERMIT) {
22946 			if (flags & TH_SYN)
22947 				to.to_flags |= TOF_SACKPERM;
22948 			else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
22949 				 tp->rcv_numsacks > 0) {
22950 				to.to_flags |= TOF_SACK;
22951 				to.to_nsacks = tp->rcv_numsacks;
22952 				to.to_sacks = (u_char *)tp->sackblks;
22953 			}
22954 		}
22955 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
22956 		/* TCP-MD5 (RFC2385). */
22957 		if (tp->t_flags & TF_SIGNATURE)
22958 			to.to_flags |= TOF_SIGNATURE;
22959 #endif
22960 
22961 		/* Processing the options. */
22962 		hdrlen += optlen = tcp_addoptions(&to, opt);
22963 		/*
22964 		 * If we wanted a TFO option to be added, but it was unable
22965 		 * to fit, ensure no data is sent.
22966 		 */
22967 		if (IS_FASTOPEN(tp->t_flags) && wanted_cookie &&
22968 		    !(to.to_flags & TOF_FASTOPEN))
22969 			len = 0;
22970 	}
22971 	if (tp->t_port) {
22972 		if (V_tcp_udp_tunneling_port == 0) {
22973 			/* The port was removed?? */
22974 			SOCKBUF_UNLOCK(&so->so_snd);
22975 #ifdef TCP_ACCOUNTING
22976 			crtsc = get_cyclecount();
22977 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22978 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
22979 			}
22980 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22981 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
22982 			}
22983 			sched_unpin();
22984 #endif
22985 			return (EHOSTUNREACH);
22986 		}
22987 		hdrlen += sizeof(struct udphdr);
22988 	}
22989 #ifdef INET6
22990 	if (isipv6)
22991 		ipoptlen = ip6_optlen(inp);
22992 	else
22993 #endif
22994 		if (inp->inp_options)
22995 			ipoptlen = inp->inp_options->m_len -
22996 				offsetof(struct ipoption, ipopt_list);
22997 		else
22998 			ipoptlen = 0;
22999 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
23000 	ipoptlen += ipsec_optlen;
23001 #endif
23002 
23003 	/*
23004 	 * Adjust data length if insertion of options will bump the packet
23005 	 * length beyond the t_maxseg length. Clear the FIN bit because we
23006 	 * cut off the tail of the segment.
23007 	 */
23008 	if (len + optlen + ipoptlen > tp->t_maxseg) {
23009 		if (tso) {
23010 			uint32_t if_hw_tsomax;
23011 			uint32_t moff;
23012 			int32_t max_len;
23013 
23014 			/* extract TSO information */
23015 			if_hw_tsomax = tp->t_tsomax;
23016 			if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
23017 			if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
23018 			KASSERT(ipoptlen == 0,
23019 				("%s: TSO can't do IP options", __func__));
23020 
23021 			/*
23022 			 * Check if we should limit by maximum payload
23023 			 * length:
23024 			 */
23025 			if (if_hw_tsomax != 0) {
23026 				/* compute maximum TSO length */
23027 				max_len = (if_hw_tsomax - hdrlen -
23028 					   max_linkhdr);
23029 				if (max_len <= 0) {
23030 					len = 0;
23031 				} else if (len > max_len) {
23032 					sendalot = 1;
23033 					len = max_len;
23034 					mark = 2;
23035 				}
23036 			}
23037 			/*
23038 			 * Prevent the last segment from being fractional
23039 			 * unless the send sockbuf can be emptied:
23040 			 */
23041 			max_len = (tp->t_maxseg - optlen);
23042 			if ((sb_offset + len) < sbavail(sb)) {
23043 				moff = len % (u_int)max_len;
23044 				if (moff != 0) {
23045 					mark = 3;
23046 					len -= moff;
23047 				}
23048 			}
23049 			/*
23050 			 * In case there are too many small fragments don't
23051 			 * use TSO:
23052 			 */
23053 			if (len <= max_len) {
23054 				mark = 4;
23055 				tso = 0;
23056 			}
23057 			/*
23058 			 * Send the FIN in a separate segment after the bulk
23059 			 * sending is done. We don't trust the TSO
23060 			 * implementations to clear the FIN flag on all but
23061 			 * the last segment.
23062 			 */
23063 			if (tp->t_flags & TF_NEEDFIN) {
23064 				sendalot = 4;
23065 			}
23066 		} else {
23067 			mark = 5;
23068 			if (optlen + ipoptlen >= tp->t_maxseg) {
23069 				/*
23070 				 * Since we don't have enough space to put
23071 				 * the IP header chain and the TCP header in
23072 				 * one packet as required by RFC 7112, don't
23073 				 * send it. Also ensure that at least one
23074 				 * byte of the payload can be put into the
23075 				 * TCP segment.
23076 				 */
23077 				SOCKBUF_UNLOCK(&so->so_snd);
23078 				error = EMSGSIZE;
23079 				sack_rxmit = 0;
23080 				goto out;
23081 			}
23082 			len = tp->t_maxseg - optlen - ipoptlen;
23083 			sendalot = 5;
23084 		}
23085 	} else {
23086 		tso = 0;
23087 		mark = 6;
23088 	}
23089 	KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET,
23090 		("%s: len > IP_MAXPACKET", __func__));
23091 #ifdef DIAGNOSTIC
23092 #ifdef INET6
23093 	if (max_linkhdr + hdrlen > MCLBYTES)
23094 #else
23095 		if (max_linkhdr + hdrlen > MHLEN)
23096 #endif
23097 			panic("tcphdr too big");
23098 #endif
23099 
23100 	/*
23101 	 * This KASSERT is here to catch edge cases at a well defined place.
23102 	 * Before, those had triggered (random) panic conditions further
23103 	 * down.
23104 	 */
23105 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
23106 	if ((len == 0) &&
23107 	    (flags & TH_FIN) &&
23108 	    (sbused(sb))) {
23109 		/*
23110 		 * We have outstanding data, don't send a fin by itself!.
23111 		 *
23112 		 * Check to see if we need to send a challenge ack.
23113 		 */
23114 		if ((sbused(sb) == (tp->snd_max - tp->snd_una)) &&
23115 		    ((tp->snd_max - tp->snd_una) <= segsiz)) {
23116 			/*
23117 			 * Ok less than or right at a MSS is
23118 			 * outstanding. The original FreeBSD stack would
23119 			 * have sent a FIN, which can speed things up for
23120 			 * a transactional application doing a MSG_WAITALL.
23121 			 * To speed things up since we do *not* send a FIN
23122 			 * if data is outstanding, we send a "challenge ack".
23123 			 * The idea behind that is instead of having to have
23124 			 * the peer wait for the delayed-ack timer to run off
23125 			 * we send an ack that makes the peer send us an ack.
23126 			 */
23127 			rack_send_ack_challange(rack);
23128 		}
23129 		goto just_return;
23130 	}
23131 	/*
23132 	 * Grab a header mbuf, attaching a copy of data to be transmitted,
23133 	 * and initialize the header from the template for sends on this
23134 	 * connection.
23135 	 */
23136 	hw_tls = tp->t_nic_ktls_xmit != 0;
23137 	if (len) {
23138 		uint32_t max_val;
23139 		uint32_t moff;
23140 
23141 		if (pace_max_seg)
23142 			max_val = pace_max_seg;
23143 		else
23144 			max_val = len;
23145 		/*
23146 		 * We allow a limit on sending with hptsi.
23147 		 */
23148 		if (len > max_val) {
23149 			mark = 7;
23150 			len = max_val;
23151 		}
23152 #ifdef INET6
23153 		if (MHLEN < hdrlen + max_linkhdr)
23154 			m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
23155 		else
23156 #endif
23157 			m = m_gethdr(M_NOWAIT, MT_DATA);
23158 
23159 		if (m == NULL) {
23160 			SOCKBUF_UNLOCK(sb);
23161 			error = ENOBUFS;
23162 			sack_rxmit = 0;
23163 			goto out;
23164 		}
23165 		m->m_data += max_linkhdr;
23166 		m->m_len = hdrlen;
23167 
23168 		/*
23169 		 * Start the m_copy functions from the closest mbuf to the
23170 		 * sb_offset in the socket buffer chain.
23171 		 */
23172 		mb = sbsndptr_noadv(sb, sb_offset, &moff);
23173 		s_mb = mb;
23174 		s_moff = moff;
23175 		if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) {
23176 			m_copydata(mb, moff, (int)len,
23177 				   mtod(m, caddr_t)+hdrlen);
23178 			/*
23179 			 * If we are not retransmitting advance the
23180 			 * sndptr to help remember the next place in
23181 			 * the sb.
23182 			 */
23183 			if (rsm == NULL)
23184 				sbsndptr_adv(sb, mb, len);
23185 			m->m_len += len;
23186 		} else {
23187 			struct sockbuf *msb;
23188 
23189 			/*
23190 			 * If we are not retransmitting pass in msb so
23191 			 * the socket buffer can be advanced. Otherwise
23192 			 * set it to NULL if its a retransmission since
23193 			 * we don't want to change the sb remembered
23194 			 * location.
23195 			 */
23196 			if (rsm == NULL)
23197 				msb = sb;
23198 			else
23199 				msb = NULL;
23200 			m->m_next = tcp_m_copym(
23201 				mb, moff, &len,
23202 				if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, msb,
23203 				((rsm == NULL) ? hw_tls : 0)
23204 #ifdef NETFLIX_COPY_ARGS
23205 				, &s_mb, &s_moff
23206 #endif
23207 				);
23208 			if (len <= (tp->t_maxseg - optlen)) {
23209 				/*
23210 				 * Must have ran out of mbufs for the copy
23211 				 * shorten it to no longer need tso. Lets
23212 				 * not put on sendalot since we are low on
23213 				 * mbufs.
23214 				 */
23215 				tso = 0;
23216 			}
23217 			if (m->m_next == NULL) {
23218 				SOCKBUF_UNLOCK(sb);
23219 				(void)m_free(m);
23220 				error = ENOBUFS;
23221 				sack_rxmit = 0;
23222 				goto out;
23223 			}
23224 		}
23225 		if (sack_rxmit) {
23226 			if (rsm && (rsm->r_flags & RACK_TLP)) {
23227 				/*
23228 				 * TLP should not count in retran count, but
23229 				 * in its own bin
23230 				 */
23231 				counter_u64_add(rack_tlp_retran, 1);
23232 				counter_u64_add(rack_tlp_retran_bytes, len);
23233 			} else {
23234 				tp->t_sndrexmitpack++;
23235 				KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
23236 				KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
23237 			}
23238 #ifdef STATS
23239 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
23240 						 len);
23241 #endif
23242 		} else {
23243 			KMOD_TCPSTAT_INC(tcps_sndpack);
23244 			KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
23245 #ifdef STATS
23246 			stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
23247 						 len);
23248 #endif
23249 		}
23250 		/*
23251 		 * If we're sending everything we've got, set PUSH. (This
23252 		 * will keep happy those implementations which only give
23253 		 * data to the user when a buffer fills or a PUSH comes in.)
23254 		 */
23255 		if (sb_offset + len == sbused(sb) &&
23256 		    sbused(sb) &&
23257 		    !(flags & TH_SYN)) {
23258 			flags |= TH_PUSH;
23259 			add_flag |= RACK_HAD_PUSH;
23260 		}
23261 
23262 		SOCKBUF_UNLOCK(sb);
23263 	} else {
23264 		SOCKBUF_UNLOCK(sb);
23265 		if (tp->t_flags & TF_ACKNOW)
23266 			KMOD_TCPSTAT_INC(tcps_sndacks);
23267 		else if (flags & (TH_SYN | TH_FIN | TH_RST))
23268 			KMOD_TCPSTAT_INC(tcps_sndctrl);
23269 		else
23270 			KMOD_TCPSTAT_INC(tcps_sndwinup);
23271 
23272 		m = m_gethdr(M_NOWAIT, MT_DATA);
23273 		if (m == NULL) {
23274 			error = ENOBUFS;
23275 			sack_rxmit = 0;
23276 			goto out;
23277 		}
23278 #ifdef INET6
23279 		if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
23280 		    MHLEN >= hdrlen) {
23281 			M_ALIGN(m, hdrlen);
23282 		} else
23283 #endif
23284 			m->m_data += max_linkhdr;
23285 		m->m_len = hdrlen;
23286 	}
23287 	SOCKBUF_UNLOCK_ASSERT(sb);
23288 	m->m_pkthdr.rcvif = (struct ifnet *)0;
23289 #ifdef MAC
23290 	mac_inpcb_create_mbuf(inp, m);
23291 #endif
23292 	if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) &&  rack->r_fsb_inited) {
23293 #ifdef INET6
23294 		if (isipv6)
23295 			ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
23296 		else
23297 #endif				/* INET6 */
23298 #ifdef INET
23299 			ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
23300 #endif
23301 		th = rack->r_ctl.fsb.th;
23302 		udp = rack->r_ctl.fsb.udp;
23303 		if (udp) {
23304 #ifdef INET6
23305 			if (isipv6)
23306 				ulen = hdrlen + len - sizeof(struct ip6_hdr);
23307 			else
23308 #endif				/* INET6 */
23309 				ulen = hdrlen + len - sizeof(struct ip);
23310 			udp->uh_ulen = htons(ulen);
23311 		}
23312 	} else {
23313 #ifdef INET6
23314 		if (isipv6) {
23315 			ip6 = mtod(m, struct ip6_hdr *);
23316 			if (tp->t_port) {
23317 				udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
23318 				udp->uh_sport = htons(V_tcp_udp_tunneling_port);
23319 				udp->uh_dport = tp->t_port;
23320 				ulen = hdrlen + len - sizeof(struct ip6_hdr);
23321 				udp->uh_ulen = htons(ulen);
23322 				th = (struct tcphdr *)(udp + 1);
23323 			} else
23324 				th = (struct tcphdr *)(ip6 + 1);
23325 			tcpip_fillheaders(inp, tp->t_port, ip6, th);
23326 		} else
23327 #endif				/* INET6 */
23328 		{
23329 #ifdef INET
23330 			ip = mtod(m, struct ip *);
23331 			if (tp->t_port) {
23332 				udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
23333 				udp->uh_sport = htons(V_tcp_udp_tunneling_port);
23334 				udp->uh_dport = tp->t_port;
23335 				ulen = hdrlen + len - sizeof(struct ip);
23336 				udp->uh_ulen = htons(ulen);
23337 				th = (struct tcphdr *)(udp + 1);
23338 			} else
23339 				th = (struct tcphdr *)(ip + 1);
23340 			tcpip_fillheaders(inp, tp->t_port, ip, th);
23341 #endif
23342 		}
23343 	}
23344 	/*
23345 	 * If we are starting a connection, send ECN setup SYN packet. If we
23346 	 * are on a retransmit, we may resend those bits a number of times
23347 	 * as per RFC 3168.
23348 	 */
23349 	if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn) {
23350 		flags |= tcp_ecn_output_syn_sent(tp);
23351 	}
23352 	/* Also handle parallel SYN for ECN */
23353 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
23354 	    (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) {
23355 		int ect = tcp_ecn_output_established(tp, &flags, len, sack_rxmit);
23356 		if ((tp->t_state == TCPS_SYN_RECEIVED) &&
23357 		    (tp->t_flags2 & TF2_ECN_SND_ECE))
23358 			tp->t_flags2 &= ~TF2_ECN_SND_ECE;
23359 #ifdef INET6
23360 		if (isipv6) {
23361 			ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
23362 			ip6->ip6_flow |= htonl(ect << 20);
23363 		}
23364 		else
23365 #endif
23366 		{
23367 #ifdef INET
23368 			ip->ip_tos &= ~IPTOS_ECN_MASK;
23369 			ip->ip_tos |= ect;
23370 #endif
23371 		}
23372 	}
23373 	th->th_seq = htonl(rack_seq);
23374 	th->th_ack = htonl(tp->rcv_nxt);
23375 	tcp_set_flags(th, flags);
23376 	/*
23377 	 * Calculate receive window.  Don't shrink window, but avoid silly
23378 	 * window syndrome.
23379 	 * If a RST segment is sent, advertise a window of zero.
23380 	 */
23381 	if (flags & TH_RST) {
23382 		recwin = 0;
23383 	} else {
23384 		if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
23385 		    recwin < (long)segsiz) {
23386 			recwin = 0;
23387 		}
23388 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) &&
23389 		    recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
23390 			recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
23391 	}
23392 
23393 	/*
23394 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or
23395 	 * <SYN,ACK>) segment itself is never scaled.  The <SYN,ACK> case is
23396 	 * handled in syncache.
23397 	 */
23398 	if (flags & TH_SYN)
23399 		th->th_win = htons((u_short)
23400 				   (min(sbspace(&so->so_rcv), TCP_MAXWIN)));
23401 	else {
23402 		/* Avoid shrinking window with window scaling. */
23403 		recwin = roundup2(recwin, 1 << tp->rcv_scale);
23404 		th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
23405 	}
23406 	/*
23407 	 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0
23408 	 * window.  This may cause the remote transmitter to stall.  This
23409 	 * flag tells soreceive() to disable delayed acknowledgements when
23410 	 * draining the buffer.  This can occur if the receiver is
23411 	 * attempting to read more data than can be buffered prior to
23412 	 * transmitting on the connection.
23413 	 */
23414 	if (th->th_win == 0) {
23415 		tp->t_sndzerowin++;
23416 		tp->t_flags |= TF_RXWIN0SENT;
23417 	} else
23418 		tp->t_flags &= ~TF_RXWIN0SENT;
23419 	tp->snd_up = tp->snd_una;	/* drag it along, its deprecated */
23420 	/* Now are we using fsb?, if so copy the template data to the mbuf */
23421 	if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) && rack->r_fsb_inited) {
23422 		uint8_t *cpto;
23423 
23424 		cpto = mtod(m, uint8_t *);
23425 		memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
23426 		/*
23427 		 * We have just copied in:
23428 		 * IP/IP6
23429 		 * <optional udphdr>
23430 		 * tcphdr (no options)
23431 		 *
23432 		 * We need to grab the correct pointers into the mbuf
23433 		 * for both the tcp header, and possibly the udp header (if tunneling).
23434 		 * We do this by using the offset in the copy buffer and adding it
23435 		 * to the mbuf base pointer (cpto).
23436 		 */
23437 #ifdef INET6
23438 		if (isipv6)
23439 			ip6 = mtod(m, struct ip6_hdr *);
23440 		else
23441 #endif				/* INET6 */
23442 #ifdef INET
23443 			ip = mtod(m, struct ip *);
23444 #endif
23445 		th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
23446 		/* If we have a udp header lets set it into the mbuf as well */
23447 		if (udp)
23448 			udp = (struct udphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.udp - rack->r_ctl.fsb.tcp_ip_hdr));
23449 	}
23450 	if (optlen) {
23451 		bcopy(opt, th + 1, optlen);
23452 		th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
23453 	}
23454 	/*
23455 	 * Put TCP length in extended header, and then checksum extended
23456 	 * header and data.
23457 	 */
23458 	m->m_pkthdr.len = hdrlen + len;	/* in6_cksum() need this */
23459 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
23460 	if (to.to_flags & TOF_SIGNATURE) {
23461 		/*
23462 		 * Calculate MD5 signature and put it into the place
23463 		 * determined before.
23464 		 * NOTE: since TCP options buffer doesn't point into
23465 		 * mbuf's data, calculate offset and use it.
23466 		 */
23467 		if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
23468 						       (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
23469 			/*
23470 			 * Do not send segment if the calculation of MD5
23471 			 * digest has failed.
23472 			 */
23473 			goto out;
23474 		}
23475 	}
23476 #endif
23477 #ifdef INET6
23478 	if (isipv6) {
23479 		/*
23480 		 * ip6_plen is not need to be filled now, and will be filled
23481 		 * in ip6_output.
23482 		 */
23483 		if (tp->t_port) {
23484 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
23485 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
23486 			udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
23487 			th->th_sum = htons(0);
23488 			UDPSTAT_INC(udps_opackets);
23489 		} else {
23490 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
23491 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
23492 			th->th_sum = in6_cksum_pseudo(ip6,
23493 						      sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
23494 						      0);
23495 		}
23496 	}
23497 #endif
23498 #if defined(INET6) && defined(INET)
23499 	else
23500 #endif
23501 #ifdef INET
23502 	{
23503 		if (tp->t_port) {
23504 			m->m_pkthdr.csum_flags = CSUM_UDP;
23505 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
23506 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
23507 						ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
23508 			th->th_sum = htons(0);
23509 			UDPSTAT_INC(udps_opackets);
23510 		} else {
23511 			m->m_pkthdr.csum_flags = CSUM_TCP;
23512 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
23513 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
23514 					       ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
23515 									IPPROTO_TCP + len + optlen));
23516 		}
23517 		/* IP version must be set here for ipv4/ipv6 checking later */
23518 		KASSERT(ip->ip_v == IPVERSION,
23519 			("%s: IP version incorrect: %d", __func__, ip->ip_v));
23520 	}
23521 #endif
23522 	/*
23523 	 * Enable TSO and specify the size of the segments. The TCP pseudo
23524 	 * header checksum is always provided. XXX: Fixme: This is currently
23525 	 * not the case for IPv6.
23526 	 */
23527 	if (tso) {
23528 		/*
23529 		 * Here we must use t_maxseg and the optlen since
23530 		 * the optlen may include SACK's (or DSACK).
23531 		 */
23532 		KASSERT(len > tp->t_maxseg - optlen,
23533 			("%s: len <= tso_segsz", __func__));
23534 		m->m_pkthdr.csum_flags |= CSUM_TSO;
23535 		m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen;
23536 	}
23537 	KASSERT(len + hdrlen == m_length(m, NULL),
23538 		("%s: mbuf chain different than expected: %d + %u != %u",
23539 		 __func__, len, hdrlen, m_length(m, NULL)));
23540 
23541 #ifdef TCP_HHOOK
23542 	/* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */
23543 	hhook_run_tcp_est_out(tp, th, &to, len, tso);
23544 #endif
23545 	if ((rack->r_ctl.crte != NULL) &&
23546 	    (rack->rc_hw_nobuf == 0) &&
23547 	    tcp_bblogging_on(tp)) {
23548 		rack_log_queue_level(tp, rack, len, &tv, cts);
23549 	}
23550 	/* We're getting ready to send; log now. */
23551 	if (tcp_bblogging_on(rack->rc_tp)) {
23552 		union tcp_log_stackspecific log;
23553 
23554 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
23555 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
23556 		if (rack->rack_no_prr)
23557 			log.u_bbr.flex1 = 0;
23558 		else
23559 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
23560 		log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
23561 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
23562 		log.u_bbr.flex4 = orig_len;
23563 		/* Save off the early/late values */
23564 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
23565 		log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
23566 		log.u_bbr.bw_inuse = rack_get_bw(rack);
23567 		log.u_bbr.cur_del_rate = rack->r_ctl.gp_bw;
23568 		log.u_bbr.flex8 = 0;
23569 		if (rsm) {
23570 			if (rsm->r_flags & RACK_RWND_COLLAPSED) {
23571 				rack_log_collapse(rack, rsm->r_start, rsm->r_end, 0, __LINE__, 5, rsm->r_flags, rsm);
23572 				counter_u64_add(rack_collapsed_win_rxt, 1);
23573 				counter_u64_add(rack_collapsed_win_rxt_bytes, (rsm->r_end - rsm->r_start));
23574 			}
23575 			if (doing_tlp)
23576 				log.u_bbr.flex8 = 2;
23577 			else
23578 				log.u_bbr.flex8 = 1;
23579 		} else {
23580 			if (doing_tlp)
23581 				log.u_bbr.flex8 = 3;
23582 		}
23583 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm);
23584 		log.u_bbr.flex7 = mark;
23585 		log.u_bbr.flex7 <<= 8;
23586 		log.u_bbr.flex7 |= pass;
23587 		log.u_bbr.pkts_out = tp->t_maxseg;
23588 		log.u_bbr.timeStamp = cts;
23589 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
23590 		if (rsm && (rsm->r_rtr_cnt > 0)) {
23591 			/*
23592 			 * When we have a retransmit we want to log the
23593 			 * burst at send and flight at send from before.
23594 			 */
23595 			log.u_bbr.flex5 = rsm->r_fas;
23596 			log.u_bbr.bbr_substate = rsm->r_bas;
23597 		} else {
23598 			/*
23599 			 * New transmits we log in flex5 the inflight again as
23600 			 * well as the number of segments in our send in the
23601 			 * substate field.
23602 			 */
23603 			log.u_bbr.flex5 = log.u_bbr.inflight;
23604 			log.u_bbr.bbr_substate = (uint8_t)((len + segsiz - 1)/segsiz);
23605 		}
23606 		log.u_bbr.lt_epoch = cwnd_to_use;
23607 		log.u_bbr.delivered = sendalot;
23608 		log.u_bbr.rttProp = (uint64_t)rsm;
23609 		log.u_bbr.pkt_epoch = __LINE__;
23610 		if (rsm) {
23611 			log.u_bbr.delRate = rsm->r_flags;
23612 			log.u_bbr.delRate <<= 31;
23613 			log.u_bbr.delRate |= rack->r_must_retran;
23614 			log.u_bbr.delRate <<= 1;
23615 			log.u_bbr.delRate |= (sack_rxmit & 0x00000001);
23616 		} else {
23617 			log.u_bbr.delRate = rack->r_must_retran;
23618 			log.u_bbr.delRate <<= 1;
23619 			log.u_bbr.delRate |= (sack_rxmit & 0x00000001);
23620 		}
23621 		lgb = tcp_log_event(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK,
23622 				    len, &log, false, NULL, __func__, __LINE__, &tv);
23623 	} else
23624 		lgb = NULL;
23625 
23626 	/*
23627 	 * Fill in IP length and desired time to live and send to IP level.
23628 	 * There should be a better way to handle ttl and tos; we could keep
23629 	 * them in the template, but need a way to checksum without them.
23630 	 */
23631 	/*
23632 	 * m->m_pkthdr.len should have been set before cksum calcuration,
23633 	 * because in6_cksum() need it.
23634 	 */
23635 #ifdef INET6
23636 	if (isipv6) {
23637 		/*
23638 		 * we separately set hoplimit for every segment, since the
23639 		 * user might want to change the value via setsockopt. Also,
23640 		 * desired default hop limit might be changed via Neighbor
23641 		 * Discovery.
23642 		 */
23643 		rack->r_ctl.fsb.hoplimit = ip6->ip6_hlim = in6_selecthlim(inp, NULL);
23644 
23645 		/*
23646 		 * Set the packet size here for the benefit of DTrace
23647 		 * probes. ip6_output() will set it properly; it's supposed
23648 		 * to include the option header lengths as well.
23649 		 */
23650 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
23651 
23652 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
23653 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
23654 		else
23655 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
23656 
23657 		if (tp->t_state == TCPS_SYN_SENT)
23658 			TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th);
23659 
23660 		TCP_PROBE5(send, NULL, tp, ip6, tp, th);
23661 		/* TODO: IPv6 IP6TOS_ECT bit on */
23662 		error = ip6_output(m,
23663 				   inp->in6p_outputopts,
23664 				   &inp->inp_route6,
23665 				   ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0),
23666 				   NULL, NULL, inp);
23667 
23668 		if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL)
23669 			mtu = inp->inp_route6.ro_nh->nh_mtu;
23670 	}
23671 #endif				/* INET6 */
23672 #if defined(INET) && defined(INET6)
23673 	else
23674 #endif
23675 #ifdef INET
23676 	{
23677 		ip->ip_len = htons(m->m_pkthdr.len);
23678 #ifdef INET6
23679 		if (inp->inp_vflag & INP_IPV6PROTO)
23680 			ip->ip_ttl = in6_selecthlim(inp, NULL);
23681 #endif				/* INET6 */
23682 		rack->r_ctl.fsb.hoplimit = ip->ip_ttl;
23683 		/*
23684 		 * If we do path MTU discovery, then we set DF on every
23685 		 * packet. This might not be the best thing to do according
23686 		 * to RFC3390 Section 2. However the tcp hostcache migitates
23687 		 * the problem so it affects only the first tcp connection
23688 		 * with a host.
23689 		 *
23690 		 * NB: Don't set DF on small MTU/MSS to have a safe
23691 		 * fallback.
23692 		 */
23693 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
23694 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
23695 			if (tp->t_port == 0 || len < V_tcp_minmss) {
23696 				ip->ip_off |= htons(IP_DF);
23697 			}
23698 		} else {
23699 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
23700 		}
23701 
23702 		if (tp->t_state == TCPS_SYN_SENT)
23703 			TCP_PROBE5(connect__request, NULL, tp, ip, tp, th);
23704 
23705 		TCP_PROBE5(send, NULL, tp, ip, tp, th);
23706 
23707 		error = ip_output(m,
23708 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
23709 				  inp->inp_options,
23710 #else
23711 				  NULL,
23712 #endif
23713 				  &inp->inp_route,
23714 				  ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0,
23715 				  inp);
23716 		if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL)
23717 			mtu = inp->inp_route.ro_nh->nh_mtu;
23718 	}
23719 #endif				/* INET */
23720 
23721 out:
23722 	if (lgb) {
23723 		lgb->tlb_errno = error;
23724 		lgb = NULL;
23725 	}
23726 	/*
23727 	 * In transmit state, time the transmission and arrange for the
23728 	 * retransmit.  In persist state, just set snd_max.
23729 	 */
23730 	rack_log_output(tp, &to, len, rack_seq, (uint8_t) flags, error,
23731 			rack_to_usec_ts(&tv),
23732 			rsm, add_flag, s_mb, s_moff, hw_tls, segsiz);
23733 	if (error == 0) {
23734 		if (add_flag & RACK_IS_PCM) {
23735 			/* We just launched a PCM */
23736 			/* rrs here log */
23737 			rack->pcm_in_progress = 1;
23738 			rack->pcm_needed = 0;
23739 			rack_log_pcm(rack, 7, len, rack->r_ctl.pcm_max_seg,  add_flag);
23740 		}
23741 		if (rsm == NULL) {
23742 			if (rack->lt_bw_up == 0) {
23743 				rack->r_ctl.lt_timemark = tcp_tv_to_lusectick(&tv);
23744 				rack->r_ctl.lt_seq = tp->snd_una;
23745 				rack->lt_bw_up = 1;
23746 			} else if (((rack_seq + len) - rack->r_ctl.lt_seq) > 0x7fffffff) {
23747 				/*
23748 				 * Need to record what we have since we are
23749 				 * approaching seq wrap.
23750 				 */
23751 				uint64_t tmark;
23752 
23753 				rack->r_ctl.lt_bw_bytes += (tp->snd_una - rack->r_ctl.lt_seq);
23754 				rack->r_ctl.lt_seq = tp->snd_una;
23755 				tmark = tcp_get_u64_usecs(&tv);
23756 				if (tmark > rack->r_ctl.lt_timemark) {
23757 					rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark);
23758 					rack->r_ctl.lt_timemark = tmark;
23759 				}
23760 			}
23761 		}
23762 		rack->forced_ack = 0;	/* If we send something zap the FA flag */
23763 		counter_u64_add(rack_total_bytes, len);
23764 		tcp_account_for_send(tp, len, (rsm != NULL), doing_tlp, hw_tls);
23765 		if (rsm && doing_tlp) {
23766 			rack->rc_last_sent_tlp_past_cumack = 0;
23767 			rack->rc_last_sent_tlp_seq_valid = 1;
23768 			rack->r_ctl.last_sent_tlp_seq = rsm->r_start;
23769 			rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start;
23770 		}
23771 		if (rack->rc_hw_nobuf) {
23772 			rack->rc_hw_nobuf = 0;
23773 			rack->r_ctl.rc_agg_delayed = 0;
23774 			rack->r_early = 0;
23775 			rack->r_late = 0;
23776 			rack->r_ctl.rc_agg_early = 0;
23777 		}
23778 		if (rsm && (doing_tlp == 0)) {
23779 			/* Set we retransmitted */
23780 			rack->rc_gp_saw_rec = 1;
23781 		} else {
23782 			if (cwnd_to_use > tp->snd_ssthresh) {
23783 				/* Set we sent in CA */
23784 				rack->rc_gp_saw_ca = 1;
23785 			} else {
23786 				/* Set we sent in SS */
23787 				rack->rc_gp_saw_ss = 1;
23788 			}
23789 		}
23790 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
23791 		    (tp->t_flags & TF_SACK_PERMIT) &&
23792 		    tp->rcv_numsacks > 0)
23793 			tcp_clean_dsack_blocks(tp);
23794 		tot_len_this_send += len;
23795 		if (len == 0) {
23796 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_SNDACK], 1);
23797 		} else {
23798 			int idx;
23799 
23800 			idx = (len / segsiz) + 3;
23801 			if (idx >= TCP_MSS_ACCT_ATIMER)
23802 				counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
23803 			else
23804 				counter_u64_add(rack_out_size[idx], 1);
23805 		}
23806 	}
23807 	if ((rack->rack_no_prr == 0) &&
23808 	    sub_from_prr &&
23809 	    (error == 0)) {
23810 		if (rack->r_ctl.rc_prr_sndcnt >= len)
23811 			rack->r_ctl.rc_prr_sndcnt -= len;
23812 		else
23813 			rack->r_ctl.rc_prr_sndcnt = 0;
23814 	}
23815 	sub_from_prr = 0;
23816 	if (doing_tlp) {
23817 		/* Make sure the TLP is added */
23818 		add_flag |= RACK_TLP;
23819 	} else if (rsm) {
23820 		/* If its a resend without TLP then it must not have the flag */
23821 		rsm->r_flags &= ~RACK_TLP;
23822 	}
23823 
23824 
23825 	if ((error == 0) &&
23826 	    (len > 0) &&
23827 	    (tp->snd_una == tp->snd_max))
23828 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
23829 
23830 	{
23831 		/*
23832 		 * This block is not associated with the above error == 0 test.
23833 		 * It is used to advance snd_max if we have a new transmit.
23834 		 */
23835 		tcp_seq startseq = tp->snd_max;
23836 
23837 
23838 		if (rsm && (doing_tlp == 0))
23839 			rack->r_ctl.rc_loss_count += rsm->r_end - rsm->r_start;
23840 		if (error)
23841 			/* We don't log or do anything with errors */
23842 			goto nomore;
23843 		if (doing_tlp == 0) {
23844 			if (rsm == NULL) {
23845 				/*
23846 				 * Not a retransmission of some
23847 				 * sort, new data is going out so
23848 				 * clear our TLP count and flag.
23849 				 */
23850 				rack->rc_tlp_in_progress = 0;
23851 				rack->r_ctl.rc_tlp_cnt_out = 0;
23852 			}
23853 		} else {
23854 			/*
23855 			 * We have just sent a TLP, mark that it is true
23856 			 * and make sure our in progress is set so we
23857 			 * continue to check the count.
23858 			 */
23859 			rack->rc_tlp_in_progress = 1;
23860 			rack->r_ctl.rc_tlp_cnt_out++;
23861 		}
23862 		/*
23863 		 * If we are retransmitting we are done, snd_max
23864 		 * does not get updated.
23865 		 */
23866 		if (sack_rxmit)
23867 			goto nomore;
23868 		if ((tp->snd_una == tp->snd_max) && (len > 0)) {
23869 			/*
23870 			 * Update the time we just added data since
23871 			 * nothing was outstanding.
23872 			 */
23873 			rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
23874 			tp->t_acktime = ticks;
23875 		}
23876 		/*
23877 		 * Now for special SYN/FIN handling.
23878 		 */
23879 		if (flags & (TH_SYN | TH_FIN)) {
23880 			if ((flags & TH_SYN) &&
23881 			    ((tp->t_flags & TF_SENTSYN) == 0)) {
23882 				tp->snd_max++;
23883 				tp->t_flags |= TF_SENTSYN;
23884 			}
23885 			if ((flags & TH_FIN) &&
23886 			    ((tp->t_flags & TF_SENTFIN) == 0)) {
23887 				tp->snd_max++;
23888 				tp->t_flags |= TF_SENTFIN;
23889 			}
23890 		}
23891 		tp->snd_max += len;
23892 		if (rack->rc_new_rnd_needed) {
23893 			rack_new_round_starts(tp, rack, tp->snd_max);
23894 		}
23895 		/*
23896 		 * Time this transmission if not a retransmission and
23897 		 * not currently timing anything.
23898 		 * This is only relevant in case of switching back to
23899 		 * the base stack.
23900 		 */
23901 		if (tp->t_rtttime == 0) {
23902 			tp->t_rtttime = ticks;
23903 			tp->t_rtseq = startseq;
23904 			KMOD_TCPSTAT_INC(tcps_segstimed);
23905 		}
23906 		if (len &&
23907 		    ((tp->t_flags & TF_GPUTINPROG) == 0))
23908 			rack_start_gp_measurement(tp, rack, startseq, sb_offset);
23909 		/*
23910 		 * If we are doing FO we need to update the mbuf position and subtract
23911 		 * this happens when the peer sends us duplicate information and
23912 		 * we thus want to send a DSACK.
23913 		 *
23914 		 * XXXRRS: This brings to mind a ?, when we send a DSACK block is TSO
23915 		 * turned off? If not then we are going to echo multiple DSACK blocks
23916 		 * out (with the TSO), which we should not be doing.
23917 		 */
23918 		if (rack->r_fast_output && len) {
23919 			if (rack->r_ctl.fsb.left_to_send > len)
23920 				rack->r_ctl.fsb.left_to_send -= len;
23921 			else
23922 				rack->r_ctl.fsb.left_to_send = 0;
23923 			if (rack->r_ctl.fsb.left_to_send < segsiz)
23924 				rack->r_fast_output = 0;
23925 			if (rack->r_fast_output) {
23926 				rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
23927 				rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
23928 				rack->r_ctl.fsb.o_t_len = M_TRAILINGROOM(rack->r_ctl.fsb.m);
23929 			}
23930 		}
23931 		if (rack_pcm_blast == 0) {
23932 			if ((orig_len > len) &&
23933 			    (add_flag & RACK_IS_PCM) &&
23934 			    (len < pace_max_seg) &&
23935 			    ((pace_max_seg - len) > segsiz)) {
23936 				/*
23937 				 * We are doing a PCM measurement and we did
23938 				 * not get enough data in the TSO to meet the
23939 				 * burst requirement.
23940 				 */
23941 				uint32_t n_len;
23942 
23943 				n_len = (orig_len - len);
23944 				orig_len -= len;
23945 				pace_max_seg -= len;
23946 				len = n_len;
23947 				sb_offset = tp->snd_max - tp->snd_una;
23948 				/* Re-lock for the next spin */
23949 				SOCKBUF_LOCK(sb);
23950 				goto send;
23951 			}
23952 		} else {
23953 			if ((orig_len > len) &&
23954 			    (add_flag & RACK_IS_PCM) &&
23955 			    ((orig_len - len) > segsiz)) {
23956 				/*
23957 				 * We are doing a PCM measurement and we did
23958 				 * not get enough data in the TSO to meet the
23959 				 * burst requirement.
23960 				 */
23961 				uint32_t n_len;
23962 
23963 				n_len = (orig_len - len);
23964 				orig_len -= len;
23965 				len = n_len;
23966 				sb_offset = tp->snd_max - tp->snd_una;
23967 				/* Re-lock for the next spin */
23968 				SOCKBUF_LOCK(sb);
23969 				goto send;
23970 			}
23971 		}
23972 	}
23973 nomore:
23974 	if (error) {
23975 		rack->r_ctl.rc_agg_delayed = 0;
23976 		rack->r_early = 0;
23977 		rack->r_late = 0;
23978 		rack->r_ctl.rc_agg_early = 0;
23979 		SOCKBUF_UNLOCK_ASSERT(sb);	/* Check gotos. */
23980 		/*
23981 		 * Failures do not advance the seq counter above. For the
23982 		 * case of ENOBUFS we will fall out and retry in 1ms with
23983 		 * the hpts. Everything else will just have to retransmit
23984 		 * with the timer.
23985 		 *
23986 		 * In any case, we do not want to loop around for another
23987 		 * send without a good reason.
23988 		 */
23989 		sendalot = 0;
23990 		switch (error) {
23991 		case EPERM:
23992 		case EACCES:
23993 			tp->t_softerror = error;
23994 #ifdef TCP_ACCOUNTING
23995 			crtsc = get_cyclecount();
23996 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
23997 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
23998 			}
23999 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
24000 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
24001 			}
24002 			sched_unpin();
24003 #endif
24004 			return (error);
24005 		case ENOBUFS:
24006 			/*
24007 			 * Pace us right away to retry in a some
24008 			 * time
24009 			 */
24010 			if (rack->r_ctl.crte != NULL) {
24011 				tcp_trace_point(rack->rc_tp, TCP_TP_HWENOBUF);
24012 				if (tcp_bblogging_on(rack->rc_tp))
24013 					rack_log_queue_level(tp, rack, len, &tv, cts);
24014 			} else
24015 				tcp_trace_point(rack->rc_tp, TCP_TP_ENOBUF);
24016 			slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC);
24017 			if (rack->rc_enobuf < 0x7f)
24018 				rack->rc_enobuf++;
24019 			if (slot < (10 * HPTS_USEC_IN_MSEC))
24020 				slot = 10 * HPTS_USEC_IN_MSEC;
24021 			if (rack->r_ctl.crte != NULL) {
24022 				counter_u64_add(rack_saw_enobuf_hw, 1);
24023 				tcp_rl_log_enobuf(rack->r_ctl.crte);
24024 			}
24025 			counter_u64_add(rack_saw_enobuf, 1);
24026 			goto enobufs;
24027 		case EMSGSIZE:
24028 			/*
24029 			 * For some reason the interface we used initially
24030 			 * to send segments changed to another or lowered
24031 			 * its MTU. If TSO was active we either got an
24032 			 * interface without TSO capabilits or TSO was
24033 			 * turned off. If we obtained mtu from ip_output()
24034 			 * then update it and try again.
24035 			 */
24036 			if (tso)
24037 				tp->t_flags &= ~TF_TSO;
24038 			if (mtu != 0) {
24039 				int saved_mtu;
24040 
24041 				saved_mtu = tp->t_maxseg;
24042 				tcp_mss_update(tp, -1, mtu, NULL, NULL);
24043 				if (saved_mtu > tp->t_maxseg) {
24044 					goto again;
24045 				}
24046 			}
24047 			slot = 10 * HPTS_USEC_IN_MSEC;
24048 			rack_start_hpts_timer(rack, tp, cts, slot, 0, 0);
24049 #ifdef TCP_ACCOUNTING
24050 			crtsc = get_cyclecount();
24051 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
24052 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
24053 			}
24054 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
24055 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
24056 			}
24057 			sched_unpin();
24058 #endif
24059 			return (error);
24060 		case ENETUNREACH:
24061 			counter_u64_add(rack_saw_enetunreach, 1);
24062 		case EHOSTDOWN:
24063 		case EHOSTUNREACH:
24064 		case ENETDOWN:
24065 			if (TCPS_HAVERCVDSYN(tp->t_state)) {
24066 				tp->t_softerror = error;
24067 			}
24068 			/* FALLTHROUGH */
24069 		default:
24070 			slot = 10 * HPTS_USEC_IN_MSEC;
24071 			rack_start_hpts_timer(rack, tp, cts, slot, 0, 0);
24072 #ifdef TCP_ACCOUNTING
24073 			crtsc = get_cyclecount();
24074 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
24075 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
24076 			}
24077 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
24078 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
24079 			}
24080 			sched_unpin();
24081 #endif
24082 			return (error);
24083 		}
24084 	} else {
24085 		rack->rc_enobuf = 0;
24086 		if (IN_FASTRECOVERY(tp->t_flags) && rsm)
24087 			rack->r_ctl.retran_during_recovery += len;
24088 	}
24089 	KMOD_TCPSTAT_INC(tcps_sndtotal);
24090 
24091 	/*
24092 	 * Data sent (as far as we can tell). If this advertises a larger
24093 	 * window than any other segment, then remember the size of the
24094 	 * advertised window. Any pending ACK has now been sent.
24095 	 */
24096 	if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
24097 		tp->rcv_adv = tp->rcv_nxt + recwin;
24098 
24099 	tp->last_ack_sent = tp->rcv_nxt;
24100 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
24101 enobufs:
24102 	if (sendalot) {
24103 		/* Do we need to turn off sendalot? */
24104 		if (pace_max_seg &&
24105 		    (tot_len_this_send >= pace_max_seg)) {
24106 			/* We hit our max. */
24107 			sendalot = 0;
24108 		}
24109 	}
24110 	if ((error == 0) && (flags & TH_FIN))
24111 		tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN);
24112 	if (flags & TH_RST) {
24113 		/*
24114 		 * We don't send again after sending a RST.
24115 		 */
24116 		slot = 0;
24117 		sendalot = 0;
24118 		if (error == 0)
24119 			tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
24120 	} else if ((slot == 0) && (sendalot == 0) && tot_len_this_send) {
24121 		/*
24122 		 * Get our pacing rate, if an error
24123 		 * occurred in sending (ENOBUF) we would
24124 		 * hit the else if with slot preset. Other
24125 		 * errors return.
24126 		 */
24127 		slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, rsm, segsiz, __LINE__);
24128 	}
24129 	/* We have sent clear the flag */
24130 	rack->r_ent_rec_ns = 0;
24131 	if (rack->r_must_retran) {
24132 		if (rsm) {
24133 			rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start);
24134 			if (SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) {
24135 				/*
24136 				 * We have retransmitted all.
24137 				 */
24138 				rack->r_must_retran = 0;
24139 				rack->r_ctl.rc_out_at_rto = 0;
24140 			}
24141 		} else if (SEQ_GEQ(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) {
24142 			/*
24143 			 * Sending new data will also kill
24144 			 * the loop.
24145 			 */
24146 			rack->r_must_retran = 0;
24147 			rack->r_ctl.rc_out_at_rto = 0;
24148 		}
24149 	}
24150 	rack->r_ctl.fsb.recwin = recwin;
24151 	if ((tp->t_flags & (TF_WASCRECOVERY|TF_WASFRECOVERY)) &&
24152 	    SEQ_GT(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) {
24153 		/*
24154 		 * We hit an RTO and now have past snd_max at the RTO
24155 		 * clear all the WAS flags.
24156 		 */
24157 		tp->t_flags &= ~(TF_WASCRECOVERY|TF_WASFRECOVERY);
24158 	}
24159 	if (slot) {
24160 		/* set the rack tcb into the slot N */
24161 		if ((error == 0) &&
24162 		    rack_use_rfo &&
24163 		    ((flags & (TH_SYN|TH_FIN)) == 0) &&
24164 		    (rsm == NULL) &&
24165 		    (ipoptlen == 0) &&
24166 		    (tp->rcv_numsacks == 0) &&
24167 		    (rack->rc_policer_detected == 0)  &&
24168 		    rack->r_fsb_inited &&
24169 		    TCPS_HAVEESTABLISHED(tp->t_state) &&
24170 		    ((IN_RECOVERY(tp->t_flags)) == 0) &&
24171 		    (rack->r_must_retran == 0) &&
24172 		    ((tp->t_flags & TF_NEEDFIN) == 0) &&
24173 		    (len > 0) && (orig_len > 0) &&
24174 		    (orig_len > len) &&
24175 		    ((orig_len - len) >= segsiz) &&
24176 		    ((optlen == 0) ||
24177 		     ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
24178 			/* We can send at least one more MSS using our fsb */
24179 			rack_setup_fast_output(tp, rack, sb, len, orig_len,
24180 					       segsiz, pace_max_seg, hw_tls, flags);
24181 		} else
24182 			rack->r_fast_output = 0;
24183 		rack_log_fsb(rack, tp, so, flags,
24184 			     ipoptlen, orig_len, len, error,
24185 			     (rsm == NULL), optlen, __LINE__, 2);
24186 	} else if (sendalot) {
24187 		int ret;
24188 
24189 		sack_rxmit = 0;
24190 		if ((error == 0) &&
24191 		    rack_use_rfo &&
24192 		    ((flags & (TH_SYN|TH_FIN)) == 0) &&
24193 		    (rsm == NULL) &&
24194 		    (ipoptlen == 0) &&
24195 		    (tp->rcv_numsacks == 0) &&
24196 		    (rack->r_must_retran == 0) &&
24197 		    rack->r_fsb_inited &&
24198 		    TCPS_HAVEESTABLISHED(tp->t_state) &&
24199 		    ((IN_RECOVERY(tp->t_flags)) == 0) &&
24200 		    ((tp->t_flags & TF_NEEDFIN) == 0) &&
24201 		    (len > 0) && (orig_len > 0) &&
24202 		    (orig_len > len) &&
24203 		    ((orig_len - len) >= segsiz) &&
24204 		    ((optlen == 0) ||
24205 		     ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
24206 			/* we can use fast_output for more */
24207 			rack_setup_fast_output(tp, rack, sb, len, orig_len,
24208 					       segsiz, pace_max_seg, hw_tls, flags);
24209 			if (rack->r_fast_output) {
24210 				error = 0;
24211 				ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error);
24212 				if (ret >= 0)
24213 					return (ret);
24214 			        else if (error)
24215 					goto nomore;
24216 
24217 			}
24218 		}
24219 		goto again;
24220 	}
24221 skip_all_send:
24222 	/* Assure when we leave that snd_nxt will point to top */
24223 	if (SEQ_GT(tp->snd_max, tp->snd_nxt))
24224 		tp->snd_nxt = tp->snd_max;
24225 	rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, 0);
24226 #ifdef TCP_ACCOUNTING
24227 	crtsc = get_cyclecount() - ts_val;
24228 	if (tot_len_this_send) {
24229 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
24230 			tp->tcp_cnt_counters[SND_OUT_DATA]++;
24231 		}
24232 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
24233 			tp->tcp_proc_time[SND_OUT_DATA] += crtsc;
24234 		}
24235 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
24236 			tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) /segsiz);
24237 		}
24238 	} else {
24239 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
24240 			tp->tcp_cnt_counters[SND_OUT_ACK]++;
24241 		}
24242 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
24243 			tp->tcp_proc_time[SND_OUT_ACK] += crtsc;
24244 		}
24245 	}
24246 	sched_unpin();
24247 #endif
24248 	if (error == ENOBUFS)
24249 		error = 0;
24250 	return (error);
24251 }
24252 
24253 static void
24254 rack_update_seg(struct tcp_rack *rack)
24255 {
24256 	uint32_t orig_val;
24257 
24258 	orig_val = rack->r_ctl.rc_pace_max_segs;
24259 	rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
24260 	if (orig_val != rack->r_ctl.rc_pace_max_segs)
24261 		rack_log_pacing_delay_calc(rack, 0, 0, orig_val, 0, 0, 15, __LINE__, NULL, 0);
24262 }
24263 
24264 static void
24265 rack_mtu_change(struct tcpcb *tp)
24266 {
24267 	/*
24268 	 * The MSS may have changed
24269 	 */
24270 	struct tcp_rack *rack;
24271 	struct rack_sendmap *rsm;
24272 
24273 	rack = (struct tcp_rack *)tp->t_fb_ptr;
24274 	if (rack->r_ctl.rc_pace_min_segs != ctf_fixed_maxseg(tp)) {
24275 		/*
24276 		 * The MTU has changed we need to resend everything
24277 		 * since all we have sent is lost. We first fix
24278 		 * up the mtu though.
24279 		 */
24280 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
24281 		/* We treat this like a full retransmit timeout without the cwnd adjustment */
24282 		rack_remxt_tmr(tp);
24283 		rack->r_fast_output = 0;
24284 		rack->r_ctl.rc_out_at_rto = ctf_flight_size(tp,
24285 						rack->r_ctl.rc_sacked);
24286 		rack->r_ctl.rc_snd_max_at_rto = tp->snd_max;
24287 		rack->r_must_retran = 1;
24288 		/* Mark all inflight to needing to be rxt'd */
24289 		TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
24290 			rsm->r_flags |= (RACK_MUST_RXT|RACK_PMTU_CHG);
24291 		}
24292 	}
24293 	sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
24294 	/* We don't use snd_nxt to retransmit */
24295 	tp->snd_nxt = tp->snd_max;
24296 }
24297 
24298 static int
24299 rack_set_dgp(struct tcp_rack *rack)
24300 {
24301 	if (rack->dgp_on == 1)
24302 		return(0);
24303 	if ((rack->use_fixed_rate == 1) &&
24304 	    (rack->rc_always_pace == 1)) {
24305 		/*
24306 		 * We are already pacing another
24307 		 * way.
24308 		 */
24309 		return (EBUSY);
24310 	}
24311 	if (rack->rc_always_pace == 1) {
24312 		rack_remove_pacing(rack);
24313 	}
24314 	if (tcp_incr_dgp_pacing_cnt() == 0)
24315 		return (ENOSPC);
24316 	rack->r_ctl.pacing_method |= RACK_DGP_PACING;
24317 	rack->rc_fillcw_apply_discount = 0;
24318 	rack->dgp_on = 1;
24319 	rack->rc_always_pace = 1;
24320 	rack->rc_pace_dnd = 1;
24321 	rack->use_fixed_rate = 0;
24322 	if (rack->gp_ready)
24323 		rack_set_cc_pacing(rack);
24324 	rack->rc_tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
24325 	rack->rack_attempt_hdwr_pace = 0;
24326 	/* rxt settings */
24327 	rack->full_size_rxt = 1;
24328 	rack->shape_rxt_to_pacing_min  = 0;
24329 	/* cmpack=1 */
24330 	rack->r_use_cmp_ack = 1;
24331 	if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state) &&
24332 	    rack->r_use_cmp_ack)
24333 		rack->rc_tp->t_flags2 |= TF2_MBUF_ACKCMP;
24334 	/* scwnd=1 */
24335 	rack->rack_enable_scwnd = 1;
24336 	/* dynamic=100 */
24337 	rack->rc_gp_dyn_mul = 1;
24338 	/* gp_inc_ca */
24339 	rack->r_ctl.rack_per_of_gp_ca = 100;
24340 	/* rrr_conf=3 */
24341 	rack->r_rr_config = 3;
24342 	/* npush=2 */
24343 	rack->r_ctl.rc_no_push_at_mrtt = 2;
24344 	/* fillcw=1 */
24345 	rack->rc_pace_to_cwnd = 1;
24346 	rack->rc_pace_fill_if_rttin_range = 0;
24347 	rack->rtt_limit_mul = 0;
24348 	/* noprr=1 */
24349 	rack->rack_no_prr = 1;
24350 	/* lscwnd=1 */
24351 	rack->r_limit_scw = 1;
24352 	/* gp_inc_rec */
24353 	rack->r_ctl.rack_per_of_gp_rec = 90;
24354 	return (0);
24355 }
24356 
24357 static int
24358 rack_set_profile(struct tcp_rack *rack, int prof)
24359 {
24360 	int err = EINVAL;
24361 	if (prof == 1) {
24362 		/*
24363 		 * Profile 1 is "standard" DGP. It ignores
24364 		 * client buffer level.
24365 		 */
24366 		err = rack_set_dgp(rack);
24367 		if (err)
24368 			return (err);
24369 	} else if (prof == 6) {
24370 		err = rack_set_dgp(rack);
24371 		if (err)
24372 			return (err);
24373 		/*
24374 		 * Profile 6 tweaks DGP so that it will apply to
24375 		 * fill-cw the same settings that profile5 does
24376 		 * to replace DGP. It gets then the max(dgp-rate, fillcw(discounted).
24377 		 */
24378 		rack->rc_fillcw_apply_discount = 1;
24379 	} else if (prof == 0) {
24380 		/* This changes things back to the default settings */
24381 		if (rack->rc_always_pace == 1) {
24382 			rack_remove_pacing(rack);
24383 		} else {
24384 			/* Make sure any stray flags are off */
24385 			rack->dgp_on = 0;
24386 			rack->rc_hybrid_mode = 0;
24387 			rack->use_fixed_rate = 0;
24388 		}
24389 		err = 0;
24390 		if (rack_fill_cw_state)
24391 			rack->rc_pace_to_cwnd = 1;
24392 		else
24393 			rack->rc_pace_to_cwnd = 0;
24394 
24395 		if (rack_pace_every_seg && tcp_can_enable_pacing()) {
24396 			rack->r_ctl.pacing_method |= RACK_REG_PACING;
24397 			rack->rc_always_pace = 1;
24398 			if (rack->rack_hibeta)
24399 				rack_set_cc_pacing(rack);
24400 		} else
24401 			rack->rc_always_pace = 0;
24402 		if (rack_dsack_std_based & 0x1) {
24403 			/* Basically this means all rack timers are at least (srtt + 1/4 srtt) */
24404 			rack->rc_rack_tmr_std_based = 1;
24405 		}
24406 		if (rack_dsack_std_based & 0x2) {
24407 			/* Basically this means  rack timers are extended based on dsack by up to (2 * srtt) */
24408 			rack->rc_rack_use_dsack = 1;
24409 		}
24410 		if (rack_use_cmp_acks)
24411 			rack->r_use_cmp_ack = 1;
24412 		else
24413 			rack->r_use_cmp_ack = 0;
24414 		if (rack_disable_prr)
24415 			rack->rack_no_prr = 1;
24416 		else
24417 			rack->rack_no_prr = 0;
24418 		if (rack_gp_no_rec_chg)
24419 			rack->rc_gp_no_rec_chg = 1;
24420 		else
24421 			rack->rc_gp_no_rec_chg = 0;
24422 		if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack) {
24423 			rack->r_mbuf_queue = 1;
24424 			if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state))
24425 				rack->rc_tp->t_flags2 |= TF2_MBUF_ACKCMP;
24426 			rack->rc_tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
24427 		} else {
24428 			rack->r_mbuf_queue = 0;
24429 			rack->rc_tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
24430 		}
24431 		if (rack_enable_shared_cwnd)
24432 			rack->rack_enable_scwnd = 1;
24433 		else
24434 			rack->rack_enable_scwnd = 0;
24435 		if (rack_do_dyn_mul) {
24436 			/* When dynamic adjustment is on CA needs to start at 100% */
24437 			rack->rc_gp_dyn_mul = 1;
24438 			if (rack_do_dyn_mul >= 100)
24439 				rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul;
24440 		} else {
24441 			rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca;
24442 			rack->rc_gp_dyn_mul = 0;
24443 		}
24444 		rack->r_rr_config = 0;
24445 		rack->r_ctl.rc_no_push_at_mrtt = 0;
24446 		rack->rc_pace_fill_if_rttin_range = 0;
24447 		rack->rtt_limit_mul = 0;
24448 
24449 		if (rack_enable_hw_pacing)
24450 			rack->rack_hdw_pace_ena = 1;
24451 		else
24452 			rack->rack_hdw_pace_ena = 0;
24453 		if (rack_disable_prr)
24454 			rack->rack_no_prr = 1;
24455 		else
24456 			rack->rack_no_prr = 0;
24457 		if (rack_limits_scwnd)
24458 			rack->r_limit_scw  = 1;
24459 		else
24460 			rack->r_limit_scw  = 0;
24461 		rack_init_retransmit_value(rack, rack_rxt_controls);
24462 		err = 0;
24463 	}
24464 	return (err);
24465 }
24466 
24467 static int
24468 rack_add_deferred_option(struct tcp_rack *rack, int sopt_name, uint64_t loptval)
24469 {
24470 	struct deferred_opt_list *dol;
24471 
24472 	dol = malloc(sizeof(struct deferred_opt_list),
24473 		     M_TCPDO, M_NOWAIT|M_ZERO);
24474 	if (dol == NULL) {
24475 		/*
24476 		 * No space yikes -- fail out..
24477 		 */
24478 		return (0);
24479 	}
24480 	dol->optname = sopt_name;
24481 	dol->optval = loptval;
24482 	TAILQ_INSERT_TAIL(&rack->r_ctl.opt_list, dol, next);
24483 	return (1);
24484 }
24485 
24486 static int
24487 process_hybrid_pacing(struct tcp_rack *rack, struct tcp_hybrid_req *hybrid)
24488 {
24489 #ifdef TCP_REQUEST_TRK
24490 	struct tcp_sendfile_track *sft;
24491 	struct timeval tv;
24492 	tcp_seq seq;
24493 	int err;
24494 
24495 	microuptime(&tv);
24496 
24497 	/* Make sure no fixed rate is on */
24498 	rack->use_fixed_rate = 0;
24499 	rack->r_ctl.rc_fixed_pacing_rate_rec = 0;
24500 	rack->r_ctl.rc_fixed_pacing_rate_ca = 0;
24501 	rack->r_ctl.rc_fixed_pacing_rate_ss = 0;
24502 	/* Now allocate or find our entry that will have these settings */
24503 	sft = tcp_req_alloc_req_full(rack->rc_tp, &hybrid->req, tcp_tv_to_lusectick(&tv), 0);
24504 	if (sft == NULL) {
24505 		rack->rc_tp->tcp_hybrid_error++;
24506 		/* no space, where would it have gone? */
24507 		seq = rack->rc_tp->snd_una + rack->rc_tp->t_inpcb.inp_socket->so_snd.sb_ccc;
24508 		rack_log_hybrid(rack, seq, NULL, HYBRID_LOG_NO_ROOM, __LINE__, 0);
24509 		return (ENOSPC);
24510 	}
24511 	/* mask our internal flags */
24512 	hybrid->hybrid_flags &= TCP_HYBRID_PACING_USER_MASK;
24513 	/* The seq will be snd_una + everything in the buffer */
24514 	seq = sft->start_seq;
24515 	if ((hybrid->hybrid_flags & TCP_HYBRID_PACING_ENABLE) == 0) {
24516 		/* Disabling hybrid pacing */
24517 		if (rack->rc_hybrid_mode) {
24518 			rack_set_profile(rack, 0);
24519 			rack->rc_tp->tcp_hybrid_stop++;
24520 		}
24521 		rack_log_hybrid(rack, seq, sft, HYBRID_LOG_TURNED_OFF, __LINE__, 0);
24522 		return (0);
24523 	}
24524 	if (rack->dgp_on == 0) {
24525 		/*
24526 		 * If we have not yet turned DGP on, do so
24527 		 * now setting pure DGP mode, no buffer level
24528 		 * response.
24529 		 */
24530 		if ((err = rack_set_profile(rack, 1)) != 0){
24531 			/* Failed to turn pacing on */
24532 			rack->rc_tp->tcp_hybrid_error++;
24533 			rack_log_hybrid(rack, seq, sft, HYBRID_LOG_NO_PACING, __LINE__, 0);
24534 			return (err);
24535 		}
24536 	}
24537 	/*
24538 	 * Now we must switch to hybrid mode as well which also
24539 	 * means moving to regular pacing.
24540 	 */
24541 	if (rack->rc_hybrid_mode == 0) {
24542 		/* First time */
24543 		if (tcp_can_enable_pacing()) {
24544 			rack->r_ctl.pacing_method |= RACK_REG_PACING;
24545 			rack->rc_hybrid_mode = 1;
24546 		} else {
24547 			return (ENOSPC);
24548 		}
24549 		if (rack->r_ctl.pacing_method & RACK_DGP_PACING) {
24550 			/*
24551 			 * This should be true.
24552 			 */
24553 			tcp_dec_dgp_pacing_cnt();
24554 			rack->r_ctl.pacing_method &= ~RACK_DGP_PACING;
24555 		}
24556 	}
24557 	/* Now set in our flags */
24558 	sft->hybrid_flags = hybrid->hybrid_flags | TCP_HYBRID_PACING_WASSET;
24559 	if (hybrid->hybrid_flags & TCP_HYBRID_PACING_CSPR)
24560 		sft->cspr = hybrid->cspr;
24561 	else
24562 		sft->cspr = 0;
24563 	if (hybrid->hybrid_flags & TCP_HYBRID_PACING_H_MS)
24564 		sft->hint_maxseg = hybrid->hint_maxseg;
24565 	else
24566 		sft->hint_maxseg = 0;
24567 	rack->rc_tp->tcp_hybrid_start++;
24568 	rack_log_hybrid(rack, seq, sft, HYBRID_LOG_RULES_SET, __LINE__,0);
24569 	return (0);
24570 #else
24571 	return (ENOTSUP);
24572 #endif
24573 }
24574 
24575 static int
24576 rack_stack_information(struct tcpcb *tp, struct stack_specific_info *si)
24577 {
24578 	/*
24579 	 * Gather rack specific information.
24580 	 */
24581 	struct tcp_rack *rack;
24582 
24583 	rack = (struct tcp_rack *)tp->t_fb_ptr;
24584 	/* We pulled a SSI info log out what was there */
24585 	policer_detection_log(rack, rack->rc_highly_buffered, 0, 0, 0, 20);
24586 	if (rack->policer_detect_on) {
24587 		si->policer_detection_enabled = 1;
24588 		if (rack->rc_policer_detected) {
24589 			si->policer_detected = 1;
24590 			si->policer_bucket_size = rack->r_ctl.policer_bucket_size;
24591 			si->policer_last_bw = rack->r_ctl.policer_bw;
24592 		} else {
24593 			si->policer_detected = 0;
24594 			si->policer_bucket_size = 0;
24595 			si->policer_last_bw = 0;
24596 		}
24597 		si->current_round = rack->r_ctl.current_round;
24598 		si->highly_buffered = rack->rc_highly_buffered;
24599 	}
24600 	si->bytes_transmitted = tp->t_sndbytes;
24601 	si->bytes_retransmitted = tp->t_snd_rxt_bytes;
24602 	return (0);
24603 }
24604 
24605 static int
24606 rack_process_option(struct tcpcb *tp, struct tcp_rack *rack, int sopt_name,
24607 		    uint32_t optval, uint64_t loptval, struct tcp_hybrid_req *hybrid)
24608 
24609 {
24610 	struct epoch_tracker et;
24611 	struct sockopt sopt;
24612 	struct cc_newreno_opts opt;
24613 	uint64_t val;
24614 	int error = 0;
24615 	uint16_t ca, ss;
24616 
24617 	switch (sopt_name) {
24618 	case TCP_RACK_SET_RXT_OPTIONS:
24619 		if ((optval >= 0) && (optval <= 2)) {
24620 			rack_init_retransmit_value(rack, optval);
24621 		} else {
24622 			/*
24623 			 * You must send in 0, 1 or 2 all else is
24624 			 * invalid.
24625 			 */
24626 			error = EINVAL;
24627 		}
24628 		break;
24629 	case TCP_RACK_DSACK_OPT:
24630 		RACK_OPTS_INC(tcp_rack_dsack_opt);
24631 		if (optval & 0x1) {
24632 			rack->rc_rack_tmr_std_based = 1;
24633 		} else {
24634 			rack->rc_rack_tmr_std_based = 0;
24635 		}
24636 		if (optval & 0x2) {
24637 			rack->rc_rack_use_dsack = 1;
24638 		} else {
24639 			rack->rc_rack_use_dsack = 0;
24640 		}
24641 		rack_log_dsack_event(rack, 5, __LINE__, 0, 0);
24642 		break;
24643 	case TCP_RACK_PACING_DIVISOR:
24644 		RACK_OPTS_INC(tcp_rack_pacing_divisor);
24645 		if (optval == 0) {
24646 			rack->r_ctl.pace_len_divisor = rack_default_pacing_divisor;
24647 		} else {
24648 			if (optval < RL_MIN_DIVISOR)
24649 				rack->r_ctl.pace_len_divisor = RL_MIN_DIVISOR;
24650 			else
24651 				rack->r_ctl.pace_len_divisor = optval;
24652 		}
24653 		break;
24654 	case TCP_RACK_HI_BETA:
24655 		RACK_OPTS_INC(tcp_rack_hi_beta);
24656 		if (optval > 0) {
24657 			rack->rack_hibeta = 1;
24658 			if ((optval >= 50) &&
24659 			    (optval <= 100)) {
24660 				/*
24661 				 * User wants to set a custom beta.
24662 				 */
24663 				rack->r_ctl.saved_hibeta = optval;
24664 				if (rack->rc_pacing_cc_set)
24665 					rack_undo_cc_pacing(rack);
24666 				rack->r_ctl.rc_saved_beta.beta = optval;
24667 			}
24668 			if (rack->rc_pacing_cc_set == 0)
24669 				rack_set_cc_pacing(rack);
24670 		} else {
24671 			rack->rack_hibeta = 0;
24672 			if (rack->rc_pacing_cc_set)
24673 				rack_undo_cc_pacing(rack);
24674 		}
24675 		break;
24676 	case TCP_RACK_PACING_BETA:
24677 		error = EINVAL;
24678 		break;
24679 	case TCP_RACK_TIMER_SLOP:
24680 		RACK_OPTS_INC(tcp_rack_timer_slop);
24681 		rack->r_ctl.timer_slop = optval;
24682 		if (rack->rc_tp->t_srtt) {
24683 			/*
24684 			 * If we have an SRTT lets update t_rxtcur
24685 			 * to have the new slop.
24686 			 */
24687 			RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
24688 					   rack_rto_min, rack_rto_max,
24689 					   rack->r_ctl.timer_slop);
24690 		}
24691 		break;
24692 	case TCP_RACK_PACING_BETA_ECN:
24693 		RACK_OPTS_INC(tcp_rack_beta_ecn);
24694 		if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0) {
24695 			/* This only works for newreno. */
24696 			error = EINVAL;
24697 			break;
24698 		}
24699 		if (rack->rc_pacing_cc_set) {
24700 			/*
24701 			 * Set them into the real CC module
24702 			 * whats in the rack pcb is the old values
24703 			 * to be used on restoral/
24704 			 */
24705 			sopt.sopt_dir = SOPT_SET;
24706 			opt.name = CC_NEWRENO_BETA_ECN;
24707 			opt.val = optval;
24708 			if (CC_ALGO(tp)->ctl_output != NULL)
24709 				error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
24710 			else
24711 				error = ENOENT;
24712 		} else {
24713 			/*
24714 			 * Not pacing yet so set it into our local
24715 			 * rack pcb storage.
24716 			 */
24717 			rack->r_ctl.rc_saved_beta.beta_ecn = optval;
24718 			rack->r_ctl.rc_saved_beta.newreno_flags = CC_NEWRENO_BETA_ECN_ENABLED;
24719 		}
24720 		break;
24721 	case TCP_DEFER_OPTIONS:
24722 		RACK_OPTS_INC(tcp_defer_opt);
24723 		if (optval) {
24724 			if (rack->gp_ready) {
24725 				/* Too late */
24726 				error = EINVAL;
24727 				break;
24728 			}
24729 			rack->defer_options = 1;
24730 		} else
24731 			rack->defer_options = 0;
24732 		break;
24733 	case TCP_RACK_MEASURE_CNT:
24734 		RACK_OPTS_INC(tcp_rack_measure_cnt);
24735 		if (optval && (optval <= 0xff)) {
24736 			rack->r_ctl.req_measurements = optval;
24737 		} else
24738 			error = EINVAL;
24739 		break;
24740 	case TCP_REC_ABC_VAL:
24741 		RACK_OPTS_INC(tcp_rec_abc_val);
24742 		if (optval > 0)
24743 			rack->r_use_labc_for_rec = 1;
24744 		else
24745 			rack->r_use_labc_for_rec = 0;
24746 		break;
24747 	case TCP_RACK_ABC_VAL:
24748 		RACK_OPTS_INC(tcp_rack_abc_val);
24749 		if ((optval > 0) && (optval < 255))
24750 			rack->rc_labc = optval;
24751 		else
24752 			error = EINVAL;
24753 		break;
24754 	case TCP_HDWR_UP_ONLY:
24755 		RACK_OPTS_INC(tcp_pacing_up_only);
24756 		if (optval)
24757 			rack->r_up_only = 1;
24758 		else
24759 			rack->r_up_only = 0;
24760 		break;
24761 	case TCP_FILLCW_RATE_CAP:		/*  URL:fillcw_cap */
24762 		RACK_OPTS_INC(tcp_fillcw_rate_cap);
24763 		rack->r_ctl.fillcw_cap = loptval;
24764 		break;
24765 	case TCP_PACING_RATE_CAP:
24766 		RACK_OPTS_INC(tcp_pacing_rate_cap);
24767 		if ((rack->dgp_on == 1) &&
24768 		    (rack->r_ctl.pacing_method & RACK_DGP_PACING)) {
24769 			/*
24770 			 * If we are doing DGP we need to switch
24771 			 * to using the pacing limit.
24772 			 */
24773 			if (tcp_can_enable_pacing() == 0) {
24774 				error = ENOSPC;
24775 				break;
24776 			}
24777 			/*
24778 			 * Now change up the flags and counts to be correct.
24779 			 */
24780 			rack->r_ctl.pacing_method |= RACK_REG_PACING;
24781 			tcp_dec_dgp_pacing_cnt();
24782 			rack->r_ctl.pacing_method &= ~RACK_DGP_PACING;
24783 		}
24784 		rack->r_ctl.bw_rate_cap = loptval;
24785 		break;
24786 	case TCP_HYBRID_PACING:
24787 		if (hybrid == NULL) {
24788 			error = EINVAL;
24789 			break;
24790 		}
24791 		if (rack->r_ctl.side_chan_dis_mask & HYBRID_DIS_MASK) {
24792 			error = EPERM;
24793 			break;
24794 		}
24795 		error = process_hybrid_pacing(rack, hybrid);
24796 		break;
24797 	case TCP_SIDECHAN_DIS:			/*  URL:scodm */
24798 		if (optval)
24799 			rack->r_ctl.side_chan_dis_mask = optval;
24800 		else
24801 			rack->r_ctl.side_chan_dis_mask = 0;
24802 		break;
24803 	case TCP_RACK_PROFILE:
24804 		RACK_OPTS_INC(tcp_profile);
24805 		error = rack_set_profile(rack, optval);
24806 		break;
24807 	case TCP_USE_CMP_ACKS:
24808 		RACK_OPTS_INC(tcp_use_cmp_acks);
24809 		if ((optval == 0) && (tp->t_flags2 & TF2_MBUF_ACKCMP)) {
24810 			/* You can't turn it off once its on! */
24811 			error = EINVAL;
24812 		} else if ((optval == 1) && (rack->r_use_cmp_ack == 0)) {
24813 			rack->r_use_cmp_ack = 1;
24814 			rack->r_mbuf_queue = 1;
24815 			tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
24816 		}
24817 		if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
24818 			tp->t_flags2 |= TF2_MBUF_ACKCMP;
24819 		break;
24820 	case TCP_SHARED_CWND_TIME_LIMIT:
24821 		RACK_OPTS_INC(tcp_lscwnd);
24822 		if (optval)
24823 			rack->r_limit_scw = 1;
24824 		else
24825 			rack->r_limit_scw = 0;
24826 		break;
24827 	case TCP_RACK_DGP_IN_REC:
24828 		error = EINVAL;
24829 		break;
24830 	case TCP_POLICER_DETECT:		/*  URL:pol_det */
24831 		RACK_OPTS_INC(tcp_pol_detect);
24832 		rack_translate_policer_detect(rack, optval);
24833 		break;
24834 	case TCP_POLICER_MSS:
24835 		RACK_OPTS_INC(tcp_pol_mss);
24836 		rack->r_ctl.policer_del_mss = (uint8_t)optval;
24837 		if (optval & 0x00000100) {
24838 			/*
24839 			 * Value is setup like so:
24840 			 * VVVV VVVV VVVV VVVV VVVV VVAI MMMM MMMM
24841 			 * Where MMMM MMMM is MSS setting
24842 			 * I (9th bit) is the Postive value that
24843 			 * says it is being set (if its 0 then the
24844 			 * upper bits 11 - 32 have no meaning.
24845 			 * This allows setting it off with
24846 			 * 0x000001MM.
24847 			 *
24848 			 * The 10th bit is used to turn on the
24849 			 * alternate median (not the expanded one).
24850 			 *
24851 			 */
24852 			rack->r_ctl.pol_bw_comp = (optval >> 10);
24853 		}
24854 		if (optval & 0x00000200) {
24855 			rack->r_ctl.policer_alt_median = 1;
24856 		} else {
24857 			rack->r_ctl.policer_alt_median = 0;
24858 		}
24859 		break;
24860  	case TCP_RACK_PACE_TO_FILL:
24861 		RACK_OPTS_INC(tcp_fillcw);
24862 		if (optval == 0)
24863 			rack->rc_pace_to_cwnd = 0;
24864 		else {
24865 			rack->rc_pace_to_cwnd = 1;
24866 		}
24867 		if ((optval >= rack_gp_rtt_maxmul) &&
24868 		    rack_gp_rtt_maxmul &&
24869 		    (optval < 0xf)) {
24870 			rack->rc_pace_fill_if_rttin_range = 1;
24871 			rack->rtt_limit_mul = optval;
24872 		} else {
24873 			rack->rc_pace_fill_if_rttin_range = 0;
24874 			rack->rtt_limit_mul = 0;
24875 		}
24876 		break;
24877 	case TCP_RACK_NO_PUSH_AT_MAX:
24878 		RACK_OPTS_INC(tcp_npush);
24879 		if (optval == 0)
24880 			rack->r_ctl.rc_no_push_at_mrtt = 0;
24881 		else if (optval < 0xff)
24882 			rack->r_ctl.rc_no_push_at_mrtt = optval;
24883 		else
24884 			error = EINVAL;
24885 		break;
24886 	case TCP_SHARED_CWND_ENABLE:
24887 		RACK_OPTS_INC(tcp_rack_scwnd);
24888 		if (optval == 0)
24889 			rack->rack_enable_scwnd = 0;
24890 		else
24891 			rack->rack_enable_scwnd = 1;
24892 		break;
24893 	case TCP_RACK_MBUF_QUEUE:
24894 		/* Now do we use the LRO mbuf-queue feature */
24895 		RACK_OPTS_INC(tcp_rack_mbufq);
24896 		if (optval || rack->r_use_cmp_ack)
24897 			rack->r_mbuf_queue = 1;
24898 		else
24899 			rack->r_mbuf_queue = 0;
24900 		if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
24901 			tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
24902 		else
24903 			tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
24904 		break;
24905 	case TCP_RACK_NONRXT_CFG_RATE:
24906 		RACK_OPTS_INC(tcp_rack_cfg_rate);
24907 		if (optval == 0)
24908 			rack->rack_rec_nonrxt_use_cr = 0;
24909 		else
24910 			rack->rack_rec_nonrxt_use_cr = 1;
24911 		break;
24912 	case TCP_NO_PRR:
24913 		RACK_OPTS_INC(tcp_rack_noprr);
24914 		if (optval == 0)
24915 			rack->rack_no_prr = 0;
24916 		else if (optval == 1)
24917 			rack->rack_no_prr = 1;
24918 		else if (optval == 2)
24919 			rack->no_prr_addback = 1;
24920 		else
24921 			error = EINVAL;
24922 		break;
24923 	case RACK_CSPR_IS_FCC:			/*  URL:csprisfcc */
24924 		if (optval > 0)
24925 			rack->cspr_is_fcc = 1;
24926 		else
24927 			rack->cspr_is_fcc = 0;
24928 		break;
24929 	case TCP_TIMELY_DYN_ADJ:
24930 		RACK_OPTS_INC(tcp_timely_dyn);
24931 		if (optval == 0)
24932 			rack->rc_gp_dyn_mul = 0;
24933 		else {
24934 			rack->rc_gp_dyn_mul = 1;
24935 			if (optval >= 100) {
24936 				/*
24937 				 * If the user sets something 100 or more
24938 				 * its the gp_ca value.
24939 				 */
24940 				rack->r_ctl.rack_per_of_gp_ca  = optval;
24941 			}
24942 		}
24943 		break;
24944 	case TCP_RACK_DO_DETECTION:
24945 		RACK_OPTS_INC(tcp_rack_do_detection);
24946 		if (optval == 0)
24947 			rack->do_detection = 0;
24948 		else
24949 			rack->do_detection = 1;
24950 		break;
24951 	case TCP_RACK_TLP_USE:
24952 		if ((optval < TLP_USE_ID) || (optval > TLP_USE_TWO_TWO)) {
24953 			error = EINVAL;
24954 			break;
24955 		}
24956 		RACK_OPTS_INC(tcp_tlp_use);
24957 		rack->rack_tlp_threshold_use = optval;
24958 		break;
24959 	case TCP_RACK_TLP_REDUCE:
24960 		/* RACK TLP cwnd reduction (bool) */
24961 		RACK_OPTS_INC(tcp_rack_tlp_reduce);
24962 		rack->r_ctl.rc_tlp_cwnd_reduce = optval;
24963 		break;
24964 		/*  Pacing related ones */
24965 	case TCP_RACK_PACE_ALWAYS:
24966 		/*
24967 		 * zero is old rack method, 1 is new
24968 		 * method using a pacing rate.
24969 		 */
24970 		RACK_OPTS_INC(tcp_rack_pace_always);
24971 		if (rack->r_ctl.side_chan_dis_mask & CCSP_DIS_MASK) {
24972 			error = EPERM;
24973 			break;
24974 		}
24975 		if (optval > 0) {
24976 			if (rack->rc_always_pace) {
24977 				error = EALREADY;
24978 				break;
24979 			} else if (tcp_can_enable_pacing()) {
24980 				rack->r_ctl.pacing_method |= RACK_REG_PACING;
24981 				rack->rc_always_pace = 1;
24982 				if (rack->rack_hibeta)
24983 					rack_set_cc_pacing(rack);
24984 			}
24985 			else {
24986 				error = ENOSPC;
24987 				break;
24988 			}
24989 		} else {
24990 			if (rack->rc_always_pace == 1) {
24991 				rack_remove_pacing(rack);
24992 			}
24993 		}
24994 		if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
24995 			tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
24996 		else
24997 			tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
24998 		/* A rate may be set irate or other, if so set seg size */
24999 		rack_update_seg(rack);
25000 		break;
25001 	case TCP_BBR_RACK_INIT_RATE:
25002 		RACK_OPTS_INC(tcp_initial_rate);
25003 		val = optval;
25004 		/* Change from kbits per second to bytes per second */
25005 		val *= 1000;
25006 		val /= 8;
25007 		rack->r_ctl.init_rate = val;
25008 		if (rack->rc_always_pace)
25009 			rack_update_seg(rack);
25010 		break;
25011 	case TCP_BBR_IWINTSO:
25012 		error = EINVAL;
25013 		break;
25014 	case TCP_RACK_FORCE_MSEG:
25015 		RACK_OPTS_INC(tcp_rack_force_max_seg);
25016 		if (optval)
25017 			rack->rc_force_max_seg = 1;
25018 		else
25019 			rack->rc_force_max_seg = 0;
25020 		break;
25021 	case TCP_RACK_PACE_MIN_SEG:
25022 		RACK_OPTS_INC(tcp_rack_min_seg);
25023 		rack->r_ctl.rc_user_set_min_segs = (0x0000ffff & optval);
25024 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
25025 		break;
25026 	case TCP_RACK_PACE_MAX_SEG:
25027 		/* Max segments size in a pace in bytes */
25028 		RACK_OPTS_INC(tcp_rack_max_seg);
25029 		if ((rack->dgp_on == 1) &&
25030 		    (rack->r_ctl.pacing_method & RACK_DGP_PACING)) {
25031 			/*
25032 			 * If we set a max-seg and are doing DGP then
25033 			 * we now fall under the pacing limits not the
25034 			 * DGP ones.
25035 			 */
25036 			if (tcp_can_enable_pacing() == 0) {
25037 				error = ENOSPC;
25038 				break;
25039 			}
25040 			/*
25041 			 * Now change up the flags and counts to be correct.
25042 			 */
25043 			rack->r_ctl.pacing_method |= RACK_REG_PACING;
25044 			tcp_dec_dgp_pacing_cnt();
25045 			rack->r_ctl.pacing_method &= ~RACK_DGP_PACING;
25046 		}
25047 		if (optval <= MAX_USER_SET_SEG)
25048 			rack->rc_user_set_max_segs = optval;
25049 		else
25050 			rack->rc_user_set_max_segs = MAX_USER_SET_SEG;
25051 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
25052 		break;
25053 	case TCP_RACK_PACE_RATE_REC:
25054 		/* Set the fixed pacing rate in Bytes per second ca */
25055 		RACK_OPTS_INC(tcp_rack_pace_rate_rec);
25056 		if (rack->r_ctl.side_chan_dis_mask & CCSP_DIS_MASK) {
25057 			error = EPERM;
25058 			break;
25059 		}
25060 		if (rack->dgp_on) {
25061 			/*
25062 			 * We are already pacing another
25063 			 * way.
25064 			 */
25065 			error = EBUSY;
25066 			break;
25067 		}
25068 		rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
25069 		if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0)
25070 			rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
25071 		if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0)
25072 			rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
25073 		rack->use_fixed_rate = 1;
25074 		if (rack->rack_hibeta)
25075 			rack_set_cc_pacing(rack);
25076 		rack_log_pacing_delay_calc(rack,
25077 					   rack->r_ctl.rc_fixed_pacing_rate_ss,
25078 					   rack->r_ctl.rc_fixed_pacing_rate_ca,
25079 					   rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
25080 					   __LINE__, NULL,0);
25081 		break;
25082 
25083 	case TCP_RACK_PACE_RATE_SS:
25084 		/* Set the fixed pacing rate in Bytes per second ca */
25085 		RACK_OPTS_INC(tcp_rack_pace_rate_ss);
25086 		if (rack->r_ctl.side_chan_dis_mask & CCSP_DIS_MASK) {
25087 			error = EPERM;
25088 			break;
25089 		}
25090 		if (rack->dgp_on) {
25091 			/*
25092 			 * We are already pacing another
25093 			 * way.
25094 			 */
25095 			error = EBUSY;
25096 			break;
25097 		}
25098 		rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
25099 		if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0)
25100 			rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
25101 		if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0)
25102 			rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
25103 		rack->use_fixed_rate = 1;
25104 		if (rack->rack_hibeta)
25105 			rack_set_cc_pacing(rack);
25106 		rack_log_pacing_delay_calc(rack,
25107 					   rack->r_ctl.rc_fixed_pacing_rate_ss,
25108 					   rack->r_ctl.rc_fixed_pacing_rate_ca,
25109 					   rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
25110 					   __LINE__, NULL, 0);
25111 		break;
25112 
25113 	case TCP_RACK_PACE_RATE_CA:
25114 		/* Set the fixed pacing rate in Bytes per second ca */
25115 		RACK_OPTS_INC(tcp_rack_pace_rate_ca);
25116 		if (rack->r_ctl.side_chan_dis_mask & CCSP_DIS_MASK) {
25117 			error = EPERM;
25118 			break;
25119 		}
25120 		if (rack->dgp_on) {
25121 			/*
25122 			 * We are already pacing another
25123 			 * way.
25124 			 */
25125 			error = EBUSY;
25126 			break;
25127 		}
25128 		rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
25129 		if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0)
25130 			rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
25131 		if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0)
25132 			rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
25133 		rack->use_fixed_rate = 1;
25134 		if (rack->rack_hibeta)
25135 			rack_set_cc_pacing(rack);
25136 		rack_log_pacing_delay_calc(rack,
25137 					   rack->r_ctl.rc_fixed_pacing_rate_ss,
25138 					   rack->r_ctl.rc_fixed_pacing_rate_ca,
25139 					   rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
25140 					   __LINE__, NULL, 0);
25141 		break;
25142 	case TCP_RACK_GP_INCREASE_REC:
25143 		RACK_OPTS_INC(tcp_gp_inc_rec);
25144 		rack->r_ctl.rack_per_of_gp_rec = optval;
25145 		rack_log_pacing_delay_calc(rack,
25146 					   rack->r_ctl.rack_per_of_gp_ss,
25147 					   rack->r_ctl.rack_per_of_gp_ca,
25148 					   rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
25149 					   __LINE__, NULL, 0);
25150 		break;
25151 	case TCP_RACK_GP_INCREASE_CA:
25152 		RACK_OPTS_INC(tcp_gp_inc_ca);
25153 		ca = optval;
25154 		if (ca < 100) {
25155 			/*
25156 			 * We don't allow any reduction
25157 			 * over the GP b/w.
25158 			 */
25159 			error = EINVAL;
25160 			break;
25161 		}
25162 		rack->r_ctl.rack_per_of_gp_ca = ca;
25163 		rack_log_pacing_delay_calc(rack,
25164 					   rack->r_ctl.rack_per_of_gp_ss,
25165 					   rack->r_ctl.rack_per_of_gp_ca,
25166 					   rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
25167 					   __LINE__, NULL, 0);
25168 		break;
25169 	case TCP_RACK_GP_INCREASE_SS:
25170 		RACK_OPTS_INC(tcp_gp_inc_ss);
25171 		ss = optval;
25172 		if (ss < 100) {
25173 			/*
25174 			 * We don't allow any reduction
25175 			 * over the GP b/w.
25176 			 */
25177 			error = EINVAL;
25178 			break;
25179 		}
25180 		rack->r_ctl.rack_per_of_gp_ss = ss;
25181 		rack_log_pacing_delay_calc(rack,
25182 					   rack->r_ctl.rack_per_of_gp_ss,
25183 					   rack->r_ctl.rack_per_of_gp_ca,
25184 					   rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
25185 					   __LINE__, NULL, 0);
25186 		break;
25187 	case TCP_RACK_RR_CONF:
25188 		RACK_OPTS_INC(tcp_rack_rrr_no_conf_rate);
25189 		if (optval && optval <= 3)
25190 			rack->r_rr_config = optval;
25191 		else
25192 			rack->r_rr_config = 0;
25193 		break;
25194 	case TCP_PACING_DND:			/*  URL:dnd */
25195 		if (optval > 0)
25196 			rack->rc_pace_dnd = 1;
25197 		else
25198 			rack->rc_pace_dnd = 0;
25199 		break;
25200 	case TCP_HDWR_RATE_CAP:
25201 		RACK_OPTS_INC(tcp_hdwr_rate_cap);
25202 		if (optval) {
25203 			if (rack->r_rack_hw_rate_caps == 0)
25204 				rack->r_rack_hw_rate_caps = 1;
25205 			else
25206 				error = EALREADY;
25207 		} else {
25208 			rack->r_rack_hw_rate_caps = 0;
25209 		}
25210 		break;
25211 	case TCP_DGP_UPPER_BOUNDS:
25212 	{
25213 		uint8_t val;
25214 		val = optval & 0x0000ff;
25215 		rack->r_ctl.rack_per_upper_bound_ca = val;
25216 		val = (optval >> 16) & 0x0000ff;
25217 		rack->r_ctl.rack_per_upper_bound_ss = val;
25218 		break;
25219 	}
25220 	case TCP_SS_EEXIT:			/*  URL:eexit */
25221 		if (optval > 0) {
25222 			rack->r_ctl.gp_rnd_thresh =  optval & 0x0ff;
25223 			if (optval & 0x10000) {
25224 				rack->r_ctl.gate_to_fs = 1;
25225 			} else {
25226 				rack->r_ctl.gate_to_fs = 0;
25227 			}
25228 			if (optval & 0x20000) {
25229 				rack->r_ctl.use_gp_not_last = 1;
25230 			} else {
25231 				rack->r_ctl.use_gp_not_last = 0;
25232 			}
25233 			if (optval & 0xfffc0000) {
25234 				uint32_t v;
25235 
25236 				v = (optval >> 18) & 0x00003fff;
25237 				if (v >= 1000)
25238 					rack->r_ctl.gp_gain_req = v;
25239 			}
25240 		} else {
25241 			/* We do not do ss early exit at all */
25242 			rack->rc_initial_ss_comp = 1;
25243 			rack->r_ctl.gp_rnd_thresh = 0;
25244 		}
25245 		break;
25246 	case TCP_RACK_SPLIT_LIMIT:
25247 		RACK_OPTS_INC(tcp_split_limit);
25248 		rack->r_ctl.rc_split_limit = optval;
25249 		break;
25250 	case TCP_BBR_HDWR_PACE:
25251 		RACK_OPTS_INC(tcp_hdwr_pacing);
25252 		if (optval){
25253 			if (rack->rack_hdrw_pacing == 0) {
25254 				rack->rack_hdw_pace_ena = 1;
25255 				rack->rack_attempt_hdwr_pace = 0;
25256 			} else
25257 				error = EALREADY;
25258 		} else {
25259 			rack->rack_hdw_pace_ena = 0;
25260 #ifdef RATELIMIT
25261 			if (rack->r_ctl.crte != NULL) {
25262 				rack->rack_hdrw_pacing = 0;
25263 				rack->rack_attempt_hdwr_pace = 0;
25264 				tcp_rel_pacing_rate(rack->r_ctl.crte, tp);
25265 				rack->r_ctl.crte = NULL;
25266 			}
25267 #endif
25268 		}
25269 		break;
25270 		/*  End Pacing related ones */
25271 	case TCP_RACK_PRR_SENDALOT:
25272 		/* Allow PRR to send more than one seg */
25273 		RACK_OPTS_INC(tcp_rack_prr_sendalot);
25274 		rack->r_ctl.rc_prr_sendalot = optval;
25275 		break;
25276 	case TCP_RACK_MIN_TO:
25277 		/* Minimum time between rack t-o's in ms */
25278 		RACK_OPTS_INC(tcp_rack_min_to);
25279 		rack->r_ctl.rc_min_to = optval;
25280 		break;
25281 	case TCP_RACK_EARLY_SEG:
25282 		/* If early recovery max segments */
25283 		RACK_OPTS_INC(tcp_rack_early_seg);
25284 		rack->r_ctl.rc_early_recovery_segs = optval;
25285 		break;
25286 	case TCP_RACK_ENABLE_HYSTART:
25287 	{
25288 		if (optval) {
25289 			tp->t_ccv.flags |= CCF_HYSTART_ALLOWED;
25290 			if (rack_do_hystart > RACK_HYSTART_ON)
25291 				tp->t_ccv.flags |= CCF_HYSTART_CAN_SH_CWND;
25292 			if (rack_do_hystart > RACK_HYSTART_ON_W_SC)
25293 				tp->t_ccv.flags |= CCF_HYSTART_CONS_SSTH;
25294 		} else {
25295 			tp->t_ccv.flags &= ~(CCF_HYSTART_ALLOWED|CCF_HYSTART_CAN_SH_CWND|CCF_HYSTART_CONS_SSTH);
25296 		}
25297 	}
25298 	break;
25299 	case TCP_RACK_REORD_THRESH:
25300 		/* RACK reorder threshold (shift amount) */
25301 		RACK_OPTS_INC(tcp_rack_reord_thresh);
25302 		if ((optval > 0) && (optval < 31))
25303 			rack->r_ctl.rc_reorder_shift = optval;
25304 		else
25305 			error = EINVAL;
25306 		break;
25307 	case TCP_RACK_REORD_FADE:
25308 		/* Does reordering fade after ms time */
25309 		RACK_OPTS_INC(tcp_rack_reord_fade);
25310 		rack->r_ctl.rc_reorder_fade = optval;
25311 		break;
25312 	case TCP_RACK_TLP_THRESH:
25313 		/* RACK TLP theshold i.e. srtt+(srtt/N) */
25314 		RACK_OPTS_INC(tcp_rack_tlp_thresh);
25315 		if (optval)
25316 			rack->r_ctl.rc_tlp_threshold = optval;
25317 		else
25318 			error = EINVAL;
25319 		break;
25320 	case TCP_BBR_USE_RACK_RR:
25321 		RACK_OPTS_INC(tcp_rack_rr);
25322 		if (optval)
25323 			rack->use_rack_rr = 1;
25324 		else
25325 			rack->use_rack_rr = 0;
25326 		break;
25327 	case TCP_RACK_PKT_DELAY:
25328 		/* RACK added ms i.e. rack-rtt + reord + N */
25329 		RACK_OPTS_INC(tcp_rack_pkt_delay);
25330 		rack->r_ctl.rc_pkt_delay = optval;
25331 		break;
25332 	case TCP_DELACK:
25333 		RACK_OPTS_INC(tcp_rack_delayed_ack);
25334 		if (optval == 0)
25335 			tp->t_delayed_ack = 0;
25336 		else
25337 			tp->t_delayed_ack = 1;
25338 		if (tp->t_flags & TF_DELACK) {
25339 			tp->t_flags &= ~TF_DELACK;
25340 			tp->t_flags |= TF_ACKNOW;
25341 			NET_EPOCH_ENTER(et);
25342 			rack_output(tp);
25343 			NET_EPOCH_EXIT(et);
25344 		}
25345 		break;
25346 
25347 	case TCP_BBR_RACK_RTT_USE:
25348 		RACK_OPTS_INC(tcp_rack_rtt_use);
25349 		if ((optval != USE_RTT_HIGH) &&
25350 		    (optval != USE_RTT_LOW) &&
25351 		    (optval != USE_RTT_AVG))
25352 			error = EINVAL;
25353 		else
25354 			rack->r_ctl.rc_rate_sample_method = optval;
25355 		break;
25356 	case TCP_HONOR_HPTS_MIN:
25357 		RACK_OPTS_INC(tcp_honor_hpts);
25358 		if (optval) {
25359 			rack->r_use_hpts_min = 1;
25360 			/*
25361 			 * Must be between 2 - 80% to be a reduction else
25362 			 * we keep the default (10%).
25363 			 */
25364 			if ((optval > 1) && (optval <= 80)) {
25365 				rack->r_ctl.max_reduction = optval;
25366 			}
25367 		} else
25368 			rack->r_use_hpts_min = 0;
25369 		break;
25370 	case TCP_REC_IS_DYN:			/*  URL:dynrec */
25371 		RACK_OPTS_INC(tcp_dyn_rec);
25372 		if (optval)
25373 			rack->rc_gp_no_rec_chg = 1;
25374 		else
25375 			rack->rc_gp_no_rec_chg = 0;
25376 		break;
25377 	case TCP_NO_TIMELY:
25378 		RACK_OPTS_INC(tcp_notimely);
25379 		if (optval) {
25380 			rack->rc_skip_timely = 1;
25381 			rack->r_ctl.rack_per_of_gp_rec = 90;
25382 			rack->r_ctl.rack_per_of_gp_ca = 100;
25383 			rack->r_ctl.rack_per_of_gp_ss = 250;
25384 		} else {
25385 			rack->rc_skip_timely = 0;
25386 		}
25387 		break;
25388 	case TCP_GP_USE_LTBW:
25389 		if (optval == 0) {
25390 			rack->use_lesser_lt_bw = 0;
25391 			rack->dis_lt_bw = 1;
25392 		} else if (optval == 1) {
25393 			rack->use_lesser_lt_bw = 1;
25394 			rack->dis_lt_bw = 0;
25395 		} else if (optval == 2) {
25396 			rack->use_lesser_lt_bw = 0;
25397 			rack->dis_lt_bw = 0;
25398 		}
25399 		break;
25400 	case TCP_DATA_AFTER_CLOSE:
25401 		RACK_OPTS_INC(tcp_data_after_close);
25402 		if (optval)
25403 			rack->rc_allow_data_af_clo = 1;
25404 		else
25405 			rack->rc_allow_data_af_clo = 0;
25406 		break;
25407 	default:
25408 		break;
25409 	}
25410 	tcp_log_socket_option(tp, sopt_name, optval, error);
25411 	return (error);
25412 }
25413 
25414 static void
25415 rack_inherit(struct tcpcb *tp, struct inpcb *parent)
25416 {
25417 	/*
25418 	 * A new connection has been created (tp) and
25419 	 * the parent is the inpcb given. We want to
25420 	 * apply a read-lock to the parent (we are already
25421 	 * holding a write lock on the tp) and copy anything
25422 	 * out of the rack specific data as long as its tfb is
25423 	 * the same as ours i.e. we are the same stack. Otherwise
25424 	 * we just return.
25425 	 */
25426 	struct tcpcb *par;
25427 	struct tcp_rack *dest, *src;
25428 	int cnt = 0;
25429 
25430 	par = intotcpcb(parent);
25431 	if (par->t_fb != tp->t_fb) {
25432 		/* Not the same stack */
25433 		tcp_log_socket_option(tp, 0, 0, 1);
25434 		return;
25435 	}
25436 	/* Ok if we reach here lets setup the two rack pointers */
25437 	dest = (struct tcp_rack *)tp->t_fb_ptr;
25438 	src = (struct tcp_rack *)par->t_fb_ptr;
25439 	if ((src == NULL) || (dest == NULL)) {
25440 		/* Huh? */
25441 		tcp_log_socket_option(tp, 0, 0, 2);
25442 		return;
25443 	}
25444 	/* Now copy out anything we wish to inherit i.e. things in socket-options */
25445 	/* TCP_RACK_PROFILE we can't know but we can set DGP if its on */
25446 	if ((src->dgp_on) && (dest->dgp_on == 0)) {
25447 		/* Profile 1 had to be set via sock opt */
25448 		rack_set_dgp(dest);
25449 		cnt++;
25450 	}
25451 	/* TCP_RACK_SET_RXT_OPTIONS */
25452 	if (dest->full_size_rxt != src->full_size_rxt) {
25453 		dest->full_size_rxt = src->full_size_rxt;
25454 		cnt++;
25455 	}
25456 	if (dest->shape_rxt_to_pacing_min  != src->shape_rxt_to_pacing_min) {
25457 		dest->shape_rxt_to_pacing_min = src->shape_rxt_to_pacing_min;
25458 		cnt++;
25459 	}
25460 	/* TCP_RACK_DSACK_OPT */
25461 	if (dest->rc_rack_tmr_std_based != src->rc_rack_tmr_std_based) {
25462 		dest->rc_rack_tmr_std_based = src->rc_rack_tmr_std_based;
25463 		cnt++;
25464 	}
25465 	if (dest->rc_rack_use_dsack != src->rc_rack_use_dsack) {
25466 		dest->rc_rack_use_dsack = src->rc_rack_use_dsack;
25467 		cnt++;
25468 	}
25469 	/* TCP_RACK_PACING_DIVISOR */
25470 	if (dest->r_ctl.pace_len_divisor != src->r_ctl.pace_len_divisor) {
25471 		dest->r_ctl.pace_len_divisor = src->r_ctl.pace_len_divisor;
25472 		cnt++;
25473 	}
25474 	/* TCP_RACK_HI_BETA */
25475 	if (src->rack_hibeta != dest->rack_hibeta) {
25476 		cnt++;
25477 		if (src->rack_hibeta) {
25478 			dest->r_ctl.rc_saved_beta.beta = src->r_ctl.rc_saved_beta.beta;
25479 			dest->rack_hibeta = 1;
25480 		} else {
25481 			dest->rack_hibeta = 0;
25482 		}
25483 	}
25484 	/* TCP_RACK_TIMER_SLOP */
25485 	if (dest->r_ctl.timer_slop != src->r_ctl.timer_slop) {
25486 		dest->r_ctl.timer_slop = src->r_ctl.timer_slop;
25487 		cnt++;
25488 	}
25489 	/* TCP_RACK_PACING_BETA_ECN */
25490 	if (dest->r_ctl.rc_saved_beta.beta_ecn != src->r_ctl.rc_saved_beta.beta_ecn) {
25491 		dest->r_ctl.rc_saved_beta.beta_ecn = src->r_ctl.rc_saved_beta.beta_ecn;
25492 		cnt++;
25493 	}
25494 	if (dest->r_ctl.rc_saved_beta.newreno_flags != src->r_ctl.rc_saved_beta.newreno_flags) {
25495 		dest->r_ctl.rc_saved_beta.newreno_flags = src->r_ctl.rc_saved_beta.newreno_flags;
25496 		cnt++;
25497 	}
25498 	/* We do not do TCP_DEFER_OPTIONS */
25499 	/* TCP_RACK_MEASURE_CNT */
25500 	if (dest->r_ctl.req_measurements != src->r_ctl.req_measurements) {
25501 		dest->r_ctl.req_measurements = src->r_ctl.req_measurements;
25502 		cnt++;
25503 	}
25504 	/* TCP_HDWR_UP_ONLY */
25505 	if (dest->r_up_only != src->r_up_only) {
25506 		dest->r_up_only = src->r_up_only;
25507 		cnt++;
25508 	}
25509 	/* TCP_FILLCW_RATE_CAP */
25510 	if (dest->r_ctl.fillcw_cap != src->r_ctl.fillcw_cap) {
25511 		dest->r_ctl.fillcw_cap = src->r_ctl.fillcw_cap;
25512 		cnt++;
25513 	}
25514 	/* TCP_PACING_RATE_CAP */
25515 	if (dest->r_ctl.bw_rate_cap != src->r_ctl.bw_rate_cap) {
25516 		dest->r_ctl.bw_rate_cap = src->r_ctl.bw_rate_cap;
25517 		cnt++;
25518 	}
25519 	/* A listener can't set TCP_HYBRID_PACING */
25520 	/* TCP_SIDECHAN_DIS */
25521 	if (dest->r_ctl.side_chan_dis_mask != src->r_ctl.side_chan_dis_mask) {
25522 		dest->r_ctl.side_chan_dis_mask = src->r_ctl.side_chan_dis_mask;
25523 		cnt++;
25524 	}
25525 	/* TCP_SHARED_CWND_TIME_LIMIT */
25526 	if (dest->r_limit_scw != src->r_limit_scw) {
25527 		dest->r_limit_scw = src->r_limit_scw;
25528 		cnt++;
25529 	}
25530 	/* TCP_POLICER_DETECT */
25531 	if (dest->r_ctl.policer_rxt_threshold != src->r_ctl.policer_rxt_threshold) {
25532 		dest->r_ctl.policer_rxt_threshold = src->r_ctl.policer_rxt_threshold;
25533 		cnt++;
25534 	}
25535 	if (dest->r_ctl.policer_avg_threshold != src->r_ctl.policer_avg_threshold) {
25536 		dest->r_ctl.policer_avg_threshold = src->r_ctl.policer_avg_threshold;
25537 		cnt++;
25538 	}
25539 	if (dest->r_ctl.policer_med_threshold != src->r_ctl.policer_med_threshold) {
25540 		dest->r_ctl.policer_med_threshold = src->r_ctl.policer_med_threshold;
25541 		cnt++;
25542 	}
25543 	if (dest->policer_detect_on != src->policer_detect_on) {
25544 		dest->policer_detect_on = src->policer_detect_on;
25545 		cnt++;
25546 	}
25547 
25548 	if (dest->r_ctl.saved_policer_val != src->r_ctl.saved_policer_val) {
25549 		dest->r_ctl.saved_policer_val = src->r_ctl.saved_policer_val;
25550 		cnt++;
25551 	}
25552 	/* TCP_POLICER_MSS */
25553 	if (dest->r_ctl.policer_del_mss != src->r_ctl.policer_del_mss) {
25554 		dest->r_ctl.policer_del_mss = src->r_ctl.policer_del_mss;
25555 		cnt++;
25556 	}
25557 
25558 	if (dest->r_ctl.pol_bw_comp != src->r_ctl.pol_bw_comp) {
25559 		dest->r_ctl.pol_bw_comp = src->r_ctl.pol_bw_comp;
25560 		cnt++;
25561 	}
25562 
25563 	if (dest->r_ctl.policer_alt_median != src->r_ctl.policer_alt_median) {
25564 		dest->r_ctl.policer_alt_median = src->r_ctl.policer_alt_median;
25565 		cnt++;
25566 	}
25567 	/* TCP_RACK_PACE_TO_FILL */
25568 	if (dest->rc_pace_to_cwnd != src->rc_pace_to_cwnd) {
25569 		dest->rc_pace_to_cwnd = src->rc_pace_to_cwnd;
25570 		cnt++;
25571 	}
25572 	if (dest->rc_pace_fill_if_rttin_range != src->rc_pace_fill_if_rttin_range) {
25573 		dest->rc_pace_fill_if_rttin_range = src->rc_pace_fill_if_rttin_range;
25574 		cnt++;
25575 	}
25576 	if (dest->rtt_limit_mul != src->rtt_limit_mul) {
25577 		dest->rtt_limit_mul = src->rtt_limit_mul;
25578 		cnt++;
25579 	}
25580 	/* TCP_RACK_NO_PUSH_AT_MAX */
25581 	if (dest->r_ctl.rc_no_push_at_mrtt != src->r_ctl.rc_no_push_at_mrtt) {
25582 		dest->r_ctl.rc_no_push_at_mrtt = src->r_ctl.rc_no_push_at_mrtt;
25583 		cnt++;
25584 	}
25585 	/* TCP_SHARED_CWND_ENABLE */
25586 	if (dest->rack_enable_scwnd != src->rack_enable_scwnd) {
25587 		dest->rack_enable_scwnd = src->rack_enable_scwnd;
25588 		cnt++;
25589 	}
25590 	/* TCP_USE_CMP_ACKS */
25591 	if (dest->r_use_cmp_ack != src->r_use_cmp_ack) {
25592 		dest->r_use_cmp_ack = src->r_use_cmp_ack;
25593 		cnt++;
25594 	}
25595 
25596 	if (dest->r_mbuf_queue != src->r_mbuf_queue) {
25597 		dest->r_mbuf_queue = src->r_mbuf_queue;
25598 		cnt++;
25599 	}
25600 	/* TCP_RACK_MBUF_QUEUE */
25601 	if (dest->r_mbuf_queue != src->r_mbuf_queue) {
25602 		dest->r_mbuf_queue = src->r_mbuf_queue;
25603 		cnt++;
25604 	}
25605 	if  (dest->r_mbuf_queue || dest->rc_always_pace || dest->r_use_cmp_ack) {
25606 		tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
25607 	} else {
25608 		tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
25609 	}
25610 	if (dest->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state)) {
25611 		tp->t_flags2 |= TF2_MBUF_ACKCMP;
25612 	}
25613 	/* TCP_RACK_NONRXT_CFG_RATE */
25614 	if (dest->rack_rec_nonrxt_use_cr != src->rack_rec_nonrxt_use_cr) {
25615 		dest->rack_rec_nonrxt_use_cr = src->rack_rec_nonrxt_use_cr;
25616 		cnt++;
25617 	}
25618 	/* TCP_NO_PRR */
25619 	if (dest->rack_no_prr != src->rack_no_prr) {
25620 		dest->rack_no_prr = src->rack_no_prr;
25621 		cnt++;
25622 	}
25623 	if (dest->no_prr_addback != src->no_prr_addback) {
25624 		dest->no_prr_addback = src->no_prr_addback;
25625 		cnt++;
25626 	}
25627 	/* RACK_CSPR_IS_FCC */
25628 	if (dest->cspr_is_fcc != src->cspr_is_fcc) {
25629 		dest->cspr_is_fcc = src->cspr_is_fcc;
25630 		cnt++;
25631 	}
25632 	/* TCP_TIMELY_DYN_ADJ */
25633 	if (dest->rc_gp_dyn_mul != src->rc_gp_dyn_mul) {
25634 		dest->rc_gp_dyn_mul = src->rc_gp_dyn_mul;
25635 		cnt++;
25636 	}
25637 	if (dest->r_ctl.rack_per_of_gp_ca != src->r_ctl.rack_per_of_gp_ca) {
25638 		dest->r_ctl.rack_per_of_gp_ca = src->r_ctl.rack_per_of_gp_ca;
25639 		cnt++;
25640 	}
25641 	/* TCP_RACK_DO_DETECTION */
25642 	if (dest->do_detection != src->do_detection) {
25643 		dest->do_detection = src->do_detection;
25644 		cnt++;
25645 	}
25646 	/* TCP_RACK_TLP_USE */
25647 	if (dest->rack_tlp_threshold_use != src->rack_tlp_threshold_use) {
25648 		dest->rack_tlp_threshold_use = src->rack_tlp_threshold_use;
25649 		cnt++;
25650 	}
25651 	/* we don't allow inheritence of TCP_RACK_PACE_ALWAYS */
25652 	/* TCP_BBR_RACK_INIT_RATE */
25653 	if (dest->r_ctl.init_rate != src->r_ctl.init_rate) {
25654 		dest->r_ctl.init_rate = src->r_ctl.init_rate;
25655 		cnt++;
25656 	}
25657 	/* TCP_RACK_FORCE_MSEG */
25658 	if (dest->rc_force_max_seg != src->rc_force_max_seg) {
25659 		dest->rc_force_max_seg = src->rc_force_max_seg;
25660 		cnt++;
25661 	}
25662 	/* TCP_RACK_PACE_MIN_SEG */
25663 	if (dest->r_ctl.rc_user_set_min_segs != src->r_ctl.rc_user_set_min_segs) {
25664 		dest->r_ctl.rc_user_set_min_segs = src->r_ctl.rc_user_set_min_segs;
25665 		cnt++;
25666 	}
25667 	/* we don't allow TCP_RACK_PACE_MAX_SEG */
25668 	/* TCP_RACK_PACE_RATE_REC, TCP_RACK_PACE_RATE_SS,  TCP_RACK_PACE_RATE_CA */
25669 	if (dest->r_ctl.rc_fixed_pacing_rate_ca != src->r_ctl.rc_fixed_pacing_rate_ca) {
25670 		dest->r_ctl.rc_fixed_pacing_rate_ca = src->r_ctl.rc_fixed_pacing_rate_ca;
25671 		cnt++;
25672 	}
25673 	if (dest->r_ctl.rc_fixed_pacing_rate_ss != src->r_ctl.rc_fixed_pacing_rate_ss) {
25674 		dest->r_ctl.rc_fixed_pacing_rate_ss = src->r_ctl.rc_fixed_pacing_rate_ss;
25675 		cnt++;
25676 	}
25677 	if (dest->r_ctl.rc_fixed_pacing_rate_rec != src->r_ctl.rc_fixed_pacing_rate_rec) {
25678 		dest->r_ctl.rc_fixed_pacing_rate_rec = src->r_ctl.rc_fixed_pacing_rate_rec;
25679 		cnt++;
25680 	}
25681 	/* TCP_RACK_GP_INCREASE_REC, TCP_RACK_GP_INCREASE_CA, TCP_RACK_GP_INCREASE_SS */
25682 	if (dest->r_ctl.rack_per_of_gp_rec != src->r_ctl.rack_per_of_gp_rec) {
25683 		dest->r_ctl.rack_per_of_gp_rec = src->r_ctl.rack_per_of_gp_rec;
25684 		cnt++;
25685 	}
25686 	if (dest->r_ctl.rack_per_of_gp_ca != src->r_ctl.rack_per_of_gp_ca) {
25687 		dest->r_ctl.rack_per_of_gp_ca = src->r_ctl.rack_per_of_gp_ca;
25688 		cnt++;
25689 	}
25690 
25691 	if (dest->r_ctl.rack_per_of_gp_ss != src->r_ctl.rack_per_of_gp_ss) {
25692 		dest->r_ctl.rack_per_of_gp_ss = src->r_ctl.rack_per_of_gp_ss;
25693 		cnt++;
25694 	}
25695 	/* TCP_RACK_RR_CONF */
25696 	if (dest->r_rr_config != src->r_rr_config) {
25697 		dest->r_rr_config = src->r_rr_config;
25698 		cnt++;
25699 	}
25700 	/* TCP_PACING_DND */
25701 	if (dest->rc_pace_dnd != src->rc_pace_dnd) {
25702 		dest->rc_pace_dnd = src->rc_pace_dnd;
25703 		cnt++;
25704 	}
25705 	/* TCP_HDWR_RATE_CAP */
25706 	if (dest->r_rack_hw_rate_caps != src->r_rack_hw_rate_caps) {
25707 		dest->r_rack_hw_rate_caps = src->r_rack_hw_rate_caps;
25708 		cnt++;
25709 	}
25710 	/* TCP_DGP_UPPER_BOUNDS */
25711 	if (dest->r_ctl.rack_per_upper_bound_ca != src->r_ctl.rack_per_upper_bound_ca) {
25712 		dest->r_ctl.rack_per_upper_bound_ca = src->r_ctl.rack_per_upper_bound_ca;
25713 		cnt++;
25714 	}
25715 	if (dest->r_ctl.rack_per_upper_bound_ss != src->r_ctl.rack_per_upper_bound_ss) {
25716 		dest->r_ctl.rack_per_upper_bound_ss = src->r_ctl.rack_per_upper_bound_ss;
25717 		cnt++;
25718 	}
25719 	/* TCP_SS_EEXIT */
25720 	if (dest->r_ctl.gp_rnd_thresh != src->r_ctl.gp_rnd_thresh) {
25721 		dest->r_ctl.gp_rnd_thresh = src->r_ctl.gp_rnd_thresh;
25722 		cnt++;
25723 	}
25724 	if (dest->r_ctl.gate_to_fs != src->r_ctl.gate_to_fs) {
25725 		dest->r_ctl.gate_to_fs = src->r_ctl.gate_to_fs;
25726 		cnt++;
25727 	}
25728 	if (dest->r_ctl.use_gp_not_last != src->r_ctl.use_gp_not_last) {
25729 		dest->r_ctl.use_gp_not_last = src->r_ctl.use_gp_not_last;
25730 		cnt++;
25731 	}
25732 	if (dest->r_ctl.gp_gain_req != src->r_ctl.gp_gain_req) {
25733 		dest->r_ctl.gp_gain_req = src->r_ctl.gp_gain_req;
25734 		cnt++;
25735 	}
25736 	/* TCP_BBR_HDWR_PACE */
25737 	if (dest->rack_hdw_pace_ena != src->rack_hdw_pace_ena) {
25738 		dest->rack_hdw_pace_ena = src->rack_hdw_pace_ena;
25739 		cnt++;
25740 	}
25741 	if (dest->rack_attempt_hdwr_pace != src->rack_attempt_hdwr_pace) {
25742 		dest->rack_attempt_hdwr_pace = src->rack_attempt_hdwr_pace;
25743 		cnt++;
25744 	}
25745 	/* TCP_RACK_PRR_SENDALOT */
25746 	if (dest->r_ctl.rc_prr_sendalot != src->r_ctl.rc_prr_sendalot) {
25747 		dest->r_ctl.rc_prr_sendalot = src->r_ctl.rc_prr_sendalot;
25748 		cnt++;
25749 	}
25750 	/* TCP_RACK_MIN_TO */
25751 	if (dest->r_ctl.rc_min_to != src->r_ctl.rc_min_to) {
25752 		dest->r_ctl.rc_min_to = src->r_ctl.rc_min_to;
25753 		cnt++;
25754 	}
25755 	/* TCP_RACK_EARLY_SEG */
25756 	if (dest->r_ctl.rc_early_recovery_segs != src->r_ctl.rc_early_recovery_segs) {
25757 		dest->r_ctl.rc_early_recovery_segs = src->r_ctl.rc_early_recovery_segs;
25758 		cnt++;
25759 	}
25760 	/* TCP_RACK_ENABLE_HYSTART */
25761 	if (par->t_ccv.flags != tp->t_ccv.flags) {
25762 		cnt++;
25763 		if (par->t_ccv.flags & CCF_HYSTART_ALLOWED) {
25764 			tp->t_ccv.flags |= CCF_HYSTART_ALLOWED;
25765 			if (rack_do_hystart > RACK_HYSTART_ON)
25766 				tp->t_ccv.flags |= CCF_HYSTART_CAN_SH_CWND;
25767 			if (rack_do_hystart > RACK_HYSTART_ON_W_SC)
25768 				tp->t_ccv.flags |= CCF_HYSTART_CONS_SSTH;
25769 		} else {
25770 			tp->t_ccv.flags &= ~(CCF_HYSTART_ALLOWED|CCF_HYSTART_CAN_SH_CWND|CCF_HYSTART_CONS_SSTH);
25771 		}
25772 	}
25773 	/* TCP_RACK_REORD_THRESH */
25774 	if (dest->r_ctl.rc_reorder_shift != src->r_ctl.rc_reorder_shift) {
25775 		dest->r_ctl.rc_reorder_shift = src->r_ctl.rc_reorder_shift;
25776 		cnt++;
25777 	}
25778 	/* TCP_RACK_REORD_FADE */
25779 	if (dest->r_ctl.rc_reorder_fade != src->r_ctl.rc_reorder_fade) {
25780 		dest->r_ctl.rc_reorder_fade = src->r_ctl.rc_reorder_fade;
25781 		cnt++;
25782 	}
25783 	/* TCP_RACK_TLP_THRESH */
25784 	if (dest->r_ctl.rc_tlp_threshold != src->r_ctl.rc_tlp_threshold) {
25785 		dest->r_ctl.rc_tlp_threshold = src->r_ctl.rc_tlp_threshold;
25786 		cnt++;
25787 	}
25788 	/* TCP_BBR_USE_RACK_RR */
25789 	if (dest->use_rack_rr != src->use_rack_rr) {
25790 		dest->use_rack_rr = src->use_rack_rr;
25791 		cnt++;
25792 	}
25793 	/* TCP_RACK_PKT_DELAY */
25794 	if (dest->r_ctl.rc_pkt_delay != src->r_ctl.rc_pkt_delay) {
25795 		dest->r_ctl.rc_pkt_delay = src->r_ctl.rc_pkt_delay;
25796 		cnt++;
25797 	}
25798 	/* TCP_DELACK will get copied via the main code if applicable */
25799 	/* TCP_BBR_RACK_RTT_USE */
25800 	if (dest->r_ctl.rc_rate_sample_method != src->r_ctl.rc_rate_sample_method) {
25801 		dest->r_ctl.rc_rate_sample_method = src->r_ctl.rc_rate_sample_method;
25802 		cnt++;
25803 	}
25804 	/* TCP_HONOR_HPTS_MIN */
25805 	if (dest->r_use_hpts_min != src->r_use_hpts_min) {
25806 		dest->r_use_hpts_min = src->r_use_hpts_min;
25807 		cnt++;
25808 	}
25809 	if (dest->r_ctl.max_reduction != src->r_ctl.max_reduction) {
25810 		dest->r_ctl.max_reduction = src->r_ctl.max_reduction;
25811 		cnt++;
25812 	}
25813 	/* TCP_REC_IS_DYN */
25814 	if (dest->rc_gp_no_rec_chg != src->rc_gp_no_rec_chg) {
25815 		dest->rc_gp_no_rec_chg = src->rc_gp_no_rec_chg;
25816 		cnt++;
25817 	}
25818 	if (dest->rc_skip_timely != src->rc_skip_timely) {
25819 		dest->rc_skip_timely = src->rc_skip_timely;
25820 		cnt++;
25821 	}
25822 	/* TCP_DATA_AFTER_CLOSE */
25823 	if (dest->rc_allow_data_af_clo != src->rc_allow_data_af_clo) {
25824 		dest->rc_allow_data_af_clo = src->rc_allow_data_af_clo;
25825 		cnt++;
25826 	}
25827 	/* TCP_GP_USE_LTBW */
25828 	if (src->use_lesser_lt_bw != dest->use_lesser_lt_bw) {
25829 		dest->use_lesser_lt_bw = src->use_lesser_lt_bw;
25830 		cnt++;
25831 	}
25832 	if (dest->dis_lt_bw != src->dis_lt_bw) {
25833 		dest->dis_lt_bw = src->dis_lt_bw;
25834 		cnt++;
25835 	}
25836 	tcp_log_socket_option(tp, 0, cnt, 0);
25837 }
25838 
25839 
25840 static void
25841 rack_apply_deferred_options(struct tcp_rack *rack)
25842 {
25843 	struct deferred_opt_list *dol, *sdol;
25844 	uint32_t s_optval;
25845 
25846 	TAILQ_FOREACH_SAFE(dol, &rack->r_ctl.opt_list, next, sdol) {
25847 		TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next);
25848 		/* Disadvantage of deferal is you loose the error return */
25849 		s_optval = (uint32_t)dol->optval;
25850 		(void)rack_process_option(rack->rc_tp, rack, dol->optname, s_optval, dol->optval, NULL);
25851 		free(dol, M_TCPDO);
25852 	}
25853 }
25854 
25855 static void
25856 rack_hw_tls_change(struct tcpcb *tp, int chg)
25857 {
25858 	/* Update HW tls state */
25859 	struct tcp_rack *rack;
25860 
25861 	rack = (struct tcp_rack *)tp->t_fb_ptr;
25862 	if (chg)
25863 		rack->r_ctl.fsb.hw_tls = 1;
25864 	else
25865 		rack->r_ctl.fsb.hw_tls = 0;
25866 }
25867 
25868 static int
25869 rack_pru_options(struct tcpcb *tp, int flags)
25870 {
25871 	if (flags & PRUS_OOB)
25872 		return (EOPNOTSUPP);
25873 	return (0);
25874 }
25875 
25876 static bool
25877 rack_wake_check(struct tcpcb *tp)
25878 {
25879 	struct tcp_rack *rack;
25880 	struct timeval tv;
25881 	uint32_t cts;
25882 
25883 	rack = (struct tcp_rack *)tp->t_fb_ptr;
25884 	if (rack->r_ctl.rc_hpts_flags) {
25885 		cts = tcp_get_usecs(&tv);
25886 		if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == PACE_PKT_OUTPUT){
25887 			/*
25888 			 * Pacing timer is up, check if we are ready.
25889 			 */
25890 			if (TSTMP_GEQ(cts, rack->r_ctl.rc_last_output_to))
25891 				return (true);
25892 		} else if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) != 0) {
25893 			/*
25894 			 * A timer is up, check if we are ready.
25895 			 */
25896 			if (TSTMP_GEQ(cts, rack->r_ctl.rc_timer_exp))
25897 				return (true);
25898 		}
25899 	}
25900 	return (false);
25901 }
25902 
25903 static struct tcp_function_block __tcp_rack = {
25904 	.tfb_tcp_block_name = __XSTRING(STACKNAME),
25905 	.tfb_tcp_output = rack_output,
25906 	.tfb_do_queued_segments = ctf_do_queued_segments,
25907 	.tfb_do_segment_nounlock = rack_do_segment_nounlock,
25908 	.tfb_tcp_do_segment = rack_do_segment,
25909 	.tfb_tcp_ctloutput = rack_ctloutput,
25910 	.tfb_tcp_fb_init = rack_init,
25911 	.tfb_tcp_fb_fini = rack_fini,
25912 	.tfb_tcp_timer_stop_all = rack_stopall,
25913 	.tfb_tcp_rexmit_tmr = rack_remxt_tmr,
25914 	.tfb_tcp_handoff_ok = rack_handoff_ok,
25915 	.tfb_tcp_mtu_chg = rack_mtu_change,
25916 	.tfb_pru_options = rack_pru_options,
25917 	.tfb_hwtls_change = rack_hw_tls_change,
25918 	.tfb_chg_query = rack_chg_query,
25919 	.tfb_switch_failed = rack_switch_failed,
25920 	.tfb_early_wake_check = rack_wake_check,
25921 	.tfb_compute_pipe = rack_compute_pipe,
25922 	.tfb_stack_info = rack_stack_information,
25923 	.tfb_inherit = rack_inherit,
25924 	.tfb_flags = TCP_FUNC_OUTPUT_CANDROP,
25925 
25926 };
25927 
25928 /*
25929  * rack_ctloutput() must drop the inpcb lock before performing copyin on
25930  * socket option arguments.  When it re-acquires the lock after the copy, it
25931  * has to revalidate that the connection is still valid for the socket
25932  * option.
25933  */
25934 static int
25935 rack_set_sockopt(struct tcpcb *tp, struct sockopt *sopt)
25936 {
25937 	struct inpcb *inp = tptoinpcb(tp);
25938 #ifdef INET
25939 	struct ip *ip;
25940 #endif
25941 	struct tcp_rack *rack;
25942 	struct tcp_hybrid_req hybrid;
25943 	uint64_t loptval;
25944 	int32_t error = 0, optval;
25945 
25946 	rack = (struct tcp_rack *)tp->t_fb_ptr;
25947 	if (rack == NULL) {
25948 		INP_WUNLOCK(inp);
25949 		return (EINVAL);
25950 	}
25951 #ifdef INET
25952 	ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
25953 #endif
25954 
25955 	switch (sopt->sopt_level) {
25956 #ifdef INET6
25957 	case IPPROTO_IPV6:
25958 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
25959 		switch (sopt->sopt_name) {
25960 		case IPV6_USE_MIN_MTU:
25961 			tcp6_use_min_mtu(tp);
25962 			break;
25963 		}
25964 		INP_WUNLOCK(inp);
25965 		return (0);
25966 #endif
25967 #ifdef INET
25968 	case IPPROTO_IP:
25969 		switch (sopt->sopt_name) {
25970 		case IP_TOS:
25971 			/*
25972 			 * The DSCP codepoint has changed, update the fsb.
25973 			 */
25974 			ip->ip_tos = rack->rc_inp->inp_ip_tos;
25975 			break;
25976 		case IP_TTL:
25977 			/*
25978 			 * The TTL has changed, update the fsb.
25979 			 */
25980 			ip->ip_ttl = rack->rc_inp->inp_ip_ttl;
25981 			break;
25982 		}
25983 		INP_WUNLOCK(inp);
25984 		return (0);
25985 #endif
25986 #ifdef SO_PEERPRIO
25987 	case SOL_SOCKET:
25988 		switch (sopt->sopt_name) {
25989 		case SO_PEERPRIO:			/*  SC-URL:bs */
25990 			/* Already read in and sanity checked in sosetopt(). */
25991 			if (inp->inp_socket) {
25992 				rack->client_bufferlvl = inp->inp_socket->so_peerprio;
25993 			}
25994 			break;
25995 		}
25996 		INP_WUNLOCK(inp);
25997 		return (0);
25998 #endif
25999 	case IPPROTO_TCP:
26000 		switch (sopt->sopt_name) {
26001 		case TCP_RACK_TLP_REDUCE:		/*  URL:tlp_reduce */
26002 		/*  Pacing related ones */
26003 		case TCP_RACK_PACE_ALWAYS:		/*  URL:pace_always */
26004 		case TCP_BBR_RACK_INIT_RATE:		/*  URL:irate */
26005 		case TCP_RACK_PACE_MIN_SEG:		/*  URL:pace_min_seg */
26006 		case TCP_RACK_PACE_MAX_SEG:		/*  URL:pace_max_seg */
26007 		case TCP_RACK_FORCE_MSEG:		/*  URL:force_max_seg */
26008 		case TCP_RACK_PACE_RATE_CA:		/*  URL:pr_ca */
26009 		case TCP_RACK_PACE_RATE_SS:		/*  URL:pr_ss*/
26010 		case TCP_RACK_PACE_RATE_REC:		/*  URL:pr_rec */
26011 		case TCP_RACK_GP_INCREASE_CA:		/*  URL:gp_inc_ca */
26012 		case TCP_RACK_GP_INCREASE_SS:		/*  URL:gp_inc_ss */
26013 		case TCP_RACK_GP_INCREASE_REC:		/*  URL:gp_inc_rec */
26014 		case TCP_RACK_RR_CONF:			/*  URL:rrr_conf */
26015 		case TCP_BBR_HDWR_PACE:			/*  URL:hdwrpace */
26016 		case TCP_HDWR_RATE_CAP:			/*  URL:hdwrcap boolean */
26017 		case TCP_PACING_RATE_CAP:		/*  URL:cap  -- used by side-channel */
26018 		case TCP_HDWR_UP_ONLY:			/*  URL:uponly -- hardware pacing  boolean */
26019 		case TCP_FILLCW_RATE_CAP:		/*  URL:fillcw_cap */
26020 		case TCP_RACK_PACING_BETA_ECN:		/*  URL:pacing_beta_ecn */
26021 		case TCP_RACK_PACE_TO_FILL:		/*  URL:fillcw */
26022 			/* End pacing related */
26023 		case TCP_POLICER_DETECT:		/*  URL:pol_det */
26024 		case TCP_POLICER_MSS:			/*  URL:pol_mss */
26025 		case TCP_DELACK:			/*  URL:delack (in base TCP i.e. tcp_hints along with cc etc ) */
26026 		case TCP_RACK_PRR_SENDALOT:		/*  URL:prr_sendalot */
26027 		case TCP_RACK_MIN_TO:			/*  URL:min_to */
26028 		case TCP_RACK_EARLY_SEG:		/*  URL:early_seg */
26029 		case TCP_RACK_REORD_THRESH:		/*  URL:reord_thresh */
26030 		case TCP_RACK_REORD_FADE:		/*  URL:reord_fade */
26031 		case TCP_RACK_TLP_THRESH:		/*  URL:tlp_thresh */
26032 		case TCP_RACK_PKT_DELAY:		/*  URL:pkt_delay */
26033 		case TCP_RACK_TLP_USE:			/*  URL:tlp_use */
26034 		case TCP_BBR_RACK_RTT_USE:		/*  URL:rttuse */
26035 		case TCP_BBR_USE_RACK_RR:		/*  URL:rackrr */
26036 		case TCP_RACK_DO_DETECTION:		/*  URL:detect */
26037 		case TCP_NO_PRR:			/*  URL:noprr */
26038 		case TCP_TIMELY_DYN_ADJ:      		/*  URL:dynamic */
26039 		case TCP_DATA_AFTER_CLOSE:		/*  no URL */
26040 		case TCP_RACK_NONRXT_CFG_RATE:		/*  URL:nonrxtcr */
26041 		case TCP_SHARED_CWND_ENABLE:		/*  URL:scwnd */
26042 		case TCP_RACK_MBUF_QUEUE:		/*  URL:mqueue */
26043 		case TCP_RACK_NO_PUSH_AT_MAX:		/*  URL:npush */
26044 		case TCP_SHARED_CWND_TIME_LIMIT:	/*  URL:lscwnd */
26045 		case TCP_RACK_PROFILE:			/*  URL:profile */
26046 		case TCP_SIDECHAN_DIS:			/*  URL:scodm */
26047 		case TCP_HYBRID_PACING:			/*  URL:pacing=hybrid */
26048 		case TCP_USE_CMP_ACKS:			/*  URL:cmpack */
26049 		case TCP_RACK_ABC_VAL:			/*  URL:labc */
26050 		case TCP_REC_ABC_VAL:			/*  URL:reclabc */
26051 		case TCP_RACK_MEASURE_CNT:		/*  URL:measurecnt */
26052 		case TCP_DEFER_OPTIONS:			/*  URL:defer */
26053 		case TCP_RACK_DSACK_OPT:		/*  URL:dsack */
26054 		case TCP_RACK_TIMER_SLOP:		/*  URL:timer_slop */
26055 		case TCP_RACK_ENABLE_HYSTART:		/*  URL:hystart */
26056 		case TCP_RACK_SET_RXT_OPTIONS:		/*  URL:rxtsz */
26057 		case TCP_RACK_HI_BETA:			/*  URL:hibeta */
26058 		case TCP_RACK_SPLIT_LIMIT:		/*  URL:split */
26059 		case TCP_SS_EEXIT:			/*  URL:eexit */
26060 		case TCP_DGP_UPPER_BOUNDS:		/*  URL:upper */
26061 		case TCP_RACK_PACING_DIVISOR:		/*  URL:divisor */
26062 		case TCP_PACING_DND:			/*  URL:dnd */
26063 		case TCP_NO_TIMELY:			/*  URL:notimely */
26064 		case RACK_CSPR_IS_FCC:			/*  URL:csprisfcc */
26065 		case TCP_HONOR_HPTS_MIN:		/*  URL:hptsmin */
26066 		case TCP_REC_IS_DYN:			/*  URL:dynrec */
26067 		case TCP_GP_USE_LTBW:			/*  URL:useltbw */
26068 			goto process_opt;
26069 			break;
26070 		default:
26071 			/* Filter off all unknown options to the base stack */
26072 			return (tcp_default_ctloutput(tp, sopt));
26073 			break;
26074 		}
26075 	default:
26076 		INP_WUNLOCK(inp);
26077 		return (0);
26078 	}
26079 process_opt:
26080 	INP_WUNLOCK(inp);
26081 	if ((sopt->sopt_name == TCP_PACING_RATE_CAP) ||
26082 	    (sopt->sopt_name == TCP_FILLCW_RATE_CAP)) {
26083 		error = sooptcopyin(sopt, &loptval, sizeof(loptval), sizeof(loptval));
26084 		/*
26085 		 * We truncate it down to 32 bits for the socket-option trace this
26086 		 * means rates > 34Gbps won't show right, but thats probably ok.
26087 		 */
26088 		optval = (uint32_t)loptval;
26089 	} else if (sopt->sopt_name == TCP_HYBRID_PACING) {
26090 		error = sooptcopyin(sopt, &hybrid, sizeof(hybrid), sizeof(hybrid));
26091 	} else {
26092 		error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
26093 		/* Save it in 64 bit form too */
26094 		loptval = optval;
26095 	}
26096 	if (error)
26097 		return (error);
26098 	INP_WLOCK(inp);
26099 	if (tp->t_fb != &__tcp_rack) {
26100 		INP_WUNLOCK(inp);
26101 		return (ENOPROTOOPT);
26102 	}
26103 	if (rack->defer_options && (rack->gp_ready == 0) &&
26104 	    (sopt->sopt_name != TCP_DEFER_OPTIONS) &&
26105 	    (sopt->sopt_name != TCP_HYBRID_PACING) &&
26106 	    (sopt->sopt_name != TCP_RACK_SET_RXT_OPTIONS) &&
26107 	    (sopt->sopt_name != TCP_RACK_PACING_BETA_ECN) &&
26108 	    (sopt->sopt_name != TCP_RACK_MEASURE_CNT)) {
26109 		/* Options are being deferred */
26110 		if (rack_add_deferred_option(rack, sopt->sopt_name, loptval)) {
26111 			INP_WUNLOCK(inp);
26112 			return (0);
26113 		} else {
26114 			/* No memory to defer, fail */
26115 			INP_WUNLOCK(inp);
26116 			return (ENOMEM);
26117 		}
26118 	}
26119 	error = rack_process_option(tp, rack, sopt->sopt_name, optval, loptval, &hybrid);
26120 	INP_WUNLOCK(inp);
26121 	return (error);
26122 }
26123 
26124 static void
26125 rack_fill_info(struct tcpcb *tp, struct tcp_info *ti)
26126 {
26127 
26128 	INP_WLOCK_ASSERT(tptoinpcb(tp));
26129 	bzero(ti, sizeof(*ti));
26130 
26131 	ti->tcpi_state = tp->t_state;
26132 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
26133 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
26134 	if (tp->t_flags & TF_SACK_PERMIT)
26135 		ti->tcpi_options |= TCPI_OPT_SACK;
26136 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
26137 		ti->tcpi_options |= TCPI_OPT_WSCALE;
26138 		ti->tcpi_snd_wscale = tp->snd_scale;
26139 		ti->tcpi_rcv_wscale = tp->rcv_scale;
26140 	}
26141 	if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
26142 		ti->tcpi_options |= TCPI_OPT_ECN;
26143 	if (tp->t_flags & TF_FASTOPEN)
26144 		ti->tcpi_options |= TCPI_OPT_TFO;
26145 	/* still kept in ticks is t_rcvtime */
26146 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
26147 	/* Since we hold everything in precise useconds this is easy */
26148 	ti->tcpi_rtt = tp->t_srtt;
26149 	ti->tcpi_rttvar = tp->t_rttvar;
26150 	ti->tcpi_rto = tp->t_rxtcur;
26151 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
26152 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
26153 	/*
26154 	 * FreeBSD-specific extension fields for tcp_info.
26155 	 */
26156 	ti->tcpi_rcv_space = tp->rcv_wnd;
26157 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
26158 	ti->tcpi_snd_wnd = tp->snd_wnd;
26159 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
26160 	ti->tcpi_snd_nxt = tp->snd_nxt;
26161 	ti->tcpi_snd_mss = tp->t_maxseg;
26162 	ti->tcpi_rcv_mss = tp->t_maxseg;
26163 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
26164 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
26165 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
26166 	ti->tcpi_total_tlp = tp->t_sndtlppack;
26167 	ti->tcpi_total_tlp_bytes = tp->t_sndtlpbyte;
26168 	ti->tcpi_rttmin = tp->t_rttlow;
26169 #ifdef NETFLIX_STATS
26170 	memcpy(&ti->tcpi_rxsyninfo, &tp->t_rxsyninfo, sizeof(struct tcpsyninfo));
26171 #endif
26172 #ifdef TCP_OFFLOAD
26173 	if (tp->t_flags & TF_TOE) {
26174 		ti->tcpi_options |= TCPI_OPT_TOE;
26175 		tcp_offload_tcp_info(tp, ti);
26176 	}
26177 #endif
26178 }
26179 
26180 static int
26181 rack_get_sockopt(struct tcpcb *tp, struct sockopt *sopt)
26182 {
26183 	struct inpcb *inp = tptoinpcb(tp);
26184 	struct tcp_rack *rack;
26185 	int32_t error, optval;
26186 	uint64_t val, loptval;
26187 	struct	tcp_info ti;
26188 	/*
26189 	 * Because all our options are either boolean or an int, we can just
26190 	 * pull everything into optval and then unlock and copy. If we ever
26191 	 * add a option that is not a int, then this will have quite an
26192 	 * impact to this routine.
26193 	 */
26194 	error = 0;
26195 	rack = (struct tcp_rack *)tp->t_fb_ptr;
26196 	if (rack == NULL) {
26197 		INP_WUNLOCK(inp);
26198 		return (EINVAL);
26199 	}
26200 	switch (sopt->sopt_name) {
26201 	case TCP_INFO:
26202 		/* First get the info filled */
26203 		rack_fill_info(tp, &ti);
26204 		/* Fix up the rtt related fields if needed */
26205 		INP_WUNLOCK(inp);
26206 		error = sooptcopyout(sopt, &ti, sizeof ti);
26207 		return (error);
26208 	/*
26209 	 * Beta is the congestion control value for NewReno that influences how
26210 	 * much of a backoff happens when loss is detected. It is normally set
26211 	 * to 50 for 50% i.e. the cwnd is reduced to 50% of its previous value
26212 	 * when you exit recovery.
26213 	 */
26214 	case TCP_RACK_PACING_BETA:
26215 		break;
26216 		/*
26217 		 * Beta_ecn is the congestion control value for NewReno that influences how
26218 		 * much of a backoff happens when a ECN mark is detected. It is normally set
26219 		 * to 80 for 80% i.e. the cwnd is reduced by 20% of its previous value when
26220 		 * you exit recovery. Note that classic ECN has a beta of 50, it is only
26221 		 * ABE Ecn that uses this "less" value, but we do too with pacing :)
26222 		 */
26223 
26224 	case TCP_RACK_PACING_BETA_ECN:
26225 		if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0)
26226 			error = EINVAL;
26227 		else if (rack->rc_pacing_cc_set == 0)
26228 			optval = rack->r_ctl.rc_saved_beta.beta_ecn;
26229 		else {
26230 			/*
26231 			 * Reach out into the CC data and report back what
26232 			 * I have previously set. Yeah it looks hackish but
26233 			 * we don't want to report the saved values.
26234 			 */
26235 			if (tp->t_ccv.cc_data)
26236 				optval = ((struct newreno *)tp->t_ccv.cc_data)->beta_ecn;
26237 			else
26238 				error = EINVAL;
26239 		}
26240 		break;
26241 	case TCP_RACK_DSACK_OPT:
26242 		optval = 0;
26243 		if (rack->rc_rack_tmr_std_based) {
26244 			optval |= 1;
26245 		}
26246 		if (rack->rc_rack_use_dsack) {
26247 			optval |= 2;
26248 		}
26249 		break;
26250 	case TCP_RACK_ENABLE_HYSTART:
26251 	{
26252 		if (tp->t_ccv.flags & CCF_HYSTART_ALLOWED) {
26253 			optval = RACK_HYSTART_ON;
26254 			if (tp->t_ccv.flags & CCF_HYSTART_CAN_SH_CWND)
26255 				optval = RACK_HYSTART_ON_W_SC;
26256 			if (tp->t_ccv.flags & CCF_HYSTART_CONS_SSTH)
26257 				optval = RACK_HYSTART_ON_W_SC_C;
26258 		} else {
26259 			optval = RACK_HYSTART_OFF;
26260 		}
26261 	}
26262 	break;
26263 	case TCP_RACK_DGP_IN_REC:
26264 		error = EINVAL;
26265 		break;
26266 	case TCP_RACK_HI_BETA:
26267 		optval = rack->rack_hibeta;
26268 		break;
26269 	case TCP_POLICER_MSS:
26270 		optval = rack->r_ctl.policer_del_mss;
26271 		break;
26272 	case TCP_POLICER_DETECT:
26273 		optval = rack->r_ctl.saved_policer_val;
26274 		break;
26275 	case TCP_DEFER_OPTIONS:
26276 		optval = rack->defer_options;
26277 		break;
26278 	case TCP_RACK_MEASURE_CNT:
26279 		optval = rack->r_ctl.req_measurements;
26280 		break;
26281 	case TCP_REC_ABC_VAL:
26282 		optval = rack->r_use_labc_for_rec;
26283 		break;
26284 	case TCP_RACK_ABC_VAL:
26285 		optval = rack->rc_labc;
26286 		break;
26287 	case TCP_HDWR_UP_ONLY:
26288 		optval= rack->r_up_only;
26289 		break;
26290 	case TCP_FILLCW_RATE_CAP:
26291 		loptval = rack->r_ctl.fillcw_cap;
26292 		break;
26293 	case TCP_PACING_RATE_CAP:
26294 		loptval = rack->r_ctl.bw_rate_cap;
26295 		break;
26296 	case TCP_RACK_PROFILE:
26297 		/* You cannot retrieve a profile, its write only */
26298 		error = EINVAL;
26299 		break;
26300 	case TCP_SIDECHAN_DIS:
26301 		optval = rack->r_ctl.side_chan_dis_mask;
26302 		break;
26303 	case TCP_HYBRID_PACING:
26304 		/* You cannot retrieve hybrid pacing information, its write only */
26305 		error = EINVAL;
26306 		break;
26307 	case TCP_USE_CMP_ACKS:
26308 		optval = rack->r_use_cmp_ack;
26309 		break;
26310 	case TCP_RACK_PACE_TO_FILL:
26311 		optval = rack->rc_pace_to_cwnd;
26312 		break;
26313 	case TCP_RACK_NO_PUSH_AT_MAX:
26314 		optval = rack->r_ctl.rc_no_push_at_mrtt;
26315 		break;
26316 	case TCP_SHARED_CWND_ENABLE:
26317 		optval = rack->rack_enable_scwnd;
26318 		break;
26319 	case TCP_RACK_NONRXT_CFG_RATE:
26320 		optval = rack->rack_rec_nonrxt_use_cr;
26321 		break;
26322 	case TCP_NO_PRR:
26323 		if (rack->rack_no_prr  == 1)
26324 			optval = 1;
26325 		else if (rack->no_prr_addback == 1)
26326 			optval = 2;
26327 		else
26328 			optval = 0;
26329 		break;
26330 	case TCP_GP_USE_LTBW:
26331 		if (rack->dis_lt_bw) {
26332 			/* It is not used */
26333 			optval = 0;
26334 		} else if (rack->use_lesser_lt_bw) {
26335 			/* we use min() */
26336 			optval = 1;
26337 		} else {
26338 			/* we use max() */
26339 			optval = 2;
26340 		}
26341 		break;
26342 	case TCP_RACK_DO_DETECTION:
26343 		optval = rack->do_detection;
26344 		break;
26345 	case TCP_RACK_MBUF_QUEUE:
26346 		/* Now do we use the LRO mbuf-queue feature */
26347 		optval = rack->r_mbuf_queue;
26348 		break;
26349 	case RACK_CSPR_IS_FCC:
26350 		optval = rack->cspr_is_fcc;
26351 		break;
26352 	case TCP_TIMELY_DYN_ADJ:
26353 		optval = rack->rc_gp_dyn_mul;
26354 		break;
26355 	case TCP_BBR_IWINTSO:
26356 		error = EINVAL;
26357 		break;
26358 	case TCP_RACK_TLP_REDUCE:
26359 		/* RACK TLP cwnd reduction (bool) */
26360 		optval = rack->r_ctl.rc_tlp_cwnd_reduce;
26361 		break;
26362 	case TCP_BBR_RACK_INIT_RATE:
26363 		val = rack->r_ctl.init_rate;
26364 		/* convert to kbits per sec */
26365 		val *= 8;
26366 		val /= 1000;
26367 		optval = (uint32_t)val;
26368 		break;
26369 	case TCP_RACK_FORCE_MSEG:
26370 		optval = rack->rc_force_max_seg;
26371 		break;
26372 	case TCP_RACK_PACE_MIN_SEG:
26373 		optval = rack->r_ctl.rc_user_set_min_segs;
26374 		break;
26375 	case TCP_RACK_PACE_MAX_SEG:
26376 		/* Max segments in a pace */
26377 		optval = rack->rc_user_set_max_segs;
26378 		break;
26379 	case TCP_RACK_PACE_ALWAYS:
26380 		/* Use the always pace method */
26381 		optval = rack->rc_always_pace;
26382 		break;
26383 	case TCP_RACK_PRR_SENDALOT:
26384 		/* Allow PRR to send more than one seg */
26385 		optval = rack->r_ctl.rc_prr_sendalot;
26386 		break;
26387 	case TCP_RACK_MIN_TO:
26388 		/* Minimum time between rack t-o's in ms */
26389 		optval = rack->r_ctl.rc_min_to;
26390 		break;
26391 	case TCP_RACK_SPLIT_LIMIT:
26392 		optval = rack->r_ctl.rc_split_limit;
26393 		break;
26394 	case TCP_RACK_EARLY_SEG:
26395 		/* If early recovery max segments */
26396 		optval = rack->r_ctl.rc_early_recovery_segs;
26397 		break;
26398 	case TCP_RACK_REORD_THRESH:
26399 		/* RACK reorder threshold (shift amount) */
26400 		optval = rack->r_ctl.rc_reorder_shift;
26401 		break;
26402 	case TCP_SS_EEXIT:
26403 		if (rack->r_ctl.gp_rnd_thresh) {
26404 			uint32_t v;
26405 
26406 			v = rack->r_ctl.gp_gain_req;
26407 			v <<= 17;
26408 			optval = v | (rack->r_ctl.gp_rnd_thresh & 0xff);
26409 			if (rack->r_ctl.gate_to_fs == 1)
26410 				optval |= 0x10000;
26411 		} else
26412 			optval = 0;
26413 		break;
26414 	case TCP_RACK_REORD_FADE:
26415 		/* Does reordering fade after ms time */
26416 		optval = rack->r_ctl.rc_reorder_fade;
26417 		break;
26418 	case TCP_BBR_USE_RACK_RR:
26419 		/* Do we use the rack cheat for rxt */
26420 		optval = rack->use_rack_rr;
26421 		break;
26422 	case TCP_RACK_RR_CONF:
26423 		optval = rack->r_rr_config;
26424 		break;
26425 	case TCP_HDWR_RATE_CAP:
26426 		optval = rack->r_rack_hw_rate_caps;
26427 		break;
26428 	case TCP_BBR_HDWR_PACE:
26429 		optval = rack->rack_hdw_pace_ena;
26430 		break;
26431 	case TCP_RACK_TLP_THRESH:
26432 		/* RACK TLP theshold i.e. srtt+(srtt/N) */
26433 		optval = rack->r_ctl.rc_tlp_threshold;
26434 		break;
26435 	case TCP_RACK_PKT_DELAY:
26436 		/* RACK added ms i.e. rack-rtt + reord + N */
26437 		optval = rack->r_ctl.rc_pkt_delay;
26438 		break;
26439 	case TCP_RACK_TLP_USE:
26440 		optval = rack->rack_tlp_threshold_use;
26441 		break;
26442 	case TCP_PACING_DND:
26443 		optval = rack->rc_pace_dnd;
26444 		break;
26445 	case TCP_RACK_PACE_RATE_CA:
26446 		optval = rack->r_ctl.rc_fixed_pacing_rate_ca;
26447 		break;
26448 	case TCP_RACK_PACE_RATE_SS:
26449 		optval = rack->r_ctl.rc_fixed_pacing_rate_ss;
26450 		break;
26451 	case TCP_RACK_PACE_RATE_REC:
26452 		optval = rack->r_ctl.rc_fixed_pacing_rate_rec;
26453 		break;
26454 	case TCP_DGP_UPPER_BOUNDS:
26455 		optval = rack->r_ctl.rack_per_upper_bound_ss;
26456 		optval <<= 16;
26457 		optval |= rack->r_ctl.rack_per_upper_bound_ca;
26458 		break;
26459 	case TCP_RACK_GP_INCREASE_SS:
26460 		optval = rack->r_ctl.rack_per_of_gp_ca;
26461 		break;
26462 	case TCP_RACK_GP_INCREASE_CA:
26463 		optval = rack->r_ctl.rack_per_of_gp_ss;
26464 		break;
26465 	case TCP_RACK_PACING_DIVISOR:
26466 		optval = rack->r_ctl.pace_len_divisor;
26467 		break;
26468 	case TCP_BBR_RACK_RTT_USE:
26469 		optval = rack->r_ctl.rc_rate_sample_method;
26470 		break;
26471 	case TCP_DELACK:
26472 		optval = tp->t_delayed_ack;
26473 		break;
26474 	case TCP_DATA_AFTER_CLOSE:
26475 		optval = rack->rc_allow_data_af_clo;
26476 		break;
26477 	case TCP_SHARED_CWND_TIME_LIMIT:
26478 		optval = rack->r_limit_scw;
26479 		break;
26480 	case TCP_HONOR_HPTS_MIN:
26481 		if (rack->r_use_hpts_min)
26482 			optval = rack->r_ctl.max_reduction;
26483 		else
26484 			optval = 0;
26485 		break;
26486 	case TCP_REC_IS_DYN:
26487 		optval = rack->rc_gp_no_rec_chg;
26488 		break;
26489 	case TCP_NO_TIMELY:
26490 		optval = rack->rc_skip_timely;
26491 		break;
26492 	case TCP_RACK_TIMER_SLOP:
26493 		optval = rack->r_ctl.timer_slop;
26494 		break;
26495 	default:
26496 		return (tcp_default_ctloutput(tp, sopt));
26497 		break;
26498 	}
26499 	INP_WUNLOCK(inp);
26500 	if (error == 0) {
26501 		if ((sopt->sopt_name == TCP_PACING_RATE_CAP) ||
26502 		    (sopt->sopt_name == TCP_FILLCW_RATE_CAP))
26503 			error = sooptcopyout(sopt, &loptval, sizeof loptval);
26504 		else
26505 			error = sooptcopyout(sopt, &optval, sizeof optval);
26506 	}
26507 	return (error);
26508 }
26509 
26510 static int
26511 rack_ctloutput(struct tcpcb *tp, struct sockopt *sopt)
26512 {
26513 	if (sopt->sopt_dir == SOPT_SET) {
26514 		return (rack_set_sockopt(tp, sopt));
26515 	} else if (sopt->sopt_dir == SOPT_GET) {
26516 		return (rack_get_sockopt(tp, sopt));
26517 	} else {
26518 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
26519 	}
26520 }
26521 
26522 static const char *rack_stack_names[] = {
26523 	__XSTRING(STACKNAME),
26524 #ifdef STACKALIAS
26525 	__XSTRING(STACKALIAS),
26526 #endif
26527 };
26528 
26529 static int
26530 rack_ctor(void *mem, int32_t size, void *arg, int32_t how)
26531 {
26532 	memset(mem, 0, size);
26533 	return (0);
26534 }
26535 
26536 static void
26537 rack_dtor(void *mem, int32_t size, void *arg)
26538 {
26539 
26540 }
26541 
26542 static bool rack_mod_inited = false;
26543 
26544 static int
26545 tcp_addrack(module_t mod, int32_t type, void *data)
26546 {
26547 	int32_t err = 0;
26548 	int num_stacks;
26549 
26550 	switch (type) {
26551 	case MOD_LOAD:
26552 		rack_zone = uma_zcreate(__XSTRING(MODNAME) "_map",
26553 		    sizeof(struct rack_sendmap),
26554 		    rack_ctor, rack_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
26555 
26556 		rack_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb",
26557 		    sizeof(struct tcp_rack),
26558 		    rack_ctor, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
26559 
26560 		sysctl_ctx_init(&rack_sysctl_ctx);
26561 		rack_sysctl_root = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
26562 		    SYSCTL_STATIC_CHILDREN(_net_inet_tcp),
26563 		    OID_AUTO,
26564 #ifdef STACKALIAS
26565 		    __XSTRING(STACKALIAS),
26566 #else
26567 		    __XSTRING(STACKNAME),
26568 #endif
26569 		    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
26570 		    "");
26571 		if (rack_sysctl_root == NULL) {
26572 			printf("Failed to add sysctl node\n");
26573 			err = EFAULT;
26574 			goto free_uma;
26575 		}
26576 		rack_init_sysctls();
26577 		num_stacks = nitems(rack_stack_names);
26578 		err = register_tcp_functions_as_names(&__tcp_rack, M_WAITOK,
26579 		    rack_stack_names, &num_stacks);
26580 		if (err) {
26581 			printf("Failed to register %s stack name for "
26582 			    "%s module\n", rack_stack_names[num_stacks],
26583 			    __XSTRING(MODNAME));
26584 			sysctl_ctx_free(&rack_sysctl_ctx);
26585 free_uma:
26586 			uma_zdestroy(rack_zone);
26587 			uma_zdestroy(rack_pcb_zone);
26588 			rack_counter_destroy();
26589 			printf("Failed to register rack module -- err:%d\n", err);
26590 			return (err);
26591 		}
26592 		tcp_lro_reg_mbufq();
26593 		rack_mod_inited = true;
26594 		break;
26595 	case MOD_QUIESCE:
26596 		err = deregister_tcp_functions(&__tcp_rack, true, false);
26597 		break;
26598 	case MOD_UNLOAD:
26599 		err = deregister_tcp_functions(&__tcp_rack, false, true);
26600 		if (err == EBUSY)
26601 			break;
26602 		if (rack_mod_inited) {
26603 			uma_zdestroy(rack_zone);
26604 			uma_zdestroy(rack_pcb_zone);
26605 			sysctl_ctx_free(&rack_sysctl_ctx);
26606 			rack_counter_destroy();
26607 			rack_mod_inited = false;
26608 		}
26609 		tcp_lro_dereg_mbufq();
26610 		err = 0;
26611 		break;
26612 	default:
26613 		return (EOPNOTSUPP);
26614 	}
26615 	return (err);
26616 }
26617 
26618 static moduledata_t tcp_rack = {
26619 	.name = __XSTRING(MODNAME),
26620 	.evhand = tcp_addrack,
26621 	.priv = 0
26622 };
26623 
26624 MODULE_VERSION(MODNAME, 1);
26625 DECLARE_MODULE(MODNAME, tcp_rack, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
26626 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1);
26627 
26628 #endif /* #if !defined(INET) && !defined(INET6) */
26629