xref: /freebsd/sys/netinet/tcp_stacks/rack.c (revision f0d4c2afd65f821b8983152848f4005239ff975a)
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 /*
245  * Currently regular tcp has a rto_min of 30ms
246  * the backoff goes 12 times so that ends up
247  * being a total of 122.850 seconds before a
248  * connection is killed.
249  */
250 static uint32_t rack_def_data_window = 20;
251 static uint32_t rack_goal_bdp = 2;
252 static uint32_t rack_min_srtts = 1;
253 static uint32_t rack_min_measure_usec = 0;
254 static int32_t rack_tlp_min = 10000;	/* 10ms */
255 static int32_t rack_rto_min = 30000;	/* 30,000 usec same as main freebsd */
256 static int32_t rack_rto_max = 4000000;	/* 4 seconds in usec's */
257 static const int32_t rack_free_cache = 2;
258 static int32_t rack_hptsi_segments = 40;
259 static int32_t rack_rate_sample_method = USE_RTT_LOW;
260 static int32_t rack_pace_every_seg = 0;
261 static int32_t rack_delayed_ack_time = 40000;	/* 40ms in usecs */
262 static int32_t rack_slot_reduction = 4;
263 static int32_t rack_wma_divisor = 8;		/* For WMA calculation */
264 static int32_t rack_cwnd_block_ends_measure = 0;
265 static int32_t rack_rwnd_block_ends_measure = 0;
266 static int32_t rack_def_profile = 0;
267 
268 static int32_t rack_lower_cwnd_at_tlp = 0;
269 static int32_t rack_limited_retran = 0;
270 static int32_t rack_always_send_oldest = 0;
271 static int32_t rack_tlp_threshold_use = TLP_USE_TWO_ONE;
272 
273 static uint16_t rack_per_of_gp_ss = 250;	/* 250 % slow-start */
274 static uint16_t rack_per_of_gp_ca = 200;	/* 200 % congestion-avoidance */
275 static uint16_t rack_per_of_gp_rec = 200;	/* 200 % of bw */
276 
277 /* Probertt */
278 static uint16_t rack_per_of_gp_probertt = 60;	/* 60% of bw */
279 static uint16_t rack_per_of_gp_lowthresh = 40;	/* 40% is bottom */
280 static uint16_t rack_per_of_gp_probertt_reduce = 10; /* 10% reduction */
281 static uint16_t rack_atexit_prtt_hbp = 130;	/* Clamp to 130% on exit prtt if highly buffered path */
282 static uint16_t rack_atexit_prtt = 130;	/* Clamp to 100% on exit prtt if non highly buffered path */
283 
284 static uint32_t rack_max_drain_wait = 2;	/* How man gp srtt's before we give up draining */
285 static uint32_t rack_must_drain = 1;		/* How many GP srtt's we *must* wait */
286 static uint32_t rack_probertt_use_min_rtt_entry = 1;	/* Use the min to calculate the goal else gp_srtt */
287 static uint32_t rack_probertt_use_min_rtt_exit = 0;
288 static uint32_t rack_probe_rtt_sets_cwnd = 0;
289 static uint32_t rack_probe_rtt_safety_val = 2000000;	/* No more than 2 sec in probe-rtt */
290 static uint32_t rack_time_between_probertt = 9600000;	/* 9.6 sec in usecs */
291 static uint32_t rack_probertt_gpsrtt_cnt_mul = 0;	/* How many srtt periods does probe-rtt last top fraction */
292 static uint32_t rack_probertt_gpsrtt_cnt_div = 0;	/* How many srtt periods does probe-rtt last bottom fraction */
293 static uint32_t rack_min_probertt_hold = 40000;		/* Equal to delayed ack time */
294 static uint32_t rack_probertt_filter_life = 10000000;
295 static uint32_t rack_probertt_lower_within = 10;
296 static uint32_t rack_min_rtt_movement = 250000;	/* Must move at least 250ms (in microseconds)  to count as a lowering */
297 static int32_t rack_pace_one_seg = 0;		/* Shall we pace for less than 1.4Meg 1MSS at a time */
298 static int32_t rack_probertt_clear_is = 1;
299 static int32_t rack_max_drain_hbp = 1;		/* Extra drain times gpsrtt for highly buffered paths */
300 static int32_t rack_hbp_thresh = 3;		/* what is the divisor max_rtt/min_rtt to decided a hbp */
301 
302 /* Part of pacing */
303 static int32_t rack_max_per_above = 30;		/* When we go to increment stop if above 100+this% */
304 
305 /* Timely information */
306 /* Combine these two gives the range of 'no change' to bw */
307 /* ie the up/down provide the upper and lower bound */
308 static int32_t rack_gp_per_bw_mul_up = 2;	/* 2% */
309 static int32_t rack_gp_per_bw_mul_down = 4;	/* 4% */
310 static int32_t rack_gp_rtt_maxmul = 3;		/* 3 x maxmin */
311 static int32_t rack_gp_rtt_minmul = 1;		/* minrtt + (minrtt/mindiv) is lower rtt */
312 static int32_t rack_gp_rtt_mindiv = 4;		/* minrtt + (minrtt * minmul/mindiv) is lower rtt */
313 static int32_t rack_gp_decrease_per = 20;	/* 20% decrease in multiplier */
314 static int32_t rack_gp_increase_per = 2;	/* 2% increase in multiplier */
315 static int32_t rack_per_lower_bound = 50;	/* Don't allow to drop below this multiplier */
316 static int32_t rack_per_upper_bound_ss = 0;	/* Don't allow SS to grow above this */
317 static int32_t rack_per_upper_bound_ca = 0;	/* Don't allow CA to grow above this */
318 static int32_t rack_do_dyn_mul = 0;		/* Are the rack gp multipliers dynamic */
319 static int32_t rack_gp_no_rec_chg = 1;		/* Prohibit recovery from reducing it's multiplier */
320 static int32_t rack_timely_dec_clear = 6;	/* Do we clear decrement count at a value (6)? */
321 static int32_t rack_timely_max_push_rise = 3;	/* One round of pushing */
322 static int32_t rack_timely_max_push_drop = 3;	/* Three round of pushing */
323 static int32_t rack_timely_min_segs = 4;	/* 4 segment minimum */
324 static int32_t rack_use_max_for_nobackoff = 0;
325 static int32_t rack_timely_int_timely_only = 0;	/* do interim timely's only use the timely algo (no b/w changes)? */
326 static int32_t rack_timely_no_stopping = 0;
327 static int32_t rack_down_raise_thresh = 100;
328 static int32_t rack_req_segs = 1;
329 static uint64_t rack_bw_rate_cap = 0;
330 static uint32_t rack_trace_point_config = 0;
331 static uint32_t rack_trace_point_bb_mode = 4;
332 static int32_t rack_trace_point_count = 0;
333 
334 
335 /* Weird delayed ack mode */
336 static int32_t rack_use_imac_dack = 0;
337 /* Rack specific counters */
338 counter_u64_t rack_saw_enobuf;
339 counter_u64_t rack_saw_enobuf_hw;
340 counter_u64_t rack_saw_enetunreach;
341 counter_u64_t rack_persists_sends;
342 counter_u64_t rack_persists_acks;
343 counter_u64_t rack_persists_loss;
344 counter_u64_t rack_persists_lost_ends;
345 #ifdef INVARIANTS
346 counter_u64_t rack_adjust_map_bw;
347 #endif
348 /* Tail loss probe counters */
349 counter_u64_t rack_tlp_tot;
350 counter_u64_t rack_tlp_newdata;
351 counter_u64_t rack_tlp_retran;
352 counter_u64_t rack_tlp_retran_bytes;
353 counter_u64_t rack_to_tot;
354 counter_u64_t rack_hot_alloc;
355 counter_u64_t rack_to_alloc;
356 counter_u64_t rack_to_alloc_hard;
357 counter_u64_t rack_to_alloc_emerg;
358 counter_u64_t rack_to_alloc_limited;
359 counter_u64_t rack_alloc_limited_conns;
360 counter_u64_t rack_split_limited;
361 
362 counter_u64_t rack_multi_single_eq;
363 counter_u64_t rack_proc_non_comp_ack;
364 
365 counter_u64_t rack_fto_send;
366 counter_u64_t rack_fto_rsm_send;
367 counter_u64_t rack_nfto_resend;
368 counter_u64_t rack_non_fto_send;
369 counter_u64_t rack_extended_rfo;
370 
371 counter_u64_t rack_sack_proc_all;
372 counter_u64_t rack_sack_proc_short;
373 counter_u64_t rack_sack_proc_restart;
374 counter_u64_t rack_sack_attacks_detected;
375 counter_u64_t rack_sack_attacks_reversed;
376 counter_u64_t rack_sack_used_next_merge;
377 counter_u64_t rack_sack_splits;
378 counter_u64_t rack_sack_used_prev_merge;
379 counter_u64_t rack_sack_skipped_acked;
380 counter_u64_t rack_ack_total;
381 counter_u64_t rack_express_sack;
382 counter_u64_t rack_sack_total;
383 counter_u64_t rack_move_none;
384 counter_u64_t rack_move_some;
385 
386 counter_u64_t rack_input_idle_reduces;
387 counter_u64_t rack_collapsed_win;
388 counter_u64_t rack_collapsed_win_seen;
389 counter_u64_t rack_collapsed_win_rxt;
390 counter_u64_t rack_collapsed_win_rxt_bytes;
391 counter_u64_t rack_try_scwnd;
392 counter_u64_t rack_hw_pace_init_fail;
393 counter_u64_t rack_hw_pace_lost;
394 
395 counter_u64_t rack_out_size[TCP_MSS_ACCT_SIZE];
396 counter_u64_t rack_opts_arry[RACK_OPTS_SIZE];
397 
398 
399 #define	RACK_REXMTVAL(tp) max(rack_rto_min, ((tp)->t_srtt + ((tp)->t_rttvar << 2)))
400 
401 #define	RACK_TCPT_RANGESET(tv, value, tvmin, tvmax, slop) do {	\
402 	(tv) = (value) + slop;	 \
403 	if ((u_long)(tv) < (u_long)(tvmin)) \
404 		(tv) = (tvmin); \
405 	if ((u_long)(tv) > (u_long)(tvmax)) \
406 		(tv) = (tvmax); \
407 } while (0)
408 
409 static void
410 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick,  int event, int line);
411 
412 static int
413 rack_process_ack(struct mbuf *m, struct tcphdr *th,
414     struct socket *so, struct tcpcb *tp, struct tcpopt *to,
415     uint32_t tiwin, int32_t tlen, int32_t * ofia, int32_t thflags, int32_t * ret_val);
416 static int
417 rack_process_data(struct mbuf *m, struct tcphdr *th,
418     struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
419     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt);
420 static void
421 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack,
422    uint32_t th_ack, uint16_t nsegs, uint16_t type, int32_t recovery);
423 static struct rack_sendmap *rack_alloc(struct tcp_rack *rack);
424 static struct rack_sendmap *rack_alloc_limit(struct tcp_rack *rack,
425     uint8_t limit_type);
426 static struct rack_sendmap *
427 rack_check_recovery_mode(struct tcpcb *tp,
428     uint32_t tsused);
429 static void
430 rack_cong_signal(struct tcpcb *tp,
431 		 uint32_t type, uint32_t ack, int );
432 static void rack_counter_destroy(void);
433 static int
434 rack_ctloutput(struct inpcb *inp, struct sockopt *sopt);
435 static int32_t rack_ctor(void *mem, int32_t size, void *arg, int32_t how);
436 static void
437 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override);
438 static void
439 rack_do_segment(struct mbuf *m, struct tcphdr *th,
440     struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
441     uint8_t iptos);
442 static void rack_dtor(void *mem, int32_t size, void *arg);
443 static void
444 rack_log_alt_to_to_cancel(struct tcp_rack *rack,
445     uint32_t flex1, uint32_t flex2,
446     uint32_t flex3, uint32_t flex4,
447     uint32_t flex5, uint32_t flex6,
448     uint16_t flex7, uint8_t mod);
449 
450 static void
451 rack_log_pacing_delay_calc(struct tcp_rack *rack, uint32_t len, uint32_t slot,
452    uint64_t bw_est, uint64_t bw, uint64_t len_time, int method, int line,
453    struct rack_sendmap *rsm, uint8_t quality);
454 static struct rack_sendmap *
455 rack_find_high_nonack(struct tcp_rack *rack,
456     struct rack_sendmap *rsm);
457 static struct rack_sendmap *rack_find_lowest_rsm(struct tcp_rack *rack);
458 static void rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm);
459 static void rack_fini(struct tcpcb *tp, int32_t tcb_is_purged);
460 static int rack_get_sockopt(struct inpcb *inp, struct sockopt *sopt);
461 static void
462 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
463 			    tcp_seq th_ack, int line, uint8_t quality);
464 static uint32_t
465 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss);
466 static int32_t rack_handoff_ok(struct tcpcb *tp);
467 static int32_t rack_init(struct tcpcb *tp);
468 static void rack_init_sysctls(void);
469 static void
470 rack_log_ack(struct tcpcb *tp, struct tcpopt *to,
471     struct tcphdr *th, int entered_rec, int dup_ack_struck);
472 static void
473 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
474     uint32_t seq_out, uint16_t th_flags, int32_t err, uint64_t ts,
475     struct rack_sendmap *hintrsm, uint16_t add_flags, struct mbuf *s_mb, uint32_t s_moff, int hw_tls);
476 
477 static void
478 rack_log_sack_passed(struct tcpcb *tp, struct tcp_rack *rack,
479     struct rack_sendmap *rsm);
480 static void rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm);
481 static int32_t rack_output(struct tcpcb *tp);
482 
483 static uint32_t
484 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack,
485     struct sackblk *sack, struct tcpopt *to, struct rack_sendmap **prsm,
486     uint32_t cts, int *moved_two);
487 static void rack_post_recovery(struct tcpcb *tp, uint32_t th_seq);
488 static void rack_remxt_tmr(struct tcpcb *tp);
489 static int rack_set_sockopt(struct inpcb *inp, struct sockopt *sopt);
490 static void rack_set_state(struct tcpcb *tp, struct tcp_rack *rack);
491 static int32_t rack_stopall(struct tcpcb *tp);
492 static void
493 rack_timer_activate(struct tcpcb *tp, uint32_t timer_type,
494     uint32_t delta);
495 static int32_t rack_timer_active(struct tcpcb *tp, uint32_t timer_type);
496 static void rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line);
497 static void rack_timer_stop(struct tcpcb *tp, uint32_t timer_type);
498 static uint32_t
499 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack,
500     struct rack_sendmap *rsm, uint64_t ts, int32_t * lenp, uint16_t add_flag);
501 static void
502 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
503     struct rack_sendmap *rsm, uint64_t ts, uint16_t add_flag);
504 static int
505 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack,
506     struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack);
507 static int32_t tcp_addrack(module_t mod, int32_t type, void *data);
508 static int
509 rack_do_close_wait(struct mbuf *m, struct tcphdr *th,
510     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
511     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
512 static int
513 rack_do_closing(struct mbuf *m, struct tcphdr *th,
514     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
515     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
516 static int
517 rack_do_established(struct mbuf *m, struct tcphdr *th,
518     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
519     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
520 static int
521 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th,
522     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
523     int32_t tlen, uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos);
524 static int
525 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th,
526     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
527     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
528 static int
529 rack_do_fin_wait_2(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_lastack(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_syn_recv(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_syn_sent(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 thflags, int32_t nxt_pkt, uint8_t iptos);
544 struct rack_sendmap *
545 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack,
546     uint32_t tsused);
547 static void tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt,
548     uint32_t len, uint32_t us_tim, int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt);
549 static void
550      tcp_rack_partialack(struct tcpcb *tp);
551 static int
552 rack_set_profile(struct tcp_rack *rack, int prof);
553 static void
554 rack_apply_deferred_options(struct tcp_rack *rack);
555 
556 int32_t rack_clear_counter=0;
557 
558 static inline void
559 rack_trace_point(struct tcp_rack *rack, int num)
560 {
561 	if (((rack_trace_point_config == num)  ||
562 	     (rack_trace_point_config = 0xffffffff)) &&
563 	    (rack_trace_point_bb_mode != 0) &&
564 	    (rack_trace_point_count > 0) &&
565 	    (rack->rc_tp->t_logstate == 0)) {
566 		int res;
567 		res = atomic_fetchadd_int(&rack_trace_point_count, -1);
568 		if (res > 0) {
569 			rack->rc_tp->t_logstate = rack_trace_point_bb_mode;
570 		} else {
571 			/* Loss a race assure its zero now */
572 			rack_trace_point_count = 0;
573 		}
574 	}
575 }
576 
577 static void
578 rack_set_cc_pacing(struct tcp_rack *rack)
579 {
580 	struct sockopt sopt;
581 	struct cc_newreno_opts opt;
582 	struct newreno old, *ptr;
583 	struct tcpcb *tp;
584 	int error;
585 
586 	if (rack->rc_pacing_cc_set)
587 		return;
588 
589 	tp = rack->rc_tp;
590 	if (tp->cc_algo == NULL) {
591 		/* Tcb is leaving */
592 		return;
593 	}
594 	rack->rc_pacing_cc_set = 1;
595 	if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0) {
596 		/* Not new-reno we can't play games with beta! */
597 		goto out;
598 	}
599 	ptr = ((struct newreno *)tp->ccv->cc_data);
600 	if (CC_ALGO(tp)->ctl_output == NULL)  {
601 		/* Huh, why does new_reno no longer have a set function? */
602 		goto out;
603 	}
604 	if (ptr == NULL) {
605 		/* Just the default values */
606 		old.beta = V_newreno_beta_ecn;
607 		old.beta_ecn = V_newreno_beta_ecn;
608 		old.newreno_flags = 0;
609 	} else {
610 		old.beta = ptr->beta;
611 		old.beta_ecn = ptr->beta_ecn;
612 		old.newreno_flags = ptr->newreno_flags;
613 	}
614 	sopt.sopt_valsize = sizeof(struct cc_newreno_opts);
615 	sopt.sopt_dir = SOPT_SET;
616 	opt.name = CC_NEWRENO_BETA;
617 	opt.val = rack->r_ctl.rc_saved_beta.beta;
618 	error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
619 	if (error)  {
620 		goto out;
621 	}
622 	/*
623 	 * Hack alert we need to set in our newreno_flags
624 	 * so that Abe behavior is also applied.
625 	 */
626 	((struct newreno *)tp->ccv->cc_data)->newreno_flags |= CC_NEWRENO_BETA_ECN_ENABLED;
627 	opt.name = CC_NEWRENO_BETA_ECN;
628 	opt.val = rack->r_ctl.rc_saved_beta.beta_ecn;
629 	error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
630 	if (error) {
631 		goto out;
632 	}
633 	/* Save off the original values for restoral */
634 	memcpy(&rack->r_ctl.rc_saved_beta, &old, sizeof(struct newreno));
635 out:
636 	if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
637 		union tcp_log_stackspecific log;
638 		struct timeval tv;
639 
640 		ptr = ((struct newreno *)tp->ccv->cc_data);
641 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
642 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
643 		if (ptr) {
644 			log.u_bbr.flex1 = ptr->beta;
645 			log.u_bbr.flex2 = ptr->beta_ecn;
646 			log.u_bbr.flex3 = ptr->newreno_flags;
647 		}
648 		log.u_bbr.flex4 = rack->r_ctl.rc_saved_beta.beta;
649 		log.u_bbr.flex5 = rack->r_ctl.rc_saved_beta.beta_ecn;
650 		log.u_bbr.flex6 = rack->r_ctl.rc_saved_beta.newreno_flags;
651 		log.u_bbr.flex7 = rack->gp_ready;
652 		log.u_bbr.flex7 <<= 1;
653 		log.u_bbr.flex7 |= rack->use_fixed_rate;
654 		log.u_bbr.flex7 <<= 1;
655 		log.u_bbr.flex7 |= rack->rc_pacing_cc_set;
656 		log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
657 		log.u_bbr.flex8 = 3;
658 		tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, error,
659 			       0, &log, false, NULL, NULL, 0, &tv);
660 	}
661 }
662 
663 static void
664 rack_undo_cc_pacing(struct tcp_rack *rack)
665 {
666 	struct newreno old, *ptr;
667 	struct tcpcb *tp;
668 
669 	if (rack->rc_pacing_cc_set == 0)
670 		return;
671 	tp = rack->rc_tp;
672 	rack->rc_pacing_cc_set = 0;
673 	if (tp->cc_algo == NULL)
674 		/* Tcb is leaving */
675 		return;
676 	if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0) {
677 		/* Not new-reno nothing to do! */
678 		return;
679 	}
680 	ptr = ((struct newreno *)tp->ccv->cc_data);
681 	if (ptr == NULL) {
682 		/*
683 		 * This happens at rack_fini() if the
684 		 * cc module gets freed on us. In that
685 		 * case we loose our "new" settings but
686 		 * thats ok, since the tcb is going away anyway.
687 		 */
688 		return;
689 	}
690 	/* Grab out our set values */
691 	memcpy(&old, ptr, sizeof(struct newreno));
692 	/* Copy back in the original values */
693 	memcpy(ptr, &rack->r_ctl.rc_saved_beta, sizeof(struct newreno));
694 	/* Now save back the values we had set in (for when pacing is restored) */
695 	memcpy(&rack->r_ctl.rc_saved_beta, &old, sizeof(struct newreno));
696 	if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
697 		union tcp_log_stackspecific log;
698 		struct timeval tv;
699 
700 		ptr = ((struct newreno *)tp->ccv->cc_data);
701 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
702 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
703 		log.u_bbr.flex1 = ptr->beta;
704 		log.u_bbr.flex2 = ptr->beta_ecn;
705 		log.u_bbr.flex3 = ptr->newreno_flags;
706 		log.u_bbr.flex4 = rack->r_ctl.rc_saved_beta.beta;
707 		log.u_bbr.flex5 = rack->r_ctl.rc_saved_beta.beta_ecn;
708 		log.u_bbr.flex6 = rack->r_ctl.rc_saved_beta.newreno_flags;
709 		log.u_bbr.flex7 = rack->gp_ready;
710 		log.u_bbr.flex7 <<= 1;
711 		log.u_bbr.flex7 |= rack->use_fixed_rate;
712 		log.u_bbr.flex7 <<= 1;
713 		log.u_bbr.flex7 |= rack->rc_pacing_cc_set;
714 		log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
715 		log.u_bbr.flex8 = 4;
716 		tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
717 			       0, &log, false, NULL, NULL, 0, &tv);
718 	}
719 }
720 
721 #ifdef NETFLIX_PEAKRATE
722 static inline void
723 rack_update_peakrate_thr(struct tcpcb *tp)
724 {
725 	/* Keep in mind that t_maxpeakrate is in B/s. */
726 	uint64_t peak;
727 	peak = uqmax((tp->t_maxseg * 2),
728 		     (((uint64_t)tp->t_maxpeakrate * (uint64_t)(tp->t_srtt)) / (uint64_t)HPTS_USEC_IN_SEC));
729 	tp->t_peakrate_thr = (uint32_t)uqmin(peak, UINT32_MAX);
730 }
731 #endif
732 
733 static int
734 sysctl_rack_clear(SYSCTL_HANDLER_ARGS)
735 {
736 	uint32_t stat;
737 	int32_t error;
738 
739 	error = SYSCTL_OUT(req, &rack_clear_counter, sizeof(uint32_t));
740 	if (error || req->newptr == NULL)
741 		return error;
742 
743 	error = SYSCTL_IN(req, &stat, sizeof(uint32_t));
744 	if (error)
745 		return (error);
746 	if (stat == 1) {
747 #ifdef INVARIANTS
748 		printf("Clearing RACK counters\n");
749 #endif
750 		counter_u64_zero(rack_tlp_tot);
751 		counter_u64_zero(rack_tlp_newdata);
752 		counter_u64_zero(rack_tlp_retran);
753 		counter_u64_zero(rack_tlp_retran_bytes);
754 		counter_u64_zero(rack_to_tot);
755 		counter_u64_zero(rack_saw_enobuf);
756 		counter_u64_zero(rack_saw_enobuf_hw);
757 		counter_u64_zero(rack_saw_enetunreach);
758 		counter_u64_zero(rack_persists_sends);
759 		counter_u64_zero(rack_persists_acks);
760 		counter_u64_zero(rack_persists_loss);
761 		counter_u64_zero(rack_persists_lost_ends);
762 #ifdef INVARIANTS
763 		counter_u64_zero(rack_adjust_map_bw);
764 #endif
765 		counter_u64_zero(rack_to_alloc_hard);
766 		counter_u64_zero(rack_to_alloc_emerg);
767 		counter_u64_zero(rack_sack_proc_all);
768 		counter_u64_zero(rack_fto_send);
769 		counter_u64_zero(rack_fto_rsm_send);
770 		counter_u64_zero(rack_extended_rfo);
771 		counter_u64_zero(rack_hw_pace_init_fail);
772 		counter_u64_zero(rack_hw_pace_lost);
773 		counter_u64_zero(rack_non_fto_send);
774 		counter_u64_zero(rack_nfto_resend);
775 		counter_u64_zero(rack_sack_proc_short);
776 		counter_u64_zero(rack_sack_proc_restart);
777 		counter_u64_zero(rack_to_alloc);
778 		counter_u64_zero(rack_to_alloc_limited);
779 		counter_u64_zero(rack_alloc_limited_conns);
780 		counter_u64_zero(rack_split_limited);
781 		counter_u64_zero(rack_multi_single_eq);
782 		counter_u64_zero(rack_proc_non_comp_ack);
783 		counter_u64_zero(rack_sack_attacks_detected);
784 		counter_u64_zero(rack_sack_attacks_reversed);
785 		counter_u64_zero(rack_sack_used_next_merge);
786 		counter_u64_zero(rack_sack_used_prev_merge);
787 		counter_u64_zero(rack_sack_splits);
788 		counter_u64_zero(rack_sack_skipped_acked);
789 		counter_u64_zero(rack_ack_total);
790 		counter_u64_zero(rack_express_sack);
791 		counter_u64_zero(rack_sack_total);
792 		counter_u64_zero(rack_move_none);
793 		counter_u64_zero(rack_move_some);
794 		counter_u64_zero(rack_try_scwnd);
795 		counter_u64_zero(rack_collapsed_win);
796 		counter_u64_zero(rack_collapsed_win_rxt);
797 		counter_u64_zero(rack_collapsed_win_seen);
798 		counter_u64_zero(rack_collapsed_win_rxt_bytes);
799 	}
800 	rack_clear_counter = 0;
801 	return (0);
802 }
803 
804 static void
805 rack_init_sysctls(void)
806 {
807 	struct sysctl_oid *rack_counters;
808 	struct sysctl_oid *rack_attack;
809 	struct sysctl_oid *rack_pacing;
810 	struct sysctl_oid *rack_timely;
811 	struct sysctl_oid *rack_timers;
812 	struct sysctl_oid *rack_tlp;
813 	struct sysctl_oid *rack_misc;
814 	struct sysctl_oid *rack_features;
815 	struct sysctl_oid *rack_measure;
816 	struct sysctl_oid *rack_probertt;
817 	struct sysctl_oid *rack_hw_pacing;
818 	struct sysctl_oid *rack_tracepoint;
819 
820 	rack_attack = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
821 	    SYSCTL_CHILDREN(rack_sysctl_root),
822 	    OID_AUTO,
823 	    "sack_attack",
824 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
825 	    "Rack Sack Attack Counters and Controls");
826 	rack_counters = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
827 	    SYSCTL_CHILDREN(rack_sysctl_root),
828 	    OID_AUTO,
829 	    "stats",
830 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
831 	    "Rack Counters");
832 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
833 	    SYSCTL_CHILDREN(rack_sysctl_root),
834 	    OID_AUTO, "rate_sample_method", CTLFLAG_RW,
835 	    &rack_rate_sample_method , USE_RTT_LOW,
836 	    "What method should we use for rate sampling 0=high, 1=low ");
837 	/* Probe rtt related controls */
838 	rack_probertt = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
839 	    SYSCTL_CHILDREN(rack_sysctl_root),
840 	    OID_AUTO,
841 	    "probertt",
842 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
843 	    "ProbeRTT related Controls");
844 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
845 	    SYSCTL_CHILDREN(rack_probertt),
846 	    OID_AUTO, "exit_per_hpb", CTLFLAG_RW,
847 	    &rack_atexit_prtt_hbp, 130,
848 	    "What percentage above goodput do we clamp CA/SS to at exit on high-BDP path 110%");
849 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
850 	    SYSCTL_CHILDREN(rack_probertt),
851 	    OID_AUTO, "exit_per_nonhpb", CTLFLAG_RW,
852 	    &rack_atexit_prtt, 130,
853 	    "What percentage above goodput do we clamp CA/SS to at exit on a non high-BDP path 100%");
854 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
855 	    SYSCTL_CHILDREN(rack_probertt),
856 	    OID_AUTO, "gp_per_mul", CTLFLAG_RW,
857 	    &rack_per_of_gp_probertt, 60,
858 	    "What percentage of goodput do we pace at in probertt");
859 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
860 	    SYSCTL_CHILDREN(rack_probertt),
861 	    OID_AUTO, "gp_per_reduce", CTLFLAG_RW,
862 	    &rack_per_of_gp_probertt_reduce, 10,
863 	    "What percentage of goodput do we reduce every gp_srtt");
864 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
865 	    SYSCTL_CHILDREN(rack_probertt),
866 	    OID_AUTO, "gp_per_low", CTLFLAG_RW,
867 	    &rack_per_of_gp_lowthresh, 40,
868 	    "What percentage of goodput do we allow the multiplier to fall to");
869 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
870 	    SYSCTL_CHILDREN(rack_probertt),
871 	    OID_AUTO, "time_between", CTLFLAG_RW,
872 	    & rack_time_between_probertt, 96000000,
873 	    "How many useconds between the lowest rtt falling must past before we enter probertt");
874 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
875 	    SYSCTL_CHILDREN(rack_probertt),
876 	    OID_AUTO, "safety", CTLFLAG_RW,
877 	    &rack_probe_rtt_safety_val, 2000000,
878 	    "If not zero, provides a maximum usecond that you can stay in probertt (2sec = 2000000)");
879 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
880 	    SYSCTL_CHILDREN(rack_probertt),
881 	    OID_AUTO, "sets_cwnd", CTLFLAG_RW,
882 	    &rack_probe_rtt_sets_cwnd, 0,
883 	    "Do we set the cwnd too (if always_lower is on)");
884 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
885 	    SYSCTL_CHILDREN(rack_probertt),
886 	    OID_AUTO, "maxdrainsrtts", CTLFLAG_RW,
887 	    &rack_max_drain_wait, 2,
888 	    "Maximum number of gp_srtt's to hold in drain waiting for flight to reach goal");
889 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
890 	    SYSCTL_CHILDREN(rack_probertt),
891 	    OID_AUTO, "mustdrainsrtts", CTLFLAG_RW,
892 	    &rack_must_drain, 1,
893 	    "We must drain this many gp_srtt's waiting for flight to reach goal");
894 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
895 	    SYSCTL_CHILDREN(rack_probertt),
896 	    OID_AUTO, "goal_use_min_entry", CTLFLAG_RW,
897 	    &rack_probertt_use_min_rtt_entry, 1,
898 	    "Should we use the min-rtt to calculate the goal rtt (else gp_srtt) at entry");
899 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
900 	    SYSCTL_CHILDREN(rack_probertt),
901 	    OID_AUTO, "goal_use_min_exit", CTLFLAG_RW,
902 	    &rack_probertt_use_min_rtt_exit, 0,
903 	    "How to set cwnd at exit, 0 - dynamic, 1 - use min-rtt, 2 - use curgprtt, 3 - entry gp-rtt");
904 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
905 	    SYSCTL_CHILDREN(rack_probertt),
906 	    OID_AUTO, "length_div", CTLFLAG_RW,
907 	    &rack_probertt_gpsrtt_cnt_div, 0,
908 	    "How many recent goodput srtt periods plus hold tim does probertt last (bottom of fraction)");
909 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
910 	    SYSCTL_CHILDREN(rack_probertt),
911 	    OID_AUTO, "length_mul", CTLFLAG_RW,
912 	    &rack_probertt_gpsrtt_cnt_mul, 0,
913 	    "How many recent goodput srtt periods plus hold tim does probertt last (top of fraction)");
914 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
915 	    SYSCTL_CHILDREN(rack_probertt),
916 	    OID_AUTO, "holdtim_at_target", CTLFLAG_RW,
917 	    &rack_min_probertt_hold, 200000,
918 	    "What is the minimum time we hold probertt at target");
919 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
920 	    SYSCTL_CHILDREN(rack_probertt),
921 	    OID_AUTO, "filter_life", CTLFLAG_RW,
922 	    &rack_probertt_filter_life, 10000000,
923 	    "What is the time for the filters life in useconds");
924 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
925 	    SYSCTL_CHILDREN(rack_probertt),
926 	    OID_AUTO, "lower_within", CTLFLAG_RW,
927 	    &rack_probertt_lower_within, 10,
928 	    "If the rtt goes lower within this percentage of the time, go into probe-rtt");
929 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
930 	    SYSCTL_CHILDREN(rack_probertt),
931 	    OID_AUTO, "must_move", CTLFLAG_RW,
932 	    &rack_min_rtt_movement, 250,
933 	    "How much is the minimum movement in rtt to count as a drop for probertt purposes");
934 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
935 	    SYSCTL_CHILDREN(rack_probertt),
936 	    OID_AUTO, "clear_is_cnts", CTLFLAG_RW,
937 	    &rack_probertt_clear_is, 1,
938 	    "Do we clear I/S counts on exiting probe-rtt");
939 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
940 	    SYSCTL_CHILDREN(rack_probertt),
941 	    OID_AUTO, "hbp_extra_drain", CTLFLAG_RW,
942 	    &rack_max_drain_hbp, 1,
943 	    "How many extra drain gpsrtt's do we get in highly buffered paths");
944 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
945 	    SYSCTL_CHILDREN(rack_probertt),
946 	    OID_AUTO, "hbp_threshold", CTLFLAG_RW,
947 	    &rack_hbp_thresh, 3,
948 	    "We are highly buffered if min_rtt_seen / max_rtt_seen > this-threshold");
949 
950 	rack_tracepoint = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
951 	    SYSCTL_CHILDREN(rack_sysctl_root),
952 	    OID_AUTO,
953 	    "tp",
954 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
955 	    "Rack tracepoint facility");
956 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
957 	    SYSCTL_CHILDREN(rack_tracepoint),
958 	    OID_AUTO, "number", CTLFLAG_RW,
959 	    &rack_trace_point_config, 0,
960 	    "What is the trace point number to activate (0=none, 0xffffffff = all)?");
961 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
962 	    SYSCTL_CHILDREN(rack_tracepoint),
963 	    OID_AUTO, "bbmode", CTLFLAG_RW,
964 	    &rack_trace_point_bb_mode, 4,
965 	    "What is BB logging mode that is activated?");
966 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
967 	    SYSCTL_CHILDREN(rack_tracepoint),
968 	    OID_AUTO, "count", CTLFLAG_RW,
969 	    &rack_trace_point_count, 0,
970 	    "How many connections will have BB logging turned on that hit the tracepoint?");
971 	/* Pacing related sysctls */
972 	rack_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
973 	    SYSCTL_CHILDREN(rack_sysctl_root),
974 	    OID_AUTO,
975 	    "pacing",
976 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
977 	    "Pacing related Controls");
978 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
979 	    SYSCTL_CHILDREN(rack_pacing),
980 	    OID_AUTO, "max_pace_over", CTLFLAG_RW,
981 	    &rack_max_per_above, 30,
982 	    "What is the maximum allowable percentage that we can pace above (so 30 = 130% of our goal)");
983 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
984 	    SYSCTL_CHILDREN(rack_pacing),
985 	    OID_AUTO, "pace_to_one", CTLFLAG_RW,
986 	    &rack_pace_one_seg, 0,
987 	    "Do we allow low b/w pacing of 1MSS instead of two");
988 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
989 	    SYSCTL_CHILDREN(rack_pacing),
990 	    OID_AUTO, "limit_wsrtt", CTLFLAG_RW,
991 	    &rack_limit_time_with_srtt, 0,
992 	    "Do we limit pacing time based on srtt");
993 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
994 	    SYSCTL_CHILDREN(rack_pacing),
995 	    OID_AUTO, "init_win", CTLFLAG_RW,
996 	    &rack_default_init_window, 0,
997 	    "Do we have a rack initial window 0 = system default");
998 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
999 	    SYSCTL_CHILDREN(rack_pacing),
1000 	    OID_AUTO, "gp_per_ss", CTLFLAG_RW,
1001 	    &rack_per_of_gp_ss, 250,
1002 	    "If non zero, what percentage of goodput to pace at in slow start");
1003 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1004 	    SYSCTL_CHILDREN(rack_pacing),
1005 	    OID_AUTO, "gp_per_ca", CTLFLAG_RW,
1006 	    &rack_per_of_gp_ca, 150,
1007 	    "If non zero, what percentage of goodput to pace at in congestion avoidance");
1008 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1009 	    SYSCTL_CHILDREN(rack_pacing),
1010 	    OID_AUTO, "gp_per_rec", CTLFLAG_RW,
1011 	    &rack_per_of_gp_rec, 200,
1012 	    "If non zero, what percentage of goodput to pace at in recovery");
1013 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1014 	    SYSCTL_CHILDREN(rack_pacing),
1015 	    OID_AUTO, "pace_max_seg", CTLFLAG_RW,
1016 	    &rack_hptsi_segments, 40,
1017 	    "What size is the max for TSO segments in pacing and burst mitigation");
1018 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1019 	    SYSCTL_CHILDREN(rack_pacing),
1020 	    OID_AUTO, "burst_reduces", CTLFLAG_RW,
1021 	    &rack_slot_reduction, 4,
1022 	    "When doing only burst mitigation what is the reduce divisor");
1023 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1024 	    SYSCTL_CHILDREN(rack_sysctl_root),
1025 	    OID_AUTO, "use_pacing", CTLFLAG_RW,
1026 	    &rack_pace_every_seg, 0,
1027 	    "If set we use pacing, if clear we use only the original burst mitigation");
1028 	SYSCTL_ADD_U64(&rack_sysctl_ctx,
1029 	    SYSCTL_CHILDREN(rack_pacing),
1030 	    OID_AUTO, "rate_cap", CTLFLAG_RW,
1031 	    &rack_bw_rate_cap, 0,
1032 	    "If set we apply this value to the absolute rate cap used by pacing");
1033 	SYSCTL_ADD_U8(&rack_sysctl_ctx,
1034 	    SYSCTL_CHILDREN(rack_sysctl_root),
1035 	    OID_AUTO, "req_measure_cnt", CTLFLAG_RW,
1036 	    &rack_req_measurements, 1,
1037 	    "If doing dynamic pacing, how many measurements must be in before we start pacing?");
1038 	/* Hardware pacing */
1039 	rack_hw_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1040 	    SYSCTL_CHILDREN(rack_sysctl_root),
1041 	    OID_AUTO,
1042 	    "hdwr_pacing",
1043 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1044 	    "Pacing related Controls");
1045 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1046 	    SYSCTL_CHILDREN(rack_hw_pacing),
1047 	    OID_AUTO, "rwnd_factor", CTLFLAG_RW,
1048 	    &rack_hw_rwnd_factor, 2,
1049 	    "How many times does snd_wnd need to be bigger than pace_max_seg so we will hold off and get more acks?");
1050 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1051 	    SYSCTL_CHILDREN(rack_hw_pacing),
1052 	    OID_AUTO, "pace_enobuf_mult", CTLFLAG_RW,
1053 	    &rack_enobuf_hw_boost_mult, 2,
1054 	    "By how many time_betweens should we boost the pacing time if we see a ENOBUFS?");
1055 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1056 	    SYSCTL_CHILDREN(rack_hw_pacing),
1057 	    OID_AUTO, "pace_enobuf_max", CTLFLAG_RW,
1058 	    &rack_enobuf_hw_max, 2,
1059 	    "What is the max 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_min", CTLFLAG_RW,
1063 	    &rack_enobuf_hw_min, 2,
1064 	    "What is the min 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, "enable", CTLFLAG_RW,
1068 	    &rack_enable_hw_pacing, 0,
1069 	    "Should RACK attempt to use hw pacing?");
1070 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1071 	    SYSCTL_CHILDREN(rack_hw_pacing),
1072 	    OID_AUTO, "rate_cap", CTLFLAG_RW,
1073 	    &rack_hw_rate_caps, 1,
1074 	    "Does the highest hardware pacing rate cap the rate we will send at??");
1075 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1076 	    SYSCTL_CHILDREN(rack_hw_pacing),
1077 	    OID_AUTO, "rate_min", CTLFLAG_RW,
1078 	    &rack_hw_rate_min, 0,
1079 	    "Do we need a minimum estimate of this many bytes per second in order to engage hw pacing?");
1080 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1081 	    SYSCTL_CHILDREN(rack_hw_pacing),
1082 	    OID_AUTO, "rate_to_low", CTLFLAG_RW,
1083 	    &rack_hw_rate_to_low, 0,
1084 	    "If we fall below this rate, dis-engage hw pacing?");
1085 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1086 	    SYSCTL_CHILDREN(rack_hw_pacing),
1087 	    OID_AUTO, "up_only", CTLFLAG_RW,
1088 	    &rack_hw_up_only, 1,
1089 	    "Do we allow hw pacing to lower the rate selected?");
1090 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1091 	    SYSCTL_CHILDREN(rack_hw_pacing),
1092 	    OID_AUTO, "extra_mss_precise", CTLFLAG_RW,
1093 	    &rack_hw_pace_extra_slots, 2,
1094 	    "If the rates between software and hardware match precisely how many extra time_betweens do we get?");
1095 	rack_timely = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1096 	    SYSCTL_CHILDREN(rack_sysctl_root),
1097 	    OID_AUTO,
1098 	    "timely",
1099 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1100 	    "Rack Timely RTT Controls");
1101 	/* Timely based GP dynmics */
1102 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1103 	    SYSCTL_CHILDREN(rack_timely),
1104 	    OID_AUTO, "upper", CTLFLAG_RW,
1105 	    &rack_gp_per_bw_mul_up, 2,
1106 	    "Rack timely upper range for equal b/w (in percentage)");
1107 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1108 	    SYSCTL_CHILDREN(rack_timely),
1109 	    OID_AUTO, "lower", CTLFLAG_RW,
1110 	    &rack_gp_per_bw_mul_down, 4,
1111 	    "Rack timely lower range for equal b/w (in percentage)");
1112 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1113 	    SYSCTL_CHILDREN(rack_timely),
1114 	    OID_AUTO, "rtt_max_mul", CTLFLAG_RW,
1115 	    &rack_gp_rtt_maxmul, 3,
1116 	    "Rack timely multiplier of lowest rtt for rtt_max");
1117 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1118 	    SYSCTL_CHILDREN(rack_timely),
1119 	    OID_AUTO, "rtt_min_div", CTLFLAG_RW,
1120 	    &rack_gp_rtt_mindiv, 4,
1121 	    "Rack timely divisor used for rtt + (rtt * mul/divisor) for check for lower rtt");
1122 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1123 	    SYSCTL_CHILDREN(rack_timely),
1124 	    OID_AUTO, "rtt_min_mul", CTLFLAG_RW,
1125 	    &rack_gp_rtt_minmul, 1,
1126 	    "Rack timely multiplier 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, "decrease", CTLFLAG_RW,
1130 	    &rack_gp_decrease_per, 20,
1131 	    "Rack timely decrease percentage of our GP multiplication factor");
1132 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1133 	    SYSCTL_CHILDREN(rack_timely),
1134 	    OID_AUTO, "increase", CTLFLAG_RW,
1135 	    &rack_gp_increase_per, 2,
1136 	    "Rack timely increase perentage of our GP multiplication factor");
1137 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1138 	    SYSCTL_CHILDREN(rack_timely),
1139 	    OID_AUTO, "lowerbound", CTLFLAG_RW,
1140 	    &rack_per_lower_bound, 50,
1141 	    "Rack timely lowest percentage we allow GP multiplier to fall to");
1142 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1143 	    SYSCTL_CHILDREN(rack_timely),
1144 	    OID_AUTO, "upperboundss", CTLFLAG_RW,
1145 	    &rack_per_upper_bound_ss, 0,
1146 	    "Rack timely highest percentage we allow GP multiplier in SS to raise to (0 is no upperbound)");
1147 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1148 	    SYSCTL_CHILDREN(rack_timely),
1149 	    OID_AUTO, "upperboundca", CTLFLAG_RW,
1150 	    &rack_per_upper_bound_ca, 0,
1151 	    "Rack timely highest percentage we allow GP multiplier to CA raise to (0 is no upperbound)");
1152 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1153 	    SYSCTL_CHILDREN(rack_timely),
1154 	    OID_AUTO, "dynamicgp", CTLFLAG_RW,
1155 	    &rack_do_dyn_mul, 0,
1156 	    "Rack timely do we enable dynmaic timely goodput by default");
1157 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1158 	    SYSCTL_CHILDREN(rack_timely),
1159 	    OID_AUTO, "no_rec_red", CTLFLAG_RW,
1160 	    &rack_gp_no_rec_chg, 1,
1161 	    "Rack timely do we prohibit the recovery multiplier from being lowered");
1162 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1163 	    SYSCTL_CHILDREN(rack_timely),
1164 	    OID_AUTO, "red_clear_cnt", CTLFLAG_RW,
1165 	    &rack_timely_dec_clear, 6,
1166 	    "Rack timely what threshold do we count to before another boost during b/w decent");
1167 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1168 	    SYSCTL_CHILDREN(rack_timely),
1169 	    OID_AUTO, "max_push_rise", CTLFLAG_RW,
1170 	    &rack_timely_max_push_rise, 3,
1171 	    "Rack timely how many times do we push up with b/w increase");
1172 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1173 	    SYSCTL_CHILDREN(rack_timely),
1174 	    OID_AUTO, "max_push_drop", CTLFLAG_RW,
1175 	    &rack_timely_max_push_drop, 3,
1176 	    "Rack timely how many times do we push back on b/w decent");
1177 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1178 	    SYSCTL_CHILDREN(rack_timely),
1179 	    OID_AUTO, "min_segs", CTLFLAG_RW,
1180 	    &rack_timely_min_segs, 4,
1181 	    "Rack timely when setting the cwnd what is the min num segments");
1182 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1183 	    SYSCTL_CHILDREN(rack_timely),
1184 	    OID_AUTO, "noback_max", CTLFLAG_RW,
1185 	    &rack_use_max_for_nobackoff, 0,
1186 	    "Rack timely when deciding if to backoff on a loss, do we use under max rtt else min");
1187 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1188 	    SYSCTL_CHILDREN(rack_timely),
1189 	    OID_AUTO, "interim_timely_only", CTLFLAG_RW,
1190 	    &rack_timely_int_timely_only, 0,
1191 	    "Rack timely when doing interim timely's do we only do timely (no b/w consideration)");
1192 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1193 	    SYSCTL_CHILDREN(rack_timely),
1194 	    OID_AUTO, "nonstop", CTLFLAG_RW,
1195 	    &rack_timely_no_stopping, 0,
1196 	    "Rack timely don't stop increase");
1197 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1198 	    SYSCTL_CHILDREN(rack_timely),
1199 	    OID_AUTO, "dec_raise_thresh", CTLFLAG_RW,
1200 	    &rack_down_raise_thresh, 100,
1201 	    "If the CA or SS is below this threshold raise on the first 3 b/w lowers (0=always)");
1202 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1203 	    SYSCTL_CHILDREN(rack_timely),
1204 	    OID_AUTO, "bottom_drag_segs", CTLFLAG_RW,
1205 	    &rack_req_segs, 1,
1206 	    "Bottom dragging if not these many segments outstanding and room");
1207 
1208 	/* TLP and Rack related parameters */
1209 	rack_tlp = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1210 	    SYSCTL_CHILDREN(rack_sysctl_root),
1211 	    OID_AUTO,
1212 	    "tlp",
1213 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1214 	    "TLP and Rack related Controls");
1215 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1216 	    SYSCTL_CHILDREN(rack_tlp),
1217 	    OID_AUTO, "use_rrr", CTLFLAG_RW,
1218 	    &use_rack_rr, 1,
1219 	    "Do we use Rack Rapid Recovery");
1220 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1221 	    SYSCTL_CHILDREN(rack_tlp),
1222 	    OID_AUTO, "post_rec_labc", CTLFLAG_RW,
1223 	    &rack_max_abc_post_recovery, 2,
1224 	    "Since we do early recovery, do we override the l_abc to a value, if so what?");
1225 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1226 	    SYSCTL_CHILDREN(rack_tlp),
1227 	    OID_AUTO, "nonrxt_use_cr", CTLFLAG_RW,
1228 	    &rack_non_rxt_use_cr, 0,
1229 	    "Do we use ss/ca rate if in recovery we are transmitting a new data chunk");
1230 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1231 	    SYSCTL_CHILDREN(rack_tlp),
1232 	    OID_AUTO, "tlpmethod", CTLFLAG_RW,
1233 	    &rack_tlp_threshold_use, TLP_USE_TWO_ONE,
1234 	    "What method do we do for TLP time calc 0=no-de-ack-comp, 1=ID, 2=2.1, 3=2.2");
1235 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1236 	    SYSCTL_CHILDREN(rack_tlp),
1237 	    OID_AUTO, "limit", CTLFLAG_RW,
1238 	    &rack_tlp_limit, 2,
1239 	    "How many TLP's can be sent without sending new data");
1240 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1241 	    SYSCTL_CHILDREN(rack_tlp),
1242 	    OID_AUTO, "use_greater", CTLFLAG_RW,
1243 	    &rack_tlp_use_greater, 1,
1244 	    "Should we use the rack_rtt time if its greater than srtt");
1245 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1246 	    SYSCTL_CHILDREN(rack_tlp),
1247 	    OID_AUTO, "tlpminto", CTLFLAG_RW,
1248 	    &rack_tlp_min, 10000,
1249 	    "TLP minimum timeout per the specification (in microseconds)");
1250 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1251 	    SYSCTL_CHILDREN(rack_tlp),
1252 	    OID_AUTO, "send_oldest", CTLFLAG_RW,
1253 	    &rack_always_send_oldest, 0,
1254 	    "Should we always send the oldest TLP and RACK-TLP");
1255 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1256 	    SYSCTL_CHILDREN(rack_tlp),
1257 	    OID_AUTO, "rack_tlimit", CTLFLAG_RW,
1258 	    &rack_limited_retran, 0,
1259 	    "How many times can a rack timeout drive out sends");
1260 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1261 	    SYSCTL_CHILDREN(rack_tlp),
1262 	    OID_AUTO, "tlp_cwnd_flag", CTLFLAG_RW,
1263 	    &rack_lower_cwnd_at_tlp, 0,
1264 	    "When a TLP completes a retran should we enter recovery");
1265 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1266 	    SYSCTL_CHILDREN(rack_tlp),
1267 	    OID_AUTO, "reorder_thresh", CTLFLAG_RW,
1268 	    &rack_reorder_thresh, 2,
1269 	    "What factor for rack will be added when seeing reordering (shift right)");
1270 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1271 	    SYSCTL_CHILDREN(rack_tlp),
1272 	    OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW,
1273 	    &rack_tlp_thresh, 1,
1274 	    "What divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)");
1275 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1276 	    SYSCTL_CHILDREN(rack_tlp),
1277 	    OID_AUTO, "reorder_fade", CTLFLAG_RW,
1278 	    &rack_reorder_fade, 60000000,
1279 	    "Does reorder detection fade, if so how many microseconds (0 means never)");
1280 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1281 	    SYSCTL_CHILDREN(rack_tlp),
1282 	    OID_AUTO, "pktdelay", CTLFLAG_RW,
1283 	    &rack_pkt_delay, 1000,
1284 	    "Extra RACK time (in microseconds) besides reordering thresh");
1285 
1286 	/* Timer related controls */
1287 	rack_timers = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1288 	    SYSCTL_CHILDREN(rack_sysctl_root),
1289 	    OID_AUTO,
1290 	    "timers",
1291 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1292 	    "Timer related controls");
1293 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1294 	    SYSCTL_CHILDREN(rack_timers),
1295 	    OID_AUTO, "persmin", CTLFLAG_RW,
1296 	    &rack_persist_min, 250000,
1297 	    "What is the minimum time in microseconds between persists");
1298 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1299 	    SYSCTL_CHILDREN(rack_timers),
1300 	    OID_AUTO, "persmax", CTLFLAG_RW,
1301 	    &rack_persist_max, 2000000,
1302 	    "What is the largest delay in microseconds between persists");
1303 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1304 	    SYSCTL_CHILDREN(rack_timers),
1305 	    OID_AUTO, "delayed_ack", CTLFLAG_RW,
1306 	    &rack_delayed_ack_time, 40000,
1307 	    "Delayed ack time (40ms in microseconds)");
1308 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1309 	    SYSCTL_CHILDREN(rack_timers),
1310 	    OID_AUTO, "minrto", CTLFLAG_RW,
1311 	    &rack_rto_min, 30000,
1312 	    "Minimum RTO in microseconds -- set with caution below 1000 due to TLP");
1313 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1314 	    SYSCTL_CHILDREN(rack_timers),
1315 	    OID_AUTO, "maxrto", CTLFLAG_RW,
1316 	    &rack_rto_max, 4000000,
1317 	    "Maximum RTO in microseconds -- should be at least as large as min_rto");
1318 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1319 	    SYSCTL_CHILDREN(rack_timers),
1320 	    OID_AUTO, "minto", CTLFLAG_RW,
1321 	    &rack_min_to, 1000,
1322 	    "Minimum rack timeout in microseconds");
1323 	/* Measure controls */
1324 	rack_measure = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1325 	    SYSCTL_CHILDREN(rack_sysctl_root),
1326 	    OID_AUTO,
1327 	    "measure",
1328 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1329 	    "Measure related controls");
1330 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1331 	    SYSCTL_CHILDREN(rack_measure),
1332 	    OID_AUTO, "wma_divisor", CTLFLAG_RW,
1333 	    &rack_wma_divisor, 8,
1334 	    "When doing b/w calculation what is the  divisor for the WMA");
1335 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1336 	    SYSCTL_CHILDREN(rack_measure),
1337 	    OID_AUTO, "end_cwnd", CTLFLAG_RW,
1338 	    &rack_cwnd_block_ends_measure, 0,
1339 	    "Does a cwnd just-return end the measurement window (app limited)");
1340 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1341 	    SYSCTL_CHILDREN(rack_measure),
1342 	    OID_AUTO, "end_rwnd", CTLFLAG_RW,
1343 	    &rack_rwnd_block_ends_measure, 0,
1344 	    "Does an rwnd just-return end the measurement window (app limited -- not persists)");
1345 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1346 	    SYSCTL_CHILDREN(rack_measure),
1347 	    OID_AUTO, "min_target", CTLFLAG_RW,
1348 	    &rack_def_data_window, 20,
1349 	    "What is the minimum target window (in mss) for a GP measurements");
1350 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1351 	    SYSCTL_CHILDREN(rack_measure),
1352 	    OID_AUTO, "goal_bdp", CTLFLAG_RW,
1353 	    &rack_goal_bdp, 2,
1354 	    "What is the goal BDP to measure");
1355 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1356 	    SYSCTL_CHILDREN(rack_measure),
1357 	    OID_AUTO, "min_srtts", CTLFLAG_RW,
1358 	    &rack_min_srtts, 1,
1359 	    "What is the goal BDP to measure");
1360 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1361 	    SYSCTL_CHILDREN(rack_measure),
1362 	    OID_AUTO, "min_measure_tim", CTLFLAG_RW,
1363 	    &rack_min_measure_usec, 0,
1364 	    "What is the Minimum time time for a measurement if 0, this is off");
1365 	/* Features */
1366 	rack_features = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1367 	    SYSCTL_CHILDREN(rack_sysctl_root),
1368 	    OID_AUTO,
1369 	    "features",
1370 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1371 	    "Feature controls");
1372 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1373 	    SYSCTL_CHILDREN(rack_features),
1374 	    OID_AUTO, "cmpack", CTLFLAG_RW,
1375 	    &rack_use_cmp_acks, 1,
1376 	    "Should RACK have LRO send compressed acks");
1377 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1378 	    SYSCTL_CHILDREN(rack_features),
1379 	    OID_AUTO, "fsb", CTLFLAG_RW,
1380 	    &rack_use_fsb, 1,
1381 	    "Should RACK use the fast send block?");
1382 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1383 	    SYSCTL_CHILDREN(rack_features),
1384 	    OID_AUTO, "rfo", CTLFLAG_RW,
1385 	    &rack_use_rfo, 1,
1386 	    "Should RACK use rack_fast_output()?");
1387 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1388 	    SYSCTL_CHILDREN(rack_features),
1389 	    OID_AUTO, "rsmrfo", CTLFLAG_RW,
1390 	    &rack_use_rsm_rfo, 1,
1391 	    "Should RACK use rack_fast_rsm_output()?");
1392 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1393 	    SYSCTL_CHILDREN(rack_features),
1394 	    OID_AUTO, "non_paced_lro_queue", CTLFLAG_RW,
1395 	    &rack_enable_mqueue_for_nonpaced, 0,
1396 	    "Should RACK use mbuf queuing for non-paced connections");
1397 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1398 	    SYSCTL_CHILDREN(rack_features),
1399 	    OID_AUTO, "hystartplusplus", CTLFLAG_RW,
1400 	    &rack_do_hystart, 0,
1401 	    "Should RACK enable HyStart++ on connections?");
1402 	/* Misc rack controls */
1403 	rack_misc = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1404 	    SYSCTL_CHILDREN(rack_sysctl_root),
1405 	    OID_AUTO,
1406 	    "misc",
1407 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1408 	    "Misc related controls");
1409 #ifdef TCP_ACCOUNTING
1410 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1411 	    SYSCTL_CHILDREN(rack_misc),
1412 	    OID_AUTO, "tcp_acct", CTLFLAG_RW,
1413 	    &rack_tcp_accounting, 0,
1414 	    "Should we turn on TCP accounting for all rack sessions?");
1415 #endif
1416 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1417 	    SYSCTL_CHILDREN(rack_misc),
1418 	    OID_AUTO, "apply_rtt_with_low_conf", CTLFLAG_RW,
1419 	    &rack_apply_rtt_with_reduced_conf, 0,
1420 	    "When a persist or keep-alive probe is not answered do we calculate rtt on subsequent answers?");
1421 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1422 	    SYSCTL_CHILDREN(rack_misc),
1423 	    OID_AUTO, "rack_dsack_ctl", CTLFLAG_RW,
1424 	    &rack_dsack_std_based, 3,
1425 	    "How do we process dsack with respect to rack timers, bit field, 3 is standards based?");
1426 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1427 	    SYSCTL_CHILDREN(rack_misc),
1428 	    OID_AUTO, "prr_addback_max", CTLFLAG_RW,
1429 	    &rack_prr_addbackmax, 2,
1430 	    "What is the maximum number of MSS we allow to be added back if prr can't send all its data?");
1431 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1432 	    SYSCTL_CHILDREN(rack_misc),
1433 	    OID_AUTO, "stats_gets_ms", CTLFLAG_RW,
1434 	    &rack_stats_gets_ms_rtt, 1,
1435 	    "What do we feed the stats framework (1 = ms_rtt, 0 = us_rtt, 2 = ms_rtt from hdwr, > 2 usec rtt from hdwr)?");
1436 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1437 	    SYSCTL_CHILDREN(rack_misc),
1438 	    OID_AUTO, "clientlowbuf", CTLFLAG_RW,
1439 	    &rack_client_low_buf, 0,
1440 	    "Client low buffer level (below this we are more aggressive in DGP exiting recovery (0 = off)?");
1441 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1442 	    SYSCTL_CHILDREN(rack_misc),
1443 	    OID_AUTO, "defprofile", CTLFLAG_RW,
1444 	    &rack_def_profile, 0,
1445 	    "Should RACK use a default profile (0=no, num == profile num)?");
1446 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1447 	    SYSCTL_CHILDREN(rack_misc),
1448 	    OID_AUTO, "shared_cwnd", CTLFLAG_RW,
1449 	    &rack_enable_shared_cwnd, 1,
1450 	    "Should RACK try to use the shared cwnd on connections where allowed");
1451 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1452 	    SYSCTL_CHILDREN(rack_misc),
1453 	    OID_AUTO, "limits_on_scwnd", CTLFLAG_RW,
1454 	    &rack_limits_scwnd, 1,
1455 	    "Should RACK place low end time limits on the shared cwnd feature");
1456 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1457 	    SYSCTL_CHILDREN(rack_misc),
1458 	    OID_AUTO, "iMac_dack", CTLFLAG_RW,
1459 	    &rack_use_imac_dack, 0,
1460 	    "Should RACK try to emulate iMac delayed ack");
1461 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1462 	    SYSCTL_CHILDREN(rack_misc),
1463 	    OID_AUTO, "no_prr", CTLFLAG_RW,
1464 	    &rack_disable_prr, 0,
1465 	    "Should RACK not use prr and only pace (must have pacing on)");
1466 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1467 	    SYSCTL_CHILDREN(rack_misc),
1468 	    OID_AUTO, "bb_verbose", CTLFLAG_RW,
1469 	    &rack_verbose_logging, 0,
1470 	    "Should RACK black box logging be verbose");
1471 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1472 	    SYSCTL_CHILDREN(rack_misc),
1473 	    OID_AUTO, "data_after_close", CTLFLAG_RW,
1474 	    &rack_ignore_data_after_close, 1,
1475 	    "Do we hold off sending a RST until all pending data is ack'd");
1476 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1477 	    SYSCTL_CHILDREN(rack_misc),
1478 	    OID_AUTO, "no_sack_needed", CTLFLAG_RW,
1479 	    &rack_sack_not_required, 1,
1480 	    "Do we allow rack to run on connections not supporting SACK");
1481 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1482 	    SYSCTL_CHILDREN(rack_misc),
1483 	    OID_AUTO, "prr_sendalot", CTLFLAG_RW,
1484 	    &rack_send_a_lot_in_prr, 1,
1485 	    "Send a lot in prr");
1486 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1487 	    SYSCTL_CHILDREN(rack_misc),
1488 	    OID_AUTO, "autoscale", CTLFLAG_RW,
1489 	    &rack_autosndbuf_inc, 20,
1490 	    "What percentage should rack scale up its snd buffer by?");
1491 	/* Sack Attacker detection stuff */
1492 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1493 	    SYSCTL_CHILDREN(rack_attack),
1494 	    OID_AUTO, "detect_highsackratio", CTLFLAG_RW,
1495 	    &rack_highest_sack_thresh_seen, 0,
1496 	    "Highest sack to ack ratio seen");
1497 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1498 	    SYSCTL_CHILDREN(rack_attack),
1499 	    OID_AUTO, "detect_highmoveratio", CTLFLAG_RW,
1500 	    &rack_highest_move_thresh_seen, 0,
1501 	    "Highest move to non-move ratio seen");
1502 	rack_ack_total = counter_u64_alloc(M_WAITOK);
1503 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1504 	    SYSCTL_CHILDREN(rack_attack),
1505 	    OID_AUTO, "acktotal", CTLFLAG_RD,
1506 	    &rack_ack_total,
1507 	    "Total number of Ack's");
1508 	rack_express_sack = counter_u64_alloc(M_WAITOK);
1509 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1510 	    SYSCTL_CHILDREN(rack_attack),
1511 	    OID_AUTO, "exp_sacktotal", CTLFLAG_RD,
1512 	    &rack_express_sack,
1513 	    "Total expresss number of Sack's");
1514 	rack_sack_total = counter_u64_alloc(M_WAITOK);
1515 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1516 	    SYSCTL_CHILDREN(rack_attack),
1517 	    OID_AUTO, "sacktotal", CTLFLAG_RD,
1518 	    &rack_sack_total,
1519 	    "Total number of SACKs");
1520 	rack_move_none = counter_u64_alloc(M_WAITOK);
1521 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1522 	    SYSCTL_CHILDREN(rack_attack),
1523 	    OID_AUTO, "move_none", CTLFLAG_RD,
1524 	    &rack_move_none,
1525 	    "Total number of SACK index reuse of positions under threshold");
1526 	rack_move_some = counter_u64_alloc(M_WAITOK);
1527 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1528 	    SYSCTL_CHILDREN(rack_attack),
1529 	    OID_AUTO, "move_some", CTLFLAG_RD,
1530 	    &rack_move_some,
1531 	    "Total number of SACK index reuse of positions over threshold");
1532 	rack_sack_attacks_detected = counter_u64_alloc(M_WAITOK);
1533 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1534 	    SYSCTL_CHILDREN(rack_attack),
1535 	    OID_AUTO, "attacks", CTLFLAG_RD,
1536 	    &rack_sack_attacks_detected,
1537 	    "Total number of SACK attackers that had sack disabled");
1538 	rack_sack_attacks_reversed = counter_u64_alloc(M_WAITOK);
1539 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1540 	    SYSCTL_CHILDREN(rack_attack),
1541 	    OID_AUTO, "reversed", CTLFLAG_RD,
1542 	    &rack_sack_attacks_reversed,
1543 	    "Total number of SACK attackers that were later determined false positive");
1544 	rack_sack_used_next_merge = counter_u64_alloc(M_WAITOK);
1545 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1546 	    SYSCTL_CHILDREN(rack_attack),
1547 	    OID_AUTO, "nextmerge", CTLFLAG_RD,
1548 	    &rack_sack_used_next_merge,
1549 	    "Total number of times we used the next merge");
1550 	rack_sack_used_prev_merge = counter_u64_alloc(M_WAITOK);
1551 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1552 	    SYSCTL_CHILDREN(rack_attack),
1553 	    OID_AUTO, "prevmerge", CTLFLAG_RD,
1554 	    &rack_sack_used_prev_merge,
1555 	    "Total number of times we used the prev merge");
1556 	/* Counters */
1557 	rack_fto_send = counter_u64_alloc(M_WAITOK);
1558 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1559 	    SYSCTL_CHILDREN(rack_counters),
1560 	    OID_AUTO, "fto_send", CTLFLAG_RD,
1561 	    &rack_fto_send, "Total number of rack_fast_output sends");
1562 	rack_fto_rsm_send = counter_u64_alloc(M_WAITOK);
1563 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1564 	    SYSCTL_CHILDREN(rack_counters),
1565 	    OID_AUTO, "fto_rsm_send", CTLFLAG_RD,
1566 	    &rack_fto_rsm_send, "Total number of rack_fast_rsm_output sends");
1567 	rack_nfto_resend = counter_u64_alloc(M_WAITOK);
1568 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1569 	    SYSCTL_CHILDREN(rack_counters),
1570 	    OID_AUTO, "nfto_resend", CTLFLAG_RD,
1571 	    &rack_nfto_resend, "Total number of rack_output retransmissions");
1572 	rack_non_fto_send = counter_u64_alloc(M_WAITOK);
1573 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1574 	    SYSCTL_CHILDREN(rack_counters),
1575 	    OID_AUTO, "nfto_send", CTLFLAG_RD,
1576 	    &rack_non_fto_send, "Total number of rack_output first sends");
1577 	rack_extended_rfo = counter_u64_alloc(M_WAITOK);
1578 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1579 	    SYSCTL_CHILDREN(rack_counters),
1580 	    OID_AUTO, "rfo_extended", CTLFLAG_RD,
1581 	    &rack_extended_rfo, "Total number of times we extended rfo");
1582 
1583 	rack_hw_pace_init_fail = counter_u64_alloc(M_WAITOK);
1584 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1585 	    SYSCTL_CHILDREN(rack_counters),
1586 	    OID_AUTO, "hwpace_init_fail", CTLFLAG_RD,
1587 	    &rack_hw_pace_init_fail, "Total number of times we failed to initialize hw pacing");
1588 	rack_hw_pace_lost = counter_u64_alloc(M_WAITOK);
1589 
1590 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1591 	    SYSCTL_CHILDREN(rack_counters),
1592 	    OID_AUTO, "hwpace_lost", CTLFLAG_RD,
1593 	    &rack_hw_pace_lost, "Total number of times we failed to initialize hw pacing");
1594 	rack_tlp_tot = counter_u64_alloc(M_WAITOK);
1595 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1596 	    SYSCTL_CHILDREN(rack_counters),
1597 	    OID_AUTO, "tlp_to_total", CTLFLAG_RD,
1598 	    &rack_tlp_tot,
1599 	    "Total number of tail loss probe expirations");
1600 	rack_tlp_newdata = counter_u64_alloc(M_WAITOK);
1601 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1602 	    SYSCTL_CHILDREN(rack_counters),
1603 	    OID_AUTO, "tlp_new", CTLFLAG_RD,
1604 	    &rack_tlp_newdata,
1605 	    "Total number of tail loss probe sending new data");
1606 	rack_tlp_retran = counter_u64_alloc(M_WAITOK);
1607 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1608 	    SYSCTL_CHILDREN(rack_counters),
1609 	    OID_AUTO, "tlp_retran", CTLFLAG_RD,
1610 	    &rack_tlp_retran,
1611 	    "Total number of tail loss probe sending retransmitted data");
1612 	rack_tlp_retran_bytes = counter_u64_alloc(M_WAITOK);
1613 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1614 	    SYSCTL_CHILDREN(rack_counters),
1615 	    OID_AUTO, "tlp_retran_bytes", CTLFLAG_RD,
1616 	    &rack_tlp_retran_bytes,
1617 	    "Total bytes of tail loss probe sending retransmitted data");
1618 	rack_to_tot = counter_u64_alloc(M_WAITOK);
1619 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1620 	    SYSCTL_CHILDREN(rack_counters),
1621 	    OID_AUTO, "rack_to_tot", CTLFLAG_RD,
1622 	    &rack_to_tot,
1623 	    "Total number of times the rack to expired");
1624 	rack_saw_enobuf = counter_u64_alloc(M_WAITOK);
1625 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1626 	    SYSCTL_CHILDREN(rack_counters),
1627 	    OID_AUTO, "saw_enobufs", CTLFLAG_RD,
1628 	    &rack_saw_enobuf,
1629 	    "Total number of times a sends returned enobuf for non-hdwr paced connections");
1630 	rack_saw_enobuf_hw = counter_u64_alloc(M_WAITOK);
1631 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1632 	    SYSCTL_CHILDREN(rack_counters),
1633 	    OID_AUTO, "saw_enobufs_hw", CTLFLAG_RD,
1634 	    &rack_saw_enobuf_hw,
1635 	    "Total number of times a send returned enobuf for hdwr paced connections");
1636 	rack_saw_enetunreach = counter_u64_alloc(M_WAITOK);
1637 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1638 	    SYSCTL_CHILDREN(rack_counters),
1639 	    OID_AUTO, "saw_enetunreach", CTLFLAG_RD,
1640 	    &rack_saw_enetunreach,
1641 	    "Total number of times a send received a enetunreachable");
1642 	rack_hot_alloc = counter_u64_alloc(M_WAITOK);
1643 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1644 	    SYSCTL_CHILDREN(rack_counters),
1645 	    OID_AUTO, "alloc_hot", CTLFLAG_RD,
1646 	    &rack_hot_alloc,
1647 	    "Total allocations from the top of our list");
1648 	rack_to_alloc = counter_u64_alloc(M_WAITOK);
1649 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1650 	    SYSCTL_CHILDREN(rack_counters),
1651 	    OID_AUTO, "allocs", CTLFLAG_RD,
1652 	    &rack_to_alloc,
1653 	    "Total allocations of tracking structures");
1654 	rack_to_alloc_hard = counter_u64_alloc(M_WAITOK);
1655 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1656 	    SYSCTL_CHILDREN(rack_counters),
1657 	    OID_AUTO, "allochard", CTLFLAG_RD,
1658 	    &rack_to_alloc_hard,
1659 	    "Total allocations done with sleeping the hard way");
1660 	rack_to_alloc_emerg = counter_u64_alloc(M_WAITOK);
1661 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1662 	    SYSCTL_CHILDREN(rack_counters),
1663 	    OID_AUTO, "allocemerg", CTLFLAG_RD,
1664 	    &rack_to_alloc_emerg,
1665 	    "Total allocations done from emergency cache");
1666 	rack_to_alloc_limited = counter_u64_alloc(M_WAITOK);
1667 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1668 	    SYSCTL_CHILDREN(rack_counters),
1669 	    OID_AUTO, "alloc_limited", CTLFLAG_RD,
1670 	    &rack_to_alloc_limited,
1671 	    "Total allocations dropped due to limit");
1672 	rack_alloc_limited_conns = counter_u64_alloc(M_WAITOK);
1673 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1674 	    SYSCTL_CHILDREN(rack_counters),
1675 	    OID_AUTO, "alloc_limited_conns", CTLFLAG_RD,
1676 	    &rack_alloc_limited_conns,
1677 	    "Connections with allocations dropped due to limit");
1678 	rack_split_limited = counter_u64_alloc(M_WAITOK);
1679 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1680 	    SYSCTL_CHILDREN(rack_counters),
1681 	    OID_AUTO, "split_limited", CTLFLAG_RD,
1682 	    &rack_split_limited,
1683 	    "Split allocations dropped due to limit");
1684 	rack_persists_sends = counter_u64_alloc(M_WAITOK);
1685 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1686 	    SYSCTL_CHILDREN(rack_counters),
1687 	    OID_AUTO, "persist_sends", CTLFLAG_RD,
1688 	    &rack_persists_sends,
1689 	    "Number of times we sent a persist probe");
1690 	rack_persists_acks = counter_u64_alloc(M_WAITOK);
1691 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1692 	    SYSCTL_CHILDREN(rack_counters),
1693 	    OID_AUTO, "persist_acks", CTLFLAG_RD,
1694 	    &rack_persists_acks,
1695 	    "Number of times a persist probe was acked");
1696 	rack_persists_loss = counter_u64_alloc(M_WAITOK);
1697 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1698 	    SYSCTL_CHILDREN(rack_counters),
1699 	    OID_AUTO, "persist_loss", CTLFLAG_RD,
1700 	    &rack_persists_loss,
1701 	    "Number of times we detected a lost persist probe (no ack)");
1702 	rack_persists_lost_ends = counter_u64_alloc(M_WAITOK);
1703 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1704 	    SYSCTL_CHILDREN(rack_counters),
1705 	    OID_AUTO, "persist_loss_ends", CTLFLAG_RD,
1706 	    &rack_persists_lost_ends,
1707 	    "Number of lost persist probe (no ack) that the run ended with a PERSIST abort");
1708 #ifdef INVARIANTS
1709 	rack_adjust_map_bw = counter_u64_alloc(M_WAITOK);
1710 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1711 	    SYSCTL_CHILDREN(rack_counters),
1712 	    OID_AUTO, "map_adjust_req", CTLFLAG_RD,
1713 	    &rack_adjust_map_bw,
1714 	    "Number of times we hit the case where the sb went up and down on a sendmap entry");
1715 #endif
1716 	rack_multi_single_eq = counter_u64_alloc(M_WAITOK);
1717 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1718 	    SYSCTL_CHILDREN(rack_counters),
1719 	    OID_AUTO, "cmp_ack_equiv", CTLFLAG_RD,
1720 	    &rack_multi_single_eq,
1721 	    "Number of compressed acks total represented");
1722 	rack_proc_non_comp_ack = counter_u64_alloc(M_WAITOK);
1723 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1724 	    SYSCTL_CHILDREN(rack_counters),
1725 	    OID_AUTO, "cmp_ack_not", CTLFLAG_RD,
1726 	    &rack_proc_non_comp_ack,
1727 	    "Number of non compresseds acks that we processed");
1728 
1729 
1730 	rack_sack_proc_all = counter_u64_alloc(M_WAITOK);
1731 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1732 	    SYSCTL_CHILDREN(rack_counters),
1733 	    OID_AUTO, "sack_long", CTLFLAG_RD,
1734 	    &rack_sack_proc_all,
1735 	    "Total times we had to walk whole list for sack processing");
1736 	rack_sack_proc_restart = counter_u64_alloc(M_WAITOK);
1737 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1738 	    SYSCTL_CHILDREN(rack_counters),
1739 	    OID_AUTO, "sack_restart", CTLFLAG_RD,
1740 	    &rack_sack_proc_restart,
1741 	    "Total times we had to walk whole list due to a restart");
1742 	rack_sack_proc_short = counter_u64_alloc(M_WAITOK);
1743 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1744 	    SYSCTL_CHILDREN(rack_counters),
1745 	    OID_AUTO, "sack_short", CTLFLAG_RD,
1746 	    &rack_sack_proc_short,
1747 	    "Total times we took shortcut for sack processing");
1748 	rack_sack_skipped_acked = counter_u64_alloc(M_WAITOK);
1749 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1750 	    SYSCTL_CHILDREN(rack_attack),
1751 	    OID_AUTO, "skipacked", CTLFLAG_RD,
1752 	    &rack_sack_skipped_acked,
1753 	    "Total number of times we skipped previously sacked");
1754 	rack_sack_splits = counter_u64_alloc(M_WAITOK);
1755 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1756 	    SYSCTL_CHILDREN(rack_attack),
1757 	    OID_AUTO, "ofsplit", CTLFLAG_RD,
1758 	    &rack_sack_splits,
1759 	    "Total number of times we did the old fashion tree split");
1760 	rack_input_idle_reduces = counter_u64_alloc(M_WAITOK);
1761 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1762 	    SYSCTL_CHILDREN(rack_counters),
1763 	    OID_AUTO, "idle_reduce_oninput", CTLFLAG_RD,
1764 	    &rack_input_idle_reduces,
1765 	    "Total number of idle reductions on input");
1766 	rack_collapsed_win_seen = counter_u64_alloc(M_WAITOK);
1767 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1768 	    SYSCTL_CHILDREN(rack_counters),
1769 	    OID_AUTO, "collapsed_win_seen", CTLFLAG_RD,
1770 	    &rack_collapsed_win_seen,
1771 	    "Total number of collapsed window events seen (where our window shrinks)");
1772 
1773 	rack_collapsed_win = counter_u64_alloc(M_WAITOK);
1774 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1775 	    SYSCTL_CHILDREN(rack_counters),
1776 	    OID_AUTO, "collapsed_win", CTLFLAG_RD,
1777 	    &rack_collapsed_win,
1778 	    "Total number of collapsed window events where we mark packets");
1779 	rack_collapsed_win_rxt = counter_u64_alloc(M_WAITOK);
1780 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1781 	    SYSCTL_CHILDREN(rack_counters),
1782 	    OID_AUTO, "collapsed_win_rxt", CTLFLAG_RD,
1783 	    &rack_collapsed_win_rxt,
1784 	    "Total number of packets that were retransmitted");
1785 	rack_collapsed_win_rxt_bytes = counter_u64_alloc(M_WAITOK);
1786 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1787 	    SYSCTL_CHILDREN(rack_counters),
1788 	    OID_AUTO, "collapsed_win_bytes", CTLFLAG_RD,
1789 	    &rack_collapsed_win_rxt_bytes,
1790 	    "Total number of bytes that were retransmitted");
1791 	rack_try_scwnd = counter_u64_alloc(M_WAITOK);
1792 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1793 	    SYSCTL_CHILDREN(rack_counters),
1794 	    OID_AUTO, "tried_scwnd", CTLFLAG_RD,
1795 	    &rack_try_scwnd,
1796 	    "Total number of scwnd attempts");
1797 	COUNTER_ARRAY_ALLOC(rack_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK);
1798 	SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
1799 	    OID_AUTO, "outsize", CTLFLAG_RD,
1800 	    rack_out_size, TCP_MSS_ACCT_SIZE, "MSS send sizes");
1801 	COUNTER_ARRAY_ALLOC(rack_opts_arry, RACK_OPTS_SIZE, M_WAITOK);
1802 	SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
1803 	    OID_AUTO, "opts", CTLFLAG_RD,
1804 	    rack_opts_arry, RACK_OPTS_SIZE, "RACK Option Stats");
1805 	SYSCTL_ADD_PROC(&rack_sysctl_ctx,
1806 	    SYSCTL_CHILDREN(rack_sysctl_root),
1807 	    OID_AUTO, "clear", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1808 	    &rack_clear_counter, 0, sysctl_rack_clear, "IU", "Clear counters");
1809 }
1810 
1811 static __inline int
1812 rb_map_cmp(struct rack_sendmap *b, struct rack_sendmap *a)
1813 {
1814 	if (SEQ_GEQ(b->r_start, a->r_start) &&
1815 	    SEQ_LT(b->r_start, a->r_end)) {
1816 		/*
1817 		 * The entry b is within the
1818 		 * block a. i.e.:
1819 		 * a --   |-------------|
1820 		 * b --   |----|
1821 		 * <or>
1822 		 * b --       |------|
1823 		 * <or>
1824 		 * b --       |-----------|
1825 		 */
1826 		return (0);
1827 	} else if (SEQ_GEQ(b->r_start, a->r_end)) {
1828 		/*
1829 		 * b falls as either the next
1830 		 * sequence block after a so a
1831 		 * is said to be smaller than b.
1832 		 * i.e:
1833 		 * a --   |------|
1834 		 * b --          |--------|
1835 		 * or
1836 		 * b --              |-----|
1837 		 */
1838 		return (1);
1839 	}
1840 	/*
1841 	 * Whats left is where a is
1842 	 * larger than b. i.e:
1843 	 * a --         |-------|
1844 	 * b --  |---|
1845 	 * or even possibly
1846 	 * b --   |--------------|
1847 	 */
1848 	return (-1);
1849 }
1850 
1851 RB_PROTOTYPE(rack_rb_tree_head, rack_sendmap, r_next, rb_map_cmp);
1852 RB_GENERATE(rack_rb_tree_head, rack_sendmap, r_next, rb_map_cmp);
1853 
1854 static uint32_t
1855 rc_init_window(struct tcp_rack *rack)
1856 {
1857 	uint32_t win;
1858 
1859 	if (rack->rc_init_win == 0) {
1860 		/*
1861 		 * Nothing set by the user, use the system stack
1862 		 * default.
1863 		 */
1864 		return (tcp_compute_initwnd(tcp_maxseg(rack->rc_tp)));
1865 	}
1866 	win = ctf_fixed_maxseg(rack->rc_tp) * rack->rc_init_win;
1867 	return (win);
1868 }
1869 
1870 static uint64_t
1871 rack_get_fixed_pacing_bw(struct tcp_rack *rack)
1872 {
1873 	if (IN_FASTRECOVERY(rack->rc_tp->t_flags))
1874 		return (rack->r_ctl.rc_fixed_pacing_rate_rec);
1875 	else if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh)
1876 		return (rack->r_ctl.rc_fixed_pacing_rate_ss);
1877 	else
1878 		return (rack->r_ctl.rc_fixed_pacing_rate_ca);
1879 }
1880 
1881 static uint64_t
1882 rack_get_bw(struct tcp_rack *rack)
1883 {
1884 	if (rack->use_fixed_rate) {
1885 		/* Return the fixed pacing rate */
1886 		return (rack_get_fixed_pacing_bw(rack));
1887 	}
1888 	if (rack->r_ctl.gp_bw == 0) {
1889 		/*
1890 		 * We have yet no b/w measurement,
1891 		 * if we have a user set initial bw
1892 		 * return it. If we don't have that and
1893 		 * we have an srtt, use the tcp IW (10) to
1894 		 * calculate a fictional b/w over the SRTT
1895 		 * which is more or less a guess. Note
1896 		 * we don't use our IW from rack on purpose
1897 		 * so if we have like IW=30, we are not
1898 		 * calculating a "huge" b/w.
1899 		 */
1900 		uint64_t bw, srtt;
1901 		if (rack->r_ctl.init_rate)
1902 			return (rack->r_ctl.init_rate);
1903 
1904 		/* Has the user set a max peak rate? */
1905 #ifdef NETFLIX_PEAKRATE
1906 		if (rack->rc_tp->t_maxpeakrate)
1907 			return (rack->rc_tp->t_maxpeakrate);
1908 #endif
1909 		/* Ok lets come up with the IW guess, if we have a srtt */
1910 		if (rack->rc_tp->t_srtt == 0) {
1911 			/*
1912 			 * Go with old pacing method
1913 			 * i.e. burst mitigation only.
1914 			 */
1915 			return (0);
1916 		}
1917 		/* Ok lets get the initial TCP win (not racks) */
1918 		bw = tcp_compute_initwnd(tcp_maxseg(rack->rc_tp));
1919 		srtt = (uint64_t)rack->rc_tp->t_srtt;
1920 		bw *= (uint64_t)USECS_IN_SECOND;
1921 		bw /= srtt;
1922 		if (rack->r_ctl.bw_rate_cap && (bw > rack->r_ctl.bw_rate_cap))
1923 			bw = rack->r_ctl.bw_rate_cap;
1924 		return (bw);
1925 	} else {
1926 		uint64_t bw;
1927 
1928 		if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) {
1929 			/* Averaging is done, we can return the value */
1930 			bw = rack->r_ctl.gp_bw;
1931 		} else {
1932 			/* Still doing initial average must calculate */
1933 			bw = rack->r_ctl.gp_bw / rack->r_ctl.num_measurements;
1934 		}
1935 #ifdef NETFLIX_PEAKRATE
1936 		if ((rack->rc_tp->t_maxpeakrate) &&
1937 		    (bw > rack->rc_tp->t_maxpeakrate)) {
1938 			/* The user has set a peak rate to pace at
1939 			 * don't allow us to pace faster than that.
1940 			 */
1941 			return (rack->rc_tp->t_maxpeakrate);
1942 		}
1943 #endif
1944 		if (rack->r_ctl.bw_rate_cap && (bw > rack->r_ctl.bw_rate_cap))
1945 			bw = rack->r_ctl.bw_rate_cap;
1946 		return (bw);
1947 	}
1948 }
1949 
1950 static uint16_t
1951 rack_get_output_gain(struct tcp_rack *rack, struct rack_sendmap *rsm)
1952 {
1953 	if (rack->use_fixed_rate) {
1954 		return (100);
1955 	} else if (rack->in_probe_rtt && (rsm == NULL))
1956 		return (rack->r_ctl.rack_per_of_gp_probertt);
1957 	else if ((IN_FASTRECOVERY(rack->rc_tp->t_flags) &&
1958 		  rack->r_ctl.rack_per_of_gp_rec)) {
1959 		if (rsm) {
1960 			/* a retransmission always use the recovery rate */
1961 			return (rack->r_ctl.rack_per_of_gp_rec);
1962 		} else if (rack->rack_rec_nonrxt_use_cr) {
1963 			/* Directed to use the configured rate */
1964 			goto configured_rate;
1965 		} else if (rack->rack_no_prr &&
1966 			   (rack->r_ctl.rack_per_of_gp_rec > 100)) {
1967 			/* No PRR, lets just use the b/w estimate only */
1968 			return (100);
1969 		} else {
1970 			/*
1971 			 * Here we may have a non-retransmit but we
1972 			 * have no overrides, so just use the recovery
1973 			 * rate (prr is in effect).
1974 			 */
1975 			return (rack->r_ctl.rack_per_of_gp_rec);
1976 		}
1977 	}
1978 configured_rate:
1979 	/* For the configured rate we look at our cwnd vs the ssthresh */
1980 	if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh)
1981 		return (rack->r_ctl.rack_per_of_gp_ss);
1982 	else
1983 		return (rack->r_ctl.rack_per_of_gp_ca);
1984 }
1985 
1986 static void
1987 rack_log_dsack_event(struct tcp_rack *rack, uint8_t mod, uint32_t flex4, uint32_t flex5, uint32_t flex6)
1988 {
1989 	/*
1990 	 * Types of logs (mod value)
1991 	 * 1 = dsack_persists reduced by 1 via T-O or fast recovery exit.
1992 	 * 2 = a dsack round begins, persist is reset to 16.
1993 	 * 3 = a dsack round ends
1994 	 * 4 = Dsack option increases rack rtt flex5 is the srtt input, flex6 is thresh
1995 	 * 5 = Socket option set changing the control flags rc_rack_tmr_std_based, rc_rack_use_dsack
1996 	 * 6 = Final rack rtt, flex4 is srtt and flex6 is final limited thresh.
1997 	 */
1998 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1999 		union tcp_log_stackspecific log;
2000 		struct timeval tv;
2001 
2002 		memset(&log, 0, sizeof(log));
2003 		log.u_bbr.flex1 = rack->rc_rack_tmr_std_based;
2004 		log.u_bbr.flex1 <<= 1;
2005 		log.u_bbr.flex1 |= rack->rc_rack_use_dsack;
2006 		log.u_bbr.flex1 <<= 1;
2007 		log.u_bbr.flex1 |= rack->rc_dsack_round_seen;
2008 		log.u_bbr.flex2 = rack->r_ctl.dsack_round_end;
2009 		log.u_bbr.flex3 = rack->r_ctl.num_dsack;
2010 		log.u_bbr.flex4 = flex4;
2011 		log.u_bbr.flex5 = flex5;
2012 		log.u_bbr.flex6 = flex6;
2013 		log.u_bbr.flex7 = rack->r_ctl.dsack_persist;
2014 		log.u_bbr.flex8 = mod;
2015 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2016 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2017 		    &rack->rc_inp->inp_socket->so_rcv,
2018 		    &rack->rc_inp->inp_socket->so_snd,
2019 		    RACK_DSACK_HANDLING, 0,
2020 		    0, &log, false, &tv);
2021 	}
2022 }
2023 
2024 static void
2025 rack_log_hdwr_pacing(struct tcp_rack *rack,
2026 		     uint64_t rate, uint64_t hw_rate, int line,
2027 		     int error, uint16_t mod)
2028 {
2029 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2030 		union tcp_log_stackspecific log;
2031 		struct timeval tv;
2032 		const struct ifnet *ifp;
2033 
2034 		memset(&log, 0, sizeof(log));
2035 		log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff);
2036 		log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff);
2037 		if (rack->r_ctl.crte) {
2038 			ifp = rack->r_ctl.crte->ptbl->rs_ifp;
2039 		} else if (rack->rc_inp->inp_route.ro_nh &&
2040 			   rack->rc_inp->inp_route.ro_nh->nh_ifp) {
2041 			ifp = rack->rc_inp->inp_route.ro_nh->nh_ifp;
2042 		} else
2043 			ifp = NULL;
2044 		if (ifp) {
2045 			log.u_bbr.flex3 = (((uint64_t)ifp  >> 32) & 0x00000000ffffffff);
2046 			log.u_bbr.flex4 = ((uint64_t)ifp & 0x00000000ffffffff);
2047 		}
2048 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2049 		log.u_bbr.bw_inuse = rate;
2050 		log.u_bbr.flex5 = line;
2051 		log.u_bbr.flex6 = error;
2052 		log.u_bbr.flex7 = mod;
2053 		log.u_bbr.applimited = rack->r_ctl.rc_pace_max_segs;
2054 		log.u_bbr.flex8 = rack->use_fixed_rate;
2055 		log.u_bbr.flex8 <<= 1;
2056 		log.u_bbr.flex8 |= rack->rack_hdrw_pacing;
2057 		log.u_bbr.pkts_out = rack->rc_tp->t_maxseg;
2058 		log.u_bbr.delRate = rack->r_ctl.crte_prev_rate;
2059 		if (rack->r_ctl.crte)
2060 			log.u_bbr.cur_del_rate = rack->r_ctl.crte->rate;
2061 		else
2062 			log.u_bbr.cur_del_rate = 0;
2063 		log.u_bbr.rttProp = rack->r_ctl.last_hw_bw_req;
2064 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2065 		    &rack->rc_inp->inp_socket->so_rcv,
2066 		    &rack->rc_inp->inp_socket->so_snd,
2067 		    BBR_LOG_HDWR_PACE, 0,
2068 		    0, &log, false, &tv);
2069 	}
2070 }
2071 
2072 static uint64_t
2073 rack_get_output_bw(struct tcp_rack *rack, uint64_t bw, struct rack_sendmap *rsm, int *capped)
2074 {
2075 	/*
2076 	 * We allow rack_per_of_gp_xx to dictate our bw rate we want.
2077 	 */
2078 	uint64_t bw_est, high_rate;
2079 	uint64_t gain;
2080 
2081 	gain = (uint64_t)rack_get_output_gain(rack, rsm);
2082 	bw_est = bw * gain;
2083 	bw_est /= (uint64_t)100;
2084 	/* Never fall below the minimum (def 64kbps) */
2085 	if (bw_est < RACK_MIN_BW)
2086 		bw_est = RACK_MIN_BW;
2087 	if (rack->r_rack_hw_rate_caps) {
2088 		/* Rate caps are in place */
2089 		if (rack->r_ctl.crte != NULL) {
2090 			/* We have a hdwr rate already */
2091 			high_rate = tcp_hw_highest_rate(rack->r_ctl.crte);
2092 			if (bw_est >= high_rate) {
2093 				/* We are capping bw at the highest rate table entry */
2094 				rack_log_hdwr_pacing(rack,
2095 						     bw_est, high_rate, __LINE__,
2096 						     0, 3);
2097 				bw_est = high_rate;
2098 				if (capped)
2099 					*capped = 1;
2100 			}
2101 		} else if ((rack->rack_hdrw_pacing == 0) &&
2102 			   (rack->rack_hdw_pace_ena) &&
2103 			   (rack->rack_attempt_hdwr_pace == 0) &&
2104 			   (rack->rc_inp->inp_route.ro_nh != NULL) &&
2105 			   (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
2106 			/*
2107 			 * Special case, we have not yet attempted hardware
2108 			 * pacing, and yet we may, when we do, find out if we are
2109 			 * above the highest rate. We need to know the maxbw for the interface
2110 			 * in question (if it supports ratelimiting). We get back
2111 			 * a 0, if the interface is not found in the RL lists.
2112 			 */
2113 			high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp);
2114 			if (high_rate) {
2115 				/* Yep, we have a rate is it above this rate? */
2116 				if (bw_est > high_rate) {
2117 					bw_est = high_rate;
2118 					if (capped)
2119 						*capped = 1;
2120 				}
2121 			}
2122 		}
2123 	}
2124 	return (bw_est);
2125 }
2126 
2127 static void
2128 rack_log_retran_reason(struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t tsused, uint32_t thresh, int mod)
2129 {
2130 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2131 		union tcp_log_stackspecific log;
2132 		struct timeval tv;
2133 
2134 		if ((mod != 1) && (rack_verbose_logging == 0)) {
2135 			/*
2136 			 * We get 3 values currently for mod
2137 			 * 1 - We are retransmitting and this tells the reason.
2138 			 * 2 - We are clearing a dup-ack count.
2139 			 * 3 - We are incrementing a dup-ack count.
2140 			 *
2141 			 * The clear/increment are only logged
2142 			 * if you have BBverbose on.
2143 			 */
2144 			return;
2145 		}
2146 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2147 		log.u_bbr.flex1 = tsused;
2148 		log.u_bbr.flex2 = thresh;
2149 		log.u_bbr.flex3 = rsm->r_flags;
2150 		log.u_bbr.flex4 = rsm->r_dupack;
2151 		log.u_bbr.flex5 = rsm->r_start;
2152 		log.u_bbr.flex6 = rsm->r_end;
2153 		log.u_bbr.flex8 = mod;
2154 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2155 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2156 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2157 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2158 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2159 		log.u_bbr.pacing_gain = rack->r_must_retran;
2160 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2161 		    &rack->rc_inp->inp_socket->so_rcv,
2162 		    &rack->rc_inp->inp_socket->so_snd,
2163 		    BBR_LOG_SETTINGS_CHG, 0,
2164 		    0, &log, false, &tv);
2165 	}
2166 }
2167 
2168 static void
2169 rack_log_to_start(struct tcp_rack *rack, uint32_t cts, uint32_t to, int32_t slot, uint8_t which)
2170 {
2171 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2172 		union tcp_log_stackspecific log;
2173 		struct timeval tv;
2174 
2175 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2176 		log.u_bbr.flex1 = rack->rc_tp->t_srtt;
2177 		log.u_bbr.flex2 = to;
2178 		log.u_bbr.flex3 = rack->r_ctl.rc_hpts_flags;
2179 		log.u_bbr.flex4 = slot;
2180 		log.u_bbr.flex5 = rack->rc_inp->inp_hptsslot;
2181 		log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
2182 		log.u_bbr.flex7 = rack->rc_in_persist;
2183 		log.u_bbr.flex8 = which;
2184 		if (rack->rack_no_prr)
2185 			log.u_bbr.pkts_out = 0;
2186 		else
2187 			log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
2188 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2189 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2190 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2191 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2192 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2193 		log.u_bbr.pacing_gain = rack->r_must_retran;
2194 		log.u_bbr.cwnd_gain = rack->rc_has_collapsed;
2195 		log.u_bbr.lt_epoch = rack->rc_tp->t_rxtshift;
2196 		log.u_bbr.lost = rack_rto_min;
2197 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2198 		    &rack->rc_inp->inp_socket->so_rcv,
2199 		    &rack->rc_inp->inp_socket->so_snd,
2200 		    BBR_LOG_TIMERSTAR, 0,
2201 		    0, &log, false, &tv);
2202 	}
2203 }
2204 
2205 static void
2206 rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm)
2207 {
2208 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2209 		union tcp_log_stackspecific log;
2210 		struct timeval tv;
2211 
2212 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2213 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2214 		log.u_bbr.flex8 = to_num;
2215 		log.u_bbr.flex1 = rack->r_ctl.rc_rack_min_rtt;
2216 		log.u_bbr.flex2 = rack->rc_rack_rtt;
2217 		if (rsm == NULL)
2218 			log.u_bbr.flex3 = 0;
2219 		else
2220 			log.u_bbr.flex3 = rsm->r_end - rsm->r_start;
2221 		if (rack->rack_no_prr)
2222 			log.u_bbr.flex5 = 0;
2223 		else
2224 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
2225 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2226 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2227 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2228 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2229 		log.u_bbr.pacing_gain = rack->r_must_retran;
2230 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2231 		    &rack->rc_inp->inp_socket->so_rcv,
2232 		    &rack->rc_inp->inp_socket->so_snd,
2233 		    BBR_LOG_RTO, 0,
2234 		    0, &log, false, &tv);
2235 	}
2236 }
2237 
2238 static void
2239 rack_log_map_chg(struct tcpcb *tp, struct tcp_rack *rack,
2240 		 struct rack_sendmap *prev,
2241 		 struct rack_sendmap *rsm,
2242 		 struct rack_sendmap *next,
2243 		 int flag, uint32_t th_ack, int line)
2244 {
2245 	if (rack_verbose_logging && (tp->t_logstate != TCP_LOG_STATE_OFF)) {
2246 		union tcp_log_stackspecific log;
2247 		struct timeval tv;
2248 
2249 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2250 		log.u_bbr.flex8 = flag;
2251 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2252 		log.u_bbr.cur_del_rate = (uint64_t)prev;
2253 		log.u_bbr.delRate = (uint64_t)rsm;
2254 		log.u_bbr.rttProp = (uint64_t)next;
2255 		log.u_bbr.flex7 = 0;
2256 		if (prev) {
2257 			log.u_bbr.flex1 = prev->r_start;
2258 			log.u_bbr.flex2 = prev->r_end;
2259 			log.u_bbr.flex7 |= 0x4;
2260 		}
2261 		if (rsm) {
2262 			log.u_bbr.flex3 = rsm->r_start;
2263 			log.u_bbr.flex4 = rsm->r_end;
2264 			log.u_bbr.flex7 |= 0x2;
2265 		}
2266 		if (next) {
2267 			log.u_bbr.flex5 = next->r_start;
2268 			log.u_bbr.flex6 = next->r_end;
2269 			log.u_bbr.flex7 |= 0x1;
2270 		}
2271 		log.u_bbr.applimited = line;
2272 		log.u_bbr.pkts_out = th_ack;
2273 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2274 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2275 		if (rack->rack_no_prr)
2276 			log.u_bbr.lost = 0;
2277 		else
2278 			log.u_bbr.lost = rack->r_ctl.rc_prr_sndcnt;
2279 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2280 		    &rack->rc_inp->inp_socket->so_rcv,
2281 		    &rack->rc_inp->inp_socket->so_snd,
2282 		    TCP_LOG_MAPCHG, 0,
2283 		    0, &log, false, &tv);
2284 	}
2285 }
2286 
2287 static void
2288 rack_log_rtt_upd(struct tcpcb *tp, struct tcp_rack *rack, uint32_t t, uint32_t len,
2289 		 struct rack_sendmap *rsm, int conf)
2290 {
2291 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
2292 		union tcp_log_stackspecific log;
2293 		struct timeval tv;
2294 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2295 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2296 		log.u_bbr.flex1 = t;
2297 		log.u_bbr.flex2 = len;
2298 		log.u_bbr.flex3 = rack->r_ctl.rc_rack_min_rtt;
2299 		log.u_bbr.flex4 = rack->r_ctl.rack_rs.rs_rtt_lowest;
2300 		log.u_bbr.flex5 = rack->r_ctl.rack_rs.rs_rtt_highest;
2301 		log.u_bbr.flex6 = rack->r_ctl.rack_rs.rs_us_rtrcnt;
2302 		log.u_bbr.flex7 = conf;
2303 		log.u_bbr.rttProp = (uint64_t)rack->r_ctl.rack_rs.rs_rtt_tot;
2304 		log.u_bbr.flex8 = rack->r_ctl.rc_rate_sample_method;
2305 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2306 		log.u_bbr.delivered = rack->r_ctl.rack_rs.rs_us_rtrcnt;
2307 		log.u_bbr.pkts_out = rack->r_ctl.rack_rs.rs_flags;
2308 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2309 		if (rsm) {
2310 			log.u_bbr.pkt_epoch = rsm->r_start;
2311 			log.u_bbr.lost = rsm->r_end;
2312 			log.u_bbr.cwnd_gain = rsm->r_rtr_cnt;
2313 			/* We loose any upper of the 24 bits */
2314 			log.u_bbr.pacing_gain = (uint16_t)rsm->r_flags;
2315 		} else {
2316 			/* Its a SYN */
2317 			log.u_bbr.pkt_epoch = rack->rc_tp->iss;
2318 			log.u_bbr.lost = 0;
2319 			log.u_bbr.cwnd_gain = 0;
2320 			log.u_bbr.pacing_gain = 0;
2321 		}
2322 		/* Write out general bits of interest rrs here */
2323 		log.u_bbr.use_lt_bw = rack->rc_highly_buffered;
2324 		log.u_bbr.use_lt_bw <<= 1;
2325 		log.u_bbr.use_lt_bw |= rack->forced_ack;
2326 		log.u_bbr.use_lt_bw <<= 1;
2327 		log.u_bbr.use_lt_bw |= rack->rc_gp_dyn_mul;
2328 		log.u_bbr.use_lt_bw <<= 1;
2329 		log.u_bbr.use_lt_bw |= rack->in_probe_rtt;
2330 		log.u_bbr.use_lt_bw <<= 1;
2331 		log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt;
2332 		log.u_bbr.use_lt_bw <<= 1;
2333 		log.u_bbr.use_lt_bw |= rack->app_limited_needs_set;
2334 		log.u_bbr.use_lt_bw <<= 1;
2335 		log.u_bbr.use_lt_bw |= rack->rc_gp_filled;
2336 		log.u_bbr.use_lt_bw <<= 1;
2337 		log.u_bbr.use_lt_bw |= rack->rc_dragged_bottom;
2338 		log.u_bbr.applimited = rack->r_ctl.rc_target_probertt_flight;
2339 		log.u_bbr.epoch = rack->r_ctl.rc_time_probertt_starts;
2340 		log.u_bbr.lt_epoch = rack->r_ctl.rc_time_probertt_entered;
2341 		log.u_bbr.cur_del_rate = rack->r_ctl.rc_lower_rtt_us_cts;
2342 		log.u_bbr.delRate = rack->r_ctl.rc_gp_srtt;
2343 		log.u_bbr.bw_inuse = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
2344 		log.u_bbr.bw_inuse <<= 32;
2345 		if (rsm)
2346 			log.u_bbr.bw_inuse |= ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]);
2347 		TCP_LOG_EVENTP(tp, NULL,
2348 		    &rack->rc_inp->inp_socket->so_rcv,
2349 		    &rack->rc_inp->inp_socket->so_snd,
2350 		    BBR_LOG_BBRRTT, 0,
2351 		    0, &log, false, &tv);
2352 
2353 
2354 	}
2355 }
2356 
2357 static void
2358 rack_log_rtt_sample(struct tcp_rack *rack, uint32_t rtt)
2359 {
2360 	/*
2361 	 * Log the rtt sample we are
2362 	 * applying to the srtt algorithm in
2363 	 * useconds.
2364 	 */
2365 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2366 		union tcp_log_stackspecific log;
2367 		struct timeval tv;
2368 
2369 		/* Convert our ms to a microsecond */
2370 		memset(&log, 0, sizeof(log));
2371 		log.u_bbr.flex1 = rtt;
2372 		log.u_bbr.flex2 = rack->r_ctl.ack_count;
2373 		log.u_bbr.flex3 = rack->r_ctl.sack_count;
2374 		log.u_bbr.flex4 = rack->r_ctl.sack_noextra_move;
2375 		log.u_bbr.flex5 = rack->r_ctl.sack_moved_extra;
2376 		log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
2377 		log.u_bbr.flex7 = 1;
2378 		log.u_bbr.flex8 = rack->sack_attack_disable;
2379 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2380 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2381 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2382 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2383 		log.u_bbr.pacing_gain = rack->r_must_retran;
2384 		/*
2385 		 * We capture in delRate the upper 32 bits as
2386 		 * the confidence level we had declared, and the
2387 		 * lower 32 bits as the actual RTT using the arrival
2388 		 * timestamp.
2389 		 */
2390 		log.u_bbr.delRate = rack->r_ctl.rack_rs.confidence;
2391 		log.u_bbr.delRate <<= 32;
2392 		log.u_bbr.delRate |= rack->r_ctl.rack_rs.rs_us_rtt;
2393 		/* Lets capture all the things that make up t_rtxcur */
2394 		log.u_bbr.applimited = rack_rto_min;
2395 		log.u_bbr.epoch = rack_rto_max;
2396 		log.u_bbr.lt_epoch = rack->r_ctl.timer_slop;
2397 		log.u_bbr.lost = rack_rto_min;
2398 		log.u_bbr.pkt_epoch = TICKS_2_USEC(tcp_rexmit_slop);
2399 		log.u_bbr.rttProp = RACK_REXMTVAL(rack->rc_tp);
2400 		log.u_bbr.bw_inuse = rack->r_ctl.act_rcv_time.tv_sec;
2401 		log.u_bbr.bw_inuse *= HPTS_USEC_IN_SEC;
2402 		log.u_bbr.bw_inuse += rack->r_ctl.act_rcv_time.tv_usec;
2403 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2404 		    &rack->rc_inp->inp_socket->so_rcv,
2405 		    &rack->rc_inp->inp_socket->so_snd,
2406 		    TCP_LOG_RTT, 0,
2407 		    0, &log, false, &tv);
2408 	}
2409 }
2410 
2411 static void
2412 rack_log_rtt_sample_calc(struct tcp_rack *rack, uint32_t rtt, uint32_t send_time, uint32_t ack_time, int where)
2413 {
2414 	if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2415 		union tcp_log_stackspecific log;
2416 		struct timeval tv;
2417 
2418 		/* Convert our ms to a microsecond */
2419 		memset(&log, 0, sizeof(log));
2420 		log.u_bbr.flex1 = rtt;
2421 		log.u_bbr.flex2 = send_time;
2422 		log.u_bbr.flex3 = ack_time;
2423 		log.u_bbr.flex4 = where;
2424 		log.u_bbr.flex7 = 2;
2425 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2426 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2427 		    &rack->rc_inp->inp_socket->so_rcv,
2428 		    &rack->rc_inp->inp_socket->so_snd,
2429 		    TCP_LOG_RTT, 0,
2430 		    0, &log, false, &tv);
2431 	}
2432 }
2433 
2434 
2435 
2436 static inline void
2437 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick,  int event, int line)
2438 {
2439 	if (rack_verbose_logging && (tp->t_logstate != TCP_LOG_STATE_OFF)) {
2440 		union tcp_log_stackspecific log;
2441 		struct timeval tv;
2442 
2443 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2444 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2445 		log.u_bbr.flex1 = line;
2446 		log.u_bbr.flex2 = tick;
2447 		log.u_bbr.flex3 = tp->t_maxunacktime;
2448 		log.u_bbr.flex4 = tp->t_acktime;
2449 		log.u_bbr.flex8 = event;
2450 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2451 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2452 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2453 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2454 		log.u_bbr.pacing_gain = rack->r_must_retran;
2455 		TCP_LOG_EVENTP(tp, NULL,
2456 		    &rack->rc_inp->inp_socket->so_rcv,
2457 		    &rack->rc_inp->inp_socket->so_snd,
2458 		    BBR_LOG_PROGRESS, 0,
2459 		    0, &log, false, &tv);
2460 	}
2461 }
2462 
2463 static void
2464 rack_log_type_bbrsnd(struct tcp_rack *rack, uint32_t len, uint32_t slot, uint32_t cts, struct timeval *tv)
2465 {
2466 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2467 		union tcp_log_stackspecific log;
2468 
2469 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2470 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2471 		log.u_bbr.flex1 = slot;
2472 		if (rack->rack_no_prr)
2473 			log.u_bbr.flex2 = 0;
2474 		else
2475 			log.u_bbr.flex2 = rack->r_ctl.rc_prr_sndcnt;
2476 		log.u_bbr.flex7 = (0x0000ffff & rack->r_ctl.rc_hpts_flags);
2477 		log.u_bbr.flex8 = rack->rc_in_persist;
2478 		log.u_bbr.timeStamp = cts;
2479 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2480 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2481 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2482 		log.u_bbr.pacing_gain = rack->r_must_retran;
2483 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2484 		    &rack->rc_inp->inp_socket->so_rcv,
2485 		    &rack->rc_inp->inp_socket->so_snd,
2486 		    BBR_LOG_BBRSND, 0,
2487 		    0, &log, false, tv);
2488 	}
2489 }
2490 
2491 static void
2492 rack_log_doseg_done(struct tcp_rack *rack, uint32_t cts, int32_t nxt_pkt, int32_t did_out, int way_out, int nsegs)
2493 {
2494 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2495 		union tcp_log_stackspecific log;
2496 		struct timeval tv;
2497 
2498 		memset(&log, 0, sizeof(log));
2499 		log.u_bbr.flex1 = did_out;
2500 		log.u_bbr.flex2 = nxt_pkt;
2501 		log.u_bbr.flex3 = way_out;
2502 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
2503 		if (rack->rack_no_prr)
2504 			log.u_bbr.flex5 = 0;
2505 		else
2506 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
2507 		log.u_bbr.flex6 = nsegs;
2508 		log.u_bbr.applimited = rack->r_ctl.rc_pace_min_segs;
2509 		log.u_bbr.flex7 = rack->rc_ack_can_sendout_data;	/* Do we have ack-can-send set */
2510 		log.u_bbr.flex7 <<= 1;
2511 		log.u_bbr.flex7 |= rack->r_fast_output;	/* is fast output primed */
2512 		log.u_bbr.flex7 <<= 1;
2513 		log.u_bbr.flex7 |= rack->r_wanted_output;	/* Do we want output */
2514 		log.u_bbr.flex8 = rack->rc_in_persist;
2515 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2516 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2517 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2518 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
2519 		log.u_bbr.use_lt_bw <<= 1;
2520 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
2521 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2522 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2523 		log.u_bbr.pacing_gain = rack->r_must_retran;
2524 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2525 		    &rack->rc_inp->inp_socket->so_rcv,
2526 		    &rack->rc_inp->inp_socket->so_snd,
2527 		    BBR_LOG_DOSEG_DONE, 0,
2528 		    0, &log, false, &tv);
2529 	}
2530 }
2531 
2532 static void
2533 rack_log_type_pacing_sizes(struct tcpcb *tp, struct tcp_rack *rack, uint32_t arg1, uint32_t arg2, uint32_t arg3, uint8_t frm)
2534 {
2535 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
2536 		union tcp_log_stackspecific log;
2537 		struct timeval tv;
2538 
2539 		memset(&log, 0, sizeof(log));
2540 		log.u_bbr.flex1 = rack->r_ctl.rc_pace_min_segs;
2541 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
2542 		log.u_bbr.flex4 = arg1;
2543 		log.u_bbr.flex5 = arg2;
2544 		log.u_bbr.flex6 = arg3;
2545 		log.u_bbr.flex8 = frm;
2546 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2547 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2548 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2549 		log.u_bbr.applimited = rack->r_ctl.rc_sacked;
2550 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2551 		log.u_bbr.pacing_gain = rack->r_must_retran;
2552 		TCP_LOG_EVENTP(tp, NULL,
2553 		    &tp->t_inpcb->inp_socket->so_rcv,
2554 		    &tp->t_inpcb->inp_socket->so_snd,
2555 		    TCP_HDWR_PACE_SIZE, 0,
2556 		    0, &log, false, &tv);
2557 	}
2558 }
2559 
2560 static void
2561 rack_log_type_just_return(struct tcp_rack *rack, uint32_t cts, uint32_t tlen, uint32_t slot,
2562 			  uint8_t hpts_calling, int reason, uint32_t cwnd_to_use)
2563 {
2564 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2565 		union tcp_log_stackspecific log;
2566 		struct timeval tv;
2567 
2568 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2569 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2570 		log.u_bbr.flex1 = slot;
2571 		log.u_bbr.flex2 = rack->r_ctl.rc_hpts_flags;
2572 		log.u_bbr.flex4 = reason;
2573 		if (rack->rack_no_prr)
2574 			log.u_bbr.flex5 = 0;
2575 		else
2576 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
2577 		log.u_bbr.flex7 = hpts_calling;
2578 		log.u_bbr.flex8 = rack->rc_in_persist;
2579 		log.u_bbr.lt_epoch = cwnd_to_use;
2580 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2581 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2582 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2583 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2584 		log.u_bbr.pacing_gain = rack->r_must_retran;
2585 		log.u_bbr.cwnd_gain = rack->rc_has_collapsed;
2586 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2587 		    &rack->rc_inp->inp_socket->so_rcv,
2588 		    &rack->rc_inp->inp_socket->so_snd,
2589 		    BBR_LOG_JUSTRET, 0,
2590 		    tlen, &log, false, &tv);
2591 	}
2592 }
2593 
2594 static void
2595 rack_log_to_cancel(struct tcp_rack *rack, int32_t hpts_removed, int line, uint32_t us_cts,
2596 		   struct timeval *tv, uint32_t flags_on_entry)
2597 {
2598 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2599 		union tcp_log_stackspecific log;
2600 
2601 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2602 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
2603 		log.u_bbr.flex1 = line;
2604 		log.u_bbr.flex2 = rack->r_ctl.rc_last_output_to;
2605 		log.u_bbr.flex3 = flags_on_entry;
2606 		log.u_bbr.flex4 = us_cts;
2607 		if (rack->rack_no_prr)
2608 			log.u_bbr.flex5 = 0;
2609 		else
2610 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
2611 		log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
2612 		log.u_bbr.flex7 = hpts_removed;
2613 		log.u_bbr.flex8 = 1;
2614 		log.u_bbr.applimited = rack->r_ctl.rc_hpts_flags;
2615 		log.u_bbr.timeStamp = us_cts;
2616 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2617 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2618 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2619 		log.u_bbr.pacing_gain = rack->r_must_retran;
2620 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2621 		    &rack->rc_inp->inp_socket->so_rcv,
2622 		    &rack->rc_inp->inp_socket->so_snd,
2623 		    BBR_LOG_TIMERCANC, 0,
2624 		    0, &log, false, tv);
2625 	}
2626 }
2627 
2628 static void
2629 rack_log_alt_to_to_cancel(struct tcp_rack *rack,
2630 			  uint32_t flex1, uint32_t flex2,
2631 			  uint32_t flex3, uint32_t flex4,
2632 			  uint32_t flex5, uint32_t flex6,
2633 			  uint16_t flex7, uint8_t mod)
2634 {
2635 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2636 		union tcp_log_stackspecific log;
2637 		struct timeval tv;
2638 
2639 		if (mod == 1) {
2640 			/* No you can't use 1, its for the real to cancel */
2641 			return;
2642 		}
2643 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2644 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2645 		log.u_bbr.flex1 = flex1;
2646 		log.u_bbr.flex2 = flex2;
2647 		log.u_bbr.flex3 = flex3;
2648 		log.u_bbr.flex4 = flex4;
2649 		log.u_bbr.flex5 = flex5;
2650 		log.u_bbr.flex6 = flex6;
2651 		log.u_bbr.flex7 = flex7;
2652 		log.u_bbr.flex8 = mod;
2653 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2654 		    &rack->rc_inp->inp_socket->so_rcv,
2655 		    &rack->rc_inp->inp_socket->so_snd,
2656 		    BBR_LOG_TIMERCANC, 0,
2657 		    0, &log, false, &tv);
2658 	}
2659 }
2660 
2661 static void
2662 rack_log_to_processing(struct tcp_rack *rack, uint32_t cts, int32_t ret, int32_t timers)
2663 {
2664 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2665 		union tcp_log_stackspecific log;
2666 		struct timeval tv;
2667 
2668 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2669 		log.u_bbr.flex1 = timers;
2670 		log.u_bbr.flex2 = ret;
2671 		log.u_bbr.flex3 = rack->r_ctl.rc_timer_exp;
2672 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
2673 		log.u_bbr.flex5 = cts;
2674 		if (rack->rack_no_prr)
2675 			log.u_bbr.flex6 = 0;
2676 		else
2677 			log.u_bbr.flex6 = rack->r_ctl.rc_prr_sndcnt;
2678 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2679 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2680 		log.u_bbr.pacing_gain = rack->r_must_retran;
2681 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2682 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2683 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2684 		    &rack->rc_inp->inp_socket->so_rcv,
2685 		    &rack->rc_inp->inp_socket->so_snd,
2686 		    BBR_LOG_TO_PROCESS, 0,
2687 		    0, &log, false, &tv);
2688 	}
2689 }
2690 
2691 static void
2692 rack_log_to_prr(struct tcp_rack *rack, int frm, int orig_cwnd, int line)
2693 {
2694 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2695 		union tcp_log_stackspecific log;
2696 		struct timeval tv;
2697 
2698 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2699 		log.u_bbr.flex1 = rack->r_ctl.rc_prr_out;
2700 		log.u_bbr.flex2 = rack->r_ctl.rc_prr_recovery_fs;
2701 		if (rack->rack_no_prr)
2702 			log.u_bbr.flex3 = 0;
2703 		else
2704 			log.u_bbr.flex3 = rack->r_ctl.rc_prr_sndcnt;
2705 		log.u_bbr.flex4 = rack->r_ctl.rc_prr_delivered;
2706 		log.u_bbr.flex5 = rack->r_ctl.rc_sacked;
2707 		log.u_bbr.flex6 = rack->r_ctl.rc_holes_rxt;
2708 		log.u_bbr.flex7 = line;
2709 		log.u_bbr.flex8 = frm;
2710 		log.u_bbr.pkts_out = orig_cwnd;
2711 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2712 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2713 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
2714 		log.u_bbr.use_lt_bw <<= 1;
2715 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
2716 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2717 		    &rack->rc_inp->inp_socket->so_rcv,
2718 		    &rack->rc_inp->inp_socket->so_snd,
2719 		    BBR_LOG_BBRUPD, 0,
2720 		    0, &log, false, &tv);
2721 	}
2722 }
2723 
2724 #ifdef NETFLIX_EXP_DETECTION
2725 static void
2726 rack_log_sad(struct tcp_rack *rack, int event)
2727 {
2728 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2729 		union tcp_log_stackspecific log;
2730 		struct timeval tv;
2731 
2732 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2733 		log.u_bbr.flex1 = rack->r_ctl.sack_count;
2734 		log.u_bbr.flex2 = rack->r_ctl.ack_count;
2735 		log.u_bbr.flex3 = rack->r_ctl.sack_moved_extra;
2736 		log.u_bbr.flex4 = rack->r_ctl.sack_noextra_move;
2737 		log.u_bbr.flex5 = rack->r_ctl.rc_num_maps_alloced;
2738 		log.u_bbr.flex6 = tcp_sack_to_ack_thresh;
2739 		log.u_bbr.pkts_out = tcp_sack_to_move_thresh;
2740 		log.u_bbr.lt_epoch = (tcp_force_detection << 8);
2741 		log.u_bbr.lt_epoch |= rack->do_detection;
2742 		log.u_bbr.applimited = tcp_map_minimum;
2743 		log.u_bbr.flex7 = rack->sack_attack_disable;
2744 		log.u_bbr.flex8 = event;
2745 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2746 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2747 		log.u_bbr.delivered = tcp_sad_decay_val;
2748 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2749 		    &rack->rc_inp->inp_socket->so_rcv,
2750 		    &rack->rc_inp->inp_socket->so_snd,
2751 		    TCP_SAD_DETECTION, 0,
2752 		    0, &log, false, &tv);
2753 	}
2754 }
2755 #endif
2756 
2757 static void
2758 rack_counter_destroy(void)
2759 {
2760 	counter_u64_free(rack_fto_send);
2761 	counter_u64_free(rack_fto_rsm_send);
2762 	counter_u64_free(rack_nfto_resend);
2763 	counter_u64_free(rack_hw_pace_init_fail);
2764 	counter_u64_free(rack_hw_pace_lost);
2765 	counter_u64_free(rack_non_fto_send);
2766 	counter_u64_free(rack_extended_rfo);
2767 	counter_u64_free(rack_ack_total);
2768 	counter_u64_free(rack_express_sack);
2769 	counter_u64_free(rack_sack_total);
2770 	counter_u64_free(rack_move_none);
2771 	counter_u64_free(rack_move_some);
2772 	counter_u64_free(rack_sack_attacks_detected);
2773 	counter_u64_free(rack_sack_attacks_reversed);
2774 	counter_u64_free(rack_sack_used_next_merge);
2775 	counter_u64_free(rack_sack_used_prev_merge);
2776 	counter_u64_free(rack_tlp_tot);
2777 	counter_u64_free(rack_tlp_newdata);
2778 	counter_u64_free(rack_tlp_retran);
2779 	counter_u64_free(rack_tlp_retran_bytes);
2780 	counter_u64_free(rack_to_tot);
2781 	counter_u64_free(rack_saw_enobuf);
2782 	counter_u64_free(rack_saw_enobuf_hw);
2783 	counter_u64_free(rack_saw_enetunreach);
2784 	counter_u64_free(rack_hot_alloc);
2785 	counter_u64_free(rack_to_alloc);
2786 	counter_u64_free(rack_to_alloc_hard);
2787 	counter_u64_free(rack_to_alloc_emerg);
2788 	counter_u64_free(rack_to_alloc_limited);
2789 	counter_u64_free(rack_alloc_limited_conns);
2790 	counter_u64_free(rack_split_limited);
2791 	counter_u64_free(rack_multi_single_eq);
2792 	counter_u64_free(rack_proc_non_comp_ack);
2793 	counter_u64_free(rack_sack_proc_all);
2794 	counter_u64_free(rack_sack_proc_restart);
2795 	counter_u64_free(rack_sack_proc_short);
2796 	counter_u64_free(rack_sack_skipped_acked);
2797 	counter_u64_free(rack_sack_splits);
2798 	counter_u64_free(rack_input_idle_reduces);
2799 	counter_u64_free(rack_collapsed_win);
2800 	counter_u64_free(rack_collapsed_win_rxt);
2801 	counter_u64_free(rack_collapsed_win_rxt_bytes);
2802 	counter_u64_free(rack_collapsed_win_seen);
2803 	counter_u64_free(rack_try_scwnd);
2804 	counter_u64_free(rack_persists_sends);
2805 	counter_u64_free(rack_persists_acks);
2806 	counter_u64_free(rack_persists_loss);
2807 	counter_u64_free(rack_persists_lost_ends);
2808 #ifdef INVARIANTS
2809 	counter_u64_free(rack_adjust_map_bw);
2810 #endif
2811 	COUNTER_ARRAY_FREE(rack_out_size, TCP_MSS_ACCT_SIZE);
2812 	COUNTER_ARRAY_FREE(rack_opts_arry, RACK_OPTS_SIZE);
2813 }
2814 
2815 static struct rack_sendmap *
2816 rack_alloc(struct tcp_rack *rack)
2817 {
2818 	struct rack_sendmap *rsm;
2819 
2820 	/*
2821 	 * First get the top of the list it in
2822 	 * theory is the "hottest" rsm we have,
2823 	 * possibly just freed by ack processing.
2824 	 */
2825 	if (rack->rc_free_cnt > rack_free_cache) {
2826 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
2827 		TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
2828 		counter_u64_add(rack_hot_alloc, 1);
2829 		rack->rc_free_cnt--;
2830 		return (rsm);
2831 	}
2832 	/*
2833 	 * Once we get under our free cache we probably
2834 	 * no longer have a "hot" one available. Lets
2835 	 * get one from UMA.
2836 	 */
2837 	rsm = uma_zalloc(rack_zone, M_NOWAIT);
2838 	if (rsm) {
2839 		rack->r_ctl.rc_num_maps_alloced++;
2840 		counter_u64_add(rack_to_alloc, 1);
2841 		return (rsm);
2842 	}
2843 	/*
2844 	 * Dig in to our aux rsm's (the last two) since
2845 	 * UMA failed to get us one.
2846 	 */
2847 	if (rack->rc_free_cnt) {
2848 		counter_u64_add(rack_to_alloc_emerg, 1);
2849 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
2850 		TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
2851 		rack->rc_free_cnt--;
2852 		return (rsm);
2853 	}
2854 	return (NULL);
2855 }
2856 
2857 static struct rack_sendmap *
2858 rack_alloc_full_limit(struct tcp_rack *rack)
2859 {
2860 	if ((V_tcp_map_entries_limit > 0) &&
2861 	    (rack->do_detection == 0) &&
2862 	    (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
2863 		counter_u64_add(rack_to_alloc_limited, 1);
2864 		if (!rack->alloc_limit_reported) {
2865 			rack->alloc_limit_reported = 1;
2866 			counter_u64_add(rack_alloc_limited_conns, 1);
2867 		}
2868 		return (NULL);
2869 	}
2870 	return (rack_alloc(rack));
2871 }
2872 
2873 /* wrapper to allocate a sendmap entry, subject to a specific limit */
2874 static struct rack_sendmap *
2875 rack_alloc_limit(struct tcp_rack *rack, uint8_t limit_type)
2876 {
2877 	struct rack_sendmap *rsm;
2878 
2879 	if (limit_type) {
2880 		/* currently there is only one limit type */
2881 		if (V_tcp_map_split_limit > 0 &&
2882 		    (rack->do_detection == 0) &&
2883 		    rack->r_ctl.rc_num_split_allocs >= V_tcp_map_split_limit) {
2884 			counter_u64_add(rack_split_limited, 1);
2885 			if (!rack->alloc_limit_reported) {
2886 				rack->alloc_limit_reported = 1;
2887 				counter_u64_add(rack_alloc_limited_conns, 1);
2888 			}
2889 			return (NULL);
2890 		}
2891 	}
2892 
2893 	/* allocate and mark in the limit type, if set */
2894 	rsm = rack_alloc(rack);
2895 	if (rsm != NULL && limit_type) {
2896 		rsm->r_limit_type = limit_type;
2897 		rack->r_ctl.rc_num_split_allocs++;
2898 	}
2899 	return (rsm);
2900 }
2901 
2902 static void
2903 rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm)
2904 {
2905 	if (rsm->r_flags & RACK_APP_LIMITED) {
2906 		if (rack->r_ctl.rc_app_limited_cnt > 0) {
2907 			rack->r_ctl.rc_app_limited_cnt--;
2908 		}
2909 	}
2910 	if (rsm->r_limit_type) {
2911 		/* currently there is only one limit type */
2912 		rack->r_ctl.rc_num_split_allocs--;
2913 	}
2914 	if (rsm == rack->r_ctl.rc_first_appl) {
2915 		if (rack->r_ctl.rc_app_limited_cnt == 0)
2916 			rack->r_ctl.rc_first_appl = NULL;
2917 		else {
2918 			/* Follow the next one out */
2919 			struct rack_sendmap fe;
2920 
2921 			fe.r_start = rsm->r_nseq_appl;
2922 			rack->r_ctl.rc_first_appl = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
2923 		}
2924 	}
2925 	if (rsm == rack->r_ctl.rc_resend)
2926 		rack->r_ctl.rc_resend = NULL;
2927 	if (rsm == rack->r_ctl.rc_end_appl)
2928 		rack->r_ctl.rc_end_appl = NULL;
2929 	if (rack->r_ctl.rc_tlpsend == rsm)
2930 		rack->r_ctl.rc_tlpsend = NULL;
2931 	if (rack->r_ctl.rc_sacklast == rsm)
2932 		rack->r_ctl.rc_sacklast = NULL;
2933 	memset(rsm, 0, sizeof(struct rack_sendmap));
2934 	TAILQ_INSERT_HEAD(&rack->r_ctl.rc_free, rsm, r_tnext);
2935 	rack->rc_free_cnt++;
2936 }
2937 
2938 static void
2939 rack_free_trim(struct tcp_rack *rack)
2940 {
2941 	struct rack_sendmap *rsm;
2942 
2943 	/*
2944 	 * Free up all the tail entries until
2945 	 * we get our list down to the limit.
2946 	 */
2947 	while (rack->rc_free_cnt > rack_free_cache) {
2948 		rsm = TAILQ_LAST(&rack->r_ctl.rc_free, rack_head);
2949 		TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
2950 		rack->rc_free_cnt--;
2951 		uma_zfree(rack_zone, rsm);
2952 	}
2953 }
2954 
2955 
2956 static uint32_t
2957 rack_get_measure_window(struct tcpcb *tp, struct tcp_rack *rack)
2958 {
2959 	uint64_t srtt, bw, len, tim;
2960 	uint32_t segsiz, def_len, minl;
2961 
2962 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
2963 	def_len = rack_def_data_window * segsiz;
2964 	if (rack->rc_gp_filled == 0) {
2965 		/*
2966 		 * We have no measurement (IW is in flight?) so
2967 		 * we can only guess using our data_window sysctl
2968 		 * value (usually 20MSS).
2969 		 */
2970 		return (def_len);
2971 	}
2972 	/*
2973 	 * Now we have a number of factors to consider.
2974 	 *
2975 	 * 1) We have a desired BDP which is usually
2976 	 *    at least 2.
2977 	 * 2) We have a minimum number of rtt's usually 1 SRTT
2978 	 *    but we allow it too to be more.
2979 	 * 3) We want to make sure a measurement last N useconds (if
2980 	 *    we have set rack_min_measure_usec.
2981 	 *
2982 	 * We handle the first concern here by trying to create a data
2983 	 * window of max(rack_def_data_window, DesiredBDP). The
2984 	 * second concern we handle in not letting the measurement
2985 	 * window end normally until at least the required SRTT's
2986 	 * have gone by which is done further below in
2987 	 * rack_enough_for_measurement(). Finally the third concern
2988 	 * we also handle here by calculating how long that time
2989 	 * would take at the current BW and then return the
2990 	 * max of our first calculation and that length. Note
2991 	 * that if rack_min_measure_usec is 0, we don't deal
2992 	 * with concern 3. Also for both Concern 1 and 3 an
2993 	 * application limited period could end the measurement
2994 	 * earlier.
2995 	 *
2996 	 * So lets calculate the BDP with the "known" b/w using
2997 	 * the SRTT has our rtt and then multiply it by the
2998 	 * goal.
2999 	 */
3000 	bw = rack_get_bw(rack);
3001 	srtt = (uint64_t)tp->t_srtt;
3002 	len = bw * srtt;
3003 	len /= (uint64_t)HPTS_USEC_IN_SEC;
3004 	len *= max(1, rack_goal_bdp);
3005 	/* Now we need to round up to the nearest MSS */
3006 	len = roundup(len, segsiz);
3007 	if (rack_min_measure_usec) {
3008 		/* Now calculate our min length for this b/w */
3009 		tim = rack_min_measure_usec;
3010 		minl = (tim * bw) / (uint64_t)HPTS_USEC_IN_SEC;
3011 		if (minl == 0)
3012 			minl = 1;
3013 		minl = roundup(minl, segsiz);
3014 		if (len < minl)
3015 			len = minl;
3016 	}
3017 	/*
3018 	 * Now if we have a very small window we want
3019 	 * to attempt to get the window that is
3020 	 * as small as possible. This happens on
3021 	 * low b/w connections and we don't want to
3022 	 * span huge numbers of rtt's between measurements.
3023 	 *
3024 	 * We basically include 2 over our "MIN window" so
3025 	 * that the measurement can be shortened (possibly) by
3026 	 * an ack'ed packet.
3027 	 */
3028 	if (len < def_len)
3029 		return (max((uint32_t)len, ((MIN_GP_WIN+2) * segsiz)));
3030 	else
3031 		return (max((uint32_t)len, def_len));
3032 
3033 }
3034 
3035 static int
3036 rack_enough_for_measurement(struct tcpcb *tp, struct tcp_rack *rack, tcp_seq th_ack, uint8_t *quality)
3037 {
3038 	uint32_t tim, srtts, segsiz;
3039 
3040 	/*
3041 	 * Has enough time passed for the GP measurement to be valid?
3042 	 */
3043 	if ((tp->snd_max == tp->snd_una) ||
3044 	    (th_ack == tp->snd_max)){
3045 		/* All is acked */
3046 		*quality = RACK_QUALITY_ALLACKED;
3047 		return (1);
3048 	}
3049 	if (SEQ_LT(th_ack, tp->gput_seq)) {
3050 		/* Not enough bytes yet */
3051 		return (0);
3052 	}
3053 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
3054 	if (SEQ_LT(th_ack, tp->gput_ack) &&
3055 	    ((th_ack - tp->gput_seq) < max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) {
3056 		/* Not enough bytes yet */
3057 		return (0);
3058 	}
3059 	if (rack->r_ctl.rc_first_appl &&
3060 	    (SEQ_GEQ(th_ack, rack->r_ctl.rc_first_appl->r_end))) {
3061 		/*
3062 		 * We are up to the app limited send point
3063 		 * we have to measure irrespective of the time..
3064 		 */
3065 		*quality = RACK_QUALITY_APPLIMITED;
3066 		return (1);
3067 	}
3068 	/* Now what about time? */
3069 	srtts = (rack->r_ctl.rc_gp_srtt * rack_min_srtts);
3070 	tim = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - tp->gput_ts;
3071 	if (tim >= srtts) {
3072 		*quality = RACK_QUALITY_HIGH;
3073 		return (1);
3074 	}
3075 	/* Nope not even a full SRTT has passed */
3076 	return (0);
3077 }
3078 
3079 static void
3080 rack_log_timely(struct tcp_rack *rack,
3081 		uint32_t logged, uint64_t cur_bw, uint64_t low_bnd,
3082 		uint64_t up_bnd, int line, uint8_t method)
3083 {
3084 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
3085 		union tcp_log_stackspecific log;
3086 		struct timeval tv;
3087 
3088 		memset(&log, 0, sizeof(log));
3089 		log.u_bbr.flex1 = logged;
3090 		log.u_bbr.flex2 = rack->rc_gp_timely_inc_cnt;
3091 		log.u_bbr.flex2 <<= 4;
3092 		log.u_bbr.flex2 |= rack->rc_gp_timely_dec_cnt;
3093 		log.u_bbr.flex2 <<= 4;
3094 		log.u_bbr.flex2 |= rack->rc_gp_incr;
3095 		log.u_bbr.flex2 <<= 4;
3096 		log.u_bbr.flex2 |= rack->rc_gp_bwred;
3097 		log.u_bbr.flex3 = rack->rc_gp_incr;
3098 		log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss;
3099 		log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ca;
3100 		log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_rec;
3101 		log.u_bbr.flex7 = rack->rc_gp_bwred;
3102 		log.u_bbr.flex8 = method;
3103 		log.u_bbr.cur_del_rate = cur_bw;
3104 		log.u_bbr.delRate = low_bnd;
3105 		log.u_bbr.bw_inuse = up_bnd;
3106 		log.u_bbr.rttProp = rack_get_bw(rack);
3107 		log.u_bbr.pkt_epoch = line;
3108 		log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff;
3109 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3110 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3111 		log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt;
3112 		log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt;
3113 		log.u_bbr.cwnd_gain = rack->rc_dragged_bottom;
3114 		log.u_bbr.cwnd_gain <<= 1;
3115 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_rec;
3116 		log.u_bbr.cwnd_gain <<= 1;
3117 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss;
3118 		log.u_bbr.cwnd_gain <<= 1;
3119 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca;
3120 		log.u_bbr.lost = rack->r_ctl.rc_loss_count;
3121 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3122 		    &rack->rc_inp->inp_socket->so_rcv,
3123 		    &rack->rc_inp->inp_socket->so_snd,
3124 		    TCP_TIMELY_WORK, 0,
3125 		    0, &log, false, &tv);
3126 	}
3127 }
3128 
3129 static int
3130 rack_bw_can_be_raised(struct tcp_rack *rack, uint64_t cur_bw, uint64_t last_bw_est, uint16_t mult)
3131 {
3132 	/*
3133 	 * Before we increase we need to know if
3134 	 * the estimate just made was less than
3135 	 * our pacing goal (i.e. (cur_bw * mult) > last_bw_est)
3136 	 *
3137 	 * If we already are pacing at a fast enough
3138 	 * rate to push us faster there is no sense of
3139 	 * increasing.
3140 	 *
3141 	 * We first caculate our actual pacing rate (ss or ca multiplier
3142 	 * times our cur_bw).
3143 	 *
3144 	 * Then we take the last measured rate and multipy by our
3145 	 * maximum pacing overage to give us a max allowable rate.
3146 	 *
3147 	 * If our act_rate is smaller than our max_allowable rate
3148 	 * then we should increase. Else we should hold steady.
3149 	 *
3150 	 */
3151 	uint64_t act_rate, max_allow_rate;
3152 
3153 	if (rack_timely_no_stopping)
3154 		return (1);
3155 
3156 	if ((cur_bw == 0) || (last_bw_est == 0)) {
3157 		/*
3158 		 * Initial startup case or
3159 		 * everything is acked case.
3160 		 */
3161 		rack_log_timely(rack,  mult, cur_bw, 0, 0,
3162 				__LINE__, 9);
3163 		return (1);
3164 	}
3165 	if (mult <= 100) {
3166 		/*
3167 		 * We can always pace at or slightly above our rate.
3168 		 */
3169 		rack_log_timely(rack,  mult, cur_bw, 0, 0,
3170 				__LINE__, 9);
3171 		return (1);
3172 	}
3173 	act_rate = cur_bw * (uint64_t)mult;
3174 	act_rate /= 100;
3175 	max_allow_rate = last_bw_est * ((uint64_t)rack_max_per_above + (uint64_t)100);
3176 	max_allow_rate /= 100;
3177 	if (act_rate < max_allow_rate) {
3178 		/*
3179 		 * Here the rate we are actually pacing at
3180 		 * is smaller than 10% above our last measurement.
3181 		 * This means we are pacing below what we would
3182 		 * like to try to achieve (plus some wiggle room).
3183 		 */
3184 		rack_log_timely(rack,  mult, cur_bw, act_rate, max_allow_rate,
3185 				__LINE__, 9);
3186 		return (1);
3187 	} else {
3188 		/*
3189 		 * Here we are already pacing at least rack_max_per_above(10%)
3190 		 * what we are getting back. This indicates most likely
3191 		 * that we are being limited (cwnd/rwnd/app) and can't
3192 		 * get any more b/w. There is no sense of trying to
3193 		 * raise up the pacing rate its not speeding us up
3194 		 * and we already are pacing faster than we are getting.
3195 		 */
3196 		rack_log_timely(rack,  mult, cur_bw, act_rate, max_allow_rate,
3197 				__LINE__, 8);
3198 		return (0);
3199 	}
3200 }
3201 
3202 static void
3203 rack_validate_multipliers_at_or_above100(struct tcp_rack *rack)
3204 {
3205 	/*
3206 	 * When we drag bottom, we want to assure
3207 	 * that no multiplier is below 1.0, if so
3208 	 * we want to restore it to at least that.
3209 	 */
3210 	if (rack->r_ctl.rack_per_of_gp_rec  < 100) {
3211 		/* This is unlikely we usually do not touch recovery */
3212 		rack->r_ctl.rack_per_of_gp_rec = 100;
3213 	}
3214 	if (rack->r_ctl.rack_per_of_gp_ca < 100) {
3215 		rack->r_ctl.rack_per_of_gp_ca = 100;
3216 	}
3217 	if (rack->r_ctl.rack_per_of_gp_ss < 100) {
3218 		rack->r_ctl.rack_per_of_gp_ss = 100;
3219 	}
3220 }
3221 
3222 static void
3223 rack_validate_multipliers_at_or_below_100(struct tcp_rack *rack)
3224 {
3225 	if (rack->r_ctl.rack_per_of_gp_ca > 100) {
3226 		rack->r_ctl.rack_per_of_gp_ca = 100;
3227 	}
3228 	if (rack->r_ctl.rack_per_of_gp_ss > 100) {
3229 		rack->r_ctl.rack_per_of_gp_ss = 100;
3230 	}
3231 }
3232 
3233 static void
3234 rack_increase_bw_mul(struct tcp_rack *rack, int timely_says, uint64_t cur_bw, uint64_t last_bw_est, int override)
3235 {
3236 	int32_t  calc, logged, plus;
3237 
3238 	logged = 0;
3239 
3240 	if (override) {
3241 		/*
3242 		 * override is passed when we are
3243 		 * loosing b/w and making one last
3244 		 * gasp at trying to not loose out
3245 		 * to a new-reno flow.
3246 		 */
3247 		goto extra_boost;
3248 	}
3249 	/* In classic timely we boost by 5x if we have 5 increases in a row, lets not */
3250 	if (rack->rc_gp_incr &&
3251 	    ((rack->rc_gp_timely_inc_cnt + 1) >= RACK_TIMELY_CNT_BOOST)) {
3252 		/*
3253 		 * Reset and get 5 strokes more before the boost. Note
3254 		 * that the count is 0 based so we have to add one.
3255 		 */
3256 extra_boost:
3257 		plus = (uint32_t)rack_gp_increase_per * RACK_TIMELY_CNT_BOOST;
3258 		rack->rc_gp_timely_inc_cnt = 0;
3259 	} else
3260 		plus = (uint32_t)rack_gp_increase_per;
3261 	/* Must be at least 1% increase for true timely increases */
3262 	if ((plus < 1) &&
3263 	    ((rack->r_ctl.rc_rtt_diff <= 0) || (timely_says <= 0)))
3264 		plus = 1;
3265 	if (rack->rc_gp_saw_rec &&
3266 	    (rack->rc_gp_no_rec_chg == 0) &&
3267 	    rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3268 				  rack->r_ctl.rack_per_of_gp_rec)) {
3269 		/* We have been in recovery ding it too */
3270 		calc = rack->r_ctl.rack_per_of_gp_rec + plus;
3271 		if (calc > 0xffff)
3272 			calc = 0xffff;
3273 		logged |= 1;
3274 		rack->r_ctl.rack_per_of_gp_rec = (uint16_t)calc;
3275 		if (rack_per_upper_bound_ss &&
3276 		    (rack->rc_dragged_bottom == 0) &&
3277 		    (rack->r_ctl.rack_per_of_gp_rec > rack_per_upper_bound_ss))
3278 			rack->r_ctl.rack_per_of_gp_rec = rack_per_upper_bound_ss;
3279 	}
3280 	if (rack->rc_gp_saw_ca &&
3281 	    (rack->rc_gp_saw_ss == 0) &&
3282 	    rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3283 				  rack->r_ctl.rack_per_of_gp_ca)) {
3284 		/* In CA */
3285 		calc = rack->r_ctl.rack_per_of_gp_ca + plus;
3286 		if (calc > 0xffff)
3287 			calc = 0xffff;
3288 		logged |= 2;
3289 		rack->r_ctl.rack_per_of_gp_ca = (uint16_t)calc;
3290 		if (rack_per_upper_bound_ca &&
3291 		    (rack->rc_dragged_bottom == 0) &&
3292 		    (rack->r_ctl.rack_per_of_gp_ca > rack_per_upper_bound_ca))
3293 			rack->r_ctl.rack_per_of_gp_ca = rack_per_upper_bound_ca;
3294 	}
3295 	if (rack->rc_gp_saw_ss &&
3296 	    rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3297 				  rack->r_ctl.rack_per_of_gp_ss)) {
3298 		/* In SS */
3299 		calc = rack->r_ctl.rack_per_of_gp_ss + plus;
3300 		if (calc > 0xffff)
3301 			calc = 0xffff;
3302 		rack->r_ctl.rack_per_of_gp_ss = (uint16_t)calc;
3303 		if (rack_per_upper_bound_ss &&
3304 		    (rack->rc_dragged_bottom == 0) &&
3305 		    (rack->r_ctl.rack_per_of_gp_ss > rack_per_upper_bound_ss))
3306 			rack->r_ctl.rack_per_of_gp_ss = rack_per_upper_bound_ss;
3307 		logged |= 4;
3308 	}
3309 	if (logged &&
3310 	    (rack->rc_gp_incr == 0)){
3311 		/* Go into increment mode */
3312 		rack->rc_gp_incr = 1;
3313 		rack->rc_gp_timely_inc_cnt = 0;
3314 	}
3315 	if (rack->rc_gp_incr &&
3316 	    logged &&
3317 	    (rack->rc_gp_timely_inc_cnt < RACK_TIMELY_CNT_BOOST)) {
3318 		rack->rc_gp_timely_inc_cnt++;
3319 	}
3320 	rack_log_timely(rack,  logged, plus, 0, 0,
3321 			__LINE__, 1);
3322 }
3323 
3324 static uint32_t
3325 rack_get_decrease(struct tcp_rack *rack, uint32_t curper, int32_t rtt_diff)
3326 {
3327 	/*
3328 	 * norm_grad = rtt_diff / minrtt;
3329 	 * new_per = curper * (1 - B * norm_grad)
3330 	 *
3331 	 * B = rack_gp_decrease_per (default 10%)
3332 	 * rtt_dif = input var current rtt-diff
3333 	 * curper = input var current percentage
3334 	 * minrtt = from rack filter
3335 	 *
3336 	 */
3337 	uint64_t perf;
3338 
3339 	perf = (((uint64_t)curper * ((uint64_t)1000000 -
3340 		    ((uint64_t)rack_gp_decrease_per * (uint64_t)10000 *
3341 		     (((uint64_t)rtt_diff * (uint64_t)1000000)/
3342 		      (uint64_t)get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt)))/
3343 		     (uint64_t)1000000)) /
3344 		(uint64_t)1000000);
3345 	if (perf > curper) {
3346 		/* TSNH */
3347 		perf = curper - 1;
3348 	}
3349 	return ((uint32_t)perf);
3350 }
3351 
3352 static uint32_t
3353 rack_decrease_highrtt(struct tcp_rack *rack, uint32_t curper, uint32_t rtt)
3354 {
3355 	/*
3356 	 *                                   highrttthresh
3357 	 * result = curper * (1 - (B * ( 1 -  ------          ))
3358 	 *                                     gp_srtt
3359 	 *
3360 	 * B = rack_gp_decrease_per (default 10%)
3361 	 * highrttthresh = filter_min * rack_gp_rtt_maxmul
3362 	 */
3363 	uint64_t perf;
3364 	uint32_t highrttthresh;
3365 
3366 	highrttthresh = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
3367 
3368 	perf = (((uint64_t)curper * ((uint64_t)1000000 -
3369 				     ((uint64_t)rack_gp_decrease_per * ((uint64_t)1000000 -
3370 					((uint64_t)highrttthresh * (uint64_t)1000000) /
3371 						    (uint64_t)rtt)) / 100)) /(uint64_t)1000000);
3372 	return (perf);
3373 }
3374 
3375 static void
3376 rack_decrease_bw_mul(struct tcp_rack *rack, int timely_says, uint32_t rtt, int32_t rtt_diff)
3377 {
3378 	uint64_t logvar, logvar2, logvar3;
3379 	uint32_t logged, new_per, ss_red, ca_red, rec_red, alt, val;
3380 
3381 	if (rack->rc_gp_incr) {
3382 		/* Turn off increment counting */
3383 		rack->rc_gp_incr = 0;
3384 		rack->rc_gp_timely_inc_cnt = 0;
3385 	}
3386 	ss_red = ca_red = rec_red = 0;
3387 	logged = 0;
3388 	/* Calculate the reduction value */
3389 	if (rtt_diff < 0) {
3390 		rtt_diff *= -1;
3391 	}
3392 	/* Must be at least 1% reduction */
3393 	if (rack->rc_gp_saw_rec && (rack->rc_gp_no_rec_chg == 0)) {
3394 		/* We have been in recovery ding it too */
3395 		if (timely_says == 2) {
3396 			new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_rec, rtt);
3397 			alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
3398 			if (alt < new_per)
3399 				val = alt;
3400 			else
3401 				val = new_per;
3402 		} else
3403 			 val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
3404 		if (rack->r_ctl.rack_per_of_gp_rec > val) {
3405 			rec_red = (rack->r_ctl.rack_per_of_gp_rec - val);
3406 			rack->r_ctl.rack_per_of_gp_rec = (uint16_t)val;
3407 		} else {
3408 			rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound;
3409 			rec_red = 0;
3410 		}
3411 		if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_rec)
3412 			rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound;
3413 		logged |= 1;
3414 	}
3415 	if (rack->rc_gp_saw_ss) {
3416 		/* Sent in SS */
3417 		if (timely_says == 2) {
3418 			new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ss, rtt);
3419 			alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
3420 			if (alt < new_per)
3421 				val = alt;
3422 			else
3423 				val = new_per;
3424 		} else
3425 			val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ss, rtt_diff);
3426 		if (rack->r_ctl.rack_per_of_gp_ss > new_per) {
3427 			ss_red = rack->r_ctl.rack_per_of_gp_ss - val;
3428 			rack->r_ctl.rack_per_of_gp_ss = (uint16_t)val;
3429 		} else {
3430 			ss_red = new_per;
3431 			rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound;
3432 			logvar = new_per;
3433 			logvar <<= 32;
3434 			logvar |= alt;
3435 			logvar2 = (uint32_t)rtt;
3436 			logvar2 <<= 32;
3437 			logvar2 |= (uint32_t)rtt_diff;
3438 			logvar3 = rack_gp_rtt_maxmul;
3439 			logvar3 <<= 32;
3440 			logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
3441 			rack_log_timely(rack, timely_says,
3442 					logvar2, logvar3,
3443 					logvar, __LINE__, 10);
3444 		}
3445 		if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ss)
3446 			rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound;
3447 		logged |= 4;
3448 	} else if (rack->rc_gp_saw_ca) {
3449 		/* Sent in CA */
3450 		if (timely_says == 2) {
3451 			new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ca, rtt);
3452 			alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
3453 			if (alt < new_per)
3454 				val = alt;
3455 			else
3456 				val = new_per;
3457 		} else
3458 			val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ca, rtt_diff);
3459 		if (rack->r_ctl.rack_per_of_gp_ca > val) {
3460 			ca_red = rack->r_ctl.rack_per_of_gp_ca - val;
3461 			rack->r_ctl.rack_per_of_gp_ca = (uint16_t)val;
3462 		} else {
3463 			rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound;
3464 			ca_red = 0;
3465 			logvar = new_per;
3466 			logvar <<= 32;
3467 			logvar |= alt;
3468 			logvar2 = (uint32_t)rtt;
3469 			logvar2 <<= 32;
3470 			logvar2 |= (uint32_t)rtt_diff;
3471 			logvar3 = rack_gp_rtt_maxmul;
3472 			logvar3 <<= 32;
3473 			logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
3474 			rack_log_timely(rack, timely_says,
3475 					logvar2, logvar3,
3476 					logvar, __LINE__, 10);
3477 		}
3478 		if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ca)
3479 			rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound;
3480 		logged |= 2;
3481 	}
3482 	if (rack->rc_gp_timely_dec_cnt < 0x7) {
3483 		rack->rc_gp_timely_dec_cnt++;
3484 		if (rack_timely_dec_clear &&
3485 		    (rack->rc_gp_timely_dec_cnt == rack_timely_dec_clear))
3486 			rack->rc_gp_timely_dec_cnt = 0;
3487 	}
3488 	logvar = ss_red;
3489 	logvar <<= 32;
3490 	logvar |= ca_red;
3491 	rack_log_timely(rack,  logged, rec_red, rack_per_lower_bound, logvar,
3492 			__LINE__, 2);
3493 }
3494 
3495 static void
3496 rack_log_rtt_shrinks(struct tcp_rack *rack, uint32_t us_cts,
3497 		     uint32_t rtt, uint32_t line, uint8_t reas)
3498 {
3499 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
3500 		union tcp_log_stackspecific log;
3501 		struct timeval tv;
3502 
3503 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3504 		log.u_bbr.flex1 = line;
3505 		log.u_bbr.flex2 = rack->r_ctl.rc_time_probertt_starts;
3506 		log.u_bbr.flex3 = rack->r_ctl.rc_lower_rtt_us_cts;
3507 		log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss;
3508 		log.u_bbr.flex5 = rtt;
3509 		log.u_bbr.flex6 = rack->rc_highly_buffered;
3510 		log.u_bbr.flex6 <<= 1;
3511 		log.u_bbr.flex6 |= rack->forced_ack;
3512 		log.u_bbr.flex6 <<= 1;
3513 		log.u_bbr.flex6 |= rack->rc_gp_dyn_mul;
3514 		log.u_bbr.flex6 <<= 1;
3515 		log.u_bbr.flex6 |= rack->in_probe_rtt;
3516 		log.u_bbr.flex6 <<= 1;
3517 		log.u_bbr.flex6 |= rack->measure_saw_probe_rtt;
3518 		log.u_bbr.flex7 = rack->r_ctl.rack_per_of_gp_probertt;
3519 		log.u_bbr.pacing_gain = rack->r_ctl.rack_per_of_gp_ca;
3520 		log.u_bbr.cwnd_gain = rack->r_ctl.rack_per_of_gp_rec;
3521 		log.u_bbr.flex8 = reas;
3522 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3523 		log.u_bbr.delRate = rack_get_bw(rack);
3524 		log.u_bbr.cur_del_rate = rack->r_ctl.rc_highest_us_rtt;
3525 		log.u_bbr.cur_del_rate <<= 32;
3526 		log.u_bbr.cur_del_rate |= rack->r_ctl.rc_lowest_us_rtt;
3527 		log.u_bbr.applimited = rack->r_ctl.rc_time_probertt_entered;
3528 		log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff;
3529 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3530 		log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt;
3531 		log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt;
3532 		log.u_bbr.pkt_epoch = rack->r_ctl.rc_lower_rtt_us_cts;
3533 		log.u_bbr.delivered = rack->r_ctl.rc_target_probertt_flight;
3534 		log.u_bbr.lost = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
3535 		log.u_bbr.rttProp = us_cts;
3536 		log.u_bbr.rttProp <<= 32;
3537 		log.u_bbr.rttProp |= rack->r_ctl.rc_entry_gp_rtt;
3538 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3539 		    &rack->rc_inp->inp_socket->so_rcv,
3540 		    &rack->rc_inp->inp_socket->so_snd,
3541 		    BBR_LOG_RTT_SHRINKS, 0,
3542 		    0, &log, false, &rack->r_ctl.act_rcv_time);
3543 	}
3544 }
3545 
3546 static void
3547 rack_set_prtt_target(struct tcp_rack *rack, uint32_t segsiz, uint32_t rtt)
3548 {
3549 	uint64_t bwdp;
3550 
3551 	bwdp = rack_get_bw(rack);
3552 	bwdp *= (uint64_t)rtt;
3553 	bwdp /= (uint64_t)HPTS_USEC_IN_SEC;
3554 	rack->r_ctl.rc_target_probertt_flight = roundup((uint32_t)bwdp, segsiz);
3555 	if (rack->r_ctl.rc_target_probertt_flight < (segsiz * rack_timely_min_segs)) {
3556 		/*
3557 		 * A window protocol must be able to have 4 packets
3558 		 * outstanding as the floor in order to function
3559 		 * (especially considering delayed ack :D).
3560 		 */
3561 		rack->r_ctl.rc_target_probertt_flight = (segsiz * rack_timely_min_segs);
3562 	}
3563 }
3564 
3565 static void
3566 rack_enter_probertt(struct tcp_rack *rack, uint32_t us_cts)
3567 {
3568 	/**
3569 	 * ProbeRTT is a bit different in rack_pacing than in
3570 	 * BBR. It is like BBR in that it uses the lowering of
3571 	 * the RTT as a signal that we saw something new and
3572 	 * counts from there for how long between. But it is
3573 	 * different in that its quite simple. It does not
3574 	 * play with the cwnd and wait until we get down
3575 	 * to N segments outstanding and hold that for
3576 	 * 200ms. Instead it just sets the pacing reduction
3577 	 * rate to a set percentage (70 by default) and hold
3578 	 * that for a number of recent GP Srtt's.
3579 	 */
3580 	uint32_t segsiz;
3581 
3582 	if (rack->rc_gp_dyn_mul == 0)
3583 		return;
3584 
3585 	if (rack->rc_tp->snd_max == rack->rc_tp->snd_una) {
3586 		/* We are idle */
3587 		return;
3588 	}
3589 	if ((rack->rc_tp->t_flags & TF_GPUTINPROG) &&
3590 	    SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) {
3591 		/*
3592 		 * Stop the goodput now, the idea here is
3593 		 * that future measurements with in_probe_rtt
3594 		 * won't register if they are not greater so
3595 		 * we want to get what info (if any) is available
3596 		 * now.
3597 		 */
3598 		rack_do_goodput_measurement(rack->rc_tp, rack,
3599 					    rack->rc_tp->snd_una, __LINE__,
3600 					    RACK_QUALITY_PROBERTT);
3601 	}
3602 	rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
3603 	rack->r_ctl.rc_time_probertt_entered = us_cts;
3604 	segsiz = min(ctf_fixed_maxseg(rack->rc_tp),
3605 		     rack->r_ctl.rc_pace_min_segs);
3606 	rack->in_probe_rtt = 1;
3607 	rack->measure_saw_probe_rtt = 1;
3608 	rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
3609 	rack->r_ctl.rc_time_probertt_starts = 0;
3610 	rack->r_ctl.rc_entry_gp_rtt = rack->r_ctl.rc_gp_srtt;
3611 	if (rack_probertt_use_min_rtt_entry)
3612 		rack_set_prtt_target(rack, segsiz, get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt));
3613 	else
3614 		rack_set_prtt_target(rack, segsiz, rack->r_ctl.rc_gp_srtt);
3615 	rack_log_rtt_shrinks(rack,  us_cts,  get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
3616 			     __LINE__, RACK_RTTS_ENTERPROBE);
3617 }
3618 
3619 static void
3620 rack_exit_probertt(struct tcp_rack *rack, uint32_t us_cts)
3621 {
3622 	struct rack_sendmap *rsm;
3623 	uint32_t segsiz;
3624 
3625 	segsiz = min(ctf_fixed_maxseg(rack->rc_tp),
3626 		     rack->r_ctl.rc_pace_min_segs);
3627 	rack->in_probe_rtt = 0;
3628 	if ((rack->rc_tp->t_flags & TF_GPUTINPROG) &&
3629 	    SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) {
3630 		/*
3631 		 * Stop the goodput now, the idea here is
3632 		 * that future measurements with in_probe_rtt
3633 		 * won't register if they are not greater so
3634 		 * we want to get what info (if any) is available
3635 		 * now.
3636 		 */
3637 		rack_do_goodput_measurement(rack->rc_tp, rack,
3638 					    rack->rc_tp->snd_una, __LINE__,
3639 					    RACK_QUALITY_PROBERTT);
3640 	} else if (rack->rc_tp->t_flags & TF_GPUTINPROG) {
3641 		/*
3642 		 * We don't have enough data to make a measurement.
3643 		 * So lets just stop and start here after exiting
3644 		 * probe-rtt. We probably are not interested in
3645 		 * the results anyway.
3646 		 */
3647 		rack->rc_tp->t_flags &= ~TF_GPUTINPROG;
3648 	}
3649 	/*
3650 	 * Measurements through the current snd_max are going
3651 	 * to be limited by the slower pacing rate.
3652 	 *
3653 	 * We need to mark these as app-limited so we
3654 	 * don't collapse the b/w.
3655 	 */
3656 	rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
3657 	if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) {
3658 		if (rack->r_ctl.rc_app_limited_cnt == 0)
3659 			rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm;
3660 		else {
3661 			/*
3662 			 * Go out to the end app limited and mark
3663 			 * this new one as next and move the end_appl up
3664 			 * to this guy.
3665 			 */
3666 			if (rack->r_ctl.rc_end_appl)
3667 				rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start;
3668 			rack->r_ctl.rc_end_appl = rsm;
3669 		}
3670 		rsm->r_flags |= RACK_APP_LIMITED;
3671 		rack->r_ctl.rc_app_limited_cnt++;
3672 	}
3673 	/*
3674 	 * Now, we need to examine our pacing rate multipliers.
3675 	 * If its under 100%, we need to kick it back up to
3676 	 * 100%. We also don't let it be over our "max" above
3677 	 * the actual rate i.e. 100% + rack_clamp_atexit_prtt.
3678 	 * Note setting clamp_atexit_prtt to 0 has the effect
3679 	 * of setting CA/SS to 100% always at exit (which is
3680 	 * the default behavior).
3681 	 */
3682 	if (rack_probertt_clear_is) {
3683 		rack->rc_gp_incr = 0;
3684 		rack->rc_gp_bwred = 0;
3685 		rack->rc_gp_timely_inc_cnt = 0;
3686 		rack->rc_gp_timely_dec_cnt = 0;
3687 	}
3688 	/* Do we do any clamping at exit? */
3689 	if (rack->rc_highly_buffered && rack_atexit_prtt_hbp) {
3690 		rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt_hbp;
3691 		rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt_hbp;
3692 	}
3693 	if ((rack->rc_highly_buffered == 0) && rack_atexit_prtt) {
3694 		rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt;
3695 		rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt;
3696 	}
3697 	/*
3698 	 * Lets set rtt_diff to 0, so that we will get a "boost"
3699 	 * after exiting.
3700 	 */
3701 	rack->r_ctl.rc_rtt_diff = 0;
3702 
3703 	/* Clear all flags so we start fresh */
3704 	rack->rc_tp->t_bytes_acked = 0;
3705 	rack->rc_tp->ccv->flags &= ~CCF_ABC_SENTAWND;
3706 	/*
3707 	 * If configured to, set the cwnd and ssthresh to
3708 	 * our targets.
3709 	 */
3710 	if (rack_probe_rtt_sets_cwnd) {
3711 		uint64_t ebdp;
3712 		uint32_t setto;
3713 
3714 		/* Set ssthresh so we get into CA once we hit our target */
3715 		if (rack_probertt_use_min_rtt_exit == 1) {
3716 			/* Set to min rtt */
3717 			rack_set_prtt_target(rack, segsiz,
3718 					     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt));
3719 		} else if (rack_probertt_use_min_rtt_exit == 2) {
3720 			/* Set to current gp rtt */
3721 			rack_set_prtt_target(rack, segsiz,
3722 					     rack->r_ctl.rc_gp_srtt);
3723 		} else if (rack_probertt_use_min_rtt_exit == 3) {
3724 			/* Set to entry gp rtt */
3725 			rack_set_prtt_target(rack, segsiz,
3726 					     rack->r_ctl.rc_entry_gp_rtt);
3727 		} else {
3728 			uint64_t sum;
3729 			uint32_t setval;
3730 
3731 			sum = rack->r_ctl.rc_entry_gp_rtt;
3732 			sum *= 10;
3733 			sum /= (uint64_t)(max(1, rack->r_ctl.rc_gp_srtt));
3734 			if (sum >= 20) {
3735 				/*
3736 				 * A highly buffered path needs
3737 				 * cwnd space for timely to work.
3738 				 * Lets set things up as if
3739 				 * we are heading back here again.
3740 				 */
3741 				setval = rack->r_ctl.rc_entry_gp_rtt;
3742 			} else if (sum >= 15) {
3743 				/*
3744 				 * Lets take the smaller of the
3745 				 * two since we are just somewhat
3746 				 * buffered.
3747 				 */
3748 				setval = rack->r_ctl.rc_gp_srtt;
3749 				if (setval > rack->r_ctl.rc_entry_gp_rtt)
3750 					setval = rack->r_ctl.rc_entry_gp_rtt;
3751 			} else {
3752 				/*
3753 				 * Here we are not highly buffered
3754 				 * and should pick the min we can to
3755 				 * keep from causing loss.
3756 				 */
3757 				setval = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
3758 			}
3759 			rack_set_prtt_target(rack, segsiz,
3760 					     setval);
3761 		}
3762 		if (rack_probe_rtt_sets_cwnd > 1) {
3763 			/* There is a percentage here to boost */
3764 			ebdp = rack->r_ctl.rc_target_probertt_flight;
3765 			ebdp *= rack_probe_rtt_sets_cwnd;
3766 			ebdp /= 100;
3767 			setto = rack->r_ctl.rc_target_probertt_flight + ebdp;
3768 		} else
3769 			setto = rack->r_ctl.rc_target_probertt_flight;
3770 		rack->rc_tp->snd_cwnd = roundup(setto, segsiz);
3771 		if (rack->rc_tp->snd_cwnd < (segsiz * rack_timely_min_segs)) {
3772 			/* Enforce a min */
3773 			rack->rc_tp->snd_cwnd = segsiz * rack_timely_min_segs;
3774 		}
3775 		/* If we set in the cwnd also set the ssthresh point so we are in CA */
3776 		rack->rc_tp->snd_ssthresh = (rack->rc_tp->snd_cwnd - 1);
3777 	}
3778 	rack_log_rtt_shrinks(rack,  us_cts,
3779 			     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
3780 			     __LINE__, RACK_RTTS_EXITPROBE);
3781 	/* Clear times last so log has all the info */
3782 	rack->r_ctl.rc_probertt_sndmax_atexit = rack->rc_tp->snd_max;
3783 	rack->r_ctl.rc_time_probertt_entered = us_cts;
3784 	rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
3785 	rack->r_ctl.rc_time_of_last_probertt = us_cts;
3786 }
3787 
3788 static void
3789 rack_check_probe_rtt(struct tcp_rack *rack, uint32_t us_cts)
3790 {
3791 	/* Check in on probe-rtt */
3792 	if (rack->rc_gp_filled == 0) {
3793 		/* We do not do p-rtt unless we have gp measurements */
3794 		return;
3795 	}
3796 	if (rack->in_probe_rtt) {
3797 		uint64_t no_overflow;
3798 		uint32_t endtime, must_stay;
3799 
3800 		if (rack->r_ctl.rc_went_idle_time &&
3801 		    ((us_cts - rack->r_ctl.rc_went_idle_time) > rack_min_probertt_hold)) {
3802 			/*
3803 			 * We went idle during prtt, just exit now.
3804 			 */
3805 			rack_exit_probertt(rack, us_cts);
3806 		} else if (rack_probe_rtt_safety_val &&
3807 		    TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered) &&
3808 		    ((us_cts - rack->r_ctl.rc_time_probertt_entered) > rack_probe_rtt_safety_val)) {
3809 			/*
3810 			 * Probe RTT safety value triggered!
3811 			 */
3812 			rack_log_rtt_shrinks(rack,  us_cts,
3813 					     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
3814 					     __LINE__, RACK_RTTS_SAFETY);
3815 			rack_exit_probertt(rack, us_cts);
3816 		}
3817 		/* Calculate the max we will wait */
3818 		endtime = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_max_drain_wait);
3819 		if (rack->rc_highly_buffered)
3820 			endtime += (rack->r_ctl.rc_gp_srtt * rack_max_drain_hbp);
3821 		/* Calculate the min we must wait */
3822 		must_stay = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_must_drain);
3823 		if ((ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.rc_target_probertt_flight) &&
3824 		    TSTMP_LT(us_cts, endtime)) {
3825 			uint32_t calc;
3826 			/* Do we lower more? */
3827 no_exit:
3828 			if (TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered))
3829 				calc = us_cts - rack->r_ctl.rc_time_probertt_entered;
3830 			else
3831 				calc = 0;
3832 			calc /= max(rack->r_ctl.rc_gp_srtt, 1);
3833 			if (calc) {
3834 				/* Maybe */
3835 				calc *= rack_per_of_gp_probertt_reduce;
3836 				rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt - calc;
3837 				/* Limit it too */
3838 				if (rack->r_ctl.rack_per_of_gp_probertt < rack_per_of_gp_lowthresh)
3839 					rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_lowthresh;
3840 			}
3841 			/* We must reach target or the time set */
3842 			return;
3843 		}
3844 		if (rack->r_ctl.rc_time_probertt_starts == 0) {
3845 			if ((TSTMP_LT(us_cts, must_stay) &&
3846 			     rack->rc_highly_buffered) ||
3847 			     (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) >
3848 			      rack->r_ctl.rc_target_probertt_flight)) {
3849 				/* We are not past the must_stay time */
3850 				goto no_exit;
3851 			}
3852 			rack_log_rtt_shrinks(rack,  us_cts,
3853 					     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
3854 					     __LINE__, RACK_RTTS_REACHTARGET);
3855 			rack->r_ctl.rc_time_probertt_starts = us_cts;
3856 			if (rack->r_ctl.rc_time_probertt_starts == 0)
3857 				rack->r_ctl.rc_time_probertt_starts = 1;
3858 			/* Restore back to our rate we want to pace at in prtt */
3859 			rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
3860 		}
3861 		/*
3862 		 * Setup our end time, some number of gp_srtts plus 200ms.
3863 		 */
3864 		no_overflow = ((uint64_t)rack->r_ctl.rc_gp_srtt *
3865 			       (uint64_t)rack_probertt_gpsrtt_cnt_mul);
3866 		if (rack_probertt_gpsrtt_cnt_div)
3867 			endtime = (uint32_t)(no_overflow / (uint64_t)rack_probertt_gpsrtt_cnt_div);
3868 		else
3869 			endtime = 0;
3870 		endtime += rack_min_probertt_hold;
3871 		endtime += rack->r_ctl.rc_time_probertt_starts;
3872 		if (TSTMP_GEQ(us_cts,  endtime)) {
3873 			/* yes, exit probertt */
3874 			rack_exit_probertt(rack, us_cts);
3875 		}
3876 
3877 	} else if ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= rack_time_between_probertt) {
3878 		/* Go into probertt, its been too long since we went lower */
3879 		rack_enter_probertt(rack, us_cts);
3880 	}
3881 }
3882 
3883 static void
3884 rack_update_multiplier(struct tcp_rack *rack, int32_t timely_says, uint64_t last_bw_est,
3885 		       uint32_t rtt, int32_t rtt_diff)
3886 {
3887 	uint64_t cur_bw, up_bnd, low_bnd, subfr;
3888 	uint32_t losses;
3889 
3890 	if ((rack->rc_gp_dyn_mul == 0) ||
3891 	    (rack->use_fixed_rate) ||
3892 	    (rack->in_probe_rtt) ||
3893 	    (rack->rc_always_pace == 0)) {
3894 		/* No dynamic GP multiplier in play */
3895 		return;
3896 	}
3897 	losses = rack->r_ctl.rc_loss_count - rack->r_ctl.rc_loss_at_start;
3898 	cur_bw = rack_get_bw(rack);
3899 	/* Calculate our up and down range */
3900 	up_bnd = rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_up;
3901 	up_bnd /= 100;
3902 	up_bnd += rack->r_ctl.last_gp_comp_bw;
3903 
3904 	subfr = (uint64_t)rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_down;
3905 	subfr /= 100;
3906 	low_bnd = rack->r_ctl.last_gp_comp_bw - subfr;
3907 	if ((timely_says == 2) && (rack->r_ctl.rc_no_push_at_mrtt)) {
3908 		/*
3909 		 * This is the case where our RTT is above
3910 		 * the max target and we have been configured
3911 		 * to just do timely no bonus up stuff in that case.
3912 		 *
3913 		 * There are two configurations, set to 1, and we
3914 		 * just do timely if we are over our max. If its
3915 		 * set above 1 then we slam the multipliers down
3916 		 * to 100 and then decrement per timely.
3917 		 */
3918 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
3919 				__LINE__, 3);
3920 		if (rack->r_ctl.rc_no_push_at_mrtt > 1)
3921 			rack_validate_multipliers_at_or_below_100(rack);
3922 		rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff);
3923 	} else if ((last_bw_est < low_bnd) && !losses) {
3924 		/*
3925 		 * We are decreasing this is a bit complicated this
3926 		 * means we are loosing ground. This could be
3927 		 * because another flow entered and we are competing
3928 		 * for b/w with it. This will push the RTT up which
3929 		 * makes timely unusable unless we want to get shoved
3930 		 * into a corner and just be backed off (the age
3931 		 * old problem with delay based CC).
3932 		 *
3933 		 * On the other hand if it was a route change we
3934 		 * would like to stay somewhat contained and not
3935 		 * blow out the buffers.
3936 		 */
3937 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
3938 				__LINE__, 3);
3939 		rack->r_ctl.last_gp_comp_bw = cur_bw;
3940 		if (rack->rc_gp_bwred == 0) {
3941 			/* Go into reduction counting */
3942 			rack->rc_gp_bwred = 1;
3943 			rack->rc_gp_timely_dec_cnt = 0;
3944 		}
3945 		if ((rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) ||
3946 		    (timely_says == 0)) {
3947 			/*
3948 			 * Push another time with a faster pacing
3949 			 * to try to gain back (we include override to
3950 			 * get a full raise factor).
3951 			 */
3952 			if ((rack->rc_gp_saw_ca && rack->r_ctl.rack_per_of_gp_ca <= rack_down_raise_thresh) ||
3953 			    (rack->rc_gp_saw_ss && rack->r_ctl.rack_per_of_gp_ss <= rack_down_raise_thresh) ||
3954 			    (timely_says == 0) ||
3955 			    (rack_down_raise_thresh == 0)) {
3956 				/*
3957 				 * Do an override up in b/w if we were
3958 				 * below the threshold or if the threshold
3959 				 * is zero we always do the raise.
3960 				 */
3961 				rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 1);
3962 			} else {
3963 				/* Log it stays the same */
3964 				rack_log_timely(rack,  0, last_bw_est, low_bnd, 0,
3965 						__LINE__, 11);
3966 			}
3967 			rack->rc_gp_timely_dec_cnt++;
3968 			/* We are not incrementing really no-count */
3969 			rack->rc_gp_incr = 0;
3970 			rack->rc_gp_timely_inc_cnt = 0;
3971 		} else {
3972 			/*
3973 			 * Lets just use the RTT
3974 			 * information and give up
3975 			 * pushing.
3976 			 */
3977 			goto use_timely;
3978 		}
3979 	} else if ((timely_says != 2) &&
3980 		    !losses &&
3981 		    (last_bw_est > up_bnd)) {
3982 		/*
3983 		 * We are increasing b/w lets keep going, updating
3984 		 * our b/w and ignoring any timely input, unless
3985 		 * of course we are at our max raise (if there is one).
3986 		 */
3987 
3988 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
3989 				__LINE__, 3);
3990 		rack->r_ctl.last_gp_comp_bw = cur_bw;
3991 		if (rack->rc_gp_saw_ss &&
3992 		    rack_per_upper_bound_ss &&
3993 		     (rack->r_ctl.rack_per_of_gp_ss == rack_per_upper_bound_ss)) {
3994 			    /*
3995 			     * In cases where we can't go higher
3996 			     * we should just use timely.
3997 			     */
3998 			    goto use_timely;
3999 		}
4000 		if (rack->rc_gp_saw_ca &&
4001 		    rack_per_upper_bound_ca &&
4002 		    (rack->r_ctl.rack_per_of_gp_ca == rack_per_upper_bound_ca)) {
4003 			    /*
4004 			     * In cases where we can't go higher
4005 			     * we should just use timely.
4006 			     */
4007 			    goto use_timely;
4008 		}
4009 		rack->rc_gp_bwred = 0;
4010 		rack->rc_gp_timely_dec_cnt = 0;
4011 		/* You get a set number of pushes if timely is trying to reduce */
4012 		if ((rack->rc_gp_incr < rack_timely_max_push_rise) || (timely_says == 0)) {
4013 			rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4014 		} else {
4015 			/* Log it stays the same */
4016 			rack_log_timely(rack,  0, last_bw_est, up_bnd, 0,
4017 			    __LINE__, 12);
4018 		}
4019 		return;
4020 	} else {
4021 		/*
4022 		 * We are staying between the lower and upper range bounds
4023 		 * so use timely to decide.
4024 		 */
4025 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4026 				__LINE__, 3);
4027 use_timely:
4028 		if (timely_says) {
4029 			rack->rc_gp_incr = 0;
4030 			rack->rc_gp_timely_inc_cnt = 0;
4031 			if ((rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) &&
4032 			    !losses &&
4033 			    (last_bw_est < low_bnd)) {
4034 				/* We are loosing ground */
4035 				rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4036 				rack->rc_gp_timely_dec_cnt++;
4037 				/* We are not incrementing really no-count */
4038 				rack->rc_gp_incr = 0;
4039 				rack->rc_gp_timely_inc_cnt = 0;
4040 			} else
4041 				rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff);
4042 		} else {
4043 			rack->rc_gp_bwred = 0;
4044 			rack->rc_gp_timely_dec_cnt = 0;
4045 			rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4046 		}
4047 	}
4048 }
4049 
4050 static int32_t
4051 rack_make_timely_judgement(struct tcp_rack *rack, uint32_t rtt, int32_t rtt_diff, uint32_t prev_rtt)
4052 {
4053 	int32_t timely_says;
4054 	uint64_t log_mult, log_rtt_a_diff;
4055 
4056 	log_rtt_a_diff = rtt;
4057 	log_rtt_a_diff <<= 32;
4058 	log_rtt_a_diff |= (uint32_t)rtt_diff;
4059 	if (rtt >= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) *
4060 		    rack_gp_rtt_maxmul)) {
4061 		/* Reduce the b/w multiplier */
4062 		timely_says = 2;
4063 		log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
4064 		log_mult <<= 32;
4065 		log_mult |= prev_rtt;
4066 		rack_log_timely(rack,  timely_says, log_mult,
4067 				get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4068 				log_rtt_a_diff, __LINE__, 4);
4069 	} else if (rtt <= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) +
4070 			   ((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) /
4071 			    max(rack_gp_rtt_mindiv , 1)))) {
4072 		/* Increase the b/w multiplier */
4073 		log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) +
4074 			((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) /
4075 			 max(rack_gp_rtt_mindiv , 1));
4076 		log_mult <<= 32;
4077 		log_mult |= prev_rtt;
4078 		timely_says = 0;
4079 		rack_log_timely(rack,  timely_says, log_mult ,
4080 				get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4081 				log_rtt_a_diff, __LINE__, 5);
4082 	} else {
4083 		/*
4084 		 * Use a gradient to find it the timely gradient
4085 		 * is:
4086 		 * grad = rc_rtt_diff / min_rtt;
4087 		 *
4088 		 * anything below or equal to 0 will be
4089 		 * a increase indication. Anything above
4090 		 * zero is a decrease. Note we take care
4091 		 * of the actual gradient calculation
4092 		 * in the reduction (its not needed for
4093 		 * increase).
4094 		 */
4095 		log_mult = prev_rtt;
4096 		if (rtt_diff <= 0) {
4097 			/*
4098 			 * Rttdiff is less than zero, increase the
4099 			 * b/w multiplier (its 0 or negative)
4100 			 */
4101 			timely_says = 0;
4102 			rack_log_timely(rack,  timely_says, log_mult,
4103 					get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 6);
4104 		} else {
4105 			/* Reduce the b/w multiplier */
4106 			timely_says = 1;
4107 			rack_log_timely(rack,  timely_says, log_mult,
4108 					get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 7);
4109 		}
4110 	}
4111 	return (timely_says);
4112 }
4113 
4114 static void
4115 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
4116 			    tcp_seq th_ack, int line, uint8_t quality)
4117 {
4118 	uint64_t tim, bytes_ps, ltim, stim, utim;
4119 	uint32_t segsiz, bytes, reqbytes, us_cts;
4120 	int32_t gput, new_rtt_diff, timely_says;
4121 	uint64_t  resid_bw, subpart = 0, addpart = 0, srtt;
4122 	int did_add = 0;
4123 
4124 	us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
4125 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
4126 	if (TSTMP_GEQ(us_cts, tp->gput_ts))
4127 		tim = us_cts - tp->gput_ts;
4128 	else
4129 		tim = 0;
4130 	if (rack->r_ctl.rc_gp_cumack_ts > rack->r_ctl.rc_gp_output_ts)
4131 		stim = rack->r_ctl.rc_gp_cumack_ts - rack->r_ctl.rc_gp_output_ts;
4132 	else
4133 		stim = 0;
4134 	/*
4135 	 * Use the larger of the send time or ack time. This prevents us
4136 	 * from being influenced by ack artifacts to come up with too
4137 	 * high of measurement. Note that since we are spanning over many more
4138 	 * bytes in most of our measurements hopefully that is less likely to
4139 	 * occur.
4140 	 */
4141 	if (tim > stim)
4142 		utim = max(tim, 1);
4143 	else
4144 		utim = max(stim, 1);
4145 	/* Lets get a msec time ltim too for the old stuff */
4146 	ltim = max(1, (utim / HPTS_USEC_IN_MSEC));
4147 	gput = (((uint64_t) (th_ack - tp->gput_seq)) << 3) / ltim;
4148 	reqbytes = min(rc_init_window(rack), (MIN_GP_WIN * segsiz));
4149 	if ((tim == 0) && (stim == 0)) {
4150 		/*
4151 		 * Invalid measurement time, maybe
4152 		 * all on one ack/one send?
4153 		 */
4154 		bytes = 0;
4155 		bytes_ps = 0;
4156 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4157 					   0, 0, 0, 10, __LINE__, NULL, quality);
4158 		goto skip_measurement;
4159 	}
4160 	if (rack->r_ctl.rc_gp_lowrtt == 0xffffffff) {
4161 		/* We never made a us_rtt measurement? */
4162 		bytes = 0;
4163 		bytes_ps = 0;
4164 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4165 					   0, 0, 0, 10, __LINE__, NULL, quality);
4166 		goto skip_measurement;
4167 	}
4168 	/*
4169 	 * Calculate the maximum possible b/w this connection
4170 	 * could have. We base our calculation on the lowest
4171 	 * rtt we have seen during the measurement and the
4172 	 * largest rwnd the client has given us in that time. This
4173 	 * forms a BDP that is the maximum that we could ever
4174 	 * get to the client. Anything larger is not valid.
4175 	 *
4176 	 * I originally had code here that rejected measurements
4177 	 * where the time was less than 1/2 the latest us_rtt.
4178 	 * But after thinking on that I realized its wrong since
4179 	 * say you had a 150Mbps or even 1Gbps link, and you
4180 	 * were a long way away.. example I am in Europe (100ms rtt)
4181 	 * talking to my 1Gbps link in S.C. Now measuring say 150,000
4182 	 * bytes my time would be 1.2ms, and yet my rtt would say
4183 	 * the measurement was invalid the time was < 50ms. The
4184 	 * same thing is true for 150Mb (8ms of time).
4185 	 *
4186 	 * A better way I realized is to look at what the maximum
4187 	 * the connection could possibly do. This is gated on
4188 	 * the lowest RTT we have seen and the highest rwnd.
4189 	 * We should in theory never exceed that, if we are
4190 	 * then something on the path is storing up packets
4191 	 * and then feeding them all at once to our endpoint
4192 	 * messing up our measurement.
4193 	 */
4194 	rack->r_ctl.last_max_bw = rack->r_ctl.rc_gp_high_rwnd;
4195 	rack->r_ctl.last_max_bw *= HPTS_USEC_IN_SEC;
4196 	rack->r_ctl.last_max_bw /= rack->r_ctl.rc_gp_lowrtt;
4197 	if (SEQ_LT(th_ack, tp->gput_seq)) {
4198 		/* No measurement can be made */
4199 		bytes = 0;
4200 		bytes_ps = 0;
4201 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4202 					   0, 0, 0, 10, __LINE__, NULL, quality);
4203 		goto skip_measurement;
4204 	} else
4205 		bytes = (th_ack - tp->gput_seq);
4206 	bytes_ps = (uint64_t)bytes;
4207 	/*
4208 	 * Don't measure a b/w for pacing unless we have gotten at least
4209 	 * an initial windows worth of data in this measurement interval.
4210 	 *
4211 	 * Small numbers of bytes get badly influenced by delayed ack and
4212 	 * other artifacts. Note we take the initial window or our
4213 	 * defined minimum GP (defaulting to 10 which hopefully is the
4214 	 * IW).
4215 	 */
4216 	if (rack->rc_gp_filled == 0) {
4217 		/*
4218 		 * The initial estimate is special. We
4219 		 * have blasted out an IW worth of packets
4220 		 * without a real valid ack ts results. We
4221 		 * then setup the app_limited_needs_set flag,
4222 		 * this should get the first ack in (probably 2
4223 		 * MSS worth) to be recorded as the timestamp.
4224 		 * We thus allow a smaller number of bytes i.e.
4225 		 * IW - 2MSS.
4226 		 */
4227 		reqbytes -= (2 * segsiz);
4228 		/* Also lets fill previous for our first measurement to be neutral */
4229 		rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt;
4230 	}
4231 	if ((bytes_ps < reqbytes) || rack->app_limited_needs_set) {
4232 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4233 					   rack->r_ctl.rc_app_limited_cnt,
4234 					   0, 0, 10, __LINE__, NULL, quality);
4235 		goto skip_measurement;
4236 	}
4237 	/*
4238 	 * We now need to calculate the Timely like status so
4239 	 * we can update (possibly) the b/w multipliers.
4240 	 */
4241 	new_rtt_diff = (int32_t)rack->r_ctl.rc_gp_srtt - (int32_t)rack->r_ctl.rc_prev_gp_srtt;
4242 	if (rack->rc_gp_filled == 0) {
4243 		/* No previous reading */
4244 		rack->r_ctl.rc_rtt_diff = new_rtt_diff;
4245 	} else {
4246 		if (rack->measure_saw_probe_rtt == 0) {
4247 			/*
4248 			 * We don't want a probertt to be counted
4249 			 * since it will be negative incorrectly. We
4250 			 * expect to be reducing the RTT when we
4251 			 * pace at a slower rate.
4252 			 */
4253 			rack->r_ctl.rc_rtt_diff -= (rack->r_ctl.rc_rtt_diff / 8);
4254 			rack->r_ctl.rc_rtt_diff += (new_rtt_diff / 8);
4255 		}
4256 	}
4257 	timely_says = rack_make_timely_judgement(rack,
4258 		rack->r_ctl.rc_gp_srtt,
4259 		rack->r_ctl.rc_rtt_diff,
4260 	        rack->r_ctl.rc_prev_gp_srtt
4261 		);
4262 	bytes_ps *= HPTS_USEC_IN_SEC;
4263 	bytes_ps /= utim;
4264 	if (bytes_ps > rack->r_ctl.last_max_bw) {
4265 		/*
4266 		 * Something is on path playing
4267 		 * since this b/w is not possible based
4268 		 * on our BDP (highest rwnd and lowest rtt
4269 		 * we saw in the measurement window).
4270 		 *
4271 		 * Another option here would be to
4272 		 * instead skip the measurement.
4273 		 */
4274 		rack_log_pacing_delay_calc(rack, bytes, reqbytes,
4275 					   bytes_ps, rack->r_ctl.last_max_bw, 0,
4276 					   11, __LINE__, NULL, quality);
4277 		bytes_ps = rack->r_ctl.last_max_bw;
4278 	}
4279 	/* We store gp for b/w in bytes per second */
4280 	if (rack->rc_gp_filled == 0) {
4281 		/* Initial measurement */
4282 		if (bytes_ps) {
4283 			rack->r_ctl.gp_bw = bytes_ps;
4284 			rack->rc_gp_filled = 1;
4285 			rack->r_ctl.num_measurements = 1;
4286 			rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
4287 		} else {
4288 			rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4289 						   rack->r_ctl.rc_app_limited_cnt,
4290 						   0, 0, 10, __LINE__, NULL, quality);
4291 		}
4292 		if (tcp_in_hpts(rack->rc_inp) &&
4293 		    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
4294 			/*
4295 			 * Ok we can't trust the pacer in this case
4296 			 * where we transition from un-paced to paced.
4297 			 * Or for that matter when the burst mitigation
4298 			 * was making a wild guess and got it wrong.
4299 			 * Stop the pacer and clear up all the aggregate
4300 			 * delays etc.
4301 			 */
4302 			tcp_hpts_remove(rack->rc_inp);
4303 			rack->r_ctl.rc_hpts_flags = 0;
4304 			rack->r_ctl.rc_last_output_to = 0;
4305 		}
4306 		did_add = 2;
4307 	} else if (rack->r_ctl.num_measurements < RACK_REQ_AVG) {
4308 		/* Still a small number run an average */
4309 		rack->r_ctl.gp_bw += bytes_ps;
4310 		addpart = rack->r_ctl.num_measurements;
4311 		rack->r_ctl.num_measurements++;
4312 		if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) {
4313 			/* We have collected enough to move forward */
4314 			rack->r_ctl.gp_bw /= (uint64_t)rack->r_ctl.num_measurements;
4315 		}
4316 		did_add = 3;
4317 	} else {
4318 		/*
4319 		 * We want to take 1/wma of the goodput and add in to 7/8th
4320 		 * of the old value weighted by the srtt. So if your measurement
4321 		 * period is say 2 SRTT's long you would get 1/4 as the
4322 		 * value, if it was like 1/2 SRTT then you would get 1/16th.
4323 		 *
4324 		 * But we must be careful not to take too much i.e. if the
4325 		 * srtt is say 20ms and the measurement is taken over
4326 		 * 400ms our weight would be 400/20 i.e. 20. On the
4327 		 * other hand if we get a measurement over 1ms with a
4328 		 * 10ms rtt we only want to take a much smaller portion.
4329 		 */
4330 		if (rack->r_ctl.num_measurements < 0xff) {
4331 			rack->r_ctl.num_measurements++;
4332 		}
4333 		srtt = (uint64_t)tp->t_srtt;
4334 		if (srtt == 0) {
4335 			/*
4336 			 * Strange why did t_srtt go back to zero?
4337 			 */
4338 			if (rack->r_ctl.rc_rack_min_rtt)
4339 				srtt = rack->r_ctl.rc_rack_min_rtt;
4340 			else
4341 				srtt = HPTS_USEC_IN_MSEC;
4342 		}
4343 		/*
4344 		 * XXXrrs: Note for reviewers, in playing with
4345 		 * dynamic pacing I discovered this GP calculation
4346 		 * as done originally leads to some undesired results.
4347 		 * Basically you can get longer measurements contributing
4348 		 * too much to the WMA. Thus I changed it if you are doing
4349 		 * dynamic adjustments to only do the aportioned adjustment
4350 		 * if we have a very small (time wise) measurement. Longer
4351 		 * measurements just get there weight (defaulting to 1/8)
4352 		 * add to the WMA. We may want to think about changing
4353 		 * this to always do that for both sides i.e. dynamic
4354 		 * and non-dynamic... but considering lots of folks
4355 		 * were playing with this I did not want to change the
4356 		 * calculation per.se. without your thoughts.. Lawerence?
4357 		 * Peter??
4358 		 */
4359 		if (rack->rc_gp_dyn_mul == 0) {
4360 			subpart = rack->r_ctl.gp_bw * utim;
4361 			subpart /= (srtt * 8);
4362 			if (subpart < (rack->r_ctl.gp_bw / 2)) {
4363 				/*
4364 				 * The b/w update takes no more
4365 				 * away then 1/2 our running total
4366 				 * so factor it in.
4367 				 */
4368 				addpart = bytes_ps * utim;
4369 				addpart /= (srtt * 8);
4370 			} else {
4371 				/*
4372 				 * Don't allow a single measurement
4373 				 * to account for more than 1/2 of the
4374 				 * WMA. This could happen on a retransmission
4375 				 * where utim becomes huge compared to
4376 				 * srtt (multiple retransmissions when using
4377 				 * the sending rate which factors in all the
4378 				 * transmissions from the first one).
4379 				 */
4380 				subpart = rack->r_ctl.gp_bw / 2;
4381 				addpart = bytes_ps / 2;
4382 			}
4383 			resid_bw = rack->r_ctl.gp_bw - subpart;
4384 			rack->r_ctl.gp_bw = resid_bw + addpart;
4385 			did_add = 1;
4386 		} else {
4387 			if ((utim / srtt) <= 1) {
4388 				/*
4389 				 * The b/w update was over a small period
4390 				 * of time. The idea here is to prevent a small
4391 				 * measurement time period from counting
4392 				 * too much. So we scale it based on the
4393 				 * time so it attributes less than 1/rack_wma_divisor
4394 				 * of its measurement.
4395 				 */
4396 				subpart = rack->r_ctl.gp_bw * utim;
4397 				subpart /= (srtt * rack_wma_divisor);
4398 				addpart = bytes_ps * utim;
4399 				addpart /= (srtt * rack_wma_divisor);
4400 			} else {
4401 				/*
4402 				 * The scaled measurement was long
4403 				 * enough so lets just add in the
4404 				 * portion of the measurement i.e. 1/rack_wma_divisor
4405 				 */
4406 				subpart = rack->r_ctl.gp_bw / rack_wma_divisor;
4407 				addpart = bytes_ps / rack_wma_divisor;
4408 			}
4409 			if ((rack->measure_saw_probe_rtt == 0) ||
4410 		            (bytes_ps > rack->r_ctl.gp_bw)) {
4411 				/*
4412 				 * For probe-rtt we only add it in
4413 				 * if its larger, all others we just
4414 				 * add in.
4415 				 */
4416 				did_add = 1;
4417 				resid_bw = rack->r_ctl.gp_bw - subpart;
4418 				rack->r_ctl.gp_bw = resid_bw + addpart;
4419 			}
4420 		}
4421 	}
4422 	if ((rack->gp_ready == 0) &&
4423 	    (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) {
4424 		/* We have enough measurements now */
4425 		rack->gp_ready = 1;
4426 		rack_set_cc_pacing(rack);
4427 		if (rack->defer_options)
4428 			rack_apply_deferred_options(rack);
4429 	}
4430 	rack_log_pacing_delay_calc(rack, subpart, addpart, bytes_ps, stim,
4431 				   rack_get_bw(rack), 22, did_add, NULL, quality);
4432 	/* We do not update any multipliers if we are in or have seen a probe-rtt */
4433 	if ((rack->measure_saw_probe_rtt == 0) && rack->rc_gp_rtt_set)
4434 		rack_update_multiplier(rack, timely_says, bytes_ps,
4435 				       rack->r_ctl.rc_gp_srtt,
4436 				       rack->r_ctl.rc_rtt_diff);
4437 	rack_log_pacing_delay_calc(rack, bytes, tim, bytes_ps, stim,
4438 				   rack_get_bw(rack), 3, line, NULL, quality);
4439 	/* reset the gp srtt and setup the new prev */
4440 	rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt;
4441 	/* Record the lost count for the next measurement */
4442 	rack->r_ctl.rc_loss_at_start = rack->r_ctl.rc_loss_count;
4443 	/*
4444 	 * We restart our diffs based on the gpsrtt in the
4445 	 * measurement window.
4446 	 */
4447 	rack->rc_gp_rtt_set = 0;
4448 	rack->rc_gp_saw_rec = 0;
4449 	rack->rc_gp_saw_ca = 0;
4450 	rack->rc_gp_saw_ss = 0;
4451 	rack->rc_dragged_bottom = 0;
4452 skip_measurement:
4453 
4454 #ifdef STATS
4455 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
4456 				 gput);
4457 	/*
4458 	 * XXXLAS: This is a temporary hack, and should be
4459 	 * chained off VOI_TCP_GPUT when stats(9) grows an
4460 	 * API to deal with chained VOIs.
4461 	 */
4462 	if (tp->t_stats_gput_prev > 0)
4463 		stats_voi_update_abs_s32(tp->t_stats,
4464 					 VOI_TCP_GPUT_ND,
4465 					 ((gput - tp->t_stats_gput_prev) * 100) /
4466 					 tp->t_stats_gput_prev);
4467 #endif
4468 	tp->t_flags &= ~TF_GPUTINPROG;
4469 	tp->t_stats_gput_prev = gput;
4470 	/*
4471 	 * Now are we app limited now and there is space from where we
4472 	 * were to where we want to go?
4473 	 *
4474 	 * We don't do the other case i.e. non-applimited here since
4475 	 * the next send will trigger us picking up the missing data.
4476 	 */
4477 	if (rack->r_ctl.rc_first_appl &&
4478 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
4479 	    rack->r_ctl.rc_app_limited_cnt &&
4480 	    (SEQ_GT(rack->r_ctl.rc_first_appl->r_start, th_ack)) &&
4481 	    ((rack->r_ctl.rc_first_appl->r_end - th_ack) >
4482 	     max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) {
4483 		/*
4484 		 * Yep there is enough outstanding to make a measurement here.
4485 		 */
4486 		struct rack_sendmap *rsm, fe;
4487 
4488 		rack->r_ctl.rc_gp_lowrtt = 0xffffffff;
4489 		rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
4490 		tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
4491 		rack->app_limited_needs_set = 0;
4492 		tp->gput_seq = th_ack;
4493 		if (rack->in_probe_rtt)
4494 			rack->measure_saw_probe_rtt = 1;
4495 		else if ((rack->measure_saw_probe_rtt) &&
4496 			 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
4497 			rack->measure_saw_probe_rtt = 0;
4498 		if ((rack->r_ctl.rc_first_appl->r_end - th_ack) >= rack_get_measure_window(tp, rack)) {
4499 			/* There is a full window to gain info from */
4500 			tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
4501 		} else {
4502 			/* We can only measure up to the applimited point */
4503 			tp->gput_ack = tp->gput_seq + (rack->r_ctl.rc_first_appl->r_end - th_ack);
4504 			if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) {
4505 				/*
4506 				 * We don't have enough to make a measurement.
4507 				 */
4508 				tp->t_flags &= ~TF_GPUTINPROG;
4509 				rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq,
4510 							   0, 0, 0, 6, __LINE__, NULL, quality);
4511 				return;
4512 			}
4513 		}
4514 		if (tp->t_state >= TCPS_FIN_WAIT_1) {
4515 			/*
4516 			 * We will get no more data into the SB
4517 			 * this means we need to have the data available
4518 			 * before we start a measurement.
4519 			 */
4520 			if (sbavail(&tp->t_inpcb->inp_socket->so_snd) < (tp->gput_ack - tp->gput_seq)) {
4521 				/* Nope not enough data. */
4522 				return;
4523 			}
4524 		}
4525 		tp->t_flags |= TF_GPUTINPROG;
4526 		/*
4527 		 * Now we need to find the timestamp of the send at tp->gput_seq
4528 		 * for the send based measurement.
4529 		 */
4530 		fe.r_start = tp->gput_seq;
4531 		rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
4532 		if (rsm) {
4533 			/* Ok send-based limit is set */
4534 			if (SEQ_LT(rsm->r_start, tp->gput_seq)) {
4535 				/*
4536 				 * Move back to include the earlier part
4537 				 * so our ack time lines up right (this may
4538 				 * make an overlapping measurement but thats
4539 				 * ok).
4540 				 */
4541 				tp->gput_seq = rsm->r_start;
4542 			}
4543 			if (rsm->r_flags & RACK_ACKED)
4544 				tp->gput_ts = (uint32_t)rsm->r_ack_arrival;
4545 			else
4546 				rack->app_limited_needs_set = 1;
4547 			rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
4548 		} else {
4549 			/*
4550 			 * If we don't find the rsm due to some
4551 			 * send-limit set the current time, which
4552 			 * basically disables the send-limit.
4553 			 */
4554 			struct timeval tv;
4555 
4556 			microuptime(&tv);
4557 			rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
4558 		}
4559 		rack_log_pacing_delay_calc(rack,
4560 					   tp->gput_seq,
4561 					   tp->gput_ack,
4562 					   (uint64_t)rsm,
4563 					   tp->gput_ts,
4564 					   rack->r_ctl.rc_app_limited_cnt,
4565 					   9,
4566 					   __LINE__, NULL, quality);
4567 	}
4568 }
4569 
4570 /*
4571  * CC wrapper hook functions
4572  */
4573 static void
4574 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack, uint32_t th_ack, uint16_t nsegs,
4575     uint16_t type, int32_t recovery)
4576 {
4577 	uint32_t prior_cwnd, acked;
4578 	struct tcp_log_buffer *lgb = NULL;
4579 	uint8_t labc_to_use, quality;
4580 
4581 	INP_WLOCK_ASSERT(tp->t_inpcb);
4582 	tp->ccv->nsegs = nsegs;
4583 	acked = tp->ccv->bytes_this_ack = (th_ack - tp->snd_una);
4584 	if ((recovery) && (rack->r_ctl.rc_early_recovery_segs)) {
4585 		uint32_t max;
4586 
4587 		max = rack->r_ctl.rc_early_recovery_segs * ctf_fixed_maxseg(tp);
4588 		if (tp->ccv->bytes_this_ack > max) {
4589 			tp->ccv->bytes_this_ack = max;
4590 		}
4591 	}
4592 #ifdef STATS
4593 	stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF,
4594 	    ((int32_t)rack->r_ctl.cwnd_to_use) - tp->snd_wnd);
4595 #endif
4596 	quality = RACK_QUALITY_NONE;
4597 	if ((tp->t_flags & TF_GPUTINPROG) &&
4598 	    rack_enough_for_measurement(tp, rack, th_ack, &quality)) {
4599 		/* Measure the Goodput */
4600 		rack_do_goodput_measurement(tp, rack, th_ack, __LINE__, quality);
4601 #ifdef NETFLIX_PEAKRATE
4602 		if ((type == CC_ACK) &&
4603 		    (tp->t_maxpeakrate)) {
4604 			/*
4605 			 * We update t_peakrate_thr. This gives us roughly
4606 			 * one update per round trip time. Note
4607 			 * it will only be used if pace_always is off i.e
4608 			 * we don't do this for paced flows.
4609 			 */
4610 			rack_update_peakrate_thr(tp);
4611 		}
4612 #endif
4613 	}
4614 	/* Which way our we limited, if not cwnd limited no advance in CA */
4615 	if (tp->snd_cwnd <= tp->snd_wnd)
4616 		tp->ccv->flags |= CCF_CWND_LIMITED;
4617 	else
4618 		tp->ccv->flags &= ~CCF_CWND_LIMITED;
4619 	if (tp->snd_cwnd > tp->snd_ssthresh) {
4620 		tp->t_bytes_acked += min(tp->ccv->bytes_this_ack,
4621 			 nsegs * V_tcp_abc_l_var * ctf_fixed_maxseg(tp));
4622 		/* For the setting of a window past use the actual scwnd we are using */
4623 		if (tp->t_bytes_acked >= rack->r_ctl.cwnd_to_use) {
4624 			tp->t_bytes_acked -= rack->r_ctl.cwnd_to_use;
4625 			tp->ccv->flags |= CCF_ABC_SENTAWND;
4626 		}
4627 	} else {
4628 		tp->ccv->flags &= ~CCF_ABC_SENTAWND;
4629 		tp->t_bytes_acked = 0;
4630 	}
4631 	prior_cwnd = tp->snd_cwnd;
4632 	if ((recovery == 0) || (rack_max_abc_post_recovery == 0) || rack->r_use_labc_for_rec ||
4633 	    (rack_client_low_buf && (rack->client_bufferlvl < rack_client_low_buf)))
4634 		labc_to_use = rack->rc_labc;
4635 	else
4636 		labc_to_use = rack_max_abc_post_recovery;
4637 	if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
4638 		union tcp_log_stackspecific log;
4639 		struct timeval tv;
4640 
4641 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
4642 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
4643 		log.u_bbr.flex1 = th_ack;
4644 		log.u_bbr.flex2 = tp->ccv->flags;
4645 		log.u_bbr.flex3 = tp->ccv->bytes_this_ack;
4646 		log.u_bbr.flex4 = tp->ccv->nsegs;
4647 		log.u_bbr.flex5 = labc_to_use;
4648 		log.u_bbr.flex6 = prior_cwnd;
4649 		log.u_bbr.flex7 = V_tcp_do_newsack;
4650 		log.u_bbr.flex8 = 1;
4651 		lgb = tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
4652 				     0, &log, false, NULL, NULL, 0, &tv);
4653 	}
4654 	if (CC_ALGO(tp)->ack_received != NULL) {
4655 		/* XXXLAS: Find a way to live without this */
4656 		tp->ccv->curack = th_ack;
4657 		tp->ccv->labc = labc_to_use;
4658 		tp->ccv->flags |= CCF_USE_LOCAL_ABC;
4659 		CC_ALGO(tp)->ack_received(tp->ccv, type);
4660 	}
4661 	if (lgb) {
4662 		lgb->tlb_stackinfo.u_bbr.flex6 = tp->snd_cwnd;
4663 	}
4664 	if (rack->r_must_retran) {
4665 		if (SEQ_GEQ(th_ack, rack->r_ctl.rc_snd_max_at_rto)) {
4666 			/*
4667 			 * We now are beyond the rxt point so lets disable
4668 			 * the flag.
4669 			 */
4670 			rack->r_ctl.rc_out_at_rto = 0;
4671 			rack->r_must_retran = 0;
4672 		} else if ((prior_cwnd + ctf_fixed_maxseg(tp)) <= tp->snd_cwnd) {
4673 			/*
4674 			 * Only decrement the rc_out_at_rto if the cwnd advances
4675 			 * at least a whole segment. Otherwise next time the peer
4676 			 * acks, we won't be able to send this generaly happens
4677 			 * when we are in Congestion Avoidance.
4678 			 */
4679 			if (acked <= rack->r_ctl.rc_out_at_rto){
4680 				rack->r_ctl.rc_out_at_rto -= acked;
4681 			} else {
4682 				rack->r_ctl.rc_out_at_rto = 0;
4683 			}
4684 		}
4685 	}
4686 #ifdef STATS
4687 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, rack->r_ctl.cwnd_to_use);
4688 #endif
4689 	if (rack->r_ctl.rc_rack_largest_cwnd < rack->r_ctl.cwnd_to_use) {
4690 		rack->r_ctl.rc_rack_largest_cwnd = rack->r_ctl.cwnd_to_use;
4691 	}
4692 #ifdef NETFLIX_PEAKRATE
4693 	/* we enforce max peak rate if it is set and we are not pacing */
4694 	if ((rack->rc_always_pace == 0) &&
4695 	    tp->t_peakrate_thr &&
4696 	    (tp->snd_cwnd > tp->t_peakrate_thr)) {
4697 		tp->snd_cwnd = tp->t_peakrate_thr;
4698 	}
4699 #endif
4700 }
4701 
4702 static void
4703 tcp_rack_partialack(struct tcpcb *tp)
4704 {
4705 	struct tcp_rack *rack;
4706 
4707 	rack = (struct tcp_rack *)tp->t_fb_ptr;
4708 	INP_WLOCK_ASSERT(tp->t_inpcb);
4709 	/*
4710 	 * If we are doing PRR and have enough
4711 	 * room to send <or> we are pacing and prr
4712 	 * is disabled we will want to see if we
4713 	 * can send data (by setting r_wanted_output to
4714 	 * true).
4715 	 */
4716 	if ((rack->r_ctl.rc_prr_sndcnt > 0) ||
4717 	    rack->rack_no_prr)
4718 		rack->r_wanted_output = 1;
4719 }
4720 
4721 static void
4722 rack_post_recovery(struct tcpcb *tp, uint32_t th_ack)
4723 {
4724 	struct tcp_rack *rack;
4725 	uint32_t orig_cwnd;
4726 
4727 	orig_cwnd = tp->snd_cwnd;
4728 	INP_WLOCK_ASSERT(tp->t_inpcb);
4729 	rack = (struct tcp_rack *)tp->t_fb_ptr;
4730 	/* only alert CC if we alerted when we entered */
4731 	if (CC_ALGO(tp)->post_recovery != NULL) {
4732 		tp->ccv->curack = th_ack;
4733 		CC_ALGO(tp)->post_recovery(tp->ccv);
4734 		if (tp->snd_cwnd < tp->snd_ssthresh) {
4735 			/*
4736 			 * Rack has burst control and pacing
4737 			 * so lets not set this any lower than
4738 			 * snd_ssthresh per RFC-6582 (option 2).
4739 			 */
4740 			tp->snd_cwnd = tp->snd_ssthresh;
4741 		}
4742 	}
4743 	if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
4744 		union tcp_log_stackspecific log;
4745 		struct timeval tv;
4746 
4747 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
4748 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
4749 		log.u_bbr.flex1 = th_ack;
4750 		log.u_bbr.flex2 = tp->ccv->flags;
4751 		log.u_bbr.flex3 = tp->ccv->bytes_this_ack;
4752 		log.u_bbr.flex4 = tp->ccv->nsegs;
4753 		log.u_bbr.flex5 = V_tcp_abc_l_var;
4754 		log.u_bbr.flex6 = orig_cwnd;
4755 		log.u_bbr.flex7 = V_tcp_do_newsack;
4756 		log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
4757 		log.u_bbr.flex8 = 2;
4758 		tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
4759 			       0, &log, false, NULL, NULL, 0, &tv);
4760 	}
4761 	if ((rack->rack_no_prr == 0) &&
4762 	    (rack->no_prr_addback == 0) &&
4763 	    (rack->r_ctl.rc_prr_sndcnt > 0)) {
4764 		/*
4765 		 * Suck the next prr cnt back into cwnd, but
4766 		 * only do that if we are not application limited.
4767 		 */
4768 		if (ctf_outstanding(tp) <= sbavail(&(tp->t_inpcb->inp_socket->so_snd))) {
4769 			/*
4770 			 * We are allowed to add back to the cwnd the amount we did
4771 			 * not get out if:
4772 			 * a) no_prr_addback is off.
4773 			 * b) we are not app limited
4774 			 * c) we are doing prr
4775 			 * <and>
4776 			 * d) it is bounded by rack_prr_addbackmax (if addback is 0, then none).
4777 			 */
4778 			tp->snd_cwnd += min((ctf_fixed_maxseg(tp) * rack_prr_addbackmax),
4779 					    rack->r_ctl.rc_prr_sndcnt);
4780 		}
4781 		rack->r_ctl.rc_prr_sndcnt = 0;
4782 		rack_log_to_prr(rack, 1, 0, __LINE__);
4783 	}
4784 	rack_log_to_prr(rack, 14, orig_cwnd, __LINE__);
4785 	tp->snd_recover = tp->snd_una;
4786 	if (rack->r_ctl.dsack_persist) {
4787 		rack->r_ctl.dsack_persist--;
4788 		if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
4789 			rack->r_ctl.num_dsack = 0;
4790 		}
4791 		rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
4792 	}
4793 	EXIT_RECOVERY(tp->t_flags);
4794 }
4795 
4796 static void
4797 rack_cong_signal(struct tcpcb *tp, uint32_t type, uint32_t ack, int line)
4798 {
4799 	struct tcp_rack *rack;
4800 	uint32_t ssthresh_enter, cwnd_enter, in_rec_at_entry, orig_cwnd;
4801 
4802 	INP_WLOCK_ASSERT(tp->t_inpcb);
4803 #ifdef STATS
4804 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type);
4805 #endif
4806 	if (IN_RECOVERY(tp->t_flags) == 0) {
4807 		in_rec_at_entry = 0;
4808 		ssthresh_enter = tp->snd_ssthresh;
4809 		cwnd_enter = tp->snd_cwnd;
4810 	} else
4811 		in_rec_at_entry = 1;
4812 	rack = (struct tcp_rack *)tp->t_fb_ptr;
4813 	switch (type) {
4814 	case CC_NDUPACK:
4815 		tp->t_flags &= ~TF_WASFRECOVERY;
4816 		tp->t_flags &= ~TF_WASCRECOVERY;
4817 		if (!IN_FASTRECOVERY(tp->t_flags)) {
4818 			rack->r_ctl.rc_prr_delivered = 0;
4819 			rack->r_ctl.rc_prr_out = 0;
4820 			if (rack->rack_no_prr == 0) {
4821 				rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
4822 				rack_log_to_prr(rack, 2, in_rec_at_entry, line);
4823 			}
4824 			rack->r_ctl.rc_prr_recovery_fs = tp->snd_max - tp->snd_una;
4825 			tp->snd_recover = tp->snd_max;
4826 			if (tp->t_flags2 & TF2_ECN_PERMIT)
4827 				tp->t_flags2 |= TF2_ECN_SND_CWR;
4828 		}
4829 		break;
4830 	case CC_ECN:
4831 		if (!IN_CONGRECOVERY(tp->t_flags) ||
4832 		    /*
4833 		     * Allow ECN reaction on ACK to CWR, if
4834 		     * that data segment was also CE marked.
4835 		     */
4836 		    SEQ_GEQ(ack, tp->snd_recover)) {
4837 			EXIT_CONGRECOVERY(tp->t_flags);
4838 			KMOD_TCPSTAT_INC(tcps_ecn_rcwnd);
4839 			tp->snd_recover = tp->snd_max + 1;
4840 			if (tp->t_flags2 & TF2_ECN_PERMIT)
4841 				tp->t_flags2 |= TF2_ECN_SND_CWR;
4842 		}
4843 		break;
4844 	case CC_RTO:
4845 		tp->t_dupacks = 0;
4846 		tp->t_bytes_acked = 0;
4847 		EXIT_RECOVERY(tp->t_flags);
4848 		tp->snd_ssthresh = max(2, min(tp->snd_wnd, rack->r_ctl.cwnd_to_use) / 2 /
4849 		    ctf_fixed_maxseg(tp)) * ctf_fixed_maxseg(tp);
4850 		orig_cwnd = tp->snd_cwnd;
4851 		tp->snd_cwnd = ctf_fixed_maxseg(tp);
4852 		rack_log_to_prr(rack, 16, orig_cwnd, line);
4853 		if (tp->t_flags2 & TF2_ECN_PERMIT)
4854 			tp->t_flags2 |= TF2_ECN_SND_CWR;
4855 		break;
4856 	case CC_RTO_ERR:
4857 		KMOD_TCPSTAT_INC(tcps_sndrexmitbad);
4858 		/* RTO was unnecessary, so reset everything. */
4859 		tp->snd_cwnd = tp->snd_cwnd_prev;
4860 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
4861 		tp->snd_recover = tp->snd_recover_prev;
4862 		if (tp->t_flags & TF_WASFRECOVERY) {
4863 			ENTER_FASTRECOVERY(tp->t_flags);
4864 			tp->t_flags &= ~TF_WASFRECOVERY;
4865 		}
4866 		if (tp->t_flags & TF_WASCRECOVERY) {
4867 			ENTER_CONGRECOVERY(tp->t_flags);
4868 			tp->t_flags &= ~TF_WASCRECOVERY;
4869 		}
4870 		tp->snd_nxt = tp->snd_max;
4871 		tp->t_badrxtwin = 0;
4872 		break;
4873 	}
4874 	if ((CC_ALGO(tp)->cong_signal != NULL)  &&
4875 	    (type != CC_RTO)){
4876 		tp->ccv->curack = ack;
4877 		CC_ALGO(tp)->cong_signal(tp->ccv, type);
4878 	}
4879 	if ((in_rec_at_entry == 0) && IN_RECOVERY(tp->t_flags)) {
4880 		rack_log_to_prr(rack, 15, cwnd_enter, line);
4881 		rack->r_ctl.dsack_byte_cnt = 0;
4882 		rack->r_ctl.retran_during_recovery = 0;
4883 		rack->r_ctl.rc_cwnd_at_erec = cwnd_enter;
4884 		rack->r_ctl.rc_ssthresh_at_erec = ssthresh_enter;
4885 		rack->r_ent_rec_ns = 1;
4886 	}
4887 }
4888 
4889 static inline void
4890 rack_cc_after_idle(struct tcp_rack *rack, struct tcpcb *tp)
4891 {
4892 	uint32_t i_cwnd;
4893 
4894 	INP_WLOCK_ASSERT(tp->t_inpcb);
4895 
4896 #ifdef NETFLIX_STATS
4897 	KMOD_TCPSTAT_INC(tcps_idle_restarts);
4898 	if (tp->t_state == TCPS_ESTABLISHED)
4899 		KMOD_TCPSTAT_INC(tcps_idle_estrestarts);
4900 #endif
4901 	if (CC_ALGO(tp)->after_idle != NULL)
4902 		CC_ALGO(tp)->after_idle(tp->ccv);
4903 
4904 	if (tp->snd_cwnd == 1)
4905 		i_cwnd = tp->t_maxseg;		/* SYN(-ACK) lost */
4906 	else
4907 		i_cwnd = rc_init_window(rack);
4908 
4909 	/*
4910 	 * Being idle is no different than the initial window. If the cc
4911 	 * clamps it down below the initial window raise it to the initial
4912 	 * window.
4913 	 */
4914 	if (tp->snd_cwnd < i_cwnd) {
4915 		tp->snd_cwnd = i_cwnd;
4916 	}
4917 }
4918 
4919 /*
4920  * Indicate whether this ack should be delayed.  We can delay the ack if
4921  * following conditions are met:
4922  *	- There is no delayed ack timer in progress.
4923  *	- Our last ack wasn't a 0-sized window. We never want to delay
4924  *	  the ack that opens up a 0-sized window.
4925  *	- LRO wasn't used for this segment. We make sure by checking that the
4926  *	  segment size is not larger than the MSS.
4927  *	- Delayed acks are enabled or this is a half-synchronized T/TCP
4928  *	  connection.
4929  */
4930 #define DELAY_ACK(tp, tlen)			 \
4931 	(((tp->t_flags & TF_RXWIN0SENT) == 0) && \
4932 	((tp->t_flags & TF_DELACK) == 0) &&	 \
4933 	(tlen <= tp->t_maxseg) &&		 \
4934 	(tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN)))
4935 
4936 static struct rack_sendmap *
4937 rack_find_lowest_rsm(struct tcp_rack *rack)
4938 {
4939 	struct rack_sendmap *rsm;
4940 
4941 	/*
4942 	 * Walk the time-order transmitted list looking for an rsm that is
4943 	 * not acked. This will be the one that was sent the longest time
4944 	 * ago that is still outstanding.
4945 	 */
4946 	TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
4947 		if (rsm->r_flags & RACK_ACKED) {
4948 			continue;
4949 		}
4950 		goto finish;
4951 	}
4952 finish:
4953 	return (rsm);
4954 }
4955 
4956 static struct rack_sendmap *
4957 rack_find_high_nonack(struct tcp_rack *rack, struct rack_sendmap *rsm)
4958 {
4959 	struct rack_sendmap *prsm;
4960 
4961 	/*
4962 	 * Walk the sequence order list backward until we hit and arrive at
4963 	 * the highest seq not acked. In theory when this is called it
4964 	 * should be the last segment (which it was not).
4965 	 */
4966 	prsm = rsm;
4967 	RB_FOREACH_REVERSE_FROM(prsm, rack_rb_tree_head, rsm) {
4968 		if (prsm->r_flags & (RACK_ACKED | RACK_HAS_FIN)) {
4969 			continue;
4970 		}
4971 		return (prsm);
4972 	}
4973 	return (NULL);
4974 }
4975 
4976 static uint32_t
4977 rack_calc_thresh_rack(struct tcp_rack *rack, uint32_t srtt, uint32_t cts)
4978 {
4979 	int32_t lro;
4980 	uint32_t thresh;
4981 
4982 	/*
4983 	 * lro is the flag we use to determine if we have seen reordering.
4984 	 * If it gets set we have seen reordering. The reorder logic either
4985 	 * works in one of two ways:
4986 	 *
4987 	 * If reorder-fade is configured, then we track the last time we saw
4988 	 * re-ordering occur. If we reach the point where enough time as
4989 	 * passed we no longer consider reordering has occuring.
4990 	 *
4991 	 * Or if reorder-face is 0, then once we see reordering we consider
4992 	 * the connection to alway be subject to reordering and just set lro
4993 	 * to 1.
4994 	 *
4995 	 * In the end if lro is non-zero we add the extra time for
4996 	 * reordering in.
4997 	 */
4998 	if (srtt == 0)
4999 		srtt = 1;
5000 	if (rack->r_ctl.rc_reorder_ts) {
5001 		if (rack->r_ctl.rc_reorder_fade) {
5002 			if (SEQ_GEQ(cts, rack->r_ctl.rc_reorder_ts)) {
5003 				lro = cts - rack->r_ctl.rc_reorder_ts;
5004 				if (lro == 0) {
5005 					/*
5006 					 * No time as passed since the last
5007 					 * reorder, mark it as reordering.
5008 					 */
5009 					lro = 1;
5010 				}
5011 			} else {
5012 				/* Negative time? */
5013 				lro = 0;
5014 			}
5015 			if (lro > rack->r_ctl.rc_reorder_fade) {
5016 				/* Turn off reordering seen too */
5017 				rack->r_ctl.rc_reorder_ts = 0;
5018 				lro = 0;
5019 			}
5020 		} else {
5021 			/* Reodering does not fade */
5022 			lro = 1;
5023 		}
5024 	} else {
5025 		lro = 0;
5026 	}
5027 	if (rack->rc_rack_tmr_std_based == 0) {
5028 		thresh = srtt + rack->r_ctl.rc_pkt_delay;
5029 	} else {
5030 		/* Standards based pkt-delay is 1/4 srtt */
5031 		thresh = srtt +  (srtt >> 2);
5032 	}
5033 	if (lro && (rack->rc_rack_tmr_std_based == 0)) {
5034 		/* It must be set, if not you get 1/4 rtt */
5035 		if (rack->r_ctl.rc_reorder_shift)
5036 			thresh += (srtt >> rack->r_ctl.rc_reorder_shift);
5037 		else
5038 			thresh += (srtt >> 2);
5039 	}
5040 	if (rack->rc_rack_use_dsack &&
5041 	    lro &&
5042 	    (rack->r_ctl.num_dsack > 0)) {
5043 		/*
5044 		 * We only increase the reordering window if we
5045 		 * have seen reordering <and> we have a DSACK count.
5046 		 */
5047 		thresh += rack->r_ctl.num_dsack * (srtt >> 2);
5048 		rack_log_dsack_event(rack, 4, __LINE__, srtt, thresh);
5049 	}
5050 	/* SRTT * 2 is the ceiling */
5051 	if (thresh > (srtt * 2)) {
5052 		thresh = srtt * 2;
5053 	}
5054 	/* And we don't want it above the RTO max either */
5055 	if (thresh > rack_rto_max) {
5056 		thresh = rack_rto_max;
5057 	}
5058 	rack_log_dsack_event(rack, 6, __LINE__, srtt, thresh);
5059 	return (thresh);
5060 }
5061 
5062 static uint32_t
5063 rack_calc_thresh_tlp(struct tcpcb *tp, struct tcp_rack *rack,
5064 		     struct rack_sendmap *rsm, uint32_t srtt)
5065 {
5066 	struct rack_sendmap *prsm;
5067 	uint32_t thresh, len;
5068 	int segsiz;
5069 
5070 	if (srtt == 0)
5071 		srtt = 1;
5072 	if (rack->r_ctl.rc_tlp_threshold)
5073 		thresh = srtt + (srtt / rack->r_ctl.rc_tlp_threshold);
5074 	else
5075 		thresh = (srtt * 2);
5076 
5077 	/* Get the previous sent packet, if any */
5078 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
5079 	len = rsm->r_end - rsm->r_start;
5080 	if (rack->rack_tlp_threshold_use == TLP_USE_ID) {
5081 		/* Exactly like the ID */
5082 		if (((tp->snd_max - tp->snd_una) - rack->r_ctl.rc_sacked + rack->r_ctl.rc_holes_rxt) <= segsiz) {
5083 			uint32_t alt_thresh;
5084 			/*
5085 			 * Compensate for delayed-ack with the d-ack time.
5086 			 */
5087 			alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
5088 			if (alt_thresh > thresh)
5089 				thresh = alt_thresh;
5090 		}
5091 	} else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_ONE) {
5092 		/* 2.1 behavior */
5093 		prsm = TAILQ_PREV(rsm, rack_head, r_tnext);
5094 		if (prsm && (len <= segsiz)) {
5095 			/*
5096 			 * Two packets outstanding, thresh should be (2*srtt) +
5097 			 * possible inter-packet delay (if any).
5098 			 */
5099 			uint32_t inter_gap = 0;
5100 			int idx, nidx;
5101 
5102 			idx = rsm->r_rtr_cnt - 1;
5103 			nidx = prsm->r_rtr_cnt - 1;
5104 			if (rsm->r_tim_lastsent[nidx] >= prsm->r_tim_lastsent[idx]) {
5105 				/* Yes it was sent later (or at the same time) */
5106 				inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx];
5107 			}
5108 			thresh += inter_gap;
5109 		} else if (len <= segsiz) {
5110 			/*
5111 			 * Possibly compensate for delayed-ack.
5112 			 */
5113 			uint32_t alt_thresh;
5114 
5115 			alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
5116 			if (alt_thresh > thresh)
5117 				thresh = alt_thresh;
5118 		}
5119 	} else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_TWO) {
5120 		/* 2.2 behavior */
5121 		if (len <= segsiz) {
5122 			uint32_t alt_thresh;
5123 			/*
5124 			 * Compensate for delayed-ack with the d-ack time.
5125 			 */
5126 			alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
5127 			if (alt_thresh > thresh)
5128 				thresh = alt_thresh;
5129 		}
5130 	}
5131 	/* Not above an RTO */
5132 	if (thresh > tp->t_rxtcur) {
5133 		thresh = tp->t_rxtcur;
5134 	}
5135 	/* Not above a RTO max */
5136 	if (thresh > rack_rto_max) {
5137 		thresh = rack_rto_max;
5138 	}
5139 	/* Apply user supplied min TLP */
5140 	if (thresh < rack_tlp_min) {
5141 		thresh = rack_tlp_min;
5142 	}
5143 	return (thresh);
5144 }
5145 
5146 static uint32_t
5147 rack_grab_rtt(struct tcpcb *tp, struct tcp_rack *rack)
5148 {
5149 	/*
5150 	 * We want the rack_rtt which is the
5151 	 * last rtt we measured. However if that
5152 	 * does not exist we fallback to the srtt (which
5153 	 * we probably will never do) and then as a last
5154 	 * resort we use RACK_INITIAL_RTO if no srtt is
5155 	 * yet set.
5156 	 */
5157 	if (rack->rc_rack_rtt)
5158 		return (rack->rc_rack_rtt);
5159 	else if (tp->t_srtt == 0)
5160 		return (RACK_INITIAL_RTO);
5161 	return (tp->t_srtt);
5162 }
5163 
5164 static struct rack_sendmap *
5165 rack_check_recovery_mode(struct tcpcb *tp, uint32_t tsused)
5166 {
5167 	/*
5168 	 * Check to see that we don't need to fall into recovery. We will
5169 	 * need to do so if our oldest transmit is past the time we should
5170 	 * have had an ack.
5171 	 */
5172 	struct tcp_rack *rack;
5173 	struct rack_sendmap *rsm;
5174 	int32_t idx;
5175 	uint32_t srtt, thresh;
5176 
5177 	rack = (struct tcp_rack *)tp->t_fb_ptr;
5178 	if (RB_EMPTY(&rack->r_ctl.rc_mtree)) {
5179 		return (NULL);
5180 	}
5181 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
5182 	if (rsm == NULL)
5183 		return (NULL);
5184 
5185 
5186 	if (rsm->r_flags & RACK_ACKED) {
5187 		rsm = rack_find_lowest_rsm(rack);
5188 		if (rsm == NULL)
5189 			return (NULL);
5190 	}
5191 	idx = rsm->r_rtr_cnt - 1;
5192 	srtt = rack_grab_rtt(tp, rack);
5193 	thresh = rack_calc_thresh_rack(rack, srtt, tsused);
5194 	if (TSTMP_LT(tsused, ((uint32_t)rsm->r_tim_lastsent[idx]))) {
5195 		return (NULL);
5196 	}
5197 	if ((tsused - ((uint32_t)rsm->r_tim_lastsent[idx])) < thresh) {
5198 		return (NULL);
5199 	}
5200 	/* Ok if we reach here we are over-due and this guy can be sent */
5201 	rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__);
5202 	return (rsm);
5203 }
5204 
5205 static uint32_t
5206 rack_get_persists_timer_val(struct tcpcb *tp, struct tcp_rack *rack)
5207 {
5208 	int32_t t;
5209 	int32_t tt;
5210 	uint32_t ret_val;
5211 
5212 	t = (tp->t_srtt + (tp->t_rttvar << 2));
5213 	RACK_TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
5214  	    rack_persist_min, rack_persist_max, rack->r_ctl.timer_slop);
5215 	rack->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT;
5216 	ret_val = (uint32_t)tt;
5217 	return (ret_val);
5218 }
5219 
5220 static uint32_t
5221 rack_timer_start(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int sup_rack)
5222 {
5223 	/*
5224 	 * Start the FR timer, we do this based on getting the first one in
5225 	 * the rc_tmap. Note that if its NULL we must stop the timer. in all
5226 	 * events we need to stop the running timer (if its running) before
5227 	 * starting the new one.
5228 	 */
5229 	uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse;
5230 	uint32_t srtt_cur;
5231 	int32_t idx;
5232 	int32_t is_tlp_timer = 0;
5233 	struct rack_sendmap *rsm;
5234 
5235 	if (rack->t_timers_stopped) {
5236 		/* All timers have been stopped none are to run */
5237 		return (0);
5238 	}
5239 	if (rack->rc_in_persist) {
5240 		/* We can't start any timer in persists */
5241 		return (rack_get_persists_timer_val(tp, rack));
5242 	}
5243 	rack->rc_on_min_to = 0;
5244 	if ((tp->t_state < TCPS_ESTABLISHED) ||
5245 	    ((tp->t_flags & TF_SACK_PERMIT) == 0)) {
5246 		goto activate_rxt;
5247 	}
5248 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
5249 	if ((rsm == NULL) || sup_rack) {
5250 		/* Nothing on the send map or no rack */
5251 activate_rxt:
5252 		time_since_sent = 0;
5253 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
5254 		if (rsm) {
5255 			/*
5256 			 * Should we discount the RTX timer any?
5257 			 *
5258 			 * We want to discount it the smallest amount.
5259 			 * If a timer (Rack/TLP or RXT) has gone off more
5260 			 * recently thats the discount we want to use (now - timer time).
5261 			 * If the retransmit of the oldest packet was more recent then
5262 			 * we want to use that (now - oldest-packet-last_transmit_time).
5263 			 *
5264 			 */
5265 			idx = rsm->r_rtr_cnt - 1;
5266 			if (TSTMP_GEQ(rack->r_ctl.rc_tlp_rxt_last_time, ((uint32_t)rsm->r_tim_lastsent[idx])))
5267 				tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time;
5268 			else
5269 				tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx];
5270 			if (TSTMP_GT(cts, tstmp_touse))
5271 			    time_since_sent = cts - tstmp_touse;
5272 		}
5273 		if (SEQ_LT(tp->snd_una, tp->snd_max) || sbavail(&(tp->t_inpcb->inp_socket->so_snd))) {
5274 			rack->r_ctl.rc_hpts_flags |= PACE_TMR_RXT;
5275 			to = tp->t_rxtcur;
5276 			if (to > time_since_sent)
5277 				to -= time_since_sent;
5278 			else
5279 				to = rack->r_ctl.rc_min_to;
5280 			if (to == 0)
5281 				to = 1;
5282 			/* Special case for KEEPINIT */
5283 			if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) &&
5284 			    (TP_KEEPINIT(tp) != 0) &&
5285 			    rsm) {
5286 				/*
5287 				 * We have to put a ceiling on the rxt timer
5288 				 * of the keep-init timeout.
5289 				 */
5290 				uint32_t max_time, red;
5291 
5292 				max_time = TICKS_2_USEC(TP_KEEPINIT(tp));
5293 				if (TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) {
5294 					red = (cts - (uint32_t)rsm->r_tim_lastsent[0]);
5295 					if (red < max_time)
5296 						max_time -= red;
5297 					else
5298 						max_time = 1;
5299 				}
5300 				/* Reduce timeout to the keep value if needed */
5301 				if (max_time < to)
5302 					to = max_time;
5303 			}
5304 			return (to);
5305 		}
5306 		return (0);
5307 	}
5308 	if (rsm->r_flags & RACK_ACKED) {
5309 		rsm = rack_find_lowest_rsm(rack);
5310 		if (rsm == NULL) {
5311 			/* No lowest? */
5312 			goto activate_rxt;
5313 		}
5314 	}
5315 	if (rack->sack_attack_disable) {
5316 		/*
5317 		 * We don't want to do
5318 		 * any TLP's if you are an attacker.
5319 		 * Though if you are doing what
5320 		 * is expected you may still have
5321 		 * SACK-PASSED marks.
5322 		 */
5323 		goto activate_rxt;
5324 	}
5325 	/* Convert from ms to usecs */
5326 	if ((rsm->r_flags & RACK_SACK_PASSED) ||
5327 	    (rsm->r_flags & RACK_RWND_COLLAPSED) ||
5328 	    (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
5329 		if ((tp->t_flags & TF_SENTFIN) &&
5330 		    ((tp->snd_max - tp->snd_una) == 1) &&
5331 		    (rsm->r_flags & RACK_HAS_FIN)) {
5332 			/*
5333 			 * We don't start a rack timer if all we have is a
5334 			 * FIN outstanding.
5335 			 */
5336 			goto activate_rxt;
5337 		}
5338 		if ((rack->use_rack_rr == 0) &&
5339 		    (IN_FASTRECOVERY(tp->t_flags)) &&
5340 		    (rack->rack_no_prr == 0) &&
5341 		     (rack->r_ctl.rc_prr_sndcnt  < ctf_fixed_maxseg(tp))) {
5342 			/*
5343 			 * We are not cheating, in recovery  and
5344 			 * not enough ack's to yet get our next
5345 			 * retransmission out.
5346 			 *
5347 			 * Note that classified attackers do not
5348 			 * get to use the rack-cheat.
5349 			 */
5350 			goto activate_tlp;
5351 		}
5352 		srtt = rack_grab_rtt(tp, rack);
5353 		thresh = rack_calc_thresh_rack(rack, srtt, cts);
5354 		idx = rsm->r_rtr_cnt - 1;
5355 		exp = ((uint32_t)rsm->r_tim_lastsent[idx]) + thresh;
5356 		if (SEQ_GEQ(exp, cts)) {
5357 			to = exp - cts;
5358 			if (to < rack->r_ctl.rc_min_to) {
5359 				to = rack->r_ctl.rc_min_to;
5360 				if (rack->r_rr_config == 3)
5361 					rack->rc_on_min_to = 1;
5362 			}
5363 		} else {
5364 			to = rack->r_ctl.rc_min_to;
5365 			if (rack->r_rr_config == 3)
5366 				rack->rc_on_min_to = 1;
5367 		}
5368 	} else {
5369 		/* Ok we need to do a TLP not RACK */
5370 activate_tlp:
5371 		if ((rack->rc_tlp_in_progress != 0) &&
5372 		    (rack->r_ctl.rc_tlp_cnt_out >= rack_tlp_limit)) {
5373 			/*
5374 			 * The previous send was a TLP and we have sent
5375 			 * N TLP's without sending new data.
5376 			 */
5377 			goto activate_rxt;
5378 		}
5379 		rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
5380 		if (rsm == NULL) {
5381 			/* We found no rsm to TLP with. */
5382 			goto activate_rxt;
5383 		}
5384 		if (rsm->r_flags & RACK_HAS_FIN) {
5385 			/* If its a FIN we dont do TLP */
5386 			rsm = NULL;
5387 			goto activate_rxt;
5388 		}
5389 		idx = rsm->r_rtr_cnt - 1;
5390 		time_since_sent = 0;
5391 		if (TSTMP_GEQ(((uint32_t)rsm->r_tim_lastsent[idx]), rack->r_ctl.rc_tlp_rxt_last_time))
5392 			tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx];
5393 		else
5394 			tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time;
5395 		if (TSTMP_GT(cts, tstmp_touse))
5396 		    time_since_sent = cts - tstmp_touse;
5397 		is_tlp_timer = 1;
5398 		if (tp->t_srtt) {
5399 			if ((rack->rc_srtt_measure_made == 0) &&
5400 			    (tp->t_srtt == 1)) {
5401 				/*
5402 				 * If another stack as run and set srtt to 1,
5403 				 * then the srtt was 0, so lets use the initial.
5404 				 */
5405 				srtt = RACK_INITIAL_RTO;
5406 			} else {
5407 				srtt_cur = tp->t_srtt;
5408 				srtt = srtt_cur;
5409 			}
5410 		} else
5411 			srtt = RACK_INITIAL_RTO;
5412 		/*
5413 		 * If the SRTT is not keeping up and the
5414 		 * rack RTT has spiked we want to use
5415 		 * the last RTT not the smoothed one.
5416 		 */
5417 		if (rack_tlp_use_greater &&
5418 		    tp->t_srtt &&
5419 		    (srtt < rack_grab_rtt(tp, rack))) {
5420 			srtt = rack_grab_rtt(tp, rack);
5421 		}
5422 		thresh = rack_calc_thresh_tlp(tp, rack, rsm, srtt);
5423 		if (thresh > time_since_sent) {
5424 			to = thresh - time_since_sent;
5425 		} else {
5426 			to = rack->r_ctl.rc_min_to;
5427 			rack_log_alt_to_to_cancel(rack,
5428 						  thresh,		/* flex1 */
5429 						  time_since_sent,	/* flex2 */
5430 						  tstmp_touse,		/* flex3 */
5431 						  rack->r_ctl.rc_tlp_rxt_last_time, /* flex4 */
5432 						  (uint32_t)rsm->r_tim_lastsent[idx],
5433 						  srtt,
5434 						  idx, 99);
5435 		}
5436 		if (to < rack_tlp_min) {
5437 			to = rack_tlp_min;
5438 		}
5439 		if (to > TICKS_2_USEC(TCPTV_REXMTMAX)) {
5440 			/*
5441 			 * If the TLP time works out to larger than the max
5442 			 * RTO lets not do TLP.. just RTO.
5443 			 */
5444 			goto activate_rxt;
5445 		}
5446 	}
5447 	if (is_tlp_timer == 0) {
5448 		rack->r_ctl.rc_hpts_flags |= PACE_TMR_RACK;
5449 	} else {
5450 		rack->r_ctl.rc_hpts_flags |= PACE_TMR_TLP;
5451 	}
5452 	if (to == 0)
5453 		to = 1;
5454 	return (to);
5455 }
5456 
5457 static void
5458 rack_enter_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
5459 {
5460 	if (rack->rc_in_persist == 0) {
5461 		if (tp->t_flags & TF_GPUTINPROG) {
5462 			/*
5463 			 * Stop the goodput now, the calling of the
5464 			 * measurement function clears the flag.
5465 			 */
5466 			rack_do_goodput_measurement(tp, rack, tp->snd_una, __LINE__,
5467 						    RACK_QUALITY_PERSIST);
5468 		}
5469 #ifdef NETFLIX_SHARED_CWND
5470 		if (rack->r_ctl.rc_scw) {
5471 			tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
5472 			rack->rack_scwnd_is_idle = 1;
5473 		}
5474 #endif
5475 		rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
5476 		if (rack->r_ctl.rc_went_idle_time == 0)
5477 			rack->r_ctl.rc_went_idle_time = 1;
5478 		rack_timer_cancel(tp, rack, cts, __LINE__);
5479 		rack->r_ctl.persist_lost_ends = 0;
5480 		rack->probe_not_answered = 0;
5481 		rack->forced_ack = 0;
5482 		tp->t_rxtshift = 0;
5483 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
5484 			      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
5485 		rack->rc_in_persist = 1;
5486 	}
5487 }
5488 
5489 static void
5490 rack_exit_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
5491 {
5492 	if (tcp_in_hpts(rack->rc_inp)) {
5493 		tcp_hpts_remove(rack->rc_inp);
5494 		rack->r_ctl.rc_hpts_flags = 0;
5495 	}
5496 #ifdef NETFLIX_SHARED_CWND
5497 	if (rack->r_ctl.rc_scw) {
5498 		tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
5499 		rack->rack_scwnd_is_idle = 0;
5500 	}
5501 #endif
5502 	if (rack->rc_gp_dyn_mul &&
5503 	    (rack->use_fixed_rate == 0) &&
5504 	    (rack->rc_always_pace)) {
5505 		/*
5506 		 * Do we count this as if a probe-rtt just
5507 		 * finished?
5508 		 */
5509 		uint32_t time_idle, idle_min;
5510 
5511 		time_idle = tcp_get_usecs(NULL) - rack->r_ctl.rc_went_idle_time;
5512 		idle_min = rack_min_probertt_hold;
5513 		if (rack_probertt_gpsrtt_cnt_div) {
5514 			uint64_t extra;
5515 			extra = (uint64_t)rack->r_ctl.rc_gp_srtt *
5516 				(uint64_t)rack_probertt_gpsrtt_cnt_mul;
5517 			extra /= (uint64_t)rack_probertt_gpsrtt_cnt_div;
5518 			idle_min += (uint32_t)extra;
5519 		}
5520 		if (time_idle >= idle_min) {
5521 			/* Yes, we count it as a probe-rtt. */
5522 			uint32_t us_cts;
5523 
5524 			us_cts = tcp_get_usecs(NULL);
5525 			if (rack->in_probe_rtt == 0) {
5526 				rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
5527 				rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts;
5528 				rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts;
5529 				rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts;
5530 			} else {
5531 				rack_exit_probertt(rack, us_cts);
5532 			}
5533 		}
5534 	}
5535 	rack->rc_in_persist = 0;
5536 	rack->r_ctl.rc_went_idle_time = 0;
5537 	tp->t_rxtshift = 0;
5538 	RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
5539 	   rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
5540 	rack->r_ctl.rc_agg_delayed = 0;
5541 	rack->r_early = 0;
5542 	rack->r_late = 0;
5543 	rack->r_ctl.rc_agg_early = 0;
5544 }
5545 
5546 static void
5547 rack_log_hpts_diag(struct tcp_rack *rack, uint32_t cts,
5548 		   struct hpts_diag *diag, struct timeval *tv)
5549 {
5550 	if (rack_verbose_logging && rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
5551 		union tcp_log_stackspecific log;
5552 
5553 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5554 		log.u_bbr.flex1 = diag->p_nxt_slot;
5555 		log.u_bbr.flex2 = diag->p_cur_slot;
5556 		log.u_bbr.flex3 = diag->slot_req;
5557 		log.u_bbr.flex4 = diag->inp_hptsslot;
5558 		log.u_bbr.flex5 = diag->slot_remaining;
5559 		log.u_bbr.flex6 = diag->need_new_to;
5560 		log.u_bbr.flex7 = diag->p_hpts_active;
5561 		log.u_bbr.flex8 = diag->p_on_min_sleep;
5562 		/* Hijack other fields as needed */
5563 		log.u_bbr.epoch = diag->have_slept;
5564 		log.u_bbr.lt_epoch = diag->yet_to_sleep;
5565 		log.u_bbr.pkts_out = diag->co_ret;
5566 		log.u_bbr.applimited = diag->hpts_sleep_time;
5567 		log.u_bbr.delivered = diag->p_prev_slot;
5568 		log.u_bbr.inflight = diag->p_runningslot;
5569 		log.u_bbr.bw_inuse = diag->wheel_slot;
5570 		log.u_bbr.rttProp = diag->wheel_cts;
5571 		log.u_bbr.timeStamp = cts;
5572 		log.u_bbr.delRate = diag->maxslots;
5573 		log.u_bbr.cur_del_rate = diag->p_curtick;
5574 		log.u_bbr.cur_del_rate <<= 32;
5575 		log.u_bbr.cur_del_rate |= diag->p_lasttick;
5576 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
5577 		    &rack->rc_inp->inp_socket->so_rcv,
5578 		    &rack->rc_inp->inp_socket->so_snd,
5579 		    BBR_LOG_HPTSDIAG, 0,
5580 		    0, &log, false, tv);
5581 	}
5582 
5583 }
5584 
5585 static void
5586 rack_log_wakeup(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb, uint32_t len, int type)
5587 {
5588 	if (rack_verbose_logging && rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
5589 		union tcp_log_stackspecific log;
5590 		struct timeval tv;
5591 
5592 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5593 		log.u_bbr.flex1 = sb->sb_flags;
5594 		log.u_bbr.flex2 = len;
5595 		log.u_bbr.flex3 = sb->sb_state;
5596 		log.u_bbr.flex8 = type;
5597 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5598 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
5599 		    &rack->rc_inp->inp_socket->so_rcv,
5600 		    &rack->rc_inp->inp_socket->so_snd,
5601 		    TCP_LOG_SB_WAKE, 0,
5602 		    len, &log, false, &tv);
5603 	}
5604 }
5605 
5606 static void
5607 rack_start_hpts_timer(struct tcp_rack *rack, struct tcpcb *tp, uint32_t cts,
5608       int32_t slot, uint32_t tot_len_this_send, int sup_rack)
5609 {
5610 	struct hpts_diag diag;
5611 	struct inpcb *inp;
5612 	struct timeval tv;
5613 	uint32_t delayed_ack = 0;
5614 	uint32_t hpts_timeout;
5615 	uint32_t entry_slot = slot;
5616 	uint8_t stopped;
5617 	uint32_t left = 0;
5618 	uint32_t us_cts;
5619 
5620 	inp = tp->t_inpcb;
5621 	if ((tp->t_state == TCPS_CLOSED) ||
5622 	    (tp->t_state == TCPS_LISTEN)) {
5623 		return;
5624 	}
5625 	if (tcp_in_hpts(inp)) {
5626 		/* Already on the pacer */
5627 		return;
5628 	}
5629 	stopped = rack->rc_tmr_stopped;
5630 	if (stopped && TSTMP_GT(rack->r_ctl.rc_timer_exp, cts)) {
5631 		left = rack->r_ctl.rc_timer_exp - cts;
5632 	}
5633 	rack->r_ctl.rc_timer_exp = 0;
5634 	rack->r_ctl.rc_hpts_flags = 0;
5635 	us_cts = tcp_get_usecs(&tv);
5636 	/* Now early/late accounting */
5637 	rack_log_pacing_delay_calc(rack, entry_slot, slot, 0, 0, 0, 26, __LINE__, NULL, 0);
5638 	if (rack->r_early && (rack->rc_ack_can_sendout_data == 0)) {
5639 		/*
5640 		 * We have a early carry over set,
5641 		 * we can always add more time so we
5642 		 * can always make this compensation.
5643 		 *
5644 		 * Note if ack's are allowed to wake us do not
5645 		 * penalize the next timer for being awoke
5646 		 * by an ack aka the rc_agg_early (non-paced mode).
5647 		 */
5648 		slot += rack->r_ctl.rc_agg_early;
5649 		rack->r_early = 0;
5650 		rack->r_ctl.rc_agg_early = 0;
5651 	}
5652 	if (rack->r_late) {
5653 		/*
5654 		 * This is harder, we can
5655 		 * compensate some but it
5656 		 * really depends on what
5657 		 * the current pacing time is.
5658 		 */
5659 		if (rack->r_ctl.rc_agg_delayed >= slot) {
5660 			/*
5661 			 * We can't compensate for it all.
5662 			 * And we have to have some time
5663 			 * on the clock. We always have a min
5664 			 * 10 slots (10 x 10 i.e. 100 usecs).
5665 			 */
5666 			if (slot <= HPTS_TICKS_PER_SLOT) {
5667 				/* We gain delay */
5668 				rack->r_ctl.rc_agg_delayed += (HPTS_TICKS_PER_SLOT - slot);
5669 				slot = HPTS_TICKS_PER_SLOT;
5670 			} else {
5671 				/* We take off some */
5672 				rack->r_ctl.rc_agg_delayed -= (slot - HPTS_TICKS_PER_SLOT);
5673 				slot = HPTS_TICKS_PER_SLOT;
5674 			}
5675 		} else {
5676 			slot -= rack->r_ctl.rc_agg_delayed;
5677 			rack->r_ctl.rc_agg_delayed = 0;
5678 			/* Make sure we have 100 useconds at minimum */
5679 			if (slot < HPTS_TICKS_PER_SLOT) {
5680 				rack->r_ctl.rc_agg_delayed = HPTS_TICKS_PER_SLOT - slot;
5681 				slot = HPTS_TICKS_PER_SLOT;
5682 			}
5683 			if (rack->r_ctl.rc_agg_delayed == 0)
5684 				rack->r_late = 0;
5685 		}
5686 	}
5687 	if (slot) {
5688 		/* We are pacing too */
5689 		rack->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT;
5690 	}
5691 	hpts_timeout = rack_timer_start(tp, rack, cts, sup_rack);
5692 #ifdef NETFLIX_EXP_DETECTION
5693 	if (rack->sack_attack_disable &&
5694 	    (slot < tcp_sad_pacing_interval)) {
5695 		/*
5696 		 * We have a potential attacker on
5697 		 * the line. We have possibly some
5698 		 * (or now) pacing time set. We want to
5699 		 * slow down the processing of sacks by some
5700 		 * amount (if it is an attacker). Set the default
5701 		 * slot for attackers in place (unless the orginal
5702 		 * interval is longer). Its stored in
5703 		 * micro-seconds, so lets convert to msecs.
5704 		 */
5705 		slot = tcp_sad_pacing_interval;
5706 	}
5707 #endif
5708 	if (tp->t_flags & TF_DELACK) {
5709 		delayed_ack = TICKS_2_USEC(tcp_delacktime);
5710 		rack->r_ctl.rc_hpts_flags |= PACE_TMR_DELACK;
5711 	}
5712 	if (delayed_ack && ((hpts_timeout == 0) ||
5713 			    (delayed_ack < hpts_timeout)))
5714 		hpts_timeout = delayed_ack;
5715 	else
5716 		rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
5717 	/*
5718 	 * If no timers are going to run and we will fall off the hptsi
5719 	 * wheel, we resort to a keep-alive timer if its configured.
5720 	 */
5721 	if ((hpts_timeout == 0) &&
5722 	    (slot == 0)) {
5723 		if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
5724 		    (tp->t_state <= TCPS_CLOSING)) {
5725 			/*
5726 			 * Ok we have no timer (persists, rack, tlp, rxt  or
5727 			 * del-ack), we don't have segments being paced. So
5728 			 * all that is left is the keepalive timer.
5729 			 */
5730 			if (TCPS_HAVEESTABLISHED(tp->t_state)) {
5731 				/* Get the established keep-alive time */
5732 				hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp));
5733 			} else {
5734 				/*
5735 				 * Get the initial setup keep-alive time,
5736 				 * note that this is probably not going to
5737 				 * happen, since rack will be running a rxt timer
5738 				 * if a SYN of some sort is outstanding. It is
5739 				 * actually handled in rack_timeout_rxt().
5740 				 */
5741 				hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp));
5742 			}
5743 			rack->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP;
5744 			if (rack->in_probe_rtt) {
5745 				/*
5746 				 * We want to instead not wake up a long time from
5747 				 * now but to wake up about the time we would
5748 				 * exit probe-rtt and initiate a keep-alive ack.
5749 				 * This will get us out of probe-rtt and update
5750 				 * our min-rtt.
5751 				 */
5752 				hpts_timeout = rack_min_probertt_hold;
5753 			}
5754 		}
5755 	}
5756 	if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) ==
5757 	    (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) {
5758 		/*
5759 		 * RACK, TLP, persists and RXT timers all are restartable
5760 		 * based on actions input .. i.e we received a packet (ack
5761 		 * or sack) and that changes things (rw, or snd_una etc).
5762 		 * Thus we can restart them with a new value. For
5763 		 * keep-alive, delayed_ack we keep track of what was left
5764 		 * and restart the timer with a smaller value.
5765 		 */
5766 		if (left < hpts_timeout)
5767 			hpts_timeout = left;
5768 	}
5769 	if (hpts_timeout) {
5770 		/*
5771 		 * Hack alert for now we can't time-out over 2,147,483
5772 		 * seconds (a bit more than 596 hours), which is probably ok
5773 		 * :).
5774 		 */
5775 		if (hpts_timeout > 0x7ffffffe)
5776 			hpts_timeout = 0x7ffffffe;
5777 		rack->r_ctl.rc_timer_exp = cts + hpts_timeout;
5778 	}
5779 	rack_log_pacing_delay_calc(rack, entry_slot, slot, hpts_timeout, 0, 0, 27, __LINE__, NULL, 0);
5780 	if ((rack->gp_ready == 0) &&
5781 	    (rack->use_fixed_rate == 0) &&
5782 	    (hpts_timeout < slot) &&
5783 	    (rack->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) {
5784 		/*
5785 		 * We have no good estimate yet for the
5786 		 * old clunky burst mitigation or the
5787 		 * real pacing. And the tlp or rxt is smaller
5788 		 * than the pacing calculation. Lets not
5789 		 * pace that long since we know the calculation
5790 		 * so far is not accurate.
5791 		 */
5792 		slot = hpts_timeout;
5793 	}
5794 	/**
5795 	 * Turn off all the flags for queuing by default. The
5796 	 * flags have important meanings to what happens when
5797 	 * LRO interacts with the transport. Most likely (by default now)
5798 	 * mbuf_queueing and ack compression are on. So the transport
5799 	 * has a couple of flags that control what happens (if those
5800 	 * are not on then these flags won't have any effect since it
5801 	 * won't go through the queuing LRO path).
5802 	 *
5803 	 * INP_MBUF_QUEUE_READY - This flags says that I am busy
5804 	 *                        pacing output, so don't disturb. But
5805 	 *                        it also means LRO can wake me if there
5806 	 *                        is a SACK arrival.
5807 	 *
5808 	 * INP_DONT_SACK_QUEUE - This flag is used in conjunction
5809 	 *                       with the above flag (QUEUE_READY) and
5810 	 *                       when present it says don't even wake me
5811 	 *                       if a SACK arrives.
5812 	 *
5813 	 * The idea behind these flags is that if we are pacing we
5814 	 * set the MBUF_QUEUE_READY and only get woken up if
5815 	 * a SACK arrives (which could change things) or if
5816 	 * our pacing timer expires. If, however, we have a rack
5817 	 * timer running, then we don't even want a sack to wake
5818 	 * us since the rack timer has to expire before we can send.
5819 	 *
5820 	 * Other cases should usually have none of the flags set
5821 	 * so LRO can call into us.
5822 	 */
5823 	inp->inp_flags2 &= ~(INP_DONT_SACK_QUEUE|INP_MBUF_QUEUE_READY);
5824 	if (slot) {
5825 		rack->r_ctl.rc_last_output_to = us_cts + slot;
5826 		/*
5827 		 * A pacing timer (slot) is being set, in
5828 		 * such a case we cannot send (we are blocked by
5829 		 * the timer). So lets tell LRO that it should not
5830 		 * wake us unless there is a SACK. Note this only
5831 		 * will be effective if mbuf queueing is on or
5832 		 * compressed acks are being processed.
5833 		 */
5834 		inp->inp_flags2 |= INP_MBUF_QUEUE_READY;
5835 		/*
5836 		 * But wait if we have a Rack timer running
5837 		 * even a SACK should not disturb us (with
5838 		 * the exception of r_rr_config 3).
5839 		 */
5840 		if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK) &&
5841 		    (rack->r_rr_config != 3))
5842 			inp->inp_flags2 |= INP_DONT_SACK_QUEUE;
5843 		if (rack->rc_ack_can_sendout_data) {
5844 			/*
5845 			 * Ahh but wait, this is that special case
5846 			 * where the pacing timer can be disturbed
5847 			 * backout the changes (used for non-paced
5848 			 * burst limiting).
5849 			 */
5850 			inp->inp_flags2 &= ~(INP_DONT_SACK_QUEUE|INP_MBUF_QUEUE_READY);
5851 		}
5852 		if ((rack->use_rack_rr) &&
5853 		    (rack->r_rr_config < 2) &&
5854 		    ((hpts_timeout) && (hpts_timeout < slot))) {
5855 			/*
5856 			 * Arrange for the hpts to kick back in after the
5857 			 * t-o if the t-o does not cause a send.
5858 			 */
5859 			(void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(hpts_timeout),
5860 						   __LINE__, &diag);
5861 			rack_log_hpts_diag(rack, us_cts, &diag, &tv);
5862 			rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
5863 		} else {
5864 			(void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(slot),
5865 						   __LINE__, &diag);
5866 			rack_log_hpts_diag(rack, us_cts, &diag, &tv);
5867 			rack_log_to_start(rack, cts, hpts_timeout, slot, 1);
5868 		}
5869 	} else if (hpts_timeout) {
5870 		/*
5871 		 * With respect to inp_flags2 here, lets let any new acks wake
5872 		 * us up here. Since we are not pacing (no pacing timer), output
5873 		 * can happen so we should let it. If its a Rack timer, then any inbound
5874 		 * packet probably won't change the sending (we will be blocked)
5875 		 * but it may change the prr stats so letting it in (the set defaults
5876 		 * at the start of this block) are good enough.
5877 		 */
5878 		(void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(hpts_timeout),
5879 					   __LINE__, &diag);
5880 		rack_log_hpts_diag(rack, us_cts, &diag, &tv);
5881 		rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
5882 	} else {
5883 		/* No timer starting */
5884 #ifdef INVARIANTS
5885 		if (SEQ_GT(tp->snd_max, tp->snd_una)) {
5886 			panic("tp:%p rack:%p tlts:%d cts:%u slot:%u pto:%u -- no timer started?",
5887 			    tp, rack, tot_len_this_send, cts, slot, hpts_timeout);
5888 		}
5889 #endif
5890 	}
5891 	rack->rc_tmr_stopped = 0;
5892 	if (slot)
5893 		rack_log_type_bbrsnd(rack, tot_len_this_send, slot, us_cts, &tv);
5894 }
5895 
5896 /*
5897  * RACK Timer, here we simply do logging and house keeping.
5898  * the normal rack_output() function will call the
5899  * appropriate thing to check if we need to do a RACK retransmit.
5900  * We return 1, saying don't proceed with rack_output only
5901  * when all timers have been stopped (destroyed PCB?).
5902  */
5903 static int
5904 rack_timeout_rack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
5905 {
5906 	/*
5907 	 * This timer simply provides an internal trigger to send out data.
5908 	 * The check_recovery_mode call will see if there are needed
5909 	 * retransmissions, if so we will enter fast-recovery. The output
5910 	 * call may or may not do the same thing depending on sysctl
5911 	 * settings.
5912 	 */
5913 	struct rack_sendmap *rsm;
5914 
5915 	if (tp->t_timers->tt_flags & TT_STOPPED) {
5916 		return (1);
5917 	}
5918 	counter_u64_add(rack_to_tot, 1);
5919 	if (rack->r_state && (rack->r_state != tp->t_state))
5920 		rack_set_state(tp, rack);
5921 	rack->rc_on_min_to = 0;
5922 	rsm = rack_check_recovery_mode(tp, cts);
5923 	rack_log_to_event(rack, RACK_TO_FRM_RACK, rsm);
5924 	if (rsm) {
5925 		rack->r_ctl.rc_resend = rsm;
5926 		rack->r_timer_override = 1;
5927 		if (rack->use_rack_rr) {
5928 			/*
5929 			 * Don't accumulate extra pacing delay
5930 			 * we are allowing the rack timer to
5931 			 * over-ride pacing i.e. rrr takes precedence
5932 			 * if the pacing interval is longer than the rrr
5933 			 * time (in other words we get the min pacing
5934 			 * time versus rrr pacing time).
5935 			 */
5936 			rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
5937 		}
5938 	}
5939 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK;
5940 	if (rsm == NULL) {
5941 		/* restart a timer and return 1 */
5942 		rack_start_hpts_timer(rack, tp, cts,
5943 				      0, 0, 0);
5944 		return (1);
5945 	}
5946 	return (0);
5947 }
5948 
5949 static void
5950 rack_adjust_orig_mlen(struct rack_sendmap *rsm)
5951 {
5952 	if (rsm->m->m_len > rsm->orig_m_len) {
5953 		/*
5954 		 * Mbuf grew, caused by sbcompress, our offset does
5955 		 * not change.
5956 		 */
5957 		rsm->orig_m_len = rsm->m->m_len;
5958 	} else if (rsm->m->m_len < rsm->orig_m_len) {
5959 		/*
5960 		 * Mbuf shrank, trimmed off the top by an ack, our
5961 		 * offset changes.
5962 		 */
5963 		rsm->soff -= (rsm->orig_m_len - rsm->m->m_len);
5964 		rsm->orig_m_len = rsm->m->m_len;
5965 	}
5966 }
5967 
5968 static void
5969 rack_setup_offset_for_rsm(struct rack_sendmap *src_rsm, struct rack_sendmap *rsm)
5970 {
5971 	struct mbuf *m;
5972 	uint32_t soff;
5973 
5974 	if (src_rsm->m && (src_rsm->orig_m_len != src_rsm->m->m_len)) {
5975 		/* Fix up the orig_m_len and possibly the mbuf offset */
5976 		rack_adjust_orig_mlen(src_rsm);
5977 	}
5978 	m = src_rsm->m;
5979 	soff = src_rsm->soff + (src_rsm->r_end - src_rsm->r_start);
5980 	while (soff >= m->m_len) {
5981 		/* Move out past this mbuf */
5982 		soff -= m->m_len;
5983 		m = m->m_next;
5984 		KASSERT((m != NULL),
5985 			("rsm:%p nrsm:%p hit at soff:%u null m",
5986 			 src_rsm, rsm, soff));
5987 	}
5988 	rsm->m = m;
5989 	rsm->soff = soff;
5990 	rsm->orig_m_len = m->m_len;
5991 }
5992 
5993 static __inline void
5994 rack_clone_rsm(struct tcp_rack *rack, struct rack_sendmap *nrsm,
5995 	       struct rack_sendmap *rsm, uint32_t start)
5996 {
5997 	int idx;
5998 
5999 	nrsm->r_start = start;
6000 	nrsm->r_end = rsm->r_end;
6001 	nrsm->r_rtr_cnt = rsm->r_rtr_cnt;
6002 	nrsm->r_flags = rsm->r_flags;
6003 	nrsm->r_dupack = rsm->r_dupack;
6004 	nrsm->r_no_rtt_allowed = rsm->r_no_rtt_allowed;
6005 	nrsm->r_rtr_bytes = 0;
6006 	nrsm->r_fas = rsm->r_fas;
6007 	rsm->r_end = nrsm->r_start;
6008 	nrsm->r_just_ret = rsm->r_just_ret;
6009 	for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) {
6010 		nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx];
6011 	}
6012 	/* Now if we have SYN flag we keep it on the left edge */
6013 	if (nrsm->r_flags & RACK_HAS_SYN)
6014 		nrsm->r_flags &= ~RACK_HAS_SYN;
6015 	/* Now if we have a FIN flag we keep it on the right edge */
6016 	if (rsm->r_flags & RACK_HAS_FIN)
6017 		rsm->r_flags &= ~RACK_HAS_FIN;
6018 	/* Push bit must go to the right edge as well */
6019 	if (rsm->r_flags & RACK_HAD_PUSH)
6020 		rsm->r_flags &= ~RACK_HAD_PUSH;
6021 	/* Clone over the state of the hw_tls flag */
6022 	nrsm->r_hw_tls = rsm->r_hw_tls;
6023 	/*
6024 	 * Now we need to find nrsm's new location in the mbuf chain
6025 	 * we basically calculate a new offset, which is soff +
6026 	 * how much is left in original rsm. Then we walk out the mbuf
6027 	 * chain to find the righ position, it may be the same mbuf
6028 	 * or maybe not.
6029 	 */
6030 	KASSERT(((rsm->m != NULL) ||
6031 		 (rsm->r_flags & (RACK_HAS_SYN|RACK_HAS_FIN))),
6032 		("rsm:%p nrsm:%p rack:%p -- rsm->m is NULL?", rsm, nrsm, rack));
6033 	if (rsm->m)
6034 		rack_setup_offset_for_rsm(rsm, nrsm);
6035 }
6036 
6037 static struct rack_sendmap *
6038 rack_merge_rsm(struct tcp_rack *rack,
6039 	       struct rack_sendmap *l_rsm,
6040 	       struct rack_sendmap *r_rsm)
6041 {
6042 	/*
6043 	 * We are merging two ack'd RSM's,
6044 	 * the l_rsm is on the left (lower seq
6045 	 * values) and the r_rsm is on the right
6046 	 * (higher seq value). The simplest way
6047 	 * to merge these is to move the right
6048 	 * one into the left. I don't think there
6049 	 * is any reason we need to try to find
6050 	 * the oldest (or last oldest retransmitted).
6051 	 */
6052 #ifdef INVARIANTS
6053 	struct rack_sendmap *rm;
6054 #endif
6055 	rack_log_map_chg(rack->rc_tp, rack, NULL,
6056 			 l_rsm, r_rsm, MAP_MERGE, r_rsm->r_end, __LINE__);
6057 	l_rsm->r_end = r_rsm->r_end;
6058 	if (l_rsm->r_dupack < r_rsm->r_dupack)
6059 		l_rsm->r_dupack = r_rsm->r_dupack;
6060 	if (r_rsm->r_rtr_bytes)
6061 		l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes;
6062 	if (r_rsm->r_in_tmap) {
6063 		/* This really should not happen */
6064 		TAILQ_REMOVE(&rack->r_ctl.rc_tmap, r_rsm, r_tnext);
6065 		r_rsm->r_in_tmap = 0;
6066 	}
6067 
6068 	/* Now the flags */
6069 	if (r_rsm->r_flags & RACK_HAS_FIN)
6070 		l_rsm->r_flags |= RACK_HAS_FIN;
6071 	if (r_rsm->r_flags & RACK_TLP)
6072 		l_rsm->r_flags |= RACK_TLP;
6073 	if (r_rsm->r_flags & RACK_RWND_COLLAPSED)
6074 		l_rsm->r_flags |= RACK_RWND_COLLAPSED;
6075 	if ((r_rsm->r_flags & RACK_APP_LIMITED)  &&
6076 	    ((l_rsm->r_flags & RACK_APP_LIMITED) == 0)) {
6077 		/*
6078 		 * If both are app-limited then let the
6079 		 * free lower the count. If right is app
6080 		 * limited and left is not, transfer.
6081 		 */
6082 		l_rsm->r_flags |= RACK_APP_LIMITED;
6083 		r_rsm->r_flags &= ~RACK_APP_LIMITED;
6084 		if (r_rsm == rack->r_ctl.rc_first_appl)
6085 			rack->r_ctl.rc_first_appl = l_rsm;
6086 	}
6087 #ifndef INVARIANTS
6088 	(void)RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, r_rsm);
6089 #else
6090 	rm = RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, r_rsm);
6091 	if (rm != r_rsm) {
6092 		panic("removing head in rack:%p rsm:%p rm:%p",
6093 		      rack, r_rsm, rm);
6094 	}
6095 #endif
6096 	if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) {
6097 		/* Transfer the split limit to the map we free */
6098 		r_rsm->r_limit_type = l_rsm->r_limit_type;
6099 		l_rsm->r_limit_type = 0;
6100 	}
6101 	rack_free(rack, r_rsm);
6102 	return (l_rsm);
6103 }
6104 
6105 /*
6106  * TLP Timer, here we simply setup what segment we want to
6107  * have the TLP expire on, the normal rack_output() will then
6108  * send it out.
6109  *
6110  * We return 1, saying don't proceed with rack_output only
6111  * when all timers have been stopped (destroyed PCB?).
6112  */
6113 static int
6114 rack_timeout_tlp(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t *doing_tlp)
6115 {
6116 	/*
6117 	 * Tail Loss Probe.
6118 	 */
6119 	struct rack_sendmap *rsm = NULL;
6120 #ifdef INVARIANTS
6121 	struct rack_sendmap *insret;
6122 #endif
6123 	struct socket *so;
6124 	uint32_t amm;
6125 	uint32_t out, avail;
6126 	int collapsed_win = 0;
6127 
6128 	if (tp->t_timers->tt_flags & TT_STOPPED) {
6129 		return (1);
6130 	}
6131 	if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
6132 		/* Its not time yet */
6133 		return (0);
6134 	}
6135 	if (ctf_progress_timeout_check(tp, true)) {
6136 		rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
6137 		return (-ETIMEDOUT);	/* tcp_drop() */
6138 	}
6139 	/*
6140 	 * A TLP timer has expired. We have been idle for 2 rtts. So we now
6141 	 * need to figure out how to force a full MSS segment out.
6142 	 */
6143 	rack_log_to_event(rack, RACK_TO_FRM_TLP, NULL);
6144 	rack->r_ctl.retran_during_recovery = 0;
6145 	rack->r_ctl.dsack_byte_cnt = 0;
6146 	counter_u64_add(rack_tlp_tot, 1);
6147 	if (rack->r_state && (rack->r_state != tp->t_state))
6148 		rack_set_state(tp, rack);
6149 	so = tp->t_inpcb->inp_socket;
6150 	avail = sbavail(&so->so_snd);
6151 	out = tp->snd_max - tp->snd_una;
6152 	if ((out > tp->snd_wnd) || rack->rc_has_collapsed) {
6153 		/* special case, we need a retransmission */
6154 		collapsed_win = 1;
6155 		goto need_retran;
6156 	}
6157 	if (rack->r_ctl.dsack_persist && (rack->r_ctl.rc_tlp_cnt_out >= 1)) {
6158 		rack->r_ctl.dsack_persist--;
6159 		if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
6160 			rack->r_ctl.num_dsack = 0;
6161 		}
6162 		rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
6163 	}
6164 	if ((tp->t_flags & TF_GPUTINPROG) &&
6165 	    (rack->r_ctl.rc_tlp_cnt_out == 1)) {
6166 		/*
6167 		 * If this is the second in a row
6168 		 * TLP and we are doing a measurement
6169 		 * its time to abandon the measurement.
6170 		 * Something is likely broken on
6171 		 * the clients network and measuring a
6172 		 * broken network does us no good.
6173 		 */
6174 		tp->t_flags &= ~TF_GPUTINPROG;
6175 		rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
6176 					   rack->r_ctl.rc_gp_srtt /*flex1*/,
6177 					   tp->gput_seq,
6178 					   0, 0, 18, __LINE__, NULL, 0);
6179 	}
6180 	/*
6181 	 * Check our send oldest always settings, and if
6182 	 * there is an oldest to send jump to the need_retran.
6183 	 */
6184 	if (rack_always_send_oldest && (TAILQ_EMPTY(&rack->r_ctl.rc_tmap) == 0))
6185 		goto need_retran;
6186 
6187 	if (avail > out) {
6188 		/* New data is available */
6189 		amm = avail - out;
6190 		if (amm > ctf_fixed_maxseg(tp)) {
6191 			amm = ctf_fixed_maxseg(tp);
6192 			if ((amm + out) > tp->snd_wnd) {
6193 				/* We are rwnd limited */
6194 				goto need_retran;
6195 			}
6196 		} else if (amm < ctf_fixed_maxseg(tp)) {
6197 			/* not enough to fill a MTU */
6198 			goto need_retran;
6199 		}
6200 		if (IN_FASTRECOVERY(tp->t_flags)) {
6201 			/* Unlikely */
6202 			if (rack->rack_no_prr == 0) {
6203 				if (out + amm <= tp->snd_wnd) {
6204 					rack->r_ctl.rc_prr_sndcnt = amm;
6205 					rack->r_ctl.rc_tlp_new_data = amm;
6206 					rack_log_to_prr(rack, 4, 0, __LINE__);
6207 				}
6208 			} else
6209 				goto need_retran;
6210 		} else {
6211 			/* Set the send-new override */
6212 			if (out + amm <= tp->snd_wnd)
6213 				rack->r_ctl.rc_tlp_new_data = amm;
6214 			else
6215 				goto need_retran;
6216 		}
6217 		rack->r_ctl.rc_tlpsend = NULL;
6218 		counter_u64_add(rack_tlp_newdata, 1);
6219 		goto send;
6220 	}
6221 need_retran:
6222 	/*
6223 	 * Ok we need to arrange the last un-acked segment to be re-sent, or
6224 	 * optionally the first un-acked segment.
6225 	 */
6226 	if (collapsed_win == 0) {
6227 		if (rack_always_send_oldest)
6228 			rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6229 		else {
6230 			rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
6231 			if (rsm && (rsm->r_flags & (RACK_ACKED | RACK_HAS_FIN))) {
6232 				rsm = rack_find_high_nonack(rack, rsm);
6233 			}
6234 		}
6235 		if (rsm == NULL) {
6236 #ifdef TCP_BLACKBOX
6237 			tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true);
6238 #endif
6239 			goto out;
6240 		}
6241 	} else {
6242 		/*
6243 		 * We must find the last segment
6244 		 * that was acceptable by the client.
6245 		 */
6246 		RB_FOREACH_REVERSE(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
6247 			if ((rsm->r_flags & RACK_RWND_COLLAPSED) == 0) {
6248 				/* Found one */
6249 				break;
6250 			}
6251 		}
6252 		if (rsm == NULL) {
6253 			/* None? if so send the first */
6254 			rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
6255 			if (rsm == NULL) {
6256 #ifdef TCP_BLACKBOX
6257 				tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true);
6258 #endif
6259 				goto out;
6260 			}
6261 		}
6262 	}
6263 	if ((rsm->r_end - rsm->r_start) > ctf_fixed_maxseg(tp)) {
6264 		/*
6265 		 * We need to split this the last segment in two.
6266 		 */
6267 		struct rack_sendmap *nrsm;
6268 
6269 		nrsm = rack_alloc_full_limit(rack);
6270 		if (nrsm == NULL) {
6271 			/*
6272 			 * No memory to split, we will just exit and punt
6273 			 * off to the RXT timer.
6274 			 */
6275 			goto out;
6276 		}
6277 		rack_clone_rsm(rack, nrsm, rsm,
6278 			       (rsm->r_end - ctf_fixed_maxseg(tp)));
6279 		rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
6280 #ifndef INVARIANTS
6281 		(void)RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
6282 #else
6283 		insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
6284 		if (insret != NULL) {
6285 			panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
6286 			      nrsm, insret, rack, rsm);
6287 		}
6288 #endif
6289 		if (rsm->r_in_tmap) {
6290 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
6291 			nrsm->r_in_tmap = 1;
6292 		}
6293 		rsm = nrsm;
6294 	}
6295 	rack->r_ctl.rc_tlpsend = rsm;
6296 send:
6297 	/* Make sure output path knows we are doing a TLP */
6298 	*doing_tlp = 1;
6299 	rack->r_timer_override = 1;
6300 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
6301 	return (0);
6302 out:
6303 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
6304 	return (0);
6305 }
6306 
6307 /*
6308  * Delayed ack Timer, here we simply need to setup the
6309  * ACK_NOW flag and remove the DELACK flag. From there
6310  * the output routine will send the ack out.
6311  *
6312  * We only return 1, saying don't proceed, if all timers
6313  * are stopped (destroyed PCB?).
6314  */
6315 static int
6316 rack_timeout_delack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6317 {
6318 	if (tp->t_timers->tt_flags & TT_STOPPED) {
6319 		return (1);
6320 	}
6321 	rack_log_to_event(rack, RACK_TO_FRM_DELACK, NULL);
6322 	tp->t_flags &= ~TF_DELACK;
6323 	tp->t_flags |= TF_ACKNOW;
6324 	KMOD_TCPSTAT_INC(tcps_delack);
6325 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
6326 	return (0);
6327 }
6328 
6329 /*
6330  * Persists timer, here we simply send the
6331  * same thing as a keepalive will.
6332  * the one byte send.
6333  *
6334  * We only return 1, saying don't proceed, if all timers
6335  * are stopped (destroyed PCB?).
6336  */
6337 static int
6338 rack_timeout_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6339 {
6340 	struct tcptemp *t_template;
6341 #ifdef INVARIANTS
6342 	struct inpcb *inp = tp->t_inpcb;
6343 #endif
6344 	int32_t retval = 1;
6345 
6346 	if (tp->t_timers->tt_flags & TT_STOPPED) {
6347 		return (1);
6348 	}
6349 	if (rack->rc_in_persist == 0)
6350 		return (0);
6351 	if (ctf_progress_timeout_check(tp, false)) {
6352 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
6353 		rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
6354 		counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
6355 		return (-ETIMEDOUT);	/* tcp_drop() */
6356 	}
6357 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
6358 	/*
6359 	 * Persistence timer into zero window. Force a byte to be output, if
6360 	 * possible.
6361 	 */
6362 	KMOD_TCPSTAT_INC(tcps_persisttimeo);
6363 	/*
6364 	 * Hack: if the peer is dead/unreachable, we do not time out if the
6365 	 * window is closed.  After a full backoff, drop the connection if
6366 	 * the idle time (no responses to probes) reaches the maximum
6367 	 * backoff that we would use if retransmitting.
6368 	 */
6369 	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
6370 	    (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
6371 	     TICKS_2_USEC(ticks - tp->t_rcvtime) >= RACK_REXMTVAL(tp) * tcp_totbackoff)) {
6372 		KMOD_TCPSTAT_INC(tcps_persistdrop);
6373 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
6374 		counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
6375 		retval = -ETIMEDOUT;	/* tcp_drop() */
6376 		goto out;
6377 	}
6378 	if ((sbavail(&rack->rc_inp->inp_socket->so_snd) == 0) &&
6379 	    tp->snd_una == tp->snd_max)
6380 		rack_exit_persist(tp, rack, cts);
6381 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT;
6382 	/*
6383 	 * If the user has closed the socket then drop a persisting
6384 	 * connection after a much reduced timeout.
6385 	 */
6386 	if (tp->t_state > TCPS_CLOSE_WAIT &&
6387 	    (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
6388 		KMOD_TCPSTAT_INC(tcps_persistdrop);
6389 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
6390 		counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
6391 		retval = -ETIMEDOUT;	/* tcp_drop() */
6392 		goto out;
6393 	}
6394 	t_template = tcpip_maketemplate(rack->rc_inp);
6395 	if (t_template) {
6396 		/* only set it if we were answered */
6397 		if (rack->forced_ack == 0) {
6398 			rack->forced_ack = 1;
6399 			rack->r_ctl.forced_ack_ts = tcp_get_usecs(NULL);
6400 		} else {
6401 			rack->probe_not_answered = 1;
6402 			counter_u64_add(rack_persists_loss, 1);
6403 			rack->r_ctl.persist_lost_ends++;
6404 		}
6405 		counter_u64_add(rack_persists_sends, 1);
6406 		tcp_respond(tp, t_template->tt_ipgen,
6407 			    &t_template->tt_t, (struct mbuf *)NULL,
6408 			    tp->rcv_nxt, tp->snd_una - 1, 0);
6409 		/* This sends an ack */
6410 		if (tp->t_flags & TF_DELACK)
6411 			tp->t_flags &= ~TF_DELACK;
6412 		free(t_template, M_TEMP);
6413 	}
6414 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
6415 		tp->t_rxtshift++;
6416 out:
6417 	rack_log_to_event(rack, RACK_TO_FRM_PERSIST, NULL);
6418 	rack_start_hpts_timer(rack, tp, cts,
6419 			      0, 0, 0);
6420 	return (retval);
6421 }
6422 
6423 /*
6424  * If a keepalive goes off, we had no other timers
6425  * happening. We always return 1 here since this
6426  * routine either drops the connection or sends
6427  * out a segment with respond.
6428  */
6429 static int
6430 rack_timeout_keepalive(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6431 {
6432 	struct tcptemp *t_template;
6433 	struct inpcb *inp;
6434 
6435 	if (tp->t_timers->tt_flags & TT_STOPPED) {
6436 		return (1);
6437 	}
6438 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP;
6439 	inp = tp->t_inpcb;
6440 	rack_log_to_event(rack, RACK_TO_FRM_KEEP, NULL);
6441 	/*
6442 	 * Keep-alive timer went off; send something or drop connection if
6443 	 * idle for too long.
6444 	 */
6445 	KMOD_TCPSTAT_INC(tcps_keeptimeo);
6446 	if (tp->t_state < TCPS_ESTABLISHED)
6447 		goto dropit;
6448 	if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
6449 	    tp->t_state <= TCPS_CLOSING) {
6450 		if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
6451 			goto dropit;
6452 		/*
6453 		 * Send a packet designed to force a response if the peer is
6454 		 * up and reachable: either an ACK if the connection is
6455 		 * still alive, or an RST if the peer has closed the
6456 		 * connection due to timeout or reboot. Using sequence
6457 		 * number tp->snd_una-1 causes the transmitted zero-length
6458 		 * segment to lie outside the receive window; by the
6459 		 * protocol spec, this requires the correspondent TCP to
6460 		 * respond.
6461 		 */
6462 		KMOD_TCPSTAT_INC(tcps_keepprobe);
6463 		t_template = tcpip_maketemplate(inp);
6464 		if (t_template) {
6465 			if (rack->forced_ack == 0) {
6466 				rack->forced_ack = 1;
6467 				rack->r_ctl.forced_ack_ts = tcp_get_usecs(NULL);
6468 			} else {
6469 				rack->probe_not_answered = 1;
6470 			}
6471 			tcp_respond(tp, t_template->tt_ipgen,
6472 			    &t_template->tt_t, (struct mbuf *)NULL,
6473 			    tp->rcv_nxt, tp->snd_una - 1, 0);
6474 			free(t_template, M_TEMP);
6475 		}
6476 	}
6477 	rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
6478 	return (1);
6479 dropit:
6480 	KMOD_TCPSTAT_INC(tcps_keepdrops);
6481 	tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
6482 	return (-ETIMEDOUT);	/* tcp_drop() */
6483 }
6484 
6485 /*
6486  * Retransmit helper function, clear up all the ack
6487  * flags and take care of important book keeping.
6488  */
6489 static void
6490 rack_remxt_tmr(struct tcpcb *tp)
6491 {
6492 	/*
6493 	 * The retransmit timer went off, all sack'd blocks must be
6494 	 * un-acked.
6495 	 */
6496 	struct rack_sendmap *rsm, *trsm = NULL;
6497 	struct tcp_rack *rack;
6498 
6499 	rack = (struct tcp_rack *)tp->t_fb_ptr;
6500 	rack_timer_cancel(tp, rack, tcp_get_usecs(NULL), __LINE__);
6501 	rack_log_to_event(rack, RACK_TO_FRM_TMR, NULL);
6502 	if (rack->r_state && (rack->r_state != tp->t_state))
6503 		rack_set_state(tp, rack);
6504 	/*
6505 	 * Ideally we would like to be able to
6506 	 * mark SACK-PASS on anything not acked here.
6507 	 *
6508 	 * However, if we do that we would burst out
6509 	 * all that data 1ms apart. This would be unwise,
6510 	 * so for now we will just let the normal rxt timer
6511 	 * and tlp timer take care of it.
6512 	 *
6513 	 * Also we really need to stick them back in sequence
6514 	 * order. This way we send in the proper order and any
6515 	 * sacks that come floating in will "re-ack" the data.
6516 	 * To do this we zap the tmap with an INIT and then
6517 	 * walk through and place every rsm in the RB tree
6518 	 * back in its seq ordered place.
6519 	 */
6520 	TAILQ_INIT(&rack->r_ctl.rc_tmap);
6521 	RB_FOREACH(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
6522 		rsm->r_dupack = 0;
6523 		rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
6524 		/* We must re-add it back to the tlist */
6525 		if (trsm == NULL) {
6526 			TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
6527 		} else {
6528 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, trsm, rsm, r_tnext);
6529 		}
6530 		rsm->r_in_tmap = 1;
6531 		trsm = rsm;
6532 		if (rsm->r_flags & RACK_ACKED)
6533 			rsm->r_flags |= RACK_WAS_ACKED;
6534 		rsm->r_flags &= ~(RACK_ACKED | RACK_SACK_PASSED | RACK_WAS_SACKPASS | RACK_RWND_COLLAPSED);
6535 		rsm->r_flags |= RACK_MUST_RXT;
6536 	}
6537 	/* Clear the count (we just un-acked them) */
6538 	rack->r_ctl.rc_last_timeout_snduna = tp->snd_una;
6539 	rack->r_ctl.rc_sacked = 0;
6540 	rack->r_ctl.rc_sacklast = NULL;
6541 	rack->r_ctl.rc_agg_delayed = 0;
6542 	rack->r_early = 0;
6543 	rack->r_ctl.rc_agg_early = 0;
6544 	rack->r_late = 0;
6545 	/* Clear the tlp rtx mark */
6546 	rack->r_ctl.rc_resend = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
6547 	if (rack->r_ctl.rc_resend != NULL)
6548 		rack->r_ctl.rc_resend->r_flags |= RACK_TO_REXT;
6549 	rack->r_ctl.rc_prr_sndcnt = 0;
6550 	rack_log_to_prr(rack, 6, 0, __LINE__);
6551 	rack->r_timer_override = 1;
6552 	if ((((tp->t_flags & TF_SACK_PERMIT) == 0)
6553 #ifdef NETFLIX_EXP_DETECTION
6554 	    || (rack->sack_attack_disable != 0)
6555 #endif
6556 		    ) && ((tp->t_flags & TF_SENTFIN) == 0)) {
6557 		/*
6558 		 * For non-sack customers new data
6559 		 * needs to go out as retransmits until
6560 		 * we retransmit up to snd_max.
6561 		 */
6562 		rack->r_must_retran = 1;
6563 		rack->r_ctl.rc_out_at_rto = ctf_flight_size(rack->rc_tp,
6564 						rack->r_ctl.rc_sacked);
6565 	}
6566 	rack->r_ctl.rc_snd_max_at_rto = tp->snd_max;
6567 }
6568 
6569 static void
6570 rack_convert_rtts(struct tcpcb *tp)
6571 {
6572 	if (tp->t_srtt > 1) {
6573 		uint32_t val, frac;
6574 
6575 		val = tp->t_srtt >> TCP_RTT_SHIFT;
6576 		frac = tp->t_srtt & 0x1f;
6577 		tp->t_srtt = TICKS_2_USEC(val);
6578 		/*
6579 		 * frac is the fractional part of the srtt (if any)
6580 		 * but its in ticks and every bit represents
6581 		 * 1/32nd of a hz.
6582 		 */
6583 		if (frac) {
6584 			if (hz == 1000) {
6585 				frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_MSEC) / (uint64_t)TCP_RTT_SCALE);
6586 			} else {
6587 				frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_SEC) / ((uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE));
6588 			}
6589 			tp->t_srtt += frac;
6590 		}
6591 	}
6592 	if (tp->t_rttvar) {
6593 		uint32_t val, frac;
6594 
6595 		val = tp->t_rttvar >> TCP_RTTVAR_SHIFT;
6596 		frac = tp->t_rttvar & 0x1f;
6597 		tp->t_rttvar = TICKS_2_USEC(val);
6598 		/*
6599 		 * frac is the fractional part of the srtt (if any)
6600 		 * but its in ticks and every bit represents
6601 		 * 1/32nd of a hz.
6602 		 */
6603 		if (frac) {
6604 			if (hz == 1000) {
6605 				frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_MSEC) / (uint64_t)TCP_RTT_SCALE);
6606 			} else {
6607 				frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_SEC) / ((uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE));
6608 			}
6609 			tp->t_rttvar += frac;
6610 		}
6611 	}
6612 	tp->t_rxtcur = RACK_REXMTVAL(tp);
6613 	if (TCPS_HAVEESTABLISHED(tp->t_state)) {
6614 		tp->t_rxtcur += TICKS_2_USEC(tcp_rexmit_slop);
6615 	}
6616 	if (tp->t_rxtcur > rack_rto_max) {
6617 		tp->t_rxtcur = rack_rto_max;
6618 	}
6619 }
6620 
6621 static void
6622 rack_cc_conn_init(struct tcpcb *tp)
6623 {
6624 	struct tcp_rack *rack;
6625 	uint32_t srtt;
6626 
6627 	rack = (struct tcp_rack *)tp->t_fb_ptr;
6628 	srtt = tp->t_srtt;
6629 	cc_conn_init(tp);
6630 	/*
6631 	 * Now convert to rack's internal format,
6632 	 * if required.
6633 	 */
6634 	if ((srtt == 0) && (tp->t_srtt != 0))
6635 		rack_convert_rtts(tp);
6636 	/*
6637 	 * We want a chance to stay in slowstart as
6638 	 * we create a connection. TCP spec says that
6639 	 * initially ssthresh is infinite. For our
6640 	 * purposes that is the snd_wnd.
6641 	 */
6642 	if (tp->snd_ssthresh < tp->snd_wnd) {
6643 		tp->snd_ssthresh = tp->snd_wnd;
6644 	}
6645 	/*
6646 	 * We also want to assure a IW worth of
6647 	 * data can get inflight.
6648 	 */
6649 	if (rc_init_window(rack) < tp->snd_cwnd)
6650 		tp->snd_cwnd = rc_init_window(rack);
6651 }
6652 
6653 /*
6654  * Re-transmit timeout! If we drop the PCB we will return 1, otherwise
6655  * we will setup to retransmit the lowest seq number outstanding.
6656  */
6657 static int
6658 rack_timeout_rxt(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6659 {
6660 	int32_t rexmt;
6661 	int32_t retval = 0;
6662 	bool isipv6;
6663 
6664 	if (tp->t_timers->tt_flags & TT_STOPPED) {
6665 		return (1);
6666 	}
6667 	if ((tp->t_flags & TF_GPUTINPROG) &&
6668 	    (tp->t_rxtshift)) {
6669 		/*
6670 		 * We have had a second timeout
6671 		 * measurements on successive rxt's are not profitable.
6672 		 * It is unlikely to be of any use (the network is
6673 		 * broken or the client went away).
6674 		 */
6675 		tp->t_flags &= ~TF_GPUTINPROG;
6676 		rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
6677 					   rack->r_ctl.rc_gp_srtt /*flex1*/,
6678 					   tp->gput_seq,
6679 					   0, 0, 18, __LINE__, NULL, 0);
6680 	}
6681 	if (ctf_progress_timeout_check(tp, false)) {
6682 		tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
6683 		rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
6684 		return (-ETIMEDOUT);	/* tcp_drop() */
6685 	}
6686 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT;
6687 	rack->r_ctl.retran_during_recovery = 0;
6688 	rack->r_ctl.dsack_byte_cnt = 0;
6689 	if (IN_FASTRECOVERY(tp->t_flags))
6690 		tp->t_flags |= TF_WASFRECOVERY;
6691 	else
6692 		tp->t_flags &= ~TF_WASFRECOVERY;
6693 	if (IN_CONGRECOVERY(tp->t_flags))
6694 		tp->t_flags |= TF_WASCRECOVERY;
6695 	else
6696 		tp->t_flags &= ~TF_WASCRECOVERY;
6697 	if (TCPS_HAVEESTABLISHED(tp->t_state) &&
6698 	    (tp->snd_una == tp->snd_max)) {
6699 		/* Nothing outstanding .. nothing to do */
6700 		return (0);
6701 	}
6702 	if (rack->r_ctl.dsack_persist) {
6703 		rack->r_ctl.dsack_persist--;
6704 		if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
6705 			rack->r_ctl.num_dsack = 0;
6706 		}
6707 		rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
6708 	}
6709 	/*
6710 	 * Rack can only run one timer  at a time, so we cannot
6711 	 * run a KEEPINIT (gating SYN sending) and a retransmit
6712 	 * timer for the SYN. So if we are in a front state and
6713 	 * have a KEEPINIT timer we need to check the first transmit
6714 	 * against now to see if we have exceeded the KEEPINIT time
6715 	 * (if one is set).
6716 	 */
6717 	if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) &&
6718 	    (TP_KEEPINIT(tp) != 0)) {
6719 		struct rack_sendmap *rsm;
6720 
6721 		rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
6722 		if (rsm) {
6723 			/* Ok we have something outstanding to test keepinit with */
6724 			if ((TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) &&
6725 			    ((cts - (uint32_t)rsm->r_tim_lastsent[0]) >= TICKS_2_USEC(TP_KEEPINIT(tp)))) {
6726 				/* We have exceeded the KEEPINIT time */
6727 				tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
6728 				goto drop_it;
6729 			}
6730 		}
6731 	}
6732 	/*
6733 	 * Retransmission timer went off.  Message has not been acked within
6734 	 * retransmit interval.  Back off to a longer retransmit interval
6735 	 * and retransmit one segment.
6736 	 */
6737 	rack_remxt_tmr(tp);
6738 	if ((rack->r_ctl.rc_resend == NULL) ||
6739 	    ((rack->r_ctl.rc_resend->r_flags & RACK_RWND_COLLAPSED) == 0)) {
6740 		/*
6741 		 * If the rwnd collapsed on
6742 		 * the one we are retransmitting
6743 		 * it does not count against the
6744 		 * rxt count.
6745 		 */
6746 		tp->t_rxtshift++;
6747 	}
6748 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT) {
6749 		tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
6750 drop_it:
6751 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
6752 		KMOD_TCPSTAT_INC(tcps_timeoutdrop);
6753 		/* XXXGL: previously t_softerror was casted to uint16_t */
6754 		MPASS(tp->t_softerror >= 0);
6755 		retval = tp->t_softerror ? -tp->t_softerror : -ETIMEDOUT;
6756 		goto out;	/* tcp_drop() */
6757 	}
6758 	if (tp->t_state == TCPS_SYN_SENT) {
6759 		/*
6760 		 * If the SYN was retransmitted, indicate CWND to be limited
6761 		 * to 1 segment in cc_conn_init().
6762 		 */
6763 		tp->snd_cwnd = 1;
6764 	} else if (tp->t_rxtshift == 1) {
6765 		/*
6766 		 * first retransmit; record ssthresh and cwnd so they can be
6767 		 * recovered if this turns out to be a "bad" retransmit. A
6768 		 * retransmit is considered "bad" if an ACK for this segment
6769 		 * is received within RTT/2 interval; the assumption here is
6770 		 * that the ACK was already in flight.  See "On Estimating
6771 		 * End-to-End Network Path Properties" by Allman and Paxson
6772 		 * for more details.
6773 		 */
6774 		tp->snd_cwnd_prev = tp->snd_cwnd;
6775 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
6776 		tp->snd_recover_prev = tp->snd_recover;
6777 		tp->t_badrxtwin = ticks + (USEC_2_TICKS(tp->t_srtt)/2);
6778 		tp->t_flags |= TF_PREVVALID;
6779 	} else if ((tp->t_flags & TF_RCVD_TSTMP) == 0)
6780 		tp->t_flags &= ~TF_PREVVALID;
6781 	KMOD_TCPSTAT_INC(tcps_rexmttimeo);
6782 	if ((tp->t_state == TCPS_SYN_SENT) ||
6783 	    (tp->t_state == TCPS_SYN_RECEIVED))
6784 		rexmt = RACK_INITIAL_RTO * tcp_backoff[tp->t_rxtshift];
6785 	else
6786 		rexmt = max(rack_rto_min, (tp->t_srtt + (tp->t_rttvar << 2))) * tcp_backoff[tp->t_rxtshift];
6787 
6788 	RACK_TCPT_RANGESET(tp->t_rxtcur, rexmt,
6789 	   max(rack_rto_min, rexmt), rack_rto_max, rack->r_ctl.timer_slop);
6790 	/*
6791 	 * We enter the path for PLMTUD if connection is established or, if
6792 	 * connection is FIN_WAIT_1 status, reason for the last is that if
6793 	 * amount of data we send is very small, we could send it in couple
6794 	 * of packets and process straight to FIN. In that case we won't
6795 	 * catch ESTABLISHED state.
6796 	 */
6797 #ifdef INET6
6798 	isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? true : false;
6799 #else
6800 	isipv6 = false;
6801 #endif
6802 	if (((V_tcp_pmtud_blackhole_detect == 1) ||
6803 	    (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) ||
6804 	    (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) &&
6805 	    ((tp->t_state == TCPS_ESTABLISHED) ||
6806 	    (tp->t_state == TCPS_FIN_WAIT_1))) {
6807 		/*
6808 		 * Idea here is that at each stage of mtu probe (usually,
6809 		 * 1448 -> 1188 -> 524) should be given 2 chances to recover
6810 		 * before further clamping down. 'tp->t_rxtshift % 2 == 0'
6811 		 * should take care of that.
6812 		 */
6813 		if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) ==
6814 		    (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) &&
6815 		    (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 &&
6816 		    tp->t_rxtshift % 2 == 0)) {
6817 			/*
6818 			 * Enter Path MTU Black-hole Detection mechanism: -
6819 			 * Disable Path MTU Discovery (IP "DF" bit). -
6820 			 * Reduce MTU to lower value than what we negotiated
6821 			 * with peer.
6822 			 */
6823 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
6824 				/* Record that we may have found a black hole. */
6825 				tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
6826 				/* Keep track of previous MSS. */
6827 				tp->t_pmtud_saved_maxseg = tp->t_maxseg;
6828 			}
6829 
6830 			/*
6831 			 * Reduce the MSS to blackhole value or to the
6832 			 * default in an attempt to retransmit.
6833 			 */
6834 #ifdef INET6
6835 			if (isipv6 &&
6836 			    tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) {
6837 				/* Use the sysctl tuneable blackhole MSS. */
6838 				tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
6839 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
6840 			} else if (isipv6) {
6841 				/* Use the default MSS. */
6842 				tp->t_maxseg = V_tcp_v6mssdflt;
6843 				/*
6844 				 * Disable Path MTU Discovery when we switch
6845 				 * to minmss.
6846 				 */
6847 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
6848 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
6849 			}
6850 #endif
6851 #if defined(INET6) && defined(INET)
6852 			else
6853 #endif
6854 #ifdef INET
6855 			if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) {
6856 				/* Use the sysctl tuneable blackhole MSS. */
6857 				tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
6858 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
6859 			} else {
6860 				/* Use the default MSS. */
6861 				tp->t_maxseg = V_tcp_mssdflt;
6862 				/*
6863 				 * Disable Path MTU Discovery when we switch
6864 				 * to minmss.
6865 				 */
6866 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
6867 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
6868 			}
6869 #endif
6870 		} else {
6871 			/*
6872 			 * If further retransmissions are still unsuccessful
6873 			 * with a lowered MTU, maybe this isn't a blackhole
6874 			 * and we restore the previous MSS and blackhole
6875 			 * detection flags. The limit '6' is determined by
6876 			 * giving each probe stage (1448, 1188, 524) 2
6877 			 * chances to recover.
6878 			 */
6879 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
6880 			    (tp->t_rxtshift >= 6)) {
6881 				tp->t_flags2 |= TF2_PLPMTU_PMTUD;
6882 				tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
6883 				tp->t_maxseg = tp->t_pmtud_saved_maxseg;
6884 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed);
6885 			}
6886 		}
6887 	}
6888 	/*
6889 	 * Disable RFC1323 and SACK if we haven't got any response to
6890 	 * our third SYN to work-around some broken terminal servers
6891 	 * (most of which have hopefully been retired) that have bad VJ
6892 	 * header compression code which trashes TCP segments containing
6893 	 * unknown-to-them TCP options.
6894 	 */
6895 	if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
6896 	    (tp->t_rxtshift == 3))
6897 		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
6898 	/*
6899 	 * If we backed off this far, our srtt estimate is probably bogus.
6900 	 * Clobber it so we'll take the next rtt measurement as our srtt;
6901 	 * move the current srtt into rttvar to keep the current retransmit
6902 	 * times until then.
6903 	 */
6904 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
6905 #ifdef INET6
6906 		if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
6907 			in6_losing(tp->t_inpcb);
6908 		else
6909 #endif
6910 			in_losing(tp->t_inpcb);
6911 		tp->t_rttvar += tp->t_srtt;
6912 		tp->t_srtt = 0;
6913 	}
6914 	sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
6915 	tp->snd_recover = tp->snd_max;
6916 	tp->t_flags |= TF_ACKNOW;
6917 	tp->t_rtttime = 0;
6918 	rack_cong_signal(tp, CC_RTO, tp->snd_una, __LINE__);
6919 out:
6920 	return (retval);
6921 }
6922 
6923 static int
6924 rack_process_timers(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t hpts_calling, uint8_t *doing_tlp)
6925 {
6926 	int32_t ret = 0;
6927 	int32_t timers = (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK);
6928 
6929 	if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
6930 	    (tp->t_flags & TF_GPUTINPROG)) {
6931 		/*
6932 		 * We have a goodput in progress
6933 		 * and we have entered a late state.
6934 		 * Do we have enough data in the sb
6935 		 * to handle the GPUT request?
6936 		 */
6937 		uint32_t bytes;
6938 
6939 		bytes = tp->gput_ack - tp->gput_seq;
6940 		if (SEQ_GT(tp->gput_seq, tp->snd_una))
6941 			bytes += tp->gput_seq - tp->snd_una;
6942 		if (bytes > sbavail(&tp->t_inpcb->inp_socket->so_snd)) {
6943 			/*
6944 			 * There are not enough bytes in the socket
6945 			 * buffer that have been sent to cover this
6946 			 * measurement. Cancel it.
6947 			 */
6948 			rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
6949 						   rack->r_ctl.rc_gp_srtt /*flex1*/,
6950 						   tp->gput_seq,
6951 						   0, 0, 18, __LINE__, NULL, 0);
6952 			tp->t_flags &= ~TF_GPUTINPROG;
6953 		}
6954 	}
6955 	if (timers == 0) {
6956 		return (0);
6957 	}
6958 	if (tp->t_state == TCPS_LISTEN) {
6959 		/* no timers on listen sockets */
6960 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)
6961 			return (0);
6962 		return (1);
6963 	}
6964 	if ((timers & PACE_TMR_RACK) &&
6965 	    rack->rc_on_min_to) {
6966 		/*
6967 		 * For the rack timer when we
6968 		 * are on a min-timeout (which means rrr_conf = 3)
6969 		 * we don't want to check the timer. It may
6970 		 * be going off for a pace and thats ok we
6971 		 * want to send the retransmit (if its ready).
6972 		 *
6973 		 * If its on a normal rack timer (non-min) then
6974 		 * we will check if its expired.
6975 		 */
6976 		goto skip_time_check;
6977 	}
6978 	if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
6979 		uint32_t left;
6980 
6981 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
6982 			ret = -1;
6983 			rack_log_to_processing(rack, cts, ret, 0);
6984 			return (0);
6985 		}
6986 		if (hpts_calling == 0) {
6987 			/*
6988 			 * A user send or queued mbuf (sack) has called us? We
6989 			 * return 0 and let the pacing guards
6990 			 * deal with it if they should or
6991 			 * should not cause a send.
6992 			 */
6993 			ret = -2;
6994 			rack_log_to_processing(rack, cts, ret, 0);
6995 			return (0);
6996 		}
6997 		/*
6998 		 * Ok our timer went off early and we are not paced false
6999 		 * alarm, go back to sleep.
7000 		 */
7001 		ret = -3;
7002 		left = rack->r_ctl.rc_timer_exp - cts;
7003 		tcp_hpts_insert(tp->t_inpcb, HPTS_MS_TO_SLOTS(left));
7004 		rack_log_to_processing(rack, cts, ret, left);
7005 		return (1);
7006 	}
7007 skip_time_check:
7008 	rack->rc_tmr_stopped = 0;
7009 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK;
7010 	if (timers & PACE_TMR_DELACK) {
7011 		ret = rack_timeout_delack(tp, rack, cts);
7012 	} else if (timers & PACE_TMR_RACK) {
7013 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
7014 		rack->r_fast_output = 0;
7015 		ret = rack_timeout_rack(tp, rack, cts);
7016 	} else if (timers & PACE_TMR_TLP) {
7017 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
7018 		ret = rack_timeout_tlp(tp, rack, cts, doing_tlp);
7019 	} else if (timers & PACE_TMR_RXT) {
7020 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
7021 		rack->r_fast_output = 0;
7022 		ret = rack_timeout_rxt(tp, rack, cts);
7023 	} else if (timers & PACE_TMR_PERSIT) {
7024 		ret = rack_timeout_persist(tp, rack, cts);
7025 	} else if (timers & PACE_TMR_KEEP) {
7026 		ret = rack_timeout_keepalive(tp, rack, cts);
7027 	}
7028 	rack_log_to_processing(rack, cts, ret, timers);
7029 	return (ret);
7030 }
7031 
7032 static void
7033 rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line)
7034 {
7035 	struct timeval tv;
7036 	uint32_t us_cts, flags_on_entry;
7037 	uint8_t hpts_removed = 0;
7038 
7039 	flags_on_entry = rack->r_ctl.rc_hpts_flags;
7040 	us_cts = tcp_get_usecs(&tv);
7041 	if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
7042 	    ((TSTMP_GEQ(us_cts, rack->r_ctl.rc_last_output_to)) ||
7043 	     ((tp->snd_max - tp->snd_una) == 0))) {
7044 		tcp_hpts_remove(rack->rc_inp);
7045 		hpts_removed = 1;
7046 		/* If we were not delayed cancel out the flag. */
7047 		if ((tp->snd_max - tp->snd_una) == 0)
7048 			rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
7049 		rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry);
7050 	}
7051 	if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
7052 		rack->rc_tmr_stopped = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
7053 		if (tcp_in_hpts(rack->rc_inp) &&
7054 		    ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)) {
7055 			/*
7056 			 * Canceling timer's when we have no output being
7057 			 * paced. We also must remove ourselves from the
7058 			 * hpts.
7059 			 */
7060 			tcp_hpts_remove(rack->rc_inp);
7061 			hpts_removed = 1;
7062 		}
7063 		rack->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK);
7064 	}
7065 	if (hpts_removed == 0)
7066 		rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry);
7067 }
7068 
7069 static void
7070 rack_timer_stop(struct tcpcb *tp, uint32_t timer_type)
7071 {
7072 	return;
7073 }
7074 
7075 static int
7076 rack_stopall(struct tcpcb *tp)
7077 {
7078 	struct tcp_rack *rack;
7079 	rack = (struct tcp_rack *)tp->t_fb_ptr;
7080 	rack->t_timers_stopped = 1;
7081 	return (0);
7082 }
7083 
7084 static void
7085 rack_timer_activate(struct tcpcb *tp, uint32_t timer_type, uint32_t delta)
7086 {
7087 	return;
7088 }
7089 
7090 static int
7091 rack_timer_active(struct tcpcb *tp, uint32_t timer_type)
7092 {
7093 	return (0);
7094 }
7095 
7096 static void
7097 rack_stop_all_timers(struct tcpcb *tp)
7098 {
7099 	struct tcp_rack *rack;
7100 
7101 	/*
7102 	 * Assure no timers are running.
7103 	 */
7104 	if (tcp_timer_active(tp, TT_PERSIST)) {
7105 		/* We enter in persists, set the flag appropriately */
7106 		rack = (struct tcp_rack *)tp->t_fb_ptr;
7107 		rack->rc_in_persist = 1;
7108 	}
7109 	tcp_timer_suspend(tp, TT_PERSIST);
7110 	tcp_timer_suspend(tp, TT_REXMT);
7111 	tcp_timer_suspend(tp, TT_KEEP);
7112 	tcp_timer_suspend(tp, TT_DELACK);
7113 }
7114 
7115 static void
7116 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
7117     struct rack_sendmap *rsm, uint64_t ts, uint16_t add_flag)
7118 {
7119 	int32_t idx;
7120 
7121 	rsm->r_rtr_cnt++;
7122 	rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
7123 	rsm->r_dupack = 0;
7124 	if (rsm->r_rtr_cnt > RACK_NUM_OF_RETRANS) {
7125 		rsm->r_rtr_cnt = RACK_NUM_OF_RETRANS;
7126 		rsm->r_flags |= RACK_OVERMAX;
7127 	}
7128 	if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & RACK_TLP) == 0)) {
7129 		rack->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start);
7130 		rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start);
7131 	}
7132 	idx = rsm->r_rtr_cnt - 1;
7133 	rsm->r_tim_lastsent[idx] = ts;
7134 	/*
7135 	 * Here we don't add in the len of send, since its already
7136 	 * in snduna <->snd_max.
7137 	 */
7138 	rsm->r_fas = ctf_flight_size(rack->rc_tp,
7139 				     rack->r_ctl.rc_sacked);
7140 	if (rsm->r_flags & RACK_ACKED) {
7141 		/* Problably MTU discovery messing with us */
7142 		rsm->r_flags &= ~RACK_ACKED;
7143 		rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
7144 	}
7145 	if (rsm->r_in_tmap) {
7146 		TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
7147 		rsm->r_in_tmap = 0;
7148 	}
7149 	TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
7150 	rsm->r_in_tmap = 1;
7151 	/* Take off the must retransmit flag, if its on */
7152 	if (rsm->r_flags & RACK_MUST_RXT) {
7153 		if (rack->r_must_retran)
7154 			rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start);
7155 		if (SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) {
7156 			/*
7157 			 * We have retransmitted all we need. Clear
7158 			 * any must retransmit flags.
7159 			 */
7160 			rack->r_must_retran = 0;
7161 			rack->r_ctl.rc_out_at_rto = 0;
7162 		}
7163 		rsm->r_flags &= ~RACK_MUST_RXT;
7164 	}
7165 	if (rsm->r_flags & RACK_SACK_PASSED) {
7166 		/* We have retransmitted due to the SACK pass */
7167 		rsm->r_flags &= ~RACK_SACK_PASSED;
7168 		rsm->r_flags |= RACK_WAS_SACKPASS;
7169 	}
7170 }
7171 
7172 static uint32_t
7173 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack,
7174     struct rack_sendmap *rsm, uint64_t ts, int32_t *lenp, uint16_t add_flag)
7175 {
7176 	/*
7177 	 * We (re-)transmitted starting at rsm->r_start for some length
7178 	 * (possibly less than r_end.
7179 	 */
7180 	struct rack_sendmap *nrsm;
7181 #ifdef INVARIANTS
7182 	struct rack_sendmap *insret;
7183 #endif
7184 	uint32_t c_end;
7185 	int32_t len;
7186 
7187 	len = *lenp;
7188 	c_end = rsm->r_start + len;
7189 	if (SEQ_GEQ(c_end, rsm->r_end)) {
7190 		/*
7191 		 * We retransmitted the whole piece or more than the whole
7192 		 * slopping into the next rsm.
7193 		 */
7194 		rack_update_rsm(tp, rack, rsm, ts, add_flag);
7195 		if (c_end == rsm->r_end) {
7196 			*lenp = 0;
7197 			return (0);
7198 		} else {
7199 			int32_t act_len;
7200 
7201 			/* Hangs over the end return whats left */
7202 			act_len = rsm->r_end - rsm->r_start;
7203 			*lenp = (len - act_len);
7204 			return (rsm->r_end);
7205 		}
7206 		/* We don't get out of this block. */
7207 	}
7208 	/*
7209 	 * Here we retransmitted less than the whole thing which means we
7210 	 * have to split this into what was transmitted and what was not.
7211 	 */
7212 	nrsm = rack_alloc_full_limit(rack);
7213 	if (nrsm == NULL) {
7214 		/*
7215 		 * We can't get memory, so lets not proceed.
7216 		 */
7217 		*lenp = 0;
7218 		return (0);
7219 	}
7220 	/*
7221 	 * So here we are going to take the original rsm and make it what we
7222 	 * retransmitted. nrsm will be the tail portion we did not
7223 	 * retransmit. For example say the chunk was 1, 11 (10 bytes). And
7224 	 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to
7225 	 * 1, 6 and the new piece will be 6, 11.
7226 	 */
7227 	rack_clone_rsm(rack, nrsm, rsm, c_end);
7228 	nrsm->r_dupack = 0;
7229 	rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2);
7230 #ifndef INVARIANTS
7231 	(void)RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
7232 #else
7233 	insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
7234 	if (insret != NULL) {
7235 		panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
7236 		      nrsm, insret, rack, rsm);
7237 	}
7238 #endif
7239 	if (rsm->r_in_tmap) {
7240 		TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7241 		nrsm->r_in_tmap = 1;
7242 	}
7243 	rsm->r_flags &= (~RACK_HAS_FIN);
7244 	rack_update_rsm(tp, rack, rsm, ts, add_flag);
7245 	/* Log a split of rsm into rsm and nrsm */
7246 	rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
7247 	*lenp = 0;
7248 	return (0);
7249 }
7250 
7251 static void
7252 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
7253 		uint32_t seq_out, uint16_t th_flags, int32_t err, uint64_t cts,
7254 		struct rack_sendmap *hintrsm, uint16_t add_flag, struct mbuf *s_mb, uint32_t s_moff, int hw_tls)
7255 {
7256 	struct tcp_rack *rack;
7257 	struct rack_sendmap *rsm, *nrsm, fe;
7258 #ifdef INVARIANTS
7259 	struct rack_sendmap *insret;
7260 #endif
7261 	register uint32_t snd_max, snd_una;
7262 
7263 	/*
7264 	 * Add to the RACK log of packets in flight or retransmitted. If
7265 	 * there is a TS option we will use the TS echoed, if not we will
7266 	 * grab a TS.
7267 	 *
7268 	 * Retransmissions will increment the count and move the ts to its
7269 	 * proper place. Note that if options do not include TS's then we
7270 	 * won't be able to effectively use the ACK for an RTT on a retran.
7271 	 *
7272 	 * Notes about r_start and r_end. Lets consider a send starting at
7273 	 * sequence 1 for 10 bytes. In such an example the r_start would be
7274 	 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11.
7275 	 * This means that r_end is actually the first sequence for the next
7276 	 * slot (11).
7277 	 *
7278 	 */
7279 	/*
7280 	 * If err is set what do we do XXXrrs? should we not add the thing?
7281 	 * -- i.e. return if err != 0 or should we pretend we sent it? --
7282 	 * i.e. proceed with add ** do this for now.
7283 	 */
7284 	INP_WLOCK_ASSERT(tp->t_inpcb);
7285 	if (err)
7286 		/*
7287 		 * We don't log errors -- we could but snd_max does not
7288 		 * advance in this case either.
7289 		 */
7290 		return;
7291 
7292 	if (th_flags & TH_RST) {
7293 		/*
7294 		 * We don't log resets and we return immediately from
7295 		 * sending
7296 		 */
7297 		return;
7298 	}
7299 	rack = (struct tcp_rack *)tp->t_fb_ptr;
7300 	snd_una = tp->snd_una;
7301 	snd_max = tp->snd_max;
7302 	if (th_flags & (TH_SYN | TH_FIN)) {
7303 		/*
7304 		 * The call to rack_log_output is made before bumping
7305 		 * snd_max. This means we can record one extra byte on a SYN
7306 		 * or FIN if seq_out is adding more on and a FIN is present
7307 		 * (and we are not resending).
7308 		 */
7309 		if ((th_flags & TH_SYN) && (seq_out == tp->iss))
7310 			len++;
7311 		if (th_flags & TH_FIN)
7312 			len++;
7313 		if (SEQ_LT(snd_max, tp->snd_nxt)) {
7314 			/*
7315 			 * The add/update as not been done for the FIN/SYN
7316 			 * yet.
7317 			 */
7318 			snd_max = tp->snd_nxt;
7319 		}
7320 	}
7321 	if (SEQ_LEQ((seq_out + len), snd_una)) {
7322 		/* Are sending an old segment to induce an ack (keep-alive)? */
7323 		return;
7324 	}
7325 	if (SEQ_LT(seq_out, snd_una)) {
7326 		/* huh? should we panic? */
7327 		uint32_t end;
7328 
7329 		end = seq_out + len;
7330 		seq_out = snd_una;
7331 		if (SEQ_GEQ(end, seq_out))
7332 			len = end - seq_out;
7333 		else
7334 			len = 0;
7335 	}
7336 	if (len == 0) {
7337 		/* We don't log zero window probes */
7338 		return;
7339 	}
7340 	if (IN_FASTRECOVERY(tp->t_flags)) {
7341 		rack->r_ctl.rc_prr_out += len;
7342 	}
7343 	/* First question is it a retransmission or new? */
7344 	if (seq_out == snd_max) {
7345 		/* Its new */
7346 again:
7347 		rsm = rack_alloc(rack);
7348 		if (rsm == NULL) {
7349 			/*
7350 			 * Hmm out of memory and the tcb got destroyed while
7351 			 * we tried to wait.
7352 			 */
7353 			return;
7354 		}
7355 		if (th_flags & TH_FIN) {
7356 			rsm->r_flags = RACK_HAS_FIN|add_flag;
7357 		} else {
7358 			rsm->r_flags = add_flag;
7359 		}
7360 		if (hw_tls)
7361 			rsm->r_hw_tls = 1;
7362 		rsm->r_tim_lastsent[0] = cts;
7363 		rsm->r_rtr_cnt = 1;
7364 		rsm->r_rtr_bytes = 0;
7365 		if (th_flags & TH_SYN) {
7366 			/* The data space is one beyond snd_una */
7367 			rsm->r_flags |= RACK_HAS_SYN;
7368 		}
7369 		rsm->r_start = seq_out;
7370 		rsm->r_end = rsm->r_start + len;
7371 		rsm->r_dupack = 0;
7372 		/*
7373 		 * save off the mbuf location that
7374 		 * sndmbuf_noadv returned (which is
7375 		 * where we started copying from)..
7376 		 */
7377 		rsm->m = s_mb;
7378 		rsm->soff = s_moff;
7379 		/*
7380 		 * Here we do add in the len of send, since its not yet
7381 		 * reflected in in snduna <->snd_max
7382 		 */
7383 		rsm->r_fas = (ctf_flight_size(rack->rc_tp,
7384 					      rack->r_ctl.rc_sacked) +
7385 			      (rsm->r_end - rsm->r_start));
7386 		/* rsm->m will be NULL if RACK_HAS_SYN or RACK_HAS_FIN is set */
7387 		if (rsm->m) {
7388 			if (rsm->m->m_len <= rsm->soff) {
7389 				/*
7390 				 * XXXrrs Question, will this happen?
7391 				 *
7392 				 * If sbsndptr is set at the correct place
7393 				 * then s_moff should always be somewhere
7394 				 * within rsm->m. But if the sbsndptr was
7395 				 * off then that won't be true. If it occurs
7396 				 * we need to walkout to the correct location.
7397 				 */
7398 				struct mbuf *lm;
7399 
7400 				lm = rsm->m;
7401 				while (lm->m_len <= rsm->soff) {
7402 					rsm->soff -= lm->m_len;
7403 					lm = lm->m_next;
7404 					KASSERT(lm != NULL, ("%s rack:%p lm goes null orig_off:%u origmb:%p rsm->soff:%u",
7405 							     __func__, rack, s_moff, s_mb, rsm->soff));
7406 				}
7407 				rsm->m = lm;
7408 			}
7409 			rsm->orig_m_len = rsm->m->m_len;
7410 		} else
7411 			rsm->orig_m_len = 0;
7412 		rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
7413 		/* Log a new rsm */
7414 		rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_NEW, 0, __LINE__);
7415 #ifndef INVARIANTS
7416 		(void)RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7417 #else
7418 		insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7419 		if (insret != NULL) {
7420 			panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
7421 			      nrsm, insret, rack, rsm);
7422 		}
7423 #endif
7424 		TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
7425 		rsm->r_in_tmap = 1;
7426 		/*
7427 		 * Special case detection, is there just a single
7428 		 * packet outstanding when we are not in recovery?
7429 		 *
7430 		 * If this is true mark it so.
7431 		 */
7432 		if ((IN_FASTRECOVERY(tp->t_flags) == 0) &&
7433 		    (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) == ctf_fixed_maxseg(tp))) {
7434 			struct rack_sendmap *prsm;
7435 
7436 			prsm = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7437 			if (prsm)
7438 				prsm->r_one_out_nr = 1;
7439 		}
7440 		return;
7441 	}
7442 	/*
7443 	 * If we reach here its a retransmission and we need to find it.
7444 	 */
7445 	memset(&fe, 0, sizeof(fe));
7446 more:
7447 	if (hintrsm && (hintrsm->r_start == seq_out)) {
7448 		rsm = hintrsm;
7449 		hintrsm = NULL;
7450 	} else {
7451 		/* No hints sorry */
7452 		rsm = NULL;
7453 	}
7454 	if ((rsm) && (rsm->r_start == seq_out)) {
7455 		seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag);
7456 		if (len == 0) {
7457 			return;
7458 		} else {
7459 			goto more;
7460 		}
7461 	}
7462 	/* Ok it was not the last pointer go through it the hard way. */
7463 refind:
7464 	fe.r_start = seq_out;
7465 	rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
7466 	if (rsm) {
7467 		if (rsm->r_start == seq_out) {
7468 			seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag);
7469 			if (len == 0) {
7470 				return;
7471 			} else {
7472 				goto refind;
7473 			}
7474 		}
7475 		if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) {
7476 			/* Transmitted within this piece */
7477 			/*
7478 			 * Ok we must split off the front and then let the
7479 			 * update do the rest
7480 			 */
7481 			nrsm = rack_alloc_full_limit(rack);
7482 			if (nrsm == NULL) {
7483 				rack_update_rsm(tp, rack, rsm, cts, add_flag);
7484 				return;
7485 			}
7486 			/*
7487 			 * copy rsm to nrsm and then trim the front of rsm
7488 			 * to not include this part.
7489 			 */
7490 			rack_clone_rsm(rack, nrsm, rsm, seq_out);
7491 			rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
7492 #ifndef INVARIANTS
7493 			(void)RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
7494 #else
7495 			insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
7496 			if (insret != NULL) {
7497 				panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
7498 				      nrsm, insret, rack, rsm);
7499 			}
7500 #endif
7501 			if (rsm->r_in_tmap) {
7502 				TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7503 				nrsm->r_in_tmap = 1;
7504 			}
7505 			rsm->r_flags &= (~RACK_HAS_FIN);
7506 			seq_out = rack_update_entry(tp, rack, nrsm, cts, &len, add_flag);
7507 			if (len == 0) {
7508 				return;
7509 			} else if (len > 0)
7510 				goto refind;
7511 		}
7512 	}
7513 	/*
7514 	 * Hmm not found in map did they retransmit both old and on into the
7515 	 * new?
7516 	 */
7517 	if (seq_out == tp->snd_max) {
7518 		goto again;
7519 	} else if (SEQ_LT(seq_out, tp->snd_max)) {
7520 #ifdef INVARIANTS
7521 		printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n",
7522 		       seq_out, len, tp->snd_una, tp->snd_max);
7523 		printf("Starting Dump of all rack entries\n");
7524 		RB_FOREACH(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
7525 			printf("rsm:%p start:%u end:%u\n",
7526 			       rsm, rsm->r_start, rsm->r_end);
7527 		}
7528 		printf("Dump complete\n");
7529 		panic("seq_out not found rack:%p tp:%p",
7530 		      rack, tp);
7531 #endif
7532 	} else {
7533 #ifdef INVARIANTS
7534 		/*
7535 		 * Hmm beyond sndmax? (only if we are using the new rtt-pack
7536 		 * flag)
7537 		 */
7538 		panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p",
7539 		      seq_out, len, tp->snd_max, tp);
7540 #endif
7541 	}
7542 }
7543 
7544 /*
7545  * Record one of the RTT updates from an ack into
7546  * our sample structure.
7547  */
7548 
7549 static void
7550 tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt, uint32_t len, uint32_t us_rtt,
7551 		    int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt)
7552 {
7553 	if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
7554 	    (rack->r_ctl.rack_rs.rs_rtt_lowest > rtt)) {
7555 		rack->r_ctl.rack_rs.rs_rtt_lowest = rtt;
7556 	}
7557 	if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
7558 	    (rack->r_ctl.rack_rs.rs_rtt_highest < rtt)) {
7559 		rack->r_ctl.rack_rs.rs_rtt_highest = rtt;
7560 	}
7561 	if (rack->rc_tp->t_flags & TF_GPUTINPROG) {
7562 	    if (us_rtt < rack->r_ctl.rc_gp_lowrtt)
7563 		rack->r_ctl.rc_gp_lowrtt = us_rtt;
7564 	    if (rack->rc_tp->snd_wnd > rack->r_ctl.rc_gp_high_rwnd)
7565 		    rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
7566 	}
7567 	if ((confidence == 1) &&
7568 	    ((rsm == NULL) ||
7569 	     (rsm->r_just_ret) ||
7570 	     (rsm->r_one_out_nr &&
7571 	      len < (ctf_fixed_maxseg(rack->rc_tp) * 2)))) {
7572 		/*
7573 		 * If the rsm had a just return
7574 		 * hit it then we can't trust the
7575 		 * rtt measurement for buffer deterimination
7576 		 * Note that a confidence of 2, indicates
7577 		 * SACK'd which overrides the r_just_ret or
7578 		 * the r_one_out_nr. If it was a CUM-ACK and
7579 		 * we had only two outstanding, but get an
7580 		 * ack for only 1. Then that also lowers our
7581 		 * confidence.
7582 		 */
7583 		confidence = 0;
7584 	}
7585 	if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
7586 	    (rack->r_ctl.rack_rs.rs_us_rtt > us_rtt)) {
7587 		if (rack->r_ctl.rack_rs.confidence == 0) {
7588 			/*
7589 			 * We take anything with no current confidence
7590 			 * saved.
7591 			 */
7592 			rack->r_ctl.rack_rs.rs_us_rtt = us_rtt;
7593 			rack->r_ctl.rack_rs.confidence = confidence;
7594 			rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt;
7595 		} else if (confidence || rack->r_ctl.rack_rs.confidence) {
7596 			/*
7597 			 * Once we have a confident number,
7598 			 * we can update it with a smaller
7599 			 * value since this confident number
7600 			 * may include the DSACK time until
7601 			 * the next segment (the second one) arrived.
7602 			 */
7603 			rack->r_ctl.rack_rs.rs_us_rtt = us_rtt;
7604 			rack->r_ctl.rack_rs.confidence = confidence;
7605 			rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt;
7606 		}
7607 	}
7608 	rack_log_rtt_upd(rack->rc_tp, rack, us_rtt, len, rsm, confidence);
7609 	rack->r_ctl.rack_rs.rs_flags = RACK_RTT_VALID;
7610 	rack->r_ctl.rack_rs.rs_rtt_tot += rtt;
7611 	rack->r_ctl.rack_rs.rs_rtt_cnt++;
7612 }
7613 
7614 /*
7615  * Collect new round-trip time estimate
7616  * and update averages and current timeout.
7617  */
7618 static void
7619 tcp_rack_xmit_timer_commit(struct tcp_rack *rack, struct tcpcb *tp)
7620 {
7621 	int32_t delta;
7622 	int32_t rtt;
7623 
7624 	if (rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY)
7625 		/* No valid sample */
7626 		return;
7627 	if (rack->r_ctl.rc_rate_sample_method == USE_RTT_LOW) {
7628 		/* We are to use the lowest RTT seen in a single ack */
7629 		rtt = rack->r_ctl.rack_rs.rs_rtt_lowest;
7630 	} else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_HIGH) {
7631 		/* We are to use the highest RTT seen in a single ack */
7632 		rtt = rack->r_ctl.rack_rs.rs_rtt_highest;
7633 	} else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_AVG) {
7634 		/* We are to use the average RTT seen in a single ack */
7635 		rtt = (int32_t)(rack->r_ctl.rack_rs.rs_rtt_tot /
7636 				(uint64_t)rack->r_ctl.rack_rs.rs_rtt_cnt);
7637 	} else {
7638 #ifdef INVARIANTS
7639 		panic("Unknown rtt variant %d", rack->r_ctl.rc_rate_sample_method);
7640 #endif
7641 		return;
7642 	}
7643 	if (rtt == 0)
7644 		rtt = 1;
7645 	if (rack->rc_gp_rtt_set == 0) {
7646 		/*
7647 		 * With no RTT we have to accept
7648 		 * even one we are not confident of.
7649 		 */
7650 		rack->r_ctl.rc_gp_srtt = rack->r_ctl.rack_rs.rs_us_rtt;
7651 		rack->rc_gp_rtt_set = 1;
7652 	} else if (rack->r_ctl.rack_rs.confidence) {
7653 		/* update the running gp srtt */
7654 		rack->r_ctl.rc_gp_srtt -= (rack->r_ctl.rc_gp_srtt/8);
7655 		rack->r_ctl.rc_gp_srtt += rack->r_ctl.rack_rs.rs_us_rtt / 8;
7656 	}
7657 	if (rack->r_ctl.rack_rs.confidence) {
7658 		/*
7659 		 * record the low and high for highly buffered path computation,
7660 		 * we only do this if we are confident (not a retransmission).
7661 		 */
7662 		if (rack->r_ctl.rc_highest_us_rtt < rack->r_ctl.rack_rs.rs_us_rtt) {
7663 			rack->r_ctl.rc_highest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
7664 		}
7665 		if (rack->rc_highly_buffered == 0) {
7666 			/*
7667 			 * Currently once we declare a path has
7668 			 * highly buffered there is no going
7669 			 * back, which may be a problem...
7670 			 */
7671 			if ((rack->r_ctl.rc_highest_us_rtt / rack->r_ctl.rc_lowest_us_rtt) > rack_hbp_thresh) {
7672 				rack_log_rtt_shrinks(rack, rack->r_ctl.rack_rs.rs_us_rtt,
7673 						     rack->r_ctl.rc_highest_us_rtt,
7674 						     rack->r_ctl.rc_lowest_us_rtt,
7675 						     RACK_RTTS_SEEHBP);
7676 				rack->rc_highly_buffered = 1;
7677 			}
7678 		}
7679 	}
7680 	if ((rack->r_ctl.rack_rs.confidence) ||
7681 	    (rack->r_ctl.rack_rs.rs_us_rtrcnt == 1)) {
7682 		/*
7683 		 * If we are highly confident of it <or> it was
7684 		 * never retransmitted we accept it as the last us_rtt.
7685 		 */
7686 		rack->r_ctl.rc_last_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
7687 		/* The lowest rtt can be set if its was not retransmited */
7688 		if (rack->r_ctl.rc_lowest_us_rtt > rack->r_ctl.rack_rs.rs_us_rtt) {
7689 			rack->r_ctl.rc_lowest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
7690 			if (rack->r_ctl.rc_lowest_us_rtt == 0)
7691 				rack->r_ctl.rc_lowest_us_rtt = 1;
7692 		}
7693 	}
7694 	rack = (struct tcp_rack *)tp->t_fb_ptr;
7695 	if (tp->t_srtt != 0) {
7696 		/*
7697 		 * We keep a simple srtt in microseconds, like our rtt
7698 		 * measurement. We don't need to do any tricks with shifting
7699 		 * etc. Instead we just add in 1/8th of the new measurement
7700 		 * and subtract out 1/8 of the old srtt. We do the same with
7701 		 * the variance after finding the absolute value of the
7702 		 * difference between this sample and the current srtt.
7703 		 */
7704 		delta = tp->t_srtt - rtt;
7705 		/* Take off 1/8th of the current sRTT */
7706 		tp->t_srtt -= (tp->t_srtt >> 3);
7707 		/* Add in 1/8th of the new RTT just measured */
7708 		tp->t_srtt += (rtt >> 3);
7709 		if (tp->t_srtt <= 0)
7710 			tp->t_srtt = 1;
7711 		/* Now lets make the absolute value of the variance */
7712 		if (delta < 0)
7713 			delta = -delta;
7714 		/* Subtract out 1/8th */
7715 		tp->t_rttvar -= (tp->t_rttvar >> 3);
7716 		/* Add in 1/8th of the new variance we just saw */
7717 		tp->t_rttvar += (delta >> 3);
7718 		if (tp->t_rttvar <= 0)
7719 			tp->t_rttvar = 1;
7720 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
7721 			tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
7722 	} else {
7723 		/*
7724 		 * No rtt measurement yet - use the unsmoothed rtt. Set the
7725 		 * variance to half the rtt (so our first retransmit happens
7726 		 * at 3*rtt).
7727 		 */
7728 		tp->t_srtt = rtt;
7729 		tp->t_rttvar = rtt >> 1;
7730 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
7731 	}
7732 	rack->rc_srtt_measure_made = 1;
7733 	KMOD_TCPSTAT_INC(tcps_rttupdated);
7734 	tp->t_rttupdated++;
7735 #ifdef STATS
7736 	if (rack_stats_gets_ms_rtt == 0) {
7737 		/* Send in the microsecond rtt used for rxt timeout purposes */
7738 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt));
7739 	} else if (rack_stats_gets_ms_rtt == 1) {
7740 		/* Send in the millisecond rtt used for rxt timeout purposes */
7741 		int32_t ms_rtt;
7742 
7743 		/* Round up */
7744 		ms_rtt = (rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC;
7745 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt));
7746 	} else if (rack_stats_gets_ms_rtt == 2) {
7747 		/* Send in the millisecond rtt has close to the path RTT as we can get  */
7748 		int32_t ms_rtt;
7749 
7750 		/* Round up */
7751 		ms_rtt = (rack->r_ctl.rack_rs.rs_us_rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC;
7752 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt));
7753 	}  else {
7754 		/* Send in the microsecond rtt has close to the path RTT as we can get  */
7755 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rack->r_ctl.rack_rs.rs_us_rtt));
7756 	}
7757 
7758 #endif
7759 	/*
7760 	 * the retransmit should happen at rtt + 4 * rttvar. Because of the
7761 	 * way we do the smoothing, srtt and rttvar will each average +1/2
7762 	 * tick of bias.  When we compute the retransmit timer, we want 1/2
7763 	 * tick of rounding and 1 extra tick because of +-1/2 tick
7764 	 * uncertainty in the firing of the timer.  The bias will give us
7765 	 * exactly the 1.5 tick we need.  But, because the bias is
7766 	 * statistical, we have to test that we don't drop below the minimum
7767 	 * feasible timer (which is 2 ticks).
7768 	 */
7769 	tp->t_rxtshift = 0;
7770 	RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
7771 		      max(rack_rto_min, rtt + 2), rack_rto_max, rack->r_ctl.timer_slop);
7772 	rack_log_rtt_sample(rack, rtt);
7773 	tp->t_softerror = 0;
7774 }
7775 
7776 
7777 static void
7778 rack_apply_updated_usrtt(struct tcp_rack *rack, uint32_t us_rtt, uint32_t us_cts)
7779 {
7780 	/*
7781 	 * Apply to filter the inbound us-rtt at us_cts.
7782 	 */
7783 	uint32_t old_rtt;
7784 
7785 	old_rtt = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
7786 	apply_filter_min_small(&rack->r_ctl.rc_gp_min_rtt,
7787 			       us_rtt, us_cts);
7788 	if (old_rtt > us_rtt) {
7789 		/* We just hit a new lower rtt time */
7790 		rack_log_rtt_shrinks(rack,  us_cts,  old_rtt,
7791 				     __LINE__, RACK_RTTS_NEWRTT);
7792 		/*
7793 		 * Only count it if its lower than what we saw within our
7794 		 * calculated range.
7795 		 */
7796 		if ((old_rtt - us_rtt) > rack_min_rtt_movement) {
7797 			if (rack_probertt_lower_within &&
7798 			    rack->rc_gp_dyn_mul &&
7799 			    (rack->use_fixed_rate == 0) &&
7800 			    (rack->rc_always_pace)) {
7801 				/*
7802 				 * We are seeing a new lower rtt very close
7803 				 * to the time that we would have entered probe-rtt.
7804 				 * This is probably due to the fact that a peer flow
7805 				 * has entered probe-rtt. Lets go in now too.
7806 				 */
7807 				uint32_t val;
7808 
7809 				val = rack_probertt_lower_within * rack_time_between_probertt;
7810 				val /= 100;
7811 				if ((rack->in_probe_rtt == 0)  &&
7812 				    ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= (rack_time_between_probertt - val)))	{
7813 					rack_enter_probertt(rack, us_cts);
7814 				}
7815 			}
7816 			rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
7817 		}
7818 	}
7819 }
7820 
7821 static int
7822 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack,
7823     struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack)
7824 {
7825 	uint32_t us_rtt;
7826 	int32_t i, all;
7827 	uint32_t t, len_acked;
7828 
7829 	if ((rsm->r_flags & RACK_ACKED) ||
7830 	    (rsm->r_flags & RACK_WAS_ACKED))
7831 		/* Already done */
7832 		return (0);
7833 	if (rsm->r_no_rtt_allowed) {
7834 		/* Not allowed */
7835 		return (0);
7836 	}
7837 	if (ack_type == CUM_ACKED) {
7838 		if (SEQ_GT(th_ack, rsm->r_end)) {
7839 			len_acked = rsm->r_end - rsm->r_start;
7840 			all = 1;
7841 		} else {
7842 			len_acked = th_ack - rsm->r_start;
7843 			all = 0;
7844 		}
7845 	} else {
7846 		len_acked = rsm->r_end - rsm->r_start;
7847 		all = 0;
7848 	}
7849 	if (rsm->r_rtr_cnt == 1) {
7850 
7851 		t = cts - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
7852 		if ((int)t <= 0)
7853 			t = 1;
7854 		if (!tp->t_rttlow || tp->t_rttlow > t)
7855 			tp->t_rttlow = t;
7856 		if (!rack->r_ctl.rc_rack_min_rtt ||
7857 		    SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
7858 			rack->r_ctl.rc_rack_min_rtt = t;
7859 			if (rack->r_ctl.rc_rack_min_rtt == 0) {
7860 				rack->r_ctl.rc_rack_min_rtt = 1;
7861 			}
7862 		}
7863 		if (TSTMP_GT(tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time), rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]))
7864 			us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
7865 		else
7866 			us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
7867 		if (us_rtt == 0)
7868 			us_rtt = 1;
7869 		if (CC_ALGO(tp)->rttsample != NULL) {
7870 			/* Kick the RTT to the CC */
7871 			CC_ALGO(tp)->rttsample(tp->ccv, us_rtt, 1, rsm->r_fas);
7872 		}
7873 		rack_apply_updated_usrtt(rack, us_rtt, tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time));
7874 		if (ack_type == SACKED) {
7875 			rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 1);
7876 			tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt, 2 , rsm, rsm->r_rtr_cnt);
7877 		} else {
7878 			/*
7879 			 * We need to setup what our confidence
7880 			 * is in this ack.
7881 			 *
7882 			 * If the rsm was app limited and it is
7883 			 * less than a mss in length (the end
7884 			 * of the send) then we have a gap. If we
7885 			 * were app limited but say we were sending
7886 			 * multiple MSS's then we are more confident
7887 			 * int it.
7888 			 *
7889 			 * When we are not app-limited then we see if
7890 			 * the rsm is being included in the current
7891 			 * measurement, we tell this by the app_limited_needs_set
7892 			 * flag.
7893 			 *
7894 			 * Note that being cwnd blocked is not applimited
7895 			 * as well as the pacing delay between packets which
7896 			 * are sending only 1 or 2 MSS's also will show up
7897 			 * in the RTT. We probably need to examine this algorithm
7898 			 * a bit more and enhance it to account for the delay
7899 			 * between rsm's. We could do that by saving off the
7900 			 * pacing delay of each rsm (in an rsm) and then
7901 			 * factoring that in somehow though for now I am
7902 			 * not sure how :)
7903 			 */
7904 			int calc_conf = 0;
7905 
7906 			if (rsm->r_flags & RACK_APP_LIMITED) {
7907 				if (all && (len_acked <= ctf_fixed_maxseg(tp)))
7908 					calc_conf = 0;
7909 				else
7910 					calc_conf = 1;
7911 			} else if (rack->app_limited_needs_set == 0) {
7912 				calc_conf = 1;
7913 			} else {
7914 				calc_conf = 0;
7915 			}
7916 			rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 2);
7917 			tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt,
7918 					    calc_conf, rsm, rsm->r_rtr_cnt);
7919 		}
7920 		if ((rsm->r_flags & RACK_TLP) &&
7921 		    (!IN_FASTRECOVERY(tp->t_flags))) {
7922 			/* Segment was a TLP and our retrans matched */
7923 			if (rack->r_ctl.rc_tlp_cwnd_reduce) {
7924 				rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__);
7925 			}
7926 		}
7927 		if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)])) {
7928 			/* New more recent rack_tmit_time */
7929 			rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
7930 			rack->rc_rack_rtt = t;
7931 		}
7932 		return (1);
7933 	}
7934 	/*
7935 	 * We clear the soft/rxtshift since we got an ack.
7936 	 * There is no assurance we will call the commit() function
7937 	 * so we need to clear these to avoid incorrect handling.
7938 	 */
7939 	tp->t_rxtshift = 0;
7940 	RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
7941 		      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
7942 	tp->t_softerror = 0;
7943 	if (to && (to->to_flags & TOF_TS) &&
7944 	    (ack_type == CUM_ACKED) &&
7945 	    (to->to_tsecr) &&
7946 	    ((rsm->r_flags & RACK_OVERMAX) == 0)) {
7947 		/*
7948 		 * Now which timestamp does it match? In this block the ACK
7949 		 * must be coming from a previous transmission.
7950 		 */
7951 		for (i = 0; i < rsm->r_rtr_cnt; i++) {
7952 			if (rack_ts_to_msec(rsm->r_tim_lastsent[i]) == to->to_tsecr) {
7953 				t = cts - (uint32_t)rsm->r_tim_lastsent[i];
7954 				if ((int)t <= 0)
7955 					t = 1;
7956 				if (CC_ALGO(tp)->rttsample != NULL) {
7957 					/*
7958 					 * Kick the RTT to the CC, here
7959 					 * we lie a bit in that we know the
7960 					 * retransmission is correct even though
7961 					 * we retransmitted. This is because
7962 					 * we match the timestamps.
7963 					 */
7964 					if (TSTMP_GT(tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time), rsm->r_tim_lastsent[i]))
7965 						us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - (uint32_t)rsm->r_tim_lastsent[i];
7966 					else
7967 						us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[i];
7968 					CC_ALGO(tp)->rttsample(tp->ccv, us_rtt, 1, rsm->r_fas);
7969 				}
7970 				if ((i + 1) < rsm->r_rtr_cnt) {
7971 					/*
7972 					 * The peer ack'd from our previous
7973 					 * transmission. We have a spurious
7974 					 * retransmission and thus we dont
7975 					 * want to update our rack_rtt.
7976 					 *
7977 					 * Hmm should there be a CC revert here?
7978 					 *
7979 					 */
7980 					return (0);
7981 				}
7982 				if (!tp->t_rttlow || tp->t_rttlow > t)
7983 					tp->t_rttlow = t;
7984 				if (!rack->r_ctl.rc_rack_min_rtt || SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
7985 					rack->r_ctl.rc_rack_min_rtt = t;
7986 					if (rack->r_ctl.rc_rack_min_rtt == 0) {
7987 						rack->r_ctl.rc_rack_min_rtt = 1;
7988 					}
7989 				}
7990 				if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
7991 					   (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)])) {
7992 					/* New more recent rack_tmit_time */
7993 					rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
7994 					rack->rc_rack_rtt = t;
7995 				}
7996 				rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[i], cts, 3);
7997 				tcp_rack_xmit_timer(rack, t + 1, len_acked, t, 0, rsm,
7998 						    rsm->r_rtr_cnt);
7999 				return (1);
8000 			}
8001 		}
8002 		goto ts_not_found;
8003 	} else {
8004 		/*
8005 		 * Ok its a SACK block that we retransmitted. or a windows
8006 		 * machine without timestamps. We can tell nothing from the
8007 		 * time-stamp since its not there or the time the peer last
8008 		 * recieved a segment that moved forward its cum-ack point.
8009 		 */
8010 ts_not_found:
8011 		i = rsm->r_rtr_cnt - 1;
8012 		t = cts - (uint32_t)rsm->r_tim_lastsent[i];
8013 		if ((int)t <= 0)
8014 			t = 1;
8015 		if (rack->r_ctl.rc_rack_min_rtt && SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
8016 			/*
8017 			 * We retransmitted and the ack came back in less
8018 			 * than the smallest rtt we have observed. We most
8019 			 * likely did an improper retransmit as outlined in
8020 			 * 6.2 Step 2 point 2 in the rack-draft so we
8021 			 * don't want to update our rack_rtt. We in
8022 			 * theory (in future) might want to think about reverting our
8023 			 * cwnd state but we won't for now.
8024 			 */
8025 			return (0);
8026 		} else if (rack->r_ctl.rc_rack_min_rtt) {
8027 			/*
8028 			 * We retransmitted it and the retransmit did the
8029 			 * job.
8030 			 */
8031 			if (!rack->r_ctl.rc_rack_min_rtt ||
8032 			    SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
8033 				rack->r_ctl.rc_rack_min_rtt = t;
8034 				if (rack->r_ctl.rc_rack_min_rtt == 0) {
8035 					rack->r_ctl.rc_rack_min_rtt = 1;
8036 				}
8037 			}
8038 			if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time, (uint32_t)rsm->r_tim_lastsent[i])) {
8039 				/* New more recent rack_tmit_time */
8040 				rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[i];
8041 				rack->rc_rack_rtt = t;
8042 			}
8043 			return (1);
8044 		}
8045 	}
8046 	return (0);
8047 }
8048 
8049 /*
8050  * Mark the SACK_PASSED flag on all entries prior to rsm send wise.
8051  */
8052 static void
8053 rack_log_sack_passed(struct tcpcb *tp,
8054     struct tcp_rack *rack, struct rack_sendmap *rsm)
8055 {
8056 	struct rack_sendmap *nrsm;
8057 
8058 	nrsm = rsm;
8059 	TAILQ_FOREACH_REVERSE_FROM(nrsm, &rack->r_ctl.rc_tmap,
8060 	    rack_head, r_tnext) {
8061 		if (nrsm == rsm) {
8062 			/* Skip orginal segment he is acked */
8063 			continue;
8064 		}
8065 		if (nrsm->r_flags & RACK_ACKED) {
8066 			/*
8067 			 * Skip ack'd segments, though we
8068 			 * should not see these, since tmap
8069 			 * should not have ack'd segments.
8070 			 */
8071 			continue;
8072 		}
8073 		if (nrsm->r_flags & RACK_RWND_COLLAPSED) {
8074 			/*
8075 			 * If the peer dropped the rwnd on
8076 			 * these then we don't worry about them.
8077 			 */
8078 			continue;
8079 		}
8080 		if (nrsm->r_flags & RACK_SACK_PASSED) {
8081 			/*
8082 			 * We found one that is already marked
8083 			 * passed, we have been here before and
8084 			 * so all others below this are marked.
8085 			 */
8086 			break;
8087 		}
8088 		nrsm->r_flags |= RACK_SACK_PASSED;
8089 		nrsm->r_flags &= ~RACK_WAS_SACKPASS;
8090 	}
8091 }
8092 
8093 static void
8094 rack_need_set_test(struct tcpcb *tp,
8095 		   struct tcp_rack *rack,
8096 		   struct rack_sendmap *rsm,
8097 		   tcp_seq th_ack,
8098 		   int line,
8099 		   int use_which)
8100 {
8101 
8102 	if ((tp->t_flags & TF_GPUTINPROG) &&
8103 	    SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
8104 		/*
8105 		 * We were app limited, and this ack
8106 		 * butts up or goes beyond the point where we want
8107 		 * to start our next measurement. We need
8108 		 * to record the new gput_ts as here and
8109 		 * possibly update the start sequence.
8110 		 */
8111 		uint32_t seq, ts;
8112 
8113 		if (rsm->r_rtr_cnt > 1) {
8114 			/*
8115 			 * This is a retransmit, can we
8116 			 * really make any assessment at this
8117 			 * point?  We are not really sure of
8118 			 * the timestamp, is it this or the
8119 			 * previous transmission?
8120 			 *
8121 			 * Lets wait for something better that
8122 			 * is not retransmitted.
8123 			 */
8124 			return;
8125 		}
8126 		seq = tp->gput_seq;
8127 		ts = tp->gput_ts;
8128 		rack->app_limited_needs_set = 0;
8129 		tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
8130 		/* Do we start at a new end? */
8131 		if ((use_which == RACK_USE_BEG) &&
8132 		    SEQ_GEQ(rsm->r_start, tp->gput_seq)) {
8133 			/*
8134 			 * When we get an ACK that just eats
8135 			 * up some of the rsm, we set RACK_USE_BEG
8136 			 * since whats at r_start (i.e. th_ack)
8137 			 * is left unacked and thats where the
8138 			 * measurement not starts.
8139 			 */
8140 			tp->gput_seq = rsm->r_start;
8141 			rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8142 		}
8143 		if ((use_which == RACK_USE_END) &&
8144 		    SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
8145 			    /*
8146 			     * We use the end when the cumack
8147 			     * is moving forward and completely
8148 			     * deleting the rsm passed so basically
8149 			     * r_end holds th_ack.
8150 			     *
8151 			     * For SACK's we also want to use the end
8152 			     * since this piece just got sacked and
8153 			     * we want to target anything after that
8154 			     * in our measurement.
8155 			     */
8156 			    tp->gput_seq = rsm->r_end;
8157 			    rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8158 		}
8159 		if (use_which == RACK_USE_END_OR_THACK) {
8160 			/*
8161 			 * special case for ack moving forward,
8162 			 * not a sack, we need to move all the
8163 			 * way up to where this ack cum-ack moves
8164 			 * to.
8165 			 */
8166 			if (SEQ_GT(th_ack, rsm->r_end))
8167 				tp->gput_seq = th_ack;
8168 			else
8169 				tp->gput_seq = rsm->r_end;
8170 			rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8171 		}
8172 		if (SEQ_GT(tp->gput_seq, tp->gput_ack)) {
8173 			/*
8174 			 * We moved beyond this guy's range, re-calculate
8175 			 * the new end point.
8176 			 */
8177 			if (rack->rc_gp_filled == 0) {
8178 				tp->gput_ack = tp->gput_seq + max(rc_init_window(rack), (MIN_GP_WIN * ctf_fixed_maxseg(tp)));
8179 			} else {
8180 				tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
8181 			}
8182 		}
8183 		/*
8184 		 * We are moving the goal post, we may be able to clear the
8185 		 * measure_saw_probe_rtt flag.
8186 		 */
8187 		if ((rack->in_probe_rtt == 0) &&
8188 		    (rack->measure_saw_probe_rtt) &&
8189 		    (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
8190 			rack->measure_saw_probe_rtt = 0;
8191 		rack_log_pacing_delay_calc(rack, ts, tp->gput_ts,
8192 					   seq, tp->gput_seq, 0, 5, line, NULL, 0);
8193 		if (rack->rc_gp_filled &&
8194 		    ((tp->gput_ack - tp->gput_seq) <
8195 		     max(rc_init_window(rack), (MIN_GP_WIN *
8196 						ctf_fixed_maxseg(tp))))) {
8197 			uint32_t ideal_amount;
8198 
8199 			ideal_amount = rack_get_measure_window(tp, rack);
8200 			if (ideal_amount > sbavail(&tp->t_inpcb->inp_socket->so_snd)) {
8201 				/*
8202 				 * There is no sense of continuing this measurement
8203 				 * because its too small to gain us anything we
8204 				 * trust. Skip it and that way we can start a new
8205 				 * measurement quicker.
8206 				 */
8207 				tp->t_flags &= ~TF_GPUTINPROG;
8208 				rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq,
8209 							   0, 0, 0, 6, __LINE__, NULL, 0);
8210 			} else {
8211 				/*
8212 				 * Reset the window further out.
8213 				 */
8214 				tp->gput_ack = tp->gput_seq + ideal_amount;
8215 			}
8216 		}
8217 	}
8218 }
8219 
8220 static inline int
8221 is_rsm_inside_declared_tlp_block(struct tcp_rack *rack, struct rack_sendmap *rsm)
8222 {
8223 	if (SEQ_LT(rsm->r_end, rack->r_ctl.last_tlp_acked_start)) {
8224 		/* Behind our TLP definition or right at */
8225 		return (0);
8226 	}
8227 	if (SEQ_GT(rsm->r_start, rack->r_ctl.last_tlp_acked_end)) {
8228 		/* The start is beyond or right at our end of TLP definition */
8229 		return (0);
8230 	}
8231 	/* It has to be a sub-part of the original TLP recorded */
8232 	return (1);
8233 }
8234 
8235 
8236 static uint32_t
8237 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack, struct sackblk *sack,
8238 		   struct tcpopt *to, struct rack_sendmap **prsm, uint32_t cts, int *moved_two)
8239 {
8240 	uint32_t start, end, changed = 0;
8241 	struct rack_sendmap stack_map;
8242 	struct rack_sendmap *rsm, *nrsm, fe, *prev, *next;
8243 #ifdef INVARIANTS
8244 	struct rack_sendmap *insret;
8245 #endif
8246 	int32_t used_ref = 1;
8247 	int moved = 0;
8248 
8249 	start = sack->start;
8250 	end = sack->end;
8251 	rsm = *prsm;
8252 	memset(&fe, 0, sizeof(fe));
8253 do_rest_ofb:
8254 	if ((rsm == NULL) ||
8255 	    (SEQ_LT(end, rsm->r_start)) ||
8256 	    (SEQ_GEQ(start, rsm->r_end)) ||
8257 	    (SEQ_LT(start, rsm->r_start))) {
8258 		/*
8259 		 * We are not in the right spot,
8260 		 * find the correct spot in the tree.
8261 		 */
8262 		used_ref = 0;
8263 		fe.r_start = start;
8264 		rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
8265 		moved++;
8266 	}
8267 	if (rsm == NULL) {
8268 		/* TSNH */
8269 		goto out;
8270 	}
8271 	/* Ok we have an ACK for some piece of this rsm */
8272 	if (rsm->r_start != start) {
8273 		if ((rsm->r_flags & RACK_ACKED) == 0) {
8274 			/*
8275 			 * Before any splitting or hookery is
8276 			 * done is it a TLP of interest i.e. rxt?
8277 			 */
8278 			if ((rsm->r_flags & RACK_TLP) &&
8279 			    (rsm->r_rtr_cnt > 1)) {
8280 				/*
8281 				 * We are splitting a rxt TLP, check
8282 				 * if we need to save off the start/end
8283 				 */
8284 				if (rack->rc_last_tlp_acked_set &&
8285 				    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
8286 					/*
8287 					 * We already turned this on since we are inside
8288 					 * the previous one was a partially sack now we
8289 					 * are getting another one (maybe all of it).
8290 					 *
8291 					 */
8292 					rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
8293 					/*
8294 					 * Lets make sure we have all of it though.
8295 					 */
8296 					if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
8297 						rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8298 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8299 								     rack->r_ctl.last_tlp_acked_end);
8300 					}
8301 					if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
8302 						rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8303 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8304 								     rack->r_ctl.last_tlp_acked_end);
8305 					}
8306 				} else {
8307 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8308 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8309 					rack->rc_last_tlp_past_cumack = 0;
8310 					rack->rc_last_tlp_acked_set = 1;
8311 					rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
8312 				}
8313 			}
8314 			/**
8315 			 * Need to split this in two pieces the before and after,
8316 			 * the before remains in the map, the after must be
8317 			 * added. In other words we have:
8318 			 * rsm        |--------------|
8319 			 * sackblk        |------->
8320 			 * rsm will become
8321 			 *     rsm    |---|
8322 			 * and nrsm will be  the sacked piece
8323 			 *     nrsm       |----------|
8324 			 *
8325 			 * But before we start down that path lets
8326 			 * see if the sack spans over on top of
8327 			 * the next guy and it is already sacked.
8328 			 *
8329 			 */
8330 			next = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8331 			if (next && (next->r_flags & RACK_ACKED) &&
8332 			    SEQ_GEQ(end, next->r_start)) {
8333 				/**
8334 				 * So the next one is already acked, and
8335 				 * we can thus by hookery use our stack_map
8336 				 * to reflect the piece being sacked and
8337 				 * then adjust the two tree entries moving
8338 				 * the start and ends around. So we start like:
8339 				 *  rsm     |------------|             (not-acked)
8340 				 *  next                 |-----------| (acked)
8341 				 *  sackblk        |-------->
8342 				 *  We want to end like so:
8343 				 *  rsm     |------|                   (not-acked)
8344 				 *  next           |-----------------| (acked)
8345 				 *  nrsm           |-----|
8346 				 * Where nrsm is a temporary stack piece we
8347 				 * use to update all the gizmos.
8348 				 */
8349 				/* Copy up our fudge block */
8350 				nrsm = &stack_map;
8351 				memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
8352 				/* Now adjust our tree blocks */
8353 				rsm->r_end = start;
8354 				next->r_start = start;
8355 				/* Now we must adjust back where next->m is */
8356 				rack_setup_offset_for_rsm(rsm, next);
8357 
8358 				/* We don't need to adjust rsm, it did not change */
8359 				/* Clear out the dup ack count of the remainder */
8360 				rsm->r_dupack = 0;
8361 				rsm->r_just_ret = 0;
8362 				rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
8363 				/* Now lets make sure our fudge block is right */
8364 				nrsm->r_start = start;
8365 				/* Now lets update all the stats and such */
8366 				rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0);
8367 				if (rack->app_limited_needs_set)
8368 					rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END);
8369 				changed += (nrsm->r_end - nrsm->r_start);
8370 				rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start);
8371 				if (nrsm->r_flags & RACK_SACK_PASSED) {
8372 					rack->r_ctl.rc_reorder_ts = cts;
8373 				}
8374 				/*
8375 				 * Now we want to go up from rsm (the
8376 				 * one left un-acked) to the next one
8377 				 * in the tmap. We do this so when
8378 				 * we walk backwards we include marking
8379 				 * sack-passed on rsm (The one passed in
8380 				 * is skipped since it is generally called
8381 				 * on something sacked before removing it
8382 				 * from the tmap).
8383 				 */
8384 				if (rsm->r_in_tmap) {
8385 					nrsm = TAILQ_NEXT(rsm, r_tnext);
8386 					/*
8387 					 * Now that we have the next
8388 					 * one walk backwards from there.
8389 					 */
8390 					if (nrsm && nrsm->r_in_tmap)
8391 						rack_log_sack_passed(tp, rack, nrsm);
8392 				}
8393 				/* Now are we done? */
8394 				if (SEQ_LT(end, next->r_end) ||
8395 				    (end == next->r_end)) {
8396 					/* Done with block */
8397 					goto out;
8398 				}
8399 				rack_log_map_chg(tp, rack, &stack_map, rsm, next, MAP_SACK_M1, end, __LINE__);
8400 				counter_u64_add(rack_sack_used_next_merge, 1);
8401 				/* Postion for the next block */
8402 				start = next->r_end;
8403 				rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, next);
8404 				if (rsm == NULL)
8405 					goto out;
8406 			} else {
8407 				/**
8408 				 * We can't use any hookery here, so we
8409 				 * need to split the map. We enter like
8410 				 * so:
8411 				 *  rsm      |--------|
8412 				 *  sackblk       |----->
8413 				 * We will add the new block nrsm and
8414 				 * that will be the new portion, and then
8415 				 * fall through after reseting rsm. So we
8416 				 * split and look like this:
8417 				 *  rsm      |----|
8418 				 *  sackblk       |----->
8419 				 *  nrsm          |---|
8420 				 * We then fall through reseting
8421 				 * rsm to nrsm, so the next block
8422 				 * picks it up.
8423 				 */
8424 				nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
8425 				if (nrsm == NULL) {
8426 					/*
8427 					 * failed XXXrrs what can we do but loose the sack
8428 					 * info?
8429 					 */
8430 					goto out;
8431 				}
8432 				counter_u64_add(rack_sack_splits, 1);
8433 				rack_clone_rsm(rack, nrsm, rsm, start);
8434 				rsm->r_just_ret = 0;
8435 #ifndef INVARIANTS
8436 				(void)RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
8437 #else
8438 				insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
8439 				if (insret != NULL) {
8440 					panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
8441 					      nrsm, insret, rack, rsm);
8442 				}
8443 #endif
8444 				if (rsm->r_in_tmap) {
8445 					TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8446 					nrsm->r_in_tmap = 1;
8447 				}
8448 				rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M2, end, __LINE__);
8449 				rsm->r_flags &= (~RACK_HAS_FIN);
8450 				/* Position us to point to the new nrsm that starts the sack blk */
8451 				rsm = nrsm;
8452 			}
8453 		} else {
8454 			/* Already sacked this piece */
8455 			counter_u64_add(rack_sack_skipped_acked, 1);
8456 			moved++;
8457 			if (end == rsm->r_end) {
8458 				/* Done with block */
8459 				rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8460 				goto out;
8461 			} else if (SEQ_LT(end, rsm->r_end)) {
8462 				/* A partial sack to a already sacked block */
8463 				moved++;
8464 				rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8465 				goto out;
8466 			} else {
8467 				/*
8468 				 * The end goes beyond this guy
8469 				 * reposition the start to the
8470 				 * next block.
8471 				 */
8472 				start = rsm->r_end;
8473 				rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8474 				if (rsm == NULL)
8475 					goto out;
8476 			}
8477 		}
8478 	}
8479 	if (SEQ_GEQ(end, rsm->r_end)) {
8480 		/**
8481 		 * The end of this block is either beyond this guy or right
8482 		 * at this guy. I.e.:
8483 		 *  rsm ---                 |-----|
8484 		 *  end                     |-----|
8485 		 *  <or>
8486 		 *  end                     |---------|
8487 		 */
8488 		if ((rsm->r_flags & RACK_ACKED) == 0) {
8489 			/*
8490 			 * Is it a TLP of interest?
8491 			 */
8492 			if ((rsm->r_flags & RACK_TLP) &&
8493 			    (rsm->r_rtr_cnt > 1)) {
8494 				/*
8495 				 * We are splitting a rxt TLP, check
8496 				 * if we need to save off the start/end
8497 				 */
8498 				if (rack->rc_last_tlp_acked_set &&
8499 				    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
8500 					/*
8501 					 * We already turned this on since we are inside
8502 					 * the previous one was a partially sack now we
8503 					 * are getting another one (maybe all of it).
8504 					 */
8505 					rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
8506 					/*
8507 					 * Lets make sure we have all of it though.
8508 					 */
8509 					if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
8510 						rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8511 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8512 								     rack->r_ctl.last_tlp_acked_end);
8513 					}
8514 					if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
8515 						rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8516 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8517 								     rack->r_ctl.last_tlp_acked_end);
8518 					}
8519 				} else {
8520 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8521 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8522 					rack->rc_last_tlp_past_cumack = 0;
8523 					rack->rc_last_tlp_acked_set = 1;
8524 					rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
8525 				}
8526 			}
8527 			rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0);
8528 			changed += (rsm->r_end - rsm->r_start);
8529 			rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
8530 			if (rsm->r_in_tmap) /* should be true */
8531 				rack_log_sack_passed(tp, rack, rsm);
8532 			/* Is Reordering occuring? */
8533 			if (rsm->r_flags & RACK_SACK_PASSED) {
8534 				rsm->r_flags &= ~RACK_SACK_PASSED;
8535 				rack->r_ctl.rc_reorder_ts = cts;
8536 			}
8537 			if (rack->app_limited_needs_set)
8538 				rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END);
8539 			rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
8540 			rsm->r_flags |= RACK_ACKED;
8541 			if (rsm->r_in_tmap) {
8542 				TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8543 				rsm->r_in_tmap = 0;
8544 			}
8545 			rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_SACK_M3, end, __LINE__);
8546 		} else {
8547 			counter_u64_add(rack_sack_skipped_acked, 1);
8548 			moved++;
8549 		}
8550 		if (end == rsm->r_end) {
8551 			/* This block only - done, setup for next */
8552 			goto out;
8553 		}
8554 		/*
8555 		 * There is more not coverend by this rsm move on
8556 		 * to the next block in the RB tree.
8557 		 */
8558 		nrsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8559 		start = rsm->r_end;
8560 		rsm = nrsm;
8561 		if (rsm == NULL)
8562 			goto out;
8563 		goto do_rest_ofb;
8564 	}
8565 	/**
8566 	 * The end of this sack block is smaller than
8567 	 * our rsm i.e.:
8568 	 *  rsm ---                 |-----|
8569 	 *  end                     |--|
8570 	 */
8571 	if ((rsm->r_flags & RACK_ACKED) == 0) {
8572 		/*
8573 		 * Is it a TLP of interest?
8574 		 */
8575 		if ((rsm->r_flags & RACK_TLP) &&
8576 		    (rsm->r_rtr_cnt > 1)) {
8577 			/*
8578 			 * We are splitting a rxt TLP, check
8579 			 * if we need to save off the start/end
8580 			 */
8581 			if (rack->rc_last_tlp_acked_set &&
8582 			    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
8583 				/*
8584 				 * We already turned this on since we are inside
8585 				 * the previous one was a partially sack now we
8586 				 * are getting another one (maybe all of it).
8587 				 */
8588 				rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
8589 				/*
8590 				 * Lets make sure we have all of it though.
8591 				 */
8592 				if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
8593 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8594 					rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8595 							     rack->r_ctl.last_tlp_acked_end);
8596 				}
8597 				if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
8598 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8599 					rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8600 							     rack->r_ctl.last_tlp_acked_end);
8601 				}
8602 			} else {
8603 				rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8604 				rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8605 				rack->rc_last_tlp_past_cumack = 0;
8606 				rack->rc_last_tlp_acked_set = 1;
8607 				rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
8608 			}
8609 		}
8610 		prev = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8611 		if (prev &&
8612 		    (prev->r_flags & RACK_ACKED)) {
8613 			/**
8614 			 * Goal, we want the right remainder of rsm to shrink
8615 			 * in place and span from (rsm->r_start = end) to rsm->r_end.
8616 			 * We want to expand prev to go all the way
8617 			 * to prev->r_end <- end.
8618 			 * so in the tree we have before:
8619 			 *   prev     |--------|         (acked)
8620 			 *   rsm               |-------| (non-acked)
8621 			 *   sackblk           |-|
8622 			 * We churn it so we end up with
8623 			 *   prev     |----------|       (acked)
8624 			 *   rsm                 |-----| (non-acked)
8625 			 *   nrsm              |-| (temporary)
8626 			 *
8627 			 * Note if either prev/rsm is a TLP we don't
8628 			 * do this.
8629 			 */
8630 			nrsm = &stack_map;
8631 			memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
8632 			prev->r_end = end;
8633 			rsm->r_start = end;
8634 			/* Now adjust nrsm (stack copy) to be
8635 			 * the one that is the small
8636 			 * piece that was "sacked".
8637 			 */
8638 			nrsm->r_end = end;
8639 			rsm->r_dupack = 0;
8640 			rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
8641 			/*
8642 			 * Now that the rsm has had its start moved forward
8643 			 * lets go ahead and get its new place in the world.
8644 			 */
8645 			rack_setup_offset_for_rsm(prev, rsm);
8646 			/*
8647 			 * Now nrsm is our new little piece
8648 			 * that is acked (which was merged
8649 			 * to prev). Update the rtt and changed
8650 			 * based on that. Also check for reordering.
8651 			 */
8652 			rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0);
8653 			if (rack->app_limited_needs_set)
8654 				rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END);
8655 			changed += (nrsm->r_end - nrsm->r_start);
8656 			rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start);
8657 			if (nrsm->r_flags & RACK_SACK_PASSED) {
8658 				rack->r_ctl.rc_reorder_ts = cts;
8659 			}
8660 			rack_log_map_chg(tp, rack, prev, &stack_map, rsm, MAP_SACK_M4, end, __LINE__);
8661 			rsm = prev;
8662 			counter_u64_add(rack_sack_used_prev_merge, 1);
8663 		} else {
8664 			/**
8665 			 * This is the case where our previous
8666 			 * block is not acked either, so we must
8667 			 * split the block in two.
8668 			 */
8669 			nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
8670 			if (nrsm == NULL) {
8671 				/* failed rrs what can we do but loose the sack info? */
8672 				goto out;
8673 			}
8674 			if ((rsm->r_flags & RACK_TLP) &&
8675 			    (rsm->r_rtr_cnt > 1)) {
8676 				/*
8677 				 * We are splitting a rxt TLP, check
8678 				 * if we need to save off the start/end
8679 				 */
8680 				if (rack->rc_last_tlp_acked_set &&
8681 				    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
8682 					    /*
8683 					     * We already turned this on since this block is inside
8684 					     * the previous one was a partially sack now we
8685 					     * are getting another one (maybe all of it).
8686 					     */
8687 					    rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
8688 					    /*
8689 					     * Lets make sure we have all of it though.
8690 					     */
8691 					    if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
8692 						    rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8693 						    rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8694 									 rack->r_ctl.last_tlp_acked_end);
8695 					    }
8696 					    if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
8697 						    rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8698 						    rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8699 									 rack->r_ctl.last_tlp_acked_end);
8700 					    }
8701 				    } else {
8702 					    rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8703 					    rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8704 					    rack->rc_last_tlp_acked_set = 1;
8705 					    rack->rc_last_tlp_past_cumack = 0;
8706 					    rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
8707 				    }
8708 			}
8709 			/**
8710 			 * In this case nrsm becomes
8711 			 * nrsm->r_start = end;
8712 			 * nrsm->r_end = rsm->r_end;
8713 			 * which is un-acked.
8714 			 * <and>
8715 			 * rsm->r_end = nrsm->r_start;
8716 			 * i.e. the remaining un-acked
8717 			 * piece is left on the left
8718 			 * hand side.
8719 			 *
8720 			 * So we start like this
8721 			 * rsm      |----------| (not acked)
8722 			 * sackblk  |---|
8723 			 * build it so we have
8724 			 * rsm      |---|         (acked)
8725 			 * nrsm         |------|  (not acked)
8726 			 */
8727 			counter_u64_add(rack_sack_splits, 1);
8728 			rack_clone_rsm(rack, nrsm, rsm, end);
8729 			rsm->r_flags &= (~RACK_HAS_FIN);
8730 			rsm->r_just_ret = 0;
8731 #ifndef INVARIANTS
8732 			(void)RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
8733 #else
8734 			insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
8735 			if (insret != NULL) {
8736 				panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
8737 				      nrsm, insret, rack, rsm);
8738 			}
8739 #endif
8740 			if (rsm->r_in_tmap) {
8741 				TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8742 				nrsm->r_in_tmap = 1;
8743 			}
8744 			nrsm->r_dupack = 0;
8745 			rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2);
8746 			rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0);
8747 			changed += (rsm->r_end - rsm->r_start);
8748 			rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
8749 			if (rsm->r_in_tmap) /* should be true */
8750 				rack_log_sack_passed(tp, rack, rsm);
8751 			/* Is Reordering occuring? */
8752 			if (rsm->r_flags & RACK_SACK_PASSED) {
8753 				rsm->r_flags &= ~RACK_SACK_PASSED;
8754 				rack->r_ctl.rc_reorder_ts = cts;
8755 			}
8756 			if (rack->app_limited_needs_set)
8757 				rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END);
8758 			rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
8759 			rsm->r_flags |= RACK_ACKED;
8760 			rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M5, end, __LINE__);
8761 			if (rsm->r_in_tmap) {
8762 				TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8763 				rsm->r_in_tmap = 0;
8764 			}
8765 		}
8766 	} else if (start != end){
8767 		/*
8768 		 * The block was already acked.
8769 		 */
8770 		counter_u64_add(rack_sack_skipped_acked, 1);
8771 		moved++;
8772 	}
8773 out:
8774 	if (rsm &&
8775 	    ((rsm->r_flags & RACK_TLP) == 0) &&
8776 	    (rsm->r_flags & RACK_ACKED)) {
8777 		/*
8778 		 * Now can we merge where we worked
8779 		 * with either the previous or
8780 		 * next block?
8781 		 */
8782 		next = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8783 		while (next) {
8784 			if (next->r_flags & RACK_TLP)
8785 				break;
8786 			if (next->r_flags & RACK_ACKED) {
8787 			/* yep this and next can be merged */
8788 				rsm = rack_merge_rsm(rack, rsm, next);
8789 				next = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8790 			} else
8791 				break;
8792 		}
8793 		/* Now what about the previous? */
8794 		prev = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8795 		while (prev) {
8796 			if (prev->r_flags & RACK_TLP)
8797 				break;
8798 			if (prev->r_flags & RACK_ACKED) {
8799 				/* yep the previous and this can be merged */
8800 				rsm = rack_merge_rsm(rack, prev, rsm);
8801 				prev = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8802 			} else
8803 				break;
8804 		}
8805 	}
8806 	if (used_ref == 0) {
8807 		counter_u64_add(rack_sack_proc_all, 1);
8808 	} else {
8809 		counter_u64_add(rack_sack_proc_short, 1);
8810 	}
8811 	/* Save off the next one for quick reference. */
8812 	if (rsm)
8813 		nrsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8814 	else
8815 		nrsm = NULL;
8816 	*prsm = rack->r_ctl.rc_sacklast = nrsm;
8817 	/* Pass back the moved. */
8818 	*moved_two = moved;
8819 	return (changed);
8820 }
8821 
8822 static void inline
8823 rack_peer_reneges(struct tcp_rack *rack, struct rack_sendmap *rsm, tcp_seq th_ack)
8824 {
8825 	struct rack_sendmap *tmap;
8826 
8827 	tmap = NULL;
8828 	while (rsm && (rsm->r_flags & RACK_ACKED)) {
8829 		/* Its no longer sacked, mark it so */
8830 		rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
8831 #ifdef INVARIANTS
8832 		if (rsm->r_in_tmap) {
8833 			panic("rack:%p rsm:%p flags:0x%x in tmap?",
8834 			      rack, rsm, rsm->r_flags);
8835 		}
8836 #endif
8837 		rsm->r_flags &= ~(RACK_ACKED|RACK_SACK_PASSED|RACK_WAS_SACKPASS);
8838 		/* Rebuild it into our tmap */
8839 		if (tmap == NULL) {
8840 			TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8841 			tmap = rsm;
8842 		} else {
8843 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, tmap, rsm, r_tnext);
8844 			tmap = rsm;
8845 		}
8846 		tmap->r_in_tmap = 1;
8847 		rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8848 	}
8849 	/*
8850 	 * Now lets possibly clear the sack filter so we start
8851 	 * recognizing sacks that cover this area.
8852 	 */
8853 	sack_filter_clear(&rack->r_ctl.rack_sf, th_ack);
8854 
8855 }
8856 
8857 static void
8858 rack_do_decay(struct tcp_rack *rack)
8859 {
8860 	struct timeval res;
8861 
8862 #define	timersub(tvp, uvp, vvp)						\
8863 	do {								\
8864 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
8865 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
8866 		if ((vvp)->tv_usec < 0) {				\
8867 			(vvp)->tv_sec--;				\
8868 			(vvp)->tv_usec += 1000000;			\
8869 		}							\
8870 	} while (0)
8871 
8872 	timersub(&rack->r_ctl.act_rcv_time, &rack->r_ctl.rc_last_time_decay, &res);
8873 #undef timersub
8874 
8875 	rack->r_ctl.input_pkt++;
8876 	if ((rack->rc_in_persist) ||
8877 	    (res.tv_sec >= 1) ||
8878 	    (rack->rc_tp->snd_max == rack->rc_tp->snd_una)) {
8879 		/*
8880 		 * Check for decay of non-SAD,
8881 		 * we want all SAD detection metrics to
8882 		 * decay 1/4 per second (or more) passed.
8883 		 */
8884 #ifdef NETFLIX_EXP_DETECTION
8885 		uint32_t pkt_delta;
8886 
8887 		pkt_delta = rack->r_ctl.input_pkt - rack->r_ctl.saved_input_pkt;
8888 #endif
8889 		/* Update our saved tracking values */
8890 		rack->r_ctl.saved_input_pkt = rack->r_ctl.input_pkt;
8891 		rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time;
8892 		/* Now do we escape without decay? */
8893 #ifdef NETFLIX_EXP_DETECTION
8894 		if (rack->rc_in_persist ||
8895 		    (rack->rc_tp->snd_max == rack->rc_tp->snd_una) ||
8896 		    (pkt_delta < tcp_sad_low_pps)){
8897 			/*
8898 			 * We don't decay idle connections
8899 			 * or ones that have a low input pps.
8900 			 */
8901 			return;
8902 		}
8903 		/* Decay the counters */
8904 		rack->r_ctl.ack_count = ctf_decay_count(rack->r_ctl.ack_count,
8905 							tcp_sad_decay_val);
8906 		rack->r_ctl.sack_count = ctf_decay_count(rack->r_ctl.sack_count,
8907 							 tcp_sad_decay_val);
8908 		rack->r_ctl.sack_moved_extra = ctf_decay_count(rack->r_ctl.sack_moved_extra,
8909 							       tcp_sad_decay_val);
8910 		rack->r_ctl.sack_noextra_move = ctf_decay_count(rack->r_ctl.sack_noextra_move,
8911 								tcp_sad_decay_val);
8912 #endif
8913 	}
8914 }
8915 
8916 static void
8917 rack_process_to_cumack(struct tcpcb *tp, struct tcp_rack *rack, register uint32_t th_ack, uint32_t cts, struct tcpopt *to)
8918 {
8919 	struct rack_sendmap *rsm;
8920 #ifdef INVARIANTS
8921 	struct rack_sendmap *rm;
8922 #endif
8923 
8924 	/*
8925 	 * The ACK point is advancing to th_ack, we must drop off
8926 	 * the packets in the rack log and calculate any eligble
8927 	 * RTT's.
8928 	 */
8929 	rack->r_wanted_output = 1;
8930 
8931 	/* Tend any TLP that has been marked for 1/2 the seq space (its old)  */
8932 	if ((rack->rc_last_tlp_acked_set == 1)&&
8933 	    (rack->rc_last_tlp_past_cumack == 1) &&
8934 	    (SEQ_GT(rack->r_ctl.last_tlp_acked_start, th_ack))) {
8935 		/*
8936 		 * We have reached the point where our last rack
8937 		 * tlp retransmit sequence is ahead of the cum-ack.
8938 		 * This can only happen when the cum-ack moves all
8939 		 * the way around (its been a full 2^^31+1 bytes
8940 		 * or more since we sent a retransmitted TLP). Lets
8941 		 * turn off the valid flag since its not really valid.
8942 		 *
8943 		 * Note since sack's also turn on this event we have
8944 		 * a complication, we have to wait to age it out until
8945 		 * the cum-ack is by the TLP before checking which is
8946 		 * what the next else clause does.
8947 		 */
8948 		rack_log_dsack_event(rack, 9, __LINE__,
8949 				     rack->r_ctl.last_tlp_acked_start,
8950 				     rack->r_ctl.last_tlp_acked_end);
8951 		rack->rc_last_tlp_acked_set = 0;
8952 		rack->rc_last_tlp_past_cumack = 0;
8953 	} else if ((rack->rc_last_tlp_acked_set == 1) &&
8954 		   (rack->rc_last_tlp_past_cumack == 0) &&
8955 		   (SEQ_GEQ(th_ack, rack->r_ctl.last_tlp_acked_end))) {
8956 		/*
8957 		 * It is safe to start aging TLP's out.
8958 		 */
8959 		rack->rc_last_tlp_past_cumack = 1;
8960 	}
8961 	/* We do the same for the tlp send seq as well */
8962 	if ((rack->rc_last_sent_tlp_seq_valid == 1) &&
8963 	    (rack->rc_last_sent_tlp_past_cumack == 1) &&
8964 	    (SEQ_GT(rack->r_ctl.last_sent_tlp_seq,  th_ack))) {
8965 		rack_log_dsack_event(rack, 9, __LINE__,
8966 				     rack->r_ctl.last_sent_tlp_seq,
8967 				     (rack->r_ctl.last_sent_tlp_seq +
8968 				      rack->r_ctl.last_sent_tlp_len));
8969 		rack->rc_last_sent_tlp_seq_valid = 0;
8970 		rack->rc_last_sent_tlp_past_cumack = 0;
8971 	} else if ((rack->rc_last_sent_tlp_seq_valid == 1) &&
8972 		   (rack->rc_last_sent_tlp_past_cumack == 0) &&
8973 		   (SEQ_GEQ(th_ack, rack->r_ctl.last_sent_tlp_seq))) {
8974 		/*
8975 		 * It is safe to start aging TLP's send.
8976 		 */
8977 		rack->rc_last_sent_tlp_past_cumack = 1;
8978 	}
8979 more:
8980 	rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
8981 	if (rsm == NULL) {
8982 		if ((th_ack - 1) == tp->iss) {
8983 			/*
8984 			 * For the SYN incoming case we will not
8985 			 * have called tcp_output for the sending of
8986 			 * the SYN, so there will be no map. All
8987 			 * other cases should probably be a panic.
8988 			 */
8989 			return;
8990 		}
8991 		if (tp->t_flags & TF_SENTFIN) {
8992 			/* if we sent a FIN we often will not have map */
8993 			return;
8994 		}
8995 #ifdef INVARIANTS
8996 		panic("No rack map tp:%p for state:%d ack:%u rack:%p snd_una:%u snd_max:%u snd_nxt:%u\n",
8997 		      tp,
8998 		      tp->t_state, th_ack, rack,
8999 		      tp->snd_una, tp->snd_max, tp->snd_nxt);
9000 #endif
9001 		return;
9002 	}
9003 	if (SEQ_LT(th_ack, rsm->r_start)) {
9004 		/* Huh map is missing this */
9005 #ifdef INVARIANTS
9006 		printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d\n",
9007 		       rsm->r_start,
9008 		       th_ack, tp->t_state, rack->r_state);
9009 #endif
9010 		return;
9011 	}
9012 	rack_update_rtt(tp, rack, rsm, to, cts, CUM_ACKED, th_ack);
9013 
9014 	/* Now was it a retransmitted TLP? */
9015 	if ((rsm->r_flags & RACK_TLP) &&
9016 	    (rsm->r_rtr_cnt > 1)) {
9017 		/*
9018 		 * Yes, this rsm was a TLP and retransmitted, remember that
9019 		 * since if a DSACK comes back on this we don't want
9020 		 * to think of it as a reordered segment. This may
9021 		 * get updated again with possibly even other TLPs
9022 		 * in flight, but thats ok. Only when we don't send
9023 		 * a retransmitted TLP for 1/2 the sequences space
9024 		 * will it get turned off (above).
9025 		 */
9026 		if (rack->rc_last_tlp_acked_set &&
9027 		    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
9028 			/*
9029 			 * We already turned this on since the end matches,
9030 			 * the previous one was a partially ack now we
9031 			 * are getting another one (maybe all of it).
9032 			 */
9033 			rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
9034 			/*
9035 			 * Lets make sure we have all of it though.
9036 			 */
9037 			if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
9038 				rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9039 				rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9040 						     rack->r_ctl.last_tlp_acked_end);
9041 			}
9042 			if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
9043 				rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9044 				rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9045 						     rack->r_ctl.last_tlp_acked_end);
9046 			}
9047 		} else {
9048 			rack->rc_last_tlp_past_cumack = 1;
9049 			rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9050 			rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9051 			rack->rc_last_tlp_acked_set = 1;
9052 			rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
9053 		}
9054 	}
9055 	/* Now do we consume the whole thing? */
9056 	if (SEQ_GEQ(th_ack, rsm->r_end)) {
9057 		/* Its all consumed. */
9058 		uint32_t left;
9059 		uint8_t newly_acked;
9060 
9061 		rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_FREE, rsm->r_end, __LINE__);
9062 		rack->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
9063 		rsm->r_rtr_bytes = 0;
9064 		/* Record the time of highest cumack sent */
9065 		rack->r_ctl.rc_gp_cumack_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
9066 #ifndef INVARIANTS
9067 		(void)RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
9068 #else
9069 		rm = RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
9070 		if (rm != rsm) {
9071 			panic("removing head in rack:%p rsm:%p rm:%p",
9072 			      rack, rsm, rm);
9073 		}
9074 #endif
9075 		if (rsm->r_in_tmap) {
9076 			TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
9077 			rsm->r_in_tmap = 0;
9078 		}
9079 		newly_acked = 1;
9080 		if (rsm->r_flags & RACK_ACKED) {
9081 			/*
9082 			 * It was acked on the scoreboard -- remove
9083 			 * it from total
9084 			 */
9085 			rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
9086 			newly_acked = 0;
9087 		} else if (rsm->r_flags & RACK_SACK_PASSED) {
9088 			/*
9089 			 * There are segments ACKED on the
9090 			 * scoreboard further up. We are seeing
9091 			 * reordering.
9092 			 */
9093 			rsm->r_flags &= ~RACK_SACK_PASSED;
9094 			rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
9095 			rsm->r_flags |= RACK_ACKED;
9096 			rack->r_ctl.rc_reorder_ts = cts;
9097 			if (rack->r_ent_rec_ns) {
9098 				/*
9099 				 * We have sent no more, and we saw an sack
9100 				 * then ack arrive.
9101 				 */
9102 				rack->r_might_revert = 1;
9103 			}
9104 		}
9105 		if ((rsm->r_flags & RACK_TO_REXT) &&
9106 		    (tp->t_flags & TF_RCVD_TSTMP) &&
9107 		    (to->to_flags & TOF_TS) &&
9108 		    (to->to_tsecr != 0) &&
9109 		    (tp->t_flags & TF_PREVVALID)) {
9110 			/*
9111 			 * We can use the timestamp to see
9112 			 * if this retransmission was from the
9113 			 * first transmit. If so we made a mistake.
9114 			 */
9115 			tp->t_flags &= ~TF_PREVVALID;
9116 			if (to->to_tsecr == rack_ts_to_msec(rsm->r_tim_lastsent[0])) {
9117 				/* The first transmit is what this ack is for */
9118 				rack_cong_signal(tp, CC_RTO_ERR, th_ack, __LINE__);
9119 			}
9120 		}
9121 		left = th_ack - rsm->r_end;
9122 		if (rack->app_limited_needs_set && newly_acked)
9123 			rack_need_set_test(tp, rack, rsm, th_ack, __LINE__, RACK_USE_END_OR_THACK);
9124 		/* Free back to zone */
9125 		rack_free(rack, rsm);
9126 		if (left) {
9127 			goto more;
9128 		}
9129 		/* Check for reneging */
9130 		rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
9131 		if (rsm && (rsm->r_flags & RACK_ACKED) && (th_ack == rsm->r_start)) {
9132 			/*
9133 			 * The peer has moved snd_una up to
9134 			 * the edge of this send, i.e. one
9135 			 * that it had previously acked. The only
9136 			 * way that can be true if the peer threw
9137 			 * away data (space issues) that it had
9138 			 * previously sacked (else it would have
9139 			 * given us snd_una up to (rsm->r_end).
9140 			 * We need to undo the acked markings here.
9141 			 *
9142 			 * Note we have to look to make sure th_ack is
9143 			 * our rsm->r_start in case we get an old ack
9144 			 * where th_ack is behind snd_una.
9145 			 */
9146 			rack_peer_reneges(rack, rsm, th_ack);
9147 		}
9148 		return;
9149 	}
9150 	if (rsm->r_flags & RACK_ACKED) {
9151 		/*
9152 		 * It was acked on the scoreboard -- remove it from
9153 		 * total for the part being cum-acked.
9154 		 */
9155 		rack->r_ctl.rc_sacked -= (th_ack - rsm->r_start);
9156 	}
9157 	/*
9158 	 * Clear the dup ack count for
9159 	 * the piece that remains.
9160 	 */
9161 	rsm->r_dupack = 0;
9162 	rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
9163 	if (rsm->r_rtr_bytes) {
9164 		/*
9165 		 * It was retransmitted adjust the
9166 		 * sack holes for what was acked.
9167 		 */
9168 		int ack_am;
9169 
9170 		ack_am = (th_ack - rsm->r_start);
9171 		if (ack_am >= rsm->r_rtr_bytes) {
9172 			rack->r_ctl.rc_holes_rxt -= ack_am;
9173 			rsm->r_rtr_bytes -= ack_am;
9174 		}
9175 	}
9176 	/*
9177 	 * Update where the piece starts and record
9178 	 * the time of send of highest cumack sent.
9179 	 */
9180 	rack->r_ctl.rc_gp_cumack_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
9181 	rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_TRIM_HEAD, th_ack, __LINE__);
9182 	/* Now we need to move our offset forward too */
9183 	if (rsm->m && (rsm->orig_m_len != rsm->m->m_len)) {
9184 		/* Fix up the orig_m_len and possibly the mbuf offset */
9185 		rack_adjust_orig_mlen(rsm);
9186 	}
9187 	rsm->soff += (th_ack - rsm->r_start);
9188 	rsm->r_start = th_ack;
9189 	/* Now do we need to move the mbuf fwd too? */
9190 	if (rsm->m) {
9191 		while (rsm->soff >= rsm->m->m_len) {
9192 			rsm->soff -= rsm->m->m_len;
9193 			rsm->m = rsm->m->m_next;
9194 			KASSERT((rsm->m != NULL),
9195 				(" nrsm:%p hit at soff:%u null m",
9196 				 rsm, rsm->soff));
9197 		}
9198 		rsm->orig_m_len = rsm->m->m_len;
9199 	}
9200 	if (rack->app_limited_needs_set)
9201 		rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_BEG);
9202 }
9203 
9204 static void
9205 rack_handle_might_revert(struct tcpcb *tp, struct tcp_rack *rack)
9206 {
9207 	struct rack_sendmap *rsm;
9208 	int sack_pass_fnd = 0;
9209 
9210 	if (rack->r_might_revert) {
9211 		/*
9212 		 * Ok we have reordering, have not sent anything, we
9213 		 * might want to revert the congestion state if nothing
9214 		 * further has SACK_PASSED on it. Lets check.
9215 		 *
9216 		 * We also get here when we have DSACKs come in for
9217 		 * all the data that we FR'd. Note that a rxt or tlp
9218 		 * timer clears this from happening.
9219 		 */
9220 
9221 		TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
9222 			if (rsm->r_flags & RACK_SACK_PASSED) {
9223 				sack_pass_fnd = 1;
9224 				break;
9225 			}
9226 		}
9227 		if (sack_pass_fnd == 0) {
9228 			/*
9229 			 * We went into recovery
9230 			 * incorrectly due to reordering!
9231 			 */
9232 			int orig_cwnd;
9233 
9234 			rack->r_ent_rec_ns = 0;
9235 			orig_cwnd = tp->snd_cwnd;
9236 			tp->snd_ssthresh = rack->r_ctl.rc_ssthresh_at_erec;
9237 			tp->snd_recover = tp->snd_una;
9238 			rack_log_to_prr(rack, 14, orig_cwnd, __LINE__);
9239 			EXIT_RECOVERY(tp->t_flags);
9240 		}
9241 		rack->r_might_revert = 0;
9242 	}
9243 }
9244 
9245 #ifdef NETFLIX_EXP_DETECTION
9246 static void
9247 rack_do_detection(struct tcpcb *tp, struct tcp_rack *rack,  uint32_t bytes_this_ack, uint32_t segsiz)
9248 {
9249 	if ((rack->do_detection || tcp_force_detection) &&
9250 	    tcp_sack_to_ack_thresh &&
9251 	    tcp_sack_to_move_thresh &&
9252 	    ((rack->r_ctl.rc_num_maps_alloced > tcp_map_minimum) || rack->sack_attack_disable)) {
9253 		/*
9254 		 * We have thresholds set to find
9255 		 * possible attackers and disable sack.
9256 		 * Check them.
9257 		 */
9258 		uint64_t ackratio, moveratio, movetotal;
9259 
9260 		/* Log detecting */
9261 		rack_log_sad(rack, 1);
9262 		ackratio = (uint64_t)(rack->r_ctl.sack_count);
9263 		ackratio *= (uint64_t)(1000);
9264 		if (rack->r_ctl.ack_count)
9265 			ackratio /= (uint64_t)(rack->r_ctl.ack_count);
9266 		else {
9267 			/* We really should not hit here */
9268 			ackratio = 1000;
9269 		}
9270 		if ((rack->sack_attack_disable == 0) &&
9271 		    (ackratio > rack_highest_sack_thresh_seen))
9272 			rack_highest_sack_thresh_seen = (uint32_t)ackratio;
9273 		movetotal = rack->r_ctl.sack_moved_extra;
9274 		movetotal += rack->r_ctl.sack_noextra_move;
9275 		moveratio = rack->r_ctl.sack_moved_extra;
9276 		moveratio *= (uint64_t)1000;
9277 		if (movetotal)
9278 			moveratio /= movetotal;
9279 		else {
9280 			/* No moves, thats pretty good */
9281 			moveratio = 0;
9282 		}
9283 		if ((rack->sack_attack_disable == 0) &&
9284 		    (moveratio > rack_highest_move_thresh_seen))
9285 			rack_highest_move_thresh_seen = (uint32_t)moveratio;
9286 		if (rack->sack_attack_disable == 0) {
9287 			if ((ackratio > tcp_sack_to_ack_thresh) &&
9288 			    (moveratio > tcp_sack_to_move_thresh)) {
9289 				/* Disable sack processing */
9290 				rack->sack_attack_disable = 1;
9291 				if (rack->r_rep_attack == 0) {
9292 					rack->r_rep_attack = 1;
9293 					counter_u64_add(rack_sack_attacks_detected, 1);
9294 				}
9295 				if (tcp_attack_on_turns_on_logging) {
9296 					/*
9297 					 * Turn on logging, used for debugging
9298 					 * false positives.
9299 					 */
9300 					rack->rc_tp->t_logstate = tcp_attack_on_turns_on_logging;
9301 				}
9302 				/* Clamp the cwnd at flight size */
9303 				rack->r_ctl.rc_saved_cwnd = rack->rc_tp->snd_cwnd;
9304 				rack->rc_tp->snd_cwnd = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
9305 				rack_log_sad(rack, 2);
9306 			}
9307 		} else {
9308 			/* We are sack-disabled check for false positives */
9309 			if ((ackratio <= tcp_restoral_thresh) ||
9310 			    (rack->r_ctl.rc_num_maps_alloced  < tcp_map_minimum)) {
9311 				rack->sack_attack_disable = 0;
9312 				rack_log_sad(rack, 3);
9313 				/* Restart counting */
9314 				rack->r_ctl.sack_count = 0;
9315 				rack->r_ctl.sack_moved_extra = 0;
9316 				rack->r_ctl.sack_noextra_move = 1;
9317 				rack->r_ctl.ack_count = max(1,
9318 				      (bytes_this_ack / segsiz));
9319 
9320 				if (rack->r_rep_reverse == 0) {
9321 					rack->r_rep_reverse = 1;
9322 					counter_u64_add(rack_sack_attacks_reversed, 1);
9323 				}
9324 				/* Restore the cwnd */
9325 				if (rack->r_ctl.rc_saved_cwnd > rack->rc_tp->snd_cwnd)
9326 					rack->rc_tp->snd_cwnd = rack->r_ctl.rc_saved_cwnd;
9327 			}
9328 		}
9329 	}
9330 }
9331 #endif
9332 
9333 static int
9334 rack_note_dsack(struct tcp_rack *rack, tcp_seq start, tcp_seq end)
9335 {
9336 
9337 	uint32_t am, l_end;
9338 	int was_tlp = 0;
9339 
9340 	if (SEQ_GT(end, start))
9341 		am = end - start;
9342 	else
9343 		am = 0;
9344 	if ((rack->rc_last_tlp_acked_set ) &&
9345 	    (SEQ_GEQ(start, rack->r_ctl.last_tlp_acked_start)) &&
9346 	    (SEQ_LEQ(end, rack->r_ctl.last_tlp_acked_end))) {
9347 		/*
9348 		 * The DSACK is because of a TLP which we don't
9349 		 * do anything with the reordering window over since
9350 		 * it was not reordering that caused the DSACK but
9351 		 * our previous retransmit TLP.
9352 		 */
9353 		rack_log_dsack_event(rack, 7, __LINE__, start, end);
9354 		was_tlp = 1;
9355 		goto skip_dsack_round;
9356 	}
9357 	if (rack->rc_last_sent_tlp_seq_valid) {
9358 		l_end = rack->r_ctl.last_sent_tlp_seq + rack->r_ctl.last_sent_tlp_len;
9359 		if (SEQ_GEQ(start, rack->r_ctl.last_sent_tlp_seq) &&
9360 		    (SEQ_LEQ(end, l_end))) {
9361 			/*
9362 			 * This dsack is from the last sent TLP, ignore it
9363 			 * for reordering purposes.
9364 			 */
9365 			rack_log_dsack_event(rack, 7, __LINE__, start, end);
9366 			was_tlp = 1;
9367 			goto skip_dsack_round;
9368 		}
9369 	}
9370 	if (rack->rc_dsack_round_seen == 0) {
9371 		rack->rc_dsack_round_seen = 1;
9372 		rack->r_ctl.dsack_round_end = rack->rc_tp->snd_max;
9373 		rack->r_ctl.num_dsack++;
9374 		rack->r_ctl.dsack_persist = 16;	/* 16 is from the standard */
9375 		rack_log_dsack_event(rack, 2, __LINE__, 0, 0);
9376 	}
9377 skip_dsack_round:
9378 	/*
9379 	 * We keep track of how many DSACK blocks we get
9380 	 * after a recovery incident.
9381 	 */
9382 	rack->r_ctl.dsack_byte_cnt += am;
9383 	if (!IN_FASTRECOVERY(rack->rc_tp->t_flags) &&
9384 	    rack->r_ctl.retran_during_recovery &&
9385 	    (rack->r_ctl.dsack_byte_cnt >= rack->r_ctl.retran_during_recovery)) {
9386 		/*
9387 		 * False recovery most likely culprit is reordering. If
9388 		 * nothing else is missing we need to revert.
9389 		 */
9390 		rack->r_might_revert = 1;
9391 		rack_handle_might_revert(rack->rc_tp, rack);
9392 		rack->r_might_revert = 0;
9393 		rack->r_ctl.retran_during_recovery = 0;
9394 		rack->r_ctl.dsack_byte_cnt = 0;
9395 	}
9396 	return (was_tlp);
9397 }
9398 
9399 static void
9400 rack_update_prr(struct tcpcb *tp, struct tcp_rack *rack, uint32_t changed, tcp_seq th_ack)
9401 {
9402 	/* Deal with changed and PRR here (in recovery only) */
9403 	uint32_t pipe, snd_una;
9404 
9405 	rack->r_ctl.rc_prr_delivered += changed;
9406 
9407 	if (sbavail(&rack->rc_inp->inp_socket->so_snd) <= (tp->snd_max - tp->snd_una)) {
9408 		/*
9409 		 * It is all outstanding, we are application limited
9410 		 * and thus we don't need more room to send anything.
9411 		 * Note we use tp->snd_una here and not th_ack because
9412 		 * the data as yet not been cut from the sb.
9413 		 */
9414 		rack->r_ctl.rc_prr_sndcnt = 0;
9415 		return;
9416 	}
9417 	/* Compute prr_sndcnt */
9418 	if (SEQ_GT(tp->snd_una, th_ack)) {
9419 		snd_una = tp->snd_una;
9420 	} else {
9421 		snd_una = th_ack;
9422 	}
9423 	pipe = ((tp->snd_max - snd_una) - rack->r_ctl.rc_sacked) + rack->r_ctl.rc_holes_rxt;
9424 	if (pipe > tp->snd_ssthresh) {
9425 		long sndcnt;
9426 
9427 		sndcnt = rack->r_ctl.rc_prr_delivered * tp->snd_ssthresh;
9428 		if (rack->r_ctl.rc_prr_recovery_fs > 0)
9429 			sndcnt /= (long)rack->r_ctl.rc_prr_recovery_fs;
9430 		else {
9431 			rack->r_ctl.rc_prr_sndcnt = 0;
9432 			rack_log_to_prr(rack, 9, 0, __LINE__);
9433 			sndcnt = 0;
9434 		}
9435 		sndcnt++;
9436 		if (sndcnt > (long)rack->r_ctl.rc_prr_out)
9437 			sndcnt -= rack->r_ctl.rc_prr_out;
9438 		else
9439 			sndcnt = 0;
9440 		rack->r_ctl.rc_prr_sndcnt = sndcnt;
9441 		rack_log_to_prr(rack, 10, 0, __LINE__);
9442 	} else {
9443 		uint32_t limit;
9444 
9445 		if (rack->r_ctl.rc_prr_delivered > rack->r_ctl.rc_prr_out)
9446 			limit = (rack->r_ctl.rc_prr_delivered - rack->r_ctl.rc_prr_out);
9447 		else
9448 			limit = 0;
9449 		if (changed > limit)
9450 			limit = changed;
9451 		limit += ctf_fixed_maxseg(tp);
9452 		if (tp->snd_ssthresh > pipe) {
9453 			rack->r_ctl.rc_prr_sndcnt = min((tp->snd_ssthresh - pipe), limit);
9454 			rack_log_to_prr(rack, 11, 0, __LINE__);
9455 		} else {
9456 			rack->r_ctl.rc_prr_sndcnt = min(0, limit);
9457 			rack_log_to_prr(rack, 12, 0, __LINE__);
9458 		}
9459 	}
9460 }
9461 
9462 static void
9463 rack_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th, int entered_recovery, int dup_ack_struck)
9464 {
9465 	uint32_t changed;
9466 	struct tcp_rack *rack;
9467 	struct rack_sendmap *rsm;
9468 	struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1];
9469 	register uint32_t th_ack;
9470 	int32_t i, j, k, num_sack_blks = 0;
9471 	uint32_t cts, acked, ack_point;
9472 	int loop_start = 0, moved_two = 0;
9473 	uint32_t tsused;
9474 
9475 
9476 	INP_WLOCK_ASSERT(tp->t_inpcb);
9477 	if (tcp_get_flags(th) & TH_RST) {
9478 		/* We don't log resets */
9479 		return;
9480 	}
9481 	rack = (struct tcp_rack *)tp->t_fb_ptr;
9482 	cts = tcp_get_usecs(NULL);
9483 	rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
9484 	changed = 0;
9485 	th_ack = th->th_ack;
9486 	if (rack->sack_attack_disable == 0)
9487 		rack_do_decay(rack);
9488 	if (BYTES_THIS_ACK(tp, th) >= ctf_fixed_maxseg(rack->rc_tp)) {
9489 		/*
9490 		 * You only get credit for
9491 		 * MSS and greater (and you get extra
9492 		 * credit for larger cum-ack moves).
9493 		 */
9494 		int ac;
9495 
9496 		ac = BYTES_THIS_ACK(tp, th) / ctf_fixed_maxseg(rack->rc_tp);
9497 		rack->r_ctl.ack_count += ac;
9498 		counter_u64_add(rack_ack_total, ac);
9499 	}
9500 	if (rack->r_ctl.ack_count > 0xfff00000) {
9501 		/*
9502 		 * reduce the number to keep us under
9503 		 * a uint32_t.
9504 		 */
9505 		rack->r_ctl.ack_count /= 2;
9506 		rack->r_ctl.sack_count /= 2;
9507 	}
9508 	if (SEQ_GT(th_ack, tp->snd_una)) {
9509 		rack_log_progress_event(rack, tp, ticks, PROGRESS_UPDATE, __LINE__);
9510 		tp->t_acktime = ticks;
9511 	}
9512 	if (rsm && SEQ_GT(th_ack, rsm->r_start))
9513 		changed = th_ack - rsm->r_start;
9514 	if (changed) {
9515 		rack_process_to_cumack(tp, rack, th_ack, cts, to);
9516 	}
9517 	if ((to->to_flags & TOF_SACK) == 0) {
9518 		/* We are done nothing left and no sack. */
9519 		rack_handle_might_revert(tp, rack);
9520 		/*
9521 		 * For cases where we struck a dup-ack
9522 		 * with no SACK, add to the changes so
9523 		 * PRR will work right.
9524 		 */
9525 		if (dup_ack_struck && (changed == 0)) {
9526 			changed += ctf_fixed_maxseg(rack->rc_tp);
9527 		}
9528 		goto out;
9529 	}
9530 	/* Sack block processing */
9531 	if (SEQ_GT(th_ack, tp->snd_una))
9532 		ack_point = th_ack;
9533 	else
9534 		ack_point = tp->snd_una;
9535 	for (i = 0; i < to->to_nsacks; i++) {
9536 		bcopy((to->to_sacks + i * TCPOLEN_SACK),
9537 		      &sack, sizeof(sack));
9538 		sack.start = ntohl(sack.start);
9539 		sack.end = ntohl(sack.end);
9540 		if (SEQ_GT(sack.end, sack.start) &&
9541 		    SEQ_GT(sack.start, ack_point) &&
9542 		    SEQ_LT(sack.start, tp->snd_max) &&
9543 		    SEQ_GT(sack.end, ack_point) &&
9544 		    SEQ_LEQ(sack.end, tp->snd_max)) {
9545 			sack_blocks[num_sack_blks] = sack;
9546 			num_sack_blks++;
9547 		} else if (SEQ_LEQ(sack.start, th_ack) &&
9548 			   SEQ_LEQ(sack.end, th_ack)) {
9549 			int was_tlp;
9550 
9551 			was_tlp = rack_note_dsack(rack, sack.start, sack.end);
9552 			/*
9553 			 * Its a D-SACK block.
9554 			 */
9555 			tcp_record_dsack(tp, sack.start, sack.end, was_tlp);
9556 		}
9557 	}
9558 	if (rack->rc_dsack_round_seen) {
9559 		/* Is the dsack roound over? */
9560 		if (SEQ_GEQ(th_ack, rack->r_ctl.dsack_round_end)) {
9561 			/* Yes it is */
9562 			rack->rc_dsack_round_seen = 0;
9563 			rack_log_dsack_event(rack, 3, __LINE__, 0, 0);
9564 		}
9565 	}
9566 	/*
9567 	 * Sort the SACK blocks so we can update the rack scoreboard with
9568 	 * just one pass.
9569 	 */
9570 	num_sack_blks = sack_filter_blks(&rack->r_ctl.rack_sf, sack_blocks,
9571 					 num_sack_blks, th->th_ack);
9572 	ctf_log_sack_filter(rack->rc_tp, num_sack_blks, sack_blocks);
9573 	if (num_sack_blks == 0) {
9574 		/* Nothing to sack (DSACKs?) */
9575 		goto out_with_totals;
9576 	}
9577 	if (num_sack_blks < 2) {
9578 		/* Only one, we don't need to sort */
9579 		goto do_sack_work;
9580 	}
9581 	/* Sort the sacks */
9582 	for (i = 0; i < num_sack_blks; i++) {
9583 		for (j = i + 1; j < num_sack_blks; j++) {
9584 			if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
9585 				sack = sack_blocks[i];
9586 				sack_blocks[i] = sack_blocks[j];
9587 				sack_blocks[j] = sack;
9588 			}
9589 		}
9590 	}
9591 	/*
9592 	 * Now are any of the sack block ends the same (yes some
9593 	 * implementations send these)?
9594 	 */
9595 again:
9596 	if (num_sack_blks == 0)
9597 		goto out_with_totals;
9598 	if (num_sack_blks > 1) {
9599 		for (i = 0; i < num_sack_blks; i++) {
9600 			for (j = i + 1; j < num_sack_blks; j++) {
9601 				if (sack_blocks[i].end == sack_blocks[j].end) {
9602 					/*
9603 					 * Ok these two have the same end we
9604 					 * want the smallest end and then
9605 					 * throw away the larger and start
9606 					 * again.
9607 					 */
9608 					if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) {
9609 						/*
9610 						 * The second block covers
9611 						 * more area use that
9612 						 */
9613 						sack_blocks[i].start = sack_blocks[j].start;
9614 					}
9615 					/*
9616 					 * Now collapse out the dup-sack and
9617 					 * lower the count
9618 					 */
9619 					for (k = (j + 1); k < num_sack_blks; k++) {
9620 						sack_blocks[j].start = sack_blocks[k].start;
9621 						sack_blocks[j].end = sack_blocks[k].end;
9622 						j++;
9623 					}
9624 					num_sack_blks--;
9625 					goto again;
9626 				}
9627 			}
9628 		}
9629 	}
9630 do_sack_work:
9631 	/*
9632 	 * First lets look to see if
9633 	 * we have retransmitted and
9634 	 * can use the transmit next?
9635 	 */
9636 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
9637 	if (rsm &&
9638 	    SEQ_GT(sack_blocks[0].end, rsm->r_start) &&
9639 	    SEQ_LT(sack_blocks[0].start, rsm->r_end)) {
9640 		/*
9641 		 * We probably did the FR and the next
9642 		 * SACK in continues as we would expect.
9643 		 */
9644 		acked = rack_proc_sack_blk(tp, rack, &sack_blocks[0], to, &rsm, cts, &moved_two);
9645 		if (acked) {
9646 			rack->r_wanted_output = 1;
9647 			changed += acked;
9648 		}
9649 		if (num_sack_blks == 1) {
9650 			/*
9651 			 * This is what we would expect from
9652 			 * a normal implementation to happen
9653 			 * after we have retransmitted the FR,
9654 			 * i.e the sack-filter pushes down
9655 			 * to 1 block and the next to be retransmitted
9656 			 * is the sequence in the sack block (has more
9657 			 * are acked). Count this as ACK'd data to boost
9658 			 * up the chances of recovering any false positives.
9659 			 */
9660 			rack->r_ctl.ack_count += (acked / ctf_fixed_maxseg(rack->rc_tp));
9661 			counter_u64_add(rack_ack_total, (acked / ctf_fixed_maxseg(rack->rc_tp)));
9662 			counter_u64_add(rack_express_sack, 1);
9663 			if (rack->r_ctl.ack_count > 0xfff00000) {
9664 				/*
9665 				 * reduce the number to keep us under
9666 				 * a uint32_t.
9667 				 */
9668 				rack->r_ctl.ack_count /= 2;
9669 				rack->r_ctl.sack_count /= 2;
9670 			}
9671 			goto out_with_totals;
9672 		} else {
9673 			/*
9674 			 * Start the loop through the
9675 			 * rest of blocks, past the first block.
9676 			 */
9677 			moved_two = 0;
9678 			loop_start = 1;
9679 		}
9680 	}
9681 	/* Its a sack of some sort */
9682 	rack->r_ctl.sack_count++;
9683 	if (rack->r_ctl.sack_count > 0xfff00000) {
9684 		/*
9685 		 * reduce the number to keep us under
9686 		 * a uint32_t.
9687 		 */
9688 		rack->r_ctl.ack_count /= 2;
9689 		rack->r_ctl.sack_count /= 2;
9690 	}
9691 	counter_u64_add(rack_sack_total, 1);
9692 	if (rack->sack_attack_disable) {
9693 		/* An attacker disablement is in place */
9694 		if (num_sack_blks > 1) {
9695 			rack->r_ctl.sack_count += (num_sack_blks - 1);
9696 			rack->r_ctl.sack_moved_extra++;
9697 			counter_u64_add(rack_move_some, 1);
9698 			if (rack->r_ctl.sack_moved_extra > 0xfff00000) {
9699 				rack->r_ctl.sack_moved_extra /= 2;
9700 				rack->r_ctl.sack_noextra_move /= 2;
9701 			}
9702 		}
9703 		goto out;
9704 	}
9705 	rsm = rack->r_ctl.rc_sacklast;
9706 	for (i = loop_start; i < num_sack_blks; i++) {
9707 		acked = rack_proc_sack_blk(tp, rack, &sack_blocks[i], to, &rsm, cts, &moved_two);
9708 		if (acked) {
9709 			rack->r_wanted_output = 1;
9710 			changed += acked;
9711 		}
9712 		if (moved_two) {
9713 			/*
9714 			 * If we did not get a SACK for at least a MSS and
9715 			 * had to move at all, or if we moved more than our
9716 			 * threshold, it counts against the "extra" move.
9717 			 */
9718 			rack->r_ctl.sack_moved_extra += moved_two;
9719 			counter_u64_add(rack_move_some, 1);
9720 		} else {
9721 			/*
9722 			 * else we did not have to move
9723 			 * any more than we would expect.
9724 			 */
9725 			rack->r_ctl.sack_noextra_move++;
9726 			counter_u64_add(rack_move_none, 1);
9727 		}
9728 		if (moved_two && (acked < ctf_fixed_maxseg(rack->rc_tp))) {
9729 			/*
9730 			 * If the SACK was not a full MSS then
9731 			 * we add to sack_count the number of
9732 			 * MSS's (or possibly more than
9733 			 * a MSS if its a TSO send) we had to skip by.
9734 			 */
9735 			rack->r_ctl.sack_count += moved_two;
9736 			counter_u64_add(rack_sack_total, moved_two);
9737 		}
9738 		/*
9739 		 * Now we need to setup for the next
9740 		 * round. First we make sure we won't
9741 		 * exceed the size of our uint32_t on
9742 		 * the various counts, and then clear out
9743 		 * moved_two.
9744 		 */
9745 		if ((rack->r_ctl.sack_moved_extra > 0xfff00000) ||
9746 		    (rack->r_ctl.sack_noextra_move > 0xfff00000)) {
9747 			rack->r_ctl.sack_moved_extra /= 2;
9748 			rack->r_ctl.sack_noextra_move /= 2;
9749 		}
9750 		if (rack->r_ctl.sack_count > 0xfff00000) {
9751 			rack->r_ctl.ack_count /= 2;
9752 			rack->r_ctl.sack_count /= 2;
9753 		}
9754 		moved_two = 0;
9755 	}
9756 out_with_totals:
9757 	if (num_sack_blks > 1) {
9758 		/*
9759 		 * You get an extra stroke if
9760 		 * you have more than one sack-blk, this
9761 		 * could be where we are skipping forward
9762 		 * and the sack-filter is still working, or
9763 		 * it could be an attacker constantly
9764 		 * moving us.
9765 		 */
9766 		rack->r_ctl.sack_moved_extra++;
9767 		counter_u64_add(rack_move_some, 1);
9768 	}
9769 out:
9770 #ifdef NETFLIX_EXP_DETECTION
9771 	rack_do_detection(tp, rack, BYTES_THIS_ACK(tp, th), ctf_fixed_maxseg(rack->rc_tp));
9772 #endif
9773 	if (changed) {
9774 		/* Something changed cancel the rack timer */
9775 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
9776 	}
9777 	tsused = tcp_get_usecs(NULL);
9778 	rsm = tcp_rack_output(tp, rack, tsused);
9779 	if ((!IN_FASTRECOVERY(tp->t_flags)) &&
9780 	    rsm &&
9781 	    ((rsm->r_flags & RACK_MUST_RXT) == 0)) {
9782 		/* Enter recovery */
9783 		entered_recovery = 1;
9784 		rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__);
9785 		/*
9786 		 * When we enter recovery we need to assure we send
9787 		 * one packet.
9788 		 */
9789 		if (rack->rack_no_prr == 0) {
9790 			rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
9791 			rack_log_to_prr(rack, 8, 0, __LINE__);
9792 		}
9793 		rack->r_timer_override = 1;
9794 		rack->r_early = 0;
9795 		rack->r_ctl.rc_agg_early = 0;
9796 	} else if (IN_FASTRECOVERY(tp->t_flags) &&
9797 		   rsm &&
9798 		   (rack->r_rr_config == 3)) {
9799 		/*
9800 		 * Assure we can output and we get no
9801 		 * remembered pace time except the retransmit.
9802 		 */
9803 		rack->r_timer_override = 1;
9804 		rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
9805 		rack->r_ctl.rc_resend = rsm;
9806 	}
9807 	if (IN_FASTRECOVERY(tp->t_flags) &&
9808 	    (rack->rack_no_prr == 0) &&
9809 	    (entered_recovery == 0)) {
9810 		rack_update_prr(tp, rack, changed, th_ack);
9811 		if ((rsm && (rack->r_ctl.rc_prr_sndcnt >= ctf_fixed_maxseg(tp)) &&
9812 		     ((tcp_in_hpts(rack->rc_inp) == 0) &&
9813 		      ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)))) {
9814 			/*
9815 			 * If you are pacing output you don't want
9816 			 * to override.
9817 			 */
9818 			rack->r_early = 0;
9819 			rack->r_ctl.rc_agg_early = 0;
9820 			rack->r_timer_override = 1;
9821 		}
9822 	}
9823 }
9824 
9825 static void
9826 rack_strike_dupack(struct tcp_rack *rack)
9827 {
9828 	struct rack_sendmap *rsm;
9829 
9830 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
9831 	while (rsm && (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
9832 		rsm = TAILQ_NEXT(rsm, r_tnext);
9833 		if (rsm->r_flags & RACK_MUST_RXT) {
9834 			/* Sendmap entries that are marked to
9835 			 * be retransmitted do not need dupack's
9836 			 * struck. We get these marks for a number
9837 			 * of reasons (rxt timeout with no sack,
9838 			 * mtu change, or rwnd collapses). When
9839 			 * these events occur, we know we must retransmit
9840 			 * them and mark the sendmap entries. Dupack counting
9841 			 * is not needed since we are already set to retransmit
9842 			 * it as soon as we can.
9843 			 */
9844 			continue;
9845 		}
9846 	}
9847 	if (rsm && (rsm->r_dupack < 0xff)) {
9848 		rsm->r_dupack++;
9849 		if (rsm->r_dupack >= DUP_ACK_THRESHOLD) {
9850 			struct timeval tv;
9851 			uint32_t cts;
9852 			/*
9853 			 * Here we see if we need to retransmit. For
9854 			 * a SACK type connection if enough time has passed
9855 			 * we will get a return of the rsm. For a non-sack
9856 			 * connection we will get the rsm returned if the
9857 			 * dupack value is 3 or more.
9858 			 */
9859 			cts = tcp_get_usecs(&tv);
9860 			rack->r_ctl.rc_resend = tcp_rack_output(rack->rc_tp, rack, cts);
9861 			if (rack->r_ctl.rc_resend != NULL) {
9862 				if (!IN_FASTRECOVERY(rack->rc_tp->t_flags)) {
9863 					rack_cong_signal(rack->rc_tp, CC_NDUPACK,
9864 							 rack->rc_tp->snd_una, __LINE__);
9865 				}
9866 				rack->r_wanted_output = 1;
9867 				rack->r_timer_override = 1;
9868 				rack_log_retran_reason(rack, rsm, __LINE__, 1, 3);
9869 			}
9870 		} else {
9871 			rack_log_retran_reason(rack, rsm, __LINE__, 0, 3);
9872 		}
9873 	}
9874 }
9875 
9876 static void
9877 rack_check_bottom_drag(struct tcpcb *tp,
9878 		       struct tcp_rack *rack,
9879 		       struct socket *so, int32_t acked)
9880 {
9881 	uint32_t segsiz, minseg;
9882 
9883 	segsiz = ctf_fixed_maxseg(tp);
9884 	minseg = segsiz;
9885 
9886 	if (tp->snd_max == tp->snd_una) {
9887 		/*
9888 		 * We are doing dynamic pacing and we are way
9889 		 * under. Basically everything got acked while
9890 		 * we were still waiting on the pacer to expire.
9891 		 *
9892 		 * This means we need to boost the b/w in
9893 		 * addition to any earlier boosting of
9894 		 * the multiplier.
9895 		 */
9896 		rack->rc_dragged_bottom = 1;
9897 		rack_validate_multipliers_at_or_above100(rack);
9898 		/*
9899 		 * Lets use the segment bytes acked plus
9900 		 * the lowest RTT seen as the basis to
9901 		 * form a b/w estimate. This will be off
9902 		 * due to the fact that the true estimate
9903 		 * should be around 1/2 the time of the RTT
9904 		 * but we can settle for that.
9905 		 */
9906 		if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_VALID) &&
9907 		    acked) {
9908 			uint64_t bw, calc_bw, rtt;
9909 
9910 			rtt = rack->r_ctl.rack_rs.rs_us_rtt;
9911 			if (rtt == 0) {
9912 				/* no us sample is there a ms one? */
9913 				if (rack->r_ctl.rack_rs.rs_rtt_lowest) {
9914 					rtt = rack->r_ctl.rack_rs.rs_rtt_lowest;
9915 				} else {
9916 					goto no_measurement;
9917 				}
9918 			}
9919 			bw = acked;
9920 			calc_bw = bw * 1000000;
9921 			calc_bw /= rtt;
9922 			if (rack->r_ctl.last_max_bw &&
9923 			    (rack->r_ctl.last_max_bw < calc_bw)) {
9924 				/*
9925 				 * If we have a last calculated max bw
9926 				 * enforce it.
9927 				 */
9928 				calc_bw = rack->r_ctl.last_max_bw;
9929 			}
9930 			/* now plop it in */
9931 			if (rack->rc_gp_filled == 0) {
9932 				if (calc_bw > ONE_POINT_TWO_MEG) {
9933 					/*
9934 					 * If we have no measurement
9935 					 * don't let us set in more than
9936 					 * 1.2Mbps. If we are still too
9937 					 * low after pacing with this we
9938 					 * will hopefully have a max b/w
9939 					 * available to sanity check things.
9940 					 */
9941 					calc_bw = ONE_POINT_TWO_MEG;
9942 				}
9943 				rack->r_ctl.rc_rtt_diff = 0;
9944 				rack->r_ctl.gp_bw = calc_bw;
9945 				rack->rc_gp_filled = 1;
9946 				if (rack->r_ctl.num_measurements < RACK_REQ_AVG)
9947 					rack->r_ctl.num_measurements = RACK_REQ_AVG;
9948 				rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
9949 			} else if (calc_bw > rack->r_ctl.gp_bw) {
9950 				rack->r_ctl.rc_rtt_diff = 0;
9951 				if (rack->r_ctl.num_measurements < RACK_REQ_AVG)
9952 					rack->r_ctl.num_measurements = RACK_REQ_AVG;
9953 				rack->r_ctl.gp_bw = calc_bw;
9954 				rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
9955 			} else
9956 				rack_increase_bw_mul(rack, -1, 0, 0, 1);
9957 			if ((rack->gp_ready == 0) &&
9958 			    (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) {
9959 				/* We have enough measurements now */
9960 				rack->gp_ready = 1;
9961 				rack_set_cc_pacing(rack);
9962 				if (rack->defer_options)
9963 					rack_apply_deferred_options(rack);
9964 			}
9965 			/*
9966 			 * For acks over 1mss we do a extra boost to simulate
9967 			 * where we would get 2 acks (we want 110 for the mul).
9968 			 */
9969 			if (acked > segsiz)
9970 				rack_increase_bw_mul(rack, -1, 0, 0, 1);
9971 		} else {
9972 			/*
9973 			 * zero rtt possibly?, settle for just an old increase.
9974 			 */
9975 no_measurement:
9976 			rack_increase_bw_mul(rack, -1, 0, 0, 1);
9977 		}
9978 	} else if ((IN_FASTRECOVERY(tp->t_flags) == 0) &&
9979 		   (sbavail(&so->so_snd) > max((segsiz * (4 + rack_req_segs)),
9980 					       minseg)) &&
9981 		   (rack->r_ctl.cwnd_to_use > max((segsiz * (rack_req_segs + 2)), minseg)) &&
9982 		   (tp->snd_wnd > max((segsiz * (rack_req_segs + 2)), minseg)) &&
9983 		   (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) <=
9984 		    (segsiz * rack_req_segs))) {
9985 		/*
9986 		 * We are doing dynamic GP pacing and
9987 		 * we have everything except 1MSS or less
9988 		 * bytes left out. We are still pacing away.
9989 		 * And there is data that could be sent, This
9990 		 * means we are inserting delayed ack time in
9991 		 * our measurements because we are pacing too slow.
9992 		 */
9993 		rack_validate_multipliers_at_or_above100(rack);
9994 		rack->rc_dragged_bottom = 1;
9995 		rack_increase_bw_mul(rack, -1, 0, 0, 1);
9996 	}
9997 }
9998 
9999 
10000 
10001 static void
10002 rack_gain_for_fastoutput(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t acked_amount)
10003 {
10004 	/*
10005 	 * The fast output path is enabled and we
10006 	 * have moved the cumack forward. Lets see if
10007 	 * we can expand forward the fast path length by
10008 	 * that amount. What we would ideally like to
10009 	 * do is increase the number of bytes in the
10010 	 * fast path block (left_to_send) by the
10011 	 * acked amount. However we have to gate that
10012 	 * by two factors:
10013 	 * 1) The amount outstanding and the rwnd of the peer
10014 	 *    (i.e. we don't want to exceed the rwnd of the peer).
10015 	 *    <and>
10016 	 * 2) The amount of data left in the socket buffer (i.e.
10017 	 *    we can't send beyond what is in the buffer).
10018 	 *
10019 	 * Note that this does not take into account any increase
10020 	 * in the cwnd. We will only extend the fast path by
10021 	 * what was acked.
10022 	 */
10023 	uint32_t new_total, gating_val;
10024 
10025 	new_total = acked_amount + rack->r_ctl.fsb.left_to_send;
10026 	gating_val = min((sbavail(&so->so_snd) - (tp->snd_max - tp->snd_una)),
10027 			 (tp->snd_wnd - (tp->snd_max - tp->snd_una)));
10028 	if (new_total <= gating_val) {
10029 		/* We can increase left_to_send by the acked amount */
10030 		counter_u64_add(rack_extended_rfo, 1);
10031 		rack->r_ctl.fsb.left_to_send = new_total;
10032 		KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(&rack->rc_inp->inp_socket->so_snd) - (tp->snd_max - tp->snd_una))),
10033 			("rack:%p left_to_send:%u sbavail:%u out:%u",
10034 			 rack, rack->r_ctl.fsb.left_to_send,
10035 			 sbavail(&rack->rc_inp->inp_socket->so_snd),
10036 			 (tp->snd_max - tp->snd_una)));
10037 
10038 	}
10039 }
10040 
10041 static void
10042 rack_adjust_sendmap(struct tcp_rack *rack, struct sockbuf *sb, tcp_seq snd_una)
10043 {
10044 	/*
10045 	 * Here any sendmap entry that points to the
10046 	 * beginning mbuf must be adjusted to the correct
10047 	 * offset. This must be called with:
10048 	 * 1) The socket buffer locked
10049 	 * 2) snd_una adjusted to its new postion.
10050 	 *
10051 	 * Note that (2) implies rack_ack_received has also
10052 	 * been called.
10053 	 *
10054 	 * We grab the first mbuf in the socket buffer and
10055 	 * then go through the front of the sendmap, recalculating
10056 	 * the stored offset for any sendmap entry that has
10057 	 * that mbuf. We must use the sb functions to do this
10058 	 * since its possible an add was done has well as
10059 	 * the subtraction we may have just completed. This should
10060 	 * not be a penalty though, since we just referenced the sb
10061 	 * to go in and trim off the mbufs that we freed (of course
10062 	 * there will be a penalty for the sendmap references though).
10063 	 */
10064 	struct mbuf *m;
10065 	struct rack_sendmap *rsm;
10066 
10067 	SOCKBUF_LOCK_ASSERT(sb);
10068 	m = sb->sb_mb;
10069 	rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
10070 	if ((rsm == NULL) || (m == NULL)) {
10071 		/* Nothing outstanding */
10072 		return;
10073 	}
10074 	while (rsm->m && (rsm->m == m)) {
10075 		/* one to adjust */
10076 #ifdef INVARIANTS
10077 		struct mbuf *tm;
10078 		uint32_t soff;
10079 
10080 		tm = sbsndmbuf(sb, (rsm->r_start - snd_una), &soff);
10081 		if (rsm->orig_m_len != m->m_len) {
10082 			rack_adjust_orig_mlen(rsm);
10083 		}
10084 		if (rsm->soff != soff) {
10085 			/*
10086 			 * This is not a fatal error, we anticipate it
10087 			 * might happen (the else code), so we count it here
10088 			 * so that under invariant we can see that it really
10089 			 * does happen.
10090 			 */
10091 			counter_u64_add(rack_adjust_map_bw, 1);
10092 		}
10093 		rsm->m = tm;
10094 		rsm->soff = soff;
10095 		if (tm)
10096 			rsm->orig_m_len = rsm->m->m_len;
10097 		else
10098 			rsm->orig_m_len = 0;
10099 #else
10100 		rsm->m = sbsndmbuf(sb, (rsm->r_start - snd_una), &rsm->soff);
10101 		if (rsm->m)
10102 			rsm->orig_m_len = rsm->m->m_len;
10103 		else
10104 			rsm->orig_m_len = 0;
10105 #endif
10106 		rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree,
10107 			      rsm);
10108 		if (rsm == NULL)
10109 			break;
10110 	}
10111 }
10112 
10113 /*
10114  * Return value of 1, we do not need to call rack_process_data().
10115  * return value of 0, rack_process_data can be called.
10116  * For ret_val if its 0 the TCP is locked, if its non-zero
10117  * its unlocked and probably unsafe to touch the TCB.
10118  */
10119 static int
10120 rack_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so,
10121     struct tcpcb *tp, struct tcpopt *to,
10122     uint32_t tiwin, int32_t tlen,
10123     int32_t * ofia, int32_t thflags, int32_t *ret_val)
10124 {
10125 	int32_t ourfinisacked = 0;
10126 	int32_t nsegs, acked_amount;
10127 	int32_t acked;
10128 	struct mbuf *mfree;
10129 	struct tcp_rack *rack;
10130 	int32_t under_pacing = 0;
10131 	int32_t recovery = 0;
10132 
10133 	rack = (struct tcp_rack *)tp->t_fb_ptr;
10134 	if (SEQ_GT(th->th_ack, tp->snd_max)) {
10135 		__ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val,
10136 				      &rack->r_ctl.challenge_ack_ts,
10137 				      &rack->r_ctl.challenge_ack_cnt);
10138 		rack->r_wanted_output = 1;
10139 		return (1);
10140 	}
10141 	if (rack->gp_ready &&
10142 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
10143 		under_pacing = 1;
10144 	}
10145 	if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) {
10146 		int in_rec, dup_ack_struck = 0;
10147 
10148 		in_rec = IN_FASTRECOVERY(tp->t_flags);
10149 		if (rack->rc_in_persist) {
10150 			tp->t_rxtshift = 0;
10151 			RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
10152 				      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
10153 		}
10154 		if ((th->th_ack == tp->snd_una) &&
10155 		    (tiwin == tp->snd_wnd) &&
10156 		    ((to->to_flags & TOF_SACK) == 0)) {
10157 			rack_strike_dupack(rack);
10158 			dup_ack_struck = 1;
10159 		}
10160 		rack_log_ack(tp, to, th, ((in_rec == 0) && IN_FASTRECOVERY(tp->t_flags)), dup_ack_struck);
10161 	}
10162 	if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
10163 		/*
10164 		 * Old ack, behind (or duplicate to) the last one rcv'd
10165 		 * Note: We mark reordering is occuring if its
10166 		 * less than and we have not closed our window.
10167 		 */
10168 		if (SEQ_LT(th->th_ack, tp->snd_una) && (sbspace(&so->so_rcv) > ctf_fixed_maxseg(tp))) {
10169 			rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
10170 		}
10171 		return (0);
10172 	}
10173 	/*
10174 	 * If we reach this point, ACK is not a duplicate, i.e., it ACKs
10175 	 * something we sent.
10176 	 */
10177 	if (tp->t_flags & TF_NEEDSYN) {
10178 		/*
10179 		 * T/TCP: Connection was half-synchronized, and our SYN has
10180 		 * been ACK'd (so connection is now fully synchronized).  Go
10181 		 * to non-starred state, increment snd_una for ACK of SYN,
10182 		 * and check if we can do window scaling.
10183 		 */
10184 		tp->t_flags &= ~TF_NEEDSYN;
10185 		tp->snd_una++;
10186 		/* Do window scaling? */
10187 		if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
10188 		    (TF_RCVD_SCALE | TF_REQ_SCALE)) {
10189 			tp->rcv_scale = tp->request_r_scale;
10190 			/* Send window already scaled. */
10191 		}
10192 	}
10193 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
10194 	INP_WLOCK_ASSERT(tp->t_inpcb);
10195 
10196 	acked = BYTES_THIS_ACK(tp, th);
10197 	if (acked) {
10198 		/*
10199 		 * Any time we move the cum-ack forward clear
10200 		 * keep-alive tied probe-not-answered. The
10201 		 * persists clears its own on entry.
10202 		 */
10203 		rack->probe_not_answered = 0;
10204 	}
10205 	KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
10206 	KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
10207 	/*
10208 	 * If we just performed our first retransmit, and the ACK arrives
10209 	 * within our recovery window, then it was a mistake to do the
10210 	 * retransmit in the first place.  Recover our original cwnd and
10211 	 * ssthresh, and proceed to transmit where we left off.
10212 	 */
10213 	if ((tp->t_flags & TF_PREVVALID) &&
10214 	    ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
10215 		tp->t_flags &= ~TF_PREVVALID;
10216 		if (tp->t_rxtshift == 1 &&
10217 		    (int)(ticks - tp->t_badrxtwin) < 0)
10218 			rack_cong_signal(tp, CC_RTO_ERR, th->th_ack, __LINE__);
10219 	}
10220 	if (acked) {
10221 		/* assure we are not backed off */
10222 		tp->t_rxtshift = 0;
10223 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
10224 			      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
10225 		rack->rc_tlp_in_progress = 0;
10226 		rack->r_ctl.rc_tlp_cnt_out = 0;
10227 		/*
10228 		 * If it is the RXT timer we want to
10229 		 * stop it, so we can restart a TLP.
10230 		 */
10231 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
10232 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
10233 #ifdef NETFLIX_HTTP_LOGGING
10234 		tcp_http_check_for_comp(rack->rc_tp, th->th_ack);
10235 #endif
10236 	}
10237 	/*
10238 	 * If we have a timestamp reply, update smoothed round trip time. If
10239 	 * no timestamp is present but transmit timer is running and timed
10240 	 * sequence number was acked, update smoothed round trip time. Since
10241 	 * we now have an rtt measurement, cancel the timer backoff (cf.,
10242 	 * Phil Karn's retransmit alg.). Recompute the initial retransmit
10243 	 * timer.
10244 	 *
10245 	 * Some boxes send broken timestamp replies during the SYN+ACK
10246 	 * phase, ignore timestamps of 0 or we could calculate a huge RTT
10247 	 * and blow up the retransmit timer.
10248 	 */
10249 	/*
10250 	 * If all outstanding data is acked, stop retransmit timer and
10251 	 * remember to restart (more output or persist). If there is more
10252 	 * data to be acked, restart retransmit timer, using current
10253 	 * (possibly backed-off) value.
10254 	 */
10255 	if (acked == 0) {
10256 		if (ofia)
10257 			*ofia = ourfinisacked;
10258 		return (0);
10259 	}
10260 	if (IN_RECOVERY(tp->t_flags)) {
10261 		if (SEQ_LT(th->th_ack, tp->snd_recover) &&
10262 		    (SEQ_LT(th->th_ack, tp->snd_max))) {
10263 			tcp_rack_partialack(tp);
10264 		} else {
10265 			rack_post_recovery(tp, th->th_ack);
10266 			recovery = 1;
10267 		}
10268 	}
10269 	/*
10270 	 * Let the congestion control algorithm update congestion control
10271 	 * related information. This typically means increasing the
10272 	 * congestion window.
10273 	 */
10274 	rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, recovery);
10275 	SOCKBUF_LOCK(&so->so_snd);
10276 	acked_amount = min(acked, (int)sbavail(&so->so_snd));
10277 	tp->snd_wnd -= acked_amount;
10278 	mfree = sbcut_locked(&so->so_snd, acked_amount);
10279 	if ((sbused(&so->so_snd) == 0) &&
10280 	    (acked > acked_amount) &&
10281 	    (tp->t_state >= TCPS_FIN_WAIT_1) &&
10282 	    (tp->t_flags & TF_SENTFIN)) {
10283 		/*
10284 		 * We must be sure our fin
10285 		 * was sent and acked (we can be
10286 		 * in FIN_WAIT_1 without having
10287 		 * sent the fin).
10288 		 */
10289 		ourfinisacked = 1;
10290 	}
10291 	tp->snd_una = th->th_ack;
10292 	if (acked_amount && sbavail(&so->so_snd))
10293 		rack_adjust_sendmap(rack, &so->so_snd, tp->snd_una);
10294 	rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
10295 	/* NB: sowwakeup_locked() does an implicit unlock. */
10296 	sowwakeup_locked(so);
10297 	m_freem(mfree);
10298 	if (SEQ_GT(tp->snd_una, tp->snd_recover))
10299 		tp->snd_recover = tp->snd_una;
10300 
10301 	if (SEQ_LT(tp->snd_nxt, tp->snd_una)) {
10302 		tp->snd_nxt = tp->snd_una;
10303 	}
10304 	if (under_pacing &&
10305 	    (rack->use_fixed_rate == 0) &&
10306 	    (rack->in_probe_rtt == 0) &&
10307 	    rack->rc_gp_dyn_mul &&
10308 	    rack->rc_always_pace) {
10309 		/* Check if we are dragging bottom */
10310 		rack_check_bottom_drag(tp, rack, so, acked);
10311 	}
10312 	if (tp->snd_una == tp->snd_max) {
10313 		/* Nothing left outstanding */
10314 		tp->t_flags &= ~TF_PREVVALID;
10315 		rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
10316 		rack->r_ctl.retran_during_recovery = 0;
10317 		rack->r_ctl.dsack_byte_cnt = 0;
10318 		if (rack->r_ctl.rc_went_idle_time == 0)
10319 			rack->r_ctl.rc_went_idle_time = 1;
10320 		rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
10321 		if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0)
10322 			tp->t_acktime = 0;
10323 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
10324 		/* Set need output so persist might get set */
10325 		rack->r_wanted_output = 1;
10326 		sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
10327 		if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
10328 		    (sbavail(&so->so_snd) == 0) &&
10329 		    (tp->t_flags2 & TF2_DROP_AF_DATA)) {
10330 			/*
10331 			 * The socket was gone and the
10332 			 * peer sent data (now or in the past), time to
10333 			 * reset him.
10334 			 */
10335 			*ret_val = 1;
10336 			/* tcp_close will kill the inp pre-log the Reset */
10337 			tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
10338 			tp = tcp_close(tp);
10339 			ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen);
10340 			return (1);
10341 		}
10342 	}
10343 	if (ofia)
10344 		*ofia = ourfinisacked;
10345 	return (0);
10346 }
10347 
10348 
10349 static void
10350 rack_log_collapse(struct tcp_rack *rack, uint32_t cnt, uint32_t split, uint32_t out, int line,
10351 		  int dir, uint32_t flags, struct rack_sendmap *rsm)
10352 {
10353 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
10354 		union tcp_log_stackspecific log;
10355 		struct timeval tv;
10356 
10357 		memset(&log, 0, sizeof(log));
10358 		log.u_bbr.flex1 = cnt;
10359 		log.u_bbr.flex2 = split;
10360 		log.u_bbr.flex3 = out;
10361 		log.u_bbr.flex4 = line;
10362 		log.u_bbr.flex5 = rack->r_must_retran;
10363 		log.u_bbr.flex6 = flags;
10364 		log.u_bbr.flex7 = rack->rc_has_collapsed;
10365 		log.u_bbr.flex8 = dir;	/*
10366 					 * 1 is collapsed, 0 is uncollapsed,
10367 					 * 2 is log of a rsm being marked, 3 is a split.
10368 					 */
10369 		if (rsm == NULL)
10370 			log.u_bbr.rttProp = 0;
10371 		else
10372 			log.u_bbr.rttProp = (uint64_t)rsm;
10373 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
10374 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
10375 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
10376 		    &rack->rc_inp->inp_socket->so_rcv,
10377 		    &rack->rc_inp->inp_socket->so_snd,
10378 		    TCP_RACK_LOG_COLLAPSE, 0,
10379 		    0, &log, false, &tv);
10380 	}
10381 }
10382 
10383 static void
10384 rack_collapsed_window(struct tcp_rack *rack, uint32_t out, int line)
10385 {
10386 	/*
10387 	 * Here all we do is mark the collapsed point and set the flag.
10388 	 * This may happen again and again, but there is no
10389 	 * sense splitting our map until we know where the
10390 	 * peer finally lands in the collapse.
10391 	 */
10392 	rack_trace_point(rack, RACK_TP_COLLAPSED_WND);
10393 	if ((rack->rc_has_collapsed == 0) ||
10394 	    (rack->r_ctl.last_collapse_point != (rack->rc_tp->snd_una + rack->rc_tp->snd_wnd)))
10395 		counter_u64_add(rack_collapsed_win_seen, 1);
10396 	rack->r_ctl.last_collapse_point = rack->rc_tp->snd_una + rack->rc_tp->snd_wnd;
10397 	rack->r_ctl.high_collapse_point = rack->rc_tp->snd_max;
10398 	rack->rc_has_collapsed = 1;
10399 	rack->r_collapse_point_valid = 1;
10400 	rack_log_collapse(rack, 0, 0, rack->r_ctl.last_collapse_point, line, 1, 0, NULL);
10401 }
10402 
10403 static void
10404 rack_un_collapse_window(struct tcp_rack *rack, int line)
10405 {
10406 	struct rack_sendmap *nrsm, *rsm, fe;
10407 	int cnt = 0, split = 0;
10408 #ifdef INVARIANTS
10409 	struct rack_sendmap *insret;
10410 #endif
10411 
10412 	memset(&fe, 0, sizeof(fe));
10413 	rack->rc_has_collapsed = 0;
10414 	fe.r_start = rack->r_ctl.last_collapse_point;
10415 	rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
10416 	if (rsm == NULL) {
10417 		/* Nothing to do maybe the peer ack'ed it all */
10418 		rack_log_collapse(rack, 0, 0, ctf_outstanding(rack->rc_tp), line, 0, 0, NULL);
10419 		return;
10420 	}
10421 	/* Now do we need to split this one? */
10422 	if (SEQ_GT(rack->r_ctl.last_collapse_point, rsm->r_start)) {
10423 		rack_log_collapse(rack, rsm->r_start, rsm->r_end,
10424 				  rack->r_ctl.last_collapse_point, line, 3, rsm->r_flags, rsm);
10425 		nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
10426 		if (nrsm == NULL) {
10427 			/* We can't get a rsm, mark all? */
10428 			nrsm = rsm;
10429 			goto no_split;
10430 		}
10431 		/* Clone it */
10432 		split = 1;
10433 		rack_clone_rsm(rack, nrsm, rsm, rack->r_ctl.last_collapse_point);
10434 #ifndef INVARIANTS
10435 		(void)RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
10436 #else
10437 		insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
10438 		if (insret != NULL) {
10439 			panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
10440 			      nrsm, insret, rack, rsm);
10441 		}
10442 #endif
10443 		rack_log_map_chg(rack->rc_tp, rack, NULL, rsm, nrsm, MAP_SPLIT,
10444 				 rack->r_ctl.last_collapse_point, __LINE__);
10445 		if (rsm->r_in_tmap) {
10446 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
10447 			nrsm->r_in_tmap = 1;
10448 		}
10449 		/*
10450 		 * Set in the new RSM as the
10451 		 * collapsed starting point
10452 		 */
10453 		rsm = nrsm;
10454 	}
10455 no_split:
10456 	RB_FOREACH_FROM(nrsm, rack_rb_tree_head, rsm) {
10457 		nrsm->r_flags |= RACK_RWND_COLLAPSED;
10458 		rack_log_collapse(rack, nrsm->r_start, nrsm->r_end, 0, line, 4, nrsm->r_flags, nrsm);
10459 		cnt++;
10460 	}
10461 	if (cnt) {
10462 		counter_u64_add(rack_collapsed_win, 1);
10463 	}
10464 	rack_log_collapse(rack, cnt, split, ctf_outstanding(rack->rc_tp), line, 0, 0, NULL);
10465 }
10466 
10467 static void
10468 rack_handle_delayed_ack(struct tcpcb *tp, struct tcp_rack *rack,
10469 			int32_t tlen, int32_t tfo_syn)
10470 {
10471 	if (DELAY_ACK(tp, tlen) || tfo_syn) {
10472 		if (rack->rc_dack_mode &&
10473 		    (tlen > 500) &&
10474 		    (rack->rc_dack_toggle == 1)) {
10475 			goto no_delayed_ack;
10476 		}
10477 		rack_timer_cancel(tp, rack,
10478 				  rack->r_ctl.rc_rcvtime, __LINE__);
10479 		tp->t_flags |= TF_DELACK;
10480 	} else {
10481 no_delayed_ack:
10482 		rack->r_wanted_output = 1;
10483 		tp->t_flags |= TF_ACKNOW;
10484 		if (rack->rc_dack_mode) {
10485 			if (tp->t_flags & TF_DELACK)
10486 				rack->rc_dack_toggle = 1;
10487 			else
10488 				rack->rc_dack_toggle = 0;
10489 		}
10490 	}
10491 }
10492 
10493 static void
10494 rack_validate_fo_sendwin_up(struct tcpcb *tp, struct tcp_rack *rack)
10495 {
10496 	/*
10497 	 * If fast output is in progress, lets validate that
10498 	 * the new window did not shrink on us and make it
10499 	 * so fast output should end.
10500 	 */
10501 	if (rack->r_fast_output) {
10502 		uint32_t out;
10503 
10504 		/*
10505 		 * Calculate what we will send if left as is
10506 		 * and compare that to our send window.
10507 		 */
10508 		out = ctf_outstanding(tp);
10509 		if ((out + rack->r_ctl.fsb.left_to_send) > tp->snd_wnd) {
10510 			/* ok we have an issue */
10511 			if (out >= tp->snd_wnd) {
10512 				/* Turn off fast output the window is met or collapsed */
10513 				rack->r_fast_output = 0;
10514 			} else {
10515 				/* we have some room left */
10516 				rack->r_ctl.fsb.left_to_send = tp->snd_wnd - out;
10517 				if (rack->r_ctl.fsb.left_to_send < ctf_fixed_maxseg(tp)) {
10518 					/* If not at least 1 full segment never mind */
10519 					rack->r_fast_output = 0;
10520 				}
10521 			}
10522 		}
10523 	}
10524 }
10525 
10526 
10527 /*
10528  * Return value of 1, the TCB is unlocked and most
10529  * likely gone, return value of 0, the TCP is still
10530  * locked.
10531  */
10532 static int
10533 rack_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so,
10534     struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
10535     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
10536 {
10537 	/*
10538 	 * Update window information. Don't look at window if no ACK: TAC's
10539 	 * send garbage on first SYN.
10540 	 */
10541 	int32_t nsegs;
10542 	int32_t tfo_syn;
10543 	struct tcp_rack *rack;
10544 
10545 	rack = (struct tcp_rack *)tp->t_fb_ptr;
10546 	INP_WLOCK_ASSERT(tp->t_inpcb);
10547 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
10548 	if ((thflags & TH_ACK) &&
10549 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
10550 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
10551 	    (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
10552 		/* keep track of pure window updates */
10553 		if (tlen == 0 &&
10554 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
10555 			KMOD_TCPSTAT_INC(tcps_rcvwinupd);
10556 		tp->snd_wnd = tiwin;
10557 		rack_validate_fo_sendwin_up(tp, rack);
10558 		tp->snd_wl1 = th->th_seq;
10559 		tp->snd_wl2 = th->th_ack;
10560 		if (tp->snd_wnd > tp->max_sndwnd)
10561 			tp->max_sndwnd = tp->snd_wnd;
10562 		rack->r_wanted_output = 1;
10563 	} else if (thflags & TH_ACK) {
10564 		if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) {
10565 			tp->snd_wnd = tiwin;
10566 			rack_validate_fo_sendwin_up(tp, rack);
10567 			tp->snd_wl1 = th->th_seq;
10568 			tp->snd_wl2 = th->th_ack;
10569 		}
10570 	}
10571 	if (tp->snd_wnd < ctf_outstanding(tp))
10572 		/* The peer collapsed the window */
10573 		rack_collapsed_window(rack, ctf_outstanding(tp), __LINE__);
10574 	else if (rack->rc_has_collapsed)
10575 		rack_un_collapse_window(rack, __LINE__);
10576 	if ((rack->r_collapse_point_valid) &&
10577 	    (SEQ_GT(th->th_ack, rack->r_ctl.high_collapse_point)))
10578 		rack->r_collapse_point_valid = 0;
10579 	/* Was persist timer active and now we have window space? */
10580 	if ((rack->rc_in_persist != 0) &&
10581 	    (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
10582 				rack->r_ctl.rc_pace_min_segs))) {
10583 		rack_exit_persist(tp, rack, rack->r_ctl.rc_rcvtime);
10584 		tp->snd_nxt = tp->snd_max;
10585 		/* Make sure we output to start the timer */
10586 		rack->r_wanted_output = 1;
10587 	}
10588 	/* Do we enter persists? */
10589 	if ((rack->rc_in_persist == 0) &&
10590 	    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
10591 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
10592 	    ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) &&
10593 	    sbavail(&tp->t_inpcb->inp_socket->so_snd) &&
10594 	    (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) {
10595 		/*
10596 		 * Here the rwnd is less than
10597 		 * the pacing size, we are established,
10598 		 * nothing is outstanding, and there is
10599 		 * data to send. Enter persists.
10600 		 */
10601 		rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime);
10602 	}
10603 	if (tp->t_flags2 & TF2_DROP_AF_DATA) {
10604 		m_freem(m);
10605 		return (0);
10606 	}
10607 	/*
10608 	 * don't process the URG bit, ignore them drag
10609 	 * along the up.
10610 	 */
10611 	tp->rcv_up = tp->rcv_nxt;
10612 	INP_WLOCK_ASSERT(tp->t_inpcb);
10613 
10614 	/*
10615 	 * Process the segment text, merging it into the TCP sequencing
10616 	 * queue, and arranging for acknowledgment of receipt if necessary.
10617 	 * This process logically involves adjusting tp->rcv_wnd as data is
10618 	 * presented to the user (this happens in tcp_usrreq.c, case
10619 	 * PRU_RCVD).  If a FIN has already been received on this connection
10620 	 * then we just ignore the text.
10621 	 */
10622 	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
10623 		   IS_FASTOPEN(tp->t_flags));
10624 	if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
10625 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
10626 		tcp_seq save_start = th->th_seq;
10627 		tcp_seq save_rnxt  = tp->rcv_nxt;
10628 		int     save_tlen  = tlen;
10629 
10630 		m_adj(m, drop_hdrlen);	/* delayed header drop */
10631 		/*
10632 		 * Insert segment which includes th into TCP reassembly
10633 		 * queue with control block tp.  Set thflags to whether
10634 		 * reassembly now includes a segment with FIN.  This handles
10635 		 * the common case inline (segment is the next to be
10636 		 * received on an established connection, and the queue is
10637 		 * empty), avoiding linkage into and removal from the queue
10638 		 * and repetition of various conversions. Set DELACK for
10639 		 * segments received in order, but ack immediately when
10640 		 * segments are out of order (so fast retransmit can work).
10641 		 */
10642 		if (th->th_seq == tp->rcv_nxt &&
10643 		    SEGQ_EMPTY(tp) &&
10644 		    (TCPS_HAVEESTABLISHED(tp->t_state) ||
10645 		    tfo_syn)) {
10646 #ifdef NETFLIX_SB_LIMITS
10647 			u_int mcnt, appended;
10648 
10649 			if (so->so_rcv.sb_shlim) {
10650 				mcnt = m_memcnt(m);
10651 				appended = 0;
10652 				if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
10653 				    CFO_NOSLEEP, NULL) == false) {
10654 					counter_u64_add(tcp_sb_shlim_fails, 1);
10655 					m_freem(m);
10656 					return (0);
10657 				}
10658 			}
10659 #endif
10660 			rack_handle_delayed_ack(tp, rack, tlen, tfo_syn);
10661 			tp->rcv_nxt += tlen;
10662 			if (tlen &&
10663 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
10664 			    (tp->t_fbyte_in == 0)) {
10665 				tp->t_fbyte_in = ticks;
10666 				if (tp->t_fbyte_in == 0)
10667 					tp->t_fbyte_in = 1;
10668 				if (tp->t_fbyte_out && tp->t_fbyte_in)
10669 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
10670 			}
10671 			thflags = tcp_get_flags(th) & TH_FIN;
10672 			KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs);
10673 			KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
10674 			SOCKBUF_LOCK(&so->so_rcv);
10675 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
10676 				m_freem(m);
10677 			} else
10678 #ifdef NETFLIX_SB_LIMITS
10679 				appended =
10680 #endif
10681 					sbappendstream_locked(&so->so_rcv, m, 0);
10682 
10683 			rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1);
10684 			/* NB: sorwakeup_locked() does an implicit unlock. */
10685 			sorwakeup_locked(so);
10686 #ifdef NETFLIX_SB_LIMITS
10687 			if (so->so_rcv.sb_shlim && appended != mcnt)
10688 				counter_fo_release(so->so_rcv.sb_shlim,
10689 				    mcnt - appended);
10690 #endif
10691 		} else {
10692 			/*
10693 			 * XXX: Due to the header drop above "th" is
10694 			 * theoretically invalid by now.  Fortunately
10695 			 * m_adj() doesn't actually frees any mbufs when
10696 			 * trimming from the head.
10697 			 */
10698 			tcp_seq temp = save_start;
10699 
10700 			thflags = tcp_reass(tp, th, &temp, &tlen, m);
10701 			tp->t_flags |= TF_ACKNOW;
10702 			if (tp->t_flags & TF_WAKESOR) {
10703 				tp->t_flags &= ~TF_WAKESOR;
10704 				/* NB: sorwakeup_locked() does an implicit unlock. */
10705 				sorwakeup_locked(so);
10706 			}
10707 		}
10708 		if ((tp->t_flags & TF_SACK_PERMIT) &&
10709 		    (save_tlen > 0) &&
10710 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
10711 			if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
10712 				/*
10713 				 * DSACK actually handled in the fastpath
10714 				 * above.
10715 				 */
10716 				RACK_OPTS_INC(tcp_sack_path_1);
10717 				tcp_update_sack_list(tp, save_start,
10718 				    save_start + save_tlen);
10719 			} else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
10720 				if ((tp->rcv_numsacks >= 1) &&
10721 				    (tp->sackblks[0].end == save_start)) {
10722 					/*
10723 					 * Partial overlap, recorded at todrop
10724 					 * above.
10725 					 */
10726 					RACK_OPTS_INC(tcp_sack_path_2a);
10727 					tcp_update_sack_list(tp,
10728 					    tp->sackblks[0].start,
10729 					    tp->sackblks[0].end);
10730 				} else {
10731 					RACK_OPTS_INC(tcp_sack_path_2b);
10732 					tcp_update_dsack_list(tp, save_start,
10733 					    save_start + save_tlen);
10734 				}
10735 			} else if (tlen >= save_tlen) {
10736 				/* Update of sackblks. */
10737 				RACK_OPTS_INC(tcp_sack_path_3);
10738 				tcp_update_dsack_list(tp, save_start,
10739 				    save_start + save_tlen);
10740 			} else if (tlen > 0) {
10741 				RACK_OPTS_INC(tcp_sack_path_4);
10742 				tcp_update_dsack_list(tp, save_start,
10743 				    save_start + tlen);
10744 			}
10745 		}
10746 	} else {
10747 		m_freem(m);
10748 		thflags &= ~TH_FIN;
10749 	}
10750 
10751 	/*
10752 	 * If FIN is received ACK the FIN and let the user know that the
10753 	 * connection is closing.
10754 	 */
10755 	if (thflags & TH_FIN) {
10756 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
10757 			/* The socket upcall is handled by socantrcvmore. */
10758 			socantrcvmore(so);
10759 			/*
10760 			 * If connection is half-synchronized (ie NEEDSYN
10761 			 * flag on) then delay ACK, so it may be piggybacked
10762 			 * when SYN is sent. Otherwise, since we received a
10763 			 * FIN then no more input can be expected, send ACK
10764 			 * now.
10765 			 */
10766 			if (tp->t_flags & TF_NEEDSYN) {
10767 				rack_timer_cancel(tp, rack,
10768 				    rack->r_ctl.rc_rcvtime, __LINE__);
10769 				tp->t_flags |= TF_DELACK;
10770 			} else {
10771 				tp->t_flags |= TF_ACKNOW;
10772 			}
10773 			tp->rcv_nxt++;
10774 		}
10775 		switch (tp->t_state) {
10776 			/*
10777 			 * In SYN_RECEIVED and ESTABLISHED STATES enter the
10778 			 * CLOSE_WAIT state.
10779 			 */
10780 		case TCPS_SYN_RECEIVED:
10781 			tp->t_starttime = ticks;
10782 			/* FALLTHROUGH */
10783 		case TCPS_ESTABLISHED:
10784 			rack_timer_cancel(tp, rack,
10785 			    rack->r_ctl.rc_rcvtime, __LINE__);
10786 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
10787 			break;
10788 
10789 			/*
10790 			 * If still in FIN_WAIT_1 STATE FIN has not been
10791 			 * acked so enter the CLOSING state.
10792 			 */
10793 		case TCPS_FIN_WAIT_1:
10794 			rack_timer_cancel(tp, rack,
10795 			    rack->r_ctl.rc_rcvtime, __LINE__);
10796 			tcp_state_change(tp, TCPS_CLOSING);
10797 			break;
10798 
10799 			/*
10800 			 * In FIN_WAIT_2 state enter the TIME_WAIT state,
10801 			 * starting the time-wait timer, turning off the
10802 			 * other standard timers.
10803 			 */
10804 		case TCPS_FIN_WAIT_2:
10805 			rack_timer_cancel(tp, rack,
10806 			    rack->r_ctl.rc_rcvtime, __LINE__);
10807 			tcp_twstart(tp);
10808 			return (1);
10809 		}
10810 	}
10811 	/*
10812 	 * Return any desired output.
10813 	 */
10814 	if ((tp->t_flags & TF_ACKNOW) ||
10815 	    (sbavail(&so->so_snd) > (tp->snd_max - tp->snd_una))) {
10816 		rack->r_wanted_output = 1;
10817 	}
10818 	INP_WLOCK_ASSERT(tp->t_inpcb);
10819 	return (0);
10820 }
10821 
10822 /*
10823  * Here nothing is really faster, its just that we
10824  * have broken out the fast-data path also just like
10825  * the fast-ack.
10826  */
10827 static int
10828 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so,
10829     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
10830     uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos)
10831 {
10832 	int32_t nsegs;
10833 	int32_t newsize = 0;	/* automatic sockbuf scaling */
10834 	struct tcp_rack *rack;
10835 #ifdef NETFLIX_SB_LIMITS
10836 	u_int mcnt, appended;
10837 #endif
10838 #ifdef TCPDEBUG
10839 	/*
10840 	 * The size of tcp_saveipgen must be the size of the max ip header,
10841 	 * now IPv6.
10842 	 */
10843 	u_char tcp_saveipgen[IP6_HDR_LEN];
10844 	struct tcphdr tcp_savetcp;
10845 	short ostate = 0;
10846 
10847 #endif
10848 	/*
10849 	 * If last ACK falls within this segment's sequence numbers, record
10850 	 * the timestamp. NOTE that the test is modified according to the
10851 	 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
10852 	 */
10853 	if (__predict_false(th->th_seq != tp->rcv_nxt)) {
10854 		return (0);
10855 	}
10856 	if (__predict_false(tp->snd_nxt != tp->snd_max)) {
10857 		return (0);
10858 	}
10859 	if (tiwin && tiwin != tp->snd_wnd) {
10860 		return (0);
10861 	}
10862 	if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) {
10863 		return (0);
10864 	}
10865 	if (__predict_false((to->to_flags & TOF_TS) &&
10866 	    (TSTMP_LT(to->to_tsval, tp->ts_recent)))) {
10867 		return (0);
10868 	}
10869 	if (__predict_false((th->th_ack != tp->snd_una))) {
10870 		return (0);
10871 	}
10872 	if (__predict_false(tlen > sbspace(&so->so_rcv))) {
10873 		return (0);
10874 	}
10875 	if ((to->to_flags & TOF_TS) != 0 &&
10876 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
10877 		tp->ts_recent_age = tcp_ts_getticks();
10878 		tp->ts_recent = to->to_tsval;
10879 	}
10880 	rack = (struct tcp_rack *)tp->t_fb_ptr;
10881 	/*
10882 	 * This is a pure, in-sequence data packet with nothing on the
10883 	 * reassembly queue and we have enough buffer space to take it.
10884 	 */
10885 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
10886 
10887 #ifdef NETFLIX_SB_LIMITS
10888 	if (so->so_rcv.sb_shlim) {
10889 		mcnt = m_memcnt(m);
10890 		appended = 0;
10891 		if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
10892 		    CFO_NOSLEEP, NULL) == false) {
10893 			counter_u64_add(tcp_sb_shlim_fails, 1);
10894 			m_freem(m);
10895 			return (1);
10896 		}
10897 	}
10898 #endif
10899 	/* Clean receiver SACK report if present */
10900 	if (tp->rcv_numsacks)
10901 		tcp_clean_sackreport(tp);
10902 	KMOD_TCPSTAT_INC(tcps_preddat);
10903 	tp->rcv_nxt += tlen;
10904 	if (tlen &&
10905 	    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
10906 	    (tp->t_fbyte_in == 0)) {
10907 		tp->t_fbyte_in = ticks;
10908 		if (tp->t_fbyte_in == 0)
10909 			tp->t_fbyte_in = 1;
10910 		if (tp->t_fbyte_out && tp->t_fbyte_in)
10911 			tp->t_flags2 |= TF2_FBYTES_COMPLETE;
10912 	}
10913 	/*
10914 	 * Pull snd_wl1 up to prevent seq wrap relative to th_seq.
10915 	 */
10916 	tp->snd_wl1 = th->th_seq;
10917 	/*
10918 	 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt.
10919 	 */
10920 	tp->rcv_up = tp->rcv_nxt;
10921 	KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs);
10922 	KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
10923 #ifdef TCPDEBUG
10924 	if (so->so_options & SO_DEBUG)
10925 		tcp_trace(TA_INPUT, ostate, tp,
10926 		    (void *)tcp_saveipgen, &tcp_savetcp, 0);
10927 #endif
10928 	newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
10929 
10930 	/* Add data to socket buffer. */
10931 	SOCKBUF_LOCK(&so->so_rcv);
10932 	if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
10933 		m_freem(m);
10934 	} else {
10935 		/*
10936 		 * Set new socket buffer size. Give up when limit is
10937 		 * reached.
10938 		 */
10939 		if (newsize)
10940 			if (!sbreserve_locked(so, SO_RCV, newsize, NULL))
10941 				so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
10942 		m_adj(m, drop_hdrlen);	/* delayed header drop */
10943 #ifdef NETFLIX_SB_LIMITS
10944 		appended =
10945 #endif
10946 			sbappendstream_locked(&so->so_rcv, m, 0);
10947 		ctf_calc_rwin(so, tp);
10948 	}
10949 	rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1);
10950 	/* NB: sorwakeup_locked() does an implicit unlock. */
10951 	sorwakeup_locked(so);
10952 #ifdef NETFLIX_SB_LIMITS
10953 	if (so->so_rcv.sb_shlim && mcnt != appended)
10954 		counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended);
10955 #endif
10956 	rack_handle_delayed_ack(tp, rack, tlen, 0);
10957 	if (tp->snd_una == tp->snd_max)
10958 		sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
10959 	return (1);
10960 }
10961 
10962 /*
10963  * This subfunction is used to try to highly optimize the
10964  * fast path. We again allow window updates that are
10965  * in sequence to remain in the fast-path. We also add
10966  * in the __predict's to attempt to help the compiler.
10967  * Note that if we return a 0, then we can *not* process
10968  * it and the caller should push the packet into the
10969  * slow-path.
10970  */
10971 static int
10972 rack_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
10973     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
10974     uint32_t tiwin, int32_t nxt_pkt, uint32_t cts)
10975 {
10976 	int32_t acked;
10977 	int32_t nsegs;
10978 #ifdef TCPDEBUG
10979 	/*
10980 	 * The size of tcp_saveipgen must be the size of the max ip header,
10981 	 * now IPv6.
10982 	 */
10983 	u_char tcp_saveipgen[IP6_HDR_LEN];
10984 	struct tcphdr tcp_savetcp;
10985 	short ostate = 0;
10986 #endif
10987 	int32_t under_pacing = 0;
10988 	struct tcp_rack *rack;
10989 
10990 	if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
10991 		/* Old ack, behind (or duplicate to) the last one rcv'd */
10992 		return (0);
10993 	}
10994 	if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) {
10995 		/* Above what we have sent? */
10996 		return (0);
10997 	}
10998 	if (__predict_false(tp->snd_nxt != tp->snd_max)) {
10999 		/* We are retransmitting */
11000 		return (0);
11001 	}
11002 	if (__predict_false(tiwin == 0)) {
11003 		/* zero window */
11004 		return (0);
11005 	}
11006 	if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) {
11007 		/* We need a SYN or a FIN, unlikely.. */
11008 		return (0);
11009 	}
11010 	if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) {
11011 		/* Timestamp is behind .. old ack with seq wrap? */
11012 		return (0);
11013 	}
11014 	if (__predict_false(IN_RECOVERY(tp->t_flags))) {
11015 		/* Still recovering */
11016 		return (0);
11017 	}
11018 	rack = (struct tcp_rack *)tp->t_fb_ptr;
11019 	if (rack->r_ctl.rc_sacked) {
11020 		/* We have sack holes on our scoreboard */
11021 		return (0);
11022 	}
11023 	/* Ok if we reach here, we can process a fast-ack */
11024 	if (rack->gp_ready &&
11025 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
11026 		under_pacing = 1;
11027 	}
11028 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
11029 	rack_log_ack(tp, to, th, 0, 0);
11030 	/* Did the window get updated? */
11031 	if (tiwin != tp->snd_wnd) {
11032 		tp->snd_wnd = tiwin;
11033 		rack_validate_fo_sendwin_up(tp, rack);
11034 		tp->snd_wl1 = th->th_seq;
11035 		if (tp->snd_wnd > tp->max_sndwnd)
11036 			tp->max_sndwnd = tp->snd_wnd;
11037 	}
11038 	/* Do we exit persists? */
11039 	if ((rack->rc_in_persist != 0) &&
11040 	    (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
11041 			       rack->r_ctl.rc_pace_min_segs))) {
11042 		rack_exit_persist(tp, rack, cts);
11043 	}
11044 	/* Do we enter persists? */
11045 	if ((rack->rc_in_persist == 0) &&
11046 	    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
11047 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
11048 	    ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) &&
11049 	    sbavail(&tp->t_inpcb->inp_socket->so_snd) &&
11050 	    (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) {
11051 		/*
11052 		 * Here the rwnd is less than
11053 		 * the pacing size, we are established,
11054 		 * nothing is outstanding, and there is
11055 		 * data to send. Enter persists.
11056 		 */
11057 		rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime);
11058 	}
11059 	/*
11060 	 * If last ACK falls within this segment's sequence numbers, record
11061 	 * the timestamp. NOTE that the test is modified according to the
11062 	 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
11063 	 */
11064 	if ((to->to_flags & TOF_TS) != 0 &&
11065 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
11066 		tp->ts_recent_age = tcp_ts_getticks();
11067 		tp->ts_recent = to->to_tsval;
11068 	}
11069 	/*
11070 	 * This is a pure ack for outstanding data.
11071 	 */
11072 	KMOD_TCPSTAT_INC(tcps_predack);
11073 
11074 	/*
11075 	 * "bad retransmit" recovery.
11076 	 */
11077 	if ((tp->t_flags & TF_PREVVALID) &&
11078 	    ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
11079 		tp->t_flags &= ~TF_PREVVALID;
11080 		if (tp->t_rxtshift == 1 &&
11081 		    (int)(ticks - tp->t_badrxtwin) < 0)
11082 			rack_cong_signal(tp, CC_RTO_ERR, th->th_ack, __LINE__);
11083 	}
11084 	/*
11085 	 * Recalculate the transmit timer / rtt.
11086 	 *
11087 	 * Some boxes send broken timestamp replies during the SYN+ACK
11088 	 * phase, ignore timestamps of 0 or we could calculate a huge RTT
11089 	 * and blow up the retransmit timer.
11090 	 */
11091 	acked = BYTES_THIS_ACK(tp, th);
11092 
11093 #ifdef TCP_HHOOK
11094 	/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
11095 	hhook_run_tcp_est_in(tp, th, to);
11096 #endif
11097 	KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
11098 	KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
11099 	if (acked) {
11100 		struct mbuf *mfree;
11101 
11102 		rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, 0);
11103 		SOCKBUF_LOCK(&so->so_snd);
11104 		mfree = sbcut_locked(&so->so_snd, acked);
11105 		tp->snd_una = th->th_ack;
11106 		/* Note we want to hold the sb lock through the sendmap adjust */
11107 		rack_adjust_sendmap(rack, &so->so_snd, tp->snd_una);
11108 		/* Wake up the socket if we have room to write more */
11109 		rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
11110 		sowwakeup_locked(so);
11111 		m_freem(mfree);
11112 		tp->t_rxtshift = 0;
11113 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
11114 			      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
11115 		rack->rc_tlp_in_progress = 0;
11116 		rack->r_ctl.rc_tlp_cnt_out = 0;
11117 		/*
11118 		 * If it is the RXT timer we want to
11119 		 * stop it, so we can restart a TLP.
11120 		 */
11121 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
11122 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
11123 #ifdef NETFLIX_HTTP_LOGGING
11124 		tcp_http_check_for_comp(rack->rc_tp, th->th_ack);
11125 #endif
11126 	}
11127 	/*
11128 	 * Let the congestion control algorithm update congestion control
11129 	 * related information. This typically means increasing the
11130 	 * congestion window.
11131 	 */
11132 	if (tp->snd_wnd < ctf_outstanding(tp)) {
11133 		/* The peer collapsed the window */
11134 		rack_collapsed_window(rack, ctf_outstanding(tp), __LINE__);
11135 	} else if (rack->rc_has_collapsed)
11136 		rack_un_collapse_window(rack, __LINE__);
11137 	if ((rack->r_collapse_point_valid) &&
11138 	    (SEQ_GT(tp->snd_una, rack->r_ctl.high_collapse_point)))
11139 		rack->r_collapse_point_valid = 0;
11140 	/*
11141 	 * Pull snd_wl2 up to prevent seq wrap relative to th_ack.
11142 	 */
11143 	tp->snd_wl2 = th->th_ack;
11144 	tp->t_dupacks = 0;
11145 	m_freem(m);
11146 	/* ND6_HINT(tp);	 *//* Some progress has been made. */
11147 
11148 	/*
11149 	 * If all outstanding data are acked, stop retransmit timer,
11150 	 * otherwise restart timer using current (possibly backed-off)
11151 	 * value. If process is waiting for space, wakeup/selwakeup/signal.
11152 	 * If data are ready to send, let tcp_output decide between more
11153 	 * output or persist.
11154 	 */
11155 #ifdef TCPDEBUG
11156 	if (so->so_options & SO_DEBUG)
11157 		tcp_trace(TA_INPUT, ostate, tp,
11158 		    (void *)tcp_saveipgen,
11159 		    &tcp_savetcp, 0);
11160 #endif
11161 	if (under_pacing &&
11162 	    (rack->use_fixed_rate == 0) &&
11163 	    (rack->in_probe_rtt == 0) &&
11164 	    rack->rc_gp_dyn_mul &&
11165 	    rack->rc_always_pace) {
11166 		/* Check if we are dragging bottom */
11167 		rack_check_bottom_drag(tp, rack, so, acked);
11168 	}
11169 	if (tp->snd_una == tp->snd_max) {
11170 		tp->t_flags &= ~TF_PREVVALID;
11171 		rack->r_ctl.retran_during_recovery = 0;
11172 		rack->r_ctl.dsack_byte_cnt = 0;
11173 		rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
11174 		if (rack->r_ctl.rc_went_idle_time == 0)
11175 			rack->r_ctl.rc_went_idle_time = 1;
11176 		rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
11177 		if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0)
11178 			tp->t_acktime = 0;
11179 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
11180 	}
11181 	if (acked && rack->r_fast_output)
11182 		rack_gain_for_fastoutput(rack, tp, so, (uint32_t)acked);
11183 	if (sbavail(&so->so_snd)) {
11184 		rack->r_wanted_output = 1;
11185 	}
11186 	return (1);
11187 }
11188 
11189 /*
11190  * Return value of 1, the TCB is unlocked and most
11191  * likely gone, return value of 0, the TCP is still
11192  * locked.
11193  */
11194 static int
11195 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so,
11196     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
11197     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
11198 {
11199 	int32_t ret_val = 0;
11200 	int32_t todrop;
11201 	int32_t ourfinisacked = 0;
11202 	struct tcp_rack *rack;
11203 
11204 	ctf_calc_rwin(so, tp);
11205 	/*
11206 	 * If the state is SYN_SENT: if seg contains an ACK, but not for our
11207 	 * SYN, drop the input. if seg contains a RST, then drop the
11208 	 * connection. if seg does not contain SYN, then drop it. Otherwise
11209 	 * this is an acceptable SYN segment initialize tp->rcv_nxt and
11210 	 * tp->irs if seg contains ack then advance tp->snd_una if seg
11211 	 * contains an ECE and ECN support is enabled, the stream is ECN
11212 	 * capable. if SYN has been acked change to ESTABLISHED else
11213 	 * SYN_RCVD state arrange for segment to be acked (eventually)
11214 	 * continue processing rest of data/controls.
11215 	 */
11216 	if ((thflags & TH_ACK) &&
11217 	    (SEQ_LEQ(th->th_ack, tp->iss) ||
11218 	    SEQ_GT(th->th_ack, tp->snd_max))) {
11219 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
11220 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11221 		return (1);
11222 	}
11223 	if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) {
11224 		TCP_PROBE5(connect__refused, NULL, tp,
11225 		    mtod(m, const char *), tp, th);
11226 		tp = tcp_drop(tp, ECONNREFUSED);
11227 		ctf_do_drop(m, tp);
11228 		return (1);
11229 	}
11230 	if (thflags & TH_RST) {
11231 		ctf_do_drop(m, tp);
11232 		return (1);
11233 	}
11234 	if (!(thflags & TH_SYN)) {
11235 		ctf_do_drop(m, tp);
11236 		return (1);
11237 	}
11238 	tp->irs = th->th_seq;
11239 	tcp_rcvseqinit(tp);
11240 	rack = (struct tcp_rack *)tp->t_fb_ptr;
11241 	if (thflags & TH_ACK) {
11242 		int tfo_partial = 0;
11243 
11244 		KMOD_TCPSTAT_INC(tcps_connects);
11245 		soisconnected(so);
11246 #ifdef MAC
11247 		mac_socketpeer_set_from_mbuf(m, so);
11248 #endif
11249 		/* Do window scaling on this connection? */
11250 		if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
11251 		    (TF_RCVD_SCALE | TF_REQ_SCALE)) {
11252 			tp->rcv_scale = tp->request_r_scale;
11253 		}
11254 		tp->rcv_adv += min(tp->rcv_wnd,
11255 		    TCP_MAXWIN << tp->rcv_scale);
11256 		/*
11257 		 * If not all the data that was sent in the TFO SYN
11258 		 * has been acked, resend the remainder right away.
11259 		 */
11260 		if (IS_FASTOPEN(tp->t_flags) &&
11261 		    (tp->snd_una != tp->snd_max)) {
11262 			tp->snd_nxt = th->th_ack;
11263 			tfo_partial = 1;
11264 		}
11265 		/*
11266 		 * If there's data, delay ACK; if there's also a FIN ACKNOW
11267 		 * will be turned on later.
11268 		 */
11269 		if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial) {
11270 			rack_timer_cancel(tp, rack,
11271 					  rack->r_ctl.rc_rcvtime, __LINE__);
11272 			tp->t_flags |= TF_DELACK;
11273 		} else {
11274 			rack->r_wanted_output = 1;
11275 			tp->t_flags |= TF_ACKNOW;
11276 			rack->rc_dack_toggle = 0;
11277 		}
11278 
11279 		tcp_ecn_input_syn_sent(tp, thflags, iptos);
11280 
11281 		if (SEQ_GT(th->th_ack, tp->snd_una)) {
11282 			/*
11283 			 * We advance snd_una for the
11284 			 * fast open case. If th_ack is
11285 			 * acknowledging data beyond
11286 			 * snd_una we can't just call
11287 			 * ack-processing since the
11288 			 * data stream in our send-map
11289 			 * will start at snd_una + 1 (one
11290 			 * beyond the SYN). If its just
11291 			 * equal we don't need to do that
11292 			 * and there is no send_map.
11293 			 */
11294 			tp->snd_una++;
11295 		}
11296 		/*
11297 		 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions:
11298 		 * SYN_SENT  --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1
11299 		 */
11300 		tp->t_starttime = ticks;
11301 		if (tp->t_flags & TF_NEEDFIN) {
11302 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
11303 			tp->t_flags &= ~TF_NEEDFIN;
11304 			thflags &= ~TH_SYN;
11305 		} else {
11306 			tcp_state_change(tp, TCPS_ESTABLISHED);
11307 			TCP_PROBE5(connect__established, NULL, tp,
11308 			    mtod(m, const char *), tp, th);
11309 			rack_cc_conn_init(tp);
11310 		}
11311 	} else {
11312 		/*
11313 		 * Received initial SYN in SYN-SENT[*] state => simultaneous
11314 		 * open.  If segment contains CC option and there is a
11315 		 * cached CC, apply TAO test. If it succeeds, connection is *
11316 		 * half-synchronized. Otherwise, do 3-way handshake:
11317 		 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If
11318 		 * there was no CC option, clear cached CC value.
11319 		 */
11320 		tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
11321 		tcp_state_change(tp, TCPS_SYN_RECEIVED);
11322 	}
11323 	INP_WLOCK_ASSERT(tp->t_inpcb);
11324 	/*
11325 	 * Advance th->th_seq to correspond to first data byte. If data,
11326 	 * trim to stay within window, dropping FIN if necessary.
11327 	 */
11328 	th->th_seq++;
11329 	if (tlen > tp->rcv_wnd) {
11330 		todrop = tlen - tp->rcv_wnd;
11331 		m_adj(m, -todrop);
11332 		tlen = tp->rcv_wnd;
11333 		thflags &= ~TH_FIN;
11334 		KMOD_TCPSTAT_INC(tcps_rcvpackafterwin);
11335 		KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
11336 	}
11337 	tp->snd_wl1 = th->th_seq - 1;
11338 	tp->rcv_up = th->th_seq;
11339 	/*
11340 	 * Client side of transaction: already sent SYN and data. If the
11341 	 * remote host used T/TCP to validate the SYN, our data will be
11342 	 * ACK'd; if so, enter normal data segment processing in the middle
11343 	 * of step 5, ack processing. Otherwise, goto step 6.
11344 	 */
11345 	if (thflags & TH_ACK) {
11346 		/* For syn-sent we need to possibly update the rtt */
11347 		if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) {
11348 			uint32_t t, mcts;
11349 
11350 			mcts = tcp_ts_getticks();
11351 			t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC;
11352 			if (!tp->t_rttlow || tp->t_rttlow > t)
11353 				tp->t_rttlow = t;
11354 			rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 4);
11355 			tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2);
11356 			tcp_rack_xmit_timer_commit(rack, tp);
11357 		}
11358 		if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val))
11359 			return (ret_val);
11360 		/* We may have changed to FIN_WAIT_1 above */
11361 		if (tp->t_state == TCPS_FIN_WAIT_1) {
11362 			/*
11363 			 * In FIN_WAIT_1 STATE in addition to the processing
11364 			 * for the ESTABLISHED state if our FIN is now
11365 			 * acknowledged then enter FIN_WAIT_2.
11366 			 */
11367 			if (ourfinisacked) {
11368 				/*
11369 				 * If we can't receive any more data, then
11370 				 * closing user can proceed. Starting the
11371 				 * timer is contrary to the specification,
11372 				 * but if we don't get a FIN we'll hang
11373 				 * forever.
11374 				 *
11375 				 * XXXjl: we should release the tp also, and
11376 				 * use a compressed state.
11377 				 */
11378 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
11379 					soisdisconnected(so);
11380 					tcp_timer_activate(tp, TT_2MSL,
11381 					    (tcp_fast_finwait2_recycle ?
11382 					    tcp_finwait2_timeout :
11383 					    TP_MAXIDLE(tp)));
11384 				}
11385 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
11386 			}
11387 		}
11388 	}
11389 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11390 	   tiwin, thflags, nxt_pkt));
11391 }
11392 
11393 /*
11394  * Return value of 1, the TCB is unlocked and most
11395  * likely gone, return value of 0, the TCP is still
11396  * locked.
11397  */
11398 static int
11399 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so,
11400     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
11401     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
11402 {
11403 	struct tcp_rack *rack;
11404 	int32_t ret_val = 0;
11405 	int32_t ourfinisacked = 0;
11406 
11407 	ctf_calc_rwin(so, tp);
11408 	if ((thflags & TH_ACK) &&
11409 	    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
11410 	    SEQ_GT(th->th_ack, tp->snd_max))) {
11411 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
11412 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11413 		return (1);
11414 	}
11415 	rack = (struct tcp_rack *)tp->t_fb_ptr;
11416 	if (IS_FASTOPEN(tp->t_flags)) {
11417 		/*
11418 		 * When a TFO connection is in SYN_RECEIVED, the
11419 		 * only valid packets are the initial SYN, a
11420 		 * retransmit/copy of the initial SYN (possibly with
11421 		 * a subset of the original data), a valid ACK, a
11422 		 * FIN, or a RST.
11423 		 */
11424 		if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) {
11425 			tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
11426 			ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11427 			return (1);
11428 		} else if (thflags & TH_SYN) {
11429 			/* non-initial SYN is ignored */
11430 			if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) ||
11431 			    (rack->r_ctl.rc_hpts_flags & PACE_TMR_TLP) ||
11432 			    (rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) {
11433 				ctf_do_drop(m, NULL);
11434 				return (0);
11435 			}
11436 		} else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) {
11437 			ctf_do_drop(m, NULL);
11438 			return (0);
11439 		}
11440 	}
11441 
11442 	if ((thflags & TH_RST) ||
11443 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
11444 		return (__ctf_process_rst(m, th, so, tp,
11445 					  &rack->r_ctl.challenge_ack_ts,
11446 					  &rack->r_ctl.challenge_ack_cnt));
11447 	/*
11448 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
11449 	 * it's less than ts_recent, drop it.
11450 	 */
11451 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
11452 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
11453 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
11454 			return (ret_val);
11455 	}
11456 	/*
11457 	 * In the SYN-RECEIVED state, validate that the packet belongs to
11458 	 * this connection before trimming the data to fit the receive
11459 	 * window.  Check the sequence number versus IRS since we know the
11460 	 * sequence numbers haven't wrapped.  This is a partial fix for the
11461 	 * "LAND" DoS attack.
11462 	 */
11463 	if (SEQ_LT(th->th_seq, tp->irs)) {
11464 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
11465 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11466 		return (1);
11467 	}
11468 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
11469 			      &rack->r_ctl.challenge_ack_ts,
11470 			      &rack->r_ctl.challenge_ack_cnt)) {
11471 		return (ret_val);
11472 	}
11473 	/*
11474 	 * If last ACK falls within this segment's sequence numbers, record
11475 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
11476 	 * from the latest proposal of the tcplw@cray.com list (Braden
11477 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
11478 	 * with our earlier PAWS tests, so this check should be solely
11479 	 * predicated on the sequence space of this segment. 3) That we
11480 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
11481 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
11482 	 * SEG.Len, This modified check allows us to overcome RFC1323's
11483 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
11484 	 * p.869. In such cases, we can still calculate the RTT correctly
11485 	 * when RCV.NXT == Last.ACK.Sent.
11486 	 */
11487 	if ((to->to_flags & TOF_TS) != 0 &&
11488 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
11489 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
11490 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
11491 		tp->ts_recent_age = tcp_ts_getticks();
11492 		tp->ts_recent = to->to_tsval;
11493 	}
11494 	tp->snd_wnd = tiwin;
11495 	rack_validate_fo_sendwin_up(tp, rack);
11496 	/*
11497 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
11498 	 * is on (half-synchronized state), then queue data for later
11499 	 * processing; else drop segment and return.
11500 	 */
11501 	if ((thflags & TH_ACK) == 0) {
11502 		if (IS_FASTOPEN(tp->t_flags)) {
11503 			rack_cc_conn_init(tp);
11504 		}
11505 		return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11506 		    tiwin, thflags, nxt_pkt));
11507 	}
11508 	KMOD_TCPSTAT_INC(tcps_connects);
11509 	soisconnected(so);
11510 	/* Do window scaling? */
11511 	if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
11512 	    (TF_RCVD_SCALE | TF_REQ_SCALE)) {
11513 		tp->rcv_scale = tp->request_r_scale;
11514 	}
11515 	/*
11516 	 * Make transitions: SYN-RECEIVED  -> ESTABLISHED SYN-RECEIVED* ->
11517 	 * FIN-WAIT-1
11518 	 */
11519 	tp->t_starttime = ticks;
11520 	if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
11521 		tcp_fastopen_decrement_counter(tp->t_tfo_pending);
11522 		tp->t_tfo_pending = NULL;
11523 	}
11524 	if (tp->t_flags & TF_NEEDFIN) {
11525 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
11526 		tp->t_flags &= ~TF_NEEDFIN;
11527 	} else {
11528 		tcp_state_change(tp, TCPS_ESTABLISHED);
11529 		TCP_PROBE5(accept__established, NULL, tp,
11530 		    mtod(m, const char *), tp, th);
11531 		/*
11532 		 * TFO connections call cc_conn_init() during SYN
11533 		 * processing.  Calling it again here for such connections
11534 		 * is not harmless as it would undo the snd_cwnd reduction
11535 		 * that occurs when a TFO SYN|ACK is retransmitted.
11536 		 */
11537 		if (!IS_FASTOPEN(tp->t_flags))
11538 			rack_cc_conn_init(tp);
11539 	}
11540 	/*
11541 	 * Account for the ACK of our SYN prior to
11542 	 * regular ACK processing below, except for
11543 	 * simultaneous SYN, which is handled later.
11544 	 */
11545 	if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
11546 		tp->snd_una++;
11547 	/*
11548 	 * If segment contains data or ACK, will call tcp_reass() later; if
11549 	 * not, do so now to pass queued data to user.
11550 	 */
11551 	if (tlen == 0 && (thflags & TH_FIN) == 0) {
11552 		(void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
11553 		    (struct mbuf *)0);
11554 		if (tp->t_flags & TF_WAKESOR) {
11555 			tp->t_flags &= ~TF_WAKESOR;
11556 			/* NB: sorwakeup_locked() does an implicit unlock. */
11557 			sorwakeup_locked(so);
11558 		}
11559 	}
11560 	tp->snd_wl1 = th->th_seq - 1;
11561 	/* For syn-recv we need to possibly update the rtt */
11562 	if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) {
11563 		uint32_t t, mcts;
11564 
11565 		mcts = tcp_ts_getticks();
11566 		t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC;
11567 		if (!tp->t_rttlow || tp->t_rttlow > t)
11568 			tp->t_rttlow = t;
11569 		rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 5);
11570 		tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2);
11571 		tcp_rack_xmit_timer_commit(rack, tp);
11572 	}
11573 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
11574 		return (ret_val);
11575 	}
11576 	if (tp->t_state == TCPS_FIN_WAIT_1) {
11577 		/* We could have went to FIN_WAIT_1 (or EST) above */
11578 		/*
11579 		 * In FIN_WAIT_1 STATE in addition to the processing for the
11580 		 * ESTABLISHED state if our FIN is now acknowledged then
11581 		 * enter FIN_WAIT_2.
11582 		 */
11583 		if (ourfinisacked) {
11584 			/*
11585 			 * If we can't receive any more data, then closing
11586 			 * user can proceed. Starting the timer is contrary
11587 			 * to the specification, but if we don't get a FIN
11588 			 * we'll hang forever.
11589 			 *
11590 			 * XXXjl: we should release the tp also, and use a
11591 			 * compressed state.
11592 			 */
11593 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
11594 				soisdisconnected(so);
11595 				tcp_timer_activate(tp, TT_2MSL,
11596 				    (tcp_fast_finwait2_recycle ?
11597 				    tcp_finwait2_timeout :
11598 				    TP_MAXIDLE(tp)));
11599 			}
11600 			tcp_state_change(tp, TCPS_FIN_WAIT_2);
11601 		}
11602 	}
11603 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11604 	    tiwin, thflags, nxt_pkt));
11605 }
11606 
11607 /*
11608  * Return value of 1, the TCB is unlocked and most
11609  * likely gone, return value of 0, the TCP is still
11610  * locked.
11611  */
11612 static int
11613 rack_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so,
11614     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
11615     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
11616 {
11617 	int32_t ret_val = 0;
11618 	struct tcp_rack *rack;
11619 
11620 	/*
11621 	 * Header prediction: check for the two common cases of a
11622 	 * uni-directional data xfer.  If the packet has no control flags,
11623 	 * is in-sequence, the window didn't change and we're not
11624 	 * retransmitting, it's a candidate.  If the length is zero and the
11625 	 * ack moved forward, we're the sender side of the xfer.  Just free
11626 	 * the data acked & wake any higher level process that was blocked
11627 	 * waiting for space.  If the length is non-zero and the ack didn't
11628 	 * move, we're the receiver side.  If we're getting packets in-order
11629 	 * (the reassembly queue is empty), add the data toc The socket
11630 	 * buffer and note that we need a delayed ack. Make sure that the
11631 	 * hidden state-flags are also off. Since we check for
11632 	 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN.
11633 	 */
11634 	rack = (struct tcp_rack *)tp->t_fb_ptr;
11635 	if (__predict_true(((to->to_flags & TOF_SACK) == 0)) &&
11636 	    __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) == TH_ACK) &&
11637 	    __predict_true(SEGQ_EMPTY(tp)) &&
11638 	    __predict_true(th->th_seq == tp->rcv_nxt)) {
11639 		if (tlen == 0) {
11640 			if (rack_fastack(m, th, so, tp, to, drop_hdrlen, tlen,
11641 			    tiwin, nxt_pkt, rack->r_ctl.rc_rcvtime)) {
11642 				return (0);
11643 			}
11644 		} else {
11645 			if (rack_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen,
11646 			    tiwin, nxt_pkt, iptos)) {
11647 				return (0);
11648 			}
11649 		}
11650 	}
11651 	ctf_calc_rwin(so, tp);
11652 
11653 	if ((thflags & TH_RST) ||
11654 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
11655 		return (__ctf_process_rst(m, th, so, tp,
11656 					  &rack->r_ctl.challenge_ack_ts,
11657 					  &rack->r_ctl.challenge_ack_cnt));
11658 
11659 	/*
11660 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
11661 	 * synchronized state.
11662 	 */
11663 	if (thflags & TH_SYN) {
11664 		ctf_challenge_ack(m, th, tp, &ret_val);
11665 		return (ret_val);
11666 	}
11667 	/*
11668 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
11669 	 * it's less than ts_recent, drop it.
11670 	 */
11671 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
11672 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
11673 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
11674 			return (ret_val);
11675 	}
11676 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
11677 			      &rack->r_ctl.challenge_ack_ts,
11678 			      &rack->r_ctl.challenge_ack_cnt)) {
11679 		return (ret_val);
11680 	}
11681 	/*
11682 	 * If last ACK falls within this segment's sequence numbers, record
11683 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
11684 	 * from the latest proposal of the tcplw@cray.com list (Braden
11685 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
11686 	 * with our earlier PAWS tests, so this check should be solely
11687 	 * predicated on the sequence space of this segment. 3) That we
11688 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
11689 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
11690 	 * SEG.Len, This modified check allows us to overcome RFC1323's
11691 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
11692 	 * p.869. In such cases, we can still calculate the RTT correctly
11693 	 * when RCV.NXT == Last.ACK.Sent.
11694 	 */
11695 	if ((to->to_flags & TOF_TS) != 0 &&
11696 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
11697 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
11698 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
11699 		tp->ts_recent_age = tcp_ts_getticks();
11700 		tp->ts_recent = to->to_tsval;
11701 	}
11702 	/*
11703 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
11704 	 * is on (half-synchronized state), then queue data for later
11705 	 * processing; else drop segment and return.
11706 	 */
11707 	if ((thflags & TH_ACK) == 0) {
11708 		if (tp->t_flags & TF_NEEDSYN) {
11709 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11710 			    tiwin, thflags, nxt_pkt));
11711 
11712 		} else if (tp->t_flags & TF_ACKNOW) {
11713 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
11714 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
11715 			return (ret_val);
11716 		} else {
11717 			ctf_do_drop(m, NULL);
11718 			return (0);
11719 		}
11720 	}
11721 	/*
11722 	 * Ack processing.
11723 	 */
11724 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
11725 		return (ret_val);
11726 	}
11727 	if (sbavail(&so->so_snd)) {
11728 		if (ctf_progress_timeout_check(tp, true)) {
11729 			rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
11730 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11731 			return (1);
11732 		}
11733 	}
11734 	/* State changes only happen in rack_process_data() */
11735 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11736 	    tiwin, thflags, nxt_pkt));
11737 }
11738 
11739 /*
11740  * Return value of 1, the TCB is unlocked and most
11741  * likely gone, return value of 0, the TCP is still
11742  * locked.
11743  */
11744 static int
11745 rack_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so,
11746     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
11747     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
11748 {
11749 	int32_t ret_val = 0;
11750 	struct tcp_rack *rack;
11751 
11752 	rack = (struct tcp_rack *)tp->t_fb_ptr;
11753 	ctf_calc_rwin(so, tp);
11754 	if ((thflags & TH_RST) ||
11755 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
11756 		return (__ctf_process_rst(m, th, so, tp,
11757 					  &rack->r_ctl.challenge_ack_ts,
11758 					  &rack->r_ctl.challenge_ack_cnt));
11759 	/*
11760 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
11761 	 * synchronized state.
11762 	 */
11763 	if (thflags & TH_SYN) {
11764 		ctf_challenge_ack(m, th, tp, &ret_val);
11765 		return (ret_val);
11766 	}
11767 	/*
11768 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
11769 	 * it's less than ts_recent, drop it.
11770 	 */
11771 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
11772 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
11773 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
11774 			return (ret_val);
11775 	}
11776 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
11777 			      &rack->r_ctl.challenge_ack_ts,
11778 			      &rack->r_ctl.challenge_ack_cnt)) {
11779 		return (ret_val);
11780 	}
11781 	/*
11782 	 * If last ACK falls within this segment's sequence numbers, record
11783 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
11784 	 * from the latest proposal of the tcplw@cray.com list (Braden
11785 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
11786 	 * with our earlier PAWS tests, so this check should be solely
11787 	 * predicated on the sequence space of this segment. 3) That we
11788 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
11789 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
11790 	 * SEG.Len, This modified check allows us to overcome RFC1323's
11791 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
11792 	 * p.869. In such cases, we can still calculate the RTT correctly
11793 	 * when RCV.NXT == Last.ACK.Sent.
11794 	 */
11795 	if ((to->to_flags & TOF_TS) != 0 &&
11796 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
11797 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
11798 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
11799 		tp->ts_recent_age = tcp_ts_getticks();
11800 		tp->ts_recent = to->to_tsval;
11801 	}
11802 	/*
11803 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
11804 	 * is on (half-synchronized state), then queue data for later
11805 	 * processing; else drop segment and return.
11806 	 */
11807 	if ((thflags & TH_ACK) == 0) {
11808 		if (tp->t_flags & TF_NEEDSYN) {
11809 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11810 			    tiwin, thflags, nxt_pkt));
11811 
11812 		} else if (tp->t_flags & TF_ACKNOW) {
11813 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
11814 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
11815 			return (ret_val);
11816 		} else {
11817 			ctf_do_drop(m, NULL);
11818 			return (0);
11819 		}
11820 	}
11821 	/*
11822 	 * Ack processing.
11823 	 */
11824 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
11825 		return (ret_val);
11826 	}
11827 	if (sbavail(&so->so_snd)) {
11828 		if (ctf_progress_timeout_check(tp, true)) {
11829 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
11830 						tp, tick, PROGRESS_DROP, __LINE__);
11831 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11832 			return (1);
11833 		}
11834 	}
11835 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11836 	    tiwin, thflags, nxt_pkt));
11837 }
11838 
11839 static int
11840 rack_check_data_after_close(struct mbuf *m,
11841     struct tcpcb *tp, int32_t *tlen, struct tcphdr *th, struct socket *so)
11842 {
11843 	struct tcp_rack *rack;
11844 
11845 	rack = (struct tcp_rack *)tp->t_fb_ptr;
11846 	if (rack->rc_allow_data_af_clo == 0) {
11847 	close_now:
11848 		tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
11849 		/* tcp_close will kill the inp pre-log the Reset */
11850 		tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
11851 		tp = tcp_close(tp);
11852 		KMOD_TCPSTAT_INC(tcps_rcvafterclose);
11853 		ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen));
11854 		return (1);
11855 	}
11856 	if (sbavail(&so->so_snd) == 0)
11857 		goto close_now;
11858 	/* Ok we allow data that is ignored and a followup reset */
11859 	tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
11860 	tp->rcv_nxt = th->th_seq + *tlen;
11861 	tp->t_flags2 |= TF2_DROP_AF_DATA;
11862 	rack->r_wanted_output = 1;
11863 	*tlen = 0;
11864 	return (0);
11865 }
11866 
11867 /*
11868  * Return value of 1, the TCB is unlocked and most
11869  * likely gone, return value of 0, the TCP is still
11870  * locked.
11871  */
11872 static int
11873 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so,
11874     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
11875     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
11876 {
11877 	int32_t ret_val = 0;
11878 	int32_t ourfinisacked = 0;
11879 	struct tcp_rack *rack;
11880 
11881 	rack = (struct tcp_rack *)tp->t_fb_ptr;
11882 	ctf_calc_rwin(so, tp);
11883 
11884 	if ((thflags & TH_RST) ||
11885 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
11886 		return (__ctf_process_rst(m, th, so, tp,
11887 					  &rack->r_ctl.challenge_ack_ts,
11888 					  &rack->r_ctl.challenge_ack_cnt));
11889 	/*
11890 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
11891 	 * synchronized state.
11892 	 */
11893 	if (thflags & TH_SYN) {
11894 		ctf_challenge_ack(m, th, tp, &ret_val);
11895 		return (ret_val);
11896 	}
11897 	/*
11898 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
11899 	 * it's less than ts_recent, drop it.
11900 	 */
11901 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
11902 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
11903 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
11904 			return (ret_val);
11905 	}
11906 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
11907 			      &rack->r_ctl.challenge_ack_ts,
11908 			      &rack->r_ctl.challenge_ack_cnt)) {
11909 		return (ret_val);
11910 	}
11911 	/*
11912 	 * If new data are received on a connection after the user processes
11913 	 * are gone, then RST the other end.
11914 	 */
11915 	if ((tp->t_flags & TF_CLOSED) && tlen &&
11916 	    rack_check_data_after_close(m, tp, &tlen, th, so))
11917 		return (1);
11918 	/*
11919 	 * If last ACK falls within this segment's sequence numbers, record
11920 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
11921 	 * from the latest proposal of the tcplw@cray.com list (Braden
11922 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
11923 	 * with our earlier PAWS tests, so this check should be solely
11924 	 * predicated on the sequence space of this segment. 3) That we
11925 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
11926 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
11927 	 * SEG.Len, This modified check allows us to overcome RFC1323's
11928 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
11929 	 * p.869. In such cases, we can still calculate the RTT correctly
11930 	 * when RCV.NXT == Last.ACK.Sent.
11931 	 */
11932 	if ((to->to_flags & TOF_TS) != 0 &&
11933 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
11934 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
11935 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
11936 		tp->ts_recent_age = tcp_ts_getticks();
11937 		tp->ts_recent = to->to_tsval;
11938 	}
11939 	/*
11940 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
11941 	 * is on (half-synchronized state), then queue data for later
11942 	 * processing; else drop segment and return.
11943 	 */
11944 	if ((thflags & TH_ACK) == 0) {
11945 		if (tp->t_flags & TF_NEEDSYN) {
11946 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11947 			    tiwin, thflags, nxt_pkt));
11948 		} else if (tp->t_flags & TF_ACKNOW) {
11949 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
11950 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
11951 			return (ret_val);
11952 		} else {
11953 			ctf_do_drop(m, NULL);
11954 			return (0);
11955 		}
11956 	}
11957 	/*
11958 	 * Ack processing.
11959 	 */
11960 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
11961 		return (ret_val);
11962 	}
11963 	if (ourfinisacked) {
11964 		/*
11965 		 * If we can't receive any more data, then closing user can
11966 		 * proceed. Starting the timer is contrary to the
11967 		 * specification, but if we don't get a FIN we'll hang
11968 		 * forever.
11969 		 *
11970 		 * XXXjl: we should release the tp also, and use a
11971 		 * compressed state.
11972 		 */
11973 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
11974 			soisdisconnected(so);
11975 			tcp_timer_activate(tp, TT_2MSL,
11976 			    (tcp_fast_finwait2_recycle ?
11977 			    tcp_finwait2_timeout :
11978 			    TP_MAXIDLE(tp)));
11979 		}
11980 		tcp_state_change(tp, TCPS_FIN_WAIT_2);
11981 	}
11982 	if (sbavail(&so->so_snd)) {
11983 		if (ctf_progress_timeout_check(tp, true)) {
11984 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
11985 						tp, tick, PROGRESS_DROP, __LINE__);
11986 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11987 			return (1);
11988 		}
11989 	}
11990 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11991 	    tiwin, thflags, nxt_pkt));
11992 }
11993 
11994 /*
11995  * Return value of 1, the TCB is unlocked and most
11996  * likely gone, return value of 0, the TCP is still
11997  * locked.
11998  */
11999 static int
12000 rack_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so,
12001     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12002     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
12003 {
12004 	int32_t ret_val = 0;
12005 	int32_t ourfinisacked = 0;
12006 	struct tcp_rack *rack;
12007 
12008 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12009 	ctf_calc_rwin(so, tp);
12010 
12011 	if ((thflags & TH_RST) ||
12012 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
12013 		return (__ctf_process_rst(m, th, so, tp,
12014 					  &rack->r_ctl.challenge_ack_ts,
12015 					  &rack->r_ctl.challenge_ack_cnt));
12016 	/*
12017 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
12018 	 * synchronized state.
12019 	 */
12020 	if (thflags & TH_SYN) {
12021 		ctf_challenge_ack(m, th, tp, &ret_val);
12022 		return (ret_val);
12023 	}
12024 	/*
12025 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
12026 	 * it's less than ts_recent, drop it.
12027 	 */
12028 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
12029 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
12030 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
12031 			return (ret_val);
12032 	}
12033 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
12034 			      &rack->r_ctl.challenge_ack_ts,
12035 			      &rack->r_ctl.challenge_ack_cnt)) {
12036 		return (ret_val);
12037 	}
12038 	/*
12039 	 * If new data are received on a connection after the user processes
12040 	 * are gone, then RST the other end.
12041 	 */
12042 	if ((tp->t_flags & TF_CLOSED) && tlen &&
12043 	    rack_check_data_after_close(m, tp, &tlen, th, so))
12044 		return (1);
12045 	/*
12046 	 * If last ACK falls within this segment's sequence numbers, record
12047 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
12048 	 * from the latest proposal of the tcplw@cray.com list (Braden
12049 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
12050 	 * with our earlier PAWS tests, so this check should be solely
12051 	 * predicated on the sequence space of this segment. 3) That we
12052 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
12053 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
12054 	 * SEG.Len, This modified check allows us to overcome RFC1323's
12055 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
12056 	 * p.869. In such cases, we can still calculate the RTT correctly
12057 	 * when RCV.NXT == Last.ACK.Sent.
12058 	 */
12059 	if ((to->to_flags & TOF_TS) != 0 &&
12060 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
12061 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
12062 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
12063 		tp->ts_recent_age = tcp_ts_getticks();
12064 		tp->ts_recent = to->to_tsval;
12065 	}
12066 	/*
12067 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
12068 	 * is on (half-synchronized state), then queue data for later
12069 	 * processing; else drop segment and return.
12070 	 */
12071 	if ((thflags & TH_ACK) == 0) {
12072 		if (tp->t_flags & TF_NEEDSYN) {
12073 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12074 			    tiwin, thflags, nxt_pkt));
12075 		} else if (tp->t_flags & TF_ACKNOW) {
12076 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
12077 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
12078 			return (ret_val);
12079 		} else {
12080 			ctf_do_drop(m, NULL);
12081 			return (0);
12082 		}
12083 	}
12084 	/*
12085 	 * Ack processing.
12086 	 */
12087 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
12088 		return (ret_val);
12089 	}
12090 	if (ourfinisacked) {
12091 		tcp_twstart(tp);
12092 		m_freem(m);
12093 		return (1);
12094 	}
12095 	if (sbavail(&so->so_snd)) {
12096 		if (ctf_progress_timeout_check(tp, true)) {
12097 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
12098 						tp, tick, PROGRESS_DROP, __LINE__);
12099 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
12100 			return (1);
12101 		}
12102 	}
12103 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12104 	    tiwin, thflags, nxt_pkt));
12105 }
12106 
12107 /*
12108  * Return value of 1, the TCB is unlocked and most
12109  * likely gone, return value of 0, the TCP is still
12110  * locked.
12111  */
12112 static int
12113 rack_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
12114     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12115     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
12116 {
12117 	int32_t ret_val = 0;
12118 	int32_t ourfinisacked = 0;
12119 	struct tcp_rack *rack;
12120 
12121 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12122 	ctf_calc_rwin(so, tp);
12123 
12124 	if ((thflags & TH_RST) ||
12125 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
12126 		return (__ctf_process_rst(m, th, so, tp,
12127 					  &rack->r_ctl.challenge_ack_ts,
12128 					  &rack->r_ctl.challenge_ack_cnt));
12129 	/*
12130 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
12131 	 * synchronized state.
12132 	 */
12133 	if (thflags & TH_SYN) {
12134 		ctf_challenge_ack(m, th, tp, &ret_val);
12135 		return (ret_val);
12136 	}
12137 	/*
12138 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
12139 	 * it's less than ts_recent, drop it.
12140 	 */
12141 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
12142 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
12143 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
12144 			return (ret_val);
12145 	}
12146 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
12147 			      &rack->r_ctl.challenge_ack_ts,
12148 			      &rack->r_ctl.challenge_ack_cnt)) {
12149 		return (ret_val);
12150 	}
12151 	/*
12152 	 * If new data are received on a connection after the user processes
12153 	 * are gone, then RST the other end.
12154 	 */
12155 	if ((tp->t_flags & TF_CLOSED) && tlen &&
12156 	    rack_check_data_after_close(m, tp, &tlen, th, so))
12157 		return (1);
12158 	/*
12159 	 * If last ACK falls within this segment's sequence numbers, record
12160 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
12161 	 * from the latest proposal of the tcplw@cray.com list (Braden
12162 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
12163 	 * with our earlier PAWS tests, so this check should be solely
12164 	 * predicated on the sequence space of this segment. 3) That we
12165 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
12166 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
12167 	 * SEG.Len, This modified check allows us to overcome RFC1323's
12168 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
12169 	 * p.869. In such cases, we can still calculate the RTT correctly
12170 	 * when RCV.NXT == Last.ACK.Sent.
12171 	 */
12172 	if ((to->to_flags & TOF_TS) != 0 &&
12173 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
12174 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
12175 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
12176 		tp->ts_recent_age = tcp_ts_getticks();
12177 		tp->ts_recent = to->to_tsval;
12178 	}
12179 	/*
12180 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
12181 	 * is on (half-synchronized state), then queue data for later
12182 	 * processing; else drop segment and return.
12183 	 */
12184 	if ((thflags & TH_ACK) == 0) {
12185 		if (tp->t_flags & TF_NEEDSYN) {
12186 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12187 			    tiwin, thflags, nxt_pkt));
12188 		} else if (tp->t_flags & TF_ACKNOW) {
12189 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
12190 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
12191 			return (ret_val);
12192 		} else {
12193 			ctf_do_drop(m, NULL);
12194 			return (0);
12195 		}
12196 	}
12197 	/*
12198 	 * case TCPS_LAST_ACK: Ack processing.
12199 	 */
12200 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
12201 		return (ret_val);
12202 	}
12203 	if (ourfinisacked) {
12204 		tp = tcp_close(tp);
12205 		ctf_do_drop(m, tp);
12206 		return (1);
12207 	}
12208 	if (sbavail(&so->so_snd)) {
12209 		if (ctf_progress_timeout_check(tp, true)) {
12210 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
12211 						tp, tick, PROGRESS_DROP, __LINE__);
12212 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
12213 			return (1);
12214 		}
12215 	}
12216 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12217 	    tiwin, thflags, nxt_pkt));
12218 }
12219 
12220 /*
12221  * Return value of 1, the TCB is unlocked and most
12222  * likely gone, return value of 0, the TCP is still
12223  * locked.
12224  */
12225 static int
12226 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so,
12227     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12228     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
12229 {
12230 	int32_t ret_val = 0;
12231 	int32_t ourfinisacked = 0;
12232 	struct tcp_rack *rack;
12233 
12234 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12235 	ctf_calc_rwin(so, tp);
12236 
12237 	/* Reset receive buffer auto scaling when not in bulk receive mode. */
12238 	if ((thflags & TH_RST) ||
12239 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
12240 		return (__ctf_process_rst(m, th, so, tp,
12241 					  &rack->r_ctl.challenge_ack_ts,
12242 					  &rack->r_ctl.challenge_ack_cnt));
12243 	/*
12244 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
12245 	 * synchronized state.
12246 	 */
12247 	if (thflags & TH_SYN) {
12248 		ctf_challenge_ack(m, th, tp, &ret_val);
12249 		return (ret_val);
12250 	}
12251 	/*
12252 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
12253 	 * it's less than ts_recent, drop it.
12254 	 */
12255 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
12256 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
12257 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
12258 			return (ret_val);
12259 	}
12260 	if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
12261 			      &rack->r_ctl.challenge_ack_ts,
12262 			      &rack->r_ctl.challenge_ack_cnt)) {
12263 		return (ret_val);
12264 	}
12265 	/*
12266 	 * If new data are received on a connection after the user processes
12267 	 * are gone, then RST the other end.
12268 	 */
12269 	if ((tp->t_flags & TF_CLOSED) && tlen &&
12270 	    rack_check_data_after_close(m, tp, &tlen, th, so))
12271 		return (1);
12272 	/*
12273 	 * If last ACK falls within this segment's sequence numbers, record
12274 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
12275 	 * from the latest proposal of the tcplw@cray.com list (Braden
12276 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
12277 	 * with our earlier PAWS tests, so this check should be solely
12278 	 * predicated on the sequence space of this segment. 3) That we
12279 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
12280 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
12281 	 * SEG.Len, This modified check allows us to overcome RFC1323's
12282 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
12283 	 * p.869. In such cases, we can still calculate the RTT correctly
12284 	 * when RCV.NXT == Last.ACK.Sent.
12285 	 */
12286 	if ((to->to_flags & TOF_TS) != 0 &&
12287 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
12288 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
12289 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
12290 		tp->ts_recent_age = tcp_ts_getticks();
12291 		tp->ts_recent = to->to_tsval;
12292 	}
12293 	/*
12294 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
12295 	 * is on (half-synchronized state), then queue data for later
12296 	 * processing; else drop segment and return.
12297 	 */
12298 	if ((thflags & TH_ACK) == 0) {
12299 		if (tp->t_flags & TF_NEEDSYN) {
12300 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12301 			    tiwin, thflags, nxt_pkt));
12302 		} else if (tp->t_flags & TF_ACKNOW) {
12303 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
12304 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
12305 			return (ret_val);
12306 		} else {
12307 			ctf_do_drop(m, NULL);
12308 			return (0);
12309 		}
12310 	}
12311 	/*
12312 	 * Ack processing.
12313 	 */
12314 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
12315 		return (ret_val);
12316 	}
12317 	if (sbavail(&so->so_snd)) {
12318 		if (ctf_progress_timeout_check(tp, true)) {
12319 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
12320 						tp, tick, PROGRESS_DROP, __LINE__);
12321 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
12322 			return (1);
12323 		}
12324 	}
12325 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12326 	    tiwin, thflags, nxt_pkt));
12327 }
12328 
12329 static void inline
12330 rack_clear_rate_sample(struct tcp_rack *rack)
12331 {
12332 	rack->r_ctl.rack_rs.rs_flags = RACK_RTT_EMPTY;
12333 	rack->r_ctl.rack_rs.rs_rtt_cnt = 0;
12334 	rack->r_ctl.rack_rs.rs_rtt_tot = 0;
12335 }
12336 
12337 static void
12338 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override)
12339 {
12340 	uint64_t bw_est, rate_wanted;
12341 	int chged = 0;
12342 	uint32_t user_max, orig_min, orig_max;
12343 
12344 	orig_min = rack->r_ctl.rc_pace_min_segs;
12345 	orig_max = rack->r_ctl.rc_pace_max_segs;
12346 	user_max = ctf_fixed_maxseg(tp) * rack->rc_user_set_max_segs;
12347 	if (ctf_fixed_maxseg(tp) != rack->r_ctl.rc_pace_min_segs)
12348 		chged = 1;
12349 	rack->r_ctl.rc_pace_min_segs = ctf_fixed_maxseg(tp);
12350 	if (rack->use_fixed_rate || rack->rc_force_max_seg) {
12351 		if (user_max != rack->r_ctl.rc_pace_max_segs)
12352 			chged = 1;
12353 	}
12354 	if (rack->rc_force_max_seg) {
12355 		rack->r_ctl.rc_pace_max_segs = user_max;
12356 	} else if (rack->use_fixed_rate) {
12357 		bw_est = rack_get_bw(rack);
12358 		if ((rack->r_ctl.crte == NULL) ||
12359 		    (bw_est != rack->r_ctl.crte->rate)) {
12360 			rack->r_ctl.rc_pace_max_segs = user_max;
12361 		} else {
12362 			/* We are pacing right at the hardware rate */
12363 			uint32_t segsiz;
12364 
12365 			segsiz = min(ctf_fixed_maxseg(tp),
12366 				     rack->r_ctl.rc_pace_min_segs);
12367 			rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size(
12368 				                           tp, bw_est, segsiz, 0,
12369 							   rack->r_ctl.crte, NULL);
12370 		}
12371 	} else if (rack->rc_always_pace) {
12372 		if (rack->r_ctl.gp_bw ||
12373 #ifdef NETFLIX_PEAKRATE
12374 		    rack->rc_tp->t_maxpeakrate ||
12375 #endif
12376 		    rack->r_ctl.init_rate) {
12377 			/* We have a rate of some sort set */
12378 			uint32_t  orig;
12379 
12380 			bw_est = rack_get_bw(rack);
12381 			orig = rack->r_ctl.rc_pace_max_segs;
12382 			if (fill_override)
12383 				rate_wanted = *fill_override;
12384 			else
12385 				rate_wanted = rack_get_output_bw(rack, bw_est, NULL, NULL);
12386 			if (rate_wanted) {
12387 				/* We have something */
12388 				rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack,
12389 										   rate_wanted,
12390 										   ctf_fixed_maxseg(rack->rc_tp));
12391 			} else
12392 				rack->r_ctl.rc_pace_max_segs = rack->r_ctl.rc_pace_min_segs;
12393 			if (orig != rack->r_ctl.rc_pace_max_segs)
12394 				chged = 1;
12395 		} else if ((rack->r_ctl.gp_bw == 0) &&
12396 			   (rack->r_ctl.rc_pace_max_segs == 0)) {
12397 			/*
12398 			 * If we have nothing limit us to bursting
12399 			 * out IW sized pieces.
12400 			 */
12401 			chged = 1;
12402 			rack->r_ctl.rc_pace_max_segs = rc_init_window(rack);
12403 		}
12404 	}
12405 	if (rack->r_ctl.rc_pace_max_segs > PACE_MAX_IP_BYTES) {
12406 		chged = 1;
12407 		rack->r_ctl.rc_pace_max_segs = PACE_MAX_IP_BYTES;
12408 	}
12409 	if (chged)
12410 		rack_log_type_pacing_sizes(tp, rack, orig_min, orig_max, line, 2);
12411 }
12412 
12413 
12414 static void
12415 rack_init_fsb_block(struct tcpcb *tp, struct tcp_rack *rack)
12416 {
12417 #ifdef INET6
12418 	struct ip6_hdr *ip6 = NULL;
12419 #endif
12420 #ifdef INET
12421 	struct ip *ip = NULL;
12422 #endif
12423 	struct udphdr *udp = NULL;
12424 
12425 	/* Ok lets fill in the fast block, it can only be used with no IP options! */
12426 #ifdef INET6
12427 	if (rack->r_is_v6) {
12428 		rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
12429 		ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
12430 		if (tp->t_port) {
12431 			rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr);
12432 			udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
12433 			udp->uh_sport = htons(V_tcp_udp_tunneling_port);
12434 			udp->uh_dport = tp->t_port;
12435 			rack->r_ctl.fsb.udp = udp;
12436 			rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1);
12437 		} else
12438 		{
12439 			rack->r_ctl.fsb.th = (struct tcphdr *)(ip6 + 1);
12440 			rack->r_ctl.fsb.udp = NULL;
12441 		}
12442 		tcpip_fillheaders(rack->rc_inp,
12443 				  tp->t_port,
12444 				  ip6, rack->r_ctl.fsb.th);
12445 	} else
12446 #endif				/* INET6 */
12447 	{
12448 		rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr);
12449 		ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
12450 		if (tp->t_port) {
12451 			rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr);
12452 			udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
12453 			udp->uh_sport = htons(V_tcp_udp_tunneling_port);
12454 			udp->uh_dport = tp->t_port;
12455 			rack->r_ctl.fsb.udp = udp;
12456 			rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1);
12457 		} else
12458 		{
12459 			rack->r_ctl.fsb.udp = NULL;
12460 			rack->r_ctl.fsb.th = (struct tcphdr *)(ip + 1);
12461 		}
12462 		tcpip_fillheaders(rack->rc_inp,
12463 				  tp->t_port,
12464 				  ip, rack->r_ctl.fsb.th);
12465 	}
12466 	rack->r_fsb_inited = 1;
12467 }
12468 
12469 static int
12470 rack_init_fsb(struct tcpcb *tp, struct tcp_rack *rack)
12471 {
12472 	/*
12473 	 * Allocate the larger of spaces V6 if available else just
12474 	 * V4 and include udphdr (overbook)
12475 	 */
12476 #ifdef INET6
12477 	rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr) + sizeof(struct udphdr);
12478 #else
12479 	rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr) + sizeof(struct udphdr);
12480 #endif
12481 	rack->r_ctl.fsb.tcp_ip_hdr = malloc(rack->r_ctl.fsb.tcp_ip_hdr_len,
12482 					    M_TCPFSB, M_NOWAIT|M_ZERO);
12483 	if (rack->r_ctl.fsb.tcp_ip_hdr == NULL) {
12484 		return (ENOMEM);
12485 	}
12486 	rack->r_fsb_inited = 0;
12487 	return (0);
12488 }
12489 
12490 static int
12491 rack_init(struct tcpcb *tp)
12492 {
12493 	struct tcp_rack *rack = NULL;
12494 #ifdef INVARIANTS
12495 	struct rack_sendmap *insret;
12496 #endif
12497 	uint32_t iwin, snt, us_cts;
12498 	int err;
12499 
12500 	tp->t_fb_ptr = uma_zalloc(rack_pcb_zone, M_NOWAIT);
12501 	if (tp->t_fb_ptr == NULL) {
12502 		/*
12503 		 * We need to allocate memory but cant. The INP and INP_INFO
12504 		 * locks and they are recursive (happens during setup. So a
12505 		 * scheme to drop the locks fails :(
12506 		 *
12507 		 */
12508 		return (ENOMEM);
12509 	}
12510 	memset(tp->t_fb_ptr, 0, sizeof(struct tcp_rack));
12511 
12512 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12513 	RB_INIT(&rack->r_ctl.rc_mtree);
12514 	TAILQ_INIT(&rack->r_ctl.rc_free);
12515 	TAILQ_INIT(&rack->r_ctl.rc_tmap);
12516 	rack->rc_tp = tp;
12517 	rack->rc_inp = tp->t_inpcb;
12518 	/* Set the flag */
12519 	rack->r_is_v6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
12520 	/* Probably not needed but lets be sure */
12521 	rack_clear_rate_sample(rack);
12522 	/*
12523 	 * Save off the default values, socket options will poke
12524 	 * at these if pacing is not on or we have not yet
12525 	 * reached where pacing is on (gp_ready/fixed enabled).
12526 	 * When they get set into the CC module (when gp_ready
12527 	 * is enabled or we enable fixed) then we will set these
12528 	 * values into the CC and place in here the old values
12529 	 * so we have a restoral. Then we will set the flag
12530 	 * rc_pacing_cc_set. That way whenever we turn off pacing
12531 	 * or switch off this stack, we will know to go restore
12532 	 * the saved values.
12533 	 */
12534 	rack->r_ctl.rc_saved_beta.beta = V_newreno_beta_ecn;
12535 	rack->r_ctl.rc_saved_beta.beta_ecn = V_newreno_beta_ecn;
12536 	/* We want abe like behavior as well */
12537 	rack->r_ctl.rc_saved_beta.newreno_flags |= CC_NEWRENO_BETA_ECN_ENABLED;
12538 	rack->r_ctl.rc_reorder_fade = rack_reorder_fade;
12539 	rack->rc_allow_data_af_clo = rack_ignore_data_after_close;
12540 	rack->r_ctl.rc_tlp_threshold = rack_tlp_thresh;
12541 	rack->r_ctl.roundends = tp->snd_max;
12542 	if (use_rack_rr)
12543 		rack->use_rack_rr = 1;
12544 	if (V_tcp_delack_enabled)
12545 		tp->t_delayed_ack = 1;
12546 	else
12547 		tp->t_delayed_ack = 0;
12548 #ifdef TCP_ACCOUNTING
12549 	if (rack_tcp_accounting) {
12550 		tp->t_flags2 |= TF2_TCP_ACCOUNTING;
12551 	}
12552 #endif
12553 	if (rack_enable_shared_cwnd)
12554 		rack->rack_enable_scwnd = 1;
12555 	rack->rc_user_set_max_segs = rack_hptsi_segments;
12556 	rack->rc_force_max_seg = 0;
12557 	if (rack_use_imac_dack)
12558 		rack->rc_dack_mode = 1;
12559 	TAILQ_INIT(&rack->r_ctl.opt_list);
12560 	rack->r_ctl.rc_reorder_shift = rack_reorder_thresh;
12561 	rack->r_ctl.rc_pkt_delay = rack_pkt_delay;
12562 	rack->r_ctl.rc_tlp_cwnd_reduce = rack_lower_cwnd_at_tlp;
12563 	rack->r_ctl.rc_lowest_us_rtt = 0xffffffff;
12564 	rack->r_ctl.rc_highest_us_rtt = 0;
12565 	rack->r_ctl.bw_rate_cap = rack_bw_rate_cap;
12566 	rack->r_ctl.timer_slop = TICKS_2_USEC(tcp_rexmit_slop);
12567 	if (rack_use_cmp_acks)
12568 		rack->r_use_cmp_ack = 1;
12569 	if (rack_disable_prr)
12570 		rack->rack_no_prr = 1;
12571 	if (rack_gp_no_rec_chg)
12572 		rack->rc_gp_no_rec_chg = 1;
12573 	if (rack_pace_every_seg && tcp_can_enable_pacing()) {
12574 		rack->rc_always_pace = 1;
12575 		if (rack->use_fixed_rate || rack->gp_ready)
12576 			rack_set_cc_pacing(rack);
12577 	} else
12578 		rack->rc_always_pace = 0;
12579 	if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack)
12580 		rack->r_mbuf_queue = 1;
12581 	else
12582 		rack->r_mbuf_queue = 0;
12583 	if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
12584 		tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ;
12585 	else
12586 		tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
12587 	rack_set_pace_segments(tp, rack, __LINE__, NULL);
12588 	if (rack_limits_scwnd)
12589 		rack->r_limit_scw = 1;
12590 	else
12591 		rack->r_limit_scw = 0;
12592 	rack->rc_labc = V_tcp_abc_l_var;
12593 	rack->r_ctl.rc_high_rwnd = tp->snd_wnd;
12594 	rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
12595 	rack->r_ctl.rc_rate_sample_method = rack_rate_sample_method;
12596 	rack->rack_tlp_threshold_use = rack_tlp_threshold_use;
12597 	rack->r_ctl.rc_prr_sendalot = rack_send_a_lot_in_prr;
12598 	rack->r_ctl.rc_min_to = rack_min_to;
12599 	microuptime(&rack->r_ctl.act_rcv_time);
12600 	rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time;
12601 	rack->rc_init_win = rack_default_init_window;
12602 	rack->r_ctl.rack_per_of_gp_ss = rack_per_of_gp_ss;
12603 	if (rack_hw_up_only)
12604 		rack->r_up_only = 1;
12605 	if (rack_do_dyn_mul) {
12606 		/* When dynamic adjustment is on CA needs to start at 100% */
12607 		rack->rc_gp_dyn_mul = 1;
12608 		if (rack_do_dyn_mul >= 100)
12609 			rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul;
12610 	} else
12611 		rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca;
12612 	rack->r_ctl.rack_per_of_gp_rec = rack_per_of_gp_rec;
12613 	rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
12614 	rack->r_ctl.rc_tlp_rxt_last_time = tcp_tv_to_mssectick(&rack->r_ctl.act_rcv_time);
12615 	setup_time_filter_small(&rack->r_ctl.rc_gp_min_rtt, FILTER_TYPE_MIN,
12616 				rack_probertt_filter_life);
12617 	us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
12618 	rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
12619 	rack->r_ctl.rc_time_of_last_probertt = us_cts;
12620 	rack->r_ctl.challenge_ack_ts = tcp_ts_getticks();
12621 	rack->r_ctl.rc_time_probertt_starts = 0;
12622 	if (rack_dsack_std_based & 0x1) {
12623 		/* Basically this means all rack timers are at least (srtt + 1/4 srtt) */
12624 		rack->rc_rack_tmr_std_based = 1;
12625 	}
12626 	if (rack_dsack_std_based & 0x2) {
12627 		/* Basically this means  rack timers are extended based on dsack by up to (2 * srtt) */
12628 		rack->rc_rack_use_dsack = 1;
12629 	}
12630 	/* We require at least one measurement, even if the sysctl is 0 */
12631 	if (rack_req_measurements)
12632 		rack->r_ctl.req_measurements = rack_req_measurements;
12633 	else
12634 		rack->r_ctl.req_measurements = 1;
12635 	if (rack_enable_hw_pacing)
12636 		rack->rack_hdw_pace_ena = 1;
12637 	if (rack_hw_rate_caps)
12638 		rack->r_rack_hw_rate_caps = 1;
12639 	/* Do we force on detection? */
12640 #ifdef NETFLIX_EXP_DETECTION
12641 	if (tcp_force_detection)
12642 		rack->do_detection = 1;
12643 	else
12644 #endif
12645 		rack->do_detection = 0;
12646 	if (rack_non_rxt_use_cr)
12647 		rack->rack_rec_nonrxt_use_cr = 1;
12648 	err = rack_init_fsb(tp, rack);
12649 	if (err) {
12650 		uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
12651 		tp->t_fb_ptr = NULL;
12652 		return (err);
12653 	}
12654 	if (tp->snd_una != tp->snd_max) {
12655 		/* Create a send map for the current outstanding data */
12656 		struct rack_sendmap *rsm;
12657 
12658 		rsm = rack_alloc(rack);
12659 		if (rsm == NULL) {
12660 			uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
12661 			tp->t_fb_ptr = NULL;
12662 			return (ENOMEM);
12663 		}
12664 		rsm->r_no_rtt_allowed = 1;
12665 		rsm->r_tim_lastsent[0] = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
12666 		rsm->r_rtr_cnt = 1;
12667 		rsm->r_rtr_bytes = 0;
12668 		if (tp->t_flags & TF_SENTFIN)
12669 			rsm->r_flags |= RACK_HAS_FIN;
12670 		if ((tp->snd_una == tp->iss) &&
12671 		    !TCPS_HAVEESTABLISHED(tp->t_state))
12672 			rsm->r_flags |= RACK_HAS_SYN;
12673 		rsm->r_start = tp->snd_una;
12674 		rsm->r_end = tp->snd_max;
12675 		rsm->r_dupack = 0;
12676 		if (rack->rc_inp->inp_socket->so_snd.sb_mb != NULL) {
12677 			rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd, 0, &rsm->soff);
12678 			if (rsm->m)
12679 				rsm->orig_m_len = rsm->m->m_len;
12680 			else
12681 				rsm->orig_m_len = 0;
12682 		} else {
12683 			/*
12684 			 * This can happen if we have a stand-alone FIN or
12685 			 *  SYN.
12686 			 */
12687 			rsm->m = NULL;
12688 			rsm->orig_m_len = 0;
12689 			rsm->soff = 0;
12690 		}
12691 #ifndef INVARIANTS
12692 		(void)RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
12693 #else
12694 		insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
12695 		if (insret != NULL) {
12696 			panic("Insert in rb tree fails ret:%p rack:%p rsm:%p",
12697 			      insret, rack, rsm);
12698 		}
12699 #endif
12700 		TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
12701 		rsm->r_in_tmap = 1;
12702 	}
12703 	/*
12704 	 * Timers in Rack are kept in microseconds so lets
12705 	 * convert any initial incoming variables
12706 	 * from ticks into usecs. Note that we
12707 	 * also change the values of t_srtt and t_rttvar, if
12708 	 * they are non-zero. They are kept with a 5
12709 	 * bit decimal so we have to carefully convert
12710 	 * these to get the full precision.
12711 	 */
12712 	rack_convert_rtts(tp);
12713 	tp->t_rttlow = TICKS_2_USEC(tp->t_rttlow);
12714 	if (rack_do_hystart) {
12715 		tp->ccv->flags |= CCF_HYSTART_ALLOWED;
12716 		if (rack_do_hystart > 1)
12717 			tp->ccv->flags |= CCF_HYSTART_CAN_SH_CWND;
12718 		if (rack_do_hystart > 2)
12719 			tp->ccv->flags |= CCF_HYSTART_CONS_SSTH;
12720 	}
12721 	if (rack_def_profile)
12722 		rack_set_profile(rack, rack_def_profile);
12723 	/* Cancel the GP measurement in progress */
12724 	tp->t_flags &= ~TF_GPUTINPROG;
12725 	if (SEQ_GT(tp->snd_max, tp->iss))
12726 		snt = tp->snd_max - tp->iss;
12727 	else
12728 		snt = 0;
12729 	iwin = rc_init_window(rack);
12730 	if (snt < iwin) {
12731 		/* We are not past the initial window
12732 		 * so we need to make sure cwnd is
12733 		 * correct.
12734 		 */
12735 		if (tp->snd_cwnd < iwin)
12736 			tp->snd_cwnd = iwin;
12737 		/*
12738 		 * If we are within the initial window
12739 		 * we want ssthresh to be unlimited. Setting
12740 		 * it to the rwnd (which the default stack does
12741 		 * and older racks) is not really a good idea
12742 		 * since we want to be in SS and grow both the
12743 		 * cwnd and the rwnd (via dynamic rwnd growth). If
12744 		 * we set it to the rwnd then as the peer grows its
12745 		 * rwnd we will be stuck in CA and never hit SS.
12746 		 *
12747 		 * Its far better to raise it up high (this takes the
12748 		 * risk that there as been a loss already, probably
12749 		 * we should have an indicator in all stacks of loss
12750 		 * but we don't), but considering the normal use this
12751 		 * is a risk worth taking. The consequences of not
12752 		 * hitting SS are far worse than going one more time
12753 		 * into it early on (before we have sent even a IW).
12754 		 * It is highly unlikely that we will have had a loss
12755 		 * before getting the IW out.
12756 		 */
12757 		tp->snd_ssthresh = 0xffffffff;
12758 	}
12759 	rack_stop_all_timers(tp);
12760 	/* Lets setup the fsb block */
12761 	rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
12762 	rack_log_rtt_shrinks(rack,  us_cts,  tp->t_rxtcur,
12763 			     __LINE__, RACK_RTTS_INIT);
12764 	return (0);
12765 }
12766 
12767 static int
12768 rack_handoff_ok(struct tcpcb *tp)
12769 {
12770 	if ((tp->t_state == TCPS_CLOSED) ||
12771 	    (tp->t_state == TCPS_LISTEN)) {
12772 		/* Sure no problem though it may not stick */
12773 		return (0);
12774 	}
12775 	if ((tp->t_state == TCPS_SYN_SENT) ||
12776 	    (tp->t_state == TCPS_SYN_RECEIVED)) {
12777 		/*
12778 		 * We really don't know if you support sack,
12779 		 * you have to get to ESTAB or beyond to tell.
12780 		 */
12781 		return (EAGAIN);
12782 	}
12783 	if ((tp->t_flags & TF_SENTFIN) && ((tp->snd_max - tp->snd_una) > 1)) {
12784 		/*
12785 		 * Rack will only send a FIN after all data is acknowledged.
12786 		 * So in this case we have more data outstanding. We can't
12787 		 * switch stacks until either all data and only the FIN
12788 		 * is left (in which case rack_init() now knows how
12789 		 * to deal with that) <or> all is acknowledged and we
12790 		 * are only left with incoming data, though why you
12791 		 * would want to switch to rack after all data is acknowledged
12792 		 * I have no idea (rrs)!
12793 		 */
12794 		return (EAGAIN);
12795 	}
12796 	if ((tp->t_flags & TF_SACK_PERMIT) || rack_sack_not_required){
12797 		return (0);
12798 	}
12799 	/*
12800 	 * If we reach here we don't do SACK on this connection so we can
12801 	 * never do rack.
12802 	 */
12803 	return (EINVAL);
12804 }
12805 
12806 
12807 static void
12808 rack_fini(struct tcpcb *tp, int32_t tcb_is_purged)
12809 {
12810 	if (tp->t_fb_ptr) {
12811 		struct tcp_rack *rack;
12812 		struct rack_sendmap *rsm, *nrsm;
12813 #ifdef INVARIANTS
12814 		struct rack_sendmap *rm;
12815 #endif
12816 
12817 		rack = (struct tcp_rack *)tp->t_fb_ptr;
12818 		if (tp->t_in_pkt) {
12819 			/*
12820 			 * It is unsafe to process the packets since a
12821 			 * reset may be lurking in them (its rare but it
12822 			 * can occur). If we were to find a RST, then we
12823 			 * would end up dropping the connection and the
12824 			 * INP lock, so when we return the caller (tcp_usrreq)
12825 			 * will blow up when it trys to unlock the inp.
12826 			 */
12827 			struct mbuf *save, *m;
12828 
12829 			m = tp->t_in_pkt;
12830 			tp->t_in_pkt = NULL;
12831 			tp->t_tail_pkt = NULL;
12832 			while (m) {
12833 				save = m->m_nextpkt;
12834 				m->m_nextpkt = NULL;
12835 				m_freem(m);
12836 				m = save;
12837 			}
12838 		}
12839 		tp->t_flags &= ~TF_FORCEDATA;
12840 #ifdef NETFLIX_SHARED_CWND
12841 		if (rack->r_ctl.rc_scw) {
12842 			uint32_t limit;
12843 
12844 			if (rack->r_limit_scw)
12845 				limit = max(1, rack->r_ctl.rc_lowest_us_rtt);
12846 			else
12847 				limit = 0;
12848 			tcp_shared_cwnd_free_full(tp, rack->r_ctl.rc_scw,
12849 						  rack->r_ctl.rc_scw_index,
12850 						  limit);
12851 			rack->r_ctl.rc_scw = NULL;
12852 		}
12853 #endif
12854 		if (rack->r_ctl.fsb.tcp_ip_hdr) {
12855 			free(rack->r_ctl.fsb.tcp_ip_hdr, M_TCPFSB);
12856 			rack->r_ctl.fsb.tcp_ip_hdr = NULL;
12857 			rack->r_ctl.fsb.th = NULL;
12858 		}
12859 		/* Convert back to ticks, with  */
12860 		if (tp->t_srtt > 1) {
12861 			uint32_t val, frac;
12862 
12863 			val = USEC_2_TICKS(tp->t_srtt);
12864 			frac = tp->t_srtt % (HPTS_USEC_IN_SEC / hz);
12865 			tp->t_srtt = val << TCP_RTT_SHIFT;
12866 			/*
12867 			 * frac is the fractional part here is left
12868 			 * over from converting to hz and shifting.
12869 			 * We need to convert this to the 5 bit
12870 			 * remainder.
12871 			 */
12872 			if (frac) {
12873 				if (hz == 1000) {
12874 					frac = (((uint64_t)frac *  (uint64_t)TCP_RTT_SCALE) / (uint64_t)HPTS_USEC_IN_MSEC);
12875 				} else {
12876 					frac = (((uint64_t)frac * (uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE) /(uint64_t)HPTS_USEC_IN_SEC);
12877 				}
12878 				tp->t_srtt += frac;
12879 			}
12880 		}
12881 		if (tp->t_rttvar) {
12882 			uint32_t val, frac;
12883 
12884 			val = USEC_2_TICKS(tp->t_rttvar);
12885 			frac = tp->t_srtt % (HPTS_USEC_IN_SEC / hz);
12886 			tp->t_rttvar = val <<  TCP_RTTVAR_SHIFT;
12887 			/*
12888 			 * frac is the fractional part here is left
12889 			 * over from converting to hz and shifting.
12890 			 * We need to convert this to the 5 bit
12891 			 * remainder.
12892 			 */
12893 			if (frac) {
12894 				if (hz == 1000) {
12895 					frac = (((uint64_t)frac *  (uint64_t)TCP_RTT_SCALE) / (uint64_t)HPTS_USEC_IN_MSEC);
12896 				} else {
12897 					frac = (((uint64_t)frac * (uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE) /(uint64_t)HPTS_USEC_IN_SEC);
12898 				}
12899 				tp->t_rttvar += frac;
12900 			}
12901 		}
12902 		tp->t_rxtcur = USEC_2_TICKS(tp->t_rxtcur);
12903 		tp->t_rttlow = USEC_2_TICKS(tp->t_rttlow);
12904 		if (rack->rc_always_pace) {
12905 			tcp_decrement_paced_conn();
12906 			rack_undo_cc_pacing(rack);
12907 			rack->rc_always_pace = 0;
12908 		}
12909 		/* Clean up any options if they were not applied */
12910 		while (!TAILQ_EMPTY(&rack->r_ctl.opt_list)) {
12911 			struct deferred_opt_list *dol;
12912 
12913 			dol = TAILQ_FIRST(&rack->r_ctl.opt_list);
12914 			TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next);
12915 			free(dol, M_TCPDO);
12916 		}
12917 		/* rack does not use force data but other stacks may clear it */
12918 		if (rack->r_ctl.crte != NULL) {
12919 			tcp_rel_pacing_rate(rack->r_ctl.crte, tp);
12920 			rack->rack_hdrw_pacing = 0;
12921 			rack->r_ctl.crte = NULL;
12922 		}
12923 #ifdef TCP_BLACKBOX
12924 		tcp_log_flowend(tp);
12925 #endif
12926 		RB_FOREACH_SAFE(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm) {
12927 #ifndef INVARIANTS
12928 			(void)RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
12929 #else
12930 			rm = RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
12931 			if (rm != rsm) {
12932 				panic("At fini, rack:%p rsm:%p rm:%p",
12933 				      rack, rsm, rm);
12934 			}
12935 #endif
12936 			uma_zfree(rack_zone, rsm);
12937 		}
12938 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
12939 		while (rsm) {
12940 			TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
12941 			uma_zfree(rack_zone, rsm);
12942 			rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
12943 		}
12944 		rack->rc_free_cnt = 0;
12945 		uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
12946 		tp->t_fb_ptr = NULL;
12947 	}
12948 	if (tp->t_inpcb) {
12949 		tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
12950 		tp->t_inpcb->inp_flags2 &= ~INP_MBUF_QUEUE_READY;
12951 		tp->t_inpcb->inp_flags2 &= ~INP_DONT_SACK_QUEUE;
12952 		tp->t_inpcb->inp_flags2 &= ~INP_MBUF_ACKCMP;
12953 		/* Cancel the GP measurement in progress */
12954 		tp->t_flags &= ~TF_GPUTINPROG;
12955 		tp->t_inpcb->inp_flags2 &= ~INP_MBUF_L_ACKS;
12956 	}
12957 	/* Make sure snd_nxt is correctly set */
12958 	tp->snd_nxt = tp->snd_max;
12959 }
12960 
12961 static void
12962 rack_set_state(struct tcpcb *tp, struct tcp_rack *rack)
12963 {
12964 	if ((rack->r_state == TCPS_CLOSED) && (tp->t_state != TCPS_CLOSED)) {
12965 		rack->r_is_v6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
12966 	}
12967 	switch (tp->t_state) {
12968 	case TCPS_SYN_SENT:
12969 		rack->r_state = TCPS_SYN_SENT;
12970 		rack->r_substate = rack_do_syn_sent;
12971 		break;
12972 	case TCPS_SYN_RECEIVED:
12973 		rack->r_state = TCPS_SYN_RECEIVED;
12974 		rack->r_substate = rack_do_syn_recv;
12975 		break;
12976 	case TCPS_ESTABLISHED:
12977 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
12978 		rack->r_state = TCPS_ESTABLISHED;
12979 		rack->r_substate = rack_do_established;
12980 		break;
12981 	case TCPS_CLOSE_WAIT:
12982 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
12983 		rack->r_state = TCPS_CLOSE_WAIT;
12984 		rack->r_substate = rack_do_close_wait;
12985 		break;
12986 	case TCPS_FIN_WAIT_1:
12987 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
12988 		rack->r_state = TCPS_FIN_WAIT_1;
12989 		rack->r_substate = rack_do_fin_wait_1;
12990 		break;
12991 	case TCPS_CLOSING:
12992 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
12993 		rack->r_state = TCPS_CLOSING;
12994 		rack->r_substate = rack_do_closing;
12995 		break;
12996 	case TCPS_LAST_ACK:
12997 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
12998 		rack->r_state = TCPS_LAST_ACK;
12999 		rack->r_substate = rack_do_lastack;
13000 		break;
13001 	case TCPS_FIN_WAIT_2:
13002 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
13003 		rack->r_state = TCPS_FIN_WAIT_2;
13004 		rack->r_substate = rack_do_fin_wait_2;
13005 		break;
13006 	case TCPS_LISTEN:
13007 	case TCPS_CLOSED:
13008 	case TCPS_TIME_WAIT:
13009 	default:
13010 		break;
13011 	};
13012 	if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
13013 		rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
13014 
13015 }
13016 
13017 static void
13018 rack_timer_audit(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb)
13019 {
13020 	/*
13021 	 * We received an ack, and then did not
13022 	 * call send or were bounced out due to the
13023 	 * hpts was running. Now a timer is up as well, is
13024 	 * it the right timer?
13025 	 */
13026 	struct rack_sendmap *rsm;
13027 	int tmr_up;
13028 
13029 	tmr_up = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
13030 	if (rack->rc_in_persist && (tmr_up == PACE_TMR_PERSIT))
13031 		return;
13032 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
13033 	if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) &&
13034 	    (tmr_up == PACE_TMR_RXT)) {
13035 		/* Should be an RXT */
13036 		return;
13037 	}
13038 	if (rsm == NULL) {
13039 		/* Nothing outstanding? */
13040 		if (tp->t_flags & TF_DELACK) {
13041 			if (tmr_up == PACE_TMR_DELACK)
13042 				/* We are supposed to have delayed ack up and we do */
13043 				return;
13044 		} else if (sbavail(&tp->t_inpcb->inp_socket->so_snd) && (tmr_up == PACE_TMR_RXT)) {
13045 			/*
13046 			 * if we hit enobufs then we would expect the possibility
13047 			 * of nothing outstanding and the RXT up (and the hptsi timer).
13048 			 */
13049 			return;
13050 		} else if (((V_tcp_always_keepalive ||
13051 			     rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
13052 			    (tp->t_state <= TCPS_CLOSING)) &&
13053 			   (tmr_up == PACE_TMR_KEEP) &&
13054 			   (tp->snd_max == tp->snd_una)) {
13055 			/* We should have keep alive up and we do */
13056 			return;
13057 		}
13058 	}
13059 	if (SEQ_GT(tp->snd_max, tp->snd_una) &&
13060 		   ((tmr_up == PACE_TMR_TLP) ||
13061 		    (tmr_up == PACE_TMR_RACK) ||
13062 		    (tmr_up == PACE_TMR_RXT))) {
13063 		/*
13064 		 * Either a Rack, TLP or RXT is fine if  we
13065 		 * have outstanding data.
13066 		 */
13067 		return;
13068 	} else if (tmr_up == PACE_TMR_DELACK) {
13069 		/*
13070 		 * If the delayed ack was going to go off
13071 		 * before the rtx/tlp/rack timer were going to
13072 		 * expire, then that would be the timer in control.
13073 		 * Note we don't check the time here trusting the
13074 		 * code is correct.
13075 		 */
13076 		return;
13077 	}
13078 	/*
13079 	 * Ok the timer originally started is not what we want now.
13080 	 * We will force the hpts to be stopped if any, and restart
13081 	 * with the slot set to what was in the saved slot.
13082 	 */
13083 	if (tcp_in_hpts(rack->rc_inp)) {
13084 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
13085 			uint32_t us_cts;
13086 
13087 			us_cts = tcp_get_usecs(NULL);
13088 			if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
13089 				rack->r_early = 1;
13090 				rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
13091 			}
13092 			rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
13093 		}
13094 		tcp_hpts_remove(tp->t_inpcb);
13095 	}
13096 	rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
13097 	rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
13098 }
13099 
13100 
13101 static void
13102 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)
13103 {
13104 	if ((SEQ_LT(tp->snd_wl1, seq) ||
13105 	    (tp->snd_wl1 == seq && (SEQ_LT(tp->snd_wl2, ack) ||
13106 	    (tp->snd_wl2 == ack && tiwin > tp->snd_wnd))))) {
13107 		/* keep track of pure window updates */
13108 		if ((tp->snd_wl2 == ack) && (tiwin > tp->snd_wnd))
13109 			KMOD_TCPSTAT_INC(tcps_rcvwinupd);
13110 		tp->snd_wnd = tiwin;
13111 		rack_validate_fo_sendwin_up(tp, rack);
13112 		tp->snd_wl1 = seq;
13113 		tp->snd_wl2 = ack;
13114 		if (tp->snd_wnd > tp->max_sndwnd)
13115 			tp->max_sndwnd = tp->snd_wnd;
13116 	    rack->r_wanted_output = 1;
13117 	} else if ((tp->snd_wl2 == ack) && (tiwin < tp->snd_wnd)) {
13118 		tp->snd_wnd = tiwin;
13119 		rack_validate_fo_sendwin_up(tp, rack);
13120 		tp->snd_wl1 = seq;
13121 		tp->snd_wl2 = ack;
13122 	} else {
13123 		/* Not a valid win update */
13124 		return;
13125 	}
13126 	/* Do we exit persists? */
13127 	if ((rack->rc_in_persist != 0) &&
13128 	    (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
13129 				rack->r_ctl.rc_pace_min_segs))) {
13130 		rack_exit_persist(tp, rack, cts);
13131 	}
13132 	/* Do we enter persists? */
13133 	if ((rack->rc_in_persist == 0) &&
13134 	    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
13135 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
13136 	    ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) &&
13137 	    sbavail(&tp->t_inpcb->inp_socket->so_snd) &&
13138 	    (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) {
13139 		/*
13140 		 * Here the rwnd is less than
13141 		 * the pacing size, we are established,
13142 		 * nothing is outstanding, and there is
13143 		 * data to send. Enter persists.
13144 		 */
13145 		rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime);
13146 	}
13147 }
13148 
13149 static void
13150 rack_log_input_packet(struct tcpcb *tp, struct tcp_rack *rack, struct tcp_ackent *ae, int ackval, uint32_t high_seq)
13151 {
13152 
13153 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
13154 		union tcp_log_stackspecific log;
13155 		struct timeval ltv;
13156 		char tcp_hdr_buf[60];
13157 		struct tcphdr *th;
13158 		struct timespec ts;
13159 		uint32_t orig_snd_una;
13160 		uint8_t xx = 0;
13161 
13162 #ifdef NETFLIX_HTTP_LOGGING
13163 		struct http_sendfile_track *http_req;
13164 
13165 		if (SEQ_GT(ae->ack, tp->snd_una)) {
13166 			http_req = tcp_http_find_req_for_seq(tp, (ae->ack-1));
13167 		} else {
13168 			http_req = tcp_http_find_req_for_seq(tp, ae->ack);
13169 		}
13170 #endif
13171 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
13172 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
13173 		if (rack->rack_no_prr == 0)
13174 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
13175 		else
13176 			log.u_bbr.flex1 = 0;
13177 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
13178 		log.u_bbr.use_lt_bw <<= 1;
13179 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
13180 		log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced;
13181 		log.u_bbr.inflight = ctf_flight_size(tp, rack->r_ctl.rc_sacked);
13182 		log.u_bbr.pkts_out = tp->t_maxseg;
13183 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
13184 		log.u_bbr.flex7 = 1;
13185 		log.u_bbr.lost = ae->flags;
13186 		log.u_bbr.cwnd_gain = ackval;
13187 		log.u_bbr.pacing_gain = 0x2;
13188 		if (ae->flags & TSTMP_HDWR) {
13189 			/* Record the hardware timestamp if present */
13190 			log.u_bbr.flex3 = M_TSTMP;
13191 			ts.tv_sec = ae->timestamp / 1000000000;
13192 			ts.tv_nsec = ae->timestamp % 1000000000;
13193 			ltv.tv_sec = ts.tv_sec;
13194 			ltv.tv_usec = ts.tv_nsec / 1000;
13195 			log.u_bbr.lt_epoch = tcp_tv_to_usectick(&ltv);
13196 		} else if (ae->flags & TSTMP_LRO) {
13197 			/* Record the LRO the arrival timestamp */
13198 			log.u_bbr.flex3 = M_TSTMP_LRO;
13199 			ts.tv_sec = ae->timestamp / 1000000000;
13200 			ts.tv_nsec = ae->timestamp % 1000000000;
13201 			ltv.tv_sec = ts.tv_sec;
13202 			ltv.tv_usec = ts.tv_nsec / 1000;
13203 			log.u_bbr.flex5 = tcp_tv_to_usectick(&ltv);
13204 		}
13205 		log.u_bbr.timeStamp = tcp_get_usecs(&ltv);
13206 		/* Log the rcv time */
13207 		log.u_bbr.delRate = ae->timestamp;
13208 #ifdef NETFLIX_HTTP_LOGGING
13209 		log.u_bbr.applimited = tp->t_http_closed;
13210 		log.u_bbr.applimited <<= 8;
13211 		log.u_bbr.applimited |= tp->t_http_open;
13212 		log.u_bbr.applimited <<= 8;
13213 		log.u_bbr.applimited |= tp->t_http_req;
13214 		if (http_req) {
13215 			/* Copy out any client req info */
13216 			/* seconds */
13217 			log.u_bbr.pkt_epoch = (http_req->localtime / HPTS_USEC_IN_SEC);
13218 			/* useconds */
13219 			log.u_bbr.delivered = (http_req->localtime % HPTS_USEC_IN_SEC);
13220 			log.u_bbr.rttProp = http_req->timestamp;
13221 			log.u_bbr.cur_del_rate = http_req->start;
13222 			if (http_req->flags & TCP_HTTP_TRACK_FLG_OPEN) {
13223 				log.u_bbr.flex8 |= 1;
13224 			} else {
13225 				log.u_bbr.flex8 |= 2;
13226 				log.u_bbr.bw_inuse = http_req->end;
13227 			}
13228 			log.u_bbr.flex6 = http_req->start_seq;
13229 			if (http_req->flags & TCP_HTTP_TRACK_FLG_COMP) {
13230 				log.u_bbr.flex8 |= 4;
13231 				log.u_bbr.epoch = http_req->end_seq;
13232 			}
13233 		}
13234 #endif
13235 		memset(tcp_hdr_buf, 0, sizeof(tcp_hdr_buf));
13236 		th = (struct tcphdr *)tcp_hdr_buf;
13237 		th->th_seq = ae->seq;
13238 		th->th_ack = ae->ack;
13239 		th->th_win = ae->win;
13240 		/* Now fill in the ports */
13241 		th->th_sport = tp->t_inpcb->inp_fport;
13242 		th->th_dport = tp->t_inpcb->inp_lport;
13243 		tcp_set_flags(th, ae->flags);
13244 		/* Now do we have a timestamp option? */
13245 		if (ae->flags & HAS_TSTMP) {
13246 			u_char *cp;
13247 			uint32_t val;
13248 
13249 			th->th_off = ((sizeof(struct tcphdr) + TCPOLEN_TSTAMP_APPA) >> 2);
13250 			cp = (u_char *)(th + 1);
13251 			*cp = TCPOPT_NOP;
13252 			cp++;
13253 			*cp = TCPOPT_NOP;
13254 			cp++;
13255 			*cp = TCPOPT_TIMESTAMP;
13256 			cp++;
13257 			*cp = TCPOLEN_TIMESTAMP;
13258 			cp++;
13259 			val = htonl(ae->ts_value);
13260 			bcopy((char *)&val,
13261 			      (char *)cp, sizeof(uint32_t));
13262 			val = htonl(ae->ts_echo);
13263 			bcopy((char *)&val,
13264 			      (char *)(cp + 4), sizeof(uint32_t));
13265 		} else
13266 			th->th_off = (sizeof(struct tcphdr) >> 2);
13267 
13268 		/*
13269 		 * For sane logging we need to play a little trick.
13270 		 * If the ack were fully processed we would have moved
13271 		 * snd_una to high_seq, but since compressed acks are
13272 		 * processed in two phases, at this point (logging) snd_una
13273 		 * won't be advanced. So we would see multiple acks showing
13274 		 * the advancement. We can prevent that by "pretending" that
13275 		 * snd_una was advanced and then un-advancing it so that the
13276 		 * logging code has the right value for tlb_snd_una.
13277 		 */
13278 		if (tp->snd_una != high_seq) {
13279 			orig_snd_una = tp->snd_una;
13280 			tp->snd_una = high_seq;
13281 			xx = 1;
13282 		} else
13283 			xx = 0;
13284 		TCP_LOG_EVENTP(tp, th,
13285 			       &tp->t_inpcb->inp_socket->so_rcv,
13286 			       &tp->t_inpcb->inp_socket->so_snd, TCP_LOG_IN, 0,
13287 			       0, &log, true, &ltv);
13288 		if (xx) {
13289 			tp->snd_una = orig_snd_una;
13290 		}
13291 	}
13292 
13293 }
13294 
13295 static void
13296 rack_handle_probe_response(struct tcp_rack *rack, uint32_t tiwin, uint32_t us_cts)
13297 {
13298 	uint32_t us_rtt;
13299 	/*
13300 	 * A persist or keep-alive was forced out, update our
13301 	 * min rtt time. Note now worry about lost responses.
13302 	 * When a subsequent keep-alive or persist times out
13303 	 * and forced_ack is still on, then the last probe
13304 	 * was not responded to. In such cases we have a
13305 	 * sysctl that controls the behavior. Either we apply
13306 	 * the rtt but with reduced confidence (0). Or we just
13307 	 * plain don't apply the rtt estimate. Having data flow
13308 	 * will clear the probe_not_answered flag i.e. cum-ack
13309 	 * move forward <or> exiting and reentering persists.
13310 	 */
13311 
13312 	rack->forced_ack = 0;
13313 	rack->rc_tp->t_rxtshift = 0;
13314 	if ((rack->rc_in_persist &&
13315 	     (tiwin == rack->rc_tp->snd_wnd)) ||
13316 	    (rack->rc_in_persist == 0)) {
13317 		/*
13318 		 * In persists only apply the RTT update if this is
13319 		 * a response to our window probe. And that
13320 		 * means the rwnd sent must match the current
13321 		 * snd_wnd. If it does not, then we got a
13322 		 * window update ack instead. For keepalive
13323 		 * we allow the answer no matter what the window.
13324 		 *
13325 		 * Note that if the probe_not_answered is set then
13326 		 * the forced_ack_ts is the oldest one i.e. the first
13327 		 * probe sent that might have been lost. This assures
13328 		 * us that if we do calculate an RTT it is longer not
13329 		 * some short thing.
13330 		 */
13331 		if (rack->rc_in_persist)
13332 			counter_u64_add(rack_persists_acks, 1);
13333 		us_rtt = us_cts - rack->r_ctl.forced_ack_ts;
13334 		if (us_rtt == 0)
13335 			us_rtt = 1;
13336 		if (rack->probe_not_answered == 0) {
13337 			rack_apply_updated_usrtt(rack, us_rtt, us_cts);
13338 			tcp_rack_xmit_timer(rack, us_rtt, 0, us_rtt, 3, NULL, 1);
13339 		} else {
13340 			/* We have a retransmitted probe here too */
13341 			if (rack_apply_rtt_with_reduced_conf) {
13342 				rack_apply_updated_usrtt(rack, us_rtt, us_cts);
13343 				tcp_rack_xmit_timer(rack, us_rtt, 0, us_rtt, 0, NULL, 1);
13344 			}
13345 		}
13346 	}
13347 }
13348 
13349 static int
13350 rack_do_compressed_ack_processing(struct tcpcb *tp, struct socket *so, struct mbuf *m, int nxt_pkt, struct timeval *tv)
13351 {
13352 	/*
13353 	 * Handle a "special" compressed ack mbuf. Each incoming
13354 	 * ack has only four possible dispositions:
13355 	 *
13356 	 * A) It moves the cum-ack forward
13357 	 * B) It is behind the cum-ack.
13358 	 * C) It is a window-update ack.
13359 	 * D) It is a dup-ack.
13360 	 *
13361 	 * Note that we can have between 1 -> TCP_COMP_ACK_ENTRIES
13362 	 * in the incoming mbuf. We also need to still pay attention
13363 	 * to nxt_pkt since there may be another packet after this
13364 	 * one.
13365 	 */
13366 #ifdef TCP_ACCOUNTING
13367 	uint64_t ts_val;
13368 	uint64_t rdstc;
13369 #endif
13370 	int segsiz;
13371 	struct timespec ts;
13372 	struct tcp_rack *rack;
13373 	struct tcp_ackent *ae;
13374 	uint32_t tiwin, ms_cts, cts, acked, acked_amount, high_seq, win_seq, the_win, win_upd_ack;
13375 	int cnt, i, did_out, ourfinisacked = 0;
13376 	struct tcpopt to_holder, *to = NULL;
13377 #ifdef TCP_ACCOUNTING
13378 	int win_up_req = 0;
13379 #endif
13380 	int nsegs = 0;
13381 	int under_pacing = 1;
13382 	int recovery = 0;
13383 #ifdef TCP_ACCOUNTING
13384 	sched_pin();
13385 #endif
13386 	rack = (struct tcp_rack *)tp->t_fb_ptr;
13387 	if (rack->gp_ready &&
13388 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT))
13389 		under_pacing = 0;
13390 	else
13391 		under_pacing = 1;
13392 
13393 	if (rack->r_state != tp->t_state)
13394 		rack_set_state(tp, rack);
13395 	if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
13396 	    (tp->t_flags & TF_GPUTINPROG)) {
13397 		/*
13398 		 * We have a goodput in progress
13399 		 * and we have entered a late state.
13400 		 * Do we have enough data in the sb
13401 		 * to handle the GPUT request?
13402 		 */
13403 		uint32_t bytes;
13404 
13405 		bytes = tp->gput_ack - tp->gput_seq;
13406 		if (SEQ_GT(tp->gput_seq, tp->snd_una))
13407 			bytes += tp->gput_seq - tp->snd_una;
13408 		if (bytes > sbavail(&tp->t_inpcb->inp_socket->so_snd)) {
13409 			/*
13410 			 * There are not enough bytes in the socket
13411 			 * buffer that have been sent to cover this
13412 			 * measurement. Cancel it.
13413 			 */
13414 			rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
13415 						   rack->r_ctl.rc_gp_srtt /*flex1*/,
13416 						   tp->gput_seq,
13417 						   0, 0, 18, __LINE__, NULL, 0);
13418 			tp->t_flags &= ~TF_GPUTINPROG;
13419 		}
13420 	}
13421 	to = &to_holder;
13422 	to->to_flags = 0;
13423 	KASSERT((m->m_len >= sizeof(struct tcp_ackent)),
13424 		("tp:%p m_cmpack:%p with invalid len:%u", tp, m, m->m_len));
13425 	cnt = m->m_len / sizeof(struct tcp_ackent);
13426 	counter_u64_add(rack_multi_single_eq, cnt);
13427 	high_seq = tp->snd_una;
13428 	the_win = tp->snd_wnd;
13429 	win_seq = tp->snd_wl1;
13430 	win_upd_ack = tp->snd_wl2;
13431 	cts = tcp_tv_to_usectick(tv);
13432 	ms_cts = tcp_tv_to_mssectick(tv);
13433 	rack->r_ctl.rc_rcvtime = cts;
13434 	segsiz = ctf_fixed_maxseg(tp);
13435 	if ((rack->rc_gp_dyn_mul) &&
13436 	    (rack->use_fixed_rate == 0) &&
13437 	    (rack->rc_always_pace)) {
13438 		/* Check in on probertt */
13439 		rack_check_probe_rtt(rack, cts);
13440 	}
13441 	for (i = 0; i < cnt; i++) {
13442 #ifdef TCP_ACCOUNTING
13443 		ts_val = get_cyclecount();
13444 #endif
13445 		rack_clear_rate_sample(rack);
13446 		ae = ((mtod(m, struct tcp_ackent *)) + i);
13447 		/* Setup the window */
13448 		tiwin = ae->win << tp->snd_scale;
13449 		if (tiwin > rack->r_ctl.rc_high_rwnd)
13450 			rack->r_ctl.rc_high_rwnd = tiwin;
13451 		/* figure out the type of ack */
13452 		if (SEQ_LT(ae->ack, high_seq)) {
13453 			/* Case B*/
13454 			ae->ack_val_set = ACK_BEHIND;
13455 		} else if (SEQ_GT(ae->ack, high_seq)) {
13456 			/* Case A */
13457 			ae->ack_val_set = ACK_CUMACK;
13458 		} else if ((tiwin == the_win) && (rack->rc_in_persist == 0)){
13459 			/* Case D */
13460 			ae->ack_val_set = ACK_DUPACK;
13461 		} else {
13462 			/* Case C */
13463 			ae->ack_val_set = ACK_RWND;
13464 		}
13465 		rack_log_input_packet(tp, rack, ae, ae->ack_val_set, high_seq);
13466 		/* Validate timestamp */
13467 		if (ae->flags & HAS_TSTMP) {
13468 			/* Setup for a timestamp */
13469 			to->to_flags = TOF_TS;
13470 			ae->ts_echo -= tp->ts_offset;
13471 			to->to_tsecr = ae->ts_echo;
13472 			to->to_tsval = ae->ts_value;
13473 			/*
13474 			 * If echoed timestamp is later than the current time, fall back to
13475 			 * non RFC1323 RTT calculation.  Normalize timestamp if syncookies
13476 			 * were used when this connection was established.
13477 			 */
13478 			if (TSTMP_GT(ae->ts_echo, ms_cts))
13479 				to->to_tsecr = 0;
13480 			if (tp->ts_recent &&
13481 			    TSTMP_LT(ae->ts_value, tp->ts_recent)) {
13482 				if (ctf_ts_check_ac(tp, (ae->flags & 0xff))) {
13483 #ifdef TCP_ACCOUNTING
13484 					rdstc = get_cyclecount();
13485 					if (rdstc > ts_val) {
13486 						counter_u64_add(tcp_proc_time[ae->ack_val_set] ,
13487 								(rdstc - ts_val));
13488 						if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13489 							tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val);
13490 						}
13491 					}
13492 #endif
13493 					continue;
13494 				}
13495 			}
13496 			if (SEQ_LEQ(ae->seq, tp->last_ack_sent) &&
13497 			    SEQ_LEQ(tp->last_ack_sent, ae->seq)) {
13498 				tp->ts_recent_age = tcp_ts_getticks();
13499 				tp->ts_recent = ae->ts_value;
13500 			}
13501 		} else {
13502 			/* Setup for a no options */
13503 			to->to_flags = 0;
13504 		}
13505 		/* Update the rcv time and perform idle reduction possibly */
13506 		if  (tp->t_idle_reduce &&
13507 		     (tp->snd_max == tp->snd_una) &&
13508 		     (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur)) {
13509 			counter_u64_add(rack_input_idle_reduces, 1);
13510 			rack_cc_after_idle(rack, tp);
13511 		}
13512 		tp->t_rcvtime = ticks;
13513 		/* Now what about ECN? */
13514 		if (tcp_ecn_input_segment(tp, ae->flags, ae->codepoint))
13515 			rack_cong_signal(tp, CC_ECN, ae->ack, __LINE__);
13516 #ifdef TCP_ACCOUNTING
13517 		/* Count for the specific type of ack in */
13518 		counter_u64_add(tcp_cnt_counters[ae->ack_val_set], 1);
13519 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13520 			tp->tcp_cnt_counters[ae->ack_val_set]++;
13521 		}
13522 #endif
13523 		/*
13524 		 * Note how we could move up these in the determination
13525 		 * above, but we don't so that way the timestamp checks (and ECN)
13526 		 * is done first before we do any processing on the ACK.
13527 		 * The non-compressed path through the code has this
13528 		 * weakness (noted by @jtl) that it actually does some
13529 		 * processing before verifying the timestamp information.
13530 		 * We don't take that path here which is why we set
13531 		 * the ack_val_set first, do the timestamp and ecn
13532 		 * processing, and then look at what we have setup.
13533 		 */
13534 		if (ae->ack_val_set == ACK_BEHIND) {
13535 			/*
13536 			 * Case B flag reordering, if window is not closed
13537 			 * or it could be a keep-alive or persists
13538 			 */
13539 			if (SEQ_LT(ae->ack, tp->snd_una) && (sbspace(&so->so_rcv) > segsiz)) {
13540 				rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
13541 			}
13542 		} else if (ae->ack_val_set == ACK_DUPACK) {
13543 			/* Case D */
13544 			rack_strike_dupack(rack);
13545 		} else if (ae->ack_val_set == ACK_RWND) {
13546 			/* Case C */
13547 			if ((ae->flags & TSTMP_LRO) || (ae->flags & TSTMP_HDWR)) {
13548 				ts.tv_sec = ae->timestamp / 1000000000;
13549 				ts.tv_nsec = ae->timestamp % 1000000000;
13550 				rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
13551 				rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
13552 			} else {
13553 				rack->r_ctl.act_rcv_time = *tv;
13554 			}
13555 			if (rack->forced_ack) {
13556 				rack_handle_probe_response(rack, tiwin,
13557 							   tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time));
13558 			}
13559 #ifdef TCP_ACCOUNTING
13560 			win_up_req = 1;
13561 #endif
13562 			win_upd_ack = ae->ack;
13563 			win_seq = ae->seq;
13564 			the_win = tiwin;
13565 			rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts, high_seq);
13566 		} else {
13567 			/* Case A */
13568 			if (SEQ_GT(ae->ack, tp->snd_max)) {
13569 				/*
13570 				 * We just send an ack since the incoming
13571 				 * ack is beyond the largest seq we sent.
13572 				 */
13573 				if ((tp->t_flags & TF_ACKNOW) == 0) {
13574 					ctf_ack_war_checks(tp, &rack->r_ctl.challenge_ack_ts, &rack->r_ctl.challenge_ack_cnt);
13575 					if (tp->t_flags && TF_ACKNOW)
13576 						rack->r_wanted_output = 1;
13577 				}
13578 			} else {
13579 				nsegs++;
13580 				/* If the window changed setup to update */
13581 				if (tiwin != tp->snd_wnd) {
13582 					win_upd_ack = ae->ack;
13583 					win_seq = ae->seq;
13584 					the_win = tiwin;
13585 					rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts, high_seq);
13586 				}
13587 #ifdef TCP_ACCOUNTING
13588 				/* Account for the acks */
13589 				if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13590 					tp->tcp_cnt_counters[CNT_OF_ACKS_IN] += (((ae->ack - high_seq) + segsiz - 1) / segsiz);
13591 				}
13592 				counter_u64_add(tcp_cnt_counters[CNT_OF_ACKS_IN],
13593 						(((ae->ack - high_seq) + segsiz - 1) / segsiz));
13594 #endif
13595 				high_seq = ae->ack;
13596 				if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
13597 					union tcp_log_stackspecific log;
13598 					struct timeval tv;
13599 
13600 					memset(&log.u_bbr, 0, sizeof(log.u_bbr));
13601 					log.u_bbr.timeStamp = tcp_get_usecs(&tv);
13602 					log.u_bbr.flex1 = high_seq;
13603 					log.u_bbr.flex2 = rack->r_ctl.roundends;
13604 					log.u_bbr.flex3 = rack->r_ctl.current_round;
13605 					log.u_bbr.rttProp = (uint64_t)CC_ALGO(tp)->newround;
13606 					log.u_bbr.flex8 = 8;
13607 					tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
13608 						       0, &log, false, NULL, NULL, 0, &tv);
13609 				}
13610 				/*
13611 				 * The draft (v3) calls for us to use SEQ_GEQ, but that
13612 				 * causes issues when we are just going app limited. Lets
13613 				 * instead use SEQ_GT <or> where its equal but more data
13614 				 * is outstanding.
13615 				 */
13616 				if ((SEQ_GT(high_seq, rack->r_ctl.roundends)) ||
13617 				    ((high_seq == rack->r_ctl.roundends) &&
13618 				     SEQ_GT(tp->snd_max, tp->snd_una))) {
13619 					rack->r_ctl.current_round++;
13620 					rack->r_ctl.roundends = tp->snd_max;
13621 					if (CC_ALGO(tp)->newround != NULL) {
13622 						CC_ALGO(tp)->newround(tp->ccv, rack->r_ctl.current_round);
13623 					}
13624 				}
13625 				/* Setup our act_rcv_time */
13626 				if ((ae->flags & TSTMP_LRO) || (ae->flags & TSTMP_HDWR)) {
13627 					ts.tv_sec = ae->timestamp / 1000000000;
13628 					ts.tv_nsec = ae->timestamp % 1000000000;
13629 					rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
13630 					rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
13631 				} else {
13632 					rack->r_ctl.act_rcv_time = *tv;
13633 				}
13634 				rack_process_to_cumack(tp, rack, ae->ack, cts, to);
13635 				if (rack->rc_dsack_round_seen) {
13636 					/* Is the dsack round over? */
13637 					if (SEQ_GEQ(ae->ack, rack->r_ctl.dsack_round_end)) {
13638 						/* Yes it is */
13639 						rack->rc_dsack_round_seen = 0;
13640 						rack_log_dsack_event(rack, 3, __LINE__, 0, 0);
13641 					}
13642 				}
13643 			}
13644 		}
13645 		/* And lets be sure to commit the rtt measurements for this ack */
13646 		tcp_rack_xmit_timer_commit(rack, tp);
13647 #ifdef TCP_ACCOUNTING
13648 		rdstc = get_cyclecount();
13649 		if (rdstc > ts_val) {
13650 			counter_u64_add(tcp_proc_time[ae->ack_val_set] , (rdstc - ts_val));
13651 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13652 				tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val);
13653 				if (ae->ack_val_set == ACK_CUMACK)
13654 					tp->tcp_proc_time[CYC_HANDLE_MAP] += (rdstc - ts_val);
13655 			}
13656 		}
13657 #endif
13658 	}
13659 #ifdef TCP_ACCOUNTING
13660 	ts_val = get_cyclecount();
13661 #endif
13662 	/* Tend to any collapsed window */
13663 	if (SEQ_GT(tp->snd_max, high_seq) && (tp->snd_wnd < (tp->snd_max - high_seq))) {
13664 		/* The peer collapsed the window */
13665 		rack_collapsed_window(rack, (tp->snd_max - high_seq), __LINE__);
13666 	} else if (rack->rc_has_collapsed)
13667 		rack_un_collapse_window(rack, __LINE__);
13668 	if ((rack->r_collapse_point_valid) &&
13669 	    (SEQ_GT(high_seq, rack->r_ctl.high_collapse_point)))
13670 		rack->r_collapse_point_valid = 0;
13671 	acked_amount = acked = (high_seq - tp->snd_una);
13672 	if (acked) {
13673 		/*
13674 		 * Clear the probe not answered flag
13675 		 * since cum-ack moved forward.
13676 		 */
13677 		rack->probe_not_answered = 0;
13678 		if (rack->sack_attack_disable == 0)
13679 			rack_do_decay(rack);
13680 		if (acked >= segsiz) {
13681 			/*
13682 			 * You only get credit for
13683 			 * MSS and greater (and you get extra
13684 			 * credit for larger cum-ack moves).
13685 			 */
13686 			int ac;
13687 
13688 			ac = acked / segsiz;
13689 			rack->r_ctl.ack_count += ac;
13690 			counter_u64_add(rack_ack_total, ac);
13691 		}
13692 		if (rack->r_ctl.ack_count > 0xfff00000) {
13693 			/*
13694 			 * reduce the number to keep us under
13695 			 * a uint32_t.
13696 			 */
13697 			rack->r_ctl.ack_count /= 2;
13698 			rack->r_ctl.sack_count /= 2;
13699 		}
13700 		if (tp->t_flags & TF_NEEDSYN) {
13701 			/*
13702 			 * T/TCP: Connection was half-synchronized, and our SYN has
13703 			 * been ACK'd (so connection is now fully synchronized).  Go
13704 			 * to non-starred state, increment snd_una for ACK of SYN,
13705 			 * and check if we can do window scaling.
13706 			 */
13707 			tp->t_flags &= ~TF_NEEDSYN;
13708 			tp->snd_una++;
13709 			acked_amount = acked = (high_seq - tp->snd_una);
13710 		}
13711 		if (acked > sbavail(&so->so_snd))
13712 			acked_amount = sbavail(&so->so_snd);
13713 #ifdef NETFLIX_EXP_DETECTION
13714 		/*
13715 		 * We only care on a cum-ack move if we are in a sack-disabled
13716 		 * state. We have already added in to the ack_count, and we never
13717 		 * would disable on a cum-ack move, so we only care to do the
13718 		 * detection if it may "undo" it, i.e. we were in disabled already.
13719 		 */
13720 		if (rack->sack_attack_disable)
13721 			rack_do_detection(tp, rack, acked_amount, segsiz);
13722 #endif
13723 		if (IN_FASTRECOVERY(tp->t_flags) &&
13724 		    (rack->rack_no_prr == 0))
13725 			rack_update_prr(tp, rack, acked_amount, high_seq);
13726 		if (IN_RECOVERY(tp->t_flags)) {
13727 			if (SEQ_LT(high_seq, tp->snd_recover) &&
13728 			    (SEQ_LT(high_seq, tp->snd_max))) {
13729 				tcp_rack_partialack(tp);
13730 			} else {
13731 				rack_post_recovery(tp, high_seq);
13732 				recovery = 1;
13733 			}
13734 		}
13735 		/* Handle the rack-log-ack part (sendmap) */
13736 		if ((sbused(&so->so_snd) == 0) &&
13737 		    (acked > acked_amount) &&
13738 		    (tp->t_state >= TCPS_FIN_WAIT_1) &&
13739 		    (tp->t_flags & TF_SENTFIN)) {
13740 			/*
13741 			 * We must be sure our fin
13742 			 * was sent and acked (we can be
13743 			 * in FIN_WAIT_1 without having
13744 			 * sent the fin).
13745 			 */
13746 			ourfinisacked = 1;
13747 			/*
13748 			 * Lets make sure snd_una is updated
13749 			 * since most likely acked_amount = 0 (it
13750 			 * should be).
13751 			 */
13752 			tp->snd_una = high_seq;
13753 		}
13754 		/* Did we make a RTO error? */
13755 		if ((tp->t_flags & TF_PREVVALID) &&
13756 		    ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
13757 			tp->t_flags &= ~TF_PREVVALID;
13758 			if (tp->t_rxtshift == 1 &&
13759 			    (int)(ticks - tp->t_badrxtwin) < 0)
13760 				rack_cong_signal(tp, CC_RTO_ERR, high_seq, __LINE__);
13761 		}
13762 		/* Handle the data in the socket buffer */
13763 		KMOD_TCPSTAT_ADD(tcps_rcvackpack, 1);
13764 		KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
13765 		if (acked_amount > 0) {
13766 			struct mbuf *mfree;
13767 
13768 			rack_ack_received(tp, rack, high_seq, nsegs, CC_ACK, recovery);
13769 			SOCKBUF_LOCK(&so->so_snd);
13770 			mfree = sbcut_locked(&so->so_snd, acked_amount);
13771 			tp->snd_una = high_seq;
13772 			/* Note we want to hold the sb lock through the sendmap adjust */
13773 			rack_adjust_sendmap(rack, &so->so_snd, tp->snd_una);
13774 			/* Wake up the socket if we have room to write more */
13775 			rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
13776 			sowwakeup_locked(so);
13777 			m_freem(mfree);
13778 		}
13779 		/* update progress */
13780 		tp->t_acktime = ticks;
13781 		rack_log_progress_event(rack, tp, tp->t_acktime,
13782 					PROGRESS_UPDATE, __LINE__);
13783 		/* Clear out shifts and such */
13784 		tp->t_rxtshift = 0;
13785 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
13786 				   rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
13787 		rack->rc_tlp_in_progress = 0;
13788 		rack->r_ctl.rc_tlp_cnt_out = 0;
13789 		/* Send recover and snd_nxt must be dragged along */
13790 		if (SEQ_GT(tp->snd_una, tp->snd_recover))
13791 			tp->snd_recover = tp->snd_una;
13792 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
13793 			tp->snd_nxt = tp->snd_una;
13794 		/*
13795 		 * If the RXT timer is running we want to
13796 		 * stop it, so we can restart a TLP (or new RXT).
13797 		 */
13798 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
13799 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
13800 #ifdef NETFLIX_HTTP_LOGGING
13801 		tcp_http_check_for_comp(rack->rc_tp, high_seq);
13802 #endif
13803 		tp->snd_wl2 = high_seq;
13804 		tp->t_dupacks = 0;
13805 		if (under_pacing &&
13806 		    (rack->use_fixed_rate == 0) &&
13807 		    (rack->in_probe_rtt == 0) &&
13808 		    rack->rc_gp_dyn_mul &&
13809 		    rack->rc_always_pace) {
13810 			/* Check if we are dragging bottom */
13811 			rack_check_bottom_drag(tp, rack, so, acked);
13812 		}
13813 		if (tp->snd_una == tp->snd_max) {
13814 			tp->t_flags &= ~TF_PREVVALID;
13815 			rack->r_ctl.retran_during_recovery = 0;
13816 			rack->r_ctl.dsack_byte_cnt = 0;
13817 			rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
13818 			if (rack->r_ctl.rc_went_idle_time == 0)
13819 				rack->r_ctl.rc_went_idle_time = 1;
13820 			rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
13821 			if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0)
13822 				tp->t_acktime = 0;
13823 			/* Set so we might enter persists... */
13824 			rack->r_wanted_output = 1;
13825 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
13826 			sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
13827 			if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
13828 			    (sbavail(&so->so_snd) == 0) &&
13829 			    (tp->t_flags2 & TF2_DROP_AF_DATA)) {
13830 				/*
13831 				 * The socket was gone and the
13832 				 * peer sent data (not now in the past), time to
13833 				 * reset him.
13834 				 */
13835 				rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
13836 				/* tcp_close will kill the inp pre-log the Reset */
13837 				tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
13838 #ifdef TCP_ACCOUNTING
13839 				rdstc = get_cyclecount();
13840 				if (rdstc > ts_val) {
13841 					counter_u64_add(tcp_proc_time[ACK_CUMACK] , (rdstc - ts_val));
13842 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13843 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
13844 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
13845 					}
13846 				}
13847 #endif
13848 				m_freem(m);
13849 				tp = tcp_close(tp);
13850 				if (tp == NULL) {
13851 #ifdef TCP_ACCOUNTING
13852 					sched_unpin();
13853 #endif
13854 					return (1);
13855 				}
13856 				/*
13857 				 * We would normally do drop-with-reset which would
13858 				 * send back a reset. We can't since we don't have
13859 				 * all the needed bits. Instead lets arrange for
13860 				 * a call to tcp_output(). That way since we
13861 				 * are in the closed state we will generate a reset.
13862 				 *
13863 				 * Note if tcp_accounting is on we don't unpin since
13864 				 * we do that after the goto label.
13865 				 */
13866 				goto send_out_a_rst;
13867 			}
13868 			if ((sbused(&so->so_snd) == 0) &&
13869 			    (tp->t_state >= TCPS_FIN_WAIT_1) &&
13870 			    (tp->t_flags & TF_SENTFIN)) {
13871 				/*
13872 				 * If we can't receive any more data, then closing user can
13873 				 * proceed. Starting the timer is contrary to the
13874 				 * specification, but if we don't get a FIN we'll hang
13875 				 * forever.
13876 				 *
13877 				 */
13878 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
13879 					soisdisconnected(so);
13880 					tcp_timer_activate(tp, TT_2MSL,
13881 							   (tcp_fast_finwait2_recycle ?
13882 							    tcp_finwait2_timeout :
13883 							    TP_MAXIDLE(tp)));
13884 				}
13885 				if (ourfinisacked == 0) {
13886 					/*
13887 					 * We don't change to fin-wait-2 if we have our fin acked
13888 					 * which means we are probably in TCPS_CLOSING.
13889 					 */
13890 					tcp_state_change(tp, TCPS_FIN_WAIT_2);
13891 				}
13892 			}
13893 		}
13894 		/* Wake up the socket if we have room to write more */
13895 		if (sbavail(&so->so_snd)) {
13896 			rack->r_wanted_output = 1;
13897 			if (ctf_progress_timeout_check(tp, true)) {
13898 				rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
13899 							tp, tick, PROGRESS_DROP, __LINE__);
13900 				/*
13901 				 * We cheat here and don't send a RST, we should send one
13902 				 * when the pacer drops the connection.
13903 				 */
13904 #ifdef TCP_ACCOUNTING
13905 				rdstc = get_cyclecount();
13906 				if (rdstc > ts_val) {
13907 					counter_u64_add(tcp_proc_time[ACK_CUMACK] , (rdstc - ts_val));
13908 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13909 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
13910 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
13911 					}
13912 				}
13913 				sched_unpin();
13914 #endif
13915 				(void)tcp_drop(tp, ETIMEDOUT);
13916 				m_freem(m);
13917 				return (1);
13918 			}
13919 		}
13920 		if (ourfinisacked) {
13921 			switch(tp->t_state) {
13922 			case TCPS_CLOSING:
13923 #ifdef TCP_ACCOUNTING
13924 				rdstc = get_cyclecount();
13925 				if (rdstc > ts_val) {
13926 					counter_u64_add(tcp_proc_time[ACK_CUMACK] ,
13927 							(rdstc - ts_val));
13928 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13929 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
13930 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
13931 					}
13932 				}
13933 				sched_unpin();
13934 #endif
13935 				tcp_twstart(tp);
13936 				m_freem(m);
13937 				return (1);
13938 				break;
13939 			case TCPS_LAST_ACK:
13940 #ifdef TCP_ACCOUNTING
13941 				rdstc = get_cyclecount();
13942 				if (rdstc > ts_val) {
13943 					counter_u64_add(tcp_proc_time[ACK_CUMACK] ,
13944 							(rdstc - ts_val));
13945 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13946 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
13947 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
13948 					}
13949 				}
13950 				sched_unpin();
13951 #endif
13952 				tp = tcp_close(tp);
13953 				ctf_do_drop(m, tp);
13954 				return (1);
13955 				break;
13956 			case TCPS_FIN_WAIT_1:
13957 #ifdef TCP_ACCOUNTING
13958 				rdstc = get_cyclecount();
13959 				if (rdstc > ts_val) {
13960 					counter_u64_add(tcp_proc_time[ACK_CUMACK] ,
13961 							(rdstc - ts_val));
13962 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13963 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
13964 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
13965 					}
13966 				}
13967 #endif
13968 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
13969 					soisdisconnected(so);
13970 					tcp_timer_activate(tp, TT_2MSL,
13971 							   (tcp_fast_finwait2_recycle ?
13972 							    tcp_finwait2_timeout :
13973 							    TP_MAXIDLE(tp)));
13974 				}
13975 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
13976 				break;
13977 			default:
13978 				break;
13979 			}
13980 		}
13981 		if (rack->r_fast_output) {
13982 			/*
13983 			 * We re doing fast output.. can we expand that?
13984 			 */
13985 			rack_gain_for_fastoutput(rack, tp, so, acked_amount);
13986 		}
13987 #ifdef TCP_ACCOUNTING
13988 		rdstc = get_cyclecount();
13989 		if (rdstc > ts_val) {
13990 			counter_u64_add(tcp_proc_time[ACK_CUMACK] , (rdstc - ts_val));
13991 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13992 				tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
13993 				tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
13994 			}
13995 		}
13996 
13997 	} else if (win_up_req) {
13998 		rdstc = get_cyclecount();
13999 		if (rdstc > ts_val) {
14000 			counter_u64_add(tcp_proc_time[ACK_RWND] , (rdstc - ts_val));
14001 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
14002 				tp->tcp_proc_time[ACK_RWND] += (rdstc - ts_val);
14003 			}
14004 		}
14005 #endif
14006 	}
14007 	/* Now is there a next packet, if so we are done */
14008 	m_freem(m);
14009 	did_out = 0;
14010 	if (nxt_pkt) {
14011 #ifdef TCP_ACCOUNTING
14012 		sched_unpin();
14013 #endif
14014 		rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 5, nsegs);
14015 		return (0);
14016 	}
14017 	rack_handle_might_revert(tp, rack);
14018 	ctf_calc_rwin(so, tp);
14019 	if ((rack->r_wanted_output != 0) || (rack->r_fast_output != 0)) {
14020 	send_out_a_rst:
14021 		if (tcp_output(tp) < 0) {
14022 #ifdef TCP_ACCOUNTING
14023 			sched_unpin();
14024 #endif
14025 			return (1);
14026 		}
14027 		did_out = 1;
14028 	}
14029 	rack_free_trim(rack);
14030 #ifdef TCP_ACCOUNTING
14031 	sched_unpin();
14032 #endif
14033 	rack_timer_audit(tp, rack, &so->so_snd);
14034 	rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 6, nsegs);
14035 	return (0);
14036 }
14037 
14038 
14039 static int
14040 rack_do_segment_nounlock(struct mbuf *m, struct tcphdr *th, struct socket *so,
14041     struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos,
14042     int32_t nxt_pkt, struct timeval *tv)
14043 {
14044 #ifdef TCP_ACCOUNTING
14045 	uint64_t ts_val;
14046 #endif
14047 	int32_t thflags, retval, did_out = 0;
14048 	int32_t way_out = 0;
14049 	/*
14050 	 * cts - is the current time from tv (caller gets ts) in microseconds.
14051 	 * ms_cts - is the current time from tv in milliseconds.
14052 	 * us_cts - is the time that LRO or hardware actually got the packet in microseconds.
14053 	 */
14054 	uint32_t cts, us_cts, ms_cts;
14055 	uint32_t tiwin, high_seq;
14056 	struct timespec ts;
14057 	struct tcpopt to;
14058 	struct tcp_rack *rack;
14059 	struct rack_sendmap *rsm;
14060 	int32_t prev_state = 0;
14061 #ifdef TCP_ACCOUNTING
14062 	int ack_val_set = 0xf;
14063 #endif
14064 	int nsegs;
14065 	/*
14066 	 * tv passed from common code is from either M_TSTMP_LRO or
14067 	 * tcp_get_usecs() if no LRO m_pkthdr timestamp is present.
14068 	 */
14069 	rack = (struct tcp_rack *)tp->t_fb_ptr;
14070 	if (m->m_flags & M_ACKCMP) {
14071 		return (rack_do_compressed_ack_processing(tp, so, m, nxt_pkt, tv));
14072 	}
14073 	if (m->m_flags & M_ACKCMP) {
14074 		panic("Impossible reach m has ackcmp? m:%p tp:%p", m, tp);
14075 	}
14076 	cts = tcp_tv_to_usectick(tv);
14077 	ms_cts =  tcp_tv_to_mssectick(tv);
14078 	nsegs = m->m_pkthdr.lro_nsegs;
14079 	counter_u64_add(rack_proc_non_comp_ack, 1);
14080 	thflags = tcp_get_flags(th);
14081 #ifdef TCP_ACCOUNTING
14082 	sched_pin();
14083 	if (thflags & TH_ACK)
14084 		ts_val = get_cyclecount();
14085 #endif
14086 	if ((m->m_flags & M_TSTMP) ||
14087 	    (m->m_flags & M_TSTMP_LRO)) {
14088 		mbuf_tstmp2timespec(m, &ts);
14089 		rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
14090 		rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
14091 	} else
14092 		rack->r_ctl.act_rcv_time = *tv;
14093 	kern_prefetch(rack, &prev_state);
14094 	prev_state = 0;
14095 	/*
14096 	 * Unscale the window into a 32-bit value. For the SYN_SENT state
14097 	 * the scale is zero.
14098 	 */
14099 	tiwin = th->th_win << tp->snd_scale;
14100 #ifdef TCP_ACCOUNTING
14101 	if (thflags & TH_ACK) {
14102 		/*
14103 		 * We have a tradeoff here. We can either do what we are
14104 		 * doing i.e. pinning to this CPU and then doing the accounting
14105 		 * <or> we could do a critical enter, setup the rdtsc and cpu
14106 		 * as in below, and then validate we are on the same CPU on
14107 		 * exit. I have choosen to not do the critical enter since
14108 		 * that often will gain you a context switch, and instead lock
14109 		 * us (line above this if) to the same CPU with sched_pin(). This
14110 		 * means we may be context switched out for a higher priority
14111 		 * interupt but we won't be moved to another CPU.
14112 		 *
14113 		 * If this occurs (which it won't very often since we most likely
14114 		 * are running this code in interupt context and only a higher
14115 		 * priority will bump us ... clock?) we will falsely add in
14116 		 * to the time the interupt processing time plus the ack processing
14117 		 * time. This is ok since its a rare event.
14118 		 */
14119 		ack_val_set = tcp_do_ack_accounting(tp, th, &to, tiwin,
14120 						    ctf_fixed_maxseg(tp));
14121 	}
14122 #endif
14123 	/*
14124 	 * Parse options on any incoming segment.
14125 	 */
14126 	memset(&to, 0, sizeof(to));
14127 	tcp_dooptions(&to, (u_char *)(th + 1),
14128 	    (th->th_off << 2) - sizeof(struct tcphdr),
14129 	    (thflags & TH_SYN) ? TO_SYN : 0);
14130 	NET_EPOCH_ASSERT();
14131 	INP_WLOCK_ASSERT(tp->t_inpcb);
14132 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
14133 	    __func__));
14134 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
14135 	    __func__));
14136 	if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
14137 	    (tp->t_flags & TF_GPUTINPROG)) {
14138 		/*
14139 		 * We have a goodput in progress
14140 		 * and we have entered a late state.
14141 		 * Do we have enough data in the sb
14142 		 * to handle the GPUT request?
14143 		 */
14144 		uint32_t bytes;
14145 
14146 		bytes = tp->gput_ack - tp->gput_seq;
14147 		if (SEQ_GT(tp->gput_seq, tp->snd_una))
14148 			bytes += tp->gput_seq - tp->snd_una;
14149 		if (bytes > sbavail(&tp->t_inpcb->inp_socket->so_snd)) {
14150 			/*
14151 			 * There are not enough bytes in the socket
14152 			 * buffer that have been sent to cover this
14153 			 * measurement. Cancel it.
14154 			 */
14155 			rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
14156 						   rack->r_ctl.rc_gp_srtt /*flex1*/,
14157 						   tp->gput_seq,
14158 						   0, 0, 18, __LINE__, NULL, 0);
14159 			tp->t_flags &= ~TF_GPUTINPROG;
14160 		}
14161 	}
14162 	high_seq = th->th_ack;
14163 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
14164 		union tcp_log_stackspecific log;
14165 		struct timeval ltv;
14166 #ifdef NETFLIX_HTTP_LOGGING
14167 		struct http_sendfile_track *http_req;
14168 
14169 		if (SEQ_GT(th->th_ack, tp->snd_una)) {
14170 			http_req = tcp_http_find_req_for_seq(tp, (th->th_ack-1));
14171 		} else {
14172 			http_req = tcp_http_find_req_for_seq(tp, th->th_ack);
14173 		}
14174 #endif
14175 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
14176 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
14177 		if (rack->rack_no_prr == 0)
14178 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
14179 		else
14180 			log.u_bbr.flex1 = 0;
14181 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
14182 		log.u_bbr.use_lt_bw <<= 1;
14183 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
14184 		log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced;
14185 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
14186 		log.u_bbr.pkts_out = rack->rc_tp->t_maxseg;
14187 		log.u_bbr.flex3 = m->m_flags;
14188 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
14189 		log.u_bbr.lost = thflags;
14190 		log.u_bbr.pacing_gain = 0x1;
14191 #ifdef TCP_ACCOUNTING
14192 		log.u_bbr.cwnd_gain = ack_val_set;
14193 #endif
14194 		log.u_bbr.flex7 = 2;
14195 		if (m->m_flags & M_TSTMP) {
14196 			/* Record the hardware timestamp if present */
14197 			mbuf_tstmp2timespec(m, &ts);
14198 			ltv.tv_sec = ts.tv_sec;
14199 			ltv.tv_usec = ts.tv_nsec / 1000;
14200 			log.u_bbr.lt_epoch = tcp_tv_to_usectick(&ltv);
14201 		} else if (m->m_flags & M_TSTMP_LRO) {
14202 			/* Record the LRO the arrival timestamp */
14203 			mbuf_tstmp2timespec(m, &ts);
14204 			ltv.tv_sec = ts.tv_sec;
14205 			ltv.tv_usec = ts.tv_nsec / 1000;
14206 			log.u_bbr.flex5 = tcp_tv_to_usectick(&ltv);
14207 		}
14208 		log.u_bbr.timeStamp = tcp_get_usecs(&ltv);
14209 		/* Log the rcv time */
14210 		log.u_bbr.delRate = m->m_pkthdr.rcv_tstmp;
14211 #ifdef NETFLIX_HTTP_LOGGING
14212 		log.u_bbr.applimited = tp->t_http_closed;
14213 		log.u_bbr.applimited <<= 8;
14214 		log.u_bbr.applimited |= tp->t_http_open;
14215 		log.u_bbr.applimited <<= 8;
14216 		log.u_bbr.applimited |= tp->t_http_req;
14217 		if (http_req) {
14218 			/* Copy out any client req info */
14219 			/* seconds */
14220 			log.u_bbr.pkt_epoch = (http_req->localtime / HPTS_USEC_IN_SEC);
14221 			/* useconds */
14222 			log.u_bbr.delivered = (http_req->localtime % HPTS_USEC_IN_SEC);
14223 			log.u_bbr.rttProp = http_req->timestamp;
14224 			log.u_bbr.cur_del_rate = http_req->start;
14225 			if (http_req->flags & TCP_HTTP_TRACK_FLG_OPEN) {
14226 				log.u_bbr.flex8 |= 1;
14227 			} else {
14228 				log.u_bbr.flex8 |= 2;
14229 				log.u_bbr.bw_inuse = http_req->end;
14230 			}
14231 			log.u_bbr.flex6 = http_req->start_seq;
14232 			if (http_req->flags & TCP_HTTP_TRACK_FLG_COMP) {
14233 				log.u_bbr.flex8 |= 4;
14234 				log.u_bbr.epoch = http_req->end_seq;
14235 			}
14236 		}
14237 #endif
14238 		TCP_LOG_EVENTP(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
14239 		    tlen, &log, true, &ltv);
14240 	}
14241 	if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
14242 		way_out = 4;
14243 		retval = 0;
14244 		m_freem(m);
14245 		goto done_with_input;
14246 	}
14247 	/*
14248 	 * If a segment with the ACK-bit set arrives in the SYN-SENT state
14249 	 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9.
14250 	 */
14251 	if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
14252 	    (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
14253 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
14254 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
14255 #ifdef TCP_ACCOUNTING
14256 		sched_unpin();
14257 #endif
14258 		return (1);
14259 	}
14260 	/*
14261 	 * If timestamps were negotiated during SYN/ACK and a
14262 	 * segment without a timestamp is received, silently drop
14263 	 * the segment, unless it is a RST segment or missing timestamps are
14264 	 * tolerated.
14265 	 * See section 3.2 of RFC 7323.
14266 	 */
14267 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) &&
14268 	    ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) {
14269 		way_out = 5;
14270 		retval = 0;
14271 		m_freem(m);
14272 		goto done_with_input;
14273 	}
14274 
14275 	/*
14276 	 * Segment received on connection. Reset idle time and keep-alive
14277 	 * timer. XXX: This should be done after segment validation to
14278 	 * ignore broken/spoofed segs.
14279 	 */
14280 	if  (tp->t_idle_reduce &&
14281 	     (tp->snd_max == tp->snd_una) &&
14282 	     (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur)) {
14283 		counter_u64_add(rack_input_idle_reduces, 1);
14284 		rack_cc_after_idle(rack, tp);
14285 	}
14286 	tp->t_rcvtime = ticks;
14287 #ifdef STATS
14288 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
14289 #endif
14290 	if (tiwin > rack->r_ctl.rc_high_rwnd)
14291 		rack->r_ctl.rc_high_rwnd = tiwin;
14292 	/*
14293 	 * TCP ECN processing. XXXJTL: If we ever use ECN, we need to move
14294 	 * this to occur after we've validated the segment.
14295 	 */
14296 	if (tcp_ecn_input_segment(tp, thflags, iptos))
14297 		rack_cong_signal(tp, CC_ECN, th->th_ack, __LINE__);
14298 
14299 	/*
14300 	 * If echoed timestamp is later than the current time, fall back to
14301 	 * non RFC1323 RTT calculation.  Normalize timestamp if syncookies
14302 	 * were used when this connection was established.
14303 	 */
14304 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
14305 		to.to_tsecr -= tp->ts_offset;
14306 		if (TSTMP_GT(to.to_tsecr, ms_cts))
14307 			to.to_tsecr = 0;
14308 	}
14309 
14310 	/*
14311 	 * If its the first time in we need to take care of options and
14312 	 * verify we can do SACK for rack!
14313 	 */
14314 	if (rack->r_state == 0) {
14315 		/* Should be init'd by rack_init() */
14316 		KASSERT(rack->rc_inp != NULL,
14317 		    ("%s: rack->rc_inp unexpectedly NULL", __func__));
14318 		if (rack->rc_inp == NULL) {
14319 			rack->rc_inp = tp->t_inpcb;
14320 		}
14321 
14322 		/*
14323 		 * Process options only when we get SYN/ACK back. The SYN
14324 		 * case for incoming connections is handled in tcp_syncache.
14325 		 * According to RFC1323 the window field in a SYN (i.e., a
14326 		 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX
14327 		 * this is traditional behavior, may need to be cleaned up.
14328 		 */
14329 		if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
14330 			/* Handle parallel SYN for ECN */
14331 			tcp_ecn_input_parallel_syn(tp, thflags, iptos);
14332 			if ((to.to_flags & TOF_SCALE) &&
14333 			    (tp->t_flags & TF_REQ_SCALE)) {
14334 				tp->t_flags |= TF_RCVD_SCALE;
14335 				tp->snd_scale = to.to_wscale;
14336 			} else
14337 				tp->t_flags &= ~TF_REQ_SCALE;
14338 			/*
14339 			 * Initial send window.  It will be updated with the
14340 			 * next incoming segment to the scaled value.
14341 			 */
14342 			tp->snd_wnd = th->th_win;
14343 			rack_validate_fo_sendwin_up(tp, rack);
14344 			if ((to.to_flags & TOF_TS) &&
14345 			    (tp->t_flags & TF_REQ_TSTMP)) {
14346 				tp->t_flags |= TF_RCVD_TSTMP;
14347 				tp->ts_recent = to.to_tsval;
14348 				tp->ts_recent_age = cts;
14349 			} else
14350 				tp->t_flags &= ~TF_REQ_TSTMP;
14351 			if (to.to_flags & TOF_MSS) {
14352 				tcp_mss(tp, to.to_mss);
14353 			}
14354 			if ((tp->t_flags & TF_SACK_PERMIT) &&
14355 			    (to.to_flags & TOF_SACKPERM) == 0)
14356 				tp->t_flags &= ~TF_SACK_PERMIT;
14357 			if (IS_FASTOPEN(tp->t_flags)) {
14358 				if (to.to_flags & TOF_FASTOPEN) {
14359 					uint16_t mss;
14360 
14361 					if (to.to_flags & TOF_MSS)
14362 						mss = to.to_mss;
14363 					else
14364 						if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
14365 							mss = TCP6_MSS;
14366 						else
14367 							mss = TCP_MSS;
14368 					tcp_fastopen_update_cache(tp, mss,
14369 					    to.to_tfo_len, to.to_tfo_cookie);
14370 				} else
14371 					tcp_fastopen_disable_path(tp);
14372 			}
14373 		}
14374 		/*
14375 		 * At this point we are at the initial call. Here we decide
14376 		 * if we are doing RACK or not. We do this by seeing if
14377 		 * TF_SACK_PERMIT is set and the sack-not-required is clear.
14378 		 * The code now does do dup-ack counting so if you don't
14379 		 * switch back you won't get rack & TLP, but you will still
14380 		 * get this stack.
14381 		 */
14382 
14383 		if ((rack_sack_not_required == 0) &&
14384 		    ((tp->t_flags & TF_SACK_PERMIT) == 0)) {
14385 			tcp_switch_back_to_default(tp);
14386 			(*tp->t_fb->tfb_tcp_do_segment) (m, th, so, tp, drop_hdrlen,
14387 			    tlen, iptos);
14388 #ifdef TCP_ACCOUNTING
14389 			sched_unpin();
14390 #endif
14391 			return (1);
14392 		}
14393 		tcp_set_hpts(tp->t_inpcb);
14394 		sack_filter_clear(&rack->r_ctl.rack_sf, th->th_ack);
14395 	}
14396 	if (thflags & TH_FIN)
14397 		tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
14398 	us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
14399 	if ((rack->rc_gp_dyn_mul) &&
14400 	    (rack->use_fixed_rate == 0) &&
14401 	    (rack->rc_always_pace)) {
14402 		/* Check in on probertt */
14403 		rack_check_probe_rtt(rack, us_cts);
14404 	}
14405 	rack_clear_rate_sample(rack);
14406 	if ((rack->forced_ack) &&
14407 	    ((tcp_get_flags(th) & TH_RST) == 0)) {
14408 		rack_handle_probe_response(rack, tiwin, us_cts);
14409 	}
14410 	/*
14411 	 * This is the one exception case where we set the rack state
14412 	 * always. All other times (timers etc) we must have a rack-state
14413 	 * set (so we assure we have done the checks above for SACK).
14414 	 */
14415 	rack->r_ctl.rc_rcvtime = cts;
14416 	if (rack->r_state != tp->t_state)
14417 		rack_set_state(tp, rack);
14418 	if (SEQ_GT(th->th_ack, tp->snd_una) &&
14419 	    (rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree)) != NULL)
14420 		kern_prefetch(rsm, &prev_state);
14421 	prev_state = rack->r_state;
14422 	retval = (*rack->r_substate) (m, th, so,
14423 	    tp, &to, drop_hdrlen,
14424 	    tlen, tiwin, thflags, nxt_pkt, iptos);
14425 #ifdef INVARIANTS
14426 	if ((retval == 0) &&
14427 	    (tp->t_inpcb == NULL)) {
14428 		panic("retval:%d tp:%p t_inpcb:NULL state:%d",
14429 		    retval, tp, prev_state);
14430 	}
14431 #endif
14432 	if (retval == 0) {
14433 		/*
14434 		 * If retval is 1 the tcb is unlocked and most likely the tp
14435 		 * is gone.
14436 		 */
14437 		INP_WLOCK_ASSERT(tp->t_inpcb);
14438 		if ((rack->rc_gp_dyn_mul) &&
14439 		    (rack->rc_always_pace) &&
14440 		    (rack->use_fixed_rate == 0) &&
14441 		    rack->in_probe_rtt &&
14442 		    (rack->r_ctl.rc_time_probertt_starts == 0)) {
14443 			/*
14444 			 * If we are going for target, lets recheck before
14445 			 * we output.
14446 			 */
14447 			rack_check_probe_rtt(rack, us_cts);
14448 		}
14449 		if (rack->set_pacing_done_a_iw == 0) {
14450 			/* How much has been acked? */
14451 			if ((tp->snd_una - tp->iss) > (ctf_fixed_maxseg(tp) * 10)) {
14452 				/* We have enough to set in the pacing segment size */
14453 				rack->set_pacing_done_a_iw = 1;
14454 				rack_set_pace_segments(tp, rack, __LINE__, NULL);
14455 			}
14456 		}
14457 		tcp_rack_xmit_timer_commit(rack, tp);
14458 #ifdef TCP_ACCOUNTING
14459 		/*
14460 		 * If we set the ack_val_se to what ack processing we are doing
14461 		 * we also want to track how many cycles we burned. Note
14462 		 * the bits after tcp_output we let be "free". This is because
14463 		 * we are also tracking the tcp_output times as well. Note the
14464 		 * use of 0xf here since we only have 11 counter (0 - 0xa) and
14465 		 * 0xf cannot be returned and is what we initialize it too to
14466 		 * indicate we are not doing the tabulations.
14467 		 */
14468 		if (ack_val_set != 0xf) {
14469 			uint64_t crtsc;
14470 
14471 			crtsc = get_cyclecount();
14472 			counter_u64_add(tcp_proc_time[ack_val_set] , (crtsc - ts_val));
14473 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
14474 				tp->tcp_proc_time[ack_val_set] += (crtsc - ts_val);
14475 			}
14476 		}
14477 #endif
14478 		if (nxt_pkt == 0) {
14479 			if ((rack->r_wanted_output != 0) || (rack->r_fast_output != 0)) {
14480 do_output_now:
14481 				if (tcp_output(tp) < 0)
14482 					return (1);
14483 				did_out = 1;
14484 			}
14485 			rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
14486 			rack_free_trim(rack);
14487 		}
14488 		/* Update any rounds needed */
14489 		if (rack_verbose_logging &&  (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
14490 			union tcp_log_stackspecific log;
14491 			struct timeval tv;
14492 
14493 			memset(&log.u_bbr, 0, sizeof(log.u_bbr));
14494 			log.u_bbr.timeStamp = tcp_get_usecs(&tv);
14495 			log.u_bbr.flex1 = high_seq;
14496 			log.u_bbr.flex2 = rack->r_ctl.roundends;
14497 			log.u_bbr.flex3 = rack->r_ctl.current_round;
14498 			log.u_bbr.rttProp = (uint64_t)CC_ALGO(tp)->newround;
14499 			log.u_bbr.flex8 = 9;
14500 			tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
14501 				       0, &log, false, NULL, NULL, 0, &tv);
14502 		}
14503 		/*
14504 		 * The draft (v3) calls for us to use SEQ_GEQ, but that
14505 		 * causes issues when we are just going app limited. Lets
14506 		 * instead use SEQ_GT <or> where its equal but more data
14507 		 * is outstanding.
14508 		 */
14509 		if ((SEQ_GT(tp->snd_una, rack->r_ctl.roundends)) ||
14510 		    ((tp->snd_una == rack->r_ctl.roundends) && SEQ_GT(tp->snd_max, tp->snd_una))) {
14511 			rack->r_ctl.current_round++;
14512 			rack->r_ctl.roundends = tp->snd_max;
14513 			if (CC_ALGO(tp)->newround != NULL) {
14514 				CC_ALGO(tp)->newround(tp->ccv, rack->r_ctl.current_round);
14515 			}
14516 		}
14517 		if ((nxt_pkt == 0) &&
14518 		    ((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) &&
14519 		    (SEQ_GT(tp->snd_max, tp->snd_una) ||
14520 		     (tp->t_flags & TF_DELACK) ||
14521 		     ((V_tcp_always_keepalive || rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
14522 		      (tp->t_state <= TCPS_CLOSING)))) {
14523 			/* We could not send (probably in the hpts but stopped the timer earlier)? */
14524 			if ((tp->snd_max == tp->snd_una) &&
14525 			    ((tp->t_flags & TF_DELACK) == 0) &&
14526 			    (tcp_in_hpts(rack->rc_inp)) &&
14527 			    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
14528 				/* keep alive not needed if we are hptsi output yet */
14529 				;
14530 			} else {
14531 				int late = 0;
14532 				if (tcp_in_hpts(rack->rc_inp)) {
14533 					if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
14534 						us_cts = tcp_get_usecs(NULL);
14535 						if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
14536 							rack->r_early = 1;
14537 							rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
14538 						} else
14539 							late = 1;
14540 						rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
14541 					}
14542 					tcp_hpts_remove(tp->t_inpcb);
14543 				}
14544 				if (late && (did_out == 0)) {
14545 					/*
14546 					 * We are late in the sending
14547 					 * and we did not call the output
14548 					 * (this probably should not happen).
14549 					 */
14550 					goto do_output_now;
14551 				}
14552 				rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
14553 			}
14554 			way_out = 1;
14555 		} else if (nxt_pkt == 0) {
14556 			/* Do we have the correct timer running? */
14557 			rack_timer_audit(tp, rack, &so->so_snd);
14558 			way_out = 2;
14559 		}
14560 	done_with_input:
14561 		rack_log_doseg_done(rack, cts, nxt_pkt, did_out, way_out, max(1, nsegs));
14562 		if (did_out)
14563 			rack->r_wanted_output = 0;
14564 #ifdef INVARIANTS
14565 		if (tp->t_inpcb == NULL) {
14566 			panic("OP:%d retval:%d tp:%p t_inpcb:NULL state:%d",
14567 			      did_out,
14568 			      retval, tp, prev_state);
14569 		}
14570 #endif
14571 #ifdef TCP_ACCOUNTING
14572 	} else {
14573 		/*
14574 		 * Track the time (see above).
14575 		 */
14576 		if (ack_val_set != 0xf) {
14577 			uint64_t crtsc;
14578 
14579 			crtsc = get_cyclecount();
14580 			counter_u64_add(tcp_proc_time[ack_val_set] , (crtsc - ts_val));
14581 			/*
14582 			 * Note we *DO NOT* increment the per-tcb counters since
14583 			 * in the else the TP may be gone!!
14584 			 */
14585 		}
14586 #endif
14587 	}
14588 #ifdef TCP_ACCOUNTING
14589 	sched_unpin();
14590 #endif
14591 	return (retval);
14592 }
14593 
14594 void
14595 rack_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
14596     struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos)
14597 {
14598 	struct timeval tv;
14599 
14600 	/* First lets see if we have old packets */
14601 	if (tp->t_in_pkt) {
14602 		if (ctf_do_queued_segments(so, tp, 1)) {
14603 			m_freem(m);
14604 			return;
14605 		}
14606 	}
14607 	if (m->m_flags & M_TSTMP_LRO) {
14608 		tv.tv_sec = m->m_pkthdr.rcv_tstmp /1000000000;
14609 		tv.tv_usec = (m->m_pkthdr.rcv_tstmp % 1000000000)/1000;
14610 	} else {
14611 		/* Should not be should we kassert instead? */
14612 		tcp_get_usecs(&tv);
14613 	}
14614 	if (rack_do_segment_nounlock(m, th, so, tp,
14615 				     drop_hdrlen, tlen, iptos, 0, &tv) == 0) {
14616 		INP_WUNLOCK(tp->t_inpcb);
14617 	}
14618 }
14619 
14620 struct rack_sendmap *
14621 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tsused)
14622 {
14623 	struct rack_sendmap *rsm = NULL;
14624 	int32_t idx;
14625 	uint32_t srtt = 0, thresh = 0, ts_low = 0;
14626 
14627 	/* Return the next guy to be re-transmitted */
14628 	if (RB_EMPTY(&rack->r_ctl.rc_mtree)) {
14629 		return (NULL);
14630 	}
14631 	if (tp->t_flags & TF_SENTFIN) {
14632 		/* retran the end FIN? */
14633 		return (NULL);
14634 	}
14635 	/* ok lets look at this one */
14636 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
14637 	if (rack->r_must_retran && rsm && (rsm->r_flags & RACK_MUST_RXT)) {
14638 		return (rsm);
14639 	}
14640 	if (rsm && ((rsm->r_flags & RACK_ACKED) == 0)) {
14641 		goto check_it;
14642 	}
14643 	rsm = rack_find_lowest_rsm(rack);
14644 	if (rsm == NULL) {
14645 		return (NULL);
14646 	}
14647 check_it:
14648 	if (((rack->rc_tp->t_flags & TF_SACK_PERMIT) == 0) &&
14649 	    (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
14650 		/*
14651 		 * No sack so we automatically do the 3 strikes and
14652 		 * retransmit (no rack timer would be started).
14653 		 */
14654 
14655 		return (rsm);
14656 	}
14657 	if (rsm->r_flags & RACK_ACKED) {
14658 		return (NULL);
14659 	}
14660 	if (((rsm->r_flags & RACK_SACK_PASSED) == 0) &&
14661 	    (rsm->r_dupack < DUP_ACK_THRESHOLD)) {
14662 		/* Its not yet ready */
14663 		return (NULL);
14664 	}
14665 	srtt = rack_grab_rtt(tp, rack);
14666 	idx = rsm->r_rtr_cnt - 1;
14667 	ts_low = (uint32_t)rsm->r_tim_lastsent[idx];
14668 	thresh = rack_calc_thresh_rack(rack, srtt, tsused);
14669 	if ((tsused == ts_low) ||
14670 	    (TSTMP_LT(tsused, ts_low))) {
14671 		/* No time since sending */
14672 		return (NULL);
14673 	}
14674 	if ((tsused - ts_low) < thresh) {
14675 		/* It has not been long enough yet */
14676 		return (NULL);
14677 	}
14678 	if ((rsm->r_dupack >= DUP_ACK_THRESHOLD) ||
14679 	    ((rsm->r_flags & RACK_SACK_PASSED) &&
14680 	     (rack->sack_attack_disable == 0))) {
14681 		/*
14682 		 * We have passed the dup-ack threshold <or>
14683 		 * a SACK has indicated this is missing.
14684 		 * Note that if you are a declared attacker
14685 		 * it is only the dup-ack threshold that
14686 		 * will cause retransmits.
14687 		 */
14688 		/* log retransmit reason */
14689 		rack_log_retran_reason(rack, rsm, (tsused - ts_low), thresh, 1);
14690 		rack->r_fast_output = 0;
14691 		return (rsm);
14692 	}
14693 	return (NULL);
14694 }
14695 
14696 static void
14697 rack_log_pacing_delay_calc(struct tcp_rack *rack, uint32_t len, uint32_t slot,
14698 			   uint64_t bw_est, uint64_t bw, uint64_t len_time, int method,
14699 			   int line, struct rack_sendmap *rsm, uint8_t quality)
14700 {
14701 	if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
14702 		union tcp_log_stackspecific log;
14703 		struct timeval tv;
14704 
14705 		memset(&log, 0, sizeof(log));
14706 		log.u_bbr.flex1 = slot;
14707 		log.u_bbr.flex2 = len;
14708 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_min_segs;
14709 		log.u_bbr.flex4 = rack->r_ctl.rc_pace_max_segs;
14710 		log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ss;
14711 		log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_ca;
14712 		log.u_bbr.use_lt_bw = rack->rc_ack_can_sendout_data;
14713 		log.u_bbr.use_lt_bw <<= 1;
14714 		log.u_bbr.use_lt_bw |= rack->r_late;
14715 		log.u_bbr.use_lt_bw <<= 1;
14716 		log.u_bbr.use_lt_bw |= rack->r_early;
14717 		log.u_bbr.use_lt_bw <<= 1;
14718 		log.u_bbr.use_lt_bw |= rack->app_limited_needs_set;
14719 		log.u_bbr.use_lt_bw <<= 1;
14720 		log.u_bbr.use_lt_bw |= rack->rc_gp_filled;
14721 		log.u_bbr.use_lt_bw <<= 1;
14722 		log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt;
14723 		log.u_bbr.use_lt_bw <<= 1;
14724 		log.u_bbr.use_lt_bw |= rack->in_probe_rtt;
14725 		log.u_bbr.use_lt_bw <<= 1;
14726 		log.u_bbr.use_lt_bw |= rack->gp_ready;
14727 		log.u_bbr.pkt_epoch = line;
14728 		log.u_bbr.epoch = rack->r_ctl.rc_agg_delayed;
14729 		log.u_bbr.lt_epoch = rack->r_ctl.rc_agg_early;
14730 		log.u_bbr.applimited = rack->r_ctl.rack_per_of_gp_rec;
14731 		log.u_bbr.bw_inuse = bw_est;
14732 		log.u_bbr.delRate = bw;
14733 		if (rack->r_ctl.gp_bw == 0)
14734 			log.u_bbr.cur_del_rate = 0;
14735 		else
14736 			log.u_bbr.cur_del_rate = rack_get_bw(rack);
14737 		log.u_bbr.rttProp = len_time;
14738 		log.u_bbr.pkts_out = rack->r_ctl.rc_rack_min_rtt;
14739 		log.u_bbr.lost = rack->r_ctl.rc_probertt_sndmax_atexit;
14740 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm);
14741 		if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh) {
14742 			/* We are in slow start */
14743 			log.u_bbr.flex7 = 1;
14744 		} else {
14745 			/* we are on congestion avoidance */
14746 			log.u_bbr.flex7 = 0;
14747 		}
14748 		log.u_bbr.flex8 = method;
14749 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
14750 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
14751 		log.u_bbr.cwnd_gain = rack->rc_gp_saw_rec;
14752 		log.u_bbr.cwnd_gain <<= 1;
14753 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss;
14754 		log.u_bbr.cwnd_gain <<= 1;
14755 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca;
14756 		log.u_bbr.bbr_substate = quality;
14757 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
14758 		    &rack->rc_inp->inp_socket->so_rcv,
14759 		    &rack->rc_inp->inp_socket->so_snd,
14760 		    BBR_LOG_HPTSI_CALC, 0,
14761 		    0, &log, false, &tv);
14762 	}
14763 }
14764 
14765 static uint32_t
14766 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss)
14767 {
14768 	uint32_t new_tso, user_max;
14769 
14770 	user_max = rack->rc_user_set_max_segs * mss;
14771 	if (rack->rc_force_max_seg) {
14772 		return (user_max);
14773 	}
14774 	if (rack->use_fixed_rate &&
14775 	    ((rack->r_ctl.crte == NULL) ||
14776 	     (bw != rack->r_ctl.crte->rate))) {
14777 		/* Use the user mss since we are not exactly matched */
14778 		return (user_max);
14779 	}
14780 	new_tso = tcp_get_pacing_burst_size(rack->rc_tp, bw, mss, rack_pace_one_seg, rack->r_ctl.crte, NULL);
14781 	if (new_tso > user_max)
14782 		new_tso = user_max;
14783 	return (new_tso);
14784 }
14785 
14786 static int32_t
14787 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)
14788 {
14789 	uint64_t lentim, fill_bw;
14790 
14791 	/* Lets first see if we are full, if so continue with normal rate */
14792 	rack->r_via_fill_cw = 0;
14793 	if (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.cwnd_to_use)
14794 		return (slot);
14795 	if ((ctf_outstanding(rack->rc_tp) + (segsiz-1)) > rack->rc_tp->snd_wnd)
14796 		return (slot);
14797 	if (rack->r_ctl.rc_last_us_rtt == 0)
14798 		return (slot);
14799 	if (rack->rc_pace_fill_if_rttin_range &&
14800 	    (rack->r_ctl.rc_last_us_rtt >=
14801 	     (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack->rtt_limit_mul))) {
14802 		/* The rtt is huge, N * smallest, lets not fill */
14803 		return (slot);
14804 	}
14805 	/*
14806 	 * first lets calculate the b/w based on the last us-rtt
14807 	 * and the sndwnd.
14808 	 */
14809 	fill_bw = rack->r_ctl.cwnd_to_use;
14810 	/* Take the rwnd if its smaller */
14811 	if (fill_bw > rack->rc_tp->snd_wnd)
14812 		fill_bw = rack->rc_tp->snd_wnd;
14813 	if (rack->r_fill_less_agg) {
14814 		/*
14815 		 * Now take away the inflight (this will reduce our
14816 		 * aggressiveness and yeah, if we get that much out in 1RTT
14817 		 * we will have had acks come back and still be behind).
14818 		 */
14819 		fill_bw -= ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
14820 	}
14821 	/* Now lets make it into a b/w */
14822 	fill_bw *= (uint64_t)HPTS_USEC_IN_SEC;
14823 	fill_bw /= (uint64_t)rack->r_ctl.rc_last_us_rtt;
14824 	/* We are below the min b/w */
14825 	if (non_paced)
14826 		*rate_wanted = fill_bw;
14827 	if ((fill_bw < RACK_MIN_BW) || (fill_bw < *rate_wanted))
14828 		return (slot);
14829 	if (rack->r_ctl.bw_rate_cap && (fill_bw > rack->r_ctl.bw_rate_cap))
14830 		fill_bw = rack->r_ctl.bw_rate_cap;
14831 	rack->r_via_fill_cw = 1;
14832 	if (rack->r_rack_hw_rate_caps &&
14833 	    (rack->r_ctl.crte != NULL)) {
14834 		uint64_t high_rate;
14835 
14836 		high_rate = tcp_hw_highest_rate(rack->r_ctl.crte);
14837 		if (fill_bw > high_rate) {
14838 			/* We are capping bw at the highest rate table entry */
14839 			if (*rate_wanted > high_rate) {
14840 				/* The original rate was also capped */
14841 				rack->r_via_fill_cw = 0;
14842 			}
14843 			rack_log_hdwr_pacing(rack,
14844 					     fill_bw, high_rate, __LINE__,
14845 					     0, 3);
14846 			fill_bw = high_rate;
14847 			if (capped)
14848 				*capped = 1;
14849 		}
14850 	} else if ((rack->r_ctl.crte == NULL) &&
14851 		   (rack->rack_hdrw_pacing == 0) &&
14852 		   (rack->rack_hdw_pace_ena) &&
14853 		   rack->r_rack_hw_rate_caps &&
14854 		   (rack->rack_attempt_hdwr_pace == 0) &&
14855 		   (rack->rc_inp->inp_route.ro_nh != NULL) &&
14856 		   (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
14857 		/*
14858 		 * Ok we may have a first attempt that is greater than our top rate
14859 		 * lets check.
14860 		 */
14861 		uint64_t high_rate;
14862 
14863 		high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp);
14864 		if (high_rate) {
14865 			if (fill_bw > high_rate) {
14866 				fill_bw = high_rate;
14867 				if (capped)
14868 					*capped = 1;
14869 			}
14870 		}
14871 	}
14872 	/*
14873 	 * Ok fill_bw holds our mythical b/w to fill the cwnd
14874 	 * in a rtt, what does that time wise equate too?
14875 	 */
14876 	lentim = (uint64_t)(len) * (uint64_t)HPTS_USEC_IN_SEC;
14877 	lentim /= fill_bw;
14878 	*rate_wanted = fill_bw;
14879 	if (non_paced || (lentim < slot)) {
14880 		rack_log_pacing_delay_calc(rack, len, slot, fill_bw,
14881 					   0, lentim, 12, __LINE__, NULL, 0);
14882 		return ((int32_t)lentim);
14883 	} else
14884 		return (slot);
14885 }
14886 
14887 static int32_t
14888 rack_get_pacing_delay(struct tcp_rack *rack, struct tcpcb *tp, uint32_t len, struct rack_sendmap *rsm, uint32_t segsiz)
14889 {
14890 	uint64_t srtt;
14891 	int32_t slot = 0;
14892 	int can_start_hw_pacing = 1;
14893 	int err;
14894 
14895 	if (rack->rc_always_pace == 0) {
14896 		/*
14897 		 * We use the most optimistic possible cwnd/srtt for
14898 		 * sending calculations. This will make our
14899 		 * calculation anticipate getting more through
14900 		 * quicker then possible. But thats ok we don't want
14901 		 * the peer to have a gap in data sending.
14902 		 */
14903 		uint64_t cwnd, tr_perms = 0;
14904 		int32_t reduce = 0;
14905 
14906 	old_method:
14907 		/*
14908 		 * We keep no precise pacing with the old method
14909 		 * instead we use the pacer to mitigate bursts.
14910 		 */
14911 		if (rack->r_ctl.rc_rack_min_rtt)
14912 			srtt = rack->r_ctl.rc_rack_min_rtt;
14913 		else
14914 			srtt = max(tp->t_srtt, 1);
14915 		if (rack->r_ctl.rc_rack_largest_cwnd)
14916 			cwnd = rack->r_ctl.rc_rack_largest_cwnd;
14917 		else
14918 			cwnd = rack->r_ctl.cwnd_to_use;
14919 		/* Inflate cwnd by 1000 so srtt of usecs is in ms */
14920 		tr_perms = (cwnd * 1000) / srtt;
14921 		if (tr_perms == 0) {
14922 			tr_perms = ctf_fixed_maxseg(tp);
14923 		}
14924 		/*
14925 		 * Calculate how long this will take to drain, if
14926 		 * the calculation comes out to zero, thats ok we
14927 		 * will use send_a_lot to possibly spin around for
14928 		 * more increasing tot_len_this_send to the point
14929 		 * that its going to require a pace, or we hit the
14930 		 * cwnd. Which in that case we are just waiting for
14931 		 * a ACK.
14932 		 */
14933 		slot = len / tr_perms;
14934 		/* Now do we reduce the time so we don't run dry? */
14935 		if (slot && rack_slot_reduction) {
14936 			reduce = (slot / rack_slot_reduction);
14937 			if (reduce < slot) {
14938 				slot -= reduce;
14939 			} else
14940 				slot = 0;
14941 		}
14942 		slot *= HPTS_USEC_IN_MSEC;
14943 		if (rack->rc_pace_to_cwnd) {
14944 			uint64_t rate_wanted = 0;
14945 
14946 			slot = pace_to_fill_cwnd(rack, slot, len, segsiz, NULL, &rate_wanted, 1);
14947 			rack->rc_ack_can_sendout_data = 1;
14948 			rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, 0, 0, 14, __LINE__, NULL, 0);
14949 		} else
14950 			rack_log_pacing_delay_calc(rack, len, slot, tr_perms, reduce, 0, 7, __LINE__, NULL, 0);
14951 	} else {
14952 		uint64_t bw_est, res, lentim, rate_wanted;
14953 		uint32_t orig_val, segs, oh;
14954 		int capped = 0;
14955 		int prev_fill;
14956 
14957 		if ((rack->r_rr_config == 1) && rsm) {
14958 			return (rack->r_ctl.rc_min_to);
14959 		}
14960 		if (rack->use_fixed_rate) {
14961 			rate_wanted = bw_est = rack_get_fixed_pacing_bw(rack);
14962 		} else if ((rack->r_ctl.init_rate == 0) &&
14963 #ifdef NETFLIX_PEAKRATE
14964 			   (rack->rc_tp->t_maxpeakrate == 0) &&
14965 #endif
14966 			   (rack->r_ctl.gp_bw == 0)) {
14967 			/* no way to yet do an estimate */
14968 			bw_est = rate_wanted = 0;
14969 		} else {
14970 			bw_est = rack_get_bw(rack);
14971 			rate_wanted = rack_get_output_bw(rack, bw_est, rsm, &capped);
14972 		}
14973 		if ((bw_est == 0) || (rate_wanted == 0) ||
14974 		    ((rack->gp_ready == 0) && (rack->use_fixed_rate == 0))) {
14975 			/*
14976 			 * No way yet to make a b/w estimate or
14977 			 * our raise is set incorrectly.
14978 			 */
14979 			goto old_method;
14980 		}
14981 		/* We need to account for all the overheads */
14982 		segs = (len + segsiz - 1) / segsiz;
14983 		/*
14984 		 * We need the diff between 1514 bytes (e-mtu with e-hdr)
14985 		 * and how much data we put in each packet. Yes this
14986 		 * means we may be off if we are larger than 1500 bytes
14987 		 * or smaller. But this just makes us more conservative.
14988 		 */
14989 		if (rack_hw_rate_min &&
14990 		    (bw_est < rack_hw_rate_min))
14991 			can_start_hw_pacing = 0;
14992 		if (ETHERNET_SEGMENT_SIZE > segsiz)
14993 			oh = ETHERNET_SEGMENT_SIZE - segsiz;
14994 		else
14995 			oh = 0;
14996 		segs *= oh;
14997 		lentim = (uint64_t)(len + segs) * (uint64_t)HPTS_USEC_IN_SEC;
14998 		res = lentim / rate_wanted;
14999 		slot = (uint32_t)res;
15000 		orig_val = rack->r_ctl.rc_pace_max_segs;
15001 		if (rack->r_ctl.crte == NULL) {
15002 			/*
15003 			 * Only do this if we are not hardware pacing
15004 			 * since if we are doing hw-pacing below we will
15005 			 * set make a call after setting up or changing
15006 			 * the rate.
15007 			 */
15008 			rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
15009 		} else if (rack->rc_inp->inp_snd_tag == NULL) {
15010 			/*
15011 			 * We lost our rate somehow, this can happen
15012 			 * if the interface changed underneath us.
15013 			 */
15014 			tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
15015 			rack->r_ctl.crte = NULL;
15016 			/* Lets re-allow attempting to setup pacing */
15017 			rack->rack_hdrw_pacing = 0;
15018 			rack->rack_attempt_hdwr_pace = 0;
15019 			rack_log_hdwr_pacing(rack,
15020 					     rate_wanted, bw_est, __LINE__,
15021 					     0, 6);
15022 		}
15023 		/* Did we change the TSO size, if so log it */
15024 		if (rack->r_ctl.rc_pace_max_segs != orig_val)
15025 			rack_log_pacing_delay_calc(rack, len, slot, orig_val, 0, 0, 15, __LINE__, NULL, 0);
15026 		prev_fill = rack->r_via_fill_cw;
15027 		if ((rack->rc_pace_to_cwnd) &&
15028 		    (capped == 0) &&
15029 		    (rack->use_fixed_rate == 0) &&
15030 		    (rack->in_probe_rtt == 0) &&
15031 		    (IN_FASTRECOVERY(rack->rc_tp->t_flags) == 0)) {
15032 			/*
15033 			 * We want to pace at our rate *or* faster to
15034 			 * fill the cwnd to the max if its not full.
15035 			 */
15036 			slot = pace_to_fill_cwnd(rack, slot, (len+segs), segsiz, &capped, &rate_wanted, 0);
15037 		}
15038 		if ((rack->rc_inp->inp_route.ro_nh != NULL) &&
15039 		    (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
15040 			if ((rack->rack_hdw_pace_ena) &&
15041 			    (can_start_hw_pacing > 0) &&
15042 			    (rack->rack_hdrw_pacing == 0) &&
15043 			    (rack->rack_attempt_hdwr_pace == 0)) {
15044 				/*
15045 				 * Lets attempt to turn on hardware pacing
15046 				 * if we can.
15047 				 */
15048 				rack->rack_attempt_hdwr_pace = 1;
15049 				rack->r_ctl.crte = tcp_set_pacing_rate(rack->rc_tp,
15050 								       rack->rc_inp->inp_route.ro_nh->nh_ifp,
15051 								       rate_wanted,
15052 								       RS_PACING_GEQ,
15053 								       &err, &rack->r_ctl.crte_prev_rate);
15054 				if (rack->r_ctl.crte) {
15055 					rack->rack_hdrw_pacing = 1;
15056 					rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size(tp, rate_wanted, segsiz,
15057 												 0, rack->r_ctl.crte,
15058 												 NULL);
15059 					rack_log_hdwr_pacing(rack,
15060 							     rate_wanted, rack->r_ctl.crte->rate, __LINE__,
15061 							     err, 0);
15062 					rack->r_ctl.last_hw_bw_req = rate_wanted;
15063 				} else {
15064 					counter_u64_add(rack_hw_pace_init_fail, 1);
15065 				}
15066 			} else if (rack->rack_hdrw_pacing &&
15067 				   (rack->r_ctl.last_hw_bw_req != rate_wanted)) {
15068 				/* Do we need to adjust our rate? */
15069 				const struct tcp_hwrate_limit_table *nrte;
15070 
15071 				if (rack->r_up_only &&
15072 				    (rate_wanted < rack->r_ctl.crte->rate)) {
15073 					/**
15074 					 * We have four possible states here
15075 					 * having to do with the previous time
15076 					 * and this time.
15077 					 *   previous  |  this-time
15078 					 * A)     0      |     0   -- fill_cw not in the picture
15079 					 * B)     1      |     0   -- we were doing a fill-cw but now are not
15080 					 * C)     1      |     1   -- all rates from fill_cw
15081 					 * D)     0      |     1   -- we were doing non-fill and now we are filling
15082 					 *
15083 					 * For case A, C and D we don't allow a drop. But for
15084 					 * case B where we now our on our steady rate we do
15085 					 * allow a drop.
15086 					 *
15087 					 */
15088 					if (!((prev_fill == 1) && (rack->r_via_fill_cw == 0)))
15089 						goto done_w_hdwr;
15090 				}
15091 				if ((rate_wanted > rack->r_ctl.crte->rate) ||
15092 				    (rate_wanted <= rack->r_ctl.crte_prev_rate)) {
15093 					if (rack_hw_rate_to_low &&
15094 					    (bw_est < rack_hw_rate_to_low)) {
15095 						/*
15096 						 * The pacing rate is too low for hardware, but
15097 						 * do allow hardware pacing to be restarted.
15098 						 */
15099 						rack_log_hdwr_pacing(rack,
15100 							     bw_est, rack->r_ctl.crte->rate, __LINE__,
15101 							     0, 5);
15102 						tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
15103 						rack->r_ctl.crte = NULL;
15104 						rack->rack_attempt_hdwr_pace = 0;
15105 						rack->rack_hdrw_pacing = 0;
15106 						rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
15107 						goto done_w_hdwr;
15108 					}
15109 					nrte = tcp_chg_pacing_rate(rack->r_ctl.crte,
15110 								   rack->rc_tp,
15111 								   rack->rc_inp->inp_route.ro_nh->nh_ifp,
15112 								   rate_wanted,
15113 								   RS_PACING_GEQ,
15114 								   &err, &rack->r_ctl.crte_prev_rate);
15115 					if (nrte == NULL) {
15116 						/* Lost the rate */
15117 						rack->rack_hdrw_pacing = 0;
15118 						rack->r_ctl.crte = NULL;
15119 						rack_log_hdwr_pacing(rack,
15120 								     rate_wanted, 0, __LINE__,
15121 								     err, 1);
15122 						rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
15123 						counter_u64_add(rack_hw_pace_lost, 1);
15124 					} else if (nrte != rack->r_ctl.crte) {
15125 						rack->r_ctl.crte = nrte;
15126 						rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size(tp, rate_wanted,
15127 													 segsiz, 0,
15128 													 rack->r_ctl.crte,
15129 													 NULL);
15130 						rack_log_hdwr_pacing(rack,
15131 								     rate_wanted, rack->r_ctl.crte->rate, __LINE__,
15132 								     err, 2);
15133 						rack->r_ctl.last_hw_bw_req = rate_wanted;
15134 					}
15135 				} else {
15136 					/* We just need to adjust the segment size */
15137 					rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
15138 					rack_log_hdwr_pacing(rack,
15139 							     rate_wanted, rack->r_ctl.crte->rate, __LINE__,
15140 							     0, 4);
15141 					rack->r_ctl.last_hw_bw_req = rate_wanted;
15142 				}
15143 			}
15144 		}
15145 		if ((rack->r_ctl.crte != NULL) &&
15146 		    (rack->r_ctl.crte->rate == rate_wanted)) {
15147 			/*
15148 			 * We need to add a extra if the rates
15149 			 * are exactly matched. The idea is
15150 			 * we want the software to make sure the
15151 			 * queue is empty before adding more, this
15152 			 * gives us N MSS extra pace times where
15153 			 * N is our sysctl
15154 			 */
15155 			slot += (rack->r_ctl.crte->time_between * rack_hw_pace_extra_slots);
15156 		}
15157 done_w_hdwr:
15158 		if (rack_limit_time_with_srtt &&
15159 		    (rack->use_fixed_rate == 0) &&
15160 #ifdef NETFLIX_PEAKRATE
15161 		    (rack->rc_tp->t_maxpeakrate == 0) &&
15162 #endif
15163 		    (rack->rack_hdrw_pacing == 0)) {
15164 			/*
15165 			 * Sanity check, we do not allow the pacing delay
15166 			 * to be longer than the SRTT of the path. If it is
15167 			 * a slow path, then adding a packet should increase
15168 			 * the RTT and compensate for this i.e. the srtt will
15169 			 * be greater so the allowed pacing time will be greater.
15170 			 *
15171 			 * Note this restriction is not for where a peak rate
15172 			 * is set, we are doing fixed pacing or hardware pacing.
15173 			 */
15174 			if (rack->rc_tp->t_srtt)
15175 				srtt = rack->rc_tp->t_srtt;
15176 			else
15177 				srtt = RACK_INITIAL_RTO * HPTS_USEC_IN_MSEC;	/* its in ms convert */
15178 			if (srtt < (uint64_t)slot) {
15179 				rack_log_pacing_delay_calc(rack, srtt, slot, rate_wanted, bw_est, lentim, 99, __LINE__, NULL, 0);
15180 				slot = srtt;
15181 			}
15182 		}
15183 		rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, bw_est, lentim, 2, __LINE__, rsm, 0);
15184 	}
15185 	if (rack->r_ctl.crte && (rack->r_ctl.crte->rs_num_enobufs > 0)) {
15186 		/*
15187 		 * If this rate is seeing enobufs when it
15188 		 * goes to send then either the nic is out
15189 		 * of gas or we are mis-estimating the time
15190 		 * somehow and not letting the queue empty
15191 		 * completely. Lets add to the pacing time.
15192 		 */
15193 		int hw_boost_delay;
15194 
15195 		hw_boost_delay = rack->r_ctl.crte->time_between * rack_enobuf_hw_boost_mult;
15196 		if (hw_boost_delay > rack_enobuf_hw_max)
15197 			hw_boost_delay = rack_enobuf_hw_max;
15198 		else if (hw_boost_delay < rack_enobuf_hw_min)
15199 			hw_boost_delay = rack_enobuf_hw_min;
15200 		slot += hw_boost_delay;
15201 	}
15202 	return (slot);
15203 }
15204 
15205 static void
15206 rack_start_gp_measurement(struct tcpcb *tp, struct tcp_rack *rack,
15207     tcp_seq startseq, uint32_t sb_offset)
15208 {
15209 	struct rack_sendmap *my_rsm = NULL;
15210 	struct rack_sendmap fe;
15211 
15212 	if (tp->t_state < TCPS_ESTABLISHED) {
15213 		/*
15214 		 * We don't start any measurements if we are
15215 		 * not at least established.
15216 		 */
15217 		return;
15218 	}
15219 	if (tp->t_state >= TCPS_FIN_WAIT_1) {
15220 		/*
15221 		 * We will get no more data into the SB
15222 		 * this means we need to have the data available
15223 		 * before we start a measurement.
15224 		 */
15225 
15226 		if (sbavail(&tp->t_inpcb->inp_socket->so_snd) <
15227 		    max(rc_init_window(rack),
15228 			(MIN_GP_WIN * ctf_fixed_maxseg(tp)))) {
15229 			/* Nope not enough data */
15230 			return;
15231 		}
15232 	}
15233 	tp->t_flags |= TF_GPUTINPROG;
15234 	rack->r_ctl.rc_gp_lowrtt = 0xffffffff;
15235 	rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
15236 	tp->gput_seq = startseq;
15237 	rack->app_limited_needs_set = 0;
15238 	if (rack->in_probe_rtt)
15239 		rack->measure_saw_probe_rtt = 1;
15240 	else if ((rack->measure_saw_probe_rtt) &&
15241 		 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
15242 		rack->measure_saw_probe_rtt = 0;
15243 	if (rack->rc_gp_filled)
15244 		tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
15245 	else {
15246 		/* Special case initial measurement */
15247 		struct timeval tv;
15248 
15249 		tp->gput_ts = tcp_get_usecs(&tv);
15250 		rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
15251 	}
15252 	/*
15253 	 * We take a guess out into the future,
15254 	 * if we have no measurement and no
15255 	 * initial rate, we measure the first
15256 	 * initial-windows worth of data to
15257 	 * speed up getting some GP measurement and
15258 	 * thus start pacing.
15259 	 */
15260 	if ((rack->rc_gp_filled == 0) && (rack->r_ctl.init_rate == 0)) {
15261 		rack->app_limited_needs_set = 1;
15262 		tp->gput_ack = startseq + max(rc_init_window(rack),
15263 					      (MIN_GP_WIN * ctf_fixed_maxseg(tp)));
15264 		rack_log_pacing_delay_calc(rack,
15265 					   tp->gput_seq,
15266 					   tp->gput_ack,
15267 					   0,
15268 					   tp->gput_ts,
15269 					   rack->r_ctl.rc_app_limited_cnt,
15270 					   9,
15271 					   __LINE__, NULL, 0);
15272 		return;
15273 	}
15274 	if (sb_offset) {
15275 		/*
15276 		 * We are out somewhere in the sb
15277 		 * can we use the already outstanding data?
15278 		 */
15279 		if (rack->r_ctl.rc_app_limited_cnt == 0) {
15280 			/*
15281 			 * Yes first one is good and in this case
15282 			 * the tp->gput_ts is correctly set based on
15283 			 * the last ack that arrived (no need to
15284 			 * set things up when an ack comes in).
15285 			 */
15286 			my_rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
15287 			if ((my_rsm == NULL) ||
15288 			    (my_rsm->r_rtr_cnt != 1)) {
15289 				/* retransmission? */
15290 				goto use_latest;
15291 			}
15292 		} else {
15293 			if (rack->r_ctl.rc_first_appl == NULL) {
15294 				/*
15295 				 * If rc_first_appl is NULL
15296 				 * then the cnt should be 0.
15297 				 * This is probably an error, maybe
15298 				 * a KASSERT would be approprate.
15299 				 */
15300 				goto use_latest;
15301 			}
15302 			/*
15303 			 * If we have a marker pointer to the last one that is
15304 			 * app limited we can use that, but we need to set
15305 			 * things up so that when it gets ack'ed we record
15306 			 * the ack time (if its not already acked).
15307 			 */
15308 			rack->app_limited_needs_set = 1;
15309 			/*
15310 			 * We want to get to the rsm that is either
15311 			 * next with space i.e. over 1 MSS or the one
15312 			 * after that (after the app-limited).
15313 			 */
15314 			my_rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree,
15315 					 rack->r_ctl.rc_first_appl);
15316 			if (my_rsm) {
15317 				if ((my_rsm->r_end - my_rsm->r_start) <= ctf_fixed_maxseg(tp))
15318 					/* Have to use the next one */
15319 					my_rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree,
15320 							 my_rsm);
15321 				else {
15322 					/* Use after the first MSS of it is acked */
15323 					tp->gput_seq = my_rsm->r_start + ctf_fixed_maxseg(tp);
15324 					goto start_set;
15325 				}
15326 			}
15327 			if ((my_rsm == NULL) ||
15328 			    (my_rsm->r_rtr_cnt != 1)) {
15329 				/*
15330 				 * Either its a retransmit or
15331 				 * the last is the app-limited one.
15332 				 */
15333 				goto use_latest;
15334 			}
15335 		}
15336 		tp->gput_seq = my_rsm->r_start;
15337 start_set:
15338 		if (my_rsm->r_flags & RACK_ACKED) {
15339 			/*
15340 			 * This one has been acked use the arrival ack time
15341 			 */
15342 			tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival;
15343 			rack->app_limited_needs_set = 0;
15344 		}
15345 		rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[(my_rsm->r_rtr_cnt-1)];
15346 		tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
15347 		rack_log_pacing_delay_calc(rack,
15348 					   tp->gput_seq,
15349 					   tp->gput_ack,
15350 					   (uint64_t)my_rsm,
15351 					   tp->gput_ts,
15352 					   rack->r_ctl.rc_app_limited_cnt,
15353 					   9,
15354 					   __LINE__, NULL, 0);
15355 		return;
15356 	}
15357 
15358 use_latest:
15359 	/*
15360 	 * We don't know how long we may have been
15361 	 * idle or if this is the first-send. Lets
15362 	 * setup the flag so we will trim off
15363 	 * the first ack'd data so we get a true
15364 	 * measurement.
15365 	 */
15366 	rack->app_limited_needs_set = 1;
15367 	tp->gput_ack = startseq + rack_get_measure_window(tp, rack);
15368 	/* Find this guy so we can pull the send time */
15369 	fe.r_start = startseq;
15370 	my_rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
15371 	if (my_rsm) {
15372 		rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[(my_rsm->r_rtr_cnt-1)];
15373 		if (my_rsm->r_flags & RACK_ACKED) {
15374 			/*
15375 			 * Unlikely since its probably what was
15376 			 * just transmitted (but I am paranoid).
15377 			 */
15378 			tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival;
15379 			rack->app_limited_needs_set = 0;
15380 		}
15381 		if (SEQ_LT(my_rsm->r_start, tp->gput_seq)) {
15382 			/* This also is unlikely */
15383 			tp->gput_seq = my_rsm->r_start;
15384 		}
15385 	} else {
15386 		/*
15387 		 * TSNH unless we have some send-map limit,
15388 		 * and even at that it should not be hitting
15389 		 * that limit (we should have stopped sending).
15390 		 */
15391 		struct timeval tv;
15392 
15393 		microuptime(&tv);
15394 		rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
15395 	}
15396 	rack_log_pacing_delay_calc(rack,
15397 				   tp->gput_seq,
15398 				   tp->gput_ack,
15399 				   (uint64_t)my_rsm,
15400 				   tp->gput_ts,
15401 				   rack->r_ctl.rc_app_limited_cnt,
15402 				   9, __LINE__, NULL, 0);
15403 }
15404 
15405 static inline uint32_t
15406 rack_what_can_we_send(struct tcpcb *tp, struct tcp_rack *rack,  uint32_t cwnd_to_use,
15407     uint32_t avail, int32_t sb_offset)
15408 {
15409 	uint32_t len;
15410 	uint32_t sendwin;
15411 
15412 	if (tp->snd_wnd > cwnd_to_use)
15413 		sendwin = cwnd_to_use;
15414 	else
15415 		sendwin = tp->snd_wnd;
15416 	if (ctf_outstanding(tp) >= tp->snd_wnd) {
15417 		/* We never want to go over our peers rcv-window */
15418 		len = 0;
15419 	} else {
15420 		uint32_t flight;
15421 
15422 		flight = ctf_flight_size(tp, rack->r_ctl.rc_sacked);
15423 		if (flight >= sendwin) {
15424 			/*
15425 			 * We have in flight what we are allowed by cwnd (if
15426 			 * it was rwnd blocking it would have hit above out
15427 			 * >= tp->snd_wnd).
15428 			 */
15429 			return (0);
15430 		}
15431 		len = sendwin - flight;
15432 		if ((len + ctf_outstanding(tp)) > tp->snd_wnd) {
15433 			/* We would send too much (beyond the rwnd) */
15434 			len = tp->snd_wnd - ctf_outstanding(tp);
15435 		}
15436 		if ((len + sb_offset) > avail) {
15437 			/*
15438 			 * We don't have that much in the SB, how much is
15439 			 * there?
15440 			 */
15441 			len = avail - sb_offset;
15442 		}
15443 	}
15444 	return (len);
15445 }
15446 
15447 static void
15448 rack_log_fsb(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t flags,
15449 	     unsigned ipoptlen, int32_t orig_len, int32_t len, int error,
15450 	     int rsm_is_null, int optlen, int line, uint16_t mode)
15451 {
15452 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
15453 		union tcp_log_stackspecific log;
15454 		struct timeval tv;
15455 
15456 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
15457 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
15458 		log.u_bbr.flex1 = error;
15459 		log.u_bbr.flex2 = flags;
15460 		log.u_bbr.flex3 = rsm_is_null;
15461 		log.u_bbr.flex4 = ipoptlen;
15462 		log.u_bbr.flex5 = tp->rcv_numsacks;
15463 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
15464 		log.u_bbr.flex7 = optlen;
15465 		log.u_bbr.flex8 = rack->r_fsb_inited;
15466 		log.u_bbr.applimited = rack->r_fast_output;
15467 		log.u_bbr.bw_inuse = rack_get_bw(rack);
15468 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
15469 		log.u_bbr.cwnd_gain = mode;
15470 		log.u_bbr.pkts_out = orig_len;
15471 		log.u_bbr.lt_epoch = len;
15472 		log.u_bbr.delivered = line;
15473 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
15474 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
15475 		tcp_log_event_(tp, NULL, &so->so_rcv, &so->so_snd, TCP_LOG_FSB, 0,
15476 			       len, &log, false, NULL, NULL, 0, &tv);
15477 	}
15478 }
15479 
15480 
15481 static struct mbuf *
15482 rack_fo_base_copym(struct mbuf *the_m, uint32_t the_off, int32_t *plen,
15483 		   struct rack_fast_send_blk *fsb,
15484 		   int32_t seglimit, int32_t segsize, int hw_tls)
15485 {
15486 #ifdef KERN_TLS
15487 	struct ktls_session *tls, *ntls;
15488 #ifdef INVARIANTS
15489 	struct mbuf *start;
15490 #endif
15491 #endif
15492 	struct mbuf *m, *n, **np, *smb;
15493 	struct mbuf *top;
15494 	int32_t off, soff;
15495 	int32_t len = *plen;
15496 	int32_t fragsize;
15497 	int32_t len_cp = 0;
15498 	uint32_t mlen, frags;
15499 
15500 	soff = off = the_off;
15501 	smb = m = the_m;
15502 	np = &top;
15503 	top = NULL;
15504 #ifdef KERN_TLS
15505 	if (hw_tls && (m->m_flags & M_EXTPG))
15506 		tls = m->m_epg_tls;
15507 	else
15508 		tls = NULL;
15509 #ifdef INVARIANTS
15510 	start = m;
15511 #endif
15512 #endif
15513 	while (len > 0) {
15514 		if (m == NULL) {
15515 			*plen = len_cp;
15516 			break;
15517 		}
15518 #ifdef KERN_TLS
15519 		if (hw_tls) {
15520 			if (m->m_flags & M_EXTPG)
15521 				ntls = m->m_epg_tls;
15522 			else
15523 				ntls = NULL;
15524 
15525 			/*
15526 			 * Avoid mixing TLS records with handshake
15527 			 * data or TLS records from different
15528 			 * sessions.
15529 			 */
15530 			if (tls != ntls) {
15531 				MPASS(m != start);
15532 				*plen = len_cp;
15533 				break;
15534 			}
15535 		}
15536 #endif
15537 		mlen = min(len, m->m_len - off);
15538 		if (seglimit) {
15539 			/*
15540 			 * For M_EXTPG mbufs, add 3 segments
15541 			 * + 1 in case we are crossing page boundaries
15542 			 * + 2 in case the TLS hdr/trailer are used
15543 			 * It is cheaper to just add the segments
15544 			 * than it is to take the cache miss to look
15545 			 * at the mbuf ext_pgs state in detail.
15546 			 */
15547 			if (m->m_flags & M_EXTPG) {
15548 				fragsize = min(segsize, PAGE_SIZE);
15549 				frags = 3;
15550 			} else {
15551 				fragsize = segsize;
15552 				frags = 0;
15553 			}
15554 
15555 			/* Break if we really can't fit anymore. */
15556 			if ((frags + 1) >= seglimit) {
15557 				*plen =	len_cp;
15558 				break;
15559 			}
15560 
15561 			/*
15562 			 * Reduce size if you can't copy the whole
15563 			 * mbuf. If we can't copy the whole mbuf, also
15564 			 * adjust len so the loop will end after this
15565 			 * mbuf.
15566 			 */
15567 			if ((frags + howmany(mlen, fragsize)) >= seglimit) {
15568 				mlen = (seglimit - frags - 1) * fragsize;
15569 				len = mlen;
15570 				*plen = len_cp + len;
15571 			}
15572 			frags += howmany(mlen, fragsize);
15573 			if (frags == 0)
15574 				frags++;
15575 			seglimit -= frags;
15576 			KASSERT(seglimit > 0,
15577 			    ("%s: seglimit went too low", __func__));
15578 		}
15579 		n = m_get(M_NOWAIT, m->m_type);
15580 		*np = n;
15581 		if (n == NULL)
15582 			goto nospace;
15583 		n->m_len = mlen;
15584 		soff += mlen;
15585 		len_cp += n->m_len;
15586 		if (m->m_flags & (M_EXT|M_EXTPG)) {
15587 			n->m_data = m->m_data + off;
15588 			mb_dupcl(n, m);
15589 		} else {
15590 			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
15591 			    (u_int)n->m_len);
15592 		}
15593 		len -= n->m_len;
15594 		off = 0;
15595 		m = m->m_next;
15596 		np = &n->m_next;
15597 		if (len || (soff == smb->m_len)) {
15598 			/*
15599 			 * We have more so we move forward  or
15600 			 * we have consumed the entire mbuf and
15601 			 * len has fell to 0.
15602 			 */
15603 			soff = 0;
15604 			smb = m;
15605 		}
15606 
15607 	}
15608 	if (fsb != NULL) {
15609 		fsb->m = smb;
15610 		fsb->off = soff;
15611 		if (smb) {
15612 			/*
15613 			 * Save off the size of the mbuf. We do
15614 			 * this so that we can recognize when it
15615 			 * has been trimmed by sbcut() as acks
15616 			 * come in.
15617 			 */
15618 			fsb->o_m_len = smb->m_len;
15619 		} else {
15620 			/*
15621 			 * This is the case where the next mbuf went to NULL. This
15622 			 * means with this copy we have sent everything in the sb.
15623 			 * In theory we could clear the fast_output flag, but lets
15624 			 * not since its possible that we could get more added
15625 			 * and acks that call the extend function which would let
15626 			 * us send more.
15627 			 */
15628 			fsb->o_m_len = 0;
15629 		}
15630 	}
15631 	return (top);
15632 nospace:
15633 	if (top)
15634 		m_freem(top);
15635 	return (NULL);
15636 
15637 }
15638 
15639 /*
15640  * This is a copy of m_copym(), taking the TSO segment size/limit
15641  * constraints into account, and advancing the sndptr as it goes.
15642  */
15643 static struct mbuf *
15644 rack_fo_m_copym(struct tcp_rack *rack, int32_t *plen,
15645 		int32_t seglimit, int32_t segsize, struct mbuf **s_mb, int *s_soff)
15646 {
15647 	struct mbuf *m, *n;
15648 	int32_t soff;
15649 
15650 	soff = rack->r_ctl.fsb.off;
15651 	m = rack->r_ctl.fsb.m;
15652 	if (rack->r_ctl.fsb.o_m_len > m->m_len) {
15653 		/*
15654 		 * The mbuf had the front of it chopped off by an ack
15655 		 * we need to adjust the soff/off by that difference.
15656 		 */
15657 		uint32_t delta;
15658 
15659 		delta = rack->r_ctl.fsb.o_m_len - m->m_len;
15660 		soff -= delta;
15661 	} else if (rack->r_ctl.fsb.o_m_len < m->m_len) {
15662 		/*
15663 		 * The mbuf was expanded probably by
15664 		 * a m_compress. Just update o_m_len.
15665 		 */
15666 		rack->r_ctl.fsb.o_m_len = m->m_len;
15667 	}
15668 	KASSERT(soff >= 0, ("%s, negative off %d", __FUNCTION__, soff));
15669 	KASSERT(*plen >= 0, ("%s, negative len %d", __FUNCTION__, *plen));
15670 	KASSERT(soff < m->m_len, ("%s rack:%p len:%u m:%p m->m_len:%u < off?",
15671 				 __FUNCTION__,
15672 				 rack, *plen, m, m->m_len));
15673 	/* Save off the right location before we copy and advance */
15674 	*s_soff = soff;
15675 	*s_mb = rack->r_ctl.fsb.m;
15676 	n = rack_fo_base_copym(m, soff, plen,
15677 			       &rack->r_ctl.fsb,
15678 			       seglimit, segsize, rack->r_ctl.fsb.hw_tls);
15679 	return (n);
15680 }
15681 
15682 static int
15683 rack_fast_rsm_output(struct tcpcb *tp, struct tcp_rack *rack, struct rack_sendmap *rsm,
15684 		     uint64_t ts_val, uint32_t cts, uint32_t ms_cts, struct timeval *tv, int len, uint8_t doing_tlp)
15685 {
15686 	/*
15687 	 * Enter the fast retransmit path. We are given that a sched_pin is
15688 	 * in place (if accounting is compliled in) and the cycle count taken
15689 	 * at the entry is in the ts_val. The concept her is that the rsm
15690 	 * now holds the mbuf offsets and such so we can directly transmit
15691 	 * without a lot of overhead, the len field is already set for
15692 	 * us to prohibit us from sending too much (usually its 1MSS).
15693 	 */
15694 	struct ip *ip = NULL;
15695 	struct udphdr *udp = NULL;
15696 	struct tcphdr *th = NULL;
15697 	struct mbuf *m = NULL;
15698 	struct inpcb *inp;
15699 	uint8_t *cpto;
15700 	struct tcp_log_buffer *lgb;
15701 #ifdef TCP_ACCOUNTING
15702 	uint64_t crtsc;
15703 	int cnt_thru = 1;
15704 #endif
15705 	struct tcpopt to;
15706 	u_char opt[TCP_MAXOLEN];
15707 	uint32_t hdrlen, optlen;
15708 	int32_t slot, segsiz, max_val, tso = 0, error, ulen = 0;
15709 	uint16_t flags;
15710 	uint32_t if_hw_tsomaxsegcount = 0, startseq;
15711 	uint32_t if_hw_tsomaxsegsize;
15712 
15713 #ifdef INET6
15714 	struct ip6_hdr *ip6 = NULL;
15715 
15716 	if (rack->r_is_v6) {
15717 		ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
15718 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
15719 	} else
15720 #endif				/* INET6 */
15721 	{
15722 		ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
15723 		hdrlen = sizeof(struct tcpiphdr);
15724 	}
15725 	if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) {
15726 		goto failed;
15727 	}
15728 	if (doing_tlp) {
15729 		/* Its a TLP add the flag, it may already be there but be sure */
15730 		rsm->r_flags |= RACK_TLP;
15731 	} else {
15732 		/* If it was a TLP it is not not on this retransmit */
15733 		rsm->r_flags &= ~RACK_TLP;
15734 	}
15735 	startseq = rsm->r_start;
15736 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
15737 	inp = rack->rc_inp;
15738 	to.to_flags = 0;
15739 	flags = tcp_outflags[tp->t_state];
15740 	if (flags & (TH_SYN|TH_RST)) {
15741 		goto failed;
15742 	}
15743 	if (rsm->r_flags & RACK_HAS_FIN) {
15744 		/* We can't send a FIN here */
15745 		goto failed;
15746 	}
15747 	if (flags & TH_FIN) {
15748 		/* We never send a FIN */
15749 		flags &= ~TH_FIN;
15750 	}
15751 	if (tp->t_flags & TF_RCVD_TSTMP) {
15752 		to.to_tsval = ms_cts + tp->ts_offset;
15753 		to.to_tsecr = tp->ts_recent;
15754 		to.to_flags = TOF_TS;
15755 	}
15756 	optlen = tcp_addoptions(&to, opt);
15757 	hdrlen += optlen;
15758 	udp = rack->r_ctl.fsb.udp;
15759 	if (udp)
15760 		hdrlen += sizeof(struct udphdr);
15761 	if (rack->r_ctl.rc_pace_max_segs)
15762 		max_val = rack->r_ctl.rc_pace_max_segs;
15763 	else if (rack->rc_user_set_max_segs)
15764 		max_val = rack->rc_user_set_max_segs * segsiz;
15765 	else
15766 		max_val = len;
15767 	if ((tp->t_flags & TF_TSO) &&
15768 	    V_tcp_do_tso &&
15769 	    (len > segsiz) &&
15770 	    (tp->t_port == 0))
15771 		tso = 1;
15772 #ifdef INET6
15773 	if (MHLEN < hdrlen + max_linkhdr)
15774 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
15775 	else
15776 #endif
15777 		m = m_gethdr(M_NOWAIT, MT_DATA);
15778 	if (m == NULL)
15779 		goto failed;
15780 	m->m_data += max_linkhdr;
15781 	m->m_len = hdrlen;
15782 	th = rack->r_ctl.fsb.th;
15783 	/* Establish the len to send */
15784 	if (len > max_val)
15785 		len = max_val;
15786 	if ((tso) && (len + optlen > tp->t_maxseg)) {
15787 		uint32_t if_hw_tsomax;
15788 		int32_t max_len;
15789 
15790 		/* extract TSO information */
15791 		if_hw_tsomax = tp->t_tsomax;
15792 		if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
15793 		if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
15794 		/*
15795 		 * Check if we should limit by maximum payload
15796 		 * length:
15797 		 */
15798 		if (if_hw_tsomax != 0) {
15799 			/* compute maximum TSO length */
15800 			max_len = (if_hw_tsomax - hdrlen -
15801 				   max_linkhdr);
15802 			if (max_len <= 0) {
15803 				goto failed;
15804 			} else if (len > max_len) {
15805 				len = max_len;
15806 			}
15807 		}
15808 		if (len <= segsiz) {
15809 			/*
15810 			 * In case there are too many small fragments don't
15811 			 * use TSO:
15812 			 */
15813 			tso = 0;
15814 		}
15815 	} else {
15816 		tso = 0;
15817 	}
15818 	if ((tso == 0) && (len > segsiz))
15819 		len = segsiz;
15820 	if ((len == 0) ||
15821 	    (len <= MHLEN - hdrlen - max_linkhdr)) {
15822 		goto failed;
15823 	}
15824 	th->th_seq = htonl(rsm->r_start);
15825 	th->th_ack = htonl(tp->rcv_nxt);
15826 	/*
15827 	 * The PUSH bit should only be applied
15828 	 * if the full retransmission is made. If
15829 	 * we are sending less than this is the
15830 	 * left hand edge and should not have
15831 	 * the PUSH bit.
15832 	 */
15833 	if ((rsm->r_flags & RACK_HAD_PUSH) &&
15834 	    (len == (rsm->r_end - rsm->r_start)))
15835 		flags |= TH_PUSH;
15836 	th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale));
15837 	if (th->th_win == 0) {
15838 		tp->t_sndzerowin++;
15839 		tp->t_flags |= TF_RXWIN0SENT;
15840 	} else
15841 		tp->t_flags &= ~TF_RXWIN0SENT;
15842 	if (rsm->r_flags & RACK_TLP) {
15843 		/*
15844 		 * TLP should not count in retran count, but
15845 		 * in its own bin
15846 		 */
15847 		counter_u64_add(rack_tlp_retran, 1);
15848 		counter_u64_add(rack_tlp_retran_bytes, len);
15849 	} else {
15850 		tp->t_sndrexmitpack++;
15851 		KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
15852 		KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
15853 	}
15854 #ifdef STATS
15855 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
15856 				 len);
15857 #endif
15858 	if (rsm->m == NULL)
15859 		goto failed;
15860 	if (rsm->orig_m_len != rsm->m->m_len) {
15861 		/* Fix up the orig_m_len and possibly the mbuf offset */
15862 		rack_adjust_orig_mlen(rsm);
15863 	}
15864 	m->m_next = rack_fo_base_copym(rsm->m, rsm->soff, &len, NULL, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, rsm->r_hw_tls);
15865 	if (len <= segsiz) {
15866 		/*
15867 		 * Must have ran out of mbufs for the copy
15868 		 * shorten it to no longer need tso. Lets
15869 		 * not put on sendalot since we are low on
15870 		 * mbufs.
15871 		 */
15872 		tso = 0;
15873 	}
15874 	if ((m->m_next == NULL) || (len <= 0)){
15875 		goto failed;
15876 	}
15877 	if (udp) {
15878 		if (rack->r_is_v6)
15879 			ulen = hdrlen + len - sizeof(struct ip6_hdr);
15880 		else
15881 			ulen = hdrlen + len - sizeof(struct ip);
15882 		udp->uh_ulen = htons(ulen);
15883 	}
15884 	m->m_pkthdr.rcvif = (struct ifnet *)0;
15885 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
15886 	    (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) {
15887 		int ect = tcp_ecn_output_established(tp, &flags, len, true);
15888 		if ((tp->t_state == TCPS_SYN_RECEIVED) &&
15889 		    (tp->t_flags2 & TF2_ECN_SND_ECE))
15890 		    tp->t_flags2 &= ~TF2_ECN_SND_ECE;
15891 #ifdef INET6
15892 		if (rack->r_is_v6) {
15893 		    ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
15894 		    ip6->ip6_flow |= htonl(ect << 20);
15895 		}
15896 		else
15897 #endif
15898 		{
15899 		    ip->ip_tos &= ~IPTOS_ECN_MASK;
15900 		    ip->ip_tos |= ect;
15901 		}
15902 	}
15903 	tcp_set_flags(th, flags);
15904 	m->m_pkthdr.len = hdrlen + len;	/* in6_cksum() need this */
15905 #ifdef INET6
15906 	if (rack->r_is_v6) {
15907 		if (tp->t_port) {
15908 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
15909 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
15910 			udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
15911 			th->th_sum = htons(0);
15912 			UDPSTAT_INC(udps_opackets);
15913 		} else {
15914 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
15915 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
15916 			th->th_sum = in6_cksum_pseudo(ip6,
15917 						      sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
15918 						      0);
15919 		}
15920 	}
15921 #endif
15922 #if defined(INET6) && defined(INET)
15923 	else
15924 #endif
15925 #ifdef INET
15926 	{
15927 		if (tp->t_port) {
15928 			m->m_pkthdr.csum_flags = CSUM_UDP;
15929 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
15930 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
15931 						ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
15932 			th->th_sum = htons(0);
15933 			UDPSTAT_INC(udps_opackets);
15934 		} else {
15935 			m->m_pkthdr.csum_flags = CSUM_TCP;
15936 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
15937 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
15938 					       ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
15939 									IPPROTO_TCP + len + optlen));
15940 		}
15941 		/* IP version must be set here for ipv4/ipv6 checking later */
15942 		KASSERT(ip->ip_v == IPVERSION,
15943 			("%s: IP version incorrect: %d", __func__, ip->ip_v));
15944 	}
15945 #endif
15946 	if (tso) {
15947 		KASSERT(len > tp->t_maxseg - optlen,
15948 			("%s: len <= tso_segsz tp:%p", __func__, tp));
15949 		m->m_pkthdr.csum_flags |= CSUM_TSO;
15950 		m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen;
15951 	}
15952 #ifdef INET6
15953 	if (rack->r_is_v6) {
15954 		ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit;
15955 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
15956 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
15957 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
15958 		else
15959 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
15960 	}
15961 #endif
15962 #if defined(INET) && defined(INET6)
15963 	else
15964 #endif
15965 #ifdef INET
15966 	{
15967 		ip->ip_len = htons(m->m_pkthdr.len);
15968 		ip->ip_ttl = rack->r_ctl.fsb.hoplimit;
15969 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
15970 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
15971 			if (tp->t_port == 0 || len < V_tcp_minmss) {
15972 				ip->ip_off |= htons(IP_DF);
15973 			}
15974 		} else {
15975 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
15976 		}
15977 	}
15978 #endif
15979 	/* Time to copy in our header */
15980 	cpto = mtod(m, uint8_t *);
15981 	memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
15982 	th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
15983 	if (optlen) {
15984 		bcopy(opt, th + 1, optlen);
15985 		th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
15986 	} else {
15987 		th->th_off = sizeof(struct tcphdr) >> 2;
15988 	}
15989 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
15990 		union tcp_log_stackspecific log;
15991 
15992 		if (rsm->r_flags & RACK_RWND_COLLAPSED) {
15993 			rack_log_collapse(rack, rsm->r_start, rsm->r_end, 0, __LINE__, 5, rsm->r_flags, rsm);
15994 			counter_u64_add(rack_collapsed_win_rxt, 1);
15995 			counter_u64_add(rack_collapsed_win_rxt_bytes, (rsm->r_end - rsm->r_start));
15996 		}
15997 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
15998 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
15999 		if (rack->rack_no_prr)
16000 			log.u_bbr.flex1 = 0;
16001 		else
16002 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
16003 		log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
16004 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
16005 		log.u_bbr.flex4 = max_val;
16006 		log.u_bbr.flex5 = 0;
16007 		/* Save off the early/late values */
16008 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
16009 		log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
16010 		log.u_bbr.bw_inuse = rack_get_bw(rack);
16011 		if (doing_tlp == 0)
16012 			log.u_bbr.flex8 = 1;
16013 		else
16014 			log.u_bbr.flex8 = 2;
16015 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
16016 		log.u_bbr.flex7 = 55;
16017 		log.u_bbr.pkts_out = tp->t_maxseg;
16018 		log.u_bbr.timeStamp = cts;
16019 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
16020 		log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use;
16021 		log.u_bbr.delivered = 0;
16022 		lgb = tcp_log_event_(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
16023 				     len, &log, false, NULL, NULL, 0, tv);
16024 	} else
16025 		lgb = NULL;
16026 #ifdef INET6
16027 	if (rack->r_is_v6) {
16028 		error = ip6_output(m, NULL,
16029 				   &inp->inp_route6,
16030 				   0, NULL, NULL, inp);
16031 	}
16032 #endif
16033 #if defined(INET) && defined(INET6)
16034 	else
16035 #endif
16036 #ifdef INET
16037 	{
16038 		error = ip_output(m, NULL,
16039 				  &inp->inp_route,
16040 				  0, 0, inp);
16041 	}
16042 #endif
16043 	m = NULL;
16044 	if (lgb) {
16045 		lgb->tlb_errno = error;
16046 		lgb = NULL;
16047 	}
16048 	if (error) {
16049 		goto failed;
16050 	}
16051 	rack_log_output(tp, &to, len, rsm->r_start, flags, error, rack_to_usec_ts(tv),
16052 			rsm, RACK_SENT_FP, rsm->m, rsm->soff, rsm->r_hw_tls);
16053 	if (doing_tlp && (rack->fast_rsm_hack == 0)) {
16054 		rack->rc_tlp_in_progress = 1;
16055 		rack->r_ctl.rc_tlp_cnt_out++;
16056 	}
16057 	if (error == 0) {
16058 		tcp_account_for_send(tp, len, 1, doing_tlp, rsm->r_hw_tls);
16059 		if (doing_tlp) {
16060 			rack->rc_last_sent_tlp_past_cumack = 0;
16061 			rack->rc_last_sent_tlp_seq_valid = 1;
16062 			rack->r_ctl.last_sent_tlp_seq = rsm->r_start;
16063 			rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start;
16064 		}
16065 	}
16066 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
16067 	rack->forced_ack = 0;	/* If we send something zap the FA flag */
16068 	if (IN_FASTRECOVERY(tp->t_flags) && rsm)
16069 		rack->r_ctl.retran_during_recovery += len;
16070 	{
16071 		int idx;
16072 
16073 		idx = (len / segsiz) + 3;
16074 		if (idx >= TCP_MSS_ACCT_ATIMER)
16075 			counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
16076 		else
16077 			counter_u64_add(rack_out_size[idx], 1);
16078 	}
16079 	if (tp->t_rtttime == 0) {
16080 		tp->t_rtttime = ticks;
16081 		tp->t_rtseq = startseq;
16082 		KMOD_TCPSTAT_INC(tcps_segstimed);
16083 	}
16084 	counter_u64_add(rack_fto_rsm_send, 1);
16085 	if (error && (error == ENOBUFS)) {
16086 		if (rack->r_ctl.crte != NULL) {
16087 			rack_trace_point(rack, RACK_TP_HWENOBUF);
16088 		} else
16089 			rack_trace_point(rack, RACK_TP_ENOBUF);
16090 		slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC);
16091 		if (rack->rc_enobuf < 0x7f)
16092 			rack->rc_enobuf++;
16093 		if (slot < (10 * HPTS_USEC_IN_MSEC))
16094 			slot = 10 * HPTS_USEC_IN_MSEC;
16095 	} else
16096 		slot = rack_get_pacing_delay(rack, tp, len, NULL, segsiz);
16097 	if ((slot == 0) ||
16098 	    (rack->rc_always_pace == 0) ||
16099 	    (rack->r_rr_config == 1)) {
16100 		/*
16101 		 * We have no pacing set or we
16102 		 * are using old-style rack or
16103 		 * we are overridden to use the old 1ms pacing.
16104 		 */
16105 		slot = rack->r_ctl.rc_min_to;
16106 	}
16107 	rack_start_hpts_timer(rack, tp, cts, slot, len, 0);
16108 #ifdef TCP_ACCOUNTING
16109 	crtsc = get_cyclecount();
16110 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16111 		tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru;
16112 	}
16113 	counter_u64_add(tcp_cnt_counters[SND_OUT_DATA], cnt_thru);
16114 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16115 		tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
16116 	}
16117 	counter_u64_add(tcp_proc_time[SND_OUT_DATA], (crtsc - ts_val));
16118 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16119 		tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((len + segsiz - 1) / segsiz);
16120 	}
16121 	counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((len + segsiz - 1) / segsiz));
16122 	sched_unpin();
16123 #endif
16124 	return (0);
16125 failed:
16126 	if (m)
16127 		m_free(m);
16128 	return (-1);
16129 }
16130 
16131 static void
16132 rack_sndbuf_autoscale(struct tcp_rack *rack)
16133 {
16134 	/*
16135 	 * Automatic sizing of send socket buffer.  Often the send buffer
16136 	 * size is not optimally adjusted to the actual network conditions
16137 	 * at hand (delay bandwidth product).  Setting the buffer size too
16138 	 * small limits throughput on links with high bandwidth and high
16139 	 * delay (eg. trans-continental/oceanic links).  Setting the
16140 	 * buffer size too big consumes too much real kernel memory,
16141 	 * especially with many connections on busy servers.
16142 	 *
16143 	 * The criteria to step up the send buffer one notch are:
16144 	 *  1. receive window of remote host is larger than send buffer
16145 	 *     (with a fudge factor of 5/4th);
16146 	 *  2. send buffer is filled to 7/8th with data (so we actually
16147 	 *     have data to make use of it);
16148 	 *  3. send buffer fill has not hit maximal automatic size;
16149 	 *  4. our send window (slow start and cogestion controlled) is
16150 	 *     larger than sent but unacknowledged data in send buffer.
16151 	 *
16152 	 * Note that the rack version moves things much faster since
16153 	 * we want to avoid hitting cache lines in the rack_fast_output()
16154 	 * path so this is called much less often and thus moves
16155 	 * the SB forward by a percentage.
16156 	 */
16157 	struct socket *so;
16158 	struct tcpcb *tp;
16159 	uint32_t sendwin, scaleup;
16160 
16161 	tp = rack->rc_tp;
16162 	so = rack->rc_inp->inp_socket;
16163 	sendwin = min(rack->r_ctl.cwnd_to_use, tp->snd_wnd);
16164 	if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) {
16165 		if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
16166 		    sbused(&so->so_snd) >=
16167 		    (so->so_snd.sb_hiwat / 8 * 7) &&
16168 		    sbused(&so->so_snd) < V_tcp_autosndbuf_max &&
16169 		    sendwin >= (sbused(&so->so_snd) -
16170 		    (tp->snd_nxt - tp->snd_una))) {
16171 			if (rack_autosndbuf_inc)
16172 				scaleup = (rack_autosndbuf_inc * so->so_snd.sb_hiwat) / 100;
16173 			else
16174 				scaleup = V_tcp_autosndbuf_inc;
16175 			if (scaleup < V_tcp_autosndbuf_inc)
16176 				scaleup = V_tcp_autosndbuf_inc;
16177 			scaleup += so->so_snd.sb_hiwat;
16178 			if (scaleup > V_tcp_autosndbuf_max)
16179 				scaleup = V_tcp_autosndbuf_max;
16180 			if (!sbreserve_locked(so, SO_SND, scaleup, curthread))
16181 				so->so_snd.sb_flags &= ~SB_AUTOSIZE;
16182 		}
16183 	}
16184 }
16185 
16186 static int
16187 rack_fast_output(struct tcpcb *tp, struct tcp_rack *rack, uint64_t ts_val,
16188 		 uint32_t cts, uint32_t ms_cts, struct timeval *tv, long tot_len, int *send_err)
16189 {
16190 	/*
16191 	 * Enter to do fast output. We are given that the sched_pin is
16192 	 * in place (if accounting is compiled in) and the cycle count taken
16193 	 * at entry is in place in ts_val. The idea here is that
16194 	 * we know how many more bytes needs to be sent (presumably either
16195 	 * during pacing or to fill the cwnd and that was greater than
16196 	 * the max-burst). We have how much to send and all the info we
16197 	 * need to just send.
16198 	 */
16199 	struct ip *ip = NULL;
16200 	struct udphdr *udp = NULL;
16201 	struct tcphdr *th = NULL;
16202 	struct mbuf *m, *s_mb;
16203 	struct inpcb *inp;
16204 	uint8_t *cpto;
16205 	struct tcp_log_buffer *lgb;
16206 #ifdef TCP_ACCOUNTING
16207 	uint64_t crtsc;
16208 #endif
16209 	struct tcpopt to;
16210 	u_char opt[TCP_MAXOLEN];
16211 	uint32_t hdrlen, optlen;
16212 #ifdef TCP_ACCOUNTING
16213 	int cnt_thru = 1;
16214 #endif
16215 	int32_t slot, segsiz, len, max_val, tso = 0, sb_offset, error, ulen = 0;
16216 	uint16_t flags;
16217 	uint32_t s_soff;
16218 	uint32_t if_hw_tsomaxsegcount = 0, startseq;
16219 	uint32_t if_hw_tsomaxsegsize;
16220 	uint16_t add_flag = RACK_SENT_FP;
16221 #ifdef INET6
16222 	struct ip6_hdr *ip6 = NULL;
16223 
16224 	if (rack->r_is_v6) {
16225 		ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
16226 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
16227 	} else
16228 #endif				/* INET6 */
16229 	{
16230 		ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
16231 		hdrlen = sizeof(struct tcpiphdr);
16232 	}
16233 	if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) {
16234 		m = NULL;
16235 		goto failed;
16236 	}
16237 	startseq = tp->snd_max;
16238 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
16239 	inp = rack->rc_inp;
16240 	len = rack->r_ctl.fsb.left_to_send;
16241 	to.to_flags = 0;
16242 	flags = rack->r_ctl.fsb.tcp_flags;
16243 	if (tp->t_flags & TF_RCVD_TSTMP) {
16244 		to.to_tsval = ms_cts + tp->ts_offset;
16245 		to.to_tsecr = tp->ts_recent;
16246 		to.to_flags = TOF_TS;
16247 	}
16248 	optlen = tcp_addoptions(&to, opt);
16249 	hdrlen += optlen;
16250 	udp = rack->r_ctl.fsb.udp;
16251 	if (udp)
16252 		hdrlen += sizeof(struct udphdr);
16253 	if (rack->r_ctl.rc_pace_max_segs)
16254 		max_val = rack->r_ctl.rc_pace_max_segs;
16255 	else if (rack->rc_user_set_max_segs)
16256 		max_val = rack->rc_user_set_max_segs * segsiz;
16257 	else
16258 		max_val = len;
16259 	if ((tp->t_flags & TF_TSO) &&
16260 	    V_tcp_do_tso &&
16261 	    (len > segsiz) &&
16262 	    (tp->t_port == 0))
16263 		tso = 1;
16264 again:
16265 #ifdef INET6
16266 	if (MHLEN < hdrlen + max_linkhdr)
16267 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
16268 	else
16269 #endif
16270 		m = m_gethdr(M_NOWAIT, MT_DATA);
16271 	if (m == NULL)
16272 		goto failed;
16273 	m->m_data += max_linkhdr;
16274 	m->m_len = hdrlen;
16275 	th = rack->r_ctl.fsb.th;
16276 	/* Establish the len to send */
16277 	if (len > max_val)
16278 		len = max_val;
16279 	if ((tso) && (len + optlen > tp->t_maxseg)) {
16280 		uint32_t if_hw_tsomax;
16281 		int32_t max_len;
16282 
16283 		/* extract TSO information */
16284 		if_hw_tsomax = tp->t_tsomax;
16285 		if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
16286 		if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
16287 		/*
16288 		 * Check if we should limit by maximum payload
16289 		 * length:
16290 		 */
16291 		if (if_hw_tsomax != 0) {
16292 			/* compute maximum TSO length */
16293 			max_len = (if_hw_tsomax - hdrlen -
16294 				   max_linkhdr);
16295 			if (max_len <= 0) {
16296 				goto failed;
16297 			} else if (len > max_len) {
16298 				len = max_len;
16299 			}
16300 		}
16301 		if (len <= segsiz) {
16302 			/*
16303 			 * In case there are too many small fragments don't
16304 			 * use TSO:
16305 			 */
16306 			tso = 0;
16307 		}
16308 	} else {
16309 		tso = 0;
16310 	}
16311 	if ((tso == 0) && (len > segsiz))
16312 		len = segsiz;
16313 	if ((len == 0) ||
16314 	    (len <= MHLEN - hdrlen - max_linkhdr)) {
16315 		goto failed;
16316 	}
16317 	sb_offset = tp->snd_max - tp->snd_una;
16318 	th->th_seq = htonl(tp->snd_max);
16319 	th->th_ack = htonl(tp->rcv_nxt);
16320 	th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale));
16321 	if (th->th_win == 0) {
16322 		tp->t_sndzerowin++;
16323 		tp->t_flags |= TF_RXWIN0SENT;
16324 	} else
16325 		tp->t_flags &= ~TF_RXWIN0SENT;
16326 	tp->snd_up = tp->snd_una;	/* drag it along, its deprecated */
16327 	KMOD_TCPSTAT_INC(tcps_sndpack);
16328 	KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
16329 #ifdef STATS
16330 	stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
16331 				 len);
16332 #endif
16333 	if (rack->r_ctl.fsb.m == NULL)
16334 		goto failed;
16335 
16336 	/* s_mb and s_soff are saved for rack_log_output */
16337 	m->m_next = rack_fo_m_copym(rack, &len, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize,
16338 				    &s_mb, &s_soff);
16339 	if (len <= segsiz) {
16340 		/*
16341 		 * Must have ran out of mbufs for the copy
16342 		 * shorten it to no longer need tso. Lets
16343 		 * not put on sendalot since we are low on
16344 		 * mbufs.
16345 		 */
16346 		tso = 0;
16347 	}
16348 	if (rack->r_ctl.fsb.rfo_apply_push &&
16349 	    (len == rack->r_ctl.fsb.left_to_send)) {
16350 		flags |= TH_PUSH;
16351 		add_flag |= RACK_HAD_PUSH;
16352 	}
16353 	if ((m->m_next == NULL) || (len <= 0)){
16354 		goto failed;
16355 	}
16356 	if (udp) {
16357 		if (rack->r_is_v6)
16358 			ulen = hdrlen + len - sizeof(struct ip6_hdr);
16359 		else
16360 			ulen = hdrlen + len - sizeof(struct ip);
16361 		udp->uh_ulen = htons(ulen);
16362 	}
16363 	m->m_pkthdr.rcvif = (struct ifnet *)0;
16364 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
16365 	    (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) {
16366 		int ect = tcp_ecn_output_established(tp, &flags, len, false);
16367 		if ((tp->t_state == TCPS_SYN_RECEIVED) &&
16368 		    (tp->t_flags2 & TF2_ECN_SND_ECE))
16369 			tp->t_flags2 &= ~TF2_ECN_SND_ECE;
16370 #ifdef INET6
16371 		if (rack->r_is_v6) {
16372 			ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
16373 			ip6->ip6_flow |= htonl(ect << 20);
16374 		}
16375 		else
16376 #endif
16377 		{
16378 			ip->ip_tos &= ~IPTOS_ECN_MASK;
16379 			ip->ip_tos |= ect;
16380 		}
16381 	}
16382 	tcp_set_flags(th, flags);
16383 	m->m_pkthdr.len = hdrlen + len;	/* in6_cksum() need this */
16384 #ifdef INET6
16385 	if (rack->r_is_v6) {
16386 		if (tp->t_port) {
16387 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
16388 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
16389 			udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
16390 			th->th_sum = htons(0);
16391 			UDPSTAT_INC(udps_opackets);
16392 		} else {
16393 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
16394 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
16395 			th->th_sum = in6_cksum_pseudo(ip6,
16396 						      sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
16397 						      0);
16398 		}
16399 	}
16400 #endif
16401 #if defined(INET6) && defined(INET)
16402 	else
16403 #endif
16404 #ifdef INET
16405 	{
16406 		if (tp->t_port) {
16407 			m->m_pkthdr.csum_flags = CSUM_UDP;
16408 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
16409 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
16410 						ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
16411 			th->th_sum = htons(0);
16412 			UDPSTAT_INC(udps_opackets);
16413 		} else {
16414 			m->m_pkthdr.csum_flags = CSUM_TCP;
16415 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
16416 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
16417 					       ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
16418 									IPPROTO_TCP + len + optlen));
16419 		}
16420 		/* IP version must be set here for ipv4/ipv6 checking later */
16421 		KASSERT(ip->ip_v == IPVERSION,
16422 			("%s: IP version incorrect: %d", __func__, ip->ip_v));
16423 	}
16424 #endif
16425 	if (tso) {
16426 		KASSERT(len > tp->t_maxseg - optlen,
16427 			("%s: len <= tso_segsz tp:%p", __func__, tp));
16428 		m->m_pkthdr.csum_flags |= CSUM_TSO;
16429 		m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen;
16430 	}
16431 #ifdef INET6
16432 	if (rack->r_is_v6) {
16433 		ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit;
16434 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
16435 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
16436 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
16437 		else
16438 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
16439 	}
16440 #endif
16441 #if defined(INET) && defined(INET6)
16442 	else
16443 #endif
16444 #ifdef INET
16445 	{
16446 		ip->ip_len = htons(m->m_pkthdr.len);
16447 		ip->ip_ttl = rack->r_ctl.fsb.hoplimit;
16448 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
16449 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
16450 			if (tp->t_port == 0 || len < V_tcp_minmss) {
16451 				ip->ip_off |= htons(IP_DF);
16452 			}
16453 		} else {
16454 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
16455 		}
16456 	}
16457 #endif
16458 	/* Time to copy in our header */
16459 	cpto = mtod(m, uint8_t *);
16460 	memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
16461 	th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
16462 	if (optlen) {
16463 		bcopy(opt, th + 1, optlen);
16464 		th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
16465 	} else {
16466 		th->th_off = sizeof(struct tcphdr) >> 2;
16467 	}
16468 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
16469 		union tcp_log_stackspecific log;
16470 
16471 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
16472 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
16473 		if (rack->rack_no_prr)
16474 			log.u_bbr.flex1 = 0;
16475 		else
16476 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
16477 		log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
16478 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
16479 		log.u_bbr.flex4 = max_val;
16480 		log.u_bbr.flex5 = 0;
16481 		/* Save off the early/late values */
16482 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
16483 		log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
16484 		log.u_bbr.bw_inuse = rack_get_bw(rack);
16485 		log.u_bbr.flex8 = 0;
16486 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
16487 		log.u_bbr.flex7 = 44;
16488 		log.u_bbr.pkts_out = tp->t_maxseg;
16489 		log.u_bbr.timeStamp = cts;
16490 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
16491 		log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use;
16492 		log.u_bbr.delivered = 0;
16493 		lgb = tcp_log_event_(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
16494 				     len, &log, false, NULL, NULL, 0, tv);
16495 	} else
16496 		lgb = NULL;
16497 #ifdef INET6
16498 	if (rack->r_is_v6) {
16499 		error = ip6_output(m, NULL,
16500 				   &inp->inp_route6,
16501 				   0, NULL, NULL, inp);
16502 	}
16503 #endif
16504 #if defined(INET) && defined(INET6)
16505 	else
16506 #endif
16507 #ifdef INET
16508 	{
16509 		error = ip_output(m, NULL,
16510 				  &inp->inp_route,
16511 				  0, 0, inp);
16512 	}
16513 #endif
16514 	if (lgb) {
16515 		lgb->tlb_errno = error;
16516 		lgb = NULL;
16517 	}
16518 	if (error) {
16519 		*send_err = error;
16520 		m = NULL;
16521 		goto failed;
16522 	}
16523 	rack_log_output(tp, &to, len, tp->snd_max, flags, error, rack_to_usec_ts(tv),
16524 			NULL, add_flag, s_mb, s_soff, rack->r_ctl.fsb.hw_tls);
16525 	m = NULL;
16526 	if (tp->snd_una == tp->snd_max) {
16527 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
16528 		rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
16529 		tp->t_acktime = ticks;
16530 	}
16531 	if (error == 0)
16532 		tcp_account_for_send(tp, len, 0, 0, rack->r_ctl.fsb.hw_tls);
16533 
16534 	rack->forced_ack = 0;	/* If we send something zap the FA flag */
16535 	tot_len += len;
16536 	if ((tp->t_flags & TF_GPUTINPROG) == 0)
16537 		rack_start_gp_measurement(tp, rack, tp->snd_max, sb_offset);
16538 	tp->snd_max += len;
16539 	tp->snd_nxt = tp->snd_max;
16540 	{
16541 		int idx;
16542 
16543 		idx = (len / segsiz) + 3;
16544 		if (idx >= TCP_MSS_ACCT_ATIMER)
16545 			counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
16546 		else
16547 			counter_u64_add(rack_out_size[idx], 1);
16548 	}
16549 	if (len <= rack->r_ctl.fsb.left_to_send)
16550 		rack->r_ctl.fsb.left_to_send -= len;
16551 	else
16552 		rack->r_ctl.fsb.left_to_send = 0;
16553 	if (rack->r_ctl.fsb.left_to_send < segsiz) {
16554 		rack->r_fast_output = 0;
16555 		rack->r_ctl.fsb.left_to_send = 0;
16556 		/* At the end of fast_output scale up the sb */
16557 		SOCKBUF_LOCK(&rack->rc_inp->inp_socket->so_snd);
16558 		rack_sndbuf_autoscale(rack);
16559 		SOCKBUF_UNLOCK(&rack->rc_inp->inp_socket->so_snd);
16560 	}
16561 	if (tp->t_rtttime == 0) {
16562 		tp->t_rtttime = ticks;
16563 		tp->t_rtseq = startseq;
16564 		KMOD_TCPSTAT_INC(tcps_segstimed);
16565 	}
16566 	if ((rack->r_ctl.fsb.left_to_send >= segsiz) &&
16567 	    (max_val > len) &&
16568 	    (tso == 0)) {
16569 		max_val -= len;
16570 		len = segsiz;
16571 		th = rack->r_ctl.fsb.th;
16572 #ifdef TCP_ACCOUNTING
16573 		cnt_thru++;
16574 #endif
16575 		goto again;
16576 	}
16577 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
16578 	counter_u64_add(rack_fto_send, 1);
16579 	slot = rack_get_pacing_delay(rack, tp, tot_len, NULL, segsiz);
16580 	rack_start_hpts_timer(rack, tp, cts, slot, tot_len, 0);
16581 #ifdef TCP_ACCOUNTING
16582 	crtsc = get_cyclecount();
16583 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16584 		tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru;
16585 	}
16586 	counter_u64_add(tcp_cnt_counters[SND_OUT_DATA], cnt_thru);
16587 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16588 		tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
16589 	}
16590 	counter_u64_add(tcp_proc_time[SND_OUT_DATA], (crtsc - ts_val));
16591 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16592 		tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len + segsiz - 1) / segsiz);
16593 	}
16594 	counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((tot_len + segsiz - 1) / segsiz));
16595 	sched_unpin();
16596 #endif
16597 	return (0);
16598 failed:
16599 	if (m)
16600 		m_free(m);
16601 	rack->r_fast_output = 0;
16602 	return (-1);
16603 }
16604 
16605 static struct rack_sendmap *
16606 rack_check_collapsed(struct tcp_rack *rack, uint32_t cts)
16607 {
16608 	struct rack_sendmap *rsm = NULL;
16609 	struct rack_sendmap fe;
16610 	int thresh;
16611 
16612 restart:
16613 	fe.r_start = rack->r_ctl.last_collapse_point;
16614 	rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
16615 	if ((rsm == NULL) || ((rsm->r_flags & RACK_RWND_COLLAPSED) == 0)) {
16616 		/* Nothing, strange turn off validity  */
16617 		rack->r_collapse_point_valid = 0;
16618 		return (NULL);
16619 	}
16620 	/* Can we send it yet? */
16621 	if (rsm->r_end > (rack->rc_tp->snd_una + rack->rc_tp->snd_wnd)) {
16622 		/*
16623 		 * Receiver window has not grown enough for
16624 		 * the segment to be put on the wire.
16625 		 */
16626 		return (NULL);
16627 	}
16628 	if (rsm->r_flags & RACK_ACKED) {
16629 		/*
16630 		 * It has been sacked, lets move to the
16631 		 * next one if possible.
16632 		 */
16633 		rack->r_ctl.last_collapse_point = rsm->r_end;
16634 		/* Are we done? */
16635 		if (SEQ_GEQ(rack->r_ctl.last_collapse_point,
16636 			    rack->r_ctl.high_collapse_point)) {
16637 			rack->r_collapse_point_valid = 0;
16638 			return (NULL);
16639 		}
16640 		goto restart;
16641 	}
16642 	/* Now has it been long enough ? */
16643 	thresh = rack_calc_thresh_rack(rack, rack_grab_rtt(rack->rc_tp, rack), cts);
16644 	if ((cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])) > thresh) {
16645 		rack_log_collapse(rack, rsm->r_start,
16646 				  (cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])),
16647 				  thresh, __LINE__, 6, rsm->r_flags, rsm);
16648 		return (rsm);
16649 	}
16650 	/* Not enough time */
16651 	rack_log_collapse(rack, rsm->r_start,
16652 			  (cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])),
16653 			  thresh, __LINE__, 7, rsm->r_flags, rsm);
16654 	return (NULL);
16655 }
16656 
16657 static int
16658 rack_output(struct tcpcb *tp)
16659 {
16660 	struct socket *so;
16661 	uint32_t recwin;
16662 	uint32_t sb_offset, s_moff = 0;
16663 	int32_t len, error = 0;
16664 	uint16_t flags;
16665 	struct mbuf *m, *s_mb = NULL;
16666 	struct mbuf *mb;
16667 	uint32_t if_hw_tsomaxsegcount = 0;
16668 	uint32_t if_hw_tsomaxsegsize;
16669 	int32_t segsiz, minseg;
16670 	long tot_len_this_send = 0;
16671 #ifdef INET
16672 	struct ip *ip = NULL;
16673 #endif
16674 	struct udphdr *udp = NULL;
16675 	struct tcp_rack *rack;
16676 	struct tcphdr *th;
16677 	uint8_t pass = 0;
16678 	uint8_t mark = 0;
16679 	uint8_t wanted_cookie = 0;
16680 	u_char opt[TCP_MAXOLEN];
16681 	unsigned ipoptlen, optlen, hdrlen, ulen=0;
16682 	uint32_t rack_seq;
16683 
16684 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
16685 	unsigned ipsec_optlen = 0;
16686 
16687 #endif
16688 	int32_t idle, sendalot;
16689 	int32_t sub_from_prr = 0;
16690 	volatile int32_t sack_rxmit;
16691 	struct rack_sendmap *rsm = NULL;
16692 	int32_t tso, mtu;
16693 	struct tcpopt to;
16694 	int32_t slot = 0;
16695 	int32_t sup_rack = 0;
16696 	uint32_t cts, ms_cts, delayed, early;
16697 	uint16_t add_flag = RACK_SENT_SP;
16698 	/* The doing_tlp flag will be set by the actual rack_timeout_tlp() */
16699 	uint8_t hpts_calling,  doing_tlp = 0;
16700 	uint32_t cwnd_to_use, pace_max_seg;
16701 	int32_t do_a_prefetch = 0;
16702 	int32_t prefetch_rsm = 0;
16703 	int32_t orig_len = 0;
16704 	struct timeval tv;
16705 	int32_t prefetch_so_done = 0;
16706 	struct tcp_log_buffer *lgb;
16707 	struct inpcb *inp;
16708 	struct sockbuf *sb;
16709 	uint64_t ts_val = 0;
16710 #ifdef TCP_ACCOUNTING
16711 	uint64_t crtsc;
16712 #endif
16713 #ifdef INET6
16714 	struct ip6_hdr *ip6 = NULL;
16715 	int32_t isipv6;
16716 #endif
16717 	bool hw_tls = false;
16718 
16719 	/* setup and take the cache hits here */
16720 	rack = (struct tcp_rack *)tp->t_fb_ptr;
16721 #ifdef TCP_ACCOUNTING
16722 	sched_pin();
16723 	ts_val = get_cyclecount();
16724 #endif
16725 	hpts_calling = rack->rc_inp->inp_hpts_calls;
16726 	NET_EPOCH_ASSERT();
16727 	INP_WLOCK_ASSERT(rack->rc_inp);
16728 #ifdef TCP_OFFLOAD
16729 	if (tp->t_flags & TF_TOE) {
16730 #ifdef TCP_ACCOUNTING
16731 		sched_unpin();
16732 #endif
16733 		return (tcp_offload_output(tp));
16734 	}
16735 #endif
16736 	/*
16737 	 * For TFO connections in SYN_RECEIVED, only allow the initial
16738 	 * SYN|ACK and those sent by the retransmit timer.
16739 	 */
16740 	if (IS_FASTOPEN(tp->t_flags) &&
16741 	    (tp->t_state == TCPS_SYN_RECEIVED) &&
16742 	    SEQ_GT(tp->snd_max, tp->snd_una) &&    /* initial SYN|ACK sent */
16743 	    (rack->r_ctl.rc_resend == NULL)) {         /* not a retransmit */
16744 #ifdef TCP_ACCOUNTING
16745 		sched_unpin();
16746 #endif
16747 		return (0);
16748 	}
16749 #ifdef INET6
16750 	if (rack->r_state) {
16751 		/* Use the cache line loaded if possible */
16752 		isipv6 = rack->r_is_v6;
16753 	} else {
16754 		isipv6 = (rack->rc_inp->inp_vflag & INP_IPV6) != 0;
16755 	}
16756 #endif
16757 	early = 0;
16758 	cts = tcp_get_usecs(&tv);
16759 	ms_cts = tcp_tv_to_mssectick(&tv);
16760 	if (((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) &&
16761 	    tcp_in_hpts(rack->rc_inp)) {
16762 		/*
16763 		 * We are on the hpts for some timer but not hptsi output.
16764 		 * Remove from the hpts unconditionally.
16765 		 */
16766 		rack_timer_cancel(tp, rack, cts, __LINE__);
16767 	}
16768 	/* Are we pacing and late? */
16769 	if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
16770 	    TSTMP_GEQ(cts, rack->r_ctl.rc_last_output_to)) {
16771 		/* We are delayed */
16772 		delayed = cts - rack->r_ctl.rc_last_output_to;
16773 	} else {
16774 		delayed = 0;
16775 	}
16776 	/* Do the timers, which may override the pacer */
16777 	if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
16778 		int retval;
16779 
16780 		retval = rack_process_timers(tp, rack, cts, hpts_calling,
16781 		    &doing_tlp);
16782 		if (retval != 0) {
16783 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_ATIMER], 1);
16784 #ifdef TCP_ACCOUNTING
16785 			sched_unpin();
16786 #endif
16787 			/*
16788 			 * If timers want tcp_drop(), then pass error out,
16789 			 * otherwise suppress it.
16790 			 */
16791 			return (retval < 0 ? retval : 0);
16792 		}
16793 	}
16794 	if (rack->rc_in_persist) {
16795 		if (tcp_in_hpts(rack->rc_inp) == 0) {
16796 			/* Timer is not running */
16797 			rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
16798 		}
16799 #ifdef TCP_ACCOUNTING
16800 		sched_unpin();
16801 #endif
16802 		return (0);
16803 	}
16804 	if ((rack->r_timer_override) ||
16805 	    (rack->rc_ack_can_sendout_data) ||
16806 	    (delayed) ||
16807 	    (tp->t_state < TCPS_ESTABLISHED)) {
16808 		rack->rc_ack_can_sendout_data = 0;
16809 		if (tcp_in_hpts(rack->rc_inp))
16810 			tcp_hpts_remove(rack->rc_inp);
16811 	} else if (tcp_in_hpts(rack->rc_inp)) {
16812 		/*
16813 		 * On the hpts you can't pass even if ACKNOW is on, we will
16814 		 * when the hpts fires.
16815 		 */
16816 #ifdef TCP_ACCOUNTING
16817 		crtsc = get_cyclecount();
16818 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16819 			tp->tcp_proc_time[SND_BLOCKED] += (crtsc - ts_val);
16820 		}
16821 		counter_u64_add(tcp_proc_time[SND_BLOCKED], (crtsc - ts_val));
16822 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16823 			tp->tcp_cnt_counters[SND_BLOCKED]++;
16824 		}
16825 		counter_u64_add(tcp_cnt_counters[SND_BLOCKED], 1);
16826 		sched_unpin();
16827 #endif
16828 		counter_u64_add(rack_out_size[TCP_MSS_ACCT_INPACE], 1);
16829 		return (0);
16830 	}
16831 	rack->rc_inp->inp_hpts_calls = 0;
16832 	/* Finish out both pacing early and late accounting */
16833 	if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
16834 	    TSTMP_GT(rack->r_ctl.rc_last_output_to, cts)) {
16835 		early = rack->r_ctl.rc_last_output_to - cts;
16836 	} else
16837 		early = 0;
16838 	if (delayed) {
16839 		rack->r_ctl.rc_agg_delayed += delayed;
16840 		rack->r_late = 1;
16841 	} else if (early) {
16842 		rack->r_ctl.rc_agg_early += early;
16843 		rack->r_early = 1;
16844 	}
16845 	/* Now that early/late accounting is done turn off the flag */
16846 	rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
16847 	rack->r_wanted_output = 0;
16848 	rack->r_timer_override = 0;
16849 	if ((tp->t_state != rack->r_state) &&
16850 	    TCPS_HAVEESTABLISHED(tp->t_state)) {
16851 		rack_set_state(tp, rack);
16852 	}
16853 	if ((rack->r_fast_output) &&
16854 	    (doing_tlp == 0) &&
16855 	    (tp->rcv_numsacks == 0)) {
16856 		int ret;
16857 
16858 		error = 0;
16859 		ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error);
16860 		if (ret >= 0)
16861 			return(ret);
16862 		else if (error) {
16863 			inp = rack->rc_inp;
16864 			so = inp->inp_socket;
16865 			sb = &so->so_snd;
16866 			goto nomore;
16867 		}
16868 	}
16869 	inp = rack->rc_inp;
16870 	/*
16871 	 * For TFO connections in SYN_SENT or SYN_RECEIVED,
16872 	 * only allow the initial SYN or SYN|ACK and those sent
16873 	 * by the retransmit timer.
16874 	 */
16875 	if (IS_FASTOPEN(tp->t_flags) &&
16876 	    ((tp->t_state == TCPS_SYN_RECEIVED) ||
16877 	     (tp->t_state == TCPS_SYN_SENT)) &&
16878 	    SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */
16879 	    (tp->t_rxtshift == 0)) {              /* not a retransmit */
16880 		cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
16881 		so = inp->inp_socket;
16882 		sb = &so->so_snd;
16883 		goto just_return_nolock;
16884 	}
16885 	/*
16886 	 * Determine length of data that should be transmitted, and flags
16887 	 * that will be used. If there is some data or critical controls
16888 	 * (SYN, RST) to send, then transmit; otherwise, investigate
16889 	 * further.
16890 	 */
16891 	idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
16892 	if (tp->t_idle_reduce) {
16893 		if (idle && (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur))
16894 			rack_cc_after_idle(rack, tp);
16895 	}
16896 	tp->t_flags &= ~TF_LASTIDLE;
16897 	if (idle) {
16898 		if (tp->t_flags & TF_MORETOCOME) {
16899 			tp->t_flags |= TF_LASTIDLE;
16900 			idle = 0;
16901 		}
16902 	}
16903 	if ((tp->snd_una == tp->snd_max) &&
16904 	    rack->r_ctl.rc_went_idle_time &&
16905 	    TSTMP_GT(cts, rack->r_ctl.rc_went_idle_time)) {
16906 		idle = cts - rack->r_ctl.rc_went_idle_time;
16907 		if (idle > rack_min_probertt_hold) {
16908 			/* Count as a probe rtt */
16909 			if (rack->in_probe_rtt == 0) {
16910 				rack->r_ctl.rc_lower_rtt_us_cts = cts;
16911 				rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts;
16912 				rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts;
16913 				rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts;
16914 			} else {
16915 				rack_exit_probertt(rack, cts);
16916 			}
16917 		}
16918 		idle = 0;
16919 	}
16920 	if (rack_use_fsb && (rack->r_fsb_inited == 0) && (rack->r_state != TCPS_CLOSED))
16921 		rack_init_fsb_block(tp, rack);
16922 again:
16923 	/*
16924 	 * If we've recently taken a timeout, snd_max will be greater than
16925 	 * snd_nxt.  There may be SACK information that allows us to avoid
16926 	 * resending already delivered data.  Adjust snd_nxt accordingly.
16927 	 */
16928 	sendalot = 0;
16929 	cts = tcp_get_usecs(&tv);
16930 	ms_cts = tcp_tv_to_mssectick(&tv);
16931 	tso = 0;
16932 	mtu = 0;
16933 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
16934 	minseg = segsiz;
16935 	if (rack->r_ctl.rc_pace_max_segs == 0)
16936 		pace_max_seg = rack->rc_user_set_max_segs * segsiz;
16937 	else
16938 		pace_max_seg = rack->r_ctl.rc_pace_max_segs;
16939 	sb_offset = tp->snd_max - tp->snd_una;
16940 	cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
16941 	flags = tcp_outflags[tp->t_state];
16942 	while (rack->rc_free_cnt < rack_free_cache) {
16943 		rsm = rack_alloc(rack);
16944 		if (rsm == NULL) {
16945 			if (inp->inp_hpts_calls)
16946 				/* Retry in a ms */
16947 				slot = (1 * HPTS_USEC_IN_MSEC);
16948 			so = inp->inp_socket;
16949 			sb = &so->so_snd;
16950 			goto just_return_nolock;
16951 		}
16952 		TAILQ_INSERT_TAIL(&rack->r_ctl.rc_free, rsm, r_tnext);
16953 		rack->rc_free_cnt++;
16954 		rsm = NULL;
16955 	}
16956 	if (inp->inp_hpts_calls)
16957 		inp->inp_hpts_calls = 0;
16958 	sack_rxmit = 0;
16959 	len = 0;
16960 	rsm = NULL;
16961 	if (flags & TH_RST) {
16962 		SOCKBUF_LOCK(&inp->inp_socket->so_snd);
16963 		so = inp->inp_socket;
16964 		sb = &so->so_snd;
16965 		goto send;
16966 	}
16967 	if (rack->r_ctl.rc_resend) {
16968 		/* Retransmit timer */
16969 		rsm = rack->r_ctl.rc_resend;
16970 		rack->r_ctl.rc_resend = NULL;
16971 		len = rsm->r_end - rsm->r_start;
16972 		sack_rxmit = 1;
16973 		sendalot = 0;
16974 		KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
16975 			("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
16976 			 __func__, __LINE__,
16977 			 rsm->r_start, tp->snd_una, tp, rack, rsm));
16978 		sb_offset = rsm->r_start - tp->snd_una;
16979 		if (len >= segsiz)
16980 			len = segsiz;
16981 	} else if (rack->r_collapse_point_valid &&
16982 		   ((rsm = rack_check_collapsed(rack, cts)) != NULL)) {
16983 		/*
16984 		 * If an RSM is returned then enough time has passed
16985 		 * for us to retransmit it. Move up the collapse point,
16986 		 * since this rsm has its chance to retransmit now.
16987 		 */
16988 		rack_trace_point(rack, RACK_TP_COLLAPSED_RXT);
16989 		rack->r_ctl.last_collapse_point = rsm->r_end;
16990 		/* Are we done? */
16991 		if (SEQ_GEQ(rack->r_ctl.last_collapse_point,
16992 			    rack->r_ctl.high_collapse_point))
16993 			rack->r_collapse_point_valid = 0;
16994 		sack_rxmit = 1;
16995 		/* We are not doing a TLP */
16996 		doing_tlp = 0;
16997 		len = rsm->r_end - rsm->r_start;
16998 		sb_offset = rsm->r_start - tp->snd_una;
16999 		sendalot = 0;
17000 		if ((rack->full_size_rxt == 0) &&
17001 		    (rack->shape_rxt_to_pacing_min == 0) &&
17002 		    (len >= segsiz))
17003 			len = segsiz;
17004 	} else if ((rsm = tcp_rack_output(tp, rack, cts)) != NULL) {
17005 		/* We have a retransmit that takes precedence */
17006 		if ((!IN_FASTRECOVERY(tp->t_flags)) &&
17007 		    ((rsm->r_flags & RACK_MUST_RXT) == 0) &&
17008 		    ((tp->t_flags & TF_WASFRECOVERY) == 0)) {
17009 			/* Enter recovery if not induced by a time-out */
17010 			rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__);
17011 		}
17012 #ifdef INVARIANTS
17013 		if (SEQ_LT(rsm->r_start, tp->snd_una)) {
17014 			panic("Huh, tp:%p rack:%p rsm:%p start:%u < snd_una:%u\n",
17015 			      tp, rack, rsm, rsm->r_start, tp->snd_una);
17016 		}
17017 #endif
17018 		len = rsm->r_end - rsm->r_start;
17019 		KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
17020 			("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
17021 			 __func__, __LINE__,
17022 			 rsm->r_start, tp->snd_una, tp, rack, rsm));
17023 		sb_offset = rsm->r_start - tp->snd_una;
17024 		sendalot = 0;
17025 		if (len >= segsiz)
17026 			len = segsiz;
17027 		if (len > 0) {
17028 			sack_rxmit = 1;
17029 			KMOD_TCPSTAT_INC(tcps_sack_rexmits);
17030 			KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes,
17031 			    min(len, segsiz));
17032 		}
17033 	} else if (rack->r_ctl.rc_tlpsend) {
17034 		/* Tail loss probe */
17035 		long cwin;
17036 		long tlen;
17037 
17038 		/*
17039 		 * Check if we can do a TLP with a RACK'd packet
17040 		 * this can happen if we are not doing the rack
17041 		 * cheat and we skipped to a TLP and it
17042 		 * went off.
17043 		 */
17044 		rsm = rack->r_ctl.rc_tlpsend;
17045 		/* We are doing a TLP make sure the flag is preent */
17046 		rsm->r_flags |= RACK_TLP;
17047 		rack->r_ctl.rc_tlpsend = NULL;
17048 		sack_rxmit = 1;
17049 		tlen = rsm->r_end - rsm->r_start;
17050 		if (tlen > segsiz)
17051 			tlen = segsiz;
17052 		KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
17053 			("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
17054 			 __func__, __LINE__,
17055 			 rsm->r_start, tp->snd_una, tp, rack, rsm));
17056 		sb_offset = rsm->r_start - tp->snd_una;
17057 		cwin = min(tp->snd_wnd, tlen);
17058 		len = cwin;
17059 	}
17060 	if (rack->r_must_retran &&
17061 	    (doing_tlp == 0) &&
17062 	    (SEQ_GT(tp->snd_max, tp->snd_una)) &&
17063 	    (rsm == NULL)) {
17064 		/*
17065 		 * There are two different ways that we
17066 		 * can get into this block:
17067 		 * a) This is a non-sack connection, we had a time-out
17068 		 *    and thus r_must_retran was set and everything
17069 		 *    left outstanding as been marked for retransmit.
17070 		 * b) The MTU of the path shrank, so that everything
17071 		 *    was marked to be retransmitted with the smaller
17072 		 *    mtu and r_must_retran was set.
17073 		 *
17074 		 * This means that we expect the sendmap (outstanding)
17075 		 * to all be marked must. We can use the tmap to
17076 		 * look at them.
17077 		 *
17078 		 */
17079 		int sendwin, flight;
17080 
17081 		sendwin = min(tp->snd_wnd, tp->snd_cwnd);
17082 		flight = ctf_flight_size(tp, rack->r_ctl.rc_out_at_rto);
17083 		if (flight >= sendwin) {
17084 			/*
17085 			 * We can't send yet.
17086 			 */
17087 			so = inp->inp_socket;
17088 			sb = &so->so_snd;
17089 			goto just_return_nolock;
17090 		}
17091 		/*
17092 		 * This is the case a/b mentioned above. All
17093 		 * outstanding/not-acked should be marked.
17094 		 * We can use the tmap to find them.
17095 		 */
17096 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
17097 		if (rsm == NULL) {
17098 			/* TSNH */
17099 			rack->r_must_retran = 0;
17100 			rack->r_ctl.rc_out_at_rto = 0;
17101 			so = inp->inp_socket;
17102 			sb = &so->so_snd;
17103 			goto just_return_nolock;
17104 		}
17105 		if ((rsm->r_flags & RACK_MUST_RXT) == 0) {
17106 			/*
17107 			 * The first one does not have the flag, did we collapse
17108 			 * further up in our list?
17109 			 */
17110 			rack->r_must_retran = 0;
17111 			rack->r_ctl.rc_out_at_rto = 0;
17112 			rsm = NULL;
17113 			sack_rxmit = 0;
17114 		} else {
17115 			sack_rxmit = 1;
17116 			len = rsm->r_end - rsm->r_start;
17117 			sb_offset = rsm->r_start - tp->snd_una;
17118 			sendalot = 0;
17119 			if ((rack->full_size_rxt == 0) &&
17120 			    (rack->shape_rxt_to_pacing_min == 0) &&
17121 			    (len >= segsiz))
17122 				len = segsiz;
17123 			/*
17124 			 * Delay removing the flag RACK_MUST_RXT so
17125 			 * that the fastpath for retransmit will
17126 			 * work with this rsm.
17127 			 */
17128 		}
17129 	}
17130 	/*
17131 	 * Enforce a connection sendmap count limit if set
17132 	 * as long as we are not retransmiting.
17133 	 */
17134 	if ((rsm == NULL) &&
17135 	    (rack->do_detection == 0) &&
17136 	    (V_tcp_map_entries_limit > 0) &&
17137 	    (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
17138 		counter_u64_add(rack_to_alloc_limited, 1);
17139 		if (!rack->alloc_limit_reported) {
17140 			rack->alloc_limit_reported = 1;
17141 			counter_u64_add(rack_alloc_limited_conns, 1);
17142 		}
17143 		so = inp->inp_socket;
17144 		sb = &so->so_snd;
17145 		goto just_return_nolock;
17146 	}
17147 	if (rsm && (rsm->r_flags & RACK_HAS_FIN)) {
17148 		/* we are retransmitting the fin */
17149 		len--;
17150 		if (len) {
17151 			/*
17152 			 * When retransmitting data do *not* include the
17153 			 * FIN. This could happen from a TLP probe.
17154 			 */
17155 			flags &= ~TH_FIN;
17156 		}
17157 	}
17158 	if (rsm && rack->r_fsb_inited && rack_use_rsm_rfo &&
17159 	    ((rsm->r_flags & RACK_HAS_FIN) == 0)) {
17160 		int ret;
17161 
17162 		ret = rack_fast_rsm_output(tp, rack, rsm, ts_val, cts, ms_cts, &tv, len, doing_tlp);
17163 		if (ret == 0)
17164 			return (0);
17165 	}
17166 	so = inp->inp_socket;
17167 	sb = &so->so_snd;
17168 	if (do_a_prefetch == 0) {
17169 		kern_prefetch(sb, &do_a_prefetch);
17170 		do_a_prefetch = 1;
17171 	}
17172 #ifdef NETFLIX_SHARED_CWND
17173 	if ((tp->t_flags2 & TF2_TCP_SCWND_ALLOWED) &&
17174 	    rack->rack_enable_scwnd) {
17175 		/* We are doing cwnd sharing */
17176 		if (rack->gp_ready &&
17177 		    (rack->rack_attempted_scwnd == 0) &&
17178 		    (rack->r_ctl.rc_scw == NULL) &&
17179 		    tp->t_lib) {
17180 			/* The pcbid is in, lets make an attempt */
17181 			counter_u64_add(rack_try_scwnd, 1);
17182 			rack->rack_attempted_scwnd = 1;
17183 			rack->r_ctl.rc_scw = tcp_shared_cwnd_alloc(tp,
17184 								   &rack->r_ctl.rc_scw_index,
17185 								   segsiz);
17186 		}
17187 		if (rack->r_ctl.rc_scw &&
17188 		    (rack->rack_scwnd_is_idle == 1) &&
17189 		    sbavail(&so->so_snd)) {
17190 			/* we are no longer out of data */
17191 			tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
17192 			rack->rack_scwnd_is_idle = 0;
17193 		}
17194 		if (rack->r_ctl.rc_scw) {
17195 			/* First lets update and get the cwnd */
17196 			rack->r_ctl.cwnd_to_use = cwnd_to_use = tcp_shared_cwnd_update(rack->r_ctl.rc_scw,
17197 								    rack->r_ctl.rc_scw_index,
17198 								    tp->snd_cwnd, tp->snd_wnd, segsiz);
17199 		}
17200 	}
17201 #endif
17202 	/*
17203 	 * Get standard flags, and add SYN or FIN if requested by 'hidden'
17204 	 * state flags.
17205 	 */
17206 	if (tp->t_flags & TF_NEEDFIN)
17207 		flags |= TH_FIN;
17208 	if (tp->t_flags & TF_NEEDSYN)
17209 		flags |= TH_SYN;
17210 	if ((sack_rxmit == 0) && (prefetch_rsm == 0)) {
17211 		void *end_rsm;
17212 		end_rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
17213 		if (end_rsm)
17214 			kern_prefetch(end_rsm, &prefetch_rsm);
17215 		prefetch_rsm = 1;
17216 	}
17217 	SOCKBUF_LOCK(sb);
17218 	/*
17219 	 * If snd_nxt == snd_max and we have transmitted a FIN, the
17220 	 * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a
17221 	 * negative length.  This can also occur when TCP opens up its
17222 	 * congestion window while receiving additional duplicate acks after
17223 	 * fast-retransmit because TCP will reset snd_nxt to snd_max after
17224 	 * the fast-retransmit.
17225 	 *
17226 	 * In the normal retransmit-FIN-only case, however, snd_nxt will be
17227 	 * set to snd_una, the sb_offset will be 0, and the length may wind
17228 	 * up 0.
17229 	 *
17230 	 * If sack_rxmit is true we are retransmitting from the scoreboard
17231 	 * in which case len is already set.
17232 	 */
17233 	if ((sack_rxmit == 0) &&
17234 	    (TCPS_HAVEESTABLISHED(tp->t_state) || IS_FASTOPEN(tp->t_flags))) {
17235 		uint32_t avail;
17236 
17237 		avail = sbavail(sb);
17238 		if (SEQ_GT(tp->snd_nxt, tp->snd_una) && avail)
17239 			sb_offset = tp->snd_nxt - tp->snd_una;
17240 		else
17241 			sb_offset = 0;
17242 		if ((IN_FASTRECOVERY(tp->t_flags) == 0) || rack->rack_no_prr) {
17243 			if (rack->r_ctl.rc_tlp_new_data) {
17244 				/* TLP is forcing out new data */
17245 				if (rack->r_ctl.rc_tlp_new_data > (uint32_t) (avail - sb_offset)) {
17246 					rack->r_ctl.rc_tlp_new_data = (uint32_t) (avail - sb_offset);
17247 				}
17248 				if ((rack->r_ctl.rc_tlp_new_data + sb_offset) > tp->snd_wnd) {
17249 					if (tp->snd_wnd > sb_offset)
17250 						len = tp->snd_wnd - sb_offset;
17251 					else
17252 						len = 0;
17253 				} else {
17254 					len = rack->r_ctl.rc_tlp_new_data;
17255 				}
17256 				rack->r_ctl.rc_tlp_new_data = 0;
17257 			}  else {
17258 				len = rack_what_can_we_send(tp, rack, cwnd_to_use, avail, sb_offset);
17259 			}
17260 			if ((rack->r_ctl.crte == NULL) && IN_FASTRECOVERY(tp->t_flags) && (len > segsiz)) {
17261 				/*
17262 				 * For prr=off, we need to send only 1 MSS
17263 				 * at a time. We do this because another sack could
17264 				 * be arriving that causes us to send retransmits and
17265 				 * we don't want to be on a long pace due to a larger send
17266 				 * that keeps us from sending out the retransmit.
17267 				 */
17268 				len = segsiz;
17269 			}
17270 		} else {
17271 			uint32_t outstanding;
17272 			/*
17273 			 * We are inside of a Fast recovery episode, this
17274 			 * is caused by a SACK or 3 dup acks. At this point
17275 			 * we have sent all the retransmissions and we rely
17276 			 * on PRR to dictate what we will send in the form of
17277 			 * new data.
17278 			 */
17279 
17280 			outstanding = tp->snd_max - tp->snd_una;
17281 			if ((rack->r_ctl.rc_prr_sndcnt + outstanding) > tp->snd_wnd) {
17282 				if (tp->snd_wnd > outstanding) {
17283 					len = tp->snd_wnd - outstanding;
17284 					/* Check to see if we have the data */
17285 					if ((sb_offset + len) > avail) {
17286 						/* It does not all fit */
17287 						if (avail > sb_offset)
17288 							len = avail - sb_offset;
17289 						else
17290 							len = 0;
17291 					}
17292 				} else {
17293 					len = 0;
17294 				}
17295 			} else if (avail > sb_offset) {
17296 				len = avail - sb_offset;
17297 			} else {
17298 				len = 0;
17299 			}
17300 			if (len > 0) {
17301 				if (len > rack->r_ctl.rc_prr_sndcnt) {
17302 					len = rack->r_ctl.rc_prr_sndcnt;
17303 				}
17304 				if (len > 0) {
17305 					sub_from_prr = 1;
17306 				}
17307 			}
17308 			if (len > segsiz) {
17309 				/*
17310 				 * We should never send more than a MSS when
17311 				 * retransmitting or sending new data in prr
17312 				 * mode unless the override flag is on. Most
17313 				 * likely the PRR algorithm is not going to
17314 				 * let us send a lot as well :-)
17315 				 */
17316 				if (rack->r_ctl.rc_prr_sendalot == 0) {
17317 					len = segsiz;
17318 				}
17319 			} else if (len < segsiz) {
17320 				/*
17321 				 * Do we send any? The idea here is if the
17322 				 * send empty's the socket buffer we want to
17323 				 * do it. However if not then lets just wait
17324 				 * for our prr_sndcnt to get bigger.
17325 				 */
17326 				long leftinsb;
17327 
17328 				leftinsb = sbavail(sb) - sb_offset;
17329 				if (leftinsb > len) {
17330 					/* This send does not empty the sb */
17331 					len = 0;
17332 				}
17333 			}
17334 		}
17335 	} else if (!TCPS_HAVEESTABLISHED(tp->t_state)) {
17336 		/*
17337 		 * If you have not established
17338 		 * and are not doing FAST OPEN
17339 		 * no data please.
17340 		 */
17341 		if ((sack_rxmit == 0) &&
17342 		    (!IS_FASTOPEN(tp->t_flags))){
17343 			len = 0;
17344 			sb_offset = 0;
17345 		}
17346 	}
17347 	if (prefetch_so_done == 0) {
17348 		kern_prefetch(so, &prefetch_so_done);
17349 		prefetch_so_done = 1;
17350 	}
17351 	/*
17352 	 * Lop off SYN bit if it has already been sent.  However, if this is
17353 	 * SYN-SENT state and if segment contains data and if we don't know
17354 	 * that foreign host supports TAO, suppress sending segment.
17355 	 */
17356 	if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una) &&
17357 	    ((sack_rxmit == 0) && (tp->t_rxtshift == 0))) {
17358 		/*
17359 		 * When sending additional segments following a TFO SYN|ACK,
17360 		 * do not include the SYN bit.
17361 		 */
17362 		if (IS_FASTOPEN(tp->t_flags) &&
17363 		    (tp->t_state == TCPS_SYN_RECEIVED))
17364 			flags &= ~TH_SYN;
17365 	}
17366 	/*
17367 	 * Be careful not to send data and/or FIN on SYN segments. This
17368 	 * measure is needed to prevent interoperability problems with not
17369 	 * fully conformant TCP implementations.
17370 	 */
17371 	if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
17372 		len = 0;
17373 		flags &= ~TH_FIN;
17374 	}
17375 	/*
17376 	 * On TFO sockets, ensure no data is sent in the following cases:
17377 	 *
17378 	 *  - When retransmitting SYN|ACK on a passively-created socket
17379 	 *
17380 	 *  - When retransmitting SYN on an actively created socket
17381 	 *
17382 	 *  - When sending a zero-length cookie (cookie request) on an
17383 	 *    actively created socket
17384 	 *
17385 	 *  - When the socket is in the CLOSED state (RST is being sent)
17386 	 */
17387 	if (IS_FASTOPEN(tp->t_flags) &&
17388 	    (((flags & TH_SYN) && (tp->t_rxtshift > 0)) ||
17389 	     ((tp->t_state == TCPS_SYN_SENT) &&
17390 	      (tp->t_tfo_client_cookie_len == 0)) ||
17391 	     (flags & TH_RST))) {
17392 		sack_rxmit = 0;
17393 		len = 0;
17394 	}
17395 	/* Without fast-open there should never be data sent on a SYN */
17396 	if ((flags & TH_SYN) && (!IS_FASTOPEN(tp->t_flags))) {
17397 		tp->snd_nxt = tp->iss;
17398 		len = 0;
17399 	}
17400 	if ((len > segsiz) && (tcp_dsack_block_exists(tp))) {
17401 		/* We only send 1 MSS if we have a DSACK block */
17402 		add_flag |= RACK_SENT_W_DSACK;
17403 		len = segsiz;
17404 	}
17405 	orig_len = len;
17406 	if (len <= 0) {
17407 		/*
17408 		 * If FIN has been sent but not acked, but we haven't been
17409 		 * called to retransmit, len will be < 0.  Otherwise, window
17410 		 * shrank after we sent into it.  If window shrank to 0,
17411 		 * cancel pending retransmit, pull snd_nxt back to (closed)
17412 		 * window, and set the persist timer if it isn't already
17413 		 * going.  If the window didn't close completely, just wait
17414 		 * for an ACK.
17415 		 *
17416 		 * We also do a general check here to ensure that we will
17417 		 * set the persist timer when we have data to send, but a
17418 		 * 0-byte window. This makes sure the persist timer is set
17419 		 * even if the packet hits one of the "goto send" lines
17420 		 * below.
17421 		 */
17422 		len = 0;
17423 		if ((tp->snd_wnd == 0) &&
17424 		    (TCPS_HAVEESTABLISHED(tp->t_state)) &&
17425 		    (tp->snd_una == tp->snd_max) &&
17426 		    (sb_offset < (int)sbavail(sb))) {
17427 			rack_enter_persist(tp, rack, cts);
17428 		}
17429 	} else if ((rsm == NULL) &&
17430 		   (doing_tlp == 0) &&
17431 		   (len < pace_max_seg)) {
17432 		/*
17433 		 * We are not sending a maximum sized segment for
17434 		 * some reason. Should we not send anything (think
17435 		 * sws or persists)?
17436 		 */
17437 		if ((tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg)) &&
17438 		    (TCPS_HAVEESTABLISHED(tp->t_state)) &&
17439 		    (len < minseg) &&
17440 		    (len < (int)(sbavail(sb) - sb_offset))) {
17441 			/*
17442 			 * Here the rwnd is less than
17443 			 * the minimum pacing size, this is not a retransmit,
17444 			 * we are established and
17445 			 * the send is not the last in the socket buffer
17446 			 * we send nothing, and we may enter persists
17447 			 * if nothing is outstanding.
17448 			 */
17449 			len = 0;
17450 			if (tp->snd_max == tp->snd_una) {
17451 				/*
17452 				 * Nothing out we can
17453 				 * go into persists.
17454 				 */
17455 				rack_enter_persist(tp, rack, cts);
17456 			}
17457 		     } else if ((cwnd_to_use >= max(minseg, (segsiz * 4))) &&
17458 			   (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) &&
17459 			   (len < (int)(sbavail(sb) - sb_offset)) &&
17460 			   (len < minseg)) {
17461 			/*
17462 			 * Here we are not retransmitting, and
17463 			 * the cwnd is not so small that we could
17464 			 * not send at least a min size (rxt timer
17465 			 * not having gone off), We have 2 segments or
17466 			 * more already in flight, its not the tail end
17467 			 * of the socket buffer  and the cwnd is blocking
17468 			 * us from sending out a minimum pacing segment size.
17469 			 * Lets not send anything.
17470 			 */
17471 			len = 0;
17472 		} else if (((tp->snd_wnd - ctf_outstanding(tp)) <
17473 			    min((rack->r_ctl.rc_high_rwnd/2), minseg)) &&
17474 			   (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) &&
17475 			   (len < (int)(sbavail(sb) - sb_offset)) &&
17476 			   (TCPS_HAVEESTABLISHED(tp->t_state))) {
17477 			/*
17478 			 * Here we have a send window but we have
17479 			 * filled it up and we can't send another pacing segment.
17480 			 * We also have in flight more than 2 segments
17481 			 * and we are not completing the sb i.e. we allow
17482 			 * the last bytes of the sb to go out even if
17483 			 * its not a full pacing segment.
17484 			 */
17485 			len = 0;
17486 		} else if ((rack->r_ctl.crte != NULL) &&
17487 			   (tp->snd_wnd >= (pace_max_seg * max(1, rack_hw_rwnd_factor))) &&
17488 			   (cwnd_to_use >= (pace_max_seg + (4 * segsiz))) &&
17489 			   (ctf_flight_size(tp, rack->r_ctl.rc_sacked) >= (2 * segsiz)) &&
17490 			   (len < (int)(sbavail(sb) - sb_offset))) {
17491 			/*
17492 			 * Here we are doing hardware pacing, this is not a TLP,
17493 			 * we are not sending a pace max segment size, there is rwnd
17494 			 * room to send at least N pace_max_seg, the cwnd is greater
17495 			 * than or equal to a full pacing segments plus 4 mss and we have 2 or
17496 			 * more segments in flight and its not the tail of the socket buffer.
17497 			 *
17498 			 * We don't want to send instead we need to get more ack's in to
17499 			 * allow us to send a full pacing segment. Normally, if we are pacing
17500 			 * about the right speed, we should have finished our pacing
17501 			 * send as most of the acks have come back if we are at the
17502 			 * right rate. This is a bit fuzzy since return path delay
17503 			 * can delay the acks, which is why we want to make sure we
17504 			 * have cwnd space to have a bit more than a max pace segments in flight.
17505 			 *
17506 			 * If we have not gotten our acks back we are pacing at too high a
17507 			 * rate delaying will not hurt and will bring our GP estimate down by
17508 			 * injecting the delay. If we don't do this we will send
17509 			 * 2 MSS out in response to the acks being clocked in which
17510 			 * defeats the point of hw-pacing (i.e. to help us get
17511 			 * larger TSO's out).
17512 			 */
17513 			len = 0;
17514 
17515 		}
17516 
17517 	}
17518 	/* len will be >= 0 after this point. */
17519 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
17520 	rack_sndbuf_autoscale(rack);
17521 	/*
17522 	 * Decide if we can use TCP Segmentation Offloading (if supported by
17523 	 * hardware).
17524 	 *
17525 	 * TSO may only be used if we are in a pure bulk sending state.  The
17526 	 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP
17527 	 * options prevent using TSO.  With TSO the TCP header is the same
17528 	 * (except for the sequence number) for all generated packets.  This
17529 	 * makes it impossible to transmit any options which vary per
17530 	 * generated segment or packet.
17531 	 *
17532 	 * IPv4 handling has a clear separation of ip options and ip header
17533 	 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does
17534 	 * the right thing below to provide length of just ip options and thus
17535 	 * checking for ipoptlen is enough to decide if ip options are present.
17536 	 */
17537 	ipoptlen = 0;
17538 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
17539 	/*
17540 	 * Pre-calculate here as we save another lookup into the darknesses
17541 	 * of IPsec that way and can actually decide if TSO is ok.
17542 	 */
17543 #ifdef INET6
17544 	if (isipv6 && IPSEC_ENABLED(ipv6))
17545 		ipsec_optlen = IPSEC_HDRSIZE(ipv6, tp->t_inpcb);
17546 #ifdef INET
17547 	else
17548 #endif
17549 #endif				/* INET6 */
17550 #ifdef INET
17551 		if (IPSEC_ENABLED(ipv4))
17552 			ipsec_optlen = IPSEC_HDRSIZE(ipv4, tp->t_inpcb);
17553 #endif				/* INET */
17554 #endif
17555 
17556 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
17557 	ipoptlen += ipsec_optlen;
17558 #endif
17559 	if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > segsiz &&
17560 	    (tp->t_port == 0) &&
17561 	    ((tp->t_flags & TF_SIGNATURE) == 0) &&
17562 	    tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
17563 	    ipoptlen == 0)
17564 		tso = 1;
17565 	{
17566 		uint32_t outstanding __unused;
17567 
17568 		outstanding = tp->snd_max - tp->snd_una;
17569 		if (tp->t_flags & TF_SENTFIN) {
17570 			/*
17571 			 * If we sent a fin, snd_max is 1 higher than
17572 			 * snd_una
17573 			 */
17574 			outstanding--;
17575 		}
17576 		if (sack_rxmit) {
17577 			if ((rsm->r_flags & RACK_HAS_FIN) == 0)
17578 				flags &= ~TH_FIN;
17579 		} else {
17580 			if (SEQ_LT(tp->snd_nxt + len, tp->snd_una +
17581 				   sbused(sb)))
17582 				flags &= ~TH_FIN;
17583 		}
17584 	}
17585 	recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
17586 	    (long)TCP_MAXWIN << tp->rcv_scale);
17587 
17588 	/*
17589 	 * Sender silly window avoidance.   We transmit under the following
17590 	 * conditions when len is non-zero:
17591 	 *
17592 	 * - We have a full segment (or more with TSO) - This is the last
17593 	 * buffer in a write()/send() and we are either idle or running
17594 	 * NODELAY - we've timed out (e.g. persist timer) - we have more
17595 	 * then 1/2 the maximum send window's worth of data (receiver may be
17596 	 * limited the window size) - we need to retransmit
17597 	 */
17598 	if (len) {
17599 		if (len >= segsiz) {
17600 			goto send;
17601 		}
17602 		/*
17603 		 * NOTE! on localhost connections an 'ack' from the remote
17604 		 * end may occur synchronously with the output and cause us
17605 		 * to flush a buffer queued with moretocome.  XXX
17606 		 *
17607 		 */
17608 		if (!(tp->t_flags & TF_MORETOCOME) &&	/* normal case */
17609 		    (idle || (tp->t_flags & TF_NODELAY)) &&
17610 		    ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) &&
17611 		    (tp->t_flags & TF_NOPUSH) == 0) {
17612 			pass = 2;
17613 			goto send;
17614 		}
17615 		if ((tp->snd_una == tp->snd_max) && len) {	/* Nothing outstanding */
17616 			pass = 22;
17617 			goto send;
17618 		}
17619 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) {
17620 			pass = 4;
17621 			goto send;
17622 		}
17623 		if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {	/* retransmit case */
17624 			pass = 5;
17625 			goto send;
17626 		}
17627 		if (sack_rxmit) {
17628 			pass = 6;
17629 			goto send;
17630 		}
17631 		if (((tp->snd_wnd - ctf_outstanding(tp)) < segsiz) &&
17632 		    (ctf_outstanding(tp) < (segsiz * 2))) {
17633 			/*
17634 			 * We have less than two MSS outstanding (delayed ack)
17635 			 * and our rwnd will not let us send a full sized
17636 			 * MSS. Lets go ahead and let this small segment
17637 			 * out because we want to try to have at least two
17638 			 * packets inflight to not be caught by delayed ack.
17639 			 */
17640 			pass = 12;
17641 			goto send;
17642 		}
17643 	}
17644 	/*
17645 	 * Sending of standalone window updates.
17646 	 *
17647 	 * Window updates are important when we close our window due to a
17648 	 * full socket buffer and are opening it again after the application
17649 	 * reads data from it.  Once the window has opened again and the
17650 	 * remote end starts to send again the ACK clock takes over and
17651 	 * provides the most current window information.
17652 	 *
17653 	 * We must avoid the silly window syndrome whereas every read from
17654 	 * the receive buffer, no matter how small, causes a window update
17655 	 * to be sent.  We also should avoid sending a flurry of window
17656 	 * updates when the socket buffer had queued a lot of data and the
17657 	 * application is doing small reads.
17658 	 *
17659 	 * Prevent a flurry of pointless window updates by only sending an
17660 	 * update when we can increase the advertized window by more than
17661 	 * 1/4th of the socket buffer capacity.  When the buffer is getting
17662 	 * full or is very small be more aggressive and send an update
17663 	 * whenever we can increase by two mss sized segments. In all other
17664 	 * situations the ACK's to new incoming data will carry further
17665 	 * window increases.
17666 	 *
17667 	 * Don't send an independent window update if a delayed ACK is
17668 	 * pending (it will get piggy-backed on it) or the remote side
17669 	 * already has done a half-close and won't send more data.  Skip
17670 	 * this if the connection is in T/TCP half-open state.
17671 	 */
17672 	if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
17673 	    !(tp->t_flags & TF_DELACK) &&
17674 	    !TCPS_HAVERCVDFIN(tp->t_state)) {
17675 		/*
17676 		 * "adv" is the amount we could increase the window, taking
17677 		 * into account that we are limited by TCP_MAXWIN <<
17678 		 * tp->rcv_scale.
17679 		 */
17680 		int32_t adv;
17681 		int oldwin;
17682 
17683 		adv = recwin;
17684 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
17685 			oldwin = (tp->rcv_adv - tp->rcv_nxt);
17686 			if (adv > oldwin)
17687 			    adv -= oldwin;
17688 			else {
17689 				/* We can't increase the window */
17690 				adv = 0;
17691 			}
17692 		} else
17693 			oldwin = 0;
17694 
17695 		/*
17696 		 * If the new window size ends up being the same as or less
17697 		 * than the old size when it is scaled, then don't force
17698 		 * a window update.
17699 		 */
17700 		if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale)
17701 			goto dontupdate;
17702 
17703 		if (adv >= (int32_t)(2 * segsiz) &&
17704 		    (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) ||
17705 		     recwin <= (int32_t)(so->so_rcv.sb_hiwat / 8) ||
17706 		     so->so_rcv.sb_hiwat <= 8 * segsiz)) {
17707 			pass = 7;
17708 			goto send;
17709 		}
17710 		if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat) {
17711 			pass = 23;
17712 			goto send;
17713 		}
17714 	}
17715 dontupdate:
17716 
17717 	/*
17718 	 * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
17719 	 * is also a catch-all for the retransmit timer timeout case.
17720 	 */
17721 	if (tp->t_flags & TF_ACKNOW) {
17722 		pass = 8;
17723 		goto send;
17724 	}
17725 	if (((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) {
17726 		pass = 9;
17727 		goto send;
17728 	}
17729 	/*
17730 	 * If our state indicates that FIN should be sent and we have not
17731 	 * yet done so, then we need to send.
17732 	 */
17733 	if ((flags & TH_FIN) &&
17734 	    (tp->snd_nxt == tp->snd_una)) {
17735 		pass = 11;
17736 		goto send;
17737 	}
17738 	/*
17739 	 * No reason to send a segment, just return.
17740 	 */
17741 just_return:
17742 	SOCKBUF_UNLOCK(sb);
17743 just_return_nolock:
17744 	{
17745 		int app_limited = CTF_JR_SENT_DATA;
17746 
17747 		if (tot_len_this_send > 0) {
17748 			/* Make sure snd_nxt is up to max */
17749 			rack->r_ctl.fsb.recwin = recwin;
17750 			slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, NULL, segsiz);
17751 			if ((error == 0) &&
17752 			    rack_use_rfo &&
17753 			    ((flags & (TH_SYN|TH_FIN)) == 0) &&
17754 			    (ipoptlen == 0) &&
17755 			    (tp->snd_nxt == tp->snd_max) &&
17756 			    (tp->rcv_numsacks == 0) &&
17757 			    rack->r_fsb_inited &&
17758 			    TCPS_HAVEESTABLISHED(tp->t_state) &&
17759 			    (rack->r_must_retran == 0) &&
17760 			    ((tp->t_flags & TF_NEEDFIN) == 0) &&
17761 			    (len > 0) && (orig_len > 0) &&
17762 			    (orig_len > len) &&
17763 			    ((orig_len - len) >= segsiz) &&
17764 			    ((optlen == 0) ||
17765 			     ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
17766 				/* We can send at least one more MSS using our fsb */
17767 
17768 				rack->r_fast_output = 1;
17769 				rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
17770 				rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
17771 				rack->r_ctl.fsb.tcp_flags = flags;
17772 				rack->r_ctl.fsb.left_to_send = orig_len - len;
17773 				if (hw_tls)
17774 					rack->r_ctl.fsb.hw_tls = 1;
17775 				else
17776 					rack->r_ctl.fsb.hw_tls = 0;
17777 				KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(sb) - (tp->snd_max - tp->snd_una))),
17778 					("rack:%p left_to_send:%u sbavail:%u out:%u",
17779 					rack, rack->r_ctl.fsb.left_to_send, sbavail(sb),
17780 					 (tp->snd_max - tp->snd_una)));
17781 				if (rack->r_ctl.fsb.left_to_send < segsiz)
17782 					rack->r_fast_output = 0;
17783 				else {
17784 					if (rack->r_ctl.fsb.left_to_send == (sbavail(sb) - (tp->snd_max - tp->snd_una)))
17785 						rack->r_ctl.fsb.rfo_apply_push = 1;
17786 					else
17787 						rack->r_ctl.fsb.rfo_apply_push = 0;
17788 				}
17789 			} else
17790 				rack->r_fast_output = 0;
17791 
17792 
17793 			rack_log_fsb(rack, tp, so, flags,
17794 				     ipoptlen, orig_len, len, 0,
17795 				     1, optlen, __LINE__, 1);
17796 			if (SEQ_GT(tp->snd_max, tp->snd_nxt))
17797 				tp->snd_nxt = tp->snd_max;
17798 		} else {
17799 			int end_window = 0;
17800 			uint32_t seq = tp->gput_ack;
17801 
17802 			rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
17803 			if (rsm) {
17804 				/*
17805 				 * Mark the last sent that we just-returned (hinting
17806 				 * that delayed ack may play a role in any rtt measurement).
17807 				 */
17808 				rsm->r_just_ret = 1;
17809 			}
17810 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_JUSTRET], 1);
17811 			rack->r_ctl.rc_agg_delayed = 0;
17812 			rack->r_early = 0;
17813 			rack->r_late = 0;
17814 			rack->r_ctl.rc_agg_early = 0;
17815 			if ((ctf_outstanding(tp) +
17816 			     min(max(segsiz, (rack->r_ctl.rc_high_rwnd/2)),
17817 				 minseg)) >= tp->snd_wnd) {
17818 				/* We are limited by the rwnd */
17819 				app_limited = CTF_JR_RWND_LIMITED;
17820 				if (IN_FASTRECOVERY(tp->t_flags))
17821 				    rack->r_ctl.rc_prr_sndcnt = 0;
17822 			} else if (ctf_outstanding(tp) >= sbavail(sb)) {
17823 				/* We are limited by whats available -- app limited */
17824 				app_limited = CTF_JR_APP_LIMITED;
17825 				if (IN_FASTRECOVERY(tp->t_flags))
17826 				    rack->r_ctl.rc_prr_sndcnt = 0;
17827 			} else if ((idle == 0) &&
17828 				   ((tp->t_flags & TF_NODELAY) == 0) &&
17829 				   ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) &&
17830 				   (len < segsiz)) {
17831 				/*
17832 				 * No delay is not on and the
17833 				 * user is sending less than 1MSS. This
17834 				 * brings out SWS avoidance so we
17835 				 * don't send. Another app-limited case.
17836 				 */
17837 				app_limited = CTF_JR_APP_LIMITED;
17838 			} else if (tp->t_flags & TF_NOPUSH) {
17839 				/*
17840 				 * The user has requested no push of
17841 				 * the last segment and we are
17842 				 * at the last segment. Another app
17843 				 * limited case.
17844 				 */
17845 				app_limited = CTF_JR_APP_LIMITED;
17846 			} else if ((ctf_outstanding(tp) + minseg) > cwnd_to_use) {
17847 				/* Its the cwnd */
17848 				app_limited = CTF_JR_CWND_LIMITED;
17849 			} else if (IN_FASTRECOVERY(tp->t_flags) &&
17850 				   (rack->rack_no_prr == 0) &&
17851 				   (rack->r_ctl.rc_prr_sndcnt < segsiz)) {
17852 				app_limited = CTF_JR_PRR;
17853 			} else {
17854 				/* Now why here are we not sending? */
17855 #ifdef NOW
17856 #ifdef INVARIANTS
17857 				panic("rack:%p hit JR_ASSESSING case cwnd_to_use:%u?", rack, cwnd_to_use);
17858 #endif
17859 #endif
17860 				app_limited = CTF_JR_ASSESSING;
17861 			}
17862 			/*
17863 			 * App limited in some fashion, for our pacing GP
17864 			 * measurements we don't want any gap (even cwnd).
17865 			 * Close  down the measurement window.
17866 			 */
17867 			if (rack_cwnd_block_ends_measure &&
17868 			    ((app_limited == CTF_JR_CWND_LIMITED) ||
17869 			     (app_limited == CTF_JR_PRR))) {
17870 				/*
17871 				 * The reason we are not sending is
17872 				 * the cwnd (or prr). We have been configured
17873 				 * to end the measurement window in
17874 				 * this case.
17875 				 */
17876 				end_window = 1;
17877 			} else if (rack_rwnd_block_ends_measure &&
17878 				   (app_limited == CTF_JR_RWND_LIMITED)) {
17879 				/*
17880 				 * We are rwnd limited and have been
17881 				 * configured to end the measurement
17882 				 * window in this case.
17883 				 */
17884 				end_window = 1;
17885 			} else if (app_limited == CTF_JR_APP_LIMITED) {
17886 				/*
17887 				 * A true application limited period, we have
17888 				 * ran out of data.
17889 				 */
17890 				end_window = 1;
17891 			} else if (app_limited == CTF_JR_ASSESSING) {
17892 				/*
17893 				 * In the assessing case we hit the end of
17894 				 * the if/else and had no known reason
17895 				 * This will panic us under invariants..
17896 				 *
17897 				 * If we get this out in logs we need to
17898 				 * investagate which reason we missed.
17899 				 */
17900 				end_window = 1;
17901 			}
17902 			if (end_window) {
17903 				uint8_t log = 0;
17904 
17905 				/* Adjust the Gput measurement */
17906 				if ((tp->t_flags & TF_GPUTINPROG) &&
17907 				    SEQ_GT(tp->gput_ack, tp->snd_max)) {
17908 					tp->gput_ack = tp->snd_max;
17909 					if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) {
17910 						/*
17911 						 * There is not enough to measure.
17912 						 */
17913 						tp->t_flags &= ~TF_GPUTINPROG;
17914 						rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
17915 									   rack->r_ctl.rc_gp_srtt /*flex1*/,
17916 									   tp->gput_seq,
17917 									   0, 0, 18, __LINE__, NULL, 0);
17918 					} else
17919 						log = 1;
17920 				}
17921 				/* Mark the last packet has app limited */
17922 				rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
17923 				if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) {
17924 					if (rack->r_ctl.rc_app_limited_cnt == 0)
17925 						rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm;
17926 					else {
17927 						/*
17928 						 * Go out to the end app limited and mark
17929 						 * this new one as next and move the end_appl up
17930 						 * to this guy.
17931 						 */
17932 						if (rack->r_ctl.rc_end_appl)
17933 							rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start;
17934 						rack->r_ctl.rc_end_appl = rsm;
17935 					}
17936 					rsm->r_flags |= RACK_APP_LIMITED;
17937 					rack->r_ctl.rc_app_limited_cnt++;
17938 				}
17939 				if (log)
17940 					rack_log_pacing_delay_calc(rack,
17941 								   rack->r_ctl.rc_app_limited_cnt, seq,
17942 								   tp->gput_ack, 0, 0, 4, __LINE__, NULL, 0);
17943 			}
17944 		}
17945 		/* Check if we need to go into persists or not */
17946 		if ((tp->snd_max == tp->snd_una) &&
17947 		    TCPS_HAVEESTABLISHED(tp->t_state) &&
17948 		    sbavail(sb) &&
17949 		    (sbavail(sb) > tp->snd_wnd) &&
17950 		    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg))) {
17951 			/* Yes lets make sure to move to persist before timer-start */
17952 			rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime);
17953 		}
17954 		rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, sup_rack);
17955 		rack_log_type_just_return(rack, cts, tot_len_this_send, slot, hpts_calling, app_limited, cwnd_to_use);
17956 	}
17957 #ifdef NETFLIX_SHARED_CWND
17958 	if ((sbavail(sb) == 0) &&
17959 	    rack->r_ctl.rc_scw) {
17960 		tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
17961 		rack->rack_scwnd_is_idle = 1;
17962 	}
17963 #endif
17964 #ifdef TCP_ACCOUNTING
17965 	if (tot_len_this_send > 0) {
17966 		crtsc = get_cyclecount();
17967 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17968 			tp->tcp_cnt_counters[SND_OUT_DATA]++;
17969 		}
17970 		counter_u64_add(tcp_cnt_counters[SND_OUT_DATA], 1);
17971 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17972 			tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
17973 		}
17974 		counter_u64_add(tcp_proc_time[SND_OUT_DATA], (crtsc - ts_val));
17975 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17976 			tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) / segsiz);
17977 		}
17978 		counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((tot_len_this_send + segsiz - 1) / segsiz));
17979 	} else {
17980 		crtsc = get_cyclecount();
17981 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17982 			tp->tcp_cnt_counters[SND_LIMITED]++;
17983 		}
17984 		counter_u64_add(tcp_cnt_counters[SND_LIMITED], 1);
17985 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17986 			tp->tcp_proc_time[SND_LIMITED] += (crtsc - ts_val);
17987 		}
17988 		counter_u64_add(tcp_proc_time[SND_LIMITED], (crtsc - ts_val));
17989 	}
17990 	sched_unpin();
17991 #endif
17992 	return (0);
17993 
17994 send:
17995 	if (rsm || sack_rxmit)
17996 		counter_u64_add(rack_nfto_resend, 1);
17997 	else
17998 		counter_u64_add(rack_non_fto_send, 1);
17999 	if ((flags & TH_FIN) &&
18000 	    sbavail(sb)) {
18001 		/*
18002 		 * We do not transmit a FIN
18003 		 * with data outstanding. We
18004 		 * need to make it so all data
18005 		 * is acked first.
18006 		 */
18007 		flags &= ~TH_FIN;
18008 	}
18009 	/* Enforce stack imposed max seg size if we have one */
18010 	if (rack->r_ctl.rc_pace_max_segs &&
18011 	    (len > rack->r_ctl.rc_pace_max_segs)) {
18012 		mark = 1;
18013 		len = rack->r_ctl.rc_pace_max_segs;
18014 	}
18015 	SOCKBUF_LOCK_ASSERT(sb);
18016 	if (len > 0) {
18017 		if (len >= segsiz)
18018 			tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT;
18019 		else
18020 			tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT;
18021 	}
18022 	/*
18023 	 * Before ESTABLISHED, force sending of initial options unless TCP
18024 	 * set not to do any options. NOTE: we assume that the IP/TCP header
18025 	 * plus TCP options always fit in a single mbuf, leaving room for a
18026 	 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr)
18027 	 * + optlen <= MCLBYTES
18028 	 */
18029 	optlen = 0;
18030 #ifdef INET6
18031 	if (isipv6)
18032 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
18033 	else
18034 #endif
18035 		hdrlen = sizeof(struct tcpiphdr);
18036 
18037 	/*
18038 	 * Compute options for segment. We only have to care about SYN and
18039 	 * established connection segments.  Options for SYN-ACK segments
18040 	 * are handled in TCP syncache.
18041 	 */
18042 	to.to_flags = 0;
18043 	if ((tp->t_flags & TF_NOOPT) == 0) {
18044 		/* Maximum segment size. */
18045 		if (flags & TH_SYN) {
18046 			tp->snd_nxt = tp->iss;
18047 			to.to_mss = tcp_mssopt(&inp->inp_inc);
18048 			if (tp->t_port)
18049 				to.to_mss -= V_tcp_udp_tunneling_overhead;
18050 			to.to_flags |= TOF_MSS;
18051 
18052 			/*
18053 			 * On SYN or SYN|ACK transmits on TFO connections,
18054 			 * only include the TFO option if it is not a
18055 			 * retransmit, as the presence of the TFO option may
18056 			 * have caused the original SYN or SYN|ACK to have
18057 			 * been dropped by a middlebox.
18058 			 */
18059 			if (IS_FASTOPEN(tp->t_flags) &&
18060 			    (tp->t_rxtshift == 0)) {
18061 				if (tp->t_state == TCPS_SYN_RECEIVED) {
18062 					to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN;
18063 					to.to_tfo_cookie =
18064 						(u_int8_t *)&tp->t_tfo_cookie.server;
18065 					to.to_flags |= TOF_FASTOPEN;
18066 					wanted_cookie = 1;
18067 				} else if (tp->t_state == TCPS_SYN_SENT) {
18068 					to.to_tfo_len =
18069 						tp->t_tfo_client_cookie_len;
18070 					to.to_tfo_cookie =
18071 						tp->t_tfo_cookie.client;
18072 					to.to_flags |= TOF_FASTOPEN;
18073 					wanted_cookie = 1;
18074 					/*
18075 					 * If we wind up having more data to
18076 					 * send with the SYN than can fit in
18077 					 * one segment, don't send any more
18078 					 * until the SYN|ACK comes back from
18079 					 * the other end.
18080 					 */
18081 					sendalot = 0;
18082 				}
18083 			}
18084 		}
18085 		/* Window scaling. */
18086 		if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
18087 			to.to_wscale = tp->request_r_scale;
18088 			to.to_flags |= TOF_SCALE;
18089 		}
18090 		/* Timestamps. */
18091 		if ((tp->t_flags & TF_RCVD_TSTMP) ||
18092 		    ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
18093 			to.to_tsval = ms_cts + tp->ts_offset;
18094 			to.to_tsecr = tp->ts_recent;
18095 			to.to_flags |= TOF_TS;
18096 		}
18097 		/* Set receive buffer autosizing timestamp. */
18098 		if (tp->rfbuf_ts == 0 &&
18099 		    (so->so_rcv.sb_flags & SB_AUTOSIZE))
18100 			tp->rfbuf_ts = tcp_ts_getticks();
18101 		/* Selective ACK's. */
18102 		if (tp->t_flags & TF_SACK_PERMIT) {
18103 			if (flags & TH_SYN)
18104 				to.to_flags |= TOF_SACKPERM;
18105 			else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
18106 				 tp->rcv_numsacks > 0) {
18107 				to.to_flags |= TOF_SACK;
18108 				to.to_nsacks = tp->rcv_numsacks;
18109 				to.to_sacks = (u_char *)tp->sackblks;
18110 			}
18111 		}
18112 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
18113 		/* TCP-MD5 (RFC2385). */
18114 		if (tp->t_flags & TF_SIGNATURE)
18115 			to.to_flags |= TOF_SIGNATURE;
18116 #endif				/* TCP_SIGNATURE */
18117 
18118 		/* Processing the options. */
18119 		hdrlen += optlen = tcp_addoptions(&to, opt);
18120 		/*
18121 		 * If we wanted a TFO option to be added, but it was unable
18122 		 * to fit, ensure no data is sent.
18123 		 */
18124 		if (IS_FASTOPEN(tp->t_flags) && wanted_cookie &&
18125 		    !(to.to_flags & TOF_FASTOPEN))
18126 			len = 0;
18127 	}
18128 	if (tp->t_port) {
18129 		if (V_tcp_udp_tunneling_port == 0) {
18130 			/* The port was removed?? */
18131 			SOCKBUF_UNLOCK(&so->so_snd);
18132 #ifdef TCP_ACCOUNTING
18133 			crtsc = get_cyclecount();
18134 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
18135 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
18136 			}
18137 			counter_u64_add(tcp_cnt_counters[SND_OUT_FAIL], 1);
18138 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
18139 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
18140 			}
18141 			counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val));
18142 			sched_unpin();
18143 #endif
18144 			return (EHOSTUNREACH);
18145 		}
18146 		hdrlen += sizeof(struct udphdr);
18147 	}
18148 #ifdef INET6
18149 	if (isipv6)
18150 		ipoptlen = ip6_optlen(tp->t_inpcb);
18151 	else
18152 #endif
18153 		if (tp->t_inpcb->inp_options)
18154 			ipoptlen = tp->t_inpcb->inp_options->m_len -
18155 				offsetof(struct ipoption, ipopt_list);
18156 		else
18157 			ipoptlen = 0;
18158 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
18159 	ipoptlen += ipsec_optlen;
18160 #endif
18161 
18162 	/*
18163 	 * Adjust data length if insertion of options will bump the packet
18164 	 * length beyond the t_maxseg length. Clear the FIN bit because we
18165 	 * cut off the tail of the segment.
18166 	 */
18167 	if (len + optlen + ipoptlen > tp->t_maxseg) {
18168 		if (tso) {
18169 			uint32_t if_hw_tsomax;
18170 			uint32_t moff;
18171 			int32_t max_len;
18172 
18173 			/* extract TSO information */
18174 			if_hw_tsomax = tp->t_tsomax;
18175 			if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
18176 			if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
18177 			KASSERT(ipoptlen == 0,
18178 				("%s: TSO can't do IP options", __func__));
18179 
18180 			/*
18181 			 * Check if we should limit by maximum payload
18182 			 * length:
18183 			 */
18184 			if (if_hw_tsomax != 0) {
18185 				/* compute maximum TSO length */
18186 				max_len = (if_hw_tsomax - hdrlen -
18187 					   max_linkhdr);
18188 				if (max_len <= 0) {
18189 					len = 0;
18190 				} else if (len > max_len) {
18191 					sendalot = 1;
18192 					len = max_len;
18193 					mark = 2;
18194 				}
18195 			}
18196 			/*
18197 			 * Prevent the last segment from being fractional
18198 			 * unless the send sockbuf can be emptied:
18199 			 */
18200 			max_len = (tp->t_maxseg - optlen);
18201 			if ((sb_offset + len) < sbavail(sb)) {
18202 				moff = len % (u_int)max_len;
18203 				if (moff != 0) {
18204 					mark = 3;
18205 					len -= moff;
18206 				}
18207 			}
18208 			/*
18209 			 * In case there are too many small fragments don't
18210 			 * use TSO:
18211 			 */
18212 			if (len <= segsiz) {
18213 				mark = 4;
18214 				tso = 0;
18215 			}
18216 			/*
18217 			 * Send the FIN in a separate segment after the bulk
18218 			 * sending is done. We don't trust the TSO
18219 			 * implementations to clear the FIN flag on all but
18220 			 * the last segment.
18221 			 */
18222 			if (tp->t_flags & TF_NEEDFIN) {
18223 				sendalot = 4;
18224 			}
18225 		} else {
18226 			mark = 5;
18227 			if (optlen + ipoptlen >= tp->t_maxseg) {
18228 				/*
18229 				 * Since we don't have enough space to put
18230 				 * the IP header chain and the TCP header in
18231 				 * one packet as required by RFC 7112, don't
18232 				 * send it. Also ensure that at least one
18233 				 * byte of the payload can be put into the
18234 				 * TCP segment.
18235 				 */
18236 				SOCKBUF_UNLOCK(&so->so_snd);
18237 				error = EMSGSIZE;
18238 				sack_rxmit = 0;
18239 				goto out;
18240 			}
18241 			len = tp->t_maxseg - optlen - ipoptlen;
18242 			sendalot = 5;
18243 		}
18244 	} else {
18245 		tso = 0;
18246 		mark = 6;
18247 	}
18248 	KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET,
18249 		("%s: len > IP_MAXPACKET", __func__));
18250 #ifdef DIAGNOSTIC
18251 #ifdef INET6
18252 	if (max_linkhdr + hdrlen > MCLBYTES)
18253 #else
18254 		if (max_linkhdr + hdrlen > MHLEN)
18255 #endif
18256 			panic("tcphdr too big");
18257 #endif
18258 
18259 	/*
18260 	 * This KASSERT is here to catch edge cases at a well defined place.
18261 	 * Before, those had triggered (random) panic conditions further
18262 	 * down.
18263 	 */
18264 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
18265 	if ((len == 0) &&
18266 	    (flags & TH_FIN) &&
18267 	    (sbused(sb))) {
18268 		/*
18269 		 * We have outstanding data, don't send a fin by itself!.
18270 		 */
18271 		goto just_return;
18272 	}
18273 	/*
18274 	 * Grab a header mbuf, attaching a copy of data to be transmitted,
18275 	 * and initialize the header from the template for sends on this
18276 	 * connection.
18277 	 */
18278 	hw_tls = (sb->sb_flags & SB_TLS_IFNET) != 0;
18279 	if (len) {
18280 		uint32_t max_val;
18281 		uint32_t moff;
18282 
18283 		if (rack->r_ctl.rc_pace_max_segs)
18284 			max_val = rack->r_ctl.rc_pace_max_segs;
18285 		else if (rack->rc_user_set_max_segs)
18286 			max_val = rack->rc_user_set_max_segs * segsiz;
18287 		else
18288 			max_val = len;
18289 		/*
18290 		 * We allow a limit on sending with hptsi.
18291 		 */
18292 		if (len > max_val) {
18293 			mark = 7;
18294 			len = max_val;
18295 		}
18296 #ifdef INET6
18297 		if (MHLEN < hdrlen + max_linkhdr)
18298 			m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
18299 		else
18300 #endif
18301 			m = m_gethdr(M_NOWAIT, MT_DATA);
18302 
18303 		if (m == NULL) {
18304 			SOCKBUF_UNLOCK(sb);
18305 			error = ENOBUFS;
18306 			sack_rxmit = 0;
18307 			goto out;
18308 		}
18309 		m->m_data += max_linkhdr;
18310 		m->m_len = hdrlen;
18311 
18312 		/*
18313 		 * Start the m_copy functions from the closest mbuf to the
18314 		 * sb_offset in the socket buffer chain.
18315 		 */
18316 		mb = sbsndptr_noadv(sb, sb_offset, &moff);
18317 		s_mb = mb;
18318 		s_moff = moff;
18319 		if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) {
18320 			m_copydata(mb, moff, (int)len,
18321 				   mtod(m, caddr_t)+hdrlen);
18322 			if (SEQ_LT(tp->snd_nxt, tp->snd_max))
18323 				sbsndptr_adv(sb, mb, len);
18324 			m->m_len += len;
18325 		} else {
18326 			struct sockbuf *msb;
18327 
18328 			if (SEQ_LT(tp->snd_nxt, tp->snd_max))
18329 				msb = NULL;
18330 			else
18331 				msb = sb;
18332 			m->m_next = tcp_m_copym(
18333 				mb, moff, &len,
18334 				if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, msb,
18335 				((rsm == NULL) ? hw_tls : 0)
18336 #ifdef NETFLIX_COPY_ARGS
18337 				, &s_mb, &s_moff
18338 #endif
18339 				);
18340 			if (len <= (tp->t_maxseg - optlen)) {
18341 				/*
18342 				 * Must have ran out of mbufs for the copy
18343 				 * shorten it to no longer need tso. Lets
18344 				 * not put on sendalot since we are low on
18345 				 * mbufs.
18346 				 */
18347 				tso = 0;
18348 			}
18349 			if (m->m_next == NULL) {
18350 				SOCKBUF_UNLOCK(sb);
18351 				(void)m_free(m);
18352 				error = ENOBUFS;
18353 				sack_rxmit = 0;
18354 				goto out;
18355 			}
18356 		}
18357 		if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) {
18358 			if (rsm && (rsm->r_flags & RACK_TLP)) {
18359 				/*
18360 				 * TLP should not count in retran count, but
18361 				 * in its own bin
18362 				 */
18363 				counter_u64_add(rack_tlp_retran, 1);
18364 				counter_u64_add(rack_tlp_retran_bytes, len);
18365 			} else {
18366 				tp->t_sndrexmitpack++;
18367 				KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
18368 				KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
18369 			}
18370 #ifdef STATS
18371 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
18372 						 len);
18373 #endif
18374 		} else {
18375 			KMOD_TCPSTAT_INC(tcps_sndpack);
18376 			KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
18377 #ifdef STATS
18378 			stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
18379 						 len);
18380 #endif
18381 		}
18382 		/*
18383 		 * If we're sending everything we've got, set PUSH. (This
18384 		 * will keep happy those implementations which only give
18385 		 * data to the user when a buffer fills or a PUSH comes in.)
18386 		 */
18387 		if (sb_offset + len == sbused(sb) &&
18388 		    sbused(sb) &&
18389 		    !(flags & TH_SYN)) {
18390 			flags |= TH_PUSH;
18391 			add_flag |= RACK_HAD_PUSH;
18392 		}
18393 
18394 		SOCKBUF_UNLOCK(sb);
18395 	} else {
18396 		SOCKBUF_UNLOCK(sb);
18397 		if (tp->t_flags & TF_ACKNOW)
18398 			KMOD_TCPSTAT_INC(tcps_sndacks);
18399 		else if (flags & (TH_SYN | TH_FIN | TH_RST))
18400 			KMOD_TCPSTAT_INC(tcps_sndctrl);
18401 		else
18402 			KMOD_TCPSTAT_INC(tcps_sndwinup);
18403 
18404 		m = m_gethdr(M_NOWAIT, MT_DATA);
18405 		if (m == NULL) {
18406 			error = ENOBUFS;
18407 			sack_rxmit = 0;
18408 			goto out;
18409 		}
18410 #ifdef INET6
18411 		if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
18412 		    MHLEN >= hdrlen) {
18413 			M_ALIGN(m, hdrlen);
18414 		} else
18415 #endif
18416 			m->m_data += max_linkhdr;
18417 		m->m_len = hdrlen;
18418 	}
18419 	SOCKBUF_UNLOCK_ASSERT(sb);
18420 	m->m_pkthdr.rcvif = (struct ifnet *)0;
18421 #ifdef MAC
18422 	mac_inpcb_create_mbuf(inp, m);
18423 #endif
18424 	if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) &&  rack->r_fsb_inited) {
18425 #ifdef INET6
18426 		if (isipv6)
18427 			ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
18428 		else
18429 #endif				/* INET6 */
18430 			ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
18431 		th = rack->r_ctl.fsb.th;
18432 		udp = rack->r_ctl.fsb.udp;
18433 		if (udp) {
18434 #ifdef INET6
18435 			if (isipv6)
18436 				ulen = hdrlen + len - sizeof(struct ip6_hdr);
18437 			else
18438 #endif				/* INET6 */
18439 				ulen = hdrlen + len - sizeof(struct ip);
18440 			udp->uh_ulen = htons(ulen);
18441 		}
18442 	} else {
18443 #ifdef INET6
18444 		if (isipv6) {
18445 			ip6 = mtod(m, struct ip6_hdr *);
18446 			if (tp->t_port) {
18447 				udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
18448 				udp->uh_sport = htons(V_tcp_udp_tunneling_port);
18449 				udp->uh_dport = tp->t_port;
18450 				ulen = hdrlen + len - sizeof(struct ip6_hdr);
18451 				udp->uh_ulen = htons(ulen);
18452 				th = (struct tcphdr *)(udp + 1);
18453 			} else
18454 				th = (struct tcphdr *)(ip6 + 1);
18455 			tcpip_fillheaders(inp, tp->t_port, ip6, th);
18456 		} else
18457 #endif				/* INET6 */
18458 		{
18459 			ip = mtod(m, struct ip *);
18460 			if (tp->t_port) {
18461 				udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
18462 				udp->uh_sport = htons(V_tcp_udp_tunneling_port);
18463 				udp->uh_dport = tp->t_port;
18464 				ulen = hdrlen + len - sizeof(struct ip);
18465 				udp->uh_ulen = htons(ulen);
18466 				th = (struct tcphdr *)(udp + 1);
18467 			} else
18468 				th = (struct tcphdr *)(ip + 1);
18469 			tcpip_fillheaders(inp, tp->t_port, ip, th);
18470 		}
18471 	}
18472 	/*
18473 	 * Fill in fields, remembering maximum advertised window for use in
18474 	 * delaying messages about window sizes. If resending a FIN, be sure
18475 	 * not to use a new sequence number.
18476 	 */
18477 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
18478 	    tp->snd_nxt == tp->snd_max)
18479 		tp->snd_nxt--;
18480 	/*
18481 	 * If we are starting a connection, send ECN setup SYN packet. If we
18482 	 * are on a retransmit, we may resend those bits a number of times
18483 	 * as per RFC 3168.
18484 	 */
18485 	if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn) {
18486 		flags |= tcp_ecn_output_syn_sent(tp);
18487 	}
18488 	/* Also handle parallel SYN for ECN */
18489 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
18490 	    (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) {
18491 		int ect = tcp_ecn_output_established(tp, &flags, len, sack_rxmit);
18492 		if ((tp->t_state == TCPS_SYN_RECEIVED) &&
18493 		    (tp->t_flags2 & TF2_ECN_SND_ECE))
18494 			tp->t_flags2 &= ~TF2_ECN_SND_ECE;
18495 #ifdef INET6
18496 		if (isipv6) {
18497 			ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
18498 			ip6->ip6_flow |= htonl(ect << 20);
18499 		}
18500 		else
18501 #endif
18502 		{
18503 			ip->ip_tos &= ~IPTOS_ECN_MASK;
18504 			ip->ip_tos |= ect;
18505 		}
18506 	}
18507 	/*
18508 	 * If we are doing retransmissions, then snd_nxt will not reflect
18509 	 * the first unsent octet.  For ACK only packets, we do not want the
18510 	 * sequence number of the retransmitted packet, we want the sequence
18511 	 * number of the next unsent octet.  So, if there is no data (and no
18512 	 * SYN or FIN), use snd_max instead of snd_nxt when filling in
18513 	 * ti_seq.  But if we are in persist state, snd_max might reflect
18514 	 * one byte beyond the right edge of the window, so use snd_nxt in
18515 	 * that case, since we know we aren't doing a retransmission.
18516 	 * (retransmit and persist are mutually exclusive...)
18517 	 */
18518 	if (sack_rxmit == 0) {
18519 		if (len || (flags & (TH_SYN | TH_FIN))) {
18520 			th->th_seq = htonl(tp->snd_nxt);
18521 			rack_seq = tp->snd_nxt;
18522 		} else {
18523 			th->th_seq = htonl(tp->snd_max);
18524 			rack_seq = tp->snd_max;
18525 		}
18526 	} else {
18527 		th->th_seq = htonl(rsm->r_start);
18528 		rack_seq = rsm->r_start;
18529 	}
18530 	th->th_ack = htonl(tp->rcv_nxt);
18531 	tcp_set_flags(th, flags);
18532 	/*
18533 	 * Calculate receive window.  Don't shrink window, but avoid silly
18534 	 * window syndrome.
18535 	 * If a RST segment is sent, advertise a window of zero.
18536 	 */
18537 	if (flags & TH_RST) {
18538 		recwin = 0;
18539 	} else {
18540 		if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
18541 		    recwin < (long)segsiz) {
18542 			recwin = 0;
18543 		}
18544 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) &&
18545 		    recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
18546 			recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
18547 	}
18548 
18549 	/*
18550 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or
18551 	 * <SYN,ACK>) segment itself is never scaled.  The <SYN,ACK> case is
18552 	 * handled in syncache.
18553 	 */
18554 	if (flags & TH_SYN)
18555 		th->th_win = htons((u_short)
18556 				   (min(sbspace(&so->so_rcv), TCP_MAXWIN)));
18557 	else {
18558 		/* Avoid shrinking window with window scaling. */
18559 		recwin = roundup2(recwin, 1 << tp->rcv_scale);
18560 		th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
18561 	}
18562 	/*
18563 	 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0
18564 	 * window.  This may cause the remote transmitter to stall.  This
18565 	 * flag tells soreceive() to disable delayed acknowledgements when
18566 	 * draining the buffer.  This can occur if the receiver is
18567 	 * attempting to read more data than can be buffered prior to
18568 	 * transmitting on the connection.
18569 	 */
18570 	if (th->th_win == 0) {
18571 		tp->t_sndzerowin++;
18572 		tp->t_flags |= TF_RXWIN0SENT;
18573 	} else
18574 		tp->t_flags &= ~TF_RXWIN0SENT;
18575 	tp->snd_up = tp->snd_una;	/* drag it along, its deprecated */
18576 	/* Now are we using fsb?, if so copy the template data to the mbuf */
18577 	if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) && rack->r_fsb_inited) {
18578 		uint8_t *cpto;
18579 
18580 		cpto = mtod(m, uint8_t *);
18581 		memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
18582 		/*
18583 		 * We have just copied in:
18584 		 * IP/IP6
18585 		 * <optional udphdr>
18586 		 * tcphdr (no options)
18587 		 *
18588 		 * We need to grab the correct pointers into the mbuf
18589 		 * for both the tcp header, and possibly the udp header (if tunneling).
18590 		 * We do this by using the offset in the copy buffer and adding it
18591 		 * to the mbuf base pointer (cpto).
18592 		 */
18593 #ifdef INET6
18594 		if (isipv6)
18595 			ip6 = mtod(m, struct ip6_hdr *);
18596 		else
18597 #endif				/* INET6 */
18598 			ip = mtod(m, struct ip *);
18599 		th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
18600 		/* If we have a udp header lets set it into the mbuf as well */
18601 		if (udp)
18602 			udp = (struct udphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.udp - rack->r_ctl.fsb.tcp_ip_hdr));
18603 	}
18604 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
18605 	if (to.to_flags & TOF_SIGNATURE) {
18606 		/*
18607 		 * Calculate MD5 signature and put it into the place
18608 		 * determined before.
18609 		 * NOTE: since TCP options buffer doesn't point into
18610 		 * mbuf's data, calculate offset and use it.
18611 		 */
18612 		if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
18613 						       (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
18614 			/*
18615 			 * Do not send segment if the calculation of MD5
18616 			 * digest has failed.
18617 			 */
18618 			goto out;
18619 		}
18620 	}
18621 #endif
18622 	if (optlen) {
18623 		bcopy(opt, th + 1, optlen);
18624 		th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
18625 	}
18626 	/*
18627 	 * Put TCP length in extended header, and then checksum extended
18628 	 * header and data.
18629 	 */
18630 	m->m_pkthdr.len = hdrlen + len;	/* in6_cksum() need this */
18631 #ifdef INET6
18632 	if (isipv6) {
18633 		/*
18634 		 * ip6_plen is not need to be filled now, and will be filled
18635 		 * in ip6_output.
18636 		 */
18637 		if (tp->t_port) {
18638 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
18639 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
18640 			udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
18641 			th->th_sum = htons(0);
18642 			UDPSTAT_INC(udps_opackets);
18643 		} else {
18644 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
18645 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
18646 			th->th_sum = in6_cksum_pseudo(ip6,
18647 						      sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
18648 						      0);
18649 		}
18650 	}
18651 #endif
18652 #if defined(INET6) && defined(INET)
18653 	else
18654 #endif
18655 #ifdef INET
18656 	{
18657 		if (tp->t_port) {
18658 			m->m_pkthdr.csum_flags = CSUM_UDP;
18659 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
18660 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
18661 						ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
18662 			th->th_sum = htons(0);
18663 			UDPSTAT_INC(udps_opackets);
18664 		} else {
18665 			m->m_pkthdr.csum_flags = CSUM_TCP;
18666 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
18667 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
18668 					       ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
18669 									IPPROTO_TCP + len + optlen));
18670 		}
18671 		/* IP version must be set here for ipv4/ipv6 checking later */
18672 		KASSERT(ip->ip_v == IPVERSION,
18673 			("%s: IP version incorrect: %d", __func__, ip->ip_v));
18674 	}
18675 #endif
18676 	/*
18677 	 * Enable TSO and specify the size of the segments. The TCP pseudo
18678 	 * header checksum is always provided. XXX: Fixme: This is currently
18679 	 * not the case for IPv6.
18680 	 */
18681 	if (tso) {
18682 		KASSERT(len > tp->t_maxseg - optlen,
18683 			("%s: len <= tso_segsz", __func__));
18684 		m->m_pkthdr.csum_flags |= CSUM_TSO;
18685 		m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen;
18686 	}
18687 	KASSERT(len + hdrlen == m_length(m, NULL),
18688 		("%s: mbuf chain different than expected: %d + %u != %u",
18689 		 __func__, len, hdrlen, m_length(m, NULL)));
18690 
18691 #ifdef TCP_HHOOK
18692 	/* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */
18693 	hhook_run_tcp_est_out(tp, th, &to, len, tso);
18694 #endif
18695 	/* We're getting ready to send; log now. */
18696 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
18697 		union tcp_log_stackspecific log;
18698 
18699 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
18700 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_inp);
18701 		if (rack->rack_no_prr)
18702 			log.u_bbr.flex1 = 0;
18703 		else
18704 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
18705 		log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
18706 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
18707 		log.u_bbr.flex4 = orig_len;
18708 		/* Save off the early/late values */
18709 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
18710 		log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
18711 		log.u_bbr.bw_inuse = rack_get_bw(rack);
18712 		log.u_bbr.flex8 = 0;
18713 		if (rsm) {
18714 			if (rsm->r_flags & RACK_RWND_COLLAPSED) {
18715 				rack_log_collapse(rack, rsm->r_start, rsm->r_end, 0, __LINE__, 5, rsm->r_flags, rsm);
18716 				counter_u64_add(rack_collapsed_win_rxt, 1);
18717 				counter_u64_add(rack_collapsed_win_rxt_bytes, (rsm->r_end - rsm->r_start));
18718 			}
18719 			if (doing_tlp)
18720 				log.u_bbr.flex8 = 2;
18721 			else
18722 				log.u_bbr.flex8 = 1;
18723 		} else {
18724 			if (doing_tlp)
18725 				log.u_bbr.flex8 = 3;
18726 			else
18727 				log.u_bbr.flex8 = 0;
18728 		}
18729 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm);
18730 		log.u_bbr.flex7 = mark;
18731 		log.u_bbr.flex7 <<= 8;
18732 		log.u_bbr.flex7 |= pass;
18733 		log.u_bbr.pkts_out = tp->t_maxseg;
18734 		log.u_bbr.timeStamp = cts;
18735 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18736 		log.u_bbr.lt_epoch = cwnd_to_use;
18737 		log.u_bbr.delivered = sendalot;
18738 		lgb = tcp_log_event_(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK,
18739 				     len, &log, false, NULL, NULL, 0, &tv);
18740 	} else
18741 		lgb = NULL;
18742 
18743 	/*
18744 	 * Fill in IP length and desired time to live and send to IP level.
18745 	 * There should be a better way to handle ttl and tos; we could keep
18746 	 * them in the template, but need a way to checksum without them.
18747 	 */
18748 	/*
18749 	 * m->m_pkthdr.len should have been set before cksum calcuration,
18750 	 * because in6_cksum() need it.
18751 	 */
18752 #ifdef INET6
18753 	if (isipv6) {
18754 		/*
18755 		 * we separately set hoplimit for every segment, since the
18756 		 * user might want to change the value via setsockopt. Also,
18757 		 * desired default hop limit might be changed via Neighbor
18758 		 * Discovery.
18759 		 */
18760 		rack->r_ctl.fsb.hoplimit = ip6->ip6_hlim = in6_selecthlim(inp, NULL);
18761 
18762 		/*
18763 		 * Set the packet size here for the benefit of DTrace
18764 		 * probes. ip6_output() will set it properly; it's supposed
18765 		 * to include the option header lengths as well.
18766 		 */
18767 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
18768 
18769 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
18770 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
18771 		else
18772 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
18773 
18774 		if (tp->t_state == TCPS_SYN_SENT)
18775 			TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th);
18776 
18777 		TCP_PROBE5(send, NULL, tp, ip6, tp, th);
18778 		/* TODO: IPv6 IP6TOS_ECT bit on */
18779 		error = ip6_output(m,
18780 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
18781 				   inp->in6p_outputopts,
18782 #else
18783 				   NULL,
18784 #endif
18785 				   &inp->inp_route6,
18786 				   ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0),
18787 				   NULL, NULL, inp);
18788 
18789 		if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL)
18790 			mtu = inp->inp_route6.ro_nh->nh_mtu;
18791 	}
18792 #endif				/* INET6 */
18793 #if defined(INET) && defined(INET6)
18794 	else
18795 #endif
18796 #ifdef INET
18797 	{
18798 		ip->ip_len = htons(m->m_pkthdr.len);
18799 #ifdef INET6
18800 		if (inp->inp_vflag & INP_IPV6PROTO)
18801 			ip->ip_ttl = in6_selecthlim(inp, NULL);
18802 #endif				/* INET6 */
18803 		rack->r_ctl.fsb.hoplimit = ip->ip_ttl;
18804 		/*
18805 		 * If we do path MTU discovery, then we set DF on every
18806 		 * packet. This might not be the best thing to do according
18807 		 * to RFC3390 Section 2. However the tcp hostcache migitates
18808 		 * the problem so it affects only the first tcp connection
18809 		 * with a host.
18810 		 *
18811 		 * NB: Don't set DF on small MTU/MSS to have a safe
18812 		 * fallback.
18813 		 */
18814 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
18815 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
18816 			if (tp->t_port == 0 || len < V_tcp_minmss) {
18817 				ip->ip_off |= htons(IP_DF);
18818 			}
18819 		} else {
18820 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
18821 		}
18822 
18823 		if (tp->t_state == TCPS_SYN_SENT)
18824 			TCP_PROBE5(connect__request, NULL, tp, ip, tp, th);
18825 
18826 		TCP_PROBE5(send, NULL, tp, ip, tp, th);
18827 
18828 		error = ip_output(m,
18829 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
18830 				  inp->inp_options,
18831 #else
18832 				  NULL,
18833 #endif
18834 				  &inp->inp_route,
18835 				  ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0,
18836 				  inp);
18837 		if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL)
18838 			mtu = inp->inp_route.ro_nh->nh_mtu;
18839 	}
18840 #endif				/* INET */
18841 
18842 out:
18843 	if (lgb) {
18844 		lgb->tlb_errno = error;
18845 		lgb = NULL;
18846 	}
18847 	/*
18848 	 * In transmit state, time the transmission and arrange for the
18849 	 * retransmit.  In persist state, just set snd_max.
18850 	 */
18851 	if (error == 0) {
18852 		tcp_account_for_send(tp, len, (rsm != NULL), doing_tlp, hw_tls);
18853 		if (rsm && doing_tlp) {
18854 			rack->rc_last_sent_tlp_past_cumack = 0;
18855 			rack->rc_last_sent_tlp_seq_valid = 1;
18856 			rack->r_ctl.last_sent_tlp_seq = rsm->r_start;
18857 			rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start;
18858 		}
18859 		rack->forced_ack = 0;	/* If we send something zap the FA flag */
18860 		if (rsm && (doing_tlp == 0)) {
18861 			/* Set we retransmitted */
18862 			rack->rc_gp_saw_rec = 1;
18863 		} else {
18864 			if (cwnd_to_use > tp->snd_ssthresh) {
18865 				/* Set we sent in CA */
18866 				rack->rc_gp_saw_ca = 1;
18867 			} else {
18868 				/* Set we sent in SS */
18869 				rack->rc_gp_saw_ss = 1;
18870 			}
18871 		}
18872 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
18873 		    (tp->t_flags & TF_SACK_PERMIT) &&
18874 		    tp->rcv_numsacks > 0)
18875 			tcp_clean_dsack_blocks(tp);
18876 		tot_len_this_send += len;
18877 		if (len == 0)
18878 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_SNDACK], 1);
18879 		else if (len == 1) {
18880 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_PERSIST], 1);
18881 		} else if (len > 1) {
18882 			int idx;
18883 
18884 			idx = (len / segsiz) + 3;
18885 			if (idx >= TCP_MSS_ACCT_ATIMER)
18886 				counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
18887 			else
18888 				counter_u64_add(rack_out_size[idx], 1);
18889 		}
18890 	}
18891 	if ((rack->rack_no_prr == 0) &&
18892 	    sub_from_prr &&
18893 	    (error == 0)) {
18894 		if (rack->r_ctl.rc_prr_sndcnt >= len)
18895 			rack->r_ctl.rc_prr_sndcnt -= len;
18896 		else
18897 			rack->r_ctl.rc_prr_sndcnt = 0;
18898 	}
18899 	sub_from_prr = 0;
18900 	if (doing_tlp) {
18901 		/* Make sure the TLP is added */
18902 		add_flag |= RACK_TLP;
18903 	} else if (rsm) {
18904 		/* If its a resend without TLP then it must not have the flag */
18905 		rsm->r_flags &= ~RACK_TLP;
18906 	}
18907 	rack_log_output(tp, &to, len, rack_seq, (uint8_t) flags, error,
18908 			rack_to_usec_ts(&tv),
18909 			rsm, add_flag, s_mb, s_moff, hw_tls);
18910 
18911 
18912 	if ((error == 0) &&
18913 	    (len > 0) &&
18914 	    (tp->snd_una == tp->snd_max))
18915 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
18916 	{
18917 		tcp_seq startseq = tp->snd_nxt;
18918 
18919 		/* Track our lost count */
18920 		if (rsm && (doing_tlp == 0))
18921 			rack->r_ctl.rc_loss_count += rsm->r_end - rsm->r_start;
18922 		/*
18923 		 * Advance snd_nxt over sequence space of this segment.
18924 		 */
18925 		if (error)
18926 			/* We don't log or do anything with errors */
18927 			goto nomore;
18928 		if (doing_tlp == 0) {
18929 			if (rsm == NULL) {
18930 				/*
18931 				 * Not a retransmission of some
18932 				 * sort, new data is going out so
18933 				 * clear our TLP count and flag.
18934 				 */
18935 				rack->rc_tlp_in_progress = 0;
18936 				rack->r_ctl.rc_tlp_cnt_out = 0;
18937 			}
18938 		} else {
18939 			/*
18940 			 * We have just sent a TLP, mark that it is true
18941 			 * and make sure our in progress is set so we
18942 			 * continue to check the count.
18943 			 */
18944 			rack->rc_tlp_in_progress = 1;
18945 			rack->r_ctl.rc_tlp_cnt_out++;
18946 		}
18947 		if (flags & (TH_SYN | TH_FIN)) {
18948 			if (flags & TH_SYN)
18949 				tp->snd_nxt++;
18950 			if (flags & TH_FIN) {
18951 				tp->snd_nxt++;
18952 				tp->t_flags |= TF_SENTFIN;
18953 			}
18954 		}
18955 		/* In the ENOBUFS case we do *not* update snd_max */
18956 		if (sack_rxmit)
18957 			goto nomore;
18958 
18959 		tp->snd_nxt += len;
18960 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
18961 			if (tp->snd_una == tp->snd_max) {
18962 				/*
18963 				 * Update the time we just added data since
18964 				 * none was outstanding.
18965 				 */
18966 				rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
18967 				tp->t_acktime = ticks;
18968 			}
18969 			tp->snd_max = tp->snd_nxt;
18970 			/*
18971 			 * Time this transmission if not a retransmission and
18972 			 * not currently timing anything.
18973 			 * This is only relevant in case of switching back to
18974 			 * the base stack.
18975 			 */
18976 			if (tp->t_rtttime == 0) {
18977 				tp->t_rtttime = ticks;
18978 				tp->t_rtseq = startseq;
18979 				KMOD_TCPSTAT_INC(tcps_segstimed);
18980 			}
18981 			if (len &&
18982 			    ((tp->t_flags & TF_GPUTINPROG) == 0))
18983 				rack_start_gp_measurement(tp, rack, startseq, sb_offset);
18984 		}
18985 		/*
18986 		 * If we are doing FO we need to update the mbuf position and subtract
18987 		 * this happens when the peer sends us duplicate information and
18988 		 * we thus want to send a DSACK.
18989 		 *
18990 		 * XXXRRS: This brings to mind a ?, when we send a DSACK block is TSO
18991 		 * turned off? If not then we are going to echo multiple DSACK blocks
18992 		 * out (with the TSO), which we should not be doing.
18993 		 */
18994 		if (rack->r_fast_output && len) {
18995 			if (rack->r_ctl.fsb.left_to_send > len)
18996 				rack->r_ctl.fsb.left_to_send -= len;
18997 			else
18998 				rack->r_ctl.fsb.left_to_send = 0;
18999 			if (rack->r_ctl.fsb.left_to_send < segsiz)
19000 				rack->r_fast_output = 0;
19001 			if (rack->r_fast_output) {
19002 				rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
19003 				rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
19004 			}
19005 		}
19006 	}
19007 nomore:
19008 	if (error) {
19009 		rack->r_ctl.rc_agg_delayed = 0;
19010 		rack->r_early = 0;
19011 		rack->r_late = 0;
19012 		rack->r_ctl.rc_agg_early = 0;
19013 		SOCKBUF_UNLOCK_ASSERT(sb);	/* Check gotos. */
19014 		/*
19015 		 * Failures do not advance the seq counter above. For the
19016 		 * case of ENOBUFS we will fall out and retry in 1ms with
19017 		 * the hpts. Everything else will just have to retransmit
19018 		 * with the timer.
19019 		 *
19020 		 * In any case, we do not want to loop around for another
19021 		 * send without a good reason.
19022 		 */
19023 		sendalot = 0;
19024 		switch (error) {
19025 		case EPERM:
19026 			tp->t_softerror = error;
19027 #ifdef TCP_ACCOUNTING
19028 			crtsc = get_cyclecount();
19029 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19030 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
19031 			}
19032 			counter_u64_add(tcp_cnt_counters[SND_OUT_FAIL], 1);
19033 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19034 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
19035 			}
19036 			counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val));
19037 			sched_unpin();
19038 #endif
19039 			return (error);
19040 		case ENOBUFS:
19041 			/*
19042 			 * Pace us right away to retry in a some
19043 			 * time
19044 			 */
19045 			if (rack->r_ctl.crte != NULL) {
19046 				rack_trace_point(rack, RACK_TP_HWENOBUF);
19047 			} else
19048 				rack_trace_point(rack, RACK_TP_ENOBUF);
19049 			slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC);
19050 			if (rack->rc_enobuf < 0x7f)
19051 				rack->rc_enobuf++;
19052 			if (slot < (10 * HPTS_USEC_IN_MSEC))
19053 				slot = 10 * HPTS_USEC_IN_MSEC;
19054 			if (rack->r_ctl.crte != NULL) {
19055 				counter_u64_add(rack_saw_enobuf_hw, 1);
19056 				tcp_rl_log_enobuf(rack->r_ctl.crte);
19057 			}
19058 			counter_u64_add(rack_saw_enobuf, 1);
19059 			goto enobufs;
19060 		case EMSGSIZE:
19061 			/*
19062 			 * For some reason the interface we used initially
19063 			 * to send segments changed to another or lowered
19064 			 * its MTU. If TSO was active we either got an
19065 			 * interface without TSO capabilits or TSO was
19066 			 * turned off. If we obtained mtu from ip_output()
19067 			 * then update it and try again.
19068 			 */
19069 			if (tso)
19070 				tp->t_flags &= ~TF_TSO;
19071 			if (mtu != 0) {
19072 				tcp_mss_update(tp, -1, mtu, NULL, NULL);
19073 				goto again;
19074 			}
19075 			slot = 10 * HPTS_USEC_IN_MSEC;
19076 			rack_start_hpts_timer(rack, tp, cts, slot, 0, 0);
19077 #ifdef TCP_ACCOUNTING
19078 			crtsc = get_cyclecount();
19079 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19080 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
19081 			}
19082 			counter_u64_add(tcp_cnt_counters[SND_OUT_FAIL], 1);
19083 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19084 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
19085 			}
19086 			counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val));
19087 			sched_unpin();
19088 #endif
19089 			return (error);
19090 		case ENETUNREACH:
19091 			counter_u64_add(rack_saw_enetunreach, 1);
19092 		case EHOSTDOWN:
19093 		case EHOSTUNREACH:
19094 		case ENETDOWN:
19095 			if (TCPS_HAVERCVDSYN(tp->t_state)) {
19096 				tp->t_softerror = error;
19097 			}
19098 			/* FALLTHROUGH */
19099 		default:
19100 			slot = 10 * HPTS_USEC_IN_MSEC;
19101 			rack_start_hpts_timer(rack, tp, cts, slot, 0, 0);
19102 #ifdef TCP_ACCOUNTING
19103 			crtsc = get_cyclecount();
19104 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19105 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
19106 			}
19107 			counter_u64_add(tcp_cnt_counters[SND_OUT_FAIL], 1);
19108 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19109 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
19110 			}
19111 			counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val));
19112 			sched_unpin();
19113 #endif
19114 			return (error);
19115 		}
19116 	} else {
19117 		rack->rc_enobuf = 0;
19118 		if (IN_FASTRECOVERY(tp->t_flags) && rsm)
19119 			rack->r_ctl.retran_during_recovery += len;
19120 	}
19121 	KMOD_TCPSTAT_INC(tcps_sndtotal);
19122 
19123 	/*
19124 	 * Data sent (as far as we can tell). If this advertises a larger
19125 	 * window than any other segment, then remember the size of the
19126 	 * advertised window. Any pending ACK has now been sent.
19127 	 */
19128 	if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
19129 		tp->rcv_adv = tp->rcv_nxt + recwin;
19130 
19131 	tp->last_ack_sent = tp->rcv_nxt;
19132 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
19133 enobufs:
19134 	if (sendalot) {
19135 		/* Do we need to turn off sendalot? */
19136 		if (rack->r_ctl.rc_pace_max_segs &&
19137 		    (tot_len_this_send >= rack->r_ctl.rc_pace_max_segs)) {
19138 			/* We hit our max. */
19139 			sendalot = 0;
19140 		} else if ((rack->rc_user_set_max_segs) &&
19141 			   (tot_len_this_send >= (rack->rc_user_set_max_segs * segsiz))) {
19142 			/* We hit the user defined max */
19143 			sendalot = 0;
19144 		}
19145 	}
19146 	if ((error == 0) && (flags & TH_FIN))
19147 		tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN);
19148 	if (flags & TH_RST) {
19149 		/*
19150 		 * We don't send again after sending a RST.
19151 		 */
19152 		slot = 0;
19153 		sendalot = 0;
19154 		if (error == 0)
19155 			tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
19156 	} else if ((slot == 0) && (sendalot == 0) && tot_len_this_send) {
19157 		/*
19158 		 * Get our pacing rate, if an error
19159 		 * occurred in sending (ENOBUF) we would
19160 		 * hit the else if with slot preset. Other
19161 		 * errors return.
19162 		 */
19163 		slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, rsm, segsiz);
19164 	}
19165 	if (rsm &&
19166 	    (rsm->r_flags & RACK_HAS_SYN) == 0 &&
19167 	    rack->use_rack_rr) {
19168 		/* Its a retransmit and we use the rack cheat? */
19169 		if ((slot == 0) ||
19170 		    (rack->rc_always_pace == 0) ||
19171 		    (rack->r_rr_config == 1)) {
19172 			/*
19173 			 * We have no pacing set or we
19174 			 * are using old-style rack or
19175 			 * we are overridden to use the old 1ms pacing.
19176 			 */
19177 			slot = rack->r_ctl.rc_min_to;
19178 		}
19179 	}
19180 	/* We have sent clear the flag */
19181 	rack->r_ent_rec_ns = 0;
19182 	if (rack->r_must_retran) {
19183 		if (rsm) {
19184 			rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start);
19185 			if (SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) {
19186 				/*
19187 				 * We have retransmitted all.
19188 				 */
19189 				rack->r_must_retran = 0;
19190 				rack->r_ctl.rc_out_at_rto = 0;
19191 			}
19192 		} else if (SEQ_GEQ(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) {
19193 			/*
19194 			 * Sending new data will also kill
19195 			 * the loop.
19196 			 */
19197 			rack->r_must_retran = 0;
19198 			rack->r_ctl.rc_out_at_rto = 0;
19199 		}
19200 	}
19201 	rack->r_ctl.fsb.recwin = recwin;
19202 	if ((tp->t_flags & (TF_WASCRECOVERY|TF_WASFRECOVERY)) &&
19203 	    SEQ_GT(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) {
19204 		/*
19205 		 * We hit an RTO and now have past snd_max at the RTO
19206 		 * clear all the WAS flags.
19207 		 */
19208 		tp->t_flags &= ~(TF_WASCRECOVERY|TF_WASFRECOVERY);
19209 	}
19210 	if (slot) {
19211 		/* set the rack tcb into the slot N */
19212 		if ((error == 0) &&
19213 		    rack_use_rfo &&
19214 		    ((flags & (TH_SYN|TH_FIN)) == 0) &&
19215 		    (rsm == NULL) &&
19216 		    (tp->snd_nxt == tp->snd_max) &&
19217 		    (ipoptlen == 0) &&
19218 		    (tp->rcv_numsacks == 0) &&
19219 		    rack->r_fsb_inited &&
19220 		    TCPS_HAVEESTABLISHED(tp->t_state) &&
19221 		    (rack->r_must_retran == 0) &&
19222 		    ((tp->t_flags & TF_NEEDFIN) == 0) &&
19223 		    (len > 0) && (orig_len > 0) &&
19224 		    (orig_len > len) &&
19225 		    ((orig_len - len) >= segsiz) &&
19226 		    ((optlen == 0) ||
19227 		     ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
19228 			/* We can send at least one more MSS using our fsb */
19229 
19230 			rack->r_fast_output = 1;
19231 			rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
19232 			rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
19233 			rack->r_ctl.fsb.tcp_flags = flags;
19234 			rack->r_ctl.fsb.left_to_send = orig_len - len;
19235 			if (hw_tls)
19236 				rack->r_ctl.fsb.hw_tls = 1;
19237 			else
19238 				rack->r_ctl.fsb.hw_tls = 0;
19239 			KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(sb) - (tp->snd_max - tp->snd_una))),
19240 				("rack:%p left_to_send:%u sbavail:%u out:%u",
19241 				 rack, rack->r_ctl.fsb.left_to_send, sbavail(sb),
19242 				 (tp->snd_max - tp->snd_una)));
19243 			if (rack->r_ctl.fsb.left_to_send < segsiz)
19244 				rack->r_fast_output = 0;
19245 			else {
19246 				if (rack->r_ctl.fsb.left_to_send == (sbavail(sb) - (tp->snd_max - tp->snd_una)))
19247 					rack->r_ctl.fsb.rfo_apply_push = 1;
19248 				else
19249 					rack->r_ctl.fsb.rfo_apply_push = 0;
19250 			}
19251 		} else
19252 			rack->r_fast_output = 0;
19253 		rack_log_fsb(rack, tp, so, flags,
19254 			     ipoptlen, orig_len, len, error,
19255 			     (rsm == NULL), optlen, __LINE__, 2);
19256 	} else if (sendalot) {
19257 		int ret;
19258 
19259 		sack_rxmit = 0;
19260 		if ((error == 0) &&
19261 		    rack_use_rfo &&
19262 		    ((flags & (TH_SYN|TH_FIN)) == 0) &&
19263 		    (rsm == NULL) &&
19264 		    (ipoptlen == 0) &&
19265 		    (tp->rcv_numsacks == 0) &&
19266 		    (tp->snd_nxt == tp->snd_max) &&
19267 		    (rack->r_must_retran == 0) &&
19268 		    rack->r_fsb_inited &&
19269 		    TCPS_HAVEESTABLISHED(tp->t_state) &&
19270 		    ((tp->t_flags & TF_NEEDFIN) == 0) &&
19271 		    (len > 0) && (orig_len > 0) &&
19272 		    (orig_len > len) &&
19273 		    ((orig_len - len) >= segsiz) &&
19274 		    ((optlen == 0) ||
19275 		     ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
19276 			/* we can use fast_output for more */
19277 
19278 			rack->r_fast_output = 1;
19279 			rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
19280 			rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
19281 			rack->r_ctl.fsb.tcp_flags = flags;
19282 			rack->r_ctl.fsb.left_to_send = orig_len - len;
19283 			if (hw_tls)
19284 				rack->r_ctl.fsb.hw_tls = 1;
19285 			else
19286 				rack->r_ctl.fsb.hw_tls = 0;
19287 			KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(sb) - (tp->snd_max - tp->snd_una))),
19288 				("rack:%p left_to_send:%u sbavail:%u out:%u",
19289 				 rack, rack->r_ctl.fsb.left_to_send, sbavail(sb),
19290 				 (tp->snd_max - tp->snd_una)));
19291 			if (rack->r_ctl.fsb.left_to_send < segsiz) {
19292 				rack->r_fast_output = 0;
19293 			}
19294 			if (rack->r_fast_output) {
19295 				if (rack->r_ctl.fsb.left_to_send == (sbavail(sb) - (tp->snd_max - tp->snd_una)))
19296 					rack->r_ctl.fsb.rfo_apply_push = 1;
19297 				else
19298 					rack->r_ctl.fsb.rfo_apply_push = 0;
19299 				rack_log_fsb(rack, tp, so, flags,
19300 					     ipoptlen, orig_len, len, error,
19301 					     (rsm == NULL), optlen, __LINE__, 3);
19302 				error = 0;
19303 				ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error);
19304 				if (ret >= 0)
19305 					return (ret);
19306 			        else if (error)
19307 					goto nomore;
19308 
19309 			}
19310 		}
19311 		goto again;
19312 	}
19313 	/* Assure when we leave that snd_nxt will point to top */
19314 	if (SEQ_GT(tp->snd_max, tp->snd_nxt))
19315 		tp->snd_nxt = tp->snd_max;
19316 	rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, 0);
19317 #ifdef TCP_ACCOUNTING
19318 	crtsc = get_cyclecount() - ts_val;
19319 	if (tot_len_this_send) {
19320 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19321 			tp->tcp_cnt_counters[SND_OUT_DATA]++;
19322 		}
19323 		counter_u64_add(tcp_cnt_counters[SND_OUT_DATA], 1);
19324 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19325 			tp->tcp_proc_time[SND_OUT_DATA] += crtsc;
19326 		}
19327 		counter_u64_add(tcp_proc_time[SND_OUT_DATA], crtsc);
19328 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19329 			tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) /segsiz);
19330 		}
19331 		counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((tot_len_this_send + segsiz - 1) /segsiz));
19332 	} else {
19333 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19334 			tp->tcp_cnt_counters[SND_OUT_ACK]++;
19335 		}
19336 		counter_u64_add(tcp_cnt_counters[SND_OUT_ACK], 1);
19337 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19338 			tp->tcp_proc_time[SND_OUT_ACK] += crtsc;
19339 		}
19340 		counter_u64_add(tcp_proc_time[SND_OUT_ACK], crtsc);
19341 	}
19342 	sched_unpin();
19343 #endif
19344 	if (error == ENOBUFS)
19345 		error = 0;
19346 	return (error);
19347 }
19348 
19349 static void
19350 rack_update_seg(struct tcp_rack *rack)
19351 {
19352 	uint32_t orig_val;
19353 
19354 	orig_val = rack->r_ctl.rc_pace_max_segs;
19355 	rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
19356 	if (orig_val != rack->r_ctl.rc_pace_max_segs)
19357 		rack_log_pacing_delay_calc(rack, 0, 0, orig_val, 0, 0, 15, __LINE__, NULL, 0);
19358 }
19359 
19360 static void
19361 rack_mtu_change(struct tcpcb *tp)
19362 {
19363 	/*
19364 	 * The MSS may have changed
19365 	 */
19366 	struct tcp_rack *rack;
19367 	struct rack_sendmap *rsm;
19368 
19369 	rack = (struct tcp_rack *)tp->t_fb_ptr;
19370 	if (rack->r_ctl.rc_pace_min_segs != ctf_fixed_maxseg(tp)) {
19371 		/*
19372 		 * The MTU has changed we need to resend everything
19373 		 * since all we have sent is lost. We first fix
19374 		 * up the mtu though.
19375 		 */
19376 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
19377 		/* We treat this like a full retransmit timeout without the cwnd adjustment */
19378 		rack_remxt_tmr(tp);
19379 		rack->r_fast_output = 0;
19380 		rack->r_ctl.rc_out_at_rto = ctf_flight_size(tp,
19381 						rack->r_ctl.rc_sacked);
19382 		rack->r_ctl.rc_snd_max_at_rto = tp->snd_max;
19383 		rack->r_must_retran = 1;
19384 		/* Mark all inflight to needing to be rxt'd */
19385 		TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
19386 			rsm->r_flags |= RACK_MUST_RXT;
19387 		}
19388 	}
19389 	sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
19390 	/* We don't use snd_nxt to retransmit */
19391 	tp->snd_nxt = tp->snd_max;
19392 }
19393 
19394 static int
19395 rack_set_profile(struct tcp_rack *rack, int prof)
19396 {
19397 	int err = EINVAL;
19398 	if (prof == 1) {
19399 		/* pace_always=1 */
19400 		if (rack->rc_always_pace == 0) {
19401 			if (tcp_can_enable_pacing() == 0)
19402 				return (EBUSY);
19403 		}
19404 		rack->rc_always_pace = 1;
19405 		if (rack->use_fixed_rate || rack->gp_ready)
19406 			rack_set_cc_pacing(rack);
19407 		rack->rc_inp->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19408 		rack->rack_attempt_hdwr_pace = 0;
19409 		/* cmpack=1 */
19410 		if (rack_use_cmp_acks)
19411 			rack->r_use_cmp_ack = 1;
19412 		if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state) &&
19413 		    rack->r_use_cmp_ack)
19414 			rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
19415 		/* scwnd=1 */
19416 		rack->rack_enable_scwnd = 1;
19417 		/* dynamic=100 */
19418 		rack->rc_gp_dyn_mul = 1;
19419 		/* gp_inc_ca */
19420 		rack->r_ctl.rack_per_of_gp_ca = 100;
19421 		/* rrr_conf=3 */
19422 		rack->r_rr_config = 3;
19423 		/* npush=2 */
19424 		rack->r_ctl.rc_no_push_at_mrtt = 2;
19425 		/* fillcw=1 */
19426 		rack->rc_pace_to_cwnd = 1;
19427 		rack->rc_pace_fill_if_rttin_range = 0;
19428 		rack->rtt_limit_mul = 0;
19429 		/* noprr=1 */
19430 		rack->rack_no_prr = 1;
19431 		/* lscwnd=1 */
19432 		rack->r_limit_scw = 1;
19433 		/* gp_inc_rec */
19434 		rack->r_ctl.rack_per_of_gp_rec = 90;
19435 		err = 0;
19436 
19437 	} else if (prof == 3) {
19438 		/* Same as profile one execept fill_cw becomes 2 (less aggressive set) */
19439 		/* pace_always=1 */
19440 		if (rack->rc_always_pace == 0) {
19441 			if (tcp_can_enable_pacing() == 0)
19442 				return (EBUSY);
19443 		}
19444 		rack->rc_always_pace = 1;
19445 		if (rack->use_fixed_rate || rack->gp_ready)
19446 			rack_set_cc_pacing(rack);
19447 		rack->rc_inp->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19448 		rack->rack_attempt_hdwr_pace = 0;
19449 		/* cmpack=1 */
19450 		if (rack_use_cmp_acks)
19451 			rack->r_use_cmp_ack = 1;
19452 		if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state) &&
19453 		    rack->r_use_cmp_ack)
19454 			rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
19455 		/* scwnd=1 */
19456 		rack->rack_enable_scwnd = 1;
19457 		/* dynamic=100 */
19458 		rack->rc_gp_dyn_mul = 1;
19459 		/* gp_inc_ca */
19460 		rack->r_ctl.rack_per_of_gp_ca = 100;
19461 		/* rrr_conf=3 */
19462 		rack->r_rr_config = 3;
19463 		/* npush=2 */
19464 		rack->r_ctl.rc_no_push_at_mrtt = 2;
19465 		/* fillcw=2 */
19466 		rack->rc_pace_to_cwnd = 1;
19467 		rack->r_fill_less_agg = 1;
19468 		rack->rc_pace_fill_if_rttin_range = 0;
19469 		rack->rtt_limit_mul = 0;
19470 		/* noprr=1 */
19471 		rack->rack_no_prr = 1;
19472 		/* lscwnd=1 */
19473 		rack->r_limit_scw = 1;
19474 		/* gp_inc_rec */
19475 		rack->r_ctl.rack_per_of_gp_rec = 90;
19476 		err = 0;
19477 
19478 
19479 	} else if (prof == 2) {
19480 		/* cmpack=1 */
19481 		if (rack->rc_always_pace == 0) {
19482 			if (tcp_can_enable_pacing() == 0)
19483 				return (EBUSY);
19484 		}
19485 		rack->rc_always_pace = 1;
19486 		if (rack->use_fixed_rate || rack->gp_ready)
19487 			rack_set_cc_pacing(rack);
19488 		rack->r_use_cmp_ack = 1;
19489 		if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state))
19490 			rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
19491 		/* pace_always=1 */
19492 		rack->rc_inp->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19493 		/* scwnd=1 */
19494 		rack->rack_enable_scwnd = 1;
19495 		/* dynamic=100 */
19496 		rack->rc_gp_dyn_mul = 1;
19497 		rack->r_ctl.rack_per_of_gp_ca = 100;
19498 		/* rrr_conf=3 */
19499 		rack->r_rr_config = 3;
19500 		/* npush=2 */
19501 		rack->r_ctl.rc_no_push_at_mrtt = 2;
19502 		/* fillcw=1 */
19503 		rack->rc_pace_to_cwnd = 1;
19504 		rack->rc_pace_fill_if_rttin_range = 0;
19505 		rack->rtt_limit_mul = 0;
19506 		/* noprr=1 */
19507 		rack->rack_no_prr = 1;
19508 		/* lscwnd=0 */
19509 		rack->r_limit_scw = 0;
19510 		err = 0;
19511 	} else if (prof == 0) {
19512 		/* This changes things back to the default settings */
19513 		err = 0;
19514 		if (rack->rc_always_pace) {
19515 			tcp_decrement_paced_conn();
19516 			rack_undo_cc_pacing(rack);
19517 			rack->rc_always_pace = 0;
19518 		}
19519 		if (rack_pace_every_seg && tcp_can_enable_pacing()) {
19520 			rack->rc_always_pace = 1;
19521 			if (rack->use_fixed_rate || rack->gp_ready)
19522 				rack_set_cc_pacing(rack);
19523 		} else
19524 			rack->rc_always_pace = 0;
19525 		if (rack_dsack_std_based & 0x1) {
19526 			/* Basically this means all rack timers are at least (srtt + 1/4 srtt) */
19527 			rack->rc_rack_tmr_std_based = 1;
19528 		}
19529 		if (rack_dsack_std_based & 0x2) {
19530 			/* Basically this means  rack timers are extended based on dsack by up to (2 * srtt) */
19531 			rack->rc_rack_use_dsack = 1;
19532 		}
19533 		if (rack_use_cmp_acks)
19534 			rack->r_use_cmp_ack = 1;
19535 		else
19536 			rack->r_use_cmp_ack = 0;
19537 		if (rack_disable_prr)
19538 			rack->rack_no_prr = 1;
19539 		else
19540 			rack->rack_no_prr = 0;
19541 		if (rack_gp_no_rec_chg)
19542 			rack->rc_gp_no_rec_chg = 1;
19543 		else
19544 			rack->rc_gp_no_rec_chg = 0;
19545 		if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack) {
19546 			rack->r_mbuf_queue = 1;
19547 			if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state))
19548 				rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
19549 			rack->rc_inp->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19550 		} else {
19551 			rack->r_mbuf_queue = 0;
19552 			rack->rc_inp->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
19553 		}
19554 		if (rack_enable_shared_cwnd)
19555 			rack->rack_enable_scwnd = 1;
19556 		else
19557 			rack->rack_enable_scwnd = 0;
19558 		if (rack_do_dyn_mul) {
19559 			/* When dynamic adjustment is on CA needs to start at 100% */
19560 			rack->rc_gp_dyn_mul = 1;
19561 			if (rack_do_dyn_mul >= 100)
19562 				rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul;
19563 		} else {
19564 			rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca;
19565 			rack->rc_gp_dyn_mul = 0;
19566 		}
19567 		rack->r_rr_config = 0;
19568 		rack->r_ctl.rc_no_push_at_mrtt = 0;
19569 		rack->rc_pace_to_cwnd = 0;
19570 		rack->rc_pace_fill_if_rttin_range = 0;
19571 		rack->rtt_limit_mul = 0;
19572 
19573 		if (rack_enable_hw_pacing)
19574 			rack->rack_hdw_pace_ena = 1;
19575 		else
19576 			rack->rack_hdw_pace_ena = 0;
19577 		if (rack_disable_prr)
19578 			rack->rack_no_prr = 1;
19579 		else
19580 			rack->rack_no_prr = 0;
19581 		if (rack_limits_scwnd)
19582 			rack->r_limit_scw  = 1;
19583 		else
19584 			rack->r_limit_scw  = 0;
19585 		err = 0;
19586 	}
19587 	return (err);
19588 }
19589 
19590 static int
19591 rack_add_deferred_option(struct tcp_rack *rack, int sopt_name, uint64_t loptval)
19592 {
19593 	struct deferred_opt_list *dol;
19594 
19595 	dol = malloc(sizeof(struct deferred_opt_list),
19596 		     M_TCPFSB, M_NOWAIT|M_ZERO);
19597 	if (dol == NULL) {
19598 		/*
19599 		 * No space yikes -- fail out..
19600 		 */
19601 		return (0);
19602 	}
19603 	dol->optname = sopt_name;
19604 	dol->optval = loptval;
19605 	TAILQ_INSERT_TAIL(&rack->r_ctl.opt_list, dol, next);
19606 	return (1);
19607 }
19608 
19609 static int
19610 rack_process_option(struct tcpcb *tp, struct tcp_rack *rack, int sopt_name,
19611 		    uint32_t optval, uint64_t loptval)
19612 {
19613 	struct epoch_tracker et;
19614 	struct sockopt sopt;
19615 	struct cc_newreno_opts opt;
19616 	uint64_t val;
19617 	int error = 0;
19618 	uint16_t ca, ss;
19619 
19620 	switch (sopt_name) {
19621 
19622 	case TCP_RACK_DSACK_OPT:
19623 		RACK_OPTS_INC(tcp_rack_dsack_opt);
19624 		if (optval & 0x1) {
19625 			rack->rc_rack_tmr_std_based = 1;
19626 		} else {
19627 			rack->rc_rack_tmr_std_based = 0;
19628 		}
19629 		if (optval & 0x2) {
19630 			rack->rc_rack_use_dsack = 1;
19631 		} else {
19632 			rack->rc_rack_use_dsack = 0;
19633 		}
19634 		rack_log_dsack_event(rack, 5, __LINE__, 0, 0);
19635 		break;
19636 	case TCP_RACK_PACING_BETA:
19637 		RACK_OPTS_INC(tcp_rack_beta);
19638 		if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0) {
19639 			/* This only works for newreno. */
19640 			error = EINVAL;
19641 			break;
19642 		}
19643 		if (rack->rc_pacing_cc_set) {
19644 			/*
19645 			 * Set them into the real CC module
19646 			 * whats in the rack pcb is the old values
19647 			 * to be used on restoral/
19648 			 */
19649 			sopt.sopt_dir = SOPT_SET;
19650 			opt.name = CC_NEWRENO_BETA;
19651 			opt.val = optval;
19652 			if (CC_ALGO(tp)->ctl_output != NULL)
19653 				error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
19654 			else {
19655 				error = ENOENT;
19656 				break;
19657 			}
19658 		} else {
19659 			/*
19660 			 * Not pacing yet so set it into our local
19661 			 * rack pcb storage.
19662 			 */
19663 			rack->r_ctl.rc_saved_beta.beta = optval;
19664 		}
19665 		break;
19666 	case TCP_RACK_TIMER_SLOP:
19667 		RACK_OPTS_INC(tcp_rack_timer_slop);
19668 		rack->r_ctl.timer_slop = optval;
19669 		if (rack->rc_tp->t_srtt) {
19670 			/*
19671 			 * If we have an SRTT lets update t_rxtcur
19672 			 * to have the new slop.
19673 			 */
19674 			RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
19675 					   rack_rto_min, rack_rto_max,
19676 					   rack->r_ctl.timer_slop);
19677 		}
19678 		break;
19679 	case TCP_RACK_PACING_BETA_ECN:
19680 		RACK_OPTS_INC(tcp_rack_beta_ecn);
19681 		if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0) {
19682 			/* This only works for newreno. */
19683 			error = EINVAL;
19684 			break;
19685 		}
19686 		if (rack->rc_pacing_cc_set) {
19687 			/*
19688 			 * Set them into the real CC module
19689 			 * whats in the rack pcb is the old values
19690 			 * to be used on restoral/
19691 			 */
19692 			sopt.sopt_dir = SOPT_SET;
19693 			opt.name = CC_NEWRENO_BETA_ECN;
19694 			opt.val = optval;
19695 			if (CC_ALGO(tp)->ctl_output != NULL)
19696 				error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
19697 			else
19698 				error = ENOENT;
19699 		} else {
19700 			/*
19701 			 * Not pacing yet so set it into our local
19702 			 * rack pcb storage.
19703 			 */
19704 			rack->r_ctl.rc_saved_beta.beta_ecn = optval;
19705 			rack->r_ctl.rc_saved_beta.newreno_flags = CC_NEWRENO_BETA_ECN_ENABLED;
19706 		}
19707 		break;
19708 	case TCP_DEFER_OPTIONS:
19709 		RACK_OPTS_INC(tcp_defer_opt);
19710 		if (optval) {
19711 			if (rack->gp_ready) {
19712 				/* Too late */
19713 				error = EINVAL;
19714 				break;
19715 			}
19716 			rack->defer_options = 1;
19717 		} else
19718 			rack->defer_options = 0;
19719 		break;
19720 	case TCP_RACK_MEASURE_CNT:
19721 		RACK_OPTS_INC(tcp_rack_measure_cnt);
19722 		if (optval && (optval <= 0xff)) {
19723 			rack->r_ctl.req_measurements = optval;
19724 		} else
19725 			error = EINVAL;
19726 		break;
19727 	case TCP_REC_ABC_VAL:
19728 		RACK_OPTS_INC(tcp_rec_abc_val);
19729 		if (optval > 0)
19730 			rack->r_use_labc_for_rec = 1;
19731 		else
19732 			rack->r_use_labc_for_rec = 0;
19733 		break;
19734 	case TCP_RACK_ABC_VAL:
19735 		RACK_OPTS_INC(tcp_rack_abc_val);
19736 		if ((optval > 0) && (optval < 255))
19737 			rack->rc_labc = optval;
19738 		else
19739 			error = EINVAL;
19740 		break;
19741 	case TCP_HDWR_UP_ONLY:
19742 		RACK_OPTS_INC(tcp_pacing_up_only);
19743 		if (optval)
19744 			rack->r_up_only = 1;
19745 		else
19746 			rack->r_up_only = 0;
19747 		break;
19748 	case TCP_PACING_RATE_CAP:
19749 		RACK_OPTS_INC(tcp_pacing_rate_cap);
19750 		rack->r_ctl.bw_rate_cap = loptval;
19751 		break;
19752 	case TCP_RACK_PROFILE:
19753 		RACK_OPTS_INC(tcp_profile);
19754 		error = rack_set_profile(rack, optval);
19755 		break;
19756 	case TCP_USE_CMP_ACKS:
19757 		RACK_OPTS_INC(tcp_use_cmp_acks);
19758 		if ((optval == 0) && (rack->rc_inp->inp_flags2 & INP_MBUF_ACKCMP)) {
19759 			/* You can't turn it off once its on! */
19760 			error = EINVAL;
19761 		} else if ((optval == 1) && (rack->r_use_cmp_ack == 0)) {
19762 			rack->r_use_cmp_ack = 1;
19763 			rack->r_mbuf_queue = 1;
19764 			tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19765 		}
19766 		if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
19767 			rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
19768 		break;
19769 	case TCP_SHARED_CWND_TIME_LIMIT:
19770 		RACK_OPTS_INC(tcp_lscwnd);
19771 		if (optval)
19772 			rack->r_limit_scw = 1;
19773 		else
19774 			rack->r_limit_scw = 0;
19775 		break;
19776  	case TCP_RACK_PACE_TO_FILL:
19777 		RACK_OPTS_INC(tcp_fillcw);
19778 		if (optval == 0)
19779 			rack->rc_pace_to_cwnd = 0;
19780 		else {
19781 			rack->rc_pace_to_cwnd = 1;
19782 			if (optval > 1)
19783 				rack->r_fill_less_agg = 1;
19784 		}
19785 		if ((optval >= rack_gp_rtt_maxmul) &&
19786 		    rack_gp_rtt_maxmul &&
19787 		    (optval < 0xf)) {
19788 			rack->rc_pace_fill_if_rttin_range = 1;
19789 			rack->rtt_limit_mul = optval;
19790 		} else {
19791 			rack->rc_pace_fill_if_rttin_range = 0;
19792 			rack->rtt_limit_mul = 0;
19793 		}
19794 		break;
19795 	case TCP_RACK_NO_PUSH_AT_MAX:
19796 		RACK_OPTS_INC(tcp_npush);
19797 		if (optval == 0)
19798 			rack->r_ctl.rc_no_push_at_mrtt = 0;
19799 		else if (optval < 0xff)
19800 			rack->r_ctl.rc_no_push_at_mrtt = optval;
19801 		else
19802 			error = EINVAL;
19803 		break;
19804 	case TCP_SHARED_CWND_ENABLE:
19805 		RACK_OPTS_INC(tcp_rack_scwnd);
19806 		if (optval == 0)
19807 			rack->rack_enable_scwnd = 0;
19808 		else
19809 			rack->rack_enable_scwnd = 1;
19810 		break;
19811 	case TCP_RACK_MBUF_QUEUE:
19812 		/* Now do we use the LRO mbuf-queue feature */
19813 		RACK_OPTS_INC(tcp_rack_mbufq);
19814 		if (optval || rack->r_use_cmp_ack)
19815 			rack->r_mbuf_queue = 1;
19816 		else
19817 			rack->r_mbuf_queue = 0;
19818 		if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
19819 			tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19820 		else
19821 			tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
19822 		break;
19823 	case TCP_RACK_NONRXT_CFG_RATE:
19824 		RACK_OPTS_INC(tcp_rack_cfg_rate);
19825 		if (optval == 0)
19826 			rack->rack_rec_nonrxt_use_cr = 0;
19827 		else
19828 			rack->rack_rec_nonrxt_use_cr = 1;
19829 		break;
19830 	case TCP_NO_PRR:
19831 		RACK_OPTS_INC(tcp_rack_noprr);
19832 		if (optval == 0)
19833 			rack->rack_no_prr = 0;
19834 		else if (optval == 1)
19835 			rack->rack_no_prr = 1;
19836 		else if (optval == 2)
19837 			rack->no_prr_addback = 1;
19838 		else
19839 			error = EINVAL;
19840 		break;
19841 	case TCP_TIMELY_DYN_ADJ:
19842 		RACK_OPTS_INC(tcp_timely_dyn);
19843 		if (optval == 0)
19844 			rack->rc_gp_dyn_mul = 0;
19845 		else {
19846 			rack->rc_gp_dyn_mul = 1;
19847 			if (optval >= 100) {
19848 				/*
19849 				 * If the user sets something 100 or more
19850 				 * its the gp_ca value.
19851 				 */
19852 				rack->r_ctl.rack_per_of_gp_ca  = optval;
19853 			}
19854 		}
19855 		break;
19856 	case TCP_RACK_DO_DETECTION:
19857 		RACK_OPTS_INC(tcp_rack_do_detection);
19858 		if (optval == 0)
19859 			rack->do_detection = 0;
19860 		else
19861 			rack->do_detection = 1;
19862 		break;
19863 	case TCP_RACK_TLP_USE:
19864 		if ((optval < TLP_USE_ID) || (optval > TLP_USE_TWO_TWO)) {
19865 			error = EINVAL;
19866 			break;
19867 		}
19868 		RACK_OPTS_INC(tcp_tlp_use);
19869 		rack->rack_tlp_threshold_use = optval;
19870 		break;
19871 	case TCP_RACK_TLP_REDUCE:
19872 		/* RACK TLP cwnd reduction (bool) */
19873 		RACK_OPTS_INC(tcp_rack_tlp_reduce);
19874 		rack->r_ctl.rc_tlp_cwnd_reduce = optval;
19875 		break;
19876 	/*  Pacing related ones */
19877 	case TCP_RACK_PACE_ALWAYS:
19878 		/*
19879 		 * zero is old rack method, 1 is new
19880 		 * method using a pacing rate.
19881 		 */
19882 		RACK_OPTS_INC(tcp_rack_pace_always);
19883 		if (optval > 0) {
19884 			if (rack->rc_always_pace) {
19885 				error = EALREADY;
19886 				break;
19887 			} else if (tcp_can_enable_pacing()) {
19888 				rack->rc_always_pace = 1;
19889 				if (rack->use_fixed_rate || rack->gp_ready)
19890 					rack_set_cc_pacing(rack);
19891 			}
19892 			else {
19893 				error = ENOSPC;
19894 				break;
19895 			}
19896 		} else {
19897 			if (rack->rc_always_pace) {
19898 				tcp_decrement_paced_conn();
19899 				rack->rc_always_pace = 0;
19900 				rack_undo_cc_pacing(rack);
19901 			}
19902 		}
19903 		if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
19904 			tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19905 		else
19906 			tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
19907 		/* A rate may be set irate or other, if so set seg size */
19908 		rack_update_seg(rack);
19909 		break;
19910 	case TCP_BBR_RACK_INIT_RATE:
19911 		RACK_OPTS_INC(tcp_initial_rate);
19912 		val = optval;
19913 		/* Change from kbits per second to bytes per second */
19914 		val *= 1000;
19915 		val /= 8;
19916 		rack->r_ctl.init_rate = val;
19917 		if (rack->rc_init_win != rack_default_init_window) {
19918 			uint32_t win, snt;
19919 
19920 			/*
19921 			 * Options don't always get applied
19922 			 * in the order you think. So in order
19923 			 * to assure we update a cwnd we need
19924 			 * to check and see if we are still
19925 			 * where we should raise the cwnd.
19926 			 */
19927 			win = rc_init_window(rack);
19928 			if (SEQ_GT(tp->snd_max, tp->iss))
19929 				snt = tp->snd_max - tp->iss;
19930 			else
19931 				snt = 0;
19932 			if ((snt < win) &&
19933 			    (tp->snd_cwnd < win))
19934 				tp->snd_cwnd = win;
19935 		}
19936 		if (rack->rc_always_pace)
19937 			rack_update_seg(rack);
19938 		break;
19939 	case TCP_BBR_IWINTSO:
19940 		RACK_OPTS_INC(tcp_initial_win);
19941 		if (optval && (optval <= 0xff)) {
19942 			uint32_t win, snt;
19943 
19944 			rack->rc_init_win = optval;
19945 			win = rc_init_window(rack);
19946 			if (SEQ_GT(tp->snd_max, tp->iss))
19947 				snt = tp->snd_max - tp->iss;
19948 			else
19949 				snt = 0;
19950 			if ((snt < win) &&
19951 			    (tp->t_srtt |
19952 #ifdef NETFLIX_PEAKRATE
19953 			     tp->t_maxpeakrate |
19954 #endif
19955 			     rack->r_ctl.init_rate)) {
19956 				/*
19957 				 * We are not past the initial window
19958 				 * and we have some bases for pacing,
19959 				 * so we need to possibly adjust up
19960 				 * the cwnd. Note even if we don't set
19961 				 * the cwnd, its still ok to raise the rc_init_win
19962 				 * which can be used coming out of idle when we
19963 				 * would have a rate.
19964 				 */
19965 				if (tp->snd_cwnd < win)
19966 					tp->snd_cwnd = win;
19967 			}
19968 			if (rack->rc_always_pace)
19969 				rack_update_seg(rack);
19970 		} else
19971 			error = EINVAL;
19972 		break;
19973 	case TCP_RACK_FORCE_MSEG:
19974 		RACK_OPTS_INC(tcp_rack_force_max_seg);
19975 		if (optval)
19976 			rack->rc_force_max_seg = 1;
19977 		else
19978 			rack->rc_force_max_seg = 0;
19979 		break;
19980 	case TCP_RACK_PACE_MAX_SEG:
19981 		/* Max segments size in a pace in bytes */
19982 		RACK_OPTS_INC(tcp_rack_max_seg);
19983 		rack->rc_user_set_max_segs = optval;
19984 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
19985 		break;
19986 	case TCP_RACK_PACE_RATE_REC:
19987 		/* Set the fixed pacing rate in Bytes per second ca */
19988 		RACK_OPTS_INC(tcp_rack_pace_rate_rec);
19989 		rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
19990 		if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0)
19991 			rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
19992 		if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0)
19993 			rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
19994 		rack->use_fixed_rate = 1;
19995 		if (rack->rc_always_pace)
19996 			rack_set_cc_pacing(rack);
19997 		rack_log_pacing_delay_calc(rack,
19998 					   rack->r_ctl.rc_fixed_pacing_rate_ss,
19999 					   rack->r_ctl.rc_fixed_pacing_rate_ca,
20000 					   rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
20001 					   __LINE__, NULL,0);
20002 		break;
20003 
20004 	case TCP_RACK_PACE_RATE_SS:
20005 		/* Set the fixed pacing rate in Bytes per second ca */
20006 		RACK_OPTS_INC(tcp_rack_pace_rate_ss);
20007 		rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
20008 		if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0)
20009 			rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
20010 		if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0)
20011 			rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
20012 		rack->use_fixed_rate = 1;
20013 		if (rack->rc_always_pace)
20014 			rack_set_cc_pacing(rack);
20015 		rack_log_pacing_delay_calc(rack,
20016 					   rack->r_ctl.rc_fixed_pacing_rate_ss,
20017 					   rack->r_ctl.rc_fixed_pacing_rate_ca,
20018 					   rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
20019 					   __LINE__, NULL, 0);
20020 		break;
20021 
20022 	case TCP_RACK_PACE_RATE_CA:
20023 		/* Set the fixed pacing rate in Bytes per second ca */
20024 		RACK_OPTS_INC(tcp_rack_pace_rate_ca);
20025 		rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
20026 		if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0)
20027 			rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
20028 		if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0)
20029 			rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
20030 		rack->use_fixed_rate = 1;
20031 		if (rack->rc_always_pace)
20032 			rack_set_cc_pacing(rack);
20033 		rack_log_pacing_delay_calc(rack,
20034 					   rack->r_ctl.rc_fixed_pacing_rate_ss,
20035 					   rack->r_ctl.rc_fixed_pacing_rate_ca,
20036 					   rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
20037 					   __LINE__, NULL, 0);
20038 		break;
20039 	case TCP_RACK_GP_INCREASE_REC:
20040 		RACK_OPTS_INC(tcp_gp_inc_rec);
20041 		rack->r_ctl.rack_per_of_gp_rec = optval;
20042 		rack_log_pacing_delay_calc(rack,
20043 					   rack->r_ctl.rack_per_of_gp_ss,
20044 					   rack->r_ctl.rack_per_of_gp_ca,
20045 					   rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
20046 					   __LINE__, NULL, 0);
20047 		break;
20048 	case TCP_RACK_GP_INCREASE_CA:
20049 		RACK_OPTS_INC(tcp_gp_inc_ca);
20050 		ca = optval;
20051 		if (ca < 100) {
20052 			/*
20053 			 * We don't allow any reduction
20054 			 * over the GP b/w.
20055 			 */
20056 			error = EINVAL;
20057 			break;
20058 		}
20059 		rack->r_ctl.rack_per_of_gp_ca = ca;
20060 		rack_log_pacing_delay_calc(rack,
20061 					   rack->r_ctl.rack_per_of_gp_ss,
20062 					   rack->r_ctl.rack_per_of_gp_ca,
20063 					   rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
20064 					   __LINE__, NULL, 0);
20065 		break;
20066 	case TCP_RACK_GP_INCREASE_SS:
20067 		RACK_OPTS_INC(tcp_gp_inc_ss);
20068 		ss = optval;
20069 		if (ss < 100) {
20070 			/*
20071 			 * We don't allow any reduction
20072 			 * over the GP b/w.
20073 			 */
20074 			error = EINVAL;
20075 			break;
20076 		}
20077 		rack->r_ctl.rack_per_of_gp_ss = ss;
20078 		rack_log_pacing_delay_calc(rack,
20079 					   rack->r_ctl.rack_per_of_gp_ss,
20080 					   rack->r_ctl.rack_per_of_gp_ca,
20081 					   rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
20082 					   __LINE__, NULL, 0);
20083 		break;
20084 	case TCP_RACK_RR_CONF:
20085 		RACK_OPTS_INC(tcp_rack_rrr_no_conf_rate);
20086 		if (optval && optval <= 3)
20087 			rack->r_rr_config = optval;
20088 		else
20089 			rack->r_rr_config = 0;
20090 		break;
20091 	case TCP_HDWR_RATE_CAP:
20092 		RACK_OPTS_INC(tcp_hdwr_rate_cap);
20093 		if (optval) {
20094 			if (rack->r_rack_hw_rate_caps == 0)
20095 				rack->r_rack_hw_rate_caps = 1;
20096 			else
20097 				error = EALREADY;
20098 		} else {
20099 			rack->r_rack_hw_rate_caps = 0;
20100 		}
20101 		break;
20102 	case TCP_BBR_HDWR_PACE:
20103 		RACK_OPTS_INC(tcp_hdwr_pacing);
20104 		if (optval){
20105 			if (rack->rack_hdrw_pacing == 0) {
20106 				rack->rack_hdw_pace_ena = 1;
20107 				rack->rack_attempt_hdwr_pace = 0;
20108 			} else
20109 				error = EALREADY;
20110 		} else {
20111 			rack->rack_hdw_pace_ena = 0;
20112 #ifdef RATELIMIT
20113 			if (rack->r_ctl.crte != NULL) {
20114 				rack->rack_hdrw_pacing = 0;
20115 				rack->rack_attempt_hdwr_pace = 0;
20116 				tcp_rel_pacing_rate(rack->r_ctl.crte, tp);
20117 				rack->r_ctl.crte = NULL;
20118 			}
20119 #endif
20120 		}
20121 		break;
20122 	/*  End Pacing related ones */
20123 	case TCP_RACK_PRR_SENDALOT:
20124 		/* Allow PRR to send more than one seg */
20125 		RACK_OPTS_INC(tcp_rack_prr_sendalot);
20126 		rack->r_ctl.rc_prr_sendalot = optval;
20127 		break;
20128 	case TCP_RACK_MIN_TO:
20129 		/* Minimum time between rack t-o's in ms */
20130 		RACK_OPTS_INC(tcp_rack_min_to);
20131 		rack->r_ctl.rc_min_to = optval;
20132 		break;
20133 	case TCP_RACK_EARLY_SEG:
20134 		/* If early recovery max segments */
20135 		RACK_OPTS_INC(tcp_rack_early_seg);
20136 		rack->r_ctl.rc_early_recovery_segs = optval;
20137 		break;
20138 	case TCP_RACK_ENABLE_HYSTART:
20139 	{
20140 		if (optval) {
20141 			tp->ccv->flags |= CCF_HYSTART_ALLOWED;
20142 			if (rack_do_hystart > RACK_HYSTART_ON)
20143 				tp->ccv->flags |= CCF_HYSTART_CAN_SH_CWND;
20144 			if (rack_do_hystart > RACK_HYSTART_ON_W_SC)
20145 				tp->ccv->flags |= CCF_HYSTART_CONS_SSTH;
20146 		} else {
20147 			tp->ccv->flags &= ~(CCF_HYSTART_ALLOWED|CCF_HYSTART_CAN_SH_CWND|CCF_HYSTART_CONS_SSTH);
20148 		}
20149 	}
20150 	break;
20151 	case TCP_RACK_REORD_THRESH:
20152 		/* RACK reorder threshold (shift amount) */
20153 		RACK_OPTS_INC(tcp_rack_reord_thresh);
20154 		if ((optval > 0) && (optval < 31))
20155 			rack->r_ctl.rc_reorder_shift = optval;
20156 		else
20157 			error = EINVAL;
20158 		break;
20159 	case TCP_RACK_REORD_FADE:
20160 		/* Does reordering fade after ms time */
20161 		RACK_OPTS_INC(tcp_rack_reord_fade);
20162 		rack->r_ctl.rc_reorder_fade = optval;
20163 		break;
20164 	case TCP_RACK_TLP_THRESH:
20165 		/* RACK TLP theshold i.e. srtt+(srtt/N) */
20166 		RACK_OPTS_INC(tcp_rack_tlp_thresh);
20167 		if (optval)
20168 			rack->r_ctl.rc_tlp_threshold = optval;
20169 		else
20170 			error = EINVAL;
20171 		break;
20172 	case TCP_BBR_USE_RACK_RR:
20173 		RACK_OPTS_INC(tcp_rack_rr);
20174 		if (optval)
20175 			rack->use_rack_rr = 1;
20176 		else
20177 			rack->use_rack_rr = 0;
20178 		break;
20179 	case TCP_FAST_RSM_HACK:
20180 		RACK_OPTS_INC(tcp_rack_fastrsm_hack);
20181 		if (optval)
20182 			rack->fast_rsm_hack = 1;
20183 		else
20184 			rack->fast_rsm_hack = 0;
20185 		break;
20186 	case TCP_RACK_PKT_DELAY:
20187 		/* RACK added ms i.e. rack-rtt + reord + N */
20188 		RACK_OPTS_INC(tcp_rack_pkt_delay);
20189 		rack->r_ctl.rc_pkt_delay = optval;
20190 		break;
20191 	case TCP_DELACK:
20192 		RACK_OPTS_INC(tcp_rack_delayed_ack);
20193 		if (optval == 0)
20194 			tp->t_delayed_ack = 0;
20195 		else
20196 			tp->t_delayed_ack = 1;
20197 		if (tp->t_flags & TF_DELACK) {
20198 			tp->t_flags &= ~TF_DELACK;
20199 			tp->t_flags |= TF_ACKNOW;
20200 			NET_EPOCH_ENTER(et);
20201 			rack_output(tp);
20202 			NET_EPOCH_EXIT(et);
20203 		}
20204 		break;
20205 
20206 	case TCP_BBR_RACK_RTT_USE:
20207 		RACK_OPTS_INC(tcp_rack_rtt_use);
20208 		if ((optval != USE_RTT_HIGH) &&
20209 		    (optval != USE_RTT_LOW) &&
20210 		    (optval != USE_RTT_AVG))
20211 			error = EINVAL;
20212 		else
20213 			rack->r_ctl.rc_rate_sample_method = optval;
20214 		break;
20215 	case TCP_DATA_AFTER_CLOSE:
20216 		RACK_OPTS_INC(tcp_data_after_close);
20217 		if (optval)
20218 			rack->rc_allow_data_af_clo = 1;
20219 		else
20220 			rack->rc_allow_data_af_clo = 0;
20221 		break;
20222 	default:
20223 		break;
20224 	}
20225 #ifdef NETFLIX_STATS
20226 	tcp_log_socket_option(tp, sopt_name, optval, error);
20227 #endif
20228 	return (error);
20229 }
20230 
20231 
20232 static void
20233 rack_apply_deferred_options(struct tcp_rack *rack)
20234 {
20235 	struct deferred_opt_list *dol, *sdol;
20236 	uint32_t s_optval;
20237 
20238 	TAILQ_FOREACH_SAFE(dol, &rack->r_ctl.opt_list, next, sdol) {
20239 		TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next);
20240 		/* Disadvantage of deferal is you loose the error return */
20241 		s_optval = (uint32_t)dol->optval;
20242 		(void)rack_process_option(rack->rc_tp, rack, dol->optname, s_optval, dol->optval);
20243 		free(dol, M_TCPDO);
20244 	}
20245 }
20246 
20247 static void
20248 rack_hw_tls_change(struct tcpcb *tp, int chg)
20249 {
20250 	/*
20251 	 * HW tls state has changed.. fix all
20252 	 * rsm's in flight.
20253 	 */
20254 	struct tcp_rack *rack;
20255 	struct rack_sendmap *rsm;
20256 
20257 	rack = (struct tcp_rack *)tp->t_fb_ptr;
20258 	RB_FOREACH(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
20259 		if (chg)
20260 			rsm->r_hw_tls = 1;
20261 		else
20262 			rsm->r_hw_tls = 0;
20263 	}
20264 	if (chg)
20265 		rack->r_ctl.fsb.hw_tls = 1;
20266 	else
20267 		rack->r_ctl.fsb.hw_tls = 0;
20268 }
20269 
20270 static int
20271 rack_pru_options(struct tcpcb *tp, int flags)
20272 {
20273 	if (flags & PRUS_OOB)
20274 		return (EOPNOTSUPP);
20275 	return (0);
20276 }
20277 
20278 static struct tcp_function_block __tcp_rack = {
20279 	.tfb_tcp_block_name = __XSTRING(STACKNAME),
20280 	.tfb_tcp_output = rack_output,
20281 	.tfb_do_queued_segments = ctf_do_queued_segments,
20282 	.tfb_do_segment_nounlock = rack_do_segment_nounlock,
20283 	.tfb_tcp_do_segment = rack_do_segment,
20284 	.tfb_tcp_ctloutput = rack_ctloutput,
20285 	.tfb_tcp_fb_init = rack_init,
20286 	.tfb_tcp_fb_fini = rack_fini,
20287 	.tfb_tcp_timer_stop_all = rack_stopall,
20288 	.tfb_tcp_timer_activate = rack_timer_activate,
20289 	.tfb_tcp_timer_active = rack_timer_active,
20290 	.tfb_tcp_timer_stop = rack_timer_stop,
20291 	.tfb_tcp_rexmit_tmr = rack_remxt_tmr,
20292 	.tfb_tcp_handoff_ok = rack_handoff_ok,
20293 	.tfb_tcp_mtu_chg = rack_mtu_change,
20294 	.tfb_pru_options = rack_pru_options,
20295 	.tfb_hwtls_change = rack_hw_tls_change,
20296 	.tfb_flags = TCP_FUNC_OUTPUT_CANDROP,
20297 };
20298 
20299 /*
20300  * rack_ctloutput() must drop the inpcb lock before performing copyin on
20301  * socket option arguments.  When it re-acquires the lock after the copy, it
20302  * has to revalidate that the connection is still valid for the socket
20303  * option.
20304  */
20305 static int
20306 rack_set_sockopt(struct inpcb *inp, struct sockopt *sopt)
20307 {
20308 #ifdef INET6
20309 	struct ip6_hdr *ip6;
20310 #endif
20311 #ifdef INET
20312 	struct ip *ip;
20313 #endif
20314 	struct tcpcb *tp;
20315 	struct tcp_rack *rack;
20316 	uint64_t loptval;
20317 	int32_t error = 0, optval;
20318 
20319 	tp = intotcpcb(inp);
20320 	rack = (struct tcp_rack *)tp->t_fb_ptr;
20321 	if (rack == NULL) {
20322 		INP_WUNLOCK(inp);
20323 		return (EINVAL);
20324 	}
20325 #ifdef INET6
20326 	ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
20327 #endif
20328 #ifdef INET
20329 	ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
20330 #endif
20331 
20332 	switch (sopt->sopt_level) {
20333 #ifdef INET6
20334 	case IPPROTO_IPV6:
20335 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
20336 		switch (sopt->sopt_name) {
20337 		case IPV6_USE_MIN_MTU:
20338 			tcp6_use_min_mtu(tp);
20339 			break;
20340 		case IPV6_TCLASS:
20341 			/*
20342 			 * The DSCP codepoint has changed, update the fsb.
20343 			 */
20344 			ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
20345 			    (rack->rc_inp->inp_flow & IPV6_FLOWINFO_MASK);
20346 			break;
20347 		}
20348 		INP_WUNLOCK(inp);
20349 		return (0);
20350 #endif
20351 #ifdef INET
20352 	case IPPROTO_IP:
20353 		switch (sopt->sopt_name) {
20354 		case IP_TOS:
20355 			/*
20356 			 * The DSCP codepoint has changed, update the fsb.
20357 			 */
20358 			ip->ip_tos = rack->rc_inp->inp_ip_tos;
20359 			break;
20360 		case IP_TTL:
20361 			/*
20362 			 * The TTL has changed, update the fsb.
20363 			 */
20364 			ip->ip_ttl = rack->rc_inp->inp_ip_ttl;
20365 			break;
20366 		}
20367 		INP_WUNLOCK(inp);
20368 		return (0);
20369 #endif
20370 	}
20371 
20372 	switch (sopt->sopt_name) {
20373 	case TCP_RACK_TLP_REDUCE:		/*  URL:tlp_reduce */
20374 	/*  Pacing related ones */
20375 	case TCP_RACK_PACE_ALWAYS:		/*  URL:pace_always */
20376 	case TCP_BBR_RACK_INIT_RATE:		/*  URL:irate */
20377 	case TCP_BBR_IWINTSO:			/*  URL:tso_iwin */
20378 	case TCP_RACK_PACE_MAX_SEG:		/*  URL:pace_max_seg */
20379 	case TCP_RACK_FORCE_MSEG:		/*  URL:force_max_seg */
20380 	case TCP_RACK_PACE_RATE_CA:		/*  URL:pr_ca */
20381 	case TCP_RACK_PACE_RATE_SS:		/*  URL:pr_ss*/
20382 	case TCP_RACK_PACE_RATE_REC:		/*  URL:pr_rec */
20383 	case TCP_RACK_GP_INCREASE_CA:		/*  URL:gp_inc_ca */
20384 	case TCP_RACK_GP_INCREASE_SS:		/*  URL:gp_inc_ss */
20385 	case TCP_RACK_GP_INCREASE_REC:		/*  URL:gp_inc_rec */
20386 	case TCP_RACK_RR_CONF:			/*  URL:rrr_conf */
20387 	case TCP_BBR_HDWR_PACE:			/*  URL:hdwrpace */
20388 	case TCP_HDWR_RATE_CAP:			/*  URL:hdwrcap boolean */
20389 	case TCP_PACING_RATE_CAP:		/*  URL:cap  -- used by side-channel */
20390 	case TCP_HDWR_UP_ONLY:			/*  URL:uponly -- hardware pacing  boolean */
20391        /* End pacing related */
20392 	case TCP_FAST_RSM_HACK:			/*  URL:frsm_hack */
20393 	case TCP_DELACK:			/*  URL:delack (in base TCP i.e. tcp_hints along with cc etc ) */
20394 	case TCP_RACK_PRR_SENDALOT:		/*  URL:prr_sendalot */
20395 	case TCP_RACK_MIN_TO:			/*  URL:min_to */
20396 	case TCP_RACK_EARLY_SEG:		/*  URL:early_seg */
20397 	case TCP_RACK_REORD_THRESH:		/*  URL:reord_thresh */
20398 	case TCP_RACK_REORD_FADE:		/*  URL:reord_fade */
20399 	case TCP_RACK_TLP_THRESH:		/*  URL:tlp_thresh */
20400 	case TCP_RACK_PKT_DELAY:		/*  URL:pkt_delay */
20401 	case TCP_RACK_TLP_USE:			/*  URL:tlp_use */
20402 	case TCP_BBR_RACK_RTT_USE:		/*  URL:rttuse */
20403 	case TCP_BBR_USE_RACK_RR:		/*  URL:rackrr */
20404 	case TCP_RACK_DO_DETECTION:		/*  URL:detect */
20405 	case TCP_NO_PRR:			/*  URL:noprr */
20406 	case TCP_TIMELY_DYN_ADJ:      		/*  URL:dynamic */
20407 	case TCP_DATA_AFTER_CLOSE:		/*  no URL */
20408 	case TCP_RACK_NONRXT_CFG_RATE:		/*  URL:nonrxtcr */
20409 	case TCP_SHARED_CWND_ENABLE:		/*  URL:scwnd */
20410 	case TCP_RACK_MBUF_QUEUE:		/*  URL:mqueue */
20411 	case TCP_RACK_NO_PUSH_AT_MAX:		/*  URL:npush */
20412 	case TCP_RACK_PACE_TO_FILL:		/*  URL:fillcw */
20413 	case TCP_SHARED_CWND_TIME_LIMIT:	/*  URL:lscwnd */
20414 	case TCP_RACK_PROFILE:			/*  URL:profile */
20415 	case TCP_USE_CMP_ACKS:			/*  URL:cmpack */
20416 	case TCP_RACK_ABC_VAL:			/*  URL:labc */
20417 	case TCP_REC_ABC_VAL:			/*  URL:reclabc */
20418 	case TCP_RACK_MEASURE_CNT:		/*  URL:measurecnt */
20419 	case TCP_DEFER_OPTIONS:			/*  URL:defer */
20420 	case TCP_RACK_DSACK_OPT:		/*  URL:dsack */
20421 	case TCP_RACK_PACING_BETA:		/*  URL:pacing_beta */
20422 	case TCP_RACK_PACING_BETA_ECN:		/*  URL:pacing_beta_ecn */
20423 	case TCP_RACK_TIMER_SLOP:		/*  URL:timer_slop */
20424 	case TCP_RACK_ENABLE_HYSTART:		/*  URL:hystart */
20425 		break;
20426 	default:
20427 		/* Filter off all unknown options to the base stack */
20428 		return (tcp_default_ctloutput(inp, sopt));
20429 		break;
20430 	}
20431 	INP_WUNLOCK(inp);
20432 	if (sopt->sopt_name == TCP_PACING_RATE_CAP) {
20433 		error = sooptcopyin(sopt, &loptval, sizeof(loptval), sizeof(loptval));
20434 		/*
20435 		 * We truncate it down to 32 bits for the socket-option trace this
20436 		 * means rates > 34Gbps won't show right, but thats probably ok.
20437 		 */
20438 		optval = (uint32_t)loptval;
20439 	} else {
20440 		error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
20441 		/* Save it in 64 bit form too */
20442 		loptval = optval;
20443 	}
20444 	if (error)
20445 		return (error);
20446 	INP_WLOCK(inp);
20447 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
20448 		INP_WUNLOCK(inp);
20449 		return (ECONNRESET);
20450 	}
20451 	if (tp->t_fb != &__tcp_rack) {
20452 		INP_WUNLOCK(inp);
20453 		return (ENOPROTOOPT);
20454 	}
20455 	if (rack->defer_options && (rack->gp_ready == 0) &&
20456 	    (sopt->sopt_name != TCP_DEFER_OPTIONS) &&
20457 	    (sopt->sopt_name != TCP_RACK_PACING_BETA) &&
20458 	    (sopt->sopt_name != TCP_RACK_PACING_BETA_ECN) &&
20459 	    (sopt->sopt_name != TCP_RACK_MEASURE_CNT)) {
20460 		/* Options are beind deferred */
20461 		if (rack_add_deferred_option(rack, sopt->sopt_name, loptval)) {
20462 			INP_WUNLOCK(inp);
20463 			return (0);
20464 		} else {
20465 			/* No memory to defer, fail */
20466 			INP_WUNLOCK(inp);
20467 			return (ENOMEM);
20468 		}
20469 	}
20470 	error = rack_process_option(tp, rack, sopt->sopt_name, optval, loptval);
20471 	INP_WUNLOCK(inp);
20472 	return (error);
20473 }
20474 
20475 static void
20476 rack_fill_info(struct tcpcb *tp, struct tcp_info *ti)
20477 {
20478 
20479 	INP_WLOCK_ASSERT(tp->t_inpcb);
20480 	bzero(ti, sizeof(*ti));
20481 
20482 	ti->tcpi_state = tp->t_state;
20483 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
20484 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
20485 	if (tp->t_flags & TF_SACK_PERMIT)
20486 		ti->tcpi_options |= TCPI_OPT_SACK;
20487 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
20488 		ti->tcpi_options |= TCPI_OPT_WSCALE;
20489 		ti->tcpi_snd_wscale = tp->snd_scale;
20490 		ti->tcpi_rcv_wscale = tp->rcv_scale;
20491 	}
20492 	if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
20493 		ti->tcpi_options |= TCPI_OPT_ECN;
20494 	if (tp->t_flags & TF_FASTOPEN)
20495 		ti->tcpi_options |= TCPI_OPT_TFO;
20496 	/* still kept in ticks is t_rcvtime */
20497 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
20498 	/* Since we hold everything in precise useconds this is easy */
20499 	ti->tcpi_rtt = tp->t_srtt;
20500 	ti->tcpi_rttvar = tp->t_rttvar;
20501 	ti->tcpi_rto = tp->t_rxtcur;
20502 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
20503 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
20504 	/*
20505 	 * FreeBSD-specific extension fields for tcp_info.
20506 	 */
20507 	ti->tcpi_rcv_space = tp->rcv_wnd;
20508 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
20509 	ti->tcpi_snd_wnd = tp->snd_wnd;
20510 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
20511 	ti->tcpi_snd_nxt = tp->snd_nxt;
20512 	ti->tcpi_snd_mss = tp->t_maxseg;
20513 	ti->tcpi_rcv_mss = tp->t_maxseg;
20514 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
20515 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
20516 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
20517 #ifdef NETFLIX_STATS
20518 	ti->tcpi_total_tlp = tp->t_sndtlppack;
20519 	ti->tcpi_total_tlp_bytes = tp->t_sndtlpbyte;
20520 	memcpy(&ti->tcpi_rxsyninfo, &tp->t_rxsyninfo, sizeof(struct tcpsyninfo));
20521 #endif
20522 #ifdef TCP_OFFLOAD
20523 	if (tp->t_flags & TF_TOE) {
20524 		ti->tcpi_options |= TCPI_OPT_TOE;
20525 		tcp_offload_tcp_info(tp, ti);
20526 	}
20527 #endif
20528 }
20529 
20530 static int
20531 rack_get_sockopt(struct inpcb *inp, struct sockopt *sopt)
20532 {
20533 	struct tcpcb *tp;
20534 	struct tcp_rack *rack;
20535 	int32_t error, optval;
20536 	uint64_t val, loptval;
20537 	struct	tcp_info ti;
20538 	/*
20539 	 * Because all our options are either boolean or an int, we can just
20540 	 * pull everything into optval and then unlock and copy. If we ever
20541 	 * add a option that is not a int, then this will have quite an
20542 	 * impact to this routine.
20543 	 */
20544 	error = 0;
20545 	tp = intotcpcb(inp);
20546 	rack = (struct tcp_rack *)tp->t_fb_ptr;
20547 	if (rack == NULL) {
20548 		INP_WUNLOCK(inp);
20549 		return (EINVAL);
20550 	}
20551 	switch (sopt->sopt_name) {
20552 	case TCP_INFO:
20553 		/* First get the info filled */
20554 		rack_fill_info(tp, &ti);
20555 		/* Fix up the rtt related fields if needed */
20556 		INP_WUNLOCK(inp);
20557 		error = sooptcopyout(sopt, &ti, sizeof ti);
20558 		return (error);
20559 	/*
20560 	 * Beta is the congestion control value for NewReno that influences how
20561 	 * much of a backoff happens when loss is detected. It is normally set
20562 	 * to 50 for 50% i.e. the cwnd is reduced to 50% of its previous value
20563 	 * when you exit recovery.
20564 	 */
20565 	case TCP_RACK_PACING_BETA:
20566 		if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0)
20567 			error = EINVAL;
20568 		else if (rack->rc_pacing_cc_set == 0)
20569 			optval = rack->r_ctl.rc_saved_beta.beta;
20570 		else {
20571 			/*
20572 			 * Reach out into the CC data and report back what
20573 			 * I have previously set. Yeah it looks hackish but
20574 			 * we don't want to report the saved values.
20575 			 */
20576 			if (tp->ccv->cc_data)
20577 				optval = ((struct newreno *)tp->ccv->cc_data)->beta;
20578 			else
20579 				error = EINVAL;
20580 		}
20581 		break;
20582 		/*
20583 		 * Beta_ecn is the congestion control value for NewReno that influences how
20584 		 * much of a backoff happens when a ECN mark is detected. It is normally set
20585 		 * to 80 for 80% i.e. the cwnd is reduced by 20% of its previous value when
20586 		 * you exit recovery. Note that classic ECN has a beta of 50, it is only
20587 		 * ABE Ecn that uses this "less" value, but we do too with pacing :)
20588 		 */
20589 
20590 	case TCP_RACK_PACING_BETA_ECN:
20591 		if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0)
20592 			error = EINVAL;
20593 		else if (rack->rc_pacing_cc_set == 0)
20594 			optval = rack->r_ctl.rc_saved_beta.beta_ecn;
20595 		else {
20596 			/*
20597 			 * Reach out into the CC data and report back what
20598 			 * I have previously set. Yeah it looks hackish but
20599 			 * we don't want to report the saved values.
20600 			 */
20601 			if (tp->ccv->cc_data)
20602 				optval = ((struct newreno *)tp->ccv->cc_data)->beta_ecn;
20603 			else
20604 				error = EINVAL;
20605 		}
20606 		break;
20607 	case TCP_RACK_DSACK_OPT:
20608 		optval = 0;
20609 		if (rack->rc_rack_tmr_std_based) {
20610 			optval |= 1;
20611 		}
20612 		if (rack->rc_rack_use_dsack) {
20613 			optval |= 2;
20614 		}
20615 		break;
20616  	case TCP_RACK_ENABLE_HYSTART:
20617 	{
20618 		if (tp->ccv->flags & CCF_HYSTART_ALLOWED) {
20619 			optval = RACK_HYSTART_ON;
20620 			if (tp->ccv->flags & CCF_HYSTART_CAN_SH_CWND)
20621 				optval = RACK_HYSTART_ON_W_SC;
20622 			if (tp->ccv->flags & CCF_HYSTART_CONS_SSTH)
20623 				optval = RACK_HYSTART_ON_W_SC_C;
20624 		} else {
20625 			optval = RACK_HYSTART_OFF;
20626 		}
20627 	}
20628 	break;
20629 	case TCP_FAST_RSM_HACK:
20630 		optval = rack->fast_rsm_hack;
20631 		break;
20632 	case TCP_DEFER_OPTIONS:
20633 		optval = rack->defer_options;
20634 		break;
20635 	case TCP_RACK_MEASURE_CNT:
20636 		optval = rack->r_ctl.req_measurements;
20637 		break;
20638 	case TCP_REC_ABC_VAL:
20639 		optval = rack->r_use_labc_for_rec;
20640 		break;
20641 	case TCP_RACK_ABC_VAL:
20642 		optval = rack->rc_labc;
20643 		break;
20644 	case TCP_HDWR_UP_ONLY:
20645 		optval= rack->r_up_only;
20646 		break;
20647 	case TCP_PACING_RATE_CAP:
20648 		loptval = rack->r_ctl.bw_rate_cap;
20649 		break;
20650 	case TCP_RACK_PROFILE:
20651 		/* You cannot retrieve a profile, its write only */
20652 		error = EINVAL;
20653 		break;
20654 	case TCP_USE_CMP_ACKS:
20655 		optval = rack->r_use_cmp_ack;
20656 		break;
20657 	case TCP_RACK_PACE_TO_FILL:
20658 		optval = rack->rc_pace_to_cwnd;
20659 		if (optval && rack->r_fill_less_agg)
20660 			optval++;
20661 		break;
20662 	case TCP_RACK_NO_PUSH_AT_MAX:
20663 		optval = rack->r_ctl.rc_no_push_at_mrtt;
20664 		break;
20665 	case TCP_SHARED_CWND_ENABLE:
20666 		optval = rack->rack_enable_scwnd;
20667 		break;
20668 	case TCP_RACK_NONRXT_CFG_RATE:
20669 		optval = rack->rack_rec_nonrxt_use_cr;
20670 		break;
20671 	case TCP_NO_PRR:
20672 		if (rack->rack_no_prr  == 1)
20673 			optval = 1;
20674 		else if (rack->no_prr_addback == 1)
20675 			optval = 2;
20676 		else
20677 			optval = 0;
20678 		break;
20679 	case TCP_RACK_DO_DETECTION:
20680 		optval = rack->do_detection;
20681 		break;
20682 	case TCP_RACK_MBUF_QUEUE:
20683 		/* Now do we use the LRO mbuf-queue feature */
20684 		optval = rack->r_mbuf_queue;
20685 		break;
20686 	case TCP_TIMELY_DYN_ADJ:
20687 		optval = rack->rc_gp_dyn_mul;
20688 		break;
20689 	case TCP_BBR_IWINTSO:
20690 		optval = rack->rc_init_win;
20691 		break;
20692 	case TCP_RACK_TLP_REDUCE:
20693 		/* RACK TLP cwnd reduction (bool) */
20694 		optval = rack->r_ctl.rc_tlp_cwnd_reduce;
20695 		break;
20696 	case TCP_BBR_RACK_INIT_RATE:
20697 		val = rack->r_ctl.init_rate;
20698 		/* convert to kbits per sec */
20699 		val *= 8;
20700 		val /= 1000;
20701 		optval = (uint32_t)val;
20702 		break;
20703 	case TCP_RACK_FORCE_MSEG:
20704 		optval = rack->rc_force_max_seg;
20705 		break;
20706 	case TCP_RACK_PACE_MAX_SEG:
20707 		/* Max segments in a pace */
20708 		optval = rack->rc_user_set_max_segs;
20709 		break;
20710 	case TCP_RACK_PACE_ALWAYS:
20711 		/* Use the always pace method */
20712 		optval = rack->rc_always_pace;
20713 		break;
20714 	case TCP_RACK_PRR_SENDALOT:
20715 		/* Allow PRR to send more than one seg */
20716 		optval = rack->r_ctl.rc_prr_sendalot;
20717 		break;
20718 	case TCP_RACK_MIN_TO:
20719 		/* Minimum time between rack t-o's in ms */
20720 		optval = rack->r_ctl.rc_min_to;
20721 		break;
20722 	case TCP_RACK_EARLY_SEG:
20723 		/* If early recovery max segments */
20724 		optval = rack->r_ctl.rc_early_recovery_segs;
20725 		break;
20726 	case TCP_RACK_REORD_THRESH:
20727 		/* RACK reorder threshold (shift amount) */
20728 		optval = rack->r_ctl.rc_reorder_shift;
20729 		break;
20730 	case TCP_RACK_REORD_FADE:
20731 		/* Does reordering fade after ms time */
20732 		optval = rack->r_ctl.rc_reorder_fade;
20733 		break;
20734 	case TCP_BBR_USE_RACK_RR:
20735 		/* Do we use the rack cheat for rxt */
20736 		optval = rack->use_rack_rr;
20737 		break;
20738 	case TCP_RACK_RR_CONF:
20739 		optval = rack->r_rr_config;
20740 		break;
20741 	case TCP_HDWR_RATE_CAP:
20742 		optval = rack->r_rack_hw_rate_caps;
20743 		break;
20744 	case TCP_BBR_HDWR_PACE:
20745 		optval = rack->rack_hdw_pace_ena;
20746 		break;
20747 	case TCP_RACK_TLP_THRESH:
20748 		/* RACK TLP theshold i.e. srtt+(srtt/N) */
20749 		optval = rack->r_ctl.rc_tlp_threshold;
20750 		break;
20751 	case TCP_RACK_PKT_DELAY:
20752 		/* RACK added ms i.e. rack-rtt + reord + N */
20753 		optval = rack->r_ctl.rc_pkt_delay;
20754 		break;
20755 	case TCP_RACK_TLP_USE:
20756 		optval = rack->rack_tlp_threshold_use;
20757 		break;
20758 	case TCP_RACK_PACE_RATE_CA:
20759 		optval = rack->r_ctl.rc_fixed_pacing_rate_ca;
20760 		break;
20761 	case TCP_RACK_PACE_RATE_SS:
20762 		optval = rack->r_ctl.rc_fixed_pacing_rate_ss;
20763 		break;
20764 	case TCP_RACK_PACE_RATE_REC:
20765 		optval = rack->r_ctl.rc_fixed_pacing_rate_rec;
20766 		break;
20767 	case TCP_RACK_GP_INCREASE_SS:
20768 		optval = rack->r_ctl.rack_per_of_gp_ca;
20769 		break;
20770 	case TCP_RACK_GP_INCREASE_CA:
20771 		optval = rack->r_ctl.rack_per_of_gp_ss;
20772 		break;
20773 	case TCP_BBR_RACK_RTT_USE:
20774 		optval = rack->r_ctl.rc_rate_sample_method;
20775 		break;
20776 	case TCP_DELACK:
20777 		optval = tp->t_delayed_ack;
20778 		break;
20779 	case TCP_DATA_AFTER_CLOSE:
20780 		optval = rack->rc_allow_data_af_clo;
20781 		break;
20782 	case TCP_SHARED_CWND_TIME_LIMIT:
20783 		optval = rack->r_limit_scw;
20784 		break;
20785 	case TCP_RACK_TIMER_SLOP:
20786 		optval = rack->r_ctl.timer_slop;
20787 		break;
20788 	default:
20789 		return (tcp_default_ctloutput(inp, sopt));
20790 		break;
20791 	}
20792 	INP_WUNLOCK(inp);
20793 	if (error == 0) {
20794 		if (TCP_PACING_RATE_CAP)
20795 			error = sooptcopyout(sopt, &loptval, sizeof loptval);
20796 		else
20797 			error = sooptcopyout(sopt, &optval, sizeof optval);
20798 	}
20799 	return (error);
20800 }
20801 
20802 static int
20803 rack_ctloutput(struct inpcb *inp, struct sockopt *sopt)
20804 {
20805 	if (sopt->sopt_dir == SOPT_SET) {
20806 		return (rack_set_sockopt(inp, sopt));
20807 	} else if (sopt->sopt_dir == SOPT_GET) {
20808 		return (rack_get_sockopt(inp, sopt));
20809 	} else {
20810 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
20811 	}
20812 }
20813 
20814 static const char *rack_stack_names[] = {
20815 	__XSTRING(STACKNAME),
20816 #ifdef STACKALIAS
20817 	__XSTRING(STACKALIAS),
20818 #endif
20819 };
20820 
20821 static int
20822 rack_ctor(void *mem, int32_t size, void *arg, int32_t how)
20823 {
20824 	memset(mem, 0, size);
20825 	return (0);
20826 }
20827 
20828 static void
20829 rack_dtor(void *mem, int32_t size, void *arg)
20830 {
20831 
20832 }
20833 
20834 static bool rack_mod_inited = false;
20835 
20836 static int
20837 tcp_addrack(module_t mod, int32_t type, void *data)
20838 {
20839 	int32_t err = 0;
20840 	int num_stacks;
20841 
20842 	switch (type) {
20843 	case MOD_LOAD:
20844 		rack_zone = uma_zcreate(__XSTRING(MODNAME) "_map",
20845 		    sizeof(struct rack_sendmap),
20846 		    rack_ctor, rack_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
20847 
20848 		rack_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb",
20849 		    sizeof(struct tcp_rack),
20850 		    rack_ctor, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
20851 
20852 		sysctl_ctx_init(&rack_sysctl_ctx);
20853 		rack_sysctl_root = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
20854 		    SYSCTL_STATIC_CHILDREN(_net_inet_tcp),
20855 		    OID_AUTO,
20856 #ifdef STACKALIAS
20857 		    __XSTRING(STACKALIAS),
20858 #else
20859 		    __XSTRING(STACKNAME),
20860 #endif
20861 		    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
20862 		    "");
20863 		if (rack_sysctl_root == NULL) {
20864 			printf("Failed to add sysctl node\n");
20865 			err = EFAULT;
20866 			goto free_uma;
20867 		}
20868 		rack_init_sysctls();
20869 		num_stacks = nitems(rack_stack_names);
20870 		err = register_tcp_functions_as_names(&__tcp_rack, M_WAITOK,
20871 		    rack_stack_names, &num_stacks);
20872 		if (err) {
20873 			printf("Failed to register %s stack name for "
20874 			    "%s module\n", rack_stack_names[num_stacks],
20875 			    __XSTRING(MODNAME));
20876 			sysctl_ctx_free(&rack_sysctl_ctx);
20877 free_uma:
20878 			uma_zdestroy(rack_zone);
20879 			uma_zdestroy(rack_pcb_zone);
20880 			rack_counter_destroy();
20881 			printf("Failed to register rack module -- err:%d\n", err);
20882 			return (err);
20883 		}
20884 		tcp_lro_reg_mbufq();
20885 		rack_mod_inited = true;
20886 		break;
20887 	case MOD_QUIESCE:
20888 		err = deregister_tcp_functions(&__tcp_rack, true, false);
20889 		break;
20890 	case MOD_UNLOAD:
20891 		err = deregister_tcp_functions(&__tcp_rack, false, true);
20892 		if (err == EBUSY)
20893 			break;
20894 		if (rack_mod_inited) {
20895 			uma_zdestroy(rack_zone);
20896 			uma_zdestroy(rack_pcb_zone);
20897 			sysctl_ctx_free(&rack_sysctl_ctx);
20898 			rack_counter_destroy();
20899 			rack_mod_inited = false;
20900 		}
20901 		tcp_lro_dereg_mbufq();
20902 		err = 0;
20903 		break;
20904 	default:
20905 		return (EOPNOTSUPP);
20906 	}
20907 	return (err);
20908 }
20909 
20910 static moduledata_t tcp_rack = {
20911 	.name = __XSTRING(MODNAME),
20912 	.evhand = tcp_addrack,
20913 	.priv = 0
20914 };
20915 
20916 MODULE_VERSION(MODNAME, 1);
20917 DECLARE_MODULE(MODNAME, tcp_rack, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
20918 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1);
20919