xref: /freebsd/sys/netinet/tcp_stacks/rack.c (revision f7220c486c1bf858e97c2d8e5c4c9dac4947d50a)
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 __FBSDID("$FreeBSD$");
29 
30 #include "opt_inet.h"
31 #include "opt_inet6.h"
32 #include "opt_ipsec.h"
33 #include "opt_tcpdebug.h"
34 #include "opt_ratelimit.h"
35 #include "opt_kern_tls.h"
36 #include <sys/param.h>
37 #include <sys/arb.h>
38 #include <sys/module.h>
39 #include <sys/kernel.h>
40 #ifdef TCP_HHOOK
41 #include <sys/hhook.h>
42 #endif
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/mbuf.h>
48 #include <sys/proc.h>		/* for proc0 declaration */
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
53 #ifdef STATS
54 #include <sys/qmath.h>
55 #include <sys/tree.h>
56 #include <sys/stats.h> /* Must come after qmath.h and tree.h */
57 #else
58 #include <sys/tree.h>
59 #endif
60 #include <sys/refcount.h>
61 #include <sys/queue.h>
62 #include <sys/tim_filter.h>
63 #include <sys/smp.h>
64 #include <sys/kthread.h>
65 #include <sys/kern_prefetch.h>
66 #include <sys/protosw.h>
67 #ifdef TCP_ACCOUNTING
68 #include <sys/sched.h>
69 #include <machine/cpu.h>
70 #endif
71 #include <vm/uma.h>
72 
73 #include <net/route.h>
74 #include <net/route/nhop.h>
75 #include <net/vnet.h>
76 
77 #define TCPSTATES		/* for logging */
78 
79 #include <netinet/in.h>
80 #include <netinet/in_kdtrace.h>
81 #include <netinet/in_pcb.h>
82 #include <netinet/ip.h>
83 #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
84 #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
85 #include <netinet/ip_var.h>
86 #include <netinet/ip6.h>
87 #include <netinet6/in6_pcb.h>
88 #include <netinet6/ip6_var.h>
89 #include <netinet/tcp.h>
90 #define	TCPOUTFLAGS
91 #include <netinet/tcp_fsm.h>
92 #include <netinet/tcp_log_buf.h>
93 #include <netinet/tcp_seq.h>
94 #include <netinet/tcp_timer.h>
95 #include <netinet/tcp_var.h>
96 #include <netinet/tcp_syncache.h>
97 #include <netinet/tcp_hpts.h>
98 #include <netinet/tcp_ratelimit.h>
99 #include <netinet/tcp_accounting.h>
100 #include <netinet/tcpip.h>
101 #include <netinet/cc/cc.h>
102 #include <netinet/cc/cc_newreno.h>
103 #include <netinet/tcp_fastopen.h>
104 #include <netinet/tcp_lro.h>
105 #ifdef NETFLIX_SHARED_CWND
106 #include <netinet/tcp_shared_cwnd.h>
107 #endif
108 #ifdef TCPDEBUG
109 #include <netinet/tcp_debug.h>
110 #endif				/* TCPDEBUG */
111 #ifdef TCP_OFFLOAD
112 #include <netinet/tcp_offload.h>
113 #endif
114 #ifdef INET6
115 #include <netinet6/tcp6_var.h>
116 #endif
117 #include <netinet/tcp_ecn.h>
118 
119 #include <netipsec/ipsec_support.h>
120 
121 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
122 #include <netipsec/ipsec.h>
123 #include <netipsec/ipsec6.h>
124 #endif				/* IPSEC */
125 
126 #include <netinet/udp.h>
127 #include <netinet/udp_var.h>
128 #include <machine/in_cksum.h>
129 
130 #ifdef MAC
131 #include <security/mac/mac_framework.h>
132 #endif
133 #include "sack_filter.h"
134 #include "tcp_rack.h"
135 #include "rack_bbr_common.h"
136 
137 uma_zone_t rack_zone;
138 uma_zone_t rack_pcb_zone;
139 
140 #ifndef TICKS2SBT
141 #define	TICKS2SBT(__t)	(tick_sbt * ((sbintime_t)(__t)))
142 #endif
143 
144 VNET_DECLARE(uint32_t, newreno_beta);
145 VNET_DECLARE(uint32_t, newreno_beta_ecn);
146 #define V_newreno_beta VNET(newreno_beta)
147 #define V_newreno_beta_ecn VNET(newreno_beta_ecn)
148 
149 
150 MALLOC_DEFINE(M_TCPFSB, "tcp_fsb", "TCP fast send block");
151 MALLOC_DEFINE(M_TCPDO, "tcp_do", "TCP deferred options");
152 
153 struct sysctl_ctx_list rack_sysctl_ctx;
154 struct sysctl_oid *rack_sysctl_root;
155 
156 #define CUM_ACKED 1
157 #define SACKED 2
158 
159 /*
160  * The RACK module incorporates a number of
161  * TCP ideas that have been put out into the IETF
162  * over the last few years:
163  * - Matt Mathis's Rate Halving which slowly drops
164  *    the congestion window so that the ack clock can
165  *    be maintained during a recovery.
166  * - Yuchung Cheng's RACK TCP (for which its named) that
167  *    will stop us using the number of dup acks and instead
168  *    use time as the gage of when we retransmit.
169  * - Reorder Detection of RFC4737 and the Tail-Loss probe draft
170  *    of Dukkipati et.al.
171  * RACK depends on SACK, so if an endpoint arrives that
172  * cannot do SACK the state machine below will shuttle the
173  * connection back to using the "default" TCP stack that is
174  * in FreeBSD.
175  *
176  * To implement RACK the original TCP stack was first decomposed
177  * into a functional state machine with individual states
178  * for each of the possible TCP connection states. The do_segment
179  * functions role in life is to mandate the connection supports SACK
180  * initially and then assure that the RACK state matches the conenction
181  * state before calling the states do_segment function. Each
182  * state is simplified due to the fact that the original do_segment
183  * has been decomposed and we *know* what state we are in (no
184  * switches on the state) and all tests for SACK are gone. This
185  * greatly simplifies what each state does.
186  *
187  * TCP output is also over-written with a new version since it
188  * must maintain the new rack scoreboard.
189  *
190  */
191 static int32_t rack_tlp_thresh = 1;
192 static int32_t rack_tlp_limit = 2;	/* No more than 2 TLPs w-out new data */
193 static int32_t rack_tlp_use_greater = 1;
194 static int32_t rack_reorder_thresh = 2;
195 static int32_t rack_reorder_fade = 60000000;	/* 0 - never fade, def 60,000,000
196 						 * - 60 seconds */
197 static uint8_t rack_req_measurements = 1;
198 /* Attack threshold detections */
199 static uint32_t rack_highest_sack_thresh_seen = 0;
200 static uint32_t rack_highest_move_thresh_seen = 0;
201 static int32_t rack_enable_hw_pacing = 0; /* Due to CCSP keep it off by default */
202 static int32_t rack_hw_pace_extra_slots = 2;	/* 2 extra MSS time betweens */
203 static int32_t rack_hw_rate_caps = 1; /* 1; */
204 static int32_t rack_hw_rate_min = 0; /* 1500000;*/
205 static int32_t rack_hw_rate_to_low = 0; /* 1200000; */
206 static int32_t rack_hw_up_only = 1;
207 static int32_t rack_stats_gets_ms_rtt = 1;
208 static int32_t rack_prr_addbackmax = 2;
209 static int32_t rack_do_hystart = 0;
210 static int32_t rack_apply_rtt_with_reduced_conf = 0;
211 
212 static int32_t rack_pkt_delay = 1000;
213 static int32_t rack_send_a_lot_in_prr = 1;
214 static int32_t rack_min_to = 1000;	/* Number of microsecond  min timeout */
215 static int32_t rack_verbose_logging = 0;
216 static int32_t rack_ignore_data_after_close = 1;
217 static int32_t rack_enable_shared_cwnd = 1;
218 static int32_t rack_use_cmp_acks = 1;
219 static int32_t rack_use_fsb = 1;
220 static int32_t rack_use_rfo = 1;
221 static int32_t rack_use_rsm_rfo = 1;
222 static int32_t rack_max_abc_post_recovery = 2;
223 static int32_t rack_client_low_buf = 0;
224 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 */
225 #ifdef TCP_ACCOUNTING
226 static int32_t rack_tcp_accounting = 0;
227 #endif
228 static int32_t rack_limits_scwnd = 1;
229 static int32_t rack_enable_mqueue_for_nonpaced = 0;
230 static int32_t rack_disable_prr = 0;
231 static int32_t use_rack_rr = 1;
232 static int32_t rack_non_rxt_use_cr = 0; /* does a non-rxt in recovery use the configured rate (ss/ca)? */
233 static int32_t rack_persist_min = 250000;	/* 250usec */
234 static int32_t rack_persist_max = 2000000;	/* 2 Second in usec's */
235 static int32_t rack_sack_not_required = 1;	/* set to one to allow non-sack to use rack */
236 static int32_t rack_default_init_window = 0;	/* Use system default */
237 static int32_t rack_limit_time_with_srtt = 0;
238 static int32_t rack_autosndbuf_inc = 20;	/* In percentage form */
239 static int32_t rack_enobuf_hw_boost_mult = 2;	/* How many times the hw rate we boost slot using time_between */
240 static int32_t rack_enobuf_hw_max = 12000;	/* 12 ms in usecs */
241 static int32_t rack_enobuf_hw_min = 10000;	/* 10 ms in usecs */
242 static int32_t rack_hw_rwnd_factor = 2;		/* How many max_segs the rwnd must be before we hold off sending */
243 /*
244  * Currently regular tcp has a rto_min of 30ms
245  * the backoff goes 12 times so that ends up
246  * being a total of 122.850 seconds before a
247  * connection is killed.
248  */
249 static uint32_t rack_def_data_window = 20;
250 static uint32_t rack_goal_bdp = 2;
251 static uint32_t rack_min_srtts = 1;
252 static uint32_t rack_min_measure_usec = 0;
253 static int32_t rack_tlp_min = 10000;	/* 10ms */
254 static int32_t rack_rto_min = 30000;	/* 30,000 usec same as main freebsd */
255 static int32_t rack_rto_max = 4000000;	/* 4 seconds in usec's */
256 static const int32_t rack_free_cache = 2;
257 static int32_t rack_hptsi_segments = 40;
258 static int32_t rack_rate_sample_method = USE_RTT_LOW;
259 static int32_t rack_pace_every_seg = 0;
260 static int32_t rack_delayed_ack_time = 40000;	/* 40ms in usecs */
261 static int32_t rack_slot_reduction = 4;
262 static int32_t rack_wma_divisor = 8;		/* For WMA calculation */
263 static int32_t rack_cwnd_block_ends_measure = 0;
264 static int32_t rack_rwnd_block_ends_measure = 0;
265 static int32_t rack_def_profile = 0;
266 
267 static int32_t rack_lower_cwnd_at_tlp = 0;
268 static int32_t rack_limited_retran = 0;
269 static int32_t rack_always_send_oldest = 0;
270 static int32_t rack_tlp_threshold_use = TLP_USE_TWO_ONE;
271 
272 static uint16_t rack_per_of_gp_ss = 250;	/* 250 % slow-start */
273 static uint16_t rack_per_of_gp_ca = 200;	/* 200 % congestion-avoidance */
274 static uint16_t rack_per_of_gp_rec = 200;	/* 200 % of bw */
275 
276 /* Probertt */
277 static uint16_t rack_per_of_gp_probertt = 60;	/* 60% of bw */
278 static uint16_t rack_per_of_gp_lowthresh = 40;	/* 40% is bottom */
279 static uint16_t rack_per_of_gp_probertt_reduce = 10; /* 10% reduction */
280 static uint16_t rack_atexit_prtt_hbp = 130;	/* Clamp to 130% on exit prtt if highly buffered path */
281 static uint16_t rack_atexit_prtt = 130;	/* Clamp to 100% on exit prtt if non highly buffered path */
282 
283 static uint32_t rack_max_drain_wait = 2;	/* How man gp srtt's before we give up draining */
284 static uint32_t rack_must_drain = 1;		/* How many GP srtt's we *must* wait */
285 static uint32_t rack_probertt_use_min_rtt_entry = 1;	/* Use the min to calculate the goal else gp_srtt */
286 static uint32_t rack_probertt_use_min_rtt_exit = 0;
287 static uint32_t rack_probe_rtt_sets_cwnd = 0;
288 static uint32_t rack_probe_rtt_safety_val = 2000000;	/* No more than 2 sec in probe-rtt */
289 static uint32_t rack_time_between_probertt = 9600000;	/* 9.6 sec in usecs */
290 static uint32_t rack_probertt_gpsrtt_cnt_mul = 0;	/* How many srtt periods does probe-rtt last top fraction */
291 static uint32_t rack_probertt_gpsrtt_cnt_div = 0;	/* How many srtt periods does probe-rtt last bottom fraction */
292 static uint32_t rack_min_probertt_hold = 40000;		/* Equal to delayed ack time */
293 static uint32_t rack_probertt_filter_life = 10000000;
294 static uint32_t rack_probertt_lower_within = 10;
295 static uint32_t rack_min_rtt_movement = 250000;	/* Must move at least 250ms (in microseconds)  to count as a lowering */
296 static int32_t rack_pace_one_seg = 0;		/* Shall we pace for less than 1.4Meg 1MSS at a time */
297 static int32_t rack_probertt_clear_is = 1;
298 static int32_t rack_max_drain_hbp = 1;		/* Extra drain times gpsrtt for highly buffered paths */
299 static int32_t rack_hbp_thresh = 3;		/* what is the divisor max_rtt/min_rtt to decided a hbp */
300 
301 /* Part of pacing */
302 static int32_t rack_max_per_above = 30;		/* When we go to increment stop if above 100+this% */
303 
304 /* Timely information */
305 /* Combine these two gives the range of 'no change' to bw */
306 /* ie the up/down provide the upper and lower bound */
307 static int32_t rack_gp_per_bw_mul_up = 2;	/* 2% */
308 static int32_t rack_gp_per_bw_mul_down = 4;	/* 4% */
309 static int32_t rack_gp_rtt_maxmul = 3;		/* 3 x maxmin */
310 static int32_t rack_gp_rtt_minmul = 1;		/* minrtt + (minrtt/mindiv) is lower rtt */
311 static int32_t rack_gp_rtt_mindiv = 4;		/* minrtt + (minrtt * minmul/mindiv) is lower rtt */
312 static int32_t rack_gp_decrease_per = 20;	/* 20% decrease in multipler */
313 static int32_t rack_gp_increase_per = 2;	/* 2% increase in multipler */
314 static int32_t rack_per_lower_bound = 50;	/* Don't allow to drop below this multiplier */
315 static int32_t rack_per_upper_bound_ss = 0;	/* Don't allow SS to grow above this */
316 static int32_t rack_per_upper_bound_ca = 0;	/* Don't allow CA to grow above this */
317 static int32_t rack_do_dyn_mul = 0;		/* Are the rack gp multipliers dynamic */
318 static int32_t rack_gp_no_rec_chg = 1;		/* Prohibit recovery from reducing it's multiplier */
319 static int32_t rack_timely_dec_clear = 6;	/* Do we clear decrement count at a value (6)? */
320 static int32_t rack_timely_max_push_rise = 3;	/* One round of pushing */
321 static int32_t rack_timely_max_push_drop = 3;	/* Three round of pushing */
322 static int32_t rack_timely_min_segs = 4;	/* 4 segment minimum */
323 static int32_t rack_use_max_for_nobackoff = 0;
324 static int32_t rack_timely_int_timely_only = 0;	/* do interim timely's only use the timely algo (no b/w changes)? */
325 static int32_t rack_timely_no_stopping = 0;
326 static int32_t rack_down_raise_thresh = 100;
327 static int32_t rack_req_segs = 1;
328 static uint64_t rack_bw_rate_cap = 0;
329 
330 /* Weird delayed ack mode */
331 static int32_t rack_use_imac_dack = 0;
332 /* Rack specific counters */
333 counter_u64_t rack_badfr;
334 counter_u64_t rack_badfr_bytes;
335 counter_u64_t rack_rtm_prr_retran;
336 counter_u64_t rack_rtm_prr_newdata;
337 counter_u64_t rack_timestamp_mismatch;
338 counter_u64_t rack_reorder_seen;
339 counter_u64_t rack_paced_segments;
340 counter_u64_t rack_unpaced_segments;
341 counter_u64_t rack_calc_zero;
342 counter_u64_t rack_calc_nonzero;
343 counter_u64_t rack_saw_enobuf;
344 counter_u64_t rack_saw_enobuf_hw;
345 counter_u64_t rack_saw_enetunreach;
346 counter_u64_t rack_per_timer_hole;
347 counter_u64_t rack_large_ackcmp;
348 counter_u64_t rack_small_ackcmp;
349 counter_u64_t rack_persists_sends;
350 counter_u64_t rack_persists_acks;
351 counter_u64_t rack_persists_loss;
352 counter_u64_t rack_persists_lost_ends;
353 #ifdef INVARIANTS
354 counter_u64_t rack_adjust_map_bw;
355 #endif
356 /* Tail loss probe counters */
357 counter_u64_t rack_tlp_tot;
358 counter_u64_t rack_tlp_newdata;
359 counter_u64_t rack_tlp_retran;
360 counter_u64_t rack_tlp_retran_bytes;
361 counter_u64_t rack_tlp_retran_fail;
362 counter_u64_t rack_to_tot;
363 counter_u64_t rack_to_arm_rack;
364 counter_u64_t rack_to_arm_tlp;
365 counter_u64_t rack_hot_alloc;
366 counter_u64_t rack_to_alloc;
367 counter_u64_t rack_to_alloc_hard;
368 counter_u64_t rack_to_alloc_emerg;
369 counter_u64_t rack_to_alloc_limited;
370 counter_u64_t rack_alloc_limited_conns;
371 counter_u64_t rack_split_limited;
372 
373 #define MAX_NUM_OF_CNTS 13
374 counter_u64_t rack_proc_comp_ack[MAX_NUM_OF_CNTS];
375 counter_u64_t rack_multi_single_eq;
376 counter_u64_t rack_proc_non_comp_ack;
377 
378 counter_u64_t rack_fto_send;
379 counter_u64_t rack_fto_rsm_send;
380 counter_u64_t rack_nfto_resend;
381 counter_u64_t rack_non_fto_send;
382 counter_u64_t rack_extended_rfo;
383 
384 counter_u64_t rack_sack_proc_all;
385 counter_u64_t rack_sack_proc_short;
386 counter_u64_t rack_sack_proc_restart;
387 counter_u64_t rack_sack_attacks_detected;
388 counter_u64_t rack_sack_attacks_reversed;
389 counter_u64_t rack_sack_used_next_merge;
390 counter_u64_t rack_sack_splits;
391 counter_u64_t rack_sack_used_prev_merge;
392 counter_u64_t rack_sack_skipped_acked;
393 counter_u64_t rack_ack_total;
394 counter_u64_t rack_express_sack;
395 counter_u64_t rack_sack_total;
396 counter_u64_t rack_move_none;
397 counter_u64_t rack_move_some;
398 
399 counter_u64_t rack_used_tlpmethod;
400 counter_u64_t rack_used_tlpmethod2;
401 counter_u64_t rack_enter_tlp_calc;
402 counter_u64_t rack_input_idle_reduces;
403 counter_u64_t rack_collapsed_win;
404 counter_u64_t rack_tlp_does_nada;
405 counter_u64_t rack_try_scwnd;
406 counter_u64_t rack_hw_pace_init_fail;
407 counter_u64_t rack_hw_pace_lost;
408 counter_u64_t rack_sbsndptr_right;
409 counter_u64_t rack_sbsndptr_wrong;
410 
411 /* Temp CPU counters */
412 counter_u64_t rack_find_high;
413 
414 counter_u64_t rack_progress_drops;
415 counter_u64_t rack_out_size[TCP_MSS_ACCT_SIZE];
416 counter_u64_t rack_opts_arry[RACK_OPTS_SIZE];
417 
418 
419 #define	RACK_REXMTVAL(tp) max(rack_rto_min, ((tp)->t_srtt + ((tp)->t_rttvar << 2)))
420 
421 #define	RACK_TCPT_RANGESET(tv, value, tvmin, tvmax, slop) do {	\
422 	(tv) = (value) + slop;	 \
423 	if ((u_long)(tv) < (u_long)(tvmin)) \
424 		(tv) = (tvmin); \
425 	if ((u_long)(tv) > (u_long)(tvmax)) \
426 		(tv) = (tvmax); \
427 } while (0)
428 
429 static void
430 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick,  int event, int line);
431 
432 static int
433 rack_process_ack(struct mbuf *m, struct tcphdr *th,
434     struct socket *so, struct tcpcb *tp, struct tcpopt *to,
435     uint32_t tiwin, int32_t tlen, int32_t * ofia, int32_t thflags, int32_t * ret_val);
436 static int
437 rack_process_data(struct mbuf *m, struct tcphdr *th,
438     struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
439     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt);
440 static void
441 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack,
442    uint32_t th_ack, uint16_t nsegs, uint16_t type, int32_t recovery);
443 static struct rack_sendmap *rack_alloc(struct tcp_rack *rack);
444 static struct rack_sendmap *rack_alloc_limit(struct tcp_rack *rack,
445     uint8_t limit_type);
446 static struct rack_sendmap *
447 rack_check_recovery_mode(struct tcpcb *tp,
448     uint32_t tsused);
449 static void
450 rack_cong_signal(struct tcpcb *tp,
451 		 uint32_t type, uint32_t ack);
452 static void rack_counter_destroy(void);
453 static int
454 rack_ctloutput(struct inpcb *inp, struct sockopt *sopt);
455 static int32_t rack_ctor(void *mem, int32_t size, void *arg, int32_t how);
456 static void
457 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override);
458 static void
459 rack_do_segment(struct mbuf *m, struct tcphdr *th,
460     struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
461     uint8_t iptos);
462 static void rack_dtor(void *mem, int32_t size, void *arg);
463 static void
464 rack_log_alt_to_to_cancel(struct tcp_rack *rack,
465     uint32_t flex1, uint32_t flex2,
466     uint32_t flex3, uint32_t flex4,
467     uint32_t flex5, uint32_t flex6,
468     uint16_t flex7, uint8_t mod);
469 
470 static void
471 rack_log_pacing_delay_calc(struct tcp_rack *rack, uint32_t len, uint32_t slot,
472    uint64_t bw_est, uint64_t bw, uint64_t len_time, int method, int line,
473    struct rack_sendmap *rsm, uint8_t quality);
474 static struct rack_sendmap *
475 rack_find_high_nonack(struct tcp_rack *rack,
476     struct rack_sendmap *rsm);
477 static struct rack_sendmap *rack_find_lowest_rsm(struct tcp_rack *rack);
478 static void rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm);
479 static void rack_fini(struct tcpcb *tp, int32_t tcb_is_purged);
480 static int rack_get_sockopt(struct inpcb *inp, struct sockopt *sopt);
481 static void
482 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
483 			    tcp_seq th_ack, int line, uint8_t quality);
484 static uint32_t
485 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss);
486 static int32_t rack_handoff_ok(struct tcpcb *tp);
487 static int32_t rack_init(struct tcpcb *tp);
488 static void rack_init_sysctls(void);
489 static void
490 rack_log_ack(struct tcpcb *tp, struct tcpopt *to,
491     struct tcphdr *th, int entered_rec, int dup_ack_struck);
492 static void
493 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
494     uint32_t seq_out, uint16_t th_flags, int32_t err, uint64_t ts,
495     struct rack_sendmap *hintrsm, uint16_t add_flags, struct mbuf *s_mb, uint32_t s_moff, int hw_tls);
496 
497 static void
498 rack_log_sack_passed(struct tcpcb *tp, struct tcp_rack *rack,
499     struct rack_sendmap *rsm);
500 static void rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm);
501 static int32_t rack_output(struct tcpcb *tp);
502 
503 static uint32_t
504 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack,
505     struct sackblk *sack, struct tcpopt *to, struct rack_sendmap **prsm,
506     uint32_t cts, int *moved_two);
507 static void rack_post_recovery(struct tcpcb *tp, uint32_t th_seq);
508 static void rack_remxt_tmr(struct tcpcb *tp);
509 static int rack_set_sockopt(struct inpcb *inp, struct sockopt *sopt);
510 static void rack_set_state(struct tcpcb *tp, struct tcp_rack *rack);
511 static int32_t rack_stopall(struct tcpcb *tp);
512 static void
513 rack_timer_activate(struct tcpcb *tp, uint32_t timer_type,
514     uint32_t delta);
515 static int32_t rack_timer_active(struct tcpcb *tp, uint32_t timer_type);
516 static void rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line);
517 static void rack_timer_stop(struct tcpcb *tp, uint32_t timer_type);
518 static uint32_t
519 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack,
520     struct rack_sendmap *rsm, uint64_t ts, int32_t * lenp, uint16_t add_flag);
521 static void
522 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
523     struct rack_sendmap *rsm, uint64_t ts, uint16_t add_flag);
524 static int
525 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack,
526     struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack);
527 static int32_t tcp_addrack(module_t mod, int32_t type, void *data);
528 static int
529 rack_do_close_wait(struct mbuf *m, struct tcphdr *th,
530     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
531     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
532 static int
533 rack_do_closing(struct mbuf *m, struct tcphdr *th,
534     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
535     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
536 static int
537 rack_do_established(struct mbuf *m, struct tcphdr *th,
538     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
539     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
540 static int
541 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th,
542     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
543     int32_t tlen, uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos);
544 static int
545 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th,
546     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
547     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
548 static int
549 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th,
550     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
551     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
552 static int
553 rack_do_lastack(struct mbuf *m, struct tcphdr *th,
554     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
555     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
556 static int
557 rack_do_syn_recv(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 static int
561 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th,
562     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
563     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
564 struct rack_sendmap *
565 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack,
566     uint32_t tsused);
567 static void tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt,
568     uint32_t len, uint32_t us_tim, int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt);
569 static void
570      tcp_rack_partialack(struct tcpcb *tp);
571 static int
572 rack_set_profile(struct tcp_rack *rack, int prof);
573 static void
574 rack_apply_deferred_options(struct tcp_rack *rack);
575 
576 int32_t rack_clear_counter=0;
577 
578 static void
579 rack_set_cc_pacing(struct tcp_rack *rack)
580 {
581 	struct sockopt sopt;
582 	struct cc_newreno_opts opt;
583 	struct newreno old, *ptr;
584 	struct tcpcb *tp;
585 	int error;
586 
587 	if (rack->rc_pacing_cc_set)
588 		return;
589 
590 	tp = rack->rc_tp;
591 	if (tp->cc_algo == NULL) {
592 		/* Tcb is leaving */
593 		printf("No cc algorithm?\n");
594 		return;
595 	}
596 	rack->rc_pacing_cc_set = 1;
597 	if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0) {
598 		/* Not new-reno we can't play games with beta! */
599 		goto out;
600 	}
601 	ptr = ((struct newreno *)tp->ccv->cc_data);
602 	if (CC_ALGO(tp)->ctl_output == NULL)  {
603 		/* Huh, why does new_reno no longer have a set function? */
604 		goto out;
605 	}
606 	if (ptr == NULL) {
607 		/* Just the default values */
608 		old.beta = V_newreno_beta_ecn;
609 		old.beta_ecn = V_newreno_beta_ecn;
610 		old.newreno_flags = 0;
611 	} else {
612 		old.beta = ptr->beta;
613 		old.beta_ecn = ptr->beta_ecn;
614 		old.newreno_flags = ptr->newreno_flags;
615 	}
616 	sopt.sopt_valsize = sizeof(struct cc_newreno_opts);
617 	sopt.sopt_dir = SOPT_SET;
618 	opt.name = CC_NEWRENO_BETA;
619 	opt.val = rack->r_ctl.rc_saved_beta.beta;
620 	error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
621 	if (error)  {
622 		goto out;
623 	}
624 	/*
625 	 * Hack alert we need to set in our newreno_flags
626 	 * so that Abe behavior is also applied.
627 	 */
628 	((struct newreno *)tp->ccv->cc_data)->newreno_flags |= CC_NEWRENO_BETA_ECN_ENABLED;
629 	opt.name = CC_NEWRENO_BETA_ECN;
630 	opt.val = rack->r_ctl.rc_saved_beta.beta_ecn;
631 	error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
632 	if (error) {
633 		goto out;
634 	}
635 	/* Save off the original values for restoral */
636 	memcpy(&rack->r_ctl.rc_saved_beta, &old, sizeof(struct newreno));
637 out:
638 	if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
639 		union tcp_log_stackspecific log;
640 		struct timeval tv;
641 
642 		ptr = ((struct newreno *)tp->ccv->cc_data);
643 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
644 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
645 		if (ptr) {
646 			log.u_bbr.flex1 = ptr->beta;
647 			log.u_bbr.flex2 = ptr->beta_ecn;
648 			log.u_bbr.flex3 = ptr->newreno_flags;
649 		}
650 		log.u_bbr.flex4 = rack->r_ctl.rc_saved_beta.beta;
651 		log.u_bbr.flex5 = rack->r_ctl.rc_saved_beta.beta_ecn;
652 		log.u_bbr.flex6 = rack->r_ctl.rc_saved_beta.newreno_flags;
653 		log.u_bbr.flex7 = rack->gp_ready;
654 		log.u_bbr.flex7 <<= 1;
655 		log.u_bbr.flex7 |= rack->use_fixed_rate;
656 		log.u_bbr.flex7 <<= 1;
657 		log.u_bbr.flex7 |= rack->rc_pacing_cc_set;
658 		log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
659 		log.u_bbr.flex8 = 3;
660 		tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, error,
661 			       0, &log, false, NULL, NULL, 0, &tv);
662 	}
663 }
664 
665 static void
666 rack_undo_cc_pacing(struct tcp_rack *rack)
667 {
668 	struct newreno old, *ptr;
669 	struct tcpcb *tp;
670 
671 	if (rack->rc_pacing_cc_set == 0)
672 		return;
673 	tp = rack->rc_tp;
674 	rack->rc_pacing_cc_set = 0;
675 	if (tp->cc_algo == NULL)
676 		/* Tcb is leaving */
677 		return;
678 	if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0) {
679 		/* Not new-reno nothing to do! */
680 		return;
681 	}
682 	ptr = ((struct newreno *)tp->ccv->cc_data);
683 	if (ptr == NULL) {
684 		/*
685 		 * This happens at rack_fini() if the
686 		 * cc module gets freed on us. In that
687 		 * case we loose our "new" settings but
688 		 * thats ok, since the tcb is going away anyway.
689 		 */
690 		return;
691 	}
692 	/* Grab out our set values */
693 	memcpy(&old, ptr, sizeof(struct newreno));
694 	/* Copy back in the original values */
695 	memcpy(ptr, &rack->r_ctl.rc_saved_beta, sizeof(struct newreno));
696 	/* Now save back the values we had set in (for when pacing is restored) */
697 	memcpy(&rack->r_ctl.rc_saved_beta, &old, sizeof(struct newreno));
698 	if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
699 		union tcp_log_stackspecific log;
700 		struct timeval tv;
701 
702 		ptr = ((struct newreno *)tp->ccv->cc_data);
703 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
704 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
705 		log.u_bbr.flex1 = ptr->beta;
706 		log.u_bbr.flex2 = ptr->beta_ecn;
707 		log.u_bbr.flex3 = ptr->newreno_flags;
708 		log.u_bbr.flex4 = rack->r_ctl.rc_saved_beta.beta;
709 		log.u_bbr.flex5 = rack->r_ctl.rc_saved_beta.beta_ecn;
710 		log.u_bbr.flex6 = rack->r_ctl.rc_saved_beta.newreno_flags;
711 		log.u_bbr.flex7 = rack->gp_ready;
712 		log.u_bbr.flex7 <<= 1;
713 		log.u_bbr.flex7 |= rack->use_fixed_rate;
714 		log.u_bbr.flex7 <<= 1;
715 		log.u_bbr.flex7 |= rack->rc_pacing_cc_set;
716 		log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
717 		log.u_bbr.flex8 = 4;
718 		tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
719 			       0, &log, false, NULL, NULL, 0, &tv);
720 	}
721 }
722 
723 #ifdef NETFLIX_PEAKRATE
724 static inline void
725 rack_update_peakrate_thr(struct tcpcb *tp)
726 {
727 	/* Keep in mind that t_maxpeakrate is in B/s. */
728 	uint64_t peak;
729 	peak = uqmax((tp->t_maxseg * 2),
730 		     (((uint64_t)tp->t_maxpeakrate * (uint64_t)(tp->t_srtt)) / (uint64_t)HPTS_USEC_IN_SEC));
731 	tp->t_peakrate_thr = (uint32_t)uqmin(peak, UINT32_MAX);
732 }
733 #endif
734 
735 static int
736 sysctl_rack_clear(SYSCTL_HANDLER_ARGS)
737 {
738 	uint32_t stat;
739 	int32_t error;
740 	int i;
741 
742 	error = SYSCTL_OUT(req, &rack_clear_counter, sizeof(uint32_t));
743 	if (error || req->newptr == NULL)
744 		return error;
745 
746 	error = SYSCTL_IN(req, &stat, sizeof(uint32_t));
747 	if (error)
748 		return (error);
749 	if (stat == 1) {
750 #ifdef INVARIANTS
751 		printf("Clearing RACK counters\n");
752 #endif
753 		counter_u64_zero(rack_badfr);
754 		counter_u64_zero(rack_badfr_bytes);
755 		counter_u64_zero(rack_rtm_prr_retran);
756 		counter_u64_zero(rack_rtm_prr_newdata);
757 		counter_u64_zero(rack_timestamp_mismatch);
758 		counter_u64_zero(rack_reorder_seen);
759 		counter_u64_zero(rack_tlp_tot);
760 		counter_u64_zero(rack_tlp_newdata);
761 		counter_u64_zero(rack_tlp_retran);
762 		counter_u64_zero(rack_tlp_retran_bytes);
763 		counter_u64_zero(rack_tlp_retran_fail);
764 		counter_u64_zero(rack_to_tot);
765 		counter_u64_zero(rack_to_arm_rack);
766 		counter_u64_zero(rack_to_arm_tlp);
767 		counter_u64_zero(rack_paced_segments);
768 		counter_u64_zero(rack_calc_zero);
769 		counter_u64_zero(rack_calc_nonzero);
770 		counter_u64_zero(rack_unpaced_segments);
771 		counter_u64_zero(rack_saw_enobuf);
772 		counter_u64_zero(rack_saw_enobuf_hw);
773 		counter_u64_zero(rack_saw_enetunreach);
774 		counter_u64_zero(rack_per_timer_hole);
775 		counter_u64_zero(rack_large_ackcmp);
776 		counter_u64_zero(rack_small_ackcmp);
777 		counter_u64_zero(rack_persists_sends);
778 		counter_u64_zero(rack_persists_acks);
779 		counter_u64_zero(rack_persists_loss);
780 		counter_u64_zero(rack_persists_lost_ends);
781 #ifdef INVARIANTS
782 		counter_u64_zero(rack_adjust_map_bw);
783 #endif
784 		counter_u64_zero(rack_to_alloc_hard);
785 		counter_u64_zero(rack_to_alloc_emerg);
786 		counter_u64_zero(rack_sack_proc_all);
787 		counter_u64_zero(rack_fto_send);
788 		counter_u64_zero(rack_fto_rsm_send);
789 		counter_u64_zero(rack_extended_rfo);
790 		counter_u64_zero(rack_hw_pace_init_fail);
791 		counter_u64_zero(rack_hw_pace_lost);
792 		counter_u64_zero(rack_sbsndptr_wrong);
793 		counter_u64_zero(rack_sbsndptr_right);
794 		counter_u64_zero(rack_non_fto_send);
795 		counter_u64_zero(rack_nfto_resend);
796 		counter_u64_zero(rack_sack_proc_short);
797 		counter_u64_zero(rack_sack_proc_restart);
798 		counter_u64_zero(rack_to_alloc);
799 		counter_u64_zero(rack_to_alloc_limited);
800 		counter_u64_zero(rack_alloc_limited_conns);
801 		counter_u64_zero(rack_split_limited);
802 		for (i = 0; i < MAX_NUM_OF_CNTS; i++) {
803 			counter_u64_zero(rack_proc_comp_ack[i]);
804 		}
805 		counter_u64_zero(rack_multi_single_eq);
806 		counter_u64_zero(rack_proc_non_comp_ack);
807 		counter_u64_zero(rack_find_high);
808 		counter_u64_zero(rack_sack_attacks_detected);
809 		counter_u64_zero(rack_sack_attacks_reversed);
810 		counter_u64_zero(rack_sack_used_next_merge);
811 		counter_u64_zero(rack_sack_used_prev_merge);
812 		counter_u64_zero(rack_sack_splits);
813 		counter_u64_zero(rack_sack_skipped_acked);
814 		counter_u64_zero(rack_ack_total);
815 		counter_u64_zero(rack_express_sack);
816 		counter_u64_zero(rack_sack_total);
817 		counter_u64_zero(rack_move_none);
818 		counter_u64_zero(rack_move_some);
819 		counter_u64_zero(rack_used_tlpmethod);
820 		counter_u64_zero(rack_used_tlpmethod2);
821 		counter_u64_zero(rack_enter_tlp_calc);
822 		counter_u64_zero(rack_progress_drops);
823 		counter_u64_zero(rack_tlp_does_nada);
824 		counter_u64_zero(rack_try_scwnd);
825 		counter_u64_zero(rack_collapsed_win);
826 	}
827 	rack_clear_counter = 0;
828 	return (0);
829 }
830 
831 static void
832 rack_init_sysctls(void)
833 {
834 	int i;
835 	struct sysctl_oid *rack_counters;
836 	struct sysctl_oid *rack_attack;
837 	struct sysctl_oid *rack_pacing;
838 	struct sysctl_oid *rack_timely;
839 	struct sysctl_oid *rack_timers;
840 	struct sysctl_oid *rack_tlp;
841 	struct sysctl_oid *rack_misc;
842 	struct sysctl_oid *rack_features;
843 	struct sysctl_oid *rack_measure;
844 	struct sysctl_oid *rack_probertt;
845 	struct sysctl_oid *rack_hw_pacing;
846 
847 	rack_attack = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
848 	    SYSCTL_CHILDREN(rack_sysctl_root),
849 	    OID_AUTO,
850 	    "sack_attack",
851 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
852 	    "Rack Sack Attack Counters and Controls");
853 	rack_counters = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
854 	    SYSCTL_CHILDREN(rack_sysctl_root),
855 	    OID_AUTO,
856 	    "stats",
857 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
858 	    "Rack Counters");
859 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
860 	    SYSCTL_CHILDREN(rack_sysctl_root),
861 	    OID_AUTO, "rate_sample_method", CTLFLAG_RW,
862 	    &rack_rate_sample_method , USE_RTT_LOW,
863 	    "What method should we use for rate sampling 0=high, 1=low ");
864 	/* Probe rtt related controls */
865 	rack_probertt = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
866 	    SYSCTL_CHILDREN(rack_sysctl_root),
867 	    OID_AUTO,
868 	    "probertt",
869 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
870 	    "ProbeRTT related Controls");
871 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
872 	    SYSCTL_CHILDREN(rack_probertt),
873 	    OID_AUTO, "exit_per_hpb", CTLFLAG_RW,
874 	    &rack_atexit_prtt_hbp, 130,
875 	    "What percentage above goodput do we clamp CA/SS to at exit on high-BDP path 110%");
876 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
877 	    SYSCTL_CHILDREN(rack_probertt),
878 	    OID_AUTO, "exit_per_nonhpb", CTLFLAG_RW,
879 	    &rack_atexit_prtt, 130,
880 	    "What percentage above goodput do we clamp CA/SS to at exit on a non high-BDP path 100%");
881 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
882 	    SYSCTL_CHILDREN(rack_probertt),
883 	    OID_AUTO, "gp_per_mul", CTLFLAG_RW,
884 	    &rack_per_of_gp_probertt, 60,
885 	    "What percentage of goodput do we pace at in probertt");
886 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
887 	    SYSCTL_CHILDREN(rack_probertt),
888 	    OID_AUTO, "gp_per_reduce", CTLFLAG_RW,
889 	    &rack_per_of_gp_probertt_reduce, 10,
890 	    "What percentage of goodput do we reduce every gp_srtt");
891 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
892 	    SYSCTL_CHILDREN(rack_probertt),
893 	    OID_AUTO, "gp_per_low", CTLFLAG_RW,
894 	    &rack_per_of_gp_lowthresh, 40,
895 	    "What percentage of goodput do we allow the multiplier to fall to");
896 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
897 	    SYSCTL_CHILDREN(rack_probertt),
898 	    OID_AUTO, "time_between", CTLFLAG_RW,
899 	    & rack_time_between_probertt, 96000000,
900 	    "How many useconds between the lowest rtt falling must past before we enter probertt");
901 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
902 	    SYSCTL_CHILDREN(rack_probertt),
903 	    OID_AUTO, "safety", CTLFLAG_RW,
904 	    &rack_probe_rtt_safety_val, 2000000,
905 	    "If not zero, provides a maximum usecond that you can stay in probertt (2sec = 2000000)");
906 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
907 	    SYSCTL_CHILDREN(rack_probertt),
908 	    OID_AUTO, "sets_cwnd", CTLFLAG_RW,
909 	    &rack_probe_rtt_sets_cwnd, 0,
910 	    "Do we set the cwnd too (if always_lower is on)");
911 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
912 	    SYSCTL_CHILDREN(rack_probertt),
913 	    OID_AUTO, "maxdrainsrtts", CTLFLAG_RW,
914 	    &rack_max_drain_wait, 2,
915 	    "Maximum number of gp_srtt's to hold in drain waiting for flight to reach goal");
916 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
917 	    SYSCTL_CHILDREN(rack_probertt),
918 	    OID_AUTO, "mustdrainsrtts", CTLFLAG_RW,
919 	    &rack_must_drain, 1,
920 	    "We must drain this many gp_srtt's waiting for flight to reach goal");
921 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
922 	    SYSCTL_CHILDREN(rack_probertt),
923 	    OID_AUTO, "goal_use_min_entry", CTLFLAG_RW,
924 	    &rack_probertt_use_min_rtt_entry, 1,
925 	    "Should we use the min-rtt to calculate the goal rtt (else gp_srtt) at entry");
926 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
927 	    SYSCTL_CHILDREN(rack_probertt),
928 	    OID_AUTO, "goal_use_min_exit", CTLFLAG_RW,
929 	    &rack_probertt_use_min_rtt_exit, 0,
930 	    "How to set cwnd at exit, 0 - dynamic, 1 - use min-rtt, 2 - use curgprtt, 3 - entry gp-rtt");
931 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
932 	    SYSCTL_CHILDREN(rack_probertt),
933 	    OID_AUTO, "length_div", CTLFLAG_RW,
934 	    &rack_probertt_gpsrtt_cnt_div, 0,
935 	    "How many recent goodput srtt periods plus hold tim does probertt last (bottom of fraction)");
936 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
937 	    SYSCTL_CHILDREN(rack_probertt),
938 	    OID_AUTO, "length_mul", CTLFLAG_RW,
939 	    &rack_probertt_gpsrtt_cnt_mul, 0,
940 	    "How many recent goodput srtt periods plus hold tim does probertt last (top of fraction)");
941 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
942 	    SYSCTL_CHILDREN(rack_probertt),
943 	    OID_AUTO, "holdtim_at_target", CTLFLAG_RW,
944 	    &rack_min_probertt_hold, 200000,
945 	    "What is the minimum time we hold probertt at target");
946 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
947 	    SYSCTL_CHILDREN(rack_probertt),
948 	    OID_AUTO, "filter_life", CTLFLAG_RW,
949 	    &rack_probertt_filter_life, 10000000,
950 	    "What is the time for the filters life in useconds");
951 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
952 	    SYSCTL_CHILDREN(rack_probertt),
953 	    OID_AUTO, "lower_within", CTLFLAG_RW,
954 	    &rack_probertt_lower_within, 10,
955 	    "If the rtt goes lower within this percentage of the time, go into probe-rtt");
956 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
957 	    SYSCTL_CHILDREN(rack_probertt),
958 	    OID_AUTO, "must_move", CTLFLAG_RW,
959 	    &rack_min_rtt_movement, 250,
960 	    "How much is the minimum movement in rtt to count as a drop for probertt purposes");
961 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
962 	    SYSCTL_CHILDREN(rack_probertt),
963 	    OID_AUTO, "clear_is_cnts", CTLFLAG_RW,
964 	    &rack_probertt_clear_is, 1,
965 	    "Do we clear I/S counts on exiting probe-rtt");
966 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
967 	    SYSCTL_CHILDREN(rack_probertt),
968 	    OID_AUTO, "hbp_extra_drain", CTLFLAG_RW,
969 	    &rack_max_drain_hbp, 1,
970 	    "How many extra drain gpsrtt's do we get in highly buffered paths");
971 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
972 	    SYSCTL_CHILDREN(rack_probertt),
973 	    OID_AUTO, "hbp_threshold", CTLFLAG_RW,
974 	    &rack_hbp_thresh, 3,
975 	    "We are highly buffered if min_rtt_seen / max_rtt_seen > this-threshold");
976 	/* Pacing related sysctls */
977 	rack_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
978 	    SYSCTL_CHILDREN(rack_sysctl_root),
979 	    OID_AUTO,
980 	    "pacing",
981 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
982 	    "Pacing related Controls");
983 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
984 	    SYSCTL_CHILDREN(rack_pacing),
985 	    OID_AUTO, "max_pace_over", CTLFLAG_RW,
986 	    &rack_max_per_above, 30,
987 	    "What is the maximum allowable percentage that we can pace above (so 30 = 130% of our goal)");
988 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
989 	    SYSCTL_CHILDREN(rack_pacing),
990 	    OID_AUTO, "pace_to_one", CTLFLAG_RW,
991 	    &rack_pace_one_seg, 0,
992 	    "Do we allow low b/w pacing of 1MSS instead of two");
993 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
994 	    SYSCTL_CHILDREN(rack_pacing),
995 	    OID_AUTO, "limit_wsrtt", CTLFLAG_RW,
996 	    &rack_limit_time_with_srtt, 0,
997 	    "Do we limit pacing time based on srtt");
998 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
999 	    SYSCTL_CHILDREN(rack_pacing),
1000 	    OID_AUTO, "init_win", CTLFLAG_RW,
1001 	    &rack_default_init_window, 0,
1002 	    "Do we have a rack initial window 0 = system default");
1003 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1004 	    SYSCTL_CHILDREN(rack_pacing),
1005 	    OID_AUTO, "gp_per_ss", CTLFLAG_RW,
1006 	    &rack_per_of_gp_ss, 250,
1007 	    "If non zero, what percentage of goodput to pace at in slow start");
1008 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1009 	    SYSCTL_CHILDREN(rack_pacing),
1010 	    OID_AUTO, "gp_per_ca", CTLFLAG_RW,
1011 	    &rack_per_of_gp_ca, 150,
1012 	    "If non zero, what percentage of goodput to pace at in congestion avoidance");
1013 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1014 	    SYSCTL_CHILDREN(rack_pacing),
1015 	    OID_AUTO, "gp_per_rec", CTLFLAG_RW,
1016 	    &rack_per_of_gp_rec, 200,
1017 	    "If non zero, what percentage of goodput to pace at in recovery");
1018 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1019 	    SYSCTL_CHILDREN(rack_pacing),
1020 	    OID_AUTO, "pace_max_seg", CTLFLAG_RW,
1021 	    &rack_hptsi_segments, 40,
1022 	    "What size is the max for TSO segments in pacing and burst mitigation");
1023 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1024 	    SYSCTL_CHILDREN(rack_pacing),
1025 	    OID_AUTO, "burst_reduces", CTLFLAG_RW,
1026 	    &rack_slot_reduction, 4,
1027 	    "When doing only burst mitigation what is the reduce divisor");
1028 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1029 	    SYSCTL_CHILDREN(rack_sysctl_root),
1030 	    OID_AUTO, "use_pacing", CTLFLAG_RW,
1031 	    &rack_pace_every_seg, 0,
1032 	    "If set we use pacing, if clear we use only the original burst mitigation");
1033 	SYSCTL_ADD_U64(&rack_sysctl_ctx,
1034 	    SYSCTL_CHILDREN(rack_pacing),
1035 	    OID_AUTO, "rate_cap", CTLFLAG_RW,
1036 	    &rack_bw_rate_cap, 0,
1037 	    "If set we apply this value to the absolute rate cap used by pacing");
1038 	SYSCTL_ADD_U8(&rack_sysctl_ctx,
1039 	    SYSCTL_CHILDREN(rack_sysctl_root),
1040 	    OID_AUTO, "req_measure_cnt", CTLFLAG_RW,
1041 	    &rack_req_measurements, 1,
1042 	    "If doing dynamic pacing, how many measurements must be in before we start pacing?");
1043 	/* Hardware pacing */
1044 	rack_hw_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1045 	    SYSCTL_CHILDREN(rack_sysctl_root),
1046 	    OID_AUTO,
1047 	    "hdwr_pacing",
1048 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1049 	    "Pacing related Controls");
1050 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1051 	    SYSCTL_CHILDREN(rack_hw_pacing),
1052 	    OID_AUTO, "rwnd_factor", CTLFLAG_RW,
1053 	    &rack_hw_rwnd_factor, 2,
1054 	    "How many times does snd_wnd need to be bigger than pace_max_seg so we will hold off and get more acks?");
1055 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1056 	    SYSCTL_CHILDREN(rack_hw_pacing),
1057 	    OID_AUTO, "pace_enobuf_mult", CTLFLAG_RW,
1058 	    &rack_enobuf_hw_boost_mult, 2,
1059 	    "By how many time_betweens should we boost the pacing time if we see a ENOBUFS?");
1060 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1061 	    SYSCTL_CHILDREN(rack_hw_pacing),
1062 	    OID_AUTO, "pace_enobuf_max", CTLFLAG_RW,
1063 	    &rack_enobuf_hw_max, 2,
1064 	    "What is the max boost the pacing time if we see a ENOBUFS?");
1065 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1066 	    SYSCTL_CHILDREN(rack_hw_pacing),
1067 	    OID_AUTO, "pace_enobuf_min", CTLFLAG_RW,
1068 	    &rack_enobuf_hw_min, 2,
1069 	    "What is the min boost the pacing time if we see a ENOBUFS?");
1070 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1071 	    SYSCTL_CHILDREN(rack_hw_pacing),
1072 	    OID_AUTO, "enable", CTLFLAG_RW,
1073 	    &rack_enable_hw_pacing, 0,
1074 	    "Should RACK attempt to use hw pacing?");
1075 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1076 	    SYSCTL_CHILDREN(rack_hw_pacing),
1077 	    OID_AUTO, "rate_cap", CTLFLAG_RW,
1078 	    &rack_hw_rate_caps, 1,
1079 	    "Does the highest hardware pacing rate cap the rate we will send at??");
1080 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1081 	    SYSCTL_CHILDREN(rack_hw_pacing),
1082 	    OID_AUTO, "rate_min", CTLFLAG_RW,
1083 	    &rack_hw_rate_min, 0,
1084 	    "Do we need a minimum estimate of this many bytes per second in order to engage hw pacing?");
1085 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1086 	    SYSCTL_CHILDREN(rack_hw_pacing),
1087 	    OID_AUTO, "rate_to_low", CTLFLAG_RW,
1088 	    &rack_hw_rate_to_low, 0,
1089 	    "If we fall below this rate, dis-engage hw pacing?");
1090 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1091 	    SYSCTL_CHILDREN(rack_hw_pacing),
1092 	    OID_AUTO, "up_only", CTLFLAG_RW,
1093 	    &rack_hw_up_only, 1,
1094 	    "Do we allow hw pacing to lower the rate selected?");
1095 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1096 	    SYSCTL_CHILDREN(rack_hw_pacing),
1097 	    OID_AUTO, "extra_mss_precise", CTLFLAG_RW,
1098 	    &rack_hw_pace_extra_slots, 2,
1099 	    "If the rates between software and hardware match precisely how many extra time_betweens do we get?");
1100 	rack_timely = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1101 	    SYSCTL_CHILDREN(rack_sysctl_root),
1102 	    OID_AUTO,
1103 	    "timely",
1104 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1105 	    "Rack Timely RTT Controls");
1106 	/* Timely based GP dynmics */
1107 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1108 	    SYSCTL_CHILDREN(rack_timely),
1109 	    OID_AUTO, "upper", CTLFLAG_RW,
1110 	    &rack_gp_per_bw_mul_up, 2,
1111 	    "Rack timely upper range for equal b/w (in percentage)");
1112 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1113 	    SYSCTL_CHILDREN(rack_timely),
1114 	    OID_AUTO, "lower", CTLFLAG_RW,
1115 	    &rack_gp_per_bw_mul_down, 4,
1116 	    "Rack timely lower range for equal b/w (in percentage)");
1117 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1118 	    SYSCTL_CHILDREN(rack_timely),
1119 	    OID_AUTO, "rtt_max_mul", CTLFLAG_RW,
1120 	    &rack_gp_rtt_maxmul, 3,
1121 	    "Rack timely multipler of lowest rtt for rtt_max");
1122 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1123 	    SYSCTL_CHILDREN(rack_timely),
1124 	    OID_AUTO, "rtt_min_div", CTLFLAG_RW,
1125 	    &rack_gp_rtt_mindiv, 4,
1126 	    "Rack timely divisor used for rtt + (rtt * mul/divisor) for check for lower rtt");
1127 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1128 	    SYSCTL_CHILDREN(rack_timely),
1129 	    OID_AUTO, "rtt_min_mul", CTLFLAG_RW,
1130 	    &rack_gp_rtt_minmul, 1,
1131 	    "Rack timely multiplier used for rtt + (rtt * mul/divisor) for check for lower rtt");
1132 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1133 	    SYSCTL_CHILDREN(rack_timely),
1134 	    OID_AUTO, "decrease", CTLFLAG_RW,
1135 	    &rack_gp_decrease_per, 20,
1136 	    "Rack timely decrease percentage of our GP multiplication factor");
1137 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1138 	    SYSCTL_CHILDREN(rack_timely),
1139 	    OID_AUTO, "increase", CTLFLAG_RW,
1140 	    &rack_gp_increase_per, 2,
1141 	    "Rack timely increase perentage of our GP multiplication factor");
1142 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1143 	    SYSCTL_CHILDREN(rack_timely),
1144 	    OID_AUTO, "lowerbound", CTLFLAG_RW,
1145 	    &rack_per_lower_bound, 50,
1146 	    "Rack timely lowest percentage we allow GP multiplier to fall to");
1147 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1148 	    SYSCTL_CHILDREN(rack_timely),
1149 	    OID_AUTO, "upperboundss", CTLFLAG_RW,
1150 	    &rack_per_upper_bound_ss, 0,
1151 	    "Rack timely higest percentage we allow GP multiplier in SS to raise to (0 is no upperbound)");
1152 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1153 	    SYSCTL_CHILDREN(rack_timely),
1154 	    OID_AUTO, "upperboundca", CTLFLAG_RW,
1155 	    &rack_per_upper_bound_ca, 0,
1156 	    "Rack timely higest percentage we allow GP multiplier to CA raise to (0 is no upperbound)");
1157 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1158 	    SYSCTL_CHILDREN(rack_timely),
1159 	    OID_AUTO, "dynamicgp", CTLFLAG_RW,
1160 	    &rack_do_dyn_mul, 0,
1161 	    "Rack timely do we enable dynmaic timely goodput by default");
1162 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1163 	    SYSCTL_CHILDREN(rack_timely),
1164 	    OID_AUTO, "no_rec_red", CTLFLAG_RW,
1165 	    &rack_gp_no_rec_chg, 1,
1166 	    "Rack timely do we prohibit the recovery multiplier from being lowered");
1167 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1168 	    SYSCTL_CHILDREN(rack_timely),
1169 	    OID_AUTO, "red_clear_cnt", CTLFLAG_RW,
1170 	    &rack_timely_dec_clear, 6,
1171 	    "Rack timely what threshold do we count to before another boost during b/w decent");
1172 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1173 	    SYSCTL_CHILDREN(rack_timely),
1174 	    OID_AUTO, "max_push_rise", CTLFLAG_RW,
1175 	    &rack_timely_max_push_rise, 3,
1176 	    "Rack timely how many times do we push up with b/w increase");
1177 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1178 	    SYSCTL_CHILDREN(rack_timely),
1179 	    OID_AUTO, "max_push_drop", CTLFLAG_RW,
1180 	    &rack_timely_max_push_drop, 3,
1181 	    "Rack timely how many times do we push back on b/w decent");
1182 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1183 	    SYSCTL_CHILDREN(rack_timely),
1184 	    OID_AUTO, "min_segs", CTLFLAG_RW,
1185 	    &rack_timely_min_segs, 4,
1186 	    "Rack timely when setting the cwnd what is the min num segments");
1187 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1188 	    SYSCTL_CHILDREN(rack_timely),
1189 	    OID_AUTO, "noback_max", CTLFLAG_RW,
1190 	    &rack_use_max_for_nobackoff, 0,
1191 	    "Rack timely when deciding if to backoff on a loss, do we use under max rtt else min");
1192 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1193 	    SYSCTL_CHILDREN(rack_timely),
1194 	    OID_AUTO, "interim_timely_only", CTLFLAG_RW,
1195 	    &rack_timely_int_timely_only, 0,
1196 	    "Rack timely when doing interim timely's do we only do timely (no b/w consideration)");
1197 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1198 	    SYSCTL_CHILDREN(rack_timely),
1199 	    OID_AUTO, "nonstop", CTLFLAG_RW,
1200 	    &rack_timely_no_stopping, 0,
1201 	    "Rack timely don't stop increase");
1202 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1203 	    SYSCTL_CHILDREN(rack_timely),
1204 	    OID_AUTO, "dec_raise_thresh", CTLFLAG_RW,
1205 	    &rack_down_raise_thresh, 100,
1206 	    "If the CA or SS is below this threshold raise on the first 3 b/w lowers (0=always)");
1207 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1208 	    SYSCTL_CHILDREN(rack_timely),
1209 	    OID_AUTO, "bottom_drag_segs", CTLFLAG_RW,
1210 	    &rack_req_segs, 1,
1211 	    "Bottom dragging if not these many segments outstanding and room");
1212 
1213 	/* TLP and Rack related parameters */
1214 	rack_tlp = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1215 	    SYSCTL_CHILDREN(rack_sysctl_root),
1216 	    OID_AUTO,
1217 	    "tlp",
1218 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1219 	    "TLP and Rack related Controls");
1220 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1221 	    SYSCTL_CHILDREN(rack_tlp),
1222 	    OID_AUTO, "use_rrr", CTLFLAG_RW,
1223 	    &use_rack_rr, 1,
1224 	    "Do we use Rack Rapid Recovery");
1225 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1226 	    SYSCTL_CHILDREN(rack_tlp),
1227 	    OID_AUTO, "post_rec_labc", CTLFLAG_RW,
1228 	    &rack_max_abc_post_recovery, 2,
1229 	    "Since we do early recovery, do we override the l_abc to a value, if so what?");
1230 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1231 	    SYSCTL_CHILDREN(rack_tlp),
1232 	    OID_AUTO, "nonrxt_use_cr", CTLFLAG_RW,
1233 	    &rack_non_rxt_use_cr, 0,
1234 	    "Do we use ss/ca rate if in recovery we are transmitting a new data chunk");
1235 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1236 	    SYSCTL_CHILDREN(rack_tlp),
1237 	    OID_AUTO, "tlpmethod", CTLFLAG_RW,
1238 	    &rack_tlp_threshold_use, TLP_USE_TWO_ONE,
1239 	    "What method do we do for TLP time calc 0=no-de-ack-comp, 1=ID, 2=2.1, 3=2.2");
1240 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1241 	    SYSCTL_CHILDREN(rack_tlp),
1242 	    OID_AUTO, "limit", CTLFLAG_RW,
1243 	    &rack_tlp_limit, 2,
1244 	    "How many TLP's can be sent without sending new data");
1245 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1246 	    SYSCTL_CHILDREN(rack_tlp),
1247 	    OID_AUTO, "use_greater", CTLFLAG_RW,
1248 	    &rack_tlp_use_greater, 1,
1249 	    "Should we use the rack_rtt time if its greater than srtt");
1250 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1251 	    SYSCTL_CHILDREN(rack_tlp),
1252 	    OID_AUTO, "tlpminto", CTLFLAG_RW,
1253 	    &rack_tlp_min, 10000,
1254 	    "TLP minimum timeout per the specification (in microseconds)");
1255 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1256 	    SYSCTL_CHILDREN(rack_tlp),
1257 	    OID_AUTO, "send_oldest", CTLFLAG_RW,
1258 	    &rack_always_send_oldest, 0,
1259 	    "Should we always send the oldest TLP and RACK-TLP");
1260 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1261 	    SYSCTL_CHILDREN(rack_tlp),
1262 	    OID_AUTO, "rack_tlimit", CTLFLAG_RW,
1263 	    &rack_limited_retran, 0,
1264 	    "How many times can a rack timeout drive out sends");
1265 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1266 	    SYSCTL_CHILDREN(rack_tlp),
1267 	    OID_AUTO, "tlp_cwnd_flag", CTLFLAG_RW,
1268 	    &rack_lower_cwnd_at_tlp, 0,
1269 	    "When a TLP completes a retran should we enter recovery");
1270 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1271 	    SYSCTL_CHILDREN(rack_tlp),
1272 	    OID_AUTO, "reorder_thresh", CTLFLAG_RW,
1273 	    &rack_reorder_thresh, 2,
1274 	    "What factor for rack will be added when seeing reordering (shift right)");
1275 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1276 	    SYSCTL_CHILDREN(rack_tlp),
1277 	    OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW,
1278 	    &rack_tlp_thresh, 1,
1279 	    "What divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)");
1280 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1281 	    SYSCTL_CHILDREN(rack_tlp),
1282 	    OID_AUTO, "reorder_fade", CTLFLAG_RW,
1283 	    &rack_reorder_fade, 60000000,
1284 	    "Does reorder detection fade, if so how many microseconds (0 means never)");
1285 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1286 	    SYSCTL_CHILDREN(rack_tlp),
1287 	    OID_AUTO, "pktdelay", CTLFLAG_RW,
1288 	    &rack_pkt_delay, 1000,
1289 	    "Extra RACK time (in microseconds) besides reordering thresh");
1290 
1291 	/* Timer related controls */
1292 	rack_timers = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1293 	    SYSCTL_CHILDREN(rack_sysctl_root),
1294 	    OID_AUTO,
1295 	    "timers",
1296 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1297 	    "Timer related controls");
1298 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1299 	    SYSCTL_CHILDREN(rack_timers),
1300 	    OID_AUTO, "persmin", CTLFLAG_RW,
1301 	    &rack_persist_min, 250000,
1302 	    "What is the minimum time in microseconds between persists");
1303 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1304 	    SYSCTL_CHILDREN(rack_timers),
1305 	    OID_AUTO, "persmax", CTLFLAG_RW,
1306 	    &rack_persist_max, 2000000,
1307 	    "What is the largest delay in microseconds between persists");
1308 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1309 	    SYSCTL_CHILDREN(rack_timers),
1310 	    OID_AUTO, "delayed_ack", CTLFLAG_RW,
1311 	    &rack_delayed_ack_time, 40000,
1312 	    "Delayed ack time (40ms in microseconds)");
1313 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1314 	    SYSCTL_CHILDREN(rack_timers),
1315 	    OID_AUTO, "minrto", CTLFLAG_RW,
1316 	    &rack_rto_min, 30000,
1317 	    "Minimum RTO in microseconds -- set with caution below 1000 due to TLP");
1318 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1319 	    SYSCTL_CHILDREN(rack_timers),
1320 	    OID_AUTO, "maxrto", CTLFLAG_RW,
1321 	    &rack_rto_max, 4000000,
1322 	    "Maximum RTO in microseconds -- should be at least as large as min_rto");
1323 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1324 	    SYSCTL_CHILDREN(rack_timers),
1325 	    OID_AUTO, "minto", CTLFLAG_RW,
1326 	    &rack_min_to, 1000,
1327 	    "Minimum rack timeout in microseconds");
1328 	/* Measure controls */
1329 	rack_measure = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1330 	    SYSCTL_CHILDREN(rack_sysctl_root),
1331 	    OID_AUTO,
1332 	    "measure",
1333 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1334 	    "Measure related controls");
1335 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1336 	    SYSCTL_CHILDREN(rack_measure),
1337 	    OID_AUTO, "wma_divisor", CTLFLAG_RW,
1338 	    &rack_wma_divisor, 8,
1339 	    "When doing b/w calculation what is the  divisor for the WMA");
1340 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1341 	    SYSCTL_CHILDREN(rack_measure),
1342 	    OID_AUTO, "end_cwnd", CTLFLAG_RW,
1343 	    &rack_cwnd_block_ends_measure, 0,
1344 	    "Does a cwnd just-return end the measurement window (app limited)");
1345 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1346 	    SYSCTL_CHILDREN(rack_measure),
1347 	    OID_AUTO, "end_rwnd", CTLFLAG_RW,
1348 	    &rack_rwnd_block_ends_measure, 0,
1349 	    "Does an rwnd just-return end the measurement window (app limited -- not persists)");
1350 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1351 	    SYSCTL_CHILDREN(rack_measure),
1352 	    OID_AUTO, "min_target", CTLFLAG_RW,
1353 	    &rack_def_data_window, 20,
1354 	    "What is the minimum target window (in mss) for a GP measurements");
1355 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1356 	    SYSCTL_CHILDREN(rack_measure),
1357 	    OID_AUTO, "goal_bdp", CTLFLAG_RW,
1358 	    &rack_goal_bdp, 2,
1359 	    "What is the goal BDP to measure");
1360 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1361 	    SYSCTL_CHILDREN(rack_measure),
1362 	    OID_AUTO, "min_srtts", CTLFLAG_RW,
1363 	    &rack_min_srtts, 1,
1364 	    "What is the goal BDP to measure");
1365 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1366 	    SYSCTL_CHILDREN(rack_measure),
1367 	    OID_AUTO, "min_measure_tim", CTLFLAG_RW,
1368 	    &rack_min_measure_usec, 0,
1369 	    "What is the Minimum time time for a measurement if 0, this is off");
1370 	/* Features */
1371 	rack_features = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1372 	    SYSCTL_CHILDREN(rack_sysctl_root),
1373 	    OID_AUTO,
1374 	    "features",
1375 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1376 	    "Feature controls");
1377 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1378 	    SYSCTL_CHILDREN(rack_features),
1379 	    OID_AUTO, "cmpack", CTLFLAG_RW,
1380 	    &rack_use_cmp_acks, 1,
1381 	    "Should RACK have LRO send compressed acks");
1382 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1383 	    SYSCTL_CHILDREN(rack_features),
1384 	    OID_AUTO, "fsb", CTLFLAG_RW,
1385 	    &rack_use_fsb, 1,
1386 	    "Should RACK use the fast send block?");
1387 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1388 	    SYSCTL_CHILDREN(rack_features),
1389 	    OID_AUTO, "rfo", CTLFLAG_RW,
1390 	    &rack_use_rfo, 1,
1391 	    "Should RACK use rack_fast_output()?");
1392 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1393 	    SYSCTL_CHILDREN(rack_features),
1394 	    OID_AUTO, "rsmrfo", CTLFLAG_RW,
1395 	    &rack_use_rsm_rfo, 1,
1396 	    "Should RACK use rack_fast_rsm_output()?");
1397 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1398 	    SYSCTL_CHILDREN(rack_features),
1399 	    OID_AUTO, "non_paced_lro_queue", CTLFLAG_RW,
1400 	    &rack_enable_mqueue_for_nonpaced, 0,
1401 	    "Should RACK use mbuf queuing for non-paced connections");
1402 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1403 	    SYSCTL_CHILDREN(rack_features),
1404 	    OID_AUTO, "hystartplusplus", CTLFLAG_RW,
1405 	    &rack_do_hystart, 0,
1406 	    "Should RACK enable HyStart++ on connections?");
1407 	/* Misc rack controls */
1408 	rack_misc = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1409 	    SYSCTL_CHILDREN(rack_sysctl_root),
1410 	    OID_AUTO,
1411 	    "misc",
1412 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1413 	    "Misc related controls");
1414 #ifdef TCP_ACCOUNTING
1415 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1416 	    SYSCTL_CHILDREN(rack_misc),
1417 	    OID_AUTO, "tcp_acct", CTLFLAG_RW,
1418 	    &rack_tcp_accounting, 0,
1419 	    "Should we turn on TCP accounting for all rack sessions?");
1420 #endif
1421 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1422 	    SYSCTL_CHILDREN(rack_misc),
1423 	    OID_AUTO, "apply_rtt_with_low_conf", CTLFLAG_RW,
1424 	    &rack_apply_rtt_with_reduced_conf, 0,
1425 	    "When a persist or keep-alive probe is not answered do we calculate rtt on subsequent answers?");
1426 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1427 	    SYSCTL_CHILDREN(rack_misc),
1428 	    OID_AUTO, "rack_dsack_ctl", CTLFLAG_RW,
1429 	    &rack_dsack_std_based, 3,
1430 	    "How do we process dsack with respect to rack timers, bit field, 3 is standards based?");
1431 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1432 	    SYSCTL_CHILDREN(rack_misc),
1433 	    OID_AUTO, "prr_addback_max", CTLFLAG_RW,
1434 	    &rack_prr_addbackmax, 2,
1435 	    "What is the maximum number of MSS we allow to be added back if prr can't send all its data?");
1436 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1437 	    SYSCTL_CHILDREN(rack_misc),
1438 	    OID_AUTO, "stats_gets_ms", CTLFLAG_RW,
1439 	    &rack_stats_gets_ms_rtt, 1,
1440 	    "What do we feed the stats framework (1 = ms_rtt, 0 = us_rtt, 2 = ms_rtt from hdwr, > 2 usec rtt from hdwr)?");
1441 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1442 	    SYSCTL_CHILDREN(rack_misc),
1443 	    OID_AUTO, "clientlowbuf", CTLFLAG_RW,
1444 	    &rack_client_low_buf, 0,
1445 	    "Client low buffer level (below this we are more aggressive in DGP exiting recovery (0 = off)?");
1446 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1447 	    SYSCTL_CHILDREN(rack_misc),
1448 	    OID_AUTO, "defprofile", CTLFLAG_RW,
1449 	    &rack_def_profile, 0,
1450 	    "Should RACK use a default profile (0=no, num == profile num)?");
1451 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1452 	    SYSCTL_CHILDREN(rack_misc),
1453 	    OID_AUTO, "shared_cwnd", CTLFLAG_RW,
1454 	    &rack_enable_shared_cwnd, 1,
1455 	    "Should RACK try to use the shared cwnd on connections where allowed");
1456 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1457 	    SYSCTL_CHILDREN(rack_misc),
1458 	    OID_AUTO, "limits_on_scwnd", CTLFLAG_RW,
1459 	    &rack_limits_scwnd, 1,
1460 	    "Should RACK place low end time limits on the shared cwnd feature");
1461 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1462 	    SYSCTL_CHILDREN(rack_misc),
1463 	    OID_AUTO, "iMac_dack", CTLFLAG_RW,
1464 	    &rack_use_imac_dack, 0,
1465 	    "Should RACK try to emulate iMac delayed ack");
1466 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1467 	    SYSCTL_CHILDREN(rack_misc),
1468 	    OID_AUTO, "no_prr", CTLFLAG_RW,
1469 	    &rack_disable_prr, 0,
1470 	    "Should RACK not use prr and only pace (must have pacing on)");
1471 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1472 	    SYSCTL_CHILDREN(rack_misc),
1473 	    OID_AUTO, "bb_verbose", CTLFLAG_RW,
1474 	    &rack_verbose_logging, 0,
1475 	    "Should RACK black box logging be verbose");
1476 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1477 	    SYSCTL_CHILDREN(rack_misc),
1478 	    OID_AUTO, "data_after_close", CTLFLAG_RW,
1479 	    &rack_ignore_data_after_close, 1,
1480 	    "Do we hold off sending a RST until all pending data is ack'd");
1481 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1482 	    SYSCTL_CHILDREN(rack_misc),
1483 	    OID_AUTO, "no_sack_needed", CTLFLAG_RW,
1484 	    &rack_sack_not_required, 1,
1485 	    "Do we allow rack to run on connections not supporting SACK");
1486 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1487 	    SYSCTL_CHILDREN(rack_misc),
1488 	    OID_AUTO, "prr_sendalot", CTLFLAG_RW,
1489 	    &rack_send_a_lot_in_prr, 1,
1490 	    "Send a lot in prr");
1491 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1492 	    SYSCTL_CHILDREN(rack_misc),
1493 	    OID_AUTO, "autoscale", CTLFLAG_RW,
1494 	    &rack_autosndbuf_inc, 20,
1495 	    "What percentage should rack scale up its snd buffer by?");
1496 	/* Sack Attacker detection stuff */
1497 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1498 	    SYSCTL_CHILDREN(rack_attack),
1499 	    OID_AUTO, "detect_highsackratio", CTLFLAG_RW,
1500 	    &rack_highest_sack_thresh_seen, 0,
1501 	    "Highest sack to ack ratio seen");
1502 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1503 	    SYSCTL_CHILDREN(rack_attack),
1504 	    OID_AUTO, "detect_highmoveratio", CTLFLAG_RW,
1505 	    &rack_highest_move_thresh_seen, 0,
1506 	    "Highest move to non-move ratio seen");
1507 	rack_ack_total = counter_u64_alloc(M_WAITOK);
1508 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1509 	    SYSCTL_CHILDREN(rack_attack),
1510 	    OID_AUTO, "acktotal", CTLFLAG_RD,
1511 	    &rack_ack_total,
1512 	    "Total number of Ack's");
1513 	rack_express_sack = counter_u64_alloc(M_WAITOK);
1514 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1515 	    SYSCTL_CHILDREN(rack_attack),
1516 	    OID_AUTO, "exp_sacktotal", CTLFLAG_RD,
1517 	    &rack_express_sack,
1518 	    "Total expresss number of Sack's");
1519 	rack_sack_total = counter_u64_alloc(M_WAITOK);
1520 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1521 	    SYSCTL_CHILDREN(rack_attack),
1522 	    OID_AUTO, "sacktotal", CTLFLAG_RD,
1523 	    &rack_sack_total,
1524 	    "Total number of SACKs");
1525 	rack_move_none = counter_u64_alloc(M_WAITOK);
1526 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1527 	    SYSCTL_CHILDREN(rack_attack),
1528 	    OID_AUTO, "move_none", CTLFLAG_RD,
1529 	    &rack_move_none,
1530 	    "Total number of SACK index reuse of postions under threshold");
1531 	rack_move_some = counter_u64_alloc(M_WAITOK);
1532 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1533 	    SYSCTL_CHILDREN(rack_attack),
1534 	    OID_AUTO, "move_some", CTLFLAG_RD,
1535 	    &rack_move_some,
1536 	    "Total number of SACK index reuse of postions over threshold");
1537 	rack_sack_attacks_detected = counter_u64_alloc(M_WAITOK);
1538 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1539 	    SYSCTL_CHILDREN(rack_attack),
1540 	    OID_AUTO, "attacks", CTLFLAG_RD,
1541 	    &rack_sack_attacks_detected,
1542 	    "Total number of SACK attackers that had sack disabled");
1543 	rack_sack_attacks_reversed = counter_u64_alloc(M_WAITOK);
1544 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1545 	    SYSCTL_CHILDREN(rack_attack),
1546 	    OID_AUTO, "reversed", CTLFLAG_RD,
1547 	    &rack_sack_attacks_reversed,
1548 	    "Total number of SACK attackers that were later determined false positive");
1549 	rack_sack_used_next_merge = counter_u64_alloc(M_WAITOK);
1550 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1551 	    SYSCTL_CHILDREN(rack_attack),
1552 	    OID_AUTO, "nextmerge", CTLFLAG_RD,
1553 	    &rack_sack_used_next_merge,
1554 	    "Total number of times we used the next merge");
1555 	rack_sack_used_prev_merge = counter_u64_alloc(M_WAITOK);
1556 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1557 	    SYSCTL_CHILDREN(rack_attack),
1558 	    OID_AUTO, "prevmerge", CTLFLAG_RD,
1559 	    &rack_sack_used_prev_merge,
1560 	    "Total number of times we used the prev merge");
1561 	/* Counters */
1562 	rack_fto_send = counter_u64_alloc(M_WAITOK);
1563 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1564 	    SYSCTL_CHILDREN(rack_counters),
1565 	    OID_AUTO, "fto_send", CTLFLAG_RD,
1566 	    &rack_fto_send, "Total number of rack_fast_output sends");
1567 	rack_fto_rsm_send = counter_u64_alloc(M_WAITOK);
1568 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1569 	    SYSCTL_CHILDREN(rack_counters),
1570 	    OID_AUTO, "fto_rsm_send", CTLFLAG_RD,
1571 	    &rack_fto_rsm_send, "Total number of rack_fast_rsm_output sends");
1572 	rack_nfto_resend = counter_u64_alloc(M_WAITOK);
1573 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1574 	    SYSCTL_CHILDREN(rack_counters),
1575 	    OID_AUTO, "nfto_resend", CTLFLAG_RD,
1576 	    &rack_nfto_resend, "Total number of rack_output retransmissions");
1577 	rack_non_fto_send = counter_u64_alloc(M_WAITOK);
1578 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1579 	    SYSCTL_CHILDREN(rack_counters),
1580 	    OID_AUTO, "nfto_send", CTLFLAG_RD,
1581 	    &rack_non_fto_send, "Total number of rack_output first sends");
1582 	rack_extended_rfo = counter_u64_alloc(M_WAITOK);
1583 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1584 	    SYSCTL_CHILDREN(rack_counters),
1585 	    OID_AUTO, "rfo_extended", CTLFLAG_RD,
1586 	    &rack_extended_rfo, "Total number of times we extended rfo");
1587 
1588 	rack_hw_pace_init_fail = counter_u64_alloc(M_WAITOK);
1589 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1590 	    SYSCTL_CHILDREN(rack_counters),
1591 	    OID_AUTO, "hwpace_init_fail", CTLFLAG_RD,
1592 	    &rack_hw_pace_init_fail, "Total number of times we failed to initialize hw pacing");
1593 	rack_hw_pace_lost = counter_u64_alloc(M_WAITOK);
1594 
1595 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1596 	    SYSCTL_CHILDREN(rack_counters),
1597 	    OID_AUTO, "hwpace_lost", CTLFLAG_RD,
1598 	    &rack_hw_pace_lost, "Total number of times we failed to initialize hw pacing");
1599 	rack_badfr = counter_u64_alloc(M_WAITOK);
1600 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1601 	    SYSCTL_CHILDREN(rack_counters),
1602 	    OID_AUTO, "badfr", CTLFLAG_RD,
1603 	    &rack_badfr, "Total number of bad FRs");
1604 	rack_badfr_bytes = counter_u64_alloc(M_WAITOK);
1605 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1606 	    SYSCTL_CHILDREN(rack_counters),
1607 	    OID_AUTO, "badfr_bytes", CTLFLAG_RD,
1608 	    &rack_badfr_bytes, "Total number of bad FRs");
1609 	rack_rtm_prr_retran = counter_u64_alloc(M_WAITOK);
1610 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1611 	    SYSCTL_CHILDREN(rack_counters),
1612 	    OID_AUTO, "prrsndret", CTLFLAG_RD,
1613 	    &rack_rtm_prr_retran,
1614 	    "Total number of prr based retransmits");
1615 	rack_rtm_prr_newdata = counter_u64_alloc(M_WAITOK);
1616 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1617 	    SYSCTL_CHILDREN(rack_counters),
1618 	    OID_AUTO, "prrsndnew", CTLFLAG_RD,
1619 	    &rack_rtm_prr_newdata,
1620 	    "Total number of prr based new transmits");
1621 	rack_timestamp_mismatch = counter_u64_alloc(M_WAITOK);
1622 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1623 	    SYSCTL_CHILDREN(rack_counters),
1624 	    OID_AUTO, "tsnf", CTLFLAG_RD,
1625 	    &rack_timestamp_mismatch,
1626 	    "Total number of timestamps that we could not find the reported ts");
1627 	rack_find_high = counter_u64_alloc(M_WAITOK);
1628 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1629 	    SYSCTL_CHILDREN(rack_counters),
1630 	    OID_AUTO, "findhigh", CTLFLAG_RD,
1631 	    &rack_find_high,
1632 	    "Total number of FIN causing find-high");
1633 	rack_reorder_seen = counter_u64_alloc(M_WAITOK);
1634 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1635 	    SYSCTL_CHILDREN(rack_counters),
1636 	    OID_AUTO, "reordering", CTLFLAG_RD,
1637 	    &rack_reorder_seen,
1638 	    "Total number of times we added delay due to reordering");
1639 	rack_tlp_tot = counter_u64_alloc(M_WAITOK);
1640 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1641 	    SYSCTL_CHILDREN(rack_counters),
1642 	    OID_AUTO, "tlp_to_total", CTLFLAG_RD,
1643 	    &rack_tlp_tot,
1644 	    "Total number of tail loss probe expirations");
1645 	rack_tlp_newdata = counter_u64_alloc(M_WAITOK);
1646 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1647 	    SYSCTL_CHILDREN(rack_counters),
1648 	    OID_AUTO, "tlp_new", CTLFLAG_RD,
1649 	    &rack_tlp_newdata,
1650 	    "Total number of tail loss probe sending new data");
1651 	rack_tlp_retran = counter_u64_alloc(M_WAITOK);
1652 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1653 	    SYSCTL_CHILDREN(rack_counters),
1654 	    OID_AUTO, "tlp_retran", CTLFLAG_RD,
1655 	    &rack_tlp_retran,
1656 	    "Total number of tail loss probe sending retransmitted data");
1657 	rack_tlp_retran_bytes = counter_u64_alloc(M_WAITOK);
1658 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1659 	    SYSCTL_CHILDREN(rack_counters),
1660 	    OID_AUTO, "tlp_retran_bytes", CTLFLAG_RD,
1661 	    &rack_tlp_retran_bytes,
1662 	    "Total bytes of tail loss probe sending retransmitted data");
1663 	rack_tlp_retran_fail = counter_u64_alloc(M_WAITOK);
1664 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1665 	    SYSCTL_CHILDREN(rack_counters),
1666 	    OID_AUTO, "tlp_retran_fail", CTLFLAG_RD,
1667 	    &rack_tlp_retran_fail,
1668 	    "Total number of tail loss probe sending retransmitted data that failed (wait for t3)");
1669 	rack_to_tot = counter_u64_alloc(M_WAITOK);
1670 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1671 	    SYSCTL_CHILDREN(rack_counters),
1672 	    OID_AUTO, "rack_to_tot", CTLFLAG_RD,
1673 	    &rack_to_tot,
1674 	    "Total number of times the rack to expired");
1675 	rack_to_arm_rack = counter_u64_alloc(M_WAITOK);
1676 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1677 	    SYSCTL_CHILDREN(rack_counters),
1678 	    OID_AUTO, "arm_rack", CTLFLAG_RD,
1679 	    &rack_to_arm_rack,
1680 	    "Total number of times the rack timer armed");
1681 	rack_to_arm_tlp = counter_u64_alloc(M_WAITOK);
1682 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1683 	    SYSCTL_CHILDREN(rack_counters),
1684 	    OID_AUTO, "arm_tlp", CTLFLAG_RD,
1685 	    &rack_to_arm_tlp,
1686 	    "Total number of times the tlp timer armed");
1687 	rack_calc_zero = counter_u64_alloc(M_WAITOK);
1688 	rack_calc_nonzero = counter_u64_alloc(M_WAITOK);
1689 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1690 	    SYSCTL_CHILDREN(rack_counters),
1691 	    OID_AUTO, "calc_zero", CTLFLAG_RD,
1692 	    &rack_calc_zero,
1693 	    "Total number of times pacing time worked out to zero");
1694 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1695 	    SYSCTL_CHILDREN(rack_counters),
1696 	    OID_AUTO, "calc_nonzero", CTLFLAG_RD,
1697 	    &rack_calc_nonzero,
1698 	    "Total number of times pacing time worked out to non-zero");
1699 	rack_paced_segments = counter_u64_alloc(M_WAITOK);
1700 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1701 	    SYSCTL_CHILDREN(rack_counters),
1702 	    OID_AUTO, "paced", CTLFLAG_RD,
1703 	    &rack_paced_segments,
1704 	    "Total number of times a segment send caused hptsi");
1705 	rack_unpaced_segments = counter_u64_alloc(M_WAITOK);
1706 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1707 	    SYSCTL_CHILDREN(rack_counters),
1708 	    OID_AUTO, "unpaced", CTLFLAG_RD,
1709 	    &rack_unpaced_segments,
1710 	    "Total number of times a segment did not cause hptsi");
1711 	rack_saw_enobuf = counter_u64_alloc(M_WAITOK);
1712 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1713 	    SYSCTL_CHILDREN(rack_counters),
1714 	    OID_AUTO, "saw_enobufs", CTLFLAG_RD,
1715 	    &rack_saw_enobuf,
1716 	    "Total number of times a sends returned enobuf for non-hdwr paced connections");
1717 	rack_saw_enobuf_hw = counter_u64_alloc(M_WAITOK);
1718 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1719 	    SYSCTL_CHILDREN(rack_counters),
1720 	    OID_AUTO, "saw_enobufs_hw", CTLFLAG_RD,
1721 	    &rack_saw_enobuf_hw,
1722 	    "Total number of times a send returned enobuf for hdwr paced connections");
1723 	rack_saw_enetunreach = counter_u64_alloc(M_WAITOK);
1724 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1725 	    SYSCTL_CHILDREN(rack_counters),
1726 	    OID_AUTO, "saw_enetunreach", CTLFLAG_RD,
1727 	    &rack_saw_enetunreach,
1728 	    "Total number of times a send received a enetunreachable");
1729 	rack_hot_alloc = counter_u64_alloc(M_WAITOK);
1730 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1731 	    SYSCTL_CHILDREN(rack_counters),
1732 	    OID_AUTO, "alloc_hot", CTLFLAG_RD,
1733 	    &rack_hot_alloc,
1734 	    "Total allocations from the top of our list");
1735 	rack_to_alloc = counter_u64_alloc(M_WAITOK);
1736 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1737 	    SYSCTL_CHILDREN(rack_counters),
1738 	    OID_AUTO, "allocs", CTLFLAG_RD,
1739 	    &rack_to_alloc,
1740 	    "Total allocations of tracking structures");
1741 	rack_to_alloc_hard = counter_u64_alloc(M_WAITOK);
1742 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1743 	    SYSCTL_CHILDREN(rack_counters),
1744 	    OID_AUTO, "allochard", CTLFLAG_RD,
1745 	    &rack_to_alloc_hard,
1746 	    "Total allocations done with sleeping the hard way");
1747 	rack_to_alloc_emerg = counter_u64_alloc(M_WAITOK);
1748 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1749 	    SYSCTL_CHILDREN(rack_counters),
1750 	    OID_AUTO, "allocemerg", CTLFLAG_RD,
1751 	    &rack_to_alloc_emerg,
1752 	    "Total allocations done from emergency cache");
1753 	rack_to_alloc_limited = counter_u64_alloc(M_WAITOK);
1754 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1755 	    SYSCTL_CHILDREN(rack_counters),
1756 	    OID_AUTO, "alloc_limited", CTLFLAG_RD,
1757 	    &rack_to_alloc_limited,
1758 	    "Total allocations dropped due to limit");
1759 	rack_alloc_limited_conns = counter_u64_alloc(M_WAITOK);
1760 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1761 	    SYSCTL_CHILDREN(rack_counters),
1762 	    OID_AUTO, "alloc_limited_conns", CTLFLAG_RD,
1763 	    &rack_alloc_limited_conns,
1764 	    "Connections with allocations dropped due to limit");
1765 	rack_split_limited = counter_u64_alloc(M_WAITOK);
1766 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1767 	    SYSCTL_CHILDREN(rack_counters),
1768 	    OID_AUTO, "split_limited", CTLFLAG_RD,
1769 	    &rack_split_limited,
1770 	    "Split allocations dropped due to limit");
1771 
1772 	for (i = 0; i < MAX_NUM_OF_CNTS; i++) {
1773 		char name[32];
1774 		sprintf(name, "cmp_ack_cnt_%d", i);
1775 		rack_proc_comp_ack[i] = counter_u64_alloc(M_WAITOK);
1776 		SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1777 				       SYSCTL_CHILDREN(rack_counters),
1778 				       OID_AUTO, name, CTLFLAG_RD,
1779 				       &rack_proc_comp_ack[i],
1780 				       "Number of compressed acks we processed");
1781 	}
1782 	rack_large_ackcmp = counter_u64_alloc(M_WAITOK);
1783 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1784 	    SYSCTL_CHILDREN(rack_counters),
1785 	    OID_AUTO, "cmp_large_mbufs", CTLFLAG_RD,
1786 	    &rack_large_ackcmp,
1787 	    "Number of TCP connections with large mbuf's for compressed acks");
1788 	rack_persists_sends = counter_u64_alloc(M_WAITOK);
1789 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1790 	    SYSCTL_CHILDREN(rack_counters),
1791 	    OID_AUTO, "persist_sends", CTLFLAG_RD,
1792 	    &rack_persists_sends,
1793 	    "Number of times we sent a persist probe");
1794 	rack_persists_acks = counter_u64_alloc(M_WAITOK);
1795 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1796 	    SYSCTL_CHILDREN(rack_counters),
1797 	    OID_AUTO, "persist_acks", CTLFLAG_RD,
1798 	    &rack_persists_acks,
1799 	    "Number of times a persist probe was acked");
1800 	rack_persists_loss = counter_u64_alloc(M_WAITOK);
1801 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1802 	    SYSCTL_CHILDREN(rack_counters),
1803 	    OID_AUTO, "persist_loss", CTLFLAG_RD,
1804 	    &rack_persists_loss,
1805 	    "Number of times we detected a lost persist probe (no ack)");
1806 	rack_persists_lost_ends = counter_u64_alloc(M_WAITOK);
1807 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1808 	    SYSCTL_CHILDREN(rack_counters),
1809 	    OID_AUTO, "persist_loss_ends", CTLFLAG_RD,
1810 	    &rack_persists_lost_ends,
1811 	    "Number of lost persist probe (no ack) that the run ended with a PERSIST abort");
1812 	rack_small_ackcmp = counter_u64_alloc(M_WAITOK);
1813 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1814 	    SYSCTL_CHILDREN(rack_counters),
1815 	    OID_AUTO, "cmp_small_mbufs", CTLFLAG_RD,
1816 	    &rack_small_ackcmp,
1817 	    "Number of TCP connections with small mbuf's for compressed acks");
1818 #ifdef INVARIANTS
1819 	rack_adjust_map_bw = counter_u64_alloc(M_WAITOK);
1820 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1821 	    SYSCTL_CHILDREN(rack_counters),
1822 	    OID_AUTO, "map_adjust_req", CTLFLAG_RD,
1823 	    &rack_adjust_map_bw,
1824 	    "Number of times we hit the case where the sb went up and down on a sendmap entry");
1825 #endif
1826 	rack_multi_single_eq = counter_u64_alloc(M_WAITOK);
1827 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1828 	    SYSCTL_CHILDREN(rack_counters),
1829 	    OID_AUTO, "cmp_ack_equiv", CTLFLAG_RD,
1830 	    &rack_multi_single_eq,
1831 	    "Number of compressed acks total represented");
1832 	rack_proc_non_comp_ack = counter_u64_alloc(M_WAITOK);
1833 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1834 	    SYSCTL_CHILDREN(rack_counters),
1835 	    OID_AUTO, "cmp_ack_not", CTLFLAG_RD,
1836 	    &rack_proc_non_comp_ack,
1837 	    "Number of non compresseds acks that we processed");
1838 
1839 
1840 	rack_sack_proc_all = counter_u64_alloc(M_WAITOK);
1841 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1842 	    SYSCTL_CHILDREN(rack_counters),
1843 	    OID_AUTO, "sack_long", CTLFLAG_RD,
1844 	    &rack_sack_proc_all,
1845 	    "Total times we had to walk whole list for sack processing");
1846 	rack_sack_proc_restart = counter_u64_alloc(M_WAITOK);
1847 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1848 	    SYSCTL_CHILDREN(rack_counters),
1849 	    OID_AUTO, "sack_restart", CTLFLAG_RD,
1850 	    &rack_sack_proc_restart,
1851 	    "Total times we had to walk whole list due to a restart");
1852 	rack_sack_proc_short = counter_u64_alloc(M_WAITOK);
1853 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1854 	    SYSCTL_CHILDREN(rack_counters),
1855 	    OID_AUTO, "sack_short", CTLFLAG_RD,
1856 	    &rack_sack_proc_short,
1857 	    "Total times we took shortcut for sack processing");
1858 	rack_enter_tlp_calc = counter_u64_alloc(M_WAITOK);
1859 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1860 	    SYSCTL_CHILDREN(rack_counters),
1861 	    OID_AUTO, "tlp_calc_entered", CTLFLAG_RD,
1862 	    &rack_enter_tlp_calc,
1863 	    "Total times we called calc-tlp");
1864 	rack_used_tlpmethod = counter_u64_alloc(M_WAITOK);
1865 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1866 	    SYSCTL_CHILDREN(rack_counters),
1867 	    OID_AUTO, "hit_tlp_method", CTLFLAG_RD,
1868 	    &rack_used_tlpmethod,
1869 	    "Total number of runt sacks");
1870 	rack_used_tlpmethod2 = counter_u64_alloc(M_WAITOK);
1871 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1872 	    SYSCTL_CHILDREN(rack_counters),
1873 	    OID_AUTO, "hit_tlp_method2", CTLFLAG_RD,
1874 	    &rack_used_tlpmethod2,
1875 	    "Total number of times we hit TLP method 2");
1876 	rack_sack_skipped_acked = counter_u64_alloc(M_WAITOK);
1877 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1878 	    SYSCTL_CHILDREN(rack_attack),
1879 	    OID_AUTO, "skipacked", CTLFLAG_RD,
1880 	    &rack_sack_skipped_acked,
1881 	    "Total number of times we skipped previously sacked");
1882 	rack_sack_splits = counter_u64_alloc(M_WAITOK);
1883 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1884 	    SYSCTL_CHILDREN(rack_attack),
1885 	    OID_AUTO, "ofsplit", CTLFLAG_RD,
1886 	    &rack_sack_splits,
1887 	    "Total number of times we did the old fashion tree split");
1888 	rack_progress_drops = counter_u64_alloc(M_WAITOK);
1889 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1890 	    SYSCTL_CHILDREN(rack_counters),
1891 	    OID_AUTO, "prog_drops", CTLFLAG_RD,
1892 	    &rack_progress_drops,
1893 	    "Total number of progress drops");
1894 	rack_input_idle_reduces = counter_u64_alloc(M_WAITOK);
1895 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1896 	    SYSCTL_CHILDREN(rack_counters),
1897 	    OID_AUTO, "idle_reduce_oninput", CTLFLAG_RD,
1898 	    &rack_input_idle_reduces,
1899 	    "Total number of idle reductions on input");
1900 	rack_collapsed_win = counter_u64_alloc(M_WAITOK);
1901 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1902 	    SYSCTL_CHILDREN(rack_counters),
1903 	    OID_AUTO, "collapsed_win", CTLFLAG_RD,
1904 	    &rack_collapsed_win,
1905 	    "Total number of collapsed windows");
1906 	rack_tlp_does_nada = counter_u64_alloc(M_WAITOK);
1907 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1908 	    SYSCTL_CHILDREN(rack_counters),
1909 	    OID_AUTO, "tlp_nada", CTLFLAG_RD,
1910 	    &rack_tlp_does_nada,
1911 	    "Total number of nada tlp calls");
1912 	rack_try_scwnd = counter_u64_alloc(M_WAITOK);
1913 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1914 	    SYSCTL_CHILDREN(rack_counters),
1915 	    OID_AUTO, "tried_scwnd", CTLFLAG_RD,
1916 	    &rack_try_scwnd,
1917 	    "Total number of scwnd attempts");
1918 
1919 	rack_per_timer_hole = counter_u64_alloc(M_WAITOK);
1920 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1921 	    SYSCTL_CHILDREN(rack_counters),
1922 	    OID_AUTO, "timer_hole", CTLFLAG_RD,
1923 	    &rack_per_timer_hole,
1924 	    "Total persists start in timer hole");
1925 
1926 	rack_sbsndptr_wrong = counter_u64_alloc(M_WAITOK);
1927 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1928 	    SYSCTL_CHILDREN(rack_counters),
1929 	    OID_AUTO, "sndptr_wrong", CTLFLAG_RD,
1930 	    &rack_sbsndptr_wrong, "Total number of times the saved sbsndptr was incorrect");
1931 	rack_sbsndptr_right = counter_u64_alloc(M_WAITOK);
1932 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1933 	    SYSCTL_CHILDREN(rack_counters),
1934 	    OID_AUTO, "sndptr_right", CTLFLAG_RD,
1935 	    &rack_sbsndptr_right, "Total number of times the saved sbsndptr was correct");
1936 
1937 	COUNTER_ARRAY_ALLOC(rack_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK);
1938 	SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
1939 	    OID_AUTO, "outsize", CTLFLAG_RD,
1940 	    rack_out_size, TCP_MSS_ACCT_SIZE, "MSS send sizes");
1941 	COUNTER_ARRAY_ALLOC(rack_opts_arry, RACK_OPTS_SIZE, M_WAITOK);
1942 	SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
1943 	    OID_AUTO, "opts", CTLFLAG_RD,
1944 	    rack_opts_arry, RACK_OPTS_SIZE, "RACK Option Stats");
1945 	SYSCTL_ADD_PROC(&rack_sysctl_ctx,
1946 	    SYSCTL_CHILDREN(rack_sysctl_root),
1947 	    OID_AUTO, "clear", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1948 	    &rack_clear_counter, 0, sysctl_rack_clear, "IU", "Clear counters");
1949 }
1950 
1951 static __inline int
1952 rb_map_cmp(struct rack_sendmap *b, struct rack_sendmap *a)
1953 {
1954 	if (SEQ_GEQ(b->r_start, a->r_start) &&
1955 	    SEQ_LT(b->r_start, a->r_end)) {
1956 		/*
1957 		 * The entry b is within the
1958 		 * block a. i.e.:
1959 		 * a --   |-------------|
1960 		 * b --   |----|
1961 		 * <or>
1962 		 * b --       |------|
1963 		 * <or>
1964 		 * b --       |-----------|
1965 		 */
1966 		return (0);
1967 	} else if (SEQ_GEQ(b->r_start, a->r_end)) {
1968 		/*
1969 		 * b falls as either the next
1970 		 * sequence block after a so a
1971 		 * is said to be smaller than b.
1972 		 * i.e:
1973 		 * a --   |------|
1974 		 * b --          |--------|
1975 		 * or
1976 		 * b --              |-----|
1977 		 */
1978 		return (1);
1979 	}
1980 	/*
1981 	 * Whats left is where a is
1982 	 * larger than b. i.e:
1983 	 * a --         |-------|
1984 	 * b --  |---|
1985 	 * or even possibly
1986 	 * b --   |--------------|
1987 	 */
1988 	return (-1);
1989 }
1990 
1991 RB_PROTOTYPE(rack_rb_tree_head, rack_sendmap, r_next, rb_map_cmp);
1992 RB_GENERATE(rack_rb_tree_head, rack_sendmap, r_next, rb_map_cmp);
1993 
1994 static uint32_t
1995 rc_init_window(struct tcp_rack *rack)
1996 {
1997 	uint32_t win;
1998 
1999 	if (rack->rc_init_win == 0) {
2000 		/*
2001 		 * Nothing set by the user, use the system stack
2002 		 * default.
2003 		 */
2004 		return (tcp_compute_initwnd(tcp_maxseg(rack->rc_tp)));
2005 	}
2006 	win = ctf_fixed_maxseg(rack->rc_tp) * rack->rc_init_win;
2007 	return (win);
2008 }
2009 
2010 static uint64_t
2011 rack_get_fixed_pacing_bw(struct tcp_rack *rack)
2012 {
2013 	if (IN_FASTRECOVERY(rack->rc_tp->t_flags))
2014 		return (rack->r_ctl.rc_fixed_pacing_rate_rec);
2015 	else if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh)
2016 		return (rack->r_ctl.rc_fixed_pacing_rate_ss);
2017 	else
2018 		return (rack->r_ctl.rc_fixed_pacing_rate_ca);
2019 }
2020 
2021 static uint64_t
2022 rack_get_bw(struct tcp_rack *rack)
2023 {
2024 	if (rack->use_fixed_rate) {
2025 		/* Return the fixed pacing rate */
2026 		return (rack_get_fixed_pacing_bw(rack));
2027 	}
2028 	if (rack->r_ctl.gp_bw == 0) {
2029 		/*
2030 		 * We have yet no b/w measurement,
2031 		 * if we have a user set initial bw
2032 		 * return it. If we don't have that and
2033 		 * we have an srtt, use the tcp IW (10) to
2034 		 * calculate a fictional b/w over the SRTT
2035 		 * which is more or less a guess. Note
2036 		 * we don't use our IW from rack on purpose
2037 		 * so if we have like IW=30, we are not
2038 		 * calculating a "huge" b/w.
2039 		 */
2040 		uint64_t bw, srtt;
2041 		if (rack->r_ctl.init_rate)
2042 			return (rack->r_ctl.init_rate);
2043 
2044 		/* Has the user set a max peak rate? */
2045 #ifdef NETFLIX_PEAKRATE
2046 		if (rack->rc_tp->t_maxpeakrate)
2047 			return (rack->rc_tp->t_maxpeakrate);
2048 #endif
2049 		/* Ok lets come up with the IW guess, if we have a srtt */
2050 		if (rack->rc_tp->t_srtt == 0) {
2051 			/*
2052 			 * Go with old pacing method
2053 			 * i.e. burst mitigation only.
2054 			 */
2055 			return (0);
2056 		}
2057 		/* Ok lets get the initial TCP win (not racks) */
2058 		bw = tcp_compute_initwnd(tcp_maxseg(rack->rc_tp));
2059 		srtt = (uint64_t)rack->rc_tp->t_srtt;
2060 		bw *= (uint64_t)USECS_IN_SECOND;
2061 		bw /= srtt;
2062 		if (rack->r_ctl.bw_rate_cap && (bw > rack->r_ctl.bw_rate_cap))
2063 			bw = rack->r_ctl.bw_rate_cap;
2064 		return (bw);
2065 	} else {
2066 		uint64_t bw;
2067 
2068 		if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) {
2069 			/* Averaging is done, we can return the value */
2070 			bw = rack->r_ctl.gp_bw;
2071 		} else {
2072 			/* Still doing initial average must calculate */
2073 			bw = rack->r_ctl.gp_bw / rack->r_ctl.num_measurements;
2074 		}
2075 #ifdef NETFLIX_PEAKRATE
2076 		if ((rack->rc_tp->t_maxpeakrate) &&
2077 		    (bw > rack->rc_tp->t_maxpeakrate)) {
2078 			/* The user has set a peak rate to pace at
2079 			 * don't allow us to pace faster than that.
2080 			 */
2081 			return (rack->rc_tp->t_maxpeakrate);
2082 		}
2083 #endif
2084 		if (rack->r_ctl.bw_rate_cap && (bw > rack->r_ctl.bw_rate_cap))
2085 			bw = rack->r_ctl.bw_rate_cap;
2086 		return (bw);
2087 	}
2088 }
2089 
2090 static uint16_t
2091 rack_get_output_gain(struct tcp_rack *rack, struct rack_sendmap *rsm)
2092 {
2093 	if (rack->use_fixed_rate) {
2094 		return (100);
2095 	} else if (rack->in_probe_rtt && (rsm == NULL))
2096 		return (rack->r_ctl.rack_per_of_gp_probertt);
2097 	else if ((IN_FASTRECOVERY(rack->rc_tp->t_flags) &&
2098 		  rack->r_ctl.rack_per_of_gp_rec)) {
2099 		if (rsm) {
2100 			/* a retransmission always use the recovery rate */
2101 			return (rack->r_ctl.rack_per_of_gp_rec);
2102 		} else if (rack->rack_rec_nonrxt_use_cr) {
2103 			/* Directed to use the configured rate */
2104 			goto configured_rate;
2105 		} else if (rack->rack_no_prr &&
2106 			   (rack->r_ctl.rack_per_of_gp_rec > 100)) {
2107 			/* No PRR, lets just use the b/w estimate only */
2108 			return (100);
2109 		} else {
2110 			/*
2111 			 * Here we may have a non-retransmit but we
2112 			 * have no overrides, so just use the recovery
2113 			 * rate (prr is in effect).
2114 			 */
2115 			return (rack->r_ctl.rack_per_of_gp_rec);
2116 		}
2117 	}
2118 configured_rate:
2119 	/* For the configured rate we look at our cwnd vs the ssthresh */
2120 	if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh)
2121 		return (rack->r_ctl.rack_per_of_gp_ss);
2122 	else
2123 		return (rack->r_ctl.rack_per_of_gp_ca);
2124 }
2125 
2126 static void
2127 rack_log_dsack_event(struct tcp_rack *rack, uint8_t mod, uint32_t flex4, uint32_t flex5, uint32_t flex6)
2128 {
2129 	/*
2130 	 * Types of logs (mod value)
2131 	 * 1 = dsack_persists reduced by 1 via T-O or fast recovery exit.
2132 	 * 2 = a dsack round begins, persist is reset to 16.
2133 	 * 3 = a dsack round ends
2134 	 * 4 = Dsack option increases rack rtt flex5 is the srtt input, flex6 is thresh
2135 	 * 5 = Socket option set changing the control flags rc_rack_tmr_std_based, rc_rack_use_dsack
2136 	 * 6 = Final rack rtt, flex4 is srtt and flex6 is final limited thresh.
2137 	 */
2138 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2139 		union tcp_log_stackspecific log;
2140 		struct timeval tv;
2141 
2142 		memset(&log, 0, sizeof(log));
2143 		log.u_bbr.flex1 = rack->rc_rack_tmr_std_based;
2144 		log.u_bbr.flex1 <<= 1;
2145 		log.u_bbr.flex1 |= rack->rc_rack_use_dsack;
2146 		log.u_bbr.flex1 <<= 1;
2147 		log.u_bbr.flex1 |= rack->rc_dsack_round_seen;
2148 		log.u_bbr.flex2 = rack->r_ctl.dsack_round_end;
2149 		log.u_bbr.flex3 = rack->r_ctl.num_dsack;
2150 		log.u_bbr.flex4 = flex4;
2151 		log.u_bbr.flex5 = flex5;
2152 		log.u_bbr.flex6 = flex6;
2153 		log.u_bbr.flex7 = rack->r_ctl.dsack_persist;
2154 		log.u_bbr.flex8 = mod;
2155 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2156 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2157 		    &rack->rc_inp->inp_socket->so_rcv,
2158 		    &rack->rc_inp->inp_socket->so_snd,
2159 		    RACK_DSACK_HANDLING, 0,
2160 		    0, &log, false, &tv);
2161 	}
2162 }
2163 
2164 static void
2165 rack_log_hdwr_pacing(struct tcp_rack *rack,
2166 		     uint64_t rate, uint64_t hw_rate, int line,
2167 		     int error, uint16_t mod)
2168 {
2169 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2170 		union tcp_log_stackspecific log;
2171 		struct timeval tv;
2172 		const struct ifnet *ifp;
2173 
2174 		memset(&log, 0, sizeof(log));
2175 		log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff);
2176 		log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff);
2177 		if (rack->r_ctl.crte) {
2178 			ifp = rack->r_ctl.crte->ptbl->rs_ifp;
2179 		} else if (rack->rc_inp->inp_route.ro_nh &&
2180 			   rack->rc_inp->inp_route.ro_nh->nh_ifp) {
2181 			ifp = rack->rc_inp->inp_route.ro_nh->nh_ifp;
2182 		} else
2183 			ifp = NULL;
2184 		if (ifp) {
2185 			log.u_bbr.flex3 = (((uint64_t)ifp  >> 32) & 0x00000000ffffffff);
2186 			log.u_bbr.flex4 = ((uint64_t)ifp & 0x00000000ffffffff);
2187 		}
2188 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2189 		log.u_bbr.bw_inuse = rate;
2190 		log.u_bbr.flex5 = line;
2191 		log.u_bbr.flex6 = error;
2192 		log.u_bbr.flex7 = mod;
2193 		log.u_bbr.applimited = rack->r_ctl.rc_pace_max_segs;
2194 		log.u_bbr.flex8 = rack->use_fixed_rate;
2195 		log.u_bbr.flex8 <<= 1;
2196 		log.u_bbr.flex8 |= rack->rack_hdrw_pacing;
2197 		log.u_bbr.pkts_out = rack->rc_tp->t_maxseg;
2198 		log.u_bbr.delRate = rack->r_ctl.crte_prev_rate;
2199 		if (rack->r_ctl.crte)
2200 			log.u_bbr.cur_del_rate = rack->r_ctl.crte->rate;
2201 		else
2202 			log.u_bbr.cur_del_rate = 0;
2203 		log.u_bbr.rttProp = rack->r_ctl.last_hw_bw_req;
2204 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2205 		    &rack->rc_inp->inp_socket->so_rcv,
2206 		    &rack->rc_inp->inp_socket->so_snd,
2207 		    BBR_LOG_HDWR_PACE, 0,
2208 		    0, &log, false, &tv);
2209 	}
2210 }
2211 
2212 static uint64_t
2213 rack_get_output_bw(struct tcp_rack *rack, uint64_t bw, struct rack_sendmap *rsm, int *capped)
2214 {
2215 	/*
2216 	 * We allow rack_per_of_gp_xx to dictate our bw rate we want.
2217 	 */
2218 	uint64_t bw_est, high_rate;
2219 	uint64_t gain;
2220 
2221 	gain = (uint64_t)rack_get_output_gain(rack, rsm);
2222 	bw_est = bw * gain;
2223 	bw_est /= (uint64_t)100;
2224 	/* Never fall below the minimum (def 64kbps) */
2225 	if (bw_est < RACK_MIN_BW)
2226 		bw_est = RACK_MIN_BW;
2227 	if (rack->r_rack_hw_rate_caps) {
2228 		/* Rate caps are in place */
2229 		if (rack->r_ctl.crte != NULL) {
2230 			/* We have a hdwr rate already */
2231 			high_rate = tcp_hw_highest_rate(rack->r_ctl.crte);
2232 			if (bw_est >= high_rate) {
2233 				/* We are capping bw at the highest rate table entry */
2234 				rack_log_hdwr_pacing(rack,
2235 						     bw_est, high_rate, __LINE__,
2236 						     0, 3);
2237 				bw_est = high_rate;
2238 				if (capped)
2239 					*capped = 1;
2240 			}
2241 		} else if ((rack->rack_hdrw_pacing == 0) &&
2242 			   (rack->rack_hdw_pace_ena) &&
2243 			   (rack->rack_attempt_hdwr_pace == 0) &&
2244 			   (rack->rc_inp->inp_route.ro_nh != NULL) &&
2245 			   (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
2246 			/*
2247 			 * Special case, we have not yet attempted hardware
2248 			 * pacing, and yet we may, when we do, find out if we are
2249 			 * above the highest rate. We need to know the maxbw for the interface
2250 			 * in question (if it supports ratelimiting). We get back
2251 			 * a 0, if the interface is not found in the RL lists.
2252 			 */
2253 			high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp);
2254 			if (high_rate) {
2255 				/* Yep, we have a rate is it above this rate? */
2256 				if (bw_est > high_rate) {
2257 					bw_est = high_rate;
2258 					if (capped)
2259 						*capped = 1;
2260 				}
2261 			}
2262 		}
2263 	}
2264 	return (bw_est);
2265 }
2266 
2267 static void
2268 rack_log_retran_reason(struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t tsused, uint32_t thresh, int mod)
2269 {
2270 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2271 		union tcp_log_stackspecific log;
2272 		struct timeval tv;
2273 
2274 		if ((mod != 1) && (rack_verbose_logging == 0)) {
2275 			/*
2276 			 * We get 3 values currently for mod
2277 			 * 1 - We are retransmitting and this tells the reason.
2278 			 * 2 - We are clearing a dup-ack count.
2279 			 * 3 - We are incrementing a dup-ack count.
2280 			 *
2281 			 * The clear/increment are only logged
2282 			 * if you have BBverbose on.
2283 			 */
2284 			return;
2285 		}
2286 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2287 		log.u_bbr.flex1 = tsused;
2288 		log.u_bbr.flex2 = thresh;
2289 		log.u_bbr.flex3 = rsm->r_flags;
2290 		log.u_bbr.flex4 = rsm->r_dupack;
2291 		log.u_bbr.flex5 = rsm->r_start;
2292 		log.u_bbr.flex6 = rsm->r_end;
2293 		log.u_bbr.flex8 = mod;
2294 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2295 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2296 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2297 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2298 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2299 		log.u_bbr.pacing_gain = rack->r_must_retran;
2300 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2301 		    &rack->rc_inp->inp_socket->so_rcv,
2302 		    &rack->rc_inp->inp_socket->so_snd,
2303 		    BBR_LOG_SETTINGS_CHG, 0,
2304 		    0, &log, false, &tv);
2305 	}
2306 }
2307 
2308 static void
2309 rack_log_to_start(struct tcp_rack *rack, uint32_t cts, uint32_t to, int32_t slot, uint8_t which)
2310 {
2311 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2312 		union tcp_log_stackspecific log;
2313 		struct timeval tv;
2314 
2315 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2316 		log.u_bbr.flex1 = rack->rc_tp->t_srtt;
2317 		log.u_bbr.flex2 = to;
2318 		log.u_bbr.flex3 = rack->r_ctl.rc_hpts_flags;
2319 		log.u_bbr.flex4 = slot;
2320 		log.u_bbr.flex5 = rack->rc_inp->inp_hptsslot;
2321 		log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
2322 		log.u_bbr.flex7 = rack->rc_in_persist;
2323 		log.u_bbr.flex8 = which;
2324 		if (rack->rack_no_prr)
2325 			log.u_bbr.pkts_out = 0;
2326 		else
2327 			log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
2328 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2329 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2330 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2331 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2332 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2333 		log.u_bbr.pacing_gain = rack->r_must_retran;
2334 		log.u_bbr.lt_epoch = rack->rc_tp->t_rxtshift;
2335 		log.u_bbr.lost = rack_rto_min;
2336 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2337 		    &rack->rc_inp->inp_socket->so_rcv,
2338 		    &rack->rc_inp->inp_socket->so_snd,
2339 		    BBR_LOG_TIMERSTAR, 0,
2340 		    0, &log, false, &tv);
2341 	}
2342 }
2343 
2344 static void
2345 rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm)
2346 {
2347 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2348 		union tcp_log_stackspecific log;
2349 		struct timeval tv;
2350 
2351 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2352 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2353 		log.u_bbr.flex8 = to_num;
2354 		log.u_bbr.flex1 = rack->r_ctl.rc_rack_min_rtt;
2355 		log.u_bbr.flex2 = rack->rc_rack_rtt;
2356 		if (rsm == NULL)
2357 			log.u_bbr.flex3 = 0;
2358 		else
2359 			log.u_bbr.flex3 = rsm->r_end - rsm->r_start;
2360 		if (rack->rack_no_prr)
2361 			log.u_bbr.flex5 = 0;
2362 		else
2363 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
2364 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2365 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2366 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2367 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2368 		log.u_bbr.pacing_gain = rack->r_must_retran;
2369 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2370 		    &rack->rc_inp->inp_socket->so_rcv,
2371 		    &rack->rc_inp->inp_socket->so_snd,
2372 		    BBR_LOG_RTO, 0,
2373 		    0, &log, false, &tv);
2374 	}
2375 }
2376 
2377 static void
2378 rack_log_map_chg(struct tcpcb *tp, struct tcp_rack *rack,
2379 		 struct rack_sendmap *prev,
2380 		 struct rack_sendmap *rsm,
2381 		 struct rack_sendmap *next,
2382 		 int flag, uint32_t th_ack, int line)
2383 {
2384 	if (rack_verbose_logging && (tp->t_logstate != TCP_LOG_STATE_OFF)) {
2385 		union tcp_log_stackspecific log;
2386 		struct timeval tv;
2387 
2388 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2389 		log.u_bbr.flex8 = flag;
2390 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2391 		log.u_bbr.cur_del_rate = (uint64_t)prev;
2392 		log.u_bbr.delRate = (uint64_t)rsm;
2393 		log.u_bbr.rttProp = (uint64_t)next;
2394 		log.u_bbr.flex7 = 0;
2395 		if (prev) {
2396 			log.u_bbr.flex1 = prev->r_start;
2397 			log.u_bbr.flex2 = prev->r_end;
2398 			log.u_bbr.flex7 |= 0x4;
2399 		}
2400 		if (rsm) {
2401 			log.u_bbr.flex3 = rsm->r_start;
2402 			log.u_bbr.flex4 = rsm->r_end;
2403 			log.u_bbr.flex7 |= 0x2;
2404 		}
2405 		if (next) {
2406 			log.u_bbr.flex5 = next->r_start;
2407 			log.u_bbr.flex6 = next->r_end;
2408 			log.u_bbr.flex7 |= 0x1;
2409 		}
2410 		log.u_bbr.applimited = line;
2411 		log.u_bbr.pkts_out = th_ack;
2412 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2413 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2414 		if (rack->rack_no_prr)
2415 			log.u_bbr.lost = 0;
2416 		else
2417 			log.u_bbr.lost = rack->r_ctl.rc_prr_sndcnt;
2418 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2419 		    &rack->rc_inp->inp_socket->so_rcv,
2420 		    &rack->rc_inp->inp_socket->so_snd,
2421 		    TCP_LOG_MAPCHG, 0,
2422 		    0, &log, false, &tv);
2423 	}
2424 }
2425 
2426 static void
2427 rack_log_rtt_upd(struct tcpcb *tp, struct tcp_rack *rack, uint32_t t, uint32_t len,
2428 		 struct rack_sendmap *rsm, int conf)
2429 {
2430 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
2431 		union tcp_log_stackspecific log;
2432 		struct timeval tv;
2433 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2434 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2435 		log.u_bbr.flex1 = t;
2436 		log.u_bbr.flex2 = len;
2437 		log.u_bbr.flex3 = rack->r_ctl.rc_rack_min_rtt;
2438 		log.u_bbr.flex4 = rack->r_ctl.rack_rs.rs_rtt_lowest;
2439 		log.u_bbr.flex5 = rack->r_ctl.rack_rs.rs_rtt_highest;
2440 		log.u_bbr.flex6 = rack->r_ctl.rack_rs.rs_us_rtrcnt;
2441 		log.u_bbr.flex7 = conf;
2442 		log.u_bbr.rttProp = (uint64_t)rack->r_ctl.rack_rs.rs_rtt_tot;
2443 		log.u_bbr.flex8 = rack->r_ctl.rc_rate_sample_method;
2444 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2445 		log.u_bbr.delivered = rack->r_ctl.rack_rs.rs_us_rtrcnt;
2446 		log.u_bbr.pkts_out = rack->r_ctl.rack_rs.rs_flags;
2447 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2448 		if (rsm) {
2449 			log.u_bbr.pkt_epoch = rsm->r_start;
2450 			log.u_bbr.lost = rsm->r_end;
2451 			log.u_bbr.cwnd_gain = rsm->r_rtr_cnt;
2452 			/* We loose any upper of the 24 bits */
2453 			log.u_bbr.pacing_gain = (uint16_t)rsm->r_flags;
2454 		} else {
2455 			/* Its a SYN */
2456 			log.u_bbr.pkt_epoch = rack->rc_tp->iss;
2457 			log.u_bbr.lost = 0;
2458 			log.u_bbr.cwnd_gain = 0;
2459 			log.u_bbr.pacing_gain = 0;
2460 		}
2461 		/* Write out general bits of interest rrs here */
2462 		log.u_bbr.use_lt_bw = rack->rc_highly_buffered;
2463 		log.u_bbr.use_lt_bw <<= 1;
2464 		log.u_bbr.use_lt_bw |= rack->forced_ack;
2465 		log.u_bbr.use_lt_bw <<= 1;
2466 		log.u_bbr.use_lt_bw |= rack->rc_gp_dyn_mul;
2467 		log.u_bbr.use_lt_bw <<= 1;
2468 		log.u_bbr.use_lt_bw |= rack->in_probe_rtt;
2469 		log.u_bbr.use_lt_bw <<= 1;
2470 		log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt;
2471 		log.u_bbr.use_lt_bw <<= 1;
2472 		log.u_bbr.use_lt_bw |= rack->app_limited_needs_set;
2473 		log.u_bbr.use_lt_bw <<= 1;
2474 		log.u_bbr.use_lt_bw |= rack->rc_gp_filled;
2475 		log.u_bbr.use_lt_bw <<= 1;
2476 		log.u_bbr.use_lt_bw |= rack->rc_dragged_bottom;
2477 		log.u_bbr.applimited = rack->r_ctl.rc_target_probertt_flight;
2478 		log.u_bbr.epoch = rack->r_ctl.rc_time_probertt_starts;
2479 		log.u_bbr.lt_epoch = rack->r_ctl.rc_time_probertt_entered;
2480 		log.u_bbr.cur_del_rate = rack->r_ctl.rc_lower_rtt_us_cts;
2481 		log.u_bbr.delRate = rack->r_ctl.rc_gp_srtt;
2482 		log.u_bbr.bw_inuse = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
2483 		log.u_bbr.bw_inuse <<= 32;
2484 		if (rsm)
2485 			log.u_bbr.bw_inuse |= ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]);
2486 		TCP_LOG_EVENTP(tp, NULL,
2487 		    &rack->rc_inp->inp_socket->so_rcv,
2488 		    &rack->rc_inp->inp_socket->so_snd,
2489 		    BBR_LOG_BBRRTT, 0,
2490 		    0, &log, false, &tv);
2491 
2492 
2493 	}
2494 }
2495 
2496 static void
2497 rack_log_rtt_sample(struct tcp_rack *rack, uint32_t rtt)
2498 {
2499 	/*
2500 	 * Log the rtt sample we are
2501 	 * applying to the srtt algorithm in
2502 	 * useconds.
2503 	 */
2504 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2505 		union tcp_log_stackspecific log;
2506 		struct timeval tv;
2507 
2508 		/* Convert our ms to a microsecond */
2509 		memset(&log, 0, sizeof(log));
2510 		log.u_bbr.flex1 = rtt;
2511 		log.u_bbr.flex2 = rack->r_ctl.ack_count;
2512 		log.u_bbr.flex3 = rack->r_ctl.sack_count;
2513 		log.u_bbr.flex4 = rack->r_ctl.sack_noextra_move;
2514 		log.u_bbr.flex5 = rack->r_ctl.sack_moved_extra;
2515 		log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
2516 		log.u_bbr.flex7 = 1;
2517 		log.u_bbr.flex8 = rack->sack_attack_disable;
2518 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2519 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2520 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2521 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2522 		log.u_bbr.pacing_gain = rack->r_must_retran;
2523 		/*
2524 		 * We capture in delRate the upper 32 bits as
2525 		 * the confidence level we had declared, and the
2526 		 * lower 32 bits as the actual RTT using the arrival
2527 		 * timestamp.
2528 		 */
2529 		log.u_bbr.delRate = rack->r_ctl.rack_rs.confidence;
2530 		log.u_bbr.delRate <<= 32;
2531 		log.u_bbr.delRate |= rack->r_ctl.rack_rs.rs_us_rtt;
2532 		/* Lets capture all the things that make up t_rtxcur */
2533 		log.u_bbr.applimited = rack_rto_min;
2534 		log.u_bbr.epoch = rack_rto_max;
2535 		log.u_bbr.lt_epoch = rack->r_ctl.timer_slop;
2536 		log.u_bbr.lost = rack_rto_min;
2537 		log.u_bbr.pkt_epoch = TICKS_2_USEC(tcp_rexmit_slop);
2538 		log.u_bbr.rttProp = RACK_REXMTVAL(rack->rc_tp);
2539 		log.u_bbr.bw_inuse = rack->r_ctl.act_rcv_time.tv_sec;
2540 		log.u_bbr.bw_inuse *= HPTS_USEC_IN_SEC;
2541 		log.u_bbr.bw_inuse += rack->r_ctl.act_rcv_time.tv_usec;
2542 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2543 		    &rack->rc_inp->inp_socket->so_rcv,
2544 		    &rack->rc_inp->inp_socket->so_snd,
2545 		    TCP_LOG_RTT, 0,
2546 		    0, &log, false, &tv);
2547 	}
2548 }
2549 
2550 static void
2551 rack_log_rtt_sample_calc(struct tcp_rack *rack, uint32_t rtt, uint32_t send_time, uint32_t ack_time, int where)
2552 {
2553 	if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2554 		union tcp_log_stackspecific log;
2555 		struct timeval tv;
2556 
2557 		/* Convert our ms to a microsecond */
2558 		memset(&log, 0, sizeof(log));
2559 		log.u_bbr.flex1 = rtt;
2560 		log.u_bbr.flex2 = send_time;
2561 		log.u_bbr.flex3 = ack_time;
2562 		log.u_bbr.flex4 = where;
2563 		log.u_bbr.flex7 = 2;
2564 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2565 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2566 		    &rack->rc_inp->inp_socket->so_rcv,
2567 		    &rack->rc_inp->inp_socket->so_snd,
2568 		    TCP_LOG_RTT, 0,
2569 		    0, &log, false, &tv);
2570 	}
2571 }
2572 
2573 
2574 
2575 static inline void
2576 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick,  int event, int line)
2577 {
2578 	if (rack_verbose_logging && (tp->t_logstate != TCP_LOG_STATE_OFF)) {
2579 		union tcp_log_stackspecific log;
2580 		struct timeval tv;
2581 
2582 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2583 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2584 		log.u_bbr.flex1 = line;
2585 		log.u_bbr.flex2 = tick;
2586 		log.u_bbr.flex3 = tp->t_maxunacktime;
2587 		log.u_bbr.flex4 = tp->t_acktime;
2588 		log.u_bbr.flex8 = event;
2589 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2590 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2591 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2592 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2593 		log.u_bbr.pacing_gain = rack->r_must_retran;
2594 		TCP_LOG_EVENTP(tp, NULL,
2595 		    &rack->rc_inp->inp_socket->so_rcv,
2596 		    &rack->rc_inp->inp_socket->so_snd,
2597 		    BBR_LOG_PROGRESS, 0,
2598 		    0, &log, false, &tv);
2599 	}
2600 }
2601 
2602 static void
2603 rack_log_type_bbrsnd(struct tcp_rack *rack, uint32_t len, uint32_t slot, uint32_t cts, struct timeval *tv)
2604 {
2605 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2606 		union tcp_log_stackspecific log;
2607 
2608 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2609 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2610 		log.u_bbr.flex1 = slot;
2611 		if (rack->rack_no_prr)
2612 			log.u_bbr.flex2 = 0;
2613 		else
2614 			log.u_bbr.flex2 = rack->r_ctl.rc_prr_sndcnt;
2615 		log.u_bbr.flex7 = (0x0000ffff & rack->r_ctl.rc_hpts_flags);
2616 		log.u_bbr.flex8 = rack->rc_in_persist;
2617 		log.u_bbr.timeStamp = cts;
2618 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2619 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2620 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2621 		log.u_bbr.pacing_gain = rack->r_must_retran;
2622 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2623 		    &rack->rc_inp->inp_socket->so_rcv,
2624 		    &rack->rc_inp->inp_socket->so_snd,
2625 		    BBR_LOG_BBRSND, 0,
2626 		    0, &log, false, tv);
2627 	}
2628 }
2629 
2630 static void
2631 rack_log_doseg_done(struct tcp_rack *rack, uint32_t cts, int32_t nxt_pkt, int32_t did_out, int way_out, int nsegs)
2632 {
2633 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2634 		union tcp_log_stackspecific log;
2635 		struct timeval tv;
2636 
2637 		memset(&log, 0, sizeof(log));
2638 		log.u_bbr.flex1 = did_out;
2639 		log.u_bbr.flex2 = nxt_pkt;
2640 		log.u_bbr.flex3 = way_out;
2641 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
2642 		if (rack->rack_no_prr)
2643 			log.u_bbr.flex5 = 0;
2644 		else
2645 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
2646 		log.u_bbr.flex6 = nsegs;
2647 		log.u_bbr.applimited = rack->r_ctl.rc_pace_min_segs;
2648 		log.u_bbr.flex7 = rack->rc_ack_can_sendout_data;	/* Do we have ack-can-send set */
2649 		log.u_bbr.flex7 <<= 1;
2650 		log.u_bbr.flex7 |= rack->r_fast_output;	/* is fast output primed */
2651 		log.u_bbr.flex7 <<= 1;
2652 		log.u_bbr.flex7 |= rack->r_wanted_output;	/* Do we want output */
2653 		log.u_bbr.flex8 = rack->rc_in_persist;
2654 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2655 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2656 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2657 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
2658 		log.u_bbr.use_lt_bw <<= 1;
2659 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
2660 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2661 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2662 		log.u_bbr.pacing_gain = rack->r_must_retran;
2663 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2664 		    &rack->rc_inp->inp_socket->so_rcv,
2665 		    &rack->rc_inp->inp_socket->so_snd,
2666 		    BBR_LOG_DOSEG_DONE, 0,
2667 		    0, &log, false, &tv);
2668 	}
2669 }
2670 
2671 static void
2672 rack_log_type_pacing_sizes(struct tcpcb *tp, struct tcp_rack *rack, uint32_t arg1, uint32_t arg2, uint32_t arg3, uint8_t frm)
2673 {
2674 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
2675 		union tcp_log_stackspecific log;
2676 		struct timeval tv;
2677 
2678 		memset(&log, 0, sizeof(log));
2679 		log.u_bbr.flex1 = rack->r_ctl.rc_pace_min_segs;
2680 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
2681 		log.u_bbr.flex4 = arg1;
2682 		log.u_bbr.flex5 = arg2;
2683 		log.u_bbr.flex6 = arg3;
2684 		log.u_bbr.flex8 = frm;
2685 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2686 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2687 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2688 		log.u_bbr.applimited = rack->r_ctl.rc_sacked;
2689 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2690 		log.u_bbr.pacing_gain = rack->r_must_retran;
2691 		TCP_LOG_EVENTP(tp, NULL,
2692 		    &tp->t_inpcb->inp_socket->so_rcv,
2693 		    &tp->t_inpcb->inp_socket->so_snd,
2694 		    TCP_HDWR_PACE_SIZE, 0,
2695 		    0, &log, false, &tv);
2696 	}
2697 }
2698 
2699 static void
2700 rack_log_type_just_return(struct tcp_rack *rack, uint32_t cts, uint32_t tlen, uint32_t slot,
2701 			  uint8_t hpts_calling, int reason, uint32_t cwnd_to_use)
2702 {
2703 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2704 		union tcp_log_stackspecific log;
2705 		struct timeval tv;
2706 
2707 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2708 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2709 		log.u_bbr.flex1 = slot;
2710 		log.u_bbr.flex2 = rack->r_ctl.rc_hpts_flags;
2711 		log.u_bbr.flex4 = reason;
2712 		if (rack->rack_no_prr)
2713 			log.u_bbr.flex5 = 0;
2714 		else
2715 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
2716 		log.u_bbr.flex7 = hpts_calling;
2717 		log.u_bbr.flex8 = rack->rc_in_persist;
2718 		log.u_bbr.lt_epoch = cwnd_to_use;
2719 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2720 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2721 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2722 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2723 		log.u_bbr.pacing_gain = rack->r_must_retran;
2724 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2725 		    &rack->rc_inp->inp_socket->so_rcv,
2726 		    &rack->rc_inp->inp_socket->so_snd,
2727 		    BBR_LOG_JUSTRET, 0,
2728 		    tlen, &log, false, &tv);
2729 	}
2730 }
2731 
2732 static void
2733 rack_log_to_cancel(struct tcp_rack *rack, int32_t hpts_removed, int line, uint32_t us_cts,
2734 		   struct timeval *tv, uint32_t flags_on_entry)
2735 {
2736 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2737 		union tcp_log_stackspecific log;
2738 
2739 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2740 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2741 		log.u_bbr.flex1 = line;
2742 		log.u_bbr.flex2 = rack->r_ctl.rc_last_output_to;
2743 		log.u_bbr.flex3 = flags_on_entry;
2744 		log.u_bbr.flex4 = us_cts;
2745 		if (rack->rack_no_prr)
2746 			log.u_bbr.flex5 = 0;
2747 		else
2748 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
2749 		log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
2750 		log.u_bbr.flex7 = hpts_removed;
2751 		log.u_bbr.flex8 = 1;
2752 		log.u_bbr.applimited = rack->r_ctl.rc_hpts_flags;
2753 		log.u_bbr.timeStamp = us_cts;
2754 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2755 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2756 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2757 		log.u_bbr.pacing_gain = rack->r_must_retran;
2758 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2759 		    &rack->rc_inp->inp_socket->so_rcv,
2760 		    &rack->rc_inp->inp_socket->so_snd,
2761 		    BBR_LOG_TIMERCANC, 0,
2762 		    0, &log, false, tv);
2763 	}
2764 }
2765 
2766 static void
2767 rack_log_alt_to_to_cancel(struct tcp_rack *rack,
2768 			  uint32_t flex1, uint32_t flex2,
2769 			  uint32_t flex3, uint32_t flex4,
2770 			  uint32_t flex5, uint32_t flex6,
2771 			  uint16_t flex7, uint8_t mod)
2772 {
2773 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2774 		union tcp_log_stackspecific log;
2775 		struct timeval tv;
2776 
2777 		if (mod == 1) {
2778 			/* No you can't use 1, its for the real to cancel */
2779 			return;
2780 		}
2781 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2782 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2783 		log.u_bbr.flex1 = flex1;
2784 		log.u_bbr.flex2 = flex2;
2785 		log.u_bbr.flex3 = flex3;
2786 		log.u_bbr.flex4 = flex4;
2787 		log.u_bbr.flex5 = flex5;
2788 		log.u_bbr.flex6 = flex6;
2789 		log.u_bbr.flex7 = flex7;
2790 		log.u_bbr.flex8 = mod;
2791 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2792 		    &rack->rc_inp->inp_socket->so_rcv,
2793 		    &rack->rc_inp->inp_socket->so_snd,
2794 		    BBR_LOG_TIMERCANC, 0,
2795 		    0, &log, false, &tv);
2796 	}
2797 }
2798 
2799 static void
2800 rack_log_to_processing(struct tcp_rack *rack, uint32_t cts, int32_t ret, int32_t timers)
2801 {
2802 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2803 		union tcp_log_stackspecific log;
2804 		struct timeval tv;
2805 
2806 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2807 		log.u_bbr.flex1 = timers;
2808 		log.u_bbr.flex2 = ret;
2809 		log.u_bbr.flex3 = rack->r_ctl.rc_timer_exp;
2810 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
2811 		log.u_bbr.flex5 = cts;
2812 		if (rack->rack_no_prr)
2813 			log.u_bbr.flex6 = 0;
2814 		else
2815 			log.u_bbr.flex6 = rack->r_ctl.rc_prr_sndcnt;
2816 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2817 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2818 		log.u_bbr.pacing_gain = rack->r_must_retran;
2819 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2820 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2821 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2822 		    &rack->rc_inp->inp_socket->so_rcv,
2823 		    &rack->rc_inp->inp_socket->so_snd,
2824 		    BBR_LOG_TO_PROCESS, 0,
2825 		    0, &log, false, &tv);
2826 	}
2827 }
2828 
2829 static void
2830 rack_log_to_prr(struct tcp_rack *rack, int frm, int orig_cwnd)
2831 {
2832 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2833 		union tcp_log_stackspecific log;
2834 		struct timeval tv;
2835 
2836 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2837 		log.u_bbr.flex1 = rack->r_ctl.rc_prr_out;
2838 		log.u_bbr.flex2 = rack->r_ctl.rc_prr_recovery_fs;
2839 		if (rack->rack_no_prr)
2840 			log.u_bbr.flex3 = 0;
2841 		else
2842 			log.u_bbr.flex3 = rack->r_ctl.rc_prr_sndcnt;
2843 		log.u_bbr.flex4 = rack->r_ctl.rc_prr_delivered;
2844 		log.u_bbr.flex5 = rack->r_ctl.rc_sacked;
2845 		log.u_bbr.flex6 = rack->r_ctl.rc_holes_rxt;
2846 		log.u_bbr.flex8 = frm;
2847 		log.u_bbr.pkts_out = orig_cwnd;
2848 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2849 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2850 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
2851 		log.u_bbr.use_lt_bw <<= 1;
2852 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
2853 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2854 		    &rack->rc_inp->inp_socket->so_rcv,
2855 		    &rack->rc_inp->inp_socket->so_snd,
2856 		    BBR_LOG_BBRUPD, 0,
2857 		    0, &log, false, &tv);
2858 	}
2859 }
2860 
2861 #ifdef NETFLIX_EXP_DETECTION
2862 static void
2863 rack_log_sad(struct tcp_rack *rack, int event)
2864 {
2865 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2866 		union tcp_log_stackspecific log;
2867 		struct timeval tv;
2868 
2869 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2870 		log.u_bbr.flex1 = rack->r_ctl.sack_count;
2871 		log.u_bbr.flex2 = rack->r_ctl.ack_count;
2872 		log.u_bbr.flex3 = rack->r_ctl.sack_moved_extra;
2873 		log.u_bbr.flex4 = rack->r_ctl.sack_noextra_move;
2874 		log.u_bbr.flex5 = rack->r_ctl.rc_num_maps_alloced;
2875 		log.u_bbr.flex6 = tcp_sack_to_ack_thresh;
2876 		log.u_bbr.pkts_out = tcp_sack_to_move_thresh;
2877 		log.u_bbr.lt_epoch = (tcp_force_detection << 8);
2878 		log.u_bbr.lt_epoch |= rack->do_detection;
2879 		log.u_bbr.applimited = tcp_map_minimum;
2880 		log.u_bbr.flex7 = rack->sack_attack_disable;
2881 		log.u_bbr.flex8 = event;
2882 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2883 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2884 		log.u_bbr.delivered = tcp_sad_decay_val;
2885 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2886 		    &rack->rc_inp->inp_socket->so_rcv,
2887 		    &rack->rc_inp->inp_socket->so_snd,
2888 		    TCP_SAD_DETECTION, 0,
2889 		    0, &log, false, &tv);
2890 	}
2891 }
2892 #endif
2893 
2894 static void
2895 rack_counter_destroy(void)
2896 {
2897 	int i;
2898 
2899 	counter_u64_free(rack_fto_send);
2900 	counter_u64_free(rack_fto_rsm_send);
2901 	counter_u64_free(rack_nfto_resend);
2902 	counter_u64_free(rack_hw_pace_init_fail);
2903 	counter_u64_free(rack_hw_pace_lost);
2904 	counter_u64_free(rack_non_fto_send);
2905 	counter_u64_free(rack_extended_rfo);
2906 	counter_u64_free(rack_ack_total);
2907 	counter_u64_free(rack_express_sack);
2908 	counter_u64_free(rack_sack_total);
2909 	counter_u64_free(rack_move_none);
2910 	counter_u64_free(rack_move_some);
2911 	counter_u64_free(rack_sack_attacks_detected);
2912 	counter_u64_free(rack_sack_attacks_reversed);
2913 	counter_u64_free(rack_sack_used_next_merge);
2914 	counter_u64_free(rack_sack_used_prev_merge);
2915 	counter_u64_free(rack_badfr);
2916 	counter_u64_free(rack_badfr_bytes);
2917 	counter_u64_free(rack_rtm_prr_retran);
2918 	counter_u64_free(rack_rtm_prr_newdata);
2919 	counter_u64_free(rack_timestamp_mismatch);
2920 	counter_u64_free(rack_find_high);
2921 	counter_u64_free(rack_reorder_seen);
2922 	counter_u64_free(rack_tlp_tot);
2923 	counter_u64_free(rack_tlp_newdata);
2924 	counter_u64_free(rack_tlp_retran);
2925 	counter_u64_free(rack_tlp_retran_bytes);
2926 	counter_u64_free(rack_tlp_retran_fail);
2927 	counter_u64_free(rack_to_tot);
2928 	counter_u64_free(rack_to_arm_rack);
2929 	counter_u64_free(rack_to_arm_tlp);
2930 	counter_u64_free(rack_calc_zero);
2931 	counter_u64_free(rack_calc_nonzero);
2932 	counter_u64_free(rack_paced_segments);
2933 	counter_u64_free(rack_unpaced_segments);
2934 	counter_u64_free(rack_saw_enobuf);
2935 	counter_u64_free(rack_saw_enobuf_hw);
2936 	counter_u64_free(rack_saw_enetunreach);
2937 	counter_u64_free(rack_hot_alloc);
2938 	counter_u64_free(rack_to_alloc);
2939 	counter_u64_free(rack_to_alloc_hard);
2940 	counter_u64_free(rack_to_alloc_emerg);
2941 	counter_u64_free(rack_to_alloc_limited);
2942 	counter_u64_free(rack_alloc_limited_conns);
2943 	counter_u64_free(rack_split_limited);
2944 	for (i = 0; i < MAX_NUM_OF_CNTS; i++) {
2945 		counter_u64_free(rack_proc_comp_ack[i]);
2946 	}
2947 	counter_u64_free(rack_multi_single_eq);
2948 	counter_u64_free(rack_proc_non_comp_ack);
2949 	counter_u64_free(rack_sack_proc_all);
2950 	counter_u64_free(rack_sack_proc_restart);
2951 	counter_u64_free(rack_sack_proc_short);
2952 	counter_u64_free(rack_enter_tlp_calc);
2953 	counter_u64_free(rack_used_tlpmethod);
2954 	counter_u64_free(rack_used_tlpmethod2);
2955 	counter_u64_free(rack_sack_skipped_acked);
2956 	counter_u64_free(rack_sack_splits);
2957 	counter_u64_free(rack_progress_drops);
2958 	counter_u64_free(rack_input_idle_reduces);
2959 	counter_u64_free(rack_collapsed_win);
2960 	counter_u64_free(rack_tlp_does_nada);
2961 	counter_u64_free(rack_try_scwnd);
2962 	counter_u64_free(rack_per_timer_hole);
2963 	counter_u64_free(rack_large_ackcmp);
2964 	counter_u64_free(rack_small_ackcmp);
2965 	counter_u64_free(rack_persists_sends);
2966 	counter_u64_free(rack_persists_acks);
2967 	counter_u64_free(rack_persists_loss);
2968 	counter_u64_free(rack_persists_lost_ends);
2969 #ifdef INVARIANTS
2970 	counter_u64_free(rack_adjust_map_bw);
2971 #endif
2972 	COUNTER_ARRAY_FREE(rack_out_size, TCP_MSS_ACCT_SIZE);
2973 	COUNTER_ARRAY_FREE(rack_opts_arry, RACK_OPTS_SIZE);
2974 }
2975 
2976 static struct rack_sendmap *
2977 rack_alloc(struct tcp_rack *rack)
2978 {
2979 	struct rack_sendmap *rsm;
2980 
2981 	/*
2982 	 * First get the top of the list it in
2983 	 * theory is the "hottest" rsm we have,
2984 	 * possibly just freed by ack processing.
2985 	 */
2986 	if (rack->rc_free_cnt > rack_free_cache) {
2987 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
2988 		TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
2989 		counter_u64_add(rack_hot_alloc, 1);
2990 		rack->rc_free_cnt--;
2991 		return (rsm);
2992 	}
2993 	/*
2994 	 * Once we get under our free cache we probably
2995 	 * no longer have a "hot" one available. Lets
2996 	 * get one from UMA.
2997 	 */
2998 	rsm = uma_zalloc(rack_zone, M_NOWAIT);
2999 	if (rsm) {
3000 		rack->r_ctl.rc_num_maps_alloced++;
3001 		counter_u64_add(rack_to_alloc, 1);
3002 		return (rsm);
3003 	}
3004 	/*
3005 	 * Dig in to our aux rsm's (the last two) since
3006 	 * UMA failed to get us one.
3007 	 */
3008 	if (rack->rc_free_cnt) {
3009 		counter_u64_add(rack_to_alloc_emerg, 1);
3010 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
3011 		TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
3012 		rack->rc_free_cnt--;
3013 		return (rsm);
3014 	}
3015 	return (NULL);
3016 }
3017 
3018 static struct rack_sendmap *
3019 rack_alloc_full_limit(struct tcp_rack *rack)
3020 {
3021 	if ((V_tcp_map_entries_limit > 0) &&
3022 	    (rack->do_detection == 0) &&
3023 	    (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
3024 		counter_u64_add(rack_to_alloc_limited, 1);
3025 		if (!rack->alloc_limit_reported) {
3026 			rack->alloc_limit_reported = 1;
3027 			counter_u64_add(rack_alloc_limited_conns, 1);
3028 		}
3029 		return (NULL);
3030 	}
3031 	return (rack_alloc(rack));
3032 }
3033 
3034 /* wrapper to allocate a sendmap entry, subject to a specific limit */
3035 static struct rack_sendmap *
3036 rack_alloc_limit(struct tcp_rack *rack, uint8_t limit_type)
3037 {
3038 	struct rack_sendmap *rsm;
3039 
3040 	if (limit_type) {
3041 		/* currently there is only one limit type */
3042 		if (V_tcp_map_split_limit > 0 &&
3043 		    (rack->do_detection == 0) &&
3044 		    rack->r_ctl.rc_num_split_allocs >= V_tcp_map_split_limit) {
3045 			counter_u64_add(rack_split_limited, 1);
3046 			if (!rack->alloc_limit_reported) {
3047 				rack->alloc_limit_reported = 1;
3048 				counter_u64_add(rack_alloc_limited_conns, 1);
3049 			}
3050 			return (NULL);
3051 		}
3052 	}
3053 
3054 	/* allocate and mark in the limit type, if set */
3055 	rsm = rack_alloc(rack);
3056 	if (rsm != NULL && limit_type) {
3057 		rsm->r_limit_type = limit_type;
3058 		rack->r_ctl.rc_num_split_allocs++;
3059 	}
3060 	return (rsm);
3061 }
3062 
3063 static void
3064 rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm)
3065 {
3066 	if (rsm->r_flags & RACK_APP_LIMITED) {
3067 		if (rack->r_ctl.rc_app_limited_cnt > 0) {
3068 			rack->r_ctl.rc_app_limited_cnt--;
3069 		}
3070 	}
3071 	if (rsm->r_limit_type) {
3072 		/* currently there is only one limit type */
3073 		rack->r_ctl.rc_num_split_allocs--;
3074 	}
3075 	if (rsm == rack->r_ctl.rc_first_appl) {
3076 		if (rack->r_ctl.rc_app_limited_cnt == 0)
3077 			rack->r_ctl.rc_first_appl = NULL;
3078 		else {
3079 			/* Follow the next one out */
3080 			struct rack_sendmap fe;
3081 
3082 			fe.r_start = rsm->r_nseq_appl;
3083 			rack->r_ctl.rc_first_appl = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
3084 		}
3085 	}
3086 	if (rsm == rack->r_ctl.rc_resend)
3087 		rack->r_ctl.rc_resend = NULL;
3088 	if (rsm == rack->r_ctl.rc_rsm_at_retran)
3089 		rack->r_ctl.rc_rsm_at_retran = NULL;
3090 	if (rsm == rack->r_ctl.rc_end_appl)
3091 		rack->r_ctl.rc_end_appl = NULL;
3092 	if (rack->r_ctl.rc_tlpsend == rsm)
3093 		rack->r_ctl.rc_tlpsend = NULL;
3094 	if (rack->r_ctl.rc_sacklast == rsm)
3095 		rack->r_ctl.rc_sacklast = NULL;
3096 	memset(rsm, 0, sizeof(struct rack_sendmap));
3097 	TAILQ_INSERT_HEAD(&rack->r_ctl.rc_free, rsm, r_tnext);
3098 	rack->rc_free_cnt++;
3099 }
3100 
3101 static void
3102 rack_free_trim(struct tcp_rack *rack)
3103 {
3104 	struct rack_sendmap *rsm;
3105 
3106 	/*
3107 	 * Free up all the tail entries until
3108 	 * we get our list down to the limit.
3109 	 */
3110 	while (rack->rc_free_cnt > rack_free_cache) {
3111 		rsm = TAILQ_LAST(&rack->r_ctl.rc_free, rack_head);
3112 		TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
3113 		rack->rc_free_cnt--;
3114 		uma_zfree(rack_zone, rsm);
3115 	}
3116 }
3117 
3118 
3119 static uint32_t
3120 rack_get_measure_window(struct tcpcb *tp, struct tcp_rack *rack)
3121 {
3122 	uint64_t srtt, bw, len, tim;
3123 	uint32_t segsiz, def_len, minl;
3124 
3125 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
3126 	def_len = rack_def_data_window * segsiz;
3127 	if (rack->rc_gp_filled == 0) {
3128 		/*
3129 		 * We have no measurement (IW is in flight?) so
3130 		 * we can only guess using our data_window sysctl
3131 		 * value (usually 20MSS).
3132 		 */
3133 		return (def_len);
3134 	}
3135 	/*
3136 	 * Now we have a number of factors to consider.
3137 	 *
3138 	 * 1) We have a desired BDP which is usually
3139 	 *    at least 2.
3140 	 * 2) We have a minimum number of rtt's usually 1 SRTT
3141 	 *    but we allow it too to be more.
3142 	 * 3) We want to make sure a measurement last N useconds (if
3143 	 *    we have set rack_min_measure_usec.
3144 	 *
3145 	 * We handle the first concern here by trying to create a data
3146 	 * window of max(rack_def_data_window, DesiredBDP). The
3147 	 * second concern we handle in not letting the measurement
3148 	 * window end normally until at least the required SRTT's
3149 	 * have gone by which is done further below in
3150 	 * rack_enough_for_measurement(). Finally the third concern
3151 	 * we also handle here by calculating how long that time
3152 	 * would take at the current BW and then return the
3153 	 * max of our first calculation and that length. Note
3154 	 * that if rack_min_measure_usec is 0, we don't deal
3155 	 * with concern 3. Also for both Concern 1 and 3 an
3156 	 * application limited period could end the measurement
3157 	 * earlier.
3158 	 *
3159 	 * So lets calculate the BDP with the "known" b/w using
3160 	 * the SRTT has our rtt and then multiply it by the
3161 	 * goal.
3162 	 */
3163 	bw = rack_get_bw(rack);
3164 	srtt = (uint64_t)tp->t_srtt;
3165 	len = bw * srtt;
3166 	len /= (uint64_t)HPTS_USEC_IN_SEC;
3167 	len *= max(1, rack_goal_bdp);
3168 	/* Now we need to round up to the nearest MSS */
3169 	len = roundup(len, segsiz);
3170 	if (rack_min_measure_usec) {
3171 		/* Now calculate our min length for this b/w */
3172 		tim = rack_min_measure_usec;
3173 		minl = (tim * bw) / (uint64_t)HPTS_USEC_IN_SEC;
3174 		if (minl == 0)
3175 			minl = 1;
3176 		minl = roundup(minl, segsiz);
3177 		if (len < minl)
3178 			len = minl;
3179 	}
3180 	/*
3181 	 * Now if we have a very small window we want
3182 	 * to attempt to get the window that is
3183 	 * as small as possible. This happens on
3184 	 * low b/w connections and we don't want to
3185 	 * span huge numbers of rtt's between measurements.
3186 	 *
3187 	 * We basically include 2 over our "MIN window" so
3188 	 * that the measurement can be shortened (possibly) by
3189 	 * an ack'ed packet.
3190 	 */
3191 	if (len < def_len)
3192 		return (max((uint32_t)len, ((MIN_GP_WIN+2) * segsiz)));
3193 	else
3194 		return (max((uint32_t)len, def_len));
3195 
3196 }
3197 
3198 static int
3199 rack_enough_for_measurement(struct tcpcb *tp, struct tcp_rack *rack, tcp_seq th_ack, uint8_t *quality)
3200 {
3201 	uint32_t tim, srtts, segsiz;
3202 
3203 	/*
3204 	 * Has enough time passed for the GP measurement to be valid?
3205 	 */
3206 	if ((tp->snd_max == tp->snd_una) ||
3207 	    (th_ack == tp->snd_max)){
3208 		/* All is acked */
3209 		*quality = RACK_QUALITY_ALLACKED;
3210 		return (1);
3211 	}
3212 	if (SEQ_LT(th_ack, tp->gput_seq)) {
3213 		/* Not enough bytes yet */
3214 		return (0);
3215 	}
3216 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
3217 	if (SEQ_LT(th_ack, tp->gput_ack) &&
3218 	    ((th_ack - tp->gput_seq) < max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) {
3219 		/* Not enough bytes yet */
3220 		return (0);
3221 	}
3222 	if (rack->r_ctl.rc_first_appl &&
3223 	    (SEQ_GEQ(th_ack, rack->r_ctl.rc_first_appl->r_end))) {
3224 		/*
3225 		 * We are up to the app limited send point
3226 		 * we have to measure irrespective of the time..
3227 		 */
3228 		*quality = RACK_QUALITY_APPLIMITED;
3229 		return (1);
3230 	}
3231 	/* Now what about time? */
3232 	srtts = (rack->r_ctl.rc_gp_srtt * rack_min_srtts);
3233 	tim = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - tp->gput_ts;
3234 	if (tim >= srtts) {
3235 		*quality = RACK_QUALITY_HIGH;
3236 		return (1);
3237 	}
3238 	/* Nope not even a full SRTT has passed */
3239 	return (0);
3240 }
3241 
3242 static void
3243 rack_log_timely(struct tcp_rack *rack,
3244 		uint32_t logged, uint64_t cur_bw, uint64_t low_bnd,
3245 		uint64_t up_bnd, int line, uint8_t method)
3246 {
3247 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
3248 		union tcp_log_stackspecific log;
3249 		struct timeval tv;
3250 
3251 		memset(&log, 0, sizeof(log));
3252 		log.u_bbr.flex1 = logged;
3253 		log.u_bbr.flex2 = rack->rc_gp_timely_inc_cnt;
3254 		log.u_bbr.flex2 <<= 4;
3255 		log.u_bbr.flex2 |= rack->rc_gp_timely_dec_cnt;
3256 		log.u_bbr.flex2 <<= 4;
3257 		log.u_bbr.flex2 |= rack->rc_gp_incr;
3258 		log.u_bbr.flex2 <<= 4;
3259 		log.u_bbr.flex2 |= rack->rc_gp_bwred;
3260 		log.u_bbr.flex3 = rack->rc_gp_incr;
3261 		log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss;
3262 		log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ca;
3263 		log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_rec;
3264 		log.u_bbr.flex7 = rack->rc_gp_bwred;
3265 		log.u_bbr.flex8 = method;
3266 		log.u_bbr.cur_del_rate = cur_bw;
3267 		log.u_bbr.delRate = low_bnd;
3268 		log.u_bbr.bw_inuse = up_bnd;
3269 		log.u_bbr.rttProp = rack_get_bw(rack);
3270 		log.u_bbr.pkt_epoch = line;
3271 		log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff;
3272 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3273 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3274 		log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt;
3275 		log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt;
3276 		log.u_bbr.cwnd_gain = rack->rc_dragged_bottom;
3277 		log.u_bbr.cwnd_gain <<= 1;
3278 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_rec;
3279 		log.u_bbr.cwnd_gain <<= 1;
3280 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss;
3281 		log.u_bbr.cwnd_gain <<= 1;
3282 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca;
3283 		log.u_bbr.lost = rack->r_ctl.rc_loss_count;
3284 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3285 		    &rack->rc_inp->inp_socket->so_rcv,
3286 		    &rack->rc_inp->inp_socket->so_snd,
3287 		    TCP_TIMELY_WORK, 0,
3288 		    0, &log, false, &tv);
3289 	}
3290 }
3291 
3292 static int
3293 rack_bw_can_be_raised(struct tcp_rack *rack, uint64_t cur_bw, uint64_t last_bw_est, uint16_t mult)
3294 {
3295 	/*
3296 	 * Before we increase we need to know if
3297 	 * the estimate just made was less than
3298 	 * our pacing goal (i.e. (cur_bw * mult) > last_bw_est)
3299 	 *
3300 	 * If we already are pacing at a fast enough
3301 	 * rate to push us faster there is no sense of
3302 	 * increasing.
3303 	 *
3304 	 * We first caculate our actual pacing rate (ss or ca multipler
3305 	 * times our cur_bw).
3306 	 *
3307 	 * Then we take the last measured rate and multipy by our
3308 	 * maximum pacing overage to give us a max allowable rate.
3309 	 *
3310 	 * If our act_rate is smaller than our max_allowable rate
3311 	 * then we should increase. Else we should hold steady.
3312 	 *
3313 	 */
3314 	uint64_t act_rate, max_allow_rate;
3315 
3316 	if (rack_timely_no_stopping)
3317 		return (1);
3318 
3319 	if ((cur_bw == 0) || (last_bw_est == 0)) {
3320 		/*
3321 		 * Initial startup case or
3322 		 * everything is acked case.
3323 		 */
3324 		rack_log_timely(rack,  mult, cur_bw, 0, 0,
3325 				__LINE__, 9);
3326 		return (1);
3327 	}
3328 	if (mult <= 100) {
3329 		/*
3330 		 * We can always pace at or slightly above our rate.
3331 		 */
3332 		rack_log_timely(rack,  mult, cur_bw, 0, 0,
3333 				__LINE__, 9);
3334 		return (1);
3335 	}
3336 	act_rate = cur_bw * (uint64_t)mult;
3337 	act_rate /= 100;
3338 	max_allow_rate = last_bw_est * ((uint64_t)rack_max_per_above + (uint64_t)100);
3339 	max_allow_rate /= 100;
3340 	if (act_rate < max_allow_rate) {
3341 		/*
3342 		 * Here the rate we are actually pacing at
3343 		 * is smaller than 10% above our last measurement.
3344 		 * This means we are pacing below what we would
3345 		 * like to try to achieve (plus some wiggle room).
3346 		 */
3347 		rack_log_timely(rack,  mult, cur_bw, act_rate, max_allow_rate,
3348 				__LINE__, 9);
3349 		return (1);
3350 	} else {
3351 		/*
3352 		 * Here we are already pacing at least rack_max_per_above(10%)
3353 		 * what we are getting back. This indicates most likely
3354 		 * that we are being limited (cwnd/rwnd/app) and can't
3355 		 * get any more b/w. There is no sense of trying to
3356 		 * raise up the pacing rate its not speeding us up
3357 		 * and we already are pacing faster than we are getting.
3358 		 */
3359 		rack_log_timely(rack,  mult, cur_bw, act_rate, max_allow_rate,
3360 				__LINE__, 8);
3361 		return (0);
3362 	}
3363 }
3364 
3365 static void
3366 rack_validate_multipliers_at_or_above100(struct tcp_rack *rack)
3367 {
3368 	/*
3369 	 * When we drag bottom, we want to assure
3370 	 * that no multiplier is below 1.0, if so
3371 	 * we want to restore it to at least that.
3372 	 */
3373 	if (rack->r_ctl.rack_per_of_gp_rec  < 100) {
3374 		/* This is unlikely we usually do not touch recovery */
3375 		rack->r_ctl.rack_per_of_gp_rec = 100;
3376 	}
3377 	if (rack->r_ctl.rack_per_of_gp_ca < 100) {
3378 		rack->r_ctl.rack_per_of_gp_ca = 100;
3379 	}
3380 	if (rack->r_ctl.rack_per_of_gp_ss < 100) {
3381 		rack->r_ctl.rack_per_of_gp_ss = 100;
3382 	}
3383 }
3384 
3385 static void
3386 rack_validate_multipliers_at_or_below_100(struct tcp_rack *rack)
3387 {
3388 	if (rack->r_ctl.rack_per_of_gp_ca > 100) {
3389 		rack->r_ctl.rack_per_of_gp_ca = 100;
3390 	}
3391 	if (rack->r_ctl.rack_per_of_gp_ss > 100) {
3392 		rack->r_ctl.rack_per_of_gp_ss = 100;
3393 	}
3394 }
3395 
3396 static void
3397 rack_increase_bw_mul(struct tcp_rack *rack, int timely_says, uint64_t cur_bw, uint64_t last_bw_est, int override)
3398 {
3399 	int32_t  calc, logged, plus;
3400 
3401 	logged = 0;
3402 
3403 	if (override) {
3404 		/*
3405 		 * override is passed when we are
3406 		 * loosing b/w and making one last
3407 		 * gasp at trying to not loose out
3408 		 * to a new-reno flow.
3409 		 */
3410 		goto extra_boost;
3411 	}
3412 	/* In classic timely we boost by 5x if we have 5 increases in a row, lets not */
3413 	if (rack->rc_gp_incr &&
3414 	    ((rack->rc_gp_timely_inc_cnt + 1) >= RACK_TIMELY_CNT_BOOST)) {
3415 		/*
3416 		 * Reset and get 5 strokes more before the boost. Note
3417 		 * that the count is 0 based so we have to add one.
3418 		 */
3419 extra_boost:
3420 		plus = (uint32_t)rack_gp_increase_per * RACK_TIMELY_CNT_BOOST;
3421 		rack->rc_gp_timely_inc_cnt = 0;
3422 	} else
3423 		plus = (uint32_t)rack_gp_increase_per;
3424 	/* Must be at least 1% increase for true timely increases */
3425 	if ((plus < 1) &&
3426 	    ((rack->r_ctl.rc_rtt_diff <= 0) || (timely_says <= 0)))
3427 		plus = 1;
3428 	if (rack->rc_gp_saw_rec &&
3429 	    (rack->rc_gp_no_rec_chg == 0) &&
3430 	    rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3431 				  rack->r_ctl.rack_per_of_gp_rec)) {
3432 		/* We have been in recovery ding it too */
3433 		calc = rack->r_ctl.rack_per_of_gp_rec + plus;
3434 		if (calc > 0xffff)
3435 			calc = 0xffff;
3436 		logged |= 1;
3437 		rack->r_ctl.rack_per_of_gp_rec = (uint16_t)calc;
3438 		if (rack_per_upper_bound_ss &&
3439 		    (rack->rc_dragged_bottom == 0) &&
3440 		    (rack->r_ctl.rack_per_of_gp_rec > rack_per_upper_bound_ss))
3441 			rack->r_ctl.rack_per_of_gp_rec = rack_per_upper_bound_ss;
3442 	}
3443 	if (rack->rc_gp_saw_ca &&
3444 	    (rack->rc_gp_saw_ss == 0) &&
3445 	    rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3446 				  rack->r_ctl.rack_per_of_gp_ca)) {
3447 		/* In CA */
3448 		calc = rack->r_ctl.rack_per_of_gp_ca + plus;
3449 		if (calc > 0xffff)
3450 			calc = 0xffff;
3451 		logged |= 2;
3452 		rack->r_ctl.rack_per_of_gp_ca = (uint16_t)calc;
3453 		if (rack_per_upper_bound_ca &&
3454 		    (rack->rc_dragged_bottom == 0) &&
3455 		    (rack->r_ctl.rack_per_of_gp_ca > rack_per_upper_bound_ca))
3456 			rack->r_ctl.rack_per_of_gp_ca = rack_per_upper_bound_ca;
3457 	}
3458 	if (rack->rc_gp_saw_ss &&
3459 	    rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3460 				  rack->r_ctl.rack_per_of_gp_ss)) {
3461 		/* In SS */
3462 		calc = rack->r_ctl.rack_per_of_gp_ss + plus;
3463 		if (calc > 0xffff)
3464 			calc = 0xffff;
3465 		rack->r_ctl.rack_per_of_gp_ss = (uint16_t)calc;
3466 		if (rack_per_upper_bound_ss &&
3467 		    (rack->rc_dragged_bottom == 0) &&
3468 		    (rack->r_ctl.rack_per_of_gp_ss > rack_per_upper_bound_ss))
3469 			rack->r_ctl.rack_per_of_gp_ss = rack_per_upper_bound_ss;
3470 		logged |= 4;
3471 	}
3472 	if (logged &&
3473 	    (rack->rc_gp_incr == 0)){
3474 		/* Go into increment mode */
3475 		rack->rc_gp_incr = 1;
3476 		rack->rc_gp_timely_inc_cnt = 0;
3477 	}
3478 	if (rack->rc_gp_incr &&
3479 	    logged &&
3480 	    (rack->rc_gp_timely_inc_cnt < RACK_TIMELY_CNT_BOOST)) {
3481 		rack->rc_gp_timely_inc_cnt++;
3482 	}
3483 	rack_log_timely(rack,  logged, plus, 0, 0,
3484 			__LINE__, 1);
3485 }
3486 
3487 static uint32_t
3488 rack_get_decrease(struct tcp_rack *rack, uint32_t curper, int32_t rtt_diff)
3489 {
3490 	/*
3491 	 * norm_grad = rtt_diff / minrtt;
3492 	 * new_per = curper * (1 - B * norm_grad)
3493 	 *
3494 	 * B = rack_gp_decrease_per (default 10%)
3495 	 * rtt_dif = input var current rtt-diff
3496 	 * curper = input var current percentage
3497 	 * minrtt = from rack filter
3498 	 *
3499 	 */
3500 	uint64_t perf;
3501 
3502 	perf = (((uint64_t)curper * ((uint64_t)1000000 -
3503 		    ((uint64_t)rack_gp_decrease_per * (uint64_t)10000 *
3504 		     (((uint64_t)rtt_diff * (uint64_t)1000000)/
3505 		      (uint64_t)get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt)))/
3506 		     (uint64_t)1000000)) /
3507 		(uint64_t)1000000);
3508 	if (perf > curper) {
3509 		/* TSNH */
3510 		perf = curper - 1;
3511 	}
3512 	return ((uint32_t)perf);
3513 }
3514 
3515 static uint32_t
3516 rack_decrease_highrtt(struct tcp_rack *rack, uint32_t curper, uint32_t rtt)
3517 {
3518 	/*
3519 	 *                                   highrttthresh
3520 	 * result = curper * (1 - (B * ( 1 -  ------          ))
3521 	 *                                     gp_srtt
3522 	 *
3523 	 * B = rack_gp_decrease_per (default 10%)
3524 	 * highrttthresh = filter_min * rack_gp_rtt_maxmul
3525 	 */
3526 	uint64_t perf;
3527 	uint32_t highrttthresh;
3528 
3529 	highrttthresh = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
3530 
3531 	perf = (((uint64_t)curper * ((uint64_t)1000000 -
3532 				     ((uint64_t)rack_gp_decrease_per * ((uint64_t)1000000 -
3533 					((uint64_t)highrttthresh * (uint64_t)1000000) /
3534 						    (uint64_t)rtt)) / 100)) /(uint64_t)1000000);
3535 	return (perf);
3536 }
3537 
3538 static void
3539 rack_decrease_bw_mul(struct tcp_rack *rack, int timely_says, uint32_t rtt, int32_t rtt_diff)
3540 {
3541 	uint64_t logvar, logvar2, logvar3;
3542 	uint32_t logged, new_per, ss_red, ca_red, rec_red, alt, val;
3543 
3544 	if (rack->rc_gp_incr) {
3545 		/* Turn off increment counting */
3546 		rack->rc_gp_incr = 0;
3547 		rack->rc_gp_timely_inc_cnt = 0;
3548 	}
3549 	ss_red = ca_red = rec_red = 0;
3550 	logged = 0;
3551 	/* Calculate the reduction value */
3552 	if (rtt_diff < 0) {
3553 		rtt_diff *= -1;
3554 	}
3555 	/* Must be at least 1% reduction */
3556 	if (rack->rc_gp_saw_rec && (rack->rc_gp_no_rec_chg == 0)) {
3557 		/* We have been in recovery ding it too */
3558 		if (timely_says == 2) {
3559 			new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_rec, rtt);
3560 			alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
3561 			if (alt < new_per)
3562 				val = alt;
3563 			else
3564 				val = new_per;
3565 		} else
3566 			 val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
3567 		if (rack->r_ctl.rack_per_of_gp_rec > val) {
3568 			rec_red = (rack->r_ctl.rack_per_of_gp_rec - val);
3569 			rack->r_ctl.rack_per_of_gp_rec = (uint16_t)val;
3570 		} else {
3571 			rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound;
3572 			rec_red = 0;
3573 		}
3574 		if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_rec)
3575 			rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound;
3576 		logged |= 1;
3577 	}
3578 	if (rack->rc_gp_saw_ss) {
3579 		/* Sent in SS */
3580 		if (timely_says == 2) {
3581 			new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ss, rtt);
3582 			alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
3583 			if (alt < new_per)
3584 				val = alt;
3585 			else
3586 				val = new_per;
3587 		} else
3588 			val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ss, rtt_diff);
3589 		if (rack->r_ctl.rack_per_of_gp_ss > new_per) {
3590 			ss_red = rack->r_ctl.rack_per_of_gp_ss - val;
3591 			rack->r_ctl.rack_per_of_gp_ss = (uint16_t)val;
3592 		} else {
3593 			ss_red = new_per;
3594 			rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound;
3595 			logvar = new_per;
3596 			logvar <<= 32;
3597 			logvar |= alt;
3598 			logvar2 = (uint32_t)rtt;
3599 			logvar2 <<= 32;
3600 			logvar2 |= (uint32_t)rtt_diff;
3601 			logvar3 = rack_gp_rtt_maxmul;
3602 			logvar3 <<= 32;
3603 			logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
3604 			rack_log_timely(rack, timely_says,
3605 					logvar2, logvar3,
3606 					logvar, __LINE__, 10);
3607 		}
3608 		if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ss)
3609 			rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound;
3610 		logged |= 4;
3611 	} else if (rack->rc_gp_saw_ca) {
3612 		/* Sent in CA */
3613 		if (timely_says == 2) {
3614 			new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ca, rtt);
3615 			alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
3616 			if (alt < new_per)
3617 				val = alt;
3618 			else
3619 				val = new_per;
3620 		} else
3621 			val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ca, rtt_diff);
3622 		if (rack->r_ctl.rack_per_of_gp_ca > val) {
3623 			ca_red = rack->r_ctl.rack_per_of_gp_ca - val;
3624 			rack->r_ctl.rack_per_of_gp_ca = (uint16_t)val;
3625 		} else {
3626 			rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound;
3627 			ca_red = 0;
3628 			logvar = new_per;
3629 			logvar <<= 32;
3630 			logvar |= alt;
3631 			logvar2 = (uint32_t)rtt;
3632 			logvar2 <<= 32;
3633 			logvar2 |= (uint32_t)rtt_diff;
3634 			logvar3 = rack_gp_rtt_maxmul;
3635 			logvar3 <<= 32;
3636 			logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
3637 			rack_log_timely(rack, timely_says,
3638 					logvar2, logvar3,
3639 					logvar, __LINE__, 10);
3640 		}
3641 		if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ca)
3642 			rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound;
3643 		logged |= 2;
3644 	}
3645 	if (rack->rc_gp_timely_dec_cnt < 0x7) {
3646 		rack->rc_gp_timely_dec_cnt++;
3647 		if (rack_timely_dec_clear &&
3648 		    (rack->rc_gp_timely_dec_cnt == rack_timely_dec_clear))
3649 			rack->rc_gp_timely_dec_cnt = 0;
3650 	}
3651 	logvar = ss_red;
3652 	logvar <<= 32;
3653 	logvar |= ca_red;
3654 	rack_log_timely(rack,  logged, rec_red, rack_per_lower_bound, logvar,
3655 			__LINE__, 2);
3656 }
3657 
3658 static void
3659 rack_log_rtt_shrinks(struct tcp_rack *rack, uint32_t us_cts,
3660 		     uint32_t rtt, uint32_t line, uint8_t reas)
3661 {
3662 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
3663 		union tcp_log_stackspecific log;
3664 		struct timeval tv;
3665 
3666 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3667 		log.u_bbr.flex1 = line;
3668 		log.u_bbr.flex2 = rack->r_ctl.rc_time_probertt_starts;
3669 		log.u_bbr.flex3 = rack->r_ctl.rc_lower_rtt_us_cts;
3670 		log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss;
3671 		log.u_bbr.flex5 = rtt;
3672 		log.u_bbr.flex6 = rack->rc_highly_buffered;
3673 		log.u_bbr.flex6 <<= 1;
3674 		log.u_bbr.flex6 |= rack->forced_ack;
3675 		log.u_bbr.flex6 <<= 1;
3676 		log.u_bbr.flex6 |= rack->rc_gp_dyn_mul;
3677 		log.u_bbr.flex6 <<= 1;
3678 		log.u_bbr.flex6 |= rack->in_probe_rtt;
3679 		log.u_bbr.flex6 <<= 1;
3680 		log.u_bbr.flex6 |= rack->measure_saw_probe_rtt;
3681 		log.u_bbr.flex7 = rack->r_ctl.rack_per_of_gp_probertt;
3682 		log.u_bbr.pacing_gain = rack->r_ctl.rack_per_of_gp_ca;
3683 		log.u_bbr.cwnd_gain = rack->r_ctl.rack_per_of_gp_rec;
3684 		log.u_bbr.flex8 = reas;
3685 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3686 		log.u_bbr.delRate = rack_get_bw(rack);
3687 		log.u_bbr.cur_del_rate = rack->r_ctl.rc_highest_us_rtt;
3688 		log.u_bbr.cur_del_rate <<= 32;
3689 		log.u_bbr.cur_del_rate |= rack->r_ctl.rc_lowest_us_rtt;
3690 		log.u_bbr.applimited = rack->r_ctl.rc_time_probertt_entered;
3691 		log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff;
3692 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3693 		log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt;
3694 		log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt;
3695 		log.u_bbr.pkt_epoch = rack->r_ctl.rc_lower_rtt_us_cts;
3696 		log.u_bbr.delivered = rack->r_ctl.rc_target_probertt_flight;
3697 		log.u_bbr.lost = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
3698 		log.u_bbr.rttProp = us_cts;
3699 		log.u_bbr.rttProp <<= 32;
3700 		log.u_bbr.rttProp |= rack->r_ctl.rc_entry_gp_rtt;
3701 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3702 		    &rack->rc_inp->inp_socket->so_rcv,
3703 		    &rack->rc_inp->inp_socket->so_snd,
3704 		    BBR_LOG_RTT_SHRINKS, 0,
3705 		    0, &log, false, &rack->r_ctl.act_rcv_time);
3706 	}
3707 }
3708 
3709 static void
3710 rack_set_prtt_target(struct tcp_rack *rack, uint32_t segsiz, uint32_t rtt)
3711 {
3712 	uint64_t bwdp;
3713 
3714 	bwdp = rack_get_bw(rack);
3715 	bwdp *= (uint64_t)rtt;
3716 	bwdp /= (uint64_t)HPTS_USEC_IN_SEC;
3717 	rack->r_ctl.rc_target_probertt_flight = roundup((uint32_t)bwdp, segsiz);
3718 	if (rack->r_ctl.rc_target_probertt_flight < (segsiz * rack_timely_min_segs)) {
3719 		/*
3720 		 * A window protocol must be able to have 4 packets
3721 		 * outstanding as the floor in order to function
3722 		 * (especially considering delayed ack :D).
3723 		 */
3724 		rack->r_ctl.rc_target_probertt_flight = (segsiz * rack_timely_min_segs);
3725 	}
3726 }
3727 
3728 static void
3729 rack_enter_probertt(struct tcp_rack *rack, uint32_t us_cts)
3730 {
3731 	/**
3732 	 * ProbeRTT is a bit different in rack_pacing than in
3733 	 * BBR. It is like BBR in that it uses the lowering of
3734 	 * the RTT as a signal that we saw something new and
3735 	 * counts from there for how long between. But it is
3736 	 * different in that its quite simple. It does not
3737 	 * play with the cwnd and wait until we get down
3738 	 * to N segments outstanding and hold that for
3739 	 * 200ms. Instead it just sets the pacing reduction
3740 	 * rate to a set percentage (70 by default) and hold
3741 	 * that for a number of recent GP Srtt's.
3742 	 */
3743 	uint32_t segsiz;
3744 
3745 	if (rack->rc_gp_dyn_mul == 0)
3746 		return;
3747 
3748 	if (rack->rc_tp->snd_max == rack->rc_tp->snd_una) {
3749 		/* We are idle */
3750 		return;
3751 	}
3752 	if ((rack->rc_tp->t_flags & TF_GPUTINPROG) &&
3753 	    SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) {
3754 		/*
3755 		 * Stop the goodput now, the idea here is
3756 		 * that future measurements with in_probe_rtt
3757 		 * won't register if they are not greater so
3758 		 * we want to get what info (if any) is available
3759 		 * now.
3760 		 */
3761 		rack_do_goodput_measurement(rack->rc_tp, rack,
3762 					    rack->rc_tp->snd_una, __LINE__,
3763 					    RACK_QUALITY_PROBERTT);
3764 	}
3765 	rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
3766 	rack->r_ctl.rc_time_probertt_entered = us_cts;
3767 	segsiz = min(ctf_fixed_maxseg(rack->rc_tp),
3768 		     rack->r_ctl.rc_pace_min_segs);
3769 	rack->in_probe_rtt = 1;
3770 	rack->measure_saw_probe_rtt = 1;
3771 	rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
3772 	rack->r_ctl.rc_time_probertt_starts = 0;
3773 	rack->r_ctl.rc_entry_gp_rtt = rack->r_ctl.rc_gp_srtt;
3774 	if (rack_probertt_use_min_rtt_entry)
3775 		rack_set_prtt_target(rack, segsiz, get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt));
3776 	else
3777 		rack_set_prtt_target(rack, segsiz, rack->r_ctl.rc_gp_srtt);
3778 	rack_log_rtt_shrinks(rack,  us_cts,  get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
3779 			     __LINE__, RACK_RTTS_ENTERPROBE);
3780 }
3781 
3782 static void
3783 rack_exit_probertt(struct tcp_rack *rack, uint32_t us_cts)
3784 {
3785 	struct rack_sendmap *rsm;
3786 	uint32_t segsiz;
3787 
3788 	segsiz = min(ctf_fixed_maxseg(rack->rc_tp),
3789 		     rack->r_ctl.rc_pace_min_segs);
3790 	rack->in_probe_rtt = 0;
3791 	if ((rack->rc_tp->t_flags & TF_GPUTINPROG) &&
3792 	    SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) {
3793 		/*
3794 		 * Stop the goodput now, the idea here is
3795 		 * that future measurements with in_probe_rtt
3796 		 * won't register if they are not greater so
3797 		 * we want to get what info (if any) is available
3798 		 * now.
3799 		 */
3800 		rack_do_goodput_measurement(rack->rc_tp, rack,
3801 					    rack->rc_tp->snd_una, __LINE__,
3802 					    RACK_QUALITY_PROBERTT);
3803 	} else if (rack->rc_tp->t_flags & TF_GPUTINPROG) {
3804 		/*
3805 		 * We don't have enough data to make a measurement.
3806 		 * So lets just stop and start here after exiting
3807 		 * probe-rtt. We probably are not interested in
3808 		 * the results anyway.
3809 		 */
3810 		rack->rc_tp->t_flags &= ~TF_GPUTINPROG;
3811 	}
3812 	/*
3813 	 * Measurements through the current snd_max are going
3814 	 * to be limited by the slower pacing rate.
3815 	 *
3816 	 * We need to mark these as app-limited so we
3817 	 * don't collapse the b/w.
3818 	 */
3819 	rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
3820 	if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) {
3821 		if (rack->r_ctl.rc_app_limited_cnt == 0)
3822 			rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm;
3823 		else {
3824 			/*
3825 			 * Go out to the end app limited and mark
3826 			 * this new one as next and move the end_appl up
3827 			 * to this guy.
3828 			 */
3829 			if (rack->r_ctl.rc_end_appl)
3830 				rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start;
3831 			rack->r_ctl.rc_end_appl = rsm;
3832 		}
3833 		rsm->r_flags |= RACK_APP_LIMITED;
3834 		rack->r_ctl.rc_app_limited_cnt++;
3835 	}
3836 	/*
3837 	 * Now, we need to examine our pacing rate multipliers.
3838 	 * If its under 100%, we need to kick it back up to
3839 	 * 100%. We also don't let it be over our "max" above
3840 	 * the actual rate i.e. 100% + rack_clamp_atexit_prtt.
3841 	 * Note setting clamp_atexit_prtt to 0 has the effect
3842 	 * of setting CA/SS to 100% always at exit (which is
3843 	 * the default behavior).
3844 	 */
3845 	if (rack_probertt_clear_is) {
3846 		rack->rc_gp_incr = 0;
3847 		rack->rc_gp_bwred = 0;
3848 		rack->rc_gp_timely_inc_cnt = 0;
3849 		rack->rc_gp_timely_dec_cnt = 0;
3850 	}
3851 	/* Do we do any clamping at exit? */
3852 	if (rack->rc_highly_buffered && rack_atexit_prtt_hbp) {
3853 		rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt_hbp;
3854 		rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt_hbp;
3855 	}
3856 	if ((rack->rc_highly_buffered == 0) && rack_atexit_prtt) {
3857 		rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt;
3858 		rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt;
3859 	}
3860 	/*
3861 	 * Lets set rtt_diff to 0, so that we will get a "boost"
3862 	 * after exiting.
3863 	 */
3864 	rack->r_ctl.rc_rtt_diff = 0;
3865 
3866 	/* Clear all flags so we start fresh */
3867 	rack->rc_tp->t_bytes_acked = 0;
3868 	rack->rc_tp->ccv->flags &= ~CCF_ABC_SENTAWND;
3869 	/*
3870 	 * If configured to, set the cwnd and ssthresh to
3871 	 * our targets.
3872 	 */
3873 	if (rack_probe_rtt_sets_cwnd) {
3874 		uint64_t ebdp;
3875 		uint32_t setto;
3876 
3877 		/* Set ssthresh so we get into CA once we hit our target */
3878 		if (rack_probertt_use_min_rtt_exit == 1) {
3879 			/* Set to min rtt */
3880 			rack_set_prtt_target(rack, segsiz,
3881 					     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt));
3882 		} else if (rack_probertt_use_min_rtt_exit == 2) {
3883 			/* Set to current gp rtt */
3884 			rack_set_prtt_target(rack, segsiz,
3885 					     rack->r_ctl.rc_gp_srtt);
3886 		} else if (rack_probertt_use_min_rtt_exit == 3) {
3887 			/* Set to entry gp rtt */
3888 			rack_set_prtt_target(rack, segsiz,
3889 					     rack->r_ctl.rc_entry_gp_rtt);
3890 		} else {
3891 			uint64_t sum;
3892 			uint32_t setval;
3893 
3894 			sum = rack->r_ctl.rc_entry_gp_rtt;
3895 			sum *= 10;
3896 			sum /= (uint64_t)(max(1, rack->r_ctl.rc_gp_srtt));
3897 			if (sum >= 20) {
3898 				/*
3899 				 * A highly buffered path needs
3900 				 * cwnd space for timely to work.
3901 				 * Lets set things up as if
3902 				 * we are heading back here again.
3903 				 */
3904 				setval = rack->r_ctl.rc_entry_gp_rtt;
3905 			} else if (sum >= 15) {
3906 				/*
3907 				 * Lets take the smaller of the
3908 				 * two since we are just somewhat
3909 				 * buffered.
3910 				 */
3911 				setval = rack->r_ctl.rc_gp_srtt;
3912 				if (setval > rack->r_ctl.rc_entry_gp_rtt)
3913 					setval = rack->r_ctl.rc_entry_gp_rtt;
3914 			} else {
3915 				/*
3916 				 * Here we are not highly buffered
3917 				 * and should pick the min we can to
3918 				 * keep from causing loss.
3919 				 */
3920 				setval = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
3921 			}
3922 			rack_set_prtt_target(rack, segsiz,
3923 					     setval);
3924 		}
3925 		if (rack_probe_rtt_sets_cwnd > 1) {
3926 			/* There is a percentage here to boost */
3927 			ebdp = rack->r_ctl.rc_target_probertt_flight;
3928 			ebdp *= rack_probe_rtt_sets_cwnd;
3929 			ebdp /= 100;
3930 			setto = rack->r_ctl.rc_target_probertt_flight + ebdp;
3931 		} else
3932 			setto = rack->r_ctl.rc_target_probertt_flight;
3933 		rack->rc_tp->snd_cwnd = roundup(setto, segsiz);
3934 		if (rack->rc_tp->snd_cwnd < (segsiz * rack_timely_min_segs)) {
3935 			/* Enforce a min */
3936 			rack->rc_tp->snd_cwnd = segsiz * rack_timely_min_segs;
3937 		}
3938 		/* If we set in the cwnd also set the ssthresh point so we are in CA */
3939 		rack->rc_tp->snd_ssthresh = (rack->rc_tp->snd_cwnd - 1);
3940 	}
3941 	rack_log_rtt_shrinks(rack,  us_cts,
3942 			     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
3943 			     __LINE__, RACK_RTTS_EXITPROBE);
3944 	/* Clear times last so log has all the info */
3945 	rack->r_ctl.rc_probertt_sndmax_atexit = rack->rc_tp->snd_max;
3946 	rack->r_ctl.rc_time_probertt_entered = us_cts;
3947 	rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
3948 	rack->r_ctl.rc_time_of_last_probertt = us_cts;
3949 }
3950 
3951 static void
3952 rack_check_probe_rtt(struct tcp_rack *rack, uint32_t us_cts)
3953 {
3954 	/* Check in on probe-rtt */
3955 	if (rack->rc_gp_filled == 0) {
3956 		/* We do not do p-rtt unless we have gp measurements */
3957 		return;
3958 	}
3959 	if (rack->in_probe_rtt) {
3960 		uint64_t no_overflow;
3961 		uint32_t endtime, must_stay;
3962 
3963 		if (rack->r_ctl.rc_went_idle_time &&
3964 		    ((us_cts - rack->r_ctl.rc_went_idle_time) > rack_min_probertt_hold)) {
3965 			/*
3966 			 * We went idle during prtt, just exit now.
3967 			 */
3968 			rack_exit_probertt(rack, us_cts);
3969 		} else if (rack_probe_rtt_safety_val &&
3970 		    TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered) &&
3971 		    ((us_cts - rack->r_ctl.rc_time_probertt_entered) > rack_probe_rtt_safety_val)) {
3972 			/*
3973 			 * Probe RTT safety value triggered!
3974 			 */
3975 			rack_log_rtt_shrinks(rack,  us_cts,
3976 					     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
3977 					     __LINE__, RACK_RTTS_SAFETY);
3978 			rack_exit_probertt(rack, us_cts);
3979 		}
3980 		/* Calculate the max we will wait */
3981 		endtime = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_max_drain_wait);
3982 		if (rack->rc_highly_buffered)
3983 			endtime += (rack->r_ctl.rc_gp_srtt * rack_max_drain_hbp);
3984 		/* Calculate the min we must wait */
3985 		must_stay = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_must_drain);
3986 		if ((ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.rc_target_probertt_flight) &&
3987 		    TSTMP_LT(us_cts, endtime)) {
3988 			uint32_t calc;
3989 			/* Do we lower more? */
3990 no_exit:
3991 			if (TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered))
3992 				calc = us_cts - rack->r_ctl.rc_time_probertt_entered;
3993 			else
3994 				calc = 0;
3995 			calc /= max(rack->r_ctl.rc_gp_srtt, 1);
3996 			if (calc) {
3997 				/* Maybe */
3998 				calc *= rack_per_of_gp_probertt_reduce;
3999 				rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt - calc;
4000 				/* Limit it too */
4001 				if (rack->r_ctl.rack_per_of_gp_probertt < rack_per_of_gp_lowthresh)
4002 					rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_lowthresh;
4003 			}
4004 			/* We must reach target or the time set */
4005 			return;
4006 		}
4007 		if (rack->r_ctl.rc_time_probertt_starts == 0) {
4008 			if ((TSTMP_LT(us_cts, must_stay) &&
4009 			     rack->rc_highly_buffered) ||
4010 			     (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) >
4011 			      rack->r_ctl.rc_target_probertt_flight)) {
4012 				/* We are not past the must_stay time */
4013 				goto no_exit;
4014 			}
4015 			rack_log_rtt_shrinks(rack,  us_cts,
4016 					     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4017 					     __LINE__, RACK_RTTS_REACHTARGET);
4018 			rack->r_ctl.rc_time_probertt_starts = us_cts;
4019 			if (rack->r_ctl.rc_time_probertt_starts == 0)
4020 				rack->r_ctl.rc_time_probertt_starts = 1;
4021 			/* Restore back to our rate we want to pace at in prtt */
4022 			rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
4023 		}
4024 		/*
4025 		 * Setup our end time, some number of gp_srtts plus 200ms.
4026 		 */
4027 		no_overflow = ((uint64_t)rack->r_ctl.rc_gp_srtt *
4028 			       (uint64_t)rack_probertt_gpsrtt_cnt_mul);
4029 		if (rack_probertt_gpsrtt_cnt_div)
4030 			endtime = (uint32_t)(no_overflow / (uint64_t)rack_probertt_gpsrtt_cnt_div);
4031 		else
4032 			endtime = 0;
4033 		endtime += rack_min_probertt_hold;
4034 		endtime += rack->r_ctl.rc_time_probertt_starts;
4035 		if (TSTMP_GEQ(us_cts,  endtime)) {
4036 			/* yes, exit probertt */
4037 			rack_exit_probertt(rack, us_cts);
4038 		}
4039 
4040 	} else if ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= rack_time_between_probertt) {
4041 		/* Go into probertt, its been too long since we went lower */
4042 		rack_enter_probertt(rack, us_cts);
4043 	}
4044 }
4045 
4046 static void
4047 rack_update_multiplier(struct tcp_rack *rack, int32_t timely_says, uint64_t last_bw_est,
4048 		       uint32_t rtt, int32_t rtt_diff)
4049 {
4050 	uint64_t cur_bw, up_bnd, low_bnd, subfr;
4051 	uint32_t losses;
4052 
4053 	if ((rack->rc_gp_dyn_mul == 0) ||
4054 	    (rack->use_fixed_rate) ||
4055 	    (rack->in_probe_rtt) ||
4056 	    (rack->rc_always_pace == 0)) {
4057 		/* No dynamic GP multipler in play */
4058 		return;
4059 	}
4060 	losses = rack->r_ctl.rc_loss_count - rack->r_ctl.rc_loss_at_start;
4061 	cur_bw = rack_get_bw(rack);
4062 	/* Calculate our up and down range */
4063 	up_bnd = rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_up;
4064 	up_bnd /= 100;
4065 	up_bnd += rack->r_ctl.last_gp_comp_bw;
4066 
4067 	subfr = (uint64_t)rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_down;
4068 	subfr /= 100;
4069 	low_bnd = rack->r_ctl.last_gp_comp_bw - subfr;
4070 	if ((timely_says == 2) && (rack->r_ctl.rc_no_push_at_mrtt)) {
4071 		/*
4072 		 * This is the case where our RTT is above
4073 		 * the max target and we have been configured
4074 		 * to just do timely no bonus up stuff in that case.
4075 		 *
4076 		 * There are two configurations, set to 1, and we
4077 		 * just do timely if we are over our max. If its
4078 		 * set above 1 then we slam the multipliers down
4079 		 * to 100 and then decrement per timely.
4080 		 */
4081 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4082 				__LINE__, 3);
4083 		if (rack->r_ctl.rc_no_push_at_mrtt > 1)
4084 			rack_validate_multipliers_at_or_below_100(rack);
4085 		rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff);
4086 	} else if ((last_bw_est < low_bnd) && !losses) {
4087 		/*
4088 		 * We are decreasing this is a bit complicated this
4089 		 * means we are loosing ground. This could be
4090 		 * because another flow entered and we are competing
4091 		 * for b/w with it. This will push the RTT up which
4092 		 * makes timely unusable unless we want to get shoved
4093 		 * into a corner and just be backed off (the age
4094 		 * old problem with delay based CC).
4095 		 *
4096 		 * On the other hand if it was a route change we
4097 		 * would like to stay somewhat contained and not
4098 		 * blow out the buffers.
4099 		 */
4100 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4101 				__LINE__, 3);
4102 		rack->r_ctl.last_gp_comp_bw = cur_bw;
4103 		if (rack->rc_gp_bwred == 0) {
4104 			/* Go into reduction counting */
4105 			rack->rc_gp_bwred = 1;
4106 			rack->rc_gp_timely_dec_cnt = 0;
4107 		}
4108 		if ((rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) ||
4109 		    (timely_says == 0)) {
4110 			/*
4111 			 * Push another time with a faster pacing
4112 			 * to try to gain back (we include override to
4113 			 * get a full raise factor).
4114 			 */
4115 			if ((rack->rc_gp_saw_ca && rack->r_ctl.rack_per_of_gp_ca <= rack_down_raise_thresh) ||
4116 			    (rack->rc_gp_saw_ss && rack->r_ctl.rack_per_of_gp_ss <= rack_down_raise_thresh) ||
4117 			    (timely_says == 0) ||
4118 			    (rack_down_raise_thresh == 0)) {
4119 				/*
4120 				 * Do an override up in b/w if we were
4121 				 * below the threshold or if the threshold
4122 				 * is zero we always do the raise.
4123 				 */
4124 				rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 1);
4125 			} else {
4126 				/* Log it stays the same */
4127 				rack_log_timely(rack,  0, last_bw_est, low_bnd, 0,
4128 						__LINE__, 11);
4129 			}
4130 			rack->rc_gp_timely_dec_cnt++;
4131 			/* We are not incrementing really no-count */
4132 			rack->rc_gp_incr = 0;
4133 			rack->rc_gp_timely_inc_cnt = 0;
4134 		} else {
4135 			/*
4136 			 * Lets just use the RTT
4137 			 * information and give up
4138 			 * pushing.
4139 			 */
4140 			goto use_timely;
4141 		}
4142 	} else if ((timely_says != 2) &&
4143 		    !losses &&
4144 		    (last_bw_est > up_bnd)) {
4145 		/*
4146 		 * We are increasing b/w lets keep going, updating
4147 		 * our b/w and ignoring any timely input, unless
4148 		 * of course we are at our max raise (if there is one).
4149 		 */
4150 
4151 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4152 				__LINE__, 3);
4153 		rack->r_ctl.last_gp_comp_bw = cur_bw;
4154 		if (rack->rc_gp_saw_ss &&
4155 		    rack_per_upper_bound_ss &&
4156 		     (rack->r_ctl.rack_per_of_gp_ss == rack_per_upper_bound_ss)) {
4157 			    /*
4158 			     * In cases where we can't go higher
4159 			     * we should just use timely.
4160 			     */
4161 			    goto use_timely;
4162 		}
4163 		if (rack->rc_gp_saw_ca &&
4164 		    rack_per_upper_bound_ca &&
4165 		    (rack->r_ctl.rack_per_of_gp_ca == rack_per_upper_bound_ca)) {
4166 			    /*
4167 			     * In cases where we can't go higher
4168 			     * we should just use timely.
4169 			     */
4170 			    goto use_timely;
4171 		}
4172 		rack->rc_gp_bwred = 0;
4173 		rack->rc_gp_timely_dec_cnt = 0;
4174 		/* You get a set number of pushes if timely is trying to reduce */
4175 		if ((rack->rc_gp_incr < rack_timely_max_push_rise) || (timely_says == 0)) {
4176 			rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4177 		} else {
4178 			/* Log it stays the same */
4179 			rack_log_timely(rack,  0, last_bw_est, up_bnd, 0,
4180 			    __LINE__, 12);
4181 		}
4182 		return;
4183 	} else {
4184 		/*
4185 		 * We are staying between the lower and upper range bounds
4186 		 * so use timely to decide.
4187 		 */
4188 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4189 				__LINE__, 3);
4190 use_timely:
4191 		if (timely_says) {
4192 			rack->rc_gp_incr = 0;
4193 			rack->rc_gp_timely_inc_cnt = 0;
4194 			if ((rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) &&
4195 			    !losses &&
4196 			    (last_bw_est < low_bnd)) {
4197 				/* We are loosing ground */
4198 				rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4199 				rack->rc_gp_timely_dec_cnt++;
4200 				/* We are not incrementing really no-count */
4201 				rack->rc_gp_incr = 0;
4202 				rack->rc_gp_timely_inc_cnt = 0;
4203 			} else
4204 				rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff);
4205 		} else {
4206 			rack->rc_gp_bwred = 0;
4207 			rack->rc_gp_timely_dec_cnt = 0;
4208 			rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4209 		}
4210 	}
4211 }
4212 
4213 static int32_t
4214 rack_make_timely_judgement(struct tcp_rack *rack, uint32_t rtt, int32_t rtt_diff, uint32_t prev_rtt)
4215 {
4216 	int32_t timely_says;
4217 	uint64_t log_mult, log_rtt_a_diff;
4218 
4219 	log_rtt_a_diff = rtt;
4220 	log_rtt_a_diff <<= 32;
4221 	log_rtt_a_diff |= (uint32_t)rtt_diff;
4222 	if (rtt >= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) *
4223 		    rack_gp_rtt_maxmul)) {
4224 		/* Reduce the b/w multipler */
4225 		timely_says = 2;
4226 		log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
4227 		log_mult <<= 32;
4228 		log_mult |= prev_rtt;
4229 		rack_log_timely(rack,  timely_says, log_mult,
4230 				get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4231 				log_rtt_a_diff, __LINE__, 4);
4232 	} else if (rtt <= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) +
4233 			   ((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) /
4234 			    max(rack_gp_rtt_mindiv , 1)))) {
4235 		/* Increase the b/w multipler */
4236 		log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) +
4237 			((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) /
4238 			 max(rack_gp_rtt_mindiv , 1));
4239 		log_mult <<= 32;
4240 		log_mult |= prev_rtt;
4241 		timely_says = 0;
4242 		rack_log_timely(rack,  timely_says, log_mult ,
4243 				get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4244 				log_rtt_a_diff, __LINE__, 5);
4245 	} else {
4246 		/*
4247 		 * Use a gradient to find it the timely gradient
4248 		 * is:
4249 		 * grad = rc_rtt_diff / min_rtt;
4250 		 *
4251 		 * anything below or equal to 0 will be
4252 		 * a increase indication. Anything above
4253 		 * zero is a decrease. Note we take care
4254 		 * of the actual gradient calculation
4255 		 * in the reduction (its not needed for
4256 		 * increase).
4257 		 */
4258 		log_mult = prev_rtt;
4259 		if (rtt_diff <= 0) {
4260 			/*
4261 			 * Rttdiff is less than zero, increase the
4262 			 * b/w multipler (its 0 or negative)
4263 			 */
4264 			timely_says = 0;
4265 			rack_log_timely(rack,  timely_says, log_mult,
4266 					get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 6);
4267 		} else {
4268 			/* Reduce the b/w multipler */
4269 			timely_says = 1;
4270 			rack_log_timely(rack,  timely_says, log_mult,
4271 					get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 7);
4272 		}
4273 	}
4274 	return (timely_says);
4275 }
4276 
4277 static void
4278 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
4279 			    tcp_seq th_ack, int line, uint8_t quality)
4280 {
4281 	uint64_t tim, bytes_ps, ltim, stim, utim;
4282 	uint32_t segsiz, bytes, reqbytes, us_cts;
4283 	int32_t gput, new_rtt_diff, timely_says;
4284 	uint64_t  resid_bw, subpart = 0, addpart = 0, srtt;
4285 	int did_add = 0;
4286 
4287 	us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
4288 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
4289 	if (TSTMP_GEQ(us_cts, tp->gput_ts))
4290 		tim = us_cts - tp->gput_ts;
4291 	else
4292 		tim = 0;
4293 	if (rack->r_ctl.rc_gp_cumack_ts > rack->r_ctl.rc_gp_output_ts)
4294 		stim = rack->r_ctl.rc_gp_cumack_ts - rack->r_ctl.rc_gp_output_ts;
4295 	else
4296 		stim = 0;
4297 	/*
4298 	 * Use the larger of the send time or ack time. This prevents us
4299 	 * from being influenced by ack artifacts to come up with too
4300 	 * high of measurement. Note that since we are spanning over many more
4301 	 * bytes in most of our measurements hopefully that is less likely to
4302 	 * occur.
4303 	 */
4304 	if (tim > stim)
4305 		utim = max(tim, 1);
4306 	else
4307 		utim = max(stim, 1);
4308 	/* Lets get a msec time ltim too for the old stuff */
4309 	ltim = max(1, (utim / HPTS_USEC_IN_MSEC));
4310 	gput = (((uint64_t) (th_ack - tp->gput_seq)) << 3) / ltim;
4311 	reqbytes = min(rc_init_window(rack), (MIN_GP_WIN * segsiz));
4312 	if ((tim == 0) && (stim == 0)) {
4313 		/*
4314 		 * Invalid measurement time, maybe
4315 		 * all on one ack/one send?
4316 		 */
4317 		bytes = 0;
4318 		bytes_ps = 0;
4319 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4320 					   0, 0, 0, 10, __LINE__, NULL, quality);
4321 		goto skip_measurement;
4322 	}
4323 	if (rack->r_ctl.rc_gp_lowrtt == 0xffffffff) {
4324 		/* We never made a us_rtt measurement? */
4325 		bytes = 0;
4326 		bytes_ps = 0;
4327 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4328 					   0, 0, 0, 10, __LINE__, NULL, quality);
4329 		goto skip_measurement;
4330 	}
4331 	/*
4332 	 * Calculate the maximum possible b/w this connection
4333 	 * could have. We base our calculation on the lowest
4334 	 * rtt we have seen during the measurement and the
4335 	 * largest rwnd the client has given us in that time. This
4336 	 * forms a BDP that is the maximum that we could ever
4337 	 * get to the client. Anything larger is not valid.
4338 	 *
4339 	 * I originally had code here that rejected measurements
4340 	 * where the time was less than 1/2 the latest us_rtt.
4341 	 * But after thinking on that I realized its wrong since
4342 	 * say you had a 150Mbps or even 1Gbps link, and you
4343 	 * were a long way away.. example I am in Europe (100ms rtt)
4344 	 * talking to my 1Gbps link in S.C. Now measuring say 150,000
4345 	 * bytes my time would be 1.2ms, and yet my rtt would say
4346 	 * the measurement was invalid the time was < 50ms. The
4347 	 * same thing is true for 150Mb (8ms of time).
4348 	 *
4349 	 * A better way I realized is to look at what the maximum
4350 	 * the connection could possibly do. This is gated on
4351 	 * the lowest RTT we have seen and the highest rwnd.
4352 	 * We should in theory never exceed that, if we are
4353 	 * then something on the path is storing up packets
4354 	 * and then feeding them all at once to our endpoint
4355 	 * messing up our measurement.
4356 	 */
4357 	rack->r_ctl.last_max_bw = rack->r_ctl.rc_gp_high_rwnd;
4358 	rack->r_ctl.last_max_bw *= HPTS_USEC_IN_SEC;
4359 	rack->r_ctl.last_max_bw /= rack->r_ctl.rc_gp_lowrtt;
4360 	if (SEQ_LT(th_ack, tp->gput_seq)) {
4361 		/* No measurement can be made */
4362 		bytes = 0;
4363 		bytes_ps = 0;
4364 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4365 					   0, 0, 0, 10, __LINE__, NULL, quality);
4366 		goto skip_measurement;
4367 	} else
4368 		bytes = (th_ack - tp->gput_seq);
4369 	bytes_ps = (uint64_t)bytes;
4370 	/*
4371 	 * Don't measure a b/w for pacing unless we have gotten at least
4372 	 * an initial windows worth of data in this measurement interval.
4373 	 *
4374 	 * Small numbers of bytes get badly influenced by delayed ack and
4375 	 * other artifacts. Note we take the initial window or our
4376 	 * defined minimum GP (defaulting to 10 which hopefully is the
4377 	 * IW).
4378 	 */
4379 	if (rack->rc_gp_filled == 0) {
4380 		/*
4381 		 * The initial estimate is special. We
4382 		 * have blasted out an IW worth of packets
4383 		 * without a real valid ack ts results. We
4384 		 * then setup the app_limited_needs_set flag,
4385 		 * this should get the first ack in (probably 2
4386 		 * MSS worth) to be recorded as the timestamp.
4387 		 * We thus allow a smaller number of bytes i.e.
4388 		 * IW - 2MSS.
4389 		 */
4390 		reqbytes -= (2 * segsiz);
4391 		/* Also lets fill previous for our first measurement to be neutral */
4392 		rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt;
4393 	}
4394 	if ((bytes_ps < reqbytes) || rack->app_limited_needs_set) {
4395 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4396 					   rack->r_ctl.rc_app_limited_cnt,
4397 					   0, 0, 10, __LINE__, NULL, quality);
4398 		goto skip_measurement;
4399 	}
4400 	/*
4401 	 * We now need to calculate the Timely like status so
4402 	 * we can update (possibly) the b/w multipliers.
4403 	 */
4404 	new_rtt_diff = (int32_t)rack->r_ctl.rc_gp_srtt - (int32_t)rack->r_ctl.rc_prev_gp_srtt;
4405 	if (rack->rc_gp_filled == 0) {
4406 		/* No previous reading */
4407 		rack->r_ctl.rc_rtt_diff = new_rtt_diff;
4408 	} else {
4409 		if (rack->measure_saw_probe_rtt == 0) {
4410 			/*
4411 			 * We don't want a probertt to be counted
4412 			 * since it will be negative incorrectly. We
4413 			 * expect to be reducing the RTT when we
4414 			 * pace at a slower rate.
4415 			 */
4416 			rack->r_ctl.rc_rtt_diff -= (rack->r_ctl.rc_rtt_diff / 8);
4417 			rack->r_ctl.rc_rtt_diff += (new_rtt_diff / 8);
4418 		}
4419 	}
4420 	timely_says = rack_make_timely_judgement(rack,
4421 		rack->r_ctl.rc_gp_srtt,
4422 		rack->r_ctl.rc_rtt_diff,
4423 	        rack->r_ctl.rc_prev_gp_srtt
4424 		);
4425 	bytes_ps *= HPTS_USEC_IN_SEC;
4426 	bytes_ps /= utim;
4427 	if (bytes_ps > rack->r_ctl.last_max_bw) {
4428 		/*
4429 		 * Something is on path playing
4430 		 * since this b/w is not possible based
4431 		 * on our BDP (highest rwnd and lowest rtt
4432 		 * we saw in the measurement window).
4433 		 *
4434 		 * Another option here would be to
4435 		 * instead skip the measurement.
4436 		 */
4437 		rack_log_pacing_delay_calc(rack, bytes, reqbytes,
4438 					   bytes_ps, rack->r_ctl.last_max_bw, 0,
4439 					   11, __LINE__, NULL, quality);
4440 		bytes_ps = rack->r_ctl.last_max_bw;
4441 	}
4442 	/* We store gp for b/w in bytes per second */
4443 	if (rack->rc_gp_filled == 0) {
4444 		/* Initial measurement */
4445 		if (bytes_ps) {
4446 			rack->r_ctl.gp_bw = bytes_ps;
4447 			rack->rc_gp_filled = 1;
4448 			rack->r_ctl.num_measurements = 1;
4449 			rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
4450 		} else {
4451 			rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4452 						   rack->r_ctl.rc_app_limited_cnt,
4453 						   0, 0, 10, __LINE__, NULL, quality);
4454 		}
4455 		if (tcp_in_hpts(rack->rc_inp) &&
4456 		    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
4457 			/*
4458 			 * Ok we can't trust the pacer in this case
4459 			 * where we transition from un-paced to paced.
4460 			 * Or for that matter when the burst mitigation
4461 			 * was making a wild guess and got it wrong.
4462 			 * Stop the pacer and clear up all the aggregate
4463 			 * delays etc.
4464 			 */
4465 			tcp_hpts_remove(rack->rc_inp);
4466 			rack->r_ctl.rc_hpts_flags = 0;
4467 			rack->r_ctl.rc_last_output_to = 0;
4468 		}
4469 		did_add = 2;
4470 	} else if (rack->r_ctl.num_measurements < RACK_REQ_AVG) {
4471 		/* Still a small number run an average */
4472 		rack->r_ctl.gp_bw += bytes_ps;
4473 		addpart = rack->r_ctl.num_measurements;
4474 		rack->r_ctl.num_measurements++;
4475 		if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) {
4476 			/* We have collected enought to move forward */
4477 			rack->r_ctl.gp_bw /= (uint64_t)rack->r_ctl.num_measurements;
4478 		}
4479 		did_add = 3;
4480 	} else {
4481 		/*
4482 		 * We want to take 1/wma of the goodput and add in to 7/8th
4483 		 * of the old value weighted by the srtt. So if your measurement
4484 		 * period is say 2 SRTT's long you would get 1/4 as the
4485 		 * value, if it was like 1/2 SRTT then you would get 1/16th.
4486 		 *
4487 		 * But we must be careful not to take too much i.e. if the
4488 		 * srtt is say 20ms and the measurement is taken over
4489 		 * 400ms our weight would be 400/20 i.e. 20. On the
4490 		 * other hand if we get a measurement over 1ms with a
4491 		 * 10ms rtt we only want to take a much smaller portion.
4492 		 */
4493 		if (rack->r_ctl.num_measurements < 0xff) {
4494 			rack->r_ctl.num_measurements++;
4495 		}
4496 		srtt = (uint64_t)tp->t_srtt;
4497 		if (srtt == 0) {
4498 			/*
4499 			 * Strange why did t_srtt go back to zero?
4500 			 */
4501 			if (rack->r_ctl.rc_rack_min_rtt)
4502 				srtt = rack->r_ctl.rc_rack_min_rtt;
4503 			else
4504 				srtt = HPTS_USEC_IN_MSEC;
4505 		}
4506 		/*
4507 		 * XXXrrs: Note for reviewers, in playing with
4508 		 * dynamic pacing I discovered this GP calculation
4509 		 * as done originally leads to some undesired results.
4510 		 * Basically you can get longer measurements contributing
4511 		 * too much to the WMA. Thus I changed it if you are doing
4512 		 * dynamic adjustments to only do the aportioned adjustment
4513 		 * if we have a very small (time wise) measurement. Longer
4514 		 * measurements just get there weight (defaulting to 1/8)
4515 		 * add to the WMA. We may want to think about changing
4516 		 * this to always do that for both sides i.e. dynamic
4517 		 * and non-dynamic... but considering lots of folks
4518 		 * were playing with this I did not want to change the
4519 		 * calculation per.se. without your thoughts.. Lawerence?
4520 		 * Peter??
4521 		 */
4522 		if (rack->rc_gp_dyn_mul == 0) {
4523 			subpart = rack->r_ctl.gp_bw * utim;
4524 			subpart /= (srtt * 8);
4525 			if (subpart < (rack->r_ctl.gp_bw / 2)) {
4526 				/*
4527 				 * The b/w update takes no more
4528 				 * away then 1/2 our running total
4529 				 * so factor it in.
4530 				 */
4531 				addpart = bytes_ps * utim;
4532 				addpart /= (srtt * 8);
4533 			} else {
4534 				/*
4535 				 * Don't allow a single measurement
4536 				 * to account for more than 1/2 of the
4537 				 * WMA. This could happen on a retransmission
4538 				 * where utim becomes huge compared to
4539 				 * srtt (multiple retransmissions when using
4540 				 * the sending rate which factors in all the
4541 				 * transmissions from the first one).
4542 				 */
4543 				subpart = rack->r_ctl.gp_bw / 2;
4544 				addpart = bytes_ps / 2;
4545 			}
4546 			resid_bw = rack->r_ctl.gp_bw - subpart;
4547 			rack->r_ctl.gp_bw = resid_bw + addpart;
4548 			did_add = 1;
4549 		} else {
4550 			if ((utim / srtt) <= 1) {
4551 				/*
4552 				 * The b/w update was over a small period
4553 				 * of time. The idea here is to prevent a small
4554 				 * measurement time period from counting
4555 				 * too much. So we scale it based on the
4556 				 * time so it attributes less than 1/rack_wma_divisor
4557 				 * of its measurement.
4558 				 */
4559 				subpart = rack->r_ctl.gp_bw * utim;
4560 				subpart /= (srtt * rack_wma_divisor);
4561 				addpart = bytes_ps * utim;
4562 				addpart /= (srtt * rack_wma_divisor);
4563 			} else {
4564 				/*
4565 				 * The scaled measurement was long
4566 				 * enough so lets just add in the
4567 				 * portion of the measurement i.e. 1/rack_wma_divisor
4568 				 */
4569 				subpart = rack->r_ctl.gp_bw / rack_wma_divisor;
4570 				addpart = bytes_ps / rack_wma_divisor;
4571 			}
4572 			if ((rack->measure_saw_probe_rtt == 0) ||
4573 		            (bytes_ps > rack->r_ctl.gp_bw)) {
4574 				/*
4575 				 * For probe-rtt we only add it in
4576 				 * if its larger, all others we just
4577 				 * add in.
4578 				 */
4579 				did_add = 1;
4580 				resid_bw = rack->r_ctl.gp_bw - subpart;
4581 				rack->r_ctl.gp_bw = resid_bw + addpart;
4582 			}
4583 		}
4584 	}
4585 	if ((rack->gp_ready == 0) &&
4586 	    (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) {
4587 		/* We have enough measurements now */
4588 		rack->gp_ready = 1;
4589 		rack_set_cc_pacing(rack);
4590 		if (rack->defer_options)
4591 			rack_apply_deferred_options(rack);
4592 	}
4593 	rack_log_pacing_delay_calc(rack, subpart, addpart, bytes_ps, stim,
4594 				   rack_get_bw(rack), 22, did_add, NULL, quality);
4595 	/* We do not update any multipliers if we are in or have seen a probe-rtt */
4596 	if ((rack->measure_saw_probe_rtt == 0) && rack->rc_gp_rtt_set)
4597 		rack_update_multiplier(rack, timely_says, bytes_ps,
4598 				       rack->r_ctl.rc_gp_srtt,
4599 				       rack->r_ctl.rc_rtt_diff);
4600 	rack_log_pacing_delay_calc(rack, bytes, tim, bytes_ps, stim,
4601 				   rack_get_bw(rack), 3, line, NULL, quality);
4602 	/* reset the gp srtt and setup the new prev */
4603 	rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt;
4604 	/* Record the lost count for the next measurement */
4605 	rack->r_ctl.rc_loss_at_start = rack->r_ctl.rc_loss_count;
4606 	/*
4607 	 * We restart our diffs based on the gpsrtt in the
4608 	 * measurement window.
4609 	 */
4610 	rack->rc_gp_rtt_set = 0;
4611 	rack->rc_gp_saw_rec = 0;
4612 	rack->rc_gp_saw_ca = 0;
4613 	rack->rc_gp_saw_ss = 0;
4614 	rack->rc_dragged_bottom = 0;
4615 skip_measurement:
4616 
4617 #ifdef STATS
4618 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
4619 				 gput);
4620 	/*
4621 	 * XXXLAS: This is a temporary hack, and should be
4622 	 * chained off VOI_TCP_GPUT when stats(9) grows an
4623 	 * API to deal with chained VOIs.
4624 	 */
4625 	if (tp->t_stats_gput_prev > 0)
4626 		stats_voi_update_abs_s32(tp->t_stats,
4627 					 VOI_TCP_GPUT_ND,
4628 					 ((gput - tp->t_stats_gput_prev) * 100) /
4629 					 tp->t_stats_gput_prev);
4630 #endif
4631 	tp->t_flags &= ~TF_GPUTINPROG;
4632 	tp->t_stats_gput_prev = gput;
4633 	/*
4634 	 * Now are we app limited now and there is space from where we
4635 	 * were to where we want to go?
4636 	 *
4637 	 * We don't do the other case i.e. non-applimited here since
4638 	 * the next send will trigger us picking up the missing data.
4639 	 */
4640 	if (rack->r_ctl.rc_first_appl &&
4641 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
4642 	    rack->r_ctl.rc_app_limited_cnt &&
4643 	    (SEQ_GT(rack->r_ctl.rc_first_appl->r_start, th_ack)) &&
4644 	    ((rack->r_ctl.rc_first_appl->r_end - th_ack) >
4645 	     max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) {
4646 		/*
4647 		 * Yep there is enough outstanding to make a measurement here.
4648 		 */
4649 		struct rack_sendmap *rsm, fe;
4650 
4651 		rack->r_ctl.rc_gp_lowrtt = 0xffffffff;
4652 		rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
4653 		tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
4654 		rack->app_limited_needs_set = 0;
4655 		tp->gput_seq = th_ack;
4656 		if (rack->in_probe_rtt)
4657 			rack->measure_saw_probe_rtt = 1;
4658 		else if ((rack->measure_saw_probe_rtt) &&
4659 			 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
4660 			rack->measure_saw_probe_rtt = 0;
4661 		if ((rack->r_ctl.rc_first_appl->r_end - th_ack) >= rack_get_measure_window(tp, rack)) {
4662 			/* There is a full window to gain info from */
4663 			tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
4664 		} else {
4665 			/* We can only measure up to the applimited point */
4666 			tp->gput_ack = tp->gput_seq + (rack->r_ctl.rc_first_appl->r_end - th_ack);
4667 			if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) {
4668 				/*
4669 				 * We don't have enough to make a measurement.
4670 				 */
4671 				tp->t_flags &= ~TF_GPUTINPROG;
4672 				rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq,
4673 							   0, 0, 0, 6, __LINE__, NULL, quality);
4674 				return;
4675 			}
4676 		}
4677 		if (tp->t_state >= TCPS_FIN_WAIT_1) {
4678 			/*
4679 			 * We will get no more data into the SB
4680 			 * this means we need to have the data available
4681 			 * before we start a measurement.
4682 			 */
4683 			if (sbavail(&tp->t_inpcb->inp_socket->so_snd) < (tp->gput_ack - tp->gput_seq)) {
4684 				/* Nope not enough data. */
4685 				return;
4686 			}
4687 		}
4688 		tp->t_flags |= TF_GPUTINPROG;
4689 		/*
4690 		 * Now we need to find the timestamp of the send at tp->gput_seq
4691 		 * for the send based measurement.
4692 		 */
4693 		fe.r_start = tp->gput_seq;
4694 		rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
4695 		if (rsm) {
4696 			/* Ok send-based limit is set */
4697 			if (SEQ_LT(rsm->r_start, tp->gput_seq)) {
4698 				/*
4699 				 * Move back to include the earlier part
4700 				 * so our ack time lines up right (this may
4701 				 * make an overlapping measurement but thats
4702 				 * ok).
4703 				 */
4704 				tp->gput_seq = rsm->r_start;
4705 			}
4706 			if (rsm->r_flags & RACK_ACKED)
4707 				tp->gput_ts = (uint32_t)rsm->r_ack_arrival;
4708 			else
4709 				rack->app_limited_needs_set = 1;
4710 			rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
4711 		} else {
4712 			/*
4713 			 * If we don't find the rsm due to some
4714 			 * send-limit set the current time, which
4715 			 * basically disables the send-limit.
4716 			 */
4717 			struct timeval tv;
4718 
4719 			microuptime(&tv);
4720 			rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
4721 		}
4722 		rack_log_pacing_delay_calc(rack,
4723 					   tp->gput_seq,
4724 					   tp->gput_ack,
4725 					   (uint64_t)rsm,
4726 					   tp->gput_ts,
4727 					   rack->r_ctl.rc_app_limited_cnt,
4728 					   9,
4729 					   __LINE__, NULL, quality);
4730 	}
4731 }
4732 
4733 /*
4734  * CC wrapper hook functions
4735  */
4736 static void
4737 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack, uint32_t th_ack, uint16_t nsegs,
4738     uint16_t type, int32_t recovery)
4739 {
4740 	uint32_t prior_cwnd, acked;
4741 	struct tcp_log_buffer *lgb = NULL;
4742 	uint8_t labc_to_use, quality;
4743 
4744 	INP_WLOCK_ASSERT(tp->t_inpcb);
4745 	tp->ccv->nsegs = nsegs;
4746 	acked = tp->ccv->bytes_this_ack = (th_ack - tp->snd_una);
4747 	if ((recovery) && (rack->r_ctl.rc_early_recovery_segs)) {
4748 		uint32_t max;
4749 
4750 		max = rack->r_ctl.rc_early_recovery_segs * ctf_fixed_maxseg(tp);
4751 		if (tp->ccv->bytes_this_ack > max) {
4752 			tp->ccv->bytes_this_ack = max;
4753 		}
4754 	}
4755 #ifdef STATS
4756 	stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF,
4757 	    ((int32_t)rack->r_ctl.cwnd_to_use) - tp->snd_wnd);
4758 #endif
4759 	quality = RACK_QUALITY_NONE;
4760 	if ((tp->t_flags & TF_GPUTINPROG) &&
4761 	    rack_enough_for_measurement(tp, rack, th_ack, &quality)) {
4762 		/* Measure the Goodput */
4763 		rack_do_goodput_measurement(tp, rack, th_ack, __LINE__, quality);
4764 #ifdef NETFLIX_PEAKRATE
4765 		if ((type == CC_ACK) &&
4766 		    (tp->t_maxpeakrate)) {
4767 			/*
4768 			 * We update t_peakrate_thr. This gives us roughly
4769 			 * one update per round trip time. Note
4770 			 * it will only be used if pace_always is off i.e
4771 			 * we don't do this for paced flows.
4772 			 */
4773 			rack_update_peakrate_thr(tp);
4774 		}
4775 #endif
4776 	}
4777 	/* Which way our we limited, if not cwnd limited no advance in CA */
4778 	if (tp->snd_cwnd <= tp->snd_wnd)
4779 		tp->ccv->flags |= CCF_CWND_LIMITED;
4780 	else
4781 		tp->ccv->flags &= ~CCF_CWND_LIMITED;
4782 	if (tp->snd_cwnd > tp->snd_ssthresh) {
4783 		tp->t_bytes_acked += min(tp->ccv->bytes_this_ack,
4784 			 nsegs * V_tcp_abc_l_var * ctf_fixed_maxseg(tp));
4785 		/* For the setting of a window past use the actual scwnd we are using */
4786 		if (tp->t_bytes_acked >= rack->r_ctl.cwnd_to_use) {
4787 			tp->t_bytes_acked -= rack->r_ctl.cwnd_to_use;
4788 			tp->ccv->flags |= CCF_ABC_SENTAWND;
4789 		}
4790 	} else {
4791 		tp->ccv->flags &= ~CCF_ABC_SENTAWND;
4792 		tp->t_bytes_acked = 0;
4793 	}
4794 	prior_cwnd = tp->snd_cwnd;
4795 	if ((recovery == 0) || (rack_max_abc_post_recovery == 0) || rack->r_use_labc_for_rec ||
4796 	    (rack_client_low_buf && (rack->client_bufferlvl < rack_client_low_buf)))
4797 		labc_to_use = rack->rc_labc;
4798 	else
4799 		labc_to_use = rack_max_abc_post_recovery;
4800 	if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
4801 		union tcp_log_stackspecific log;
4802 		struct timeval tv;
4803 
4804 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
4805 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
4806 		log.u_bbr.flex1 = th_ack;
4807 		log.u_bbr.flex2 = tp->ccv->flags;
4808 		log.u_bbr.flex3 = tp->ccv->bytes_this_ack;
4809 		log.u_bbr.flex4 = tp->ccv->nsegs;
4810 		log.u_bbr.flex5 = labc_to_use;
4811 		log.u_bbr.flex6 = prior_cwnd;
4812 		log.u_bbr.flex7 = V_tcp_do_newsack;
4813 		log.u_bbr.flex8 = 1;
4814 		lgb = tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
4815 				     0, &log, false, NULL, NULL, 0, &tv);
4816 	}
4817 	if (CC_ALGO(tp)->ack_received != NULL) {
4818 		/* XXXLAS: Find a way to live without this */
4819 		tp->ccv->curack = th_ack;
4820 		tp->ccv->labc = labc_to_use;
4821 		tp->ccv->flags |= CCF_USE_LOCAL_ABC;
4822 		CC_ALGO(tp)->ack_received(tp->ccv, type);
4823 	}
4824 	if (lgb) {
4825 		lgb->tlb_stackinfo.u_bbr.flex6 = tp->snd_cwnd;
4826 	}
4827 	if (rack->r_must_retran) {
4828 		if (SEQ_GEQ(th_ack, rack->r_ctl.rc_snd_max_at_rto)) {
4829 			/*
4830 			 * We now are beyond the rxt point so lets disable
4831 			 * the flag.
4832 			 */
4833 			rack->r_ctl.rc_out_at_rto = 0;
4834 			rack->r_must_retran = 0;
4835 		} else if ((prior_cwnd + ctf_fixed_maxseg(tp)) <= tp->snd_cwnd) {
4836 			/*
4837 			 * Only decrement the rc_out_at_rto if the cwnd advances
4838 			 * at least a whole segment. Otherwise next time the peer
4839 			 * acks, we won't be able to send this generaly happens
4840 			 * when we are in Congestion Avoidance.
4841 			 */
4842 			if (acked <= rack->r_ctl.rc_out_at_rto){
4843 				rack->r_ctl.rc_out_at_rto -= acked;
4844 			} else {
4845 				rack->r_ctl.rc_out_at_rto = 0;
4846 			}
4847 		}
4848 	}
4849 #ifdef STATS
4850 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, rack->r_ctl.cwnd_to_use);
4851 #endif
4852 	if (rack->r_ctl.rc_rack_largest_cwnd < rack->r_ctl.cwnd_to_use) {
4853 		rack->r_ctl.rc_rack_largest_cwnd = rack->r_ctl.cwnd_to_use;
4854 	}
4855 #ifdef NETFLIX_PEAKRATE
4856 	/* we enforce max peak rate if it is set and we are not pacing */
4857 	if ((rack->rc_always_pace == 0) &&
4858 	    tp->t_peakrate_thr &&
4859 	    (tp->snd_cwnd > tp->t_peakrate_thr)) {
4860 		tp->snd_cwnd = tp->t_peakrate_thr;
4861 	}
4862 #endif
4863 }
4864 
4865 static void
4866 tcp_rack_partialack(struct tcpcb *tp)
4867 {
4868 	struct tcp_rack *rack;
4869 
4870 	rack = (struct tcp_rack *)tp->t_fb_ptr;
4871 	INP_WLOCK_ASSERT(tp->t_inpcb);
4872 	/*
4873 	 * If we are doing PRR and have enough
4874 	 * room to send <or> we are pacing and prr
4875 	 * is disabled we will want to see if we
4876 	 * can send data (by setting r_wanted_output to
4877 	 * true).
4878 	 */
4879 	if ((rack->r_ctl.rc_prr_sndcnt > 0) ||
4880 	    rack->rack_no_prr)
4881 		rack->r_wanted_output = 1;
4882 }
4883 
4884 static void
4885 rack_post_recovery(struct tcpcb *tp, uint32_t th_ack)
4886 {
4887 	struct tcp_rack *rack;
4888 	uint32_t orig_cwnd;
4889 
4890 	orig_cwnd = tp->snd_cwnd;
4891 	INP_WLOCK_ASSERT(tp->t_inpcb);
4892 	rack = (struct tcp_rack *)tp->t_fb_ptr;
4893 	/* only alert CC if we alerted when we entered */
4894 	if (CC_ALGO(tp)->post_recovery != NULL) {
4895 		tp->ccv->curack = th_ack;
4896 		CC_ALGO(tp)->post_recovery(tp->ccv);
4897 		if (tp->snd_cwnd < tp->snd_ssthresh) {
4898 			/*
4899 			 * Rack has burst control and pacing
4900 			 * so lets not set this any lower than
4901 			 * snd_ssthresh per RFC-6582 (option 2).
4902 			 */
4903 			tp->snd_cwnd = tp->snd_ssthresh;
4904 		}
4905 	}
4906 	if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
4907 		union tcp_log_stackspecific log;
4908 		struct timeval tv;
4909 
4910 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
4911 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
4912 		log.u_bbr.flex1 = th_ack;
4913 		log.u_bbr.flex2 = tp->ccv->flags;
4914 		log.u_bbr.flex3 = tp->ccv->bytes_this_ack;
4915 		log.u_bbr.flex4 = tp->ccv->nsegs;
4916 		log.u_bbr.flex5 = V_tcp_abc_l_var;
4917 		log.u_bbr.flex6 = orig_cwnd;
4918 		log.u_bbr.flex7 = V_tcp_do_newsack;
4919 		log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
4920 		log.u_bbr.flex8 = 2;
4921 		tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
4922 			       0, &log, false, NULL, NULL, 0, &tv);
4923 	}
4924 	if ((rack->rack_no_prr == 0) &&
4925 	    (rack->no_prr_addback == 0) &&
4926 	    (rack->r_ctl.rc_prr_sndcnt > 0)) {
4927 		/*
4928 		 * Suck the next prr cnt back into cwnd, but
4929 		 * only do that if we are not application limited.
4930 		 */
4931 		if (ctf_outstanding(tp) <= sbavail(&(tp->t_inpcb->inp_socket->so_snd))) {
4932 			/*
4933 			 * We are allowed to add back to the cwnd the amount we did
4934 			 * not get out if:
4935 			 * a) no_prr_addback is off.
4936 			 * b) we are not app limited
4937 			 * c) we are doing prr
4938 			 * <and>
4939 			 * d) it is bounded by rack_prr_addbackmax (if addback is 0, then none).
4940 			 */
4941 			tp->snd_cwnd += min((ctf_fixed_maxseg(tp) * rack_prr_addbackmax),
4942 					    rack->r_ctl.rc_prr_sndcnt);
4943 		}
4944 		rack->r_ctl.rc_prr_sndcnt = 0;
4945 		rack_log_to_prr(rack, 1, 0);
4946 	}
4947 	rack_log_to_prr(rack, 14, orig_cwnd);
4948 	tp->snd_recover = tp->snd_una;
4949 	if (rack->r_ctl.dsack_persist) {
4950 		rack->r_ctl.dsack_persist--;
4951 		if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
4952 			rack->r_ctl.num_dsack = 0;
4953 		}
4954 		rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
4955 	}
4956 	EXIT_RECOVERY(tp->t_flags);
4957 }
4958 
4959 static void
4960 rack_cong_signal(struct tcpcb *tp, uint32_t type, uint32_t ack)
4961 {
4962 	struct tcp_rack *rack;
4963 	uint32_t ssthresh_enter, cwnd_enter, in_rec_at_entry, orig_cwnd;
4964 
4965 	INP_WLOCK_ASSERT(tp->t_inpcb);
4966 #ifdef STATS
4967 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type);
4968 #endif
4969 	if (IN_RECOVERY(tp->t_flags) == 0) {
4970 		in_rec_at_entry = 0;
4971 		ssthresh_enter = tp->snd_ssthresh;
4972 		cwnd_enter = tp->snd_cwnd;
4973 	} else
4974 		in_rec_at_entry = 1;
4975 	rack = (struct tcp_rack *)tp->t_fb_ptr;
4976 	switch (type) {
4977 	case CC_NDUPACK:
4978 		tp->t_flags &= ~TF_WASFRECOVERY;
4979 		tp->t_flags &= ~TF_WASCRECOVERY;
4980 		if (!IN_FASTRECOVERY(tp->t_flags)) {
4981 			rack->r_ctl.rc_prr_delivered = 0;
4982 			rack->r_ctl.rc_prr_out = 0;
4983 			if (rack->rack_no_prr == 0) {
4984 				rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
4985 				rack_log_to_prr(rack, 2, in_rec_at_entry);
4986 			}
4987 			rack->r_ctl.rc_prr_recovery_fs = tp->snd_max - tp->snd_una;
4988 			tp->snd_recover = tp->snd_max;
4989 			if (tp->t_flags2 & TF2_ECN_PERMIT)
4990 				tp->t_flags2 |= TF2_ECN_SND_CWR;
4991 		}
4992 		break;
4993 	case CC_ECN:
4994 		if (!IN_CONGRECOVERY(tp->t_flags) ||
4995 		    /*
4996 		     * Allow ECN reaction on ACK to CWR, if
4997 		     * that data segment was also CE marked.
4998 		     */
4999 		    SEQ_GEQ(ack, tp->snd_recover)) {
5000 			EXIT_CONGRECOVERY(tp->t_flags);
5001 			KMOD_TCPSTAT_INC(tcps_ecn_rcwnd);
5002 			tp->snd_recover = tp->snd_max + 1;
5003 			if (tp->t_flags2 & TF2_ECN_PERMIT)
5004 				tp->t_flags2 |= TF2_ECN_SND_CWR;
5005 		}
5006 		break;
5007 	case CC_RTO:
5008 		tp->t_dupacks = 0;
5009 		tp->t_bytes_acked = 0;
5010 		EXIT_RECOVERY(tp->t_flags);
5011 		tp->snd_ssthresh = max(2, min(tp->snd_wnd, rack->r_ctl.cwnd_to_use) / 2 /
5012 		    ctf_fixed_maxseg(tp)) * ctf_fixed_maxseg(tp);
5013 		orig_cwnd = tp->snd_cwnd;
5014 		tp->snd_cwnd = ctf_fixed_maxseg(tp);
5015 		rack_log_to_prr(rack, 16, orig_cwnd);
5016 		if (tp->t_flags2 & TF2_ECN_PERMIT)
5017 			tp->t_flags2 |= TF2_ECN_SND_CWR;
5018 		break;
5019 	case CC_RTO_ERR:
5020 		KMOD_TCPSTAT_INC(tcps_sndrexmitbad);
5021 		/* RTO was unnecessary, so reset everything. */
5022 		tp->snd_cwnd = tp->snd_cwnd_prev;
5023 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
5024 		tp->snd_recover = tp->snd_recover_prev;
5025 		if (tp->t_flags & TF_WASFRECOVERY) {
5026 			ENTER_FASTRECOVERY(tp->t_flags);
5027 			tp->t_flags &= ~TF_WASFRECOVERY;
5028 		}
5029 		if (tp->t_flags & TF_WASCRECOVERY) {
5030 			ENTER_CONGRECOVERY(tp->t_flags);
5031 			tp->t_flags &= ~TF_WASCRECOVERY;
5032 		}
5033 		tp->snd_nxt = tp->snd_max;
5034 		tp->t_badrxtwin = 0;
5035 		break;
5036 	}
5037 	if ((CC_ALGO(tp)->cong_signal != NULL)  &&
5038 	    (type != CC_RTO)){
5039 		tp->ccv->curack = ack;
5040 		CC_ALGO(tp)->cong_signal(tp->ccv, type);
5041 	}
5042 	if ((in_rec_at_entry == 0) && IN_RECOVERY(tp->t_flags)) {
5043 		rack_log_to_prr(rack, 15, cwnd_enter);
5044 		rack->r_ctl.dsack_byte_cnt = 0;
5045 		rack->r_ctl.retran_during_recovery = 0;
5046 		rack->r_ctl.rc_cwnd_at_erec = cwnd_enter;
5047 		rack->r_ctl.rc_ssthresh_at_erec = ssthresh_enter;
5048 		rack->r_ent_rec_ns = 1;
5049 	}
5050 }
5051 
5052 static inline void
5053 rack_cc_after_idle(struct tcp_rack *rack, struct tcpcb *tp)
5054 {
5055 	uint32_t i_cwnd;
5056 
5057 	INP_WLOCK_ASSERT(tp->t_inpcb);
5058 
5059 #ifdef NETFLIX_STATS
5060 	KMOD_TCPSTAT_INC(tcps_idle_restarts);
5061 	if (tp->t_state == TCPS_ESTABLISHED)
5062 		KMOD_TCPSTAT_INC(tcps_idle_estrestarts);
5063 #endif
5064 	if (CC_ALGO(tp)->after_idle != NULL)
5065 		CC_ALGO(tp)->after_idle(tp->ccv);
5066 
5067 	if (tp->snd_cwnd == 1)
5068 		i_cwnd = tp->t_maxseg;		/* SYN(-ACK) lost */
5069 	else
5070 		i_cwnd = rc_init_window(rack);
5071 
5072 	/*
5073 	 * Being idle is no different than the initial window. If the cc
5074 	 * clamps it down below the initial window raise it to the initial
5075 	 * window.
5076 	 */
5077 	if (tp->snd_cwnd < i_cwnd) {
5078 		tp->snd_cwnd = i_cwnd;
5079 	}
5080 }
5081 
5082 /*
5083  * Indicate whether this ack should be delayed.  We can delay the ack if
5084  * following conditions are met:
5085  *	- There is no delayed ack timer in progress.
5086  *	- Our last ack wasn't a 0-sized window. We never want to delay
5087  *	  the ack that opens up a 0-sized window.
5088  *	- LRO wasn't used for this segment. We make sure by checking that the
5089  *	  segment size is not larger than the MSS.
5090  *	- Delayed acks are enabled or this is a half-synchronized T/TCP
5091  *	  connection.
5092  */
5093 #define DELAY_ACK(tp, tlen)			 \
5094 	(((tp->t_flags & TF_RXWIN0SENT) == 0) && \
5095 	((tp->t_flags & TF_DELACK) == 0) &&	 \
5096 	(tlen <= tp->t_maxseg) &&		 \
5097 	(tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN)))
5098 
5099 static struct rack_sendmap *
5100 rack_find_lowest_rsm(struct tcp_rack *rack)
5101 {
5102 	struct rack_sendmap *rsm;
5103 
5104 	/*
5105 	 * Walk the time-order transmitted list looking for an rsm that is
5106 	 * not acked. This will be the one that was sent the longest time
5107 	 * ago that is still outstanding.
5108 	 */
5109 	TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
5110 		if (rsm->r_flags & RACK_ACKED) {
5111 			continue;
5112 		}
5113 		goto finish;
5114 	}
5115 finish:
5116 	return (rsm);
5117 }
5118 
5119 static struct rack_sendmap *
5120 rack_find_high_nonack(struct tcp_rack *rack, struct rack_sendmap *rsm)
5121 {
5122 	struct rack_sendmap *prsm;
5123 
5124 	/*
5125 	 * Walk the sequence order list backward until we hit and arrive at
5126 	 * the highest seq not acked. In theory when this is called it
5127 	 * should be the last segment (which it was not).
5128 	 */
5129 	counter_u64_add(rack_find_high, 1);
5130 	prsm = rsm;
5131 	RB_FOREACH_REVERSE_FROM(prsm, rack_rb_tree_head, rsm) {
5132 		if (prsm->r_flags & (RACK_ACKED | RACK_HAS_FIN)) {
5133 			continue;
5134 		}
5135 		return (prsm);
5136 	}
5137 	return (NULL);
5138 }
5139 
5140 static uint32_t
5141 rack_calc_thresh_rack(struct tcp_rack *rack, uint32_t srtt, uint32_t cts)
5142 {
5143 	int32_t lro;
5144 	uint32_t thresh;
5145 
5146 	/*
5147 	 * lro is the flag we use to determine if we have seen reordering.
5148 	 * If it gets set we have seen reordering. The reorder logic either
5149 	 * works in one of two ways:
5150 	 *
5151 	 * If reorder-fade is configured, then we track the last time we saw
5152 	 * re-ordering occur. If we reach the point where enough time as
5153 	 * passed we no longer consider reordering has occuring.
5154 	 *
5155 	 * Or if reorder-face is 0, then once we see reordering we consider
5156 	 * the connection to alway be subject to reordering and just set lro
5157 	 * to 1.
5158 	 *
5159 	 * In the end if lro is non-zero we add the extra time for
5160 	 * reordering in.
5161 	 */
5162 	if (srtt == 0)
5163 		srtt = 1;
5164 	if (rack->r_ctl.rc_reorder_ts) {
5165 		if (rack->r_ctl.rc_reorder_fade) {
5166 			if (SEQ_GEQ(cts, rack->r_ctl.rc_reorder_ts)) {
5167 				lro = cts - rack->r_ctl.rc_reorder_ts;
5168 				if (lro == 0) {
5169 					/*
5170 					 * No time as passed since the last
5171 					 * reorder, mark it as reordering.
5172 					 */
5173 					lro = 1;
5174 				}
5175 			} else {
5176 				/* Negative time? */
5177 				lro = 0;
5178 			}
5179 			if (lro > rack->r_ctl.rc_reorder_fade) {
5180 				/* Turn off reordering seen too */
5181 				rack->r_ctl.rc_reorder_ts = 0;
5182 				lro = 0;
5183 			}
5184 		} else {
5185 			/* Reodering does not fade */
5186 			lro = 1;
5187 		}
5188 	} else {
5189 		lro = 0;
5190 	}
5191 	if (rack->rc_rack_tmr_std_based == 0) {
5192 		thresh = srtt + rack->r_ctl.rc_pkt_delay;
5193 	} else {
5194 		/* Standards based pkt-delay is 1/4 srtt */
5195 		thresh = srtt +  (srtt >> 2);
5196 	}
5197 	if (lro && (rack->rc_rack_tmr_std_based == 0)) {
5198 		/* It must be set, if not you get 1/4 rtt */
5199 		if (rack->r_ctl.rc_reorder_shift)
5200 			thresh += (srtt >> rack->r_ctl.rc_reorder_shift);
5201 		else
5202 			thresh += (srtt >> 2);
5203 	}
5204 	if (rack->rc_rack_use_dsack &&
5205 	    lro &&
5206 	    (rack->r_ctl.num_dsack > 0)) {
5207 		/*
5208 		 * We only increase the reordering window if we
5209 		 * have seen reordering <and> we have a DSACK count.
5210 		 */
5211 		thresh += rack->r_ctl.num_dsack * (srtt >> 2);
5212 		rack_log_dsack_event(rack, 4, __LINE__, srtt, thresh);
5213 	}
5214 	/* SRTT * 2 is the ceiling */
5215 	if (thresh > (srtt * 2)) {
5216 		thresh = srtt * 2;
5217 	}
5218 	/* And we don't want it above the RTO max either */
5219 	if (thresh > rack_rto_max) {
5220 		thresh = rack_rto_max;
5221 	}
5222 	rack_log_dsack_event(rack, 6, __LINE__, srtt, thresh);
5223 	return (thresh);
5224 }
5225 
5226 static uint32_t
5227 rack_calc_thresh_tlp(struct tcpcb *tp, struct tcp_rack *rack,
5228 		     struct rack_sendmap *rsm, uint32_t srtt)
5229 {
5230 	struct rack_sendmap *prsm;
5231 	uint32_t thresh, len;
5232 	int segsiz;
5233 
5234 	if (srtt == 0)
5235 		srtt = 1;
5236 	if (rack->r_ctl.rc_tlp_threshold)
5237 		thresh = srtt + (srtt / rack->r_ctl.rc_tlp_threshold);
5238 	else
5239 		thresh = (srtt * 2);
5240 
5241 	/* Get the previous sent packet, if any */
5242 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
5243 	counter_u64_add(rack_enter_tlp_calc, 1);
5244 	len = rsm->r_end - rsm->r_start;
5245 	if (rack->rack_tlp_threshold_use == TLP_USE_ID) {
5246 		/* Exactly like the ID */
5247 		if (((tp->snd_max - tp->snd_una) - rack->r_ctl.rc_sacked + rack->r_ctl.rc_holes_rxt) <= segsiz) {
5248 			uint32_t alt_thresh;
5249 			/*
5250 			 * Compensate for delayed-ack with the d-ack time.
5251 			 */
5252 			counter_u64_add(rack_used_tlpmethod, 1);
5253 			alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
5254 			if (alt_thresh > thresh)
5255 				thresh = alt_thresh;
5256 		}
5257 	} else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_ONE) {
5258 		/* 2.1 behavior */
5259 		prsm = TAILQ_PREV(rsm, rack_head, r_tnext);
5260 		if (prsm && (len <= segsiz)) {
5261 			/*
5262 			 * Two packets outstanding, thresh should be (2*srtt) +
5263 			 * possible inter-packet delay (if any).
5264 			 */
5265 			uint32_t inter_gap = 0;
5266 			int idx, nidx;
5267 
5268 			counter_u64_add(rack_used_tlpmethod, 1);
5269 			idx = rsm->r_rtr_cnt - 1;
5270 			nidx = prsm->r_rtr_cnt - 1;
5271 			if (rsm->r_tim_lastsent[nidx] >= prsm->r_tim_lastsent[idx]) {
5272 				/* Yes it was sent later (or at the same time) */
5273 				inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx];
5274 			}
5275 			thresh += inter_gap;
5276 		} else if (len <= segsiz) {
5277 			/*
5278 			 * Possibly compensate for delayed-ack.
5279 			 */
5280 			uint32_t alt_thresh;
5281 
5282 			counter_u64_add(rack_used_tlpmethod2, 1);
5283 			alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
5284 			if (alt_thresh > thresh)
5285 				thresh = alt_thresh;
5286 		}
5287 	} else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_TWO) {
5288 		/* 2.2 behavior */
5289 		if (len <= segsiz) {
5290 			uint32_t alt_thresh;
5291 			/*
5292 			 * Compensate for delayed-ack with the d-ack time.
5293 			 */
5294 			counter_u64_add(rack_used_tlpmethod, 1);
5295 			alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
5296 			if (alt_thresh > thresh)
5297 				thresh = alt_thresh;
5298 		}
5299 	}
5300 	/* Not above an RTO */
5301 	if (thresh > tp->t_rxtcur) {
5302 		thresh = tp->t_rxtcur;
5303 	}
5304 	/* Not above a RTO max */
5305 	if (thresh > rack_rto_max) {
5306 		thresh = rack_rto_max;
5307 	}
5308 	/* Apply user supplied min TLP */
5309 	if (thresh < rack_tlp_min) {
5310 		thresh = rack_tlp_min;
5311 	}
5312 	return (thresh);
5313 }
5314 
5315 static uint32_t
5316 rack_grab_rtt(struct tcpcb *tp, struct tcp_rack *rack)
5317 {
5318 	/*
5319 	 * We want the rack_rtt which is the
5320 	 * last rtt we measured. However if that
5321 	 * does not exist we fallback to the srtt (which
5322 	 * we probably will never do) and then as a last
5323 	 * resort we use RACK_INITIAL_RTO if no srtt is
5324 	 * yet set.
5325 	 */
5326 	if (rack->rc_rack_rtt)
5327 		return (rack->rc_rack_rtt);
5328 	else if (tp->t_srtt == 0)
5329 		return (RACK_INITIAL_RTO);
5330 	return (tp->t_srtt);
5331 }
5332 
5333 static struct rack_sendmap *
5334 rack_check_recovery_mode(struct tcpcb *tp, uint32_t tsused)
5335 {
5336 	/*
5337 	 * Check to see that we don't need to fall into recovery. We will
5338 	 * need to do so if our oldest transmit is past the time we should
5339 	 * have had an ack.
5340 	 */
5341 	struct tcp_rack *rack;
5342 	struct rack_sendmap *rsm;
5343 	int32_t idx;
5344 	uint32_t srtt, thresh;
5345 
5346 	rack = (struct tcp_rack *)tp->t_fb_ptr;
5347 	if (RB_EMPTY(&rack->r_ctl.rc_mtree)) {
5348 		return (NULL);
5349 	}
5350 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
5351 	if (rsm == NULL)
5352 		return (NULL);
5353 
5354 	if (rsm->r_flags & RACK_ACKED) {
5355 		rsm = rack_find_lowest_rsm(rack);
5356 		if (rsm == NULL)
5357 			return (NULL);
5358 	}
5359 	idx = rsm->r_rtr_cnt - 1;
5360 	srtt = rack_grab_rtt(tp, rack);
5361 	thresh = rack_calc_thresh_rack(rack, srtt, tsused);
5362 	if (TSTMP_LT(tsused, ((uint32_t)rsm->r_tim_lastsent[idx]))) {
5363 		return (NULL);
5364 	}
5365 	if ((tsused - ((uint32_t)rsm->r_tim_lastsent[idx])) < thresh) {
5366 		return (NULL);
5367 	}
5368 	/* Ok if we reach here we are over-due and this guy can be sent */
5369 	if (IN_RECOVERY(tp->t_flags) == 0) {
5370 		/*
5371 		 * For the one that enters us into recovery record undo
5372 		 * info.
5373 		 */
5374 		rack->r_ctl.rc_rsm_start = rsm->r_start;
5375 		rack->r_ctl.rc_cwnd_at = tp->snd_cwnd;
5376 		rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh;
5377 	}
5378 	rack_cong_signal(tp, CC_NDUPACK, tp->snd_una);
5379 	return (rsm);
5380 }
5381 
5382 static uint32_t
5383 rack_get_persists_timer_val(struct tcpcb *tp, struct tcp_rack *rack)
5384 {
5385 	int32_t t;
5386 	int32_t tt;
5387 	uint32_t ret_val;
5388 
5389 	t = (tp->t_srtt + (tp->t_rttvar << 2));
5390 	RACK_TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
5391  	    rack_persist_min, rack_persist_max, rack->r_ctl.timer_slop);
5392 	rack->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT;
5393 	ret_val = (uint32_t)tt;
5394 	return (ret_val);
5395 }
5396 
5397 static uint32_t
5398 rack_timer_start(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int sup_rack)
5399 {
5400 	/*
5401 	 * Start the FR timer, we do this based on getting the first one in
5402 	 * the rc_tmap. Note that if its NULL we must stop the timer. in all
5403 	 * events we need to stop the running timer (if its running) before
5404 	 * starting the new one.
5405 	 */
5406 	uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse;
5407 	uint32_t srtt_cur;
5408 	int32_t idx;
5409 	int32_t is_tlp_timer = 0;
5410 	struct rack_sendmap *rsm;
5411 
5412 	if (rack->t_timers_stopped) {
5413 		/* All timers have been stopped none are to run */
5414 		return (0);
5415 	}
5416 	if (rack->rc_in_persist) {
5417 		/* We can't start any timer in persists */
5418 		return (rack_get_persists_timer_val(tp, rack));
5419 	}
5420 	rack->rc_on_min_to = 0;
5421 	if ((tp->t_state < TCPS_ESTABLISHED) ||
5422 	    ((tp->t_flags & TF_SACK_PERMIT) == 0)) {
5423 		goto activate_rxt;
5424 	}
5425 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
5426 	if ((rsm == NULL) || sup_rack) {
5427 		/* Nothing on the send map or no rack */
5428 activate_rxt:
5429 		time_since_sent = 0;
5430 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
5431 		if (rsm) {
5432 			/*
5433 			 * Should we discount the RTX timer any?
5434 			 *
5435 			 * We want to discount it the smallest amount.
5436 			 * If a timer (Rack/TLP or RXT) has gone off more
5437 			 * recently thats the discount we want to use (now - timer time).
5438 			 * If the retransmit of the oldest packet was more recent then
5439 			 * we want to use that (now - oldest-packet-last_transmit_time).
5440 			 *
5441 			 */
5442 			idx = rsm->r_rtr_cnt - 1;
5443 			if (TSTMP_GEQ(rack->r_ctl.rc_tlp_rxt_last_time, ((uint32_t)rsm->r_tim_lastsent[idx])))
5444 				tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time;
5445 			else
5446 				tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx];
5447 			if (TSTMP_GT(cts, tstmp_touse))
5448 			    time_since_sent = cts - tstmp_touse;
5449 		}
5450 		if (SEQ_LT(tp->snd_una, tp->snd_max) || sbavail(&(tp->t_inpcb->inp_socket->so_snd))) {
5451 			rack->r_ctl.rc_hpts_flags |= PACE_TMR_RXT;
5452 			to = tp->t_rxtcur;
5453 			if (to > time_since_sent)
5454 				to -= time_since_sent;
5455 			else
5456 				to = rack->r_ctl.rc_min_to;
5457 			if (to == 0)
5458 				to = 1;
5459 			/* Special case for KEEPINIT */
5460 			if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) &&
5461 			    (TP_KEEPINIT(tp) != 0) &&
5462 			    rsm) {
5463 				/*
5464 				 * We have to put a ceiling on the rxt timer
5465 				 * of the keep-init timeout.
5466 				 */
5467 				uint32_t max_time, red;
5468 
5469 				max_time = TICKS_2_USEC(TP_KEEPINIT(tp));
5470 				if (TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) {
5471 					red = (cts - (uint32_t)rsm->r_tim_lastsent[0]);
5472 					if (red < max_time)
5473 						max_time -= red;
5474 					else
5475 						max_time = 1;
5476 				}
5477 				/* Reduce timeout to the keep value if needed */
5478 				if (max_time < to)
5479 					to = max_time;
5480 			}
5481 			return (to);
5482 		}
5483 		return (0);
5484 	}
5485 	if (rsm->r_flags & RACK_ACKED) {
5486 		rsm = rack_find_lowest_rsm(rack);
5487 		if (rsm == NULL) {
5488 			/* No lowest? */
5489 			goto activate_rxt;
5490 		}
5491 	}
5492 	if (rack->sack_attack_disable) {
5493 		/*
5494 		 * We don't want to do
5495 		 * any TLP's if you are an attacker.
5496 		 * Though if you are doing what
5497 		 * is expected you may still have
5498 		 * SACK-PASSED marks.
5499 		 */
5500 		goto activate_rxt;
5501 	}
5502 	/* Convert from ms to usecs */
5503 	if ((rsm->r_flags & RACK_SACK_PASSED) || (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
5504 		if ((tp->t_flags & TF_SENTFIN) &&
5505 		    ((tp->snd_max - tp->snd_una) == 1) &&
5506 		    (rsm->r_flags & RACK_HAS_FIN)) {
5507 			/*
5508 			 * We don't start a rack timer if all we have is a
5509 			 * FIN outstanding.
5510 			 */
5511 			goto activate_rxt;
5512 		}
5513 		if ((rack->use_rack_rr == 0) &&
5514 		    (IN_FASTRECOVERY(tp->t_flags)) &&
5515 		    (rack->rack_no_prr == 0) &&
5516 		     (rack->r_ctl.rc_prr_sndcnt  < ctf_fixed_maxseg(tp))) {
5517 			/*
5518 			 * We are not cheating, in recovery  and
5519 			 * not enough ack's to yet get our next
5520 			 * retransmission out.
5521 			 *
5522 			 * Note that classified attackers do not
5523 			 * get to use the rack-cheat.
5524 			 */
5525 			goto activate_tlp;
5526 		}
5527 		srtt = rack_grab_rtt(tp, rack);
5528 		thresh = rack_calc_thresh_rack(rack, srtt, cts);
5529 		idx = rsm->r_rtr_cnt - 1;
5530 		exp = ((uint32_t)rsm->r_tim_lastsent[idx]) + thresh;
5531 		if (SEQ_GEQ(exp, cts)) {
5532 			to = exp - cts;
5533 			if (to < rack->r_ctl.rc_min_to) {
5534 				to = rack->r_ctl.rc_min_to;
5535 				if (rack->r_rr_config == 3)
5536 					rack->rc_on_min_to = 1;
5537 			}
5538 		} else {
5539 			to = rack->r_ctl.rc_min_to;
5540 			if (rack->r_rr_config == 3)
5541 				rack->rc_on_min_to = 1;
5542 		}
5543 	} else {
5544 		/* Ok we need to do a TLP not RACK */
5545 activate_tlp:
5546 		if ((rack->rc_tlp_in_progress != 0) &&
5547 		    (rack->r_ctl.rc_tlp_cnt_out >= rack_tlp_limit)) {
5548 			/*
5549 			 * The previous send was a TLP and we have sent
5550 			 * N TLP's without sending new data.
5551 			 */
5552 			goto activate_rxt;
5553 		}
5554 		rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
5555 		if (rsm == NULL) {
5556 			/* We found no rsm to TLP with. */
5557 			goto activate_rxt;
5558 		}
5559 		if (rsm->r_flags & RACK_HAS_FIN) {
5560 			/* If its a FIN we dont do TLP */
5561 			rsm = NULL;
5562 			goto activate_rxt;
5563 		}
5564 		idx = rsm->r_rtr_cnt - 1;
5565 		time_since_sent = 0;
5566 		if (TSTMP_GEQ(((uint32_t)rsm->r_tim_lastsent[idx]), rack->r_ctl.rc_tlp_rxt_last_time))
5567 			tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx];
5568 		else
5569 			tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time;
5570 		if (TSTMP_GT(cts, tstmp_touse))
5571 		    time_since_sent = cts - tstmp_touse;
5572 		is_tlp_timer = 1;
5573 		if (tp->t_srtt) {
5574 			if ((rack->rc_srtt_measure_made == 0) &&
5575 			    (tp->t_srtt == 1)) {
5576 				/*
5577 				 * If another stack as run and set srtt to 1,
5578 				 * then the srtt was 0, so lets use the initial.
5579 				 */
5580 				srtt = RACK_INITIAL_RTO;
5581 			} else {
5582 				srtt_cur = tp->t_srtt;
5583 				srtt = srtt_cur;
5584 			}
5585 		} else
5586 			srtt = RACK_INITIAL_RTO;
5587 		/*
5588 		 * If the SRTT is not keeping up and the
5589 		 * rack RTT has spiked we want to use
5590 		 * the last RTT not the smoothed one.
5591 		 */
5592 		if (rack_tlp_use_greater &&
5593 		    tp->t_srtt &&
5594 		    (srtt < rack_grab_rtt(tp, rack))) {
5595 			srtt = rack_grab_rtt(tp, rack);
5596 		}
5597 		thresh = rack_calc_thresh_tlp(tp, rack, rsm, srtt);
5598 		if (thresh > time_since_sent) {
5599 			to = thresh - time_since_sent;
5600 		} else {
5601 			to = rack->r_ctl.rc_min_to;
5602 			rack_log_alt_to_to_cancel(rack,
5603 						  thresh,		/* flex1 */
5604 						  time_since_sent,	/* flex2 */
5605 						  tstmp_touse,		/* flex3 */
5606 						  rack->r_ctl.rc_tlp_rxt_last_time, /* flex4 */
5607 						  (uint32_t)rsm->r_tim_lastsent[idx],
5608 						  srtt,
5609 						  idx, 99);
5610 		}
5611 		if (to < rack_tlp_min) {
5612 			to = rack_tlp_min;
5613 		}
5614 		if (to > TICKS_2_USEC(TCPTV_REXMTMAX)) {
5615 			/*
5616 			 * If the TLP time works out to larger than the max
5617 			 * RTO lets not do TLP.. just RTO.
5618 			 */
5619 			goto activate_rxt;
5620 		}
5621 	}
5622 	if (is_tlp_timer == 0) {
5623 		rack->r_ctl.rc_hpts_flags |= PACE_TMR_RACK;
5624 	} else {
5625 		rack->r_ctl.rc_hpts_flags |= PACE_TMR_TLP;
5626 	}
5627 	if (to == 0)
5628 		to = 1;
5629 	return (to);
5630 }
5631 
5632 static void
5633 rack_enter_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
5634 {
5635 	if (rack->rc_in_persist == 0) {
5636 		if (tp->t_flags & TF_GPUTINPROG) {
5637 			/*
5638 			 * Stop the goodput now, the calling of the
5639 			 * measurement function clears the flag.
5640 			 */
5641 			rack_do_goodput_measurement(tp, rack, tp->snd_una, __LINE__,
5642 						    RACK_QUALITY_PERSIST);
5643 		}
5644 #ifdef NETFLIX_SHARED_CWND
5645 		if (rack->r_ctl.rc_scw) {
5646 			tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
5647 			rack->rack_scwnd_is_idle = 1;
5648 		}
5649 #endif
5650 		rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
5651 		if (rack->r_ctl.rc_went_idle_time == 0)
5652 			rack->r_ctl.rc_went_idle_time = 1;
5653 		rack_timer_cancel(tp, rack, cts, __LINE__);
5654 		rack->r_ctl.persist_lost_ends = 0;
5655 		rack->probe_not_answered = 0;
5656 		rack->forced_ack = 0;
5657 		tp->t_rxtshift = 0;
5658 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
5659 			      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
5660 		rack->rc_in_persist = 1;
5661 	}
5662 }
5663 
5664 static void
5665 rack_exit_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
5666 {
5667 	if (tcp_in_hpts(rack->rc_inp)) {
5668 		tcp_hpts_remove(rack->rc_inp);
5669 		rack->r_ctl.rc_hpts_flags = 0;
5670 	}
5671 #ifdef NETFLIX_SHARED_CWND
5672 	if (rack->r_ctl.rc_scw) {
5673 		tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
5674 		rack->rack_scwnd_is_idle = 0;
5675 	}
5676 #endif
5677 	if (rack->rc_gp_dyn_mul &&
5678 	    (rack->use_fixed_rate == 0) &&
5679 	    (rack->rc_always_pace)) {
5680 		/*
5681 		 * Do we count this as if a probe-rtt just
5682 		 * finished?
5683 		 */
5684 		uint32_t time_idle, idle_min;
5685 
5686 		time_idle = tcp_get_usecs(NULL) - rack->r_ctl.rc_went_idle_time;
5687 		idle_min = rack_min_probertt_hold;
5688 		if (rack_probertt_gpsrtt_cnt_div) {
5689 			uint64_t extra;
5690 			extra = (uint64_t)rack->r_ctl.rc_gp_srtt *
5691 				(uint64_t)rack_probertt_gpsrtt_cnt_mul;
5692 			extra /= (uint64_t)rack_probertt_gpsrtt_cnt_div;
5693 			idle_min += (uint32_t)extra;
5694 		}
5695 		if (time_idle >= idle_min) {
5696 			/* Yes, we count it as a probe-rtt. */
5697 			uint32_t us_cts;
5698 
5699 			us_cts = tcp_get_usecs(NULL);
5700 			if (rack->in_probe_rtt == 0) {
5701 				rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
5702 				rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts;
5703 				rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts;
5704 				rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts;
5705 			} else {
5706 				rack_exit_probertt(rack, us_cts);
5707 			}
5708 		}
5709 	}
5710 	rack->rc_in_persist = 0;
5711 	rack->r_ctl.rc_went_idle_time = 0;
5712 	tp->t_rxtshift = 0;
5713 	RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
5714 	   rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
5715 	rack->r_ctl.rc_agg_delayed = 0;
5716 	rack->r_early = 0;
5717 	rack->r_late = 0;
5718 	rack->r_ctl.rc_agg_early = 0;
5719 }
5720 
5721 static void
5722 rack_log_hpts_diag(struct tcp_rack *rack, uint32_t cts,
5723 		   struct hpts_diag *diag, struct timeval *tv)
5724 {
5725 	if (rack_verbose_logging && rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
5726 		union tcp_log_stackspecific log;
5727 
5728 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5729 		log.u_bbr.flex1 = diag->p_nxt_slot;
5730 		log.u_bbr.flex2 = diag->p_cur_slot;
5731 		log.u_bbr.flex3 = diag->slot_req;
5732 		log.u_bbr.flex4 = diag->inp_hptsslot;
5733 		log.u_bbr.flex5 = diag->slot_remaining;
5734 		log.u_bbr.flex6 = diag->need_new_to;
5735 		log.u_bbr.flex7 = diag->p_hpts_active;
5736 		log.u_bbr.flex8 = diag->p_on_min_sleep;
5737 		/* Hijack other fields as needed */
5738 		log.u_bbr.epoch = diag->have_slept;
5739 		log.u_bbr.lt_epoch = diag->yet_to_sleep;
5740 		log.u_bbr.pkts_out = diag->co_ret;
5741 		log.u_bbr.applimited = diag->hpts_sleep_time;
5742 		log.u_bbr.delivered = diag->p_prev_slot;
5743 		log.u_bbr.inflight = diag->p_runningslot;
5744 		log.u_bbr.bw_inuse = diag->wheel_slot;
5745 		log.u_bbr.rttProp = diag->wheel_cts;
5746 		log.u_bbr.timeStamp = cts;
5747 		log.u_bbr.delRate = diag->maxslots;
5748 		log.u_bbr.cur_del_rate = diag->p_curtick;
5749 		log.u_bbr.cur_del_rate <<= 32;
5750 		log.u_bbr.cur_del_rate |= diag->p_lasttick;
5751 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
5752 		    &rack->rc_inp->inp_socket->so_rcv,
5753 		    &rack->rc_inp->inp_socket->so_snd,
5754 		    BBR_LOG_HPTSDIAG, 0,
5755 		    0, &log, false, tv);
5756 	}
5757 
5758 }
5759 
5760 static void
5761 rack_log_wakeup(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb, uint32_t len, int type)
5762 {
5763 	if (rack_verbose_logging && rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
5764 		union tcp_log_stackspecific log;
5765 		struct timeval tv;
5766 
5767 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5768 		log.u_bbr.flex1 = sb->sb_flags;
5769 		log.u_bbr.flex2 = len;
5770 		log.u_bbr.flex3 = sb->sb_state;
5771 		log.u_bbr.flex8 = type;
5772 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5773 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
5774 		    &rack->rc_inp->inp_socket->so_rcv,
5775 		    &rack->rc_inp->inp_socket->so_snd,
5776 		    TCP_LOG_SB_WAKE, 0,
5777 		    len, &log, false, &tv);
5778 	}
5779 }
5780 
5781 static void
5782 rack_start_hpts_timer(struct tcp_rack *rack, struct tcpcb *tp, uint32_t cts,
5783       int32_t slot, uint32_t tot_len_this_send, int sup_rack)
5784 {
5785 	struct hpts_diag diag;
5786 	struct inpcb *inp;
5787 	struct timeval tv;
5788 	uint32_t delayed_ack = 0;
5789 	uint32_t hpts_timeout;
5790 	uint32_t entry_slot = slot;
5791 	uint8_t stopped;
5792 	uint32_t left = 0;
5793 	uint32_t us_cts;
5794 
5795 	inp = tp->t_inpcb;
5796 	if ((tp->t_state == TCPS_CLOSED) ||
5797 	    (tp->t_state == TCPS_LISTEN)) {
5798 		return;
5799 	}
5800 	if (tcp_in_hpts(inp)) {
5801 		/* Already on the pacer */
5802 		return;
5803 	}
5804 	stopped = rack->rc_tmr_stopped;
5805 	if (stopped && TSTMP_GT(rack->r_ctl.rc_timer_exp, cts)) {
5806 		left = rack->r_ctl.rc_timer_exp - cts;
5807 	}
5808 	rack->r_ctl.rc_timer_exp = 0;
5809 	rack->r_ctl.rc_hpts_flags = 0;
5810 	us_cts = tcp_get_usecs(&tv);
5811 	/* Now early/late accounting */
5812 	rack_log_pacing_delay_calc(rack, entry_slot, slot, 0, 0, 0, 26, __LINE__, NULL, 0);
5813 	if (rack->r_early && (rack->rc_ack_can_sendout_data == 0)) {
5814 		/*
5815 		 * We have a early carry over set,
5816 		 * we can always add more time so we
5817 		 * can always make this compensation.
5818 		 *
5819 		 * Note if ack's are allowed to wake us do not
5820 		 * penalize the next timer for being awoke
5821 		 * by an ack aka the rc_agg_early (non-paced mode).
5822 		 */
5823 		slot += rack->r_ctl.rc_agg_early;
5824 		rack->r_early = 0;
5825 		rack->r_ctl.rc_agg_early = 0;
5826 	}
5827 	if (rack->r_late) {
5828 		/*
5829 		 * This is harder, we can
5830 		 * compensate some but it
5831 		 * really depends on what
5832 		 * the current pacing time is.
5833 		 */
5834 		if (rack->r_ctl.rc_agg_delayed >= slot) {
5835 			/*
5836 			 * We can't compensate for it all.
5837 			 * And we have to have some time
5838 			 * on the clock. We always have a min
5839 			 * 10 slots (10 x 10 i.e. 100 usecs).
5840 			 */
5841 			if (slot <= HPTS_TICKS_PER_SLOT) {
5842 				/* We gain delay */
5843 				rack->r_ctl.rc_agg_delayed += (HPTS_TICKS_PER_SLOT - slot);
5844 				slot = HPTS_TICKS_PER_SLOT;
5845 			} else {
5846 				/* We take off some */
5847 				rack->r_ctl.rc_agg_delayed -= (slot - HPTS_TICKS_PER_SLOT);
5848 				slot = HPTS_TICKS_PER_SLOT;
5849 			}
5850 		} else {
5851 			slot -= rack->r_ctl.rc_agg_delayed;
5852 			rack->r_ctl.rc_agg_delayed = 0;
5853 			/* Make sure we have 100 useconds at minimum */
5854 			if (slot < HPTS_TICKS_PER_SLOT) {
5855 				rack->r_ctl.rc_agg_delayed = HPTS_TICKS_PER_SLOT - slot;
5856 				slot = HPTS_TICKS_PER_SLOT;
5857 			}
5858 			if (rack->r_ctl.rc_agg_delayed == 0)
5859 				rack->r_late = 0;
5860 		}
5861 	}
5862 	if (slot) {
5863 		/* We are pacing too */
5864 		rack->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT;
5865 	}
5866 	hpts_timeout = rack_timer_start(tp, rack, cts, sup_rack);
5867 #ifdef NETFLIX_EXP_DETECTION
5868 	if (rack->sack_attack_disable &&
5869 	    (slot < tcp_sad_pacing_interval)) {
5870 		/*
5871 		 * We have a potential attacker on
5872 		 * the line. We have possibly some
5873 		 * (or now) pacing time set. We want to
5874 		 * slow down the processing of sacks by some
5875 		 * amount (if it is an attacker). Set the default
5876 		 * slot for attackers in place (unless the orginal
5877 		 * interval is longer). Its stored in
5878 		 * micro-seconds, so lets convert to msecs.
5879 		 */
5880 		slot = tcp_sad_pacing_interval;
5881 	}
5882 #endif
5883 	if (tp->t_flags & TF_DELACK) {
5884 		delayed_ack = TICKS_2_USEC(tcp_delacktime);
5885 		rack->r_ctl.rc_hpts_flags |= PACE_TMR_DELACK;
5886 	}
5887 	if (delayed_ack && ((hpts_timeout == 0) ||
5888 			    (delayed_ack < hpts_timeout)))
5889 		hpts_timeout = delayed_ack;
5890 	else
5891 		rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
5892 	/*
5893 	 * If no timers are going to run and we will fall off the hptsi
5894 	 * wheel, we resort to a keep-alive timer if its configured.
5895 	 */
5896 	if ((hpts_timeout == 0) &&
5897 	    (slot == 0)) {
5898 		if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
5899 		    (tp->t_state <= TCPS_CLOSING)) {
5900 			/*
5901 			 * Ok we have no timer (persists, rack, tlp, rxt  or
5902 			 * del-ack), we don't have segments being paced. So
5903 			 * all that is left is the keepalive timer.
5904 			 */
5905 			if (TCPS_HAVEESTABLISHED(tp->t_state)) {
5906 				/* Get the established keep-alive time */
5907 				hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp));
5908 			} else {
5909 				/*
5910 				 * Get the initial setup keep-alive time,
5911 				 * note that this is probably not going to
5912 				 * happen, since rack will be running a rxt timer
5913 				 * if a SYN of some sort is outstanding. It is
5914 				 * actually handled in rack_timeout_rxt().
5915 				 */
5916 				hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp));
5917 			}
5918 			rack->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP;
5919 			if (rack->in_probe_rtt) {
5920 				/*
5921 				 * We want to instead not wake up a long time from
5922 				 * now but to wake up about the time we would
5923 				 * exit probe-rtt and initiate a keep-alive ack.
5924 				 * This will get us out of probe-rtt and update
5925 				 * our min-rtt.
5926 				 */
5927 				hpts_timeout = rack_min_probertt_hold;
5928 			}
5929 		}
5930 	}
5931 	if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) ==
5932 	    (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) {
5933 		/*
5934 		 * RACK, TLP, persists and RXT timers all are restartable
5935 		 * based on actions input .. i.e we received a packet (ack
5936 		 * or sack) and that changes things (rw, or snd_una etc).
5937 		 * Thus we can restart them with a new value. For
5938 		 * keep-alive, delayed_ack we keep track of what was left
5939 		 * and restart the timer with a smaller value.
5940 		 */
5941 		if (left < hpts_timeout)
5942 			hpts_timeout = left;
5943 	}
5944 	if (hpts_timeout) {
5945 		/*
5946 		 * Hack alert for now we can't time-out over 2,147,483
5947 		 * seconds (a bit more than 596 hours), which is probably ok
5948 		 * :).
5949 		 */
5950 		if (hpts_timeout > 0x7ffffffe)
5951 			hpts_timeout = 0x7ffffffe;
5952 		rack->r_ctl.rc_timer_exp = cts + hpts_timeout;
5953 	}
5954 	rack_log_pacing_delay_calc(rack, entry_slot, slot, hpts_timeout, 0, 0, 27, __LINE__, NULL, 0);
5955 	if ((rack->gp_ready == 0) &&
5956 	    (rack->use_fixed_rate == 0) &&
5957 	    (hpts_timeout < slot) &&
5958 	    (rack->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) {
5959 		/*
5960 		 * We have no good estimate yet for the
5961 		 * old clunky burst mitigation or the
5962 		 * real pacing. And the tlp or rxt is smaller
5963 		 * than the pacing calculation. Lets not
5964 		 * pace that long since we know the calculation
5965 		 * so far is not accurate.
5966 		 */
5967 		slot = hpts_timeout;
5968 	}
5969 	rack->r_ctl.last_pacing_time = slot;
5970 	/**
5971 	 * Turn off all the flags for queuing by default. The
5972 	 * flags have important meanings to what happens when
5973 	 * LRO interacts with the transport. Most likely (by default now)
5974 	 * mbuf_queueing and ack compression are on. So the transport
5975 	 * has a couple of flags that control what happens (if those
5976 	 * are not on then these flags won't have any effect since it
5977 	 * won't go through the queuing LRO path).
5978 	 *
5979 	 * INP_MBUF_QUEUE_READY - This flags says that I am busy
5980 	 *                        pacing output, so don't disturb. But
5981 	 *                        it also means LRO can wake me if there
5982 	 *                        is a SACK arrival.
5983 	 *
5984 	 * INP_DONT_SACK_QUEUE - This flag is used in conjunction
5985 	 *                       with the above flag (QUEUE_READY) and
5986 	 *                       when present it says don't even wake me
5987 	 *                       if a SACK arrives.
5988 	 *
5989 	 * The idea behind these flags is that if we are pacing we
5990 	 * set the MBUF_QUEUE_READY and only get woken up if
5991 	 * a SACK arrives (which could change things) or if
5992 	 * our pacing timer expires. If, however, we have a rack
5993 	 * timer running, then we don't even want a sack to wake
5994 	 * us since the rack timer has to expire before we can send.
5995 	 *
5996 	 * Other cases should usually have none of the flags set
5997 	 * so LRO can call into us.
5998 	 */
5999 	inp->inp_flags2 &= ~(INP_DONT_SACK_QUEUE|INP_MBUF_QUEUE_READY);
6000 	if (slot) {
6001 		rack->r_ctl.rc_last_output_to = us_cts + slot;
6002 		/*
6003 		 * A pacing timer (slot) is being set, in
6004 		 * such a case we cannot send (we are blocked by
6005 		 * the timer). So lets tell LRO that it should not
6006 		 * wake us unless there is a SACK. Note this only
6007 		 * will be effective if mbuf queueing is on or
6008 		 * compressed acks are being processed.
6009 		 */
6010 		inp->inp_flags2 |= INP_MBUF_QUEUE_READY;
6011 		/*
6012 		 * But wait if we have a Rack timer running
6013 		 * even a SACK should not disturb us (with
6014 		 * the exception of r_rr_config 3).
6015 		 */
6016 		if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK) &&
6017 		    (rack->r_rr_config != 3))
6018 			inp->inp_flags2 |= INP_DONT_SACK_QUEUE;
6019 		if (rack->rc_ack_can_sendout_data) {
6020 			/*
6021 			 * Ahh but wait, this is that special case
6022 			 * where the pacing timer can be disturbed
6023 			 * backout the changes (used for non-paced
6024 			 * burst limiting).
6025 			 */
6026 			inp->inp_flags2 &= ~(INP_DONT_SACK_QUEUE|INP_MBUF_QUEUE_READY);
6027 		}
6028 		if ((rack->use_rack_rr) &&
6029 		    (rack->r_rr_config < 2) &&
6030 		    ((hpts_timeout) && (hpts_timeout < slot))) {
6031 			/*
6032 			 * Arrange for the hpts to kick back in after the
6033 			 * t-o if the t-o does not cause a send.
6034 			 */
6035 			(void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(hpts_timeout),
6036 						   __LINE__, &diag);
6037 			rack_log_hpts_diag(rack, us_cts, &diag, &tv);
6038 			rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
6039 		} else {
6040 			(void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(slot),
6041 						   __LINE__, &diag);
6042 			rack_log_hpts_diag(rack, us_cts, &diag, &tv);
6043 			rack_log_to_start(rack, cts, hpts_timeout, slot, 1);
6044 		}
6045 	} else if (hpts_timeout) {
6046 		/*
6047 		 * With respect to inp_flags2 here, lets let any new acks wake
6048 		 * us up here. Since we are not pacing (no pacing timer), output
6049 		 * can happen so we should let it. If its a Rack timer, then any inbound
6050 		 * packet probably won't change the sending (we will be blocked)
6051 		 * but it may change the prr stats so letting it in (the set defaults
6052 		 * at the start of this block) are good enough.
6053 		 */
6054 		(void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(hpts_timeout),
6055 					   __LINE__, &diag);
6056 		rack_log_hpts_diag(rack, us_cts, &diag, &tv);
6057 		rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
6058 	} else {
6059 		/* No timer starting */
6060 #ifdef INVARIANTS
6061 		if (SEQ_GT(tp->snd_max, tp->snd_una)) {
6062 			panic("tp:%p rack:%p tlts:%d cts:%u slot:%u pto:%u -- no timer started?",
6063 			    tp, rack, tot_len_this_send, cts, slot, hpts_timeout);
6064 		}
6065 #endif
6066 	}
6067 	rack->rc_tmr_stopped = 0;
6068 	if (slot)
6069 		rack_log_type_bbrsnd(rack, tot_len_this_send, slot, us_cts, &tv);
6070 }
6071 
6072 /*
6073  * RACK Timer, here we simply do logging and house keeping.
6074  * the normal rack_output() function will call the
6075  * appropriate thing to check if we need to do a RACK retransmit.
6076  * We return 1, saying don't proceed with rack_output only
6077  * when all timers have been stopped (destroyed PCB?).
6078  */
6079 static int
6080 rack_timeout_rack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6081 {
6082 	/*
6083 	 * This timer simply provides an internal trigger to send out data.
6084 	 * The check_recovery_mode call will see if there are needed
6085 	 * retransmissions, if so we will enter fast-recovery. The output
6086 	 * call may or may not do the same thing depending on sysctl
6087 	 * settings.
6088 	 */
6089 	struct rack_sendmap *rsm;
6090 
6091 	if (tp->t_timers->tt_flags & TT_STOPPED) {
6092 		return (1);
6093 	}
6094 	counter_u64_add(rack_to_tot, 1);
6095 	if (rack->r_state && (rack->r_state != tp->t_state))
6096 		rack_set_state(tp, rack);
6097 	rack->rc_on_min_to = 0;
6098 	rsm = rack_check_recovery_mode(tp, cts);
6099 	rack_log_to_event(rack, RACK_TO_FRM_RACK, rsm);
6100 	if (rsm) {
6101 		rack->r_ctl.rc_resend = rsm;
6102 		rack->r_timer_override = 1;
6103 		if (rack->use_rack_rr) {
6104 			/*
6105 			 * Don't accumulate extra pacing delay
6106 			 * we are allowing the rack timer to
6107 			 * over-ride pacing i.e. rrr takes precedence
6108 			 * if the pacing interval is longer than the rrr
6109 			 * time (in other words we get the min pacing
6110 			 * time versus rrr pacing time).
6111 			 */
6112 			rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
6113 		}
6114 	}
6115 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK;
6116 	if (rsm == NULL) {
6117 		/* restart a timer and return 1 */
6118 		rack_start_hpts_timer(rack, tp, cts,
6119 				      0, 0, 0);
6120 		return (1);
6121 	}
6122 	return (0);
6123 }
6124 
6125 static void
6126 rack_adjust_orig_mlen(struct rack_sendmap *rsm)
6127 {
6128 	if (rsm->m->m_len > rsm->orig_m_len) {
6129 		/*
6130 		 * Mbuf grew, caused by sbcompress, our offset does
6131 		 * not change.
6132 		 */
6133 		rsm->orig_m_len = rsm->m->m_len;
6134 	} else if (rsm->m->m_len < rsm->orig_m_len) {
6135 		/*
6136 		 * Mbuf shrank, trimmed off the top by an ack, our
6137 		 * offset changes.
6138 		 */
6139 		rsm->soff -= (rsm->orig_m_len - rsm->m->m_len);
6140 		rsm->orig_m_len = rsm->m->m_len;
6141 	}
6142 }
6143 
6144 static void
6145 rack_setup_offset_for_rsm(struct rack_sendmap *src_rsm, struct rack_sendmap *rsm)
6146 {
6147 	struct mbuf *m;
6148 	uint32_t soff;
6149 
6150 	if (src_rsm->m && (src_rsm->orig_m_len != src_rsm->m->m_len)) {
6151 		/* Fix up the orig_m_len and possibly the mbuf offset */
6152 		rack_adjust_orig_mlen(src_rsm);
6153 	}
6154 	m = src_rsm->m;
6155 	soff = src_rsm->soff + (src_rsm->r_end - src_rsm->r_start);
6156 	while (soff >= m->m_len) {
6157 		/* Move out past this mbuf */
6158 		soff -= m->m_len;
6159 		m = m->m_next;
6160 		KASSERT((m != NULL),
6161 			("rsm:%p nrsm:%p hit at soff:%u null m",
6162 			 src_rsm, rsm, soff));
6163 	}
6164 	rsm->m = m;
6165 	rsm->soff = soff;
6166 	rsm->orig_m_len = m->m_len;
6167 }
6168 
6169 static __inline void
6170 rack_clone_rsm(struct tcp_rack *rack, struct rack_sendmap *nrsm,
6171 	       struct rack_sendmap *rsm, uint32_t start)
6172 {
6173 	int idx;
6174 
6175 	nrsm->r_start = start;
6176 	nrsm->r_end = rsm->r_end;
6177 	nrsm->r_rtr_cnt = rsm->r_rtr_cnt;
6178 	nrsm->r_flags = rsm->r_flags;
6179 	nrsm->r_dupack = rsm->r_dupack;
6180 	nrsm->r_no_rtt_allowed = rsm->r_no_rtt_allowed;
6181 	nrsm->r_rtr_bytes = 0;
6182 	nrsm->r_fas = rsm->r_fas;
6183 	rsm->r_end = nrsm->r_start;
6184 	nrsm->r_just_ret = rsm->r_just_ret;
6185 	for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) {
6186 		nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx];
6187 	}
6188 	/* Now if we have SYN flag we keep it on the left edge */
6189 	if (nrsm->r_flags & RACK_HAS_SYN)
6190 		nrsm->r_flags &= ~RACK_HAS_SYN;
6191 	/* Now if we have a FIN flag we keep it on the right edge */
6192 	if (rsm->r_flags & RACK_HAS_FIN)
6193 		rsm->r_flags &= ~RACK_HAS_FIN;
6194 	/* Push bit must go to the right edge as well */
6195 	if (rsm->r_flags & RACK_HAD_PUSH)
6196 		rsm->r_flags &= ~RACK_HAD_PUSH;
6197 	/* Clone over the state of the hw_tls flag */
6198 	nrsm->r_hw_tls = rsm->r_hw_tls;
6199 	/*
6200 	 * Now we need to find nrsm's new location in the mbuf chain
6201 	 * we basically calculate a new offset, which is soff +
6202 	 * how much is left in original rsm. Then we walk out the mbuf
6203 	 * chain to find the righ postion, it may be the same mbuf
6204 	 * or maybe not.
6205 	 */
6206 	KASSERT(((rsm->m != NULL) ||
6207 		 (rsm->r_flags & (RACK_HAS_SYN|RACK_HAS_FIN))),
6208 		("rsm:%p nrsm:%p rack:%p -- rsm->m is NULL?", rsm, nrsm, rack));
6209 	if (rsm->m)
6210 		rack_setup_offset_for_rsm(rsm, nrsm);
6211 }
6212 
6213 static struct rack_sendmap *
6214 rack_merge_rsm(struct tcp_rack *rack,
6215 	       struct rack_sendmap *l_rsm,
6216 	       struct rack_sendmap *r_rsm)
6217 {
6218 	/*
6219 	 * We are merging two ack'd RSM's,
6220 	 * the l_rsm is on the left (lower seq
6221 	 * values) and the r_rsm is on the right
6222 	 * (higher seq value). The simplest way
6223 	 * to merge these is to move the right
6224 	 * one into the left. I don't think there
6225 	 * is any reason we need to try to find
6226 	 * the oldest (or last oldest retransmitted).
6227 	 */
6228 #ifdef INVARIANTS
6229 	struct rack_sendmap *rm;
6230 #endif
6231 	rack_log_map_chg(rack->rc_tp, rack, NULL,
6232 			 l_rsm, r_rsm, MAP_MERGE, r_rsm->r_end, __LINE__);
6233 	l_rsm->r_end = r_rsm->r_end;
6234 	if (l_rsm->r_dupack < r_rsm->r_dupack)
6235 		l_rsm->r_dupack = r_rsm->r_dupack;
6236 	if (r_rsm->r_rtr_bytes)
6237 		l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes;
6238 	if (r_rsm->r_in_tmap) {
6239 		/* This really should not happen */
6240 		TAILQ_REMOVE(&rack->r_ctl.rc_tmap, r_rsm, r_tnext);
6241 		r_rsm->r_in_tmap = 0;
6242 	}
6243 
6244 	/* Now the flags */
6245 	if (r_rsm->r_flags & RACK_HAS_FIN)
6246 		l_rsm->r_flags |= RACK_HAS_FIN;
6247 	if (r_rsm->r_flags & RACK_TLP)
6248 		l_rsm->r_flags |= RACK_TLP;
6249 	if (r_rsm->r_flags & RACK_RWND_COLLAPSED)
6250 		l_rsm->r_flags |= RACK_RWND_COLLAPSED;
6251 	if ((r_rsm->r_flags & RACK_APP_LIMITED)  &&
6252 	    ((l_rsm->r_flags & RACK_APP_LIMITED) == 0)) {
6253 		/*
6254 		 * If both are app-limited then let the
6255 		 * free lower the count. If right is app
6256 		 * limited and left is not, transfer.
6257 		 */
6258 		l_rsm->r_flags |= RACK_APP_LIMITED;
6259 		r_rsm->r_flags &= ~RACK_APP_LIMITED;
6260 		if (r_rsm == rack->r_ctl.rc_first_appl)
6261 			rack->r_ctl.rc_first_appl = l_rsm;
6262 	}
6263 #ifndef INVARIANTS
6264 	(void)RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, r_rsm);
6265 #else
6266 	rm = RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, r_rsm);
6267 	if (rm != r_rsm) {
6268 		panic("removing head in rack:%p rsm:%p rm:%p",
6269 		      rack, r_rsm, rm);
6270 	}
6271 #endif
6272 	if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) {
6273 		/* Transfer the split limit to the map we free */
6274 		r_rsm->r_limit_type = l_rsm->r_limit_type;
6275 		l_rsm->r_limit_type = 0;
6276 	}
6277 	rack_free(rack, r_rsm);
6278 	return (l_rsm);
6279 }
6280 
6281 /*
6282  * TLP Timer, here we simply setup what segment we want to
6283  * have the TLP expire on, the normal rack_output() will then
6284  * send it out.
6285  *
6286  * We return 1, saying don't proceed with rack_output only
6287  * when all timers have been stopped (destroyed PCB?).
6288  */
6289 static int
6290 rack_timeout_tlp(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t *doing_tlp)
6291 {
6292 	/*
6293 	 * Tail Loss Probe.
6294 	 */
6295 	struct rack_sendmap *rsm = NULL;
6296 #ifdef INVARIANTS
6297 	struct rack_sendmap *insret;
6298 #endif
6299 	struct socket *so;
6300 	uint32_t amm;
6301 	uint32_t out, avail;
6302 	int collapsed_win = 0;
6303 
6304 	if (tp->t_timers->tt_flags & TT_STOPPED) {
6305 		return (1);
6306 	}
6307 	if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
6308 		/* Its not time yet */
6309 		return (0);
6310 	}
6311 	if (ctf_progress_timeout_check(tp, true)) {
6312 		rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
6313 		return (-ETIMEDOUT);	/* tcp_drop() */
6314 	}
6315 	/*
6316 	 * A TLP timer has expired. We have been idle for 2 rtts. So we now
6317 	 * need to figure out how to force a full MSS segment out.
6318 	 */
6319 	rack_log_to_event(rack, RACK_TO_FRM_TLP, NULL);
6320 	rack->r_ctl.retran_during_recovery = 0;
6321 	rack->r_ctl.dsack_byte_cnt = 0;
6322 	counter_u64_add(rack_tlp_tot, 1);
6323 	if (rack->r_state && (rack->r_state != tp->t_state))
6324 		rack_set_state(tp, rack);
6325 	so = tp->t_inpcb->inp_socket;
6326 	avail = sbavail(&so->so_snd);
6327 	out = tp->snd_max - tp->snd_una;
6328 	if (out > tp->snd_wnd) {
6329 		/* special case, we need a retransmission */
6330 		collapsed_win = 1;
6331 		goto need_retran;
6332 	}
6333 	if (rack->r_ctl.dsack_persist && (rack->r_ctl.rc_tlp_cnt_out >= 1)) {
6334 		rack->r_ctl.dsack_persist--;
6335 		if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
6336 			rack->r_ctl.num_dsack = 0;
6337 		}
6338 		rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
6339 	}
6340 	if ((tp->t_flags & TF_GPUTINPROG) &&
6341 	    (rack->r_ctl.rc_tlp_cnt_out == 1)) {
6342 		/*
6343 		 * If this is the second in a row
6344 		 * TLP and we are doing a measurement
6345 		 * its time to abandon the measurement.
6346 		 * Something is likely broken on
6347 		 * the clients network and measuring a
6348 		 * broken network does us no good.
6349 		 */
6350 		tp->t_flags &= ~TF_GPUTINPROG;
6351 		rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
6352 					   rack->r_ctl.rc_gp_srtt /*flex1*/,
6353 					   tp->gput_seq,
6354 					   0, 0, 18, __LINE__, NULL, 0);
6355 	}
6356 	/*
6357 	 * Check our send oldest always settings, and if
6358 	 * there is an oldest to send jump to the need_retran.
6359 	 */
6360 	if (rack_always_send_oldest && (TAILQ_EMPTY(&rack->r_ctl.rc_tmap) == 0))
6361 		goto need_retran;
6362 
6363 	if (avail > out) {
6364 		/* New data is available */
6365 		amm = avail - out;
6366 		if (amm > ctf_fixed_maxseg(tp)) {
6367 			amm = ctf_fixed_maxseg(tp);
6368 			if ((amm + out) > tp->snd_wnd) {
6369 				/* We are rwnd limited */
6370 				goto need_retran;
6371 			}
6372 		} else if (amm < ctf_fixed_maxseg(tp)) {
6373 			/* not enough to fill a MTU */
6374 			goto need_retran;
6375 		}
6376 		if (IN_FASTRECOVERY(tp->t_flags)) {
6377 			/* Unlikely */
6378 			if (rack->rack_no_prr == 0) {
6379 				if (out + amm <= tp->snd_wnd) {
6380 					rack->r_ctl.rc_prr_sndcnt = amm;
6381 					rack->r_ctl.rc_tlp_new_data = amm;
6382 					rack_log_to_prr(rack, 4, 0);
6383 				}
6384 			} else
6385 				goto need_retran;
6386 		} else {
6387 			/* Set the send-new override */
6388 			if (out + amm <= tp->snd_wnd)
6389 				rack->r_ctl.rc_tlp_new_data = amm;
6390 			else
6391 				goto need_retran;
6392 		}
6393 		rack->r_ctl.rc_tlpsend = NULL;
6394 		counter_u64_add(rack_tlp_newdata, 1);
6395 		goto send;
6396 	}
6397 need_retran:
6398 	/*
6399 	 * Ok we need to arrange the last un-acked segment to be re-sent, or
6400 	 * optionally the first un-acked segment.
6401 	 */
6402 	if (collapsed_win == 0) {
6403 		if (rack_always_send_oldest)
6404 			rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6405 		else {
6406 			rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
6407 			if (rsm && (rsm->r_flags & (RACK_ACKED | RACK_HAS_FIN))) {
6408 				rsm = rack_find_high_nonack(rack, rsm);
6409 			}
6410 		}
6411 		if (rsm == NULL) {
6412 			counter_u64_add(rack_tlp_does_nada, 1);
6413 #ifdef TCP_BLACKBOX
6414 			tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true);
6415 #endif
6416 			goto out;
6417 		}
6418 	} else {
6419 		/*
6420 		 * We must find the last segment
6421 		 * that was acceptable by the client.
6422 		 */
6423 		RB_FOREACH_REVERSE(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
6424 			if ((rsm->r_flags & RACK_RWND_COLLAPSED) == 0) {
6425 				/* Found one */
6426 				break;
6427 			}
6428 		}
6429 		if (rsm == NULL) {
6430 			/* None? if so send the first */
6431 			rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
6432 			if (rsm == NULL) {
6433 				counter_u64_add(rack_tlp_does_nada, 1);
6434 #ifdef TCP_BLACKBOX
6435 				tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true);
6436 #endif
6437 				goto out;
6438 			}
6439 		}
6440 	}
6441 	if ((rsm->r_end - rsm->r_start) > ctf_fixed_maxseg(tp)) {
6442 		/*
6443 		 * We need to split this the last segment in two.
6444 		 */
6445 		struct rack_sendmap *nrsm;
6446 
6447 		nrsm = rack_alloc_full_limit(rack);
6448 		if (nrsm == NULL) {
6449 			/*
6450 			 * No memory to split, we will just exit and punt
6451 			 * off to the RXT timer.
6452 			 */
6453 			counter_u64_add(rack_tlp_does_nada, 1);
6454 			goto out;
6455 		}
6456 		rack_clone_rsm(rack, nrsm, rsm,
6457 			       (rsm->r_end - ctf_fixed_maxseg(tp)));
6458 		rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
6459 #ifndef INVARIANTS
6460 		(void)RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
6461 #else
6462 		insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
6463 		if (insret != NULL) {
6464 			panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
6465 			      nrsm, insret, rack, rsm);
6466 		}
6467 #endif
6468 		if (rsm->r_in_tmap) {
6469 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
6470 			nrsm->r_in_tmap = 1;
6471 		}
6472 		rsm = nrsm;
6473 	}
6474 	rack->r_ctl.rc_tlpsend = rsm;
6475 send:
6476 	/* Make sure output path knows we are doing a TLP */
6477 	*doing_tlp = 1;
6478 	rack->r_timer_override = 1;
6479 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
6480 	return (0);
6481 out:
6482 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
6483 	return (0);
6484 }
6485 
6486 /*
6487  * Delayed ack Timer, here we simply need to setup the
6488  * ACK_NOW flag and remove the DELACK flag. From there
6489  * the output routine will send the ack out.
6490  *
6491  * We only return 1, saying don't proceed, if all timers
6492  * are stopped (destroyed PCB?).
6493  */
6494 static int
6495 rack_timeout_delack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6496 {
6497 	if (tp->t_timers->tt_flags & TT_STOPPED) {
6498 		return (1);
6499 	}
6500 	rack_log_to_event(rack, RACK_TO_FRM_DELACK, NULL);
6501 	tp->t_flags &= ~TF_DELACK;
6502 	tp->t_flags |= TF_ACKNOW;
6503 	KMOD_TCPSTAT_INC(tcps_delack);
6504 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
6505 	return (0);
6506 }
6507 
6508 /*
6509  * Persists timer, here we simply send the
6510  * same thing as a keepalive will.
6511  * the one byte send.
6512  *
6513  * We only return 1, saying don't proceed, if all timers
6514  * are stopped (destroyed PCB?).
6515  */
6516 static int
6517 rack_timeout_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6518 {
6519 	struct tcptemp *t_template;
6520 	struct inpcb *inp;
6521 	int32_t retval = 1;
6522 
6523 	inp = tp->t_inpcb;
6524 
6525 	if (tp->t_timers->tt_flags & TT_STOPPED) {
6526 		return (1);
6527 	}
6528 	if (rack->rc_in_persist == 0)
6529 		return (0);
6530 	if (ctf_progress_timeout_check(tp, false)) {
6531 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
6532 		rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
6533 		counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
6534 		return (-ETIMEDOUT);	/* tcp_drop() */
6535 	}
6536 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
6537 	/*
6538 	 * Persistence timer into zero window. Force a byte to be output, if
6539 	 * possible.
6540 	 */
6541 	KMOD_TCPSTAT_INC(tcps_persisttimeo);
6542 	/*
6543 	 * Hack: if the peer is dead/unreachable, we do not time out if the
6544 	 * window is closed.  After a full backoff, drop the connection if
6545 	 * the idle time (no responses to probes) reaches the maximum
6546 	 * backoff that we would use if retransmitting.
6547 	 */
6548 	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
6549 	    (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
6550 	     TICKS_2_USEC(ticks - tp->t_rcvtime) >= RACK_REXMTVAL(tp) * tcp_totbackoff)) {
6551 		KMOD_TCPSTAT_INC(tcps_persistdrop);
6552 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
6553 		counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
6554 		retval = -ETIMEDOUT;	/* tcp_drop() */
6555 		goto out;
6556 	}
6557 	if ((sbavail(&rack->rc_inp->inp_socket->so_snd) == 0) &&
6558 	    tp->snd_una == tp->snd_max)
6559 		rack_exit_persist(tp, rack, cts);
6560 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT;
6561 	/*
6562 	 * If the user has closed the socket then drop a persisting
6563 	 * connection after a much reduced timeout.
6564 	 */
6565 	if (tp->t_state > TCPS_CLOSE_WAIT &&
6566 	    (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
6567 		KMOD_TCPSTAT_INC(tcps_persistdrop);
6568 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
6569 		counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
6570 		retval = -ETIMEDOUT;	/* tcp_drop() */
6571 		goto out;
6572 	}
6573 	t_template = tcpip_maketemplate(rack->rc_inp);
6574 	if (t_template) {
6575 		/* only set it if we were answered */
6576 		if (rack->forced_ack == 0) {
6577 			rack->forced_ack = 1;
6578 			rack->r_ctl.forced_ack_ts = tcp_get_usecs(NULL);
6579 		} else {
6580 			rack->probe_not_answered = 1;
6581 			counter_u64_add(rack_persists_loss, 1);
6582 			rack->r_ctl.persist_lost_ends++;
6583 		}
6584 		counter_u64_add(rack_persists_sends, 1);
6585 		tcp_respond(tp, t_template->tt_ipgen,
6586 			    &t_template->tt_t, (struct mbuf *)NULL,
6587 			    tp->rcv_nxt, tp->snd_una - 1, 0);
6588 		/* This sends an ack */
6589 		if (tp->t_flags & TF_DELACK)
6590 			tp->t_flags &= ~TF_DELACK;
6591 		free(t_template, M_TEMP);
6592 	}
6593 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
6594 		tp->t_rxtshift++;
6595 out:
6596 	rack_log_to_event(rack, RACK_TO_FRM_PERSIST, NULL);
6597 	rack_start_hpts_timer(rack, tp, cts,
6598 			      0, 0, 0);
6599 	return (retval);
6600 }
6601 
6602 /*
6603  * If a keepalive goes off, we had no other timers
6604  * happening. We always return 1 here since this
6605  * routine either drops the connection or sends
6606  * out a segment with respond.
6607  */
6608 static int
6609 rack_timeout_keepalive(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6610 {
6611 	struct tcptemp *t_template;
6612 	struct inpcb *inp;
6613 
6614 	if (tp->t_timers->tt_flags & TT_STOPPED) {
6615 		return (1);
6616 	}
6617 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP;
6618 	inp = tp->t_inpcb;
6619 	rack_log_to_event(rack, RACK_TO_FRM_KEEP, NULL);
6620 	/*
6621 	 * Keep-alive timer went off; send something or drop connection if
6622 	 * idle for too long.
6623 	 */
6624 	KMOD_TCPSTAT_INC(tcps_keeptimeo);
6625 	if (tp->t_state < TCPS_ESTABLISHED)
6626 		goto dropit;
6627 	if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
6628 	    tp->t_state <= TCPS_CLOSING) {
6629 		if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
6630 			goto dropit;
6631 		/*
6632 		 * Send a packet designed to force a response if the peer is
6633 		 * up and reachable: either an ACK if the connection is
6634 		 * still alive, or an RST if the peer has closed the
6635 		 * connection due to timeout or reboot. Using sequence
6636 		 * number tp->snd_una-1 causes the transmitted zero-length
6637 		 * segment to lie outside the receive window; by the
6638 		 * protocol spec, this requires the correspondent TCP to
6639 		 * respond.
6640 		 */
6641 		KMOD_TCPSTAT_INC(tcps_keepprobe);
6642 		t_template = tcpip_maketemplate(inp);
6643 		if (t_template) {
6644 			if (rack->forced_ack == 0) {
6645 				rack->forced_ack = 1;
6646 				rack->r_ctl.forced_ack_ts = tcp_get_usecs(NULL);
6647 			} else {
6648 				rack->probe_not_answered = 1;
6649 			}
6650 			tcp_respond(tp, t_template->tt_ipgen,
6651 			    &t_template->tt_t, (struct mbuf *)NULL,
6652 			    tp->rcv_nxt, tp->snd_una - 1, 0);
6653 			free(t_template, M_TEMP);
6654 		}
6655 	}
6656 	rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
6657 	return (1);
6658 dropit:
6659 	KMOD_TCPSTAT_INC(tcps_keepdrops);
6660 	tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
6661 	return (-ETIMEDOUT);	/* tcp_drop() */
6662 }
6663 
6664 /*
6665  * Retransmit helper function, clear up all the ack
6666  * flags and take care of important book keeping.
6667  */
6668 static void
6669 rack_remxt_tmr(struct tcpcb *tp)
6670 {
6671 	/*
6672 	 * The retransmit timer went off, all sack'd blocks must be
6673 	 * un-acked.
6674 	 */
6675 	struct rack_sendmap *rsm, *trsm = NULL;
6676 	struct tcp_rack *rack;
6677 
6678 	rack = (struct tcp_rack *)tp->t_fb_ptr;
6679 	rack_timer_cancel(tp, rack, tcp_get_usecs(NULL), __LINE__);
6680 	rack_log_to_event(rack, RACK_TO_FRM_TMR, NULL);
6681 	if (rack->r_state && (rack->r_state != tp->t_state))
6682 		rack_set_state(tp, rack);
6683 	/*
6684 	 * Ideally we would like to be able to
6685 	 * mark SACK-PASS on anything not acked here.
6686 	 *
6687 	 * However, if we do that we would burst out
6688 	 * all that data 1ms apart. This would be unwise,
6689 	 * so for now we will just let the normal rxt timer
6690 	 * and tlp timer take care of it.
6691 	 *
6692 	 * Also we really need to stick them back in sequence
6693 	 * order. This way we send in the proper order and any
6694 	 * sacks that come floating in will "re-ack" the data.
6695 	 * To do this we zap the tmap with an INIT and then
6696 	 * walk through and place every rsm in the RB tree
6697 	 * back in its seq ordered place.
6698 	 */
6699 	TAILQ_INIT(&rack->r_ctl.rc_tmap);
6700 	RB_FOREACH(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
6701 		rsm->r_dupack = 0;
6702 		rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
6703 		/* We must re-add it back to the tlist */
6704 		if (trsm == NULL) {
6705 			TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
6706 		} else {
6707 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, trsm, rsm, r_tnext);
6708 		}
6709 		rsm->r_in_tmap = 1;
6710 		trsm = rsm;
6711 		if (rsm->r_flags & RACK_ACKED)
6712 			rsm->r_flags |= RACK_WAS_ACKED;
6713 		rsm->r_flags &= ~(RACK_ACKED | RACK_SACK_PASSED | RACK_WAS_SACKPASS);
6714 		rsm->r_flags |= RACK_MUST_RXT;
6715 	}
6716 	/* Clear the count (we just un-acked them) */
6717 	rack->r_ctl.rc_last_timeout_snduna = tp->snd_una;
6718 	rack->r_ctl.rc_sacked = 0;
6719 	rack->r_ctl.rc_sacklast = NULL;
6720 	rack->r_ctl.rc_agg_delayed = 0;
6721 	rack->r_early = 0;
6722 	rack->r_ctl.rc_agg_early = 0;
6723 	rack->r_late = 0;
6724 	/* Clear the tlp rtx mark */
6725 	rack->r_ctl.rc_resend = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
6726 	if (rack->r_ctl.rc_resend != NULL)
6727 		rack->r_ctl.rc_resend->r_flags |= RACK_TO_REXT;
6728 	rack->r_ctl.rc_prr_sndcnt = 0;
6729 	rack_log_to_prr(rack, 6, 0);
6730 	rack->r_timer_override = 1;
6731 	if ((((tp->t_flags & TF_SACK_PERMIT) == 0)
6732 #ifdef NETFLIX_EXP_DETECTION
6733 	    || (rack->sack_attack_disable != 0)
6734 #endif
6735 		    ) && ((tp->t_flags & TF_SENTFIN) == 0)) {
6736 		/*
6737 		 * For non-sack customers new data
6738 		 * needs to go out as retransmits until
6739 		 * we retransmit up to snd_max.
6740 		 */
6741 		rack->r_must_retran = 1;
6742 		rack->r_ctl.rc_out_at_rto = ctf_flight_size(rack->rc_tp,
6743 						rack->r_ctl.rc_sacked);
6744 	}
6745 	rack->r_ctl.rc_snd_max_at_rto = tp->snd_max;
6746 }
6747 
6748 static void
6749 rack_convert_rtts(struct tcpcb *tp)
6750 {
6751 	if (tp->t_srtt > 1) {
6752 		uint32_t val, frac;
6753 
6754 		val = tp->t_srtt >> TCP_RTT_SHIFT;
6755 		frac = tp->t_srtt & 0x1f;
6756 		tp->t_srtt = TICKS_2_USEC(val);
6757 		/*
6758 		 * frac is the fractional part of the srtt (if any)
6759 		 * but its in ticks and every bit represents
6760 		 * 1/32nd of a hz.
6761 		 */
6762 		if (frac) {
6763 			if (hz == 1000) {
6764 				frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_MSEC) / (uint64_t)TCP_RTT_SCALE);
6765 			} else {
6766 				frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_SEC) / ((uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE));
6767 			}
6768 			tp->t_srtt += frac;
6769 		}
6770 	}
6771 	if (tp->t_rttvar) {
6772 		uint32_t val, frac;
6773 
6774 		val = tp->t_rttvar >> TCP_RTTVAR_SHIFT;
6775 		frac = tp->t_rttvar & 0x1f;
6776 		tp->t_rttvar = TICKS_2_USEC(val);
6777 		/*
6778 		 * frac is the fractional part of the srtt (if any)
6779 		 * but its in ticks and every bit represents
6780 		 * 1/32nd of a hz.
6781 		 */
6782 		if (frac) {
6783 			if (hz == 1000) {
6784 				frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_MSEC) / (uint64_t)TCP_RTT_SCALE);
6785 			} else {
6786 				frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_SEC) / ((uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE));
6787 			}
6788 			tp->t_rttvar += frac;
6789 		}
6790 	}
6791 	tp->t_rxtcur = RACK_REXMTVAL(tp);
6792 	if (TCPS_HAVEESTABLISHED(tp->t_state)) {
6793 		tp->t_rxtcur += TICKS_2_USEC(tcp_rexmit_slop);
6794 	}
6795 	if (tp->t_rxtcur > rack_rto_max) {
6796 		tp->t_rxtcur = rack_rto_max;
6797 	}
6798 }
6799 
6800 static void
6801 rack_cc_conn_init(struct tcpcb *tp)
6802 {
6803 	struct tcp_rack *rack;
6804 	uint32_t srtt;
6805 
6806 	rack = (struct tcp_rack *)tp->t_fb_ptr;
6807 	srtt = tp->t_srtt;
6808 	cc_conn_init(tp);
6809 	/*
6810 	 * Now convert to rack's internal format,
6811 	 * if required.
6812 	 */
6813 	if ((srtt == 0) && (tp->t_srtt != 0))
6814 		rack_convert_rtts(tp);
6815 	/*
6816 	 * We want a chance to stay in slowstart as
6817 	 * we create a connection. TCP spec says that
6818 	 * initially ssthresh is infinite. For our
6819 	 * purposes that is the snd_wnd.
6820 	 */
6821 	if (tp->snd_ssthresh < tp->snd_wnd) {
6822 		tp->snd_ssthresh = tp->snd_wnd;
6823 	}
6824 	/*
6825 	 * We also want to assure a IW worth of
6826 	 * data can get inflight.
6827 	 */
6828 	if (rc_init_window(rack) < tp->snd_cwnd)
6829 		tp->snd_cwnd = rc_init_window(rack);
6830 }
6831 
6832 /*
6833  * Re-transmit timeout! If we drop the PCB we will return 1, otherwise
6834  * we will setup to retransmit the lowest seq number outstanding.
6835  */
6836 static int
6837 rack_timeout_rxt(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6838 {
6839 	int32_t rexmt;
6840 	int32_t retval = 0;
6841 	bool isipv6;
6842 
6843 	if (tp->t_timers->tt_flags & TT_STOPPED) {
6844 		return (1);
6845 	}
6846 	if ((tp->t_flags & TF_GPUTINPROG) &&
6847 	    (tp->t_rxtshift)) {
6848 		/*
6849 		 * We have had a second timeout
6850 		 * measurements on successive rxt's are not profitable.
6851 		 * It is unlikely to be of any use (the network is
6852 		 * broken or the client went away).
6853 		 */
6854 		tp->t_flags &= ~TF_GPUTINPROG;
6855 		rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
6856 					   rack->r_ctl.rc_gp_srtt /*flex1*/,
6857 					   tp->gput_seq,
6858 					   0, 0, 18, __LINE__, NULL, 0);
6859 	}
6860 	if (ctf_progress_timeout_check(tp, false)) {
6861 		tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
6862 		rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
6863 		return (-ETIMEDOUT);	/* tcp_drop() */
6864 	}
6865 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT;
6866 	rack->r_ctl.retran_during_recovery = 0;
6867 	rack->r_ctl.dsack_byte_cnt = 0;
6868 	if (IN_FASTRECOVERY(tp->t_flags))
6869 		tp->t_flags |= TF_WASFRECOVERY;
6870 	else
6871 		tp->t_flags &= ~TF_WASFRECOVERY;
6872 	if (IN_CONGRECOVERY(tp->t_flags))
6873 		tp->t_flags |= TF_WASCRECOVERY;
6874 	else
6875 		tp->t_flags &= ~TF_WASCRECOVERY;
6876 	if (TCPS_HAVEESTABLISHED(tp->t_state) &&
6877 	    (tp->snd_una == tp->snd_max)) {
6878 		/* Nothing outstanding .. nothing to do */
6879 		return (0);
6880 	}
6881 	if (rack->r_ctl.dsack_persist) {
6882 		rack->r_ctl.dsack_persist--;
6883 		if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
6884 			rack->r_ctl.num_dsack = 0;
6885 		}
6886 		rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
6887 	}
6888 	/*
6889 	 * Rack can only run one timer  at a time, so we cannot
6890 	 * run a KEEPINIT (gating SYN sending) and a retransmit
6891 	 * timer for the SYN. So if we are in a front state and
6892 	 * have a KEEPINIT timer we need to check the first transmit
6893 	 * against now to see if we have exceeded the KEEPINIT time
6894 	 * (if one is set).
6895 	 */
6896 	if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) &&
6897 	    (TP_KEEPINIT(tp) != 0)) {
6898 		struct rack_sendmap *rsm;
6899 
6900 		rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
6901 		if (rsm) {
6902 			/* Ok we have something outstanding to test keepinit with */
6903 			if ((TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) &&
6904 			    ((cts - (uint32_t)rsm->r_tim_lastsent[0]) >= TICKS_2_USEC(TP_KEEPINIT(tp)))) {
6905 				/* We have exceeded the KEEPINIT time */
6906 				tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
6907 				goto drop_it;
6908 			}
6909 		}
6910 	}
6911 	/*
6912 	 * Retransmission timer went off.  Message has not been acked within
6913 	 * retransmit interval.  Back off to a longer retransmit interval
6914 	 * and retransmit one segment.
6915 	 */
6916 	rack_remxt_tmr(tp);
6917 	if ((rack->r_ctl.rc_resend == NULL) ||
6918 	    ((rack->r_ctl.rc_resend->r_flags & RACK_RWND_COLLAPSED) == 0)) {
6919 		/*
6920 		 * If the rwnd collapsed on
6921 		 * the one we are retransmitting
6922 		 * it does not count against the
6923 		 * rxt count.
6924 		 */
6925 		tp->t_rxtshift++;
6926 	}
6927 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT) {
6928 		tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
6929 drop_it:
6930 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
6931 		KMOD_TCPSTAT_INC(tcps_timeoutdrop);
6932 		/* XXXGL: previously t_softerror was casted to uint16_t */
6933 		MPASS(tp->t_softerror >= 0);
6934 		retval = tp->t_softerror ? -tp->t_softerror : -ETIMEDOUT;
6935 		goto out;	/* tcp_drop() */
6936 	}
6937 	if (tp->t_state == TCPS_SYN_SENT) {
6938 		/*
6939 		 * If the SYN was retransmitted, indicate CWND to be limited
6940 		 * to 1 segment in cc_conn_init().
6941 		 */
6942 		tp->snd_cwnd = 1;
6943 	} else if (tp->t_rxtshift == 1) {
6944 		/*
6945 		 * first retransmit; record ssthresh and cwnd so they can be
6946 		 * recovered if this turns out to be a "bad" retransmit. A
6947 		 * retransmit is considered "bad" if an ACK for this segment
6948 		 * is received within RTT/2 interval; the assumption here is
6949 		 * that the ACK was already in flight.  See "On Estimating
6950 		 * End-to-End Network Path Properties" by Allman and Paxson
6951 		 * for more details.
6952 		 */
6953 		tp->snd_cwnd_prev = tp->snd_cwnd;
6954 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
6955 		tp->snd_recover_prev = tp->snd_recover;
6956 		tp->t_badrxtwin = ticks + (USEC_2_TICKS(tp->t_srtt)/2);
6957 		tp->t_flags |= TF_PREVVALID;
6958 	} else if ((tp->t_flags & TF_RCVD_TSTMP) == 0)
6959 		tp->t_flags &= ~TF_PREVVALID;
6960 	KMOD_TCPSTAT_INC(tcps_rexmttimeo);
6961 	if ((tp->t_state == TCPS_SYN_SENT) ||
6962 	    (tp->t_state == TCPS_SYN_RECEIVED))
6963 		rexmt = RACK_INITIAL_RTO * tcp_backoff[tp->t_rxtshift];
6964 	else
6965 		rexmt = max(rack_rto_min, (tp->t_srtt + (tp->t_rttvar << 2))) * tcp_backoff[tp->t_rxtshift];
6966 
6967 	RACK_TCPT_RANGESET(tp->t_rxtcur, rexmt,
6968 	   max(rack_rto_min, rexmt), rack_rto_max, rack->r_ctl.timer_slop);
6969 	/*
6970 	 * We enter the path for PLMTUD if connection is established or, if
6971 	 * connection is FIN_WAIT_1 status, reason for the last is that if
6972 	 * amount of data we send is very small, we could send it in couple
6973 	 * of packets and process straight to FIN. In that case we won't
6974 	 * catch ESTABLISHED state.
6975 	 */
6976 #ifdef INET6
6977 	isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? true : false;
6978 #else
6979 	isipv6 = false;
6980 #endif
6981 	if (((V_tcp_pmtud_blackhole_detect == 1) ||
6982 	    (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) ||
6983 	    (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) &&
6984 	    ((tp->t_state == TCPS_ESTABLISHED) ||
6985 	    (tp->t_state == TCPS_FIN_WAIT_1))) {
6986 		/*
6987 		 * Idea here is that at each stage of mtu probe (usually,
6988 		 * 1448 -> 1188 -> 524) should be given 2 chances to recover
6989 		 * before further clamping down. 'tp->t_rxtshift % 2 == 0'
6990 		 * should take care of that.
6991 		 */
6992 		if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) ==
6993 		    (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) &&
6994 		    (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 &&
6995 		    tp->t_rxtshift % 2 == 0)) {
6996 			/*
6997 			 * Enter Path MTU Black-hole Detection mechanism: -
6998 			 * Disable Path MTU Discovery (IP "DF" bit). -
6999 			 * Reduce MTU to lower value than what we negotiated
7000 			 * with peer.
7001 			 */
7002 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
7003 				/* Record that we may have found a black hole. */
7004 				tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
7005 				/* Keep track of previous MSS. */
7006 				tp->t_pmtud_saved_maxseg = tp->t_maxseg;
7007 			}
7008 
7009 			/*
7010 			 * Reduce the MSS to blackhole value or to the
7011 			 * default in an attempt to retransmit.
7012 			 */
7013 #ifdef INET6
7014 			if (isipv6 &&
7015 			    tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) {
7016 				/* Use the sysctl tuneable blackhole MSS. */
7017 				tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
7018 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
7019 			} else if (isipv6) {
7020 				/* Use the default MSS. */
7021 				tp->t_maxseg = V_tcp_v6mssdflt;
7022 				/*
7023 				 * Disable Path MTU Discovery when we switch
7024 				 * to minmss.
7025 				 */
7026 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
7027 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
7028 			}
7029 #endif
7030 #if defined(INET6) && defined(INET)
7031 			else
7032 #endif
7033 #ifdef INET
7034 			if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) {
7035 				/* Use the sysctl tuneable blackhole MSS. */
7036 				tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
7037 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
7038 			} else {
7039 				/* Use the default MSS. */
7040 				tp->t_maxseg = V_tcp_mssdflt;
7041 				/*
7042 				 * Disable Path MTU Discovery when we switch
7043 				 * to minmss.
7044 				 */
7045 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
7046 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
7047 			}
7048 #endif
7049 		} else {
7050 			/*
7051 			 * If further retransmissions are still unsuccessful
7052 			 * with a lowered MTU, maybe this isn't a blackhole
7053 			 * and we restore the previous MSS and blackhole
7054 			 * detection flags. The limit '6' is determined by
7055 			 * giving each probe stage (1448, 1188, 524) 2
7056 			 * chances to recover.
7057 			 */
7058 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
7059 			    (tp->t_rxtshift >= 6)) {
7060 				tp->t_flags2 |= TF2_PLPMTU_PMTUD;
7061 				tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
7062 				tp->t_maxseg = tp->t_pmtud_saved_maxseg;
7063 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed);
7064 			}
7065 		}
7066 	}
7067 	/*
7068 	 * Disable RFC1323 and SACK if we haven't got any response to
7069 	 * our third SYN to work-around some broken terminal servers
7070 	 * (most of which have hopefully been retired) that have bad VJ
7071 	 * header compression code which trashes TCP segments containing
7072 	 * unknown-to-them TCP options.
7073 	 */
7074 	if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
7075 	    (tp->t_rxtshift == 3))
7076 		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
7077 	/*
7078 	 * If we backed off this far, our srtt estimate is probably bogus.
7079 	 * Clobber it so we'll take the next rtt measurement as our srtt;
7080 	 * move the current srtt into rttvar to keep the current retransmit
7081 	 * times until then.
7082 	 */
7083 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
7084 #ifdef INET6
7085 		if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
7086 			in6_losing(tp->t_inpcb);
7087 		else
7088 #endif
7089 			in_losing(tp->t_inpcb);
7090 		tp->t_rttvar += tp->t_srtt;
7091 		tp->t_srtt = 0;
7092 	}
7093 	sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
7094 	tp->snd_recover = tp->snd_max;
7095 	tp->t_flags |= TF_ACKNOW;
7096 	tp->t_rtttime = 0;
7097 	rack_cong_signal(tp, CC_RTO, tp->snd_una);
7098 out:
7099 	return (retval);
7100 }
7101 
7102 static int
7103 rack_process_timers(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t hpts_calling, uint8_t *doing_tlp)
7104 {
7105 	int32_t ret = 0;
7106 	int32_t timers = (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK);
7107 
7108 	if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
7109 	    (tp->t_flags & TF_GPUTINPROG)) {
7110 		/*
7111 		 * We have a goodput in progress
7112 		 * and we have entered a late state.
7113 		 * Do we have enough data in the sb
7114 		 * to handle the GPUT request?
7115 		 */
7116 		uint32_t bytes;
7117 
7118 		bytes = tp->gput_ack - tp->gput_seq;
7119 		if (SEQ_GT(tp->gput_seq, tp->snd_una))
7120 			bytes += tp->gput_seq - tp->snd_una;
7121 		if (bytes > sbavail(&tp->t_inpcb->inp_socket->so_snd)) {
7122 			/*
7123 			 * There are not enough bytes in the socket
7124 			 * buffer that have been sent to cover this
7125 			 * measurement. Cancel it.
7126 			 */
7127 			rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
7128 						   rack->r_ctl.rc_gp_srtt /*flex1*/,
7129 						   tp->gput_seq,
7130 						   0, 0, 18, __LINE__, NULL, 0);
7131 			tp->t_flags &= ~TF_GPUTINPROG;
7132 		}
7133 	}
7134 	if (timers == 0) {
7135 		return (0);
7136 	}
7137 	if (tp->t_state == TCPS_LISTEN) {
7138 		/* no timers on listen sockets */
7139 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)
7140 			return (0);
7141 		return (1);
7142 	}
7143 	if ((timers & PACE_TMR_RACK) &&
7144 	    rack->rc_on_min_to) {
7145 		/*
7146 		 * For the rack timer when we
7147 		 * are on a min-timeout (which means rrr_conf = 3)
7148 		 * we don't want to check the timer. It may
7149 		 * be going off for a pace and thats ok we
7150 		 * want to send the retransmit (if its ready).
7151 		 *
7152 		 * If its on a normal rack timer (non-min) then
7153 		 * we will check if its expired.
7154 		 */
7155 		goto skip_time_check;
7156 	}
7157 	if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
7158 		uint32_t left;
7159 
7160 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
7161 			ret = -1;
7162 			rack_log_to_processing(rack, cts, ret, 0);
7163 			return (0);
7164 		}
7165 		if (hpts_calling == 0) {
7166 			/*
7167 			 * A user send or queued mbuf (sack) has called us? We
7168 			 * return 0 and let the pacing guards
7169 			 * deal with it if they should or
7170 			 * should not cause a send.
7171 			 */
7172 			ret = -2;
7173 			rack_log_to_processing(rack, cts, ret, 0);
7174 			return (0);
7175 		}
7176 		/*
7177 		 * Ok our timer went off early and we are not paced false
7178 		 * alarm, go back to sleep.
7179 		 */
7180 		ret = -3;
7181 		left = rack->r_ctl.rc_timer_exp - cts;
7182 		tcp_hpts_insert(tp->t_inpcb, HPTS_MS_TO_SLOTS(left));
7183 		rack_log_to_processing(rack, cts, ret, left);
7184 		return (1);
7185 	}
7186 skip_time_check:
7187 	rack->rc_tmr_stopped = 0;
7188 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK;
7189 	if (timers & PACE_TMR_DELACK) {
7190 		ret = rack_timeout_delack(tp, rack, cts);
7191 	} else if (timers & PACE_TMR_RACK) {
7192 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
7193 		rack->r_fast_output = 0;
7194 		ret = rack_timeout_rack(tp, rack, cts);
7195 	} else if (timers & PACE_TMR_TLP) {
7196 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
7197 		ret = rack_timeout_tlp(tp, rack, cts, doing_tlp);
7198 	} else if (timers & PACE_TMR_RXT) {
7199 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
7200 		rack->r_fast_output = 0;
7201 		ret = rack_timeout_rxt(tp, rack, cts);
7202 	} else if (timers & PACE_TMR_PERSIT) {
7203 		ret = rack_timeout_persist(tp, rack, cts);
7204 	} else if (timers & PACE_TMR_KEEP) {
7205 		ret = rack_timeout_keepalive(tp, rack, cts);
7206 	}
7207 	rack_log_to_processing(rack, cts, ret, timers);
7208 	return (ret);
7209 }
7210 
7211 static void
7212 rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line)
7213 {
7214 	struct timeval tv;
7215 	uint32_t us_cts, flags_on_entry;
7216 	uint8_t hpts_removed = 0;
7217 
7218 	flags_on_entry = rack->r_ctl.rc_hpts_flags;
7219 	us_cts = tcp_get_usecs(&tv);
7220 	if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
7221 	    ((TSTMP_GEQ(us_cts, rack->r_ctl.rc_last_output_to)) ||
7222 	     ((tp->snd_max - tp->snd_una) == 0))) {
7223 		tcp_hpts_remove(rack->rc_inp);
7224 		hpts_removed = 1;
7225 		/* If we were not delayed cancel out the flag. */
7226 		if ((tp->snd_max - tp->snd_una) == 0)
7227 			rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
7228 		rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry);
7229 	}
7230 	if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
7231 		rack->rc_tmr_stopped = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
7232 		if (tcp_in_hpts(rack->rc_inp) &&
7233 		    ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)) {
7234 			/*
7235 			 * Canceling timer's when we have no output being
7236 			 * paced. We also must remove ourselves from the
7237 			 * hpts.
7238 			 */
7239 			tcp_hpts_remove(rack->rc_inp);
7240 			hpts_removed = 1;
7241 		}
7242 		rack->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK);
7243 	}
7244 	if (hpts_removed == 0)
7245 		rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry);
7246 }
7247 
7248 static void
7249 rack_timer_stop(struct tcpcb *tp, uint32_t timer_type)
7250 {
7251 	return;
7252 }
7253 
7254 static int
7255 rack_stopall(struct tcpcb *tp)
7256 {
7257 	struct tcp_rack *rack;
7258 	rack = (struct tcp_rack *)tp->t_fb_ptr;
7259 	rack->t_timers_stopped = 1;
7260 	return (0);
7261 }
7262 
7263 static void
7264 rack_timer_activate(struct tcpcb *tp, uint32_t timer_type, uint32_t delta)
7265 {
7266 	return;
7267 }
7268 
7269 static int
7270 rack_timer_active(struct tcpcb *tp, uint32_t timer_type)
7271 {
7272 	return (0);
7273 }
7274 
7275 static void
7276 rack_stop_all_timers(struct tcpcb *tp)
7277 {
7278 	struct tcp_rack *rack;
7279 
7280 	/*
7281 	 * Assure no timers are running.
7282 	 */
7283 	if (tcp_timer_active(tp, TT_PERSIST)) {
7284 		/* We enter in persists, set the flag appropriately */
7285 		rack = (struct tcp_rack *)tp->t_fb_ptr;
7286 		rack->rc_in_persist = 1;
7287 	}
7288 	tcp_timer_suspend(tp, TT_PERSIST);
7289 	tcp_timer_suspend(tp, TT_REXMT);
7290 	tcp_timer_suspend(tp, TT_KEEP);
7291 	tcp_timer_suspend(tp, TT_DELACK);
7292 }
7293 
7294 static void
7295 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
7296     struct rack_sendmap *rsm, uint64_t ts, uint16_t add_flag)
7297 {
7298 	int32_t idx;
7299 
7300 	rsm->r_rtr_cnt++;
7301 	rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
7302 	rsm->r_dupack = 0;
7303 	if (rsm->r_rtr_cnt > RACK_NUM_OF_RETRANS) {
7304 		rsm->r_rtr_cnt = RACK_NUM_OF_RETRANS;
7305 		rsm->r_flags |= RACK_OVERMAX;
7306 	}
7307 	if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & RACK_TLP) == 0)) {
7308 		rack->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start);
7309 		rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start);
7310 	}
7311 	idx = rsm->r_rtr_cnt - 1;
7312 	rsm->r_tim_lastsent[idx] = ts;
7313 	/*
7314 	 * Here we don't add in the len of send, since its already
7315 	 * in snduna <->snd_max.
7316 	 */
7317 	rsm->r_fas = ctf_flight_size(rack->rc_tp,
7318 				     rack->r_ctl.rc_sacked);
7319 	if (rsm->r_flags & RACK_ACKED) {
7320 		/* Problably MTU discovery messing with us */
7321 		rsm->r_flags &= ~RACK_ACKED;
7322 		rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
7323 	}
7324 	if (rsm->r_in_tmap) {
7325 		TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
7326 		rsm->r_in_tmap = 0;
7327 	}
7328 	TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
7329 	rsm->r_in_tmap = 1;
7330 	if (rsm->r_flags & RACK_SACK_PASSED) {
7331 		/* We have retransmitted due to the SACK pass */
7332 		rsm->r_flags &= ~RACK_SACK_PASSED;
7333 		rsm->r_flags |= RACK_WAS_SACKPASS;
7334 	}
7335 }
7336 
7337 static uint32_t
7338 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack,
7339     struct rack_sendmap *rsm, uint64_t ts, int32_t *lenp, uint16_t add_flag)
7340 {
7341 	/*
7342 	 * We (re-)transmitted starting at rsm->r_start for some length
7343 	 * (possibly less than r_end.
7344 	 */
7345 	struct rack_sendmap *nrsm;
7346 #ifdef INVARIANTS
7347 	struct rack_sendmap *insret;
7348 #endif
7349 	uint32_t c_end;
7350 	int32_t len;
7351 
7352 	len = *lenp;
7353 	c_end = rsm->r_start + len;
7354 	if (SEQ_GEQ(c_end, rsm->r_end)) {
7355 		/*
7356 		 * We retransmitted the whole piece or more than the whole
7357 		 * slopping into the next rsm.
7358 		 */
7359 		rack_update_rsm(tp, rack, rsm, ts, add_flag);
7360 		if (c_end == rsm->r_end) {
7361 			*lenp = 0;
7362 			return (0);
7363 		} else {
7364 			int32_t act_len;
7365 
7366 			/* Hangs over the end return whats left */
7367 			act_len = rsm->r_end - rsm->r_start;
7368 			*lenp = (len - act_len);
7369 			return (rsm->r_end);
7370 		}
7371 		/* We don't get out of this block. */
7372 	}
7373 	/*
7374 	 * Here we retransmitted less than the whole thing which means we
7375 	 * have to split this into what was transmitted and what was not.
7376 	 */
7377 	nrsm = rack_alloc_full_limit(rack);
7378 	if (nrsm == NULL) {
7379 		/*
7380 		 * We can't get memory, so lets not proceed.
7381 		 */
7382 		*lenp = 0;
7383 		return (0);
7384 	}
7385 	/*
7386 	 * So here we are going to take the original rsm and make it what we
7387 	 * retransmitted. nrsm will be the tail portion we did not
7388 	 * retransmit. For example say the chunk was 1, 11 (10 bytes). And
7389 	 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to
7390 	 * 1, 6 and the new piece will be 6, 11.
7391 	 */
7392 	rack_clone_rsm(rack, nrsm, rsm, c_end);
7393 	nrsm->r_dupack = 0;
7394 	rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2);
7395 #ifndef INVARIANTS
7396 	(void)RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
7397 #else
7398 	insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
7399 	if (insret != NULL) {
7400 		panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
7401 		      nrsm, insret, rack, rsm);
7402 	}
7403 #endif
7404 	if (rsm->r_in_tmap) {
7405 		TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7406 		nrsm->r_in_tmap = 1;
7407 	}
7408 	rsm->r_flags &= (~RACK_HAS_FIN);
7409 	rack_update_rsm(tp, rack, rsm, ts, add_flag);
7410 	/* Log a split of rsm into rsm and nrsm */
7411 	rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
7412 	*lenp = 0;
7413 	return (0);
7414 }
7415 
7416 static void
7417 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
7418 		uint32_t seq_out, uint16_t th_flags, int32_t err, uint64_t cts,
7419 		struct rack_sendmap *hintrsm, uint16_t add_flag, struct mbuf *s_mb, uint32_t s_moff, int hw_tls)
7420 {
7421 	struct tcp_rack *rack;
7422 	struct rack_sendmap *rsm, *nrsm, fe;
7423 #ifdef INVARIANTS
7424 	struct rack_sendmap *insret;
7425 #endif
7426 	register uint32_t snd_max, snd_una;
7427 
7428 	/*
7429 	 * Add to the RACK log of packets in flight or retransmitted. If
7430 	 * there is a TS option we will use the TS echoed, if not we will
7431 	 * grab a TS.
7432 	 *
7433 	 * Retransmissions will increment the count and move the ts to its
7434 	 * proper place. Note that if options do not include TS's then we
7435 	 * won't be able to effectively use the ACK for an RTT on a retran.
7436 	 *
7437 	 * Notes about r_start and r_end. Lets consider a send starting at
7438 	 * sequence 1 for 10 bytes. In such an example the r_start would be
7439 	 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11.
7440 	 * This means that r_end is actually the first sequence for the next
7441 	 * slot (11).
7442 	 *
7443 	 */
7444 	/*
7445 	 * If err is set what do we do XXXrrs? should we not add the thing?
7446 	 * -- i.e. return if err != 0 or should we pretend we sent it? --
7447 	 * i.e. proceed with add ** do this for now.
7448 	 */
7449 	INP_WLOCK_ASSERT(tp->t_inpcb);
7450 	if (err)
7451 		/*
7452 		 * We don't log errors -- we could but snd_max does not
7453 		 * advance in this case either.
7454 		 */
7455 		return;
7456 
7457 	if (th_flags & TH_RST) {
7458 		/*
7459 		 * We don't log resets and we return immediately from
7460 		 * sending
7461 		 */
7462 		return;
7463 	}
7464 	rack = (struct tcp_rack *)tp->t_fb_ptr;
7465 	snd_una = tp->snd_una;
7466 	snd_max = tp->snd_max;
7467 	if (th_flags & (TH_SYN | TH_FIN)) {
7468 		/*
7469 		 * The call to rack_log_output is made before bumping
7470 		 * snd_max. This means we can record one extra byte on a SYN
7471 		 * or FIN if seq_out is adding more on and a FIN is present
7472 		 * (and we are not resending).
7473 		 */
7474 		if ((th_flags & TH_SYN) && (seq_out == tp->iss))
7475 			len++;
7476 		if (th_flags & TH_FIN)
7477 			len++;
7478 		if (SEQ_LT(snd_max, tp->snd_nxt)) {
7479 			/*
7480 			 * The add/update as not been done for the FIN/SYN
7481 			 * yet.
7482 			 */
7483 			snd_max = tp->snd_nxt;
7484 		}
7485 	}
7486 	if (SEQ_LEQ((seq_out + len), snd_una)) {
7487 		/* Are sending an old segment to induce an ack (keep-alive)? */
7488 		return;
7489 	}
7490 	if (SEQ_LT(seq_out, snd_una)) {
7491 		/* huh? should we panic? */
7492 		uint32_t end;
7493 
7494 		end = seq_out + len;
7495 		seq_out = snd_una;
7496 		if (SEQ_GEQ(end, seq_out))
7497 			len = end - seq_out;
7498 		else
7499 			len = 0;
7500 	}
7501 	if (len == 0) {
7502 		/* We don't log zero window probes */
7503 		return;
7504 	}
7505 	rack->r_ctl.rc_time_last_sent = cts;
7506 	if (IN_FASTRECOVERY(tp->t_flags)) {
7507 		rack->r_ctl.rc_prr_out += len;
7508 	}
7509 	/* First question is it a retransmission or new? */
7510 	if (seq_out == snd_max) {
7511 		/* Its new */
7512 again:
7513 		rsm = rack_alloc(rack);
7514 		if (rsm == NULL) {
7515 			/*
7516 			 * Hmm out of memory and the tcb got destroyed while
7517 			 * we tried to wait.
7518 			 */
7519 			return;
7520 		}
7521 		if (th_flags & TH_FIN) {
7522 			rsm->r_flags = RACK_HAS_FIN|add_flag;
7523 		} else {
7524 			rsm->r_flags = add_flag;
7525 		}
7526 		if (hw_tls)
7527 			rsm->r_hw_tls = 1;
7528 		rsm->r_tim_lastsent[0] = cts;
7529 		rsm->r_rtr_cnt = 1;
7530 		rsm->r_rtr_bytes = 0;
7531 		if (th_flags & TH_SYN) {
7532 			/* The data space is one beyond snd_una */
7533 			rsm->r_flags |= RACK_HAS_SYN;
7534 		}
7535 		rsm->r_start = seq_out;
7536 		rsm->r_end = rsm->r_start + len;
7537 		rsm->r_dupack = 0;
7538 		/*
7539 		 * save off the mbuf location that
7540 		 * sndmbuf_noadv returned (which is
7541 		 * where we started copying from)..
7542 		 */
7543 		rsm->m = s_mb;
7544 		rsm->soff = s_moff;
7545 		/*
7546 		 * Here we do add in the len of send, since its not yet
7547 		 * reflected in in snduna <->snd_max
7548 		 */
7549 		rsm->r_fas = (ctf_flight_size(rack->rc_tp,
7550 					      rack->r_ctl.rc_sacked) +
7551 			      (rsm->r_end - rsm->r_start));
7552 		/* rsm->m will be NULL if RACK_HAS_SYN or RACK_HAS_FIN is set */
7553 		if (rsm->m) {
7554 			if (rsm->m->m_len <= rsm->soff) {
7555 				/*
7556 				 * XXXrrs Question, will this happen?
7557 				 *
7558 				 * If sbsndptr is set at the correct place
7559 				 * then s_moff should always be somewhere
7560 				 * within rsm->m. But if the sbsndptr was
7561 				 * off then that won't be true. If it occurs
7562 				 * we need to walkout to the correct location.
7563 				 */
7564 				struct mbuf *lm;
7565 
7566 				lm = rsm->m;
7567 				while (lm->m_len <= rsm->soff) {
7568 					rsm->soff -= lm->m_len;
7569 					lm = lm->m_next;
7570 					KASSERT(lm != NULL, ("%s rack:%p lm goes null orig_off:%u origmb:%p rsm->soff:%u",
7571 							     __func__, rack, s_moff, s_mb, rsm->soff));
7572 				}
7573 				rsm->m = lm;
7574 				counter_u64_add(rack_sbsndptr_wrong, 1);
7575 			} else
7576 				counter_u64_add(rack_sbsndptr_right, 1);
7577 			rsm->orig_m_len = rsm->m->m_len;
7578 		} else
7579 			rsm->orig_m_len = 0;
7580 		rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
7581 		/* Log a new rsm */
7582 		rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_NEW, 0, __LINE__);
7583 #ifndef INVARIANTS
7584 		(void)RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7585 #else
7586 		insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7587 		if (insret != NULL) {
7588 			panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
7589 			      nrsm, insret, rack, rsm);
7590 		}
7591 #endif
7592 		TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
7593 		rsm->r_in_tmap = 1;
7594 		/*
7595 		 * Special case detection, is there just a single
7596 		 * packet outstanding when we are not in recovery?
7597 		 *
7598 		 * If this is true mark it so.
7599 		 */
7600 		if ((IN_FASTRECOVERY(tp->t_flags) == 0) &&
7601 		    (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) == ctf_fixed_maxseg(tp))) {
7602 			struct rack_sendmap *prsm;
7603 
7604 			prsm = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7605 			if (prsm)
7606 				prsm->r_one_out_nr = 1;
7607 		}
7608 		return;
7609 	}
7610 	/*
7611 	 * If we reach here its a retransmission and we need to find it.
7612 	 */
7613 	memset(&fe, 0, sizeof(fe));
7614 more:
7615 	if (hintrsm && (hintrsm->r_start == seq_out)) {
7616 		rsm = hintrsm;
7617 		hintrsm = NULL;
7618 	} else {
7619 		/* No hints sorry */
7620 		rsm = NULL;
7621 	}
7622 	if ((rsm) && (rsm->r_start == seq_out)) {
7623 		seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag);
7624 		if (len == 0) {
7625 			return;
7626 		} else {
7627 			goto more;
7628 		}
7629 	}
7630 	/* Ok it was not the last pointer go through it the hard way. */
7631 refind:
7632 	fe.r_start = seq_out;
7633 	rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
7634 	if (rsm) {
7635 		if (rsm->r_start == seq_out) {
7636 			seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag);
7637 			if (len == 0) {
7638 				return;
7639 			} else {
7640 				goto refind;
7641 			}
7642 		}
7643 		if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) {
7644 			/* Transmitted within this piece */
7645 			/*
7646 			 * Ok we must split off the front and then let the
7647 			 * update do the rest
7648 			 */
7649 			nrsm = rack_alloc_full_limit(rack);
7650 			if (nrsm == NULL) {
7651 				rack_update_rsm(tp, rack, rsm, cts, add_flag);
7652 				return;
7653 			}
7654 			/*
7655 			 * copy rsm to nrsm and then trim the front of rsm
7656 			 * to not include this part.
7657 			 */
7658 			rack_clone_rsm(rack, nrsm, rsm, seq_out);
7659 			rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
7660 #ifndef INVARIANTS
7661 			(void)RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
7662 #else
7663 			insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
7664 			if (insret != NULL) {
7665 				panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
7666 				      nrsm, insret, rack, rsm);
7667 			}
7668 #endif
7669 			if (rsm->r_in_tmap) {
7670 				TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7671 				nrsm->r_in_tmap = 1;
7672 			}
7673 			rsm->r_flags &= (~RACK_HAS_FIN);
7674 			seq_out = rack_update_entry(tp, rack, nrsm, cts, &len, add_flag);
7675 			if (len == 0) {
7676 				return;
7677 			} else if (len > 0)
7678 				goto refind;
7679 		}
7680 	}
7681 	/*
7682 	 * Hmm not found in map did they retransmit both old and on into the
7683 	 * new?
7684 	 */
7685 	if (seq_out == tp->snd_max) {
7686 		goto again;
7687 	} else if (SEQ_LT(seq_out, tp->snd_max)) {
7688 #ifdef INVARIANTS
7689 		printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n",
7690 		       seq_out, len, tp->snd_una, tp->snd_max);
7691 		printf("Starting Dump of all rack entries\n");
7692 		RB_FOREACH(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
7693 			printf("rsm:%p start:%u end:%u\n",
7694 			       rsm, rsm->r_start, rsm->r_end);
7695 		}
7696 		printf("Dump complete\n");
7697 		panic("seq_out not found rack:%p tp:%p",
7698 		      rack, tp);
7699 #endif
7700 	} else {
7701 #ifdef INVARIANTS
7702 		/*
7703 		 * Hmm beyond sndmax? (only if we are using the new rtt-pack
7704 		 * flag)
7705 		 */
7706 		panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p",
7707 		      seq_out, len, tp->snd_max, tp);
7708 #endif
7709 	}
7710 }
7711 
7712 /*
7713  * Record one of the RTT updates from an ack into
7714  * our sample structure.
7715  */
7716 
7717 static void
7718 tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt, uint32_t len, uint32_t us_rtt,
7719 		    int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt)
7720 {
7721 	if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
7722 	    (rack->r_ctl.rack_rs.rs_rtt_lowest > rtt)) {
7723 		rack->r_ctl.rack_rs.rs_rtt_lowest = rtt;
7724 	}
7725 	if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
7726 	    (rack->r_ctl.rack_rs.rs_rtt_highest < rtt)) {
7727 		rack->r_ctl.rack_rs.rs_rtt_highest = rtt;
7728 	}
7729 	if (rack->rc_tp->t_flags & TF_GPUTINPROG) {
7730 	    if (us_rtt < rack->r_ctl.rc_gp_lowrtt)
7731 		rack->r_ctl.rc_gp_lowrtt = us_rtt;
7732 	    if (rack->rc_tp->snd_wnd > rack->r_ctl.rc_gp_high_rwnd)
7733 		    rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
7734 	}
7735 	if ((confidence == 1) &&
7736 	    ((rsm == NULL) ||
7737 	     (rsm->r_just_ret) ||
7738 	     (rsm->r_one_out_nr &&
7739 	      len < (ctf_fixed_maxseg(rack->rc_tp) * 2)))) {
7740 		/*
7741 		 * If the rsm had a just return
7742 		 * hit it then we can't trust the
7743 		 * rtt measurement for buffer deterimination
7744 		 * Note that a confidence of 2, indicates
7745 		 * SACK'd which overrides the r_just_ret or
7746 		 * the r_one_out_nr. If it was a CUM-ACK and
7747 		 * we had only two outstanding, but get an
7748 		 * ack for only 1. Then that also lowers our
7749 		 * confidence.
7750 		 */
7751 		confidence = 0;
7752 	}
7753 	if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
7754 	    (rack->r_ctl.rack_rs.rs_us_rtt > us_rtt)) {
7755 		if (rack->r_ctl.rack_rs.confidence == 0) {
7756 			/*
7757 			 * We take anything with no current confidence
7758 			 * saved.
7759 			 */
7760 			rack->r_ctl.rack_rs.rs_us_rtt = us_rtt;
7761 			rack->r_ctl.rack_rs.confidence = confidence;
7762 			rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt;
7763 		} else if (confidence || rack->r_ctl.rack_rs.confidence) {
7764 			/*
7765 			 * Once we have a confident number,
7766 			 * we can update it with a smaller
7767 			 * value since this confident number
7768 			 * may include the DSACK time until
7769 			 * the next segment (the second one) arrived.
7770 			 */
7771 			rack->r_ctl.rack_rs.rs_us_rtt = us_rtt;
7772 			rack->r_ctl.rack_rs.confidence = confidence;
7773 			rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt;
7774 		}
7775 	}
7776 	rack_log_rtt_upd(rack->rc_tp, rack, us_rtt, len, rsm, confidence);
7777 	rack->r_ctl.rack_rs.rs_flags = RACK_RTT_VALID;
7778 	rack->r_ctl.rack_rs.rs_rtt_tot += rtt;
7779 	rack->r_ctl.rack_rs.rs_rtt_cnt++;
7780 }
7781 
7782 /*
7783  * Collect new round-trip time estimate
7784  * and update averages and current timeout.
7785  */
7786 static void
7787 tcp_rack_xmit_timer_commit(struct tcp_rack *rack, struct tcpcb *tp)
7788 {
7789 	int32_t delta;
7790 	int32_t rtt;
7791 
7792 	if (rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY)
7793 		/* No valid sample */
7794 		return;
7795 	if (rack->r_ctl.rc_rate_sample_method == USE_RTT_LOW) {
7796 		/* We are to use the lowest RTT seen in a single ack */
7797 		rtt = rack->r_ctl.rack_rs.rs_rtt_lowest;
7798 	} else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_HIGH) {
7799 		/* We are to use the highest RTT seen in a single ack */
7800 		rtt = rack->r_ctl.rack_rs.rs_rtt_highest;
7801 	} else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_AVG) {
7802 		/* We are to use the average RTT seen in a single ack */
7803 		rtt = (int32_t)(rack->r_ctl.rack_rs.rs_rtt_tot /
7804 				(uint64_t)rack->r_ctl.rack_rs.rs_rtt_cnt);
7805 	} else {
7806 #ifdef INVARIANTS
7807 		panic("Unknown rtt variant %d", rack->r_ctl.rc_rate_sample_method);
7808 #endif
7809 		return;
7810 	}
7811 	if (rtt == 0)
7812 		rtt = 1;
7813 	if (rack->rc_gp_rtt_set == 0) {
7814 		/*
7815 		 * With no RTT we have to accept
7816 		 * even one we are not confident of.
7817 		 */
7818 		rack->r_ctl.rc_gp_srtt = rack->r_ctl.rack_rs.rs_us_rtt;
7819 		rack->rc_gp_rtt_set = 1;
7820 	} else if (rack->r_ctl.rack_rs.confidence) {
7821 		/* update the running gp srtt */
7822 		rack->r_ctl.rc_gp_srtt -= (rack->r_ctl.rc_gp_srtt/8);
7823 		rack->r_ctl.rc_gp_srtt += rack->r_ctl.rack_rs.rs_us_rtt / 8;
7824 	}
7825 	if (rack->r_ctl.rack_rs.confidence) {
7826 		/*
7827 		 * record the low and high for highly buffered path computation,
7828 		 * we only do this if we are confident (not a retransmission).
7829 		 */
7830 		if (rack->r_ctl.rc_highest_us_rtt < rack->r_ctl.rack_rs.rs_us_rtt) {
7831 			rack->r_ctl.rc_highest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
7832 		}
7833 		if (rack->rc_highly_buffered == 0) {
7834 			/*
7835 			 * Currently once we declare a path has
7836 			 * highly buffered there is no going
7837 			 * back, which may be a problem...
7838 			 */
7839 			if ((rack->r_ctl.rc_highest_us_rtt / rack->r_ctl.rc_lowest_us_rtt) > rack_hbp_thresh) {
7840 				rack_log_rtt_shrinks(rack, rack->r_ctl.rack_rs.rs_us_rtt,
7841 						     rack->r_ctl.rc_highest_us_rtt,
7842 						     rack->r_ctl.rc_lowest_us_rtt,
7843 						     RACK_RTTS_SEEHBP);
7844 				rack->rc_highly_buffered = 1;
7845 			}
7846 		}
7847 	}
7848 	if ((rack->r_ctl.rack_rs.confidence) ||
7849 	    (rack->r_ctl.rack_rs.rs_us_rtrcnt == 1)) {
7850 		/*
7851 		 * If we are highly confident of it <or> it was
7852 		 * never retransmitted we accept it as the last us_rtt.
7853 		 */
7854 		rack->r_ctl.rc_last_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
7855 		/* The lowest rtt can be set if its was not retransmited */
7856 		if (rack->r_ctl.rc_lowest_us_rtt > rack->r_ctl.rack_rs.rs_us_rtt) {
7857 			rack->r_ctl.rc_lowest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
7858 			if (rack->r_ctl.rc_lowest_us_rtt == 0)
7859 				rack->r_ctl.rc_lowest_us_rtt = 1;
7860 		}
7861 	}
7862 	rack = (struct tcp_rack *)tp->t_fb_ptr;
7863 	if (tp->t_srtt != 0) {
7864 		/*
7865 		 * We keep a simple srtt in microseconds, like our rtt
7866 		 * measurement. We don't need to do any tricks with shifting
7867 		 * etc. Instead we just add in 1/8th of the new measurement
7868 		 * and subtract out 1/8 of the old srtt. We do the same with
7869 		 * the variance after finding the absolute value of the
7870 		 * difference between this sample and the current srtt.
7871 		 */
7872 		delta = tp->t_srtt - rtt;
7873 		/* Take off 1/8th of the current sRTT */
7874 		tp->t_srtt -= (tp->t_srtt >> 3);
7875 		/* Add in 1/8th of the new RTT just measured */
7876 		tp->t_srtt += (rtt >> 3);
7877 		if (tp->t_srtt <= 0)
7878 			tp->t_srtt = 1;
7879 		/* Now lets make the absolute value of the variance */
7880 		if (delta < 0)
7881 			delta = -delta;
7882 		/* Subtract out 1/8th */
7883 		tp->t_rttvar -= (tp->t_rttvar >> 3);
7884 		/* Add in 1/8th of the new variance we just saw */
7885 		tp->t_rttvar += (delta >> 3);
7886 		if (tp->t_rttvar <= 0)
7887 			tp->t_rttvar = 1;
7888 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
7889 			tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
7890 	} else {
7891 		/*
7892 		 * No rtt measurement yet - use the unsmoothed rtt. Set the
7893 		 * variance to half the rtt (so our first retransmit happens
7894 		 * at 3*rtt).
7895 		 */
7896 		tp->t_srtt = rtt;
7897 		tp->t_rttvar = rtt >> 1;
7898 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
7899 	}
7900 	rack->rc_srtt_measure_made = 1;
7901 	KMOD_TCPSTAT_INC(tcps_rttupdated);
7902 	tp->t_rttupdated++;
7903 #ifdef STATS
7904 	if (rack_stats_gets_ms_rtt == 0) {
7905 		/* Send in the microsecond rtt used for rxt timeout purposes */
7906 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt));
7907 	} else if (rack_stats_gets_ms_rtt == 1) {
7908 		/* Send in the millisecond rtt used for rxt timeout purposes */
7909 		int32_t ms_rtt;
7910 
7911 		/* Round up */
7912 		ms_rtt = (rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC;
7913 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt));
7914 	} else if (rack_stats_gets_ms_rtt == 2) {
7915 		/* Send in the millisecond rtt has close to the path RTT as we can get  */
7916 		int32_t ms_rtt;
7917 
7918 		/* Round up */
7919 		ms_rtt = (rack->r_ctl.rack_rs.rs_us_rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC;
7920 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt));
7921 	}  else {
7922 		/* Send in the microsecond rtt has close to the path RTT as we can get  */
7923 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rack->r_ctl.rack_rs.rs_us_rtt));
7924 	}
7925 
7926 #endif
7927 	/*
7928 	 * the retransmit should happen at rtt + 4 * rttvar. Because of the
7929 	 * way we do the smoothing, srtt and rttvar will each average +1/2
7930 	 * tick of bias.  When we compute the retransmit timer, we want 1/2
7931 	 * tick of rounding and 1 extra tick because of +-1/2 tick
7932 	 * uncertainty in the firing of the timer.  The bias will give us
7933 	 * exactly the 1.5 tick we need.  But, because the bias is
7934 	 * statistical, we have to test that we don't drop below the minimum
7935 	 * feasible timer (which is 2 ticks).
7936 	 */
7937 	tp->t_rxtshift = 0;
7938 	RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
7939 		      max(rack_rto_min, rtt + 2), rack_rto_max, rack->r_ctl.timer_slop);
7940 	rack_log_rtt_sample(rack, rtt);
7941 	tp->t_softerror = 0;
7942 }
7943 
7944 
7945 static void
7946 rack_apply_updated_usrtt(struct tcp_rack *rack, uint32_t us_rtt, uint32_t us_cts)
7947 {
7948 	/*
7949 	 * Apply to filter the inbound us-rtt at us_cts.
7950 	 */
7951 	uint32_t old_rtt;
7952 
7953 	old_rtt = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
7954 	apply_filter_min_small(&rack->r_ctl.rc_gp_min_rtt,
7955 			       us_rtt, us_cts);
7956 	if (rack->r_ctl.last_pacing_time &&
7957 	    rack->rc_gp_dyn_mul &&
7958 	    (rack->r_ctl.last_pacing_time > us_rtt))
7959 		rack->pacing_longer_than_rtt = 1;
7960 	else
7961 		rack->pacing_longer_than_rtt = 0;
7962 	if (old_rtt > us_rtt) {
7963 		/* We just hit a new lower rtt time */
7964 		rack_log_rtt_shrinks(rack,  us_cts,  old_rtt,
7965 				     __LINE__, RACK_RTTS_NEWRTT);
7966 		/*
7967 		 * Only count it if its lower than what we saw within our
7968 		 * calculated range.
7969 		 */
7970 		if ((old_rtt - us_rtt) > rack_min_rtt_movement) {
7971 			if (rack_probertt_lower_within &&
7972 			    rack->rc_gp_dyn_mul &&
7973 			    (rack->use_fixed_rate == 0) &&
7974 			    (rack->rc_always_pace)) {
7975 				/*
7976 				 * We are seeing a new lower rtt very close
7977 				 * to the time that we would have entered probe-rtt.
7978 				 * This is probably due to the fact that a peer flow
7979 				 * has entered probe-rtt. Lets go in now too.
7980 				 */
7981 				uint32_t val;
7982 
7983 				val = rack_probertt_lower_within * rack_time_between_probertt;
7984 				val /= 100;
7985 				if ((rack->in_probe_rtt == 0)  &&
7986 				    ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= (rack_time_between_probertt - val)))	{
7987 					rack_enter_probertt(rack, us_cts);
7988 				}
7989 			}
7990 			rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
7991 		}
7992 	}
7993 }
7994 
7995 static int
7996 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack,
7997     struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack)
7998 {
7999 	uint32_t us_rtt;
8000 	int32_t i, all;
8001 	uint32_t t, len_acked;
8002 
8003 	if ((rsm->r_flags & RACK_ACKED) ||
8004 	    (rsm->r_flags & RACK_WAS_ACKED))
8005 		/* Already done */
8006 		return (0);
8007 	if (rsm->r_no_rtt_allowed) {
8008 		/* Not allowed */
8009 		return (0);
8010 	}
8011 	if (ack_type == CUM_ACKED) {
8012 		if (SEQ_GT(th_ack, rsm->r_end)) {
8013 			len_acked = rsm->r_end - rsm->r_start;
8014 			all = 1;
8015 		} else {
8016 			len_acked = th_ack - rsm->r_start;
8017 			all = 0;
8018 		}
8019 	} else {
8020 		len_acked = rsm->r_end - rsm->r_start;
8021 		all = 0;
8022 	}
8023 	if (rsm->r_rtr_cnt == 1) {
8024 
8025 		t = cts - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
8026 		if ((int)t <= 0)
8027 			t = 1;
8028 		if (!tp->t_rttlow || tp->t_rttlow > t)
8029 			tp->t_rttlow = t;
8030 		if (!rack->r_ctl.rc_rack_min_rtt ||
8031 		    SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
8032 			rack->r_ctl.rc_rack_min_rtt = t;
8033 			if (rack->r_ctl.rc_rack_min_rtt == 0) {
8034 				rack->r_ctl.rc_rack_min_rtt = 1;
8035 			}
8036 		}
8037 		if (TSTMP_GT(tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time), rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]))
8038 			us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8039 		else
8040 			us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8041 		if (us_rtt == 0)
8042 			us_rtt = 1;
8043 		if (CC_ALGO(tp)->rttsample != NULL) {
8044 			/* Kick the RTT to the CC */
8045 			CC_ALGO(tp)->rttsample(tp->ccv, us_rtt, 1, rsm->r_fas);
8046 		}
8047 		rack_apply_updated_usrtt(rack, us_rtt, tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time));
8048 		if (ack_type == SACKED) {
8049 			rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 1);
8050 			tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt, 2 , rsm, rsm->r_rtr_cnt);
8051 		} else {
8052 			/*
8053 			 * We need to setup what our confidence
8054 			 * is in this ack.
8055 			 *
8056 			 * If the rsm was app limited and it is
8057 			 * less than a mss in length (the end
8058 			 * of the send) then we have a gap. If we
8059 			 * were app limited but say we were sending
8060 			 * multiple MSS's then we are more confident
8061 			 * int it.
8062 			 *
8063 			 * When we are not app-limited then we see if
8064 			 * the rsm is being included in the current
8065 			 * measurement, we tell this by the app_limited_needs_set
8066 			 * flag.
8067 			 *
8068 			 * Note that being cwnd blocked is not applimited
8069 			 * as well as the pacing delay between packets which
8070 			 * are sending only 1 or 2 MSS's also will show up
8071 			 * in the RTT. We probably need to examine this algorithm
8072 			 * a bit more and enhance it to account for the delay
8073 			 * between rsm's. We could do that by saving off the
8074 			 * pacing delay of each rsm (in an rsm) and then
8075 			 * factoring that in somehow though for now I am
8076 			 * not sure how :)
8077 			 */
8078 			int calc_conf = 0;
8079 
8080 			if (rsm->r_flags & RACK_APP_LIMITED) {
8081 				if (all && (len_acked <= ctf_fixed_maxseg(tp)))
8082 					calc_conf = 0;
8083 				else
8084 					calc_conf = 1;
8085 			} else if (rack->app_limited_needs_set == 0) {
8086 				calc_conf = 1;
8087 			} else {
8088 				calc_conf = 0;
8089 			}
8090 			rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 2);
8091 			tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt,
8092 					    calc_conf, rsm, rsm->r_rtr_cnt);
8093 		}
8094 		if ((rsm->r_flags & RACK_TLP) &&
8095 		    (!IN_FASTRECOVERY(tp->t_flags))) {
8096 			/* Segment was a TLP and our retrans matched */
8097 			if (rack->r_ctl.rc_tlp_cwnd_reduce) {
8098 				rack->r_ctl.rc_rsm_start = tp->snd_max;
8099 				rack->r_ctl.rc_cwnd_at = tp->snd_cwnd;
8100 				rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh;
8101 				rack_cong_signal(tp, CC_NDUPACK, tp->snd_una);
8102 			}
8103 		}
8104 		if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)])) {
8105 			/* New more recent rack_tmit_time */
8106 			rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
8107 			rack->rc_rack_rtt = t;
8108 		}
8109 		return (1);
8110 	}
8111 	/*
8112 	 * We clear the soft/rxtshift since we got an ack.
8113 	 * There is no assurance we will call the commit() function
8114 	 * so we need to clear these to avoid incorrect handling.
8115 	 */
8116 	tp->t_rxtshift = 0;
8117 	RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
8118 		      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
8119 	tp->t_softerror = 0;
8120 	if (to && (to->to_flags & TOF_TS) &&
8121 	    (ack_type == CUM_ACKED) &&
8122 	    (to->to_tsecr) &&
8123 	    ((rsm->r_flags & RACK_OVERMAX) == 0)) {
8124 		/*
8125 		 * Now which timestamp does it match? In this block the ACK
8126 		 * must be coming from a previous transmission.
8127 		 */
8128 		for (i = 0; i < rsm->r_rtr_cnt; i++) {
8129 			if (rack_ts_to_msec(rsm->r_tim_lastsent[i]) == to->to_tsecr) {
8130 				t = cts - (uint32_t)rsm->r_tim_lastsent[i];
8131 				if ((int)t <= 0)
8132 					t = 1;
8133 				if (CC_ALGO(tp)->rttsample != NULL) {
8134 					/*
8135 					 * Kick the RTT to the CC, here
8136 					 * we lie a bit in that we know the
8137 					 * retransmission is correct even though
8138 					 * we retransmitted. This is because
8139 					 * we match the timestamps.
8140 					 */
8141 					if (TSTMP_GT(tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time), rsm->r_tim_lastsent[i]))
8142 						us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - (uint32_t)rsm->r_tim_lastsent[i];
8143 					else
8144 						us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[i];
8145 					CC_ALGO(tp)->rttsample(tp->ccv, us_rtt, 1, rsm->r_fas);
8146 				}
8147 				if ((i + 1) < rsm->r_rtr_cnt) {
8148 					/*
8149 					 * The peer ack'd from our previous
8150 					 * transmission. We have a spurious
8151 					 * retransmission and thus we dont
8152 					 * want to update our rack_rtt.
8153 					 *
8154 					 * Hmm should there be a CC revert here?
8155 					 *
8156 					 */
8157 					return (0);
8158 				}
8159 				if (!tp->t_rttlow || tp->t_rttlow > t)
8160 					tp->t_rttlow = t;
8161 				if (!rack->r_ctl.rc_rack_min_rtt || SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
8162 					rack->r_ctl.rc_rack_min_rtt = t;
8163 					if (rack->r_ctl.rc_rack_min_rtt == 0) {
8164 						rack->r_ctl.rc_rack_min_rtt = 1;
8165 					}
8166 				}
8167 				if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
8168 					   (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)])) {
8169 					/* New more recent rack_tmit_time */
8170 					rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
8171 					rack->rc_rack_rtt = t;
8172 				}
8173 				rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[i], cts, 3);
8174 				tcp_rack_xmit_timer(rack, t + 1, len_acked, t, 0, rsm,
8175 						    rsm->r_rtr_cnt);
8176 				return (1);
8177 			}
8178 		}
8179 		goto ts_not_found;
8180 	} else {
8181 		/*
8182 		 * Ok its a SACK block that we retransmitted. or a windows
8183 		 * machine without timestamps. We can tell nothing from the
8184 		 * time-stamp since its not there or the time the peer last
8185 		 * recieved a segment that moved forward its cum-ack point.
8186 		 */
8187 ts_not_found:
8188 		i = rsm->r_rtr_cnt - 1;
8189 		t = cts - (uint32_t)rsm->r_tim_lastsent[i];
8190 		if ((int)t <= 0)
8191 			t = 1;
8192 		if (rack->r_ctl.rc_rack_min_rtt && SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
8193 			/*
8194 			 * We retransmitted and the ack came back in less
8195 			 * than the smallest rtt we have observed. We most
8196 			 * likely did an improper retransmit as outlined in
8197 			 * 6.2 Step 2 point 2 in the rack-draft so we
8198 			 * don't want to update our rack_rtt. We in
8199 			 * theory (in future) might want to think about reverting our
8200 			 * cwnd state but we won't for now.
8201 			 */
8202 			return (0);
8203 		} else if (rack->r_ctl.rc_rack_min_rtt) {
8204 			/*
8205 			 * We retransmitted it and the retransmit did the
8206 			 * job.
8207 			 */
8208 			if (!rack->r_ctl.rc_rack_min_rtt ||
8209 			    SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
8210 				rack->r_ctl.rc_rack_min_rtt = t;
8211 				if (rack->r_ctl.rc_rack_min_rtt == 0) {
8212 					rack->r_ctl.rc_rack_min_rtt = 1;
8213 				}
8214 			}
8215 			if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time, (uint32_t)rsm->r_tim_lastsent[i])) {
8216 				/* New more recent rack_tmit_time */
8217 				rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[i];
8218 				rack->rc_rack_rtt = t;
8219 			}
8220 			return (1);
8221 		}
8222 	}
8223 	return (0);
8224 }
8225 
8226 /*
8227  * Mark the SACK_PASSED flag on all entries prior to rsm send wise.
8228  */
8229 static void
8230 rack_log_sack_passed(struct tcpcb *tp,
8231     struct tcp_rack *rack, struct rack_sendmap *rsm)
8232 {
8233 	struct rack_sendmap *nrsm;
8234 
8235 	nrsm = rsm;
8236 	TAILQ_FOREACH_REVERSE_FROM(nrsm, &rack->r_ctl.rc_tmap,
8237 	    rack_head, r_tnext) {
8238 		if (nrsm == rsm) {
8239 			/* Skip orginal segment he is acked */
8240 			continue;
8241 		}
8242 		if (nrsm->r_flags & RACK_ACKED) {
8243 			/*
8244 			 * Skip ack'd segments, though we
8245 			 * should not see these, since tmap
8246 			 * should not have ack'd segments.
8247 			 */
8248 			continue;
8249 		}
8250 		if (nrsm->r_flags & RACK_SACK_PASSED) {
8251 			/*
8252 			 * We found one that is already marked
8253 			 * passed, we have been here before and
8254 			 * so all others below this are marked.
8255 			 */
8256 			break;
8257 		}
8258 		nrsm->r_flags |= RACK_SACK_PASSED;
8259 		nrsm->r_flags &= ~RACK_WAS_SACKPASS;
8260 	}
8261 }
8262 
8263 static void
8264 rack_need_set_test(struct tcpcb *tp,
8265 		   struct tcp_rack *rack,
8266 		   struct rack_sendmap *rsm,
8267 		   tcp_seq th_ack,
8268 		   int line,
8269 		   int use_which)
8270 {
8271 
8272 	if ((tp->t_flags & TF_GPUTINPROG) &&
8273 	    SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
8274 		/*
8275 		 * We were app limited, and this ack
8276 		 * butts up or goes beyond the point where we want
8277 		 * to start our next measurement. We need
8278 		 * to record the new gput_ts as here and
8279 		 * possibly update the start sequence.
8280 		 */
8281 		uint32_t seq, ts;
8282 
8283 		if (rsm->r_rtr_cnt > 1) {
8284 			/*
8285 			 * This is a retransmit, can we
8286 			 * really make any assessment at this
8287 			 * point?  We are not really sure of
8288 			 * the timestamp, is it this or the
8289 			 * previous transmission?
8290 			 *
8291 			 * Lets wait for something better that
8292 			 * is not retransmitted.
8293 			 */
8294 			return;
8295 		}
8296 		seq = tp->gput_seq;
8297 		ts = tp->gput_ts;
8298 		rack->app_limited_needs_set = 0;
8299 		tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
8300 		/* Do we start at a new end? */
8301 		if ((use_which == RACK_USE_BEG) &&
8302 		    SEQ_GEQ(rsm->r_start, tp->gput_seq)) {
8303 			/*
8304 			 * When we get an ACK that just eats
8305 			 * up some of the rsm, we set RACK_USE_BEG
8306 			 * since whats at r_start (i.e. th_ack)
8307 			 * is left unacked and thats where the
8308 			 * measurement not starts.
8309 			 */
8310 			tp->gput_seq = rsm->r_start;
8311 			rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8312 		}
8313 		if ((use_which == RACK_USE_END) &&
8314 		    SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
8315 			    /*
8316 			     * We use the end when the cumack
8317 			     * is moving forward and completely
8318 			     * deleting the rsm passed so basically
8319 			     * r_end holds th_ack.
8320 			     *
8321 			     * For SACK's we also want to use the end
8322 			     * since this piece just got sacked and
8323 			     * we want to target anything after that
8324 			     * in our measurement.
8325 			     */
8326 			    tp->gput_seq = rsm->r_end;
8327 			    rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8328 		}
8329 		if (use_which == RACK_USE_END_OR_THACK) {
8330 			/*
8331 			 * special case for ack moving forward,
8332 			 * not a sack, we need to move all the
8333 			 * way up to where this ack cum-ack moves
8334 			 * to.
8335 			 */
8336 			if (SEQ_GT(th_ack, rsm->r_end))
8337 				tp->gput_seq = th_ack;
8338 			else
8339 				tp->gput_seq = rsm->r_end;
8340 			rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8341 		}
8342 		if (SEQ_GT(tp->gput_seq, tp->gput_ack)) {
8343 			/*
8344 			 * We moved beyond this guy's range, re-calculate
8345 			 * the new end point.
8346 			 */
8347 			if (rack->rc_gp_filled == 0) {
8348 				tp->gput_ack = tp->gput_seq + max(rc_init_window(rack), (MIN_GP_WIN * ctf_fixed_maxseg(tp)));
8349 			} else {
8350 				tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
8351 			}
8352 		}
8353 		/*
8354 		 * We are moving the goal post, we may be able to clear the
8355 		 * measure_saw_probe_rtt flag.
8356 		 */
8357 		if ((rack->in_probe_rtt == 0) &&
8358 		    (rack->measure_saw_probe_rtt) &&
8359 		    (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
8360 			rack->measure_saw_probe_rtt = 0;
8361 		rack_log_pacing_delay_calc(rack, ts, tp->gput_ts,
8362 					   seq, tp->gput_seq, 0, 5, line, NULL, 0);
8363 		if (rack->rc_gp_filled &&
8364 		    ((tp->gput_ack - tp->gput_seq) <
8365 		     max(rc_init_window(rack), (MIN_GP_WIN *
8366 						ctf_fixed_maxseg(tp))))) {
8367 			uint32_t ideal_amount;
8368 
8369 			ideal_amount = rack_get_measure_window(tp, rack);
8370 			if (ideal_amount > sbavail(&tp->t_inpcb->inp_socket->so_snd)) {
8371 				/*
8372 				 * There is no sense of continuing this measurement
8373 				 * because its too small to gain us anything we
8374 				 * trust. Skip it and that way we can start a new
8375 				 * measurement quicker.
8376 				 */
8377 				tp->t_flags &= ~TF_GPUTINPROG;
8378 				rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq,
8379 							   0, 0, 0, 6, __LINE__, NULL, 0);
8380 			} else {
8381 				/*
8382 				 * Reset the window further out.
8383 				 */
8384 				tp->gput_ack = tp->gput_seq + ideal_amount;
8385 			}
8386 		}
8387 	}
8388 }
8389 
8390 static inline int
8391 is_rsm_inside_declared_tlp_block(struct tcp_rack *rack, struct rack_sendmap *rsm)
8392 {
8393 	if (SEQ_LT(rsm->r_end, rack->r_ctl.last_tlp_acked_start)) {
8394 		/* Behind our TLP definition or right at */
8395 		return (0);
8396 	}
8397 	if (SEQ_GT(rsm->r_start, rack->r_ctl.last_tlp_acked_end)) {
8398 		/* The start is beyond or right at our end of TLP definition */
8399 		return (0);
8400 	}
8401 	/* It has to be a sub-part of the original TLP recorded */
8402 	return (1);
8403 }
8404 
8405 
8406 static uint32_t
8407 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack, struct sackblk *sack,
8408 		   struct tcpopt *to, struct rack_sendmap **prsm, uint32_t cts, int *moved_two)
8409 {
8410 	uint32_t start, end, changed = 0;
8411 	struct rack_sendmap stack_map;
8412 	struct rack_sendmap *rsm, *nrsm, fe, *prev, *next;
8413 #ifdef INVARIANTS
8414 	struct rack_sendmap *insret;
8415 #endif
8416 	int32_t used_ref = 1;
8417 	int moved = 0;
8418 
8419 	start = sack->start;
8420 	end = sack->end;
8421 	rsm = *prsm;
8422 	memset(&fe, 0, sizeof(fe));
8423 do_rest_ofb:
8424 	if ((rsm == NULL) ||
8425 	    (SEQ_LT(end, rsm->r_start)) ||
8426 	    (SEQ_GEQ(start, rsm->r_end)) ||
8427 	    (SEQ_LT(start, rsm->r_start))) {
8428 		/*
8429 		 * We are not in the right spot,
8430 		 * find the correct spot in the tree.
8431 		 */
8432 		used_ref = 0;
8433 		fe.r_start = start;
8434 		rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
8435 		moved++;
8436 	}
8437 	if (rsm == NULL) {
8438 		/* TSNH */
8439 		goto out;
8440 	}
8441 	/* Ok we have an ACK for some piece of this rsm */
8442 	if (rsm->r_start != start) {
8443 		if ((rsm->r_flags & RACK_ACKED) == 0) {
8444 			/*
8445 			 * Before any splitting or hookery is
8446 			 * done is it a TLP of interest i.e. rxt?
8447 			 */
8448 			if ((rsm->r_flags & RACK_TLP) &&
8449 			    (rsm->r_rtr_cnt > 1)) {
8450 				/*
8451 				 * We are splitting a rxt TLP, check
8452 				 * if we need to save off the start/end
8453 				 */
8454 				if (rack->rc_last_tlp_acked_set &&
8455 				    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
8456 					/*
8457 					 * We already turned this on since we are inside
8458 					 * the previous one was a partially sack now we
8459 					 * are getting another one (maybe all of it).
8460 					 *
8461 					 */
8462 					rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
8463 					/*
8464 					 * Lets make sure we have all of it though.
8465 					 */
8466 					if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
8467 						rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8468 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8469 								     rack->r_ctl.last_tlp_acked_end);
8470 					}
8471 					if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
8472 						rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8473 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8474 								     rack->r_ctl.last_tlp_acked_end);
8475 					}
8476 				} else {
8477 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8478 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8479 					rack->rc_last_tlp_past_cumack = 0;
8480 					rack->rc_last_tlp_acked_set = 1;
8481 					rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
8482 				}
8483 			}
8484 			/**
8485 			 * Need to split this in two pieces the before and after,
8486 			 * the before remains in the map, the after must be
8487 			 * added. In other words we have:
8488 			 * rsm        |--------------|
8489 			 * sackblk        |------->
8490 			 * rsm will become
8491 			 *     rsm    |---|
8492 			 * and nrsm will be  the sacked piece
8493 			 *     nrsm       |----------|
8494 			 *
8495 			 * But before we start down that path lets
8496 			 * see if the sack spans over on top of
8497 			 * the next guy and it is already sacked.
8498 			 *
8499 			 */
8500 			next = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8501 			if (next && (next->r_flags & RACK_ACKED) &&
8502 			    SEQ_GEQ(end, next->r_start)) {
8503 				/**
8504 				 * So the next one is already acked, and
8505 				 * we can thus by hookery use our stack_map
8506 				 * to reflect the piece being sacked and
8507 				 * then adjust the two tree entries moving
8508 				 * the start and ends around. So we start like:
8509 				 *  rsm     |------------|             (not-acked)
8510 				 *  next                 |-----------| (acked)
8511 				 *  sackblk        |-------->
8512 				 *  We want to end like so:
8513 				 *  rsm     |------|                   (not-acked)
8514 				 *  next           |-----------------| (acked)
8515 				 *  nrsm           |-----|
8516 				 * Where nrsm is a temporary stack piece we
8517 				 * use to update all the gizmos.
8518 				 */
8519 				/* Copy up our fudge block */
8520 				nrsm = &stack_map;
8521 				memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
8522 				/* Now adjust our tree blocks */
8523 				rsm->r_end = start;
8524 				next->r_start = start;
8525 				/* Now we must adjust back where next->m is */
8526 				rack_setup_offset_for_rsm(rsm, next);
8527 
8528 				/* We don't need to adjust rsm, it did not change */
8529 				/* Clear out the dup ack count of the remainder */
8530 				rsm->r_dupack = 0;
8531 				rsm->r_just_ret = 0;
8532 				rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
8533 				/* Now lets make sure our fudge block is right */
8534 				nrsm->r_start = start;
8535 				/* Now lets update all the stats and such */
8536 				rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0);
8537 				if (rack->app_limited_needs_set)
8538 					rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END);
8539 				changed += (nrsm->r_end - nrsm->r_start);
8540 				rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start);
8541 				if (nrsm->r_flags & RACK_SACK_PASSED) {
8542 					counter_u64_add(rack_reorder_seen, 1);
8543 					rack->r_ctl.rc_reorder_ts = cts;
8544 				}
8545 				/*
8546 				 * Now we want to go up from rsm (the
8547 				 * one left un-acked) to the next one
8548 				 * in the tmap. We do this so when
8549 				 * we walk backwards we include marking
8550 				 * sack-passed on rsm (The one passed in
8551 				 * is skipped since it is generally called
8552 				 * on something sacked before removing it
8553 				 * from the tmap).
8554 				 */
8555 				if (rsm->r_in_tmap) {
8556 					nrsm = TAILQ_NEXT(rsm, r_tnext);
8557 					/*
8558 					 * Now that we have the next
8559 					 * one walk backwards from there.
8560 					 */
8561 					if (nrsm && nrsm->r_in_tmap)
8562 						rack_log_sack_passed(tp, rack, nrsm);
8563 				}
8564 				/* Now are we done? */
8565 				if (SEQ_LT(end, next->r_end) ||
8566 				    (end == next->r_end)) {
8567 					/* Done with block */
8568 					goto out;
8569 				}
8570 				rack_log_map_chg(tp, rack, &stack_map, rsm, next, MAP_SACK_M1, end, __LINE__);
8571 				counter_u64_add(rack_sack_used_next_merge, 1);
8572 				/* Postion for the next block */
8573 				start = next->r_end;
8574 				rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, next);
8575 				if (rsm == NULL)
8576 					goto out;
8577 			} else {
8578 				/**
8579 				 * We can't use any hookery here, so we
8580 				 * need to split the map. We enter like
8581 				 * so:
8582 				 *  rsm      |--------|
8583 				 *  sackblk       |----->
8584 				 * We will add the new block nrsm and
8585 				 * that will be the new portion, and then
8586 				 * fall through after reseting rsm. So we
8587 				 * split and look like this:
8588 				 *  rsm      |----|
8589 				 *  sackblk       |----->
8590 				 *  nrsm          |---|
8591 				 * We then fall through reseting
8592 				 * rsm to nrsm, so the next block
8593 				 * picks it up.
8594 				 */
8595 				nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
8596 				if (nrsm == NULL) {
8597 					/*
8598 					 * failed XXXrrs what can we do but loose the sack
8599 					 * info?
8600 					 */
8601 					goto out;
8602 				}
8603 				counter_u64_add(rack_sack_splits, 1);
8604 				rack_clone_rsm(rack, nrsm, rsm, start);
8605 				rsm->r_just_ret = 0;
8606 #ifndef INVARIANTS
8607 				(void)RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
8608 #else
8609 				insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
8610 				if (insret != NULL) {
8611 					panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
8612 					      nrsm, insret, rack, rsm);
8613 				}
8614 #endif
8615 				if (rsm->r_in_tmap) {
8616 					TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8617 					nrsm->r_in_tmap = 1;
8618 				}
8619 				rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M2, end, __LINE__);
8620 				rsm->r_flags &= (~RACK_HAS_FIN);
8621 				/* Position us to point to the new nrsm that starts the sack blk */
8622 				rsm = nrsm;
8623 			}
8624 		} else {
8625 			/* Already sacked this piece */
8626 			counter_u64_add(rack_sack_skipped_acked, 1);
8627 			moved++;
8628 			if (end == rsm->r_end) {
8629 				/* Done with block */
8630 				rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8631 				goto out;
8632 			} else if (SEQ_LT(end, rsm->r_end)) {
8633 				/* A partial sack to a already sacked block */
8634 				moved++;
8635 				rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8636 				goto out;
8637 			} else {
8638 				/*
8639 				 * The end goes beyond this guy
8640 				 * repostion the start to the
8641 				 * next block.
8642 				 */
8643 				start = rsm->r_end;
8644 				rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8645 				if (rsm == NULL)
8646 					goto out;
8647 			}
8648 		}
8649 	}
8650 	if (SEQ_GEQ(end, rsm->r_end)) {
8651 		/**
8652 		 * The end of this block is either beyond this guy or right
8653 		 * at this guy. I.e.:
8654 		 *  rsm ---                 |-----|
8655 		 *  end                     |-----|
8656 		 *  <or>
8657 		 *  end                     |---------|
8658 		 */
8659 		if ((rsm->r_flags & RACK_ACKED) == 0) {
8660 			/*
8661 			 * Is it a TLP of interest?
8662 			 */
8663 			if ((rsm->r_flags & RACK_TLP) &&
8664 			    (rsm->r_rtr_cnt > 1)) {
8665 				/*
8666 				 * We are splitting a rxt TLP, check
8667 				 * if we need to save off the start/end
8668 				 */
8669 				if (rack->rc_last_tlp_acked_set &&
8670 				    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
8671 					/*
8672 					 * We already turned this on since we are inside
8673 					 * the previous one was a partially sack now we
8674 					 * are getting another one (maybe all of it).
8675 					 */
8676 					rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
8677 					/*
8678 					 * Lets make sure we have all of it though.
8679 					 */
8680 					if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
8681 						rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8682 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8683 								     rack->r_ctl.last_tlp_acked_end);
8684 					}
8685 					if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
8686 						rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8687 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8688 								     rack->r_ctl.last_tlp_acked_end);
8689 					}
8690 				} else {
8691 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8692 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8693 					rack->rc_last_tlp_past_cumack = 0;
8694 					rack->rc_last_tlp_acked_set = 1;
8695 					rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
8696 				}
8697 			}
8698 			rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0);
8699 			changed += (rsm->r_end - rsm->r_start);
8700 			rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
8701 			if (rsm->r_in_tmap) /* should be true */
8702 				rack_log_sack_passed(tp, rack, rsm);
8703 			/* Is Reordering occuring? */
8704 			if (rsm->r_flags & RACK_SACK_PASSED) {
8705 				rsm->r_flags &= ~RACK_SACK_PASSED;
8706 				counter_u64_add(rack_reorder_seen, 1);
8707 				rack->r_ctl.rc_reorder_ts = cts;
8708 			}
8709 			if (rack->app_limited_needs_set)
8710 				rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END);
8711 			rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
8712 			rsm->r_flags |= RACK_ACKED;
8713 			if (rsm->r_in_tmap) {
8714 				TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8715 				rsm->r_in_tmap = 0;
8716 			}
8717 			rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_SACK_M3, end, __LINE__);
8718 		} else {
8719 			counter_u64_add(rack_sack_skipped_acked, 1);
8720 			moved++;
8721 		}
8722 		if (end == rsm->r_end) {
8723 			/* This block only - done, setup for next */
8724 			goto out;
8725 		}
8726 		/*
8727 		 * There is more not coverend by this rsm move on
8728 		 * to the next block in the RB tree.
8729 		 */
8730 		nrsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8731 		start = rsm->r_end;
8732 		rsm = nrsm;
8733 		if (rsm == NULL)
8734 			goto out;
8735 		goto do_rest_ofb;
8736 	}
8737 	/**
8738 	 * The end of this sack block is smaller than
8739 	 * our rsm i.e.:
8740 	 *  rsm ---                 |-----|
8741 	 *  end                     |--|
8742 	 */
8743 	if ((rsm->r_flags & RACK_ACKED) == 0) {
8744 		/*
8745 		 * Is it a TLP of interest?
8746 		 */
8747 		if ((rsm->r_flags & RACK_TLP) &&
8748 		    (rsm->r_rtr_cnt > 1)) {
8749 			/*
8750 			 * We are splitting a rxt TLP, check
8751 			 * if we need to save off the start/end
8752 			 */
8753 			if (rack->rc_last_tlp_acked_set &&
8754 			    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
8755 				/*
8756 				 * We already turned this on since we are inside
8757 				 * the previous one was a partially sack now we
8758 				 * are getting another one (maybe all of it).
8759 				 */
8760 				rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
8761 				/*
8762 				 * Lets make sure we have all of it though.
8763 				 */
8764 				if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
8765 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8766 					rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8767 							     rack->r_ctl.last_tlp_acked_end);
8768 				}
8769 				if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
8770 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8771 					rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8772 							     rack->r_ctl.last_tlp_acked_end);
8773 				}
8774 			} else {
8775 				rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8776 				rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8777 				rack->rc_last_tlp_past_cumack = 0;
8778 				rack->rc_last_tlp_acked_set = 1;
8779 				rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
8780 			}
8781 		}
8782 		prev = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8783 		if (prev &&
8784 		    (prev->r_flags & RACK_ACKED)) {
8785 			/**
8786 			 * Goal, we want the right remainder of rsm to shrink
8787 			 * in place and span from (rsm->r_start = end) to rsm->r_end.
8788 			 * We want to expand prev to go all the way
8789 			 * to prev->r_end <- end.
8790 			 * so in the tree we have before:
8791 			 *   prev     |--------|         (acked)
8792 			 *   rsm               |-------| (non-acked)
8793 			 *   sackblk           |-|
8794 			 * We churn it so we end up with
8795 			 *   prev     |----------|       (acked)
8796 			 *   rsm                 |-----| (non-acked)
8797 			 *   nrsm              |-| (temporary)
8798 			 *
8799 			 * Note if either prev/rsm is a TLP we don't
8800 			 * do this.
8801 			 */
8802 			nrsm = &stack_map;
8803 			memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
8804 			prev->r_end = end;
8805 			rsm->r_start = end;
8806 			/* Now adjust nrsm (stack copy) to be
8807 			 * the one that is the small
8808 			 * piece that was "sacked".
8809 			 */
8810 			nrsm->r_end = end;
8811 			rsm->r_dupack = 0;
8812 			rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
8813 			/*
8814 			 * Now that the rsm has had its start moved forward
8815 			 * lets go ahead and get its new place in the world.
8816 			 */
8817 			rack_setup_offset_for_rsm(prev, rsm);
8818 			/*
8819 			 * Now nrsm is our new little piece
8820 			 * that is acked (which was merged
8821 			 * to prev). Update the rtt and changed
8822 			 * based on that. Also check for reordering.
8823 			 */
8824 			rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0);
8825 			if (rack->app_limited_needs_set)
8826 				rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END);
8827 			changed += (nrsm->r_end - nrsm->r_start);
8828 			rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start);
8829 			if (nrsm->r_flags & RACK_SACK_PASSED) {
8830 				counter_u64_add(rack_reorder_seen, 1);
8831 				rack->r_ctl.rc_reorder_ts = cts;
8832 			}
8833 			rack_log_map_chg(tp, rack, prev, &stack_map, rsm, MAP_SACK_M4, end, __LINE__);
8834 			rsm = prev;
8835 			counter_u64_add(rack_sack_used_prev_merge, 1);
8836 		} else {
8837 			/**
8838 			 * This is the case where our previous
8839 			 * block is not acked either, so we must
8840 			 * split the block in two.
8841 			 */
8842 			nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
8843 			if (nrsm == NULL) {
8844 				/* failed rrs what can we do but loose the sack info? */
8845 				goto out;
8846 			}
8847 			if ((rsm->r_flags & RACK_TLP) &&
8848 			    (rsm->r_rtr_cnt > 1)) {
8849 				/*
8850 				 * We are splitting a rxt TLP, check
8851 				 * if we need to save off the start/end
8852 				 */
8853 				if (rack->rc_last_tlp_acked_set &&
8854 				    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
8855 					    /*
8856 					     * We already turned this on since this block is inside
8857 					     * the previous one was a partially sack now we
8858 					     * are getting another one (maybe all of it).
8859 					     */
8860 					    rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
8861 					    /*
8862 					     * Lets make sure we have all of it though.
8863 					     */
8864 					    if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
8865 						    rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8866 						    rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8867 									 rack->r_ctl.last_tlp_acked_end);
8868 					    }
8869 					    if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
8870 						    rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8871 						    rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8872 									 rack->r_ctl.last_tlp_acked_end);
8873 					    }
8874 				    } else {
8875 					    rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8876 					    rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8877 					    rack->rc_last_tlp_acked_set = 1;
8878 					    rack->rc_last_tlp_past_cumack = 0;
8879 					    rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
8880 				    }
8881 			}
8882 			/**
8883 			 * In this case nrsm becomes
8884 			 * nrsm->r_start = end;
8885 			 * nrsm->r_end = rsm->r_end;
8886 			 * which is un-acked.
8887 			 * <and>
8888 			 * rsm->r_end = nrsm->r_start;
8889 			 * i.e. the remaining un-acked
8890 			 * piece is left on the left
8891 			 * hand side.
8892 			 *
8893 			 * So we start like this
8894 			 * rsm      |----------| (not acked)
8895 			 * sackblk  |---|
8896 			 * build it so we have
8897 			 * rsm      |---|         (acked)
8898 			 * nrsm         |------|  (not acked)
8899 			 */
8900 			counter_u64_add(rack_sack_splits, 1);
8901 			rack_clone_rsm(rack, nrsm, rsm, end);
8902 			rsm->r_flags &= (~RACK_HAS_FIN);
8903 			rsm->r_just_ret = 0;
8904 #ifndef INVARIANTS
8905 			(void)RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
8906 #else
8907 			insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
8908 			if (insret != NULL) {
8909 				panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
8910 				      nrsm, insret, rack, rsm);
8911 			}
8912 #endif
8913 			if (rsm->r_in_tmap) {
8914 				TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8915 				nrsm->r_in_tmap = 1;
8916 			}
8917 			nrsm->r_dupack = 0;
8918 			rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2);
8919 			rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0);
8920 			changed += (rsm->r_end - rsm->r_start);
8921 			rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
8922 			if (rsm->r_in_tmap) /* should be true */
8923 				rack_log_sack_passed(tp, rack, rsm);
8924 			/* Is Reordering occuring? */
8925 			if (rsm->r_flags & RACK_SACK_PASSED) {
8926 				rsm->r_flags &= ~RACK_SACK_PASSED;
8927 				counter_u64_add(rack_reorder_seen, 1);
8928 				rack->r_ctl.rc_reorder_ts = cts;
8929 			}
8930 			if (rack->app_limited_needs_set)
8931 				rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END);
8932 			rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
8933 			rsm->r_flags |= RACK_ACKED;
8934 			rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M5, end, __LINE__);
8935 			if (rsm->r_in_tmap) {
8936 				TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8937 				rsm->r_in_tmap = 0;
8938 			}
8939 		}
8940 	} else if (start != end){
8941 		/*
8942 		 * The block was already acked.
8943 		 */
8944 		counter_u64_add(rack_sack_skipped_acked, 1);
8945 		moved++;
8946 	}
8947 out:
8948 	if (rsm &&
8949 	    ((rsm->r_flags & RACK_TLP) == 0) &&
8950 	    (rsm->r_flags & RACK_ACKED)) {
8951 		/*
8952 		 * Now can we merge where we worked
8953 		 * with either the previous or
8954 		 * next block?
8955 		 */
8956 		next = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8957 		while (next) {
8958 			if (next->r_flags & RACK_TLP)
8959 				break;
8960 			if (next->r_flags & RACK_ACKED) {
8961 			/* yep this and next can be merged */
8962 				rsm = rack_merge_rsm(rack, rsm, next);
8963 				next = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8964 			} else
8965 				break;
8966 		}
8967 		/* Now what about the previous? */
8968 		prev = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8969 		while (prev) {
8970 			if (prev->r_flags & RACK_TLP)
8971 				break;
8972 			if (prev->r_flags & RACK_ACKED) {
8973 				/* yep the previous and this can be merged */
8974 				rsm = rack_merge_rsm(rack, prev, rsm);
8975 				prev = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8976 			} else
8977 				break;
8978 		}
8979 	}
8980 	if (used_ref == 0) {
8981 		counter_u64_add(rack_sack_proc_all, 1);
8982 	} else {
8983 		counter_u64_add(rack_sack_proc_short, 1);
8984 	}
8985 	/* Save off the next one for quick reference. */
8986 	if (rsm)
8987 		nrsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8988 	else
8989 		nrsm = NULL;
8990 	*prsm = rack->r_ctl.rc_sacklast = nrsm;
8991 	/* Pass back the moved. */
8992 	*moved_two = moved;
8993 	return (changed);
8994 }
8995 
8996 static void inline
8997 rack_peer_reneges(struct tcp_rack *rack, struct rack_sendmap *rsm, tcp_seq th_ack)
8998 {
8999 	struct rack_sendmap *tmap;
9000 
9001 	tmap = NULL;
9002 	while (rsm && (rsm->r_flags & RACK_ACKED)) {
9003 		/* Its no longer sacked, mark it so */
9004 		rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
9005 #ifdef INVARIANTS
9006 		if (rsm->r_in_tmap) {
9007 			panic("rack:%p rsm:%p flags:0x%x in tmap?",
9008 			      rack, rsm, rsm->r_flags);
9009 		}
9010 #endif
9011 		rsm->r_flags &= ~(RACK_ACKED|RACK_SACK_PASSED|RACK_WAS_SACKPASS);
9012 		/* Rebuild it into our tmap */
9013 		if (tmap == NULL) {
9014 			TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
9015 			tmap = rsm;
9016 		} else {
9017 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, tmap, rsm, r_tnext);
9018 			tmap = rsm;
9019 		}
9020 		tmap->r_in_tmap = 1;
9021 		rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
9022 	}
9023 	/*
9024 	 * Now lets possibly clear the sack filter so we start
9025 	 * recognizing sacks that cover this area.
9026 	 */
9027 	sack_filter_clear(&rack->r_ctl.rack_sf, th_ack);
9028 
9029 }
9030 
9031 static void
9032 rack_do_decay(struct tcp_rack *rack)
9033 {
9034 	struct timeval res;
9035 
9036 #define	timersub(tvp, uvp, vvp)						\
9037 	do {								\
9038 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
9039 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
9040 		if ((vvp)->tv_usec < 0) {				\
9041 			(vvp)->tv_sec--;				\
9042 			(vvp)->tv_usec += 1000000;			\
9043 		}							\
9044 	} while (0)
9045 
9046 	timersub(&rack->r_ctl.act_rcv_time, &rack->r_ctl.rc_last_time_decay, &res);
9047 #undef timersub
9048 
9049 	rack->r_ctl.input_pkt++;
9050 	if ((rack->rc_in_persist) ||
9051 	    (res.tv_sec >= 1) ||
9052 	    (rack->rc_tp->snd_max == rack->rc_tp->snd_una)) {
9053 		/*
9054 		 * Check for decay of non-SAD,
9055 		 * we want all SAD detection metrics to
9056 		 * decay 1/4 per second (or more) passed.
9057 		 */
9058 #ifdef NETFLIX_EXP_DETECTION
9059 		uint32_t pkt_delta;
9060 
9061 		pkt_delta = rack->r_ctl.input_pkt - rack->r_ctl.saved_input_pkt;
9062 #endif
9063 		/* Update our saved tracking values */
9064 		rack->r_ctl.saved_input_pkt = rack->r_ctl.input_pkt;
9065 		rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time;
9066 		/* Now do we escape without decay? */
9067 #ifdef NETFLIX_EXP_DETECTION
9068 		if (rack->rc_in_persist ||
9069 		    (rack->rc_tp->snd_max == rack->rc_tp->snd_una) ||
9070 		    (pkt_delta < tcp_sad_low_pps)){
9071 			/*
9072 			 * We don't decay idle connections
9073 			 * or ones that have a low input pps.
9074 			 */
9075 			return;
9076 		}
9077 		/* Decay the counters */
9078 		rack->r_ctl.ack_count = ctf_decay_count(rack->r_ctl.ack_count,
9079 							tcp_sad_decay_val);
9080 		rack->r_ctl.sack_count = ctf_decay_count(rack->r_ctl.sack_count,
9081 							 tcp_sad_decay_val);
9082 		rack->r_ctl.sack_moved_extra = ctf_decay_count(rack->r_ctl.sack_moved_extra,
9083 							       tcp_sad_decay_val);
9084 		rack->r_ctl.sack_noextra_move = ctf_decay_count(rack->r_ctl.sack_noextra_move,
9085 								tcp_sad_decay_val);
9086 #endif
9087 	}
9088 }
9089 
9090 static void
9091 rack_process_to_cumack(struct tcpcb *tp, struct tcp_rack *rack, register uint32_t th_ack, uint32_t cts, struct tcpopt *to)
9092 {
9093 	struct rack_sendmap *rsm;
9094 #ifdef INVARIANTS
9095 	struct rack_sendmap *rm;
9096 #endif
9097 
9098 	/*
9099 	 * The ACK point is advancing to th_ack, we must drop off
9100 	 * the packets in the rack log and calculate any eligble
9101 	 * RTT's.
9102 	 */
9103 	rack->r_wanted_output = 1;
9104 
9105 	/* Tend any TLP that has been marked for 1/2 the seq space (its old)  */
9106 	if ((rack->rc_last_tlp_acked_set == 1)&&
9107 	    (rack->rc_last_tlp_past_cumack == 1) &&
9108 	    (SEQ_GT(rack->r_ctl.last_tlp_acked_start, th_ack))) {
9109 		/*
9110 		 * We have reached the point where our last rack
9111 		 * tlp retransmit sequence is ahead of the cum-ack.
9112 		 * This can only happen when the cum-ack moves all
9113 		 * the way around (its been a full 2^^31+1 bytes
9114 		 * or more since we sent a retransmitted TLP). Lets
9115 		 * turn off the valid flag since its not really valid.
9116 		 *
9117 		 * Note since sack's also turn on this event we have
9118 		 * a complication, we have to wait to age it out until
9119 		 * the cum-ack is by the TLP before checking which is
9120 		 * what the next else clause does.
9121 		 */
9122 		rack_log_dsack_event(rack, 9, __LINE__,
9123 				     rack->r_ctl.last_tlp_acked_start,
9124 				     rack->r_ctl.last_tlp_acked_end);
9125 		rack->rc_last_tlp_acked_set = 0;
9126 		rack->rc_last_tlp_past_cumack = 0;
9127 	} else if ((rack->rc_last_tlp_acked_set == 1) &&
9128 		   (rack->rc_last_tlp_past_cumack == 0) &&
9129 		   (SEQ_GEQ(th_ack, rack->r_ctl.last_tlp_acked_end))) {
9130 		/*
9131 		 * It is safe to start aging TLP's out.
9132 		 */
9133 		rack->rc_last_tlp_past_cumack = 1;
9134 	}
9135 	/* We do the same for the tlp send seq as well */
9136 	if ((rack->rc_last_sent_tlp_seq_valid == 1) &&
9137 	    (rack->rc_last_sent_tlp_past_cumack == 1) &&
9138 	    (SEQ_GT(rack->r_ctl.last_sent_tlp_seq,  th_ack))) {
9139 		rack_log_dsack_event(rack, 9, __LINE__,
9140 				     rack->r_ctl.last_sent_tlp_seq,
9141 				     (rack->r_ctl.last_sent_tlp_seq +
9142 				      rack->r_ctl.last_sent_tlp_len));
9143 		rack->rc_last_sent_tlp_seq_valid = 0;
9144 		rack->rc_last_sent_tlp_past_cumack = 0;
9145 	} else if ((rack->rc_last_sent_tlp_seq_valid == 1) &&
9146 		   (rack->rc_last_sent_tlp_past_cumack == 0) &&
9147 		   (SEQ_GEQ(th_ack, rack->r_ctl.last_sent_tlp_seq))) {
9148 		/*
9149 		 * It is safe to start aging TLP's send.
9150 		 */
9151 		rack->rc_last_sent_tlp_past_cumack = 1;
9152 	}
9153 more:
9154 	rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
9155 	if (rsm == NULL) {
9156 		if ((th_ack - 1) == tp->iss) {
9157 			/*
9158 			 * For the SYN incoming case we will not
9159 			 * have called tcp_output for the sending of
9160 			 * the SYN, so there will be no map. All
9161 			 * other cases should probably be a panic.
9162 			 */
9163 			return;
9164 		}
9165 		if (tp->t_flags & TF_SENTFIN) {
9166 			/* if we sent a FIN we often will not have map */
9167 			return;
9168 		}
9169 #ifdef INVARIANTS
9170 		panic("No rack map tp:%p for state:%d ack:%u rack:%p snd_una:%u snd_max:%u snd_nxt:%u\n",
9171 		      tp,
9172 		      tp->t_state, th_ack, rack,
9173 		      tp->snd_una, tp->snd_max, tp->snd_nxt);
9174 #endif
9175 		return;
9176 	}
9177 	if (SEQ_LT(th_ack, rsm->r_start)) {
9178 		/* Huh map is missing this */
9179 #ifdef INVARIANTS
9180 		printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d\n",
9181 		       rsm->r_start,
9182 		       th_ack, tp->t_state, rack->r_state);
9183 #endif
9184 		return;
9185 	}
9186 	rack_update_rtt(tp, rack, rsm, to, cts, CUM_ACKED, th_ack);
9187 
9188 	/* Now was it a retransmitted TLP? */
9189 	if ((rsm->r_flags & RACK_TLP) &&
9190 	    (rsm->r_rtr_cnt > 1)) {
9191 		/*
9192 		 * Yes, this rsm was a TLP and retransmitted, remember that
9193 		 * since if a DSACK comes back on this we don't want
9194 		 * to think of it as a reordered segment. This may
9195 		 * get updated again with possibly even other TLPs
9196 		 * in flight, but thats ok. Only when we don't send
9197 		 * a retransmitted TLP for 1/2 the sequences space
9198 		 * will it get turned off (above).
9199 		 */
9200 		if (rack->rc_last_tlp_acked_set &&
9201 		    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
9202 			/*
9203 			 * We already turned this on since the end matches,
9204 			 * the previous one was a partially ack now we
9205 			 * are getting another one (maybe all of it).
9206 			 */
9207 			rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
9208 			/*
9209 			 * Lets make sure we have all of it though.
9210 			 */
9211 			if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
9212 				rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9213 				rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9214 						     rack->r_ctl.last_tlp_acked_end);
9215 			}
9216 			if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
9217 				rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9218 				rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9219 						     rack->r_ctl.last_tlp_acked_end);
9220 			}
9221 		} else {
9222 			rack->rc_last_tlp_past_cumack = 1;
9223 			rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9224 			rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9225 			rack->rc_last_tlp_acked_set = 1;
9226 			rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
9227 		}
9228 	}
9229 	/* Now do we consume the whole thing? */
9230 	if (SEQ_GEQ(th_ack, rsm->r_end)) {
9231 		/* Its all consumed. */
9232 		uint32_t left;
9233 		uint8_t newly_acked;
9234 
9235 		rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_FREE, rsm->r_end, __LINE__);
9236 		rack->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
9237 		rsm->r_rtr_bytes = 0;
9238 		/* Record the time of highest cumack sent */
9239 		rack->r_ctl.rc_gp_cumack_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
9240 #ifndef INVARIANTS
9241 		(void)RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
9242 #else
9243 		rm = RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
9244 		if (rm != rsm) {
9245 			panic("removing head in rack:%p rsm:%p rm:%p",
9246 			      rack, rsm, rm);
9247 		}
9248 #endif
9249 		if (rsm->r_in_tmap) {
9250 			TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
9251 			rsm->r_in_tmap = 0;
9252 		}
9253 		newly_acked = 1;
9254 		if (rsm->r_flags & RACK_ACKED) {
9255 			/*
9256 			 * It was acked on the scoreboard -- remove
9257 			 * it from total
9258 			 */
9259 			rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
9260 			newly_acked = 0;
9261 		} else if (rsm->r_flags & RACK_SACK_PASSED) {
9262 			/*
9263 			 * There are segments ACKED on the
9264 			 * scoreboard further up. We are seeing
9265 			 * reordering.
9266 			 */
9267 			rsm->r_flags &= ~RACK_SACK_PASSED;
9268 			counter_u64_add(rack_reorder_seen, 1);
9269 			rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
9270 			rsm->r_flags |= RACK_ACKED;
9271 			rack->r_ctl.rc_reorder_ts = cts;
9272 			if (rack->r_ent_rec_ns) {
9273 				/*
9274 				 * We have sent no more, and we saw an sack
9275 				 * then ack arrive.
9276 				 */
9277 				rack->r_might_revert = 1;
9278 			}
9279 		}
9280 		if ((rsm->r_flags & RACK_TO_REXT) &&
9281 		    (tp->t_flags & TF_RCVD_TSTMP) &&
9282 		    (to->to_flags & TOF_TS) &&
9283 		    (to->to_tsecr != 0) &&
9284 		    (tp->t_flags & TF_PREVVALID)) {
9285 			/*
9286 			 * We can use the timestamp to see
9287 			 * if this retransmission was from the
9288 			 * first transmit. If so we made a mistake.
9289 			 */
9290 			tp->t_flags &= ~TF_PREVVALID;
9291 			if (to->to_tsecr == rack_ts_to_msec(rsm->r_tim_lastsent[0])) {
9292 				/* The first transmit is what this ack is for */
9293 				rack_cong_signal(tp, CC_RTO_ERR, th_ack);
9294 			}
9295 		}
9296 		left = th_ack - rsm->r_end;
9297 		if (rack->app_limited_needs_set && newly_acked)
9298 			rack_need_set_test(tp, rack, rsm, th_ack, __LINE__, RACK_USE_END_OR_THACK);
9299 		/* Free back to zone */
9300 		rack_free(rack, rsm);
9301 		if (left) {
9302 			goto more;
9303 		}
9304 		/* Check for reneging */
9305 		rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
9306 		if (rsm && (rsm->r_flags & RACK_ACKED) && (th_ack == rsm->r_start)) {
9307 			/*
9308 			 * The peer has moved snd_una up to
9309 			 * the edge of this send, i.e. one
9310 			 * that it had previously acked. The only
9311 			 * way that can be true if the peer threw
9312 			 * away data (space issues) that it had
9313 			 * previously sacked (else it would have
9314 			 * given us snd_una up to (rsm->r_end).
9315 			 * We need to undo the acked markings here.
9316 			 *
9317 			 * Note we have to look to make sure th_ack is
9318 			 * our rsm->r_start in case we get an old ack
9319 			 * where th_ack is behind snd_una.
9320 			 */
9321 			rack_peer_reneges(rack, rsm, th_ack);
9322 		}
9323 		return;
9324 	}
9325 	if (rsm->r_flags & RACK_ACKED) {
9326 		/*
9327 		 * It was acked on the scoreboard -- remove it from
9328 		 * total for the part being cum-acked.
9329 		 */
9330 		rack->r_ctl.rc_sacked -= (th_ack - rsm->r_start);
9331 	}
9332 	/*
9333 	 * Clear the dup ack count for
9334 	 * the piece that remains.
9335 	 */
9336 	rsm->r_dupack = 0;
9337 	rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
9338 	if (rsm->r_rtr_bytes) {
9339 		/*
9340 		 * It was retransmitted adjust the
9341 		 * sack holes for what was acked.
9342 		 */
9343 		int ack_am;
9344 
9345 		ack_am = (th_ack - rsm->r_start);
9346 		if (ack_am >= rsm->r_rtr_bytes) {
9347 			rack->r_ctl.rc_holes_rxt -= ack_am;
9348 			rsm->r_rtr_bytes -= ack_am;
9349 		}
9350 	}
9351 	/*
9352 	 * Update where the piece starts and record
9353 	 * the time of send of highest cumack sent.
9354 	 */
9355 	rack->r_ctl.rc_gp_cumack_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
9356 	rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_TRIM_HEAD, th_ack, __LINE__);
9357 	/* Now we need to move our offset forward too */
9358 	if (rsm->m && (rsm->orig_m_len != rsm->m->m_len)) {
9359 		/* Fix up the orig_m_len and possibly the mbuf offset */
9360 		rack_adjust_orig_mlen(rsm);
9361 	}
9362 	rsm->soff += (th_ack - rsm->r_start);
9363 	rsm->r_start = th_ack;
9364 	/* Now do we need to move the mbuf fwd too? */
9365 	if (rsm->m) {
9366 		while (rsm->soff >= rsm->m->m_len) {
9367 			rsm->soff -= rsm->m->m_len;
9368 			rsm->m = rsm->m->m_next;
9369 			KASSERT((rsm->m != NULL),
9370 				(" nrsm:%p hit at soff:%u null m",
9371 				 rsm, rsm->soff));
9372 		}
9373 		rsm->orig_m_len = rsm->m->m_len;
9374 	}
9375 	if (rack->app_limited_needs_set)
9376 		rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_BEG);
9377 }
9378 
9379 static void
9380 rack_handle_might_revert(struct tcpcb *tp, struct tcp_rack *rack)
9381 {
9382 	struct rack_sendmap *rsm;
9383 	int sack_pass_fnd = 0;
9384 
9385 	if (rack->r_might_revert) {
9386 		/*
9387 		 * Ok we have reordering, have not sent anything, we
9388 		 * might want to revert the congestion state if nothing
9389 		 * further has SACK_PASSED on it. Lets check.
9390 		 *
9391 		 * We also get here when we have DSACKs come in for
9392 		 * all the data that we FR'd. Note that a rxt or tlp
9393 		 * timer clears this from happening.
9394 		 */
9395 
9396 		TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
9397 			if (rsm->r_flags & RACK_SACK_PASSED) {
9398 				sack_pass_fnd = 1;
9399 				break;
9400 			}
9401 		}
9402 		if (sack_pass_fnd == 0) {
9403 			/*
9404 			 * We went into recovery
9405 			 * incorrectly due to reordering!
9406 			 */
9407 			int orig_cwnd;
9408 
9409 			rack->r_ent_rec_ns = 0;
9410 			orig_cwnd = tp->snd_cwnd;
9411 			tp->snd_cwnd = rack->r_ctl.rc_cwnd_at_erec;
9412 			tp->snd_ssthresh = rack->r_ctl.rc_ssthresh_at_erec;
9413 			tp->snd_recover = tp->snd_una;
9414 			rack_log_to_prr(rack, 14, orig_cwnd);
9415 			EXIT_RECOVERY(tp->t_flags);
9416 		}
9417 		rack->r_might_revert = 0;
9418 	}
9419 }
9420 
9421 #ifdef NETFLIX_EXP_DETECTION
9422 static void
9423 rack_do_detection(struct tcpcb *tp, struct tcp_rack *rack,  uint32_t bytes_this_ack, uint32_t segsiz)
9424 {
9425 	if ((rack->do_detection || tcp_force_detection) &&
9426 	    tcp_sack_to_ack_thresh &&
9427 	    tcp_sack_to_move_thresh &&
9428 	    ((rack->r_ctl.rc_num_maps_alloced > tcp_map_minimum) || rack->sack_attack_disable)) {
9429 		/*
9430 		 * We have thresholds set to find
9431 		 * possible attackers and disable sack.
9432 		 * Check them.
9433 		 */
9434 		uint64_t ackratio, moveratio, movetotal;
9435 
9436 		/* Log detecting */
9437 		rack_log_sad(rack, 1);
9438 		ackratio = (uint64_t)(rack->r_ctl.sack_count);
9439 		ackratio *= (uint64_t)(1000);
9440 		if (rack->r_ctl.ack_count)
9441 			ackratio /= (uint64_t)(rack->r_ctl.ack_count);
9442 		else {
9443 			/* We really should not hit here */
9444 			ackratio = 1000;
9445 		}
9446 		if ((rack->sack_attack_disable == 0) &&
9447 		    (ackratio > rack_highest_sack_thresh_seen))
9448 			rack_highest_sack_thresh_seen = (uint32_t)ackratio;
9449 		movetotal = rack->r_ctl.sack_moved_extra;
9450 		movetotal += rack->r_ctl.sack_noextra_move;
9451 		moveratio = rack->r_ctl.sack_moved_extra;
9452 		moveratio *= (uint64_t)1000;
9453 		if (movetotal)
9454 			moveratio /= movetotal;
9455 		else {
9456 			/* No moves, thats pretty good */
9457 			moveratio = 0;
9458 		}
9459 		if ((rack->sack_attack_disable == 0) &&
9460 		    (moveratio > rack_highest_move_thresh_seen))
9461 			rack_highest_move_thresh_seen = (uint32_t)moveratio;
9462 		if (rack->sack_attack_disable == 0) {
9463 			if ((ackratio > tcp_sack_to_ack_thresh) &&
9464 			    (moveratio > tcp_sack_to_move_thresh)) {
9465 				/* Disable sack processing */
9466 				rack->sack_attack_disable = 1;
9467 				if (rack->r_rep_attack == 0) {
9468 					rack->r_rep_attack = 1;
9469 					counter_u64_add(rack_sack_attacks_detected, 1);
9470 				}
9471 				if (tcp_attack_on_turns_on_logging) {
9472 					/*
9473 					 * Turn on logging, used for debugging
9474 					 * false positives.
9475 					 */
9476 					rack->rc_tp->t_logstate = tcp_attack_on_turns_on_logging;
9477 				}
9478 				/* Clamp the cwnd at flight size */
9479 				rack->r_ctl.rc_saved_cwnd = rack->rc_tp->snd_cwnd;
9480 				rack->rc_tp->snd_cwnd = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
9481 				rack_log_sad(rack, 2);
9482 			}
9483 		} else {
9484 			/* We are sack-disabled check for false positives */
9485 			if ((ackratio <= tcp_restoral_thresh) ||
9486 			    (rack->r_ctl.rc_num_maps_alloced  < tcp_map_minimum)) {
9487 				rack->sack_attack_disable = 0;
9488 				rack_log_sad(rack, 3);
9489 				/* Restart counting */
9490 				rack->r_ctl.sack_count = 0;
9491 				rack->r_ctl.sack_moved_extra = 0;
9492 				rack->r_ctl.sack_noextra_move = 1;
9493 				rack->r_ctl.ack_count = max(1,
9494 				      (bytes_this_ack / segsiz));
9495 
9496 				if (rack->r_rep_reverse == 0) {
9497 					rack->r_rep_reverse = 1;
9498 					counter_u64_add(rack_sack_attacks_reversed, 1);
9499 				}
9500 				/* Restore the cwnd */
9501 				if (rack->r_ctl.rc_saved_cwnd > rack->rc_tp->snd_cwnd)
9502 					rack->rc_tp->snd_cwnd = rack->r_ctl.rc_saved_cwnd;
9503 			}
9504 		}
9505 	}
9506 }
9507 #endif
9508 
9509 static int
9510 rack_note_dsack(struct tcp_rack *rack, tcp_seq start, tcp_seq end)
9511 {
9512 
9513 	uint32_t am, l_end;
9514 	int was_tlp = 0;
9515 
9516 	if (SEQ_GT(end, start))
9517 		am = end - start;
9518 	else
9519 		am = 0;
9520 	if ((rack->rc_last_tlp_acked_set ) &&
9521 	    (SEQ_GEQ(start, rack->r_ctl.last_tlp_acked_start)) &&
9522 	    (SEQ_LEQ(end, rack->r_ctl.last_tlp_acked_end))) {
9523 		/*
9524 		 * The DSACK is because of a TLP which we don't
9525 		 * do anything with the reordering window over since
9526 		 * it was not reordering that caused the DSACK but
9527 		 * our previous retransmit TLP.
9528 		 */
9529 		rack_log_dsack_event(rack, 7, __LINE__, start, end);
9530 		was_tlp = 1;
9531 		goto skip_dsack_round;
9532 	}
9533 	if (rack->rc_last_sent_tlp_seq_valid) {
9534 		l_end = rack->r_ctl.last_sent_tlp_seq + rack->r_ctl.last_sent_tlp_len;
9535 		if (SEQ_GEQ(start, rack->r_ctl.last_sent_tlp_seq) &&
9536 		    (SEQ_LEQ(end, l_end))) {
9537 			/*
9538 			 * This dsack is from the last sent TLP, ignore it
9539 			 * for reordering purposes.
9540 			 */
9541 			rack_log_dsack_event(rack, 7, __LINE__, start, end);
9542 			was_tlp = 1;
9543 			goto skip_dsack_round;
9544 		}
9545 	}
9546 	if (rack->rc_dsack_round_seen == 0) {
9547 		rack->rc_dsack_round_seen = 1;
9548 		rack->r_ctl.dsack_round_end = rack->rc_tp->snd_max;
9549 		rack->r_ctl.num_dsack++;
9550 		rack->r_ctl.dsack_persist = 16;	/* 16 is from the standard */
9551 		rack_log_dsack_event(rack, 2, __LINE__, 0, 0);
9552 	}
9553 skip_dsack_round:
9554 	/*
9555 	 * We keep track of how many DSACK blocks we get
9556 	 * after a recovery incident.
9557 	 */
9558 	rack->r_ctl.dsack_byte_cnt += am;
9559 	if (!IN_FASTRECOVERY(rack->rc_tp->t_flags) &&
9560 	    rack->r_ctl.retran_during_recovery &&
9561 	    (rack->r_ctl.dsack_byte_cnt >= rack->r_ctl.retran_during_recovery)) {
9562 		/*
9563 		 * False recovery most likely culprit is reordering. If
9564 		 * nothing else is missing we need to revert.
9565 		 */
9566 		rack->r_might_revert = 1;
9567 		rack_handle_might_revert(rack->rc_tp, rack);
9568 		rack->r_might_revert = 0;
9569 		rack->r_ctl.retran_during_recovery = 0;
9570 		rack->r_ctl.dsack_byte_cnt = 0;
9571 	}
9572 	return (was_tlp);
9573 }
9574 
9575 static void
9576 rack_update_prr(struct tcpcb *tp, struct tcp_rack *rack, uint32_t changed, tcp_seq th_ack)
9577 {
9578 	/* Deal with changed and PRR here (in recovery only) */
9579 	uint32_t pipe, snd_una;
9580 
9581 	rack->r_ctl.rc_prr_delivered += changed;
9582 
9583 	if (sbavail(&rack->rc_inp->inp_socket->so_snd) <= (tp->snd_max - tp->snd_una)) {
9584 		/*
9585 		 * It is all outstanding, we are application limited
9586 		 * and thus we don't need more room to send anything.
9587 		 * Note we use tp->snd_una here and not th_ack because
9588 		 * the data as yet not been cut from the sb.
9589 		 */
9590 		rack->r_ctl.rc_prr_sndcnt = 0;
9591 		return;
9592 	}
9593 	/* Compute prr_sndcnt */
9594 	if (SEQ_GT(tp->snd_una, th_ack)) {
9595 		snd_una = tp->snd_una;
9596 	} else {
9597 		snd_una = th_ack;
9598 	}
9599 	pipe = ((tp->snd_max - snd_una) - rack->r_ctl.rc_sacked) + rack->r_ctl.rc_holes_rxt;
9600 	if (pipe > tp->snd_ssthresh) {
9601 		long sndcnt;
9602 
9603 		sndcnt = rack->r_ctl.rc_prr_delivered * tp->snd_ssthresh;
9604 		if (rack->r_ctl.rc_prr_recovery_fs > 0)
9605 			sndcnt /= (long)rack->r_ctl.rc_prr_recovery_fs;
9606 		else {
9607 			rack->r_ctl.rc_prr_sndcnt = 0;
9608 			rack_log_to_prr(rack, 9, 0);
9609 			sndcnt = 0;
9610 		}
9611 		sndcnt++;
9612 		if (sndcnt > (long)rack->r_ctl.rc_prr_out)
9613 			sndcnt -= rack->r_ctl.rc_prr_out;
9614 		else
9615 			sndcnt = 0;
9616 		rack->r_ctl.rc_prr_sndcnt = sndcnt;
9617 		rack_log_to_prr(rack, 10, 0);
9618 	} else {
9619 		uint32_t limit;
9620 
9621 		if (rack->r_ctl.rc_prr_delivered > rack->r_ctl.rc_prr_out)
9622 			limit = (rack->r_ctl.rc_prr_delivered - rack->r_ctl.rc_prr_out);
9623 		else
9624 			limit = 0;
9625 		if (changed > limit)
9626 			limit = changed;
9627 		limit += ctf_fixed_maxseg(tp);
9628 		if (tp->snd_ssthresh > pipe) {
9629 			rack->r_ctl.rc_prr_sndcnt = min((tp->snd_ssthresh - pipe), limit);
9630 			rack_log_to_prr(rack, 11, 0);
9631 		} else {
9632 			rack->r_ctl.rc_prr_sndcnt = min(0, limit);
9633 			rack_log_to_prr(rack, 12, 0);
9634 		}
9635 	}
9636 }
9637 
9638 static void
9639 rack_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th, int entered_recovery, int dup_ack_struck)
9640 {
9641 	uint32_t changed;
9642 	struct tcp_rack *rack;
9643 	struct rack_sendmap *rsm;
9644 	struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1];
9645 	register uint32_t th_ack;
9646 	int32_t i, j, k, num_sack_blks = 0;
9647 	uint32_t cts, acked, ack_point;
9648 	int loop_start = 0, moved_two = 0;
9649 	uint32_t tsused;
9650 
9651 
9652 	INP_WLOCK_ASSERT(tp->t_inpcb);
9653 	if (tcp_get_flags(th) & TH_RST) {
9654 		/* We don't log resets */
9655 		return;
9656 	}
9657 	rack = (struct tcp_rack *)tp->t_fb_ptr;
9658 	cts = tcp_get_usecs(NULL);
9659 	rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
9660 	changed = 0;
9661 	th_ack = th->th_ack;
9662 	if (rack->sack_attack_disable == 0)
9663 		rack_do_decay(rack);
9664 	if (BYTES_THIS_ACK(tp, th) >= ctf_fixed_maxseg(rack->rc_tp)) {
9665 		/*
9666 		 * You only get credit for
9667 		 * MSS and greater (and you get extra
9668 		 * credit for larger cum-ack moves).
9669 		 */
9670 		int ac;
9671 
9672 		ac = BYTES_THIS_ACK(tp, th) / ctf_fixed_maxseg(rack->rc_tp);
9673 		rack->r_ctl.ack_count += ac;
9674 		counter_u64_add(rack_ack_total, ac);
9675 	}
9676 	if (rack->r_ctl.ack_count > 0xfff00000) {
9677 		/*
9678 		 * reduce the number to keep us under
9679 		 * a uint32_t.
9680 		 */
9681 		rack->r_ctl.ack_count /= 2;
9682 		rack->r_ctl.sack_count /= 2;
9683 	}
9684 	if (SEQ_GT(th_ack, tp->snd_una)) {
9685 		rack_log_progress_event(rack, tp, ticks, PROGRESS_UPDATE, __LINE__);
9686 		tp->t_acktime = ticks;
9687 	}
9688 	if (rsm && SEQ_GT(th_ack, rsm->r_start))
9689 		changed = th_ack - rsm->r_start;
9690 	if (changed) {
9691 		rack_process_to_cumack(tp, rack, th_ack, cts, to);
9692 	}
9693 	if ((to->to_flags & TOF_SACK) == 0) {
9694 		/* We are done nothing left and no sack. */
9695 		rack_handle_might_revert(tp, rack);
9696 		/*
9697 		 * For cases where we struck a dup-ack
9698 		 * with no SACK, add to the changes so
9699 		 * PRR will work right.
9700 		 */
9701 		if (dup_ack_struck && (changed == 0)) {
9702 			changed += ctf_fixed_maxseg(rack->rc_tp);
9703 		}
9704 		goto out;
9705 	}
9706 	/* Sack block processing */
9707 	if (SEQ_GT(th_ack, tp->snd_una))
9708 		ack_point = th_ack;
9709 	else
9710 		ack_point = tp->snd_una;
9711 	for (i = 0; i < to->to_nsacks; i++) {
9712 		bcopy((to->to_sacks + i * TCPOLEN_SACK),
9713 		      &sack, sizeof(sack));
9714 		sack.start = ntohl(sack.start);
9715 		sack.end = ntohl(sack.end);
9716 		if (SEQ_GT(sack.end, sack.start) &&
9717 		    SEQ_GT(sack.start, ack_point) &&
9718 		    SEQ_LT(sack.start, tp->snd_max) &&
9719 		    SEQ_GT(sack.end, ack_point) &&
9720 		    SEQ_LEQ(sack.end, tp->snd_max)) {
9721 			sack_blocks[num_sack_blks] = sack;
9722 			num_sack_blks++;
9723 		} else if (SEQ_LEQ(sack.start, th_ack) &&
9724 			   SEQ_LEQ(sack.end, th_ack)) {
9725 			int was_tlp;
9726 
9727 			was_tlp = rack_note_dsack(rack, sack.start, sack.end);
9728 			/*
9729 			 * Its a D-SACK block.
9730 			 */
9731 			tcp_record_dsack(tp, sack.start, sack.end, was_tlp);
9732 		}
9733 	}
9734 	if (rack->rc_dsack_round_seen) {
9735 		/* Is the dsack roound over? */
9736 		if (SEQ_GEQ(th_ack, rack->r_ctl.dsack_round_end)) {
9737 			/* Yes it is */
9738 			rack->rc_dsack_round_seen = 0;
9739 			rack_log_dsack_event(rack, 3, __LINE__, 0, 0);
9740 		}
9741 	}
9742 	/*
9743 	 * Sort the SACK blocks so we can update the rack scoreboard with
9744 	 * just one pass.
9745 	 */
9746 	num_sack_blks = sack_filter_blks(&rack->r_ctl.rack_sf, sack_blocks,
9747 					 num_sack_blks, th->th_ack);
9748 	ctf_log_sack_filter(rack->rc_tp, num_sack_blks, sack_blocks);
9749 	if (num_sack_blks == 0) {
9750 		/* Nothing to sack (DSACKs?) */
9751 		goto out_with_totals;
9752 	}
9753 	if (num_sack_blks < 2) {
9754 		/* Only one, we don't need to sort */
9755 		goto do_sack_work;
9756 	}
9757 	/* Sort the sacks */
9758 	for (i = 0; i < num_sack_blks; i++) {
9759 		for (j = i + 1; j < num_sack_blks; j++) {
9760 			if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
9761 				sack = sack_blocks[i];
9762 				sack_blocks[i] = sack_blocks[j];
9763 				sack_blocks[j] = sack;
9764 			}
9765 		}
9766 	}
9767 	/*
9768 	 * Now are any of the sack block ends the same (yes some
9769 	 * implementations send these)?
9770 	 */
9771 again:
9772 	if (num_sack_blks == 0)
9773 		goto out_with_totals;
9774 	if (num_sack_blks > 1) {
9775 		for (i = 0; i < num_sack_blks; i++) {
9776 			for (j = i + 1; j < num_sack_blks; j++) {
9777 				if (sack_blocks[i].end == sack_blocks[j].end) {
9778 					/*
9779 					 * Ok these two have the same end we
9780 					 * want the smallest end and then
9781 					 * throw away the larger and start
9782 					 * again.
9783 					 */
9784 					if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) {
9785 						/*
9786 						 * The second block covers
9787 						 * more area use that
9788 						 */
9789 						sack_blocks[i].start = sack_blocks[j].start;
9790 					}
9791 					/*
9792 					 * Now collapse out the dup-sack and
9793 					 * lower the count
9794 					 */
9795 					for (k = (j + 1); k < num_sack_blks; k++) {
9796 						sack_blocks[j].start = sack_blocks[k].start;
9797 						sack_blocks[j].end = sack_blocks[k].end;
9798 						j++;
9799 					}
9800 					num_sack_blks--;
9801 					goto again;
9802 				}
9803 			}
9804 		}
9805 	}
9806 do_sack_work:
9807 	/*
9808 	 * First lets look to see if
9809 	 * we have retransmitted and
9810 	 * can use the transmit next?
9811 	 */
9812 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
9813 	if (rsm &&
9814 	    SEQ_GT(sack_blocks[0].end, rsm->r_start) &&
9815 	    SEQ_LT(sack_blocks[0].start, rsm->r_end)) {
9816 		/*
9817 		 * We probably did the FR and the next
9818 		 * SACK in continues as we would expect.
9819 		 */
9820 		acked = rack_proc_sack_blk(tp, rack, &sack_blocks[0], to, &rsm, cts, &moved_two);
9821 		if (acked) {
9822 			rack->r_wanted_output = 1;
9823 			changed += acked;
9824 		}
9825 		if (num_sack_blks == 1) {
9826 			/*
9827 			 * This is what we would expect from
9828 			 * a normal implementation to happen
9829 			 * after we have retransmitted the FR,
9830 			 * i.e the sack-filter pushes down
9831 			 * to 1 block and the next to be retransmitted
9832 			 * is the sequence in the sack block (has more
9833 			 * are acked). Count this as ACK'd data to boost
9834 			 * up the chances of recovering any false positives.
9835 			 */
9836 			rack->r_ctl.ack_count += (acked / ctf_fixed_maxseg(rack->rc_tp));
9837 			counter_u64_add(rack_ack_total, (acked / ctf_fixed_maxseg(rack->rc_tp)));
9838 			counter_u64_add(rack_express_sack, 1);
9839 			if (rack->r_ctl.ack_count > 0xfff00000) {
9840 				/*
9841 				 * reduce the number to keep us under
9842 				 * a uint32_t.
9843 				 */
9844 				rack->r_ctl.ack_count /= 2;
9845 				rack->r_ctl.sack_count /= 2;
9846 			}
9847 			goto out_with_totals;
9848 		} else {
9849 			/*
9850 			 * Start the loop through the
9851 			 * rest of blocks, past the first block.
9852 			 */
9853 			moved_two = 0;
9854 			loop_start = 1;
9855 		}
9856 	}
9857 	/* Its a sack of some sort */
9858 	rack->r_ctl.sack_count++;
9859 	if (rack->r_ctl.sack_count > 0xfff00000) {
9860 		/*
9861 		 * reduce the number to keep us under
9862 		 * a uint32_t.
9863 		 */
9864 		rack->r_ctl.ack_count /= 2;
9865 		rack->r_ctl.sack_count /= 2;
9866 	}
9867 	counter_u64_add(rack_sack_total, 1);
9868 	if (rack->sack_attack_disable) {
9869 		/* An attacker disablement is in place */
9870 		if (num_sack_blks > 1) {
9871 			rack->r_ctl.sack_count += (num_sack_blks - 1);
9872 			rack->r_ctl.sack_moved_extra++;
9873 			counter_u64_add(rack_move_some, 1);
9874 			if (rack->r_ctl.sack_moved_extra > 0xfff00000) {
9875 				rack->r_ctl.sack_moved_extra /= 2;
9876 				rack->r_ctl.sack_noextra_move /= 2;
9877 			}
9878 		}
9879 		goto out;
9880 	}
9881 	rsm = rack->r_ctl.rc_sacklast;
9882 	for (i = loop_start; i < num_sack_blks; i++) {
9883 		acked = rack_proc_sack_blk(tp, rack, &sack_blocks[i], to, &rsm, cts, &moved_two);
9884 		if (acked) {
9885 			rack->r_wanted_output = 1;
9886 			changed += acked;
9887 		}
9888 		if (moved_two) {
9889 			/*
9890 			 * If we did not get a SACK for at least a MSS and
9891 			 * had to move at all, or if we moved more than our
9892 			 * threshold, it counts against the "extra" move.
9893 			 */
9894 			rack->r_ctl.sack_moved_extra += moved_two;
9895 			counter_u64_add(rack_move_some, 1);
9896 		} else {
9897 			/*
9898 			 * else we did not have to move
9899 			 * any more than we would expect.
9900 			 */
9901 			rack->r_ctl.sack_noextra_move++;
9902 			counter_u64_add(rack_move_none, 1);
9903 		}
9904 		if (moved_two && (acked < ctf_fixed_maxseg(rack->rc_tp))) {
9905 			/*
9906 			 * If the SACK was not a full MSS then
9907 			 * we add to sack_count the number of
9908 			 * MSS's (or possibly more than
9909 			 * a MSS if its a TSO send) we had to skip by.
9910 			 */
9911 			rack->r_ctl.sack_count += moved_two;
9912 			counter_u64_add(rack_sack_total, moved_two);
9913 		}
9914 		/*
9915 		 * Now we need to setup for the next
9916 		 * round. First we make sure we won't
9917 		 * exceed the size of our uint32_t on
9918 		 * the various counts, and then clear out
9919 		 * moved_two.
9920 		 */
9921 		if ((rack->r_ctl.sack_moved_extra > 0xfff00000) ||
9922 		    (rack->r_ctl.sack_noextra_move > 0xfff00000)) {
9923 			rack->r_ctl.sack_moved_extra /= 2;
9924 			rack->r_ctl.sack_noextra_move /= 2;
9925 		}
9926 		if (rack->r_ctl.sack_count > 0xfff00000) {
9927 			rack->r_ctl.ack_count /= 2;
9928 			rack->r_ctl.sack_count /= 2;
9929 		}
9930 		moved_two = 0;
9931 	}
9932 out_with_totals:
9933 	if (num_sack_blks > 1) {
9934 		/*
9935 		 * You get an extra stroke if
9936 		 * you have more than one sack-blk, this
9937 		 * could be where we are skipping forward
9938 		 * and the sack-filter is still working, or
9939 		 * it could be an attacker constantly
9940 		 * moving us.
9941 		 */
9942 		rack->r_ctl.sack_moved_extra++;
9943 		counter_u64_add(rack_move_some, 1);
9944 	}
9945 out:
9946 #ifdef NETFLIX_EXP_DETECTION
9947 	rack_do_detection(tp, rack, BYTES_THIS_ACK(tp, th), ctf_fixed_maxseg(rack->rc_tp));
9948 #endif
9949 	if (changed) {
9950 		/* Something changed cancel the rack timer */
9951 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
9952 	}
9953 	tsused = tcp_get_usecs(NULL);
9954 	rsm = tcp_rack_output(tp, rack, tsused);
9955 	if ((!IN_FASTRECOVERY(tp->t_flags)) &&
9956 	    rsm) {
9957 		/* Enter recovery */
9958 		rack->r_ctl.rc_rsm_start = rsm->r_start;
9959 		rack->r_ctl.rc_cwnd_at = tp->snd_cwnd;
9960 		rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh;
9961 		entered_recovery = 1;
9962 		rack_cong_signal(tp, CC_NDUPACK, tp->snd_una);
9963 		/*
9964 		 * When we enter recovery we need to assure we send
9965 		 * one packet.
9966 		 */
9967 		if (rack->rack_no_prr == 0) {
9968 			rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
9969 			rack_log_to_prr(rack, 8, 0);
9970 		}
9971 		rack->r_timer_override = 1;
9972 		rack->r_early = 0;
9973 		rack->r_ctl.rc_agg_early = 0;
9974 	} else if (IN_FASTRECOVERY(tp->t_flags) &&
9975 		   rsm &&
9976 		   (rack->r_rr_config == 3)) {
9977 		/*
9978 		 * Assure we can output and we get no
9979 		 * remembered pace time except the retransmit.
9980 		 */
9981 		rack->r_timer_override = 1;
9982 		rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
9983 		rack->r_ctl.rc_resend = rsm;
9984 	}
9985 	if (IN_FASTRECOVERY(tp->t_flags) &&
9986 	    (rack->rack_no_prr == 0) &&
9987 	    (entered_recovery == 0)) {
9988 		rack_update_prr(tp, rack, changed, th_ack);
9989 		if ((rsm && (rack->r_ctl.rc_prr_sndcnt >= ctf_fixed_maxseg(tp)) &&
9990 		     ((tcp_in_hpts(rack->rc_inp) == 0) &&
9991 		      ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)))) {
9992 			/*
9993 			 * If you are pacing output you don't want
9994 			 * to override.
9995 			 */
9996 			rack->r_early = 0;
9997 			rack->r_ctl.rc_agg_early = 0;
9998 			rack->r_timer_override = 1;
9999 		}
10000 	}
10001 }
10002 
10003 static void
10004 rack_strike_dupack(struct tcp_rack *rack)
10005 {
10006 	struct rack_sendmap *rsm;
10007 
10008 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
10009 	while (rsm && (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
10010 		rsm = TAILQ_NEXT(rsm, r_tnext);
10011 	}
10012 	if (rsm && (rsm->r_dupack < 0xff)) {
10013 		rsm->r_dupack++;
10014 		if (rsm->r_dupack >= DUP_ACK_THRESHOLD) {
10015 			struct timeval tv;
10016 			uint32_t cts;
10017 			/*
10018 			 * Here we see if we need to retransmit. For
10019 			 * a SACK type connection if enough time has passed
10020 			 * we will get a return of the rsm. For a non-sack
10021 			 * connection we will get the rsm returned if the
10022 			 * dupack value is 3 or more.
10023 			 */
10024 			cts = tcp_get_usecs(&tv);
10025 			rack->r_ctl.rc_resend = tcp_rack_output(rack->rc_tp, rack, cts);
10026 			if (rack->r_ctl.rc_resend != NULL) {
10027 				if (!IN_FASTRECOVERY(rack->rc_tp->t_flags)) {
10028 					rack_cong_signal(rack->rc_tp, CC_NDUPACK,
10029 							 rack->rc_tp->snd_una);
10030 				}
10031 				rack->r_wanted_output = 1;
10032 				rack->r_timer_override = 1;
10033 				rack_log_retran_reason(rack, rsm, __LINE__, 1, 3);
10034 			}
10035 		} else {
10036 			rack_log_retran_reason(rack, rsm, __LINE__, 0, 3);
10037 		}
10038 	}
10039 }
10040 
10041 static void
10042 rack_check_bottom_drag(struct tcpcb *tp,
10043 		       struct tcp_rack *rack,
10044 		       struct socket *so, int32_t acked)
10045 {
10046 	uint32_t segsiz, minseg;
10047 
10048 	segsiz = ctf_fixed_maxseg(tp);
10049 	minseg = segsiz;
10050 
10051 	if (tp->snd_max == tp->snd_una) {
10052 		/*
10053 		 * We are doing dynamic pacing and we are way
10054 		 * under. Basically everything got acked while
10055 		 * we were still waiting on the pacer to expire.
10056 		 *
10057 		 * This means we need to boost the b/w in
10058 		 * addition to any earlier boosting of
10059 		 * the multipler.
10060 		 */
10061 		rack->rc_dragged_bottom = 1;
10062 		rack_validate_multipliers_at_or_above100(rack);
10063 		/*
10064 		 * Lets use the segment bytes acked plus
10065 		 * the lowest RTT seen as the basis to
10066 		 * form a b/w estimate. This will be off
10067 		 * due to the fact that the true estimate
10068 		 * should be around 1/2 the time of the RTT
10069 		 * but we can settle for that.
10070 		 */
10071 		if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_VALID) &&
10072 		    acked) {
10073 			uint64_t bw, calc_bw, rtt;
10074 
10075 			rtt = rack->r_ctl.rack_rs.rs_us_rtt;
10076 			if (rtt == 0) {
10077 				/* no us sample is there a ms one? */
10078 				if (rack->r_ctl.rack_rs.rs_rtt_lowest) {
10079 					rtt = rack->r_ctl.rack_rs.rs_rtt_lowest;
10080 				} else {
10081 					goto no_measurement;
10082 				}
10083 			}
10084 			bw = acked;
10085 			calc_bw = bw * 1000000;
10086 			calc_bw /= rtt;
10087 			if (rack->r_ctl.last_max_bw &&
10088 			    (rack->r_ctl.last_max_bw < calc_bw)) {
10089 				/*
10090 				 * If we have a last calculated max bw
10091 				 * enforce it.
10092 				 */
10093 				calc_bw = rack->r_ctl.last_max_bw;
10094 			}
10095 			/* now plop it in */
10096 			if (rack->rc_gp_filled == 0) {
10097 				if (calc_bw > ONE_POINT_TWO_MEG) {
10098 					/*
10099 					 * If we have no measurement
10100 					 * don't let us set in more than
10101 					 * 1.2Mbps. If we are still too
10102 					 * low after pacing with this we
10103 					 * will hopefully have a max b/w
10104 					 * available to sanity check things.
10105 					 */
10106 					calc_bw = ONE_POINT_TWO_MEG;
10107 				}
10108 				rack->r_ctl.rc_rtt_diff = 0;
10109 				rack->r_ctl.gp_bw = calc_bw;
10110 				rack->rc_gp_filled = 1;
10111 				if (rack->r_ctl.num_measurements < RACK_REQ_AVG)
10112 					rack->r_ctl.num_measurements = RACK_REQ_AVG;
10113 				rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
10114 			} else if (calc_bw > rack->r_ctl.gp_bw) {
10115 				rack->r_ctl.rc_rtt_diff = 0;
10116 				if (rack->r_ctl.num_measurements < RACK_REQ_AVG)
10117 					rack->r_ctl.num_measurements = RACK_REQ_AVG;
10118 				rack->r_ctl.gp_bw = calc_bw;
10119 				rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
10120 			} else
10121 				rack_increase_bw_mul(rack, -1, 0, 0, 1);
10122 			if ((rack->gp_ready == 0) &&
10123 			    (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) {
10124 				/* We have enough measurements now */
10125 				rack->gp_ready = 1;
10126 				rack_set_cc_pacing(rack);
10127 				if (rack->defer_options)
10128 					rack_apply_deferred_options(rack);
10129 			}
10130 			/*
10131 			 * For acks over 1mss we do a extra boost to simulate
10132 			 * where we would get 2 acks (we want 110 for the mul).
10133 			 */
10134 			if (acked > segsiz)
10135 				rack_increase_bw_mul(rack, -1, 0, 0, 1);
10136 		} else {
10137 			/*
10138 			 * zero rtt possibly?, settle for just an old increase.
10139 			 */
10140 no_measurement:
10141 			rack_increase_bw_mul(rack, -1, 0, 0, 1);
10142 		}
10143 	} else if ((IN_FASTRECOVERY(tp->t_flags) == 0) &&
10144 		   (sbavail(&so->so_snd) > max((segsiz * (4 + rack_req_segs)),
10145 					       minseg)) &&
10146 		   (rack->r_ctl.cwnd_to_use > max((segsiz * (rack_req_segs + 2)), minseg)) &&
10147 		   (tp->snd_wnd > max((segsiz * (rack_req_segs + 2)), minseg)) &&
10148 		   (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) <=
10149 		    (segsiz * rack_req_segs))) {
10150 		/*
10151 		 * We are doing dynamic GP pacing and
10152 		 * we have everything except 1MSS or less
10153 		 * bytes left out. We are still pacing away.
10154 		 * And there is data that could be sent, This
10155 		 * means we are inserting delayed ack time in
10156 		 * our measurements because we are pacing too slow.
10157 		 */
10158 		rack_validate_multipliers_at_or_above100(rack);
10159 		rack->rc_dragged_bottom = 1;
10160 		rack_increase_bw_mul(rack, -1, 0, 0, 1);
10161 	}
10162 }
10163 
10164 
10165 
10166 static void
10167 rack_gain_for_fastoutput(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t acked_amount)
10168 {
10169 	/*
10170 	 * The fast output path is enabled and we
10171 	 * have moved the cumack forward. Lets see if
10172 	 * we can expand forward the fast path length by
10173 	 * that amount. What we would ideally like to
10174 	 * do is increase the number of bytes in the
10175 	 * fast path block (left_to_send) by the
10176 	 * acked amount. However we have to gate that
10177 	 * by two factors:
10178 	 * 1) The amount outstanding and the rwnd of the peer
10179 	 *    (i.e. we don't want to exceed the rwnd of the peer).
10180 	 *    <and>
10181 	 * 2) The amount of data left in the socket buffer (i.e.
10182 	 *    we can't send beyond what is in the buffer).
10183 	 *
10184 	 * Note that this does not take into account any increase
10185 	 * in the cwnd. We will only extend the fast path by
10186 	 * what was acked.
10187 	 */
10188 	uint32_t new_total, gating_val;
10189 
10190 	new_total = acked_amount + rack->r_ctl.fsb.left_to_send;
10191 	gating_val = min((sbavail(&so->so_snd) - (tp->snd_max - tp->snd_una)),
10192 			 (tp->snd_wnd - (tp->snd_max - tp->snd_una)));
10193 	if (new_total <= gating_val) {
10194 		/* We can increase left_to_send by the acked amount */
10195 		counter_u64_add(rack_extended_rfo, 1);
10196 		rack->r_ctl.fsb.left_to_send = new_total;
10197 		KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(&rack->rc_inp->inp_socket->so_snd) - (tp->snd_max - tp->snd_una))),
10198 			("rack:%p left_to_send:%u sbavail:%u out:%u",
10199 			 rack, rack->r_ctl.fsb.left_to_send,
10200 			 sbavail(&rack->rc_inp->inp_socket->so_snd),
10201 			 (tp->snd_max - tp->snd_una)));
10202 
10203 	}
10204 }
10205 
10206 static void
10207 rack_adjust_sendmap(struct tcp_rack *rack, struct sockbuf *sb, tcp_seq snd_una)
10208 {
10209 	/*
10210 	 * Here any sendmap entry that points to the
10211 	 * beginning mbuf must be adjusted to the correct
10212 	 * offset. This must be called with:
10213 	 * 1) The socket buffer locked
10214 	 * 2) snd_una adjusted to its new postion.
10215 	 *
10216 	 * Note that (2) implies rack_ack_received has also
10217 	 * been called.
10218 	 *
10219 	 * We grab the first mbuf in the socket buffer and
10220 	 * then go through the front of the sendmap, recalculating
10221 	 * the stored offset for any sendmap entry that has
10222 	 * that mbuf. We must use the sb functions to do this
10223 	 * since its possible an add was done has well as
10224 	 * the subtraction we may have just completed. This should
10225 	 * not be a penalty though, since we just referenced the sb
10226 	 * to go in and trim off the mbufs that we freed (of course
10227 	 * there will be a penalty for the sendmap references though).
10228 	 */
10229 	struct mbuf *m;
10230 	struct rack_sendmap *rsm;
10231 
10232 	SOCKBUF_LOCK_ASSERT(sb);
10233 	m = sb->sb_mb;
10234 	rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
10235 	if ((rsm == NULL) || (m == NULL)) {
10236 		/* Nothing outstanding */
10237 		return;
10238 	}
10239 	while (rsm->m && (rsm->m == m)) {
10240 		/* one to adjust */
10241 #ifdef INVARIANTS
10242 		struct mbuf *tm;
10243 		uint32_t soff;
10244 
10245 		tm = sbsndmbuf(sb, (rsm->r_start - snd_una), &soff);
10246 		if (rsm->orig_m_len != m->m_len) {
10247 			rack_adjust_orig_mlen(rsm);
10248 		}
10249 		if (rsm->soff != soff) {
10250 			/*
10251 			 * This is not a fatal error, we anticipate it
10252 			 * might happen (the else code), so we count it here
10253 			 * so that under invariant we can see that it really
10254 			 * does happen.
10255 			 */
10256 			counter_u64_add(rack_adjust_map_bw, 1);
10257 		}
10258 		rsm->m = tm;
10259 		rsm->soff = soff;
10260 		if (tm)
10261 			rsm->orig_m_len = rsm->m->m_len;
10262 		else
10263 			rsm->orig_m_len = 0;
10264 #else
10265 		rsm->m = sbsndmbuf(sb, (rsm->r_start - snd_una), &rsm->soff);
10266 		if (rsm->m)
10267 			rsm->orig_m_len = rsm->m->m_len;
10268 		else
10269 			rsm->orig_m_len = 0;
10270 #endif
10271 		rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree,
10272 			      rsm);
10273 		if (rsm == NULL)
10274 			break;
10275 	}
10276 }
10277 
10278 /*
10279  * Return value of 1, we do not need to call rack_process_data().
10280  * return value of 0, rack_process_data can be called.
10281  * For ret_val if its 0 the TCP is locked, if its non-zero
10282  * its unlocked and probably unsafe to touch the TCB.
10283  */
10284 static int
10285 rack_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so,
10286     struct tcpcb *tp, struct tcpopt *to,
10287     uint32_t tiwin, int32_t tlen,
10288     int32_t * ofia, int32_t thflags, int32_t *ret_val)
10289 {
10290 	int32_t ourfinisacked = 0;
10291 	int32_t nsegs, acked_amount;
10292 	int32_t acked;
10293 	struct mbuf *mfree;
10294 	struct tcp_rack *rack;
10295 	int32_t under_pacing = 0;
10296 	int32_t recovery = 0;
10297 
10298 	rack = (struct tcp_rack *)tp->t_fb_ptr;
10299 	if (SEQ_GT(th->th_ack, tp->snd_max)) {
10300 		__ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val,
10301 				      &rack->r_ctl.challenge_ack_ts,
10302 				      &rack->r_ctl.challenge_ack_cnt);
10303 		rack->r_wanted_output = 1;
10304 		return (1);
10305 	}
10306 	if (rack->gp_ready &&
10307 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
10308 		under_pacing = 1;
10309 	}
10310 	if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) {
10311 		int in_rec, dup_ack_struck = 0;
10312 
10313 		in_rec = IN_FASTRECOVERY(tp->t_flags);
10314 		if (rack->rc_in_persist) {
10315 			tp->t_rxtshift = 0;
10316 			RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
10317 				      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
10318 		}
10319 		if ((th->th_ack == tp->snd_una) &&
10320 		    (tiwin == tp->snd_wnd) &&
10321 		    ((to->to_flags & TOF_SACK) == 0)) {
10322 			rack_strike_dupack(rack);
10323 			dup_ack_struck = 1;
10324 		}
10325 		rack_log_ack(tp, to, th, ((in_rec == 0) && IN_FASTRECOVERY(tp->t_flags)), dup_ack_struck);
10326 	}
10327 	if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
10328 		/*
10329 		 * Old ack, behind (or duplicate to) the last one rcv'd
10330 		 * Note: We mark reordering is occuring if its
10331 		 * less than and we have not closed our window.
10332 		 */
10333 		if (SEQ_LT(th->th_ack, tp->snd_una) && (sbspace(&so->so_rcv) > ctf_fixed_maxseg(tp))) {
10334 			counter_u64_add(rack_reorder_seen, 1);
10335 			rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
10336 		}
10337 		return (0);
10338 	}
10339 	/*
10340 	 * If we reach this point, ACK is not a duplicate, i.e., it ACKs
10341 	 * something we sent.
10342 	 */
10343 	if (tp->t_flags & TF_NEEDSYN) {
10344 		/*
10345 		 * T/TCP: Connection was half-synchronized, and our SYN has
10346 		 * been ACK'd (so connection is now fully synchronized).  Go
10347 		 * to non-starred state, increment snd_una for ACK of SYN,
10348 		 * and check if we can do window scaling.
10349 		 */
10350 		tp->t_flags &= ~TF_NEEDSYN;
10351 		tp->snd_una++;
10352 		/* Do window scaling? */
10353 		if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
10354 		    (TF_RCVD_SCALE | TF_REQ_SCALE)) {
10355 			tp->rcv_scale = tp->request_r_scale;
10356 			/* Send window already scaled. */
10357 		}
10358 	}
10359 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
10360 	INP_WLOCK_ASSERT(tp->t_inpcb);
10361 
10362 	acked = BYTES_THIS_ACK(tp, th);
10363 	if (acked) {
10364 		/*
10365 		 * Any time we move the cum-ack forward clear
10366 		 * keep-alive tied probe-not-answered. The
10367 		 * persists clears its own on entry.
10368 		 */
10369 		rack->probe_not_answered = 0;
10370 	}
10371 	KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
10372 	KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
10373 	/*
10374 	 * If we just performed our first retransmit, and the ACK arrives
10375 	 * within our recovery window, then it was a mistake to do the
10376 	 * retransmit in the first place.  Recover our original cwnd and
10377 	 * ssthresh, and proceed to transmit where we left off.
10378 	 */
10379 	if ((tp->t_flags & TF_PREVVALID) &&
10380 	    ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
10381 		tp->t_flags &= ~TF_PREVVALID;
10382 		if (tp->t_rxtshift == 1 &&
10383 		    (int)(ticks - tp->t_badrxtwin) < 0)
10384 			rack_cong_signal(tp, CC_RTO_ERR, th->th_ack);
10385 	}
10386 	if (acked) {
10387 		/* assure we are not backed off */
10388 		tp->t_rxtshift = 0;
10389 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
10390 			      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
10391 		rack->rc_tlp_in_progress = 0;
10392 		rack->r_ctl.rc_tlp_cnt_out = 0;
10393 		/*
10394 		 * If it is the RXT timer we want to
10395 		 * stop it, so we can restart a TLP.
10396 		 */
10397 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
10398 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
10399 #ifdef NETFLIX_HTTP_LOGGING
10400 		tcp_http_check_for_comp(rack->rc_tp, th->th_ack);
10401 #endif
10402 	}
10403 	/*
10404 	 * If we have a timestamp reply, update smoothed round trip time. If
10405 	 * no timestamp is present but transmit timer is running and timed
10406 	 * sequence number was acked, update smoothed round trip time. Since
10407 	 * we now have an rtt measurement, cancel the timer backoff (cf.,
10408 	 * Phil Karn's retransmit alg.). Recompute the initial retransmit
10409 	 * timer.
10410 	 *
10411 	 * Some boxes send broken timestamp replies during the SYN+ACK
10412 	 * phase, ignore timestamps of 0 or we could calculate a huge RTT
10413 	 * and blow up the retransmit timer.
10414 	 */
10415 	/*
10416 	 * If all outstanding data is acked, stop retransmit timer and
10417 	 * remember to restart (more output or persist). If there is more
10418 	 * data to be acked, restart retransmit timer, using current
10419 	 * (possibly backed-off) value.
10420 	 */
10421 	if (acked == 0) {
10422 		if (ofia)
10423 			*ofia = ourfinisacked;
10424 		return (0);
10425 	}
10426 	if (IN_RECOVERY(tp->t_flags)) {
10427 		if (SEQ_LT(th->th_ack, tp->snd_recover) &&
10428 		    (SEQ_LT(th->th_ack, tp->snd_max))) {
10429 			tcp_rack_partialack(tp);
10430 		} else {
10431 			rack_post_recovery(tp, th->th_ack);
10432 			recovery = 1;
10433 		}
10434 	}
10435 	/*
10436 	 * Let the congestion control algorithm update congestion control
10437 	 * related information. This typically means increasing the
10438 	 * congestion window.
10439 	 */
10440 	rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, recovery);
10441 	SOCKBUF_LOCK(&so->so_snd);
10442 	acked_amount = min(acked, (int)sbavail(&so->so_snd));
10443 	tp->snd_wnd -= acked_amount;
10444 	mfree = sbcut_locked(&so->so_snd, acked_amount);
10445 	if ((sbused(&so->so_snd) == 0) &&
10446 	    (acked > acked_amount) &&
10447 	    (tp->t_state >= TCPS_FIN_WAIT_1) &&
10448 	    (tp->t_flags & TF_SENTFIN)) {
10449 		/*
10450 		 * We must be sure our fin
10451 		 * was sent and acked (we can be
10452 		 * in FIN_WAIT_1 without having
10453 		 * sent the fin).
10454 		 */
10455 		ourfinisacked = 1;
10456 	}
10457 	tp->snd_una = th->th_ack;
10458 	if (acked_amount && sbavail(&so->so_snd))
10459 		rack_adjust_sendmap(rack, &so->so_snd, tp->snd_una);
10460 	rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
10461 	/* NB: sowwakeup_locked() does an implicit unlock. */
10462 	sowwakeup_locked(so);
10463 	m_freem(mfree);
10464 	if (SEQ_GT(tp->snd_una, tp->snd_recover))
10465 		tp->snd_recover = tp->snd_una;
10466 
10467 	if (SEQ_LT(tp->snd_nxt, tp->snd_una)) {
10468 		tp->snd_nxt = tp->snd_una;
10469 	}
10470 	if (under_pacing &&
10471 	    (rack->use_fixed_rate == 0) &&
10472 	    (rack->in_probe_rtt == 0) &&
10473 	    rack->rc_gp_dyn_mul &&
10474 	    rack->rc_always_pace) {
10475 		/* Check if we are dragging bottom */
10476 		rack_check_bottom_drag(tp, rack, so, acked);
10477 	}
10478 	if (tp->snd_una == tp->snd_max) {
10479 		/* Nothing left outstanding */
10480 		tp->t_flags &= ~TF_PREVVALID;
10481 		rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
10482 		rack->r_ctl.retran_during_recovery = 0;
10483 		rack->r_ctl.dsack_byte_cnt = 0;
10484 		if (rack->r_ctl.rc_went_idle_time == 0)
10485 			rack->r_ctl.rc_went_idle_time = 1;
10486 		rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
10487 		if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0)
10488 			tp->t_acktime = 0;
10489 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
10490 		/* Set need output so persist might get set */
10491 		rack->r_wanted_output = 1;
10492 		sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
10493 		if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
10494 		    (sbavail(&so->so_snd) == 0) &&
10495 		    (tp->t_flags2 & TF2_DROP_AF_DATA)) {
10496 			/*
10497 			 * The socket was gone and the
10498 			 * peer sent data (now or in the past), time to
10499 			 * reset him.
10500 			 */
10501 			*ret_val = 1;
10502 			/* tcp_close will kill the inp pre-log the Reset */
10503 			tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
10504 			tp = tcp_close(tp);
10505 			ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen);
10506 			return (1);
10507 		}
10508 	}
10509 	if (ofia)
10510 		*ofia = ourfinisacked;
10511 	return (0);
10512 }
10513 
10514 static void
10515 rack_collapsed_window(struct tcp_rack *rack)
10516 {
10517 	/*
10518 	 * Now we must walk the
10519 	 * send map and divide the
10520 	 * ones left stranded. These
10521 	 * guys can't cause us to abort
10522 	 * the connection and are really
10523 	 * "unsent". However if a buggy
10524 	 * client actually did keep some
10525 	 * of the data i.e. collapsed the win
10526 	 * and refused to ack and then opened
10527 	 * the win and acked that data. We would
10528 	 * get into an ack war, the simplier
10529 	 * method then of just pretending we
10530 	 * did not send those segments something
10531 	 * won't work.
10532 	 */
10533 	struct rack_sendmap *rsm, *nrsm, fe;
10534 #ifdef INVARIANTS
10535 	struct rack_sendmap *insret;
10536 #endif
10537 	tcp_seq max_seq;
10538 
10539 	max_seq = rack->rc_tp->snd_una + rack->rc_tp->snd_wnd;
10540 	memset(&fe, 0, sizeof(fe));
10541 	fe.r_start = max_seq;
10542 	/* Find the first seq past or at maxseq */
10543 	rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
10544 	if (rsm == NULL) {
10545 		/* Nothing to do strange */
10546 		rack->rc_has_collapsed = 0;
10547 		return;
10548 	}
10549 	/*
10550 	 * Now do we need to split at
10551 	 * the collapse point?
10552 	 */
10553 	if (SEQ_GT(max_seq, rsm->r_start)) {
10554 		nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
10555 		if (nrsm == NULL) {
10556 			/* We can't get a rsm, mark all? */
10557 			nrsm = rsm;
10558 			goto no_split;
10559 		}
10560 		/* Clone it */
10561 		rack_clone_rsm(rack, nrsm, rsm, max_seq);
10562 #ifndef INVARIANTS
10563 		(void)RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
10564 #else
10565 		insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
10566 		if (insret != NULL) {
10567 			panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
10568 			      nrsm, insret, rack, rsm);
10569 		}
10570 #endif
10571 		rack_log_map_chg(rack->rc_tp, rack, NULL, rsm, nrsm, MAP_SPLIT, max_seq, __LINE__);
10572 		if (rsm->r_in_tmap) {
10573 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
10574 			nrsm->r_in_tmap = 1;
10575 		}
10576 		/*
10577 		 * Set in the new RSM as the
10578 		 * collapsed starting point
10579 		 */
10580 		rsm = nrsm;
10581 	}
10582 no_split:
10583 	counter_u64_add(rack_collapsed_win, 1);
10584 	RB_FOREACH_FROM(nrsm, rack_rb_tree_head, rsm) {
10585 		nrsm->r_flags |= RACK_RWND_COLLAPSED;
10586 	}
10587 	rack->rc_has_collapsed = 1;
10588 }
10589 
10590 static void
10591 rack_un_collapse_window(struct tcp_rack *rack)
10592 {
10593 	struct rack_sendmap *rsm;
10594 
10595 	RB_FOREACH_REVERSE(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
10596 		if (rsm->r_flags & RACK_RWND_COLLAPSED)
10597 			rsm->r_flags &= ~RACK_RWND_COLLAPSED;
10598 		else
10599 			break;
10600 	}
10601 	rack->rc_has_collapsed = 0;
10602 }
10603 
10604 static void
10605 rack_handle_delayed_ack(struct tcpcb *tp, struct tcp_rack *rack,
10606 			int32_t tlen, int32_t tfo_syn)
10607 {
10608 	if (DELAY_ACK(tp, tlen) || tfo_syn) {
10609 		if (rack->rc_dack_mode &&
10610 		    (tlen > 500) &&
10611 		    (rack->rc_dack_toggle == 1)) {
10612 			goto no_delayed_ack;
10613 		}
10614 		rack_timer_cancel(tp, rack,
10615 				  rack->r_ctl.rc_rcvtime, __LINE__);
10616 		tp->t_flags |= TF_DELACK;
10617 	} else {
10618 no_delayed_ack:
10619 		rack->r_wanted_output = 1;
10620 		tp->t_flags |= TF_ACKNOW;
10621 		if (rack->rc_dack_mode) {
10622 			if (tp->t_flags & TF_DELACK)
10623 				rack->rc_dack_toggle = 1;
10624 			else
10625 				rack->rc_dack_toggle = 0;
10626 		}
10627 	}
10628 }
10629 
10630 static void
10631 rack_validate_fo_sendwin_up(struct tcpcb *tp, struct tcp_rack *rack)
10632 {
10633 	/*
10634 	 * If fast output is in progress, lets validate that
10635 	 * the new window did not shrink on us and make it
10636 	 * so fast output should end.
10637 	 */
10638 	if (rack->r_fast_output) {
10639 		uint32_t out;
10640 
10641 		/*
10642 		 * Calculate what we will send if left as is
10643 		 * and compare that to our send window.
10644 		 */
10645 		out = ctf_outstanding(tp);
10646 		if ((out + rack->r_ctl.fsb.left_to_send) > tp->snd_wnd) {
10647 			/* ok we have an issue */
10648 			if (out >= tp->snd_wnd) {
10649 				/* Turn off fast output the window is met or collapsed */
10650 				rack->r_fast_output = 0;
10651 			} else {
10652 				/* we have some room left */
10653 				rack->r_ctl.fsb.left_to_send = tp->snd_wnd - out;
10654 				if (rack->r_ctl.fsb.left_to_send < ctf_fixed_maxseg(tp)) {
10655 					/* If not at least 1 full segment never mind */
10656 					rack->r_fast_output = 0;
10657 				}
10658 			}
10659 		}
10660 	}
10661 }
10662 
10663 
10664 /*
10665  * Return value of 1, the TCB is unlocked and most
10666  * likely gone, return value of 0, the TCP is still
10667  * locked.
10668  */
10669 static int
10670 rack_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so,
10671     struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
10672     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
10673 {
10674 	/*
10675 	 * Update window information. Don't look at window if no ACK: TAC's
10676 	 * send garbage on first SYN.
10677 	 */
10678 	int32_t nsegs;
10679 	int32_t tfo_syn;
10680 	struct tcp_rack *rack;
10681 
10682 	rack = (struct tcp_rack *)tp->t_fb_ptr;
10683 	INP_WLOCK_ASSERT(tp->t_inpcb);
10684 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
10685 	if ((thflags & TH_ACK) &&
10686 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
10687 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
10688 	    (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
10689 		/* keep track of pure window updates */
10690 		if (tlen == 0 &&
10691 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
10692 			KMOD_TCPSTAT_INC(tcps_rcvwinupd);
10693 		tp->snd_wnd = tiwin;
10694 		rack_validate_fo_sendwin_up(tp, rack);
10695 		tp->snd_wl1 = th->th_seq;
10696 		tp->snd_wl2 = th->th_ack;
10697 		if (tp->snd_wnd > tp->max_sndwnd)
10698 			tp->max_sndwnd = tp->snd_wnd;
10699 		rack->r_wanted_output = 1;
10700 	} else if (thflags & TH_ACK) {
10701 		if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) {
10702 			tp->snd_wnd = tiwin;
10703 			rack_validate_fo_sendwin_up(tp, rack);
10704 			tp->snd_wl1 = th->th_seq;
10705 			tp->snd_wl2 = th->th_ack;
10706 		}
10707 	}
10708 	if (tp->snd_wnd < ctf_outstanding(tp))
10709 		/* The peer collapsed the window */
10710 		rack_collapsed_window(rack);
10711 	else if (rack->rc_has_collapsed)
10712 		rack_un_collapse_window(rack);
10713 	/* Was persist timer active and now we have window space? */
10714 	if ((rack->rc_in_persist != 0) &&
10715 	    (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
10716 				rack->r_ctl.rc_pace_min_segs))) {
10717 		rack_exit_persist(tp, rack, rack->r_ctl.rc_rcvtime);
10718 		tp->snd_nxt = tp->snd_max;
10719 		/* Make sure we output to start the timer */
10720 		rack->r_wanted_output = 1;
10721 	}
10722 	/* Do we enter persists? */
10723 	if ((rack->rc_in_persist == 0) &&
10724 	    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
10725 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
10726 	    (tp->snd_max == tp->snd_una) &&
10727 	    sbavail(&tp->t_inpcb->inp_socket->so_snd) &&
10728 	    (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) {
10729 		/*
10730 		 * Here the rwnd is less than
10731 		 * the pacing size, we are established,
10732 		 * nothing is outstanding, and there is
10733 		 * data to send. Enter persists.
10734 		 */
10735 		rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime);
10736 	}
10737 	if (tp->t_flags2 & TF2_DROP_AF_DATA) {
10738 		m_freem(m);
10739 		return (0);
10740 	}
10741 	/*
10742 	 * don't process the URG bit, ignore them drag
10743 	 * along the up.
10744 	 */
10745 	tp->rcv_up = tp->rcv_nxt;
10746 	INP_WLOCK_ASSERT(tp->t_inpcb);
10747 
10748 	/*
10749 	 * Process the segment text, merging it into the TCP sequencing
10750 	 * queue, and arranging for acknowledgment of receipt if necessary.
10751 	 * This process logically involves adjusting tp->rcv_wnd as data is
10752 	 * presented to the user (this happens in tcp_usrreq.c, case
10753 	 * PRU_RCVD).  If a FIN has already been received on this connection
10754 	 * then we just ignore the text.
10755 	 */
10756 	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
10757 		   IS_FASTOPEN(tp->t_flags));
10758 	if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
10759 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
10760 		tcp_seq save_start = th->th_seq;
10761 		tcp_seq save_rnxt  = tp->rcv_nxt;
10762 		int     save_tlen  = tlen;
10763 
10764 		m_adj(m, drop_hdrlen);	/* delayed header drop */
10765 		/*
10766 		 * Insert segment which includes th into TCP reassembly
10767 		 * queue with control block tp.  Set thflags to whether
10768 		 * reassembly now includes a segment with FIN.  This handles
10769 		 * the common case inline (segment is the next to be
10770 		 * received on an established connection, and the queue is
10771 		 * empty), avoiding linkage into and removal from the queue
10772 		 * and repetition of various conversions. Set DELACK for
10773 		 * segments received in order, but ack immediately when
10774 		 * segments are out of order (so fast retransmit can work).
10775 		 */
10776 		if (th->th_seq == tp->rcv_nxt &&
10777 		    SEGQ_EMPTY(tp) &&
10778 		    (TCPS_HAVEESTABLISHED(tp->t_state) ||
10779 		    tfo_syn)) {
10780 #ifdef NETFLIX_SB_LIMITS
10781 			u_int mcnt, appended;
10782 
10783 			if (so->so_rcv.sb_shlim) {
10784 				mcnt = m_memcnt(m);
10785 				appended = 0;
10786 				if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
10787 				    CFO_NOSLEEP, NULL) == false) {
10788 					counter_u64_add(tcp_sb_shlim_fails, 1);
10789 					m_freem(m);
10790 					return (0);
10791 				}
10792 			}
10793 #endif
10794 			rack_handle_delayed_ack(tp, rack, tlen, tfo_syn);
10795 			tp->rcv_nxt += tlen;
10796 			if (tlen &&
10797 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
10798 			    (tp->t_fbyte_in == 0)) {
10799 				tp->t_fbyte_in = ticks;
10800 				if (tp->t_fbyte_in == 0)
10801 					tp->t_fbyte_in = 1;
10802 				if (tp->t_fbyte_out && tp->t_fbyte_in)
10803 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
10804 			}
10805 			thflags = tcp_get_flags(th) & TH_FIN;
10806 			KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs);
10807 			KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
10808 			SOCKBUF_LOCK(&so->so_rcv);
10809 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
10810 				m_freem(m);
10811 			} else
10812 #ifdef NETFLIX_SB_LIMITS
10813 				appended =
10814 #endif
10815 					sbappendstream_locked(&so->so_rcv, m, 0);
10816 
10817 			rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1);
10818 			/* NB: sorwakeup_locked() does an implicit unlock. */
10819 			sorwakeup_locked(so);
10820 #ifdef NETFLIX_SB_LIMITS
10821 			if (so->so_rcv.sb_shlim && appended != mcnt)
10822 				counter_fo_release(so->so_rcv.sb_shlim,
10823 				    mcnt - appended);
10824 #endif
10825 		} else {
10826 			/*
10827 			 * XXX: Due to the header drop above "th" is
10828 			 * theoretically invalid by now.  Fortunately
10829 			 * m_adj() doesn't actually frees any mbufs when
10830 			 * trimming from the head.
10831 			 */
10832 			tcp_seq temp = save_start;
10833 
10834 			thflags = tcp_reass(tp, th, &temp, &tlen, m);
10835 			tp->t_flags |= TF_ACKNOW;
10836 			if (tp->t_flags & TF_WAKESOR) {
10837 				tp->t_flags &= ~TF_WAKESOR;
10838 				/* NB: sorwakeup_locked() does an implicit unlock. */
10839 				sorwakeup_locked(so);
10840 			}
10841 		}
10842 		if ((tp->t_flags & TF_SACK_PERMIT) &&
10843 		    (save_tlen > 0) &&
10844 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
10845 			if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
10846 				/*
10847 				 * DSACK actually handled in the fastpath
10848 				 * above.
10849 				 */
10850 				RACK_OPTS_INC(tcp_sack_path_1);
10851 				tcp_update_sack_list(tp, save_start,
10852 				    save_start + save_tlen);
10853 			} else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
10854 				if ((tp->rcv_numsacks >= 1) &&
10855 				    (tp->sackblks[0].end == save_start)) {
10856 					/*
10857 					 * Partial overlap, recorded at todrop
10858 					 * above.
10859 					 */
10860 					RACK_OPTS_INC(tcp_sack_path_2a);
10861 					tcp_update_sack_list(tp,
10862 					    tp->sackblks[0].start,
10863 					    tp->sackblks[0].end);
10864 				} else {
10865 					RACK_OPTS_INC(tcp_sack_path_2b);
10866 					tcp_update_dsack_list(tp, save_start,
10867 					    save_start + save_tlen);
10868 				}
10869 			} else if (tlen >= save_tlen) {
10870 				/* Update of sackblks. */
10871 				RACK_OPTS_INC(tcp_sack_path_3);
10872 				tcp_update_dsack_list(tp, save_start,
10873 				    save_start + save_tlen);
10874 			} else if (tlen > 0) {
10875 				RACK_OPTS_INC(tcp_sack_path_4);
10876 				tcp_update_dsack_list(tp, save_start,
10877 				    save_start + tlen);
10878 			}
10879 		}
10880 	} else {
10881 		m_freem(m);
10882 		thflags &= ~TH_FIN;
10883 	}
10884 
10885 	/*
10886 	 * If FIN is received ACK the FIN and let the user know that the
10887 	 * connection is closing.
10888 	 */
10889 	if (thflags & TH_FIN) {
10890 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
10891 			/* The socket upcall is handled by socantrcvmore. */
10892 			socantrcvmore(so);
10893 			/*
10894 			 * If connection is half-synchronized (ie NEEDSYN
10895 			 * flag on) then delay ACK, so it may be piggybacked
10896 			 * when SYN is sent. Otherwise, since we received a
10897 			 * FIN then no more input can be expected, send ACK
10898 			 * now.
10899 			 */
10900 			if (tp->t_flags & TF_NEEDSYN) {
10901 				rack_timer_cancel(tp, rack,
10902 				    rack->r_ctl.rc_rcvtime, __LINE__);
10903 				tp->t_flags |= TF_DELACK;
10904 			} else {
10905 				tp->t_flags |= TF_ACKNOW;
10906 			}
10907 			tp->rcv_nxt++;
10908 		}
10909 		switch (tp->t_state) {
10910 			/*
10911 			 * In SYN_RECEIVED and ESTABLISHED STATES enter the
10912 			 * CLOSE_WAIT state.
10913 			 */
10914 		case TCPS_SYN_RECEIVED:
10915 			tp->t_starttime = ticks;
10916 			/* FALLTHROUGH */
10917 		case TCPS_ESTABLISHED:
10918 			rack_timer_cancel(tp, rack,
10919 			    rack->r_ctl.rc_rcvtime, __LINE__);
10920 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
10921 			break;
10922 
10923 			/*
10924 			 * If still in FIN_WAIT_1 STATE FIN has not been
10925 			 * acked so enter the CLOSING state.
10926 			 */
10927 		case TCPS_FIN_WAIT_1:
10928 			rack_timer_cancel(tp, rack,
10929 			    rack->r_ctl.rc_rcvtime, __LINE__);
10930 			tcp_state_change(tp, TCPS_CLOSING);
10931 			break;
10932 
10933 			/*
10934 			 * In FIN_WAIT_2 state enter the TIME_WAIT state,
10935 			 * starting the time-wait timer, turning off the
10936 			 * other standard timers.
10937 			 */
10938 		case TCPS_FIN_WAIT_2:
10939 			rack_timer_cancel(tp, rack,
10940 			    rack->r_ctl.rc_rcvtime, __LINE__);
10941 			tcp_twstart(tp);
10942 			return (1);
10943 		}
10944 	}
10945 	/*
10946 	 * Return any desired output.
10947 	 */
10948 	if ((tp->t_flags & TF_ACKNOW) ||
10949 	    (sbavail(&so->so_snd) > (tp->snd_max - tp->snd_una))) {
10950 		rack->r_wanted_output = 1;
10951 	}
10952 	INP_WLOCK_ASSERT(tp->t_inpcb);
10953 	return (0);
10954 }
10955 
10956 /*
10957  * Here nothing is really faster, its just that we
10958  * have broken out the fast-data path also just like
10959  * the fast-ack.
10960  */
10961 static int
10962 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so,
10963     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
10964     uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos)
10965 {
10966 	int32_t nsegs;
10967 	int32_t newsize = 0;	/* automatic sockbuf scaling */
10968 	struct tcp_rack *rack;
10969 #ifdef NETFLIX_SB_LIMITS
10970 	u_int mcnt, appended;
10971 #endif
10972 #ifdef TCPDEBUG
10973 	/*
10974 	 * The size of tcp_saveipgen must be the size of the max ip header,
10975 	 * now IPv6.
10976 	 */
10977 	u_char tcp_saveipgen[IP6_HDR_LEN];
10978 	struct tcphdr tcp_savetcp;
10979 	short ostate = 0;
10980 
10981 #endif
10982 	/*
10983 	 * If last ACK falls within this segment's sequence numbers, record
10984 	 * the timestamp. NOTE that the test is modified according to the
10985 	 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
10986 	 */
10987 	if (__predict_false(th->th_seq != tp->rcv_nxt)) {
10988 		return (0);
10989 	}
10990 	if (__predict_false(tp->snd_nxt != tp->snd_max)) {
10991 		return (0);
10992 	}
10993 	if (tiwin && tiwin != tp->snd_wnd) {
10994 		return (0);
10995 	}
10996 	if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) {
10997 		return (0);
10998 	}
10999 	if (__predict_false((to->to_flags & TOF_TS) &&
11000 	    (TSTMP_LT(to->to_tsval, tp->ts_recent)))) {
11001 		return (0);
11002 	}
11003 	if (__predict_false((th->th_ack != tp->snd_una))) {
11004 		return (0);
11005 	}
11006 	if (__predict_false(tlen > sbspace(&so->so_rcv))) {
11007 		return (0);
11008 	}
11009 	if ((to->to_flags & TOF_TS) != 0 &&
11010 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
11011 		tp->ts_recent_age = tcp_ts_getticks();
11012 		tp->ts_recent = to->to_tsval;
11013 	}
11014 	rack = (struct tcp_rack *)tp->t_fb_ptr;
11015 	/*
11016 	 * This is a pure, in-sequence data packet with nothing on the
11017 	 * reassembly queue and we have enough buffer space to take it.
11018 	 */
11019 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
11020 
11021 #ifdef NETFLIX_SB_LIMITS
11022 	if (so->so_rcv.sb_shlim) {
11023 		mcnt = m_memcnt(m);
11024 		appended = 0;
11025 		if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
11026 		    CFO_NOSLEEP, NULL) == false) {
11027 			counter_u64_add(tcp_sb_shlim_fails, 1);
11028 			m_freem(m);
11029 			return (1);
11030 		}
11031 	}
11032 #endif
11033 	/* Clean receiver SACK report if present */
11034 	if (tp->rcv_numsacks)
11035 		tcp_clean_sackreport(tp);
11036 	KMOD_TCPSTAT_INC(tcps_preddat);
11037 	tp->rcv_nxt += tlen;
11038 	if (tlen &&
11039 	    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
11040 	    (tp->t_fbyte_in == 0)) {
11041 		tp->t_fbyte_in = ticks;
11042 		if (tp->t_fbyte_in == 0)
11043 			tp->t_fbyte_in = 1;
11044 		if (tp->t_fbyte_out && tp->t_fbyte_in)
11045 			tp->t_flags2 |= TF2_FBYTES_COMPLETE;
11046 	}
11047 	/*
11048 	 * Pull snd_wl1 up to prevent seq wrap relative to th_seq.
11049 	 */
11050 	tp->snd_wl1 = th->th_seq;
11051 	/*
11052 	 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt.
11053 	 */
11054 	tp->rcv_up = tp->rcv_nxt;
11055 	KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs);
11056 	KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
11057 #ifdef TCPDEBUG
11058 	if (so->so_options & SO_DEBUG)
11059 		tcp_trace(TA_INPUT, ostate, tp,
11060 		    (void *)tcp_saveipgen, &tcp_savetcp, 0);
11061 #endif
11062 	newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
11063 
11064 	/* Add data to socket buffer. */
11065 	SOCKBUF_LOCK(&so->so_rcv);
11066 	if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
11067 		m_freem(m);
11068 	} else {
11069 		/*
11070 		 * Set new socket buffer size. Give up when limit is
11071 		 * reached.
11072 		 */
11073 		if (newsize)
11074 			if (!sbreserve_locked(&so->so_rcv,
11075 			    newsize, so, NULL))
11076 				so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
11077 		m_adj(m, drop_hdrlen);	/* delayed header drop */
11078 #ifdef NETFLIX_SB_LIMITS
11079 		appended =
11080 #endif
11081 			sbappendstream_locked(&so->so_rcv, m, 0);
11082 		ctf_calc_rwin(so, tp);
11083 	}
11084 	rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1);
11085 	/* NB: sorwakeup_locked() does an implicit unlock. */
11086 	sorwakeup_locked(so);
11087 #ifdef NETFLIX_SB_LIMITS
11088 	if (so->so_rcv.sb_shlim && mcnt != appended)
11089 		counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended);
11090 #endif
11091 	rack_handle_delayed_ack(tp, rack, tlen, 0);
11092 	if (tp->snd_una == tp->snd_max)
11093 		sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
11094 	return (1);
11095 }
11096 
11097 /*
11098  * This subfunction is used to try to highly optimize the
11099  * fast path. We again allow window updates that are
11100  * in sequence to remain in the fast-path. We also add
11101  * in the __predict's to attempt to help the compiler.
11102  * Note that if we return a 0, then we can *not* process
11103  * it and the caller should push the packet into the
11104  * slow-path.
11105  */
11106 static int
11107 rack_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
11108     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
11109     uint32_t tiwin, int32_t nxt_pkt, uint32_t cts)
11110 {
11111 	int32_t acked;
11112 	int32_t nsegs;
11113 #ifdef TCPDEBUG
11114 	/*
11115 	 * The size of tcp_saveipgen must be the size of the max ip header,
11116 	 * now IPv6.
11117 	 */
11118 	u_char tcp_saveipgen[IP6_HDR_LEN];
11119 	struct tcphdr tcp_savetcp;
11120 	short ostate = 0;
11121 #endif
11122 	int32_t under_pacing = 0;
11123 	struct tcp_rack *rack;
11124 
11125 	if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
11126 		/* Old ack, behind (or duplicate to) the last one rcv'd */
11127 		return (0);
11128 	}
11129 	if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) {
11130 		/* Above what we have sent? */
11131 		return (0);
11132 	}
11133 	if (__predict_false(tp->snd_nxt != tp->snd_max)) {
11134 		/* We are retransmitting */
11135 		return (0);
11136 	}
11137 	if (__predict_false(tiwin == 0)) {
11138 		/* zero window */
11139 		return (0);
11140 	}
11141 	if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) {
11142 		/* We need a SYN or a FIN, unlikely.. */
11143 		return (0);
11144 	}
11145 	if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) {
11146 		/* Timestamp is behind .. old ack with seq wrap? */
11147 		return (0);
11148 	}
11149 	if (__predict_false(IN_RECOVERY(tp->t_flags))) {
11150 		/* Still recovering */
11151 		return (0);
11152 	}
11153 	rack = (struct tcp_rack *)tp->t_fb_ptr;
11154 	if (rack->r_ctl.rc_sacked) {
11155 		/* We have sack holes on our scoreboard */
11156 		return (0);
11157 	}
11158 	/* Ok if we reach here, we can process a fast-ack */
11159 	if (rack->gp_ready &&
11160 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
11161 		under_pacing = 1;
11162 	}
11163 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
11164 	rack_log_ack(tp, to, th, 0, 0);
11165 	/* Did the window get updated? */
11166 	if (tiwin != tp->snd_wnd) {
11167 		tp->snd_wnd = tiwin;
11168 		rack_validate_fo_sendwin_up(tp, rack);
11169 		tp->snd_wl1 = th->th_seq;
11170 		if (tp->snd_wnd > tp->max_sndwnd)
11171 			tp->max_sndwnd = tp->snd_wnd;
11172 	}
11173 	/* Do we exit persists? */
11174 	if ((rack->rc_in_persist != 0) &&
11175 	    (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
11176 			       rack->r_ctl.rc_pace_min_segs))) {
11177 		rack_exit_persist(tp, rack, cts);
11178 	}
11179 	/* Do we enter persists? */
11180 	if ((rack->rc_in_persist == 0) &&
11181 	    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
11182 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
11183 	    (tp->snd_max == tp->snd_una) &&
11184 	    sbavail(&tp->t_inpcb->inp_socket->so_snd) &&
11185 	    (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) {
11186 		/*
11187 		 * Here the rwnd is less than
11188 		 * the pacing size, we are established,
11189 		 * nothing is outstanding, and there is
11190 		 * data to send. Enter persists.
11191 		 */
11192 		rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime);
11193 	}
11194 	/*
11195 	 * If last ACK falls within this segment's sequence numbers, record
11196 	 * the timestamp. NOTE that the test is modified according to the
11197 	 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
11198 	 */
11199 	if ((to->to_flags & TOF_TS) != 0 &&
11200 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
11201 		tp->ts_recent_age = tcp_ts_getticks();
11202 		tp->ts_recent = to->to_tsval;
11203 	}
11204 	/*
11205 	 * This is a pure ack for outstanding data.
11206 	 */
11207 	KMOD_TCPSTAT_INC(tcps_predack);
11208 
11209 	/*
11210 	 * "bad retransmit" recovery.
11211 	 */
11212 	if ((tp->t_flags & TF_PREVVALID) &&
11213 	    ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
11214 		tp->t_flags &= ~TF_PREVVALID;
11215 		if (tp->t_rxtshift == 1 &&
11216 		    (int)(ticks - tp->t_badrxtwin) < 0)
11217 			rack_cong_signal(tp, CC_RTO_ERR, th->th_ack);
11218 	}
11219 	/*
11220 	 * Recalculate the transmit timer / rtt.
11221 	 *
11222 	 * Some boxes send broken timestamp replies during the SYN+ACK
11223 	 * phase, ignore timestamps of 0 or we could calculate a huge RTT
11224 	 * and blow up the retransmit timer.
11225 	 */
11226 	acked = BYTES_THIS_ACK(tp, th);
11227 
11228 #ifdef TCP_HHOOK
11229 	/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
11230 	hhook_run_tcp_est_in(tp, th, to);
11231 #endif
11232 	KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
11233 	KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
11234 	if (acked) {
11235 		struct mbuf *mfree;
11236 
11237 		rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, 0);
11238 		SOCKBUF_LOCK(&so->so_snd);
11239 		mfree = sbcut_locked(&so->so_snd, acked);
11240 		tp->snd_una = th->th_ack;
11241 		/* Note we want to hold the sb lock through the sendmap adjust */
11242 		rack_adjust_sendmap(rack, &so->so_snd, tp->snd_una);
11243 		/* Wake up the socket if we have room to write more */
11244 		rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
11245 		sowwakeup_locked(so);
11246 		m_freem(mfree);
11247 		tp->t_rxtshift = 0;
11248 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
11249 			      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
11250 		rack->rc_tlp_in_progress = 0;
11251 		rack->r_ctl.rc_tlp_cnt_out = 0;
11252 		/*
11253 		 * If it is the RXT timer we want to
11254 		 * stop it, so we can restart a TLP.
11255 		 */
11256 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
11257 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
11258 #ifdef NETFLIX_HTTP_LOGGING
11259 		tcp_http_check_for_comp(rack->rc_tp, th->th_ack);
11260 #endif
11261 	}
11262 	/*
11263 	 * Let the congestion control algorithm update congestion control
11264 	 * related information. This typically means increasing the
11265 	 * congestion window.
11266 	 */
11267 	if (tp->snd_wnd < ctf_outstanding(tp)) {
11268 		/* The peer collapsed the window */
11269 		rack_collapsed_window(rack);
11270 	} else if (rack->rc_has_collapsed)
11271 		rack_un_collapse_window(rack);
11272 
11273 	/*
11274 	 * Pull snd_wl2 up to prevent seq wrap relative to th_ack.
11275 	 */
11276 	tp->snd_wl2 = th->th_ack;
11277 	tp->t_dupacks = 0;
11278 	m_freem(m);
11279 	/* ND6_HINT(tp);	 *//* Some progress has been made. */
11280 
11281 	/*
11282 	 * If all outstanding data are acked, stop retransmit timer,
11283 	 * otherwise restart timer using current (possibly backed-off)
11284 	 * value. If process is waiting for space, wakeup/selwakeup/signal.
11285 	 * If data are ready to send, let tcp_output decide between more
11286 	 * output or persist.
11287 	 */
11288 #ifdef TCPDEBUG
11289 	if (so->so_options & SO_DEBUG)
11290 		tcp_trace(TA_INPUT, ostate, tp,
11291 		    (void *)tcp_saveipgen,
11292 		    &tcp_savetcp, 0);
11293 #endif
11294 	if (under_pacing &&
11295 	    (rack->use_fixed_rate == 0) &&
11296 	    (rack->in_probe_rtt == 0) &&
11297 	    rack->rc_gp_dyn_mul &&
11298 	    rack->rc_always_pace) {
11299 		/* Check if we are dragging bottom */
11300 		rack_check_bottom_drag(tp, rack, so, acked);
11301 	}
11302 	if (tp->snd_una == tp->snd_max) {
11303 		tp->t_flags &= ~TF_PREVVALID;
11304 		rack->r_ctl.retran_during_recovery = 0;
11305 		rack->r_ctl.dsack_byte_cnt = 0;
11306 		rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
11307 		if (rack->r_ctl.rc_went_idle_time == 0)
11308 			rack->r_ctl.rc_went_idle_time = 1;
11309 		rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
11310 		if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0)
11311 			tp->t_acktime = 0;
11312 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
11313 	}
11314 	if (acked && rack->r_fast_output)
11315 		rack_gain_for_fastoutput(rack, tp, so, (uint32_t)acked);
11316 	if (sbavail(&so->so_snd)) {
11317 		rack->r_wanted_output = 1;
11318 	}
11319 	return (1);
11320 }
11321 
11322 /*
11323  * Return value of 1, the TCB is unlocked and most
11324  * likely gone, return value of 0, the TCP is still
11325  * locked.
11326  */
11327 static int
11328 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so,
11329     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
11330     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
11331 {
11332 	int32_t ret_val = 0;
11333 	int32_t todrop;
11334 	int32_t ourfinisacked = 0;
11335 	struct tcp_rack *rack;
11336 
11337 	ctf_calc_rwin(so, tp);
11338 	/*
11339 	 * If the state is SYN_SENT: if seg contains an ACK, but not for our
11340 	 * SYN, drop the input. if seg contains a RST, then drop the
11341 	 * connection. if seg does not contain SYN, then drop it. Otherwise
11342 	 * this is an acceptable SYN segment initialize tp->rcv_nxt and
11343 	 * tp->irs if seg contains ack then advance tp->snd_una if seg
11344 	 * contains an ECE and ECN support is enabled, the stream is ECN
11345 	 * capable. if SYN has been acked change to ESTABLISHED else
11346 	 * SYN_RCVD state arrange for segment to be acked (eventually)
11347 	 * continue processing rest of data/controls.
11348 	 */
11349 	if ((thflags & TH_ACK) &&
11350 	    (SEQ_LEQ(th->th_ack, tp->iss) ||
11351 	    SEQ_GT(th->th_ack, tp->snd_max))) {
11352 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
11353 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11354 		return (1);
11355 	}
11356 	if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) {
11357 		TCP_PROBE5(connect__refused, NULL, tp,
11358 		    mtod(m, const char *), tp, th);
11359 		tp = tcp_drop(tp, ECONNREFUSED);
11360 		ctf_do_drop(m, tp);
11361 		return (1);
11362 	}
11363 	if (thflags & TH_RST) {
11364 		ctf_do_drop(m, tp);
11365 		return (1);
11366 	}
11367 	if (!(thflags & TH_SYN)) {
11368 		ctf_do_drop(m, tp);
11369 		return (1);
11370 	}
11371 	tp->irs = th->th_seq;
11372 	tcp_rcvseqinit(tp);
11373 	rack = (struct tcp_rack *)tp->t_fb_ptr;
11374 	if (thflags & TH_ACK) {
11375 		int tfo_partial = 0;
11376 
11377 		KMOD_TCPSTAT_INC(tcps_connects);
11378 		soisconnected(so);
11379 #ifdef MAC
11380 		mac_socketpeer_set_from_mbuf(m, so);
11381 #endif
11382 		/* Do window scaling on this connection? */
11383 		if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
11384 		    (TF_RCVD_SCALE | TF_REQ_SCALE)) {
11385 			tp->rcv_scale = tp->request_r_scale;
11386 		}
11387 		tp->rcv_adv += min(tp->rcv_wnd,
11388 		    TCP_MAXWIN << tp->rcv_scale);
11389 		/*
11390 		 * If not all the data that was sent in the TFO SYN
11391 		 * has been acked, resend the remainder right away.
11392 		 */
11393 		if (IS_FASTOPEN(tp->t_flags) &&
11394 		    (tp->snd_una != tp->snd_max)) {
11395 			tp->snd_nxt = th->th_ack;
11396 			tfo_partial = 1;
11397 		}
11398 		/*
11399 		 * If there's data, delay ACK; if there's also a FIN ACKNOW
11400 		 * will be turned on later.
11401 		 */
11402 		if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial) {
11403 			rack_timer_cancel(tp, rack,
11404 					  rack->r_ctl.rc_rcvtime, __LINE__);
11405 			tp->t_flags |= TF_DELACK;
11406 		} else {
11407 			rack->r_wanted_output = 1;
11408 			tp->t_flags |= TF_ACKNOW;
11409 			rack->rc_dack_toggle = 0;
11410 		}
11411 
11412 		tcp_ecn_input_syn_sent(tp, thflags, iptos);
11413 
11414 		if (SEQ_GT(th->th_ack, tp->snd_una)) {
11415 			/*
11416 			 * We advance snd_una for the
11417 			 * fast open case. If th_ack is
11418 			 * acknowledging data beyond
11419 			 * snd_una we can't just call
11420 			 * ack-processing since the
11421 			 * data stream in our send-map
11422 			 * will start at snd_una + 1 (one
11423 			 * beyond the SYN). If its just
11424 			 * equal we don't need to do that
11425 			 * and there is no send_map.
11426 			 */
11427 			tp->snd_una++;
11428 		}
11429 		/*
11430 		 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions:
11431 		 * SYN_SENT  --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1
11432 		 */
11433 		tp->t_starttime = ticks;
11434 		if (tp->t_flags & TF_NEEDFIN) {
11435 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
11436 			tp->t_flags &= ~TF_NEEDFIN;
11437 			thflags &= ~TH_SYN;
11438 		} else {
11439 			tcp_state_change(tp, TCPS_ESTABLISHED);
11440 			TCP_PROBE5(connect__established, NULL, tp,
11441 			    mtod(m, const char *), tp, th);
11442 			rack_cc_conn_init(tp);
11443 		}
11444 	} else {
11445 		/*
11446 		 * Received initial SYN in SYN-SENT[*] state => simultaneous
11447 		 * open.  If segment contains CC option and there is a
11448 		 * cached CC, apply TAO test. If it succeeds, connection is *
11449 		 * half-synchronized. Otherwise, do 3-way handshake:
11450 		 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If
11451 		 * there was no CC option, clear cached CC value.
11452 		 */
11453 		tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
11454 		tcp_state_change(tp, TCPS_SYN_RECEIVED);
11455 	}
11456 	INP_WLOCK_ASSERT(tp->t_inpcb);
11457 	/*
11458 	 * Advance th->th_seq to correspond to first data byte. If data,
11459 	 * trim to stay within window, dropping FIN if necessary.
11460 	 */
11461 	th->th_seq++;
11462 	if (tlen > tp->rcv_wnd) {
11463 		todrop = tlen - tp->rcv_wnd;
11464 		m_adj(m, -todrop);
11465 		tlen = tp->rcv_wnd;
11466 		thflags &= ~TH_FIN;
11467 		KMOD_TCPSTAT_INC(tcps_rcvpackafterwin);
11468 		KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
11469 	}
11470 	tp->snd_wl1 = th->th_seq - 1;
11471 	tp->rcv_up = th->th_seq;
11472 	/*
11473 	 * Client side of transaction: already sent SYN and data. If the
11474 	 * remote host used T/TCP to validate the SYN, our data will be
11475 	 * ACK'd; if so, enter normal data segment processing in the middle
11476 	 * of step 5, ack processing. Otherwise, goto step 6.
11477 	 */
11478 	if (thflags & TH_ACK) {
11479 		/* For syn-sent we need to possibly update the rtt */
11480 		if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) {
11481 			uint32_t t, mcts;
11482 
11483 			mcts = tcp_ts_getticks();
11484 			t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC;
11485 			if (!tp->t_rttlow || tp->t_rttlow > t)
11486 				tp->t_rttlow = t;
11487 			rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 4);
11488 			tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2);
11489 			tcp_rack_xmit_timer_commit(rack, tp);
11490 		}
11491 		if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val))
11492 			return (ret_val);
11493 		/* We may have changed to FIN_WAIT_1 above */
11494 		if (tp->t_state == TCPS_FIN_WAIT_1) {
11495 			/*
11496 			 * In FIN_WAIT_1 STATE in addition to the processing
11497 			 * for the ESTABLISHED state if our FIN is now
11498 			 * acknowledged then enter FIN_WAIT_2.
11499 			 */
11500 			if (ourfinisacked) {
11501 				/*
11502 				 * If we can't receive any more data, then
11503 				 * closing user can proceed. Starting the
11504 				 * timer is contrary to the specification,
11505 				 * but if we don't get a FIN we'll hang
11506 				 * forever.
11507 				 *
11508 				 * XXXjl: we should release the tp also, and
11509 				 * use a compressed state.
11510 				 */
11511 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
11512 					soisdisconnected(so);
11513 					tcp_timer_activate(tp, TT_2MSL,
11514 					    (tcp_fast_finwait2_recycle ?
11515 					    tcp_finwait2_timeout :
11516 					    TP_MAXIDLE(tp)));
11517 				}
11518 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
11519 			}
11520 		}
11521 	}
11522 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11523 	   tiwin, thflags, nxt_pkt));
11524 }
11525 
11526 /*
11527  * Return value of 1, the TCB is unlocked and most
11528  * likely gone, return value of 0, the TCP is still
11529  * locked.
11530  */
11531 static int
11532 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so,
11533     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
11534     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
11535 {
11536 	struct tcp_rack *rack;
11537 	int32_t ret_val = 0;
11538 	int32_t ourfinisacked = 0;
11539 
11540 	ctf_calc_rwin(so, tp);
11541 	if ((thflags & TH_ACK) &&
11542 	    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
11543 	    SEQ_GT(th->th_ack, tp->snd_max))) {
11544 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
11545 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11546 		return (1);
11547 	}
11548 	rack = (struct tcp_rack *)tp->t_fb_ptr;
11549 	if (IS_FASTOPEN(tp->t_flags)) {
11550 		/*
11551 		 * When a TFO connection is in SYN_RECEIVED, the
11552 		 * only valid packets are the initial SYN, a
11553 		 * retransmit/copy of the initial SYN (possibly with
11554 		 * a subset of the original data), a valid ACK, a
11555 		 * FIN, or a RST.
11556 		 */
11557 		if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) {
11558 			tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
11559 			ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11560 			return (1);
11561 		} else if (thflags & TH_SYN) {
11562 			/* non-initial SYN is ignored */
11563 			if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) ||
11564 			    (rack->r_ctl.rc_hpts_flags & PACE_TMR_TLP) ||
11565 			    (rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) {
11566 				ctf_do_drop(m, NULL);
11567 				return (0);
11568 			}
11569 		} else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) {
11570 			ctf_do_drop(m, NULL);
11571 			return (0);
11572 		}
11573 	}
11574 
11575 	if ((thflags & TH_RST) ||
11576 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
11577 		return (__ctf_process_rst(m, th, so, tp,
11578 					  &rack->r_ctl.challenge_ack_ts,
11579 					  &rack->r_ctl.challenge_ack_cnt));
11580 	/*
11581 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
11582 	 * it's less than ts_recent, drop it.
11583 	 */
11584 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
11585 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
11586 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
11587 			return (ret_val);
11588 	}
11589 	/*
11590 	 * In the SYN-RECEIVED state, validate that the packet belongs to
11591 	 * this connection before trimming the data to fit the receive
11592 	 * window.  Check the sequence number versus IRS since we know the
11593 	 * sequence numbers haven't wrapped.  This is a partial fix for the
11594 	 * "LAND" DoS attack.
11595 	 */
11596 	if (SEQ_LT(th->th_seq, tp->irs)) {
11597 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
11598 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11599 		return (1);
11600 	}
11601 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
11602 			      &rack->r_ctl.challenge_ack_ts,
11603 			      &rack->r_ctl.challenge_ack_cnt)) {
11604 		return (ret_val);
11605 	}
11606 	/*
11607 	 * If last ACK falls within this segment's sequence numbers, record
11608 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
11609 	 * from the latest proposal of the tcplw@cray.com list (Braden
11610 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
11611 	 * with our earlier PAWS tests, so this check should be solely
11612 	 * predicated on the sequence space of this segment. 3) That we
11613 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
11614 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
11615 	 * SEG.Len, This modified check allows us to overcome RFC1323's
11616 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
11617 	 * p.869. In such cases, we can still calculate the RTT correctly
11618 	 * when RCV.NXT == Last.ACK.Sent.
11619 	 */
11620 	if ((to->to_flags & TOF_TS) != 0 &&
11621 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
11622 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
11623 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
11624 		tp->ts_recent_age = tcp_ts_getticks();
11625 		tp->ts_recent = to->to_tsval;
11626 	}
11627 	tp->snd_wnd = tiwin;
11628 	rack_validate_fo_sendwin_up(tp, rack);
11629 	/*
11630 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
11631 	 * is on (half-synchronized state), then queue data for later
11632 	 * processing; else drop segment and return.
11633 	 */
11634 	if ((thflags & TH_ACK) == 0) {
11635 		if (IS_FASTOPEN(tp->t_flags)) {
11636 			rack_cc_conn_init(tp);
11637 		}
11638 		return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11639 		    tiwin, thflags, nxt_pkt));
11640 	}
11641 	KMOD_TCPSTAT_INC(tcps_connects);
11642 	soisconnected(so);
11643 	/* Do window scaling? */
11644 	if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
11645 	    (TF_RCVD_SCALE | TF_REQ_SCALE)) {
11646 		tp->rcv_scale = tp->request_r_scale;
11647 	}
11648 	/*
11649 	 * Make transitions: SYN-RECEIVED  -> ESTABLISHED SYN-RECEIVED* ->
11650 	 * FIN-WAIT-1
11651 	 */
11652 	tp->t_starttime = ticks;
11653 	if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
11654 		tcp_fastopen_decrement_counter(tp->t_tfo_pending);
11655 		tp->t_tfo_pending = NULL;
11656 	}
11657 	if (tp->t_flags & TF_NEEDFIN) {
11658 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
11659 		tp->t_flags &= ~TF_NEEDFIN;
11660 	} else {
11661 		tcp_state_change(tp, TCPS_ESTABLISHED);
11662 		TCP_PROBE5(accept__established, NULL, tp,
11663 		    mtod(m, const char *), tp, th);
11664 		/*
11665 		 * TFO connections call cc_conn_init() during SYN
11666 		 * processing.  Calling it again here for such connections
11667 		 * is not harmless as it would undo the snd_cwnd reduction
11668 		 * that occurs when a TFO SYN|ACK is retransmitted.
11669 		 */
11670 		if (!IS_FASTOPEN(tp->t_flags))
11671 			rack_cc_conn_init(tp);
11672 	}
11673 	/*
11674 	 * Account for the ACK of our SYN prior to
11675 	 * regular ACK processing below, except for
11676 	 * simultaneous SYN, which is handled later.
11677 	 */
11678 	if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
11679 		tp->snd_una++;
11680 	/*
11681 	 * If segment contains data or ACK, will call tcp_reass() later; if
11682 	 * not, do so now to pass queued data to user.
11683 	 */
11684 	if (tlen == 0 && (thflags & TH_FIN) == 0) {
11685 		(void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
11686 		    (struct mbuf *)0);
11687 		if (tp->t_flags & TF_WAKESOR) {
11688 			tp->t_flags &= ~TF_WAKESOR;
11689 			/* NB: sorwakeup_locked() does an implicit unlock. */
11690 			sorwakeup_locked(so);
11691 		}
11692 	}
11693 	tp->snd_wl1 = th->th_seq - 1;
11694 	/* For syn-recv we need to possibly update the rtt */
11695 	if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) {
11696 		uint32_t t, mcts;
11697 
11698 		mcts = tcp_ts_getticks();
11699 		t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC;
11700 		if (!tp->t_rttlow || tp->t_rttlow > t)
11701 			tp->t_rttlow = t;
11702 		rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 5);
11703 		tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2);
11704 		tcp_rack_xmit_timer_commit(rack, tp);
11705 	}
11706 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
11707 		return (ret_val);
11708 	}
11709 	if (tp->t_state == TCPS_FIN_WAIT_1) {
11710 		/* We could have went to FIN_WAIT_1 (or EST) above */
11711 		/*
11712 		 * In FIN_WAIT_1 STATE in addition to the processing for the
11713 		 * ESTABLISHED state if our FIN is now acknowledged then
11714 		 * enter FIN_WAIT_2.
11715 		 */
11716 		if (ourfinisacked) {
11717 			/*
11718 			 * If we can't receive any more data, then closing
11719 			 * user can proceed. Starting the timer is contrary
11720 			 * to the specification, but if we don't get a FIN
11721 			 * we'll hang forever.
11722 			 *
11723 			 * XXXjl: we should release the tp also, and use a
11724 			 * compressed state.
11725 			 */
11726 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
11727 				soisdisconnected(so);
11728 				tcp_timer_activate(tp, TT_2MSL,
11729 				    (tcp_fast_finwait2_recycle ?
11730 				    tcp_finwait2_timeout :
11731 				    TP_MAXIDLE(tp)));
11732 			}
11733 			tcp_state_change(tp, TCPS_FIN_WAIT_2);
11734 		}
11735 	}
11736 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11737 	    tiwin, thflags, nxt_pkt));
11738 }
11739 
11740 /*
11741  * Return value of 1, the TCB is unlocked and most
11742  * likely gone, return value of 0, the TCP is still
11743  * locked.
11744  */
11745 static int
11746 rack_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so,
11747     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
11748     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
11749 {
11750 	int32_t ret_val = 0;
11751 	struct tcp_rack *rack;
11752 
11753 	/*
11754 	 * Header prediction: check for the two common cases of a
11755 	 * uni-directional data xfer.  If the packet has no control flags,
11756 	 * is in-sequence, the window didn't change and we're not
11757 	 * retransmitting, it's a candidate.  If the length is zero and the
11758 	 * ack moved forward, we're the sender side of the xfer.  Just free
11759 	 * the data acked & wake any higher level process that was blocked
11760 	 * waiting for space.  If the length is non-zero and the ack didn't
11761 	 * move, we're the receiver side.  If we're getting packets in-order
11762 	 * (the reassembly queue is empty), add the data toc The socket
11763 	 * buffer and note that we need a delayed ack. Make sure that the
11764 	 * hidden state-flags are also off. Since we check for
11765 	 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN.
11766 	 */
11767 	rack = (struct tcp_rack *)tp->t_fb_ptr;
11768 	if (__predict_true(((to->to_flags & TOF_SACK) == 0)) &&
11769 	    __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) == TH_ACK) &&
11770 	    __predict_true(SEGQ_EMPTY(tp)) &&
11771 	    __predict_true(th->th_seq == tp->rcv_nxt)) {
11772 		if (tlen == 0) {
11773 			if (rack_fastack(m, th, so, tp, to, drop_hdrlen, tlen,
11774 			    tiwin, nxt_pkt, rack->r_ctl.rc_rcvtime)) {
11775 				return (0);
11776 			}
11777 		} else {
11778 			if (rack_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen,
11779 			    tiwin, nxt_pkt, iptos)) {
11780 				return (0);
11781 			}
11782 		}
11783 	}
11784 	ctf_calc_rwin(so, tp);
11785 
11786 	if ((thflags & TH_RST) ||
11787 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
11788 		return (__ctf_process_rst(m, th, so, tp,
11789 					  &rack->r_ctl.challenge_ack_ts,
11790 					  &rack->r_ctl.challenge_ack_cnt));
11791 
11792 	/*
11793 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
11794 	 * synchronized state.
11795 	 */
11796 	if (thflags & TH_SYN) {
11797 		ctf_challenge_ack(m, th, tp, &ret_val);
11798 		return (ret_val);
11799 	}
11800 	/*
11801 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
11802 	 * it's less than ts_recent, drop it.
11803 	 */
11804 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
11805 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
11806 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
11807 			return (ret_val);
11808 	}
11809 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
11810 			      &rack->r_ctl.challenge_ack_ts,
11811 			      &rack->r_ctl.challenge_ack_cnt)) {
11812 		return (ret_val);
11813 	}
11814 	/*
11815 	 * If last ACK falls within this segment's sequence numbers, record
11816 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
11817 	 * from the latest proposal of the tcplw@cray.com list (Braden
11818 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
11819 	 * with our earlier PAWS tests, so this check should be solely
11820 	 * predicated on the sequence space of this segment. 3) That we
11821 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
11822 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
11823 	 * SEG.Len, This modified check allows us to overcome RFC1323's
11824 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
11825 	 * p.869. In such cases, we can still calculate the RTT correctly
11826 	 * when RCV.NXT == Last.ACK.Sent.
11827 	 */
11828 	if ((to->to_flags & TOF_TS) != 0 &&
11829 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
11830 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
11831 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
11832 		tp->ts_recent_age = tcp_ts_getticks();
11833 		tp->ts_recent = to->to_tsval;
11834 	}
11835 	/*
11836 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
11837 	 * is on (half-synchronized state), then queue data for later
11838 	 * processing; else drop segment and return.
11839 	 */
11840 	if ((thflags & TH_ACK) == 0) {
11841 		if (tp->t_flags & TF_NEEDSYN) {
11842 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11843 			    tiwin, thflags, nxt_pkt));
11844 
11845 		} else if (tp->t_flags & TF_ACKNOW) {
11846 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
11847 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
11848 			return (ret_val);
11849 		} else {
11850 			ctf_do_drop(m, NULL);
11851 			return (0);
11852 		}
11853 	}
11854 	/*
11855 	 * Ack processing.
11856 	 */
11857 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
11858 		return (ret_val);
11859 	}
11860 	if (sbavail(&so->so_snd)) {
11861 		if (ctf_progress_timeout_check(tp, true)) {
11862 			rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
11863 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11864 			return (1);
11865 		}
11866 	}
11867 	/* State changes only happen in rack_process_data() */
11868 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11869 	    tiwin, thflags, nxt_pkt));
11870 }
11871 
11872 /*
11873  * Return value of 1, the TCB is unlocked and most
11874  * likely gone, return value of 0, the TCP is still
11875  * locked.
11876  */
11877 static int
11878 rack_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so,
11879     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
11880     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
11881 {
11882 	int32_t ret_val = 0;
11883 	struct tcp_rack *rack;
11884 
11885 	rack = (struct tcp_rack *)tp->t_fb_ptr;
11886 	ctf_calc_rwin(so, tp);
11887 	if ((thflags & TH_RST) ||
11888 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
11889 		return (__ctf_process_rst(m, th, so, tp,
11890 					  &rack->r_ctl.challenge_ack_ts,
11891 					  &rack->r_ctl.challenge_ack_cnt));
11892 	/*
11893 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
11894 	 * synchronized state.
11895 	 */
11896 	if (thflags & TH_SYN) {
11897 		ctf_challenge_ack(m, th, tp, &ret_val);
11898 		return (ret_val);
11899 	}
11900 	/*
11901 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
11902 	 * it's less than ts_recent, drop it.
11903 	 */
11904 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
11905 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
11906 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
11907 			return (ret_val);
11908 	}
11909 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
11910 			      &rack->r_ctl.challenge_ack_ts,
11911 			      &rack->r_ctl.challenge_ack_cnt)) {
11912 		return (ret_val);
11913 	}
11914 	/*
11915 	 * If last ACK falls within this segment's sequence numbers, record
11916 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
11917 	 * from the latest proposal of the tcplw@cray.com list (Braden
11918 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
11919 	 * with our earlier PAWS tests, so this check should be solely
11920 	 * predicated on the sequence space of this segment. 3) That we
11921 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
11922 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
11923 	 * SEG.Len, This modified check allows us to overcome RFC1323's
11924 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
11925 	 * p.869. In such cases, we can still calculate the RTT correctly
11926 	 * when RCV.NXT == Last.ACK.Sent.
11927 	 */
11928 	if ((to->to_flags & TOF_TS) != 0 &&
11929 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
11930 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
11931 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
11932 		tp->ts_recent_age = tcp_ts_getticks();
11933 		tp->ts_recent = to->to_tsval;
11934 	}
11935 	/*
11936 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
11937 	 * is on (half-synchronized state), then queue data for later
11938 	 * processing; else drop segment and return.
11939 	 */
11940 	if ((thflags & TH_ACK) == 0) {
11941 		if (tp->t_flags & TF_NEEDSYN) {
11942 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11943 			    tiwin, thflags, nxt_pkt));
11944 
11945 		} else if (tp->t_flags & TF_ACKNOW) {
11946 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
11947 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
11948 			return (ret_val);
11949 		} else {
11950 			ctf_do_drop(m, NULL);
11951 			return (0);
11952 		}
11953 	}
11954 	/*
11955 	 * Ack processing.
11956 	 */
11957 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
11958 		return (ret_val);
11959 	}
11960 	if (sbavail(&so->so_snd)) {
11961 		if (ctf_progress_timeout_check(tp, true)) {
11962 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
11963 						tp, tick, PROGRESS_DROP, __LINE__);
11964 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11965 			return (1);
11966 		}
11967 	}
11968 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11969 	    tiwin, thflags, nxt_pkt));
11970 }
11971 
11972 static int
11973 rack_check_data_after_close(struct mbuf *m,
11974     struct tcpcb *tp, int32_t *tlen, struct tcphdr *th, struct socket *so)
11975 {
11976 	struct tcp_rack *rack;
11977 
11978 	rack = (struct tcp_rack *)tp->t_fb_ptr;
11979 	if (rack->rc_allow_data_af_clo == 0) {
11980 	close_now:
11981 		tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
11982 		/* tcp_close will kill the inp pre-log the Reset */
11983 		tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
11984 		tp = tcp_close(tp);
11985 		KMOD_TCPSTAT_INC(tcps_rcvafterclose);
11986 		ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen));
11987 		return (1);
11988 	}
11989 	if (sbavail(&so->so_snd) == 0)
11990 		goto close_now;
11991 	/* Ok we allow data that is ignored and a followup reset */
11992 	tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
11993 	tp->rcv_nxt = th->th_seq + *tlen;
11994 	tp->t_flags2 |= TF2_DROP_AF_DATA;
11995 	rack->r_wanted_output = 1;
11996 	*tlen = 0;
11997 	return (0);
11998 }
11999 
12000 /*
12001  * Return value of 1, the TCB is unlocked and most
12002  * likely gone, return value of 0, the TCP is still
12003  * locked.
12004  */
12005 static int
12006 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so,
12007     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12008     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
12009 {
12010 	int32_t ret_val = 0;
12011 	int32_t ourfinisacked = 0;
12012 	struct tcp_rack *rack;
12013 
12014 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12015 	ctf_calc_rwin(so, tp);
12016 
12017 	if ((thflags & TH_RST) ||
12018 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
12019 		return (__ctf_process_rst(m, th, so, tp,
12020 					  &rack->r_ctl.challenge_ack_ts,
12021 					  &rack->r_ctl.challenge_ack_cnt));
12022 	/*
12023 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
12024 	 * synchronized state.
12025 	 */
12026 	if (thflags & TH_SYN) {
12027 		ctf_challenge_ack(m, th, tp, &ret_val);
12028 		return (ret_val);
12029 	}
12030 	/*
12031 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
12032 	 * it's less than ts_recent, drop it.
12033 	 */
12034 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
12035 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
12036 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
12037 			return (ret_val);
12038 	}
12039 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
12040 			      &rack->r_ctl.challenge_ack_ts,
12041 			      &rack->r_ctl.challenge_ack_cnt)) {
12042 		return (ret_val);
12043 	}
12044 	/*
12045 	 * If new data are received on a connection after the user processes
12046 	 * are gone, then RST the other end.
12047 	 */
12048 	if ((so->so_state & SS_NOFDREF) && tlen) {
12049 		if (rack_check_data_after_close(m, tp, &tlen, th, so))
12050 			return (1);
12051 	}
12052 	/*
12053 	 * If last ACK falls within this segment's sequence numbers, record
12054 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
12055 	 * from the latest proposal of the tcplw@cray.com list (Braden
12056 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
12057 	 * with our earlier PAWS tests, so this check should be solely
12058 	 * predicated on the sequence space of this segment. 3) That we
12059 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
12060 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
12061 	 * SEG.Len, This modified check allows us to overcome RFC1323's
12062 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
12063 	 * p.869. In such cases, we can still calculate the RTT correctly
12064 	 * when RCV.NXT == Last.ACK.Sent.
12065 	 */
12066 	if ((to->to_flags & TOF_TS) != 0 &&
12067 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
12068 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
12069 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
12070 		tp->ts_recent_age = tcp_ts_getticks();
12071 		tp->ts_recent = to->to_tsval;
12072 	}
12073 	/*
12074 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
12075 	 * is on (half-synchronized state), then queue data for later
12076 	 * processing; else drop segment and return.
12077 	 */
12078 	if ((thflags & TH_ACK) == 0) {
12079 		if (tp->t_flags & TF_NEEDSYN) {
12080 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12081 			    tiwin, thflags, nxt_pkt));
12082 		} else if (tp->t_flags & TF_ACKNOW) {
12083 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
12084 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
12085 			return (ret_val);
12086 		} else {
12087 			ctf_do_drop(m, NULL);
12088 			return (0);
12089 		}
12090 	}
12091 	/*
12092 	 * Ack processing.
12093 	 */
12094 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
12095 		return (ret_val);
12096 	}
12097 	if (ourfinisacked) {
12098 		/*
12099 		 * If we can't receive any more data, then closing user can
12100 		 * proceed. Starting the timer is contrary to the
12101 		 * specification, but if we don't get a FIN we'll hang
12102 		 * forever.
12103 		 *
12104 		 * XXXjl: we should release the tp also, and use a
12105 		 * compressed state.
12106 		 */
12107 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
12108 			soisdisconnected(so);
12109 			tcp_timer_activate(tp, TT_2MSL,
12110 			    (tcp_fast_finwait2_recycle ?
12111 			    tcp_finwait2_timeout :
12112 			    TP_MAXIDLE(tp)));
12113 		}
12114 		tcp_state_change(tp, TCPS_FIN_WAIT_2);
12115 	}
12116 	if (sbavail(&so->so_snd)) {
12117 		if (ctf_progress_timeout_check(tp, true)) {
12118 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
12119 						tp, tick, PROGRESS_DROP, __LINE__);
12120 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
12121 			return (1);
12122 		}
12123 	}
12124 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12125 	    tiwin, thflags, nxt_pkt));
12126 }
12127 
12128 /*
12129  * Return value of 1, the TCB is unlocked and most
12130  * likely gone, return value of 0, the TCP is still
12131  * locked.
12132  */
12133 static int
12134 rack_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so,
12135     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12136     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
12137 {
12138 	int32_t ret_val = 0;
12139 	int32_t ourfinisacked = 0;
12140 	struct tcp_rack *rack;
12141 
12142 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12143 	ctf_calc_rwin(so, tp);
12144 
12145 	if ((thflags & TH_RST) ||
12146 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
12147 		return (__ctf_process_rst(m, th, so, tp,
12148 					  &rack->r_ctl.challenge_ack_ts,
12149 					  &rack->r_ctl.challenge_ack_cnt));
12150 	/*
12151 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
12152 	 * synchronized state.
12153 	 */
12154 	if (thflags & TH_SYN) {
12155 		ctf_challenge_ack(m, th, tp, &ret_val);
12156 		return (ret_val);
12157 	}
12158 	/*
12159 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
12160 	 * it's less than ts_recent, drop it.
12161 	 */
12162 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
12163 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
12164 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
12165 			return (ret_val);
12166 	}
12167 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
12168 			      &rack->r_ctl.challenge_ack_ts,
12169 			      &rack->r_ctl.challenge_ack_cnt)) {
12170 		return (ret_val);
12171 	}
12172 	/*
12173 	 * If new data are received on a connection after the user processes
12174 	 * are gone, then RST the other end.
12175 	 */
12176 	if ((so->so_state & SS_NOFDREF) && tlen) {
12177 		if (rack_check_data_after_close(m, tp, &tlen, th, so))
12178 			return (1);
12179 	}
12180 	/*
12181 	 * If last ACK falls within this segment's sequence numbers, record
12182 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
12183 	 * from the latest proposal of the tcplw@cray.com list (Braden
12184 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
12185 	 * with our earlier PAWS tests, so this check should be solely
12186 	 * predicated on the sequence space of this segment. 3) That we
12187 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
12188 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
12189 	 * SEG.Len, This modified check allows us to overcome RFC1323's
12190 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
12191 	 * p.869. In such cases, we can still calculate the RTT correctly
12192 	 * when RCV.NXT == Last.ACK.Sent.
12193 	 */
12194 	if ((to->to_flags & TOF_TS) != 0 &&
12195 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
12196 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
12197 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
12198 		tp->ts_recent_age = tcp_ts_getticks();
12199 		tp->ts_recent = to->to_tsval;
12200 	}
12201 	/*
12202 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
12203 	 * is on (half-synchronized state), then queue data for later
12204 	 * processing; else drop segment and return.
12205 	 */
12206 	if ((thflags & TH_ACK) == 0) {
12207 		if (tp->t_flags & TF_NEEDSYN) {
12208 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12209 			    tiwin, thflags, nxt_pkt));
12210 		} else if (tp->t_flags & TF_ACKNOW) {
12211 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
12212 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
12213 			return (ret_val);
12214 		} else {
12215 			ctf_do_drop(m, NULL);
12216 			return (0);
12217 		}
12218 	}
12219 	/*
12220 	 * Ack processing.
12221 	 */
12222 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
12223 		return (ret_val);
12224 	}
12225 	if (ourfinisacked) {
12226 		tcp_twstart(tp);
12227 		m_freem(m);
12228 		return (1);
12229 	}
12230 	if (sbavail(&so->so_snd)) {
12231 		if (ctf_progress_timeout_check(tp, true)) {
12232 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
12233 						tp, tick, PROGRESS_DROP, __LINE__);
12234 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
12235 			return (1);
12236 		}
12237 	}
12238 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12239 	    tiwin, thflags, nxt_pkt));
12240 }
12241 
12242 /*
12243  * Return value of 1, the TCB is unlocked and most
12244  * likely gone, return value of 0, the TCP is still
12245  * locked.
12246  */
12247 static int
12248 rack_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
12249     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12250     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
12251 {
12252 	int32_t ret_val = 0;
12253 	int32_t ourfinisacked = 0;
12254 	struct tcp_rack *rack;
12255 
12256 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12257 	ctf_calc_rwin(so, tp);
12258 
12259 	if ((thflags & TH_RST) ||
12260 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
12261 		return (__ctf_process_rst(m, th, so, tp,
12262 					  &rack->r_ctl.challenge_ack_ts,
12263 					  &rack->r_ctl.challenge_ack_cnt));
12264 	/*
12265 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
12266 	 * synchronized state.
12267 	 */
12268 	if (thflags & TH_SYN) {
12269 		ctf_challenge_ack(m, th, tp, &ret_val);
12270 		return (ret_val);
12271 	}
12272 	/*
12273 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
12274 	 * it's less than ts_recent, drop it.
12275 	 */
12276 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
12277 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
12278 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
12279 			return (ret_val);
12280 	}
12281 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
12282 			      &rack->r_ctl.challenge_ack_ts,
12283 			      &rack->r_ctl.challenge_ack_cnt)) {
12284 		return (ret_val);
12285 	}
12286 	/*
12287 	 * If new data are received on a connection after the user processes
12288 	 * are gone, then RST the other end.
12289 	 */
12290 	if ((so->so_state & SS_NOFDREF) && tlen) {
12291 		if (rack_check_data_after_close(m, tp, &tlen, th, so))
12292 			return (1);
12293 	}
12294 	/*
12295 	 * If last ACK falls within this segment's sequence numbers, record
12296 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
12297 	 * from the latest proposal of the tcplw@cray.com list (Braden
12298 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
12299 	 * with our earlier PAWS tests, so this check should be solely
12300 	 * predicated on the sequence space of this segment. 3) That we
12301 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
12302 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
12303 	 * SEG.Len, This modified check allows us to overcome RFC1323's
12304 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
12305 	 * p.869. In such cases, we can still calculate the RTT correctly
12306 	 * when RCV.NXT == Last.ACK.Sent.
12307 	 */
12308 	if ((to->to_flags & TOF_TS) != 0 &&
12309 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
12310 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
12311 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
12312 		tp->ts_recent_age = tcp_ts_getticks();
12313 		tp->ts_recent = to->to_tsval;
12314 	}
12315 	/*
12316 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
12317 	 * is on (half-synchronized state), then queue data for later
12318 	 * processing; else drop segment and return.
12319 	 */
12320 	if ((thflags & TH_ACK) == 0) {
12321 		if (tp->t_flags & TF_NEEDSYN) {
12322 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12323 			    tiwin, thflags, nxt_pkt));
12324 		} else if (tp->t_flags & TF_ACKNOW) {
12325 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
12326 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
12327 			return (ret_val);
12328 		} else {
12329 			ctf_do_drop(m, NULL);
12330 			return (0);
12331 		}
12332 	}
12333 	/*
12334 	 * case TCPS_LAST_ACK: Ack processing.
12335 	 */
12336 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
12337 		return (ret_val);
12338 	}
12339 	if (ourfinisacked) {
12340 		tp = tcp_close(tp);
12341 		ctf_do_drop(m, tp);
12342 		return (1);
12343 	}
12344 	if (sbavail(&so->so_snd)) {
12345 		if (ctf_progress_timeout_check(tp, true)) {
12346 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
12347 						tp, tick, PROGRESS_DROP, __LINE__);
12348 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
12349 			return (1);
12350 		}
12351 	}
12352 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12353 	    tiwin, thflags, nxt_pkt));
12354 }
12355 
12356 /*
12357  * Return value of 1, the TCB is unlocked and most
12358  * likely gone, return value of 0, the TCP is still
12359  * locked.
12360  */
12361 static int
12362 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so,
12363     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12364     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
12365 {
12366 	int32_t ret_val = 0;
12367 	int32_t ourfinisacked = 0;
12368 	struct tcp_rack *rack;
12369 
12370 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12371 	ctf_calc_rwin(so, tp);
12372 
12373 	/* Reset receive buffer auto scaling when not in bulk receive mode. */
12374 	if ((thflags & TH_RST) ||
12375 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
12376 		return (__ctf_process_rst(m, th, so, tp,
12377 					  &rack->r_ctl.challenge_ack_ts,
12378 					  &rack->r_ctl.challenge_ack_cnt));
12379 	/*
12380 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
12381 	 * synchronized state.
12382 	 */
12383 	if (thflags & TH_SYN) {
12384 		ctf_challenge_ack(m, th, tp, &ret_val);
12385 		return (ret_val);
12386 	}
12387 	/*
12388 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
12389 	 * it's less than ts_recent, drop it.
12390 	 */
12391 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
12392 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
12393 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
12394 			return (ret_val);
12395 	}
12396 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
12397 			      &rack->r_ctl.challenge_ack_ts,
12398 			      &rack->r_ctl.challenge_ack_cnt)) {
12399 		return (ret_val);
12400 	}
12401 	/*
12402 	 * If new data are received on a connection after the user processes
12403 	 * are gone, then RST the other end.
12404 	 */
12405 	if ((so->so_state & SS_NOFDREF) &&
12406 	    tlen) {
12407 		if (rack_check_data_after_close(m, tp, &tlen, th, so))
12408 			return (1);
12409 	}
12410 	/*
12411 	 * If last ACK falls within this segment's sequence numbers, record
12412 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
12413 	 * from the latest proposal of the tcplw@cray.com list (Braden
12414 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
12415 	 * with our earlier PAWS tests, so this check should be solely
12416 	 * predicated on the sequence space of this segment. 3) That we
12417 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
12418 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
12419 	 * SEG.Len, This modified check allows us to overcome RFC1323's
12420 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
12421 	 * p.869. In such cases, we can still calculate the RTT correctly
12422 	 * when RCV.NXT == Last.ACK.Sent.
12423 	 */
12424 	if ((to->to_flags & TOF_TS) != 0 &&
12425 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
12426 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
12427 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
12428 		tp->ts_recent_age = tcp_ts_getticks();
12429 		tp->ts_recent = to->to_tsval;
12430 	}
12431 	/*
12432 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
12433 	 * is on (half-synchronized state), then queue data for later
12434 	 * processing; else drop segment and return.
12435 	 */
12436 	if ((thflags & TH_ACK) == 0) {
12437 		if (tp->t_flags & TF_NEEDSYN) {
12438 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12439 			    tiwin, thflags, nxt_pkt));
12440 		} else if (tp->t_flags & TF_ACKNOW) {
12441 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
12442 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
12443 			return (ret_val);
12444 		} else {
12445 			ctf_do_drop(m, NULL);
12446 			return (0);
12447 		}
12448 	}
12449 	/*
12450 	 * Ack processing.
12451 	 */
12452 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
12453 		return (ret_val);
12454 	}
12455 	if (sbavail(&so->so_snd)) {
12456 		if (ctf_progress_timeout_check(tp, true)) {
12457 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
12458 						tp, tick, PROGRESS_DROP, __LINE__);
12459 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
12460 			return (1);
12461 		}
12462 	}
12463 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12464 	    tiwin, thflags, nxt_pkt));
12465 }
12466 
12467 static void inline
12468 rack_clear_rate_sample(struct tcp_rack *rack)
12469 {
12470 	rack->r_ctl.rack_rs.rs_flags = RACK_RTT_EMPTY;
12471 	rack->r_ctl.rack_rs.rs_rtt_cnt = 0;
12472 	rack->r_ctl.rack_rs.rs_rtt_tot = 0;
12473 }
12474 
12475 static void
12476 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override)
12477 {
12478 	uint64_t bw_est, rate_wanted;
12479 	int chged = 0;
12480 	uint32_t user_max, orig_min, orig_max;
12481 
12482 	orig_min = rack->r_ctl.rc_pace_min_segs;
12483 	orig_max = rack->r_ctl.rc_pace_max_segs;
12484 	user_max = ctf_fixed_maxseg(tp) * rack->rc_user_set_max_segs;
12485 	if (ctf_fixed_maxseg(tp) != rack->r_ctl.rc_pace_min_segs)
12486 		chged = 1;
12487 	rack->r_ctl.rc_pace_min_segs = ctf_fixed_maxseg(tp);
12488 	if (rack->use_fixed_rate || rack->rc_force_max_seg) {
12489 		if (user_max != rack->r_ctl.rc_pace_max_segs)
12490 			chged = 1;
12491 	}
12492 	if (rack->rc_force_max_seg) {
12493 		rack->r_ctl.rc_pace_max_segs = user_max;
12494 	} else if (rack->use_fixed_rate) {
12495 		bw_est = rack_get_bw(rack);
12496 		if ((rack->r_ctl.crte == NULL) ||
12497 		    (bw_est != rack->r_ctl.crte->rate)) {
12498 			rack->r_ctl.rc_pace_max_segs = user_max;
12499 		} else {
12500 			/* We are pacing right at the hardware rate */
12501 			uint32_t segsiz;
12502 
12503 			segsiz = min(ctf_fixed_maxseg(tp),
12504 				     rack->r_ctl.rc_pace_min_segs);
12505 			rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size(
12506 				                           tp, bw_est, segsiz, 0,
12507 							   rack->r_ctl.crte, NULL);
12508 		}
12509 	} else if (rack->rc_always_pace) {
12510 		if (rack->r_ctl.gp_bw ||
12511 #ifdef NETFLIX_PEAKRATE
12512 		    rack->rc_tp->t_maxpeakrate ||
12513 #endif
12514 		    rack->r_ctl.init_rate) {
12515 			/* We have a rate of some sort set */
12516 			uint32_t  orig;
12517 
12518 			bw_est = rack_get_bw(rack);
12519 			orig = rack->r_ctl.rc_pace_max_segs;
12520 			if (fill_override)
12521 				rate_wanted = *fill_override;
12522 			else
12523 				rate_wanted = rack_get_output_bw(rack, bw_est, NULL, NULL);
12524 			if (rate_wanted) {
12525 				/* We have something */
12526 				rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack,
12527 										   rate_wanted,
12528 										   ctf_fixed_maxseg(rack->rc_tp));
12529 			} else
12530 				rack->r_ctl.rc_pace_max_segs = rack->r_ctl.rc_pace_min_segs;
12531 			if (orig != rack->r_ctl.rc_pace_max_segs)
12532 				chged = 1;
12533 		} else if ((rack->r_ctl.gp_bw == 0) &&
12534 			   (rack->r_ctl.rc_pace_max_segs == 0)) {
12535 			/*
12536 			 * If we have nothing limit us to bursting
12537 			 * out IW sized pieces.
12538 			 */
12539 			chged = 1;
12540 			rack->r_ctl.rc_pace_max_segs = rc_init_window(rack);
12541 		}
12542 	}
12543 	if (rack->r_ctl.rc_pace_max_segs > PACE_MAX_IP_BYTES) {
12544 		chged = 1;
12545 		rack->r_ctl.rc_pace_max_segs = PACE_MAX_IP_BYTES;
12546 	}
12547 	if (chged)
12548 		rack_log_type_pacing_sizes(tp, rack, orig_min, orig_max, line, 2);
12549 }
12550 
12551 
12552 static void
12553 rack_init_fsb_block(struct tcpcb *tp, struct tcp_rack *rack)
12554 {
12555 #ifdef INET6
12556 	struct ip6_hdr *ip6 = NULL;
12557 #endif
12558 #ifdef INET
12559 	struct ip *ip = NULL;
12560 #endif
12561 	struct udphdr *udp = NULL;
12562 
12563 	/* Ok lets fill in the fast block, it can only be used with no IP options! */
12564 #ifdef INET6
12565 	if (rack->r_is_v6) {
12566 		rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
12567 		ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
12568 		if (tp->t_port) {
12569 			rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr);
12570 			udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
12571 			udp->uh_sport = htons(V_tcp_udp_tunneling_port);
12572 			udp->uh_dport = tp->t_port;
12573 			rack->r_ctl.fsb.udp = udp;
12574 			rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1);
12575 		} else
12576 		{
12577 			rack->r_ctl.fsb.th = (struct tcphdr *)(ip6 + 1);
12578 			rack->r_ctl.fsb.udp = NULL;
12579 		}
12580 		tcpip_fillheaders(rack->rc_inp,
12581 				  tp->t_port,
12582 				  ip6, rack->r_ctl.fsb.th);
12583 	} else
12584 #endif				/* INET6 */
12585 	{
12586 		rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr);
12587 		ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
12588 		if (tp->t_port) {
12589 			rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr);
12590 			udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
12591 			udp->uh_sport = htons(V_tcp_udp_tunneling_port);
12592 			udp->uh_dport = tp->t_port;
12593 			rack->r_ctl.fsb.udp = udp;
12594 			rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1);
12595 		} else
12596 		{
12597 			rack->r_ctl.fsb.udp = NULL;
12598 			rack->r_ctl.fsb.th = (struct tcphdr *)(ip + 1);
12599 		}
12600 		tcpip_fillheaders(rack->rc_inp,
12601 				  tp->t_port,
12602 				  ip, rack->r_ctl.fsb.th);
12603 	}
12604 	rack->r_fsb_inited = 1;
12605 }
12606 
12607 static int
12608 rack_init_fsb(struct tcpcb *tp, struct tcp_rack *rack)
12609 {
12610 	/*
12611 	 * Allocate the larger of spaces V6 if available else just
12612 	 * V4 and include udphdr (overbook)
12613 	 */
12614 #ifdef INET6
12615 	rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr) + sizeof(struct udphdr);
12616 #else
12617 	rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr) + sizeof(struct udphdr);
12618 #endif
12619 	rack->r_ctl.fsb.tcp_ip_hdr = malloc(rack->r_ctl.fsb.tcp_ip_hdr_len,
12620 					    M_TCPFSB, M_NOWAIT|M_ZERO);
12621 	if (rack->r_ctl.fsb.tcp_ip_hdr == NULL) {
12622 		return (ENOMEM);
12623 	}
12624 	rack->r_fsb_inited = 0;
12625 	return (0);
12626 }
12627 
12628 static int
12629 rack_init(struct tcpcb *tp)
12630 {
12631 	struct tcp_rack *rack = NULL;
12632 #ifdef INVARIANTS
12633 	struct rack_sendmap *insret;
12634 #endif
12635 	uint32_t iwin, snt, us_cts;
12636 	int err;
12637 
12638 	tp->t_fb_ptr = uma_zalloc(rack_pcb_zone, M_NOWAIT);
12639 	if (tp->t_fb_ptr == NULL) {
12640 		/*
12641 		 * We need to allocate memory but cant. The INP and INP_INFO
12642 		 * locks and they are recusive (happens during setup. So a
12643 		 * scheme to drop the locks fails :(
12644 		 *
12645 		 */
12646 		return (ENOMEM);
12647 	}
12648 	memset(tp->t_fb_ptr, 0, sizeof(struct tcp_rack));
12649 
12650 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12651 	RB_INIT(&rack->r_ctl.rc_mtree);
12652 	TAILQ_INIT(&rack->r_ctl.rc_free);
12653 	TAILQ_INIT(&rack->r_ctl.rc_tmap);
12654 	rack->rc_tp = tp;
12655 	rack->rc_inp = tp->t_inpcb;
12656 	/* Set the flag */
12657 	rack->r_is_v6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
12658 	/* Probably not needed but lets be sure */
12659 	rack_clear_rate_sample(rack);
12660 	/*
12661 	 * Save off the default values, socket options will poke
12662 	 * at these if pacing is not on or we have not yet
12663 	 * reached where pacing is on (gp_ready/fixed enabled).
12664 	 * When they get set into the CC module (when gp_ready
12665 	 * is enabled or we enable fixed) then we will set these
12666 	 * values into the CC and place in here the old values
12667 	 * so we have a restoral. Then we will set the flag
12668 	 * rc_pacing_cc_set. That way whenever we turn off pacing
12669 	 * or switch off this stack, we will know to go restore
12670 	 * the saved values.
12671 	 */
12672 	rack->r_ctl.rc_saved_beta.beta = V_newreno_beta_ecn;
12673 	rack->r_ctl.rc_saved_beta.beta_ecn = V_newreno_beta_ecn;
12674 	/* We want abe like behavior as well */
12675 	rack->r_ctl.rc_saved_beta.newreno_flags |= CC_NEWRENO_BETA_ECN_ENABLED;
12676 	rack->r_ctl.rc_reorder_fade = rack_reorder_fade;
12677 	rack->rc_allow_data_af_clo = rack_ignore_data_after_close;
12678 	rack->r_ctl.rc_tlp_threshold = rack_tlp_thresh;
12679 	rack->r_ctl.roundends = tp->snd_max;
12680 	if (use_rack_rr)
12681 		rack->use_rack_rr = 1;
12682 	if (V_tcp_delack_enabled)
12683 		tp->t_delayed_ack = 1;
12684 	else
12685 		tp->t_delayed_ack = 0;
12686 #ifdef TCP_ACCOUNTING
12687 	if (rack_tcp_accounting) {
12688 		tp->t_flags2 |= TF2_TCP_ACCOUNTING;
12689 	}
12690 #endif
12691 	if (rack_enable_shared_cwnd)
12692 		rack->rack_enable_scwnd = 1;
12693 	rack->rc_user_set_max_segs = rack_hptsi_segments;
12694 	rack->rc_force_max_seg = 0;
12695 	if (rack_use_imac_dack)
12696 		rack->rc_dack_mode = 1;
12697 	TAILQ_INIT(&rack->r_ctl.opt_list);
12698 	rack->r_ctl.rc_reorder_shift = rack_reorder_thresh;
12699 	rack->r_ctl.rc_pkt_delay = rack_pkt_delay;
12700 	rack->r_ctl.rc_tlp_cwnd_reduce = rack_lower_cwnd_at_tlp;
12701 	rack->r_ctl.rc_lowest_us_rtt = 0xffffffff;
12702 	rack->r_ctl.rc_highest_us_rtt = 0;
12703 	rack->r_ctl.bw_rate_cap = rack_bw_rate_cap;
12704 	rack->r_ctl.timer_slop = TICKS_2_USEC(tcp_rexmit_slop);
12705 	if (rack_use_cmp_acks)
12706 		rack->r_use_cmp_ack = 1;
12707 	if (rack_disable_prr)
12708 		rack->rack_no_prr = 1;
12709 	if (rack_gp_no_rec_chg)
12710 		rack->rc_gp_no_rec_chg = 1;
12711 	if (rack_pace_every_seg && tcp_can_enable_pacing()) {
12712 		rack->rc_always_pace = 1;
12713 		if (rack->use_fixed_rate || rack->gp_ready)
12714 			rack_set_cc_pacing(rack);
12715 	} else
12716 		rack->rc_always_pace = 0;
12717 	if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack)
12718 		rack->r_mbuf_queue = 1;
12719 	else
12720 		rack->r_mbuf_queue = 0;
12721 	if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
12722 		tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ;
12723 	else
12724 		tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
12725 	rack_set_pace_segments(tp, rack, __LINE__, NULL);
12726 	if (rack_limits_scwnd)
12727 		rack->r_limit_scw = 1;
12728 	else
12729 		rack->r_limit_scw = 0;
12730 	rack->rc_labc = V_tcp_abc_l_var;
12731 	rack->r_ctl.rc_high_rwnd = tp->snd_wnd;
12732 	rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
12733 	rack->r_ctl.rc_rate_sample_method = rack_rate_sample_method;
12734 	rack->rack_tlp_threshold_use = rack_tlp_threshold_use;
12735 	rack->r_ctl.rc_prr_sendalot = rack_send_a_lot_in_prr;
12736 	rack->r_ctl.rc_min_to = rack_min_to;
12737 	microuptime(&rack->r_ctl.act_rcv_time);
12738 	rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time;
12739 	rack->r_running_late = 0;
12740 	rack->r_running_early = 0;
12741 	rack->rc_init_win = rack_default_init_window;
12742 	rack->r_ctl.rack_per_of_gp_ss = rack_per_of_gp_ss;
12743 	if (rack_hw_up_only)
12744 		rack->r_up_only = 1;
12745 	if (rack_do_dyn_mul) {
12746 		/* When dynamic adjustment is on CA needs to start at 100% */
12747 		rack->rc_gp_dyn_mul = 1;
12748 		if (rack_do_dyn_mul >= 100)
12749 			rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul;
12750 	} else
12751 		rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca;
12752 	rack->r_ctl.rack_per_of_gp_rec = rack_per_of_gp_rec;
12753 	rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
12754 	rack->r_ctl.rc_tlp_rxt_last_time = tcp_tv_to_mssectick(&rack->r_ctl.act_rcv_time);
12755 	setup_time_filter_small(&rack->r_ctl.rc_gp_min_rtt, FILTER_TYPE_MIN,
12756 				rack_probertt_filter_life);
12757 	us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
12758 	rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
12759 	rack->r_ctl.rc_time_of_last_probertt = us_cts;
12760 	rack->r_ctl.challenge_ack_ts = tcp_ts_getticks();
12761 	rack->r_ctl.rc_time_probertt_starts = 0;
12762 	if (rack_dsack_std_based & 0x1) {
12763 		/* Basically this means all rack timers are at least (srtt + 1/4 srtt) */
12764 		rack->rc_rack_tmr_std_based = 1;
12765 	}
12766 	if (rack_dsack_std_based & 0x2) {
12767 		/* Basically this means  rack timers are extended based on dsack by up to (2 * srtt) */
12768 		rack->rc_rack_use_dsack = 1;
12769 	}
12770 	/* We require at least one measurement, even if the sysctl is 0 */
12771 	if (rack_req_measurements)
12772 		rack->r_ctl.req_measurements = rack_req_measurements;
12773 	else
12774 		rack->r_ctl.req_measurements = 1;
12775 	if (rack_enable_hw_pacing)
12776 		rack->rack_hdw_pace_ena = 1;
12777 	if (rack_hw_rate_caps)
12778 		rack->r_rack_hw_rate_caps = 1;
12779 	/* Do we force on detection? */
12780 #ifdef NETFLIX_EXP_DETECTION
12781 	if (tcp_force_detection)
12782 		rack->do_detection = 1;
12783 	else
12784 #endif
12785 		rack->do_detection = 0;
12786 	if (rack_non_rxt_use_cr)
12787 		rack->rack_rec_nonrxt_use_cr = 1;
12788 	err = rack_init_fsb(tp, rack);
12789 	if (err) {
12790 		uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
12791 		tp->t_fb_ptr = NULL;
12792 		return (err);
12793 	}
12794 	if (tp->snd_una != tp->snd_max) {
12795 		/* Create a send map for the current outstanding data */
12796 		struct rack_sendmap *rsm;
12797 
12798 		rsm = rack_alloc(rack);
12799 		if (rsm == NULL) {
12800 			uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
12801 			tp->t_fb_ptr = NULL;
12802 			return (ENOMEM);
12803 		}
12804 		rsm->r_no_rtt_allowed = 1;
12805 		rsm->r_tim_lastsent[0] = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
12806 		rsm->r_rtr_cnt = 1;
12807 		rsm->r_rtr_bytes = 0;
12808 		if (tp->t_flags & TF_SENTFIN) {
12809 			rsm->r_end = tp->snd_max - 1;
12810 			rsm->r_flags |= RACK_HAS_FIN;
12811 		} else {
12812 			rsm->r_end = tp->snd_max;
12813 		}
12814 		if (tp->snd_una == tp->iss) {
12815 			/* The data space is one beyond snd_una */
12816 			rsm->r_flags |= RACK_HAS_SYN;
12817 			rsm->r_start = tp->iss;
12818 			rsm->r_end = rsm->r_start + (tp->snd_max - tp->snd_una);
12819 		} else
12820 			rsm->r_start = tp->snd_una;
12821 		rsm->r_dupack = 0;
12822 		if (rack->rc_inp->inp_socket->so_snd.sb_mb != NULL) {
12823 			rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd, 0, &rsm->soff);
12824 			if (rsm->m)
12825 				rsm->orig_m_len = rsm->m->m_len;
12826 			else
12827 				rsm->orig_m_len = 0;
12828 		} else {
12829 			/*
12830 			 * This can happen if we have a stand-alone FIN or
12831 			 *  SYN.
12832 			 */
12833 			rsm->m = NULL;
12834 			rsm->orig_m_len = 0;
12835 			rsm->soff = 0;
12836 		}
12837 #ifndef INVARIANTS
12838 		(void)RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
12839 #else
12840 		insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
12841 		if (insret != NULL) {
12842 			panic("Insert in rb tree fails ret:%p rack:%p rsm:%p",
12843 			      insret, rack, rsm);
12844 		}
12845 #endif
12846 		TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
12847 		rsm->r_in_tmap = 1;
12848 	}
12849 	/*
12850 	 * Timers in Rack are kept in microseconds so lets
12851 	 * convert any initial incoming variables
12852 	 * from ticks into usecs. Note that we
12853 	 * also change the values of t_srtt and t_rttvar, if
12854 	 * they are non-zero. They are kept with a 5
12855 	 * bit decimal so we have to carefully convert
12856 	 * these to get the full precision.
12857 	 */
12858 	rack_convert_rtts(tp);
12859 	tp->t_rttlow = TICKS_2_USEC(tp->t_rttlow);
12860 	if (rack_do_hystart) {
12861 		struct sockopt sopt;
12862 		struct cc_newreno_opts opt;
12863 
12864 		sopt.sopt_valsize = sizeof(struct cc_newreno_opts);
12865 		sopt.sopt_dir = SOPT_SET;
12866 		opt.name = CC_NEWRENO_ENABLE_HYSTART;
12867 		opt.val = rack_do_hystart;
12868 		if (CC_ALGO(tp)->ctl_output != NULL)
12869 			(void)CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
12870 	}
12871 	if (rack_def_profile)
12872 		rack_set_profile(rack, rack_def_profile);
12873 	/* Cancel the GP measurement in progress */
12874 	tp->t_flags &= ~TF_GPUTINPROG;
12875 	if (SEQ_GT(tp->snd_max, tp->iss))
12876 		snt = tp->snd_max - tp->iss;
12877 	else
12878 		snt = 0;
12879 	iwin = rc_init_window(rack);
12880 	if (snt < iwin) {
12881 		/* We are not past the initial window
12882 		 * so we need to make sure cwnd is
12883 		 * correct.
12884 		 */
12885 		if (tp->snd_cwnd < iwin)
12886 			tp->snd_cwnd = iwin;
12887 		/*
12888 		 * If we are within the initial window
12889 		 * we want ssthresh to be unlimited. Setting
12890 		 * it to the rwnd (which the default stack does
12891 		 * and older racks) is not really a good idea
12892 		 * since we want to be in SS and grow both the
12893 		 * cwnd and the rwnd (via dynamic rwnd growth). If
12894 		 * we set it to the rwnd then as the peer grows its
12895 		 * rwnd we will be stuck in CA and never hit SS.
12896 		 *
12897 		 * Its far better to raise it up high (this takes the
12898 		 * risk that there as been a loss already, probably
12899 		 * we should have an indicator in all stacks of loss
12900 		 * but we don't), but considering the normal use this
12901 		 * is a risk worth taking. The consequences of not
12902 		 * hitting SS are far worse than going one more time
12903 		 * into it early on (before we have sent even a IW).
12904 		 * It is highly unlikely that we will have had a loss
12905 		 * before getting the IW out.
12906 		 */
12907 		tp->snd_ssthresh = 0xffffffff;
12908 	}
12909 	rack_stop_all_timers(tp);
12910 	/* Lets setup the fsb block */
12911 	rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
12912 	rack_log_rtt_shrinks(rack,  us_cts,  tp->t_rxtcur,
12913 			     __LINE__, RACK_RTTS_INIT);
12914 	return (0);
12915 }
12916 
12917 static int
12918 rack_handoff_ok(struct tcpcb *tp)
12919 {
12920 	if ((tp->t_state == TCPS_CLOSED) ||
12921 	    (tp->t_state == TCPS_LISTEN)) {
12922 		/* Sure no problem though it may not stick */
12923 		return (0);
12924 	}
12925 	if ((tp->t_state == TCPS_SYN_SENT) ||
12926 	    (tp->t_state == TCPS_SYN_RECEIVED)) {
12927 		/*
12928 		 * We really don't know if you support sack,
12929 		 * you have to get to ESTAB or beyond to tell.
12930 		 */
12931 		return (EAGAIN);
12932 	}
12933 	if ((tp->t_flags & TF_SENTFIN) && ((tp->snd_max - tp->snd_una) > 1)) {
12934 		/*
12935 		 * Rack will only send a FIN after all data is acknowledged.
12936 		 * So in this case we have more data outstanding. We can't
12937 		 * switch stacks until either all data and only the FIN
12938 		 * is left (in which case rack_init() now knows how
12939 		 * to deal with that) <or> all is acknowledged and we
12940 		 * are only left with incoming data, though why you
12941 		 * would want to switch to rack after all data is acknowledged
12942 		 * I have no idea (rrs)!
12943 		 */
12944 		return (EAGAIN);
12945 	}
12946 	if ((tp->t_flags & TF_SACK_PERMIT) || rack_sack_not_required){
12947 		return (0);
12948 	}
12949 	/*
12950 	 * If we reach here we don't do SACK on this connection so we can
12951 	 * never do rack.
12952 	 */
12953 	return (EINVAL);
12954 }
12955 
12956 
12957 static void
12958 rack_fini(struct tcpcb *tp, int32_t tcb_is_purged)
12959 {
12960 	int ack_cmp = 0;
12961 
12962 	if (tp->t_fb_ptr) {
12963 		struct tcp_rack *rack;
12964 		struct rack_sendmap *rsm, *nrsm;
12965 #ifdef INVARIANTS
12966 		struct rack_sendmap *rm;
12967 #endif
12968 
12969 		rack = (struct tcp_rack *)tp->t_fb_ptr;
12970 		if (tp->t_in_pkt) {
12971 			/*
12972 			 * It is unsafe to process the packets since a
12973 			 * reset may be lurking in them (its rare but it
12974 			 * can occur). If we were to find a RST, then we
12975 			 * would end up dropping the connection and the
12976 			 * INP lock, so when we return the caller (tcp_usrreq)
12977 			 * will blow up when it trys to unlock the inp.
12978 			 */
12979 			struct mbuf *save, *m;
12980 
12981 			m = tp->t_in_pkt;
12982 			tp->t_in_pkt = NULL;
12983 			tp->t_tail_pkt = NULL;
12984 			while (m) {
12985 				save = m->m_nextpkt;
12986 				m->m_nextpkt = NULL;
12987 				m_freem(m);
12988 				m = save;
12989 			}
12990 			if ((tp->t_inpcb) &&
12991 			    (tp->t_inpcb->inp_flags2 & INP_MBUF_ACKCMP))
12992 				ack_cmp = 1;
12993 			if (ack_cmp) {
12994 				/* Total if we used large or small (if ack-cmp was used). */
12995 				if (rack->rc_inp->inp_flags2 & INP_MBUF_L_ACKS)
12996 					counter_u64_add(rack_large_ackcmp, 1);
12997 				else
12998 					counter_u64_add(rack_small_ackcmp, 1);
12999 			}
13000 		}
13001 		tp->t_flags &= ~TF_FORCEDATA;
13002 #ifdef NETFLIX_SHARED_CWND
13003 		if (rack->r_ctl.rc_scw) {
13004 			uint32_t limit;
13005 
13006 			if (rack->r_limit_scw)
13007 				limit = max(1, rack->r_ctl.rc_lowest_us_rtt);
13008 			else
13009 				limit = 0;
13010 			tcp_shared_cwnd_free_full(tp, rack->r_ctl.rc_scw,
13011 						  rack->r_ctl.rc_scw_index,
13012 						  limit);
13013 			rack->r_ctl.rc_scw = NULL;
13014 		}
13015 #endif
13016 		if (rack->r_ctl.fsb.tcp_ip_hdr) {
13017 			free(rack->r_ctl.fsb.tcp_ip_hdr, M_TCPFSB);
13018 			rack->r_ctl.fsb.tcp_ip_hdr = NULL;
13019 			rack->r_ctl.fsb.th = NULL;
13020 		}
13021 		/* Convert back to ticks, with  */
13022 		if (tp->t_srtt > 1) {
13023 			uint32_t val, frac;
13024 
13025 			val = USEC_2_TICKS(tp->t_srtt);
13026 			frac = tp->t_srtt % (HPTS_USEC_IN_SEC / hz);
13027 			tp->t_srtt = val << TCP_RTT_SHIFT;
13028 			/*
13029 			 * frac is the fractional part here is left
13030 			 * over from converting to hz and shifting.
13031 			 * We need to convert this to the 5 bit
13032 			 * remainder.
13033 			 */
13034 			if (frac) {
13035 				if (hz == 1000) {
13036 					frac = (((uint64_t)frac *  (uint64_t)TCP_RTT_SCALE) / (uint64_t)HPTS_USEC_IN_MSEC);
13037 				} else {
13038 					frac = (((uint64_t)frac * (uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE) /(uint64_t)HPTS_USEC_IN_SEC);
13039 				}
13040 				tp->t_srtt += frac;
13041 			}
13042 		}
13043 		if (tp->t_rttvar) {
13044 			uint32_t val, frac;
13045 
13046 			val = USEC_2_TICKS(tp->t_rttvar);
13047 			frac = tp->t_srtt % (HPTS_USEC_IN_SEC / hz);
13048 			tp->t_rttvar = val <<  TCP_RTTVAR_SHIFT;
13049 			/*
13050 			 * frac is the fractional part here is left
13051 			 * over from converting to hz and shifting.
13052 			 * We need to convert this to the 5 bit
13053 			 * remainder.
13054 			 */
13055 			if (frac) {
13056 				if (hz == 1000) {
13057 					frac = (((uint64_t)frac *  (uint64_t)TCP_RTT_SCALE) / (uint64_t)HPTS_USEC_IN_MSEC);
13058 				} else {
13059 					frac = (((uint64_t)frac * (uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE) /(uint64_t)HPTS_USEC_IN_SEC);
13060 				}
13061 				tp->t_rttvar += frac;
13062 			}
13063 		}
13064 		tp->t_rxtcur = USEC_2_TICKS(tp->t_rxtcur);
13065 		tp->t_rttlow = USEC_2_TICKS(tp->t_rttlow);
13066 		if (rack->rc_always_pace) {
13067 			tcp_decrement_paced_conn();
13068 			rack_undo_cc_pacing(rack);
13069 			rack->rc_always_pace = 0;
13070 		}
13071 		/* Clean up any options if they were not applied */
13072 		while (!TAILQ_EMPTY(&rack->r_ctl.opt_list)) {
13073 			struct deferred_opt_list *dol;
13074 
13075 			dol = TAILQ_FIRST(&rack->r_ctl.opt_list);
13076 			TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next);
13077 			free(dol, M_TCPDO);
13078 		}
13079 		/* rack does not use force data but other stacks may clear it */
13080 		if (rack->r_ctl.crte != NULL) {
13081 			tcp_rel_pacing_rate(rack->r_ctl.crte, tp);
13082 			rack->rack_hdrw_pacing = 0;
13083 			rack->r_ctl.crte = NULL;
13084 		}
13085 #ifdef TCP_BLACKBOX
13086 		tcp_log_flowend(tp);
13087 #endif
13088 		RB_FOREACH_SAFE(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm) {
13089 #ifndef INVARIANTS
13090 			(void)RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
13091 #else
13092 			rm = RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
13093 			if (rm != rsm) {
13094 				panic("At fini, rack:%p rsm:%p rm:%p",
13095 				      rack, rsm, rm);
13096 			}
13097 #endif
13098 			uma_zfree(rack_zone, rsm);
13099 		}
13100 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
13101 		while (rsm) {
13102 			TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
13103 			uma_zfree(rack_zone, rsm);
13104 			rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
13105 		}
13106 		rack->rc_free_cnt = 0;
13107 		uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
13108 		tp->t_fb_ptr = NULL;
13109 	}
13110 	if (tp->t_inpcb) {
13111 		tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
13112 		tp->t_inpcb->inp_flags2 &= ~INP_MBUF_QUEUE_READY;
13113 		tp->t_inpcb->inp_flags2 &= ~INP_DONT_SACK_QUEUE;
13114 		tp->t_inpcb->inp_flags2 &= ~INP_MBUF_ACKCMP;
13115 		/* Cancel the GP measurement in progress */
13116 		tp->t_flags &= ~TF_GPUTINPROG;
13117 		tp->t_inpcb->inp_flags2 &= ~INP_MBUF_L_ACKS;
13118 	}
13119 	/* Make sure snd_nxt is correctly set */
13120 	tp->snd_nxt = tp->snd_max;
13121 }
13122 
13123 static void
13124 rack_set_state(struct tcpcb *tp, struct tcp_rack *rack)
13125 {
13126 	if ((rack->r_state == TCPS_CLOSED) && (tp->t_state != TCPS_CLOSED)) {
13127 		rack->r_is_v6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
13128 	}
13129 	switch (tp->t_state) {
13130 	case TCPS_SYN_SENT:
13131 		rack->r_state = TCPS_SYN_SENT;
13132 		rack->r_substate = rack_do_syn_sent;
13133 		break;
13134 	case TCPS_SYN_RECEIVED:
13135 		rack->r_state = TCPS_SYN_RECEIVED;
13136 		rack->r_substate = rack_do_syn_recv;
13137 		break;
13138 	case TCPS_ESTABLISHED:
13139 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
13140 		rack->r_state = TCPS_ESTABLISHED;
13141 		rack->r_substate = rack_do_established;
13142 		break;
13143 	case TCPS_CLOSE_WAIT:
13144 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
13145 		rack->r_state = TCPS_CLOSE_WAIT;
13146 		rack->r_substate = rack_do_close_wait;
13147 		break;
13148 	case TCPS_FIN_WAIT_1:
13149 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
13150 		rack->r_state = TCPS_FIN_WAIT_1;
13151 		rack->r_substate = rack_do_fin_wait_1;
13152 		break;
13153 	case TCPS_CLOSING:
13154 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
13155 		rack->r_state = TCPS_CLOSING;
13156 		rack->r_substate = rack_do_closing;
13157 		break;
13158 	case TCPS_LAST_ACK:
13159 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
13160 		rack->r_state = TCPS_LAST_ACK;
13161 		rack->r_substate = rack_do_lastack;
13162 		break;
13163 	case TCPS_FIN_WAIT_2:
13164 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
13165 		rack->r_state = TCPS_FIN_WAIT_2;
13166 		rack->r_substate = rack_do_fin_wait_2;
13167 		break;
13168 	case TCPS_LISTEN:
13169 	case TCPS_CLOSED:
13170 	case TCPS_TIME_WAIT:
13171 	default:
13172 		break;
13173 	};
13174 	if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
13175 		rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
13176 
13177 }
13178 
13179 static void
13180 rack_timer_audit(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb)
13181 {
13182 	/*
13183 	 * We received an ack, and then did not
13184 	 * call send or were bounced out due to the
13185 	 * hpts was running. Now a timer is up as well, is
13186 	 * it the right timer?
13187 	 */
13188 	struct rack_sendmap *rsm;
13189 	int tmr_up;
13190 
13191 	tmr_up = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
13192 	if (rack->rc_in_persist && (tmr_up == PACE_TMR_PERSIT))
13193 		return;
13194 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
13195 	if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) &&
13196 	    (tmr_up == PACE_TMR_RXT)) {
13197 		/* Should be an RXT */
13198 		return;
13199 	}
13200 	if (rsm == NULL) {
13201 		/* Nothing outstanding? */
13202 		if (tp->t_flags & TF_DELACK) {
13203 			if (tmr_up == PACE_TMR_DELACK)
13204 				/* We are supposed to have delayed ack up and we do */
13205 				return;
13206 		} else if (sbavail(&tp->t_inpcb->inp_socket->so_snd) && (tmr_up == PACE_TMR_RXT)) {
13207 			/*
13208 			 * if we hit enobufs then we would expect the possiblity
13209 			 * of nothing outstanding and the RXT up (and the hptsi timer).
13210 			 */
13211 			return;
13212 		} else if (((V_tcp_always_keepalive ||
13213 			     rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
13214 			    (tp->t_state <= TCPS_CLOSING)) &&
13215 			   (tmr_up == PACE_TMR_KEEP) &&
13216 			   (tp->snd_max == tp->snd_una)) {
13217 			/* We should have keep alive up and we do */
13218 			return;
13219 		}
13220 	}
13221 	if (SEQ_GT(tp->snd_max, tp->snd_una) &&
13222 		   ((tmr_up == PACE_TMR_TLP) ||
13223 		    (tmr_up == PACE_TMR_RACK) ||
13224 		    (tmr_up == PACE_TMR_RXT))) {
13225 		/*
13226 		 * Either a Rack, TLP or RXT is fine if  we
13227 		 * have outstanding data.
13228 		 */
13229 		return;
13230 	} else if (tmr_up == PACE_TMR_DELACK) {
13231 		/*
13232 		 * If the delayed ack was going to go off
13233 		 * before the rtx/tlp/rack timer were going to
13234 		 * expire, then that would be the timer in control.
13235 		 * Note we don't check the time here trusting the
13236 		 * code is correct.
13237 		 */
13238 		return;
13239 	}
13240 	/*
13241 	 * Ok the timer originally started is not what we want now.
13242 	 * We will force the hpts to be stopped if any, and restart
13243 	 * with the slot set to what was in the saved slot.
13244 	 */
13245 	if (tcp_in_hpts(rack->rc_inp)) {
13246 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
13247 			uint32_t us_cts;
13248 
13249 			us_cts = tcp_get_usecs(NULL);
13250 			if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
13251 				rack->r_early = 1;
13252 				rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
13253 			}
13254 			rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
13255 		}
13256 		tcp_hpts_remove(tp->t_inpcb);
13257 	}
13258 	rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
13259 	rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
13260 }
13261 
13262 
13263 static void
13264 rack_do_win_updates(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tiwin, uint32_t seq, uint32_t ack, uint32_t cts, uint32_t high_seq)
13265 {
13266 	if ((SEQ_LT(tp->snd_wl1, seq) ||
13267 	    (tp->snd_wl1 == seq && (SEQ_LT(tp->snd_wl2, ack) ||
13268 	    (tp->snd_wl2 == ack && tiwin > tp->snd_wnd))))) {
13269 		/* keep track of pure window updates */
13270 		if ((tp->snd_wl2 == ack) && (tiwin > tp->snd_wnd))
13271 			KMOD_TCPSTAT_INC(tcps_rcvwinupd);
13272 		tp->snd_wnd = tiwin;
13273 		rack_validate_fo_sendwin_up(tp, rack);
13274 		tp->snd_wl1 = seq;
13275 		tp->snd_wl2 = ack;
13276 		if (tp->snd_wnd > tp->max_sndwnd)
13277 			tp->max_sndwnd = tp->snd_wnd;
13278 	    rack->r_wanted_output = 1;
13279 	} else if ((tp->snd_wl2 == ack) && (tiwin < tp->snd_wnd)) {
13280 		tp->snd_wnd = tiwin;
13281 		rack_validate_fo_sendwin_up(tp, rack);
13282 		tp->snd_wl1 = seq;
13283 		tp->snd_wl2 = ack;
13284 	} else {
13285 		/* Not a valid win update */
13286 		return;
13287 	}
13288 	if (tp->snd_wnd > tp->max_sndwnd)
13289 		tp->max_sndwnd = tp->snd_wnd;
13290 	if (tp->snd_wnd < (tp->snd_max - high_seq)) {
13291 		/* The peer collapsed the window */
13292 		rack_collapsed_window(rack);
13293 	} else if (rack->rc_has_collapsed)
13294 		rack_un_collapse_window(rack);
13295 	/* Do we exit persists? */
13296 	if ((rack->rc_in_persist != 0) &&
13297 	    (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
13298 				rack->r_ctl.rc_pace_min_segs))) {
13299 		rack_exit_persist(tp, rack, cts);
13300 	}
13301 	/* Do we enter persists? */
13302 	if ((rack->rc_in_persist == 0) &&
13303 	    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
13304 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
13305 	    (tp->snd_max == tp->snd_una) &&
13306 	    sbavail(&tp->t_inpcb->inp_socket->so_snd) &&
13307 	    (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) {
13308 		/*
13309 		 * Here the rwnd is less than
13310 		 * the pacing size, we are established,
13311 		 * nothing is outstanding, and there is
13312 		 * data to send. Enter persists.
13313 		 */
13314 		rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime);
13315 	}
13316 }
13317 
13318 static void
13319 rack_log_input_packet(struct tcpcb *tp, struct tcp_rack *rack, struct tcp_ackent *ae, int ackval, uint32_t high_seq)
13320 {
13321 
13322 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
13323 		union tcp_log_stackspecific log;
13324 		struct timeval ltv;
13325 		char tcp_hdr_buf[60];
13326 		struct tcphdr *th;
13327 		struct timespec ts;
13328 		uint32_t orig_snd_una;
13329 		uint8_t xx = 0;
13330 
13331 #ifdef NETFLIX_HTTP_LOGGING
13332 		struct http_sendfile_track *http_req;
13333 
13334 		if (SEQ_GT(ae->ack, tp->snd_una)) {
13335 			http_req = tcp_http_find_req_for_seq(tp, (ae->ack-1));
13336 		} else {
13337 			http_req = tcp_http_find_req_for_seq(tp, ae->ack);
13338 		}
13339 #endif
13340 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
13341 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
13342 		if (rack->rack_no_prr == 0)
13343 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
13344 		else
13345 			log.u_bbr.flex1 = 0;
13346 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
13347 		log.u_bbr.use_lt_bw <<= 1;
13348 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
13349 		log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced;
13350 		log.u_bbr.inflight = ctf_flight_size(tp, rack->r_ctl.rc_sacked);
13351 		log.u_bbr.pkts_out = tp->t_maxseg;
13352 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
13353 		log.u_bbr.flex7 = 1;
13354 		log.u_bbr.lost = ae->flags;
13355 		log.u_bbr.cwnd_gain = ackval;
13356 		log.u_bbr.pacing_gain = 0x2;
13357 		if (ae->flags & TSTMP_HDWR) {
13358 			/* Record the hardware timestamp if present */
13359 			log.u_bbr.flex3 = M_TSTMP;
13360 			ts.tv_sec = ae->timestamp / 1000000000;
13361 			ts.tv_nsec = ae->timestamp % 1000000000;
13362 			ltv.tv_sec = ts.tv_sec;
13363 			ltv.tv_usec = ts.tv_nsec / 1000;
13364 			log.u_bbr.lt_epoch = tcp_tv_to_usectick(&ltv);
13365 		} else if (ae->flags & TSTMP_LRO) {
13366 			/* Record the LRO the arrival timestamp */
13367 			log.u_bbr.flex3 = M_TSTMP_LRO;
13368 			ts.tv_sec = ae->timestamp / 1000000000;
13369 			ts.tv_nsec = ae->timestamp % 1000000000;
13370 			ltv.tv_sec = ts.tv_sec;
13371 			ltv.tv_usec = ts.tv_nsec / 1000;
13372 			log.u_bbr.flex5 = tcp_tv_to_usectick(&ltv);
13373 		}
13374 		log.u_bbr.timeStamp = tcp_get_usecs(&ltv);
13375 		/* Log the rcv time */
13376 		log.u_bbr.delRate = ae->timestamp;
13377 #ifdef NETFLIX_HTTP_LOGGING
13378 		log.u_bbr.applimited = tp->t_http_closed;
13379 		log.u_bbr.applimited <<= 8;
13380 		log.u_bbr.applimited |= tp->t_http_open;
13381 		log.u_bbr.applimited <<= 8;
13382 		log.u_bbr.applimited |= tp->t_http_req;
13383 		if (http_req) {
13384 			/* Copy out any client req info */
13385 			/* seconds */
13386 			log.u_bbr.pkt_epoch = (http_req->localtime / HPTS_USEC_IN_SEC);
13387 			/* useconds */
13388 			log.u_bbr.delivered = (http_req->localtime % HPTS_USEC_IN_SEC);
13389 			log.u_bbr.rttProp = http_req->timestamp;
13390 			log.u_bbr.cur_del_rate = http_req->start;
13391 			if (http_req->flags & TCP_HTTP_TRACK_FLG_OPEN) {
13392 				log.u_bbr.flex8 |= 1;
13393 			} else {
13394 				log.u_bbr.flex8 |= 2;
13395 				log.u_bbr.bw_inuse = http_req->end;
13396 			}
13397 			log.u_bbr.flex6 = http_req->start_seq;
13398 			if (http_req->flags & TCP_HTTP_TRACK_FLG_COMP) {
13399 				log.u_bbr.flex8 |= 4;
13400 				log.u_bbr.epoch = http_req->end_seq;
13401 			}
13402 		}
13403 #endif
13404 		memset(tcp_hdr_buf, 0, sizeof(tcp_hdr_buf));
13405 		th = (struct tcphdr *)tcp_hdr_buf;
13406 		th->th_seq = ae->seq;
13407 		th->th_ack = ae->ack;
13408 		th->th_win = ae->win;
13409 		/* Now fill in the ports */
13410 		th->th_sport = tp->t_inpcb->inp_fport;
13411 		th->th_dport = tp->t_inpcb->inp_lport;
13412 		tcp_set_flags(th, ae->flags);
13413 		/* Now do we have a timestamp option? */
13414 		if (ae->flags & HAS_TSTMP) {
13415 			u_char *cp;
13416 			uint32_t val;
13417 
13418 			th->th_off = ((sizeof(struct tcphdr) + TCPOLEN_TSTAMP_APPA) >> 2);
13419 			cp = (u_char *)(th + 1);
13420 			*cp = TCPOPT_NOP;
13421 			cp++;
13422 			*cp = TCPOPT_NOP;
13423 			cp++;
13424 			*cp = TCPOPT_TIMESTAMP;
13425 			cp++;
13426 			*cp = TCPOLEN_TIMESTAMP;
13427 			cp++;
13428 			val = htonl(ae->ts_value);
13429 			bcopy((char *)&val,
13430 			      (char *)cp, sizeof(uint32_t));
13431 			val = htonl(ae->ts_echo);
13432 			bcopy((char *)&val,
13433 			      (char *)(cp + 4), sizeof(uint32_t));
13434 		} else
13435 			th->th_off = (sizeof(struct tcphdr) >> 2);
13436 
13437 		/*
13438 		 * For sane logging we need to play a little trick.
13439 		 * If the ack were fully processed we would have moved
13440 		 * snd_una to high_seq, but since compressed acks are
13441 		 * processed in two phases, at this point (logging) snd_una
13442 		 * won't be advanced. So we would see multiple acks showing
13443 		 * the advancement. We can prevent that by "pretending" that
13444 		 * snd_una was advanced and then un-advancing it so that the
13445 		 * logging code has the right value for tlb_snd_una.
13446 		 */
13447 		if (tp->snd_una != high_seq) {
13448 			orig_snd_una = tp->snd_una;
13449 			tp->snd_una = high_seq;
13450 			xx = 1;
13451 		} else
13452 			xx = 0;
13453 		TCP_LOG_EVENTP(tp, th,
13454 			       &tp->t_inpcb->inp_socket->so_rcv,
13455 			       &tp->t_inpcb->inp_socket->so_snd, TCP_LOG_IN, 0,
13456 			       0, &log, true, &ltv);
13457 		if (xx) {
13458 			tp->snd_una = orig_snd_una;
13459 		}
13460 	}
13461 
13462 }
13463 
13464 static void
13465 rack_handle_probe_response(struct tcp_rack *rack, uint32_t tiwin, uint32_t us_cts)
13466 {
13467 	uint32_t us_rtt;
13468 	/*
13469 	 * A persist or keep-alive was forced out, update our
13470 	 * min rtt time. Note now worry about lost responses.
13471 	 * When a subsequent keep-alive or persist times out
13472 	 * and forced_ack is still on, then the last probe
13473 	 * was not responded to. In such cases we have a
13474 	 * sysctl that controls the behavior. Either we apply
13475 	 * the rtt but with reduced confidence (0). Or we just
13476 	 * plain don't apply the rtt estimate. Having data flow
13477 	 * will clear the probe_not_answered flag i.e. cum-ack
13478 	 * move forward <or> exiting and reentering persists.
13479 	 */
13480 
13481 	rack->forced_ack = 0;
13482 	rack->rc_tp->t_rxtshift = 0;
13483 	if ((rack->rc_in_persist &&
13484 	     (tiwin == rack->rc_tp->snd_wnd)) ||
13485 	    (rack->rc_in_persist == 0)) {
13486 		/*
13487 		 * In persists only apply the RTT update if this is
13488 		 * a response to our window probe. And that
13489 		 * means the rwnd sent must match the current
13490 		 * snd_wnd. If it does not, then we got a
13491 		 * window update ack instead. For keepalive
13492 		 * we allow the answer no matter what the window.
13493 		 *
13494 		 * Note that if the probe_not_answered is set then
13495 		 * the forced_ack_ts is the oldest one i.e. the first
13496 		 * probe sent that might have been lost. This assures
13497 		 * us that if we do calculate an RTT it is longer not
13498 		 * some short thing.
13499 		 */
13500 		if (rack->rc_in_persist)
13501 			counter_u64_add(rack_persists_acks, 1);
13502 		us_rtt = us_cts - rack->r_ctl.forced_ack_ts;
13503 		if (us_rtt == 0)
13504 			us_rtt = 1;
13505 		if (rack->probe_not_answered == 0) {
13506 			rack_apply_updated_usrtt(rack, us_rtt, us_cts);
13507 			tcp_rack_xmit_timer(rack, us_rtt, 0, us_rtt, 3, NULL, 1);
13508 		} else {
13509 			/* We have a retransmitted probe here too */
13510 			if (rack_apply_rtt_with_reduced_conf) {
13511 				rack_apply_updated_usrtt(rack, us_rtt, us_cts);
13512 				tcp_rack_xmit_timer(rack, us_rtt, 0, us_rtt, 0, NULL, 1);
13513 			}
13514 		}
13515 	}
13516 }
13517 
13518 
13519 static int
13520 rack_do_compressed_ack_processing(struct tcpcb *tp, struct socket *so, struct mbuf *m, int nxt_pkt, struct timeval *tv)
13521 {
13522 	/*
13523 	 * Handle a "special" compressed ack mbuf. Each incoming
13524 	 * ack has only four possible dispositions:
13525 	 *
13526 	 * A) It moves the cum-ack forward
13527 	 * B) It is behind the cum-ack.
13528 	 * C) It is a window-update ack.
13529 	 * D) It is a dup-ack.
13530 	 *
13531 	 * Note that we can have between 1 -> TCP_COMP_ACK_ENTRIES
13532 	 * in the incoming mbuf. We also need to still pay attention
13533 	 * to nxt_pkt since there may be another packet after this
13534 	 * one.
13535 	 */
13536 #ifdef TCP_ACCOUNTING
13537 	uint64_t ts_val;
13538 	uint64_t rdstc;
13539 #endif
13540 	int segsiz;
13541 	struct timespec ts;
13542 	struct tcp_rack *rack;
13543 	struct tcp_ackent *ae;
13544 	uint32_t tiwin, ms_cts, cts, acked, acked_amount, high_seq, win_seq, the_win, win_upd_ack;
13545 	int cnt, i, did_out, ourfinisacked = 0;
13546 	struct tcpopt to_holder, *to = NULL;
13547 #ifdef TCP_ACCOUNTING
13548 	int win_up_req = 0;
13549 #endif
13550 	int nsegs = 0;
13551 	int under_pacing = 1;
13552 	int recovery = 0;
13553 	int idx;
13554 #ifdef TCP_ACCOUNTING
13555 	sched_pin();
13556 #endif
13557 	rack = (struct tcp_rack *)tp->t_fb_ptr;
13558 	if (rack->gp_ready &&
13559 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT))
13560 		under_pacing = 0;
13561 	else
13562 		under_pacing = 1;
13563 
13564 	if (rack->r_state != tp->t_state)
13565 		rack_set_state(tp, rack);
13566 	if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
13567 	    (tp->t_flags & TF_GPUTINPROG)) {
13568 		/*
13569 		 * We have a goodput in progress
13570 		 * and we have entered a late state.
13571 		 * Do we have enough data in the sb
13572 		 * to handle the GPUT request?
13573 		 */
13574 		uint32_t bytes;
13575 
13576 		bytes = tp->gput_ack - tp->gput_seq;
13577 		if (SEQ_GT(tp->gput_seq, tp->snd_una))
13578 			bytes += tp->gput_seq - tp->snd_una;
13579 		if (bytes > sbavail(&tp->t_inpcb->inp_socket->so_snd)) {
13580 			/*
13581 			 * There are not enough bytes in the socket
13582 			 * buffer that have been sent to cover this
13583 			 * measurement. Cancel it.
13584 			 */
13585 			rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
13586 						   rack->r_ctl.rc_gp_srtt /*flex1*/,
13587 						   tp->gput_seq,
13588 						   0, 0, 18, __LINE__, NULL, 0);
13589 			tp->t_flags &= ~TF_GPUTINPROG;
13590 		}
13591 	}
13592 	to = &to_holder;
13593 	to->to_flags = 0;
13594 	KASSERT((m->m_len >= sizeof(struct tcp_ackent)),
13595 		("tp:%p m_cmpack:%p with invalid len:%u", tp, m, m->m_len));
13596 	cnt = m->m_len / sizeof(struct tcp_ackent);
13597 	idx = cnt / 5;
13598 	if (idx >= MAX_NUM_OF_CNTS)
13599 		idx = MAX_NUM_OF_CNTS - 1;
13600 	counter_u64_add(rack_proc_comp_ack[idx], 1);
13601 	counter_u64_add(rack_multi_single_eq, cnt);
13602 	high_seq = tp->snd_una;
13603 	the_win = tp->snd_wnd;
13604 	win_seq = tp->snd_wl1;
13605 	win_upd_ack = tp->snd_wl2;
13606 	cts = tcp_tv_to_usectick(tv);
13607 	ms_cts = tcp_tv_to_mssectick(tv);
13608 	segsiz = ctf_fixed_maxseg(tp);
13609 	if ((rack->rc_gp_dyn_mul) &&
13610 	    (rack->use_fixed_rate == 0) &&
13611 	    (rack->rc_always_pace)) {
13612 		/* Check in on probertt */
13613 		rack_check_probe_rtt(rack, cts);
13614 	}
13615 	for (i = 0; i < cnt; i++) {
13616 #ifdef TCP_ACCOUNTING
13617 		ts_val = get_cyclecount();
13618 #endif
13619 		rack_clear_rate_sample(rack);
13620 		ae = ((mtod(m, struct tcp_ackent *)) + i);
13621 		/* Setup the window */
13622 		tiwin = ae->win << tp->snd_scale;
13623 		/* figure out the type of ack */
13624 		if (SEQ_LT(ae->ack, high_seq)) {
13625 			/* Case B*/
13626 			ae->ack_val_set = ACK_BEHIND;
13627 		} else if (SEQ_GT(ae->ack, high_seq)) {
13628 			/* Case A */
13629 			ae->ack_val_set = ACK_CUMACK;
13630 		} else if ((tiwin == the_win) && (rack->rc_in_persist == 0)){
13631 			/* Case D */
13632 			ae->ack_val_set = ACK_DUPACK;
13633 		} else {
13634 			/* Case C */
13635 			ae->ack_val_set = ACK_RWND;
13636 		}
13637 		rack_log_input_packet(tp, rack, ae, ae->ack_val_set, high_seq);
13638 		/* Validate timestamp */
13639 		if (ae->flags & HAS_TSTMP) {
13640 			/* Setup for a timestamp */
13641 			to->to_flags = TOF_TS;
13642 			ae->ts_echo -= tp->ts_offset;
13643 			to->to_tsecr = ae->ts_echo;
13644 			to->to_tsval = ae->ts_value;
13645 			/*
13646 			 * If echoed timestamp is later than the current time, fall back to
13647 			 * non RFC1323 RTT calculation.  Normalize timestamp if syncookies
13648 			 * were used when this connection was established.
13649 			 */
13650 			if (TSTMP_GT(ae->ts_echo, ms_cts))
13651 				to->to_tsecr = 0;
13652 			if (tp->ts_recent &&
13653 			    TSTMP_LT(ae->ts_value, tp->ts_recent)) {
13654 				if (ctf_ts_check_ac(tp, (ae->flags & 0xff))) {
13655 #ifdef TCP_ACCOUNTING
13656 					rdstc = get_cyclecount();
13657 					if (rdstc > ts_val) {
13658 						counter_u64_add(tcp_proc_time[ae->ack_val_set] ,
13659 								(rdstc - ts_val));
13660 						if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13661 							tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val);
13662 						}
13663 					}
13664 #endif
13665 					continue;
13666 				}
13667 			}
13668 			if (SEQ_LEQ(ae->seq, tp->last_ack_sent) &&
13669 			    SEQ_LEQ(tp->last_ack_sent, ae->seq)) {
13670 				tp->ts_recent_age = tcp_ts_getticks();
13671 				tp->ts_recent = ae->ts_value;
13672 			}
13673 		} else {
13674 			/* Setup for a no options */
13675 			to->to_flags = 0;
13676 		}
13677 		/* Update the rcv time and perform idle reduction possibly */
13678 		if  (tp->t_idle_reduce &&
13679 		     (tp->snd_max == tp->snd_una) &&
13680 		     ((ticks - tp->t_rcvtime) >= tp->t_rxtcur)) {
13681 			counter_u64_add(rack_input_idle_reduces, 1);
13682 			rack_cc_after_idle(rack, tp);
13683 		}
13684 		tp->t_rcvtime = ticks;
13685 		/* Now what about ECN? */
13686 		if (tcp_ecn_input_segment(tp, ae->flags, ae->codepoint))
13687 			rack_cong_signal(tp, CC_ECN, ae->ack);
13688 #ifdef TCP_ACCOUNTING
13689 		/* Count for the specific type of ack in */
13690 		counter_u64_add(tcp_cnt_counters[ae->ack_val_set], 1);
13691 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13692 			tp->tcp_cnt_counters[ae->ack_val_set]++;
13693 		}
13694 #endif
13695 		/*
13696 		 * Note how we could move up these in the determination
13697 		 * above, but we don't so that way the timestamp checks (and ECN)
13698 		 * is done first before we do any processing on the ACK.
13699 		 * The non-compressed path through the code has this
13700 		 * weakness (noted by @jtl) that it actually does some
13701 		 * processing before verifying the timestamp information.
13702 		 * We don't take that path here which is why we set
13703 		 * the ack_val_set first, do the timestamp and ecn
13704 		 * processing, and then look at what we have setup.
13705 		 */
13706 		if (ae->ack_val_set == ACK_BEHIND) {
13707 			/*
13708 			 * Case B flag reordering, if window is not closed
13709 			 * or it could be a keep-alive or persists
13710 			 */
13711 			if (SEQ_LT(ae->ack, tp->snd_una) && (sbspace(&so->so_rcv) > segsiz)) {
13712 				counter_u64_add(rack_reorder_seen, 1);
13713 				rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
13714 			}
13715 		} else if (ae->ack_val_set == ACK_DUPACK) {
13716 			/* Case D */
13717 			rack_strike_dupack(rack);
13718 		} else if (ae->ack_val_set == ACK_RWND) {
13719 			/* Case C */
13720 			if ((ae->flags & TSTMP_LRO) || (ae->flags & TSTMP_HDWR)) {
13721 				ts.tv_sec = ae->timestamp / 1000000000;
13722 				ts.tv_nsec = ae->timestamp % 1000000000;
13723 				rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
13724 				rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
13725 			} else {
13726 				rack->r_ctl.act_rcv_time = *tv;
13727 			}
13728 			if (rack->forced_ack) {
13729 				rack_handle_probe_response(rack, tiwin,
13730 							   tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time));
13731 			}
13732 #ifdef TCP_ACCOUNTING
13733 			win_up_req = 1;
13734 #endif
13735 			win_upd_ack = ae->ack;
13736 			win_seq = ae->seq;
13737 			the_win = tiwin;
13738 			rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts, high_seq);
13739 		} else {
13740 			/* Case A */
13741 			if (SEQ_GT(ae->ack, tp->snd_max)) {
13742 				/*
13743 				 * We just send an ack since the incoming
13744 				 * ack is beyond the largest seq we sent.
13745 				 */
13746 				if ((tp->t_flags & TF_ACKNOW) == 0) {
13747 					ctf_ack_war_checks(tp, &rack->r_ctl.challenge_ack_ts, &rack->r_ctl.challenge_ack_cnt);
13748 					if (tp->t_flags && TF_ACKNOW)
13749 						rack->r_wanted_output = 1;
13750 				}
13751 			} else {
13752 				nsegs++;
13753 				/* If the window changed setup to update */
13754 				if (tiwin != tp->snd_wnd) {
13755 					win_upd_ack = ae->ack;
13756 					win_seq = ae->seq;
13757 					the_win = tiwin;
13758 					rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts, high_seq);
13759 				}
13760 #ifdef TCP_ACCOUNTING
13761 				/* Account for the acks */
13762 				if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13763 					tp->tcp_cnt_counters[CNT_OF_ACKS_IN] += (((ae->ack - high_seq) + segsiz - 1) / segsiz);
13764 				}
13765 				counter_u64_add(tcp_cnt_counters[CNT_OF_ACKS_IN],
13766 						(((ae->ack - high_seq) + segsiz - 1) / segsiz));
13767 #endif
13768 				high_seq = ae->ack;
13769 				if (SEQ_GEQ(high_seq, rack->r_ctl.roundends)) {
13770 					rack->r_ctl.current_round++;
13771 					rack->r_ctl.roundends = tp->snd_max;
13772 					if (CC_ALGO(tp)->newround != NULL) {
13773 						CC_ALGO(tp)->newround(tp->ccv, rack->r_ctl.current_round);
13774 					}
13775 				}
13776 				/* Setup our act_rcv_time */
13777 				if ((ae->flags & TSTMP_LRO) || (ae->flags & TSTMP_HDWR)) {
13778 					ts.tv_sec = ae->timestamp / 1000000000;
13779 					ts.tv_nsec = ae->timestamp % 1000000000;
13780 					rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
13781 					rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
13782 				} else {
13783 					rack->r_ctl.act_rcv_time = *tv;
13784 				}
13785 				rack_process_to_cumack(tp, rack, ae->ack, cts, to);
13786 				if (rack->rc_dsack_round_seen) {
13787 					/* Is the dsack round over? */
13788 					if (SEQ_GEQ(ae->ack, rack->r_ctl.dsack_round_end)) {
13789 						/* Yes it is */
13790 						rack->rc_dsack_round_seen = 0;
13791 						rack_log_dsack_event(rack, 3, __LINE__, 0, 0);
13792 					}
13793 				}
13794 			}
13795 		}
13796 		/* And lets be sure to commit the rtt measurements for this ack */
13797 		tcp_rack_xmit_timer_commit(rack, tp);
13798 #ifdef TCP_ACCOUNTING
13799 		rdstc = get_cyclecount();
13800 		if (rdstc > ts_val) {
13801 			counter_u64_add(tcp_proc_time[ae->ack_val_set] , (rdstc - ts_val));
13802 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13803 				tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val);
13804 				if (ae->ack_val_set == ACK_CUMACK)
13805 					tp->tcp_proc_time[CYC_HANDLE_MAP] += (rdstc - ts_val);
13806 			}
13807 		}
13808 #endif
13809 	}
13810 #ifdef TCP_ACCOUNTING
13811 	ts_val = get_cyclecount();
13812 #endif
13813 	acked_amount = acked = (high_seq - tp->snd_una);
13814 	if (acked) {
13815 		/*
13816 		 * Clear the probe not answered flag
13817 		 * since cum-ack moved forward.
13818 		 */
13819 		rack->probe_not_answered = 0;
13820 		if (rack->sack_attack_disable == 0)
13821 			rack_do_decay(rack);
13822 		if (acked >= segsiz) {
13823 			/*
13824 			 * You only get credit for
13825 			 * MSS and greater (and you get extra
13826 			 * credit for larger cum-ack moves).
13827 			 */
13828 			int ac;
13829 
13830 			ac = acked / segsiz;
13831 			rack->r_ctl.ack_count += ac;
13832 			counter_u64_add(rack_ack_total, ac);
13833 		}
13834 		if (rack->r_ctl.ack_count > 0xfff00000) {
13835 			/*
13836 			 * reduce the number to keep us under
13837 			 * a uint32_t.
13838 			 */
13839 			rack->r_ctl.ack_count /= 2;
13840 			rack->r_ctl.sack_count /= 2;
13841 		}
13842 		if (tp->t_flags & TF_NEEDSYN) {
13843 			/*
13844 			 * T/TCP: Connection was half-synchronized, and our SYN has
13845 			 * been ACK'd (so connection is now fully synchronized).  Go
13846 			 * to non-starred state, increment snd_una for ACK of SYN,
13847 			 * and check if we can do window scaling.
13848 			 */
13849 			tp->t_flags &= ~TF_NEEDSYN;
13850 			tp->snd_una++;
13851 			acked_amount = acked = (high_seq - tp->snd_una);
13852 		}
13853 		if (acked > sbavail(&so->so_snd))
13854 			acked_amount = sbavail(&so->so_snd);
13855 #ifdef NETFLIX_EXP_DETECTION
13856 		/*
13857 		 * We only care on a cum-ack move if we are in a sack-disabled
13858 		 * state. We have already added in to the ack_count, and we never
13859 		 * would disable on a cum-ack move, so we only care to do the
13860 		 * detection if it may "undo" it, i.e. we were in disabled already.
13861 		 */
13862 		if (rack->sack_attack_disable)
13863 			rack_do_detection(tp, rack, acked_amount, segsiz);
13864 #endif
13865 		if (IN_FASTRECOVERY(tp->t_flags) &&
13866 		    (rack->rack_no_prr == 0))
13867 			rack_update_prr(tp, rack, acked_amount, high_seq);
13868 		if (IN_RECOVERY(tp->t_flags)) {
13869 			if (SEQ_LT(high_seq, tp->snd_recover) &&
13870 			    (SEQ_LT(high_seq, tp->snd_max))) {
13871 				tcp_rack_partialack(tp);
13872 			} else {
13873 				rack_post_recovery(tp, high_seq);
13874 				recovery = 1;
13875 			}
13876 		}
13877 		/* Handle the rack-log-ack part (sendmap) */
13878 		if ((sbused(&so->so_snd) == 0) &&
13879 		    (acked > acked_amount) &&
13880 		    (tp->t_state >= TCPS_FIN_WAIT_1) &&
13881 		    (tp->t_flags & TF_SENTFIN)) {
13882 			/*
13883 			 * We must be sure our fin
13884 			 * was sent and acked (we can be
13885 			 * in FIN_WAIT_1 without having
13886 			 * sent the fin).
13887 			 */
13888 			ourfinisacked = 1;
13889 			/*
13890 			 * Lets make sure snd_una is updated
13891 			 * since most likely acked_amount = 0 (it
13892 			 * should be).
13893 			 */
13894 			tp->snd_una = high_seq;
13895 		}
13896 		/* Did we make a RTO error? */
13897 		if ((tp->t_flags & TF_PREVVALID) &&
13898 		    ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
13899 			tp->t_flags &= ~TF_PREVVALID;
13900 			if (tp->t_rxtshift == 1 &&
13901 			    (int)(ticks - tp->t_badrxtwin) < 0)
13902 				rack_cong_signal(tp, CC_RTO_ERR, high_seq);
13903 		}
13904 		/* Handle the data in the socket buffer */
13905 		KMOD_TCPSTAT_ADD(tcps_rcvackpack, 1);
13906 		KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
13907 		if (acked_amount > 0) {
13908 			struct mbuf *mfree;
13909 
13910 			rack_ack_received(tp, rack, high_seq, nsegs, CC_ACK, recovery);
13911 			SOCKBUF_LOCK(&so->so_snd);
13912 			mfree = sbcut_locked(&so->so_snd, acked_amount);
13913 			tp->snd_una = high_seq;
13914 			/* Note we want to hold the sb lock through the sendmap adjust */
13915 			rack_adjust_sendmap(rack, &so->so_snd, tp->snd_una);
13916 			/* Wake up the socket if we have room to write more */
13917 			rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
13918 			sowwakeup_locked(so);
13919 			m_freem(mfree);
13920 		}
13921 		/* update progress */
13922 		tp->t_acktime = ticks;
13923 		rack_log_progress_event(rack, tp, tp->t_acktime,
13924 					PROGRESS_UPDATE, __LINE__);
13925 		/* Clear out shifts and such */
13926 		tp->t_rxtshift = 0;
13927 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
13928 				   rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
13929 		rack->rc_tlp_in_progress = 0;
13930 		rack->r_ctl.rc_tlp_cnt_out = 0;
13931 		/* Send recover and snd_nxt must be dragged along */
13932 		if (SEQ_GT(tp->snd_una, tp->snd_recover))
13933 			tp->snd_recover = tp->snd_una;
13934 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
13935 			tp->snd_nxt = tp->snd_una;
13936 		/*
13937 		 * If the RXT timer is running we want to
13938 		 * stop it, so we can restart a TLP (or new RXT).
13939 		 */
13940 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
13941 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
13942 #ifdef NETFLIX_HTTP_LOGGING
13943 		tcp_http_check_for_comp(rack->rc_tp, high_seq);
13944 #endif
13945 		tp->snd_wl2 = high_seq;
13946 		tp->t_dupacks = 0;
13947 		if (under_pacing &&
13948 		    (rack->use_fixed_rate == 0) &&
13949 		    (rack->in_probe_rtt == 0) &&
13950 		    rack->rc_gp_dyn_mul &&
13951 		    rack->rc_always_pace) {
13952 			/* Check if we are dragging bottom */
13953 			rack_check_bottom_drag(tp, rack, so, acked);
13954 		}
13955 		if (tp->snd_una == tp->snd_max) {
13956 			tp->t_flags &= ~TF_PREVVALID;
13957 			rack->r_ctl.retran_during_recovery = 0;
13958 			rack->r_ctl.dsack_byte_cnt = 0;
13959 			rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
13960 			if (rack->r_ctl.rc_went_idle_time == 0)
13961 				rack->r_ctl.rc_went_idle_time = 1;
13962 			rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
13963 			if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0)
13964 				tp->t_acktime = 0;
13965 			/* Set so we might enter persists... */
13966 			rack->r_wanted_output = 1;
13967 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
13968 			sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
13969 			if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
13970 			    (sbavail(&so->so_snd) == 0) &&
13971 			    (tp->t_flags2 & TF2_DROP_AF_DATA)) {
13972 				/*
13973 				 * The socket was gone and the
13974 				 * peer sent data (not now in the past), time to
13975 				 * reset him.
13976 				 */
13977 				rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
13978 				/* tcp_close will kill the inp pre-log the Reset */
13979 				tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
13980 #ifdef TCP_ACCOUNTING
13981 				rdstc = get_cyclecount();
13982 				if (rdstc > ts_val) {
13983 					counter_u64_add(tcp_proc_time[ACK_CUMACK] , (rdstc - ts_val));
13984 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13985 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
13986 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
13987 					}
13988 				}
13989 #endif
13990 				m_freem(m);
13991 				tp = tcp_close(tp);
13992 				if (tp == NULL) {
13993 #ifdef TCP_ACCOUNTING
13994 					sched_unpin();
13995 #endif
13996 					return (1);
13997 				}
13998 				/*
13999 				 * We would normally do drop-with-reset which would
14000 				 * send back a reset. We can't since we don't have
14001 				 * all the needed bits. Instead lets arrange for
14002 				 * a call to tcp_output(). That way since we
14003 				 * are in the closed state we will generate a reset.
14004 				 *
14005 				 * Note if tcp_accounting is on we don't unpin since
14006 				 * we do that after the goto label.
14007 				 */
14008 				goto send_out_a_rst;
14009 			}
14010 			if ((sbused(&so->so_snd) == 0) &&
14011 			    (tp->t_state >= TCPS_FIN_WAIT_1) &&
14012 			    (tp->t_flags & TF_SENTFIN)) {
14013 				/*
14014 				 * If we can't receive any more data, then closing user can
14015 				 * proceed. Starting the timer is contrary to the
14016 				 * specification, but if we don't get a FIN we'll hang
14017 				 * forever.
14018 				 *
14019 				 */
14020 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
14021 					soisdisconnected(so);
14022 					tcp_timer_activate(tp, TT_2MSL,
14023 							   (tcp_fast_finwait2_recycle ?
14024 							    tcp_finwait2_timeout :
14025 							    TP_MAXIDLE(tp)));
14026 				}
14027 				if (ourfinisacked == 0) {
14028 					/*
14029 					 * We don't change to fin-wait-2 if we have our fin acked
14030 					 * which means we are probably in TCPS_CLOSING.
14031 					 */
14032 					tcp_state_change(tp, TCPS_FIN_WAIT_2);
14033 				}
14034 			}
14035 		}
14036 		/* Wake up the socket if we have room to write more */
14037 		if (sbavail(&so->so_snd)) {
14038 			rack->r_wanted_output = 1;
14039 			if (ctf_progress_timeout_check(tp, true)) {
14040 				rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
14041 							tp, tick, PROGRESS_DROP, __LINE__);
14042 				/*
14043 				 * We cheat here and don't send a RST, we should send one
14044 				 * when the pacer drops the connection.
14045 				 */
14046 #ifdef TCP_ACCOUNTING
14047 				rdstc = get_cyclecount();
14048 				if (rdstc > ts_val) {
14049 					counter_u64_add(tcp_proc_time[ACK_CUMACK] , (rdstc - ts_val));
14050 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
14051 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
14052 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
14053 					}
14054 				}
14055 				sched_unpin();
14056 #endif
14057 				(void)tcp_drop(tp, ETIMEDOUT);
14058 				m_freem(m);
14059 				return (1);
14060 			}
14061 		}
14062 		if (ourfinisacked) {
14063 			switch(tp->t_state) {
14064 			case TCPS_CLOSING:
14065 #ifdef TCP_ACCOUNTING
14066 				rdstc = get_cyclecount();
14067 				if (rdstc > ts_val) {
14068 					counter_u64_add(tcp_proc_time[ACK_CUMACK] ,
14069 							(rdstc - ts_val));
14070 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
14071 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
14072 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
14073 					}
14074 				}
14075 				sched_unpin();
14076 #endif
14077 				tcp_twstart(tp);
14078 				m_freem(m);
14079 				return (1);
14080 				break;
14081 			case TCPS_LAST_ACK:
14082 #ifdef TCP_ACCOUNTING
14083 				rdstc = get_cyclecount();
14084 				if (rdstc > ts_val) {
14085 					counter_u64_add(tcp_proc_time[ACK_CUMACK] ,
14086 							(rdstc - ts_val));
14087 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
14088 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
14089 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
14090 					}
14091 				}
14092 				sched_unpin();
14093 #endif
14094 				tp = tcp_close(tp);
14095 				ctf_do_drop(m, tp);
14096 				return (1);
14097 				break;
14098 			case TCPS_FIN_WAIT_1:
14099 #ifdef TCP_ACCOUNTING
14100 				rdstc = get_cyclecount();
14101 				if (rdstc > ts_val) {
14102 					counter_u64_add(tcp_proc_time[ACK_CUMACK] ,
14103 							(rdstc - ts_val));
14104 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
14105 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
14106 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
14107 					}
14108 				}
14109 #endif
14110 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
14111 					soisdisconnected(so);
14112 					tcp_timer_activate(tp, TT_2MSL,
14113 							   (tcp_fast_finwait2_recycle ?
14114 							    tcp_finwait2_timeout :
14115 							    TP_MAXIDLE(tp)));
14116 				}
14117 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
14118 				break;
14119 			default:
14120 				break;
14121 			}
14122 		}
14123 		if (rack->r_fast_output) {
14124 			/*
14125 			 * We re doing fast output.. can we expand that?
14126 			 */
14127 			rack_gain_for_fastoutput(rack, tp, so, acked_amount);
14128 		}
14129 #ifdef TCP_ACCOUNTING
14130 		rdstc = get_cyclecount();
14131 		if (rdstc > ts_val) {
14132 			counter_u64_add(tcp_proc_time[ACK_CUMACK] , (rdstc - ts_val));
14133 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
14134 				tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
14135 				tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
14136 			}
14137 		}
14138 
14139 	} else if (win_up_req) {
14140 		rdstc = get_cyclecount();
14141 		if (rdstc > ts_val) {
14142 			counter_u64_add(tcp_proc_time[ACK_RWND] , (rdstc - ts_val));
14143 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
14144 				tp->tcp_proc_time[ACK_RWND] += (rdstc - ts_val);
14145 			}
14146 		}
14147 #endif
14148 	}
14149 	/* Now is there a next packet, if so we are done */
14150 	m_freem(m);
14151 	did_out = 0;
14152 	if (nxt_pkt) {
14153 #ifdef TCP_ACCOUNTING
14154 		sched_unpin();
14155 #endif
14156 		rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 5, nsegs);
14157 		return (0);
14158 	}
14159 	rack_handle_might_revert(tp, rack);
14160 	ctf_calc_rwin(so, tp);
14161 	if ((rack->r_wanted_output != 0) || (rack->r_fast_output != 0)) {
14162 	send_out_a_rst:
14163 		if (tcp_output(tp) < 0) {
14164 #ifdef TCP_ACCOUNTING
14165 			sched_unpin();
14166 #endif
14167 			return (1);
14168 		}
14169 		did_out = 1;
14170 	}
14171 	rack_free_trim(rack);
14172 #ifdef TCP_ACCOUNTING
14173 	sched_unpin();
14174 #endif
14175 	rack_timer_audit(tp, rack, &so->so_snd);
14176 	rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 6, nsegs);
14177 	return (0);
14178 }
14179 
14180 
14181 static int
14182 rack_do_segment_nounlock(struct mbuf *m, struct tcphdr *th, struct socket *so,
14183     struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos,
14184     int32_t nxt_pkt, struct timeval *tv)
14185 {
14186 #ifdef TCP_ACCOUNTING
14187 	uint64_t ts_val;
14188 #endif
14189 	int32_t thflags, retval, did_out = 0;
14190 	int32_t way_out = 0;
14191 	/*
14192 	 * cts - is the current time from tv (caller gets ts) in microseconds.
14193 	 * ms_cts - is the current time from tv in milliseconds.
14194 	 * us_cts - is the time that LRO or hardware actually got the packet in microseconds.
14195 	 */
14196 	uint32_t cts, us_cts, ms_cts;
14197 	uint32_t tiwin;
14198 	struct timespec ts;
14199 	struct tcpopt to;
14200 	struct tcp_rack *rack;
14201 	struct rack_sendmap *rsm;
14202 	int32_t prev_state = 0;
14203 #ifdef TCP_ACCOUNTING
14204 	int ack_val_set = 0xf;
14205 #endif
14206 	int nsegs;
14207 	/*
14208 	 * tv passed from common code is from either M_TSTMP_LRO or
14209 	 * tcp_get_usecs() if no LRO m_pkthdr timestamp is present.
14210 	 */
14211 	rack = (struct tcp_rack *)tp->t_fb_ptr;
14212 	if (m->m_flags & M_ACKCMP) {
14213 		return (rack_do_compressed_ack_processing(tp, so, m, nxt_pkt, tv));
14214 	}
14215 	if (m->m_flags & M_ACKCMP) {
14216 		panic("Impossible reach m has ackcmp? m:%p tp:%p", m, tp);
14217 	}
14218 	cts = tcp_tv_to_usectick(tv);
14219 	ms_cts =  tcp_tv_to_mssectick(tv);
14220 	nsegs = m->m_pkthdr.lro_nsegs;
14221 	counter_u64_add(rack_proc_non_comp_ack, 1);
14222 	thflags = tcp_get_flags(th);
14223 #ifdef TCP_ACCOUNTING
14224 	sched_pin();
14225 	if (thflags & TH_ACK)
14226 		ts_val = get_cyclecount();
14227 #endif
14228 	if ((m->m_flags & M_TSTMP) ||
14229 	    (m->m_flags & M_TSTMP_LRO)) {
14230 		mbuf_tstmp2timespec(m, &ts);
14231 		rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
14232 		rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
14233 	} else
14234 		rack->r_ctl.act_rcv_time = *tv;
14235 	kern_prefetch(rack, &prev_state);
14236 	prev_state = 0;
14237 	/*
14238 	 * Unscale the window into a 32-bit value. For the SYN_SENT state
14239 	 * the scale is zero.
14240 	 */
14241 	tiwin = th->th_win << tp->snd_scale;
14242 #ifdef TCP_ACCOUNTING
14243 	if (thflags & TH_ACK) {
14244 		/*
14245 		 * We have a tradeoff here. We can either do what we are
14246 		 * doing i.e. pinning to this CPU and then doing the accounting
14247 		 * <or> we could do a critical enter, setup the rdtsc and cpu
14248 		 * as in below, and then validate we are on the same CPU on
14249 		 * exit. I have choosen to not do the critical enter since
14250 		 * that often will gain you a context switch, and instead lock
14251 		 * us (line above this if) to the same CPU with sched_pin(). This
14252 		 * means we may be context switched out for a higher priority
14253 		 * interupt but we won't be moved to another CPU.
14254 		 *
14255 		 * If this occurs (which it won't very often since we most likely
14256 		 * are running this code in interupt context and only a higher
14257 		 * priority will bump us ... clock?) we will falsely add in
14258 		 * to the time the interupt processing time plus the ack processing
14259 		 * time. This is ok since its a rare event.
14260 		 */
14261 		ack_val_set = tcp_do_ack_accounting(tp, th, &to, tiwin,
14262 						    ctf_fixed_maxseg(tp));
14263 	}
14264 #endif
14265 	/*
14266 	 * Parse options on any incoming segment.
14267 	 */
14268 	memset(&to, 0, sizeof(to));
14269 	tcp_dooptions(&to, (u_char *)(th + 1),
14270 	    (th->th_off << 2) - sizeof(struct tcphdr),
14271 	    (thflags & TH_SYN) ? TO_SYN : 0);
14272 	NET_EPOCH_ASSERT();
14273 	INP_WLOCK_ASSERT(tp->t_inpcb);
14274 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
14275 	    __func__));
14276 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
14277 	    __func__));
14278 	if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
14279 	    (tp->t_flags & TF_GPUTINPROG)) {
14280 		/*
14281 		 * We have a goodput in progress
14282 		 * and we have entered a late state.
14283 		 * Do we have enough data in the sb
14284 		 * to handle the GPUT request?
14285 		 */
14286 		uint32_t bytes;
14287 
14288 		bytes = tp->gput_ack - tp->gput_seq;
14289 		if (SEQ_GT(tp->gput_seq, tp->snd_una))
14290 			bytes += tp->gput_seq - tp->snd_una;
14291 		if (bytes > sbavail(&tp->t_inpcb->inp_socket->so_snd)) {
14292 			/*
14293 			 * There are not enough bytes in the socket
14294 			 * buffer that have been sent to cover this
14295 			 * measurement. Cancel it.
14296 			 */
14297 			rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
14298 						   rack->r_ctl.rc_gp_srtt /*flex1*/,
14299 						   tp->gput_seq,
14300 						   0, 0, 18, __LINE__, NULL, 0);
14301 			tp->t_flags &= ~TF_GPUTINPROG;
14302 		}
14303 	}
14304 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
14305 		union tcp_log_stackspecific log;
14306 		struct timeval ltv;
14307 #ifdef NETFLIX_HTTP_LOGGING
14308 		struct http_sendfile_track *http_req;
14309 
14310 		if (SEQ_GT(th->th_ack, tp->snd_una)) {
14311 			http_req = tcp_http_find_req_for_seq(tp, (th->th_ack-1));
14312 		} else {
14313 			http_req = tcp_http_find_req_for_seq(tp, th->th_ack);
14314 		}
14315 #endif
14316 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
14317 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
14318 		if (rack->rack_no_prr == 0)
14319 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
14320 		else
14321 			log.u_bbr.flex1 = 0;
14322 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
14323 		log.u_bbr.use_lt_bw <<= 1;
14324 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
14325 		log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced;
14326 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
14327 		log.u_bbr.pkts_out = rack->rc_tp->t_maxseg;
14328 		log.u_bbr.flex3 = m->m_flags;
14329 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
14330 		log.u_bbr.lost = thflags;
14331 		log.u_bbr.pacing_gain = 0x1;
14332 #ifdef TCP_ACCOUNTING
14333 		log.u_bbr.cwnd_gain = ack_val_set;
14334 #endif
14335 		log.u_bbr.flex7 = 2;
14336 		if (m->m_flags & M_TSTMP) {
14337 			/* Record the hardware timestamp if present */
14338 			mbuf_tstmp2timespec(m, &ts);
14339 			ltv.tv_sec = ts.tv_sec;
14340 			ltv.tv_usec = ts.tv_nsec / 1000;
14341 			log.u_bbr.lt_epoch = tcp_tv_to_usectick(&ltv);
14342 		} else if (m->m_flags & M_TSTMP_LRO) {
14343 			/* Record the LRO the arrival timestamp */
14344 			mbuf_tstmp2timespec(m, &ts);
14345 			ltv.tv_sec = ts.tv_sec;
14346 			ltv.tv_usec = ts.tv_nsec / 1000;
14347 			log.u_bbr.flex5 = tcp_tv_to_usectick(&ltv);
14348 		}
14349 		log.u_bbr.timeStamp = tcp_get_usecs(&ltv);
14350 		/* Log the rcv time */
14351 		log.u_bbr.delRate = m->m_pkthdr.rcv_tstmp;
14352 #ifdef NETFLIX_HTTP_LOGGING
14353 		log.u_bbr.applimited = tp->t_http_closed;
14354 		log.u_bbr.applimited <<= 8;
14355 		log.u_bbr.applimited |= tp->t_http_open;
14356 		log.u_bbr.applimited <<= 8;
14357 		log.u_bbr.applimited |= tp->t_http_req;
14358 		if (http_req) {
14359 			/* Copy out any client req info */
14360 			/* seconds */
14361 			log.u_bbr.pkt_epoch = (http_req->localtime / HPTS_USEC_IN_SEC);
14362 			/* useconds */
14363 			log.u_bbr.delivered = (http_req->localtime % HPTS_USEC_IN_SEC);
14364 			log.u_bbr.rttProp = http_req->timestamp;
14365 			log.u_bbr.cur_del_rate = http_req->start;
14366 			if (http_req->flags & TCP_HTTP_TRACK_FLG_OPEN) {
14367 				log.u_bbr.flex8 |= 1;
14368 			} else {
14369 				log.u_bbr.flex8 |= 2;
14370 				log.u_bbr.bw_inuse = http_req->end;
14371 			}
14372 			log.u_bbr.flex6 = http_req->start_seq;
14373 			if (http_req->flags & TCP_HTTP_TRACK_FLG_COMP) {
14374 				log.u_bbr.flex8 |= 4;
14375 				log.u_bbr.epoch = http_req->end_seq;
14376 			}
14377 		}
14378 #endif
14379 		TCP_LOG_EVENTP(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
14380 		    tlen, &log, true, &ltv);
14381 	}
14382 	if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
14383 		way_out = 4;
14384 		retval = 0;
14385 		m_freem(m);
14386 		goto done_with_input;
14387 	}
14388 	/*
14389 	 * If a segment with the ACK-bit set arrives in the SYN-SENT state
14390 	 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9.
14391 	 */
14392 	if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
14393 	    (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
14394 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
14395 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
14396 #ifdef TCP_ACCOUNTING
14397 		sched_unpin();
14398 #endif
14399 		return (1);
14400 	}
14401 	/*
14402 	 * If timestamps were negotiated during SYN/ACK and a
14403 	 * segment without a timestamp is received, silently drop
14404 	 * the segment, unless it is a RST segment or missing timestamps are
14405 	 * tolerated.
14406 	 * See section 3.2 of RFC 7323.
14407 	 */
14408 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) &&
14409 	    ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) {
14410 		way_out = 5;
14411 		retval = 0;
14412 		m_freem(m);
14413 		goto done_with_input;
14414 	}
14415 
14416 	/*
14417 	 * Segment received on connection. Reset idle time and keep-alive
14418 	 * timer. XXX: This should be done after segment validation to
14419 	 * ignore broken/spoofed segs.
14420 	 */
14421 	if  (tp->t_idle_reduce &&
14422 	     (tp->snd_max == tp->snd_una) &&
14423 	     ((ticks - tp->t_rcvtime) >= tp->t_rxtcur)) {
14424 		counter_u64_add(rack_input_idle_reduces, 1);
14425 		rack_cc_after_idle(rack, tp);
14426 	}
14427 	tp->t_rcvtime = ticks;
14428 #ifdef STATS
14429 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
14430 #endif
14431 	if (tiwin > rack->r_ctl.rc_high_rwnd)
14432 		rack->r_ctl.rc_high_rwnd = tiwin;
14433 	/*
14434 	 * TCP ECN processing. XXXJTL: If we ever use ECN, we need to move
14435 	 * this to occur after we've validated the segment.
14436 	 */
14437 	if (tcp_ecn_input_segment(tp, thflags, iptos))
14438 		rack_cong_signal(tp, CC_ECN, th->th_ack);
14439 
14440 	/*
14441 	 * If echoed timestamp is later than the current time, fall back to
14442 	 * non RFC1323 RTT calculation.  Normalize timestamp if syncookies
14443 	 * were used when this connection was established.
14444 	 */
14445 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
14446 		to.to_tsecr -= tp->ts_offset;
14447 		if (TSTMP_GT(to.to_tsecr, ms_cts))
14448 			to.to_tsecr = 0;
14449 	}
14450 
14451 	/*
14452 	 * If its the first time in we need to take care of options and
14453 	 * verify we can do SACK for rack!
14454 	 */
14455 	if (rack->r_state == 0) {
14456 		/* Should be init'd by rack_init() */
14457 		KASSERT(rack->rc_inp != NULL,
14458 		    ("%s: rack->rc_inp unexpectedly NULL", __func__));
14459 		if (rack->rc_inp == NULL) {
14460 			rack->rc_inp = tp->t_inpcb;
14461 		}
14462 
14463 		/*
14464 		 * Process options only when we get SYN/ACK back. The SYN
14465 		 * case for incoming connections is handled in tcp_syncache.
14466 		 * According to RFC1323 the window field in a SYN (i.e., a
14467 		 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX
14468 		 * this is traditional behavior, may need to be cleaned up.
14469 		 */
14470 		if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
14471 			/* Handle parallel SYN for ECN */
14472 			tcp_ecn_input_parallel_syn(tp, thflags, iptos);
14473 			if ((to.to_flags & TOF_SCALE) &&
14474 			    (tp->t_flags & TF_REQ_SCALE)) {
14475 				tp->t_flags |= TF_RCVD_SCALE;
14476 				tp->snd_scale = to.to_wscale;
14477 			} else
14478 				tp->t_flags &= ~TF_REQ_SCALE;
14479 			/*
14480 			 * Initial send window.  It will be updated with the
14481 			 * next incoming segment to the scaled value.
14482 			 */
14483 			tp->snd_wnd = th->th_win;
14484 			rack_validate_fo_sendwin_up(tp, rack);
14485 			if ((to.to_flags & TOF_TS) &&
14486 			    (tp->t_flags & TF_REQ_TSTMP)) {
14487 				tp->t_flags |= TF_RCVD_TSTMP;
14488 				tp->ts_recent = to.to_tsval;
14489 				tp->ts_recent_age = cts;
14490 			} else
14491 				tp->t_flags &= ~TF_REQ_TSTMP;
14492 			if (to.to_flags & TOF_MSS) {
14493 				tcp_mss(tp, to.to_mss);
14494 			}
14495 			if ((tp->t_flags & TF_SACK_PERMIT) &&
14496 			    (to.to_flags & TOF_SACKPERM) == 0)
14497 				tp->t_flags &= ~TF_SACK_PERMIT;
14498 			if (IS_FASTOPEN(tp->t_flags)) {
14499 				if (to.to_flags & TOF_FASTOPEN) {
14500 					uint16_t mss;
14501 
14502 					if (to.to_flags & TOF_MSS)
14503 						mss = to.to_mss;
14504 					else
14505 						if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
14506 							mss = TCP6_MSS;
14507 						else
14508 							mss = TCP_MSS;
14509 					tcp_fastopen_update_cache(tp, mss,
14510 					    to.to_tfo_len, to.to_tfo_cookie);
14511 				} else
14512 					tcp_fastopen_disable_path(tp);
14513 			}
14514 		}
14515 		/*
14516 		 * At this point we are at the initial call. Here we decide
14517 		 * if we are doing RACK or not. We do this by seeing if
14518 		 * TF_SACK_PERMIT is set and the sack-not-required is clear.
14519 		 * The code now does do dup-ack counting so if you don't
14520 		 * switch back you won't get rack & TLP, but you will still
14521 		 * get this stack.
14522 		 */
14523 
14524 		if ((rack_sack_not_required == 0) &&
14525 		    ((tp->t_flags & TF_SACK_PERMIT) == 0)) {
14526 			tcp_switch_back_to_default(tp);
14527 			(*tp->t_fb->tfb_tcp_do_segment) (m, th, so, tp, drop_hdrlen,
14528 			    tlen, iptos);
14529 #ifdef TCP_ACCOUNTING
14530 			sched_unpin();
14531 #endif
14532 			return (1);
14533 		}
14534 		tcp_set_hpts(tp->t_inpcb);
14535 		sack_filter_clear(&rack->r_ctl.rack_sf, th->th_ack);
14536 	}
14537 	if (thflags & TH_FIN)
14538 		tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
14539 	us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
14540 	if ((rack->rc_gp_dyn_mul) &&
14541 	    (rack->use_fixed_rate == 0) &&
14542 	    (rack->rc_always_pace)) {
14543 		/* Check in on probertt */
14544 		rack_check_probe_rtt(rack, us_cts);
14545 	}
14546 	rack_clear_rate_sample(rack);
14547 	if ((rack->forced_ack) &&
14548 	    ((tcp_get_flags(th) & TH_RST) == 0)) {
14549 		rack_handle_probe_response(rack, tiwin, us_cts);
14550 	}
14551 	/*
14552 	 * This is the one exception case where we set the rack state
14553 	 * always. All other times (timers etc) we must have a rack-state
14554 	 * set (so we assure we have done the checks above for SACK).
14555 	 */
14556 	rack->r_ctl.rc_rcvtime = cts;
14557 	if (rack->r_state != tp->t_state)
14558 		rack_set_state(tp, rack);
14559 	if (SEQ_GT(th->th_ack, tp->snd_una) &&
14560 	    (rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree)) != NULL)
14561 		kern_prefetch(rsm, &prev_state);
14562 	prev_state = rack->r_state;
14563 	retval = (*rack->r_substate) (m, th, so,
14564 	    tp, &to, drop_hdrlen,
14565 	    tlen, tiwin, thflags, nxt_pkt, iptos);
14566 #ifdef INVARIANTS
14567 	if ((retval == 0) &&
14568 	    (tp->t_inpcb == NULL)) {
14569 		panic("retval:%d tp:%p t_inpcb:NULL state:%d",
14570 		    retval, tp, prev_state);
14571 	}
14572 #endif
14573 	if (retval == 0) {
14574 		/*
14575 		 * If retval is 1 the tcb is unlocked and most likely the tp
14576 		 * is gone.
14577 		 */
14578 		INP_WLOCK_ASSERT(tp->t_inpcb);
14579 		if ((rack->rc_gp_dyn_mul) &&
14580 		    (rack->rc_always_pace) &&
14581 		    (rack->use_fixed_rate == 0) &&
14582 		    rack->in_probe_rtt &&
14583 		    (rack->r_ctl.rc_time_probertt_starts == 0)) {
14584 			/*
14585 			 * If we are going for target, lets recheck before
14586 			 * we output.
14587 			 */
14588 			rack_check_probe_rtt(rack, us_cts);
14589 		}
14590 		if (rack->set_pacing_done_a_iw == 0) {
14591 			/* How much has been acked? */
14592 			if ((tp->snd_una - tp->iss) > (ctf_fixed_maxseg(tp) * 10)) {
14593 				/* We have enough to set in the pacing segment size */
14594 				rack->set_pacing_done_a_iw = 1;
14595 				rack_set_pace_segments(tp, rack, __LINE__, NULL);
14596 			}
14597 		}
14598 		tcp_rack_xmit_timer_commit(rack, tp);
14599 #ifdef TCP_ACCOUNTING
14600 		/*
14601 		 * If we set the ack_val_se to what ack processing we are doing
14602 		 * we also want to track how many cycles we burned. Note
14603 		 * the bits after tcp_output we let be "free". This is because
14604 		 * we are also tracking the tcp_output times as well. Note the
14605 		 * use of 0xf here since we only have 11 counter (0 - 0xa) and
14606 		 * 0xf cannot be returned and is what we initialize it too to
14607 		 * indicate we are not doing the tabulations.
14608 		 */
14609 		if (ack_val_set != 0xf) {
14610 			uint64_t crtsc;
14611 
14612 			crtsc = get_cyclecount();
14613 			counter_u64_add(tcp_proc_time[ack_val_set] , (crtsc - ts_val));
14614 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
14615 				tp->tcp_proc_time[ack_val_set] += (crtsc - ts_val);
14616 			}
14617 		}
14618 #endif
14619 		if (nxt_pkt == 0) {
14620 			if ((rack->r_wanted_output != 0) || (rack->r_fast_output != 0)) {
14621 do_output_now:
14622 				if (tcp_output(tp) < 0)
14623 					return (1);
14624 				did_out = 1;
14625 			}
14626 			rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
14627 			rack_free_trim(rack);
14628 		}
14629 		/* Update any rounds needed */
14630 		if (SEQ_GEQ(tp->snd_una, rack->r_ctl.roundends)) {
14631 			rack->r_ctl.current_round++;
14632 			rack->r_ctl.roundends = tp->snd_max;
14633 			if (CC_ALGO(tp)->newround != NULL) {
14634 				CC_ALGO(tp)->newround(tp->ccv, rack->r_ctl.current_round);
14635 			}
14636 		}
14637 		if ((nxt_pkt == 0) &&
14638 		    ((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) &&
14639 		    (SEQ_GT(tp->snd_max, tp->snd_una) ||
14640 		     (tp->t_flags & TF_DELACK) ||
14641 		     ((V_tcp_always_keepalive || rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
14642 		      (tp->t_state <= TCPS_CLOSING)))) {
14643 			/* We could not send (probably in the hpts but stopped the timer earlier)? */
14644 			if ((tp->snd_max == tp->snd_una) &&
14645 			    ((tp->t_flags & TF_DELACK) == 0) &&
14646 			    (tcp_in_hpts(rack->rc_inp)) &&
14647 			    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
14648 				/* keep alive not needed if we are hptsi output yet */
14649 				;
14650 			} else {
14651 				int late = 0;
14652 				if (tcp_in_hpts(rack->rc_inp)) {
14653 					if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
14654 						us_cts = tcp_get_usecs(NULL);
14655 						if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
14656 							rack->r_early = 1;
14657 							rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
14658 						} else
14659 							late = 1;
14660 						rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
14661 					}
14662 					tcp_hpts_remove(tp->t_inpcb);
14663 				}
14664 				if (late && (did_out == 0)) {
14665 					/*
14666 					 * We are late in the sending
14667 					 * and we did not call the output
14668 					 * (this probably should not happen).
14669 					 */
14670 					goto do_output_now;
14671 				}
14672 				rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
14673 			}
14674 			way_out = 1;
14675 		} else if (nxt_pkt == 0) {
14676 			/* Do we have the correct timer running? */
14677 			rack_timer_audit(tp, rack, &so->so_snd);
14678 			way_out = 2;
14679 		}
14680 	done_with_input:
14681 		rack_log_doseg_done(rack, cts, nxt_pkt, did_out, way_out, max(1, nsegs));
14682 		if (did_out)
14683 			rack->r_wanted_output = 0;
14684 #ifdef INVARIANTS
14685 		if (tp->t_inpcb == NULL) {
14686 			panic("OP:%d retval:%d tp:%p t_inpcb:NULL state:%d",
14687 			      did_out,
14688 			      retval, tp, prev_state);
14689 		}
14690 #endif
14691 #ifdef TCP_ACCOUNTING
14692 	} else {
14693 		/*
14694 		 * Track the time (see above).
14695 		 */
14696 		if (ack_val_set != 0xf) {
14697 			uint64_t crtsc;
14698 
14699 			crtsc = get_cyclecount();
14700 			counter_u64_add(tcp_proc_time[ack_val_set] , (crtsc - ts_val));
14701 			/*
14702 			 * Note we *DO NOT* increment the per-tcb counters since
14703 			 * in the else the TP may be gone!!
14704 			 */
14705 		}
14706 #endif
14707 	}
14708 #ifdef TCP_ACCOUNTING
14709 	sched_unpin();
14710 #endif
14711 	return (retval);
14712 }
14713 
14714 void
14715 rack_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
14716     struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos)
14717 {
14718 	struct timeval tv;
14719 
14720 	/* First lets see if we have old packets */
14721 	if (tp->t_in_pkt) {
14722 		if (ctf_do_queued_segments(so, tp, 1)) {
14723 			m_freem(m);
14724 			return;
14725 		}
14726 	}
14727 	if (m->m_flags & M_TSTMP_LRO) {
14728 		tv.tv_sec = m->m_pkthdr.rcv_tstmp /1000000000;
14729 		tv.tv_usec = (m->m_pkthdr.rcv_tstmp % 1000000000)/1000;
14730 	} else {
14731 		/* Should not be should we kassert instead? */
14732 		tcp_get_usecs(&tv);
14733 	}
14734 	if (rack_do_segment_nounlock(m, th, so, tp,
14735 				     drop_hdrlen, tlen, iptos, 0, &tv) == 0) {
14736 		INP_WUNLOCK(tp->t_inpcb);
14737 	}
14738 }
14739 
14740 struct rack_sendmap *
14741 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tsused)
14742 {
14743 	struct rack_sendmap *rsm = NULL;
14744 	int32_t idx;
14745 	uint32_t srtt = 0, thresh = 0, ts_low = 0;
14746 
14747 	/* Return the next guy to be re-transmitted */
14748 	if (RB_EMPTY(&rack->r_ctl.rc_mtree)) {
14749 		return (NULL);
14750 	}
14751 	if (tp->t_flags & TF_SENTFIN) {
14752 		/* retran the end FIN? */
14753 		return (NULL);
14754 	}
14755 	/* ok lets look at this one */
14756 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
14757 	if (rsm && ((rsm->r_flags & RACK_ACKED) == 0)) {
14758 		goto check_it;
14759 	}
14760 	rsm = rack_find_lowest_rsm(rack);
14761 	if (rsm == NULL) {
14762 		return (NULL);
14763 	}
14764 check_it:
14765 	if (((rack->rc_tp->t_flags & TF_SACK_PERMIT) == 0) &&
14766 	    (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
14767 		/*
14768 		 * No sack so we automatically do the 3 strikes and
14769 		 * retransmit (no rack timer would be started).
14770 		 */
14771 
14772 		return (rsm);
14773 	}
14774 	if (rsm->r_flags & RACK_ACKED) {
14775 		return (NULL);
14776 	}
14777 	if (((rsm->r_flags & RACK_SACK_PASSED) == 0) &&
14778 	    (rsm->r_dupack < DUP_ACK_THRESHOLD)) {
14779 		/* Its not yet ready */
14780 		return (NULL);
14781 	}
14782 	srtt = rack_grab_rtt(tp, rack);
14783 	idx = rsm->r_rtr_cnt - 1;
14784 	ts_low = (uint32_t)rsm->r_tim_lastsent[idx];
14785 	thresh = rack_calc_thresh_rack(rack, srtt, tsused);
14786 	if ((tsused == ts_low) ||
14787 	    (TSTMP_LT(tsused, ts_low))) {
14788 		/* No time since sending */
14789 		return (NULL);
14790 	}
14791 	if ((tsused - ts_low) < thresh) {
14792 		/* It has not been long enough yet */
14793 		return (NULL);
14794 	}
14795 	if ((rsm->r_dupack >= DUP_ACK_THRESHOLD) ||
14796 	    ((rsm->r_flags & RACK_SACK_PASSED) &&
14797 	     (rack->sack_attack_disable == 0))) {
14798 		/*
14799 		 * We have passed the dup-ack threshold <or>
14800 		 * a SACK has indicated this is missing.
14801 		 * Note that if you are a declared attacker
14802 		 * it is only the dup-ack threshold that
14803 		 * will cause retransmits.
14804 		 */
14805 		/* log retransmit reason */
14806 		rack_log_retran_reason(rack, rsm, (tsused - ts_low), thresh, 1);
14807 		rack->r_fast_output = 0;
14808 		return (rsm);
14809 	}
14810 	return (NULL);
14811 }
14812 
14813 static void
14814 rack_log_pacing_delay_calc(struct tcp_rack *rack, uint32_t len, uint32_t slot,
14815 			   uint64_t bw_est, uint64_t bw, uint64_t len_time, int method,
14816 			   int line, struct rack_sendmap *rsm, uint8_t quality)
14817 {
14818 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
14819 		union tcp_log_stackspecific log;
14820 		struct timeval tv;
14821 
14822 		memset(&log, 0, sizeof(log));
14823 		log.u_bbr.flex1 = slot;
14824 		log.u_bbr.flex2 = len;
14825 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_min_segs;
14826 		log.u_bbr.flex4 = rack->r_ctl.rc_pace_max_segs;
14827 		log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ss;
14828 		log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_ca;
14829 		log.u_bbr.use_lt_bw = rack->rc_ack_can_sendout_data;
14830 		log.u_bbr.use_lt_bw <<= 1;
14831 		log.u_bbr.use_lt_bw |= rack->r_late;
14832 		log.u_bbr.use_lt_bw <<= 1;
14833 		log.u_bbr.use_lt_bw |= rack->r_early;
14834 		log.u_bbr.use_lt_bw <<= 1;
14835 		log.u_bbr.use_lt_bw |= rack->app_limited_needs_set;
14836 		log.u_bbr.use_lt_bw <<= 1;
14837 		log.u_bbr.use_lt_bw |= rack->rc_gp_filled;
14838 		log.u_bbr.use_lt_bw <<= 1;
14839 		log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt;
14840 		log.u_bbr.use_lt_bw <<= 1;
14841 		log.u_bbr.use_lt_bw |= rack->in_probe_rtt;
14842 		log.u_bbr.use_lt_bw <<= 1;
14843 		log.u_bbr.use_lt_bw |= rack->gp_ready;
14844 		log.u_bbr.pkt_epoch = line;
14845 		log.u_bbr.epoch = rack->r_ctl.rc_agg_delayed;
14846 		log.u_bbr.lt_epoch = rack->r_ctl.rc_agg_early;
14847 		log.u_bbr.applimited = rack->r_ctl.rack_per_of_gp_rec;
14848 		log.u_bbr.bw_inuse = bw_est;
14849 		log.u_bbr.delRate = bw;
14850 		if (rack->r_ctl.gp_bw == 0)
14851 			log.u_bbr.cur_del_rate = 0;
14852 		else
14853 			log.u_bbr.cur_del_rate = rack_get_bw(rack);
14854 		log.u_bbr.rttProp = len_time;
14855 		log.u_bbr.pkts_out = rack->r_ctl.rc_rack_min_rtt;
14856 		log.u_bbr.lost = rack->r_ctl.rc_probertt_sndmax_atexit;
14857 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm);
14858 		if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh) {
14859 			/* We are in slow start */
14860 			log.u_bbr.flex7 = 1;
14861 		} else {
14862 			/* we are on congestion avoidance */
14863 			log.u_bbr.flex7 = 0;
14864 		}
14865 		log.u_bbr.flex8 = method;
14866 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
14867 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
14868 		log.u_bbr.cwnd_gain = rack->rc_gp_saw_rec;
14869 		log.u_bbr.cwnd_gain <<= 1;
14870 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss;
14871 		log.u_bbr.cwnd_gain <<= 1;
14872 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca;
14873 		log.u_bbr.bbr_substate = quality;
14874 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
14875 		    &rack->rc_inp->inp_socket->so_rcv,
14876 		    &rack->rc_inp->inp_socket->so_snd,
14877 		    BBR_LOG_HPTSI_CALC, 0,
14878 		    0, &log, false, &tv);
14879 	}
14880 }
14881 
14882 static uint32_t
14883 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss)
14884 {
14885 	uint32_t new_tso, user_max;
14886 
14887 	user_max = rack->rc_user_set_max_segs * mss;
14888 	if (rack->rc_force_max_seg) {
14889 		return (user_max);
14890 	}
14891 	if (rack->use_fixed_rate &&
14892 	    ((rack->r_ctl.crte == NULL) ||
14893 	     (bw != rack->r_ctl.crte->rate))) {
14894 		/* Use the user mss since we are not exactly matched */
14895 		return (user_max);
14896 	}
14897 	new_tso = tcp_get_pacing_burst_size(rack->rc_tp, bw, mss, rack_pace_one_seg, rack->r_ctl.crte, NULL);
14898 	if (new_tso > user_max)
14899 		new_tso = user_max;
14900 	return (new_tso);
14901 }
14902 
14903 static int32_t
14904 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)
14905 {
14906 	uint64_t lentim, fill_bw;
14907 
14908 	/* Lets first see if we are full, if so continue with normal rate */
14909 	rack->r_via_fill_cw = 0;
14910 	if (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.cwnd_to_use)
14911 		return (slot);
14912 	if ((ctf_outstanding(rack->rc_tp) + (segsiz-1)) > rack->rc_tp->snd_wnd)
14913 		return (slot);
14914 	if (rack->r_ctl.rc_last_us_rtt == 0)
14915 		return (slot);
14916 	if (rack->rc_pace_fill_if_rttin_range &&
14917 	    (rack->r_ctl.rc_last_us_rtt >=
14918 	     (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack->rtt_limit_mul))) {
14919 		/* The rtt is huge, N * smallest, lets not fill */
14920 		return (slot);
14921 	}
14922 	/*
14923 	 * first lets calculate the b/w based on the last us-rtt
14924 	 * and the sndwnd.
14925 	 */
14926 	fill_bw = rack->r_ctl.cwnd_to_use;
14927 	/* Take the rwnd if its smaller */
14928 	if (fill_bw > rack->rc_tp->snd_wnd)
14929 		fill_bw = rack->rc_tp->snd_wnd;
14930 	if (rack->r_fill_less_agg) {
14931 		/*
14932 		 * Now take away the inflight (this will reduce our
14933 		 * aggressiveness and yeah, if we get that much out in 1RTT
14934 		 * we will have had acks come back and still be behind).
14935 		 */
14936 		fill_bw -= ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
14937 	}
14938 	/* Now lets make it into a b/w */
14939 	fill_bw *= (uint64_t)HPTS_USEC_IN_SEC;
14940 	fill_bw /= (uint64_t)rack->r_ctl.rc_last_us_rtt;
14941 	/* We are below the min b/w */
14942 	if (non_paced)
14943 		*rate_wanted = fill_bw;
14944 	if ((fill_bw < RACK_MIN_BW) || (fill_bw < *rate_wanted))
14945 		return (slot);
14946 	if (rack->r_ctl.bw_rate_cap && (fill_bw > rack->r_ctl.bw_rate_cap))
14947 		fill_bw = rack->r_ctl.bw_rate_cap;
14948 	rack->r_via_fill_cw = 1;
14949 	if (rack->r_rack_hw_rate_caps &&
14950 	    (rack->r_ctl.crte != NULL)) {
14951 		uint64_t high_rate;
14952 
14953 		high_rate = tcp_hw_highest_rate(rack->r_ctl.crte);
14954 		if (fill_bw > high_rate) {
14955 			/* We are capping bw at the highest rate table entry */
14956 			if (*rate_wanted > high_rate) {
14957 				/* The original rate was also capped */
14958 				rack->r_via_fill_cw = 0;
14959 			}
14960 			rack_log_hdwr_pacing(rack,
14961 					     fill_bw, high_rate, __LINE__,
14962 					     0, 3);
14963 			fill_bw = high_rate;
14964 			if (capped)
14965 				*capped = 1;
14966 		}
14967 	} else if ((rack->r_ctl.crte == NULL) &&
14968 		   (rack->rack_hdrw_pacing == 0) &&
14969 		   (rack->rack_hdw_pace_ena) &&
14970 		   rack->r_rack_hw_rate_caps &&
14971 		   (rack->rack_attempt_hdwr_pace == 0) &&
14972 		   (rack->rc_inp->inp_route.ro_nh != NULL) &&
14973 		   (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
14974 		/*
14975 		 * Ok we may have a first attempt that is greater than our top rate
14976 		 * lets check.
14977 		 */
14978 		uint64_t high_rate;
14979 
14980 		high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp);
14981 		if (high_rate) {
14982 			if (fill_bw > high_rate) {
14983 				fill_bw = high_rate;
14984 				if (capped)
14985 					*capped = 1;
14986 			}
14987 		}
14988 	}
14989 	/*
14990 	 * Ok fill_bw holds our mythical b/w to fill the cwnd
14991 	 * in a rtt, what does that time wise equate too?
14992 	 */
14993 	lentim = (uint64_t)(len) * (uint64_t)HPTS_USEC_IN_SEC;
14994 	lentim /= fill_bw;
14995 	*rate_wanted = fill_bw;
14996 	if (non_paced || (lentim < slot)) {
14997 		rack_log_pacing_delay_calc(rack, len, slot, fill_bw,
14998 					   0, lentim, 12, __LINE__, NULL, 0);
14999 		return ((int32_t)lentim);
15000 	} else
15001 		return (slot);
15002 }
15003 
15004 static int32_t
15005 rack_get_pacing_delay(struct tcp_rack *rack, struct tcpcb *tp, uint32_t len, struct rack_sendmap *rsm, uint32_t segsiz)
15006 {
15007 	uint64_t srtt;
15008 	int32_t slot = 0;
15009 	int can_start_hw_pacing = 1;
15010 	int err;
15011 
15012 	if (rack->rc_always_pace == 0) {
15013 		/*
15014 		 * We use the most optimistic possible cwnd/srtt for
15015 		 * sending calculations. This will make our
15016 		 * calculation anticipate getting more through
15017 		 * quicker then possible. But thats ok we don't want
15018 		 * the peer to have a gap in data sending.
15019 		 */
15020 		uint64_t cwnd, tr_perms = 0;
15021 		int32_t reduce = 0;
15022 
15023 	old_method:
15024 		/*
15025 		 * We keep no precise pacing with the old method
15026 		 * instead we use the pacer to mitigate bursts.
15027 		 */
15028 		if (rack->r_ctl.rc_rack_min_rtt)
15029 			srtt = rack->r_ctl.rc_rack_min_rtt;
15030 		else
15031 			srtt = max(tp->t_srtt, 1);
15032 		if (rack->r_ctl.rc_rack_largest_cwnd)
15033 			cwnd = rack->r_ctl.rc_rack_largest_cwnd;
15034 		else
15035 			cwnd = rack->r_ctl.cwnd_to_use;
15036 		/* Inflate cwnd by 1000 so srtt of usecs is in ms */
15037 		tr_perms = (cwnd * 1000) / srtt;
15038 		if (tr_perms == 0) {
15039 			tr_perms = ctf_fixed_maxseg(tp);
15040 		}
15041 		/*
15042 		 * Calculate how long this will take to drain, if
15043 		 * the calculation comes out to zero, thats ok we
15044 		 * will use send_a_lot to possibly spin around for
15045 		 * more increasing tot_len_this_send to the point
15046 		 * that its going to require a pace, or we hit the
15047 		 * cwnd. Which in that case we are just waiting for
15048 		 * a ACK.
15049 		 */
15050 		slot = len / tr_perms;
15051 		/* Now do we reduce the time so we don't run dry? */
15052 		if (slot && rack_slot_reduction) {
15053 			reduce = (slot / rack_slot_reduction);
15054 			if (reduce < slot) {
15055 				slot -= reduce;
15056 			} else
15057 				slot = 0;
15058 		}
15059 		slot *= HPTS_USEC_IN_MSEC;
15060 		if (rack->rc_pace_to_cwnd) {
15061 			uint64_t rate_wanted = 0;
15062 
15063 			slot = pace_to_fill_cwnd(rack, slot, len, segsiz, NULL, &rate_wanted, 1);
15064 			rack->rc_ack_can_sendout_data = 1;
15065 			rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, 0, 0, 14, __LINE__, NULL, 0);
15066 		} else
15067 			rack_log_pacing_delay_calc(rack, len, slot, tr_perms, reduce, 0, 7, __LINE__, NULL, 0);
15068 	} else {
15069 		uint64_t bw_est, res, lentim, rate_wanted;
15070 		uint32_t orig_val, segs, oh;
15071 		int capped = 0;
15072 		int prev_fill;
15073 
15074 		if ((rack->r_rr_config == 1) && rsm) {
15075 			return (rack->r_ctl.rc_min_to);
15076 		}
15077 		if (rack->use_fixed_rate) {
15078 			rate_wanted = bw_est = rack_get_fixed_pacing_bw(rack);
15079 		} else if ((rack->r_ctl.init_rate == 0) &&
15080 #ifdef NETFLIX_PEAKRATE
15081 			   (rack->rc_tp->t_maxpeakrate == 0) &&
15082 #endif
15083 			   (rack->r_ctl.gp_bw == 0)) {
15084 			/* no way to yet do an estimate */
15085 			bw_est = rate_wanted = 0;
15086 		} else {
15087 			bw_est = rack_get_bw(rack);
15088 			rate_wanted = rack_get_output_bw(rack, bw_est, rsm, &capped);
15089 		}
15090 		if ((bw_est == 0) || (rate_wanted == 0) ||
15091 		    ((rack->gp_ready == 0) && (rack->use_fixed_rate == 0))) {
15092 			/*
15093 			 * No way yet to make a b/w estimate or
15094 			 * our raise is set incorrectly.
15095 			 */
15096 			goto old_method;
15097 		}
15098 		/* We need to account for all the overheads */
15099 		segs = (len + segsiz - 1) / segsiz;
15100 		/*
15101 		 * We need the diff between 1514 bytes (e-mtu with e-hdr)
15102 		 * and how much data we put in each packet. Yes this
15103 		 * means we may be off if we are larger than 1500 bytes
15104 		 * or smaller. But this just makes us more conservative.
15105 		 */
15106 		if (rack_hw_rate_min &&
15107 		    (bw_est < rack_hw_rate_min))
15108 			can_start_hw_pacing = 0;
15109 		if (ETHERNET_SEGMENT_SIZE > segsiz)
15110 			oh = ETHERNET_SEGMENT_SIZE - segsiz;
15111 		else
15112 			oh = 0;
15113 		segs *= oh;
15114 		lentim = (uint64_t)(len + segs) * (uint64_t)HPTS_USEC_IN_SEC;
15115 		res = lentim / rate_wanted;
15116 		slot = (uint32_t)res;
15117 		orig_val = rack->r_ctl.rc_pace_max_segs;
15118 		if (rack->r_ctl.crte == NULL) {
15119 			/*
15120 			 * Only do this if we are not hardware pacing
15121 			 * since if we are doing hw-pacing below we will
15122 			 * set make a call after setting up or changing
15123 			 * the rate.
15124 			 */
15125 			rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
15126 		} else if (rack->rc_inp->inp_snd_tag == NULL) {
15127 			/*
15128 			 * We lost our rate somehow, this can happen
15129 			 * if the interface changed underneath us.
15130 			 */
15131 			tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
15132 			rack->r_ctl.crte = NULL;
15133 			/* Lets re-allow attempting to setup pacing */
15134 			rack->rack_hdrw_pacing = 0;
15135 			rack->rack_attempt_hdwr_pace = 0;
15136 			rack_log_hdwr_pacing(rack,
15137 					     rate_wanted, bw_est, __LINE__,
15138 					     0, 6);
15139 		}
15140 		/* Did we change the TSO size, if so log it */
15141 		if (rack->r_ctl.rc_pace_max_segs != orig_val)
15142 			rack_log_pacing_delay_calc(rack, len, slot, orig_val, 0, 0, 15, __LINE__, NULL, 0);
15143 		prev_fill = rack->r_via_fill_cw;
15144 		if ((rack->rc_pace_to_cwnd) &&
15145 		    (capped == 0) &&
15146 		    (rack->use_fixed_rate == 0) &&
15147 		    (rack->in_probe_rtt == 0) &&
15148 		    (IN_FASTRECOVERY(rack->rc_tp->t_flags) == 0)) {
15149 			/*
15150 			 * We want to pace at our rate *or* faster to
15151 			 * fill the cwnd to the max if its not full.
15152 			 */
15153 			slot = pace_to_fill_cwnd(rack, slot, (len+segs), segsiz, &capped, &rate_wanted, 0);
15154 		}
15155 		if ((rack->rc_inp->inp_route.ro_nh != NULL) &&
15156 		    (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
15157 			if ((rack->rack_hdw_pace_ena) &&
15158 			    (can_start_hw_pacing > 0) &&
15159 			    (rack->rack_hdrw_pacing == 0) &&
15160 			    (rack->rack_attempt_hdwr_pace == 0)) {
15161 				/*
15162 				 * Lets attempt to turn on hardware pacing
15163 				 * if we can.
15164 				 */
15165 				rack->rack_attempt_hdwr_pace = 1;
15166 				rack->r_ctl.crte = tcp_set_pacing_rate(rack->rc_tp,
15167 								       rack->rc_inp->inp_route.ro_nh->nh_ifp,
15168 								       rate_wanted,
15169 								       RS_PACING_GEQ,
15170 								       &err, &rack->r_ctl.crte_prev_rate);
15171 				if (rack->r_ctl.crte) {
15172 					rack->rack_hdrw_pacing = 1;
15173 					rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size(tp, rate_wanted, segsiz,
15174 												 0, rack->r_ctl.crte,
15175 												 NULL);
15176 					rack_log_hdwr_pacing(rack,
15177 							     rate_wanted, rack->r_ctl.crte->rate, __LINE__,
15178 							     err, 0);
15179 					rack->r_ctl.last_hw_bw_req = rate_wanted;
15180 				} else {
15181 					counter_u64_add(rack_hw_pace_init_fail, 1);
15182 				}
15183 			} else if (rack->rack_hdrw_pacing &&
15184 				   (rack->r_ctl.last_hw_bw_req != rate_wanted)) {
15185 				/* Do we need to adjust our rate? */
15186 				const struct tcp_hwrate_limit_table *nrte;
15187 
15188 				if (rack->r_up_only &&
15189 				    (rate_wanted < rack->r_ctl.crte->rate)) {
15190 					/**
15191 					 * We have four possible states here
15192 					 * having to do with the previous time
15193 					 * and this time.
15194 					 *   previous  |  this-time
15195 					 * A)     0      |     0   -- fill_cw not in the picture
15196 					 * B)     1      |     0   -- we were doing a fill-cw but now are not
15197 					 * C)     1      |     1   -- all rates from fill_cw
15198 					 * D)     0      |     1   -- we were doing non-fill and now we are filling
15199 					 *
15200 					 * For case A, C and D we don't allow a drop. But for
15201 					 * case B where we now our on our steady rate we do
15202 					 * allow a drop.
15203 					 *
15204 					 */
15205 					if (!((prev_fill == 1) && (rack->r_via_fill_cw == 0)))
15206 						goto done_w_hdwr;
15207 				}
15208 				if ((rate_wanted > rack->r_ctl.crte->rate) ||
15209 				    (rate_wanted <= rack->r_ctl.crte_prev_rate)) {
15210 					if (rack_hw_rate_to_low &&
15211 					    (bw_est < rack_hw_rate_to_low)) {
15212 						/*
15213 						 * The pacing rate is too low for hardware, but
15214 						 * do allow hardware pacing to be restarted.
15215 						 */
15216 						rack_log_hdwr_pacing(rack,
15217 							     bw_est, rack->r_ctl.crte->rate, __LINE__,
15218 							     0, 5);
15219 						tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
15220 						rack->r_ctl.crte = NULL;
15221 						rack->rack_attempt_hdwr_pace = 0;
15222 						rack->rack_hdrw_pacing = 0;
15223 						rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
15224 						goto done_w_hdwr;
15225 					}
15226 					nrte = tcp_chg_pacing_rate(rack->r_ctl.crte,
15227 								   rack->rc_tp,
15228 								   rack->rc_inp->inp_route.ro_nh->nh_ifp,
15229 								   rate_wanted,
15230 								   RS_PACING_GEQ,
15231 								   &err, &rack->r_ctl.crte_prev_rate);
15232 					if (nrte == NULL) {
15233 						/* Lost the rate */
15234 						rack->rack_hdrw_pacing = 0;
15235 						rack->r_ctl.crte = NULL;
15236 						rack_log_hdwr_pacing(rack,
15237 								     rate_wanted, 0, __LINE__,
15238 								     err, 1);
15239 						rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
15240 						counter_u64_add(rack_hw_pace_lost, 1);
15241 					} else if (nrte != rack->r_ctl.crte) {
15242 						rack->r_ctl.crte = nrte;
15243 						rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size(tp, rate_wanted,
15244 													 segsiz, 0,
15245 													 rack->r_ctl.crte,
15246 													 NULL);
15247 						rack_log_hdwr_pacing(rack,
15248 								     rate_wanted, rack->r_ctl.crte->rate, __LINE__,
15249 								     err, 2);
15250 						rack->r_ctl.last_hw_bw_req = rate_wanted;
15251 					}
15252 				} else {
15253 					/* We just need to adjust the segment size */
15254 					rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
15255 					rack_log_hdwr_pacing(rack,
15256 							     rate_wanted, rack->r_ctl.crte->rate, __LINE__,
15257 							     0, 4);
15258 					rack->r_ctl.last_hw_bw_req = rate_wanted;
15259 				}
15260 			}
15261 		}
15262 		if ((rack->r_ctl.crte != NULL) &&
15263 		    (rack->r_ctl.crte->rate == rate_wanted)) {
15264 			/*
15265 			 * We need to add a extra if the rates
15266 			 * are exactly matched. The idea is
15267 			 * we want the software to make sure the
15268 			 * queue is empty before adding more, this
15269 			 * gives us N MSS extra pace times where
15270 			 * N is our sysctl
15271 			 */
15272 			slot += (rack->r_ctl.crte->time_between * rack_hw_pace_extra_slots);
15273 		}
15274 done_w_hdwr:
15275 		if (rack_limit_time_with_srtt &&
15276 		    (rack->use_fixed_rate == 0) &&
15277 #ifdef NETFLIX_PEAKRATE
15278 		    (rack->rc_tp->t_maxpeakrate == 0) &&
15279 #endif
15280 		    (rack->rack_hdrw_pacing == 0)) {
15281 			/*
15282 			 * Sanity check, we do not allow the pacing delay
15283 			 * to be longer than the SRTT of the path. If it is
15284 			 * a slow path, then adding a packet should increase
15285 			 * the RTT and compensate for this i.e. the srtt will
15286 			 * be greater so the allowed pacing time will be greater.
15287 			 *
15288 			 * Note this restriction is not for where a peak rate
15289 			 * is set, we are doing fixed pacing or hardware pacing.
15290 			 */
15291 			if (rack->rc_tp->t_srtt)
15292 				srtt = rack->rc_tp->t_srtt;
15293 			else
15294 				srtt = RACK_INITIAL_RTO * HPTS_USEC_IN_MSEC;	/* its in ms convert */
15295 			if (srtt < (uint64_t)slot) {
15296 				rack_log_pacing_delay_calc(rack, srtt, slot, rate_wanted, bw_est, lentim, 99, __LINE__, NULL, 0);
15297 				slot = srtt;
15298 			}
15299 		}
15300 		rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, bw_est, lentim, 2, __LINE__, rsm, 0);
15301 	}
15302 	if (rack->r_ctl.crte && (rack->r_ctl.crte->rs_num_enobufs > 0)) {
15303 		/*
15304 		 * If this rate is seeing enobufs when it
15305 		 * goes to send then either the nic is out
15306 		 * of gas or we are mis-estimating the time
15307 		 * somehow and not letting the queue empty
15308 		 * completely. Lets add to the pacing time.
15309 		 */
15310 		int hw_boost_delay;
15311 
15312 		hw_boost_delay = rack->r_ctl.crte->time_between * rack_enobuf_hw_boost_mult;
15313 		if (hw_boost_delay > rack_enobuf_hw_max)
15314 			hw_boost_delay = rack_enobuf_hw_max;
15315 		else if (hw_boost_delay < rack_enobuf_hw_min)
15316 			hw_boost_delay = rack_enobuf_hw_min;
15317 		slot += hw_boost_delay;
15318 	}
15319 	if (slot)
15320 		counter_u64_add(rack_calc_nonzero, 1);
15321 	else
15322 		counter_u64_add(rack_calc_zero, 1);
15323 	return (slot);
15324 }
15325 
15326 static void
15327 rack_start_gp_measurement(struct tcpcb *tp, struct tcp_rack *rack,
15328     tcp_seq startseq, uint32_t sb_offset)
15329 {
15330 	struct rack_sendmap *my_rsm = NULL;
15331 	struct rack_sendmap fe;
15332 
15333 	if (tp->t_state < TCPS_ESTABLISHED) {
15334 		/*
15335 		 * We don't start any measurements if we are
15336 		 * not at least established.
15337 		 */
15338 		return;
15339 	}
15340 	if (tp->t_state >= TCPS_FIN_WAIT_1) {
15341 		/*
15342 		 * We will get no more data into the SB
15343 		 * this means we need to have the data available
15344 		 * before we start a measurement.
15345 		 */
15346 
15347 		if (sbavail(&tp->t_inpcb->inp_socket->so_snd) <
15348 		    max(rc_init_window(rack),
15349 			(MIN_GP_WIN * ctf_fixed_maxseg(tp)))) {
15350 			/* Nope not enough data */
15351 			return;
15352 		}
15353 	}
15354 	tp->t_flags |= TF_GPUTINPROG;
15355 	rack->r_ctl.rc_gp_lowrtt = 0xffffffff;
15356 	rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
15357 	tp->gput_seq = startseq;
15358 	rack->app_limited_needs_set = 0;
15359 	if (rack->in_probe_rtt)
15360 		rack->measure_saw_probe_rtt = 1;
15361 	else if ((rack->measure_saw_probe_rtt) &&
15362 		 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
15363 		rack->measure_saw_probe_rtt = 0;
15364 	if (rack->rc_gp_filled)
15365 		tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
15366 	else {
15367 		/* Special case initial measurement */
15368 		struct timeval tv;
15369 
15370 		tp->gput_ts = tcp_get_usecs(&tv);
15371 		rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
15372 	}
15373 	/*
15374 	 * We take a guess out into the future,
15375 	 * if we have no measurement and no
15376 	 * initial rate, we measure the first
15377 	 * initial-windows worth of data to
15378 	 * speed up getting some GP measurement and
15379 	 * thus start pacing.
15380 	 */
15381 	if ((rack->rc_gp_filled == 0) && (rack->r_ctl.init_rate == 0)) {
15382 		rack->app_limited_needs_set = 1;
15383 		tp->gput_ack = startseq + max(rc_init_window(rack),
15384 					      (MIN_GP_WIN * ctf_fixed_maxseg(tp)));
15385 		rack_log_pacing_delay_calc(rack,
15386 					   tp->gput_seq,
15387 					   tp->gput_ack,
15388 					   0,
15389 					   tp->gput_ts,
15390 					   rack->r_ctl.rc_app_limited_cnt,
15391 					   9,
15392 					   __LINE__, NULL, 0);
15393 		return;
15394 	}
15395 	if (sb_offset) {
15396 		/*
15397 		 * We are out somewhere in the sb
15398 		 * can we use the already outstanding data?
15399 		 */
15400 		if (rack->r_ctl.rc_app_limited_cnt == 0) {
15401 			/*
15402 			 * Yes first one is good and in this case
15403 			 * the tp->gput_ts is correctly set based on
15404 			 * the last ack that arrived (no need to
15405 			 * set things up when an ack comes in).
15406 			 */
15407 			my_rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
15408 			if ((my_rsm == NULL) ||
15409 			    (my_rsm->r_rtr_cnt != 1)) {
15410 				/* retransmission? */
15411 				goto use_latest;
15412 			}
15413 		} else {
15414 			if (rack->r_ctl.rc_first_appl == NULL) {
15415 				/*
15416 				 * If rc_first_appl is NULL
15417 				 * then the cnt should be 0.
15418 				 * This is probably an error, maybe
15419 				 * a KASSERT would be approprate.
15420 				 */
15421 				goto use_latest;
15422 			}
15423 			/*
15424 			 * If we have a marker pointer to the last one that is
15425 			 * app limited we can use that, but we need to set
15426 			 * things up so that when it gets ack'ed we record
15427 			 * the ack time (if its not already acked).
15428 			 */
15429 			rack->app_limited_needs_set = 1;
15430 			/*
15431 			 * We want to get to the rsm that is either
15432 			 * next with space i.e. over 1 MSS or the one
15433 			 * after that (after the app-limited).
15434 			 */
15435 			my_rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree,
15436 					 rack->r_ctl.rc_first_appl);
15437 			if (my_rsm) {
15438 				if ((my_rsm->r_end - my_rsm->r_start) <= ctf_fixed_maxseg(tp))
15439 					/* Have to use the next one */
15440 					my_rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree,
15441 							 my_rsm);
15442 				else {
15443 					/* Use after the first MSS of it is acked */
15444 					tp->gput_seq = my_rsm->r_start + ctf_fixed_maxseg(tp);
15445 					goto start_set;
15446 				}
15447 			}
15448 			if ((my_rsm == NULL) ||
15449 			    (my_rsm->r_rtr_cnt != 1)) {
15450 				/*
15451 				 * Either its a retransmit or
15452 				 * the last is the app-limited one.
15453 				 */
15454 				goto use_latest;
15455 			}
15456 		}
15457 		tp->gput_seq = my_rsm->r_start;
15458 start_set:
15459 		if (my_rsm->r_flags & RACK_ACKED) {
15460 			/*
15461 			 * This one has been acked use the arrival ack time
15462 			 */
15463 			tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival;
15464 			rack->app_limited_needs_set = 0;
15465 		}
15466 		rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[(my_rsm->r_rtr_cnt-1)];
15467 		tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
15468 		rack_log_pacing_delay_calc(rack,
15469 					   tp->gput_seq,
15470 					   tp->gput_ack,
15471 					   (uint64_t)my_rsm,
15472 					   tp->gput_ts,
15473 					   rack->r_ctl.rc_app_limited_cnt,
15474 					   9,
15475 					   __LINE__, NULL, 0);
15476 		return;
15477 	}
15478 
15479 use_latest:
15480 	/*
15481 	 * We don't know how long we may have been
15482 	 * idle or if this is the first-send. Lets
15483 	 * setup the flag so we will trim off
15484 	 * the first ack'd data so we get a true
15485 	 * measurement.
15486 	 */
15487 	rack->app_limited_needs_set = 1;
15488 	tp->gput_ack = startseq + rack_get_measure_window(tp, rack);
15489 	/* Find this guy so we can pull the send time */
15490 	fe.r_start = startseq;
15491 	my_rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
15492 	if (my_rsm) {
15493 		rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[(my_rsm->r_rtr_cnt-1)];
15494 		if (my_rsm->r_flags & RACK_ACKED) {
15495 			/*
15496 			 * Unlikely since its probably what was
15497 			 * just transmitted (but I am paranoid).
15498 			 */
15499 			tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival;
15500 			rack->app_limited_needs_set = 0;
15501 		}
15502 		if (SEQ_LT(my_rsm->r_start, tp->gput_seq)) {
15503 			/* This also is unlikely */
15504 			tp->gput_seq = my_rsm->r_start;
15505 		}
15506 	} else {
15507 		/*
15508 		 * TSNH unless we have some send-map limit,
15509 		 * and even at that it should not be hitting
15510 		 * that limit (we should have stopped sending).
15511 		 */
15512 		struct timeval tv;
15513 
15514 		microuptime(&tv);
15515 		rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
15516 	}
15517 	rack_log_pacing_delay_calc(rack,
15518 				   tp->gput_seq,
15519 				   tp->gput_ack,
15520 				   (uint64_t)my_rsm,
15521 				   tp->gput_ts,
15522 				   rack->r_ctl.rc_app_limited_cnt,
15523 				   9, __LINE__, NULL, 0);
15524 }
15525 
15526 static inline uint32_t
15527 rack_what_can_we_send(struct tcpcb *tp, struct tcp_rack *rack,  uint32_t cwnd_to_use,
15528     uint32_t avail, int32_t sb_offset)
15529 {
15530 	uint32_t len;
15531 	uint32_t sendwin;
15532 
15533 	if (tp->snd_wnd > cwnd_to_use)
15534 		sendwin = cwnd_to_use;
15535 	else
15536 		sendwin = tp->snd_wnd;
15537 	if (ctf_outstanding(tp) >= tp->snd_wnd) {
15538 		/* We never want to go over our peers rcv-window */
15539 		len = 0;
15540 	} else {
15541 		uint32_t flight;
15542 
15543 		flight = ctf_flight_size(tp, rack->r_ctl.rc_sacked);
15544 		if (flight >= sendwin) {
15545 			/*
15546 			 * We have in flight what we are allowed by cwnd (if
15547 			 * it was rwnd blocking it would have hit above out
15548 			 * >= tp->snd_wnd).
15549 			 */
15550 			return (0);
15551 		}
15552 		len = sendwin - flight;
15553 		if ((len + ctf_outstanding(tp)) > tp->snd_wnd) {
15554 			/* We would send too much (beyond the rwnd) */
15555 			len = tp->snd_wnd - ctf_outstanding(tp);
15556 		}
15557 		if ((len + sb_offset) > avail) {
15558 			/*
15559 			 * We don't have that much in the SB, how much is
15560 			 * there?
15561 			 */
15562 			len = avail - sb_offset;
15563 		}
15564 	}
15565 	return (len);
15566 }
15567 
15568 static void
15569 rack_log_fsb(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t flags,
15570 	     unsigned ipoptlen, int32_t orig_len, int32_t len, int error,
15571 	     int rsm_is_null, int optlen, int line, uint16_t mode)
15572 {
15573 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
15574 		union tcp_log_stackspecific log;
15575 		struct timeval tv;
15576 
15577 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
15578 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
15579 		log.u_bbr.flex1 = error;
15580 		log.u_bbr.flex2 = flags;
15581 		log.u_bbr.flex3 = rsm_is_null;
15582 		log.u_bbr.flex4 = ipoptlen;
15583 		log.u_bbr.flex5 = tp->rcv_numsacks;
15584 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
15585 		log.u_bbr.flex7 = optlen;
15586 		log.u_bbr.flex8 = rack->r_fsb_inited;
15587 		log.u_bbr.applimited = rack->r_fast_output;
15588 		log.u_bbr.bw_inuse = rack_get_bw(rack);
15589 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
15590 		log.u_bbr.cwnd_gain = mode;
15591 		log.u_bbr.pkts_out = orig_len;
15592 		log.u_bbr.lt_epoch = len;
15593 		log.u_bbr.delivered = line;
15594 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
15595 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
15596 		tcp_log_event_(tp, NULL, &so->so_rcv, &so->so_snd, TCP_LOG_FSB, 0,
15597 			       len, &log, false, NULL, NULL, 0, &tv);
15598 	}
15599 }
15600 
15601 
15602 static struct mbuf *
15603 rack_fo_base_copym(struct mbuf *the_m, uint32_t the_off, int32_t *plen,
15604 		   struct rack_fast_send_blk *fsb,
15605 		   int32_t seglimit, int32_t segsize, int hw_tls)
15606 {
15607 #ifdef KERN_TLS
15608 	struct ktls_session *tls, *ntls;
15609 #ifdef INVARIANTS
15610 	struct mbuf *start;
15611 #endif
15612 #endif
15613 	struct mbuf *m, *n, **np, *smb;
15614 	struct mbuf *top;
15615 	int32_t off, soff;
15616 	int32_t len = *plen;
15617 	int32_t fragsize;
15618 	int32_t len_cp = 0;
15619 	uint32_t mlen, frags;
15620 
15621 	soff = off = the_off;
15622 	smb = m = the_m;
15623 	np = &top;
15624 	top = NULL;
15625 #ifdef KERN_TLS
15626 	if (hw_tls && (m->m_flags & M_EXTPG))
15627 		tls = m->m_epg_tls;
15628 	else
15629 		tls = NULL;
15630 #ifdef INVARIANTS
15631 	start = m;
15632 #endif
15633 #endif
15634 	while (len > 0) {
15635 		if (m == NULL) {
15636 			*plen = len_cp;
15637 			break;
15638 		}
15639 #ifdef KERN_TLS
15640 		if (hw_tls) {
15641 			if (m->m_flags & M_EXTPG)
15642 				ntls = m->m_epg_tls;
15643 			else
15644 				ntls = NULL;
15645 
15646 			/*
15647 			 * Avoid mixing TLS records with handshake
15648 			 * data or TLS records from different
15649 			 * sessions.
15650 			 */
15651 			if (tls != ntls) {
15652 				MPASS(m != start);
15653 				*plen = len_cp;
15654 				break;
15655 			}
15656 		}
15657 #endif
15658 		mlen = min(len, m->m_len - off);
15659 		if (seglimit) {
15660 			/*
15661 			 * For M_EXTPG mbufs, add 3 segments
15662 			 * + 1 in case we are crossing page boundaries
15663 			 * + 2 in case the TLS hdr/trailer are used
15664 			 * It is cheaper to just add the segments
15665 			 * than it is to take the cache miss to look
15666 			 * at the mbuf ext_pgs state in detail.
15667 			 */
15668 			if (m->m_flags & M_EXTPG) {
15669 				fragsize = min(segsize, PAGE_SIZE);
15670 				frags = 3;
15671 			} else {
15672 				fragsize = segsize;
15673 				frags = 0;
15674 			}
15675 
15676 			/* Break if we really can't fit anymore. */
15677 			if ((frags + 1) >= seglimit) {
15678 				*plen =	len_cp;
15679 				break;
15680 			}
15681 
15682 			/*
15683 			 * Reduce size if you can't copy the whole
15684 			 * mbuf. If we can't copy the whole mbuf, also
15685 			 * adjust len so the loop will end after this
15686 			 * mbuf.
15687 			 */
15688 			if ((frags + howmany(mlen, fragsize)) >= seglimit) {
15689 				mlen = (seglimit - frags - 1) * fragsize;
15690 				len = mlen;
15691 				*plen = len_cp + len;
15692 			}
15693 			frags += howmany(mlen, fragsize);
15694 			if (frags == 0)
15695 				frags++;
15696 			seglimit -= frags;
15697 			KASSERT(seglimit > 0,
15698 			    ("%s: seglimit went too low", __func__));
15699 		}
15700 		n = m_get(M_NOWAIT, m->m_type);
15701 		*np = n;
15702 		if (n == NULL)
15703 			goto nospace;
15704 		n->m_len = mlen;
15705 		soff += mlen;
15706 		len_cp += n->m_len;
15707 		if (m->m_flags & (M_EXT|M_EXTPG)) {
15708 			n->m_data = m->m_data + off;
15709 			mb_dupcl(n, m);
15710 		} else {
15711 			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
15712 			    (u_int)n->m_len);
15713 		}
15714 		len -= n->m_len;
15715 		off = 0;
15716 		m = m->m_next;
15717 		np = &n->m_next;
15718 		if (len || (soff == smb->m_len)) {
15719 			/*
15720 			 * We have more so we move forward  or
15721 			 * we have consumed the entire mbuf and
15722 			 * len has fell to 0.
15723 			 */
15724 			soff = 0;
15725 			smb = m;
15726 		}
15727 
15728 	}
15729 	if (fsb != NULL) {
15730 		fsb->m = smb;
15731 		fsb->off = soff;
15732 		if (smb) {
15733 			/*
15734 			 * Save off the size of the mbuf. We do
15735 			 * this so that we can recognize when it
15736 			 * has been trimmed by sbcut() as acks
15737 			 * come in.
15738 			 */
15739 			fsb->o_m_len = smb->m_len;
15740 		} else {
15741 			/*
15742 			 * This is the case where the next mbuf went to NULL. This
15743 			 * means with this copy we have sent everything in the sb.
15744 			 * In theory we could clear the fast_output flag, but lets
15745 			 * not since its possible that we could get more added
15746 			 * and acks that call the extend function which would let
15747 			 * us send more.
15748 			 */
15749 			fsb->o_m_len = 0;
15750 		}
15751 	}
15752 	return (top);
15753 nospace:
15754 	if (top)
15755 		m_freem(top);
15756 	return (NULL);
15757 
15758 }
15759 
15760 /*
15761  * This is a copy of m_copym(), taking the TSO segment size/limit
15762  * constraints into account, and advancing the sndptr as it goes.
15763  */
15764 static struct mbuf *
15765 rack_fo_m_copym(struct tcp_rack *rack, int32_t *plen,
15766 		int32_t seglimit, int32_t segsize, struct mbuf **s_mb, int *s_soff)
15767 {
15768 	struct mbuf *m, *n;
15769 	int32_t soff;
15770 
15771 	soff = rack->r_ctl.fsb.off;
15772 	m = rack->r_ctl.fsb.m;
15773 	if (rack->r_ctl.fsb.o_m_len > m->m_len) {
15774 		/*
15775 		 * The mbuf had the front of it chopped off by an ack
15776 		 * we need to adjust the soff/off by that difference.
15777 		 */
15778 		uint32_t delta;
15779 
15780 		delta = rack->r_ctl.fsb.o_m_len - m->m_len;
15781 		soff -= delta;
15782 	} else if (rack->r_ctl.fsb.o_m_len < m->m_len) {
15783 		/*
15784 		 * The mbuf was expanded probably by
15785 		 * a m_compress. Just update o_m_len.
15786 		 */
15787 		rack->r_ctl.fsb.o_m_len = m->m_len;
15788 	}
15789 	KASSERT(soff >= 0, ("%s, negative off %d", __FUNCTION__, soff));
15790 	KASSERT(*plen >= 0, ("%s, negative len %d", __FUNCTION__, *plen));
15791 	KASSERT(soff < m->m_len, ("%s rack:%p len:%u m:%p m->m_len:%u < off?",
15792 				 __FUNCTION__,
15793 				 rack, *plen, m, m->m_len));
15794 	/* Save off the right location before we copy and advance */
15795 	*s_soff = soff;
15796 	*s_mb = rack->r_ctl.fsb.m;
15797 	n = rack_fo_base_copym(m, soff, plen,
15798 			       &rack->r_ctl.fsb,
15799 			       seglimit, segsize, rack->r_ctl.fsb.hw_tls);
15800 	return (n);
15801 }
15802 
15803 static int
15804 rack_fast_rsm_output(struct tcpcb *tp, struct tcp_rack *rack, struct rack_sendmap *rsm,
15805 		     uint64_t ts_val, uint32_t cts, uint32_t ms_cts, struct timeval *tv, int len, uint8_t doing_tlp)
15806 {
15807 	/*
15808 	 * Enter the fast retransmit path. We are given that a sched_pin is
15809 	 * in place (if accounting is compliled in) and the cycle count taken
15810 	 * at the entry is in the ts_val. The concept her is that the rsm
15811 	 * now holds the mbuf offsets and such so we can directly transmit
15812 	 * without a lot of overhead, the len field is already set for
15813 	 * us to prohibit us from sending too much (usually its 1MSS).
15814 	 */
15815 	struct ip *ip = NULL;
15816 	struct udphdr *udp = NULL;
15817 	struct tcphdr *th = NULL;
15818 	struct mbuf *m = NULL;
15819 	struct inpcb *inp;
15820 	uint8_t *cpto;
15821 	struct tcp_log_buffer *lgb;
15822 #ifdef TCP_ACCOUNTING
15823 	uint64_t crtsc;
15824 	int cnt_thru = 1;
15825 #endif
15826 	struct tcpopt to;
15827 	u_char opt[TCP_MAXOLEN];
15828 	uint32_t hdrlen, optlen;
15829 	int32_t slot, segsiz, max_val, tso = 0, error, ulen = 0;
15830 	uint16_t flags;
15831 	uint32_t if_hw_tsomaxsegcount = 0, startseq;
15832 	uint32_t if_hw_tsomaxsegsize;
15833 
15834 #ifdef INET6
15835 	struct ip6_hdr *ip6 = NULL;
15836 
15837 	if (rack->r_is_v6) {
15838 		ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
15839 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
15840 	} else
15841 #endif				/* INET6 */
15842 	{
15843 		ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
15844 		hdrlen = sizeof(struct tcpiphdr);
15845 	}
15846 	if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) {
15847 		goto failed;
15848 	}
15849 	if (doing_tlp) {
15850 		/* Its a TLP add the flag, it may already be there but be sure */
15851 		rsm->r_flags |= RACK_TLP;
15852 	} else {
15853 		/* If it was a TLP it is not not on this retransmit */
15854 		rsm->r_flags &= ~RACK_TLP;
15855 	}
15856 	startseq = rsm->r_start;
15857 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
15858 	inp = rack->rc_inp;
15859 	to.to_flags = 0;
15860 	flags = tcp_outflags[tp->t_state];
15861 	if (flags & (TH_SYN|TH_RST)) {
15862 		goto failed;
15863 	}
15864 	if (rsm->r_flags & RACK_HAS_FIN) {
15865 		/* We can't send a FIN here */
15866 		goto failed;
15867 	}
15868 	if (flags & TH_FIN) {
15869 		/* We never send a FIN */
15870 		flags &= ~TH_FIN;
15871 	}
15872 	if (tp->t_flags & TF_RCVD_TSTMP) {
15873 		to.to_tsval = ms_cts + tp->ts_offset;
15874 		to.to_tsecr = tp->ts_recent;
15875 		to.to_flags = TOF_TS;
15876 	}
15877 	optlen = tcp_addoptions(&to, opt);
15878 	hdrlen += optlen;
15879 	udp = rack->r_ctl.fsb.udp;
15880 	if (udp)
15881 		hdrlen += sizeof(struct udphdr);
15882 	if (rack->r_ctl.rc_pace_max_segs)
15883 		max_val = rack->r_ctl.rc_pace_max_segs;
15884 	else if (rack->rc_user_set_max_segs)
15885 		max_val = rack->rc_user_set_max_segs * segsiz;
15886 	else
15887 		max_val = len;
15888 	if ((tp->t_flags & TF_TSO) &&
15889 	    V_tcp_do_tso &&
15890 	    (len > segsiz) &&
15891 	    (tp->t_port == 0))
15892 		tso = 1;
15893 #ifdef INET6
15894 	if (MHLEN < hdrlen + max_linkhdr)
15895 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
15896 	else
15897 #endif
15898 		m = m_gethdr(M_NOWAIT, MT_DATA);
15899 	if (m == NULL)
15900 		goto failed;
15901 	m->m_data += max_linkhdr;
15902 	m->m_len = hdrlen;
15903 	th = rack->r_ctl.fsb.th;
15904 	/* Establish the len to send */
15905 	if (len > max_val)
15906 		len = max_val;
15907 	if ((tso) && (len + optlen > tp->t_maxseg)) {
15908 		uint32_t if_hw_tsomax;
15909 		int32_t max_len;
15910 
15911 		/* extract TSO information */
15912 		if_hw_tsomax = tp->t_tsomax;
15913 		if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
15914 		if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
15915 		/*
15916 		 * Check if we should limit by maximum payload
15917 		 * length:
15918 		 */
15919 		if (if_hw_tsomax != 0) {
15920 			/* compute maximum TSO length */
15921 			max_len = (if_hw_tsomax - hdrlen -
15922 				   max_linkhdr);
15923 			if (max_len <= 0) {
15924 				goto failed;
15925 			} else if (len > max_len) {
15926 				len = max_len;
15927 			}
15928 		}
15929 		if (len <= segsiz) {
15930 			/*
15931 			 * In case there are too many small fragments don't
15932 			 * use TSO:
15933 			 */
15934 			tso = 0;
15935 		}
15936 	} else {
15937 		tso = 0;
15938 	}
15939 	if ((tso == 0) && (len > segsiz))
15940 		len = segsiz;
15941 	if ((len == 0) ||
15942 	    (len <= MHLEN - hdrlen - max_linkhdr)) {
15943 		goto failed;
15944 	}
15945 	th->th_seq = htonl(rsm->r_start);
15946 	th->th_ack = htonl(tp->rcv_nxt);
15947 	/*
15948 	 * The PUSH bit should only be applied
15949 	 * if the full retransmission is made. If
15950 	 * we are sending less than this is the
15951 	 * left hand edge and should not have
15952 	 * the PUSH bit.
15953 	 */
15954 	if ((rsm->r_flags & RACK_HAD_PUSH) &&
15955 	    (len == (rsm->r_end - rsm->r_start)))
15956 		flags |= TH_PUSH;
15957 	th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale));
15958 	if (th->th_win == 0) {
15959 		tp->t_sndzerowin++;
15960 		tp->t_flags |= TF_RXWIN0SENT;
15961 	} else
15962 		tp->t_flags &= ~TF_RXWIN0SENT;
15963 	if (rsm->r_flags & RACK_TLP) {
15964 		/*
15965 		 * TLP should not count in retran count, but
15966 		 * in its own bin
15967 		 */
15968 		counter_u64_add(rack_tlp_retran, 1);
15969 		counter_u64_add(rack_tlp_retran_bytes, len);
15970 	} else {
15971 		tp->t_sndrexmitpack++;
15972 		KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
15973 		KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
15974 	}
15975 #ifdef STATS
15976 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
15977 				 len);
15978 #endif
15979 	if (rsm->m == NULL)
15980 		goto failed;
15981 	if (rsm->orig_m_len != rsm->m->m_len) {
15982 		/* Fix up the orig_m_len and possibly the mbuf offset */
15983 		rack_adjust_orig_mlen(rsm);
15984 	}
15985 	m->m_next = rack_fo_base_copym(rsm->m, rsm->soff, &len, NULL, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, rsm->r_hw_tls);
15986 	if (len <= segsiz) {
15987 		/*
15988 		 * Must have ran out of mbufs for the copy
15989 		 * shorten it to no longer need tso. Lets
15990 		 * not put on sendalot since we are low on
15991 		 * mbufs.
15992 		 */
15993 		tso = 0;
15994 	}
15995 	if ((m->m_next == NULL) || (len <= 0)){
15996 		goto failed;
15997 	}
15998 	if (udp) {
15999 		if (rack->r_is_v6)
16000 			ulen = hdrlen + len - sizeof(struct ip6_hdr);
16001 		else
16002 			ulen = hdrlen + len - sizeof(struct ip);
16003 		udp->uh_ulen = htons(ulen);
16004 	}
16005 	m->m_pkthdr.rcvif = (struct ifnet *)0;
16006 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
16007 	    (tp->t_flags2 & TF2_ECN_PERMIT)) {
16008 		int ect = tcp_ecn_output_established(tp, &flags, len);
16009 		if ((tp->t_state == TCPS_SYN_RECEIVED) &&
16010 		    (tp->t_flags2 & TF2_ECN_SND_ECE))
16011 		    tp->t_flags2 &= ~TF2_ECN_SND_ECE;
16012 #ifdef INET6
16013 		if (rack->r_is_v6) {
16014 		    ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
16015 		    ip6->ip6_flow |= htonl(ect << 20);
16016 		}
16017 		else
16018 #endif
16019 		{
16020 		    ip->ip_tos &= ~IPTOS_ECN_MASK;
16021 		    ip->ip_tos |= ect;
16022 		}
16023 	}
16024 	tcp_set_flags(th, flags);
16025 	m->m_pkthdr.len = hdrlen + len;	/* in6_cksum() need this */
16026 #ifdef INET6
16027 	if (rack->r_is_v6) {
16028 		if (tp->t_port) {
16029 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
16030 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
16031 			udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
16032 			th->th_sum = htons(0);
16033 			UDPSTAT_INC(udps_opackets);
16034 		} else {
16035 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
16036 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
16037 			th->th_sum = in6_cksum_pseudo(ip6,
16038 						      sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
16039 						      0);
16040 		}
16041 	}
16042 #endif
16043 #if defined(INET6) && defined(INET)
16044 	else
16045 #endif
16046 #ifdef INET
16047 	{
16048 		if (tp->t_port) {
16049 			m->m_pkthdr.csum_flags = CSUM_UDP;
16050 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
16051 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
16052 						ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
16053 			th->th_sum = htons(0);
16054 			UDPSTAT_INC(udps_opackets);
16055 		} else {
16056 			m->m_pkthdr.csum_flags = CSUM_TCP;
16057 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
16058 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
16059 					       ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
16060 									IPPROTO_TCP + len + optlen));
16061 		}
16062 		/* IP version must be set here for ipv4/ipv6 checking later */
16063 		KASSERT(ip->ip_v == IPVERSION,
16064 			("%s: IP version incorrect: %d", __func__, ip->ip_v));
16065 	}
16066 #endif
16067 	if (tso) {
16068 		KASSERT(len > tp->t_maxseg - optlen,
16069 			("%s: len <= tso_segsz tp:%p", __func__, tp));
16070 		m->m_pkthdr.csum_flags |= CSUM_TSO;
16071 		m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen;
16072 	}
16073 #ifdef INET6
16074 	if (rack->r_is_v6) {
16075 		ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit;
16076 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
16077 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
16078 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
16079 		else
16080 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
16081 	}
16082 #endif
16083 #if defined(INET) && defined(INET6)
16084 	else
16085 #endif
16086 #ifdef INET
16087 	{
16088 		ip->ip_len = htons(m->m_pkthdr.len);
16089 		ip->ip_ttl = rack->r_ctl.fsb.hoplimit;
16090 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
16091 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
16092 			if (tp->t_port == 0 || len < V_tcp_minmss) {
16093 				ip->ip_off |= htons(IP_DF);
16094 			}
16095 		} else {
16096 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
16097 		}
16098 	}
16099 #endif
16100 	/* Time to copy in our header */
16101 	cpto = mtod(m, uint8_t *);
16102 	memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
16103 	th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
16104 	if (optlen) {
16105 		bcopy(opt, th + 1, optlen);
16106 		th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
16107 	} else {
16108 		th->th_off = sizeof(struct tcphdr) >> 2;
16109 	}
16110 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
16111 		union tcp_log_stackspecific log;
16112 
16113 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
16114 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
16115 		if (rack->rack_no_prr)
16116 			log.u_bbr.flex1 = 0;
16117 		else
16118 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
16119 		log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
16120 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
16121 		log.u_bbr.flex4 = max_val;
16122 		log.u_bbr.flex5 = 0;
16123 		/* Save off the early/late values */
16124 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
16125 		log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
16126 		log.u_bbr.bw_inuse = rack_get_bw(rack);
16127 		if (doing_tlp == 0)
16128 			log.u_bbr.flex8 = 1;
16129 		else
16130 			log.u_bbr.flex8 = 2;
16131 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
16132 		log.u_bbr.flex7 = 55;
16133 		log.u_bbr.pkts_out = tp->t_maxseg;
16134 		log.u_bbr.timeStamp = cts;
16135 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
16136 		log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use;
16137 		log.u_bbr.delivered = 0;
16138 		lgb = tcp_log_event_(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
16139 				     len, &log, false, NULL, NULL, 0, tv);
16140 	} else
16141 		lgb = NULL;
16142 #ifdef INET6
16143 	if (rack->r_is_v6) {
16144 		error = ip6_output(m, NULL,
16145 				   &inp->inp_route6,
16146 				   0, NULL, NULL, inp);
16147 	}
16148 #endif
16149 #if defined(INET) && defined(INET6)
16150 	else
16151 #endif
16152 #ifdef INET
16153 	{
16154 		error = ip_output(m, NULL,
16155 				  &inp->inp_route,
16156 				  0, 0, inp);
16157 	}
16158 #endif
16159 	m = NULL;
16160 	if (lgb) {
16161 		lgb->tlb_errno = error;
16162 		lgb = NULL;
16163 	}
16164 	if (error) {
16165 		goto failed;
16166 	}
16167 	rack_log_output(tp, &to, len, rsm->r_start, flags, error, rack_to_usec_ts(tv),
16168 			rsm, RACK_SENT_FP, rsm->m, rsm->soff, rsm->r_hw_tls);
16169 	if (doing_tlp && (rack->fast_rsm_hack == 0)) {
16170 		rack->rc_tlp_in_progress = 1;
16171 		rack->r_ctl.rc_tlp_cnt_out++;
16172 	}
16173 	if (error == 0) {
16174 		tcp_account_for_send(tp, len, 1, doing_tlp, rsm->r_hw_tls);
16175 		if (doing_tlp) {
16176 			rack->rc_last_sent_tlp_past_cumack = 0;
16177 			rack->rc_last_sent_tlp_seq_valid = 1;
16178 			rack->r_ctl.last_sent_tlp_seq = rsm->r_start;
16179 			rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start;
16180 		}
16181 	}
16182 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
16183 	rack->forced_ack = 0;	/* If we send something zap the FA flag */
16184 	if (IN_FASTRECOVERY(tp->t_flags) && rsm)
16185 		rack->r_ctl.retran_during_recovery += len;
16186 	{
16187 		int idx;
16188 
16189 		idx = (len / segsiz) + 3;
16190 		if (idx >= TCP_MSS_ACCT_ATIMER)
16191 			counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
16192 		else
16193 			counter_u64_add(rack_out_size[idx], 1);
16194 	}
16195 	if (tp->t_rtttime == 0) {
16196 		tp->t_rtttime = ticks;
16197 		tp->t_rtseq = startseq;
16198 		KMOD_TCPSTAT_INC(tcps_segstimed);
16199 	}
16200 	counter_u64_add(rack_fto_rsm_send, 1);
16201 	if (error && (error == ENOBUFS)) {
16202 		slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC);
16203 		if (rack->rc_enobuf < 0x7f)
16204 			rack->rc_enobuf++;
16205 		if (slot < (10 * HPTS_USEC_IN_MSEC))
16206 			slot = 10 * HPTS_USEC_IN_MSEC;
16207 	} else
16208 		slot = rack_get_pacing_delay(rack, tp, len, NULL, segsiz);
16209 	if ((slot == 0) ||
16210 	    (rack->rc_always_pace == 0) ||
16211 	    (rack->r_rr_config == 1)) {
16212 		/*
16213 		 * We have no pacing set or we
16214 		 * are using old-style rack or
16215 		 * we are overriden to use the old 1ms pacing.
16216 		 */
16217 		slot = rack->r_ctl.rc_min_to;
16218 	}
16219 	rack_start_hpts_timer(rack, tp, cts, slot, len, 0);
16220 	if (rack->r_must_retran) {
16221 		rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start);
16222 		if ((SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) ||
16223 		    ((rsm->r_flags & RACK_MUST_RXT) == 0)) {
16224 			/*
16225 			 * We have retransmitted all we need. If
16226 			 * RACK_MUST_RXT is not set then we need to
16227 			 * not retransmit this guy.
16228 			 */
16229 			rack->r_must_retran = 0;
16230 			rack->r_ctl.rc_out_at_rto = 0;
16231 			if ((rsm->r_flags & RACK_MUST_RXT) == 0) {
16232 				/* Not one we should rxt */
16233 				goto failed;
16234 			} else {
16235 				/* Clear the flag */
16236 				rsm->r_flags &= ~RACK_MUST_RXT;
16237 			}
16238 		} else {
16239 			/* Remove  the flag */
16240 			rsm->r_flags &= ~RACK_MUST_RXT;
16241 		}
16242 	}
16243 #ifdef TCP_ACCOUNTING
16244 	crtsc = get_cyclecount();
16245 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16246 		tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru;
16247 	}
16248 	counter_u64_add(tcp_cnt_counters[SND_OUT_DATA], cnt_thru);
16249 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16250 		tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
16251 	}
16252 	counter_u64_add(tcp_proc_time[SND_OUT_DATA], (crtsc - ts_val));
16253 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16254 		tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((len + segsiz - 1) / segsiz);
16255 	}
16256 	counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((len + segsiz - 1) / segsiz));
16257 	sched_unpin();
16258 #endif
16259 	return (0);
16260 failed:
16261 	if (m)
16262 		m_free(m);
16263 	return (-1);
16264 }
16265 
16266 static void
16267 rack_sndbuf_autoscale(struct tcp_rack *rack)
16268 {
16269 	/*
16270 	 * Automatic sizing of send socket buffer.  Often the send buffer
16271 	 * size is not optimally adjusted to the actual network conditions
16272 	 * at hand (delay bandwidth product).  Setting the buffer size too
16273 	 * small limits throughput on links with high bandwidth and high
16274 	 * delay (eg. trans-continental/oceanic links).  Setting the
16275 	 * buffer size too big consumes too much real kernel memory,
16276 	 * especially with many connections on busy servers.
16277 	 *
16278 	 * The criteria to step up the send buffer one notch are:
16279 	 *  1. receive window of remote host is larger than send buffer
16280 	 *     (with a fudge factor of 5/4th);
16281 	 *  2. send buffer is filled to 7/8th with data (so we actually
16282 	 *     have data to make use of it);
16283 	 *  3. send buffer fill has not hit maximal automatic size;
16284 	 *  4. our send window (slow start and cogestion controlled) is
16285 	 *     larger than sent but unacknowledged data in send buffer.
16286 	 *
16287 	 * Note that the rack version moves things much faster since
16288 	 * we want to avoid hitting cache lines in the rack_fast_output()
16289 	 * path so this is called much less often and thus moves
16290 	 * the SB forward by a percentage.
16291 	 */
16292 	struct socket *so;
16293 	struct tcpcb *tp;
16294 	uint32_t sendwin, scaleup;
16295 
16296 	tp = rack->rc_tp;
16297 	so = rack->rc_inp->inp_socket;
16298 	sendwin = min(rack->r_ctl.cwnd_to_use, tp->snd_wnd);
16299 	if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) {
16300 		if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
16301 		    sbused(&so->so_snd) >=
16302 		    (so->so_snd.sb_hiwat / 8 * 7) &&
16303 		    sbused(&so->so_snd) < V_tcp_autosndbuf_max &&
16304 		    sendwin >= (sbused(&so->so_snd) -
16305 		    (tp->snd_nxt - tp->snd_una))) {
16306 			if (rack_autosndbuf_inc)
16307 				scaleup = (rack_autosndbuf_inc * so->so_snd.sb_hiwat) / 100;
16308 			else
16309 				scaleup = V_tcp_autosndbuf_inc;
16310 			if (scaleup < V_tcp_autosndbuf_inc)
16311 				scaleup = V_tcp_autosndbuf_inc;
16312 			scaleup += so->so_snd.sb_hiwat;
16313 			if (scaleup > V_tcp_autosndbuf_max)
16314 				scaleup = V_tcp_autosndbuf_max;
16315 			if (!sbreserve_locked(&so->so_snd, scaleup, so, curthread))
16316 				so->so_snd.sb_flags &= ~SB_AUTOSIZE;
16317 		}
16318 	}
16319 }
16320 
16321 static int
16322 rack_fast_output(struct tcpcb *tp, struct tcp_rack *rack, uint64_t ts_val,
16323 		 uint32_t cts, uint32_t ms_cts, struct timeval *tv, long tot_len, int *send_err)
16324 {
16325 	/*
16326 	 * Enter to do fast output. We are given that the sched_pin is
16327 	 * in place (if accounting is compiled in) and the cycle count taken
16328 	 * at entry is in place in ts_val. The idea here is that
16329 	 * we know how many more bytes needs to be sent (presumably either
16330 	 * during pacing or to fill the cwnd and that was greater than
16331 	 * the max-burst). We have how much to send and all the info we
16332 	 * need to just send.
16333 	 */
16334 	struct ip *ip = NULL;
16335 	struct udphdr *udp = NULL;
16336 	struct tcphdr *th = NULL;
16337 	struct mbuf *m, *s_mb;
16338 	struct inpcb *inp;
16339 	uint8_t *cpto;
16340 	struct tcp_log_buffer *lgb;
16341 #ifdef TCP_ACCOUNTING
16342 	uint64_t crtsc;
16343 #endif
16344 	struct tcpopt to;
16345 	u_char opt[TCP_MAXOLEN];
16346 	uint32_t hdrlen, optlen;
16347 	int cnt_thru = 1;
16348 	int32_t slot, segsiz, len, max_val, tso = 0, sb_offset, error, ulen = 0;
16349 	uint16_t flags;
16350 	uint32_t s_soff;
16351 	uint32_t if_hw_tsomaxsegcount = 0, startseq;
16352 	uint32_t if_hw_tsomaxsegsize;
16353 	uint16_t add_flag = RACK_SENT_FP;
16354 #ifdef INET6
16355 	struct ip6_hdr *ip6 = NULL;
16356 
16357 	if (rack->r_is_v6) {
16358 		ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
16359 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
16360 	} else
16361 #endif				/* INET6 */
16362 	{
16363 		ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
16364 		hdrlen = sizeof(struct tcpiphdr);
16365 	}
16366 	if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) {
16367 		m = NULL;
16368 		goto failed;
16369 	}
16370 	startseq = tp->snd_max;
16371 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
16372 	inp = rack->rc_inp;
16373 	len = rack->r_ctl.fsb.left_to_send;
16374 	to.to_flags = 0;
16375 	flags = rack->r_ctl.fsb.tcp_flags;
16376 	if (tp->t_flags & TF_RCVD_TSTMP) {
16377 		to.to_tsval = ms_cts + tp->ts_offset;
16378 		to.to_tsecr = tp->ts_recent;
16379 		to.to_flags = TOF_TS;
16380 	}
16381 	optlen = tcp_addoptions(&to, opt);
16382 	hdrlen += optlen;
16383 	udp = rack->r_ctl.fsb.udp;
16384 	if (udp)
16385 		hdrlen += sizeof(struct udphdr);
16386 	if (rack->r_ctl.rc_pace_max_segs)
16387 		max_val = rack->r_ctl.rc_pace_max_segs;
16388 	else if (rack->rc_user_set_max_segs)
16389 		max_val = rack->rc_user_set_max_segs * segsiz;
16390 	else
16391 		max_val = len;
16392 	if ((tp->t_flags & TF_TSO) &&
16393 	    V_tcp_do_tso &&
16394 	    (len > segsiz) &&
16395 	    (tp->t_port == 0))
16396 		tso = 1;
16397 again:
16398 #ifdef INET6
16399 	if (MHLEN < hdrlen + max_linkhdr)
16400 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
16401 	else
16402 #endif
16403 		m = m_gethdr(M_NOWAIT, MT_DATA);
16404 	if (m == NULL)
16405 		goto failed;
16406 	m->m_data += max_linkhdr;
16407 	m->m_len = hdrlen;
16408 	th = rack->r_ctl.fsb.th;
16409 	/* Establish the len to send */
16410 	if (len > max_val)
16411 		len = max_val;
16412 	if ((tso) && (len + optlen > tp->t_maxseg)) {
16413 		uint32_t if_hw_tsomax;
16414 		int32_t max_len;
16415 
16416 		/* extract TSO information */
16417 		if_hw_tsomax = tp->t_tsomax;
16418 		if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
16419 		if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
16420 		/*
16421 		 * Check if we should limit by maximum payload
16422 		 * length:
16423 		 */
16424 		if (if_hw_tsomax != 0) {
16425 			/* compute maximum TSO length */
16426 			max_len = (if_hw_tsomax - hdrlen -
16427 				   max_linkhdr);
16428 			if (max_len <= 0) {
16429 				goto failed;
16430 			} else if (len > max_len) {
16431 				len = max_len;
16432 			}
16433 		}
16434 		if (len <= segsiz) {
16435 			/*
16436 			 * In case there are too many small fragments don't
16437 			 * use TSO:
16438 			 */
16439 			tso = 0;
16440 		}
16441 	} else {
16442 		tso = 0;
16443 	}
16444 	if ((tso == 0) && (len > segsiz))
16445 		len = segsiz;
16446 	if ((len == 0) ||
16447 	    (len <= MHLEN - hdrlen - max_linkhdr)) {
16448 		goto failed;
16449 	}
16450 	sb_offset = tp->snd_max - tp->snd_una;
16451 	th->th_seq = htonl(tp->snd_max);
16452 	th->th_ack = htonl(tp->rcv_nxt);
16453 	tcp_set_flags(th, flags);
16454 	th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale));
16455 	if (th->th_win == 0) {
16456 		tp->t_sndzerowin++;
16457 		tp->t_flags |= TF_RXWIN0SENT;
16458 	} else
16459 		tp->t_flags &= ~TF_RXWIN0SENT;
16460 	tp->snd_up = tp->snd_una;	/* drag it along, its deprecated */
16461 	KMOD_TCPSTAT_INC(tcps_sndpack);
16462 	KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
16463 #ifdef STATS
16464 	stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
16465 				 len);
16466 #endif
16467 	if (rack->r_ctl.fsb.m == NULL)
16468 		goto failed;
16469 
16470 	/* s_mb and s_soff are saved for rack_log_output */
16471 	m->m_next = rack_fo_m_copym(rack, &len, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize,
16472 				    &s_mb, &s_soff);
16473 	if (len <= segsiz) {
16474 		/*
16475 		 * Must have ran out of mbufs for the copy
16476 		 * shorten it to no longer need tso. Lets
16477 		 * not put on sendalot since we are low on
16478 		 * mbufs.
16479 		 */
16480 		tso = 0;
16481 	}
16482 	if (rack->r_ctl.fsb.rfo_apply_push &&
16483 	    (len == rack->r_ctl.fsb.left_to_send)) {
16484 		tcp_set_flags(th, flags | TH_PUSH);
16485 		add_flag |= RACK_HAD_PUSH;
16486 	}
16487 	if ((m->m_next == NULL) || (len <= 0)){
16488 		goto failed;
16489 	}
16490 	if (udp) {
16491 		if (rack->r_is_v6)
16492 			ulen = hdrlen + len - sizeof(struct ip6_hdr);
16493 		else
16494 			ulen = hdrlen + len - sizeof(struct ip);
16495 		udp->uh_ulen = htons(ulen);
16496 	}
16497 	m->m_pkthdr.rcvif = (struct ifnet *)0;
16498 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
16499 	    (tp->t_flags2 & TF2_ECN_PERMIT)) {
16500 		int ect = tcp_ecn_output_established(tp, &flags, len);
16501 		if ((tp->t_state == TCPS_SYN_RECEIVED) &&
16502 		    (tp->t_flags2 & TF2_ECN_SND_ECE))
16503 			tp->t_flags2 &= ~TF2_ECN_SND_ECE;
16504 #ifdef INET6
16505 		if (rack->r_is_v6) {
16506 			ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
16507 			ip6->ip6_flow |= htonl(ect << 20);
16508 		}
16509 		else
16510 #endif
16511 		{
16512 			ip->ip_tos &= ~IPTOS_ECN_MASK;
16513 			ip->ip_tos |= ect;
16514 		}
16515 	}
16516 	m->m_pkthdr.len = hdrlen + len;	/* in6_cksum() need this */
16517 #ifdef INET6
16518 	if (rack->r_is_v6) {
16519 		if (tp->t_port) {
16520 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
16521 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
16522 			udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
16523 			th->th_sum = htons(0);
16524 			UDPSTAT_INC(udps_opackets);
16525 		} else {
16526 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
16527 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
16528 			th->th_sum = in6_cksum_pseudo(ip6,
16529 						      sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
16530 						      0);
16531 		}
16532 	}
16533 #endif
16534 #if defined(INET6) && defined(INET)
16535 	else
16536 #endif
16537 #ifdef INET
16538 	{
16539 		if (tp->t_port) {
16540 			m->m_pkthdr.csum_flags = CSUM_UDP;
16541 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
16542 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
16543 						ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
16544 			th->th_sum = htons(0);
16545 			UDPSTAT_INC(udps_opackets);
16546 		} else {
16547 			m->m_pkthdr.csum_flags = CSUM_TCP;
16548 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
16549 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
16550 					       ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
16551 									IPPROTO_TCP + len + optlen));
16552 		}
16553 		/* IP version must be set here for ipv4/ipv6 checking later */
16554 		KASSERT(ip->ip_v == IPVERSION,
16555 			("%s: IP version incorrect: %d", __func__, ip->ip_v));
16556 	}
16557 #endif
16558 	if (tso) {
16559 		KASSERT(len > tp->t_maxseg - optlen,
16560 			("%s: len <= tso_segsz tp:%p", __func__, tp));
16561 		m->m_pkthdr.csum_flags |= CSUM_TSO;
16562 		m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen;
16563 	}
16564 #ifdef INET6
16565 	if (rack->r_is_v6) {
16566 		ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit;
16567 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
16568 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
16569 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
16570 		else
16571 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
16572 	}
16573 #endif
16574 #if defined(INET) && defined(INET6)
16575 	else
16576 #endif
16577 #ifdef INET
16578 	{
16579 		ip->ip_len = htons(m->m_pkthdr.len);
16580 		ip->ip_ttl = rack->r_ctl.fsb.hoplimit;
16581 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
16582 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
16583 			if (tp->t_port == 0 || len < V_tcp_minmss) {
16584 				ip->ip_off |= htons(IP_DF);
16585 			}
16586 		} else {
16587 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
16588 		}
16589 	}
16590 #endif
16591 	/* Time to copy in our header */
16592 	cpto = mtod(m, uint8_t *);
16593 	memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
16594 	th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
16595 	if (optlen) {
16596 		bcopy(opt, th + 1, optlen);
16597 		th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
16598 	} else {
16599 		th->th_off = sizeof(struct tcphdr) >> 2;
16600 	}
16601 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
16602 		union tcp_log_stackspecific log;
16603 
16604 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
16605 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
16606 		if (rack->rack_no_prr)
16607 			log.u_bbr.flex1 = 0;
16608 		else
16609 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
16610 		log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
16611 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
16612 		log.u_bbr.flex4 = max_val;
16613 		log.u_bbr.flex5 = 0;
16614 		/* Save off the early/late values */
16615 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
16616 		log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
16617 		log.u_bbr.bw_inuse = rack_get_bw(rack);
16618 		log.u_bbr.flex8 = 0;
16619 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
16620 		log.u_bbr.flex7 = 44;
16621 		log.u_bbr.pkts_out = tp->t_maxseg;
16622 		log.u_bbr.timeStamp = cts;
16623 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
16624 		log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use;
16625 		log.u_bbr.delivered = 0;
16626 		lgb = tcp_log_event_(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
16627 				     len, &log, false, NULL, NULL, 0, tv);
16628 	} else
16629 		lgb = NULL;
16630 #ifdef INET6
16631 	if (rack->r_is_v6) {
16632 		error = ip6_output(m, NULL,
16633 				   &inp->inp_route6,
16634 				   0, NULL, NULL, inp);
16635 	}
16636 #endif
16637 #if defined(INET) && defined(INET6)
16638 	else
16639 #endif
16640 #ifdef INET
16641 	{
16642 		error = ip_output(m, NULL,
16643 				  &inp->inp_route,
16644 				  0, 0, inp);
16645 	}
16646 #endif
16647 	if (lgb) {
16648 		lgb->tlb_errno = error;
16649 		lgb = NULL;
16650 	}
16651 	if (error) {
16652 		*send_err = error;
16653 		m = NULL;
16654 		goto failed;
16655 	}
16656 	rack_log_output(tp, &to, len, tp->snd_max, flags, error, rack_to_usec_ts(tv),
16657 			NULL, add_flag, s_mb, s_soff, rack->r_ctl.fsb.hw_tls);
16658 	m = NULL;
16659 	if (tp->snd_una == tp->snd_max) {
16660 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
16661 		rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
16662 		tp->t_acktime = ticks;
16663 	}
16664 	if (error == 0)
16665 		tcp_account_for_send(tp, len, 0, 0, rack->r_ctl.fsb.hw_tls);
16666 
16667 	rack->forced_ack = 0;	/* If we send something zap the FA flag */
16668 	tot_len += len;
16669 	if ((tp->t_flags & TF_GPUTINPROG) == 0)
16670 		rack_start_gp_measurement(tp, rack, tp->snd_max, sb_offset);
16671 	tp->snd_max += len;
16672 	tp->snd_nxt = tp->snd_max;
16673 	{
16674 		int idx;
16675 
16676 		idx = (len / segsiz) + 3;
16677 		if (idx >= TCP_MSS_ACCT_ATIMER)
16678 			counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
16679 		else
16680 			counter_u64_add(rack_out_size[idx], 1);
16681 	}
16682 	if (len <= rack->r_ctl.fsb.left_to_send)
16683 		rack->r_ctl.fsb.left_to_send -= len;
16684 	else
16685 		rack->r_ctl.fsb.left_to_send = 0;
16686 	if (rack->r_ctl.fsb.left_to_send < segsiz) {
16687 		rack->r_fast_output = 0;
16688 		rack->r_ctl.fsb.left_to_send = 0;
16689 		/* At the end of fast_output scale up the sb */
16690 		SOCKBUF_LOCK(&rack->rc_inp->inp_socket->so_snd);
16691 		rack_sndbuf_autoscale(rack);
16692 		SOCKBUF_UNLOCK(&rack->rc_inp->inp_socket->so_snd);
16693 	}
16694 	if (tp->t_rtttime == 0) {
16695 		tp->t_rtttime = ticks;
16696 		tp->t_rtseq = startseq;
16697 		KMOD_TCPSTAT_INC(tcps_segstimed);
16698 	}
16699 	if ((rack->r_ctl.fsb.left_to_send >= segsiz) &&
16700 	    (max_val > len) &&
16701 	    (tso == 0)) {
16702 		max_val -= len;
16703 		len = segsiz;
16704 		th = rack->r_ctl.fsb.th;
16705 		cnt_thru++;
16706 		goto again;
16707 	}
16708 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
16709 	counter_u64_add(rack_fto_send, 1);
16710 	slot = rack_get_pacing_delay(rack, tp, tot_len, NULL, segsiz);
16711 	rack_start_hpts_timer(rack, tp, cts, slot, tot_len, 0);
16712 #ifdef TCP_ACCOUNTING
16713 	crtsc = get_cyclecount();
16714 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16715 		tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru;
16716 	}
16717 	counter_u64_add(tcp_cnt_counters[SND_OUT_DATA], cnt_thru);
16718 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16719 		tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
16720 	}
16721 	counter_u64_add(tcp_proc_time[SND_OUT_DATA], (crtsc - ts_val));
16722 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16723 		tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len + segsiz - 1) / segsiz);
16724 	}
16725 	counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((tot_len + segsiz - 1) / segsiz));
16726 	sched_unpin();
16727 #endif
16728 	return (0);
16729 failed:
16730 	if (m)
16731 		m_free(m);
16732 	rack->r_fast_output = 0;
16733 	return (-1);
16734 }
16735 
16736 static int
16737 rack_output(struct tcpcb *tp)
16738 {
16739 	struct socket *so;
16740 	uint32_t recwin;
16741 	uint32_t sb_offset, s_moff = 0;
16742 	int32_t len, error = 0;
16743 	uint16_t flags;
16744 	struct mbuf *m, *s_mb = NULL;
16745 	struct mbuf *mb;
16746 	uint32_t if_hw_tsomaxsegcount = 0;
16747 	uint32_t if_hw_tsomaxsegsize;
16748 	int32_t segsiz, minseg;
16749 	long tot_len_this_send = 0;
16750 #ifdef INET
16751 	struct ip *ip = NULL;
16752 #endif
16753 #ifdef TCPDEBUG
16754 	struct ipovly *ipov = NULL;
16755 #endif
16756 	struct udphdr *udp = NULL;
16757 	struct tcp_rack *rack;
16758 	struct tcphdr *th;
16759 	uint8_t pass = 0;
16760 	uint8_t mark = 0;
16761 	uint8_t wanted_cookie = 0;
16762 	u_char opt[TCP_MAXOLEN];
16763 	unsigned ipoptlen, optlen, hdrlen, ulen=0;
16764 	uint32_t rack_seq;
16765 
16766 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
16767 	unsigned ipsec_optlen = 0;
16768 
16769 #endif
16770 	int32_t idle, sendalot;
16771 	int32_t sub_from_prr = 0;
16772 	volatile int32_t sack_rxmit;
16773 	struct rack_sendmap *rsm = NULL;
16774 	int32_t tso, mtu;
16775 	struct tcpopt to;
16776 	int32_t slot = 0;
16777 	int32_t sup_rack = 0;
16778 	uint32_t cts, ms_cts, delayed, early;
16779 	uint16_t add_flag = RACK_SENT_SP;
16780 	/* The doing_tlp flag will be set by the actual rack_timeout_tlp() */
16781 	uint8_t hpts_calling,  doing_tlp = 0;
16782 	uint32_t cwnd_to_use, pace_max_seg;
16783 	int32_t do_a_prefetch = 0;
16784 	int32_t prefetch_rsm = 0;
16785 	int32_t orig_len = 0;
16786 	struct timeval tv;
16787 	int32_t prefetch_so_done = 0;
16788 	struct tcp_log_buffer *lgb;
16789 	struct inpcb *inp;
16790 	struct sockbuf *sb;
16791 	uint64_t ts_val = 0;
16792 #ifdef TCP_ACCOUNTING
16793 	uint64_t crtsc;
16794 #endif
16795 #ifdef INET6
16796 	struct ip6_hdr *ip6 = NULL;
16797 	int32_t isipv6;
16798 #endif
16799 	uint8_t filled_all = 0;
16800 	bool hw_tls = false;
16801 
16802 	/* setup and take the cache hits here */
16803 	rack = (struct tcp_rack *)tp->t_fb_ptr;
16804 #ifdef TCP_ACCOUNTING
16805 	sched_pin();
16806 	ts_val = get_cyclecount();
16807 #endif
16808 	hpts_calling = rack->rc_inp->inp_hpts_calls;
16809 	NET_EPOCH_ASSERT();
16810 	INP_WLOCK_ASSERT(rack->rc_inp);
16811 #ifdef TCP_OFFLOAD
16812 	if (tp->t_flags & TF_TOE) {
16813 #ifdef TCP_ACCOUNTING
16814 		sched_unpin();
16815 #endif
16816 		return (tcp_offload_output(tp));
16817 	}
16818 #endif
16819 	/*
16820 	 * For TFO connections in SYN_RECEIVED, only allow the initial
16821 	 * SYN|ACK and those sent by the retransmit timer.
16822 	 */
16823 	if (IS_FASTOPEN(tp->t_flags) &&
16824 	    (tp->t_state == TCPS_SYN_RECEIVED) &&
16825 	    SEQ_GT(tp->snd_max, tp->snd_una) &&    /* initial SYN|ACK sent */
16826 	    (rack->r_ctl.rc_resend == NULL)) {         /* not a retransmit */
16827 #ifdef TCP_ACCOUNTING
16828 		sched_unpin();
16829 #endif
16830 		return (0);
16831 	}
16832 #ifdef INET6
16833 	if (rack->r_state) {
16834 		/* Use the cache line loaded if possible */
16835 		isipv6 = rack->r_is_v6;
16836 	} else {
16837 		isipv6 = (rack->rc_inp->inp_vflag & INP_IPV6) != 0;
16838 	}
16839 #endif
16840 	early = 0;
16841 	cts = tcp_get_usecs(&tv);
16842 	ms_cts = tcp_tv_to_mssectick(&tv);
16843 	if (((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) &&
16844 	    tcp_in_hpts(rack->rc_inp)) {
16845 		/*
16846 		 * We are on the hpts for some timer but not hptsi output.
16847 		 * Remove from the hpts unconditionally.
16848 		 */
16849 		rack_timer_cancel(tp, rack, cts, __LINE__);
16850 	}
16851 	/* Are we pacing and late? */
16852 	if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
16853 	    TSTMP_GEQ(cts, rack->r_ctl.rc_last_output_to)) {
16854 		/* We are delayed */
16855 		delayed = cts - rack->r_ctl.rc_last_output_to;
16856 	} else {
16857 		delayed = 0;
16858 	}
16859 	/* Do the timers, which may override the pacer */
16860 	if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
16861 		int retval;
16862 
16863 		retval = rack_process_timers(tp, rack, cts, hpts_calling,
16864 		    &doing_tlp);
16865 		if (retval != 0) {
16866 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_ATIMER], 1);
16867 #ifdef TCP_ACCOUNTING
16868 			sched_unpin();
16869 #endif
16870 			/*
16871 			 * If timers want tcp_drop(), then pass error out,
16872 			 * otherwise suppress it.
16873 			 */
16874 			return (retval < 0 ? retval : 0);
16875 		}
16876 	}
16877 	if (rack->rc_in_persist) {
16878 		if (tcp_in_hpts(rack->rc_inp) == 0) {
16879 			/* Timer is not running */
16880 			rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
16881 		}
16882 #ifdef TCP_ACCOUNTING
16883 		sched_unpin();
16884 #endif
16885 		return (0);
16886 	}
16887 	if ((rack->r_timer_override) ||
16888 	    (rack->rc_ack_can_sendout_data) ||
16889 	    (delayed) ||
16890 	    (tp->t_state < TCPS_ESTABLISHED)) {
16891 		rack->rc_ack_can_sendout_data = 0;
16892 		if (tcp_in_hpts(rack->rc_inp))
16893 			tcp_hpts_remove(rack->rc_inp);
16894 	} else if (tcp_in_hpts(rack->rc_inp)) {
16895 		/*
16896 		 * On the hpts you can't pass even if ACKNOW is on, we will
16897 		 * when the hpts fires.
16898 		 */
16899 #ifdef TCP_ACCOUNTING
16900 		crtsc = get_cyclecount();
16901 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16902 			tp->tcp_proc_time[SND_BLOCKED] += (crtsc - ts_val);
16903 		}
16904 		counter_u64_add(tcp_proc_time[SND_BLOCKED], (crtsc - ts_val));
16905 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16906 			tp->tcp_cnt_counters[SND_BLOCKED]++;
16907 		}
16908 		counter_u64_add(tcp_cnt_counters[SND_BLOCKED], 1);
16909 		sched_unpin();
16910 #endif
16911 		counter_u64_add(rack_out_size[TCP_MSS_ACCT_INPACE], 1);
16912 		return (0);
16913 	}
16914 	rack->rc_inp->inp_hpts_calls = 0;
16915 	/* Finish out both pacing early and late accounting */
16916 	if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
16917 	    TSTMP_GT(rack->r_ctl.rc_last_output_to, cts)) {
16918 		early = rack->r_ctl.rc_last_output_to - cts;
16919 	} else
16920 		early = 0;
16921 	if (delayed) {
16922 		rack->r_ctl.rc_agg_delayed += delayed;
16923 		rack->r_late = 1;
16924 	} else if (early) {
16925 		rack->r_ctl.rc_agg_early += early;
16926 		rack->r_early = 1;
16927 	}
16928 	/* Now that early/late accounting is done turn off the flag */
16929 	rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
16930 	rack->r_wanted_output = 0;
16931 	rack->r_timer_override = 0;
16932 	if ((tp->t_state != rack->r_state) &&
16933 	    TCPS_HAVEESTABLISHED(tp->t_state)) {
16934 		rack_set_state(tp, rack);
16935 	}
16936 	if ((rack->r_fast_output) &&
16937 	    (doing_tlp == 0) &&
16938 	    (tp->rcv_numsacks == 0)) {
16939 		int ret;
16940 
16941 		error = 0;
16942 		ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error);
16943 		if (ret >= 0)
16944 			return(ret);
16945 		else if (error) {
16946 			inp = rack->rc_inp;
16947 			so = inp->inp_socket;
16948 			sb = &so->so_snd;
16949 			goto nomore;
16950 		}
16951 	}
16952 	inp = rack->rc_inp;
16953 	/*
16954 	 * For TFO connections in SYN_SENT or SYN_RECEIVED,
16955 	 * only allow the initial SYN or SYN|ACK and those sent
16956 	 * by the retransmit timer.
16957 	 */
16958 	if (IS_FASTOPEN(tp->t_flags) &&
16959 	    ((tp->t_state == TCPS_SYN_RECEIVED) ||
16960 	     (tp->t_state == TCPS_SYN_SENT)) &&
16961 	    SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */
16962 	    (tp->t_rxtshift == 0)) {              /* not a retransmit */
16963 		cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
16964 		so = inp->inp_socket;
16965 		sb = &so->so_snd;
16966 		goto just_return_nolock;
16967 	}
16968 	/*
16969 	 * Determine length of data that should be transmitted, and flags
16970 	 * that will be used. If there is some data or critical controls
16971 	 * (SYN, RST) to send, then transmit; otherwise, investigate
16972 	 * further.
16973 	 */
16974 	idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
16975 	if (tp->t_idle_reduce) {
16976 		if (idle && ((ticks - tp->t_rcvtime) >= tp->t_rxtcur))
16977 			rack_cc_after_idle(rack, tp);
16978 	}
16979 	tp->t_flags &= ~TF_LASTIDLE;
16980 	if (idle) {
16981 		if (tp->t_flags & TF_MORETOCOME) {
16982 			tp->t_flags |= TF_LASTIDLE;
16983 			idle = 0;
16984 		}
16985 	}
16986 	if ((tp->snd_una == tp->snd_max) &&
16987 	    rack->r_ctl.rc_went_idle_time &&
16988 	    TSTMP_GT(cts, rack->r_ctl.rc_went_idle_time)) {
16989 		idle = cts - rack->r_ctl.rc_went_idle_time;
16990 		if (idle > rack_min_probertt_hold) {
16991 			/* Count as a probe rtt */
16992 			if (rack->in_probe_rtt == 0) {
16993 				rack->r_ctl.rc_lower_rtt_us_cts = cts;
16994 				rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts;
16995 				rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts;
16996 				rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts;
16997 			} else {
16998 				rack_exit_probertt(rack, cts);
16999 			}
17000 		}
17001 		idle = 0;
17002 	}
17003 	if (rack_use_fsb && (rack->r_fsb_inited == 0) && (rack->r_state != TCPS_CLOSED))
17004 		rack_init_fsb_block(tp, rack);
17005 again:
17006 	/*
17007 	 * If we've recently taken a timeout, snd_max will be greater than
17008 	 * snd_nxt.  There may be SACK information that allows us to avoid
17009 	 * resending already delivered data.  Adjust snd_nxt accordingly.
17010 	 */
17011 	sendalot = 0;
17012 	cts = tcp_get_usecs(&tv);
17013 	ms_cts = tcp_tv_to_mssectick(&tv);
17014 	tso = 0;
17015 	mtu = 0;
17016 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
17017 	minseg = segsiz;
17018 	if (rack->r_ctl.rc_pace_max_segs == 0)
17019 		pace_max_seg = rack->rc_user_set_max_segs * segsiz;
17020 	else
17021 		pace_max_seg = rack->r_ctl.rc_pace_max_segs;
17022 	sb_offset = tp->snd_max - tp->snd_una;
17023 	cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
17024 	flags = tcp_outflags[tp->t_state];
17025 	while (rack->rc_free_cnt < rack_free_cache) {
17026 		rsm = rack_alloc(rack);
17027 		if (rsm == NULL) {
17028 			if (inp->inp_hpts_calls)
17029 				/* Retry in a ms */
17030 				slot = (1 * HPTS_USEC_IN_MSEC);
17031 			so = inp->inp_socket;
17032 			sb = &so->so_snd;
17033 			goto just_return_nolock;
17034 		}
17035 		TAILQ_INSERT_TAIL(&rack->r_ctl.rc_free, rsm, r_tnext);
17036 		rack->rc_free_cnt++;
17037 		rsm = NULL;
17038 	}
17039 	if (inp->inp_hpts_calls)
17040 		inp->inp_hpts_calls = 0;
17041 	sack_rxmit = 0;
17042 	len = 0;
17043 	rsm = NULL;
17044 	if (flags & TH_RST) {
17045 		SOCKBUF_LOCK(&inp->inp_socket->so_snd);
17046 		so = inp->inp_socket;
17047 		sb = &so->so_snd;
17048 		goto send;
17049 	}
17050 	if (rack->r_ctl.rc_resend) {
17051 		/* Retransmit timer */
17052 		rsm = rack->r_ctl.rc_resend;
17053 		rack->r_ctl.rc_resend = NULL;
17054 		len = rsm->r_end - rsm->r_start;
17055 		sack_rxmit = 1;
17056 		sendalot = 0;
17057 		KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
17058 			("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
17059 			 __func__, __LINE__,
17060 			 rsm->r_start, tp->snd_una, tp, rack, rsm));
17061 		sb_offset = rsm->r_start - tp->snd_una;
17062 		if (len >= segsiz)
17063 			len = segsiz;
17064 	} else if ((rsm = tcp_rack_output(tp, rack, cts)) != NULL) {
17065 		/* We have a retransmit that takes precedence */
17066 		if ((!IN_FASTRECOVERY(tp->t_flags)) &&
17067 		    ((tp->t_flags & TF_WASFRECOVERY) == 0)) {
17068 			/* Enter recovery if not induced by a time-out */
17069 			rack->r_ctl.rc_rsm_start = rsm->r_start;
17070 			rack->r_ctl.rc_cwnd_at = tp->snd_cwnd;
17071 			rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh;
17072 			rack_cong_signal(tp, CC_NDUPACK, tp->snd_una);
17073 		}
17074 #ifdef INVARIANTS
17075 		if (SEQ_LT(rsm->r_start, tp->snd_una)) {
17076 			panic("Huh, tp:%p rack:%p rsm:%p start:%u < snd_una:%u\n",
17077 			      tp, rack, rsm, rsm->r_start, tp->snd_una);
17078 		}
17079 #endif
17080 		len = rsm->r_end - rsm->r_start;
17081 		KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
17082 			("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
17083 			 __func__, __LINE__,
17084 			 rsm->r_start, tp->snd_una, tp, rack, rsm));
17085 		sb_offset = rsm->r_start - tp->snd_una;
17086 		sendalot = 0;
17087 		if (len >= segsiz)
17088 			len = segsiz;
17089 		if (len > 0) {
17090 			sack_rxmit = 1;
17091 			KMOD_TCPSTAT_INC(tcps_sack_rexmits);
17092 			KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes,
17093 			    min(len, segsiz));
17094 			counter_u64_add(rack_rtm_prr_retran, 1);
17095 		}
17096 	} else if (rack->r_ctl.rc_tlpsend) {
17097 		/* Tail loss probe */
17098 		long cwin;
17099 		long tlen;
17100 
17101 		/*
17102 		 * Check if we can do a TLP with a RACK'd packet
17103 		 * this can happen if we are not doing the rack
17104 		 * cheat and we skipped to a TLP and it
17105 		 * went off.
17106 		 */
17107 		rsm = rack->r_ctl.rc_tlpsend;
17108 		/* We are doing a TLP make sure the flag is preent */
17109 		rsm->r_flags |= RACK_TLP;
17110 		rack->r_ctl.rc_tlpsend = NULL;
17111 		sack_rxmit = 1;
17112 		tlen = rsm->r_end - rsm->r_start;
17113 		if (tlen > segsiz)
17114 			tlen = segsiz;
17115 		KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
17116 			("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
17117 			 __func__, __LINE__,
17118 			 rsm->r_start, tp->snd_una, tp, rack, rsm));
17119 		sb_offset = rsm->r_start - tp->snd_una;
17120 		cwin = min(tp->snd_wnd, tlen);
17121 		len = cwin;
17122 	}
17123 	if (rack->r_must_retran &&
17124 	    (doing_tlp == 0) &&
17125 	    (rsm == NULL)) {
17126 		/*
17127 		 * Non-Sack and we had a RTO or Sack/non-Sack and a
17128 		 * MTU change, we need to retransmit until we reach
17129 		 * the former snd_max (rack->r_ctl.rc_snd_max_at_rto).
17130 		 */
17131 		if (SEQ_GT(tp->snd_max, tp->snd_una)) {
17132 			int sendwin, flight;
17133 
17134 			sendwin = min(tp->snd_wnd, tp->snd_cwnd);
17135 			flight = ctf_flight_size(tp, rack->r_ctl.rc_out_at_rto);
17136 			if (flight >= sendwin) {
17137 				so = inp->inp_socket;
17138 				sb = &so->so_snd;
17139 				goto just_return_nolock;
17140 			}
17141 			rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
17142 			if (rsm == NULL) {
17143 				/* TSNH */
17144 				rack->r_must_retran = 0;
17145 				rack->r_ctl.rc_out_at_rto = 0;
17146 				rack->r_must_retran = 0;
17147 				so = inp->inp_socket;
17148 				sb = &so->so_snd;
17149 				goto just_return_nolock;
17150 			}
17151 			if ((rsm->r_flags & RACK_MUST_RXT) == 0) {
17152 				/* It does not have the flag, we are done */
17153 				rack->r_must_retran = 0;
17154 				rack->r_ctl.rc_out_at_rto = 0;
17155 			} else {
17156 				sack_rxmit = 1;
17157 				len = rsm->r_end - rsm->r_start;
17158 				sendalot = 0;
17159 				sb_offset = rsm->r_start - tp->snd_una;
17160 				if (len >= segsiz)
17161 					len = segsiz;
17162 				/*
17163 				 * Delay removing the flag RACK_MUST_RXT so
17164 				 * that the fastpath for retransmit will
17165 				 * work with this rsm.
17166 				 */
17167 
17168 			}
17169 		} else {
17170 			/* We must be done if there is nothing outstanding */
17171 			rack->r_must_retran = 0;
17172 			rack->r_ctl.rc_out_at_rto = 0;
17173 		}
17174 	}
17175 	/*
17176 	 * Enforce a connection sendmap count limit if set
17177 	 * as long as we are not retransmiting.
17178 	 */
17179 	if ((rsm == NULL) &&
17180 	    (rack->do_detection == 0) &&
17181 	    (V_tcp_map_entries_limit > 0) &&
17182 	    (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
17183 		counter_u64_add(rack_to_alloc_limited, 1);
17184 		if (!rack->alloc_limit_reported) {
17185 			rack->alloc_limit_reported = 1;
17186 			counter_u64_add(rack_alloc_limited_conns, 1);
17187 		}
17188 		so = inp->inp_socket;
17189 		sb = &so->so_snd;
17190 		goto just_return_nolock;
17191 	}
17192 	if (rsm && (rsm->r_flags & RACK_HAS_FIN)) {
17193 		/* we are retransmitting the fin */
17194 		len--;
17195 		if (len) {
17196 			/*
17197 			 * When retransmitting data do *not* include the
17198 			 * FIN. This could happen from a TLP probe.
17199 			 */
17200 			flags &= ~TH_FIN;
17201 		}
17202 	}
17203 #ifdef INVARIANTS
17204 	/* For debugging */
17205 	rack->r_ctl.rc_rsm_at_retran = rsm;
17206 #endif
17207 	if (rsm && rack->r_fsb_inited && rack_use_rsm_rfo &&
17208 	    ((rsm->r_flags & RACK_HAS_FIN) == 0)) {
17209 		int ret;
17210 
17211 		ret = rack_fast_rsm_output(tp, rack, rsm, ts_val, cts, ms_cts, &tv, len, doing_tlp);
17212 		if (ret == 0)
17213 			return (0);
17214 	}
17215 	if (rsm && (rsm->r_flags & RACK_MUST_RXT)) {
17216 		/*
17217 		 * Clear the flag in prep for the send
17218 		 * note that if we can't get an mbuf
17219 		 * and fail, we won't retransmit this
17220 		 * rsm but that should be ok (its rare).
17221 		 */
17222 		rsm->r_flags &= ~RACK_MUST_RXT;
17223 	}
17224 	so = inp->inp_socket;
17225 	sb = &so->so_snd;
17226 	if (do_a_prefetch == 0) {
17227 		kern_prefetch(sb, &do_a_prefetch);
17228 		do_a_prefetch = 1;
17229 	}
17230 #ifdef NETFLIX_SHARED_CWND
17231 	if ((tp->t_flags2 & TF2_TCP_SCWND_ALLOWED) &&
17232 	    rack->rack_enable_scwnd) {
17233 		/* We are doing cwnd sharing */
17234 		if (rack->gp_ready &&
17235 		    (rack->rack_attempted_scwnd == 0) &&
17236 		    (rack->r_ctl.rc_scw == NULL) &&
17237 		    tp->t_lib) {
17238 			/* The pcbid is in, lets make an attempt */
17239 			counter_u64_add(rack_try_scwnd, 1);
17240 			rack->rack_attempted_scwnd = 1;
17241 			rack->r_ctl.rc_scw = tcp_shared_cwnd_alloc(tp,
17242 								   &rack->r_ctl.rc_scw_index,
17243 								   segsiz);
17244 		}
17245 		if (rack->r_ctl.rc_scw &&
17246 		    (rack->rack_scwnd_is_idle == 1) &&
17247 		    sbavail(&so->so_snd)) {
17248 			/* we are no longer out of data */
17249 			tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
17250 			rack->rack_scwnd_is_idle = 0;
17251 		}
17252 		if (rack->r_ctl.rc_scw) {
17253 			/* First lets update and get the cwnd */
17254 			rack->r_ctl.cwnd_to_use = cwnd_to_use = tcp_shared_cwnd_update(rack->r_ctl.rc_scw,
17255 								    rack->r_ctl.rc_scw_index,
17256 								    tp->snd_cwnd, tp->snd_wnd, segsiz);
17257 		}
17258 	}
17259 #endif
17260 	/*
17261 	 * Get standard flags, and add SYN or FIN if requested by 'hidden'
17262 	 * state flags.
17263 	 */
17264 	if (tp->t_flags & TF_NEEDFIN)
17265 		flags |= TH_FIN;
17266 	if (tp->t_flags & TF_NEEDSYN)
17267 		flags |= TH_SYN;
17268 	if ((sack_rxmit == 0) && (prefetch_rsm == 0)) {
17269 		void *end_rsm;
17270 		end_rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
17271 		if (end_rsm)
17272 			kern_prefetch(end_rsm, &prefetch_rsm);
17273 		prefetch_rsm = 1;
17274 	}
17275 	SOCKBUF_LOCK(sb);
17276 	/*
17277 	 * If snd_nxt == snd_max and we have transmitted a FIN, the
17278 	 * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a
17279 	 * negative length.  This can also occur when TCP opens up its
17280 	 * congestion window while receiving additional duplicate acks after
17281 	 * fast-retransmit because TCP will reset snd_nxt to snd_max after
17282 	 * the fast-retransmit.
17283 	 *
17284 	 * In the normal retransmit-FIN-only case, however, snd_nxt will be
17285 	 * set to snd_una, the sb_offset will be 0, and the length may wind
17286 	 * up 0.
17287 	 *
17288 	 * If sack_rxmit is true we are retransmitting from the scoreboard
17289 	 * in which case len is already set.
17290 	 */
17291 	if ((sack_rxmit == 0) &&
17292 	    (TCPS_HAVEESTABLISHED(tp->t_state) || IS_FASTOPEN(tp->t_flags))) {
17293 		uint32_t avail;
17294 
17295 		avail = sbavail(sb);
17296 		if (SEQ_GT(tp->snd_nxt, tp->snd_una) && avail)
17297 			sb_offset = tp->snd_nxt - tp->snd_una;
17298 		else
17299 			sb_offset = 0;
17300 		if ((IN_FASTRECOVERY(tp->t_flags) == 0) || rack->rack_no_prr) {
17301 			if (rack->r_ctl.rc_tlp_new_data) {
17302 				/* TLP is forcing out new data */
17303 				if (rack->r_ctl.rc_tlp_new_data > (uint32_t) (avail - sb_offset)) {
17304 					rack->r_ctl.rc_tlp_new_data = (uint32_t) (avail - sb_offset);
17305 				}
17306 				if ((rack->r_ctl.rc_tlp_new_data + sb_offset) > tp->snd_wnd) {
17307 					if (tp->snd_wnd > sb_offset)
17308 						len = tp->snd_wnd - sb_offset;
17309 					else
17310 						len = 0;
17311 				} else {
17312 					len = rack->r_ctl.rc_tlp_new_data;
17313 				}
17314 				rack->r_ctl.rc_tlp_new_data = 0;
17315 			}  else {
17316 				len = rack_what_can_we_send(tp, rack, cwnd_to_use, avail, sb_offset);
17317 			}
17318 			if ((rack->r_ctl.crte == NULL) && IN_FASTRECOVERY(tp->t_flags) && (len > segsiz)) {
17319 				/*
17320 				 * For prr=off, we need to send only 1 MSS
17321 				 * at a time. We do this because another sack could
17322 				 * be arriving that causes us to send retransmits and
17323 				 * we don't want to be on a long pace due to a larger send
17324 				 * that keeps us from sending out the retransmit.
17325 				 */
17326 				len = segsiz;
17327 			}
17328 		} else {
17329 			uint32_t outstanding;
17330 			/*
17331 			 * We are inside of a Fast recovery episode, this
17332 			 * is caused by a SACK or 3 dup acks. At this point
17333 			 * we have sent all the retransmissions and we rely
17334 			 * on PRR to dictate what we will send in the form of
17335 			 * new data.
17336 			 */
17337 
17338 			outstanding = tp->snd_max - tp->snd_una;
17339 			if ((rack->r_ctl.rc_prr_sndcnt + outstanding) > tp->snd_wnd) {
17340 				if (tp->snd_wnd > outstanding) {
17341 					len = tp->snd_wnd - outstanding;
17342 					/* Check to see if we have the data */
17343 					if ((sb_offset + len) > avail) {
17344 						/* It does not all fit */
17345 						if (avail > sb_offset)
17346 							len = avail - sb_offset;
17347 						else
17348 							len = 0;
17349 					}
17350 				} else {
17351 					len = 0;
17352 				}
17353 			} else if (avail > sb_offset) {
17354 				len = avail - sb_offset;
17355 			} else {
17356 				len = 0;
17357 			}
17358 			if (len > 0) {
17359 				if (len > rack->r_ctl.rc_prr_sndcnt) {
17360 					len = rack->r_ctl.rc_prr_sndcnt;
17361 				}
17362 				if (len > 0) {
17363 					sub_from_prr = 1;
17364 					counter_u64_add(rack_rtm_prr_newdata, 1);
17365 				}
17366 			}
17367 			if (len > segsiz) {
17368 				/*
17369 				 * We should never send more than a MSS when
17370 				 * retransmitting or sending new data in prr
17371 				 * mode unless the override flag is on. Most
17372 				 * likely the PRR algorithm is not going to
17373 				 * let us send a lot as well :-)
17374 				 */
17375 				if (rack->r_ctl.rc_prr_sendalot == 0) {
17376 					len = segsiz;
17377 				}
17378 			} else if (len < segsiz) {
17379 				/*
17380 				 * Do we send any? The idea here is if the
17381 				 * send empty's the socket buffer we want to
17382 				 * do it. However if not then lets just wait
17383 				 * for our prr_sndcnt to get bigger.
17384 				 */
17385 				long leftinsb;
17386 
17387 				leftinsb = sbavail(sb) - sb_offset;
17388 				if (leftinsb > len) {
17389 					/* This send does not empty the sb */
17390 					len = 0;
17391 				}
17392 			}
17393 		}
17394 	} else if (!TCPS_HAVEESTABLISHED(tp->t_state)) {
17395 		/*
17396 		 * If you have not established
17397 		 * and are not doing FAST OPEN
17398 		 * no data please.
17399 		 */
17400 		if ((sack_rxmit == 0) &&
17401 		    (!IS_FASTOPEN(tp->t_flags))){
17402 			len = 0;
17403 			sb_offset = 0;
17404 		}
17405 	}
17406 	if (prefetch_so_done == 0) {
17407 		kern_prefetch(so, &prefetch_so_done);
17408 		prefetch_so_done = 1;
17409 	}
17410 	/*
17411 	 * Lop off SYN bit if it has already been sent.  However, if this is
17412 	 * SYN-SENT state and if segment contains data and if we don't know
17413 	 * that foreign host supports TAO, suppress sending segment.
17414 	 */
17415 	if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una) &&
17416 	    ((sack_rxmit == 0) && (tp->t_rxtshift == 0))) {
17417 		/*
17418 		 * When sending additional segments following a TFO SYN|ACK,
17419 		 * do not include the SYN bit.
17420 		 */
17421 		if (IS_FASTOPEN(tp->t_flags) &&
17422 		    (tp->t_state == TCPS_SYN_RECEIVED))
17423 			flags &= ~TH_SYN;
17424 	}
17425 	/*
17426 	 * Be careful not to send data and/or FIN on SYN segments. This
17427 	 * measure is needed to prevent interoperability problems with not
17428 	 * fully conformant TCP implementations.
17429 	 */
17430 	if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
17431 		len = 0;
17432 		flags &= ~TH_FIN;
17433 	}
17434 	/*
17435 	 * On TFO sockets, ensure no data is sent in the following cases:
17436 	 *
17437 	 *  - When retransmitting SYN|ACK on a passively-created socket
17438 	 *
17439 	 *  - When retransmitting SYN on an actively created socket
17440 	 *
17441 	 *  - When sending a zero-length cookie (cookie request) on an
17442 	 *    actively created socket
17443 	 *
17444 	 *  - When the socket is in the CLOSED state (RST is being sent)
17445 	 */
17446 	if (IS_FASTOPEN(tp->t_flags) &&
17447 	    (((flags & TH_SYN) && (tp->t_rxtshift > 0)) ||
17448 	     ((tp->t_state == TCPS_SYN_SENT) &&
17449 	      (tp->t_tfo_client_cookie_len == 0)) ||
17450 	     (flags & TH_RST))) {
17451 		sack_rxmit = 0;
17452 		len = 0;
17453 	}
17454 	/* Without fast-open there should never be data sent on a SYN */
17455 	if ((flags & TH_SYN) && (!IS_FASTOPEN(tp->t_flags))) {
17456 		tp->snd_nxt = tp->iss;
17457 		len = 0;
17458 	}
17459 	if ((len > segsiz) && (tcp_dsack_block_exists(tp))) {
17460 		/* We only send 1 MSS if we have a DSACK block */
17461 		add_flag |= RACK_SENT_W_DSACK;
17462 		len = segsiz;
17463 	}
17464 	orig_len = len;
17465 	if (len <= 0) {
17466 		/*
17467 		 * If FIN has been sent but not acked, but we haven't been
17468 		 * called to retransmit, len will be < 0.  Otherwise, window
17469 		 * shrank after we sent into it.  If window shrank to 0,
17470 		 * cancel pending retransmit, pull snd_nxt back to (closed)
17471 		 * window, and set the persist timer if it isn't already
17472 		 * going.  If the window didn't close completely, just wait
17473 		 * for an ACK.
17474 		 *
17475 		 * We also do a general check here to ensure that we will
17476 		 * set the persist timer when we have data to send, but a
17477 		 * 0-byte window. This makes sure the persist timer is set
17478 		 * even if the packet hits one of the "goto send" lines
17479 		 * below.
17480 		 */
17481 		len = 0;
17482 		if ((tp->snd_wnd == 0) &&
17483 		    (TCPS_HAVEESTABLISHED(tp->t_state)) &&
17484 		    (tp->snd_una == tp->snd_max) &&
17485 		    (sb_offset < (int)sbavail(sb))) {
17486 			rack_enter_persist(tp, rack, cts);
17487 		}
17488 	} else if ((rsm == NULL) &&
17489 		   (doing_tlp == 0) &&
17490 		   (len < pace_max_seg)) {
17491 		/*
17492 		 * We are not sending a maximum sized segment for
17493 		 * some reason. Should we not send anything (think
17494 		 * sws or persists)?
17495 		 */
17496 		if ((tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg)) &&
17497 		    (TCPS_HAVEESTABLISHED(tp->t_state)) &&
17498 		    (len < minseg) &&
17499 		    (len < (int)(sbavail(sb) - sb_offset))) {
17500 			/*
17501 			 * Here the rwnd is less than
17502 			 * the minimum pacing size, this is not a retransmit,
17503 			 * we are established and
17504 			 * the send is not the last in the socket buffer
17505 			 * we send nothing, and we may enter persists
17506 			 * if nothing is outstanding.
17507 			 */
17508 			len = 0;
17509 			if (tp->snd_max == tp->snd_una) {
17510 				/*
17511 				 * Nothing out we can
17512 				 * go into persists.
17513 				 */
17514 				rack_enter_persist(tp, rack, cts);
17515 			}
17516 		     } else if ((cwnd_to_use >= max(minseg, (segsiz * 4))) &&
17517 			   (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) &&
17518 			   (len < (int)(sbavail(sb) - sb_offset)) &&
17519 			   (len < minseg)) {
17520 			/*
17521 			 * Here we are not retransmitting, and
17522 			 * the cwnd is not so small that we could
17523 			 * not send at least a min size (rxt timer
17524 			 * not having gone off), We have 2 segments or
17525 			 * more already in flight, its not the tail end
17526 			 * of the socket buffer  and the cwnd is blocking
17527 			 * us from sending out a minimum pacing segment size.
17528 			 * Lets not send anything.
17529 			 */
17530 			len = 0;
17531 		} else if (((tp->snd_wnd - ctf_outstanding(tp)) <
17532 			    min((rack->r_ctl.rc_high_rwnd/2), minseg)) &&
17533 			   (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) &&
17534 			   (len < (int)(sbavail(sb) - sb_offset)) &&
17535 			   (TCPS_HAVEESTABLISHED(tp->t_state))) {
17536 			/*
17537 			 * Here we have a send window but we have
17538 			 * filled it up and we can't send another pacing segment.
17539 			 * We also have in flight more than 2 segments
17540 			 * and we are not completing the sb i.e. we allow
17541 			 * the last bytes of the sb to go out even if
17542 			 * its not a full pacing segment.
17543 			 */
17544 			len = 0;
17545 		} else if ((rack->r_ctl.crte != NULL) &&
17546 			   (tp->snd_wnd >= (pace_max_seg * max(1, rack_hw_rwnd_factor))) &&
17547 			   (cwnd_to_use >= (pace_max_seg + (4 * segsiz))) &&
17548 			   (ctf_flight_size(tp, rack->r_ctl.rc_sacked) >= (2 * segsiz)) &&
17549 			   (len < (int)(sbavail(sb) - sb_offset))) {
17550 			/*
17551 			 * Here we are doing hardware pacing, this is not a TLP,
17552 			 * we are not sending a pace max segment size, there is rwnd
17553 			 * room to send at least N pace_max_seg, the cwnd is greater
17554 			 * than or equal to a full pacing segments plus 4 mss and we have 2 or
17555 			 * more segments in flight and its not the tail of the socket buffer.
17556 			 *
17557 			 * We don't want to send instead we need to get more ack's in to
17558 			 * allow us to send a full pacing segment. Normally, if we are pacing
17559 			 * about the right speed, we should have finished our pacing
17560 			 * send as most of the acks have come back if we are at the
17561 			 * right rate. This is a bit fuzzy since return path delay
17562 			 * can delay the acks, which is why we want to make sure we
17563 			 * have cwnd space to have a bit more than a max pace segments in flight.
17564 			 *
17565 			 * If we have not gotten our acks back we are pacing at too high a
17566 			 * rate delaying will not hurt and will bring our GP estimate down by
17567 			 * injecting the delay. If we don't do this we will send
17568 			 * 2 MSS out in response to the acks being clocked in which
17569 			 * defeats the point of hw-pacing (i.e. to help us get
17570 			 * larger TSO's out).
17571 			 */
17572 			len = 0;
17573 
17574 		}
17575 
17576 	}
17577 	/* len will be >= 0 after this point. */
17578 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
17579 	rack_sndbuf_autoscale(rack);
17580 	/*
17581 	 * Decide if we can use TCP Segmentation Offloading (if supported by
17582 	 * hardware).
17583 	 *
17584 	 * TSO may only be used if we are in a pure bulk sending state.  The
17585 	 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP
17586 	 * options prevent using TSO.  With TSO the TCP header is the same
17587 	 * (except for the sequence number) for all generated packets.  This
17588 	 * makes it impossible to transmit any options which vary per
17589 	 * generated segment or packet.
17590 	 *
17591 	 * IPv4 handling has a clear separation of ip options and ip header
17592 	 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does
17593 	 * the right thing below to provide length of just ip options and thus
17594 	 * checking for ipoptlen is enough to decide if ip options are present.
17595 	 */
17596 	ipoptlen = 0;
17597 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
17598 	/*
17599 	 * Pre-calculate here as we save another lookup into the darknesses
17600 	 * of IPsec that way and can actually decide if TSO is ok.
17601 	 */
17602 #ifdef INET6
17603 	if (isipv6 && IPSEC_ENABLED(ipv6))
17604 		ipsec_optlen = IPSEC_HDRSIZE(ipv6, tp->t_inpcb);
17605 #ifdef INET
17606 	else
17607 #endif
17608 #endif				/* INET6 */
17609 #ifdef INET
17610 		if (IPSEC_ENABLED(ipv4))
17611 			ipsec_optlen = IPSEC_HDRSIZE(ipv4, tp->t_inpcb);
17612 #endif				/* INET */
17613 #endif
17614 
17615 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
17616 	ipoptlen += ipsec_optlen;
17617 #endif
17618 	if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > segsiz &&
17619 	    (tp->t_port == 0) &&
17620 	    ((tp->t_flags & TF_SIGNATURE) == 0) &&
17621 	    tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
17622 	    ipoptlen == 0)
17623 		tso = 1;
17624 	{
17625 		uint32_t outstanding;
17626 
17627 		outstanding = tp->snd_max - tp->snd_una;
17628 		if (tp->t_flags & TF_SENTFIN) {
17629 			/*
17630 			 * If we sent a fin, snd_max is 1 higher than
17631 			 * snd_una
17632 			 */
17633 			outstanding--;
17634 		}
17635 		if (sack_rxmit) {
17636 			if ((rsm->r_flags & RACK_HAS_FIN) == 0)
17637 				flags &= ~TH_FIN;
17638 		} else {
17639 			if (SEQ_LT(tp->snd_nxt + len, tp->snd_una +
17640 				   sbused(sb)))
17641 				flags &= ~TH_FIN;
17642 		}
17643 	}
17644 	recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
17645 	    (long)TCP_MAXWIN << tp->rcv_scale);
17646 
17647 	/*
17648 	 * Sender silly window avoidance.   We transmit under the following
17649 	 * conditions when len is non-zero:
17650 	 *
17651 	 * - We have a full segment (or more with TSO) - This is the last
17652 	 * buffer in a write()/send() and we are either idle or running
17653 	 * NODELAY - we've timed out (e.g. persist timer) - we have more
17654 	 * then 1/2 the maximum send window's worth of data (receiver may be
17655 	 * limited the window size) - we need to retransmit
17656 	 */
17657 	if (len) {
17658 		if (len >= segsiz) {
17659 			goto send;
17660 		}
17661 		/*
17662 		 * NOTE! on localhost connections an 'ack' from the remote
17663 		 * end may occur synchronously with the output and cause us
17664 		 * to flush a buffer queued with moretocome.  XXX
17665 		 *
17666 		 */
17667 		if (!(tp->t_flags & TF_MORETOCOME) &&	/* normal case */
17668 		    (idle || (tp->t_flags & TF_NODELAY)) &&
17669 		    ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) &&
17670 		    (tp->t_flags & TF_NOPUSH) == 0) {
17671 			pass = 2;
17672 			goto send;
17673 		}
17674 		if ((tp->snd_una == tp->snd_max) && len) {	/* Nothing outstanding */
17675 			pass = 22;
17676 			goto send;
17677 		}
17678 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) {
17679 			pass = 4;
17680 			goto send;
17681 		}
17682 		if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {	/* retransmit case */
17683 			pass = 5;
17684 			goto send;
17685 		}
17686 		if (sack_rxmit) {
17687 			pass = 6;
17688 			goto send;
17689 		}
17690 		if (((tp->snd_wnd - ctf_outstanding(tp)) < segsiz) &&
17691 		    (ctf_outstanding(tp) < (segsiz * 2))) {
17692 			/*
17693 			 * We have less than two MSS outstanding (delayed ack)
17694 			 * and our rwnd will not let us send a full sized
17695 			 * MSS. Lets go ahead and let this small segment
17696 			 * out because we want to try to have at least two
17697 			 * packets inflight to not be caught by delayed ack.
17698 			 */
17699 			pass = 12;
17700 			goto send;
17701 		}
17702 	}
17703 	/*
17704 	 * Sending of standalone window updates.
17705 	 *
17706 	 * Window updates are important when we close our window due to a
17707 	 * full socket buffer and are opening it again after the application
17708 	 * reads data from it.  Once the window has opened again and the
17709 	 * remote end starts to send again the ACK clock takes over and
17710 	 * provides the most current window information.
17711 	 *
17712 	 * We must avoid the silly window syndrome whereas every read from
17713 	 * the receive buffer, no matter how small, causes a window update
17714 	 * to be sent.  We also should avoid sending a flurry of window
17715 	 * updates when the socket buffer had queued a lot of data and the
17716 	 * application is doing small reads.
17717 	 *
17718 	 * Prevent a flurry of pointless window updates by only sending an
17719 	 * update when we can increase the advertized window by more than
17720 	 * 1/4th of the socket buffer capacity.  When the buffer is getting
17721 	 * full or is very small be more aggressive and send an update
17722 	 * whenever we can increase by two mss sized segments. In all other
17723 	 * situations the ACK's to new incoming data will carry further
17724 	 * window increases.
17725 	 *
17726 	 * Don't send an independent window update if a delayed ACK is
17727 	 * pending (it will get piggy-backed on it) or the remote side
17728 	 * already has done a half-close and won't send more data.  Skip
17729 	 * this if the connection is in T/TCP half-open state.
17730 	 */
17731 	if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
17732 	    !(tp->t_flags & TF_DELACK) &&
17733 	    !TCPS_HAVERCVDFIN(tp->t_state)) {
17734 		/*
17735 		 * "adv" is the amount we could increase the window, taking
17736 		 * into account that we are limited by TCP_MAXWIN <<
17737 		 * tp->rcv_scale.
17738 		 */
17739 		int32_t adv;
17740 		int oldwin;
17741 
17742 		adv = recwin;
17743 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
17744 			oldwin = (tp->rcv_adv - tp->rcv_nxt);
17745 			if (adv > oldwin)
17746 			    adv -= oldwin;
17747 			else {
17748 				/* We can't increase the window */
17749 				adv = 0;
17750 			}
17751 		} else
17752 			oldwin = 0;
17753 
17754 		/*
17755 		 * If the new window size ends up being the same as or less
17756 		 * than the old size when it is scaled, then don't force
17757 		 * a window update.
17758 		 */
17759 		if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale)
17760 			goto dontupdate;
17761 
17762 		if (adv >= (int32_t)(2 * segsiz) &&
17763 		    (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) ||
17764 		     recwin <= (int32_t)(so->so_rcv.sb_hiwat / 8) ||
17765 		     so->so_rcv.sb_hiwat <= 8 * segsiz)) {
17766 			pass = 7;
17767 			goto send;
17768 		}
17769 		if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat) {
17770 			pass = 23;
17771 			goto send;
17772 		}
17773 	}
17774 dontupdate:
17775 
17776 	/*
17777 	 * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
17778 	 * is also a catch-all for the retransmit timer timeout case.
17779 	 */
17780 	if (tp->t_flags & TF_ACKNOW) {
17781 		pass = 8;
17782 		goto send;
17783 	}
17784 	if (((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) {
17785 		pass = 9;
17786 		goto send;
17787 	}
17788 	/*
17789 	 * If our state indicates that FIN should be sent and we have not
17790 	 * yet done so, then we need to send.
17791 	 */
17792 	if ((flags & TH_FIN) &&
17793 	    (tp->snd_nxt == tp->snd_una)) {
17794 		pass = 11;
17795 		goto send;
17796 	}
17797 	/*
17798 	 * No reason to send a segment, just return.
17799 	 */
17800 just_return:
17801 	SOCKBUF_UNLOCK(sb);
17802 just_return_nolock:
17803 	{
17804 		int app_limited = CTF_JR_SENT_DATA;
17805 
17806 		if (tot_len_this_send > 0) {
17807 			/* Make sure snd_nxt is up to max */
17808 			rack->r_ctl.fsb.recwin = recwin;
17809 			slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, NULL, segsiz);
17810 			if ((error == 0) &&
17811 			    rack_use_rfo &&
17812 			    ((flags & (TH_SYN|TH_FIN)) == 0) &&
17813 			    (ipoptlen == 0) &&
17814 			    (tp->snd_nxt == tp->snd_max) &&
17815 			    (tp->rcv_numsacks == 0) &&
17816 			    rack->r_fsb_inited &&
17817 			    TCPS_HAVEESTABLISHED(tp->t_state) &&
17818 			    (rack->r_must_retran == 0) &&
17819 			    ((tp->t_flags & TF_NEEDFIN) == 0) &&
17820 			    (len > 0) && (orig_len > 0) &&
17821 			    (orig_len > len) &&
17822 			    ((orig_len - len) >= segsiz) &&
17823 			    ((optlen == 0) ||
17824 			     ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
17825 				/* We can send at least one more MSS using our fsb */
17826 
17827 				rack->r_fast_output = 1;
17828 				rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
17829 				rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
17830 				rack->r_ctl.fsb.tcp_flags = flags;
17831 				rack->r_ctl.fsb.left_to_send = orig_len - len;
17832 				if (hw_tls)
17833 					rack->r_ctl.fsb.hw_tls = 1;
17834 				else
17835 					rack->r_ctl.fsb.hw_tls = 0;
17836 				KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(sb) - (tp->snd_max - tp->snd_una))),
17837 					("rack:%p left_to_send:%u sbavail:%u out:%u",
17838 					rack, rack->r_ctl.fsb.left_to_send, sbavail(sb),
17839 					 (tp->snd_max - tp->snd_una)));
17840 				if (rack->r_ctl.fsb.left_to_send < segsiz)
17841 					rack->r_fast_output = 0;
17842 				else {
17843 					if (rack->r_ctl.fsb.left_to_send == (sbavail(sb) - (tp->snd_max - tp->snd_una)))
17844 						rack->r_ctl.fsb.rfo_apply_push = 1;
17845 					else
17846 						rack->r_ctl.fsb.rfo_apply_push = 0;
17847 				}
17848 			} else
17849 				rack->r_fast_output = 0;
17850 
17851 
17852 			rack_log_fsb(rack, tp, so, flags,
17853 				     ipoptlen, orig_len, len, 0,
17854 				     1, optlen, __LINE__, 1);
17855 			if (SEQ_GT(tp->snd_max, tp->snd_nxt))
17856 				tp->snd_nxt = tp->snd_max;
17857 		} else {
17858 			int end_window = 0;
17859 			uint32_t seq = tp->gput_ack;
17860 
17861 			rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
17862 			if (rsm) {
17863 				/*
17864 				 * Mark the last sent that we just-returned (hinting
17865 				 * that delayed ack may play a role in any rtt measurement).
17866 				 */
17867 				rsm->r_just_ret = 1;
17868 			}
17869 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_JUSTRET], 1);
17870 			rack->r_ctl.rc_agg_delayed = 0;
17871 			rack->r_early = 0;
17872 			rack->r_late = 0;
17873 			rack->r_ctl.rc_agg_early = 0;
17874 			if ((ctf_outstanding(tp) +
17875 			     min(max(segsiz, (rack->r_ctl.rc_high_rwnd/2)),
17876 				 minseg)) >= tp->snd_wnd) {
17877 				/* We are limited by the rwnd */
17878 				app_limited = CTF_JR_RWND_LIMITED;
17879 				if (IN_FASTRECOVERY(tp->t_flags))
17880 				    rack->r_ctl.rc_prr_sndcnt = 0;
17881 			} else if (ctf_outstanding(tp) >= sbavail(sb)) {
17882 				/* We are limited by whats available -- app limited */
17883 				app_limited = CTF_JR_APP_LIMITED;
17884 				if (IN_FASTRECOVERY(tp->t_flags))
17885 				    rack->r_ctl.rc_prr_sndcnt = 0;
17886 			} else if ((idle == 0) &&
17887 				   ((tp->t_flags & TF_NODELAY) == 0) &&
17888 				   ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) &&
17889 				   (len < segsiz)) {
17890 				/*
17891 				 * No delay is not on and the
17892 				 * user is sending less than 1MSS. This
17893 				 * brings out SWS avoidance so we
17894 				 * don't send. Another app-limited case.
17895 				 */
17896 				app_limited = CTF_JR_APP_LIMITED;
17897 			} else if (tp->t_flags & TF_NOPUSH) {
17898 				/*
17899 				 * The user has requested no push of
17900 				 * the last segment and we are
17901 				 * at the last segment. Another app
17902 				 * limited case.
17903 				 */
17904 				app_limited = CTF_JR_APP_LIMITED;
17905 			} else if ((ctf_outstanding(tp) + minseg) > cwnd_to_use) {
17906 				/* Its the cwnd */
17907 				app_limited = CTF_JR_CWND_LIMITED;
17908 			} else if (IN_FASTRECOVERY(tp->t_flags) &&
17909 				   (rack->rack_no_prr == 0) &&
17910 				   (rack->r_ctl.rc_prr_sndcnt < segsiz)) {
17911 				app_limited = CTF_JR_PRR;
17912 			} else {
17913 				/* Now why here are we not sending? */
17914 #ifdef NOW
17915 #ifdef INVARIANTS
17916 				panic("rack:%p hit JR_ASSESSING case cwnd_to_use:%u?", rack, cwnd_to_use);
17917 #endif
17918 #endif
17919 				app_limited = CTF_JR_ASSESSING;
17920 			}
17921 			/*
17922 			 * App limited in some fashion, for our pacing GP
17923 			 * measurements we don't want any gap (even cwnd).
17924 			 * Close  down the measurement window.
17925 			 */
17926 			if (rack_cwnd_block_ends_measure &&
17927 			    ((app_limited == CTF_JR_CWND_LIMITED) ||
17928 			     (app_limited == CTF_JR_PRR))) {
17929 				/*
17930 				 * The reason we are not sending is
17931 				 * the cwnd (or prr). We have been configured
17932 				 * to end the measurement window in
17933 				 * this case.
17934 				 */
17935 				end_window = 1;
17936 			} else if (rack_rwnd_block_ends_measure &&
17937 				   (app_limited == CTF_JR_RWND_LIMITED)) {
17938 				/*
17939 				 * We are rwnd limited and have been
17940 				 * configured to end the measurement
17941 				 * window in this case.
17942 				 */
17943 				end_window = 1;
17944 			} else if (app_limited == CTF_JR_APP_LIMITED) {
17945 				/*
17946 				 * A true application limited period, we have
17947 				 * ran out of data.
17948 				 */
17949 				end_window = 1;
17950 			} else if (app_limited == CTF_JR_ASSESSING) {
17951 				/*
17952 				 * In the assessing case we hit the end of
17953 				 * the if/else and had no known reason
17954 				 * This will panic us under invariants..
17955 				 *
17956 				 * If we get this out in logs we need to
17957 				 * investagate which reason we missed.
17958 				 */
17959 				end_window = 1;
17960 			}
17961 			if (end_window) {
17962 				uint8_t log = 0;
17963 
17964 				/* Adjust the Gput measurement */
17965 				if ((tp->t_flags & TF_GPUTINPROG) &&
17966 				    SEQ_GT(tp->gput_ack, tp->snd_max)) {
17967 					tp->gput_ack = tp->snd_max;
17968 					if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) {
17969 						/*
17970 						 * There is not enough to measure.
17971 						 */
17972 						tp->t_flags &= ~TF_GPUTINPROG;
17973 						rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
17974 									   rack->r_ctl.rc_gp_srtt /*flex1*/,
17975 									   tp->gput_seq,
17976 									   0, 0, 18, __LINE__, NULL, 0);
17977 					} else
17978 						log = 1;
17979 				}
17980 				/* Mark the last packet has app limited */
17981 				rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
17982 				if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) {
17983 					if (rack->r_ctl.rc_app_limited_cnt == 0)
17984 						rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm;
17985 					else {
17986 						/*
17987 						 * Go out to the end app limited and mark
17988 						 * this new one as next and move the end_appl up
17989 						 * to this guy.
17990 						 */
17991 						if (rack->r_ctl.rc_end_appl)
17992 							rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start;
17993 						rack->r_ctl.rc_end_appl = rsm;
17994 					}
17995 					rsm->r_flags |= RACK_APP_LIMITED;
17996 					rack->r_ctl.rc_app_limited_cnt++;
17997 				}
17998 				if (log)
17999 					rack_log_pacing_delay_calc(rack,
18000 								   rack->r_ctl.rc_app_limited_cnt, seq,
18001 								   tp->gput_ack, 0, 0, 4, __LINE__, NULL, 0);
18002 			}
18003 		}
18004 		if (slot) {
18005 			/* set the rack tcb into the slot N */
18006 			counter_u64_add(rack_paced_segments, 1);
18007 		} else if (tot_len_this_send) {
18008 			counter_u64_add(rack_unpaced_segments, 1);
18009 		}
18010 		/* Check if we need to go into persists or not */
18011 		if ((tp->snd_max == tp->snd_una) &&
18012 		    TCPS_HAVEESTABLISHED(tp->t_state) &&
18013 		    sbavail(sb) &&
18014 		    (sbavail(sb) > tp->snd_wnd) &&
18015 		    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg))) {
18016 			/* Yes lets make sure to move to persist before timer-start */
18017 			rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime);
18018 		}
18019 		rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, sup_rack);
18020 		rack_log_type_just_return(rack, cts, tot_len_this_send, slot, hpts_calling, app_limited, cwnd_to_use);
18021 	}
18022 #ifdef NETFLIX_SHARED_CWND
18023 	if ((sbavail(sb) == 0) &&
18024 	    rack->r_ctl.rc_scw) {
18025 		tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
18026 		rack->rack_scwnd_is_idle = 1;
18027 	}
18028 #endif
18029 #ifdef TCP_ACCOUNTING
18030 	if (tot_len_this_send > 0) {
18031 		crtsc = get_cyclecount();
18032 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
18033 			tp->tcp_cnt_counters[SND_OUT_DATA]++;
18034 		}
18035 		counter_u64_add(tcp_cnt_counters[SND_OUT_DATA], 1);
18036 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
18037 			tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
18038 		}
18039 		counter_u64_add(tcp_proc_time[SND_OUT_DATA], (crtsc - ts_val));
18040 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
18041 			tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) / segsiz);
18042 		}
18043 		counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((tot_len_this_send + segsiz - 1) / segsiz));
18044 	} else {
18045 		crtsc = get_cyclecount();
18046 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
18047 			tp->tcp_cnt_counters[SND_LIMITED]++;
18048 		}
18049 		counter_u64_add(tcp_cnt_counters[SND_LIMITED], 1);
18050 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
18051 			tp->tcp_proc_time[SND_LIMITED] += (crtsc - ts_val);
18052 		}
18053 		counter_u64_add(tcp_proc_time[SND_LIMITED], (crtsc - ts_val));
18054 	}
18055 	sched_unpin();
18056 #endif
18057 	return (0);
18058 
18059 send:
18060 	if (rsm || sack_rxmit)
18061 		counter_u64_add(rack_nfto_resend, 1);
18062 	else
18063 		counter_u64_add(rack_non_fto_send, 1);
18064 	if ((flags & TH_FIN) &&
18065 	    sbavail(sb)) {
18066 		/*
18067 		 * We do not transmit a FIN
18068 		 * with data outstanding. We
18069 		 * need to make it so all data
18070 		 * is acked first.
18071 		 */
18072 		flags &= ~TH_FIN;
18073 	}
18074 	/* Enforce stack imposed max seg size if we have one */
18075 	if (rack->r_ctl.rc_pace_max_segs &&
18076 	    (len > rack->r_ctl.rc_pace_max_segs)) {
18077 		mark = 1;
18078 		len = rack->r_ctl.rc_pace_max_segs;
18079 	}
18080 	SOCKBUF_LOCK_ASSERT(sb);
18081 	if (len > 0) {
18082 		if (len >= segsiz)
18083 			tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT;
18084 		else
18085 			tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT;
18086 	}
18087 	/*
18088 	 * Before ESTABLISHED, force sending of initial options unless TCP
18089 	 * set not to do any options. NOTE: we assume that the IP/TCP header
18090 	 * plus TCP options always fit in a single mbuf, leaving room for a
18091 	 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr)
18092 	 * + optlen <= MCLBYTES
18093 	 */
18094 	optlen = 0;
18095 #ifdef INET6
18096 	if (isipv6)
18097 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
18098 	else
18099 #endif
18100 		hdrlen = sizeof(struct tcpiphdr);
18101 
18102 	/*
18103 	 * Compute options for segment. We only have to care about SYN and
18104 	 * established connection segments.  Options for SYN-ACK segments
18105 	 * are handled in TCP syncache.
18106 	 */
18107 	to.to_flags = 0;
18108 	if ((tp->t_flags & TF_NOOPT) == 0) {
18109 		/* Maximum segment size. */
18110 		if (flags & TH_SYN) {
18111 			tp->snd_nxt = tp->iss;
18112 			to.to_mss = tcp_mssopt(&inp->inp_inc);
18113 			if (tp->t_port)
18114 				to.to_mss -= V_tcp_udp_tunneling_overhead;
18115 			to.to_flags |= TOF_MSS;
18116 
18117 			/*
18118 			 * On SYN or SYN|ACK transmits on TFO connections,
18119 			 * only include the TFO option if it is not a
18120 			 * retransmit, as the presence of the TFO option may
18121 			 * have caused the original SYN or SYN|ACK to have
18122 			 * been dropped by a middlebox.
18123 			 */
18124 			if (IS_FASTOPEN(tp->t_flags) &&
18125 			    (tp->t_rxtshift == 0)) {
18126 				if (tp->t_state == TCPS_SYN_RECEIVED) {
18127 					to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN;
18128 					to.to_tfo_cookie =
18129 						(u_int8_t *)&tp->t_tfo_cookie.server;
18130 					to.to_flags |= TOF_FASTOPEN;
18131 					wanted_cookie = 1;
18132 				} else if (tp->t_state == TCPS_SYN_SENT) {
18133 					to.to_tfo_len =
18134 						tp->t_tfo_client_cookie_len;
18135 					to.to_tfo_cookie =
18136 						tp->t_tfo_cookie.client;
18137 					to.to_flags |= TOF_FASTOPEN;
18138 					wanted_cookie = 1;
18139 					/*
18140 					 * If we wind up having more data to
18141 					 * send with the SYN than can fit in
18142 					 * one segment, don't send any more
18143 					 * until the SYN|ACK comes back from
18144 					 * the other end.
18145 					 */
18146 					sendalot = 0;
18147 				}
18148 			}
18149 		}
18150 		/* Window scaling. */
18151 		if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
18152 			to.to_wscale = tp->request_r_scale;
18153 			to.to_flags |= TOF_SCALE;
18154 		}
18155 		/* Timestamps. */
18156 		if ((tp->t_flags & TF_RCVD_TSTMP) ||
18157 		    ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
18158 			to.to_tsval = ms_cts + tp->ts_offset;
18159 			to.to_tsecr = tp->ts_recent;
18160 			to.to_flags |= TOF_TS;
18161 		}
18162 		/* Set receive buffer autosizing timestamp. */
18163 		if (tp->rfbuf_ts == 0 &&
18164 		    (so->so_rcv.sb_flags & SB_AUTOSIZE))
18165 			tp->rfbuf_ts = tcp_ts_getticks();
18166 		/* Selective ACK's. */
18167 		if (tp->t_flags & TF_SACK_PERMIT) {
18168 			if (flags & TH_SYN)
18169 				to.to_flags |= TOF_SACKPERM;
18170 			else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
18171 				 tp->rcv_numsacks > 0) {
18172 				to.to_flags |= TOF_SACK;
18173 				to.to_nsacks = tp->rcv_numsacks;
18174 				to.to_sacks = (u_char *)tp->sackblks;
18175 			}
18176 		}
18177 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
18178 		/* TCP-MD5 (RFC2385). */
18179 		if (tp->t_flags & TF_SIGNATURE)
18180 			to.to_flags |= TOF_SIGNATURE;
18181 #endif				/* TCP_SIGNATURE */
18182 
18183 		/* Processing the options. */
18184 		hdrlen += optlen = tcp_addoptions(&to, opt);
18185 		/*
18186 		 * If we wanted a TFO option to be added, but it was unable
18187 		 * to fit, ensure no data is sent.
18188 		 */
18189 		if (IS_FASTOPEN(tp->t_flags) && wanted_cookie &&
18190 		    !(to.to_flags & TOF_FASTOPEN))
18191 			len = 0;
18192 	}
18193 	if (tp->t_port) {
18194 		if (V_tcp_udp_tunneling_port == 0) {
18195 			/* The port was removed?? */
18196 			SOCKBUF_UNLOCK(&so->so_snd);
18197 #ifdef TCP_ACCOUNTING
18198 			crtsc = get_cyclecount();
18199 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
18200 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
18201 			}
18202 			counter_u64_add(tcp_cnt_counters[SND_OUT_FAIL], 1);
18203 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
18204 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
18205 			}
18206 			counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val));
18207 			sched_unpin();
18208 #endif
18209 			return (EHOSTUNREACH);
18210 		}
18211 		hdrlen += sizeof(struct udphdr);
18212 	}
18213 #ifdef INET6
18214 	if (isipv6)
18215 		ipoptlen = ip6_optlen(tp->t_inpcb);
18216 	else
18217 #endif
18218 		if (tp->t_inpcb->inp_options)
18219 			ipoptlen = tp->t_inpcb->inp_options->m_len -
18220 				offsetof(struct ipoption, ipopt_list);
18221 		else
18222 			ipoptlen = 0;
18223 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
18224 	ipoptlen += ipsec_optlen;
18225 #endif
18226 
18227 	/*
18228 	 * Adjust data length if insertion of options will bump the packet
18229 	 * length beyond the t_maxseg length. Clear the FIN bit because we
18230 	 * cut off the tail of the segment.
18231 	 */
18232 	if (len + optlen + ipoptlen > tp->t_maxseg) {
18233 		if (tso) {
18234 			uint32_t if_hw_tsomax;
18235 			uint32_t moff;
18236 			int32_t max_len;
18237 
18238 			/* extract TSO information */
18239 			if_hw_tsomax = tp->t_tsomax;
18240 			if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
18241 			if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
18242 			KASSERT(ipoptlen == 0,
18243 				("%s: TSO can't do IP options", __func__));
18244 
18245 			/*
18246 			 * Check if we should limit by maximum payload
18247 			 * length:
18248 			 */
18249 			if (if_hw_tsomax != 0) {
18250 				/* compute maximum TSO length */
18251 				max_len = (if_hw_tsomax - hdrlen -
18252 					   max_linkhdr);
18253 				if (max_len <= 0) {
18254 					len = 0;
18255 				} else if (len > max_len) {
18256 					sendalot = 1;
18257 					len = max_len;
18258 					mark = 2;
18259 				}
18260 			}
18261 			/*
18262 			 * Prevent the last segment from being fractional
18263 			 * unless the send sockbuf can be emptied:
18264 			 */
18265 			max_len = (tp->t_maxseg - optlen);
18266 			if ((sb_offset + len) < sbavail(sb)) {
18267 				moff = len % (u_int)max_len;
18268 				if (moff != 0) {
18269 					mark = 3;
18270 					len -= moff;
18271 				}
18272 			}
18273 			/*
18274 			 * In case there are too many small fragments don't
18275 			 * use TSO:
18276 			 */
18277 			if (len <= segsiz) {
18278 				mark = 4;
18279 				tso = 0;
18280 			}
18281 			/*
18282 			 * Send the FIN in a separate segment after the bulk
18283 			 * sending is done. We don't trust the TSO
18284 			 * implementations to clear the FIN flag on all but
18285 			 * the last segment.
18286 			 */
18287 			if (tp->t_flags & TF_NEEDFIN) {
18288 				sendalot = 4;
18289 			}
18290 		} else {
18291 			mark = 5;
18292 			if (optlen + ipoptlen >= tp->t_maxseg) {
18293 				/*
18294 				 * Since we don't have enough space to put
18295 				 * the IP header chain and the TCP header in
18296 				 * one packet as required by RFC 7112, don't
18297 				 * send it. Also ensure that at least one
18298 				 * byte of the payload can be put into the
18299 				 * TCP segment.
18300 				 */
18301 				SOCKBUF_UNLOCK(&so->so_snd);
18302 				error = EMSGSIZE;
18303 				sack_rxmit = 0;
18304 				goto out;
18305 			}
18306 			len = tp->t_maxseg - optlen - ipoptlen;
18307 			sendalot = 5;
18308 		}
18309 	} else {
18310 		tso = 0;
18311 		mark = 6;
18312 	}
18313 	KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET,
18314 		("%s: len > IP_MAXPACKET", __func__));
18315 #ifdef DIAGNOSTIC
18316 #ifdef INET6
18317 	if (max_linkhdr + hdrlen > MCLBYTES)
18318 #else
18319 		if (max_linkhdr + hdrlen > MHLEN)
18320 #endif
18321 			panic("tcphdr too big");
18322 #endif
18323 
18324 	/*
18325 	 * This KASSERT is here to catch edge cases at a well defined place.
18326 	 * Before, those had triggered (random) panic conditions further
18327 	 * down.
18328 	 */
18329 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
18330 	if ((len == 0) &&
18331 	    (flags & TH_FIN) &&
18332 	    (sbused(sb))) {
18333 		/*
18334 		 * We have outstanding data, don't send a fin by itself!.
18335 		 */
18336 		goto just_return;
18337 	}
18338 	/*
18339 	 * Grab a header mbuf, attaching a copy of data to be transmitted,
18340 	 * and initialize the header from the template for sends on this
18341 	 * connection.
18342 	 */
18343 	hw_tls = (sb->sb_flags & SB_TLS_IFNET) != 0;
18344 	if (len) {
18345 		uint32_t max_val;
18346 		uint32_t moff;
18347 
18348 		if (rack->r_ctl.rc_pace_max_segs)
18349 			max_val = rack->r_ctl.rc_pace_max_segs;
18350 		else if (rack->rc_user_set_max_segs)
18351 			max_val = rack->rc_user_set_max_segs * segsiz;
18352 		else
18353 			max_val = len;
18354 		/*
18355 		 * We allow a limit on sending with hptsi.
18356 		 */
18357 		if (len > max_val) {
18358 			mark = 7;
18359 			len = max_val;
18360 		}
18361 #ifdef INET6
18362 		if (MHLEN < hdrlen + max_linkhdr)
18363 			m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
18364 		else
18365 #endif
18366 			m = m_gethdr(M_NOWAIT, MT_DATA);
18367 
18368 		if (m == NULL) {
18369 			SOCKBUF_UNLOCK(sb);
18370 			error = ENOBUFS;
18371 			sack_rxmit = 0;
18372 			goto out;
18373 		}
18374 		m->m_data += max_linkhdr;
18375 		m->m_len = hdrlen;
18376 
18377 		/*
18378 		 * Start the m_copy functions from the closest mbuf to the
18379 		 * sb_offset in the socket buffer chain.
18380 		 */
18381 		mb = sbsndptr_noadv(sb, sb_offset, &moff);
18382 		s_mb = mb;
18383 		s_moff = moff;
18384 		if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) {
18385 			m_copydata(mb, moff, (int)len,
18386 				   mtod(m, caddr_t)+hdrlen);
18387 			if (SEQ_LT(tp->snd_nxt, tp->snd_max))
18388 				sbsndptr_adv(sb, mb, len);
18389 			m->m_len += len;
18390 		} else {
18391 			struct sockbuf *msb;
18392 
18393 			if (SEQ_LT(tp->snd_nxt, tp->snd_max))
18394 				msb = NULL;
18395 			else
18396 				msb = sb;
18397 			m->m_next = tcp_m_copym(
18398 				mb, moff, &len,
18399 				if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, msb,
18400 				((rsm == NULL) ? hw_tls : 0)
18401 #ifdef NETFLIX_COPY_ARGS
18402 				, &filled_all
18403 #endif
18404 				);
18405 			if (len <= (tp->t_maxseg - optlen)) {
18406 				/*
18407 				 * Must have ran out of mbufs for the copy
18408 				 * shorten it to no longer need tso. Lets
18409 				 * not put on sendalot since we are low on
18410 				 * mbufs.
18411 				 */
18412 				tso = 0;
18413 			}
18414 			if (m->m_next == NULL) {
18415 				SOCKBUF_UNLOCK(sb);
18416 				(void)m_free(m);
18417 				error = ENOBUFS;
18418 				sack_rxmit = 0;
18419 				goto out;
18420 			}
18421 		}
18422 		if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) {
18423 			if (rsm && (rsm->r_flags & RACK_TLP)) {
18424 				/*
18425 				 * TLP should not count in retran count, but
18426 				 * in its own bin
18427 				 */
18428 				counter_u64_add(rack_tlp_retran, 1);
18429 				counter_u64_add(rack_tlp_retran_bytes, len);
18430 			} else {
18431 				tp->t_sndrexmitpack++;
18432 				KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
18433 				KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
18434 			}
18435 #ifdef STATS
18436 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
18437 						 len);
18438 #endif
18439 		} else {
18440 			KMOD_TCPSTAT_INC(tcps_sndpack);
18441 			KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
18442 #ifdef STATS
18443 			stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
18444 						 len);
18445 #endif
18446 		}
18447 		/*
18448 		 * If we're sending everything we've got, set PUSH. (This
18449 		 * will keep happy those implementations which only give
18450 		 * data to the user when a buffer fills or a PUSH comes in.)
18451 		 */
18452 		if (sb_offset + len == sbused(sb) &&
18453 		    sbused(sb) &&
18454 		    !(flags & TH_SYN)) {
18455 			flags |= TH_PUSH;
18456 			add_flag |= RACK_HAD_PUSH;
18457 		}
18458 
18459 		SOCKBUF_UNLOCK(sb);
18460 	} else {
18461 		SOCKBUF_UNLOCK(sb);
18462 		if (tp->t_flags & TF_ACKNOW)
18463 			KMOD_TCPSTAT_INC(tcps_sndacks);
18464 		else if (flags & (TH_SYN | TH_FIN | TH_RST))
18465 			KMOD_TCPSTAT_INC(tcps_sndctrl);
18466 		else
18467 			KMOD_TCPSTAT_INC(tcps_sndwinup);
18468 
18469 		m = m_gethdr(M_NOWAIT, MT_DATA);
18470 		if (m == NULL) {
18471 			error = ENOBUFS;
18472 			sack_rxmit = 0;
18473 			goto out;
18474 		}
18475 #ifdef INET6
18476 		if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
18477 		    MHLEN >= hdrlen) {
18478 			M_ALIGN(m, hdrlen);
18479 		} else
18480 #endif
18481 			m->m_data += max_linkhdr;
18482 		m->m_len = hdrlen;
18483 	}
18484 	SOCKBUF_UNLOCK_ASSERT(sb);
18485 	m->m_pkthdr.rcvif = (struct ifnet *)0;
18486 #ifdef MAC
18487 	mac_inpcb_create_mbuf(inp, m);
18488 #endif
18489 	if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) &&  rack->r_fsb_inited) {
18490 #ifdef INET6
18491 		if (isipv6)
18492 			ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
18493 		else
18494 #endif				/* INET6 */
18495 			ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
18496 		th = rack->r_ctl.fsb.th;
18497 		udp = rack->r_ctl.fsb.udp;
18498 		if (udp) {
18499 #ifdef INET6
18500 			if (isipv6)
18501 				ulen = hdrlen + len - sizeof(struct ip6_hdr);
18502 			else
18503 #endif				/* INET6 */
18504 				ulen = hdrlen + len - sizeof(struct ip);
18505 			udp->uh_ulen = htons(ulen);
18506 		}
18507 	} else {
18508 #ifdef INET6
18509 		if (isipv6) {
18510 			ip6 = mtod(m, struct ip6_hdr *);
18511 			if (tp->t_port) {
18512 				udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
18513 				udp->uh_sport = htons(V_tcp_udp_tunneling_port);
18514 				udp->uh_dport = tp->t_port;
18515 				ulen = hdrlen + len - sizeof(struct ip6_hdr);
18516 				udp->uh_ulen = htons(ulen);
18517 				th = (struct tcphdr *)(udp + 1);
18518 			} else
18519 				th = (struct tcphdr *)(ip6 + 1);
18520 			tcpip_fillheaders(inp, tp->t_port, ip6, th);
18521 		} else
18522 #endif				/* INET6 */
18523 		{
18524 			ip = mtod(m, struct ip *);
18525 #ifdef TCPDEBUG
18526 			ipov = (struct ipovly *)ip;
18527 #endif
18528 			if (tp->t_port) {
18529 				udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
18530 				udp->uh_sport = htons(V_tcp_udp_tunneling_port);
18531 				udp->uh_dport = tp->t_port;
18532 				ulen = hdrlen + len - sizeof(struct ip);
18533 				udp->uh_ulen = htons(ulen);
18534 				th = (struct tcphdr *)(udp + 1);
18535 			} else
18536 				th = (struct tcphdr *)(ip + 1);
18537 			tcpip_fillheaders(inp, tp->t_port, ip, th);
18538 		}
18539 	}
18540 	/*
18541 	 * Fill in fields, remembering maximum advertised window for use in
18542 	 * delaying messages about window sizes. If resending a FIN, be sure
18543 	 * not to use a new sequence number.
18544 	 */
18545 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
18546 	    tp->snd_nxt == tp->snd_max)
18547 		tp->snd_nxt--;
18548 	/*
18549 	 * If we are starting a connection, send ECN setup SYN packet. If we
18550 	 * are on a retransmit, we may resend those bits a number of times
18551 	 * as per RFC 3168.
18552 	 */
18553 	if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn) {
18554 		flags |= tcp_ecn_output_syn_sent(tp);
18555 	}
18556 	/* Also handle parallel SYN for ECN */
18557 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
18558 	    (tp->t_flags2 & TF2_ECN_PERMIT)) {
18559 		int ect = tcp_ecn_output_established(tp, &flags, len);
18560 		if ((tp->t_state == TCPS_SYN_RECEIVED) &&
18561 		    (tp->t_flags2 & TF2_ECN_SND_ECE))
18562 			tp->t_flags2 &= ~TF2_ECN_SND_ECE;
18563 #ifdef INET6
18564 		if (isipv6) {
18565 			ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
18566 			ip6->ip6_flow |= htonl(ect << 20);
18567 		}
18568 		else
18569 #endif
18570 		{
18571 			ip->ip_tos &= ~IPTOS_ECN_MASK;
18572 			ip->ip_tos |= ect;
18573 		}
18574 	}
18575 	/*
18576 	 * If we are doing retransmissions, then snd_nxt will not reflect
18577 	 * the first unsent octet.  For ACK only packets, we do not want the
18578 	 * sequence number of the retransmitted packet, we want the sequence
18579 	 * number of the next unsent octet.  So, if there is no data (and no
18580 	 * SYN or FIN), use snd_max instead of snd_nxt when filling in
18581 	 * ti_seq.  But if we are in persist state, snd_max might reflect
18582 	 * one byte beyond the right edge of the window, so use snd_nxt in
18583 	 * that case, since we know we aren't doing a retransmission.
18584 	 * (retransmit and persist are mutually exclusive...)
18585 	 */
18586 	if (sack_rxmit == 0) {
18587 		if (len || (flags & (TH_SYN | TH_FIN))) {
18588 			th->th_seq = htonl(tp->snd_nxt);
18589 			rack_seq = tp->snd_nxt;
18590 		} else {
18591 			th->th_seq = htonl(tp->snd_max);
18592 			rack_seq = tp->snd_max;
18593 		}
18594 	} else {
18595 		th->th_seq = htonl(rsm->r_start);
18596 		rack_seq = rsm->r_start;
18597 	}
18598 	th->th_ack = htonl(tp->rcv_nxt);
18599 	tcp_set_flags(th, flags);
18600 	/*
18601 	 * Calculate receive window.  Don't shrink window, but avoid silly
18602 	 * window syndrome.
18603 	 * If a RST segment is sent, advertise a window of zero.
18604 	 */
18605 	if (flags & TH_RST) {
18606 		recwin = 0;
18607 	} else {
18608 		if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
18609 		    recwin < (long)segsiz) {
18610 			recwin = 0;
18611 		}
18612 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) &&
18613 		    recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
18614 			recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
18615 	}
18616 
18617 	/*
18618 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or
18619 	 * <SYN,ACK>) segment itself is never scaled.  The <SYN,ACK> case is
18620 	 * handled in syncache.
18621 	 */
18622 	if (flags & TH_SYN)
18623 		th->th_win = htons((u_short)
18624 				   (min(sbspace(&so->so_rcv), TCP_MAXWIN)));
18625 	else {
18626 		/* Avoid shrinking window with window scaling. */
18627 		recwin = roundup2(recwin, 1 << tp->rcv_scale);
18628 		th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
18629 	}
18630 	/*
18631 	 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0
18632 	 * window.  This may cause the remote transmitter to stall.  This
18633 	 * flag tells soreceive() to disable delayed acknowledgements when
18634 	 * draining the buffer.  This can occur if the receiver is
18635 	 * attempting to read more data than can be buffered prior to
18636 	 * transmitting on the connection.
18637 	 */
18638 	if (th->th_win == 0) {
18639 		tp->t_sndzerowin++;
18640 		tp->t_flags |= TF_RXWIN0SENT;
18641 	} else
18642 		tp->t_flags &= ~TF_RXWIN0SENT;
18643 	tp->snd_up = tp->snd_una;	/* drag it along, its deprecated */
18644 	/* Now are we using fsb?, if so copy the template data to the mbuf */
18645 	if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) && rack->r_fsb_inited) {
18646 		uint8_t *cpto;
18647 
18648 		cpto = mtod(m, uint8_t *);
18649 		memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
18650 		/*
18651 		 * We have just copied in:
18652 		 * IP/IP6
18653 		 * <optional udphdr>
18654 		 * tcphdr (no options)
18655 		 *
18656 		 * We need to grab the correct pointers into the mbuf
18657 		 * for both the tcp header, and possibly the udp header (if tunneling).
18658 		 * We do this by using the offset in the copy buffer and adding it
18659 		 * to the mbuf base pointer (cpto).
18660 		 */
18661 #ifdef INET6
18662 		if (isipv6)
18663 			ip6 = mtod(m, struct ip6_hdr *);
18664 		else
18665 #endif				/* INET6 */
18666 			ip = mtod(m, struct ip *);
18667 		th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
18668 		/* If we have a udp header lets set it into the mbuf as well */
18669 		if (udp)
18670 			udp = (struct udphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.udp - rack->r_ctl.fsb.tcp_ip_hdr));
18671 	}
18672 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
18673 	if (to.to_flags & TOF_SIGNATURE) {
18674 		/*
18675 		 * Calculate MD5 signature and put it into the place
18676 		 * determined before.
18677 		 * NOTE: since TCP options buffer doesn't point into
18678 		 * mbuf's data, calculate offset and use it.
18679 		 */
18680 		if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
18681 						       (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
18682 			/*
18683 			 * Do not send segment if the calculation of MD5
18684 			 * digest has failed.
18685 			 */
18686 			goto out;
18687 		}
18688 	}
18689 #endif
18690 	if (optlen) {
18691 		bcopy(opt, th + 1, optlen);
18692 		th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
18693 	}
18694 	/*
18695 	 * Put TCP length in extended header, and then checksum extended
18696 	 * header and data.
18697 	 */
18698 	m->m_pkthdr.len = hdrlen + len;	/* in6_cksum() need this */
18699 #ifdef INET6
18700 	if (isipv6) {
18701 		/*
18702 		 * ip6_plen is not need to be filled now, and will be filled
18703 		 * in ip6_output.
18704 		 */
18705 		if (tp->t_port) {
18706 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
18707 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
18708 			udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
18709 			th->th_sum = htons(0);
18710 			UDPSTAT_INC(udps_opackets);
18711 		} else {
18712 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
18713 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
18714 			th->th_sum = in6_cksum_pseudo(ip6,
18715 						      sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
18716 						      0);
18717 		}
18718 	}
18719 #endif
18720 #if defined(INET6) && defined(INET)
18721 	else
18722 #endif
18723 #ifdef INET
18724 	{
18725 		if (tp->t_port) {
18726 			m->m_pkthdr.csum_flags = CSUM_UDP;
18727 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
18728 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
18729 						ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
18730 			th->th_sum = htons(0);
18731 			UDPSTAT_INC(udps_opackets);
18732 		} else {
18733 			m->m_pkthdr.csum_flags = CSUM_TCP;
18734 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
18735 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
18736 					       ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
18737 									IPPROTO_TCP + len + optlen));
18738 		}
18739 		/* IP version must be set here for ipv4/ipv6 checking later */
18740 		KASSERT(ip->ip_v == IPVERSION,
18741 			("%s: IP version incorrect: %d", __func__, ip->ip_v));
18742 	}
18743 #endif
18744 	/*
18745 	 * Enable TSO and specify the size of the segments. The TCP pseudo
18746 	 * header checksum is always provided. XXX: Fixme: This is currently
18747 	 * not the case for IPv6.
18748 	 */
18749 	if (tso) {
18750 		KASSERT(len > tp->t_maxseg - optlen,
18751 			("%s: len <= tso_segsz", __func__));
18752 		m->m_pkthdr.csum_flags |= CSUM_TSO;
18753 		m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen;
18754 	}
18755 	KASSERT(len + hdrlen == m_length(m, NULL),
18756 		("%s: mbuf chain different than expected: %d + %u != %u",
18757 		 __func__, len, hdrlen, m_length(m, NULL)));
18758 
18759 #ifdef TCP_HHOOK
18760 	/* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */
18761 	hhook_run_tcp_est_out(tp, th, &to, len, tso);
18762 #endif
18763 	/* We're getting ready to send; log now. */
18764 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
18765 		union tcp_log_stackspecific log;
18766 
18767 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
18768 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
18769 		if (rack->rack_no_prr)
18770 			log.u_bbr.flex1 = 0;
18771 		else
18772 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
18773 		log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
18774 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
18775 		log.u_bbr.flex4 = orig_len;
18776 		if (filled_all)
18777 			log.u_bbr.flex5 = 0x80000000;
18778 		else
18779 			log.u_bbr.flex5 = 0;
18780 		/* Save off the early/late values */
18781 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
18782 		log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
18783 		log.u_bbr.bw_inuse = rack_get_bw(rack);
18784 		if (rsm || sack_rxmit) {
18785 			if (doing_tlp)
18786 				log.u_bbr.flex8 = 2;
18787 			else
18788 				log.u_bbr.flex8 = 1;
18789 		} else {
18790 			if (doing_tlp)
18791 				log.u_bbr.flex8 = 3;
18792 			else
18793 				log.u_bbr.flex8 = 0;
18794 		}
18795 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm);
18796 		log.u_bbr.flex7 = mark;
18797 		log.u_bbr.flex7 <<= 8;
18798 		log.u_bbr.flex7 |= pass;
18799 		log.u_bbr.pkts_out = tp->t_maxseg;
18800 		log.u_bbr.timeStamp = cts;
18801 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18802 		log.u_bbr.lt_epoch = cwnd_to_use;
18803 		log.u_bbr.delivered = sendalot;
18804 		lgb = tcp_log_event_(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK,
18805 				     len, &log, false, NULL, NULL, 0, &tv);
18806 	} else
18807 		lgb = NULL;
18808 
18809 	/*
18810 	 * Fill in IP length and desired time to live and send to IP level.
18811 	 * There should be a better way to handle ttl and tos; we could keep
18812 	 * them in the template, but need a way to checksum without them.
18813 	 */
18814 	/*
18815 	 * m->m_pkthdr.len should have been set before cksum calcuration,
18816 	 * because in6_cksum() need it.
18817 	 */
18818 #ifdef INET6
18819 	if (isipv6) {
18820 		/*
18821 		 * we separately set hoplimit for every segment, since the
18822 		 * user might want to change the value via setsockopt. Also,
18823 		 * desired default hop limit might be changed via Neighbor
18824 		 * Discovery.
18825 		 */
18826 		rack->r_ctl.fsb.hoplimit = ip6->ip6_hlim = in6_selecthlim(inp, NULL);
18827 
18828 		/*
18829 		 * Set the packet size here for the benefit of DTrace
18830 		 * probes. ip6_output() will set it properly; it's supposed
18831 		 * to include the option header lengths as well.
18832 		 */
18833 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
18834 
18835 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
18836 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
18837 		else
18838 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
18839 
18840 		if (tp->t_state == TCPS_SYN_SENT)
18841 			TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th);
18842 
18843 		TCP_PROBE5(send, NULL, tp, ip6, tp, th);
18844 		/* TODO: IPv6 IP6TOS_ECT bit on */
18845 		error = ip6_output(m,
18846 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
18847 				   inp->in6p_outputopts,
18848 #else
18849 				   NULL,
18850 #endif
18851 				   &inp->inp_route6,
18852 				   ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0),
18853 				   NULL, NULL, inp);
18854 
18855 		if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL)
18856 			mtu = inp->inp_route6.ro_nh->nh_mtu;
18857 	}
18858 #endif				/* INET6 */
18859 #if defined(INET) && defined(INET6)
18860 	else
18861 #endif
18862 #ifdef INET
18863 	{
18864 		ip->ip_len = htons(m->m_pkthdr.len);
18865 #ifdef INET6
18866 		if (inp->inp_vflag & INP_IPV6PROTO)
18867 			ip->ip_ttl = in6_selecthlim(inp, NULL);
18868 #endif				/* INET6 */
18869 		rack->r_ctl.fsb.hoplimit = ip->ip_ttl;
18870 		/*
18871 		 * If we do path MTU discovery, then we set DF on every
18872 		 * packet. This might not be the best thing to do according
18873 		 * to RFC3390 Section 2. However the tcp hostcache migitates
18874 		 * the problem so it affects only the first tcp connection
18875 		 * with a host.
18876 		 *
18877 		 * NB: Don't set DF on small MTU/MSS to have a safe
18878 		 * fallback.
18879 		 */
18880 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
18881 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
18882 			if (tp->t_port == 0 || len < V_tcp_minmss) {
18883 				ip->ip_off |= htons(IP_DF);
18884 			}
18885 		} else {
18886 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
18887 		}
18888 
18889 		if (tp->t_state == TCPS_SYN_SENT)
18890 			TCP_PROBE5(connect__request, NULL, tp, ip, tp, th);
18891 
18892 		TCP_PROBE5(send, NULL, tp, ip, tp, th);
18893 
18894 		error = ip_output(m,
18895 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
18896 				  inp->inp_options,
18897 #else
18898 				  NULL,
18899 #endif
18900 				  &inp->inp_route,
18901 				  ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0,
18902 				  inp);
18903 		if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL)
18904 			mtu = inp->inp_route.ro_nh->nh_mtu;
18905 	}
18906 #endif				/* INET */
18907 
18908 out:
18909 	if (lgb) {
18910 		lgb->tlb_errno = error;
18911 		lgb = NULL;
18912 	}
18913 	/*
18914 	 * In transmit state, time the transmission and arrange for the
18915 	 * retransmit.  In persist state, just set snd_max.
18916 	 */
18917 	if (error == 0) {
18918 		tcp_account_for_send(tp, len, (rsm != NULL), doing_tlp, hw_tls);
18919 		if (rsm && doing_tlp) {
18920 			rack->rc_last_sent_tlp_past_cumack = 0;
18921 			rack->rc_last_sent_tlp_seq_valid = 1;
18922 			rack->r_ctl.last_sent_tlp_seq = rsm->r_start;
18923 			rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start;
18924 		}
18925 		rack->forced_ack = 0;	/* If we send something zap the FA flag */
18926 		if (rsm && (doing_tlp == 0)) {
18927 			/* Set we retransmitted */
18928 			rack->rc_gp_saw_rec = 1;
18929 		} else {
18930 			if (cwnd_to_use > tp->snd_ssthresh) {
18931 				/* Set we sent in CA */
18932 				rack->rc_gp_saw_ca = 1;
18933 			} else {
18934 				/* Set we sent in SS */
18935 				rack->rc_gp_saw_ss = 1;
18936 			}
18937 		}
18938 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
18939 		    (tp->t_flags & TF_SACK_PERMIT) &&
18940 		    tp->rcv_numsacks > 0)
18941 			tcp_clean_dsack_blocks(tp);
18942 		tot_len_this_send += len;
18943 		if (len == 0)
18944 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_SNDACK], 1);
18945 		else if (len == 1) {
18946 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_PERSIST], 1);
18947 		} else if (len > 1) {
18948 			int idx;
18949 
18950 			idx = (len / segsiz) + 3;
18951 			if (idx >= TCP_MSS_ACCT_ATIMER)
18952 				counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
18953 			else
18954 				counter_u64_add(rack_out_size[idx], 1);
18955 		}
18956 	}
18957 	if ((rack->rack_no_prr == 0) &&
18958 	    sub_from_prr &&
18959 	    (error == 0)) {
18960 		if (rack->r_ctl.rc_prr_sndcnt >= len)
18961 			rack->r_ctl.rc_prr_sndcnt -= len;
18962 		else
18963 			rack->r_ctl.rc_prr_sndcnt = 0;
18964 	}
18965 	sub_from_prr = 0;
18966 	if (doing_tlp) {
18967 		/* Make sure the TLP is added */
18968 		add_flag |= RACK_TLP;
18969 	} else if (rsm) {
18970 		/* If its a resend without TLP then it must not have the flag */
18971 		rsm->r_flags &= ~RACK_TLP;
18972 	}
18973 	rack_log_output(tp, &to, len, rack_seq, (uint8_t) flags, error,
18974 			rack_to_usec_ts(&tv),
18975 			rsm, add_flag, s_mb, s_moff, hw_tls);
18976 
18977 
18978 	if ((error == 0) &&
18979 	    (len > 0) &&
18980 	    (tp->snd_una == tp->snd_max))
18981 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
18982 	{
18983 		tcp_seq startseq = tp->snd_nxt;
18984 
18985 		/* Track our lost count */
18986 		if (rsm && (doing_tlp == 0))
18987 			rack->r_ctl.rc_loss_count += rsm->r_end - rsm->r_start;
18988 		/*
18989 		 * Advance snd_nxt over sequence space of this segment.
18990 		 */
18991 		if (error)
18992 			/* We don't log or do anything with errors */
18993 			goto nomore;
18994 		if (doing_tlp == 0) {
18995 			if (rsm == NULL) {
18996 				/*
18997 				 * Not a retransmission of some
18998 				 * sort, new data is going out so
18999 				 * clear our TLP count and flag.
19000 				 */
19001 				rack->rc_tlp_in_progress = 0;
19002 				rack->r_ctl.rc_tlp_cnt_out = 0;
19003 			}
19004 		} else {
19005 			/*
19006 			 * We have just sent a TLP, mark that it is true
19007 			 * and make sure our in progress is set so we
19008 			 * continue to check the count.
19009 			 */
19010 			rack->rc_tlp_in_progress = 1;
19011 			rack->r_ctl.rc_tlp_cnt_out++;
19012 		}
19013 		if (flags & (TH_SYN | TH_FIN)) {
19014 			if (flags & TH_SYN)
19015 				tp->snd_nxt++;
19016 			if (flags & TH_FIN) {
19017 				tp->snd_nxt++;
19018 				tp->t_flags |= TF_SENTFIN;
19019 			}
19020 		}
19021 		/* In the ENOBUFS case we do *not* update snd_max */
19022 		if (sack_rxmit)
19023 			goto nomore;
19024 
19025 		tp->snd_nxt += len;
19026 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
19027 			if (tp->snd_una == tp->snd_max) {
19028 				/*
19029 				 * Update the time we just added data since
19030 				 * none was outstanding.
19031 				 */
19032 				rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
19033 				tp->t_acktime = ticks;
19034 			}
19035 			tp->snd_max = tp->snd_nxt;
19036 			/*
19037 			 * Time this transmission if not a retransmission and
19038 			 * not currently timing anything.
19039 			 * This is only relevant in case of switching back to
19040 			 * the base stack.
19041 			 */
19042 			if (tp->t_rtttime == 0) {
19043 				tp->t_rtttime = ticks;
19044 				tp->t_rtseq = startseq;
19045 				KMOD_TCPSTAT_INC(tcps_segstimed);
19046 			}
19047 			if (len &&
19048 			    ((tp->t_flags & TF_GPUTINPROG) == 0))
19049 				rack_start_gp_measurement(tp, rack, startseq, sb_offset);
19050 		}
19051 		/*
19052 		 * If we are doing FO we need to update the mbuf position and subtract
19053 		 * this happens when the peer sends us duplicate information and
19054 		 * we thus want to send a DSACK.
19055 		 *
19056 		 * XXXRRS: This brings to mind a ?, when we send a DSACK block is TSO
19057 		 * turned off? If not then we are going to echo multiple DSACK blocks
19058 		 * out (with the TSO), which we should not be doing.
19059 		 */
19060 		if (rack->r_fast_output && len) {
19061 			if (rack->r_ctl.fsb.left_to_send > len)
19062 				rack->r_ctl.fsb.left_to_send -= len;
19063 			else
19064 				rack->r_ctl.fsb.left_to_send = 0;
19065 			if (rack->r_ctl.fsb.left_to_send < segsiz)
19066 				rack->r_fast_output = 0;
19067 			if (rack->r_fast_output) {
19068 				rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
19069 				rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
19070 			}
19071 		}
19072 	}
19073 nomore:
19074 	if (error) {
19075 		rack->r_ctl.rc_agg_delayed = 0;
19076 		rack->r_early = 0;
19077 		rack->r_late = 0;
19078 		rack->r_ctl.rc_agg_early = 0;
19079 		SOCKBUF_UNLOCK_ASSERT(sb);	/* Check gotos. */
19080 		/*
19081 		 * Failures do not advance the seq counter above. For the
19082 		 * case of ENOBUFS we will fall out and retry in 1ms with
19083 		 * the hpts. Everything else will just have to retransmit
19084 		 * with the timer.
19085 		 *
19086 		 * In any case, we do not want to loop around for another
19087 		 * send without a good reason.
19088 		 */
19089 		sendalot = 0;
19090 		switch (error) {
19091 		case EPERM:
19092 			tp->t_softerror = error;
19093 #ifdef TCP_ACCOUNTING
19094 			crtsc = get_cyclecount();
19095 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19096 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
19097 			}
19098 			counter_u64_add(tcp_cnt_counters[SND_OUT_FAIL], 1);
19099 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19100 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
19101 			}
19102 			counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val));
19103 			sched_unpin();
19104 #endif
19105 			return (error);
19106 		case ENOBUFS:
19107 			/*
19108 			 * Pace us right away to retry in a some
19109 			 * time
19110 			 */
19111 			slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC);
19112 			if (rack->rc_enobuf < 0x7f)
19113 				rack->rc_enobuf++;
19114 			if (slot < (10 * HPTS_USEC_IN_MSEC))
19115 				slot = 10 * HPTS_USEC_IN_MSEC;
19116 			if (rack->r_ctl.crte != NULL) {
19117 				counter_u64_add(rack_saw_enobuf_hw, 1);
19118 				tcp_rl_log_enobuf(rack->r_ctl.crte);
19119 			}
19120 			counter_u64_add(rack_saw_enobuf, 1);
19121 			goto enobufs;
19122 		case EMSGSIZE:
19123 			/*
19124 			 * For some reason the interface we used initially
19125 			 * to send segments changed to another or lowered
19126 			 * its MTU. If TSO was active we either got an
19127 			 * interface without TSO capabilits or TSO was
19128 			 * turned off. If we obtained mtu from ip_output()
19129 			 * then update it and try again.
19130 			 */
19131 			if (tso)
19132 				tp->t_flags &= ~TF_TSO;
19133 			if (mtu != 0) {
19134 				tcp_mss_update(tp, -1, mtu, NULL, NULL);
19135 				goto again;
19136 			}
19137 			slot = 10 * HPTS_USEC_IN_MSEC;
19138 			rack_start_hpts_timer(rack, tp, cts, slot, 0, 0);
19139 #ifdef TCP_ACCOUNTING
19140 			crtsc = get_cyclecount();
19141 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19142 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
19143 			}
19144 			counter_u64_add(tcp_cnt_counters[SND_OUT_FAIL], 1);
19145 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19146 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
19147 			}
19148 			counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val));
19149 			sched_unpin();
19150 #endif
19151 			return (error);
19152 		case ENETUNREACH:
19153 			counter_u64_add(rack_saw_enetunreach, 1);
19154 		case EHOSTDOWN:
19155 		case EHOSTUNREACH:
19156 		case ENETDOWN:
19157 			if (TCPS_HAVERCVDSYN(tp->t_state)) {
19158 				tp->t_softerror = error;
19159 			}
19160 			/* FALLTHROUGH */
19161 		default:
19162 			slot = 10 * HPTS_USEC_IN_MSEC;
19163 			rack_start_hpts_timer(rack, tp, cts, slot, 0, 0);
19164 #ifdef TCP_ACCOUNTING
19165 			crtsc = get_cyclecount();
19166 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19167 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
19168 			}
19169 			counter_u64_add(tcp_cnt_counters[SND_OUT_FAIL], 1);
19170 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19171 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
19172 			}
19173 			counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val));
19174 			sched_unpin();
19175 #endif
19176 			return (error);
19177 		}
19178 	} else {
19179 		rack->rc_enobuf = 0;
19180 		if (IN_FASTRECOVERY(tp->t_flags) && rsm)
19181 			rack->r_ctl.retran_during_recovery += len;
19182 	}
19183 	KMOD_TCPSTAT_INC(tcps_sndtotal);
19184 
19185 	/*
19186 	 * Data sent (as far as we can tell). If this advertises a larger
19187 	 * window than any other segment, then remember the size of the
19188 	 * advertised window. Any pending ACK has now been sent.
19189 	 */
19190 	if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
19191 		tp->rcv_adv = tp->rcv_nxt + recwin;
19192 
19193 	tp->last_ack_sent = tp->rcv_nxt;
19194 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
19195 enobufs:
19196 	if (sendalot) {
19197 		/* Do we need to turn off sendalot? */
19198 		if (rack->r_ctl.rc_pace_max_segs &&
19199 		    (tot_len_this_send >= rack->r_ctl.rc_pace_max_segs)) {
19200 			/* We hit our max. */
19201 			sendalot = 0;
19202 		} else if ((rack->rc_user_set_max_segs) &&
19203 			   (tot_len_this_send >= (rack->rc_user_set_max_segs * segsiz))) {
19204 			/* We hit the user defined max */
19205 			sendalot = 0;
19206 		}
19207 	}
19208 	if ((error == 0) && (flags & TH_FIN))
19209 		tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN);
19210 	if (flags & TH_RST) {
19211 		/*
19212 		 * We don't send again after sending a RST.
19213 		 */
19214 		slot = 0;
19215 		sendalot = 0;
19216 		if (error == 0)
19217 			tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
19218 	} else if ((slot == 0) && (sendalot == 0) && tot_len_this_send) {
19219 		/*
19220 		 * Get our pacing rate, if an error
19221 		 * occurred in sending (ENOBUF) we would
19222 		 * hit the else if with slot preset. Other
19223 		 * errors return.
19224 		 */
19225 		slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, rsm, segsiz);
19226 	}
19227 	if (rsm &&
19228 	    (rsm->r_flags & RACK_HAS_SYN) == 0 &&
19229 	    rack->use_rack_rr) {
19230 		/* Its a retransmit and we use the rack cheat? */
19231 		if ((slot == 0) ||
19232 		    (rack->rc_always_pace == 0) ||
19233 		    (rack->r_rr_config == 1)) {
19234 			/*
19235 			 * We have no pacing set or we
19236 			 * are using old-style rack or
19237 			 * we are overriden to use the old 1ms pacing.
19238 			 */
19239 			slot = rack->r_ctl.rc_min_to;
19240 		}
19241 	}
19242 	/* We have sent clear the flag */
19243 	rack->r_ent_rec_ns = 0;
19244 	if (rack->r_must_retran) {
19245 		if (rsm) {
19246 			rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start);
19247 			if (SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) {
19248 				/*
19249 				 * We have retransmitted all.
19250 				 */
19251 				rack->r_must_retran = 0;
19252 				rack->r_ctl.rc_out_at_rto = 0;
19253 			}
19254 		} else if (SEQ_GEQ(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) {
19255 			/*
19256 			 * Sending new data will also kill
19257 			 * the loop.
19258 			 */
19259 			rack->r_must_retran = 0;
19260 			rack->r_ctl.rc_out_at_rto = 0;
19261 		}
19262 	}
19263 	rack->r_ctl.fsb.recwin = recwin;
19264 	if ((tp->t_flags & (TF_WASCRECOVERY|TF_WASFRECOVERY)) &&
19265 	    SEQ_GT(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) {
19266 		/*
19267 		 * We hit an RTO and now have past snd_max at the RTO
19268 		 * clear all the WAS flags.
19269 		 */
19270 		tp->t_flags &= ~(TF_WASCRECOVERY|TF_WASFRECOVERY);
19271 	}
19272 	if (slot) {
19273 		/* set the rack tcb into the slot N */
19274 		counter_u64_add(rack_paced_segments, 1);
19275 		if ((error == 0) &&
19276 		    rack_use_rfo &&
19277 		    ((flags & (TH_SYN|TH_FIN)) == 0) &&
19278 		    (rsm == NULL) &&
19279 		    (tp->snd_nxt == tp->snd_max) &&
19280 		    (ipoptlen == 0) &&
19281 		    (tp->rcv_numsacks == 0) &&
19282 		    rack->r_fsb_inited &&
19283 		    TCPS_HAVEESTABLISHED(tp->t_state) &&
19284 		    (rack->r_must_retran == 0) &&
19285 		    ((tp->t_flags & TF_NEEDFIN) == 0) &&
19286 		    (len > 0) && (orig_len > 0) &&
19287 		    (orig_len > len) &&
19288 		    ((orig_len - len) >= segsiz) &&
19289 		    ((optlen == 0) ||
19290 		     ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
19291 			/* We can send at least one more MSS using our fsb */
19292 
19293 			rack->r_fast_output = 1;
19294 			rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
19295 			rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
19296 			rack->r_ctl.fsb.tcp_flags = flags;
19297 			rack->r_ctl.fsb.left_to_send = orig_len - len;
19298 			if (hw_tls)
19299 				rack->r_ctl.fsb.hw_tls = 1;
19300 			else
19301 				rack->r_ctl.fsb.hw_tls = 0;
19302 			KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(sb) - (tp->snd_max - tp->snd_una))),
19303 				("rack:%p left_to_send:%u sbavail:%u out:%u",
19304 				 rack, rack->r_ctl.fsb.left_to_send, sbavail(sb),
19305 				 (tp->snd_max - tp->snd_una)));
19306 			if (rack->r_ctl.fsb.left_to_send < segsiz)
19307 				rack->r_fast_output = 0;
19308 			else {
19309 				if (rack->r_ctl.fsb.left_to_send == (sbavail(sb) - (tp->snd_max - tp->snd_una)))
19310 					rack->r_ctl.fsb.rfo_apply_push = 1;
19311 				else
19312 					rack->r_ctl.fsb.rfo_apply_push = 0;
19313 			}
19314 		} else
19315 			rack->r_fast_output = 0;
19316 		rack_log_fsb(rack, tp, so, flags,
19317 			     ipoptlen, orig_len, len, error,
19318 			     (rsm == NULL), optlen, __LINE__, 2);
19319 	} else if (sendalot) {
19320 		int ret;
19321 
19322 		if (len)
19323 			counter_u64_add(rack_unpaced_segments, 1);
19324 		sack_rxmit = 0;
19325 		if ((error == 0) &&
19326 		    rack_use_rfo &&
19327 		    ((flags & (TH_SYN|TH_FIN)) == 0) &&
19328 		    (rsm == NULL) &&
19329 		    (ipoptlen == 0) &&
19330 		    (tp->rcv_numsacks == 0) &&
19331 		    (tp->snd_nxt == tp->snd_max) &&
19332 		    (rack->r_must_retran == 0) &&
19333 		    rack->r_fsb_inited &&
19334 		    TCPS_HAVEESTABLISHED(tp->t_state) &&
19335 		    ((tp->t_flags & TF_NEEDFIN) == 0) &&
19336 		    (len > 0) && (orig_len > 0) &&
19337 		    (orig_len > len) &&
19338 		    ((orig_len - len) >= segsiz) &&
19339 		    ((optlen == 0) ||
19340 		     ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
19341 			/* we can use fast_output for more */
19342 
19343 			rack->r_fast_output = 1;
19344 			rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
19345 			rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
19346 			rack->r_ctl.fsb.tcp_flags = flags;
19347 			rack->r_ctl.fsb.left_to_send = orig_len - len;
19348 			if (hw_tls)
19349 				rack->r_ctl.fsb.hw_tls = 1;
19350 			else
19351 				rack->r_ctl.fsb.hw_tls = 0;
19352 			KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(sb) - (tp->snd_max - tp->snd_una))),
19353 				("rack:%p left_to_send:%u sbavail:%u out:%u",
19354 				 rack, rack->r_ctl.fsb.left_to_send, sbavail(sb),
19355 				 (tp->snd_max - tp->snd_una)));
19356 			if (rack->r_ctl.fsb.left_to_send < segsiz) {
19357 				rack->r_fast_output = 0;
19358 			}
19359 			if (rack->r_fast_output) {
19360 				if (rack->r_ctl.fsb.left_to_send == (sbavail(sb) - (tp->snd_max - tp->snd_una)))
19361 					rack->r_ctl.fsb.rfo_apply_push = 1;
19362 				else
19363 					rack->r_ctl.fsb.rfo_apply_push = 0;
19364 				rack_log_fsb(rack, tp, so, flags,
19365 					     ipoptlen, orig_len, len, error,
19366 					     (rsm == NULL), optlen, __LINE__, 3);
19367 				error = 0;
19368 				ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error);
19369 				if (ret >= 0)
19370 					return (ret);
19371 			        else if (error)
19372 					goto nomore;
19373 
19374 			}
19375 		}
19376 		goto again;
19377 	} else if (len) {
19378 		counter_u64_add(rack_unpaced_segments, 1);
19379 	}
19380 	/* Assure when we leave that snd_nxt will point to top */
19381 	if (SEQ_GT(tp->snd_max, tp->snd_nxt))
19382 		tp->snd_nxt = tp->snd_max;
19383 	rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, 0);
19384 #ifdef TCP_ACCOUNTING
19385 	crtsc = get_cyclecount() - ts_val;
19386 	if (tot_len_this_send) {
19387 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19388 			tp->tcp_cnt_counters[SND_OUT_DATA]++;
19389 		}
19390 		counter_u64_add(tcp_cnt_counters[SND_OUT_DATA], 1);
19391 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19392 			tp->tcp_proc_time[SND_OUT_DATA] += crtsc;
19393 		}
19394 		counter_u64_add(tcp_proc_time[SND_OUT_DATA], crtsc);
19395 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19396 			tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) /segsiz);
19397 		}
19398 		counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((tot_len_this_send + segsiz - 1) /segsiz));
19399 	} else {
19400 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19401 			tp->tcp_cnt_counters[SND_OUT_ACK]++;
19402 		}
19403 		counter_u64_add(tcp_cnt_counters[SND_OUT_ACK], 1);
19404 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19405 			tp->tcp_proc_time[SND_OUT_ACK] += crtsc;
19406 		}
19407 		counter_u64_add(tcp_proc_time[SND_OUT_ACK], crtsc);
19408 	}
19409 	sched_unpin();
19410 #endif
19411 	if (error == ENOBUFS)
19412 		error = 0;
19413 	return (error);
19414 }
19415 
19416 static void
19417 rack_update_seg(struct tcp_rack *rack)
19418 {
19419 	uint32_t orig_val;
19420 
19421 	orig_val = rack->r_ctl.rc_pace_max_segs;
19422 	rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
19423 	if (orig_val != rack->r_ctl.rc_pace_max_segs)
19424 		rack_log_pacing_delay_calc(rack, 0, 0, orig_val, 0, 0, 15, __LINE__, NULL, 0);
19425 }
19426 
19427 static void
19428 rack_mtu_change(struct tcpcb *tp)
19429 {
19430 	/*
19431 	 * The MSS may have changed
19432 	 */
19433 	struct tcp_rack *rack;
19434 	struct rack_sendmap *rsm;
19435 
19436 	rack = (struct tcp_rack *)tp->t_fb_ptr;
19437 	if (rack->r_ctl.rc_pace_min_segs != ctf_fixed_maxseg(tp)) {
19438 		/*
19439 		 * The MTU has changed we need to resend everything
19440 		 * since all we have sent is lost. We first fix
19441 		 * up the mtu though.
19442 		 */
19443 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
19444 		/* We treat this like a full retransmit timeout without the cwnd adjustment */
19445 		rack_remxt_tmr(tp);
19446 		rack->r_fast_output = 0;
19447 		rack->r_ctl.rc_out_at_rto = ctf_flight_size(tp,
19448 						rack->r_ctl.rc_sacked);
19449 		rack->r_ctl.rc_snd_max_at_rto = tp->snd_max;
19450 		rack->r_must_retran = 1;
19451 		/* Mark all inflight to needing to be rxt'd */
19452 		TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
19453 			rsm->r_flags |= RACK_MUST_RXT;
19454 		}
19455 	}
19456 	sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
19457 	/* We don't use snd_nxt to retransmit */
19458 	tp->snd_nxt = tp->snd_max;
19459 }
19460 
19461 static int
19462 rack_set_profile(struct tcp_rack *rack, int prof)
19463 {
19464 	int err = EINVAL;
19465 	if (prof == 1) {
19466 		/* pace_always=1 */
19467 		if (rack->rc_always_pace == 0) {
19468 			if (tcp_can_enable_pacing() == 0)
19469 				return (EBUSY);
19470 		}
19471 		rack->rc_always_pace = 1;
19472 		if (rack->use_fixed_rate || rack->gp_ready)
19473 			rack_set_cc_pacing(rack);
19474 		rack->rc_inp->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19475 		rack->rack_attempt_hdwr_pace = 0;
19476 		/* cmpack=1 */
19477 		if (rack_use_cmp_acks)
19478 			rack->r_use_cmp_ack = 1;
19479 		if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state) &&
19480 		    rack->r_use_cmp_ack)
19481 			rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
19482 		/* scwnd=1 */
19483 		rack->rack_enable_scwnd = 1;
19484 		/* dynamic=100 */
19485 		rack->rc_gp_dyn_mul = 1;
19486 		/* gp_inc_ca */
19487 		rack->r_ctl.rack_per_of_gp_ca = 100;
19488 		/* rrr_conf=3 */
19489 		rack->r_rr_config = 3;
19490 		/* npush=2 */
19491 		rack->r_ctl.rc_no_push_at_mrtt = 2;
19492 		/* fillcw=1 */
19493 		rack->rc_pace_to_cwnd = 1;
19494 		rack->rc_pace_fill_if_rttin_range = 0;
19495 		rack->rtt_limit_mul = 0;
19496 		/* noprr=1 */
19497 		rack->rack_no_prr = 1;
19498 		/* lscwnd=1 */
19499 		rack->r_limit_scw = 1;
19500 		/* gp_inc_rec */
19501 		rack->r_ctl.rack_per_of_gp_rec = 90;
19502 		err = 0;
19503 
19504 	} else if (prof == 3) {
19505 		/* Same as profile one execept fill_cw becomes 2 (less aggressive set) */
19506 		/* pace_always=1 */
19507 		if (rack->rc_always_pace == 0) {
19508 			if (tcp_can_enable_pacing() == 0)
19509 				return (EBUSY);
19510 		}
19511 		rack->rc_always_pace = 1;
19512 		if (rack->use_fixed_rate || rack->gp_ready)
19513 			rack_set_cc_pacing(rack);
19514 		rack->rc_inp->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19515 		rack->rack_attempt_hdwr_pace = 0;
19516 		/* cmpack=1 */
19517 		if (rack_use_cmp_acks)
19518 			rack->r_use_cmp_ack = 1;
19519 		if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state) &&
19520 		    rack->r_use_cmp_ack)
19521 			rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
19522 		/* scwnd=1 */
19523 		rack->rack_enable_scwnd = 1;
19524 		/* dynamic=100 */
19525 		rack->rc_gp_dyn_mul = 1;
19526 		/* gp_inc_ca */
19527 		rack->r_ctl.rack_per_of_gp_ca = 100;
19528 		/* rrr_conf=3 */
19529 		rack->r_rr_config = 3;
19530 		/* npush=2 */
19531 		rack->r_ctl.rc_no_push_at_mrtt = 2;
19532 		/* fillcw=2 */
19533 		rack->rc_pace_to_cwnd = 1;
19534 		rack->r_fill_less_agg = 1;
19535 		rack->rc_pace_fill_if_rttin_range = 0;
19536 		rack->rtt_limit_mul = 0;
19537 		/* noprr=1 */
19538 		rack->rack_no_prr = 1;
19539 		/* lscwnd=1 */
19540 		rack->r_limit_scw = 1;
19541 		/* gp_inc_rec */
19542 		rack->r_ctl.rack_per_of_gp_rec = 90;
19543 		err = 0;
19544 
19545 
19546 	} else if (prof == 2) {
19547 		/* cmpack=1 */
19548 		if (rack->rc_always_pace == 0) {
19549 			if (tcp_can_enable_pacing() == 0)
19550 				return (EBUSY);
19551 		}
19552 		rack->rc_always_pace = 1;
19553 		if (rack->use_fixed_rate || rack->gp_ready)
19554 			rack_set_cc_pacing(rack);
19555 		rack->r_use_cmp_ack = 1;
19556 		if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state))
19557 			rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
19558 		/* pace_always=1 */
19559 		rack->rc_inp->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19560 		/* scwnd=1 */
19561 		rack->rack_enable_scwnd = 1;
19562 		/* dynamic=100 */
19563 		rack->rc_gp_dyn_mul = 1;
19564 		rack->r_ctl.rack_per_of_gp_ca = 100;
19565 		/* rrr_conf=3 */
19566 		rack->r_rr_config = 3;
19567 		/* npush=2 */
19568 		rack->r_ctl.rc_no_push_at_mrtt = 2;
19569 		/* fillcw=1 */
19570 		rack->rc_pace_to_cwnd = 1;
19571 		rack->rc_pace_fill_if_rttin_range = 0;
19572 		rack->rtt_limit_mul = 0;
19573 		/* noprr=1 */
19574 		rack->rack_no_prr = 1;
19575 		/* lscwnd=0 */
19576 		rack->r_limit_scw = 0;
19577 		err = 0;
19578 	} else if (prof == 0) {
19579 		/* This changes things back to the default settings */
19580 		err = 0;
19581 		if (rack->rc_always_pace) {
19582 			tcp_decrement_paced_conn();
19583 			rack_undo_cc_pacing(rack);
19584 			rack->rc_always_pace = 0;
19585 		}
19586 		if (rack_pace_every_seg && tcp_can_enable_pacing()) {
19587 			rack->rc_always_pace = 1;
19588 			if (rack->use_fixed_rate || rack->gp_ready)
19589 				rack_set_cc_pacing(rack);
19590 		} else
19591 			rack->rc_always_pace = 0;
19592 		if (rack_dsack_std_based & 0x1) {
19593 			/* Basically this means all rack timers are at least (srtt + 1/4 srtt) */
19594 			rack->rc_rack_tmr_std_based = 1;
19595 		}
19596 		if (rack_dsack_std_based & 0x2) {
19597 			/* Basically this means  rack timers are extended based on dsack by up to (2 * srtt) */
19598 			rack->rc_rack_use_dsack = 1;
19599 		}
19600 		if (rack_use_cmp_acks)
19601 			rack->r_use_cmp_ack = 1;
19602 		else
19603 			rack->r_use_cmp_ack = 0;
19604 		if (rack_disable_prr)
19605 			rack->rack_no_prr = 1;
19606 		else
19607 			rack->rack_no_prr = 0;
19608 		if (rack_gp_no_rec_chg)
19609 			rack->rc_gp_no_rec_chg = 1;
19610 		else
19611 			rack->rc_gp_no_rec_chg = 0;
19612 		if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack) {
19613 			rack->r_mbuf_queue = 1;
19614 			if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state))
19615 				rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
19616 			rack->rc_inp->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19617 		} else {
19618 			rack->r_mbuf_queue = 0;
19619 			rack->rc_inp->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
19620 		}
19621 		if (rack_enable_shared_cwnd)
19622 			rack->rack_enable_scwnd = 1;
19623 		else
19624 			rack->rack_enable_scwnd = 0;
19625 		if (rack_do_dyn_mul) {
19626 			/* When dynamic adjustment is on CA needs to start at 100% */
19627 			rack->rc_gp_dyn_mul = 1;
19628 			if (rack_do_dyn_mul >= 100)
19629 				rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul;
19630 		} else {
19631 			rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca;
19632 			rack->rc_gp_dyn_mul = 0;
19633 		}
19634 		rack->r_rr_config = 0;
19635 		rack->r_ctl.rc_no_push_at_mrtt = 0;
19636 		rack->rc_pace_to_cwnd = 0;
19637 		rack->rc_pace_fill_if_rttin_range = 0;
19638 		rack->rtt_limit_mul = 0;
19639 
19640 		if (rack_enable_hw_pacing)
19641 			rack->rack_hdw_pace_ena = 1;
19642 		else
19643 			rack->rack_hdw_pace_ena = 0;
19644 		if (rack_disable_prr)
19645 			rack->rack_no_prr = 1;
19646 		else
19647 			rack->rack_no_prr = 0;
19648 		if (rack_limits_scwnd)
19649 			rack->r_limit_scw  = 1;
19650 		else
19651 			rack->r_limit_scw  = 0;
19652 		err = 0;
19653 	}
19654 	return (err);
19655 }
19656 
19657 static int
19658 rack_add_deferred_option(struct tcp_rack *rack, int sopt_name, uint64_t loptval)
19659 {
19660 	struct deferred_opt_list *dol;
19661 
19662 	dol = malloc(sizeof(struct deferred_opt_list),
19663 		     M_TCPFSB, M_NOWAIT|M_ZERO);
19664 	if (dol == NULL) {
19665 		/*
19666 		 * No space yikes -- fail out..
19667 		 */
19668 		return (0);
19669 	}
19670 	dol->optname = sopt_name;
19671 	dol->optval = loptval;
19672 	TAILQ_INSERT_TAIL(&rack->r_ctl.opt_list, dol, next);
19673 	return (1);
19674 }
19675 
19676 static int
19677 rack_process_option(struct tcpcb *tp, struct tcp_rack *rack, int sopt_name,
19678 		    uint32_t optval, uint64_t loptval)
19679 {
19680 	struct epoch_tracker et;
19681 	struct sockopt sopt;
19682 	struct cc_newreno_opts opt;
19683 	uint64_t val;
19684 	int error = 0;
19685 	uint16_t ca, ss;
19686 
19687 	switch (sopt_name) {
19688 
19689 	case TCP_RACK_DSACK_OPT:
19690 		RACK_OPTS_INC(tcp_rack_dsack_opt);
19691 		if (optval & 0x1) {
19692 			rack->rc_rack_tmr_std_based = 1;
19693 		} else {
19694 			rack->rc_rack_tmr_std_based = 0;
19695 		}
19696 		if (optval & 0x2) {
19697 			rack->rc_rack_use_dsack = 1;
19698 		} else {
19699 			rack->rc_rack_use_dsack = 0;
19700 		}
19701 		rack_log_dsack_event(rack, 5, __LINE__, 0, 0);
19702 		break;
19703 	case TCP_RACK_PACING_BETA:
19704 		RACK_OPTS_INC(tcp_rack_beta);
19705 		if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0) {
19706 			/* This only works for newreno. */
19707 			error = EINVAL;
19708 			break;
19709 		}
19710 		if (rack->rc_pacing_cc_set) {
19711 			/*
19712 			 * Set them into the real CC module
19713 			 * whats in the rack pcb is the old values
19714 			 * to be used on restoral/
19715 			 */
19716 			sopt.sopt_dir = SOPT_SET;
19717 			opt.name = CC_NEWRENO_BETA;
19718 			opt.val = optval;
19719 			if (CC_ALGO(tp)->ctl_output != NULL)
19720 				error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
19721 			else {
19722 				error = ENOENT;
19723 				break;
19724 			}
19725 		} else {
19726 			/*
19727 			 * Not pacing yet so set it into our local
19728 			 * rack pcb storage.
19729 			 */
19730 			rack->r_ctl.rc_saved_beta.beta = optval;
19731 		}
19732 		break;
19733 	case TCP_RACK_TIMER_SLOP:
19734 		RACK_OPTS_INC(tcp_rack_timer_slop);
19735 		rack->r_ctl.timer_slop = optval;
19736 		if (rack->rc_tp->t_srtt) {
19737 			/*
19738 			 * If we have an SRTT lets update t_rxtcur
19739 			 * to have the new slop.
19740 			 */
19741 			RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
19742 					   rack_rto_min, rack_rto_max,
19743 					   rack->r_ctl.timer_slop);
19744 		}
19745 		break;
19746 	case TCP_RACK_PACING_BETA_ECN:
19747 		RACK_OPTS_INC(tcp_rack_beta_ecn);
19748 		if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0) {
19749 			/* This only works for newreno. */
19750 			error = EINVAL;
19751 			break;
19752 		}
19753 		if (rack->rc_pacing_cc_set) {
19754 			/*
19755 			 * Set them into the real CC module
19756 			 * whats in the rack pcb is the old values
19757 			 * to be used on restoral/
19758 			 */
19759 			sopt.sopt_dir = SOPT_SET;
19760 			opt.name = CC_NEWRENO_BETA_ECN;
19761 			opt.val = optval;
19762 			if (CC_ALGO(tp)->ctl_output != NULL)
19763 				error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
19764 			else
19765 				error = ENOENT;
19766 		} else {
19767 			/*
19768 			 * Not pacing yet so set it into our local
19769 			 * rack pcb storage.
19770 			 */
19771 			rack->r_ctl.rc_saved_beta.beta_ecn = optval;
19772 			rack->r_ctl.rc_saved_beta.newreno_flags = CC_NEWRENO_BETA_ECN_ENABLED;
19773 		}
19774 		break;
19775 	case TCP_DEFER_OPTIONS:
19776 		RACK_OPTS_INC(tcp_defer_opt);
19777 		if (optval) {
19778 			if (rack->gp_ready) {
19779 				/* Too late */
19780 				error = EINVAL;
19781 				break;
19782 			}
19783 			rack->defer_options = 1;
19784 		} else
19785 			rack->defer_options = 0;
19786 		break;
19787 	case TCP_RACK_MEASURE_CNT:
19788 		RACK_OPTS_INC(tcp_rack_measure_cnt);
19789 		if (optval && (optval <= 0xff)) {
19790 			rack->r_ctl.req_measurements = optval;
19791 		} else
19792 			error = EINVAL;
19793 		break;
19794 	case TCP_REC_ABC_VAL:
19795 		RACK_OPTS_INC(tcp_rec_abc_val);
19796 		if (optval > 0)
19797 			rack->r_use_labc_for_rec = 1;
19798 		else
19799 			rack->r_use_labc_for_rec = 0;
19800 		break;
19801 	case TCP_RACK_ABC_VAL:
19802 		RACK_OPTS_INC(tcp_rack_abc_val);
19803 		if ((optval > 0) && (optval < 255))
19804 			rack->rc_labc = optval;
19805 		else
19806 			error = EINVAL;
19807 		break;
19808 	case TCP_HDWR_UP_ONLY:
19809 		RACK_OPTS_INC(tcp_pacing_up_only);
19810 		if (optval)
19811 			rack->r_up_only = 1;
19812 		else
19813 			rack->r_up_only = 0;
19814 		break;
19815 	case TCP_PACING_RATE_CAP:
19816 		RACK_OPTS_INC(tcp_pacing_rate_cap);
19817 		rack->r_ctl.bw_rate_cap = loptval;
19818 		break;
19819 	case TCP_RACK_PROFILE:
19820 		RACK_OPTS_INC(tcp_profile);
19821 		error = rack_set_profile(rack, optval);
19822 		break;
19823 	case TCP_USE_CMP_ACKS:
19824 		RACK_OPTS_INC(tcp_use_cmp_acks);
19825 		if ((optval == 0) && (rack->rc_inp->inp_flags2 & INP_MBUF_ACKCMP)) {
19826 			/* You can't turn it off once its on! */
19827 			error = EINVAL;
19828 		} else if ((optval == 1) && (rack->r_use_cmp_ack == 0)) {
19829 			rack->r_use_cmp_ack = 1;
19830 			rack->r_mbuf_queue = 1;
19831 			tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19832 		}
19833 		if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
19834 			rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
19835 		break;
19836 	case TCP_SHARED_CWND_TIME_LIMIT:
19837 		RACK_OPTS_INC(tcp_lscwnd);
19838 		if (optval)
19839 			rack->r_limit_scw = 1;
19840 		else
19841 			rack->r_limit_scw = 0;
19842 		break;
19843  	case TCP_RACK_PACE_TO_FILL:
19844 		RACK_OPTS_INC(tcp_fillcw);
19845 		if (optval == 0)
19846 			rack->rc_pace_to_cwnd = 0;
19847 		else {
19848 			rack->rc_pace_to_cwnd = 1;
19849 			if (optval > 1)
19850 				rack->r_fill_less_agg = 1;
19851 		}
19852 		if ((optval >= rack_gp_rtt_maxmul) &&
19853 		    rack_gp_rtt_maxmul &&
19854 		    (optval < 0xf)) {
19855 			rack->rc_pace_fill_if_rttin_range = 1;
19856 			rack->rtt_limit_mul = optval;
19857 		} else {
19858 			rack->rc_pace_fill_if_rttin_range = 0;
19859 			rack->rtt_limit_mul = 0;
19860 		}
19861 		break;
19862 	case TCP_RACK_NO_PUSH_AT_MAX:
19863 		RACK_OPTS_INC(tcp_npush);
19864 		if (optval == 0)
19865 			rack->r_ctl.rc_no_push_at_mrtt = 0;
19866 		else if (optval < 0xff)
19867 			rack->r_ctl.rc_no_push_at_mrtt = optval;
19868 		else
19869 			error = EINVAL;
19870 		break;
19871 	case TCP_SHARED_CWND_ENABLE:
19872 		RACK_OPTS_INC(tcp_rack_scwnd);
19873 		if (optval == 0)
19874 			rack->rack_enable_scwnd = 0;
19875 		else
19876 			rack->rack_enable_scwnd = 1;
19877 		break;
19878 	case TCP_RACK_MBUF_QUEUE:
19879 		/* Now do we use the LRO mbuf-queue feature */
19880 		RACK_OPTS_INC(tcp_rack_mbufq);
19881 		if (optval || rack->r_use_cmp_ack)
19882 			rack->r_mbuf_queue = 1;
19883 		else
19884 			rack->r_mbuf_queue = 0;
19885 		if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
19886 			tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19887 		else
19888 			tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
19889 		break;
19890 	case TCP_RACK_NONRXT_CFG_RATE:
19891 		RACK_OPTS_INC(tcp_rack_cfg_rate);
19892 		if (optval == 0)
19893 			rack->rack_rec_nonrxt_use_cr = 0;
19894 		else
19895 			rack->rack_rec_nonrxt_use_cr = 1;
19896 		break;
19897 	case TCP_NO_PRR:
19898 		RACK_OPTS_INC(tcp_rack_noprr);
19899 		if (optval == 0)
19900 			rack->rack_no_prr = 0;
19901 		else if (optval == 1)
19902 			rack->rack_no_prr = 1;
19903 		else if (optval == 2)
19904 			rack->no_prr_addback = 1;
19905 		else
19906 			error = EINVAL;
19907 		break;
19908 	case TCP_TIMELY_DYN_ADJ:
19909 		RACK_OPTS_INC(tcp_timely_dyn);
19910 		if (optval == 0)
19911 			rack->rc_gp_dyn_mul = 0;
19912 		else {
19913 			rack->rc_gp_dyn_mul = 1;
19914 			if (optval >= 100) {
19915 				/*
19916 				 * If the user sets something 100 or more
19917 				 * its the gp_ca value.
19918 				 */
19919 				rack->r_ctl.rack_per_of_gp_ca  = optval;
19920 			}
19921 		}
19922 		break;
19923 	case TCP_RACK_DO_DETECTION:
19924 		RACK_OPTS_INC(tcp_rack_do_detection);
19925 		if (optval == 0)
19926 			rack->do_detection = 0;
19927 		else
19928 			rack->do_detection = 1;
19929 		break;
19930 	case TCP_RACK_TLP_USE:
19931 		if ((optval < TLP_USE_ID) || (optval > TLP_USE_TWO_TWO)) {
19932 			error = EINVAL;
19933 			break;
19934 		}
19935 		RACK_OPTS_INC(tcp_tlp_use);
19936 		rack->rack_tlp_threshold_use = optval;
19937 		break;
19938 	case TCP_RACK_TLP_REDUCE:
19939 		/* RACK TLP cwnd reduction (bool) */
19940 		RACK_OPTS_INC(tcp_rack_tlp_reduce);
19941 		rack->r_ctl.rc_tlp_cwnd_reduce = optval;
19942 		break;
19943 	/*  Pacing related ones */
19944 	case TCP_RACK_PACE_ALWAYS:
19945 		/*
19946 		 * zero is old rack method, 1 is new
19947 		 * method using a pacing rate.
19948 		 */
19949 		RACK_OPTS_INC(tcp_rack_pace_always);
19950 		if (optval > 0) {
19951 			if (rack->rc_always_pace) {
19952 				error = EALREADY;
19953 				break;
19954 			} else if (tcp_can_enable_pacing()) {
19955 				rack->rc_always_pace = 1;
19956 				if (rack->use_fixed_rate || rack->gp_ready)
19957 					rack_set_cc_pacing(rack);
19958 			}
19959 			else {
19960 				error = ENOSPC;
19961 				break;
19962 			}
19963 		} else {
19964 			if (rack->rc_always_pace) {
19965 				tcp_decrement_paced_conn();
19966 				rack->rc_always_pace = 0;
19967 				rack_undo_cc_pacing(rack);
19968 			}
19969 		}
19970 		if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
19971 			tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19972 		else
19973 			tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
19974 		/* A rate may be set irate or other, if so set seg size */
19975 		rack_update_seg(rack);
19976 		break;
19977 	case TCP_BBR_RACK_INIT_RATE:
19978 		RACK_OPTS_INC(tcp_initial_rate);
19979 		val = optval;
19980 		/* Change from kbits per second to bytes per second */
19981 		val *= 1000;
19982 		val /= 8;
19983 		rack->r_ctl.init_rate = val;
19984 		if (rack->rc_init_win != rack_default_init_window) {
19985 			uint32_t win, snt;
19986 
19987 			/*
19988 			 * Options don't always get applied
19989 			 * in the order you think. So in order
19990 			 * to assure we update a cwnd we need
19991 			 * to check and see if we are still
19992 			 * where we should raise the cwnd.
19993 			 */
19994 			win = rc_init_window(rack);
19995 			if (SEQ_GT(tp->snd_max, tp->iss))
19996 				snt = tp->snd_max - tp->iss;
19997 			else
19998 				snt = 0;
19999 			if ((snt < win) &&
20000 			    (tp->snd_cwnd < win))
20001 				tp->snd_cwnd = win;
20002 		}
20003 		if (rack->rc_always_pace)
20004 			rack_update_seg(rack);
20005 		break;
20006 	case TCP_BBR_IWINTSO:
20007 		RACK_OPTS_INC(tcp_initial_win);
20008 		if (optval && (optval <= 0xff)) {
20009 			uint32_t win, snt;
20010 
20011 			rack->rc_init_win = optval;
20012 			win = rc_init_window(rack);
20013 			if (SEQ_GT(tp->snd_max, tp->iss))
20014 				snt = tp->snd_max - tp->iss;
20015 			else
20016 				snt = 0;
20017 			if ((snt < win) &&
20018 			    (tp->t_srtt |
20019 #ifdef NETFLIX_PEAKRATE
20020 			     tp->t_maxpeakrate |
20021 #endif
20022 			     rack->r_ctl.init_rate)) {
20023 				/*
20024 				 * We are not past the initial window
20025 				 * and we have some bases for pacing,
20026 				 * so we need to possibly adjust up
20027 				 * the cwnd. Note even if we don't set
20028 				 * the cwnd, its still ok to raise the rc_init_win
20029 				 * which can be used coming out of idle when we
20030 				 * would have a rate.
20031 				 */
20032 				if (tp->snd_cwnd < win)
20033 					tp->snd_cwnd = win;
20034 			}
20035 			if (rack->rc_always_pace)
20036 				rack_update_seg(rack);
20037 		} else
20038 			error = EINVAL;
20039 		break;
20040 	case TCP_RACK_FORCE_MSEG:
20041 		RACK_OPTS_INC(tcp_rack_force_max_seg);
20042 		if (optval)
20043 			rack->rc_force_max_seg = 1;
20044 		else
20045 			rack->rc_force_max_seg = 0;
20046 		break;
20047 	case TCP_RACK_PACE_MAX_SEG:
20048 		/* Max segments size in a pace in bytes */
20049 		RACK_OPTS_INC(tcp_rack_max_seg);
20050 		rack->rc_user_set_max_segs = optval;
20051 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
20052 		break;
20053 	case TCP_RACK_PACE_RATE_REC:
20054 		/* Set the fixed pacing rate in Bytes per second ca */
20055 		RACK_OPTS_INC(tcp_rack_pace_rate_rec);
20056 		rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
20057 		if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0)
20058 			rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
20059 		if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0)
20060 			rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
20061 		rack->use_fixed_rate = 1;
20062 		if (rack->rc_always_pace)
20063 			rack_set_cc_pacing(rack);
20064 		rack_log_pacing_delay_calc(rack,
20065 					   rack->r_ctl.rc_fixed_pacing_rate_ss,
20066 					   rack->r_ctl.rc_fixed_pacing_rate_ca,
20067 					   rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
20068 					   __LINE__, NULL,0);
20069 		break;
20070 
20071 	case TCP_RACK_PACE_RATE_SS:
20072 		/* Set the fixed pacing rate in Bytes per second ca */
20073 		RACK_OPTS_INC(tcp_rack_pace_rate_ss);
20074 		rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
20075 		if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0)
20076 			rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
20077 		if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0)
20078 			rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
20079 		rack->use_fixed_rate = 1;
20080 		if (rack->rc_always_pace)
20081 			rack_set_cc_pacing(rack);
20082 		rack_log_pacing_delay_calc(rack,
20083 					   rack->r_ctl.rc_fixed_pacing_rate_ss,
20084 					   rack->r_ctl.rc_fixed_pacing_rate_ca,
20085 					   rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
20086 					   __LINE__, NULL, 0);
20087 		break;
20088 
20089 	case TCP_RACK_PACE_RATE_CA:
20090 		/* Set the fixed pacing rate in Bytes per second ca */
20091 		RACK_OPTS_INC(tcp_rack_pace_rate_ca);
20092 		rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
20093 		if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0)
20094 			rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
20095 		if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0)
20096 			rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
20097 		rack->use_fixed_rate = 1;
20098 		if (rack->rc_always_pace)
20099 			rack_set_cc_pacing(rack);
20100 		rack_log_pacing_delay_calc(rack,
20101 					   rack->r_ctl.rc_fixed_pacing_rate_ss,
20102 					   rack->r_ctl.rc_fixed_pacing_rate_ca,
20103 					   rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
20104 					   __LINE__, NULL, 0);
20105 		break;
20106 	case TCP_RACK_GP_INCREASE_REC:
20107 		RACK_OPTS_INC(tcp_gp_inc_rec);
20108 		rack->r_ctl.rack_per_of_gp_rec = optval;
20109 		rack_log_pacing_delay_calc(rack,
20110 					   rack->r_ctl.rack_per_of_gp_ss,
20111 					   rack->r_ctl.rack_per_of_gp_ca,
20112 					   rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
20113 					   __LINE__, NULL, 0);
20114 		break;
20115 	case TCP_RACK_GP_INCREASE_CA:
20116 		RACK_OPTS_INC(tcp_gp_inc_ca);
20117 		ca = optval;
20118 		if (ca < 100) {
20119 			/*
20120 			 * We don't allow any reduction
20121 			 * over the GP b/w.
20122 			 */
20123 			error = EINVAL;
20124 			break;
20125 		}
20126 		rack->r_ctl.rack_per_of_gp_ca = ca;
20127 		rack_log_pacing_delay_calc(rack,
20128 					   rack->r_ctl.rack_per_of_gp_ss,
20129 					   rack->r_ctl.rack_per_of_gp_ca,
20130 					   rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
20131 					   __LINE__, NULL, 0);
20132 		break;
20133 	case TCP_RACK_GP_INCREASE_SS:
20134 		RACK_OPTS_INC(tcp_gp_inc_ss);
20135 		ss = optval;
20136 		if (ss < 100) {
20137 			/*
20138 			 * We don't allow any reduction
20139 			 * over the GP b/w.
20140 			 */
20141 			error = EINVAL;
20142 			break;
20143 		}
20144 		rack->r_ctl.rack_per_of_gp_ss = ss;
20145 		rack_log_pacing_delay_calc(rack,
20146 					   rack->r_ctl.rack_per_of_gp_ss,
20147 					   rack->r_ctl.rack_per_of_gp_ca,
20148 					   rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
20149 					   __LINE__, NULL, 0);
20150 		break;
20151 	case TCP_RACK_RR_CONF:
20152 		RACK_OPTS_INC(tcp_rack_rrr_no_conf_rate);
20153 		if (optval && optval <= 3)
20154 			rack->r_rr_config = optval;
20155 		else
20156 			rack->r_rr_config = 0;
20157 		break;
20158 	case TCP_HDWR_RATE_CAP:
20159 		RACK_OPTS_INC(tcp_hdwr_rate_cap);
20160 		if (optval) {
20161 			if (rack->r_rack_hw_rate_caps == 0)
20162 				rack->r_rack_hw_rate_caps = 1;
20163 			else
20164 				error = EALREADY;
20165 		} else {
20166 			rack->r_rack_hw_rate_caps = 0;
20167 		}
20168 		break;
20169 	case TCP_BBR_HDWR_PACE:
20170 		RACK_OPTS_INC(tcp_hdwr_pacing);
20171 		if (optval){
20172 			if (rack->rack_hdrw_pacing == 0) {
20173 				rack->rack_hdw_pace_ena = 1;
20174 				rack->rack_attempt_hdwr_pace = 0;
20175 			} else
20176 				error = EALREADY;
20177 		} else {
20178 			rack->rack_hdw_pace_ena = 0;
20179 #ifdef RATELIMIT
20180 			if (rack->r_ctl.crte != NULL) {
20181 				rack->rack_hdrw_pacing = 0;
20182 				rack->rack_attempt_hdwr_pace = 0;
20183 				tcp_rel_pacing_rate(rack->r_ctl.crte, tp);
20184 				rack->r_ctl.crte = NULL;
20185 			}
20186 #endif
20187 		}
20188 		break;
20189 	/*  End Pacing related ones */
20190 	case TCP_RACK_PRR_SENDALOT:
20191 		/* Allow PRR to send more than one seg */
20192 		RACK_OPTS_INC(tcp_rack_prr_sendalot);
20193 		rack->r_ctl.rc_prr_sendalot = optval;
20194 		break;
20195 	case TCP_RACK_MIN_TO:
20196 		/* Minimum time between rack t-o's in ms */
20197 		RACK_OPTS_INC(tcp_rack_min_to);
20198 		rack->r_ctl.rc_min_to = optval;
20199 		break;
20200 	case TCP_RACK_EARLY_SEG:
20201 		/* If early recovery max segments */
20202 		RACK_OPTS_INC(tcp_rack_early_seg);
20203 		rack->r_ctl.rc_early_recovery_segs = optval;
20204 		break;
20205 	case TCP_RACK_ENABLE_HYSTART:
20206 	{
20207 		struct sockopt sopt;
20208 		struct cc_newreno_opts opt;
20209 
20210 		sopt.sopt_valsize = sizeof(struct cc_newreno_opts);
20211 		sopt.sopt_dir = SOPT_SET;
20212 		opt.name = CC_NEWRENO_ENABLE_HYSTART;
20213 		opt.val = optval;
20214 		if (CC_ALGO(tp)->ctl_output != NULL)
20215 			error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
20216 		else
20217 			error = EINVAL;
20218 	}
20219 	break;
20220 	case TCP_RACK_REORD_THRESH:
20221 		/* RACK reorder threshold (shift amount) */
20222 		RACK_OPTS_INC(tcp_rack_reord_thresh);
20223 		if ((optval > 0) && (optval < 31))
20224 			rack->r_ctl.rc_reorder_shift = optval;
20225 		else
20226 			error = EINVAL;
20227 		break;
20228 	case TCP_RACK_REORD_FADE:
20229 		/* Does reordering fade after ms time */
20230 		RACK_OPTS_INC(tcp_rack_reord_fade);
20231 		rack->r_ctl.rc_reorder_fade = optval;
20232 		break;
20233 	case TCP_RACK_TLP_THRESH:
20234 		/* RACK TLP theshold i.e. srtt+(srtt/N) */
20235 		RACK_OPTS_INC(tcp_rack_tlp_thresh);
20236 		if (optval)
20237 			rack->r_ctl.rc_tlp_threshold = optval;
20238 		else
20239 			error = EINVAL;
20240 		break;
20241 	case TCP_BBR_USE_RACK_RR:
20242 		RACK_OPTS_INC(tcp_rack_rr);
20243 		if (optval)
20244 			rack->use_rack_rr = 1;
20245 		else
20246 			rack->use_rack_rr = 0;
20247 		break;
20248 	case TCP_FAST_RSM_HACK:
20249 		RACK_OPTS_INC(tcp_rack_fastrsm_hack);
20250 		if (optval)
20251 			rack->fast_rsm_hack = 1;
20252 		else
20253 			rack->fast_rsm_hack = 0;
20254 		break;
20255 	case TCP_RACK_PKT_DELAY:
20256 		/* RACK added ms i.e. rack-rtt + reord + N */
20257 		RACK_OPTS_INC(tcp_rack_pkt_delay);
20258 		rack->r_ctl.rc_pkt_delay = optval;
20259 		break;
20260 	case TCP_DELACK:
20261 		RACK_OPTS_INC(tcp_rack_delayed_ack);
20262 		if (optval == 0)
20263 			tp->t_delayed_ack = 0;
20264 		else
20265 			tp->t_delayed_ack = 1;
20266 		if (tp->t_flags & TF_DELACK) {
20267 			tp->t_flags &= ~TF_DELACK;
20268 			tp->t_flags |= TF_ACKNOW;
20269 			NET_EPOCH_ENTER(et);
20270 			rack_output(tp);
20271 			NET_EPOCH_EXIT(et);
20272 		}
20273 		break;
20274 
20275 	case TCP_BBR_RACK_RTT_USE:
20276 		RACK_OPTS_INC(tcp_rack_rtt_use);
20277 		if ((optval != USE_RTT_HIGH) &&
20278 		    (optval != USE_RTT_LOW) &&
20279 		    (optval != USE_RTT_AVG))
20280 			error = EINVAL;
20281 		else
20282 			rack->r_ctl.rc_rate_sample_method = optval;
20283 		break;
20284 	case TCP_DATA_AFTER_CLOSE:
20285 		RACK_OPTS_INC(tcp_data_after_close);
20286 		if (optval)
20287 			rack->rc_allow_data_af_clo = 1;
20288 		else
20289 			rack->rc_allow_data_af_clo = 0;
20290 		break;
20291 	default:
20292 		break;
20293 	}
20294 #ifdef NETFLIX_STATS
20295 	tcp_log_socket_option(tp, sopt_name, optval, error);
20296 #endif
20297 	return (error);
20298 }
20299 
20300 
20301 static void
20302 rack_apply_deferred_options(struct tcp_rack *rack)
20303 {
20304 	struct deferred_opt_list *dol, *sdol;
20305 	uint32_t s_optval;
20306 
20307 	TAILQ_FOREACH_SAFE(dol, &rack->r_ctl.opt_list, next, sdol) {
20308 		TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next);
20309 		/* Disadvantage of deferal is you loose the error return */
20310 		s_optval = (uint32_t)dol->optval;
20311 		(void)rack_process_option(rack->rc_tp, rack, dol->optname, s_optval, dol->optval);
20312 		free(dol, M_TCPDO);
20313 	}
20314 }
20315 
20316 static void
20317 rack_hw_tls_change(struct tcpcb *tp, int chg)
20318 {
20319 	/*
20320 	 * HW tls state has changed.. fix all
20321 	 * rsm's in flight.
20322 	 */
20323 	struct tcp_rack *rack;
20324 	struct rack_sendmap *rsm;
20325 
20326 	rack = (struct tcp_rack *)tp->t_fb_ptr;
20327 	RB_FOREACH(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
20328 		if (chg)
20329 			rsm->r_hw_tls = 1;
20330 		else
20331 			rsm->r_hw_tls = 0;
20332 	}
20333 	if (chg)
20334 		rack->r_ctl.fsb.hw_tls = 1;
20335 	else
20336 		rack->r_ctl.fsb.hw_tls = 0;
20337 }
20338 
20339 static int
20340 rack_pru_options(struct tcpcb *tp, int flags)
20341 {
20342 	if (flags & PRUS_OOB)
20343 		return (EOPNOTSUPP);
20344 	return (0);
20345 }
20346 
20347 static struct tcp_function_block __tcp_rack = {
20348 	.tfb_tcp_block_name = __XSTRING(STACKNAME),
20349 	.tfb_tcp_output = rack_output,
20350 	.tfb_do_queued_segments = ctf_do_queued_segments,
20351 	.tfb_do_segment_nounlock = rack_do_segment_nounlock,
20352 	.tfb_tcp_do_segment = rack_do_segment,
20353 	.tfb_tcp_ctloutput = rack_ctloutput,
20354 	.tfb_tcp_fb_init = rack_init,
20355 	.tfb_tcp_fb_fini = rack_fini,
20356 	.tfb_tcp_timer_stop_all = rack_stopall,
20357 	.tfb_tcp_timer_activate = rack_timer_activate,
20358 	.tfb_tcp_timer_active = rack_timer_active,
20359 	.tfb_tcp_timer_stop = rack_timer_stop,
20360 	.tfb_tcp_rexmit_tmr = rack_remxt_tmr,
20361 	.tfb_tcp_handoff_ok = rack_handoff_ok,
20362 	.tfb_tcp_mtu_chg = rack_mtu_change,
20363 	.tfb_pru_options = rack_pru_options,
20364 	.tfb_hwtls_change = rack_hw_tls_change,
20365 	.tfb_flags = TCP_FUNC_OUTPUT_CANDROP,
20366 };
20367 
20368 /*
20369  * rack_ctloutput() must drop the inpcb lock before performing copyin on
20370  * socket option arguments.  When it re-acquires the lock after the copy, it
20371  * has to revalidate that the connection is still valid for the socket
20372  * option.
20373  */
20374 static int
20375 rack_set_sockopt(struct inpcb *inp, struct sockopt *sopt)
20376 {
20377 #ifdef INET6
20378 	struct ip6_hdr *ip6;
20379 #endif
20380 #ifdef INET
20381 	struct ip *ip;
20382 #endif
20383 	struct tcpcb *tp;
20384 	struct tcp_rack *rack;
20385 	uint64_t loptval;
20386 	int32_t error = 0, optval;
20387 
20388 	tp = intotcpcb(inp);
20389 	rack = (struct tcp_rack *)tp->t_fb_ptr;
20390 	if (rack == NULL) {
20391 		INP_WUNLOCK(inp);
20392 		return (EINVAL);
20393 	}
20394 #ifdef INET6
20395 	ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
20396 #endif
20397 #ifdef INET
20398 	ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
20399 #endif
20400 
20401 	switch (sopt->sopt_level) {
20402 #ifdef INET6
20403 	case IPPROTO_IPV6:
20404 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
20405 		switch (sopt->sopt_name) {
20406 		case IPV6_USE_MIN_MTU:
20407 			tcp6_use_min_mtu(tp);
20408 			break;
20409 		case IPV6_TCLASS:
20410 			/*
20411 			 * The DSCP codepoint has changed, update the fsb.
20412 			 */
20413 			ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
20414 			    (rack->rc_inp->inp_flow & IPV6_FLOWINFO_MASK);
20415 			break;
20416 		}
20417 		INP_WUNLOCK(inp);
20418 		return (0);
20419 #endif
20420 #ifdef INET
20421 	case IPPROTO_IP:
20422 		switch (sopt->sopt_name) {
20423 		case IP_TOS:
20424 			/*
20425 			 * The DSCP codepoint has changed, update the fsb.
20426 			 */
20427 			ip->ip_tos = rack->rc_inp->inp_ip_tos;
20428 			break;
20429 		case IP_TTL:
20430 			/*
20431 			 * The TTL has changed, update the fsb.
20432 			 */
20433 			ip->ip_ttl = rack->rc_inp->inp_ip_ttl;
20434 			break;
20435 		}
20436 		INP_WUNLOCK(inp);
20437 		return (0);
20438 #endif
20439 	}
20440 
20441 	switch (sopt->sopt_name) {
20442 	case TCP_RACK_TLP_REDUCE:		/*  URL:tlp_reduce */
20443 	/*  Pacing related ones */
20444 	case TCP_RACK_PACE_ALWAYS:		/*  URL:pace_always */
20445 	case TCP_BBR_RACK_INIT_RATE:		/*  URL:irate */
20446 	case TCP_BBR_IWINTSO:			/*  URL:tso_iwin */
20447 	case TCP_RACK_PACE_MAX_SEG:		/*  URL:pace_max_seg */
20448 	case TCP_RACK_FORCE_MSEG:		/*  URL:force_max_seg */
20449 	case TCP_RACK_PACE_RATE_CA:		/*  URL:pr_ca */
20450 	case TCP_RACK_PACE_RATE_SS:		/*  URL:pr_ss*/
20451 	case TCP_RACK_PACE_RATE_REC:		/*  URL:pr_rec */
20452 	case TCP_RACK_GP_INCREASE_CA:		/*  URL:gp_inc_ca */
20453 	case TCP_RACK_GP_INCREASE_SS:		/*  URL:gp_inc_ss */
20454 	case TCP_RACK_GP_INCREASE_REC:		/*  URL:gp_inc_rec */
20455 	case TCP_RACK_RR_CONF:			/*  URL:rrr_conf */
20456 	case TCP_BBR_HDWR_PACE:			/*  URL:hdwrpace */
20457 	case TCP_HDWR_RATE_CAP:			/*  URL:hdwrcap boolean */
20458 	case TCP_PACING_RATE_CAP:		/*  URL:cap  -- used by side-channel */
20459 	case TCP_HDWR_UP_ONLY:			/*  URL:uponly -- hardware pacing  boolean */
20460        /* End pacing related */
20461 	case TCP_FAST_RSM_HACK:			/*  URL:frsm_hack */
20462 	case TCP_DELACK:			/*  URL:delack (in base TCP i.e. tcp_hints along with cc etc ) */
20463 	case TCP_RACK_PRR_SENDALOT:		/*  URL:prr_sendalot */
20464 	case TCP_RACK_MIN_TO:			/*  URL:min_to */
20465 	case TCP_RACK_EARLY_SEG:		/*  URL:early_seg */
20466 	case TCP_RACK_REORD_THRESH:		/*  URL:reord_thresh */
20467 	case TCP_RACK_REORD_FADE:		/*  URL:reord_fade */
20468 	case TCP_RACK_TLP_THRESH:		/*  URL:tlp_thresh */
20469 	case TCP_RACK_PKT_DELAY:		/*  URL:pkt_delay */
20470 	case TCP_RACK_TLP_USE:			/*  URL:tlp_use */
20471 	case TCP_BBR_RACK_RTT_USE:		/*  URL:rttuse */
20472 	case TCP_BBR_USE_RACK_RR:		/*  URL:rackrr */
20473 	case TCP_RACK_DO_DETECTION:		/*  URL:detect */
20474 	case TCP_NO_PRR:			/*  URL:noprr */
20475 	case TCP_TIMELY_DYN_ADJ:      		/*  URL:dynamic */
20476 	case TCP_DATA_AFTER_CLOSE:		/*  no URL */
20477 	case TCP_RACK_NONRXT_CFG_RATE:		/*  URL:nonrxtcr */
20478 	case TCP_SHARED_CWND_ENABLE:		/*  URL:scwnd */
20479 	case TCP_RACK_MBUF_QUEUE:		/*  URL:mqueue */
20480 	case TCP_RACK_NO_PUSH_AT_MAX:		/*  URL:npush */
20481 	case TCP_RACK_PACE_TO_FILL:		/*  URL:fillcw */
20482 	case TCP_SHARED_CWND_TIME_LIMIT:	/*  URL:lscwnd */
20483 	case TCP_RACK_PROFILE:			/*  URL:profile */
20484 	case TCP_USE_CMP_ACKS:			/*  URL:cmpack */
20485 	case TCP_RACK_ABC_VAL:			/*  URL:labc */
20486 	case TCP_REC_ABC_VAL:			/*  URL:reclabc */
20487 	case TCP_RACK_MEASURE_CNT:		/*  URL:measurecnt */
20488 	case TCP_DEFER_OPTIONS:			/*  URL:defer */
20489 	case TCP_RACK_DSACK_OPT:		/*  URL:dsack */
20490 	case TCP_RACK_PACING_BETA:		/*  URL:pacing_beta */
20491 	case TCP_RACK_PACING_BETA_ECN:		/*  URL:pacing_beta_ecn */
20492 	case TCP_RACK_TIMER_SLOP:		/*  URL:timer_slop */
20493 	case TCP_RACK_ENABLE_HYSTART:		/*  URL:hystart */
20494 		break;
20495 	default:
20496 		/* Filter off all unknown options to the base stack */
20497 		return (tcp_default_ctloutput(inp, sopt));
20498 		break;
20499 	}
20500 	INP_WUNLOCK(inp);
20501 	if (sopt->sopt_name == TCP_PACING_RATE_CAP) {
20502 		error = sooptcopyin(sopt, &loptval, sizeof(loptval), sizeof(loptval));
20503 		/*
20504 		 * We truncate it down to 32 bits for the socket-option trace this
20505 		 * means rates > 34Gbps won't show right, but thats probably ok.
20506 		 */
20507 		optval = (uint32_t)loptval;
20508 	} else {
20509 		error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
20510 		/* Save it in 64 bit form too */
20511 		loptval = optval;
20512 	}
20513 	if (error)
20514 		return (error);
20515 	INP_WLOCK(inp);
20516 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
20517 		INP_WUNLOCK(inp);
20518 		return (ECONNRESET);
20519 	}
20520 	if (tp->t_fb != &__tcp_rack) {
20521 		INP_WUNLOCK(inp);
20522 		return (ENOPROTOOPT);
20523 	}
20524 	if (rack->defer_options && (rack->gp_ready == 0) &&
20525 	    (sopt->sopt_name != TCP_DEFER_OPTIONS) &&
20526 	    (sopt->sopt_name != TCP_RACK_PACING_BETA) &&
20527 	    (sopt->sopt_name != TCP_RACK_PACING_BETA_ECN) &&
20528 	    (sopt->sopt_name != TCP_RACK_MEASURE_CNT)) {
20529 		/* Options are beind deferred */
20530 		if (rack_add_deferred_option(rack, sopt->sopt_name, loptval)) {
20531 			INP_WUNLOCK(inp);
20532 			return (0);
20533 		} else {
20534 			/* No memory to defer, fail */
20535 			INP_WUNLOCK(inp);
20536 			return (ENOMEM);
20537 		}
20538 	}
20539 	error = rack_process_option(tp, rack, sopt->sopt_name, optval, loptval);
20540 	INP_WUNLOCK(inp);
20541 	return (error);
20542 }
20543 
20544 static void
20545 rack_fill_info(struct tcpcb *tp, struct tcp_info *ti)
20546 {
20547 
20548 	INP_WLOCK_ASSERT(tp->t_inpcb);
20549 	bzero(ti, sizeof(*ti));
20550 
20551 	ti->tcpi_state = tp->t_state;
20552 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
20553 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
20554 	if (tp->t_flags & TF_SACK_PERMIT)
20555 		ti->tcpi_options |= TCPI_OPT_SACK;
20556 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
20557 		ti->tcpi_options |= TCPI_OPT_WSCALE;
20558 		ti->tcpi_snd_wscale = tp->snd_scale;
20559 		ti->tcpi_rcv_wscale = tp->rcv_scale;
20560 	}
20561 	if (tp->t_flags2 & TF2_ECN_PERMIT)
20562 		ti->tcpi_options |= TCPI_OPT_ECN;
20563 	if (tp->t_flags & TF_FASTOPEN)
20564 		ti->tcpi_options |= TCPI_OPT_TFO;
20565 	/* still kept in ticks is t_rcvtime */
20566 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
20567 	/* Since we hold everything in precise useconds this is easy */
20568 	ti->tcpi_rtt = tp->t_srtt;
20569 	ti->tcpi_rttvar = tp->t_rttvar;
20570 	ti->tcpi_rto = tp->t_rxtcur;
20571 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
20572 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
20573 	/*
20574 	 * FreeBSD-specific extension fields for tcp_info.
20575 	 */
20576 	ti->tcpi_rcv_space = tp->rcv_wnd;
20577 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
20578 	ti->tcpi_snd_wnd = tp->snd_wnd;
20579 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
20580 	ti->tcpi_snd_nxt = tp->snd_nxt;
20581 	ti->tcpi_snd_mss = tp->t_maxseg;
20582 	ti->tcpi_rcv_mss = tp->t_maxseg;
20583 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
20584 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
20585 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
20586 #ifdef NETFLIX_STATS
20587 	ti->tcpi_total_tlp = tp->t_sndtlppack;
20588 	ti->tcpi_total_tlp_bytes = tp->t_sndtlpbyte;
20589 	memcpy(&ti->tcpi_rxsyninfo, &tp->t_rxsyninfo, sizeof(struct tcpsyninfo));
20590 #endif
20591 #ifdef TCP_OFFLOAD
20592 	if (tp->t_flags & TF_TOE) {
20593 		ti->tcpi_options |= TCPI_OPT_TOE;
20594 		tcp_offload_tcp_info(tp, ti);
20595 	}
20596 #endif
20597 }
20598 
20599 static int
20600 rack_get_sockopt(struct inpcb *inp, struct sockopt *sopt)
20601 {
20602 	struct tcpcb *tp;
20603 	struct tcp_rack *rack;
20604 	int32_t error, optval;
20605 	uint64_t val, loptval;
20606 	struct	tcp_info ti;
20607 	/*
20608 	 * Because all our options are either boolean or an int, we can just
20609 	 * pull everything into optval and then unlock and copy. If we ever
20610 	 * add a option that is not a int, then this will have quite an
20611 	 * impact to this routine.
20612 	 */
20613 	error = 0;
20614 	tp = intotcpcb(inp);
20615 	rack = (struct tcp_rack *)tp->t_fb_ptr;
20616 	if (rack == NULL) {
20617 		INP_WUNLOCK(inp);
20618 		return (EINVAL);
20619 	}
20620 	switch (sopt->sopt_name) {
20621 	case TCP_INFO:
20622 		/* First get the info filled */
20623 		rack_fill_info(tp, &ti);
20624 		/* Fix up the rtt related fields if needed */
20625 		INP_WUNLOCK(inp);
20626 		error = sooptcopyout(sopt, &ti, sizeof ti);
20627 		return (error);
20628 	/*
20629 	 * Beta is the congestion control value for NewReno that influences how
20630 	 * much of a backoff happens when loss is detected. It is normally set
20631 	 * to 50 for 50% i.e. the cwnd is reduced to 50% of its previous value
20632 	 * when you exit recovery.
20633 	 */
20634 	case TCP_RACK_PACING_BETA:
20635 		if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0)
20636 			error = EINVAL;
20637 		else if (rack->rc_pacing_cc_set == 0)
20638 			optval = rack->r_ctl.rc_saved_beta.beta;
20639 		else {
20640 			/*
20641 			 * Reach out into the CC data and report back what
20642 			 * I have previously set. Yeah it looks hackish but
20643 			 * we don't want to report the saved values.
20644 			 */
20645 			if (tp->ccv->cc_data)
20646 				optval = ((struct newreno *)tp->ccv->cc_data)->beta;
20647 			else
20648 				error = EINVAL;
20649 		}
20650 		break;
20651 		/*
20652 		 * Beta_ecn is the congestion control value for NewReno that influences how
20653 		 * much of a backoff happens when a ECN mark is detected. It is normally set
20654 		 * to 80 for 80% i.e. the cwnd is reduced by 20% of its previous value when
20655 		 * you exit recovery. Note that classic ECN has a beta of 50, it is only
20656 		 * ABE Ecn that uses this "less" value, but we do too with pacing :)
20657 		 */
20658 
20659 	case TCP_RACK_PACING_BETA_ECN:
20660 		if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0)
20661 			error = EINVAL;
20662 		else if (rack->rc_pacing_cc_set == 0)
20663 			optval = rack->r_ctl.rc_saved_beta.beta_ecn;
20664 		else {
20665 			/*
20666 			 * Reach out into the CC data and report back what
20667 			 * I have previously set. Yeah it looks hackish but
20668 			 * we don't want to report the saved values.
20669 			 */
20670 			if (tp->ccv->cc_data)
20671 				optval = ((struct newreno *)tp->ccv->cc_data)->beta_ecn;
20672 			else
20673 				error = EINVAL;
20674 		}
20675 		break;
20676 	case TCP_RACK_DSACK_OPT:
20677 		optval = 0;
20678 		if (rack->rc_rack_tmr_std_based) {
20679 			optval |= 1;
20680 		}
20681 		if (rack->rc_rack_use_dsack) {
20682 			optval |= 2;
20683 		}
20684 		break;
20685  	case TCP_RACK_ENABLE_HYSTART:
20686 	{
20687 		struct sockopt sopt;
20688 		struct cc_newreno_opts opt;
20689 
20690 		sopt.sopt_valsize = sizeof(struct cc_newreno_opts);
20691 		sopt.sopt_dir = SOPT_GET;
20692 		opt.name = CC_NEWRENO_ENABLE_HYSTART;
20693 		if (CC_ALGO(tp)->ctl_output != NULL)
20694 			error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
20695 		else
20696 			error = EINVAL;
20697 		optval = opt.val;
20698 	}
20699 	break;
20700 	case TCP_FAST_RSM_HACK:
20701 		optval = rack->fast_rsm_hack;
20702 		break;
20703 	case TCP_DEFER_OPTIONS:
20704 		optval = rack->defer_options;
20705 		break;
20706 	case TCP_RACK_MEASURE_CNT:
20707 		optval = rack->r_ctl.req_measurements;
20708 		break;
20709 	case TCP_REC_ABC_VAL:
20710 		optval = rack->r_use_labc_for_rec;
20711 		break;
20712 	case TCP_RACK_ABC_VAL:
20713 		optval = rack->rc_labc;
20714 		break;
20715 	case TCP_HDWR_UP_ONLY:
20716 		optval= rack->r_up_only;
20717 		break;
20718 	case TCP_PACING_RATE_CAP:
20719 		loptval = rack->r_ctl.bw_rate_cap;
20720 		break;
20721 	case TCP_RACK_PROFILE:
20722 		/* You cannot retrieve a profile, its write only */
20723 		error = EINVAL;
20724 		break;
20725 	case TCP_USE_CMP_ACKS:
20726 		optval = rack->r_use_cmp_ack;
20727 		break;
20728 	case TCP_RACK_PACE_TO_FILL:
20729 		optval = rack->rc_pace_to_cwnd;
20730 		if (optval && rack->r_fill_less_agg)
20731 			optval++;
20732 		break;
20733 	case TCP_RACK_NO_PUSH_AT_MAX:
20734 		optval = rack->r_ctl.rc_no_push_at_mrtt;
20735 		break;
20736 	case TCP_SHARED_CWND_ENABLE:
20737 		optval = rack->rack_enable_scwnd;
20738 		break;
20739 	case TCP_RACK_NONRXT_CFG_RATE:
20740 		optval = rack->rack_rec_nonrxt_use_cr;
20741 		break;
20742 	case TCP_NO_PRR:
20743 		if (rack->rack_no_prr  == 1)
20744 			optval = 1;
20745 		else if (rack->no_prr_addback == 1)
20746 			optval = 2;
20747 		else
20748 			optval = 0;
20749 		break;
20750 	case TCP_RACK_DO_DETECTION:
20751 		optval = rack->do_detection;
20752 		break;
20753 	case TCP_RACK_MBUF_QUEUE:
20754 		/* Now do we use the LRO mbuf-queue feature */
20755 		optval = rack->r_mbuf_queue;
20756 		break;
20757 	case TCP_TIMELY_DYN_ADJ:
20758 		optval = rack->rc_gp_dyn_mul;
20759 		break;
20760 	case TCP_BBR_IWINTSO:
20761 		optval = rack->rc_init_win;
20762 		break;
20763 	case TCP_RACK_TLP_REDUCE:
20764 		/* RACK TLP cwnd reduction (bool) */
20765 		optval = rack->r_ctl.rc_tlp_cwnd_reduce;
20766 		break;
20767 	case TCP_BBR_RACK_INIT_RATE:
20768 		val = rack->r_ctl.init_rate;
20769 		/* convert to kbits per sec */
20770 		val *= 8;
20771 		val /= 1000;
20772 		optval = (uint32_t)val;
20773 		break;
20774 	case TCP_RACK_FORCE_MSEG:
20775 		optval = rack->rc_force_max_seg;
20776 		break;
20777 	case TCP_RACK_PACE_MAX_SEG:
20778 		/* Max segments in a pace */
20779 		optval = rack->rc_user_set_max_segs;
20780 		break;
20781 	case TCP_RACK_PACE_ALWAYS:
20782 		/* Use the always pace method */
20783 		optval = rack->rc_always_pace;
20784 		break;
20785 	case TCP_RACK_PRR_SENDALOT:
20786 		/* Allow PRR to send more than one seg */
20787 		optval = rack->r_ctl.rc_prr_sendalot;
20788 		break;
20789 	case TCP_RACK_MIN_TO:
20790 		/* Minimum time between rack t-o's in ms */
20791 		optval = rack->r_ctl.rc_min_to;
20792 		break;
20793 	case TCP_RACK_EARLY_SEG:
20794 		/* If early recovery max segments */
20795 		optval = rack->r_ctl.rc_early_recovery_segs;
20796 		break;
20797 	case TCP_RACK_REORD_THRESH:
20798 		/* RACK reorder threshold (shift amount) */
20799 		optval = rack->r_ctl.rc_reorder_shift;
20800 		break;
20801 	case TCP_RACK_REORD_FADE:
20802 		/* Does reordering fade after ms time */
20803 		optval = rack->r_ctl.rc_reorder_fade;
20804 		break;
20805 	case TCP_BBR_USE_RACK_RR:
20806 		/* Do we use the rack cheat for rxt */
20807 		optval = rack->use_rack_rr;
20808 		break;
20809 	case TCP_RACK_RR_CONF:
20810 		optval = rack->r_rr_config;
20811 		break;
20812 	case TCP_HDWR_RATE_CAP:
20813 		optval = rack->r_rack_hw_rate_caps;
20814 		break;
20815 	case TCP_BBR_HDWR_PACE:
20816 		optval = rack->rack_hdw_pace_ena;
20817 		break;
20818 	case TCP_RACK_TLP_THRESH:
20819 		/* RACK TLP theshold i.e. srtt+(srtt/N) */
20820 		optval = rack->r_ctl.rc_tlp_threshold;
20821 		break;
20822 	case TCP_RACK_PKT_DELAY:
20823 		/* RACK added ms i.e. rack-rtt + reord + N */
20824 		optval = rack->r_ctl.rc_pkt_delay;
20825 		break;
20826 	case TCP_RACK_TLP_USE:
20827 		optval = rack->rack_tlp_threshold_use;
20828 		break;
20829 	case TCP_RACK_PACE_RATE_CA:
20830 		optval = rack->r_ctl.rc_fixed_pacing_rate_ca;
20831 		break;
20832 	case TCP_RACK_PACE_RATE_SS:
20833 		optval = rack->r_ctl.rc_fixed_pacing_rate_ss;
20834 		break;
20835 	case TCP_RACK_PACE_RATE_REC:
20836 		optval = rack->r_ctl.rc_fixed_pacing_rate_rec;
20837 		break;
20838 	case TCP_RACK_GP_INCREASE_SS:
20839 		optval = rack->r_ctl.rack_per_of_gp_ca;
20840 		break;
20841 	case TCP_RACK_GP_INCREASE_CA:
20842 		optval = rack->r_ctl.rack_per_of_gp_ss;
20843 		break;
20844 	case TCP_BBR_RACK_RTT_USE:
20845 		optval = rack->r_ctl.rc_rate_sample_method;
20846 		break;
20847 	case TCP_DELACK:
20848 		optval = tp->t_delayed_ack;
20849 		break;
20850 	case TCP_DATA_AFTER_CLOSE:
20851 		optval = rack->rc_allow_data_af_clo;
20852 		break;
20853 	case TCP_SHARED_CWND_TIME_LIMIT:
20854 		optval = rack->r_limit_scw;
20855 		break;
20856 	case TCP_RACK_TIMER_SLOP:
20857 		optval = rack->r_ctl.timer_slop;
20858 		break;
20859 	default:
20860 		return (tcp_default_ctloutput(inp, sopt));
20861 		break;
20862 	}
20863 	INP_WUNLOCK(inp);
20864 	if (error == 0) {
20865 		if (TCP_PACING_RATE_CAP)
20866 			error = sooptcopyout(sopt, &loptval, sizeof loptval);
20867 		else
20868 			error = sooptcopyout(sopt, &optval, sizeof optval);
20869 	}
20870 	return (error);
20871 }
20872 
20873 static int
20874 rack_ctloutput(struct inpcb *inp, struct sockopt *sopt)
20875 {
20876 	if (sopt->sopt_dir == SOPT_SET) {
20877 		return (rack_set_sockopt(inp, sopt));
20878 	} else if (sopt->sopt_dir == SOPT_GET) {
20879 		return (rack_get_sockopt(inp, sopt));
20880 	} else {
20881 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
20882 	}
20883 }
20884 
20885 static const char *rack_stack_names[] = {
20886 	__XSTRING(STACKNAME),
20887 #ifdef STACKALIAS
20888 	__XSTRING(STACKALIAS),
20889 #endif
20890 };
20891 
20892 static int
20893 rack_ctor(void *mem, int32_t size, void *arg, int32_t how)
20894 {
20895 	memset(mem, 0, size);
20896 	return (0);
20897 }
20898 
20899 static void
20900 rack_dtor(void *mem, int32_t size, void *arg)
20901 {
20902 
20903 }
20904 
20905 static bool rack_mod_inited = false;
20906 
20907 static int
20908 tcp_addrack(module_t mod, int32_t type, void *data)
20909 {
20910 	int32_t err = 0;
20911 	int num_stacks;
20912 
20913 	switch (type) {
20914 	case MOD_LOAD:
20915 		rack_zone = uma_zcreate(__XSTRING(MODNAME) "_map",
20916 		    sizeof(struct rack_sendmap),
20917 		    rack_ctor, rack_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
20918 
20919 		rack_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb",
20920 		    sizeof(struct tcp_rack),
20921 		    rack_ctor, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
20922 
20923 		sysctl_ctx_init(&rack_sysctl_ctx);
20924 		rack_sysctl_root = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
20925 		    SYSCTL_STATIC_CHILDREN(_net_inet_tcp),
20926 		    OID_AUTO,
20927 #ifdef STACKALIAS
20928 		    __XSTRING(STACKALIAS),
20929 #else
20930 		    __XSTRING(STACKNAME),
20931 #endif
20932 		    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
20933 		    "");
20934 		if (rack_sysctl_root == NULL) {
20935 			printf("Failed to add sysctl node\n");
20936 			err = EFAULT;
20937 			goto free_uma;
20938 		}
20939 		rack_init_sysctls();
20940 		num_stacks = nitems(rack_stack_names);
20941 		err = register_tcp_functions_as_names(&__tcp_rack, M_WAITOK,
20942 		    rack_stack_names, &num_stacks);
20943 		if (err) {
20944 			printf("Failed to register %s stack name for "
20945 			    "%s module\n", rack_stack_names[num_stacks],
20946 			    __XSTRING(MODNAME));
20947 			sysctl_ctx_free(&rack_sysctl_ctx);
20948 free_uma:
20949 			uma_zdestroy(rack_zone);
20950 			uma_zdestroy(rack_pcb_zone);
20951 			rack_counter_destroy();
20952 			printf("Failed to register rack module -- err:%d\n", err);
20953 			return (err);
20954 		}
20955 		tcp_lro_reg_mbufq();
20956 		rack_mod_inited = true;
20957 		break;
20958 	case MOD_QUIESCE:
20959 		err = deregister_tcp_functions(&__tcp_rack, true, false);
20960 		break;
20961 	case MOD_UNLOAD:
20962 		err = deregister_tcp_functions(&__tcp_rack, false, true);
20963 		if (err == EBUSY)
20964 			break;
20965 		if (rack_mod_inited) {
20966 			uma_zdestroy(rack_zone);
20967 			uma_zdestroy(rack_pcb_zone);
20968 			sysctl_ctx_free(&rack_sysctl_ctx);
20969 			rack_counter_destroy();
20970 			rack_mod_inited = false;
20971 		}
20972 		tcp_lro_dereg_mbufq();
20973 		err = 0;
20974 		break;
20975 	default:
20976 		return (EOPNOTSUPP);
20977 	}
20978 	return (err);
20979 }
20980 
20981 static moduledata_t tcp_rack = {
20982 	.name = __XSTRING(MODNAME),
20983 	.evhand = tcp_addrack,
20984 	.priv = 0
20985 };
20986 
20987 MODULE_VERSION(MODNAME, 1);
20988 DECLARE_MODULE(MODNAME, tcp_rack, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
20989 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1);
20990